Welcome Guest ( Log In | Register )
   Mobile App






Reply to this topicStart new topicStart Poll

> Python, Just a fanboy's observation
Bookmark and Share
talleyrand 
Posted: 01-May-2004, 10:52 PM
Quote Post

Member is Offline



Celtic Guardian
********

Group: Celtic Nation
Posts: 279
Joined: 26-Jan-2004
ZodiacAlder

Realm: Kansas City, Missouri USA

male





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
PMEmail PosterUsers WebsiteMy Photo Album               View My Space Profile.
Top
CelticRadio 
Posted: 04-May-2004, 10:00 PM
Quote Post

Member is Offline



Celtic Guardian
Group Icon

Group: Administrator
Posts: 8,239
Joined: 23-Sep-2001
ZodiacOak

Realm: Boston, MA

male





TalleyRand,

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! thumbs_up.gif


--------------------
Wallace MacArthur
http://www.CelticRadio.net
Celtic Radio Music Network

Imagination is more Important than Knowledge
PMEmail PosterUsers Website My Photo Album               View my Facebook Profile.View my Linked-In Profile.View my Google plus Profile.View my Twitter Profile.View My Space Profile.View my YouTube Profile.
Top
Danann 
Posted: 05-May-2004, 09:49 AM
Quote Post

Member is Offline



Celtic Guardian
********

Group: Celtic Nation
Posts: 526
Joined: 02-Dec-2003
ZodiacHolly

Realm: Alabama

female





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
PMEmail PosterUsers Website               
Top
Aaediwen 
Posted: 05-May-2004, 04:23 PM
Quote Post

Member is Offline



Celtic Guardian
Group Icon

Group: Super Moderator
Posts: 3,061
Joined: 09-Oct-2003
ZodiacHolly

Realm: Kentucky

male





I keep thinking about experimenting with Python or Perk, but I just need an application for it =)


--------------------
Poet and seeker of knowledge



Mountain Legacy -- Born in the isles, raised in Appalachia
PMEmail Poster My Photo Album               View My Space Profile.
Top
talleyrand 
Posted: 05-May-2004, 10:33 PM
Quote Post

Member is Offline



Celtic Guardian
********

Group: Celtic Nation
Posts: 279
Joined: 26-Jan-2004
ZodiacAlder

Realm: Kansas City, Missouri USA

male





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:

4,22,208,,1,2/6/2004 8:53:22,6,,,,0
7,23,165,,1,2/6/2004 8:53:22,5,,,,0
23,1,209,,1,2/6/2004 8:53:22,5,,,,0

It is the 3rd field that I need to increment so the above would change to:

4,22,209,,1,2/6/2004 8:53:22,6,,,,0
7,23,166,,1,2/6/2004 8:53:22,5,,,,0
23,1,210,,1,2/6/2004 8:53:22,5,,,,0

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]
PMEmail PosterUsers WebsiteMy Photo Album               View My Space Profile.
Top
talleyrand 
Posted: 05-May-2004, 10:54 PM
Quote Post

Member is Offline



Celtic Guardian
********

Group: Celtic Nation
Posts: 279
Joined: 26-Jan-2004
ZodiacAlder

Realm: Kansas City, Missouri USA

male





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
PMEmail PosterUsers WebsiteMy Photo Album               View My Space Profile.
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Reply to this topicStart new topicStart Poll

 








Celtic RadioTM broadcasts through Live365.com and StreamLicensing.com which are officially licensed under SoundExchange, ASCAP, BMI, SESAC and SOCAN.
2014 Celtic Radio Network, Highlander Radio, Celtic Moon, Celtic Dance, Ye O' Celtic Pub and Celt-Rock-Radio.
All rights and trademarks reserved. Read our Privacy Policy.
Celtic Graphics 2014, Cari Buziak


Link to CelticRadio.net!
Link to CelticRadio.net
View Broadcast Status and Statistics!

Best Viewed With IE 8.0 (1680 x 1050 Resolution), Javascript & Cookies Enabled.


[Home] [Top]

Celtic Hearts Gallery | Celtic Mates Dating | My Celtic Friends | Celtic Music Radio | Family Heraldry | Medival Kingdom | Top Celtic Sites | Web Celt Blog | Video Celt