persisting data within a module
I'm having trouble understanding how namespaces work in modules. I want to execute a module within the interpreter and then have values that are calculated persist so that other modules that get executed can retrieve them. For example, consider the two simple modules below. The first method fails and I'm not sure exactly why. (Note: assume one instance of an interpreter. In my case a 3rd party software tool that starts an interpreter when it launches). Two alternate ways of running it: 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver using the conditional statement if (__name__=="__main__"): test.py run2.py or, 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver. run.py _test.py__ import sys,os A = 0 def getA(): global A return A def run(): global A A = 10 if (__name__=="__main__"): run() _run.py__ import test test.run() print "A = " + str(test.getA()) _run2.py__ import test print "A = " + str(test.getA()) -- Peter Bismuti Boeing Information Technology Renton, WA (425) 234-0873 W (425) 442-7775 C -- http://mail.python.org/mailman/listinfo/python-list
Re: Distributed RVS, Darcs, tech love
Be nice. > Boy, you really have to get a clue. -- http://mail.python.org/mailman/listinfo/python-list
Re: persisting data within a module
I'm not sure how to better state my question than to post my code. The question boils down to which namespace to variable in the module (in this case A) end up in depending on whether or not the module is simply imported by another module which acts as the driver (run.py is __main__), or when the module drives itself (test.py __main__). For example: my guess is that in the first case A ends up in the namespace of test and could be referenced by test.A, in the second case A ends up in the global namespace (and therefore must be referred to as simply A by other modules). Can anyone please shed some light on this for me? Thanks > > I'm having trouble understanding how namespaces work in modules. I want > > to execute a module within the interpreter and then have values that are > > calculated persist so that other modules that get executed can retrieve > > them. > > Modules retain state their state across all imports in the same > interpreter instance. Module state is not shared among different > instances of the interpreter. > > > For example, consider the two simple modules below. The first method > > fails and I'm not sure exactly why. (Note: assume one instance of an > > interpreter. In my case a 3rd party software tool that starts an > > interpreter when it launches). > > > > Two alternate ways of running it: > > > > 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver > > using the conditional statement if (__name__=="__main__"): > > > > test.py > > run2.py > > Ok, what do you mean by this? Do you mean run test.py and then run > run2.py? In so, then you will have *two* instances -- one for each > file being executed. You can only have one main module per > interpreter instance. I suspect this is the source of your confusion. > > > or, > > > > 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver. > > > > run.py > > > > _test.py__ > > > > import sys,os > > > > A = 0 > > > > def getA(): > > global A > > return A > > > > def run(): > > global A > > A = 10 > > > > if (__name__=="__main__"): > > run() > > Here, A is only initialized when the module is loaded iff it is the > main module. If it's not the main module, then it will have A set to > 0 until some other code calls run(). > > > _run.py__ > > > > import test > > > > test.run() > > print "A = " + str(test.getA()) > > This code calls test.run(), which is necessary for A to be 10. > > > _run2.py__ > > > > import test > > > > print "A = " + str(test.getA()) > > > > -- > > This code gets the value of test.A without calling test.run(). Since > test.run() was not called, A is the value it was initialized when the > test module was loaded -- namely, 0. > > Hope this helps, > > --Nathan Davis -- Peter Bismuti Boeing Information Technology Renton, WA (425) 234-0873 W (425) 442-7775 C -- http://mail.python.org/mailman/listinfo/python-list
how can I interpret a file within the interactive interpreter
I want to interpret a file (or whatever you call it) and then keep the interactive interpreter alive so I can then continue to issue commands. How can this be done? I saw online a -m flag but it does not seem to work. Thanks -- Peter Bismuti Boeing -- http://mail.python.org/mailman/listinfo/python-list
Re: persisting data within a module
How is that state different depending on whether a module has been simply imported (#2. some other block of code has __name__ == "__main__") and the script itself being run (#1. and having __name__=="__main__")? Ultimately, what I want is for a module to remember (persist) the value of A, regardless of how the module has been loaded into the interpreter. Thanks > Modules retain state their state across all imports in the same > interpreter instance. Module state is not shared among different > instances of the interpreter. > > > For example, consider the two simple modules below. The first method > > fails and I'm not sure exactly why. (Note: assume one instance of an > > interpreter. In my case a 3rd party software tool that starts an > > interpreter when it launches). > > > > Two alternate ways of running it: > > > > 1. (FAILS: RESULTS A = 0) Use the module "test" itself as the driver > > using the conditional statement if (__name__=="__main__"): > > > > test.py > > run2.py > > Ok, what do you mean by this? Do you mean run test.py and then run > run2.py? In so, then you will have *two* instances -- one for each > file being executed. You can only have one main module per > interpreter instance. I suspect this is the source of your confusion. > > > or, > > > > 2. (SUCCES: RESULTS A = 10) Use "run.py" as the driver. > > > > run.py > > > > _test.py__ > > > > import sys,os > > > > A = 0 > > > > def getA(): > > global A > > return A > > > > def run(): > > global A > > A = 10 > > > > if (__name__=="__main__"): > > run() > > Here, A is only initialized when the module is loaded iff it is the > main module. If it's not the main module, then it will have A set to > 0 until some other code calls run(). > > > _run.py__ > > > > import test > > > > test.run() > > print "A = " + str(test.getA()) > > This code calls test.run(), which is necessary for A to be 10. > > > _run2.py__ > > > > import test > > > > print "A = " + str(test.getA()) > > > > -- > > This code gets the value of test.A without calling test.run(). Since > test.run() was not called, A is the value it was initialized when the > test module was loaded -- namely, 0. > > Hope this helps, > > --Nathan Davis -- Peter Bismuti Boeing Information Technology Renton, WA (425) 234-0873 W (425) 442-7775 C -- http://mail.python.org/mailman/listinfo/python-list
module data member?
How do you define a "module data member" (I want to understand out how this works before making converting to a Class)? Right now I'm defining variables in a module that get put into the global namespace. Instead I want to put them in a "module global" namespace that will be the same regardless of whether not this module is imported or run as __main__. Thanks PS Sorry for the near redundant posts. I'm simply trying to refine my question as I get a better understanding of what I'm confused about. Thanks -- Peter Bismuti Boeing -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I interpret a file within the interactive interpreter
Still can't get the -m flat to work. Perhaps this feature is not in the version I am using? Thanks. -bash-3.00$ python -m test Unknown option: -m usage: python [option] ... [-c cmd | file | -] [arg] ... Try `python -h' for more information. -bash-3.00$ python Python 2.3.4 (#1, Jan 9 2007, 16:40:09) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> > Peter J. Bismuti wrote: > > I want to interpret a file (or whatever you call it) and then keep the > > interactive interpreter alive so I can then continue to issue commands. > > That's what the -i option is for. > > > How can this be done? I saw online a -m flag but it does not seem to > > work. > > -m is used to load a module via python's import mechanism. > > $ echo 'print "Hello from", __name__' > tmp.py > > $ python -m tmp > Hello from __main__ > > $ python -i tmp.py > Hello from __main__ > > > You can combine both options: > > $ python -i -m tmp > Hello from __main__ > > > Peter -- Peter Bismuti Boeing Information Technology Renton, WA (425) 234-0873 W (425) 442-7775 C -- http://mail.python.org/mailman/listinfo/python-list
Re: module data member?
This did the trick for the most part, but it still leaves a copy of the variable A in the "non-module" global namespace (see output below). I want A declared global to the module (so as not to be local within the functions of the module) but not visible outside of the module namespace (like B, see below). How can this be done? Thanks again python -i test.py :>>> print test.A 10 :>>> print A 0 :>>> print B Traceback (most recent call last): File "", line 1, in ? NameError: name 'B' is not defined test.py___ A = 0 def getA(): global A return A def run(): global A A = 10 if (__name__=="__main__"): import test test.run() __ > def main(): > # do mainy stuff here > > if __name__ == "__main__": >import myself >myself.main() > > This of course won't prevent __main__ to be existant, nor running possible > initialization code twice. But if the module is supposed to do some work, > it will operate on the one data all the other importing modules see. > > You can take this one step further, using an explicit init()-function that > creates global state (remember, it's only module-global). > > Diez -- Peter Bismuti Boeing Information Technology Renton, WA (425) 234-0873 W (425) 442-7775 C -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] The Jew Spam on this list: Blocking the IP address of spammers
Why aren't these spammers having their IP addresses blocked (or something like that)? People making these posts should not be allowed to post ever again. Is there not someone administering this mailing list? > I agree! > > On Nov 13, 2007 1:26 PM, Martin Marcher <[EMAIL PROTECTED]> wrote: > > Hello, > > > > please do not respond to the "political" spam on this list anymore. > > Rather report it as spam to your provider/anti-spam-measures or report > > it to the listmasters (if you have the feeling that it helps, I guess > > they're already on this issue). > > > > I understand that this might be a heated topic but people please it's > > "just spam" and every message regarding this topic is spam too (funny > > enough, so is this message) please just add this stuff to your > > killfile or whatever you use. > > > > thanks > > martin > > > > PS: if you must discuss this opinion with me answer to me off list as > > I guess most people just aren't interested... > > > > -- > > http://noneisyours.marcher.name > > http://feeds.feedburner.com/NoneIsYours > > -- > > http://mail.python.org/mailman/listinfo/python-list -- Peter Bismuti Boeing Information Technology Renton, WA -- http://mail.python.org/mailman/listinfo/python-list
accessing attributes when inheriting?
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?! ... #!/usr/bin/python from Dialog import Dialog import enscmd class RatDialog(Dialog): def __init__(self,parent = Dialog,name = "RatDialog",modal = 0,fl = 0): Dialog.__init__(self) self.ClipListView.header().setLabel(0,self.__tr("Clips"))