From ian-campbell at shaw.ca Fri May 1 17:45:04 2009 From: ian-campbell at shaw.ca (Ian Campbell) Date: Fri, 01 May 2009 08:45:04 -0700 Subject: [Tutor] Running subProcesses in parallel for mixed external Windows.exe and Python functions on specified CPUs. Message-ID: <49FB1900.1000207@shaw.ca> Running subProcesses in parallel for mixed external Windows.exe and Python functions on specified CPUs. How does a newbie start to make this work? def startMyFiles(): (not real code, this is only for explaining my problem) import OS import time import sys import subprocess from win32event import WaitForSingleObject, WAIT_TIMEOUT from win32api import TerminateProcess import myNewPythonCodeReadWriteSQLite # my new module import myNewPythonCodeReadSQLiteShowMatPlotLib # my new module "show" here? or "draw"? # these two Windows executables should run in sequence and on CPU_2 # but as a group of 2 run in parallel with the next 2 groups p11 = subprocess.Popen("C:\myDdeServer.exe ") # loads DDE Server p12 = subprocess.Popen("c:\ddeClicks.ahk ") # starts dde server with clicks and keyboard strokes # these two should run in sequence on CPU_3 # but as a group of 2 run in parallel with the next group p21 = subprocess.Popen("C:\showRawData.xlsm ") # loads Excel spreadsheet to show incoming data in real time p22 = subprocess.Popen("c:\xlsClicks.ahk ") # send mouse clicks and keyboard strokes to start Excel # these two should run in sequence on CPU_4 # but as a group of 2 run in parallel with the prior 2 groups p31 = subprocess.Popen("pw5.exe c:\legacyScript.was") # loads legacy code to read and write from spreadsheet to SQLite data base p32 = subprocess.Popen("c:\wasClicks.ahk ") # sends mouse clicks and keyboard strokes to start legacy code #this new Python code runs in parallel for ever on CPU_2 readWriteSQLiteDef() # from myNewPythonCodeReadWriteSQLite Module # runs for ever to read and write and refresh SQLite in parallel what subprocess? #this new Python code runs in parallel for ever on CPU_3 readSQLiteShowMatPlotLibDef() # from myNewPythonCodeReadSQLiteShowMatPlotLib Module # runs for ever to refresh graphics from SQLite what subprocess? From alan.gauld at btinternet.com Fri May 1 18:00:56 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 May 2009 17:00:56 +0100 Subject: [Tutor] Running subProcesses in parallel for mixed external Windows.exe and Python functions on specified CPUs. References: <49FB1900.1000207@shaw.ca> Message-ID: "Ian Campbell" wrote > Running subProcesses in parallel for mixed external Windows.exe and > Python functions on specified CPUs. This is getting a wee bit technical for the tutor list and only slightly related to Python - the real issue is how to do it using the Win32 API. I suspect you would be better trying on the main comp.lang.python newsgroup. That having been said I am constantly surprised by the breadth of specialist knowledge on this list so you may yet get an answer here! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mail at timgolden.me.uk Fri May 1 18:04:13 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 01 May 2009 17:04:13 +0100 Subject: [Tutor] Running subProcesses in parallel for mixed external Windows.exe and Python functions on specified CPUs. In-Reply-To: <49FB1900.1000207@shaw.ca> References: <49FB1900.1000207@shaw.ca> Message-ID: <49FB1D7D.1020708@timgolden.me.uk> Ian Campbell wrote: > Running subProcesses in parallel for mixed external Windows.exe and > Python functions on specified CPUs. > > How does a newbie start to make this work? [... snip most stuff ...] > p11 = subprocess.Popen("C:\myDdeServer.exe ") # loads > DDE Server p12 = subprocess.Popen("c:\ddeClicks.ahk > group p21 = subprocess.Popen("C:\showRawData.xlsm > p22 = subprocess.Popen("c:\xlsClicks.ahk ") # send > subprocess.Popen("pw5.exe c:\legacyScript.was") # loads legacy > p32 = subprocess.Popen("c:\wasClicks.ahk ") # You're going to have to use raw strings or reverse the backslashes on those paths: r"C:\myDdeServer.exe " or "C:/myDdeServer.exe " Haven't looked at the rest of the code yet, not least because it comes out a bit messy on my email client. TJG From pablofaria at gmail.com Fri May 1 20:19:29 2009 From: pablofaria at gmail.com (Pablo P. F. de Faria) Date: Fri, 1 May 2009 15:19:29 -0300 Subject: [Tutor] Encode problem Message-ID: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> Hi, all. I need some help. I'm trying to save a configuration file, using ConfigParser. The code is the following: self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) As one can see, the character encoding is explicitly UTF-8. But ConfigParser keeps trying to save it as a 'ascii' file and gives me error for directory-names containing >128 code characters (like "?"). It is just a horrible thing to me, for my app will be used mostly by brazillians. What can I do? I tried everything (encode/decode). The only solution I've found until now is to change the "site.py" file (sys.setdefaultencoding('utf-8'), but I can't do that for every machine that will run my app... So, there must be another way. What is strange for me is that the File object is explicit about the encoding. Another info: while running my app inside Eclipse this erros does not occur. Platforms tested: Ubuntu 8.04 and Windows XP SP3 (Python 2.5) Any ideas? Thanks. -- --------------------------------- "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." (Oscar Wilde) --------------------------------- Pablo Faria pablofaria at gmail.com From kent37 at tds.net Fri May 1 21:55:25 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 1 May 2009 15:55:25 -0400 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> Message-ID: <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> On Fri, May 1, 2009 at 2:19 PM, Pablo P. F. de Faria wrote: > Hi, all. I need some help. > > I'm trying to save a configuration file, using ConfigParser. The code > is the following: > > self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) > > As one can see, the character encoding is explicitly UTF-8. But > ConfigParser keeps trying to save it as a 'ascii' file and gives me > error for directory-names containing >128 code characters (like "?"). > It is just a horrible thing to me, for my app will be used mostly by > brazillians. What is the exact error and stack trace you get? What directory name is the problem? Is it the directory where you are saving the file or a directory name that is in the configuration data? Kent From denis.spir at free.fr Fri May 1 22:06:01 2009 From: denis.spir at free.fr (spir) Date: Fri, 1 May 2009 22:06:01 +0200 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> Message-ID: <20090501220601.31891dfc@o> Le Fri, 1 May 2009 15:19:29 -0300, "Pablo P. F. de Faria" s'exprima ainsi: > self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) > > As one can see, the character encoding is explicitly UTF-8. But > ConfigParser keeps trying to save it as a 'ascii' file and gives me > error for directory-names containing >128 code characters (like "?"). > It is just a horrible thing to me, for my app will be used mostly by > brazillians. Just superficial suggestions, only because it's 1st of May and WE so that better answers won't maybe come up before monday. If all what you describe is right, then there must be something wrong with char encoding in configParser's write method. Have you had a look at it? While I hardly imagine why/how ConfigParser would limit file pathes to 7-bit ASCII... Also, for porteguese characters, you shouldn't even need explicit encoding; they should pass through silently because they fit in an 8 bit latin charset. (I never encode french path/file names.) Can you consider writing to file "manually" if the config file structure is simple enough? I mean ConfigParser can be a great help for /parsing/ a config file. But writing it out may well be much easier, no? Denis ------ la vita e estrany From kent37 at tds.net Fri May 1 23:36:02 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 1 May 2009 17:36:02 -0400 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> Message-ID: <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria wrote: > Hi, Kent. > > The stack trace is: > > Traceback (most recent call last): > ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in OnClose > ? ?self.SavePreferences() > ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068, > in SavePreferences > ? ?self.cfg.set(u'File Settings',u'Recent files', > unicode(",".join(self.recent_files))) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position > 12: ordinal not in range(128) > > The "unicode" function, actually doesn't do any difference... The > content of the string being saved is "/home/pablo/?rea de > Trabalho/teste.xml". OK, this error is in your code, not the ConfigParser. The problem is with ",".join(self.recent_files) Are the entries in self.recent_files unicode strings? If so, then I think the join is trying to convert to a string using the default codec. Try self.cfg.set('File Settings','Recent files', ','.join(name.encode('utf-8') for name in self.recent_files)) Looking at the ConfigParser.write() code, it wants the values to be strings or convertible to strings by calling str(), so non-ascii unicode values will be a problem there. I would use plain strings for all the interaction with ConfigParser and convert to Unicode yourself. Kent PS Please Reply All to reply to the list. From sudhanshu9252 at gmail.com Sat May 2 07:41:49 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Sat, 2 May 2009 11:11:49 +0530 Subject: [Tutor] Why use decorators ? Message-ID: <6f9dbdf40905012241s760da94fuef40d8423279d8d3@mail.gmail.com> Last night was using a pyglet for creating some good graphical applications ,it was my first introduction with the pyglet,so just went on pyglet.org and downloaded a pdf file . Now simply made two programs that is mentioned in the pdf of the pyglet program 1.It is simple program that prints 'HELLO WORLD' program2.It is simple takes the keyboard and mouse events Now I am going to write both the code then regarding queries also. 1.import pyglet window=pyglet.window.Window() lable=pyglet.text.Lable('Hello, world', font_name='Times New Roman', font_size=36, x=window.width//2, y=window.height//2, anchor_x='center', anchor_y='center') *@window.event * def on_draw(): window.clear() label.draw() pyglet.app.run() okay I just drop second example just tell me why we are using that all the code that is showed in RED color . 2What is Event Handlers as well as What is DECORATORS TELL ME ALSO regards sudhanshu -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sat May 2 13:11:04 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 2 May 2009 07:11:04 -0400 Subject: [Tutor] Why use decorators ? In-Reply-To: <6f9dbdf40905012241s760da94fuef40d8423279d8d3@mail.gmail.com> References: <6f9dbdf40905012241s760da94fuef40d8423279d8d3@mail.gmail.com> Message-ID: <1c2a2c590905020411r1995869dif19430ceff5ca627@mail.gmail.com> On Sat, May 2, 2009 at 1:41 AM, sudhanshu gautam wrote: > Last night was using a pyglet for creating some good graphical applications > ,it was my first introduction with the pyglet,so just went on pyglet.org and > downloaded a pdf file . > > Now simply made two programs that is mentioned in the pdf of the pyglet > > > > program 1.It is simple program that prints 'HELLO WORLD' > > > program2.It is simple takes the keyboard and mouse events > > > Now I am going to write both the code then regarding queries also. > > 1.import pyglet > window=pyglet.window.Window() > > lable=pyglet.text.Lable('Hello, world', > ?font_name='Times New Roman', > ?font_size=36, > ?x=window.width//2, y=window.height//2, > ?anchor_x='center', anchor_y='center') > @window.event > def on_draw(): > ??? window.clear() > ??? label.draw() > pyglet.app.run() > > okay I just drop second example just tell me why we are using that all the > code that is showed in RED color . That is the code that draws the window. > 2What is Event Handlers as well as What is DECORATORS TELL ME ALSO Most GUI frameworks are "event-driven". This means that the flow of control is driven by things the user does, such as press a key or click the mouse. User actions create events. Much of a GUI program is written as event handlers - bits of code that respond to a particular user action. This is very different from, for example, a procedural style program where the flow of control starts in a main function and proceeds through a sequence of subroutine calls until it exits. GUI frameworks differ in how the event handlers are defined and connected to the framework. pyglet seems to use naming conventions to define the event to be handled. on_draw() is handling a request to draw the window. Decorators are a syntactic shortcut. What you have written could also be written as def on_draw(): ... on_draw = window.event(on_draw) I have a longer explanation here: http://personalpages.tds.net/~kent37/kk/00001.html Kent From lie.1296 at gmail.com Sat May 2 17:39:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 03 May 2009 01:39:34 +1000 Subject: [Tutor] Why use decorators ? In-Reply-To: <6f9dbdf40905012241s760da94fuef40d8423279d8d3@mail.gmail.com> References: <6f9dbdf40905012241s760da94fuef40d8423279d8d3@mail.gmail.com> Message-ID: sudhanshu gautam wrote: > 1.import pyglet > window=pyglet.window.Window() > > lable=pyglet.text.Lable('Hello, world', > font_name='Times New Roman', > font_size=36, > x=window.width//2, y=window.height//2, > anchor_x='center', anchor_y='center') > *@window.event * > def on_draw(): > window.clear() > label.draw() > pyglet.app.run() > > okay I just drop second example just tell me why we are using that all > the code that is showed in RED color . I never used pyglet, but by reading the documentation it seems that the decorator is used to register the on_draw() function to the event loop. As you know, decorator is merely a syntax sugar for calling a function like this: def on_draw(): ... on_draw = window.event(on_draw) I guess the function window.event is the function to register the on_draw() function as the event handler of the window event. In this specific case, the window.event() internally will call the window.set_handler() with the appropriate arguments. My guess is the window.event() function contains something similar to this: def event(self, f): window.add_handler(f.__name__, f) return f (of course with a bit more elaborations since it seems it also accept string argument to replace the f.__name__) From burgess.nick at gmail.com Sun May 3 22:42:49 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Sun, 3 May 2009 16:42:49 -0400 Subject: [Tutor] returning the entire line when regex matches Message-ID: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> How do I make this code print lines NOT containing the string 'Domains'? import re for line in log: if re.search (r'Domains:', line): print line This does not work... if re.search != (r'Domains:', line): From steve at alchemy.com Sun May 3 22:48:34 2009 From: steve at alchemy.com (Steve Willoughby) Date: Sun, 03 May 2009 13:48:34 -0700 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> Message-ID: <49FE0322.2050703@alchemy.com> Nick Burgess wrote: > How do I make this code print lines NOT containing the string 'Domains'? > > > import re > for line in log: > if re.search (r'Domains:', line): > print line > > > This does not work... > > if re.search != (r'Domains:', line): re.search (r'Domains:', line) is a function call which (simplifying slightly here) returns a true value if matches the regex "Domains:". It might make it more clear if you leave out the extra space there (because I think you're getting confused thinking re.search and (...) are two separate expressions since you put != between them): re.search(r'Domains:', line) If you want to print lines which do NOT match, try this: if not re.search(r'Domains:', line): print line From alan.gauld at btinternet.com Sun May 3 23:04:51 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 May 2009 22:04:51 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> Message-ID: "Nick Burgess" wrote > How do I make this code print lines NOT containing the string 'Domains'? > Don't use regex, use in: for line in log: if "Domains" in line: print line > This does not work... > > if re.search != (r'Domains:', line): Because you are assigning a tuple containing a string and a line of text to a name which is currently bound to a function object (re.search) which is not a sensible thing to do since you lose access to the search function. If you really do need to use regex you probably want: if not re.search(r'Domains', line): print line But if it is a simple string you are searching for regex is overkill and probably slower than using in. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at alchemy.com Sun May 3 23:23:06 2009 From: steve at alchemy.com (Steve Willoughby) Date: Sun, 03 May 2009 14:23:06 -0700 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> Message-ID: <49FE0B3A.3040608@alchemy.com> Alan Gauld wrote: >> if re.search != (r'Domains:', line): > > Because you are assigning a tuple containing a string and a line of text > to a name which is currently bound to a function object (re.search) Actually, he's COMPARING the two, not assigning. But comparing a function object and a tuple is still not going to do anything sensible. > But if it is a simple string you are searching for regex is overkill and > probably slower than using in. This is good advice. I was assuming that the question was about regex and would be grown into a more complex pattern match in the final application. If all you really want to do is see if a constant text string like "Domain:" is in a string, regex will work but is overkill. From alan.gauld at btinternet.com Mon May 4 00:19:14 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 May 2009 23:19:14 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <49FE0B3A.3040608@alchemy.com> Message-ID: "Steve Willoughby" wrote >>> if re.search != (r'Domains:', line): >> >> Because you are assigning a tuple containing a string and a line of text >> to a name which is currently bound to a function object (re.search) > > Actually, he's COMPARING the two, not assigning. But comparing a > function object and a tuple is still not going to do anything sensible. So he is, I didn't notice the ! in there. I really must start wearing my glasses when using the PC!!! :-( Alan G. From alan.gauld at btinternet.com Mon May 4 00:21:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 May 2009 23:21:11 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> Message-ID: "Alan Gauld" wrote >> How do I make this code print lines NOT containing the string 'Domains'? >> > > Don't use regex, use in: > > for line in log: > if "Domains" in line: > print line Should, of course, be if "Domains" not in line: print line Alan G. From cyberjacob at googlemail.com Sun May 3 17:06:51 2009 From: cyberjacob at googlemail.com (Jacob Mansfield) Date: Sun, 3 May 2009 16:06:51 +0100 Subject: [Tutor] Code Dosent work. Message-ID: hi everyone, I'm a bit new here but i was wondering if someone could check some of my code, it's not doing quite what it's meant to. thanks Databox_2_0.py: import pygame, sys, os pygame.init() def load(filename): if filename != '': e = 1 dec = "placeholder" fic = open(filename, "r") while dec != '': num = str(e) print "found " + num + " enteries" dec = fic.readline(e) databox[e] = dec dec = fic.readline((e+1)) databox2[e] = dec e = e+1 fic.close() else: return 0 def search(): print "\n" x = 1 items = len(databox) ins = items+1 while ins > x : dac = databox[x] dac2 = databox2[x] x = x + 1 print dac + " " + dac2 print "\n\n" def add(): dat = raw_input("First name.\n") dat2 = raw_input("\nSecond name.\n") items = len(databox) ins = items+1 databox[ins] = dat databox2[ins] = dat2 print "Done.\n\n" def exitprog(): fic = open('databox.txt','w') print "saveing\n" x = 1 items = len(databox) ins = items+1 while ins > x : dac = databox[x] dac2 = databox2[x] x = x + 1 fic.write(dac) fic.write(dac2) fic.close() print "goodbye" pygame.time.delay(900) exit() databox = dict() databox2 = dict() go = raw_input("filename, blank for none.\n") load(go) while True: print "Welcome to databox V2.0." print " 1. Searth the database." print " 2. Add a record." print " 3. Exit." inme = raw_input("Please make a selection.\n") if inme == "1": search() elif inme == "2": add() elif inme == "3": exitprog() else: print "input not recignised." -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at abbottdavid.com Mon May 4 03:40:59 2009 From: david at abbottdavid.com (David) Date: Sun, 03 May 2009 21:40:59 -0400 Subject: [Tutor] Code Dosent work. In-Reply-To: References: Message-ID: <49FE47AB.3050208@abbottdavid.com> Jacob Mansfield wrote: > hi everyone, I'm a bit new here but i was wondering if someone could > check some of my code, it's not doing quite what it's meant to. > thanks Works without pygame; filename, blank for none. Welcome to databox V2.0. 1. Searth the database. 2. Add a record. 3. Exit. Please make a selection. 2 First name. David Second name. William Done. Welcome to databox V2.0. 1. Searth the database. 2. Add a record. 3. Exit. Please make a selection. 1 David William Why were you using pygame? -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From danliang20 at gmail.com Mon May 4 03:59:23 2009 From: danliang20 at gmail.com (Dan Liang) Date: Sun, 3 May 2009 21:59:23 -0400 Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? Message-ID: Hi tutors, I am working on a file and need to replace each occurrence of a certain label (part of speech tag in this case) by a number of sub-labels. The file has the following format: word1 \t Tag1 word2 \t Tag2 word3 \t Tag3 Now the tags are complex and I wanted to split them in a tab-delimited fashion to have this: word1 \t Tag1Part1 \t Tag2Part2 \t Tag3Part3 I searched online for some solution and found the code below which uses a dictionary to store the tags that I want to replace in keys and the sub-tags as values. The problem with this is that it sometimes replaces tags that are not surrounded by spaces, which I do not like to happen. Also, I wanted each new sub-tag to be followed by a tab, so that the new items that I end up having in my file are tab-delimited. For this, I put tabs between the items of each key in the dictionary. I started thinking that this will not be the best solution of the problem and perhaps a script that uses regular expressions would be better. Since I am new to Python, I thought I should ask you for your thoughts for a best solution. The items I want to replace are about 150 and I did not know how to iterate over them with regular expressions. Below is my previous code: #!usr/bin/python import re, sys f = file(sys.argv[1]) readed= f.read() def replace_words(text, word_dic): for k, v in word_dic.iteritems(): text = text.replace(k, v) return text # the dictionary has target_word:replacement_word pairs word_dic = { 'abbrev': 'abbrev null null', 'adj': 'adj null null', 'adv': 'adv null null', 'case_def_acc': 'case_def acc null', 'case_def_gen': 'case_def gen null', 'case_def_nom': 'case_def nom null', 'case_indef_acc': 'case_indef acc null', 'verb_part': 'verb_part null null'} # call the function and get the changed text myString = replace_words(readed, word_dic) fout = open(sys.argv[2], "w") fout.write(myString) fout.close() --dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Mon May 4 07:40:39 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 04 May 2009 15:40:39 +1000 Subject: [Tutor] Code Dosent work. In-Reply-To: <49FE47AB.3050208@abbottdavid.com> References: <49FE47AB.3050208@abbottdavid.com> Message-ID: David wrote: > Jacob Mansfield wrote: >> hi everyone, I'm a bit new here but i was wondering if someone could >> check some of my code, it's not doing quite what it's meant to. >> thanks > > Why were you using pygame? > The only line that uses pygame is pygame.time.delay(900). It is an overkill to import pygame just to put delay. Especially when it is possible to do the same thing with standard lib. import time time.sleep(0.9) Now on to what you should know: 1) pythons' file.readline(x) reads until the end of line returning at most x bytes (it could return less than x bytes if it sees a newline). The x argument is not linenumber. 2) The problem starts from your "save" function. Look at your data file, it does not have line ending. change the while block inside exitprog() function with: while ins > x : dac = databox[x] dac2 = databox2[x] x = x + 1 fic.write(dac + ' ' + dac2 + '\n') also, change this while block in the load(): while dec != '': num = str(e) print "found " + num + " enteries" dec = fic.readline(e) databox[e] = dec dec = fic.readline((e+1)) databox2[e] = dec e = e+1 with: for e, line in enumerate(fic): databox[e+1], databox2[e+1] = line.split() print "found" + str(e) + "entries" Also, these minor things bothers me: 1) For databox and databox2, it is unnecessary to use dict. A list() or set() should serve you better (and is simpler than dict). 2) You have databox and databox2, which is a parallel container for each other. Keeping them in sync is going to be a mess. Rather you should use a single databox which contains a 2-tuple. 3) time.sleep() can replace pygame.time.delay(), however while pygame.time.delay() is in milisecond, time.sleep is in seconds so use 0.9 instead of 900. 4) In python, index number usually starts with 0, the ugly [e+1] code in the for loop I gave above is caused by the use of 1-based indexing in the rest of the code. 5) Use for loop, it's easier. From stefan_ml at behnel.de Mon May 4 08:41:38 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 04 May 2009 08:41:38 +0200 Subject: [Tutor] Code Dosent work. In-Reply-To: References: Message-ID: Jacob Mansfield wrote: > hi everyone, I'm a bit new here but i was wondering if someone could check > some of my code, it's not doing quite what it's meant to. As a general remark: it's a good idea to add a note on what your code is supposed to do, and in what way it behaves unexpectedly, i.e. what is the expected and the observed behaviour. Posting error messages is helpful, too. Stefan From denis.spir at free.fr Mon May 4 11:09:16 2009 From: denis.spir at free.fr (spir) Date: Mon, 4 May 2009 11:09:16 +0200 Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? In-Reply-To: References: Message-ID: <20090504110916.3d460e95@o> Le Sun, 3 May 2009 21:59:23 -0400, Dan Liang s'exprima ainsi: > Hi tutors, > > I am working on a file and need to replace each occurrence of a certain > label (part of speech tag in this case) by a number of sub-labels. The file > has the following format: > > word1 \t Tag1 > word2 \t Tag2 > word3 \t Tag3 > > Now the tags are complex and I wanted to split them in a tab-delimited > fashion to have this: > > word1 \t Tag1Part1 \t Tag2Part2 \t Tag3Part3 > > I searched online for some solution and found the code below which uses a > dictionary to store the tags that I want to replace in keys and the sub-tags > as values. The problem with this is that it sometimes replaces tags that are > not surrounded by spaces, which I do not like to happen*1*. Also, I wanted > each new sub-tag to be followed by a tab, so that the new items that I end > up having in my file are tab-delimited. For this, I put tabs between the > items of each key in the dictionary*2*. I started thinking that this will > not be the best solution of the problem and perhaps a script that uses > regular expressions would be better*3*. Since I am new to Python, I thought > I should ask you for your thoughts for a best solution. The items I want to > replace are about 150 and I did not know how to iterate over them with > regular expressions. *3* I think regular expressions are not the proper tool here. Because you are knew and it's really hairy. But above all because they help parsing, not rewriting. Here the input is very simple, while you have some work for the replacement function. *1* If the source really looks like above, then as I understand it, "tags that are not surrounded by spaces" can only occur in words (eg the word 'noun'). On more reason for not using regex. You just need to read each line, keep the left part unchanged an cope with the tag. An issue is that you replace tags "blindly", without taking into account the easy structure of the source -- which would help you. *2* I would rather have a dict which values are lists of (sub)tags. Then let a replacement function cope with output formatting. word_dic = { 'abbrev': ['abbrev, null, null'], 'adj': ['adj, null, null'], 'adv': ['adv, null, null'], ... } It's not only cleaner, it lets you modify formatting at will.?The dict is only constant *data*. Separating data from process is good practice. I would do something like (untested): tags = {......, 'foo':['foo1','foo2,'foo3'],..........} # tag dict TAB = '\t' def newlyTaggedWord(line): (word,tag) = line.split(TAB) # separate parts of line, keeping data only new_tags = tags['tag'] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): source_file = file(source_name, 'r') source = source_file.read() # not really necessary target_file = open(target_name, "w") # replacement loop for line in source: new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) source_file.close() target_file.close() if __name__ == "__main__" source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) > Below is my previous code: > > > #!usr/bin/python > > import re, sys > f = file(sys.argv[1]) > readed= f.read() > > def replace_words(text, word_dic): > for k, v in word_dic.iteritems(): > text = text.replace(k, v) > return text > > # the dictionary has target_word:replacement_word pairs > > word_dic = { > 'abbrev': 'abbrev null null', > 'adj': 'adj null null', > 'adv': 'adv null null', > 'case_def_acc': 'case_def acc null', > 'case_def_gen': 'case_def gen null', > 'case_def_nom': 'case_def nom null', > 'case_indef_acc': 'case_indef acc null', > 'verb_part': 'verb_part null null'} > > > # call the function and get the changed text > > myString = replace_words(readed, word_dic) > > > fout = open(sys.argv[2], "w") > fout.write(myString) > fout.close() > > --dan ------ la vita e estrany From danliang20 at gmail.com Mon May 4 16:15:35 2009 From: danliang20 at gmail.com (Dan Liang) Date: Mon, 4 May 2009 10:15:35 -0400 Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? Message-ID: Hi Spir and tutors, Thank you Spir for your response. I went ahead and tried your code after adding a couple of dictionary entries, as below: -----------Code Begins--------------- #!usr/bin/python tags = { 'case_def_gen':['case_def','gen','null'], 'nsuff_fem_pl':['nsuff','null', 'null'], 'abbrev': ['abbrev, null, null'], 'adj': ['adj, null, null'], 'adv': ['adv, null, null'],} # tag dict TAB = '\t' def newlyTaggedWord(line): (word,tag) = line.split(TAB) # separate parts of line, keeping data only new_tags = tags['tag'] # read in dict--Index by string tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): source_file = file(source_name, 'r') source = source_file.read() # not really necessary target_file = open(target_name, "w") # replacement loop for line in source: new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) source_file.close() target_file.close() if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) -----------Code Ends--------------- The file I am working on looks like this: word \t case_def_gen word \t nsuff_fem_pl word \t adj word \t abbrev word \t adv I get the following error when I try to run it, and I cannot figure out where the problem lies: -----------Error Begins--------------- Traceback (most recent call last): File "tag.formatter.py", line 36, in ? replaceTagging(source_name, target_name) File "tag.formatter.py", line 28, in replaceTagging new_line = newlyTaggedWord(line) + '\n' File "tag.formatter.py", line 16, in newlyTaggedWord (word,tag) = line.split(TAB) # separate parts of line, keeping data only ValueError: unpack list of wrong size -----------Error Ends--------------- Any ideas? Thank you! --dan From: Dan Liang Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? To: tutor at python.org Message-ID: > > > Content-Type: text/plain; charset="iso-8859-1" > > Hi tutors, > > I am working on a file and need to replace each occurrence of a certain > label (part of speech tag in this case) by a number of sub-labels. The file > has the following format: > > word1 \t Tag1 > word2 \t Tag2 > word3 \t Tag3 > > Now the tags are complex and I wanted to split them in a tab-delimited > fashion to have this: > > word1 \t Tag1Part1 \t Tag2Part2 \t Tag3Part3 > > I searched online for some solution and found the code below which uses a > dictionary to store the tags that I want to replace in keys and the > sub-tags > as values. The problem with this is that it sometimes replaces tags that > are > not surrounded by spaces, which I do not like to happen. Also, I wanted > each > new sub-tag to be followed by a tab, so that the new items that I end up > having in my file are tab-delimited. For this, I put tabs between the items > of each key in the dictionary. I started thinking that this will not be the > best solution of the problem and perhaps a script that uses regular > expressions would be better. Since I am new to Python, I thought I should > ask you for your thoughts for a best solution. The items I want to replace > are about 150 and I did not know how to iterate over them with regular > expressions. Below is my previous code: > > > #!usr/bin/python > > import re, sys > f = file(sys.argv[1]) > readed= f.read() > > def replace_words(text, word_dic): > for k, v in word_dic.iteritems(): > text = text.replace(k, v) > return text > > # the dictionary has target_word:replacement_word pairs > > word_dic = { > 'abbrev': 'abbrev null null', > 'adj': 'adj null null', > 'adv': 'adv null null', > 'case_def_acc': 'case_def acc null', > 'case_def_gen': 'case_def gen null', > 'case_def_nom': 'case_def nom null', > 'case_indef_acc': 'case_indef acc null', > 'verb_part': 'verb_part null null'} > > > # call the function and get the changed text > > myString = replace_words(readed, word_dic) > > > fout = open(sys.argv[2], "w") > fout.write(myString) > fout.close() > > --dan > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20090503/bd82a183/attachment-0001.htm > > > > ------------------------------ -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Mon May 4 16:52:00 2009 From: denis.spir at free.fr (spir) Date: Mon, 4 May 2009 16:52:00 +0200 Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? In-Reply-To: References: Message-ID: <20090504165200.65c674bf@o> Le Mon, 4 May 2009 10:15:35 -0400, Dan Liang s'exprima ainsi: > Hi Spir and tutors, > > Thank you Spir for your response. I went ahead and tried your code after > adding a couple of dictionary entries, as below: > -----------Code Begins--------------- > #!usr/bin/python > > tags = { > > > 'case_def_gen':['case_def','gen','null'], > 'nsuff_fem_pl':['nsuff','null', 'null'], > 'abbrev': ['abbrev, null, null'], > 'adj': ['adj, null, null'], > 'adv': ['adv, null, null'],} # tag dict > TAB = '\t' > > def newlyTaggedWord(line): > (word,tag) = line.split(TAB) # separate parts of line, keeping > data only > new_tags = tags['tag'] # read in dict--Index by string > > tagging = TAB.join(new_tags) # join with TABs > return word + TAB + tagging # formatted result > > def replaceTagging(source_name, target_name): > source_file = file(source_name, 'r') > source = source_file.read() # not really necessary > target_file = open(target_name, "w") > # replacement loop > for line in source: > new_line = newlyTaggedWord(line) + '\n' > target_file.write(new_line) > source_file.close() > target_file.close() > > if __name__ == "__main__": > source_name = sys.argv[1] > target_name = sys.argv[2] > replaceTagging(source_name, target_name) > > -----------Code Ends--------------- > > The file I am working on looks like this: > > > word \t case_def_gen > word \t nsuff_fem_pl > word \t adj > word \t abbrev > word \t adv > > I get the following error when I try to run it, and I cannot figure out > where the problem lies: > > -----------Error Begins--------------- > > Traceback (most recent call last): > File "tag.formatter.py", line 36, in ? > replaceTagging(source_name, target_name) > File "tag.formatter.py", line 28, in replaceTagging > new_line = newlyTaggedWord(line) + '\n' > File "tag.formatter.py", line 16, in newlyTaggedWord > (word,tag) = line.split(TAB) # separate parts of line, keeping data > only > ValueError: unpack list of wrong size > > -----------Error Ends--------------- > > Any ideas? > > Thank you! > > --dan Good that I mentioned "untested" ;-) Can you decipher the error message? What can you reason or guess from it? Where, how, why does an error happen? What kind of test could you perform to better point to a proper diagnosis? I ask all of that because you do not explain us what reflexions and/or trials you did to solve the issue yourself -- instead you just write "Any ideas?". Denis ------ la vita e estrany From alan.gauld at btinternet.com Mon May 4 17:15:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 May 2009 16:15:21 +0100 Subject: [Tutor] Iterating over a long list with regular expressions andchanging each item? References: Message-ID: "Dan Liang" wrote > def replaceTagging(source_name, target_name): > source_file = file(source_name, 'r') > source = source_file.read() # not really necessary this reads the entire file as a string > target_file = open(target_name, "w") > # replacement loop > for line in source: this iterates over the characters in the string. Remove the two source lines above and use for line in open(source_name): HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From burgess.nick at gmail.com Mon May 4 17:57:48 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Mon, 4 May 2009 11:57:48 -0400 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> Message-ID: <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> So far the script works fine, it avoids printing the lines i want and I can add new domain names as needed. It looks like this: #!/usr/bin/python import re outFile = open('outFile.dat', 'w') log = file("log.dat", 'r').read().split('Source') # Set the line delimiter for line in log: if not re.search(r'notneeded.com|notneeded1.com',line): outFile.write(line) I tried the in method but it missed any other strings I put in, like the pipe has no effect. More complex strings will likely be needed so perhaps re might be better..? the next task would be to parse all files in all subdirectories, regardless of the name of the file as the file names are the same but the directory names change I have been playing with os.walk but im not sure if it is the best way. for root, dirs, files in os.walk I guess merging all of the files into one big one before the parse would work but I would need help with that too. the tutelage is much appreciated -nick On Sun, May 3, 2009 at 6:21 PM, Alan Gauld wrote: > > "Alan Gauld" wrote > >>> How do I make this code print lines NOT containing the string 'Domains'? >>> >> >> Don't use regex, use in: >> >> for line in log: >> ? ?if "Domains" in line: >> ? ? ? ?print line > > Should, of course, ?be > > ? ? ?if "Domains" not in line: > ? ? ? ? ?print line > > Alan G. > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From ptmcg at austin.rr.com Mon May 4 18:17:53 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Mon, 4 May 2009 11:17:53 -0500 Subject: [Tutor] Iterating over a long list with regular expressions and changing each item? In-Reply-To: References: Message-ID: <99B447F3C7EF4996AA2ED683F1EE6DB6@AWA2> Original: 'case_def_gen':['case_def','gen','null'], 'nsuff_fem_pl':['nsuff','null', 'null'], 'abbrev': ['abbrev, null, null'], 'adj': ['adj, null, null'], 'adv': ['adv, null, null'],} Note the values for 'abbrev', 'adj' and 'adv' are not lists, but strings containing comma-separated lists. Should be: 'case_def_gen':['case_def','gen','null'], 'nsuff_fem_pl':['nsuff','null', 'null'], 'abbrev': ['abbrev', 'null', 'null'], 'adj': ['adj', 'null', 'null'], 'adv': ['adv', 'null', 'null'],} For much of my own code, I find lists of string literals to be tedious to enter, and easy to drop a ' character. This style is a little easier on the eyes, and harder to screw up. 'case_def_gen':['case_def gen null'.split()], 'nsuff_fem_pl':['nsuff null null'.split()], 'abbrev': ['abbrev null null'.split()], 'adj': ['adj null null'.split()], 'adv': ['adv null null'.split()],} Since all that your code does at runtime with the value strings is "\t".join() them, then you might as well initialize the dict with these computed values, for at least some small gain in runtime performance: T = lambda s : "\t".join(s.split()) 'case_def_gen' : T('case_def gen null'), 'nsuff_fem_pl' : T('nsuff null null'), 'abbrev' : T('abbrev null null'), 'adj' : T('adj null null'), 'adv' : T('adv null null'),} del T (Yes, I know PEP8 says *not* to add spaces to line up assignments or other related values, but I think there are isolated cases where it does help to see what's going on. You could even write this as: T = lambda s : "\t".join(s.split()) 'case_def_gen' : T('case_def gen null'), 'nsuff_fem_pl' : T('nsuff null null'), 'abbrev' : T('abbrev null null'), 'adj' : T('adj null null'), 'adv' : T('adv null null'),} del T and the extra spaces help you to see the individual subtags more easily, with no change in the resulting values since split() splits on multiple whitespace the same as a single space.) Of course you could simply code as: 'case_def_gen' : T('case_def\tgen\t null'), 'nsuff_fem_pl' : T('nsuff\tnull\tnull'), 'abbrev' : T('abbrev\tnull\tnull'), 'adj' : T('adj\tnull\tnull'), 'adv' : T('adv\tnull\tnull'),} But I think readability definitely suffers here, I would probably go with the penultimate version. -- Paul From alex.feddor at gmail.com Mon May 4 14:45:06 2009 From: alex.feddor at gmail.com (Alex Feddor) Date: Mon, 4 May 2009 14:45:06 +0200 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. Message-ID: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Hi I am looking for method enables advanced text string search. Method string.find() or re module seems no supporting what I am looking for. The idea is as follows: Text ="FDA meeting was successful. New drug is approved for whole sale distribution!" I would like to scan the text using AND and OR operators and gets -1 or other value if the searching elements haven't found in the text. Example 01: search criteria: "FDA" AND ( "approve*" OR "supported") The catch is that in Text variable FDA and approve words are not one after another (other words are in between). Example 02: search criteria: "Ben" The catch is that code sould find only exact Ben words not also words which that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben is the right word we are looking for. I would really appreciated your advice - code sample / links how above can be achieved! if possible I would appreciated solution achieved with free of charge module. Cheers, Alex PS: A few moths ago I have discovered Python. I am amazed what all can be done with it. Really cool programming language.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From pablofaria at gmail.com Mon May 4 16:09:25 2009 From: pablofaria at gmail.com (Pablo P. F. de Faria) Date: Mon, 4 May 2009 11:09:25 -0300 Subject: [Tutor] Encode problem In-Reply-To: <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> Message-ID: <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> Thanks, Kent, but that doesn't solve my problem. In fact, I need ConfigParser to work with non-ascii characters, since my App may run in "latin-1" environments (folders e files names). I must find out why the str() function in the module ConfigParser doesn't use the encoding defined for the application (# -*- coding: utf-8 -*-). The rest of the application works properly with utf-8, except for ConfigParser. What I found out is that ConfigParser seems to make use of the configuration in Site.py (which is set to 'ascii'), instead of the configuration defined for the App (if I change . But this is very problematic to have to change Site.py in every computer... So I wonder if there is a way to replace the settings in Site.py only for my App. 2009/5/1 Kent Johnson : > On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria > wrote: >> Hi, Kent. >> >> The stack trace is: >> >> Traceback (most recent call last): >> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in OnClose >> ? ?self.SavePreferences() >> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068, >> in SavePreferences >> ? ?self.cfg.set(u'File Settings',u'Recent files', >> unicode(",".join(self.recent_files))) >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position >> 12: ordinal not in range(128) >> >> The "unicode" function, actually doesn't do any difference... The >> content of the string being saved is "/home/pablo/?rea de >> Trabalho/teste.xml". > > OK, this error is in your code, not the ConfigParser. The problem is with > ",".join(self.recent_files) > > Are the entries in self.recent_files unicode strings? If so, then I > think the join is trying to convert to a string using the default > codec. Try > > self.cfg.set('File Settings','Recent files', > ','.join(name.encode('utf-8') for name in self.recent_files)) > > Looking at the ConfigParser.write() code, it wants the values to be > strings or convertible to strings by calling str(), so non-ascii > unicode values will be a problem there. I would use plain strings for > all the interaction with ConfigParser and convert to Unicode yourself. > > Kent > > PS Please Reply All to reply to the list. > -- --------------------------------- "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." (Oscar Wilde) --------------------------------- Pablo Faria Mestrando em Aquisi??o de Linguagem - IEL/Unicamp Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica (19) 3521-1570 http://www.tycho.iel.unicamp.br/~pablofaria/ pablofaria at gmail.com From pablofaria at gmail.com Mon May 4 16:11:58 2009 From: pablofaria at gmail.com (Pablo P. F. de Faria) Date: Mon, 4 May 2009 11:11:58 -0300 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> Message-ID: <3ea81d4c0905040711p62376925n26fb93a8955fefe4@mail.gmail.com> Here is the traceback, after the last change you sugested: Traceback (most recent call last): File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in OnClose self.SavePreferences() File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1069, in SavePreferences self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) File "/usr/lib/python2.5/ConfigParser.py", line 373, in write (key, str(value).replace('\n', '\n\t'))) File "/usr/lib/python2.5/codecs.py", line 638, in write return self.writer.write(data) File "/usr/lib/python2.5/codecs.py", line 303, in write data, consumed = self.encode(object, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 27: ordinal not in range(128) So, in "str(value)" the content is a folder name with an accented character (?). 2009/5/4 Pablo P. F. de Faria : > Thanks, Kent, but that doesn't solve my problem. In fact, I need > ConfigParser to work with non-ascii characters, since my App may run > in "latin-1" environments (folders e files names). I must find out why > the str() function in the module ConfigParser doesn't use the encoding > defined for the application (# -*- coding: utf-8 -*-). The rest of the > application works properly with utf-8, except for ConfigParser. What I > found out is that ConfigParser seems to make use of the configuration > in Site.py (which is set to 'ascii'), instead of the configuration > defined for the App (if I change . But this is very problematic to > have to change Site.py in every computer... So I wonder if there is a > way to replace the settings in Site.py only for my App. > > 2009/5/1 Kent Johnson : >> On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria >> wrote: >>> Hi, Kent. >>> >>> The stack trace is: >>> >>> Traceback (most recent call last): >>> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in OnClose >>> ? ?self.SavePreferences() >>> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068, >>> in SavePreferences >>> ? ?self.cfg.set(u'File Settings',u'Recent files', >>> unicode(",".join(self.recent_files))) >>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position >>> 12: ordinal not in range(128) >>> >>> The "unicode" function, actually doesn't do any difference... The >>> content of the string being saved is "/home/pablo/?rea de >>> Trabalho/teste.xml". >> >> OK, this error is in your code, not the ConfigParser. The problem is with >> ",".join(self.recent_files) >> >> Are the entries in self.recent_files unicode strings? If so, then I >> think the join is trying to convert to a string using the default >> codec. Try >> >> self.cfg.set('File Settings','Recent files', >> ','.join(name.encode('utf-8') for name in self.recent_files)) >> >> Looking at the ConfigParser.write() code, it wants the values to be >> strings or convertible to strings by calling str(), so non-ascii >> unicode values will be a problem there. I would use plain strings for >> all the interaction with ConfigParser and convert to Unicode yourself. >> >> Kent >> >> PS Please Reply All to reply to the list. >> > > > > -- > --------------------------------- > "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." > (Oscar Wilde) > --------------------------------- > Pablo Faria > Mestrando em Aquisi??o de Linguagem - IEL/Unicamp > Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica > (19) 3521-1570 > http://www.tycho.iel.unicamp.br/~pablofaria/ > pablofaria at gmail.com > -- --------------------------------- "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." (Oscar Wilde) --------------------------------- Pablo Faria Mestrando em Aquisi??o de Linguagem - IEL/Unicamp Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica (19) 3521-1570 http://www.tycho.iel.unicamp.br/~pablofaria/ pablofaria at gmail.com From vinces1979 at gmail.com Mon May 4 18:38:31 2009 From: vinces1979 at gmail.com (vince spicer) Date: Mon, 4 May 2009 10:38:31 -0600 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: <1e53c510905040938q25d787f3w17f7a18f65bd0410@mail.gmail.com> Advanced Strings searches are Regex via re module. EX: import re m = re.compile("(FDA.*?(approved|supported)|Ben[^\s])*") if m.search(Text): print m.search(Text).group() Vince On Mon, May 4, 2009 at 6:45 AM, Alex Feddor wrote: > Hi > > I am looking for method enables advanced text string search. Method > string.find() or re module seems no supporting what I am looking for. The > idea is as follows: > > Text ="FDA meeting was successful. New drug is approved for whole sale > distribution!" > > I would like to scan the text using AND and OR operators and gets -1 or > other value if the searching elements haven't found in the text. > Example 01: > search criteria: "FDA" AND ( "approve*" OR "supported") > The catch is that in Text variable FDA and approve words are not one after > another (other words are in between). > Example 02: > search criteria: "Ben" > The catch is that code sould find only exact Ben words not also words which > that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben is > the right word we are looking for. > > I would really appreciated your advice - code sample / links how above can > be achieved! if possible I would appreciated solution achieved with free of > charge module. > > Cheers, Alex > PS: > A few moths ago I have discovered Python. I am amazed what all can be done > with it. Really cool programming language.. > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 4 19:45:17 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 May 2009 18:45:17 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> Message-ID: "Nick Burgess" wrote > for line in log: > if not re.search(r'notneeded.com|notneeded1.com',line): > outFile.write(line) > > I tried the in method but it missed any other strings I put in, like > the pipe has no effect. More complex strings will likely be needed so > perhaps re might be better..? Yes, in only works for simple strings. If you need combinations then the regex is better > I have been playing with os.walk but im not sure if it is the best way. It is almost certainly the best way. > I guess merging all of the files into one big one before the parse > would work but I would need help with that too. You shouldn't need to do that. Your function can take a file and process it so just use os.walk to feed it files one by one as you find them If the file names vary you might find glob.glob useful too. I show examples of using os,.walk and glob in the OS topic in my tutorial. Look under the heading 'Manipulating Files' HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mwalsh at mwalsh.org Mon May 4 19:46:16 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Mon, 04 May 2009 12:46:16 -0500 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> Message-ID: <49FF29E8.4070904@mwalsh.org> Nick Burgess wrote: > So far the script works fine, it avoids printing the lines i want and > I can add new domain names as needed. It looks like this: > > #!/usr/bin/python > import re > > outFile = open('outFile.dat', 'w') > log = file("log.dat", 'r').read().split('Source') # Set the line delimiter > for line in log: > if not re.search(r'notneeded.com|notneeded1.com',line): > outFile.write(line) There is a subtle problem here -- the '.' means match any single character. I suppose it's unlikely to bite you, but it could -- for example, a line containing a domain named notneeded12com.net would match. You should probably escape the dot, and while you're at it compile the regular expression. # untested pattern = re.compile(r'notneeded\.com|notneeded1\.com') for line in log: if not pattern.search(line): outFile.write(line) HTH, Marty From alan.gauld at btinternet.com Mon May 4 19:50:30 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 May 2009 18:50:30 +0100 Subject: [Tutor] Iterating over a long list with regular expressions andchanging each item? References: <99B447F3C7EF4996AA2ED683F1EE6DB6@AWA2> Message-ID: "Paul McGuire" wrote > For much of my own code, I find lists of string literals to be tedious to > enter, and easy to drop a ' character. This style is a little easier on > the > eyes, and harder to screw up. > > 'case_def_gen':['case_def gen null'.split()], > 'nsuff_fem_pl':['nsuff null null'.split()], Shouldn't that be: > 'case_def_gen':'case_def gen null'.split(), > 'nsuff_fem_pl':'nsuff null null'.split(), Otherwise you get a list inside a list. > 'abbrev' : T('abbrev null null'), > 'adj' : T('adj null null'), > 'adv' : T('adv null null'),} > (Yes, I know PEP8 says *not* to add spaces to line up assignments or > other > related values, but I think there are isolated cases where it does help > to > see what's going on. You could even write this as: Absolutely! There are a few of the Python style PEPs that I disagree with, this looks like another one. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon May 4 20:03:46 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 May 2009 19:03:46 +0100 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: "Alex Feddor" wrote > I am looking for method enables advanced text string search. Method > string.find() or re module seems no supporting what I am looking for. > The > idea is as follows: The re module almost certainly can do what you want but regex are notoriously hard to master and often obscure. > Text ="FDA meeting was successful. New drug is approved for whole sale > distribution!" > > Example 01: > search criteria: "FDA" AND ( "approve*" OR "supported") The regex will search for FDA followed by either approve or supported. There is no AND operator in regex since AND just implies a sequence within the string. There is an OR operator however which is '|' > The catch is that in Text variable FDA and approve words are not one > after > another (other words are in between). And regex allows for you to specify a sequence of anything after FDA > Example 02: > search criteria: "Ben" > The catch is that code sould find only exact Ben words not also words > which > that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben > is > the right word we are looking for. And again regex provides ways of ensuring an exact match. > I would really appreciated your advice - code sample / links how above > can > be achieved! if possible I would appreciated solution achieved with free > of > charge module. You need to go through one of the many regex tutorials to understand what can be done with these extremely powerful search tools (and what can't!) There is a very basic introduction in my tutorial which unfortunately doesn't cover all that you need here but might be a good starting point. The python HOWTO is another good start and goes a bit deeper with a different approach: http://docs.python.org/howto/regex.html HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Mon May 4 20:23:23 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 May 2009 11:23:23 -0700 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: On 5/4/2009 11:03 AM Alan Gauld said... > "Alex Feddor" wrote > >> I am looking for method enables advanced text string search. Method >> string.find() or re module seems no supporting what I am looking for. >> The >> idea is as follows: > > The re module almost certainly can do what you want but regex > are notoriously hard to master and often obscure. Seconded. I almost always find it faster and easier to simply write the python routine I need rather than suffer the pain that results from getting the regex to actually perform what's needed ... Emile From kent37 at tds.net Mon May 4 21:08:55 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 15:08:55 -0400 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> Message-ID: <1c2a2c590905041208qb4e98a3o783ab031ba9d3e25@mail.gmail.com> On Mon, May 4, 2009 at 10:09 AM, Pablo P. F. de Faria wrote: > Thanks, Kent, but that doesn't solve my problem. In fact, I need > ConfigParser to work with non-ascii characters, since my App may run > in "latin-1" environments (folders e files names). Yes, I understand that. Python has two different kinds of strings - byte strings, which are instances of class str, and unicode strings, which are instances of class unicode. String objects are byte strings - sequences of bytes. They are not limited to ascii characters, they hold encoded strings in any supported encoding. In particular, UTF-8 data is stored in string objects. Unicode objects hold "unencoded" unicode data. (I know, Unicode is an encoding, but it is useful to think of it this way in this context.) str.decode() converts a string to a unicode object. unicode.encode() converts a unicode object to a (byte) string. Both of these functions take the encoding as a parameter. When Python is given a string, but it needs a unicode object, or vice-versa, it will encode or decode as needed. The encode or decode will use the system default encoding, which as you have discovered is ascii. If the data being encoded or decoded contains non-ascii characters, you get an error that you are familiar with. These errors indicate that you are not correctly handling encoded data. See the references at the end of this essay for more background information: http://personalpages.tds.net/~kent37/stories/00018.html > I must find out why > the str() function in the module ConfigParser doesn't use the encoding > defined for the application (# -*- coding: utf-8 -*-). Because the encoding declaration doesn't define an encoding for the application. It defines the encoding of the text of the source file containing the declaration, that's all. > The rest of the > application works properly with utf-8, except for ConfigParser. I guess you have been lucky. > What I > found out is that ConfigParser seems to make use of the configuration > in Site.py (which is set to 'ascii'), instead of the configuration > defined for the App (if I change . But this is very problematic to > have to change Site.py in every computer... So I wonder if there is a > way to replace the settings in Site.py only for my App. It is the wrong solution. What you should do is - understand why you have a problem. Hint: it's not a ConfigParser bug - give only utf-8-encoded strings to ConfigParser - don't use the codecs module, because the data you are writing will already be encoded. Kent From kent37 at tds.net Mon May 4 21:10:40 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 15:10:40 -0400 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905041032p76cc9a2dnabf835a638752491@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> <3ea81d4c0905040711p62376925n26fb93a8955fefe4@mail.gmail.com> <3ea81d4c0905041032p76cc9a2dnabf835a638752491@mail.gmail.com> Message-ID: <1c2a2c590905041210o1505274cx692649ed98e69b3b@mail.gmail.com> On Mon, May 4, 2009 at 1:32 PM, Pablo P. F. de Faria wrote: > Hi, all. > > I've found something that worked for me, but I'm not sure of its > secureness. The solution is: > > reload(sys) > sys.setdefaultencoding('utf-8') > > That's exactly what I wanted to do, but is this good practice? No. You should understand and fix the underlying problem. Kent From denis.spir at free.fr Mon May 4 21:18:24 2009 From: denis.spir at free.fr (spir) Date: Mon, 4 May 2009 21:18:24 +0200 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <1e53c510905040938q25d787f3w17f7a18f65bd0410@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> <1e53c510905040938q25d787f3w17f7a18f65bd0410@mail.gmail.com> Message-ID: <20090504211824.457d3662@o> Le Mon, 4 May 2009 10:38:31 -0600, vince spicer s'exprima ainsi: > Advanced Strings searches are Regex via re module. > > EX: > > import re > > m = re.compile("(FDA.*?(approved|supported)|Ben[^\s])*") > > if m.search(Text): > print m.search(Text).group() > > > Vince This is not at all what the origial poster looks for, I guess (or maybe it didn't understand?). Regex can only match one individual sample of request expressed in a logical form with AND and OR clauses. What he wants is a module able to decode and perform logical searches. It can certainly be built on top of regex, with a layer that: * decodes logical requests * performs "sub-matches" for items in the request * then builds unions (OR) or intersections (AND) of results I do not know of anything like that for python. But it would be a nice project ;-) Denis ------ la vita e estrany From denis.spir at free.fr Mon May 4 21:26:30 2009 From: denis.spir at free.fr (spir) Date: Mon, 4 May 2009 21:26:30 +0200 Subject: [Tutor] Encode problem In-Reply-To: <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> Message-ID: <20090504212630.72db0a1f@o> Le Mon, 4 May 2009 11:09:25 -0300, "Pablo P. F. de Faria" s'exprima ainsi: > Thanks, Kent, but that doesn't solve my problem. In fact, I need > ConfigParser to work with non-ascii characters, since my App may run > in "latin-1" environments (folders e files names). I must find out why > the str() function in the module ConfigParser doesn't use the encoding > defined for the application (# -*- coding: utf-8 -*-). The rest of the > application works properly with utf-8, except for ConfigParser. What I > found out is that ConfigParser seems to make use of the configuration > in Site.py (which is set to 'ascii'), instead of the configuration > defined for the App (if I change . But this is very problematic to > have to change Site.py in every computer... So I wonder if there is a > way to replace the settings in Site.py only for my App. The parameter in question is the default encoding. We used to read (sys.getdefaultencoding()) and define it (e.g. sys.getdefaultencoding('utf8')), but I remember something has changed in later versions of python. Someone? Denis ------ la vita e estrany From burgess.nick at gmail.com Mon May 4 21:51:36 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Mon, 4 May 2009 15:51:36 -0400 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: <49FF29E8.4070904@mwalsh.org> References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> <49FF29E8.4070904@mwalsh.org> Message-ID: <3a13c8e50905041251re566f17oe6ee4927e7c366f3@mail.gmail.com> Compiling the regular expression works great, I cant find the tutorial Mr. Gauld is referring to!! I searched python.org and alan-g.me.uk. Does anyone have a link? On Mon, May 4, 2009 at 1:46 PM, Martin Walsh wrote: > Nick Burgess wrote: >> So far the script works fine, it avoids printing the lines i want and >> I can add new domain names as needed. It looks like this: >> >> #!/usr/bin/python >> import re >> >> outFile = open('outFile.dat', 'w') >> log = file("log.dat", 'r').read().split('Source') # Set the line delimiter >> for line in log: >> ? ? if not re.search(r'notneeded.com|notneeded1.com',line): >> ? ? ? ? outFile.write(line) > > There is a subtle problem here -- the '.' means match any single > character. I suppose it's unlikely to bite you, but it could -- for > example, a line containing a domain named notneeded12com.net would > match. You should probably escape the dot, and while you're at it > compile the regular expression. > > # untested > pattern = re.compile(r'notneeded\.com|notneeded1\.com') > for line in log: > ? ?if not pattern.search(line): > ? ? ? ?outFile.write(line) > > HTH, > Marty > > From sander.sweers at gmail.com Mon May 4 21:54:34 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 4 May 2009 21:54:34 +0200 Subject: [Tutor] Encode problem In-Reply-To: <1c2a2c590905041208qb4e98a3o783ab031ba9d3e25@mail.gmail.com> References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> <1c2a2c590905041208qb4e98a3o783ab031ba9d3e25@mail.gmail.com> Message-ID: 2009/5/4 Kent Johnson : > str.decode() converts a string to a unicode object. unicode.encode() > converts a unicode object to a (byte) string. Both of these functions > take the encoding as a parameter. When Python is given a string, but > it needs a unicode object, or vice-versa, it will encode or decode as > needed. The encode or decode will use the system default encoding, > which as you have discovered is ascii. If the data being encoded or > decoded contains non-ascii characters, you get an error that you are > familiar with. These errors indicate that you are not correctly > handling encoded data. Very interesting read Kent! So if I get it correctly you are saying the join() is joining strings of str and unicode type? Then would it help to add a couple of "print type(the_string), the_string" before the .join() help finding which string is not unicode or is unicode where it shouldn't? Thanks Sander From kent37 at tds.net Mon May 4 22:10:18 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 16:10:18 -0400 Subject: [Tutor] Encode problem In-Reply-To: References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <1c2a2c590905011255ybe46b9akf31f2995712bb6f@mail.gmail.com> <3ea81d4c0905011354t1a4a2624gfda10bc7740ad958@mail.gmail.com> <1c2a2c590905011436x34928c4q6113ac83f3b1545e@mail.gmail.com> <3ea81d4c0905040709m78a45d11j2037943380817297@mail.gmail.com> <1c2a2c590905041208qb4e98a3o783ab031ba9d3e25@mail.gmail.com> Message-ID: <1c2a2c590905041310j9f4244em24ef3666b4718da9@mail.gmail.com> On Mon, May 4, 2009 at 3:54 PM, Sander Sweers wrote: > 2009/5/4 Kent Johnson : >> str.decode() converts a string to a unicode object. unicode.encode() >> converts a unicode object to a (byte) string. Both of these functions >> take the encoding as a parameter. When Python is given a string, but >> it needs a unicode object, or vice-versa, it will encode or decode as >> needed. The encode or decode will use the system default encoding, >> which as you have discovered is ascii. If the data being encoded or >> decoded contains non-ascii characters, you get an error that you are >> familiar with. These errors indicate that you are not correctly >> handling encoded data. > > Very interesting read Kent! > > So if I get it correctly you are saying the join() is joining strings > of str and unicode type? Then would it help to add a couple of "print > type(the_string), the_string" before the .join() help finding which > string is not unicode or is unicode where it shouldn't? I think that was the original problem though I haven't seen enough code to be sure. The current problem is (I tihnk) that he is writing encoded data to a codec writer that expects unicode input, so it is trying to convert str to unicode (so it can convert back to str!) and failing. Kent From kent37 at tds.net Mon May 4 22:22:47 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 16:22:47 -0400 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: <1c2a2c590905041322n2ad0ad98o98e3ef69a05dbe21@mail.gmail.com> On Mon, May 4, 2009 at 8:45 AM, Alex Feddor wrote: > Hi > > I am looking for method enables advanced?text string search. Method > string.find() or re module seems?no??supporting what I am looking for. The > idea is as follows: > > Text ="FDA meeting was successful. New drug?is approved for whole sale > distribution!" > > I would like to scan the text using AND and OR operators and gets -1 or > other value if the searching elements haven't found in the text. There are some Python search engines that will do this. They might be overkill unless you have a lot of text to search: http://whoosh.ca/ http://lucene.apache.org/pylucene/ http://pypi.python.org/pypi/pyswish/20080920 Kent From kent37 at tds.net Mon May 4 22:32:10 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 16:32:10 -0400 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <1e53c510905040938q25d787f3w17f7a18f65bd0410@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> <1e53c510905040938q25d787f3w17f7a18f65bd0410@mail.gmail.com> Message-ID: <1c2a2c590905041332x18ef1432td44864ce151e9b6a@mail.gmail.com> On Mon, May 4, 2009 at 12:38 PM, vince spicer wrote: > Advanced Strings searches are Regex via re module. > > EX: > > import re > > m = re.compile("(FDA.*?(approved| > supported)|Ben[^\s])*") > > if m.search(Text): > ??? print m.search(Text).group() This won't match "approved FDA" which may be desired. It also quickly gets complicated as the search expressions get more complex. Regex would also have a hard time with something like "FDA" AND NOT "approved" Kent From ldl08 at gmx.net Mon May 4 23:40:21 2009 From: ldl08 at gmx.net (David) Date: Tue, 05 May 2009 05:40:21 +0800 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') Message-ID: <49FF60C5.4040707@gmx.net> Dear list, in different books I come across different syntax for dealing with files. It seems that open(filename, 'r') and file(filename, 'r') are used interchangeably, and I wonder what this is all about. Is there a reason why Python allows such ambiguity here? Cheers for a quick shot of enlightenment ;-) David From bill at celestial.net Mon May 4 23:49:27 2009 From: bill at celestial.net (Bill Campbell) Date: Mon, 4 May 2009 14:49:27 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <49FF60C5.4040707@gmx.net> References: <49FF60C5.4040707@gmx.net> Message-ID: <20090504214927.GA17937@ayn.mi.celestial.com> On Tue, May 05, 2009, David wrote: >Dear list, > >in different books I come across different syntax for dealing with >files. It seems that open(filename, 'r') and file(filename, 'r') are >used interchangeably, and I wonder what this is all about. Is there a >reason why Python allows such ambiguity here? > >Cheers for a quick shot of enlightenment ;-) ``pydoc file'' is your friend. It says open is an alias for file. Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792 A petty thief is put in jail. A great brigand becomes ruler of a State. -- Chuang Tzu From bgailer at gmail.com Mon May 4 23:50:55 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 04 May 2009 17:50:55 -0400 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <49FF60C5.4040707@gmx.net> References: <49FF60C5.4040707@gmx.net> Message-ID: <49FF633F.9070007@gmail.com> PDavid wrote: > Dear list, > > in different books I come across different syntax for dealing with > files. It seems that open(filename, 'r') and file(filename, 'r') are > used interchangeably, and I wonder what this is all about. Is there a > reason why Python allows such ambiguity here? > regarding file, the docs say: Constructor function for the file type, described further in section 3.9, ``File Objects''. The constructor's arguments are the same as those of the open() built-in function described below. When opening a file, it's preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing "isinstance(f, file)"). Unfortunately no explanation as to WHY open is preferred. I have long wondered that myself. Perhaps someone with more enlightenment can tell us! -- Bob Gailer Chapel Hill NC 919-636-4239 From timmichelsen at gmx-topmail.de Tue May 5 00:37:53 2009 From: timmichelsen at gmx-topmail.de (Tim Michelsen) Date: Tue, 05 May 2009 00:37:53 +0200 Subject: [Tutor] how to reference a function itself when accessing its private functions? Message-ID: Dear Tutors and fellow pythonistas, I would like to get access to the private methods of my function. For instance: Who can I reference the docstring of a function within the function itself? Please have a look at the code below and assist me. Thanks and regards, Timmie #### CODE ### s = 'hello' def show(str): """prints str""" print str return str def show2(str): """prints str""" print str d = self.__doc__ print d >>> show2(s) hello --------------------------------------------------------------------------- NameError Traceback (most recent call last) in () in show2(str) NameError: global name 'self' is not defined From emile at fenx.com Tue May 5 00:59:49 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 May 2009 15:59:49 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <49FF633F.9070007@gmail.com> References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> Message-ID: On 5/4/2009 2:50 PM bob gailer said... > PDavid wrote: >> Dear list, >> >> in different books I come across different syntax for dealing with >> files. It seems that open(filename, 'r') and file(filename, 'r') are >> used interchangeably, and I wonder what this is all about. Is there a >> reason why Python allows such ambiguity here? Backwards compatibility. The file type was introduced in python 2.2, before which there was open. Emile From emile at fenx.com Tue May 5 01:08:38 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 May 2009 16:08:38 -0700 Subject: [Tutor] how to reference a function itself when accessing its private functions? In-Reply-To: References: Message-ID: On 5/4/2009 3:37 PM Tim Michelsen said... > Dear Tutors and fellow pythonistas, > I would like to get access to the private methods of my function. > > For instance: > Who can I reference the docstring of a function within the function itself? > > def show2(str): > """prints str""" > print str > d = self.__doc__ > print d >>> def show2(str): ... """prints str""" ... print str ... print globals()['show2'].__doc__ ... >>> show2('hello') hello prints str >>> This is the easy way -- ie, you know where to look and what name to use. You can discover the name using the inspect module, but it can get ugly. If you're interested start with... from inspect import getframeinfo, currentframe HTH, Emile From xchimeras at gmail.com Tue May 5 01:17:43 2009 From: xchimeras at gmail.com (Tom Green) Date: Mon, 4 May 2009 19:17:43 -0400 Subject: [Tutor] Conversion question Message-ID: First, thanks in advance for any insight on how to assist in making me a better Python programmer. Here is my question. I work with a lot of sockets and most of them require hex data. I am usually given a string of data to send to the socket. Example: "414243440d0a" Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... Sometimes my string values are 99+ bytes in length. I did write a parsing program that would basically loop thru the string and insert the \x, but I was wondering if there was another or better way. Again, thanks in advance for any feedback. Mike. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue May 5 01:32:55 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 00:32:55 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com><49FF29E8.4070904@mwalsh.org> <3a13c8e50905041251re566f17oe6ee4927e7c366f3@mail.gmail.com> Message-ID: > Mr. Gauld is referring to!! I searched python.org and alan-g.me.uk. > Does anyone have a link? I posted a link to the Python howto and my tutorial is at alan-g.me.uk You will find it on the contents frame under Regular Expressions... Its in the Advanced Topics section. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Tue May 5 01:40:51 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 May 2009 16:40:51 -0700 Subject: [Tutor] Conversion question In-Reply-To: References: Message-ID: On 5/4/2009 4:17 PM Tom Green said... > First, thanks in advance for any insight on how to assist in making me a > better Python programmer. > > Here is my question. I work with a lot of sockets and most of them > require hex data. I am usually given a string of data to send to the > socket. Example: > > "414243440d0a" > > Is there a way in Python to say this is a string of HEX characters like > Perl's pack? Right now I have to take the string and add a \x to every > two values i.e. \x41\x42... import binascii binascii.a2b_hex('41424344') Emile From alan.gauld at btinternet.com Tue May 5 01:41:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 00:41:39 +0100 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> Message-ID: "Emile van Sebille" wrote in message news:gtnrtf$pi8$1 at ger.gmane.org... > On 5/4/2009 2:50 PM bob gailer said... >> PDavid wrote: >>> Dear list, >>> >>> in different books I come across different syntax for dealing with >>> files. It seems that open(filename, 'r') and file(filename, 'r') are >>> used interchangeably, and I wonder what this is all about. Is there a >>> reason why Python allows such ambiguity here? > > Backwards compatibility. The file type was introduced in python 2.2, > before which there was open. And file has been removed again in Python v3.... In fact open is now an alias for io.open and no longer simply returns a file object - in fact the file type itself is gone too! A pity, there are cases where I found file() more intuitive than open and vice versa so liked having both available. The fact that it looked like creating an instance of a class seemed to fit well in OO code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From xchimeras at gmail.com Tue May 5 01:42:30 2009 From: xchimeras at gmail.com (Tom Green) Date: Mon, 4 May 2009 19:42:30 -0400 Subject: [Tutor] Conversion question In-Reply-To: References: Message-ID: Thank you, I didn't realize it was that easy. I tried binascii before and I thought it didn't work properly. I appreciate it. Mike. On Mon, May 4, 2009 at 7:40 PM, Emile van Sebille wrote: > On 5/4/2009 4:17 PM Tom Green said... > >> First, thanks in advance for any insight on how to assist in making me a >> better Python programmer. >> >> Here is my question. I work with a lot of sockets and most of them >> require hex data. I am usually given a string of data to send to the >> socket. Example: >> >> "414243440d0a" >> >> Is there a way in Python to say this is a string of HEX characters like >> Perl's pack? Right now I have to take the string and add a \x to every two >> values i.e. \x41\x42... >> > > > import binascii > binascii.a2b_hex('41424344') > > Emile > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue May 5 01:52:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 00:52:11 +0100 Subject: [Tutor] Conversion question References: Message-ID: "Tom Green" wrote > Here is my question. I work with a lot of sockets and most of them > require > hex data. I am usually given a string of data to send to the socket. > Example: > > "414243440d0a" > > Is there a way in Python to say this is a string of HEX characters like > Perl's pack? Right now I have to take the string and add a \x to every > two > values i.e. \x41\x42... Assuming you actually want to send the hex values rather than a hex string representation then the way I'd send that would be to convert that to a number using int() then transmit it using struct() > Sometimes my string values are 99+ bytes in length. I did write a > parsing > program that would basically loop thru the string and insert the \x, but > I > was wondering if there was another or better way. OK, Maybe you do want to send the hex representation rather than the actual data (I can't think why unless you have a very strange parser at the other end). In that case I think you do need to insert the \x characters. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Tue May 5 01:55:25 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 4 May 2009 19:55:25 -0400 Subject: [Tutor] how to reference a function itself when accessing its private functions? In-Reply-To: References: Message-ID: <1c2a2c590905041655k289aac31r1b710d6159e3a3b6@mail.gmail.com> On Mon, May 4, 2009 at 6:37 PM, Tim Michelsen wrote: > Who can I reference the docstring of a function within the function itself? You can refer to the function by name inside the function. By the time the body is actually executed, the name is defined: In [1]: def show2(s): ...: """prints s""" ...: print s ...: print show2.__doc__ In [2]: show2("test") test prints s > Please have a look at the code below and assist me. > > def show(str): > ? ?"""prints str""" > ? ?print str It's a good idea not to use the names of builtins, such as 'str', as variable names in your program. Kent From danliang20 at gmail.com Tue May 5 05:43:23 2009 From: danliang20 at gmail.com (Dan Liang) Date: Mon, 4 May 2009 23:43:23 -0400 Subject: [Tutor] Tutor Digest, Vol 63, Issue 8 In-Reply-To: References: Message-ID: Hello Spir, Alan, and Paul, Thank you for your help. I have been working on the file, but I still have a problem doing what I wanted. As a reminder, I have #!usr/bin/python tags = { 'noun-prop': 'noun_prop null null'.split(), 'case_def_gen': 'case_def gen null'.split(), 'dem_pron_f': 'dem_pron f null'.split(), 'case_def_acc': 'case_def acc null'.split(), } TAB = '\t' def newlyTaggedWord(line): (word,tag) = line.split(TAB) # separate parts of line, keeping data only new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): target_file = open(target_name, "w") # replacement loop for line in open(source_name, "r"): new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) target_file.close() if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) On Mon, May 4, 2009 at 12:38 PM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Iterating over a long list with regular expressions and > changing each item? (Paul McGuire) > 2. Advanced String Search using operators AND, OR etc.. (Alex Feddor) > 3. Re: Encode problem (Pablo P. F. de Faria) > 4. Re: Encode problem (Pablo P. F. de Faria) > 5. Re: Advanced String Search using operators AND, OR etc.. > (vince spicer) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 4 May 2009 11:17:53 -0500 > From: "Paul McGuire" > Subject: Re: [Tutor] Iterating over a long list with regular > expressions and changing each item? > To: > Message-ID: <99B447F3C7EF4996AA2ED683F1EE6DB6 at AWA2> > Content-Type: text/plain; charset="us-ascii" > > Original: > 'case_def_gen':['case_def','gen','null'], > 'nsuff_fem_pl':['nsuff','null', 'null'], > 'abbrev': ['abbrev, null, null'], > 'adj': ['adj, null, null'], > 'adv': ['adv, null, null'],} > > Note the values for 'abbrev', 'adj' and 'adv' are not lists, but strings > containing comma-separated lists. > > Should be: > 'case_def_gen':['case_def','gen','null'], > 'nsuff_fem_pl':['nsuff','null', 'null'], > 'abbrev': ['abbrev', 'null', 'null'], > 'adj': ['adj', 'null', 'null'], > 'adv': ['adv', 'null', 'null'],} > > For much of my own code, I find lists of string literals to be tedious to > enter, and easy to drop a ' character. This style is a little easier on > the > eyes, and harder to screw up. > > 'case_def_gen':['case_def gen null'.split()], > 'nsuff_fem_pl':['nsuff null null'.split()], > 'abbrev': ['abbrev null null'.split()], > 'adj': ['adj null null'.split()], > 'adv': ['adv null null'.split()],} > > Since all that your code does at runtime with the value strings is > "\t".join() them, then you might as well initialize the dict with these > computed values, for at least some small gain in runtime performance: > > T = lambda s : "\t".join(s.split()) > 'case_def_gen' : T('case_def gen null'), > 'nsuff_fem_pl' : T('nsuff null null'), > 'abbrev' : T('abbrev null null'), > 'adj' : T('adj null null'), > 'adv' : T('adv null null'),} > del T > > (Yes, I know PEP8 says *not* to add spaces to line up assignments or other > related values, but I think there are isolated cases where it does help to > see what's going on. You could even write this as: > > T = lambda s : "\t".join(s.split()) > 'case_def_gen' : T('case_def gen null'), > 'nsuff_fem_pl' : T('nsuff null null'), > 'abbrev' : T('abbrev null null'), > 'adj' : T('adj null null'), > 'adv' : T('adv null null'),} > del T > > and the extra spaces help you to see the individual subtags more easily, > with no change in the resulting values since split() splits on multiple > whitespace the same as a single space.) > > Of course you could simply code as: > > 'case_def_gen' : T('case_def\tgen\t null'), > 'nsuff_fem_pl' : T('nsuff\tnull\tnull'), > 'abbrev' : T('abbrev\tnull\tnull'), > 'adj' : T('adj\tnull\tnull'), > 'adv' : T('adv\tnull\tnull'),} > > But I think readability definitely suffers here, I would probably go with > the penultimate version. > > -- Paul > > > > > ------------------------------ > > Message: 2 > Date: Mon, 4 May 2009 14:45:06 +0200 > From: Alex Feddor > Subject: [Tutor] Advanced String Search using operators AND, OR etc.. > To: tutor at python.org > Message-ID: > <5bf184e30905040545i78bc75b8ic78eabf44a55aa20 at mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hi > > I am looking for method enables advanced text string search. Method > string.find() or re module seems no supporting what I am looking for. The > idea is as follows: > > Text ="FDA meeting was successful. New drug is approved for whole sale > distribution!" > > I would like to scan the text using AND and OR operators and gets -1 or > other value if the searching elements haven't found in the text. > Example 01: > search criteria: "FDA" AND ( "approve*" OR "supported") > The catch is that in Text variable FDA and approve words are not one after > another (other words are in between). > Example 02: > search criteria: "Ben" > The catch is that code sould find only exact Ben words not also words which > that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben > is > the right word we are looking for. > > I would really appreciated your advice - code sample / links how above can > be achieved! if possible I would appreciated solution achieved with free of > charge module. > > Cheers, Alex > PS: > A few moths ago I have discovered Python. I am amazed what all can be done > with it. Really cool programming language.. > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20090504/bbd34b5a/attachment-0001.htm > > > > ------------------------------ > > Message: 3 > Date: Mon, 4 May 2009 11:09:25 -0300 > From: "Pablo P. F. de Faria" > Subject: Re: [Tutor] Encode problem > To: Kent Johnson > Cc: *tutor python > Message-ID: > <3ea81d4c0905040709m78a45d11j2037943380817297 at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Thanks, Kent, but that doesn't solve my problem. In fact, I need > ConfigParser to work with non-ascii characters, since my App may run > in "latin-1" environments (folders e files names). I must find out why > the str() function in the module ConfigParser doesn't use the encoding > defined for the application (# -*- coding: utf-8 -*-). The rest of the > application works properly with utf-8, except for ConfigParser. What I > found out is that ConfigParser seems to make use of the configuration > in Site.py (which is set to 'ascii'), instead of the configuration > defined for the App (if I change . But this is very problematic to > have to change Site.py in every computer... So I wonder if there is a > way to replace the settings in Site.py only for my App. > > 2009/5/1 Kent Johnson : > > On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria > > wrote: > >> Hi, Kent. > >> > >> The stack trace is: > >> > >> Traceback (most recent call last): > >> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in > OnClose > >> ? ?self.SavePreferences() > >> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068, > >> in SavePreferences > >> ? ?self.cfg.set(u'File Settings',u'Recent files', > >> unicode(",".join(self.recent_files))) > >> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position > >> 12: ordinal not in range(128) > >> > >> The "unicode" function, actually doesn't do any difference... The > >> content of the string being saved is "/home/pablo/?rea de > >> Trabalho/teste.xml". > > > > OK, this error is in your code, not the ConfigParser. The problem is with > > ",".join(self.recent_files) > > > > Are the entries in self.recent_files unicode strings? If so, then I > > think the join is trying to convert to a string using the default > > codec. Try > > > > self.cfg.set('File Settings','Recent files', > > ','.join(name.encode('utf-8') for name in self.recent_files)) > > > > Looking at the ConfigParser.write() code, it wants the values to be > > strings or convertible to strings by calling str(), so non-ascii > > unicode values will be a problem there. I would use plain strings for > > all the interaction with ConfigParser and convert to Unicode yourself. > > > > Kent > > > > PS Please Reply All to reply to the list. > > > > > > -- > --------------------------------- > "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." > (Oscar Wilde) > --------------------------------- > Pablo Faria > Mestrando em Aquisi??o de Linguagem - IEL/Unicamp > Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica > (19) 3521-1570 > http://www.tycho.iel.unicamp.br/~pablofaria/ > pablofaria at gmail.com > > > ------------------------------ > > Message: 4 > Date: Mon, 4 May 2009 11:11:58 -0300 > From: "Pablo P. F. de Faria" > Subject: Re: [Tutor] Encode problem > To: Kent Johnson > Cc: *tutor python > Message-ID: > <3ea81d4c0905040711p62376925n26fb93a8955fefe4 at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Here is the traceback, after the last change you sugested: > > Traceback (most recent call last): > File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in > OnClose > self.SavePreferences() > File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1069, > in SavePreferences > self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) > File "/usr/lib/python2.5/ConfigParser.py", line 373, in write > (key, str(value).replace('\n', '\n\t'))) > File "/usr/lib/python2.5/codecs.py", line 638, in write > return self.writer.write(data) > File "/usr/lib/python2.5/codecs.py", line 303, in write > data, consumed = self.encode(object, self.errors) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position > 27: ordinal not in range(128) > > So, in "str(value)" the content is a folder name with an accented character > (?). > > 2009/5/4 Pablo P. F. de Faria : > > Thanks, Kent, but that doesn't solve my problem. In fact, I need > > ConfigParser to work with non-ascii characters, since my App may run > > in "latin-1" environments (folders e files names). I must find out why > > the str() function in the module ConfigParser doesn't use the encoding > > defined for the application (# -*- coding: utf-8 -*-). The rest of the > > application works properly with utf-8, except for ConfigParser. What I > > found out is that ConfigParser seems to make use of the configuration > > in Site.py (which is set to 'ascii'), instead of the configuration > > defined for the App (if I change . But this is very problematic to > > have to change Site.py in every computer... So I wonder if there is a > > way to replace the settings in Site.py only for my App. > > > > 2009/5/1 Kent Johnson : > >> On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria > >> wrote: > >>> Hi, Kent. > >>> > >>> The stack trace is: > >>> > >>> Traceback (most recent call last): > >>> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in > OnClose > >>> ? ?self.SavePreferences() > >>> ?File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068, > >>> in SavePreferences > >>> ? ?self.cfg.set(u'File Settings',u'Recent files', > >>> unicode(",".join(self.recent_files))) > >>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position > >>> 12: ordinal not in range(128) > >>> > >>> The "unicode" function, actually doesn't do any difference... The > >>> content of the string being saved is "/home/pablo/?rea de > >>> Trabalho/teste.xml". > >> > >> OK, this error is in your code, not the ConfigParser. The problem is > with > >> ",".join(self.recent_files) > >> > >> Are the entries in self.recent_files unicode strings? If so, then I > >> think the join is trying to convert to a string using the default > >> codec. Try > >> > >> self.cfg.set('File Settings','Recent files', > >> ','.join(name.encode('utf-8') for name in self.recent_files)) > >> > >> Looking at the ConfigParser.write() code, it wants the values to be > >> strings or convertible to strings by calling str(), so non-ascii > >> unicode values will be a problem there. I would use plain strings for > >> all the interaction with ConfigParser and convert to Unicode yourself. > >> > >> Kent > >> > >> PS Please Reply All to reply to the list. > >> > > > > > > > > -- > > --------------------------------- > > "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." > > (Oscar Wilde) > > --------------------------------- > > Pablo Faria > > Mestrando em Aquisi??o de Linguagem - IEL/Unicamp > > Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica > > (19) 3521-1570 > > http://www.tycho.iel.unicamp.br/~pablofaria/ > > pablofaria at gmail.com > > > > > > -- > --------------------------------- > "Estamos todos na sarjeta, mas alguns de n?s olham para as estrelas." > (Oscar Wilde) > --------------------------------- > Pablo Faria > Mestrando em Aquisi??o de Linguagem - IEL/Unicamp > Bolsista t?cnico FAPESP no Projeto Padr?es R?tmicos e Mudan?a Ling??stica > (19) 3521-1570 > http://www.tycho.iel.unicamp.br/~pablofaria/ > pablofaria at gmail.com > > > ------------------------------ > > Message: 5 > Date: Mon, 4 May 2009 10:38:31 -0600 > From: vince spicer > Subject: Re: [Tutor] Advanced String Search using operators AND, OR > etc.. > To: Alex Feddor > Cc: tutor at python.org > Message-ID: > <1e53c510905040938q25d787f3w17f7a18f65bd0410 at mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Advanced Strings searches are Regex via re module. > > EX: > > import re > > m = re.compile("(FDA.*?(approved|supported)|Ben[^\s])*") > > if m.search(Text): > print m.search(Text).group() > > > Vince > > > On Mon, May 4, 2009 at 6:45 AM, Alex Feddor wrote: > > > Hi > > > > I am looking for method enables advanced text string search. Method > > string.find() or re module seems no supporting what I am looking for. > The > > idea is as follows: > > > > Text ="FDA meeting was successful. New drug is approved for whole sale > > distribution!" > > > > I would like to scan the text using AND and OR operators and gets -1 or > > other value if the searching elements haven't found in the text. > > Example 01: > > search criteria: "FDA" AND ( "approve*" OR "supported") > > The catch is that in Text variable FDA and approve words are not one > after > > another (other words are in between). > > Example 02: > > search criteria: "Ben" > > The catch is that code sould find only exact Ben words not also words > which > > that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben > is > > the right word we are looking for. > > > > I would really appreciated your advice - code sample / links how above > can > > be achieved! if possible I would appreciated solution achieved with free > of > > charge module. > > > > Cheers, Alex > > PS: > > A few moths ago I have discovered Python. I am amazed what all can be > done > > with it. Really cool programming language.. > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20090504/88993fa6/attachment.htm > > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 63, Issue 8 > ************************************ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From danliang20 at gmail.com Tue May 5 06:22:45 2009 From: danliang20 at gmail.com (Dan Liang) Date: Tue, 5 May 2009 00:22:45 -0400 Subject: [Tutor] Replacing fields in lines of various lengths In-Reply-To: References: Message-ID: (Please disregard my earlier message that was sent by mistake before I finished composing. Sorry about that! :(). Hello Spir, Alan, and Paul, and tutors, Thank you Spir, Alan, and Paul for your help with my previous code! Earlier, I was asking how to separate a composite tag like the one in field 2 below with sub-tags like those in the values of the dictionary below. In my original question, I was asking about data formatted as follows: w1 \t case_def_acc w2? \t noun_prop ?w3? \t case_def_gen w4 \t dem_pron_f And I put together the code below based on your suggestions, with minor changes and it does work. -------------Begin code---------------------------- #!usr/bin/python tags = { 'noun-prop': 'noun_prop null null'.split(), 'case_def_gen': 'case_def gen null'.split(), 'dem_pron_f': 'dem_pron f null'.split(), 'case_def_acc': 'case_def acc null'.split(), } TAB = '\t' def newlyTaggedWord(line): line = line.rstrip() # I strip line ending (word,tag) = line.split(TAB) # separate parts of line, keeping data only new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): target_file = open(target_name, "w") # replacement loop for line in open(source_name, "r"): new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) source_name.close() target_file.close() if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) -------------End code---------------------------- Now since I have to workon different data format as follows: -------------Begin data---------------------------- w1 \t case_def_acc \t yes w2? \t noun_prop \t no ?w3? \t case_def_gen \t w4 \t dem_pron_f \t no w3? \t case_def_gen \t w4 \t dem_pron_f \t no w1 \t case_def_acc \t yes w3? \t case_def_gen \t w3? \t case_def_gen \t -------------End data---------------------------- Notices that some lines have nothing in yes-no filed, and hence end in a tab. My question is how to replace data in the filed of composite tags by sub-tags like those in the dictionary values above and still be able to print the whole line only with this change (i.e, composite tags replace by sub-tags). Earlier, we read words and tags from line directly into the dictionary since we were sure each line had 2 fields after separating by tabs. Here, lines have various field lengths and sometimes have yes and no finally, and sometimes not. I tried to make changes to the code above by changing the function where we read the dictionary, but it did not work. While it is ugly, I include it as a proof that I have worked on the problem. I am sure you will have various nice ideas. -------------End code---------------------------- def newlyTaggedWord(line): tagging = "" line = line.split(TAB) # separate parts of line, keeping data only if len(line)==3: word = line[-3] tag = line[-2] new_tags = tags[tag] decision = line[-1] # in decision I wanted to store #either yes or no if one of #these existed elif len(line)==2: word = line[-2] tag = line[-1] decision = TAB # I thought if it is a must to put sth in decision while decision #is really absent in line, I would put a tab. But I really want to #avoid putting anything there. new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging + TAB + decision -------------End code---------------------------- I appreciate your support! --dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From metolone+gmane at gmail.com Tue May 5 07:14:24 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Mon, 4 May 2009 22:14:24 -0700 Subject: [Tutor] Encode problem References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <20090501220601.31891dfc@o> Message-ID: "spir" wrote in message news:20090501220601.31891dfc at o... > Le Fri, 1 May 2009 15:19:29 -0300, > "Pablo P. F. de Faria" s'exprima ainsi: > >> self.cfg.write(codecs.open(self.properties_file,'w','utf-8')) >> >> As one can see, the character encoding is explicitly UTF-8. But >> ConfigParser keeps trying to save it as a 'ascii' file and gives me >> error for directory-names containing >128 code characters (like "?"). >> It is just a horrible thing to me, for my app will be used mostly by >> brazillians. > > Just superficial suggestions, only because it's 1st of May and WE so that > better answers won't maybe come up before monday. > > If all what you describe is right, then there must be something wrong with > char encoding in configParser's write method. Have you had a look at it? > While I hardly imagine why/how ConfigParser would limit file pathes to > 7-bit ASCII... > Also, for porteguese characters, you shouldn't even need explicit > encoding; they should pass through silently because they fit in an 8 bit > latin charset. (I never encode french path/file names.) The below works. ConfigParser isn't written to support Unicode correctly. I was able to get Unicode sections to write out, but it was just luck. Unicode keys and values break as the OP discovered. So treat everything as byte strings: ---------------------------------------------------- # coding: utf-8 # Note coding is required because of non-ascii # in the source code. This ONLY controls the # encoding of the source file characters saved to disk. import ConfigParser import glob import sys c = ConfigParser.ConfigParser() c.add_section('??') # this is a utf-8 encoded byte string...no u'') c.set('??','??','??') # so are these # The following could be glob.glob(u'.') to get a filename in # Unicode, but this is for illustration that the encoding of the # source file has no bearing on the encoding strings other than # one's hard-coded in the source file. The 'files' list will be byte # strings in the default file system encoding. Which for Windows # is 'mbcs'...a magic value that changes depending on the # which country's version of Windows is running. files = glob.glob('*.txt') c.add_section('files') for i,fn in enumerate(files): fn = fn.decode(sys.getfilesystemencoding()) fn = fn.encode('utf-8') c.set('files','file%d'%(i+1),fn) # Don't need a codec here...everything is already UTF8. c.write(open('chinese.txt','wt')) -------------------------------------------------------------- Here is the content of my utf-8 file: ----------------------------- [files] file3 = ascii.txt file2 = chinese.txt file1 = blah.txt file5 = ?????.txt file4 = other.txt [??] ?? = ?? ---------------------------- Hope this helps, Mark From lie.1296 at gmail.com Tue May 5 08:02:37 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 05 May 2009 16:02:37 +1000 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> Message-ID: Alan Gauld wrote: > And file has been removed again in Python v3.... > In fact open is now an alias for io.open and no longer simply returns > a file object - in fact the file type itself is gone too! > > A pity, there are cases where I found file() more intuitive than > open and vice versa so liked having both available. The fact that it > looked like creating an instance of a class seemed to fit well > in OO code. But having both of them is a violation of "There should be one-- and preferably only one --obvious way to do it." I think python's duck typing culture makes it very rare that you want to test whether a file is an impostor. From smiles at worksmail.net Tue May 5 08:17:38 2009 From: smiles at worksmail.net (C or L Smith) Date: Tue, 5 May 2009 12:02:38 +0545 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. References: Message-ID: <6E6137FD96CE416FA3D6D59EC4AD92BE@kisc.edu.np> >> From: Alex Feddor >> >> I am looking for method enables advanced text string search. Method >> string.find() or re module seems no supporting what I am looking >> for. The idea is as follows: >> >> Text ="FDA meeting was successful. New drug is approved for whole >> sale distribution!" >> >> >> I would really appreciated your advice - code sample / links how >> above can >> be achieved! if possible I would appreciated solution achieved with >> free of >> charge module. The pieces to assemble a solution are not too hard to master. Instead of thinking of searching your text, think about searching a list of words in the text for what you are interested in. The re pattern to match a word containing only letters is [a-zA-Z]+. This pattern can cut your text into words for you. A list of words corresponding to your text can then be made with re.findall(): ### >>> word=re.compile('[a-zA-Z]+') >>> text = """FDA meeting was successful.""" >>> Words = re.findall(word, text) >>> Words ['FDA', 'meeting', 'was', 'successful'] >>> ### There are some gems hidden in some of the modules that are intended for one purpose but can be handy for another. For your purposes, the fnmatch module has a lightweight (compared to re) string matching function that can be used to find out if a word matches a given criteria or not. There are only 4 types of patterns to master: * matches anything ? matches a single character [seq] matches any character in the sequence [!seq] matches any character NOT in the sequence Within the module there is a case sensitive and case insensitive version of a pattern matcher. We can write a helper function that allows us to use either one (and it is set right now to be case sensitive by default): ### import fnmatch def match(pat, words, case=True): """See if pat matches an word in words list. It uses a generator rather than a list inside the any() so as not to generate the whole list if at all possible.""" if case: return any(x for x in words if fnmatch.fnmatchcase(x,pat)) else: return any(x for x in words if fnmatch.fnmatch(x,pat)) ### Now you can see if a certain pattern is in your list of words or not: ### >>> Words=['FDA', 'meeting', 'was', 'successful'] >>> match('FDA',Words) True >>> match('fda',Words) False >>> match('fda',Words, case=False) True >>> ### And now string together whatever tests you like for a given line: ### >>> match('FDA',Words) and (match('approve*',Words) or match('success*',Words)) True >>> ### If you are searching a large piece of text you might want to turn the list of words into a set of unique words so there is less to search. The match function will work with it equally as well. ### >>> text='this is a list is a list is a list' >>> re.findall(word,text) ['this', 'is', 'a', 'list', 'is', 'a', 'list', 'is', 'a', 'list'] >>> set(_) set(['this', 'a', 'is', 'list']) >>> match('is', _) True >>> ### You also might want to apply your search line by line, but those are details you might already know how to handle. Hope that helps! /chris From denis.spir at free.fr Tue May 5 11:27:28 2009 From: denis.spir at free.fr (spir) Date: Tue, 5 May 2009 11:27:28 +0200 Subject: [Tutor] how to reference a function itself when accessing its private functions? In-Reply-To: References: Message-ID: <20090505112728.4ee9e1d3@o> Le Mon, 04 May 2009 16:08:38 -0700, Emile van Sebille s'exprima ainsi: > On 5/4/2009 3:37 PM Tim Michelsen said... > > Dear Tutors and fellow pythonistas, > > I would like to get access to the private methods of my function. > > > > For instance: > > Who can I reference the docstring of a function within the function > > itself? > > > > > def show2(str): > > """prints str""" > > print str > > d = self.__doc__ > > print d > > >>> def show2(str): > ... """prints str""" > ... print str > ... print globals()['show2'].__doc__ > ... > >>> show2('hello') > hello > prints str > >>> Hello Emile, Why don't you use the func name directly? print show2.__doc__ I mean it's a constant in the sense that it is know at design time, right? And you need it anyway, as shown by the fact that it becomes a string literal in the your version. It's not like if you would _not_ know it ;-) Fortunately, funcs (and methods and classes and modules -- but not other object AFAIK) know their own (birth)name: def docString(obj): name = obj.__name__ return globals()[name].__doc__ Should work for the above listed object types. Anyway, other types have no docstring... > This is the easy way -- ie, you know where to look and what name to use. > You can discover the name using the inspect module, but it can get > ugly. If you're interested start with... > > from inspect import getframeinfo, currentframe ------ la vita e estrany From denis.spir at free.fr Tue May 5 11:33:41 2009 From: denis.spir at free.fr (spir) Date: Tue, 5 May 2009 11:33:41 +0200 Subject: [Tutor] Conversion question In-Reply-To: References: Message-ID: <20090505113341.5f4265e7@o> Le Tue, 5 May 2009 00:52:11 +0100, "Alan Gauld" s'exprima ainsi: > > Is there a way in Python to say this is a string of HEX characters like > > Perl's pack? Right now I have to take the string and add a \x to every > > two > > values i.e. \x41\x42... > > Assuming you actually want to send the hex values rather than > a hex string representation then the way I'd send that would be > to convert that to a number using int() then transmit it using > struct() I can hardly imagine why you want an hex string representation for further process instead of the values, neither. Would be interested in the reason for this. Assuming that an interface require hex string, wouldn't it be easier to change this so that it directly gets values? Id est avoid double format conversion. Denis ------ la vita e estrany From alan.gauld at btinternet.com Tue May 5 11:37:27 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 10:37:27 +0100 Subject: [Tutor] Conversion question References: Message-ID: "Emile van Sebille" wrote >> "414243440d0a" >> >> Is there a way in Python to say this is a string of HEX characters like >> Perl's pack? Right now I have to take the string and add a \x to every >> two values i.e. \x41\x42... > > import binascii > binascii.a2b_hex('41424344') I hadn't come across binascii before, but it doesn't do what I expected: >>> import binascii as b >>> b.a2b_hex('414243440d0a') 'ABCD\r\n' >>> This appears to be converting it to a binary value then displaying that binary value as an ascii string. I'm not sure what the value of that is over struct or int? Can anyone enlighten me about why I'd ever want to use this? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 5 11:43:22 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 10:43:22 +0100 Subject: [Tutor] Replacing fields in lines of various lengths References: Message-ID: "Dan Liang" wrote > And I put together the code below based on your suggestions, with minor > changes and it does work. Good, now your question is? -------------Begin code---------------------------- #!usr/bin/python tags = { 'noun-prop': 'noun_prop null null'.split(), 'case_def_gen': 'case_def gen null'.split(), 'dem_pron_f': 'dem_pron f null'.split(), 'case_def_acc': 'case_def acc null'.split(), } TAB = '\t' def newlyTaggedWord(line): line = line.rstrip() # I strip line ending (word,tag) = line.split(TAB) # separate parts of line, keeping data only new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): target_file = open(target_name, "w") # replacement loop for line in open(source_name, "r"): new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) source_name.close() target_file.close() AG> These two lines should be inside the function, after the loop. if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) -------------End code---------------------------- Now since I have to workon different data format as follows: -------------Begin data---------------------------- w1 \t case_def_acc \t yes w2? \t noun_prop \t no ?w3? \t case_def_gen \t w4 \t dem_pron_f \t no w3? \t case_def_gen \t w4 \t dem_pron_f \t no w1 \t case_def_acc \t yes w3? \t case_def_gen \t w3? \t case_def_gen \t -------------End data---------------------------- Notices that some lines have nothing in yes-no filed, and hence end in a tab. My question is how to replace data in the filed of composite tags by sub-tags like those in the dictionary values above and still be able to print the whole line only with this change (i.e, composite tags replace by sub-tags). Earlier, we read words and tags from line directly into the dictionary since we were sure each line had 2 fields after separating by tabs. Here, lines have various field lengths and sometimes have yes and no finally, and sometimes not. I tried to make changes to the code above by changing the function where we read the dictionary, but it did not work. While it is ugly, I include it as a proof that I have worked on the problem. I am sure you will have various nice ideas. -------------End code---------------------------- def newlyTaggedWord(line): tagging = "" line = line.split(TAB) # separate parts of line, keeping data only if len(line)==3: word = line[-3] tag = line[-2] new_tags = tags[tag] decision = line[-1] # in decision I wanted to store #either yes or no if one of #these existed elif len(line)==2: word = line[-2] tag = line[-1] decision = TAB # I thought if it is a must to put sth in decision while decision #is really absent in line, I would put a tab. But I really want to #avoid putting anything there. new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags) # join with TABs return word + TAB + tagging + TAB + decision -------------End code---------------------------- I appreciate your support! --dan -------------------------------------------------------------------------------- > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Tue May 5 11:40:06 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 10:40:06 +0100 Subject: [Tutor] Tutor Digest, Vol 63, Issue 8 References: Message-ID: Please use a sensible subject and don;t reporst the whole digest. "Dan Liang" wrote in message > Thank you for your help. I have been working on the file, but I still > have a > problem doing what I wanted. As a reminder, Can you tell us what exactly the problem is? It's easier than us trying to guess. > #!usr/bin/python > tags = { > 'noun-prop': 'noun_prop null null'.split(), > 'case_def_gen': 'case_def gen null'.split(), > 'dem_pron_f': 'dem_pron f null'.split(), > 'case_def_acc': 'case_def acc null'.split(), > } > > TAB = '\t' > > def newlyTaggedWord(line): > (word,tag) = line.split(TAB) # separate parts of line, keeping > new_tags = tags[tag] # read in dict > tagging = TAB.join(new_tags) # join with TABs > return word + TAB + tagging # formatted result > > def replaceTagging(source_name, target_name): > target_file = open(target_name, "w") > # replacement loop > for line in open(source_name, "r"): > new_line = newlyTaggedWord(line) + '\n' > target_file.write(new_line) > > target_file.close() > > if __name__ == "__main__": > source_name = sys.argv[1] > target_name = sys.argv[2] > replaceTagging(source_name, target_name) From alan.gauld at btinternet.com Tue May 5 11:46:01 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 10:46:01 +0100 Subject: [Tutor] Replacing fields in lines of various lengths References: Message-ID: My turn to apologise for premature sending of email... For some reason I didn't notice the section of mail after the code snippet! Alan G "Dan Liang" wrote in message Now since I have to work on different data format as follows: -------------Begin data---------------------------- w1 \t case_def_acc \t yes w2? \t noun_prop \t no ?w3? \t case_def_gen \t w4 \t dem_pron_f \t no -------------End data---------------------------- Notices that some lines have nothing in yes-no filed, and hence end in a tab. My question is how to replace data in the filed of composite tags by sub-tags like those in the dictionary values above and still be able to print the whole line only with this change (i.e, composite tags replace by sub-tags). From denis.spir at free.fr Tue May 5 12:06:48 2009 From: denis.spir at free.fr (spir) Date: Tue, 5 May 2009 12:06:48 +0200 Subject: [Tutor] Replacing fields in lines of various lengths In-Reply-To: References: Message-ID: <20090505120648.38b9dcc5@o> Le Tue, 5 May 2009 00:22:45 -0400, Dan Liang s'exprima ainsi: > -------------Begin data---------------------------- > > w1 \t case_def_acc \t yes > w2? \t noun_prop \t no > ?w3? \t case_def_gen \t > w4 \t dem_pron_f \t no > w3? \t case_def_gen \t > w4 \t dem_pron_f \t no > w1 \t case_def_acc \t yes > w3? \t case_def_gen \t > w3? \t case_def_gen \t > > -------------End data---------------------------- > I tried to make changes to the code above by changing the function where we > read the dictionary, but it did not work. While it is ugly, I include it as > a proof that I have worked on the problem. I am sure you will have various > nice ideas. > > > -------------End code---------------------------- > def newlyTaggedWord(line): > tagging = "" > line = line.split(TAB) # separate parts of line, keeping data only > if len(line)==3: > word = line[-3] > tag = line[-2] > new_tags = tags[tag] > decision = line[-1] > > # in decision I wanted to store #either yes or no if one of #these existed > > elif len(line)==2: > word = line[-2] > tag = line[-1] > decision = TAB > > # I thought if it is a must to put sth in decision while decision #is really > absent in line, I would put a tab. But I really want to #avoid putting > anything there. > > new_tags = tags[tag] # read in dict > tagging = TAB.join(new_tags) # join with TABs > return word + TAB + tagging + TAB + decision > -------------End code---------------------------- > For simplicity, it would be cool if file would have some placeholder in place of absent yes/no 'decisions' so that you know there are always 3 fields. That's what would be cool with most languages. But python is rather flexible and clever for such border cases. Watch the example below: s1, s2 = "1\t2\t3", "1\t2\t" items1, items2 = s1.split('\t'), s2.split('\t') print items1, items2 ==> ['1', '2', '3'] ['1', '2', ''] So that you always have 3 items, the 3rd one maybe the empty string. Right? This means: * You can safely write "(word,tag,decision) = line.split(TAB)" [Beware of misleading naming like "line = line.split(TAB)", for after this the name 'line' actually refers to field values.] * You can have a single process. * The elif branch in you code above will never run, i guess ;-) [place a print instruction inside to check that] Denis Ps: I noticed that in your final version for the case of files with 2 fields only, you misplaced the file closings. They fit better in the func. ------ la vita e estrany From xchimeras at gmail.com Tue May 5 12:17:42 2009 From: xchimeras at gmail.com (Tom Green) Date: Tue, 5 May 2009 06:17:42 -0400 Subject: [Tutor] Conversion question In-Reply-To: References: Message-ID: Thanks everyone for your replies. This reason for the long string is sometimes I have to pass a 32 byte key (MD5 hash value) to the socket. The data I am sending is the hex values i.e. 41=A. I believe the binascii will work. Mike On Mon, May 4, 2009 at 7:52 PM, Alan Gauld wrote: > > "Tom Green" wrote > > Here is my question. I work with a lot of sockets and most of them >> require >> hex data. I am usually given a string of data to send to the socket. >> Example: >> >> "414243440d0a" >> >> Is there a way in Python to say this is a string of HEX characters like >> Perl's pack? Right now I have to take the string and add a \x to every >> two >> values i.e. \x41\x42... >> > > Assuming you actually want to send the hex values rather than > a hex string representation then the way I'd send that would be > to convert that to a number using int() then transmit it using > struct() > > Sometimes my string values are 99+ bytes in length. I did write a parsing >> program that would basically loop thru the string and insert the \x, but I >> was wondering if there was another or better way. >> > > OK, Maybe you do want to send the hex representation rather than > the actual data (I can't think why unless you have a very strange > parser at the other end). In that case I think you do need to insert > the \x characters. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue May 5 12:37:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 5 May 2009 06:37:53 -0400 Subject: [Tutor] Encode problem In-Reply-To: References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com> <20090501220601.31891dfc@o> Message-ID: <1c2a2c590905050337j1afc177ene64f800dcc3a7e7a@mail.gmail.com> On Tue, May 5, 2009 at 1:14 AM, Mark Tolonen wrote: > The below works. ?ConfigParser isn't written to support Unicode correctly. I > was able to get Unicode sections to write out, but it was just luck. Unicode > keys and values break as the OP discovered. ?So treat everything as byte > strings: Thanks for the complete example. > files = glob.glob('*.txt') > c.add_section('files') > > for i,fn in enumerate(files): > ? fn = fn.decode(sys.getfilesystemencoding()) I think if you give a Unicode string to glob.glob(), e.g. glob.glob(u'*.txt'), then the strings returned will also be unicode and this decode step will not be needed. Kent From denis.spir at free.fr Tue May 5 12:41:21 2009 From: denis.spir at free.fr (spir) Date: Tue, 5 May 2009 12:41:21 +0200 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> Message-ID: <20090505124121.30a474cc@o> Le Tue, 5 May 2009 00:41:39 +0100, "Alan Gauld" s'exprima ainsi: > > Backwards compatibility. The file type was introduced in python 2.2, > > before which there was open. > > And file has been removed again in Python v3.... > In fact open is now an alias for io.open and no longer simply returns > a file object - in fact the file type itself is gone too! > > A pity, there are cases where I found file() more intuitive than > open and vice versa so liked having both available. The fact that it > looked like creating an instance of a class seemed to fit well > in OO code. Same for me. It makes files an exception in the python OO system. Conversely, I have always considered open(), rather than file(), a flaw. (But there are numerous things on which I have a different point of view than the majority of pythonistas ;-) Denis ------ la vita e estrany From srilyk at gmail.com Tue May 5 13:08:35 2009 From: srilyk at gmail.com (W W) Date: Tue, 5 May 2009 06:08:35 -0500 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <20090505124121.30a474cc@o> References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> <20090505124121.30a474cc@o> Message-ID: <333efb450905050408m48246dd8wc90b94880898e3ca@mail.gmail.com> On Tue, May 5, 2009 at 5:41 AM, spir wrote: > Le Tue, 5 May 2009 00:41:39 +0100, > "Alan Gauld" s'exprima ainsi: > > > > Backwards compatibility. The file type was introduced in python 2.2, > > > before which there was open. > > > > And file has been removed again in Python v3.... > > In fact open is now an alias for io.open and no longer simply returns > > a file object - in fact the file type itself is gone too! > > > > A pity, there are cases where I found file() more intuitive than > > open and vice versa so liked having both available. The fact that it > > looked like creating an instance of a class seemed to fit well > > in OO code. > > Same for me. It makes files an exception in the python OO system. > Conversely, I have always considered open(), rather than file(), a flaw. > (But there are numerous things on which I have a different point of view > than the majority of pythonistas ;-) > Well, when you consider that it's really, underneath everything(way down in the assembly code), a pointer to a location on the hard disk/memory, open does make a little more sense. Of course, conversely there are also (like Alan mentioned) cases where file just makes more sense when you're looking at/writing the code, even if the concept may not technically be correct. Still, I think that's part of OO programming - obscuring some of the underlying ideas in favor of clarity of purpose. For those of us who may still find a need/desire to use file(), I'm sure file = open still works in python3 ( of course that's not making one preferably obvious right way of doing things ) -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue May 5 14:11:22 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 05 May 2009 22:11:22 +1000 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: Alex Feddor wrote: > Hi > > I am looking for method enables advanced text string search. Method > string.find() or re module seems no supporting what I am looking for. > The idea is as follows: > > Text ="FDA meeting was successful. New drug is approved for whole sale > distribution!" > > I would like to scan the text using AND and OR operators and gets -1 or > other value if the searching elements haven't found in the text. > > Example 01: > search criteria: "FDA" AND ( "approve*" OR "supported") > The catch is that in Text variable FDA and approve words are not one > after another (other words are in between). Bring on your hardest searches... class Pattern(object): pass class Logical(Pattern): def __init__(self, pat1, pat2): self.pat1 = pat1 self.pat2 = pat2 def __call__(self, text): a, b = self.pat1(text), self.pat2(text) if self.op(a != len(text), b != len(text)): return min((a, b)) return len(text) def __str__(self): return '(%s %s %s)' % (self.pat1, self.op_name, self.pat2) class P(Pattern): def __init__(self, pat): self.pat = pat def __call__(self, text): ret = text.find(self.pat) return ret if ret != -1 else len(text) def __str__(self): return '"%s"' % self.pat class NOT(Pattern): def __init__(self, pat): self.op_name = 'NOT' self.pat = pat def __call__(self, text): ret = self.pat(text) return ret - 1 if ret == len(text) else len(text) def __str__(self): return '%s (%s)' % (self.op_name, self.pat) class XOR(Logical): def __init__(self, pat1, pat2): self.op_name = 'XOR' self.op = lambda a, b: not(a and b) and (a or b) super().__init__(pat1, pat2) class OR(Logical): def __init__(self, pat1, pat2): self.op_name = 'OR' self.op = lambda a, b: a or b super().__init__(pat1, pat2) class AND(Logical): def __init__(self, pat1, pat2): self.op_name = 'AND' self.op = lambda a, b: a and b super().__init__(pat1, pat2) class Suite(object): def __init__(self, pat): self.pat = pat def __call__(self, text): ret = self.pat(text) return ret if ret != len(text) else -1 def __str__(self): return '[%s]' % self.pat pat1 = P('FDA') pat2 = P('approve*') pat3 = P('supported') p = Suite(AND(pat1, OR(pat2, pat3))) print(p('')) print(p('FDA')) print(p('FDA supported')) print(p('supported FDA')) print(p('blah FDA bloh supported blih')) print(p('blah FDA bleh supported bloh supported blih ')) p = Suite(AND(OR(pat1, pat2), XOR(pat2, NOT(pat3)))) print(p) print(p('')) print(p('FDA')) print(p('FDA supported')) print(p('supported sdc FDA sd')) print(p('blah blih FDA bluh')) print(p('blah blif supported blog')) ################# I guess I went a bit overboard here (had too much time on hand), the working is based on function composition, so instead of evaluation, you composes a function (or more accurately, a callable class) that will evaluate the logical value and return the index of the first item that matches the logical expression. It currently uses str's builtin find, but I guess it wouldn't be very hard to adapt it to use the re myfind() below (only P class will need to change) The Suite class is only there to turn the NotFound sentinel from len(text) to -1 (used len(text) since it simplifies the code a lot...) Caveat: The NOT class cannot reliably convert a False to True because I don't know what index number to use. Code written for efficient vertical space, not the most readable in the world. No guarantee no bug. Idea: Overrides the operator on Pattern class so we could write it like: P("Hello") & P("World") instead of AND(P("Hello"), P("World")) > Example 02: > search criteria: "Ben" > The catch is that code sould find only exact Ben words not also words > which that has firts three letters Ben such as Benquick, Benseek etc.. > Only Ben is the right word we are looking for. The second one was easier... import re def myfind(pattern, text): pattern = r'(.*?)\b(%s)\b(.*)' % pattern m = re.match(pattern, text) if m: return len(m.group(1)) textfound = 'This is a Ben test string' texttrick = 'This is a Benquick Benseek McBen QuickBenSeek string' textnotfound = 'He is away' textmulti = 'Our Ben found another Ben which is quite odd' pat = 'Ben' print(myfind(pat, textfound)) # 10 print(myfind(pat, texttrick)) # None print(myfind(pat, textnotfound)) # None print(myfind(pat, textmulti)) # 4 if you only want to test for existence, simply: pattern = 'Ben' if re.match(r'(.*?)\b(%s)\b(.*)' % pattern, text): pass > I would really appreciated your advice - code sample / links how above > can be achieved! if possible I would appreciated solution achieved > with free of charge module. Standard library is free of charge, no? From kent37 at tds.net Tue May 5 14:45:23 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 5 May 2009 08:45:23 -0400 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: <1c2a2c590905050545l7ebff77fk85be5f8ac58b6db1@mail.gmail.com> On Tue, May 5, 2009 at 8:11 AM, Lie Ryan wrote: > Bring on your hardest searches... Nice! > The Suite class is only there to turn the NotFound sentinel from len(text) > to -1 (used len(text) since it simplifies the code a lot...) How does this simplify the code? Why not use the 'in' operator and return True or False from the terms? Kent From lie.1296 at gmail.com Tue May 5 15:42:09 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 05 May 2009 23:42:09 +1000 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: <1c2a2c590905050545l7ebff77fk85be5f8ac58b6db1@mail.gmail.com> References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> <1c2a2c590905050545l7ebff77fk85be5f8ac58b6db1@mail.gmail.com> Message-ID: <4A004231.5000602@gmail.com> Kent Johnson wrote: > On Tue, May 5, 2009 at 8:11 AM, Lie Ryan wrote: > >> Bring on your hardest searches... > > Nice! > >> The Suite class is only there to turn the NotFound sentinel from len(text) >> to -1 (used len(text) since it simplifies the code a lot...) > > How does this simplify the code? Why not use the 'in' operator and > return True or False from the terms? Using len(text) as NotFound sentinel simplifies the code because, as it was searching for the lowest index that matches the expression, I used min((a, b)) to do the comparison, and if NotFound is -1, min((a, b)) will return -1. Since len(text) is always higher (or equal) to the other expression and thus will not affect the comparison in codespeak: text = 'ABCD' pat1 = 'C' # P('C') determined 2 pat2 = 'Z' # P('Z') determined 4/NotFound expr = OR(P('C'), P('Z')) # 2 or NotFound is True # min(2, 4) is 2 pat3 = 'A' # P('A') determined 0 expr = OR(P('C'), P('A')) # 3 or 0 is True # min((3, 0)) is 0 pat4 = 'Y' # P('Y') determined 4/NotFound expr = OR(P('Z'), P('Y')) # NotFound or NotFound is False $ min(4, 4) is 4, i.e. NotFound Also, the 'in' operator cannot be used to handle nested expressions. On nested expressions, I simply need to evaluate two indexes and find their lower index (if applicable). Other alternative sentinel I was thinking about was 0, which simplify the if expressions, but it 1) also suffers the same problem with -1 and 2) 0 is already occupied by the first character. I decided that having pat1 != len(text) sprinkled is simpler than rewriting min() to special case -1 AND still had to have pat != -1 sprinkled. From denis.spir at free.fr Tue May 5 15:49:08 2009 From: denis.spir at free.fr (spir) Date: Tue, 5 May 2009 15:49:08 +0200 Subject: [Tutor] Advanced String Search using operators AND, OR etc.. In-Reply-To: References: <5bf184e30905040545i78bc75b8ic78eabf44a55aa20@mail.gmail.com> Message-ID: <20090505154908.1e6e37af@o> Le Tue, 05 May 2009 22:11:22 +1000, Lie Ryan s'exprima ainsi: > Idea: > Overrides the operator on Pattern class so we could write it like: > P("Hello") & P("World") instead of AND(P("Hello"), P("World")) You could also override directly all python logical ops to allow direct expression of logical searches in python (~ like in pyparsing). (P("Hello") or P("Salut")) and (P("World") and not P("Universe")) (Then rewriting from user expression may even be easier.) ;-) Denis ------ la vita e estrany From metolone+gmane at gmail.com Tue May 5 16:16:54 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Tue, 5 May 2009 07:16:54 -0700 Subject: [Tutor] Encode problem References: <3ea81d4c0905011119y5c2e19dbyc385937d6f5b97b2@mail.gmail.com><20090501220601.31891dfc@o> <1c2a2c590905050337j1afc177ene64f800dcc3a7e7a@mail.gmail.com> Message-ID: "Kent Johnson" wrote in message news:1c2a2c590905050337j1afc177ene64f800dcc3a7e7a at mail.gmail.com... > On Tue, May 5, 2009 at 1:14 AM, Mark Tolonen > wrote: > > The below works. ConfigParser isn't written to support Unicode > > correctly. I > > was able to get Unicode sections to write out, but it was just luck. > > Unicode > > keys and values break as the OP discovered. So treat everything as byte > > strings: > Thanks for the complete example. > > files = glob.glob('*.txt') > > c.add_section('files') > > > > for i,fn in enumerate(files): > > fn = fn.decode(sys.getfilesystemencoding()) > I think if you give a Unicode string to glob.glob(), e.g. > glob.glob(u'*.txt'), then the strings returned will also be unicode > and this decode step will not be needed. You're right, that's why I had the comment above it :^) # The following could be glob.glob(u'.') to get a filename in # Unicode, but this is for illustration that the encoding of the # source file has no bearing on the encoding strings other than # ones hard-coded in the source file. The OP had wondered why his source file encoding "doesn't use the encoding defined for the application (# -*- coding: utf-8 -*-)." and I thought this would illustrate that byte strings could be in other encodings. It also shows the reason spir could said "... you shouldn't even need explicit encoding; they should pass through silently because they fit in an 8 bit latin charset.". If I'd left out the Chinese, I could've use a latin-1 encoding for everthing and not decode or encode at all (assuming the file system was latin-1). -Mark From metolone+gmane at gmail.com Tue May 5 16:20:35 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Tue, 5 May 2009 07:20:35 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') References: <49FF60C5.4040707@gmx.net> <49FF633F.9070007@gmail.com> <20090505124121.30a474cc@o> <333efb450905050408m48246dd8wc90b94880898e3ca@mail.gmail.com> Message-ID: "W W" wrote in message news:333efb450905050408m48246dd8wc90b94880898e3ca at mail.gmail.com... On Tue, May 5, 2009 at 5:41 AM, spir wrote: > Le Tue, 5 May 2009 00:41:39 +0100, > "Alan Gauld" s'exprima ainsi: > > > > Backwards compatibility. The file type was introduced in python 2.2, > > > before which there was open. > > > > And file has been removed again in Python v3.... > > In fact open is now an alias for io.open and no longer simply returns > > a file object - in fact the file type itself is gone too! I, for one, am glad it is gone again. It took awhile to train myself not to write: file = open('filename.txt') and incorrectly shadow the built-in. It's just the most natural name for the variable :^) -Mark From eduardo.susan at gmail.com Tue May 5 18:19:17 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Tue, 5 May 2009 10:19:17 -0600 Subject: [Tutor] Using read() before closing a file Message-ID: <9356b9f30905050919o3e8fdb16t9d9c4748ccb0dcd4@mail.gmail.com> Hello, I found interesting today, as I was playing with this code in the shell: >>> arq = open('c:/myscripts/simple2.ttt', 'w') >>> len(a) 9 >>> arq.write('The length is %d' % len(a)) >>> res = arq.read() >>> print res The print doesn't print the text I had written, but some thing else I can't copy and paste to this message. But if I had closed the file and then did this: res = open('c:/myscripts/simple2.ttt').read(), it would have worked. Just thought of sharing this. Eduardo From alan.gauld at btinternet.com Tue May 5 19:37:42 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 May 2009 18:37:42 +0100 Subject: [Tutor] Using read() before closing a file References: <9356b9f30905050919o3e8fdb16t9d9c4748ccb0dcd4@mail.gmail.com> Message-ID: "Eduardo Vieira" wrote >>>> arq = open('c:/myscripts/simple2.ttt', 'w') >>>> arq.write('The length is %d' % len(a)) >>>> res = arq.read() >>>> print res > > The print doesn't print the text I had written, That's because of two things: a) You opened the file for write only not reading b) You had moved the file cursor beyond the text you wrote On (b): the file is conceptually a sequential data stream so think of the cursor in a text editor line. After you have written something you cannot select what you have written unless you cursor back to the beginning. Similarly with a file. Even if you opened it with rw access you would still need to position the cursor (with the seek() call) to the point before what you wrote to be able to read it. After reading it the cursor would once again be at the end of the file and you could write more data. > But if I had closed the file and > then did this: res = open('c:/myscripts/simple2.ttt').read(), it would > have worked. Thats right because you now open the file for reading, therefore the cursor is repositioned at he start of the file and the mode is set correctly. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From wescpy at gmail.com Tue May 5 20:38:03 2009 From: wescpy at gmail.com (wesley chun) Date: Tue, 5 May 2009 11:38:03 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <49FF60C5.4040707@gmx.net> References: <49FF60C5.4040707@gmx.net> Message-ID: <78b3a9580905051138i55719a1dwe3ed11722bf7b001@mail.gmail.com> > It seems that open(filename, 'r') and file(filename, 'r') are > used interchangeably, and I wonder what this is all about. as alan and others have pointed out, file() was added in 2.2. this was part of the unification of types and classes. every object now has a factory function, i.e., list() creates a list, and same goes for int, float, dict, etc., and of course, file() creates a file. open(), on the other hand, has been around for ages and will remain so while file() is removed in 3.0 -- yes, easy come, easy go. the original intention was *not* to have file() for opening files with, but rather for type checking. in other words: OPEN A FILE with open(), i.e., f = open('foo.txt', 'r') TEST FOR A FILE with file(), i.e., if isinstance(f, file): ... does anyone have the idiom for the above isinstance() check for 3.x? hope the rest of 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 From timmichelsen at gmx-topmail.de Tue May 5 21:21:03 2009 From: timmichelsen at gmx-topmail.de (Tim Michelsen) Date: Tue, 05 May 2009 21:21:03 +0200 Subject: [Tutor] how to reference a function itself when accessing its private functions? In-Reply-To: <1c2a2c590905041655k289aac31r1b710d6159e3a3b6@mail.gmail.com> References: <1c2a2c590905041655k289aac31r1b710d6159e3a3b6@mail.gmail.com> Message-ID: Thanks a lot! From Larry at Riedel.org Tue May 5 23:35:26 2009 From: Larry at Riedel.org (Larry Riedel) Date: Tue, 5 May 2009 14:35:26 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: <49FF60C5.4040707@gmx.net> References: <49FF60C5.4040707@gmx.net> Message-ID: <7c64c2920905051435t5ef08dd3ndc3ee76a5e773af1@mail.gmail.com> It may not be a good reason, but I like that "open" is preferred, because in Java a "File" object is not a stream for the content in the file, just its directory entry, and simply creating a File object does not open the file at all. Larry From burgess.nick at gmail.com Wed May 6 00:15:26 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Tue, 5 May 2009 18:15:26 -0400 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> <49FF29E8.4070904@mwalsh.org> <3a13c8e50905041251re566f17oe6ee4927e7c366f3@mail.gmail.com> Message-ID: <3a13c8e50905051515l291ab4c8o214db70cb31d79ae@mail.gmail.com> for root, dirs, files in os.walk('./'): for f in files: if f.endswith('.txt'): allfiles.append(os.path.join(root, f)) This returns a list of the *.txt files. Of course now I am stuck on how to apply the delimiter and search function to the items in the list..? Any clues/examples would be great. ty On Mon, May 4, 2009 at 7:32 PM, Alan Gauld wrote: >> Mr. Gauld is referring to!! ?I searched python.org and alan-g.me.uk. >> Does anyone have a link? > > I posted a link to the Python howto and my tutorial is at alan-g.me.uk > You will find it on the contents frame under Regular Expressions... > Its in the Advanced Topics section. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From burgess.nick at gmail.com Wed May 6 00:16:38 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Tue, 5 May 2009 18:16:38 -0400 Subject: [Tutor] returning the entire line when regex matches In-Reply-To: <3a13c8e50905051515l291ab4c8o214db70cb31d79ae@mail.gmail.com> References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com> <49FF29E8.4070904@mwalsh.org> <3a13c8e50905041251re566f17oe6ee4927e7c366f3@mail.gmail.com> <3a13c8e50905051515l291ab4c8o214db70cb31d79ae@mail.gmail.com> Message-ID: <3a13c8e50905051516r3f96485ak42f50097bb4ed9cd@mail.gmail.com> forgot to add this at the top, allfiles = [] On Tue, May 5, 2009 at 6:15 PM, Nick Burgess wrote: > for root, dirs, files in os.walk('./'): > ? ?for f in files: > ? ? ? ?if f.endswith('.txt'): > ? ? ? ? ? ?allfiles.append(os.path.join(root, f)) > > This returns a list of the *.txt files. Of course now I am stuck on > how to apply the delimiter and search function to the items in the > list..? ?Any clues/examples would be great. ty > > On Mon, May 4, 2009 at 7:32 PM, Alan Gauld wrote: >>> Mr. Gauld is referring to!! ?I searched python.org and alan-g.me.uk. >>> Does anyone have a link? >> >> I posted a link to the Python howto and my tutorial is at alan-g.me.uk >> You will find it on the contents frame under Regular Expressions... >> Its in the Advanced Topics section. >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > From alan.gauld at btinternet.com Wed May 6 02:00:48 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 01:00:48 +0100 Subject: [Tutor] returning the entire line when regex matches References: <3a13c8e50905031342n4a914261wa9c106a0bb9bd757@mail.gmail.com> <3a13c8e50905040857v3529798bwbb15872f6334af45@mail.gmail.com><49FF29E8.4070904@mwalsh.org><3a13c8e50905041251re566f17oe6ee4927e7c366f3@mail.gmail.com> <3a13c8e50905051515l291ab4c8o214db70cb31d79ae@mail.gmail.com> Message-ID: "Nick Burgess" wrote > for root, dirs, files in os.walk('./'): > for f in files: > if f.endswith('.txt'): > allfiles.append(os.path.join(root, f)) myFunction( os.path.join(root,f) ) > This returns a list of the *.txt files. Of course now I am stuck on > how to apply the delimiter and search function to the items in the > list..? Either call your function on each file as you find it -- no need for a list, or... for f in allfiles: myFunction(f) Provided your function can take the filename as a parameter either method will work. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed May 6 02:02:27 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 01:02:27 +0100 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') References: <49FF60C5.4040707@gmx.net> <78b3a9580905051138i55719a1dwe3ed11722bf7b001@mail.gmail.com> Message-ID: "wesley chun" wrote > does anyone have the idiom for the above isinstance() check for 3.x? Test for io.stream (or a derivative) - which is what open now returns. files are no more... Alan G. From alan.gauld at btinternet.com Wed May 6 02:04:12 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 01:04:12 +0100 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') References: <49FF60C5.4040707@gmx.net> <7c64c2920905051435t5ef08dd3ndc3ee76a5e773af1@mail.gmail.com> Message-ID: "Larry Riedel" wrote > It may not be a good reason, but I like that "open" > is preferred, because in Java a "File" object is not > a stream for the content in the file, just its directory > entry, and simply creating a File object does not > open the file at all. I'm not sure citing anything Java does is a good rationale in Python! Hopefully we can do rather better than the horrors of Java :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From Larry at Riedel.org Wed May 6 02:24:31 2009 From: Larry at Riedel.org (Larry Riedel) Date: Tue, 5 May 2009 17:24:31 -0700 Subject: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r') In-Reply-To: References: <49FF60C5.4040707@gmx.net> <7c64c2920905051435t5ef08dd3ndc3ee76a5e773af1@mail.gmail.com> Message-ID: <7c64c2920905051724n335ad370l8944e63b96af46f6@mail.gmail.com> > I'm not sure citing anything Java does is a good rationale in Python! I find the languages/platforms complementary, so I appreciate when they are not arbitrarily inconsistent with each other. Larry From david at abbottdavid.com Wed May 6 04:45:10 2009 From: david at abbottdavid.com (David) Date: Tue, 05 May 2009 22:45:10 -0400 Subject: [Tutor] Reset while loop Message-ID: <4A00F9B6.40509@abbottdavid.com> Hi, not even sure if I can ask this very smartly but we shall try: I was playing around with trying to create a python program like tail -f as discussed on the list. i came up with this; def tail_it(bufsize=8192, linesep=__import__('os').linesep): f = None if f == None: f = open(tail_fname, 'rb') f.seek(0, 2) pos, tailsize = divmod(f.tell(), bufsize) if tailsize == 0: pos = max(0, pos-1) pos *= bufsize f.seek(pos) lines = f.read().split(linesep) x = len(lines) x = x-2 print lines[x:] f.close() while True: new_time = os.stat(tail_fname).st_mtime if new_time > old_time: f = open(tail_fname, 'rb') f.seek(0, 2) pos, tailsize = divmod(f.tell(), bufsize) if tailsize == 0: pos = max(0, pos-1) pos *= bufsize f.seek(pos) lines = f.read().split(linesep) x = len(lines) x = x-2 print lines[x:] time.sleep(sec_to_wait) f.close() [output] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons )', ''] ['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons )', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] [/output] That was with sec_to_wait set at 30 The problem I am having is it will print something whatever sec_to_wait is set to even if the file's st_mtime is the same. I want it to only prine when the st_mtime has changed. I think I need to update the while loop and make old_time = new-time and start over but I need some direction to get started, thanks -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Wed May 6 09:48:08 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 08:48:08 +0100 Subject: [Tutor] Reset while loop References: <4A00F9B6.40509@abbottdavid.com> Message-ID: "David" wrote > I was playing around with trying to create a python program like tail -f > as discussed on the list. i came up with this; Some general comments first: > def tail_it(bufsize=8192, linesep=__import__('os').linesep): No idea why you are using the import stuff, just use os.linesep > f = None > if f == None: This is redundant. You set f to None so the if test is always true, so you don't need it. > f = open(tail_fname, 'rb') Where is tail_fname defined? It would be sensible to make it an input parameter. > f.seek(0, 2) > pos, tailsize = divmod(f.tell(), bufsize) > if tailsize == 0: > pos = max(0, pos-1) > pos *= bufsize I got confused trying to work out exactly what this was doing. pos is initially the number of times bufsize fits in the file, but then gets modified to zero or one less than its current value. > f.seek(pos) > lines = f.read().split(linesep) > x = len(lines) > x = x-2 > print lines[x:] Could just be print lines[-2:] > f.close() All of the above is repeated inside your while loop so put it into a function and call the function inside the loop. > while True: > new_time = os.stat(tail_fname).st_mtime > if new_time > old_time: Where is old_time defined? You could call your new function here > f = open(tail_fname, 'rb') > f.seek(0, 2) > pos, tailsize = divmod(f.tell(), bufsize) > if tailsize == 0: > pos = max(0, pos-1) > pos *= bufsize > f.seek(pos) > lines = f.read().split(linesep) > x = len(lines) > x = x-2 > print lines[x:] > time.sleep(sec_to_wait) And call this after the function > f.close() > [output] > ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed > for user root', ''] > ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed > for user root', ''] > ['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x > /usr/sbin/run-crons && /usr/sbin/run-crons )', ''] > ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change > 4001', ''] > ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change > 4001', ''] > [/output] > > That was with sec_to_wait set at 30 > > The problem I am having is it will print something whatever sec_to_wait > is set to even if the file's st_mtime is the same. I want it to only > prine when the st_mtime has changed. I think I need to update the while > loop and make old_time = new-time and start over but I need some > direction to get started, thanks You need to set the old time somewhere, it never changes in your code. If you put the actual read/print code in a function (it would be better not to have the print in there, just return the string to be printed. Then your code would look like: import os def get_tail(fname, bufsize, linesep): f = open(fname, 'rb') f.seek(0, 2) pos, tailsize = divmod(f.tell(), bufsize) if tailsize == 0: pos = max(0, pos-1) pos *= bufsize f.seek(pos) lines = f.read().split(linesep) f.close() return lines[-2:] def tail_it(fname, bufsize=8192, linesep=os.linesep): while True: new_time = os.stat(tail_fname).st_mtime if new_time > old_time: time.sleep(sec_to_wait) print get_tail (fname, bufsize, linesep) old_time = new_time I haven't tried it but that should be close to what you want. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed May 6 15:05:27 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 14:05:27 +0100 Subject: [Tutor] Reset while loop References: <4A00F9B6.40509@abbottdavid.com> Message-ID: "Alan Gauld" wrote >> f.seek(0, 2) >> pos, tailsize = divmod(f.tell(), bufsize) >> if tailsize == 0: >> pos = max(0, pos-1) >> pos *= bufsize >> f.seek(pos) > > I got confused trying to work out exactly what this was doing. > pos is initially the number of times bufsize fits in the file, but > then gets modified to zero or one less than its current value. I had another look during a cup of coffee and what I think you are doing is positioning the cursor up to buffsize away from the end of the file? If so I think this is easier: f.seek(0, os.SEEK_END) # goto end of file offset = f.tell() % buffsize # trim off N buffers worth if offset == 0: # if no tail backup one buffers worth offset = buffsize f.seek(-offset, os.SEEK_END) # goto beginning of tail If I misunderstood I'm sure someone will say! :-) OTOH since you only use the last two lines why not miss all that out and go straight to where you want to be with: f.seek(-buffsize, os.SEEK_END) # goto one buffsize away from end return f.read().split(linesep)[-2:] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Wed May 6 22:28:30 2009 From: david at abbottdavid.com (David) Date: Wed, 06 May 2009 16:28:30 -0400 Subject: [Tutor] Reset while loop In-Reply-To: References: <4A00F9B6.40509@abbottdavid.com> Message-ID: <4A01F2EE.80800@abbottdavid.com> Alan Gauld wrote: > > "David" wrote > > >> I was playing around with trying to create a python program like tail >> -f as discussed on the list. i came up with this; Alan, thanks for your time and great tips :) > > import os > > def get_tail(fname, bufsize, linesep): > f = open(fname, 'rb') > f.seek(0, 2) > pos, tailsize = divmod(f.tell(), bufsize) > if tailsize == 0: > pos = max(0, pos-1) > pos *= bufsize > f.seek(pos) > lines = f.read().split(linesep) > f.close() > return lines[-2:] > > > def tail_it(fname, bufsize=8192, linesep=os.linesep): > while True: > new_time = os.stat(tail_fname).st_mtime > if new_time > old_time: > time.sleep(sec_to_wait) > print get_tail (fname, bufsize, linesep) > old_time = new_time > > I haven't tried it but that should be close to what you want. > > This helps a lot because I can now see how the loop works. One of my problems is putting the pieces of code in the right places. It helps me understand when I can run the program and see the errors and output to make adjustments. > OTOH since you only use the last two lines why not miss all > that out and go straight to where you want to be with: > > f.seek(-buffsize, os.SEEK_END) # goto one buffsize away from end > return f.read().split(linesep)[-2:] I like it! Here is what I came up with; #!/usr/bin/python "Python program similar to tail -f" import os import sys import commands import time sec_to_wait = 60 fname = raw_input('Enter the log file to be monitored: ') def usage(): """Explains the program to the user""" print \ "You will need to be root to monitor most log files." def check_whoami(): """Check if Root""" if commands.getoutput('whoami') != 'root': sys.exit('\nYou will need to be Root!\n') def print_log_end(fname, bufsize, linesep): f = open(fname, 'r') f.seek(-bufsize, os.SEEK_END) # goto one buffsize away from end log = f.read().split(linesep)[-8:] for i in log: print i f.close() def tail_it(fname, bufsize, linesep): old_time = os.stat(fname).st_mtime while True: new_time = os.stat(fname).st_mtime if new_time > old_time: time.sleep(sec_to_wait) updated = print_tail(fname, bufsize, linesep) for i in updated: print i old_time = new_time def print_tail(fname, bufsize, linesep): f = open(fname, 'r') f.seek(-bufsize, os.SEEK_END) # goto one buffsize away from end return f.read().split(linesep)[-2:] f.close() if __name__ == "__main__": print_log_end(fname, bufsize=8192, linesep=os.linesep) check_whoami() tail_it(fname, bufsize=8192, linesep=os.linesep) [output] Enter the log file to be monitored: /var/log/messages May 6 16:00:01 opteron cron[24679]: (root) CMD (rm -f /var/spool/cron/lastrun/cron.hourly) May 6 16:00:01 opteron run-crons[24685]: (root) CMD (/etc/cron.hourly/moodle) May 6 16:00:01 opteron run-crons[24688]: (root) CMD (/etc/cron.hourly/vnstat) May 6 16:03:06 opteron su[24712]: Successful su for root by david May 6 16:03:06 opteron su[24712]: + pts/6 david:root May 6 16:03:06 opteron su[24712]: pam_unix(su:session): session opened for user root by david(uid=1000) May 6 16:10:01 opteron cron[24751]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons ) May 6 16:20:01 opteron cron[24788]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons ) -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Thu May 7 00:40:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 May 2009 23:40:39 +0100 Subject: [Tutor] Reset while loop References: <4A00F9B6.40509@abbottdavid.com> <4A01F2EE.80800@abbottdavid.com> Message-ID: "David" wrote > Alan, thanks for your time and great tips :) You're welcome, I was in a hurry so wasn't sure I understood fully. > Here is what I came up with; > > def print_log_end(fname, bufsize, linesep): > f = open(fname, 'r') > f.seek(-bufsize, os.SEEK_END) # goto one buffsize away from end > log = f.read().split(linesep)[-8:] > for i in log: > print i > f.close() > > def print_tail(fname, bufsize, linesep): > f = open(fname, 'r') > f.seek(-bufsize, os.SEEK_END) # goto one buffsize away from end > return f.read().split(linesep)[-2:] > f.close() One of the key principles of programming is DRY: Don't Repeat Yourself. Where you see 2 functions with near identical code look to see how you can combine them or reuse one of them. In the case above if you make print_tail(*) take a numlines parameter you can reuse it in print_log_end. (*)But you shout rename it since it doewsn't print it returns! So call it get_tail() or similar... So the functions become: def print_log_end(fname, bufsize, linesep): for i in get_tail(fname, 8, bufsize, linesep): print i def print_tail(fname, numlines, bufsize, linesep): f = open(fname, 'r') f.seek(-bufsize, os.SEEK_END) # goto one buffsize away from end result = f.read().split(linesep)[-numlines:] f.close() return result Note: The return needs to be after the close since othewise the close won't get called explicitly - it is called implicitly by the file going out of scope at the end of the function - and its good practice to explicitly close the file HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Thu May 7 02:44:55 2009 From: david at abbottdavid.com (David) Date: Wed, 06 May 2009 20:44:55 -0400 Subject: [Tutor] Reset while loop In-Reply-To: References: <4A00F9B6.40509@abbottdavid.com> <4A01F2EE.80800@abbottdavid.com> Message-ID: <4A022F07.7050709@abbottdavid.com> When running the program cpu usage is 100%. Here is the part that is running; def tail_it(fname, bufsize, linesep): """Update if monitored file time changes""" old_time = os.stat(fname).st_mtime while True: new_time = os.stat(fname).st_mtime if new_time > old_time: time.sleep(sec_to_wait) updated = return_tail(fname, bufsize, linesep) for i in updated: print i old_time = new_time It is waiting here; new_time = os.stat(fname).st_mtime Why would something like that happen and is there any way to fix it? The program is just for me to learn Python, if I wanted to tail a log file I would just use tail, but an understanding may help in the future. thanks, -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From david at abbottdavid.com Thu May 7 05:04:42 2009 From: david at abbottdavid.com (David) Date: Wed, 06 May 2009 23:04:42 -0400 Subject: [Tutor] Reset while loop In-Reply-To: <4A0238E6.8080909@gmail.com> References: <4A00F9B6.40509@abbottdavid.com> <4A01F2EE.80800@abbottdavid.com> <4A022F07.7050709@abbottdavid.com> <4A0238E6.8080909@gmail.com> Message-ID: <4A024FCA.5090200@abbottdavid.com> bob gailer wrote: > > You call sleep only when the file is updated. As long as the file is not > updated the loop runs continuously and therefore 100%. > >> and is there any way to fix it? > > move the sleep call out of the if, and put the update of old_time in the if > > while True: > new_time = os.stat(fname).st_mtime > if new_time > old_time: > updated = return_tail(fname, bufsize, linesep) > for i in updated: > print i > old_time = new_time > time.sleep(sec_to_wait) Thank You Bob -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From kent37 at tds.net Thu May 7 12:42:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 7 May 2009 06:42:42 -0400 Subject: [Tutor] New beginner book - "Practical Programming" Message-ID: <1c2a2c590905070342jd4960f9xab7b37e871136fd1@mail.gmail.com> The Pragmatic Bookshelf has released a new book, Practical Programming: An Introduction to Computer Science Using Python. This looks like a good addition to the list of beginners' books. I would be interested in a review if anyone reads it. http://www.pragprog.com/titles/gwpy/practical-programming Kent From alan.gauld at btinternet.com Thu May 7 17:54:57 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 7 May 2009 16:54:57 +0100 Subject: [Tutor] New beginner book - "Practical Programming" References: <1c2a2c590905070342jd4960f9xab7b37e871136fd1@mail.gmail.com> Message-ID: "Kent Johnson" wrote in message news:1c2a2c590905070342jd4960f9xab7b37e871136fd1 at mail.gmail.com... > The Pragmatic Bookshelf has released a new book, Practical > Programming: An Introduction to Computer Science Using Python. This > looks like a good addition to the list of beginners' books. I would be > interested in a review if anyone reads it. > http://www.pragprog.com/titles/gwpy/practical-programming Based on the excerpt that I read online it looks pretty good. I couldn't see anything to carp about at all. Very impressive. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From expression1987 at comcast.net Fri May 8 01:21:03 2009 From: expression1987 at comcast.net (Christopher Barkley) Date: Thu, 7 May 2009 16:21:03 -0700 Subject: [Tutor] Need help with registry access Message-ID: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> Python 3.0.1 Windows XP PROFESSIONAL, SP3 Looking for help manipulating the registry through python. Heres what I'm wanting to do. My friend and I are techies, he's on my LAN. I want to disable his keyboard through the registry (or some other fun way) while he's in the middle of playing starcraft by running a script through the LAN. Easy fix, he can restore or just reinstall his keyboard. But I wanna see the look on his face. I'd like to do it through the registry, since that'd be easy to fix. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri May 8 02:20:32 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 8 May 2009 01:20:32 +0100 Subject: [Tutor] Need help with registry access References: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> Message-ID: "Christopher Barkley" wrote > My friend and I are techies, he's on my LAN. I want to disable > his keyboard through the registry (or some other fun way) > while he's in the middle of playing starcraft by running a > script through the LAN. Look at the winreg module for accessing the registry. How you do it over the LAN and how you disable the keyboard I'll leave as an exercise for your investigation! One other option is to look into using WSH via the pythonwin or ctypes API. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Fri May 8 07:22:57 2009 From: denis.spir at free.fr (spir) Date: Fri, 8 May 2009 07:22:57 +0200 Subject: [Tutor] Need help with registry access In-Reply-To: References: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> Message-ID: <20090508072257.1301930d@o> Le Fri, 8 May 2009 01:20:32 +0100, "Alan Gauld" s'exprima ainsi: > > "Christopher Barkley" wrote > > > My friend and I are techies, he's on my LAN. I want to disable > > his keyboard through the registry (or some other fun way) > > while he's in the middle of playing starcraft by running a > > script through the LAN. > Look at the winreg module for accessing the registry. > > How you do it over the LAN and how you disable the keyboard > I'll leave as an exercise for your investigation! > > One other option is to look into using WSH via the pythonwin > or ctypes API. > If he can do that, I'm really happy not to be a windozista! Denis ------ la vita e estrany From lie.1296 at gmail.com Fri May 8 08:49:42 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 08 May 2009 16:49:42 +1000 Subject: [Tutor] Need help with registry access In-Reply-To: <20090508072257.1301930d@o> References: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> <20090508072257.1301930d@o> Message-ID: spir wrote: > Le Fri, 8 May 2009 01:20:32 +0100, > "Alan Gauld" s'exprima ainsi: > >> "Christopher Barkley" wrote >> >>> My friend and I are techies, he's on my LAN. I want to disable >>> his keyboard through the registry (or some other fun way) >>> while he's in the middle of playing starcraft by running a >>> script through the LAN. > >> Look at the winreg module for accessing the registry. >> >> How you do it over the LAN and how you disable the keyboard >> I'll leave as an exercise for your investigation! >> >> One other option is to look into using WSH via the pythonwin >> or ctypes API. >> > > If he can do that, I'm really happy not to be a windozista! > It is trivial if he can get physical access to the computer and can install a software that will listen to a certain port to hear when to shut off the keyboard. Well, if you can get physical access to the computer, pretty much anything is possible... From alan.gauld at btinternet.com Fri May 8 10:07:13 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 8 May 2009 08:07:13 +0000 (GMT) Subject: [Tutor] Need help with registry access In-Reply-To: <000601c9cf92$c74ce510$9924b943@dbug> References: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> <000601c9cf92$c74ce510$9924b943@dbug> Message-ID: <894102.57583.qm@web86704.mail.ird.yahoo.com> Always use ReplyAll to include the mailing list. The _winreg module is part of the standard library and provides functions for accessing the Windows registry. But you need to learn enough about Python to be able to use modules and functions. You will also need to know enough about Windows internals to manage the keyboard. I suggest you start with a beginners python tutorial. If you have never programmed before then look at the non programmers list (or just try mine), if you have programmed in another language try the official tutorial http://wiki.python.org/moin/BeginnersGuide/NonProgrammers http://docs.python.org/tutorial/ Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ----- Original Message ---- > From: Christopher Barkley > To: Alan Gauld > Sent: Friday, 8 May, 2009 5:09:25 AM > Subject: Re: [Tutor] Need help with registry access > > no idea what any of that means. Total newb. Examples would help. > ----- Original Message ----- From: "Alan Gauld" > To: > Sent: Thursday, May 07, 2009 5:20 PM > Subject: Re: [Tutor] Need help with registry access > > > > > > "Christopher Barkley" wrote > >> My friend and I are techies, he's on my LAN. I want to disable his keyboard > through the registry (or some other fun way) while he's in the middle of playing > starcraft by running a script through the LAN. > > > > Look at the winreg module for accessing the registry. > > > > How you do it over the LAN and how you disable the keyboard I'll leave as an > exercise for your investigation! > > > > One other option is to look into using WSH via the pythonwin or ctypes API. > > > > -- Alan Gauld > > Author of the Learn to Program web site > > http://www.alan-g.me.uk/ > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > From mail at timgolden.me.uk Fri May 8 10:12:40 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 08 May 2009 09:12:40 +0100 Subject: [Tutor] Need help with registry access In-Reply-To: <894102.57583.qm@web86704.mail.ird.yahoo.com> References: <000601c9cf6a$7e4a6ea0$2a00a8c0@dbug> <000601c9cf92$c74ce510$9924b943@dbug> <894102.57583.qm@web86704.mail.ird.yahoo.com> Message-ID: <4A03E978.8010602@timgolden.me.uk> ALAN GAULD wrote: > The _winreg module is part of the standard library and provides > functions for accessing the Windows registry. There's some (incomplete) help here, but as Alan said, you need to learn Python to some extent before trying to do this. http://timgolden.me.uk/python-on-windows/programming-areas/registry.html TJG From python at bdurham.com Fri May 8 19:03:47 2009 From: python at bdurham.com (python at bdurham.com) Date: Fri, 08 May 2009 13:03:47 -0400 Subject: [Tutor] Pythonic way to normalize vertical whitespace Message-ID: <1241802227.24180.1314501473@webmail.messagingengine.com> Note: Following cross-posted to python-list where it got queued due to suspicious subject line. I'm looking for suggestions on technique (not necessarily code) about the most pythonic way to normalize vertical whitespace in blocks of text so that there is never more than 1 blank line between paragraphs. Our source text has newlines normalized to single newlines (\n vs. combinations of \r and \n), but there may be leading and trailing whitespace around each newline. Approaches: 1. split text to list of lines that get stripped then: a. walk this list building a new list of lines that track and ignore extra blank lines -OR- b. re-join lines and replace '\n\n\n' wth' \n\n' until no more '\n\n\n' matches exist 2. use regular expressions to match and replace whitespace pattern of 3 or more adjacent \n's with surrounding whitespace 3. a 3rd party text processing library designed for efficiently cleaning up text Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From msh at blisses.org Fri May 8 19:04:59 2009 From: msh at blisses.org (Matt Herzog) Date: Fri, 8 May 2009 13:04:59 -0400 Subject: [Tutor] glob paramiko Message-ID: <20090508170459.GB24903@chicago.blisses.org> Hey All. All I need to do in this script is scp or sftp a bunch of files to a remote server. This script will run from a cron job eventually. For whatever reason, paramiko can't cope with a list. --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/msh/ in () AttributeError: 'NoneType' object has no attribute 'put' In [18]: type localpath -------> type(localpath) Out[18]: In [19]: for each in localpath ------------------------------------------------------------ ------ script begins ------------ import paramiko import glob import os paramiko.util.log_to_file('/tmp/paramiko.log') host = "sftp.okcomputer.yo" port = 22 transport = paramiko.Transport((host, port)) password = "T0pS3kr1t!" username = "schizznits" transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport) os.chdir("/home/fatcat/") filepath = '/' localpath = glob.glob("*.tab") sftp.put(localpath, filepath) sftp.close() transport.close() --- script ends ------ -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From kent37 at tds.net Fri May 8 20:30:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 8 May 2009 14:30:22 -0400 Subject: [Tutor] glob paramiko In-Reply-To: <20090508170459.GB24903@chicago.blisses.org> References: <20090508170459.GB24903@chicago.blisses.org> Message-ID: <1c2a2c590905081130s5679549fg6b1ed4b612bf4a29@mail.gmail.com> On Fri, May 8, 2009 at 1:04 PM, Matt Herzog wrote: > Hey All. > > All I need to do in this script is scp or sftp a bunch of files to a remote server. This script will run from a cron job eventually. > > For whatever reason, paramiko can't cope with a list. > > --------------------------------------------------------------------------- > AttributeError ? ? ? ? ? ? ? ? ? ? ? ? ? ?Traceback (most recent call last) > > /Users/msh/ in () > > AttributeError: 'NoneType' object has no attribute 'put' It would be helpful to see the full stack trace. > sftp = paramiko.SFTPClient.from_transport(transport) > > os.chdir("/home/fatcat/") > filepath = '/' > localpath = glob.glob("*.tab") > sftp.put(localpath, filepath) It looks like sftp is None. That is why it has no attribute "put". Kent From msh at blisses.org Fri May 8 20:56:35 2009 From: msh at blisses.org (Matt Herzog) Date: Fri, 8 May 2009 14:56:35 -0400 Subject: [Tutor] glob paramiko In-Reply-To: <1c2a2c590905081130s5679549fg6b1ed4b612bf4a29@mail.gmail.com> References: <20090508170459.GB24903@chicago.blisses.org> <1c2a2c590905081130s5679549fg6b1ed4b612bf4a29@mail.gmail.com> Message-ID: <20090508185635.GA27344@chicago.blisses.org> On Fri, May 08, 2009 at 02:30:22PM -0400, Kent Johnson wrote: > On Fri, May 8, 2009 at 1:04 PM, Matt Herzog wrote: > > Hey All. > > > > All I need to do in this script is scp or sftp a bunch of files to a remote server. This script will run from a cron job eventually. > > > > For whatever reason, paramiko can't cope with a list. > > > > --------------------------------------------------------------------------- > > AttributeError ? ? ? ? ? ? ? ? ? ? ? ? ? ?Traceback (most recent call last) > > > > /Users/msh/ in () > > > > AttributeError: 'NoneType' object has no attribute 'put' > > It would be helpful to see the full stack trace. Here it is, unless I can give you something more verbose. Not sure how to produce "full stack trace." Do you mean this: http://docs.python.org/library/traceback.html ? (15)[datasvcs at iggy ~/AIG]$ ./parameek0.py Traceback (most recent call last): File "./parameek0.py", line 24, in ? sftp.put(localpath, filepath) File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 547, in put TypeError: coercing to Unicode: need string or buffer, list found > > > sftp = paramiko.SFTPClient.from_transport(transport) > > > > os.chdir("/home/fatcat/") > > filepath = '/' > > localpath = glob.glob("*.tab") > > sftp.put(localpath, filepath) > > It looks like sftp is None. That is why it has no attribute "put". Yeah, it shows up as a type list. I haven't been doing any python thinking in a couple months. The script works without the glob syntax on a single file defined as: localpath = '/home/foo/bar.txt' Thanks for helping. > > Kent -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From kent37 at tds.net Fri May 8 21:26:07 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 8 May 2009 15:26:07 -0400 Subject: [Tutor] glob paramiko In-Reply-To: <20090508185635.GA27344@chicago.blisses.org> References: <20090508170459.GB24903@chicago.blisses.org> <1c2a2c590905081130s5679549fg6b1ed4b612bf4a29@mail.gmail.com> <20090508185635.GA27344@chicago.blisses.org> Message-ID: <1c2a2c590905081226y5bab8e21j7e3659680c161a8b@mail.gmail.com> On Fri, May 8, 2009 at 2:56 PM, Matt Herzog wrote: > On Fri, May 08, 2009 at 02:30:22PM -0400, Kent Johnson wrote: >> On Fri, May 8, 2009 at 1:04 PM, Matt Herzog wrote: >> > Hey All. >> > >> > All I need to do in this script is scp or sftp a bunch of files to a remote server. This script will run from a cron job eventually. >> > >> > For whatever reason, paramiko can't cope with a list. >> > >> > --------------------------------------------------------------------------- >> > AttributeError ? ? ? ? ? ? ? ? ? ? ? ? ? ?Traceback (most recent call last) >> > >> > /Users/msh/ in () >> > >> > AttributeError: 'NoneType' object has no attribute 'put' >> >> It would be helpful to see the full stack trace. > > Here it is, unless I can give you something more verbose. Not sure how to produce "full stack trace." > Do you mean this: http://docs.python.org/library/traceback.html ? > > (15)[datasvcs at iggy ~/AIG]$ ./parameek0.py > Traceback (most recent call last): > ?File "./parameek0.py", line 24, in ? > ? ? ?sftp.put(localpath, filepath) > ? ? ? ?File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 547, in put > ? ? ? ?TypeError: coercing to Unicode: need string or buffer, list found That's what I was asking for. Note that this is completely different from the first error you posted! It is telling you the problem, it just doesn't support a list as the first argument. And in fact there is nothing in the docs to indicate that it should work with a list. Just put the put() call in a loop and do each file separately. > >> >> > sftp = paramiko.SFTPClient.from_transport(transport) >> > >> > os.chdir("/home/fatcat/") >> > filepath = '/' >> > localpath = glob.glob("*.tab") >> > sftp.put(localpath, filepath) >> >> It looks like sftp is None. That is why it has no attribute "put". > > Yeah, it shows up as a type list. I haven't been doing any python thinking in a couple months. No, localpath is a list. Kent From bgailer at gmail.com Fri May 8 22:43:02 2009 From: bgailer at gmail.com (bob gailer) Date: Fri, 08 May 2009 16:43:02 -0400 Subject: [Tutor] Pythonic way to normalize vertical whitespace In-Reply-To: <1241802227.24180.1314501473@webmail.messagingengine.com> References: <1241802227.24180.1314501473@webmail.messagingengine.com> Message-ID: <4A049956.3040900@gmail.com> python at bdurham.com wrote: > Note: Following cross-posted to python-list where it got queued due to > suspicious subject line. > > I'm looking for suggestions on technique (not necessarily code) about > the most pythonic way to normalize vertical whitespace in blocks of > text so that there is never more than 1 blank line between paragraphs. > Our source text has newlines normalized to single newlines (\n vs. > combinations of \r and \n), but there may be leading and trailing > whitespace around each newline. I can't follow that! Please provide a before and after example. > > Approaches: > > 1. split text to list of lines that get stripped then: > > a. walk this list building a new list of lines that track and ignore > extra blank lines > > -OR- > > b. re-join lines and replace '\n\n\n' wth' \n\n' until no more > '\n\n\n' matches exist > > 2. use regular expressions to match and replace whitespace pattern of > 3 or more adjacent \n's with surrounding whitespace > > 3. a 3rd party text processing library designed for efficiently > cleaning up text > > Thanks! > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From carroll at tjc.com Sat May 9 00:37:04 2009 From: carroll at tjc.com (Terry Carroll) Date: Fri, 8 May 2009 15:37:04 -0700 (PDT) Subject: [Tutor] Optparse question: if only certain values are acceptable Message-ID: I'm tryng to use optparse for the first time. The toy summary is that I want to have the following command format: prognam -f FORMAT Where FORMAT, if specified, must be one of "X", "Y", or "Z". In otherwords, if the user enters: progname -f X It runs, producing its output in format X. Similar if "Y" or "Z" is specified instead of "X". But if the user specifies progname -f A I want it to spit up because A is not a recognized format. I don't see anything in the docs that directly addresses this. Is this something that I should use the callback parameter for? From sander.sweers at gmail.com Sat May 9 02:41:49 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 9 May 2009 02:41:49 +0200 Subject: [Tutor] Optparse question: if only certain values are acceptable In-Reply-To: References: Message-ID: 2009/5/9 Terry Carroll : > In otherwords, if the user enters: > > ?progname -f X > > It runs, producing its output in format X. ?Similar if "Y" or "Z" is > specified instead of "X". > > But if the user specifies > > ?progname -f A > > I want it to spit up because A is not a recognized format. Is the below what you are looking for? >>> import optparse >>> parser = optparse.OptionParser() >>> parser.add_option('-f', '--format', type='choice', action='store', choices=('x','y','z'), dest='format') >>> args = ['-f', 'd'] #Wrong format d >>> options, restargs = parser.parse_args(args) Usage: [options] : error: option -f: invalid choice: 'd' (choose from 'x', 'y', 'z') Traceback (most recent call last): File "", line 1, in parser.parse_args(args) File "C:\Python26\lib\optparse.py", line 1382, in parse_args self.error(str(err)) File "C:\Python26\lib\optparse.py", line 1564, in error self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg)) File "C:\Python26\lib\optparse.py", line 1554, in exit sys.exit(status) SystemExit: 2 >>> args = ['-f', 'x'] #Correct format x >>> options, restargs = parser.parse_args(args) >>> options.format 'x' Greets Sander From carroll at tjc.com Sat May 9 03:09:54 2009 From: carroll at tjc.com (Terry Carroll) Date: Fri, 8 May 2009 18:09:54 -0700 (PDT) Subject: [Tutor] Optparse question: if only certain values are acceptable In-Reply-To: Message-ID: On Sat, 9 May 2009, Sander Sweers wrote: > Is the below what you are looking for? It's exactly what I was looking for. Thanks very much. Now I'm going to have to re-read the docs and see why I couldn't pick that up from them. From kent37 at tds.net Sat May 9 03:51:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 8 May 2009 21:51:43 -0400 Subject: [Tutor] glob paramiko In-Reply-To: <20090508195055.GC27344@chicago.blisses.org> References: <20090508170459.GB24903@chicago.blisses.org> <1c2a2c590905081130s5679549fg6b1ed4b612bf4a29@mail.gmail.com> <20090508185635.GA27344@chicago.blisses.org> <1c2a2c590905081226y5bab8e21j7e3659680c161a8b@mail.gmail.com> <20090508195055.GC27344@chicago.blisses.org> Message-ID: <1c2a2c590905081851n17a8bc25t69ac7bf3501f613@mail.gmail.com> On Fri, May 8, 2009 at 3:50 PM, Matt Herzog wrote: > localpath = glob.glob("*.tab") > > for blob in localpath: > ? ? ? ?sftp.put(blob, filepath) > sftp.close() > transport.close() > > This fails. I have forgotten most of what I knew, I guess. How does it fail? Again, the full traceback and error message are very helpful. Kent PS Please use Reply All to reply to the list. From kent37 at tds.net Sat May 9 03:54:48 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 8 May 2009 21:54:48 -0400 Subject: [Tutor] Pythonic way to normalize vertical whitespace In-Reply-To: <1241802227.24180.1314501473@webmail.messagingengine.com> References: <1241802227.24180.1314501473@webmail.messagingengine.com> Message-ID: <1c2a2c590905081854x4a69e895h864b2d9a581c468f@mail.gmail.com> On Fri, May 8, 2009 at 1:03 PM, wrote: > Note: Following cross-posted to python-list where it got queued due to > suspicious subject line. > > I'm looking for suggestions on technique (not necessarily code) about the > most pythonic way to normalize vertical whitespace in blocks of text so that > there is never more than 1 blank line between paragraphs. Our source text > has newlines normalized to single newlines (\n vs. combinations of \r and > \n), but there may be leading and trailing whitespace around each newline. > > Approaches: > > 1. split text to list of lines that get stripped then: > > a. walk this list building a new list of lines that track and ignore extra > blank lines > > -OR- > > b. re-join lines and replace '\n\n\n' wth' \n\n' until no more '\n\n\n' > matches exist > > 2. use regular expressions to match and replace whitespace pattern of 3 or > more adjacent \n's with surrounding whitespace > > 3. a 3rd party text processing library designed for efficiently cleaning up > text 1 sounds simple enough. That is the first thing I thought of. 2 should also be easy if you have any regex-fu and might turn out to be faster if you have a lot of text. Kent From alan.gauld at btinternet.com Sat May 9 10:10:46 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 May 2009 09:10:46 +0100 Subject: [Tutor] Optparse question: if only certain values are acceptable References: Message-ID: "Terry Carroll" wrote > The toy summary is that I want to have the following command format: > > prognam -f FORMAT > > Where FORMAT, if specified, must be one of "X", "Y", or "Z". > > In otherwords, if the user enters: > > progname -f X > > It runs, producing its output in format X. Similar if "Y" or "Z" is > specified instead of "X". > > But if the user specifies > > progname -f A > > I want it to spit up because A is not a recognized format. > > I don't see anything in the docs that directly addresses this. Is this > something that I should use the callback parameter for? Which part do you not understand or do you believe is not addresssed? optparse can certainly allow you to specify the -f option and store the user entered value. Your code will have to act accordingly based on the value, optparse won't do that bit. And for the invalid option value I think you can handle that too, in an else clause. Thats the easiest way to use it IMHO. You can define a callback for your option but with only one option I dont see much point, you still need the value if/else inside the callback. You can define the list of values too (via choice) but again catching anything other than X,Y,Z is just as easy using an else clause in your case. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Sat May 9 12:41:25 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 9 May 2009 06:41:25 -0400 Subject: [Tutor] Optparse question: if only certain values are acceptable In-Reply-To: References: Message-ID: <1c2a2c590905090341h6af87011k15a0c4cf9e29b1dc@mail.gmail.com> On Fri, May 8, 2009 at 9:09 PM, Terry Carroll wrote: > It's exactly what I was looking for. ?Thanks very much. > > Now I'm going to have to re-read the docs and see why I couldn't pick that > up from them. You have to read pretty far down, to the sections on Option attributes and Standard Option Types http://docs.python.org/library/optparse.html#option-attributes http://docs.python.org/library/optparse.html#standard-option-types Kent From denis.spir at free.fr Sat May 9 12:51:49 2009 From: denis.spir at free.fr (spir) Date: Sat, 9 May 2009 12:51:49 +0200 Subject: [Tutor] Pythonic way to normalize vertical whitespace In-Reply-To: <1241802227.24180.1314501473@webmail.messagingengine.com> References: <1241802227.24180.1314501473@webmail.messagingengine.com> Message-ID: <20090509125149.6c23b761@o> Le Fri, 08 May 2009 13:03:47 -0400, python at bdurham.com s'exprima ainsi: [...] > Approaches: > 1. split text to list of lines that get stripped then: > a. walk this list building a new list of lines that track and > ignore extra blank lines > -OR- > b. re-join lines and replace '\n\n\n' wth' \n\n' until no more > '\n\n\n' matches exist > 2. use regular expressions to match and replace whitespace > pattern of 3 or more adjacent \n's with surrounding whitespace > 3. a 3rd party text processing library designed for efficiently > cleaning up text > Thanks! You should try 1a and 1b if only to have written the code at least once ;-) The set of python string methods is very complete and practicle. Still, in this case, 2 is rather straightforward and simple. If you cannot figure out the proper pattern, have a look at http://www.amk.ca/python/howto/regex/ and/or ask again the list. Denis ------ la vita e estrany From david at abbottdavid.com Sat May 9 19:54:23 2009 From: david at abbottdavid.com (David) Date: Sat, 09 May 2009 13:54:23 -0400 Subject: [Tutor] Code Dosent work. In-Reply-To: References: <49FE47AB.3050208@abbottdavid.com> Message-ID: <4A05C34F.4010908@abbottdavid.com> Jacob Mansfield wrote: > not sure why i was using pygame, anyway the problem is with the saveing, > press exit, the program will save to databox.txt, then start the program > again giveing it the filename that it saved to, then look at the records. Post the code you have now and I am sure someone can help point you in the right direction. -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From msh at blisses.org Sat May 9 23:11:28 2009 From: msh at blisses.org (Matt Herzog) Date: Sat, 9 May 2009 17:11:28 -0400 Subject: [Tutor] paramiko again Message-ID: <20090509211128.GD27344@chicago.blisses.org> Hello again. This code comes straight from the http://oreilly.com/catalog/9780596515829/ book. The only actual code I changed was s/get/put on the second to last line. The author says I ought to be able to do this and have it Just Work. There are several things I don't understand. Would be nice if the books' author was on this list so I could ask him directly. Heh. The code runs but does not upload any files. I would rather be specifying the local dir on the source machine rather than path on the destination. 1. How can paramiko know whether the dir_path is on the local vs remote system? 2. I can't find sftp.put or sftp.get in ipython. Are they part of paramiko? If not, where do they come from? Thanks for any insight. ------------------- begin -------------------------- #!/usr/bin/env python import paramiko import os hostname = '192.168.1.15' port = 22 username = 'revjimjones' password = 'C0ol4id3' dir_path = '/home/revjimjones/logs' if __name__ == "__main__": t = paramiko.Transport((hostname, port)) t.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(t) files = sftp.listdir(dir_path) for f in files: print 'Uploading', f sftp.put(os.path.join(dir_path, f), f) t.close() -------------------- end --------------------------- -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From david at abbottdavid.com Sun May 10 00:27:28 2009 From: david at abbottdavid.com (David) Date: Sat, 09 May 2009 18:27:28 -0400 Subject: [Tutor] paramiko again In-Reply-To: <20090509211128.GD27344@chicago.blisses.org> References: <20090509211128.GD27344@chicago.blisses.org> Message-ID: <4A060350.3050201@abbottdavid.com> Matt Herzog wrote: > Hello again. > > This code comes straight from the http://oreilly.com/catalog/9780596515829/ book. > > The only actual code I changed was s/get/put on the second to last line. The author says I ought to be able to do this and have it Just Work. There are several things I don't understand. Would be nice if the books' author was on this list so I could ask him directly. Heh. > > The code runs but does not upload any files. I would rather be specifying the local dir on the source machine rather than path on the destination. > > 1. How can paramiko know whether the dir_path is on the local vs remote system? > 2. I can't find sftp.put or sftp.get in ipython. Are they part of paramiko? If not, where do they come from? > > Thanks for any insight. > > ------------------- begin -------------------------- > #!/usr/bin/env python > import paramiko > import os > hostname = '192.168.1.15' > port = 22 > username = 'revjimjones' > password = 'C0ol4id3' > dir_path = '/home/revjimjones/logs' > if __name__ == "__main__": > t = paramiko.Transport((hostname, port)) > t.connect(username=username, password=password) > sftp = paramiko.SFTPClient.from_transport(t) > files = sftp.listdir(dir_path) > for f in files: > print 'Uploading', f > sftp.put(os.path.join(dir_path, f), f) > t.close() > > -------------------- end --------------------------- > > Your going to have to read up on it. Download the source here; http://github.com/robey/paramiko/downloads Then look at the demos folder where you will see an example for sftp like; # now, connect and use paramiko Transport to negotiate SSH2 across the connection try: t = paramiko.Transport((hostname, port)) t.connect(username=username, password=password, hostkey=hostkey) sftp = paramiko.SFTPClient.from_transport(t) From gdoghomes at gmail.com Sun May 10 01:23:02 2009 From: gdoghomes at gmail.com (Michael Morrissey) Date: Sun, 10 May 2009 06:23:02 +0700 Subject: [Tutor] Parsing Question Message-ID: Forgive me if I'm asking something stupid, but I'm not sure how to do this (there are so many options in Python, I'm overwhelmed and confused). I have a text file that looks like this: 1 the 126 name 2 of 127 very 3 to 128 through 4 and 129 just 5 a 130 form 6 in 131 much 7 is 132 great etc... Each has 2 numbers and 2 words on it. Each number is related to the word that comes after it. So "1" and "the" are connected (kinda like a dictionary), and "126" and "name" are related. Using the above text file as an input, I'm trying to make an output that lists all the words, one word per line, but none of the numbers. I was hoping to get the words listed in order of their related numbers. So, the output needs to look like: the of to .... (skip to the 127th line) very through just I can open and loop over the text, I'm just not sure what commands I should use to parse each line. Can anyone give me suggestions or examples? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pjsjoca at yahoo.com Sun May 10 01:42:29 2009 From: pjsjoca at yahoo.com (robert mcquirt) Date: Sat, 9 May 2009 16:42:29 -0700 (PDT) Subject: [Tutor] Different Command Result Executing in Shell vs. Program Message-ID: <791836.30463.qm@web53905.mail.re2.yahoo.com> Hi. I've not been working with Python very long and have run into a puzzling thing. I'm working on a program that needs to identify the filetype of files without extensions. Using the file command in the shell works fantastic, as in: robert at ubuntu:~$ file -b linuxlogotag JPEG image data, JFIF standard 1.01 I want to be able to perform the same task in a program for batch processing. After a bit of research, I found that I can execute the shell command from within a Python program using: import os os.system('file -b /home/robert/linuxlogotag') This also works fine. The problem is that when I try to replace the hard coded path/name with a string variable for looping, the results are not the same as the shell's. Here's the modified code I'm using for testing (the original code is from Bogdan's blog at http://bogdan.org.ua/). import os, glob path = '/home/robert' for infile in glob.glob( os.path.join(path, '*.*') ): ?testCommand = "'file -b " + infile + "'" ?print testCommand, ?test = os.system(testCommand) ?print test The code does indeed step through all the files in the directory and the testCommand string constructs properly. Yet, the output is not the same as shell results. All files, regardless of type, simply output the number 32512, with no description. I have searched forums, read the man file and perused several Python references and have not been able to find the answer. Why does the same command yield different results, depending on how it's run? Any education is greatly appreciated. Linux 2.6 (kubuntu 8.04) Python 2.5.2 file command 4.21 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 10 01:53:49 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 00:53:49 +0100 Subject: [Tutor] paramiko again References: <20090509211128.GD27344@chicago.blisses.org> Message-ID: "Matt Herzog" wrote > This code comes straight from the > http://oreilly.com/catalog/9780596515829/ book. > > The only actual code I changed was s/get/put on the second to last line. > The author says I ought to be able to do this and have it Just Work. > There are several things I don't understand. What are they? You raise a few issues below but are they all? > The code runs but does not upload any files. How do you know? Where are you looking for them? What are you looking for? > I would rather be specifying the local dir on the source machine > rather than path on the destination. Have you used normal ftp in its command line version? The put command specifies the location on the remote machine where you want to store the files. This is normal ftp behaviour. > 1. How can paramiko know whether the dir_path is on the > local vs remote system? It always expects it to be on the remote machine I guess. > 2. I can't find sftp.put or sftp.get in ipython. > Are they part of paramiko? Yes. sftp is an object that you create with a call from paramiko. The class exists in paramiko and you create an instance of it called sftp. sftp = paramiko.SFTPClient.from_transport(t) > If not, where do they come from? So get./put will be methods of whatever class SFTPClient.from_transport() returns an instance of. > ------------------- begin -------------------------- > #!/usr/bin/env python > import paramiko > import os > hostname = '192.168.1.15' > port = 22 > username = 'revjimjones' > password = 'C0ol4id3' > dir_path = '/home/revjimjones/logs' > if __name__ == "__main__": > t = paramiko.Transport((hostname, port)) > t.connect(username=username, password=password) > sftp = paramiko.SFTPClient.from_transport(t) > files = sftp.listdir(dir_path) This gets a list of the files from dir_path on the remote machine. > for f in files: > print 'Uploading', f > sftp.put(os.path.join(dir_path, f), f) So you are trying to upload files that currently exist on the remote machine from your machine, that is unlikely to work. It would only work if you happen to have files of exactly the same name (updated versions maybe?). In that case you will overwrite the remote server versions with your local copy. I suspect you probably need to change the listdir() call to use os.listdir(local_path) instead of stftp.listdir(). Where local_path is wherever you store the files on the local mnachine that you want to transfer. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Sun May 10 02:25:13 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 09 May 2009 17:25:13 -0700 Subject: [Tutor] Parsing Question In-Reply-To: References: Message-ID: On 5/9/2009 4:23 PM Michael Morrissey said... > Forgive me if I'm asking something stupid, but I'm not sure how to do > this (there are so many options in Python, I'm overwhelmed and > confused). Look into the split method of string objects (which are what you should be reading from the file), then look into indexing the resulting list objects. That should focus your attentions... Emile > I have a text file that looks like this: > > 1 the 126 name > 2 of 127 very > 3 to 128 through > 4 and 129 just > 5 a 130 form > 6 in 131 much > 7 is 132 great > etc... > > Each has 2 numbers and 2 words on it. Each number is related to the word > that comes after it. So "1" and "the" are connected (kinda like a > dictionary), and "126" and "name" are related. > > Using the above text file as an input, I'm trying to make an output that > lists all the words, one word per line, but none of the numbers. I was > hoping to get the words listed in order of their related numbers. So, > the output needs to look like: > > the > of > to > .... (skip to the 127th line) > very > through > just > > I can open and loop over the text, I'm just not sure what commands I > should use to parse each line. Can anyone give me suggestions or examples? > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sun May 10 02:30:04 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 01:30:04 +0100 Subject: [Tutor] Parsing Question References: Message-ID: "Michael Morrissey" wrote > a text file that looks like this: > 1 the 126 name > 2 of 127 very > > Each has 2 numbers and 2 words on it. Each number is related to the word > that comes after it. So "1" and "the" are connected (kinda like a > dictionary), and "126" and "name" are related. > > Using the above text file as an input, I'm trying to make an output that > lists all the words, one word per line, but none of the numbers. I was > hoping to get the words listed in order of their related numbers. So, the > > I can open and loop over the text, I'm just not sure what commands I > should > use to parse each line. Can anyone give me suggestions or examples? For this a simple split() command should give you a list of 4 items. I suggest you store each pair in a tuple (num, word) and put the tuples in a list. You can then sort the list and the tuples will be in numeric order (element 0). Then iterate over the list printing the word (element 1 of the tuple) In pseudo code: words = [] for line in open(fname): four = line.split() words.append( (four[0],four[1]) ) words.append( (four[2],four[3]) ) for word in sorted(words): print word[1] You should probably be able to do the first for loop as a list comprehension, but I can't think of how to get the split() call embedded into it right now! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From msh at blisses.org Sun May 10 02:32:19 2009 From: msh at blisses.org (Matt Herzog) Date: Sat, 9 May 2009 20:32:19 -0400 Subject: [Tutor] paramiko again In-Reply-To: References: <20090509211128.GD27344@chicago.blisses.org> Message-ID: <20090510003219.GG27344@chicago.blisses.org> On Sun, May 10, 2009 at 12:53:49AM +0100, Alan Gauld wrote: > Have you used normal ftp in its command line version? > The put command specifies the location on the remote machine > where you want to store the files. This is normal ftp behaviour. The server supports only sftp. Yeah, I could turn on ftp on some server but that would be more work. Heh. > Yes. sftp is an object that you create with a call from paramiko. > The class exists in paramiko and you create an instance of it > called sftp. Oh. I could call it anything, not that I'd want to. Thanks. I kept thinking I could find it using ipython. > So you are trying to upload files that currently exist on the > remote machine from your machine, that is unlikely to work. Yes. That was not the plan though. Heh. > I suspect you probably need to change the listdir() call to use > os.listdir(local_path) instead of stftp.listdir(). Where local_path > is wherever you store the files on the local mnachine that you > want to transfer. Yes, that is exactly what I wanted, os.listdir. But now I have another issue: specifying the remote dir. When I login to the sftp server and type pwd: sftp> pwd Remote working directory: / Is what I see. Also, I don't think I want "join" in the below, do I? Why do I want to "join" the two dirs? As far as I'm concerned, I just want to dump files into the only dir I can reach, which is "/" on the remote host's jail. sftp.get(os.path.join(dir_path, f), f) > > HTH, It did help. Many thanks! > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From alan.gauld at btinternet.com Sun May 10 02:36:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 01:36:21 +0100 Subject: [Tutor] Different Command Result Executing in Shell vs. Program References: <791836.30463.qm@web53905.mail.re2.yahoo.com> Message-ID: "robert mcquirt" wrote > import os > os.system('file -b /home/robert/linuxlogotag') > > This also works fine. The problem is that when I try to replace > the hard coded path/name with a string variable for looping, > the results are not the same as the shell's. > import os, glob > path = '/home/robert' > for infile in glob.glob( os.path.join(path, '*.*') ): > testCommand = "'file -b " + infile + "'" > print testCommand, > test = os.system(testCommand) The problem is that the return code from os.system is the exit code of the command being executed - usually 0 for success or an error code. What you want is to actually capture the output of the command. To do that you need to look at the subprocess module which provides several examples of what you want. > output is not the same as shell results. All files, regardless > of type, simply output the number 32512, with no description. That is the exit code of the file -b command. You would need to look at the file documentation to find out what it signifies! PS. You can also find some simple examples using subprocess in the Using the OS topic of my tutorial. HTH, Alan G. From alex.feddor at gmail.com Sat May 9 22:26:46 2009 From: alex.feddor at gmail.com (Alex Feddor) Date: Sat, 9 May 2009 22:26:46 +0200 Subject: [Tutor] Triggering code on 1 minute intervale .. Message-ID: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> .. What will be the best solution to trigger python code every minute as soon as PC in on. Cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Sun May 10 04:40:09 2009 From: srilyk at gmail.com (W W) Date: Sat, 9 May 2009 21:40:09 -0500 Subject: [Tutor] Triggering code on 1 minute intervale .. In-Reply-To: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> References: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> Message-ID: <333efb450905091940w16632e34p8e219177eb3e9207@mail.gmail.com> On Sat, May 9, 2009 at 3:26 PM, Alex Feddor wrote: > .. What will be the best solution to trigger python code every minute as > soon as PC in on. > an autorun script... and in your script use the time.sleep(60) I believe. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sun May 10 04:41:34 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 09 May 2009 22:41:34 -0400 Subject: [Tutor] Different Command Result Executing in Shell vs. Program In-Reply-To: References: Message-ID: <4A063EDE.4030109@ieee.org> robert mcquirt wrote: > Hi. I've not been working with Python very long and have run into a puzzling thing. I'm working on a program that needs to identify the filetype of files without extensions. Using the file command in the shell works fantastic, as in: > > > robert at ubuntu:~$ file -b linuxlogotag > JPEG image data, JFIF standard 1.01 > > > I want to be able to perform the same task in a program for batch processing. After a bit of research, I found that I can execute the shell command from within a Python program using: > > > import os > os.system('file -b /home/robert/linuxlogotag') > > > This also works fine. The problem is that when I try to replace the hard coded path/name with a string variable for looping, the results are not the same as the shell's. Here's the modified code I'm using for testing (the original code is from Bogdan's blog at http://bogdan.org.ua/). > > > import os, glob > path = '/home/robert' > for infile in glob.glob( os.path.join(path, '*.*') ): > ?testCommand = "'file -b " + infile + "'" > ?print testCommand, > ?test = os.system(testCommand) > ?print test > > > The code does indeed step through all the files in the directory and the testCommand string constructs properly. Yet, the output is not the same as shell results. All files, regardless of type, simply output the number 32512, with no description. > > > I have searched forums, read the man file and perused several Python references and have not been able to find the answer. Why does the same command yield different results, depending on how it's run? Any education is greatly appreciated. > > > Linux 2.6 (kubuntu 8.04) > Python 2.5.2 > file command 4.21 > > I think you have an extra set of quotes. Remove the single quotes from the testCommand and see if that works any better. testCommand = "file -b " + infile From contactdayo at gmail.com Sun May 10 05:19:14 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Sun, 10 May 2009 04:19:14 +0100 Subject: [Tutor] Triggering code on 1 minute intervale .. In-Reply-To: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> References: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> Message-ID: <4A0647B2.6060804@gmail.com> Alex Feddor wrote: > > .. What will be the best solution to trigger python code every minute > as soon as PC in on. > > Cheers, Alex > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > A cron job? Regards Dayo From alex.feddor at gmail.com Sat May 9 22:26:02 2009 From: alex.feddor at gmail.com (Alex Feddor) Date: Sat, 9 May 2009 22:26:02 +0200 Subject: [Tutor] Fwd: Sending Email - Outlook 2003 / Overcome Security Warning! In-Reply-To: <5bf184e30905091253v55df91fcr59883e4e7507c8e7@mail.gmail.com> References: <5bf184e30905091253v55df91fcr59883e4e7507c8e7@mail.gmail.com> Message-ID: <5bf184e30905091326r441edbadg7dda54609780d8cd@mail.gmail.com> Hi I would like to send email inside my Python code. On PC I have installed Outlook 2003. On the web I found code bellow which is working however I need every time manual clicked outlook security warning - alerting that outside process would like to send email. Please advise how the security warning can be tricked... Here is working code sample: from win32com.client import Dispatch session = Dispatch("MAPI.session") session.Logon('OUTLOOK') # MAPI profile name msg = session.Outbox.Messages.Add('Hellow', 'Test Message 400') msg.Recipients.Add('ales', 'SMTP:alex.feddor at gmail.com ') msg.Send() session.Logoff() On the web I found solution for VB.NET . There must be similar solution also for Python. http://www.add-in-express.com/docs/outlook-security-manager-automate.php (vb.NET CODE) Dim SecurityManager As New AddinExpress.Outlook.SecurityManager SecurityManager.ConnectTo(outlookApp) I tried running VB code inside Python however with no success as this is not VBScript. import win32com.client as win vbscript = win.Dispatch("ScriptControl") vbscript.language = "vbscript" vbscript.addcode('Main\n\ *Sub Main*\n\ *OlSecurityManager.ConnectTo OutlookApp*\n\ *OlSecurityManager.DisableOOMWarnings = True*\n\ *End Sub*\n') vbscript.eval("Main") Looking forward to your advice ... Cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 10 09:56:05 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 08:56:05 +0100 Subject: [Tutor] paramiko again References: <20090509211128.GD27344@chicago.blisses.org> <20090510003219.GG27344@chicago.blisses.org> Message-ID: "Matt Herzog" wrote > Yes, that is exactly what I wanted, os.listdir. But now I have another > issue: > specifying the remote dir. When I login to the sftp server and type pwd: > > sftp> pwd > Remote working directory: / > > Is what I see. OK, so either you have to change dir on the remote machine(via sftp) or you use the / directory. > Also, I don't think I want "join" in the below, do I? Why do I want to > "join" the two dirs? I'm not sure whether you should use join either. I think the first agument should be the path not the fuill filename, but I may be wrong. But at least you need to pass '/' as your path. > sftp.get(os.path.join(dir_path, f), f) dir_path = '/' sftp.get(dir_path, f) If that doesn't work put the join back in... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 10 09:59:34 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 08:59:34 +0100 Subject: [Tutor] Sending Email - Outlook 2003 / Overcome SecurityWarning! References: <5bf184e30905091253v55df91fcr59883e4e7507c8e7@mail.gmail.com> <5bf184e30905091326r441edbadg7dda54609780d8cd@mail.gmail.com> Message-ID: "Alex Feddor" wrote > I would like to send email inside my Python code. On PC I have installed > Outlook 2003. Outlook is a mail client, you don't need to use it to send email from Python. You can use the email module to do this, or for more detailed access use the smtp module. This just sends the mail directly to the mail server without going near Outlook. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 10 10:01:30 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 May 2009 09:01:30 +0100 Subject: [Tutor] Triggering code on 1 minute intervale .. References: <5bf184e30905091326o5de1d9d6qa85ccd6efc67947b@mail.gmail.com> Message-ID: "Alex Feddor" wrote > .. What will be the best solution to trigger python code every minute as > soon as PC in on. This depends on your OS. If its Linux/MacOs then cron might be the best bet. If its Windows you can use scheduled tasks to do it, or the at command. Or youcould write a python script that runs in the background (a service or daemon) and launces a thread at periodic intrevals. But I'd go with cron/at as the simplest solution. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Sun May 10 21:41:01 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 10 May 2009 12:41:01 -0700 Subject: [Tutor] Parsing Question In-Reply-To: References: Message-ID: On 5/9/2009 5:30 PM Alan Gauld said... > You should probably be able to do the first for loop as a list > comprehension, but I can't think of how to get the split() call > embedded into it right now! > Just for fun -- given datafile contents... 1 one 2 two 3 three 4 four 5 five 6 six 7 seven 8 eight 9 nine 10 ten 11 eleven 12 twelve ... here's a single list comprehension answer [ v for k,v in sorted( dict ( (int(pair[0]),pair[1]) for pairs in [ dataline.split() for dataline in open(r'c:\datafile').readlines() if dataline ] for pair in (pairs[:2], pairs[2:4]) ).items() )] ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'] Emile From pjsjoca at yahoo.com Sun May 10 22:50:32 2009 From: pjsjoca at yahoo.com (robert mcquirt) Date: Sun, 10 May 2009 13:50:32 -0700 (PDT) Subject: [Tutor] Different Command Result Executing in Shell vs. Program Message-ID: <798093.45022.qm@web53904.mail.re2.yahoo.com> @alan: Thank you very much for the information you provided and also for suggesting your tutorial. I am now studying the subprocess module. I was impressed with the depth of your tutorial and am studying it as well (and not just for my current problem). Thanks again. @dave: Thanks for your reply Dave. I did try removing the quote as you suggested and then received an error of missing quote. As best I can determine the quotes are needed to surround the command being used in the os.system() context. However, your reply did serve to remind me to check the simple possibilities first before seeking a more involved solution. Thanks again for that.? -------------- next part -------------- An HTML attachment was scrubbed... URL: From onyxtic at gmail.com Mon May 11 00:38:23 2009 From: onyxtic at gmail.com (Evans Anyokwu) Date: Sun, 10 May 2009 23:38:23 +0100 Subject: [Tutor] [OT] EuroPython Message-ID: Is anyone of you guys on this list going to EuroPython? http://www.europython.eu/ Unfortunately, I have registered to attend JavaOne, so can't make it. -- Evans http://www.javawug.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From ecasbasj at yahoo.es Mon May 11 00:48:38 2009 From: ecasbasj at yahoo.es (Emilio Casbas) Date: Sun, 10 May 2009 22:48:38 +0000 (GMT) Subject: [Tutor] How to control the not existent keys in a dict Message-ID: <226670.66826.qm@web24105.mail.ird.yahoo.com> Hello, I have some problems testing keys not existent in a dictionary. The code works testing existent keys with his corresponding value, but If I enter a not existent key, an exception is raised. How can I control the not existent key and assign him a default value? user at cipher:~/project/programming/python$ python3.0 test.py ?What is your name? KEY-NOT-EXISTENT Good Morning KEY-NOT-EXISTENT ?Which is the secret word? any Traceback (most recent call last): File "test.py", line 30, in while not correct_password(name,guess,dictionary): File "test.py", line 8, in correct_password if (is_in_dict and password == dict[nombre.capitalize()]): KeyError: 'KEY-NOT-EXISTENT' -relevant code snippet- dictionary = {'Mark': 'python', 'Dave': 'java', 'Ane': 'C++', 'default':'pass'} def correct_password(name,password,dict): if (not is_in_dict and password == dict['default']): return 1 elif (is_in_dict and password == dict[nombre.capitalize()]): return 1 return 0 def is_in_dict(nom,dict): for n in dict: if nom.lower() == n.lower(): return 1 return 0 -end relevant code snippet- Regards From emile at fenx.com Mon May 11 01:02:39 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 10 May 2009 16:02:39 -0700 Subject: [Tutor] How to control the not existent keys in a dict In-Reply-To: <226670.66826.qm@web24105.mail.ird.yahoo.com> References: <226670.66826.qm@web24105.mail.ird.yahoo.com> Message-ID: On 5/10/2009 3:48 PM Emilio Casbas said... > Hello, > > I have some problems testing keys not existent in a dictionary. > The code works testing existent keys with his corresponding value, but > If I enter a not existent key, an exception is raised. > How can I control the not existent key and assign him a default value? > > user at cipher:~/project/programming/python$ python3.0 test.py > ?What is your name? KEY-NOT-EXISTENT > Good Morning KEY-NOT-EXISTENT > ?Which is the secret word? any > Traceback (most recent call last): > File "test.py", line 30, in > while not correct_password(name,guess,dictionary): > File "test.py", line 8, in correct_password > if (is_in_dict and password == dict[nombre.capitalize()]): > KeyError: 'KEY-NOT-EXISTENT' > > -relevant code snippet- > dictionary = {'Mark': 'python', 'Dave': 'java', 'Ane': 'C++', 'default':'pass'} > > def correct_password(name,password,dict): > if (not is_in_dict and password == dict['default']): Both here... > return 1 > elif (is_in_dict and password == dict[nombre.capitalize()]): ... and here you're not testing the inputs, you're testing if is_in_dict _is_ (which of course it is as it's defined and ergo is) -- you need to pass it your parameters, ie, 'is_in_dict(nombre,dictionary)' > return 1 > return 0 > > def is_in_dict(nom,dict): also -- it's not a good ides to shadow built-in names -- better to use a different one like n_dict HTH, Emile > for n in dict: > if nom.lower() == n.lower(): > return 1 > return 0 > -end relevant code snippet- > > Regards > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From david at abbottdavid.com Mon May 11 01:08:17 2009 From: david at abbottdavid.com (David) Date: Sun, 10 May 2009 19:08:17 -0400 Subject: [Tutor] Suggestions for while loop Message-ID: <4A075E61.9050809@abbottdavid.com> Hi, I am going through Wesley Chun's book and this is Exercise 8-11; Write a program to ask the user to input a list of names, format "Last Name" "comma" "First Name". Write a function that manages the input so that when/if the user types in the names in the wrong format the error is corrected, and keep track of the number of errors. When done sort the list. Questions; This; answers = raw_input('Enter Name: ') index = answers.find(',') Does not seem to be a very good to me, how could I check for a comma between two words? And This; try: total_names = int(raw_input('Enter Total Number of Names: ')) except ValueError: print 'You must enter a number!' total_names = int(raw_input('Enter Total Number of Names: ')) Does not handle the second input for exceptions. thanks, -david #!/usr/bin/python import string names = [] def get_names(): wrong_format = 0 count = 0 try: total_names = int(raw_input('Enter Total Number of Names: ')) except ValueError: print 'You must enter a number!' total_names = int(raw_input('Enter Total Number of Names: ')) while count < total_names: print 'Format: LastName, First Name' answers = raw_input('Enter Name: ') index = answers.find(',') answers = answers.title() index = answers.find(',') if index != -1: names.append(answers) count += 1 else: wrong_format += 1 print '\nWrong Format!!!' if wrong_format > 1: print 'That is %i Fixing Format ...' % wrong_format else: print 'You have done this %i times, Fixing Format ...' % wrong_format print 'Done, continue:\n' answers = answers.split() answers = string.join(answers, ', ') names.append(answers) count += 1 def show_names(): print 'Your Names Are: \n' for i in sorted(names): print i def main(): get_names() if names: show_names() if __name__ == "__main__": main() -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Mon May 11 01:59:32 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 May 2009 00:59:32 +0100 Subject: [Tutor] How to control the not existent keys in a dict References: <226670.66826.qm@web24105.mail.ird.yahoo.com> Message-ID: "Emilio Casbas" wrote > If I enter a not existent key, an exception is raised. > How can I control the not existent key and assign him a default value? One way is to use the get() method of a dictionary: >>> d = {'Exists': True} >>> d['Exists'] True >>> d['Error'] Traceback!!!.... >>> d.get('Error',False) False HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at ieee.org Mon May 11 03:13:04 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 10 May 2009 21:13:04 -0400 Subject: [Tutor] Suggestions for while loop In-Reply-To: References: Message-ID: <4A077BA0.2090009@ieee.org> David wrote: > > Hi, I am going through Wesley Chun's book and this is Exercise 8-11; > Write a program to ask the user to input a list of names, format "Last > Name" "comma" "First Name". Write a function that manages the input so > that when/if the user types in the names in the wrong format the error > is corrected, and keep track of the number of errors. When done sort the > list. > > Questions; > > This; > answers = raw_input('Enter Name: ') > index = answers.find(',') > > Does not seem to be a very good to me, how could I check for a comma > between two words? > > And This; > try: > total_names = int(raw_input('Enter Total Number of Names: ')) > except ValueError: > print 'You must enter a number!' > total_names = int(raw_input('Enter Total Number of Names: ')) > > Does not handle the second input for exceptions. > > thanks, > -david > > > #!/usr/bin/python > import string > > names = [] > > def get_names(): > wrong_format = 0 > count = 0 > try: > total_names = int(raw_input('Enter Total Number of Names: ')) > except ValueError: > print 'You must enter a number!' > total_names = int(raw_input('Enter Total Number of Names: ')) > > while count < total_names: > print 'Format: LastName, First Name' > answers = raw_input('Enter Name: ') > index = answers.find(',') > answers = answers.title() > index = answers.find(',') > if index != -1: > names.append(answers) > count += 1 > else: > wrong_format += 1 > print '\nWrong Format!!!' > if wrong_format > 1: > print 'That is %i Fixing Format ...' % wrong_format > else: > print 'You have done this %i times, Fixing Format ...' > % wrong_format > print 'Done, continue:\n' > answers = answers.split() > answers = string.join(answers, ', ') > names.append(answers) > count += 1 > > def show_names(): > print 'Your Names Are: \n' > for i in sorted(names): > print i > > def main(): > get_names() > if names: > show_names() > > if __name__ == "__main__": > main() > > For your second question, there are several things that could be wrong with the user's input. Usually it's easiest to make that a separate function to make it readable. Then the function returns whenever the result passes its validation. def getTotalNumber(prompt): while True: try: numstr = raw_input(prompt) n = int(numstr) if 0 < n < 50: #some validation return n print "Number is not reasonable, try again" except ValueError: print "You must enter a number!" This way there are two forms of validation happening. If the exception fires, the user gets one print statement, and if the validation fails, he gets the other. The purist might complain that the return is in the middle of the function, so if you want to fix that, move the return n to the end, and change the existing one to a break.. The first question can be solved in a similar way, though the meaning of "the error is corrected" is unclear. You can correct the error by asking the user to fix it. Or you could take a wild guess that instead of lastname, firstname the user has entered firstname lastname But since many names have multiple spaces (Dr. William Smith, or Henry James, Jr.) or even other punctuation, I'd think the only safe thing would be to ask the user separately for the fields you want. And each field may contain spaces or commas, or periods. Anyway, since it's just an exercise, I'd probably model it the same way as the first function, and just ask the user again. So the function would look quite similar, with the one difference that it would return an error count as well as the name. Then the main function would accumulate the counts and print the summary at the end. DaveA From roadierich at googlemail.com Mon May 11 05:33:17 2009 From: roadierich at googlemail.com (Richard Lovely) Date: Mon, 11 May 2009 04:33:17 +0100 Subject: [Tutor] Suggestions for while loop In-Reply-To: <4A075E61.9050809@abbottdavid.com> References: <4A075E61.9050809@abbottdavid.com> Message-ID: 2009/5/11 David : > Hi, I am going through Wesley Chun's book and this is Exercise 8-11; > Write a program to ask the user to input a list of names, format "Last Name" > "comma" "First Name". Write a function that manages the input so that > when/if the user types in the names in the wrong format the error is > corrected, and keep track of the number of errors. When done sort the list. > > Questions; > > This; > answers = raw_input('Enter Name: ') > index = answers.find(',') > > Does not seem to be a very good to me, how could I check for a comma between > two words? Look up string.split(), see what happens if the comma isn't in the string you try to split. You could also try the "in" operator. > > And This; > try: > ? ?total_names = int(raw_input('Enter Total Number of Names: ')) > except ValueError: > ? ?print 'You must enter a number!' > ? ?total_names = int(raw_input('Enter Total Number of Names: ')) > > Does not handle the second input for exceptions. while True: # loop forever try: total_names = int(raw_input('Enter Total Number of Names: ')) except ValueError: print 'You must enter a number!' else: # execution only gets to here if no exception occurred break # exit the loop You could do the same using a continue statement in the except: clause, and break outside the entire structure. total_names = -1 # set an invalid initial value while not 0 < total_names < 50: # loop while total_names has an invalid value try: # If this excepts, total_names is not changed. total_names = int(raw_input('Enter Total Number of Names: ')) # if total_names now has a valid value, the loop exits. except ValueError: print 'You must enter a number!' # loop continues. -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com From roadierich at googlemail.com Mon May 11 05:41:24 2009 From: roadierich at googlemail.com (Richard Lovely) Date: Mon, 11 May 2009 04:41:24 +0100 Subject: [Tutor] How to control the not existent keys in a dict In-Reply-To: <226670.66826.qm@web24105.mail.ird.yahoo.com> References: <226670.66826.qm@web24105.mail.ird.yahoo.com> Message-ID: 2009/5/10 Emilio Casbas : > > Hello, > > I have some problems testing keys not existent in a dictionary. > The code works testing existent keys with his corresponding value, but > If I enter a not existent key, an exception is raised. > How can I control the not existent key and assign him a default value? > > user at cipher:~/project/programming/python$ python3.0 test.py > ?What is your name? KEY-NOT-EXISTENT > Good Morning KEY-NOT-EXISTENT > ?Which is the secret word? any > Traceback (most recent call last): > ?File "test.py", line 30, in > ? ?while not correct_password(name,guess,dictionary): > ?File "test.py", line 8, in correct_password > ? ?if (is_in_dict and password == dict[nombre.capitalize()]): > KeyError: 'KEY-NOT-EXISTENT' > > -relevant code snippet- > dictionary = {'Mark': 'python', 'Dave': 'java', 'Ane': 'C++', 'default':'pass'} > > def correct_password(name,password,dict): > ? ? ? ?if (not is_in_dict and password == dict['default']): > ? ? ? ? ? ? ? ?return 1 > ? ? ? ?elif (is_in_dict and password == dict[nombre.capitalize()]): > ? ? ? ? ? ? ? ?return 1 > ? ? ? ?return 0 > > def is_in_dict(nom,dict): > ? ? ? ?for n in dict: > ? ? ? ? ? ? ? ?if nom.lower() == n.lower(): > ? ? ? ? ? ? ? ? ? ? ? ?return 1 > ? ? ? ?return 0 > -end relevant code snippet- > > Regards > > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > There's also collections.defaultdict, which you can initialise with a "default value factory" which will generate a value for every attempt to get a non-existent key. It can be a lot of fun with a bit of thought. -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com From rony_dsouza at yahoo.com Mon May 11 06:23:24 2009 From: rony_dsouza at yahoo.com (rony dsouza) Date: Sun, 10 May 2009 21:23:24 -0700 (PDT) Subject: [Tutor] Guidance needed for script Message-ID: <114608.24632.qm@web110705.mail.gq1.yahoo.com> Hi Al, ? My name is Rony, I am new to python. ? Need your help and guidance for developing a python script with?the below details: ? Using GnuPG to encrypt a file(s) on linux.? Essentially two modes of operation would be ideal: ? 1.?????? Running something like ?./encrypt.py inputfilename.txt? which will output a file called ?inputfilename.txt.pgp?. ? 2.?????? The ability to pipe input into ?encrypt.py? and have it write out in encrypted format.? Something like ?echo thisistestdata | ./encrypt.py > encrypted.pgp?. ? I would be greatful, to you for the needful. ? Regards, ? Rony -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Mon May 11 08:04:40 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 11 May 2009 08:04:40 +0200 Subject: [Tutor] Parsing Question In-Reply-To: References: Message-ID: Emile van Sebille wrote: > On 5/9/2009 5:30 PM Alan Gauld said... > >> You should probably be able to do the first for loop as a list >> comprehension, but I can't think of how to get the split() call >> embedded into it right now! >> > > Just for fun -- given datafile contents... > > 1 one 2 two > 3 three 4 four > 5 five 6 six > 7 seven 8 eight > 9 nine 10 ten > 11 eleven 12 twelve > > ... here's a single list comprehension answer > > [ v for k,v in sorted( > dict ( (int(pair[0]),pair[1]) > for pairs in [ dataline.split() > for dataline in open(r'c:\datafile').readlines() if dataline ] > for pair in (pairs[:2], pairs[2:4]) ).items() )] > > > ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', > 'ten', 'eleven', 'twelve'] Kids, don't try this at home ... Stefan From a.t.hofkamp at tue.nl Mon May 11 08:35:39 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 11 May 2009 08:35:39 +0200 Subject: [Tutor] Guidance needed for script In-Reply-To: <114608.24632.qm@web110705.mail.gq1.yahoo.com> References: <114608.24632.qm@web110705.mail.gq1.yahoo.com> Message-ID: <4A07C73B.9050106@tue.nl> rony dsouza wrote: > Hi Al, > > My name is Rony, I am new to python. Welcome to the list. > Need your help and guidance for developing a python script with the below details: > > Using GnuPG to encrypt a file(s) on linux. Essentially two modes of operation would be ideal: > > 1. Running something like ?./encrypt.py inputfilename.txt? which will output a file called ?inputfilename.txt.pgp?. > > 2. The ability to pipe input into ?encrypt.py? and have it write out in encrypted format. Something like ?echo thisistestdata | ./encrypt.py > encrypted.pgp?. Sounds like a nice first project. Grab yourself a Python 2.5 or 2.6, read a tutorial on Python, then start trying to accomplish your project. When you get stuck, post a question about that specific problem, and we will try to guide you back on track so you can finish your project. To interface with the GnuPG program, the subprocess module seems like a good starting point to me. Albert From alan.gauld at btinternet.com Mon May 11 09:59:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 May 2009 08:59:16 +0100 Subject: [Tutor] Guidance needed for script References: <114608.24632.qm@web110705.mail.gq1.yahoo.com> Message-ID: "rony dsouza" wrote > Using GnuPG to encrypt a file(s) on linux. Essentially two modes of > operation would be ideal: You might find the GPG module useful but it is sadly no longer maintained by AMK(since 2006). So it may not work in recent versions of Python. http://wiki.python.org/moin/GnuPrivacyGuard#Python.2BAC8-GnuPGinterface There is mention of someone possibly picking it up... There is also something called PyMe which is an interface to GPGMe http://pyme.sourceforge.net/ > 1. Running something like ?./encrypt.py inputfilename.txt? which will > output a file called > ?inputfilename.txt.pgp?. > > 2. The ability to pipe input into ?encrypt.py? and have it write out in > encrypted format. > Something like ?echo thisistestdata | ./encrypt.py > encrypted.pgp?. The use of stdin/stdout for piping and the use of command line arguments are both covered in the 'Talking to the User' topic of my tutorial. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From timomlists at gmail.com Mon May 11 19:39:20 2009 From: timomlists at gmail.com (Timo) Date: Mon, 11 May 2009 19:39:20 +0200 Subject: [Tutor] Alternative for Shelve Message-ID: <4A0862C8.2000002@gmail.com> Hello all, I have an issue with the Shelve module. It works great for my needs, the only problem is that a file made with Shelve isn't interchangable between different computers. I want my data to be exchanged between users. Does someone have an alternative to Shelve? It must be able to contain keys and values, and the values are a list of dictionaries. For example: 1234567 = [{'date' : '2009-05-11', 'location' : 'Germany'}, {'date' : '2009-05-04', 'location' : 'France'}] 7654321 = [{'date' : '2009-03-12', 'location' : 'Belgium'}, {'date' : '2009-04-23', 'location' : 'Spain'}] And so on... Thanks, Timo From msh at blisses.org Mon May 11 22:22:36 2009 From: msh at blisses.org (Matt Herzog) Date: Mon, 11 May 2009 16:22:36 -0400 Subject: [Tutor] moving files from one dir to another Message-ID: <20090511202236.GI27344@chicago.blisses.org> Should be simple, right? Not for me, heh. def schmove(src,dst): ... src = '/home/datasvcs/PIG/cjomeda_exp/' ... dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' ... listOfFiles = os.listdir(src) ... for filez in listOfFiles: ... os.system("mv"+ " " + src + " " + dst) The above code does not copy anything. I sure wish it would spit just one error. I sure wish I could for the last line go, shutil.move("src", "dest") That would be cool. Plz halp! -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From david at abbottdavid.com Mon May 11 22:42:50 2009 From: david at abbottdavid.com (David) Date: Mon, 11 May 2009 16:42:50 -0400 Subject: [Tutor] Suggestions for while loop In-Reply-To: <4A077BA0.2090009@ieee.org> References: <4A077BA0.2090009@ieee.org> Message-ID: <4A088DCA.6060804@abbottdavid.com> Dave Angel wrote: > > def getTotalNumber(prompt): > while True: > try: > numstr = raw_input(prompt) > n = int(numstr) > if 0 < n < 50: #some validation > return n > print "Number is not reasonable, try again" > except ValueError: > print "You must enter a number!" > Richard "Roadie Rich" Lovely wrote: > total_names = -1 # set an invalid initial value > while not 0 < total_names < 50: # loop while total_names has an invalid value > try: > # If this excepts, total_names is not changed. > total_names = int(raw_input('Enter Total Number of Names: ')) > # if total_names now has a valid value, the loop exits. > except ValueError: > print 'You must enter a number!' # loop continues. Thanks Dave and Richard, And to run it is this correct total_names = getTotalNumber('Enter Total Number of Names: ') -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From mandel at themacaque.com Mon May 11 22:44:09 2009 From: mandel at themacaque.com (mandel at themacaque.com) Date: Mon, 11 May 2009 21:44:09 +0100 (BST) Subject: [Tutor] Import package module problem Message-ID: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> Hello there, I have just started working with python and I have some issues understanding how I should be importing modules from packages. Curretly I have the following tree structure: general/ __init__.py address_book.py groups/ __init__.py contact_group.py where groups and general are in the same level and all the __init__.py files are empty. In address_book.py I want to import the ContactGroup which is at contact_group.py. I have try the following posibilities: from ..groups.contact_group import ContactGroup from groups.contact_group import ContactGroups from groups.contact_group import * But I cannot get it working. I know that the third option will not work because I have not set up __all__ but I do not understand why it does not work. I have read books and tutorials about it, but I cannot get it. Am I doing something wrong? I would really appreciate a hand or pointer to s step by step example. Kr, Manuel From ecasbasj at yahoo.es Mon May 11 23:47:03 2009 From: ecasbasj at yahoo.es (Emilio Casbas) Date: Mon, 11 May 2009 21:47:03 +0000 (GMT) Subject: [Tutor] How to control the not existent keys in a dict In-Reply-To: References: <226670.66826.qm@web24105.mail.ird.yahoo.com> Message-ID: <942903.92244.qm@web24102.mail.ird.yahoo.com> Thanks to all for the responses. Problem solved!, and I get a lot of valuable info. Thanks Emilio ----- Mensaje original ---- > De: Alan Gauld > Para: tutor at python.org > Enviado: lunes, 11 de mayo, 2009 1:59:32 > Asunto: Re: [Tutor] How to control the not existent keys in a dict > > > "Emilio Casbas" wrote > > > If I enter a not existent key, an exception is raised. > > How can I control the not existent key and assign him a default value? > > One way is to use the get() method of a dictionary: > > >>> d = {'Exists': True} > > >>> d['Exists'] > True > >>> d['Error'] > Traceback!!!.... > >>> d.get('Error',False) > False > > HTH, > > > -- Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Mon May 11 23:59:29 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 May 2009 22:59:29 +0100 Subject: [Tutor] moving files from one dir to another References: <20090511202236.GI27344@chicago.blisses.org> Message-ID: "Matt Herzog" wrote > I sure wish I could for the last line go, > > shutil.move("src", "dest") > > That would be cool. So why don't you? What is the problem? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon May 11 23:56:07 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 May 2009 22:56:07 +0100 Subject: [Tutor] Alternative for Shelve References: <4A0862C8.2000002@gmail.com> Message-ID: "Timo" wrote > I have an issue with the Shelve module. It works great for my needs, the > only problem is that a file made with Shelve isn't interchangable between > different computers. I thought it would be. What kind of computers are you having issues with? And what kind of issues? > Does someone have an alternative to Shelve? > It must be able to contain keys and values, and the values are a list of > dictionaries. A SqlLite database? With record where the fields correspond to your dictionary entries. ID:Date:Location > 1234567 = [{'date' : '2009-05-11', 'location' : 'Germany'}, {'date' : > '2009-05-04', 'location' : 'France'}] > 7654321 = [{'date' : '2009-03-12', 'location' : 'Belgium'}, {'date' : > '2009-04-23', 'location' : 'Spain'}] populating your dictionary becomes a matter of extracting the data: SELECT DATE, LOCATION FROM DATA WHERE ID = key SQLite has the advantage that the atabase is a single file and it is portable across platforms provided they have Sqlite installed. And its part of the Python standard library. Examples of using Sqlite in the databases topic of my tutorial. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Tue May 12 01:33:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 11 May 2009 19:33:13 -0400 Subject: [Tutor] Alternative for Shelve In-Reply-To: <4A0862C8.2000002@gmail.com> References: <4A0862C8.2000002@gmail.com> Message-ID: <1c2a2c590905111633q5c047853r1438d4ccef3aad87@mail.gmail.com> On Mon, May 11, 2009 at 1:39 PM, Timo wrote: > Hello all, > > I have an issue with the Shelve module. It works great for my needs, the > only problem is that a file made with Shelve isn't interchangable between > different computers. I want my data to be exchanged between users. > > Does someone have an alternative to Shelve? > It must be able to contain keys and values, and the values are a list of > dictionaries. Try the pickle module. Kent From kent37 at tds.net Tue May 12 01:38:00 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 11 May 2009 19:38:00 -0400 Subject: [Tutor] moving files from one dir to another In-Reply-To: <20090511202236.GI27344@chicago.blisses.org> References: <20090511202236.GI27344@chicago.blisses.org> Message-ID: <1c2a2c590905111638i15d403b5wfd2675824567cb29@mail.gmail.com> On Mon, May 11, 2009 at 4:22 PM, Matt Herzog wrote: > Should be simple, right? Not for me, heh. > > def schmove(src,dst): > ... ? ? ? ? src = '/home/datasvcs/PIG/cjomeda_exp/' > ... ? ? ? ? dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' > ... ? ? ? ? listOfFiles = os.listdir(src) > ... ? ? ? ? for filez in listOfFiles: > ... ? ? ? ? ? ? os.system("mv"+ " " + src + " " + dst) You aren't using filez at all. Try os.system("mv %s/%s %s" % (src, filez, dst)) or you can also use os.path.join() to construct the source path. > The above code does not copy anything. I sure wish it would spit just one error. > > I sure wish I could for the last line go, > > shutil.move("src", "dest") should be shutil.move(os.path.join(src, filez), dst) Kent From davea at ieee.org Tue May 12 03:25:06 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 11 May 2009 21:25:06 -0400 Subject: [Tutor] moving files from one dir to another In-Reply-To: References: Message-ID: <4A08CFF2.5080103@ieee.org> Matt Herzog wrote: > Should be simple, right? Not for me, heh. > > def schmove(src,dst): > ... src = '/home/datasvcs/PIG/cjomeda_exp/' > ... dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' > ... listOfFiles = os.listdir(src) > ... for filez in listOfFiles: > ... os.system("mv"+ " " + src + " " + dst) > > > The above code does not copy anything. I sure wish it would spit just one error. > > I sure wish I could for the last line go, > > shutil.move("src", "dest") > > That would be cool. > > Plz halp! > > > Just use os.rename() if the dest is a directory, it'll move the file there. From nickelflipper at yahoo.com Tue May 12 04:09:30 2009 From: nickelflipper at yahoo.com (nickel flipper) Date: Mon, 11 May 2009 19:09:30 -0700 (PDT) Subject: [Tutor] How to set up an Array? Message-ID: <200109.40341.qm@web65513.mail.ac4.yahoo.com> Hello, Just getting started with Python, and have had some early, although trivial success. It looks like just the ticket to parse a data file. Total noob at this, but really kind of overwhelmed by all the options. Looking for some direction on which modules, librarys, or ? to accomplish the goal. To date have been using conditional type statement to tease out information. Now I need to match some strings, put them in an array or list? and read them back after X number of lines. The linesplits are in a predictable pattern and padded by '-' to get eight values or arrays that will be joined together after a set condition is no longer true. So from the data set below, will be looking to print out: RA7,OSC1,CLKI RA6,OSC2 RA5,AN4,nSS1,LVDIN,RCV,RP2 ETC. Any tips greatly appreciated. Thanks. sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') reset (por='xxxxxxxx' mclr='uuuuuuuu') bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') bit (tag=scl names='RA' width='8') bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1') bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1') bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1') bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1') Here is some noob code used to pick out the RA7, RA6, RA5 etc. Comments welcome. if line.startswith ( " bit (names='R" ): (a, pin7, pin6, pin5, pin4, pin3, pin2, pin1, pin0) = (line.split() + ["", ""])[:9] startpin = pin7[8:3] if startpin.startswith == "RA" or "RB" or "RC" or "RD" or "RE" or "RF" or "RG" or "RH" or "RJ": print pin7[8:] + "," + pin6 + "," + pin5 + "," + pin4 + "," + pin3 + "," + pin2 + "," + pin1 + "," + pin0[:3] + "\n" else: pass From john at fouhy.net Tue May 12 05:05:07 2009 From: john at fouhy.net (John Fouhy) Date: Tue, 12 May 2009 15:05:07 +1200 Subject: [Tutor] How to set up an Array? In-Reply-To: <200109.40341.qm@web65513.mail.ac4.yahoo.com> References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> Message-ID: <5e58f2e40905112005o59ab0fe7u23f3cf7c023b5301@mail.gmail.com> 2009/5/12 nickel flipper : > sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') > ? ?reset (por='xxxxxxxx' mclr='uuuuuuuu') > ? ?bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') > ? ?bit (tag=scl names='RA' width='8') > ? ?bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') > ? ?bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1') > ? ?bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1') > ? ?bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1') > ? ?bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1') Hmm, hairy! You could try looking into regular expressions. Let's see: \b\w+='.+?' That should be a regular expression matching the beginning of a word, followed by one or more word characters, an =, a ', some characters, up to the first '. i.e. we're trying to match things like "names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0'" So, here we go: >>> import re >>> s = " bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1')" >>> rex = re.compile(r"\b\w+='.+?'") (note the r before the string.. that makes it a "raw" string and prevents backslashes from causing problems, mostly) >>> def to_dict(s): ... res = {} ... for token in rex.findall(s): ... word, rest = token.split('=') ... rest = rest.replace("'", '') ... res[word] = rest.split() ... return res ... (note that my regular expression, rex, is here effectively a global variable. That's convenient in the interactive interpreter, but you may want to do that a little differently in your final script) >>> to_dict(s) {'width': ['1', '1', '1', '1', '1', '1', '1', '1'], 'names': ['RA7', 'RA6', 'RA5', '-', 'RA3', 'RA2', 'RA1', 'RA0']} If regular expressions aren't powerful enough, you could look into a full-fledged parsing library. There are two I know of: simpleparse and pyparsing. SimpleParse works well if you're familiar with writing grammars: you write a grammar in the usual style, and simpleparse makes a parser out of it. pyparsing is more OO. -- John. From lie.1296 at gmail.com Tue May 12 07:46:43 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 12 May 2009 15:46:43 +1000 Subject: [Tutor] How to set up an Array? In-Reply-To: <5e58f2e40905112005o59ab0fe7u23f3cf7c023b5301@mail.gmail.com> References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> <5e58f2e40905112005o59ab0fe7u23f3cf7c023b5301@mail.gmail.com> Message-ID: John Fouhy wrote: > 2009/5/12 nickel flipper : >> sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') >> reset (por='xxxxxxxx' mclr='uuuuuuuu') >> bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') >> bit (tag=scl names='RA' width='8') >> bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') >> bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1') >> bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1') >> bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1') >> bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1') > > Hmm, hairy! > > You could try looking into regular expressions. Let's see: > > \b\w+='.+?' > > That should be a regular expression matching the beginning of a word, > followed by one or more word characters, an =, a ', some characters, > up to the first '. > > i.e. we're trying to match things like "names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0'" > > So, here we go: > >>>> import re >>>> s = " bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1')" >>>> rex = re.compile(r"\b\w+='.+?'") > > (note the r before the string.. that makes it a "raw" string and > prevents backslashes from causing problems, mostly) > >>>> def to_dict(s): > .... res = {} > .... for token in rex.findall(s): > .... word, rest = token.split('=') > .... rest = rest.replace("'", '') > .... res[word] = rest.split() > .... return res > .... > > (note that my regular expression, rex, is here effectively a global > variable. That's convenient in the interactive interpreter, but you > may want to do that a little differently in your final script) > >>>> to_dict(s) > {'width': ['1', '1', '1', '1', '1', '1', '1', '1'], 'names': ['RA7', > 'RA6', 'RA5', '-', 'RA3', 'RA2', 'RA1', 'RA0']} > > If regular expressions aren't powerful enough, you could look into a > full-fledged parsing library. There are two I know of: simpleparse > and pyparsing. SimpleParse works well if you're familiar with writing > grammars: you write a grammar in the usual style, and simpleparse > makes a parser out of it. pyparsing is more OO. > Surely it is easiest to do this with regular expression or parsing library, however I would not recommend you to start learning regex or parser libs until you have the basics first. If you do, you will just confuse yourself. Read more on the beginner tutorials in the documentation. More specifically you must know that str.startswith is a function and startpin.startswith == "RA" or "RB" or ... or "RJ" does not do anything useful since you're comparing a function with "RA", then or-ing it with a string. Since non-empty string is always True, the conditional is always True. The correct way to do it (using .startswith) would be startpin.startswith(("RA", "RB", "RC", "RD", "RE", "RF", "RG", "RH", "RJ")) and it is generally not a good idea to hardcode the string indexes (e.g. pin7[8:3]) as it will be PITA if the format has even the slightest change. From denis.spir at free.fr Tue May 12 09:01:50 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 09:01:50 +0200 Subject: [Tutor] Import package module problem In-Reply-To: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> References: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> Message-ID: <20090512090150.704c8712@o> Le Mon, 11 May 2009 21:44:09 +0100 (BST), mandel at themacaque.com s'exprima ainsi: > Hello there, > > I have just started working with python and I have some issues > understanding how I should be importing modules from packages. Curretly I > have the following tree structure: > > general/ > __init__.py > address_book.py > groups/ > __init__.py > contact_group.py > > where groups and general are in the same level and all the __init__.py > files are empty. > > In address_book.py I want to import the ContactGroup which is at > contact_group.py. I have try the following posibilities: > > from ..groups.contact_group import ContactGroup > from groups.contact_group import ContactGroups > from groups.contact_group import * > > But I cannot get it working. I know that the third option will not work > because I have not set up __all__ but I do not understand why it does not > work. I have read books and tutorials about it, but I cannot get it. > > Am I doing something wrong? I would really appreciate a hand or pointer to > s step by step example. Not sure at all, 'cause python packaging is still rather unclear for me, but I guess you should have module (or other names thingies) imported inside __init__.py; and that's precisely their main purpose (in addition to possibly running startup code).?If I'm right, simply writing in groups/__init__.py import contact_group or from contact_group import ContactGroups should do the job. Actually, python does *not* automatically populate (sub-)packages' dict of names with the files in the directory (as I would also have expected). [The reason why I still do not understand what python packaging, esp. the use of __init__ files, is supposed to bring to the developper and/or the user. I've read several docs several times, but they all say the same, even using the same examples, and none points to the actual purpose. Having a 'normal' py file importing names intended to be exported would do the same job. Anyone?] Denis > Kr, > > Manuel > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > ------ la vita e estrany From a.t.hofkamp at tue.nl Tue May 12 09:14:36 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Tue, 12 May 2009 09:14:36 +0200 Subject: [Tutor] Import package module problem In-Reply-To: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> References: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> Message-ID: <4A0921DC.9020704@tue.nl> mandel at themacaque.com wrote: > Hello there, > > I have just started working with python and I have some issues > understanding how I should be importing modules from packages. Curretly I > have the following tree structure: > > general/ > __init__.py > address_book.py > groups/ > __init__.py > contact_group.py > > where groups and general are in the same level and all the __init__.py > files are empty. You seem to need a common starting point: % ls -R .: general groups main.py ./general: gen.py __init__.py ./groups: grp.py __init__.py The same setup as you have, except my names are less nice, and I have a ./main.py added The latter file is the starting point: % cat main.py from general import gen The general/gen file imports the groups/grp file, and then prints some text: % cat general/gen.py from groups import grp print "inside gen.py" Finally, the groups/grp.py file outputs only some text: % cat groups/grp.py print "inside grp.py" When I run this code, I get the following output: % python main.py inside grp.py inside gen.py By starting from main.py, the Python interpreter uses the current "." directory as starting point for imports. That starting point is then used to refer to other files (possibly in another package). Sincerely, Albert From mandel at themacaque.com Tue May 12 09:39:00 2009 From: mandel at themacaque.com (mandel at themacaque.com) Date: Tue, 12 May 2009 08:39:00 +0100 (BST) Subject: [Tutor] Import package module problem Message-ID: <18449.194.29.97.144.1242113940.squirrel@ip-208-109-127-191.ip.secureserver.net> > Le Mon, 11 May 2009 21:44:09 +0100 (BST), > mandel at themacaque.com s'exprima ainsi: > >> Hello there, >> >> I have just started working with python and I have some issues >> understanding how I should be importing modules from packages. Curretly >> I >> have the following tree structure: >> >> general/ >> __init__.py >> address_book.py >> groups/ >> __init__.py >> contact_group.py >> >> where groups and general are in the same level and all the __init__.py >> files are empty. >> >> In address_book.py I want to import the ContactGroup which is at >> contact_group.py. I have try the following posibilities: >> >> from ..groups.contact_group import ContactGroup >> from groups.contact_group import ContactGroups >> from groups.contact_group import * >> >> But I cannot get it working. I know that the third option will not work >> because I have not set up __all__ but I do not understand why it does >> not >> work. I have read books and tutorials about it, but I cannot get it. >> >> Am I doing something wrong? I would really appreciate a hand or pointer >> to >> s step by step example. > > Not sure at all, 'cause python packaging is still rather unclear for me, > but I guess you should have module (or other names thingies) imported > inside __init__.py; and that's precisely their main purpose (in addition > to possibly running startup code).??If I'm right, simply writing in > groups/__init__.py > import contact_group > or > from contact_group import ContactGroups > should do the job. Actually, python does *not* automatically populate > (sub-)packages' dict of names with the files in the directory (as I would > also have expected). > Lets see if I understand what you mean, in my example I should add an import inside __init__.py to be able to use relative imports: /general __init__.py >> from address_book import AddressBook address_book.py /groups __init__.py >> from contact_group import ContactGroup contact_group.py Lets say I have the above, would I be able to use the following? from ..groups.contact_group import ContactGroup I'm trying to use this type of import to be able to bundle my unit tests with my code and let devels work with it. I have read somewhere that __all__ can do the trick but it did not say how. > [The reason why I still do not understand what python packaging, esp. the > use of __init__ files, is supposed to bring to the developper and/or the > user. I've read several docs several times, but they all say the same, > even using the same examples, and none points to the actual purpose. > Having a 'normal' py file importing names intended to be exported would do > the same job. Anyone?] > > Denis > >> Kr, >> >> Manuel >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From mandel at themacaque.com Tue May 12 09:42:52 2009 From: mandel at themacaque.com (mandel at themacaque.com) Date: Tue, 12 May 2009 08:42:52 +0100 (BST) Subject: [Tutor] Import package module problem In-Reply-To: <4A0921DC.9020704@tue.nl> References: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> <4A0921DC.9020704@tue.nl> Message-ID: <4809.194.29.97.144.1242114172.squirrel@ip-208-109-127-191.ip.secureserver.net> > mandel at themacaque.com wrote: >> Hello there, >> >> I have just started working with python and I have some issues >> understanding how I should be importing modules from packages. Curretly >> I >> have the following tree structure: >> >> general/ >> __init__.py >> address_book.py >> groups/ >> __init__.py >> contact_group.py >> >> where groups and general are in the same level and all the __init__.py >> files are empty. > > You seem to need a common starting point: > > % ls -R > .: > general groups main.py > > ./general: > gen.py __init__.py > > ./groups: > grp.py __init__.py > > The same setup as you have, except my names are less nice, and I have a > ./main.py added > > The latter file is the starting point: > > % cat main.py > from general import gen > > The general/gen file imports the groups/grp file, and then prints some > text: > > % cat general/gen.py > from groups import grp > print "inside gen.py" > > Finally, the groups/grp.py file outputs only some text: > > % cat groups/grp.py > print "inside grp.py" > > When I run this code, I get the following output: > > % python main.py > inside grp.py > inside gen.py > > > By starting from main.py, the Python interpreter uses the current "." > directory as starting point for imports. That starting point is then used > to > refer to other files (possibly in another package). > > > Sincerely, > Albert > > I have actually rearranged my code to have such hierarchy which obviously works. The problem is that I want ot be able to perform unit tests withinn the package of the code. I keep having the problem with from ..groups.contact_group import ContactGroup I have read that the notation is correct, but how do you get it too work? I can alway move all the tests one level up, but I really would prefer not to have to do it. Kr, Manuel From timomlists at gmail.com Tue May 12 09:59:05 2009 From: timomlists at gmail.com (Timo) Date: Tue, 12 May 2009 09:59:05 +0200 Subject: [Tutor] Alternative for Shelve In-Reply-To: References: <4A0862C8.2000002@gmail.com> Message-ID: <4A092C49.8030909@gmail.com> Alan Gauld schreef: > "Timo" wrote > >> I have an issue with the Shelve module. It works great for my needs, >> the only problem is that a file made with Shelve isn't interchangable >> between different computers. > > I thought it would be. > What kind of computers are you having issues with? > And what kind of issues? Currently only tested between Ubuntu and Windows on the same computer. This is the end of the traceback: bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- : unsupported hash version: 9') > >> Does someone have an alternative to Shelve? >> It must be able to contain keys and values, and the values are a list >> of dictionaries. > > A SqlLite database? > With record where the fields correspond to your dictionary entries. > > ID:Date:Location > >> 1234567 = [{'date' : '2009-05-11', 'location' : 'Germany'}, {'date' : >> '2009-05-04', 'location' : 'France'}] >> 7654321 = [{'date' : '2009-03-12', 'location' : 'Belgium'}, {'date' : >> '2009-04-23', 'location' : 'Spain'}] > > populating your dictionary becomes a matter of extracting the data: > > SELECT DATE, LOCATION FROM DATA WHERE ID = key > > SQLite has the advantage that the atabase is a single file and > it is portable across platforms provided they have Sqlite installed. > And its part of the Python standard library. > > > Examples of using Sqlite in the databases topic of my tutorial. Thank you. I will check the tutorial. Timo > > HTH, > > From denis.spir at free.fr Tue May 12 09:59:19 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 09:59:19 +0200 Subject: [Tutor] How to set up an Array? In-Reply-To: <200109.40341.qm@web65513.mail.ac4.yahoo.com> References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> Message-ID: <20090512095919.48db4654@o> Le Mon, 11 May 2009 19:09:30 -0700 (PDT), nickel flipper s'exprima ainsi: > > Hello, > Just getting started with Python, and have had some early, although trivial > success. It looks like just the ticket to parse a data file. Total noob at > this, but really kind of overwhelmed by all the options. Looking for some > direction on which modules, librarys, or ? to accomplish the goal. > > To date have been using conditional type statement to tease out > information. Now I need to match some strings, put them in an array or > list? and read them back after X number of lines. The linesplits are in a > predictable pattern and padded by '-' to get eight values or arrays that > will be joined together after a set condition is no longer true. > > So from the data set below, will be looking to print out: > RA7,OSC1,CLKI > RA6,OSC2 > RA5,AN4,nSS1,LVDIN,RCV,RP2 > ETC. > > Any tips greatly appreciated. Thanks. > > > sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') > reset (por='xxxxxxxx' mclr='uuuuuuuu') > bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') > bit (tag=scl names='RA' width='8') > bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') > bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 > 1 1 1 1 1 1 1') bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' > width='1 1 1 1 1 1 1 1') bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 > 1 1 1 1 1 1') bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1') > > Here is some noob code used to pick out the RA7, RA6, RA5 etc. Comments > welcome. > > if line.startswith ( " bit (names='R" ): > (a, pin7, pin6, pin5, pin4, pin3, pin2, pin1, pin0) = (line.split() > + ["", ""])[:9] startpin = pin7[8:3] > if startpin.startswith == "RA" or "RB" or "RC" or "RD" or "RE" or > "RF" or "RG" or "RH" or "RJ": print pin7[8:] + "," + pin6 + "," + pin5 + > "," + pin4 + "," + pin3 + "," + pin2 + "," + pin1 + "," + pin0[:3] + "\n" > else: pass Hem, rather complicated. I would use a regex for once in this precise case. But forget it if you're not used to regexes, rather take the opportunity to learn python builtin strings methods better. Anyway, as a step on the regex path, the following code s = """\ sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') reset (por='xxxxxxxx' mclr='uuuuuuuu') bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') bit (tag=scl names='RA' width='8') bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1') bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1') bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1') bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1')""" from re import compile as Pattern p = Pattern(r"names='((?:(?:\w+|-) ?){8})'") r = p.findall(s) print r outputs ['RA7 RA6 RA5 - RA3 RA2 RA1 RA0', 'OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0', 'CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA', '- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6', '- - RCV - - C2INB RP1 RP0', '- - RP2 - - - - -'] As you see the pattern is hardly legible. It matches a sequence of 8 'things' inside "names='...'. Each 'thing' is either an alphanumeric string or '-', possibly followed by a space. (Actually it's not fully correct.) We have to use twice so-called non-grouping parentheses "(?:...)". What is actually captured is inside simple (...). Denis ------ la vita e estrany From timomlists at gmail.com Tue May 12 10:00:54 2009 From: timomlists at gmail.com (Timo) Date: Tue, 12 May 2009 10:00:54 +0200 Subject: [Tutor] Alternative for Shelve In-Reply-To: <1c2a2c590905111633q5c047853r1438d4ccef3aad87@mail.gmail.com> References: <4A0862C8.2000002@gmail.com> <1c2a2c590905111633q5c047853r1438d4ccef3aad87@mail.gmail.com> Message-ID: <4A092CB6.7070504@gmail.com> Kent Johnson schreef: > On Mon, May 11, 2009 at 1:39 PM, Timo wrote: > >> Hello all, >> >> I have an issue with the Shelve module. It works great for my needs, the >> only problem is that a file made with Shelve isn't interchangable between >> different computers. I want my data to be exchanged between users. >> >> Does someone have an alternative to Shelve? >> It must be able to contain keys and values, and the values are a list of >> dictionaries. >> > > Try the pickle module. > > Kent > I went from Pickle to Shelve because I couldn't store my wanted list of dictionaries in Pickle. Timo From thegreentealeaf at gmail.com Tue May 12 10:05:00 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 10:05:00 +0200 Subject: [Tutor] Calling method in parent class Message-ID: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> Hi, I've started to learn Python and I'm a bit confused over how to call a method in a parent class. Assume I have: class Parent(object): def somemethod( self, bla ): print 'Parent',bla I then create a child class that want to call somemethod. As I understand it I can either do it like this class Child(Parent): def somemethod( self, bla ): Parent.somemethod(self,bla) or like this class Child(Parent): def somemethod( self, bla ): super(Child,self).somemethod(bla) The first version seem to have the obvious disadvantage that I need to know the name of the parent class when I write the call, so I thought that the second version was the "proper" way of doing it. But when doing some research on the web it seem like the second version also have some problems. My question is simple: what is the "best" way of doing this and why? Or should I mix both these approaches? -- The Green Tea Leaf ? thegreentealeaf at gmail.com ? thegreentealeaf.blogspot.com From nickelflipper at yahoo.com Tue May 12 10:21:18 2009 From: nickelflipper at yahoo.com (nickel) Date: Tue, 12 May 2009 08:21:18 +0000 (UTC) Subject: [Tutor] How to set up an Array? References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> <5e58f2e40905112005o59ab0fe7u23f3cf7c023b5301@mail.gmail.com> Message-ID: John Fouhy fouhy.net> writes: > Hmm, hairy! > Thank you John for your quick reply. It looks like its going to take a bit to digest what you have written. Back to the Python 2.6 docs and the net. Had actually read the expressions section earlier in the day, but did not see how to apply to said problem. Will post progress ASAP. P.S. will take a gander at the parsers too. From dotancohen at gmail.com Tue May 12 10:26:00 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Tue, 12 May 2009 11:26:00 +0300 Subject: [Tutor] Considering translating bash script to Python to learn Message-ID: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> I am considering translating a homegrown bash script to Python to learn the language. The script grabs different specific pages of either a ODF or PDF file (I can use either as the input file, based on Python's abilities), combines it with an HTML file, rotates, rearranges, then sends the whole thing to the printer. However, I seem to depend on many bash-specific functions (os programs) and I cannot find Python equivalents. Here are some lines of the script, and the relevant questions: gnome-web-print --mode=print --files $FILE /tmp/weeklyCalendar.pdf Here, I am converting an HTML file to PDF. I rely on the external program gnome-web-print to do the conversion. Does Python have a native way to handle this, or would I just wind up calling gnome-web-print from within python? pdftops -f 2 -l 2 $HOME/.bin/todo/todo.odf.pdf /tmp/weeklyTodo.ps Here I am exporting the second page of a PDF file as a PS file. Actually, the file in question is a hybrid ODF-PDF file and if Python has a way of exporting a specific page of a ODF file that is fine too. I could export to PDF as well, the important bit is too isolate a single page of either an ODF or PDF file and have it as either a PS or PDF. gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/tmp/weeklyCalendarPrintMe.pdf $HOME/.bin/todo/weeklyCalendar-blank.pdf /tmp/weeklyCalendar.pdf /tmp/weeklyTodo.ps $HOME/.bin/todo/weeklyCalendar-blank.pdf Here, I am combining several PDF and PS files as a single PDf file. Can Python do this? lpr -P printer -o number-up=2 /tmp/weeklyCalendarPrintMe.pdf Here I am sending the final file to the printer, and having it print 2 pages on each page. Again, is this something that Python can handle internally? Thanks for the help. I know that Python is not specifically designed to manipulate PDF files, but as the script is getting more complex and performs logic in other sections (not shown here) I would like to take advantage of other Python properties. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From mandel at themacaque.com Tue May 12 10:33:21 2009 From: mandel at themacaque.com (mandel at themacaque.com) Date: Tue, 12 May 2009 09:33:21 +0100 (BST) Subject: [Tutor] Import package module problem In-Reply-To: <20090512100430.539ba2c5@o> References: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> <20090512090150.704c8712@o> <4886.194.29.97.144.1242113847.squirrel@ip-208-109-127-191.ip.secureserver.net> <20090512100430.539ba2c5@o> Message-ID: <22880.194.29.97.144.1242117201.squirrel@ip-208-109-127-191.ip.secureserver.net> > Le Tue, 12 May 2009 08:37:27 +0100 (BST), > mandel at themacaque.com s'exprima ainsi: > >> Lets see if I understand what you mean, in my example I should add an >> import inside __init__.py to be able to use relative imports: >> >> /general >> __init__.py >> from address_book import AddressBook >> address_book.py >> /groups >> __init__.py >> from contact_group import ContactGroup >> contact_group.py >> >> Lets say I have the above, would I be able to use the following? >> >> from ..groups.contact_group import ContactGroup >> >> I'm trying to use this type of import to be able to bundle my unit tests >> with my code and let devels work with it. I have read somewhere that >> __all__ can do the trick but it did not say how. > > Have you tried it? > > Denis > ------ > la vita e estrany > Yes I have tried and I keep getting the following ValueError: ValueError: Attempted relative import in non-package I have tried with my code, do you have an example or yours that works that way? I might have not done it worng (I hope I haven't) Kr, Manuel From jeremiah.dodds at gmail.com Tue May 12 10:26:20 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 12 May 2009 09:26:20 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> Message-ID: <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> On Tue, May 12, 2009 at 9:05 AM, The Green Tea Leaf < thegreentealeaf at gmail.com> wrote: > Hi, > I've started to learn Python and I'm a bit confused over how to call a > method in a parent class. Assume I have: > > class Parent(object): > def somemethod( self, bla ): > print 'Parent',bla > > I then create a child class that want to call somemethod. As I > understand it I can either do it like this > > class Child(Parent): > def somemethod( self, bla ): > Parent.somemethod(self,bla) > > or like this > > class Child(Parent): > def somemethod( self, bla ): > super(Child,self).somemethod(bla) > > The first version seem to have the obvious disadvantage that I need to > know the name of the parent class when I write the call, so I thought > that the second version was the "proper" way of doing it. But when > doing some research on the web it seem like the second version also > have some problems. > > My question is simple: what is the "best" way of doing this and why? > Or should I mix both these approaches? > > -- > Assuming you don't have the same method in Child, just use self.somemethod: In [2]: class Parent(object): ...: def foo(self): ...: print "in parent foo" ...: In [3]: class Child(Parent): ...: pass ...: In [4]: x = Child() In [5]: x.foo Out[5]: > In [6]: x.foo() in parent foo In [7]: class Child(Parent): ...: def bar(self): ...: self.foo() ...: print "in child bar" ...: In [8]: x = Child() In [9]: x.bar() in parent foo in child bar -------------- next part -------------- An HTML attachment was scrubbed... URL: From thegreentealeaf at gmail.com Tue May 12 10:55:18 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 10:55:18 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> Message-ID: <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> OK, bad example. But assume I have the same method in both classes and want to call the method in the parent. On Tue, May 12, 2009 at 10:26, Jeremiah Dodds wrote: > > > On Tue, May 12, 2009 at 9:05 AM, The Green Tea Leaf > wrote: >> >> Hi, >> I've started to learn Python and I'm a bit confused over how to call a >> method in a parent class. Assume I have: >> >> class Parent(object): >> ? ?def somemethod( self, bla ): >> ? ? ? ?print 'Parent',bla >> >> I then create a child class that want to call somemethod. As I >> understand it I can either do it like this >> >> class Child(Parent): >> ? ?def somemethod( self, bla ): >> ? ? ? ?Parent.somemethod(self,bla) >> >> or like this >> >> class Child(Parent): >> ? ?def somemethod( self, bla ): >> ? ? ? ?super(Child,self).somemethod(bla) >> >> The first version seem to have the obvious disadvantage that I need to >> know the name of the parent class when I write the call, so I thought >> that the second version was the "proper" way of doing it. But when >> doing some research on the web it seem like the second version also >> have some problems. >> >> My question is simple: what is the "best" way of doing this and why? >> Or should I mix both these approaches? >> >> -- > > Assuming you don't have the same method in Child, just use self.somemethod: > > > In [2]: class Parent(object): > ?? ...:???? def foo(self): > ?? ...:???????? print "in parent foo" > ?? ...: > > In [3]: class Child(Parent): > ?? ...:???? pass > ?? ...: > > In [4]: x = Child() > > In [5]: x.foo > Out[5]: > > > In [6]: x.foo() > in parent foo > > In [7]: class Child(Parent): > ?? ...:???? def bar(self): > ?? ...:???????? self.foo() > ?? ...:???????? print "in child bar" > ?? ...: > > In [8]: x = Child() > > In [9]: x.bar() > in parent foo > in child bar > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- The Green Tea Leaf thegreentealeaf at gmail.com thegreentealeaf.blogspot.com From jeremiah.dodds at gmail.com Tue May 12 11:11:24 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 12 May 2009 10:11:24 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> Message-ID: <12cbbbfc0905120211h6f7f2bday4553d7c222067692@mail.gmail.com> On Tue, May 12, 2009 at 9:55 AM, The Green Tea Leaf < thegreentealeaf at gmail.com> wrote: > OK, bad example. But assume I have the same method in both classes and > want to call the method in the parent. > > Can you give a concrete example of _why_ you would want to do this? You can use super, if you really want to, but it can get ugly (I do not fully understand all of supers caveats). I can't think of a case off the top of my head where you would want to call a parent class's method that a child instance has overriden, but I'm sure it happens sometimes. My guess is that you probably want to re-organize your classes, or not use inheritance in this instance, or rename the method in the child class. It's hard to tell without knowing what exactly you're trying to do in your code. (I mean on the end-result level, not on the "I'm trying to call a parent classes method" level. Sorry if this doesn't seem helpful, and I could be wrong here myself, but I don't think that this is something that should happen very often at all, and is probably a pretty rank code smell. -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Tue May 12 11:11:38 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 11:11:38 +0200 Subject: [Tutor] Considering translating bash script to Python to learn In-Reply-To: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> References: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> Message-ID: <20090512111138.20bea0bf@o> Le Tue, 12 May 2009 11:26:00 +0300, Dotan Cohen s'exprima ainsi: > I am considering translating a homegrown bash script to Python to > learn the language. The script grabs different specific pages of > either a ODF or PDF file (I can use either as the input file, based on > Python's abilities), combines it with an HTML file, rotates, > rearranges, then sends the whole thing to the printer. However, I seem > to depend on many bash-specific functions (os programs) and I cannot > find Python equivalents. Here are some lines of the script, and the > relevant questions: > [...] I think you should first keep these very pdf-specific tasks the way they are. Use the subprocess module to launch os commands. So that you can concentrate on translating the overall logic into python, which should not be too hard, probably. > > Thanks for the help. I know that Python is not specifically designed > to manipulate PDF files, but as the script is getting more complex and > performs logic in other sections (not shown here) I would like to take > advantage of other Python properties. > Yes, I think it's a sensible thing to do. Python will provide a clearer process logic on a higher-level, and numerous side-advantages such as builtin or library toolsets. Denis ------ la vita e estrany From thegreentealeaf at gmail.com Tue May 12 11:17:47 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 11:17:47 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <12cbbbfc0905120211h6f7f2bday4553d7c222067692@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <12cbbbfc0905120211h6f7f2bday4553d7c222067692@mail.gmail.com> Message-ID: <3753b0ed0905120217g5e92ddb6pd6c6e9dc3ac6c8b7@mail.gmail.com> I just want to know what is the best way to do this. As for an example, I would say the __init__ method where the parent class do some initialization, to be sure that everything is set up correctly I would call the parents class __init__ method before doing something else. From denis.spir at free.fr Tue May 12 11:21:38 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 11:21:38 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> Message-ID: <20090512112138.14d3d038@o> Le Tue, 12 May 2009 10:55:18 +0200, The Green Tea Leaf s'exprima ainsi: > OK, bad example. But assume I have the same method in both classes and > want to call the method in the parent. That should not happen! Basic contract is: same name = same meaning. Either you implement a method in a parent class to let all instances of child classes use it; with possible overriding in specific child classes. Or you have two different methods with different names that implement different semantics. Having two methods with the name that both need two be used on the same object is clearly a design flaw. What do you think? The only case is when the parent method performs a part of what child class methods have to do. E.g a common case for __init__: class Parent(object): def __init__(self,arg0): self.arg0 = arg0 class OneChild(Parent): def __init__(self,arg0,arg1): Parent.__init__(self,arg0) self.arg1 = arg1 one = OneChild(0,1) print one.arg0,one.arg1 ==> 0 1 Denis ------ la vita e estrany From thegreentealeaf at gmail.com Tue May 12 12:02:54 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 12:02:54 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <20090512112138.14d3d038@o> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> Message-ID: <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> > That should not happen! Basic contract is: same name = same meaning. Same meaning yes, but that doesn't mean that I can't/shouldn't reuse code that address a part of the problem. > Having two methods with the name that both need two be used on the same object is clearly a design flaw. What do you think? It think that it depends, as you write yourself if I have a method that does something that needs/should be done I think it's perfectly OK to do it (in fact that it should be done). > The only case is when the parent method performs a part of what child class methods have to do. E.g a common case for __init__: Exacly, this is the prime example (and I can't come up with another right now without inventing some convoluted example that would be really silly) > class OneChild(Parent): > ? ? ? ?def __init__(self,arg0,arg1): > ? ? ? ? ? ? ? ?Parent.__init__(self,arg0) > ? ? ? ? ? ? ? ?self.arg1 = arg1 So this is the preferred way? Not super(OneChild, self).__init__ ? -- The Green Tea Leaf thegreentealeaf at gmail.com thegreentealeaf.blogspot.com From jeremiah.dodds at gmail.com Tue May 12 12:32:34 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 12 May 2009 11:32:34 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> Message-ID: <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> On Tue, May 12, 2009 at 11:02 AM, The Green Tea Leaf < thegreentealeaf at gmail.com> wrote: > > That should not happen! Basic contract is: same name = same meaning. > > Same meaning yes, but that doesn't mean that I can't/shouldn't reuse > code that address a part of the problem. > > > If your superclass has a method with the same name (other than __init__ here), that contains some logic that a subclass that overrides the method needs, it's written wrong in python. In this case, use different method names, or factor out the parent class methods functionality into (probably) a decorator. Code reuse should be strived for, but that's not the only purpose of inheritance. If you need to override a method in a subclass, and still need to call the parents method in that subclass, you're almost definately using inheritance wrong, with the special exception of __init__. In the case of __init__, you probably want to use Parent.__init__, and not super, if only because of all the weird edge cases with super. -------------- next part -------------- An HTML attachment was scrubbed... URL: From girishmsampath at gmail.com Tue May 12 09:28:28 2009 From: girishmsampath at gmail.com (Sampath Girish) Date: Tue, 12 May 2009 12:58:28 +0530 Subject: [Tutor] Retrieving Data from Pmw.EntryFeild Message-ID: Hi friend.... This is Sam, a python learner. I have got some problem in retrieving data from a grid of Pmw.EntryFields. Now i have developed a grid of size 3X3 using two for loops. One for row and other for column. Now i Have 9 entry fields in the form of a matrix. I need to get all the entered data in the form of list one by one as soon as i press 'Submit' button. Please help me in this. Thanks, Sampath Girish M -------------- next part -------------- An HTML attachment was scrubbed... URL: From thegreentealeaf at gmail.com Tue May 12 12:39:36 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 12:39:36 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> Message-ID: <3753b0ed0905120339s4e3d30bdy900ac7fcb432ef8b@mail.gmail.com> > If your superclass has a method with the same name (other than __init__ > here), that contains some logic that a subclass that overrides the method > needs, it's written wrong in python. In this case, use different method > names, or factor out the parent class methods functionality into (probably) > a decorator. Code reuse should be strived for, but that's not the only > purpose of inheritance. If you need to override a method in a subclass, and > still need to call the parents method in that subclass, you're almost > definately using inheritance wrong, with the special exception of __init__. I think we agree on this, I can imagine that there are cases where it's desirable to have this ... but I can't come up with a good counter example so I assume this mean that we agree :) > In the case of __init__, you probably want to use Parent.__init__, and not > super, if only because of all the weird edge cases with super. Yes, it was those edge cases that I was worried about. -- The Green Tea Leaf thegreentealeaf at gmail.com thegreentealeaf.blogspot.com From kent37 at tds.net Tue May 12 12:58:56 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 06:58:56 -0400 Subject: [Tutor] Alternative for Shelve In-Reply-To: <4A092C49.8030909@gmail.com> References: <4A0862C8.2000002@gmail.com> <4A092C49.8030909@gmail.com> Message-ID: <1c2a2c590905120358p2da6a2bfpd90b1fb7c2c74678@mail.gmail.com> On Tue, May 12, 2009 at 3:59 AM, Timo wrote: > Alan Gauld schreef: >> >> "Timo" wrote >> >>> I have an issue with the Shelve module. It works great for my needs, the >>> only problem is that a file made with Shelve isn't interchangable between >>> different computers. >> >> I thought it would be. >> What kind of computers are you having issues with? >> And what kind of issues? > > Currently only tested between Ubuntu and Windows on the same computer. > This is the end of the traceback: > bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- : > unsupported hash version: 9') shelve relies on the anydbm module for storage. anydbm may use any one of several actual database implementations (dbhash, gdbm, dbm or dumbdbm) depending on what is available. I guess you have different modules available on the two machines. You can find out which module is being used like this: In [7]: import anydbm In [8]: anydbm._defaultmod Out[8]: You could force the use of a common db module by opening the file explicitly. For example, to use dumbdbm, which is slow, simple and guaranteed to be available, something like this (untested) should work: import dumbdbm from shelve import Shelf shelf = Shelf(dumbdbm.open('myfile', 'c')) Kent From kent37 at tds.net Tue May 12 12:59:19 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 06:59:19 -0400 Subject: [Tutor] Alternative for Shelve In-Reply-To: <4A092CB6.7070504@gmail.com> References: <4A0862C8.2000002@gmail.com> <1c2a2c590905111633q5c047853r1438d4ccef3aad87@mail.gmail.com> <4A092CB6.7070504@gmail.com> Message-ID: <1c2a2c590905120359m5944fe76qccc9c523dd1deb62@mail.gmail.com> On Tue, May 12, 2009 at 4:00 AM, Timo wrote: > Kent Johnson schreef: >> Try the pickle module. > > I went from Pickle to Shelve because I couldn't store my wanted list of > dictionaries in Pickle. What was the problem? You should be able to create a dict whose values are lists of dicts. This would correspond to the dict-like object you get from shelve. Then the whole dict could be pickled. For example: In [10]: import pickle In [11]: d=dict() In [12]: d[1234567] = [{'date' : '2009-05-11', 'location' : 'Germany'}, {'date' : '2009-05-04', 'location' : 'France'}] In [13]: d[7654321] = [{'date' : '2009-03-12', 'location' : 'Belgium'}, {'date' : '2009-04-23', 'location' : 'Spain'}] In [14]: s = pickle.dumps(d) In [15]: s Out[15]: "(dp0\nI7654321\n(lp1\n(dp2\nS'date'\np3\nS'2009-03-12'\np4\nsS'location'\np5\nS'Belgium'\np6\nsa(dp7\ng3\nS'2009-04-23'\np8\nsg5\nS'Spain'\np9\nsasI1234567\n(lp10\n(dp11\ng3\nS'2009-05-11'\np12\nsg5\nS'Germany'\np13\nsa(dp14\ng3\nS'2009-05-04'\np15\nsg5\nS'France'\np16\nsas." In [16]: d2 = pickle.loads(s) In [17]: d2 Out[17]: {1234567: [{'date': '2009-05-11', 'location': 'Germany'}, {'date': '2009-05-04', 'location': 'France'}], 7654321: [{'date': '2009-03-12', 'location': 'Belgium'}, {'date': '2009-04-23', 'location': 'Spain'}]} Kent From kent37 at tds.net Tue May 12 13:27:52 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 07:27:52 -0400 Subject: [Tutor] Calling method in parent class In-Reply-To: <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> Message-ID: <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> On Tue, May 12, 2009 at 6:32 AM, Jeremiah Dodds wrote: > If your superclass has a method with the same name (other than __init__ > here), that contains some logic that a subclass that overrides the method > needs, it's written wrong in python. In this case, use different method > names, or factor out the parent class methods functionality into (probably) > a decorator. Code reuse should be strived for, but that's not the only > purpose of inheritance. If you need to override a method in a subclass, and > still need to call the parents method in that subclass, you're almost > definately using inheritance wrong, with the special exception of __init__. I don't agree with this at all. It's not at all unusual for a derived class to override a base class method in order to add additional functionality to it, then to call the base class method to complete the implementation. __init__() is the most common example but not the only one. I don't consider this a design flaw at all. Another way to do this is for the base class method to call a hook method that subclasses can define but IMO that is often more trouble than it is worth. I don't see how you would use a decorator to do this. > In the case of __init__, you probably want to use Parent.__init__, and not > super, if only because of all the weird edge cases with super. super() is intended to solve problems that arise when using multiple inheritance. It has to be used carefully and consistently. Personally I use the Parent.method(self) style. Kent From kent37 at tds.net Tue May 12 13:30:10 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 07:30:10 -0400 Subject: [Tutor] Retrieving Data from Pmw.EntryFeild In-Reply-To: References: Message-ID: <1c2a2c590905120430x609a3ba8x679adeb6ffef57ae@mail.gmail.com> On Tue, May 12, 2009 at 3:28 AM, Sampath Girish wrote: > Hi friend.... > ?? ? ? ? ? ? ? ?This is Sam, a python learner. I have got some problem in > retrieving data from a grid of Pmw.EntryFields. Now i have developed a grid > of size 3X3 using two for loops. One for row and other for column. Now i > Have 9 entry fields in the form of a matrix. I need to get all the entered > data in the form of list one by one as soon as i press 'Submit' button. > Please help me in this. You can use two for loops again. I can't be specific without seeing your code but something like this: result = [] for i in range(3): for j in range(3): value = result.append(value) Kent From kent37 at tds.net Tue May 12 13:16:56 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 07:16:56 -0400 Subject: [Tutor] How to set up an Array? In-Reply-To: <20090512095919.48db4654@o> References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> <20090512095919.48db4654@o> Message-ID: <1c2a2c590905120416k45fdbf58wa2c124ac5c7a3974@mail.gmail.com> On Tue, May 12, 2009 at 3:59 AM, spir wrote: > Le Mon, 11 May 2009 19:09:30 -0700 (PDT), > nickel flipper s'exprima ainsi: >> So from the data set below, will be looking to print out: >> RA7,OSC1,CLKI >> RA6,OSC2 >> RA5,AN4,nSS1,LVDIN,RCV,RP2 >> ETC. >> >> Any tips greatly appreciated. ?Thanks. >> >> >> sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') >> ? ? reset (por='xxxxxxxx' mclr='uuuuuuuu') >> ? ? bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') >> ? ? bit (tag=scl names='RA' width='8') >> ? ? bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') >> ? ? bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 >> 1 1 1 1 1 1 1') bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' >> width='1 1 1 1 1 1 1 1') bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 >> 1 1 1 1 1 1') bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1') > Hem, rather complicated. I would use a regex for once in this precise case. But forget it if you're not used to regexes, rather take the opportunity to learn python builtin strings methods better. > from re import compile as Pattern > p = Pattern(r"names='((?:(?:\w+|-) ?){8})'") That is quite a bit more complicated than needed. You want to match a string that starts with "bit (names='" and ends with the next '. You can do that like this: In [18]: s = """\ ....: sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw') ....: reset (por='xxxxxxxx' mclr='uuuuuuuu') ....: bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1') ....: bit (tag=scl names='RA' width='8') ....: bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1') ....: bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1') ....: bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1') ....: bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1') ....: bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1')""" In [19]: import re In [23]: p = re.compile(r"bit \(names='([^']+)'") In [24]: r = p.findall(s) In [25]: r Out[25]: ['RA7 RA6 RA5 - RA3 RA2 RA1 RA0', 'OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0', 'CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA', '- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6', '- - RCV - - C2INB RP1 RP0', '- - RP2 - - - - -'] Now split the matches into individual words: In [26]: r = [ i.split() for i in r ] In [27]: r Out[27]: [['RA7', 'RA6', 'RA5', '-', 'RA3', 'RA2', 'RA1', 'RA0'], ['OSC1', 'OSC2', 'AN4', '-', 'AN3', 'AN2', 'AN1', 'AN0'], ['CLKI', 'CLKO', 'nSS1', '-', 'VREF_PLUS', 'VREF_MINUS', 'C2INA', 'C1INA'], ['-', '-', 'LVDIN', '-', 'C1INB', 'CVREF_MINUS', 'PMPA7', 'PMPA6'], ['-', '-', 'RCV', '-', '-', 'C2INB', 'RP1', 'RP0'], ['-', '-', 'RP2', '-', '-', '-', '-', '-']] Transpose the list of lists: In [28]: r = zip(*r) In [29]: r Out[29]: [('RA7', 'OSC1', 'CLKI', '-', '-', '-'), ('RA6', 'OSC2', 'CLKO', '-', '-', '-'), ('RA5', 'AN4', 'nSS1', 'LVDIN', 'RCV', 'RP2'), ('-', '-', '-', '-', '-', '-'), ('RA3', 'AN3', 'VREF_PLUS', 'C1INB', '-', '-'), ('RA2', 'AN2', 'VREF_MINUS', 'CVREF_MINUS', 'C2INB', '-'), ('RA1', 'AN1', 'C2INA', 'PMPA7', 'RP1', '-'), ('RA0', 'AN0', 'C1INA', 'PMPA6', 'RP0', '-')] Filter out the '-' entries In [30]: r = [ [ t for t in i if t!='-' ] for i in r ] In [31]: r Out[31]: [['RA7', 'OSC1', 'CLKI'], ['RA6', 'OSC2', 'CLKO'], ['RA5', 'AN4', 'nSS1', 'LVDIN', 'RCV', 'RP2'], [], ['RA3', 'AN3', 'VREF_PLUS', 'C1INB'], ['RA2', 'AN2', 'VREF_MINUS', 'CVREF_MINUS', 'C2INB'], ['RA1', 'AN1', 'C2INA', 'PMPA7', 'RP1'], ['RA0', 'AN0', 'C1INA', 'PMPA6', 'RP0']] And print: In [32]: for i in r: ....: print ','.join(i) RA7,OSC1,CLKI RA6,OSC2,CLKO RA5,AN4,nSS1,LVDIN,RCV,RP2 RA3,AN3,VREF_PLUS,C1INB RA2,AN2,VREF_MINUS,CVREF_MINUS,C2INB RA1,AN1,C2INA,PMPA7,RP1 RA0,AN0,C1INA,PMPA6,RP0 Other than the blank line, this is what was wanted. You can even make it a one-liner if you want to make it really obscure ;-) In [39]: rr = [ ','.join(t for t in i if t!='-') for i in zip(*( i.split() for i in p.findall(s) )) ] In [40]: rr Out[40]: ['RA7,OSC1,CLKI', 'RA6,OSC2,CLKO', 'RA5,AN4,nSS1,LVDIN,RCV,RP2', '', 'RA3,AN3,VREF_PLUS,C1INB', 'RA2,AN2,VREF_MINUS,CVREF_MINUS,C2INB', 'RA1,AN1,C2INA,PMPA7,RP1', 'RA0,AN0,C1INA,PMPA6,RP0'] Kent From jeremiah.dodds at gmail.com Tue May 12 14:44:56 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 12 May 2009 13:44:56 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> Message-ID: <12cbbbfc0905120544x2d92db70oacafdcd6b1cf53e0@mail.gmail.com> On Tue, May 12, 2009 at 12:27 PM, Kent Johnson wrote: > On Tue, May 12, 2009 at 6:32 AM, Jeremiah Dodds > wrote: > > > If your superclass has a method with the same name (other than __init__ > > here), that contains some logic that a subclass that overrides the method > > needs, it's written wrong in python. In this case, use different method > > names, or factor out the parent class methods functionality into > (probably) > > a decorator. Code reuse should be strived for, but that's not the only > > purpose of inheritance. If you need to override a method in a subclass, > and > > still need to call the parents method in that subclass, you're almost > > definately using inheritance wrong, with the special exception of > __init__. > > I don't agree with this at all. It's not at all unusual for a derived > class to override a base class method in order to add additional > functionality to it, then to call the base class method to complete > the implementation. __init__() is the most common example but not the > only one. I don't consider this a design flaw at all. Another way to > do this is for the base class method to call a hook method that > subclasses can define but IMO that is often more trouble than it is > worth. > > I don't see how you would use a decorator to do this. > Can you give an example? I've never seen this done, outside of __init__, in a way where it wouldn't be clearer if the base class's methodname was just named something else, and then have the Parent class's eventually overriden method just call the renamed method. Put the common functionality in a method that's not overriden. Generally, I see subclasses adding methods to base classes and entirely overriding methods, and nothing more. Depending on what you need to do, you can pull out what would normally be in the base classes method into a decorator that calls the subclassess method and then does what it needs to do. I don't see this too often. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thegreentealeaf at gmail.com Tue May 12 15:24:16 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 15:24:16 +0200 Subject: [Tutor] The use of leading underscore in names Message-ID: <3753b0ed0905120624q177052a8gf395ea3e43a15b25@mail.gmail.com> The Python style guide says "Use one leading underscore only for non-public methods and instance variables." Have I understood correctly if I say that this is purely a convention and there is nothing in the language that says that it should be this way and that nothing special happens (except that it affects the 'from X import *'). And no I don't intend to break this, I just want to check that I've understood correctly. (I understand the name mangling feature of double underscore) -- The Green Tea Leaf thegreentealeaf at gmail.com thegreentealeaf.blogspot.com From kent37 at tds.net Tue May 12 15:27:30 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 09:27:30 -0400 Subject: [Tutor] Calling method in parent class In-Reply-To: <12cbbbfc0905120544x2d92db70oacafdcd6b1cf53e0@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> <12cbbbfc0905120544x2d92db70oacafdcd6b1cf53e0@mail.gmail.com> Message-ID: <1c2a2c590905120627y10bb2a1ewca842430c97829a0@mail.gmail.com> On Tue, May 12, 2009 at 8:44 AM, Jeremiah Dodds wrote: > On Tue, May 12, 2009 at 12:27 PM, Kent Johnson wrote: >> I don't agree with this at all. It's not at all unusual for a derived >> class to override a base class method in order to add additional >> functionality to it, then to call the base class method to complete >> the implementation. __init__() is the most common example but not the >> only one. I don't consider this a design flaw at all. Another way to >> do this is for the base class method to call a hook method that >> subclasses can define but IMO that is often more trouble than it is >> worth. >> >> I don't see how you would use a decorator to do this. > > Can you give an example? Sure, here are real world examples from my current work project, in C#... Many classes define protected override void Dispose(bool disposing) and call base.Dispose(disposing) in the implementation. This is a fine example; the derived class has to implement some extra Dispose() functionality and also must ensure that the base class Dispose() is called. There are 55 examples of this in my project so I would have to say it is common :-) GUI classes with custom painting or resizing behaviour call the base class method to get the common behaviour, then add their particular extra bit such as drawing a custom highlight. I have a class that overrides ToString() (the C# equivalent of __str__) to provide additional output. It calls base.ToString() to get the base class representation. Classes with serialization methods call the base class methods to serialize base class fields, then serialize their own fields. etc... > I've never seen this done, outside of __init__, in > a way where it wouldn't be clearer if the base class's methodname was just > named something else, and then have the Parent class's eventually overriden > method just call the renamed method. Put the common functionality in a > method that's not overriden. Sure, you can write class Base(object): def someMethod(self): self._someMethodImpl() def _someMethodImpl(self): # do something class Derived(Base): def someMethod(self): self._someMethodImpl() # do something else but I don't see that that gives any advantage over the simpler class Base(object): def someMethod(self): # do something class Derived(Base): def someMethod(self): Base.someMethod(self) # do something else and if you don't have control over the code for Base (perhaps it is part of a GUI framework or other third-party code) then you don't have a choice to use the first option. > Depending on what you need to do, you can pull out what would normally be in > the base classes method into a decorator that calls the subclassess method > and then does what it needs to do. I don't see this too often. Can you give an example? I still don't see how that would work or why it would be preferable to simply calling the base class method as in my second option above. Kent From kent37 at tds.net Tue May 12 15:45:47 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 09:45:47 -0400 Subject: [Tutor] The use of leading underscore in names In-Reply-To: <3753b0ed0905120624q177052a8gf395ea3e43a15b25@mail.gmail.com> References: <3753b0ed0905120624q177052a8gf395ea3e43a15b25@mail.gmail.com> Message-ID: <1c2a2c590905120645j382a664cvb9a8680f3450b16f@mail.gmail.com> On Tue, May 12, 2009 at 9:24 AM, The Green Tea Leaf wrote: > The Python style guide says > > "Use one leading underscore only for non-public methods and instance variables." > > Have I understood correctly if I say that this is purely a convention > and there is nothing in the language that says that it should be this > way and that nothing special happens (except that it affects the 'from > X import *'). And no I don't intend to break this, I just want to > check that I've understood correctly. Yes, that is correct. Kent From thegreentealeaf at gmail.com Tue May 12 15:46:27 2009 From: thegreentealeaf at gmail.com (The Green Tea Leaf) Date: Tue, 12 May 2009 15:46:27 +0200 Subject: [Tutor] The use of leading underscore in names In-Reply-To: <1c2a2c590905120645j382a664cvb9a8680f3450b16f@mail.gmail.com> References: <3753b0ed0905120624q177052a8gf395ea3e43a15b25@mail.gmail.com> <1c2a2c590905120645j382a664cvb9a8680f3450b16f@mail.gmail.com> Message-ID: <3753b0ed0905120646i75a5bba1m1ea25205c00a1907@mail.gmail.com> Thank you From jeremiah.dodds at gmail.com Tue May 12 16:16:42 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 12 May 2009 15:16:42 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <1c2a2c590905120627y10bb2a1ewca842430c97829a0@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> <12cbbbfc0905120544x2d92db70oacafdcd6b1cf53e0@mail.gmail.com> <1c2a2c590905120627y10bb2a1ewca842430c97829a0@mail.gmail.com> Message-ID: <12cbbbfc0905120716m17b24e90l4fca19ca94f719c6@mail.gmail.com> On Tue, May 12, 2009 at 2:27 PM, Kent Johnson wrote: > On Tue, May 12, 2009 at 8:44 AM, Jeremiah Dodds > wrote: > > On Tue, May 12, 2009 at 12:27 PM, Kent Johnson wrote: > > >> I don't agree with this at all. It's not at all unusual for a derived > >> class to override a base class method in order to add additional > >> functionality to it, then to call the base class method to complete > >> the implementation. __init__() is the most common example but not the > >> only one. I don't consider this a design flaw at all. Another way to > >> do this is for the base class method to call a hook method that > >> subclasses can define but IMO that is often more trouble than it is > >> worth. > >> > >> I don't see how you would use a decorator to do this. > > > > Can you give an example? > > Sure, here are real world examples from my current work project, in C#... > > Many classes define > protected override void Dispose(bool disposing) > and call > base.Dispose(disposing) > in the implementation. This is a fine example; the derived class has > to implement some extra Dispose() functionality and also must ensure > that the base class Dispose() is called. There are 55 examples of this > in my project so I would have to say it is common :-) > > GUI classes with custom painting or resizing behaviour call the base > class method to get the common behaviour, then add their particular > extra bit such as drawing a custom highlight. > > I have a class that overrides ToString() (the C# equivalent of > __str__) to provide additional output. It calls base.ToString() to get > the base class representation. > > Classes with serialization methods call the base class methods to > serialize base class fields, then serialize their own fields. > > etc... > Ahh, I stand corrected. Perhaps because the shop I work in is primarily python, and because we strongly favor composition over inheritance, I never see (python) classes being used this way. > > > I've never seen this done, outside of __init__, in > > a way where it wouldn't be clearer if the base class's methodname was > just > > named something else, and then have the Parent class's eventually > overriden > > method just call the renamed method. Put the common functionality in a > > method that's not overriden. > > Sure, you can write > > class Base(object): > def someMethod(self): > self._someMethodImpl() > > def _someMethodImpl(self): > # do something > > class Derived(Base): > def someMethod(self): > self._someMethodImpl() > # do something else > > but I don't see that that gives any advantage over the simpler > > class Base(object): > def someMethod(self): > # do something > > class Derived(Base): > def someMethod(self): > Base.someMethod(self) > # do something else > > and if you don't have control over the code for Base (perhaps it is > part of a GUI framework or other third-party code) then you don't have > a choice to use the first option. > Yes, if you don't have control over the code for Base, you don't really have a choice. Most of the time, when I see inheritance used in python, it's pretty much just classes being used as containers for common functionality. If there are two methods, one in the Base, and one in a Child, that share the same name, but not all the functionality, it will be written like so: class Base(object): def common(self): #do common stuff here def overridden(self): #do base-specific-stuff here self.common() class Child(Base): def overriden(self): #do child-specific-stuff here self.common() > > > Depending on what you need to do, you can pull out what would normally be > in > > the base classes method into a decorator that calls the subclassess > method > > and then does what it needs to do. I don't see this too often. > > Can you give an example? I still don't see how that would work or why > it would be preferable to simply calling the base class method as in > my second option above. > > Kent > Yes, but again, it's not something that I see commonly done. We have a bunch of python objects that communicate with a bunch of legacy apps via (and this is something of a simplification) HTTP. Sometimes, we need to enforce a specific delay in between POSTs or GETs for a specific app, and it turned out that there are other parts in our suite that benefit from having a delay imposed, for various reasons. Instead of putting the delay functionality into the Base class for these apps, we pulled it out into a decorator that sits in a utility module. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue May 12 16:21:47 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 10:21:47 -0400 Subject: [Tutor] Retrieving Data from Pmw.EntryFeild In-Reply-To: References: <1c2a2c590905120430x609a3ba8x679adeb6ffef57ae@mail.gmail.com> Message-ID: <1c2a2c590905120721m24e0ac68qcb9a6445fb7b5516@mail.gmail.com> On Tue, May 12, 2009 at 9:48 AM, Sampath Girish wrote: > Thank you Mr Kent for giving me reply. I've done with that this afternoon. > Now i got one more hurdle to cross. I need to take that data row wise and > add it to a grid of same size. > ?? ? ? ? For example i retrieved the entire data into a list as you said. > Now i have to add it in the form of a grid(Its widget format is > Tkinter.Label). i.e., I have to retrieve it from EntryField and place it in > Tkinter.Label. > ?? ? ? ? I got stuck at that step. Hope u got my bug and be helpful to reach > this step. So my part would be great here. This should be just another nested for loop. Inside the loop you can read from the EntryField and write to the Label. Kent PS Please Reply All to reply to the list. From eike.welk at gmx.net Tue May 12 16:27:39 2009 From: eike.welk at gmx.net (Eike Welk) Date: Tue, 12 May 2009 16:27:39 +0200 Subject: [Tutor] Import package module problem In-Reply-To: <4809.194.29.97.144.1242114172.squirrel@ip-208-109-127-191.ip.secureserver.net> References: <51807.87.64.149.91.1242074649.squirrel@ip-208-109-127-191.ip.secureserver.net> <4A0921DC.9020704@tue.nl> <4809.194.29.97.144.1242114172.squirrel@ip-208-109-127-191.ip.secureserver.net> Message-ID: <200905121627.39845.eike.welk@gmx.net> Hello Manuel! On Tuesday 12 May 2009, mandel at themacaque.com wrote: > I have actually rearranged my code to have such hierarchy which > obviously works. The problem is that I want ot be able to perform > unit tests withinn the package of the code. I keep having the > problem Use a test discovery program like py.test or nose. These programs start the test scripts with the right sys.path so that they can find their libraries. py.test: http://codespeak.net/py/dist/test/test.html nose: http://code.google.com/p/python-nose/ I've had similar problems with executing tests, and I have posted a short description how to use py.test to this list. http://www.nabble.com/testing-framework-td23196352.html Kind regards, Eike. From dotancohen at gmail.com Tue May 12 17:26:25 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Tue, 12 May 2009 18:26:25 +0300 Subject: [Tutor] Considering translating bash script to Python to learn In-Reply-To: <20090512111138.20bea0bf@o> References: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> <20090512111138.20bea0bf@o> Message-ID: <880dece00905120826i52e7f50ds40464a01721ee89f@mail.gmail.com> > I think you should first keep these very pdf-specific tasks the way > they are. Use the subprocess module to launch os commands. So > that you can concentrate on translating the overall logic into python, > which should not be too hard, probably. This is what I thought, thanks. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From timomlists at gmail.com Tue May 12 18:21:11 2009 From: timomlists at gmail.com (Timo) Date: Tue, 12 May 2009 18:21:11 +0200 Subject: [Tutor] Alternative for Shelve In-Reply-To: <1c2a2c590905120358p2da6a2bfpd90b1fb7c2c74678@mail.gmail.com> References: <4A0862C8.2000002@gmail.com> <4A092C49.8030909@gmail.com> <1c2a2c590905120358p2da6a2bfpd90b1fb7c2c74678@mail.gmail.com> Message-ID: <4A09A1F7.4090503@gmail.com> Kent Johnson schreef: > On Tue, May 12, 2009 at 3:59 AM, Timo wrote: > >> Alan Gauld schreef: >> >>> "Timo" wrote >>> >>> >>>> I have an issue with the Shelve module. It works great for my needs, the >>>> only problem is that a file made with Shelve isn't interchangable between >>>> different computers. >>>> >>> I thought it would be. >>> What kind of computers are you having issues with? >>> And what kind of issues? >>> >> Currently only tested between Ubuntu and Windows on the same computer. >> This is the end of the traceback: >> bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- : >> unsupported hash version: 9') >> > > shelve relies on the anydbm module for storage. anydbm may use any one > of several actual database implementations (dbhash, gdbm, dbm or > dumbdbm) depending on what is available. I guess you have different > modules available on the two machines. You can find out which module > is being used like this: > > In [7]: import anydbm > In [8]: anydbm._defaultmod > Out[8]: '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/dbhash.pyc'> > I get the same result, only I have Python 2.6 on my Ubuntu installation and 2.5 on Windows. Maybe that is the problem. But still, then the user can't upgrade without breaking their database. Also tried: >>> import whichdb >>> whichdb.whichdb('mydb.db') 'dbhash' Same result on both. Timo > You could force the use of a common db module by opening the file > explicitly. For example, to use dumbdbm, which is slow, simple and > guaranteed to be available, something like this (untested) should > work: > > import dumbdbm > from shelve import Shelf > > shelf = Shelf(dumbdbm.open('myfile', 'c')) > > Kent > From timomlists at gmail.com Tue May 12 18:23:29 2009 From: timomlists at gmail.com (Timo) Date: Tue, 12 May 2009 18:23:29 +0200 Subject: [Tutor] Alternative for Shelve In-Reply-To: <1c2a2c590905120359m5944fe76qccc9c523dd1deb62@mail.gmail.com> References: <4A0862C8.2000002@gmail.com> <1c2a2c590905111633q5c047853r1438d4ccef3aad87@mail.gmail.com> <4A092CB6.7070504@gmail.com> <1c2a2c590905120359m5944fe76qccc9c523dd1deb62@mail.gmail.com> Message-ID: <4A09A281.3080409@gmail.com> Kent Johnson schreef: > On Tue, May 12, 2009 at 4:00 AM, Timo wrote: > >> Kent Johnson schreef: >> >>> Try the pickle module. >>> >> I went from Pickle to Shelve because I couldn't store my wanted list of >> dictionaries in Pickle. >> > > What was the problem? You should be able to create a dict whose values > are lists of dicts. This would correspond to the dict-like object you > get from shelve. Then the whole dict could be pickled. For example: > > In [10]: import pickle > > In [11]: d=dict() > > In [12]: d[1234567] = [{'date' : '2009-05-11', 'location' : > 'Germany'}, {'date' : '2009-05-04', 'location' : 'France'}] > > In [13]: d[7654321] = [{'date' : '2009-03-12', 'location' : > 'Belgium'}, {'date' : '2009-04-23', 'location' : 'Spain'}] > > In [14]: s = pickle.dumps(d) > > In [15]: s > Out[15]: "(dp0\nI7654321\n(lp1\n(dp2\nS'date'\np3\nS'2009-03-12'\np4\nsS'location'\np5\nS'Belgium'\np6\nsa(dp7\ng3\nS'2009-04-23'\np8\nsg5\nS'Spain'\np9\nsasI1234567\n(lp10\n(dp11\ng3\nS'2009-05-11'\np12\nsg5\nS'Germany'\np13\nsa(dp14\ng3\nS'2009-05-04'\np15\nsg5\nS'France'\np16\nsas." > > In [16]: d2 = pickle.loads(s) > > In [17]: d2 > Out[17]: > {1234567: [{'date': '2009-05-11', 'location': 'Germany'}, > {'date': '2009-05-04', 'location': 'France'}], > 7654321: [{'date': '2009-03-12', 'location': 'Belgium'}, > {'date': '2009-04-23', 'location': 'Spain'}]} > > Kent > Hmm, I apparently missed something when I was researching it. I got mislead by the thing that Pickle can only have one key, but if you do it like this, I can indeed store multiiple ID's. I will check this out. Thanks, Timo From alan.gauld at btinternet.com Tue May 12 18:27:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 17:27:13 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> Message-ID: "The Green Tea Leaf" wrote class Child(Parent): def somemethod( self, bla ): Parent.somemethod(self,bla) or like this class Child(Parent): def somemethod( self, bla ): super(Child,self).somemethod(bla) > The first version seem to have the obvious disadvantage that I need to > know the name of the parent class when I write the call, But since you know the name of the parent when you write the class thats seldom an issue. > that the second version was the "proper" way of doing it. But when > doing some research on the web it seem like the second version also > have some problems. This has been much improved in Python v3 but the issues with super in v2 are such that I usually recommend the explicit call (option 1) > My question is simple: what is the "best" way of doing this and why? The one that works for you. In my case its option 1 because its explicit and therefore clear what exactly I'm calling. > Or should I mix both these approaches? No, do not mix them, that way leads to madness IMHO! :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 12 18:37:05 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 17:37:05 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <12cbbbfc0905120211h6f7f2bday4553d7c222067692@mail.gmail.com> Message-ID: "Jeremiah Dodds" wrote > Can you give a concrete example of _why_ you would want to do this? You > can > use super, if you really want to, but it can get ugly (I do not fully > understand all of supers caveats). I can't think of a case off the top of > my > head where you would want to call a parent class's method that a child > instance has overriden, but I'm sure it happens sometimes. Actually I'd say this was the normal case in good OO programming. You should program the differences so that mostly you are writing either some extra initialisation or some local post processing with the bulk of the code in the base class. __init__ is the obvious case where you simply call the base class to initialise the inherited attributes then add on the local attributes in the child class. This ripples all the way up the inheritance chain. Another similar case is where you are serialising an object. The child class calls the parent class to serialise all of the inherited stuff and then tags on the child features at the end. To deserialize simply reverse the process. Lisp makes this explicit by having before and after methods which are automatically called. Thus for method foo the order is before_foo() # ripples up the tree calling all the precondition code foo() # executes all the core code, usually only in 1 or 2 classes after_foo() # ripples up tree calling all the post condition code We can fake this in other languages with a naming convention such as the one above but we still need to do a fair bit of explicit coding. Lisp does it automagically. (You might be able to do something like it in Python using decorators....hmmmm.) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue May 12 18:40:12 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 18:40:12 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> <1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com> Message-ID: <20090512184012.03c57bbd@o> Le Tue, 12 May 2009 07:27:52 -0400, Kent Johnson s'exprima ainsi: > I don't agree with this at all. It's not at all unusual for a derived > class to override a base class method in order to add additional > functionality to it, then to call the base class method to complete > the implementation. __init__() is the most common example but not the > only one. I don't consider this a design flaw at all. Aside __init__, it's also common when overloading operators. One often needs the logic implemented in the base class to process the operation on instances of sub classes. There's also the case of delegation. Denis ------ la vita e estrany From alan.gauld at btinternet.com Tue May 12 18:43:24 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 17:43:24 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> Message-ID: "spir" wrote >> OK, bad example. But assume I have the same method in both classes and >> want to call the method in the parent. > > That should not happen! Basic contract is: same name = same meaning. Nope, its called polymorphism. The semantics may be the same but the implementation detail may differ and more specifically the child implementation is likely to be a slight change on the parent. Its the calling of the parent code that keeps the essential semantics consistent. > Either you implement a method in a parent class to let all instances > of child classes use it; Thats what he is doing, except that in the overriding metjod he wants to reuse the inherited functionality. > Having two methods with the name that both need two be used > on the same object is clearly a design flaw. What do you think? Two methods only one message. It is what polymorphism is all about. > The only case is when the parent method performs a part of > what child class methods have to do. Exactly and in my experience of OO inheritance thats what happens most of the time. Think: foo.draw(), save(), write(), print(), copy(), read(), move(), etc All of these operations will likely use the superclass functionality and add a little bit of local processing for any attributes in the child class. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 12 18:45:52 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 17:45:52 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com><20090512112138.14d3d038@o><3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com> <12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com> Message-ID: "Jeremiah Dodds" wrote >> > That should not happen! Basic contract is: same name = same meaning. >> >> Same meaning yes, but that doesn't mean that I can't/shouldn't reuse >> code that address a part of the problem. >> > If your superclass has a method with the same name (other than __init__ > here), that contains some logic that a subclass that overrides the method > needs, it's written wrong in python. No its not, this is standard OOP design in any OO language. Polymorphism is where the power of OOP reallly comes into play. If you have to write all of the code for every overridden method then I'd say that was bad design. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 12 18:52:52 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 17:52:52 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com><20090512112138.14d3d038@o><3753b0ed0905120302ncf1026keee6faaf2eb157da@mail.gmail.com><12cbbbfc0905120332m1792c780uffe4178892a9425@mail.gmail.com><1c2a2c590905120427tdbdd632w661338fb0de93e4f@mail.gmail.com><12cbbbfc0905120544x2d92db70oacafdcd6b1cf53e0@mail.gmail.com><1c2a2c590905120627y10bb2a1ewca842430c97829a0@mail.gmail.com> <12cbbbfc0905120716m17b24e90l4fca19ca94f719c6@mail.gmail.com> Message-ID: "Jeremiah Dodds" wrote > Ahh, I stand corrected. Perhaps because the shop I work in is primarily > python, and because we strongly favor composition over inheritance, I > never > see (python) classes being used this way. Composition over inheritance has now become so overused it is losing one of the biggest adbvantages of OOP. That concept was introduced because in the early days of OOP adoption (early 90s) people were grossly abusing inheritance to reduce coding but then causing horrible problems with maintenance and reuse. But inheritance is still the best mechanism when the "is-a" relationship test is valid. Widgets in a GUI heirarchy are good examples, as are bank acounts, data streams, etc > Most of the time, when I see inheritance used in python, it's pretty much > just classes being used as containers for common functionality. Sadly thats often the case but its not really OO programming, its just programming with objects! The hysteria over abuse of inheritance has kind of thrown the baby out with the bathwater IMHO! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 12 19:01:28 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 18:01:28 +0100 Subject: [Tutor] Considering translating bash script to Python to learn References: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> Message-ID: "Dotan Cohen" wrote >I am considering translating a homegrown bash script to Python to > learn the language. Thats rarely a good approach. While you can replace bash with Python you will just wind up calling a bunch of external programs and thats what shell scripts are best at. The only time its worthwhile is where the bash code is structurally complex with lots of loops and conditionals and local state. > rearranges, then sends the whole thing to the printer. However, I seem > to depend on many bash-specific functions (os programs) and I cannot > find Python equivalents. Here are some lines of the script, and the > relevant questions: The key is what you say here. The core code depends on os programs (not bash functions!). Python can probably replace some of those os programs but why bother unless you need to retire them or are paying money for them? > gnome-web-print --mode=print --files $FILE /tmp/weeklyCalendar.pdf > Here, I am converting an HTML file to PDF. I rely on the external > program gnome-web-print to do the conversion. Does Python have a > native way to handle this, or would I just wind up calling > gnome-web-print from within python? You could write a program to do this in python but it would be bigger than your entire bash script replacement. > pdftops -f 2 -l 2 $HOME/.bin/todo/todo.odf.pdf /tmp/weeklyTodo.ps > Here I am exporting the second page of a PDF file as a PS file. > Actually, the file in question is a hybrid ODF-PDF file and if Python > has a way of exporting a specific page of a ODF file that is fine too. > I could export to PDF as well, the important bit is too isolate a > single page of either an ODF or PDF file and have it as either a PS or > PDF. Same here although ReportLab (a non standard lib Python module) may have some helper functions for converting PDF to ps. > gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite > -sOutputFile=/tmp/weeklyCalendarPrintMe.pdf > $HOME/.bin/todo/weeklyCalendar-blank.pdf /tmp/weeklyCalendar.pdf > /tmp/weeklyTodo.ps $HOME/.bin/todo/weeklyCalendar-blank.pdf > Here, I am combining several PDF and PS files as a single PDf file. > Can Python do this? Again you probably could since PDF and PS are both text like formats internally but it would be quite a lot of work. > lpr -P printer -o number-up=2 /tmp/weeklyCalendarPrintMe.pdf > Here I am sending the final file to the printer, and having it print 2 > pages on each page. Again, is this something that Python can handle > internally? This is so OS and local environment specific that Python usually just delegates this to lpr in my experience! > Thanks for the help. I know that Python is not specifically designed > to manipulate PDF files, but as the script is getting more complex and > performs logic in other sections (not shown here) I would like to take > advantage of other Python properties. For PDF you should look at ReportLab. It is really designed for creating PDFs from non PDF data (eg out of a database or with graphics etc). Combined with a html parser such as Beautiful Soup that aspect may be worth converting to Python. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue May 12 19:23:53 2009 From: denis.spir at free.fr (spir) Date: Tue, 12 May 2009 19:23:53 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> Message-ID: <20090512192353.10f28559@o> Le Tue, 12 May 2009 17:43:24 +0100, "Alan Gauld" s'exprima ainsi: > > Having two methods with the name that both need two be used > > on the same object is clearly a design flaw. What do you think? > > Two methods only one message. It is what polymorphism is all about. Well, I do not want to argue. But this is not what I call polymorphism. Not "on the same object". Polymorphism as I know it rather dispatches depending on the object (usually it's actual type). The kind of exception beeing the case of one method calling the other (as I related later in the same post). But it still is not is calling 2 methods with the same name on the same object. It's rather one method reusing another. Denis ------ la vita e estrany From msh at blisses.org Tue May 12 19:51:36 2009 From: msh at blisses.org (Matt Herzog) Date: Tue, 12 May 2009 13:51:36 -0400 Subject: [Tutor] simply moving files Message-ID: <20090512175136.GA16490@chicago.blisses.org> On monday I posted the below code: def schmove(src,dst): ... src = '/home/datasvcs/PIG/cjomeda_exp/' ... dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' ... listOfFiles = os.listdir(src) ... for filez in listOfFiles: ... os.system("mv"+ " " + src + " " + dst) David Angel replied to my post and I guess I accidentally deleted his response. Today I found his response posted on mail-archive.com and tried some variations. They all failed. David said, "Just use os.rename() if the dest is a directory, it'll move the file there." Example: If I use: "os.rename(src, dst)" where I had "os.system("mv"+ " " + src + " " + dst)" no files are coppied. os.renames happily renames the source directory, but that's not what I want. Any other suggestions? -- Matt -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From cbc at unc.edu Tue May 12 19:34:32 2009 From: cbc at unc.edu (Chris Calloway) Date: Tue, 12 May 2009 13:34:32 -0400 Subject: [Tutor] Toronto PyCamp 2009 Message-ID: <4A09B328.3010301@unc.edu> For beginners, this ultra-low-cost Python Boot Camp developed by the Triangle Zope and Python Users Group makes you productive so you can get your work done quickly. PyCamp emphasizes the features which make Python a simpler and more efficient language. Following along by example speeds your learning process in a modern high-tech classroom. Become a self-sufficient Python developer in just five days at PyCamp! The University or Toronto Department of Physics brings PyCamp to Toronto, July 13-17, 2009. Register today at http://trizpug.org/boot-camp/pycamp-toronto-2009/ -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From david at abbottdavid.com Tue May 12 21:50:00 2009 From: david at abbottdavid.com (David) Date: Tue, 12 May 2009 15:50:00 -0400 Subject: [Tutor] simply moving files] In-Reply-To: <4A09BE6D.3050808@abbottdavid.com> References: <4A09BE6D.3050808@abbottdavid.com> Message-ID: <4A09D2E8.2010100@abbottdavid.com> David wrote: > > ------------------------------------------------------------------------ > > Subject: > [Tutor] simply moving files > From: > Matt Herzog > Date: > Tue, 12 May 2009 13:51:36 -0400 > To: > Python List > > To: > Python List > > > On monday I posted the below code: > > def schmove(src,dst): > ... src = '/home/datasvcs/PIG/cjomeda_exp/' > ... dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' > ... listOfFiles = os.listdir(src) > ... for filez in listOfFiles: > ... os.system("mv"+ " " + src + " " + dst) > > David Angel replied to my post and I guess I accidentally deleted his response. Today I found his response posted on mail-archive.com and tried some variations. They all failed. > > David said, "Just use os.rename() if the dest is a directory, it'll move the file there." > > Example: If I use: "os.rename(src, dst)" where I had "os.system("mv"+ " " + src + " " + dst)" no files are coppied. > > os.renames happily renames the source directory, but that's not what I want. > > Any other suggestions? > > -- Matt > > > I did not try Davids example but Kents works fine here; #!/usr/bin/python #Python 2.6.2 import os src = '/home/david/test_src/' dst = '/home/david/test/' listofFiles = os.listdir(src) for fnames in listofFiles: os.system("mv %s/%s %s" % (src, fnames, dst)) results = os.listdir(dst) print results [results] david [03:37 PM] opteron ~ $ ./tutor_mv.py ['fruit_loops.py', 'numberList.py', 'sumList.py'] david [03:42 PM] opteron ~ $ ls /home/david/test fruit_loops.py numberList.py sumList.py -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From dotancohen at gmail.com Tue May 12 21:57:21 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Tue, 12 May 2009 22:57:21 +0300 Subject: [Tutor] Considering translating bash script to Python to learn In-Reply-To: References: <880dece00905120126q7405bb71kcd221ea67db88221@mail.gmail.com> Message-ID: <880dece00905121257p7ce3d82o9d35b0b9caac94bb@mail.gmail.com> > Thats rarely a good approach. While you can replace bash with Python you > will just wind up calling a bunch of external programs and thats what shell > scripts are best at. That is why is has been a bash script until now. > The only time its worthwhile is where the bash code is > structurally complex with lots of loops and conditionals and local state. > That's where it is going, I am starting to have date-dependant functions and such. The bash is getting messy. >> rearranges, then sends the whole thing to the printer. However, I seem >> to depend on many bash-specific functions (os programs) and I cannot >> find Python equivalents. Here are some lines of the script, and the >> relevant questions: > > The key is what you say here. The core code depends on os programs (not bash > functions!). Python can probably replace some of those os programs but why > bother unless you need to retire them or are paying money for them? Because the direction of the script is getting complex. But I still have my reservations. >> gnome-web-print --mode=print --files $FILE /tmp/weeklyCalendar.pdf >> Here, I am converting an HTML file to PDF. I rely on the external >> program gnome-web-print to do the conversion. Does Python have a >> native way to handle this, or would I just wind up calling >> gnome-web-print from within python? > > You could write a program to do this in python but it would be bigger than > your entire bash script replacement. > I see! >> pdftops -f 2 -l 2 $HOME/.bin/todo/todo.odf.pdf /tmp/weeklyTodo.ps >> Here I am exporting the second page of a PDF file as a PS file. >> Actually, the file in question is a hybrid ODF-PDF file and if Python >> has a way of exporting a specific page of a ODF file that is fine too. >> I could export to PDF as well, the important bit is too isolate a >> single page of either an ODF or PDF file and have it as either a PS or >> PDF. > > Same here although ReportLab (a non standard lib Python module) may have > some helper functions for converting PDF to ps. > Thanks, I did see that mentioned today while googling. >> gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite >> -sOutputFile=/tmp/weeklyCalendarPrintMe.pdf >> $HOME/.bin/todo/weeklyCalendar-blank.pdf /tmp/weeklyCalendar.pdf >> /tmp/weeklyTodo.ps $HOME/.bin/todo/weeklyCalendar-blank.pdf >> Here, I am combining several PDF and PS files as a single PDf file. >> Can Python do this? > > Again you probably could since PDF and PS are both text like formats > internally but it would be quite a lot of work. > The intention was to have a library function to use, not to code a parser myself. >> lpr -P printer -o number-up=2 /tmp/weeklyCalendarPrintMe.pdf >> Here I am sending the final file to the printer, and having it print 2 >> pages on each page. Again, is this something that Python can handle >> internally? > > This is so OS and local environment specific that Python usually just > delegates this to lpr in my experience! > That is logical and what I expected to hear. >> Thanks for the help. I know that Python is not specifically designed >> to manipulate PDF files, but as the script is getting more complex and >> performs logic in other sections (not shown here) I would like to take >> advantage of other Python properties. > > For PDF you should look at ReportLab. It is really designed for creating > PDFs from non PDF data (eg out of a database or with graphics etc). Combined > with a html parser such as Beautiful Soup > that aspect may be worth converting to Python. > Thanks. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From kent37 at tds.net Tue May 12 22:15:14 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 12 May 2009 16:15:14 -0400 Subject: [Tutor] simply moving files In-Reply-To: <20090512175136.GA16490@chicago.blisses.org> References: <20090512175136.GA16490@chicago.blisses.org> Message-ID: <1c2a2c590905121315o65bc52d0vc1972bc47c04f49f@mail.gmail.com> On Tue, May 12, 2009 at 1:51 PM, Matt Herzog wrote: > On monday I posted the below code: > > def schmove(src,dst): > ... ? ? ? ? src = '/home/datasvcs/PIG/cjomeda_exp/' > ... ? ? ? ? dst = '/home/datasvcs/PIG/cjomeda_exp_archive/' > ... ? ? ? ? listOfFiles = os.listdir(src) > ... ? ? ? ? for filez in listOfFiles: > ... ? ? ? ? ? ? os.system("mv"+ " " + src + " " + dst) > > David Angel replied to my post and I guess I accidentally deleted his response. Today I found his response posted on mail-archive.com and tried some variations. They all failed. > > David said, "Just use os.rename() if the dest is a directory, it'll move the file there." > > Example: If I use: "os.rename(src, dst)" where I had "os.system("mv"+ " " + src + " " + dst)" no files are coppied. > > os.renames happily renames the source directory, but that's not what I want. You are still not using the actual file name in your command. src is a directory, so os.rename(src, dst) renames the directory. Try os.rename(os.path.join(src, filez), os.path.join(dst, filez)) Kent From nickelflipper at yahoo.com Tue May 12 23:32:00 2009 From: nickelflipper at yahoo.com (nickel) Date: Tue, 12 May 2009 21:32:00 +0000 (UTC) Subject: [Tutor] How to set up an Array? References: <200109.40341.qm@web65513.mail.ac4.yahoo.com> <20090512095919.48db4654@o> <1c2a2c590905120416k45fdbf58wa2c124ac5c7a3974@mail.gmail.com> Message-ID: Kent Johnson tds.net> writes: > In [39]: rr = [ ','.join(t for t in i if t!='-') for i in zip(*( > i.split() for i in p.findall(s) )) ] > > In [40]: rr > Out[40]: > ['RA7,OSC1,CLKI', > 'RA6,OSC2,CLKO', > 'RA5,AN4,nSS1,LVDIN,RCV,RP2', > '', > 'RA3,AN3,VREF_PLUS,C1INB', > 'RA2,AN2,VREF_MINUS,CVREF_MINUS,C2INB', > 'RA1,AN1,C2INA,PMPA7,RP1', > 'RA0,AN0,C1INA,PMPA6,RP0'] > > Kent Wow, really impressed with the reply's so far. Have compiled them all, and much to learn from each point of view. No 'one liners' for me (at the moment) though......haha:-). Seeing the Python functions, library's, syntax etc. on a real problem isinspirational to me. Much to learn and digest. With some additional conditions, Python will let me slice through the target 68k text file in no time. Thanks to all. Any future posts will be given due consideration, thanks in advance. Kent (yes, there is another one) From alan.gauld at btinternet.com Wed May 13 00:23:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 23:23:02 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com><20090512112138.14d3d038@o> <20090512192353.10f28559@o> Message-ID: "spir" wrote >> Two methods only one message. It is what polymorphism is all about. > > Well, I do not want to argue. But this is not what I call polymorphism. > Not "on the same object". Polymorphism as I know it rather dispatches > depending on the object (usually it's actual type). Yes, the two methiods are in different classes. But it is totally normal for a polymorphic method to call the code in the base class for that same method. In my experience that is more common than not doing it. It would be very suspicious to me to be overriding a method and *not* calling the base class (unless the base class was a pure abstract class - aka an interface) since that would imply that the methods were semantically different. > The kind of exception beeing the case of one method calling > the other (as I related later in the same post). But it still is not is > calling 2 methods with the same name on the same object. > It's rather one method reusing another. Yes, that would be self messaging which is a different thing altogether But calling the method of a superclass from the same method is very, very common. Bjarne Stroustrup describes this practice in "The C++ Programming Language" as: ---------- quote ------- "The cleanest solution is for the derived class to use only the public members of its base class. For example void Manager::print() const { Empoyee::print() // print Employee info cout << level; // print Manager specific info }; Note that :: must be used because print has been redefined in manager. SUCH REUSE OF NAMES IS TYPICAL --------------end quote-------------- (caps mine) Although Python is not C++ it makes no difference in this case, the principle is the same. And Grady Booch says in his OOAD book when discussing a method defined as: ------------- quote ---------- void ElectricData::send() { TelemetryData::send() // transmit the electric data } Most OO languages permit the implementation of a subclass's method to invoke a method defined in some super class. As this exampler shows IT IS COMMON for the implementation of a redefined method TO INVOKE THE METHOD OF THE SAME NAME DEFINED BY A PARENT CLASS. ....... In our experience, a developer USUALLY NEEDS TO INVOKE A SUPERCLASS METHOD EITHER JUST BEFORE OR AFTER DOING SOME OTHER ACTION. In this way subclass methods play a role in AUGMENTING THE BEHAVIOUR DEFINED IN THE SUPERCLASS. ------------- end quote ---------- (caps mine) So at least two well known OOP authorities recognise that such is common practice. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed May 13 00:27:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 May 2009 23:27:33 +0100 Subject: [Tutor] simply moving files References: <20090512175136.GA16490@chicago.blisses.org> Message-ID: "Matt Herzog" wrote > os.renames happily renames the source directory, but that's not what I > want. > > Any other suggestions? You still haven't said why you can't use shutil.move() move() uses rename if appropriate or copies/deletes if not. It makes moving much more reliable and saves you a lot of extra work checking whether the src and dst are on the same filesystem etc. You should justy be abloe to use shutil.move() What is the probnlem with this (apart from Kent's point that you were not passing filenames originally!) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Wed May 13 02:07:10 2009 From: david at abbottdavid.com (David) Date: Tue, 12 May 2009 20:07:10 -0400 Subject: [Tutor] [Fwd: Re: simply moving files]] Message-ID: <4A0A0F2E.9060308@abbottdavid.com> Forwarded to the list, I would also like to understand the forward slash; os.system("mv %s/%s %s" % (src, fnames, dst)) -- Powered by Gentoo GNU/Linux http://linuxcrazy.com -------------- next part -------------- An embedded message was scrubbed... From: Matt Herzog Subject: Re: [Tutor] simply moving files] Date: Tue, 12 May 2009 16:28:04 -0400 Size: 3318 URL: From sli1que at yahoo.com Wed May 13 02:34:18 2009 From: sli1que at yahoo.com (sli1que at yahoo.com) Date: Wed, 13 May 2009 00:34:18 +0000 Subject: [Tutor] [Fwd: Re: simply moving files]] In-Reply-To: <4A0A0F2E.9060308@abbottdavid.com> References: <4A0A0F2E.9060308@abbottdavid.com> Message-ID: <529169180-1242174821-cardhu_decombobulator_blackberry.rim.net-991548913-@bxe1213.bisx.prod.on.blackberry> Dave Slashes in linux indicate a directory path. You use them when trying to specify paths. You can also use them to specify a path to a file. Sent from my Verizon Wireless BlackBerry -----Original Message----- From: David Date: Tue, 12 May 2009 20:07:10 To: Subject: [Tutor] [Fwd: Re: simply moving files]] Forwarded to the list, I would also like to understand the forward slash; os.system("mv %s/%s %s" % (src, fnames, dst)) -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Wed May 13 09:58:56 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 08:58:56 +0100 Subject: [Tutor] simply moving files]] References: <4A0A0F2E.9060308@abbottdavid.com> Message-ID: "David" wrote > Forwarded to the list, I would also like to understand the forward slash; > os.system("mv %s/%s %s" % (src, fnames, dst)) The slash separates the file path, src, from the filename. Thus if src is /foo/bar/baz and fnames is spam then %s/%s becomes /foo/bar/baz/spam Another way of doing the same thing would be with join: '/'.join([src,fnames]) or for platform independance: os.sep.join([src,fnames]) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Wed May 13 10:40:05 2009 From: david at abbottdavid.com (David) Date: Wed, 13 May 2009 04:40:05 -0400 Subject: [Tutor] simply moving files]] In-Reply-To: References: <4A0A0F2E.9060308@abbottdavid.com> Message-ID: <4A0A8765.4070805@abbottdavid.com> Alan Gauld wrote: > > "David" wrote > >> Forwarded to the list, I would also like to understand the forward slash; >> os.system("mv %s/%s %s" % (src, fnames, dst)) > > The slash separates the file path, src, from the filename. > > Thus if src is /foo/bar/baz > and fnames is spam > > then %s/%s becomes > > /foo/bar/baz/spam > > Another way of doing the same thing would be with join: > > '/'.join([src,fnames]) > > or for platform independance: > > os.sep.join([src,fnames]) > > HTH, > Thank you Alan, you are a very good teacher, you should write a book :) -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From denis.spir at free.fr Wed May 13 10:44:34 2009 From: denis.spir at free.fr (spir) Date: Wed, 13 May 2009 10:44:34 +0200 Subject: [Tutor] Calling method in parent class In-Reply-To: References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <20090512192353.10f28559@o> Message-ID: <20090513104434.5f75fdcf@o> Le Tue, 12 May 2009 23:23:02 +0100, "Alan Gauld" s'exprima ainsi: > But calling the method of a superclass from the same method is very, > very common. [...] Yep, for sure; and I was not discussing this actually. (1) In fact, the whole exchange started when the OP asked how to call 2 different methods on the same object, one beeing defined on on its own class, the other one on the parent class. Which I still think is design fault. (2) In the same post, I pointed to the above exception, with the example of __init__. But note that it's not actually calling on the same object: (2) is not in fact a special case of (1). Then someone stated that, except for __init__, this should be considered wrong. You and Kent disagreed (and indeed I do too). But discussions on points (1) and (2) have been messed up. (I have never stated that (2) is wrong.) Denis ------ la vita e estrany From alan.gauld at btinternet.com Wed May 13 11:00:14 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 10:00:14 +0100 Subject: [Tutor] Calling method in parent class References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com><12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com><3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com><20090512112138.14d3d038@o> <20090512192353.10f28559@o> <20090513104434.5f75fdcf@o> Message-ID: "spir" wrote >> But calling the method of a superclass from the same method is very, >> very common. > > Yep, for sure; and I was not discussing this actually. > (1) In fact, the whole exchange started when the OP asked how to > call 2 different methods on the same object, one beeing defined > on on its own class, the other one on the parent class. No he was asking about calling the supercall version of the same method. Here is the post: ------------- class Child(Parent): def somemethod( self, bla ): Parent.somemethod(self,bla) ------------- It uses somemethod() in both child and parent. And that is what I am saying is a very common, and desirable, pattern. I may be coming across a bit strong on this one but it is such a fundamentally important feature of OOP that I feel on a list like tutor it is important to make it clear that this is not only correct behaviour but is very common in practice. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jeremiah.dodds at gmail.com Wed May 13 11:07:45 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 13 May 2009 10:07:45 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: <20090513104434.5f75fdcf@o> References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <20090512192353.10f28559@o> <20090513104434.5f75fdcf@o> Message-ID: <12cbbbfc0905130207s2b10dad6p45883625425bbf58@mail.gmail.com> On Wed, May 13, 2009 at 9:44 AM, spir wrote: > > > Then someone stated that, except for __init__, this should be considered > wrong. You and Kent disagreed (and indeed I do too). Yup, that was me. I was incorrect, and am now searching around for writings on proper OOP design with a python slant. Sorry for derailing the original discussion with incorrect opinions stated as fact, will be more careful in the future. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeremiah.dodds at gmail.com Wed May 13 11:10:48 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Wed, 13 May 2009 10:10:48 +0100 Subject: [Tutor] Calling method in parent class In-Reply-To: References: <3753b0ed0905120105s34a1d4a2rfd7548add107ed08@mail.gmail.com> <12cbbbfc0905120126u26924c21g8bc7c0bdc0425aa4@mail.gmail.com> <3753b0ed0905120155g257cb374i88e294049e986afd@mail.gmail.com> <20090512112138.14d3d038@o> <20090512192353.10f28559@o> <20090513104434.5f75fdcf@o> Message-ID: <12cbbbfc0905130210v139b6b95s577d2c73efbdf9e8@mail.gmail.com> On Wed, May 13, 2009 at 10:00 AM, Alan Gauld wrote: > > I may be coming across a bit strong on this one but it is such a > fundamentally important feature of OOP that I feel on a list like tutor > it is important to make it clear that this is not only correct behaviour > but is very common in practice. Thanks, Alan. I'm going to spend some time reading through the OOP section on your site, as it's apparent that you know what you're talking about, and also apparent that I have some studying to do. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jabinjezreel at gmail.com Wed May 13 05:04:20 2009 From: jabinjezreel at gmail.com (Jabin Jezreel) Date: Tue, 12 May 2009 20:04:20 -0700 Subject: [Tutor] (a, b) = (l[0:2], l[2:]) Message-ID: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> What is the idiomatic way to write the right side of (a, b) = (l[0:2], l[2:]) ? (not worried about the parens, just the splitting of the sequence) -------------- next part -------------- An HTML attachment was scrubbed... URL: From girishmsampath at gmail.com Wed May 13 11:08:51 2009 From: girishmsampath at gmail.com (Sampath Girish) Date: Wed, 13 May 2009 14:38:51 +0530 Subject: [Tutor] Retrieving Data from Pmw.EntryFeild In-Reply-To: <1c2a2c590905120721m24e0ac68qcb9a6445fb7b5516@mail.gmail.com> References: <1c2a2c590905120430x609a3ba8x679adeb6ffef57ae@mail.gmail.com> <1c2a2c590905120721m24e0ac68qcb9a6445fb7b5516@mail.gmail.com> Message-ID: Ok.... Now i have one more target to reach. I need to add 1st and 3rd fields in both the rows and place it in fourth column. Can u give me any example? On Tue, May 12, 2009 at 7:51 PM, Kent Johnson wrote: > On Tue, May 12, 2009 at 9:48 AM, Sampath Girish > wrote: > > Thank you Mr Kent for giving me reply. I've done with that this > afternoon. > > Now i got one more hurdle to cross. I need to take that data row wise and > > add it to a grid of same size. > > For example i retrieved the entire data into a list as you said. > > Now i have to add it in the form of a grid(Its widget format is > > Tkinter.Label). i.e., I have to retrieve it from EntryField and place it > in > > Tkinter.Label. > > I got stuck at that step. Hope u got my bug and be helpful to > reach > > this step. So my part would be great here. > > This should be just another nested for loop. Inside the loop you can > read from the EntryField and write to the Label. > > Kent > > PS Please Reply All to reply to the list. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed May 13 12:15:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 13 May 2009 06:15:53 -0400 Subject: [Tutor] simply moving files]] In-Reply-To: References: <4A0A0F2E.9060308@abbottdavid.com> Message-ID: <1c2a2c590905130315r36f68302s9ac7bdc8177d78f3@mail.gmail.com> On Wed, May 13, 2009 at 3:58 AM, Alan Gauld wrote: > or for platform independance: > > os.sep.join([src,fnames]) or os.path.join(src, fnames) Kent From lie.1296 at gmail.com Wed May 13 12:34:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 13 May 2009 20:34:28 +1000 Subject: [Tutor] (a, b) = (l[0:2], l[2:]) In-Reply-To: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> References: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> Message-ID: Jabin Jezreel wrote: > What is the idiomatic way to write the right side of > (a, b) = (l[0:2], l[2:]) > ? > (not worried about the parens, just the splitting of the sequence) > that way is fine. Although usually it don't use parens and 0 is omitted... a, b = l[:2], l[2:] In python, direct index access is generally avoided whenever it is still possible to use for or other constructs (but practical beats purity, choose whatever method that produce a code that looks good and performs good enough) From cwitts at compuscan.co.za Wed May 13 12:36:26 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 13 May 2009 12:36:26 +0200 Subject: [Tutor] (a, b) = (l[0:2], l[2:]) In-Reply-To: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> References: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> Message-ID: <4A0AA2AA.7070902@compuscan.co.za> Jabin Jezreel wrote: > What is the idiomatic way to write the right side of > (a, b) = (l[0:2], l[2:]) > ? > (not worried about the parens, just the splitting of the sequence) > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > What you are doing is already fine although you can drop the starting index for the first slice >>> l=[1,2,3,4,5,6,7,8,9,10] >>> l[:2], l[2:] ([1, 2], [3, 4, 5, 6, 7, 8, 9, 10]) -- Kind Regards, Christian Witts From alan.gauld at btinternet.com Wed May 13 16:23:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 15:23:03 +0100 Subject: [Tutor] Retrieving Data from Pmw.EntryFeild References: <1c2a2c590905120430x609a3ba8x679adeb6ffef57ae@mail.gmail.com><1c2a2c590905120721m24e0ac68qcb9a6445fb7b5516@mail.gmail.com> Message-ID: "Sampath Girish" wrote > Ok.... Now i have one more target to reach. I need to add 1st and 3rd > fields > in both the rows and place it in fourth column. Can u give me any > example? Can you explain what you mean? You mentioned earlier a 3x3 matrix of entry fields. So you already have a 1st field and 3rd field? And what do you mean by *both* rows? You have 3 rows? It is not clear what exactly you are asking for. It is also not clear what you don't understand. Can you give us an example of what you have done and explain how it fails? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ >> On Tue, May 12, 2009 at 9:48 AM, Sampath Girish >> wrote: >> > Thank you Mr Kent for giving me reply. I've done with that this >> afternoon. >> > Now i got one more hurdle to cross. I need to take that data row wise >> > and >> > add it to a grid of same size. >> > For example i retrieved the entire data into a list as you >> > said. >> > Now i have to add it in the form of a grid(Its widget format is >> > Tkinter.Label). i.e., I have to retrieve it from EntryField and place >> > it >> in >> > Tkinter.Label. >> >> This should be just another nested for loop. Inside the loop you can >> read from the EntryField and write to the Label. From cyberjacob at googlemail.com Wed May 13 15:08:13 2009 From: cyberjacob at googlemail.com (Jacob Mansfield) Date: Wed, 13 May 2009 14:08:13 +0100 Subject: [Tutor] saveing and loading text data Message-ID: hi everyone, I'm a bit new here but i was wondering if someone could check some of my code, it's not doing quite what it's meant to. the problem is when you start the application again and enter the databox.txt to load from thanks Databox_2_0.py: import sys, os pygame.init() def load(filename): if filename != '': e = 1 dec = "placeholder" fic = open(filename, "r") while dec != '': num = str(e) print "found " + num + " enteries" dec = fic.readline(e) databox[e] = dec dec = fic.readline((e+1)) databox2[e] = dec e = e+1 fic.close() else: return 0 def search(): print "\n" x = 1 items = len(databox) ins = items+1 while ins > x : dac = databox[x] dac2 = databox2[x] x = x + 1 print dac + " " + dac2 print "\n\n" def add(): dat = raw_input("First name.\n") dat2 = raw_input("\nSecond name.\n") items = len(databox) ins = items+1 databox[ins] = dat databox2[ins] = dat2 print "Done.\n\n" def exitprog(): fic = open('databox.txt','w') print "saveing\n" x = 1 items = len(databox) ins = items+1 while ins > x : dac = databox[x] dac2 = databox2[x] x = x + 1 fic.write(dac) fic.write(dac2) fic.close() print "goodbye" pygame.time.delay(900) exit() databox = dict() databox2 = dict() go = raw_input("filename, blank for none.\n") load(go) while True: print "Welcome to databox V2.0." print " 1. Searth the database." print " 2. Add a record." print " 3. Exit." inme = raw_input("Please make a selection.\n") if inme == "1": search() elif inme == "2": add() elif inme == "3": exitprog() else: print "input not recignised." -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 13 16:25:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 15:25:33 +0100 Subject: [Tutor] (a, b) = (l[0:2], l[2:]) References: <6f8a657a0905122004x190cc01bkbab9f28de1b5a569@mail.gmail.com> Message-ID: "Jabin Jezreel" wrote > What is the idiomatic way to write the right side of > (a, b) = (l[0:2], l[2:]) > ? > (not worried about the parens, just the splitting of the sequence) I think you have it. I'm not aware of any better way. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From cwitts at compuscan.co.za Wed May 13 17:04:11 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 13 May 2009 17:04:11 +0200 Subject: [Tutor] saveing and loading text data In-Reply-To: References: Message-ID: <4A0AE16B.8030104@compuscan.co.za> Jacob Mansfield wrote: > hi everyone, I'm a bit new here but i was wondering if someone could > check some of my code, it's not doing quite what it's meant to. the > problem is when you start the application again and enter the > databox.txt to load from > thanks > > Databox_2_0.py: > > import sys, os > pygame.init() > def load(filename): > if filename != '': > e = 1 > dec = "placeholder" > fic = open(filename, "r") > while dec != '': > num = str(e) > print "found " + num + " enteries" > dec = fic.readline(e) > databox[e] = dec > dec = fic.readline((e+1)) > databox2[e] = dec > e = e+1 > fic.close() > else: > return 0 > def search(): > print "\n" > x = 1 > items = len(databox) > ins = items+1 > while ins > x : > dac = databox[x] > dac2 = databox2[x] > x = x + 1 > print dac + " " + dac2 > print "\n\n" > > def add(): > dat = raw_input("First name.\n") > dat2 = raw_input("\nSecond name.\n") > items = len(databox) > ins = items+1 > databox[ins] = dat > databox2[ins] = dat2 > print "Done.\n\n" > def exitprog(): > fic = open('databox.txt','w') > print "saveing\n" > x = 1 > items = len(databox) > ins = items+1 > while ins > x : > dac = databox[x] > dac2 = databox2[x] > x = x + 1 > fic.write(dac) > fic.write(dac2) > fic.close() > print "goodbye" > pygame.time.delay(900) > exit() > databox = dict() > databox2 = dict() > go = raw_input("filename, blank for none.\n") > load(go) > while True: > print "Welcome to databox V2.0." > print " 1. Searth the database." > print " 2. Add a record." > print " 3. Exit." > inme = raw_input("Please make a selection.\n") > if inme == "1": > search() > elif inme == "2": > add() > elif inme == "3": > exitprog() > else: > print "input not recignised." > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Hi there, I would personally change your load method to something like this: def load(filename): try: fIn = open(filename, 'rb') except OSError: return 0 for idx, line in enumerate(fIn): rec_cnt = (idx / 2) + 1 if not idx % 2: databox[rec_cnt] = line.strip() else: databox2[rec_cnt] = line.strip() fIn.close() The problem with your original load was the fact that using file.readline( a_number ) does a readline of a specific size. You could of done it with just a .readline() which would return the next line for you. I changed the stucture of it to look cleaner, you now enumerate over the file object which returns you a counter as well as yielding the next line. The record counter will track the correct record (from your original post you had Name on one line and Surname on the following line so with that in mind it will now split it between the two dictionaries based on that. Hope that helps get you in the right direction. -- Kind Regards, Christian Witts From denis.spir at free.fr Wed May 13 17:32:13 2009 From: denis.spir at free.fr (spir) Date: Wed, 13 May 2009 17:32:13 +0200 Subject: [Tutor] cannot subclass imported type Message-ID: <20090513173213.1bb92729@o> Hello, I wanted to subclass the type Window of pyGTK for main app windows (for the obvious reason that they always contain the same init and end code) and run into an unexpected problem: ============================= import pygtk pygtk.require('2.0') import gtk # debug output print gtk print gtk.Window # app window class WApp(gkt.Window): ============================= ==> ============================= Traceback (most recent call last): File "appWindow.py", line 11, in class WApp(gkt.Window): NameError: name 'gkt' is not defined ============================= ??? Someone could explain? Denis ------ la vita e estrany From emile at fenx.com Wed May 13 17:40:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 13 May 2009 08:40:25 -0700 Subject: [Tutor] cannot subclass imported type In-Reply-To: <20090513173213.1bb92729@o> References: <20090513173213.1bb92729@o> Message-ID: On 5/13/2009 8:32 AM spir said... > Hello, > > I wanted to subclass the type Window of pyGTK for main app windows (for the obvious reason that they always contain the same init and end code) and run into an unexpected problem: > ============================= > import pygtk > pygtk.require('2.0') > import gtk > # debug output > print gtk --------GTK > print gtk.Window > > # app window > class WApp(gkt.Window): -------------GKT? > > ============================= > ==> > ============================= > > > Traceback (most recent call last): > File "appWindow.py", line 11, in > class WApp(gkt.Window): > NameError: name 'gkt' is not defined > ============================= > > ??? > Someone could explain? > > Denis > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From denis.spir at free.fr Wed May 13 17:57:57 2009 From: denis.spir at free.fr (spir) Date: Wed, 13 May 2009 17:57:57 +0200 Subject: [Tutor] cannot subclass imported type In-Reply-To: References: <20090513173213.1bb92729@o> Message-ID: <20090513175757.198260fb@o> Le Wed, 13 May 2009 08:40:25 -0700, Emile van Sebille s'exprima ainsi: > On 5/13/2009 8:32 AM spir said... > > Hello, > > > > I wanted to subclass the type Window of pyGTK for main app windows (for > > the obvious reason that they always contain the same init and end code) > > and run into an unexpected problem: ============================= import > > pygtk pygtk.require('2.0') > > import gtk > > # debug output > > print gtk > --------GTK > > > print gtk.Window > > > > # app window > > class WApp(gkt.Window): > -------------GKT? Woops! thanks, Emile! Denis ------ la vita e estrany From timomlists at gmail.com Wed May 13 18:25:49 2009 From: timomlists at gmail.com (Timo) Date: Wed, 13 May 2009 18:25:49 +0200 Subject: [Tutor] Sorting a list Message-ID: <4A0AF48D.6080002@gmail.com> Hello, I don't think this should be difficult, so maybe I look over it. But I can't seem to find the solution. I have a list with one word and a couple of numbers. Now I want the word to be kept in the first location and the numbers to be sorted. So this: [4, 6, 'word', 3, 9] should be: ['word', 3, 4, 6, 9] Timo From emile at fenx.com Wed May 13 18:41:24 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 13 May 2009 09:41:24 -0700 Subject: [Tutor] Sorting a list In-Reply-To: <4A0AF48D.6080002@gmail.com> References: <4A0AF48D.6080002@gmail.com> Message-ID: On 5/13/2009 9:25 AM Timo said... > Hello, > > I don't think this should be difficult, so maybe I look over it. But I > can't seem to find the solution. > > I have a list with one word and a couple of numbers. If this is always the case you can use the sort method of lists, then relocate the last entry to the front -- >>> a = [4, 6, 'word', 3, 9] >>> a.sort() >>> a.insert(0,a.pop()) >>> a ['word', 3, 4, 6, 9] >>> HTH, Emile > Now I want the word > to be kept in the first location and the numbers to be sorted. > > So this: > [4, 6, 'word', 3, 9] > > should be: > > ['word', 3, 4, 6, 9] > > Timo > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From vinces1979 at gmail.com Wed May 13 18:44:11 2009 From: vinces1979 at gmail.com (vince spicer) Date: Wed, 13 May 2009 10:44:11 -0600 Subject: [Tutor] Sorting a list In-Reply-To: <4A0AF48D.6080002@gmail.com> References: <4A0AF48D.6080002@gmail.com> Message-ID: <1e53c510905130944v473d01b9gd917529d218d8ecc@mail.gmail.com> you can pass sort a custom compare function Simple ex: def compare(x,y): try: return cmp(int(x), int(y)) except: pass try: int(x) except: return -1 try: int(y) except: return 1 return 0 x = [4, 6, 'word', 3, 9] x.sort(cmp=compare) >> ['word', 3,4, 6, 9] On Wed, May 13, 2009 at 10:25 AM, Timo wrote: > Hello, > > I don't think this should be difficult, so maybe I look over it. But I > can't seem to find the solution. > > I have a list with one word and a couple of numbers. Now I want the word to > be kept in the first location and the numbers to be sorted. > > So this: > [4, 6, 'word', 3, 9] > > should be: > > ['word', 3, 4, 6, 9] > > Timo > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 13 19:10:43 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 18:10:43 +0100 Subject: [Tutor] saveing and loading text data References: Message-ID: "Jacob Mansfield" wrote > hi everyone, I'm a bit new here but i was wondering if someone could > check > some of my code, it's not doing quite what it's meant to. That's not very specific. What is it meant to do? What does it actually do? Do you get any error messages? If so post them in the email. > the problem is when you start the application again and > enter the databox.txt to load from thanks With no idea of what databox.txt is that doesn't help too much either. > Databox_2_0.py: > > import sys, os > pygame.init() > def load(filename): > if filename != '': > e = 1 > dec = "placeholder" > fic = open(filename, "r") > while dec != '': This would be a lot neater using a for loop. > num = str(e) > print "found " + num + " enteries" > dec = fic.readline(e) This is almost certainly wrong. an argument to readline will read (up to) that many characters. I have no idea what you thought it would do... So in this case dec will contain 1 character the first time through, 2 characters the second time etc. > databox[e] = dec > dec = fic.readline((e+1)) And it will contain 2 then 3 characters here... > databox2[e] = dec > e = e+1 > fic.close() > else: > return 0 Returning 0 is probably not that useful? The funcvtion will return None the rest of the time. Both evaluate to False in a boolean test so code like this if not load(): print "Load failed" will always fail. If you returned a non zero value you could at least compare success and failure more easily. However it would be better still to just allow the eception to flow through IMHO! > def search(): > print "\n" > x = 1 > items = len(databox) > ins = items+1 > while ins > x : > dac = databox[x] > dac2 = databox2[x] > x = x + 1 > print dac + " " + dac2 > print "\n\n" Not sure what this is intended to do but it doesn't look like a search! > def add(): > dat = raw_input("First name.\n") > dat2 = raw_input("\nSecond name.\n") > items = len(databox) > ins = items+1 > databox[ins] = dat > databox2[ins] = dat2 > print "Done.\n\n" > def exitprog(): > fic = open('databox.txt','w') > print "saveing\n" > x = 1 > items = len(databox) > ins = items+1 > while ins > x : > dac = databox[x] > dac2 = databox2[x] > x = x + 1 > fic.write(dac) > fic.write(dac2) Note that you are not writing newline characters here so the data will all be on a single line. > fic.close() > print "goodbye" > pygame.time.delay(900) > exit() Should this be sys.exit()?? > databox = dict() > databox2 = dict() > go = raw_input("filename, blank for none.\n") > load(go) The return value from load() is irrelevant since you don't check it! > while True: > print "Welcome to databox V2.0." > print " 1. Searth the database." > print " 2. Add a record." > print " 3. Exit." > inme = raw_input("Please make a selection.\n") > if inme == "1": > search() > elif inme == "2": > add() > elif inme == "3": > exitprog() > else: > print "input not recignised." > There are a lot of things there that could be making it "not quite right". You need to give us more information if these ideas don't fix it. Alan G. From alan.gauld at btinternet.com Wed May 13 19:25:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 May 2009 18:25:16 +0100 Subject: [Tutor] Sorting a list References: <4A0AF48D.6080002@gmail.com> <1e53c510905130944v473d01b9gd917529d218d8ecc@mail.gmail.com> Message-ID: "vince spicer" wrote > def compare(x,y): > try: > return cmp(int(x), int(y)) > except: > pass > try: > int(x) > except: > return -1 > try: > int(y) > except: > return 1 > return 0 Or def compare(x,y): if type(x) == str: return -1 if type(y) == str: return 1 return cmp(x,y) Runs into problems if you have multiple strings and want them sorted too... >>> L = [4,3,'word','picture',1,2,3] >>> sorted(L, cmp=comp) ['picture', 'word', 1, 2, 3, 3, 4] >>> L = [4,3,'picture','word',1,2,3] >>> sorted(L, cmp=comp) ['word', 'picture', 1, 2, 3, 3, 4] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From ataulla at gmail.com Wed May 13 19:42:29 2009 From: ataulla at gmail.com (Ataulla S H) Date: Wed, 13 May 2009 23:12:29 +0530 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> <1e53c510905130944v473d01b9gd917529d218d8ecc@mail.gmail.com> Message-ID: >>> l = [4, 6, 'word','a', 3, 9] >>> l.sort() >>> l [3, 4, 6, 9, 'a', 'word'] >>> k = [each for each in l if type(each) == type(int())] >>> k [3, 4, 6, 9] >>> kex = [each for each in l if type(each) != type(int())] >>> kex ['a', 'word'] >>> kex+k ['a', 'word', 3, 4, 6, 9] On Wed, May 13, 2009 at 10:55 PM, Alan Gauld wrote: > > "vince spicer" wrote >> >> def compare(x,y): >> ? try: >> ? ? ? return cmp(int(x), int(y)) >> ? except: >> ? ? ? pass >> ? try: >> ? ? ? int(x) >> ? except: >> ? ? ? return -1 >> ? try: >> ? ? ? int(y) >> ? except: >> ? ? ? return 1 >> ? return 0 > > Or > > def compare(x,y): > ? ?if type(x) == str: ?return ?-1 > ? ?if type(y) == str: return 1 > ? ?return cmp(x,y) > > > Runs into problems if you have multiple strings and want them sorted too... > > >>>> L = [4,3,'word','picture',1,2,3] >>>> sorted(L, cmp=comp) > > ['picture', 'word', 1, 2, 3, 3, 4] >>>> >>>> L = [4,3,'picture','word',1,2,3] >>>> sorted(L, cmp=comp) > > ['word', 'picture', 1, 2, 3, 3, 4] > > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Ataulla SH web:www.kring.com personal blog:www.ataulla.objectis.net KRING Technologies India Pvt. Ltd. 1st Floor, Tower B, Infinity Towers, DLF II, Gurgaon-122 002 From denis.spir at free.fr Wed May 13 20:34:31 2009 From: denis.spir at free.fr (spir) Date: Wed, 13 May 2009 20:34:31 +0200 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> <1e53c510905130944v473d01b9gd917529d218d8ecc@mail.gmail.com> Message-ID: <20090513203431.51c31675@o> Le Wed, 13 May 2009 23:12:29 +0530, Ataulla S H s'exprima ainsi: > >>> k = [each for each in l if type(each) == type(int())] You can write "int" (is a type since py 2.2, I guess). assert int==type(int()) # ok Denis ------ la vita e estrany From timomlists at gmail.com Thu May 14 09:34:55 2009 From: timomlists at gmail.com (Timo) Date: Thu, 14 May 2009 09:34:55 +0200 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> Message-ID: <4A0BC99F.1090908@gmail.com> Emile van Sebille schreef: > On 5/13/2009 9:25 AM Timo said... >> Hello, >> >> I don't think this should be difficult, so maybe I look over it. But >> I can't seem to find the solution. >> >> I have a list with one word and a couple of numbers. > > If this is always the case you can use the sort method of lists, then > relocate the last entry to the front -- > > >>> a = [4, 6, 'word', 3, 9] > >>> a.sort() > >>> a.insert(0,a.pop()) > >>> a > ['word', 3, 4, 6, 9] > >>> > Thanks all for your answers. I think I will go for this solution (should have come to it myself). Thanks, Timo > HTH, > > Emile > > >> Now I want the word to be kept in the first location and the numbers >> to be sorted. >> >> So this: >> [4, 6, 'word', 3, 9] >> >> should be: >> >> ['word', 3, 4, 6, 9] >> >> Timo >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From jabinjezreel at gmail.com Thu May 14 03:36:19 2009 From: jabinjezreel at gmail.com (Jabin Jezreel) Date: Wed, 13 May 2009 18:36:19 -0700 Subject: [Tutor] t = (1, *(2, 3)) Message-ID: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> I am not allowed to do >>> t = (1, *(2, 3)) But I am allowed to do >>> def ts(*t): ...??? return t ... >>> ts(1, *(2, 3)) (1, 2, 3) I realize I can do >>> (1,) + (2,3) (1, 2, 3) What is the rationale behind not having t = (1, *(2, 3)) have the same semantics as the "ts" case above? From kent37 at tds.net Thu May 14 12:02:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 14 May 2009 06:02:53 -0400 Subject: [Tutor] Sorting a list In-Reply-To: <4A0BC99F.1090908@gmail.com> References: <4A0AF48D.6080002@gmail.com> <4A0BC99F.1090908@gmail.com> Message-ID: <1c2a2c590905140302v1537d150oa07faa1f194f086f@mail.gmail.com> On Thu, May 14, 2009 at 3:34 AM, Timo wrote: > Emile van Sebille schreef: >> If this is always the case you can use the sort method of lists, then >> relocate the last entry to the front -- >> >> >>> a = [4, 6, 'word', 3, 9] >> >>> a.sort() >> >>> a.insert(0,a.pop()) >> >>> a >> ['word', 3, 4, 6, 9] >> >>> >> > Thanks all for your answers. I think I will go for this solution (should > have come to it myself). I don't think this will be reliable. IIUC unlike objects are compared by ID, so it is not guaranteed that strings will sort after numbers. The ability to compare unlike objects at all is considered a mis-feature and has been removed in Python 3: http://mail.python.org/pipermail/python-dev/2004-June/045111.html Python 3.0.1 (r301:69556, Feb 14 2009, 22:08:17) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a = [4, 6, 'word', 3, 9] >>> a.sort() Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: str() < int() Kent From cwitts at compuscan.co.za Thu May 14 12:24:15 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Thu, 14 May 2009 12:24:15 +0200 Subject: [Tutor] Sorting a list In-Reply-To: <1c2a2c590905140302v1537d150oa07faa1f194f086f@mail.gmail.com> References: <4A0AF48D.6080002@gmail.com> <4A0BC99F.1090908@gmail.com> <1c2a2c590905140302v1537d150oa07faa1f194f086f@mail.gmail.com> Message-ID: <4A0BF14F.5060702@compuscan.co.za> Kent Johnson wrote: > On Thu, May 14, 2009 at 3:34 AM, Timo wrote: > >> Emile van Sebille schreef: >> > > >>> If this is always the case you can use the sort method of lists, then >>> relocate the last entry to the front -- >>> >>> >>>>>> a = [4, 6, 'word', 3, 9] >>>>>> a.sort() >>>>>> a.insert(0,a.pop()) >>>>>> a >>>>>> >>> ['word', 3, 4, 6, 9] >>> >> Thanks all for your answers. I think I will go for this solution (should >> have come to it myself). >> > > I don't think this will be reliable. IIUC unlike objects are compared > by ID, so it is not guaranteed that strings will sort after numbers. > The ability to compare unlike objects at all is considered a > mis-feature and has been removed in Python 3: > http://mail.python.org/pipermail/python-dev/2004-June/045111.html > > Python 3.0.1 (r301:69556, Feb 14 2009, 22:08:17) > [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > > >>>> a = [4, 6, 'word', 3, 9] >>>> a.sort() >>>> > Traceback (most recent call last): > File "", line 1, in > TypeError: unorderable types: str() < int() > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Why not be safe and generate 2 lists and merge them afterwards like: >>> numeric_list, string_list = [], [] >>> a=[4,6,'word',3,9] >>> [numeric_list.append(int(element)) if element.isdigit() else string_list.append(element) for element in map(str,a)] [None, None, None, None, None] >>> numeric_list.sort(); string_list.sort(); >>> numeric_list, string_list ([3, 4, 6, 9], ['word']) >>> string_list.extend(numeric_list) >>> string_list ['word', 3, 4, 6, 9] -- Kind Regards, Christian Witts -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 14 13:02:59 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 May 2009 12:02:59 +0100 Subject: [Tutor] t = (1, *(2, 3)) References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> Message-ID: "Jabin Jezreel" wrote > I am not allowed to do > >>> t = (1, *(2, 3)) Just to be clear, what do you think this means? What would you expect to happen? > But I am allowed to do > >>> def ts(*t): > ... return t > ... > >>> ts(1, *(2, 3)) > (1, 2, 3) What do you think is happening here that is different? > I realize I can do > >>> (1,) + (2,3) > (1, 2, 3) And how does this relate to the use of the asterisk? Is this what you expect the asterisk version above to do? > What is the rationale behind not having t = (1, *(2, 3)) > have the same semantics as the "ts" case above? That I don't know. The asterisk notation can only be used for unpacking function arguments, but why it is limited to that role I don't know...? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Thu May 14 14:07:23 2009 From: denis.spir at free.fr (spir) Date: Thu, 14 May 2009 14:07:23 +0200 Subject: [Tutor] Sorting a list In-Reply-To: <4A0BF14F.5060702@compuscan.co.za> References: <4A0AF48D.6080002@gmail.com> <4A0BC99F.1090908@gmail.com> <1c2a2c590905140302v1537d150oa07faa1f194f086f@mail.gmail.com> <4A0BF14F.5060702@compuscan.co.za> Message-ID: <20090514140723.455eb446@o> Le Thu, 14 May 2009 12:24:15 +0200, Christian Witts s'exprima ainsi: > The ability to compare unlike objects at all is considered a > mis-feature and has been removed in Python 3: > http://mail.python.org/pipermail/python-dev/2004-June/045111.html Great! Thanks for the informaton, Kent. Denis ------ la vita e estrany From lie.1296 at gmail.com Thu May 14 15:07:16 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 14 May 2009 23:07:16 +1000 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> Message-ID: Jabin Jezreel wrote: > I am not allowed to do >>>> t = (1, *(2, 3)) > > But I am allowed to do >>>> def ts(*t): > .... return t > .... >>>> ts(1, *(2, 3)) > (1, 2, 3) > > I realize I can do >>>> (1,) + (2,3) > (1, 2, 3) > > What is the rationale behind not having t = (1, *(2, 3)) > have the same semantics as the "ts" case above? In the face of ambiguity, refuse the temptation to guess? I guess because it is not clear what (1, *(2, 3)) should mean. Parentheses when used for function call has different semantic then when parentheses is used for tuple syntax. Parentheses in function is part of the calling syntax, while parentheses in tuple is used only for grouping. PS: anyway I just realized that since tuple is immutable, having (1, *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be a better example. From lie.1296 at gmail.com Thu May 14 15:13:43 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 14 May 2009 23:13:43 +1000 Subject: [Tutor] Sorting a list In-Reply-To: <4A0AF48D.6080002@gmail.com> References: <4A0AF48D.6080002@gmail.com> Message-ID: Timo wrote: > Hello, > > I don't think this should be difficult, so maybe I look over it. But I > can't seem to find the solution. > > I have a list with one word and a couple of numbers. Now I want the word > to be kept in the first location and the numbers to be sorted. > > So this: > [4, 6, 'word', 3, 9] > > should be: > > ['word', 3, 4, 6, 9] > Why not: >>> sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x)) ['word', 3, 4, 6, 9] I think sorting tuple has a well-defined meaning, i.e. sort on first item, then second, then third, etc... and there is AFAIK no restriction that the key argument should be a number right? From kent37 at tds.net Thu May 14 15:20:41 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 14 May 2009 09:20:41 -0400 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> <1e53c510905130944v473d01b9gd917529d218d8ecc@mail.gmail.com> Message-ID: <1c2a2c590905140620y46219810k7be731e7e2138d6f@mail.gmail.com> On Wed, May 13, 2009 at 1:25 PM, Alan Gauld wrote: > def compare(x,y): > ? ?if type(x) == str: ?return ?-1 > ? ?if type(y) == str: return 1 > ? ?return cmp(x,y) > > > Runs into problems if you have multiple strings and want them sorted too... A slight variation - if the types are the same, use cmp(): In [1]: def compare(x, y): ...: if type(x) == type(y): ...: return cmp(x, y) ...: if type(x) == str: return -1 ...: if type(y) == str: return 1 ...: return cmp(x, y) # This is kind of arbitrary ...: In [2]: L = [4,3,'word','picture',1,2,3] In [3]: sorted(L, cmp=compare) Out[3]: ['picture', 'word', 1, 2, 3, 3, 4] In [4]: L = [4,3,'picture','word',1,2,3] In [5]: sorted(L, cmp=compare) Out[5]: ['picture', 'word', 1, 2, 3, 3, 4] Kent From kent37 at tds.net Thu May 14 16:23:56 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 14 May 2009 10:23:56 -0400 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> Message-ID: <1c2a2c590905140723j52afc5f5r8db4274efc362ea2@mail.gmail.com> On Thu, May 14, 2009 at 9:13 AM, Lie Ryan wrote: > Why not: >>>> sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x)) > ['word', 3, 4, 6, 9] > > I think sorting tuple has a well-defined meaning, i.e. sort on first item, > then second, then third, etc... and there is AFAIK no restriction that the > key argument should be a number right? Right. Tuples sort lexicographically (which is a fancy way to say sort on first item, etc) and the key values just have to be comparable. This is a very clean simple solution. Kent From steve at alchemy.com Thu May 14 16:05:19 2009 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 14 May 2009 07:05:19 -0700 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> Message-ID: <4A0C251F.7090906@alchemy.com> Lie Ryan wrote: > Jabin Jezreel wrote: >> I am not allowed to do >>>>> t = (1, *(2, 3)) >> >> But I am allowed to do >>>>> def ts(*t): >> .... return t >> .... >>>>> ts(1, *(2, 3)) >> (1, 2, 3) >> >> I realize I can do >>>>> (1,) + (2,3) >> (1, 2, 3) >> >> What is the rationale behind not having t = (1, *(2, 3)) >> have the same semantics as the "ts" case above? > I guess because it is not clear what (1, *(2, 3)) should mean. > Parentheses when used for function call has different semantic then when > parentheses is used for tuple syntax. Parentheses in function is part of > the calling syntax, while parentheses in tuple is used only for grouping. I'm not sure that's a strong argument against allowing "unfolding" tuples in any expression beyond function calls, though, but it is how Python works. > PS: anyway I just realized that since tuple is immutable, having (1, > *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be > a better example. No, it doesn't. This would be the expression for constructing a new tuple, which would, after that point, be immutable. From dextrous85 at gmail.com Thu May 14 16:36:35 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 14 May 2009 20:06:35 +0530 Subject: [Tutor] How to handle complexType returned by wsdl using SOAPpy Message-ID: <5487b3060905140736q554e60a1p2f4269db21cd71a4@mail.gmail.com> Hi, soapUrl = constants.JIRA_PATH + '/jira/rpc/soap/jirasoapservice-v2?wsdl' soap = SOAPpy.WSDL.Proxy(soapUrl) auth = soap.login(constants.JIRA_USERNAME,constants.JIRA_PASSWORD) b = Types.longType(10041L) role = soap.getProjectRole(auth,b) project = soap.getProjectByKey(auth,'TEMP') Now I want to pass role and project object to some other function of the webserice but I get an error as the role and project are of struct type and webservice is expecting remote role and remote project object which is of complexType as mentioned in wsdl. Can anyone tell me how can I handle the complexType and use them? -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Thu May 14 16:51:23 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 15 May 2009 00:51:23 +1000 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <4A0C251F.7090906@alchemy.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> <4A0C251F.7090906@alchemy.com> Message-ID: <4A0C2FEB.7080509@gmail.com> Steve Willoughby wrote: > Lie Ryan wrote: >> Jabin Jezreel wrote: >>> I am not allowed to do >>>>>> t = (1, *(2, 3)) >>> But I am allowed to do >>>>>> def ts(*t): >>> .... return t >>> .... >>>>>> ts(1, *(2, 3)) >>> (1, 2, 3) >>> >>> I realize I can do >>>>>> (1,) + (2,3) >>> (1, 2, 3) >>> >>> What is the rationale behind not having t = (1, *(2, 3)) >>> have the same semantics as the "ts" case above? > >> I guess because it is not clear what (1, *(2, 3)) should mean. >> Parentheses when used for function call has different semantic then when >> parentheses is used for tuple syntax. Parentheses in function is part of >> the calling syntax, while parentheses in tuple is used only for grouping. > > I'm not sure that's a strong argument against allowing "unfolding" > tuples in any expression beyond function calls, though, but it is how > Python works. > >> PS: anyway I just realized that since tuple is immutable, having (1, >> *(2, 3)) be (1, 2, 3) would violate immutability. Maybe a list would be >> a better example. > > No, it doesn't. This would be the expression for constructing a new > tuple, which would, after that point, be immutable. What's the point? Why not just write is simply as (1, 2, 3) instead of the confusing (1, *(2, 3))? Don't say that (2, 3) might be a variable, it won't work without breaking python object model. If such construct creates a new tuple, it would need to break python's object model, unless you make it as an special case. Python tend to avoid special case, unless it is really, really necessary. Anyway, I think whatever good reason you have for this syntax, the BDFL will just say no. From bgailer at gmail.com Thu May 14 15:37:47 2009 From: bgailer at gmail.com (bob gailer) Date: Thu, 14 May 2009 09:37:47 -0400 Subject: [Tutor] Sorting a list In-Reply-To: <4A0BF14F.5060702@compuscan.co.za> References: <4A0AF48D.6080002@gmail.com> <4A0BC99F.1090908@gmail.com> <1c2a2c590905140302v1537d150oa07faa1f194f086f@mail.gmail.com> <4A0BF14F.5060702@compuscan.co.za> Message-ID: <4A0C1EAB.8060701@gmail.com> Christian Witts wrote: > Kent Johnson wrote: >> On Thu, May 14, 2009 at 3:34 AM, Timo wrote: >> >>> Emile van Sebille schreef: >>> >> >> >>>> If this is always the case you can use the sort method of lists, then >>>> relocate the last entry to the front -- >>>> >>>> >>>>>>> a = [4, 6, 'word', 3, 9] >>>>>>> a.sort() >>>>>>> a.insert(0,a.pop()) >>>>>>> a >>>>>>> >>>> ['word', 3, 4, 6, 9] >>>> >>> Thanks all for your answers. I think I will go for this solution (should >>> have come to it myself). >>> >> >> I don't think this will be reliable. IIUC unlike objects are compared >> by ID, so it is not guaranteed that strings will sort after numbers. >> The ability to compare unlike objects at all is considered a >> mis-feature and has been removed in Python 3: >> http://mail.python.org/pipermail/python-dev/2004-June/045111.html >> >> Python 3.0.1 (r301:69556, Feb 14 2009, 22:08:17) >> [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin >> Type "help", "copyright", "credits" or "license" for more information. >> >> >>>>> a = [4, 6, 'word', 3, 9] >>>>> a.sort() >>>>> >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: unorderable types: str() < int() >> >> Kent >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > Why not be safe and generate 2 lists and merge them afterwards like: > > >>> numeric_list, string_list = [], [] > >>> a=[4,6,'word',3,9] > >>> [numeric_list.append(int(element)) if element.isdigit() else > string_list.append(element) for element in map(str,a)] > [None, None, None, None, None] > >>> numeric_list.sort(); string_list.sort(); > >>> numeric_list, string_list > ([3, 4, 6, 9], ['word']) > >>> string_list.extend(numeric_list) > >>> string_list > ['word', 3, 4, 6, 9] In the relentless pursuit of terseness: >>> class L(list): ... def add(self, end, *item):self[-end:-end] = item >>> a=[4,6,'word',3,9] >>> result=L() >>> [result.add(type(i) != int, i) for i in reversed(sorted(a))] [None, None, None, None, None] >>> result [3, 4, 6, 9, 'word'] -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Thu May 14 20:22:36 2009 From: bgailer at gmail.com (bob gailer) Date: Thu, 14 May 2009 14:22:36 -0400 Subject: [Tutor] Sorting a list In-Reply-To: References: <4A0AF48D.6080002@gmail.com> Message-ID: <4A0C616C.2020208@gmail.com> Lie Ryan wrote: > Timo wrote: >> Hello, >> >> I don't think this should be difficult, so maybe I look over it. But >> I can't seem to find the solution. >> >> I have a list with one word and a couple of numbers. Now I want the >> word to be kept in the first location and the numbers to be sorted. >> >> So this: >> [4, 6, 'word', 3, 9] >> >> should be: >> >> ['word', 3, 4, 6, 9] >> > > Why not: > >>> sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x)) > ['word', 3, 4, 6, 9] Again in the relentless pursuit of terseness: sorted(a, key=lambda x: (isinstance(x, int), x)) -- Bob Gailer Chapel Hill NC 919-636-4239 From mobiledreamers at gmail.com Thu May 14 21:20:42 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Thu, 14 May 2009 12:20:42 -0700 Subject: [Tutor] cheetah is extremely hard to debug - error messages are non indicative TypeError: cannot concatenate 'str' and 'int' objects Message-ID: How do i find which variable has error in these error messages? *Nonetype object is unsubscriptable* else:return web.render('searchesnew.html') File "/home/mark/work/common/web/cheetah.py", line 103, in render return str(compiled_tmpl) File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__ def __str__(self): return getattr(self, mainMethName)() File "", line 323, in respond *TypeError: cannot concatenate 'str' and 'int' objects* -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From jabinjezreel at gmail.com Thu May 14 20:10:53 2009 From: jabinjezreel at gmail.com (Jabin Jezreel) Date: Thu, 14 May 2009 11:10:53 -0700 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> Message-ID: <6f8a657a0905141110x3fc7b939ide97fb57cb21b0b0@mail.gmail.com> > Why not just write is simply as (1, 2, 3) instead of > the confusing (1, *(2, 3))? It is a contrived example. In practice it would be something more like: >>> def ts(*t): ... return t ... >>> x = (2, 3) >>> y = (1, *x) File "", line 1 SyntaxError: can use starred expression only as assignment target >>> y = ts(1, *x) >>> y (1, 2, 3) > Don't say that (2, 3) might be a variable, it > won't work without breaking python object model. > If such construct creates a new tuple, it would > need to break python's object model [...] Break how? From kent37 at tds.net Thu May 14 21:40:35 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 14 May 2009 15:40:35 -0400 Subject: [Tutor] cheetah is extremely hard to debug - error messages are non indicative TypeError: cannot concatenate 'str' and 'int' objects In-Reply-To: References: Message-ID: <1c2a2c590905141240n4cdfcc58s18229944a9f72e83@mail.gmail.com> On Thu, May 14, 2009 at 3:20 PM, wrote: > How do i find which variable has error in these error messages? Generally by looking at the last source line in the traceback you can figure it out. IPython has an extended traceback mode that shows variables (see the xmode magic command) http://ipython.scipy.org/doc/stable/html/interactive/reference.html#magic-commands Kent > > Nonetype? object is unsubscriptable > > else:return web.render('searchesnew.html') > ? File "/home/mark/work/common/web/cheetah.py", line 103, in render > ??? return str(compiled_tmpl) > ? File > "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", > line 982, in __str__ > ??? def __str__(self): return getattr(self, mainMethName)() > ? File "", line 323, in respond > TypeError: cannot concatenate 'str' and 'int' objects > > > -- > Bidegg worlds best auction site > http://bidegg.com > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From steve at alchemy.com Thu May 14 21:43:34 2009 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 14 May 2009 12:43:34 -0700 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <6f8a657a0905141110x3fc7b939ide97fb57cb21b0b0@mail.gmail.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> <6f8a657a0905141110x3fc7b939ide97fb57cb21b0b0@mail.gmail.com> Message-ID: <20090514194334.GE96655@dragon.alchemy.com> On Thu, May 14, 2009 at 11:10:53AM -0700, Jabin Jezreel wrote: > > Why not just write is simply as (1, 2, 3) instead of > > the confusing (1, *(2, 3))? > > It is a contrived example. In practice it would be > something more like: > > >>> def ts(*t): > ... return t > ... > >>> x = (2, 3) > >>> y = (1, *x) > File "", line 1 > SyntaxError: can use starred expression only as assignment target > >>> y = ts(1, *x) > >>> y > (1, 2, 3) But you can already do that without needing to extend * notation to work like that, so in the Pythonic spirit of there only being one obvious/best/clear way to do something... y = (1,) + x why does that operation have to be constructed as y = (1, *x)? > > > > Don't say that (2, 3) might be a variable, it > > won't work without breaking python object model. > > If such construct creates a new tuple, it would > > need to break python's object model [...] > > Break how? > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From walker.hale.iv at gmail.com Thu May 14 21:46:29 2009 From: walker.hale.iv at gmail.com (Walker Hale IV) Date: Thu, 14 May 2009 14:46:29 -0500 Subject: [Tutor] Sorting a list In-Reply-To: <4A0C616C.2020208@gmail.com> References: <4A0AF48D.6080002@gmail.com> <4A0C616C.2020208@gmail.com> Message-ID: <312f43620905141246jc6089cay3edb9f12ea8b952c@mail.gmail.com> The prize goes to Bob Gailer! This solution is not only shortest, but works perfectly with negative numbers. -- Walker Hale On Thu, May 14, 2009 at 1:22 PM, bob gailer wrote: > Lie Ryan wrote: >> >> Timo wrote: >>> >>> Hello, >>> >>> I don't think this should be difficult, so maybe I look over it. But I >>> can't seem to find the solution. >>> >>> I have a list with one word and a couple of numbers. Now I want the word >>> to be kept in the first location and the numbers to be sorted. >>> >>> So this: >>> [4, 6, 'word', 3, 9] >>> >>> should be: >>> >>> ['word', 3, 4, 6, 9] >>> >> >> Why not: >> >>> sorted(lst, key=lambda x: (0, x) if isinstance(x, str) else (1, x)) >> ['word', 3, 4, 6, 9] > > Again in the relentless pursuit of terseness: > > sorted(a, key=lambda x: (isinstance(x, int), x)) > > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Thu May 14 23:00:15 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 May 2009 22:00:15 +0100 Subject: [Tutor] cheetah is extremely hard to debug -error messages are non indicative TypeError: cannotconcatenate 'str' and 'int' objects References: Message-ID: wrote > How do i find which variable has error in these error messages? > > *Nonetype object is unsubscriptable* You need to send the entire error message, the last line is not much good without the context. But you should see a line in the traceback where a subscript aka indexing) operation is being used. Python says that object is not subscriptable. > else:return web.render('searchesnew.html') > File "/home/mark/work/common/web/cheetah.py", line 103, in render > return str(compiled_tmpl) > File > "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", > line 982, in __str__ > def __str__(self): return getattr(self, mainMethName)() > File "", line 323, in respond > *TypeError: cannot concatenate 'str' and 'int' objects* No idea about this bit. Is it a single error or a group of them? If a group you need to send the complete message for each error. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From lopoff at gmx.net Thu May 14 23:50:26 2009 From: lopoff at gmx.net (MK) Date: Thu, 14 May 2009 23:50:26 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate Message-ID: <1242337826.7940.5.camel@DadTux> Hi there, i am using this code to send an "cat > ThisIsMyUrl" with popen. Of cos cat now waits for the CTRL+D command. How can i send this command ? def console_command(cmd): print cmd console = os.popen(cmd,"r") output = console.read() console.close() return output command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com" console_command(command) Thank you. From Larry at Riedel.org Fri May 15 00:02:52 2009 From: Larry at Riedel.org (Larry Riedel) Date: Thu, 14 May 2009 15:02:52 -0700 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <20090514194334.GE96655@dragon.alchemy.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> <6f8a657a0905141110x3fc7b939ide97fb57cb21b0b0@mail.gmail.com> <20090514194334.GE96655@dragon.alchemy.com> Message-ID: <7c64c2920905141502s5a6b5dd1s8e7994984edd3cc@mail.gmail.com> > > >>> x = (2, 3) > > >>> y = (1, *x) > > File "", line 1 > > SyntaxError: can use starred expression only as assignment target > > But you can already do that without needing to extend * notation > to work like that To me the "*" notation already /appears/ to work like that: (Python 3.x) >>> a = [1, 2, 3] >>> (b, *c) = a >>> b 1 >>> c [2, 3] Just only in one direction. >>> a = (b, *c) File "", line 1 SyntaxError: can use starred expression only as assignment target Larry From vinces1979 at gmail.com Fri May 15 00:28:33 2009 From: vinces1979 at gmail.com (vince spicer) Date: Thu, 14 May 2009 16:28:33 -0600 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242337826.7940.5.camel@DadTux> References: <1242337826.7940.5.camel@DadTux> Message-ID: <1e53c510905141528u5e0b65b3r2a02bbc574cbdb93@mail.gmail.com> Take a peak at commands.getoutput EX: import commmands ls = commands.getoutput("ls- ls") Vince On Thu, May 14, 2009 at 3:50 PM, MK wrote: > Hi there, > > i am using this code to send an "cat > ThisIsMyUrl" with popen. > Of cos cat now waits for the CTRL+D command. > How can i send this command ? > > def console_command(cmd): > print cmd > console = os.popen(cmd,"r") > output = console.read() > console.close() > return output > > command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com" > console_command(command) > > > Thank you. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vinces1979 at gmail.com Fri May 15 00:28:57 2009 From: vinces1979 at gmail.com (vince spicer) Date: Thu, 14 May 2009 16:28:57 -0600 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1e53c510905141528u5e0b65b3r2a02bbc574cbdb93@mail.gmail.com> References: <1242337826.7940.5.camel@DadTux> <1e53c510905141528u5e0b65b3r2a02bbc574cbdb93@mail.gmail.com> Message-ID: <1e53c510905141528r68e6ebc9o3482075fe229f775@mail.gmail.com> import commands On Thu, May 14, 2009 at 4:28 PM, vince spicer wrote: > Take a peak at commands.getoutput > > > EX: > > import commmands > > ls = commands.getoutput("ls- ls") > > Vince > > > On Thu, May 14, 2009 at 3:50 PM, MK wrote: > >> Hi there, >> >> i am using this code to send an "cat > ThisIsMyUrl" with popen. >> Of cos cat now waits for the CTRL+D command. >> How can i send this command ? >> >> def console_command(cmd): >> print cmd >> console = os.popen(cmd,"r") >> output = console.read() >> console.close() >> return output >> >> command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com" >> console_command(command) >> >> >> Thank you. >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwalsh at mwalsh.org Fri May 15 01:31:32 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Thu, 14 May 2009 18:31:32 -0500 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242337826.7940.5.camel@DadTux> References: <1242337826.7940.5.camel@DadTux> Message-ID: <4A0CA9D4.6040108@mwalsh.org> MK wrote: > Hi there, > > i am using this code to send an "cat > ThisIsMyUrl" with popen. > Of cos cat now waits for the CTRL+D command. > How can i send this command ? > > def console_command(cmd): > print cmd > console = os.popen(cmd,"r") > output = console.read() > console.close() > return output > > command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com" > console_command(command) Ignoring the subprocess module for a moment, you could use os.popen2 instead of os.popen and then close the stdin fd to simulate a CTRL-D(EOF). stdin, stdout = os.popen2(cmd) stdin.write('This is line 1.\n') stdin.close() # CTRL-D With the subprocess module, you might do something like this (untested) ... from subprocess import Popen, PIPE console = Popen( cmd, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True ) console.stdin.write('This is line 1.\n') console.stdin.close() # ... or just ... # console.communicate('This is line 1.\n') But, I can't figure out why you would want to do this. Or I should say it's not at all clear to me what you are trying to accomplish -- although I suspect you are making it difficult for yourself. Can you provide a brief explanation of what you are trying to achieve? HTH, Marty From alan.gauld at btinternet.com Fri May 15 01:48:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 15 May 2009 00:48:11 +0100 Subject: [Tutor] Python popen command using cat > textfile .... how toterminate References: <1242337826.7940.5.camel@DadTux> <1e53c510905141528u5e0b65b3r2a02bbc574cbdb93@mail.gmail.com> Message-ID: "vince spicer" wrote > Take a peak at commands.getoutput Or more "correctly" look at the subprocess module. All previous mechanisms for process control are rendered obsolete and are deprecated in favour of subprocess So if we aren't using popen lets at least use subprocess. It is slightly more complex than popen or commands but it is future proof (for now at least!) Simple example: import subprocess lsout = subprocess.Popen('ls -ls', shell=True, stdout=subprocess.PIPE).stdout print lsout.read() -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sander.sweers at gmail.com Fri May 15 02:54:36 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 15 May 2009 02:54:36 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: References: <1242337826.7940.5.camel@DadTux> Message-ID: Forwarding to list because of mail delivery error... 2009/5/15 Sander Sweers : > 2009/5/14 MK : >> i am using this code to send an "cat > ThisIsMyUrl" with popen. > > First, I am not sure if you understand what "cat > ThisIsMyUrl" > actually does... It waits for data on *stdin* and pipes it to > ThisIsMyUrl. It does not provide any data to read! What is it that you > want to accomplish? > > Anyway, why use cat and not use open() and write the data into it? > ?file = open(working_dir + "/" + subdir + "www.thisismyurl.com", "w") > ?file.write(your_data_string) > ?file.close() > >> Of cos cat now waits for the CTRL+D command. > > Well yes and no, cat is waiting for data on stdin and you do not seem > to give any data to it. > >> How can i send this command ? > > CTR-D/EOF normally comes from the data you are piping into cat. For > example the below, it pipes the string "test" to the stdin from cat > which then redirects to a file test.txt. > ?echo "test" | cat > test.txt > >> def console_command(cmd): >> ? ? ? ?print cmd >> ? ? ? ?console = os.popen(cmd,"r") >> ? ? ? ?output = console.read() >> ? ? ? ?console.close() >> ? ? ? ?return output >> >> command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com" >> console_command(command) > > You should really look at the subprocess module [1]. > > Greets > Sander > > [1] http://docs.python.org/library/subprocess.html > From mobiledreamers at gmail.com Fri May 15 03:15:09 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Thu, 14 May 2009 18:15:09 -0700 Subject: [Tutor] cheetah is extremely hard to debug - error messages are non indicative TypeError: cannot concatenate 'str' and 'int' objects In-Reply-To: <1c2a2c590905141240n4cdfcc58s18229944a9f72e83@mail.gmail.com> References: <1c2a2c590905141240n4cdfcc58s18229944a9f72e83@mail.gmail.com> Message-ID: Cheetah is usually not run from ipython! On Thu, May 14, 2009 at 12:40 PM, Kent Johnson wrote: > On Thu, May 14, 2009 at 3:20 PM, wrote: > > How do i find which variable has error in these error messages? > > Generally by looking at the last source line in the traceback you can > figure it out. > > IPython has an extended traceback mode that shows variables (see the > xmode magic command) > > http://ipython.scipy.org/doc/stable/html/interactive/reference.html#magic-commands > > Kent > > > > > Nonetype object is unsubscriptable > > > > else:return web.render('searchesnew.html') > > File "/home/mark/work/common/web/cheetah.py", line 103, in render > > return str(compiled_tmpl) > > File > > > "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", > > line 982, in __str__ > > def __str__(self): return getattr(self, mainMethName)() > > File "", line 323, in respond > > TypeError: cannot concatenate 'str' and 'int' objects > > > > > > -- > > Bidegg worlds best auction site > > http://bidegg.com > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From tyler at slide.com Fri May 15 01:29:33 2009 From: tyler at slide.com (R. Tyler Ballance) Date: Thu, 14 May 2009 16:29:33 -0700 Subject: [Tutor] [Cheetahtemplate-discuss] cheetah is extremely hard to debug - error messages are non indicative TypeError: cannot concatenate 'str' and 'int' objects In-Reply-To: References: Message-ID: <20090514232933.GB22379@starfruit.corp.slide.com> On Thu, May 14, 2009 at 12:20:42PM -0700, mobiledreamers at gmail.com wrote: > How do i find which variable has error in these error messages? > > Nonetype object is unsubscriptable > > else:return web.render('searchesnew.html') > File "/home/mark/work/common/web/cheetah.py", line 103, in render > return str(compiled_tmpl) > File > "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", > line 982, in __str__ > def __str__(self): return getattr(self, mainMethName)() > File "", line 323, in respond > TypeError: cannot concatenate 'str' and 'int' objects I'm not really sure what you expect Cheetah to do for you? It can't really hold your hand and point you to an error automagically because like most compilers it doesn't understand what you "meant" but only what you told it ;) The first error is fairly easy to grok, just not sure where you'd find it without a backtrace, it's a simple `None['foo']` or `None[0]` The second is another relatively simple error `'1' + 2`. The problem with both is that you're not going to get "true" and proper backtraces because you're using dynamically compiled templates, so Python doesn't exactly have lines of code to refer back to since the objects were dynamically created. If you were to compile searchesnew.html down to a searchesnew.py and then execute it in a similar fashion, you'd get a "proper" Cheetah backtrace like this: File "base.py", line 413, in respond _v = VFFSL(SL,"page_body",False)() # '$page_body()' on line 51, col 3 File "foo/base.py", line 100, in page_body _v = VFFSL(SL,"page_content",False)() # '$page_content()' on line 19, col 2 Cheers -- -R. Tyler Ballance Slide, Inc. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: not available URL: From wolf85boy2008 at yahoo.com Fri May 15 06:46:49 2009 From: wolf85boy2008 at yahoo.com (R K) Date: Thu, 14 May 2009 21:46:49 -0700 (PDT) Subject: [Tutor] finding difference in time Message-ID: <575346.13146.qm@web43516.mail.sp1.yahoo.com> Gurus, I'm trying to write a fairly simple script that finds the number of hours / minutes / seconds between now and the next Friday at 1:30AM. I have a few little chunks of code but I can't seem to get everything to piece together nicely. import datetime,time now = datetime.datetime.now() i = 0 dayOfWeek = datetime.datetime.now().strftime( '%a' ) while dayOfWeek != 'Fri': delta = datetime.timedelta( days = i ) tom = ( now + delta ).strftime( '%a' ) if tom != 'Fri': i = i + 1 else: print i print tom break So with this code I can determine the number of days until the next Friday (if it's not Friday already). The problem I'm having, however, is with finding the number of minutes until 1:30AM on Friday. nextFridayDay = int( now.strftime( '%d' ) ) + 1 nextFridayMonth = int( now.strftime( '%m' ) ) nextFridayYear = int( now.strftime( '%Y' ) ) nextRun = datetime.datetime( nextFridayYear , nextFridayMonth , nextFridayDay , 1 , 30 , 0 ) What I gather is that I should be able to numerically manipulate two datetime objects, as seen below: In [227]: nextRun - now Out[227]: datetime.timedelta(0, 46155, 51589) The result, however, doesn't make sense. Take a look... In [231]: d = nextRun - now In [232]: d.seconds Out[232]: 46155 In [233]: d.days Out[233]: 0 Thoughts on what I may be doing wrong? Am I going about this the whole wrong way? Should I be using something different to calculate the number of minutes between now and the next Friday at 1:30AM? -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Fri May 15 08:07:41 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 15 May 2009 08:07:41 +0200 Subject: [Tutor] t = (1, *(2, 3)) In-Reply-To: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> References: <6f8a657a0905131836u1d69fdf7pa454640e2ff11b44@mail.gmail.com> Message-ID: Jabin Jezreel wrote: > I am not allowed to do >>>> t = (1, *(2, 3)) > > But I am allowed to do >>>> def ts(*t): > ... return t > ... >>>> ts(1, *(2, 3)) > (1, 2, 3) > > I realize I can do >>>> (1,) + (2,3) > (1, 2, 3) > > What is the rationale behind not having t = (1, *(2, 3)) > have the same semantics as the "ts" case above? They are different things. However, I think a proposal to make the above work sounds like a good thing to bring up on the python-ideas list. Stefan From andreengels at gmail.com Fri May 15 09:28:00 2009 From: andreengels at gmail.com (Andre Engels) Date: Fri, 15 May 2009 09:28:00 +0200 Subject: [Tutor] finding difference in time In-Reply-To: <575346.13146.qm@web43516.mail.sp1.yahoo.com> References: <575346.13146.qm@web43516.mail.sp1.yahoo.com> Message-ID: <6faf39c90905150028t563b9937y7cc55d4fa47154b1@mail.gmail.com> On Fri, May 15, 2009 at 6:46 AM, R K wrote: > Gurus, > > I'm trying to write a fairly simple script that finds the number of hours / > minutes / seconds between now and the next Friday at 1:30AM. > > I have a few little chunks of code but I can't seem to get everything to > piece together nicely. > > import datetime,time > now = datetime.datetime.now() > > i = 0 > dayOfWeek = datetime.datetime.now().strftime( '%a' ) > while dayOfWeek != 'Fri': > ??? delta = datetime.timedelta( days = i ) > ??? tom = ( now + delta ).strftime( '%a' ) > ??? if tom != 'Fri': > ??????? i = i + 1 > ??? else: > ??????? print i > ??????? print tom > ??????? break > > So with this code I can determine the number of days until the next Friday > (if it's not Friday already). > > The problem I'm having, however, is with finding the number of minutes until > 1:30AM on Friday. > > nextFridayDay = int( now.strftime( '%d' ) ) + 1 > nextFridayMonth = int( now.strftime( '%m' ) ) > nextFridayYear = int( now.strftime( '%Y' ) ) > > nextRun = datetime.datetime( nextFridayYear , nextFridayMonth , > nextFridayDay , 1 , 30 , 0 ) > > What I gather is that I should be able to numerically manipulate two > datetime objects, as seen below: > > In [227]: nextRun - now > Out[227]: datetime.timedelta(0, 46155, 51589) > > The result, however, doesn't make sense. Take a look... > > In [231]: d = nextRun - now > > In [232]: d.seconds > Out[232]: 46155 > > In [233]: d.days > Out[233]: 0 > > Thoughts on what I may be doing wrong? Am I going about this the whole wrong > way? Should I be using something different to calculate the number of > minutes between now and the next Friday at 1:30AM? Why do you think the result doesn't make sense? You basically defined "next Friday" as tomorrow (the day in the same year on the same day one number more), and then looked at the time until next Friday at 1:30AM. It appears that that time was 0 days and 46155 seconds (plus some fraction of a second, which is about 12 hours and 50 minutes. -- Andr? Engels, andreengels at gmail.com From lopoff at gmx.net Fri May 15 12:05:26 2009 From: lopoff at gmx.net (MK) Date: Fri, 15 May 2009 12:05:26 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate Message-ID: <1242381926.6420.6.camel@DadTux> Ok. I explain it once more. Its not about reading the console output of the command. Its that i want to put a empty file in my subdirectories so that the name of the file is a message or url or something else. And if you want an empty file you can do that with "cat > IAmAnEmptyFileWithOnlyAName" under linux on the CLI. But the problem or advantage of is that you can put some lines in the text file if you want. And if youre ready youm must terminate the console/terminal with CTRL+D so that cat knows that the input is finished. I have no lines i want to input in the file. So i must only send and CTRL+D to cat again that it terminates. Actually a very simple way i thought. But maybe not. Thank you. From lopoff at gmx.net Fri May 15 12:19:14 2009 From: lopoff at gmx.net (MK) Date: Fri, 15 May 2009 12:19:14 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate Message-ID: <1242382754.6420.8.camel@DadTux> Seems that i did it the wrong way still from the beginning. I did it now with open and write an empty file. But anyway i would wish to know if it is possible to terminate a running cat. Thank you all. From kent37 at tds.net Fri May 15 12:20:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 15 May 2009 06:20:42 -0400 Subject: [Tutor] finding difference in time In-Reply-To: <575346.13146.qm@web43516.mail.sp1.yahoo.com> References: <575346.13146.qm@web43516.mail.sp1.yahoo.com> Message-ID: <1c2a2c590905150320o212ac2d6yd267b67406337e7c@mail.gmail.com> On Fri, May 15, 2009 at 12:46 AM, R K wrote: > Gurus, > > I'm trying to write a fairly simple script that finds the number of hours / > minutes / seconds between now and the next Friday at 1:30AM. > > I have a few little chunks of code but I can't seem to get everything to > piece together nicely. > > import datetime,time > now = datetime.datetime.now() > > i = 0 > dayOfWeek = datetime.datetime.now().strftime( '%a' ) > while dayOfWeek != 'Fri': > ??? delta = datetime.timedelta( days = i ) > ??? tom = ( now + delta ).strftime( '%a' ) > ??? if tom != 'Fri': > ??????? i = i + 1 > ??? else: > ??????? print i > ??????? print tom > ??????? break > > So with this code I can determine the number of days until the next Friday > (if it's not Friday already). This could be simpler. I would write nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) while nextFriday.weekday() != 4: nextFriday += datetime.timedelta(days=1) Note the use of datetime attributes instead of relying on strftime(). What do you want the answer to be if you run the script at 1am Friday? at 2am Friday? If you want the next Friday in both cases, you could write this as nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) nextFriday += datetime.timedelta(days=1) # Make sure to get a Friday in the future while nextFriday.weekday() != 4: nextFriday += datetime.timedelta(days=1) Kent From sander.sweers at gmail.com Fri May 15 12:41:36 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 15 May 2009 12:41:36 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242381926.6420.6.camel@DadTux> References: <1242381926.6420.6.camel@DadTux> Message-ID: 2009/5/15 MK : > Ok. I explain it once more. Its not about reading the console output of > the command. Its that i want to put a empty file in my subdirectories > so that the name of the file is a message or url or something else. > And if you want an empty file you can do that with > "cat > IAmAnEmptyFileWithOnlyAName" > under linux on the CLI. Ah yes, cat is not what you will wan to use. You can use the unix touch command or use python's open(). Example touch and subproces (untested!). --- import subprocess command = subprocess.Popen(['touch', '/your_filepath/filename'],stdout=subprocess.PIPE) stdout, stderr = command.communicate() --- Example open(). --- filename = open('/your_filepath/filename', 'w') filename.close() --- > But the problem or advantage of is that you can put some lines in > the text file if you want. And if youre ready youm must > terminate the console/terminal with CTRL+D so that cat knows > that the input is finished. Like I said above, cat is not the right tool for this. When you use python's open() you could also write data in the file if needed. > Actually a very simple way i thought. But maybe not. It is but only with the right tools ;-) Greets Sander From a.t.hofkamp at tue.nl Fri May 15 12:45:34 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Fri, 15 May 2009 12:45:34 +0200 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242381926.6420.6.camel@DadTux> References: <1242381926.6420.6.camel@DadTux> Message-ID: <4A0D47CE.4060204@tue.nl> MK wrote: > Ok. I explain it once more. Its not about reading the console output of > the command. Its that i want to put a empty file in my subdirectories > so that the name of the file is a message or url or something else. > And if you want an empty file you can do that with > "cat > IAmAnEmptyFileWithOnlyAName" > under linux on the CLI. 'touch filename' is easier when you know the file does not yet exist. Alternatively, you can do 'cat > filename < /dev/null' > But the problem or advantage of is that you can put some lines in > the text file if you want. And if youre ready youm must > terminate the console/terminal with CTRL+D so that cat knows > that the input is finished. CTL+D means EOF. In a program you express that by closing the file handle. fp = open('xyz', 'w') fp.close() for creating an empty file, thus fp = subprocess. .... #get file handle for writing data to the child fp.close() for stating that you have no data for the child process. > I have no lines i want to input in the file. So i must only send > and CTRL+D to cat again that it terminates. > > Actually a very simple way i thought. But maybe not. It is simple indeed :) It is not Python specific, but defined by the underlying OS. That is why you don't find this information in Python documentation. Albert From davea at ieee.org Fri May 15 13:20:42 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 15 May 2009 07:20:42 -0400 Subject: [Tutor] finding difference in time In-Reply-To: References: Message-ID: <4A0D500A.4070808@ieee.org> R K wrote: > Gurus, > > I'm trying to write a fairly simple script that finds the number of hours / minutes / seconds between now and the next Friday at 1:30AM. > > I have a few little chunks of code but I can't seem to get everything to piece together nicely. > > import datetime,time > now = datetime.datetime.now() > > i = 0 > dayOfWeek = datetime.datetime.now().strftime( '%a' ) > while dayOfWeek != 'Fri': > delta = datetime.timedelta( days = i ) > tom = ( now + delta ).strftime( '%a' ) > if tom != 'Fri': > i = i + 1 > else: > print i > print tom > break > > So with this code I can determine the number of days until the next Friday (if it's not Friday already). > > The problem I'm having, however, is with finding the number of minutes until 1:30AM on Friday. > > nextFridayDay = int( now.strftime( '%d' ) ) + 1 > nextFridayMonth = int( now.strftime( '%m' ) ) > nextFridayYear = int( now.strftime( '%Y' ) ) > > nextRun = datetime.datetime( nextFridayYear , nextFridayMonth , nextFridayDay , 1 , 30 , 0 ) > > What I gather is that I should be able to numerically manipulate two datetime objects, as seen below: > > In [227]: nextRun - now > Out[227]: datetime.timedelta(0, 46155, 51589) > > The result, however, doesn't make sense. Take a look... > > In [231]: d = nextRun - now > > In [232]: d.seconds > Out[232]: 46155 > > In [233]: d.days > Out[233]: 0 > > Thoughts on what I may be doing wrong? Am I going about this the whole wrong way? Should I be using something different to calculate the number of minutes between now and the next Friday at 1:30AM? > Could you give us the exact wording of the homework assignment? I suspect that if now is on a Friday, and already past 1:30, then you want to target a week from today, rather than getting negative numbers. If so, you can't treat day-of-week as something independent. If you want to keep your present loop, you should first compare now's hour and minute to 1:30. Second problem, you're not using the result of the loop. You could use either delta or i as some indication of how many days are between now and Friday. I suspect you intended for the +1 on nextFridayDay to be +i instead. Unfortunately, that would help any time except during the last week of the month (and year). Fixing each of these in isolation would make the code painfully convoluted. Seems to me you should be finding your target date as a timedate object, and manipulating that object directly till it represents a time of 1:30, a day of Friday, and a delta that's positive. So as an outline: make a copy of now, and call it target. change target's time fields to be exactly 1:30 compare target to now, and if smaller, increment it by a delta of exactly one day in a loop, check if target's day-of-week is 'Fri' and increment by another day if not Now you have final target, so do your difference. From lie.1296 at gmail.com Fri May 15 13:52:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 15 May 2009 21:52:38 +1000 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242381926.6420.6.camel@DadTux> References: <1242381926.6420.6.camel@DadTux> Message-ID: MK wrote: > Ok. I explain it once more. Its not about reading the console output of > the command. Its that i want to put a empty file in my subdirectories > so that the name of the file is a message or url or something else. > And if you want an empty file you can do that with > "cat > IAmAnEmptyFileWithOnlyAName" > under linux on the CLI. But why not using open('filename', 'w')? From alan.gauld at btinternet.com Fri May 15 17:18:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 15 May 2009 16:18:16 +0100 Subject: [Tutor] Python popen command using cat > textfile .... how toterminate References: <1242382754.6420.8.camel@DadTux> Message-ID: "MK" wrote > Seems that i did it the wrong way still from the beginning. > I did it now with open and write an empty file. > > But anyway i would wish to know if it is possible to terminate > a running cat. It depends on what you mean by a running cat. cat simply concatenates (ie joins) its input to its output. By default cat's input is stdin and its output is stdout so if you just type cat anything you type on stdin gets echoed to stdout. You terminate the command by inputing an EOF character (Ctrl D on *nux) cat > foo writes stdin to foo Again, to terminate it you send EOF via stdin. So if you use subprocess/popen to run cat you need to write EOF to the stdin port. You can do that with popen2 or subprocess.Popen but not with simple popen which only lets you read stdout. cat > foo < /dev/null writes the non file at dev/null to foo and since that has an implicit EOF it terminates automatically echo ^D | cat > foo sends a CtrlD to cat which writes an empty file to foo. Your questions are simply about how to run cat not about Python. Any/All of the above can be run from Python. And all of them are a bad way to create an empty file in either *nix or Python. In *nix use touch and in Python use open(foo,'w').close() When working in Python, every time you are tempted to call a unix command via os.system/popen etc check to see if there is a way to do it from within python fiorst. There often is. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at alchemy.com Fri May 15 17:51:26 2009 From: steve at alchemy.com (Steve Willoughby) Date: Fri, 15 May 2009 08:51:26 -0700 Subject: [Tutor] Python popen command using cat > textfile .... how toterminate In-Reply-To: References: <1242382754.6420.8.camel@DadTux> Message-ID: <20090515155126.GA1167@dragon.alchemy.com> On Fri, May 15, 2009 at 04:18:16PM +0100, Alan Gauld wrote: > echo ^D | cat > foo > > sends a CtrlD to cat which writes an empty file to foo. And since this seems to be a point of confusion for you, keep in mind that the ^D character itself is not a "command" or even seen by the cat program at all. So the notion of "sending ^D to cat" via the interprocess pipe from Python isn't really the right concept. You never send ^D to cat. When cat (or any program) is expecting input (say, from its standard input device) which is coming from your terminal (i.e., reading in whatever you're typing), then ^D is the default character the *Unix I/O system* recognizes as signalling the end of input for that device. This will cause *the OS* to close that input device. All the cat program sees is that its input has run out of data. The same is true on other OSes like VMS or DOS or Windows, which use ^Z to mean the same thing (and note that even on Unix you can assign a different key than ^D if you want to). That's why from Python, when you're not using the normal TTY control support from the OS itself, but sending data to cat from your Python program, there's no notion of ^D, but you do the same thing to cat that the OS would do: you close the input device. I hope that makes it clearer. > And all of them are a bad way to create an empty file in either *nix or > Python. Very much so. But don't feel bad, this is a very common mistake for beginners to make. Cat has a number of uses it's well suited for, but don't fall into the trap of using it for everything when there are better tools available. I seem to keep running into people who do things like $ cat myfile | more because they just learned the pattern "cat file |..." without really learning how to use file redirection and how these things work. But more importantly, don't fall into the trap of launching external programs to do simple tasks which are already built into your language. You just introduce extra code you don't need, dependencies on the OS platform you don't need, and security and performance problems as an extra bonus. > When working in Python, every time you are tempted to call > a unix command via os.system/popen etc check to see if > there is a way to do it from within python fiorst. > There often is. Almost always. Plus, if you do it in Python your program will work on Windows. And MacOS. And VMS. And a Unix system where the user has a "cat" program which behaves differently than yours does... which trying to call "cat" yourself won't. > HTH, HAND, -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From steve at alchemy.com Fri May 15 18:06:00 2009 From: steve at alchemy.com (Steve Willoughby) Date: Fri, 15 May 2009 09:06:00 -0700 Subject: [Tutor] Python popen command using cat > textfile .... how toterminate In-Reply-To: <20090515155126.GA1167@dragon.alchemy.com> References: <1242382754.6420.8.camel@DadTux> <20090515155126.GA1167@dragon.alchemy.com> Message-ID: <20090515160600.GB1167@dragon.alchemy.com> On Fri, May 15, 2009 at 08:51:26AM -0700, Steve Willoughby wrote: > On Fri, May 15, 2009 at 04:18:16PM +0100, Alan Gauld wrote: > > echo ^D | cat > foo > > > > sends a CtrlD to cat which writes an empty file to foo. > > And since this seems to be a point of confusion for you, And that was actually supposed to be addressed to the original poster, who seemed to be confused about where the ^D character was going, not Alan. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From noufal at nibrahim.net.in Fri May 15 19:46:22 2009 From: noufal at nibrahim.net.in (Noufal Ibrahim) Date: Fri, 15 May 2009 23:16:22 +0530 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <1242337826.7940.5.camel@DadTux> References: <1242337826.7940.5.camel@DadTux> Message-ID: <4A0DAA6E.9030306@nibrahim.net.in> MK wrote: > Hi there, > > i am using this code to send an "cat > ThisIsMyUrl" with popen. > Of cos cat now waits for the CTRL+D command. > How can i send this command ? Wouldn't it be better if you directly opened the "ThisIsMyUrl" file and wrote the text into it rather than rely on shelling out for this? [..] -- ~noufal http://nibrahim.net.in/ From mwalsh at mwalsh.org Fri May 15 22:21:18 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Fri, 15 May 2009 15:21:18 -0500 Subject: [Tutor] finding difference in time In-Reply-To: <1c2a2c590905150320o212ac2d6yd267b67406337e7c@mail.gmail.com> References: <575346.13146.qm@web43516.mail.sp1.yahoo.com> <1c2a2c590905150320o212ac2d6yd267b67406337e7c@mail.gmail.com> Message-ID: <4A0DCEBE.7080607@mwalsh.org> Kent Johnson wrote: > On Fri, May 15, 2009 at 12:46 AM, R K wrote: >> Gurus, >> >> I'm trying to write a fairly simple script that finds the number of hours / >> minutes / seconds between now and the next Friday at 1:30AM. >> >> I have a few little chunks of code but I can't seem to get everything to >> piece together nicely. >> >> import datetime,time >> now = datetime.datetime.now() >> >> i = 0 >> dayOfWeek = datetime.datetime.now().strftime( '%a' ) >> while dayOfWeek != 'Fri': >> delta = datetime.timedelta( days = i ) >> tom = ( now + delta ).strftime( '%a' ) >> if tom != 'Fri': >> i = i + 1 >> else: >> print i >> print tom >> break >> >> So with this code I can determine the number of days until the next Friday >> (if it's not Friday already). > > This could be simpler. I would write > nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) > while nextFriday.weekday() != 4: > nextFriday += datetime.timedelta(days=1) > > Note the use of datetime attributes instead of relying on strftime(). > > What do you want the answer to be if you run the script at 1am Friday? > at 2am Friday? If you want the next Friday in both cases, you could > write this as > nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) > nextFriday += datetime.timedelta(days=1) # Make sure to get a Friday > in the future > while nextFriday.weekday() != 4: > nextFriday += datetime.timedelta(days=1) I don't believe you'll get much better than that, IMHO. But, dateutil (3rd party) probably deserves a mention, at least, for this kind of job. I'm pretty sure the dateutil equivalent of Kent's second approach would look something like this, but I haven't thoroughly tested it, YMMV ... from datetime import datetime from dateutil.relativedelta import relativedelta, FR now = datetime.now() delta = relativedelta( days=1, weekday=FR, hour=1, minute=30, second=0, microsecond=0 ) nextFriday = now + delta print nextFriday ... dateutil totally rocks, and I hope someday soon it will be included in the standard library. http://labix.org/python-dateutil HTH, Marty From david at abbottdavid.com Sun May 17 03:46:02 2009 From: david at abbottdavid.com (David) Date: Sat, 16 May 2009 21:46:02 -0400 Subject: [Tutor] Find files without __doc__ strings Message-ID: <4A0F6C5A.3010707@abbottdavid.com> I am doing an exercise in Wesley Chun's book. Find files in the standard library modules that have doc strings. Then find the ones that don't, "the shame list". I came up with this to find the ones with; #!/usr/bin/python import os import glob import fileinput import re pypath = "/usr/lib/python2.6/" fnames = glob.glob(os.path.join(pypath, '*.py')) def read_doc(): pattern = re.compile('"""*\w') for line in fileinput.input(fnames): if pattern.match(line): print 'Doc String Found: ', fileinput.filename(), line read_doc() There must have been an easier way :) Now I have a problem, I can not figure out how to compare the fnames with the result fileinput.filename() and get a list of any that don,t have doc strings. thanks -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From stemarie.sylvain at gmail.com Sun May 17 04:26:31 2009 From: stemarie.sylvain at gmail.com (Sylvain Ste-Marie) Date: Sat, 16 May 2009 22:26:31 -0400 Subject: [Tutor] Allow only one instance of a process Message-ID: <4A0F75D7.3040603@gmail.com> I'm currently writing a script to batch convert video for my psp It basically looks into a folder for video and launch ffmpeg: ffmpeg -i "videoname" -f psp -r 29.97 -b 768k -ar 24000 -ab 64k -s 368x208 "videoname.mp4" my idea is basically to check for pid but how do i do that? From vinces1979 at gmail.com Sun May 17 05:26:31 2009 From: vinces1979 at gmail.com (Vince Spicer) Date: Sat, 16 May 2009 21:26:31 -0600 Subject: [Tutor] Allow only one instance of a process In-Reply-To: <4A0F75D7.3040603@gmail.com> References: <4A0F75D7.3040603@gmail.com> Message-ID: <200905162126.31231.vinces1979@gmail.com> simple solution would be to create a lock file before processing, lock = open("videoname.lck") lock.close() On Saturday 16 May 2009 8:26:31 pm Sylvain Ste-Marie wrote: > I'm currently writing a script to batch convert video for my psp > > It basically looks into a folder for video and launch ffmpeg: > > ffmpeg -i "videoname" -f psp -r 29.97 -b 768k -ar 24000 -ab 64k -s > 368x208 "videoname.mp4" > > my idea is basically to check for pid but how do i do that? > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Sun May 17 08:40:12 2009 From: denis.spir at free.fr (spir) Date: Sun, 17 May 2009 08:40:12 +0200 Subject: [Tutor] Find files without __doc__ strings In-Reply-To: <4A0F6C5A.3010707@abbottdavid.com> References: <4A0F6C5A.3010707@abbottdavid.com> Message-ID: <20090517084012.57f5b45d@o> Le Sat, 16 May 2009 21:46:02 -0400, David s'exprima ainsi: > I am doing an exercise in Wesley Chun's book. Find files in the standard > library modules that have doc strings. Then find the ones that don't, > "the shame list". I came up with this to find the ones with; > #!/usr/bin/python > import os > import glob > import fileinput > import re > > pypath = "/usr/lib/python2.6/" > fnames = glob.glob(os.path.join(pypath, '*.py')) > > def read_doc(): > pattern = re.compile('"""*\w') > for line in fileinput.input(fnames): > if pattern.match(line): > print 'Doc String Found: ', fileinput.filename(), line > > read_doc() It seems to me that your approach is moderately wrong ;-) > There must have been an easier way :) Not sure. As I see it the problem is slightly more complicated. A module doc is any triple-quoted string placed before any code. But it must be closed, too. You'll have to skip blank and comment lines, then check whether the rest matches a docstring. It could be done with a single complicated pattern, but you could also go for it step by step. Say I have a file 'dummysource.py' with the following text: ============== # !/usr/bin/env python # coding: utf8 # comment # ''' """ ''' foo module doc ''' def foofunc(): ''' foofuncdoc ''' pass ============== Then, the following doc-testing code ============== import re doc = re.compile(r'(""".+?""")|(\'\'\'.+?\'\'\')', re.DOTALL) def checkDoc(sourceFileName): sourceFile = file(sourceFileName, 'r') # move until first 'code' line while True: line = sourceFile.readline() strip_line = line.strip() print "|%s|" % strip_line if (strip_line != '') and (not strip_line.startswith('#')): break # check doc (keep last line read!) source = line + sourceFile.read() result = doc.match(source) if result is not None: print "*** %s *******" % sourceFileName print result.group() return True else: return False sourceFile = file("dummysource.py",'r') print checkDoc(sourceFile) ============== will output: ============== |# !/usr/bin/env python| |# coding: utf8| || |# comment| |# ''' """| || |''' foo module| *** dummysource.py ******* ''' foo module doc ''' True ============== It's just for illustration; you can probably make things simpler or find a better way. > Now I have a problem, I can not figure out how to compare the fnames > with the result fileinput.filename() and get a list of any that don,t > have doc strings. You can use a func like the above one to filter out (or in) files that answer yes/no to the test. I would start with a list of all files, and just populate 2 new lists for "shame" and "fame" files ;-) according to the result of the test. You could use list comprehension syntax, too: fameFileNames = [fileName for fileName in fileNames if checkDoc(fileName)] But if you do this for shame files too, then every file gets tested twice. > thanks Denis ------ la vita e estrany From david at abbottdavid.com Sun May 17 17:37:25 2009 From: david at abbottdavid.com (David) Date: Sun, 17 May 2009 11:37:25 -0400 Subject: [Tutor] Find files without __doc__ strings In-Reply-To: <20090517084012.57f5b45d@o> References: <4A0F6C5A.3010707@abbottdavid.com> <20090517084012.57f5b45d@o> Message-ID: <4A102F35.6050104@abbottdavid.com> spir wrote: > Le Sat, 16 May 2009 21:46:02 -0400, > David s'exprima ainsi: > >> I am doing an exercise in Wesley Chun's book. Find files in the standard >> library modules that have doc strings. Then find the ones that don't, >> "the shame list". I came up with this to find the ones with; >> #!/usr/bin/python >> import os >> import glob >> import fileinput >> import re >> >> pypath = "/usr/lib/python2.6/" >> fnames = glob.glob(os.path.join(pypath, '*.py')) >> >> def read_doc(): >> pattern = re.compile('"""*\w') >> for line in fileinput.input(fnames): >> if pattern.match(line): >> print 'Doc String Found: ', fileinput.filename(), line >> >> read_doc() > > It seems to me that your approach is moderately wrong ;-) > >> There must have been an easier way :) > > Not sure. As I see it the problem is slightly more complicated. A module doc is any triple-quoted string placed before any code. But it must be closed, too. > You'll have to skip blank and comment lines, then check whether the rest matches a docstring. It could be done with a single complicated pattern, but you could also go for it step by step. > Say I have a file 'dummysource.py' with the following text: > ============== > # !/usr/bin/env python > # coding: utf8 > > # comment > # ''' """ > > ''' foo module > doc > ''' > def foofunc(): > ''' foofuncdoc ''' > pass > ============== > > Then, the following doc-testing code > ============== > import re > doc = re.compile(r'(""".+?""")|(\'\'\'.+?\'\'\')', re.DOTALL) > > def checkDoc(sourceFileName): > sourceFile = file(sourceFileName, 'r') > # move until first 'code' line > while True: > line = sourceFile.readline() > strip_line = line.strip() > print "|%s|" % strip_line > if (strip_line != '') and (not strip_line.startswith('#')): > break > # check doc (keep last line read!) > source = line + sourceFile.read() > result = doc.match(source) > if result is not None: > print "*** %s *******" % sourceFileName > print result.group() > return True > else: > return False > > sourceFile = file("dummysource.py",'r') > print checkDoc(sourceFile) > ============== > > will output: > > ============== > |# !/usr/bin/env python| > |# coding: utf8| > || > |# comment| > |# ''' """| > || > |''' foo module| > *** dummysource.py ******* > ''' foo module > doc > ''' > True > ============== > > It's just for illustration; you can probably make things simpler or find a better way. > >> Now I have a problem, I can not figure out how to compare the fnames >> with the result fileinput.filename() and get a list of any that don,t >> have doc strings. > > You can use a func like the above one to filter out (or in) files that answer yes/no to the test. > I would start with a list of all files, and just populate 2 new lists for "shame" and "fame" files ;-) according to the result of the test. > > You could use list comprehension syntax, too: > fameFileNames = [fileName for fileName in fileNames if checkDoc(fileName)] > But if you do this for shame files too, then every file gets tested twice. > >> thanks > > Denis > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Thanks Denis, This seems to work OK; #!/usr/bin/python import os import glob import fileinput import re pypath = "/usr/lib/python2.6/" fnames = glob.glob(os.path.join(pypath, '*.py')) fnames.sort() goodFiles = [] def shame_list(): pattern = re.compile(r'(^""")|(^\'\'\')', re.DOTALL) for line in fileinput.input(fnames): if pattern.match(line): found = fileinput.filename() goodFiles.append(found) goodFiles.sort() for item in fnames: if item in goodFiles: fnames.remove(item) print 'Shame List: \n', fnames shame_list() Shame List: ['/usr/lib/python2.6/__phello__.foo.py', '/usr/lib/python2.6/collections.py', '/usr/lib/python2.6/md5.py', '/usr/lib/python2.6/pydoc_topics.py', '/usr/lib/python2.6/sha.py', '/usr/lib/python2.6/struct.py', '/usr/lib/python2.6/this.py'] -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From walker.hale.iv at gmail.com Sun May 17 18:33:40 2009 From: walker.hale.iv at gmail.com (Walker Hale IV) Date: Sun, 17 May 2009 11:33:40 -0500 Subject: [Tutor] Python popen command using cat > textfile .... how to terminate In-Reply-To: <4A0DAA6E.9030306@nibrahim.net.in> References: <1242337826.7940.5.camel@DadTux> <4A0DAA6E.9030306@nibrahim.net.in> Message-ID: <312f43620905170933i4bd627bcy57436a2eb2d87a7a@mail.gmail.com> On Fri, May 15, 2009 at 12:46 PM, Noufal Ibrahim wrote: > MK wrote: >> >> Hi there, >> >> i am using this code to send an "cat > ThisIsMyUrl" with popen. >> Of cos cat now waits for the CTRL+D command. How can i send this command ? > > Wouldn't it be better if you directly opened the "ThisIsMyUrl" file and > wrote the text into it rather than rely on shelling out for this? > > [..] > > -- > ~noufal > http://nibrahim.net.in/ Agreed. Something like this perhaps? import sys def getFileFromUser(): f = open('ThisIsMyUrl', 'w') while True: s = sys.stdin.readline() if len(s) == 0: break # empty string denotes EOF f.write(s) f.close() Seems to work on my Mac. -- Walker Hale From kent37 at tds.net Sun May 17 20:53:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 17 May 2009 14:53:43 -0400 Subject: [Tutor] Find files without __doc__ strings In-Reply-To: <4A0F6C5A.3010707@abbottdavid.com> References: <4A0F6C5A.3010707@abbottdavid.com> Message-ID: <1c2a2c590905171153k7c670f1ei62c823a466bae1e1@mail.gmail.com> On Sat, May 16, 2009 at 9:46 PM, David wrote: > I am doing an exercise in Wesley Chun's book. Find files in the standard > ?library modules that have doc strings. Then find the ones that don't, "the > shame list". I came up with this to find the ones with; > #!/usr/bin/python > import os > import glob > import fileinput > import re > > pypath = "/usr/lib/python2.6/" > fnames = glob.glob(os.path.join(pypath, '*.py')) > > def read_doc(): > ? ?pattern = re.compile('"""*\w') > ? ?for line in fileinput.input(fnames): > ? ? ? ?if pattern.match(line): > ? ? ? ? ? ?print 'Doc String Found: ', fileinput.filename(), line > > read_doc() > > There must have been an easier way :) One problem with this is that not every triple-quoted string is a doc string. Another problem is that doc strings can use triple-single-quotes. You might look at how pydoc (in the std lib) solves this problem. Kent From kent37 at tds.net Sun May 17 20:54:54 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 17 May 2009 14:54:54 -0400 Subject: [Tutor] Allow only one instance of a process In-Reply-To: <4A0F75D7.3040603@gmail.com> References: <4A0F75D7.3040603@gmail.com> Message-ID: <1c2a2c590905171154k280ce079tcb0065a741bb10fe@mail.gmail.com> On Sat, May 16, 2009 at 10:26 PM, Sylvain Ste-Marie wrote: > I'm currently writing a script to batch convert video for my psp > > It basically looks into a folder for video and launch ffmpeg: > > ffmpeg -i "videoname" -f psp -r 29.97 -b 768k -ar 24000 -ab 64k -s 368x208 > "videoname.mp4" > > my idea is basically to check for pid but how do i do that? Look at psutil: http://code.google.com/p/psutil/ But you will have a race condition if you do something like this: if ffmpeg is not running: start ffmpeg A file lock is a better solution. Google 'python file lock' for recipes. Kent From allen.fowler at yahoo.com Mon May 18 05:36:50 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Sun, 17 May 2009 20:36:50 -0700 (PDT) Subject: [Tutor] Shared FIFO? Message-ID: <184187.47143.qm@web45603.mail.sp1.yahoo.com> Hello, I have several CGI scripts that I would like coordinate via a "First In / First Out" style buffer. That is, some processes are adding work units, and some take the oldest and start work on them. What is the right way to do this? I suppose I could use an SQL server or SQlite , but that seems very complex for just a FIFO. Thank you, :) From alan.gauld at btinternet.com Mon May 18 09:19:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 18 May 2009 08:19:21 +0100 Subject: [Tutor] Shared FIFO? References: <184187.47143.qm@web45603.mail.sp1.yahoo.com> Message-ID: "Allen Fowler" wrote > I have several CGI scripts that I would like coordinate via a > "First In / First Out" style buffer. That is, some processes > are adding work units, and some take the oldest and > start work on them. > > What is the right way to do this? > I suppose I could use an SQL server or SQlite , > but that seems very complex for just a FIFO. Given how easy it is to use something like SqlLite from Python I don't think its an unreasonable approach and it avoids or eases some of the hidden complexities around using simple files (such as locking issues). But a lot will depend on transaction volumes etc. If you never, or rarely, have concurrent accesses then using a text file might work. Or you could do it all in memory via a long running process and a queue. But if volumes are (or could become) high or bursty I'd go for the SQL route. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Mon May 18 11:40:53 2009 From: denis.spir at free.fr (spir) Date: Mon, 18 May 2009 11:40:53 +0200 Subject: [Tutor] Shared FIFO? In-Reply-To: References: <184187.47143.qm@web45603.mail.sp1.yahoo.com> Message-ID: <20090518114053.32c8b890@o> Le Mon, 18 May 2009 08:19:21 +0100, "Alan Gauld" s'exprima ainsi: > If > you never, or rarely, have concurrent accesses then using > a text file might work. That's what I would do. New tasks appended --> oldest one = first one (first line, probably). For access sharing, then you can have a very basic def accessDataFile(thingToDo): while True: try: thingToDo break Except ...: wait(a_bit) Denis ------ la vita e estrany From kent37 at tds.net Mon May 18 12:42:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 18 May 2009 06:42:42 -0400 Subject: [Tutor] Shared FIFO? In-Reply-To: <184187.47143.qm@web45603.mail.sp1.yahoo.com> References: <184187.47143.qm@web45603.mail.sp1.yahoo.com> Message-ID: <1c2a2c590905180342o665bd1d7xfb2e62b14b0a87a1@mail.gmail.com> On Sun, May 17, 2009 at 11:36 PM, Allen Fowler wrote: > > Hello, > > I have several CGI scripts that I would like coordinate via a "First In / First Out" style buffer. ? ?That is, some processes are adding work units, and some take the oldest and start work on them. > > What is the right way to do this? ? ?I suppose I could use an SQL server or SQlite , but that seems very complex for just a FIFO. Are the worker processes CGI scripts too? That doesn't make sense to me. You could consolidate all the user-facing CGI scripts into one process using something like CherryPy or Django, then use the multiprocessing module in the std lib to farm out the work to other processes. One advantage of using a disk-based queue vs multiprocessing.Queue is that it is more robust against system failures; a catastrophic failure will lose work in progress and work not yet queued but items in the queue should be preserved. Kent From lie.1296 at gmail.com Mon May 18 15:27:06 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 18 May 2009 23:27:06 +1000 Subject: [Tutor] Python popen command using cat > textfile .... how toterminate In-Reply-To: References: <1242382754.6420.8.camel@DadTux> Message-ID: Alan Gauld wrote: > > "MK" wrote > >> Seems that i did it the wrong way still from the beginning. >> I did it now with open and write an empty file. >> >> But anyway i would wish to know if it is possible to terminate >> a running cat. > > It depends on what you mean by a running cat. Of course it meant "a running cat" Ok... now stop running around... sit... sit!! Ohh... I wish I had a dog instead... From lie.1296 at gmail.com Mon May 18 15:36:48 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 18 May 2009 23:36:48 +1000 Subject: [Tutor] Find files without __doc__ strings In-Reply-To: <4A102F35.6050104@abbottdavid.com> References: <4A0F6C5A.3010707@abbottdavid.com> <20090517084012.57f5b45d@o> <4A102F35.6050104@abbottdavid.com> Message-ID: David wrote: > spir wrote: >> Le Sat, 16 May 2009 21:46:02 -0400, >> David s'exprima ainsi: >> >>> I am doing an exercise in Wesley Chun's book. Find files in the >>> standard library modules that have doc strings. Then find the ones >>> that don't, "the shame list". I came up with this to find the ones with; why not __import__() it then test whether its .__doc__ is None? def test(filename): if __import__(filename).__doc__ is None: shame_list.append(filename) else: fame_list.append(filename) From allen.fowler at yahoo.com Mon May 18 16:47:44 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Mon, 18 May 2009 07:47:44 -0700 (PDT) Subject: [Tutor] Shared FIFO? In-Reply-To: References: <184187.47143.qm@web45603.mail.sp1.yahoo.com> Message-ID: <767837.98874.qm@web45604.mail.sp1.yahoo.com> > "Allen Fowler" wrote > > I have several CGI scripts that I would like coordinate via a "First In / > First Out" style buffer. That is, some processes are adding work units, and > some take the oldest and start work on them. > > > > What is the right way to do this? I suppose I could use an SQL server or > SQlite , but that seems very complex for just a FIFO. > > Given how easy it is to use something like SqlLite from Python I don't think its > an unreasonable approach and it avoids or eases some of the hidden complexities > around using simple files (such as locking issues). > Yeah. Now that I think about there are ton of small issues that using sqlite would take care of. For clarification, this is not just for CGI scripts... there will be cron initiated processes and perhaps a TurboGears app accessing the same buffer. Since this sounds like it's been done before, can anyone direct me to tutorial or how-to on the subject? thank you, :) From dineshbvadhia at hotmail.com Mon May 18 18:34:00 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Mon, 18 May 2009 09:34:00 -0700 Subject: [Tutor] reading nested folders in gzip files Message-ID: The structure of the gzip files are: gzip archive folderA folderB list of folderC's each folderC contains the target files Within the archive, I want to open the gzip archive, open folderA, openFolderB , get the list of target files in folderC, and extract each file in folderC individually. I've used gzip before but cannot see how to move from folderA to folder B within the archive. Any ideas? Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Mon May 18 19:28:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 18 May 2009 13:28:13 -0400 Subject: [Tutor] reading nested folders in gzip files In-Reply-To: References: Message-ID: <1c2a2c590905181028vf731d86o3c88f7b80fcaf819@mail.gmail.com> On Mon, May 18, 2009 at 12:34 PM, Dinesh B Vadhia wrote: > The structure of the gzip files are: > > gzip archive > ????folderA > ????????folderB > ????????????list of folderC's > ????????????????each folderC contains the target files > > Within the archive, I want to open the gzip archive, open folderA, > openFolderB , get the list of target files in folderC, and extract each file > in folderC individually. > > I've used gzip before but cannot see how to move from folderA to folder B > within the archive.? Any ideas? Presumably the gzip file is a tar archive? If so see the examples for the tarfile module, they show something very similar to what you need. The fourth example shows how to see what is in the file and the first example shows how to filter it. http://docs.python.org/library/tarfile.html#examples Kent From david at abbottdavid.com Mon May 18 20:41:49 2009 From: david at abbottdavid.com (David) Date: Mon, 18 May 2009 14:41:49 -0400 Subject: [Tutor] [Fwd: Re: Find files without __doc__ strings] Message-ID: <4A11ABED.9070502@abbottdavid.com> -- powered by Gentoo/GNU Linux http://linuxcrazy.com -------------- next part -------------- An embedded message was scrubbed... From: David Subject: Re: [Tutor] Find files without __doc__ strings Date: Mon, 18 May 2009 14:02:38 -0400 Size: 2033 URL: From denis.spir at free.fr Mon May 18 20:49:47 2009 From: denis.spir at free.fr (spir) Date: Mon, 18 May 2009 20:49:47 +0200 Subject: [Tutor] performance loss -- profiling Message-ID: <20090518204947.4f1baa30@o> Hello, I have an big performance problem with an app I'm currently working on. It "suddenly" runs at least 5 times slower that it used to. The issue is, beeing in a finalization phase, I'm all the time touching thingies here and there. But performance change is visible only when running on big test data, which I haven't done for a few days. As a consequence I'm unable to find what bug I've introduced that causes such a change. If you of any rational method to diagnose such problems, thank you very much. Also, it's the first time I really have to cope with machine-time; so I'm totally new to technics like using a profiler. Any hints on the topics heartfully welcome :-) Denis ------ la vita e estrany From david at abbottdavid.com Mon May 18 20:02:38 2009 From: david at abbottdavid.com (David) Date: Mon, 18 May 2009 14:02:38 -0400 Subject: [Tutor] Find files without __doc__ strings In-Reply-To: References: <4A0F6C5A.3010707@abbottdavid.com> <20090517084012.57f5b45d@o> <4A102F35.6050104@abbottdavid.com> Message-ID: <4A11A2BE.3010707@abbottdavid.com> Lie Ryan wrote: > David wrote: >> spir wrote: >>> Le Sat, 16 May 2009 21:46:02 -0400, >>> David s'exprima ainsi: >>> >>>> I am doing an exercise in Wesley Chun's book. Find files in the >>>> standard library modules that have doc strings. Then find the >>>> ones that don't, "the shame list". I came up with this to find the >>>> ones with; > > why not __import__() it then test whether its .__doc__ is None? > > def test(filename): > if __import__(filename).__doc__ is None: > shame_list.append(filename) > else: > fame_list.append(filename) > Thanks Spir and Lie, How about; #!/usr/bin/python import os import glob import os.path shame_list = [] fame_list = [] pypath = "/usr/lib/python2.5/" def grab_files(): fnames = glob.glob(os.path.join(pypath, '*.py')) fnames.sort() sendall(fnames) def sendall(fnames): for filename in fnames: filename = os.path.split(filename) filename = filename[1] filename = os.path.splitext(filename) filename = filename[0] test(filename) def test(filename): try: if __import__(filename).__doc__ is None: shame_list.append(filename) else: fame_list.append(filename) except ImportError: pass grab_files() print 'Shame List: ', shame_list How can I do; def sendall(fnames): [snip] More efficient. -- powered by Gentoo/GNU Linux http://linuxcrazy.com From alan.gauld at btinternet.com Tue May 19 00:16:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 18 May 2009 23:16:13 +0100 Subject: [Tutor] performance loss -- profiling References: <20090518204947.4f1baa30@o> Message-ID: "spir" wrote > Also, it's the first time I really have to cope with machine-time; > so I'm totally new to technics like using a profiler. > Any hints on the topics heartfully welcome :-) Profilers are a bit like debuggers. Very useful when needed but usually a point of last resort. First, what OS are you on? Linux/Unix has a host of tools that can help pinpoint the causes, such as whether there is excess disk I/O or memory use or if its another process stealing CPU resource. Windows can do these things too but the tools and techniques are totally different - and require a bit more experience to interpret IMHO. But my first bet would be to check the CPU and memory usage to see if either is high. Also if it uses the network, either to go to a server(databases? files?) then check network traffic. If you can spot the nature of the hot spot you can often guess where in the code the issues are likely to be. Other common causes to consider are: - Locking of files? ie concurrent access issues? - Queuing for shared resources? Have you started running multiple instances? - data base query time - have you increased the volume of data being searched? Did you index the key columns? - Directory path traversal - have you moved the location of any key files? Either the executables/scriopts or the data? Just some ideas before you resort to profiling. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From matthew.strax-haber at nasa.gov Tue May 19 00:21:04 2009 From: matthew.strax-haber at nasa.gov (Strax-Haber, Matthew (LARC-D320)) Date: Mon, 18 May 2009 17:21:04 -0500 Subject: [Tutor] Replacement for __del__ Message-ID: To all the generous Python coders on this list, I've been told numerous times that using __del__ is not the way to handle clean-up because it is finicky. Fine. However, how should I handle the following problem: A folder is created during object instantiation. This is necessary because multiple other methods depend on the existence of that folder, and in the future things may be running in parallel so it has to be there for the entire life of the object. Before the object is deleted, I want that temp folder to be deleted (ala shutil.rmtree). Is there a better way to do this? Regardless of whether I should be using __del__, can anyone explain this to me (to satisfy curiosity): My class sub-types dict. In __init__, there is a call to self.update(pickle.load()). For some reason, this triggers __del__. Why? Thanks in advance to whomever responds. I hope I was clear enough. -- ~ Matthew Strax-Haber National Aeronautics and Space Administration Langley Research Center (LaRC) Co-op, Safety-Critical Avionics Systems Branch Student, Northeastern University W: 757-864-7378; C: 561-704-0029 Matthew.Strax-Haber at nasa.gov From alan.gauld at btinternet.com Tue May 19 01:13:07 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 May 2009 00:13:07 +0100 Subject: [Tutor] Replacement for __del__ References: Message-ID: "Strax-Haber, Matthew (LARC-D320)" wrote > My class sub-types dict. In __init__, there is a call to > self.update(pickle.load()). For some reason, this triggers > __del__. Why? > A pure guess, but maybe the pickle load creates a new instance which replaces the original which then gets garbage collected and calls del? Otherwise go to the source, or step into it with a debugger Just a thought, Alan G. From wescpy at gmail.com Tue May 19 01:45:45 2009 From: wescpy at gmail.com (wesley chun) Date: Mon, 18 May 2009 16:45:45 -0700 Subject: [Tutor] Replacement for __del__ In-Reply-To: References: Message-ID: <78b3a9580905181645g841b688re23c38f45f6a3c9@mail.gmail.com> > I've been told numerous times that using __del__ is not the way to handle > clean-up because it is finicky. matthew, welcome to Python and this Tutor mailing list! and yes, i'll echo everyone else in saqying that using __del__ should be avoided. the most critical issues regarding this is because there are object references everywhere, and it's not prdictable when they get to zero and GC'd, meaning you don't if/when it will execute. also, during program shutdown, as objects are being deallocated during termination, some of the objects in your __del__ may have already been released so exceptions may occur. to get around this exceptions occurring within __del__ are *ignored*. more here: http://docs.python.org/reference/datamodel.html#customization > Fine. However, how should I handle the > following problem: > A folder is created during object instantiation. This is necessary because > multiple other methods depend on the existence of that folder, and in the > future things may be running in parallel so it has to be there for the > entire life of the object. Before the object is deleted, I want that temp > folder to be deleted (ala shutil.rmtree). Is there a better way to do this? is your program threaded? if not, you can delete the filetree at the end of your application, right before it ends. if it is threaded, you may consider join()ing them all before deleting. 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 From lie.1296 at gmail.com Tue May 19 07:38:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 19 May 2009 15:38:49 +1000 Subject: [Tutor] [Fwd: Re: Find files without __doc__ strings] In-Reply-To: <4A11ABED.9070502@abbottdavid.com> References: <4A11ABED.9070502@abbottdavid.com> Message-ID: David wrote: > > ------------------------------------------------------------------------ > > Subject: > Re: [Tutor] Find files without __doc__ strings > From: > David > Date: > Mon, 18 May 2009 14:02:38 -0400 > > CC: > tutor at python.org > > > Lie Ryan wrote: >> David wrote: >>> spir wrote: >>>> Le Sat, 16 May 2009 21:46:02 -0400, >>>> David s'exprima ainsi: >>>> >>>>> I am doing an exercise in Wesley Chun's book. Find files in the >>>>> standard library modules that have doc strings. Then find the >>>>> ones that don't, "the shame list". I came up with this to find the >>>>> ones with; >> why not __import__() it then test whether its .__doc__ is None? >> >> def test(filename): >> if __import__(filename).__doc__ is None: >> shame_list.append(filename) >> else: >> fame_list.append(filename) >> > Thanks Spir and Lie, > How about; > > #!/usr/bin/python > > import os > import glob > import os.path > > shame_list = [] > fame_list = [] > pypath = "/usr/lib/python2.5/" > > def grab_files(): > fnames = glob.glob(os.path.join(pypath, '*.py')) > fnames.sort() > sendall(fnames) > > def sendall(fnames): > for filename in fnames: > filename = os.path.split(filename) > filename = filename[1] > filename = os.path.splitext(filename) > filename = filename[0] > test(filename) > > > def test(filename): > try: > if __import__(filename).__doc__ is None: > shame_list.append(filename) > else: > fame_list.append(filename) > except ImportError: > pass > > grab_files() > print 'Shame List: ', shame_list > > How can I do; > def sendall(fnames): > [snip] > More efficient. I don't know about being "more efficient" but using os.path.basename() can save one step: filename = os.path.splitext(os.path.basename(filename))[0] and you may try to use os.path.walk() for your grab_files(). os.path.walk will walk recursively through subdirectories and grabs other stdlib packages you may have missed (note: although it also grabs non standard-lib packages; note: you also cannot use glob with it; these are tradeoffs, so find out which is easier for you) Also in this code: > def test(filename): > try: > if __import__(filename).__doc__ is None: > shame_list.append(filename) > else: > fame_list.append(filename) > except ImportError: > pass avoid globals... From a.t.hofkamp at tue.nl Tue May 19 09:12:34 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Tue, 19 May 2009 09:12:34 +0200 Subject: [Tutor] Replacement for __del__ In-Reply-To: References: Message-ID: <4A125BE2.2090903@tue.nl> Strax-Haber, Matthew (LARC-D320) wrote: > A folder is created during object instantiation. This is necessary because > multiple other methods depend on the existence of that folder, and in the > future things may be running in parallel so it has to be there for the > entire life of the object. Before the object is deleted, I want that temp > folder to be deleted (ala shutil.rmtree). Is there a better way to do this? It sounds like the object has 2 functions, namely managing your temp disk space, and the functionality of whatever the other methods of the object do. I would suggest to split the temp disk space management to a seperate function that acts as a kind of main function: def main(): # make temp disk space obj = YourObject() obj.doit() # Run the application # Application is finished here, cleanup the disk space. This gives you explicit control when the disk space is cleared. For a multi-threaded app you may want to wait somewhere until all threads are finished before proceeding to clean up the disk. > Regardless of whether I should be using __del__, can anyone explain this to > me (to satisfy curiosity): > My class sub-types dict. In __init__, there is a call to > self.update(pickle.load()). For some reason, this triggers > __del__. Why? Somewhere an instance is created and garbage collected. This is why you really don't want to use __del__. It is very good way to make your app behave strangely and give you weeks of debugging fun :p Albert From denis.spir at free.fr Tue May 19 11:26:14 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 11:26:14 +0200 Subject: [Tutor] performance loss -- profiling In-Reply-To: References: <20090518204947.4f1baa30@o> Message-ID: <20090519112614.5998ae8f@o> Le Mon, 18 May 2009 23:16:13 +0100, "Alan Gauld" s'exprima ainsi: > "spir" wrote > > > Also, it's the first time I really have to cope with machine-time; > > so I'm totally new to technics like using a profiler. > > Any hints on the topics heartfully welcome :-) > > Profilers are a bit like debuggers. Very useful when needed > but usually a point of last resort. > > First, what OS are you on? > Linux/Unix has a host of tools that can help pinpoint the causes, > such as whether there is excess disk I/O or memory use or > if its another process stealing CPU resource. > > Windows can do these things too but the tools and techniques > are totally different - and require a bit more experience to > interpret IMHO. > > But my first bet would be to check the CPU and memory usage > to see if either is high. Also if it uses the network, either to go to > a server(databases? files?) then check network traffic. > > If you can spot the nature of the hot spot you can often guess > where in the code the issues are likely to be. > > Other common causes to consider are: > - Locking of files? ie concurrent access issues? > - Queuing for shared resources? Have you started running multiple > instances? > - data base query time - have you increased the volume of > data being searched? Did you index the key columns? > - Directory path traversal - have you moved the location of > any key files? Either the executables/scriopts or the data? > > Just some ideas before you resort to profiling. Thank you for all these tips, Alan. I've found the naughty bug. [It brings another question: see separate thread.] For the story, the app in question is a parsing and processing tool (see http://spir.wikidot.com/pijnu). I've put muuuch attention to usability, esp. feedback to developper. To help users fix grammars, which is always a difficult job, parse errors are very complete and even include a visual pointer to the failure location in source text. The main trick is that "wrapper" patterns also report errors from sub-patterns. For instance, a sequence will tell the user which pattern in the sequence has failed and add this pattern's own error on output; a choice will report why _each_ pattern in the choice has failed. To do this, wrapper patterns have to record sub-pattern errors: _my_ error was that they recorded the error message (which is complicated to create) instead of a reference to the error object. Error objects correctly don't write their message until they are requested -- but wrapper patterns asked them to do it. As a consequence, the app spent ~98% of its time writing error messages that never got printed ;-) My biggest test's running time had raised from ~1.5s to more than 1mn! Denis ------ la vita e estrany From denis.spir at free.fr Tue May 19 11:36:17 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 11:36:17 +0200 Subject: [Tutor] clean text Message-ID: <20090519113617.1ea6319c@o> Hello, This is a follow the post on performance issues. Using a profiler, I realized that inside error message creation, most of the time was spent in a tool func used to clean up source text output. The issue is that when the source text holds control chars such as \n, then the error message is hardly readible. MY solution is to replace such chars with their repr(): def _cleanRepr(text): ''' text with control chars replaced by repr() equivalent ''' result = "" for char in text: n = ord(char) if (n < 32) or (n > 126 and n < 160): char = repr(char)[1:-1] result += char return result For any reason, this func is extremely slow. While the rest of error message creation looks very complicated, this seemingly innocent consume > 90% of the time. The issue is that I cannot use repr(text), because repr will replace all non-ASCII characters. I need to replace only control characters. How else could I do that? Denis ------ la vita e estrany From denis.spir at free.fr Tue May 19 11:55:49 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 11:55:49 +0200 Subject: [Tutor] Replacement for __del__ In-Reply-To: <4A125BE2.2090903@tue.nl> References: <4A125BE2.2090903@tue.nl> Message-ID: <20090519115549.25b563f1@o> Le Tue, 19 May 2009 09:12:34 +0200, "A.T.Hofkamp" s'exprima ainsi: > A folder is created during object instantiation. Do you mean a filesytem directory? I may be wrong, bit it seems there is a bit of confusion between things and their representation as python objects. You shouldn't call __del__, id est try to do garbage collection instead of letting python do it. This is for the python object side. But I think (someone confirms/contradicts?) there is no harm in overloading a class's __del__ to accomplish additional tasks such as deleting temp disk space/dirs/files that need to exist only during object creation. If this looks you what you intend to do, then there are alternatives such as using "try...finally" to ensure cleanup (also "with"?). Sorry if I misunderstood. Denis ------ la vita e estrany From kent37 at tds.net Tue May 19 12:55:26 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 19 May 2009 06:55:26 -0400 Subject: [Tutor] performance loss -- profiling In-Reply-To: <20090518204947.4f1baa30@o> References: <20090518204947.4f1baa30@o> Message-ID: <1c2a2c590905190355i6a68f986j9ae822167e9de454@mail.gmail.com> On Mon, May 18, 2009 at 2:49 PM, spir wrote: > Hello, > > I have an big performance problem with an app I'm currently working on. > It "suddenly" runs at least 5 times slower that it used to. The issue is, beeing in a finalization phase, I'm all the time touching thingies here and there. But performance change is visible only when running on big test data, which I haven't done for a few days. > > As a consequence I'm unable to find what bug I've introduced that causes such a change. If you of any rational method to diagnose such problems, thank you very much. > > Also, it's the first time I really have to cope with machine-time; so I'm totally new to technics like using a profiler. Any hints on the topics heartfully welcome :-) Are you using a source code control system? If so, you can back out changes and see where the performance changes. Some SCCS will even help you with this, for example the Mercurial bisect command automates a binary search through the repository changesets. The Python profiler is not hard to run. Interpreting the results is more difficult :-) See the docs to get started: http://docs.python.org/library/profile.html Because profiling can slow your code down considerably, if you have a general idea where the slowdown is, you can just profile that. The output of the profiler is a file which you can load in an interactive session using pstats.Stats. You will want to sort the stats by cumulative time and print. Look for something that is taking an unexpectedly large amount of the time, or that is being called more than expected. RunSnakeRun looks like a useful program for visualizing the profile results: http://www.vrplumber.com/programming/runsnakerun/ PProfUI is a simpler profile viewer that I have found useful: http://webpages.charter.net/erburley/pprofui.html Once you find a hot spot, you have to figure out why it is hot. Is it being called to much? Is there a better algorithm or data structure? The profiler won't help with this part. line_profiler might be useful, I haven't used it: http://packages.python.org/line_profiler/ Kent From denis.spir at free.fr Tue May 19 13:08:42 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 13:08:42 +0200 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: <20090519130842.0f54d92a@o> Le Tue, 19 May 2009 11:36:17 +0200, spir s'exprima ainsi: > Hello, > > This is a follow the post on performance issues. > Using a profiler, I realized that inside error message creation, most of > the time was spent in a tool func used to clean up source text output. The > issue is that when the source text holds control chars such as \n, then the > error message is hardly readible. MY solution is to replace such chars with > their repr(): > > def _cleanRepr(text): > ''' text with control chars replaced by repr() equivalent ''' > result = "" > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > result += char > return result Changed to: def _cleanRepr(text): ''' text with control chars replaced by repr() equivalent ''' chars = [] for char in text: n = ord(char) if (n < 32) or (n > 126 and n < 160): char = repr(char)[1:-1] chars.append(char) return ''.join(chars) But what else can I do? Denis ------ la vita e estrany From kent37 at tds.net Tue May 19 13:18:23 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 19 May 2009 07:18:23 -0400 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: <1c2a2c590905190418g1158e1d5w7cd0b8a3c8ed3ce8@mail.gmail.com> On Tue, May 19, 2009 at 5:36 AM, spir wrote: > Hello, > > This is a follow the post on performance issues. > Using a profiler, I realized that inside error message creation, most of the time was spent in a tool func used to clean up source text output. > The issue is that when the source text holds control chars such as \n, then the error message is hardly readible. MY solution is to replace such chars with their repr(): > > def _cleanRepr(text): > ? ? ? ?''' text with control chars replaced by repr() equivalent ''' > ? ? ? ?result = "" > ? ? ? ?for char in text: > ? ? ? ? ? ? ? ?n = ord(char) > ? ? ? ? ? ? ? ?if (n < 32) or (n > 126 and n < 160): > ? ? ? ? ? ? ? ? ? ? ? ?char = repr(char)[1:-1] > ? ? ? ? ? ? ? ?result += char > ? ? ? ?return result > > For any reason, this func is extremely slow. While the rest of error message creation looks very complicated, this seemingly innocent consume > 90% of the time. The issue is that I cannot use repr(text), because repr will replace all non-ASCII characters. I need to replace only control characters. > How else could I do that? I would get rid of the calls to ord() and repr() to start. There is no need for ord() at all, just compare characters directly: if char < '\x20' or '\x7e' < char < '\xa0': To eliminate repr() you could create a dict mapping chars to their repr and look up in that. You should also look for a way to get the loop into C code. One way to do that would be to use a regex to search and replace. The replacement pattern in a call to re.sub() can be a function call; the function can do the dict lookup. Here is something to try: import re # Create a dictionary of replacement strings repl = {} for i in range(0, 32) + range(127, 160): c = chr(i) repl[c] = repr(c)[1:-1] def sub(m): ''' Helper function for re.sub(). m will be a Match object. ''' return repl[m.group()] # Regex to match control chars controlsRe = re.compile(r'[\x00-\x1f\x7f-\x9f]') def replaceControls(s): ''' Replace all control chars in s with their repr() ''' return controlsRe.sub(sub, s) for s in [ 'Simple test', 'Harder\ntest', 'Final\x00\x1f\x20\x7e\x7f\x9f\xa0test' ]: print replaceControls(s) Kent From a.t.hofkamp at tue.nl Tue May 19 13:28:09 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Tue, 19 May 2009 13:28:09 +0200 Subject: [Tutor] clean text In-Reply-To: <20090519130842.0f54d92a@o> References: <20090519113617.1ea6319c@o> <20090519130842.0f54d92a@o> Message-ID: <4A1297C9.3000507@tue.nl> spir wrote: > def _cleanRepr(text): > ''' text with control chars replaced by repr() equivalent ''' > chars = [] > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > chars.append(char) > return ''.join(chars) > > But what else can I do? You seem to break down the string to single characters, replace a few of them, and then build the whole string back. Maybe you can insert larger chunks of text that do not need modification, ie something like start = 0 for idx, char in text: n = ord(char) if n < 32 or 126 < n < 160: chars.append(text[start:idx]) chars.append(repr(char)[1:-1]) start = idx + 1 chars.append(text[start:]) return ''.join(chars) An alternative of the above is to keep track of the first occurrence of each of the chars you want to split on (after some 'start' position), and compute the next point to break the string as the min of all those positions instead of slowly 'walking' to it by testing each character seperately. That would reduce the number of iterations you do in the loop, at the cost of maintaining a large number of positions of the next breaking point. Albert From mail at timgolden.me.uk Tue May 19 13:00:44 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 19 May 2009 12:00:44 +0100 Subject: [Tutor] performance loss -- profiling In-Reply-To: <1c2a2c590905190355i6a68f986j9ae822167e9de454@mail.gmail.com> References: <20090518204947.4f1baa30@o> <1c2a2c590905190355i6a68f986j9ae822167e9de454@mail.gmail.com> Message-ID: <4A12915C.90200@timgolden.me.uk> Kent Johnson wrote: > The Python profiler is not hard to run. Interpreting the results is > more difficult :-) See the docs to get started: > http://docs.python.org/library/profile.html Also, it's quite useful to run it as a module: python -mcProfile You have a certain amount of configurability via the -s param. TJG From sander.sweers at gmail.com Tue May 19 13:59:00 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 19 May 2009 13:59:00 +0200 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: 2009/5/19 spir : > def _cleanRepr(text): > ? ? ? ?''' text with control chars replaced by repr() equivalent ''' > ? ? ? ?result = "" > ? ? ? ?for char in text: > ? ? ? ? ? ? ? ?n = ord(char) > ? ? ? ? ? ? ? ?if (n < 32) or (n > 126 and n < 160): > ? ? ? ? ? ? ? ? ? ? ? ?char = repr(char)[1:-1] > ? ? ? ? ? ? ? ?result += char > ? ? ? ?return result Is this faster? It replaces all occurrences of each control character in ctrlcmap for the whole string you pass to it. ctrlcmap = dict((chr(x), repr(chr(x))[1:-1]) for x in range(160) if x < 32 or x > 126 and x < 160) teststring = chr(127) + 'mytestring' def _cleanRepr(text, ctrlcmap): for ctrlc in ctrlcmap.keys(): text = text.replace(ctrlc, ctrlcmap[ctrlc]) return text >>> teststring '\x7fmytestring' >>> _cleanRepr(teststring, ctrlcmap) '\\x7fmytestring' Greets Sander From kent37 at tds.net Tue May 19 14:29:16 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 19 May 2009 08:29:16 -0400 Subject: [Tutor] clean text In-Reply-To: References: <20090519113617.1ea6319c@o> Message-ID: <1c2a2c590905190529o19aa116anae8627b04e485af0@mail.gmail.com> By the way, the timeit module is very helpful for comparing the speed of different implementations of an algorithm such as are being presented in this thread. You can find examples in the list archives: http://search.gmane.org/?query=timeit&group=gmane.comp.python.tutor Kent From mustafa.cmpe at gmail.com Tue May 19 14:47:50 2009 From: mustafa.cmpe at gmail.com (mustafa akkoc) Date: Tue, 19 May 2009 15:47:50 +0300 Subject: [Tutor] odbc connection with python Message-ID: <401fd82b0905190547m247b826bk772e74b0c1361611@mail.gmail.com> how can i make odbc connection language and i wanna make gui project after connecting database anyone has document ? -- Mustafa Akkoc -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at bdurham.com Tue May 19 14:57:05 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 19 May 2009 08:57:05 -0400 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: <1242737825.542.1316196423@webmail.messagingengine.com> Denis, Untested idea: 1. Fill a dict with pre-calculated repr() values for chars you want to replace (replaceDict) 2. Create a set() of chars that you want to replace (replaceSet). 3. Replace if (n < 32) ... test with if char in replaceSet 4. Lookup the replacement via replaceDict[ char ] vs. calculating via repr() 5. Have result = list(), then replace result += char with result.append( char ) 6. Return ''.join( result ) Does this help? Malcolm From emile at fenx.com Tue May 19 15:33:55 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 19 May 2009 06:33:55 -0700 Subject: [Tutor] odbc connection with python In-Reply-To: <401fd82b0905190547m247b826bk772e74b0c1361611@mail.gmail.com> References: <401fd82b0905190547m247b826bk772e74b0c1361611@mail.gmail.com> Message-ID: On 5/19/2009 5:47 AM mustafa akkoc said... > how can i make odbc connection language and i wanna make gui project > after connecting database anyone has document ? > There's an odbc module in python. I'd start with the docs on that and then google 'python odbc example' for more info and examples. Emile From dstanek at dstanek.com Tue May 19 17:08:35 2009 From: dstanek at dstanek.com (David Stanek) Date: Tue, 19 May 2009 11:08:35 -0400 Subject: [Tutor] Replacement for __del__ In-Reply-To: <20090519115549.25b563f1@o> References: <4A125BE2.2090903@tue.nl> <20090519115549.25b563f1@o> Message-ID: On Tue, May 19, 2009 at 5:55 AM, spir wrote: > Le Tue, 19 May 2009 09:12:34 +0200, > "A.T.Hofkamp" s'exprima ainsi: > >> A folder is created during object instantiation. > > Do you mean a filesytem directory? I may be wrong, bit it seems there is a bit of confusion between things and their representation as python objects. > > You shouldn't call __del__, id est try to do garbage collection instead of letting python do it. This is for the python object side. Correct. > But I think (someone confirms/contradicts?) there is no harm in overloading a class's __del__ to accomplish additional tasks such as deleting temp disk space/dirs/files that need to exist only during object creation. Incorrect. I usually tell people to never supply a __del__ implementation. By the time it's executed many of the resources that you want to clean up could have already been garbage collected. To protect against this I see people doing things like:: def __del__(self): try: self.f.close() except: pass To me this is just silly. -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From lie.1296 at gmail.com Tue May 19 17:59:00 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 20 May 2009 01:59:00 +1000 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: spir wrote: > Hello, > > This is a follow the post on performance issues. > Using a profiler, I realized that inside error message creation, most of the time was spent in a tool func used to clean up source text output. > The issue is that when the source text holds control chars such as \n, then the error message is hardly readible. MY solution is to replace such chars with their repr(): > > def _cleanRepr(text): > ''' text with control chars replaced by repr() equivalent ''' > result = "" > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > result += char > return result > > For any reason, this func is extremely slow. While the rest of error message creation looks very complicated, this seemingly innocent consume > 90% of the time. The issue is that I cannot use repr(text), because repr will replace all non-ASCII characters. I need to replace only control characters. > How else could I do that? > > Denis > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > If you're using python 3x., how about using the str.translate() ? In python 3.x, str.translate() accepts dictionary argument which can do a single-char to multi-char replacement. controls = list(range(0, 32)) + list(range(127, 160)) table = {char: repr(chr(char))[1:-1] for char in controls} def _cleanRepr(text): return text.translate(table) From denis.spir at free.fr Tue May 19 19:19:02 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 19:19:02 +0200 Subject: [Tutor] clean text In-Reply-To: <20090519113617.1ea6319c@o> References: <20090519113617.1ea6319c@o> Message-ID: <20090519191902.7c6977f9@o> Le Tue, 19 May 2009 11:36:17 +0200, spir s'exprima ainsi: [...] Thank you Albert, Kent, Sanders, Lie, Malcolm. This time regex wins! Thought it wouldn't because of the additional func call (too bad we cannot pass a mapping to re.sub). Actually the diff. is very small ;-) The relevant change is indeed using a dict. Replacing string concat with ''.join() is slower (tested with 10 times and 100 times bigger strings too). Strange... Membership test in a set is only very slightly faster than in dict keys. I did a test with random strings of typical length for my app. Timing is ~ stable. =================================================== ### various funcs ### # original def cleanRepr0(text): ''' text with control chars replaced by repr() equivalent ''' chars = "" for char in text: n = ord(char) if (n < 32) or (n > 126 and n < 160): char = repr(char)[1:-1] chars += char return chars # use list def cleanRepr1(text): chars = [] for char in text: n = ord(char) if (n < 32) or (n > 126 and n < 160): char = repr(char)[1:-1] chars.append(char) return ''.join(chars) control_chars = set( chr(n) for n in (range(0, 32) + range(127, 160)) ) control_char_map = dict( (c, repr(c)[1:-1]) for c in control_chars ) # use map def cleanRepr2(text): chars = "" for char in text: if char in control_char_map: char = control_char_map[char] chars += char return chars # use map & set def cleanRepr3(text): chars = [] for char in text: if char in control_chars: char = control_char_map[char] chars.append(char) return ''.join(chars) def cleanRepr3(text): chars = "" for char in text: if char in control_chars: char = control_char_map[char] chars += char return chars import re controlsRe = re.compile(r'[\x00-\x1f\x7f-\x9f]') # use regex def substChar(m): ''' Helper function for re.sub(). m will be a Match object. ''' return control_char_map[m.group()] def cleanRepr4(text): return controlsRe.sub(substChar, text) ### timing ### #helper func to generate random string from time import time import random def randomString(): count = random.randrange(11,111) chars = [chr(random.randrange(1, 255)) for n in range(count)] return ''.join(chars) def timeAll(): t0=t1=t2=t3=t4=0 for n in range(9999): s = randomString() t = time() ; cleanRepr0(s) ; t0 += time() - t t = time() ; cleanRepr1(s) ; t1 += time() - t t = time() ; cleanRepr2(s) ; t2 += time() - t t = time() ; cleanRepr3(s) ; t3 += time() - t t = time() ; cleanRepr4(s) ; t4 += time() - t print ( "original: %.3f\n" "list: %.3f\n" "map: %.3f\n" "map & set: %.3f\n" "regex: %.3f\n" %(t0,t1,t2,t3,t4) ) timeAll() =================================================== ==> original: 0.692 list: 0.829 map: 0.364 map & set: 0.349 regex: 0.341 =================================================== Denis ------ la vita e estrany From python at bdurham.com Tue May 19 19:25:40 2009 From: python at bdurham.com (python at bdurham.com) Date: Tue, 19 May 2009 13:25:40 -0400 Subject: [Tutor] clean text In-Reply-To: <20090519191902.7c6977f9@o> References: <20090519113617.1ea6319c@o> <20090519191902.7c6977f9@o> Message-ID: <1242753940.2820.1316245937@webmail.messagingengine.com> Denis, Thank you for sharing your detailed analysis with the list. I'm glad on didn't bet money on the winner :) ... I'm just as surprised as you that the regex solution was the fastest. Malcolm From emile at fenx.com Tue May 19 19:49:15 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 19 May 2009 10:49:15 -0700 Subject: [Tutor] clean text In-Reply-To: <20090519191902.7c6977f9@o> References: <20090519113617.1ea6319c@o> <20090519191902.7c6977f9@o> Message-ID: On 5/19/2009 10:19 AM spir said... > Le Tue, 19 May 2009 11:36:17 +0200, > spir s'exprima ainsi: > > [...] > > Thank you Albert, Kent, Sanders, Lie, Malcolm. > > This time regex wins! Thought it wouldn't because of the additional func call (too bad we cannot pass a mapping to re.sub). Actually the diff. is very small ;-) The relevant change is indeed using a dict. > Replacing string concat with ''.join() is slower (tested with 10 times and 100 times bigger strings too). Strange... > Membership test in a set is only very slightly faster than in dict keys. Hmm... this seems faster assuming it does the same thing... xlate = dict( (chr(c),chr(c)) for c in range(256)) xlate.update(control_char_map) def cleanRepr5(text): return "".join([ xlate[c] for c in text ]) Emile > > I did a test with random strings of typical length for my app. Timing is ~ stable. > > =================================================== > ### various funcs ### > # original > def cleanRepr0(text): > ''' text with control chars replaced by repr() equivalent ''' > chars = "" > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > chars += char > return chars > > # use list > def cleanRepr1(text): > chars = [] > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > chars.append(char) > return ''.join(chars) > > control_chars = set( chr(n) for n in (range(0, 32) + range(127, 160)) ) > control_char_map = dict( (c, repr(c)[1:-1]) for c in control_chars ) > > # use map > def cleanRepr2(text): > chars = "" > for char in text: > if char in control_char_map: > char = control_char_map[char] > chars += char > return chars > > # use map & set > def cleanRepr3(text): > chars = [] > for char in text: > if char in control_chars: > char = control_char_map[char] > chars.append(char) > return ''.join(chars) > def cleanRepr3(text): > chars = "" > for char in text: > if char in control_chars: > char = control_char_map[char] > chars += char > return chars > > import re > controlsRe = re.compile(r'[\x00-\x1f\x7f-\x9f]') > > # use regex > def substChar(m): > ''' Helper function for re.sub(). m will be a Match object. ''' > return control_char_map[m.group()] > def cleanRepr4(text): > return controlsRe.sub(substChar, text) > > > ### timing ### > #helper func to generate random string > from time import time > import random > > def randomString(): > count = random.randrange(11,111) > chars = [chr(random.randrange(1, 255)) for n in range(count)] > return ''.join(chars) > > def timeAll(): > t0=t1=t2=t3=t4=0 > for n in range(9999): > s = randomString() > t = time() ; cleanRepr0(s) ; t0 += time() - t > t = time() ; cleanRepr1(s) ; t1 += time() - t > t = time() ; cleanRepr2(s) ; t2 += time() - t > t = time() ; cleanRepr3(s) ; t3 += time() - t > t = time() ; cleanRepr4(s) ; t4 += time() - t > print ( "original: %.3f\n" > "list: %.3f\n" > "map: %.3f\n" > "map & set: %.3f\n" > "regex: %.3f\n" > %(t0,t1,t2,t3,t4) ) > > timeAll() > =================================================== > ==> > original: 0.692 > list: 0.829 > map: 0.364 > map & set: 0.349 > regex: 0.341 > =================================================== > > Denis > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From denis.spir at free.fr Tue May 19 20:22:17 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 20:22:17 +0200 Subject: [Tutor] clean text In-Reply-To: References: <20090519113617.1ea6319c@o> <20090519191902.7c6977f9@o> Message-ID: <20090519202217.564b4d5d@o> Le Tue, 19 May 2009 10:49:15 -0700, Emile van Sebille s'exprima ainsi: > On 5/19/2009 10:19 AM spir said... > > Le Tue, 19 May 2009 11:36:17 +0200, > > spir s'exprima ainsi: > > > > [...] > > > > Thank you Albert, Kent, Sanders, Lie, Malcolm. > > > > This time regex wins! Thought it wouldn't because of the additional func > > call (too bad we cannot pass a mapping to re.sub). Actually the diff. is > > very small ;-) The relevant change is indeed using a dict. Replacing > > string concat with ''.join() is slower (tested with 10 times and 100 > > times bigger strings too). Strange... Membership test in a set is only > > very slightly faster than in dict keys. > > Hmm... this seems faster assuming it does the same thing... > > xlate = dict( (chr(c),chr(c)) for c in range(256)) > xlate.update(control_char_map) > > def cleanRepr5(text): > return "".join([ xlate[c] for c in text ]) > > > Emile Thank you, Emile. I thought at this solution (having a dict for all chars). But I cannot use it because later I will extend the app to cope with unicode (~ 100_000 chars). So that I really need to filter which chars have to be converted. A useful help I guess would be to have a builtin func that returns conventional char/string repr without "'...'" around. Denis PS By the way, you don't need (anymore) to build a list comprehension for an outer func that walks through a sequence: "".join( xlate[c] for c in text ) is a shortcut for "".join( (xlate[c] for c in text) ) [a generator expression already inside () needs no additional parens -- as long as there is no additional arg -- see PEP 289 http://www.python.org/dev/peps/pep-0289/] ------ la vita e estrany From alan.gauld at btinternet.com Tue May 19 20:27:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 May 2009 19:27:16 +0100 Subject: [Tutor] Replacement for __del__ References: <4A125BE2.2090903@tue.nl> <20090519115549.25b563f1@o> Message-ID: "David Stanek" wrote >> But I think (someone confirms/contradicts?) there is no harm > in overloading a class's __del__ to accomplish additional tasks > such as deleting temp disk space/dirs/files that need to exist > only during object creation. > > Incorrect. Why? > implementation. By the time it's executed many of the resources that > you want to clean up could have already been garbage collected. True, but the things Denis refers to - temp disk space etc - will never be garbage collected. The GC only tidies up the stuff in memory. If the object, as seems to be the case here, creates a file on disk which it uses as a semaphore/lock then GC will leave that file on the disk. A __del__ would enable it to be removed. > def __del__(self): > try: > self.f.close() > except: > pass This is, I agree, pointless. but very different to: def __ del__(self): try: os.remove(self.lockname) except: pass Of course the question of whether using disk storage to indicate object lifetime is a good design pattern is another issue altogether! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Tue May 19 20:31:18 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 19 May 2009 14:31:18 -0400 Subject: [Tutor] clean text In-Reply-To: <20090519191902.7c6977f9@o> References: <20090519113617.1ea6319c@o> <20090519191902.7c6977f9@o> Message-ID: <1c2a2c590905191131x421ece40r6461fc1969d5cd8b@mail.gmail.com> On Tue, May 19, 2009 at 1:19 PM, spir wrote: > Thank you Albert, Kent, Sanders, Lie, Malcolm. > > This time regex wins! Thought it wouldn't because of the additional func call (too bad we cannot pass a mapping to re.sub). Actually the diff. is very small ;-) The relevant ?change is indeed using a dict. The substChar() function is only called when a control character is found, so the relative time between the regex version and the next best will depend on the character mix. Your random strings seem a bit heavy on control chars. My guess is that the reason regex is a win is because it gets rid of the explicit Python-coded loop. > Replacing string concat with ''.join() is slower (tested with 10 times and 100 times bigger strings too). Strange... > Membership test in a set is only very slightly faster than in dict keys. String concatenation has been optimized for this use case in recent versions of Python. Kent From alan.gauld at btinternet.com Tue May 19 20:36:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 May 2009 19:36:39 +0100 Subject: [Tutor] clean text References: <20090519113617.1ea6319c@o> Message-ID: "spir" wrote > def _cleanRepr(text): > ''' text with control chars replaced by repr() equivalent ''' > result = "" > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > result += char > return result I haven't read the rest of the responses yet but my first suggestion is to invert the process and use replace() Instead of looping through every char in the string search for the illegal chars. A regex should enable you to do a single findall search. If you find any you can loop over the instances using replace() or sub() to change them. The translate module might even let you do all changes in one go. That should be faster I think. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Tue May 19 20:40:30 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 19 May 2009 11:40:30 -0700 Subject: [Tutor] clean text In-Reply-To: <20090519202217.564b4d5d@o> References: <20090519113617.1ea6319c@o> <20090519191902.7c6977f9@o> <20090519202217.564b4d5d@o> Message-ID: On 5/19/2009 11:22 AM spir said... > I thought at this solution (having a dict for all chars). But I cannot use it because later I will extend the app to cope with unicode (~ 100_000 chars). So that I really need to filter which chars have to be converted. That seems somewhat of a premature optimization. Dicts are very efficient -- I don't imagine 100k+ entries will slow it down, but then that's what should be tested so you'll know. > A useful help I guess would be to have a builtin func that returns conventional char/string repr without "'...'" around. Like this? >>> print repr(''.join(chr(ii) for ii in range(20,40))) '\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'' >>> Emile From alan.gauld at btinternet.com Tue May 19 20:41:43 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 May 2009 19:41:43 +0100 Subject: [Tutor] odbc connection with python References: <401fd82b0905190547m247b826bk772e74b0c1361611@mail.gmail.com> Message-ID: "mustafa akkoc" wrote > how can i make odbc connection language and i wanna make gui project > after > connecting database anyone has document ? There are lots of GUI options for python but if you want to do a database centred GUI and have no previous knowledge to leverage then dabo is probably your best bet. It includes a GUI builder and has strong links to databases. http://dabodev.com/ Caveat: I've only read the web pages, not used it! But it has had some good reviews on this list before. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue May 19 20:55:11 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 20:55:11 +0200 Subject: [Tutor] Replacement for __del__ In-Reply-To: References: <4A125BE2.2090903@tue.nl> <20090519115549.25b563f1@o> Message-ID: <20090519205511.50f0bfbb@o> Le Tue, 19 May 2009 19:27:16 +0100, "Alan Gauld" s'exprima ainsi: > > def __del__(self): > > try: > > self.f.close() > > except: > > pass > > This is, I agree, pointless. but very different to: > > def __ del__(self): > try: > os.remove(self.lockname) > except: pass > > > Of course the question of whether using disk storage to indicate > object lifetime is a good design pattern is another issue altogether! But there are many other (sensible) kinds of uses for temp storage on disk. Including some for the developper's own feedback. Denis ------ la vita e estrany From roger.varley at googlemail.com Tue May 19 22:09:33 2009 From: roger.varley at googlemail.com (Roger) Date: Tue, 19 May 2009 23:09:33 +0300 Subject: [Tutor] Allow only one instance of a process In-Reply-To: <1c2a2c590905171154k280ce079tcb0065a741bb10fe@mail.gmail.com> References: <4A0F75D7.3040603@gmail.com> <1c2a2c590905171154k280ce079tcb0065a741bb10fe@mail.gmail.com> Message-ID: <200905192309.34281.roger.varley@googlemail.com> On Sunday 17 May 2009 21:54:54 Kent Johnson wrote: > On Sat, May 16, 2009 at 10:26 PM, Sylvain Ste-Marie > > wrote: > > I'm currently writing a script to batch convert video for my psp > > > > It basically looks into a folder for video and launch ffmpeg: > > > > ffmpeg -i "videoname" -f psp -r 29.97 -b 768k -ar 24000 -ab 64k -s > > 368x208 "videoname.mp4" > > > > my idea is basically to check for pid but how do i do that? > > Look at psutil: > http://code.google.com/p/psutil/ > > But you will have a race condition if you do something like this: > > if ffmpeg is not running: > start ffmpeg > > A file lock is a better solution. Google 'python file lock' for recipes. > As a Java programmer just starting with Python, this answer surprised me. I would've been googling for the Python equivalent of the Singleton pattern. I guess it's going to take longer than I thought to get my head around the differences. With the lock file solution, if your program crashes, don't you have to undertake some form of manual/automated recovery to remove any left over lock files? Regards From denis.spir at free.fr Tue May 19 22:27:21 2009 From: denis.spir at free.fr (spir) Date: Tue, 19 May 2009 22:27:21 +0200 Subject: [Tutor] Allow only one instance of a process In-Reply-To: <200905192309.34281.roger.varley@googlemail.com> References: <4A0F75D7.3040603@gmail.com> <1c2a2c590905171154k280ce079tcb0065a741bb10fe@mail.gmail.com> <200905192309.34281.roger.varley@googlemail.com> Message-ID: <20090519222721.481d2b8b@o> Le Tue, 19 May 2009 23:09:33 +0300, Roger s'exprima ainsi: > As a Java programmer just starting with Python, this answer surprised me. I > would've been googling for the Python equivalent of the Singleton pattern. > I guess it's going to take longer than I thought to get my head around the > differences. http://steve.yegge.googlepages.com/singleton-considered-stupid Denis ------ la vita e estrany From kent37 at tds.net Tue May 19 23:24:44 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 19 May 2009 17:24:44 -0400 Subject: [Tutor] Allow only one instance of a process In-Reply-To: <200905192309.34281.roger.varley@googlemail.com> References: <4A0F75D7.3040603@gmail.com> <1c2a2c590905171154k280ce079tcb0065a741bb10fe@mail.gmail.com> <200905192309.34281.roger.varley@googlemail.com> Message-ID: <1c2a2c590905191424s149bf2e2y996209a382d872bc@mail.gmail.com> On Tue, May 19, 2009 at 4:09 PM, Roger wrote: > As a Java programmer just starting with Python, this answer surprised me. I > would've been googling for the Python equivalent of the Singleton pattern. > I guess it's going to take longer than I thought to get my head around the > differences. A Singleton is a way to allow only a single instance of a class within a single running program (process). The OP was asking how to ensure he only created one process running a program. That is a very different problem. And yeah, singletons are evil and usually better served by modules in Python. Kent From cheesman at titan.physx.u-szeged.hu Wed May 20 15:54:02 2009 From: cheesman at titan.physx.u-szeged.hu (Andy Cheesman) Date: Wed, 20 May 2009 15:54:02 +0200 (CEST) Subject: [Tutor] delphi, pascal and Python Message-ID: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> Hi people Is there a Method for wrapping delphi and/or pascal code into python like SWIG? I've googled to no avail, Can anyone help me? Andy From bermanrl at cfl.rr.com Wed May 20 16:25:21 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 20 May 2009 10:25:21 -0400 Subject: [Tutor] Iterating over list of functions Message-ID: <1242829521.13109.68.camel@bermanrl-desktop> Hi, Given a list of options: option_1.......option_n. For each option I have a corresponding function: func_1..... func_n. I have all function names defined in a list similar to flist = [func_1, func_2,.......func_n] which I know is a legitimate construct having found a similar construct discussed by Kent Johnson in 2005. What I do not know how to do is to call the selected function. If the index of options is 1, then I want to call func_2; do I code flist[index]? I do not think Python has a branch indirect construct so I cannot use anything similar to that methodology. What is the best approach to take to solve this problem? Thank you for any assistance, hints, solutions, and guidelines. Robert From cwitts at compuscan.co.za Wed May 20 16:41:07 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 20 May 2009 16:41:07 +0200 Subject: [Tutor] delphi, pascal and Python In-Reply-To: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> References: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> Message-ID: <4A141683.4030800@compuscan.co.za> Andy Cheesman wrote: > Hi people > > Is there a Method for wrapping delphi and/or pascal code into python like > SWIG? > I've googled to no avail, Can anyone help me? > > Andy > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Take a look at this topic http://forum.qgis.org/viewtopic.php?f=5&t=4341 It's from November 2008 so should still be current. -- Kind Regards, Christian Witts From lie.1296 at gmail.com Wed May 20 16:42:34 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 21 May 2009 00:42:34 +1000 Subject: [Tutor] Iterating over list of functions In-Reply-To: <1242829521.13109.68.camel@bermanrl-desktop> References: <1242829521.13109.68.camel@bermanrl-desktop> Message-ID: Robert Berman wrote: > Hi, > > Given a list of options: option_1.......option_n. For each option I have > a corresponding function: func_1..... func_n. I have all function names > defined in a list similar to flist = [func_1, func_2,.......func_n] > which I know is a legitimate construct having found a similar construct > discussed by Kent Johnson in 2005. > > What I do not know how to do is to call the selected function. If the > index of options is 1, then I want to call func_2; do I code > flist[index]? I do not think Python has a branch indirect construct so I > cannot use anything similar to that methodology. What is the best > approach to take to solve this problem? > Functions in python is first-class object. It can be passed around like regular objects, and can be put into lists like regular objects. opts = [opt1, opt2, opt3] funcs = [func1, func2, func3] for f, o in zip(funcs, opts): f(o) funcs[1](o[0]) # equivalent to func2(opt1) From cwitts at compuscan.co.za Wed May 20 16:44:34 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 20 May 2009 16:44:34 +0200 Subject: [Tutor] Iterating over list of functions In-Reply-To: <1242829521.13109.68.camel@bermanrl-desktop> References: <1242829521.13109.68.camel@bermanrl-desktop> Message-ID: <4A141752.9050200@compuscan.co.za> Robert Berman wrote: > Hi, > > Given a list of options: option_1.......option_n. For each option I have > a corresponding function: func_1..... func_n. I have all function names > defined in a list similar to flist = [func_1, func_2,.......func_n] > which I know is a legitimate construct having found a similar construct > discussed by Kent Johnson in 2005. > > What I do not know how to do is to call the selected function. If the > index of options is 1, then I want to call func_2; do I code > flist[index]? I do not think Python has a branch indirect construct so I > cannot use anything similar to that methodology. What is the best > approach to take to solve this problem? > > Thank you for any assistance, hints, solutions, and guidelines. > > Robert > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Why not use a dictionary to do the heavy lifting for you >>> import string >>> funcs = {1:string.upper, 2:string.lower} >>> funcs[1]('this is a simple test') 'THIS IS A SIMPLE TEST' >>> funcs[2]('THIS IS A SIMPLE TEST') 'this is a simple test' -- Kind Regards, Christian Witts From emile at fenx.com Wed May 20 16:48:38 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 20 May 2009 07:48:38 -0700 Subject: [Tutor] Iterating over list of functions In-Reply-To: <1242829521.13109.68.camel@bermanrl-desktop> References: <1242829521.13109.68.camel@bermanrl-desktop> Message-ID: On 5/20/2009 7:25 AM Robert Berman said... > Hi, > > Given a list of options: option_1.......option_n. For each option I have > a corresponding function: func_1..... func_n. I have all function names > defined in a list similar to flist = [func_1, func_2,.......func_n] > which I know is a legitimate construct having found a similar construct > discussed by Kent Johnson in 2005. > > What I do not know how to do is to call the selected function. If the > index of options is 1, then I want to call func_2; do I code > flist[index]? Yes - with trailing () of course... def func1():return 1 def func2():return 2 def func3():return 3 flist = [func1,func2,func3] flist[0]() flist[1]() flist[2]() Emile I do not think Python has a branch indirect construct so I > cannot use anything similar to that methodology. What is the best > approach to take to solve this problem? > > Thank you for any assistance, hints, solutions, and guidelines. > > Robert > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From bermanrl at cfl.rr.com Wed May 20 16:52:08 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 20 May 2009 10:52:08 -0400 Subject: [Tutor] Iterating over list of functions In-Reply-To: References: <1242829521.13109.68.camel@bermanrl-desktop> Message-ID: <1242831128.13109.72.camel@bermanrl-desktop> Thank you, Emile. That is the exact answer I needed. Robert On Wed, 2009-05-20 at 07:48 -0700, Emile van Sebille wrote: > On 5/20/2009 7:25 AM Robert Berman said... > > Hi, > > > > Given a list of options: option_1.......option_n. For each option I have > > a corresponding function: func_1..... func_n. I have all function names > > defined in a list similar to flist = [func_1, func_2,.......func_n] > > which I know is a legitimate construct having found a similar construct > > discussed by Kent Johnson in 2005. > > > > What I do not know how to do is to call the selected function. If the > > index of options is 1, then I want to call func_2; do I code > > flist[index]? > > Yes - with trailing () of course... > > def func1():return 1 > def func2():return 2 > def func3():return 3 > flist = [func1,func2,func3] > flist[0]() > flist[1]() > flist[2]() > > Emile > > > I do not think Python has a branch indirect construct so I > > cannot use anything similar to that methodology. What is the best > > approach to take to solve this problem? > > > > Thank you for any assistance, hints, solutions, and guidelines. > > > > Robert > > > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Wed May 20 16:54:05 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 20 May 2009 10:54:05 -0400 Subject: [Tutor] Iterating over list of functions In-Reply-To: <4A141752.9050200@compuscan.co.za> References: <1242829521.13109.68.camel@bermanrl-desktop> <4A141752.9050200@compuscan.co.za> Message-ID: <1242831245.13109.75.camel@bermanrl-desktop> Thank you, Christian. This solution was one I was not expecting and am glad to receive it. It is one I will explore in greater detail later. Robert On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote: > Robert Berman wrote: > > Hi, > > > > Given a list of options: option_1.......option_n. For each option I have > > a corresponding function: func_1..... func_n. I have all function names > > defined in a list similar to flist = [func_1, func_2,.......func_n] > > which I know is a legitimate construct having found a similar construct > > discussed by Kent Johnson in 2005. > > > > What I do not know how to do is to call the selected function. If the > > index of options is 1, then I want to call func_2; do I code > > flist[index]? I do not think Python has a branch indirect construct so I > > cannot use anything similar to that methodology. What is the best > > approach to take to solve this problem? > > > > Thank you for any assistance, hints, solutions, and guidelines. > > > > Robert > > > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > Why not use a dictionary to do the heavy lifting for you > > >>> import string > >>> funcs = {1:string.upper, 2:string.lower} > >>> funcs[1]('this is a simple test') > 'THIS IS A SIMPLE TEST' > >>> funcs[2]('THIS IS A SIMPLE TEST') > 'this is a simple test' > From alan.gauld at btinternet.com Wed May 20 17:07:37 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 May 2009 16:07:37 +0100 Subject: [Tutor] Iterating over list of functions References: <1242829521.13109.68.camel@bermanrl-desktop><4A141752.9050200@compuscan.co.za> <1242831245.13109.75.camel@bermanrl-desktop> Message-ID: "Robert Berman" wrote > Thank you, Christian. This solution was one I was not expecting and am > glad to receive it. It is one I will explore in greater detail later. A dictionary of functions is the most common way to tackle this fairly common requirement. It combines readability with ease of maintenance, there is no chance of using the wrong index. >> Why not use a dictionary to do the heavy lifting for you >> >> >>> import string >> >>> funcs = {1:string.upper, 2:string.lower} >> >>> funcs[1]('this is a simple test') >> 'THIS IS A SIMPLE TEST' >> >>> funcs[2]('THIS IS A SIMPLE TEST') >> 'this is a simple test' -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bermanrl at cfl.rr.com Wed May 20 18:22:58 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 20 May 2009 12:22:58 -0400 Subject: [Tutor] Iterating over list of functions In-Reply-To: References: <1242829521.13109.68.camel@bermanrl-desktop> <4A141752.9050200@compuscan.co.za> <1242831245.13109.75.camel@bermanrl-desktop> Message-ID: <1242836578.13109.214.camel@bermanrl-desktop> Alan, The emphasis of your reply certainly makes me look at the dictionary solution as the most 'correct' solution to utilize. Before I change the code I just implemented, let me make sure I understand exactly what you are saying and what you are advocating. The 'dictionary of functions' is the 'best' approach because of simplicity and because it minimizes chances or errors. The one area you did not address is the area of efficiency. Is this method also the most efficient solution? The reason I am trying to pin you are two fold. First, you are a powerful spokesman for 'correct and proper' coding in the Python community and I take your opinions and commentaries seriously. Again, how efficient is this method and is it truly the 'most pythonesque' oriented code. Thanks again, Robert On Wed, 2009-05-20 at 16:07 +0100, Alan Gauld wrote: > "Robert Berman" wrote > > > Thank you, Christian. This solution was one I was not expecting and am > > glad to receive it. It is one I will explore in greater detail later. > > A dictionary of functions is the most common way to tackle > this fairly common requirement. It combines readability with > ease of maintenance, there is no chance of using the wrong > index. > > >> Why not use a dictionary to do the heavy lifting for you > >> > >> >>> import string > >> >>> funcs = {1:string.upper, 2:string.lower} > >> >>> funcs[1]('this is a simple test') > >> 'THIS IS A SIMPLE TEST' > >> >>> funcs[2]('THIS IS A SIMPLE TEST') > >> 'this is a simple test' > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Wed May 20 18:48:17 2009 From: denis.spir at free.fr (spir) Date: Wed, 20 May 2009 18:48:17 +0200 Subject: [Tutor] Iterating over list of functions In-Reply-To: <1242829521.13109.68.camel@bermanrl-desktop> References: <1242829521.13109.68.camel@bermanrl-desktop> Message-ID: <20090520184817.7fad51db@o> Le Wed, 20 May 2009 10:25:21 -0400, Robert Berman s'exprima ainsi: > What I do not know how to do is to call the selected function. If you have options and functions "hard-coded" in lists (or if you get them from outside), you can still let python build a dict for you, using "zip": l1 = [1,2,3] l2 = [9,8,7] ll = zip(l1,l2); print ll d = dict(ll); print d ==> [(1, 9), (2, 8), (3, 7)] {1: 9, 2: 8, 3: 7} Denis ------ la vita e estrany From alan.gauld at btinternet.com Wed May 20 19:36:17 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 20 May 2009 17:36:17 +0000 (GMT) Subject: [Tutor] Iterating over list of functions In-Reply-To: <1242836578.13109.214.camel@bermanrl-desktop> References: <1242829521.13109.68.camel@bermanrl-desktop> <4A141752.9050200@compuscan.co.za> <1242831245.13109.75.camel@bermanrl-desktop> <1242836578.13109.214.camel@bermanrl-desktop> Message-ID: <823540.19602.qm@web86712.mail.ird.yahoo.com> > understand exactly what you are saying and what you are advocating. > The 'dictionary of functions' is the 'best' approach because of simplicity > and because it minimizes chances or errors. Correct. Maintaining synch of indexes between two arrays of data items is always going to be a risky business. > The one area you did not address is the area of efficiency. > Is this method also the most efficient solution? It is an efficient solution - I haven't benchmarked it. But bear in mind that Python uses dictionaries internally to store variables and other object references - in cluding functions. Similarly classes in Python are implemented as a specialised kind of dictionary. Thus when you call a function in Python or access a class feature you are going via a dictionary (representing the namespace of the module). Most folks seem to find function dispatch fast enough! Another aspect to consider. If you have a lot of options you are probably going to use a search of your options list to find the index before applying that index to your list of functions. (If you hard code the index then you might as well hard code the function call!) The dictionary avoids the need to lookup the index. > you are a powerful spokesman for 'correct and proper' coding in the > Python community and I take your opinions and commentaries seriously. Blush... I have opinions that's true. But as for "correct and proper"? - I usually defer to Kent for that :-) Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 20 20:10:14 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 May 2009 19:10:14 +0100 Subject: [Tutor] delphi, pascal and Python References: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> Message-ID: "Andy Cheesman" wrote > Is there a Method for wrapping delphi and/or pascal code into python like > SWIG? > I've googled to no avail, Can anyone help me? Try this: http://membres.lycos.fr/marat/delphi/python.htm Alan G From bermanrl at cfl.rr.com Wed May 20 16:02:22 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 20 May 2009 10:02:22 -0400 Subject: [Tutor] Iterating through a function list Message-ID: <1242828142.13109.65.camel@bermanrl-desktop> Hi, Given a list of options: option_1.......option_n. For each option I have a corresponding function: func_1..... func_n. I have all function names defined in a list similar to flist = [func_1, func_2,.......func_n] which I know is a legitimate construct having found a similar construct discussed by Kent Johnson in 2005. What I do not know how to do is to call the selected function. If the index of options is 1, then I want to call func_2; do I code flist[index]? I do not think Python has a branch indirect construct so I cannot use anything similar to that methodology. What is the best approach to take to solve this problem? Thank you for any assistance, hints, solutions, and guidelines. Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Wed May 20 22:15:26 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 21 May 2009 01:45:26 +0530 Subject: [Tutor] Getting file properties on winodws Message-ID: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> Hi, I am trying to read properties of file on windows like there is a property call Keywords on file; I am to read this property independent of file type. I tried using win32api and win32file but I was not able to find any such function; GetFileAttributes gives some limited attributes. -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Wed May 20 23:04:22 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 20 May 2009 22:04:22 +0100 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> Message-ID: <4A147056.4030007@timgolden.me.uk> vishwajeet singh wrote: > Hi, > > I am trying to read properties of file on windows like there is a property > call Keywords on file; I am to read this property independent of file type. There's an unpolished (indeed, unfinished) example here: http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html TJG From dextrous85 at gmail.com Wed May 20 23:10:31 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 21 May 2009 02:40:31 +0530 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <4A147056.4030007@timgolden.me.uk> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> Message-ID: <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> Thanks that helped. On Thu, May 21, 2009 at 2:34 AM, Tim Golden wrote: > vishwajeet singh wrote: > >> Hi, >> >> I am trying to read properties of file on windows like there is a property >> call Keywords on file; I am to read this property independent of file >> type. >> > > There's an unpolished (indeed, unfinished) example here: > > > http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricaraoz at gmail.com Wed May 20 17:32:12 2009 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Wed, 20 May 2009 12:32:12 -0300 Subject: [Tutor] web cam Message-ID: <4A14227C.5060300@gmail.com> Hi, a friend of mine has asked me how difficult would it be to send web cam images through python program as a hobby project. Honestly, at this point I have no idea where does he want to put python in the equation nor what does the project entail. So I'm asking for pointers to : a) Any already done projects in python b) keywords to google for c) what parts do you think I'll need to put together (web service, client, browser) and which modules do you recommend. I know this is a half baked question, just some pointers on where to start from would be enough. Thanks From vinces1979 at gmail.com Thu May 21 00:30:25 2009 From: vinces1979 at gmail.com (vince spicer) Date: Wed, 20 May 2009 16:30:25 -0600 Subject: [Tutor] web cam In-Reply-To: <4A14227C.5060300@gmail.com> References: <4A14227C.5060300@gmail.com> Message-ID: <1e53c510905201530n483b703dj7e783df26df5a342@mail.gmail.com> I've seen this win32 library: http://videocapture.sourceforge.net via http://technobabbler.com?p=22 for linux http://www.antonym.org/libfg Vince 2009/5/20 Ricardo Ar?oz > Hi, a friend of mine has asked me how difficult would it be to send web > cam images through python program as a hobby project. > Honestly, at this point I have no idea where does he want to put python > in the equation nor what does the project entail. So I'm asking for > pointers to : > a) Any already done projects in python > b) keywords to google for > c) what parts do you think I'll need to put together (web service, > client, browser) and which modules do you recommend. > > I know this is a half baked question, just some pointers on where to > start from would be enough. > > Thanks > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dkuhlman at rexx.com Wed May 20 23:39:40 2009 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Wed, 20 May 2009 14:39:40 -0700 Subject: [Tutor] Iterating through a function list In-Reply-To: <1242828142.13109.65.camel@bermanrl-desktop> References: <1242828142.13109.65.camel@bermanrl-desktop> Message-ID: <20090520213940.GA96402@cutter.rexx.com> On Wed, May 20, 2009 at 10:02:22AM -0400, Robert Berman wrote: > > Hi, > Given a list of options: option_1.......option_n. For each option I > have a corresponding function: func_1..... func_n. I have all function > names defined in a list similar to flist = [func_1, > func_2,.......func_n] which I know is a legitimate construct having > found a similar construct discussed by Kent Johnson in 2005. > What I do not know how to do is to call the selected function. If the > index of options is 1, then I want to call func_2; do I code > flist[index]? Yes. Then to call that function, do: flist[index](arg1, ...) or do: func = flist[index] func(arg1, ...) Similarly, if you need to look up a function by name or some other key then use a dictionary. For example: funcs = {'func_name_one': func1, ...} if name in funcs: funcs[name](arg, ...) Python is making this too easy for you, making it hard to spot the solution. Think of parentheses as a "function call operator", which you can apply to any callable value. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From kent37 at tds.net Thu May 21 02:37:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 20 May 2009 20:37:43 -0400 Subject: [Tutor] web cam In-Reply-To: <4A14227C.5060300@gmail.com> References: <4A14227C.5060300@gmail.com> Message-ID: <1c2a2c590905201737xa3835f0y7fb46541a020295f@mail.gmail.com> 2009/5/20 Ricardo Ar?oz : > Hi, a friend of mine has asked me how difficult would it be to send web > cam images through python program as a hobby project. > Honestly, at this point I have no idea where does he want to put python > in the equation nor what does the project entail. So I'm asking for > pointers to : > a) Any already done projects in python > b) keywords to google for > c) what parts do you think I'll need to put together (web service, > client, browser) and which modules do you recommend. > > I know this is a half baked question, just some pointers on where to > start from would be enough. Google 'python web cam' for some ideas. Kent From rnrcreid at yahoo.com Thu May 21 03:25:07 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Wed, 20 May 2009 18:25:07 -0700 (PDT) Subject: [Tutor] Hi everyone Message-ID: <938016.83889.qm@web45712.mail.sp1.yahoo.com> Hi, ? I'm teaching myself Python mainly for to use as a hobby.? I'd like to do graphical programs eventually and maybe some simple graphic games.? I feel I'm doing well with the tutorial I'm using but it would be nice to have some real people to ask questions and opinions, so on that note, I'm having a little trouble understanding the following code.? I grasp the basics of what it does but am wondering if someone could explain it in simpler terms.. ? In the tutorial, I'm using Tuples and there is a Word Jumble game given to show how this can be used.? A tuple of words is created: ? WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") correct=word ? Then a variable that will hold the jumbled word is defined: jumble=' ' Ok, then a variable: word=random.choice(WORDS) is created to pick a random element from WORDS.? I get this part. ? Now here is the code I'm having trouble following: ? while word: ??? position = random.randrange(len(word)) ??? jumble += word[position] ??? word = word[:position] + word[(position + 1):] ? position = random.randrange(len(word)). This will create a starting point for the program to pick a letter out of a word at random using the length of the word as a range I think. Like if the word is 'python' and since it has 6 letters, random.randrange(len(word)) will pick a random starting point such as the letter 'y' for example. It's gonna be inside a while loop, so the next time it runs, 'python' will be 'pthon' and the random range will be 5 letters and on down till there is no more letters, is this right? ? Ok, so everyone of the letters that was 'extracted' from the word 'python' will be put into the variable 'jumble' that was defined as empty. ? This is the part that troubles me: ? word = word[:position] + word[(position + 1):] ? I know basically it is creating a new string through the while loop for extracint letters.? But I don't feel I understand it fully,? The expression is confusing to me.? Here is an excerpt from the tutorial that explains it and confuses me more: ? "The next line in the loop, word = word[:position] + word[(position + 1):] creates a new version of word minus the one letter at position position. Using slicing, the computer creates two new strings from word. The first slice, word[:position], is every letter up to, but not including, word[position]. The next slice, word[(position + 1):], is every letter after word[position]. These two string are joined together and assigned to word, which is now equal to its old self, minus the one letter word[position]." ? Can someone explain this in simpler terms? I'm sorry this is so lengthy for my first post:) ? Any help will be appreciated ? Thanks, ? Doug ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Thu May 21 04:16:26 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 20 May 2009 19:16:26 -0700 Subject: [Tutor] delphi, pascal and Python In-Reply-To: References: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> Message-ID: <40af687b0905201916p3d09e7c6je672f7c7fb07ee3@mail.gmail.com> On Wed, May 20, 2009 at 11:10 AM, Alan Gauld wrote: > > "Andy Cheesman" wrote > > Is there a Method for wrapping delphi and/or pascal code into python like >> SWIG? >> I've googled to no avail, Can anyone help me? >> > > Try this: > > http://membres.lycos.fr/marat/delphi/python.htm > > Alan G > That's a package to let you embed Python in Delphi; the OP wants to go the other direction. -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at abbottdavid.com Thu May 21 04:25:32 2009 From: david at abbottdavid.com (David) Date: Wed, 20 May 2009 22:25:32 -0400 Subject: [Tutor] web cam In-Reply-To: <4A14227C.5060300@gmail.com> References: <4A14227C.5060300@gmail.com> Message-ID: <4A14BB9C.1000202@abbottdavid.com> Ricardo Ar?oz wrote: > Hi, a friend of mine has asked me how difficult would it be to send web > cam images through python program as a hobby project. > Honestly, at this point I have no idea where does he want to put python > in the equation nor what does the project entail. So I'm asking for > pointers to : > a) Any already done projects in python > b) keywords to google for > c) what parts do you think I'll need to put together (web service, > client, browser) and which modules do you recommend. > > I know this is a half baked question, just some pointers on where to > start from would be enough. > > Thanks > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > You can upload still shots from the web cam with pycurl. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From davea at ieee.org Thu May 21 04:37:31 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 20 May 2009 22:37:31 -0400 Subject: [Tutor] Iterating over list of functions In-Reply-To: References: Message-ID: <4A14BE6B.2020409@ieee.org> Robert Berman wrote: > Thank you, Christian. This solution was one I was not expecting and am > glad to receive it. It is one I will explore in greater detail later. > > Robert > > > On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote: > >> Robert Berman wrote: >> >>> Hi, >>> >>> Given a list of options: option_1.......option_n. For each option I have >>> a corresponding function: func_1..... func_n. I have all function names >>> defined in a list similar to flist = [func_1, func_2,.......func_n] >>> which I know is a legitimate construct having found a similar construct >>> discussed by Kent Johnson in 2005. >>> >>> What I do not know how to do is to call the selected function. If the >>> index of options is 1, then I want to call func_2; do I code >>> flist[index]? I do not think Python has a branch indirect construct so I >>> cannot use anything similar to that methodology. What is the best >>> approach to take to solve this problem? >>> >>> Thank you for any assistance, hints, solutions, and guidelines. >>> >>> Robert >>> >>> >>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >> Why not use a dictionary to do the heavy lifting for you >> >> >>> import string >> >>> funcs = {1:string.upper, 2:string.lower} >> >>> funcs[1]('this is a simple test') >> 'THIS IS A SIMPLE TEST' >> >>> funcs[2]('THIS IS A SIMPLE TEST') >> 'this is a simple test' >> >> (Your top-posting makes the thread hard to follow) Note that once it's a dictionary, you can use whatever keys you would normally use in a dictionary. For example, if the options are strings, use a string as the key. If they literally are numbers, then a list is preferable, but a dictionary gives you other choices. DaveA From lie.1296 at gmail.com Thu May 21 08:16:32 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 21 May 2009 16:16:32 +1000 Subject: [Tutor] Hi everyone In-Reply-To: <938016.83889.qm@web45712.mail.sp1.yahoo.com> References: <938016.83889.qm@web45712.mail.sp1.yahoo.com> Message-ID: Doug Reid wrote: > Now here is the code I'm having trouble following: > > while word: > position = random.randrange(len(word)) > jumble += word[position] > word = word[:position] + word[(position + 1):] > > position = random.randrange(len(word)). This will create a starting > point for the program to pick a letter out of a word at random using the > length of the word as a range I think. Like if the word is 'python' and > since it has 6 letters, random.randrange(len(word)) will pick a random > starting point such as the letter 'y' for example. It's gonna be inside > a while loop, so the next time it runs, 'python' will be 'pthon' and the > random range will be 5 letters and on down till there is no more > letters, is this right? `position` is an integer. It is the index of the letter we want to put into `jumble`. The first line generates a random index, the second line copies the letter and append it to `jumble`, and the third line removes that letter from `word`. > Ok, so everyone of the letters that was 'extracted' from the word > 'python' will be put into the variable 'jumble' that was defined as empty. > > This is the part that troubles me: > > word = word[:position] + word[(position + 1):] How do we remove a single letter? In python, string is immutable; we must create a new string that has the required property (i.e. letter in `position` removed). Rather than removing, we copied the part of string before `position` (word[:position]) and after `position` (word[position+1:]) and concatenating them together to become our new `word`. How does the slicing works? The standard model for "slicing and indexing" in python is like this: -6 -5 -4 -3 -2 -1 +---+---+---+---+---+---+ | p | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 Let's say `position` is 3 (i.e. the letter "h"). word[:3] is: -6 -5 -4 -3 -2 -1 +---+---+---+---+---+---+ | p | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 |___________| word[0:3] which is "pyt" while word[3+1:] -> word[4:] is: -6 -5 -4 -3 -2 -1 +---+---+---+---+---+---+ | p | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 |_______| word[4:6] which is "on" concatenating "pyt" + "on", we get "pyton" (i.e. "h" removed) An extended version of the line that may be more easily digestible: letters_before_position = word[:position] letters_after_position = word[position+1:] word = letters_before_position + letters_after_position From alan.gauld at btinternet.com Thu May 21 09:01:41 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 21 May 2009 08:01:41 +0100 Subject: [Tutor] delphi, pascal and Python References: <34895.160.114.38.31.1242827642.squirrel@titan.physx.u-szeged.hu> <40af687b0905201916p3d09e7c6je672f7c7fb07ee3@mail.gmail.com> Message-ID: "Marc Tompkins" wrote >> Is there a Method for wrapping delphi and/or pascal code into python >> like >>> SWIG? >> http://membres.lycos.fr/marat/delphi/python.htm > > That's a package to let you embed Python in Delphi; the OP wants to go > the > other direction. So it is, I didn't read the OP question closely enough. On Windows you could create a DLL and use ctypes to access it, but thats not the same as creating an importable module as could be done with SWIG Alan G. From dextrous85 at gmail.com Thu May 21 09:38:03 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 21 May 2009 13:08:03 +0530 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> Message-ID: <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> Hi Tim, It does not seems to be working for jpg and in general with image files any pointers on that. Thanks for your help. On Thu, May 21, 2009 at 2:40 AM, vishwajeet singh wrote: > Thanks that helped. > > > On Thu, May 21, 2009 at 2:34 AM, Tim Golden wrote: > >> vishwajeet singh wrote: >> >>> Hi, >>> >>> I am trying to read properties of file on windows like there is a >>> property >>> call Keywords on file; I am to read this property independent of file >>> type. >>> >> >> There's an unpolished (indeed, unfinished) example here: >> >> >> http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html >> >> TJG >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Cheers, > Vishwajeet > http://www.singhvishwajeet.com > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Thu May 21 09:42:31 2009 From: denis.spir at free.fr (spir) Date: Thu, 21 May 2009 09:42:31 +0200 Subject: [Tutor] Hi everyone In-Reply-To: <938016.83889.qm@web45712.mail.sp1.yahoo.com> References: <938016.83889.qm@web45712.mail.sp1.yahoo.com> Message-ID: <20090521094231.6b5e672f@o> Le Wed, 20 May 2009 18:25:07 -0700 (PDT), Doug Reid s'exprima ainsi: > "The next line in the loop, > word = word[:position] + word[(position + 1):] > > creates a new version of word minus the one letter at position position. > Using slicing, the computer creates two new strings from word. The first > slice, word[:position], is every letter up to, but not including, > word[position]. The next slice, word[(position + 1):], is every letter > after word[position]. These two string are joined together and assigned to > word, which is now equal to its old self, minus the one letter > word[position]." >Can someone explain this in simpler terms? I'm sorry this > is so lengthy for my first post:) It's confusing because abstract and without any example. In the case you wrote where the word is "python", the letter 'y', and so the position is 1, you get: word "python" word[:position] "p" word[position+1:] "thon" glued together "pthon" Also, a confusing part of the program id the loop header: while word: ??? position = random.randrange(len(word)) ??? jumble += word[position] ??? word = word[:position] + word[(position + 1):] In python, a container such as a string is considered 'False' when it's empty. Right? So that the loop header above is equivalent to: while len(word) > 0: and the loop will stop when every letter has been extracted from the word. Denis ------ la vita e estrany From mail at timgolden.me.uk Thu May 21 10:13:36 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 21 May 2009 09:13:36 +0100 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> Message-ID: <4A150D30.4050609@timgolden.me.uk> [Rearranging for reading order] [vishwajeet singh wrote] I am trying to read properties of file on windows like there is a property call Keywords on file; I am to read this property independent of file type. [Tim Golden wrote:] There's an unpolished (indeed, unfinished) example here: http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html [vishwajeet singh wrote:] > It does not seems to be working for jpg and in general with image files any > pointers on that. It would probably help if you specified what "does not seem[s] to be working" meant. But I assume that you mean: if you add a Title into the Summary on a JPEG then it doesn't get picked up by the script. What's happening here (I think) is that for media types -- images, movies, etc. -- the [Summary] property sheet is overridden, probably by Windows Media Player which will use the properties embedded in the JPEG (EXIF) or WMV. This isn't Structured Storage as such and so isn't extracted by the script I showed. If I get a chance, I'll look into this a bit further. TJG From dextrous85 at gmail.com Thu May 21 10:25:06 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 21 May 2009 13:55:06 +0530 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <4A150D30.4050609@timgolden.me.uk> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> <4A150D30.4050609@timgolden.me.uk> Message-ID: <5487b3060905210125m8dffa1dh79d60eb01477fff2@mail.gmail.com> Hi Tim, My apologies for being vague. Yes you are right I am not able to get any summary property from a image file. Script runs successfully without printing anything. Thanks for your help. On Thu, May 21, 2009 at 1:43 PM, Tim Golden wrote: > [Rearranging for reading order] > > [vishwajeet singh wrote] > I am trying to read properties of file on windows like there is a > property > call Keywords on file; I am to read this property independent of file > type. > > [Tim Golden wrote:] > There's an unpolished (indeed, unfinished) example here: > http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html > > [vishwajeet singh wrote:] > >> It does not seems to be working for jpg and in general with image files >> any >> pointers on that. >> > > It would probably help if you specified what "does not seem[s] to be > working" meant. But I assume that you mean: if you add a Title into > the Summary on a JPEG then it doesn't get picked up by the script. > What's happening here (I think) is that for media types -- images, > movies, etc. -- the [Summary] property sheet is overridden, probably > by Windows Media Player which will use the properties embedded in > the JPEG (EXIF) or WMV. This isn't Structured Storage as such and > so isn't extracted by the script I showed. > > If I get a chance, I'll look into this a bit further. > > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu May 21 11:55:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 21 May 2009 05:55:13 -0400 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <5487b3060905210125m8dffa1dh79d60eb01477fff2@mail.gmail.com> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> <4A150D30.4050609@timgolden.me.uk> <5487b3060905210125m8dffa1dh79d60eb01477fff2@mail.gmail.com> Message-ID: <1c2a2c590905210255wd35e582v9d44c2034e8f5e57@mail.gmail.com> On Thu, May 21, 2009 at 4:25 AM, vishwajeet singh wrote: > Hi Tim, > > My apologies for being vague. Yes you are right I am not able to get any > summary property from a image file. > Script runs successfully without printing anything. If you are specifically looking to read EXIF tags from jpg files then try exif.py: http://exif-py.sourceforge.net/ Kent From srilyk at gmail.com Thu May 21 13:23:31 2009 From: srilyk at gmail.com (W W) Date: Thu, 21 May 2009 06:23:31 -0500 Subject: [Tutor] Hi everyone In-Reply-To: <20090521094231.6b5e672f@o> References: <938016.83889.qm@web45712.mail.sp1.yahoo.com> <20090521094231.6b5e672f@o> Message-ID: <333efb450905210423n2e532bb6s8dd3fdb26890ab22@mail.gmail.com> On Thu, May 21, 2009 at 2:42 AM, spir wrote: > > while word: > position = random.randrange(len(word)) > jumble += word[position] > word = word[:position] + word[(position + 1):] > Something that many of us use for debugging, and is also useful for comprehension is a simple print statement. If you were to convert the loop to this: while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):] print jumble print word # Optional - for further understanding print word[:position] print word[(position+1):] you would basically see what Denis wrote - only every step through the loop. HTH, Wayne p.s. - When you start graphical programming, I'd look at pyglet or pygame. Although to really understand event driven programming, it really helped me to start writing programs with Tkinter. YMMV -------------- next part -------------- An HTML attachment was scrubbed... URL: From rnrcreid at yahoo.com Thu May 21 13:54:17 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Thu, 21 May 2009 04:54:17 -0700 (PDT) Subject: [Tutor] Hi everyone....thanks for the help Message-ID: <790417.44676.qm@web45709.mail.sp1.yahoo.com> Thank you all for the help.? I believe I understand now, and think this will be a great group to learn from. ? Doug -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Thu May 21 18:17:24 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 21 May 2009 17:17:24 +0100 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <4A150D30.4050609@timgolden.me.uk> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> <4A150D30.4050609@timgolden.me.uk> Message-ID: <4A157E94.8060305@timgolden.me.uk> Tim Golden wrote: > It would probably help if you specified what "does not seem[s] to be > working" meant. But I assume that you mean: if you add a Title into > the Summary on a JPEG then it doesn't get picked up by the script. > What's happening here (I think) is that for media types -- images, > movies, etc. -- the [Summary] property sheet is overridden, probably > by Windows Media Player which will use the properties embedded in > the JPEG (EXIF) or WMV. This isn't Structured Storage as such and > so isn't extracted by the script I showed. > > If I get a chance, I'll look into this a bit further. In principle, it ought to be possible to do this by querying the IID_IPropertySetStorage on the corresponding IID_IShellItem. But it doesn't look as though the pywin32 com stuff quite supports that yet. Could be wrong. Might be worth trying with comtypes; I'll have a look if I can. TJG From mail at timgolden.me.uk Thu May 21 21:53:20 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 21 May 2009 20:53:20 +0100 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <4A157E94.8060305@timgolden.me.uk> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> <4A150D30.4050609@timgolden.me.uk> <4A157E94.8060305@timgolden.me.uk> Message-ID: <4A15B130.8000406@timgolden.me.uk> Tim Golden wrote: > Tim Golden wrote: >> It would probably help if you specified what "does not seem[s] to be >> working" meant. But I assume that you mean: if you add a Title into >> the Summary on a JPEG then it doesn't get picked up by the script. >> What's happening here (I think) is that for media types -- images, >> movies, etc. -- the [Summary] property sheet is overridden, probably >> by Windows Media Player which will use the properties embedded in >> the JPEG (EXIF) or WMV. This isn't Structured Storage as such and >> so isn't extracted by the script I showed. >> >> If I get a chance, I'll look into this a bit further. > > In principle, it ought to be possible to do this by > querying the IID_IPropertySetStorage on the corresponding > IID_IShellItem. But it doesn't look as though the pywin32 > com stuff quite supports that yet. Could be wrong. Might > be worth trying with comtypes; I'll have a look if I can. OK, more useful version now up; reads JPEGs, WMVs, etc. Still more work to be done, reverse-engineering the FMTID and property names, but at least it takes you further without too much extra work. http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html TJG From dextrous85 at gmail.com Thu May 21 22:00:47 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Fri, 22 May 2009 01:30:47 +0530 Subject: [Tutor] Getting file properties on winodws In-Reply-To: <4A15B130.8000406@timgolden.me.uk> References: <5487b3060905201315k5ebce051w853cb27a8305835c@mail.gmail.com> <4A147056.4030007@timgolden.me.uk> <5487b3060905201410t790e652ud8066c4908336040@mail.gmail.com> <5487b3060905210038x3aed5e1bsff811c7afcd3f261@mail.gmail.com> <4A150D30.4050609@timgolden.me.uk> <4A157E94.8060305@timgolden.me.uk> <4A15B130.8000406@timgolden.me.uk> Message-ID: <5487b3060905211300v529b6d2av6c7607a3a8c387b0@mail.gmail.com> On Fri, May 22, 2009 at 1:23 AM, Tim Golden wrote: > Tim Golden wrote: > >> Tim Golden wrote: >> >>> It would probably help if you specified what "does not seem[s] to be >>> working" meant. But I assume that you mean: if you add a Title into >>> the Summary on a JPEG then it doesn't get picked up by the script. >>> What's happening here (I think) is that for media types -- images, >>> movies, etc. -- the [Summary] property sheet is overridden, probably >>> by Windows Media Player which will use the properties embedded in >>> the JPEG (EXIF) or WMV. This isn't Structured Storage as such and >>> so isn't extracted by the script I showed. >>> >>> If I get a chance, I'll look into this a bit further. >>> >> >> In principle, it ought to be possible to do this by >> querying the IID_IPropertySetStorage on the corresponding >> IID_IShellItem. But it doesn't look as though the pywin32 >> com stuff quite supports that yet. Could be wrong. Might >> be worth trying with comtypes; I'll have a look if I can. >> > > OK, more useful version now up; reads JPEGs, WMVs, etc. > Still more work to be done, reverse-engineering the FMTID > and property names, but at least it takes you further without > too much extra work. > > > http://timgolden.me.uk/python/win32_how_do_i/get-document-summary-info.html > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Hi Tim, Thanks for your help; I am able to get required properties from jpg files. -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From eduardo.susan at gmail.com Thu May 21 23:26:24 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Thu, 21 May 2009 15:26:24 -0600 Subject: [Tutor] Parsing Bible verses Message-ID: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> Hello, I'm planning to create a script to read a certain file, find the line that contains Bible references and then use that to query a bible database in order to print the verses in another file. I will be looking for lines like these: Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23 So, references in different chapters are separated by a semicolon. My main challenge would be make the program guess that 10:12 refers to the previous book. 15-20 means verses 15 thru 20 inclusive. I'm afraid that will take more than Regex and I never studied anything about parser tools, really. Any suggestion how I should approach this? Eduardo www.expresssignproducts.com From srilyk at gmail.com Fri May 22 00:47:46 2009 From: srilyk at gmail.com (W W) Date: Thu, 21 May 2009 17:47:46 -0500 Subject: [Tutor] Parsing Bible verses In-Reply-To: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> References: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> Message-ID: <333efb450905211547g4b9c85dfy95517bdf337a440c@mail.gmail.com> On Thu, May 21, 2009 at 4:26 PM, Eduardo Vieira wrote: > Hello, I'm planning to create a script to read a certain file, find > the line that contains Bible references and then use that to query a > bible database in order to print the verses in another file. > I will be looking for lines like these: > Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23 > > So, references in different chapters are separated by a semicolon. My > main challenge would be make the program guess that 10:12 refers to > the previous book. 15-20 means verses 15 thru 20 inclusive. I'm afraid > that will take more than Regex and I never studied anything about > parser tools, really. > > Any suggestion how I should approach this? > Actually, a regex probably wouldn't be too far off. If you're comfortable working with them it may even be a good thing... Will the line always begin with "Lesson text:"? If so, that makes it a lot easier. Something like: for line in file: if line starts with "Lesson text": skip "lesson text" book = first word(s) verse[book] = list of references that's a really basic flow of logic, but it's probably how I'd do it. HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From john at fouhy.net Fri May 22 03:03:28 2009 From: john at fouhy.net (John Fouhy) Date: Fri, 22 May 2009 13:03:28 +1200 Subject: [Tutor] Parsing Bible verses In-Reply-To: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> References: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> Message-ID: <5e58f2e40905211803u206ebedey9c4d4e4e51903d2@mail.gmail.com> 2009/5/22 Eduardo Vieira : > I will be looking for lines like these: > Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23 > > So, references in different chapters are separated by a semicolon. My > main challenge would be make the program guess that 10:12 refers to > the previous book. 15-20 means verses 15 thru 20 inclusive. I'm afraid > that will take more than Regex and I never studied anything about > parser tools, really. Well, pyparsing is one of the standard python parsing modules. It's not that bad, really :-) Here's some code I knocked out: from pyparsing import * SingleVerse = Word(nums) VerseRange = SingleVerse + '-' + SingleVerse Verse = VerseRange | SingleVerse Verse = Verse.setResultsName('Verse').setName('Verse') Verses = Verse + ZeroOrMore(Suppress(',') + Verse) Verses = Verses.setResultsName('Verses').setName('Verses') ChapterNum = Word(nums) ChapterNum = ChapterNum.setResultsName('Chapter').setName('Chapter') ChapVerses = ChapterNum + ':' + Verses SingleChapter = Group(ChapVerses | ChapterNum) Chapters = SingleChapter + ZeroOrMore(Suppress(';') + SingleChapter) Chapters = Chapters.setResultsName('Chapters').setName('Chapters') BookName = CaselessLiteral('Acts') | CaselessLiteral('Psalm') | CaselessLiteral('John') BookName = BookName.setResultsName('Book').setName('Book') Book = Group(BookName + Chapters) Books = Book + ZeroOrMore(Suppress(';') + Book) Books = Books.setResultsName('Books').setName('Books') All = CaselessLiteral('Lesson Text:') + Books + LineEnd() s = 'Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23' res = All.parseString(s) for b in res.Books: for c in b.Chapters: if c.Verses: for v in c.Verses: print 'Book', b[0], 'Chapter', c[0], 'Verse', v else: print 'Book', b[0], 'Chapter', c[0] ###### Hopefully you can get the idea of most of it from looking at the code. Suppress() means "parse this token, but don't include it in the results". Group() is necessary for getting access to a list of things -- you can experiment by taking it out and seeing what you get. Obviously you'll need to add more names to the BookName element. Obviously also, there is a bit more work to be done on Verses. You might want to look into the concept of "parse actions". A really simple parse action might be this: def convertToNumber(string_, location, tokens): """ Used in setParseAction to make numeric parsers return numbers. """ return [int(tokens[0])] SingleVerse.setParseAction(convertToNumber) ChapterNum.setParseAction(convertToNumber) That should get you python integers instead of strings. You can probably do more with parseActions to, for instance, turn something like '15-20' into [15,16,17,18,19,20]. HTH! -- John. From eduardo.susan at gmail.com Fri May 22 04:49:22 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Thu, 21 May 2009 20:49:22 -0600 Subject: [Tutor] Parsing Bible verses In-Reply-To: <5e58f2e40905211803u206ebedey9c4d4e4e51903d2@mail.gmail.com> References: <9356b9f30905211426v3b684694if8ef0fc730e82258@mail.gmail.com> <5e58f2e40905211803u206ebedey9c4d4e4e51903d2@mail.gmail.com> Message-ID: <9356b9f30905211949t1c45c64ne9b46bd93e0e8fd6@mail.gmail.com> On Thu, May 21, 2009 at 7:03 PM, John Fouhy wrote: > 2009/5/22 Eduardo Vieira : >> I will be looking for lines like these: >> Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23 >> >> So, references in different chapters are separated by a semicolon. My >> main challenge would be make the program guess that 10:12 refers to >> the previous book. 15-20 means verses 15 thru 20 inclusive. I'm afraid >> that will take more than Regex and I never studied anything about >> parser tools, really. > > Well, pyparsing is one of the standard python parsing modules. ?It's > not that bad, really :-) > > Here's some code I knocked out: > > from pyparsing import * > > SingleVerse = Word(nums) > VerseRange = SingleVerse + '-' + SingleVerse > Verse = VerseRange | SingleVerse > Verse = Verse.setResultsName('Verse').setName('Verse') > Verses = Verse + ZeroOrMore(Suppress(',') + Verse) > Verses = Verses.setResultsName('Verses').setName('Verses') > > ChapterNum = Word(nums) > ChapterNum = ChapterNum.setResultsName('Chapter').setName('Chapter') > ChapVerses = ChapterNum + ':' + Verses > SingleChapter = Group(ChapVerses | ChapterNum) > > Chapters = SingleChapter + ZeroOrMore(Suppress(';') + SingleChapter) > Chapters = Chapters.setResultsName('Chapters').setName('Chapters') > > BookName = CaselessLiteral('Acts') | CaselessLiteral('Psalm') | > CaselessLiteral('John') > BookName = BookName.setResultsName('Book').setName('Book') > > Book = Group(BookName + Chapters) > Books = Book + ZeroOrMore(Suppress(';') + Book) > Books = Books.setResultsName('Books').setName('Books') > > All = CaselessLiteral('Lesson Text:') + Books + LineEnd() > > s = 'Lesson Text: Acts 5:15-20, 25; 10:12; John 3:16; Psalm 23' > res = All.parseString(s) > > for b in res.Books: > ? ?for c in b.Chapters: > ? ? ? ?if c.Verses: > ? ? ? ? ? ?for v in c.Verses: > ? ? ? ? ? ? ? ?print 'Book', b[0], 'Chapter', c[0], 'Verse', v > ? ? ? ?else: > ? ? ? ? ? ?print 'Book', b[0], 'Chapter', c[0] > > ###### > > Hopefully you can get the idea of most of it from looking at the code. > > Suppress() means "parse this token, but don't include it in the results". > > Group() is necessary for getting access to a list of things -- you can > experiment by taking it out and seeing what you get. > > Obviously you'll need to add more names to the BookName element. > > Obviously also, there is a bit more work to be done on Verses. ?You > might want to look into the concept of "parse actions". ?A really > simple parse action might be this: > > def convertToNumber(string_, location, tokens): > ? ?""" Used in setParseAction to make numeric parsers return numbers. """ > > ? ?return [int(tokens[0])] > > SingleVerse.setParseAction(convertToNumber) > ChapterNum.setParseAction(convertToNumber) > > That should get you python integers instead of strings. ?You can > probably do more with parseActions to, for instance, turn something > like '15-20' into [15,16,17,18,19,20]. > > HTH! > > -- > John. > Thanks for the thorough example, I guess I really should get into this thing of parsing somehow. To W W. I guess that approach can work too. I will study both things and if I get stumped, I'll try the list again. It will take a while for me to really delve into the task, but I want to do it for a good friend of mine. Eduardo From smiles at worksmail.net Sat May 23 11:37:34 2009 From: smiles at worksmail.net (C or L Smith) Date: Sat, 23 May 2009 15:22:34 +0545 Subject: [Tutor] Parsing Bible verses Message-ID: <8633E44E09394855BC66724793935E49@kisc.edu.np> Here is something from my toolbox of routines that might be useful for the number ranges: >>> indices('-5--2') [-5, -4, -3, -2] >>> indices('3-4') [3, 4] >>> indices('3-4,10') [3, 4, 10] /chris def indices(s,n=None): #("1-3,7")->1,2,3,7;i("1,-3--1")->1,-3,-2,-1; or (slc,n=None)->slc.start,stop,step [for range(n)] """Return a list of indices as defined by a MSWord print dialog-like range: e.g. "1,3,5-7" -> [1, 3, 5, 6, 7] A trailing comma will be ignored; a trailing dash will generate an error.""" # ranges must be increasing: -3--4 will not generate any numbers assert type(s) is str r=[x.strip() for x in s.split(',')] rv = [] for ri in r: if not ri: continue if ri.find('-',1)>0: #ignore - in first position dashat = ri.find('-',1) #start searching at position 1 nums = ri[:dashat],ri[dashat+1:] #one might want to use sys.maxint-1 for stop if the '-' is encountered, the #meaning being "from start to the end (as defined by the code elsewhere") #but then this should be made into an iterator rather than generating the #whole list if nums[1] in ['','-']: raise ValueError('missing number in request to indices: %s'%ri) start, stop = [int(x.strip()) for x in nums] for i in xrange(start, stop+1): rv.append(i)#yield i else: rv.append(int(ri))#yield int(ri) return rv From david at abbottdavid.com Sat May 23 17:51:38 2009 From: david at abbottdavid.com (David) Date: Sat, 23 May 2009 11:51:38 -0400 Subject: [Tutor] sqlite3 format data from tables Message-ID: <4A181B8A.1010300@abbottdavid.com> I have a budget program I am using to learn from. http://linuxcrazy.pastebin.com/f3b301daf I can not figure out how to get the transaction details to return so that it looks nice. It returns like this now. Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), (3, u'Deposit', 500), (4, u'Python Book', -50)] Here is how it gets to that point; def get_transactions(self): ct=self.connection.cursor() ct.execute("select * from transact;") return ct.fetchall() def report_transactions(self): return self.database.get_transactions() def display_transactions(self): print "Your transaction History is:",self.bl.report_transactions() self.menu[5]=("Transaction History",self.display_transactions) Can I use the numbers 1,2,3,4 as a way to return the history? Thanks, I hope I explained it correctly :) -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From david at abbottdavid.com Sat May 23 18:48:02 2009 From: david at abbottdavid.com (David) Date: Sat, 23 May 2009 12:48:02 -0400 Subject: [Tutor] sqlite3 format data from tables In-Reply-To: <4A181B8A.1010300@abbottdavid.com> References: <4A181B8A.1010300@abbottdavid.com> Message-ID: <4A1828C2.9020304@abbottdavid.com> David wrote: > I have a budget program I am using to learn from. > http://linuxcrazy.pastebin.com/f3b301daf > > I can not figure out how to get the transaction details to return so > that it looks nice. It returns like this now. > > Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), > (3, u'Deposit', 500), (4, u'Python Book', -50)] > > Here is how it gets to that point; > > def get_transactions(self): > ct=self.connection.cursor() > ct.execute("select * from transact;") > return ct.fetchall() > > def report_transactions(self): > return self.database.get_transactions() > > def display_transactions(self): > print "Your transaction History is:",self.bl.report_transactions() > > self.menu[5]=("Transaction History",self.display_transactions) > > Can I use the numbers 1,2,3,4 as a way to return the history? > > Thanks, I hope I explained it correctly :) > -david Ok this seems to work OK; def display_transactions(self): transactions = self.bl.report_transactions() for data in transactions: print "Transaction History: ", data[-2], data[-1] -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From mwalsh at mwalsh.org Sat May 23 19:03:34 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Sat, 23 May 2009 12:03:34 -0500 Subject: [Tutor] sqlite3 format data from tables In-Reply-To: <4A1828C2.9020304@abbottdavid.com> References: <4A181B8A.1010300@abbottdavid.com> <4A1828C2.9020304@abbottdavid.com> Message-ID: <4A182C66.4000905@mwalsh.org> David wrote: > David wrote: >> I have a budget program I am using to learn from. >> http://linuxcrazy.pastebin.com/f3b301daf >> >> I can not figure out how to get the transaction details to return so >> that it looks nice. It returns like this now. >> >> Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', >> -200), (3, u'Deposit', 500), (4, u'Python Book', -50)] >> >> Here is how it gets to that point; >> >> def get_transactions(self): >> ct=self.connection.cursor() >> ct.execute("select * from transact;") >> return ct.fetchall() >> >> def report_transactions(self): >> return self.database.get_transactions() >> >> def display_transactions(self): >> print "Your transaction History is:",self.bl.report_transactions() >> >> self.menu[5]=("Transaction History",self.display_transactions) >> >> Can I use the numbers 1,2,3,4 as a way to return the history? >> >> Thanks, I hope I explained it correctly :) >> -david > Ok this seems to work OK; > def display_transactions(self): > transactions = self.bl.report_transactions() > for data in transactions: > print "Transaction History: ", data[-2], data[-1] > > It would be helpful if you showed the output you expect. Here is another option ... def display_transactions(self): transactions = self.bl.report_transactions() print "Your transaction History is:" for n, k, v in transactions: print ' %-15s %10.2f' % (k, v) HTH, Marty From lists at mostrom.pp.se Sat May 23 21:09:28 2009 From: lists at mostrom.pp.se (=?UTF-8?Q?Ja=6E_E=72ik_M=6Fs=74=72=C3=B6?= =?UTF-8?Q?m?=) Date: Sat, 23 May 2009 21:09:28 +0200 Subject: [Tutor] Post to wordpress Message-ID: Hi, I'm looking for a module or an example for how to post to a wordpress blog. I want to create a post and set: + title + content + category + tags + publish date I've looked around and found (setting categories doesn't seem to work, don't know if tags/date is possible) (no code available) (setting categories doesn't seem to work, don't know about tags/date) (same as above) Does anyone know if a library exists or some example of how to do this? jem -- Jan Erik Mostr?m, http://mostrom.eu From david at abbottdavid.com Sat May 23 21:56:15 2009 From: david at abbottdavid.com (David) Date: Sat, 23 May 2009 15:56:15 -0400 Subject: [Tutor] Post to wordpress In-Reply-To: References: Message-ID: <4A1854DF.5090600@abbottdavid.com> Jan Erik Mostr?m wrote: > Hi, > > I'm looking for a module or an example for how to post to a wordpress > blog. I want to create a post and set: > > + title > + content > + category > + tags > + publish date > > I've looked around and found > > (setting > categories doesn't seem to work, don't know if tags/date is possible) > > (no code available) > > > (setting categories doesn't seem to work, don't know about tags/date) > > > (same as above) > > Does anyone know if a library exists or some example of how to do this? > > jem I use pycurl to post to a drupal site; http://linuxcrazy.pastebin.com/f6835c5f8 This may help to explain it; http://madeuce.wordpress.com/2009/02/23/further-simplification-of-pycurl/ -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Sun May 24 00:20:58 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 23 May 2009 23:20:58 +0100 Subject: [Tutor] sqlite3 format data from tables References: <4A181B8A.1010300@abbottdavid.com> Message-ID: "David" wrote > I can not figure out how to get the transaction details to return so that > it looks nice. It returns like this now. > > Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), > (3, u'Deposit', 500), (4, u'Python Book', -50)] You are just printing raw data as retuirned from the cursor. Databases are good at storing and retrieving data. They are terrible at formatting it. (Even when you use the formatting features of some SQLs) You need to get the raw data and format it how you want it yourself. You could do that in your displaydata function (since display implies some fancy formatting) or even in your report data (although here I'd more likely expect you to structure the data in a dictionary or somesuch) > Here is how it gets to that point; > > def get_transactions(self): > ct=self.connection.cursor() > ct.execute("select * from transact;") > return ct.fetchall() > > def report_transactions(self): > return self.database.get_transactions() Unless you intend doing something useful this function is just an extra later of processing! > def display_transactions(self): > print "Your transaction History is:",self.bl.report_transactions() This is where you need todeciode how the data should look and format it accordingly. The best way to dio that is probably using a format string which will let you set the field length, justification etc. (Or if using Python v3 the even more powerful new formatting features there) > self.menu[5]=("Transaction History",self.display_transactions) > > Can I use the numbers 1,2,3,4 as a way to return the history? I don;t really understand what you mean by this bit? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Sun May 24 01:15:55 2009 From: david at abbottdavid.com (David) Date: Sat, 23 May 2009 19:15:55 -0400 Subject: [Tutor] sqlite3 format data from tables In-Reply-To: <4A182C66.4000905@mwalsh.org> References: <4A181B8A.1010300@abbottdavid.com> <4A1828C2.9020304@abbottdavid.com> <4A182C66.4000905@mwalsh.org> Message-ID: <4A1883AB.9080206@abbottdavid.com> Martin Walsh wrote: > David wrote: >> David wrote: >>> I have a budget program I am using to learn from. >>> http://linuxcrazy.pastebin.com/f3b301daf >>> >>> I can not figure out how to get the transaction details to return so >>> that it looks nice. It returns like this now. >>> >>> Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', >>> -200), (3, u'Deposit', 500), (4, u'Python Book', -50)] >>> >>> Here is how it gets to that point; >>> >>> def get_transactions(self): >>> ct=self.connection.cursor() >>> ct.execute("select * from transact;") >>> return ct.fetchall() >>> >>> def report_transactions(self): >>> return self.database.get_transactions() >>> >>> def display_transactions(self): >>> print "Your transaction History is:",self.bl.report_transactions() >>> >>> self.menu[5]=("Transaction History",self.display_transactions) >>> >>> Can I use the numbers 1,2,3,4 as a way to return the history? >>> >>> Thanks, I hope I explained it correctly :) >>> -david >> Ok this seems to work OK; >> def display_transactions(self): >> transactions = self.bl.report_transactions() >> for data in transactions: >> print "Transaction History: ", data[-2], data[-1] >> >> > > > It would be helpful if you showed the output you expect. Here is another > option ... > > def display_transactions(self): > transactions = self.bl.report_transactions() > print "Your transaction History is:" > for n, k, v in transactions: > print ' %-15s %10.2f' % (k, v) > > HTH, > Marty > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Thanks very nice, Your transaction History is: Food -100.00 Deposit -200.00 Deposit 500.00 Python Book -50.00 Gift 300.00 Also thank you Alan for your tips :) >> >> def report_transactions(self): >> return self.database.get_transactions() > > Unless you intend doing something useful this function > is just an extra later of processing! I got a kick out of it, helped me understand how the different function's and the classes work together, thats all, I found most of the code on the web and just patched it together to be honest :) >> Can I use the numbers 1,2,3,4 as a way to return the history? > > I don;t really understand what you mean by this bit? > > Here is the data as it is returned from sqlite; Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), (3, u'Deposit', 500), (4, u'Python Book', -50)] I was wondering if the numbers could be used to return the contents of the tuple's. Sometimes it is hard to ask a good question when I don't have a clue, thanks for your patience. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From gsumalenurse at yahoo.com Sun May 24 05:31:07 2009 From: gsumalenurse at yahoo.com (Leon Williams) Date: Sat, 23 May 2009 20:31:07 -0700 (PDT) Subject: [Tutor] strings using Czech language characters Message-ID: <100378.44092.qm@web31103.mail.mud.yahoo.com> Hello, ? I wish to make a dictionary of english to czech words.? I thought I would first see if python will print a czech character.? I used the example in the tutorial (it uses a euro symbol,?and a different iso character set so I changed mine to include a character set that includes eastern european characters).? My results follow: ? >>> # -*- coding: iso_8859-2 -*- >>> cz_c = u"?" Unsupported characters in input ? here is a link that shows that iso 8859-2 includes?lower case?c with hacek - ? http://en.wikipedia.org/wiki/ISO-8859-2 ? I would appreciate any help, thanks. ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From meital.amitai at gmail.com Sun May 24 06:36:49 2009 From: meital.amitai at gmail.com (Meital Amitai) Date: Sat, 23 May 2009 21:36:49 -0700 Subject: [Tutor] need help opening a file in idle Message-ID: <4aeea5b00905232136j31e8f809x5b3bd84a898a4777@mail.gmail.com> Hi everyone, I am not sure how to open a file in my computer in idle. For my python class the homework states to write a function that accepts the name of a file and returns a tuple containing the number of lines, words and characters in the file. My problem is how do I get idle to first accept the name of a file. I created a file in text edit program saved with a .doc extension, saved on my desktop. However the only way I have learned to open a file in my class is to write something like: f = open('inputfile','r') line = f.readline() but when i type exactly that into my idle i get the following error message: Traceback (most recent call last): File "", line 1, in f = open('inputfile','r') IOError: [Errno 2] No such file or directory: 'inputfile' and when i ask it to open the actual file i created in my text edit program: file1.doc f = open('file1.doc','r') I get the same message. I am not sure how to tell it to go to desktop folder in my computer where i saved that file. Am I missing something, and completely not getting the point? Any help would be appreciated. Thanks so much! -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sun May 24 07:10:52 2009 From: bgailer at gmail.com (bob gailer) Date: Sun, 24 May 2009 01:10:52 -0400 Subject: [Tutor] need help opening a file in idle In-Reply-To: <4aeea5b00905232136j31e8f809x5b3bd84a898a4777@mail.gmail.com> References: <4aeea5b00905232136j31e8f809x5b3bd84a898a4777@mail.gmail.com> Message-ID: <4A18D6DC.1050907@gmail.com> Meital Amitai wrote: > Hi everyone, > > I am not sure how to open a file in my computer in idle. > > For my python class the homework states to write a function that > accepts the name of a file and returns a tuple containing the number > of lines, words and characters in the file. My problem is how do I get > idle to first accept the name of a file. I created a file in text edit > program saved with a .doc extension, saved on my desktop. However the > only way I have learned to open a file in my class is to write > something like: > > f = open('inputfile','r') > line = f.readline() > > but when i type exactly that into my idle i get the following error > message: Traceback (most recent call last): > File "", line 1, in > f = open('inputfile','r') > IOError: [Errno 2] No such file or directory: 'inputfile' > > and when i ask it to open the actual file i created in my text edit > program: file1.doc > f = open('file1.doc','r') > I get the same message. > > I am not sure how to tell it to go to desktop folder in my computer > where i saved that file. Am I missing something, and completely not > getting the point? 'file1.doc' is a relative path. It looks in the current directory. Obviously that file is not in the current directory. So you must provide the complete path. That is no longer a Python issue! The path to the desktop depends on OS and username! I suggest you put the file in a different directory; one that you can easily specify. -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Sun May 24 10:16:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 May 2009 09:16:33 +0100 Subject: [Tutor] need help opening a file in idle References: <4aeea5b00905232136j31e8f809x5b3bd84a898a4777@mail.gmail.com> Message-ID: "Meital Amitai" wrote > I am not sure how to open a file in my computer in idle. You need to be clear what you are doing. You are not opening the file in IDLE you are opening the file in Python. IDLE is just the tool you use to write Ptython programs. > For my python class the homework states to write a function that accepts > the > name of a file and returns a tuple containing the number of lines, words > and > characters in the file. My problem is how do I get idle to first accept > the > name of a file. You need to get Python to accept the name of the file. And the biggest problem there is to identify the full path of the file. To program successfully you will need to understand where and how your computer stores files. On modern computers casual users don't really need to know that kind of stuff but programmers do. > I created a file in text edit program saved with a .doc > extension, saved on my desktop. However the only way I have learned to > open > a file in my class is to write something like: > > f = open('inputfile','r') > line = f.readline() > but when i type exactly that into my idle i get the following error > message: > Traceback (most recent call last): > File "", line 1, in > f = open('inputfile','r') > IOError: [Errno 2] No such file or directory: 'inputfile' I'm assuming you realise that inputfile is a symbol that stands for any file name it doesn't have to be literally inputfile... But the file must exist before you can read it (the 'r' bit says you want to read the file) > and when i ask it to open the actual file i created in my text edit > program: > file1.doc > f = open('file1.doc','r') > I get the same message. You didn't tell Python to look in the Desktop folder (Although that's not a good place to store files unless you want a very confusing desktop! You would be better to store it in MyDocuments) > I am not sure how to tell it to go to desktop folder in my computer where > i > saved that file. Am I missing something, and completely not getting the > point? It's up to you to know where the file is and tell Python. You might try this to find out where Python is looking for the file by default. import os print os.getcwd() Which prints what Python considers the Current Working Directory. You will find a lot more on this topic, including a program for counting words in a file in the Handling files topic of my tutorial. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Sun May 24 13:34:55 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 24 May 2009 07:34:55 -0400 Subject: [Tutor] strings using Czech language characters In-Reply-To: <100378.44092.qm@web31103.mail.mud.yahoo.com> References: <100378.44092.qm@web31103.mail.mud.yahoo.com> Message-ID: <1c2a2c590905240434x5a05ae9cr3810db59a997d9f@mail.gmail.com> On Sat, May 23, 2009 at 11:31 PM, Leon Williams wrote: > Hello, > > I wish to make a dictionary of english to czech words.? I thought I would > first see if python will print a czech character.? I used the example in the > tutorial (it uses a euro symbol,?and a different iso character set so I > changed mine to include a character set that includes eastern european > characters).? My results follow: > >>>> # -*- coding: iso_8859-2 -*- >>>> cz_c = u"?" > Unsupported characters in input The coding declaration only affects the encoding of program files, not the interactive interpreter. I have some notes here hthat may help: http://personalpages.tds.net/~kent37/stories/00018.html Try cz_c = "?".decode(sys.stdin.encoding) Kent From mysterytramp at yahoo.com Sun May 24 20:08:00 2009 From: mysterytramp at yahoo.com (M Tramp) Date: Sun, 24 May 2009 11:08:00 -0700 (PDT) Subject: [Tutor] A few very basic questions Message-ID: <228072.40110.qm@web57207.mail.re3.yahoo.com> Hello all ... I've started trying ot learn about Python because I'm intrigued by what I've read about Gramps, a genealogical data manager. To run Gramps on a Mac, I need to get GTK up and running. Here's where I have a few, hopefully basic, questions. (FYI, I have a Mac, running OS X 10.4.11. Xcode 2.5) 1) GTK has several dependencies, according to http://library.gnome.org/devel/gtk-faq/stable/c192.html ... pkg-config; GNU make; JPEG, PNG and TIFF image libraries; FreeType; fontconfig; GNU libiconv library; GNU gettext; GLib; Pango; ATK. Each of these may have dependencies as well. Is there someplace to go to figure out the order they should be compiled? (I started with pkg-config, but it needs GLib) Possibly the best answer is to download the source for each and just start compiling? 2) Once each library is compiled, what should I have and where should it be on my hard drive? For example, when Python sees "import gtk" in which directory is it looking for GTK? How will GTK and the other libraries appear on my hard disk? As Unix executables or as frameworks or as something else entirely? 3) I am likely to do my Python programming inside Eclipse using pydev, though I have Xcode. The libraries will be in C, correct? Will I be able to use Xcode to compile GTK and the rest, and then access the libraries inside Eclipse? mt From michael at arpsorensen.dk Sun May 24 22:02:12 2009 From: michael at arpsorensen.dk (=?UTF-8?Q?Michael_Bernhard_Arp_S=C3=B8rensen?=) Date: Sun, 24 May 2009 22:02:12 +0200 Subject: [Tutor] Find a good linux distribution with python. Message-ID: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Hi there. I'm on an eternal quest to find the perfect linux distro to base my work on. I'm currently running Ubuntu because its told that the Ubuntu creator Mark Shuttleworth is a python hacker. Therefore there's a lot of python stuff in the repository. What distro do you run? Another point: There's so many modules on the Internet, that it's difficult to get some sort of overview. That's why a distro is such a great idea because so many packages with python stuff is included. Could you get any kind of overview over python modules/libs other than a repo in a distro? I have concluded that there's no such thing as a perfect distro. Only some distros come close to perfection. In that case I was wondering if I should create my own python repo to set up a good working environment for development. That way I could use any distro. On the other hand, any code I migth write would be difficult for others to use. Any ideas or comments? I use emacs with rope, ipython and python. I like those in newer versions if not bleeding edge. Med venlig hilsen/Kind regards Michael B. Arp S?rensen Programmer / BOFH Denmark "If you want to enter my network while I'm out, you can find my SSH-key under my mouse mat" - Michael Bernhard Arp S?rensen -------------- next part -------------- An HTML attachment was scrubbed... URL: From roberto03 at gmail.com Sun May 24 23:03:24 2009 From: roberto03 at gmail.com (roberto) Date: Sun, 24 May 2009 23:03:24 +0200 Subject: [Tutor] serious problem with graphics module Message-ID: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> hello everyone i have a problem with python 3.0 graphics module: no problem while importing the module >>> import turtle but when i issue any command like: >>> t = turtle.pen() >>> t = turtle.forward(60) nothing appears on the screen, only a blank window, with nothing inside; may you give me some hint ? OS: win Xp thank you in advance ! -- roberto From bhaaluu at gmail.com Sun May 24 23:11:55 2009 From: bhaaluu at gmail.com (bhaaluu) Date: Sun, 24 May 2009 17:11:55 -0400 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Message-ID: On Sun, May 24, 2009 at 4:02 PM, Michael Bernhard Arp S?rensen wrote: > > What distro do you run? > Debian GNU/Linux > > Another point: There's so many modules on the Internet, that it's difficult > to get some sort of overview. That's why a distro is such a great idea > because so many packages with python stuff is included. Could you get any > kind of overview over python modules/libs other than a repo in a distro? > Like anything else that is huge, just tackle the parts of it that you need. > > I have concluded that there's no such thing as a perfect distro. Only some > distros come close to perfection. In that case I was wondering if I should > create my own python repo to set up a good working environment for > development. That way I could use any distro. On the other hand, any code I > migth write would be difficult for others to use. > > Any ideas or comments? > "Ubuntu" is an African word that means "I can't configure Debian." 8^D Knoppix, Ubuntu, and several other distributions are all based on the fine work that the Debian GNU/Linux community does. Debian GNU/Linux is completely Free, and is developed by volunteers from around the world. > > I use emacs with rope, ipython and python. I like those in newer versions if > not bleeding edge. > If you're really serious about having the 'perfect' distro, you can start with Debian GNU/Linux and make your own, the way you want it to be. While it is not a trivial matter to re-master a Linux Live CD, it is much easier than it used to be! You could pack it full of Python! It is this reason that there are hundreds of Linux Live CD distributions. Each person thinks that their distro is the best. This is one reason why GNU/Linux is so fantastic! > Med venlig hilsen/Kind regards > > Michael B. Arp S?rensen > Programmer / BOFH > Denmark > > "If you want to enter my network while I'm out, you can find my SSH-key > under my mouse mat" - Michael Bernhard Arp S?rensen > Why don't you make a Python-centric Linux Live CD? -- b h a a l u u at g m a i l dot c o m Kid on Bus: What are you gonna do today, Napoleon? Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! From alan.gauld at btinternet.com Sun May 24 23:21:43 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 May 2009 22:21:43 +0100 Subject: [Tutor] A few very basic questions References: <228072.40110.qm@web57207.mail.re3.yahoo.com> Message-ID: "M Tramp" wrote > I've started trying ot learn about Python because ... Gramps, a > genealogical > To run Gramps on a Mac, I need to get GTK up and running. Have you checked fink? There's a MacOS Gimp toolkit package available. (Well 3 actually, gtk, gtk-data and gtk-shlibs) I generally use fink rather than try apt-get because I know the fink stuff is tested against MacOS. And fink packages are pre-built usually... > (FYI, I have a Mac, running OS X 10.4.11. Xcode 2.5) > 1) GTK has several dependencies, according to > http://library.gnome.org/devel/gtk-faq/stable/c192.html > ... pkg-config; GNU make; JPEG, PNG and TIFF image libraries; These should all be in place already > FreeType; fontconfig; GNU libiconv library; GNU gettext; GLib; Pango; > ATK. Not so sure about these though. > Each of these may have dependencies as well. Is there someplace to > go to figure out the order they should be compiled? apt-get should sort most of that out for you, it checks depemdencies and dependencies on dependencies etc. And if they are apt-get packages they should be either pre bu8ilt or apt-get should initiate the builds for you (I think, its been a while since I used it!) > 2) Once each library is compiled, what should I have and where > should it be on my hard drive? Using make (either directly or via apt-get) should sort that out. > For example, when Python sees "import gtk" in which directory is > it looking for GTK? Thats a different question because you need pygtk too and Python looks for the pygtk file which in turn looks for the real gtk library... And that should be a standard place that make knows about. > How will GTK and the other libraries appear on my hard disk? > As Unix executables or as frameworks or as something else entirely? In normal Unix it would be as libraries but under MacOS its entirely possible they show up as a Framework... > 3) I am likely to do my Python programming inside Eclipse using > pydev, though I have Xcode. In my experience Eclipse is a better option than XCode unless you are doing Aqua GUI programming XCode is great for Objective C but I never liked it for Python. > The libraries will be in C, correct? Yes, mostly, some of the pygtk stuff may be in python. > Will I be able to use Xcode to compile GTK and the rest, > and then access the libraries inside Eclipse? You should be able to. You may have to set up some paths in Eclipe's preferences. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 24 23:44:12 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 May 2009 22:44:12 +0100 Subject: [Tutor] Find a good linux distribution with python. References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Message-ID: "Michael Bernhard Arp S?rensen" wrote > I'm on an eternal quest to find the perfect linux distro Don't wste your time. The distro doesn't make much difference beyond: - The basic file system structure and if it sticks to (one of) the standard then thats not a big issue - The package manager - you may prefer apt-get or yast or rpm or whatever - the installer, but you only use it once (sort of!) I've used Slackware, Redhat, Mandrake/Mandiva, Suse, Debian(briefly!) and a couple of lesser known ones. Once they were up and trunning I didn't find much difference with any of them! > Therefore there's a lot of python stuff in the repository. The beauty of Python is that because it is platform independant it is also distro independant. Thus if you dowenloasd a python package and install it it will work regardless of distro. > Another point: There's so many modules on the Internet, that it's > difficult > to get some sort of overview. Most folks get by with the standard libraryb and a small number of specialist modules to meet their specific needs. If you need a new one install it, but loading lots of stuff just because its there is just a waste of disk space IMHO. > create my own python repo to set up a good working environment for > development. That way I could use any distro. By defaulyt Python keeps all its installed packages in the Python directory structure so, effectively, you do build up your own repo. But don't do it in advance just add to it as you need to. > On the other hand, any code I might write would be difficult > for others to use. If you stick to the a standard Python package tools it should be fine. Not just on Linux but on any other OS too. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From vy0123 at gmail.com Mon May 25 03:14:26 2009 From: vy0123 at gmail.com (vy) Date: Mon, 25 May 2009 11:14:26 +1000 Subject: [Tutor] strings using Czech language characters Message-ID: <561420f60905241814s14804eaby99a6053c722d7456@mail.gmail.com> [ wish to make a dictionary of english to czech words / Unsupported characters in input / ISO-8859-2 ] have a look at the manual, in my case, section 4.9.2 on stand encodings p. 149 of the 2.4.3 (lib.pdf) Python Library Reference and at the python prompt try >>> help('modules codecs') -- vy From walker.hale.iv at gmail.com Mon May 25 05:03:47 2009 From: walker.hale.iv at gmail.com (Walker Hale IV) Date: Sun, 24 May 2009 22:03:47 -0500 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Message-ID: <312f43620905242003r219d6f0ck1cc7f69b43b84fb8@mail.gmail.com> On Sun, May 24, 2009 at 4:44 PM, Alan Gauld wrote: > "Michael Bernhard Arp S?rensen" wrote > Most folks get by with the standard libraryb and a small number of > specialist modules to meet their specific needs. If you need a new > one install it, but loading lots of stuff just because its there is just a > waste of disk space IMHO. > >> create my own python repo to set up a good working environment for >> development. That way I could use any distro. > > By defaulyt Python keeps all its installed packages in the Python > directory structure so, effectively, you do build up your own repo. But > don't do it in advance just add to it as you need to. > >> On the other hand, any code I might write would be difficult >> for others to use. > > If you stick to the a standard Python package tools it should be fine. > Not just on Linux but on any other OS too. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ I agree with Alan. These days the trend seems to be in the opposite direction. I'm seeing developers keeping a "clean" Python environment and then installing additional packages into virtual environments. That keeps conflicts to a minimum and solves the problem of uninstalling unwanted packages. http://pypi.python.org/pypi/virtualenv Some developers even go so far as putting their Python environment under version control using something like Bazaar, git, or Mercurial (alphabetical order). This is in an effort to keep the contents of their Python environment under control. Once you have setuptools installed, it just too easy to type "easy_install spam" and get that package installed into your environment. The challenge is to control clutter in your environment. http://pypi.python.org/pypi/setuptools If you can keep your dependencies under control, then you can give users a reasonable set of requirements. If necessary, you can bundle things up for end users: http://www.py2exe.org/ http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html http://pypi.python.org/pypi/zc.buildout On the other hand, I do believe that Python needs an expanded distro for developers, but it would only contain a few extra things, such as the items mentioned above. -- Walker Hale From xboxmuncher at gmail.com Mon May 25 08:32:25 2009 From: xboxmuncher at gmail.com (xbmuncher) Date: Mon, 25 May 2009 02:32:25 -0400 Subject: [Tutor] Search for hex data in file Message-ID: I've been reading about ways to convert strings and what not to hex and back and forth. I'm looking for the fastest and least memory intensive way to search through a file for a hex value and gets its byte offset in the file. This hex value (that I'm looking for in the file) is of course a hex representation of the binary data and its 8 bytes long. I figured reading the whole file and converting it to hex from ascii..etc.. would be overkill, especially if its a large file. What do you guys recommend? I want to search the file for certain hex value and get the byte offset within the file. Thanks for reading. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 25 09:22:44 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 May 2009 08:22:44 +0100 Subject: [Tutor] Search for hex data in file References: Message-ID: "xbmuncher" wrote > search through a file for a hex value and gets its byte offset in the > file. > This hex value (that I'm looking for in the file) is of course a hex > representation of the binary data and its 8 bytes long. Can you give an example of your input data and what you expect the return value to look like? When you say it is a hex representation of binary data do you mean something like: "Here is some text with a hex number, 0xAB23C, embedded in it" If so then you don;t need to convert to hex, you just need a string search. But I'm not clear exactly what kind of data you are dealing with. > What do you guys recommend? I want to search the file for certain hex > value > and get the byte offset within the file. It might help if you posted some code too. And tell us what happens when you run it, and why that was not what you wanted to happen... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From zebra05 at gmail.com Mon May 25 10:05:04 2009 From: zebra05 at gmail.com (OkaMthembo) Date: Mon, 25 May 2009 10:05:04 +0200 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Message-ID: bhaaluu, your comments on Ubuntu are not only offensive, but display ignorance and arrogance all at the same time. Ubuntu is based on Debian, but provides a lower entry barrier to Linux for many. Not everybody wants to spend much time doing configurations. Many just want a system that works. Michael, to answer your question - Ubuntu is just fine! Regards, Lloyd On Sun, May 24, 2009 at 11:11 PM, bhaaluu wrote: > On Sun, May 24, 2009 at 4:02 PM, Michael Bernhard Arp S?rensen > wrote: > > > > What distro do you run? > > > > Debian GNU/Linux > > > > > Another point: There's so many modules on the Internet, that it's > difficult > > to get some sort of overview. That's why a distro is such a great idea > > because so many packages with python stuff is included. Could you get any > > kind of overview over python modules/libs other than a repo in a distro? > > > > Like anything else that is huge, just tackle the parts of it that you need. > > > > > I have concluded that there's no such thing as a perfect distro. Only > some > > distros come close to perfection. In that case I was wondering if I > should > > create my own python repo to set up a good working environment for > > development. That way I could use any distro. On the other hand, any code > I > > migth write would be difficult for others to use. > > > > Any ideas or comments? > > > > "Ubuntu" is an African word that means "I can't configure Debian." > 8^D > Knoppix, Ubuntu, and several other distributions are all based on > the fine work that the Debian GNU/Linux community does. Debian > GNU/Linux is completely Free, and is developed by volunteers from > around the world. > > > > > I use emacs with rope, ipython and python. I like those in newer versions > if > > not bleeding edge. > > > > If you're really serious about having the 'perfect' distro, you can start > with Debian GNU/Linux and make your own, the way you want it to be. > While it is not a trivial matter to re-master a Linux Live CD, it is much > easier than it used to be! You could pack it full of Python! It is this > reason that there are hundreds of Linux Live CD distributions. Each > person thinks that their distro is the best. This is one reason why > GNU/Linux is so fantastic! > > > Med venlig hilsen/Kind regards > > > > Michael B. Arp S?rensen > > Programmer / BOFH > > Denmark > > > > "If you want to enter my network while I'm out, you can find my SSH-key > > under my mouse mat" - Michael Bernhard Arp S?rensen > > > > Why don't you make a Python-centric Linux Live CD? > -- > b h a a l u u at g m a i l dot c o m > Kid on Bus: What are you gonna do today, Napoleon? > Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at arpsorensen.dk Mon May 25 10:57:36 2009 From: michael at arpsorensen.dk (=?UTF-8?Q?Michael_Bernhard_Arp_S=C3=B8rensen?=) Date: Mon, 25 May 2009 10:57:36 +0200 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> Message-ID: <1618520905250157l4919be87u561c6a354f069bb8@mail.gmail.com> Hi Lloyd. I did not take offense about the comment on Ubuntu. It was an old joke and it made me smile.... again. :-) I've used Debian before, but I have moved to Ubuntu because Debian takes forever to release a new version. Almost as long as Microsoft. I like Debian based distro the most. To all the others who was kind enough to contribute to this thread: Your points are taken. I think I'll stick a "normal" vanilla Linux distro and avoid any unnecessary setup. I particularly liked the idea of not cluttering my local environment with too much special setup since I write code for others than myself. One of the more serious issues in the past was the use of Amara (an XML parser). Amara is now a part of Debian and Ubuntu, which eliminates the need for a special build of the module. I just wished that Python was upgraded in the distros. If I want to play with Python3, I will need to compile it my self and specify which Python interpretor to use in each Python file. But this is a small problem. I'm very fond of using emacs for many purposes besides coding in Python. It keeps me productive when I don't have a "writers block". :-) To me a good linux distro contains all the great modes for Emacs and the newest version of Emacs and Python. Thank you all very much. Med venlig hilsen/Kind regards Michael B. Arp S?rensen Programmer / BOFH "If you want to enter my network while I'm out, you can find my SSH-key under my mouse mat" - Michael Bernhard Arp S?rensen On Mon, May 25, 2009 at 10:05 AM, OkaMthembo wrote: > bhaaluu, your comments on Ubuntu are not only offensive, but display > ignorance and arrogance all at the same time. > Ubuntu is based on Debian, but provides a lower entry barrier to Linux for > many. > > Not everybody wants to spend much time doing configurations. Many just want > a system that works. > > Michael, to answer your question - Ubuntu is just fine! > > Regards, > Lloyd > > > > On Sun, May 24, 2009 at 11:11 PM, bhaaluu wrote: > >> On Sun, May 24, 2009 at 4:02 PM, Michael Bernhard Arp S?rensen >> wrote: >> > >> > What distro do you run? >> > >> >> Debian GNU/Linux >> >> > >> > Another point: There's so many modules on the Internet, that it's >> difficult >> > to get some sort of overview. That's why a distro is such a great idea >> > because so many packages with python stuff is included. Could you get >> any >> > kind of overview over python modules/libs other than a repo in a distro? >> > >> >> Like anything else that is huge, just tackle the parts of it that you >> need. >> >> > >> > I have concluded that there's no such thing as a perfect distro. Only >> some >> > distros come close to perfection. In that case I was wondering if I >> should >> > create my own python repo to set up a good working environment for >> > development. That way I could use any distro. On the other hand, any >> code I >> > migth write would be difficult for others to use. >> > >> > Any ideas or comments? >> > >> >> "Ubuntu" is an African word that means "I can't configure Debian." >> 8^D >> Knoppix, Ubuntu, and several other distributions are all based on >> the fine work that the Debian GNU/Linux community does. Debian >> GNU/Linux is completely Free, and is developed by volunteers from >> around the world. >> >> > >> > I use emacs with rope, ipython and python. I like those in newer >> versions if >> > not bleeding edge. >> > >> >> If you're really serious about having the 'perfect' distro, you can start >> with Debian GNU/Linux and make your own, the way you want it to be. >> While it is not a trivial matter to re-master a Linux Live CD, it is much >> easier than it used to be! You could pack it full of Python! It is this >> reason that there are hundreds of Linux Live CD distributions. Each >> person thinks that their distro is the best. This is one reason why >> GNU/Linux is so fantastic! >> >> > Med venlig hilsen/Kind regards >> > >> > Michael B. Arp S?rensen >> > Programmer / BOFH >> > Denmark >> > >> > "If you want to enter my network while I'm out, you can find my SSH-key >> > under my mouse mat" - Michael Bernhard Arp S?rensen >> > >> >> Why don't you make a Python-centric Linux Live CD? >> -- >> b h a a l u u at g m a i l dot c o m >> Kid on Bus: What are you gonna do today, Napoleon? >> Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Lloyd Dube > -------------- next part -------------- An HTML attachment was scrubbed... URL: From prasadaraon50 at gmail.com Mon May 25 14:24:15 2009 From: prasadaraon50 at gmail.com (prasad rao) Date: Mon, 25 May 2009 17:54:15 +0530 Subject: [Tutor] ????? Message-ID: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> hello I get a problem while trying to print non callable items in dir(module).May be as the items are strings. for x in dir(sys): ?? if sys.x is callable: ???? print sys.x() ?? else:print sys.x Traceback (most recent call last): File "", line 2, in if sys.x is callable: AttributeError: 'module' object has no attribute 'x' Some one please tell me How can I do that? Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Mon May 25 14:42:59 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 25 May 2009 14:42:59 +0200 Subject: [Tutor] ????? In-Reply-To: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> Message-ID: <4A1A9253.5010603@compuscan.co.za> prasad rao wrote: > hello > I get a problem while trying to print non callable > items in dir(module).May be as the items are strings. > > > > for x in dir(sys): > ?? if sys.x is callable: > ???? print sys.x() > ?? else:print sys.x > > > > Traceback (most recent call last): > File "", line 2, in > if sys.x is callable: > AttributeError: 'module' object has no attribute 'x' > > > > Some one please tell me How can I do that? > > Prasad > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > What you should be doing is rather for attrib in dir(sys): if callable('sys.%s' % attrib): print 'Callable: sys.%s' % attrib else: print 'Not Callable: sys.%s' % attrib Any particular reason why you need to be able to do this, just wondering about the use case for such automation ? -- Kind Regards, Christian Witts From a.t.hofkamp at tue.nl Mon May 25 14:44:54 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 25 May 2009 14:44:54 +0200 Subject: [Tutor] ????? In-Reply-To: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> Message-ID: <4A1A92C6.5070200@tue.nl> prasad rao wrote: > hello > I get a problem while trying to print non callable > items in dir(module).May be as the items are strings. It is much easier to read about the contents of the sys module in the documentation: http://docs.python.org/library/sys.html#module-sys Not only is the contents listed, the intention is also explained, and last but not least, it is to a large extent guaranteed to be stable across several platforms and across several Python versions. Albert From prasadaraon50 at gmail.com Mon May 25 14:45:35 2009 From: prasadaraon50 at gmail.com (prasad rao) Date: Mon, 25 May 2009 18:15:35 +0530 Subject: [Tutor] ????? In-Reply-To: <4A1A9253.5010603@compuscan.co.za> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> <4A1A9253.5010603@compuscan.co.za> Message-ID: <9e3fac840905250545t1b6c5c52k5404fa5a80855ea2@mail.gmail.com> > > >Any particular reason why you need to be able to do this, just wondering > about the use case for >such automation ? > > -- > Kind Regards, > Christian Witts > > > Thank you Christian for the prompt repply. I am learning python. I am just examining what are all the available items in each module . I tried to use getatter but somehow I could not succeed. Thank you Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Mon May 25 14:59:46 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 25 May 2009 14:59:46 +0200 Subject: [Tutor] ????? In-Reply-To: <9e3fac840905250545t1b6c5c52k5404fa5a80855ea2@mail.gmail.com> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> <4A1A9253.5010603@compuscan.co.za> <9e3fac840905250545t1b6c5c52k5404fa5a80855ea2@mail.gmail.com> Message-ID: <4A1A9642.3080907@compuscan.co.za> prasad rao wrote: > > > >Any particular reason why you need to be able to do this, just > wondering about the use case for >such automation ? > > -- > Kind Regards, > Christian Witts > > > Thank you Christian for the prompt repply. > I am learning python. > I am just examining what are all the available > items in each module . > I tried to use getatter but somehow I could not > succeed. > > Thank you > > Prasad > If you're running in a console you can use help(sys) for example to get a list of everything in the sys module, or as Albert pointed out take a look at docs.python.org for a nice listing. -- Kind Regards, Christian Witts From prasadaraon50 at gmail.com Mon May 25 14:53:03 2009 From: prasadaraon50 at gmail.com (prasad rao) Date: Mon, 25 May 2009 18:23:03 +0530 Subject: [Tutor] ????? In-Reply-To: <4A1A9253.5010603@compuscan.co.za> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> <4A1A9253.5010603@compuscan.co.za> Message-ID: <9e3fac840905250553s4ee59247i7655c68dc856db3c@mail.gmail.com> >for attrib in dir(sys): > > if callable('sys.%s' % attrib): > >print 'Callable: sys.%s' % attrib > > else: > >print 'Not Callable: sys.%s' % attrib > > Any particular reason why you need to be able to do this, just wondering > about the use case for such automation ? > > -- > Kind Regards, > Christian Witts > > > Hello It is printing wether the item is callable or not. I want it to print sys.getdefaultencoding ASCII sys.getfilesystemencoding mbcs Like that Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Mon May 25 15:07:28 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 25 May 2009 15:07:28 +0200 Subject: [Tutor] ????? In-Reply-To: <9e3fac840905250553s4ee59247i7655c68dc856db3c@mail.gmail.com> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> <4A1A9253.5010603@compuscan.co.za> <9e3fac840905250553s4ee59247i7655c68dc856db3c@mail.gmail.com> Message-ID: <4A1A9810.9010803@compuscan.co.za> prasad rao wrote: > > > >for attrib in dir(sys): > > if callable('sys.%s' % attrib): > >print 'Callable: sys.%s' % attrib > > else: > >print 'Not Callable: sys.%s' % attrib > > Any particular reason why you need to be able to do this, just > wondering about the use case for such automation ? > > -- > Kind Regards, > Christian Witts > > > > Hello > It is printing wether the item is callable or not. > I want it to print > sys.getdefaultencoding > ASCII > sys.getfilesystemencoding > mbcs > > Like that > > Prasad > > My apologies, didn't double check. It should be: for attrib in dir(sys): if callable(sys.__getattribute__(attrib)): print 'Callable: sys.%s' % attrib) else: print 'Not Callable: sys.%s' % attrib -- Kind Regards, Christian Witts Data Management C o m p u s c a n | Confidence in Credit Telephone: +27 21 888 6000 National Cell Centre: 0861 51 41 31 Fax: +27 21 413 2424 E-mail: cwitts at compuscan.co.za NOTE: This e-mail (including attachments )is subject to the disclaimer published at: http://www.compuscan.co.za/live/content.php?Item_ID=494. If you cannot access the disclaimer, request it from email.disclaimer at compuscan.co.za or 0861 514131. From paras80 at gmail.com Mon May 25 05:18:22 2009 From: paras80 at gmail.com (Paras K.) Date: Sun, 24 May 2009 23:18:22 -0400 Subject: [Tutor] IP-range Message-ID: Hello, I came across your answer / assistance on the IP range. I am fairly new to the python world of programming. However, up to this point I have always been able to get my programs to work by reading the books or following the guides I find through google.com Here is what I have to do: I have to read a number of cvs files into 1 large file. (I have been able to get that done through a perl script). But after I get it into a large cvs file, I need to be able to look at each row and see if it falls within a specific IP ranges. IP Ranges: 162.x.x.x 151.130.x.x 145.x.x.x These are just some examples of the IP ranges. The csv file has data like below: 63.145.40.32 Gnutella File Search 14 5/15/2009 0:48 151.40.133.25 Gnutella File Search 14 5/14/2009 16:21 145.133.19.147 BitTorrent Client Activity 13 5/14/2009 19:20 Here is my source code so far: import sys import win32api import win32ui import shutil import string import os import os.path import csv #Global Variables P2Pfiles = [] #still in the development process -- where to get the files from #right now the location is C:\P2P def getp2preportdestion(): win32ui.MessageBox('Welcome to P2P Reporting.\nThis script is designed to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in C:\P2P \n\nWith no subdirectories.\n\n\n\n\nVersion 1.0 - \n\nPress "OK" to continue with this script.') #p2pdrive=raw_input("Enter the drive letter: ") #p2pdir=raw_input("Enter the directory name: ") #p2preport = p2pdrive +':\\'+p2pdir+'/' p2preport = 'C://P2P\\' return p2preport #Main Program #Get location of directories p2ploc = getp2preportdestion() #Checking to make sure directory is there. if os.path.exists(p2ploc): if os.path.isfile(p2ploc +'/p2pmerge.csv'): win32ui.MessageBox('Merge File exists..will continue with program') else: win32ui.MessageBox('Merge File does not exists, script will exit. Please run the perl script first.') exit() else: win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease create and copy all the files there.\nThen re-run this script') exit() reader = csv.reader(open('C://P2P/p2pmerge.csv', "rb")) p2pwriter = csv.writer(open('C://P2P\P2PMerge4.csv', "wb")) for row in reader: p2pwriter.writerow([row]) win32ui.MessageBox('I am here') Any assistance will be greatly appricated!!! Thanx, -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Mon May 25 15:14:33 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 25 May 2009 09:14:33 -0400 Subject: [Tutor] Search for hex data in file In-Reply-To: References: Message-ID: <1c2a2c590905250614l1d90331dm11fc478368f12a67@mail.gmail.com> On Mon, May 25, 2009 at 2:32 AM, xbmuncher wrote: > I've been reading about ways to convert strings and what not to hex and back > and forth. I'm looking for the fastest and least memory intensive way to > search through a file for a hex value and gets its byte offset in the file. > This hex value (that I'm looking for in the file) is of course a hex > representation of the binary data and its 8 bytes long. > I figured reading the whole file and converting it to hex from ascii..etc.. > would be overkill, especially if its a large file. > What do you guys recommend? I want to search the file for certain hex value > and get the byte offset within the file. Convert your hex string to a string containing the binary values. Then you can read in the whole file to a string and use the find() method to get the offset. Kent From kent37 at tds.net Mon May 25 15:19:50 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 25 May 2009 09:19:50 -0400 Subject: [Tutor] ????? In-Reply-To: <4A1A9253.5010603@compuscan.co.za> References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> <4A1A9253.5010603@compuscan.co.za> Message-ID: <1c2a2c590905250619x442c1592j8b6f32774a586174@mail.gmail.com> On Mon, May 25, 2009 at 8:42 AM, Christian Witts wrote: > prasad rao wrote: >> >> hello >> ? ? ?I get a problem while trying to print non callable >> items in dir(module).May be as the items are strings. >> >> for x in dir(sys): >> ?? if sys.x is callable: >> ???? ?print sys.x() >> ?? else:print sys.x >> >> >> Traceback (most recent call last): >> ?File "", line 2, in >> ? ?if sys.x is callable: >> AttributeError: 'module' object has no attribute 'x' > > What you should be doing is rather > > for attrib in dir(sys): > ? if callable('sys.%s' % attrib): > ? ? ? print 'Callable: sys.%s' % attrib > ? else: > ? ? ? print 'Not Callable: sys.%s' % attrib No; 'sys.%s' % attrib is a string, you are just asking if a string is callable. The correct way to get an attribute whose name you have is to use getattr(): if callable(getattr(sys, x)): Kent From alan.gauld at btinternet.com Mon May 25 16:06:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 May 2009 15:06:02 +0100 Subject: [Tutor] ????? References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> Message-ID: "prasad rao" wrote > for x in dir(sys): > ?? if sys.x is callable: Even if you use the other suggestions to prove its callable that doesn't mean you can do this: > ???? print sys.x() If the callable takes parameters then this will fail. You need to know quite a lot more about what you are calling and that is a non trivial task in the general case. Alan G From a.t.hofkamp at tue.nl Mon May 25 16:31:08 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 25 May 2009 16:31:08 +0200 Subject: [Tutor] ????? In-Reply-To: References: <9e3fac840905250524g21dd838blf07450d033d743f0@mail.gmail.com> Message-ID: <4A1AABAC.1090409@tue.nl> Alan Gauld wrote: > Even if you use the other suggestions to prove its > callable that doesn't mean you can do this: > >> ???? print sys.x() > > If the callable takes parameters then this will fail. > You need to know quite a lot more about what you > are calling and that is a non trivial task in the general case. Not to mention it is inherently dangerous to call arbitrary functions without understanding what they are doing. For example, you may lose your entire home directory. (shutil.rmtree can do that with just one call) Albert From davea at ieee.org Mon May 25 16:56:10 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 25 May 2009 10:56:10 -0400 Subject: [Tutor] ????? In-Reply-To: References: Message-ID: <4A1AB18A.8060307@ieee.org> prasad rao wrote: > Hello > It is printing wether the item is callable or not. > I want it to print > sys.getdefaultencoding > ASCII > sys.getfilesystemencoding > mbcs > > Like that > > Prasad > But when you get to sys.exit(), you'll be terminating your test. In general, it's not safe to just run through a list of functions, calling them all with default arguments. You never know what side effects may occur. Others that'll fail include sys.getrefcount(), sys.getsizeof(), sys.setcheckinterval(). They have at least one required parameter, as do many more of the setXXXX functions. So, ignoring callable functions (which shouldn't be called unless you've read the docs), try the following: import sys for attrib in dir(sys): if callable(sys.__getattribute__(attrib)): print 'Callable: sys.%s' % attrib else: print 'Not Callable: sys.%s = %s' % (attrib, getattr(sys, attrib)) Note that sometimes the output is multiline, and quite long - see sys.__doc__ for example. From reed at reedobrien.com Mon May 25 17:05:39 2009 From: reed at reedobrien.com (Reed O'Brien) Date: Mon, 25 May 2009 11:05:39 -0400 Subject: [Tutor] IP-range In-Reply-To: References: Message-ID: On May 24, 2009, at 11:18 PM, Paras K. wrote: > Hello, > > I came across your answer / assistance on the IP range. I recommend looking at the ipaddr library: http://svn.python.org/projects/python/trunk/Lib/ipaddr.py There is some Sphinx documentation with the python 2.7 docs and some on the original google code page. > I am fairly new to the python world of programming. However, up to > this point I have always been able to get my programs to work by > reading the books or following the guides I find through google.com > > Here is what I have to do: > > I have to read a number of cvs files into 1 large file. (I have been > able to get that done through a perl script). But after I get it > into a large cvs file, I need to be able to look at each row and see > if it falls within a specific IP ranges. > > IP Ranges: > > 162.x.x.x > 151.130.x.x > 145.x.x.x > > These are just some examples of the IP ranges. > > The csv file has data like below: > > 63.145.40.32 > Gnutella File Search > 14 > 5/15/2009 0:48 > 151.40.133.25 > Gnutella File Search > 14 > 5/14/2009 16:21 > 145.133.19.147 > BitTorrent Client Activity > 13 > 5/14/2009 19:20 > > Here is my source code so far: > > import sys > import win32api > import win32ui > import shutil > import string > import os > import os.path > import csv > > #Global Variables > P2Pfiles = [] > > > #still in the development process -- where to get the files from > #right now the location is C:\P2P > def getp2preportdestion(): > win32ui.MessageBox('Welcome to P2P Reporting.\nThis script is > designed to aid in the P2P reporting. \n\nThe locations of P2P > Reports should be in C:\P2P \n\nWith no subdirectories.\n\n\n\n > \nVersion 1.0 - \n\nPress "OK" to continue with this script.') > #p2pdrive=raw_input("Enter the drive letter: ") > #p2pdir=raw_input("Enter the directory name: ") > #p2preport = p2pdrive +':\\'+p2pdir+'/' > p2preport = 'C://P2P\\' > return p2preport > > > > > > #Main Program > > #Get location of directories > p2ploc = getp2preportdestion() > > #Checking to make sure directory is there. > if os.path.exists(p2ploc): > if os.path.isfile(p2ploc +'/p2pmerge.csv'): > win32ui.MessageBox('Merge File exists..will continue with > program') > else: > win32ui.MessageBox('Merge File does not exists, script will > exit. Please run the perl script first.') > exit() > else: > win32ui.MessageBox('The C:\P2P directory does not exists.\n > \nPlease create and copy all the files there.\nThen re-run this > script') > exit() > > reader = csv.reader(open('C://P2P/p2pmerge.csv', "rb")) > p2pwriter = csv.writer(open('C://P2P\P2PMerge4.csv', "wb")) > for row in reader: > p2pwriter.writerow([row]) > > win32ui.MessageBox('I am here') > > > Any assistance will be greatly appricated!!! > > Thanx, > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From srilyk at gmail.com Mon May 25 17:39:46 2009 From: srilyk at gmail.com (W W) Date: Mon, 25 May 2009 10:39:46 -0500 Subject: [Tutor] IP-range In-Reply-To: References: Message-ID: <333efb450905250839o2697b9dfxb81c8286333d9dd3@mail.gmail.com> On Sun, May 24, 2009 at 10:18 PM, Paras K. wrote: > Hello, > > I came across your answer / assistance on the IP range. I am fairly new to > the python world of programming. However, up to this point I have always > been able to get my programs to work by reading the books or following the > guides I find through google.com > > Here is what I have to do: > > I have to read a number of cvs files into 1 large file. (I have been able > to get that done through a perl script). But after I get it into a large cvs > file, I need to be able to look at each row and see if it falls within a > specific IP ranges. > > IP Ranges: > > 162.x.x.x > 151.130.x.x > 145.x.x.x > > These are just some examples of the IP ranges. > > The csv file has data like below: > 63.145.40.32 Gnutella File Search 14 5/15/2009 0:48 151.40.133.25 Gnutella > File Search 14 5/14/2009 16:21 145.133.19.147 BitTorrent Client Activity > 13 5/14/2009 19:20 > You know, unless you have an ulterior reason for merging all the files, you could probably just read each of them with a loop in python. Or use python to build the main file - which would eliminate your need for that error check. Assuming that the first element in the line will always be the IP, this should help: In [12]: myline = '192.168.1.1 Gnutella File Search 24 5/15/2009 0:48' In [15]: ip = myline.split(' ')[0] In [16]: split_ip = ip.split('.') In [17]: split_ip Out[17]: ['192', '168', '1', '1'] HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Mon May 25 18:22:50 2009 From: srilyk at gmail.com (W W) Date: Mon, 25 May 2009 11:22:50 -0500 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: <1618520905250157l4919be87u561c6a354f069bb8@mail.gmail.com> References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> <1618520905250157l4919be87u561c6a354f069bb8@mail.gmail.com> Message-ID: <333efb450905250922l46c6b18by648dea23dd8a7100@mail.gmail.com> On Mon, May 25, 2009 at 3:57 AM, Michael Bernhard Arp S?rensen < michael at arpsorensen.dk> wrote: > > I just wished that Python was upgraded in the distros. If I want to play > with Python3, I will need to compile it my self and specify which Python > interpretor to use in each Python file. But this is a small problem. > In Ubuntu Jaunty(?)- 8.11 I believe- you can apt-get the python3 package (or whatever you use). I think there's also a python 2.6 package and the default is python 2.5. I'm assuming you use terminal, and if so you can setup aliases in your .bashrc to go into whatever "mode" you want. Just add these lines to your .bashrc alias pymode3='alias python="python3"' alias pymode1.6='alias python="python1.6"' I forget if/how to change ipython between versions of python though. You'd just have to put similar aliases in your bash file. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From walker.hale.iv at gmail.com Mon May 25 18:30:08 2009 From: walker.hale.iv at gmail.com (Walker Hale IV) Date: Mon, 25 May 2009 11:30:08 -0500 Subject: [Tutor] Search for hex data in file In-Reply-To: References: Message-ID: <312f43620905250930k1edca33es45e72620f0b48b91@mail.gmail.com> On Mon, May 25, 2009 at 1:32 AM, xbmuncher wrote: > I've been reading about ways to convert strings and what not to hex and back > and forth. I'm looking for the fastest and least memory intensive way to > search through a file for a hex value and gets its byte offset in the file. > This hex value (that I'm looking for in the file) is of course a hex > representation of the binary data and its 8 bytes long. > I figured reading the whole file and converting it to hex from ascii..etc.. > would be overkill, especially if its a large file. > What do you guys recommend? I want to search the file for certain hex value > and get the byte offset within the file. > > Thanks for reading. If the input is a text file containing hex strings, then: * compile a regular expression for a hex string using the re module * initialize a byte count for previous lines to 0 * read the input file line by line ** search each line for the compiled regular expression *** for each hit found **** output a file offset based on the byte count and offset within the line ** add the length of the line to a running total of the number of bytes in the previous lines Is that what you are looking for? -- Walker Hale From xboxmuncher at gmail.com Mon May 25 19:48:43 2009 From: xboxmuncher at gmail.com (xbmuncher) Date: Mon, 25 May 2009 13:48:43 -0400 Subject: [Tutor] Can't print a string, gives me syntax error Message-ID: I ran this in IDLE: >>> t = 'hi' >>> print t SyntaxError: invalid syntax (, line 1) I've also tried this as sample.py : import string text = 'hello world' print text It gives me a syntax error on "print text" line What's going on? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Mon May 25 19:58:37 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 25 May 2009 19:58:37 +0200 Subject: [Tutor] Can't print a string, gives me syntax error In-Reply-To: References: Message-ID: 2009/5/25 xbmuncher : > I ran this in IDLE: >>>> t = 'hi' >>>> print t > SyntaxError: invalid syntax (, line 1) What version of python are you using (I suspect version 3)? > I've also tried this as sample.py : > import string > text = 'hello world' > print text If you are using python 3 then the print statement has been replaced by the print function. Try: print(text) Greets Sander From rnrcreid at yahoo.com Mon May 25 19:56:15 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Mon, 25 May 2009 10:56:15 -0700 (PDT) Subject: [Tutor] Can't print a string, gives me syntax error Message-ID: <685837.32072.qm@web45716.mail.sp1.yahoo.com> --- On Mon, 5/25/09, xbmuncher wrote: From: xbmuncher Subject: [Tutor] Can't print a string, gives me syntax error To: "tutor at python.org" Date: Monday, May 25, 2009, 5:48 PM I ran this in IDLE: >>> t = 'hi' >>> print t SyntaxError: invalid syntax (, line 1) I've also tried this as sample.py : import string text = 'hello world' print text It gives me a syntax error on "print text" line What's going on? I'm guessing, though I'm not that experienced with Python yet, you maybe using Python 3.0.? In this new version, you have to include () with print statements.? You didn't have to in 2.X versions.? So try: ? text='Hello Wold' print (text) ? That's my guess, if you are using Python 2.x then I don't know:) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mysterytramp at yahoo.com Mon May 25 22:18:01 2009 From: mysterytramp at yahoo.com (M Tramp) Date: Mon, 25 May 2009 13:18:01 -0700 (PDT) Subject: [Tutor] A few very basic questions Message-ID: <415359.69657.qm@web57208.mail.re3.yahoo.com> Alan Gauld btinternet.com> said: >>> Have you checked fink? There's a MacOS Gimp toolkit package available. (Well 3 actually, gtk, gtk-data and gtk-shlibs) I generally use fink rather than try apt-get because I know the fink stuff is tested against MacOS. And fink packages are pre-built usually... <<< I had tried Fink, but was frustrated early and abandoned it. I gave it a second go, and it seemed to work great. I was able to find and download the packages I needed. Fink appeared to be installing them fine with little interaction from me. I ran Eclipse again, but it still burps on: import gtk saying: "No module named gtk" I've tried adding paths to external libraries using the PYTHONPATH in the properties window. That didn't seem to help. I tried variations, searching my hard disk for gtk. Found several likely binary candidates, but the paths didn't seem to help. Each time, I get the same error. I'm definitely missing something. Sorry I'm acting like such an idiot mt From walker.hale.iv at gmail.com Mon May 25 22:47:46 2009 From: walker.hale.iv at gmail.com (Walker Hale IV) Date: Mon, 25 May 2009 15:47:46 -0500 Subject: [Tutor] A few very basic questions In-Reply-To: <228072.40110.qm@web57207.mail.re3.yahoo.com> References: <228072.40110.qm@web57207.mail.re3.yahoo.com> Message-ID: <312f43620905251347y4c437208jd526ac2fbd558e9b@mail.gmail.com> On Sun, May 24, 2009 at 1:08 PM, M Tramp wrote: > > Hello all ... > > I've started trying ot learn about Python because I'm intrigued by what I've read about Gramps, a genealogical data manager. To run Gramps on a Mac, I need to get GTK up and running. Here's where I have a few, hopefully basic, questions. > > (FYI, I have a Mac, running OS X 10.4.11. Xcode 2.5) > > 1) GTK has several dependencies, according to http://library.gnome.org/devel/gtk-faq/stable/c192.html > ... pkg-config; GNU make; JPEG, PNG and TIFF image libraries; FreeType; fontconfig; GNU libiconv library; GNU gettext; GLib; Pango; ATK. Each of these may have dependencies as well. Is there someplace to go to figure out the order they should be compiled? (I started with pkg-config, but it needs GLib) Possibly the best answer is to download the source for each and just start compiling? > > 2) Once each library is compiled, what should I have and where should it be on my hard drive? For example, when Python sees "import gtk" in which directory is it looking for GTK? How will GTK and the other libraries appear on my hard disk? As Unix executables or as frameworks or as something else entirely? > > 3) I am likely to do my Python programming inside Eclipse using pydev, though I have Xcode. The libraries will be in C, correct? Will I be able to use Xcode to compile GTK and the rest, and then access the libraries inside Eclipse? > > mt MacPorts is (like fink) a system for automatically downloading, configuring, and compiling software. Gramps is included. http://www.macports.org/ports.php?by=name&substr=gramps -- Walker Hale From bgailer at gmail.com Tue May 26 00:00:26 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 25 May 2009 18:00:26 -0400 Subject: [Tutor] serious problem with graphics module In-Reply-To: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> References: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> Message-ID: <4A1B14FA.2060004@gmail.com> roberto wrote: > hello everyone > i have a problem with python 3.0 graphics module: > > no problem while importing the module > >>>> import turtle >>>> > > but when i issue any command like: > >>>> t = turtle.pen() >>>> t = turtle.forward(60) >>>> Your code fails for me at turtle.pen AttributeError: 'module' object has no attribute 'pen' If I replace pen with Pen then it works as desired. > > nothing appears on the screen, only a blank window, with nothing inside; > may you give me some hint ? > > > OS: win Xp > > thank you in advance ! > > -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Tue May 26 00:32:30 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 May 2009 23:32:30 +0100 Subject: [Tutor] A few very basic questions References: <415359.69657.qm@web57208.mail.re3.yahoo.com> Message-ID: "M Tramp" wrote > the packages I needed. Fink appeared to be installing them fine with > little > interaction from me. > > I ran Eclipse again, but it still burps on: > > import gtk Have you installed pygtk? You need both pygtk and gtk. The former uses the latter. Alan G From alan.gauld at btinternet.com Tue May 26 00:35:38 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 May 2009 23:35:38 +0100 Subject: [Tutor] Can't print a string, gives me syntax error References: Message-ID: "xbmuncher" wrote >I ran this in IDLE: >>>> t = 'hi' >>>> print t > SyntaxError: invalid syntax (, line 1) > > I've also tried this as sample.py : > import string > text = 'hello world' > print text > > > It gives me a syntax error on "print text" line Looks like you are using Python v3. Most tutorials are still on v2. print has changed significantly in v3, several things are done differently. Try to find a v3 tutorial. The basics section in mine is almost complete(see .sig) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From gregor.lingl at aon.at Tue May 26 00:45:12 2009 From: gregor.lingl at aon.at (Gregor Lingl) Date: Tue, 26 May 2009 00:45:12 +0200 Subject: [Tutor] serious problem with graphics module In-Reply-To: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> References: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> Message-ID: <4A1B1F78.9070603@aon.at> roberto schrieb: > hello everyone > i have a problem with python 3.0 graphics module: > > no problem while importing the module > >>>> import turtle >>>> > > but when i issue any command like: > >>>> t = turtle.pen() >>>> t = turtle.forward(60) >>>> > > nothing appears on the screen, only a blank window, with nothing inside; > may you give me some hint ? > I know one situation in which can happen what you describe: If you use IDLE, issue the above commands and your Idle-shell-window covers the graphics window - then you have to move the shell window in order to see what's on the graphics window. Alas, in this situation the graphics window may get nonresponsive (to the Windows-event-queue) and may not be refreshed. So it remains apparentl empty. You may also experience problems when trying to close the window (because of the same reasons). *Remedy*: if you use the turtle module interactively, you have to use Idle with the -n flag, which prevents it from using subprocesses for executing scripts. I recommend to prepare a special shortcut on your desktop, something like this: C:\Python30\pythonw.exe C:\Python30\Lib\idlelib\idle.pyw -n When you fire up Idle with this link you will get the text: ==== No Subprocess ==== above your first input prompt. Now you can enter your commands and everything should work fine. hth Gregor > > OS: win Xp > > thank you in advance ! > > From gregor.lingl at aon.at Tue May 26 00:33:21 2009 From: gregor.lingl at aon.at (Gregor Lingl) Date: Tue, 26 May 2009 00:33:21 +0200 Subject: [Tutor] serious problem with graphics module In-Reply-To: <4A1B14FA.2060004@gmail.com> References: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> <4A1B14FA.2060004@gmail.com> Message-ID: <4A1B1CB1.7070106@aon.at> bob gailer schrieb: > roberto wrote: >> hello everyone >> i have a problem with python 3.0 graphics module: >> >> no problem while importing the module >> >>>>> import turtle >>>>> >> >> but when i issue any command like: >> >>>>> t = turtle.pen() >>>>> t = turtle.forward(60) >>>>> > > Your code fails for me at turtle.pen AttributeError: 'module' object > has no attribute 'pen' Do you use Python 3.0? >>> import turtle >>> turtle.pen >>> > If I replace pen with Pen then it works as desired. Then you use the Pen-class to create a Turtle t. Then you have to call the forward method for this turtle: >>> import turtle >>> t = turtle.Pen() >>> t.forward(60) >>> Regards, Gregor > From gregor.lingl at aon.at Tue May 26 00:28:31 2009 From: gregor.lingl at aon.at (Gregor Lingl) Date: Tue, 26 May 2009 00:28:31 +0200 Subject: [Tutor] serious problem with graphics module In-Reply-To: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> References: <4bcde3e10905241403t3356c053m31b5964365860f0f@mail.gmail.com> Message-ID: <4A1B1B8F.5060504@aon.at> roberto schrieb: > hello everyone > Hello Roberto, First question: Do you use Python from the command line (terminal) or do you use IDLE? I ask this, because these two show different behaviour. > i have a problem with python 3.0 graphics module: > > no problem while importing the module > >>>> import turtle >>>> > > but when i issue any command like: > >>>> t = turtle.pen() >>>> After performing this command you should be able to observe a blank window with a tiny black arrow - it's the turtle pen() is a function in the module turtle. It returns a dictionary of the attributes of the turtle's pen. Have a look at it: >>> print(t) {'pensize': 1, 'resizemode': 'noresize', 'pendown': True, 'fillcolor': 'black', 'speed': 3, 'shown': True, 'outline': 1, 'tilt': 0.0, 'pencolor': 'black', 'stretchfactor': (1.0, 1.0), 'shearfactor': 0.0} >>> >>>> t = turtle.forward(60) >>>> > > nothing appears on the screen, only a blank window, with nothing inside; > may you give me some hint ? > Hmm, in my window the turtle goes 60 pixels to the right and draws a black line. The statement >>> t = turtle.forward(60) doesn't make much sense, though. It calls the forward() function of the turtle module and names it's return value t. Alas: >>> print(t) None >>> Not really useful. >>> turtle.forward(60) is sufficient. What happens if you do this? >>> from turtle import * >>> for i in range(4): forward(100) left(90) Regards, Gregor > > OS: win Xp > > thank you in advance ! > > From phpfood at gmail.com Mon May 25 19:46:33 2009 From: phpfood at gmail.com (phpfood) Date: Mon, 25 May 2009 13:46:33 -0400 Subject: [Tutor] Can't print a string, gives me syntax error Message-ID: I ran this in IDLE: >>> t = 'hi' >>> print t SyntaxError: invalid syntax (, line 1) I've also tried this as sample.py : import string text = 'hello world' print text It gives me a syntax error on "print text" line What's going on? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rnrcreid at yahoo.com Tue May 26 02:45:39 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Mon, 25 May 2009 17:45:39 -0700 (PDT) Subject: [Tutor] Tinkering with Tkinter Message-ID: <775842.47937.qm@web45713.mail.sp1.yahoo.com> The following code and it's explanation doesn't seem to work: ? 1. >>> from Tkinter import * 2. >>> tk = Tk() 3. >>> btn = Button(tk, text="click me") 4. >>> btn.pack() ? In line 1, we import the contents of the Tk module, so we can use them?the most useful of these is Tk, which creates a basic window to which we can then add things. After you type in line 2, that window will suddenly appear on the screen. In line 3, we create a new Button and assign it to the variable btn. The button is created by passing the tk object as a parameter, along with a named parameter with the words ?click me?. ? Nothing appears on the screen after I enter line 2...I can type in the code in the editor and double click after saving the file and it does, but not from the shell, how can that be fixed or am I doing something wrong? ? Thanks ? Doug -------------- next part -------------- An HTML attachment was scrubbed... URL: From mysterytramp at yahoo.com Tue May 26 03:57:25 2009 From: mysterytramp at yahoo.com (M Tramp) Date: Mon, 25 May 2009 18:57:25 -0700 (PDT) Subject: [Tutor] A few very basic questions In-Reply-To: References: Message-ID: <584256.29750.qm@web57202.mail.re3.yahoo.com> Alan Gauld: >>> Have you installed pygtk? You need both pygtk and gtk. The former uses the latter. Alan G <<< Yes. It is. I went back and installed more. Fink Commander lists 35 files with "gtk" -- five already were. (gtk+, gtk+-data, gtk+-shlibs, pygtk and pygtk-py23) I went back and found gtk+2 (including -dev and -shlibs). The source file has a line about importing "glade" from gtk so I finkcommandered those files too. Still I get the error: "No module named gtk" So I'm back to -- or nearly so -- my original question. Where on my hard drive should these libraries reside? I've downloaded hundreds of megabytes by now; I'm suspecting this is a path problem within Eclipse, but the project's properties have paths to all the prime suspects. mt From dcml1 at crouse.us Tue May 26 06:36:20 2009 From: dcml1 at crouse.us (Dave Crouse) Date: Mon, 25 May 2009 23:36:20 -0500 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: <333efb450905250922l46c6b18by648dea23dd8a7100@mail.gmail.com> References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> <1618520905250157l4919be87u561c6a354f069bb8@mail.gmail.com> <333efb450905250922l46c6b18by648dea23dd8a7100@mail.gmail.com> Message-ID: <596da3790905252136s4ef5eb88wa21adaff355af38a@mail.gmail.com> It's not a newbie distro by any means, but i am very fond of Arch Linux. It has 3 versions of python available. Currently, python24 2.4.6-1 python 2.6.2-1 python3 3.0.1-1 are all very simple to install in Arch. I have 2.6 and 3.0 both installed. [crouse at Veronica ~]$ python --version Python 2.6.2 [crouse at Veronica ~]$ python3 --version Python 3.0.1 [crouse at Veronica ~]$ installing python3 is as simple as : pacman -S python3 Once configured and setup, Arch provides an awesome platform for python. I am a bit biased though :) ymmv On Mon, May 25, 2009 at 11:22 AM, W W wrote: > On Mon, May 25, 2009 at 3:57 AM, Michael Bernhard Arp S?rensen > wrote: >> >> >> I just wished that Python was upgraded in the distros. If I want to play >> with Python3, I will need to compile it my self and specify which Python >> interpretor to use in each Python file. But this is a small problem. > > In Ubuntu Jaunty(?)- 8.11 I believe- you can apt-get the python3 package (or > whatever you use). I think there's also a python 2.6 package and the default > is python 2.5. > > I'm assuming you use terminal, and if so you can setup aliases in your > .bashrc to go into whatever "mode" you want. Just add these lines to your > .bashrc > > alias pymode3='alias python="python3"' > alias pymode1.6='alias python="python1.6"' > > I forget if/how to change ipython between versions of python though. You'd > just have to put similar aliases in your bash file. > > HTH, > Wayne > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From eduardo.susan at gmail.com Tue May 26 07:48:35 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Mon, 25 May 2009 23:48:35 -0600 Subject: [Tutor] Parsing Bible verses In-Reply-To: <8633E44E09394855BC66724793935E49@kisc.edu.np> References: <8633E44E09394855BC66724793935E49@kisc.edu.np> Message-ID: <9356b9f30905252248m43541578m78f0bb7a998ba7a6@mail.gmail.com> On Sat, May 23, 2009 at 3:37 AM, C or L Smith wrote: > Here is something from my toolbox of routines that might be useful for the number ranges: > >>>> indices('-5--2') > [-5, -4, -3, -2] >>>> indices('3-4') > [3, 4] >>>> indices('3-4,10') > [3, 4, 10] > > /chris > > def indices(s,n=None): #("1-3,7")->1,2,3,7;i("1,-3--1")->1,-3,-2,-1; or (slc,n=None)->slc.start,stop,step [for range(n)] > ? ?"""Return a list of indices as defined by a MSWord print dialog-like range: > > ? ?e.g. "1,3,5-7" -> [1, 3, 5, 6, 7] > > ? ?A trailing comma will be ignored; a trailing dash will generate an error.""" > > ? ?# ranges must be increasing: -3--4 will not generate any numbers > ? ?assert type(s) is str > ? ?r=[x.strip() for x in s.split(',')] > ? ?rv = [] > ? ?for ri in r: > ? ? ? ?if not ri: continue > ? ? ? ?if ri.find('-',1)>0: #ignore - in first position > ? ? ? ? ? ?dashat = ri.find('-',1) #start searching at position 1 > ? ? ? ? ? ?nums = ri[:dashat],ri[dashat+1:] > ? ? ? ? ? ?#one might want to use sys.maxint-1 for stop if the '-' is encountered, the > ? ? ? ? ? ?#meaning being "from start to the end (as defined by the code elsewhere") > ? ? ? ? ? ?#but then this should be made into an iterator rather than generating the > ? ? ? ? ? ?#whole list > ? ? ? ? ? ?if nums[1] in ['','-']: > ? ? ? ? ? ? ? ?raise ValueError('missing number in request to indices: %s'%ri) > ? ? ? ? ? ?start, stop = [int(x.strip()) for x in nums] > ? ? ? ? ? ?for i in xrange(start, stop+1): > ? ? ? ? ? ? ? ?rv.append(i)#yield i > ? ? ? ?else: > ? ? ? ? ? ?rv.append(int(ri))#yield int(ri) > ? ?return rv > Thank you, your examples does give me some good ideas. I still want to investigate better the pyparsing solution, tho. One thing I came up with, was a way to parse and transform a list of verses, adding the book name where it was missing. This is a first step into the bigger program. Here's the code I came up with: #===== import re reference = 'Luke 1:25; 2:1-5, 8; 4:23; 1 Corinthians 2:24; 3:1-10; Salm 23' def addbook(ref): parseref = re.split(r'; *', ref) for i, refe in enumerate(parseref): if refe[0].isalpha() or refe[1].isspace(): book = refe.rsplit(' ', 1) elif refe[0].isdigit() and refe[1]: vers = parseref.pop(i) parseref.insert(i, book[0] + ' ' + vers) return parseref print addbook(reference) #========== This will give me this result: ['Luke 1:25', 'Luke 2:1-5, 8', 'Luke 4:23', '1 Corinthians 2:24', '1 Corinthians 3:1-10', 'Salm 23'] Now, a little farther on the topic of a Bible database. I'm not sure how I should proceed. I don't really have the db file I need, I will have to generate it somehow, from a bible software, because the version I want is for Portuguese. I have found a bible in sql, a bible in MS Access to give me some ideas on how to structure my database. But my question is do I really need a sql database for my need, since I will be only reading from it? Never adding or updating. One like sqlite. Would a pickled dictionary from Bible_reference to verse be faster? Should I work with anydbm? Thanks for your knowledge and help Eduardo From john at fouhy.net Tue May 26 07:55:02 2009 From: john at fouhy.net (John Fouhy) Date: Tue, 26 May 2009 17:55:02 +1200 Subject: [Tutor] Parsing Bible verses In-Reply-To: <9356b9f30905252248m43541578m78f0bb7a998ba7a6@mail.gmail.com> References: <8633E44E09394855BC66724793935E49@kisc.edu.np> <9356b9f30905252248m43541578m78f0bb7a998ba7a6@mail.gmail.com> Message-ID: <5e58f2e40905252255x31c20568uf6ebd1687e294fd7@mail.gmail.com> 2009/5/26 Eduardo Vieira : > Now, a little farther on the topic of a Bible database. I'm not sure > how I should proceed. I don't really have the db file I need, I will > have to generate it somehow, from a bible software, because the > version I want is for Portuguese. I have found a bible in sql, a bible > in MS Access to give me some ideas on how to structure my database. > But my question is do I really need a sql database for my need, since > I will be only reading from it? Never adding or updating. One like > sqlite. Would a pickled dictionary from Bible_reference to verse be > faster? Should I work with anydbm? If you don't want to use a database, you should probably use the shelve module. However, there is really no particular reason not to use a relational database. It seems a solution fairly well suited to your problem. Python has a database supplied these days: sqlite3. I suggest using that, rather than MS Access. From alan.gauld at btinternet.com Tue May 26 09:41:46 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 May 2009 08:41:46 +0100 Subject: [Tutor] A few very basic questions References: <584256.29750.qm@web57202.mail.re3.yahoo.com> Message-ID: "M Tramp" wrote > You need both pygtk and gtk. > The former uses the latter. > > Yes. It is. I went back and installed more. > Still I get the error: "No module named gtk" > > So I'm back to -- or nearly so -- my original question. > Where on my hard drive should these libraries reside? > I'm suspecting this is a path problem within Eclipse, but the > project's properties have paths to all the prime suspects. I'd be surprised if its a problem with Eclipse, but that's easy to prove. Just run the code outside Eclipse from the Terminal app. If you get the same error then it cannot be Eclipse at fault. Can you post the actual error text? If it is saying no module gtk that suggests to me the fault lies in your pygtk installation rather than in GTK itself. How/Where did you install pygtk? Alan G. From alan.gauld at btinternet.com Tue May 26 09:51:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 May 2009 08:51:36 +0100 Subject: [Tutor] Tinkering with Tkinter References: <775842.47937.qm@web45713.mail.sp1.yahoo.com> Message-ID: "Doug Reid" wrote > The following code and it's explanation doesn't seem to work: > 1. >>> from Tkinter import * 2. >>> tk = Tk() 3. >>> btn = Button(tk, text="click me") 4. >>> btn.pack() > > In line 1, ..... > Nothing appears on the screen after I enter line 2... A lot depends on the environment for Tk. It works slightly differently on different operating systems and with different IDEs. It is even possible that you wouldn't see any windows after line 4. You would need to add line 5: >>> tk.mainloop() before anything happens. Don't worry too much, just enter the code and run it. If you do want to use the >>> prompt to experiment use an OS command line window rather than an IDE. In pratice when working with Tkinter I've leaned not to trust any IDE but just use it as a text editor. I type the code, save it and then run it from the OS directly. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue May 26 10:11:12 2009 From: denis.spir at free.fr (spir) Date: Tue, 26 May 2009 10:11:12 +0200 Subject: [Tutor] import aliases ? Message-ID: <20090526101112.5d6605d5@o> Hello, In the case of such a dir structure: /pack __init__.py (modules) /pack1 __init__.py modules are the following imports synonyms: 1- import pack.pack1 2- from pack import pack1 ? If yes, is there a reason for this? Also, there is something I find weird: * Using idiom 1, pack1 is only known as the compound name "pack.pack1": I cannot eg "print pack1". Consistently, to import a module, I need "from pack.pack1 import mod". * Using idiom 2, pack1 is known as "pack1" alone: I can "print pack1", which outputs "". But for further imports, I still need to use the compound name "pack.pack1": "from pack1 import mod" raises "ImportError: No module named pack1", while python has just accepted to print pack1! Denis ------ la vita e estrany From norman at khine.net Tue May 26 10:20:03 2009 From: norman at khine.net (Norman Khine) Date: Tue, 26 May 2009 10:20:03 +0200 Subject: [Tutor] Find a good linux distribution with python. In-Reply-To: <596da3790905252136s4ef5eb88wa21adaff355af38a@mail.gmail.com> References: <1618520905241302r57b3d07cqb0fd46f9dd19fb3b@mail.gmail.com> <1618520905250157l4919be87u561c6a354f069bb8@mail.gmail.com> <333efb450905250922l46c6b18by648dea23dd8a7100@mail.gmail.com> <596da3790905252136s4ef5eb88wa21adaff355af38a@mail.gmail.com> Message-ID: <9c2c8ffb0905260120k14a90750he8fa75943b126872@mail.gmail.com> Gentoo is python centric, although python3 is not yet in the main portage tree, but can be setup using an overlay http://overlays.gentoo.org/proj/python/ On Tue, May 26, 2009 at 6:36 AM, Dave Crouse wrote: > It's not a newbie distro by any means, but i am very fond of Arch Linux. > It has 3 versions of python available. Currently, > > python24 2.4.6-1 > python 2.6.2-1 > python3 3.0.1-1 > > are all very simple to install in Arch. I have 2.6 and 3.0 both installed. > > [crouse at Veronica ~]$ python --version > Python 2.6.2 > [crouse at Veronica ~]$ python3 --version > Python 3.0.1 > [crouse at Veronica ~]$ > > installing python3 is as simple as : ?pacman -S python3 > > Once configured and setup, Arch provides an awesome platform for python. > I am a bit biased though :) ? ymmv > > > > > > > > > On Mon, May 25, 2009 at 11:22 AM, W W wrote: >> On Mon, May 25, 2009 at 3:57 AM, Michael Bernhard Arp S?rensen >> wrote: >>> >>> >>> I just wished that Python was upgraded in the distros. If I want to play >>> with Python3, I will need to compile it my self and specify which Python >>> interpretor to use in each Python file. But this is a small problem. >> >> In Ubuntu Jaunty(?)- 8.11 I believe- you can apt-get the python3 package (or >> whatever you use). I think there's also a python 2.6 package and the default >> is python 2.5. >> >> I'm assuming you use terminal, and if so you can setup aliases in your >> .bashrc to go into whatever "mode" you want. Just add these lines to your >> .bashrc >> >> alias pymode3='alias python="python3"' >> alias pymode1.6='alias python="python1.6"' >> >> I forget if/how to change ipython between versions of python though. You'd >> just have to put similar aliases in your bash file. >> >> HTH, >> Wayne >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From jeremiah.dodds at gmail.com Tue May 26 11:17:40 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Tue, 26 May 2009 10:17:40 +0100 Subject: [Tutor] Can't print a string, gives me syntax error In-Reply-To: References: Message-ID: <12cbbbfc0905260217i717442e8x3ff5bbd81ecc3914@mail.gmail.com> On Mon, May 25, 2009 at 6:46 PM, phpfood wrote: > I ran this in IDLE: > >>> t = 'hi' > >>> print t > SyntaxError: invalid syntax (, line 1) > > I've also tried this as sample.py : > import string > text = 'hello world' > print text > > > It gives me a syntax error on "print text" line > > What's going on? > Are you running python3? If so, it should be: >>> print(t) print changed to a function in python3. If you're just starting to learn python, it will probably be easier if you stick to the 2.X series until you get comfortable. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue May 26 12:52:26 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 26 May 2009 06:52:26 -0400 Subject: [Tutor] import aliases ? In-Reply-To: <20090526101112.5d6605d5@o> References: <20090526101112.5d6605d5@o> Message-ID: <1c2a2c590905260352p15cd116br2f8e5b868698516f@mail.gmail.com> On Tue, May 26, 2009 at 4:11 AM, spir wrote: > Hello, > > In the case of such a dir structure: > > /pack > ? __init__.py > ? (modules) > ? /pack1 > ? ? ?__init__.py > ? ? ?modules > > are the following imports synonyms: > 1- ? import pack.pack1 > 2- ? from pack import pack1 > ? > > If yes, is there a reason for this? > > Also, there is something I find weird: > > * Using idiom 1, pack1 is only known as the compound name "pack.pack1": I cannot eg "print pack1". Consistently, to import a module, I need "from pack.pack1 import mod". > > * Using idiom 2, pack1 is known as "pack1" alone: I can "print pack1", which outputs "". That is the difference between the two styles of import - the name under which the module is imported. > But for further imports, I still need to use the compound name "pack.pack1": "from pack1 import mod" raises "ImportError: No module named pack1", while python has just accepted to print pack1! Yes. The import statement is not taking a module as the first argument, it takes a name. For example in 'from pack import pack1', 'pack' does not have to be a name that is currently in scope. Kent From mysterytramp at yahoo.com Tue May 26 13:37:31 2009 From: mysterytramp at yahoo.com (M Tramp) Date: Tue, 26 May 2009 11:37:31 +0000 (UTC) Subject: [Tutor] A few very basic questions References: <584256.29750.qm@web57202.mail.re3.yahoo.com> Message-ID: Alan Gauld btinternet.com> writes: > I'd be surprised if its a problem with Eclipse, but that's easy > to prove. Just run the code outside Eclipse from the > Terminal app. If you get the same error then it cannot be > Eclipse at fault. Same error from terminal. > Can you post the actual error text? Traceback (most recent call last): File "gramps.py", line 55, in import gtk ImportError: No module named gtk > How/Where did you > install pygtk? via Fink Commander, from which I got this information: status current name pygtk installed provided version 0.6.9-13 binary 0.6.9-13 category gnome summary Placeholder for versioned pygtk packages status current name pygtk-py23 installed 0.6.9-1016 version 0.6.9-1016 binary 0.6.9-1016 category gnome summary Modules for use of GTK with Python mt From srilyk at gmail.com Tue May 26 13:42:44 2009 From: srilyk at gmail.com (W W) Date: Tue, 26 May 2009 06:42:44 -0500 Subject: [Tutor] Tinkering with Tkinter In-Reply-To: <775842.47937.qm@web45713.mail.sp1.yahoo.com> References: <775842.47937.qm@web45713.mail.sp1.yahoo.com> Message-ID: <333efb450905260442m537e9694ne4a5280863c2a19c@mail.gmail.com> On Mon, May 25, 2009 at 7:45 PM, Doug Reid wrote: > The following code and it's explanation doesn't seem to work: > > > 1. >>> from Tkinter import * > > 2. >>> tk = Tk() > > 3. >>> btn = Button(tk, text="click me") > > 4. >>> btn.pack() > > > > In line 1, we import the contents of the > Tk module, so we can use them?the > > most useful of these is > Tk, which creates a basic window to which we can then add > > things. After you type in line 2, that window will suddenly appear on the > screen. > > In line 3, we create a new Button and assign it to the variable > btn. The button > > is created by passing the > tk object as a parameter, along with a named parameter with the words > ?click me?. > > Nothing appears on the screen after I enter line 2...I can type in the code > in the editor and double click after saving the file and it does, but not > from the shell, how can that be fixed or am I doing something wrong? > Works for me! What is your os/python verision? Are you working from Idle? AFAIK, IDLE is written in Tk and so it does not so much like Tkinter programs in it's interactive shell. If you're on windows, try typing "cmd" in the run dialog and then type "python" at the prompt, then try the example. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From rnrcreid at yahoo.com Tue May 26 14:00:34 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Tue, 26 May 2009 05:00:34 -0700 (PDT) Subject: [Tutor] Tinkering with Tkinter Message-ID: <69685.92206.qm@web45711.mail.sp1.yahoo.com> --- On Tue, 5/26/09, W W wrote: From: W W Subject: Re: [Tutor] Tinkering with Tkinter To: "Doug Reid" Cc: Tutor at python.org Date: Tuesday, May 26, 2009, 11:42 AM On Mon, May 25, 2009 at 7:45 PM, Doug Reid wrote: The following code and it's explanation doesn't seem to work: ? 1. >>> from Tkinter import * 2. >>> tk = Tk() 3. >>> btn = Button(tk, text="click me") 4. >>> btn.pack() ? In line 1, we import the contents of the Tk module, so we can use them?the most useful of these is Tk, which creates a basic window to which we can then add things. After you type in line 2, that window will suddenly appear on the screen. In line 3, we create a new Button and assign it to the variable btn. The button is created by passing the tk object as a parameter, along with a named parameter with the words ?click me?. ? Nothing appears on the screen after I enter line 2...I can type in the code in the editor and double click after saving the file and it does, but not from the shell, how can that be fixed or am I doing something wrong? Works for me! What is your os/python verision? Are you working from Idle? AFAIK, IDLE is written in Tk and so it does not so much like Tkinter programs in it's interactive shell. If you're on windows, try typing "cmd" in the run dialog and then type "python" at the prompt, then try the example. HTH, Wayne ? +++Thanks for the help.? I tried it from the command line and it worked fine. ? Doug -------------- next part -------------- An HTML attachment was scrubbed... URL: From nik.mis at gmail.com Tue May 26 14:41:59 2009 From: nik.mis at gmail.com (nikhil) Date: Tue, 26 May 2009 18:11:59 +0530 Subject: [Tutor] Python Function Doubt Message-ID: Hi, I am learning Python and came across this example in the Python Online Tutorial ( http://docs.python.org/tutorial/controlflow.html#default-argument-values) for Default Argument Values. ex: def func(a, L=[ ]): L.append(a) return L print func(1) print func(2) print func(3) *O/P* [1] [1,2] [1,2,3] Now my doubt is, 1) After the first function call, when ' L' (Reference to List object) goes out of scope, why isn't it destroyed ? 2) It seems that this behavior is similar to static variable logic in C language. Is it something similar ? OR Is it related to scope rules in Python ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Tue May 26 14:59:30 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 26 May 2009 14:59:30 +0200 Subject: [Tutor] Python Function Doubt In-Reply-To: References: Message-ID: <4A1BE7B2.2080307@compuscan.co.za> nikhil wrote: > Hi, > > I am learning Python and came across this example in the Python Online > Tutorial > (http://docs.python.org/tutorial/controlflow.html#default-argument-values) > for Default Argument Values. > > ex: > > def func(a, L=[ ]): > L.append(a) > return L > > print func(1) > print func(2) > print func(3) > > _O/P_ > [1] > [1,2] > [1,2,3] > > Now my doubt is, > > 1) After the first function call, when ' L' (Reference to List > object) goes out of scope, why isn't it destroyed ? > > 2) It seems that this behavior is similar to static variable logic in > C language. Is it something similar ? OR > Is it related to scope rules in Python ? > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > It is a well known Python gotcha. Take a read through the "Mutable defaults for function/method arguments" section of http://www.ferg.org/projects/python_gotchas.html#contents_item_6 Hope that helps your understanding. -- Kind Regards, Christian Witts From nik.mis at gmail.com Tue May 26 16:12:34 2009 From: nik.mis at gmail.com (nikhil) Date: Tue, 26 May 2009 19:42:34 +0530 Subject: [Tutor] Python Function Doubt In-Reply-To: <4A1BE7B2.2080307@compuscan.co.za> References: <4A1BE7B2.2080307@compuscan.co.za> Message-ID: Hi, Thanks for reply. I went through the link you provided. It was very helpful. What I understood is this, --> Objects are created for default argument types, inside the function object. This happens only once when the function definition statement is executed. These objects persist between function calls and not destroyed/garbage collected. --> Since List is a mutable object, changes are made to the same object and hence it is visible between function calls. Is my understanding correct ? If so, I have a small doubt regarding function arguments. Are objects for arguments, that persist between function calls, created during function definition ONLY when they have default values ? OR Objects are created for arguments irrespective of whether it has a default value or not, but they are not persistent ( i.e a new object is created for every function call ) ? Regards, Nikhil On Tue, May 26, 2009 at 6:29 PM, Christian Witts wrote: > It is a well known Python gotcha. Take a read through the "Mutable > defaults for function/method arguments" section of > http://www.ferg.org/projects/python_gotchas.html#contents_item_6 > > Hope that helps your understanding. > > -- > Kind Regards, > Christian Witts > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From contactdayo at gmail.com Tue May 26 16:57:10 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Tue, 26 May 2009 15:57:10 +0100 Subject: [Tutor] writing HTML code to a variable/file Message-ID: <4A1C0346.2030407@gmail.com> Hi, I'm extracting data from OpenLDAP, which needs to be formatted into hyperlinks. So far, I can print the generated HTML code: print "" + lastName + ", " + firstName + "" However I want to write each line to a file first, so that I can alphabetically sort the links by lastName, before printing them. I've found this snippet: # Let's create a file and write it to disk. filename = "test.dat" # Let's create some data: done = 0 namelist = [] *while* *not* done: name = raw_input("Enter a name:") *if* type(name) == type(""): namelist.append(name) *else*: *break* # Create a file object: # in "write" mode FILE = open(filename,"w") FILE.writelines(namelist) # Alternatively # for name in namelist: # FILE.write(name) FILE.close() # this is icing, you can just exit and this will be # handled automagically. source: http://www.penzilla.net/tutorials/python/fileio/ Going by the above snippet, I will need to write the hyperlink to a variable, e.g. name, which is in turn appended to namelist, and subsequently written to the file. How do I save this " + lastName + ", " + firstName + " to a variable, then? Thanks Dayo From davea at ieee.org Tue May 26 16:59:24 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 26 May 2009 10:59:24 -0400 Subject: [Tutor] Python Function Doubt In-Reply-To: References: Message-ID: <4A1C03CC.8040500@ieee.org> nikhil wrote: > Hi, > > I am learning Python and came across this example in the Python Online > Tutorial ( > http://docs.python.org/tutorial/controlflow.html#default-argument-values) > for Default Argument Values. > > ex: > > def func(a, L=[ ]): > L.append(a) > return L > > print func(1) > print func(2) > print func(3) > > *O/P* > [1] > [1,2] > [1,2,3] > > Now my doubt is, > > 1) After the first function call, when ' L' (Reference to List object) goes > out of scope, why isn't it destroyed ? > > 2) It seems that this behavior is similar to static variable logic in C > language. Is it something similar ? OR > Is it related to scope rules in Python ? > > > The L parameter is a default parameter. That's a formal parameter with an initial value (L=[]). The reference for that value is established when the definition is executed, not when the function is called. The net effect when the function is at top-level is similar to a static variable in C. But it's not really L that is retained, but the object its bound to. Perhaps it's easiest to think that the function object has a reference to the same object, and copies it to L each time the function is called without that particular formal parameter. See the online help (this one's for 2.6.2, but other recent versions are the same): http://www.python.org/doc/2.6.2/reference/compound_stmts.html#function-definitions Usually, you want to make such an initial value a non-mutable value, such as None, 42, or "". But if it's mutable, it will indeed retain its value from one call to the next of that function. As you observed. There are times when this behavior is useful (eg. nested definitions), and I think it's permanently established in the lanaguage. But it is perhaps one of the most common beginner confusions. And it's part of the solution to some of the other common problems. From denis.spir at free.fr Tue May 26 19:21:51 2009 From: denis.spir at free.fr (spir) Date: Tue, 26 May 2009 19:21:51 +0200 Subject: [Tutor] Python Function Doubt In-Reply-To: References: <4A1BE7B2.2080307@compuscan.co.za> Message-ID: <20090526192151.42d9d04a@o> Le Tue, 26 May 2009 19:42:34 +0530, nikhil s'exprima ainsi: > Hi, > > Thanks for reply. > > I went through the link you provided. It was very helpful. > > What I understood is this, > > --> Objects are created for default argument types, inside the function > object. ... for default arguments -- not types. Inside the function object, yes: ===================== def exp(n, power=2, fool=[1,2,3]): return n ** power print dir(exp) # attributes of function exp print exp.func_defaults ==> ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] (2, [1, 2, 3]) ===================== Note: I think args called "func_*" are renamed "*" alone in later versions of python. > This > happens only once when the function definition statement is executed. > These objects > persist between function calls and not destroyed/garbage collected. > > --> Since List is a mutable object, changes are made to the same object and > hence > it is visible between function calls. > > Is my understanding correct ? Yes; this is true especially when the said list is changed from inside the func body. This is sometimes consciously used to make an argument act like a so-called "static" variable (which despite the name is a dynamic, changeable value). For instance: def numberSum(n, numbers=[]): numbers.append(n) print "Sum so far: %s" % sum(numbers) But the list of numbers is, in fact, an attribute of the function considered as an object, so that this may be more clearly written: def numberSum(n): numberSum.numbers.append(n) print "Sum so far: %s" % sum(numberSum.numbers) numberSum.numbers = [] It may happen too that the default argument is specified from outside, can change at runtime... even intentionally. For instance, a default value can depend on configuration a user may change at runtime. To make this explicite and obvious, it may be better to write things like: def func(arg, defaultarg=None): # if not provided, get defaultarg from current user config if defaultarg is None: defaultarg = defaultFromConfig() ....... > If so, I have a small doubt regarding function arguments. > > Are objects for arguments, that persist between function calls, created > during function definition ONLY when they have default values ? Yes. The issue is that the local variables (like "numbers" above, in the func body) that receive these default objects at call time get them, like always in python, by reference. So that when the object happens to be mutable, any change is shared by the local variable and the default value. > OR > > Objects are created for arguments irrespective of whether it has a default > value or not, but they are not persistent ( i.e a new object is created for > every function call ) ? Here I don't really understand: how can python create an object that has no value? At *call time*, python will define *all* local variables that match an argument. The ones that are not provided and have a default will point to the default objects. For the other ones, python will create short-life objects. You may pay attention to the distinction between a name, an object and the binding of both (~ variable). > Regards, > Nikhil ------ la vita e estrany From alan.gauld at btinternet.com Tue May 26 19:26:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 May 2009 18:26:36 +0100 Subject: [Tutor] Python Function Doubt References: <4A1BE7B2.2080307@compuscan.co.za> Message-ID: "nikhil" wrote > Are objects for arguments, that persist between function calls, created > during function definition ONLY when they have default values ? In Python the parameters are not objects but names. names are used to reference objects. If there is no default value then there is no object to store. So to answer your question, yes, it only affects parameters with default values but no, new objects are not created for the other parameters on each call, rather the other arguments are bound to the arguments passed by the functions user. This leads to the sometimes confusing situation with mutable types that Dave referred to. If the function changes a parameter value and it is mutable then the original object is changed. If it is immutable a new object is assigned to the parameters name. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue May 26 19:34:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 May 2009 18:34:33 +0100 Subject: [Tutor] writing HTML code to a variable/file References: <4A1C0346.2030407@gmail.com> Message-ID: "Dayo Adewunmi" wrote > subsequently written to the file. How do I save this > > " + lastName + ", " > + firstName + " > > to a variable, then? aVar = '" + lastName + ", " + firstName + "" But IMHO its much easier to use string formatting: aVar = '" + lastName > + ", " + firstName + "" This is the string object you'll have to save below. You'd better use "interpolated string" for legibility: html_link = "%s, %s" %(userName,lastName,firstName) > However I want to write each line to a file first, so that I can > alphabetically sort the links by lastName, before printing them. You don't need a file (not even for readable programmer feedback) -- just a list. And sort the list. > I've found this snippet: > > # Let's create a file and write it to disk. > filename = "test.dat" > # Let's create some data: > done = 0 > namelist = [] > *while* *not* done: > name = raw_input("Enter a name:") > *if* type(name) == type(""): > namelist.append(name) > *else*: > *break* Things like if type(name) == type(""): is considered bad python idiom (for goood reasons). Prefere: if isinstance(name, basestring): which also has the advantage to let unicode pass in. > # Create a file object: > # in "write" mode > FILE = open(filename,"w") > FILE.writelines(namelist) > > # Alternatively > # for name in namelist: > # FILE.write(name) > > FILE.close() # this is icing, you can just exit and this will be > # handled automagically. > > source: http://www.penzilla.net/tutorials/python/fileio/ > > Going by the above snippet, I will need to write the hyperlink to a > variable, e.g. name, which is in turn appended to namelist, and > subsequently written to the file. How do I save this > > " + lastName + ", " > + firstName + " > > to a variable, then? Answer above. > Thanks > > Dayo Denis ------ la vita e estrany From paras80 at gmail.com Tue May 26 20:24:35 2009 From: paras80 at gmail.com (Paras K.) Date: Tue, 26 May 2009 14:24:35 -0400 Subject: [Tutor] CVS File Opening Message-ID: Hello, I have been working on this script / program all weekend. I emailed this address before and got some great help. I hope that I can get that again! First to explain what I need to do: Have about 6 CSV files that I need to read. Then I need to split based on a range of IP address and if the count number is larger than 75. I currently merge all the CSV files by using the command line: C:Reports> copy *.csv merge.csv Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "." merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv >From some of my friends they tell me that should remove that last carriage return, which it does, however when it goes through the python script it returns no values. Now if I open the merge.csv and remove that carriage return manually and save it as P2PMerge.csv the script runs just fine. Here is my source code: # P2P Report / Bitorrent Report # Version 1.0 # Last Updated: May 26, 2009 # This script is designed to go through the cvs files and find the valid IP Address # Then copys them all to a new file import sys import win32api import win32ui import shutil import string import os import os.path import csv #Global Variables P2Pfiles = [] totalcount = 0 t = 0 #still in the development process -- where to get the files from #right now the location is C:\P2P def getp2preportdestion(): win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is designed to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to continue with this program.') p2preport = 'C://P2P\\' return p2preport #Main Program #Get location of directories p2ploc = getp2preportdestion() #Checking to make sure directory is there. if os.path.exists(p2ploc): if os.path.isfile(p2ploc +'/p2pmerge.csv'): win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill continue with P2P Reporting.') else: win32ui.MessageBox('P2PMerge.csv files does not exists. \n\nPlease run XXXXXXX.bat files first.') sys.exit() else: win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease create and copy all the files there.\nThen re-run this script') sys.exit() fh = open('C://P2P/P2PMerge.csv', "rb") ff = open('C://P2P/P2PComplete.csv', "wb") igot1 = fh.readlines() for line in igot1: readline = line ipline = readline ctline = readline count = ctline.split(',')[2] count2 = int(count) print count2 t = count2 ip = ipline.split(' ')[0] split_ip = ip.split('.') if ((split_ip[0] == '192') and (t >=75)): ff.write(readline) totalcount +=1 elif ((split_ip[0] == '151') and (t >=75)): ff.write(readline) totalcount +=1 elif (((split_ip[0] == '142') and (split_ip[1]) == '152') and (t >=75)): ff.write(readline) totalcount +=1 tc = str(totalcount) win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) fh.close() ff.close() What I am looking for is an working example of how to go through the directory and read each csv file within that directory or how to remove the carriage return at the end of the csv file. NOTE: This is not for a class - it is for work to assist me in reading multiple csv files within a couple days. Any assistance is greatly appreciated. -- Paras Kinariwala -------------- next part -------------- An HTML attachment was scrubbed... URL: From tyetheczar at hotmail.com Tue May 26 20:30:44 2009 From: tyetheczar at hotmail.com (Gregory Morton) Date: Tue, 26 May 2009 13:30:44 -0500 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu Message-ID: Looking in the directory for Python, I see no real sign of IDLE anywhere in it. The reason for why I want it visible is because I plan on storing Python (this is 3.0.1 by the way) in my USB thumbdrive so I can use and transfer it on other computers. Is there any logic behind why I can't access IDLE without relying on the Start Menu? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Wed May 27 00:51:06 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 27 May 2009 00:51:06 +0200 Subject: [Tutor] CVS File Opening In-Reply-To: References: Message-ID: 2009/5/26 Paras K. : > Hello, > > I have been working on this script / program all weekend. I emailed this > address before and got some great help. I hope that I can get that again! > > > First to explain what I need to do: > > Have about 6 CSV files that I need to read. Then I need to split based on a > range of IP address and if the count number is larger than 75. > > I currently merge all the CSV files by using the command line: > > C:Reports> copy *.csv merge.csv > > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "." > merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv > > From some of my friends?they tell me that should remove that last carriage > return, which it does, however when it goes through the python script it > returns no values. Why would you need to strip off a carriage return? And why would you not process the csv files one after another? It would be easier to have some example data. > Now if I open the merge.csv and remove that carriage return manually and > save it as P2PMerge.csv the script runs just fine. > > Here is my source code: > > # P2P Report / Bitorrent Report > # Version 1.0 > # Last Updated: May 26, 2009 > # This script is designed to go through the cvs files and find the valid IP > Address > # Then copys them all to a new file > import sys > import win32api > import win32ui > import shutil > import string > import os > import os.path > import csv You import csv but do not use it below? > #Global Variables > P2Pfiles = [] > totalcount = 0 > t = 0 > #still in the development process -- where to get the files from > #right now the location is C:\P2P > def getp2preportdestion(): > ??? win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is designed > to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to continue > with this program.') > ??? p2preport = 'C://P2P\\' > ??? return p2preport > > > #Main Program > #Get location of directories > p2ploc = getp2preportdestion() > #Checking to make sure directory is there. > if os.path.exists(p2ploc): > ??? if os.path.isfile(p2ploc +'/p2pmerge.csv'): > ??????? win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill continue > with P2P Reporting.') > ??? else: > ???????? win32ui.MessageBox('P2PMerge.csv files does not exists. \n\nPlease > run XXXXXXX.bat files first.') > ???????? sys.exit() > else: > ??? win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease > create and copy all the files there.\nThen re-run this script') > ??? sys.exit() > fh = open('C://P2P/P2PMerge.csv', "rb") > ff = open('C://P2P/P2PComplete.csv', "wb") > igot1 = fh.readlines() > > for line in igot1: You can also write the below and get rid of igot1. for line in fh.readlines(): > ??? readline = line > ??? ipline = readline > ??? ctline = readline You are making variables to the same object and all are not necessary. See below idle session which should show what I mean. >>> line = [1,2,3,4] >>> readline = line >>> ipline = readline >>> ctline = readline >>> line [1, 2, 3, 4] >>> line.append('This will be copied to readline, iplin and ctline') >>> readline [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] >>> ipline [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] >>> ctline [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > ??? count = ctline.split(',')[2] > ??? count2 = int(count) > ??? print count2 > ??? t = count2 Again making variables to the same object? And you really do not not need t. > ??? ip = ipline.split(' ')[0] so all the above can be simplified like: data = line.split(' ') count = int(data[2]) ip = data[0] > ??? split_ip = ip.split('.') > ??? if ((split_ip[0] == '192') and (t >=75)): The above then would be: if ip.startswith('192') and count >= 75: > ??????? ff.write(readline) This will change as well: ff.write(line) You can figure out the rest ;-) > ??????? totalcount +=1 > ??? elif ((split_ip[0] == '151') and (t >=75)): > ??????? ff.write(readline) > ??????? totalcount +=1 > ??? elif (((split_ip[0] == '142') and (split_ip[1]) == '152') and (t >=75)): > ????????? ff.write(readline) > ????????? totalcount +=1 > > tc = str(totalcount) > win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) > fh.close() > ff.close() > > > What I am looking for is an working example of how to go through the > directory and read each csv file within that directory or how to remove the > carriage return at the end of the csv file. You can avoid the removal of this carriage return, read below. But if you really need to you can use str.rstrip('carriage return'). > NOTE: This is not for a class - it is for work to assist me in reading > multiple csv files within a couple days. > > Any assistance is greatly appreciated. Use te glob module which can easilly find all csv files in a directory. In general I would loop over each file and do your processing. Like, import glob totalcount = 0 for f in glob.glob('inpath' + '*csv'): for line in f.readlines(): You code comes here. Greets Sander From alan.gauld at btinternet.com Wed May 27 01:22:00 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 27 May 2009 00:22:00 +0100 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu References: Message-ID: "Gregory Morton" wrote > Looking in the directory for Python, I see no real sign > of IDLE anywhere in it. Look at the properties of the start menu item. It should show the shortcut points to something like: C:\Python25\Lib\idlelib\idle.pyw > Is there any logic behind why I can't access IDLE without > relying on the Start Menu? None whatsoever, because you can. Just use the direct path that the shortcut uses. However so far as a USB vsion of Python its not quite as simple as just copying the folders Thee are some registry settings that you may need to set too, as well as the environment variables etc. But its not rocket science, just non trivial is all. Alan G. From davea at ieee.org Wed May 27 03:21:45 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 26 May 2009 21:21:45 -0400 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu In-Reply-To: References: Message-ID: <4A1C95A9.4050403@ieee.org> Gregory Morton wrote: > Looking in the directory for Python, I see no real sign of IDLE anywhere in it. The reason for why I want it visible is because I plan on storing Python (this is 3.0.1 by the way) in my USB thumbdrive so I can use and transfer it on other computers. Is there any logic behind why I can't access IDLE without relying on the Start Menu? > (I don't have 3.0.1, but I doubt if it's different from 2.6.2) Idle is started from a batch file, but it just launches the idle.pyw file. Both idle.bat and idle.pyw are in the the Lib\idlelib directory under the Python install directory. To duplicate what the shortcut does, change to the python install directory, and run Lib\idlelib\idle.pyw However, there is still a problem launching it from a thumbdrive. You can't directly launch a .py or .pyw without having the file associations set up. I don't know whether you can just run the pythonw.exe explicitly, or whether other stuff needs to be added, either to the environment or the registry. From kent37 at tds.net Wed May 27 03:30:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 26 May 2009 21:30:22 -0400 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu In-Reply-To: References: Message-ID: <1c2a2c590905261830g39dd2affwf0fc5c6627847e77@mail.gmail.com> On Tue, May 26, 2009 at 2:30 PM, Gregory Morton wrote: > Looking in the directory for Python, I see no real sign of IDLE anywhere in > it. The reason for why I want it visible is because I plan on storing Python > (this is 3.0.1 by the way) in my USB thumbdrive so I can use and transfer it > on other computers. Take a look at Portable Python. http://www.portablepython.com/site/home/ Kent From denis.spir at free.fr Wed May 27 09:02:00 2009 From: denis.spir at free.fr (spir) Date: Wed, 27 May 2009 09:02:00 +0200 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu In-Reply-To: References: Message-ID: <20090527090200.14927ea1@o> Le Wed, 27 May 2009 00:22:00 +0100, "Alan Gauld" s'exprima ainsi: > However so far as a USB vsion of Python its not quite > as simple as just copying the folders Thee are some > registry settings that you may need to set too, as well > as the environment variables etc. > > But its not rocket science, just non trivial is all. Yop, surely a better idea to install python on those computers, too. Denis ------ la vita e estrany From mysterytramp at yahoo.com Wed May 27 14:07:03 2009 From: mysterytramp at yahoo.com (M Tramp) Date: Wed, 27 May 2009 12:07:03 +0000 (UTC) Subject: [Tutor] A few very basic questions References: <584256.29750.qm@web57202.mail.re3.yahoo.com> Message-ID: Alan Gauld btinternet.com> writes: > I'd be surprised if its a problem with Eclipse, but that's easy > to prove. Just run the code outside Eclipse from the > Terminal app. If you get the same error then it cannot be > Eclipse at fault. > > Can you post the actual error text? If it is saying no > module gtk that suggests to me the fault lies in your pygtk > installation rather than in GTK itself. How/Where did you > install pygtk? Fink calls pygtk 0.6 something yet the pygtk site says it's up to 2.14. The answer probably will lie in manually compiling/installing pygtk. But once it's compiled, in which directory should the library be on my hard drive? mt From kent37 at tds.net Wed May 27 14:55:12 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 27 May 2009 08:55:12 -0400 Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu In-Reply-To: References: Message-ID: <1c2a2c590905270555p638e458cl274e5320679c236@mail.gmail.com> On Tue, May 26, 2009 at 7:22 PM, Alan Gauld wrote: > "Gregory Morton" wrote > >> Looking in the directory for Python, I see no real sign of IDLE anywhere >> in it. > > Look at the properties of the start menu item. > > It should show the shortcut points to something like: > > C:\Python25\Lib\idlelib\idle.pyw Strangely, the shortcut properties don't show the target. On my Windows XP system, the properties just show Target type: Application Target location: (blank) Target: Python 2.6.1 Kent From paras80 at gmail.com Wed May 27 15:44:52 2009 From: paras80 at gmail.com (Paras K.) Date: Wed, 27 May 2009 09:44:52 -0400 Subject: [Tutor] CVS File Opening In-Reply-To: References: Message-ID: As requested - here is some example rows from the csv files: 117.86.68.157 BitTorrent Client Activity 1 5/21/2009 6:56 82.210.106.99 BitTorrent Client Activity 1 5/20/2009 12:39 81.132.134.83 BitTorrent Client Activity 1 5/21/2009 3:14 The rows are: IP, Activity, Count, Date / Time these are typical log files. On Tue, May 26, 2009 at 6:51 PM, Sander Sweers wrote: > 2009/5/26 Paras K. : > > Hello, > > > > I have been working on this script / program all weekend. I emailed this > > address before and got some great help. I hope that I can get that again! > > > > > > First to explain what I need to do: > > > > Have about 6 CSV files that I need to read. Then I need to split based on > a > > range of IP address and if the count number is larger than 75. > > > > I currently merge all the CSV files by using the command line: > > > > C:Reports> copy *.csv merge.csv > > > > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "." > > merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv > > > > From some of my friends they tell me that should remove that last > carriage > > return, which it does, however when it goes through the python script it > > returns no values. > > Why would you need to strip off a carriage return? And why would you > not process the csv files one after another? It would be easier to > have some example data. > > > Now if I open the merge.csv and remove that carriage return manually and > > save it as P2PMerge.csv the script runs just fine. > > > > Here is my source code: > > > > # P2P Report / Bitorrent Report > > # Version 1.0 > > # Last Updated: May 26, 2009 > > # This script is designed to go through the cvs files and find the valid > IP > > Address > > # Then copys them all to a new file > > import sys > > import win32api > > import win32ui > > import shutil > > import string > > import os > > import os.path > > import csv > > You import csv but do not use it below? > > > #Global Variables > > P2Pfiles = [] > > totalcount = 0 > > t = 0 > > #still in the development process -- where to get the files from > > #right now the location is C:\P2P > > def getp2preportdestion(): > > win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is > designed > > to aid in the P2P reporting. \n\nThe locations of P2P Reports should be > in > > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to > continue > > with this program.') > > p2preport = 'C://P2P\\' > > return p2preport > > > > > > #Main Program > > #Get location of directories > > p2ploc = getp2preportdestion() > > #Checking to make sure directory is there. > > if os.path.exists(p2ploc): > > if os.path.isfile(p2ploc +'/p2pmerge.csv'): > > win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill > continue > > with P2P Reporting.') > > else: > > win32ui.MessageBox('P2PMerge.csv files does not exists. > \n\nPlease > > run XXXXXXX.bat files first.') > > sys.exit() > > else: > > win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease > > create and copy all the files there.\nThen re-run this script') > > sys.exit() > > fh = open('C://P2P/P2PMerge.csv', "rb") > > ff = open('C://P2P/P2PComplete.csv', "wb") > > igot1 = fh.readlines() > > > > for line in igot1: > > You can also write the below and get rid of igot1. > for line in fh.readlines(): > > > readline = line > > ipline = readline > > ctline = readline > > You are making variables to the same object and all are not necessary. > See below idle session which should show what I mean. > > >>> line = [1,2,3,4] > >>> readline = line > >>> ipline = readline > >>> ctline = readline > >>> line > [1, 2, 3, 4] > >>> line.append('This will be copied to readline, iplin and ctline') > >>> readline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ipline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ctline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > > > count = ctline.split(',')[2] > > count2 = int(count) > > print count2 > > t = count2 > > Again making variables to the same object? And you really do not not need > t. > > > ip = ipline.split(' ')[0] > > so all the above can be simplified like: > data = line.split(' ') > count = int(data[2]) > ip = data[0] > > > split_ip = ip.split('.') > > if ((split_ip[0] == '192') and (t >=75)): > > The above then would be: > if ip.startswith('192') and count >= 75: > > > ff.write(readline) > This will change as well: > ff.write(line) > > You can figure out the rest ;-) > > > totalcount +=1 > > elif ((split_ip[0] == '151') and (t >=75)): > > ff.write(readline) > > totalcount +=1 > > elif (((split_ip[0] == '142') and (split_ip[1]) == '152') and (t > >=75)): > > ff.write(readline) > > totalcount +=1 > > > > tc = str(totalcount) > > win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) > > fh.close() > > ff.close() > > > > > > What I am looking for is an working example of how to go through the > > directory and read each csv file within that directory or how to remove > the > > carriage return at the end of the csv file. > > You can avoid the removal of this carriage return, read below. But if > you really need to you can use str.rstrip('carriage return'). > > > NOTE: This is not for a class - it is for work to assist me in reading > > multiple csv files within a couple days. > > > > Any assistance is greatly appreciated. > > Use te glob module which can easilly find all csv files in a > directory. In general I would loop over each file and do your > processing. Like, > > import glob > > totalcount = 0 > for f in glob.glob('inpath' + '*csv'): > for line in f.readlines(): > You code comes here. > > Greets > Sander > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Wed May 27 16:18:42 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 27 May 2009 16:18:42 +0200 Subject: [Tutor] CVS File Opening In-Reply-To: References: Message-ID: <4A1D4BC2.4020102@compuscan.co.za> Paras K. wrote: > As requested - here is some example rows from the csv files: > > > > 117.86.68.157 BitTorrent Client Activity 1 5/21/2009 6:56 > 82.210.106.99 BitTorrent Client Activity 1 5/20/2009 12:39 > 81.132.134.83 BitTorrent Client Activity 1 5/21/2009 3:14 > > > The rows are: IP, Activity, Count, Date / Time these are typical log > files. > > > > On Tue, May 26, 2009 at 6:51 PM, Sander Sweers > > wrote: > > 2009/5/26 Paras K. >: > > Hello, > > > > I have been working on this script / program all weekend. I > emailed this > > address before and got some great help. I hope that I can get > that again! > > > > > > First to explain what I need to do: > > > > Have about 6 CSV files that I need to read. Then I need to split > based on a > > range of IP address and if the count number is larger than 75. > > > > I currently merge all the CSV files by using the command line: > > > > C:Reports> copy *.csv merge.csv > > > > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in > ('find "." > > merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv > > > > From some of my friends they tell me that should remove that > last carriage > > return, which it does, however when it goes through the python > script it > > returns no values. > > Why would you need to strip off a carriage return? And why would you > not process the csv files one after another? It would be easier to > have some example data. > > > Now if I open the merge.csv and remove that carriage return > manually and > > save it as P2PMerge.csv the script runs just fine. > > > > Here is my source code: > > > > # P2P Report / Bitorrent Report > > # Version 1.0 > > # Last Updated: May 26, 2009 > > # This script is designed to go through the cvs files and find > the valid IP > > Address > > # Then copys them all to a new file > > import sys > > import win32api > > import win32ui > > import shutil > > import string > > import os > > import os.path > > import csv > > You import csv but do not use it below? > > > #Global Variables > > P2Pfiles = [] > > totalcount = 0 > > t = 0 > > #still in the development process -- where to get the files from > > #right now the location is C:\P2P > > def getp2preportdestion(): > > win32ui.MessageBox('Welcome to P2P Reporting.\nThis program > is designed > > to aid in the P2P reporting. \n\nThe locations of P2P Reports > should be in > > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" > to continue > > with this program.') > > p2preport = 'C://P2P\\' > > return p2preport > > > > > > #Main Program > > #Get location of directories > > p2ploc = getp2preportdestion() > > #Checking to make sure directory is there. > > if os.path.exists(p2ploc): > > if os.path.isfile(p2ploc +'/p2pmerge.csv'): > > win32ui.MessageBox('P2PMerge.csv file does > exists.\n\nWill continue > > with P2P Reporting.') > > else: > > win32ui.MessageBox('P2PMerge.csv files does not exists. > \n\nPlease > > run XXXXXXX.bat files first.') > > sys.exit() > > else: > > win32ui.MessageBox('The C:\P2P directory does not > exists.\n\nPlease > > create and copy all the files there.\nThen re-run this script') > > sys.exit() > > fh = open('C://P2P/P2PMerge.csv', "rb") > > ff = open('C://P2P/P2PComplete.csv', "wb") > > igot1 = fh.readlines() > > > > for line in igot1: > > You can also write the below and get rid of igot1. > for line in fh.readlines(): > > > readline = line > > ipline = readline > > ctline = readline > > You are making variables to the same object and all are not necessary. > See below idle session which should show what I mean. > > >>> line = [1,2,3,4] > >>> readline = line > >>> ipline = readline > >>> ctline = readline > >>> line > [1, 2, 3, 4] > >>> line.append('This will be copied to readline, iplin and ctline') > >>> readline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ipline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ctline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > > > count = ctline.split(',')[2] > > count2 = int(count) > > print count2 > > t = count2 > > Again making variables to the same object? And you really do not > not need t. > > > ip = ipline.split(' ')[0] > > so all the above can be simplified like: > data = line.split(' ') > count = int(data[2]) > ip = data[0] > > > split_ip = ip.split('.') > > if ((split_ip[0] == '192') and (t >=75)): > > The above then would be: > if ip.startswith('192') and count >= 75: > > > ff.write(readline) > This will change as well: > ff.write(line) > > You can figure out the rest ;-) > > > totalcount +=1 > > elif ((split_ip[0] == '151') and (t >=75)): > > ff.write(readline) > > totalcount +=1 > > elif (((split_ip[0] == '142') and (split_ip[1]) == '152') > and (t >=75)): > > ff.write(readline) > > totalcount +=1 > > > > tc = str(totalcount) > > win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) > > fh.close() > > ff.close() > > > > > > What I am looking for is an working example of how to go through the > > directory and read each csv file within that directory or how to > remove the > > carriage return at the end of the csv file. > > You can avoid the removal of this carriage return, read below. But if > you really need to you can use str.rstrip('carriage return'). > > > NOTE: This is not for a class - it is for work to assist me in > reading > > multiple csv files within a couple days. > > > > Any assistance is greatly appreciated. > > Use te glob module which can easilly find all csv files in a > directory. In general I would loop over each file and do your > processing. Like, > > import glob > > totalcount = 0 > for f in glob.glob('inpath' + '*csv'): > for line in f.readlines(): > You code comes here. > > Greets > Sander > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > If that's your log structure and it's all IP Addresses and what you want is to count the amount of P2P activity per IP and for whatever purpose then what you could do is something similar to this: from glob import glob if __name__ == '__main__': IP_Addresses = dict() for filename in glob('*.csv'): fIn = open(filename, 'rb') for line in fIn: IP, Activity, Count, TimeDate = line.strip().split(' ') if IP in IP_Addresses: IP_Addresses[IP] += int(Count) else: IP_Addresses[IP] = int(Count) for IP, Cnt in IP_Addresses.items(): if Cnt >= 75: if IP.split('.')[0] in ('192', '151'): print IP, Cnt elif IP.split('.')[:2] == ['142', '152']: print IP, Cnt Obviously if you want to keep the original log line then you will need to store that in your dictionary as well, but for the purpose of reporting how many 'offences' an IP Address has had this is simple enough. -- Kind Regards, Christian Witts From paras80 at gmail.com Wed May 27 16:12:31 2009 From: paras80 at gmail.com (Paras K.) Date: Wed, 27 May 2009 10:12:31 -0400 Subject: [Tutor] CVS File Opening In-Reply-To: <4A1D4BC2.4020102@compuscan.co.za> References: <4A1D4BC2.4020102@compuscan.co.za> Message-ID: There are no headers for the log files, and there are mulitple log files so what that walk through the directory for all csv files? THANK IN ADVANCE!!! On Wed, May 27, 2009 at 10:18 AM, Christian Witts wrote: > Paras K. wrote: > >> As requested - here is some example rows from the csv files: >> >> 117.86.68.157 BitTorrent Client Activity 1 5/21/2009 6:56 >> 82.210.106.99 BitTorrent Client Activity 1 5/20/2009 12:39 >> 81.132.134.83 BitTorrent Client Activity 1 5/21/2009 3:14 >> >> The rows are: IP, Activity, Count, Date / Time these are typical log >> files. >> >> >> On Tue, May 26, 2009 at 6:51 PM, Sander Sweers > sander.sweers at gmail.com>> wrote: >> >> 2009/5/26 Paras K. >: >> >> > Hello, >> > >> > I have been working on this script / program all weekend. I >> emailed this >> > address before and got some great help. I hope that I can get >> that again! >> > >> > >> > First to explain what I need to do: >> > >> > Have about 6 CSV files that I need to read. Then I need to split >> based on a >> > range of IP address and if the count number is larger than 75. >> > >> > I currently merge all the CSV files by using the command line: >> > >> > C:Reports> copy *.csv merge.csv >> > >> > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in >> ('find "." >> > merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv >> > >> > From some of my friends they tell me that should remove that >> last carriage >> > return, which it does, however when it goes through the python >> script it >> > returns no values. >> >> Why would you need to strip off a carriage return? And why would you >> not process the csv files one after another? It would be easier to >> have some example data. >> >> > Now if I open the merge.csv and remove that carriage return >> manually and >> > save it as P2PMerge.csv the script runs just fine. >> > >> > Here is my source code: >> > >> > # P2P Report / Bitorrent Report >> > # Version 1.0 >> > # Last Updated: May 26, 2009 >> > # This script is designed to go through the cvs files and find >> the valid IP >> > Address >> > # Then copys them all to a new file >> > import sys >> > import win32api >> > import win32ui >> > import shutil >> > import string >> > import os >> > import os.path >> > import csv >> >> You import csv but do not use it below? >> >> > #Global Variables >> > P2Pfiles = [] >> > totalcount = 0 >> > t = 0 >> > #still in the development process -- where to get the files from >> > #right now the location is C:\P2P >> > def getp2preportdestion(): >> > win32ui.MessageBox('Welcome to P2P Reporting.\nThis program >> is designed >> > to aid in the P2P reporting. \n\nThe locations of P2P Reports >> should be in >> > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" >> to continue >> > with this program.') >> > p2preport = 'C://P2P\\' >> > return p2preport >> > >> > >> > #Main Program >> > #Get location of directories >> > p2ploc = getp2preportdestion() >> > #Checking to make sure directory is there. >> > if os.path.exists(p2ploc): >> > if os.path.isfile(p2ploc +'/p2pmerge.csv'): >> > win32ui.MessageBox('P2PMerge.csv file does >> exists.\n\nWill continue >> > with P2P Reporting.') >> > else: >> > win32ui.MessageBox('P2PMerge.csv files does not exists. >> \n\nPlease >> > run XXXXXXX.bat files first.') >> > sys.exit() >> > else: >> > win32ui.MessageBox('The C:\P2P directory does not >> exists.\n\nPlease >> > create and copy all the files there.\nThen re-run this script') >> > sys.exit() >> > fh = open('C://P2P/P2PMerge.csv', "rb") >> > ff = open('C://P2P/P2PComplete.csv', "wb") >> > igot1 = fh.readlines() >> > >> > for line in igot1: >> >> You can also write the below and get rid of igot1. >> for line in fh.readlines(): >> >> > readline = line >> > ipline = readline >> > ctline = readline >> >> You are making variables to the same object and all are not necessary. >> See below idle session which should show what I mean. >> >> >>> line = [1,2,3,4] >> >>> readline = line >> >>> ipline = readline >> >>> ctline = readline >> >>> line >> [1, 2, 3, 4] >> >>> line.append('This will be copied to readline, iplin and ctline') >> >>> readline >> [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] >> >>> ipline >> [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] >> >>> ctline >> [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] >> >> > count = ctline.split(',')[2] >> > count2 = int(count) >> > print count2 >> > t = count2 >> >> Again making variables to the same object? And you really do not >> not need t. >> >> > ip = ipline.split(' ')[0] >> >> so all the above can be simplified like: >> data = line.split(' ') >> count = int(data[2]) >> ip = data[0] >> >> > split_ip = ip.split('.') >> > if ((split_ip[0] == '192') and (t >=75)): >> >> The above then would be: >> if ip.startswith('192') and count >= 75: >> >> > ff.write(readline) >> This will change as well: >> ff.write(line) >> >> You can figure out the rest ;-) >> >> > totalcount +=1 >> > elif ((split_ip[0] == '151') and (t >=75)): >> > ff.write(readline) >> > totalcount +=1 >> > elif (((split_ip[0] == '142') and (split_ip[1]) == '152') >> and (t >=75)): >> > ff.write(readline) >> > totalcount +=1 >> > >> > tc = str(totalcount) >> > win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) >> > fh.close() >> > ff.close() >> > >> > >> > What I am looking for is an working example of how to go through the >> > directory and read each csv file within that directory or how to >> remove the >> > carriage return at the end of the csv file. >> >> You can avoid the removal of this carriage return, read below. But if >> you really need to you can use str.rstrip('carriage return'). >> >> > NOTE: This is not for a class - it is for work to assist me in >> reading >> > multiple csv files within a couple days. >> > >> > Any assistance is greatly appreciated. >> >> Use te glob module which can easilly find all csv files in a >> directory. In general I would loop over each file and do your >> processing. Like, >> >> import glob >> >> totalcount = 0 >> for f in glob.glob('inpath' + '*csv'): >> for line in f.readlines(): >> You code comes here. >> >> Greets >> Sander >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > If that's your log structure and it's all IP Addresses and what you want is > to count the amount of P2P activity per IP and for whatever purpose then > what you could do is something similar to this: > > from glob import glob > > if __name__ == '__main__': > IP_Addresses = dict() > for filename in glob('*.csv'): > fIn = open(filename, 'rb') > for line in fIn: > IP, Activity, Count, TimeDate = line.strip().split(' ') > if IP in IP_Addresses: > IP_Addresses[IP] += int(Count) > else: > IP_Addresses[IP] = int(Count) > for IP, Cnt in IP_Addresses.items(): > if Cnt >= 75: > if IP.split('.')[0] in ('192', '151'): > print IP, Cnt > elif IP.split('.')[:2] == ['142', '152']: > print IP, Cnt > > Obviously if you want to keep the original log line then you will need to > store that in your dictionary as well, but for the purpose of reporting how > many 'offences' an IP Address has had this is simple enough. > > -- > Kind Regards, > Christian Witts > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Wed May 27 16:50:43 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 27 May 2009 16:50:43 +0200 Subject: [Tutor] CVS File Opening In-Reply-To: References: <4A1D4BC2.4020102@compuscan.co.za> Message-ID: <4A1D5343.4060506@compuscan.co.za> Paras K. wrote: > There are no headers for the log files, and there are mulitple log > files so what that walk through the directory for all csv files? > > THANK IN ADVANCE!!! > > On Wed, May 27, 2009 at 10:18 AM, Christian Witts > > wrote: > > Paras K. wrote: > > As requested - here is some example rows from the csv files: > > 117.86.68.157 BitTorrent Client Activity 1 > 5/21/2009 6:56 > 82.210.106.99 BitTorrent Client Activity 1 > 5/20/2009 12:39 > 81.132.134.83 BitTorrent Client Activity 1 > 5/21/2009 3:14 > > The rows are: IP, Activity, Count, Date / Time these are > typical log files. > > > On Tue, May 26, 2009 at 6:51 PM, Sander Sweers > > >> wrote: > > 2009/5/26 Paras K. >>: > > > Hello, > > > > I have been working on this script / program all weekend. I > emailed this > > address before and got some great help. I hope that I can get > that again! > > > > > > First to explain what I need to do: > > > > Have about 6 CSV files that I need to read. Then I need > to split > based on a > > range of IP address and if the count number is larger > than 75. > > > > I currently merge all the CSV files by using the command > line: > > > > C:Reports> copy *.csv merge.csv > > > > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in > ('find "." > > merge.csv ^| find /v "----"') do echo %i>> P2PMerge.csv > > > > From some of my friends they tell me that should remove that > last carriage > > return, which it does, however when it goes through the > python > script it > > returns no values. > > Why would you need to strip off a carriage return? And why > would you > not process the csv files one after another? It would be > easier to > have some example data. > > > Now if I open the merge.csv and remove that carriage return > manually and > > save it as P2PMerge.csv the script runs just fine. > > > > Here is my source code: > > > > # P2P Report / Bitorrent Report > > # Version 1.0 > > # Last Updated: May 26, 2009 > > # This script is designed to go through the cvs files and > find > the valid IP > > Address > > # Then copys them all to a new file > > import sys > > import win32api > > import win32ui > > import shutil > > import string > > import os > > import os.path > > import csv > > You import csv but do not use it below? > > > #Global Variables > > P2Pfiles = [] > > totalcount = 0 > > t = 0 > > #still in the development process -- where to get the > files from > > #right now the location is C:\P2P > > def getp2preportdestion(): > > win32ui.MessageBox('Welcome to P2P Reporting.\nThis > program > is designed > > to aid in the P2P reporting. \n\nThe locations of P2P Reports > should be in > > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - > \n\nPress "OK" > to continue > > with this program.') > > p2preport = 'C://P2P\\' > > return p2preport > > > > > > #Main Program > > #Get location of directories > > p2ploc = getp2preportdestion() > > #Checking to make sure directory is there. > > if os.path.exists(p2ploc): > > if os.path.isfile(p2ploc +'/p2pmerge.csv'): > > win32ui.MessageBox('P2PMerge.csv file does > exists.\n\nWill continue > > with P2P Reporting.') > > else: > > win32ui.MessageBox('P2PMerge.csv files does not > exists. > \n\nPlease > > run XXXXXXX.bat files first.') > > sys.exit() > > else: > > win32ui.MessageBox('The C:\P2P directory does not > exists.\n\nPlease > > create and copy all the files there.\nThen re-run this > script') > > sys.exit() > > fh = open('C://P2P/P2PMerge.csv', "rb") > > ff = open('C://P2P/P2PComplete.csv', "wb") > > igot1 = fh.readlines() > > > > for line in igot1: > > You can also write the below and get rid of igot1. > for line in fh.readlines(): > > > readline = line > > ipline = readline > > ctline = readline > > You are making variables to the same object and all are not > necessary. > See below idle session which should show what I mean. > > >>> line = [1,2,3,4] > >>> readline = line > >>> ipline = readline > >>> ctline = readline > >>> line > [1, 2, 3, 4] > >>> line.append('This will be copied to readline, iplin and > ctline') > >>> readline > [1, 2, 3, 4, 'This will be copied to readline, iplin and > ctline'] > >>> ipline > [1, 2, 3, 4, 'This will be copied to readline, iplin and > ctline'] > >>> ctline > [1, 2, 3, 4, 'This will be copied to readline, iplin and > ctline'] > > > count = ctline.split(',')[2] > > count2 = int(count) > > print count2 > > t = count2 > > Again making variables to the same object? And you really > do not > not need t. > > > ip = ipline.split(' ')[0] > > so all the above can be simplified like: > data = line.split(' ') > count = int(data[2]) > ip = data[0] > > > split_ip = ip.split('.') > > if ((split_ip[0] == '192') and (t >=75)): > > The above then would be: > if ip.startswith('192') and count >= 75: > > > ff.write(readline) > This will change as well: > ff.write(line) > > You can figure out the rest ;-) > > > totalcount +=1 > > elif ((split_ip[0] == '151') and (t >=75)): > > ff.write(readline) > > totalcount +=1 > > elif (((split_ip[0] == '142') and (split_ip[1]) == '152') > and (t >=75)): > > ff.write(readline) > > totalcount +=1 > > > > tc = str(totalcount) > > win32ui.MessageBox('Total Number of IPs in P2P Reporting: > '+ tc) > > fh.close() > > ff.close() > > > > > > What I am looking for is an working example of how to go > through the > > directory and read each csv file within that directory or > how to > remove the > > carriage return at the end of the csv file. > > You can avoid the removal of this carriage return, read > below. But if > you really need to you can use str.rstrip('carriage return'). > > > NOTE: This is not for a class - it is for work to assist > me in > reading > > multiple csv files within a couple days. > > > > Any assistance is greatly appreciated. > > Use te glob module which can easilly find all csv files in a > directory. In general I would loop over each file and do your > processing. Like, > > import glob > > totalcount = 0 > for f in glob.glob('inpath' + '*csv'): > for line in f.readlines(): > You code comes here. > > Greets > Sander > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > If that's your log structure and it's all IP Addresses and what > you want is to count the amount of P2P activity per IP and for > whatever purpose then what you could do is something similar to this: > > from glob import glob > > if __name__ == '__main__': > IP_Addresses = dict() > for filename in glob('*.csv'): > fIn = open(filename, 'rb') > for line in fIn: > IP, Activity, Count, TimeDate = line.strip().split(' ') > if IP in IP_Addresses: > IP_Addresses[IP] += int(Count) > else: > IP_Addresses[IP] = int(Count) > for IP, Cnt in IP_Addresses.items(): > if Cnt >= 75: > if IP.split('.')[0] in ('192', '151'): > print IP, Cnt > elif IP.split('.')[:2] == ['142', '152']: > print IP, Cnt > > Obviously if you want to keep the original log line then you will > need to store that in your dictionary as well, but for the purpose > of reporting how many 'offences' an IP Address has had this is > simple enough. > > -- > Kind Regards, > Christian Witts > > > from glob import glob for filename in glob('/path/to/your/files/*.csv'): print filename That will recurse the files in the folder for everything with a .csv extension which is what you want. Then for each file that matches the extension type, the application I wrote in the previous with recurse through each line in the file, split the contents of the log on spaces, although it looks like tabs in your sample then just change the .split(' ') with .split('\t') which will break it up into IP, Activity, Count, DateTime. It will add the IP Address to a dictionary of IP Addresses if it is not there with the count of that log and any further from that IP will increment it by the log count. Once all files have been processed it will then check the Addresses and check what the count is (you don't care about ones with less than 75 hits) and then check what range they are in and output those. Hope that helps. -- Kind Regards, Christian Witts From alan.gauld at btinternet.com Wed May 27 17:29:20 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 27 May 2009 15:29:20 +0000 (GMT) Subject: [Tutor] IDLE shortcut doesn't exist except for in the Start Menu In-Reply-To: <1c2a2c590905270555p638e458cl274e5320679c236@mail.gmail.com> References: <1c2a2c590905270555p638e458cl274e5320679c236@mail.gmail.com> Message-ID: <713362.72043.qm@web86705.mail.ird.yahoo.com> > Strangely, the shortcut properties don't show the target. On my > Windows XP system, the properties just show > Target type: Application > Target location: (blank) > Target: Python 2.6.1 > > Kent Bizarre! My XP box says Target type: PYW File Target location: idlelib Target: C:\Python25\Lib\idlelib\idle.pyw How strange. Alan G. From endlessly.curious at gmail.com Wed May 27 19:57:29 2009 From: endlessly.curious at gmail.com (Daniel Brown) Date: Wed, 27 May 2009 10:57:29 -0700 Subject: [Tutor] Getting exposure to high quality Python code Message-ID: <743f2e480905271057r255fd0e2wfe2f6011dea0a5e7@mail.gmail.com> Good Morning list, I have just started to learn Python after about two years of putting it off. So far I have written two automation style scripts with in Python and I really enjoyed it (so much better than batch files). I am now looking at doing some tools and web development (with Django) in Python in the near future, I've mostly used C# for tools and php for web till now. Can anyone recommend any ways of getting exposed to high quality Python code? Normally I would expect this to happen during code reviews but Python usage is not wide spread where I work. So I need some alternative sources to help me develop my Python style... Cheers, Daniel ?The best laid schemes o' mice an' men, gang aft agley? From alan.gauld at btinternet.com Wed May 27 20:23:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 27 May 2009 19:23:13 +0100 Subject: [Tutor] Getting exposure to high quality Python code References: <743f2e480905271057r255fd0e2wfe2f6011dea0a5e7@mail.gmail.com> Message-ID: "Daniel Brown" wrote > Can anyone recommend any ways of getting exposed to high quality > Python code? Normally I would expect this to happen during code > reviews but Python usage is not wide spread where I work. So I need > some alternative sources to help me develop my Python style... The source code for many of the standard library modules is available to you in the installation itself. There are also some sample apps such as the regex tester. Also if you install Django you have another bunch of files to look at. And search source forge for python projects, lots of examples. Finally look at the ActiveState web site for Python recipes. Lots of good ideas there. Of course what constitutes good code is somewhat subjective but the library and recipes are virtually all pretty good code IMHO. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Wed May 27 21:04:00 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 27 May 2009 15:04:00 -0400 Subject: [Tutor] Getting exposure to high quality Python code In-Reply-To: <743f2e480905271057r255fd0e2wfe2f6011dea0a5e7@mail.gmail.com> References: <743f2e480905271057r255fd0e2wfe2f6011dea0a5e7@mail.gmail.com> Message-ID: <1c2a2c590905271204o6235a23bp68d0bae2c51e5785@mail.gmail.com> On Wed, May 27, 2009 at 1:57 PM, Daniel Brown wrote: > Can anyone recommend any ways of getting exposed to high quality > Python code? ?Normally I would expect this to happen during code > reviews but Python usage is not wide spread where I work. ?So I need > some alternative sources to help me develop my Python style... You could lurk on comp.lang.python. The print Python Cookbook is an excellent source of annotated, idiomatic Python code. I'll also follow Alan's suggestion to look at the std lib and Django. If you wonder "how does that work?" just take a look at the source code and figure it out. Kent From washakie at gmail.com Thu May 28 12:47:09 2009 From: washakie at gmail.com (John [H2O]) Date: Thu, 28 May 2009 03:47:09 -0700 (PDT) Subject: [Tutor] creating a dict-like class - asigning variables... this one may take some thought ; ) Message-ID: <23759398.post@talk.nabble.com> Hello, I am trying to create a class to hold and reference things similar to matlab's structure. ## A class definition to hold things class stuff(object): """ holds stuff """ def __init__(): pass @classmethod def items(cls): stuff = [] for i in cls.__dict__: if i[:1] != '_' and i != 'items': stuff.append((i,cls.__dict__[i])) return stuff Then in my code I would like to be able to do the follow: s = stuff s.cheese = "Brie" s.country = "French" s.age = 2 and so on... In Ipython, as above it does work. Now here is the tricky part. I'm reading in binary data from unformatted Fortan output. My present approach is as follows (recommended suggestions welcome): f2=file(filename,'rb') #Define names and create Dictionary Keys I = {0:'rl0', 1:'ibdate', 2:'ibtime', 3:'version',\ 4:'rl1', 5:'loutstep', 6:'loutaver', 7:'loutsample',\ 8:'rl2', 9:'outlon0', 10:'outlat0', 11:'numxgrid',\ 12:'numygrid', 13:'dxout', 14:'dyout', 15:'rl3', 16:'numzgrid',\ } #define the format for binary reading first part of the header file Dfmt=['i','i','i','13s','2i','i','i','i','2i','f','f','i','i','f','f','2i','i'] if f2: #print filename + ' has been opened' a=[struct.unpack(ft,f2.read(struct.calcsize(ft))) for ft in Dfmt] # Now I want to put them into my stuff class: for j in range(len(a)): cmd = "h.%s = a[%s][0]" % (I[j],j) eval(cmd) But I get an error: eval(cmd) File "", line 1 h.rl0 = a[0][0] ^ SyntaxError: invalid syntax Thoughts? Must be something simpler or more 'pythonic' Thanks! -- View this message in context: http://www.nabble.com/creating-a-dict-like-class---asigning-variables...-this-one-may-take-some-thought--%29-tp23759398p23759398.html Sent from the Python - tutor mailing list archive at Nabble.com. From mobiledreamers at gmail.com Thu May 28 09:05:40 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Thu, 28 May 2009 00:05:40 -0700 Subject: [Tutor] How do you serve Cheetah in production? webpy+cheetah Message-ID: How do you serve *Cheetah* in *production*? Guys can you share the setup on how to precompile and serve cheetah in production Since we dont compile templates in webpy it is getting upstream time out errors. If you could share a good best practise it would help * Jeremy jeremy.james at gmail.com wrote: For a production site, I use Cheetah with pre-compiled templates - it's very fast (the templates import especially quickly when python compiled and optimised). A bit of magic with the imp module takes a template name and a base directory (configured in a site-specific config) and loads up that template, taking care of #extends and import directives as appropriate. I don't use the built-in support for Cheetah, however. The new template library is also only imported to display the debugerror page -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Thu May 28 13:43:47 2009 From: denis.spir at free.fr (spir) Date: Thu, 28 May 2009 13:43:47 +0200 Subject: [Tutor] creating a dict-like class - asigning variables... this one may take some thought ; ) In-Reply-To: <23759398.post@talk.nabble.com> References: <23759398.post@talk.nabble.com> Message-ID: <20090528134347.2fae12c7@o> Le Thu, 28 May 2009 03:47:09 -0700 (PDT), "John [H2O]" s'exprima ainsi: > > Hello, I am trying to create a class to hold and reference things similar to > matlab's structure. > > ## A class definition to hold things > class stuff(object): > """ holds stuff """ > def __init__(): > pass > @classmethod > def items(cls): > stuff = [] > for i in cls.__dict__: > if i[:1] != '_' and i != 'items': > stuff.append((i,cls.__dict__[i])) > return stuff > > Then in my code I would like to be able to do the follow: > > s = stuff > s.cheese = "Brie" > s.country = "French" > s.age = 2 > > and so on... > > In Ipython, as above it does work. Now here is the tricky part. I'm reading > in binary data from unformatted Fortan output. My present approach is as > follows (recommended suggestions welcome): What you're looking for is a dictionary... s = {"cheese":"Brie", "country":"France", ...} Or maybe a kind of object type that works ~ like a dict, but with object syntax (get rid of {} and "" for keys). Example: class Stuff(object): def __iter__(self): return iter(self.__dict__.items()) def items(self): return self.__dict__ stuff = Stuff() stuff.cheese="Brie" stuff.country="France" print stuff.cheese, stuff.country print stuff.items() for st in stuff: print " ", st ==> Brie France {'cheese': 'Brie', 'country': 'France'} ('cheese', 'Brie') ('country', 'France') Denis ------ la vita e estrany From jsseabold at gmail.com Thu May 28 15:14:02 2009 From: jsseabold at gmail.com (Skipper Seabold) Date: Thu, 28 May 2009 09:14:02 -0400 Subject: [Tutor] creating a dict-like class - asigning variables... this one may take some thought ; ) In-Reply-To: <23759398.post@talk.nabble.com> References: <23759398.post@talk.nabble.com> Message-ID: On Thu, May 28, 2009 at 6:47 AM, John [H2O] wrote: > > Hello, I am trying to create a class to hold and reference things similar to > matlab's structure. > > ## A class definition to hold things > class stuff(object): > ? ?""" holds stuff """ > ? ?def __init__(): > ? ? ? ?pass > ? ?@classmethod > ? ?def items(cls): > ? ? ? ?stuff = [] > ? ? ? ?for i in cls.__dict__: > ? ? ? ? ? ?if i[:1] != '_' and i != 'items': > ? ? ? ? ? ? ? ?stuff.append((i,cls.__dict__[i])) > ? ? ? ?return stuff > > Then in my code I would like to be able to do the follow: > > s = stuff > s.cheese = "Brie" > s.country = "French" > s.age = 2 > > and so on... > > In Ipython, as above it does work. Now here is the tricky part. I'm reading > in binary data from unformatted Fortan output. My present approach is as > follows (recommended suggestions welcome): > > f2=file(filename,'rb') > #Define names and create Dictionary Keys > I = {0:'rl0', 1:'ibdate', 2:'ibtime', 3:'version',\ > ? ? ? ? 4:'rl1', 5:'loutstep', 6:'loutaver', 7:'loutsample',\ > ? ? ? ? 8:'rl2', 9:'outlon0', 10:'outlat0', 11:'numxgrid',\ > ? ? ? ? 12:'numygrid', 13:'dxout', 14:'dyout', 15:'rl3', 16:'numzgrid',\ > ? ? ? ? } > #define the format for binary reading first part of the header file > Dfmt=['i','i','i','13s','2i','i','i','i','2i','f','f','i','i','f','f','2i','i'] > if f2: > ? ?#print filename + ' has been opened' > ? ?a=[struct.unpack(ft,f2.read(struct.calcsize(ft))) for ft in Dfmt] > > # Now I want to put them into my stuff class: > ? ?for j in range(len(a)): > ? ? ? ?cmd = "h.%s = a[%s][0]" % (I[j],j) > ? ? ? ?eval(cmd) > > > But I get an error: > ? ?eval(cmd) > ?File "", line 1 > ? ?h.rl0 = a[0][0] > ? ? ? ? ?^ > SyntaxError: invalid syntax > > Thoughts? Must be something simpler or more 'pythonic' > > Thanks! For what it's worth, you might want to have a look at NumPy/SciPy unless you have some other motive. Their structured or record arrays behave in much the way you are describing and can handled binary data (or matlab data). This also might be of interest . Cheers, Skipper From kent37 at tds.net Thu May 28 16:52:57 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 28 May 2009 10:52:57 -0400 Subject: [Tutor] creating a dict-like class - asigning variables... this one may take some thought ; ) In-Reply-To: <23759398.post@talk.nabble.com> References: <23759398.post@talk.nabble.com> Message-ID: <1c2a2c590905280752y74b8a360x3316ab4aff6f4816@mail.gmail.com> On Thu, May 28, 2009 at 6:47 AM, John [H2O] wrote: > # Now I want to put them into my stuff class: > ? ?for j in range(len(a)): > ? ? ? ?cmd = "h.%s = a[%s][0]" % (I[j],j) > ? ? ? ?eval(cmd) Use setattr() rather than eval. Something like setattr(h, l[j], a[j][0]) Kent From dextrous85 at gmail.com Thu May 28 18:51:24 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 28 May 2009 22:21:24 +0530 Subject: [Tutor] Error installing review board on windows Message-ID: <5487b3060905280951m74038e91l34d82f857e3dd25a@mail.gmail.com> Hi List, I am trying to install review board on windows. It fails saying it could not build PIL 1.1.6 because visual studio was not found on the system and but I have already installed the PIL 1.1.6 using binaries available on the site but review board fails to locate it and every time tries to download the PIL and fails while building the same. error shown below *error: Setup script exited with error: Python was built with Visual Studio 2003;* *extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing "-c mingw32" to setup.py. *I tried to build the PIL from source using cygwin but that also failed with following error * `_imp___Py_NoneStruct' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xa93): undefined reference to `_imp___Py_NoneStruct' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xac2): undefined reference to `_imp__PyExc_IndexError' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xae5): undefined reference to `_imp__PyExc_TypeError' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd7c): undefined reference to `_imp___Py_NoneStruct' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd84): undefined reference to `_imp___Py_NoneStruct' build\temp.win32-2.5\Release\path.o:path.c:(.text+0xe01): undefined reference to `_imp__PyExc_AttributeError' collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1* Kindly help -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu May 28 18:59:03 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 28 May 2009 09:59:03 -0700 Subject: [Tutor] Error installing review board on windows In-Reply-To: <5487b3060905280951m74038e91l34d82f857e3dd25a@mail.gmail.com> References: <5487b3060905280951m74038e91l34d82f857e3dd25a@mail.gmail.com> Message-ID: On 5/28/2009 9:51 AM vishwajeet singh said... > Hi List, > > I am trying to install review board on windows. I'm not familiar with review board, but these type of third party software installation problems are typically better addressed through the support groups for the third party application. Emile > It fails saying it could > not build PIL 1.1.6 because visual studio was not found on the system > and but I have already installed the PIL 1.1.6 using binaries available > on the site but review board fails to locate it and every time tries to > download the PIL and fails while building the same. > > error shown below > /error: Setup script exited with error: Python was built with Visual > Studio 2003;/ > /extensions must be built with a compiler than can generate compatible > binaries. > Visual Studio 2003 was not found on this system. If you have Cygwin > installed, > you can try compiling with MingW32, by passing "-c mingw32" to setup.py. > > /I tried to build the PIL from source using cygwin but that also failed > with following error > / `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xa93): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xac2): undefined > reference to > `_imp__PyExc_IndexError' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xae5): undefined > reference to > `_imp__PyExc_TypeError' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd7c): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd84): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xe01): undefined > reference to > `_imp__PyExc_AttributeError' > collect2: ld returned 1 exit status > error: command 'gcc' failed with exit status 1/ > > Kindly help > -- > Cheers, > Vishwajeet > http://www.singhvishwajeet.com > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From tyetheczar at hotmail.com Thu May 28 19:22:20 2009 From: tyetheczar at hotmail.com (Gregory Morton) Date: Thu, 28 May 2009 12:22:20 -0500 Subject: [Tutor] Displaying range in 3.0.1 Message-ID: I've been reading this Python 3.0.1 tutorial(http://docs.python.org/3.0/tutorial/controlflow.html), and now I'm stuck at the second example in 4.3. This is what the example says the output should look like:range(5, 10) 5 through 9 range(0, 10, 3) 0, 3, 6, 9 range(-10, -100, -30) -10, -40, -70 But what I receive instead is the same as what I input (i.e. range(5, 10) just returns range(5, 10)). -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Thu May 28 20:06:03 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 28 May 2009 20:06:03 +0200 Subject: [Tutor] Displaying range in 3.0.1 In-Reply-To: References: Message-ID: 2009/5/28 Gregory Morton : > I've been reading this Python 3.0.1 > tutorial(http://docs.python.org/3.0/tutorial/controlflow.html), and now I'm > stuck at the second example in 4.3. This is what the example says the output > should look like: No it doen't but I can understand the confusion. More info below. > range(5, 10) > 5 through 9 > > range(0, 10, 3) > 0, 3, 6, 9 > > range(-10, -100, -30) > -10, -40, -70 > > But what I receive instead is the same as what I input (i.e. range(5, > 10)?just returns?range(5, 10)). What it returns is an *iterable* range object. Meaning you need to iterate over it to get the values like in the first example. for n in range(5,10): print(n) Greets Sander From wescpy at gmail.com Thu May 28 20:24:53 2009 From: wescpy at gmail.com (wesley chun) Date: Thu, 28 May 2009 11:24:53 -0700 Subject: [Tutor] Displaying range in 3.0.1 In-Reply-To: References: Message-ID: <78b3a9580905281124w7d4ab7dfm9bd7108100561933@mail.gmail.com> On Thu, May 28, 2009 at 10:22 AM, Gregory Morton wrote: > I've been reading this Python 3.0.1 tutorial(http://docs.python.org/3.0/tutorial/controlflow.html), and now I'm > stuck at the second example in 4.3. This is what the example says the output should look like: > > range(5, 10) > 5 through 9 > : > But what I receive instead is the same as what I input (i.e. range(5, > 10)?just returns?range(5, 10)). hi, and welcome to Python! with regards to your inquiry, you stopped reading too early. keep on going down that page. your question is answered starting with the paragraph that begins with, "A strange thing...." 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 From davea at ieee.org Thu May 28 23:17:09 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 28 May 2009 17:17:09 -0400 Subject: [Tutor] Error installing review board on windows In-Reply-To: References: Message-ID: <4A1EFF55.9010402@ieee.org> vishwajeet singh wrote: > > Hi List, > > I am trying to install review board on windows. It fails saying it could not > build PIL 1.1.6 because visual studio was not found on the system and but I > have already installed the PIL 1.1.6 using binaries available on the site > but review board fails to locate it and every time tries to download the PIL > and fails while building the same. > > error shown below > *error: Setup script exited with error: Python was built with Visual Studio > 2003;* > *extensions must be built with a compiler than can generate compatible > binaries. > Visual Studio 2003 was not found on this system. If you have Cygwin > installed, > you can try compiling with MingW32, by passing "-c mingw32" to setup.py. > > *I tried to build the PIL from source using cygwin but that also failed with > following error > * `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xa93): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xac2): undefined > reference to > `_imp__PyExc_IndexError' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xae5): undefined > reference to > `_imp__PyExc_TypeError' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd7c): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd84): undefined > reference to > `_imp___Py_NoneStruct' > build\temp.win32-2.5\Release\path.o:path.c:(.text+0xe01): undefined > reference to > `_imp__PyExc_AttributeError' > collect2: ld returned 1 exit status > error: command 'gcc' failed with exit status 1* > > Kindly help > You don't say just what version of Python you're using, but I'm guessing it's 2.5 You have to use the exact matching version of PIL, the one built for Python 2.5 If you look at http://www.pythonware.com/products/pil/, you can see there are multiple downloads for PIL 1.1.6. They are not interchangeable. This may not be your problem, but it's the first thing I'd check. From alan.gauld at btinternet.com Fri May 29 00:07:28 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 28 May 2009 23:07:28 +0100 Subject: [Tutor] Displaying range in 3.0.1 References: Message-ID: "Gregory Morton" wrote > I've been reading this Python 3.0.1 > tutorial(http://docs.python.org/3.0/tutorial/controlflow.html), Looks like a bug in the tutorial! In Python 3 you cannot just print a range it is now a generator. Notice that in the tutorial the range() calls are not shown at the >>> prompt. They are not valid commands, the tutorial is just illustrating the expected output from range for those values! To see them you need to explicitly convert it to a list: >>> list( range(0, 10, 3) ) [0, 3, 6, 9] >>> list( range(-10, -100, -30) ) [-10, -40, -70] HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From allen.fowler at yahoo.com Fri May 29 00:41:04 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Thu, 28 May 2009 15:41:04 -0700 (PDT) Subject: [Tutor] Mapping to an equivalent / similar object? Message-ID: <916298.49448.qm@web45608.mail.sp1.yahoo.com> Hello, Imagine the relationship between an Ice Cream retail store and the several manufactures that supply it's flavors. The retail store has a single list of flavors and any given flavor can be made by one or more manufactures. (Most are made by several.) The store's stock monitoring system will generate a list of Flavor() objects that need to be ordered. Say: Flavor('Vanilla'), or better yet, CanonicalFlavor('Vanilla') The store's ordering system takes as input a CanonicalFlavor('Vanilla') object that needs to be purchased, and must route this to a manufacturer's ordering system. The choice of which manufacture to use is based on external factors. However, each manufacturer's ordering system is different, and each expects a radically different variation of a Flavor object. (Different ingredients, parameters, etc. ) How should the mapping between the CanonicalFlavor('Vanilla') object and ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') objects be handled. (Or is this the wrong way to do this?) Thank you, :) From walksloud at gmail.com Fri May 29 02:43:13 2009 From: walksloud at gmail.com (Andre Walker-Loud) Date: Thu, 28 May 2009 20:43:13 -0400 Subject: [Tutor] creating interactive program with python Message-ID: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> Hi All, I am thinking of creating a data analysis suite with python, and I want it to be interactive - ie the program asks the user (me) for input, like which data file to use, how many parameters to minimize, etc. There are a few features I want it to have, like graphing, both the initial data as well as results of my analysis, etc. If you are familiar with it, Matlab is close to what I would like, a sort of all in one tool - but that is expensive commercial software. So I have a few questions. And I should also say I would classify myself as a medium level python scripter, but I have not used the more advanced programming features, and I am a beginning programmer. That being said, I am interested in expanding my python knowledge, especially getting into the more advanced features. Even though this is ultimately just a tool to do my job (limiting my time to play around and learn new things) I generally find python so easy to use (and fun) that I am interested in this little project of mine (and also it will help me sever my dependency on Mathematica, if you are familiar with that program). Thanks Andre 1) I am trying to trying to figure out how I would set up an interactive python script, however, I am having trouble googling this, since a search for "interactive python" just returns instructions on how to type "python" in a terminal to get an "interactive" session.... I have in mind a while loop that asks the user for arguments, and then some switch making sure I have passed enough sys.args to proceed, but I am not sure how to prompt the user for arguments and count them locally (not a global number of sys.argv which is what I am used to now). So any advice on where to look at an example of this would be great. 2) I have come across the "Scipy Superpack" http://macinscience.org/?page_id=6 which contains Scipy, Numpy, Matplotlib, iPython and PyMC. So it seems the installation would be convenient since it is all bundled together. Anyone have experience with this package? 3) I see iPython (which I have never used) is an enhanced interactive python shell. Perhaps this could do what I want - does anyone have experience with iPython? Even if iPython can do everything I want (and more I am sure) I would still like to write my own basic little interactive script to expand my python knowledge. 4) does anyone have strong recommendations for or against using matplotlib? Is it low maintenance for producing graphs, or does it take lots of user interaction? All the example pictures I see are very pretty, but it is not clear how much effort is needed to generate them - or is it just a "well know to those who know it well" thing? From alan.gauld at btinternet.com Fri May 29 03:27:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 29 May 2009 02:27:02 +0100 Subject: [Tutor] Mapping to an equivalent / similar object? References: <916298.49448.qm@web45608.mail.sp1.yahoo.com> Message-ID: "Allen Fowler" wrote > Imagine the relationship between an Ice Cream retail > store and the several manufactures that supply it's flavors. > The store's ordering system takes as input a CanonicalFlavor('Vanilla') > object that needs to be purchased, and must route this to a > manufacturer's ordering system. > The choice of which manufacture to use is based on external factors. > > However, each manufacturer's ordering system is different, > and each expects a radically different variation of a Flavor object. > (Different ingredients, parameters, etc. ) The difference between objects is measeured by their interfaces not their data so provided the supplier objects all use the same operations of a Flavor then its not too bad. You can either create subclasses of Flavor for each manufacturer that retuirns the requisite list of attributes (but requires ongoing changes for every new manufacturer - eek!) or you can write a method of Flavor that takes a list (or dictionary?) of required attributes and returns the values. It can also raise exceptions if asked for unsupported attributes, or return None, in which case the Supplier object provides a suitable default. This way you can limit the changes for a new supplier. You might also be able to data drive the Supplier object to make the required list of attributes configurable(via a database or file?). Then you only need subclasses of Supplier for the actual interface types (EDI, v FTP, v http v email etc). > How should the mapping between the CanonicalFlavor('Vanilla') > object and ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') > objects be handled. If the mapping is one to many I don;t think you need to maintain it - at least not in the objects. Just pass the Flavor object to the supplier object. Let the supplier query the Flavor for the data it needs. If the choice of supplier was driven by Flavor characteristics then the selection method would be in the Flavour and then you'd need to keep a list of suppliers in the Flavor. But since the supplier is chosen from external factors the Flavor doesn't need to know about its supplier. And the supplier only needs to know about how to query a flavour. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri May 29 03:31:37 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 29 May 2009 02:31:37 +0100 Subject: [Tutor] creating interactive program with python References: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> Message-ID: "Andre Walker-Loud" wrote > 1) I am trying to trying to figure out how I would set up an > interactive python script, however, I am having trouble googling this, > since a search for "interactive python" just returns instructions on > how to type "python" in a terminal to get an "interactive" session.... Check out the Talking to the User topic in my tutorial. It covers interactive prompting and use of command line arguments. > 3) I see iPython (which I have never used) is an enhanced interactive > python shell. Perhaps this could do what I want - does anyone have > experience with iPython? Several here use it but that is a development tool not the final application. However you might like to investigate the cmd module... It could be very helpful. > 4) does anyone have strong recommendations for or against using > matplotlib? Is it low maintenance for producing graphs, or does it > take lots of user interaction? All the example pictures I see are > very pretty, but it is not clear how much effort is needed to generate > them - or is it just a "well know to those who know it well" thing? I haven't used it seriously but those that do seem to find it works OK. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mobiledreamers at gmail.com Fri May 29 03:47:44 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Thu, 28 May 2009 18:47:44 -0700 Subject: [Tutor] problems with Cheetah base class being in templates directory Message-ID: *How do we setup Cheetah so it runs with all templates in the templates directory and all code in the .. directory code.py* production=True if not production: try:web.render('mafbase.tmpl', None, True, 'mafbase') except:pass else: from templates import mafbase templates/mafbase.tmpl templates/mafbase.py try:web.render('mafbase.tmpl', None, True, 'mafbase') This works but is very slow to load and causes timeout errors from templates import mafbase This doesnt work return self._delegate(fn, self.fvars, args) File "/home/mark/work/common/web/application.py", line 411, in _delegate return handle_class(cls) File "/home/mark/work/common/web/application.py", line 386, in handle_class return tocall(*args) File "user.py", line 262, in proxyfunc return func(self, *args, **kw) File "/home/mark/work/pop/code.py", line 1796, in GET return web.render('subclass.html') File "/home/mark/work/common/web/cheetah.py", line 104, in render return str(compiled_tmpl) File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__ def __str__(self): return getattr(self, mainMethName)() File "mafbase.py", line 634, in respond File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude nestedTemplateClass = compiler.compile(source=source,file=file) File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile fileHash = str(hash(file))+str(os.path.getmtime(file)) File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime return os.stat(filename).st_mtime OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html' -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From walksloud at gmail.com Fri May 29 04:24:13 2009 From: walksloud at gmail.com (Andre Walker-Loud) Date: Thu, 28 May 2009 22:24:13 -0400 Subject: [Tutor] creating interactive program with python In-Reply-To: References: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> Message-ID: <0D4B1A16-0CC3-4091-BF94-7C7B11C82E56@gmail.com> Hi Alan, >> 1) I am trying to trying to figure out how I would set up an >> interactive python script, however, I am having trouble googling >> this, since a search for "interactive python" just returns >> instructions on how to type "python" in a terminal to get an >> "interactive" session.... > > Check out the Talking to the User topic in my tutorial. > > It covers interactive prompting and use of command line arguments. Wow! That was sooo easy, thanks! I was expecting to have to sweat for a while to figure it out. I really appreciate the multiple example style of your tutorial! >> 3) I see iPython (which I have never used) is an enhanced >> interactive python shell. Perhaps this could do what I want - >> does anyone have experience with iPython? > > Several here use it but that is a development tool not the final > application. However you might like to investigate the cmd module... > It could be very helpful. I will check it out. Thanks, Andre > > >> 4) does anyone have strong recommendations for or against using >> matplotlib? Is it low maintenance for producing graphs, or does >> it take lots of user interaction? All the example pictures I see >> are very pretty, but it is not clear how much effort is needed to >> generate them - or is it just a "well know to those who know it >> well" thing? > > I haven't used it seriously but those that do seem to find it works > OK. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From dextrous85 at gmail.com Fri May 29 08:16:32 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Fri, 29 May 2009 11:46:32 +0530 Subject: [Tutor] Error installing review board on windows In-Reply-To: <4A1EFF55.9010402@ieee.org> References: <4A1EFF55.9010402@ieee.org> Message-ID: <5487b3060905282316y134d7p482a12066faa7b69@mail.gmail.com> Yes I am using python 2.5 and I have corect version of PIL for python 2.5. Thanks for your help anyways. On Fri, May 29, 2009 at 2:47 AM, Dave Angel wrote: > vishwajeet singh wrote: > > >> Hi List, >> >> I am trying to install review board on windows. It fails saying it could >> not >> >> build PIL 1.1.6 because visual studio was not found on the system and but >> I >> have already installed the PIL 1.1.6 using binaries available on the site >> but review board fails to locate it and every time tries to download the >> PIL >> and fails while building the same. >> >> error shown below >> *error: Setup script exited with error: Python was built with Visual >> Studio >> 2003;* >> *extensions must be built with a compiler than can generate compatible >> binaries. >> Visual Studio 2003 was not found on this system. If you have Cygwin >> installed, >> you can try compiling with MingW32, by passing "-c mingw32" to setup.py. >> >> *I tried to build the PIL from source using cygwin but that also failed >> with >> following error >> * `_imp___Py_NoneStruct' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xa93): undefined >> reference to >> `_imp___Py_NoneStruct' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xac2): undefined >> reference to >> `_imp__PyExc_IndexError' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xae5): undefined >> reference to >> `_imp__PyExc_TypeError' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd7c): undefined >> reference to >> `_imp___Py_NoneStruct' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xd84): undefined >> reference to >> `_imp___Py_NoneStruct' >> build\temp.win32-2.5\Release\path.o:path.c:(.text+0xe01): undefined >> reference to >> `_imp__PyExc_AttributeError' >> collect2: ld returned 1 exit status >> error: command 'gcc' failed with exit status 1* >> >> Kindly help >> >> > > You don't say just what version of Python you're using, but I'm guessing > it's 2.5 You have to use the exact matching version of PIL, the one built > for Python 2.5 > > If you look at http://www.pythonware.com/products/pil/, you can see there > are multiple downloads for PIL 1.1.6. They are not interchangeable. > > This may not be your problem, but it's the first thing I'd check. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri May 29 11:07:16 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 29 May 2009 19:07:16 +1000 Subject: [Tutor] missing first argument on subprocess.Popen w/ executable Message-ID: I'm wondering whether I have misunderstood the documentation or this is a bug in either the documentation or the python: Python 2.5.4 (r254:67916, May 18 2009, 20:53:31) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import subprocess >>> subprocess.Popen(['a', 'b', 'c', 'd'], executable='echo') b c d shouldn't the output be a b c d where has the first argument (a) gone to? Normally the first argument in arg is the executable name, however we may also specify it as executable argument instead; but what's the rationale for zapping the first argument when executable is present? I want to use executable argument because the argument list will be dynamically generated (list of filenames) and having to prepend the executable to the list is ugly if you're using comprehension. Or is it because I'm using a (rather) old version of python (the default python from Gentoo amd64) From a.t.hofkamp at tue.nl Fri May 29 11:21:02 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Fri, 29 May 2009 11:21:02 +0200 Subject: [Tutor] missing first argument on subprocess.Popen w/ executable In-Reply-To: References: Message-ID: <4A1FA8FE.6040802@tue.nl> Lie Ryan wrote: > I'm wondering whether I have misunderstood the documentation or this is > a bug in either the documentation or the python: > > Python 2.5.4 (r254:67916, May 18 2009, 20:53:31) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import subprocess >>>> subprocess.Popen(['a', 'b', 'c', 'd'], executable='echo') > > b c d > > shouldn't the output be > a b c d > > where has the first argument (a) gone to? Under Unix, it is the displayed executable name eg in 'ps': >>> import subprocess >>> subprocess.Popen(['60', '60'], executable='/bin/sleep') While it is running, 'ps' lists USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND user 26056 0.1 0.0 8140 3480 pts/11 S+ 11:13 0:00 python user 26059 0.0 0.0 5796 560 pts/11 S+ 11:14 0:00 60 60 > Normally the first argument in arg is the executable name, however we > may also specify it as executable argument instead; but what's the > rationale for zapping the first argument when executable is present? It is still the name of the executable, the value is just not used for searching the program, rather just displayed. > Or is it because I'm using a (rather) old version of python (the default > python from Gentoo amd64) Nope, it is feature of the OS. No way around it unless you patch the kernel. Albert From lie.1296 at gmail.com Fri May 29 11:40:18 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 29 May 2009 19:40:18 +1000 Subject: [Tutor] missing first argument on subprocess.Popen w/ executable In-Reply-To: <4A1FA8FE.6040802@tue.nl> References: <4A1FA8FE.6040802@tue.nl> Message-ID: A.T.Hofkamp wrote: > Lie Ryan wrote: >> I'm wondering whether I have misunderstood the documentation or this is >> a bug in either the documentation or the python: >> >> Python 2.5.4 (r254:67916, May 18 2009, 20:53:31) >> [GCC 4.3.2] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import subprocess >>>>> subprocess.Popen(['a', 'b', 'c', 'd'], executable='echo') >> >> b c d >> >> shouldn't the output be >> a b c d >> >> where has the first argument (a) gone to? > > Under Unix, it is the displayed executable name eg in 'ps': > >>>> import subprocess >>>> subprocess.Popen(['60', '60'], executable='/bin/sleep') > > > > While it is running, 'ps' lists > > USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND > user 26056 0.1 0.0 8140 3480 pts/11 S+ 11:13 0:00 python > user 26059 0.0 0.0 5796 560 pts/11 S+ 11:14 0:00 60 60 Now THAT is bizarre. Any word on Windows? >> Normally the first argument in arg is the executable name, however we >> may also specify it as executable argument instead; but what's the >> rationale for zapping the first argument when executable is present? > > It is still the name of the executable, the value is just not used for > searching the program, rather just displayed. > >> Or is it because I'm using a (rather) old version of python (the default >> python from Gentoo amd64) > > Nope, it is feature of the OS. No way around it unless you patch the > kernel. If it's indeed a feature, shouldn't it be mentioned in the documentation (or at least a warning for unsuspecting user)? Or even better, shouldn't a separate argument (e.g. displayed_executable_name) be used? Either way, I'll try to put this to the bug tracker... From kent37 at tds.net Fri May 29 12:54:31 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 29 May 2009 06:54:31 -0400 Subject: [Tutor] creating interactive program with python In-Reply-To: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> References: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> Message-ID: <1c2a2c590905290354u412f3e9brb769800ce8c548a6@mail.gmail.com> On Thu, May 28, 2009 at 8:43 PM, Andre Walker-Loud wrote: > Hi All, > > I am thinking of creating a data analysis suite with python, and I want it > to be interactive - ie the program asks the user (me) for input, like which > data file to use, how many parameters to minimize, etc. ?There are a few > features I want it to have, like graphing, both the initial data as well as > results of my analysis, etc. ?If you are familiar with it, Matlab is close > to what I would like, a sort of all in one tool - but that is expensive > commercial software. You might be interested in Sage: http://www.sagemath.org/index.html > 4) does anyone have strong recommendations for or against using matplotlib? > ?Is it low maintenance for producing graphs, or does it take lots of user > interaction? ?All the example pictures I see are very pretty, but it is not > clear how much effort is needed to generate them - or is it just a "well > know to those who know it well" thing? I have had good success with matlab. It doesn't take a lot of commands to create a graph. The documentation is extensive but a bit opaque - it can be hard to find out how to do something. The examples in the matplotlib gallery all have source code so you can judge for yourself how hard it is; http://matplotlib.sourceforge.net/gallery.html Kent From jsseabold at gmail.com Fri May 29 15:41:44 2009 From: jsseabold at gmail.com (Skipper Seabold) Date: Fri, 29 May 2009 08:41:44 -0500 Subject: [Tutor] creating interactive program with python In-Reply-To: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> References: <1E25BD9F-CFC3-4478-BB2E-25DC267007A0@gmail.com> Message-ID: On Thu, May 28, 2009 at 7:43 PM, Andre Walker-Loud wrote: > Hi All, > > I am thinking of creating a data analysis suite with python, and I want it > to be interactive - ie the program asks the user (me) for input, like which > data file to use, how many parameters to minimize, etc. ?There are a few > features I want it to have, like graphing, both the initial data as well as > results of my analysis, etc. ?If you are familiar with it, Matlab is close > to what I would like, a sort of all in one tool - but that is expensive > commercial software. > > So I have a few questions. ?And I should also say I would classify myself as > a medium level python scripter, but I have not used the more advanced > programming features, and I am a beginning programmer. ?That being said, I > am interested in expanding my python knowledge, especially getting into the > more advanced features. ?Even though this is ultimately just a tool to do my > job (limiting my time to play around and learn new things) I generally find > python so easy to use (and fun) that I am interested in this little project > of mine (and also it will help me sever my dependency on Mathematica, if you > are familiar with that program). > > Thanks > > Andre > > > > 1) I am trying to trying to figure out how I would set up an interactive > python script, however, I am having trouble googling this, since a search > for "interactive python" just returns instructions on how to type "python" > in a terminal to get an "interactive" session.... > > I have in mind a while loop that asks the user for arguments, and then some > switch making sure I have passed enough sys.args to proceed, but I am not > sure how to prompt the user for arguments and count them locally (not a > global number of sys.argv which is what I am used to now). ?So any advice on > where to look at an example of this would be great. > > 2) I have come across the "Scipy Superpack" > > http://macinscience.org/?page_id=6 > > which contains Scipy, Numpy, Matplotlib, iPython and PyMC. ?So it seems the > installation would be convenient since it is all bundled together. ?Anyone > have experience with this package? > > > 3) I see iPython (which I have never used) is an enhanced interactive python > shell. ?Perhaps this could do what I want - does anyone have experience with > iPython? ?Even if iPython can do everything I want (and more I am sure) I > would still like to write my own basic little interactive script to expand > my python knowledge. > You might want to have a look at embedding ipython. Here is one take (more at the link below): Note that it plays well with matplotlib and has some *really* convenient features for doing scientific computing/ data analysis with Python. completion and being able to issue commands to the system shell are worth the (free) price of admission alone for me, but that doesn't even begin to scratch the surface of iPython's nice features. Have a look at the documentation and the cookbook for tricks and embedding here: > > 4) does anyone have strong recommendations for or against using matplotlib? > ?Is it low maintenance for producing graphs, or does it take lots of user > interaction? ?All the example pictures I see are very pretty, but it is not > clear how much effort is needed to generate them - or is it just a "well > know to those who know it well" thing? > I have had plenty of luck with iPython, NumPy/SciPy, and matplotlib. You can see some examples here: For matplotlib: For Data analysis with SciPy in general (graphical output is not solely in matplotlib: Cheers, Skipper From nephish at gmail.com Fri May 29 23:27:14 2009 From: nephish at gmail.com (shawn bright) Date: Fri, 29 May 2009 16:27:14 -0500 Subject: [Tutor] how to get variable from an external script or program Message-ID: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> Hey all I have a small program that when run from the command line, will return a certain value for an arguement. Like this: > mfetchz 45 > 45j so the program is mfetchz and the argument is 45 i know i can call the program with os.system("mfetchz 45") but how do i get the return? The OS is linux, if that matters thanks sk From vinces1979 at gmail.com Fri May 29 23:36:44 2009 From: vinces1979 at gmail.com (vince spicer) Date: Fri, 29 May 2009 15:36:44 -0600 Subject: [Tutor] how to get variable from an external script or program In-Reply-To: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> Message-ID: <1e53c510905291436q45b1c754pac89bc1faf789f91@mail.gmail.com> import commands output = commands.getout("ls -lah") Vince On Fri, May 29, 2009 at 3:27 PM, shawn bright wrote: > Hey all > > I have a small program that when run from the command line, will > return a certain value for an arguement. Like this: > > > mfetchz 45 > > 45j > > so the program is mfetchz and the argument is 45 > > i know i can call the program with os.system("mfetchz 45") > but how do i get the return? > > The OS is linux, if that matters > > thanks > > sk > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vinces1979 at gmail.com Fri May 29 23:37:15 2009 From: vinces1979 at gmail.com (vince spicer) Date: Fri, 29 May 2009 15:37:15 -0600 Subject: [Tutor] how to get variable from an external script or program In-Reply-To: <1e53c510905291436q45b1c754pac89bc1faf789f91@mail.gmail.com> References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> <1e53c510905291436q45b1c754pac89bc1faf789f91@mail.gmail.com> Message-ID: <1e53c510905291437g605135e6qbb31fdaada21ba5f@mail.gmail.com> Sorry *output import commands > output = commands.getoutput("ls -lah") > Vince > > > On Fri, May 29, 2009 at 3:27 PM, shawn bright wrote: > >> Hey all >> >> I have a small program that when run from the command line, will >> return a certain value for an arguement. Like this: >> >> > mfetchz 45 >> > 45j >> >> so the program is mfetchz and the argument is 45 >> >> i know i can call the program with os.system("mfetchz 45") >> but how do i get the return? >> >> The OS is linux, if that matters >> >> thanks >> >> sk >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Fri May 29 23:39:23 2009 From: srilyk at gmail.com (W W) Date: Fri, 29 May 2009 16:39:23 -0500 Subject: [Tutor] how to get variable from an external script or program In-Reply-To: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> Message-ID: <333efb450905291439m504cb063l261a1e77db2326a8@mail.gmail.com> On Fri, May 29, 2009 at 4:27 PM, shawn bright wrote: > Hey all > > I have a small program that when run from the command line, will > return a certain value for an arguement. Like this: > > > mfetchz 45 > > 45j > > so the program is mfetchz and the argument is 45 > > i know i can call the program with os.system("mfetchz 45") > but how do i get the return? > > The OS is linux, if that matters > use subprocess module: import subprocess op = subprocess.Popen(['mfetchz', '45'], stdout=subprocess.PIPE) for line in op.stdout: print line HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From nephish at gmail.com Fri May 29 23:49:13 2009 From: nephish at gmail.com (shawn bright) Date: Fri, 29 May 2009 16:49:13 -0500 Subject: [Tutor] how to get variable from an external script or program In-Reply-To: <333efb450905291439m504cb063l261a1e77db2326a8@mail.gmail.com> References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> <333efb450905291439m504cb063l261a1e77db2326a8@mail.gmail.com> Message-ID: <384c93600905291449s2bd100cdmc07c7b1f67dd3886@mail.gmail.com> kick butt, gents, thanks a lot sk On Fri, May 29, 2009 at 4:39 PM, W W wrote: > On Fri, May 29, 2009 at 4:27 PM, shawn bright wrote: >> >> Hey all >> >> I have a small program that when run from the command line, will >> return a certain value for an arguement. Like this: >> >> > mfetchz 45 >> > 45j >> >> so the program is mfetchz and the argument is 45 >> >> i know i can call the program with os.system("mfetchz 45") >> but how do i get the return? >> >> The OS is linux, if that matters > > use subprocess module: > > import subprocess > > op = subprocess.Popen(['mfetchz', '45'], stdout=subprocess.PIPE) > > for line in op.stdout: > ??? print line > > > HTH, > Wayne > From alan.gauld at btinternet.com Sat May 30 00:09:35 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 29 May 2009 23:09:35 +0100 Subject: [Tutor] how to get variable from an external script or program References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> <1e53c510905291436q45b1c754pac89bc1faf789f91@mail.gmail.com> Message-ID: "vince spicer" wrote > import commands > output = commands.getout("ls -lah") There are many ways to do this in Python including os.popen, commands and subprocess. But subprocess is the "officially correct" version, the others are deprecated and could theoretically disappear in a future version of Python. (But not soon I suspect because of the amount of legacy code that uses it!) But its definitely worth weaning oneself onto subprocess even though it is initially harder work. In the end the superior flexibility and consistency of approach pay back. Alan G. From nephish at gmail.com Sat May 30 00:30:31 2009 From: nephish at gmail.com (shawn bright) Date: Fri, 29 May 2009 17:30:31 -0500 Subject: [Tutor] how to get variable from an external script or program In-Reply-To: References: <384c93600905291427x277f5bd8qc9497589cf3cb326@mail.gmail.com> <1e53c510905291436q45b1c754pac89bc1faf789f91@mail.gmail.com> Message-ID: <384c93600905291530r7beac80el393ced0d930e6afc@mail.gmail.com> cool, thanks again sk On Fri, May 29, 2009 at 5:09 PM, Alan Gauld wrote: > > "vince spicer" wrote >> >> import commands >> output = commands.getout("ls -lah") > > There are many ways to do this in Python including os.popen, commands and > subprocess. > > But subprocess is the "officially correct" version, the others are > deprecated and could theoretically disappear in a future version of Python. > (But not soon I suspect because of the amount of legacy code that uses it!) > > But its definitely worth weaning oneself onto subprocess even though it is > initially harder work. In the end the superior flexibility and consistency > of approach pay back. > > Alan G. > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From haztang17 at gmail.com Sat May 30 02:48:05 2009 From: haztang17 at gmail.com (Hi) Date: Fri, 29 May 2009 17:48:05 -0700 Subject: [Tutor] Docking Windows using Python Message-ID: My program will have two windows. I want to be able to dock the two so they are side by side. I am wondering if this is doable in Python. If so, could someone lead me to the right direction on where to look? Thank you very much. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tyetheczar at hotmail.com Fri May 29 16:46:17 2009 From: tyetheczar at hotmail.com (Gregory Morton) Date: Fri, 29 May 2009 09:46:17 -0500 Subject: [Tutor] (no subject) Message-ID: I'm having a problem understanding how this code works in lesson 4.4 on the python tutorial. Can anyone explain it in easy-to-digest details? I kind of know what most of the stuff means, but I can't comprehend how it all works in unison. That, and I'm having a hard time understanding what break does. for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... # loop fell through without finding a factor ... print(n, 'is a prime number') ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3Thanks for all your help,Greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Sat May 30 03:14:39 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 30 May 2009 11:14:39 +1000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: Gregory Morton wrote: > I'm having a problem understanding how this code works in lesson 4.4 on > the python tutorial. Can anyone explain it in easy-to-digest details? I > kind of know what most of the stuff means, but I can't comprehend how it > all works in unison. That, and I'm having a hard time understanding what > break does. break exits the for loop immediately even if there is still items that need to be looped. The prime finding program's algorithm is by trial division; it tests whether the number is divisible from 2 to the number itself - 1 (since a prime number is defined as only divisible by 1 and the number itself). The for-loop's else suite is executed iff the loop exits normally (i.e. when range(2, n) is exhausted, i.e. not because of break). > > for n in range(2, 10): > .... for x in range(2, n): > .... if n % x == 0: > .... print(n, 'equals', x, '*', n//x) > .... break > .... else: > .... # loop fell through without finding a factor > .... print(n, 'is a prime number') > .... > 2 is a prime number > 3 is a prime number > 4 equals 2 * 2 > 5 is a prime number > 6 equals 2 * 3 > 7 is a prime number > 8 equals 2 * 4 > 9 equals 3 * 3 > > Thanks for all your help, > Greg > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From srilyk at gmail.com Sat May 30 04:29:49 2009 From: srilyk at gmail.com (W W) Date: Fri, 29 May 2009 21:29:49 -0500 Subject: [Tutor] Docking Windows using Python In-Reply-To: References: Message-ID: <333efb450905291929w1d92892o7f01406aa9ac31e3@mail.gmail.com> On Fri, May 29, 2009 at 7:48 PM, Hi wrote: > My program will have two windows. I want to be able to dock the two so they > are side by side. I am wondering if this is doable in Python. If so, could > someone lead me to the right direction on where to look? Thank you very > much. > I think you could really use any GUI - it really all depends on how you want to do it. I'm not 100% sure about having two windows with Tkinter. But you could fairly easily (I think) hide copies of widgets inside the main window and show them only after the second window is docked, then hide that window. There are probably plenty of other ways to do it, that's just the first that popped into my mind with my limited knowledge. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat May 30 09:58:17 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 30 May 2009 08:58:17 +0100 Subject: [Tutor] (no subject) References: Message-ID: "Gregory Morton" wrote > I'm having a problem understanding how this code works > in lesson 4.4 on the python tutorial. Can you already program in another language? Say VB, Java or PHP? If not you would be better off with a different tutorial since rthe official one is really targeted at experienced programmers moving to Python from another language. This page offers several choices, including mine: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > Can anyone explain it in easy-to-digest details? I kind of know > what most of the stuff means, but I can't comprehend how it > all works in unison. > That, and I'm having a hard time understanding what break does. OK the overview is that the outer loop tests each number from 2 to 9 and tests to see if it is a prime number or not. The test it uses is to divide the number by each number from 2 to the number itself. If it finds a number that divides exactly, ie the remainder is zero then it stops, or breaks, the test and moves on to the next number from the outer loop. Line by line: > for n in range(2, 10): This is the outer loop selecting each number in the range from 2 to 9. ... for x in range(2, n): This is the test loop dividing by each number from 2 to n. ... if n % x == 0: If n divided by x has a remainder of zero ... print(n, 'equals', x, '*', n//x) ... break then it is not a prime number so we stop the test by breaking out of the loop. and going back to the top for the next number to test. ... else: If we reach the end of the loop without breaking ... # loop fell through without finding a factor ... print(n, 'is a prime number') It must be a prime number so report it as such Then go back to the top for the next number to test. You will find more about loops in the Loops topic of my tutorial. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat May 30 10:07:15 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 30 May 2009 09:07:15 +0100 Subject: [Tutor] Docking Windows using Python References: Message-ID: "Hi" wrote > My program will have two windows. I want to be able to dock the two so > they > are side by side. I am wondering if this is doable in Python. Yes it is doable but how difficult it is will depend on the GUI toolkit you use. Most GUI programming environments nowadays (eg. .NET, Delphi etc) include dockable windows as a standard widget. However, Tkinter, which is the standard GUI tool in Python, does not, so you would need to program it yourself. There are several other GUI toolkits that you could use and I'm not sure which, if any, support docking "out of the box", but I suspect that PyQt and PyGTk both will, and possibly wxPython. > someone lead me to the right direction on where to look? The question uis not really about Python it's about GUI programming in general. If you can't find a toolkit that suopports it natively then you will have to do quite a lot of programming to make it work, probably defining your own custom widget. That's a lot of work. Your best bet is to ask the question on the various toolkit specific forums and mailing lists since there will likely be people who have done it already. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From vy0123 at gmail.com Sat May 30 11:52:00 2009 From: vy0123 at gmail.com (vy) Date: Sat, 30 May 2009 19:52:00 +1000 Subject: [Tutor] lession 4.4 on finding prime number was: (no subject) Message-ID: <561420f60905300252q450dbe6bkdbbc349d6f95f726@mail.gmail.com> > Date: Fri, 29 May 2009 09:46:17 -0500 > From: Gregory Morton > To: Tutor Newsgroup Python > Subject: [Tutor] (no subject) > Message-ID: > Content-Type: text/plain; charset="iso-8859-1" > > > I'm having a problem understanding how this code works in lesson 4.4 on the python tutorial. Can anyone explain it in easy-to-digest details? I kind of know what most of the stuff means, but I can't comprehend how it all works in unison. That, and I'm having a hard time understanding what break does. There is an outer for-loop and an inner for-loop, the pair of numbers as both for-loops are stepped can be shown by: >>> for outer in range(2,10): ... for inner in range(2,outer): ... print outer,inner 3 2 4 2 4 3 5 2 5 3 5 4 6 2 6 3 6 4 6 5 7 2 7 3 7 4 7 5 7 6 8 2 8 3 8 4 8 5 8 6 8 7 9 2 9 3 9 4 9 5 9 6 9 7 9 8 ... ... ... ## [in the example given] ## n steps through the out-loop and x steps through the inner loop; ## intuitively, the outer loop cycles once and the inner loop cycles ## to a size limit (in growing cycles) for each step of n of the outer loop ## the `break' means to end the cycle through the inner loop ## as a consequence of ## the if conditional eliminating the n value as a composite number ## then the next n is used and the inner loop starts from 2 again ## if the if conditional is never met by the inner loop; that n value is considered ## a prime number > for n in range(2, 10): > ... ? ? for x in range(2, n): > ... ? ? ? ? if n % x == 0: > ... ? ? ? ? ? ? print(n, 'equals', x, '*', n//x) > ... ? ? ? ? ? ? break > ... ? ? else: > ... ? ? ? ? # loop fell through without finding a factor > ... ? ? ? ? print(n, 'is a prime number') > ... > 2 is a prime number > 3 is a prime number > 4 equals 2 * 2 > 5 is a prime number > 6 equals 2 * 3 > 7 is a prime number > 8 equals 2 * 4 > 9 equals 3 * 3Thanks for all your help,Greg Hope that helps. -- Van Ly From rnrcreid at yahoo.com Sat May 30 15:27:31 2009 From: rnrcreid at yahoo.com (Doug Reid) Date: Sat, 30 May 2009 06:27:31 -0700 (PDT) Subject: [Tutor] Using a list Message-ID: <569519.87584.qm@web45709.mail.sp1.yahoo.com> The tutorial I'm using is discussing list, tuples, and dictionaries.? An exercise at the end wants me to do this: ? Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength,Stamina, Wisdom, and Dexterity. The player should be able to spend points from the pool on any attribute and should also be able to take points from an attribute and put them back into the pool. ? I'm wondering if I'm doing this right or is their a different more practical way of accomplishing this. I tend to over complicate things sometimes.? This is how I've started it and any help pointing me in the right direction or of a different way of doing this will be apprectiated. ? Thanks Doug: ? #user menu def menu(): ??? choice=None ??? print'''\n 0. Quit 1. Display attributes 2. Add points to attributes 3. Subtract opints from attributes''' <--not active yet ??? print'Remaining points=',points ??? ??? while choice not in('0','1','2','3 '): ??????? choice=raw_input('\nWhat would you like to do? Enter number of option.\n') ??? return choice #variables used dexterity=0 strength=0 stamina=0 wisdom=0 allot=None creating=True change=0 points=30 #list of attributes and their values attributes=['dexterity=',dexterity ,'strength=',strength, 'stamina=',stamina, 'wisdom=',wisdom] #creation loop while creating: ??? ??? choice=menu() ??? if choice=='0': ??????? print '\nThank you for using character creator.' ??????? end=raw_input('\n Press enter to end.') ??????? creating=False ??? elif choice=='1':#prints out list of attributes and their values ??????? for entry in attributes: ??????????? print entry, ??? elif choice=='2': ??????? allot=raw_input('''What attribute would you like to change? Enter dex for dexterity, str for strength, etc. ''').lower() ??????? if allot=='dex': ??????????? change=int(raw_input('How many points do you wish to allot? ')) ??????????? attributes[1]=dexterity=+change ??????????? points=points-change ??????? if allot=='str': ??????????? change=int(raw_input('How many points do you wish to allot? ')) ??????????? attributes[3]=strength=+change ??????????? points=points-change ??????? if allot=='stam': ??????????? change=int(raw_input('How many points do you wish to allot? ')) ??????????? attributes[5]=stamina=+change ??????????? points=points-change ??????? if allot=='wis': ??????????? change=int(raw_input('How many points do you wish to allot? ')) ??????????? attributes[7]=wisdome=+change ??????????? points=points-change ??????????? ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From xboxmuncher at gmail.com Sat May 30 16:20:38 2009 From: xboxmuncher at gmail.com (xbmuncher) Date: Sat, 30 May 2009 10:20:38 -0400 Subject: [Tutor] Why does it display traceback info here and not there? Message-ID: I have a script called: script.py and here are the contents: h = open('file2.dat', 'rb') data = h.read(1120) h.close() print(data) input("Press ENTER to exit") I am editing it in Notepad++ and I set it up so I can run in on python.exe (I have python30) from the program, it sends these parameters: C:\Python30\python.exe pathToFile\folder\script.py Well when I run the script via this method, the screen flashes and quickly disappears, so there must be some kind of error. I used screen capture to try to capture what happened so I can read the message, this is what I got: Traceback (most recent call last): File "C:\path to file\script.py", line 1, in h = open('file2.dat', 'rb') File "C:\Python30\lib\io.py", line 278, in __new__ return open(*(args, **kwargs) File "C:\Python30\lib\io.py", line 222, in open closefd) File "C:\Python30\lib\io.py", line 619, in __init__ The WEIRD thing is, is that I can double click the same unchanged script.py file and it will run from the same program python.exe and it will display this output: \xb7\lots of these types of \xb codes Press ENTER to exit So my question is, why does it have different output run from the same python.exe program when the script remains the same. I'd like to called it to run from notepad++ out of convenience just as it runs when I double click on it.. but i'm getting different behavior and I dont know why... Maybe I can turn off traceback info? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sat May 30 17:29:33 2009 From: bgailer at gmail.com (bob gailer) Date: Sat, 30 May 2009 11:29:33 -0400 Subject: [Tutor] Why does it display traceback info here and not there? In-Reply-To: References: Message-ID: <4A2150DD.9040307@gmail.com> xbmuncher wrote: > I have a script called: script.py and here are the contents: > > h = open('file2.dat', 'rb') > data = h.read(1120) > h.close() > print(data) > > input("Press ENTER to exit") > > > I am editing it in Notepad++ and I set it up so I can run in on > python.exe (I have python30) from the program, it sends these parameters: > C:\Python30\python.exe pathToFile\folder\script.py > > Well when I run the script via this method, the screen flashes and > quickly disappears, so there must be some kind of error. I used screen > capture to try to capture what happened so I can read the message, > this is what I got: > > Traceback (most recent call last): > File "C:\path to file\script.py", line 1, in > > h = open('file2.dat', 'rb') > File "C:\Python30\lib\io.py", line 278, in __new__ > return open(*(args, **kwargs) > File "C:\Python30\lib\io.py", line 222, in open > closefd) > File "C:\Python30\lib\io.py", line 619, in __init__ > > > The WEIRD thing is, is that I can double click the same unchanged > script.py file and it will run from the same program python.exe and it > will display this output: > \xb7\lots of these types of \xb codes > Press ENTER to exit > > > So my question is, why does it have different output run from the same > python.exe program when the script remains the same. I'd like to > called it to run from notepad++ out of convenience just as it runs > when I double click on it.. but i'm getting different behavior and I > dont know why... > Maybe I can turn off traceback info? My guess is when you run from N++ there is a different current directory than when using dblclk. Run following script to verify that: import os print(os.getcwd()) -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Sat May 30 21:39:27 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 30 May 2009 20:39:27 +0100 Subject: [Tutor] Using a list References: <569519.87584.qm@web45709.mail.sp1.yahoo.com> Message-ID: "Doug Reid" wrote > The tutorial I'm using is discussing list, tuples, and dictionaries. > ... > four attributes: Strength,Stamina, Wisdom, and Dexterity. > The player should be able to spend points from the pool on > any attribute and should also be able to take points from > an attribute and put them back into the pool. How would you describe a dictionary? Can you see a way to apply it to the problem? > I'm wondering if I'm doing this right or is their a > different more practical way of accomplishing this. There are many many ways of doing this, most of them more practical than your approach. Why? Well, consider what happens with your approach if we want to have more than one player. Where would you store the attribute values? Would you create more global variables? But howe many players might there be? Can you see the problem? > I tend to over complicate things sometimes. Not so much overcomplicating but not thinking enough about how to leverage the power in the tools you have been given. #user menu #variables used dexterity=0 strength=0 stamina=0 wisdom=0 allot=None creating=True change=0 points=30 #list of attributes and their values attributes=['dexterity=',dexterity ,'strength=',strength, 'stamina=',stamina, 'wisdom=',wisdom] It is a list but not of attributes. Its a list containing strings and numbers alternating. The strings consist of an attribute name and an equals sign. But you know about a collection type that stores values against strings and allows you to retrieve those values using the string as a key. #creation loop while creating: choice=menu() if choice=='0': print '\nThank you for using character creator.' end=raw_input('\n Press enter to end.') creating=False elif choice=='1':#prints out list of attributes and their values for entry in attributes: print entry, This will print attribute= value attribute= value Not very pretty. But if you use a dictionary: for item in dictionary: print item, '=', dictionary[item] elif choice=='2': allot=raw_input('''What attribute would you like to change? Enter dex for dexterity, str for strength, etc. ''').lower() if allot=='dex': change=int(raw_input('How many points do you wish to allot? ')) attributes[1]=dexterity=+change points=points-change if allot=='str': change=int(raw_input('How many points do you wish to allot? ')) attributes[3]=strength=+change And this gets even more messy. Again think how it would look with a dictionary... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat May 30 21:45:29 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 30 May 2009 20:45:29 +0100 Subject: [Tutor] Why does it display traceback info here and not there? References: Message-ID: "xbmuncher" wrote > Traceback (most recent call last): > File "C:\path to file\script.py", line 1, in > > h = open('file2.dat', 'rb') > File "C:\Python30\lib\io.py", line 278, in __new__ > return open(*(args, **kwargs) > File "C:\Python30\lib\io.py", line 222, in open > closefd) > File "C:\Python30\lib\io.py", line 619, in __init__ > You don't have the full traceback here so we can't see the actual error condition but I suspect its a problem with paths. Python probably can't find the file because its looking in the wrong place. Does it worik if you give the fuill path to the file? > The WEIRD thing is, is that I can double click the same unchanged > script.py > file and it will run from the same program python.exe and it will display > this output: > \xb7\lots of these types of \xb codes > Press ENTER to exit > > So my question is, why does it have different output run from the same > python.exe program when the script remains the same. Probably Python is being called with the editors startup folder so cannot find your file. > Maybe I can turn off traceback info? You could but its not wise, the traceback means there is an error so turning the messages off won't help your program to run, you just won't get any clues as to why its not working! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Sat May 30 23:06:38 2009 From: david at abbottdavid.com (David) Date: Sat, 30 May 2009 17:06:38 -0400 Subject: [Tutor] Using a list In-Reply-To: References: <569519.87584.qm@web45709.mail.sp1.yahoo.com> Message-ID: <4A219FDE.5040300@abbottdavid.com> Alan Gauld wrote: > "Doug Reid" wrote > >> The tutorial I'm using is discussing list, tuples, and dictionaries. >> ... >> four attributes: Strength,Stamina, Wisdom, and Dexterity. >> The player should be able to spend points from the pool on >> any attribute and should also be able to take points from >> an attribute and put them back into the pool. > > How would you describe a dictionary? > Can you see a way to apply it to the problem? > > #list of attributes and their values > attributes=['dexterity=',dexterity ,'strength=',strength, > 'stamina=',stamina, 'wisdom=',wisdom] > > It is a list but not of attributes. Its a list containing strings and > numbers alternating. The strings consist of an attribute name > and an equals sign. But you know about a collection type that > stores values against strings and allows you to retrieve those > values using the string as a key. > > > #creation loop > while creating: > > choice=menu() > if choice=='0': > print '\nThank you for using character creator.' > end=raw_input('\n Press enter to end.') > creating=False > elif choice=='1':#prints out list of attributes and their values > for entry in attributes: > print entry, > > This will print > > attribute= > value > attribute= > value > > Not very pretty. But if you use a dictionary: > > for item in dictionary: > print item, '=', dictionary[item] > > elif choice=='2': > allot=raw_input('''What attribute would you like to change? > Enter dex for dexterity, str for strength, etc. ''').lower() > if allot=='dex': > change=int(raw_input('How many points do you wish to allot? ')) > attributes[1]=dexterity=+change > points=points-change > if allot=='str': > change=int(raw_input('How many points do you wish to allot? ')) > attributes[3]=strength=+change > > And this gets even more messy. > Again think how it would look with a dictionary... > I am also new to Python and never programed before. I use the questions from the Tutor list to learn. I put the attributes into dictionaries and came up with a little game between the user and the computer. Thank you Doug for the question. Here is my attempt; http://linuxcrazy.pastebin.com/m31d02824 -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From david at abbottdavid.com Sat May 30 23:18:22 2009 From: david at abbottdavid.com (David) Date: Sat, 30 May 2009 17:18:22 -0400 Subject: [Tutor] Using a list In-Reply-To: <4A219FDE.5040300@abbottdavid.com> References: <569519.87584.qm@web45709.mail.sp1.yahoo.com> <4A219FDE.5040300@abbottdavid.com> Message-ID: <4A21A29E.5070004@abbottdavid.com> David wrote: > Alan Gauld wrote: >> "Doug Reid" wrote >> >>> The tutorial I'm using is discussing list, tuples, and dictionaries. >>> ... >>> four attributes: Strength,Stamina, Wisdom, and Dexterity. >>> The player should be able to spend points from the pool on >>> any attribute and should also be able to take points from >>> an attribute and put them back into the pool. >> >> How would you describe a dictionary? >> Can you see a way to apply it to the problem? > >> >> #list of attributes and their values >> attributes=['dexterity=',dexterity ,'strength=',strength, >> 'stamina=',stamina, 'wisdom=',wisdom] >> >> It is a list but not of attributes. Its a list containing strings and >> numbers alternating. The strings consist of an attribute name >> and an equals sign. But you know about a collection type that >> stores values against strings and allows you to retrieve those >> values using the string as a key. >> >> >> #creation loop >> while creating: >> >> choice=menu() >> if choice=='0': >> print '\nThank you for using character creator.' >> end=raw_input('\n Press enter to end.') >> creating=False >> elif choice=='1':#prints out list of attributes and their values >> for entry in attributes: >> print entry, >> >> This will print >> >> attribute= >> value >> attribute= >> value >> >> Not very pretty. But if you use a dictionary: >> >> for item in dictionary: >> print item, '=', dictionary[item] >> >> elif choice=='2': >> allot=raw_input('''What attribute would you like to change? >> Enter dex for dexterity, str for strength, etc. ''').lower() >> if allot=='dex': >> change=int(raw_input('How many points do you wish to allot? ')) >> attributes[1]=dexterity=+change >> points=points-change >> if allot=='str': >> change=int(raw_input('How many points do you wish to allot? ')) >> attributes[3]=strength=+change >> >> And this gets even more messy. >> Again think how it would look with a dictionary... >> > I am also new to Python and never programed before. I use the questions > from the Tutor list to learn. I put the attributes into dictionaries and > came up with a little game between the user and the computer. Thank you > Doug for the question. > Here is my attempt; > http://linuxcrazy.pastebin.com/m31d02824 > Duh, I had the counter wrong, here is a better one :) http://linuxcrazy.pastebin.com/m2dc25067 -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From david at abbottdavid.com Sun May 31 00:59:39 2009 From: david at abbottdavid.com (David) Date: Sat, 30 May 2009 18:59:39 -0400 Subject: [Tutor] Class Tips Message-ID: <4A21BA5B.8070504@abbottdavid.com> I took this program that determines a fertilizer application rate; #!/usr/bin/python rate = float(raw_input("Enter rate i.e. (0.5) : ")) n = float(raw_input("Enter N from bag i.e. (.14) : ")) app = rate / n area = 43.560 acre = input("Enter total acre's to be treated: ") bag = input("Enter bag weight: ") print "You should apply", app * area * acre / bag, "bags." And converted it to class/object to learn how they work. Just looking for some pointers, if I did it correctly etc. #!/usr/bin/python class FertRate: def __init__(self, rate, nitrogen, acre, bag): self.area = 43.560 self.app = rate / (nitrogen / 100.00) self.acre = acre self.bag = bag def result(self): result = self.app * self.area * self.acre / self.bag print 'You should apply %0.2f bags.' % result def main(): rate, nitrogen, acre, bag = get_inputs() frate = FertRate(rate, nitrogen, acre, bag) frate.result() def get_inputs(): rate = float(raw_input('Enter Rate e.q., (0.5): ')) nitrogen = float(raw_input('Enter N From Bag e.q., (14): ')) acre = int(raw_input("Enter Total Acre's To Be Treated: ")) bag = int(raw_input('Enter Bag Weight: ')) return rate, nitrogen, acre, bag main() thanks, -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From cappy2112 at gmail.com Sun May 31 01:19:25 2009 From: cappy2112 at gmail.com (Tony Cappellini) Date: Sat, 30 May 2009 16:19:25 -0700 Subject: [Tutor] String formatting hex numbers Message-ID: <8249c4ac0905301619u2db75686l725dd0d4856c4d19@mail.gmail.com> I was looking at a this code which uses this code to dsiplay some hex numbers sys.stdout.write( "\r%-8s ... 0x%08X->0x%08X " % ( descr, long(startAddr), long(endAddr) ) The hex values are in this range 0x1BFFF400 to 1BFFF000 Why are these displayed with a leading negative sign (between the 0x and the actual number)- as seen below? 0x-1BFFF400 to 0x-1BFFF000 Thanks From alan.gauld at btinternet.com Sun May 31 02:15:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 May 2009 01:15:20 +0100 Subject: [Tutor] Class Tips References: <4A21BA5B.8070504@abbottdavid.com> Message-ID: "David" wrote >I took this program that determines a fertilizer application rate; > ... > And converted it to class/object to learn how they work. Just looking > for some pointers, if I did it correctly etc. For such a small program its hard to see what else you could have done. Technically itys not too bad, I've made a couple of suggestions below. However, leaving the fact that its an excercise I'd say that really this is just a function rather than a class. > class FertRate: > def __init__(self, rate, nitrogen, acre, bag): > self.area = 43.560 > self.app = rate / (nitrogen / 100.00) > self.acre = acre > self.bag = bag > > def result(self): > result = self.app * self.area * self.acre / self.bag > print 'You should apply %0.2f bags.' % result Its never a good idea to include printing in the same function as calculation. IT would be better to have two functions one to calculate and one to generate the printable output. (The actual printing is best done outside the class altogether, it improves the reusability. For example prints don't work in a GUI but a formatted string can be used. But even if you keep it as one function at lest return the result as a string rather than print it > def main(): > rate, nitrogen, acre, bag = get_inputs() > frate = FertRate(rate, nitrogen, acre, bag) This could have just been: frate = FertRate( get_inputs() ) which saves the need for the variables. > frate.result() So if you took my advice about returning a string this becomes print frate.result() But as I say you could just have made FertRate a function: print FertRate( get_inputs() ) and it would have been just as effective, classes really only start to be useful on slightly larger programs than this. But as an example of using a class its nearly OK, just the tweak of the return value to think about - and that applies to the function version too! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun May 31 02:24:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 May 2009 01:24:39 +0100 Subject: [Tutor] String formatting hex numbers References: <8249c4ac0905301619u2db75686l725dd0d4856c4d19@mail.gmail.com> Message-ID: "Tony Cappellini" wrote > sys.stdout.write( "\r%-8s ... 0x%08X->0x%08X " % ( descr, > long(startAddr), long(endAddr) ) > > Why are these displayed with a leading negative sign (between the 0x > and the actual number)- as seen below? > > 0x-1BFFF400 to 0x-1BFFF000 Because they are negative values and the leading 0x is a sting hard coded before the number. Let me rework the example to make that clearer: >>> "HEX:%0X" % -29 'HEX:-1D' >>> I replaced 0x with HEX: and useed a negative decimal number as input. Now you can see my string (HEX:) followed by the formatted hex number with the negative sign correctly placed before the digits. The problem is that Pythons string formatting doesn't know about prepending 0x to numbers. You need to use the hex function for that: >>> hex(-27) '-0x1b' >>> So the format string becomes: >>> "HEX:%s" % hex(-29) 'HEX:-0x1d' And all is as expected. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Sun May 31 03:20:26 2009 From: david at abbottdavid.com (David) Date: Sat, 30 May 2009 21:20:26 -0400 Subject: [Tutor] Class Tips In-Reply-To: References: <4A21BA5B.8070504@abbottdavid.com> Message-ID: <4A21DB5A.1020702@abbottdavid.com> Alan Gauld wrote: > > "David" wrote > >> I took this program that determines a fertilizer application rate; >> ... >> And converted it to class/object to learn how they work. Just looking >> for some pointers, if I did it correctly etc. > > For such a small program its hard to see what else you could have done. > Technically itys not too bad, I've made a couple of suggestions below. > However, leaving the fact that its an excercise I'd say that really this > is just a function rather than a class. > >> class FertRate: >> def __init__(self, rate, nitrogen, acre, bag): >> self.area = 43.560 >> self.app = rate / (nitrogen / 100.00) >> self.acre = acre >> self.bag = bag >> >> def result(self): >> result = self.app * self.area * self.acre / self.bag >> print 'You should apply %0.2f bags.' % result > > Its never a good idea to include printing in the same function as > calculation. IT would be better to have two functions one to calculate > and one to generate the printable output. (The actual printing is best > done outside the class altogether, it improves the reusability. For > example prints don't work in a GUI but a formatted string can be used. > > But even if you keep it as one function at lest return the result as a > string rather than print it > > >> def main(): >> rate, nitrogen, acre, bag = get_inputs() >> frate = FertRate(rate, nitrogen, acre, bag) > > This could have just been: > > frate = FertRate( get_inputs() ) > > which saves the need for the variables. > >> frate.result() > > So if you took my advice about returning a string this becomes > > print frate.result() > > But as I say you could just have made FertRate a function: > > print FertRate( get_inputs() ) > > and it would have been just as effective, classes really only start to > be useful on slightly larger programs than this. > > > But as an example of using a class its nearly OK, just the tweak of the > return value to think about - and that applies to the function version too! > > Thanks Alan, this works; def main(): rate, nitrogen, acre, bag = get_inputs() frate = FertRate(rate, nitrogen, acre, bag) result = frate.result() print 'You should apply %0.2f bags.' % result but I get an error here; def main(): frate = FertRate(get_inputs()) result = frate.result() print 'You should apply %0.2f bags.' % result Traceback (most recent call last): File "./fert_class.py", line 26, in main() File "./fert_class.py", line 15, in main frate = FertRate(get_inputs()) TypeError: __init__() takes exactly 5 arguments (2 given) -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From srilyk at gmail.com Sun May 31 04:07:33 2009 From: srilyk at gmail.com (W W) Date: Sat, 30 May 2009 21:07:33 -0500 Subject: [Tutor] Class Tips In-Reply-To: <4A21DB5A.1020702@abbottdavid.com> References: <4A21BA5B.8070504@abbottdavid.com> <4A21DB5A.1020702@abbottdavid.com> Message-ID: <333efb450905301907m546ec5b3x4d12f7ca4fcb0001@mail.gmail.com> On Sat, May 30, 2009 at 8:20 PM, David wrote: > > Alan Gauld wrote: >> >> "David" wrote >> >>> I took this program that determines a fertilizer application rate; >>> ... >>> And converted it to class/object to learn how they work. Just looking for some pointers, if I did it correctly etc. >>> >>> One thing that's probably not in the scope of the program but really usually a good idea is error checking. i.e. this line: rate = float(raw_input("Enter rate i.e. (0.5) : ")) could be converted to something like: try: rate = float(raw_input("Enter rate...")) except ValueError: print "Invalid input" #Quit or loop until valid input is entered. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From vy0123 at gmail.com Sun May 31 04:32:58 2009 From: vy0123 at gmail.com (vy) Date: Sun, 31 May 2009 12:32:58 +1000 Subject: [Tutor] Getting exposure to high quality Python code Message-ID: <561420f60905301932p1d3de275v7d0af6e873c79fa8@mail.gmail.com> > From: Daniel Brown > > Can anyone recommend any ways of getting exposed to high quality > Python code? Have a look at (& contribute to) the source code of projects like http://www.sagemath.org/ Kind regards, -- Van Ly From allen.fowler at yahoo.com Sun May 31 04:59:29 2009 From: allen.fowler at yahoo.com (Allen Fowler) Date: Sat, 30 May 2009 19:59:29 -0700 (PDT) Subject: [Tutor] Mapping to an equivalent / similar object? In-Reply-To: References: <916298.49448.qm@web45608.mail.sp1.yahoo.com> Message-ID: <40416.60267.qm@web45606.mail.sp1.yahoo.com> > The difference between objects is measeured by their interfaces not their data > so provided the supplier objects all use the same operations of a Flavor then > its not too bad. You can either create subclasses of Flavor for each > manufacturer that retuirns the requisite list of attributes (but requires > ongoing changes for every new manufacturer - eek!) or you can write a method of > Flavor that takes a list (or dictionary?) of required attributes and returns the > values. It can also raise exceptions if asked for unsupported attributes, or > return None, in which case the Supplier object provides a suitable default. > Hmm. I'll have to read that a couple of times. > This way you can limit the changes for a new supplier. You might also be able to > data drive the Supplier object to make the required list of attributes > configurable(via a database or file?). Then you only need subclasses of Supplier > for the actual interface types (EDI, v FTP, v http v email etc). > > > How should the mapping between the CanonicalFlavor('Vanilla') object and > ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') objects be handled. > > If the mapping is one to many I don;t think you need to maintain it - at least > not in the objects. Just pass the Flavor object to the supplier object. Let the > supplier query the Flavor for the data it needs. > > If the choice of supplier was driven by Flavor characteristics then the > selection method would be in the Flavour and then you'd need to keep a list of > suppliers in the Flavor. But since > the supplier is chosen from external factors the Flavor doesn't need to know > about its supplier. And the supplier only needs to know about how to query a > flavour. > What about just Flavor() objects and a customised "per-manufacture" SupplierA() / SupplierB() objects that internally knows how to generate an order from a passed in Flavor() object? (Which would also raise a CantMakeFlavor error if this manufature can't supply the required flavor.) From burgess.nick at gmail.com Sun May 31 06:51:06 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Sun, 31 May 2009 00:51:06 -0400 Subject: [Tutor] converting xls to csv Message-ID: <3a13c8e50905302151k41e5221bj7d3d4287a1df1da@mail.gmail.com> Hi list, I am trying to make this code work. I don't have any experience with defining things and this is my second program. The error returmed is "SyntaxError: invalid syntax" code: #!/usr/bin/python import cvs def convertXLS2CSV(aFile): '''converts a MS Excel file to csv w/ the same name in the same directory''' print "------ beginning to convert XLS to CSV ------" try: import win32com.client, os excel = win32com.client.Dispatch('Excel.Application') fileDir, fileName = os.path.split(aFile) nameOnly = os.path.splitext(fileName) newName = nameOnly[0] + ".csv" outCSV = os.path.join(fileDir, newName) workbook = excel.Workbooks.Open(aFile) workbook.SaveAs(outCSV, FileFormat=24) # 24 represents xlCSVMSDOS workbook.Close(False) excel.Quit() del excel print "...Converted " + nameOnly + " to CSV" #except: #print ">>>>>>> FAILED to convert to CSV!" convertXLS2CSV(r"F:\python\MasterList.xls") From xboxmuncher at gmail.com Sun May 31 07:55:53 2009 From: xboxmuncher at gmail.com (xbmuncher) Date: Sun, 31 May 2009 01:55:53 -0400 Subject: [Tutor] Why does it display traceback info here and not there? In-Reply-To: References: Message-ID: Well you're right about the paths... print(os.getcwd()) outputs when run from notepad++: c:\program files\Notepad++ when double clicked: actual directory path I'd like to use notepad++ to execute the scripts I edit, is there a way to solve this problem? On Sat, May 30, 2009 at 3:45 PM, Alan Gauld wrote: > > "xbmuncher" wrote > > Traceback (most recent call last): >> File "C:\path to file\script.py", line 1, in >> >> h = open('file2.dat', 'rb') >> File "C:\Python30\lib\io.py", line 278, in __new__ >> return open(*(args, **kwargs) >> File "C:\Python30\lib\io.py", line 222, in open >> closefd) >> File "C:\Python30\lib\io.py", line 619, in __init__ >> >> > You don't have the full traceback here so we can't see > the actual error condition but I suspect its a problem with > paths. Python probably can't find the file because its > looking in the wrong place. Does it worik if you give the > fuill path to the file? > > > The WEIRD thing is, is that I can double click the same unchanged >> script.py >> file and it will run from the same program python.exe and it will display >> this output: >> \xb7\lots of these types of \xb codes >> Press ENTER to exit >> >> So my question is, why does it have different output run from the same >> python.exe program when the script remains the same. >> > > Probably Python is being called with the editors startup folder > so cannot find your file. > > Maybe I can turn off traceback info? >> > > You could but its not wise, the traceback means there is an error > so turning the messages off won't help your program to run, > you just won't get any clues as to why its not working! > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 31 09:53:28 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 May 2009 08:53:28 +0100 Subject: [Tutor] Why does it display traceback info here and not there? References: Message-ID: "xbmuncher" wrote > I'd like to use notepad++ to execute the scripts I edit, is there a way > to > solve this problem? Thats really a question for Notepad++ users! You might be able to use a shortcut to Notepad++ and change that Startup folder to be the one where your python scripts live? Beyond that I don't know what other configuration items Notepad++ might offer. Try reading the help file. Alan G From alan.gauld at btinternet.com Sun May 31 09:57:06 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 May 2009 08:57:06 +0100 Subject: [Tutor] converting xls to csv References: <3a13c8e50905302151k41e5221bj7d3d4287a1df1da@mail.gmail.com> Message-ID: "Nick Burgess" wrote > I am trying to make this code work. I don't have any experience with > defining things and this is my second program. The error returmed is > "SyntaxError: invalid syntax" Please post the whole error message, much of the useful stufff - like the location of the problem! - is in the earlier lines. However taking a wild guess - are you using Python v3? > print "------ beginning to convert XLS to CSV ------" If so print is now a function so you need to surround the string with () But if you are not using v3, then please send the fuill error text. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Sun May 31 11:01:30 2009 From: david at abbottdavid.com (David) Date: Sun, 31 May 2009 05:01:30 -0400 Subject: [Tutor] Class Tips In-Reply-To: <152882.66643.qm@web86708.mail.ird.yahoo.com> References: <4A21BA5B.8070504@abbottdavid.com> <4A21DB5A.1020702@abbottdavid.com> <152882.66643.qm@web86708.mail.ird.yahoo.com> Message-ID: <4A22476A.6020502@abbottdavid.com> ALAN GAULD wrote: > >> but I get an error here; >> >> def main(): >> frate = FertRate(get_inputs()) > > >> File "./fert_class.py", line 15, in main >> frate = FertRate(get_inputs()) >> TypeError: __init__() takes exactly 5 arguments (2 given) > > > Sorry my mistake. Because get_inputs() returns a tuple > you have to tell the function to unpack it by placing an > asterisk in front of the function call: > > frate = FerrtRate( *get_inputs() ) > > That should work now. > > Alan G. > > Super, I had seen that in other programs and now I know why. I have used; def c(*args, **kwargs): My understanding is 'args' is a tuple with all the positional arguments, kwargs is a dictionary with all the named arguments. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From burgess.nick at gmail.com Sun May 31 11:09:11 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Sun, 31 May 2009 05:09:11 -0400 Subject: [Tutor] converting xls to csv In-Reply-To: References: <3a13c8e50905302151k41e5221bj7d3d4287a1df1da@mail.gmail.com> Message-ID: <3a13c8e50905310209h2c2ff914g3b216b3f57e2d1@mail.gmail.com> Thank you for your response and my apologies for the non-useful output. After searching again this task has been done by pyExcelerator, the tool xls2csv-gerry.py does this great. I am now trying to parse through the csv's and return the rows containing the matching strings. Python is looking for string or buffer, this method worked from a text search script, I am stumped on this for now. import re import csv pattern = re.compile(r'10\.229\.127\.255') spamReader = csv.reader(open('MasterIPList.csv'), delimiter=' ', quotechar='|') for row in spamReader: if pattern.search(row): print ', '.join(row) error F:\python>MastIPparse.py Traceback (most recent call last): File "F:\python\MastIPparse.py", line 9, in if pattern.search(row): TypeError: expected string or buffer When I comment out line 9 it returns all of the rows, so I now its reading OK.. Thanks for your time, -nick On Sun, May 31, 2009 at 3:57 AM, Alan Gauld wrote: > > "Nick Burgess" wrote >> >> I am trying to make this code work. ?I don't have any experience with >> defining things and this is my second program. ?The error returmed is >> "SyntaxError: invalid syntax" > > Please post the whole error message, much of the useful stufff - like the > location of the problem! - is in the earlier lines. > > However taking a wild guess - are you using Python v3? > >> print "------ beginning to convert XLS to CSV ------" > > If so print is now a function so you need to surround the string with () > > But ?if you are not using v3, then please send the fuill error text. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From burgess.nick at gmail.com Sun May 31 11:48:11 2009 From: burgess.nick at gmail.com (Nick Burgess) Date: Sun, 31 May 2009 05:48:11 -0400 Subject: [Tutor] converting xls to csv In-Reply-To: <3a13c8e50905310209h2c2ff914g3b216b3f57e2d1@mail.gmail.com> References: <3a13c8e50905302151k41e5221bj7d3d4287a1df1da@mail.gmail.com> <3a13c8e50905310209h2c2ff914g3b216b3f57e2d1@mail.gmail.com> Message-ID: <3a13c8e50905310248p50027487l51ac7fe2c8c5061d@mail.gmail.com> Got it. the row is not a string or buffer but the cell is.. for row in spamReader: for cell in row: if pattern.search(cell): print ', '.join(row) On Sun, May 31, 2009 at 5:09 AM, Nick Burgess wrote: > Thank you for your response and my apologies for the non-useful > output. ?After searching again this task has been done by > pyExcelerator, the tool xls2csv-gerry.py does this great. ?I am now > trying to parse through the csv's and return the rows containing the > matching strings. ?Python is looking for string or buffer, this method > worked from a text search script, I am stumped on this for now. > > > import re > import csv > > pattern = re.compile(r'10\.229\.127\.255') > spamReader = csv.reader(open('MasterIPList.csv'), delimiter=' ', quotechar='|') > for row in spamReader: > ? ?if pattern.search(row): > ? ? ? ?print ', '.join(row) > > > error > > F:\python>MastIPparse.py > Traceback (most recent call last): > ?File "F:\python\MastIPparse.py", line 9, in > ? ?if pattern.search(row): > TypeError: expected string or buffer > > > When I comment out line 9 it returns all of the rows, so I now its reading OK.. > > Thanks for your time, > > -nick > > On Sun, May 31, 2009 at 3:57 AM, Alan Gauld wrote: >> >> "Nick Burgess" wrote >>> >>> I am trying to make this code work. ?I don't have any experience with >>> defining things and this is my second program. ?The error returmed is >>> "SyntaxError: invalid syntax" >> >> Please post the whole error message, much of the useful stufff - like the >> location of the problem! - is in the earlier lines. >> >> However taking a wild guess - are you using Python v3? >> >>> print "------ beginning to convert XLS to CSV ------" >> >> If so print is now a function so you need to surround the string with () >> >> But ?if you are not using v3, then please send the fuill error text. >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > From roadierich at googlemail.com Sun May 31 18:45:23 2009 From: roadierich at googlemail.com (Richard Lovely) Date: Sun, 31 May 2009 17:45:23 +0100 Subject: [Tutor] converting xls to csv In-Reply-To: <3a13c8e50905310248p50027487l51ac7fe2c8c5061d@mail.gmail.com> References: <3a13c8e50905302151k41e5221bj7d3d4287a1df1da@mail.gmail.com> <3a13c8e50905310209h2c2ff914g3b216b3f57e2d1@mail.gmail.com> <3a13c8e50905310248p50027487l51ac7fe2c8c5061d@mail.gmail.com> Message-ID: 2009/5/31 Nick Burgess : > Got it. > > the row is not a string or buffer but the cell is.. > > for row in spamReader: > ? ?for cell in row: > ? ? ? ?if pattern.search(cell): > ? ? ? ? ? ?print ', '.join(row) > > Alternatively, if you know that the string you want to search for only appears in a single cell from the row, you could use for row in spamReader: if pattern.search(cell[coll_number_here]): print ', '.join(row) If there is a range of adjacent cells that could contain the text, you could try something like if pattern.search('|'.join(row[3:5]) For non-adjacent cells, try something like targetCells = [1,3,5] for row in spamReader: if pattern.search('|'.join(row[i] for i in targetCells)) Both of these solutions will be faster than iterating over th entire row, unless tht is specifically what you want. -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com From david at abbottdavid.com Sun May 31 21:08:08 2009 From: david at abbottdavid.com (David) Date: Sun, 31 May 2009 15:08:08 -0400 Subject: [Tutor] Class Tips In-Reply-To: <333efb450905301907m546ec5b3x4d12f7ca4fcb0001@mail.gmail.com> References: <4A21BA5B.8070504@abbottdavid.com> <4A21DB5A.1020702@abbottdavid.com> <333efb450905301907m546ec5b3x4d12f7ca4fcb0001@mail.gmail.com> Message-ID: <4A22D598.10202@abbottdavid.com> W W wrote: > > One thing that's probably not in the scope of the program but really > usually a good idea is error checking. > i.e. this line: > rate = float(raw_input("Enter rate i.e. (0.5) : ")) > > could be converted to something like: > > try: > rate = float(raw_input("Enter rate...")) > except ValueError: > print "Invalid input" > #Quit or loop until valid input is entered. > > HTH, > Wayne OK, this is what I came up with, how else could I do it so as not to use sys.exit() ? #!/usr/bin/python """Determine the number of bags to use to fertilize a given area given the application rate.""" from sys import exit class FertRate: def __init__(self, rate, nitrogen, acre, bag): self.area = 43.560 self.app = rate / (nitrogen / 100.00) self.acre = acre self.bag = bag def result(self): result = self.app * self.area * self.acre / self.bag return result def main(): while True: try: frate = FertRate( *get_inputs() ) result = frate.result() print 'You should apply %0.2f bags.' % result exit() except TypeError, UnboundLocalError: pass def get_inputs(): try: print 'Rate: Pounds nitrogen per 1000 (square feet)' rate = float(raw_input('Enter Rate i.e., (0.5): ')) print "Nitrogen: The first number of the fertilizer's analysis" nitrogen = float(raw_input('Enter Nitrogen From Bag i.e., (14): ')) acre = int(raw_input("Enter Total Acre's To Be Treated i.e, (3): ")) bag = int(raw_input('Enter Bag Weight (lb): i.e., (50) ')) return rate, nitrogen, acre, bag except ValueError: print 'Invalid input!' print 'You must enter a number!' if __name__ == "__main__": main() -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Sun May 31 22:16:59 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 May 2009 21:16:59 +0100 Subject: [Tutor] Class Tips References: <4A21BA5B.8070504@abbottdavid.com> <4A21DB5A.1020702@abbottdavid.com><333efb450905301907m546ec5b3x4d12f7ca4fcb0001@mail.gmail.com> <4A22D598.10202@abbottdavid.com> Message-ID: "David" wrote > OK, this is what I came up with, how else could I do it so as not to use > sys.exit() ? You don;t need the exit(), The program will just drop off the end silently without it. Thre are several other redundant bits you could just miss out: > class FertRate: > def __init__(self, rate, nitrogen, acre, bag): > self.area = 43.560 > self.app = rate / (nitrogen / 100.00) > self.acre = acre > self.bag = bag > > def result(self): > result = self.app * self.area * self.acre / self.bag > return result leave out the variable and just return the calculated value: return self.app * self.area * self.acre / self.bag > def main(): > while True: > try: > frate = FertRate( *get_inputs() ) > result = frate.result() > print 'You should apply %0.2f bags.' % result And here miss the variable and use the functin in the format string: print 'You should apply %0.2f bags.' % frate.result() > exit() And just delete this. > except TypeError, UnboundLocalError: > pass > > def get_inputs(): > try: > print 'Rate: Pounds nitrogen per 1000 (square feet)' > rate = float(raw_input('Enter Rate i.e., (0.5): ')) > print "Nitrogen: The first number of the fertilizer's analysis" > nitrogen = float(raw_input('Enter Nitrogen From Bag i.e., (14): > ')) > acre = int(raw_input("Enter Total Acre's To Be Treated i.e, (3): > ")) > bag = int(raw_input('Enter Bag Weight (lb): i.e., (50) ')) > return rate, nitrogen, acre, bag > except ValueError: > print 'Invalid input!' > print 'You must enter a number!' You could put the whole thing in a while loop so the error gets printed but the user has a chance to corect it. def get_inputs(): while True: try: # blah blah return ..... # breaks from the while loop except ValueError, TypeError: print blah HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From goldwamh at slu.edu Sun May 31 23:35:22 2009 From: goldwamh at slu.edu (Michael H. Goldwasser) Date: Sun, 31 May 2009 16:35:22 -0500 Subject: [Tutor] Challenge supporting custom deepcopy with inheritance Message-ID: <18978.63514.319199.287551@Michael-Goldwassers-Computer.local> Yesterday, I posted a question to python-list involving custom deepcopies in an inheritance hierarchy. I haven't received any responses, so I thought I'd cross-post to see if anyone on tutor has any thoughts. To avoid splitting the thread, I'll simply reference the original post at http://mail.python.org/pipermail/python-list/2009-May/714866.html and ask for responses to be sent there. With regard, Michael