mutable member, bug or ...
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near the end ). class Cgroup_info: group_name = "" count = "0" #last time checked and processed/retrieved first = "0" last = "" retrieval_type = ""# allways , ask( if more than some limit), none date_checked = "" time_checked = "" new_count = "" new_first = "" new_last = "" # local storage maintanance vars pointer_file = "" message_file = "" #maintanance vars cur_mess_num = 0 cur_mess_id = "" def __init__( self ): group_name = "" count = "0" #last time checked and processed/retrieved def get_count( self ): print self.count, type( self.count ) return string.atoi( self.count, 10 ) class server_info: def "(server_info::)"get_group_stat( self, grp ): gr_info = Cgroup_info() gr_info.group_name = grp try: ind = self.group_list.index( grp ) except ValueError: gr_info.count(0) return ( gr_info ) print ind if len( self.group_list[ind].split() ) == 4: gr_info.count = self.group_list[ind].split()[1] gr_info.first = self.group_list[ind].split()[2] gr_info.last = self.group_list[ind].split()[3] else: gr_info.count = gr_info.first = gr_info.last = "0" return( gr_info ) -- http://mail.python.org/mailman/listinfo/python-list
Re: mutable member, bug or ...
Bruno Desthuilliers wrote: > Sambo a écrit : > >> By accident I assigned int to a class member 'count' which was >> initialized to (empty) string and had no error till I tried to use it >> as string, obviously. Why was there no error on assignment( near the >> end ). > > > Python is dynamically typed - which means that it's not the name that > holds type info, but the object itself. names are just, well, names... > Hmm.. so I could have one instance with (int)count and (string)count? (yay DBase flashback, BRRR) I was going to initialize the vars in __init__() but somehow it didn't make sense to me( but left couple of them there). > BTW, I failed to see where you assigned an int to the class attribute > 'count'. I just saw a try to call a string - which should raise a > TypeError. > I must have fixed it before posting. gr_info.count = gr_info.first = gr_info.last = 0 ??? except ValueError: gr_info.count(0) ??? not sure what I was thinking there (maybe I was trying to triple fault the CPU hehe) >> class Cgroup_info: > > > Do yourself (and anyone having to work on or with your code) a favour: > use new-style classes (ie : inherit from 'object'). And FWIW, the > convention for class names is CamelCase - preferably without MS-like > hungarian annotation. > Those damn hungarians with their calculators and notations, only later did it occur to me to paste the "class ServerInfo():" statement above. > this creates a local variable 'group_name', bound to an empty string. > Using the reference to the current instance (usually named 'self', and > always passed in as first param) is *not* optional. > >>count = "0" #last time checked and processed/retrieved In __init__() it was an oversight but after the class className()... I may have thought it unnecessary, otherwise class consists only of a group of functions if as you say any vars should be created/initialized in __init__() hmmm. > the string module is mostly deprecated. Use str object methods instead - > or if you just want to create an int from it's representation as a > string, int(self.count). > Are string object methods among others, things like: words = sentence.split() This no longer works for me is it because I imported 'string' , didn't import something or didn't use "from string import *" instead. ( must be a year since I last played with python.) > >>gr_info.group_name = grp > > > Tthis create a new instance attribute "group_name" on the gr_info > object. This instance attribute will shadow the class attribute of the > same name. > Hmmm . so what is a class attribute good for? > Also, FWIW, if you always know the value for group_name when > instanciating a Cgroup_info object, you might as well pass it to the > initializer. > Good point. >>try: >>ind = self.group_list.index( grp ) > > > The common convention for indices in each and every language is 'i'. If > you really want a meaningful name, then group_index would be better. > lol, well like in a book and increasingly on the net index in used to refer to a list. So... I guess subscribed_group_list_index(_pointer) might be workable. > Also, for this kind of lookups, dicts are really faster than lists. > I am storing group count first last, although I am considering moving the numeric data elsewhere, for about 100 items .. I'll leave dictionaries for future learning. >>return ( gr_info ) > > > parenthesis here are useless (and FWIW, they would be just as useless in > C++). > A habit, true in python , in C, I think I remember reading about return function and statement. I was important at some point in C or perhaps way back in Pascal. >>print ind >>if len( self.group_list[ind].split() ) == 4: >>gr_info.count = self.group_list[ind].split()[1] >>gr_info.first = self.group_list[ind].split()[2] >>gr_info.last = self.group_list[ind].split()[3] > > > group_list[ind] is the same as grp, isn't it ? if so, using grp directly > might be much more efficient *and* much more readable. > no grp is the (string) group name that was used earlier to find the right item in the list. > Also, you're calling 4 times the same method. This is highly > inefficient. Try this instead: >parts = grp.split() >if len(parts) == 4: > gr_info.count, gr_info.first, gr_info.last = parts[1:] > > yes I realized that in another function but forgot about the unpacking assignment of a slice. >> else: &g
split with "*" in string and ljust() puzzles
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print group_info.mess_list return linecount = 0 ptr_file.seek( 512 ) print ptr_file.tell() line = ptr_file.readline() while line != "" : if line[0:1] == "<": print linecount print len(line), line print line.split( " ", 3 ) group_info.mess_list.append( line.split( " ", 3 ) ) line = ptr_file.readline() ptr_file.close() when reading the following line from file: <[EMAIL PROTECTED]> 2338 *Re: PCB Pad Size for TQFP Package?? the split command returns ['<[EMAIL PROTECTED]>','','','2338 * Re: PCB Pad Size for TQFP Package??'] instead of ['<[EMAIL PROTECTED]>, '2338','*','Re: PCB Pad Size for TQFP Package??'] I have just (finally) realized that it is splitting and removing on single space but that seams useless, and split items 1 and 2 are empty strings not spaces?? regex somewhere it shouldn't be? The other problem is in this piece of code which is trying to pad the first 512 bytes: line = group_info.group_name+" sf"+ group_info.first + " sl"+ \ group_info.last + " sc" + group_info.count + "dt" + \ group_info.date_checked + group_info.time_checked line = line + "\n" line = string.ljust( line, 512 - len(os.linesep) ) print len( os.linesep ) line += "\n" print len( line ) ptr_file.write( line ) print " "+repr(ptr_file.tell())+ " " print "message list\n" the ljust function returns string 511 bytes long, besides the fact that the logic is not exactly correct what is going on here. Is ljust trying to be smart about the EOL inside the string already? I have tried the following, which should be about right ( account for the 1 bytes added after justification and two added by file write.) line = string.ljust( line, 512 - len(os.linesep) - len(os.linesep) - 1 ) But, in this case I end up 2 bytes short of 512. Thanks for any help. -- http://mail.python.org/mailman/listinfo/python-list
Re: split with "*" in string and ljust() puzzles
George Sakkis wrote: > Serge Orlov wrote: > > >>Sambo wrote: >> >>>I have just (finally) realized that it is splitting and removing >>>on single space but that seams useless, and split items >>>1 and 2 are empty strings not spaces?? >> >>What is useless for you is worth $1,000,000 for somebody else ;) >>If you have comma separated list '1,,2'.split(',') naturally returns >>['1', '', '2']. I think you can get what you want with a simple regexp. > > > No need for regexp in this case, just use None to specify one or more > whitespace chars as delimiter: line.split(None,3) > > George > AHA! Thank You. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fixing Python instalation in win2000 by hand
BartlebyScrivener wrote: > > You may want to try disabling any anti-virus software you have running > as they > frequently cause software installation failures. NONE!! > > MSI Installers have a known issue with mapped network drives; please > copy the > installer to the c:\ root drive and try running the installation again. > > If you are using the NTFS file system, please make sure that the SYSTEM > account has full rights over the target installation directory, the > directory > containing the installer file, AND the Windows Installer directory, > located at > either c:\WINNT\Installer\ (WINNT 4 / Windows 2000) or > c:\Windows\Installer\ > (Windows XP). Since I have not formated this drive (C:) it happens to be NTFS. SYSTEM account??? what's that, where do I manage that? When I was trying to share these drives in the past with linux, I would often run into a message similar to "cannot share drive as main-c drive already shared for system 'something' (C$)" > > This may be the result of an altered Windows Scripting Host > installation. The > best workaround is to reinstall this Windows component. > > Windows Scripting Host can be downloaded from: > http://www.microsoft.com/downloads/details.aspx?FamilyID=c717d943-7e4b-4622-86eb-95a22b832caa&DisplayLang=en > > ( for Windows 200 / XP ) > > or Genuine validation? lol this should be interesting. > > http://www.microsoft.com/downloads/details.aspx?FamilyID=0a8a18f6-249c-4a72-bfcf-fc6af26dc390&DisplayLang=en > > ( for Windows 9x / Me / NT4 ) > > You will need to be logged in as an administrator of the target > computer. You > will also need to ensure that you have full permissions to install on > the > drive you have chosen. > > You must have Microsoft Windows Installer version 2.0 or greater to run > the > MSI package. There is a link to download the correct Windows Installer > for > your platform on the System Requirements page: > > http://activestate.com/Products/ActivePerl/system_requirements.plex > > The MSI file may be corrupt. You may also want to try re-downloading > the > installation package and trying the installation again. If you have > trouble > downloading the file, you may want to try getting it via FTP: > > ftp://ftp.activestate.com or http://downloads.activestate.com > > If you are still seeing errors after the re-installation attempt, it > would be > helpful if you could send us a log of the installation process. You can > perform Microsoft Installer logging using the msiexec command: > > C:\> msiexec /I installer_file_name.msi /L*v install.log > > Send us the resultant install.log file and we will investigate further. > I believe the most recent code I have tried to install was V3 InstMsiW.exe 1822848 Cre Aug. 13 2005 But if I am not mistaken it always came up with ERROR box "The specified service already exists." May have been installed by some auto update, looking at the log seams like I have installer V3. c:\temp1>msiexec /I c:\temp1\python-2.4c1.msi /L*v install.log /L*v ok here it is... before downloading and installin the VB scripting crap.( in case it starts closing apps trying to reboot and clear all this typing I've done hehe. install.log === Verbose logging started: 17/04/2006 12:28:52 Build type: SHIP UNICODE 3.01.4000.2435 Calling process: C:\WINNT\system32\msiexec.exe === MSI (c) (84:10) [12:28:52:609]: Resetting cached policy values MSI (c) (84:10) [12:28:52:609]: Machine policy value 'Debug' is 0 MSI (c) (84:10) [12:28:52:609]: *** RunEngine: *** Product: c:\temp1\python-2.4c1.msi *** Action: *** CommandLine: ** MSI (c) (84:10) [12:28:52:609]: Machine policy value 'DisableUserInstalls' is 0 MSI (c) (84:10) [12:29:23:156]: Failed to connect to server. Error: 0x80080005 MSI (c) (84:10) [12:29:23:156]: MainEngineThread is returning 1601 === Verbose logging stopped: 17/04/2006 12:29:23 === -- http://mail.python.org/mailman/listinfo/python-list
Re: Fixing Python instalation in win2000 by hand
> > c:\temp1>msiexec /I c:\temp1\python-2.4c1.msi /L*v install.log > /L*v ok here it is... before downloading and installin the VB > scripting crap.( in case it starts closing apps > trying to reboot and clear all this typing I've done hehe. > install.log > === Verbose logging started: 17/04/2006 12:28:52 Build type: SHIP > UNICODE 3.01.4000.2435 Calling process: C:\WINNT\system32\msiexec.exe === > MSI (c) (84:10) [12:28:52:609]: Resetting cached policy values > MSI (c) (84:10) [12:28:52:609]: Machine policy value 'Debug' is 0 > MSI (c) (84:10) [12:28:52:609]: *** RunEngine: > *** Product: c:\temp1\python-2.4c1.msi > *** Action: *** CommandLine: ** > MSI (c) (84:10) [12:28:52:609]: Machine policy value > 'DisableUserInstalls' is 0 > MSI (c) (84:10) [12:29:23:156]: Failed to connect to server. Error: > 0x80080005 > > MSI (c) (84:10) [12:29:23:156]: MainEngineThread is returning 1601 > === Verbose logging stopped: 17/04/2006 12:29:23 === Well, I installed the scripten.exe but to no avail. same log , same behavior. install1.log === Verbose logging started: 17/04/2006 13:20:01 Build type: SHIP UNICODE 3.01.4000.2435 Calling process: C:\WINNT\system32\msiexec.exe === MSI (c) (4C:10) [13:20:01:781]: Resetting cached policy values MSI (c) (4C:10) [13:20:01:781]: Machine policy value 'Debug' is 0 MSI (c) (4C:10) [13:20:01:781]: *** RunEngine: *** Product: python-2.4c1.msi *** Action: *** CommandLine: ** MSI (c) (4C:10) [13:20:01:890]: Machine policy value 'DisableUserInstalls' is 0 MSI (c) (4C:10) [13:20:33:296]: Failed to connect to server. Error: 0x80080005 MSI (c) (4C:10) [13:20:33:312]: MainEngineThread is returning 1601 === Verbose logging stopped: 17/04/2006 13:20:33 === Windows Installer (box) The windows Installer Service could not be accessed. this can occur in safe mode, or if Installer is not correctly installed. Contact for assistance. So I guess no way to retrieve the contents/instructions of python.msi SCRYPT to find out what should go where? Cheers. -- http://mail.python.org/mailman/listinfo/python-list
Problem calling math.cos()
I have the following module: --- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j ) ac3 = ( 0, 0j ) ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math.radians( ph1 ) ) ) ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin( math.radians( ph2 ) ) ) ac3 = ac1 + ac2 amp3 = math.abs( ac3 ) ph3 = math.atan( ac3.imag / ac3.real ) return [amp3, ph3] -- when I import it (electronics) in python.exe in windows2000 and try to use it, it croaks. ??? >>> import math >>> import electronics >>> print electronics.ac_add_a_ph( 10, 0 , 6 , 45 ) Traceback (most recent call last): File "", line 1, in ? File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math .radians( ph1 ) ) ) NameError: global name 'cos' is not defined >>> global?? huh? what does abs stand for? why is that not absolute value? hmmm. Hmm, complex numbers, cool I don't even have any idea where C stands on this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem calling math.cos()
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, Sambo <[EMAIL PROTECTED]> > wrote: > > >>I have the following module: >>--- >>import math >> >>def ac_add_a_ph( amp1, ph1, amp2, ph2 ): >> >>amp3 = 0.0 >>ph3 = 0.0 >>ac1 = ( 0, 0j ) >>ac2 = ( 0, 0j ) >>ac3 = ( 0, 0j ) >>ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( >>math.radians( ph1 ) ) ) >>ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin( >>math.radians( ph2 ) ) ) >>ac3 = ac1 + ac2 >>amp3 = math.abs( ac3 ) >>ph3 = math.atan( ac3.imag / ac3.real ) >>return [amp3, ph3] >>-- >>when I import it (electronics) in python.exe in windows2000 and >>try to use it, it croaks. ??? >> >> >>>>>import math >>>>>import electronics >>>>>print electronics.ac_add_a_ph( 10, 0 , 6 , 45 ) >> >>Traceback (most recent call last): >> File "", line 1, in ? >> File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph >>ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( >>math >>.radians( ph1 ) ) ) >>NameError: global name 'cos' is not defined >> > > That's not what I get when I run it (admittedly, not on windows). I get: > > >>>>import math >>>>import electronics >>>>print electronics.ac_add_a_ph( 10, 0 , 6 , 45 ) > > Traceback (most recent call last): > File "", line 1, in ? > File "electronics.py", line 13, in ac_add_a_ph > amp3 = math.abs( ac3 ) > AttributeError: 'module' object has no attribute 'abs' > > > which is exactly what I expected, since abs (which is indeed absolute > value) is a built-in function, not a part of the math module. Are you sure > the stack trace you posted matches the source code you posted? > Well I took the abs( 'complex' ) from the python documentation (python24.chm) section 3.1.1 has the following comment after it '# sqrt(a.real**2 + a.imag**2)' > By the way, when using math functions, I find it's usually easier to import > them into my namespace by doing "from math import *", then I can just use > sin(), cos(), etc directly, instead of having to do math.sin() or > math.cos(). Especially for common math functions, this makes your code a > lot easier to read. Ah, I thought I used to use module functions without the module name. I think my problem is reimporting electronics(.py) after modifications. Yes, now it complains about abs(). looks like enother reason to dump this w2000 installation just so I can install python from scratch and use idle. -- http://mail.python.org/mailman/listinfo/python-list
Re: Push to Make (Single Pole) Button running a loop
Hendrik van Rooyen wrote: > "Jon Todd" wrote: > > >>I'd like to have a button that when pressed executes a loop (this could be a >>thread) and then stops execution when it's released (Push to Make - Single >>Pole in electronics terms). >> >>I've tried the regular way of associating the procedure with a callback and >>tried using and bindings but am getting nowhere >>fast (in the latter case the button release event seems to occur anyhows). >> > > > You are a bit stymied : > > Afaik, Button needs the click on the button, leaving it depressed, > while ButtonRelease needs a click on the button, and the release can be > anywhere > for drag and drop implementation. So it is possible to call two routines with > one > click and release. > > But there is another hassle - you would have to code the loop as a separate > thread, > started by the first, while the second routine sets some variable that is > checked by > the loop to kill itself, as the GUI will not be responsive until the first > command > returns, because the command "captures" the main loop... > > HTH - Hendrik > > Perhaps besides all the multithreading issues you should be looking at different kind of control/button ei. checkbox or radio button (windows terminology)? My first experience at GUI with Tk/TCL?? was not a good one due to both lack of docs and fear it wouldn't work on both LUNUX and vindows. Wish there was a simple library with basic primitives like create_windows resize, get_client_area and of course some functions to hand of control to the system. At least for me, that way I could get much further much faster than trying to find the learn the behavior of all the currently available controls. Another point I came across recently is that there is no information on proper cleanup after TCL, don't remember exactly why it was a problem it is after all OOP sy. So, you may have to read the sourcecode for everything you are using and it ain't easy, Good Luck. Cheers. -- http://mail.python.org/mailman/listinfo/python-list
port forwarding: python scrypt or C ?
Anyone aware of something that I could test my new DSL modem with. when I finally unlocked it ( Siemens 4200 ) some setting I saw made me wonder if it is filtering at all, but tiberian sun internet play was having problems. After going through the instructions at {http://portforward.com/english/routers/port_forwarding/Siemens/4200/Command_and_Conquer_Tiberian_Sun.htm} although not carefully since later I realized they entered the stuff in different order (not lowest to highest like I did, but it's not IPtables so hopefully it shouldn't matter). S, I'd like to see it to believe it. Since I'll most likely going test from dialup on Linux to DSL on windows, Python would be best. Cheers. -- http://mail.python.org/mailman/listinfo/python-list
Re: noob needs help
Peter Pearson wrote: > When you type "python helloworld.py", python looks for helloworld.py > in the current "working" directory, which is probably your personal > default directory ("~"). For better results, either save helloworld.py > to this directory, or copy it to this directory > ("cp Documents/helloworld.py ."), or tell python to look for it > in the Documents directory ("python Documents/helloworld.py"). > > (I'm guessing that your directory structure looks something > like /home/collin/Documents. If I've guessed wrong, some > adjustment may be needed. Also, if you're feeling ambitious, > you might want to consider putting this project in a directory > of its own; that would involve the mkdir and cd commands.) > In slackware one needs "./" before the filename if you executing files in current dir. -- http://mail.python.org/mailman/listinfo/python-list
Fixing Python instalation in win2000 by hand
Some time ago I bought a newer computer with W2000 already on it so I moved my old drive to it as secondary( python was installed on non primary partition ). Not sure if the .msi installers were broken before, but they are now (on this installation) and the reason I can't move to brand new installation is because I am missing sound drivers. I have copied 3 files from my d:\winnt\system32 (old C:) pywintypes24.dll, pythoncom24.dll (mod 11/10/2004 4:26am) and python24.dll (mod 18/11/2004 6:55pm) As a result I can now run python.exe but not pythonw.exe Is there anything else I need and what, if anything, should be in my PYTHONPATH "f:\Python24\Lib" ? ?? Although if I type import os it has no problem. Thanks in advance, Sam -- http://mail.python.org/mailman/listinfo/python-list
Re: Fixing Python instalation in win2000 by hand
BartlebyScrivener wrote: >>>Not sure if the .msi installers were broken before, >>>but they are now (on this installation) > > > Are you installing from c:\ ? almost never > > With administrator rights? > pretty sure "YES" > Check other requirements. It chokes if you are installing from another > logical drive, e.g., d:\ > > http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/installnotes.html > > rick > This problem is not limited to Python, Ja*va.* and MPLab.msi don't work either. It was screwed up by some windows update , and never fixed by any additional updates. Hmmm. I'll try from C: but. -- http://mail.python.org/mailman/listinfo/python-list
access to base class __init__
I got myself in jam trying to be too fancy with threading.Thread Docs say / remind to call the base __init__ but I can't fighure out how. --- def main() . ls.listen(5) key = ' ' #while key != EXITCHARCTER: while stop_serving == False: cs, raddr = ls.accept() print "Main_Thread: ",cs, raddr nt = client_socket_handler( cs, raddr ) print threading.enumerate() key = getkey() #ls.close() time.sleep(4) print "Server Exiting." class client_socket_handler(threading.Thread): def __init__(self, cs, remote_address): ??? self.threading.Thread.__init__(self,self.socket_handler,None,None) self.socket = cs self.rhost_addr = remote_address print "client_socket_handler.__init__(): ", self.socket, self.rhost_addr #t1 = threading.Thread( None,self.socket_handler, None, (5,78) ) #t1.start() self.start() print "client_socket_handler.__init__(): ", self.socket, self.rhost_addr print "client_socket_handler.__init__(): enumerate()", threading.enumerate() def socket_handler( self, invar, indict ): threadname = self.getName() print "\"%s started\"" % threadname print "client_socket_handler.socket_handler() invar: ", invar instr = self.socket.recv( 500 ) #print instr req_lines = string.split( instr, "\r" ) for line in req_lines: line.strip( "\n") print req_lines print len( instr ) -- self.threading.Thread.__init__() self.Thread.__init__() ?? -- http://mail.python.org/mailman/listinfo/python-list