Bug in Python class static variable?
I am trying to define a class static variable. But the value of the static variable seems to be only defined inside the file that the class is declared. See the code below. When I run "python w.py", I got: 000===> Hello World 001===> Hello World 002===> Not Initialized 003===> Not Initialized 004===> Not Initialized 005===> Hello World Looks like even though the class variable "ClassW.w_classStaticVar" was set inside "startup()", the value never carried over to functions in "a.py". Is this a bug in Python's static variable handling. I am running Python 2.4.4. Ben #= file: w.py from a import * class ClassW: w_classStaticVar = "Not Initialized" def initA(self): print "001===>", ClassW.w_classStaticVar obj = ClassA() obj.init2() print "005===>", ClassW.w_classStaticVar def startup(): ClassW.w_classStaticVar = "Hello World" wo = ClassW() print "000===>", ClassW.w_classStaticVar wo.initA() if __name__ == '__main__': startup() #= file: a.py from w import * class ClassA: def __init__(self): print "002===>", ClassW.w_classStaticVar def init2(self): print "003===>", ClassW.w_classStaticVar w = ClassW() print "004===>", ClassW.w_classStaticVar -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python class static variable?
On Jul 2, 3:52 am, Duncan Booth <[EMAIL PROTECTED]> wrote: > Bruza <[EMAIL PROTECTED]> wrote: > > I am trying to define a class static variable. But the value of the > > static variable seems to be only defined inside the file that the > > class is declared. See the code below. When I run "python w.py", I > > got: > > When you run "python w.py" the *script* w.py is loaded as the module > __main__. Importing a module called 'w' creates a new module which is > unrelated to __main__. If you want access to variables defined in the main > script then you need to import __main__. > > Don't use 'from module import *': > > The import statements are executed when the interpreter reaches them in the > source. Even if you fix your code to import from __main__, the values you > try to import from __main__ won't exist when the import statement executes: > the first 'from a import *' will load and execute all of module 'a', but > when that executes 'from __main__ import *' it just imports names defined > in the main script *before* a was imported. > > In general, don't try to do this: put all your classes into modules and > just put minimal startup code into a script. Duncan, Thanks for replying. However, I am still confused... Even if I put "from __main__ import *" in both "a.py" and "w.py", I still got the same results. So, how should I re-structure my program to make the class static variable works??? Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python class static variable?
On Jul 2, 1:21 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > Bruza <[EMAIL PROTECTED]> wrote: > > On Jul 2, 3:52 am, Duncan Booth <[EMAIL PROTECTED]> wrote: > >> Bruza <[EMAIL PROTECTED]> wrote: > >> > I am trying to define a class static variable. But the value of the > >> > static variable seems to be only defined inside the file that the > >> > class is declared. See the code below. When I run "python w.py", I > >> > got: > > >> When you run "python w.py" the *script* w.py is loaded as the module > >> __main__. Importing a module called 'w' creates a new module which is > >> unrelated to __main__. If you want access to variables defined in the > >> main script then you need to import __main__. > > >> Don't use 'from module import *': > > >> The import statements are executed when the interpreter reaches them > >> in the source. Even if you fix your code to import from __main__, the > >> values you try to import from __main__ won't exist when the import > >> statement executes: the first 'from a import *' will load and execute > >> all of module 'a', but when that executes 'from __main__ import *' it > >> just imports names defined in the main script *before* a was > >> imported. > > >> In general, don't try to do this: put all your classes into modules > >> and just put minimal startup code into a script. > > > Duncan, > > > Thanks for replying. However, I am still confused... > > Even if I put "from __main__ import *" in both "a.py" and "w.py", I > > still got > > the same results. So, how should I re-structure my program to make the > > class > > static variable works??? > > Option 1: > In w.py put: > > import a > > and then refer to a.ClassA > > In a.py put: > > import __main__ > > and then refer to __main__.ClassW > > But better, option 2: > > Create a new file script.py which contains: > > import w > if __name__=='__main__': > w.startup() > > then in a use 'import w' and in w use 'import a' and refer to a.ClassA and > w.ClassW as above. > > Try to think through the order in which Python interprets your code: > remember everything is interpreted. Both 'import' statements and 'class' > statements are really just variation on an assignment, so none of the names > exist until the lines which declare them have been executed. A line 'import > a' is roughly the same as: > >a = __import__(something) > > and a statement such as 'class ClassA: whatever' is roughly the same as: > > ClassA = type('ClassA', baseclasses, dict) > > You should never attempt to use the 'from X import *' form of import when > you have modules which include each other (you might get away with it if > you move the imports to the end of the module instead of the beginning, but > it is much simpler just to import the modules, and good practice in any > case). Duncan, Thanks for the reply; both approaches worked!! Looks like the problem is because of "ClassW" is defined in the default "__main__" module, not the "w" module as I thought. I also figured out a 3rd approach adding the following code in "w.py". The idea is that I force the program to import "w.py" as "w" if "w.py" is loaded into "__main__". Then I can run the qualified "w.startup()". And it also works :-)... if __name__ == '__main__': import w w.startup() -- http://mail.python.org/mailman/listinfo/python-list
implement random selection in Python
I need to implement a "random selection" algorithm which takes a list of [(obj, prob),...] as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of "prob".To simplify the problem, assuming "prob" are integers, and the sum of all "prob" equals 100. For example, items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] The algorithm will take a number "N", and a [(obj, prob),...] list as inputs, and randomly pick "N" objects based on the probabilities of the objects in the list. For N=1 this is pretty simply; the following code is sufficient to do the job. def foo(items): index = random.randint(0, 99) currentP = 0 for (obj, p) in items: currentP += w if currentP > index: return obj But how about the general case, for N > 1 and N < len(items)? Is there some clever algorithm using Python standard "random" package to do the trick? Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: implement random selection in Python
On Nov 15, 9:32 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Nov 15, 10:40�pm, Bruza <[EMAIL PROTECTED]> wrote: > > > > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > likely an object, "obj", should be selected based on its probability > > of > > "prob".To simplify the problem, assuming "prob" are integers, and the > > sum of all "prob" equals 100. For example, > > > � �items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > > The algorithm will take a number "N", and a [(obj, prob),...] list as > > inputs, and randomly pick "N" objects based on the probabilities of > > the > > objects in the list. > > > For N=1 this is pretty simply; the following code is sufficient to do > > the job. > > > def foo(items): > > � � index = random.randint(0, 99) > > � � currentP = 0 > > � � for (obj, p) in items: > > � � � �currentP += w > > � � � �if currentP > index: > > � � � � � return obj > > > But how about the general case, for N > 1 and N < len(items)? Is there > > some clever algorithm using Python standard "random" package to do > > the trick? > > > Thanks, > > > Ben > > What do you think of this? > > import random > N = 100 > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > the_items = [] > for i,j in items: > the_items.extend([i]*j) > histogram = {} > for i in xrange(N): > chosen = random.choice(the_items) > print chosen, > if chosen in histogram: > histogram[chosen] += 1 > else: > histogram[chosen] = 1 > print > print > for i in histogram: > print '%4s: %d' % (i,histogram[i]) > > ## John Mary Jane Tom Tom Mary Mary Tom John John Tom John Tom > ## John Mary Mary Mary John Tom Tom John Mary Mary Tom Mary > ## Mary John Tom Jane Jane Jane John Tom Jane Tom Tom John Tom > ## Tom Mary Tom Tom Mary Tom Mary Tom Tom Tom Tom Mary Mary Tom > ## Mary Tom Mary Tom Tom Jane Tom Mary Tom Jane Tom Tom Tom Tom > ## Tom Mary Tom Jane Tom Mary Mary Jane Mary John Mary Mary Tom > ## Mary Mary Tom Mary John Tom Tom Tom Tom Mary Jane Mary Tom > ## Mary Tom Tom Jane Tom Mary Mary Tom > ## > ## Jane: 11 > ## John: 12 > ## Mary: 32 > ## Tom: 45 No. That does not solve the problem. What I want is a function def randomPick(n, the_items): which will return n DISTINCT items from "the_items" such that the n items returned are according to their probabilities specified in the (item, pro) elements inside "the_items". If I understand correctly, both of the previous replies will output one item at a time independently, instead of returning n DISTINCT items at a time. -- http://mail.python.org/mailman/listinfo/python-list
Re: implement random selection in Python
On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> wrote: > Bruza wrote: > > I need to implement a "random selection" algorithm which takes a list > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > likely an object, "obj", should be selected based on its probability > > of > > "prob".To simplify the problem, assuming "prob" are integers, and the > > sum of all "prob" equals 100. For example, > > >items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > > The algorithm will take a number "N", and a [(obj, prob),...] list as > > inputs, and randomly pick "N" objects based on the probabilities of > > the > > objects in the list. > > > For N=1 this is pretty simply; the following code is sufficient to do > > the job. > > > def foo(items): > > index = random.randint(0, 99) > > currentP = 0 > > for (obj, p) in items: > >currentP += w > >if currentP > index: > > return obj > > > But how about the general case, for N > 1 and N < len(items)? Is there > > some clever algorithm using Python standard "random" package to do > > the trick? > > I think you need to clarify what you want to do. The "probs" are > clearly not probabilities. Are they counts of items? Are you then > sampling without replacement? When you say N < len(items) do you mean N > <= sum of the "probs"? > > Duncabn I think I need to explain on the probability part: the "prob" is a relative likelihood that the object will be included in the output list. So, in my example input of items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] So, for any size of N, 'Tom' (with prob of 45) will be more likely to be included in the output list of N distinct member than 'Mary' (prob of 30) and much more likely than that of 'John' (with prob of 10). I know "prob" is not exactly the "probability" in the context of returning a multiple member list. But what I want is a way to "favor" some member in a selection process. So far, only Boris's solution is closest (but not quite) to what I need, which returns a list of N distinct object from the input "items". However, I tried with input of items = [('Mary',1), ('John', 1), ('Tom', 1), ('Jane', 97)] and have a repeated calling of Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: implement random selection in Python
On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote: > On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]> > wrote: > > > > > Bruza wrote: > > > I need to implement a "random selection" algorithm which takes a list > > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how > > > likely an object, "obj", should be selected based on its probability > > > of > > > "prob".To simplify the problem, assuming "prob" are integers, and the > > > sum of all "prob" equals 100. For example, > > > >items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > > > The algorithm will take a number "N", and a [(obj, prob),...] list as > > > inputs, and randomly pick "N" objects based on the probabilities of > > > the > > > objects in the list. > > > > For N=1 this is pretty simply; the following code is sufficient to do > > > the job. > > > > def foo(items): > > > index = random.randint(0, 99) > > > currentP = 0 > > > for (obj, p) in items: > > >currentP += w > > >if currentP > index: > > > return obj > > > > But how about the general case, for N > 1 and N < len(items)? Is there > > > some clever algorithm using Python standard "random" package to do > > > the trick? > > > I think you need to clarify what you want to do. The "probs" are > > clearly not probabilities. Are they counts of items? Are you then > > sampling without replacement? When you say N < len(items) do you mean N > > <= sum of the "probs"? > > > Duncabn > > I think I need to explain on the probability part: the "prob" is a > relative likelihood that the object will be included in the output > list. So, in my example input of > > items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)] > > So, for any size of N, 'Tom' (with prob of 45) will be more likely to > be included in the output list of N distinct member than 'Mary' (prob > of 30) and much more likely than that of 'John' (with prob of 10). > > I know "prob" is not exactly the "probability" in the context of > returning a multiple member list. But what I want is a way to "favor" > some member in a selection process. > > So far, only Boris's solution is closest (but not quite) to what I > need, which returns a list of N distinct object from the input > "items". However, I tried with input of > >items = [('Mary',1), ('John', 1), ('Tom', 1), ('Jane', 97)] > > and have a repeated calling of > > Ben OOPS. I pressed the Send too fast. The problem w/ Boris's solution is that after repeated calling of randomPick(3,items), 'Jane' is not the most "frequent appearing" member in all the out list of 3 member lists... -- http://mail.python.org/mailman/listinfo/python-list
MySQL_python install failed on Ubuntu 7.1
I installed MySQL 5.0.45 on Ubuntu 7.1 and download MySQL_python from Sourceforge (http://sourceforge.net/project/showfiles.php? group_id=22307). Then I untar the package and executed "python setup.py install". But I got compilation errors (see part of the failed messages below). Looks like the installation tried to compile the _mysql extension and failed. Anybody knows a solution to this problem? Thanks, Bruza == running install running bdist_egg running egg_info writing MySQL_python.egg-info/PKG-INFO writing top-level names to MySQL_python.egg-info/top_level.txt writing dependency_links to MySQL_python.egg-info/dependency_links.txt reading manifest file 'MySQL_python.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'MySQL_python.egg-info/SOURCES.txt' installing library code to build/bdist.linux-i686/egg running install_lib running build_py copying MySQLdb/release.py -> build/lib.linux-i686-2.5/MySQLdb running build_ext building '_mysql' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O2 -Wall -Wstrict- prototypes -fPIC -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 - I/usr/include/mysql -I/usr/include/python2.5 -c _mysql.c -o build/ temp.linux-i686-2.5/_mysql.o -DBIG_JOINS=1 In file included from _mysql.c:29: pymemcompat.h:10:20: error: Python.h: No such file or directory _mysql.c:30:26: error: structmember.h: No such file or directory In file included from /usr/include/mysql/mysql.h:43, from _mysql.c:40: /usr/include/sys/types.h:153: error: duplicate 'unsigned' /usr/include/sys/types.h:153: error: two or more data types in declaration specifiers _mysql.c:64: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:65: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:66: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:67: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:68: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:69: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:70: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:71: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:72: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:73: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:74: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token _mysql.c:77: error: expected specifier-qualifier-list before 'PyObject_HEAD' _mysql.c:87: error: expected '=', ',', ';', 'asm' or '__attribute__' before '_mysql_ConnectionObject_Type' _mysql.c:90: error: expected specifier-qualifier-list before 'PyObject_HEAD' _mysql.c:98: error: expected '=', ',', ';', 'asm' or '__attribute__' before '_mysql_ResultObject_Type' _mysql.c:107: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token : : -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQL_python install failed on Ubuntu 7.1
On Dec 17, 1:31 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Bruza a écrit : > > > I installed MySQL 5.0.45 onUbuntu7.1and downloadMySQL_pythonfrom > > Sourceforge (http://sourceforge.net/project/showfiles.php? > > group_id=22307). Then I untar the package and executed "python > > setup.pyinstall". But I got compilation errors (see part of the > >failedmessages below). > > > Looks like the installation tried to compile the _mysql extension and > >failed. Anybody knows a solution to this problem? > > (snip) > > > In file included from _mysql.c:29: > > pymemcompat.h:10:20: error: Python.h: No such file or directory > > Looks like gcc doesn't find the Python's headers. You probably need > toinstallthe "python-dev" (or whatever it's named onUbuntu) package, > that is the one with the required stuff to compile and link C libs > relying on CPython implementation. Bruno, I got it working! As you suggested, I did "sudo apt-get install python- dev", then "sudo apt-get install mysqldb-python", and it's working now. Thanks, Bruza -- http://mail.python.org/mailman/listinfo/python-list
how can I specify a timeout value to Semaphore.acquire()?
Not sure if this is related. But I found a "patch" to Python 2.4 to support adding "timeout" parameter to Semaphore.acquire() (http:// mail.python.org/pipermail/patches/2003-November/013796.html). However, when I try the "timeout" parameter on Python 2.5.1 running on Ubuntu 7.10 and I got an error of: acquire() got an unexpected keyword argument 'timeout' Is there a way to specify a timeout value to Semaphore.acquire(), or I have to implement my own "SuperSemaphore"? Thanks in advance, Bruza -- http://mail.python.org/mailman/listinfo/python-list