How did you learn Python?
I was just wondering what the best books were for learning Python. Which books are good for getting started, and which should be saved for later, or or not useful except as a reference for the learned? I have a decent programming background in VB, JavaScript, VBScript, Net.Data (IBM's macro language), regular expressions, and a teensy bit of Perl. My point is, I don't want something that is going to explain the basic programming concepts, but does give a good introduction to Python-specific things. Then, once I know how to get the job done, I would like a good book or two at the intermediate to advanced level, to learn how to write really good code. I understand that resources such as this list and Google searches have all the answers, but it seems like a more structured tool, such as a book or formal class, would be of great benefit to me. The other languages I have used were picked up because of the need to get a job done. As a result, I am able to get the job done, but any experienced coder can show me six more efficient ways to do what I'm doing. I'm new to Python, and I want to do this one right. I believe that Python will be around for a good, long time, and it matches my values as an Open-Source/Linux supporter, while having relevance in the Windows and Mac world, as well. Plus, it looks like it was designed extremely well, and I'm excited about the principles I've read about. Thanks, Shawn -- http://mail.python.org/mailman/listinfo/python-list
Re: Best book on Python?
I asked some questions last week about how those in the group learned Python, and I was given some suggestions. I ended up buying several books. Here are my brief opinions about them. In case it seems that I am too positive about these books, please take into consideration that I purchased each of them after reading plenty of reviews, and several suggestions from other list members. This list is the cream of the crop. In short, I would recommend that anyone keep all four next to the keyboard. If cost is an issue, I recommend getting them in the following order. "Dive Into Python" is available for free from diveintopython.org, but it is listed first because I think it is of the greatest immediate value. Dive Into Python Python in a Nutshell Python Cookbook Learning Python Shawn Python Cookbook Very useful as a reference. There are examples for a great many things. Almost everything I've looked for is in this book. The only downside is that the samples are sometimes too advanced for me at my beginner level. I assume that this book is meant for readers with more that a couple of weeks' experience with Python, so I doubt that the problem is with the book. Python in a Nutshell The best reference, because of the sheer volume of content. The only drawback is that, although all the options are there, clear explanations of how to make use of them are not provided, due to space considerations. This is not a negative comment -- once you have direction, you can pick up the rest elsewhere. However, I would not suggest using this as the sole reference. Dive Into Python This book is awesome. I started reading this before the others arrived. I didn't get too far into it, because I jumped directly into a project for work using Python, so I'm limping along, mainly using all three O'Reilly books as references. But this book jumps right into useful code, and does a good job of explaining it. I should have completed this book before moving on. Learning Python This book seems too basic to be used as the sole learning tool, unless the person is new to programming, not just Python. But the book does contain a lot of valuable information, and the depth of the explainations makes it a good companion to the others in my little reference set. -- http://mail.python.org/mailman/listinfo/python-list
Accessing DB2 with Python
Is anyone doing this? I would like to access a DB2 database (IBM's database) with Python. I checked "Python in a Nutshell," and it refers to ftp://people.linuxkorea.co.kr/pub/db2, but I am unable to connect to that site, although it could be a firewall issue, as I am at work. Is there any commonly used module for this? Thanks, Shawn -- http://mail.python.org/mailman/listinfo/python-list
Perl and Python, a practical side-by-side example.
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The run speed is similar (very fast) and the line count is similar. Now that they're both working, I was looking at the code and wondering what Perl-specific and Python-specific improvements to the code would look like, as judged by others more knowledgeable in the individual languages. I am not looking for the smallest number of lines, or anything else that would make the code more difficult to read in six months. Just any instances where I'm doing something inefficiently or in a "bad" way. I'm attaching both the Perl and Python versions, and I'm open to comments on either. The script reads a file from standard input and finds the best record for each unique ID (piid). The best is defined as follows: The newest expiration date (field 5) for the record with the state (field 1) which matches the desired state (field 6). If there is no record matching the desired state, then just take the newest expiration date. Thanks for taking the time to look at these. Shawn ## Perl code: ## #! /usr/bin/env perl use warnings; use strict; my $piid; my $row; my %input; my $best; my $curr; foreach $row (<>){ chomp($row); $piid = (split(/\t/, $row))[0]; push ( @{$input{$piid}}, $row ); } for $piid (keys(%input)){ $best = ""; for $curr (@{$input{$piid}}){ if ($best eq ""){ $best = $curr; }else{ #If the current record is the correct state if ((split(/\t/, $curr))[1] eq (split(/\t/, $curr))[6]){ #If existing record is the correct state if ((split(/\t/, $best))[1] eq (split(/\t/, $curr))[6]){ if ((split(/\t/, $curr))[5] gt (split(/\t/, $best))[5]){ $best = $curr; } }else{ $best = $curr; } }else{ #if the existing record does not have the correct state #and the new one has a newer expiration date if (((split(/\t/, $best))[1] ne (split(/\t/, $curr))[6]) and ((split(/\t/, $curr))[5] gt (split(/\t/, $best))[5])){ $best = $curr; } } } } print "$best\n"; } ## End Perl code ## ## Python code ## #! /usr/bin/env python import sys input = sys.stdin recs = {} for row in input: row = row.rstrip('\n') piid = row.split('\t')[0] if recs.has_key(piid) is False: recs[piid] = [] recs[piid].append(row) for piid in recs.keys(): best = "" for current in recs[piid]: if best == "": best = current; else: #If the current record is the correct state if current.split("\t")[1] == current.split("\t")[6]: #If the existing record is the correct state if best.split("\t")[1] == best.split("\t")[6]: #If the new record has a newer exp. date if current.split("\t")[5] > best.split("\t")[5]: best = current else: best = current else: #If the existing record does not have the correct state #and the new record has a newer exp. date if best.split("\t")[1] != best.split("\t")[6] and current.split("\t")[5] > best.split("\t")[5]: best = current print best ## End Python code ## -- http://mail.python.org/mailman/listinfo/python-list
Re: Python new user question - file writeline error
On 7 Feb 2007 11:31:32 -0800, James <[EMAIL PROTECTED]> wrote: > Hello, > > I'm a newbie to Python & wondering someone can help me with this... > > I have this code: > -- > #! /usr/bin/python > > import sys > > month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG': > 8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} > infile=file('TVA-0316','r') > outfile=file('tmp.out','w') > > for line in infile: > item = line.split(',') > dob = item[6].split('/') > dob = dob[2]+'-'+str(month[dob[1]])+'-'+dob[0] > lbdt = item[8].split('/') > lbdt = lbdt[2]+'-'+str(month[lbdt[1]])+'-'+lbdt[0] > lbrc = item[10].split('/') > lbrc = lbrc[2]+'-'+str(month[lbrc[1]])+'-'+lbrc[0] > lbrp = item[14].split('/') > lbrp = lbrp[2]+'-'+str(month[lbrp[1]])+'-'+lbrp[0] > item[6] = dob > item[8] = lbdt > item[10]=lbrc > item[14]=lbrp > list = ','.join(item) > outfile.writelines(list) > infile.close > outfile.close > - > > And the data file(TVA-0316) looks like this: > - > 06-0588,03,701,03701,046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/ > NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,, > 06-0588,03,701,03701,046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/ > NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h, > 06-0588,03,701,03701,046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/ > NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h, > 06-0588,03,701,03701,046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/ > NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,, > - > > Basically I'm reading in each line and converting all date fields (05/ > MAR/1950) to different format (1950-03-05) in order to load into MySQL > table. > > I have two issues: > 1. the outfile doesn't complete with no error message. when I check > the last line in the python interpreter, it has read and processed the > last line, but the output file stopped before. > 2. Is this the best way to do this in Python? > 3. (Out of scope) is there a way to load this CSV file directly into > MySQL data field without converting the format? > > Thank you. > > James > > -- > http://mail.python.org/mailman/listinfo/python-list > Your script worked for me. I'm not sure what the next step is in troubleshooting it. Is it possible that your whitespace isn't quite right? I had to reformat it, but I assume it was because of the way cut & paste worked from Gmail. I usually use Perl for data stuff like this, but I don't see why Python wouldn't be a great solution. However, I would re-write it using regexes, to seek and replace sections that are formatted like a date, rather than breaking it into a variable for each field, changing each date individually, then putting them back together. As for how MySQL likes having dates formatted in CSV input: I can't help there, but I'm sure someone else can. I'm pretty new to Python myself, but if you'd like help with a Perl/regex solution, I'm up for it. For that matter, whipping up a Python/regex solution would probably be good for me. Let me know. Shawn -- http://mail.python.org/mailman/listinfo/python-list
Fwd: Python new user question - file writeline error
To the list: I have come up with something that's working fine. However, I'm fairly new to Python, so I'd really appreciate any suggestions on how this can be made more Pythonic. Thanks, Shawn Okay, here's what I have come up with: #! /usr/bin/python import sys import re month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} infile=file('TVA-0316','r') outfile=file('tmp.out','w') def formatDatePart(x): "take a number and transform it into a two-character string, zero padded" x = str(x) while len(x) < 2: x = "0" + x return x regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},") for line in infile: matches = regex.findall(line) for someDate in matches: dayNum = formatDatePart(someDate[1:3]) monthNum = formatDatePart(month[someDate[4:7]]) yearNum = formatDatePart(someDate[8:12]) newDate = ",%s-%s-%s," % (yearNum,monthNum,dayNum) line = line.replace(someDate, newDate) outfile.writelines(line) infile.close outfile.close -- http://mail.python.org/mailman/listinfo/python-list
Re: python linux distro
On 2/8/07, dimitri pater <[EMAIL PROTECTED]> wrote: > Hi, > the world doesn't need another Linux distro, there are too many already... > (> 100) > I believe it's a better idea to spend your time contributing to an existing > distro (e.g. http://www.ubuntu.com/developers/bounties) > doing Python related stuff. Besides that, all distros I know of (4) already > have a lot of Python packages ready for download. > regards, > Dimitri > > You're right, there are too many. Not just over 100, but over 500. As of this week, DistroWatch reports having 528 in their database. Check out DistroWatch.com for details. To the original poster: You may not be a Linux expert, but if you feel like reading some documentation, you can easily remaster a live CD such as Knoppix or DSL (Damn Small Linux). Just to to the distro's site and check out the documentation. This won't be the creation of a brand-new distro -- it's the same thing as changing the wallpaper and creating your custom re-master just for yourself. Both distros I mentioned (and many others, I'm sure) have a simple, built-in tool for installing additional packages. Do that, then make your re-image from your running live version. Shawn -- http://mail.python.org/mailman/listinfo/python-list
Re: Strings in Python
On 8 Feb 2007 08:28:25 -0800, Johny <[EMAIL PROTECTED]> wrote: > Playing a little more with strings, I found out that string.find > function provides the position of > the first occurance of the substring in the string. > Is there a way how to find out all substring's position ? > To explain more, > let's suppose > > mystring='12341' > import string > > >>> string.find(mystring ,'1') > 0 > > But I need to find the possition the other '1' in mystring too. > Is it possible? > Or must I use regex? > Thanks for help > L > > -- > http://mail.python.org/mailman/listinfo/python-list > Loop it -- once you know the index of the first character, add the third argument to string.find(), which tells it the position at which to start (the last find + 1). -- http://mail.python.org/mailman/listinfo/python-list
Re: Strings in Python
On 2/8/07, Gary Herron <[EMAIL PROTECTED]> wrote: > Johny wrote: > > Playing a little more with strings, I found out that string.find > > function provides the position of > > the first occurance of the substring in the string. > > Is there a way how to find out all substring's position ? > > To explain more, > > let's suppose > > > > mystring='12341' > > import string > > > > > string.find(mystring ,'1') > > > 0 > > > > But I need to find the possition the other '1' in mystring too. > > Is it possible? > > Or must I use regex? > > Thanks for help > > L > > > > > You could use a regular expression. The re module has s function > "findall" that does what you want. > > Also, if you read the documentation for strings find method, you'll find: > > 1 S.find(sub [,start [,end]]) -> int > 2 > 3 Return the lowest index in S where substring sub is found, > 4 such that sub is contained within s[start,end]. Optional > 5 arguments start and end are interpreted as in slice notation. > 6 > 7 Return -1 on failure. > > So put your find in a loop, starting the search one past the previously > found occurrence. > > i = string.find(mystring, i+1) > > Gary Herron > > > -- > http://mail.python.org/mailman/listinfo/python-list > Speaking of regex examples, that's basically what I did in the script below which James Kim and I were collaborating on yesterday and this morning, as a result of his thread. This matches not only a string, but a regex, then loops through each match to do something to it. I hope this helps. I submitted this to the list for recommendations on how to make it more Pythonic, but at least it works. Here are the most important, stripped down pieces: #! /usr/bin/python import re #match a date in this format: 05/MAR/2006 regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},") for line in infile: matches = regex.findall(line) for someDate in matches: newDate = #do something here line = line.replace(someDate, newDate) Here is the full script: #! /usr/bin/python import sys import re month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} infile=file('TVA-0316','r') outfile=file('tmp.out','w') def formatDatePart(x): "take a number and transform it into a two-character string, zero padded" x = str(x) while len(x) < 2: x = "0" + x return x regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},") for line in infile: matches = regex.findall(line) for someDate in matches: dayNum = formatDatePart(someDate[1:3]) monthNum = formatDatePart(month[someDate[4:7]]) yearNum = formatDatePart(someDate[8:12]) newDate = ",%s-%s-%s," % (yearNum,monthNum,dayNum) line = line.replace(someDate, newDate) outfile.writelines(line) infile.close outfile.close -- http://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Python new user question - file writeline error
On 8 Feb 2007 09:05:51 -0800, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > On 8 feb, 12:41, "Shawn Milo" <[EMAIL PROTECTED]> wrote: > > > I have come up with something that's working fine. However, I'm fairly > > new to Python, so I'd really appreciate any suggestions on how this > > can be made more Pythonic. > > A few comments: > > You don't need the formatDatePart function; delete it, and replace > newDate = ",%s-%s-%s," % (yearNum,monthNum,dayNum) > with > newDate = ",%04.4d-%02.2d-%02.2d," % (yearNum,monthNum,dayNum) > > and before: > dayNum, monthNum, yearNum = [int(num) for num in > someDate[1:-1].split('/')] > > And this: outfile.writelines(line) > should be: outfile.write(line) > (writelines works almost by accident here). > > You forget again to use () to call the close methods: > infile.close() > outfile.close() > > I don't like the final replace, but for a script like this I think > it's OK. > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > Gabriel, Thanks for the comments! The new version is below. I thought it made a little more sense to format the newDate = ... line the way I have it below, although I did incorporate your suggestions. Also, the formatting options you provided seemed to specify not only string padding, but also decimal places, so I changed it. Please let me know if there is some other meaning behind the way you did it. As for not liking the replace line, what would you suggest instead? Shawn #! /usr/bin/python import sys import re month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} infile=file('TVA-0316','r') outfile=file('tmp.out','w') regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},") for line in infile: matches = regex.findall(line) for someDate in matches: dayNum = someDate[1:3] monthNum = month[someDate[4:7]] yearNum = someDate[8:12] newDate = ",%04d-%02d-%02d," % (int(yearNum),int(monthNum),int(dayNum)) line = line.replace(someDate, newDate) outfile.write(line) infile.close() outfile.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Python new user question - file writeline error
On 2/8/07, Jussi Salmela <[EMAIL PROTECTED]> wrote: > Shawn Milo kirjoitti: > > To the list: > > > > I have come up with something that's working fine. However, I'm fairly > > new to Python, so I'd really appreciate any suggestions on how this > > can be made more Pythonic. > > > > Thanks, > > Shawn > > > > > > > > > > > > > > Okay, here's what I have come up with: > > What follows may feel harsh but you asked for it ;) > > > > > > > #! /usr/bin/python > > > > import sys > > import re > > > > month > > ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12} > > > > infile=file('TVA-0316','r') > > outfile=file('tmp.out','w') > > > > def formatDatePart(x): > >"take a number and transform it into a two-character string, > > zero padded" > If a comment or doc string is misleading one would be better off without > it entirely: > "take a number": the function can in fact take (at least) > any base type > "transform it": the function doesn't transform x to anything > although the name of the variable x is the same > as the argument x > "two-character string": to a string of at least 2 chars > "zero padded": where left/right??? > >x = str(x) > >while len(x) < 2: > >x = "0" + x > You don't need loops for these kind of things. One possibility is to > replace the whole body with: > return str(x).zfill(2) > >return x > > > > regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},") > > > > for line in infile: > >matches = regex.findall(line) > >for someDate in matches: > > > Empty lines are supposed to make code more readable. The above empty > line does the contrary by separating the block controlled by the for > and the for statement > >dayNum = formatDatePart(someDate[1:3]) > >monthNum = formatDatePart(month[someDate[4:7]]) > >yearNum = formatDatePart(someDate[8:12]) > You don't need the formatDatePart function at all: > newDate = ",%4s-%02d-%2s," % \ > (someDate[8:12],month[someDate[4:7]],someDate[1:3]) > > > >newDate = ",%s-%s-%s," % (yearNum,monthNum,dayNum) > >line = line.replace(someDate, newDate) > > > >outfile.writelines(line) > > > > infile.close > > outfile.close > You have not read the answers given to the OP, have you. Because if you > had, your code would be: > infile.close() > outfile.close() > The reason your version seems to be working, is that you probably > execute your code from the command-line and exiting from Python to > command-line closes the files, even if you don't. > > Cheers, > Jussi > -- > http://mail.python.org/mailman/listinfo/python-list > Jussi, Thanks for the feedback. I received similar comments on a couple of those items, and posted a newer version an hour or two ago. I think the only thing missing there is a friendly blank line after my "for line in infile:" statement. Please let me know if there is anything else. Shawn -- http://mail.python.org/mailman/listinfo/python-list
Re: Hacking in python
On 2/10/07, hg <[EMAIL PROTECTED]> wrote: > Calvin Spealman wrote: > > > http://en.wikipedia.org/wiki/Hacker_%28disambiguation%29 > > > > Educate yourself on what hacking actually is. We're all hackers, > > because it just means we get the most out of code, enjoy pushing our > > technology to the limit, and generally love programming. The term has > > been abused by the media and you don't do much more than show your own > > naiveness by asking such a question. You also do a great job of > > insulting everyone on this list. > > > > On 2/10/07, enes naci <[EMAIL PROTECTED]> wrote: > >> > >> i would like to know about hacking in python too whether its illegal > >> or not is not the point and anyway it doesn't mean i'm gong to use it. > >> > >> -- > >> http://mail.python.org/mailman/listinfo/python-list > >> > > > > > > -- > > Read my blog! I depend on your acceptance of my opinion! I am interesting! > > http://ironfroggy-code.blogspot.com/ > > > So that was that weird feeling I felt ... insulted > > hg > > -- > http://mail.python.org/mailman/listinfo/python-list > I'm surprised you all didn't just tell him how to become a hacker. That's what he wants, right? Here's the hacker info. Enjoy! http://www.catb.org/~esr/faqs/hacker-howto.html Shawn -- http://mail.python.org/mailman/listinfo/python-list
Re: Hacking in python
On 2/10/07, Tina I <[EMAIL PROTECTED]> wrote: > zefciu wrote: > > enes naci wrote: > >> i would like to know about hacking in python too whether its illegal or > >> not is not the point and anyway it doesn't mean i'm gong to use it. > >> > > > > If you mean hacking as modyfying the code of interpreter of libraries - > > it is perfectly legal, as Python is Open Source. > > > > If you mean hacking as cracking into computer systems, then what's the > > difference if it's with Python or anything else. > > > > If you mean hacking as gaining excellency in programming - then why > > should it be? > > > > Greets > > zefciu > It's really sad. I saw this poor schmuck on "Want to be a millionaire" > once. His second question was "What is a hacker?" I don't remember all > of the alternatives but two of them was "A computer programmer" and > "Someone illegally using a computer". > He answered 'computer programmer'... guess what was the 'correct one'. > > I guess he was lucky though... it could have been the one million question. > -- > http://mail.python.org/mailman/listinfo/python-list > That's truly horrible. I would sue. -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expressions
On 10 Feb 2007 18:58:51 -0800, gregarican <[EMAIL PROTECTED]> wrote: > On Feb 10, 6:26 pm, "Geoff Hill" <[EMAIL PROTECTED]> wrote: > > What's the way to go about learning Python's regular expressions? I feel > > like such an idiot - being so strong in a programming language but knowing > > nothing about RE. > > I highly recommend reading the book "Mastering Regular Expressions," > which I believe is published by O'Reilly. It's a great reference and > helps peel the onion in terms of working through RE. They are a > language unto themselves. A fun brain exercise. > > -- > http://mail.python.org/mailman/listinfo/python-list > Absolutely: Get "Mastering Regular Expressions" by Jeffrey Friedl. Not only is it easy to read, but you'll get a lot of mileage out of regexes in general. Grep, Perl one-liners, Python, and other tools use regexes, and you'll find that they are really clever little creatures once you befriend a few of them. Shawn -- http://mail.python.org/mailman/listinfo/python-list