Re: [Tutor] Sleep
Hristos Giannopoulos wrote: Is it possible for a python script to make a windows computer sleep or wake from sleep? Preferably in windows? Nothing built in to the language. You'll need in invoke the relevant Windows API[1] either via pywin32[2] or ctypes[3]: [1] http://msdn.microsoft.com/en-us/library/aa373163%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/aa373206%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/aa373201%28VS.85%29.aspx [2] http://sourceforge.net/projects/pywin32/ [3] http://docs.python.org/library/ctypes.html TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help or Advice on MySQL, Python and test data storage
Hi All, looking for some advice on using Python with MySQL for test data storage. What builds of Python work well with MySQL and what modules allow the connection to the database and data transfer activities. I'm thinking about using PhPMyAdmin to set-up and administer my database in the first instance until I get going with the project, though I could create tables using the commandline option. Is there a Python equivalent to PhPMyAdmin? Outline of testing carried out === I use Python to control my test instruments, Spectrum Analyser, Power Meter, Signal Generator and scope using pyvisa. I am just learning wxpython for the gui side of my application (crudely working app to date) and I current dump test data out to a csv file using the csv module. I want my test data stored in a better fashion and in a way where I can compare easily the data from one device to another. Testing involves gathering power data at harmonic frequencies over a set range in one test. Another test involves injecting a signal and observing the effect on specific registers on the device under test. I want to record all the device under test set-up information and the test equipment information (calibration dates instrument type etc) input signal values during the test and the device under test response values. Test data to store is harmonic frequencies v power levels recorded at each frequency per test unit. Unit under test serial number, setup parameters, version. In a way header information for each test run. Registers settings recorded during test run possibly 8 to 10 different register settings to monitor. Screen dumps from the analyser (WMF or BMP file format, roughly 130Kb per image, possibly 250 images per test run)how best to store these in the database, i.e. as a link to a file or as raw data set (glob or blob can't remember the exact phrase used for storing data in this way). Images are important for the tester to be able to look for any sideband issues or areas of broadband noise, as some runs will overnight when no staff are on site. Thanks in advance for any responses to my questions. -- Cheers David. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Package for verify validity of any kind of IP
Hi, Can anybody suggest me a package to verify validity of any kind of IP, i.e. IPv4 or IPv6. Thanks, AD ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help or Advice on MySQL, Python and test data storage
David Jamieson wrote: Hi All, looking for some advice on using Python with MySQL for test data storage. What builds of Python work well with MySQL and what modules allow the connection to the database and data transfer activities. SQLAlchemy is one option; http://www.rmunn.com/sqlalchemy-tutorial/tutorial-0.1.html -- Powered by Gentoo GNU/Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Bounces
I keep getting Mail Delivery Subsystem notices from tutor saying my mail is undeliverable. Is that just me or are other people getting this as well? I'm realizing now that a lot of my replies are not getting through. Anyone know why this may be? (even if you don't, someone please reply so I know at least this one got through!) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bounces
This one seems to have got through though.. On Thu, Oct 8, 2009 at 7:15 AM, Luke Paireepinart wrote: > I keep getting Mail Delivery Subsystem notices from tutor saying my mail is > undeliverable. Is that just me or are other people getting this as well? > I'm realizing now that a lot of my replies are not getting through. Anyone > know why this may be? (even if you don't, someone please reply so I know at > least this one got through!) > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bounces
Luke Paireepinart wrote: I keep getting Mail Delivery Subsystem notices from tutor saying my mail is undeliverable. Is that just me or are other people getting this as well? I'm realizing now that a lot of my replies are not getting through. Anyone know why this may be? (even if you don't, someone please reply so I know at least this one got through!) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor This one came through, I also have 57 emails originating from you since the 8th of July 2009. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help or Advice on MySQL, Python and test data storage
> looking for some advice on using Python with MySQL for test data > storage. What builds of Python work well with MySQL and what modules > allow the connection to the database and data transfer activities. You'll need the MySQLdb module to connect to MySQL from Python. http://sourceforge.net/projects/mysql-python/ According to its sourceforge page, the module supports: MySQL versions 3.23-5.1; and Python versions 2.3-2.5 are supported. One of the reviews on the page says it also works with Python 2.6. > I'm thinking about using PhPMyAdmin to set-up and administer my > database in the first instance until I get going with the project, > though I could create tables using the commandline option. Is there a > Python equivalent to PhPMyAdmin? If you already know how to use phpMyAdmin and it serves your needs, why not just stick with it? Another option is MySQL's database management tools: http://dev.mysql.com/downloads/gui-tools/5.0.html I don't believe these can administer remote databases, but you'll want to check the docs on that to be sure. I've only used them on a local machine, and they worked fine for that purpose. HTH, Serdar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bounces
I got an error bounce too, saying that the bottom portion of a recent message was "ignored" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Memory usage (Lizhi Yang)
Below you see my original post. 1: Say I use boost.python. 2: For example, I allocate a big array with integers using C++ function (4 bytes for each int in C++, but 12 bytes in python), and I use boost.python to call those C++ functions. How is the memory allocation for that array then? My last question: Lizhi Yang wrote: > Confused. If I create some functions using C++ to load the data into > memory and use python to call those functions, what is the memory > usage then? First (obvious) question: how do you call those functions? Do you use ctypes? Cython? Some other way? Providing a short code snippet that shows what you are doing will greatly help in understanding your question better. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Package for verify validity of any kind of IP
On Thu, Oct 8, 2009 at 3:38 AM, Ansuman Dash wrote: > Hi, > > Can anybody suggest me a package to verify validity of any kind of IP, i.e. > IPv4 or IPv6. > > You mean to check if the IP is live, or to check if the IP is in the valid range for IP addresses? For the latter, just use a regex (google can give you explicit examples if you don't want to make your own) hth, wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginners question
Hi all - Just started on "Python Programming for Absolute Beginners" and I've got a question: The program called 'Guess my Number' goes like this: # Guess My Number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money #import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 15." print "Try to guess it in as few attempts as possible.\n" import random # set the initial values the_number = random.randrange(15) + 1 guess = int(raw_input("Take a guess: ")) tries = 1 # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit.") So here's the question - the original code has parentheses around the lines of code with *(guess !=the_number)* and *(guess* *> the_number)* . I tried to run the program without the parentheses and it runs just fine. So what are the parentheses for?? Thanks a lot for your time - Gary ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginners question
gary littwin wrote: Hi all - Just started on "Python Programming for Absolute Beginners" and I've got a question: The program called 'Guess my Number' goes like this: # Guess My Number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money #import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 15." print "Try to guess it in as few attempts as possible.\n" import random # set the initial values the_number = random.randrange(15) + 1 guess = int(raw_input("Take a guess: ")) tries = 1 # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit.") So here's the question - the original code has parentheses around the lines of code with *(guess !=the_number)* and *(guess* *> the_number)* . I tried to run the program without the parentheses and it runs just fine. So what are the parentheses for?? Thanks a lot for your time - Gary ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Some use the parentheses for code clarity as it reads easier when it's grouped by (). Your code as written won't run as it should, your while loop needs to change to include your `guess = int(raw_input("Take a guess: "))` as without it if your first guess is incorrect it would just loop infinitely (it's currently 1 level of indentation out, as well as your `tries += 1`). Welcome to Python and hope you enjoy your stay. :) -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginners question
gary littwin wrote: Hi all - Just started on "Python Programming for Absolute Beginners" and I've got a question: The program called 'Guess my Number' goes like this: # Guess My Number # # The computer picks a random number between 1 and 100 # The player tries to guess it and the computer lets # the player know if the guess is too high, too low # or right on the money #import random print "\tWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 15." print "Try to guess it in as few attempts as possible.\n" import random # set the initial values the_number = random.randrange(15) + 1 guess = int(raw_input("Take a guess: ")) tries = 1 # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) tries += 1 print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input("\n\nPress the enter key to exit.") So here's the question - the original code has parentheses around the lines of code with *(guess !=the_number)* and *(guess* *> the_number)* . I tried to run the program without the parentheses and it runs just fine. So what are the parentheses for?? Thanks a lot for your time - Gary The parentheses in this procedure are not required but don't do any harm. I often use extra parentheses to clarify what I am doing so it is more readable when going back to look at it a couple of years later. Especially in long calculations or SQL with lots of ands and ors. -- Jeff Jeff Johnson j...@dcsoftware.com Phoenix Python User Group - sunpigg...@googlegroups.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Memory usage (Lizhi Yang)
2009/10/8 Lizhi Yang > Hi Adam, > > One more question, if I want to implement algorithms and load the huge > amount of data using C++, but using Python as the glue language to > implement other functionality such as Gui and txt processing, you > think Boost.python is better or SWIG is better? Have not tried yet, > just need to make decision first, thanks. > > On Thu, Oct 8, 2009 at 9:19 AM, Lizhi Yang wrote: > > Hi Adam, > > > > Thank you so much for your help. I will do a test, and will let you > > know the result. > > > > On Thu, Oct 8, 2009 at 9:15 AM, Adam Bark wrote: > >> 2009/10/8 Lizhi Yang > >>> > >>> Below you see my original post. > >>> > >>> 1: Say I use boost.python. > >>> 2: For example, I allocate a big array with integers using C++ > >>> function (4 bytes for each int in C++, but 12 bytes in python), and I > >>> use boost.python to call those C++ functions. How is the memory > >>> allocation for that array then? > >> > >> If the data is setup in C++ using native types then it will use 4 bytes > per > >> int. I believe boost just creates a wrapper so you can call the C++ > function > >> as if it were a python function it then does all the conversion > internally > >> and will return some python type data. > >> HTH > > > Hi Lizhi, I don't really have much experience with either, personally, and I just realised I didn't reply-all my original response so I'm CC'ing this back to the list, hopefully you'll get a useful response there. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Package for verify validity of any kind of IP
On Thu, Oct 8, 2009 at 2:38 AM, Ansuman Dash wrote: > Hi, > > Can anybody suggest me a package to verify validity of any kind of IP, i.e. > IPv4 or IPv6. > > Thanks, > AD > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > I have used googles ipaddr package and have found it to work quite well http://code.google.com/p/ipaddr-py/ Vince ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Branch Coverage Tool for Python
Hi All, Is there any Python tool for Branch Coverage (as opposed to Statement Coverage)? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Wrapper of C++
Since the memory usage for saving huge data using python is not efficent at all(12 bytes per int). if I want to implement algorithms and load the huge amount of data using C++, but using Python as the glue language to implement other functionality such as Gui and txt processing, you think Boost.python is better or SWIG is better? Have not tried yet, just need to make decision first, thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if n == 0 vs if not n
On Tue, Oct 6, 2009 at 2:40 AM, Vern Ceder wrote: > Dave Angel wrote: > > Now in this case where it is only used as boolean checks which would be the most pythonic way if writing these checks? >>> The shorter version may be preferable, but it doesn't generally give the >> same results. Without knowing the possible data, these substitutions are >> not safe. >> >> For example, replacing "if not n == 0"with "if n" >> >> will give different results for values of "", [] and so on. It WILL >> work if you know that n is an int or float, however. >> >> DaveA >> > > True, I took the OP's statement that they were to be used "only as boolean > checks" to mean that there was no type mixing going on. Personally, I would > say that checking a list or string for equality (or lack thereof) with 0 is > even less "preferable". ;) > > Otherwise, one would at least prefer "if n != 0" to "if not n == 0", I > would think. > Actually, I just realized that "not" has higher precedence than "==" so this is really checking if (not n) is equal to 0, not if (n == 0) is (not) True. So essentially "not n" is evaluated and is turned into a bool, which is then compared to 0, which is the same as False in a boolean context, but "n != 0" is comparing n to 0, where n may not be the same type (eg. a string). I'm not sure if there's a situation where this difference matters, but I feel like there might be. Anyone have some counter-examples to n != 0 being the same as "not n == 0"? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrapper of C++
Lizhi Yang wrote: Since the memory usage for saving huge data using python is not efficent at all(12 bytes per int). if I want to implement algorithms and load the huge amount of data using C++, but using Python as the glue language to implement other functionality such as Gui and txt processing, you think Boost.python is better or SWIG is better? Have not tried yet, just need to make decision first, thanks. arrays of ints don't take 12 bytes per, they take 4. That's the same in C++ or in Python. So if that's the only reason to use C++, stick with Python. Probably you were looking at the overhead of list, which lets you put different types of data in different offsets. If you really have an (huge) array of ints, use array.array() to create it. As for talking between C++ and Python, you have a number of choices. You probably should look at ctypes, since it's built-in, before trying something more complex. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if n == 0 vs if not n
On Mon, Oct 5, 2009 at 9:13 PM, Luke Paireepinart wrote: > Actually, I just realized that "not" has higher precedence than "==" so this > is really checking if (not n) is equal to 0, not if (n == 0) is (not) True. No, "not" is lower precedence than "==". See http://docs.python.org/reference/expressions.html#summary > Anyone have some counter-examples to n != 0 being the same as "not n == 0"? Well, you can create a pathological class where they are different: In [17]: class funky(object): : def __eq__(self, other): : return True : def __ne__(self, other): : return True In [18]: f = funky() In [19]: f != 0 Out[19]: True In [20]: f == 0 Out[20]: True In [21]: not f == 0 Out[21]: False For a less contrived example, if n is a numpy array, n==0 is also a numpy array but (not n != 0) gives an error: In [1]: from numpy import * In [2]: a = array( [ 0, 10, 20, 30, 40 ] ) In [3]: a== 0 Out[3]: array([ True, False, False, False, False], dtype=bool) In [5]: not a!= 0 --- ValueErrorTraceback (most recent call last) C:\Project\MangoLib\ in () ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if n == 0 vs if not n
> Thanks all for the informative discussion. To re-confirm it was mostly > for boolean checks like "if b == True". wow, as the OP, you must have been surprised to see how far we have taken your (seemingly) simple question. we went from boolean checks to interning! commenting on my previous reply, i was addressing only the boolean results when shortening the comparisons. as i mentioned earlier, every Python object has the concept of a boolean value. zeros and empty containers have False values while all others are True. however, what i did *not* mention is that these (abbreviated) comparisons do not work should you care to distinguish between multiple Python objects/values that have the same boolean value. in other words, "if not b" will catch False, None, 0, etc. if your application is using 3 values like None (for unset value), False (bad/errror code), and True (correct behavior), then None and False will both cause the if clause to be executed. in other words, if you care about the actual objects, then you need to use either "==" or "is", rather than just checking their boolean outcomes. now on to the difference between "==" vs. "is" and interning. "==" is the object *value* comparison operator while "is" is the object *identity* comparison operator. although "is 0" does work, it's easier to read and less confusing than "== 0". also as others have already mentioned, it's not a good idea to rely on the undocumented underlying implementation, and for it to stay consistent. for example, back in the earlier 2.x releases, the interned integer range was (-1, 101). when i worked on the 2nd edition of Core Python, i still had that range in the original manuscript. i then discovered that it changed to (-5, 256)... no one warned me ahead of time, and i was not paying enough attention to the python-dev list! again, the bottom line is to *not* rely on the implementation because it is always subject to change, granted not for numbers like -1, 0, 1, but why confuse your fellow Python coder? "is" is better suited for constants that are not numbers and that is always only one instance of: None, True, False. best regards, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bounces
It probably means that you used "reply all" and one of the addresses was not valid, possibly because it got garbled somehow on an earlier "reply all". On Thu, Oct 8, 2009 at 2:15 AM, Luke Paireepinart wrote: > I keep getting Mail Delivery Subsystem notices from tutor saying my mail is > undeliverable. Is that just me or are other people getting this as well? > I'm realizing now that a lot of my replies are not getting through. Anyone > know why this may be? (even if you don't, someone please reply so I know at > least this one got through!) > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Robert Lummis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading individual file from tar file as a file object
I have a large tar.bz2 file that I want to extract certain files directly to an FTP path. Since the extract() method doesn't accept ftp paths... I wanted to read the files from the tar file like a file object or file I/O stream. Is there a way to do this? Here's my pseudocode: import tarfile def putThisDataSomewhere(data): #write it to a file in FTP tar = tarfile.open("HUGE_FILE_OVER_5GB.tar.bz2", "r:bz2") readSize = 50 for tarInfo in tar: fileSize = tarInfo.size #prepare for loop to read specific file inside tar if readSize > fileSize readSize = fileSize readIterations = fileSize/readSize #loop & read file for i in range(readIterations): putThisDataSomewhere(tarFile.read(readSize)) #catch remainder of file lastReadSize = fileSize - (readSize * readIterations) if lastReadSize: putThisDataSomewhere(tarFile.read(lastReadSize)) tar.close() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] if n == 0 vs if not n
On Thu, 2009-10-08 at 12:58 -0700, wesley chun wrote: > wow, as the OP, you must have been surprised to see how far we have > taken your (seemingly) simple question. Pleasently suprised :-) And I am gratefull to see the heavy weights join in. > however, what i did *not* mention is that these (abbreviated) > comparisons do not work should you care to distinguish between > multiple Python objects/values that have the same boolean value. in > other words, "if not b" will catch False, None, 0, etc. if your > application is using 3 values like None (for unset value), False > (bad/errror code), and True (correct behavior), then None and False > will both cause the if clause to be executed. in other words, if you > care about the actual objects, then you need to use either "==" or > "is", rather than just checking their boolean outcomes. Good one, I have to remember this as it will very likely bite me someday. I really appriciate all the feedback and good advice! Thanks Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Troubles with lists and control flow
Hello I'm developing a script to compare two files, finding duplicate entries and matching which id of one csv file corresponds to the id of another csv file. The first version was working nice, but I wanted to postpone the writing to a file till the end and also make a correct csv file. The code is not so great and I expect to work with no more than 3000 lines of data in either file: So here is the inicial code. I hope it's not too long or complicated: import csv import re import addrnormalize import difflib import time started = time.time() nobv = open('regnobv.csv', 'wb') yesbv = open('reginbv.csv', 'wb') bv = open(r'\\albertapdc\ESP Data\ESP Main Files\BV_Customersa.csv').read().upper() site = open(r'C:\myscripts\latestregistrants.csv').read().upper() site = re.sub(r'([0-9]{3})-([0-9]{3})-([0-9]{4})', r'\1\2\3', site) bvreader = csv.DictReader(bv.splitlines()) sitelist = csv.DictReader(site.splitlines()) def inbv(yesbv): yesbv.write(item['USER_ID'] + ',') yesbv.write(row['CUS_NO'] + ',') yesbv.write(item['COMPANY'] + ',') yesbv.write(row['BVADDR1'] + ',') yesbv.write(item['ADDRESSLINEONE']+ ',') yesbv.write(row['BVADDRTELNO1'] + ',') yesbv.write(item['PHONE'] + '\n') bvreader = list(bvreader) # or (row['NAME'] in item['COMPANY']) or (row['BVADDREMAIL'] in item['EMAIL']) for item in sitelist: for row in bvreader: if ((row['BVADDRTELNO1'] == item['PHONE'] and row['BVADDRTELNO1']) or (row['BVADDREMAIL'] == item['EMAIL'] and row['BVADDREMAIL'])): inbv(yesbv) break ## this module just makes a few string transformations to standardize both strings. Like STREET -> ST elif addrnormalize.format_address(row['BVADDR1']) == addrnormalize.format_address(item['ADDRESSLINEONE']) and row['BVADDR1'] and row['BVPROVSTATE'] == item['STATE'] and row['BVPROVSTATE']: inbv(yesbv) break ## trying some fuzzy matching here elif (difflib.SequenceMatcher(lambda x: x in " ,.-#" , row['BVADDR1'], item['ADDRESSLINEONE']).quick_ratio() > 0.87) \ and (difflib.SequenceMatcher(lambda x: x in " .-" , row['BVCITY'], item['CITY']).quick_ratio() > 0.87): inbv(yesbv) break else: nobv.write(item['USER_ID']+ ',') nobv.write(item['FIRSTNAME']+ ',') nobv.write(item['LASTNAME']+ ',') nobv.write(item['COMPANY']+ ',') nobv.write(item['EMAIL'].lower()+ ',') nobv.write(item['PHONE']+ ',') nobv.write(item['FAX']+ ',') nobv.write(item['ADDRESSLINEONE']+ ',') nobv.write(item['ADDRESSLINETWO']+ ',') nobv.write(item['CITY']+ ',') nobv.write(item['STATE']+ ',') nobv.write(item['POSTALCODE']+ ',') nobv.write(item['COUNTRY']+ ',') nobv.write('\n') nobv.close() yesbv.close() finished = time.time() print finished - started End of code --- When I try with list it does not even print the "print linha" test If I uncomment all the conditionals except the first if than I get that written to the final file: reginbv. ### How is the new function with list affecting the results? import csv import re import addrnormalize import difflib import time started = time.time() nobv = open('regnobv.csv', 'wb') bv = open(r'\\albertapdc\ESP Data\ESP Main Files\BV_Customersa.csv').read().upper() site = open(r'C:\myscripts\latestregistrants.csv').read().upper() site = re.sub(r'([0-9]{3})-([0-9]{3})-([0-9]{4})', r'\1\2\3', site) bvreader = csv.DictReader(bv.splitlines()) sitelist = csv.DictReader(site.splitlines()) list2csv = [] list_not_in_bv = [] yesbv = csv.writer(open('reginbv.csv', 'wb'), dialect="excel") nobv = csv.writer(open('regnobv.csv', 'wb'), dialect="excel") def inbv(currentline = None): """writes a line of data when a date is found in BV""" if currentline is None: currentline = [] else: currentline.append(item['USER_ID']) currentline.append(row['CUS_NO']) currentline.append(item['COMPANY']) currentline.append(row['BVADDR1']) currentline.append(item['ADDRESSLINEONE']) currentline.append(row['BVADDRTELNO1']) currentline.append(item['PHONE']) currentline.append(row['BVCITY']) currentline.append(item['CITY']) return currentline def notinbv(currentline): if currentline is None: currentline = [] else: currentline.append(item['USER_ID']) currentline.append(item['FIRSTNAME']) currentline.append(item['LASTNAME']) currentline.append(item['COMPANY']) currentline.append(item['EMAIL']) currentline.append(item['PHONE']) currentline.append(item['FAX']) currentline.append(item['ADDRESSLINEONE']) currentline.append(item['ADDRESSLINETWO']) currentline.append(item['CITY']) currentline.append(item['STATE']) currentline.append(item['POSTALCODE']) currentline.append(item['COUN
Re: [Tutor] Troubles with lists and control flow
Oops, accidentally replied off-list. -- Forwarded message -- From: Luke Paireepinart Date: Thu, Oct 8, 2009 at 3:36 PM Subject: Re: [Tutor] Troubles with lists and control flow To: Eduardo Vieira On Thu, Oct 8, 2009 at 2:42 PM, Eduardo Vieira wrote: > Hello I'm developing a script to compare two files, finding duplicate > entries and matching which id of one csv file corresponds to the id of > another csv file. > The first version was working nice, but I wanted to postpone the > writing to a file till the end and also make a correct csv file. The > code is not so great and I expect to work with no more than 3000 lines > of data in either file: > So here is the inicial code. I hope it's not too long or complicated: > It's a little long to be in a message body, it'd have been nice if you posted to pastebin and chosen Python so we could have seen it with syntax highlighting and it would guarantee it doesn't mess up your indentation, but it's fine for now. Just keep that in mind for longer code samples. > def inbv(currentline = None): >"""writes a line of data when a date is found in BV""" >if currentline is None: >currentline = [] >else: >currentline.append(item['USER_ID']) >currentline.append(row['CUS_NO']) >currentline.append(item['COMPANY']) >currentline.append(row['BVADDR1']) >currentline.append(item['ADDRESSLINEONE']) >currentline.append(row['BVADDRTELNO1']) >currentline.append(item['PHONE']) >currentline.append(row['BVCITY']) >currentline.append(item['CITY']) > >return currentline > You don't want to do it like this. What you're saying is: "if they didn't pass in an argument to my function, create a new list and return an empty list. otherwise, if they did pass in a list, append an item and return the new list." What you really want to do is "if they didn't pass in a list, create a new one. Then append a value and return the new list." There's a subtle difference, do you see it? You want to add an item to the list whether or not they passed in a list in the first place, you just want to initialize a new list first. Think about your code and how you can change it to do that. At least I think that's what your issue is, I don't really understand your code at all. Also: > > def notinbv(currentline): >if currentline is None: >currentline = [] You should go ahead and define this one the same as the previous one (with the optional currentline parameter) unless you left it out on purpose. -Luke ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 3 and tkinter Radiobuttons
Hi. I’m using Tkinter to create a new Radiobutton in Python 3. However, when I create the button, it starts off looking selected instead of unselected (though it behaves correctly like an unselected Radiobutton). So this means when I create a group of Radiobuttons they all look selected when my program begins. This happens under both Windows XP and Windows 7. Here’s a super simple example program that produces this result: from tkinter import * root = Tk() root.grid() button = Radiobutton(root, text = "Test RadioButton") button.grid(row = 0, column = 0, sticky = W) root.mainloop() Does anyone know how to solve this issue? Anyone know if this is a bug being worked on? Thanks, --Bob _ Hotmail: Powerful Free email with security by Microsoft. http://clk.atdmt.com/GBL/go/171222986/direct/01/___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading individual file from tar file as a file object
On Thu, Oct 8, 2009 at 4:10 PM, xbmuncher wrote: > I have a large tar.bz2 file that I want to extract certain files directly to > an FTP path. > Since the extract() method doesn't accept ftp paths... I wanted to read the > files from the tar file like a file object or file I/O stream. > Is there a way to do this? I think tarfile.extractfile() does what you want. The docs are a bit confusing but it returns a file-like object from which the item can be read. Your code would be something like tar = tarfile.open("HUGE_FILE_OVER_5GB.tar.bz2", "r:bz2") for tarInfo in tar: putThisDataSomewhere(tar.extractfile(tarInfo)) putThisDataSomewhere() will read from its argument and write to FTP. Any required blocking or buffering would be in putThisDataSomewhere(). Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 9:59 AM, Didar Hossain wrote: > Hi, > > I am using the following code to check for the Python version - > > import os > > t = os.sys.version_info[0:2] > if (t[0] + t[1]) < 6: Hmm, what would this give for Python 1.5? How about if t < (2, 4): > os.sys.exit("Need at least Python 2.4") > del t > > This snippet is put at the beginning of the single script file before > the rest of the code. > I need to check for the minimum specific version because I am using > the "@staticmethod" > directive. You could check for what you actually need rather than checking the version: try: staticmethod except NameError: os.sys.exit("Need at least Python 2.4") Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 3 and tkinter Radiobuttons
On Thu, Oct 8, 2009 at 6:04 PM, bob smith wrote: > Hi. I’m using Tkinter to create a new Radiobutton in Python 3. However, > when I create the button, it starts off looking selected instead of > unselected (though it behaves correctly like an unselected Radiobutton). So > this means when I create a group of Radiobuttons they all look selected when > my program begins. You have to associate the Radiobuttons with a variable, for example: from tkinter import * root = Tk() root.grid() v = IntVar() button = Radiobutton(root, text = "Test RadioButton", variable=v, value=1) button.grid(row = 0, column = 0, sticky = W) button = Radiobutton(root, text = "Test RadioButton2", variable=v, value=2) button.grid(row = 1, column = 0, sticky = W) root.mainloop() Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading individual file from tar file as a file object
Works perfectly! Thanks. Yet again, I've learned I need to read better. :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading individual file from tar file as a file object
On Thu, Oct 8, 2009 at 8:45 PM, Xbox Muncher wrote: > Works perfectly! Thanks. Yet again, I've learned I need to read better. :) And remember, if the docs are unclear, you can always look at the source, as I did... Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrapper of C++
It might also be a good application for numpy (http://www.numpy.org/) Cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] list comprehensions
I've been studying python now for a few weeks and I've recently come into list comprehensions. Some of the examples that I've found make sense, and I find them readable and concise. In particular I'm referring to the python docs on the topic (http://docs.python.org/tutorial/datastructures.html#list-comprehensions). Those make sense to me. The way I understand them is: do something to x for each x in list, with an optional qualifier. On the other hand I've seen a few examples that look similar, but there is no action done to the first x, which confuses me. An example: print sum(x for x in xrange(1,1000) if x%3==0 or x%5==0) In this case is sum() acting as the "action" on the first x? Can anyone shed some light on what it is I'm not quite grasping between the two? Christer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehensions
Christer Edwards schrieb: I've been studying python now for a few weeks and I've recently come into list comprehensions. Some of the examples that I've found make sense, and I find them readable and concise. In particular I'm referring to the python docs on the topic (http://docs.python.org/tutorial/datastructures.html#list-comprehensions). Those make sense to me. The way I understand them is: do something to x for each x in list, with an optional qualifier. On the other hand I've seen a few examples that look similar, but there is no action done to the first x, which confuses me. An example: print sum(x for x in xrange(1,1000) if x%3==0 or x%5==0) In this case is sum() acting as the "action" on the first x? No. >>> (x for x in xrange(1,100) if x%3==0 or x%5==0) at 0x011C1990> Is a generator expression. If you want to see what it generates, create e.g. a tuple >>> tuple(x for x in xrange(1,100) if x%3==0 or x%5==0) (3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 95, 96, 99) So you see that the generator selects those integers that are divisible by 3 or 5 >>> sum(x for x in xrange(1,100) if x%3==0 or x%5==0) 2318 This calculates the some of those. There are quite a couple of functions in Python that accept generator expressions as arguments Regards, Gregor Can anyone shed some light on what it is I'm not quite grasping between the two? Christer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehensions
Hello, On Wed, Oct 7, 2009 at 6:57 AM, Christer Edwards wrote: > > do something to x for each x in list, with an optional qualifier. > To be more precise: http://docs.python.org/tutorial/datastructures.html#list-comprehensions "Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses" > On the other hand I've seen a few examples that look similar, but > there is no action done to the first x, which confuses me. An example: > > print sum(x for x in xrange(1,1000) if x%3==0 or x%5==0) So, it's not an 'action' as such, but an expression, x in this case is a variable containing a number, thus stating 'x' by itself is an expression that yields x's value and adds it to the sequence being generated without doing anything else to it. The built-in function sum (on the interpreter, type: help(sum)) takes a sequence of (numeric) values and returns their... sum :-). > In this case is sum() acting as the "action" on the first x? Can > anyone shed some light on what it is I'm not quite grasping between > the two? Hence, sum does not know about x at all, just about the end result of the generator expression: the generated sequence first evaluated, then passed into it as an argument. -- Kamal -- There is more to life than increasing its speed. -- Mahatma Gandhi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list comprehensions
> I've been studying python now for a few weeks and I've recently come > into list comprehensions. [...] > Those make sense to me. The way I understand them is: > do something to x for each x in list, with an optional qualifier. that's pretty much correct. > On the other hand I've seen a few examples that look similar, but > there is no action done to the first x, which confuses me. An example: > > print sum(x for x in xrange(1,1000) if x%3==0 or x%5==0) > > In this case is sum() acting as the "action" on the first x? the "do something" can also include "do nothing!" in other words, you can simply just select certain elements that meet your particular criteria. for example, let's say you just want all the odd numbers from 0 to 19: odd = [x for x in range(20) if x % 2 != 0] you just wanted the numbers but not to really do anything with them other than have them as elements in a newly-created list. your question also brought up "generator expressions" which are lazier, more memory-friendly alternative to "listcomps." the syntax is nearly identical, with the only difference being that they're enclosed by parentheses instead of square brackets: odd_gen = (x for x in range(20) if x % 2 != 0) that's the only difference syntactically. however, there is a bigger difference under the covers: >>> odd [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] >>> odd_gen at 0x012F44E0> notice that memory is allocated and the entire list created in memory for the listcomp but just a generic object for the "genexp." they are "lazy" because you iterate over the values one at a time instead of creating the entire list. they are slightly slower but save memory. similar constructs for dicts and sets appear in Python 3: dictionary and set comprehensions. back to your question regarding sum(). sum() just iterates over the genexp and adds up all the individual numbers iterated over. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor