Python rocks. I'll qualify myself as a techie. I've been dealing with computers since this baby back in the early 80s. I've programmed in quite a few languages and when I first read about Python, I couldn't get over the fact that a modern language actually was whitespace sensitive. I kept saying "what is it, FORTRAN?" I did the Hello World but I just couldn't get it or into it. Fortunately for me, the DBA where I worked told me I should really give it a second look. I respect him quite a bit, if nothing else, we are way too similar so I did and boy am I happy. It's different from most any other language I've dealt with. It's strongly dynamically typed so gone are type declarations, at least in the traditional sense. It's got some powerful built in types that make me fear going back to C++. In particular lists and dictionaries are fantastic (tuples are the third builtin type but I don't quite have the appreciation for them as the first two). The syntax is clean and so readable. I look at the early code I wrote and I can still read it, nearly a year later. It strikes me as the perfect balance between paucity and verboseness in a language. It doesn't have the wordiness of COBOL nor the seemingly random keymashing of perl. It's had the wonderful affect of letting me find joy in coding again. Not that I was hating it but I was rather bored. Sure, I could have been learning all the .Net stuff for work but it just wasn't a priority yet. Python though, wow, I love to write programs in this language! Once again, I'm looking for problems to solve just to have an excuse to program. For example, I didn't like how my music collection was arranged. Artist/Albumn/Track Number - Track Name.mp3 I think it was a 10 or 20 line program that crawled my hard drive and renamed my music as needed. Java, sure I could have done it in there, C++ maybe if I felt like the pain. VB? I could do that but I could also slam my hand in a car door but you don't see me rushing off to do that, now do you? JScript? Use Cygwin and do some bash or tcsh scripting? Perl? It was the best candidate for best solution but really for me, Python just fits so well into the sort of programming I do on a day to day basis. Yes, we are a MS shop so I should learn to pray at their altar but I can open up TextPad or Vim and start typing before Visual Studio even finishes loading.
As I learn new features or facets of this language I am absolutely sickened by the amount of time I've spent in other languages doing similar tasks. Object persistence was the one that has probably disgusted me the most. For the built in data structures, it's as simple as one line to save it out. If it's a complex object, it's still pretty much a one line operation. I wrote a spider last year to crawl a website and extract information. The first version did all sorts of nasty string manipulation looking for chunks of data. Work actually needed one of these things so I dusted mine off and the incoming HTML was completely different than the first site so I decided to figure out how the HTML parsing library worked. Again, I'm sickened. My code is now trivial and even better it's pluggable so if I have to consume a different site, I just have to change out one piece and it's ready to go rather than rewriting the parsing rules. Yes, part of that was lack of foresight on my part in the begining but the parsing is so clean and easy.
Oh well, it's getting late but I really suggest you check out Python if you think you want to pick up a new language. If anyone wants, I can throw some sample programs out here and you can see how short yet powerful they can be. Next up for fun will be a functional language, most likely O'Caml while work drags me in deeper with C#.
--------------------
I don't think of myself as a lion. You might as well, though - I have a mighty roar. Jubel Early
You have my interest. I know that linux has python as an option. Can you post some example code, I would love to see something simple to get an idea of what it is all about. Thanks for posting this!
Wow... maybe I should give python a second look. I am more of a PHP gal myself, but probably because I do more database manipulation than I do multithreaded network servers... although I am about to try to sync up data from one server to the next... I might need some info on the Python handling of Multi-thread support. Thanks for the blurp... maybe I should give it another go.
--------------------
So, I have an new site that is pretty neat, Check out Dannah's Home Page
Also, for original storys or thoughts from me, check out my blog: Dannah's Blog
I'll start with some simple things. On another board I haunt, there are a wide range of problems people need solved. This one I thought was nice because it'll show basic file operations and use some of the built in goodies.
QUOTE
I have a database that I need to reformat and incrament one of the fields by 1. the data looks like:
Is it possible to do this with a regular expresion?
The answer is that it is not possible with regex but it's trivial in Python.
Windows users, I'd recommend ActiveState's product. Install Python and you're good to go. Otherwise, grab the appropriate version for your operating system from the source.
Copy the code below and paste it into your text editor of choice. TextPad is mine but ViM works well as do others (no need to start a holy war in this thread yet). Things to note, white space determines the block level so if you decide to fiddle with stuff, keep it on the same level and don't use hard tabs (\t) use an equivalent number of spaces. Your editor ought to have a way of specifying which to use. I'll assume you've saved it out as something like csvExample.py and that you've saved the sample csv stuff in the same directory as test.csv. Windows I'll assume C:\ while *nix I'll go with ~/programs as the location.
Running the code is as simple as shelling out to your command window. Linux people it'd be something like [root@angband ~/programs]$ python csvExample.py Windows people, your command line version would be Start -> Run (cmd or command, whichever works for you) C:\> cd \ C:\> python csvExample.py
Either way, you should now have a file in the same location named test.csv.out that has incremented all the values in column 3 by one.
CODE
import csv
def process(fileName): #create a function """ This is how you document a function. It allows for some slick reflection of objects. """ fin = open(fileName, 'r') #get a handle on the file to be opened fout = open(fileName + '.out', 'wb') #get a handle for the output file
r = csv.reader(fin) #create an instance of a CSV reader w = csv.writer(fout) #create an instance of a CSV writer #Uncomment the following line (remove the #) if there is a header row # w.writerow(r.next())
for line in r: #loop through each line in r line is a list which is like an array on crack line[2] = str(int(line[2]) + 1) #Assign the third column (0 based numbering) # the value of the third column converted to # a whole number, with one added to it # and converted back to a string w.writerow(line) #write the current list back out to the output file
def main(): #there are 3 ways of representing a string # single ticks 'I am a string' # quotes "I too am a string" # triple quotes """I am a string that can cross line boundaries no problem""" fileName = r'.\test.csv' #create a string with the value .\test.csv #the r prefacing the string means raw # so if there was a \n or \t or other special characters #(C/C++ and Java people know what I'm talking about) # it won't be treated as special process(fileName) #call the real function
if __name__ == '__main__': #This basically says if you meant to execute this main() # module, then do what's in here
Anything in particular anyone wants to see? I'm thinking about showing an example of using ADO with Python (I do lots of work ripping files apart and pushing data into or out of SQL Server) but I'm open to requests or suggestions.
[edit]Added the b to the writer file mode to specify binary mode. I was having issues with a cr lf lf being written out at my house[/edit]
Perhaps that was a bit much for a first taste. This will show the basics of the built in list data types.
Oh and for those who like the fancy editors, may I suggest PythonWin (comes with ActiveState's version) for Windows users and Boa Constructor for any platform. Both offer intellisense type of completion and lots of other swank features. You can also just type python at the command prompt and type stuff in interactively. I use it quite a bit just to figure out what methods are available in a module.
CODE
l = [] #an empty list l.append(0) #add the number 0 l.append(1) #add the number 1 l.append("String here") #add a string, lists are heterogenous print (l) #show what's in the list
l.append(1) #no problem with putting duplicates in a list l.append(1) #seriously, just keep putting as much l.append(1) # as you want into it l.append(1) # the list will grow to meet your needs
#How many instances of 1 are in my list?
#You could loop through all the values in the list # and count each time you find an instance #It would look something like # traditional for loops are available in the language but these # are so much easier and less error prone! oneCount = 0 for x in l: if (x == 1): oneCount += 1 #shorthand for oneCount = oneCount + 1 print 'Looping version to find the count ==>', oneCount
#or I could have simply said print 'Easy version to find the count ==> %s' % (l.count(1)) #For those familiar with printf
#I'll let you decide which is more useful
#Need to find the first instance of something in your list? print l.index("String here")
l.append(3) #add three to end of our list l.remove(3) #changed my mind and removed the first instance of 3
l.append(3) #add 3 to end of our list l.append(4) #add 4 to end of the list l = l[:-2] #changed my mind about both so I sliced the last two elements off of # my list and assigned the results back to the list
print type(l) print dir(l) #get a directory of what's available in a list #ignore the stuff that starts with __ for now #Hmmm that sort looks interesting print dir(l.sort)
#that wasn't too useful, how about help? print help(l.sort) #Much better #Use dir and help to your heart's content to figure out what you can do
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)