From eire1130 at gmail.com Mon Mar 1 00:24:09 2010 From: eire1130 at gmail.com (James Reynolds) Date: Sun, 28 Feb 2010 18:24:09 -0500 Subject: [Tutor] OOD - Another class question Message-ID: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> I have another question related to OOD. What I have is a module with one parent class and two child classes. Some stuff is done to the object that is passed to the function in one of the child classes and this then calls a function from the global class passing local variables (from the child class). When I do this, I am told: AttributeError: 'HillBuilder' object has no attribute 'MountainBuilder' The question is, what am I doing wrong? Here is an example: class MountainBuilder(object): def __init__(self, mountain): self.mountain = mountain self.mountain_func self.pinetree_func def pinetree_func(self, knoll) do stuff to knoll return knoll def mountain_func(self, hill) knoll = hill * 2 pinetree = pintree_func(knoll) return hill class HillBuilder(MountainBuilder): def __init__(self, mountain): OptionLoad.__init__(self, mountain) self.MountainBuilder.mountain_func self.hill_func def hill_func(self) hill= do stuff to self.mountain grassyknoll = MountainBuilder.mountain_func(hill) return grassy knoll do stuff with grassy knoll -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Mon Mar 1 00:36:38 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sun, 28 Feb 2010 15:36:38 -0800 Subject: [Tutor] OOD - Another class question In-Reply-To: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> References: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> Message-ID: <20100228233638.GA15083@dragon.alchemy.com> On Sun, Feb 28, 2010 at 06:24:09PM -0500, James Reynolds wrote: > I have another question related to OOD. What I have is a module with one > parent class and two child classes. Some stuff is done to the object that is > passed to the function in one of the child classes and this then calls a > function from the global class passing local variables (from the child > class). I think you're confusing containers with inheritance. > class MountainBuilder(object): > def __init__(self, mountain): > self.mountain = mountain > self.mountain_func <--- what's this? > self.pinetree_func <--- what's this? > > class HillBuilder(MountainBuilder): > def __init__(self, mountain): > OptionLoad.__init__(self, mountain) > self.MountainBuilder.mountain_func There is no MountainBuilder attribute in HillBuilder. Rather, HillBuilder is a refined type of a MountainBuilder, and inherits everything MountainBuilders have, plus what you change or add to that base in HillBuilder. You're treating it like it's a separate class which contains a MountainBuilder object inside it as an attribute. From alan.gauld at btinternet.com Mon Mar 1 02:04:04 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Mar 2010 01:04:04 -0000 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. References: <4B8A5921.5000008@free.fr> <4B8A8E88.7050707@free.fr> Message-ID: "Karim Liateni" wrote > def getLines(file): > try: lines = open(filename).readlines() ; return lines > except IOError: #handle error > > > but in the second 'lines = open(filename).readlines()' > I don't hold indirectly a reference to the file? Please, could you > explain more this point? Sure, the lines variable holds a reference to the list returned by readlines. There is no variable referring to the file object so immediately after readlines completes the file will be ready to be closed (at least in CPython as already pointed out by Lie) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Mar 1 02:13:16 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Mar 2010 01:13:16 -0000 Subject: [Tutor] OOD - Another class question References: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> Message-ID: "James Reynolds" wrote > parent class and two child classes. Some stuff is done to the object that > is > passed to the function in one of the child classes and this then calls a > function from the global class passing local variables (from the child > class). You really need to tighten up on the terminology because the imprecision is part of your problems understanding the concepts. You pass an object to a method of a child class which calls a method in the parent class. You pass instance variables of the child class as arguments to the parent class method.. > When I do this, I am told: AttributeError: 'HillBuilder' object has no > attribute 'MountainBuilder' > The question is, what am I doing wrong? Confusing inheritance with composition. Your child class does not have an attribute MountainBuilder, it *is* a MountainBuilder. self within the method referes to your subclass of MountainBuilder so you only need self. > class MountainBuilder(object): > def pinetree_func(self, knoll) > do stuff to knoll > return knoll > def mountain_func(self, hill) > knoll = hill * 2 > pinetree = pintree_func(knoll) > return hill > > class HillBuilder(MountainBuilder): > def __init__(self, mountain): > OptionLoad.__init__(self, mountain) > self.MountainBuilder.mountain_func This should be self.mountain_func(mountain) > def hill_func(self) > hill= do stuff to self.mountain > grassyknoll = MountainBuilder.mountain_func(hill) And this should be grassyknoll = self.mountain_func(hill) mountain func as a method of MountainBuilder is also a method of HillBuilder (thats what inheritance means) so you access it through self. Take a look at the BankAccount example in the OOP topic of my tutor for more examples of subclasses calling superclass methods HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sierra_mtnview at sbcglobal.net Mon Mar 1 06:23:37 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 28 Feb 2010 21:23:37 -0800 Subject: [Tutor] Verifying My Troublesome Linkage Claim between Python and Win7 In-Reply-To: <4B8A57B2.9030704@ieee.org> References: <4B83ED4A.4040203@sbcglobal.net> <201002280558.20154.steve@pearwood.info> <4B89809C.8070304@sbcglobal.net> <201002281224.03955.steve@pearwood.info> <4B89FD0C.1010004@sbcglobal.net> <4B8A57B2.9030704@ieee.org> Message-ID: <4B8B4F59.7040206@sbcglobal.net> I just posted the details a moment ago to Steven. On 2/28/2010 3:46 AM, Dave Angel wrote: > > > Wayne Watson wrote: >> >>> You tell us to "try this" and give a folder structure: >>> >>> Folder1 >>> track1.py >>> data1.txt >>> data2.txt >>> data3.txt >>> Folder2 >>> track1.py >>> dset1.txt >>> dset2.txt >>> ... >>> dset8.txt >>> >> > Maybe one simple test at a time will get better responses. Since you > wouldn't tell me what tabs you saw in Explorer when you did > properties, maybe you'll tell me what you see in CMD. > > Go to a cmd prompt (DOS prompt), change to the Folder2 directory, and > type dir. paste that result, all of it, into a message. I suspect > you'll see that you don't have track1.py there at all, but track1.py.lnk > > If so, that's a shortcut. The only relevant change in Win7 that I > know of is that Explorer shows shortcuts as "link" rather than > "shortcut." > > > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From cwitts at compuscan.co.za Mon Mar 1 07:57:06 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 01 Mar 2010 08:57:06 +0200 Subject: [Tutor] Top posters for 2009 In-Reply-To: <1c2a2c591002251853x5d2127a3p5c382abea293c336@mail.gmail.com> References: <1c2a2c591002251853x5d2127a3p5c382abea293c336@mail.gmail.com> Message-ID: <4B8B6542.5010904@compuscan.co.za> Kent Johnson wrote: > It's not really about keeping score :-), but once again I've compiled > a list of the top 20 posters to the tutor list for the last year. For > 2009, the rankings are > > 2009 (7730 posts, 709 posters) > ==== > Alan Gauld 969 (12.5%) > Kent Johnson 804 (10.4%) > Dave Angel 254 (3.3%) > spir 254 (3.3%) > Wayne Watson 222 (2.9%) > bob gailer 191 (2.5%) > Lie Ryan 186 (2.4%) > David 127 (1.6%) > Emile van Sebille 115 (1.5%) > Wayne 112 (1.4%) > Sander Sweers 111 (1.4%) > Serdar Tumgoren 100 (1.3%) > Luke Paireepinart 99 (1.3%) > wesley chun 99 (1.3%) > W W 74 (1.0%) > Marc Tompkins 72 (0.9%) > A.T.Hofkamp 71 (0.9%) > Robert Berman 68 (0.9%) > vince spicer 63 (0.8%) > Emad Nawfal 62 (0.8%) > > Alan, congratulations, you pulled ahead of me for the first time in > years! You posted more than in 2008, I posted less. Overall posts are > up from last year, which was the slowest year since I started > measuring (2003). > > Thank you to everyone who asks and answers questions here! > > The rankings are compiled by scraping the monthly author pages from > the tutor archives, using Beautiful Soup to extract author names. I > consolidate counts for different capitalizations of the same name but > not for different spellings. The script is below. > > Kent > > ''' Counts all posts to Python-tutor by author''' > # -*- coding: latin-1 -*- > from datetime import date, timedelta > import operator, urllib2 > from BeautifulSoup import BeautifulSoup > > today = date.today() > > for year in range(2009, 2010): > startDate = date(year, 1, 1) > endDate = date(year, 12, 31) > thirtyOne = timedelta(days=31) > counts = {} > > # Collect all the counts for a year by scraping the monthly author > archive pages > while startDate < endDate and startDate < today: > dateString = startDate.strftime('%Y-%B') > > url = 'http://mail.python.org/pipermail/tutor/%s/author.html' > % dateString > data = urllib2.urlopen(url).read() > soup = BeautifulSoup(data) > > li = soup.findAll('li')[2:-2] > > for l in li: > name = l.i.string.strip() > counts[name] = counts.get(name, 0) + 1 > > startDate += thirtyOne > > # Consolidate names that vary by case under the most popular spelling > nameMap = dict() # Map lower-case name to most popular name > > # Use counts.items() so we can delete from the dict. > for name, count in sorted(counts.items(), > key=operator.itemgetter(1), reverse=True): > lower = name.lower() > if lower in nameMap: > # Add counts for a name we have seen already and remove the duplicate > counts[nameMap[lower]] += count > del counts[name] > else: > nameMap[lower] = name > > totalPosts = sum(counts.itervalues()) > posters = len(counts) > > print > print '%s (%s posts, %s posters)' % (year, totalPosts, posters) > print '====' > for name, count in sorted(counts.iteritems(), > key=operator.itemgetter(1), reverse=True)[:20]: > pct = round(100.0*count/totalPosts, 1) > print '%s %s (%s%%)' % (name.encode('utf-8', > 'xmlcharrefreplace'), count, pct) > print > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Nice script Kent. Keep up the good signal-to-noise ratio guys. -- Kind Regards, Christian Witts From davea at ieee.org Mon Mar 1 07:57:44 2010 From: davea at ieee.org (Dave Angel) Date: Mon, 01 Mar 2010 01:57:44 -0500 Subject: [Tutor] Verifying My Troublesome ...+Properties In-Reply-To: <4B8B5130.7000407@sbcglobal.net> References: <4B83ED4A.4040203@sbcglobal.net> <201002280558.20154.steve@pearwood.info> <4B89809C.8070304@sbcglobal.net> <201002281224.03955.steve@pearwood.info> <4B8B5130.7000407@sbcglobal.net> Message-ID: <4B8B6568.4000208@ieee.org> Wayne Watson wrote: > (I sent the msg below to Steven and the list a moment ago, since msgs > going to the list with attachments either don't post or take lots of > time to post, I'm sending both of you this copy.) > > Steven, attached are three jpg files showing the properties of the two > py files. The two files are identical in name, ReportingToolA.py, and > content, but are in folders .../Events2008_NovWW and .../events. Two > of the jpg files are for the General Tab and Shortcut Tab of the py > file in ../events. The other jpg is for the file in > .../Events2008_NovWW, which has no shortcut tab. In previous > descriptions, this is like: > > Folder1 is Events2008_NovWW > Folder2 is events > > I developed RT.py (ReportingToolA.py) in the .../Events2008_NovWW > folder and copied it to ../events. The shortcut shows the events > folder RT.py file is really in Events20008_WW > > I have no idea why the RT.py file shows a shortcut. I just took a file > called junk.jpg, and right-clicked on it. I selected Shortcut from the > list, and it produced a file junk.jpg-shortcut. It is quite obvious > the file name is different. If I select Copy instead, and paste the > file into a folder called Junk, there is no shortcut created. A drag > and drop results in a move,and not a copy, so that's out of the picture. > > I have no idea how the RT.py file ever got to be a shortcut. As I said many messages ago, if your Properties dialog has a tab called Shortcut, then this is a shortcut file, not a python file. I still don't know how you created it, but that's your "anomaly," not Windows 7, and certainly not Python. Further, the name isn't RT.py, since shortcuts have other extensions (such as .lnk) that Explorer hides from you, in its infinite "helpfulness." It does give you several clues, however, such as the little arrow in the icon. You can see that without even opening the properties window, but it's repeated in that window as well. And Explorer is just a tool. The command prompt should be your home base as a programmer. When something goes wrong running a program from the either other ways, always check it at the command prompt, because every other tool has quirks it introduces into the equation. My best guess on how you created that shortcut was by using Alt-Drag. As you point out, drag does a move by default, if it's on the same drive. Ctrl-Drag will force a copy, even on the same drive. And Shift-Drag will force a move, even if it's on a different drive. These rules didn't change between XP and Windows 7, as far as I know, although in some places Explorer calls it "Link" instead of "Shortcut". But that's just a self inconsistency. DaveA From tmatsumoto at gmx.net Mon Mar 1 09:50:22 2010 From: tmatsumoto at gmx.net (C.T. Matsumoto) Date: Mon, 01 Mar 2010 09:50:22 +0100 Subject: [Tutor] OOD - Another class question In-Reply-To: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> References: <98c3f7c51002281524y1abe1c79sdadb030d309b6b97@mail.gmail.com> Message-ID: <4B8B7FCE.80107@gmx.net> James Reynolds wrote: > I have another question related to OOD. What I have is a module with > one parent class and two child classes. Some stuff is done to the > object that is passed to the function in one of the child classes and > this then calls a function from the global class passing local > variables (from the child class). > > When I do this, I am told: AttributeError: 'HillBuilder' object has no > attribute 'MountainBuilder' > > The question is, what am I doing wrong? > > Here is an example: > > class MountainBuilder(object): > def __init__(self, mountain): > self.mountain = mountain > self.mountain_func > self.pinetree_func > > > def pinetree_func(self, knoll) > do stuff to knoll > return knoll > > > def mountain_func(self, hill) > knoll = hill * 2 > pinetree = pintree_func(knoll) > return hill > > > class HillBuilder(MountainBuilder): > def __init__(self, mountain): > OptionLoad.__init__(self, mountain) > self.MountainBuilder.mountain_func > self.hill_func > > > def hill_func(self) > hill= do stuff to self.mountain > grassyknoll = MountainBuilder.mountain_func(hill) > > return grassy knoll > > > do stuff with grassy knoll > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The line grassyknoll = MountainBuilder.mountain ... Since you inherit MountainBuilder you call the mountain_func (which is a method :-) ) with self: grassyknoll = self.mountain_func(hill) All methods inside the MountainBuilder can be called as if they are a part of the HillBuilder (thus inheritance). Cheers, T From lie.1296 at gmail.com Mon Mar 1 13:45:10 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 01 Mar 2010 23:45:10 +1100 Subject: [Tutor] Over-riding radians as default for trig calculations In-Reply-To: <4B8AC65E.8080407@gmail.com> References: <4B8AC65E.8080407@gmail.com> Message-ID: On 03/01/10 06:39, AG wrote: > After importing the math module and running > > math.cos( x ) > > the result is in radians. > > Is there a way of setting this so that it results in degrees? I don't > want to over-ride this permanently for my Python settings, so am happy > to specifically do it per equation or per program. I'd recommend you to get used to using radian measurement; though it may initially looks unfamiliar, working in radian (with computer or by-hand) is much more natural once you get used to it. Many formulaes become simpler when using radian while some are only valid in radian. The only reasonable point where you should convert from and between radians is when requesting input from non-mathematician users. From sierra_mtnview at sbcglobal.net Mon Mar 1 18:13:14 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 01 Mar 2010 09:13:14 -0800 Subject: [Tutor] Why is the max size so low in this mail list? Message-ID: <4B8BF5AA.6050200@sbcglobal.net> See Subject. 40K here, but other Python lists allow for larger (total) sizes. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From alan.gauld at btinternet.com Mon Mar 1 18:30:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Mar 2010 17:30:00 -0000 Subject: [Tutor] Why is the max size so low in this mail list? References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: "Wayne Watson" wrote > See Subject. 40K here, but other Python lists allow for larger (total) > sizes. Presumably to discourage long posts or posts with large attachments? But I'm only guessing... Alan G From sander.sweers at gmail.com Mon Mar 1 19:06:13 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 1 Mar 2010 19:06:13 +0100 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: <4B8BF5AA.6050200@sbcglobal.net> References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: On 1 March 2010 18:13, Wayne Watson wrote: > See Subject. 40K here, but other Python lists allow for larger (total) > sizes. Don't know but if it is that long use pastebin [1]. Greets Sander [1] http://python.pastebin.com/ From steve at alchemy.com Mon Mar 1 19:09:26 2010 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 1 Mar 2010 10:09:26 -0800 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: <20100301180926.GA59176@dragon.alchemy.com> On Mon, Mar 01, 2010 at 05:30:00PM -0000, Alan Gauld wrote: > > "Wayne Watson" wrote > > >See Subject. 40K here, but other Python lists allow for larger (total) > >sizes. > > Presumably to discourage long posts or posts with large attachments? > But I'm only guessing... It's also mailman's* default size, so it could also be that nobody thought it was important to change it from that default. On the other hand, there's a good argument in favor of having people post links to large code snippets and not send them through the list, or (and you know it'll happen) 1600x1200 screenshots of error messages. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From bill at celestial.net Mon Mar 1 19:22:33 2010 From: bill at celestial.net (Bill Campbell) Date: Mon, 1 Mar 2010 10:22:33 -0800 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: <20100301182233.GA841@ayn.mi.celestial.com> On Mon, Mar 01, 2010, Sander Sweers wrote: >On 1 March 2010 18:13, Wayne Watson wrote: >> See Subject. 40K here, but other Python lists allow for larger (total) >> sizes. > >Don't know but if it is that long use pastebin [1]. 40K is the default on Mailman mailing lists, and for good reason. I don't know how many subscribers there are, but multiplying message size by number of recipients can generate huge volumes (don't forget that encoding binary attachments increases their size by about 1/3). Not everybody has broadband connections, and getting large attachments can cost them Real Money(tm) (as if there is any of that in the world's monetary systems these days :-), and take a long time to download. One of the best features of the horde/imp webmail program is that it makes it easy for one to upload attachments to the web server with a link to the attachment in the body of the message. That's a feature I would like to see an all webmail servers. 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 I have no reason to suppose that he, who would take away my Liberty, would not when he had me in his Power, take away everything else. John Locke From karim.liateni at free.fr Mon Mar 1 21:07:57 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Mon, 01 Mar 2010 21:07:57 +0100 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: References: <4B8A5921.5000008@free.fr> <4B8A8E88.7050707@free.fr> Message-ID: <4B8C1E9D.8020104@free.fr> Thanks for this precision! I'm using standard python so this is ok! Why people use proprietary python ? To have more trouble ? To be different from the rest of community ? Anyway in opensource people do whatever they want but you know multiple version that was the same before Common C or Lisp it was war fields. It's better to spent energy to participate with the core developers to make the common langage evoluate. Regards Karim Alan Gauld wrote: > > "Karim Liateni" wrote > >> def getLines(file): >> try: lines = open(filename).readlines() ; return lines >> except IOError: #handle error >> >> >> but in the second 'lines = open(filename).readlines()' >> I don't hold indirectly a reference to the file? Please, could you >> explain more this point? > > Sure, the lines variable holds a reference to the list returned by > readlines. > There is no variable referring to the file object so immediately after > readlines completes the file will be ready to be closed (at least in > CPython as already pointed out by Lie) > > HTH, > From bermanrl at cfl.rr.com Mon Mar 1 21:32:23 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Mon, 1 Mar 2010 15:32:23 -0500 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: <007d01cab97e$4c8430f0$e58c92d0$@rr.com> > -----Original Message----- > From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- > bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Alan Gauld > Sent: Monday, March 01, 2010 12:30 PM > To: tutor at python.org > Subject: Re: [Tutor] Why is the max size so low in this mail list? > > > "Wayne Watson" wrote > > > See Subject. 40K here, but other Python lists allow for larger > (total) > > sizes. > > Presumably to discourage long posts or posts with large attachments? > But I'm only guessing... > > Alan G > > It encouraged me to use pastebin when it was necessary to post code a tad longer than the casual fifteen line snippet. Robert Berman The opposite of love is not hate, it's indifference. The opposite of art is not ugliness, it's indifference. The opposite of faith is not heresy, it's indifference. And the opposite of life is not death, it's indifference. From steve at pearwood.info Mon Mar 1 22:25:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 2 Mar 2010 08:25:46 +1100 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: <4B8C1E9D.8020104@free.fr> References: <4B8A5921.5000008@free.fr> <4B8C1E9D.8020104@free.fr> Message-ID: <201003020825.47463.steve@pearwood.info> On Tue, 2 Mar 2010 07:07:57 am Karim Liateni wrote: > Thanks for this precision! > I'm using standard python so this is ok! > Why people use proprietary python ? > To have more trouble ? To be different from the rest of community ? Python is a language, but there can be many different implementations of that language, just like there are different C compilers or different Javascript engines. CPython is the version which was made first, it is the most common version, but it is not the only one. It is called CPython because it is written in C. Jython is a version of Python written in Java, and it was created by people wanting to use Python as a front-end to Java libraries, and to take advantage of Java's garbage collector. IronPython is Microsoft's version of Python written for .Net and Mono. PyPy is an experimental version of Python written in Python, used by people wanting to experiment with Python compilers. "Python for S60" is a version of Python written for Nokia's S60 devices. CapPython is an experimental version of Python designed for security. There are many others, they are all Python, but they have differences. -- Steven D'Aprano From alan.gauld at btinternet.com Tue Mar 2 01:01:08 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 2 Mar 2010 00:01:08 +0000 (GMT) Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: <4B8C1E9D.8020104@free.fr> References: <4B8A5921.5000008@free.fr> <4B8A8E88.7050707@free.fr> <4B8C1E9D.8020104@free.fr> Message-ID: <467254.17276.qm@web86706.mail.ird.yahoo.com> > Why people use proprietary python ? Because it does something normal Python doesn't. For example I often use Jython (Python implemented in Java instead of C) because it allows me to import Java classes and test them. It also allows me to rapidly build Java classes that I can integrate as temporary solutions in Java programs until the Java developers get round to writing pure Java versions. (For example I can use Python's powerful dynamic collections to create data processing classes) I can also use Jython to interactively experiment with or test Java classes using the >>> prompt, to find out how they behave under different conditions. Others use stackless Python for things like massively parallel processing. Still others favour Iron Python which is written in .NET so that we can use .NET classes and libraries from Python and use Python classes within .NET. > To have more trouble ? To be different from the rest of community ? No, usually to fit in with another community. Or it may be to experiment with different implementation ideas - like many Lisp researchers use Lisp to meddle with the interpreter to try out new computing concepts. Its relatively easy to do that in Python too. And so the science of computing is advanced. But at the cost of using non standard implementations. Not everyone programs to solve practical problems. :-) > It's better to spent energy to participate with the core developers to make the > common langage evoluate. But sometimes you want to go beyond the language, to explore ideas and concepts that underlie everything we do in every language. Languages like Lisp and Python make that possible, if not exactly easy! HTH, Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreas at kostyrka.org Tue Mar 2 01:25:44 2010 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Tue, 2 Mar 2010 01:25:44 +0100 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: <467254.17276.qm@web86706.mail.ird.yahoo.com> References: <4B8A5921.5000008@free.fr> <4B8C1E9D.8020104@free.fr> <467254.17276.qm@web86706.mail.ird.yahoo.com> Message-ID: <201003020125.44822.andreas@kostyrka.org> > > Why people use proprietary python ? Well, AFAIK I know, all more or less popular non-Ansi-C implementations of Python are free. (Jython is, IronPython too, although I've never checked in detail, stackless is, PyPy is too, ...) > > It's better to spent energy to participate with the core developers to > > make the common langage evoluate. > > But sometimes you want to go beyond the language, to explore ideas and > concepts that underlie everything we do in every language. Languages like > Lisp and Python make that possible, if not exactly easy! Actually, having multiple implementations of the language is a huge benefit, because that means that we have a language definition apart from the implementation. C, C++, Java, C#, Ruby do have alternate implementations. Perl, VB, ... do not. Beside the slightly abstract benefit of splitting definition and implementation (which e.g. allows for new and better implementations), most of the alternate implementations have, as Alan already pointed out, it's own nice. Jython means that my Python skills carry over into a Java environment. IronPython means the same for .NET, stackless allows for extreme designs without becoming Twisted, Pyrex/Cython (although not Python, but a Python look-alike) allows me to interface with C and more important to write code that manipulates C data types at the same speed that C does, without the pain of all the Python runtime mixing. Furthermore I do not think that most of the "core" community has a problem with the alternate implementations, as they provide very useful functions (it helps on the architecture side, because it limits somewhat what can be done, it helps on the personal side, because it increases the value of Python skills, ...), ... Andreas From sierra_mtnview at sbcglobal.net Tue Mar 2 04:32:08 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 01 Mar 2010 19:32:08 -0800 Subject: [Tutor] Verifying My Troublesome ...+My Final Word In-Reply-To: <4B8B6568.4000208@ieee.org> References: <4B83ED4A.4040203@sbcglobal.net> <201002280558.20154.steve@pearwood.info> <4B89809C.8070304@sbcglobal.net> <201002281224.03955.steve@pearwood.info> <4B8B5130.7000407@sbcglobal.net> <4B8B6568.4000208@ieee.org> Message-ID: <4B8C86B8.2020201@sbcglobal.net> For me, I'm done. I plumbed this issue elsewhere and the final message there stated this. "I've no time to verify your specific claim and have no readily available proof for mine [his claim], but I've seen similar issues on Win7." Let MS deal with it. Anyway, thanks for your effort. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From cwitts at compuscan.co.za Tue Mar 2 07:15:39 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 02 Mar 2010 08:15:39 +0200 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: <20100301180926.GA59176@dragon.alchemy.com> References: <4B8BF5AA.6050200@sbcglobal.net> <20100301180926.GA59176@dragon.alchemy.com> Message-ID: <4B8CAD0B.60508@compuscan.co.za> Steve Willoughby wrote: > On Mon, Mar 01, 2010 at 05:30:00PM -0000, Alan Gauld wrote: > >> "Wayne Watson" wrote >> >> >>> See Subject. 40K here, but other Python lists allow for larger (total) >>> sizes. >>> >> Presumably to discourage long posts or posts with large attachments? >> But I'm only guessing... >> > > It's also mailman's* default size, so it could also be that nobody > thought it was important to change it from that default. On the other > hand, there's a good argument in favor of having people post links > to large code snippets and not send them through the list, or > (and you know it'll happen) 1600x1200 screenshots of error messages. > > And of course they'll be in Bitmap format and not JPG. -- Kind Regards, Christian Witts From steve at pearwood.info Tue Mar 2 09:47:52 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 2 Mar 2010 19:47:52 +1100 Subject: [Tutor] =?iso-8859-1?q?Any_Tutor_there_=3F_Removing_redundant_par?= =?iso-8859-1?q?ameters_in_a=09models_file_having_include_files=2E?= In-Reply-To: <201003020125.44822.andreas@kostyrka.org> References: <4B8A5921.5000008@free.fr> <467254.17276.qm@web86706.mail.ird.yahoo.com> <201003020125.44822.andreas@kostyrka.org> Message-ID: <201003021947.53047.steve@pearwood.info> On Tue, 2 Mar 2010 11:25:44 am Andreas Kostyrka wrote: > Furthermore I do not think that most of the "core" community has a > problem with the alternate implementations, as they provide very > useful functions (it helps on the architecture side, because it > limits somewhat what can be done, it helps on the personal side, > because it increases the value of Python skills, ...), ... The Python development team values alternative implementations, as it gives Python the language a much wider user base. It also allows other people to shoulder some of the development burden. For example, people who want Python without the limitations of the C call stack can use Stackless Python, instead of ordinary CPython. Google is sponsoring a highly optimized version of Python with a JIT compiler: Unladen Swallow. It looks likely that Unladen Swallow will end up being merged with CPython too, which will be a great benefit. -- Steven D'Aprano From afith13 at gmail.com Tue Mar 2 07:22:43 2010 From: afith13 at gmail.com (Andrew Fithian) Date: Mon, 1 Mar 2010 22:22:43 -0800 Subject: [Tutor] parsing a "chunked" text file Message-ID: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> Hi tutor, I have a large text file that has chunks of data like this: headerA n1 line 1 line 2 ... line n1 headerB n2 line 1 line 2 ... line n2 Where each chunk is a header and the lines that follow it (up to the next header). A header has the number of lines in the chunk as its second field. I would like to turn this file into a dictionary like: dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, line 2, ... , line n2]} Is there a way to do this with a dictionary comprehension or do I have to iterate over the file with a "while 1" loop? -Drew -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Tue Mar 2 13:08:28 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 02 Mar 2010 14:08:28 +0200 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> Message-ID: <4B8CFFBC.6040300@compuscan.co.za> Andrew Fithian wrote: > Hi tutor, > > I have a large text file that has chunks of data like this: > > headerA n1 > line 1 > line 2 > ... > line n1 > headerB n2 > line 1 > line 2 > ... > line n2 > > Where each chunk is a header and the lines that follow it (up to the > next header). A header has the number of lines in the chunk as its > second field. > > I would like to turn this file into a dictionary like: > dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, > line 2, ... , line n2]} > > Is there a way to do this with a dictionary comprehension or do I have > to iterate over the file with a "while 1" loop? > > -Drew > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > A solution that could work for you could be something like... dict([(z.splitlines()[0].split()[0],z.splitlines()[1:]) for z in [x for x in open(filename).read().split('header') if x.strip()]]) {'A': ['line 1', 'line 2', '...', 'line n1'], 'B': ['line 1', 'line 2', '...', 'line n2']} Of course that doesn't look very pretty and only works for a specific case as demonstrated on your sample data. -- Kind Regards, Christian Witts Business Intelligence 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. National Credit Regulator Credit Bureau Registration No. NCRCB6 From denis.spir at gmail.com Tue Mar 2 14:04:41 2010 From: denis.spir at gmail.com (spir) Date: Tue, 2 Mar 2010 14:04:41 +0100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> Message-ID: <20100302140441.68af8c76@o> On Mon, 1 Mar 2010 22:22:43 -0800 Andrew Fithian wrote: > Hi tutor, > > I have a large text file that has chunks of data like this: > > headerA n1 > line 1 > line 2 > ... > line n1 > headerB n2 > line 1 > line 2 > ... > line n2 > > Where each chunk is a header and the lines that follow it (up to the next > header). A header has the number of lines in the chunk as its second field. > > I would like to turn this file into a dictionary like: > dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, line 2, > ... , line n2]} > > Is there a way to do this with a dictionary comprehension or do I have to > iterate over the file with a "while 1" loop? The nice way would be to split the source into a list of chunk texts. But there seems to be no easy way to do this without traversing the source. If the source is generated, just add blank lines (so that the sep is '\n\n'). Then a dict comp can map items using any makeChunk() func. If this is not doable, I would traverse lines using a "while n < s" loop, where n is current line # & and s the size of lines. Denis -- ________________________________ la vita e estrany spir.wikidot.com From norman at khine.net Tue Mar 2 15:21:53 2010 From: norman at khine.net (Norman Khine) Date: Tue, 2 Mar 2010 15:21:53 +0100 Subject: [Tutor] correctly format and insert html block using python into mysql table Message-ID: <9c2c8ffb1003020621k295c454bme79dc28232e71d6e@mail.gmail.com> hello, I have this code: >>> import re >>> import MySQLdb, csv, sys >>> conn = MySQLdb.connect (host = "localhost",user = "usr", passwd= "pass",db = "databasename") >>> c = conn.cursor() >>> file = open('Data/asdsp-lao-farmers-et-batieng-products.html', 'r') >>> data = file.read() >>> get_records = re.compile(r"""
(.*)<\/div>""", re.DOTALL).findall >>> get_titles = re.compile(r"""

(.*)<\/h3>""").findall >>> get_description = re.compile(r"""
(.*)<\/div>""", re.DOTALL).findall >>> block_record = [] >>> block_url = [] >>> records = get_records(data) >>> for record in records: ... description = get_description(record) ... print description # see http://paste.lisp.org/+21XF for output ... c.execute("INSERT INTO a (description) VALUES (%s)", description) >>> c.commit() >>> c.close() the problem is that the 'html' comes out like: http://paste.lisp.org/+21XF is there a way to format the output so that it does not include the \n\t\t and has the correct encoding? thanks norman From steve at pearwood.info Tue Mar 2 16:29:20 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 Mar 2010 02:29:20 +1100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> Message-ID: <201003030229.21233.steve@pearwood.info> On Tue, 2 Mar 2010 05:22:43 pm Andrew Fithian wrote: > Hi tutor, > > I have a large text file that has chunks of data like this: > > headerA n1 > line 1 > line 2 > ... > line n1 > headerB n2 > line 1 > line 2 > ... > line n2 > > Where each chunk is a header and the lines that follow it (up to the > next header). A header has the number of lines in the chunk as its > second field. And what happens if the header is wrong? How do you handle situations like missing headers and empty sections, header lines which are wrong, and duplicate headers? line 1 line 2 headerB 0 headerC 1 line 1 headerD 2 line 1 line 2 line 3 line 4 headerE 23 line 1 line 2 headerB 1 line 1 This is a policy decision: do you try to recover, raise an exception, raise a warning, pad missing lines as blank, throw away excess lines, or what? > I would like to turn this file into a dictionary like: > dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, > line 2, ... , line n2]} > > Is there a way to do this with a dictionary comprehension or do I > have to iterate over the file with a "while 1" loop? I wouldn't do either. I would treat this as a pipe-line problem: you have a series of lines that need to be processed. You can feed them through a pipe-line of filters: def skip_blanks(lines): """Remove leading and trailing whitespace, ignore blank lines.""" for line in lines: line = line.strip() if line: yield line def collate_section(lines): """Return a list of lines that belong in a section.""" current_header = "" accumulator = [] for line in lines: if line.startswith("header"): yield (current_header, accumulator) current_header = line accumulator = [] else: accumulator.append(line) yield (current_header, accumulator) Then put them together like this: fp = open("my_file.dat", "r") data = {} # don't shadow the built-in dict non_blank_lines = skip_blanks(fp) sections = collate_sections(non_blank_lines) for (header, lines) in sections: data[header] = lines Of course you can add your own error checking. -- Steven D'Aprano From lie.1296 at gmail.com Tue Mar 2 16:38:57 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 03 Mar 2010 02:38:57 +1100 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: <4B8BF5AA.6050200@sbcglobal.net> References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: On 03/02/2010 04:13 AM, Wayne Watson wrote: > See Subject. 40K here, but other Python lists allow for larger (total) > sizes. I don't know, I've never realized it; that's an indication that the 40K limit is reasonable, at least to me. What did you get for posting >40K mails? Is your mail bounced? And if it does, is the bounce message helpful, like "please use pastebin or put a link"? From karim.liateni at free.fr Tue Mar 2 19:50:31 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Tue, 02 Mar 2010 19:50:31 +0100 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: <201003020825.47463.steve@pearwood.info> References: <4B8A5921.5000008@free.fr> <4B8C1E9D.8020104@free.fr> <201003020825.47463.steve@pearwood.info> Message-ID: <4B8D5DF7.5050900@free.fr> Hello, Thanks a lot for this state of the art of the language, very instructive. I see now top of the iceberg ;o) Karim Steven D'Aprano wrote: > On Tue, 2 Mar 2010 07:07:57 am Karim Liateni wrote: > >> Thanks for this precision! >> I'm using standard python so this is ok! >> Why people use proprietary python ? >> To have more trouble ? To be different from the rest of community ? >> > > Python is a language, but there can be many different implementations of > that language, just like there are different C compilers or different > Javascript engines. > > CPython is the version which was made first, it is the most common > version, but it is not the only one. It is called CPython because it is > written in C. > > Jython is a version of Python written in Java, and it was created by > people wanting to use Python as a front-end to Java libraries, and to > take advantage of Java's garbage collector. > > IronPython is Microsoft's version of Python written for .Net and Mono. > > PyPy is an experimental version of Python written in Python, used by > people wanting to experiment with Python compilers. > > "Python for S60" is a version of Python written for Nokia's S60 devices. > > CapPython is an experimental version of Python designed for security. > > There are many others, they are all Python, but they have differences. > From gmail at gringer.org Tue Mar 2 23:54:13 2010 From: gmail at gringer.org (David Eccles (gringer)) Date: Wed, 03 Mar 2010 11:54:13 +1300 Subject: [Tutor] getting diagonals from a matrix Message-ID: <4B8D9715.1070609@gringer.org> I've managed to drum up some code to obtain a list containing joined diagonal elements of a matrix (I'm making a word finder generator), but am wondering if there's any better way to do this: # setup so code snippet works properly sizeW = 4 sizeH = 3 puzzleLayout = ['spam'] * (sizeW * sizeH) # Starting with, say, an array with these indices: # 0 1 2 3 # 4 5 6 7 # 8 9 A B # I want the following items for a back diagonal (not in square brackets): # [-2],[3],8 (+5) | div 4 = -1, 1, 2 # [-1],4,9 (+5) | div 4 = -1, 1, 2 # 0,5,A (+5) | div 4 = 0, 1, 2 # 1,6,B (+5) | div 4 = 0, 1, 2 # 2,7,[C] (+5) | div 4 = 0, 1, 3 # 3,[8],[D] (+5) | div 4 = 0, 2, 3 # in other words, increase sequence by sizeW + 1 each time (sizeW - 1 # for forward diagonals), only selecting if the line you're on matches # the line you want to be on # back as in backslash-like diagonal puzzleDiagBack = [(''.join([puzzleLayout[pos*(sizeW+1) + i] \ for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos])) \ for i in range(-sizeH+1,sizeW)] puzzleDiagBackRev = [(''.join(reversed([puzzleLayout[pos*(sizeW+1) + i] \ for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos]))) \ for i in range(-sizeH+1,sizeW)] # fwd as in forwardslash-like diagonal puzzleDiagFwdRev = [(''.join([puzzleLayout[pos*(sizeW-1) + i] \ for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos])) \ for i in range(sizeW+sizeH-1)] puzzleDiagFwd = [(''.join(reversed([puzzleLayout[pos*(sizeW-1) + i] \ for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos]))) \ for i in range(sizeW+sizeH-1)] Cheers, David Eccles (gringer) From jojo.mwebaze at gmail.com Wed Mar 3 09:31:22 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Wed, 3 Mar 2010 09:31:22 +0100 Subject: [Tutor] List comprehension possible with condition statements? Message-ID: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Hi There, i would like to implement the following in lists assuming x = 3 y = 4 z = None i want to create a dynamic list such that mylist = [ x , y, z ] , if z in not None if z is None then mylist = [x,y] Anyhelp! cheers Jojo -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Wed Mar 3 09:36:09 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 09:36:09 +0100 Subject: [Tutor] Encoding Message-ID: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> Hi, i am looking for more informations about encoding in python: i've read that Amazon SimpleDB accepts every string encoded in UTF-8. How can I encode a string? And, what's the default string encoding in python? the other question is about mysql DB: if i have a mysql field latin1 and extract his content in a python script, how can I handle it? thankyou Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Mar 3 09:46:39 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Mar 2010 08:46:39 -0000 Subject: [Tutor] List comprehension possible with condition statements? References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Message-ID: "Jojo Mwebaze" wrote > i would like to implement the following in lists > > assuming > > x = 3 > y = 4 > z = None > > i want to create a dynamic list such that > > mylist = [ x , y, z ] , if z in not None > > if z is None then > > mylist = [x,y] > Assuming you actually mean that you don;t want to include any item that is None you can use an if clause at the end of the comprehension: mylist = [irtem for item in aList where item != None] and aList is any list, which could be [x,y,z] in your example. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From tmatsumoto at gmx.net Wed Mar 3 09:50:59 2010 From: tmatsumoto at gmx.net (C.T. Matsumoto) Date: Wed, 03 Mar 2010 09:50:59 +0100 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Message-ID: <4B8E22F3.7090404@gmx.net> Jojo Mwebaze wrote: > Hi There, > > i would like to implement the following in lists > > assuming > > x = 3 > y = 4 > z = None > > i want to create a dynamic list such that > > mylist = [ x , y, z ] , if z in not None > > if z is None then > > mylist = [x,y] > > Anyhelp! > > cheers > > Jojo > > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Yes a list comprehension can have if statements. You can get the values of x and y pretty simply: >>> [i for i in mylist if i] [3, 4] T From cwitts at compuscan.co.za Wed Mar 3 10:02:14 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 03 Mar 2010 11:02:14 +0200 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Message-ID: <4B8E2596.3010904@compuscan.co.za> Jojo Mwebaze wrote: > Hi There, > > i would like to implement the following in lists > > assuming > > x = 3 > y = 4 > z = None > > i want to create a dynamic list such that > > mylist = [ x , y, z ] , if z in not None > > if z is None then > > mylist = [x,y] > > Anyhelp! > > cheers > > Jojo > > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > | for , So something like `x**2 for x in [1, 2, 3, 4, 5, None, 9] if x != None` would iterate over your input set pumping the current item into the variable x, it will check "if x != None" and if that condition evaluates true it will perform the function you set out to perform. The predicate section acts as a filter to your data set ensuring the variable you are working with meets certain conditions. If you wanted to for eg. still accept the None but perform a different function on it Python does allow it like `x**2 if x else 'Not a number' for x in [1, 2, 3, 4, 5, None, 9]`. Hope that helps. -- Kind Regards, Christian Witts From davea at ieee.org Wed Mar 3 10:03:28 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 04:03:28 -0500 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Message-ID: <4B8E25E0.1050301@ieee.org> Jojo Mwebaze wrote: > Hi There, > > i would like to implement the following in lists > > assuming > > x = 3 > y = 4 > z = None > > i want to create a dynamic list such that > > mylist = [ x , y, z ] , if z in not None > > if z is None then > > mylist = [x,y] > > Anyhelp! > > cheers > > Jojo > > Are there any constraints on x and y ? If you want to throw out all None values, then it's a ready problem. You try it, and if it doesn't quite work, post the code. We'll try to help. But if only the third value is special, then there's little point in making a comprehension of one value. Just conditionally append the z value to the list containing x and y. DaveA From stefan_ml at behnel.de Wed Mar 3 10:06:06 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 03 Mar 2010 10:06:06 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> Message-ID: Giorgio, 03.03.2010 09:36: > i am looking for more informations about encoding in python: > > i've read that Amazon SimpleDB accepts every string encoded in UTF-8. How > can I encode a string? byte_string = unicode_string.encode('utf-8') If you use unicode strings throughout your application, you will be happy with the above. Note that this is an advice, not a condition. > And, what's the default string encoding in python? "default encodings" are bad, don't rely on them. Stefan From jojo.mwebaze at gmail.com Wed Mar 3 10:20:52 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Wed, 3 Mar 2010 10:20:52 +0100 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: <4B8E2596.3010904@compuscan.co.za> References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> <4B8E2596.3010904@compuscan.co.za> Message-ID: <3124be321003030120h786aa4cfk9eb09db1be7efa44@mail.gmail.com> Thanks to everyone, nice ideas! cheers On Wed, Mar 3, 2010 at 10:02 AM, Christian Witts wrote: > Jojo Mwebaze wrote: > >> Hi There, >> >> i would like to implement the following in lists >> >> assuming >> >> x = 3 >> y = 4 >> z = None >> >> i want to create a dynamic list such that >> >> mylist = [ x , y, z ] , if z in not None >> >> if z is None then >> >> mylist = [x,y] >> >> Anyhelp! >> >> cheers >> >> Jojo >> >> >> >> >> >> ------------------------------------------------------------------------ >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > | for , > > So something like `x**2 for x in [1, 2, 3, 4, 5, None, 9] if x != None` > would iterate over your input set pumping the current item into the variable > x, it will check "if x != None" and if that condition evaluates true it will > perform the function you set out to perform. > > The predicate section acts as a filter to your data set ensuring the > variable you are working with meets certain conditions. If you wanted to > for eg. still accept the None but perform a different function on it Python > does allow it like `x**2 if x else 'Not a number' for x in [1, 2, 3, 4, 5, > None, 9]`. > > Hope that helps. > > -- > Kind Regards, > Christian Witts > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From c.t.matsumoto at gmail.com Wed Mar 3 10:22:33 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Wed, 03 Mar 2010 10:22:33 +0100 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: <4B8E25E0.1050301@ieee.org> References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> <4B8E25E0.1050301@ieee.org> Message-ID: <4B8E2A59.1040405@gmail.com> Dave Angel wrote: > Jojo Mwebaze wrote: >> Hi There, >> >> i would like to implement the following in lists >> >> assuming >> >> x = 3 >> y = 4 >> z = None >> >> i want to create a dynamic list such that >> >> mylist = [ x , y, z ] , if z in not None >> >> if z is None then >> >> mylist = [x,y] >> >> Anyhelp! >> >> cheers >> >> Jojo >> >> > > Are there any constraints on x and y ? If you want to throw out all > None values, then it's a ready problem. You try it, and if it doesn't > quite work, post the code. We'll try to help. > > But if only the third value is special, then there's little point in > making a comprehension of one value. Just conditionally append the z > value to the list containing x and y. > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hello, I wrote a solution which isn't correct. >>> [i for i in mylist if i] [3, 4] The if condition should test like the other examples given to the list. >>> [i for i in mylist if i != None] [3, 4] 'if i' would also leave out the integer 0. T From steve at pearwood.info Wed Mar 3 10:43:43 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 Mar 2010 20:43:43 +1100 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> Message-ID: <201003032043.44183.steve@pearwood.info> On Wed, 3 Mar 2010 07:46:39 pm Alan Gauld wrote: > mylist = [irtem for item in aList where item != None] Comparisons with None almost always should be one of: item is None item is not None The reason is that "item is None" is ONLY ever true if the item actually is the singleton object None (accept no substitutes!). On the other hand, "item == None" might be true for some customer items. So if you actually *do* want to accept substitutes, you can use ==, but that would be an unusual thing to do, and worthy of a comment explaining that you did mean == and it isn't a mistake. Likewise for "item != None" versus "item is not None". -- Steven D'Aprano From tmatsumoto at gmx.net Wed Mar 3 10:56:23 2010 From: tmatsumoto at gmx.net (C.T. Matsumoto) Date: Wed, 03 Mar 2010 10:56:23 +0100 Subject: [Tutor] sorting algorithm Message-ID: <4B8E3247.4050500@gmx.net> Hello, This is follow up on a question I had about algorithms. In the thread it was suggested I make my own sorting algorithm. Here are my results. #!/usr/bin/python def sort_(list_): for item1 in list_: pos1 = list_.index(item1) pos2 = pos1 + 1 try: item2 = list_[pos2] except IndexError: pass if item1 >= item2: try: list_.pop(pos2) list_.insert(pos1, item2) return True except IndexError: pass def mysorter(list_): while sort_(list_) is True: sort_(list_) I found this to be a great exercise. In doing the exercise, I got pretty stuck. I consulted another programmer (my dad) who described how to go about sorting. As it turned out the description he described was the Bubble sort algorithm. Since coding the solution I know the Bubble sort is inefficient because of repeated iterations over the entire list. This shed light on the quick sort algorithm which I'd like to have a go at. Something I haven't tried is sticking in really large lists. I was told that with really large list you break down the input list into smaller lists. Sort each list, then go back and use the same swapping procedure for each of the different lists. My question is, at what point to you start breaking things up? Is that based on list elements or is it based on memory(?) resources python is using? One thing I'm not pleased about is the while loop and I'd like to replace it with a for loop. Thanks, T From denis.spir at gmail.com Wed Mar 3 11:56:14 2010 From: denis.spir at gmail.com (spir) Date: Wed, 3 Mar 2010 11:56:14 +0100 Subject: [Tutor] Why is the max size so low in this mail list? In-Reply-To: References: <4B8BF5AA.6050200@sbcglobal.net> Message-ID: <20100303115614.6aa1600f@o> On Wed, 03 Mar 2010 02:38:57 +1100 Lie Ryan wrote: > On 03/02/2010 04:13 AM, Wayne Watson wrote: > > See Subject. 40K here, but other Python lists allow for larger (total) > > sizes. > > I don't know, I've never realized it; that's an indication that the 40K > limit is reasonable, at least to me. A pocket book typically holds ~ 1000 chars per page. Even with a long thread accumulation (because people do not always cut off irrelevant parts), 40K is more than enough. This post holds 651 chars (spaces, signature & this comment all included). Denis -- ________________________________ la vita e estrany spir.wikidot.com From patrick.just4fun at gmail.com Wed Mar 3 12:24:41 2010 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Wed, 03 Mar 2010 12:24:41 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> Message-ID: <4B8E46F9.2040601@gmail.com> Giorgio wrote: > i am looking for more informations about encoding in python: > > i've read that Amazon SimpleDB accepts every string encoded in UTF-8. > How can I encode a string? And, what's the default string encoding in > python? I think the safest way is to use unicode strings in your application and convert them to byte strings if needed, using the encode and decode methods. > > the other question is about mysql DB: if i have a mysql field latin1 and > extract his content in a python script, how can I handle it? if you have a byte string s encoded in 'latin1' you can simply call: s.decode('latin1') to get the unicode string. > thankyou > > Giorgio Patrick From anothernetfellow at gmail.com Wed Mar 3 14:09:03 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 14:09:03 +0100 Subject: [Tutor] Encoding In-Reply-To: References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> Message-ID: <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> > > >> byte_string = unicode_string.encode('utf-8') > > If you use unicode strings throughout your application, you will be happy > with the above. Note that this is an advice, not a condition. Mmm ok. So all strings in the app are unicode by default? Do you know if there is a function/method i can use to check encoding of a string? > > "default encodings" are bad, don't rely on them. > No, ok, it was just to understand what i'm working with. Patrick, ok. I should check if it's possible to save unicode strings in the DB. Do you think i'd better set my db to utf8? I don't need latin1, it's just the default value. Thankyou Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Wed Mar 3 14:11:57 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 14:11:57 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> Message-ID: <23ce85921003030511h48737998t87e31e4ed803ca10@mail.gmail.com> Oh, sorry, let me update my last post: if i have a string, let's say: s = "hi giorgio"; and want to store it in a latin1 db, i need to convert it to latin1 before storing, right? 2010/3/3 Giorgio > >>> byte_string = unicode_string.encode('utf-8') >> >> If you use unicode strings throughout your application, you will be happy >> with the above. Note that this is an advice, not a condition. > > > > Mmm ok. So all strings in the app are unicode by default? > > Do you know if there is a function/method i can use to check encoding of a > string? > > >> >> "default encodings" are bad, don't rely on them. >> > > No, ok, it was just to understand what i'm working with. > > Patrick, ok. I should check if it's possible to save unicode strings in the > DB. > > Do you think i'd better set my db to utf8? I don't need latin1, it's just > the default value. > > Thankyou > > Giorgio > > > -- > -- > AnotherNetFellow > Email: anothernetfellow at gmail.com > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Mar 3 14:17:45 2010 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Mar 2010 08:17:45 -0500 Subject: [Tutor] Bowing out Message-ID: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Hi all, After six years of tutor posts my interest and energy have waned and I'm ready to move on to something new. I'm planning to stop reading and contributing to the list. I have handed over list moderation duties to Alan Gauld and Wesley Chun. Thanks to everyone who contributes questions and answers. I learned a lot from my participation here. So long and keep coding! Kent From patrick.just4fun at gmail.com Wed Mar 3 14:32:22 2010 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Wed, 03 Mar 2010 14:32:22 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> Message-ID: <4B8E64E6.5080302@gmail.com> > Mmm ok. So all strings in the app are unicode by default? > Depends on your python version. If you use python 2.x, you have to use a u before the string: s = u'Hallo World' > Do you know if there is a function/method i can use to check encoding of > a string? AFAIK such a function doesn't exist. Python3 solves this by using unicode strings by default. > Patrick, ok. I should check if it's possible to save unicode strings in > the DB. It is more an issue of your database adapter, than of your database. > > Do you think i'd better set my db to utf8? I don't need latin1, it's > just the default value. I think the encoding of the db doesn't matter much in this case, but I would prefer utf-8 over latin-1. If you get an utf-8 encoded raw byte string you call .decode('utf-8'). In case of an latin-1 encoded string you call .decode('latin1') > Thankyou > > Giorgio - Patrick From kp8 at mac.com Wed Mar 3 14:39:10 2010 From: kp8 at mac.com (kevin parks) Date: Wed, 03 Mar 2010 22:39:10 +0900 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: Dang! I wish you were not going. But really, I have to say a HUGE thank you to you for all the fine teaching you have done on this list. I learned so much from reading your posts. Thanks for all the time and effort (and code) you put into this! I wish you were staying. Hats off to you. Wish there was some way to give a standing ovation over the internet. Glad to see though that the list has been handed off to 2 fantastic folks but still sad to see you go. Wish Danny Yoo was still here too. Thank you Kent. Thank you very much~ -kevin-- On Mar 3, 2010, at 10:17 PM, Kent Johnson wrote: > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From anand.shashwat at gmail.com Wed Mar 3 15:17:56 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Wed, 3 Mar 2010 19:47:56 +0530 Subject: [Tutor] Bowing out In-Reply-To: References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: Kent We salute you. Thanks for everything you did for the community. ~l0nwlf On Wed, Mar 3, 2010 at 7:09 PM, kevin parks wrote: > Dang! > > I wish you were not going. But really, I have to say a HUGE thank you to > you for all the fine teaching you have done on this list. I learned so much > from reading your posts. Thanks for all the time and effort (and code) you > put into this! I wish you were staying. Hats off to you. Wish there was some > way to give a standing ovation over the internet. Glad to see though that > the list has been handed off to 2 fantastic folks but still sad to see you > go. Wish Danny Yoo was still here too. > > Thank you Kent. Thank you very much~ > > -kevin-- > > > > > On Mar 3, 2010, at 10:17 PM, Kent Johnson wrote: > > Hi all, >> >> After six years of tutor posts my interest and energy have waned and >> I'm ready to move on to something new. I'm planning to stop reading >> and contributing to the list. I have handed over list moderation >> duties to Alan Gauld and Wesley Chun. >> >> Thanks to everyone who contributes questions and answers. I learned a >> lot from my participation here. >> >> So long and keep coding! >> Kent >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Wed Mar 3 15:21:50 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 03 Mar 2010 15:21:50 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> Message-ID: Giorgio, 03.03.2010 14:09: >>> byte_string = unicode_string.encode('utf-8') >> >> If you use unicode strings throughout your application, you will be happy >> with the above. Note that this is an advice, not a condition. > > Mmm ok. So all strings in the app are unicode by default? > > Do you know if there is a function/method i can use to check encoding of a > string? Not sure what exactly you mean here. If you meant to say "guess the encoding of a byte string", then there are a few ways to do that. But none of them is guaranteed to work. Therefore my advice: use unicode everywhere and decode byte strings on the way in, where you (hopefully) know their encoding. If you don't know the encoding on the way in, reject the input. Stefan From emadnawfal at gmail.com Wed Mar 3 15:40:58 2010 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Wed, 3 Mar 2010 09:40:58 -0500 Subject: [Tutor] Bowing out In-Reply-To: References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <652641e91003030640m1c6682f0le4f7244a8f0b41bc@mail.gmail.com> On Wed, Mar 3, 2010 at 9:17 AM, Shashwat Anand wrote: > Kent > > We salute you. Thanks for everything you did for the community. > > ~l0nwlf > > > On Wed, Mar 3, 2010 at 7:09 PM, kevin parks wrote: > >> Dang! >> >> I wish you were not going. But really, I have to say a HUGE thank you to >> you for all the fine teaching you have done on this list. I learned so much >> from reading your posts. Thanks for all the time and effort (and code) you >> put into this! I wish you were staying. Hats off to you. Wish there was some >> way to give a standing ovation over the internet. Glad to see though that >> the list has been handed off to 2 fantastic folks but still sad to see you >> go. Wish Danny Yoo was still here too. >> >> Thank you Kent. Thank you very much~ >> >> -kevin-- >> >> >> >> >> On Mar 3, 2010, at 10:17 PM, Kent Johnson wrote: >> >> Hi all, >>> >>> After six years of tutor posts my interest and energy have waned and >>> I'm ready to move on to something new. I'm planning to stop reading >>> and contributing to the list. I have handed over list moderation >>> duties to Alan Gauld and Wesley Chun. >>> >>> Thanks to everyone who contributes questions and answers. I learned a >>> lot from my participation here. >>> >>> So long and keep coding! >>> Kent >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Thank you Kent, I personally learned a lot from you. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Wed Mar 3 15:50:25 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 15:50:25 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B8E64E6.5080302@gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> Message-ID: <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> > > >> Depends on your python version. If you use python 2.x, you have to use a > u before the string: > > s = u'Hallo World' Ok. So, let's go back to my first question: s = u'Hallo World' is unicode in python 2.x -> ok s = 'Hallo World' how is encoded? >> I think the encoding of the db doesn't matter much in this case, but I > would prefer utf-8 over latin-1. If you get an utf-8 encoded raw byte string > you call .decode('utf-8'). In case of an latin-1 encoded string you call > .decode('latin1') > Ok, setting it to UTF-8. > If you don't know the encoding on the way in, reject the input. > > Well, the problem comes, i.e when i'm getting a string from an HTML form with POST. I don't and can't know the encoding, right? It depends on browser. Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From vceder at canterburyschool.org Wed Mar 3 15:55:08 2010 From: vceder at canterburyschool.org (Vern Ceder) Date: Wed, 03 Mar 2010 09:55:08 -0500 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <4B8E784C.2030701@canterburyschool.org> Kent, Thanks for all of the work you've done over the years to help make this one of the best/most useful lists around. Enjoy the new challenges ahead! It was great to finally meet you at PyCon! Cheers, Vern Kent Johnson wrote: > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW From dsarmientos at gmail.com Wed Mar 3 15:58:08 2010 From: dsarmientos at gmail.com (Daniel Sarmiento) Date: Wed, 3 Mar 2010 09:58:08 -0500 Subject: [Tutor] Bowing out Message-ID: > > Date: Wed, 3 Mar 2010 08:17:45 -0500 > From: Kent Johnson > To: Tutor at python.org > Subject: [Tutor] Bowing out > Message-ID: > <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > Thank you very much. I wish you the best of luck. I know a lot of people learned a lot from your postings and your (Kent's Korner site) I hope you keep it online... again, thank you for all the years you helped the python community. Farewell Kent. Daniel -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Wed Mar 3 16:00:52 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Wed, 3 Mar 2010 09:00:52 -0600 Subject: [Tutor] Bowing out In-Reply-To: <652641e91003030640m1c6682f0le4f7244a8f0b41bc@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> <652641e91003030640m1c6682f0le4f7244a8f0b41bc@mail.gmail.com> Message-ID: <333efb451003030700k465f2d51lf8b3d2e5b915c99b@mail.gmail.com> I can only add my personal thanks, and echo the sentiments of others. I'm certainly glad the archives exist, and that those inheriting responsibility are certainly well qualified. So long and thanks for all the fish! -Wayne 2010/3/3 Emad Nawfal (??? ???? ???) > > > On Wed, Mar 3, 2010 at 9:17 AM, Shashwat Anand wrote: > >> Kent >> >> We salute you. Thanks for everything you did for the community. >> >> ~l0nwlf >> >> >> On Wed, Mar 3, 2010 at 7:09 PM, kevin parks wrote: >> >>> Dang! >>> >>> I wish you were not going. But really, I have to say a HUGE thank you to >>> you for all the fine teaching you have done on this list. I learned so much >>> from reading your posts. Thanks for all the time and effort (and code) you >>> put into this! I wish you were staying. Hats off to you. Wish there was some >>> way to give a standing ovation over the internet. Glad to see though that >>> the list has been handed off to 2 fantastic folks but still sad to see you >>> go. Wish Danny Yoo was still here too. >>> >>> Thank you Kent. Thank you very much~ >>> >>> -kevin-- >>> >>> >>> >>> >>> On Mar 3, 2010, at 10:17 PM, Kent Johnson wrote: >>> >>> Hi all, >>>> >>>> After six years of tutor posts my interest and energy have waned and >>>> I'm ready to move on to something new. I'm planning to stop reading >>>> and contributing to the list. I have handed over list moderation >>>> duties to Alan Gauld and Wesley Chun. >>>> >>>> Thanks to everyone who contributes questions and answers. I learned a >>>> lot from my participation here. >>>> >>>> So long and keep coding! >>>> Kent >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> Thank you Kent, > I personally learned a lot from you. > > > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? > ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- 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 modulok at gmail.com Wed Mar 3 16:03:15 2010 From: modulok at gmail.com (Modulok) Date: Wed, 3 Mar 2010 08:03:15 -0700 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <64c038661003030703j9207275n8d70470934b159c7@mail.gmail.com> > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. Kent, I'm a relative newcomer, but even so, I benefited from you. Thanks for giving what you could! Best of luck with your new endeavors. Hope to see you check back in from time to time. -Modulok- From phil at xfr.co.uk Wed Mar 3 16:11:56 2010 From: phil at xfr.co.uk (Philip Kilner) Date: Wed, 03 Mar 2010 15:11:56 +0000 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <4B8E7C3C.5020308@xfr.co.uk> Hi Kent, Thank you! -- Regards, PhilK 'work as if you lived in the early days of a better nation' - alasdair gray -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 5583 bytes Desc: S/MIME Cryptographic Signature URL: From stefan_ml at behnel.de Wed Mar 3 16:12:38 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 03 Mar 2010 16:12:38 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> Message-ID: Giorgio, 03.03.2010 15:50: >>> Depends on your python version. If you use python 2.x, you have to use a >> u before the string: >> >> s = u'Hallo World' > > Ok. So, let's go back to my first question: > > s = u'Hallo World' is unicode in python 2.x -> ok Correct. > s = 'Hallo World' how is encoded? Depends on your source code encoding. http://www.python.org/dev/peps/pep-0263/ > Well, the problem comes, i.e when i'm getting a string from an HTML form > with POST. I don't and can't know the encoding, right? It depends on > browser. The browser will tell you the encoding in the headers that it transmits. Stefan From patrick.just4fun at gmail.com Wed Mar 3 16:22:03 2010 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Wed, 03 Mar 2010 16:22:03 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> Message-ID: <4B8E7E9B.9000703@gmail.com> Giorgio wrote: > > Depends on your python version. If you use python 2.x, you have to > use a u before the string: > > s = u'Hallo World' > > > Ok. So, let's go back to my first question: > > s = u'Hallo World' is unicode in python 2.x -> ok > s = 'Hallo World' how is encoded? I am not 100% sure, but I think it depends on the encoding of your source file or the coding you specify. See PEP 263 http://www.python.org/dev/peps/pep-0263/ > Well, the problem comes, i.e when i'm getting a string from an HTML > form with POST. I don't and can't know the encoding, right? It depends > on browser. Right, but you can do something about it. Tell the browser, which encoding you are going to accept:
...
- Patrick From davea at ieee.org Wed Mar 3 16:25:27 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 10:25:27 -0500 Subject: [Tutor] sorting algorithm In-Reply-To: <4B8E3247.4050500@gmx.net> References: <4B8E3247.4050500@gmx.net> Message-ID: <4B8E7F67.5030602@ieee.org> C.T. Matsumoto wrote: > Hello, > > This is follow up on a question I had about algorithms. In the thread > it was suggested I make my own sorting algorithm. > > Here are my results. > > #!/usr/bin/python > > def sort_(list_): > for item1 in list_: > pos1 = list_.index(item1) > pos2 = pos1 + 1 > try: > item2 = list_[pos2] > except IndexError: > pass > > if item1 >= item2: > try: > list_.pop(pos2) > list_.insert(pos1, item2) > return True > except IndexError: > pass > > def mysorter(list_): > while sort_(list_) is True: > sort_(list_) > > I found this to be a great exercise. In doing the exercise, I got > pretty stuck. I consulted another programmer (my dad) who described > how to go about sorting. As it turned out the description he described > was the Bubble sort algorithm. Since coding the solution I know the > Bubble sort is inefficient because of repeated iterations over the > entire list. This shed light on the quick sort algorithm which I'd > like to have a go at. > > Something I haven't tried is sticking in really large lists. I was > told that with really large list you break down the input list into > smaller lists. Sort each list, then go back and use the same swapping > procedure for each of the different lists. My question is, at what > point to you start breaking things up? Is that based on list elements > or is it based on memory(?) resources python is using? > > One thing I'm not pleased about is the while loop and I'd like to > replace it with a for loop. > > Thanks, > > T > > There are lots of references on the web about Quicksort, including a video at: http://www.youtube.com/watch?v=y_G9BkAm6B8 which I think illustrates it pretty well. It would be a great learning exercise to implement Python code directly from that description, without using the sample C++ code available. (Incidentally, there are lots of variants of Quicksort, so I'm not going to quibble about whether this is the "right" one to be called that.) I don't know what your earlier thread was, since you don't mention the subject line, but there are a number of possible reasons you might not have wanted to use the built-in sort. The best one is for educational purposes. I've done my own sort for various reasons in the past, even though I had a library function, since the library function had some limits. One time I recall, the situation was that the library sort was limited to 64k of total data, and I had to work with much larger arrays (this was in 16bit C++, in "large" model). I solved the size problem by using the C++ sort library on 16k subsets (because a pointer was 2*2 bytes). Then I merged the results of the sorts. At the time, and in the circumstances involved, there were seldom more than a dozen or so sublists to merge, so this approach worked well enough. Generally, it's better for both your development time and the efficiency and reliabilty of the end code, to base a new sort mechanism on the existing one. In my case above, I was replacing what amounted to an insertion sort, and achieved a 50* improvement for a real customer. It was fast enough that other factors completely dominated his running time. But for learning purposes? Great plan. So now I'll respond to your other questions, and comment on your present algorithm. It would be useful to understand about algorithmic complexity, the so called Order Function. In a bubble sort, if you double the size of the array, you quadruple the number of comparisons and swaps. It's order N-squared or O(n*n). So what works well for an array of size 10 might take a very long time for an array of size 10000 (like a million times as long). You can do much better by sorting smaller lists, and then combining them together. Such an algorithm can be O(n*log(n)). You ask at what point you consider sublists? In a language like C, the answer is when the list is size 3 or more. For anything larger than 2, you divide into sublists, and work on them. Now, if I may comment on your code. You're modifying a list while you're iterating through it in a for loop. In the most general case, that's undefined. I think it's safe in this case, but I would avoid it anyway, by just using xrange(len(list_)-1) to iterate through it. You use the index function to find something you would already know -- the index function is slow. And the first try/except isn't needed if you use a -1 in the xrange argument, as I do above. You use pop() and push() to exchange two adjacent items in the list. Both operations copy the remainder of the list, so they're rather slow. Since you're exchanging two items in the list, you can simply do that: list[pos1], list[pos2] = list[pos2], list[pos1] That also eliminates the need for the second try/except. You mention being bothered by the while loop. You could replace it with a simple for loop with xrange(len(list_)), since you know that N passes will always be enough. But if the list is partially sorted, your present scheme will end sooner. And if it's fully sorted, it'll only take one pass over the data. There are many refinements you could do. For example, you don't have to stop the inner loop after the first swap. You could finish the buffer, swapping any other pairs that are out of order. You'd then be saving a flag indicating if you did any swaps. You could keep a index pointing to the last pair you swapped on the previous pass, and use that for a limit next time. Then you just terminate the outer loop when that limit value is 1. You could even keep two limit values, and bubble back and forth between them, as they gradually close into the median of the list. You quit when they collide in the middle. The resultant function should be much faster for medium-sized lists, but it still will slow down quadratically as the list size increases. You still need to divide and conquer, and quicksort is just one way of doing that. DaveA From davea at ieee.org Wed Mar 3 16:30:52 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 10:30:52 -0500 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <4B8E80AC.6010704@ieee.org> Kent Johnson wrote: > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > > I'm sorry to see you go as well. I've learned an awful lot from your posts over the couple of years I've been here. Thanks for all the efforts. DaveA From anothernetfellow at gmail.com Wed Mar 3 16:32:01 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 16:32:01 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B8E7E9B.9000703@gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E7E9B.9000703@gmail.com> Message-ID: <23ce85921003030732k6830eabx492208e15a7d15f9@mail.gmail.com> Uff, encoding is a very painful thing in programming. Ok so now comes last "layer" of the encoding: the webserver. I now know how to handle encoding in a python app and in interactions with the db, but the last step is sending the content to the webserver. How should i encode pages? The encoding i choose has to be the same than the one i choose in the .htaccess file? Or maybe i can send content encoded how i like more to apache and it re-encodes in the right way all pages? Thankyou 2010/3/3 Patrick Sabin > Giorgio wrote: > >> >> Depends on your python version. If you use python 2.x, you have to >> use a u before the string: >> >> s = u'Hallo World' >> >> >> Ok. So, let's go back to my first question: >> s = u'Hallo World' is unicode in python 2.x -> ok >> s = 'Hallo World' how is encoded? >> > > I am not 100% sure, but I think it depends on the encoding of your source > file or the coding you specify. See PEP 263 > http://www.python.org/dev/peps/pep-0263/ > > > Well, the problem comes, i.e when i'm getting a string from an HTML form >> with POST. I don't and can't know the encoding, right? It depends on >> browser. >> > > Right, but you can do something about it. Tell the browser, which encoding > you are going to accept: > >
> ... >
> > - Patrick > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Mar 3 16:43:20 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 10:43:20 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> Message-ID: <4B8E8398.8040902@ieee.org> Giorgio wrote: >> >>> Depends on your python version. If you use python 2.x, you have to use a >>> >> u before the string: >> >> s = u'Hallo World' >> > > > Ok. So, let's go back to my first question: > > s = u'Hallo World' is unicode in python 2.x -> ok > s = 'Hallo World' how is encoded? > > > Since it's a quote literal in your source code, it's encoded by your text editor when it saves the file, and you tell Python which encoding it was by the second line of your source file, right after the shebang line. A sequence of bytes in an html file should be should have its encoding identified by the tag at the top of the html file. And I'd *guess* that on a form result, the encoding can be assumed to match that of the html of the form itself. DaveA From sander.sweers at gmail.com Wed Mar 3 17:06:11 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 3 Mar 2010 17:06:11 +0100 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: On 3 March 2010 14:17, Kent Johnson wrote: > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. Let me join the other people and thank you for your contribution to this list. Good luck with something new :-) Greets Sander From anothernetfellow at gmail.com Wed Mar 3 17:10:05 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 17:10:05 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B8E8398.8040902@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> Message-ID: <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> Ok. So, how do you encode .py files? UTF-8? 2010/3/3 Dave Angel > Giorgio wrote: > >> >>> >>>> Depends on your python version. If you use python 2.x, you have to use >>>> a >>>> >>>> >>> u before the string: >>> >>> s = u'Hallo World' >>> >>> >> >> >> Ok. So, let's go back to my first question: >> >> s = u'Hallo World' is unicode in python 2.x -> ok >> s = 'Hallo World' how is encoded? >> >> >> > Since it's a quote literal in your source code, it's encoded by your text > editor when it saves the file, and you tell Python which encoding it was by > the second line of your source file, right after the shebang line. > > A sequence of bytes in an html file should be should have its encoding > identified by the tag at the top of the html file. And I'd *guess* that on > a form result, the encoding can be assumed to match that of the html of the > form itself. > > DaveA > > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Wed Mar 3 17:18:52 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 3 Mar 2010 08:18:52 -0800 (PST) Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <583388.77387.qm@web110702.mail.gq1.yahoo.com> Hi Kent, Thank you very much for sharing your knowledge. Much appreciated! Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Wed, 3/3/10, Kent Johnson wrote: From: Kent Johnson Subject: [Tutor] Bowing out To: Tutor at python.org Date: Wednesday, March 3, 2010, 2:17 PM Hi all, After six years of tutor posts my interest and energy have waned and I'm ready to move on to something new. I'm planning to stop reading and contributing to the list. I have handed over list moderation duties to Alan Gauld and Wesley Chun. Thanks to everyone who contributes questions and answers. I learned a lot from my participation here. So long and keep coding! Kent _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From gmoonn at gmail.com Wed Mar 3 17:31:40 2010 From: gmoonn at gmail.com (Gman) Date: Wed, 03 Mar 2010 11:31:40 -0500 Subject: [Tutor] no bowing out for you Message-ID: Nope ya can't do it Kent, we wont have it ! Looks like a good time to start a patitiion to get you a salary or something to keep you on :) From emile at fenx.com Wed Mar 3 17:30:35 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 03 Mar 2010 08:30:35 -0800 Subject: [Tutor] getting diagonals from a matrix In-Reply-To: <4B8D9715.1070609@gringer.org> References: <4B8D9715.1070609@gringer.org> Message-ID: On 3/2/2010 2:54 PM David Eccles (gringer) said... > I've managed to drum up some code to obtain a list containing joined diagonal > elements of a matrix (I'm making a word finder generator), but am wondering if > there's any better way to do this: This works. Lots of other ways would work too. What would make one better than another? Anyway, here's my take on it... # # setup so code snippet works properly sizeW = 4 sizeH = 3 puzzleLayout = ['spam'] * (sizeW * sizeH) # this allows the result to match your example from string import digits,letters puzzleLayout = (digits+letters)[:sizeW * sizeH] # Starting with, say, an array with these indices: # It might help to consider the indices numbers as that's what # python does, so I've assumed these to be the values instead. # 0 1 2 3 # 4 5 6 7 # 8 9 A B # Looking at the index table above, note that all the results # start on an edge and work diagonally from there. I'll guess # you found the back diags easier -- no messy 'in-the-same-row' # problems -- but the forward diags are harder. Note if we # flype the table the forward diags can be built from the back # diag instructions #FpuzzleLayout # 8 9 A B # 4 5 6 7 # 0 1 2 3 # so we'll build it once like this... FpuzzleLayout = "".join( [ puzzleLayout[ii-sizeW:ii] for ii in range(sizeW*sizeH,0,-sizeW) ] ) #So now, the only starting positions we need to worry about are # on the left and top edge ignoring the start and end corners. edge = (range(sizeW * sizeH,0,-sizeW)+range(sizeW-1))[2:] # and to avoid any modular impact, we'll add a length constraint lens = range(2,sizeH+1)+range(sizeW-1,1,-1) # and do it result = [] for ii,ln in zip(edge,lens): result.append(puzzleLayout[ii::sizeW+1][:ln]) result.append(FpuzzleLayout[ii::sizeW+1][:ln]) # now add in the reversed results for ii in result[:]: result.append("".join(reversed(ii))) # and the corners result.extend([puzzleLayout[0], puzzleLayout[-1], FpuzzleLayout[0], FpuzzleLayout[-1]]) # Emile > > # I want the following items for a back diagonal (not in square brackets): > # [-2],[3],8 (+5) | div 4 = -1, 1, 2 > # [-1],4,9 (+5) | div 4 = -1, 1, 2 > # 0,5,A (+5) | div 4 = 0, 1, 2 > # 1,6,B (+5) | div 4 = 0, 1, 2 > # 2,7,[C] (+5) | div 4 = 0, 1, 3 > # 3,[8],[D] (+5) | div 4 = 0, 2, 3 > > # in other words, increase sequence by sizeW + 1 each time (sizeW - 1 > # for forward diagonals), only selecting if the line you're on matches > # the line you want to be on > > # back as in backslash-like diagonal > puzzleDiagBack = [(''.join([puzzleLayout[pos*(sizeW+1) + i] \ > for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos])) \ > for i in range(-sizeH+1,sizeW)] > puzzleDiagBackRev = [(''.join(reversed([puzzleLayout[pos*(sizeW+1) + i] \ > for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos]))) \ > for i in range(-sizeH+1,sizeW)] > # fwd as in forwardslash-like diagonal > puzzleDiagFwdRev = [(''.join([puzzleLayout[pos*(sizeW-1) + i] \ > for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos])) \ > for i in range(sizeW+sizeH-1)] > puzzleDiagFwd = [(''.join(reversed([puzzleLayout[pos*(sizeW-1) + i] \ > for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos]))) \ > for i in range(sizeW+sizeH-1)] > > Cheers, > David Eccles (gringer) > _______________________________________________ > Tutor maillist -Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From Mike.Hansen at atmel.com Wed Mar 3 17:36:15 2010 From: Mike.Hansen at atmel.com (Hansen, Mike) Date: Wed, 3 Mar 2010 09:36:15 -0700 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <7941B2693F32294AAF16C26B679A258D0F0F7477@csomb01.corp.atmel.com> > -----Original Message----- > From: tutor-bounces+mike.hansen=atmel.com at python.org > [mailto:tutor-bounces+mike.hansen=atmel.com at python.org] On > Behalf Of Kent Johnson > Sent: Wednesday, March 03, 2010 6:18 AM > To: Tutor at python.org > Subject: [Tutor] Bowing out > > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent We know what's really going on. You are moving to Ruby Tutor. =) Good luck with your "something new", and thanks for the many years of help on this list. Mike From dwightdhutto at yahoo.com Wed Mar 3 17:45:41 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Wed, 3 Mar 2010 08:45:41 -0800 (PST) Subject: [Tutor] Script Building Message-ID: <3291.23221.qm@web45313.mail.sp1.yahoo.com> I'm new to Python, so I thought as a first project I'd create a basic script building wizard to help with my understanding of the flow, and for basic practice. The following is a outline of what I would initially like to accomplish. I've begun creating small functions, and very lightly worded pseudocode, but before I go further I'd like to make sure I'm building my script insertions correctly, and see if any one has any suggestions on how to refine the process, or what I might be able to use as additives to sub-levels of my insertions. Here is a basic outline of the insertions from either raw_input or, drop down menu: append paths import modules from modules import* import module as x global variables docs version class class with docs def instance comment to line x This is the initial step-by step process which the script will create: 1)Ask for existing or new project Would you like to open an existing project or create a new one? 1a) if create new go to step 2 1b) if open existing project go to step 3a *************************************** 2)Create new project or file Would you like to created a new project, or a single file? 2a) if create single file go to step 4 2b) if create a project go to step 5 *************************************** 3)Select the folder of your existing project 3a) open project, which would be a folder, not a single file, go to step 6a 3b) open single existing file go to step 6b *************************************** 4)Setup for single file creation 4a) What would you like to name your new file? raw_input 4b) would you like to use the script wizard to setup your new file? if yes go to script wizard(it's a choose your own adventure book) if no go to step 7a ************************************** 5)Set up for project creation 5a) Two fields to fill in What would you like to name your new project? raw_input You must create a new file to go in your project folder. What would you like to call your first file? raw_input got to step 7b ************************************** ************************************** 6)open project window, or single file, depending on which of step 3 user is coming from, then begins 'recording' the current sessions input, until save or exit projects will have multiple files, so history for each file open within the project 6a) Open window with selection sidepanel of all files in project folder 6b) Window with single file, and now side panel ***************************************** **************************************** 7) New file has been created, or new project has been created with blank file 7a) open new window with file, but no open project folder 7b) open edit window with blank file and project folder contents, which at this point consist of the single blank named file ****************************************** ******************************************* sequence of events above have terminated at the editor window with open file ****************************************** ****************************************** The above excludes insertions, saving, creating a file or project into the main project window, which will probably call on some of the code from above. If anyone is familiar with it, I'm thinking of using the blender game engine(just to combine the learning process of both), but the overall code is python, so I'm pretty sure I'm posting this to the right list. Any suggestions, or critiques, are welcome. Thanks, David -------------- next part -------------- An HTML attachment was scrubbed... URL: From webtourist at gmail.com Wed Mar 3 17:51:01 2010 From: webtourist at gmail.com (Robert) Date: Wed, 3 Mar 2010 11:51:01 -0500 Subject: [Tutor] Bowing out In-Reply-To: References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <8af18dcf1003030851p41f82a76jdd79f8109dc7bad8@mail.gmail.com> so you're "done" with Python ? :) > On 3 March 2010 14:17, Kent Johnson wrote: >> After six years of tutor posts my interest and energy have waned and >> I'm ready to move on to something new. From dwightdhutto at yahoo.com Wed Mar 3 17:55:57 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Wed, 3 Mar 2010 08:55:57 -0800 (PST) Subject: [Tutor] Bowing out In-Reply-To: Message-ID: <954488.968.qm@web45302.mail.sp1.yahoo.com> --- On Wed, 3/3/10, Sander Sweers wrote: From: Sander Sweers Subject: Re: [Tutor] Bowing out To: "Kent Johnson" Cc: Tutor at python.org Date: Wednesday, March 3, 2010, 11:06 AM On 3 March 2010 14:17, Kent Johnson wrote: > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. Let me join the other people and thank you for your contribution to this list. Good luck with something new :-) Greets Sander _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Even if you didn't give me any personal help, I thank people like you. ...I bet this looks great on a resume. -------------- next part -------------- An HTML attachment was scrubbed... URL: From glenbot at gmail.com Wed Mar 3 18:16:08 2010 From: glenbot at gmail.com (Glen Zangirolami) Date: Wed, 3 Mar 2010 11:16:08 -0600 Subject: [Tutor] Bowing out In-Reply-To: <583388.77387.qm@web110702.mail.gq1.yahoo.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> <583388.77387.qm@web110702.mail.gq1.yahoo.com> Message-ID: Thank you for all your hard work! I learned a ton from your tutorials. Good luck on your future endeavors. On Wed, Mar 3, 2010 at 10:18 AM, Albert-Jan Roskam wrote: > Hi Kent, > > Thank you very much for sharing your knowledge. Much appreciated! > > Cheers!! > Albert-Jan > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > In the face of ambiguity, refuse the temptation to guess. > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > --- On *Wed, 3/3/10, Kent Johnson * wrote: > > > From: Kent Johnson > Subject: [Tutor] Bowing out > To: Tutor at python.org > Date: Wednesday, March 3, 2010, 2:17 PM > > > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From glenbot at gmail.com Wed Mar 3 18:22:26 2010 From: glenbot at gmail.com (Glen Zangirolami) Date: Wed, 3 Mar 2010 11:22:26 -0600 Subject: [Tutor] getting diagonals from a matrix In-Reply-To: <4B8D9715.1070609@gringer.org> References: <4B8D9715.1070609@gringer.org> Message-ID: I am not really sure of a better way but if your looking for a way to make your code cleaner or more efficient you can try Numpy - http://numpy.scipy.org/ On Tue, Mar 2, 2010 at 4:54 PM, David Eccles (gringer) wrote: > I've managed to drum up some code to obtain a list containing joined > diagonal > elements of a matrix (I'm making a word finder generator), but am wondering > if > there's any better way to do this: > > # setup so code snippet works properly > sizeW = 4 > sizeH = 3 > puzzleLayout = ['spam'] * (sizeW * sizeH) > > # Starting with, say, an array with these indices: > # 0 1 2 3 > # 4 5 6 7 > # 8 9 A B > > # I want the following items for a back diagonal (not in square brackets): > # [-2],[3],8 (+5) | div 4 = -1, 1, 2 > # [-1],4,9 (+5) | div 4 = -1, 1, 2 > # 0,5,A (+5) | div 4 = 0, 1, 2 > # 1,6,B (+5) | div 4 = 0, 1, 2 > # 2,7,[C] (+5) | div 4 = 0, 1, 3 > # 3,[8],[D] (+5) | div 4 = 0, 2, 3 > > # in other words, increase sequence by sizeW + 1 each time (sizeW - 1 > # for forward diagonals), only selecting if the line you're on matches > # the line you want to be on > > # back as in backslash-like diagonal > puzzleDiagBack = [(''.join([puzzleLayout[pos*(sizeW+1) + i] \ > for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos])) \ > for i in range(-sizeH+1,sizeW)] > puzzleDiagBackRev = [(''.join(reversed([puzzleLayout[pos*(sizeW+1) + i] \ > for pos in range(sizeH) if (pos*(sizeW+1) + i) / sizeW == pos]))) \ > for i in range(-sizeH+1,sizeW)] > # fwd as in forwardslash-like diagonal > puzzleDiagFwdRev = [(''.join([puzzleLayout[pos*(sizeW-1) + i] \ > for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos])) \ > for i in range(sizeW+sizeH-1)] > puzzleDiagFwd = [(''.join(reversed([puzzleLayout[pos*(sizeW-1) + i] \ > for pos in range(sizeH) if (pos*(sizeW-1) + i) / sizeW == pos]))) \ > for i in range(sizeW+sizeH-1)] > > Cheers, > David Eccles (gringer) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From c.t.matsumoto at gmail.com Wed Mar 3 18:27:23 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Wed, 03 Mar 2010 18:27:23 +0100 Subject: [Tutor] unittest Message-ID: <4B8E9BFB.2030604@gmail.com> Hello, Can someone tell me the difference between unittests assertEqual and assertEquals? Cheers, T From anothernetfellow at gmail.com Wed Mar 3 18:28:10 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 18:28:10 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> Message-ID: <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> Ops, i have another update: string = u"blabla" This is unicode, ok. Unicode UTF-8? Thankyou 2010/3/3 Giorgio > Ok. > > So, how do you encode .py files? UTF-8? > > 2010/3/3 Dave Angel > > Giorgio wrote: >> >>> >>>> >>>>> Depends on your python version. If you use python 2.x, you have to use >>>>> a >>>>> >>>>> >>>> u before the string: >>>> >>>> s = u'Hallo World' >>>> >>>> >>> >>> >>> Ok. So, let's go back to my first question: >>> >>> s = u'Hallo World' is unicode in python 2.x -> ok >>> s = 'Hallo World' how is encoded? >>> >>> >>> >> Since it's a quote literal in your source code, it's encoded by your text >> editor when it saves the file, and you tell Python which encoding it was by >> the second line of your source file, right after the shebang line. >> >> A sequence of bytes in an html file should be should have its encoding >> identified by the tag at the top of the html file. And I'd *guess* that on >> a form result, the encoding can be assumed to match that of the html of the >> form itself. >> >> DaveA >> >> > > > -- > -- > AnotherNetFellow > Email: anothernetfellow at gmail.com > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From johnf at jfcomputer.com Wed Mar 3 18:20:33 2010 From: johnf at jfcomputer.com (John) Date: Wed, 3 Mar 2010 09:20:33 -0800 Subject: [Tutor] lazy? vs not lazy? and yielding Message-ID: <201003030920.33596.johnf@jfcomputer.com> Hi, I just read a few pages of tutorial on list comprehenion and generator expression. From what I gather the difference is "[ ]" and "( )" at the ends, better memory usage and the something the tutorial labeled as "lazy evaluation". So a generator 'yields'. But what is it yielding too? John From glenbot at gmail.com Wed Mar 3 18:34:09 2010 From: glenbot at gmail.com (Glen Zangirolami) Date: Wed, 3 Mar 2010 11:34:09 -0600 Subject: [Tutor] sorting algorithm In-Reply-To: <4B8E3247.4050500@gmx.net> References: <4B8E3247.4050500@gmx.net> Message-ID: http://www.sorting-algorithms.com/ It is a fantastic website that explains each kind of sort and how it works. They also show you visuals how the sorts work and how fast they go based on the amount of data. Depending on the amount/kind of data I would choose a sorting algorithm that fits your needs. Bubble sorts tends to get worse the larger the data set but can be very useful to sort small lists. On Wed, Mar 3, 2010 at 3:56 AM, C.T. Matsumoto wrote: > Hello, > > This is follow up on a question I had about algorithms. In the thread it > was suggested I make my own sorting algorithm. > > Here are my results. > > #!/usr/bin/python > > def sort_(list_): > for item1 in list_: > pos1 = list_.index(item1) > pos2 = pos1 + 1 > try: > item2 = list_[pos2] > except IndexError: > pass > > if item1 >= item2: > try: > list_.pop(pos2) > list_.insert(pos1, item2) > return True > except IndexError: > pass > > def mysorter(list_): > while sort_(list_) is True: > sort_(list_) > > I found this to be a great exercise. In doing the exercise, I got pretty > stuck. I consulted another programmer (my dad) who described how to go about > sorting. As it turned out the description he described was the Bubble sort > algorithm. Since coding the solution I know the Bubble sort is inefficient > because of repeated iterations over the entire list. This shed light on the > quick sort algorithm which I'd like to have a go at. > > Something I haven't tried is sticking in really large lists. I was told > that with really large list you break down the input list into smaller > lists. Sort each list, then go back and use the same swapping procedure for > each of the different lists. My question is, at what point to you start > breaking things up? Is that based on list elements or is it based on > memory(?) resources python is using? > > One thing I'm not pleased about is the while loop and I'd like to replace > it with a for loop. > > Thanks, > > T > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tmatsumoto at gmx.net Wed Mar 3 18:36:22 2010 From: tmatsumoto at gmx.net (C.T. Matsumoto) Date: Wed, 03 Mar 2010 18:36:22 +0100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B8E7F67.5030602@ieee.org> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> Message-ID: <4B8E9E16.2090400@gmx.net> Dave Angel wrote: > C.T. Matsumoto wrote: >> Hello, >> >> This is follow up on a question I had about algorithms. In the thread >> it was suggested I make my own sorting algorithm. >> >> Here are my results. >> >> #!/usr/bin/python >> >> def sort_(list_): >> for item1 in list_: >> pos1 = list_.index(item1) >> pos2 = pos1 + 1 >> try: >> item2 = list_[pos2] >> except IndexError: >> pass >> >> if item1 >= item2: >> try: >> list_.pop(pos2) >> list_.insert(pos1, item2) >> return True >> except IndexError: >> pass >> >> def mysorter(list_): >> while sort_(list_) is True: >> sort_(list_) >> >> I found this to be a great exercise. In doing the exercise, I got >> pretty stuck. I consulted another programmer (my dad) who described >> how to go about sorting. As it turned out the description he >> described was the Bubble sort algorithm. Since coding the solution I >> know the Bubble sort is inefficient because of repeated iterations >> over the entire list. This shed light on the quick sort algorithm >> which I'd like to have a go at. >> >> Something I haven't tried is sticking in really large lists. I was >> told that with really large list you break down the input list into >> smaller lists. Sort each list, then go back and use the same swapping >> procedure for each of the different lists. My question is, at what >> point to you start breaking things up? Is that based on list elements >> or is it based on memory(?) resources python is using? >> >> One thing I'm not pleased about is the while loop and I'd like to >> replace it with a for loop. >> >> Thanks, >> >> T >> >> > There are lots of references on the web about Quicksort, including a > video at: > http://www.youtube.com/watch?v=y_G9BkAm6B8 > > which I think illustrates it pretty well. It would be a great > learning exercise to implement Python code directly from that > description, without using the sample C++ code available. > > (Incidentally, there are lots of variants of Quicksort, so I'm not > going to quibble about whether this is the "right" one to be called > that.) > > I don't know what your earlier thread was, since you don't mention the > subject line, but there are a number of possible reasons you might not > have wanted to use the built-in sort. The best one is for educational > purposes. I've done my own sort for various reasons in the past, even > though I had a library function, since the library function had some > limits. One time I recall, the situation was that the library sort > was limited to 64k of total data, and I had to work with much larger > arrays (this was in 16bit C++, in "large" model). I solved the size > problem by using the C++ sort library on 16k subsets (because a > pointer was 2*2 bytes). Then I merged the results of the sorts. At > the time, and in the circumstances involved, there were seldom more > than a dozen or so sublists to merge, so this approach worked well > enough. > > Generally, it's better for both your development time and the > efficiency and reliabilty of the end code, to base a new sort > mechanism on the existing one. In my case above, I was replacing what > amounted to an insertion sort, and achieved a 50* improvement for a > real customer. It was fast enough that other factors completely > dominated his running time. > > But for learning purposes? Great plan. So now I'll respond to your > other questions, and comment on your present algorithm. > > It would be useful to understand about algorithmic complexity, the so > called Order Function. In a bubble sort, if you double the size of > the array, you quadruple the number of comparisons and swaps. It's > order N-squared or O(n*n). So what works well for an array of size > 10 might take a very long time for an array of size 10000 (like a > million times as long). You can do much better by sorting smaller > lists, and then combining them together. Such an algorithm can be > O(n*log(n)). > > > You ask at what point you consider sublists? In a language like C, > the answer is when the list is size 3 or more. For anything larger > than 2, you divide into sublists, and work on them. > > Now, if I may comment on your code. You're modifying a list while > you're iterating through it in a for loop. In the most general case, > that's undefined. I think it's safe in this case, but I would avoid > it anyway, by just using xrange(len(list_)-1) to iterate through it. > You use the index function to find something you would already know -- > the index function is slow. And the first try/except isn't needed if > you use a -1 in the xrange argument, as I do above. > > You use pop() and push() to exchange two adjacent items in the list. > Both operations copy the remainder of the list, so they're rather > slow. Since you're exchanging two items in the list, you can simply > do that: > list[pos1], list[pos2] = list[pos2], list[pos1] > > That also eliminates the need for the second try/except. > > You mention being bothered by the while loop. You could replace it > with a simple for loop with xrange(len(list_)), since you know that N > passes will always be enough. But if the list is partially sorted, > your present scheme will end sooner. And if it's fully sorted, it'll > only take one pass over the data. > > There are many refinements you could do. For example, you don't have > to stop the inner loop after the first swap. You could finish the > buffer, swapping any other pairs that are out of order. You'd then be > saving a flag indicating if you did any swaps. You could keep a index > pointing to the last pair you swapped on the previous pass, and use > that for a limit next time. Then you just terminate the outer loop > when that limit value is 1. You could even keep two limit values, and > bubble back and forth between them, as they gradually close into the > median of the list. You quit when they collide in the middle. > > The resultant function should be much faster for medium-sized lists, > but it still will slow down quadratically as the list size increases. > You still need to divide and conquer, and quicksort is just one way of > doing that. > > DaveA > Thanks a lot Dave, Sorry the original thread is called 'Python and algorithms'. Yes, I think it's best to use what python provides and build on top of that. I got to asking my original question based on trying to learn more about algorithms in general, through python. Of late many people have been asking me how well I can 'build' algorithms, and this prompted me to start the thread. This is for learning purposes (which the original thread will give you and indication where I'm coming from). The refactored code looks like this. I have tackled a couple items. First the sub-listing (which I'll wait till I can get the full sort working), then the last couple of paragraphs about refinements. Starting with the first refinement, I'm not sure how *not* to stop the inner loop? def s2(list_): for pos1 in xrange(len(list_)-1): item1 = list_[pos1] pos2 = pos1 + 1 item2 = list_[pos2] if item1 >= item2: list_[pos1], list_[pos2] = list_[pos2], list_[pos1] return True def mysorter(list_): # This is the outer loop? while s2(list_) is True: # Calling s2 kicks off the inner loop? s2(list_) if __name__ == '__main__': from random import shuffle foo = range(10) shuffle(foo) mysorter(foo) Thanks again. From david at pythontoo.com Wed Mar 3 19:23:11 2010 From: david at pythontoo.com (David) Date: Wed, 03 Mar 2010 13:23:11 -0500 Subject: [Tutor] Bowing out In-Reply-To: <64c038661003030703j9207275n8d70470934b159c7@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> <64c038661003030703j9207275n8d70470934b159c7@mail.gmail.com> Message-ID: <4B8EA90F.8050004@pythontoo.com> >> After six years of tutor posts my interest and energy have waned and >> I'm ready to move on to something new. Another new Python student that received understanding from your style and knowledge, great teacher. I hope you will post some more tutorials on Kents Korner when you find the time and motivation, thanks again; David From knacktus at googlemail.com Wed Mar 3 18:43:28 2010 From: knacktus at googlemail.com (Jan Jansen) Date: Wed, 3 Mar 2010 18:43:28 +0100 Subject: [Tutor] How to wrap ctype functions Message-ID: Hi there, I wonder what's the best way to wrap given function calls (in this case ctype function calls but more generally built-in functions and those kinds). I have a huge c library and almost all functions return an error code. The library also contains a function, that returns the corresponding error message to the error code. So, what I need to do for every call to one of the libraries functions looks like this: error_code = my_c_library.SOME_FUNCTION_ CALL(argument) if error_code != 0: error_message = my_c_library.GET_ERROR_TEXT(error_code) print "error in function call SOME_FUNCTION_CALL" print error_message my_c_library.EXIT_AND_CLEAN_UP() Also, for some function calls I would need to do some preperations like: error_code = my_c_library.LOG_IN_AS_ADMINISTRATOR(admin_user, admin_password) error_code = my_c_library.SOME_FUNCTION_CALL(argument) I like the decorator idea, but I can't figure out if it's applicable here. To be able to call the function in a manner like this would be great, e.g. @change_user(admin_user, admin_password) @error_handling my_c_library.SOME_FUNCTION_CALL(argument) Best regards, Jan -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Mar 3 18:56:41 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 12:56:41 -0500 Subject: [Tutor] sorting algorithm In-Reply-To: <4B8E95D6.7080903@gmx.net> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> Message-ID: <4B8EA2D9.6050302@ieee.org> (You forgot to do a Reply-All, so your message went to just me, rather than to me and the list ) C.T. Matsumoto wrote: > Dave Angel wrote: >> C.T. Matsumoto wrote: >>> Hello, >>> >>> This is follow up on a question I had about algorithms. In the >>> thread it was suggested I make my own sorting algorithm. >>> >>> Here are my results. >>> >>> #!/usr/bin/python >>> >>> def sort_(list_): >>> for item1 in list_: >>> pos1 = list_.index(item1) >>> pos2 = pos1 + 1 >>> try: >>> item2 = list_[pos2] >>> except IndexError: >>> pass >>> >>> if item1 >= item2: >>> try: >>> list_.pop(pos2) >>> list_.insert(pos1, item2) >>> return True >>> except IndexError: >>> pass >>> >>> def mysorter(list_): >>> while sort_(list_) is True: >>> sort_(list_) >>> >>> I found this to be a great exercise. In doing the exercise, I got >>> pretty stuck. I consulted another programmer (my dad) who described >>> how to go about sorting. As it turned out the description he >>> described was the Bubble sort algorithm. Since coding the solution I >>> know the Bubble sort is inefficient because of repeated iterations >>> over the entire list. This shed light on the quick sort algorithm >>> which I'd like to have a go at. >>> >>> Something I haven't tried is sticking in really large lists. I was >>> told that with really large list you break down the input list into >>> smaller lists. Sort each list, then go back and use the same >>> swapping procedure for each of the different lists. My question is, >>> at what point to you start breaking things up? Is that based on list >>> elements or is it based on memory(?) resources python is using? >>> >>> One thing I'm not pleased about is the while loop and I'd like to >>> replace it with a for loop. >>> >>> Thanks, >>> >>> T >>> >>> >> There are lots of references on the web about Quicksort, including a >> video at: >> http://www.youtube.com/watch?v=y_G9BkAm6B8 >> >> which I think illustrates it pretty well. It would be a great >> learning exercise to implement Python code directly from that >> description, without using the sample C++ code available. >> >> (Incidentally, there are lots of variants of Quicksort, so I'm not >> going to quibble about whether this is the "right" one to be called >> that.) >> >> I don't know what your earlier thread was, since you don't mention >> the subject line, but there are a number of possible reasons you >> might not have wanted to use the built-in sort. The best one is for >> educational purposes. I've done my own sort for various reasons in >> the past, even though I had a library function, since the library >> function had some limits. One time I recall, the situation was that >> the library sort was limited to 64k of total data, and I had to work >> with much larger arrays (this was in 16bit C++, in "large" model). I >> solved the size problem by using the C++ sort library on 16k subsets >> (because a pointer was 2*2 bytes). Then I merged the results of the >> sorts. At the time, and in the circumstances involved, there were >> seldom more than a dozen or so sublists to merge, so this approach >> worked well enough. >> >> Generally, it's better for both your development time and the >> efficiency and reliabilty of the end code, to base a new sort >> mechanism on the existing one. In my case above, I was replacing >> what amounted to an insertion sort, and achieved a 50* improvement >> for a real customer. It was fast enough that other factors >> completely dominated his running time. >> >> But for learning purposes? Great plan. So now I'll respond to your >> other questions, and comment on your present algorithm. >> >> It would be useful to understand about algorithmic complexity, the so >> called Order Function. In a bubble sort, if you double the size of >> the array, you quadruple the number of comparisons and swaps. It's >> order N-squared or O(n*n). So what works well for an array of size >> 10 might take a very long time for an array of size 10000 (like a >> million times as long). You can do much better by sorting smaller >> lists, and then combining them together. Such an algorithm can be >> O(n*log(n)). >> >> >> You ask at what point you consider sublists? In a language like C, >> the answer is when the list is size 3 or more. For anything larger >> than 2, you divide into sublists, and work on them. >> >> Now, if I may comment on your code. You're modifying a list while >> you're iterating through it in a for loop. In the most general case, >> that's undefined. I think it's safe in this case, but I would avoid >> it anyway, by just using xrange(len(list_)-1) to iterate through it. >> You use the index function to find something you would already know >> -- the index function is slow. And the first try/except isn't needed >> if you use a -1 in the xrange argument, as I do above. >> >> You use pop() and push() to exchange two adjacent items in the list. >> Both operations copy the remainder of the list, so they're rather >> slow. Since you're exchanging two items in the list, you can simply >> do that: >> list[pos1], list[pos2] = list[pos2], list[pos1] >> >> That also eliminates the need for the second try/except. >> >> You mention being bothered by the while loop. You could replace it >> with a simple for loop with xrange(len(list_)), since you know that N >> passes will always be enough. But if the list is partially sorted, >> your present scheme will end sooner. And if it's fully sorted, it'll >> only take one pass over the data. >> >> There are many refinements you could do. For example, you don't have >> to stop the inner loop after the first swap. You could finish the >> buffer, swapping any other pairs that are out of order. You'd then >> be saving a flag indicating if you did any swaps. You could keep a >> index pointing to the last pair you swapped on the previous pass, and >> use that for a limit next time. Then you just terminate the outer >> loop when that limit value is 1. You could even keep two limit >> values, and bubble back and forth between them, as they gradually >> close into the median of the list. You quit when they collide in the >> middle. >> >> The resultant function should be much faster for medium-sized lists, >> but it still will slow down quadratically as the list size >> increases. You still need to divide and conquer, and quicksort is >> just one way of doing that. >> >> DaveA >> > Thanks a lot Dave, > > Sorry the original thread is called 'Python and algorithms'. > > Yes, I think it's best to use what python provides and build on top of > that. I got to asking my original question based on trying to learn > more about algorithms in general, through python. Of late many people > have been asking me how well I can 'build' algorithms, and this > prompted me to start the thread. This is for learning purposes (which > the original thread will give you and indication where I'm coming from). > > The refactored code looks like this. I have tackled a couple items. > First the sub-listing (which I'll wait till I can get the full sort > working), then the last couple of paragraphs about refinements. > Starting with the first refinement, I'm not sure how *not* to stop the > inner loop? > > def s2(list_): > for pos1 in xrange(len(list_)-1): > item1 = list_[pos1] > pos2 = pos1 + 1 > item2 = list_[pos2] > if item1 >= item2: > list_[pos1], list_[pos2] = list_[pos2], list_[pos1] > return True > > def mysorter(list_): > # This is the outer loop? > while s2(list_) is True: > # Calling s2 kicks off the inner loop? > s2(list_) > > if __name__ == '__main__': > from random import shuffle > foo = range(10) > shuffle(foo) > mysorter(foo) > > > Thanks again. > As before, I'm not actually trying this code, so there may be typos. But assuming your code here works, the next refinement would be: In s2() function, add a flag variable, initially False. Then instead of the return True, just say flag=True Then at the end of the function, return flag About the while loop. No need to say 'is True' just use while s2(list_): And no need to call s2() a second time. while s2(list_): pass Before you can refine the upper limit, you need a way to preserve it between calls. Simplest way to do that is to combine the two functions, as a nested loop. Then, instead of flag, you can have a value "limit" which indicates what index was last swapped. And the inner loop uses that as an upper limit on its xrange. DaveA From alan.gauld at btinternet.com Wed Mar 3 19:18:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Mar 2010 18:18:40 -0000 Subject: [Tutor] List comprehension possible with condition statements? References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> <201003032043.44183.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote > Comparisons with None almost always should be one of: > > item is None > item is not None Yes, but the reason I changed it (I originally had "is not") is that != is a more general test for illustrating the use of 'if' within a LC which seemed to be the real issue within the question. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Mar 3 19:21:58 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Mar 2010 18:21:58 -0000 Subject: [Tutor] Bowing out References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: "kevin parks" wrote > Wish Danny Yoo was still here too. Technically he is, but just keeps very, very quiet! :-) Alan G. From stefan_ml at behnel.de Wed Mar 3 20:24:13 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 03 Mar 2010 20:24:13 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> Message-ID: Giorgio, 03.03.2010 18:28: > string = u"blabla" > > This is unicode, ok. Unicode UTF-8? No, not UTF-8. Unicode. You may want to read this: http://www.amk.ca/python/howto/unicode Stefan From beachkid at insightbb.com Wed Mar 3 20:42:42 2010 From: beachkid at insightbb.com (Ken G.) Date: Wed, 03 Mar 2010 14:42:42 -0500 Subject: [Tutor] Bowing out Message-ID: <4B8EBBB2.3080603@insightbb.com> Thanks for helping out. I enjoyed readying your posts. Good luck on your future endeavors. Ken Kent Johnson wrote: Hi all, After six years of tutor posts my interest and energy have waned and I'm ready to move on to something new. I'm planning to stop reading and contributing to the list. I have handed over list moderation duties to Alan Gauld and Wesley Chun. Thanks to everyone who contributes questions and answers. I learned a lot from my participation here. So long and keep coding! Kent From anothernetfellow at gmail.com Wed Mar 3 20:44:51 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 20:44:51 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> Message-ID: <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> Please let me post the third update O_o. You can forgot other 2, i'll put them into this email. --- >>> s = "ciao ? ciao" >>> print s ciao ? ciao >>> s.encode('utf-8') Traceback (most recent call last): File "", line 1, in s.encode('utf-8') UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: ordinal not in range(128) --- I am getting more and more confused. I was coding in PHP and was saving some strings in the DB. Was using utf8_encode to encode them before sending to the utf8_unicode_ci table. Ok, the result was that strings were "double encoded". To fix that I simply removed the utf8_encode() function and put the "raw" data in the database (that converts them in utf8). In other words, in PHP, I can encode a string multiple times: $c = "giorgio ? giorgio"; $c = utf8_encode($c); // this will work in an utf8 html page $d = utf8_encode($c); // this won't work, will print a strange letter $d = utf8_decode($d); // this will work. will print an utf8 string Ok, now, the point is: you (and the manual) said that this line: s = u"giorgio ? giorgio" will convert the string as unicode. But also said that the part between "" will be encoded with my editor BEFORE getting encoded in unicode by python. So please pay attention to this example: My editor is working in UTF8. I create this: c = "giorgio ? giorgio" // This will be an UTF8 string because of the file's encoding d = unicode(c) // This will be an unicode string e = c.encode() // How will be encoded this string? If PY is working like PHP this will be an utf8 string. Can you help me? Thankyou VERY much Giorgio -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Wed Mar 3 21:39:00 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 3 Mar 2010 21:39:00 +0100 Subject: [Tutor] Encoding In-Reply-To: References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> Message-ID: <23ce85921003031239n6edb7175heeaf0ac25e8332bc@mail.gmail.com> I'm sorry, it's utf8_unicode_ci that's confusing me. So, "UTF-8 is one of the most commonly used encodings. UTF stands for "Unicode Transformation Format"" UTF8 is, we can say, a type of "unicode", right? And what about utf8_unicode_ci in mysql? Giorgio 2010/3/3 Stefan Behnel > Giorgio, 03.03.2010 18:28: > > string = u"blabla" >> >> This is unicode, ok. Unicode UTF-8? >> > > No, not UTF-8. Unicode. > > You may want to read this: > > http://www.amk.ca/python/howto/unicode > > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Mar 3 21:41:34 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 15:41:34 -0500 Subject: [Tutor] lazy? vs not lazy? and yielding In-Reply-To: <201003030920.33596.johnf@jfcomputer.com> References: <201003030920.33596.johnf@jfcomputer.com> Message-ID: <4B8EC97E.60102@ieee.org> John wrote: > Hi, > > I just read a few pages of tutorial on list comprehenion and generator > expression. From what I gather the difference is "[ ]" and "( )" at the > ends, better memory usage and the something the tutorial labeled as "lazy > evaluation". So a generator 'yields'. But what is it yielding too? > > John > > A list comprehension builds a whole list at one time. So if the list needed is large enough in size, it'll never finish, and besides, you'll run out of memory and crash. A generator expression builds a function instead which *acts* like a list, but actually doesn't build the values till you ask for them. But you can still do things like for item in fakelist: and it does what you'd expect. You can write a generator yourself, and better understand what it's about. Suppose you were trying to build a "list" of the squares of the integers between 3 and 15. For a list of that size, you could just use a list comprehension. But pretend it was much larger, and you couldn't spare the memory or the time. So let's write a generator function by hand, deliberately the hard way. def mygen(): i = 3 while i < 16: yield i*i i += 1 return This function is a generator, by virtue of that yield statement in it. When it's called, it does some extra magic to make it easy to construct a loop. If you now use for item in mygen(): print item Each time through the loop, it executes one more iteration of the mygen() function, up to the yield statement. And the value that's put into item comes from the yield statement. When the mygen() function returns (or falls off the end), it actually generates a special exception that quietly terminates the for/loop. Now, when we're doing simple expressions for a small number of values, we should use a list comprehension. When it gets big enough, switch to a generator expression. And if it gets complicated enough, switch to a generator function. The point here is that the user of the for/loop doesn't care which way it was done. Sometimes you really need a list. For example, you can't generally back up in a generator, or randomly access the [i] item. But a generator is a very valuable mechanism to understand. For a complex example, consider searching a hard disk for a particular file. Building a complete list might take a long time, and use a lot of memory. But if you use a generator inside a for loop, you can terminate (break) when you meet some condition, and the remainder of the files never had to be visited. See os.walk() DaveA From davea at ieee.org Wed Mar 3 22:23:25 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 03 Mar 2010 16:23:25 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> Message-ID: <4B8ED34D.2020708@ieee.org> (Don't top-post. Put your response below whatever you're responding to, or at the bottom.) Giorgio wrote: > Ok. > > So, how do you encode .py files? UTF-8? > > 2010/3/3 Dave Angel > > I personally use Komodo to edit my python source files, and tell it to use UTF8 encoding. Then I add a encoding line as the second line of the file. Many times I get lazy, because mostly my source doesn't contain non-ASCII characters. But if I'm copying characters from an email or other Unicode source, then I make sure both are set up. The editor will actually warn me if I try to save a file as ASCII with any 8 bit characters in it. Note: unicode is 16 bit characters, at least in CPython implementation. UTF-8 is an 8 bit encoding of that Unicode, where there's a direct algorithm to turn 16 or even 32 bit Unicode into 8 bit characters. They are not the same, although some people use the terms interchangeably. Also note: An 8 bit string has no inherent meaning, until you decide how to decode it into Unicode. Doing explicit decodes is much safer, rather than assuming some system defaults. And if it happens to contain only 7 bit characters, it doesn't matter what encoding you specify when you decode it. Which is why all of us have been so casual about this. From sander.sweers at gmail.com Wed Mar 3 22:41:21 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 3 Mar 2010 22:41:21 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> Message-ID: On 3 March 2010 20:44, Giorgio wrote: >>>> s = "ciao ? ciao" >>>> print s > ciao ? ciao >>>> s.encode('utf-8') > Traceback (most recent call last): > ??File "", line 1, in > ?? ?s.encode('utf-8') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: > ordinal not in range(128) It is confusing but once understand how it works it makes sense. You start with a 8bit string so you will want to *decode* it to unicode string. >>> s = "ciao ? ciao" >>> us = s.decode('latin-1') >>> us u'ciao \xe8 ciao' >>> us2 = s.decode('iso-8859-1') >>> us2 u'ciao \xe8 ciao' Greets Sander From hugo.yoshi at gmail.com Wed Mar 3 22:44:35 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 3 Mar 2010 22:44:35 +0100 Subject: [Tutor] How to wrap ctype functions In-Reply-To: References: Message-ID: <29179d161003031344v428b88d6v867a5e0aa00e3478@mail.gmail.com> On Wed, Mar 3, 2010 at 6:43 PM, Jan Jansen wrote: > Hi there, > > I wonder what's the best way to wrap given function calls (in this case > ctype function calls but more generally built-in functions and those kinds). > I have a huge c library and almost all functions return an error code. The > library also contains a function, that returns the corresponding error > message to the error code. So, what I need to do for every call to one of > the libraries functions looks like this: > > error_code = my_c_library.SOME_FUNCTION_ > CALL(argument) > if error_code != 0: > ?? error_message = my_c_library.GET_ERROR_TEXT(error_code) > ?? print "error in function call SOME_FUNCTION_CALL" > ?? print error_message > ?? my_c_library.EXIT_AND_CLEAN_UP() > > Also, for some function calls I would need to do some preperations like: > > error_code = my_c_library.LOG_IN_AS_ADMINISTRATOR(admin_user, > admin_password) > error_code = my_c_library.SOME_FUNCTION_CALL(argument) > > I like the decorator idea, but I can't figure out if it's applicable here. > To be able to call the function in a manner like this would be great, e.g. > > @change_user(admin_user, admin_password) > @error_handling > my_c_library.SOME_FUNCTION_CALL(argument) > Well, decorators only work on function definitions, not function calls. What you could do is a) write a function that takes your c function as an argument and does the error handling for you would probably look something like this: def call_with_errorhandling(func, *args): error_code = func(*args) if error_code != 0: error_message = my_c_library.GET_ERROR_TEXT(error_code) print "error in function call SOME_FUNCTION_CALL" ?? print error_message ?? my_c_library.EXIT_AND_CLEAN_UP() b) wrap all your c functions in a python function that does error handling. the wrapper/decorator would look somewhat like this: from functools import wraps def with_error_handling(func): @wraps(func) def new_func(*args): error_code = func(*args) if error_code != 0: error_message = my_c_library.GET_ERROR_TEXT(error_code) print "error in function call SOME_FUNCTION_CALL" ?? print error_message ?? my_c_library.EXIT_AND_CLEAN_UP() return new_func and you'd wrap functions with it like so: # old syntax wrapped_function = with_error_handling(library.some_C_function) #decorator syntax @with_error_handling def wrapped_function(*args) return library.some_C_function(*args) #just call your wrapped function now, the error handling will take place. wrapped_function(argument) You can write similar functions that do admin login in both ways. Both approaches wrap C functions in python functions, so the overhead should be about the same (the decorator way should consume somewhat more memory, since it creates a new function object for everything you wrap, but it's probably not very significant). HTH, Hugo From kent37 at tds.net Wed Mar 3 22:49:39 2010 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Mar 2010 16:49:39 -0500 Subject: [Tutor] Bowing out In-Reply-To: <8af18dcf1003030851p41f82a76jdd79f8109dc7bad8@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> <8af18dcf1003030851p41f82a76jdd79f8109dc7bad8@mail.gmail.com> Message-ID: <1c2a2c591003031349r85cd369vb80076b430dd4b02@mail.gmail.com> On Wed, Mar 3, 2010 at 11:51 AM, Robert wrote: > so you're "done" with Python ? :) No, I hope not! I still love Python, but my enthusiasm for beginners questions has pretty much gone away. I'm looking for a place where I can contribute code rather than answers, possibly with the Mercurial project or SunlightLabs.org. Kent > > >> On 3 March 2010 14:17, Kent Johnson wrote: >>> After six years of tutor posts my interest and energy have waned and >>> I'm ready to move on to something new. > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Wed Mar 3 22:54:43 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Mar 2010 08:54:43 +1100 Subject: [Tutor] unittest In-Reply-To: <4B8E9BFB.2030604@gmail.com> References: <4B8E9BFB.2030604@gmail.com> Message-ID: <201003040854.48898.steve@pearwood.info> On Thu, 4 Mar 2010 04:27:23 am C.T. Matsumoto wrote: > Hello, > > Can someone tell me the difference between unittests assertEqual and > assertEquals? assertEqual, assertEquals and failUnless are three spellings for the same thing. There is no difference. -- Steven D'Aprano From sander.sweers at gmail.com Wed Mar 3 22:59:19 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 3 Mar 2010 22:59:19 +0100 Subject: [Tutor] Encoding In-Reply-To: References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> Message-ID: On 3 March 2010 22:41, Sander Sweers wrote: > It is confusing but once understand how it works it makes sense. I remembered Kent explained it very clear in [1]. Greets Sander [1] http://mail.python.org/pipermail/tutor/2009-May/068920.html From steve at pearwood.info Wed Mar 3 23:06:04 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Mar 2010 09:06:04 +1100 Subject: [Tutor] sorting algorithm In-Reply-To: References: <4B8E3247.4050500@gmx.net> Message-ID: <201003040906.04951.steve@pearwood.info> On Thu, 4 Mar 2010 04:34:09 am Glen Zangirolami wrote: > http://www.sorting-algorithms.com/ > > It is a fantastic website that explains each kind of sort and how it > works. They also show you visuals how the sorts work and how fast > they go based on the amount of data. For actual practical work, you aren't going to beat the performance of Python's built-in sort. Unless you are an expert, don't even think about trying. If you are an expert, you've surely got better things to do. > Depending on the amount/kind of data I would choose a sorting > algorithm that fits your needs. > > Bubble sorts tends to get worse the larger the data set but can be > very useful to sort small lists. Bubble sorts are useless for any real work. They are a shockingly bad way of sorting anything beyond perhaps 4 or 5 items, and even for lists that small there are other algorithms which are barely more complicated but significantly faster. Bubble sorts not only get slower as the list gets bigger, but they do so at an every increasing rate: let's say it takes 1 second to sort 100 items (for the sake of the argument). Then it will take: 4 seconds to sort 200 items 9 seconds to sort 300 items 16 seconds to sort 400 items 25 seconds to sort 500 items 36 seconds to sort 600 items ... and so forth. In other words, multiplying the number of items by a factor of X will multiply the time taken by X squared. The only advantage of bubble sort is that the algorithm is easy to code. Otherwise it is horrible in just about every imaginable way. -- Steven D'Aprano From steve at pearwood.info Wed Mar 3 23:17:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Mar 2010 09:17:51 +1100 Subject: [Tutor] How to wrap ctype functions In-Reply-To: References: Message-ID: <201003040917.52993.steve@pearwood.info> On Thu, 4 Mar 2010 04:43:28 am Jan Jansen wrote: > Hi there, > > I wonder what's the best way to wrap given function calls (in this > case ctype function calls but more generally built-in functions and > those kinds). I have a huge c library and almost all functions return > an error code. The library also contains a function, that returns the > corresponding error message to the error code. So, what I need to do > for every call to one of the libraries functions looks like this: > > error_code = my_c_library.SOME_FUNCTION_ > CALL(argument) > if error_code != 0: > error_message = my_c_library.GET_ERROR_TEXT(error_code) > print "error in function call SOME_FUNCTION_CALL" > print error_message > my_c_library.EXIT_AND_CLEAN_UP() Something like this: class MyCLibraryError(ValueError): # Create our own exception. pass import functools def decorate_error_code(func): @functools.wraps(func) def inner(*args, **kwargs): error_code = func(*args, **kwargs) if error_code != 0: msg = my_c_library.GET_ERROR_TEXT(error_code) my_c_library.EXIT_AND_CLEAN_UP() raise MyCLibraryError(msg) return inner # note *no* brackets Then wrap the functions: some_function_call = decorate_error_code( my_c_library.SOME_FUNCTION_CALL) another_function_call = decorate_error_code( my_c_library.ANOTHER_FUNCTION_CALL) > Also, for some function calls I would need to do some preperations > like: > > error_code = my_c_library.LOG_IN_AS_ADMINISTRATOR(admin_user, > admin_password) > error_code = my_c_library.SOME_FUNCTION_CALL(argument) > > I like the decorator idea, but I can't figure out if it's applicable > here. To be able to call the function in a manner like this would be > great, e.g. > > @change_user(admin_user, admin_password) > @error_handling > my_c_library.SOME_FUNCTION_CALL(argument) That's not how the decorator syntax works. You can only use the @decorator syntax immediately above a function definition: @error_handling def some_function(): ... Otherwise, you use the decorator like a function, you give the name of another function as argument, and it returns a new function: new_function = error_handling(some_function) -- Steven D'Aprano From steve at pearwood.info Wed Mar 3 23:22:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Mar 2010 09:22:14 +1100 Subject: [Tutor] List comprehension possible with condition statements? In-Reply-To: References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com> <201003032043.44183.steve@pearwood.info> Message-ID: <201003040922.15047.steve@pearwood.info> On Thu, 4 Mar 2010 05:18:40 am Alan Gauld wrote: > "Steven D'Aprano" wrote > > > Comparisons with None almost always should be one of: > > > > item is None > > item is not None > > Yes, but the reason I changed it (I originally had "is not") is that > != is a more general test for illustrating the use of 'if' within a > LC which seemed to be the real issue within the question. List comps can include *any* comparison: [x+1 for x in data if (3*x+2)**2 > 100*x or x < -5] -- Steven D'Aprano From karim.liateni at free.fr Wed Mar 3 23:22:32 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Wed, 03 Mar 2010 23:22:32 +0100 Subject: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files. In-Reply-To: <201003021947.53047.steve@pearwood.info> References: <4B8A5921.5000008@free.fr> <467254.17276.qm@web86706.mail.ird.yahoo.com> <201003020125.44822.andreas@kostyrka.org> <201003021947.53047.steve@pearwood.info> Message-ID: <4B8EE128.5010005@free.fr> Hello Alan, Steven, I was narrow minded about this topic and did not see the benefits of these multiple Python implementations. You opened my eyes. Regards Karim Steven D'Aprano wrote: > On Tue, 2 Mar 2010 11:25:44 am Andreas Kostyrka wrote: > >> Furthermore I do not think that most of the "core" community has a >> problem with the alternate implementations, as they provide very >> useful functions (it helps on the architecture side, because it >> limits somewhat what can be done, it helps on the personal side, >> because it increases the value of Python skills, ...), ... >> > > The Python development team values alternative implementations, as it > gives Python the language a much wider user base. > > It also allows other people to shoulder some of the development burden. > For example, people who want Python without the limitations of the C > call stack can use Stackless Python, instead of ordinary CPython. > Google is sponsoring a highly optimized version of Python with a JIT > compiler: Unladen Swallow. It looks likely that Unladen Swallow will > end up being merged with CPython too, which will be a great benefit. > > > From karim.liateni at free.fr Thu Mar 4 01:23:06 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Thu, 04 Mar 2010 01:23:06 +0100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <201003030229.21233.steve@pearwood.info> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> <201003030229.21233.steve@pearwood.info> Message-ID: <4B8EFD6A.8080302@free.fr> Hello Steven, Is there a big difference to write your first functions as below because I am not familiar with yield keyword? def skip_blanks(lines): """Remove leading and trailing whitespace, ignore blank lines.""" return [line.strip() in lines if line.strip()] I tried to write as well the second function but it is not as straight forward. I begin to understand the use of yield in it. Regards Karim Steven D'Aprano wrote: > On Tue, 2 Mar 2010 05:22:43 pm Andrew Fithian wrote: > >> Hi tutor, >> >> I have a large text file that has chunks of data like this: >> >> headerA n1 >> line 1 >> line 2 >> ... >> line n1 >> headerB n2 >> line 1 >> line 2 >> ... >> line n2 >> >> Where each chunk is a header and the lines that follow it (up to the >> next header). A header has the number of lines in the chunk as its >> second field. >> > > And what happens if the header is wrong? How do you handle situations > like missing headers and empty sections, header lines which are wrong, > and duplicate headers? > > line 1 > line 2 > headerB 0 > headerC 1 > line 1 > headerD 2 > line 1 > line 2 > line 3 > line 4 > headerE 23 > line 1 > line 2 > headerB 1 > line 1 > > > > This is a policy decision: do you try to recover, raise an exception, > raise a warning, pad missing lines as blank, throw away excess lines, > or what? > > > >> I would like to turn this file into a dictionary like: >> dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, >> line 2, ... , line n2]} >> >> Is there a way to do this with a dictionary comprehension or do I >> have to iterate over the file with a "while 1" loop? >> > > I wouldn't do either. I would treat this as a pipe-line problem: you > have a series of lines that need to be processed. You can feed them > through a pipe-line of filters: > > def skip_blanks(lines): > """Remove leading and trailing whitespace, ignore blank lines.""" > for line in lines: > line = line.strip() > if line: > yield line > > def collate_section(lines): > """Return a list of lines that belong in a section.""" > current_header = "" > accumulator = [] > for line in lines: > if line.startswith("header"): > yield (current_header, accumulator) > current_header = line > accumulator = [] > else: > accumulator.append(line) > yield (current_header, accumulator) > > > Then put them together like this: > > > fp = open("my_file.dat", "r") > data = {} # don't shadow the built-in dict > non_blank_lines = skip_blanks(fp) > sections = collate_sections(non_blank_lines) > for (header, lines) in sections: > data[header] = lines > > > Of course you can add your own error checking. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Mar 4 02:19:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 4 Mar 2010 01:19:13 -0000 Subject: [Tutor] List comprehension possible with condition statements? References: <3124be321003030031l2a71440fs18bc4ac0d933dcf5@mail.gmail.com><201003032043.44183.steve@pearwood.info> <201003040922.15047.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote > List comps can include *any* comparison: > > [x+1 for x in data if (3*x+2)**2 > 100*x or x < -5] Sure, but the wording suggested (maybe wrongly) that the OP was a real beginner and so the concept of an expression was likely to be foreign. Sticking with equalty or inequality seemed likely to be the most familiar test to him/her. Trying to explain the differnce between is and == seemed like it would be a diversion from the primary objective, namely how to filter a list comp. However I guess this discussion has covered that topic too now, so once again we have managed to kill several sparrows with one round of buckshot... :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sierra_mtnview at sbcglobal.net Thu Mar 4 02:24:35 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 03 Mar 2010 17:24:35 -0800 Subject: [Tutor] Understanding (Complex) Modules Message-ID: <4B8F0BD3.8050000@sbcglobal.net> First a little preamble before my questions. Most of my work in Python has required modifying a program that uses modules that were imported by the original program. I've made some use of modules on a command line like math, and have used the idea of a qualifier. On occasion, I've used examples from matplotlib that required from matplotlib.image import AxesImage. Further, I've created some simple classes, and produced objects with them. No use of inheritance though. So far so good. In a few places, it is said modules are objects. I'm slightly puzzled by that, but in some way it seems reasonable from the standpoint of period notation. So far I have not created a module. In Lutz's and Ascher's Learning Python, ed. 2, chap. 16, they describe the following example, among others: module2.py as print "starting to load ..." import sys def func(): pass class klass: pass print "done loading." Their description of its use is quite readable. It shows that there is some more to a module than a list of defs, for example. Here comes the questions. Recently I began to use matplotlib, scipy, and pylab, mostly matplotlib. I've ground out a few useful pieces of code, but I'm fairly baffled by the imports required to get at various elements, of say, matplotlib (MPL). Some of the MPL examples use of imports make sense, but how the writer pulled in the necessary elements is not. How does one go about understanding the capabilities of such modules? MPL seems to have a lot of lower level components. Some of them are laid out over numerous pages as in the form of a, say, English language, description. How does one decipher this stuff. For example, open the module in an editor and start looking at the organization? I thinkthe so called MPL guide ins 900 pages long. Even the numpy guide (reference?), I believe borders on 1000 pages. There must be some way to untangle these complex modules, I would think. Some of the tutorials seem nothing more than a template to follow for a particular problem. So far, looking at the plentiful number of examples of MPL, and probably some of the other modules mentioned above have not provided a lot of insight. Is there some relationship between modules and objects that I'm not seeing that could be of value? -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From dwightdhutto at yahoo.com Thu Mar 4 03:03:09 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Wed, 3 Mar 2010 18:03:09 -0800 (PST) Subject: [Tutor] Understanding (Complex) Modules In-Reply-To: <4B8F0BD3.8050000@sbcglobal.net> Message-ID: <495054.51335.qm@web45303.mail.sp1.yahoo.com> --- On Wed, 3/3/10, Wayne Watson wrote: From: Wayne Watson Subject: [Tutor] Understanding (Complex) Modules To: tutor at python.org Date: Wednesday, March 3, 2010, 8:24 PM First a little preamble before my questions. Most of my work in Python has required modifying a program that uses modules that were imported by the original program. I've made some use of modules on a command line like math, and have used the idea of a qualifier.? On occasion, I've used examples from matplotlib that required from matplotlib.image import AxesImage. Further, I've created some simple classes, and produced? objects with them. No use of inheritance though.? So far so good.? In a few places, it is said modules are objects. I'm slightly puzzled by that, but in some way it seems reasonable from the standpoint of period notation. So far I have not created a module. In Lutz's and Ascher's Learning Python, ed. 2, chap. 16, they describe the following example, among others: module2.py as print "starting to load ..." import sys def func(): pass class klass: pass print "done loading." Their description of its use is quite readable.? It shows that there is some more to a module than a list of defs, for example. Here comes the questions. Recently I began to use matplotlib, scipy, and pylab, mostly matplotlib. I've ground out a few useful pieces of code, but I'm fairly baffled by the imports required to get at various elements, of say, matplotlib (MPL).? Some of the MPL examples use of imports make sense, but how the writer pulled in the necessary elements is not.? How does one go about understanding the capabilities of such modules? MPL seems to have a lot of lower level components. Some of them are laid out over numerous pages as in the form of a, say, English language, description. How does one decipher this stuff.? For example, open the module in an editor and start looking at the organization? I thinkthe so called MPL guide ins 900 pages long. Even the numpy guide (reference?), I believe borders on 1000 pages. There must be some way to untangle these complex modules, I would think. Some of the tutorials seem nothing more than a template to follow for a particular problem.? So far, looking at the plentiful number of examples of MPL, and probably some of the other modules mentioned above have not provided a lot of insight. Is there some relationship between modules and objects that I'm not seeing that could be of value? --? ? ? ? ? ? Wayne Watson (Watson Adventures, Prop., Nevada City, CA) ? ? ? ? ? ???(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) ? ? ? ? ? ? ? Obz Site:? 39? 15' 7" N, 121? 2' 32" W, 2700 feet ? ? ? ? ? ? ???Stop the illegal killing of dolphins and porpoises. ? ? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? Wrest the control of the world's fisheries from Japan. ? ? ? ? ? ? ? ? ? ? Web Page: _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor How about importing your foot into your ass, for trying to ask an educated question and answering it through a false addresss by yourself. -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Thu Mar 4 07:02:04 2010 From: denis.spir at gmail.com (spir) Date: Thu, 4 Mar 2010 07:02:04 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003030732k6830eabx492208e15a7d15f9@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E7E9B.9000703@gmail.com> <23ce85921003030732k6830eabx492208e15a7d15f9@mail.gmail.com> Message-ID: <20100304070204.779df1ab@o> On Wed, 3 Mar 2010 16:32:01 +0100 Giorgio wrote: > Uff, encoding is a very painful thing in programming. For sure, but it's true for any kind of data, not only text :-) Think at music or images *formats*. The issue is a bit obscured for text but the use of the mysterious, _cryptic_ (!), word "encoding". When editing an image using a software tool, there is a live representation of the image in memory (say, a plain pixel 2D array), which is probably what the developper found most practicle for image processing. [text processing in python: unicode string type] When the job is finished, you can choose between various formats (png, gif, jpeg..) to save and or transfer it. [text: utf-8/16/32, iso-8859-*, ascii...]. Conversely, if you to edit an existing image, the software needs to convert back from the file format into its internal representation; the format need to be indicated in file, or by the user, or guessed. The only difference with text is that there is no builtin image or sound representation _type_ in python -- only because text and sound are domain specific data while text is needed everywhere. Denis -- ________________________________ la vita e estrany spir.wikidot.com From c.t.matsumoto at gmail.com Thu Mar 4 07:32:22 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Thu, 04 Mar 2010 07:32:22 +0100 Subject: [Tutor] unittest In-Reply-To: <201003040854.48898.steve@pearwood.info> References: <4B8E9BFB.2030604@gmail.com> <201003040854.48898.steve@pearwood.info> Message-ID: <4B8F53F6.1040204@gmail.com> Steven D'Aprano wrote: > On Thu, 4 Mar 2010 04:27:23 am C.T. Matsumoto wrote: > >> Hello, >> >> Can someone tell me the difference between unittests assertEqual and >> assertEquals? >> > > assertEqual, assertEquals and failUnless are three spellings for the > same thing. There is no difference. > > > > Thanks, Okay, does anyone know why unittests have 3 ways to do the same thing? Thanks. T From cwitts at compuscan.co.za Thu Mar 4 07:40:47 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Thu, 04 Mar 2010 08:40:47 +0200 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <4B8F55EF.4070401@compuscan.co.za> Kent Johnson wrote: > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Best of luck for all your future endeavours Kent. /salute -- Kind Regards, Christian Witts From python at bdurham.com Thu Mar 4 07:42:25 2010 From: python at bdurham.com (python at bdurham.com) Date: Thu, 04 Mar 2010 01:42:25 -0500 Subject: [Tutor] Encoding In-Reply-To: <20100304070204.779df1ab@o> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com><23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com><4B8E64E6.5080302@gmail.com><23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com><4B8E7E9B.9000703@gmail.com><23ce85921003030732k6830eabx492208e15a7d15f9@mail.gmail.com> <20100304070204.779df1ab@o> Message-ID: <1267684945.28535.1363040197@webmail.messagingengine.com> Denis, That was a great explanation!! I'm not the OP, but your explanation really clicked with me. Regards, Malcolm For sure, but it's true for any kind of data, not only text :-) Think at music or images *formats*. The issue is a bit obscured for text but the use of the mysterious, _cryptic_ (!), word "encoding". When editing an image using a software tool, there is a live representation of the image in memory (say, a plain pixel 2D array), which is probably what the developper found most practicle for image processing. [text processing in python: unicode string type] When the job is finished, you can choose between various formats (png, gif, jpeg..) to save and or transfer it. [text: utf-8/16/32, iso-8859-*, ascii...]. Conversely, if you to edit an existing image, the software needs to convert back from the file format into its internal representation; the format need to be indicated in file, or by the user, or guessed. The only difference with text is that there is no builtin image or sound representation _type_ in python -- only because text and sound are domain specific data while text is needed everywhere. From python at bdurham.com Thu Mar 4 07:45:06 2010 From: python at bdurham.com (python at bdurham.com) Date: Thu, 04 Mar 2010 01:45:06 -0500 Subject: [Tutor] Bowing out In-Reply-To: <4B8F55EF.4070401@compuscan.co.za> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> <4B8F55EF.4070401@compuscan.co.za> Message-ID: <1267685106.28946.1363040691@webmail.messagingengine.com> Hi Kent, Your posts and web pages really helped me during my early days with python. Wishing you great success in your new endeavors!!! Cheers, Malcolm From denis.spir at gmail.com Thu Mar 4 08:01:30 2010 From: denis.spir at gmail.com (spir) Date: Thu, 4 Mar 2010 08:01:30 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> Message-ID: <20100304080130.16c6f060@o> On Wed, 3 Mar 2010 20:44:51 +0100 Giorgio wrote: > Please let me post the third update O_o. You can forgot other 2, i'll put > them into this email. > > --- > >>> s = "ciao ? ciao" > >>> print s > ciao ? ciao > >>> s.encode('utf-8') > > Traceback (most recent call last): > File "", line 1, in > s.encode('utf-8') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: > ordinal not in range(128) > --- > > I am getting more and more confused. What you enter on the terminal prompt is text, encoded in a format (ascii, latin*, utf*,...) that probably depends on your system locale. As this format is always a sequence of bytes, python stores it as a plain str: >>> s = "ciao ? ciao" >>> s,type(s) ('ciao \xc3\xa8 ciao', ) My system is parametered in utf8. c3-a8 is the repr of '?' in utf8. It needs 2 bytes because of the rules of utf8 itself. Right? To get a python unicode string, it must be decoded from its format, for me utf8: >>> u = s.decode("utf8") >>> u,type(u) (u'ciao \xe8 ciao', ) e8 is the unicode code for '?' (decimal 232). You can check that in tables. It needs here one byte only because 232<255. [comparison with php] > Ok, now, the point is: you (and the manual) said that this line: > > s = u"giorgio ? giorgio" > > will convert the string as unicode. Yes and no: it will convert it *into* a string, in the sense of a python representation for universal text. When seeing u"..." , python will automagically *decode* the part in "...", taking as source format the one you indicate in a pseudo-comment on top of you code file, eg: # coding: utf8 Else I guess the default is the system's locale format? Or ascii? Someone knows? So, in my case u"giorgio ? giorgio" is equivalent to "giorgio ? giorgio".decode("utf8"): >>> u1 = u"giorgio ? giorgio" >>> u2 = "giorgio ? giorgio".decode("utf8") >>> u1,u2 (u'giorgio \xe8 giorgio', u'giorgio \xe8 giorgio') >>> u1 == u2 True > But also said that the part between "" > will be encoded with my editor BEFORE getting encoded in unicode by python. will be encoded with my editor BEFORE getting encoded in unicode by python --> will be encoded *by* my editor BEFORE getting *decoded* *into* unicode by python > So please pay attention to this example: > > My editor is working in UTF8. I create this: > > c = "giorgio ? giorgio" // This will be an UTF8 string because of the file's > encoding Right. > d = unicode(c) // This will be an unicode string > e = c.encode() // How will be encoded this string? If PY is working like PHP > this will be an utf8 string. Have you tried it? >>> c = "giorgio ? giorgio" >>> d = unicode(c) Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128) Now, tell us why! (the answer is below *) > Can you help me? > > Thankyou VERY much > > Giorgio Denis (*) You don't tell which format the source string is encoded in. By default, python uses ascii (I know, it's stupid) which max code is 127. So, '?' is not accepted. Now, if I give a format, all works fine: >>> d = unicode(c,"utf8") >>> d u'giorgio \xe8 giorgio' Note: unicode(c,format) is an alias for c.decode(format): >>> c.decode("utf8") u'giorgio \xe8 giorgio' ________________________________ la vita e estrany spir.wikidot.com From denis.spir at gmail.com Thu Mar 4 08:47:04 2010 From: denis.spir at gmail.com (spir) Date: Thu, 4 Mar 2010 08:47:04 +0100 Subject: [Tutor] object representation Message-ID: <20100304084704.62344a90@o> Hello, In python like in most languages, I guess, objects (at least composite ones -- I don't know about ints, for instance -- someone knows?) are internally represented as associative arrays. Python associative arrays are dicts, which in turn are implemented as hash tables. Correct? Does this mean that the associative arrays representing objects are implemented like python dicts, thus hash tables? I was wondering about the question because I guess the constraints are quite different: * Dict keys are of any type, including heterogeneous (mixed). Object keys are names, ie a subset of strings. * Object keys are very stable, typically they hardly change; usually only values change. Dicts often are created empty and fed in a loop. * Objects are small mappings: entries are explicitely written in code (*). Dicts can be of any size, only limited by memory; they are often fed by computation. * In addition, dict keys can be variables, while object keys rarely are: they are literal constants (*). So, I guess the best implementations for objects and dicts may be quite different. i wonder about alternatives for objects, in particuliar trie and variants: http://en.wikipedia.org/wiki/Trie, because they are specialised for associative arrays which keys are strings. denis PS: Would someone point me to typical hash funcs for string keys, and the one used in python? (*) Except for rare cases using setattr(obj,k,v) or obj.__dict__[k]=v. -- ________________________________ la vita e estrany spir.wikidot.com From kalamata78 at yahoo.com Thu Mar 4 05:34:31 2010 From: kalamata78 at yahoo.com (Nicholas Hatzopoulos) Date: Wed, 3 Mar 2010 20:34:31 -0800 (PST) Subject: [Tutor] Python tutor Message-ID: <375744.40438.qm@web38807.mail.mud.yahoo.com> Hi my name is Nicholas and i just started with the basics on python 3.1. I will be starting research work on stars spectral analysis and graphics and i would like to know if anybody can please help me and tutor me in private or in groups. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Mar 4 09:50:53 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 4 Mar 2010 08:50:53 -0000 Subject: [Tutor] object representation References: <20100304084704.62344a90@o> Message-ID: "spir" wrote > Does this mean that the associative arrays representing objects are > implemented like python dicts, thus hash tables? Yes, in fact I think they are Python dicts - although I've never actually looked at the source to confirm that. > I was wondering about the question because I guess the constraints > are quite different: > * Dict keys are of any type, including heterogeneous (mixed). > Object keys are names, ie a subset of strings. The keys must be immutable but ... the object keys are a perfect subset of the generic dict. > * Object keys are very stable, typically they hardly change; > usually only values change. Dicts often are created empty and fed in a > loop. The normal case is as you say but you can if you with create an empty object and then later add attributes - in a loop if you wish. > * Objects are small mappings: entries are explicitely written in code > (*). > Dicts can be of any size, only limited by memory; they are often fed > by computation. But again the object usage is just a subset of the generic. > * In addition, dict keys can be variables, while object keys rarely > are: they are literal constants (*). variables in Python are just names that efer to objects. When you use a variable as a key to a dict you really use the value of the variable. >>> d = {} >>> x = 42 >>> d[x] = 66 >>> d[42] 66 >>> d[x] 66 >>> x = 5 >>> d[x] Traceback (most recent call last): File "", line 1, in d[x] KeyError: 5 >>> It is the value of x that is the key not 'x'. If you change the value of x it ceases to be a valid key of the dict. > So, I guess the best implementations for objects and dicts may > be quite different. i wonder about alternatives for objects, They could be different and perhaps optimised but I don't think they are. Pythons dicts are already pretty efficient. They form the backbone of almost everything in Python not just objects. > in particuliar trie and variants: http://en.wikipedia.org/wiki/Trie, > because they are specialised for associative arrays which keys are > strings. > PS: Would someone point me to typical hash funcs for string keys, > and the one used in python? Wikipedia for generic and the source for Python? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Mar 4 09:53:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 4 Mar 2010 08:53:44 -0000 Subject: [Tutor] Python tutor References: <375744.40438.qm@web38807.mail.mud.yahoo.com> Message-ID: "Nicholas Hatzopoulos" wrote > Hi my name is Nicholas and i just started with the basics on python 3.1. > I will be starting research work on stars spectral analysis and graphics > and i would like to know if anybody can please help me and tutor me > in private or in groups. The mailing list is the group. Just subscribe and send your questions. The group will try to answer. We don't do homework, but we will, even there, try to offer direction. When posting about errors please include the full error text and the code if not too long (otherwise use pastebin or similar). HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Thu Mar 4 10:05:26 2010 From: denis.spir at gmail.com (spir) Date: Thu, 4 Mar 2010 10:05:26 +0100 Subject: [Tutor] lazy? vs not lazy? and yielding In-Reply-To: <4B8EC97E.60102@ieee.org> References: <201003030920.33596.johnf@jfcomputer.com> <4B8EC97E.60102@ieee.org> Message-ID: <20100304100526.233f276b@o> On Wed, 03 Mar 2010 15:41:34 -0500 Dave Angel wrote: > John wrote: > > Hi, > > > > I just read a few pages of tutorial on list comprehenion and generator > > expression. From what I gather the difference is "[ ]" and "( )" at the > > ends, better memory usage and the something the tutorial labeled as "lazy > > evaluation". So a generator 'yields'. But what is it yielding too? > > > > John > > > > > A list comprehension builds a whole list at one time. So if the list > needed is large enough in size, it'll never finish, and besides, you'll > run out of memory and crash. A generator expression builds a function > instead which *acts* like a list, but actually doesn't build the values > till you ask for them. To append on Dave's explanation, let us a take special case: when the sequence is potentially infinite. Imagine you want a function (actually here a generator) to yield the sequence of squares of natural without any limit. The stop point will happen in the code using the generator. You could write it eg like that (this is just sample code): def squares(): squares.n = min if min while True: squares.n += 1 n = squares.n yield n*n squares.n = 0 Then, you can use it for instance to get the first 9 squares that happen to be multiples of 3. nineTripleSquares = [] for sq in squares(): if sq%3 == 0: nineTripleSquares.append(sq) if len(nineTripleSquares) == 9: break print nineTripleSquares # ==> [9, 36, 81, 144, 225, 324, 441, 576, 729] A list can hardly be used here, instead of a generator, because we do not know how long the list should be so that we get 9 final items. We can modify squares to give it borders: def squares(min=None, max=None): squares.n = min-1 if (min is not None) else 0 squares.max = max while True: squares.n += 1 n = squares.n if (squares.max is not None) and (n > squares.max): raise StopIteration yield n*n squares.n = 0 tripleSquares = [] for sq in squares(7,27): if sq%3 == 0: tripleSquares.append(sq) print tripleSquares # [81, 144, 225, 324, 441, 576, 729] A more object-oriented vaersion would look like: class Squares(object): def __init__(self, min, max): self.n = min-1 if (min is not None) else 0 self.max = max def next(self): self.n += 1 n = self.n if (self.max is not None) and (n > self.max): raise StopIteration return n*n def __iter__(self): return self tripleSquares = [] for sq in Squares(7,27): if sq%3 == 0: tripleSquares.append(sq) print tripleSquares # ==> same result This defines a new type of "iterable" objects. When you use "for item in obj" on such object, python looks for the magic method __iter__ to find its iterator: here, itself. Then it will repetedly call the iterator's next method to get each item. Denis -- ________________________________ la vita e estrany spir.wikidot.com From fomcl at yahoo.com Thu Mar 4 10:09:56 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 4 Mar 2010 01:09:56 -0800 (PST) Subject: [Tutor] Encoding In-Reply-To: <20100304080130.16c6f060@o> Message-ID: <829616.25561.qm@web110713.mail.gq1.yahoo.com> Hi, ? For everybody who's having trouble understanding encoding, I found this page useful: http://evanjones.ca/python-utf8.html Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Thu, 3/4/10, spir wrote: From: spir Subject: Re: [Tutor] Encoding To: tutor at python.org Date: Thursday, March 4, 2010, 8:01 AM On Wed, 3 Mar 2010 20:44:51 +0100 Giorgio wrote: > Please let me post the third update O_o. You can forgot other 2, i'll put > them into this email. > > --- > >>> s = "ciao ? ciao" > >>> print s > ciao ? ciao > >>> s.encode('utf-8') > > Traceback (most recent call last): >???File "", line 1, in >? ???s.encode('utf-8') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: > ordinal not in range(128) > --- > > I am getting more and more confused. What you enter on the terminal prompt is text, encoded in a format (ascii, latin*, utf*,...) that probably depends on your system locale. As this format is always a sequence of bytes, python stores it as a plain str: >>> s = "ciao ? ciao" >>> s,type(s) ('ciao \xc3\xa8 ciao', ) My system is parametered in utf8. c3-a8 is the repr of '?' in utf8. It needs 2 bytes because of the rules of utf8 itself. Right? To get a python unicode string, it must be decoded from its format, for me utf8: >>> u = s.decode("utf8") >>> u,type(u) (u'ciao \xe8 ciao', ) e8 is the unicode code for '?' (decimal 232). You can check that in tables. It needs here one byte only because 232<255. [comparison with php] > Ok, now, the point is: you (and the manual) said that this line: > > s = u"giorgio ? giorgio" > > will convert the string as unicode. Yes and no: it will convert it *into* a string, in the sense of a python representation for universal text. When seeing u"..." , python will automagically *decode* the part in "...", taking as source format the one you indicate in a pseudo-comment on top of you code file, eg: # coding: utf8 Else I guess the default is the system's locale format? Or ascii? Someone knows? So, in my case u"giorgio ? giorgio" is equivalent to "giorgio ? giorgio".decode("utf8"): >>> u1 = u"giorgio ? giorgio" >>> u2 = "giorgio ? giorgio".decode("utf8") >>> u1,u2 (u'giorgio \xe8 giorgio', u'giorgio \xe8 giorgio') >>> u1 == u2 True > But also said that the part between "" > will be encoded with my editor BEFORE getting encoded in unicode by python. will be encoded with my editor BEFORE getting encoded in unicode by python --> will be encoded *by* my editor BEFORE getting *decoded* *into* unicode by python > So please pay attention to this example: > > My editor is working in UTF8. I create this: > > c = "giorgio ? giorgio" // This will be an UTF8 string because of the file's > encoding Right. > d = unicode(c) // This will be an unicode string > e = c.encode() // How will be encoded this string? If PY is working like PHP > this will be an utf8 string. Have you tried it? >>> c = "giorgio ? giorgio" >>> d = unicode(c) Traceback (most recent call last): ? File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128) Now, tell us why! (the answer is below *) > Can you help me? > > Thankyou VERY much > > Giorgio Denis (*) You don't tell which format the source string is encoded in. By default, python uses ascii (I know, it's stupid) which max code is 127. So, '?' is not accepted. Now, if I give a format, all works fine: >>> d = unicode(c,"utf8") >>> d u'giorgio \xe8 giorgio' Note: unicode(c,format) is an alias for c.decode(format): >>> c.decode("utf8") u'giorgio \xe8 giorgio' ________________________________ la vita e estrany spir.wikidot.com _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Mar 4 13:20:35 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 Mar 2010 23:20:35 +1100 Subject: [Tutor] unittest In-Reply-To: <4B8F53F6.1040204@gmail.com> References: <4B8E9BFB.2030604@gmail.com> <201003040854.48898.steve@pearwood.info> <4B8F53F6.1040204@gmail.com> Message-ID: <201003042320.35673.steve@pearwood.info> On Thu, 4 Mar 2010 05:32:22 pm you wrote: > Steven D'Aprano wrote: > > On Thu, 4 Mar 2010 04:27:23 am C.T. Matsumoto wrote: > >> Hello, > >> > >> Can someone tell me the difference between unittests assertEqual > >> and assertEquals? > > > > assertEqual, assertEquals and failUnless are three spellings for > > the same thing. There is no difference. > > Thanks, > Okay, does anyone know why unittests have 3 ways to do the same > thing? They're not three different ways, they are three names for the same way. The unittest module is meant to be equivalent to Java's unit test library, so possibly it is because Java has three names for the same thing, and so Python's version tried to be as similar as possible. Or possibly because the author(s) of the unittest module couldn't agree on what name to give the functions. Or possibly it was deliberate, because the authors felt that sometimes you want a positive test "assert this is true" and sometimes you want a negative test "fail unless this is true". -- Steven D'Aprano From anothernetfellow at gmail.com Thu Mar 4 15:13:44 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Thu, 4 Mar 2010 15:13:44 +0100 Subject: [Tutor] Encoding In-Reply-To: <20100304080130.16c6f060@o> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> Message-ID: <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> Thankyou. You have clarificated many things in those emails. Due to high numbers of messages i won't quote everything. So, as i can clearly understand reading last spir's post, python gets strings encoded by my editor and to convert them to unicode i need to specify HOW they're encoded. This makes clear this example: c = "giorgio ? giorgio" d = c.decode("utf8") I create an utf8 string, and to convert it into unicode i need to tell python that the string IS utf8. Just don't understand why in my Windows XP computer in Python IDLE doesn't work: >>> ================================ RESTART ================================ >>> >>> c = "giorgio ? giorgio" >>> c 'giorgio \xe8 giorgio' >>> d = c.decode("utf8") Traceback (most recent call last): File "", line 1, in d = c.decode("utf8") File "C:\Python26\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode bytes in position 8-10: invalid data >>> In IDLE options i've set encoding to UTF8 of course. I also have some linux servers where i can try the IDLE but Putty doesn't seem to support UTF8. But, let's continue: In that example i've specified UTF8 in the decode method. If i hadn't set it python would have taken the one i specified in the second line of the file, right? As last point, i can't understand why this works: >>> a = u"giorgio ? giorgio" >>> a u'giorgio \xe8 giorgio' And this one doesn't: >>> a = "giorgio ? giorgio" >>> b = unicode(a) Traceback (most recent call last): File "", line 1, in b = unicode(a) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 8: ordinal not in range(128) >>> The second doesn't work because i have not told python how the string was encoded. But in the first too i haven't specified the encoding O_O. Thankyou again for your help. Giorgio -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Mar 4 15:22:52 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 04 Mar 2010 09:22:52 -0500 Subject: [Tutor] object representation In-Reply-To: <20100304084704.62344a90@o> References: <20100304084704.62344a90@o> Message-ID: <4B8FC23C.3@ieee.org> spir wrote: > Hello, > > In python like in most languages, I guess, objects (at least composite ones -- I don't know about ints, for instance -- someone knows?) are internally represented as associative arrays. Python associative arrays are dicts, which in turn are implemented as hash tables. Correct? > Does this mean that the associative arrays representing objects are implemented like python dicts, thus hash tables? > > I was wondering about the question because I guess the constraints are quite different: > * Dict keys are of any type, including heterogeneous (mixed). Object keys are names, ie a subset of strings. > * Object keys are very stable, typically they hardly change; usually only values change. Dicts often are created empty and fed in a loop. > * Objects are small mappings: entries are explicitely written in code (*). Dicts can be of any size, only limited by memory; they are often fed by computation. > * In addition, dict keys can be variables, while object keys rarely are: they are literal constants (*). > > So, I guess the best implementations for objects and dicts may be quite different. i wonder about alternatives for objects, in particuliar trie and variants: http://en.wikipedia.org/wiki/Trie, because they are specialised for associative arrays which keys are strings. > > denis > > PS: Would someone point me to typical hash funcs for string keys, and the one used in python? > > http://effbot.org/zone/python-hash.htm > (*) Except for rare cases using setattr(obj,k,v) or obj.__dict__[k]=v. > Speaking without knowledge of the actual code implementing CPython (or for that matter any of the other dozen implementations), I can comment on my *model* of how Python "works." Sometimes it's best not to know (or at least not to make use of) the details of a particular implementation, as your code is more likely to port readily to the next architecture, or even next Python version. I figure every object has exactly three items in it: a ref count, a implementation pointer, and a payload. The payload may vary between 4 bytes for an int object, and about 4 megabytes for a list of size a million. (And of course arbitrarily large for larger objects). You can see that these add up to 12 bytes (in version 2.6.2 of CPython) for an int by using sys.getsizeof(92). Note that if the payload refers to other objects, those sizes are not included in the getsizeof() function result. So getsizeof(a list of strings) will not show the sizes of the strings, but only the list itself. The payload for a simple object will contain just the raw data of the object. So for a string, it'd contain the count and the bytes. For compound objects that can change in size, it'd contain a pointer to a malloc'ed buffer that contains the variable-length data. The object stays put, but the malloc'ed buffer may move as it size grows and shrinks. getsizeof() is smart enough to report not only the object itself, but the buffer it references. Note that buffer is referenced by only one object, so its lifetime is intimately tied up with the object's. The bytes in the payload are meaningless without the implementation pointer. That implementation pointer will be the same for all instances of a particular type. It points to a structure that defines a particular type (or class). That structure for an empty class happens to be 452 bytes, but that doesn't matter much, as it only appears once per class. The instance of an empty class is only 32 bytes. Now, even that might seem a bit big, so Python offers the notion of slots, which reduces the size of each instance, at the cost of a little performance and a lot of flexibility. Still, slots are important, because I suspect that's how built-ins are structured, to make the objects so small. Now, some objects, probably most of the built-ins, are not extensible. You can't add new methods, or alter the behavior much. Other objects, such as instances of a class you write, are totally and very flexible. I won't get into inheritance here, except to say that it can be tricky to derive new classes from built-in types. So where do associative arrays come in? One of the builtin types is a dictionary, and that is core to much of the workings of Python. There are dictionaries in each class implementation (that 452 bytes I mentioned). And there may be dictionaries in the instances themselves. There are two syntaxes to directly access these dictionaries, the "dot" notation and the bracket [] notation. The former is a simple indirection through a special member called __dict__. So the behavior of an object depends on its implementation pointer, which points to a structure. And parts of that structure ultimately point to C code whch does all the actual work. But most of the work is some double- or triple-indirection which ultimately calls code. From denis.spir at gmail.com Thu Mar 4 17:07:01 2010 From: denis.spir at gmail.com (spir) Date: Thu, 4 Mar 2010 17:07:01 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030509lfae3518hd4e186a61227e13@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> Message-ID: <20100304170701.73c64f44@o> On Thu, 4 Mar 2010 15:13:44 +0100 Giorgio wrote: > Thankyou. > > You have clarificated many things in those emails. Due to high numbers of > messages i won't quote everything. > > So, as i can clearly understand reading last spir's post, python gets > strings encoded by my editor and to convert them to unicode i need to > specify HOW they're encoded. This makes clear this example: > > c = "giorgio ? giorgio" > d = c.decode("utf8") > > I create an utf8 string, and to convert it into unicode i need to tell > python that the string IS utf8. > > Just don't understand why in my Windows XP computer in Python IDLE doesn't > work: > > >>> ================================ RESTART > ================================ > >>> > >>> c = "giorgio ? giorgio" > >>> c > 'giorgio \xe8 giorgio' > >>> d = c.decode("utf8") > > Traceback (most recent call last): > File "", line 1, in > d = c.decode("utf8") > File "C:\Python26\lib\encodings\utf_8.py", line 16, in decode > return codecs.utf_8_decode(input, errors, True) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 8-10: > invalid data > >>> How do you know your win XP terminal is configured to deal with text using utf8? Why do you think it should? Don't know much about windows, but I've read they have their own character sets (and format?). So, probably, if you haven't personalized it, it won't. (Conversely, I guess Macs use utf8 as default. Someone confirms?) In other words, c is not a piece of text in utf8. > In IDLE options i've set encoding to UTF8 of course. I also have some linux > servers where i can try the IDLE but Putty doesn't seem to support UTF8. > > But, let's continue: > > In that example i've specified UTF8 in the decode method. If i hadn't set it > python would have taken the one i specified in the second line of the file, > right? > > As last point, i can't understand why this works: > > >>> a = u"giorgio ? giorgio" > >>> a > u'giorgio \xe8 giorgio' This trial uses the default format of your system. It does the same as a = "giorgio ? giorgio".encode(default_format) It's a shorcut for ustring *literals* (constants), directly expressed by the programmer. In source code, it would use the format specified on top of the file. > And this one doesn't: > > >>> a = "giorgio ? giorgio" > >>> b = unicode(a) > > Traceback (most recent call last): > File "", line 1, in > b = unicode(a) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 8: > ordinal not in range(128) This trial uses ascii because you give no format (yes, it can be seen as a flaw). It does the same as a = "giorgio ? giorgio".encode("ascii") > >>> > > The second doesn't work because i have not told python how the string was > encoded. But in the first too i haven't specified the encoding O_O. > > Thankyou again for your help. > > Giorgio Denis -- ________________________________ la vita e estrany spir.wikidot.com From anothernetfellow at gmail.com Thu Mar 4 20:23:06 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Thu, 4 Mar 2010 20:23:06 +0100 Subject: [Tutor] Encoding In-Reply-To: <20100304170701.73c64f44@o> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> Message-ID: <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> 2010/3/4 spir > > > How do you know your win XP terminal is configured to deal with text using > utf8? Why do you think it should? > I think there is an option in IDLE configuration to set this. So, if my entire system is not utf8 i can't use the IDLE for this test? > > This trial uses the default format of your system. It does the same as > a = "giorgio ? giorgio".encode(default_format) > It's a shorcut for ustring *literals* (constants), directly expressed by > the programmer. In source code, it would use the format specified on top of > the file. > > This trial uses ascii because you give no format (yes, it can be seen as a > flaw). It does the same as > a = "giorgio ? giorgio".encode("ascii") > Ok,so you confirm that: s = u"ciao ? ciao" will use the file specified encoding, and that t = "ciao ? ciao" t = unicode(t) Will use, if not specified in the function, ASCII. It will ignore the encoding I specified on the top of the file. right? Again, thankyou. I'm loving python and his community. Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Thu Mar 4 20:37:12 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Thu, 4 Mar 2010 20:37:12 +0100 Subject: [Tutor] WSGI / Apache Message-ID: <23ce85921003041137s1c1d9c85rfa5c1e7685ff9f23@mail.gmail.com> Hi, as you all probably know i'm using the Google App Engine platform for my python code. As I learn python i try to understand more and more how GAE works. Today i've noticed that all applications on GAE are running on WSGI. A quick Google search told me that WSGI is a new standard for web applications, developed by python. So, I decided to try it. With Apache. I found that Apache has a dedicated mod, mod_wsgi to handle this type of requests. But, as I always try to be "platform/daemon indipended" i've looked for other solutions. I found WSGIREF that as wsgi.org states "includes a threaded HTTP server, a CGI server (for running any WSGI application as a CGI script)". So it seems that wsgiref, that is actually included in python, can "convert" my apps to work with a standard CGI interface (in other words, working with EVERY server that supports CGI not only with apache and his dedicated mod). Can you confirm this? Do you have any idea i should use mod_wsgi instead of wsgiref + mod_cgi? Thankyou Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreas at kostyrka.org Thu Mar 4 21:57:18 2010 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Thu, 4 Mar 2010 21:57:18 +0100 Subject: [Tutor] lazy? vs not lazy? and yielding In-Reply-To: <4B8EC97E.60102@ieee.org> References: <201003030920.33596.johnf@jfcomputer.com> <4B8EC97E.60102@ieee.org> Message-ID: <201003042157.18601.andreas@kostyrka.org> Am Mittwoch, 3. M?rz 2010 21:41:34 schrieb Dave Angel: > John wrote: > > Hi, > > > > I just read a few pages of tutorial on list comprehenion and generator > > expression. From what I gather the difference is "[ ]" and "( )" at the > > ends, better memory usage and the something the tutorial labeled as "lazy > > evaluation". So a generator 'yields'. But what is it yielding too? > > > > John > > A list comprehension builds a whole list at one time. So if the list > needed is large enough in size, it'll never finish, and besides, you'll > run out of memory and crash. A generator expression builds a function > instead which *acts* like a list, but actually doesn't build the values Well, it act like an iterable. A list-like object would probably have __getitem__ and friends. Historically (as in I doubt you'll find a version that still does it that way in the wild, I looked it up, guess around 2.2 iterators were introduced), the for loop in python has been doing a "serial" __getitem__ call. Nowadays, for i in x: print i does something like this: x_it = iter(x) # get an iterator for x try: while True: i = x_it.next() print i except StopIteration: pass As you can see, x_it (and the underlying x) do not need to provide all items in memory at the same time. Andreas From steve at pearwood.info Fri Mar 5 00:07:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Mar 2010 10:07:34 +1100 Subject: [Tutor] lazy? vs not lazy? and yielding In-Reply-To: <201003042157.18601.andreas@kostyrka.org> References: <201003030920.33596.johnf@jfcomputer.com> <4B8EC97E.60102@ieee.org> <201003042157.18601.andreas@kostyrka.org> Message-ID: <201003051007.35041.steve@pearwood.info> On Fri, 5 Mar 2010 07:57:18 am Andreas Kostyrka wrote: > > A list comprehension builds a whole list at one time. So if the > > list needed is large enough in size, it'll never finish, and > > besides, you'll run out of memory and crash. A generator > > expression builds a function instead which *acts* like a list, but > > actually doesn't build the values > > Well, it act like an iterable. A list-like object would probably have > __getitem__ and friends. Historically (as in I doubt you'll find a > version that still does it that way in the wild, I looked it up, > guess around 2.2 iterators were introduced), the for loop in python > has been doing a "serial" __getitem__ call. for loops still support the serial __getitem__ protocol. While it is obsolete, it hasn't been removed, as it is still needed to support old code using it. When you write "for item in thing", Python first attempts the iterator protocol. It tries to call thing.next(), and if that succeeds, it repeatedly calls that until it raises StopIteration. If thing doesn't have a next method, it tries calling thing.__getitem__(0), __getitem__(1), __getitem__(2), and so on, until it raises IndexError. If thing doesn't have a __getitem__ method either, then the loop fails with TypeError. -- Steven D'Aprano From steve at pearwood.info Fri Mar 5 02:21:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Mar 2010 12:21:46 +1100 Subject: [Tutor] object representation In-Reply-To: <4B8FC23C.3@ieee.org> References: <20100304084704.62344a90@o> <4B8FC23C.3@ieee.org> Message-ID: <201003051221.47394.steve@pearwood.info> On Fri, 5 Mar 2010 01:22:52 am Dave Angel wrote: > spir wrote: [...] > > PS: Would someone point me to typical hash funcs for string keys, > > and the one used in python? > > http://effbot.org/zone/python-hash.htm But note that this was written a few years ago, and so may have been changed. As for "typical" hash functions, I don't think there is such a thing. I think everyone creates there own, unless there is a built-in hash function provided by the language or the library, which is what Python does. I've seen hash functions as simple as: def hash(s): if not s: return 0 else: return ord(s[0]) but of course that leads to *many* collisions. Slightly better (but not much!) is def hash(s): n = 0 for c in s: n += ord(c) return n % sys.maxint This is a pretty awful hash function though. Don't use it. You might also like to read this thread, to get an idea of the thought that has been put into Python's hashing: http://mail.python.org/pipermail/python-dev/2004-April/044244.html [...] > I figure every object has exactly three items in it: a ref count, a > implementation pointer, and a payload. Not quite. CPython has ref counts. IronPython and Jython don't. Other Pythons may or may not. I'm not quite sure what you mean by "implementation pointer" -- I think you're talking about a pointer back to the type itself. It's normal to just to refer to this as "the type", and ignore the fact that it's actually a pointer. The type data structure (which itself will be an object) itself is not embedded in every object! And of course, other Pythons may use some other mechanism for linking objects back to their type. -- Steven D'Aprano From steve at pearwood.info Fri Mar 5 02:45:59 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Mar 2010 12:45:59 +1100 Subject: [Tutor] object representation In-Reply-To: <20100304084704.62344a90@o> References: <20100304084704.62344a90@o> Message-ID: <201003051245.59301.steve@pearwood.info> On Thu, 4 Mar 2010 06:47:04 pm spir wrote: > Hello, > > In python like in most languages, I guess, objects (at least > composite ones -- I don't know about ints, for instance -- someone > knows?) are internally represented as associative arrays. No. You can consider a Python object to be something like a C struct, or a Pascal record. The actual structure of the object depends on the type of the object (ints, strings, lists, etc will be slightly different). See Dave Angel's post for a good description. And of course this is implementation dependent: CPython, being written in C, uses C structs to implement objects. Other Pythons, written in other languages, will use whatever data structure makes sense for their language. That's almost certainly going to be a struct-like structure, since that is a pretty fundamental data type, but it could be different. If you want to know exactly how objects are represented internally by Python, I'm afraid you will probably need to read the source code. But a good start is here: http://effbot.org/zone/python-objects.htm > Python > associative arrays are dicts, which in turn are implemented as hash > tables. Correct? Does this mean that the associative arrays > representing objects are implemented like python dicts, thus hash > tables? "Associate array" is a generic term for a data structure that maps keys to items. There are lots of ways of implementing such an associative array: at least two different sorts of hash tables, about a dozen different types of binary trees, and so on. In Python, associate arrays are called "dicts", and they are implemented in CPython as hash tables with chaining. But objects are not hash tables. *Some* objects INCLUDE a hash table as one of its fields, but not all. For example: >>> int.__dict__ >>> (2).__dict__ Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute '__dict__' >>> class C: pass ... >>> C.__dict__ {'__module__': '__main__', '__doc__': None} That is why you can't add attributes to arbitrary built-in objects: >>> {}.x = None Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'x' Because the dict instance has no __dict__, there is nowhere to insert the attribute. > I was wondering about the question because I guess the constraints > are quite different: * Dict keys are of any type, including > heterogeneous (mixed). Object keys are names, ie a subset of strings. CPython's implementation of hash tables is highly optimized for speed. The biggest bottleneck is the hash function, and that is tuned to be extremely efficient for strings and ints. [...] > So, I guess the best implementations for objects and dicts may be > quite different. i wonder about alternatives for objects, in > particuliar trie and variants: http://en.wikipedia.org/wiki/Trie, > because they are specialised for associative arrays which keys are > strings. *shrug* Maybe, maybe not. Tries are a more complicated data structure, which means bigger code and more bugs. They don't fit in very well with CPython's memory management system. And they use a large number of pointers, which can be wasteful. E.g. a trie needs six pointers just to represent the single key "python": '' -> 'p' -> 'y' -> 't' -> 'h' -> 'o' -> 'n' while a hash table uses just one: -> 'python' -- Steven D'Aprano From steve at pearwood.info Fri Mar 5 03:24:15 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Mar 2010 13:24:15 +1100 Subject: [Tutor] Understanding (Complex) Modules In-Reply-To: <4B8F0BD3.8050000@sbcglobal.net> References: <4B8F0BD3.8050000@sbcglobal.net> Message-ID: <201003051324.16104.steve@pearwood.info> On Thu, 4 Mar 2010 12:24:35 pm Wayne Watson wrote: > First a little preamble before my questions. > > Most of my work in Python has required modifying a program that uses > modules that were imported by the original program. I've made some > use of modules on a command line like math, and have used the idea of > a qualifier. On occasion, I've used examples from matplotlib that > required from matplotlib.image import AxesImage. Further, I've > created some simple classes, and produced objects with them. No use > of inheritance though. So far so good. In a few places, it is said > modules are objects. I'm slightly puzzled by that, but in some way it > seems reasonable from the standpoint of period notation. So far I > have not created a module. Yes you have, you just don't know it. In simple language, a file with Python-usable code in it is a module. There are some technicalities and complexities, but in basic terms, that's all it is. Technically, "module" refers only to the object which exists inside the Python environment after you call "import thing". The import statement does a whole lot of tricks, but in a nutshell it: * finds a file containing code called 'thing' * loads it into memory, executing code as needed * creates a 'module' object to store the code and data in the file * and makes it available to your code Python can create module objects from: compiled Windows DLLs .dll compiled Linux object files .so Python source code .py Python byte code .pyc and .pyo (and .pyw on Windows) Zip files containing any of the above and probably other things as well, but they're the main ones. So any Python file you create (any .py file) is a module, or at least it would be a module if you import it. Ignoring all the various compiled files (.dll, .so, etc) what happens when you run a .py file from the command line (or from IDLE or another IDE). E.g. you type something like: python.exe myscript.py [enter] Python reads the file myscript.py and executes it, but no module object is created. It is possible that a module object *is* created, for internal use, then thrown away when the script is finished. But your script doesn't see the module object. However, if you enter the Python interactive environment, and do this: >>> import myscript # no .py Python loads the file myscript.py into a module object, executing any code in it, and makes it available as a module. > Here comes the questions. Recently I began to use matplotlib, scipy, > and pylab, mostly matplotlib. I've ground out a few useful pieces of > code, but I'm fairly baffled by the imports required to get at > various elements, of say, matplotlib (MPL). Some of the MPL examples > use of imports make sense, but how the writer pulled in the necessary > elements is not. How does one go about understanding the > capabilities of such modules? Time, experience, and a lot of hard work. Welcome to programming! If the documentation is good, then read the documentation. If the documentation is lacking, or bad, or even wrong, then read the source code, or search the Internet for a tutorial, or buy a book about it (the numpy people, for example, sell books about numpy). Python makes experimentation easy: there are a lot of powerful introspection facilities in Python. The interactive interpreter is your best friend. You will live with it, eat with it, sleep with it, take it on double-dates, and throw yourself on live grenades to protect it. Whenever I'm programming, I almost always have three or five interactive sessions open for experimentation. The dir() and help() functions are also good friends. In an interactive session: import math dir(math) # prints a list of names in the math module help(math.sin) help(math) Don't feel that you have to understand the entire module before you use it. Many (alas, not all) modules have a reasonably gentle learning curve: you can start using math.sin without needing to know what math.sinh is for. Google and Wikipedia are also your friends, although not your *best* friends. (Don't necessarily believe *everything* you read on the Internet.) Don't forget other search engines apart from Google: they're good, but not perfect. > MPL seems to have a lot of lower level > components. Some of them are laid out over numerous pages as in the > form of a, say, English language, description. How does one decipher > this stuff. For example, open the module in an editor and start > looking at the organization? I thinkthe so called MPL guide ins 900 > pages long. Even the numpy guide (reference?), I believe borders on > 1000 pages. There must be some way to untangle these complex modules, > I would think. Some of the tutorials seem nothing more than a > template to follow for a particular problem. So far, looking at the > plentiful number of examples of MPL, and probably some of the other > modules mentioned above have not provided a lot of insight. Big, complex modules tend to have steep learning curves. There's no magic path to learning how to use a big complex module any more than there is a magic path to learning how to be a brain surgeon, or a car mechanic. > Is there some relationship between modules and objects that I'm not > seeing that could be of value? Modules are themselves objects. Everything in Python is an object: strings, ints, floats, lists, tuples, everything. Modules are compound objects, in that they contain other objects accessible by name: math.sin means "look up the name 'sin' in the math module, and return whatever you find", which in this case is a function object. And that's pretty much it. -- Steven D'Aprano From davea at ieee.org Fri Mar 5 03:39:27 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 04 Mar 2010 21:39:27 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B8E64E6.5080302@gmail.com> <23ce85921003030650k6d238825s12c11ca1118546fd@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> Message-ID: <4B906EDF.8050409@ieee.org> Giorgio wrote: > 2010/3/4 spir > > > Ok,so you confirm that: > > s = u"ciao ? ciao" will use the file specified encoding, and that > > t = "ciao ? ciao" > t = unicode(t) > > Will use, if not specified in the function, ASCII. It will ignore the > encoding I specified on the top of the file. right? > > A literal "u" string, and only such a (unicode) literal string, is affected by the encoding specification. Once some bytes have been stored in a 8 bit string, the system does *not* keep track of where they came from, and any conversions then (even if they're on an adjacent line) will use the default decoder. This is a logical example of what somebody said earlier on the thread -- decode any data to unicode as early as possible, and deal only with unicode strings in the program. Then, if necessary, encode them into whatever output form immediately before (or while) outputting them. > Again, thankyou. I'm loving python and his community. > > Giorgio > > > > > From denis.spir at gmail.com Fri Mar 5 07:58:15 2010 From: denis.spir at gmail.com (spir) Date: Fri, 5 Mar 2010 07:58:15 +0100 Subject: [Tutor] lazy? vs not lazy? and yielding In-Reply-To: <201003042157.18601.andreas@kostyrka.org> References: <201003030920.33596.johnf@jfcomputer.com> <4B8EC97E.60102@ieee.org> <201003042157.18601.andreas@kostyrka.org> Message-ID: <20100305075815.4539950d@o> On Thu, 4 Mar 2010 21:57:18 +0100 Andreas Kostyrka wrote: I would rather write it: > x_it = iter(x) # get an iterator for x > try: > while True: > i = x_it.next() > print i > except StopIteration: > pass x_it = iter(x) # get an iterator for x while True: try: i = x_it.next() except StopIteration: break else: print i (The trial is about getting each new element. The exception breaks the loop. But maybe it's only me.) Denis -- ________________________________ la vita e estrany spir.wikidot.com From davea at ieee.org Fri Mar 5 09:01:38 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 05 Mar 2010 03:01:38 -0500 Subject: [Tutor] object representation In-Reply-To: <20100305082221.0fe7feae@o> References: <20100304084704.62344a90@o> <4B8FC23C.3@ieee.org> <20100305082221.0fe7feae@o> Message-ID: <4B90BA62.8080606@ieee.org> spir wrote: > On Thu, 04 Mar 2010 09:22:52 -0500 > Dave Angel wrote: > > >> Still, slots are important, because I suspect >> that's how built-ins are structured, to make the objects so small. >> > > Sure, one cannot alter their structure. Not even of a direct instance of : > >>>> o = object() >>>> o.n=1 >>>> > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'object' object has no attribute 'n' > > >> Now, some objects, probably most of the built-ins, are not extensible. >> You can't add new methods, or alter the behavior much. >> > > This applies to any attr, not only methods, also plain "information": > >>>> s = "abc" >>>> s.n=1 >>>> > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'str' object has no attribute 'n' > > > >> Other objects, >> such as instances of a class you write, are totally and very flexible. >> > > conceptually this is equivalent to have no __slots__ slot. Or mayby they could be implemented using structs (which values would be pointers), instead of dicts. A struct is like a fixed record, as opposed to a dict. What do you think? On the implementation side, this would be much simpler, lighter, and more efficient. > Oh, this gives me an idea... (to implement so-called "value objects"). > > Denis > having not played much with slots, my model is quite weak there. But I figure the dictionary is in the implementation structure, along with a flag saying that it's readonly. Each item of such a dictionary would be an index into the fixed table in the object. Like a struct, as you say, except that in C, there's no need to know the names of the fields at run time. DaveA From metolone+gmane at gmail.com Fri Mar 5 09:05:58 2010 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 5 Mar 2010 00:05:58 -0800 Subject: [Tutor] How to wrap ctype functions References: Message-ID: "Jan Jansen" wrote in message news:f17500b71003030943w606925edie41b41d6d64ef6cf at mail.gmail.com... > Hi there, > > I wonder what's the best way to wrap given function calls (in this case > ctype function calls but more generally built-in functions and those > kinds). > I have a huge c library and almost all functions return an error code. The > library also contains a function, that returns the corresponding error > message to the error code. So, what I need to do for every call to one of > the libraries functions looks like this: > > error_code = my_c_library.SOME_FUNCTION_ > CALL(argument) > if error_code != 0: > error_message = my_c_library.GET_ERROR_TEXT(error_code) > print "error in function call SOME_FUNCTION_CALL" > print error_message > my_c_library.EXIT_AND_CLEAN_UP() ctypes has a couple of methods to post process return values. 1. A callable can be assigned has the .restype attribute of the function object. In this case, the function is assumed to return an integer, and it is passed to the callable. The function could raise an exception for non-zero return values. This usage is deprecated. See "16.15.1.8. Return types" in the Python docs (http://docs.python.org/library/ctypes.html). 2. Any return type can be assigned to .restype, and a callable can be assigned to the .errcheck attribute. This has more flexibility than #1 since it works for return types without int. The callable is provided the return value, original function object, and original arguments to help customize the behavior of the return value. See "16.15.2.3. Foreign functions" in the docs. > > Also, for some function calls I would need to do some preperations like: > > error_code = my_c_library.LOG_IN_AS_ADMINISTRATOR(admin_user, > admin_password) > error_code = my_c_library.SOME_FUNCTION_CALL(argument) > > I like the decorator idea, but I can't figure out if it's applicable here. > To be able to call the function in a manner like this would be great, e.g. > > @change_user(admin_user, admin_password) > @error_handling > my_c_library.SOME_FUNCTION_CALL(argument) Decorators don't decorate calls to functions, just function declarations. Here's a *rough* example: >>> def prep(func): ... def prepwork(*args,**kwargs): ... print 'doing prep work...' ... func(*args,**kwargs) ... return prepwork ... >>> @prep ... def something(a,b): ... print a,b ... >>> something(1,2) doing prep work... 1 2 -Mark From alan.gauld at btinternet.com Fri Mar 5 10:44:48 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 5 Mar 2010 09:44:48 +0000 (GMT) Subject: [Tutor] My tutorial Message-ID: <617634.43611.qm@web86704.mail.ird.yahoo.com> Apologies to any users of my tutorial who can't access it I am a victim of my own success and have bust the bandwidth limit on the web site for the month. I will need to upgrade my subscription package... Until then, apologies once more. The old V2 freenet site seems to still be available if you are stuck... http://www.freenetpages.co.uk/hp/alan.gauld/ Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Fri Mar 5 14:41:44 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Fri, 5 Mar 2010 08:41:44 -0500 Subject: [Tutor] Collaborative Comp Sci education using Python Message-ID: Hey everyone, A while back some folks said they were organizing an online study group to work through Wesley Chun's Core Python. I just stumbled into another online study group that might also be of interest: http://www.crunchcourse.com/class/mit-opencourseware-600-introduction/2010/jan/ The study group is built around MIT's OpenCourseware's Into to CompSci and Programming, which uses Python as its instructional language. http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2008/CourseHome/index.htm I'd be interested in hearing of similar Python-based study groups out there, so shout out if you know of any. Regards, Serdar -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Fri Mar 5 14:56:37 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Fri, 5 Mar 2010 14:56:37 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B906EDF.8050409@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> Message-ID: <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> > > >> Ok,so you confirm that: >> >> s = u"ciao ? ciao" will use the file specified encoding, and that >> >> t = "ciao ? ciao" >> t = unicode(t) >> >> Will use, if not specified in the function, ASCII. It will ignore the >> encoding I specified on the top of the file. right? >> >> >> > A literal "u" string, and only such a (unicode) literal string, is > affected by the encoding specification. Once some bytes have been stored in > a 8 bit string, the system does *not* keep track of where they came from, > and any conversions then (even if they're on an adjacent line) will use the > default decoder. This is a logical example of what somebody said earlier on > the thread -- decode any data to unicode as early as possible, and deal only > with unicode strings in the program. Then, if necessary, encode them into > whatever output form immediately before (or while) outputting them. > > > Ok Dave, What i don't understand is why: s = u"ciao ? ciao" is converting a string to unicode, decoding it from the specified encoding but t = "ciao ? ciao" t = unicode(t) That should do exactly the same instead of using the specified encoding always assume that if i'm not telling the function what the encoding is, i'm using ASCII. Is this a bug? Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri Mar 5 15:27:20 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 05 Mar 2010 09:27:20 -0500 Subject: [Tutor] Understanding (Complex) Modules In-Reply-To: <201003051324.16104.steve@pearwood.info> References: <4B8F0BD3.8050000@sbcglobal.net> <201003051324.16104.steve@pearwood.info> Message-ID: <4B9114C8.201@gmail.com> Clarifications: A module is a file. It may or may not contain python code. If it does not an exception will be raised when importing. Import executes the module's code exactly the same as if the module had been run as a script (main program). References to module objects are stored in a dict (sys.modules)s. You will notice many modules that have been "pre-imported": >>> import sys >>> for x in sys.modules.keys(): print x ... 'copy_reg' 'sre_compile' 'locale' '_sre' 'functools' 'encodings' 'site' '__builtin__' 'operator' '__main__' 'types' 'encodings.encodings' 'abc' 'encodings.cp437' 'errno' 'encodings.codecs' 'sre_constants' 're' '_abcoll' 'ntpath' '_codecs' 'nt' '_warnings' 'genericpath' 'stat' 'zipimport' 'encodings.__builtin__' 'warnings' 'UserDict' 'encodings.cp1252' 'sys' 'codecs' 'os.path' '_functools' '_locale' 'signal' 'linecache' 'encodings.aliases' 'exceptions' 'sre_parse' 'os' >>> So all import sys does is: sys = sys.modules['sys'] Whereas import foo (assuming we refer to foo.py): if 'foo' in sys.modules: foo = sys.modules['foo'] else: compile foo.py if successful: execute the compiled code thus creating a module object if successful: sys.modules['foo'] = the new module object foo = sys.modules['foo'] Herelin lies a gotcha: import foo again does NOT recompile; it just reassigns foo = sys.modules['foo']. reload(foo) will go thru the compile execute assign sequence again. Notice __main__ - that is the module of the main program. >>> sys.modules['__main__'] >>> dir(sys.modules['__main__']) ['__builtins__', '__doc__', '__name__', '__package__', 'sys', 'x'] >>> -- Bob Gailer 919-636-4239 Chapel Hill NC From lie.1296 at gmail.com Fri Mar 5 17:09:02 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 06 Mar 2010 03:09:02 +1100 Subject: [Tutor] object representation In-Reply-To: <201003051245.59301.steve@pearwood.info> References: <20100304084704.62344a90@o> <201003051245.59301.steve@pearwood.info> Message-ID: On 03/05/2010 12:45 PM, Steven D'Aprano wrote: > E.g. a trie needs six pointers just to represent the single > key "python": > > '' -> 'p' -> 'y' -> 't' -> 'h' -> 'o' -> 'n' > > while a hash table uses just one: > > -> 'python' You can argue that had trie beed used as the datatype, there will actually be no need to store the key's string representation; the index of the object in the trie implies the textual representation. Such that, you will still need 6 pointers, but you won't need to store a string object. It will just be: '' -> ptrP -> ptrY -> ptrT -> ptrH -> ptrO -> ptrN and if for some reason the name is needed (perhaps for debugging?); then there could be an algorithm to reverse-map the ptrXs to char. I can imagine that to be implementable if variable names in python be limited to alphanumerics only and probably a select few of symbols. Unicode names makes things difficult though... From davea at ieee.org Fri Mar 5 17:45:36 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 05 Mar 2010 11:45:36 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> Message-ID: <4B913530.6030003@ieee.org> Giorgio wrote: >> >>> Ok,so you confirm that: >>> >>> s = u"ciao ? ciao" will use the file specified encoding, and that >>> >>> t = "ciao ? ciao" >>> t = unicode(t) >>> >>> Will use, if not specified in the function, ASCII. It will ignore the >>> encoding I specified on the top of the file. right? >>> >>> >>> >>> >> A literal "u" string, and only such a (unicode) literal string, is >> affected by the encoding specification. Once some bytes have been stored in >> a 8 bit string, the system does *not* keep track of where they came from, >> and any conversions then (even if they're on an adjacent line) will use the >> default decoder. This is a logical example of what somebody said earlier on >> the thread -- decode any data to unicode as early as possible, and deal only >> with unicode strings in the program. Then, if necessary, encode them into >> whatever output form immediately before (or while) outputting them. >> >> >> >> > Ok Dave, What i don't understand is why: > > s = u"ciao ? ciao" is converting a string to unicode, decoding it from the > specified encoding but > > t = "ciao ? ciao" > t = unicode(t) > > That should do exactly the same instead of using the specified encoding > always assume that if i'm not telling the function what the encoding is, i'm > using ASCII. > > Is this a bug? > > Giorgio > In other words, you don't understand my paragraph above. Once the string is stored in t as an 8 bit string, it's irrelevant what the source file encoding was. If you then (whether it's in the next line, or ten thousand calls later) try to convert to unicode without specifying a decoder, it uses the default encoder, which is a application wide thing, and not a source file thing. To see what it is on your system, use sys.getdefaultencoding(). There's an encoding specified or implied for each source file of an application, and they need not be the same. It affects string literals that come from that particular file. It does not affect any other conversions, as far as I know. For that matter, many of those source files may not even exist any more by the time the application is run. There are also encodings attached to each file object, I believe, though I've got no experience with that. So sys.stdout would have an encoding defined, and any unicode strings passed to it would be converted using that specification. The point is that there isn't just one global value, and it's a good thing. You should figure everywhere characters come into your program (eg. source files, raw_input, file i/o...) and everywhere characters go out of your program, and deal with each of them individually. Don't store anything internally as strings, and you won't create the ambiguity you have with your 't' variable above. DaveA From anothernetfellow at gmail.com Fri Mar 5 18:15:06 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Fri, 5 Mar 2010 18:15:06 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B913530.6030003@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> Message-ID: <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> 2010/3/5 Dave Angel > > In other words, you don't understand my paragraph above. Maybe. But please don't be angry. I'm here to learn, and as i've run into a very difficult concept I want to fully undestand it. > Once the string is stored in t as an 8 bit string, it's irrelevant what the > source file encoding was. Ok, you've said this 2 times, but, please, can you tell me why? I think that's the key passage to understand how encoding of strings works. The source file encoding affects all file lines, also strings. If my encoding is UTF8 python will read the string "ciao ? ciao" as 'ciao \xc3\xa8 ciao' but if it's latin1 it will read 'ciao \xe8 ciao'. So, how can it be irrelevant? I think the problem is that i can't find any difference between 2 lines quoted above: a = u"ciao ? ciao" and a = "ciao ? ciao" a = unicode(a) > If you then (whether it's in the next line, or ten thousand calls later) > try to convert to unicode without specifying a decoder, it uses the default > encoder, which is a application wide thing, and not a source file thing. To > see what it is on your system, use sys.getdefaultencoding(). > And this is ok. Spir said that it uses ASCII, you now say that it uses the default encoder. I think that ASCII on spir's system is the default encoder so. > The point is that there isn't just one global value, and it's a good thing. > You should figure everywhere characters come into your program (eg. source > files, raw_input, file i/o...) and everywhere characters go out of your > program, and deal with each of them individually. Ok. But it always happen this way. I hardly ever have to work with strings defined in the file. > Don't store anything internally as strings, and you won't create the > ambiguity you have with your 't' variable above. > > DaveA > Thankyou Dave Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From mjxny at hotmail.com Fri Mar 5 18:12:48 2010 From: mjxny at hotmail.com (Ming Xue) Date: Fri, 5 Mar 2010 12:12:48 -0500 Subject: [Tutor] import simplejson got Floating exception on RedHat In-Reply-To: <201003051324.16104.steve@pearwood.info> References: <4B8F0BD3.8050000@sbcglobal.net>, <201003051324.16104.steve@pearwood.info> Message-ID: Hi Tutors, I installed simplejson-2.0.9 for Python 2.5 using `setup.py install` on RedHad machine. The installation went through without any error and it created an egg (site-packages/simplejson-2.0.9-py2.5-linux-x86_64.egg). However when I tested in a python shell by import simplejson, I got Floating exception and python shell quit. Any ideas of what could be wrong? Thanks, Ming _________________________________________________________________ Hotmail: Trusted email with powerful SPAM protection. http://clk.atdmt.com/GBL/go/201469227/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dkuhlman at rexx.com Fri Mar 5 17:51:00 2010 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Fri, 5 Mar 2010 08:51:00 -0800 Subject: [Tutor] Bowing out In-Reply-To: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> References: <1c2a2c591003030517r66cf067fgf2f5a052a4a08e1b@mail.gmail.com> Message-ID: <20100305165100.GA74122@cutter.rexx.com> On Wed, Mar 03, 2010 at 08:17:45AM -0500, Kent Johnson wrote: > Hi all, > > After six years of tutor posts my interest and energy have waned and > I'm ready to move on to something new. I'm planning to stop reading > and contributing to the list. I have handed over list moderation > duties to Alan Gauld and Wesley Chun. > > Thanks to everyone who contributes questions and answers. I learned a > lot from my participation here. > > So long and keep coding! Thank you Kent, for all you've done for those who came to this list for help. I admire your ability and knowledge about Python. And, I appreciate the huge amount of effort you've put into helping us. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From rudiger.wolf at throughputfocus.com Fri Mar 5 18:56:28 2010 From: rudiger.wolf at throughputfocus.com (=?ISO-8859-1?Q?R=FCdiger=20Wolf?=) Date: Fri, 05 Mar 2010 17:56:28 +0000 Subject: [Tutor] Process list elements as consecutive pairs Message-ID: <1267811788.1212.1363321471@webmail.messagingengine.com> Hi I am trying to Process list elements as consecutive pairs into consecutive pairs. Any pythonic suggestions? listin = [1,2,3,4,5,6,7,8,9,10] I want to process as consecutive pairs 1,2 3,4 5,6 7,8 9,10 Thanks Rudiger From greg at thewhittiers.com Fri Mar 5 19:03:45 2010 From: greg at thewhittiers.com (greg whittier) Date: Fri, 5 Mar 2010 13:03:45 -0500 Subject: [Tutor] Process list elements as consecutive pairs In-Reply-To: <1267811788.1212.1363321471@webmail.messagingengine.com> References: <1267811788.1212.1363321471@webmail.messagingengine.com> Message-ID: On Fri, Mar 5, 2010 at 12:56 PM, R?diger Wolf < rudiger.wolf at throughputfocus.com> wrote: > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 > Not sure it's pythonic but zip(listin[0::2],listin[1::2]) -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Fri Mar 5 19:03:26 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 5 Mar 2010 12:03:26 -0600 Subject: [Tutor] Process list elements as consecutive pairs In-Reply-To: <1267811788.1212.1363321471@webmail.messagingengine.com> References: <1267811788.1212.1363321471@webmail.messagingengine.com> Message-ID: <333efb451003051003of9e3271xa17062eca5e646c7@mail.gmail.com> On Fri, Mar 5, 2010 at 11:56 AM, R?diger Wolf < rudiger.wolf at throughputfocus.com> wrote: > Hi > > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 > for x in xrange(0, len(listin), 2): print listin[x], listin[x+1] - that's probably how I'd do it anyway. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Fri Mar 5 19:18:32 2010 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Fri, 5 Mar 2010 13:18:32 -0500 Subject: [Tutor] Process list elements as consecutive pairs In-Reply-To: <333efb451003051003of9e3271xa17062eca5e646c7@mail.gmail.com> References: <1267811788.1212.1363321471@webmail.messagingengine.com> <333efb451003051003of9e3271xa17062eca5e646c7@mail.gmail.com> Message-ID: <652641e91003051018r34bfe87cm2473a8e2e96ac7c0@mail.gmail.com> On Fri, Mar 5, 2010 at 1:03 PM, Wayne Werner wrote: > On Fri, Mar 5, 2010 at 11:56 AM, R?diger Wolf < > rudiger.wolf at throughputfocus.com> wrote: > >> Hi >> >> I am trying to Process list elements as consecutive pairs into >> consecutive pairs. >> Any pythonic suggestions? >> >> listin = [1,2,3,4,5,6,7,8,9,10] >> I want to process as consecutive pairs >> 1,2 >> 3,4 >> 5,6 >> 7,8 >> 9,10 >> > > for x in xrange(0, len(listin), 2): > print listin[x], listin[x+1] > > - that's probably how I'd do it anyway. > > HTH, > Wayne > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Here is a general solution that I also took from the Tutor list some time ago. It allows you to have consequences of (any) length. >>> def makeVectors(length, listname): ... """takes the length of the vector and a listname returns vectors""" ... vectors = (listname[i:i+length] for i in range(len(listname)-length+1)) ... return vectors ... >>> myList = [1,2,3,4,5,6] >>> bigrams = makeVectors(2, myList) >>> bigrams at 0xb7227e64> >>> for bigram in bigrams: print bigram ... [1, 2] [2, 3] [3, 4] [4, 5] [5, 6] >>> -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Fri Mar 5 20:26:29 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 05 Mar 2010 14:26:29 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> Message-ID: <4B915AE5.2010000@ieee.org> Giorgio wrote: > 2010/3/5 Dave Angel > >> In other words, you don't understand my paragraph above. >> > > > Maybe. But please don't be angry. I'm here to learn, and as i've run into a > very difficult concept I want to fully undestand it. > > > I'm not angry, and I'm sorry if I seemed angry. Tone of voice is hard to convey in a text message. >> Once the string is stored in t as an 8 bit string, it's irrelevant what the >> source file encoding was. >> > > > Ok, you've said this 2 times, but, please, can you tell me why? I think > that's the key passage to understand how encoding of strings works. The > source file encoding affects all file lines, also strings. Nope, not strings. It only affects string literals. > If my encoding is > UTF8 python will read the string "ciao ? ciao" as 'ciao \xc3\xa8 ciao' but > if it's latin1 it will read 'ciao \xe8 ciao'. So, how can it be irrelevant? > > I think the problem is that i can't find any difference between 2 lines > quoted above: > > s = u"ciao ? ciao" > > and > > t = "ciao ? ciao" > c = unicode(t) > > [** I took the liberty of making the variable names different so I can refer to them **] > I'm still not sure whether your confusion is to what the rules are, or why the rules were made that way. The rules are that an unqualified conversion, such as the unicode() function with no second argument, uses the default encoding, in strict mode. Thus the error. Quoting the help: "If no optional parameters are given, unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if /object/ is a Unicode string or subclass it will return that Unicode string without any additional decoding applied. For objects which provide a __unicode__() <../reference/datamodel.html#object.__unicode__> method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in 'strict' mode. " As for why the rules are that, I'd have to ask you what you'd prefer. The unicode() function has no idea that t was created from a literal (and no idea what source file that literal was in), so it has to pick some coding, called the default coding. The designers decided to use a default encoding of ASCII, because manipulating ASCII strings is always safe, while many functions won't behave as expected when given UTF-8 encoded strings. For example, what's the 7th character of t ? That is not necessarily the same as the 7th character of s, since one or more of the characters in between might have taken up multiple bytes in s. That doesn't happen to be the case for your accented character, but would be for some other European symbols, and certainly for other languages as well. >> If you then (whether it's in the next line, or ten thousand calls later) >> try to convert to unicode without specifying a decoder, it uses the default >> encoder, which is a application wide thing, and not a source file thing. To >> see what it is on your system, use sys.getdefaultencoding(). >> >> > > And this is ok. Spir said that it uses ASCII, you now say that it uses the > default encoder. I think that ASCII on spir's system is the default encoder > so. > > > I don't know, but I think it's the default in every country, at least on version 2.6. It might make sense to get some value from the OS that defined the locally preferred encoding, but then a program that worked fine in one locale might fail miserably in another. >> The point is that there isn't just one global value, and it's a good thing. >> You should figure everywhere characters come into your program (eg. source >> files, raw_input, file i/o...) and everywhere characters go out of your >> program, and deal with each of them individually. >> > > > Ok. But it always happen this way. I hardly ever have to work with strings > defined in the file. > > Not sure what you mean by "the file." If you mean the source file, that's what your examples are about. If you mean a data file, that's dealt with differently. > >> Don't store anything internally as strings, and you won't create the >> ambiguity you have with your 't' variable above. >> >> DaveA >> >> > > Thankyou Dave > > Giorgio > > > > From alan.gauld at btinternet.com Fri Mar 5 20:34:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 5 Mar 2010 19:34:13 -0000 Subject: [Tutor] My tutorial References: <617634.43611.qm@web86704.mail.ird.yahoo.com> Message-ID: It's back. Enjoy Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ From hugo.yoshi at gmail.com Fri Mar 5 22:48:41 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 5 Mar 2010 22:48:41 +0100 Subject: [Tutor] Process list elements as consecutive pairs In-Reply-To: <652641e91003051018r34bfe87cm2473a8e2e96ac7c0@mail.gmail.com> References: <1267811788.1212.1363321471@webmail.messagingengine.com> <333efb451003051003of9e3271xa17062eca5e646c7@mail.gmail.com> <652641e91003051018r34bfe87cm2473a8e2e96ac7c0@mail.gmail.com> Message-ID: <29179d161003051348m666cb5a6i4ed1d2ab343c4534@mail.gmail.com> 2010/3/5 Emad Nawfal (??? ???? ???) : > > > Here is a general solution that I also took from the Tutor list some time > ago. It allows you to have consequences of (any) length. > >>>> def makeVectors(length, listname): > ... ??? """takes the length of the vector and a listname returns vectors""" > ... ??? vectors = (listname[i:i+length] for i in > range(len(listname)-length+1)) > ... ??? return vectors > ... >>>> myList = [1,2,3,4,5,6] > >>>> bigrams = makeVectors(2, myList) >>>> bigrams > at 0xb7227e64> >>>> for bigram in bigrams: print bigram > ... > [1, 2] > [2, 3] > [3, 4] > [4, 5] > [5, 6] Except the OP requested pairs (1, 2), (3, 4), i.e. with no duplicate elements. Here is a generator that does what you need: def pairs(seq): it = iter(seq) try: while True: yield it.next(), it.next() except StopIteration: return Hugo From vandyke.geospatial at gmail.com Sat Mar 6 02:38:08 2010 From: vandyke.geospatial at gmail.com (Daryl V) Date: Fri, 5 Mar 2010 17:38:08 -0800 Subject: [Tutor] Instantiating a list of strings into a list of classes Message-ID: Hello All- I cut my teeth of Fortran95, and in the distant past rode the turtle, and while I enjoy the mind-bendy feeling of shifting my programming paradigm (and LOVE LOVE LOVE Python), I get sheepish when I feel I'm missing something basic. So: I have a csv list of data, of the form: plot, utmN83_X, utmN83_Y, plot_radius_m Spring1,348545,3589235,13.2 etc. I built a nifty ClassGPSPoint(Xpos,Ypos,plotRadius,workPaths) that eats the X&Y positions, the plot radius, and previously instantiated object holding all of the appropriate paths definitions to access the LiDAR LAS files. I make a nice CSV reader with one line (did I mention I LOVE python?)... plotReader = csv.reader(open("N:\\GIS_Projects\\095_ForestMetrics\\7_Analyses\\FinalCylinders\\Plots.csv")) What I want to do is use the first entry in that row (row[0]) as the variable name for the instantiated class. I'd get an instantiated GPSPoint object called 'Spring1' that would yield all my methods and defined values (Spring1.pathLASfile, Spring1.perturbXY(), etc).... Hence, in pseudocode: for row in plotReader: row[0] = GPSPoint(row[1],row[2],18.3,workPaths) But that doesn't work. I'm probably missing something basic and profound, (I was trying to use global to track my paths until I got my mind around the passing the instantiated object thing, but whaddya gonna do? Sequential to OO is a big shift, you know?) Thanks ya'll - Daryl -------------- next part -------------- An HTML attachment was scrubbed... URL: From metolone+gmane at gmail.com Sat Mar 6 04:29:21 2010 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 5 Mar 2010 19:29:21 -0800 Subject: [Tutor] Encoding References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com><23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com><23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com><20100304080130.16c6f060@o><23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com><20100304170701.73c64f44@o><23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com><4B906EDF.8050409@ieee.org><23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com><4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> Message-ID: "Giorgio" wrote in message news:23ce85921003050915p1a084c0co73d973282d8fb6ad at mail.gmail.com... 2010/3/5 Dave Angel > I think the problem is that i can't find any difference between 2 lines > quoted above: > > a = u"ciao ? ciao" > > and > > a = "ciao ? ciao" > a = unicode(a) Maybe this will help: # coding: utf-8 a = "ciao ? ciao" b = u"ciao ? ciao".encode('latin-1') a is a UTF-8 string, due to #coding line in source. b is a latin-1 string, due to explicit encoding. a = unicode(a) b = unicode(b) Now what will happen? unicode() uses 'ascii' if not specified, because it has no idea of the encoding of a or b. Only the programmer knows. It does not use the #coding line to decide. #coding is *only* used to specify the encoding the source file is saved in, so when Python executes the script, reads the source and parses a literal Unicode string (u'...', u"...", etc.) the bytes read from the file are decoded using the #coding specified. If Python parses a byte string ('...', "...", etc.) the bytes read from the file are stored directly in the string. The coding line isn't even used. The bytes will be exactly what was saved in the file between the quotes. -Mark From transmogribenno at gmail.com Sat Mar 6 04:47:44 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Sat, 6 Mar 2010 12:47:44 +0900 Subject: [Tutor] Instantiating a list of strings into a list of classes In-Reply-To: References: Message-ID: <9b00d1a91003051947y747536bfueff9648d4d78e29a@mail.gmail.com> On 6 March 2010 10:38, Daryl V wrote: > I have a csv list of data, of the form: > plot, utmN83_X, utmN83_Y, plot_radius_m > Spring1,348545,3589235,13.2 > etc. > I built a nifty ClassGPSPoint(Xpos,Ypos,plotRadius,workPaths) that eats the > X&Y positions, the plot radius, and previously instantiated object holding > all of the appropriate paths definitions to access the LiDAR LAS files. > I make a nice CSV reader with one line (did I mention I LOVE python?)... > plotReader = > csv.reader(open("N:\\GIS_Projects\\095_ForestMetrics\\7_Analyses\\FinalCylinders\\Plots.csv")) > What I want to do is use the first entry in that row (row[0]) as the > variable name for the instantiated class. ?I'd get an instantiated GPSPoint > object called 'Spring1' that would yield all my methods and defined values > (Spring1.pathLASfile, Spring1.perturbXY(), etc).... > Hence, in pseudocode: > for row in plotReader: > ?? row[0] = GPSPoint(row[1],row[2],18.3,workPaths) All you're doing here is replacing the name stored in row[0] with the new GPSPoint object. If I read your description correctly, I think what you're looking for should behave like this: # row read from CSV (I assume CSV values are always read as strings) row = ['Spring1', '348545', '3589235', '13.2'] # what the code you want should do automatically Spring1 = GPSPoint('348545','3589235','13.2', workPaths) I don't know how to do that in Python - at a guess it would look something like (pseudocode): __module__.vars[row[0]] = GPSPoint(...) Someone else probably knows how to do that, but I have a fairly strong feeling that it's the wrong approach to your problem. When you write your CSV file, each GPSPoint needs to know what its variable name is, which doesn't seem to make a whole lot of sense. If you have a set number of objects, then maybe you should just read and write them in a known order - your program can call them anything it likes; it won't affect the storage and loading of the data. Alternatively, if for some reason you really want to associate a name with each GPSPoint, I would suggest storing the points in a dictionary, e.g. points = {} for row in plotReader: points[row[0]] = GPSPoint(row[1], row[2], row[3], workPaths) Then when you need to write to a CSV, the name to go in the first column is simply the key of that dictionary item. HTH, benno From gilcosson_2000 at yahoo.com Sat Mar 6 06:13:49 2010 From: gilcosson_2000 at yahoo.com (Gil Cosson) Date: Fri, 5 Mar 2010 21:13:49 -0800 (PST) Subject: [Tutor] Really miss the C preprocessor!! Message-ID: <506328.3466.qm@web35603.mail.mud.yahoo.com> I have some code to perform an exponential backoff. I am interacting with a few different web services. Checking for a specific Exception and then re-invoking the failed call after waiting a bit. I don't want to? code the backoff in ten different places. Really miss the C pre-processor. I am tempted to try something like the following, but I understand that eval should be a last resort. Any pythonic best practice for this situation?: from? time import sleep from random import randint def exponentialbackoff(exception,numretrys,somecode): ??? currentIteration=0 ??? SuccessfulCall=False ??? rc=None ??? globalnamespace=getglobals(,globals) ??? while currentiteration From steve at pearwood.info Sat Mar 6 07:01:38 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 6 Mar 2010 17:01:38 +1100 Subject: [Tutor] Really miss the C preprocessor!! In-Reply-To: <506328.3466.qm@web35603.mail.mud.yahoo.com> References: <506328.3466.qm@web35603.mail.mud.yahoo.com> Message-ID: <201003061701.39039.steve@pearwood.info> On Sat, 6 Mar 2010 04:13:49 pm Gil Cosson wrote: > I have some code to perform an exponential backoff. I am interacting > with a few different web services. Checking for a specific Exception > and then re-invoking the failed call after waiting a bit. I don't > want to? code the backoff in ten different places. Really miss the C > pre-processor. > > I am tempted to try something like the following, but I understand > that eval should be a last resort. > > Any pythonic best practice for this situation?: Functions are first-class objects that can be passed around as data. Use them. def check_some_website(url, x): # whatever def exponential_backoff(function, args, exception, numretries): # Backoff 1 second, 2 seconds, 4 seconds, 8, 16, ... t = 1 for i in xrange(numretries): try: return function(*args) except exception: time.sleep(t) t *= 2 raise exception("no connection after %d attempts" % numretries) result = exponential_backoff(check_some_website, ("http://example.com", 42), HTTPError, 8) Any time you think you need eval, you almost certainly don't. -- Steven D'Aprano From steve at pearwood.info Sat Mar 6 14:51:30 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 7 Mar 2010 00:51:30 +1100 Subject: [Tutor] Really miss the C preprocessor!! In-Reply-To: <4B920FA7.4060502@lonetwin.net> References: <506328.3466.qm@web35603.mail.mud.yahoo.com> <201003061701.39039.steve@pearwood.info> <4B920FA7.4060502@lonetwin.net> Message-ID: <201003070051.31834.steve@pearwood.info> On Sat, 6 Mar 2010 07:17:43 pm you wrote: > I thought about suggesting using decorators for this, since I've done > something similar (not exactly exponential backoff, but retrying a > few times on exception). However, as I started writing the example, I > got stuck at expressing a generic way to pass the exception and > numretries. I was just wondering is there some way to do this sort of > thing with decorators ?? [...] > is there a way to decorate a function alongwith additional parameters > which will be passed to the function ? Yes, you need a decorator factory -- a function which returns a decorator. def factory(exception, numretries): def decorator(func): @functools.wraps(func) def inner(*args, **kwargs): t = 1 for i in range(numretries): try: return func(*args, **kwargs) except exception: time.sleep(t) t *= 2 msg = "no connection after %d attempts" % numretries raise exception(msg) return inner return decorator @factory(HTTPError, 8) def check_some_website(url, x): ... I haven't tested the above code, but it should do the trick. -- Steven D'Aprano From denis.spir at gmail.com Sat Mar 6 17:47:20 2010 From: denis.spir at gmail.com (spir) Date: Sat, 6 Mar 2010 17:47:20 +0100 Subject: [Tutor] Instantiating a list of strings into a list of classes In-Reply-To: References: Message-ID: <20100306174720.55dbd2ed@o> On Fri, 5 Mar 2010 17:38:08 -0800 Daryl V wrote: > I have a csv list of data, of the form: > plot, utmN83_X, utmN83_Y, plot_radius_m > Spring1,348545,3589235,13.2 > etc. [...] > What I want to do is use the first entry in that row (row[0]) as the > variable name for the instantiated class. There are several solution, for design & implementation. (1) You can do some trick to create vars as you explain. But it's ugly and in my opinion wrong: because the set of data build a kind of whole, thus should have global name, say "data". (2) Build a dictionary which keys are the names: name = row[0] data[name] = value ... v = data[name] This is much better because its standard programming and the data are properly packed. Right? (3) Build a custom composite object which attributes are named the way you want. Python does not have a syntax to set attributes with variable names, but provides a builtin func for this: name = row[0] setattr(data, name, value) ... v = data.name It may look a bit contorsed, but again it's because python does not have a syntax for this. I would go for the latter -- it may be a question of style. Denis -- ________________________________ la vita e estrany spir.wikidot.com From anothernetfellow at gmail.com Sat Mar 6 18:27:41 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Sat, 6 Mar 2010 18:27:41 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B915AE5.2010000@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> Message-ID: <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> 2010/3/5 Dave Angel I'm not angry, and I'm sorry if I seemed angry. Tone of voice is hard to > convey in a text message. Ok, sorry. I've misunderstood your mail :D > I'm still not sure whether your confusion is to what the rules are, or why > the rules were made that way. WHY the rules are made that way. But now it's clear. 2010/3/6 Mark Tolonen > > > Maybe this will help: > > # coding: utf-8 > > a = "ciao ? ciao" > b = u"ciao ? ciao".encode('latin-1') > > a is a UTF-8 string, due to #coding line in source. > b is a latin-1 string, due to explicit encoding. > Oh, right. And, if i'm not wrong B is an UTF8 string decoded to unicode (due to the coding: statement at the top of the file) and re-encoded to latin1 > -Mark Thankyou again Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Sat Mar 6 18:58:21 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 6 Mar 2010 18:58:21 +0100 Subject: [Tutor] Process list elements as consecutive pairs In-Reply-To: <29179d161003051348m666cb5a6i4ed1d2ab343c4534@mail.gmail.com> References: <1267811788.1212.1363321471@webmail.messagingengine.com> <333efb451003051003of9e3271xa17062eca5e646c7@mail.gmail.com> <652641e91003051018r34bfe87cm2473a8e2e96ac7c0@mail.gmail.com> <29179d161003051348m666cb5a6i4ed1d2ab343c4534@mail.gmail.com> Message-ID: <29179d161003060958v44c2275akfe90f501cf1bd949@mail.gmail.com> On Fri, Mar 5, 2010 at 10:48 PM, Hugo Arts wrote: > > Except the OP requested pairs (1, 2), (3, 4), i.e. with no duplicate > elements. Here is a generator that does what you need: > > def pairs(seq): > ? ?it = iter(seq) > ? ?try: > ? ? ? ?while True: > ? ? ? ? ? ?yield it.next(), it.next() > ? ?except StopIteration: > ? ? ? ?return > > Hugo > Just Noticed this tiny caveat: If the length of the sequence is uneven, the last element will not be yielded by this generator. Whether that's a problem, and if it is, how to handle it, depends on the application you're working with. I did make a tiny modification to yield the last element in a pair with None as the second value. The benefit is that it doesn't skip any values, but you'll need to handle the possible None value in your code. Of course, if you want a generator that yields consecutive pairs, passing it a sequence of uneven length is sort of problematic to begin with. So for most applications the simple version should be fine. def pairs(seq): it = iter(seq) try: while True: a = it.next() b = it.next() yield a, b except StopIteration: if len(seq) % 2: yield a, None It's not as pretty as the simple version, unfortunately. in my experience, corner cases are rarely handleable by elegant code :( Compare output of the first and second version: >>> # first (elegant) version of pairs >>> list(pairs(range(5))) [(0, 1), (2, 3)] >>> # second version >>> list(pairs_2(range(5))) [(0, 1), (2, 3), (4, None)] From alan.gauld at btinternet.com Sun Mar 7 00:57:02 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 6 Mar 2010 23:57:02 -0000 Subject: [Tutor] Instantiating a list of strings into a list of classes References: Message-ID: "Daryl V" wrote > What I want to do is use the first entry in that row (row[0]) as the > variable name for the instantiated class. Thats usually a very bad idea. Not least because all the code that comes after it would somehow, magically, have to know about this brand new variable that has appeared. Its generally better to store these kinds of things in a list or dictionary. You can use the name as the key into the dictionary if you like. Then you can process the dictionary like any other collection of objects. Or you can refer to specific ones using the name. And the previously written code doen't get confused aboutt unexpected names appearing. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Sun Mar 7 08:48:37 2010 From: __peter__ at web.de (Peter Otten) Date: Sun, 07 Mar 2010 08:48:37 +0100 Subject: [Tutor] Process list elements as consecutive pairs References: <1267811788.1212.1363321471@webmail.messagingengine.com> Message-ID: R?diger Wolf wrote: > I am trying to Process list elements as consecutive pairs into > consecutive pairs. > Any pythonic suggestions? > > listin = [1,2,3,4,5,6,7,8,9,10] > I want to process as consecutive pairs > 1,2 > 3,4 > 5,6 > 7,8 > 9,10 >>> listin = [1,2,3,4,5,6,7,8,9,10] >>> it = iter(listin) >>> zip(it, it) [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)] If listin as an odd length the last item will be lost. Peter From anothernetfellow at gmail.com Sun Mar 7 13:23:12 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Sun, 7 Mar 2010 13:23:12 +0100 Subject: [Tutor] Encoding In-Reply-To: <20100307082837.54fc6389@o> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> Message-ID: <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> 2010/3/7 spir > > > Oh, right. And, if i'm not wrong B is an UTF8 string decoded to unicode > (due > > to the coding: statement at the top of the file) and re-encoded to latin1 > > Si! :-) > Ahah. Ok, Grazie! One more question: Amazon SimpleDB only accepts UTF8. So, let's say i have to put into an image file: filestream = file.read() filetoput = filestream.encode('utf-8') Do you think this is ok? Oh, of course everything url-encoded then Giorgio > > Denis > -- > ________________________________ > > la vita e estrany > > spir.wikidot.com > > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Sun Mar 7 13:58:05 2010 From: denis.spir at gmail.com (spir) Date: Sun, 7 Mar 2010 13:58:05 +0100 Subject: [Tutor] recursive generator Message-ID: <20100307135805.6d472e87@o> Hello, Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. def __iter__(self): ''' Iteration on (key,value) pairs. ''' print '*', if self.holdsEntry: yield (self.key,self.value) for child in self.children: print "<", child.__iter__() print ">", raise StopIteration With the debug prints in code above, "for e in t: print e" outputs: * ('', 0) < > < > < > < > < > < > < > < > print len(t.children) # --> 9 Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code. Denis -- ________________________________ la vita e estrany spir.wikidot.com From steve at pearwood.info Sun Mar 7 14:27:40 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 8 Mar 2010 00:27:40 +1100 Subject: [Tutor] recursive generator In-Reply-To: <20100307135805.6d472e87@o> References: <20100307135805.6d472e87@o> Message-ID: <201003080027.40626.steve@pearwood.info> On Sun, 7 Mar 2010 11:58:05 pm spir wrote: > Hello, > > Is it possible at all to have a recursive generator? Yes. >>> def recursive_generator(n): ... if n == 0: ... return ... yield n ... for i in recursive_generator(n-1): ... yield i ... >>> it = recursive_generator(5) >>> it.next() 5 >>> it.next() 4 >>> it.next() 3 >>> it.next() 2 >>> it.next() 1 >>> it.next() Traceback (most recent call last): File "", line 1, in StopIteration > I think at a > iterator for a recursive data structure (here, a trie). The following > code does not work: it only yields a single value. Like if > child.__iter__() were never called. > > def __iter__(self): > ''' Iteration on (key,value) pairs. ''' > print '*', > if self.holdsEntry: > yield (self.key,self.value) > for child in self.children: > print "<", > child.__iter__() > print ">", > raise StopIteration __iter__ should be an ordinary function, not a generator. Something like this should work: # Untested. def __iter__(self): ''' Iteration on (key,value) pairs. ''' def inner(): print '*', # Side effects bad... if self.holdsEntry: yield (self.key,self.value) for child in self.children: print "<", child.__iter__() print ">", raise StopIteration return inner() This means that your class won't *itself* be an iterator, but calling iter() on it will return a generator object, which of course is an iterator. If you want to make your class an iterator itself, then you need to follow the iterator protocol. __iter__ must return the instance itself, and next must return (not yield) the next iteration. class MyIterator(object): def __init__(self, n): self.value = n def next(self): if self.value == 0: raise StopIteration self.value //= 2 return self.value def __iter__(self): return self See the discussion in the docs about the iterator protocol: http://docs.python.org/library/stdtypes.html#iterator-types > Why is no child.__iter__() executed at all? I imagine this can be > caused by the special feature of a generator recording current > execution point. That's exactly what generators do: when they reach a yield, execution is halted, but the state of the generator is remembered. Then when you call the next() method, execution resumes. > (But then, is it at all possible to have a call in a > generator? Or does the issue only appear whan the callee is a > generator itself?) Else, the must be an obvious error in my code. I don't understand what you mean. -- Steven D'Aprano From denis.spir at gmail.com Sun Mar 7 14:29:11 2010 From: denis.spir at gmail.com (spir) Date: Sun, 7 Mar 2010 14:29:11 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> Message-ID: <20100307142911.12d29e10@o> On Sun, 7 Mar 2010 13:23:12 +0100 Giorgio wrote: > One more question: Amazon SimpleDB only accepts UTF8. [...] > filestream = file.read() > filetoput = filestream.encode('utf-8') No! What is the content of the file? Do you think it can be a pure python representation of a unicode text? uContent = inFile.read().decode(***format***) outFile.write(uContent.encode('utf-8')) input -->decode--> process -->encode--> output This gives me an idea: when working with unicode, it would be cool to have an optional format parameter for file.read() and write. So, the above would be: uContent = inFile.read(***format***) outFile.write(uContent, 'utf-8') Or, maybe even better, the format could be given as third parameter of file open(); then any read or write operation would directly convert from/to the said format. What do you all think? denis -- ________________________________ la vita e estrany spir.wikidot.com From python at bdurham.com Sun Mar 7 15:33:32 2010 From: python at bdurham.com (python at bdurham.com) Date: Sun, 07 Mar 2010 09:33:32 -0500 Subject: [Tutor] Encoding In-Reply-To: <20100307142911.12d29e10@o> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com><20100304170701.73c64f44@o><23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com><4B906EDF.8050409@ieee.org><23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com><4B913530.6030003@ieee.org><23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com><4B915AE5.2010000@ieee.org><23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com><20100307082837.54fc6389@o><23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> <20100307142911.12d29e10@o> Message-ID: <1267972412.20807.1363535215@webmail.messagingengine.com> > Or, maybe even better, the format could be given as third parameter of file open(); then any read or write operation would directly convert from/to the said format. What do you all think? See the codecs.open() command as an alternative to open(). With all the hassles of encoding, I'm puzzled why anyone would use the regular open() for anything but binary operations. Malcolm ----- Original message ----- From: "spir" To: "Python tutor" Date: Sun, 7 Mar 2010 14:29:11 +0100 Subject: Re: [Tutor] Encoding On Sun, 7 Mar 2010 13:23:12 +0100 Giorgio wrote: > One more question: Amazon SimpleDB only accepts UTF8. [...] > filestream = file.read() > filetoput = filestream.encode('utf-8') No! What is the content of the file? Do you think it can be a pure python representation of a unicode text? uContent = inFile.read().decode(***format***) outFile.write(uContent.encode('utf-8')) input -->decode--> process -->encode--> output This gives me an idea: when working with unicode, it would be cool to have an optional format parameter for file.read() and write. So, the above would be: uContent = inFile.read(***format***) outFile.write(uContent, 'utf-8') Or, maybe even better, the format could be given as third parameter of file open(); then any read or write operation would directly convert from/to the said format. What do you all think? denis -- ________________________________ la vita e estrany spir.wikidot.com _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From stefan_ml at behnel.de Sun Mar 7 15:49:21 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 07 Mar 2010 15:49:21 +0100 Subject: [Tutor] recursive generator In-Reply-To: <201003080027.40626.steve@pearwood.info> References: <20100307135805.6d472e87@o> <201003080027.40626.steve@pearwood.info> Message-ID: Steven D'Aprano, 07.03.2010 14:27: > On Sun, 7 Mar 2010 11:58:05 pm spir wrote: >> def __iter__(self): >> ''' Iteration on (key,value) pairs. ''' >> print '*', >> if self.holdsEntry: >> yield (self.key,self.value) >> for child in self.children: >> print "<", >> child.__iter__() >> print ">", >> raise StopIteration > > > __iter__ should be an ordinary function, not a generator. Something like > this should work: > > # Untested. > def __iter__(self): > ''' Iteration on (key,value) pairs. ''' > def inner(): > print '*', # Side effects bad... > if self.holdsEntry: > yield (self.key,self.value) > for child in self.children: > print "<", > child.__iter__() > print ">", > raise StopIteration > return inner() That's just an unnecessarily redundant variation on the above. It's perfectly ok if __iter__() is a generator method. Stefan From denis.spir at gmail.com Sun Mar 7 16:05:41 2010 From: denis.spir at gmail.com (spir) Date: Sun, 7 Mar 2010 16:05:41 +0100 Subject: [Tutor] __iter__: one obvious way to do it Message-ID: <20100307160541.42228e14@o> Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __iter__ returns self, its own iterator via next() 5. __iter__ returns an external iterator object 6. __iter__ returns iter() of a collection built just on time (this one is really contrived) Also, one can always traverse the collection (already existing or built then) itself if it not quasi-infinite (no __iter__ at all). "There should be one-- and preferably only one --obvious way to do it" http://www.python.org/dev/peps/pep-0020/ I understand some ways fit given use cases better -- or worse. But it's difficult to find the best one, or even a proper one in a given case. Also, generation and iteration are rather abstract notions. And it's too much variety for me. I am lost in this field. I would enjoy a commented and examplified overview. Denis -- ________________________________ la vita e estrany spir.wikidot.com From denis.spir at gmail.com Sun Mar 7 16:07:41 2010 From: denis.spir at gmail.com (spir) Date: Sun, 7 Mar 2010 16:07:41 +0100 Subject: [Tutor] __iter__: one obvious way to do it Message-ID: <20100307160741.38acee57@o> [sorry, forgot the code] Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __iter__ returns self, its own iterator via next() 5. __iter__ returns an external iterator object 6. __iter__ returns iter() of a collection built just on time (this one is really contrived) Also, one can always traverse the collection (already existing or built then) itself if it not quasi-infinite (no __iter__ at all). "There should be one-- and preferably only one --obvious way to do it" http://www.python.org/dev/peps/pep-0020/ I understand some ways fit given use cases better -- or worse. But it's difficult to find the best one, or even a proper one in a given case. Also, generation and iteration are rather abstract notions. And it's too much variety for me. I am lost in this field. I would enjoy a commented and examplified overview. Denis ----- class Seq(object): def __init__(self, *items): self.items = list(items) def next(self): if self.i < len(self.items): x = pair(self.items[self.i]) self.i += 1 return x raise StopIteration def pairs(l): for n in l.items: yield pair(n) raise StopIteration def __iter__(self): return (pair(n) for n in self.items) def __iter__(self): for n in self.items: yield pair(n) raise StopIteration def __iter__(self): return self.pairs() def __iter__(self): self.i=0 return self def __iter__(self): return Iter(self) def __iter__(self): return iter(tuple([pair(n) for n in self.items])) def pair(n): return "%s:%s" %(n,chr(n+96)) class Iter(object): def __init__(self, l): self.i=0 self.items = l.items def __iter__(self): return self def next(self): if self.i < len(self.items): x = pair(self.items[self.i]) self.i += 1 return x raise StopIteration l = Seq(1,2,3) for x in l: print x, # ==> 1:a 2:b 3:c ________________________________ la vita e estrany spir.wikidot.com From steve at pearwood.info Sun Mar 7 16:54:48 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 8 Mar 2010 02:54:48 +1100 Subject: [Tutor] recursive generator In-Reply-To: References: <20100307135805.6d472e87@o> <201003080027.40626.steve@pearwood.info> Message-ID: <201003080254.49103.steve@pearwood.info> On Mon, 8 Mar 2010 01:49:21 am Stefan Behnel wrote: > Steven D'Aprano, 07.03.2010 14:27: > > __iter__ should be an ordinary function, not a generator. Something > > like this should work: [...] > That's just an unnecessarily redundant variation on the above. It's > perfectly ok if __iter__() is a generator method. So it is. Thank you for the correction. -- Steven D'Aprano From hugo.yoshi at gmail.com Sun Mar 7 17:21:11 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 7 Mar 2010 17:21:11 +0100 Subject: [Tutor] recursive generator In-Reply-To: <29179d161003070820i69ffcd37l68ef657cd34b73d7@mail.gmail.com> References: <20100307135805.6d472e87@o> <29179d161003070820i69ffcd37l68ef657cd34b73d7@mail.gmail.com> Message-ID: <29179d161003070821l42de8bach4825d095ecf8af70@mail.gmail.com> sorry, forgot to forward this to the list. On Sun, Mar 7, 2010 at 1:58 PM, spir wrote: > Hello, > > Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. > > ? ?def __iter__(self): > ? ? ? ?''' Iteration on (key,value) pairs. ''' > ? ? ? ?print '*', > ? ? ? ?if self.holdsEntry: > ? ? ? ? ? ?yield (self.key,self.value) > ? ? ? ?for child in self.children: > ? ? ? ? ? ?print "<", > ? ? ? ? ? ?child.__iter__() > ? ? ? ? ? ?print ">", > ? ? ? ?raise StopIteration > > With the debug prints in code above, "for e in t: print e" outputs: > > * ('', 0) > < > < > < > < > < > < > < > < > > > print len(t.children) # --> 9 > > Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code. > > > Denis remember that child.__iter__ returns a generator object. Merely calling the function won't do anything. To actually get elements from a generator object, you need to call next() on the returned iterator, or iterate over it in another way (e.g. a for loop). I would write this method more or less like so: from itertools import chain def __iter__(self): ? ?this_entry = [(self.key, self.value)] is self.holds_entry else [] ? ?return chain(this_entry, *[iter(c) for c in self.children]) (Warning! untested! I'm pretty sure chain will work like this, but you might have to do a little tweaking) From cfuller084 at thinkingplanet.net Sun Mar 7 17:10:12 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Sun, 7 Mar 2010 10:10:12 -0600 Subject: [Tutor] recursive generator In-Reply-To: <20100307135805.6d472e87@o> References: <20100307135805.6d472e87@o> Message-ID: <201003071010.12830.cfuller084@thinkingplanet.net> Here's a (more or less) useful example. It generates the "nth triangular series" (my term), n=2 is the triangular numbers, n=3 the tetrahedral numbers (n has a correspondence to dimension). Each series is the sum of the elements of the previous one. They are also the columns of Pascal's Triangle. Probably only "useful" if you enjoy playing around with math. def triangular(n): if n == 0: while True: yield 1 else: g = triangular(n-1) a = 0 while True: a += g.next() yield a I wouldn't use that to build Pascal's triangle, by the way (usually you want it rowwise). Here's my code. def pascal(): r = 0 l = [1] yield l while True: l = [1] + [ l[i] + l[i+1] for i in range(r) ] + [1] r += 1 yield l >>> g=pascal() >>> for i in range(9): ... print ' '.join(['%2d'%(i,) for i in g.next()]) ... 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 Here are the triangular-ly generated numbers (I made them a square, just to illustrate an interesting property.) >>> for i in range(7): ... g=triangular(i) ... print ' '.join(['%3d'%(j,) for j in [g.next() for k in range(7)]]) ... 1 1 1 1 1 1 1 1 2 3 4 5 6 7 1 3 6 10 15 21 28 1 4 10 20 35 56 84 1 5 15 35 70 126 210 1 6 21 56 126 252 462 1 7 28 84 210 462 924 Cheers On Sunday 07 March 2010, spir wrote: > Hello, > > Is it possible at all to have a recursive generator? I think at a iterator > for a recursive data structure (here, a trie). The following code does not > work: it only yields a single value. Like if child.__iter__() were never > called. > > def __iter__(self): > ''' Iteration on (key,value) pairs. ''' > print '*', > if self.holdsEntry: > yield (self.key,self.value) > for child in self.children: > print "<", > child.__iter__() > print ">", > raise StopIteration > > With the debug prints in code above, "for e in t: print e" outputs: > > * ('', 0) > < > < > < > < > < > < > < > < > > > print len(t.children) # --> 9 > > Why is no child.__iter__() executed at all? I imagine this can be caused by > the special feature of a generator recording current execution point. (But > then, is it at all possible to have a call in a generator? Or does the > issue only appear whan the callee is a generator itself?) Else, the must > be an obvious error in my code. > > > Denis > From steve at pearwood.info Sun Mar 7 17:37:35 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 8 Mar 2010 03:37:35 +1100 Subject: [Tutor] __iter__: one obvious way to do it In-Reply-To: <20100307160741.38acee57@o> References: <20100307160741.38acee57@o> Message-ID: <201003080337.36025.steve@pearwood.info> On Mon, 8 Mar 2010 02:07:41 am spir wrote: > [sorry, forgot the code] > > Hello, > > Below 6 working way to implement __iter__ for a container here > simulated with a plain inner list. Sure, the example is a bit > artificial ;-) > 1. __iter__ returns a generator _expression_ > def __iter__(self): > return (pair(n) for n in self.items) Seems perfectly acceptable to me. That's syntactic sugar for the next one: > 2. __iter__ *is* a generator > def __iter__(self): > for n in self.items: > yield pair(n) > raise StopIteration As Stefan pointed out, __iter__ can be a generator, so that's okay too. However, the StopIteration at the end is redundant: generators automatically raise StopIteration when they "fall off the end" at the end of the code. So this is equivalent to the above: def __iter__(self): for n in self.items: yield pair(n) > 3. __iter__ returns a generator > (this one is a bit weird, i guess) > def __iter__(self): > return self.pairs() There's nothing weird about it. It's the difference between writing code directly inline, and calling it indirectly: def f(): return [1, 2, 3] versus: def indirect(): return [1, 2, 3] def f(): return indirect() If you have a good reason for the indirection, it is perfectly fine. E.g. you might have a class with multiple iterators: class X: def iter_width(self): """Iterate left-to-right""" pass def iter_depth(self): """Iterate top-to-bottom""" pass def iter_spiral(self): pass def __iter__(self): # Default. return self.iter_width() > 4. __iter__ returns self, its own iterator via next() > def __iter__(self): > self.i=0 > return self That's how you would make the class an iterator directly. > 5. __iter__ returns an external iterator object > def __iter__(self): > return Iter(self) Built-ins such as lists, dicts, tuples and sets use that strategy: >>> iter([1,2,3]) >>> iter(dict(a=1,b=2)) > 6. __iter__ returns iter() of a collection built just on time > (this one is really contrived) > def __iter__(self): > return iter(tuple([pair(n) for n in self.items])) Not contrived, but inefficient. First you build a list, all at once, using a list comprehension. So much for lazy iteration, but sometimes you have good reason for this (see below). Then you copy everything in the list into a tuple. Why? Then you create an iterator from the tuple. If you remove the intermediate tuple, it is a reasonable approach for ensuring that you can modify the original object without changing any iterators made from it. In other words, __iter__ returns a *copy* of the data in self. But the easiest way to do that: def __iter__(self): return iter([pair(n) for n in self.items]) No need to make a tuple first. > Also, one can always traverse the collection (already existing or > built then) itself if it not quasi-infinite (no __iter__ at all). The point of __iter__ is to have a standard way to traverse data structures, so you can traverse them with for-loops. Otherwise, every data structure needs a different method: for item in tree.traverse(): for item in mapping.next_key(): for item in sequence.get_next_item(): for item in obj.walker(): > "There should be one-- and preferably only one --obvious way to do > it" http://www.python.org/dev/peps/pep-0020/ This doesn't mean that there should be *only* one way to do something. It means that the should be one OBVIOUS way to do it. -- Steven D'Aprano From roadierich at googlemail.com Sun Mar 7 17:46:28 2010 From: roadierich at googlemail.com (Rich Lovely) Date: Sun, 7 Mar 2010 16:46:28 +0000 Subject: [Tutor] recursive generator In-Reply-To: <20100307135805.6d472e87@o> References: <20100307135805.6d472e87@o> Message-ID: On 7 March 2010 12:58, spir wrote: > Hello, > > Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. > > ? ?def __iter__(self): > ? ? ? ?''' Iteration on (key,value) pairs. ''' > ? ? ? ?print '*', > ? ? ? ?if self.holdsEntry: > ? ? ? ? ? ?yield (self.key,self.value) > ? ? ? ?for child in self.children: > ? ? ? ? ? ?print "<", > ? ? ? ? ? ?child.__iter__() > ? ? ? ? ? ?print ">", > ? ? ? ?raise StopIteration > > With the debug prints in code above, "for e in t: print e" outputs: > > * ('', 0) > < > < > < > < > < > < > < > < > > > print len(t.children) # --> 9 > > Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code. > > > Denis > -- > ________________________________ > > la vita e estrany > > spir.wikidot.com > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > You are calling child.__iter__(), but it's return value is being thrown away. What you want to be doing is something like def __iter__(self): if self.holdsEntry: yield self.entry for child in self.children: print "<" for val in child: #implicit call to child.__iter__() yield val print ">" Then, when the child.__iter__() is called, the returned iterator is iterated, and the values are passed up the call stack. There's probably a more terse way of doing this using itertools, but I think this is probably more readable. Hope this clears things up (a little, anyway...) -- Rich "Roadie Rich" Lovely Just because you CAN do something, doesn't necessarily mean you SHOULD. In fact, more often than not, you probably SHOULDN'T. Especially if I suggested it. 10 re-discover BASIC 20 ??? 30 PRINT "Profit" 40 GOTO 10 From hugo.yoshi at gmail.com Sun Mar 7 17:46:43 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 7 Mar 2010 17:46:43 +0100 Subject: [Tutor] __iter__: one obvious way to do it In-Reply-To: <201003080337.36025.steve@pearwood.info> References: <20100307160741.38acee57@o> <201003080337.36025.steve@pearwood.info> Message-ID: <29179d161003070846k11cb0723xad0b96781e6ad4f@mail.gmail.com> On Sun, Mar 7, 2010 at 5:37 PM, Steven D'Aprano wrote: >> "There should be one-- and preferably only one --obvious way to do >> it" http://www.python.org/dev/peps/pep-0020/ > > This doesn't mean that there should be *only* one way to do something. > It means that the should be one OBVIOUS way to do it. > And in this case, if you consider "the thing to do" to be traversal of some collection, the one obvious way to do it is through iteration. So, the iterator protocol is a perfect example of this part of the zen; the flexibility you have in implementing iteration is necessary to allow it to be the one way. Hugo From denis.spir at gmail.com Sun Mar 7 19:43:13 2010 From: denis.spir at gmail.com (spir) Date: Sun, 7 Mar 2010 19:43:13 +0100 Subject: [Tutor] recursive generator In-Reply-To: <29179d161003070820i69ffcd37l68ef657cd34b73d7@mail.gmail.com> References: <20100307135805.6d472e87@o> <29179d161003070820i69ffcd37l68ef657cd34b73d7@mail.gmail.com> Message-ID: <20100307194313.1a726821@o> On Sun, 7 Mar 2010 17:20:07 +0100 Hugo Arts wrote: > On Sun, Mar 7, 2010 at 1:58 PM, spir wrote: > > Hello, > > > > Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. > > > > ? ?def __iter__(self): > > ? ? ? ?''' Iteration on (key,value) pairs. ''' > > ? ? ? ?print '*', > > ? ? ? ?if self.holdsEntry: > > ? ? ? ? ? ?yield (self.key,self.value) > > ? ? ? ?for child in self.children: > > ? ? ? ? ? ?print "<", > > ? ? ? ? ? ?child.__iter__() > > ? ? ? ? ? ?print ">", > > ? ? ? ?raise StopIteration > > > > With the debug prints in code above, "for e in t: print e" outputs: > > > > * ('', 0) > > < > < > < > < > < > < > < > < > > > > > print len(t.children) # --> 9 > > > > Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code. > > > > > > Denis > > remember that child.__iter__ returns a generator object. Merely > calling the function won't do anything. To actually get elements from > a generator object, you need to call next() on the returned iterator, > or iterate over it in another way (e.g. a for loop). I would write > this method more or less like so: > > from itertools import chain > > def __iter__(self): > this_entry = [(self.key, self.value)] is self.holds_entry else [] > return chain(this_entry, *[iter(c) for c in self.children]) > > (Warning! untested! I'm pretty sure chain will work like this, but you > might have to do a little tweaking) @ Hugo & Steven Thank you very much. I'll study your proposals as soon as I can, then tell you about the results. In the meanwhile, I (recursively) constructed the collection of entries and returned iter(collection). [This works, indeed, but I also want do know how to properly build a recursive iterator.] Denis -- ________________________________ la vita e estrany spir.wikidot.com From davea at ieee.org Sun Mar 7 20:37:30 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 07 Mar 2010 14:37:30 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> Message-ID: <4B94007A.1060809@ieee.org> Giorgio wrote: > 2010/3/7 spir > > > One more question: Amazon SimpleDB only accepts UTF8. > > So, let's say i have to put into an image file: > > Do you mean a binary file with image data, such as a jpeg? In that case, an emphatic - NO. not even close. > filestream = file.read() > filetoput = filestream.encode('utf-8') > > Do you think this is ok? > > Oh, of course everything url-encoded then > > Giorgio > > > Encoding binary data with utf-8 wouldn't make any sense, even if you did have the right semantics for a text file. Next problem, 'file' is a built-in keyword. So if you write what you describe, you're trying to call a non-static function with a class object, which will error. Those two lines don't make any sense by themselves. Show us some context, and we can more sensibly comment on them. And try not to use names that hide built-in keywords, or Python stdlib names. If you're trying to store binary data in a repository that only permits text, it's not enough to pretend to convert it to UTF-8. You need to do some other escaping, such as UUENCODE, that transforms the binary data into something resembling text. Then you may or may not need to encode that text with utf-8, depending on its character set. DaveA From anothernetfellow at gmail.com Sun Mar 7 21:13:12 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Sun, 7 Mar 2010 21:13:12 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B94007A.1060809@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> <4B94007A.1060809@ieee.org> Message-ID: <23ce85921003071213q520c15c8w6ce7a09d4a55631d@mail.gmail.com> 2010/3/7 Dave Angel > > Those two lines don't make any sense by themselves. Show us some context, > and we can more sensibly comment on them. And try not to use names that > hide built-in keywords, or Python stdlib names. > Hi Dave, I'm considering Amazon SimpleDB as an alternative to PGSQL, but i need to store blobs. Amazon's FAQs says that: "Q: What kind of data can I store? You can store any UTF-8 string data in Amazon SimpleDB. Please refer to the Amazon Web Services Customer Agreement for details." This is the problem. Any idea? > DaveA > Giorgio -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From pylist1 at gmail.com Sun Mar 7 22:09:41 2010 From: pylist1 at gmail.com (hithere there) Date: Sun, 7 Mar 2010 16:09:41 -0500 Subject: [Tutor] pgdb and console output Message-ID: <4f23445b1003071309p73c2a217p4d3c3a9653459d6a@mail.gmail.com> OS = RHEL 5.4 32Bit Python = 2.4.3 #! /usr/bin/env python import pgdb, sys db = pgdb.connect (dsn='192.168.0.1:familydata', user='postgres', password='') cursor = db.cursor () cursor.execute ("select * from names") rows = cursor.fetchall () for i in (rows): print i #viewtable (db) #sys.stdout.write() cursor.close () ###########END######## The code as is will display the data to the console. I have read the db API 2.0 at python.org. The problem is viewtable (db) will not work or sys.stdout.write () to write the table data to the console screen. What am I doing wrong here or what do I need to do different? Can anyone point me to a book specificly for pgdb, python and postgre? Last thing is can I define the classes in a separate files and reference them? Kinda like code reuse in .Net? I new to Python so bear with me. From alan.gauld at btinternet.com Mon Mar 8 02:24:26 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 8 Mar 2010 01:24:26 -0000 Subject: [Tutor] pgdb and console output References: <4f23445b1003071309p73c2a217p4d3c3a9653459d6a@mail.gmail.com> Message-ID: "hithere there" wrote > #! /usr/bin/env python > > import pgdb, sys > > db = pgdb.connect (dsn='192.168.0.1:familydata', > user='postgres', password='') > cursor = db.cursor () > > cursor.execute ("select * from names") > > rows = cursor.fetchall () > > for i in (rows): > print i > > #viewtable (db) No idea what viewtable does, never seen it before... The only case I found on Google was in a tutorial which defined viewtable() as a function, it wasn't in the pgdb module... > #sys.stdout.write() This writes nothing to stdout. Apparently successfully from your comment below. Try: sys.stdout.write(str(i)) > The code as is will display the data to the console. I have read the > db API 2.0 at python.org. The problem is viewtable (db) will not work > or sys.stdout.write () to write the table data to the console screen. > What am I doing wrong here or what do I need to do different? The print is writing to the console I assume? That is the normal method. > Can anyone point me to a book specificly for pgdb, python and postgre? It doesn't look lie you need a postgres specific boook but just to go through the standard Python tutorial. It should make most things clear. > Last thing is can I define the classes in a separate files and > reference them? Kinda like code reuse in .Net? I new to Python so > bear with me. Yes, just define them in a file then import that file as a module. The standard tutorial explains that too. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at ieee.org Mon Mar 8 03:51:43 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 07 Mar 2010 21:51:43 -0500 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003071213q520c15c8w6ce7a09d4a55631d@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> <4B94007A.1060809@ieee.org> <23ce85921003071213q520c15c8w6ce7a09d4a55631d@mail.gmail.com> Message-ID: <4B94663F.3060307@ieee.org> Giorgio wrote: > 2010/3/7 Dave Angel > > >> Those two lines don't make any sense by themselves. Show us some context, >> and we can more sensibly comment on them. And try not to use names that >> hide built-in keywords, or Python stdlib names. >> >> > > Hi Dave, > > I'm considering Amazon SimpleDB as an alternative to PGSQL, but i need to > store blobs. > > Amazon's FAQs says that: > > "Q: What kind of data can I store? > You can store any UTF-8 string data in Amazon SimpleDB. Please refer > to the Amazon > Web Services Customer Agreement for > details." > > This is the problem. Any idea? > > > >> DaveA >> >> > > Giorgio > > > > You still didn't provide the full context. Are you trying to do store binary data, or not? Assuming you are, you could do the UUENCODE suggestion I made. Or use base64: base64.encodestring(/s/) wlll turn binary data into (larger) binary data, also considered a string. The latter is ASCII, so it's irrelevant whether it's considered utf-8 or otherwise. You store the resulting string in your database, and use base64.decodestring(s) to reconstruct your original. There's 50 other ways, some more efficient, but this may be the simplest. DaveA From benshafat at gmail.com Mon Mar 8 05:38:49 2010 From: benshafat at gmail.com (Elisha Rosensweig) Date: Sun, 7 Mar 2010 23:38:49 -0500 Subject: [Tutor] Printing time without "if" statement Message-ID: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> Hi, I have an event-based simulator written in Python (of course). It takes a while to run, and I want to have messages printed every so often to the screen, indicating the simulation time that has passed. Currently, every event that occurs follows the following code snippet: # initilize printstep = 10 nextprint = 10 # for each event if current_time > nextprint: print 'time past: ' + str(nextprint) nextprint += printstep This seems stupid to me - why should I have to check each round if the time has been passed? I was thinking that there might be a simpler way, using threading, but I have zero experience with threading, so need your help. The idea was to have a simple thread that waited X time (CPU cycles, say) and then read the "current_time" variable and printed it. Any simple (simpler?) solutions to this? Thanks Elisha -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Mon Mar 8 06:53:26 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sun, 7 Mar 2010 21:53:26 -0800 Subject: [Tutor] Printing time without "if" statement In-Reply-To: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> References: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> Message-ID: <20100308055326.GA56258@dragon.alchemy.com> On Sun, Mar 07, 2010 at 11:38:49PM -0500, Elisha Rosensweig wrote: > Hi, > > I have an event-based simulator written in Python (of course). It takes a > while to run, and I want to have messages printed every so often to the > screen, indicating the simulation time that has passed. Currently, every > event that occurs follows the following code snippet: It depends on how your system simulates the events and their scheduling, but you might look at the sched module in the standard library. Maybe you would be able to use that to schedule your simulated events and also schedule periodic time output too. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From stefan_ml at behnel.de Mon Mar 8 07:50:06 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 08 Mar 2010 07:50:06 +0100 Subject: [Tutor] Encoding In-Reply-To: <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B8E8398.8040902@ieee.org> <23ce85921003030810m7cba47des94bda5287e9d555a@mail.gmail.com> <23ce85921003030928l416551a4k5f36a62e14cd9d14@mail.gmail.com> <23ce85921003031144w7cc36ba3q2f33bdcc934720b8@mail.gmail.com> <20100304080130.16c6f060@o> <23ce85921003040613v199ec11en5ede80e016f7ad01@mail.gmail.com> <20100304170701.73c64f44@o> <23ce85921003041123h101fe777ta4c6c42387ce8dff@mail.gmail.com> <4B906EDF.8050409@ieee.org> <23ce85921003050556k5b3218e9o49b6c2a5eb688f4f@mail.gmail.com> Message-ID: Giorgio, 05.03.2010 14:56: > What i don't understand is why: > > s = u"ciao ? ciao" is converting a string to unicode, decoding it from the > specified encoding but > > t = "ciao ? ciao" > t = unicode(t) > > That should do exactly the same instead of using the specified encoding > always assume that if i'm not telling the function what the encoding is, i'm > using ASCII. > > Is this a bug? Did you read the Unicode tutorial at the link I posted? Here's the link again: http://www.amk.ca/python/howto/unicode Stefan From noufal at nibrahim.net.in Mon Mar 8 07:27:35 2010 From: noufal at nibrahim.net.in (Noufal Ibrahim) Date: Mon, 8 Mar 2010 11:57:35 +0530 (IST) Subject: [Tutor] Passing nested structures to fcntl.ioctl Message-ID: <7130.164.164.250.10.1268029655.squirrel@mail.hcoop.net> Hello everyone, I have some code that's calling fcntl.ioctl. I need to pass a nested structure as the 3rd argument of the function. Something like this typedef struct coordinates{ int x; int y; } coordinates; typedef struct point{ int amp; coordinates* coord; } point; How would I do this? I need to allocate a point structure and then a coordinate structure and link them up together via the coord member of point, fill them up with values and pass it to the fcntl.ioctl. The actual IOCTL implementation will do the indirection to read out the data. I can't use ctypes since I'm working on an embedded system with an ARM core that doesn't have ctypes. The struct module doesn't seem to help due to the indirection. Any ideas? I'm personally planning to write a small C extension that creates the structure based on a specification in memory and then passes that to the IOCTL call directly but that looks like some work. I was also told to take a look at Cython which basically seems to be an easy way of doing what I'm planning. Is there a quicker/easier way? Thanks. From steve at pearwood.info Mon Mar 8 08:03:12 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 8 Mar 2010 18:03:12 +1100 Subject: [Tutor] Printing time without "if" statement In-Reply-To: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> References: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> Message-ID: <201003081803.13291.steve@pearwood.info> On Mon, 8 Mar 2010 03:38:49 pm Elisha Rosensweig wrote: > Hi, > > I have an event-based simulator written in Python (of course). It > takes a while to run, and I want to have messages printed every so > often to the screen, indicating the simulation time that has passed. > Currently, every event that occurs follows the following code > snippet: [...] > This seems stupid to me - why should I have to check each round if > the time has been passed? I was thinking that there might be a > simpler way, using threading, but I have zero experience with > threading, so need your help. The idea was to have a simple thread > that waited X time (CPU cycles, say) and then read the "current_time" > variable and printed it. > > Any simple (simpler?) solutions to this? That's a brilliant idea. I think I will steal it for my own code :) (Why didn't I think of it myself? Probably because I almost never use threads...) Anyway, here's something that should help: import time import threading class Clock(threading.Thread): def __init__(self, *args, **kwargs): super(Clock, self).__init__(*args, **kwargs) self.finished = False def start(self): print "Clock %s started at %s" % (self.getName(), time.ctime()) super(Clock, self).start() def run(self): while 1: if self.finished: break print "Clock %s still alive at %s" % ( self.getName(), time.ctime()) time.sleep(2) print "Clock %s quit at %s" % (self.getName(), time.ctime()) def quit(self): print "Clock %s asked to quit at %s" % ( self.getName(), time.ctime()) self.finished = True def do_work(): clock = Clock(name="clock-1") clock.start() # Start processing something hard. for i in xrange(8): print "Processing %d..." % i # Simulate real work with a sleep. time.sleep(0.75) clock.quit() And in action: >>> do_work() Clock clock-1 started at Mon Mar 8 17:40:42 2010 Processing 0... Clock clock-1 still alive at Mon Mar 8 17:40:43 2010 Processing 1... Processing 2... Clock clock-1 still alive at Mon Mar 8 17:40:45 2010 Processing 3... Processing 4... Processing 5... Clock clock-1 still alive at Mon Mar 8 17:40:47 2010 Processing 6... Processing 7... Clock clock-1 still alive at Mon Mar 8 17:40:49 2010 Clock clock-1 asked to quit at Mon Mar 8 17:40:49 2010 >>> Clock clock-1 quit at Mon Mar 8 17:40:51 2010 >>> There's a bit of a display artifact in the interactive interpreter, when the final quit message is printed: the interpreter doesn't notice it needs to redraw the prompt. But other than that, it should be fine. -- Steven D'Aprano From mhw at doctors.net.uk Mon Mar 8 08:55:13 2010 From: mhw at doctors.net.uk (mhw at doctors.net.uk) Date: Mon, 8 Mar 2010 07:55:13 +0000 Subject: [Tutor] Unit-testing advice Message-ID: <794886162-1268034941-cardhu_decombobulator_blackberry.rim.net-1336561345-@bda188.bisx.produk.on.blackberry> Dear All, Quick UT question. I am working on a CSV processing script. I want to write some tests. The tests will need to read in, manipulate and write out some CSV files. My instinct is to manually construct some small data files, and consider them as part of the test suite. The other option would be to programatically construct them (? As a set-up). Which is better? Thanks, Matt Sent from my BlackBerry? wireless device From c.t.matsumoto at gmail.com Mon Mar 8 09:43:14 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Mon, 08 Mar 2010 09:43:14 +0100 Subject: [Tutor] Unit-testing advice In-Reply-To: <794886162-1268034941-cardhu_decombobulator_blackberry.rim.net-1336561345-@bda188.bisx.produk.on.blackberry> References: <794886162-1268034941-cardhu_decombobulator_blackberry.rim.net-1336561345-@bda188.bisx.produk.on.blackberry> Message-ID: <4B94B8A2.8020606@gmail.com> mhw at doctors.net.uk wrote: > Dear All, > > Quick UT question. I am working on a CSV processing script. I want to write some tests. The tests will need to read in, manipulate and write out some CSV files. > > My instinct is to manually construct some small data files, and consider them as part of the test suite. The other option would be to programatically construct them (? As a set-up). > > Which is better? > > Thanks, > > Matt > > > Sent from my BlackBerry? wireless device > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I guess this depends on what part of your program you are testing. If you are testing small 'nuts and bolts' part of the script I usually have the test data written into the test as global variables. As your functionality becomes more complete, you probably need to switch to importing a test data file. I agree I'd manually construct some data files. But if you want to test directory/file creation you could programaticaly create a test directory in /tmp/ to test on. T From denis.spir at gmail.com Mon Mar 8 10:10:10 2010 From: denis.spir at gmail.com (spir) Date: Mon, 8 Mar 2010 10:10:10 +0100 Subject: [Tutor] Passing nested structures to fcntl.ioctl In-Reply-To: <7130.164.164.250.10.1268029655.squirrel@mail.hcoop.net> References: <7130.164.164.250.10.1268029655.squirrel@mail.hcoop.net> Message-ID: <20100308101010.6aa512f7@o> On Mon, 8 Mar 2010 11:57:35 +0530 (IST) "Noufal Ibrahim" wrote: > Hello everyone, > I have some code that's calling fcntl.ioctl. I need to pass a nested > structure as the 3rd argument of the function. Something like this > > typedef struct coordinates{ > int x; > int y; > } coordinates; > > typedef struct point{ > int amp; > coordinates* coord; > } point; > > How would I do this? I need to allocate a point structure and then a > coordinate structure and link them up together via the coord member of > point, fill them up with values and pass it to the fcntl.ioctl. The > actual IOCTL implementation will do the indirection to read out the > data. > > I can't use ctypes since I'm working on an embedded system with an ARM > core that doesn't have ctypes. The struct module doesn't seem to help > due to the indirection. > > Any ideas? I'm personally planning to write a small C extension that > creates the structure based on a specification in memory and then > passes that to the IOCTL call directly but that looks like some work. I > was also told to take a look at Cython which basically seems to be an > easy way of doing what I'm planning. Is there a quicker/easier way? > > Thanks. If your point is to implement a type (point) in C, for performance certainly, and reuse it from a dynamic language, then I'm not sure python is the best choice (*). Python programmers code in python e basta. C-python bindings are far from beeing simplistic. http://wiki.cython.org/WrappingCorCpp Denis (*) Lua instead is well-known for this kind of stuff; especially on particuliar hardware like ARM. Lua's own implementation is 100% plain ANSI C standard and Lua-C bindings are probably as straightforward as they can be. http://lua-users.org/wiki/BindingCodeToLua -- ________________________________ la vita e estrany spir.wikidot.com From vinh.dhbk at gmail.com Mon Mar 8 10:48:23 2010 From: vinh.dhbk at gmail.com (Hichiro) Date: Mon, 8 Mar 2010 16:48:23 +0700 Subject: [Tutor] Read XML records one by one Message-ID: -- Best regards, Vinh NV CNPM K50 DHBKHN Y! : Vinh.dhbk Sky : Vinh.dhbk 84 976 314 988 -------------- next part -------------- An HTML attachment was scrubbed... URL: From vinh.dhbk at gmail.com Mon Mar 8 10:48:54 2010 From: vinh.dhbk at gmail.com (Hichiro) Date: Mon, 8 Mar 2010 16:48:54 +0700 Subject: [Tutor] Read XML records one by one Message-ID: Hi all! I'm trying to read one by one record in XML file to find out its tag and attribute for schema matching. But I haven't done yet. So, could you help me?! Thanks so much! :) -- Best regards, Vinh NV CNPM K50 DHBKHN Y! : Vinh.dhbk Sky : Vinh.dhbk 84 976 314 988 -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Mon Mar 8 10:50:21 2010 From: denis.spir at gmail.com (spir) Date: Mon, 8 Mar 2010 10:50:21 +0100 Subject: [Tutor] Printing time without "if" statement In-Reply-To: <201003081803.13291.steve@pearwood.info> References: <7fd42f291003072038o205dfdcftd530ca4579d266b6@mail.gmail.com> <201003081803.13291.steve@pearwood.info> Message-ID: <20100308105021.2d8f0584@o> On Mon, 8 Mar 2010 18:03:12 +1100 Steven D'Aprano wrote: > On Mon, 8 Mar 2010 03:38:49 pm Elisha Rosensweig wrote: > > Hi, > > > > I have an event-based simulator written in Python (of course). It > > takes a while to run, and I want to have messages printed every so > > often to the screen, indicating the simulation time that has passed. > > Currently, every event that occurs follows the following code > > snippet: > [...] > > This seems stupid to me - why should I have to check each round if > > the time has been passed? I was thinking that there might be a > > simpler way, using threading, but I have zero experience with > > threading, so need your help. The idea was to have a simple thread > > that waited X time (CPU cycles, say) and then read the "current_time" > > variable and printed it. > > > > Any simple (simpler?) solutions to this? > > That's a brilliant idea. I think I will steal it for my own code :) > > (Why didn't I think of it myself? Probably because I almost never use > threads...) > > Anyway, here's something that should help: > > > import time > import threading > > class Clock(threading.Thread): > def __init__(self, *args, **kwargs): > super(Clock, self).__init__(*args, **kwargs) > self.finished = False > def start(self): > print "Clock %s started at %s" % (self.getName(), time.ctime()) > super(Clock, self).start() > def run(self): > while 1: > if self.finished: > break > print "Clock %s still alive at %s" % ( > self.getName(), time.ctime()) > time.sleep(2) > print "Clock %s quit at %s" % (self.getName(), time.ctime()) > def quit(self): > print "Clock %s asked to quit at %s" % ( > self.getName(), time.ctime()) > self.finished = True > > def do_work(): > clock = Clock(name="clock-1") > clock.start() > # Start processing something hard. > for i in xrange(8): > print "Processing %d..." % i > # Simulate real work with a sleep. > time.sleep(0.75) > clock.quit() > > > > And in action: > > >>> do_work() > Clock clock-1 started at Mon Mar 8 17:40:42 2010 > Processing 0... > Clock clock-1 still alive at Mon Mar 8 17:40:43 2010 > Processing 1... > Processing 2... > Clock clock-1 still alive at Mon Mar 8 17:40:45 2010 > Processing 3... > Processing 4... > Processing 5... > Clock clock-1 still alive at Mon Mar 8 17:40:47 2010 > Processing 6... > Processing 7... > Clock clock-1 still alive at Mon Mar 8 17:40:49 2010 > Clock clock-1 asked to quit at Mon Mar 8 17:40:49 2010 > >>> Clock clock-1 quit at Mon Mar 8 17:40:51 2010 > > >>> > > > There's a bit of a display artifact in the interactive interpreter, when > the final quit message is printed: the interpreter doesn't notice it > needs to redraw the prompt. But other than that, it should be fine. Hello, really interesting, indeed. I also have no idea about threading in python. But as timer is concerned, I would prefere something like: import time import whatever_to_ensure_run_in_separate_thread class Timer(whatever_to_ensure_run_in_separate_thread): UNIT = 1/1000000 # ?s def __init__(self, delay, action, cyclic=False): self.delay = delay self.action = action self.cyclic = cyclic self.goon = True def run(self): if self.cyclic: while self.goon: self.cycle() else: self.cycle def cycle(self): self.wait() self.action() def wait(self): delay.sleep(self.delay * self.UNIT) Or even better, launch a signal in main thread -- which is supposed to be an event cycle. This is how things work in the field of automation: class Timer(whatever_to_ensure_run_in_separate_thread): UNIT = 1/1000000 # ?s def __init__(self, delay, signal, resetDelay=1, cyclic=False): self.delay = delay self.signal = signal self.resetDelay = resetDelay self.cyclic = cyclic self.goon = True def run(self): if self.cyclic: while self.goon: self.cycle() else: self.cycle def cycle(self): self.wait(self.delay) self.signal = True self.wait(self.resetDelay) self.signal = False def wait(self, delay): delay.sleep(delay * self.UNIT) # main cycle: t1 = Timer(.......) ....... if t1_signal: do_t1_action (Actually, in python, the signal may be the index of a Timer.signals boolean array, incremented with each new instance.) Denis -- ________________________________ la vita e estrany spir.wikidot.com From platodreams at gmail.com Mon Mar 8 11:43:02 2010 From: platodreams at gmail.com (Plato P.B.) Date: Mon, 8 Mar 2010 16:13:02 +0530 Subject: [Tutor] Communicate between a thread and the main program Message-ID: Hi all, I have created a script in which i need to implement the communication between the main program and a thread. The thread looks for any newly created files in a particular directory. It will be stored in a variable in the thread function. I want to get that name from the main program. How can i do it? Thanks in Advance. :D -- Rgds.............. Plato P.B. obscurant1st.biz/blog B'Lore ( +919844882641) TsR I. ( +919995317984) TsR II. ( +919037036661) -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Mon Mar 8 11:44:38 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 08 Mar 2010 11:44:38 +0100 Subject: [Tutor] Read XML records one by one In-Reply-To: References: Message-ID: Hichiro, 08.03.2010 10:48: > I'm trying to read one by one record in XML file to find out its tag and > attribute for schema matching. But I haven't done yet. So, could you help > me?! You were not very specific about your data, neither did you provide enough information about your use case to understand what you want to achieve. Therefore, I can't give you a real solution. Here's one you can adapt: import xml.etree.ElementTree as ET for _, element in ET.iterparse("the_file.xml"): print element.tag, element.attrib Stefan From transmogribenno at gmail.com Mon Mar 8 13:20:44 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Mon, 8 Mar 2010 21:20:44 +0900 Subject: [Tutor] WSGI / Apache In-Reply-To: <23ce85921003041137s1c1d9c85rfa5c1e7685ff9f23@mail.gmail.com> References: <23ce85921003041137s1c1d9c85rfa5c1e7685ff9f23@mail.gmail.com> Message-ID: <9b00d1a91003080420ve42c60dy75983857b98fb898@mail.gmail.com> On 5 March 2010 04:37, Giorgio wrote: > Hi, > > as you all probably know i'm using the Google App Engine platform for my > python code. > > As I learn python i try to understand more and more how GAE works. Today > i've noticed that all applications on GAE are running on WSGI. A quick > Google search told me that WSGI is a new standard for web applications, > developed by python. > > So, I decided to try it. With Apache. > > I found that Apache has a dedicated mod, mod_wsgi to handle this type of > requests. But, as I always try to be "platform/daemon indipended" i've > looked for other solutions. I found WSGIREF that as wsgi.org states > "includes a threaded HTTP server, a CGI server (for running any WSGI > application as a CGI script)". > > So it seems that wsgiref, that is actually included in python, can "convert" > my apps to work with a standard CGI interface (in other words, working with > EVERY server that supports CGI not only with apache and his dedicated mod). > Can you confirm this? WSGI is based on CGI, so I imagine that it's not too difficult to have a wrapper that converts between the protocols. I haven't tried wsgiref, though. > Do you have any idea i should use mod_wsgi instead of wsgiref + mod_cgi? >From your application's perspective, if it talks WSGI it shouldn't make any difference. But if you're using GAE, what good will it do to have a local Apache server anyway? Cheers, benno From davea at ieee.org Mon Mar 8 13:51:33 2010 From: davea at ieee.org (Dave Angel) Date: Mon, 08 Mar 2010 07:51:33 -0500 Subject: [Tutor] Communicate between a thread and the main program In-Reply-To: References: Message-ID: <4B94F2D5.9050804@ieee.org> Plato P.B. wrote: > Hi all, > I have created a script in which i need to implement the communication > between the main program and a thread. > The thread looks for any newly created files in a particular directory. It > will be stored in a variable in the thread function. I want to get that name > from the main program. > How can i do it? > > Thanks in Advance. :D > Don't store it in "a variable in the thread function," but in an instance attribute of the thread object. Then the main program simply checks the object's attribute. Since it launched the thread(s), it should know its (their) instances. This way, the solution scales up as you add more threads, with different functionality. DaveA From denis.spir at gmail.com Mon Mar 8 13:57:39 2010 From: denis.spir at gmail.com (spir) Date: Mon, 8 Mar 2010 13:57:39 +0100 Subject: [Tutor] variable inheritance Message-ID: <20100308135739.0a39137b@o> Hello, Say I have a Tree type that may be based on 2 kinds of Node-s. Both conceptually and practically, a tree itself is a node, namely the top/root one. But a tree also has some additional tree-level behaviour, which is independant of the kind of node. Say, the node type performs all the underlying tree mechanics. Thus the Tree type looks like: class Tree(_Node): def __init__(self, args): _Node.__init__(self) ... ... tree-level methods ... The issue is the actual kind of node is not only initialised through "_Node.__init__(self)": it must first feature as super class in "class Tree(Node)". (Otherwise I get an error about wrong type of self.) I tried to play with __new__ in Tree and Node types, but this error seems unavoidable. Maybe I did not do it right. I can indeed have __new__ return an instance of either Node type, but then for any reason it "forgets" it is also a tree (so that calling any tree method fails). I there a way to do this right? Workaround ideas: * Make the root node an attribute of tree: unsatisfying. * Create a common tree type and 2 specialised ones: class TreeN(Tree,NodeN) def __init__(self, args): _NodeN.__init__(self) Tree.__init__(self, args) # ... e basta ... Denis -- ________________________________ la vita e estrany spir.wikidot.com From waynejwerner at gmail.com Mon Mar 8 14:00:29 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 8 Mar 2010 07:00:29 -0600 Subject: [Tutor] Really learn programming Message-ID: <333efb451003080500x8eb74o35fbd4d6415eeec@mail.gmail.com> As I was reading my favorite webcomics, Abstruse Goose had a link to this interesting site about programming in 21 days (or not). http://norvig.com/21-days.html I thought it was rather interesting (and it also recommends Python ;) enjoy, -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 karper12345 at yahoo.com Mon Mar 8 15:00:50 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Mon, 8 Mar 2010 06:00:50 -0800 (PST) Subject: [Tutor] Use ZODB or Durus to store Python-objects? Message-ID: <814330.21260.qm@web44709.mail.sp1.yahoo.com> I'm using pickle more often in my programs and now I store them as files. I want to store them in ZODB or Durus to prevent losing sight of where I store what pickled object. Which one is the best one to use? I've never worked with either one, so I would like to have some opinions. I want to achieve the following: * synchronisation of (ZODB/Durus) data on multiple servers (ie with rsync) to prevent data-loss * which is the most easy to use (for a python-starter)? * which is the most stabile to use? * performance with large datasets? * is it possible to synchronize changes done on objects (ie dictionaries) by multiple client-processes? -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Mon Mar 8 16:21:49 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 8 Mar 2010 16:21:49 +0100 Subject: [Tutor] variable inheritance In-Reply-To: <20100308135739.0a39137b@o> References: <20100308135739.0a39137b@o> Message-ID: <29179d161003080721q77595f3ve9b0f2b4d5104a02@mail.gmail.com> On Mon, Mar 8, 2010 at 1:57 PM, spir wrote: > Hello, > > Say I have a Tree type that may be based on 2 kinds of Node-s. Both conceptually and practically, a tree itself is a node, namely the top/root one. But a tree also has some additional tree-level behaviour, which is independant of the kind of node. Say, the node type performs all the underlying tree mechanics. Thus the Tree type looks like: > > class Tree(_Node): > ? ?def __init__(self, args): > ? ? ? ?_Node.__init__(self) > ? ? ? ?... > ? ?... tree-level methods ... > > The issue is the actual kind of node is not only initialised through "_Node.__init__(self)": it must first feature as super class in "class Tree(Node)". (Otherwise I get an error about wrong type of self.) > I tried to play with __new__ in Tree and Node types, but this error seems unavoidable. Maybe I did not do it right. I can indeed have __new__ return an instance of either Node type, but then for any reason it "forgets" it is also a tree (so that calling any tree method fails). > I there a way to do this right? > > Workaround ideas: > * Make the root node an attribute of tree: unsatisfying. > * Create a common tree type and 2 specialised ones: > class TreeN(Tree,NodeN) > ? ?def __init__(self, args): > ? ? ? ?_NodeN.__init__(self) > ? ? ? ?Tree.__init__(self, args) > # ... e basta ... > Design-wise, there are two ways of looking at the problem: * A tree *is* a node, containing some amount of sub-trees/nodes (they are the same thing) * A tree is a data structure that has one top-level node, which in turn contains several child-nodes, which can in turn contain child-nodes, etc. etc. In the first case, you really shouldn't make a separate Tree type. They are the same thing, they should have the same behavior. This also allows great flexibility with extracting subtrees, and has quite elegant recursive implementations. In the second case, the Tree is something separate from it's root Node, and so making Tree inherit from Node really doesn't make any sense. Use containment in this case. This case is also well suited to iterative implementations. What you're trying to do is a kind of mixture between the two, having the root Node as a separate type. I would try to avoid it. If you really must, though, making a Tree class for every node type is your best bet. Use a class factory to prevent this from getting cumbersome: def make_treetype(nodetype): class Tree(nodetype): def __init__(self): nodetype.__init__(self) # etc etc return Tree There might also be a metaclass solution, but I'm not an expert on that. Metaclasses tend to melt my brain. Hugo From karper12345 at yahoo.com Mon Mar 8 17:05:54 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Mon, 8 Mar 2010 08:05:54 -0800 (PST) Subject: [Tutor] matching words from a text to keys in a dictionary Message-ID: <971392.68173.qm@web44706.mail.sp1.yahoo.com> I want to compare words in a text to a dictionary with values attached to the words. The dictionary? looks like: { word1: [1,2,3] word2: [2,3,4,a,b ] ... } I'm trying to find a way to achieve this, but I'm having trouble getting corrects results. If I do the def below, nothing is matched. def searchWord(text, dictionary): ??? text = text.split() ??? for word in text: ??????? print word ??????? if word in dictionary: ??????????? value = dictionary[str(word)] ??????? else: ??????????? value = None ??????? return w If I try another way, I keep getting errors: def searchWord(text, dictionary): ??? for key in dictionary: ??????? value = dictionary[key] ??????? if re.search(key, text): ??????????? w = value ??? else: ??????? w = None ??? return w TypeError: list indices must be integers, not str I'm probably overlooking something and suspect that I'm doing it the slow way, so any advice is welcome. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sanelson at gmail.com Mon Mar 8 17:12:35 2010 From: sanelson at gmail.com (Stephen Nelson-Smith) Date: Mon, 8 Mar 2010 16:12:35 +0000 Subject: [Tutor] Making Regular Expressions readable Message-ID: Hi, I've written this today: #!/usr/bin/env python import re pattern = r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(, [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1}) (?P(\S*)) (?P(\S*)) (?P(\[[^\]]+\])) (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) (?P(\S*)) (?P(\S*)) (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)( )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)' regex = re.compile(pattern) lines = 0 no_cookies = 0 for line in open('/home/stephen/scratch/feb-100.txt'): lines +=1 line = line.strip() match = regex.match(line) if match: data = match.groupdict() if data['SiteIntelligenceCookie'] == '': no_cookies +=1 else: print "Couldn't match ", line print "I analysed %s lines." % (lines,) print "There were %s lines with missing Site Intelligence cookies." % (no_cookies,) It works fine, but it looks pretty unreadable and unmaintainable to anyone who hasn't spent all day writing regular expressions. I remember reading about verbose regular expressions. Would these help? How could I make the above more maintainable? S. -- Stephen Nelson-Smith Technical Director Atalanta Systems Ltd www.atalanta-systems.com From andreengels at gmail.com Mon Mar 8 17:26:46 2010 From: andreengels at gmail.com (Andre Engels) Date: Mon, 8 Mar 2010 17:26:46 +0100 Subject: [Tutor] matching words from a text to keys in a dictionary In-Reply-To: <971392.68173.qm@web44706.mail.sp1.yahoo.com> References: <971392.68173.qm@web44706.mail.sp1.yahoo.com> Message-ID: <6faf39c91003080826t4f4d38c1x4457fcdd5ba6dfd8@mail.gmail.com> On Mon, Mar 8, 2010 at 5:05 PM, Karjer Jdfjdf wrote: > I want to compare words in a text to a dictionary with values attached to > the words. > > The dictionary looks like: > { word1: [1,2,3] word2: [2,3,4,a,b ] ... } > Please give the actual dictionary, not something that it 'looks like' - an actual dictionary would never 'look like' this: it has commas between the elements, and quotes around anything that is a word. > I'm trying to find a way to achieve this, but I'm having trouble getting > corrects results. > > If I do the def below, nothing is matched. > > def searchWord(text, dictionary): > text = text.split() > for word in text: > print word > if word in dictionary: > value = dictionary[str(word)] > else: > value = None > return w > > If I try another way, I keep getting errors: > > def searchWord(text, dictionary): > for key in dictionary: > value = dictionary[key] > if re.search(key, text): > w = value > else: > w = None > return w > > > TypeError: list indices must be integers, not str > That's quite a clear statement: If this is indeed caused by the function you show here, then the only explanation is that 'dictionary' is not a dictionary at all, but a list. -- Andr? Engels, andreengels at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From vinh.dhbk at gmail.com Mon Mar 8 17:46:37 2010 From: vinh.dhbk at gmail.com (Hichiro) Date: Mon, 8 Mar 2010 23:46:37 +0700 Subject: [Tutor] Fwd: [XML-SIG] Read XML records one by one In-Reply-To: <4B94D42A.2030008@behnel.de> References: <4B94D42A.2030008@behnel.de> Message-ID: On Mon, Mar 8, 2010 at 5:40 PM, Stefan Behnel wrote: > Hichiro, 08.03.2010 10:44: > >> I'm trying to read one by one record in XML file to find out its tag and >> >> attribute for schema matching. But I haven't done yet. So, could you help >> me?! >> > > Hi, > > since it appears that you cross-posted this to the python-tutor list, I'll > answer over there. > > Stefan > -- Best regards, Vinh NV CNPM K50 DHBKHN Y! : Vinh.dhbk Sky : Vinh.dhbk 84 976 314 988 -------------- next part -------------- An HTML attachment was scrubbed... URL: From glenbot at gmail.com Mon Mar 8 17:49:40 2010 From: glenbot at gmail.com (Glen Zangirolami) Date: Mon, 8 Mar 2010 10:49:40 -0600 Subject: [Tutor] Read XML records one by one In-Reply-To: References: Message-ID: Another alternative to parsing XML is beautiful soup. Website: http://www.crummy.com/software/BeautifulSoup/ Documentation for parsing xml: http://www.crummy.com/software/BeautifulSoup/documentation.html#Parsing%20XML sample code: from BeautifulSoup import BeautifulStoneSoup xml = "Contents 1Contents 2Contents 3" soup = BeautifulStoneSoup(xml) print soup.prettify() # # # Contents 1 # # Contents 2 # # # # Contents 3 # # On Mon, Mar 8, 2010 at 3:48 AM, Hichiro wrote: > > Hi all! > I'm trying to read one by one record in XML file to find out its tag and > attribute for schema matching. But I haven't done yet. So, could you help > me?! > Thanks so much! :) > > > > > -- > Best regards, > Vinh NV > CNPM K50 DHBKHN > Y! : Vinh.dhbk > Sky : Vinh.dhbk > 84 976 314 988 > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From glenbot at gmail.com Mon Mar 8 17:52:55 2010 From: glenbot at gmail.com (Glen Zangirolami) Date: Mon, 8 Mar 2010 10:52:55 -0600 Subject: [Tutor] Communicate between a thread and the main program In-Reply-To: References: Message-ID: I think you can use Queue to communicate between threads. "The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads" http://docs.python.org/library/queue.html On Mon, Mar 8, 2010 at 4:43 AM, Plato P.B. wrote: > > Hi all, > I have created a script in which i need to implement the communication > between the main program and a thread. > The thread looks for any newly created files in a particular directory. It > will be stored in a variable in the thread function. I want to get that name > from the main program. > How can i do it? > > Thanks in Advance. :D > -- > > > > > > > Rgds.............. > Plato P.B. > obscurant1st.biz/blog > > B'Lore ( +919844882641) > TsR I. ( +919995317984) > TsR II. ( +919037036661) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Mon Mar 8 17:59:03 2010 From: denis.spir at gmail.com (spir) Date: Mon, 8 Mar 2010 17:59:03 +0100 Subject: [Tutor] matching words from a text to keys in a dictionary In-Reply-To: <971392.68173.qm@web44706.mail.sp1.yahoo.com> References: <971392.68173.qm@web44706.mail.sp1.yahoo.com> Message-ID: <20100308175903.12299e7e@o> On Mon, 8 Mar 2010 08:05:54 -0800 (PST) Karjer Jdfjdf wrote: > I want to compare words in a text to a dictionary with values attached to the words. > > The dictionary? looks like: > { word1: [1,2,3] word2: [2,3,4,a,b ] ... } And how does your source text look like? (we have half of the data) And please print (part of) your dict out (instead of providing erroneous data). > I'm trying to find a way to achieve this, but I'm having trouble getting corrects results. > > If I do the def below, nothing is matched. > > def searchWord(text, dictionary): > ??? text = text.split() > ??? for word in text: > ??????? print word print word, word in dictionary # would be useful for debug > ??????? if word in dictionary: > ??????????? value = dictionary[str(word)] word is already a string -- or should be ;-) > ??????? else: > ??????????? value = None > ??????? return w What is w? This is your main issue, I guess... [...] Denis -- ________________________________ la vita e estrany spir.wikidot.com From karper12345 at yahoo.com Mon Mar 8 18:14:29 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Mon, 8 Mar 2010 09:14:29 -0800 (PST) Subject: [Tutor] matching words from a text to keys in a dictionary Message-ID: <68549.86780.qm@web44707.mail.sp1.yahoo.com> >>? I want to compare words in a text to a dictionary with values attached to >> the words. >> >> The dictionary? looks like: >> { word1: [1,2,3] word2: [2,3,4,a,b ] ... } >> > >Please give the actual dictionary, not something that it 'looks like' - an >actual dictionary would never 'look like' this: it has commas between the >elements, and quotes around anything that is a word. > Sorry, I was a bit quick typing it, forgetting the proper format. The dictionary looks something like this (simplified): { 'chinese': ['china', '17', '3'], 'vietnamese': ['vietnam', '89'] ... } A text might be something like this and should get 2 matches: ?'A vietnamese farmer ate a chinese noodle soup, but it was not made in china' > >> I'm trying to find a way to achieve this, but I'm having trouble getting >> corrects results. >> >> If I do the def below, nothing is matched. >> >> def searchWord(text, dictionary): >>???? text = text.split() >>???? for word in text: >>???????? print word >>???????? if word in dictionary: >>???????????? value = dictionary[str(word)] >>???????? else: >>???????????? value = None >>???????? return w >> >> If I try another way, I keep getting errors: >> >> def searchWord(text, dictionary): >>???? for key in dictionary: >>???????? value = dictionary[key] >>???????? if re.search(key, text): >>???????????? w = value >>???? else: >>???????? w = None >>???? return w >> >> >> TypeError: list indices must be integers, not str >> > >That's quite a clear statement: If this is indeed caused by the function you >show here, then the only explanation is that 'dictionary' is not a >dictionary at all, but a list. Actually the values are lists. -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.just4fun at gmail.com Mon Mar 8 18:15:56 2010 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Mon, 08 Mar 2010 18:15:56 +0100 Subject: [Tutor] Resetting the namespace Message-ID: <4B9530CC.30201@gmail.com> I found this piece of code, which completely puzzles me, in the pdb module of the trunk: class Pdb(bdb.Bdb, cmd.Cmd): ... def _runscript(self, filename): # The script has to run in __main__ namespace (or imports from # __main__ will break). # # So we clear up the __main__ and set several special variables # (this gets rid of pdb's globals and cleans old variables on # restarts). import __main__ __main__.__dict__.clear() __main__.__dict__.update({"__name__" : "__main__", "__file__" : filename, "__builtins__": __builtins__, }) The intention of this code is to reset the namespace, before executing a script. When I try to do something similar, i.e. __main__.__dict__.clear(), I loose the __builtins__ variable in the namespace, e.g.: >>> import __main__ >>> print __builtins__ >>> __main__.__dict__.clear() >>> print __builtins__ Traceback (most recent call last): File "", line 1, in NameError: name '__builtins__' is not defined But for some reason the code mentioned above actually works in the case of pdb. Any ideas why? - Patrick From denis.spir at gmail.com Mon Mar 8 18:34:05 2010 From: denis.spir at gmail.com (spir) Date: Mon, 8 Mar 2010 18:34:05 +0100 Subject: [Tutor] Making Regular Expressions readable In-Reply-To: References: Message-ID: <20100308183405.225b6f2a@o> On Mon, 8 Mar 2010 16:12:35 +0000 Stephen Nelson-Smith wrote: > Hi, > > I've written this today: > > #!/usr/bin/env python > import re > > pattern = r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(, > [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1}) > (?P(\S*)) (?P(\S*)) > (?P(\[[^\]]+\])) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) > (?P(\S*)) (?P(\S*)) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)( > )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)' [...] > It works fine, but it looks pretty unreadable and unmaintainable to > anyone who hasn't spent all day writing regular expressions. ;-) > I remember reading about verbose regular expressions. Would these help? Yes, read the doc and tutorials about python regexes, you'll find some useful features. > How could I make the above more maintainable? Maybe you can be interested in the following (seems to me it definitely fits your case): I wrote a kind of "SuperPattern" ;-) extension that allows writing, testing and composing sub-regexes. I used {name} since this is illegal in a normal regex. Intended use was something like: import re ; Pattern = re.compile person = Pattern(r"""...""") email = Pattern(r"""...""") phone = Pattern(r"""...""") entry = SuperPattern(scope, r"""{person}:\s+{email}\s*--\s*{phone}""") Don't remember the details, but it was a very simple type to write (meta-using re, indeed). The only point is that the constructor must be passed the scope (typically locals(), else use globals() by default) where to find the sub-patterns. Each regex pattern in fact has a .pattern attr (not sure of the name) that actually holds its format string: so composing a superpattern is just replacing a subpattern's name by its format. > S. > Denis -- ________________________________ la vita e estrany spir.wikidot.com From thudfoo at opensuse.us Mon Mar 8 18:59:28 2010 From: thudfoo at opensuse.us (member thudfoo) Date: Mon, 8 Mar 2010 09:59:28 -0800 Subject: [Tutor] Making Regular Expressions readable In-Reply-To: <20100308183405.225b6f2a@o> References: <20100308183405.225b6f2a@o> Message-ID: <3d881a311003080959p6eed945bj6a82298fb5023e75@mail.gmail.com> On Mon, Mar 8, 2010 at 9:34 AM, spir wrote: > On Mon, 8 Mar 2010 16:12:35 +0000 > Stephen Nelson-Smith wrote: > >> Hi, >> >> I've written this today: >> >> #!/usr/bin/env python >> import re >> >> pattern = r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(, >> [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1}) >> (?P(\S*)) (?P(\S*)) >> (?P(\[[^\]]+\])) >> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) >> (?P(\S*)) (?P(\S*)) >> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) >> (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)( >> )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)' > [...] >> It works fine, but it looks pretty unreadable and unmaintainable to >> anyone who hasn't spent all day writing regular expressions. You might want to consider Plex: www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/ From karper12345 at yahoo.com Mon Mar 8 19:12:38 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Mon, 8 Mar 2010 10:12:38 -0800 (PST) Subject: [Tutor] matching words from a text to keys in a dictionary In-Reply-To: Message-ID: <720538.94536.qm@web44716.mail.sp1.yahoo.com> I brew this up. It works, but I think it will be slow with a long text and a big dictionary def searchWord(text, dictionary): ??? '''search for terms in dictionary(key) and retrieve value(keywords)''' ??? text = text.split() ??? w = [] ??? for word in text: ??????? if word in dictionary: ??????????? print word ??????????? l = dictionary[str(word)] ??????????? for i in l: ??????????????? w = w + [i]?????????????? ??????? else: ??????????? print "can't find anything" ??? return w dict1 = { 'had': ['1', '2'], 'little': ['a'] } text = 'Mary had a little lamb' w = searchWord(text, dict1) print w -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Mar 8 19:43:36 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 8 Mar 2010 18:43:36 -0000 Subject: [Tutor] Really learn programming References: <333efb451003080500x8eb74o35fbd4d6415eeec@mail.gmail.com> Message-ID: "Wayne Werner" wrote > a link to this interesting site about programming in 21 days (or not). > > http://norvig.com/21-days.html > > I thought it was rather interesting Indeed, although I disagree with some of it. (Including some of his "Answers") And it focusses on programming only, which is a bit like focussing on bricklaying when considering the building of bridges. You can only build part of a structure if all you can do is lay bricks... To engineer a complete solution takes many other skills too. Software Engineering is more than programming. But if you only need to build a garden shed or dog kennel then you don't need to go on a Civil Engineering course or even a bricklayers apprenticeship. I don't think the "Learn Z in 21 days" books are aimed at professionals, they are aimed at amateurs who usually only need to build kennels! So yes professional grade programming takes a lifetime to learn (not least because it is constantly changing!) but an amateur can meet all their needs much more quickly and have a lot of fun in doing so. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Mar 8 19:55:45 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 8 Mar 2010 18:55:45 -0000 Subject: [Tutor] variable inheritance References: <20100308135739.0a39137b@o> Message-ID: "spir" wrote > Say I have a Tree type that may be based on 2 kinds of Node-s. > Both conceptually and practically, a tree itself is a node, namely the > top/root one. Thats one way to do it... > But a tree also has some additional tree-level behaviour, which is > independant > of the kind of node. In which case its possibly not a node because it may be breaking the Liskov Substitution Principle. In that case a Tree has its own identity and behaviour and attributes, one of which is a heirarchy of nodes. So you must decide first of all; Is a tree really a node - ie is it fully interchangable with a node? Say, the node type performs all the underlying tree mechanics. Thus the Tree type looks like: > > class Tree(_Node): > def __init__(self, args): > _Node.__init__(self) > ... > ... tree-level methods ... > > The issue is the actual kind of node is not only initialised > through "_Node.__init__(self)": it must first feature as super class > in "class Tree(Node)". Surely the different types of node are all subclasses of the (possibly abstract) Node class? ie you have 3 subclasses of Node? > I tried to play with __new__ in Tree and Node types, but > this error seems unavoidable. Maybe I did not do it right. I'm not totally sure what you did to get the error? > I can indeed have __new__ return an instance of either > Node type, but then for any reason it "forgets" it is also > a tree (so that calling any tree method fails). > I there a way to do this right? Provided the tree is not dependant on the node types there should not be a problem. The Tree treats all nodes as Nodes. Polymorphism should do the rest. > Workaround ideas: > * Make the root node an attribute of tree: unsatisfying. Why is it unsatisfying? Its a perfectly legitimate model. It is the one used by most GUI frameworks for the widget containment tree > * Create a common tree type and 2 specialised ones: > class TreeN(Tree,NodeN) > def __init__(self, args): > _NodeN.__init__(self) > Tree.__init__(self, args) > # ... e basta ... Or a Common Node type and 3 subclasses, Tree,, Node1 and Node2 HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Mon Mar 8 19:57:32 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 8 Mar 2010 10:57:32 -0800 (PST) Subject: [Tutor] Really learn programming In-Reply-To: Message-ID: <799705.35526.qm@web110715.mail.gq1.yahoo.com> This very funny cartoon is about learning C++ in 21 days, but it could be about any language: http://high5.net/comic/AG/ars_longa_vita_brevis.PNG Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Mon, 3/8/10, Alan Gauld wrote: From: Alan Gauld Subject: Re: [Tutor] Really learn programming To: tutor at python.org Date: Monday, March 8, 2010, 7:43 PM "Wayne Werner" wrote > a link to this interesting site about programming in 21 days (or not). > > http://norvig.com/21-days.html > > I thought it was rather interesting Indeed, although I disagree with some of it. (Including some of his "Answers") And it focusses on programming only, which is a bit like focussing on bricklaying when considering the building of bridges. You can only build part of a structure if all you can do is lay bricks... To engineer a complete solution takes many other skills too. Software Engineering is more than programming. But if you only need to build a garden shed or dog kennel then you don't need to go on a Civil Engineering course or even a bricklayers apprenticeship. I don't think the "Learn Z in 21 days" books are aimed at professionals, they are aimed at amateurs who usually only need to build kennels! So yes professional grade programming takes a lifetime to learn (not least because it is constantly changing!) but an amateur can meet all their needs much more quickly and have a lot of fun in doing so. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at dcsoftware.com Tue Mar 9 03:13:25 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Mon, 08 Mar 2010 19:13:25 -0700 Subject: [Tutor] Really learn programming In-Reply-To: <333efb451003080500x8eb74o35fbd4d6415eeec@mail.gmail.com> References: <333efb451003080500x8eb74o35fbd4d6415eeec@mail.gmail.com> Message-ID: <4B95AEC5.8060406@dcsoftware.com> Wayne Werner wrote: > As I was reading my favorite webcomics, Abstruse Goose > had a link to this interesting site about > programming in 21 days (or not). > > http://norvig.com/21-days.html > > I thought it was rather interesting > (and it also recommends Python ;) > > enjoy, > -Wayne > I have been programming for at over 30 years in many languages. When I wanted to learn Python I saw such books. The problem is that you can't learn a language "well" in 21 days; and it's even more difficult if you are not already a programmer in some language. For the novice programmers on this list I (me) would expect to become proficient in Python in about one year. Like I said, I have been programming for over 30 years in RPG, COBOL, Basic, FoxPro mainly. So keep at it and don't expect to be an expert in 21 days. Having said that, the 21 day tutorials have great merit and can help you. YMMV -- Jeff Jeff Johnson jeff at dcsoftware.com Phoenix Python User Group - sunpiggies at googlegroups.com From zhuchunml at gmail.com Tue Mar 9 04:25:19 2010 From: zhuchunml at gmail.com (Joson) Date: Tue, 9 Mar 2010 11:25:19 +0800 Subject: [Tutor] for _ in Message-ID: Hi all, source code as below puzzles me: def make_sparse_labeled_tensor(ndim, labels=None, initial=None, accumulate=None, normalize=False): if labels is None: labels = [OrderedSet() for _ in xrange(ndim)] ...... It's a library named "divisi". "OrderedSet" is a string set defined in this lib. "ndim" is integer. What's the meaning of "for _ in" ? Joson -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Tue Mar 9 10:24:03 2010 From: denis.spir at gmail.com (spir) Date: Tue, 9 Mar 2010 10:24:03 +0100 Subject: [Tutor] for _ in In-Reply-To: References: Message-ID: <20100309102403.37ed4a18@o> On Tue, 9 Mar 2010 11:25:19 +0800 Joson wrote: > if labels is None: labels = [OrderedSet() for _ in xrange(ndim)] > ...... > > It's a library named "divisi". "OrderedSet" is a string set defined in this > lib. "ndim" is integer. What's the meaning of "for _ in" ? > > Joson [OrderedSet() for _ in xrange(n)] builds a list of ndim empty ordered sets. '_' is the index, but is unused; '_' is commonly used as required, but unused, variable name (not only in python). This is just a tricky python idiom, because there is no builtin syntax to do this. [OrderedSet()] * ndim would build a list with ndim times the *same* set, so that if one item is changed, all are changed. The above syntax instread creates *distinct* sets. Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> lists = [list()] *3 >>> lists [[], [], []] >>> list1 = lists[1] >>> list1.append('*') >>> lists [['*'], ['*'], ['*']] >>> lists = [list() for _ in range(3)] # _ is unused >>> lists [[], [], []] >>> list1 = lists[1] >>> list1.append('*') >>> lists [[], ['*'], []] >>> lists = [list()] * 3 >>> for i in range(len(lists)) : lists[i].append(i) ... >>> lists [[0, 1, 2], [0, 1, 2], [0, 1, 2]] >>> lists = [list() for i in range(3)] >>> for i in range(len(lists)) : lists[i].append(i) ... >>> lists [[0], [1], [2]] Denis -- ________________________________ la vita e estrany spir.wikidot.com From pylist1 at gmail.com Tue Mar 9 15:06:00 2010 From: pylist1 at gmail.com (pylist1) Date: Tue, 9 Mar 2010 09:06:00 -0500 (EST) Subject: [Tutor] pgdb and console output In-Reply-To: References: <4f23445b1003071309p73c2a217p4d3c3a9653459d6a@mail.gmail.com> Message-ID: On Mon, 8 Mar 2010, Alan Gauld wrote: > "hithere there" wrote >> #! /usr/bin/env python >> >> import pgdb, sys >> >> db = pgdb.connect (dsn='192.168.0.1:familydata', >> user='postgres', password='') >> cursor = db.cursor () >> >> cursor.execute ("select * from names") >> >> rows = cursor.fetchall () >> >> for i in (rows): >> print i >> >> #viewtable (db) > > No idea what viewtable does, never seen it before... > The only case I found on Google was in a tutorial which defined viewtable() > as a function, it wasn't in the pgdb module... > >> #sys.stdout.write() > > This writes nothing to stdout. Apparently successfully from your comment > below. > > Try: > > sys.stdout.write(str(i)) Yes that does indeed work like this: for i in (rows): sys.stdout.write(str(i)) But it leaves formatting issues with leaving the command prompt in the middle of the screen. That's not an issue at the moment. >> The code as is will display the data to the console. I have read the >> db API 2.0 at python.org. The problem is viewtable (db) will not work >> or sys.stdout.write () to write the table data to the console screen. >> What am I doing wrong here or what do I need to do different? > > The print is writing to the console I assume? That is the normal method. > >> Can anyone point me to a book specificly for pgdb, python and postgre? > > It doesn't look lie you need a postgres specific boook but just to go > through the standard Python tutorial. It should make most things clear. > >> Last thing is can I define the classes in a separate files and >> reference them? Kinda like code reuse in .Net? I new to Python so >> bear with me. > > Yes, just define them in a file then import that file as a module. The > standard tutorial explains that too. Ok got that I think. Python references the current running directory first then to site packages directory. I found after my initial email your pdf book you had wrote. it seems to have exactly what I am after. The console sqlite addressbook.py application. That is the way I am wantting to do mine. Going to use the way you done but with Postgre and pgdb and see how I do. Thanks for writting the book. It was a big help atleast that part. One last thing is your using "raw_input". What's the differenct between that and "str input"? If the OS is using UTF-8 and the databse is UTF-8 should it really matter? So raw_input could mean any type of input? thankyou From rouerchefani at gmail.com Tue Mar 9 16:31:28 2010 From: rouerchefani at gmail.com (Rafik Ouerchefani) Date: Tue, 9 Mar 2010 16:31:28 +0100 Subject: [Tutor] split a 5 chars word into 4 and 1 Message-ID: Hello, I'm trying to split 5 chars words (codes) into 2 variables. The first must contain the first 4 characters. The second variable will only contain the last character. I'm working in a loop pre-generated codes. Any magic command for this ? :-) Examples of codes : 3cmvA 3cmvB 3cmvC 3cmwA 1g18A 2g88A 1mo4A 2odwA 1u94A 1xmvA 1xp8A 2zr9A Thanks a lot ! a+ -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Tue Mar 9 16:34:45 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 9 Mar 2010 10:34:45 -0500 Subject: [Tutor] split a 5 chars word into 4 and 1 In-Reply-To: References: Message-ID: > Hello, > > I'm trying to split 5 chars words (codes) into 2 variables. The first must > contain the first 4 characters. The second variable will only contain the > last character. I'm working in a loop pre-generated codes. > > Any magic command for this ? :-) > > Examples of codes : > 3cmvA > 3cmvB > 3cmvC > 3cmwA > 1g18A > 2g88A > 1mo4A > 2odwA > 1u94A > 1xmvA > 1xp8A > 2zr9A > > Thanks a lot ! > > a+ > Hmm...This sounds a bit like homework. Can you show us any code that you've already tried? Meantime, you might want to explore Python's slice syntax: http://docs.python.org/tutorial/introduction.html#strings -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Tue Mar 9 16:45:38 2010 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Tue, 9 Mar 2010 10:45:38 -0500 Subject: [Tutor] split a 5 chars word into 4 and 1 In-Reply-To: References: Message-ID: <652641e91003090745i5352def3tbbf9e2278c1bc1c4@mail.gmail.com> On Tue, Mar 9, 2010 at 10:31 AM, Rafik Ouerchefani wrote: > Hello, > > I'm trying to split 5 chars words (codes) into 2 variables. The first must > contain the first 4 characters. The second variable will only contain the > last character. I'm working in a loop pre-generated codes. > > Any magic command for this ? :-) > > Examples of codes : > 3cmvA > 3cmvB > 3cmvC > 3cmwA > 1g18A > 2g88A > 1mo4A > 2odwA > 1u94A > 1xmvA > 1xp8A > 2zr9A > > Thanks a lot ! > > a+ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > What about indexing part1 = word[:4] part2 = word[4] -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Tue Mar 9 17:03:27 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 09 Mar 2010 17:03:27 +0100 Subject: [Tutor] split a 5 chars word into 4 and 1 In-Reply-To: <652641e91003090745i5352def3tbbf9e2278c1bc1c4@mail.gmail.com> References: <652641e91003090745i5352def3tbbf9e2278c1bc1c4@mail.gmail.com> Message-ID: Emad Nawfal (??? ???? ???), 09.03.2010 16:45: > What about indexing > part1 = word[:4] That's slicing. > part2 = word[4] That's indexing. Stefan From marcodrompre at gmail.com Tue Mar 9 19:35:18 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Tue, 9 Mar 2010 13:35:18 -0500 Subject: [Tutor] Macbook Pro+python programming Message-ID: <4bdcec5e1003091035t5d3d340akddbb6f48008e6292@mail.gmail.com> Does anybody know any good python emulator that would work perfectly with Mac OSX Leopard? Because I just got a brand new Mac (the one on apple store) and it seems that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz dual core and each time I open terminal, then type idle and try to use IDLE the screen freezes (something you would never expect with a computer with my specs) and I must force quit the application to be able to continue using my computer. Please someone help me!!!! I'm at university and I always do python in my intro course. Thank you -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Tue Mar 9 19:42:59 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Wed, 10 Mar 2010 00:12:59 +0530 Subject: [Tutor] Macbook Pro+python programming In-Reply-To: <4bdcec5e1003091035t5d3d340akddbb6f48008e6292@mail.gmail.com> References: <4bdcec5e1003091035t5d3d340akddbb6f48008e6292@mail.gmail.com> Message-ID: On Wed, Mar 10, 2010 at 12:05 AM, Marco Rompr? wrote: > Does anybody know any good python emulator that would work perfectly with > Mac OSX Leopard? > Why emulator ? Python 2.6.1 is pre-installed. You can install newer versions too. > > Because I just got a brand new Mac (the one on apple store) and it seems > that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz > dual core and each time I open terminal, then type idle and try to use IDLE > the screen freezes (something you would never expect with a computer with my > specs) and I must force quit the application to be able to continue using my > computer. > The problem you seems is to be having with IDLE. I never had any problem, but you can try vim+bpython, or textmate or other eclipse+pydev. IDLE is a poor choice for coding in python IMHO. > > Please someone help me!!!! > I'm at university and I always do python in my intro course. > Lucky that you do it in your intro course. We had C++ which kinda sucks :( ~l0nwlf -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Tue Mar 9 19:45:07 2010 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Tue, 9 Mar 2010 13:45:07 -0500 Subject: [Tutor] split a 5 chars word into 4 and 1 In-Reply-To: References: <652641e91003090745i5352def3tbbf9e2278c1bc1c4@mail.gmail.com> Message-ID: <652641e91003091045lb06f3f0k31f01411bf9cfa97@mail.gmail.com> On Tue, Mar 9, 2010 at 11:03 AM, Stefan Behnel wrote: > Emad Nawfal (??? ???? ???), 09.03.2010 16:45: > > What about indexing >> part1 = word[:4] >> > > That's slicing. > > part2 = word[4] >> > > That's indexing. > > Stefan > Thanks Stefan. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From brettlea at mac.com Tue Mar 9 20:09:20 2010 From: brettlea at mac.com (Brett Wunderlich) Date: Tue, 09 Mar 2010 13:09:20 -0600 Subject: [Tutor] Macbook Pro+python programming In-Reply-To: References: Message-ID: On Mar 9, 2010, at 12:35 PM, tutor-request at python.org wrote: > Does anybody know any good python emulator that would work perfectly with > Mac OSX Leopard? > > Because I just got a brand new Mac (the one on apple store) and it seems > that the build in Python 2.6.1 from darwin is garbage I'm running a 2,88 ghz > dual core and each time I open terminal, then type idle and try to use IDLE > the screen freezes (something you would never expect with a computer with my > specs) and I must force quit the application to be able to continue using my > computer. Python (and idle) on your MacBook Pro should work just fine. If it does not, you should work on fixing that, as the Mac OS uses Python for various things in the background, especially for installations. I would suggest using Disk Utility (in the /Applications/Utilities folder) to do a "Verify Disk". If it finds problems, you'll need to boot the MacBook Pro from your install disk and use Disk Utility to do a "Repair Disk". If no problems are found then I would start with an Archive and Install of the operating system. As always, before doing anything like the above, have a complete backup of any important data. http://discussions.apple.com is a great place to go for advice on any of the above. Best of luck. From aharrisreid at googlemail.com Wed Mar 10 03:37:13 2010 From: aharrisreid at googlemail.com (Alan Harris-Reid) Date: Wed, 10 Mar 2010 02:37:13 +0000 Subject: [Tutor] SQLite error messages Message-ID: <4B9705D9.2050700@googlemail.com> Hi there, I am using the sqlite3 module with Python 3.1, and have some code which goes something like as follows... import sqlite3 con = sqlite3.connect('MyDatabase.db') try: execresult = con.execute('INSERT INTO MyTable (field_name) VALUES ("MyValue")') con.commit() except: con.rollback() If con.execute() fails, nothing is returned, and although the code correctly executes the rollback next, I have no idea why, and therefore cannot create a suitable error-handler with meaningful messages. I notice from the SQLite website that there are error codes, but it looks like the sqlite3 module is not reporting them. Has anyone any ideas how to get around this problem? TIA, Alan Harris-Reid From transmogribenno at gmail.com Wed Mar 10 04:56:35 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Wed, 10 Mar 2010 12:56:35 +0900 Subject: [Tutor] SQLite error messages In-Reply-To: <4B9705D9.2050700@googlemail.com> References: <4B9705D9.2050700@googlemail.com> Message-ID: <9b00d1a91003091956w7d45a62cw58cd2585feafa62b@mail.gmail.com> On 10 March 2010 11:37, Alan Harris-Reid wrote: > Hi there, > > I am using the sqlite3 module with Python 3.1, and have some code which goes > something like as follows... > > import sqlite3 > con = sqlite3.connect('MyDatabase.db') > > try: > ? execresult = con.execute('INSERT INTO MyTable (field_name) VALUES > ("MyValue")') > ? con.commit() > except: > ? con.rollback() > ? If con.execute() fails, nothing is returned, and although the code > correctly executes the rollback next, I have no idea why, and therefore > cannot create a suitable error-handler with meaningful messages. > I notice from the SQLite website that there are error codes, but it looks > like the sqlite3 module is not reporting them. Do you mean numerical error codes? Which page on the SQLite website are you referring to? Certainly the exception contains usable data. Try something like this: try: execresult = con.execute('INSERT INTO MyTable (field_name) VALUES ("MyValue")') con.commit() except Exception as error: print("Didn't work:", error) con.rollback() (I didn't create a table, so I get "Didn't work: no such table: MyTable") HTH, benno From vincent at vincentdavis.net Wed Mar 10 05:44:38 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Tue, 9 Mar 2010 21:44:38 -0700 Subject: [Tutor] Difflib comparing string sequnces Message-ID: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> I have never used the difflib or similar and have a few questions. I am working with DNA sequences of length 25. I have a list of 230,000 and need to look for each sequence in the entire genome (toxoplasma parasite) I am not sure how large the genome is but more that 230,000 sequences. The are programs that do this and really fast, and they eve do partial matches but not quite what I need. So I am looking to build a custom solution. I need to look for each of my sequences of 25 characters example( AGCCTCCCATGATTGAACAGATCAT). The genome is formatted as a continuos string (CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG.........) I don't care where or how many times on if it exists. This is simple I think, str.find(AGCCTCCCATGATTGAACAGATCAT) But I also what to find a close match defined as only wrong at 1 location and I what to record the location. I am not sure how do do this. The only thing I can think of is using a wildcard and performing the search with a wildcard in each position. ie 25 time. For example AGCCTCCCATGATTGAACAGATCAT AGCCTCCCATGATAGAACAGATCAT close match with a miss-match at position 13 *Vincent Davis 720-301-3003 * vincent at vincentdavis.net my blog | LinkedIn -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Wed Mar 10 07:38:50 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 10 Mar 2010 07:38:50 +0100 Subject: [Tutor] SQLite error messages In-Reply-To: <4B9705D9.2050700@googlemail.com> References: <4B9705D9.2050700@googlemail.com> Message-ID: <1268203130.3836.4.camel@Nokia-N900-51-1> ----- Original message ----- > I am using the sqlite3 module with Python 3.1, and have some code which > goes something like as follows... > > import sqlite3 > con = sqlite3.connect('MyDatabase.db') > > try: >? ? ? ? execresult = con.execute('INSERT INTO MyTable (field_name) VALUES > ("MyValue")') >? ? ? ? con.commit() > except: Here you catch all exceptions. Normally you would catch a specific exception like ValueError. >? ? ? ? con.rollback() > Do you know finally? It is run after all the exceptions have been handled and this is where I would put the rollback. Greets, Sander From alan.gauld at btinternet.com Wed Mar 10 01:52:02 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 10 Mar 2010 00:52:02 -0000 Subject: [Tutor] pgdb and console output References: <4f23445b1003071309p73c2a217p4d3c3a9653459d6a@mail.gmail.com> Message-ID: "pylist1" wrote >> sys.stdout.write(str(i)) > > Yes that does indeed work like this: > > But it leaves formatting issues with leaving the command prompt in the > middle of the screen. That's not an issue at the moment. Yes, stdout.write has no formatting if you need spaces or newlines you have to explicitly provide them. That's why mosty folks use print combined with a format string to get formatted output. > I found after my initial email your pdf book you had wrote. ... > Thanks for writing the book. You're welcome but the web pages are more up top date than the pdf file. I only generate the pdf after major updates but the html files get updates fairly regularly. The zip files get updates somewhere in between > One last thing is your using "raw_input". What's the differenct between > that and "str input"? If the OS is using UTF-8 and the databse is UTF-8 > should it really matter? So raw_input could mean any type of input? I don;t know what "str input" refers to but raw_input is the standard method in Python v2 of reading characters from stdin. Its called raw because it reads the raw characters, it does not try to interpret them. Thus if you know its a number you use int() to convert the string to an integer value In Python v3 raw_input has been renamed to input() See the Talking to the User topic in my tutorial for more detail on the differences. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Mar 10 02:12:56 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 10 Mar 2010 01:12:56 -0000 Subject: [Tutor] Macbook Pro+python programming References: Message-ID: "Brett Wunderlich" wrote >> Because I just got a brand new Mac (the one on apple store) and it seems >> that the build in Python 2.6.1 from darwin is garbage Thats unlikely, but the configuration may be faulty. >> dual core and each time I open terminal, then type idle and try to use >> IDLE >> the screen freezes What happens if you type $ python /full/path/to/idle.py Where /full/path./... is the actualm path to idle.py? Does that work. > > (something you would never expect with a computer with my >> specs) and I must force quit the application to be able to continue I don;t expect that on my old 600MHz G3 iBook - almost 10 years old and running Python 2.5 and Idle quite happily! > Python (and idle) on your MacBook Pro should work just fine. > If it does not, you should work on fixing that, as the Mac OS uses > Python for various things in the background, especially for > installations. It could well be that Python is working fine and only the idle config is faulty. It could be as simple as file permissions. What happens if you use sudo to start idle? > I would suggest using Disk Utility (in the /Applications/Utilities > folder) > to do a "Verify Disk". If it finds problems, you'll need to boot the > MacBook Pro from your install disk and use Disk Utility to do > a "Repair Disk". I'd be amazed if that was needed. > If no problems are found then I would start with an Archive and > Install of the operating system. And I'd definitely keep that as a last resort! Its much more likely that you need to setup PYTHONPATH or even PATH, or set permissions differently. My first check would be that python itself works ok by just typing python in the Terminal app and checking you get a working >>> prompt. If that works try starting IDLE using the full path and explicitly calling python as described above. If that works then check your PATH/PYTHONPATH settings -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From anothernetfellow at gmail.com Wed Mar 10 14:40:43 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 10 Mar 2010 14:40:43 +0100 Subject: [Tutor] Encoding In-Reply-To: <4B94663F.3060307@ieee.org> References: <23ce85921003030036i69a4a92ei81a908a294ff6ca2@mail.gmail.com> <4B913530.6030003@ieee.org> <23ce85921003050915p1a084c0co73d973282d8fb6ad@mail.gmail.com> <4B915AE5.2010000@ieee.org> <23ce85921003060927v1140e4c9u9a087a1b7ca74fef@mail.gmail.com> <20100307082837.54fc6389@o> <23ce85921003070423o226184dfjbb826ac645cc1fb2@mail.gmail.com> <4B94007A.1060809@ieee.org> <23ce85921003071213q520c15c8w6ce7a09d4a55631d@mail.gmail.com> <4B94663F.3060307@ieee.org> Message-ID: <23ce85921003100540i3b14da18ycc491a3caadc6696@mail.gmail.com> 2010/3/8 Dave Angel > >> You still didn't provide the full context. Are you trying to do store > binary data, or not? > Yes i think ti's binary data. I'm just reading with file.read a JPG image. Stefan: yes, read that tutorial :) Giorgio > > > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From anothernetfellow at gmail.com Wed Mar 10 14:43:49 2010 From: anothernetfellow at gmail.com (Giorgio) Date: Wed, 10 Mar 2010 14:43:49 +0100 Subject: [Tutor] WSGI / Apache In-Reply-To: <9b00d1a91003080420ve42c60dy75983857b98fb898@mail.gmail.com> References: <23ce85921003041137s1c1d9c85rfa5c1e7685ff9f23@mail.gmail.com> <9b00d1a91003080420ve42c60dy75983857b98fb898@mail.gmail.com> Message-ID: <23ce85921003100543lc5b7637la06886f2264042c0@mail.gmail.com> 2010/3/8 Benno Lang > > > WSGI is based on CGI, so I imagine that it's not too difficult to have > a wrapper that converts between the protocols. I haven't tried > wsgiref, though. > Yes Benno it isn't. It seems that mod_wsgi is the most used alternative. > > > Do you have any idea i should use mod_wsgi instead of wsgiref + mod_cgi? > > From your application's perspective, if it talks WSGI it shouldn't > make any difference. But if you're using GAE, what good will it do to > have a local Apache server anyway? > Ok, that was a general question, not GAE related. Someone says that mod_wsgi is faster than wsgiref + cgi. > > Cheers, > benno > -- -- AnotherNetFellow Email: anothernetfellow at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From kushalnitt at gmail.com Wed Mar 10 15:36:17 2010 From: kushalnitt at gmail.com (kushal gupta) Date: Wed, 10 Mar 2010 20:06:17 +0530 Subject: [Tutor] Timezone issues with datetime module. Message-ID: Hi, I am facing an issue with datetime module with some timezones (especially day light savings). Given the date and the timezone, I want to convert it to the UTC date and time. Here is the snippet: import datetime import mx.DateTime from dateutil import zoneinfo def parse_date(date, tzname): feed_time = mx.DateTime.strptime(date, '%d-%m-%Y') feed_datetime = datetime.datetime(*feed_time.timetuple()[:6], tzinfo=zoneinfo.gettz(tzname)) feed_time = mx.DateTime.DateTime(*feed_datetime.utctimetuple()[:6]) return feed_time >>>parse_date('28-03-2010', 'Australia/Melbourne') >>> parse_date('29-03-2010', 'Australia/Melbourne') which says day light savings ended on 28th March (seeing at the outputs, there is a difference of 1 hr in time) but it actually ended on April 4th http://www.timeanddate.com/worldclock/city.html?n=152 for 'America/Anchorage' the same code works fine (DST starts on 14th March 02:00 hours) http://www.timeanddate.com/worldclock/city.html?n=18 >>> parse_date('14-03-2010', 'America/Anchorage') >>> parse_date('15-03-2010', 'America/Anchorage') What am i missing here? Please help. Thanks Kushal. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aharrisreid at googlemail.com Wed Mar 10 20:32:16 2010 From: aharrisreid at googlemail.com (Alan Harris-Reid) Date: Wed, 10 Mar 2010 19:32:16 +0000 Subject: [Tutor] SQLite error messages In-Reply-To: <9b00d1a91003091956w7d45a62cw58cd2585feafa62b@mail.gmail.com> References: <4B9705D9.2050700@googlemail.com> <9b00d1a91003091956w7d45a62cw58cd2585feafa62b@mail.gmail.com> Message-ID: <4B97F3C0.2030703@googlemail.com> Benno Lang wrote: > On 10 March 2010 11:37, Alan Harris-Reid wrote: > >> Hi there, >> >> I am using the sqlite3 module with Python 3.1, and have some code which goes >> something like as follows... >> >> import sqlite3 >> con = sqlite3.connect('MyDatabase.db') >> >> try: >> execresult = con.execute('INSERT INTO MyTable (field_name) VALUES >> ("MyValue")') >> con.commit() >> except: >> con.rollback() >> If con.execute() fails, nothing is returned, and although the code >> correctly executes the rollback next, I have no idea why, and therefore >> cannot create a suitable error-handler with meaningful messages. >> I notice from the SQLite website that there are error codes, but it looks >> like the sqlite3 module is not reporting them. >> > > Do you mean numerical error codes? Which page on the SQLite website > are you referring to? Certainly the exception contains usable data. > Try something like this: > > try: > execresult = con.execute('INSERT INTO MyTable (field_name) VALUES > ("MyValue")') > con.commit() > except Exception as error: > print("Didn't work:", error) > con.rollback() > > (I didn't create a table, so I get "Didn't work: no such table: MyTable") > > HTH, > benno Hi Benno, your example is great - just what I needed! Regarding SQLite error codes, the list I was referring to is at www.sqlite.org/c3ref/c_abort.html , but it doesn't look complete because I have already come-across some IntegrityError messages which aren't on the list. Regards, Alan -------------- next part -------------- An HTML attachment was scrubbed... URL: From aharrisreid at googlemail.com Wed Mar 10 21:02:22 2010 From: aharrisreid at googlemail.com (Alan Harris-Reid) Date: Wed, 10 Mar 2010 20:02:22 +0000 Subject: [Tutor] SQLite error messages In-Reply-To: <1268203130.3836.4.camel@Nokia-N900-51-1> References: <4B9705D9.2050700@googlemail.com> <1268203130.3836.4.camel@Nokia-N900-51-1> Message-ID: <4B97FACE.7000909@googlemail.com> Sander Sweers wrote: > ----- Original message ----- > >> I am using the sqlite3 module with Python 3.1, and have some code which >> goes something like as follows... >> >> import sqlite3 >> con = sqlite3.connect('MyDatabase.db') >> >> try: >> execresult = con.execute('INSERT INTO MyTable (field_name) VALUES >> ("MyValue")') >> con.commit() >> except: >> > > Here you catch all exceptions. Normally you would catch a specific exception like ValueError. > >> con.rollback() >> >> > > Do you know finally? It is run after all the exceptions have been handled and this is where I would put the rollback. > > Greets, > Sander Hello Sander, thanks for the reply. "Normally you would catch a specific exception like ValueError." Agreed, but as I don't know what type the exception is, I would have to provide a suitable error message for all exception types (ValueError, IntegrityError, etc.). At this stage catching Exception as errormessage is sufficient for my purposes. "Do you know finally? It is run after all the exceptions have been handled and this is where I would put the rollback." In this case there is no 'finally' section, because if the 'try' section doesn't work, then I want the rollback to occur for *all *exceptions. Maybe I have misunderstood you, but I always thought that the 'finally' section was run even if the 'try' section is successful, in which case I would not want a rollback. (According to the Python documentation (section 8.6) "A /finally clause/ is always executed before leaving the try statement, whether an exception has occurred or not."). Regards, Alan -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Wed Mar 10 21:37:20 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 10 Mar 2010 21:37:20 +0100 Subject: [Tutor] SQLite error messages In-Reply-To: <4B97FACE.7000909@googlemail.com> References: <4B9705D9.2050700@googlemail.com> <1268203130.3836.4.camel@Nokia-N900-51-1> <4B97FACE.7000909@googlemail.com> Message-ID: On 10 March 2010 21:02, Alan Harris-Reid wrote: > Maybe I have misunderstood you, but I always thought that the 'finally' > section was run even if the 'try' section is successful, in which case I > would not want a rollback. I was thinking something like this. import sqlite3 con = sqlite3.connect('MyDatabase.db') execresult = None try: execresult = con.execute('INSERT INTO MyTable (field_name) VALUES ("MyValue")') con.commit() finally: if not execresult: print 'Rollback' con.rollback() This way you can have a rollback and still see an exception. Greets Sander From rarmstro at water.ca.gov Wed Mar 10 22:20:19 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Wed, 10 Mar 2010 13:20:19 -0800 Subject: [Tutor] Running a dos program with python Message-ID: Hello all, This is my first post to the Tutor at python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r"c:\shake91.txt") but cannot do the rest. When I run my script import os os.system(r"c:\shake91.exe") In the IPython(x,y) console I see: ------------------------------------------------------------------------ ------------------ ***************************************************** * SHAKE -- A COMPUTER PROGRAM FOR EARTHQUAKE RESPONSE * * ANALYSIS OF HORIZONTALLY LAYERED SITES * * by: Per B. Schnabel & John Lysmer -- 1970 * * ------------------------------------------------------- * * shake85 IBM-PC version of SHAKE * * by: S.S. (Willie) Lai, January 1985 * * ------------------------------------------------------- * * shake88 : New modulus reduction curves for clays added* * using results from Sun et al (1988) * * by: J. I. Sun & Ramin Golesorkhi * * February 26, 1988 * * ------------------------------------------------------- * * SHAKE90/91: Adjust last iteration; Input now is either * * Gmax or max Vs; up to 13 material types can * * be specified by user; up to 50 Layers can * * be specified; object motion can be read in * * from a separate file and can have user * * specified format; Different periods for * * response spectral calculations; options * * are renumbered; and general cleanup * * by: J. I. Sun, I. M. Idriss & P. Dirrim * * June 1990 - February 1991 * * ------------------------------------------------------- * * SHAKE91 : General cleanup and finalization of input/ * * output format ... etc * * by: I. M. Idriss * * December 1991 * *********************************************************** Name of Input File = ------------------------------------------------------------------------ ------------------ And there is a blinking cursor after Name of Input File. At this point I can manually enter in the file name (and the two other remaining file names) and then press enter and the program does run. What I really want though is to be able to do the whole thing with a python script. Any ideas? Thanks Richie -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Wed Mar 10 23:23:58 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Wed, 10 Mar 2010 16:23:58 -0600 Subject: [Tutor] Running a dos program with python In-Reply-To: References: Message-ID: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. < rarmstro at water.ca.gov> wrote: > Hello all, > > > > This is my first post to the Tutor at python.org mailing list. I am in the > process of switching from Matlab to Python and there is one task that I am > having a hard time doing and cannot find the answer on the web. I want to > write a script in python that will open up a windows dos program, send three > inputs (file names) into program and then run it. I know one way to open up > the dos program with os.system(r?c:\shake91.txt?) but cannot do the rest. > Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From rarmstro at water.ca.gov Wed Mar 10 23:51:17 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Wed, 10 Mar 2010 14:51:17 -0800 Subject: [Tutor] Running a dos program with python References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> Message-ID: From: srilyk at gmail.com [mailto:srilyk at gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. wrote: Hello all, This is my first post to the Tutor at python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r"c:\shake91.txt") but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three "enters" into the script? Thanks, Richie -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricaraoz at gmail.com Wed Mar 10 23:57:23 2010 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Wed, 10 Mar 2010 19:57:23 -0300 Subject: [Tutor] Difflib comparing string sequnces In-Reply-To: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> References: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> Message-ID: <4B9823D3.5030209@gmail.com> Vincent Davis wrote: > I have never used the difflib or similar and have a few questions. > I am working with DNA sequences of length 25. I have a list of 230,000 > and need to look for each sequence in the entire genome (toxoplasma > parasite) I am not sure how large the genome is but more that 230,000 > sequences. > The are programs that do this and really fast, and they eve do partial > matches but not quite what I need. So I am looking to build a custom > solution. > I need to look for each of my sequences of 25 characters > example(AGCCTCCCATGATTGAACAGATCAT). > The genome is formatted as a continuos string > (CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG.........) > > I don't care where or how many times on if it exists. This is simple I > think, str.find(AGCCTCCCATGATTGAACAGATCAT) > > But I also what to find a close match defined as only wrong at 1 > location and I what to record the location. I am not sure how do do > this. The only thing I can think of is using a wildcard and performing > the search with a wildcard in each position. ie 25 time. > For example > AGCCTCCCATGATTGAACAGATCAT > AGCCTCCCATGATAGAACAGATCAT > close match with a miss-match at position 13 Untested : genome = 'CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG........' sequence = 'AGGCTTGCGGAGCCTGAGGGCGGAG' import fnmatch for i in range(len(sequence)): match = '*' + sequence[0:i] + '?' + sequence[i+1:] + '*' if fnmatch.fnmatch(genome, match) print 'It matches' -------------- next part -------------- An HTML attachment was scrubbed... URL: From ricaraoz at gmail.com Wed Mar 10 23:57:53 2010 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Wed, 10 Mar 2010 19:57:53 -0300 Subject: [Tutor] Difflib comparing string sequnces In-Reply-To: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> References: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> Message-ID: <4B9823F1.4070506@gmail.com> Vincent Davis wrote: > I have never used the difflib or similar and have a few questions. > I am working with DNA sequences of length 25. I have a list of 230,000 > and need to look for each sequence in the entire genome (toxoplasma > parasite) I am not sure how large the genome is but more that 230,000 > sequences. > The are programs that do this and really fast, and they eve do partial > matches but not quite what I need. So I am looking to build a custom > solution. > I need to look for each of my sequences of 25 characters > example(AGCCTCCCATGATTGAACAGATCAT). > The genome is formatted as a continuos string > (CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG.........) > > I don't care where or how many times on if it exists. This is simple I > think, str.find(AGCCTCCCATGATTGAACAGATCAT) > > But I also what to find a close match defined as only wrong at 1 > location and I what to record the location. I am not sure how do do > this. The only thing I can think of is using a wildcard and performing > the search with a wildcard in each position. ie 25 time. > For example > AGCCTCCCATGATTGAACAGATCAT > AGCCTCCCATGATAGAACAGATCAT > close match with a miss-match at position 13 also : sequence = 'AGGCTTGCGGAGCCTGAGGGCGGAG' seqList = ['*' + sequence[0:i] + '?' + sequence[i+1:] + '*' for i in range(len(sequence))] import fnmatch genome = 'CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG........' if any(fnmatch.fnmatch(genome, i) for i in seqList) print 'It matches' Which might be better if the sequence is fixed and the genome changes inside a loop. HTH -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Wed Mar 10 23:59:52 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Wed, 10 Mar 2010 16:59:52 -0600 Subject: [Tutor] Running a dos program with python In-Reply-To: References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> Message-ID: <333efb451003101459i54928dbcpb411f50d22bba4f6@mail.gmail.com> On Wed, Mar 10, 2010 at 4:51 PM, Armstrong, Richard J. < rarmstro at water.ca.gov> wrote: > > > The dos program pops up and if I hit the enter key three times then it > runs. How can I add these three ?enters? into the script? > I'm not at all sure if this way would work, but you could send the \r\n through a pipe: p = subprocess.Popen([file1, file2, file3], stdin=subprocess.PIPE) p.communicate("\r\n\r\n\r\n") # Three windows line ending sequences. it also may be possible to add them to the end of the last parameter: 'b.txt\r\n\r\n\r\n' I don't have much faith that it will work, but you can certainly try! HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From randyeraymond at mchsi.com Thu Mar 11 00:05:46 2010 From: randyeraymond at mchsi.com (Randy Raymond) Date: Wed, 10 Mar 2010 17:05:46 -0600 Subject: [Tutor] Running a dos program with python In-Reply-To: References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> Message-ID: <7FBF080ED36A4A6CBB761E28A60F1F0E@RandyPC> I use wxPython, which allows a statement like: wx.Execute('c:\shake91.exe "FLAC.txt" "a.txt" "b.txt"') From: Armstrong, Richard J. Sent: Wednesday, March 10, 2010 4:51 PM To: Wayne Werner Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python From: srilyk at gmail.com [mailto:srilyk at gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. wrote: Hello all, This is my first post to the Tutor at python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r"c:\shake91.txt") but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three "enters" into the script? Thanks, Richie -------------------------------------------------------------------------------- _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From rarmstro at water.ca.gov Thu Mar 11 00:06:48 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Wed, 10 Mar 2010 15:06:48 -0800 Subject: [Tutor] Running a dos program with python References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> <333efb451003101459i54928dbcpb411f50d22bba4f6@mail.gmail.com> Message-ID: From: srilyk at gmail.com [mailto:srilyk at gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 3:00 PM To: Armstrong, Richard J. Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 4:51 PM, Armstrong, Richard J. wrote: The dos program pops up and if I hit the enter key three times then it runs. How can I add these three "enters" into the script? I'm not at all sure if this way would work, but you could send the \r\n through a pipe: p = subprocess.Popen([file1, file2, file3], stdin=subprocess.PIPE) p.communicate("\r\n\r\n\r\n") # Three windows line ending sequences. it also may be possible to add them to the end of the last parameter: 'b.txt\r\n\r\n\r\n' I don't have much faith that it will work, but you can certainly try! HTH, Wayne Wayne thank you so much! This worked beautifully: import subprocess p = subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt'], stdin=subprocess.PIPE) p.communicate("\r\n\r\n\r\n") # Three windows line ending sequences. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rarmstro at water.ca.gov Thu Mar 11 00:22:39 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Wed, 10 Mar 2010 15:22:39 -0800 Subject: [Tutor] Running a dos program with python References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> <7FBF080ED36A4A6CBB761E28A60F1F0E@RandyPC> Message-ID: Thanks Randy, maybe I should check out wxPython. From: tutor-bounces+rarmstro=water.ca.gov at python.org [mailto:tutor-bounces+rarmstro=water.ca.gov at python.org] On Behalf Of Randy Raymond Sent: Wednesday, March 10, 2010 3:06 PM To: Tutor Python Subject: Re: [Tutor] Running a dos program with python I use wxPython, which allows a statement like: wx.Execute('c:\shake91.exe "FLAC.txt" "a.txt" "b.txt"') From: Armstrong, Richard J. Sent: Wednesday, March 10, 2010 4:51 PM To: Wayne Werner Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python From: srilyk at gmail.com [mailto:srilyk at gmail.com] On Behalf Of Wayne Werner Sent: Wednesday, March 10, 2010 2:24 PM To: Armstrong, Richard J. Cc: tutor at python.org Subject: Re: [Tutor] Running a dos program with python On Wed, Mar 10, 2010 at 3:20 PM, Armstrong, Richard J. wrote: Hello all, This is my first post to the Tutor at python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r"c:\shake91.txt") but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three "enters" into the script? Thanks, Richie _____ _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwalsh at mwalsh.org Thu Mar 11 00:12:47 2010 From: mwalsh at mwalsh.org (Martin Walsh) Date: Wed, 10 Mar 2010 17:12:47 -0600 Subject: [Tutor] Running a dos program with python In-Reply-To: References: <333efb451003101423p42086d5cwbd2f065c815a1752@mail.gmail.com> Message-ID: <4B98276F.4060209@mwalsh.org> Armstrong, Richard J. wrote: > Hello all, > > This is my first post to the Tutor at python.org > mailing list. I am in the process of switching from Matlab to Python and > there is one task that I am having a hard time doing and cannot find the > answer on the web. I want to write a script in python that will open up > a windows dos program, send three inputs (file names) into program and > then run it. I know one way to open up the dos program with > os.system(r?c:\shake91.txt?) but cannot do the rest. > > Use the subprocess module: > > http://docs.python.org/library/subprocess.html > > untested, but should work: > subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) > > if you want to communicate with the process you can add , > stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. > > Check the docs for more info. > > HTH, > > Wayne >> >> > Wayne, > > It kindof works. I wrote > subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) > > The dos program pops up and if I hit the enter key three times then it > runs. How can I add these three ?enters? into the script? Then perhaps something more like this (untested) ... from subprocess import Popen, PIPE proc = Popen([r'c:\shake91.exe'], stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate('FLAC.txt\na.txt\nb.txt\n') If that doesn't work you may need something like 'expect' for windows (or, pexpect and cygwin). HTH, Marty From rarmstro at water.ca.gov Wed Mar 10 20:33:46 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Wed, 10 Mar 2010 11:33:46 -0800 Subject: [Tutor] Opening a dos exe Message-ID: Hello, There is one task that I am trying to do but I cannot find the answer on the web - I hope that you can help. It is a rather simple problem. I want to open up a dos program and then run it. I can do that part with os.system. The problem comes in that the dos program requires three inputs (input1.txt, input2.txt and input3.txt - see attached picture) but I cannot find a way of getting this information to the dos program from python. Any ideas? Thank you, Richard Armstrong -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Screenshot.pdf Type: application/octet-stream Size: 352802 bytes Desc: Screenshot.pdf URL: From vincent at vincentdavis.net Thu Mar 11 01:33:24 2010 From: vincent at vincentdavis.net (Vincent Davis) Date: Wed, 10 Mar 2010 17:33:24 -0700 Subject: [Tutor] Difflib comparing string sequnces In-Reply-To: <4B9823F1.4070506@gmail.com> References: <77e831101003092044s6e93e7aav601270f843d1d4a1@mail.gmail.com> <4B9823F1.4070506@gmail.com> Message-ID: <77e831101003101633t434e0d7aj85b5bfea67d0c039@mail.gmail.com> @Ricardo Ar?oz Thanks for your response, Before I saw your response I had posted the question on stack overflow. See link below. I like your solution better than the re solution posted. It looks like this task may take longer than I think. The .re solution I guess might take more than 10 days. The search string in 80million digits long. But Obviously I can stop once I find a match and then just move on the the next sequence. You might what to post this answer on stackoverflow. I like the more interactive form of a mailing list but there seems to be a very p\broad audience on stackoverflow. Thanks again, http://stackoverflow.com/questions/2420412/search-for-string-allowing-for-one-mismatches-in-any-location-of-the-string-pyth *Vincent Davis 720-301-3003 * vincent at vincentdavis.net my blog | LinkedIn 2010/3/10 Ricardo Ar?oz > Vincent Davis wrote: > > I have never used the difflib or similar and have a few questions. > I am working with DNA sequences of length 25. I have a list of 230,000 and > need to look for each sequence in the entire genome (toxoplasma parasite) I > am not sure how large the genome is but more that 230,000 sequences. > The are programs that do this and really fast, and they eve do partial > matches but not quite what I need. So I am looking to build a custom > solution. > I need to look for each of my sequences of 25 characters example( > AGCCTCCCATGATTGAACAGATCAT). > The genome is formatted as a continuos string > (CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG.........) > > I don't care where or how many times on if it exists. This is simple I > think, str.find(AGCCTCCCATGATTGAACAGATCAT) > > But I also what to find a close match defined as only wrong at 1 location > and I what to record the location. I am not sure how do do this. The only > thing I can think of is using a wildcard and performing the search with a > wildcard in each position. ie 25 time. > For example > AGCCTCCCATGATTGAACAGATCAT > AGCCTCCCATGATAGAACAGATCAT > close match with a miss-match at position 13 > > > also : > > sequence = 'AGGCTTGCGGAGCCTGAGGGCGGAG' > seqList = ['*' + sequence[0:i] + '?' + sequence[i+1:] + '*' for i in > range(len(sequence))] > import fnmatch > > genome = 'CATGGGAGGCTTGCGGAGCCTGAGGGCGGAGCCTGAGGTGGGAGGCTTGCGGAG........' > if any(fnmatch.fnmatch(genome, i) for i in seqList) > print 'It matches' > > Which might be better if the sequence is fixed and the genome changes > inside a loop. > > HTH > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dkdropbox at gmail.com Thu Mar 11 03:17:12 2010 From: dkdropbox at gmail.com (David Kim) Date: Wed, 10 Mar 2010 21:17:12 -0500 Subject: [Tutor] Feedparser and Google News feeds Message-ID: I have been working through some of the examples in the Programming Collective Intelligence book by Toby Segaran. I highly recommend it, btw. Anyway, some of the exercises use feedparser to pull in RSS/Atom feeds from different sources (before doing more interesting things). The algorithm stuff I pretty much follow, but one thing is driving me CRAZY: I can't seem to pull more than 10 items from a google news feed. For example, I'd like to pull 1000 google news items (using some search term, let's say 'lightsabers'). The associated atom feed url, however, only holds ten items. And its hard to do some of the clustering analysis with only ten items! Anyway, I imagine this must be a straightforward thing and I'm being a moron, but I don't know where else to ask this question (none of my friends are web-savvy programmers). I did see some posts about an n=100 term one can add to the url (the limit seems to be 100 items), but it only seems to effect the webpage view and not the feed. I've also tried subscribing to the feed in Google Reader and making the feed public, but I seem to be running into the same problem. Is this a feedparser thing or a google thing? The url I'm using is http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&as_scoring=r&as_maxm=3&q=health+information+exchange&as_qdr=a&as_drrb=q&as_mind=8&as_minm=2&cf=all&as_maxd=100&output=rss Can anyone help me? I'm tearing my hair out and want to choke my computer. It's probably not relevant, but I'm running Snow Leopard and Python 2.6 (actually EPD 6.1). -------------- next part -------------- An HTML attachment was scrubbed... URL: From dkdropbox at gmail.com Thu Mar 11 03:35:54 2010 From: dkdropbox at gmail.com (DK) Date: Thu, 11 Mar 2010 02:35:54 +0000 (UTC) Subject: [Tutor] Timezone issues with datetime module. References: Message-ID: kushal gupta gmail.com> writes: > > Hi,? I am facing an issue with datetime module with some timezones You may want to check out the pytz library at http://pytz.sourceforge.net/ for easier timezone manipulation. From davidkim05 at gmail.com Thu Mar 11 03:06:22 2010 From: davidkim05 at gmail.com (David Kim) Date: Wed, 10 Mar 2010 21:06:22 -0500 Subject: [Tutor] Feedparser and google news/google reader Message-ID: I have been working through some of the examples in the Programming Collective Intelligence book by Toby Segaran. I highly recommend it, btw. Anyway, one of the simple exercises required is using feedparser to pull in RSS/Atom feeds from different sources (before doing more interesting things). The algorithm stuff I pretty much follow, but one thing is driving me CRAZY. I can't seem to pull more than 10 items from a google news feed. For example, I'd like to pull 1000 google news items (using some search term, let's say 'lightsabers'). The associated atom feed url, however, only holds ten items. And its hard to do some of the clustering exercises with only ten items! Anyway, I imagine this must be a straightforward thing and I'm being a moron, but I don't know where else to ask this question. I did see some posts about an n=100 term one can add to the url (the limit seems to be 100 items), but it only seems to effect the webpage view and not the feed. I've also tried subscribing to the feed in Google Reader and making the feed public, but I seem to be running into the same problem. Is this a feedparser thing or a google thing? The url I'm using is http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&as_scoring=r&as_maxm=3&q=health+information+exchange&as_qdr=a&as_drrb=q&as_mind=8&as_minm=2&cf=all&as_maxd=100&output=rss Can anyone help me? I'm tearing my hair out and want to choke my computer. It's probably not relevant, but I'm running Snow Leopard and Python 2.6 (actually EPD 6.1). -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Mar 11 08:48:51 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Mar 2010 07:48:51 -0000 Subject: [Tutor] Feedparser and google news/google reader References: Message-ID: "David Kim" wrote > me CRAZY. I can't seem to pull more than 10 items from a google news > feed. > For example, I'd like to pull 1000 google news items (using some search > term, let's say 'lightsabers'). The associated atom feed url, however, > only > holds ten items. And its hard to do some of the clustering exercises with > only ten items! I have no idea if this is relevantt and without code I suspect we will all be guessing blindly but... Have you checked Google's terms of use? I know they make it hard to screen scrape their search engine so they may have similar limits on their feeds. Just a thought. Alan G. From c.t.matsumoto at gmail.com Thu Mar 11 13:01:53 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Thu, 11 Mar 2010 13:01:53 +0100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B8EA2D9.6050302@ieee.org> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> Message-ID: <4B98DBB1.6070401@gmail.com> Dave Angel wrote: > (You forgot to do a Reply-All, so your message went to just me, rather > than to me and the list ) > > > C.T. Matsumoto wrote: >> Dave Angel wrote: >>> C.T. Matsumoto wrote: >>>> Hello, >>>> >>>> This is follow up on a question I had about algorithms. In the >>>> thread it was suggested I make my own sorting algorithm. >>>> >>>> Here are my results. >>>> >>>> #!/usr/bin/python >>>> >>>> def sort_(list_): >>>> for item1 in list_: >>>> pos1 = list_.index(item1) >>>> pos2 = pos1 + 1 >>>> try: >>>> item2 = list_[pos2] >>>> except IndexError: >>>> pass >>>> >>>> if item1 >= item2: >>>> try: >>>> list_.pop(pos2) >>>> list_.insert(pos1, item2) >>>> return True >>>> except IndexError: >>>> pass >>>> >>>> def mysorter(list_): >>>> while sort_(list_) is True: >>>> sort_(list_) >>>> >>>> I found this to be a great exercise. In doing the exercise, I got >>>> pretty stuck. I consulted another programmer (my dad) who described >>>> how to go about sorting. As it turned out the description he >>>> described was the Bubble sort algorithm. Since coding the solution I >>>> know the Bubble sort is inefficient because of repeated iterations >>>> over the entire list. This shed light on the quick sort algorithm >>>> which I'd like to have a go at. >>>> >>>> Something I haven't tried is sticking in really large lists. I was >>>> told that with really large list you break down the input list into >>>> smaller lists. Sort each list, then go back and use the same >>>> swapping procedure for each of the different lists. My question is, >>>> at what point to you start breaking things up? Is that based on list >>>> elements or is it based on memory(?) resources python is using? >>>> >>>> One thing I'm not pleased about is the while loop and I'd like to >>>> replace it with a for loop. >>>> >>>> Thanks, >>>> >>>> T >>>> >>>> >>> There are lots of references on the web about Quicksort, including a >>> video at: >>> http://www.youtube.com/watch?v=y_G9BkAm6B8 >>> >>> which I think illustrates it pretty well. It would be a great >>> learning exercise to implement Python code directly from that >>> description, without using the sample C++ code available. >>> >>> (Incidentally, there are lots of variants of Quicksort, so I'm not >>> going to quibble about whether this is the "right" one to be called >>> that.) >>> >>> I don't know what your earlier thread was, since you don't mention >>> the subject line, but there are a number of possible reasons you >>> might not have wanted to use the built-in sort. The best one is for >>> educational purposes. I've done my own sort for various reasons in >>> the past, even though I had a library function, since the library >>> function had some limits. One time I recall, the situation was that >>> the library sort was limited to 64k of total data, and I had to work >>> with much larger arrays (this was in 16bit C++, in "large" model). I >>> solved the size problem by using the C++ sort library on 16k subsets >>> (because a pointer was 2*2 bytes). Then I merged the results of the >>> sorts. At the time, and in the circumstances involved, there were >>> seldom more than a dozen or so sublists to merge, so this approach >>> worked well enough. >>> >>> Generally, it's better for both your development time and the >>> efficiency and reliabilty of the end code, to base a new sort >>> mechanism on the existing one. In my case above, I was replacing >>> what amounted to an insertion sort, and achieved a 50* improvement >>> for a real customer. It was fast enough that other factors >>> completely dominated his running time. >>> >>> But for learning purposes? Great plan. So now I'll respond to your >>> other questions, and comment on your present algorithm. >>> >>> It would be useful to understand about algorithmic complexity, the so >>> called Order Function. In a bubble sort, if you double the size of >>> the array, you quadruple the number of comparisons and swaps. It's >>> order N-squared or O(n*n). So what works well for an array of size >>> 10 might take a very long time for an array of size 10000 (like a >>> million times as long). You can do much better by sorting smaller >>> lists, and then combining them together. Such an algorithm can be >>> O(n*log(n)). >>> >>> >>> You ask at what point you consider sublists? In a language like C, >>> the answer is when the list is size 3 or more. For anything larger >>> than 2, you divide into sublists, and work on them. >>> >>> Now, if I may comment on your code. You're modifying a list while >>> you're iterating through it in a for loop. In the most general case, >>> that's undefined. I think it's safe in this case, but I would avoid >>> it anyway, by just using xrange(len(list_)-1) to iterate through it. >>> You use the index function to find something you would already know >>> -- the index function is slow. And the first try/except isn't needed >>> if you use a -1 in the xrange argument, as I do above. >>> >>> You use pop() and push() to exchange two adjacent items in the list. >>> Both operations copy the remainder of the list, so they're rather >>> slow. Since you're exchanging two items in the list, you can simply >>> do that: >>> list[pos1], list[pos2] = list[pos2], list[pos1] >>> >>> That also eliminates the need for the second try/except. >>> >>> You mention being bothered by the while loop. You could replace it >>> with a simple for loop with xrange(len(list_)), since you know that N >>> passes will always be enough. But if the list is partially sorted, >>> your present scheme will end sooner. And if it's fully sorted, it'll >>> only take one pass over the data. >>> >>> There are many refinements you could do. For example, you don't have >>> to stop the inner loop after the first swap. You could finish the >>> buffer, swapping any other pairs that are out of order. You'd then >>> be saving a flag indicating if you did any swaps. You could keep a >>> index pointing to the last pair you swapped on the previous pass, and >>> use that for a limit next time. Then you just terminate the outer >>> loop when that limit value is 1. You could even keep two limit >>> values, and bubble back and forth between them, as they gradually >>> close into the median of the list. You quit when they collide in the >>> middle. >>> >>> The resultant function should be much faster for medium-sized lists, >>> but it still will slow down quadratically as the list size >>> increases. You still need to divide and conquer, and quicksort is >>> just one way of doing that. >>> >>> DaveA >>> >> Thanks a lot Dave, >> >> Sorry the original thread is called 'Python and algorithms'. >> >> Yes, I think it's best to use what python provides and build on top of >> that. I got to asking my original question based on trying to learn >> more about algorithms in general, through python. Of late many people >> have been asking me how well I can 'build' algorithms, and this >> prompted me to start the thread. This is for learning purposes (which >> the original thread will give you and indication where I'm coming from). >> >> The refactored code looks like this. I have tackled a couple items. >> First the sub-listing (which I'll wait till I can get the full sort >> working), then the last couple of paragraphs about refinements. >> Starting with the first refinement, I'm not sure how *not* to stop the >> inner loop? >> >> def s2(list_): >> for pos1 in xrange(len(list_)-1): >> item1 = list_[pos1] >> pos2 = pos1 + 1 >> item2 = list_[pos2] >> if item1 >= item2: >> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >> return True >> >> def mysorter(list_): >> # This is the outer loop? >> while s2(list_) is True: >> # Calling s2 kicks off the inner loop? >> s2(list_) >> >> if __name__ == '__main__': >> from random import shuffle >> foo = range(10) >> shuffle(foo) >> mysorter(foo) >> >> >> Thanks again. >> > As before, I'm not actually trying this code, so there may be typos. > But assuming your code here works, the next refinement would be: > > In s2() function, add a flag variable, initially False. Then instead of > the return True, just say flag=True > Then at the end of the function, return flag > > About the while loop. No need to say 'is True' just use while > s2(list_): And no need to call s2() a second time. > > while s2(list_): > pass Okay up to here I follow. This all makes sense. def s2(list_): flag = False for pos1 in xrange(len(list_)-1): item1 = list_[pos1] pos2 = pos1 + 1 item2 = list_[pos2] if item1 >= item2: list_[pos1], list_[pos2] = list_[pos2], list_[pos1] flag = True return flag def mysorter(list_): while s2(list_): pass > > Before you can refine the upper limit, you need a way to preserve it > between calls. Simplest way to do that is to combine the two functions, > as a nested loop. Then, instead of flag, you can have a value "limit" > which indicates what index was last swapped. And the inner loop uses > that as an upper limit on its xrange. Where I start to get confused is refining the 'upper limit'. What is the upper limit defining? I'm guessing it is the last position processed. T > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From pylist1 at gmail.com Thu Mar 11 14:13:50 2010 From: pylist1 at gmail.com (pylist1) Date: Thu, 11 Mar 2010 08:13:50 -0500 (EST) Subject: [Tutor] pgdb and console output In-Reply-To: References: <4f23445b1003071309p73c2a217p4d3c3a9653459d6a@mail.gmail.com> Message-ID: On Wed, 10 Mar 2010, Alan Gauld wrote: > > "pylist1" wrote > Yes, stdout.write has no formatting if you need spaces or newlines you > have to explicitly provide them. Got it. > See the Talking to the User topic in my tutorial for more detail on the > differences. OK I will check that out. Thanks very much for the help. From dkdropbox at gmail.com Thu Mar 11 14:07:16 2010 From: dkdropbox at gmail.com (DK) Date: Thu, 11 Mar 2010 13:07:16 +0000 (UTC) Subject: [Tutor] Feedparser and google news/google reader References: Message-ID: Alan Gauld btinternet.com> writes: > > I have no idea if this is relevantt and without code I suspect we will all > be guessing blindly but... > > Have you checked Google's terms of use? I know they make it hard to > screen scrape their search engine so they may have similar limits on > their feeds. Just a thought. > I didn't include the code because its really just two lines, apologies: import feedparser feed = 'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&as_scoring=r&as_maxm=3&q=health+information+exchange&as_qdr=a&as_drrb=q&as_mind=8&as_minm=2&cf=all&as_maxd=100&output=rss' x = feedparser.parse(feed) len(x) (<-this always yields 10 items) In the meantime, i'll check the terms of service. From marcodrompre at gmail.com Thu Mar 11 16:11:28 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Thu, 11 Mar 2010 10:11:28 -0500 Subject: [Tutor] Problem with turtle Message-ID: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> Hi! I am relatively new to python and turtle and I really need your help. Here what I wanted turtle to do: I created a function named carr? which would draw a square with 3 different parameters (color, size, angle). I did the same for a function named triangle that would draw an equilateral triangle with the same parameters With the help of these 2 functions I wanted turtle to draw a yellow square then lift my pointer let a space and then draw a blue triangle five times in a row. Here's my code: ignore my comments def carre(taille, couleur, angle): "fonction qui dessine un carr? de taille et de couleur d?termin?es" color(couleur) c=0 while c<4: left(angle) forward(taille) right(90) c = c+1 def triangle(taille, couleur, angle): "fonction qui dessine un triangle ?quilat?ral de taille, de couleur et d'angle d?termin?s" color(couleur) c=0 while c<3: left(angle) forward(taille) right(120) c = c+1 from dessins_tortue import * n=0 while n < 10 : down() # abaisser le crayon carre(25, 'yellow', 0) # tracer un carr? up() forward(30) triangle(90, 'blue',0) n = n + 1 If I am to vague I wanted to do successfully exercise 7.6 in G?rard Swinnen tutorial for Python It was supposed to look like this -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From beachkid at insightbb.com Thu Mar 11 16:32:45 2010 From: beachkid at insightbb.com (Ken G.) Date: Thu, 11 Mar 2010 10:32:45 -0500 Subject: [Tutor] Changing the value in a list Message-ID: <4B990D1D.8090900@insightbb.com> If the following program change list[2] to 2010, replacing 1997 (and it does), why doesn't the second program work in changing line[ 9:11] to 20 from 08? FIRST PROGRAM: list = ['physics', 'chemistry', 1997, 2000] print list[2] list[2] = 2010 print list[2] OUTPUT: 1997 2010 SECOND PROGRAM: line = [2010020820841134] if line[ 9:11] == "08": line[ 9:11] = 20 print line[ 9:11] OUTPUT: "TypeError: 'str' object does not support item assignment." Thanking you all in advance, Ken From andreengels at gmail.com Thu Mar 11 16:44:31 2010 From: andreengels at gmail.com (Andre Engels) Date: Thu, 11 Mar 2010 16:44:31 +0100 Subject: [Tutor] Changing the value in a list In-Reply-To: <4B990D1D.8090900@insightbb.com> References: <4B990D1D.8090900@insightbb.com> Message-ID: <6faf39c91003110744y6afd9202hfcc9b90b406c6330@mail.gmail.com> On Thu, Mar 11, 2010 at 4:32 PM, Ken G. wrote: > If the following program change list[2] to 2010, replacing 1997 (and it > does), why doesn't the second program work in changing line[ 9:11] to 20 > from 08? > FIRST PROGRAM: > > ? list = ['physics', 'chemistry', 1997, 2000] > ? print list[2] > ? list[2] = 2010 > ? print list[2] > > OUTPUT: > > ? 1997 > ? 2010 > > SECOND PROGRAM: > ? line = [2010020820841134] > ? if line[ 9:11] == "08": > ? ? ? line[ 9:11] = 20 > ? ? ? print line[ 9:11] > > OUTPUT: > > ? "TypeError: 'str' object does not support item assignment." > > > Thanking you all in advance, First, you seem to have made a mistake in copying your programs: In the second program to get your error message, line = [2010020820841134] should read line = "2010020820841134" As for your question: The error message already says it better than I do: If x is a string, then x[9] = is not allowed, nor is x[9:11] = . -- Andr? Engels, andreengels at gmail.com From steve at alchemy.com Thu Mar 11 16:46:09 2010 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Mar 2010 07:46:09 -0800 Subject: [Tutor] Changing the value in a list In-Reply-To: <4B990D1D.8090900@insightbb.com> References: <4B990D1D.8090900@insightbb.com> Message-ID: <20100311154609.GA65473@dragon.alchemy.com> On Thu, Mar 11, 2010 at 10:32:45AM -0500, Ken G. wrote: > list = ['physics', 'chemistry', 1997, 2000] This is a list of 4 elements, and lists allow elements to be changed, so > list[2] = 2010 Will change the value of list to be ['physics','chemistry',1997,2010] > line = [2010020820841134] Is this one right, or did you mean '2010020820841134'? What is shown is a list of one element. Assuming you meant it to be a string, that would give the error you quote below. Strings are immutable objects, meaning that once they are created you can't change them, although you can construct new strings from them, so if you wanted to make a string which had characters 9 and 10 replaced with "08" you could say: line = line[:9]+"08"+line[11:] -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From dkdropbox at gmail.com Thu Mar 11 17:03:14 2010 From: dkdropbox at gmail.com (DK) Date: Thu, 11 Mar 2010 16:03:14 +0000 (UTC) Subject: [Tutor] Feedparser and google news/google reader References: Message-ID: DK gmail.com> writes: > > I didn't include the code because its really just two lines, apologies: > Well it turns out I AM a moron. Finding len(x) gives me the length of the dictionary, not the number of entries. I should have used len(x['entries']). Using the shared URL from my google reader and adding ?n=150, I was able to pull all 150 news items saved in my reader folder. The URL looks like this: ('http://www.google.com/reader/public/atom/user/USERID#HERE/lab el/HIE?n=155' I'm still not sure how to pull more google news search items from the associated rss feed, so if anyone has a clue, I'd appreciate a hint (tho I suppose we've left python land and entered google land now). Apologies for the static, dk From beachkid at insightbb.com Thu Mar 11 17:03:58 2010 From: beachkid at insightbb.com (Ken G.) Date: Thu, 11 Mar 2010 11:03:58 -0500 Subject: [Tutor] Changing the value in a list In-Reply-To: <6faf39c91003110744y6afd9202hfcc9b90b406c6330@mail.gmail.com> References: <4B990D1D.8090900@insightbb.com> <6faf39c91003110744y6afd9202hfcc9b90b406c6330@mail.gmail.com> Message-ID: <4B99146E.5020402@insightbb.com> Okay, now, it is understood. Thanks. Ken Andre Engels wrote: > On Thu, Mar 11, 2010 at 4:32 PM, Ken G. wrote: > >> If the following program change list[2] to 2010, replacing 1997 (and it >> does), why doesn't the second program work in changing line[ 9:11] to 20 >> from 08? >> FIRST PROGRAM: >> >> list = ['physics', 'chemistry', 1997, 2000] >> print list[2] >> list[2] = 2010 >> print list[2] >> >> OUTPUT: >> >> 1997 >> 2010 >> >> SECOND PROGRAM: >> line = [2010020820841134] >> if line[ 9:11] == "08": >> line[ 9:11] = 20 >> print line[ 9:11] >> >> OUTPUT: >> >> "TypeError: 'str' object does not support item assignment." >> >> >> Thanking you all in advance, >> > > First, you seem to have made a mistake in copying your programs: In > the second program to get your error message, > > line = [2010020820841134] > > should read > > line = "2010020820841134" > > As for your question: The error message already says it better than I > do: If x is a string, then x[9] = is not allowed, nor is > x[9:11] = . > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From beachkid at insightbb.com Thu Mar 11 17:04:21 2010 From: beachkid at insightbb.com (Ken G.) Date: Thu, 11 Mar 2010 11:04:21 -0500 Subject: [Tutor] Changing the value in a list In-Reply-To: <20100311154609.GA65473@dragon.alchemy.com> References: <4B990D1D.8090900@insightbb.com> <20100311154609.GA65473@dragon.alchemy.com> Message-ID: <4B991485.5010805@insightbb.com> Okay, I understand now. Thanks! Ken Steve Willoughby wrote: > On Thu, Mar 11, 2010 at 10:32:45AM -0500, Ken G. wrote: > >> list = ['physics', 'chemistry', 1997, 2000] >> > > This is a list of 4 elements, and lists allow elements to > be changed, so > > >> list[2] = 2010 >> > > Will change the value of list to be ['physics','chemistry',1997,2010] > > >> line = [2010020820841134] >> > > Is this one right, or did you mean '2010020820841134'? What is shown is > a list of one element. Assuming you meant it to be a string, that would > give the error you quote below. > > Strings are immutable objects, meaning that once they are created you can't > change them, although you can construct new strings from them, so if you > wanted to make a string which had characters 9 and 10 replaced with "08" > you could say: > line = line[:9]+"08"+line[11:] > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From emailkgnow at gmail.com Thu Mar 11 17:38:41 2010 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Thu, 11 Mar 2010 19:38:41 +0300 Subject: [Tutor] Recommendations on Workshops, Courses, Live Online Training Message-ID: Hi everyone, I've subscribed to ShowMeDo, but I feel something more than just video tutorials. Do you have any recommendations on where I can find workshops, Courses, Live Online Training where I can interact with a real person that I can ask questions and find the answers I'm looking for. I live in the middle east, so online live training would be preferable, but I am up for any ohther recommendation and suggestions. thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Mar 11 19:22:54 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Mar 2010 18:22:54 -0000 Subject: [Tutor] Recommendations on Workshops, Courses, Live Online Training References: Message-ID: "Khalid Al-Ghamdi" wrote > I've subscribed to ShowMeDo, but I feel something more than just video > tutorials. Do you have any recommendations on where I can find workshops, > Courses, Live Online Training where I can interact with a real person > that I > can ask questions and find the answers I'm looking for. Well (most) folks on the tutor list are live, and real opersons and we answer questions... But if you mean face to face then consider a Python users group - or evenas Linux User Group(more of them) since Linux users are often python users too... > I live in the middle east, so online live training would be preferable, > but I am up for any other recommendation and suggestions. I don't know of any live (ie real-time) online training but there may be a LUG omewhere within reach. An email enquiry before you go might uncover whether Python discussions are considered legitimate... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Mar 11 19:26:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Mar 2010 18:26:06 -0000 Subject: [Tutor] Problem with turtle References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> Message-ID: "Marco Rompr?" wrote > Hi! I am relatively new to python and turtle and I really need your help. Thats what we are hee for but.... > Here's my code: ignore my comments n=0 while n < 10 : down() # abaisser le crayon carre(25, 'yellow', 0) # tracer un carr? up() forward(30) triangle(90, 'blue',0) n = n + 1 > If I am to vague I wanted to do successfully exercise 7.6 in G?rard > Swinnen > tutorial for Python > > It was supposed to look like this So what happens? Is there an error message or does it just draw the wrong thing? Don't make us guess... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Thu Mar 11 20:03:35 2010 From: denis.spir at gmail.com (spir) Date: Thu, 11 Mar 2010 20:03:35 +0100 Subject: [Tutor] use of __new__ Message-ID: <20100311200335.76647c2a@o> Hello, I need a custom unicode subtype (with additional methods). This will not be directly used by the user, instead it is just for internal purpose. I would like the type to be able to cope with either a byte str or a unicode str as argument. In the first case, it needs to be first decoded. I cannot do it in __init__ because unicode will first try to decode it as ascii, which fails in the general case. So, I must have my own __new__. The issue is the object (self) is then a unicode one instead of my own type. class Unicode(unicode): Unicode.FORMAT = "utf8" def __new__(self, text, format=None): # text can be str or unicode format = Unicode.FORMAT if format is None else format if isinstance(text,str): text = text.decode(format) return text ....... x = Unicode("abc") # --> unicode, not Unicode Denis ________________________________ la vita e estrany spir.wikidot.com From denis.spir at gmail.com Thu Mar 11 20:11:57 2010 From: denis.spir at gmail.com (spir) Date: Thu, 11 Mar 2010 20:11:57 +0100 Subject: [Tutor] Problem with turtle In-Reply-To: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> Message-ID: <20100311201157.1dd0fa4a@o> On Thu, 11 Mar 2010 10:11:28 -0500 Marco Rompr? wrote: > Hi! I am relatively new to python and turtle and I really need your help. > > Here what I wanted turtle to do: I created a function named carr? which > would draw a square with 3 different parameters (color, size, angle). > I did the same for a function > named triangle that would draw an equilateral triangle with the same > parameters > With the help of these 2 > functions I wanted turtle to draw a yellow square then lift my pointer let a > space and then draw a blue triangle five times in a row. > > Here's my code: ignore my comments > > def carre(taille, couleur, angle): > "fonction qui dessine un carr? de taille et de couleur d?termin?es" > color(couleur) > c=0 > while c<4: > left(angle) > forward(taille) > right(90) > c = c+1 What is angle? Define its sense in the doc string. If it's the general orientation of the "carr?", then you should use it only once, at start. > def triangle(taille, couleur, angle): > "fonction qui dessine un triangle ?quilat?ral de taille, de couleur et > d'angle d?termin?s" > color(couleur) > c=0 > while c<3: > left(angle) > forward(taille) > right(120) > c = c+1 Idem. > from dessins_tortue import * > > n=0 > while n < 10 : > down() # abaisser le crayon > carre(25, 'yellow', 0) # tracer un carr? > up() > forward(30) > triangle(90, 'blue',0) > n = n + 1 Last line is mis-indented (maybe copy-paste problem). > If I am to vague I wanted to do successfully exercise 7.6 in G?rard Swinnen > tutorial for Python > > It was supposed to look like this ??? Denis ________________________________ la vita e estrany spir.wikidot.com From oberoc at gmail.com Thu Mar 11 20:28:44 2010 From: oberoc at gmail.com (Tino Dai) Date: Thu, 11 Mar 2010 14:28:44 -0500 Subject: [Tutor] Recommendations on Workshops, Courses, Live Online Training In-Reply-To: References: Message-ID: <2ac5d4851003111128l60bf864s21716ab2945d1a54@mail.gmail.com> On Thu, Mar 11, 2010 at 1:22 PM, Alan Gauld wrote: > > "Khalid Al-Ghamdi" wrote > > > I've subscribed to ShowMeDo, but I feel something more than just video >> tutorials. Do you have any recommendations on where I can find workshops, >> Courses, Live Online Training where I can interact with a real person that >> I >> can ask questions and find the answers I'm looking for. >> > > Well (most) folks on the tutor list are live, and real opersons and we > answer questions... But if you mean face to face then consider a Python > users group - or evenas Linux User Group(more of them) since Linux > users are often python users too... > > > Alan and the rest of the tutor regulars, I do know of a place in North Carolina, and the president of the company spoke @ PyCon this year. I don't know if this is the correct venue to put that sort of information. Guidance please. :) -Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From marcodrompre at gmail.com Thu Mar 11 20:29:13 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Thu, 11 Mar 2010 14:29:13 -0500 Subject: [Tutor] Problem with turtle In-Reply-To: References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> Message-ID: <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> I wanted turtle to draw alternatively a square and a triangle with a space between them each with a specific color, angle(orientation as you said), size. Instead, turtle was drawing a yellow square then it was drawing a triangle on the sqare but with no lines whatsovever like it was just going over it and the last problem was that it was never stopping and i had to rstart the shell to make it stop. I hope I am more precis with my explanations. Thanks for helping me learn On Thu, Mar 11, 2010 at 1:26 PM, Alan Gauld wrote: > > "Marco Rompr?" wrote > > > Hi! I am relatively new to python and turtle and I really need your help. >> > > Thats what we are hee for but.... > > > Here's my code: ignore my comments >> > > n=0 > while n < 10 : > down() # abaisser le crayon > carre(25, 'yellow', 0) # tracer un carr? > up() > forward(30) > triangle(90, 'blue',0) > n = n + 1 > > > If I am to vague I wanted to do successfully exercise 7.6 in G?rard >> Swinnen >> tutorial for Python >> >> It was supposed to look like this >> > > So what happens? Is there an error message or does it just draw > the wrong thing? Don't make us guess... > > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From vceder at canterburyschool.org Thu Mar 11 20:35:39 2010 From: vceder at canterburyschool.org (Vern Ceder) Date: Thu, 11 Mar 2010 14:35:39 -0500 Subject: [Tutor] Problem with turtle In-Reply-To: <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> Message-ID: <4B99460B.7040401@canterburyschool.org> It looks like the indentation of n = n + 1 is wrong - it will be outside of the while loop and so n will never increment and the loop will never end. Instead of a while loop I would suggest a for loop: for n in range(10: HTH, Vern Marco Rompr? wrote: > I wanted turtle to draw alternatively a square and a triangle with a > space between them each with a specific color, angle(orientation as you > said), size. Instead, turtle was drawing a yellow square then it was > drawing a triangle on the sqare but with no lines whatsovever like it > was just going over it and the last problem was that it was never > stopping and i had to rstart the shell to make it stop. > > I hope I am more precis with my explanations. > > Thanks for helping me learn > > On Thu, Mar 11, 2010 at 1:26 PM, Alan Gauld > wrote: > > > "Marco Rompr?" > wrote > > > Hi! I am relatively new to python and turtle and I really need > your help. > > > Thats what we are hee for but.... > > > Here's my code: ignore my comments > > > n=0 > while n < 10 : > down() # abaisser le crayon > carre(25, 'yellow', 0) # tracer un carr? > up() > forward(30) > triangle(90, 'blue',0) > n = n + 1 > > > If I am to vague I wanted to do successfully exercise 7.6 in > G?rard Swinnen > tutorial for Python > > It was supposed to look like this > > > So what happens? Is there an error message or does it just draw > the wrong thing? Don't make us guess... > > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > > -- > Marc-O. Rompr? > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW From jeff at dcsoftware.com Thu Mar 11 20:47:26 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Thu, 11 Mar 2010 12:47:26 -0700 Subject: [Tutor] sorting algorithm In-Reply-To: <4B98DBB1.6070401@gmail.com> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> Message-ID: <4B9948CE.10201@dcsoftware.com> C.T. Matsumoto wrote: > Dave Angel wrote: >> (You forgot to do a Reply-All, so your message went to just me, rather >> than to me and the list ) >> >> >> C.T. Matsumoto wrote: >>> Dave Angel wrote: >>>> C.T. Matsumoto wrote: >>>>> Hello, >>>>> >>>>> This is follow up on a question I had about algorithms. In the >>>>> thread it was suggested I make my own sorting algorithm. >>>>> >>>>> Here are my results. >>>>> >>>>> #!/usr/bin/python >>>>> >>>>> def sort_(list_): >>>>> for item1 in list_: >>>>> pos1 = list_.index(item1) >>>>> pos2 = pos1 + 1 >>>>> try: >>>>> item2 = list_[pos2] >>>>> except IndexError: >>>>> pass >>>>> >>>>> if item1 >= item2: >>>>> try: >>>>> list_.pop(pos2) >>>>> list_.insert(pos1, item2) >>>>> return True >>>>> except IndexError: >>>>> pass >>>>> >>>>> def mysorter(list_): >>>>> while sort_(list_) is True: >>>>> sort_(list_) >>>>> >>>>> I found this to be a great exercise. In doing the exercise, I got >>>>> pretty stuck. I consulted another programmer (my dad) who described >>>>> how to go about sorting. As it turned out the description he >>>>> described was the Bubble sort algorithm. Since coding the solution >>>>> I know the Bubble sort is inefficient because of repeated >>>>> iterations over the entire list. This shed light on the quick sort >>>>> algorithm which I'd like to have a go at. >>>>> >>>>> Something I haven't tried is sticking in really large lists. I was >>>>> told that with really large list you break down the input list into >>>>> smaller lists. Sort each list, then go back and use the same >>>>> swapping procedure for each of the different lists. My question is, >>>>> at what point to you start breaking things up? Is that based on >>>>> list elements or is it based on memory(?) resources python is using? >>>>> >>>>> One thing I'm not pleased about is the while loop and I'd like to >>>>> replace it with a for loop. >>>>> >>>>> Thanks, >>>>> >>>>> T >>>>> >>>>> >>>> There are lots of references on the web about Quicksort, including a >>>> video at: >>>> http://www.youtube.com/watch?v=y_G9BkAm6B8 >>>> >>>> which I think illustrates it pretty well. It would be a great >>>> learning exercise to implement Python code directly from that >>>> description, without using the sample C++ code available. >>>> >>>> (Incidentally, there are lots of variants of Quicksort, so I'm not >>>> going to quibble about whether this is the "right" one to be called >>>> that.) >>>> >>>> I don't know what your earlier thread was, since you don't mention >>>> the subject line, but there are a number of possible reasons you >>>> might not have wanted to use the built-in sort. The best one is for >>>> educational purposes. I've done my own sort for various reasons in >>>> the past, even though I had a library function, since the library >>>> function had some limits. One time I recall, the situation was that >>>> the library sort was limited to 64k of total data, and I had to work >>>> with much larger arrays (this was in 16bit C++, in "large" model). >>>> I solved the size problem by using the C++ sort library on 16k >>>> subsets (because a pointer was 2*2 bytes). Then I merged the >>>> results of the sorts. At the time, and in the circumstances >>>> involved, there were seldom more than a dozen or so sublists to >>>> merge, so this approach worked well enough. >>>> >>>> Generally, it's better for both your development time and the >>>> efficiency and reliabilty of the end code, to base a new sort >>>> mechanism on the existing one. In my case above, I was replacing >>>> what amounted to an insertion sort, and achieved a 50* improvement >>>> for a real customer. It was fast enough that other factors >>>> completely dominated his running time. >>>> >>>> But for learning purposes? Great plan. So now I'll respond to your >>>> other questions, and comment on your present algorithm. >>>> >>>> It would be useful to understand about algorithmic complexity, the >>>> so called Order Function. In a bubble sort, if you double the size >>>> of the array, you quadruple the number of comparisons and swaps. >>>> It's order N-squared or O(n*n). So what works well for an array of >>>> size 10 might take a very long time for an array of size 10000 (like >>>> a million times as long). You can do much better by sorting smaller >>>> lists, and then combining them together. Such an algorithm can be >>>> O(n*log(n)). >>>> >>>> >>>> You ask at what point you consider sublists? In a language like C, >>>> the answer is when the list is size 3 or more. For anything larger >>>> than 2, you divide into sublists, and work on them. >>>> >>>> Now, if I may comment on your code. You're modifying a list while >>>> you're iterating through it in a for loop. In the most general >>>> case, that's undefined. I think it's safe in this case, but I would >>>> avoid it anyway, by just using xrange(len(list_)-1) to iterate >>>> through it. You use the index function to find something you would >>>> already know -- the index function is slow. And the first >>>> try/except isn't needed if you use a -1 in the xrange argument, as I >>>> do above. >>>> >>>> You use pop() and push() to exchange two adjacent items in the >>>> list. Both operations copy the remainder of the list, so they're >>>> rather slow. Since you're exchanging two items in the list, you can >>>> simply do that: >>>> list[pos1], list[pos2] = list[pos2], list[pos1] >>>> >>>> That also eliminates the need for the second try/except. >>>> >>>> You mention being bothered by the while loop. You could replace it >>>> with a simple for loop with xrange(len(list_)), since you know that >>>> N passes will always be enough. But if the list is partially >>>> sorted, your present scheme will end sooner. And if it's fully >>>> sorted, it'll only take one pass over the data. >>>> >>>> There are many refinements you could do. For example, you don't >>>> have to stop the inner loop after the first swap. You could finish >>>> the buffer, swapping any other pairs that are out of order. You'd >>>> then be saving a flag indicating if you did any swaps. You could >>>> keep a index pointing to the last pair you swapped on the previous >>>> pass, and use that for a limit next time. Then you just terminate >>>> the outer loop when that limit value is 1. You could even keep two >>>> limit values, and bubble back and forth between them, as they >>>> gradually close into the median of the list. You quit when they >>>> collide in the middle. >>>> >>>> The resultant function should be much faster for medium-sized lists, >>>> but it still will slow down quadratically as the list size >>>> increases. You still need to divide and conquer, and quicksort is >>>> just one way of doing that. >>>> >>>> DaveA >>>> >>> Thanks a lot Dave, >>> >>> Sorry the original thread is called 'Python and algorithms'. >>> >>> Yes, I think it's best to use what python provides and build on top >>> of that. I got to asking my original question based on trying to >>> learn more about algorithms in general, through python. Of late many >>> people have been asking me how well I can 'build' algorithms, and >>> this prompted me to start the thread. This is for learning purposes >>> (which the original thread will give you and indication where I'm >>> coming from). >>> >>> The refactored code looks like this. I have tackled a couple items. >>> First the sub-listing (which I'll wait till I can get the full sort >>> working), then the last couple of paragraphs about refinements. >>> Starting with the first refinement, I'm not sure how *not* to stop >>> the inner loop? >>> >>> def s2(list_): >>> for pos1 in xrange(len(list_)-1): >>> item1 = list_[pos1] >>> pos2 = pos1 + 1 >>> item2 = list_[pos2] >>> if item1 >= item2: >>> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >>> return True >>> >>> def mysorter(list_): >>> # This is the outer loop? >>> while s2(list_) is True: >>> # Calling s2 kicks off the inner loop? >>> s2(list_) >>> >>> if __name__ == '__main__': >>> from random import shuffle >>> foo = range(10) >>> shuffle(foo) >>> mysorter(foo) >>> >>> >>> Thanks again. >>> >> As before, I'm not actually trying this code, so there may be typos. >> But assuming your code here works, the next refinement would be: >> >> In s2() function, add a flag variable, initially False. Then instead >> of the return True, just say flag=True >> Then at the end of the function, return flag >> >> About the while loop. No need to say 'is True' just use while >> s2(list_): And no need to call s2() a second time. >> >> while s2(list_): >> pass > Okay up to here I follow. This all makes sense. > > def s2(list_): > flag = False > for pos1 in xrange(len(list_)-1): > item1 = list_[pos1] > pos2 = pos1 + 1 > item2 = list_[pos2] > if item1 >= item2: > list_[pos1], list_[pos2] = list_[pos2], list_[pos1] > flag = True > return flag > > def mysorter(list_): > while s2(list_): > pass >> >> Before you can refine the upper limit, you need a way to preserve it >> between calls. Simplest way to do that is to combine the two >> functions, as a nested loop. Then, instead of flag, you can have a >> value "limit" which indicates what index was last swapped. And the >> inner loop uses that as an upper limit on its xrange. > Where I start to get confused is refining the 'upper limit'. What is the > upper limit defining? I'm guessing it is the last position processed. > > T >> >> DaveA >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Take out the = in the following line if item1 >= item2: and it will sort like items together, which I think is what you originally wanted. Also change: > return flag to: > return flag So that False gets returned if you don't make a swap. This worked for me. Thank you for the interesting thread! -- Jeff Jeff Johnson jeff at dcsoftware.com Phoenix Python User Group - sunpiggies at googlegroups.com From vceder at canterburyschool.org Thu Mar 11 20:53:37 2010 From: vceder at canterburyschool.org (Vern Ceder) Date: Thu, 11 Mar 2010 14:53:37 -0500 Subject: [Tutor] Problem with turtle In-Reply-To: <4B99460B.7040401@canterburyschool.org> References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> <4B99460B.7040401@canterburyschool.org> Message-ID: <4B994A41.7040703@canterburyschool.org> Ooops... missed a closing parenthese... that should be: for n in range(10): Vern Ceder wrote: > It looks like the indentation of n = n + 1 is wrong - it will be outside > of the while loop and so n will never increment and the loop will never > end. > > Instead of a while loop I would suggest a for loop: > > for n in range(10: > > > HTH, > > Vern > > Marco Rompr? wrote: >> I wanted turtle to draw alternatively a square and a triangle with a >> space between them each with a specific color, angle(orientation as >> you said), size. Instead, turtle was drawing a yellow square then it >> was drawing a triangle on the sqare but with no lines whatsovever like >> it was just going over it and the last problem was that it was never >> stopping and i had to rstart the shell to make it stop. >> >> I hope I am more precis with my explanations. >> >> Thanks for helping me learn >> >> On Thu, Mar 11, 2010 at 1:26 PM, Alan Gauld > > wrote: >> >> >> "Marco Rompr?" > > wrote >> >> >> Hi! I am relatively new to python and turtle and I really need >> your help. >> >> >> Thats what we are hee for but.... >> >> >> Here's my code: ignore my comments >> >> >> n=0 >> while n < 10 : >> down() # abaisser le crayon >> carre(25, 'yellow', 0) # tracer un carr? >> up() >> forward(30) >> triangle(90, 'blue',0) >> n = n + 1 >> >> >> If I am to vague I wanted to do successfully exercise 7.6 in >> G?rard Swinnen >> tutorial for Python >> >> It was supposed to look like this >> >> >> So what happens? Is there an error message or does it just draw >> the wrong thing? Don't make us guess... >> >> >> >> -- Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> >> -- >> Marc-O. Rompr? >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW From davea at ieee.org Thu Mar 11 21:09:56 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 11 Mar 2010 15:09:56 -0500 Subject: [Tutor] sorting algorithm In-Reply-To: <4B98DBB1.6070401@gmail.com> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> Message-ID: <4B994E14.4080205@ieee.org> C.T. Matsumoto wrote: > Dave Angel wrote: >> (You forgot to do a Reply-All, so your message went to just me, >> rather than to me and the list ) >> >> >> C.T. Matsumoto wrote: >>> Dave Angel wrote: >>>> C.T. Matsumoto wrote: >>>>> Hello, >>>>> >>>>> This is follow up on a question I had about algorithms. In the >>>>> thread it was suggested I make my own sorting algorithm. >>>>> >>>>> Here are my results. >>>>> >>>>> #!/usr/bin/python >>>>> >>>>> def sort_(list_): >>>>> for item1 in list_: >>>>> pos1 = list_.index(item1) >>>>> pos2 = pos1 + 1 >>>>> try: >>>>> item2 = list_[pos2] >>>>> except IndexError: >>>>> pass >>>>> >>>>> if item1 >= item2: >>>>> try: >>>>> list_.pop(pos2) >>>>> list_.insert(pos1, item2) >>>>> return True >>>>> except IndexError: >>>>> pass >>>>> >>>>> def mysorter(list_): >>>>> while sort_(list_) is True: >>>>> sort_(list_) >>>>> >>>>> I found this to be a great exercise. In doing the exercise, I got >>>>> pretty stuck. I consulted another programmer (my dad) who >>>>> described how to go about sorting. As it turned out the >>>>> description he described was the Bubble sort algorithm. Since >>>>> coding the solution I know the Bubble sort is inefficient because >>>>> of repeated iterations over the entire list. This shed light on >>>>> the quick sort algorithm which I'd like to have a go at. >>>>> >>>>> Something I haven't tried is sticking in really large lists. I was >>>>> told that with really large list you break down the input list >>>>> into smaller lists. Sort each list, then go back and use the same >>>>> swapping procedure for each of the different lists. My question >>>>> is, at what point to you start breaking things up? Is that based >>>>> on list elements or is it based on memory(?) resources python is >>>>> using? >>>>> >>>>> One thing I'm not pleased about is the while loop and I'd like to >>>>> replace it with a for loop. >>>>> >>>>> Thanks, >>>>> >>>>> T >>>>> >>>>> >>>> There are lots of references on the web about Quicksort, including >>>> a video at: >>>> http://www.youtube.com/watch?v=y_G9BkAm6B8 >>>> >>>> which I think illustrates it pretty well. It would be a great >>>> learning exercise to implement Python code directly from that >>>> description, without using the sample C++ code available. >>>> >>>> (Incidentally, there are lots of variants of Quicksort, so I'm not >>>> going to quibble about whether this is the "right" one to be called >>>> that.) >>>> >>>> I don't know what your earlier thread was, since you don't mention >>>> the subject line, but there are a number of possible reasons you >>>> might not have wanted to use the built-in sort. The best one is >>>> for educational purposes. I've done my own sort for various >>>> reasons in the past, even though I had a library function, since >>>> the library function had some limits. One time I recall, the >>>> situation was that the library sort was limited to 64k of total >>>> data, and I had to work with much larger arrays (this was in 16bit >>>> C++, in "large" model). I solved the size problem by using the >>>> C++ sort library on 16k subsets (because a pointer was 2*2 bytes). >>>> Then I merged the results of the sorts. At the time, and in the >>>> circumstances involved, there were seldom more than a dozen or so >>>> sublists to merge, so this approach worked well enough. >>>> >>>> Generally, it's better for both your development time and the >>>> efficiency and reliabilty of the end code, to base a new sort >>>> mechanism on the existing one. In my case above, I was replacing >>>> what amounted to an insertion sort, and achieved a 50* improvement >>>> for a real customer. It was fast enough that other factors >>>> completely dominated his running time. >>>> >>>> But for learning purposes? Great plan. So now I'll respond to >>>> your other questions, and comment on your present algorithm. >>>> >>>> It would be useful to understand about algorithmic complexity, the >>>> so called Order Function. In a bubble sort, if you double the size >>>> of the array, you quadruple the number of comparisons and swaps. >>>> It's order N-squared or O(n*n). So what works well for an array >>>> of size 10 might take a very long time for an array of size 10000 >>>> (like a million times as long). You can do much better by sorting >>>> smaller lists, and then combining them together. Such an algorithm >>>> can be O(n*log(n)). >>>> >>>> >>>> You ask at what point you consider sublists? In a language like C, >>>> the answer is when the list is size 3 or more. For anything larger >>>> than 2, you divide into sublists, and work on them. >>>> >>>> Now, if I may comment on your code. You're modifying a list while >>>> you're iterating through it in a for loop. In the most general >>>> case, that's undefined. I think it's safe in this case, but I >>>> would avoid it anyway, by just using xrange(len(list_)-1) to >>>> iterate through it. You use the index function to find something >>>> you would already know -- the index function is slow. And the >>>> first try/except isn't needed if you use a -1 in the xrange >>>> argument, as I do above. >>>> >>>> You use pop() and push() to exchange two adjacent items in the >>>> list. Both operations copy the remainder of the list, so they're >>>> rather slow. Since you're exchanging two items in the list, you >>>> can simply do that: >>>> list[pos1], list[pos2] = list[pos2], list[pos1] >>>> >>>> That also eliminates the need for the second try/except. >>>> >>>> You mention being bothered by the while loop. You could replace it >>>> with a simple for loop with xrange(len(list_)), since you know that >>>> N passes will always be enough. But if the list is partially >>>> sorted, your present scheme will end sooner. And if it's fully >>>> sorted, it'll only take one pass over the data. >>>> >>>> There are many refinements you could do. For example, you don't >>>> have to stop the inner loop after the first swap. You could finish >>>> the buffer, swapping any other pairs that are out of order. You'd >>>> then be saving a flag indicating if you did any swaps. You could >>>> keep a index pointing to the last pair you swapped on the previous >>>> pass, and use that for a limit next time. Then you just terminate >>>> the outer loop when that limit value is 1. You could even keep two >>>> limit values, and bubble back and forth between them, as they >>>> gradually close into the median of the list. You quit when they >>>> collide in the middle. >>>> >>>> The resultant function should be much faster for medium-sized >>>> lists, but it still will slow down quadratically as the list size >>>> increases. You still need to divide and conquer, and quicksort is >>>> just one way of doing that. >>>> >>>> DaveA >>>> >>> Thanks a lot Dave, >>> >>> Sorry the original thread is called 'Python and algorithms'. >>> >>> Yes, I think it's best to use what python provides and build on top >>> of that. I got to asking my original question based on trying to >>> learn more about algorithms in general, through python. Of late many >>> people have been asking me how well I can 'build' algorithms, and >>> this prompted me to start the thread. This is for learning purposes >>> (which the original thread will give you and indication where I'm >>> coming from). >>> >>> The refactored code looks like this. I have tackled a couple items. >>> First the sub-listing (which I'll wait till I can get the full sort >>> working), then the last couple of paragraphs about refinements. >>> Starting with the first refinement, I'm not sure how *not* to stop >>> the inner loop? >>> >>> def s2(list_): >>> for pos1 in xrange(len(list_)-1): >>> item1 = list_[pos1] >>> pos2 = pos1 + 1 >>> item2 = list_[pos2] >>> if item1 >= item2: >>> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >>> return True >>> >>> def mysorter(list_): >>> # This is the outer loop? >>> while s2(list_) is True: >>> # Calling s2 kicks off the inner loop? >>> s2(list_) >>> >>> if __name__ == '__main__': >>> from random import shuffle >>> foo = range(10) >>> shuffle(foo) >>> mysorter(foo) >>> >>> >>> Thanks again. >>> >> As before, I'm not actually trying this code, so there may be typos. >> But assuming your code here works, the next refinement would be: >> >> In s2() function, add a flag variable, initially False. Then instead >> of the return True, just say flag=True >> Then at the end of the function, return flag >> >> About the while loop. No need to say 'is True' just use while >> s2(list_): And no need to call s2() a second time. >> >> while s2(list_): >> pass > Okay up to here I follow. This all makes sense. > > def s2(list_): > flag = False > for pos1 in xrange(len(list_)-1): > item1 = list_[pos1] > pos2 = pos1 + 1 > item2 = list_[pos2] > if item1 >= item2: > list_[pos1], list_[pos2] = list_[pos2], list_[pos1] > flag = True > return flag > > def mysorter(list_): > while s2(list_): > pass >> >> Before you can refine the upper limit, you need a way to preserve it >> between calls. Simplest way to do that is to combine the two >> functions, as a nested loop. Then, instead of flag, you can have a >> value "limit" which indicates what index was last swapped. And the >> inner loop uses that as an upper limit on its xrange. > Where I start to get confused is refining the 'upper limit'. What is > the upper limit defining? I'm guessing it is the last position processed. > > T > The upper limit is the index of the last swap you had to make. That'd be either pos1 or pos2, I forget which. Anyway, the inner loop goes to that limit, instead of to the len(_list) The idea is that anything beyond that point is already sorted, and already contains the highest elements, so you don't need to visit it again. This is all from memory, so I think I'm right, but not positive. It's been many years since I had to actually code a sort loop by hand. Probably 20 or so. DaveA From marcodrompre at gmail.com Thu Mar 11 23:53:54 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Thu, 11 Mar 2010 17:53:54 -0500 Subject: [Tutor] Problem with turtle In-Reply-To: <4B994A41.7040703@canterburyschool.org> References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> <4B99460B.7040401@canterburyschool.org> <4B994A41.7040703@canterburyschool.org> Message-ID: <4bdcec5e1003111453w413cdfb0h9d5f012f686fa3f4@mail.gmail.com> It does not work either with the for n in range (10): On Thu, Mar 11, 2010 at 2:53 PM, Vern Ceder wrote: > Ooops... missed a closing parenthese... that should be: > > > for n in range(10): > > > Vern Ceder wrote: > >> It looks like the indentation of n = n + 1 is wrong - it will be outside >> of the while loop and so n will never increment and the loop will never end. >> >> Instead of a while loop I would suggest a for loop: >> >> for n in range(10: >> >> >> HTH, >> >> Vern >> >> Marco Rompr? wrote: >> >>> I wanted turtle to draw alternatively a square and a triangle with a >>> space between them each with a specific color, angle(orientation as you >>> said), size. Instead, turtle was drawing a yellow square then it was drawing >>> a triangle on the sqare but with no lines whatsovever like it was just going >>> over it and the last problem was that it was never stopping and i had to >>> rstart the shell to make it stop. >>> >>> I hope I am more precis with my explanations. >>> >>> Thanks for helping me learn >>> >>> On Thu, Mar 11, 2010 at 1:26 PM, Alan Gauld >> alan.gauld at btinternet.com>> wrote: >>> >>> >>> "Marco Rompr?" >> > wrote >>> >>> >>> Hi! I am relatively new to python and turtle and I really need >>> your help. >>> >>> >>> Thats what we are hee for but.... >>> >>> >>> Here's my code: ignore my comments >>> >>> >>> n=0 >>> while n < 10 : >>> down() # abaisser le crayon >>> carre(25, 'yellow', 0) # tracer un carr? >>> up() >>> forward(30) >>> triangle(90, 'blue',0) >>> n = n + 1 >>> >>> >>> If I am to vague I wanted to do successfully exercise 7.6 in >>> G?rard Swinnen >>> tutorial for Python >>> >>> It was supposed to look like this >>> >>> >>> So what happens? Is there an error message or does it just draw >>> the wrong thing? Don't make us guess... >>> >>> >>> >>> -- Alan Gauld >>> Author of the Learn to Program web site >>> http://www.alan-g.me.uk/ >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >>> >>> -- >>> Marc-O. Rompr? >>> >>> >>> ------------------------------------------------------------------------ >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> > -- > This time for sure! > -Bullwinkle J. Moose > ----------------------------- > Vern Ceder, Director of Technology > Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 > vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 > > The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW > -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Mar 12 00:54:31 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 11 Mar 2010 15:54:31 -0800 Subject: [Tutor] Opening a dos exe In-Reply-To: References: Message-ID: On 3/10/2010 11:33 AM Armstrong, Richard J. said... >The problem comes in that the dos program requires three > inputs (input1.txt, input2.txt and input3.txt - see attached picture) > but I cannot find a way of getting this information to the dos program > from python. Any ideas? I've sometimes written python code to create wsh (and other) scripts that I then run from within python. WSH include a sendkeys command IIRC that works with dos/command/cmd windows. There's likely ways to use the win32 api as well (win32api.keybd_event?). I currently use a tool called macroScheduler from www.mjtnet.com just for the windows automation tasks I run into (scripted from within python). There's also http://pypi.python.org/pypi/SendKeys/0.3, but at this point if I were to start over I'd look closely at http://www.rutherfurd.net/python/sendkeys/ HTH, Emile From alan.gauld at btinternet.com Fri Mar 12 01:07:38 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 00:07:38 -0000 Subject: [Tutor] Opening a dos exe References: Message-ID: "Emile van Sebille" wrote > I've sometimes written python code to create wsh (and other) scripts that > I then run from within python. WSH include a sendkeys command IIRC that > works with dos/command/cmd windows. There's likely ways to use the win32 > api as well (win32api.keybd_event?). Using Pythonwin you can run WSH natively from Python. Just register Python as a Windows Scripting language. Alan G From alan.gauld at btinternet.com Fri Mar 12 01:20:55 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 00:20:55 -0000 Subject: [Tutor] Problem with turtle References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> Message-ID: "Marco Rompr?" wrote > turtle was drawing a yellow square then it was drawing a > triangle on the sqare but with no lines whatsovever You'll need to put the pen down() again for it to draw the lines... > like it was just going over it and the last problem was > that it was never stopping Thats because you increment n outside the while loop so it never stops looping. > I hope I am more precis with my explanations. Much better, thanks. > n=0 > while n < 10 : > down() # abaisser le crayon > carre(25, 'yellow', 0) # tracer un carr? > up() > forward(30) > triangle(90, 'blue',0) # THE PEN IS STILL UP HERE > n = n + 1 # THIS IS OUTSIDE THE WHILE BLOCK > I notice this pattern in your functions too. You are using a while loop to repeat a fixed number of times. A for loop is the normal way to do that and is guaranteed to stop...: For example your square function has: c=0 while c<4: left(angle) forward(taille) right(90) c = c+1 which can be written: for c in range(4): left(angle) forward(taille) right(90) But notice the left and right kind of cancel out so you usually won't get a square... I suspect you meant to have: left(angle) for c in range(4): forward(taille) right(90) Similarly for the triangle I think you want: left(angle) for c in range(3): forward(taille) right(120) When working with the turtle its a good idea to test your functions at the interactive prompt (>>>). Put the functions in a file and import the file then you can call them directly and see whether they draw what you expect. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Mar 12 01:23:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 00:23:43 -0000 Subject: [Tutor] Recommendations on Workshops, Courses, Live Online Training References: <2ac5d4851003111128l60bf864s21716ab2945d1a54@mail.gmail.com> Message-ID: "Tino Dai" wrote > I do know of a place in North Carolina, and the president of the > company > > spoke @ PyCon this year. I don't know if this is the correct venue to put > that sort of information. Guidance please. :) I don't think I'd encourage regular announcements but where someone is specifically looking then its appropriate. Although North Carolina won't help the OP much in this case if he is based in the Middle east :-) Alan G. From alan.gauld at btinternet.com Fri Mar 12 01:26:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 00:26:19 -0000 Subject: [Tutor] use of __new__ References: <20100311200335.76647c2a@o> Message-ID: "spir" wrote > The issue is the object (self) is then a unicode one instead of my own > type. I think you need to modify self in __new__ > > class Unicode(unicode): > Unicode.FORMAT = "utf8" > def __new__(self, text, format=None): > # text can be str or unicode > format = Unicode.FORMAT if format is None else format > if isinstance(text,str): > text = text.decode(format) self = text.decode(format) But I've only used __new__ once before so am no expert! Alan G. From steve at pearwood.info Fri Mar 12 01:53:16 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 11:53:16 +1100 Subject: [Tutor] use of __new__ In-Reply-To: <20100311200335.76647c2a@o> References: <20100311200335.76647c2a@o> Message-ID: <201003121153.16814.steve@pearwood.info> On Fri, 12 Mar 2010 06:03:35 am spir wrote: > Hello, > > I need a custom unicode subtype (with additional methods). This will > not be directly used by the user, instead it is just for internal > purpose. I would like the type to be able to cope with either a byte > str or a unicode str as argument. In the first case, it needs to be > first decoded. I cannot do it in __init__ because unicode will first > try to decode it as ascii, which fails in the general case. Are you aware that you can pass an explicit encoding to unicode? >>> print unicode('cdef', 'utf-16') ?? >>> help(unicode) Help on class unicode in module __builtin__: class unicode(basestring) | unicode(string [, encoding[, errors]]) -> object > So, I > must have my own __new__. The issue is the object (self) is then a > unicode one instead of my own type. > > class Unicode(unicode): > Unicode.FORMAT = "utf8" > def __new__(self, text, format=None): > # text can be str or unicode > format = Unicode.FORMAT if format is None else format > if isinstance(text,str): > text = text.decode(format) > return text > ....... > > x = Unicode("abc") # --> unicode, not Unicode That's because you return a unicode object :) Python doesn't magically convert the result of __new__ into your class, in fact Python specifically allows __new__ to return something else. That's fairly unusual, but it does come in handy. "format" is not a good name to use. The accepted term is "encoding". You should also try to match the function signature of the built-in unicode object, which includes unicode() -> u''. Writing Unicode.FORMAT in the definition of Unicode can't work: >>> class Unicode(unicode): ... Unicode.FORMAT = 'abc' ... Traceback (most recent call last): File "", line 1, in File "", line 2, in Unicode NameError: name 'Unicode' is not defined So it looks like you've posted something slightly different from what you are actually running. I have tried to match the behaviour of the built-in unicode as close as I am able. See here: http://docs.python.org/library/functions.html#unicode class Unicode(unicode): """Unicode(string [, encoding[, errors]]) -> object Special Unicode class that has all sorts of wonderful methods missing from the built-in unicode class. """ _ENCODING = "utf8" _ERRORS = "strict" def __new__(cls, string='', encoding=None, errors=None): # If either encodings or errors is specified, then always # attempt decoding of the first argument. if (encoding, errors) != (None, None): if encoding is None: encoding = cls._ENCODING if errors is None: errors = cls._ERRORS obj = super(Unicode, cls).__new__( Unicode, string, encoding, errors) else: # Never attempt decoding. obj = super(Unicode, cls).__new__(Unicode, string) assert isinstance(obj, Unicode) return obj >>> Unicode() u'' >>> Unicode('abc') u'abc' >>> Unicode('cdef', 'utf-16') u'\u6463\u6665' >>> Unicode(u'abcd') u'abcd' -- Steven D'Aprano From steve at pearwood.info Fri Mar 12 02:17:06 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 12:17:06 +1100 Subject: [Tutor] use of __new__ In-Reply-To: References: <20100311200335.76647c2a@o> Message-ID: <201003121217.06668.steve@pearwood.info> On Fri, 12 Mar 2010 11:26:19 am Alan Gauld wrote: > "spir" wrote > > > The issue is the object (self) is then a unicode one instead of my > > own type. > > I think you need to modify self in __new__ The method signature for __new__ is usually written as: def __new__(cls, args): because when __new__ is called, no instance yet exists. __new__ is the constructor method which creates an instance, so it gets the class as the first instance. __new__ can then do one of two things: (1) return a new instance of your class; or (2) return something else. If it returns an instance of your class, Python then automatically calls the initializer __init__ with that instance as an argument (plus any other arguments passed to __new__). >>> class MyClass(object): ... def __new__(cls): ... print "Calling __new__ on object %s" % cls ... return super(MyClass, cls).__new__(cls) ... def __init__(self): ... print "Calling __init__ on object %s" % self ... >>> o = MyClass() Calling __new__ on object Calling __init__ on object <__main__.MyClass object at 0xb7c6f44c> >>> o <__main__.MyClass object at 0xb7c6f44c> For mutable types, you can modify self inside __init__, but that doesn't work for immutable objects like unicode, str, int, etc. For them, you have to do any changes inside __new__ BEFORE creating the instance. In the second case, where __new__ returns something else, __init__ is never called: >>> class AnotherClass(MyClass): ... def __new__(cls): ... ignore = super(AnotherClass, cls).__new__(cls) ... return 42 ... >>> o = AnotherClass() Calling __new__ on object >>> o 42 -- Steven D'Aprano From steve at pearwood.info Fri Mar 12 02:27:02 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 12:27:02 +1100 Subject: [Tutor] use of __new__ In-Reply-To: <201003121153.16814.steve@pearwood.info> References: <20100311200335.76647c2a@o> <201003121153.16814.steve@pearwood.info> Message-ID: <201003121227.03170.steve@pearwood.info> On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > I have tried to match the behaviour of the built-in unicode as close > as I am able. See here: > http://docs.python.org/library/functions.html#unicode And by doing so, I entirely forgot that you want to change the default encoding from 'ascii' to 'utf-8'! Oops. Sorry about that. Try changing this bit: > else: # Never attempt decoding. > obj = super(Unicode, cls).__new__(Unicode, string) to this: # Untested else: if isinstance(string, unicode): # Don't do any decoding. obj = super(Unicode, cls).__new__(Unicode, string) else: if encoding is None: encoding = cls._ENCODING if errors is None: errors = cls._ERRORS obj = super(Unicode, cls).__new__( Unicode, string, encoding, errors) You can probably clean up the method to make it a bit tidier. -- Steven D'Aprano From steve at pearwood.info Fri Mar 12 04:04:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 14:04:51 +1100 Subject: [Tutor] use of __new__ In-Reply-To: <20100311200335.76647c2a@o> References: <20100311200335.76647c2a@o> Message-ID: <201003121404.52937.steve@pearwood.info> On Fri, 12 Mar 2010 06:03:35 am spir wrote: > Hello, > > I need a custom unicode subtype (with additional methods). [snip] Here's my second attempt, and a very simple test function that passes. Obviously you have to add your own additional methods :) class Unicode(unicode): """Unicode(string [, encoding[, errors]]) -> object Special Unicode class that has all sorts of wonderful ? ? methods missing from the built-in unicode class. ? ? """ _ENCODING = "utf8" _ERRORS = "strict" def __new__(cls, string='', encoding=None, errors=None): optional_args = not (encoding is errors is None) # Set default encoding and errors. if encoding is None: encoding = cls._ENCODING if errors is None: errors = cls._ERRORS # To match the behaviour of built-in unicode, if either # optional argument is specified, we always attempt decoding. if optional_args or not isinstance(string, unicode): args = (string, encoding, errors) else: args = (string,) return super(Unicode, cls).__new__(Unicode, *args) def test(): assert Unicode() == u'' assert Unicode('abcd') == u'abcd' u = 'cdef'.decode('utf-16') assert u == u'\u6463\u6665' s = u.encode('utf-8') assert Unicode(s) == u try: unicode(s) except UnicodeDecodeError: pass else: assert False, 'failed to fail as expected' -- Steven D'Aprano From cspears2002 at yahoo.com Fri Mar 12 05:09:02 2010 From: cspears2002 at yahoo.com (Christopher Spears) Date: Thu, 11 Mar 2010 20:09:02 -0800 (PST) Subject: [Tutor] trouble with dates and python and databases Message-ID: <839302.25417.qm@web51608.mail.re2.yahoo.com> I'm trying to write a script that calculates the rate of disk usage. I think the best way to accomplish this task is to write a script that will monitor a server's capacity and how much space is being used on a daily basis and store the information in a SQLite database. Then the program can retrieve the necessary information from the database to compute the rate. Server 1 3/11/10 10 GB Used/50 GB Capacity 3/12/10 15 GB Used/50 GB Capacity 3/13/10 17 GB Used/50 GB Capacity Rate of usage = 7 GB / 3 days = 2.3 GB per day Eventually, I want the script to issue a warning if the server is in danger of becoming full in a certain number of days. My problem is I'm not sure how to store and retrieve the dates. I assume the best way to record the date is to use datetime.date. >>> import datetime >>> datetime.date.today() datetime.date(2010, 3, 11) How could I pass the datetime object into the database? Any advice would be appreciated! From cwitts at compuscan.co.za Fri Mar 12 07:27:16 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Fri, 12 Mar 2010 08:27:16 +0200 Subject: [Tutor] trouble with dates and python and databases In-Reply-To: <839302.25417.qm@web51608.mail.re2.yahoo.com> References: <839302.25417.qm@web51608.mail.re2.yahoo.com> Message-ID: <4B99DEC4.10701@compuscan.co.za> Christopher Spears wrote: > I'm trying to write a script that calculates the rate of disk usage. I think the best way to accomplish this task is to write a script that will monitor a server's capacity and how much space is being used on a daily basis and store the information in a SQLite database. Then the program can retrieve the necessary information from the database to compute the rate. > > Server 1 > 3/11/10 10 GB Used/50 GB Capacity > 3/12/10 15 GB Used/50 GB Capacity > 3/13/10 17 GB Used/50 GB Capacity > > Rate of usage = 7 GB / 3 days = 2.3 GB per day > > Eventually, I want the script to issue a warning if the server is in danger of becoming full in a certain number of days. > > My problem is I'm not sure how to store and retrieve the dates. I assume the best way to record the date is to use datetime.date. > > >>>> import datetime >>>> datetime.date.today() >>>> > datetime.date(2010, 3, 11) > > How could I pass the datetime object into the database? > > Any advice would be appreciated! > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > cursor.execute("""select * from logs where log_date = to_date(:bind_variable, 'yyyymmdd') """, bind_variable = your_date_here) I use that style most commonly, but remember if you insert the records into the database using SYSDATE you will need to call `trunc` on your log_date field in order for it to match your input. If for example you want the last 3 days of data though to trend on you can always do your select like select log_date, usage, capacity from logs where log_date >= trunc(sysdate) - 3 order by log_date asc -- Kind Regards, Christian Witts From ludolph at openhazel.co.za Fri Mar 12 09:19:39 2010 From: ludolph at openhazel.co.za (Ludolph) Date: Fri, 12 Mar 2010 10:19:39 +0200 Subject: [Tutor] Visual Python programming and decompilers? In-Reply-To: <95cccfc91003111038y6f07efb2m623122a3b8a60127@mail.gmail.com> References: <95cccfc91003111038y6f07efb2m623122a3b8a60127@mail.gmail.com> Message-ID: <95cccfc91003120019w765ba523rad9596e530465336@mail.gmail.com> Hi Guys I posted the following message on my local pug mailing list and someone recommended I post it here. At work I have been exposed to a Agile Platform called OutSystems. It allows you to visually program your web applications http://i.imgur.com/r2F0i.png and I find the idea very intriguing. So I have started to play around with the idea on how will I be able to visually represent Python code as in the above image and then allow the programmer to change some of the flow/code/logic visually and then get it back as python source code. I don't know if this have been tried before and after some googling I can't find anything like this, so maybe I'm just lacking basic googling skills or a python solution like the above does not exist yet. If anybody knows of such solution please let me know, so that I don't spend a lot of time recreating the wheel. Otherwise help me out on the following problem: I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to disassemble the code to workable objects, It even allows me to rebuild the objects to bytecode. So if I define patterns on how python interrupts the source code to bytecode I can visually represent this and also so convert my visual representations back to bytecode. The only problem I have at the moment is how will I get this bytecode back to python source code. I have googled for python decompiler but only found old projects like unpyc, decompyle and some online services. I would like to know if anybody know of a well maintained or at least recent module that can help me accomplish the above mentioned, because I'm hoping I can implement this in Python 3.1. So any input or suggestion would be greatly appreciated. Kind Regards, -- Ludolph Neethling From alan.gauld at btinternet.com Fri Mar 12 09:20:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 08:20:13 -0000 Subject: [Tutor] trouble with dates and python and databases References: <839302.25417.qm@web51608.mail.re2.yahoo.com> Message-ID: "Christopher Spears" wrote > My problem is I'm not sure how to store and retrieve the dates. > I assume the best way to record the date is to use datetime.date. If you are using a database I'd just use the database date functions. There should be functions to generate and store dates via SQL. For SQLite: http://www.sqlite.org/lang_datefunc.html HTH, Alan G. From denis.spir at gmail.com Fri Mar 12 10:41:46 2010 From: denis.spir at gmail.com (spir) Date: Fri, 12 Mar 2010 10:41:46 +0100 Subject: [Tutor] use of __new__ In-Reply-To: <201003121227.03170.steve@pearwood.info> References: <20100311200335.76647c2a@o> <201003121153.16814.steve@pearwood.info> <201003121227.03170.steve@pearwood.info> Message-ID: <20100312104146.527e1c15@o> On Fri, 12 Mar 2010 12:27:02 +1100 Steven D'Aprano wrote: > On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > > > I have tried to match the behaviour of the built-in unicode as close > > as I am able. See here: > > http://docs.python.org/library/functions.html#unicode > > And by doing so, I entirely forgot that you want to change the default > encoding from 'ascii' to 'utf-8'! Oops. Sorry about that. > > Try changing this bit: > > > else: # Never attempt decoding. > > obj = super(Unicode, cls).__new__(Unicode, string) I get it! The call to new must pass the target class as argument: obj = unicode.__new__(Unicode, string, encoding) ^ while I passed unicode instead. I guess this is the bit that decides on the final class of the return obj. > to this: > > # Untested > else: > if isinstance(string, unicode): > # Don't do any decoding. > obj = super(Unicode, cls).__new__(Unicode, string) > else: > if encoding is None: encoding = cls._ENCODING > if errors is None: errors = cls._ERRORS > obj = super(Unicode, cls).__new__( > Unicode, string, encoding, errors) Thank you again, Steven. Denis ________________________________ la vita e estrany spir.wikidot.com From ydmt923 at gmail.com Fri Mar 12 11:03:48 2010 From: ydmt923 at gmail.com (yd) Date: Fri, 12 Mar 2010 04:03:48 -0600 Subject: [Tutor] First program Message-ID: Hi, I am new to programming, altough i have read a few books about OOP and O'Reily's Learning Python. I would like some critique on my first program, is it normal for it to be this long to do something simple? I know i could have turned some of these things into classes and functions but i don't know how to do that yet. Some critique of the algorithm and writing style or anything in general would help and any pointers would be appreciated. Thanks. #title Area calculator #author Yudhishthir Singh #welcome screen msg = 'Welcome to the area calculator program ' print(msg) print('-'*len(msg)) loop = 'y' print() while loop == 'y': #Choices menu print('Please select a shape\n') print('1. Rectangle') print('2. Square') print('3. Parallelogram ') print('4. Trapezoid ') print('5. Circle ') print('6. Ellipse') print('7. Traingle\n') print('-'*len(msg)) choice = input('\nPlease enter your choice: ') if choice.isdigit() ==True: choice = int(choice) if choice ==1: #Rect height = input('please enter the height: ') width = input('please enter the width: ') height = int(height) width = int(width) areaRectangle = height*width print('\nThe area of a rectangle with {0} height and {1} width is '.format(height,width),areaRectangle,'\n') elif choice ==2: #Square side = input('enter the height or width: ') side = int(side) areaSquare = side**2 print('\nThe area of a square with a height or width of {0} is '.format(side), areaSquare,'\n') elif choice ==3: #Parallelogram height = input('enter the height: ') base = input('enter the width aka base: ') height = int(height) base = int(base) areaParallelogram = height*base print('\nThe area of a parrallelogram with height {0} and width {1} is '.format(height,base), areaParallelogram,'\n') elif choice ==4: #Trapezoid height = input('enter the height: ') base1 = input('enter the width of shorter side: ') base2 = input('enter the width of longer side: ') height = int(height) base1 = int(base1) base2 = int(base2) areaTrapezoid = (height/2)*(base1+base2) print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is '.format(height,base1,base2), areaTrapezoid, '\n') elif choice ==5: #Circle radius = input('radius: ') radius = int(radius) areaCircle = 3.14*(radius**2) print('\nThe area of a circle with radius {0} is '.format(radius), areaCircle, '\n') elif choice ==6: #Ellipse radius1 = input('enter length of radius 1: ') radius2 = input('enter length of radius 2: ') radius1 = int(radius1) radius2 = int(radius2) areaEllipse = 3.14*radius1*radius2 print('\nThe area of an ellipse with radii of length {0} and {1} is '.format(radius1,radius2), areaEllipse, '\n') elif choice ==7: #Triangle base = input('enter base: ') height = input('enter height: ') base = int(base) height = int(height) areaTriangle = (1/2 *base)*height print('\nThe area of a triange with height {0} and base {1} is '.format(height,base), areaTriangle, '\n') else: raise Exception('{0}, is not a valid choice'.format(choice)) loop = input('Do you want to calculate the area of another shape? Y/N: ') loop = loop.lower() #todo: #Improve error checking in individual modules #Turn the calculators into classes or functions #Make it so all the print statments for the results use the same function or whatever #Make a function that turns input into integer and stores it in the original input values place #etc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Fri Mar 12 11:30:10 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 12 Mar 2010 04:30:10 -0600 Subject: [Tutor] First program In-Reply-To: References: Message-ID: On Fri, Mar 12, 2010 at 4:03 AM, yd wrote: > Hi, > I am new to programming, altough i have read a few books about OOP and > O'Reily's Learning Python. > I would like some critique on my first program, is it normal for it to be > this long to do something simple? > I know i could have turned some of these things into classes and functions > but i don't know how to do that yet. > Some critique of the algorithm and writing style or anything in general > would help and any pointers would be appreciated. > Thanks. > > > One thing you should do is use more indentation. Your program structure is VERY hard to read with single space indentations. Consider using 4-spaces. This is not overtly long, I would say it's reasonable for a first program. There are some things that you could probably group together, though. Remember that code reuse is one of the core tenets of computer programming! One example: area of square, parallelogram and rectangle are all the same. You could do something like this (assuming user enters strings as choice (rather than ints)): if choice in ['rectangle', 'square', 'parallelogram']: height = int(raw_input("Height: ")) if choice != 'square': width = int(raw_input("Width: ")) else: width = height print "A %s with dimensions %sx%s has an area of %s." % (choice, height, width, width*height) Similarly with Ellipses and Circles, you can group some stuff together. One thing, though. Most people are still using Python 2.6 (which is what my example above is in) and you appear to be using 3.0. Perhaps you should learn on 2.6 first, there are more resources and libraries available than for 3.x at the moment. I could probably make more comments but it's late and I need to go to bed, hopefully that's a decent enough start. Also (just FYI - you didn't do anything wrong) when replying to a post please use "reply-all" so that the group will get the message. If you use "reply" it will just come straight back to me. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreengels at gmail.com Fri Mar 12 11:50:22 2010 From: andreengels at gmail.com (Andre Engels) Date: Fri, 12 Mar 2010 11:50:22 +0100 Subject: [Tutor] First program In-Reply-To: References: Message-ID: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> On 3/12/10, yd wrote: > Hi, > I am new to programming, altough i have read a few books about OOP and > O'Reily's Learning Python. > I would like some critique on my first program, is it normal for it to be > this long to do something simple? Well, many of your lines are user interface. Writing two lines of text to the user will in general cost you (at least) two lines of code. > I know i could have turned some of these things into classes and functions > but i don't know how to do that yet. I definitely see use for functions (long lists of if...elif.. are usually better modeled using functions and a dictionary); using classes feels like overkill for something this simple. > Some critique of the algorithm and writing style or anything in general > would help and any pointers would be appreciated. General remark: The usual number of spaces indented per level is 4, rather than the 2 that you use. This makes it easier to see the indentation level at first glance. > #title Area calculator > #author Yudhishthir Singh > > > #welcome screen > msg = 'Welcome to the area calculator program ' > print(msg) > print('-'*len(msg)) > loop = 'y' > print() > while loop == 'y': > #Choices menu > print('Please select a shape\n') > print('1. Rectangle') > print('2. Square') > print('3. Parallelogram ') > print('4. Trapezoid ') > print('5. Circle ') > print('6. Ellipse') > print('7. Traingle\n') > print('-'*len(msg)) > choice = input('\nPlease enter your choice: ') > if choice.isdigit() ==True: > choice = int(choice) 1. The if can be shortened to if choice.isdigit(): 2. This thing can be removed completely if you chance the if-statements below to if choice == "1" etcetera. > if choice ==1: > #Rect > height = input('please enter the height: ') > width = input('please enter the width: ') > height = int(height) > width = int(width) > areaRectangle = height*width > print('\nThe area of a rectangle with {0} height and {1} width is > '.format(height,width),areaRectangle,'\n') I think it's ugly to mix styles here - either use format or use commas, not both > elif choice ==2: > #Square > side = input('enter the height or width: ') > side = int(side) > areaSquare = side**2 > print('\nThe area of a square with a height or width of {0} is > '.format(side), areaSquare,'\n') > elif choice ==3: > #Parallelogram > height = input('enter the height: ') > base = input('enter the width aka base: ') > height = int(height) > base = int(base) > areaParallelogram = height*base > print('\nThe area of a parrallelogram with height {0} and width {1} is > '.format(height,base), areaParallelogram,'\n') > elif choice ==4: > #Trapezoid > height = input('enter the height: ') > base1 = input('enter the width of shorter side: ') > base2 = input('enter the width of longer side: ') > height = int(height) > base1 = int(base1) > base2 = int(base2) > areaTrapezoid = (height/2)*(base1+base2) > print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is > '.format(height,base1,base2), areaTrapezoid, '\n') > elif choice ==5: > #Circle > radius = input('radius: ') > radius = int(radius) > areaCircle = 3.14*(radius**2) > print('\nThe area of a circle with radius {0} is '.format(radius), > areaCircle, '\n') > elif choice ==6: > #Ellipse > radius1 = input('enter length of radius 1: ') > radius2 = input('enter length of radius 2: ') > radius1 = int(radius1) > radius2 = int(radius2) > areaEllipse = 3.14*radius1*radius2 > print('\nThe area of an ellipse with radii of length {0} and {1} is > '.format(radius1,radius2), areaEllipse, '\n') > elif choice ==7: > #Triangle > base = input('enter base: ') > height = input('enter height: ') > base = int(base) > height = int(height) > areaTriangle = (1/2 *base)*height > print('\nThe area of a triange with height {0} and base {1} is > '.format(height,base), areaTriangle, '\n') > else: > raise Exception('{0}, is not a valid choice'.format(choice)) This will cause the program to stop-with-error if something wrong is entered. I think that's quite rude. I would change this to: else: print('{0}, is not a valid choice'.format(choice)) > loop = input('Do you want to calculate the area of another shape? Y/N: ') > loop = loop.lower() -- Andr? Engels, andreengels at gmail.com From denis.spir at gmail.com Fri Mar 12 12:29:17 2010 From: denis.spir at gmail.com (spir) Date: Fri, 12 Mar 2010 12:29:17 +0100 Subject: [Tutor] %s %r with cutom type In-Reply-To: References: <20100311200335.76647c2a@o> Message-ID: <20100312122917.70a2a37c@o> Hello again, A different issue. On the custom Unicode type discussed in another thread, I have overloaded __str__ and __repr__ to get encoded byte strings (here with debug prints & special formats to distinguish from builtin forms): class Unicode(unicode): ENCODING = "utf8" def __new__(self, string='', encoding=None): if isinstance(string,str): encoding = Unicode.ENCODING if encoding is None else encoding string = string.decode(encoding) return unicode.__new__(Unicode, string) def __repr__(self): print '+', return '"%s"' %(self.__str__()) def __str__(self): print '*', return '`'+ self.encode(Unicode.ENCODING) + '`' An issue happens in particuliar cases, when using both %s and %r: s = "???" us = Unicode(s) # str print us, print str(us), print us.__str__(), print "%s" %us # repr print repr(us), print us.__repr__(), print "%r" %us # both print "%s %r" %(us,us) ==> ??? * `???` * `???` ??? + * "`???`" + * "`???`" + * "`???`" + * Traceback (most recent call last): File "Unicode.py", line 38, in print "%s%r" %(us,us) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128) Note that Unicode.__str__ is called neither by "print us", nore by %s. What happens? Why does the issue only occur when using both format %s & %s? If I replace the last line by "print "%s %r" %(str(us),us)", all works fine. But then what's the point with %s? And why doesn't print alone call __str__? Denis From steve at pearwood.info Fri Mar 12 12:56:37 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 22:56:37 +1100 Subject: [Tutor] use of __new__ In-Reply-To: <20100312115611.0c9f6d54@o> References: <20100311200335.76647c2a@o> <201003121404.52937.steve@pearwood.info> <20100312115611.0c9f6d54@o> Message-ID: <201003122256.38532.steve@pearwood.info> I've taken the liberty of replying back to the list rather than in private. Denis, if you mean to deliberately reply privately, please say so at the start of the email, otherwise I will assume it was an accident. On Fri, 12 Mar 2010 09:56:11 pm spir wrote: > Side-question: Why use super() when we know it can only be unicode? super is necessary for multiple inheritance to work correctly: class SpecialString(MyOtherStringClass, Unicode): ... will have hard-to-find bugs if you don't use super. But if you are absolutely sure that you will never directly or indirectly use multiple inheritance, then you could replace the calls to super with: unicode.__new__(...) But why bother? super does the right thing for both single and multiple inheritance. > And why use cls when we know it can only be Unicode? Because you might want to subclass Unicode, and if you use cls then everything will just work correctly, but if you hard-code the name of the class, things will break. Actually, my code has a bug. I wrote: return super(Unicode, cls).__new__(Unicode, *args) in the __new__ method, but that hard-codes the name of the class. Let's try it: >>> type(Unicode()) # Unicode class as defined in my previous post. >>> class K(Unicode): ... pass ... >>> type(K()) Broken! I hang my head in shame :( So you need to replace the above return with: return super(Unicode, cls).__new__(cls, *args) and then it will work correctly: >>> class K(Unicode): ... pass ... >>> type(K()) You might be tempted to change the first reference to Unicode to cls as well, but sadly that does not work. The reason is complicated, and to be honest I don't remember it, but you will probably find it by googling for "python super gotchas". -- Steven D'Aprano From c.t.matsumoto at gmail.com Fri Mar 12 13:04:13 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Fri, 12 Mar 2010 13:04:13 +0100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B9948CE.10201@dcsoftware.com> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> <4B9948CE.10201@dcsoftware.com> Message-ID: <4B9A2DBD.1020202@gmail.com> Jeff Johnson wrote: > C.T. Matsumoto wrote: >> Dave Angel wrote: >>> (You forgot to do a Reply-All, so your message went to just me, >>> rather than to me and the list ) >>> >>> >>> C.T. Matsumoto wrote: >>>> Dave Angel wrote: >>>>> C.T. Matsumoto wrote: >>>>>> Hello, >>>>>> >>>>>> This is follow up on a question I had about algorithms. In the >>>>>> thread it was suggested I make my own sorting algorithm. >>>>>> >>>>>> Here are my results. >>>>>> >>>>>> #!/usr/bin/python >>>>>> >>>>>> def sort_(list_): >>>>>> for item1 in list_: >>>>>> pos1 = list_.index(item1) >>>>>> pos2 = pos1 + 1 >>>>>> try: >>>>>> item2 = list_[pos2] >>>>>> except IndexError: >>>>>> pass >>>>>> >>>>>> if item1 >= item2: >>>>>> try: >>>>>> list_.pop(pos2) >>>>>> list_.insert(pos1, item2) >>>>>> return True >>>>>> except IndexError: >>>>>> pass >>>>>> >>>>>> def mysorter(list_): >>>>>> while sort_(list_) is True: >>>>>> sort_(list_) >>>>>> >>>>>> I found this to be a great exercise. In doing the exercise, I got >>>>>> pretty stuck. I consulted another programmer (my dad) who >>>>>> described how to go about sorting. As it turned out the >>>>>> description he described was the Bubble sort algorithm. Since >>>>>> coding the solution I know the Bubble sort is inefficient because >>>>>> of repeated iterations over the entire list. This shed light on >>>>>> the quick sort algorithm which I'd like to have a go at. >>>>>> >>>>>> Something I haven't tried is sticking in really large lists. I was >>>>>> told that with really large list you break down the input list >>>>>> into smaller lists. Sort each list, then go back and use the same >>>>>> swapping procedure for each of the different lists. My question >>>>>> is, at what point to you start breaking things up? Is that based >>>>>> on list elements or is it based on memory(?) resources python is >>>>>> using? >>>>>> >>>>>> One thing I'm not pleased about is the while loop and I'd like to >>>>>> replace it with a for loop. >>>>>> >>>>>> Thanks, >>>>>> >>>>>> T >>>>>> >>>>>> >>>>> There are lots of references on the web about Quicksort, including >>>>> a video at: >>>>> http://www.youtube.com/watch?v=y_G9BkAm6B8 >>>>> >>>>> which I think illustrates it pretty well. It would be a great >>>>> learning exercise to implement Python code directly from that >>>>> description, without using the sample C++ code available. >>>>> >>>>> (Incidentally, there are lots of variants of Quicksort, so I'm not >>>>> going to quibble about whether this is the "right" one to be called >>>>> that.) >>>>> >>>>> I don't know what your earlier thread was, since you don't mention >>>>> the subject line, but there are a number of possible reasons you >>>>> might not have wanted to use the built-in sort. The best one is >>>>> for educational purposes. I've done my own sort for various >>>>> reasons in the past, even though I had a library function, since >>>>> the library function had some limits. One time I recall, the >>>>> situation was that the library sort was limited to 64k of total >>>>> data, and I had to work with much larger arrays (this was in 16bit >>>>> C++, in "large" model). I solved the size problem by using the >>>>> C++ sort library on 16k subsets (because a pointer was 2*2 bytes). >>>>> Then I merged the results of the sorts. At the time, and in the >>>>> circumstances involved, there were seldom more than a dozen or so >>>>> sublists to merge, so this approach worked well enough. >>>>> >>>>> Generally, it's better for both your development time and the >>>>> efficiency and reliabilty of the end code, to base a new sort >>>>> mechanism on the existing one. In my case above, I was replacing >>>>> what amounted to an insertion sort, and achieved a 50* improvement >>>>> for a real customer. It was fast enough that other factors >>>>> completely dominated his running time. >>>>> >>>>> But for learning purposes? Great plan. So now I'll respond to >>>>> your other questions, and comment on your present algorithm. >>>>> >>>>> It would be useful to understand about algorithmic complexity, the >>>>> so called Order Function. In a bubble sort, if you double the size >>>>> of the array, you quadruple the number of comparisons and swaps. >>>>> It's order N-squared or O(n*n). So what works well for an array >>>>> of size 10 might take a very long time for an array of size 10000 >>>>> (like a million times as long). You can do much better by sorting >>>>> smaller lists, and then combining them together. Such an algorithm >>>>> can be O(n*log(n)). >>>>> >>>>> >>>>> You ask at what point you consider sublists? In a language like C, >>>>> the answer is when the list is size 3 or more. For anything larger >>>>> than 2, you divide into sublists, and work on them. >>>>> >>>>> Now, if I may comment on your code. You're modifying a list while >>>>> you're iterating through it in a for loop. In the most general >>>>> case, that's undefined. I think it's safe in this case, but I >>>>> would avoid it anyway, by just using xrange(len(list_)-1) to >>>>> iterate through it. You use the index function to find something >>>>> you would already know -- the index function is slow. And the >>>>> first try/except isn't needed if you use a -1 in the xrange >>>>> argument, as I do above. >>>>> >>>>> You use pop() and push() to exchange two adjacent items in the >>>>> list. Both operations copy the remainder of the list, so they're >>>>> rather slow. Since you're exchanging two items in the list, you >>>>> can simply do that: >>>>> list[pos1], list[pos2] = list[pos2], list[pos1] >>>>> >>>>> That also eliminates the need for the second try/except. >>>>> >>>>> You mention being bothered by the while loop. You could replace it >>>>> with a simple for loop with xrange(len(list_)), since you know that >>>>> N passes will always be enough. But if the list is partially >>>>> sorted, your present scheme will end sooner. And if it's fully >>>>> sorted, it'll only take one pass over the data. >>>>> >>>>> There are many refinements you could do. For example, you don't >>>>> have to stop the inner loop after the first swap. You could finish >>>>> the buffer, swapping any other pairs that are out of order. You'd >>>>> then be saving a flag indicating if you did any swaps. You could >>>>> keep a index pointing to the last pair you swapped on the previous >>>>> pass, and use that for a limit next time. Then you just terminate >>>>> the outer loop when that limit value is 1. You could even keep two >>>>> limit values, and bubble back and forth between them, as they >>>>> gradually close into the median of the list. You quit when they >>>>> collide in the middle. >>>>> >>>>> The resultant function should be much faster for medium-sized >>>>> lists, but it still will slow down quadratically as the list size >>>>> increases. You still need to divide and conquer, and quicksort is >>>>> just one way of doing that. >>>>> >>>>> DaveA >>>>> >>>> Thanks a lot Dave, >>>> >>>> Sorry the original thread is called 'Python and algorithms'. >>>> >>>> Yes, I think it's best to use what python provides and build on top >>>> of that. I got to asking my original question based on trying to >>>> learn more about algorithms in general, through python. Of late many >>>> people have been asking me how well I can 'build' algorithms, and >>>> this prompted me to start the thread. This is for learning purposes >>>> (which the original thread will give you and indication where I'm >>>> coming from). >>>> >>>> The refactored code looks like this. I have tackled a couple items. >>>> First the sub-listing (which I'll wait till I can get the full sort >>>> working), then the last couple of paragraphs about refinements. >>>> Starting with the first refinement, I'm not sure how *not* to stop >>>> the inner loop? >>>> >>>> def s2(list_): >>>> for pos1 in xrange(len(list_)-1): >>>> item1 = list_[pos1] >>>> pos2 = pos1 + 1 >>>> item2 = list_[pos2] >>>> if item1 >= item2: >>>> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >>>> return True >>>> >>>> def mysorter(list_): >>>> # This is the outer loop? >>>> while s2(list_) is True: >>>> # Calling s2 kicks off the inner loop? >>>> s2(list_) >>>> >>>> if __name__ == '__main__': >>>> from random import shuffle >>>> foo = range(10) >>>> shuffle(foo) >>>> mysorter(foo) >>>> >>>> >>>> Thanks again. >>>> >>> As before, I'm not actually trying this code, so there may be typos. >>> But assuming your code here works, the next refinement would be: >>> >>> In s2() function, add a flag variable, initially False. Then instead >>> of the return True, just say flag=True >>> Then at the end of the function, return flag >>> >>> About the while loop. No need to say 'is True' just use while >>> s2(list_): And no need to call s2() a second time. >>> >>> while s2(list_): >>> pass >> Okay up to here I follow. This all makes sense. >> >> def s2(list_): >> flag = False >> for pos1 in xrange(len(list_)-1): >> item1 = list_[pos1] >> pos2 = pos1 + 1 >> item2 = list_[pos2] >> if item1 >= item2: >> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >> flag = True return flag >> >> def mysorter(list_): >> while s2(list_): >> pass >>> >>> Before you can refine the upper limit, you need a way to preserve it >>> between calls. Simplest way to do that is to combine the two >>> functions, as a nested loop. Then, instead of flag, you can have a >>> value "limit" which indicates what index was last swapped. And the >>> inner loop uses that as an upper limit on its xrange. >> Where I start to get confused is refining the 'upper limit'. What is >> the upper limit defining? I'm guessing it is the last position processed. >> >> T >>> >>> DaveA >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > Take out the = in the following line > if item1 >= item2: > and it will sort like items together, which I think is what you > originally wanted. > > Also change: > > return flag > to: > > return flag > So that False gets returned if you don't make a swap. > > This worked for me. Thank you for the interesting thread! > Thanks Jeff. Indeed when I kept the code as is and added a doubled element to the input list, it went into an infinite loop. For running the swap it doesn't matter if the elements are equal. Catching equal elements makes a recursive loop. As I found out when I tested it. T From c.t.matsumoto at gmail.com Fri Mar 12 13:20:55 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Fri, 12 Mar 2010 13:20:55 +0100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B994E14.4080205@ieee.org> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> <4B994E14.4080205@ieee.org> Message-ID: <4B9A31A7.1080901@gmail.com> Dave Angel wrote: > C.T. Matsumoto wrote: >> Dave Angel wrote: >>> (You forgot to do a Reply-All, so your message went to just me, >>> rather than to me and the list ) >>> >>> >>> C.T. Matsumoto wrote: >>>> Dave Angel wrote: >>>>> C.T. Matsumoto wrote: >>>>>> Hello, >>>>>> >>>>>> This is follow up on a question I had about algorithms. In the >>>>>> thread it was suggested I make my own sorting algorithm. >>>>>> >>>>>> Here are my results. >>>>>> >>>>>> #!/usr/bin/python >>>>>> >>>>>> def sort_(list_): >>>>>> for item1 in list_: >>>>>> pos1 = list_.index(item1) >>>>>> pos2 = pos1 + 1 >>>>>> try: >>>>>> item2 = list_[pos2] >>>>>> except IndexError: >>>>>> pass >>>>>> >>>>>> if item1 >= item2: >>>>>> try: >>>>>> list_.pop(pos2) >>>>>> list_.insert(pos1, item2) >>>>>> return True >>>>>> except IndexError: >>>>>> pass >>>>>> >>>>>> def mysorter(list_): >>>>>> while sort_(list_) is True: >>>>>> sort_(list_) >>>>>> >>>>>> I found this to be a great exercise. In doing the exercise, I got >>>>>> pretty stuck. I consulted another programmer (my dad) who >>>>>> described how to go about sorting. As it turned out the >>>>>> description he described was the Bubble sort algorithm. Since >>>>>> coding the solution I know the Bubble sort is inefficient because >>>>>> of repeated iterations over the entire list. This shed light on >>>>>> the quick sort algorithm which I'd like to have a go at. >>>>>> >>>>>> Something I haven't tried is sticking in really large lists. I was >>>>>> told that with really large list you break down the input list >>>>>> into smaller lists. Sort each list, then go back and use the same >>>>>> swapping procedure for each of the different lists. My question >>>>>> is, at what point to you start breaking things up? Is that based >>>>>> on list elements or is it based on memory(?) resources python is >>>>>> using? >>>>>> >>>>>> One thing I'm not pleased about is the while loop and I'd like to >>>>>> replace it with a for loop. >>>>>> >>>>>> Thanks, >>>>>> >>>>>> T >>>>>> >>>>>> >>>>> There are lots of references on the web about Quicksort, including >>>>> a video at: >>>>> http://www.youtube.com/watch?v=y_G9BkAm6B8 >>>>> >>>>> which I think illustrates it pretty well. It would be a great >>>>> learning exercise to implement Python code directly from that >>>>> description, without using the sample C++ code available. >>>>> >>>>> (Incidentally, there are lots of variants of Quicksort, so I'm not >>>>> going to quibble about whether this is the "right" one to be called >>>>> that.) >>>>> >>>>> I don't know what your earlier thread was, since you don't mention >>>>> the subject line, but there are a number of possible reasons you >>>>> might not have wanted to use the built-in sort. The best one is >>>>> for educational purposes. I've done my own sort for various >>>>> reasons in the past, even though I had a library function, since >>>>> the library function had some limits. One time I recall, the >>>>> situation was that the library sort was limited to 64k of total >>>>> data, and I had to work with much larger arrays (this was in 16bit >>>>> C++, in "large" model). I solved the size problem by using the >>>>> C++ sort library on 16k subsets (because a pointer was 2*2 bytes). >>>>> Then I merged the results of the sorts. At the time, and in the >>>>> circumstances involved, there were seldom more than a dozen or so >>>>> sublists to merge, so this approach worked well enough. >>>>> >>>>> Generally, it's better for both your development time and the >>>>> efficiency and reliabilty of the end code, to base a new sort >>>>> mechanism on the existing one. In my case above, I was replacing >>>>> what amounted to an insertion sort, and achieved a 50* improvement >>>>> for a real customer. It was fast enough that other factors >>>>> completely dominated his running time. >>>>> >>>>> But for learning purposes? Great plan. So now I'll respond to >>>>> your other questions, and comment on your present algorithm. >>>>> >>>>> It would be useful to understand about algorithmic complexity, the >>>>> so called Order Function. In a bubble sort, if you double the size >>>>> of the array, you quadruple the number of comparisons and swaps. >>>>> It's order N-squared or O(n*n). So what works well for an array >>>>> of size 10 might take a very long time for an array of size 10000 >>>>> (like a million times as long). You can do much better by sorting >>>>> smaller lists, and then combining them together. Such an algorithm >>>>> can be O(n*log(n)). >>>>> >>>>> >>>>> You ask at what point you consider sublists? In a language like C, >>>>> the answer is when the list is size 3 or more. For anything larger >>>>> than 2, you divide into sublists, and work on them. >>>>> >>>>> Now, if I may comment on your code. You're modifying a list while >>>>> you're iterating through it in a for loop. In the most general >>>>> case, that's undefined. I think it's safe in this case, but I >>>>> would avoid it anyway, by just using xrange(len(list_)-1) to >>>>> iterate through it. You use the index function to find something >>>>> you would already know -- the index function is slow. And the >>>>> first try/except isn't needed if you use a -1 in the xrange >>>>> argument, as I do above. >>>>> >>>>> You use pop() and push() to exchange two adjacent items in the >>>>> list. Both operations copy the remainder of the list, so they're >>>>> rather slow. Since you're exchanging two items in the list, you >>>>> can simply do that: >>>>> list[pos1], list[pos2] = list[pos2], list[pos1] >>>>> >>>>> That also eliminates the need for the second try/except. >>>>> >>>>> You mention being bothered by the while loop. You could replace it >>>>> with a simple for loop with xrange(len(list_)), since you know that >>>>> N passes will always be enough. But if the list is partially >>>>> sorted, your present scheme will end sooner. And if it's fully >>>>> sorted, it'll only take one pass over the data. >>>>> >>>>> There are many refinements you could do. For example, you don't >>>>> have to stop the inner loop after the first swap. You could finish >>>>> the buffer, swapping any other pairs that are out of order. You'd >>>>> then be saving a flag indicating if you did any swaps. You could >>>>> keep a index pointing to the last pair you swapped on the previous >>>>> pass, and use that for a limit next time. Then you just terminate >>>>> the outer loop when that limit value is 1. You could even keep two >>>>> limit values, and bubble back and forth between them, as they >>>>> gradually close into the median of the list. You quit when they >>>>> collide in the middle. >>>>> >>>>> The resultant function should be much faster for medium-sized >>>>> lists, but it still will slow down quadratically as the list size >>>>> increases. You still need to divide and conquer, and quicksort is >>>>> just one way of doing that. >>>>> >>>>> DaveA >>>>> >>>> Thanks a lot Dave, >>>> >>>> Sorry the original thread is called 'Python and algorithms'. >>>> >>>> Yes, I think it's best to use what python provides and build on top >>>> of that. I got to asking my original question based on trying to >>>> learn more about algorithms in general, through python. Of late many >>>> people have been asking me how well I can 'build' algorithms, and >>>> this prompted me to start the thread. This is for learning purposes >>>> (which the original thread will give you and indication where I'm >>>> coming from). >>>> >>>> The refactored code looks like this. I have tackled a couple items. >>>> First the sub-listing (which I'll wait till I can get the full sort >>>> working), then the last couple of paragraphs about refinements. >>>> Starting with the first refinement, I'm not sure how *not* to stop >>>> the inner loop? >>>> >>>> def s2(list_): >>>> for pos1 in xrange(len(list_)-1): >>>> item1 = list_[pos1] >>>> pos2 = pos1 + 1 >>>> item2 = list_[pos2] >>>> if item1 >= item2: >>>> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >>>> return True >>>> >>>> def mysorter(list_): >>>> # This is the outer loop? >>>> while s2(list_) is True: >>>> # Calling s2 kicks off the inner loop? >>>> s2(list_) >>>> >>>> if __name__ == '__main__': >>>> from random import shuffle >>>> foo = range(10) >>>> shuffle(foo) >>>> mysorter(foo) >>>> >>>> >>>> Thanks again. >>>> >>> As before, I'm not actually trying this code, so there may be typos. >>> But assuming your code here works, the next refinement would be: >>> >>> In s2() function, add a flag variable, initially False. Then instead >>> of the return True, just say flag=True >>> Then at the end of the function, return flag >>> >>> About the while loop. No need to say 'is True' just use while >>> s2(list_): And no need to call s2() a second time. >>> >>> while s2(list_): >>> pass >> Okay up to here I follow. This all makes sense. >> >> def s2(list_): >> flag = False >> for pos1 in xrange(len(list_)-1): >> item1 = list_[pos1] >> pos2 = pos1 + 1 >> item2 = list_[pos2] >> if item1 >= item2: >> list_[pos1], list_[pos2] = list_[pos2], list_[pos1] >> flag = True return flag >> >> def mysorter(list_): >> while s2(list_): >> pass >>> >>> Before you can refine the upper limit, you need a way to preserve it >>> between calls. Simplest way to do that is to combine the two >>> functions, as a nested loop. Then, instead of flag, you can have a >>> value "limit" which indicates what index was last swapped. And the >>> inner loop uses that as an upper limit on its xrange. >> Where I start to get confused is refining the 'upper limit'. What is >> the upper limit defining? I'm guessing it is the last position processed. >> >> T >> > The upper limit is the index of the last swap you had to make. That'd > be either pos1 or pos2, I forget which. Anyway, the inner loop goes to > that limit, instead of to the len(_list) The idea is that anything > beyond that point is already sorted, and already contains the highest > elements, so you don't need to visit it again. > > This is all from memory, so I think I'm right, but not positive. It's > been many years since I had to actually code a sort loop by hand. > Probably 20 or so. > > DaveA > > Alright, I got the idea of the upper limit and feeding it to xrange, which can take a start argument. The inner loop code looks like this: while : for pos1 in xrange(limit, len(list_)-1): pos2 = pos1 + 1 item1 = list_[pos1] item2 = list_[pos2] if item1 > item2: list_[pos1], list_[pos2] = list_[pos2], list_[pos1] limit = pos2 return limit what is the initial value of limit? and what does while use as a truth value? BTW thanks for looking back, much appreciated. T From denis.spir at gmail.com Fri Mar 12 13:22:26 2010 From: denis.spir at gmail.com (spir) Date: Fri, 12 Mar 2010 13:22:26 +0100 Subject: [Tutor] Visual Python programming and decompilers? In-Reply-To: <95cccfc91003120019w765ba523rad9596e530465336@mail.gmail.com> References: <95cccfc91003111038y6f07efb2m623122a3b8a60127@mail.gmail.com> <95cccfc91003120019w765ba523rad9596e530465336@mail.gmail.com> Message-ID: <20100312132226.107aa119@o> On Fri, 12 Mar 2010 10:19:39 +0200 Ludolph wrote: > Hi Guys > > I posted the following message on my local pug mailing list and > someone recommended I post it here. > > At work I have been exposed to a Agile Platform called OutSystems. It > allows you to visually program your web applications > http://i.imgur.com/r2F0i.png and I find the idea very intriguing. > > So I have started to play around with the idea on how will I be able > to visually represent Python code as in the above image and then allow > the programmer to change some of the flow/code/logic visually and then > get it back as python source code. I don't know if this have been > tried before and after some googling I can't find anything like this, > so maybe I'm just lacking basic googling skills or a python solution > like the above does not exist yet. > > If anybody knows of such solution please let me know, so that I don't > spend a lot of time recreating the wheel. There has been (and probably still are) numerous projects around visual programming. > Otherwise help me out on the following problem: > I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to > disassemble the code to workable objects, It even allows me to rebuild > the objects to bytecode. So if I define patterns on how python > interrupts the source code to bytecode I can visually represent this > and also so convert my visual representations back to bytecode. > > The only problem I have at the moment is how will I get this bytecode > back to python source code. I have googled for python decompiler but > only found old projects like unpyc, decompyle and some online > services. I would like to know if anybody know of a well maintained or > at least recent module that can help me accomplish the above > mentioned, because I'm hoping I can implement this in Python 3.1. > > So any input or suggestion would be greatly appreciated. Don't understand why you work at the bytecode level. > Kind Regards, > > -- > Ludolph Neethling Denis ________________________________ la vita e estrany spir.wikidot.com From steve at pearwood.info Fri Mar 12 13:40:06 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 Mar 2010 23:40:06 +1100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B9A2DBD.1020202@gmail.com> References: <4B8E3247.4050500@gmx.net> <4B9948CE.10201@dcsoftware.com> <4B9A2DBD.1020202@gmail.com> Message-ID: <201003122340.07211.steve@pearwood.info> On Fri, 12 Mar 2010 11:04:13 pm C.T. Matsumoto wrote: [snip 269 lines of quoted text] > Thanks Jeff. Indeed when I kept the code as is and added a doubled > element to the input list, it went into an infinite loop. For running > the swap it doesn't matter if the elements are equal. Catching equal > elements makes a recursive loop. As I found out when I tested it. In future, could you please trim your quoting to be a little less excessive? There's no need to included the ENTIRE history of the thread in every post. That was about four pages of quoting to add five short sentences! Thank you. -- Steven D'Aprano From denis.spir at gmail.com Fri Mar 12 14:03:25 2010 From: denis.spir at gmail.com (spir) Date: Fri, 12 Mar 2010 14:03:25 +0100 Subject: [Tutor] use of __new__ In-Reply-To: <201003122256.38532.steve@pearwood.info> References: <20100311200335.76647c2a@o> <201003121404.52937.steve@pearwood.info> <20100312115611.0c9f6d54@o> <201003122256.38532.steve@pearwood.info> Message-ID: <20100312140325.0001b7ba@o> On Fri, 12 Mar 2010 22:56:37 +1100 Steven D'Aprano wrote: > You might be tempted to change the first reference to Unicode to cls as > well, but sadly that does not work. The reason is complicated, and to > be honest I don't remember it, but you will probably find it by > googling for "python super gotchas". Thank to your explanations, I may now have a guess on this :-) class BASE(root): def __new__(cls, ...): ... obj = super(BASE, cls).__new__(cls, *args) Let's say BASE is itself sub classed, but sub classes don't define their own __new__. How else ensure that the root class (the one actually creating new objects), is (one of) BASE's own base(s)? BASE beeing the top of a custom class tree, it's a kind of constant for its sub classes. Is this really a flaw, or instead a logical necessity? Denis ________________________________ la vita e estrany spir.wikidot.com From Mike.Hansen at atmel.com Fri Mar 12 17:00:42 2010 From: Mike.Hansen at atmel.com (Hansen, Mike) Date: Fri, 12 Mar 2010 09:00:42 -0700 Subject: [Tutor] Recommendations on Workshops, Courses, Live Online Training In-Reply-To: <2ac5d4851003111128l60bf864s21716ab2945d1a54@mail.gmail.com> References: <2ac5d4851003111128l60bf864s21716ab2945d1a54@mail.gmail.com> Message-ID: <7941B2693F32294AAF16C26B679A258D0F25AD16@csomb01.corp.atmel.com> > -----Original Message----- > From: tutor-bounces+mike.hansen=atmel.com at python.org > [mailto:tutor-bounces+mike.hansen=atmel.com at python.org] On > Behalf Of Tino Dai > Sent: Thursday, March 11, 2010 12:29 PM > To: Alan Gauld > Cc: tutor at python.org > Subject: Re: [Tutor] Recommendations on Workshops, > Courses,Live Online Training > > > > On Thu, Mar 11, 2010 at 1:22 PM, Alan Gauld > wrote: > > > > "Khalid Al-Ghamdi" wrote > > > > I've subscribed to ShowMeDo, but I feel > something more than just video > tutorials. Do you have any recommendations on > where I can find workshops, > Courses, Live Online Training where I can > interact with a real person that I > can ask questions and find the answers I'm looking for. > > > > Well (most) folks on the tutor list are live, and real > opersons and we > answer questions... But if you mean face to face then > consider a Python > users group - or evenas Linux User Group(more of them) > since Linux > users are often python users too... > > > > > Alan and the rest of the tutor regulars, > > I do know of a place in North Carolina, and the president > of the company > spoke @ PyCon this year. I don't know if this is the correct > venue to put that > sort of information. Guidance please. :) > > -Tino > > > If you have a pile of $ that you don't know what to do with, or if your company has deep pockets, then Big Nerd Ranch sounds like fun. http://www.bignerdranch.com/classes/python Mike From c.t.matsumoto at gmail.com Fri Mar 12 18:09:45 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Fri, 12 Mar 2010 18:09:45 +0100 Subject: [Tutor] sorting algorithm In-Reply-To: <4B994E14.4080205@ieee.org> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> <4B994E14.4080205@ieee.org> Message-ID: <4B9A7559.7030100@gmail.com> I've change the code and I think I have what you were talking about. def mysort(list_): for i in xrange(0, len(list_)): pos = i for j in xrange(pos+1, len(list_)): if list_[i] > list_[j]: pos = j list_[i], list_[j] = list_[j], list_[i] I finally started to think that the while couldn't remain. But if I look at this the thing that I don't get is the 'xrange(pos+1, len(list_))' snippet. What confused me was how did a new position get passed xrange(), when I do not see where it that was happening. Is 'pos' a reference to the original pos in the xrange snippet? T From marcodrompre at gmail.com Fri Mar 12 18:19:50 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Fri, 12 Mar 2010 12:19:50 -0500 Subject: [Tutor] Problem with turtle In-Reply-To: References: <4bdcec5e1003110711o578f8ed9x926c033bf4a8e9d1@mail.gmail.com> <4bdcec5e1003111129p4cec64a2ndc4ba9ec716a38f6@mail.gmail.com> Message-ID: <4bdcec5e1003120919h639f4c14q86286101c2d94f21@mail.gmail.com> The thing is that the teacher wanted us to use a while loop like in the notes but even in the notes I copied the code and it was not working either. Now, I'm more on the right track, I am able to draw the forms and the counter is almost working, the reason I say that is because turtle is doin what I want it to do but twice and then its bugs. For example it will trace my 10 red squares with a specific space between them then it will redraw them all once but all on the same squares like it would want the trace to be stronger or somehting like that. Here's my code now: from turtle import * def carre(taille, couleur, angle): "fonction qui dessine un carr? de taille et de couleur d?termin?es" color(couleur) c=0 while c<4: left(angle) forward(taille) right(90) c = c+1 def triangle(taille, couleur, angle): "fonction qui dessine un triangle ?quilat?ral de taille, de couleur et d'angle d?termin?s" color(couleur) c=0 while c<3: left(angle) forward(taille) right(120) c = c+1 from dessins_tortue import * up() goto(-400,0) n=0 while n < 10: down() # abaisser le crayon carre(25*n, 'red', 0) # tracer un carr? up() forward(30*n) n=n+1 #triangle(90, 'blue',0) #up() #forward(115) I put the triangle fucntion in comments cause I was trying to make the carr? function working before. Thank You -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at dcsoftware.com Fri Mar 12 19:44:38 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Fri, 12 Mar 2010 11:44:38 -0700 Subject: [Tutor] sorting algorithm In-Reply-To: <201003122340.07211.steve@pearwood.info> References: <4B8E3247.4050500@gmx.net> <4B9948CE.10201@dcsoftware.com> <4B9A2DBD.1020202@gmail.com> <201003122340.07211.steve@pearwood.info> Message-ID: <4B9A8B96.7000509@dcsoftware.com> Steven D'Aprano wrote: > > In future, could you please trim your quoting to be a little less > excessive? There's no need to included the ENTIRE history of the thread > in every post. That was about four pages of quoting to add five short > sentences! > > > Thank you. Will do. I usually do just didn't on that thread. Thanks, -- Jeff Jeff Johnson jeff at dcsoftware.com http://www.VetsFindingVets.org From alan.gauld at btinternet.com Fri Mar 12 23:04:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Mar 2010 22:04:11 -0000 Subject: [Tutor] First program References: Message-ID: "yd" wrote > I would like some critique on my first program, is it normal for it to be > this long to do something simple? Its OK for a first program. There are many things that could be improved but its not too bad. As to length, it could be shortened a bit but this is not really a long program. Commercial applications like Microsoft Office contain millions of lines of code and have teams of a hundred or more programmers working on them. Remember that computers can only move data around and perform very basic calculations and comparisons. All the "intelligence" comes from you, the programmer. The good news is that (a)you get used to it and (b) you can produce useful programs with only a few dozen or hundreds of lines. A single programmer can easily manage say 10,000 lines of code and thats enough to write some seriously useful stuff. > I know i could have turned some of these things into classes and > functions > but i don't know how to do that yet. Functions would help with the structure but not really shortened it much because you don't reuse the code all that much - although there are ways to change that too at the expense of making the algorithms a little less obvious.. > msg = 'Welcome to the area calculator program ' > print(msg) > print('-'*len(msg)) > loop = 'y' > print() > while loop == 'y': > #Choices menu > print('Please select a shape\n') > print('1. Rectangle') > print('2. Square') > print('3. Parallelogram ') > print('4. Trapezoid ') > print('5. Circle ') > print('6. Ellipse') > print('7. Traingle\n') > print('-'*len(msg)) Consider usiong pythons long (triple quoted) strings for this kind of thing. Its more readable IMHO and less typing for you. > choice = input('\nPlease enter your choice: ') > if choice.isdigit() ==True: > choice = int(choice) Consider wrapping the whjole thing in a try/except structure. Then you can convert to int() as you read the input. Alternatively, since its only a menu and you don;t use the ints for calculation purposes just compare to the string values. > if choice ==1: HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From dwightdhutto at yahoo.com Sat Mar 13 02:21:33 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Fri, 12 Mar 2010 17:21:33 -0800 (PST) Subject: [Tutor] Visual Python programming and decompilers? In-Reply-To: <20100312132226.107aa119@o> Message-ID: <220102.54133.qm@web45307.mail.sp1.yahoo.com> --- On Fri, 3/12/10, spir wrote: > From: spir > Subject: Re: [Tutor] Visual Python programming and decompilers? > To: tutor at python.org > Date: Friday, March 12, 2010, 7:22 AM > On Fri, 12 Mar 2010 10:19:39 +0200 > Ludolph > wrote: > > > Hi Guys > > > > I posted the following message on my local pug mailing > list and > > someone recommended I post it here. > > > > At work I have been exposed to a Agile Platform called > OutSystems. It > > allows you to visually program your web applications > > http://i.imgur.com/r2F0i.png and I find the idea very > intriguing. > > > > So I have started to play around with the idea on how > will I be able > > to visually represent Python code as in the above > image and then allow > > the programmer to change some of the flow/code/logic > visually and then > > get it back as python source code. I don't know if > this have been > > tried before and after some googling I can't find > anything like this, > > so maybe I'm just lacking basic googling skills or a > python solution > > like the above does not exist yet. > > > > If anybody knows of such solution please let me know, > so that I don't > > spend a lot of time recreating the wheel. > I was thinking of a similar project a while back using the game engine in blender(although I'm sure there's other software, this was something that uses python, and is fun to play/work with), but since there are so many ide's it went to the back burner. I posted a basic outline of my idea to list a while back, I think I still have a copy of it, but the pseudo code is lost in the digital ether after an os problem. I'd be interested in working on a project like this with you if you're interested. > There has been (and probably still are) numerous projects > around visual programming. > > >? Otherwise help me out on the following problem: > > I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to > > disassemble the code to workable objects, It even > allows me to rebuild > > the objects to bytecode. So if I define patterns on > how python > > interrupts the source code to bytecode I can visually > represent this > > and also so convert my visual representations back to > bytecode. > > > > The only problem I have at the moment is how will I > get this bytecode > > back to python source code. I have googled for python > decompiler but > > only found old projects like unpyc, decompyle and some > online > > services. I would like to know if anybody know of a > well maintained or > > at least recent module that can help me accomplish the > above > > mentioned, because I'm hoping I can implement this in > Python 3.1. > > > > So any input or suggestion would be greatly > appreciated. > > Don't understand why you work at the bytecode level. > > > Kind Regards, > > > > -- > > Ludolph Neethling > > > Denis > ________________________________ > > la vita e estrany > > spir.wikidot.com > > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > David From crp at cmc.net Sat Mar 13 03:11:25 2010 From: crp at cmc.net (Ray Parrish) Date: Fri, 12 Mar 2010 18:11:25 -0800 Subject: [Tutor] First program In-Reply-To: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> Message-ID: <4B9AF44D.7040208@cmc.net> Andre Engels wrote: > On 3/12/10, yd wrote: > >> Hi, >> I am new to programming, altough i have read a few books about OOP and >> O'Reily's Learning Python. >> I would like some critique on my first program, is it normal for it to be >> this long to do something simple? >> > > Well, many of your lines are user interface. Writing two lines of text > to the user will in general cost you (at least) two lines of code. > > >> I know i could have turned some of these things into classes and functions >> but i don't know how to do that yet. >> > > I definitely see use for functions (long lists of if...elif.. are > usually better modeled using functions and a dictionary); using > classes feels like overkill for something this simple. > > >> Some critique of the algorithm and writing style or anything in general >> would help and any pointers would be appreciated. >> > > General remark: The usual number of spaces indented per level is 4, > rather than the 2 that you use. This makes it easier to see the > indentation level at first glance. > > >> #title Area calculator >> #author Yudhishthir Singh >> >> >> #welcome screen >> msg = 'Welcome to the area calculator program ' >> print(msg) >> print('-'*len(msg)) >> loop = 'y' >> print() >> while loop == 'y': >> #Choices menu >> print('Please select a shape\n') >> print('1. Rectangle') >> print('2. Square') >> print('3. Parallelogram ') >> print('4. Trapezoid ') >> print('5. Circle ') >> print('6. Ellipse') >> print('7. Traingle\n') >> print('-'*len(msg)) >> choice = input('\nPlease enter your choice: ') >> if choice.isdigit() ==True: >> choice = int(choice) >> > > 1. The if can be shortened to > > if choice.isdigit(): > > 2. This thing can be removed completely if you chance the if-statements below to > > if choice == "1" > > etcetera. > > >> if choice ==1: >> #Rect >> height = input('please enter the height: ') >> width = input('please enter the width: ') >> height = int(height) >> width = int(width) >> areaRectangle = height*width >> print('\nThe area of a rectangle with {0} height and {1} width is >> '.format(height,width),areaRectangle,'\n') >> > > I think it's ugly to mix styles here - either use format or use commas, not both > > >> elif choice ==2: >> #Square >> side = input('enter the height or width: ') >> side = int(side) >> areaSquare = side**2 >> print('\nThe area of a square with a height or width of {0} is >> '.format(side), areaSquare,'\n') >> elif choice ==3: >> #Parallelogram >> height = input('enter the height: ') >> base = input('enter the width aka base: ') >> height = int(height) >> base = int(base) >> areaParallelogram = height*base >> print('\nThe area of a parrallelogram with height {0} and width {1} is >> '.format(height,base), areaParallelogram,'\n') >> elif choice ==4: >> #Trapezoid >> height = input('enter the height: ') >> base1 = input('enter the width of shorter side: ') >> base2 = input('enter the width of longer side: ') >> height = int(height) >> base1 = int(base1) >> base2 = int(base2) >> areaTrapezoid = (height/2)*(base1+base2) >> print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is >> '.format(height,base1,base2), areaTrapezoid, '\n') >> elif choice ==5: >> #Circle >> radius = input('radius: ') >> radius = int(radius) >> areaCircle = 3.14*(radius**2) >> print('\nThe area of a circle with radius {0} is '.format(radius), >> areaCircle, '\n') >> elif choice ==6: >> #Ellipse >> radius1 = input('enter length of radius 1: ') >> radius2 = input('enter length of radius 2: ') >> radius1 = int(radius1) >> radius2 = int(radius2) >> areaEllipse = 3.14*radius1*radius2 >> print('\nThe area of an ellipse with radii of length {0} and {1} is >> '.format(radius1,radius2), areaEllipse, '\n') >> elif choice ==7: >> #Triangle >> base = input('enter base: ') >> height = input('enter height: ') >> base = int(base) >> height = int(height) >> areaTriangle = (1/2 *base)*height >> print('\nThe area of a triange with height {0} and base {1} is >> '.format(height,base), areaTriangle, '\n') >> else: >> raise Exception('{0}, is not a valid choice'.format(choice)) >> > > This will cause the program to stop-with-error if something wrong is > entered. I think that's quite rude. I would change this to: > else: > print('{0}, is not a valid choice'.format(choice)) > Here's what I get from that, could you please explain why? >>> print('{0}, is not a valid choice'.format(choice)) Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'format' >>> print('{0}, is not a valid choice'format(choice)) File "", line 1 print('{0}, is not a valid choice'format(choice)) ^ SyntaxError: invalid syntax >>> print('{0}, is not a valid choice',format(choice)) Traceback (most recent call last): File "", line 1, in NameError: name 'format' is not defined I tried taking out the dot, then replacing the dot with a comma, neither worked. Thanks, Ray Parrish > >> loop = input('Do you want to calculate the area of another shape? Y/N: ') >> loop = loop.lower() >> > > > > -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From davea at ieee.org Sat Mar 13 03:11:46 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 12 Mar 2010 21:11:46 -0500 Subject: [Tutor] sorting algorithm In-Reply-To: <4B9A7559.7030100@gmail.com> References: <4B8E3247.4050500@gmx.net> <4B8E7F67.5030602@ieee.org> <4B8E95D6.7080903@gmx.net> <4B8EA2D9.6050302@ieee.org> <4B98DBB1.6070401@gmail.com> <4B994E14.4080205@ieee.org> <4B9A7559.7030100@gmail.com> Message-ID: <4B9AF462.9060100@ieee.org> C.T. Matsumoto wrote: > I've change the code and I think I have what you were talking about. > > def mysort(list_): > > for i in xrange(0, len(list_)): > > pos = i > > for j in xrange(pos+1, len(list_)): > > if list_[i] > list_[j]: > > pos = j > > list_[i], list_[j] = list_[j], list_[i] > > I finally started to think that the while couldn't remain. But if I > look at this the thing that I don't get is the 'xrange(pos+1, > len(list_))' snippet. What confused me was how did a new position get > passed xrange(), when I do not see where it that was happening. Is > 'pos' a reference to the original pos in the xrange snippet? > > T > That loop is not what I was describing, but I think it's nearly equivalent in performance. My loop was always swapping adjacent items, and it adjusted the ending limit as the data gets closer to sorted. This one adjusts the beginning value (pos) of the inner loop, as the data gets more sorted. For some orderings, such as if the data is already fully sorted, my approach would be much faster. Your outer loop basically finds the smallest item in the list on each pass. If the line pos=j didn't exist, the inner loop would always loop from the i+1 value to the end of the list. But since we've already done a bunch of comparisons on the previous pass, no items before pos need be compared in the current pass. I'm going to be quite busy for the next couple of days. So if I don't respond to your next post quickly, please be patient. DaveA From crp at cmc.net Sat Mar 13 03:04:42 2010 From: crp at cmc.net (Ray Parrish) Date: Fri, 12 Mar 2010 18:04:42 -0800 Subject: [Tutor] First program In-Reply-To: References: Message-ID: <4B9AF2BA.5080408@cmc.net> Luke Paireepinart wrote: > > > On Fri, Mar 12, 2010 at 4:03 AM, yd > wrote: > > Hi, > I am new to programming, altough i have read a few books about OOP > and O'Reily's Learning Python. > I would like some critique on my first program, is it normal for > it to be this long to do something simple? > I know i could have turned some of these things into classes and > functions but i don't know how to do that yet. > Some critique of the algorithm and writing style or anything in > general would help and any pointers would be appreciated. > Thanks. > > > One thing you should do is use more indentation. Your program > structure is VERY hard to read with single space indentations. > Consider using 4-spaces. > This is not overtly long, I would say it's reasonable for a first program. > There are some things that you could probably group together, though. > Remember that code reuse is one of the core tenets of computer > programming! > > One example: area of square, parallelogram and rectangle are all the > same. You could do something like this (assuming user enters strings > as choice (rather than ints)): > if choice in ['rectangle', 'square', 'parallelogram']: > height = int(raw_input("Height: ")) > if choice != 'square': > width = int(raw_input("Width: ")) > else: > width = height > print "A %s with dimensions %sx%s has an area of %s." % (choice, > height, width, width*height) > > > Hello, Isn't it a little more understandable to use a construct like the following? >>> print "The area of a " + Choice + "is " str(Width) + " x " + str(Height) + " equals " + str(Width * Height) + " square feet" The area of a rectangle is 12 x 10 equals 120 square feet. I find that putting the variables on the end like that, when you're not actually applying any special formatting to them makes it less readable when I'm debugging my stuff, or when someone else is reading my code, and trying to understand it. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From andreengels at gmail.com Sat Mar 13 03:30:26 2010 From: andreengels at gmail.com (Andre Engels) Date: Sat, 13 Mar 2010 03:30:26 +0100 Subject: [Tutor] First program In-Reply-To: <4B9AF44D.7040208@cmc.net> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> Message-ID: <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> On Sat, Mar 13, 2010 at 3:11 AM, Ray Parrish wrote: > Andre Engels wrote: >> >> On 3/12/10, yd wrote: >>> ?else: >>> ? ?raise Exception('{0}, is not a valid choice'.format(choice)) >>> >> >> This will cause the program to stop-with-error if something wrong is >> entered. I think that's quite rude. I would change this to: >> ?else: >> ? ?print('{0}, is not a valid choice'.format(choice)) >> > > Here's what I get from that, could you please explain why? You're probably using Python 2.4 or 2.5; the .format method has been introduced in Python 2.6, and is considered the 'standard' way of working in Python 3. For older Python versions, this should read print('%s, is not a valid choice'%(choice)) -- Andr? Engels, andreengels at gmail.com From rabidpoobear at gmail.com Sat Mar 13 03:31:56 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 12 Mar 2010 20:31:56 -0600 Subject: [Tutor] First program In-Reply-To: <4B9AF1E8.3060203@cmc.net> References: <4B9AF1E8.3060203@cmc.net> Message-ID: Ray, please reply on-list in the future in case someone else has input. On Fri, Mar 12, 2010 at 8:01 PM, Ray Parrish wrote: > Luke Paireepinart wrote: > > print "A %s with dimensions %sx%s has an area of %s." % (choice, height, >> width, width*height) >> >> > Isn't it a little more understandable to use a construct like the > following? > > >>> print "The area of a " + Choice + "is " str(Width) + " x " + > str(Height) + " equals " + str(Width * Height) + " square feet" > > The area of a rectangle is 12 x 10 equals 120 square feet. > > I find that putting the variables on the end like that, when you're not > actually applying any special formatting to them makes it less readable when > I'm debugging my stuff, or when someone else is reading my code, and trying > to understand it. > > > Your version creates at least 10 intermediate strings before outputting. Remember strings are immutable in Python. So you're constructing strings The area of a The area of a rectangle The area of a rectangle is 12 The area of a rectangle is 12 The area of a rectangle is 12 x 10 The area of a rectangle is 12 x 10 The area of a rectangle is 12 x 10 equals 120 The area of a rectangle is 12 x 10 equals 120 The area of a rectangle is 12 x 10 equals 120 square feet With string formatting you avoid all of these intermediate strings, so it's arguably more efficient. Other than just viewing from a performance standpoint though, I find it much easier to read my version, because any computation required takes place at the end of the line. For example, your inline str(width*height) requires you to read the whole line to see it. It's really a personal thing, it's easier for me to read the formatting version than the string concatenation version, in most cases. Now if you had used the comma convention I would have seen your point. This is, I think, the easiest to read of all 3 area = width * height print "The area of a", choice, "is", width, "x", height, ", which equals", area, "square feet." Also, why are you capitalizing variable names? That's a pretty unusual convention. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Sat Mar 13 03:33:35 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 12 Mar 2010 20:33:35 -0600 Subject: [Tutor] First program In-Reply-To: <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> Message-ID: On Fri, Mar 12, 2010 at 8:30 PM, Andre Engels wrote: > On Sat, Mar 13, 2010 at 3:11 AM, Ray Parrish wrote: > > Andre Engels wrote: > >> > >> On 3/12/10, yd wrote: > >>> else: > >>> raise Exception('{0}, is not a valid choice'.format(choice)) > >>> > >> > >> This will cause the program to stop-with-error if something wrong is > >> entered. I think that's quite rude. I would change this to: > >> else: > >> print('{0}, is not a valid choice'.format(choice)) > >> > > > > Here's what I get from that, could you please explain why? > > You're probably using Python 2.4 or 2.5; the .format method has been > introduced in Python 2.6, and is considered the 'standard' way of > working in Python 3. For older Python versions, this should read > > print('%s, is not a valid choice'%(choice)) > > Also you don't have to use parenthesis around single items, and also print is not usually used as a function either. So really for older versions the convention would be print "%s, is not a valid choice" % choice -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Mar 13 03:50:55 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Mar 2010 13:50:55 +1100 Subject: [Tutor] %s %r with cutom type In-Reply-To: <20100312122917.70a2a37c@o> References: <20100311200335.76647c2a@o> <20100312122917.70a2a37c@o> Message-ID: <201003131350.56017.steve@pearwood.info> On Fri, 12 Mar 2010 10:29:17 pm spir wrote: > Hello again, > > A different issue. On the custom Unicode type discussed in another > thread, I have overloaded __str__ and __repr__ to get encoded byte > strings (here with debug prints & special formats to distinguish from > builtin forms): [...] > Note that Unicode.__str__ is called neither by "print us", nore by > %s. What happens? Why does the issue only occur when using both > format %s & %s? The print statement understands how to directly print strings (byte-strings and unicode-strings) and doesn't call your __str__ method. http://docs.python.org/reference/simple_stmts.html#the-print-statement You can demonstrate that with a much simpler example: >>> class K(unicode): ... def __str__(self): return "xyz" ... def __repr__(self): return "XYZ" ... >>> k = K("some text") >>> str(k) 'xyz' >>> repr(k) 'XYZ' >>> print k some text print only calls __str__ if the object isn't already a string. As for string interpolation, I have reported this as a bug: http://bugs.python.org/issue8128 I have some additional comments on your class below: > class Unicode(unicode): > ENCODING = "utf8" > def __new__(self, string='', encoding=None): This is broken according to the Liskov substitution principle. http://en.wikipedia.org/wiki/Liskov_substitution_principle The short summary: subclasses should only ever *add* functionality, they should never take it away. The unicode type has a function signature that accepts an encoding and an errors argument, but you've missed errors. That means that code that works with built-in unicode objects will break if your class is used instead. If that's intentional, you need to clearly document that your class is *not* entirely compatible with the built-in unicode, and preferably explain why you have done so. If it's accidental, you should fix it. A good start is the __new__ method I posted earlier. > if isinstance(string,str): > encoding = Unicode.ENCODING if encoding is None else > encoding string = string.decode(encoding) > return unicode.__new__(Unicode, string) > def __repr__(self): > print '+', > return '"%s"' %(self.__str__()) This may be a problem. Why are you making your unicode class pretend to be a byte-string? Ideally, the output of repr(obj) should follow this rule: eval(repr(obj)) == obj For instance, for built-in unicode strings: >>> u"???" == eval(repr(u"???")) True but for your subclass, us != eval(repr(us)). So again, code that works perfectly with built-in unicode objects will fail with your subclass. Ideally, repr of your class should return a string like: "Unicode('...')" but if that's too verbose, it is acceptable to just inherit the __repr__ of unicode and return something like "u'...'". Anything else should be considered non-standard behaviour and is STRONGLY discouraged. > def __str__(self): > print '*', > return '`'+ self.encode(Unicode.ENCODING) + '`' What's the purpose of the print statements in the __str__ and __repr__ methods? Again, unless you have a good reason to do different, you are best to just inherit __str__ from unicode. Anything else is strongly discouraged. > An issue happens in particuliar cases, when using both %s and %r: > > s = "???" This may be a problem. "???" is not a valid str, because it contains non-ASCII characters. The result that you get may depend on your external environment. For instance, if I run it in my terminal, with encoding set to UTF-8, I get this: >>> s = "???" >>> print s ??? >>> len(s) 6 >>> list(s) ['\xc3', '\xa9', '\xc3', '\xa2', '\xc3', '\x84'] but if I set it to ISO 8859-1, I get this: >>> list("???") ['\xe9', '\xe2', '\xc4'] As far as I know, the behaviour of stuffing unicode characters into byte-strings is not well-defined in Python, and will depend on external factors like the terminal you are running in, if any. It may or may not work as you expect. It is better to do this: u = u"???" s = u.encode('uft-8') which will always work consistently so long as you declare a source encoding at the top of your module: # -*- coding: UTF-8 -*- -- Steven D'Aprano From steve at pearwood.info Sat Mar 13 03:52:01 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Mar 2010 13:52:01 +1100 Subject: [Tutor] First program In-Reply-To: <4B9AF44D.7040208@cmc.net> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> Message-ID: <201003131352.01827.steve@pearwood.info> On Sat, 13 Mar 2010 01:11:25 pm Ray Parrish wrote: > Here's what I get from that, could you please explain why? > > ?>>> print('{0}, is not a valid choice'.format(choice)) > Traceback (most recent call last): > ? File "", line 1, in > AttributeError: 'str' object has no attribute 'format' The original poster is using Python 3.0 or 3.1, you are using an earlier version. -- Steven D'Aprano From steve at pearwood.info Sat Mar 13 04:15:37 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Mar 2010 14:15:37 +1100 Subject: [Tutor] First program In-Reply-To: <4B9AF2BA.5080408@cmc.net> References: <4B9AF2BA.5080408@cmc.net> Message-ID: <201003131415.37843.steve@pearwood.info> On Sat, 13 Mar 2010 01:04:42 pm Ray Parrish wrote: > > ? ? print "A %s with dimensions %sx%s has an area of %s." % > > (choice, height, width, width*height) > > Hello, > > Isn't it a little more understandable to use a > construct like the following? > > >>> print "The area of a " + Choice + "is " str(Width) + " x " + > str(Height) + " equals " + str(Width * Height) + " > square feet" > > The area of a rectangle is 12 x 10 equals 120 > square feet. > > I find that putting the variables on the end like > that, when you're not actually applying any special formatting to them > makes it less readable > when I'm debugging my stuff, or when someone else > is reading my code, > and trying to understand it. Of course you are welcome to use whatever coding standards you like, but I think you will find that among experienced coders, you are in a vanishingly small minority. As a beginner, I found string interpolation confusing at first, but it soon became second-nature. And of course, there are legions of C coders who are used to it. I find an expression like: "The area of a " + Choice + "is " str(Width) + " x " + str(Height) + "equals " + str(Width * Height) + "square feet" difficult to follow: too many quotes, too many sub-expressions being added, too many repeated calls to str(), it isn't clear what is the template and what is being inserted into the template. It is too easy to miss a quote and get a SyntaxError, or to forget to add spaces where needed. To me, this is MUCH easier: template = "The area of a %s is %s x %s equals %s square feet" print template % (Width, Height Width*Height) One pair of quotes instead of five, no problems with remembering to add spaces around pieces, and no need to explicitly call str(). -- Steven D'Aprano From alan.gauld at btinternet.com Sat Mar 13 10:03:28 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Mar 2010 09:03:28 -0000 Subject: [Tutor] First program References: <4B9AF2BA.5080408@cmc.net> Message-ID: "Ray Parrish" wrote >> print "A %s with dimensions %sx%s has an area of %s." % (choice, >> height, width, width*height) >> > Isn't it a little more understandable to use a > construct like the following? > >>>> print "The area of a " + Choice + "is " str(Width) + " x " + > str(Height) + " equals " + str(Width * Height) + " > square feet" It depends on where you come from. Those of us brought up on C or COBOL are used to separating the presentation from the data. Those brought up with PASCAL and BASIC are used to iterleaving data with presentation. One thing - you don't need all the str() calls in your example, print already calls str() for you. Also comma separators are better than + signs since the plus operation on strings is quite expensive - you create a new string for each addition. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rabidpoobear at gmail.com Sat Mar 13 10:30:56 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 13 Mar 2010 03:30:56 -0600 Subject: [Tutor] First program In-Reply-To: References: <4B9AF2BA.5080408@cmc.net> Message-ID: On Sat, Mar 13, 2010 at 3:03 AM, Alan Gauld wrote: > "Ray Parrish" wrote > >> print "A %s with dimensions %sx%s has an area of %s." % (choice, >>> height, width, width*height) >>> >>> Isn't it a little more understandable to use a construct like the >> following? >> >> print "The area of a " + Choice + "is " str(Width) + " x " + >>>>> >>>> str(Height) + " equals " + str(Width * Height) + " square feet" >> > > It depends on where you come from. > Those of us brought up on C or COBOL are used to separating the > presentation from the data. Those brought up with PASCAL and BASIC are used > to iterleaving data with presentation. > > One thing - you don't need all the str() calls in your example, print > already calls str() for you. Also comma separators are better than + signs > since the plus operation on strings is quite expensive - you create a new > string for each addition. > > > print actually doesn't call str if you use concatenation. So the str() calls are necessary if you do not use "," but use "+" instead. So there are at least 2 reasons why + is worse than comma. Another thing to be aware of is that if you use commas, print inserts a space in the string, which may be either an advantage or a disadvantage depending on what you're trying to do. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Sat Mar 13 10:40:31 2010 From: denis.spir at gmail.com (spir) Date: Sat, 13 Mar 2010 10:40:31 +0100 Subject: [Tutor] %s %r with cutom type In-Reply-To: <201003131350.56017.steve@pearwood.info> References: <20100311200335.76647c2a@o> <20100312122917.70a2a37c@o> <201003131350.56017.steve@pearwood.info> Message-ID: <20100313104031.3049fa9a@o> On Sat, 13 Mar 2010 13:50:55 +1100 Steven D'Aprano wrote: > On Fri, 12 Mar 2010 10:29:17 pm spir wrote: > > Hello again, > > > > A different issue. On the custom Unicode type discussed in another > > thread, I have overloaded __str__ and __repr__ to get encoded byte > > strings (here with debug prints & special formats to distinguish from > > builtin forms): > [...] > > Note that Unicode.__str__ is called neither by "print us", nore by > > %s. What happens? Why does the issue only occur when using both > > format %s & %s? > > The print statement understands how to directly print strings > (byte-strings and unicode-strings) and doesn't call your __str__ > method. > > http://docs.python.org/reference/simple_stmts.html#the-print-statement Right. But then how to print out customized strings? > As for string interpolation, I have reported this as a bug: > > http://bugs.python.org/issue8128 Yes, at least the actual behaviour should be clear and properly documented. And it should be the same for str and unicode type, and their subclasses. But I cannot see the advantage of not calling str(): this only prevents customization -- or use of string interpolation with cutomized string types. > I have some additional comments on your class below: > > > > class Unicode(unicode): > > ENCODING = "utf8" > > def __new__(self, string='', encoding=None): > > This is broken according to the Liskov substitution principle. > > http://en.wikipedia.org/wiki/Liskov_substitution_principle > > The short summary: subclasses should only ever *add* functionality, they > should never take it away. > > The unicode type has a function signature that accepts an encoding and > an errors argument, but you've missed errors. All right, I'll have a closer look to the semantics of unicode's error arg and see if it makes sense in my case. Notes for the following comments of yours: (1) What I posted is test code written only to show the issue. (eg debug prints are not in the original code) (2) This class is intended for a kind parsing and string processing library (think at pyparsing, but designed very differently). It should work only with unicode string, so convert source and every bit of string in pattern defs (eg for literal match). __str__ and __repr__ are intended for feedback (programmer test and user information, in both cases mainly error messages). __repr__ should normally not be used, I wrote it rather for completion. [...] > > if isinstance(string,str): > > encoding = Unicode.ENCODING if encoding is None else > > encoding string = string.decode(encoding) > > return unicode.__new__(Unicode, string) > > def __repr__(self): > > print '+', > > return '"%s"' %(self.__str__()) > > This may be a problem. Why are you making your unicode class pretend to > be a byte-string? (This answer rather for __str__) Not to pollute output. Eg parse tree nodes (= match results) show like: integer:[sign:- digit:123] > Ideally, the output of repr(obj) should follow this rule: > > eval(repr(obj)) == obj > > For instance, for built-in unicode strings: > > >>> u"???" == eval(repr(u"???")) > True > but for your subclass, us != eval(repr(us)). So again, code that works > perfectly with built-in unicode objects will fail with your subclass. > > Ideally, repr of your class should return a string like: > > "Unicode('...')" I 100% agree with your comment and this what I do in general. But it does not make much sense in my case, I guess. When I'm rather sure __repr__ will not normally be used, then I will probably rewrite to show Unicode("..."). > > def __str__(self): > > print '*', > > return '`'+ self.encode(Unicode.ENCODING) + '`' > > What's the purpose of the print statements in the __str__ and __repr__ > methods? Note (1). > Again, unless you have a good reason to do different, you are best to > just inherit __str__ from unicode. Anything else is strongly > discouraged. Note (2). > > An issue happens in particuliar cases, when using both %s and %r: > > > > s = "???" > > This may be a problem. "???" is not a valid str, because it contains > non-ASCII characters. It's just a test case (note (1)) for non-ascii input, precisely. > As far as I know, the behaviour of stuffing unicode characters into > byte-strings is not well-defined in Python, and will depend on external > factors like the terminal you are running in, if any. It may or may not > work as you expect. It is better to do this: > > u = u"???" > s = u.encode('uft-8') Yo, but I cannot expect every user to always use only unicode everywhere as input to my lib (both in sources to be parsed and in pattern defs) like a robot. One main reason for my Unicode type (that accepts both str and unicode). Anyway, all that source of troubles disappears with py3 :-) Then, I only need __str__ to produce nice, clear, unpolluted output. > which will always work consistently so long as you declare a source > encoding at the top of your module: > > # -*- coding: UTF-8 -*- Yes, this applies to my own code. But what about user code calling my lib? (This is the reason for Unicode.ENCODING config param). Denis ________________________________ la vita e estrany spir.wikidot.com From ydmt923 at gmail.com Sat Mar 13 10:50:58 2010 From: ydmt923 at gmail.com (yd) Date: Sat, 13 Mar 2010 03:50:58 -0600 Subject: [Tutor] First program In-Reply-To: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> Message-ID: Thanks everyone, I am trying to figure out functions and classes right now, i will probably rewrite the program once i get that down and probably use try: and except: for error catching. -------------- next part -------------- An HTML attachment was scrubbed... URL: From crp at cmc.net Sat Mar 13 13:16:35 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 04:16:35 -0800 Subject: [Tutor] Opening a dos exe In-Reply-To: References: Message-ID: <4B9B8223.6050505@cmc.net> Emile van Sebille wrote: > On 3/10/2010 11:33 AM Armstrong, Richard J. said... >> The problem comes in that the dos program requires three >> inputs (input1.txt, input2.txt and input3.txt - see attached picture) >> but I cannot find a way of getting this information to the dos program >> from python. Any ideas? You could use os.system("startprogram.bat"), and create startprogram.bat to run the dos program, and feed it the files, either all at once, or one at a time via the command line if it accepts command line input. You could write out startprogram.bat programatically, just before you call it, then remove it after running it to reduce clutter. Hope this is what you're looking for. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From steve at pearwood.info Sat Mar 13 14:21:52 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Mar 2010 00:21:52 +1100 Subject: [Tutor] %s %r with cutom type In-Reply-To: <20100313104031.3049fa9a@o> References: <20100311200335.76647c2a@o> <201003131350.56017.steve@pearwood.info> <20100313104031.3049fa9a@o> Message-ID: <201003140021.53784.steve@pearwood.info> On Sat, 13 Mar 2010 08:40:31 pm spir wrote: > > The print statement understands how to directly print strings > > (byte-strings and unicode-strings) and doesn't call your __str__ > > method. > > > > http://docs.python.org/reference/simple_stmts.html#the-print-statem > >ent > > Right. But then how to print out customized strings? print str(obj) print any_function_you_like(obj) You shouldn't have objects try to lie about what they are. If a string is "hello" (without the quotes), then you shouldn't have it pretend to be "`hello`" (with quotes). > > As for string interpolation, I have reported this as a bug: > > > > http://bugs.python.org/issue8128 > > Yes, at least the actual behaviour should be clear and properly > documented. And it should be the same for str and unicode type, and > their subclasses. Which is why I have reported it as a bug. [...] > Notes for the following comments of yours: > (1) What I posted is test code written only to show the issue. (eg > debug prints are not in the original code) (2) This class is intended > for a kind parsing and string processing library (think at pyparsing, > but designed very differently). It should work only with unicode > string, so convert source and every bit of string in pattern defs (eg > for literal match). __str__ and __repr__ are intended for feedback > (programmer test and user information, in both cases mainly error > messages). What do you mean "mainly error messages"? __str__ is intended for converting objects into a string. That's why it is called __str__ rather than __give_the_user_feedback__. > __repr__ should normally not be used, I wrote it rather > for completion. What do you mean, "for completion"? Unicode strings already have a __repr__ method. If you don't need to customize it, don't, and your class will inherit the existing __repr__ method. > > This may be a problem. Why are you making your unicode class > > pretend to be a byte-string? > > (This answer rather for __str__) > Not to pollute output. Eg parse tree nodes (= match results) show > like: integer:[sign:- digit:123] You should keep display presentation and internal value as separate as possible. If your parse tree wants to display data in a particular format, then it is the responsibility of the parse tree to format the data correctly, not of the data. In fact, the parse tree itself should never print results. That is up to the caller: perhaps you want to write it to a file, print to standard out, or standard error, save it in a string, or anything you like. >>> class ParseTree(object): ... def match_results(self, arg): ... return (42, "+", (1, 2), "something") ... def format_results(self, arg): ... result = self.match_results(arg) ... template = "%d: [%c:- digits:%s] `%s`" ... return template % result ... >>> x = ParseTree().format_results(None) >>> print x 42: [+:- digits:(1, 2)] `something` >>> myfile.write(x + '\n') >>> If the Parse Tree does the printing, then the caller can't do anything except print. [...] > > > s = "???" > > > > This may be a problem. "???" is not a valid str, because it > > contains non-ASCII characters. > > It's just a test case (note (1)) for non-ascii input, precisely. Maybe so, but your test case depends on external factors like the terminal encoding. This is a bad test, because somebody else running it may get something completely different. > > As far as I know, the behaviour of stuffing unicode characters into > > byte-strings is not well-defined in Python, and will depend on > > external factors like the terminal you are running in, if any. It > > may or may not work as you expect. It is better to do this: > > > > u = u"???" > > s = u.encode('uft-8') > > Yo, but I cannot expect every user to always use only unicode > everywhere as input to my lib (both in sources to be parsed and in > pattern defs) like a robot. Of course you can. What happens if they pass None instead of a string? They get an error. What if they pass the integer 45? They get an error. What if they pass the list [1.235, 59.02, -267.1]? They get an error. You are not responsible for the caller passing bad data. If your class relies on the user passing unicode strings, then you document the fact that it requires unicode strings. Then you have a choice: * you can prohibit byte strings, and raise an error if they pass byte strings; or * you can make a reasonable effort to convert byte strings to unicode, by calling encode, but if the encode() fails, oh well, that's the caller's responsibility. If the user wants a string "cat" and they pass "C aT \n" instead, you're not responsible for fixing their mistake. If they want the unicode string u"???" and they pass the byte-string "\xe9\xe2\xc4" instead, that's not your problem either. > One main reason for my Unicode type (that > accepts both str and unicode). If all you want is a subclass of unicode which defaults to UTF-8 instead of ASCII for encoding, then I will agree with you 100%. That's a nice idea. But you seem to be taking a nice, neat, unicode subclass and trying to turn it into a swiss-army knife, containing all sorts of extra functionality to do everything for the user. That is a bad idea. > Anyway, all that source of troubles > disappears with py3 :-) > Then, I only need __str__ to produce nice, clear, unpolluted output. > > > which will always work consistently so long as you declare a source > > encoding at the top of your module: > > > > # -*- coding: UTF-8 -*- > > Yes, this applies to my own code. But what about user code calling my > lib? (This is the reason for Unicode.ENCODING config param). That is their responsibility, not yours. -- Steven D'Aprano From alan.gauld at btinternet.com Sat Mar 13 15:50:33 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Mar 2010 14:50:33 -0000 Subject: [Tutor] Opening a dos exe References: <4B9B8223.6050505@cmc.net> Message-ID: "Ray Parrish" wrote >>> but I cannot find a way of getting this information to the dos program >>> from python. Any ideas? > You could use os.system("startprogram.bat"), and > create startprogram.bat > to run the dos program, and feed it the files, > either all at once, or one at a time via the command line if it accepts > command line input. I don't think you can do the second option with a bat file. There is no way to interactively respond to the program once it starts. Thats why WSH is better for that kind of interactive input. Or just use subprocess.Popen... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Mar 13 15:54:12 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Mar 2010 14:54:12 -0000 Subject: [Tutor] First program References: <4B9AF2BA.5080408@cmc.net> Message-ID: "Luke Paireepinart" wrote >>> print "The area of a " + Choice + "is " str(Width) + " x " + >>> str(Height) + " equals " + str(Width * Height) + " square >>> feet" >> One thing - you don't need all the str() calls in your example, print >> already calls str() for you. Also comma separators are better than + >> signs >> > print actually doesn't call str if you use concatenation. So the str() > calls are necessary if you do not use "," but use "+" instead. Good catch! - you are, of course, right. When you use + print only sees a big single string with all the str() conversions already done. I was thinking of the more usual comma separated arguments to print Alan G. From crp at cmc.net Sat Mar 13 18:33:57 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 09:33:57 -0800 Subject: [Tutor] Escaping a single quote mark in a triple quoted string. Message-ID: <4B9BCC85.8080601@cmc.net> Hello, I am getting the following - >>> String = """Ray's Links""" >>> String 'Ray\'s Links' Note the magically appearing back slash in my result string. Now I tiry to escape the single quote. >>> String = """Ray\'s Links""" >>> String 'Ray\'s Links' Once again the unwanted back slash appears. >>> NewString = """'""" >>> NewString "'" Hmmm, no back slash this time... >>> String = """Ray""" + """'""" + """s Links""" >>> String 'Ray\'s Links' Why did quoting the single quote work in NewString when I triple quoted just the single quote, but not in the other examples where the result shows a back slash before my singe quote in String? Is there a proper way to do this? Thanks, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 18:41:14 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 09:41:14 -0800 Subject: [Tutor] Opening a dos exe In-Reply-To: References: <4B9B8223.6050505@cmc.net> Message-ID: <4B9BCE3A.1050707@cmc.net> Alan Gauld wrote: > > "Ray Parrish" wrote >>>> but I cannot find a way of getting this information to the dos program >>>> from python. Any ideas? >> You could use os.system("startprogram.bat"), and create startprogram.bat >> to run the dos program, and feed it the files, either all at once, or >> one at a time via the command line if it accepts command line input. > > I don't think you can do the second option with a bat file. There is > no way to interactively respond to the program once it starts. Thats > why WSH is better for that kind of interactive input. > > Or just use subprocess.Popen... > OK, it was not clear to me that he needed to be interactive with the dos program. He just said he needed to feed those files to the dos program, and I assumed he meant on the command line within a dos box, which can indeed be solved by running a batch file, if the dos program accepts command line parameters.. A good tool for writing interactive scripts used to be WinBatch, but I haven't used, or seen it anywhere for years. It would have to be installed on the machine you wanted to get interactive with a dos program however. I remember WinBatch from back in the Windows 3.1 days, and am not sure if they are still keeping it up to date. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 18:45:33 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 09:45:33 -0800 Subject: [Tutor] First program In-Reply-To: <201003131415.37843.steve@pearwood.info> References: <4B9AF2BA.5080408@cmc.net> <201003131415.37843.steve@pearwood.info> Message-ID: <4B9BCF3D.4030907@cmc.net> Steven D'Aprano wrote: > On Sat, 13 Mar 2010 01:04:42 pm Ray Parrish wrote: > >>> print "A %s with dimensions %sx%s has an area of %s." % >>> (choice, height, width, width*height) >>> >> Hello, >> >> Isn't it a little more understandable to use a >> construct like the following? >> >> >>>>> print "The area of a " + Choice + "is " str(Width) + " x " + >>>>> >> str(Height) + " equals " + str(Width * Height) + " >> square feet" >> >> The area of a rectangle is 12 x 10 equals 120 >> square feet. >> >> I find that putting the variables on the end like >> that, when you're not actually applying any special formatting to them >> makes it less readable >> when I'm debugging my stuff, or when someone else >> is reading my code, >> and trying to understand it. >> > > > Of course you are welcome to use whatever coding standards you like, but > I think you will find that among experienced coders, you are in a > vanishingly small minority. As a beginner, I found string interpolation > confusing at first, but it soon became second-nature. And of course, > there are legions of C coders who are used to it. > > I find an expression like: > > "The area of a " + Choice + "is " str(Width) + " x " + str(Height) > + "equals " + str(Width * Height) + "square feet" > > difficult to follow: too many quotes, too many sub-expressions being > added, too many repeated calls to str(), it isn't clear what is the > template and what is being inserted into the template. It is too easy > to miss a quote and get a SyntaxError, or to forget to add spaces where > needed. To me, this is MUCH easier: > > template = "The area of a %s is %s x %s equals %s square feet" > print template % (Width, Height Width*Height) > > One pair of quotes instead of five, no problems with remembering to add > spaces around pieces, and no need to explicitly call str(). > OK, that does seem a bit easier now to me. I'm going to have to read up on the %s, and any other formatting % codes there are however, since I'm dead green in Python yet. 8-) So, would I read about those in the string module portion of the documentation? Thanks, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 18:52:04 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 09:52:04 -0800 Subject: [Tutor] First program In-Reply-To: References: <4B9AF2BA.5080408@cmc.net> Message-ID: <4B9BD0C4.1020802@cmc.net> Alan Gauld wrote: > "Ray Parrish" wrote >>> print "A %s with dimensions %sx%s has an area of %s." % (choice, >>> height, width, width*height) >>> >> Isn't it a little more understandable to use a construct like the >> following? >> >>>>> print "The area of a " + Choice + "is " str(Width) + " x " + >> str(Height) + " equals " + str(Width * Height) + " square feet" > > It depends on where you come from. > Those of us brought up on C or COBOL are used to separating the > presentation from the data. Those brought up with PASCAL and BASIC are > used to iterleaving data with presentation. > > One thing - you don't need all the str() calls in your example, print > already calls str() for you. Also comma separators are better than + > signs since the plus operation on strings is quite expensive - you > create a new string for each addition. > > HTH, Thanks for the tips. If I understand you correctly I can do the call this way? - print "The area of ", Choice, " is ", Width, " x ", Height, " equals ", (Width * Height), " square feet" That eliminates the redundancy, and is quite readable to me. I like it. 8-) Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 18:55:19 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 09:55:19 -0800 Subject: [Tutor] First program In-Reply-To: References: <4B9AF2BA.5080408@cmc.net> Message-ID: <4B9BD187.4000704@cmc.net> Luke Paireepinart wrote: > > > On Sat, Mar 13, 2010 at 3:03 AM, Alan Gauld > wrote: > > "Ray Parrish" > wrote > > print "A %s with dimensions %sx%s has an area of %s." % > (choice, height, width, width*height) > > Isn't it a little more understandable to use a construct like > the following? > > print "The area of a " + Choice + "is " str(Width) > + " x " + > > str(Height) + " equals " + str(Width * Height) + " square feet" > > > It depends on where you come from. > Those of us brought up on C or COBOL are used to separating the > presentation from the data. Those brought up with PASCAL and BASIC > are used to iterleaving data with presentation. > > One thing - you don't need all the str() calls in your example, > print already calls str() for you. Also comma separators are > better than + signs since the plus operation on strings is quite > expensive - you create a new string for each addition. > > > print actually doesn't call str if you use concatenation. So the > str() calls are necessary if you do not use "," but use "+" instead. > So there are at least 2 reasons why + is worse than comma. > Another thing to be aware of is that if you use commas, > print inserts a space in the string, which may be either an advantage > or a disadvantage depending on what you're trying to do. > > -Luke Ahhh, thank you for the clarifications. for the cases where spaces are needed it the commas seem like a pretty good way to do it, but when I need to add together stuff with no spaces I'll try the formatted method. Thanks again, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From sander.sweers at gmail.com Sat Mar 13 18:59:01 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 13 Mar 2010 18:59:01 +0100 Subject: [Tutor] Escaping a single quote mark in a triple quoted string. In-Reply-To: <4B9BCC85.8080601@cmc.net> References: <4B9BCC85.8080601@cmc.net> Message-ID: On 13 March 2010 18:33, Ray Parrish wrote: > Hello, > > I am getting the following - > >>>> String = """Ray's Links""" >>>> String > 'Ray\'s Links' > > Note the magically appearing back slash in my result string. It is not really there. When you have a single quote in a single quote string it need to be escaped with a backslash. Simulary a double quote in a double quote string. The triple quote string does this for you. When you write the string to a file the backslash will "magically" disappear. > Now I tiry to escape the single quote. > >>>> String = """Ray\'s Links""" >>>> String > 'Ray\'s Links' > > Once again the unwanted back slash appears. See above single quotes in single quote string need to be escaped. >>>> NewString = """'""" >>>> NewString > "'" > Hmmm, no back slash this time... Correct, here you have a single quote in a double quote string. >>>> String = """Ray""" + """'""" + """s >>>> Links""" >>>> String > 'Ray\'s Links' > > Why did quoting the single quote work in NewString when I triple quoted just > the single quote, but not in the other examples where the result shows a > back slash before my singe quote in String? > > Is there a proper way to do this? You are trying to solve something that is not really a problem. What is however is your usage of a single quote in html text. You need to replace it with ' or ‘. Greets Sander From crp at cmc.net Sat Mar 13 19:04:07 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 10:04:07 -0800 Subject: [Tutor] First program In-Reply-To: References: <4B9AF1E8.3060203@cmc.net> Message-ID: <4B9BD397.3090300@cmc.net> Luke Paireepinart wrote: > Ray, please reply on-list in the future in case someone else has input. > > On Fri, Mar 12, 2010 at 8:01 PM, Ray Parrish > wrote: > > Luke Paireepinart wrote: > > print "A %s with dimensions %sx%s has an area of %s." % > (choice, height, width, width*height) > > > Isn't it a little more understandable to use a construct like the > following? > > >>> print "The area of a " + Choice + "is " str(Width) + " x " + > str(Height) + " equals " + str(Width * Height) + " square feet" > > The area of a rectangle is 12 x 10 equals 120 square feet. > > I find that putting the variables on the end like that, when > you're not actually applying any special formatting to them makes > it less readable when I'm debugging my stuff, or when someone else > is reading my code, and trying to understand it. > > > Your version creates at least 10 intermediate strings before outputting. > Remember strings are immutable in Python. > So you're constructing strings > The area of a > The area of a rectangle > The area of a rectangle is > 12 > The area of a rectangle is 12 > The area of a rectangle is 12 x > 10 > The area of a rectangle is 12 x 10 > The area of a rectangle is 12 x 10 equals > 120 > The area of a rectangle is 12 x 10 equals 120 > The area of a rectangle is 12 x 10 equals 120 square feet > > With string formatting you avoid all of these intermediate strings, so > it's arguably more efficient. > Other than just viewing from a performance standpoint though, I find > it much easier to read my version, because any computation required > takes place at the end of the line. > For example, your inline str(width*height) requires you to read the > whole line to see it. > > It's really a personal thing, it's easier for me to read the > formatting version than the string concatenation version, in most cases. > Now if you had used the comma convention I would have seen your > point. This is, I think, the easiest to read of all 3 > area = width * height > print "The area of a", choice, "is", width, "x", height, ", which > equals", area, "square feet." > > Also, why are you capitalizing variable names? That's a pretty > unusual convention. > > -Luke Thanks for letting me know how inefficient my method is. I'll remember that, and apply your suggestions to my code from now on. So, you're saying that the commas method also does not suffer from the overhead of creating a bunch of individual strings? As far as the capitalizations, it's just a habit I've held over from my Visual Basic days, and earlier programming. It's a little easier for me to pick out the individual words in a variable like ThisPerson as opposed to thisperson. I have been made aware that Python standard coding practice requires lower case variable names, and as soon as I can force myself to do it that way, or can remember to post that way I will be adopting the in place standards and conventions. Is it actually supposed to be this_person? I read part of the standards document, but can not remember right off the top of my head if underlines between words is required. Thanks, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From steve at pearwood.info Sat Mar 13 19:04:28 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Mar 2010 05:04:28 +1100 Subject: [Tutor] Escaping a single quote mark in a triple quoted string. In-Reply-To: <4B9BCC85.8080601@cmc.net> References: <4B9BCC85.8080601@cmc.net> Message-ID: <201003140504.30011.steve@pearwood.info> On Sun, 14 Mar 2010 04:33:57 am Ray Parrish wrote: > Hello, > > I am getting the following - > > >>> String = """Ray's Links""" > >>> String > 'Ray\'s Links' > > Note the magically appearing back slash in my result string. You are confusing the printed representation of the string with the contents of the string. The backslash is not part of the string, any more than the leading and trailing quotes are part of the string. They are part of the display of the string. Consider: >>> s = "ABC" # Three characters A B C. >>> s # Looks like five? 'ABC' >>> len(s) # No, really only three. 3 The quotes are not part of the string, but part of the printable representation. This is supposed to represent what you would type to get the string ABC. You have to type (quote A B C quote). Now consider: >>> s = """A"'"B""" # Five chars A double-quote single-quote d-quote B >>> s # Looks like eight? 'A"\'"B' >>> len(s) # But actually only five. 5 When printing the representation of the string, Python always wraps it in quotation marks. If the contents include quotation marks as well, Python will escape the inner quotation marks if needed, but remember this is only for the display representation. If you want to see what the string looks like without the external quotes and escapes: >>> print s A"'"B > >>> NewString = """'""" > >>> NewString > > "'" > Hmmm, no back slash this time... In this case, the string itself only contains a single-quote, no double-quote, so when showing the representation, Python wraps the contents with double-quotes and there is no need to escape the single-quote. Python's rules for showing the representation of the string includes: * wrap the string contents in single quotes ' * unless the string contains single quotes, in which case wrap it in double quotes " and display the single quotes unescaped * unless the string contains double quotes as well, in which case wrap it in single quotes and escape the inner single quotes. But remember: this is only the display of the string, not the contents. -- Steven D'Aprano From crp at cmc.net Sat Mar 13 19:07:49 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 10:07:49 -0800 Subject: [Tutor] First program In-Reply-To: <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> Message-ID: <4B9BD475.4090103@cmc.net> Andre Engels wrote: > On Sat, Mar 13, 2010 at 3:11 AM, Ray Parrish wrote: > >> Andre Engels wrote: >> >>> On 3/12/10, yd wrote: >>> >>>> else: >>>> raise Exception('{0}, is not a valid choice'.format(choice)) >>>> >>>> >>> This will cause the program to stop-with-error if something wrong is >>> entered. I think that's quite rude. I would change this to: >>> else: >>> print('{0}, is not a valid choice'.format(choice)) >>> >>> >> Here's what I get from that, could you please explain why? >> > > You're probably using Python 2.4 or 2.5; the .format method has been > introduced in Python 2.6, and is considered the 'standard' way of > working in Python 3. For older Python versions, this should read > > print('%s, is not a valid choice'%(choice)) > Yes, I'm using 2.45.2 as that is the highest version available in the Ubuntu repositories, and I'd like to keep it simple for users of programs i write. If I install a higher version from somewhere other than the repositories it will force users of my programs to do the same, and the repositories are the trusted source of software for Ubuntu, and other Linux users. Thanks for being so observant. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 19:13:37 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 10:13:37 -0800 Subject: [Tutor] Escaping a single quote mark in a triple quoted string. In-Reply-To: References: <4B9BCC85.8080601@cmc.net> Message-ID: <4B9BD5D1.1080406@cmc.net> Sander Sweers wrote: > On 13 March 2010 18:33, Ray Parrish wrote: > >> Hello, >> >> I am getting the following - >> >> >>>>> String = """Ray's Links""" >>>>> String >>>>> >> 'Ray\'s Links' >> >> Note the magically appearing back slash in my result string. >> > > It is not really there. When you have a single quote in a single quote > string it need to be escaped with a backslash. Simulary a double quote > in a double quote string. The triple quote string does this for you. > > When you write the string to a file the backslash will "magically" disappear. > > >> Now I tiry to escape the single quote. >> >> >>>>> String = """Ray\'s Links""" >>>>> String >>>>> >> 'Ray\'s Links' >> >> Once again the unwanted back slash appears. >> > > See above single quotes in single quote string need to be escaped. > > >>>>> NewString = """'""" >>>>> NewString >>>>> >> "'" >> Hmmm, no back slash this time... >> > > Correct, here you have a single quote in a double quote string. > > >>>>> String = """Ray""" + """'""" + """s >>>>> Links""" >>>>> String >>>>> >> 'Ray\'s Links' >> >> Why did quoting the single quote work in NewString when I triple quoted just >> the single quote, but not in the other examples where the result shows a >> back slash before my singe quote in String? >> >> Is there a proper way to do this? >> > > You are trying to solve something that is not really a problem. What > is however is your usage of a single quote in html text. You need to > replace it with ' or ‘. > > Greets > Sander > Thanks for your quick answer. I'll accept that there will not be a back slash in my file when I write it then. I was not aware that single quotes were bad news in HTML. I don't think I've ever experienced a problem using them myself, but will adjust, and start using the & code for it instead. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From marcodrompre at gmail.com Sat Mar 13 19:56:34 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Sat, 13 Mar 2010 13:56:34 -0500 Subject: [Tutor] Problem with little program Message-ID: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Hello I have a little problem, I am trying to define a function ligneCar(n, ca) that would print n times the caracters ca. For now I have the user entering a short sentence corresponding to ca. Here is my code: def ligneCar(n,ca): c=0 while c From alan.gauld at btinternet.com Sat Mar 13 20:27:09 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Mar 2010 19:27:09 -0000 Subject: [Tutor] First program References: <4B9AF1E8.3060203@cmc.net> <4B9BD397.3090300@cmc.net> Message-ID: "Ray Parrish" wrote > As far as the capitalizations, it's just a habit I've held over from my > Visual Basic days, and earlier programming. It's a little easier for me > to pick out the individual words in a variable like ThisPerson as > opposed to thisperson. thisPerson is fine for a variable ThisPerson implies to most Python progranmmers that it's a class. > Is it actually supposed to be this_person? I think the official style guide says use underscores, I personally prefer the this{erson stule except where it is ambiguous. Underscores have the adbvantage of never being ambiguous - but they do involve extra typing... Alan G. From alan.gauld at btinternet.com Sat Mar 13 20:34:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Mar 2010 19:34:13 -0000 Subject: [Tutor] Problem with little program References: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Message-ID: "Marco Rompr?" wrote > def ligneCar(n,ca): > c=0 > while c print ca > c+=1 > > ca = input ('Enter a short phrase : ') > n = input ('Enter how many times you want : ') > Thats the definition of my function ligne_Car The bottom two lines are not part of the function, they will be executed when you import the file - is that really what you wantr? > then in another python file > > I want to recall my function ligne_Car but it is not working. So show us the code that is not working! How are you importing the function? How are you accessing it? What error message do you get, if any? What happens? > Please help me Please help us. We can't possibly guess what you might be doing wrong with no clues. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From beachkid at insightbb.com Sat Mar 13 20:37:17 2010 From: beachkid at insightbb.com (Ken G.) Date: Sat, 13 Mar 2010 14:37:17 -0500 Subject: [Tutor] First program In-Reply-To: <4B9BD475.4090103@cmc.net> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> <4B9BD475.4090103@cmc.net> Message-ID: <4B9BE96D.1080806@insightbb.com> I am using Ubuntu 9.04 and I have versions 2.6.2 and 3.0.1+ installed. Look for IDLE in Add/Remove Applications. Perhaps, you may have a different version of Ubuntu. Ken Ray Parrish wrote: >> > Yes, I'm using 2.45.2 as that is the highest version available in the > Ubuntu repositories, and I'd like to keep it simple for users of > programs i write. If I install a higher version from somewhere other > than the repositories it will force users of my programs to do the > same, and the repositories are the trusted source of software for > Ubuntu, and other Linux users. > > Thanks for being so observant. > > Later, Ray Parrish From rabidpoobear at gmail.com Sat Mar 13 21:18:22 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 13 Mar 2010 14:18:22 -0600 Subject: [Tutor] First program In-Reply-To: <4B9BD397.3090300@cmc.net> References: <4B9AF1E8.3060203@cmc.net> <4B9BD397.3090300@cmc.net> Message-ID: On Sat, Mar 13, 2010 at 12:04 PM, Ray Parrish wrote: > Luke Paireepinart wrote: > >> >> Your version creates at least 10 intermediate strings before outputting. >> Remember strings are immutable in Python. So you're constructing strings >> The area of a >> The area of a rectangle >> The area of a rectangle is >> 12 >> The area of a rectangle is 12 >> The area of a rectangle is 12 x >> 10 >> The area of a rectangle is 12 x 10 >> The area of a rectangle is 12 x 10 equals >> 120 >> The area of a rectangle is 12 x 10 equals 120 >> The area of a rectangle is 12 x 10 equals 120 square feet >> >> With string formatting you avoid all of these intermediate strings, so >> it's arguably more efficient. >> Other than just viewing from a performance standpoint though, I find it >> much easier to read my version, because any computation required takes place >> at the end of the line. >> For example, your inline str(width*height) requires you to read the whole >> line to see it. >> >> It's really a personal thing, it's easier for me to read the formatting >> version than the string concatenation version, in most cases. >> Now if you had used the comma convention I would have seen your point. >> This is, I think, the easiest to read of all 3 >> area = width * height >> print "The area of a", choice, "is", width, "x", height, ", which equals", >> area, "square feet." >> >> Also, why are you capitalizing variable names? That's a pretty unusual >> convention. >> > > Thanks for letting me know how inefficient my method is. I'll remember > that, and apply your suggestions to my code from now on. So, you're saying > that the commas method also does not suffer from the overhead of creating a > bunch of individual strings? > Yes, the 'comma method' is actually doing something sorta tricky behind the scenes: it's creating a tuple and passing it to print. Look what happens when you just comma-separate stuff normally: >>> 'hello','how','are','you?' ('hello', 'how', 'are', 'you?') This implicit tuple conversion is useful in other situations too: >>> a, b = 1 , 2 >>> a 1 >>> b 2 this is creating the tuple (1,2) and then iterating over the tuple and assigning values to whatever's on the left hand side (a, b in this case). And you can abuse it if you really want to, to force things into tuples: >>> a, (1,) So what's happening when you call print with the tuple, is that print is basically doing this behind the scenes: for item in tuple: sys.stdout.write(item) sys.stdout.write(" ") sys.stdout.write("\n") Of course it doesn't do this in Python but rather in C, at a lower level, but that is basically the idea. If print gets a list or a tuple of strings it will iterate over them and output them to standard out with spaces. This doesn't incur the string concatenation overhead. Truthfully, the penalty for concatenating strings is not that big, the reason most people don't use the + method is just because they find it harder to read/follow. Or, I should say: the penalty for concatenating strings _that you're going to output_ is not that big, because you tend to output short strings and not a lot of them, otherwise the output is pretty much unreadable. If you do something like this: x = "" for i in range(1000000): x += str(i) + " " You'd probably want to change it to y = [] for i in range(1000000): y.append(i) x = " ".join(map(y, str)) -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From crp at cmc.net Sat Mar 13 23:46:44 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 14:46:44 -0800 Subject: [Tutor] First program In-Reply-To: <4B9BE96D.1080806@insightbb.com> References: <6faf39c91003120250m4c701c83yd7657ce6cd45724c@mail.gmail.com> <4B9AF44D.7040208@cmc.net> <6faf39c91003121830n8fc557dt2f6f22294a54301@mail.gmail.com> <4B9BD475.4090103@cmc.net> <4B9BE96D.1080806@insightbb.com> Message-ID: <4B9C15D4.6020604@cmc.net> Ken G. wrote: > I am using Ubuntu 9.04 and I have versions 2.6.2 and 3.0.1+ installed. > > Look for IDLE in Add/Remove Applications. > Perhaps, you may have a different version of Ubuntu. > > Ken > > Ray Parrish wrote: >>> >> Yes, I'm using 2.45.2 as that is the highest version available in the >> Ubuntu repositories, and I'd like to keep it simple for users of >> programs i write. If I install a higher version from somewhere other >> than the repositories it will force users of my programs to do the >> same, and the repositories are the trusted source of software for >> Ubuntu, and other Linux users. >> >> Thanks for being so observant. >> >> Later, Ray Parrish That should have been version 2.5.2 without the 4 I typoed. I'm using Hardy Heron, 8.04, and plan to stick with it until version 10 comes out as it is the next LTS version. I'm eyeballing Idle in Synaptic, and it appears to be an IDE for version 2.5 of Python for my version of Ubuntu. Idle is available for version 2.4 there as well. I'm pretty used to writing all of my code with only the aid of syntax highlighting, but have been hankering for an editor that will allow tabbed, or collapsible functions, and collapsible block elements. Does Idle do that? I guess I'll just go ahead and install it, and give it a try. Some IDEs I don't like with their project centric viewpoint, and my inability to quickly absorb their project file structures details to the point I can use them. If that doesn't happen pretty much within ten or fifteen minutes I go back to gedit. 8-) Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sat Mar 13 23:54:06 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 14:54:06 -0800 Subject: [Tutor] Escaping a single quote mark in a triple quoted string. In-Reply-To: <201003140504.30011.steve@pearwood.info> References: <4B9BCC85.8080601@cmc.net> <201003140504.30011.steve@pearwood.info> Message-ID: <4B9C178E.5040205@cmc.net> Steven D'Aprano wrote: > On Sun, 14 Mar 2010 04:33:57 am Ray Parrish wrote: > >> Hello, >> >> I am getting the following - >> >> >>> String = """Ray's Links""" >> >>> String >> 'Ray\'s Links' >> >> Note the magically appearing back slash in my result string. >> > > You are confusing the printed representation of the string with the > contents of the string. The backslash is not part of the string, any > more than the leading and trailing quotes are part of the string. They > are part of the display of the string. > > Consider: > > >>>> s = "ABC" # Three characters A B C. >>>> s # Looks like five? >>>> > 'ABC' > >>>> len(s) # No, really only three. >>>> > 3 > > The quotes are not part of the string, but part of the printable > representation. This is supposed to represent what you would type to > get the string ABC. You have to type (quote A B C quote). > > Now consider: > > >>>> s = """A"'"B""" # Five chars A double-quote single-quote d-quote B >>>> s # Looks like eight? >>>> > 'A"\'"B' > >>>> len(s) # But actually only five. >>>> > 5 > > When printing the representation of the string, Python always wraps it > in quotation marks. If the contents include quotation marks as well, > Python will escape the inner quotation marks if needed, but remember > this is only for the display representation. If you want to see what > the string looks like without the external quotes and escapes: > > >>>> print s >>>> > A"'"B > > > >> >>> NewString = """'""" >> >>> NewString >> >> "'" >> Hmmm, no back slash this time... >> > > In this case, the string itself only contains a single-quote, no > double-quote, so when showing the representation, Python wraps the > contents with double-quotes and there is no need to escape the > single-quote. > > Python's rules for showing the representation of the string includes: > > * wrap the string contents in single quotes ' > * unless the string contains single quotes, in which case wrap it > in double quotes " and display the single quotes unescaped > * unless the string contains double quotes as well, in which case > wrap it in single quotes and escape the inner single quotes. > > But remember: this is only the display of the string, not the contents. > Thank you, that was a very concise description, and has aided my comprehension greatly. Now if I can just keep it separate from the syntax in JavaScript, I'll be doing good. I keep a good record of these forum posts, so I can re-read this if necessary. Later, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From crp at cmc.net Sun Mar 14 00:06:49 2010 From: crp at cmc.net (Ray Parrish) Date: Sat, 13 Mar 2010 15:06:49 -0800 Subject: [Tutor] Declaring a compound dictionary structure. Message-ID: <4B9C1A89.8090301@cmc.net> Hello, I am stuck on the following - # Define the Dates{} dictionary structure as a dictionary # containing two dictionaries, each of which contains a list. Dates = {Today:{ThisIPAddress:[]}, Tomorrow:{ThisIPAddress:[]}} How do I pass this declaration empty values for Today, Tomorrow, and ThisIPAddress to initially clare it? I have managed to pass it [] as an empty list, but passing # "" as an empty dictionary label does not work correctly. Dates = {"":"":[]}, "":{"":[]}} The above is what I tried, and that creates a dictionary with index values of "" for all of the key values from the previous declaration. Is there a way to create this comound dictionary structure with no key values defined at all to begin with? The reason I'd like to know is that with the second declaration the "" indexed keys have to be removed after assigning at least one key value with a non-blank name. The idea behind the structure is to sort through a server log that contains entries for two dates, collecting a pair of dictionaries of ip address indexed server log lines which can then be iterated over to extract daily visit counts, and other extractable data sorted by date, and visitor. The need would not arise if the log files did not contain two dates each, but my service provider rotates their server logs around 2 am, so I always get a few entries for the next day in every log file. Thanks, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From cmcaine at googlemail.com Sat Mar 13 20:30:53 2010 From: cmcaine at googlemail.com (C M Caine) Date: Sat, 13 Mar 2010 19:30:53 +0000 Subject: [Tutor] Problem with little program In-Reply-To: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> References: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Message-ID: That's an easy mistake to make. Simply use raw_input instead of input. The former will always return a string, the latter treats whatever you enter as executable python code. You almost never want to use input. raw_input is safer. On 13 March 2010 18:56, Marco Rompr? wrote: > > Hello I have a little problem, I am trying to define a function ligneCar(n, ca) that would print n times the caracters ca. > For now I have the user entering a short sentence corresponding to ca. > Here is my code: > def ligneCar(n,ca): > ?? ?c=0 > ?? ?while c ?? ? ? ?print ca > ?? ? ? ?c+=1 > ca = input ('Enter a short phrase : ') > n = input ('Enter how many times you want ?: ') > Thats the definition of my function ligne_Car > then in another python file > I want to recall my function ligne_Car but it is not working. > Please help me > > > -- > Marc-O. Rompr? > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Sun Mar 14 02:13:29 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Mar 2010 12:13:29 +1100 Subject: [Tutor] Declaring a compound dictionary structure. In-Reply-To: <4B9C1A89.8090301@cmc.net> References: <4B9C1A89.8090301@cmc.net> Message-ID: <201003141213.30657.steve@pearwood.info> On Sun, 14 Mar 2010 10:06:49 am Ray Parrish wrote: > Hello, > > I am stuck on the following - > > # Define the Dates{} > dictionary structure as a dictionary > # containing two dictionaries, > each of which contains a list. > Dates = > {Today:{ThisIPAddress:[]}, > Tomorrow:{ThisIPAddress:[]}} > > How do I pass this declaration empty values for > Today, Tomorrow, and ThisIPAddress to initially > clare it? You don't. Once you create a key, you can't modify it. So if you create an empty value for Today etc., it stays empty. > The idea behind the structure is to sort through a > server log that contains entries for two dates, [...] Just work out the dates before hand, and populate the dictionary that way: today = "2010-03-13" tomorrow = "2010-03-14" thisIP = "123.456.789.123" entries = {today: {thisIP: []}, tomorrow: {thisIP: []}} Or don't pre-populate the dict at all. entries = {} for line in logfile: # Process the line to get a date, an IP address, and visitor date = ... address = ... visitor = ... entry = entries.get(date, {}) x = entry.get(address, {}) x.append(visitor) entry[address] = x entries[date] = entry The trick is to use get to look up the dictionary: entries.get(date, {}) looks up date in entries, and if it isn't found, it returns an empty dict {} instead of failing. Similarly for looking up the IP address. -- Steven D'Aprano From alan.gauld at btinternet.com Sun Mar 14 02:30:51 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Mar 2010 01:30:51 -0000 Subject: [Tutor] Problem with little program References: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Message-ID: "C M Caine" wrote > That's an easy mistake to make. Simply use raw_input instead of input. > The former will always return a string, the latter treats whatever you > enter as executable python code. Whilst I agree with the general principle to use raw_input, I don't see how we can say that it is the problem here. Indeed so far as I can tell we don't even know what the problem here is other than that the OP is trying to call the function he has defined in another file. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ On 13 March 2010 18:56, Marco Rompr? wrote: > > Hello I have a little problem, I am trying to define a function > ligneCar(n, ca) that would print n times the caracters ca. > For now I have the user entering a short sentence corresponding to ca. > Here is my code: > def ligneCar(n,ca): > c=0 > while c print ca > c+=1 > ca = input ('Enter a short phrase : ') > n = input ('Enter how many times you want : ') > Thats the definition of my function ligne_Car > then in another python file > I want to recall my function ligne_Car but it is not working. > Please help me From andreengels at gmail.com Sun Mar 14 12:01:33 2010 From: andreengels at gmail.com (Andre Engels) Date: Sun, 14 Mar 2010 12:01:33 +0100 Subject: [Tutor] Problem with little program In-Reply-To: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> References: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Message-ID: <6faf39c91003140401o5ec05577rdc7fb66fc2e968cb@mail.gmail.com> On Sat, Mar 13, 2010 at 7:56 PM, Marco Rompr? wrote: > Hello I have a little problem, I am trying to define a function ligneCar(n, > ca) that would print n times the caracters ca. > For now I have the user entering a short sentence corresponding to ca. > Here is my code: > def ligneCar(n,ca): > ?? ?c=0 > ?? ?while c ?? ? ? ?print ca > ?? ? ? ?c+=1 > ca = input ('Enter a short phrase : ') > n = input ('Enter how many times you want ?: ') > Thats the definition of my function ligne_Car > then in another python file > I want to recall my function ligne_Car but it is not working. Please give us more information - what code are you using to 'recall' your function, what were you expecting to be the result and what is actually the result? -- Andr? Engels, andreengels at gmail.com From dwightdhutto at yahoo.com Mon Mar 15 07:15:51 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Sun, 14 Mar 2010 23:15:51 -0700 (PDT) Subject: [Tutor] Command not issued until after escape from loop Message-ID: <837064.42964.qm@web45316.mail.sp1.yahoo.com> In the following code, the portion I'm having a problem with is outlined in stars, but since the overall code was short, I included it, just in case something else might be hindering the process. When I execute the script, it shows my main window, then if I press the CKEY it's *supposed* to call the cleanSlate() function, which grabs and deletes objects in the contents of another window. My problem is, it doesn't execute cleanSlate() until I hit the ESCKEY, or the QKEY. In the original example code I'm using as a template(I put in the ev() portion of the main template below my current code, so maybe someone could see where I deviated from the original, and why the differences in implementing redraw) , it's setup in the same manner. Why would it not execute immediately(during the main loop) in mine, but in the original it does? import Blender from Blender.BGL import * from Blender import Draw, Scene, Object, Mesh R = G = 0 B = A = 1 title = "Basic Object Manipulation" instructions = "Select an object to manipulate" len1 = Draw.GetStringWidth(title) len2 = Draw.GetStringWidth(instructions) # Removes default objects in scene def cleanSlate(scn): for ob in scn.objects: if ob.getType == 'Mesh' or 'Camera': scn.objects.unlink(ob) #defining the window def show_win(): glClearColor(R,G,B,A) # define color used to clear buffers glClear(GL_COLOR_BUFFER_BIT) # use it to clear the color buffer glColor3f(0.35,0.18,0.92) # define default color glColor3f(.0,0.0,0.0) # change default color for next item glRecti(10, 450, 20+len2, 113) # sets a box border glColor3f(0.2,0.2,0.2) # change default color for next item glRasterPos2i(14,435) # move cursor Draw.Text(title) # draw this text there glRasterPos2i(14,410) # move cursor again Draw.Text(instructions) # draw another msg ****************************************************************** # defining the event handling list def ev(evt, val): scn = Scene.getCurrent() if evt == Draw.ESCKEY or evt == Draw.QKEY: Draw.Exit() elif not val: return elif evt == Draw.CKEY: cleanSlate(scn) else: return Draw.Redraw(1) ****************************************************************** # build the window Draw.Register(show_win, ev, None) # start the main loop #End code This is the original ev() ********************************************************** def ev(evt, val): # event callback for Draw.Register() global R,G,B,A # ... it handles input events if evt == Draw.ESCKEY or evt == Draw.QKEY: Draw.Exit() # this quits the script elif not val: return elif evt == Draw.LEFTMOUSE: R = 1 - R elif evt == Draw.MIDDLEMOUSE: G = 1 - G elif evt == Draw.RIGHTMOUSE: B = 1 - B elif evt == Draw.WHEELUPMOUSE: R += 0.1 if R > 1: R = 1 elif evt == Draw.WHEELDOWNMOUSE: R -= 0.1 if R < 0: R = 0 else: return # don't redraw if nothing changed Draw.Redraw(1) TIA, David From dwightdhutto at yahoo.com Mon Mar 15 07:58:12 2010 From: dwightdhutto at yahoo.com (David Hutto) Date: Sun, 14 Mar 2010 23:58:12 -0700 (PDT) Subject: [Tutor] Command not issued until after escape from loop Message-ID: <556371.84310.qm@web45308.mail.sp1.yahoo.com> --- On Mon, 3/15/10, David Hutto wrote: > From: David Hutto > Subject: Command not issued until after escape from loop > To: tutor at python.org > Date: Monday, March 15, 2010, 2:15 AM > In the following code, the portion > I'm having a problem with is outlined in stars, but since > the overall code was short, I included it, just in case > something else might be hindering the process. > > When I execute the script, it shows my main window, then if > I press the CKEY it's *supposed* to call the cleanSlate() > function, which grabs and deletes objects in the contents of > another window. > > My problem is, it doesn't execute cleanSlate() until I hit > the ESCKEY, or the QKEY. In the original example code I'm > using as a template(I put in the ev() portion of the main > template below my current code, so maybe someone could see > where I deviated from the original, and why the differences > in implementing redraw) , it's setup in the same manner. > > Why would it not execute immediately(during the main loop) > in mine, but in the original it does? > > import Blender > from Blender.BGL import * > from Blender import Draw, Scene, Object, Mesh > R = G = 0 > B = A = 1 > title = "Basic Object Manipulation" > instructions = "Select an object to manipulate" > len1 = Draw.GetStringWidth(title) > len2 = Draw.GetStringWidth(instructions) > > # Removes default objects in scene > > def cleanSlate(scn): > ??? ??? for ob in > scn.objects: > ??? ??? ??? > ??? if ob.getType == 'Mesh' or 'Camera': > ??? ??? ??? > ??? ??? ??? > scn.objects.unlink(ob) > > #defining the window > def show_win(): > ??? ??? > glClearColor(R,G,B,A)? ? ? ? ? > ? ? ? # define color used to clear buffers > ??? ??? > glClear(GL_COLOR_BUFFER_BIT)? ? ? > ???# use it to clear the color buffer > ??? ??? > glColor3f(0.35,0.18,0.92)? ? ? ? ? > ? # define default color > ??? ??? > glColor3f(.0,0.0,0.0)? ? ? ? ? > ? ? ? # change default color for next item > ??? ??? glRecti(10, 450, > 20+len2, 113)? ? ???# sets a box > border > ??? ??? > glColor3f(0.2,0.2,0.2)? ? ? ? ? > ? ???# change default color for next > item > ??? ??? > glRasterPos2i(14,435)? ? ? ? ? > ? ? ? # move cursor > ??? ??? > Draw.Text(title)? ? ? ? ? ? > ? ? ? ???# draw this text > there > ??? ??? > glRasterPos2i(14,410)? ? ? ? ? > ? ? ? # move cursor again > ??? ??? > Draw.Text(instructions)???# draw another msg > ******************************************************************??? > ??? > # defining the event handling list > def ev(evt, val): > ??? ??? scn = > Scene.getCurrent() > ??? ??? if evt == Draw.ESCKEY > or evt == Draw.QKEY: > ??? ??? ??? > ??? Draw.Exit() > ??? ??? elif not val: > ??? ??? ??? > ??? return > ??? ??? elif evt == > Draw.CKEY: > ??? ??? ??? > ??? cleanSlate(scn) > ??? ??? else: > ??? ??? ??? > ??? return > ??? ??? Draw.Redraw(1) > ****************************************************************** > > # build the window > Draw.Register(show_win, ev, None)? ? ? # > start the main loop > > > #End code > > > This is the original ev() > ********************************************************** > > def ev(evt, val):? ? ? ? ? ? > ? ? ? ? ? # event callback for > Draw.Register() > ???global R,G,B,A? ? ? ? > ? ? ? ? ? ? > ???# ... it handles input events > ???if evt == Draw.ESCKEY or evt == > Draw.QKEY: > ? ???Draw.Exit()? ? ? > ? ? ? ? ? ? ? ? > ? # this quits the script > ???elif not val: return > ???elif evt == Draw.LEFTMOUSE: R = 1 - R > ???elif evt == Draw.MIDDLEMOUSE: G = 1 - G > ???elif evt == Draw.RIGHTMOUSE: B = 1 - B > ???elif evt == Draw.WHEELUPMOUSE: > ? ???R += 0.1 > ? ???if R > 1: R = 1 > ???elif evt == Draw.WHEELDOWNMOUSE: > ? ???R -= 0.1 > ? ???if R < 0: R = 0 > ???else: > ? ???return? ? ? ? > ? ? ? ? ? ? ? ? > ? ???# don't redraw if nothing changed > ???Draw.Redraw(1) > > > TIA, > David > > > ? ? ? > The revised question to this, after messing around a little more, is why does the loop hold the cleanSlate() function until ESC, or Q, but not hold on to the mouse events, which it executes immediately? I know the ESC, and Q are not triggering the function call, because it doesn't execute on ESC, or Q if the action to call it isn't performed during the loop. David From alan.gauld at btinternet.com Mon Mar 15 09:53:07 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 15 Mar 2010 08:53:07 -0000 Subject: [Tutor] Command not issued until after escape from loop References: <837064.42964.qm@web45316.mail.sp1.yahoo.com> Message-ID: "David Hutto" wrote Caveat: I know zero about Blender... but... > When I execute the script, it shows my main window, then if I > press the CKEY it's *supposed* to call the cleanSlate() function, > > My problem is, it doesn't execute cleanSlate() until I hit the > ESCKEY, or the QKEY. > ****************************************************************** > # defining the event handling list > def ev(evt, val): > scn = Scene.getCurrent() > if evt == Draw.ESCKEY or evt == Draw.QKEY: > Draw.Exit() > elif not val: > return > elif evt == Draw.CKEY: > cleanSlate(scn) > else: > return Can you insert debvug statements to display values? If so I'd try printing the value of val. If it is "False" - ie empty - the event function will exit before reaching your code. Alternatively try moving your elif up above the test for not val. See if that helps. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Mon Mar 15 10:44:43 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 15 Mar 2010 05:44:43 -0400 Subject: [Tutor] Command not issued until after escape from loop In-Reply-To: References: <837064.42964.qm@web45316.mail.sp1.yahoo.com> Message-ID: <2d00d7fe1003150244g56db9642heeacce7802439506@mail.gmail.com> On Mon, Mar 15, 2010 at 4:53 AM, Alan Gauld wrote: > > "David Hutto" wrote > > Caveat: I know zero about Blender... but... > I *thought* that since the code was in Python it might be the way I was executing it. I think now it might be how the other windows are updated in blender when the command is executed. > > When I execute the script, it shows my main window, then if I >> press the CKEY it's *supposed* to call the cleanSlate() function, >> >> My problem is, it doesn't execute cleanSlate() until I hit the >> ESCKEY, or the QKEY. >> ****************************************************************** >> # defining the event handling list >> def ev(evt, val): >> scn = Scene.getCurrent() >> if evt == Draw.ESCKEY or evt == Draw.QKEY: >> Draw.Exit() >> elif not val: >> return >> elif evt == Draw.CKEY: >> cleanSlate(scn) >> else: >> return >> > > > Can you insert debvug statements to display values? > If so I'd try printing the value of val. If it is "False" - ie empty - the > event function will exit before reaching your code. > > Alternatively try moving your elif up above the test for not val. > > See if that helps. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From karper12345 at yahoo.com Mon Mar 15 13:38:43 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Mon, 15 Mar 2010 05:38:43 -0700 (PDT) Subject: [Tutor] Proper way to use **kwargs? Message-ID: <725490.8709.qm@web44702.mail.sp1.yahoo.com> I want to use **kwargs to check a list of conditions (if true do this, if false do nothing) besides required parameters ( in sample a and b).? Sometimes I want to add a Python object (in example a dictionary and a list). Below is my first **kwargs-brew. ###START def function_with_kwargs(a, b, **kwargs): ??? options = { ??????? 'logfile'?? : None, ??????? 'door'????? : 'kitchendoor', ??????? 'roof'????? : 'tiles', ??????? 'mydict'??? : None, ??????? 'mylist'??? : None, } ??? options.update(kwargs) ??? logfile = options.get('logfile') ??? if logfile == None: ??????? print "No logging" ??? else: ??????? print "Logging" ??? mydict = options.get('mydict') ??? if mydict == None: ??????? print "Do nothing with dictionary" ??? else: ??????? print "Do something with dictionary" ??? mylist = options.get('mylist') ??? if mylist == None: ??????? print "Do nothing with list" ??? else: ??????? print "Do something with list" ??? print "END OF FUNCTION\n" ??????? somedict = { 'a': '1', 'b': '2', } somelist = ['1', '2'] #DO SOMETHING function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', mydict=somedict, mylist=somelist) #DO NOTHING function_with_kwargs(1, 2, door='frontdoor') ### END I have 2 questions about this code: 1. Can I use this in Python 3 ?? I'm not sure if I can use **kwargs in Python 3 because it uses the "apply" builtin (if I understand it correctly) "At this writing, both apply and the special call syntax described in this section can be used freely in Python 2.5, but it seems likely that apply may go away in Python 3.0. If you wish to future-proof your code, use the equivalent special call syntax, not apply." 2. Are there better ways to achieve what I want to do? -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Mon Mar 15 15:52:53 2010 From: norman at khine.net (Norman Khine) Date: Mon, 15 Mar 2010 15:52:53 +0100 Subject: [Tutor] return first line Message-ID: <9c2c8ffb1003150752i241e2f8g3e0f23c7440c9c55@mail.gmail.com> hello, i have this string http://paste.lisp.org/+22E5 which is extracted from an html page file using regex. the issue i have is that i am unsure how to strip the first tag in this case

Association NAtionale des Producteurs de QUInoa (ANAPQUI)

as on the pages this is not consistent. for example sometimes it is:

,

etc... how would you pull the tag and also the data from this? thanks norman From rafik at ubuntu.com Mon Mar 15 17:23:34 2010 From: rafik at ubuntu.com (Rafik Ouerchefani) Date: Mon, 15 Mar 2010 17:23:34 +0100 Subject: [Tutor] return first line In-Reply-To: <9c2c8ffb1003150752i241e2f8g3e0f23c7440c9c55@mail.gmail.com> References: <9c2c8ffb1003150752i241e2f8g3e0f23c7440c9c55@mail.gmail.com> Message-ID: Hi, assuming that your content is in input.txt, you can do : >>> f = open("/path/to/content.txt") >>> f.readline() it will return the first line only cheers, - Rafik From norman at khine.net Mon Mar 15 18:19:27 2010 From: norman at khine.net (Norman Khine) Date: Mon, 15 Mar 2010 18:19:27 +0100 Subject: [Tutor] return first line In-Reply-To: References: <9c2c8ffb1003150752i241e2f8g3e0f23c7440c9c55@mail.gmail.com> Message-ID: <9c2c8ffb1003151019h6dfc1e38vba9c0d61d424e031@mail.gmail.com> hi, i tried this, my problem is that the file isfrom a python list - here is the code i have so far: http://paste.lisp.org/+22E5/1 as the first line for each item contains the 'title' which has different tags, i want to find a way to strip this out. any advise much appreciated On Mon, Mar 15, 2010 at 5:23 PM, Rafik Ouerchefani wrote: > Hi, > > assuming that your content is in input.txt, you can do : > >>>> f = open("/path/to/content.txt") >>>> f.readline() > > it will return the first line only > > cheers, > > - Rafik > From bkjones at gmail.com Mon Mar 15 18:26:27 2010 From: bkjones at gmail.com (Brian Jones) Date: Mon, 15 Mar 2010 13:26:27 -0400 Subject: [Tutor] module.class.method in logging.debug Message-ID: <6e5927ff1003151026n477f0a2bg3ab42e2678c89345@mail.gmail.com> Hi all, I have some code that is a plugin for a larger app, and I'd like to be able to properly log issues that arise in the plugin code. I may not be maintaining this code forever, and I'd like the logging to work even if it's refactored later and code moved around, method names change, etc. So I'd like my logging to include a string identifying (programatically) the "module.class.method" where things went afoul. I tried a couple of things I could think of, and successfully got the module and class without issue, but the one missing piece was the method name. I imagine a method can introspect and get its own name, but I haven't been able to find the incantation to make it work. I saw the documentation on the data model here: http://docs.python.org/reference/datamodel.html but I think the use of things like im_func or __func__ assume that you're referencing those as an attribute of a method object. I'm just not sure how that's done from inside the method itself. Clues hereby solicited. Humbly, brian -- Brian K. Jones Python Magazine http://www.pythonmagazine.com My Blog http://www.protocolostomy.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From bkjones at gmail.com Mon Mar 15 18:59:04 2010 From: bkjones at gmail.com (Brian Jones) Date: Mon, 15 Mar 2010 13:59:04 -0400 Subject: [Tutor] module.class.method in logging.debug In-Reply-To: <6e5927ff1003151026n477f0a2bg3ab42e2678c89345@mail.gmail.com> References: <6e5927ff1003151026n477f0a2bg3ab42e2678c89345@mail.gmail.com> Message-ID: <6e5927ff1003151059t5010139fq8673afd6ef900767@mail.gmail.com> On Mon, Mar 15, 2010 at 1:26 PM, Brian Jones wrote: > Hi all, > > I have some code that is a plugin for a larger app, and I'd like to be able > to properly log issues that arise in the plugin code. I may not be > maintaining this code forever, and I'd like the logging to work even if it's > refactored later and code moved around, method names change, etc. So I'd > like my logging to include a string identifying (programatically) the > "module.class.method" where things went afoul. > Well, after some more reading, I found that I can get the module *and* the method name if I add %(module)s and %(funcName) to my logging formatter. No class though, and it's all or nothing: I really only want that behavior for debug messages, not err/crit messages. Any other thoughts? -- Brian K. Jones Python Magazine http://www.pythonmagazine.com My Blog http://www.protocolostomy.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Mon Mar 15 14:40:33 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 15 Mar 2010 14:40:33 +0100 Subject: [Tutor] Proper way to use **kwargs? In-Reply-To: <725490.8709.qm@web44702.mail.sp1.yahoo.com> References: <725490.8709.qm@web44702.mail.sp1.yahoo.com> Message-ID: <29179d161003150640q3b2e3dd7x2cd2caaff1c56369@mail.gmail.com> On Mon, Mar 15, 2010 at 1:38 PM, Karjer Jdfjdf wrote: > > I have 2 questions about this code: > > 1. Can I use this in Python 3 ? I'm not sure if I can use **kwargs in > Python 3 because it uses the "apply" builtin (if I understand it correctly) > > "At this writing, both apply and the special call syntax described in this > section can be used freely in Python 2.5, but it seems likely that apply > may go away in Python 3.0. If you wish to future-proof your code, use > the equivalent special call syntax, not apply." > > what they're saying is that in python 3, the apply function doesn't exist anymore, so if you want equivalent functionality, you'll have to use special call syntax. In other words, what you're doing is just fine. > 2. Are there better ways to achieve what I want to do? > > I'd consider making sure there are no nonsense arguments passed into the function: for key in kwargs: if key not in options: raise ArgumentError("invalid argument: %s" % key) other than that, looks fine to me Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark-ireland at msn.com Mon Mar 15 16:23:30 2010 From: mark-ireland at msn.com (mark-ireland at msn.com) Date: Mon, 15 Mar 2010 15:23:30 -0000 Subject: [Tutor] Hi there :.) Message-ID: Hey, Just introducing myself to say hi........Hi! I'm very new to programming, I got interested in it when I decided to have a go at html. When looking around for a place to start I found out that python and C++ are the usual starting place but when I found out that civilisation and eve are programmed using python (my favorite type of games), that helped make the decision. so far I've been using Create Your Own Games with Python (using python 3.1 and pygame) as a guide and been experimenting making snippets of code. My first aim is to get comfortable with python and move on to C++ (apparently python is slow ?!?). My ultimate aim is to create a couple of ideas that I've had floating around for a while. I've attached the snippets of code I've done so far plus the one I'm currently hitting a bit of a wall with, any sudjestions will be greatly appreciated. test.py: The problem i'm having with this is with the platform. I want the ball to fall if it goes off the edge but be able to jump when on the platform. it either will fall off the edge but won't jump or it will jump but it won't fall. I don't want to change the way the controls are worked (with the true/ false). knight meets wall, wall won't let knight pass, knight says "neh"! -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My Collision Demo.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My Colour Change Demo.py URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Background.JPG Type: image/jpeg Size: 129307 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Ball.jpg Type: image/jpeg Size: 1536 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Floor.JPG Type: image/jpeg Size: 88326 bytes Desc: not available URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My Movement Demo.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My Platform Controls Demo.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My RPG Controls Demo.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: My Timer Demo.py URL: From microteacher at gmail.com Mon Mar 15 11:30:24 2010 From: microteacher at gmail.com (Prasad) Date: Mon, 15 Mar 2010 16:00:24 +0530 Subject: [Tutor] local var error Message-ID: <1268649024.2550.5.camel@prasad-laptop> I am writing a function to find correct transformer lamination for specific wattage. It gives an error message: UnboundLocalError: local variable 'stdArea' referenced before assignment def power2lam(): voltAmp=raw_input("How much power in Watts you want to handle?: ") #following dictionary is for finding the correct lamination size practically available. dictLam={0.2:32,2:17,5:12,6:1,7:74,10:2,18:45,31:15,45:33,75:13,156:5,494:43} temp=dictLam.keys() temp.sort() vamaxList=temp print vamaxList for x in vamaxList: if x Message-ID: "Prasad" wrote >I am writing a function to find correct transformer lamination for > specific wattage. It gives an error message: > > UnboundLocalError: local variable 'stdArea' referenced before assignment Please al;ways senmd the full error text not just the last line. However in your case we can do without.... > voltAmp=raw_input("How much power in Watts you want to handle?:) voltAmp is a string You probabluy want to convert it to an int or float voltAmp=int( raw_input("How much power in Watts you want to handle?: ) ) > dictLam={0.2:32,2:17,5:12,6:1,7:74,10:2,18:45,31:15,45:33,75:13,156:5,494:43} > temp=dictLam.keys() > temp.sort() > vamaxList=temp > print vamaxList > for x in vamaxList: You could replace all of that with for x in sorted(dictLam): > if x pass#print ".", > else: > stdArea=(math.sqrt(x))/6 This only gets exercised if the test is false, which, if you are comparing to a string might never be true. So it might not get excercised vand the variable might never get created. Better to initialise it to a default value (zero?) outside the loop if it is only used inside a conditional. Or catch the exception if no sensible default exists. > windArea=(math.sqrt(stdArea))*0.75 > I am creating a variable in the for loop. Shouldn't it work outside it? It will if you create it, I suspect you never do. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From waynejwerner at gmail.com Mon Mar 15 20:22:38 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 15 Mar 2010 14:22:38 -0500 Subject: [Tutor] Hi there :.) In-Reply-To: References: Message-ID: <333efb451003151222i54c9511bn30705ec2e40c805d@mail.gmail.com> On Mon, Mar 15, 2010 at 10:23 AM, wrote: > Hey, > Just introducing myself to say hi........Hi! > > I'm very new to programming, I got interested in it when I decided to have > a go at html. When looking around for a place to start I found out that > python and C++ are the usual starting place but when I found out that > civilisation and eve are programmed using python (my favorite type of > games), that helped make the decision. so far I've been using Create Your > Own Games with Python (using python 3.1 and pygame) as a guide and been > experimenting making snippets of code. > Welcome to Python and programming! HTML is what got me started on my path, too. Pygame is a great package for making games with. > My first aim is to get comfortable with python and move on to C++ > (apparently python is slow ?!?). My ultimate aim is to create a couple of > ideas that I've had floating around for a while. > Slow is a relative term. If C++ does something 100x faster than python, and python can already do it in .5 seconds you won't notice much difference if you're doing it once. If you're doing 2-d game programming Python is certainly fast for plenty of stuff. Take a look at the games of pyweek: http://www.pyweek.org/ Code is available for all the games so you can take a look at how they program and that might help you with challenges you face. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at gmail.com Mon Mar 15 20:25:20 2010 From: denis.spir at gmail.com (spir) Date: Mon, 15 Mar 2010 20:25:20 +0100 Subject: [Tutor] Proper way to use **kwargs? In-Reply-To: <725490.8709.qm@web44702.mail.sp1.yahoo.com> References: <725490.8709.qm@web44702.mail.sp1.yahoo.com> Message-ID: <20100315202520.16763e36@o> On Mon, 15 Mar 2010 05:38:43 -0700 (PDT) Karjer Jdfjdf wrote: > I want to use **kwargs to check a list of conditions (if true do this, if false do nothing) besides required parameters ( in sample a and b).? Sometimes I want to add a Python object (in example a dictionary and a list). Below is my first **kwargs-brew. > > ###START > > def function_with_kwargs(a, b, **kwargs): > ??? options = { > ??????? 'logfile'?? : None, > ??????? 'door'????? : 'kitchendoor', > ??????? 'roof'????? : 'tiles', > ??????? 'mydict'??? : None, > ??????? 'mylist'??? : None, } > ??? options.update(kwargs) The default set of options is a constant independant of the function and of its (other) arguments. So, I would rather define it outside. I find it clearer. from copy import copy OPTIONS = { 'logfile' : None, 'door' : 'kitchendoor', 'roof' : 'tiles', 'mydict' : None, 'mylist' : None, } def function_with_kwargs(a, b, **kwargs): options = copy(OPTIONS) options.update(kwargs) print options (Must be copied to avoid the defaults to be altered by update.) > ??? logfile = options.get('logfile') > ??? if logfile == None: > ??????? print "No logging" > ??? else: > ??????? print "Logging" You don't need to use get, use options['logfile'] instead. Get is only useful if you use its default_value parameter. This would indeed be a good alternative to a global default dict for options: logfile = options.get('logfile', None) ... roof = options.get('roof', 'tiles') or maybe better the general pattern: x = options.get('x', OPTIONS['x']) > ??? mydict = options.get('mydict') > ??? if mydict == None: > ??????? print "Do nothing with dictionary" > ??? else: > ??????? print "Do something with dictionary" > > ??? mylist = options.get('mylist') > ??? if mylist == None: > ??????? print "Do nothing with list" > ??? else: > ??????? print "Do something with list" > > ??? print "END OF FUNCTION\n" > ??????? > > somedict = { 'a': '1', 'b': '2', } > somelist = ['1', '2'] > > #DO SOMETHING > function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', mydict=somedict, mylist=somelist) > > #DO NOTHING > function_with_kwargs(1, 2, door='frontdoor') > > > > ### END > > I have 2 questions about this code: > > 1. Can I use this in Python 3 ?? I'm not sure if I can use **kwargs in Python 3 because it uses the "apply" builtin (if I understand it correctly) > > "At this writing, both apply and the special call syntax described in this > section can be used freely in Python 2.5, but it seems likely that apply > may go away in Python 3.0. If you wish to future-proof your code, use > the equivalent special call syntax, not apply." > > 2. Are there better ways to achieve what I want to do? In this case, I would use a general config object, which attributes are individual parameters. You need to first define a fake Config class (because for any reason we cannot alter direct instances of object). Denis ________________________________ la vita e estrany spir.wikidot.com From alan.gauld at btinternet.com Mon Mar 15 20:28:26 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 15 Mar 2010 19:28:26 -0000 Subject: [Tutor] Hi there :.) References: Message-ID: wrote > Just introducing myself to say hi........Hi! Hi welcome, but please don't include lots of attachments. It blows up people's mailboxes and bandwidth allowances. Better to post them on a website and send a link. > I'm very new to programming, ... > place to start I found out that python and C++ are the usual > starting place Actually there are many: VisualBasic, Lisp/Scheme and Java are all common starting points too. And each has its own merits depending on what you want to do. > but when I found out that civilisation and eve are programmed > using python (my favorite type of games), that helped make the > decision. As good a reason as any! :-) > My first aim is to get comfortable with python and move on > to C++ (apparently python is slow ?!?). It is all relative. If you want to write fast moving graphics etc then yes, you probably need C++. For anything else you might find Python is fast enough. > The problem i'm having with this is with the platform. > I want the ball to fall if it goes off the edge but be able to > jump when on the platform. it either will fall off the edge > but won't jump or it will jump but it won't fall. > I don't want to change the way the controls are worked > (with the true/ false). test.py is the one file that didn't show up (at least for me) Lots of "demos" but no test.py. It is probably best if you can create the simplest possible example that shows the problem rather than expecting folks to read through half a dozen fairly big files and try to guess what the design looks like etc. (That may have beeen what test.py was!) Try to send some specific code that causes a problem, or at least that you think is causing the problem. Plus any error messages. It would help to tell us which OS and Python version you are using too. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Mon Mar 15 20:41:43 2010 From: denis.spir at gmail.com (spir) Date: Mon, 15 Mar 2010 20:41:43 +0100 Subject: [Tutor] module.class.method in logging.debug In-Reply-To: <6e5927ff1003151026n477f0a2bg3ab42e2678c89345@mail.gmail.com> References: <6e5927ff1003151026n477f0a2bg3ab42e2678c89345@mail.gmail.com> Message-ID: <20100315204143.3636a160@o> On Mon, 15 Mar 2010 13:26:27 -0400 Brian Jones wrote: > I tried a couple of things I could think of, and successfully got the module > and class without issue, but the one missing piece was the method name. All methods and funcs (and types) have a __name__ attr. from types import FunctionType as Function def f1(): pass def f2(): pass def f3(): pass for (name,obj) in vars().items(): if type(obj) is Function: print obj.__name__, ==> f1 f2 f3 Denis ________________________________ la vita e estrany spir.wikidot.com From ps_python at yahoo.com Mon Mar 15 23:52:26 2010 From: ps_python at yahoo.com (kumar s) Date: Mon, 15 Mar 2010 15:52:26 -0700 (PDT) Subject: [Tutor] raw_input() Message-ID: <423811.5373.qm@web112506.mail.gq1.yahoo.com> Dear group: I have a large file 3GB. Each line is a tab delim file. example lines of it: 585 chr1 433 433 rs56289060 0 + - - -/C genomic insertion unknown 0 0 unknown between 1 585 chr1 491 492 rs55998931 0 + C C C/T genomic single unknown 0 0 unknown exact 1 585 chr1 518 519 rs62636508 0 + G G C/G genomic single unknown 0 0 unknown exact 1 585 chr1 582 583 rs58108140 0 + G G A/G genomic single unknown 0 0 unknown exact 1 Now I dont want to load this entire file. I want to give each line as an input and print selective lines. For example: x1.py = second = raw_input() x = second.split('\t') y = x[1:] print '\t'.join(y) %cat mybigfile.rod | python x1.py chr1 433 433 rs56289060 0 + - - -/C genomic insertion unknown 0 0 unknown between 1 My question: this program is only printing first line. It is not processing every line that cat spits to x1.py. how do I print every line. thanks Kumar. From ps_python at yahoo.com Tue Mar 16 00:04:02 2010 From: ps_python at yahoo.com (kumar s) Date: Mon, 15 Mar 2010 16:04:02 -0700 (PDT) Subject: [Tutor] raw_input() In-Reply-To: <423811.5373.qm@web112506.mail.gq1.yahoo.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> Message-ID: <352530.89110.qm@web112516.mail.gq1.yahoo.com> Here it worked after trying a while loop: x1.py = while True: second = raw_input() x = second.split('\t') y = x[1:] print '\t'.join(y) %cat mybigfile.rod | python x1.py Traceback (most recent call last): File "x1.py", line 2, in second = raw_input() EOFError: EOF when reading a line How to notify that at EOF break and suppress exception. thanks ----- Original Message ---- From: kumar s To: tutor at python.org Sent: Mon, March 15, 2010 6:52:26 PM Subject: [Tutor] raw_input() Dear group: I have a large file 3GB. Each line is a tab delim file. example lines of it: 585 chr1 433 433 rs56289060 0 + - - -/C genomic insertion unknown 0 0 unknown between 1 585 chr1 491 492 rs55998931 0 + C C C/T genomic single unknown 0 0 unknown exact 1 585 chr1 518 519 rs62636508 0 + G G C/G genomic single unknown 0 0 unknown exact 1 585 chr1 582 583 rs58108140 0 + G G A/G genomic single unknown 0 0 unknown exact 1 Now I dont want to load this entire file. I want to give each line as an input and print selective lines. For example: x1.py = second = raw_input() x = second.split('\t') y = x[1:] print '\t'.join(y) %cat mybigfile.rod | python x1.py chr1 433 433 rs56289060 0 + - - -/C genomic insertion unknown 0 0 unknown between 1 My question: this program is only printing first line. It is not processing every line that cat spits to x1.py. how do I print every line. thanks Kumar. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From transmogribenno at gmail.com Tue Mar 16 00:19:24 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Tue, 16 Mar 2010 08:19:24 +0900 Subject: [Tutor] raw_input() In-Reply-To: <352530.89110.qm@web112516.mail.gq1.yahoo.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> <352530.89110.qm@web112516.mail.gq1.yahoo.com> Message-ID: <9b00d1a91003151619w10b9a192m9c9bb2be60e73ac3@mail.gmail.com> On 16 March 2010 08:04, kumar s wrote: > %cat mybigfile.rod | python x1.py > Traceback (most recent call last): > ?File "x1.py", line 2, in > ? ?second = raw_input() > EOFError: EOF when reading a line > > How to notify that at EOF break and suppress exception. try: second = raw_input() except EOFError: # handle error in some way I would probably supply the file name as an argument rather than piping into stdin (or allow both methods), but that's up to you. HTH, benno From ps_python at yahoo.com Tue Mar 16 00:22:55 2010 From: ps_python at yahoo.com (kumar s) Date: Mon, 15 Mar 2010 16:22:55 -0700 (PDT) Subject: [Tutor] raw_input() In-Reply-To: <9b00d1a91003151619w10b9a192m9c9bb2be60e73ac3@mail.gmail.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> <352530.89110.qm@web112516.mail.gq1.yahoo.com> <9b00d1a91003151619w10b9a192m9c9bb2be60e73ac3@mail.gmail.com> Message-ID: <299533.38989.qm@web112501.mail.gq1.yahoo.com> thanks Benno. supplying 3.6 GB file is over-kill for the script. This is the reason I chose to input lines on fly. thanks Kumar ----- Original Message ---- From: Benno Lang To: kumar s Cc: tutor at python.org Sent: Mon, March 15, 2010 7:19:24 PM Subject: Re: [Tutor] raw_input() On 16 March 2010 08:04, kumar s wrote: > %cat mybigfile.rod | python x1.py > Traceback (most recent call last): > File "x1.py", line 2, in > second = raw_input() > EOFError: EOF when reading a line > > How to notify that at EOF break and suppress exception. try: second = raw_input() except EOFError: # handle error in some way I would probably supply the file name as an argument rather than piping into stdin (or allow both methods), but that's up to you. HTH, benno From steve at pearwood.info Tue Mar 16 00:25:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 16 Mar 2010 10:25:34 +1100 Subject: [Tutor] raw_input() In-Reply-To: <423811.5373.qm@web112506.mail.gq1.yahoo.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> Message-ID: <201003161025.35014.steve@pearwood.info> On Tue, 16 Mar 2010 09:52:26 am kumar s wrote: > Dear group: > I have a large file 3GB. Each line is a tab delim file. [...] > Now I dont want to load this entire file. I want to give each line as > an input and print selective lines. datafile = open("somefile.data", "r") for line in datafile: print line will print each line. If you want to print selected lines, you have to explain what the condition is that decides whether to print it or not. I'm going to make something up: suppose you want the line to only print if the eighth column is "A": def condition(line): line = line.strip() fields = line.split('\t') return fields[7].strip().upper() == "A" datafile = open("somefile.data", "r") for line in datafile: if condition(line): print line will print only the lines where column 8 is the letter A. -- Steven D'Aprano From steve at pearwood.info Tue Mar 16 00:28:43 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 16 Mar 2010 10:28:43 +1100 Subject: [Tutor] raw_input() In-Reply-To: <299533.38989.qm@web112501.mail.gq1.yahoo.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> <9b00d1a91003151619w10b9a192m9c9bb2be60e73ac3@mail.gmail.com> <299533.38989.qm@web112501.mail.gq1.yahoo.com> Message-ID: <201003161028.43777.steve@pearwood.info> On Tue, 16 Mar 2010 10:22:55 am kumar s wrote: > thanks Benno. > > supplying 3.6 GB file is over-kill for the script. Then supply a smaller file. > This is the reason I chose to input lines on fly. I don't understand what you are trying to say. -- Steven D'Aprano From roadierich at googlemail.com Tue Mar 16 00:33:11 2010 From: roadierich at googlemail.com (Rich Lovely) Date: Mon, 15 Mar 2010 23:33:11 +0000 Subject: [Tutor] Proper way to use **kwargs? In-Reply-To: <725490.8709.qm@web44702.mail.sp1.yahoo.com> References: <725490.8709.qm@web44702.mail.sp1.yahoo.com> Message-ID: On 15 March 2010 12:38, Karjer Jdfjdf wrote: > I want to use **kwargs to check a list of conditions (if true do this, if > false do nothing) besides required parameters ( in sample a and b). > Sometimes I want to add a Python object (in example a dictionary and a > list). Below is my first **kwargs-brew. > > ###START > > def function_with_kwargs(a, b, **kwargs): > options = { > 'logfile' : None, > 'door' : 'kitchendoor', > 'roof' : 'tiles', > 'mydict' : None, > 'mylist' : None, } > options.update(kwargs) > > logfile = options.get('logfile') > if logfile == None: > print "No logging" > else: > print "Logging" > > mydict = options.get('mydict') > if mydict == None: > print "Do nothing with dictionary" > else: > print "Do something with dictionary" > > mylist = options.get('mylist') > if mylist == None: > print "Do nothing with list" > else: > print "Do something with list" > > print "END OF FUNCTION\n" > > > somedict = { 'a': '1', 'b': '2', } > somelist = ['1', '2'] > > #DO SOMETHING > function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', > mydict=somedict, mylist=somelist) > > #DO NOTHING > function_with_kwargs(1, 2, door='frontdoor') > > ### END > > I have 2 questions about this code: > > 1. Can I use this in Python 3 ? I'm not sure if I can use **kwargs in > Python 3 because it uses the "apply" builtin (if I understand it correctly) > > "At this writing, both apply and the special call syntax described in this > section can be used freely in Python 2.5, but it seems likely that apply > may go away in Python 3.0. If you wish to future-proof your code, use > the equivalent special call syntax, not apply." > > 2. Are there better ways to achieve what I want to do? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > This is not the intended use of kwargs. **kwargs is intended for situations where you don't know what the keyword arguments are going to be when you write the function. The dict() function is a good example. It is quite possible to write dict(foo=1, bar=2). In your example, however, you know exactly what the keywords are going to be, so all kwargs does is muddy the function signature, so when you're using the function, you need to look at more than just the first line to see what arguments it takes. A better way, in my opinion, would be to use optional arguements: def function_with_kwargs(a, b, logfile=None, door='kitchendoor', roof='tiles', mydict=None, mylist=None): # etc Its quite easy to see why this is an improvement if you load the function definition into idle, and then start typing the function name. A tooltip will pop up, listing all the arguments it can take. You can still use the function in exactly the same way as with **kwargs, it just won't hand you a dictionary holding the arguments passed in. It does, however, eliminate the need for lines like mydict = options.get('mydict') You can just jump straight to checking if mydict is None: #do stuff Of course, this approach gets impractical if you've got hundreds of named arguments, but that's probably a pretty good sign that you're doing something wrong. -- Rich "Roadie Rich" Lovely Just because you CAN do something, doesn't necessarily mean you SHOULD. In fact, more often than not, you probably SHOULDN'T. Especially if I suggested it. 10 re-discover BASIC 20 ??? 30 PRINT "Profit" 40 GOTO 10 -------------- next part -------------- An HTML attachment was scrubbed... URL: From transmogribenno at gmail.com Tue Mar 16 01:27:13 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Tue, 16 Mar 2010 09:27:13 +0900 Subject: [Tutor] raw_input() In-Reply-To: <201003161028.43777.steve@pearwood.info> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> <9b00d1a91003151619w10b9a192m9c9bb2be60e73ac3@mail.gmail.com> <299533.38989.qm@web112501.mail.gq1.yahoo.com> <201003161028.43777.steve@pearwood.info> Message-ID: <9b00d1a91003151727j729d557dg1c75d163a90f955d@mail.gmail.com> On 16 March 2010 08:28, Steven D'Aprano wrote: > On Tue, 16 Mar 2010 10:22:55 am kumar s wrote: >> thanks Benno. >> >> supplying 3.6 GB file is over-kill for the script. > > Then supply a smaller file. > > >> This is the reason I chose to input lines on fly. > > I don't understand what you are trying to say. I think he thinks that python is going to read the whole 3.6GB of data into memory in one hit, rather than using a small amount of memory to process it line by line. But "for line in datafile" in your code above uses a generator, right? So I don't think it's a problem - correct me if I'm wrong. Thanks, benno. From alan.gauld at btinternet.com Tue Mar 16 02:11:23 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Mar 2010 01:11:23 -0000 Subject: [Tutor] raw_input() References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> Message-ID: "kumar s" wrote > For example: > > x1.py = > > second = raw_input() > x = second.split('\t') > y = x[1:] > print '\t'.join(y) > > > %cat mybigfile.rod | python x1.py > chr1 433 433 rs56289060 0 > + - - -/C genomic insertion unknown 0 0 > unknown between 1 > > > My question: > > this program is only printing first line. It is not processing every line > that cat spits to x1.py. > how do I print every line. You need to read every line. But since you appear to be on Unix have you looked at the cut command which would appear to do what you want directly? Unless you just want the learning experience. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Tue Mar 16 11:24:43 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 16 Mar 2010 21:24:43 +1100 Subject: [Tutor] raw_input() In-Reply-To: <9b00d1a91003151727j729d557dg1c75d163a90f955d@mail.gmail.com> References: <423811.5373.qm@web112506.mail.gq1.yahoo.com> <201003161028.43777.steve@pearwood.info> <9b00d1a91003151727j729d557dg1c75d163a90f955d@mail.gmail.com> Message-ID: <201003162124.44600.steve@pearwood.info> On Tue, 16 Mar 2010 11:27:13 am you wrote: > I think he thinks that python is going to read the whole 3.6GB of > data into memory in one hit, rather than using a small amount of > memory to process it line by line. But "for line in datafile" in your > code above uses a generator, right? So I don't think it's a problem - > correct me if I'm wrong. No, you are correct -- "for line in file" reads one line at a time. Beware, though, if the file isn't line-oriented, then each "line" (separated with a newline character) could be huge. -- Steven D'Aprano From marcodrompre at gmail.com Tue Mar 16 16:23:15 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Tue, 16 Mar 2010 11:23:15 -0400 Subject: [Tutor] Hi there! Message-ID: <4bdcec5e1003160823j2c4c4c8ctc02a515ce0d22c0b@mail.gmail.com> Hi! Does anyone of you know where to find all the solutions of Gerard Swinnen Python tutorial exercises ???? In the tutorial we just have the solutions to half of the exercises. Thank you -- Marc-O. Rompr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Tue Mar 16 17:33:08 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 16 Mar 2010 17:33:08 +0100 Subject: [Tutor] Hi there :.) In-Reply-To: References: Message-ID: Alan Gauld, 15.03.2010 20:28: > wrote >> (apparently python is slow ?!?). > > It is all relative. If you want to write fast moving graphics etc then > yes, you probably need C++. For anything else you might find Python is > fast enough. A good approach tends to be: write it in Python first, benchmark it, find out *if* it's too slow. If so, find out *what* exactly is too slow, optimise that code, benchmark again. Iterate. If there are still parts that are too slow, move the 1-5% of your code that are most time critical into Cython. Benchmark, optimise. Iterate. And only *then*, move on to plain C/C++ if you can really prove with verified benchmarking numbers that you need to. In almost all cases, you don't. In any case, it will safe you all of the hassle of writing the entire code in C/C++, which usually safes you weeks, months or years of developer time, depending on the complexity of your application. Stefan From jeffpeery at yahoo.com Tue Mar 16 21:56:40 2010 From: jeffpeery at yahoo.com (Jeff Peery) Date: Tue, 16 Mar 2010 13:56:40 -0700 (PDT) Subject: [Tutor] os.popen4 help! Message-ID: <305684.34167.qm@web43139.mail.sp1.yahoo.com> Hello, I'm trying to run an executable file from a python script. the executable is "opcenum.exe". OPCENUM.EXE is an executable that is usually found on computers with OPC software; such as OPC Servers or OPC Clients. The purpose of OPCENUM.EXE is to provide an interface into the local machine's Component Category Manager that can be accessed by remote OPC clients over DCOM. OPCEnum.exe provides several useful methods IOPCServerList::EnumClassesofCategory - gets available OPC servers for a specified category of OPC server I want to use IOPCServerList::EnumClassesofCategory but I am having some trouble. currently I do the below: import os cmd = 'opcenum.exe/IOPCServerList/EnumClassesofCategory 63D5F432-CFE4-11d1-B2C8-0060083BA1FB' fin,fout = os.popen4(cmd) result = fout.read() this doesn't work, and it hangs up on fout.read(). I'm do not understand the syntax of the arguments in popen4, i.e., I'm using slashes to get to the EnumClassesofCategory method, but should I use periods or what. The long number at the end is the argument to EnumClassesofCategory, and is the component category CLSID. details are here: http://opcactivex.com/Support/General/OPCEnum/opcenum.html how can I improve things here? thanks, Jeff -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Mar 16 23:08:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 17 Mar 2010 09:08:34 +1100 Subject: [Tutor] os.popen4 help! In-Reply-To: <305684.34167.qm@web43139.mail.sp1.yahoo.com> References: <305684.34167.qm@web43139.mail.sp1.yahoo.com> Message-ID: <201003170908.34968.steve@pearwood.info> On Wed, 17 Mar 2010 07:56:40 am Jeff Peery wrote: > Hello, > I'm trying to run an executable file from a python script. the > executable is "opcenum.exe". [...] > currently I do the below: > > import os > cmd = 'opcenum.exe/IOPCServerList/EnumClassesofCategory > 63D5F432-CFE4-11d1-B2C8-0060083BA1FB' > fin,fout = os.popen4(cmd) > result = fout.read() > > this doesn't work, and it hangs up on fout.read(). What happens if you run the exact same command from the shell? I assume you're using Windows. Open a DOS Window (command.com or cmd.exe or whatever it is called) and run: opcenum.exe/IOPCServerList/EnumClassesofCategory 63D5F432-CFE4-11d1-B2C8-0060083BA1FB and see what it does. Once you get the syntax right in the shell, then use the exact same syntax in popen4. -- Steven D'Aprano From jeffpeery at yahoo.com Wed Mar 17 03:35:55 2010 From: jeffpeery at yahoo.com (Jeff Peery) Date: Tue, 16 Mar 2010 19:35:55 -0700 (PDT) Subject: [Tutor] browing windows registry Message-ID: <26763.17682.qm@web43141.mail.sp1.yahoo.com> hello, I want to browse the windows registry for the program ids listed under the categories: 63D5F430-CFE4-11d1-B2C8-0060083BA1FB 63D5F432-CFE4-11d1-B2C8-0060083BA1FB where might be the best way to learn how to do that with python? thanks, Jeff -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Mar 17 09:28:56 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Mar 2010 08:28:56 -0000 Subject: [Tutor] browing windows registry References: <26763.17682.qm@web43141.mail.sp1.yahoo.com> Message-ID: "Jeff Peery" wrote > I want to browse the windows registry for the program ids listed under > the categories: > 63D5F430-CFE4-11d1-B2C8-0060083BA1FB > 63D5F432-CFE4-11d1-B2C8-0060083BA1FB > > where might be the best way to learn how to do that with python? I'd probably start with the registry module... _winreg Or you can use the Win32 API directly from Pythonwin or use ctypes. But _winreeg is probably marginally easier. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mail at timgolden.me.uk Wed Mar 17 09:58:41 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 17 Mar 2010 08:58:41 +0000 Subject: [Tutor] browing windows registry In-Reply-To: <26763.17682.qm@web43141.mail.sp1.yahoo.com> References: <26763.17682.qm@web43141.mail.sp1.yahoo.com> Message-ID: <4BA099C1.3080607@timgolden.me.uk> On 17/03/2010 02:35, Jeff Peery wrote: > hello, > I want to browse the windows registry for the program ids listed under the categories: > 63D5F430-CFE4-11d1-B2C8-0060083BA1FB > 63D5F432-CFE4-11d1-B2C8-0060083BA1FB > > where might be the best way to learn how to do that with python? Hopefully this might be of use: http://timgolden.me.uk/python-on-windows/programming-areas/registry.html TJG From karper12345 at yahoo.com Wed Mar 17 16:02:07 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Wed, 17 Mar 2010 08:02:07 -0700 (PDT) Subject: [Tutor] Problems with iterations and breaking loops. Message-ID: <916389.49945.qm@web44714.mail.sp1.yahoo.com> I'm having problems with iterations and loops. So I'm curious about the best Python-way to do iterations of lists (in if, while etc statements) and breaking of loops. I have a list of tuples with 2 values. I want to perform calculations on all of these for each value in a range of values (e.g. 100 to 110). list_of_tuples = [(90629, 4644), (90706, 4617), (90729, 4709)] #start value n = 100 #maximum value nmax = 110 #First I create a list for the values range_list = [] while n < int(nmax+1): ??? range_list.append(n) ??? n = n + 1 print range_list for i in range_list: ??? for t in list_of_tuples: ??????? val1 = t[0] ??????? val2 = t[1] ??????? print "do stuff with\t" + str(val1) + '\t' + str(val2) + \ ????????????? '\tfor rangevalue\t' + str(i) But I think that the rangelist is not needed and it can be done better (and faster for large quantities of data). I think it's better to have somethng like the code below. But I'm having problems with breaking the second loop and returning to the first loop. If I put in another while-statement befor the for-statement it stops after 1 run and it has to continue until the end of the range. while n < int(nmax + 1): ??? #create new variable to count in 2nd loop ??? n1 = n ??? print '\n' ??? for t in list_of_tuples: ??????? val1 = t[0] ??????? val2 = t[1] ??????? print "do stuff with\t" + str(val1) + '\t' + str(val2) + \ ????????????? '\tfor range\t' + str(n1) ??????? n1 = n1 + 1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Wed Mar 17 16:39:56 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 17 Mar 2010 08:39:56 -0700 Subject: [Tutor] Problems with iterations and breaking loops. In-Reply-To: <916389.49945.qm@web44714.mail.sp1.yahoo.com> References: <916389.49945.qm@web44714.mail.sp1.yahoo.com> Message-ID: On 3/17/2010 8:02 AM Karjer Jdfjdf said... > I'm having problems with iterations and loops. So I'm curious about the best Python-way to do iterations of lists (in if, while etc statements) and breaking of loops. > > I have a list of tuples with 2 values. I want to perform calculations on all of these for each value in a range of values (e.g. 100 to 110). > > > list_of_tuples = [(90629, 4644), (90706, 4617), (90729, 4709)] > > #start value > n = 100 > #maximum value > nmax = 110 > > #First I create a list for the values > range_list = [] > while n< int(nmax+1): > range_list.append(n) > n = n + 1 > > print range_list > Look up the documentation for range -- there is an easier answer. > > for i in range_list: > for t in list_of_tuples: > val1 = t[0] > val2 = t[1] you can also unpack a tuple like: for val1,val2 in list_of_tuples: > print "do stuff with\t" + str(val1) + '\t' + str(val2) + \ > '\tfor rangevalue\t' + str(i) > > But I think that the rangelist is not needed and it can be done better (and faster for large quantities of data). I think it's better to have somethng like the code below. But I'm having problems with breaking the second loop and returning to the first loop. If I put in another while-statement befor the for-statement it stops after 1 run and it has to continue until the end of the range. > From lie.1296 at gmail.com Wed Mar 17 18:26:17 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 18 Mar 2010 04:26:17 +1100 Subject: [Tutor] Problems with iterations and breaking loops. In-Reply-To: <916389.49945.qm@web44714.mail.sp1.yahoo.com> References: <916389.49945.qm@web44714.mail.sp1.yahoo.com> Message-ID: On 03/18/2010 02:02 AM, Karjer Jdfjdf wrote: > I'm having problems with iterations and loops.. So I'm curious about the > best Python-way to do iterations of lists (in if, while etc statements) > and breaking of loops. "Best" is a relative term to the current context of the problem. What is best for one case, might be worse for another. > I have a list of tuples with 2 values. I want to perform calculations on > all of these for each value in a range of values (e.g. 100 to 110). > > > list_of_tuples = [(90629, 4644), (90706, 4617), (90729, 4709)] > > #start value > n = 100 > #maximum value > nmax = 110 > > #First I create a list for the values > range_list = [] > while n < int(nmax+1): > range_list.append(n) > n = n + 1 "for i in xrange(n, nmax):" is a common idiom in python to do something a predetermined amount of time. Actually, all that code can be replaced with just "range_list = range(n, nmax)". Also, I think it is good if you can get used to counting with a half-open interval; though initially is a bit awkward, half-open counting is much less error prone. > print range_list > > > for i in range_list: > for t in list_of_tuples: > val1 = t[0] > val2 = t[1] > print "do stuff with\t" + str(val1) + '\t' + str(val2) + \ > '\tfor rangevalue\t' + str(i) > > But I think that the rangelist is not needed and it can be done better > (and faster for large quantities of data). I think it's better to have > somethng like the code below. But I'm having problems with breaking the > second loop and returning to the first loop. If I put in another > while-statement befor the for-statement it stops after 1 run and it has > to continue until the end of the range. I usually avoid preallocating large list to store results by using list-comprehension and generators. The interpreter would still have to allocate the list, but at least they're out of my immediate sight. Anyway, you should probably elaborate more about the problem you're having. What you've described up till now is "how I think the problem should be solved"; it is much more useful to describe "what the problem really is" so we can probably suggest a totally different approach to solve the problem. From alan.gauld at btinternet.com Wed Mar 17 19:20:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Mar 2010 18:20:11 -0000 Subject: [Tutor] Problems with iterations and breaking loops. References: <916389.49945.qm@web44714.mail.sp1.yahoo.com> Message-ID: "Karjer Jdfjdf" wrote > I have a list of tuples with 2 values. > I want to perform calculations on all of these > for each value in a range of values (e.g. 100 to 110). So, if I understand you correctly, you want to write a function perform_calculations(value, list_of_tuples, ): for v1,v2 in list_of_tuples: # something = calculate( value, v1, v2) return something Then you iterate over your values: results = [perform_calculations(value, list_of_tuples) for value in range(min,max+!)] results should then be a list of results (which might in turn be tuples or lists) Does that work for you? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From azhaguselvan at gmx.com Wed Mar 17 20:26:37 2010 From: azhaguselvan at gmx.com (Azhagu selvan) Date: Thu, 18 Mar 2010 00:56:37 +0530 Subject: [Tutor] plain text to HTML parser Message-ID: <91de62931003171226w1752e3aat39efa4aea602cf2e@mail.gmail.com> Hi, I want to get a formatted plain text and convert into HTML like what markdown does. I tried python-markdown. Is there any similar python libraries to do that parsing?? -- Azhagu Selvan From karper12345 at yahoo.com Wed Mar 17 21:09:58 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Wed, 17 Mar 2010 13:09:58 -0700 (PDT) Subject: [Tutor] Problems with iterations and breaking loops Message-ID: <977037.85187.qm@web44710.mail.sp1.yahoo.com> Thank you all for the input. I've not been clear in my original question. For each value in the range (in example 10) I have to perform calculations with the values in the tuple (in example 3). This makes 30 calculations in total. In reality I have to do this for a much larger dataset (eg 10000 range * 100000 = a lot), so the for v1, v2 in list_of_tuples doesn't work because it is too large for it. Solution 1 works, but the double for-statements seem problematic to me because if I have to add a 3rd or 4th etc the code becomes unreadable. I've tried to put it in a def, but: * Solution 2 is wrong because it only does part of the calculations * Solution 3 works, but returns it 10 times, so something is wrong with it! I gues I should go for solution 1, but I prefer one of the def solutions ( 2 or 3) because if I have to add more variables I get a very nested chunk of code (which I don't understand anymore in a month from now). list_of_tuples = [(90629, 4644.91), (90706, 4617.87), (90729, 4709.50)] n = 100 nmax = 110 def perform_calculations1(n, nmax, value, list_of_tuples, ): ??? # this gives totally wrong results ??? something = [] ??? for i2 in xrange(n, int(nmax+1)): ??????? for t in list_of_tuples: ??????????? v1 = t[0] ??????????? v2 = t[1] ??????????? something = str(i2) + "\tdoes something for\t" + str(v2)??? ??? return something def perform_calculations2(n, nmax, value, list_of_tuples, ): ??? #this works too good. It returns 10 times the correct 30 values ??? something = [] ??? for i2 in xrange(n, int(nmax+1)): ??????? for t in list_of_tuples: ??????????? v1 = t[0] ??????????? v2 = t[1] ??????????? something.append(str(i2) + "\tdoes something for\t" + str(v2)) ??? return something print "\n\nSolution 1" for i in xrange(n, int(nmax+1)): ??? #returns good results ??? for t in list_of_tuples: ??????? val1 = t[0] ??????? val2 = t[1] ??????? print "do stuff with\t" + str(val1) + '\t' + str(val2) + \ ????????????? '\tfor range\t' + str(i) print "\n\nSolution 2" for i in xrange(n, int(nmax + 1)): ??? #returns wrong results ??? result = perform_calculations1(n, nmax, i, list_of_tuples, ) ??? print result print "\n\nSolution 3" for i in xrange(n, int(nmax + 1)): ??? print "\n" ??? #returns good results, but gives it back 10 times!!! ??? result = perform_calculations2(n, nmax, i, list_of_tuples, ) ??? for r in result: ??????? print r -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmcaine at googlemail.com Wed Mar 17 23:31:21 2010 From: cmcaine at googlemail.com (C M Caine) Date: Wed, 17 Mar 2010 22:31:21 +0000 Subject: [Tutor] Problem with little program In-Reply-To: References: <4bdcec5e1003131056n3ae3030g57e19d903136149b@mail.gmail.com> Message-ID: > Whilst I agree with the general principle to use raw_input, I don't > see how we can say that it is the problem here. Indeed so far as > I can tell we don't even know what the problem here is other than > that the OP is trying to call the function he has defined in > another file. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ Apologies, I didn't read the entirety of the email, or perhaps read it too quickly, you are entirely correct. I reiterate these fine gentlemen's appeals for more information, Marco. From selva4210 at gmail.com Wed Mar 17 20:11:12 2010 From: selva4210 at gmail.com (Azhagu Selvan) Date: Thu, 18 Mar 2010 00:41:12 +0530 Subject: [Tutor] plain text to HTML parser Message-ID: <4BA12950.7080303@gmail.com> Hi, I want to get a formatted plaintext and convert into HTML like what markdown does. I tried python-mardown. Is there any similar python libraries to do that parsing?? -- Azhagu Selvan From eduardo.susan at gmail.com Thu Mar 18 02:41:31 2010 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Wed, 17 Mar 2010 19:41:31 -0600 Subject: [Tutor] plain text to HTML parser In-Reply-To: <4BA12950.7080303@gmail.com> References: <4BA12950.7080303@gmail.com> Message-ID: <9356b9f31003171841s23424abchede540f595a98af3@mail.gmail.com> On Wed, Mar 17, 2010 at 1:11 PM, Azhagu Selvan wrote: > Hi, > > I want to get a formatted plaintext and convert into HTML like what > markdown does. I tried python-mardown. Is there any similar python > libraries to do that parsing?? > > -- Azhagu Selvan > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hello, what do you mean by "formatted"? Markdown is a simple markup language, and there are several converters for it. What do you need that python markdown or Restructured Text wouldn't suit? Regards, Eduardo From alan.gauld at btinternet.com Thu Mar 18 09:50:54 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 18 Mar 2010 08:50:54 -0000 Subject: [Tutor] Problems with iterations and breaking loops References: <977037.85187.qm@web44710.mail.sp1.yahoo.com> Message-ID: "Karjer Jdfjdf" wrote > In reality I have to do this for a much larger dataset > (eg 10000 range * 100000 = a lot), so the for v1, v2 in > list_of_tuples doesn't work because it is too large for it. What does the size have to do with it? v1,v2 in list_of_tuples will only unpack one tuple at a time regardless of how big the list is. > I've tried to put it in a def, but: Take the outer loop out of the function. The point of functions is to break the problem down into manageable chunks. You should probably have 3 functions here: 1) to do the calculation on a single element 2) to apply function(1) to each item in the tuple list 3) a function to apply function(") from min to max That makes your code look like def function3(min,max) for n in range(min,max): function2(val, t_lst) def function2((val, tuples) for tuple in tuples: function3(val,tuple) def function(val,pair) # whatever calc you need That way you can test each function in turn and stop the nested loops from dominating the logic. def perform_calculations2(n, nmax, value, list_of_tuples, ): #this works too good. It returns 10 times the correct 30 values Because you have repeated the outer loop inside the function. Get rid of it. something = [] for i2 in xrange(n, int(nmax+1)): And please stop calling int() when the values are already ints. for t in list_of_tuples: v1 = t[0] v2 = t[1] something.append(str(i2) + "\tdoes something for\t" + str(v2)) return something The above is all you need in your function. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sweetsithara88 at gmail.com Thu Mar 18 07:02:18 2010 From: sweetsithara88 at gmail.com (sitharak) Date: Wed, 17 Mar 2010 23:02:18 -0700 (PDT) Subject: [Tutor] Assertion Error Message-ID: <27941982.post@talk.nabble.com> I tried this statement to store in RDF form store=TripleStore() But i'm getting this error Traceback (most recent call last): File "", line 1, in store=TripleStore() File "C:\Users\Administrator\Desktop\TripleStore.py", line 13, in __init__ super(TripleStore, self).__init__(backend=backend) File "build\bdist.win32\egg\rdflib\Graph.py", line 1008, in __init__ super(BackwardCompatGraph, self).__init__(store=backend) File "build\bdist.win32\egg\rdflib\Graph.py", line 815, in __init__ assert self.store.context_aware, ("ConjunctiveGraph must be backed by" AssertionError: ConjunctiveGraph must be backed by a context aware store. plz help -- View this message in context: http://old.nabble.com/Assertion-Error-tp27941982p27941982.html Sent from the Python - tutor mailing list archive at Nabble.com. From kpkirton at gmail.com Thu Mar 18 12:42:05 2010 From: kpkirton at gmail.com (Kevin Kirton) Date: Thu, 18 Mar 2010 22:42:05 +1100 Subject: [Tutor] Self-intro and two short newbie questions Message-ID: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> Hi all, I've committed myself to learning Python and have started reading "Learning Python" (Mark Lutz) and looking through various online resources. My career so far has involved a little high school teaching and about 10 years of translating Japanese to English, but no programming or coding. I've also joined this list today and this is my first post. My aim is to be able to create Python programs, specifically "activities" that work on the OLPC's XO laptops and SoaS (Sugar on a Stick). My questions are: how long would you estimate it to take and how complicated would it be to create the following as Python programs? (I know it varies depending on the person, but for example, how long would it take _you_?) (i) a simple guitar tuning program involving an image of a guitar and the playing of each of the standard strings of a guitar (E, A, D, G, B, E) upon key input by the user (something similar to this: http://www.gieson.com/Library/projects/utilities/tuner/ (page is 782kb to open)) and (ii) a very basic turtle art program with an intentionally limited set of commands and on-screen display words (say, a total of 30 to 50 specific strings), wherein the entire set of strings is offered to the user (perhaps at first use of the program) in a format that enables easy and full localization of the program so long as each of the strings is translated appropriately and inputted to the program. I know of turtle.py and xturtle.py, but I'm thinking of starting something from scratch. It's the easy localization I'm interested in. Hope these questions are appropriate. I'm grateful to be able to ask them here. Kevin (in Australia) From stefan_ml at behnel.de Thu Mar 18 12:54:44 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 18 Mar 2010 12:54:44 +0100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <4B8EFD6A.8080302@free.fr> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> <201003030229.21233.steve@pearwood.info> <4B8EFD6A.8080302@free.fr> Message-ID: Karim Liateni, 04.03.2010 01:23: > Steven D'Aprano wrote: >> def skip_blanks(lines): >> """Remove leading and trailing whitespace, ignore blank lines.""" >> for line in lines: >> line = line.strip() >> if line: >> yield line > > Is there a big difference to write your first functions as below because > I am not familiar with yield keyword? > > def skip_blanks(lines): > """Remove leading and trailing whitespace, ignore blank lines.""" > return [line.strip() in lines if line.strip()] Yes, a *big* difference in the true sense of the word. Your code (assuming you meant to write "... for line in ..." ) evaluates the entire list comprehension before returning from the call. Steven's code returns a generator that only handles one line (or a couple of empty lines) at a time. So, assuming that this runs against a large file, Steven's code uses only a constant amount of memory, compared to the whole file in your case, and is likely also a lot faster than your code as it involves less looping. Stefan From hugo.yoshi at gmail.com Thu Mar 18 13:26:44 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 18 Mar 2010 13:26:44 +0100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> <201003030229.21233.steve@pearwood.info> <4B8EFD6A.8080302@free.fr> Message-ID: <29179d161003180526h1220e79bhb9a95f44025a19b4@mail.gmail.com> On Thu, Mar 18, 2010 at 12:54 PM, Stefan Behnel wrote: > Karim Liateni, 04.03.2010 01:23: > > Yes, a *big* difference in the true sense of the word. Your code (assuming > you meant to write "... for line in ..." ) evaluates the entire list > comprehension before returning from the call. Steven's code returns a > generator that only handles one line (or a couple of empty lines) at a time. > So, assuming that this runs against a large file, Steven's code uses only a > constant amount of memory, compared to the whole file in your case, and is > likely also a lot faster than your code as it involves less looping. > Though, if you changed the brackets into parentheses, you'd get a generator expression, which *is* equivalent to Steven's version, except that it calls strip() twice, which is a bit wasteful. If the unnecessary extra call bothers you, you could do one of two things: 1) Learn how the yield keyword works. You should do this. It's an awesome feature, and you'll come across it many more times. 2) go functional and import itertools. ifilter with a generator expression, like so (pure functional programmers can also use imap instead of the generator expr., which might be faster. profile to be sure) def skip_blanks(lines): return ifilter(None, (l.strip() for l in lines)) Very short, has all the memory and speed benefits of the generator. Personally I really like terse functional programming like this, though I believe the general consensus in the python community is that imperative alternatives are usually clearer to read. If you want to know more about the yield keyword: A terse description (assumes that you know how iterators work) is here: http://docs.python.org/tutorial/classes.html#generators A more detailed description of iterators and generators can be found here: http://www.ibm.com/developerworks/library/l-pycon.html Hugo From waynejwerner at gmail.com Thu Mar 18 15:59:56 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Thu, 18 Mar 2010 09:59:56 -0500 Subject: [Tutor] Self-intro and two short newbie questions In-Reply-To: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> References: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> Message-ID: <333efb451003180759h43afe305uf9e7978cf9de890f@mail.gmail.com> On Thu, Mar 18, 2010 at 6:42 AM, Kevin Kirton wrote: > Hi all, > > I've committed myself to learning Python and have started reading > "Learning Python" (Mark Lutz) and looking through various online > resources. > My career so far has involved a little high school teaching and about > 10 years of translating Japanese to English, but no programming or > coding. > Welcome to python! I've never read that book, but I hear it's pretty good. > > I've also joined this list today and this is my first post. > > My aim is to be able to create Python programs, specifically > "activities" that work on the OLPC's XO laptops and SoaS (Sugar on a > Stick). > I don't know anything about the SoaS, but I do know a teeny bit about OLPC's XO laptops. Python is a great tool for several reasons - among them the availability of source code allows experimentation by XO users, which is in line with the XO philosophy. > > My questions are: how long would you estimate it to take and how > complicated would it be to create the following as Python programs? (I > know it varies depending on the person, but for example, how long > would it take _you_?) > > (i) a simple guitar tuning program involving an image of a guitar and > the playing of each of the standard strings of a guitar (E, A, D, G, > B, E) upon key input by the user > (something similar to this: > http://www.gieson.com/Library/projects/utilities/tuner/ (page is 782kb > to open)) > and > I could probably make something similar to that in less than a few hours, and most of it would be learning how to output the sound. If I had that knowledge I could make a VERY basic one in ~30 minutes, and make one that's rather nice within an hour or two. > (ii) a very basic turtle art program with an intentionally limited set > of commands and on-screen display words (say, a total of 30 to 50 > specific strings), wherein the entire set of strings is offered to the > user (perhaps at first use of the program) in a format that enables > easy and full localization of the program so long as each of the > strings is translated appropriately and inputted to the program. I > know of turtle.py and xturtle.py, but I'm thinking of starting > something from scratch. It's the easy localization I'm interested in. > That would probably take a little longer if only the more complicated design. That sounds like a project that I would probably work on over the course of a few days - some research, some design, some coding. Both of these projects are very possible for the beginner/novice with a little education (I'd start on the guitar tuner first as its level of complexity is much lower). HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Mar 18 18:22:51 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 18 Mar 2010 10:22:51 -0700 Subject: [Tutor] Assertion Error In-Reply-To: <27941982.post@talk.nabble.com> References: <27941982.post@talk.nabble.com> Message-ID: On 3/17/2010 11:02 PM sitharak said... > > I tried this statement to store in RDF form This is too specialized for this list. I couldn't find a dedicated news group for this project, but there is an IRC support channel -- see http://en.wikipedia.org/wiki/RDFLib#Support HTH, Emile > > store=TripleStore() > > But i'm getting this error > > Traceback (most recent call last): > File "", line 1, in > store=TripleStore() > File "C:\Users\Administrator\Desktop\TripleStore.py", line 13, in __init__ > super(TripleStore, self).__init__(backend=backend) > File "build\bdist.win32\egg\rdflib\Graph.py", line 1008, in __init__ > super(BackwardCompatGraph, self).__init__(store=backend) > File "build\bdist.win32\egg\rdflib\Graph.py", line 815, in __init__ > assert self.store.context_aware, ("ConjunctiveGraph must be backed by" > AssertionError: ConjunctiveGraph must be backed by a context aware store. > > plz help From denis.spir at gmail.com Thu Mar 18 19:05:00 2010 From: denis.spir at gmail.com (spir) Date: Thu, 18 Mar 2010 19:05:00 +0100 Subject: [Tutor] Self-intro and two short newbie questions In-Reply-To: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> References: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> Message-ID: <20100318190500.4b34733c@o> On Thu, 18 Mar 2010 22:42:05 +1100 Kevin Kirton wrote: > My questions are: how long would you estimate it to take and how > complicated would it be to create the following as Python programs? (I > know it varies depending on the person, but for example, how long > would it take _you_?) My opinion: The time required to make a working app (with exactly the same features, in the same language) by 2 trained programmers can easily range from 1 to 10. The time required to make a given app by a given programmer can easily range from 1 to 10 depending on the level of sophistication. "Devil hides in the details." ;-) Denis ________________________________ vit e estrany spir.wikidot.com From alan.gauld at btinternet.com Thu Mar 18 19:53:41 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 18 Mar 2010 18:53:41 -0000 Subject: [Tutor] Self-intro and two short newbie questions References: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> Message-ID: "Kevin Kirton" wrote > know it varies depending on the person, but for example, how long > would it take _you_?) > > (i) a simple guitar tuning program involving an image of a guitar and > the playing of each of the standard strings of a guitar (E, A, D, G, > B, E) upon key input by the user For a professional standard finish, working full time I'd be looking at a day or two. > (ii) a very basic turtle art program with an intentionally limited set > of commands and on-screen display words (say, a total of 30 to 50 > specific strings), wherein the entire set of strings is offered to the > user (perhaps at first use of the program) in a format that enables > easy and full localization of the program so long as each of the > strings is translated appropriately and inputted to the program. I > know of turtle.py and xturtle.py, but I'm thinking of starting > something from scratch. It's the easy localization I'm interested in. This might be three days. You could do both projects to a good standard in a week. However if you need to include things like user manuals, help system, online support(auto updates) etc plus installation programs (Dunno if thats relevant to OLPC etc) You could easily double the time and effort. HTH, Alan G. From snet-1 at msn.com Fri Mar 19 01:51:24 2010 From: snet-1 at msn.com (snet-1 at msn.com) Date: Fri, 19 Mar 2010 00:51:24 -0000 Subject: [Tutor] movement controls Message-ID: A little stuck and could do with any sudjestions. Aim:- When the character goes past the middle of the screen, the background & level move downwards so the character can get to higher places, think sonic the hedgehog. This is the bit I'm having problems with, I can get the character to get to the center, the background moves up & down but the character won't go back down when the level returns to normal height. if bkGroundRect['rect'].top < 0 and charRect['rect'].top < winCenterRect.top: bkGroundRect['rect'].top += spY platRect['rect'].top += spY jumpRect.top += spY charRect['dir'] = STOP if levelRect['rect'].bottom > winHeight and charRect['rect'].centery == winCenterx: bkGroundRect['rect'].bottom -= spY platRect['rect'].bottom -= spY # jumpRect.bottom -= spY charRect['dir'] = STOP #--this won't execute ? --# if levelRect['rect'].bottom <= winHeight: if charRect['rect'].centery >= winCenterx and charRect['rect'].centerx < jumpRect.centerx: charRect['dir'] = DOWN I've included the whole code as a note pad so the above makes more sense, don't worry about all the comments, that's just for my convenience. Thanks guys Mark 'knight meets wall, wall won't let knight pass, knight says 'neh' -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: New Text Document.txt URL: From rabidpoobear at gmail.com Fri Mar 19 02:25:39 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Thu, 18 Mar 2010 19:25:39 -0600 Subject: [Tutor] movement controls In-Reply-To: References: Message-ID: You should probably decouple the view from the model - have the character move around in a 2d plane in the model and then just draw the level in a certain area around the 2d coordinates of the character. On Thu, Mar 18, 2010 at 6:51 PM, wrote: > A little stuck and could do with any sudjestions. > > Aim:- > When the character goes past the middle of the screen, the background & > level move downwards so the character can get to higher places, think sonic > the hedgehog. > > This is the bit I'm having problems with, I can get the character to get to > the center, the background moves up & down but the character won't go back > down when the level returns to normal height. > > if bkGroundRect['rect'].top < 0 and charRect['rect'].top < > winCenterRect.top: > > bkGroundRect['rect'].top += spY > platRect['rect'].top += spY > jumpRect.top += spY > charRect['dir'] = STOP > > > if levelRect['rect'].bottom > winHeight and charRect['rect'].centery == > winCenterx: > bkGroundRect['rect'].bottom -= spY > platRect['rect'].bottom -= spY > # jumpRect.bottom -= spY > charRect['dir'] = STOP > > #--this won't execute ? --# > if levelRect['rect'].bottom <= winHeight: > if charRect['rect'].centery >= winCenterx and > charRect['rect'].centerx < jumpRect.centerx: > charRect['dir'] = DOWN > > I've included the whole code as a note pad so the above makes more sense, > don't worry about all the comments, that's just for my convenience. > > Thanks guys > > Mark > > 'knight meets wall, wall won't let knight pass, knight says 'neh' > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kpkirton at gmail.com Fri Mar 19 05:24:36 2010 From: kpkirton at gmail.com (Kevin Kirton) Date: Fri, 19 Mar 2010 15:24:36 +1100 Subject: [Tutor] Self-intro and two short newbie questions In-Reply-To: References: <47e5e0951003180442s3cfa3717p105221741e32ddd5@mail.gmail.com> Message-ID: <47e5e0951003182124w1a4a580fk314c98e5b2761485@mail.gmail.com> Thanks very much for the responses. I feel encouraged now to try to create the guitar tuner program by myself first, and then the simple turtle art program after that. It's kind of both exhilarating and daunting that I don't know exactly where to start at the moment, but that's the fun of learning I guess. From mark-ireland at msn.com Fri Mar 19 01:45:21 2010 From: mark-ireland at msn.com (mark-ireland at msn.com) Date: Fri, 19 Mar 2010 00:45:21 -0000 Subject: [Tutor] Movement controls useing pygame Message-ID: A little stuck and could do with any sudjestions. Aim:- When the character goes past the middle of the screen, the background & level move downwards so the character can get to higher places, think sonic the hedgehog. This is the bit I'm having problems with, I can get the character to get to the center, the background moves up & down but the character won't go back down when the level returns to normal height. if bkGroundRect['rect'].top < 0 and charRect['rect'].top < winCenterRect.top: bkGroundRect['rect'].top += spY platRect['rect'].top += spY jumpRect.top += spY charRect['dir'] = STOP if levelRect['rect'].bottom > winHeight and charRect['rect'].centery == winCenterx: bkGroundRect['rect'].bottom -= spY platRect['rect'].bottom -= spY # jumpRect.bottom -= spY charRect['dir'] = STOP #--this won't execute ? --# if levelRect['rect'].bottom <= winHeight: if charRect['rect'].centery >= winCenterx and charRect['rect'].centerx < jumpRect.centerx: charRect['dir'] = DOWN I've included the whole code as a note pad so the above makes more sense, don't worry about all the comments, that's just for my convenience. Thanks guys Mark 'knight meets wall, wall won't let knight pass, knight says 'neh' -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Fri Mar 19 10:01:14 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 19 Mar 2010 04:01:14 -0500 Subject: [Tutor] Movement controls useing pygame In-Reply-To: References: Message-ID: Are you just reposting this exact same e-mail to the list because you didn't see my reply to the other one, or are you ignoring what I said and posting the same message again, hoping someone else will answer? I hope it's not the latter, that's kind of insulting. The least you could do is say that my idea didn't make sense or wouldn't work for you in this case. -Luke On Thu, Mar 18, 2010 at 7:45 PM, wrote: > A little stuck and could do with any sudjestions. > > Aim:- > When the character goes past the middle of the screen, the background & > level move downwards so the character can get to higher places, think sonic > the hedgehog. > > This is the bit I'm having problems with, I can get the character to get to > the center, the background moves up & down but the character won't go back > down when the level returns to normal height. > > if bkGroundRect['rect'].top < 0 and charRect['rect'].top < > winCenterRect.top: > > bkGroundRect['rect'].top += spY > platRect['rect'].top += spY > jumpRect.top += spY > charRect['dir'] = STOP > > > if levelRect['rect'].bottom > winHeight and charRect['rect'].centery == > winCenterx: > bkGroundRect['rect'].bottom -= spY > platRect['rect'].bottom -= spY > # jumpRect.bottom -= spY > charRect['dir'] = STOP > > #--this won't execute ? --# > if levelRect['rect'].bottom <= winHeight: > if charRect['rect'].centery >= winCenterx and > charRect['rect'].centerx < jumpRect.centerx: > charRect['dir'] = DOWN > > I've included the whole code as a note pad so the above makes more sense, > don't worry about all the comments, that's just for my convenience. > > Thanks guys > > Mark > > 'knight meets wall, wall won't let knight pass, knight says 'neh' > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Fri Mar 19 10:56:54 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 19 Mar 2010 04:56:54 -0500 Subject: [Tutor] movement controls In-Reply-To: References: Message-ID: On Fri, Mar 19, 2010 at 4:09 AM, wrote: > But yes, I'm not quite sure about what you mean. I guess you mean keep > the character on the x plane <----> then for the jumping movements move the > level? what do you mean by 'You should probably decouple the view from the > model' > > > and it's preferable not to use html when sending e-mails, 'cause your font settings don't necessarily look good on other people's computers. You should really be asking this on the Pygame list, and you probably still should. Having said that... I'll try to answer your question. The way you're currently doing it, you have all of your sprites' coordinates just stored in random variables, and you're calculating where they should be on the screen and then drawing them directly, right? In other words, if player.x = 100 and player.y = 200 then he is located at 100, 200 on the screen? Having a view and a model relates to the MVC (model-view-controller) paradigm in programming. Basically think of it like this. Suppose I have a window that I'm looking out, and my dog is outside running around somewhere. Now you walk by the window. Does it make more sense for me to model your interaction with my dog in relation to what I can see out of my window, or should I model your interaction with the dog in a separate space, independent of where I may be looking? Say I want to describe your position to my friend who lives up and to the right of me. Would I say "see that guy by the bench petting my dog?" or would I say "see the guy that is 12 meters forward and 1 meter to the right of the center of my window?" The friend doesn't have any idea what perspective _I_ view the world from, and he really shouldn't have to. SO in the MVC framework, you model your _world_ space and your VIEW is just an outside observer "peeking" at the current state of the world. So, for example, say your world is a grid of 1000 x 8000 units, and your character is located at 200x200. There might be an enemy moving around at 800x2000, who may not be on the screen, but I still want to model that enemy moving around, so that when the character gets over there, the enemy will be in a new location. Or maybe he'll walk over and be on the screen. When you do it this way, you can merely say "render everything that is around my player's current position" and then all your problems are solved. The view will automatically follow the player, and when he jumps up, the view will raise with him. If you set an upper and lower limit, then the view will naturally not move until the player moves past a certain point of the screen. If you continue to refer to the game objects in terms of their graphical position in relation to the screen, it's going to get very convoluted. In order to "decouple your view from your model" you will likely have to rewrite a lot of your code -- the key here is that you should've written it this way (MVC) in the first place. Read up more on MVC, and specifically how it relates to game design. Also, when you reply, use Reply to All otherwise your reply is only seen by me, not by the other members on the list. Good luck, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From karper12345 at yahoo.com Fri Mar 19 11:55:06 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Fri, 19 Mar 2010 03:55:06 -0700 (PDT) Subject: [Tutor] difflib context to string-object? Message-ID: <424786.14992.qm@web44709.mail.sp1.yahoo.com> With difflib.context_diff it is possible to write the context to files. difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]) Is it also possible to do this to seperate string-objects instead of writing them to files? -------------- next part -------------- An HTML attachment was scrubbed... URL: From richardbwest at gmail.com Fri Mar 19 13:33:36 2010 From: richardbwest at gmail.com (richard west) Date: Fri, 19 Mar 2010 19:33:36 +0700 Subject: [Tutor] using pythnn to open a password protected website Message-ID: Hi, Im trying to use python to open up a password protected website(e.g. facebook / gmail) in Firefox. supplying the login and password automatically at runtime - so that I can interface my code with fingerprint recognition code. So far I have only found urllib, urllib2 and web browser, none of which I have been able to use. urllib2 can log me in, but then I cant pass this authenicated login to the browser. At the very least I need to be able to send the form post information to the browser via my code, and preferably auto-submit the form. Does anybody have any ideas? Many Thanks, Richard. -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Fri Mar 19 13:45:26 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 19 Mar 2010 07:45:26 -0500 Subject: [Tutor] using pythnn to open a password protected website In-Reply-To: References: Message-ID: <333efb451003190545l637031edvf3618641c2b6bc73@mail.gmail.com> On Fri, Mar 19, 2010 at 7:33 AM, richard west wrote: > Hi, > > Im trying to use python to open up a password protected website(e.g. > facebook / gmail) in Firefox. supplying the login and password automatically > at runtime - so that I can interface my code with fingerprint recognition > code. So far I have only found urllib, urllib2 and web browser, none of > which I have been able to use. > > urllib2 can log me in, but then I cant pass this authenicated login to the > browser. At the very least I need to be able to send the form post > information to the browser via my code, and preferably auto-submit the form. > > Does anybody have any ideas? Check the tamper data plugin for firefox. I think that's the one. It should help you get the types of parameters, etc. that are actually passed to the website. Then I think you can use urllib2's variety of methods to connect to the site. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri Mar 19 15:11:38 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 19 Mar 2010 10:11:38 -0400 Subject: [Tutor] difflib context to string-object? In-Reply-To: <424786.14992.qm@web44709.mail.sp1.yahoo.com> References: <424786.14992.qm@web44709.mail.sp1.yahoo.com> Message-ID: <4BA3861A.9020407@gmail.com> On 3/19/2010 6:55 AM, Karjer Jdfjdf wrote: > With difflib.context_diff it is possible to write the context to files. > > difflib.context_diff(/a/, /b/[, /fromfile/][, /tofile/][, > /fromfiledate/][, /tofiledate/][, /n/][, /lineterm/]) > > Is it also possible to do this to seperate string-objects instead of > writing them to files? > > Take a look at the stringIO module. -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Fri Mar 19 15:46:19 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Fri, 19 Mar 2010 07:46:19 -0700 Subject: [Tutor] Understanding (Complex) Modules In-Reply-To: <201003051324.16104.steve@pearwood.info> References: <4B8F0BD3.8050000@sbcglobal.net> <201003051324.16104.steve@pearwood.info> Message-ID: <4BA38E3B.5020904@sbcglobal.net> (I meant to post this to both the list and Steven back a week or so ago. I missed the list, so am posting it here now. Unfortunately, I'm again in a position where I cannot respond for several days, but I will be back early next week to examine some posts that followed this.) =================================================== Thanks. I'm on the road for a few days and will be able to read this more carefully when I get back. I had a feeling from my first attempts of looking at MPL and other heavy-duty libs that it was going to require work to dig out what I needed from the module itself. In my personal view, the usage and learning documents are fairly limiting. Perhaps on in MPL, which is less traditional than numpy and the others, which are founded on common and historic implementations of math and science from older languages. From: Steven D'Aprano To: tutor at python.org Sent: Thu, March 4, 2010 6:24:15 PM Subject: Re: [Tutor] Understanding (Complex) Modules On Thu, 4 Mar 2010 12:24:35 pm Wayne Watson wrote: > First a little preamble before my questions. > > Most of my work in Python has required modifying a program that uses > modules that were imported by the original program. I've made some > use of modules on a command line like math, and have used the idea of > a qualifier. On occasion, I've used examples from matplotlib that > required from matplotlib.image import AxesImage. Further, I've > created some simple classes, and produced objects with them. No use > of inheritance though. So far so good. In a few places, it is said > modules are objects. I'm slightly puzzled by that, but in some way it > seems reasonable from the standpoint of period notation. So far I > have not created a module. Yes you have, you just don't know it. In simple language, a file with Python-usable code in it is a module. There are some technicalities and complexities, but in basic terms, that's all it is. Technically, "module" refers only to the object which exists inside the Python environment after you call "import thing". The import statement does a whole lot of tricks, but in a nutshell it: * finds a file containing code called 'thing' * loads it into memory, executing code as needed * creates a 'module' object to store the code and data in the file * and makes it available to your code Python can create module objects from: compiled Windows DLLs .dll compiled Linux object files .so Python source code .py Python byte code .pyc and .pyo (and .pyw on Windows) Zip files containing any of the above and probably other things as well, but they're the main ones. So any Python file you create (any .py file) is a module, or at least it would be a module if you import it. Ignoring all the various compiled files (.dll, .so, etc) what happens when you run a .py file from the command line (or from IDLE or another IDE). E.g. you type something like: python.exe myscript.py [enter] Python reads the file myscript.py and executes it, but no module object is created. It is possible that a module object *is* created, for internal use, then thrown away when the script is finished. But your script doesn't see the module object. However, if you enter the Python interactive environment, and do this: >>> import myscript # no .py Python loads the file myscript.py into a module object, executing any code in it, and makes it available as a module. > Here comes the questions. Recently I began to use matplotlib, scipy, > and pylab, mostly matplotlib. I've ground out a few useful pieces of > code, but I'm fairly baffled by the imports required to get at > various elements, of say, matplotlib (MPL). Some of the MPL examples > use of imports make sense, but how the writer pulled in the necessary > elements is not. How does one go about understanding the > capabilities of such modules? Time, experience, and a lot of hard work. Welcome to programming! If the documentation is good, then read the documentation. If the documentation is lacking, or bad, or even wrong, then read the source code, or search the Internet for a tutorial, or buy a book about it (the numpy people, for example, sell books about numpy). Python makes experimentation easy: there are a lot of powerful introspection facilities in Python. The interactive interpreter is your best friend. You will live with it, eat with it, sleep with it, take it on double-dates, and throw yourself on live grenades to protect it. Whenever I'm programming, I almost always have three or five interactive sessions open for experimentation. The dir() and help() functions are also good friends. In an interactive session: import math dir(math) # prints a list of names in the math module help(math.sin) help(math) Don't feel that you have to understand the entire module before you use it. Many (alas, not all) modules have a reasonably gentle learning curve: you can start using math.sin without needing to know what math.sinh is for. Google and Wikipedia are also your friends, although not your *best* friends. (Don't necessarily believe *everything* you read on the Internet.) Don't forget other search engines apart from Google: they're good, but not perfect. > MPL seems to have a lot of lower level > components. Some of them are laid out over numerous pages as in the > form of a, say, English language, description. How does one decipher > this stuff. For example, open the module in an editor and start > looking at the organization? I thinkthe so called MPL guide ins 900 > pages long. Even the numpy guide (reference?), I believe borders on > 1000 pages. There must be some way to untangle these complex modules, > I would think. Some of the tutorials seem nothing more than a > template to follow for a particular problem. So far, looking at the > plentiful number of examples of MPL, and probably some of the other > modules mentioned above have not provided a lot of insight. Big, complex modules tend to have steep learning curves. There's no magic path to learning how to use a big complex module any more than there is a magic path to learning how to be a brain surgeon, or a car mechanic. > Is there some relationship between modules and objects that I'm not > seeing that could be of value? Modules are themselves objects. Everything in Python is an object: strings, ints, floats, lists, tuples, everything. Modules are compound objects, in that they contain other objects accessible by name: math.sin means "look up the name 'sin' in the math module, and return whatever you find", which in this case is a function object. And that's pretty much it. -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor at python.org From eire1130 at gmail.com Fri Mar 19 17:41:11 2010 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 19 Mar 2010 12:41:11 -0400 Subject: [Tutor] Efficiency and speed Message-ID: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Hello all: I've still been working towards learning the language, albeit slowly and I've been working on a project that is somewhat intense on the numerical calculation end of things. Running 10,000 trials takes about 1.5 seconds and running 100,000 trials takes 11 seconds. Running a million trials takes about 81 seconds or so. I don't think 1M trials is needed for accuracy, but 100K probably is. I've made a few other optimizations today that I won't be able to test until I get home, but I was wondering if any of you could give some general pointers on how to make python run a little more quickly. There are two major pieces of code that seem slow, so I'll share the first with you. This section takes up about 1/3 of the time used when running all trials, where trials is 10K or larger. This section doesn't actually do any math, all it's doing is returning the "bin" that the randomly generated number falls in. The second section, the one that is taking up most of the time, does the math. The list nx1 is about 220 floating point numbers long. > sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample > values based on nx1 countlist = [] self.nx1.append(0) #puts a zero on the end of nx1. This is for the case > where one of the random values lies past the minimum number (it scales from > 10M down to almost 0, but not exactly 0) Antyhing past this dates yields a > 0. for s in self.mcrange_gen(sample): countlist.append(s-1) # This appends the bin number (the number > returned previously minus one) to make slices of the premium streams. and here is the generator section: def mcrange_gen(self, sample):#, lensample): lensample = len(sample) # this section is just for speed. All of these > are renames from the globals to bring calc times down at the expense of > memory. I haven't tested these yet. nx2 = self.nx1 nx2_append = nx2.append nx2_sort = nx2.sort nx2_reverse = nx2.reverse nx2_index = nx2.index nx2_remove = nx2.remove for s in range(lensample): q = sample[s] #takes the next randomly generated number from the > sample list nx2_append(q) # and appends it to nx list. nx2_sort() # and sorts it in place nx2_reverse() # reverses the list, because this was the original > order i = nx2_index(q) #get the index of that element nx2_remove(q) # and remove the element. yield i # send the position of that element back to the main > program. -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Fri Mar 19 18:56:39 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 19 Mar 2010 18:56:39 +0100 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: James Reynolds, 19.03.2010 17:41: > I've still been working towards learning the language, albeit slowly and > I've been working on a project that is somewhat intense on the numerical > calculation end of things. > > Running 10,000 trials takes about 1.5 seconds and running 100,000 trials > takes 11 seconds. Running a million trials takes about 81 seconds or so. I > don't think 1M trials is needed for accuracy, but 100K probably is. > > I've made a few other optimizations today that I won't be able to test until > I get home, but I was wondering if any of you could give some general > pointers on how to make python run a little more quickly. > > There are two major pieces of code that seem slow, so I'll share the first > with you. This section takes up about 1/3 of the time used when running all > trials, where trials is 10K or larger. > > This section doesn't actually do any math, all it's doing is returning the > "bin" that the randomly generated number falls in. > > The second section, the one that is taking up most of the time, does the > math. > > The list nx1 is about 220 floating point numbers long. > > > >> sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample >> values based on nx1 > > countlist = [] > > self.nx1.append(0) #puts a zero on the end of nx1. This is for the case >> where one of the random values lies past the minimum number (it scales from >> 10M down to almost 0, but not exactly 0) Antyhing past this dates yields a >> 0. > > for s in self.mcrange_gen(sample): > > countlist.append(s-1) # This appends the bin number (the number >> returned previously minus one) to make slices of the premium streams. > > > and here is the generator section: > > def mcrange_gen(self, sample):#, lensample): > > lensample = len(sample) # this section is just for speed. All of these >> are renames from the globals to bring calc times down at the expense of >> memory. I haven't tested these yet. > > nx2 = self.nx1 > > nx2_append = nx2.append > > nx2_sort = nx2.sort > > nx2_reverse = nx2.reverse > > nx2_index = nx2.index > > nx2_remove = nx2.remove > > for s in range(lensample): > > q = sample[s] #takes the next randomly generated number from the >> sample list > > nx2_append(q) # and appends it to nx list. > > nx2_sort() # and sorts it in place > > nx2_reverse() # reverses the list, because this was the original >> order > > i = nx2_index(q) #get the index of that element > > nx2_remove(q) # and remove the element. > > yield i # send the position of that element back to the main >> program. This certainly falls into the bin of the most inefficient algorithms I've ever seen. Even walking through the samples one by one to find the target bin would be faster than the above. Could you try to describe in a couple of words what this algorithm is supposed to do? That will almost certainly make it clear how you should write it instead. Stefan From karim.liateni at free.fr Fri Mar 19 19:46:34 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Fri, 19 Mar 2010 19:46:34 +0100 Subject: [Tutor] parsing a "chunked" text file In-Reply-To: <29179d161003180526h1220e79bhb9a95f44025a19b4@mail.gmail.com> References: <64fb0beb1003012222j41644a07gafad675aea67ffa1@mail.gmail.com> <201003030229.21233.steve@pearwood.info> <4B8EFD6A.8080302@free.fr> <29179d161003180526h1220e79bhb9a95f44025a19b4@mail.gmail.com> Message-ID: <4BA3C68A.3020307@free.fr> Hello, Thanks both of you for these useful information. Regards Karim Hugo Arts wrote: > On Thu, Mar 18, 2010 at 12:54 PM, Stefan Behnel wrote: > >> Karim Liateni, 04.03.2010 01:23: >> >> Yes, a *big* difference in the true sense of the word. Your code (assuming >> you meant to write "... for line in ..." ) evaluates the entire list >> comprehension before returning from the call. Steven's code returns a >> generator that only handles one line (or a couple of empty lines) at a time. >> So, assuming that this runs against a large file, Steven's code uses only a >> constant amount of memory, compared to the whole file in your case, and is >> likely also a lot faster than your code as it involves less looping. >> >> > > Though, if you changed the brackets into parentheses, you'd get a > generator expression, which *is* equivalent to Steven's version, > except that it calls strip() twice, which is a bit wasteful. > > If the unnecessary extra call bothers you, you could do one of two things: > 1) Learn how the yield keyword works. You should do this. It's an > awesome feature, and you'll come across it many more times. > 2) go functional and import itertools. ifilter with a generator > expression, like so (pure functional programmers can also use imap > instead of the generator expr., which might be faster. profile to be > sure) > > def skip_blanks(lines): > return ifilter(None, (l.strip() for l in lines)) > > Very short, has all the memory and speed benefits of the generator. > Personally I really like terse functional programming like this, > though I believe the general consensus in the python community is that > imperative alternatives are usually clearer to read. > > If you want to know more about the yield keyword: > A terse description (assumes that you know how iterators work) is > here: http://docs.python.org/tutorial/classes.html#generators > A more detailed description of iterators and generators can be found > here: http://www.ibm.com/developerworks/library/l-pycon.html > > Hugo > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eire1130 at gmail.com Fri Mar 19 19:47:45 2010 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 19 Mar 2010 14:47:45 -0400 Subject: [Tutor] Efficiency and speed In-Reply-To: References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: <98c3f7c51003191147v758acfb7u6a8942c2c4e838ca@mail.gmail.com> Well, I'm always out to impress! This is a monte-carlo simulation. The simulation measures the expiration of something and those somethings fall into bins that are not evenly dispersed. These bins are stored in the nx list mentioned previously. So let's say you have the bins, a, b,c,d,e,f and you have the value z from the sample list where z >b and <= a. In this case, it should return the index value at position (a). These, in turn, are used to take slices of other lists and only apply those slices from position zero to position (a), in the case above. I previously tried doing something like: if z >b and <= a then append to new list, else pass, but that seemed like a slower way of going about it, because python needs to cycle through the entire list to find the right value. I guess i could add a count to the list, like len(appendedlist) and using a while loop, so it will stop checking once it's been appended and move to the next iteration in the sample list. But before you recommend a better way of writing it, I would greatly appreciate to know why the above is inefficient and some other way is more efficient. I'm trying to learn the concepts behind programming in general and how to program in python at the same time. On Fri, Mar 19, 2010 at 1:56 PM, Stefan Behnel wrote: > James Reynolds, 19.03.2010 17:41: > > I've still been working towards learning the language, albeit slowly and >> I've been working on a project that is somewhat intense on the numerical >> calculation end of things. >> >> Running 10,000 trials takes about 1.5 seconds and running 100,000 trials >> takes 11 seconds. Running a million trials takes about 81 seconds or so. I >> don't think 1M trials is needed for accuracy, but 100K probably is. >> >> I've made a few other optimizations today that I won't be able to test >> until >> I get home, but I was wondering if any of you could give some general >> pointers on how to make python run a little more quickly. >> >> There are two major pieces of code that seem slow, so I'll share the first >> with you. This section takes up about 1/3 of the time used when running >> all >> trials, where trials is 10K or larger. >> >> This section doesn't actually do any math, all it's doing is returning the >> "bin" that the randomly generated number falls in. >> >> The second section, the one that is taking up most of the time, does the >> math. >> >> The list nx1 is about 220 floating point numbers long. >> >> >> >> sample = random.sample(range(int(self.nx1[b])), trials) # a list of >>> sample >>> values based on nx1 >>> >> >> countlist = [] >> >> self.nx1.append(0) #puts a zero on the end of nx1. This is for the case >> >>> where one of the random values lies past the minimum number (it scales >>> from >>> 10M down to almost 0, but not exactly 0) Antyhing past this dates yields >>> a >>> 0. >>> >> >> for s in self.mcrange_gen(sample): >> >> countlist.append(s-1) # This appends the bin number (the number >> >>> returned previously minus one) to make slices of the premium streams. >>> >> >> >> and here is the generator section: >> >> def mcrange_gen(self, sample):#, lensample): >> >> lensample = len(sample) # this section is just for speed. All of these >> >>> are renames from the globals to bring calc times down at the expense of >>> memory. I haven't tested these yet. >>> >> >> nx2 = self.nx1 >> >> nx2_append = nx2.append >> >> nx2_sort = nx2.sort >> >> nx2_reverse = nx2.reverse >> >> nx2_index = nx2.index >> >> nx2_remove = nx2.remove >> >> for s in range(lensample): >> >> q = sample[s] #takes the next randomly generated number from the >> >>> sample list >>> >> >> nx2_append(q) # and appends it to nx list. >> >> nx2_sort() # and sorts it in place >> >> nx2_reverse() # reverses the list, because this was the original >> >>> order >>> >> >> i = nx2_index(q) #get the index of that element >> >> nx2_remove(q) # and remove the element. >> >> yield i # send the position of that element back to the main >> >>> program. >>> >> > This certainly falls into the bin of the most inefficient algorithms I've > ever seen. Even walking through the samples one by one to find the target > bin would be faster than the above. > > Could you try to describe in a couple of words what this algorithm is > supposed to do? That will almost certainly make it clear how you should > write it instead. > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Mar 19 19:58:37 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Mar 2010 18:58:37 -0000 Subject: [Tutor] using pythnn to open a password protected website References: Message-ID: "richard west" wrote > Im trying to use python to open up a password protected website(e.g. > facebook / gmail) in Firefox. So you are trying to control Firefox not the web site? Is that correct? What mechanism are you using to communicate with Firefox? > code. So far I have only found urllib, urllib2 and web browser, none of > which I have been able to use. urllib is for interacting with the web site not with the browser. webbrowser is for launching a browser for a user to interact with, it does not really control the browser programatically. You may need to use OS dependent tools like COM on Windows or maybe even GUI events/messages to drive the browser remotely. Are you sure that's really what you want to do? Or do you really want to fill in and submit a web form on a page protected by a login? If thats the case you can do it all from Python without going near Firefox. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Fri Mar 19 20:01:38 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 19 Mar 2010 12:01:38 -0700 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: On 3/19/2010 9:41 AM James Reynolds said... OK, so starting here: def mcrange_gen(self, sample): lensample = len(sample) nx2 = self.nx1 nx2_append = nx2.append nx2_sort = nx2.sort nx2_reverse = nx2.reverse nx2_index = nx2.index nx2_remove = nx2.remove for s in range(lensample): q = sample[s] nx2_append(q) nx2_sort() nx2_reverse() i = nx2_index(q) nx2_remove(q) yield i First, the two lines: for s in range(lensample): q = sample[s] variable s is never used again, so instead we'll do: for q in sample: Which renders lensample as unused, so throw out lensample = len(sample) We now have: def mcrange_gen(self, sample): nx2 = self.nx1 nx2_append = nx2.append nx2_sort = nx2.sort nx2_reverse = nx2.reverse nx2_index = nx2.index nx2_remove = nx2.remove for q in sample: nx2_append(q) nx2_sort() nx2_reverse() i = nx2_index(q) nx2_remove(q) yield i Now, let's see what's going on for each q. You append it to self.nx1 (through the nx2 reference), then sort nx1, then reserve nx1, then scan for the new position of q, note the index, remove it from nx1, and yeild the index. OK, so that way you find out between which two elements of nx1 the current q falls, and i becomes the bin. So, how about something like (untested): def mcrange_gen(self, sample): self.nx1.sort() # now the bin boundries are in order for q in sample: ii = -1 for binlimit in self.nx1: if q Hello, I want to do a executable for linux/unix from python scripts and thus o allow to run on machine which doesn't have recent version (2.0) of python. I found the compile() method but how can I use it to make all in one executable which could be run on old system (old python). If you have any links I would love to have it Thanks Karim From alan.gauld at btinternet.com Fri Mar 19 20:15:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Mar 2010 19:15:00 -0000 Subject: [Tutor] Efficiency and speed References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: "James Reynolds" wrote > I've made a few other optimizations today that I won't be able to test > until > I get home, but I was wondering if any of you could give some general > pointers on how to make python run a little more quickly. Always, always, get the algorithm efficient before trying to make the code efficient. Then eliminate redundant variable assignments, extra loops, hidden loops (like in, any etc) Then use the profiler to identify the hot spots. Then fine tune the hot spots. This is where you can start to worry about the speedups of using local variables etc. > There are two major pieces of code that seem slow, so I'll share the > first > with you. This section takes up about 1/3 of the time used when running > all > trials, where trials is 10K or larger. How are you measuring? Is it via the profiler? Is it by inserying print time statements? Is is subjectively timing it by hand? > The second section, the one that is taking up most of the time, does the > math. Thats probably what you would expect if the math is complex. > The list nx1 is about 220 floating point numbers long. So not very big at all... >> sample = random.sample(range(int(self.nx1[b])), trials) # a list of >> sample >> values based on nx1 The use of self suggests there is an object, or at least a class definition involved? > for s in self.mcrange_gen(sample): > countlist.append(s-1) # This appends the bin number (the number > def mcrange_gen(self, sample):#, lensample): > lensample = len(sample) # this section is just for speed. All of these >> are renames from the globals to bring calc times down at the expense of >> memory. I haven't tested these yet. This is premature optimisation at this stage. Its cluttering up the code for relatively little benefit. > for s in range(lensample): > q = sample[s] #takes the next randomly generated number from the for q in sample would be more pythonic > nx2_append(q) # and appends it to nx list. > nx2_sort() # and sorts it in place > nx2_reverse() # reverses the list, because this was the original So you sort and reverse the entire list every time round the for loop? Might it be more efficient to keep the list in the right order to start with? > i = nx2_index(q) #get the index of that element > nx2_remove(q) # and remove the element. Now you find the thing you inserted and remove it. Wow. > yield i # send the position of that element back to the main So you really just want to find out where you would like to insert it in an already sorted/reversed list? Back to step one - can you improve the algorithm? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Mar 19 20:39:57 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Mar 2010 19:39:57 -0000 Subject: [Tutor] Tutorial executable from python script. References: <4BA3CBED.6070509@free.fr> Message-ID: "Karim Liateni" wrote > on machine which doesn't have recent version (2.0) of python. Given that v2 is at least 10 years old now that's not really "recent" I'd be surprised if any current Linux distros had anything that old on them! Even the ones designed for old hardware. In fact, are you sure your code runs on v1 python? There have been a lot of language changes since then. > the compile() > method but how can I use it to make all in one executable compile() doesn't make an exe, it compiles python script into python byte code - much as the javac compiler compiles java source into java bytecode. > run on old system (old python). Exactly how old is old? If you write the code to run on an old Python interpreter it should still work(mostly!) on Python 2.6. The trick is not to compile the code but to write code that is consistent wityh the oldest version of Python you need to run on. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From eire1130 at gmail.com Fri Mar 19 21:17:29 2010 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 19 Mar 2010 16:17:29 -0400 Subject: [Tutor] Efficiency and speed In-Reply-To: References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> Here's another idea I had. I thought this would be slower than then the previous algorithm because it has another for loop and another while loop. I read that the overhead of such loops is high, so I have been trying to avoid using them where possible. def mcrange_gen(self, sample): nx2 = self.nx1 for q in sample: for a in nx2: while a > q: pass yield a break On Fri, Mar 19, 2010 at 3:15 PM, Alan Gauld wrote: > "James Reynolds" wrote > > > I've made a few other optimizations today that I won't be able to test >> until >> I get home, but I was wondering if any of you could give some general >> pointers on how to make python run a little more quickly. >> > > Always, always, get the algorithm efficient before trying to make > the code efficient. > > Then eliminate redundant variable assignments, extra loops, > hidden loops (like in, any etc) > > Then use the profiler to identify the hot spots. > > Then fine tune the hot spots. > This is where you can start to worry about the speedups of > using local variables etc. > > > There are two major pieces of code that seem slow, so I'll share the first >> with you. This section takes up about 1/3 of the time used when running >> all >> trials, where trials is 10K or larger. >> > > How are you measuring? Is it via the profiler? Is it by inserying print > time statements? Is is subjectively timing it by hand? > > > The second section, the one that is taking up most of the time, does the >> math. >> > > Thats probably what you would expect if the math is complex. > > > The list nx1 is about 220 floating point numbers long. >> > > So not very big at all... > > > sample = random.sample(range(int(self.nx1[b])), trials) # a list of sample >>> values based on nx1 >>> >> > The use of self suggests there is an object, or at least a class definition > involved? > > > for s in self.mcrange_gen(sample): >> countlist.append(s-1) # This appends the bin number (the number >> > > def mcrange_gen(self, sample):#, lensample): >> lensample = len(sample) # this section is just for speed. All of these >> >>> are renames from the globals to bring calc times down at the expense of >>> memory. I haven't tested these yet. >>> >> > This is premature optimisation at this stage. Its cluttering up the code > for relatively little benefit. > > > for s in range(lensample): >> q = sample[s] #takes the next randomly generated number from the >> > > for q in sample > > would be more pythonic > > > nx2_append(q) # and appends it to nx list. >> nx2_sort() # and sorts it in place >> nx2_reverse() # reverses the list, because this was the original >> > > So you sort and reverse the entire list every time round the for loop? > Might it be more efficient to keep the list in the right order to start > with? > > > i = nx2_index(q) #get the index of that element >> nx2_remove(q) # and remove the element. >> > > Now you find the thing you inserted and remove it. Wow. > > > yield i # send the position of that element back to the main >> > > So you really just want to find out where you would like to insert it > in an already sorted/reversed list? > > Back to step one - can you improve the algorithm? > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Mar 20 01:25:07 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 20 Mar 2010 11:25:07 +1100 Subject: [Tutor] difflib context to string-object? In-Reply-To: <424786.14992.qm@web44709.mail.sp1.yahoo.com> References: <424786.14992.qm@web44709.mail.sp1.yahoo.com> Message-ID: <201003201125.07284.steve@pearwood.info> On Fri, 19 Mar 2010 09:55:06 pm Karjer Jdfjdf wrote: > With difflib.context_diff it is possible to write the context to > files. > > difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, > tofiledate][, n][, lineterm]) > > Is it also possible to do this to seperate string-objects instead of > writing them to files? Use StringIO objects, which are fake files that can be read as strings. Untested and entirely from memory, use: from StringIO import StringIO fromfile = StringIO("some text goes here") tofile = StringIO("some different text goes here") -- Steven D'Aprano From steve at pearwood.info Sat Mar 20 01:27:26 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 20 Mar 2010 11:27:26 +1100 Subject: [Tutor] using pythnn to open a password protected website In-Reply-To: References: Message-ID: <201003201127.26289.steve@pearwood.info> On Fri, 19 Mar 2010 11:33:36 pm richard west wrote: > Hi, > > Im trying to use python to open up a password protected website(e.g. > facebook / gmail) in Firefox. supplying the login and password > automatically at runtime - so that I can interface my code with > fingerprint recognition code. So far I have only found urllib, > urllib2 and web browser, none of which I have been able to use. > > urllib2 can log me in, but then I cant pass this authenicated login > to the browser. At the very least I need to be able to send the form > post information to the browser via my code, and preferably > auto-submit the form. You might like to look at Mechanize, which is a third-party Python project for dealing with just that sort of problem. -- Steven D'Aprano From rabidpoobear at gmail.com Sat Mar 20 01:41:20 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Fri, 19 Mar 2010 19:41:20 -0500 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> Message-ID: On Fri, Mar 19, 2010 at 3:17 PM, James Reynolds wrote: > Here's another idea I had. I thought this would be slower than then the > previous algorithm because it has another for loop and another while loop. I > read that the overhead of such loops is high, so I have been trying to avoid > using them where possible. > > def mcrange_gen(self, sample): > nx2 = self.nx1 > for q in sample: > for a in nx2: > while a > q: > pass > yield a > break > > One thing to consider is whether you need to run this simulation online or offline. That is, whether you can reorder your sample space. IFF you can reorder your sample space, then this algorithm can be extremely simplified / sped up, because then you can sort both the sample space and the binning list in numerical order Then you simply do a single pass through each list and only spend O(n+m) time to categorize your entire sample space. Sort of how a "merge" step of "merge sort" works, except rather than emitting the sort order you would just be emitting tuples with the sample value and its corresponding bin index. If this doesn't make sense let me know. If you are able to run the sim offline and reorder your sample space, and I'm not just being dumb & misunderstanding what you're trying to do (which is entirely possible), try it this way. If you have problems with my explanation, let me know and I'll try to explain it better. HTH, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Mar 20 02:07:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 20 Mar 2010 01:07:19 -0000 Subject: [Tutor] Efficiency and speed References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> Message-ID: "James Reynolds" wrote > Here's another idea I had. I thought this would be slower than then the > previous algorithm because it has another for loop and another while > loop. I > read that the overhead of such loops is high, so I have been trying to > avoid > using them where possible. Thats often true but nested loops are sometimes unavoidable. Its unnecessary nests that are really bad! But the other important rule when tuning performamce is: don't guess, measure. Put the new design in and test it to see if it runs any faster. Ultimately thats the only thing that matters (assuming its giving the correct results of course!) > def mcrange_gen(self, sample): > nx2 = self.nx1 > for q in sample: > for a in nx2: > while a > q: > pass > yield a > break You shouldn't need the break after the yield. The yield returns a value and the loop iteration ends naturally. Alan G. From stefan_ml at behnel.de Sat Mar 20 07:51:50 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 20 Mar 2010 07:51:50 +0100 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> Message-ID: James Reynolds, 19.03.2010 21:17: > Here's another idea I had. I thought this would be slower than then the > previous algorithm because it has another for loop and another while loop. I > read that the overhead of such loops is high, so I have been trying to avoid > using them where possible. Premature optimisation is the source of all evil. > def mcrange_gen(self, sample): > nx2 = self.nx1 > for q in sample: > for a in nx2: > while a> q: > pass Looks like an infinite loop to me. Stefan From davea at ieee.org Sat Mar 20 15:03:55 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 20 Mar 2010 09:03:55 -0500 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191317v61858a79l6161fbbbebfa1b38@mail.gmail.com> Message-ID: <4BA4D5CB.2010601@ieee.org> (Please don't top-post. It ruins the context for anyone else trying to follow it. Post your remarks at the end, or immediately after whatever you're commenting on.) James Reynolds wrote: > Here's another idea I had. I thought this would be slower than then the > previous algorithm because it has another for loop and another while loop. I > read that the overhead of such loops is high, so I have been trying to avoid > using them where possible. > > def mcrange_gen(self, sample): > nx2 = self.nx1 > for q in sample: > for a in nx2: > while a > q: > pass > yield a > break > > > On Fri, Mar 19, 2010 at 3:15 PM, Alan Gauld wrote: > > > While loops and for loops are not slow, it's the algorithm that you're using that's slow. If a while loop is the best way to do the best algorithm, then it's fast. Anyway, in addition to for and while, other "slow" approaches are find() and "in". But slowest of all is a loop that never terminates, like the while loop in this example. And once you fix that, the break is another problem, since it means you'll never do more than one value from sample. In your original example, you seemed to be calling a bunch of methods that are each probably a single python statement. I didn't respond to those, because I couldn't really figure what you were trying to do with them. But now I'll comment in general terms. Perhaps you should do something like: zip together the original list with a range list, so you now have a list of tuples. Then sort that new list. Now loop through that sorted list of tuples, and loop up your bucket for each item. That should be fast because they're in order, and you have the index to the original value, so you can store the bucket number somewhere useful. HTH, DaveA From beachkid at insightbb.com Sat Mar 20 17:34:01 2010 From: beachkid at insightbb.com (Ken G.) Date: Sat, 20 Mar 2010 12:34:01 -0400 Subject: [Tutor] Finding duplicates entry in file Message-ID: <4BA4F8F9.4080506@insightbb.com> What is a method I can use to find duplicated entry within a sorted numeric file? I was trying to read a file reading two lines at once but apparently, I can only read one line at a time. Can the same file be opened and read two times within a program? For example, a file has: 1 2 2 3 4 4 5 6 6 The newly revised file should be: 1 2 3 4 5 6 Again, thanking the group for their input, Ken From steve at pearwood.info Sat Mar 20 17:52:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Mar 2010 03:52:14 +1100 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: <4BA4F8F9.4080506@insightbb.com> References: <4BA4F8F9.4080506@insightbb.com> Message-ID: <201003210352.14883.steve@pearwood.info> On Sun, 21 Mar 2010 03:34:01 am Ken G. wrote: > What is a method I can use to find duplicated entry within a sorted > numeric file? > > I was trying to read a file reading two lines at once but apparently, > I can only read one line at a time. f = open("myfile") while True: first = f.readline() # Read one line. second = f.readline() # And a second. process(first) process(second) if second == '': # If the line is empty, that means we've passed the # end of the file and we can stop reading. break f.close() Or if the file is small (say, less than a few tens of megabytes) you can read it all at once into a list: lines = open("myfile").readlines() > Can the same file be opened and read two times within a program? You can do this: text1 = open("myfile").read() text2 = open("myfile").read() but why bother? That's just pointlessly wasteful. Better to do this: text1 = text2 = open("myfile").read() which is no longer wasteful, but probably still pointless. (Why do I need two names for the same text?) > For example, a file has: > > 1 > 2 > 2 > 3 > 4 > 4 > 5 > 6 > 6 > > The newly revised file should be: > > 1 > 2 > 3 > 4 > 5 > 6 Unless the file is huge, something like this should do: # Untested lines = open("myfile").readlines() f = open("myfile", "w") previous_line = None for line in lines: if line != previous_line: f.write(line) previous_line = line f.close() -- Steven D'Aprano From steve at pearwood.info Sat Mar 20 17:58:08 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Mar 2010 03:58:08 +1100 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> Message-ID: <201003210358.08411.steve@pearwood.info> On Sat, 20 Mar 2010 03:41:11 am James Reynolds wrote: > I've still been working towards learning the language, albeit slowly > and I've been working on a project that is somewhat intense on the > numerical calculation end of things. > > Running 10,000 trials takes about 1.5 seconds and running 100,000 > trials takes 11 seconds. Running a million trials takes about 81 > seconds or so. I don't think 1M trials is needed for accuracy, but > 100K probably is. That's 15 microseconds per trial. Why do you think that's necessarily slow? How much work are you doing per trial? > I've made a few other optimizations today that I won't be able to > test until I get home, but I was wondering if any of you could give > some general pointers on how to make python run a little more > quickly. Read this: http://www.python.org/doc/essays/list2str.html Google on "Python optimizations". And you can watch this talk from the recent Python conference: http://pycon.blip.tv/file/3322261/ Unfortunately the video is only available as a 1GB file, so I haven't watched it myself. But if you have a fast Internet link with lots of quota, go ahead. [snip] > def mcrange_gen(self, sample):#, lensample): > lensample = len(sample) # this section is just for speed. All of > > these are renames from the globals to bring calc times down at the > > expense of memory. I haven't tested these yet. > > nx2 = self.nx1 > nx2_append = nx2.append > nx2_sort = nx2.sort > nx2_reverse = nx2.reverse > nx2_index = nx2.index > nx2_remove = nx2.remove All this becomes white noise. That's the problem with optimizations, particularly micro-optimizations: so often they make the code less readable. Unless you have profiled your code and determined that these micro-optimizations make a significant difference, I'd say drop them, they're not worth it. This sort of micro-optimization is the last thing you should be adding. > for s in range(lensample): > q = sample[s] #takes the next randomly generated number from > > the sample list > nx2_append(q) # and appends it to nx list. > nx2_sort() # and sorts it in place > nx2_reverse() # reverses the list, because this was the > > original order > i = nx2_index(q) #get the index of that element > nx2_remove(q) # and remove the element. > yield i # send the position of that element back to the main > > program. Your naming conventions are ... weird. "s" for an integer index, "q" for a sample -- what's with that? And what's nx1 and nx2? Writing comments is a good thing, but only when the comments actually add something to the code. Nearly every one of the above comments is useless. For instance, you write: nx2_append(q) # and appends it to nx list. Really? Calling "append" appends? Who would have guessed! nx2_sort() # and sorts it in place Sort sorts. Wow. I trust I've made my point. There's no need to repeat the code in plain English as a comment. It just becomes noise. Since s is never used except to get the sample, I'd do this: nx = self.nx1 # This is more a typing optimization than for speed. for q in sample: nx.append(q) nx.sort(reverse=True) i = nx.index(q) nx.remove(q) yield i Now, let's look at what happens in the first four lines of the loop. Appending to a list is fast. You almost certainly don't need to care about that. Sorting is fast-ish. Python's sort is amazingly fast, as far as sorts go, but sorting is a naturally expensive operation, and you don't sort the data once, you sort it every time through the loop. Tick this as something that *maybe* you want to get rid of. index() is an O(N) operation -- slow but not painfully slow. Unless your list is short, this is another "maybe" to remove. remove() is another expensive operation. Unless your list is very short, this is something else you want to avoid when possible. -- Steven D'Aprano From beachkid at insightbb.com Sat Mar 20 18:06:25 2010 From: beachkid at insightbb.com (Ken G.) Date: Sat, 20 Mar 2010 13:06:25 -0400 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: <201003210352.14883.steve@pearwood.info> References: <4BA4F8F9.4080506@insightbb.com> <201003210352.14883.steve@pearwood.info> Message-ID: <4BA50091.9020300@insightbb.com> Thanks! You gave me something to do for the rest of the afternoon. Ken Steven D'Aprano wrote: > On Sun, 21 Mar 2010 03:34:01 am Ken G. wrote: > >> What is a method I can use to find duplicated entry within a sorted >> numeric file? >> >> I was trying to read a file reading two lines at once but apparently, >> I can only read one line at a time. >> > > f = open("myfile") > while True: > first = f.readline() # Read one line. > second = f.readline() # And a second. > process(first) > process(second) > if second == '': > # If the line is empty, that means we've passed the > # end of the file and we can stop reading. > break > f.close() > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Mar 20 18:17:15 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Mar 2010 04:17:15 +1100 Subject: [Tutor] Efficiency and speed In-Reply-To: <98c3f7c51003191147v758acfb7u6a8942c2c4e838ca@mail.gmail.com> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191147v758acfb7u6a8942c2c4e838ca@mail.gmail.com> Message-ID: <201003210417.15859.steve@pearwood.info> On Sat, 20 Mar 2010 05:47:45 am James Reynolds wrote: > This is a monte-carlo simulation. > > The simulation measures the expiration of something and those > somethings fall into bins that are not evenly dispersed. These bins > are stored in the nx list mentioned previously. > > So let's say you have the bins, a, b,c,d,e,f and you have the value z > from the sample list where z >b and <= a. In this case, it should > return the index value at position (a). I'm not sure I understand completely. An example might help. I *think* you have a list like this: nx = [10.0, 9.0, 7.0, 3.0, 2.0, 1.0] and if you have a value like z = 9.8 you want to return the index 0. Correct? That seems a bit funny. In my experience it is normal to have the bins in the opposite direction. I suppose it probably doesn't matter that much, but it does seem a bit unusual. If nx is fairly short (say, less than 40 or 50 items), then the fastest way is probably a linear search, something like this: def search_bins(nx, z): """Search bins nx for item z. >>> bins = [5.0, 4.0, 2.0, 1.0] >>> search_bins(bins, 1.2) 2 If z is not in the bins, returns -1: >>> search_bins(bins, 5.1) -1 """ for i, value in enumerate(nx): if z > value: return i-1 return -1 -- Steven D'Aprano From karim.liateni at free.fr Sat Mar 20 20:00:15 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Sat, 20 Mar 2010 20:00:15 +0100 Subject: [Tutor] Tutorial executable from python script. In-Reply-To: References: <4BA3CBED.6070509@free.fr> Message-ID: <4BA51B3F.9050606@free.fr> Hello Alan, In fact, I want to be sure the users can run it on every machine in our network. Especially, I want to be able to run it on Solaris 5.8 with python 1.5 (Unix machine). I wanted to know if I could make some custom executable like in C when you want to build a executable with a static library to be sure if the system does not have the correct shares libraries. Perhaps the better is to build a python version embedded in my application installation. Do you have any examples or tutorial on how integrate python inside a pplication to be sure that we have all in one and not depand on any local machine installation environment. I need as to embed gtk python library for graphical use. Thanks Karim Alan Gauld wrote: > > "Karim Liateni" wrote > >> on machine which doesn't have recent version (2.0) of python. > > Given that v2 is at least 10 years old now that's not really "recent" > I'd be surprised if any current Linux distros had anything that old on > them! Even the ones designed for old hardware. > In fact, are you sure your code runs on v1 python? There have been a > lot of language changes since then. > >> the compile() >> method but how can I use it to make all in one executable > > compile() doesn't make an exe, it compiles python script into python > byte code - much as the javac compiler compiles java source into java > bytecode. > >> run on old system (old python). > > Exactly how old is old? > > If you write the code to run on an old Python interpreter it should > still work(mostly!) on Python 2.6. The trick is not to compile the > code but to write code that is consistent wityh the oldest version of > Python you need to run on. > > HTH, > From rabidpoobear at gmail.com Sat Mar 20 22:02:48 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 20 Mar 2010 16:02:48 -0500 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: <4BA4F8F9.4080506@insightbb.com> References: <4BA4F8F9.4080506@insightbb.com> Message-ID: On Sat, Mar 20, 2010 at 11:34 AM, Ken G. wrote: > What is a method I can use to find duplicated entry within a sorted numeric > file? > I was trying to read a file reading two lines at once but apparently, I can > only read one line at a time. Can the same file be opened and read two > times within a program? > > For example, a file has: > > 1 > 2 > 2 > 3 > 4 > 4 > 5 > 6 > 6 > > The newly revised file should be: > > 1 > 2 > 3 > 4 > 5 > 6 > > Again, thanking the group for their input, > > One-liner: open("output.txt", "w").write("\n".join(sorted(set([i.strip() for i in open("input.txt")])))) Just for fun :) also, in your algorithm, why are you assuming there are at most 1 extra entries in the case of a duplicate? Why not generalize it for all duplicates? -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From beachkid at insightbb.com Sat Mar 20 22:50:08 2010 From: beachkid at insightbb.com (Ken G.) Date: Sat, 20 Mar 2010 17:50:08 -0400 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: References: <4BA4F8F9.4080506@insightbb.com> Message-ID: <4BA54310.6090204@insightbb.com> Thanks for the info. I already adopted a program from another person and it works like a charm. As for your question, I had no idea of if I had duplicate or more as there was some 570 line items. I whittled it down to 370 line entries. Whew. Ken Luke Paireepinart wrote: > > > On Sat, Mar 20, 2010 at 11:34 AM, Ken G. > wrote: > > What is a method I can use to find duplicated entry within a > sorted numeric file? > I was trying to read a file reading two lines at once but > apparently, I can only read one line at a time. Can the same file > be opened and read two times within a program? > > For example, a file has: > > 1 > 2 > 2 > 3 > 4 > 4 > 5 > 6 > 6 > > The newly revised file should be: > > 1 > 2 > 3 > 4 > 5 > 6 > > Again, thanking the group for their input, > > > One-liner: > open("output.txt", "w").write("\n".join(sorted(set([i.strip() for i in > open("input.txt")])))) > > > Just for fun :) > > also, in your algorithm, why are you assuming there are at most 1 > extra entries in the case of a duplicate? Why not generalize it for > all duplicates? > -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Sat Mar 20 23:10:52 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 20 Mar 2010 17:10:52 -0500 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: <4BA54310.6090204@insightbb.com> References: <4BA4F8F9.4080506@insightbb.com> <4BA54310.6090204@insightbb.com> Message-ID: On Sat, Mar 20, 2010 at 4:50 PM, Ken G. wrote: > Thanks for the info. I already adopted a program from another person and > it works like a charm. As for your question, I had no idea of if I had > duplicate or more as there was some 570 line items. I whittled it down to > 370 line entries. Whew. > Can you please try to post to the list in plain-text rather than HTML? it is very hard to read your font on my system. Thanks, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From beachkid at insightbb.com Sun Mar 21 03:41:55 2010 From: beachkid at insightbb.com (Ken G.) Date: Sat, 20 Mar 2010 22:41:55 -0400 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: References: <4BA4F8F9.4080506@insightbb.com> <4BA54310.6090204@insightbb.com> Message-ID: <4BA58773.3080803@insightbb.com> Thanks for letting me know. Corrective actions taken. Ken Luke Paireepinart wrote: > > > On Sat, Mar 20, 2010 at 4:50 PM, Ken G. > wrote: > > Thanks for the info. I already adopted a program from another > person and it works like a charm. As for your question, I had no > idea of if I had duplicate or more as there was some 570 line > items. I whittled it down to 370 line entries. Whew. > > > Can you please try to post to the list in plain-text rather than > HTML? it is very hard to read your font on my system. > > Thanks, > -Luke From steve at pearwood.info Sun Mar 21 04:20:11 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Mar 2010 14:20:11 +1100 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: References: <4BA4F8F9.4080506@insightbb.com> <4BA54310.6090204@insightbb.com> Message-ID: <201003211420.11575.steve@pearwood.info> On Sun, 21 Mar 2010 09:10:52 am Luke Paireepinart wrote: > On Sat, Mar 20, 2010 at 4:50 PM, Ken G. wrote: > > Thanks for the info. I already adopted a program from another > > person and it works like a charm. As for your question, I had no > > idea of if I had duplicate or more as there was some 570 line > > items. I whittled it down to 370 line entries. Whew. > > Can you please try to post to the list in plain-text rather than > HTML? it is very hard to read your font on my system. What client are you using? Ken's post includes both a plain text part and a HTML part. Any decent mail client I know of gives you the choice of which to view. -- Steven D'Aprano From rabidpoobear at gmail.com Sun Mar 21 05:00:03 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 20 Mar 2010 23:00:03 -0500 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: <201003211420.11575.steve@pearwood.info> References: <4BA4F8F9.4080506@insightbb.com> <4BA54310.6090204@insightbb.com> <201003211420.11575.steve@pearwood.info> Message-ID: On Sat, Mar 20, 2010 at 10:20 PM, Steven D'Aprano wrote: > On Sun, 21 Mar 2010 09:10:52 am Luke Paireepinart wrote: > > On Sat, Mar 20, 2010 at 4:50 PM, Ken G. > wrote: > > > Thanks for the info. I already adopted a program from another > > > person and it works like a charm. As for your question, I had no > > > idea of if I had duplicate or more as there was some 570 line > > > items. I whittled it down to 370 line entries. Whew. > > > > Can you please try to post to the list in plain-text rather than > > HTML? it is very hard to read your font on my system. > > > What client are you using? Ken's post includes both a plain text part > and a HTML part. Any decent mail client I know of gives you the choice > of which to view. > > gmail auto-selects html, apparently. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Sun Mar 21 09:19:17 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 21 Mar 2010 19:19:17 +1100 Subject: [Tutor] Tutorial executable from python script. In-Reply-To: <4BA51B3F.9050606@free.fr> References: <4BA3CBED.6070509@free.fr> <4BA51B3F.9050606@free.fr> Message-ID: On 03/21/2010 06:00 AM, Karim Liateni wrote: > > Hello Alan, > > In fact, I want to be sure the users can run it on every machine in our > network. > Especially, I want to be able to run it on Solaris 5.8 with python 1.5 > (Unix machine). > I wanted to know if I could make some custom executable like in C when > you want > to build a executable with a static library to be sure if the system > does not have > the correct shares libraries. If you know that the machine contains `python` (whatever the version is) you can use sys.version to check the system python's version. It can be as simple as: import sys if int(sys.version[0]) > 1 or (int(sys.version[0]) == 1 and int(sys.version[2] >= 5)): # or you can start a subprocess instead, # abusing import makes "if __name__ == '__main__':" magic not work import MyMainProgram else: # parentheses guards for python 3 print ('script is only compatible with python version 1.5 and above') Otherwise, if you cannot even rely on python being available, you may need to use shell script. > Perhaps the better is to build a python version embedded in my > application installation. > Do you have any examples or tutorial on how integrate python inside a > pplication to be > sure that we have all in one and not depand on any local machine > installation environment. > I need as to embed gtk python library for graphical use. That is indeed possible, however is there any reason why the server don't upgrade its python version? CPython makes it easy to do parallel installation of two different python version. If you can persuade the machine administrator to install python2.6 as an altinstall; you can simply change the hashbang line of your script to "#!/usr/bin/env python2.6" and all is well. From mhw at doctors.net.uk Sun Mar 21 09:20:25 2010 From: mhw at doctors.net.uk (mhw at doctors.net.uk) Date: Sun, 21 Mar 2010 08:20:25 +0000 Subject: [Tutor] Finding duplicates entry in file In-Reply-To: References: <4BA4F8F9.4080506@insightbb.com> <4BA54310.6090204@insightbb.com> <201003211420.11575.steve@pearwood.info> Message-ID: <264784983-1269159654-cardhu_decombobulator_blackberry.rim.net-1986902269-@bda188.bisx.produk.on.blackberry> Plain text definitely better for small devices - I read the tutor list on my blackberry on the way to work (and hence Top -p - apologies). Matt Sent from my BlackBerry? wireless device -----Original Message----- From: Luke Paireepinart Date: Sat, 20 Mar 2010 23:00:03 To: Steven D'Aprano Cc: Subject: Re: [Tutor] Finding duplicates entry in file _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From norman at khine.net Sun Mar 21 10:09:29 2010 From: norman at khine.net (Norman Khine) Date: Sun, 21 Mar 2010 10:09:29 +0100 Subject: [Tutor] using pythnn to open a password protected website In-Reply-To: <201003201127.26289.steve@pearwood.info> References: <201003201127.26289.steve@pearwood.info> Message-ID: <9c2c8ffb1003210209q718b428bldcc2324614bf7e93@mail.gmail.com> On Sat, Mar 20, 2010 at 1:27 AM, Steven D'Aprano wrote: > On Fri, 19 Mar 2010 11:33:36 pm richard west wrote: >> Hi, >> >> Im trying to use python to open up a password protected website(e.g. >> facebook / gmail) in Firefox. supplying the login and password >> automatically at runtime - so that I can interface my code with >> fingerprint recognition code. So far I have only found urllib, >> urllib2 and web browser, none of which I have been able to use. >> >> urllib2 can log me in, but then I cant pass this authenicated login >> to the browser. At the very least I need to be able to send the form >> post information to the browser via my code, and preferably >> auto-submit the form. You can also use twill http://twill.idyll.org/ > > You might like to look at Mechanize, which is a third-party Python > project for dealing with just that sort of problem. > > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From karim.liateni at free.fr Sun Mar 21 10:51:31 2010 From: karim.liateni at free.fr (Karim Liateni) Date: Sun, 21 Mar 2010 10:51:31 +0100 Subject: [Tutor] Tutorial executable from python script. In-Reply-To: References: <4BA3CBED.6070509@free.fr> <4BA51B3F.9050606@free.fr> Message-ID: <4BA5EC23.1070302@free.fr> Hello Lie, Thanks for your advices. To have correct updates from ITs is really a true pain. The network is worldwide in our company. I found issues having decent version. On my local workstation I have Python v1.5, on compute farm LSF machines sometimes 2.2 , 2.3, 2.6. That's why I don't want to rely on machine installation and provide a unique version with my application installation. I know we did the same for TCL to be sure to have 8.4 version. I just wanted to know if there is some tutos about this topic. Regards Karim Lie Ryan wrote: > On 03/21/2010 06:00 AM, Karim Liateni wrote: > >> Hello Alan, >> >> In fact, I want to be sure the users can run it on every machine in our >> network. >> Especially, I want to be able to run it on Solaris 5.8 with python 1.5 >> (Unix machine). >> I wanted to know if I could make some custom executable like in C when >> you want >> to build a executable with a static library to be sure if the system >> does not have >> the correct shares libraries. >> > > If you know that the machine contains `python` (whatever the version is) > you can use sys.version to check the system python's version. It can be > as simple as: > > import sys > if int(sys.version[0]) > 1 or > (int(sys.version[0]) == 1 and int(sys.version[2] >= 5)): > # or you can start a subprocess instead, > # abusing import makes "if __name__ == '__main__':" magic not work > import MyMainProgram > else: > # parentheses guards for python 3 > print ('script is only compatible with python version 1.5 and above') > > Otherwise, if you cannot even rely on python being available, you may > need to use shell script. > > >> Perhaps the better is to build a python version embedded in my >> application installation. >> Do you have any examples or tutorial on how integrate python inside a >> pplication to be >> sure that we have all in one and not depand on any local machine >> installation environment. >> I need as to embed gtk python library for graphical use. >> > > That is indeed possible, however is there any reason why the server > don't upgrade its python version? CPython makes it easy to do parallel > installation of two different python version. If you can persuade the > machine administrator to install python2.6 as an altinstall; you can > simply change the hashbang line of your script to "#!/usr/bin/env > python2.6" and all is well. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From lie.1296 at gmail.com Sun Mar 21 16:01:02 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 22 Mar 2010 02:01:02 +1100 Subject: [Tutor] Tutorial executable from python script. In-Reply-To: <4BA5EC23.1070302@free.fr> References: <4BA3CBED.6070509@free.fr> <4BA51B3F.9050606@free.fr> <4BA5EC23.1070302@free.fr> Message-ID: On 03/21/2010 08:51 PM, Karim Liateni wrote: > > Hello Lie, > > Thanks for your advices. > > To have correct updates from ITs is really a true pain. The network > is worldwide in our company. I found issues having decent version. > On my local workstation I have Python v1.5, on compute farm LSF > machines sometimes 2.2 , 2.3, 2.6. That's why I don't want to rely > on machine installation and provide a unique version with my > application installation. I know we did the same for TCL to be sure > to have 8.4 version. I just wanted to know if there is some tutos about > this topic. As Alan have said, if you target your script for python 1.5, it is quite likely that your script will still run in python 2.6. Can you describe what's your deployment strategy? Do you have an automated deployment or do you do it manually? How important is it for all computer in the network to have identical version of your script? Do all your servers have the GNU toolchain? How do the servers varies? Do they all have the same/similar hardware? Do they have a certain common subset of software? Can you give an estimate of the number of servers in the network? In general, it is impossible to have a super-package that can be deployed uniformly if your servers varies too widely. For example if most of your server uses x86 but several uses PowerPC, you will have to include different binaries for them. Or if there is a Windows-based server. The complexity of having such generic super-packager is similar to what the automake toolchain faces (in short, extremely complex). If you can guarantee that all your server contains 'python' however old it is; you can write the version checking script from the previous post. If you take some care, the version checking script should run in all python version from the very ancient to the latest. If you can guarantee that all your servers run certain version of a type of Unix, you may be able to pre-compile python in one of the machine and use this pre-compiled version on all machine. If you have to follow the local machine directory conventions (e.g. some servers want your script in /bin while the other in /usr/bin) you may need to write a script that modify the hashbang line appropriately. If you can guarantee that your servers have GNU toolchain, you can write a shell script to check the system python's version and download and compile python from source if the system's python version doesn't match. From eire1130 at gmail.com Mon Mar 22 03:18:32 2010 From: eire1130 at gmail.com (James Reynolds) Date: Sun, 21 Mar 2010 22:18:32 -0400 Subject: [Tutor] Efficiency and speed In-Reply-To: <201003210417.15859.steve@pearwood.info> References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com> <98c3f7c51003191147v758acfb7u6a8942c2c4e838ca@mail.gmail.com> <201003210417.15859.steve@pearwood.info> Message-ID: <98c3f7c51003211918l21fa99dav30c0ca64d57fd59d@mail.gmail.com> On Sat, Mar 20, 2010 at 1:17 PM, Steven D'Aprano wrote: > On Sat, 20 Mar 2010 05:47:45 am James Reynolds wrote: > > > This is a monte-carlo simulation. > > > > The simulation measures the expiration of something and those > > somethings fall into bins that are not evenly dispersed. These bins > > are stored in the nx list mentioned previously. > > > > So let's say you have the bins, a, b,c,d,e,f and you have the value z > > from the sample list where z >b and <= a. In this case, it should > > return the index value at position (a). > > I'm not sure I understand completely. An example might help. I *think* > you have a list like this: > > nx = [10.0, 9.0, 7.0, 3.0, 2.0, 1.0] > > and if you have a value like z = 9.8 you want to return the index 0. > Correct? > > That seems a bit funny. In my experience it is normal to have the bins > in the opposite direction. I suppose it probably doesn't matter that > much, but it does seem a bit unusual. > > If nx is fairly short (say, less than 40 or 50 items), then the fastest > way is probably a linear search, something like this: > > def search_bins(nx, z): > """Search bins nx for item z. > > >>> bins = [5.0, 4.0, 2.0, 1.0] > >>> search_bins(bins, 1.2) > 2 > > If z is not in the bins, returns -1: > > >>> search_bins(bins, 5.1) > -1 > > """ > for i, value in enumerate(nx): > if z > value: > return i-1 > return -1 > > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Firstly, Thank you to everyone who responded to this thread. I learned some very important lessons, some of which I've been working on over the weekend. I apologize for not responding sooner. I tinkered around with the aforementioned algorithm and I ended up using the bisect that Emilie showed earlier. I did end up making the nested for loop work earlier using an if statement, but the bisect was much more efficient (.24 seconds running 100K trials), which is a major improvement. I've just started using profiler this weekend, and It's giving me some incite into how the language functions. On that end, I'm almost done readying "beginning Python: From Novice to Professional" Can anyone recommend anything else for me to read after that? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Mar 22 11:45:03 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Mar 2010 10:45:03 -0000 Subject: [Tutor] Efficiency and speed References: <98c3f7c51003190941y6a772547j3b8d4f801ec61b4b@mail.gmail.com><98c3f7c51003191147v758acfb7u6a8942c2c4e838ca@mail.gmail.com><201003210417.15859.steve@pearwood.info> <98c3f7c51003211918l21fa99dav30c0ca64d57fd59d@mail.gmail.com> Message-ID: "James Reynolds" wrote > On that end, I'm almost done readying "beginning Python: From Novice to > Professional" Can anyone recommend anything else for me to read after > that? I'm not familiar with that book but I'd say consider what area of programming you are interested in and get a specialist title on that. For example a GUI Framework, Networking, Text processing, XML, Web frameworks, Win32, all have specialist titles to choose from. There are probably more too, those are just a few I have looked at. And finally, read code. Find a real-world project (try Source Forge) and grab the source code and read it, understand it and modify it. You could even try the standard library modules as a starter. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From laura_casta21 at hotmail.com Mon Mar 22 23:31:58 2010 From: laura_casta21 at hotmail.com (=?iso-8859-1?B?bGF1cmEgY2FzdGHxZWRh?=) Date: Mon, 22 Mar 2010 22:31:58 +0000 Subject: [Tutor] Python help please Message-ID: Hi my name is Laura and im currently trying to solve one of the challenges in the book: "Python Programming, second edition" by Michael Dawson... I'm stuck in the 5 chapter because of this challenge, im the kinda person who dont give up so i have to solve this challenge or else i cant move on its just getting really frustrating and i was wondering if u can help me with it. Right now im trying to teach my self programming and i found the book really useful but this challenge is messing with me haha well is the second challenge at the end of the 5 chapter i hope its not to much problem for u and i'd really appreciated if u help me. The challenge: "Write a character creator program for a role-playing game. the player should be given a pool of 30 point to spend on four attributes: Strength, wisdom, health 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." Thank you so much and please please help me.. this is what im thinking to do so far maybe we can work with it in the interface i want to print "Welcome to C.C." so print character, be able to change the name, change the strength (something like Strength:___ would you like to add/sub [+/-]), change health, change wisdom, change dex, and points remaining...... and in the code i was thinking to start with a "while loop" .....this is what i have so far and honestly im stuck i dont now how to start please help me thank you. Laura _________________________________________________________________ Connect to the next generation of MSN Messenger? http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Mar 23 01:13:57 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 23 Mar 2010 00:13:57 -0000 Subject: [Tutor] Python help please References: Message-ID: "laura casta?eda" wrote > Hi my name is Laura and im currently trying to solve one of the > challenges in the book: "Python Programming, ... > if u can help me with it. Right now im trying to teach my self > programming and i found the book really useful Hi feel free to sitgn up to the tutor list, that way your posts will go through a lot quicker - and the answers will therefore come back quicker too :-) > ...in the code i was thinking to start with a "while loop" Your ideas are all good. Get the data structures/variable set up, then iterate in a while loop adding and subtracting points from the pool to the character. Its a little like double entry book-keeping - for every addition there must be a corresponding subtraction and vice versa If you have a list of characters you will need a corresponding list of pools - or associate the pools with the characters - a dictionary maybe? Start with one character and try getting the most basic version running. If you get stuck show us the code plus any error messages and ask... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jojo.mwebaze at gmail.com Tue Mar 23 02:16:03 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Tue, 23 Mar 2010 02:16:03 +0100 Subject: [Tutor] parsing pyc files Message-ID: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> Hello Tutor I have two problems, any help will be highly appreciated. How is possible to trace the all method calls, object instantiations, variables used in running an experiment dynamically, without putting print - or log statements in my code? - some sort of provenance! I would like to create a tool that can look into pyc files to find classes/methods that was executed without looking the the source code. Is this possible in python. Kind Regards Jojo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Mar 23 10:27:48 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 23 Mar 2010 09:27:48 -0000 Subject: [Tutor] parsing pyc files References: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> Message-ID: "Jojo Mwebaze" wrote > How is possible to trace the all method calls, object instantiations, > variables used in running an experiment dynamically, without putting > print > - or log statements in my code? - some sort of provenance! There are several debuggers for Python including the pdb module in the standard library and the "graphical" one in IDLE. > I would like to create a tool that can look into pyc files to find > classes/methods that was executed without looking the the source code. Is > this possible in python. I don't know of a debugger for the bytecode. There are tools to generate it for a specific function so you can see what it looks like, but I don't know of any that can dynamically monitor execution. The profiler can also tell you what was executed after the fact. This is an unusual request can I ask why you need to do that? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jojo.mwebaze at gmail.com Tue Mar 23 12:01:11 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Tue, 23 Mar 2010 12:01:11 +0100 Subject: [Tutor] parsing pyc files In-Reply-To: References: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> Message-ID: <3124be321003230401kba41ec0ia1b78e6778b04be4@mail.gmail.com> Researchers at our university are allowed to checkout code from CVS, make modifications, change variables/parameters and run experiments.. After experiments are run, results are published. (However we don't allow them to commit the changes, till changes are approved) take an example, two researchers can run two experiments on the same data set but get different results depending on what someone did/changed. So the problem is how to compare both results, We need to know how the results were generated. e.g methods invoked, parameters/variables passed to that method, and probably changes made to the classes and probably store this information as part of the data. cheers Jojo On Tue, Mar 23, 2010 at 10:27 AM, Alan Gauld wrote: > > "Jojo Mwebaze" wrote > > > How is possible to trace the all method calls, object instantiations, >> variables used in running an experiment dynamically, without putting >> print >> - or log statements in my code? - some sort of provenance! >> > > There are several debuggers for Python including the pdb > module in the standard library and the "graphical" one in IDLE. > > > I would like to create a tool that can look into pyc files to find >> classes/methods that was executed without looking the the source code. Is >> this possible in python. >> > > I don't know of a debugger for the bytecode. > There are tools to generate it for a specific function so you can > see what it looks like, but I don't know of any that can dynamically > monitor execution. > > The profiler can also tell you what was executed after the fact. > > This is an unusual request can I ask why you need to do that? > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Mar 23 12:37:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Mar 2010 22:37:46 +1100 Subject: [Tutor] parsing pyc files In-Reply-To: <3124be321003230401kba41ec0ia1b78e6778b04be4@mail.gmail.com> References: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> <3124be321003230401kba41ec0ia1b78e6778b04be4@mail.gmail.com> Message-ID: <201003232237.46436.steve@pearwood.info> On Tue, 23 Mar 2010 10:01:11 pm Jojo Mwebaze wrote: > Researchers at our university are allowed to checkout code from CVS, > make modifications, change variables/parameters and run experiments.. > After experiments are run, results are published. (However we don't > allow them to commit the changes, till changes are approved) > > take an example, two researchers can run two experiments on the same > data set but get different results depending on what someone > did/changed. > > So the problem is how to compare both results, We need to know how > the results were generated. e.g methods invoked, parameters/variables > passed to that method, and probably changes made to the classes and > probably store this information as part of the data. Then ask the scientists for the source code. If you suspect that they will give you a version of code which is different from the version they actually ran, then run it on the same data. If it doesn't give the exact same results, send it back with this link: http://en.wikipedia.org/wiki/Scientific_misconduct -- Steven D'Aprano From steve at pearwood.info Tue Mar 23 12:44:02 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Mar 2010 22:44:02 +1100 Subject: [Tutor] parsing pyc files In-Reply-To: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> References: <3124be321003221816i37bb9cb5hfd8e5c6a19bc6b06@mail.gmail.com> Message-ID: <201003232244.02324.steve@pearwood.info> On Tue, 23 Mar 2010 12:16:03 pm Jojo Mwebaze wrote: > Hello Tutor > > I have two problems, any help will be highly appreciated. > > How is possible to trace the all method calls, object instantiations, > variables used in running an experiment dynamically, without putting > print - or log statements in my code? - some sort of provenance! Look at the profile module for starters. >>> def test(x): ... y = -42 ... for i in xrange(10000): ... y += func(i) ... return x + y ... >>> def func(x): ... return x ... >>> >>> import profile >>> profile.run("test(3)") 10004 function calls in 0.286 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.002 0.002 0.002 0.002 :0(setprofile) 10000 0.145 0.000 0.145 0.000 :1(func) 1 0.139 0.139 0.284 0.284 :1(test) 1 0.000 0.000 0.284 0.284 :1() 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 0.286 0.286 profile:0(test(3)) > I would like to create a tool that can look into pyc files to find > classes/methods that was executed without looking the the source > code. Is this possible in python. Look at the dis module. >>> import dis >>> dis.dis(test) 2 0 LOAD_CONST 1 (-42) 3 STORE_FAST 1 (y) 3 6 SETUP_LOOP 36 (to 45) 9 LOAD_GLOBAL 0 (xrange) 12 LOAD_CONST 2 (10000) 15 CALL_FUNCTION 1 18 GET_ITER >> 19 FOR_ITER 22 (to 44) 22 STORE_FAST 2 (i) 4 25 LOAD_FAST 1 (y) 28 LOAD_GLOBAL 1 (func) 31 LOAD_FAST 2 (i) 34 CALL_FUNCTION 1 37 INPLACE_ADD 38 STORE_FAST 1 (y) 41 JUMP_ABSOLUTE 19 >> 44 POP_BLOCK 5 >> 45 LOAD_FAST 0 (x) 48 LOAD_FAST 1 (y) 51 BINARY_ADD 52 RETURN_VALUE -- Steven D'Aprano From prasadaraon50 at gmail.com Tue Mar 23 15:28:53 2010 From: prasadaraon50 at gmail.com (prasad rao) Date: Tue, 23 Mar 2010 19:58:53 +0530 Subject: [Tutor] parsing pyc files Message-ID: <9e3fac841003230728u2f71bce4ndadd6456686f0b84@mail.gmail.com> Hi . If you want to see what is in a pyc file,just type od pycfile at bash command prompt. You can see only rows of numbers. From denis.spir at gmail.com Tue Mar 23 18:02:36 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 23 Mar 2010 18:02:36 +0100 Subject: [Tutor] Python help please In-Reply-To: References: Message-ID: <20100323180236.55e33f8a@o> On Mon, 22 Mar 2010 22:31:58 +0000 laura casta?eda wrote: > > Hi my name is Laura and im currently trying to solve one of the > challenges in the book: "Python Programming, second edition" by Michael > Dawson... I'm stuck in the 5 chapter because of this challenge, im the > kinda person who dont give up so i have to solve this challenge or else > i cant move on its just getting really frustrating and i was wondering > if u can help me with it. Right now im trying to teach my self > programming and i found the book really useful but this challenge is > messing with me haha well is the second challenge at the end of the 5 > chapter i hope its not to much problem for u and i'd really appreciated if u help me. > > The challenge: "Write a character creator program for a role-playing game. the player should be given a pool of 30 point to spend on four attributes: Strength, wisdom, health 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." > Thank you so much and please please help me.. this is what im thinking to do so far maybe we can work with it > > in the interface i want to print "Welcome to C.C." > so print character, be able to change the name, change the strength (something like Strength:___ would you like to add/sub [+/-]), change health, change wisdom, change dex, and points remaining...... and in the code i was thinking to start with a "while loop" .....this is what i have so far and honestly im stuck i dont now how to start please help me thank you. > > Laura I would just have a dict with entries for each trait. And a func changePointDispatch(trait, number, sign) Sign is here a flag saying more/less. If more, check there is enough in pool, else send error message, remove it and add to the trait. If less,... The loop, as you say, just repetitively asks for a command and calls the func, until a code saying bast?! Maybe you have better ideas. Object-orientation would be nice but seems a bit overkill here (just an opinion). The design side of the problem is to get a practicle & friendly interface to the user. Denis ________________________________ vit esse estrany ? spir.wikidot.com From denis.spir at gmail.com Tue Mar 23 19:33:42 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 23 Mar 2010 19:33:42 +0100 Subject: [Tutor] func headline & module inspect Message-ID: <20100323193342.6b734c75@o> Hello, I was looking for a way to get info about func definition, esp. its param list. The aim beeing to reproduce more or less its header line. Found useful hints at http://stackoverflow.com/questions/582056/getting-list-of-parameters-inside-python-function. Example: ================================ import inspect def func(a,b,c, d=4,e=5, *items,**params): pass argSpec = inspect.getargspec(func) print argSpec print inspect.formatargspec(argSpec) print inspect.getsourcelines(func) ==> """ ArgSpec(args=['a', 'b', 'c', 'd', 'e'], varargs='items', keywords='params', defaults=(4, 5)) ((a, b, c, d, e), items, params, (4, 5)) (['def func(a,b,c, d=4,e=5, *items,**params):\n', ' pass\n'], 8) """ =============================== How does inspect do this magic? Also: print dir(argSpec) # --> [... 'args', 'count', 'defaults', 'index', 'keywords', 'varargs'] What are (the methods) count and index supposed to provide? doc: ------------------------- inspect.getargspec(func)? Get the names and default values of a Python function?s arguments. A tuple of four things is returned: (args, varargs, varkw, defaults). args is a list of the argument names (it may contain nested lists). varargs and varkw are the names of the * and ** arguments or None. defaults is a tuple of default argument values or None if there are no default arguments; if this tuple has n elements, they correspond to the last n elements listed in args. Changed in version 2.6: Returns a named tuple ArgSpec(args, varargs, keywords, defaults). ------------------------- Finally, I found extremely complicated to rebuild the func headline: ================================ def namedArguments(argNames,defaultValues): (s1,s2) = (len(argNames),len(defaultValues)) delta = s1 - s2 defaultExpr = lambda i: '' if i<0 or i>=s2 else "=%s" %(defaultValues[i]) return ', '.join( "%s%s" %(argNames[i],defaultExpr(i-delta)) for i in range(s1) ) def headLine(f): funcName = f.__name__ argNames, varargName, kwargName, defaultValues = inspect.getargspec(f) namedArgs = namedArguments(argNames,defaultValues) varargName = '' if varargName is None else ", *%s" %varargName kwargName = '' if kwargName is None else ", **%s" %kwargName return "%s(%s%s%s)" %(funcName,namedArgs,varargName,kwargName) print headLine(func) # ==> func(a, b, c, d=4, e=5, *items, **params) ================================ Maybe I'm overlooking an easier way to do that? (And why not built in the module inspect?) Denis ________________________________ vit esse estrany ? spir.wikidot.com From rarmstro at water.ca.gov Tue Mar 23 20:16:21 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Tue, 23 Mar 2010 12:16:21 -0700 Subject: [Tutor] How to add a description to a module Message-ID: Hello, I have a simple question. I have created a module call Newmark.py and it runs fine, however, I want to add some documentation so that when I type in the console help(Newmark) it will give a description of the module. Any ideas? Thanks, Richie -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Tue Mar 23 20:30:22 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 23 Mar 2010 20:30:22 +0100 Subject: [Tutor] How to add a description to a module In-Reply-To: References: Message-ID: <29179d161003231230i7385334fo50369024b16ae907@mail.gmail.com> On Tue, Mar 23, 2010 at 8:16 PM, Armstrong, Richard J. wrote: > Hello, > > > > I have a simple question. I have created a module call Newmark.py and it > runs fine, however, I want to add some documentation so that when I type in > the console help(Newmark) it will give a description of the module. Any > ideas? > Simply add a docstring to the top of your module, below the shebang line but above the imports (At least that's where I always put it), just like you would add documentation to functions and classes. Here's the PEP describing docstring conventions: http://www.python.org/dev/peps/pep-0257/ Hugo From sierra_mtnview at sbcglobal.net Tue Mar 23 21:47:40 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 23 Mar 2010 13:47:40 -0700 Subject: [Tutor] Press Enter to quit. Silently maybe. Message-ID: <4BA928EC.3010308@sbcglobal.net> I use this code to quit a completed program. If no is selected for the yes/no prompt, warning messages appear in the shell window. I'm executing from IDLE. Is there a way to just return to the >>> prompt there? def finish(): print; print "Bye" print raw_input('Press Enter to quit') sys.exit() -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From bgailer at gmail.com Tue Mar 23 22:16:52 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 23 Mar 2010 17:16:52 -0400 Subject: [Tutor] Press Enter to quit. Silently maybe. In-Reply-To: <4BA928EC.3010308@sbcglobal.net> References: <4BA928EC.3010308@sbcglobal.net> Message-ID: <4BA92FC4.2020605@gmail.com> On 3/23/2010 4:47 PM, Wayne Watson wrote: > I use this code to quit a completed program. If no is selected for the > yes/no prompt, warning messages appear in the shell window. What is the yes/no prompt? Is it in your program or is it a feature of IDLE? What are the warning messages? > I'm executing from IDLE. Is there a way to just return to the >>> > prompt there? > > def finish(): > print; print "Bye" > print > raw_input('Press Enter to quit') > sys.exit() > -- Bob Gailer 919-636-4239 Chapel Hill NC From anand.shashwat at gmail.com Tue Mar 23 22:47:06 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Wed, 24 Mar 2010 03:17:06 +0530 Subject: [Tutor] Press Enter to quit. Silently maybe. In-Reply-To: <4BA92FC4.2020605@gmail.com> References: <4BA928EC.3010308@sbcglobal.net> <4BA92FC4.2020605@gmail.com> Message-ID: run this file (test.py) as: def finish(): print '\n', "bye", '\n' raw_input('Press Enter to quit: ') finish() $python -i test.py A second approach could be: def finish(): import os, subprocess print '\n', "bye", '\n' raw_input('Press Enter to quit: ') subprocess.call('python') finish() $python test.py bye Press Enter to quit: Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> A word of caution, am not using IDLE and preferably you should avoid it too. ~l0nwlf On Wed, Mar 24, 2010 at 2:46 AM, bob gailer wrote: > On 3/23/2010 4:47 PM, Wayne Watson wrote: > >> I use this code to quit a completed program. If no is selected for the >> yes/no prompt, warning messages appear in the shell window. >> > > What is the yes/no prompt? Is it in your program or is it a feature of > IDLE? > > What are the warning messages? > > > I'm executing from IDLE. Is there a way to just return to the >>> prompt >> there? >> >> def finish(): >> print; print "Bye" >> print >> raw_input('Press Enter to quit') >> sys.exit() >> >> > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Mar 23 23:08:52 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 24 Mar 2010 09:08:52 +1100 Subject: [Tutor] Press Enter to quit. Silently maybe. In-Reply-To: <4BA928EC.3010308@sbcglobal.net> References: <4BA928EC.3010308@sbcglobal.net> Message-ID: <201003240908.53150.steve@pearwood.info> On Wed, 24 Mar 2010 07:47:40 am Wayne Watson wrote: > I use this code to quit a completed program. What on earth for? If the program is complete, just quit. In my opinion, there is very little worse than setting up a chain of long-running programs to run overnight, then coming back in the morning expecting that they will all be finished only to discover that one of those programs is stupidly sitting them with a "Press any key to quit" message, stopping all the rest from running. In my opinion, such behaviour should be a shooting offense. *wink* > If no is selected for > the yes/no prompt, warning messages appear in the shell window. I'm > executing from IDLE. Is there a way to just return to the >>> prompt > there? > > def finish(): > print; print "Bye" > print > raw_input('Press Enter to quit') > sys.exit() What yes/no prompt? How do you select No? -- Steven D'Aprano From sierra_mtnview at sbcglobal.net Wed Mar 24 02:40:39 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 23 Mar 2010 18:40:39 -0700 Subject: [Tutor] Press Enter to quit. Silently maybe. In-Reply-To: <201003240908.53150.steve@pearwood.info> References: <4BA928EC.3010308@sbcglobal.net> <201003240908.53150.steve@pearwood.info> Message-ID: <4BA96D97.8010606@sbcglobal.net> Win 7. Some time ago, I believe under Tutor, it was suggested when quitting to move the method I described. Ah, I see what happened! I had used this in something of an earlier incarnation of the program when some tkinter code was in use. There was a loop in the code, and the quit code used there crept into the finish here. Yes, no need to fiddle with the finish. Just let it reach the end. On 3/23/2010 3:08 PM, Steven D'Aprano wrote: > On Wed, 24 Mar 2010 07:47:40 am Wayne Watson wrote: > > >> I use this code to quit a completed program. >> > What on earth for? If the program is complete, just quit. > > In my opinion, there is very little worse than setting up a chain of > long-running programs to run overnight, then coming back in the morning > expecting that they will all be finished only to discover that one of > those programs is stupidly sitting them with a "Press any key to quit" > message, stopping all the rest from running. > > In my opinion, such behaviour should be a shooting offense. > > *wink* > > > >> If no is selected for >> the yes/no prompt, warning messages appear in the shell window. I'm >> executing from IDLE. Is there a way to just return to the>>> prompt >> there? >> >> def finish(): >> print; print "Bye" >> print >> raw_input('Press Enter to quit') >> sys.exit() >> > What yes/no prompt? How do you select No? > > > > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet Stop the illegal killing of dolphins and porpoises. Wrest the control of the world's fisheries from Japan. Web Page: From denis.spir at gmail.com Thu Mar 25 10:39:32 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Thu, 25 Mar 2010 10:39:32 +0100 Subject: [Tutor] a bug I cannot solve myself ;-) Message-ID: <20100325103932.0443c6ac@o> Hello, I'm writing a kind of language simulation. Here, an expression like "a#3.b" maps to a NamedData node, with an attribute terms=[('.','a'),('#',3),('.'''b')]. (The lang uses '#' instead of "[...]" for item indexing.) When this node is "run", the source code maps to a name lookup operation in current scope, passing the terms attr as argument. Below the code with debug prints and the error I get and cannot understand: ================== class Scope(Code): ... @staticmethod def lookup(scope, terms): ''' actual data refered to by name (or context) terms''' data = scope # actually, initial container for term in terms: (sign,key) = term print data, (sign,ATTR,sign==ATTR,sign is ATTR), key if sign == "ATTR": # sign == ATTR='.' print "getAttr" data = data.getAttr(key) else: # sign == ITEM='#' print "getItem" data = data.getItem(key) ####### line 82 ####### return data === output ======= currentScope ('.', '.', True, True) a getItem ... traceback ... File "/home/spir/prog/python/claro/scope.py", line 82, in lookup data = data.getItem(key) AttributeError: 'Scope' object has no attribute 'getItem' ================== (Scopes actually have no getIem, they're plain symbol (attribute) containers.) There must be something such obvious I'm too stupid to see it! What do I overlook? How can it branch to the "getItem" side of the choice? Denis ________________________________ vit esse estrany ? spir.wikidot.com From andreengels at gmail.com Thu Mar 25 10:54:49 2010 From: andreengels at gmail.com (Andre Engels) Date: Thu, 25 Mar 2010 10:54:49 +0100 Subject: [Tutor] a bug I cannot solve myself ;-) In-Reply-To: <20100325103932.0443c6ac@o> References: <20100325103932.0443c6ac@o> Message-ID: <6faf39c91003250254q11acf21cw25a1ec8053adfcb@mail.gmail.com> 2010/3/25 spir ? : > Hello, > > > I'm writing a kind of language simulation. Here, an expression like "a#3.b" maps to a NamedData node, with an attribute terms=[('.','a'),('#',3),('.'''b')]. > (The lang uses '#' instead of "[...]" for item indexing.) > When this node is "run", the source code maps to a name lookup operation in current scope, passing the terms attr as argument. Below the code with debug prints and the error I get and cannot understand: > > ================== > class Scope(Code): > ? ?... > ? ?@staticmethod > ? ?def lookup(scope, terms): > ? ? ? ?''' actual data refered to by name (or context) terms''' > ? ? ? ?data = scope ? ?# actually, initial container > ? ? ? ?for term in terms: > ? ? ? ? ? ?(sign,key) = term > ? ? ? ? ? ?print data, (sign,ATTR,sign==ATTR,sign is ATTR), key > ? ? ? ? ? ?if sign == "ATTR": ? ?# sign == ATTR='.' > ? ? ? ? ? ? ? ?print "getAttr" > ? ? ? ? ? ? ? ?data = data.getAttr(key) > ? ? ? ? ? ?else: ? ? ? ? ? ? ? ? # sign == ITEM='#' > ? ? ? ? ? ? ? ?print "getItem" > ? ? ? ? ? ? ? ?data = data.getItem(key) ####### line 82 ####### > ? ? ? ?return data > === output ======= > currentScope ('.', '.', True, True) a > getItem > ... traceback ... > ?File "/home/spir/prog/python/claro/scope.py", line 82, in lookup > ? ?data = data.getItem(key) > AttributeError: 'Scope' object has no attribute 'getItem' > ================== > > (Scopes actually have no getIem, they're plain symbol (attribute) containers.) > > There must be something such obvious I'm too stupid to see it! What do I overlook? How can it branch to the "getItem" side of the choice? if sign == "ATTR": should be if sign == ATTR: -- Andr? Engels, andreengels at gmail.com From timomlists at gmail.com Thu Mar 25 11:55:06 2010 From: timomlists at gmail.com (Timo) Date: Thu, 25 Mar 2010 11:55:06 +0100 Subject: [Tutor] Sending mail Message-ID: Hello, I was wondering what the best way is to send an email through my program? I want the user to send an email with an attachment. I do have a webspace, should I use the smtplib module if my webhost supports it (I have to ask though), or should I put a script on my space and communicate with that? Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From crp at cmc.net Thu Mar 25 12:24:05 2010 From: crp at cmc.net (Ray Parrish) Date: Thu, 25 Mar 2010 04:24:05 -0700 Subject: [Tutor] KeyError: '61.135.168.82' with dictionary. Message-ID: <4BAB47D5.4010900@cmc.net> Hello, The following code works interactively, but when ran from a script, I get the errors, after the second code block, which is the code from the script. >>> lineList = [] >>> thisIPAddress = '61.135.168.82' >>> date = "2010 3 12" >>> dates = {date:{thisIPAddress:lineList}} >>> dates[date][thisIPAddress] [] >>> dates[date][thisIPAddress].append(16) >>> dates[date][thisIPAddress] [16] >>> dates[date][thisIPAddress].append(17) >>> dates[date][thisIPAddress] [16, 17] >>> Script code begins here - thisIPAddress = columns[10] lineList = [] if date == "": date = columns[0] dates = {date:{thisIPAddress:lineList}} # For each date in the input file collect an array of log lines # for each unique ip address. date = columns[0] dates[date][thisIPAddress].append(eachLine) Error messages follow - Traceback (most recent call last): File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2519, in ReadInDaysLog(tempLogName, countryCodes) File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2241, in ReadInDaysLog dates[date][thisIPAddress].append(eachLine) KeyError: '61.135.168.82' As you can see, it is tossing a key error on the same ip address that was used in the interactive code successfully, I can not figure out what to change to make the script work. Does someone know why this happens? Thanks, Ray Parrish -- Linux dpkg Software Report script set.. http://www.rayslinks.com/LinuxdpkgSoftwareReport.html Ray's Links, a variety of links to usefull things, and articles by Ray. http://www.rayslinks.com Writings of "The" Schizophrenic, what it's like to be a schizo, and other things, including my poetry. http://www.writingsoftheschizophrenic.com From hugo.yoshi at gmail.com Thu Mar 25 13:31:07 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 25 Mar 2010 13:31:07 +0100 Subject: [Tutor] KeyError: '61.135.168.82' with dictionary. In-Reply-To: <4BAB47D5.4010900@cmc.net> References: <4BAB47D5.4010900@cmc.net> Message-ID: <29179d161003250531u7d17895dpd808dfb0808a444d@mail.gmail.com> On Thu, Mar 25, 2010 at 12:24 PM, Ray Parrish wrote: > > As you can see, it is tossing a key error on the same ip address that was > used in the interactive code successfully, > > I can not figure out what to change to make the script work. Does someone > know why this happens? > It's impossible to tell for sure from just that snippet, but my first guess would be that the if statement is not actually executed. Put a print statement in there and check if you're really executing what you think you're executing. Hugo From ps_python at yahoo.com Thu Mar 25 16:34:51 2010 From: ps_python at yahoo.com (kumar s) Date: Thu, 25 Mar 2010 08:34:51 -0700 (PDT) Subject: [Tutor] help with loops Message-ID: <160557.76904.qm@web112501.mail.gq1.yahoo.com> Dear group: I need some tips/help from experts. I have two files tab-delimted. One file is 4K lines. The other files is 40K lines. I want to search contents of a file to other and print those lines that satisfy. File 1: chr X Y chr1 8337733 8337767 NM_001042682_cds_0_0_chr1_8337734_r 0 - RERE chr1 8338065 8338246 NM_001042682_cds_1_0_chr1_8338066_r 0 - RERE chr1 8338746 8338893 NM_001042682_cds_2_0_chr1_8338747_r 0 - RERE chr1 8340842 8341563 NM_001042682_cds_3_0_chr1_8340843_r 0 - RERE chr1 8342410 8342633 NM_001042682_cds_4_0_chr1_8342411_r 0 - RERE File 2: Chr X Y chr1 871490 871491 chr1 925085 925086 chr1 980143 980144 chr1 1548655 1548656 chr1 1589675 1589676 chr1 1977853 1977854 chr1 3384899 3384900 chr1 3406309 3406310 chr1 3732274 3732275 I want to search if file 2 X is greater or less then X and Y and print line of file 2 and last column of file 1: for j in file2: col = j.split('\t') for k in file1: cols = k.split('\t') if col[1] > cols[1]: if col[1] < cols[2]: print j +'\t'+cols[6] This prints a lot of duplicate lines and is slow. Is there any other way I can make it fast. In file 1, how a dictionary can be made. I mean unique keys that are common to file 1 and 2. thanks Kumar. From andreas at kostyrka.org Thu Mar 25 16:36:19 2010 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Thu, 25 Mar 2010 16:36:19 +0100 Subject: [Tutor] KeyError: '61.135.168.82' with dictionary. In-Reply-To: <29179d161003250531u7d17895dpd808dfb0808a444d@mail.gmail.com> References: <4BAB47D5.4010900@cmc.net> <29179d161003250531u7d17895dpd808dfb0808a444d@mail.gmail.com> Message-ID: <201003251636.20125.andreas@kostyrka.org> Am Donnerstag, 25. M?rz 2010 13:31:07 schrieb Hugo Arts: > On Thu, Mar 25, 2010 at 12:24 PM, Ray Parrish wrote: > > As you can see, it is tossing a key error on the same ip address that was > > used in the interactive code successfully, > > > > I can not figure out what to change to make the script work. Does someone > > know why this happens? > > It's impossible to tell for sure from just that snippet, but my first > guess would be that the if statement is not actually executed. Put a > print statement in there and check if you're really executing what you > think you're executing. Actually the snippet looks like it should be executed in a loop. If so, the code destroys the dictionary for each new entry, I'd say. Andreas > > Hugo > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From bgailer at gmail.com Thu Mar 25 18:55:36 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 25 Mar 2010 13:55:36 -0400 Subject: [Tutor] a bug I cannot solve myself ;-) In-Reply-To: <20100325103932.0443c6ac@o> References: <20100325103932.0443c6ac@o> Message-ID: <4BABA398.3010402@gmail.com> On 3/25/2010 5:39 AM, spir ? wrote: > Hello, > > > I'm writing a kind of language simulation. Here, an expression like "a#3.b" maps to a NamedData node, with an attribute terms=[('.','a'),('#',3),('.'''b')]. > (The lang uses '#' instead of "[...]" for item indexing.) > When this node is "run", the source code maps to a name lookup operation in current scope, passing the terms attr as argument. Below the code with debug prints and the error I get and cannot understand: > > ================== > class Scope(Code): > ... > @staticmethod > def lookup(scope, terms): > ''' actual data refered to by name (or context) terms''' > data = scope # actually, initial container > for term in terms: > (sign,key) = term > print data, (sign,ATTR,sign==ATTR,sign is ATTR), key > if sign == "ATTR": # sign == ATTR='.' > print "getAttr" > data = data.getAttr(key) > else: # sign == ITEM='#' > print "getItem" > data = data.getItem(key) ####### line 82 ####### > return data > === output ======= > currentScope ('.', '.', True, True) a > getItem > ... traceback ... > File "/home/spir/prog/python/claro/scope.py", line 82, in lookup > data = data.getItem(key) > AttributeError: 'Scope' object has no attribute 'getItem' > ================== > > (Scopes actually have no getIem, they're plain symbol (attribute) containers.) > > There must be something such obvious I'm too stupid to see it! What do I overlook? First you assign data = scope Then you reassign data = data.getAttr(key) OR data = data.getItem(key) data no longer refers to scope! Hence the error. > How can it branch to the "getItem" side of the choice? > > > Denis > ________________________________ > > vit esse estrany ? > > spir.wikidot.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 919-636-4239 Chapel Hill NC From bgailer at gmail.com Thu Mar 25 21:31:58 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 25 Mar 2010 16:31:58 -0400 Subject: [Tutor] help with loops In-Reply-To: <160557.76904.qm@web112501.mail.gq1.yahoo.com> References: <160557.76904.qm@web112501.mail.gq1.yahoo.com> Message-ID: <4BABC83E.2000500@gmail.com> On 3/25/2010 11:34 AM, kumar s wrote: > Dear group: > > I need some tips/help from experts. > > I have two files tab-delimted. > One file is 4K lines. The other files is 40K lines. > > I want to search contents of a file to other and print those lines that satisfy. > > > File 1: > chr X Y > chr1 8337733 8337767 NM_001042682_cds_0_0_chr1_8337734_r 0 - RERE > chr1 8338065 8338246 NM_001042682_cds_1_0_chr1_8338066_r 0 - RERE > chr1 8338746 8338893 NM_001042682_cds_2_0_chr1_8338747_r 0 - RERE > chr1 8340842 8341563 NM_001042682_cds_3_0_chr1_8340843_r 0 - RERE > chr1 8342410 8342633 NM_001042682_cds_4_0_chr1_8342411_r 0 - RERE > > > File 2: > Chr X Y > chr1 871490 871491 > chr1 925085 925086 > chr1 980143 980144 > chr1 1548655 1548656 > chr1 1589675 1589676 > chr1 1977853 1977854 > chr1 3384899 3384900 > chr1 3406309 3406310 > chr1 3732274 3732275 > > > I want to search if file 2 X is greater or less then X and Y and print line of file 2 and last column of file 1: > > I don't understand your desired result. Could you post a very simplified example of file1 and 2 and the desired result. > for j in file2: > col = j.split('\t') > for k in file1: > Note that the first time this loop is executed it will leave file2 at eof. Subsequent executions of this loop will process no lines as file2 is at eof. > cols = k.split('\t') > if col[1]> cols[1]: > Note that this compares 2 strings; which may not give the same result as integer comparison. > if col[1]< cols[2]: > print j +'\t'+cols[6] > > > This prints a lot of duplicate lines and is slow. Is there any other way I can make it fast. > > In file 1, how a dictionary can be made. I mean unique keys that are common to file 1 and 2. > > thanks > Kumar. > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Bob Gailer 919-636-4239 Chapel Hill NC From rarmstro at water.ca.gov Thu Mar 25 21:41:35 2010 From: rarmstro at water.ca.gov (Armstrong, Richard J.) Date: Thu, 25 Mar 2010 13:41:35 -0700 Subject: [Tutor] Interpolation function Message-ID: Hello all, Does anyone have a suggestion for a good interpolation function in numpy or scipy. I have an earthquake time history with a time step of 0.005 sec and want to convert it to a time history with a time step of say 0.01. The interpolation function numpy.interp is too "coarse" and modifies the characteristic of the time history too much. Thanks, Richie -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Thu Mar 25 21:52:42 2010 From: wprins at gmail.com (Walter Prins) Date: Thu, 25 Mar 2010 22:52:42 +0200 Subject: [Tutor] help with loops In-Reply-To: <4BABC83E.2000500@gmail.com> References: <160557.76904.qm@web112501.mail.gq1.yahoo.com> <4BABC83E.2000500@gmail.com> Message-ID: <7d961c461003251352i54b86f7dr1efca0938e8b9a0d@mail.gmail.com> So, am I right that for each X value in file 2, you want to look up to see if you can find a corresponding line in file 1 where the value from file 2 falls between the X and Y value from file 1, and if found, then output the original line from file 2 and the 6th column from the found line from file 1? Is it posible to have multiple entries/lines from file 1 where the value from file 2 will be found between X and Y in file 1? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Mar 25 22:21:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Mar 2010 08:21:56 +1100 Subject: [Tutor] Interpolation function In-Reply-To: References: Message-ID: <201003260821.56527.steve@pearwood.info> On Fri, 26 Mar 2010 07:41:35 am Armstrong, Richard J. wrote: > Hello all, > > > > Does anyone have a suggestion for a good interpolation function in > numpy or scipy. I have an earthquake time history with a time step of > 0.005 sec and want to convert it to a time history with a time step > of say 0.01. The interpolation function numpy.interp is too "coarse" > and modifies the characteristic of the time history too much. You probably should take that question to a dedicated numpy or scipy mailing list, where the folks are more likely to be numerically sophisticated. Unless there happens to be an experienced numpy/scipy user hanging around here, any answer we give would be just guessing. -- Steven D'Aprano From bala.biophysics at gmail.com Fri Mar 26 13:07:10 2010 From: bala.biophysics at gmail.com (Bala subramanian) Date: Fri, 26 Mar 2010 13:07:10 +0100 Subject: [Tutor] python magazine Message-ID: <288df32a1003260507y431f47cwc61e732bfd0d3dcd@mail.gmail.com> Friends, I am sorry if this query is not appropriate to this forum. Is there any online magazine dedicated to python especially its features and How-to's that i can subscribe for. Thanks, Bala -------------- next part -------------- An HTML attachment was scrubbed... URL: From timomlists at gmail.com Fri Mar 26 14:37:32 2010 From: timomlists at gmail.com (Timo) Date: Fri, 26 Mar 2010 14:37:32 +0100 Subject: [Tutor] Sending mail In-Reply-To: References: Message-ID: 2010/3/25 Timo > Hello, > > I was wondering what the best way is to send an email through my program? I > want the user to send an email with an attachment. > > I do have a webspace, should I use the smtplib module if my webhost > supports it (I have to ask though), or should I put a script on my space and > communicate with that? > > Cheers, > Timo > I worked on the following code. It works, but not for attachments. On the webserver I have: http://python.pastebin.com/9m8MXxuR And inside my program I then do: form = urllib.urlencode([("from", send_from), ("to", send_to), ("subject", subject), ("body", body), ("attachment", attachment)]) webbrowser.open(url + "?" + form) So, 2 questions: - Is this a good way of sending mails over my webhost through my program? - Attachments don't work because I'm sending the path to the file, and ofcourse the cgi script can't find it. How should I solve this? Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Fri Mar 26 14:47:56 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Fri, 26 Mar 2010 19:17:56 +0530 Subject: [Tutor] Sending mail In-Reply-To: References: Message-ID: I guess you can use smtplib. On Fri, Mar 26, 2010 at 7:07 PM, Timo wrote: > > > 2010/3/25 Timo > > Hello, >> >> I was wondering what the best way is to send an email through my program? >> I want the user to send an email with an attachment. >> >> I do have a webspace, should I use the smtplib module if my webhost >> supports it (I have to ask though), or should I put a script on my space and >> communicate with that? >> >> Cheers, >> Timo >> > > > I worked on the following code. It works, but not for attachments. > > On the webserver I have: http://python.pastebin.com/9m8MXxuR > > And inside my program I then do: > form = urllib.urlencode([("from", send_from), ("to", send_to), ("subject", > subject), ("body", body), ("attachment", attachment)]) > webbrowser.open(url + "?" + form) > > > So, 2 questions: > - Is this a good way of sending mails over my webhost through my program? > - Attachments don't work because I'm sending the path to the file, and > ofcourse the cgi script can't find it. How should I solve this? > > Cheers, > Timo > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From c.t.matsumoto at gmail.com Fri Mar 26 15:38:19 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Fri, 26 Mar 2010 15:38:19 +0100 Subject: [Tutor] python magazine In-Reply-To: <288df32a1003260507y431f47cwc61e732bfd0d3dcd@mail.gmail.com> References: <288df32a1003260507y431f47cwc61e732bfd0d3dcd@mail.gmail.com> Message-ID: <4BACC6DB.2070908@gmail.com> Well there was PyMag (http://pymag.phparch.com), each issue had a dedicated howto for a python topic. I say 'was' because they seem to busy doing something with their site, which says they'll be back by January 26th. I know it was possible to order back issues as pdf's. T Bala subramanian wrote: > Friends, > I am sorry if this query is not appropriate to this forum. > > Is there any online magazine dedicated to python especially its features and > How-to's that i can subscribe for. > > Thanks, > Bala > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From lowelltackett at yahoo.com Fri Mar 26 16:33:35 2010 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Fri, 26 Mar 2010 08:33:35 -0700 (PDT) Subject: [Tutor] python magazine In-Reply-To: <288df32a1003260507y431f47cwc61e732bfd0d3dcd@mail.gmail.com> Message-ID: <586585.65653.qm@web110110.mail.gq1.yahoo.com> >From the virtual desk of Lowell Tackett? --- On Fri, 3/26/10, Bala subramanian wrote: From: Bala subramanian Subject: [Tutor] python magazine To: tutor at python.org Date: Friday, March 26, 2010, 8:07 AM Friends, I am sorry if this query is not appropriate to this forum. Is there any online magazine dedicated to python especially its features and How-to's that i can subscribe for. Thanks, Bala -----Inline Attachment Follows----- http://twitter.com/pymag The Python Magazine people have now got a Twitter site--which includes a perhaps [telling] misspelling. _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From johnf at jfcomputer.com Fri Mar 26 19:32:42 2010 From: johnf at jfcomputer.com (John) Date: Fri, 26 Mar 2010 11:32:42 -0700 Subject: [Tutor] python magazine In-Reply-To: <586585.65653.qm@web110110.mail.gq1.yahoo.com> References: <586585.65653.qm@web110110.mail.gq1.yahoo.com> Message-ID: <201003261132.42097.johnf@jfcomputer.com> On Friday 26 March 2010 08:33:35 am Lowell Tackett wrote: > >From the virtual desk of Lowell Tackett? > > --- On Fri, 3/26/10, Bala subramanian wrote: > > From: Bala subramanian > Subject: [Tutor] python magazine > To: tutor at python.org > Date: Friday, March 26, 2010, 8:07 AM > > Friends, > I am sorry if this query is not appropriate to this forum. > > Is there any online magazine dedicated to python especially its features > and How-to's that i can subscribe for. > > Thanks, > Bala > > > -----Inline Attachment Follows----- > > http://twitter.com/pymag > > The Python Magazine people have now got a Twitter site--which includes a > perhaps [telling] misspelling. Last updated Jun 2009??? Johnf From lowelltackett at yahoo.com Fri Mar 26 19:38:54 2010 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Fri, 26 Mar 2010 11:38:54 -0700 (PDT) Subject: [Tutor] python magazine In-Reply-To: <201003261132.42097.johnf@jfcomputer.com> Message-ID: <342198.59124.qm@web110115.mail.gq1.yahoo.com> >From the virtual desk of Lowell Tackett? --- On Fri, 3/26/10, John wrote: From: John Subject: Re: [Tutor] python magazine To: tutor at python.org Date: Friday, March 26, 2010, 2:32 PM On Friday 26 March 2010 08:33:35 am Lowell Tackett wrote: > >From the virtual desk of Lowell Tackett > > --- On Fri, 3/26/10, Bala subramanian wrote: > > From: Bala subramanian > Subject: [Tutor] python magazine > To: tutor at python.org > Date: Friday, March 26, 2010, 8:07 AM > > Friends, > I am sorry if this query is not appropriate to this forum. > > Is there any online magazine dedicated to python especially its features > and How-to's that i can subscribe for. > > Thanks, > Bala > > > -----Inline Attachment Follows----- > > http://twitter.com/pymag > > The Python Magazine people have now got a Twitter site--which includes a > perhaps [telling] misspelling. Last updated Jun 2009??? Johnf Look again...6:32 AM?? Mar16th _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Fri Mar 26 20:44:31 2010 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Fri, 26 Mar 2010 12:44:31 -0700 Subject: [Tutor] Why this Difference in Importing NumPy 1.2 vs 1.4? Message-ID: <4BAD0E9F.9010308@sbcglobal.net> An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Mar 26 22:09:50 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 26 Mar 2010 14:09:50 -0700 Subject: [Tutor] Why this Difference in Importing NumPy 1.2 vs 1.4? In-Reply-To: <4BAD0E9F.9010308@sbcglobal.net> References: <4BAD0E9F.9010308@sbcglobal.net> Message-ID: On 3/26/2010 12:44 PM Wayne Watson said... > I wrote a program in Python 2.5 under Win7 and it runs fine using Numpy 1.2 , > but not on a colleague's machine who has a slightly newer 2.5. We both use IDLE > to execute the program. During import he gets this: > > >>> > Traceback (most recent call last): > File "C:\Documents and Settings\HP_Administrator.DavesDesktop\My > Documents\Astro\Meteors\NC-FireballReport.py", line 38, in > from scipy import stats as stats # scoreatpercentile > File "C:\Python25\lib\site-packages\scipy\stats\__init__.py", line 7, in > from stats import * > File "C:\Python25\lib\site-packages\scipy\stats\stats.py", line 191, in > import scipy.special as special > File "C:\Python25\lib\site-packages\scipy\special\__init__.py", line 22, in > from numpy.testing import NumpyTest > ImportError: cannot import name NumpyTest > >>> > > Comments? Version specific variations of packages with dedicated mailing lists are likely beyond the scope of this group. I'd ask on the numpy list. See http://www.scipy.org/Mailing_Lists Emile > > -- > Wayne Watson (Watson Adventures, Prop., Nevada City, CA) > > (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) > Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet > Poisoned Shipments. Serious illegal waste dumping may be > occuring in the Meditrainean. Radioactive material, > mercury, biohazards. -- Sci Am Mag, Feb., 2010, p14f. > > Web Page: > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From transmogribenno at gmail.com Sat Mar 27 01:38:51 2010 From: transmogribenno at gmail.com (Benno Lang) Date: Sat, 27 Mar 2010 09:38:51 +0900 Subject: [Tutor] python magazine In-Reply-To: <586585.65653.qm@web110110.mail.gq1.yahoo.com> References: <288df32a1003260507y431f47cwc61e732bfd0d3dcd@mail.gmail.com> <586585.65653.qm@web110110.mail.gq1.yahoo.com> Message-ID: <9b00d1a91003261738t376b55afg399e957204531ff5@mail.gmail.com> On 27 March 2010 00:33, Lowell Tackett wrote: > The Python Magazine people have now got a Twitter site--which includes a perhaps [telling] misspelling. Obviously that's why they're looking for a chief editor - maybe it's even a deliberate ploy. I'm not sure if this affects others, but to me your replies appear inside the quoted section of your mail, rather than beneath it. Would you mind writing plain text emails to avoid this issue? Thanks, benno From lowelltackett at yahoo.com Sat Mar 27 02:04:02 2010 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Fri, 26 Mar 2010 18:04:02 -0700 (PDT) Subject: [Tutor] python magazine Message-ID: <704855.37858.qm@web110114.mail.gq1.yahoo.com> >From the virtual desk of Lowell Tackett --- On Fri, 3/26/10, Benno Lang wrote: From: Benno Lang Subject: Re: [Tutor] python magazine To: "Lowell Tackett" Cc: tutor at python.org, "Bala subramanian" Date: Friday, March 26, 2010, 8:38 PM On 27 March 2010 00:33, Lowell Tackett wrote: > The Python Magazine people have now got a Twitter site--which includes a perhaps [telling] misspelling. Obviously that's why they're looking for a chief editor - maybe it's even a deliberate ploy. I'm not sure if this affects others, but to me your replies appear inside the quoted section of your mail, rather than beneath it. Would you mind writing plain text emails to avoid this issue? Thanks, benno Like this...? From eire1130 at gmail.com Sat Mar 27 03:03:06 2010 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 26 Mar 2010 22:03:06 -0400 Subject: [Tutor] First extension Message-ID: <98c3f7c51003261903h1130cb07i61985cdd56e9fb39@mail.gmail.com> Hello All, I'm trying to write my first extension module, and I am getting the following error in my command prompt and I was hoping you all could help me. I have taken the following steps already: 1. My path is set for mingw/bin as well as python31. 2. There is a file in my disutils folder called disutils.cfg that says [build] compiler = mingw32 3. The instructions in the 3.1 documentation state the following: "These instructions only apply if you?re using a version of Python prior to 2.4.1 with a MinGW prior to 3.0.0 (with binutils-2.13.90-20030111- 1. http://docs.python.org/py3k/install/index.html 2. I am using Python 3.1 and the latest MinGW. 4. I tested gcc/mingw by doing C:\python31>gcc -shared pdv.c -o pdv.dll and the test was succesful (or at least I was not given any errors while doing the compile) 5. I searched on the internet and the closest thing I can find is the following: http://bugs.python.org/issue4709 Below you will find the following One, the error report two,my setup.py file three, the file I am trying to turn into a python extension module by running the following two commands: python setup.py build python setup.py install #1 Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. > c:\Python31\Lib\finance>python setup.py build running build running build_ext building 'finance' extension creating build creating build\temp.win-amd64-3.1 creating build\temp.win-amd64-3.1\Release C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python31\include > -IC:\Pytho n31\PC -c finance.c -o build\temp.win-amd64-3.1\Release\finance.o finance.c: In function `PyInit_finance': finance.c:31: warning: implicit declaration of function `Py_Module_Create' finance.c:31: warning: return makes pointer from integer without a cast writing build\temp.win-amd64-3.1\Release\finance.def creating build\lib.win-amd64-3.1 C:\MinGW\bin\gcc.exe -mno-cygwin -shared -s > build\temp.win-amd64-3.1\Release\fin ance.o build\temp.win-amd64-3.1\Release\finance.def -LC:\Python31\libs > -LC:\Pyth on31\PCbuild\amd64 -lpython31 -lmsvcr90 -o > build\lib.win-amd64-3.1\finance.pyd build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x2b): undefined > ref erence to `_imp__PyArg_ParseTuple' build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x5c): undefined > ref erence to `_imp__Py_BuildValue' build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x74): undefined > ref erence to `Py_Module_Create' collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1 > c:\Python31\Lib\finance> #2 from distutils.core import setup, Extension > setup(name = "finance", version = "1.0", ext_modules = [Extension("finance", ["finance.c"])]) #3 #include #include > static PyObject * pdv(PyObject *self, PyObject *args) { double value, rate, timex, denom, pdvx; if (!PyArg_ParseTuple(args, "ddd", &value, &rate, &timex)) return NULL; denom = (double) pow ((1 + rate), (timex)); pdvx = value / denom; return Py_BuildValue("d", pdvx); } PyMethodDef pdvMethods[] = { {"pdv", pdv, METH_VARARGS, "Returns the Present Discounted Value given > of a single future value"}, {NULL, NULL, 0, NULL} }; > static struct PyModuleDef financemodule = { PyModuleDef_HEAD_INIT, "finance", /* name of module */ NULL, /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ pdvMethods }; > PyMODINIT_FUNC PyInit_finance(void) { return Py_Module_Create(&financemodule); } -------------- next part -------------- An HTML attachment was scrubbed... URL: From kp8 at mac.com Sat Mar 27 08:55:15 2010 From: kp8 at mac.com (kevin parks) Date: Sat, 27 Mar 2010 16:55:15 +0900 Subject: [Tutor] subprocess Message-ID: I tried readings some toots and tried reading alan's thing. I just still can't grok how to use subprocess. I am trying to call sox (fun fact: an early contributer to sox was none other than Guido van Rossum) In the old days you would just use os i guess, like: import os os.system('sox -V3 -D -S St.01.aif -b16 Stout-01.aif rate -s -v 44100') to call a unix executable that you would ordinarily run from the terminal. what would the equivalent of this in python's new subprocess be? perhaps if i saw an example it would click.. additionally.. sox is a sound conversion tool. I plan to batch process a bunch of files. Will subprocess start all the jobs as it finds the files? or will it process one job and que the next? If it opened a thread and started a bunch of jobs it would likely bog down the system no? anyway .... I have the os walk and other stuff that i am working on and i was hoping to get some help on subprocess. There are a few pages on subprocess but they all might just as well be in chinese and none of them, none that i can see are equivalent to what i am trying to do (they all seem to be doing os-y things) An example might help. Not sure why i am finding this so hard to get my head around. From david at pythontoo.com Sat Mar 27 09:30:05 2010 From: david at pythontoo.com (David Abbott) Date: Sat, 27 Mar 2010 04:30:05 -0400 Subject: [Tutor] subprocess In-Reply-To: References: Message-ID: <1269678605.10761.16.camel@opteron.dwabbott.com> On Sat, 2010-03-27 at 16:55 +0900, kevin parks wrote: > I tried readings some toots and tried reading alan's thing. I just still can't grok how to use subprocess. > > I am trying to call sox (fun fact: an early contributer to sox was none other than Guido van Rossum) > > In the old days you would just use os i guess, like: > > import os > os.system('sox -V3 -D -S St.01.aif -b16 Stout-01.aif rate -s -v 44100') > > to call a unix executable that you would ordinarily run from the terminal. > > what would the equivalent of this in python's new subprocess be? perhaps if i saw an example it would click.. > > additionally.. sox is a sound conversion tool. I plan to batch process a bunch of files. Will subprocess start all the jobs as it finds the files? or will it process one job and que the next? If it opened a thread and started a bunch of jobs it would likely bog down the system no? > > anyway .... I have the os walk and other stuff that i am working on and i was hoping to get some help on subprocess. There are a few pages on subprocess but they all might just as well be in chinese and none of them, none that i can see are equivalent to what i am trying to do (they all seem to be doing os-y things) > > An example might help. Not sure why i am finding this so hard to get my head around. Here is an example using subprocess.call http://dwabbott.com/code/index8.html and some more here with subprocess.Popen http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess HTH David From davea at ieee.org Sat Mar 27 11:12:08 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 27 Mar 2010 05:12:08 -0500 Subject: [Tutor] python magazine In-Reply-To: <704855.37858.qm@web110114.mail.gq1.yahoo.com> References: <704855.37858.qm@web110114.mail.gq1.yahoo.com> Message-ID: <4BADD9F8.7040806@ieee.org> Lowell Tackett wrote: > >From the virtual desk of Lowell Tackett > > > --- On Fri, 3/26/10, Benno Lang wrote: > > From: Benno Lang > Subject: Re: [Tutor] python magazine > To: "Lowell Tackett" > Cc: tutor at python.org, "Bala subramanian" > Date: Friday, March 26, 2010, 8:38 PM > > On 27 March 2010 00:33, Lowell Tackett wrote: > >> The Python Magazine people have now got a Twitter site--which includes a perhaps [telling] misspelling. >> > Obviously that's why they're looking for a chief editor - maybe it's > even a deliberate ploy. > > I'm not sure if this affects others, but to me your replies appear > inside the quoted section of your mail, rather than beneath it. Would > you mind writing plain text emails to avoid this issue? > > Thanks, > benno > > Like this...? > > > No, there's still a problem. You'll notice in this message that there are ">" symbols in front of your lines and benno's, and ">>" symbols in front of Lowell's. (Some email readers will turn the > into vertical bar, but the effect is the same). Your email program should be adding those upon a reply, so that your own message has one less > than the one to which you're replying. Then everyone reading can see who wrote what, based on how many ">" or bars precede the respective lines. Quotes from older messages have more of them. Are you using "Reply-All" in your email program? Or are you constructing a new message with copy/paste? What email are you using? Maybe it's a configuration setting somebody could help with. DaveA From kp8 at mac.com Sat Mar 27 11:56:38 2010 From: kp8 at mac.com (kevin parks) Date: Sat, 27 Mar 2010 19:56:38 +0900 Subject: [Tutor] subprocess In-Reply-To: <1269678605.10761.16.camel@opteron.dwabbott.com> References: <1269678605.10761.16.camel@opteron.dwabbott.com> Message-ID: <2B06C43A-C19D-40C5-97D0-4EDB511709AE@mac.com> Thanks David. Those are excellent short clear examples. I will look those over. Super! Thanks for that. -kp On Mar 27, 2010, at 5:30 PM, David Abbott wrote: > On Sat, 2010-03-27 at 16:55 +0900, kevin parks wrote: > > Here is an example using subprocess.call > http://dwabbott.com/code/index8.html > > and some more here with subprocess.Popen > http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess > > HTH > David > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Sat Mar 27 12:33:59 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 27 Mar 2010 12:33:59 +0100 Subject: [Tutor] subprocess In-Reply-To: <1269678605.10761.16.camel@opteron.dwabbott.com> References: <1269678605.10761.16.camel@opteron.dwabbott.com> Message-ID: On 27 March 2010 09:30, David Abbott wrote: > Here is an example using subprocess.call > http://dwabbott.com/code/index8.html > > and some more here with subprocess.Popen > http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess On top of that we have the excelent PyMOTW from Doug on subprocess. http://blog.doughellmann.com/2007/07/pymotw-subprocess.html Greets Sander From karper12345 at yahoo.com Sat Mar 27 13:45:23 2010 From: karper12345 at yahoo.com (Karjer Jdfjdf) Date: Sat, 27 Mar 2010 05:45:23 -0700 (PDT) Subject: [Tutor] "IOError: [Errno 32] Broken pipe" when running python with cron (alternatives?) Message-ID: <534486.37306.qm@web44705.mail.sp1.yahoo.com> I have made an extensive script that runs fine when started from the command line or IDLE. When I try to run it with cron it keeps giving errors: Error in sys.exitfunc: Traceback (most recent call last): ? File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs ??? func(*targs, **kargs) ? File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown ??? h.flush() ? File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush ??? self.stream.flush() IOError: [Errno 32] Broken pipe The script has the following structure: 1. retrieves data from database 1 2. modifies the data 3. inserts the modified data in another database In a previous script I solved this problem with directing the stdout and stderr to /dev/null, but doing this isn't possible because I use pickle and write some data to files. It seems to have anything to do with the stdout/stderr and cron, but after a lot of googling I don't have any clues how to fix this. How can I solve this? Does anybody know how to solve this or is there a python-friendly alternative to cron that I can use. -------------- next part -------------- An HTML attachment was scrubbed... URL: From teejmd at gmail.com Sat Mar 27 13:55:01 2010 From: teejmd at gmail.com (TJ Dack) Date: Sat, 27 Mar 2010 22:55:01 +1000 Subject: [Tutor] Odds and even exercise Message-ID: Hi, just started python at Uni and think i am in for a rough ride with zero prior experience in programming. Anyway my problem that i can't fix my self after googling. The exercise is to generate a list of odd numbers between 1-100 and the same for even numbers. So far this is what i haveCODE: SELECT ALL#A way to display numbers 1 - 100 numbers = range(100) #A way to display all odd numbers odd = numbers[::2] #A way to display all even numbers I can't find a way to easily list the even numbers, i really need a easier way to find answers my self but using the help docs in idle didn't get me far, any tips that you guys have when you come across something you can't solve? -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Sat Mar 27 14:24:13 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Sat, 27 Mar 2010 18:54:13 +0530 Subject: [Tutor] Odds and even exercise In-Reply-To: References: Message-ID: On Sat, Mar 27, 2010 at 6:25 PM, TJ Dack wrote: > Hi, just started python at Uni and think i am in for a rough ride with zero > prior experience in programming. > Anyway my problem that i can't fix my self after googling. > > The exercise is to generate a list of odd numbers between 1-100 and the > same for even numbers. > > So far this is what i have CODE: SELECT ALL #A > way to display numbers 1 - 100 > numbers = range(100) > Are you sure it displays 1 - 100. ? > #A way to display all odd numbers > odd = numbers[::2] > Are you sure this shows odd :-/ ? Had you read 'range' documentation ? > #A way to display all even numbers > > > I can't find a way to easily list the even numbers, i really need a easier > way to find answers my self but using the help docs in idle didn't get me > far, any tips that you guys have when you come across something you can't > solve? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Mar 27 14:25:49 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 Mar 2010 00:25:49 +1100 Subject: [Tutor] Odds and even exercise In-Reply-To: References: Message-ID: <201003280025.49764.steve@pearwood.info> On Sat, 27 Mar 2010 11:55:01 pm TJ Dack wrote: > Hi, just started python at Uni and think i am in for a rough ride > with zero prior experience in programming. > Anyway my problem that i can't fix my self after googling. > > The exercise is to generate a list of odd numbers between 1-100 and > the same for even numbers. > > So far this is what i haveCODE: SELECT > ALL#A > way to display numbers 1 - 100 > numbers = range(100) > #A way to display all odd numbers > odd = numbers[::2] > #A way to display all even numbers Nice try! Sadly you *just* missed getting it though. Here's an example, using 1-10 instead of 1-100: >>> numbers = range(10) >>> odd = numbers[::2] >>> print numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print odd [0, 2, 4, 6, 8] So you have two small errors: you have the numbers 0-99 instead of 1-100, and consequently the list you called "odd" is actually even. This is what programmers call an "off by one" error. Don't worry, even the best programmers make them! The first thing to remember is that Python's range function works on a "half-open" interval. That is, it includes the starting value, but excludes the ending value. Also, range defaults to a starting value of 0, but you need a starting value of 1. So you need: numbers = range(1, 101) # gives [1, 2, 3, ... 99, 100] Now your odd list will work: odd = numbers[::2] # gives [1, 3, 5, ... 99] How to get even numbers? Consider: Position: 0, 1, 2, 3, 4, 5, ... # Python starts counting at zero Number: 1, 2, 3, 4, 5, 6, ... # The values of list "numbers" Odd/Even: O, E, O, E, O, E, ... Every second number is even, just like for the odd numbers, but instead of starting at position zero you need to start at position one. Do you know how to do that? Hint: numbers[::2] means: start at 0, finish at the end of the list, and return every 2nd value. You want: start at 1, finish at the end of the list, and return every 2nd value. > I can't find a way to easily list the even numbers, i really need a > easier way to find answers my self but using the help docs in idle > didn't get me far, any tips that you guys have when you come across > something you can't solve? So far you seem to be doing well: think about the problem, google it, think about it some more, ask for help. Also, try solving the problem with pencil and paper first, then repeat what you did using Python. Finally, experiment! Open up the Python interpreter, and try things. See what they do. See if you can predict what they will do before you do them. For example, what would these give? range(1, 101, 3) range(2, 101, 4) Try it yourself and see if you predicted correctly. -- Steven D'Aprano From amonroe at columbus.rr.com Sat Mar 27 14:31:17 2010 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Sat, 27 Mar 2010 09:31:17 -0400 Subject: [Tutor] Odds and even exercise In-Reply-To: References: Message-ID: <37256666176.20100327093117@columbus.rr.com> > odd = numbers[::2] > I can't find a way to easily list the even numbers, Hint: You can designate a start number before the first colon. Alan From pythelico at gmail.com Sat Mar 27 14:37:46 2010 From: pythelico at gmail.com (Kelly Netterville) Date: Sat, 27 Mar 2010 09:37:46 -0400 Subject: [Tutor] Odds and even exercise In-Reply-To: References: Message-ID: On Sat, Mar 27, 2010 at 8:55 AM, TJ Dack wrote: > Hi, just started python at Uni and think i am in for a rough ride with zero > prior experience in programming. > Anyway my problem that i can't fix my self after googling. > > The exercise is to generate a list of odd numbers between 1-100 and the > same for even numbers. > > So far this is what i have CODE: SELECT ALL #A > way to display numbers 1 - 100 > numbers = range(100) > #A way to display all odd numbers > odd = numbers[::2] > #A way to display all even numbers > > > I can't find a way to easily list the even numbers, i really need a easier > way to find answers my self but using the help docs in idle didn't get me > far, any tips that you guys have when you come across something you can't > solve? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Sorry, didn't use 'reply all' in my initial response. Sending again to entire list. This is an exercise that is more about thinking programatically than it is about writing Python code. However, consider that even numbers are evenly divisible by 2 and odds are not. Now try to use the basics you've learned so far about Python to solve your problem. A while loop, if, and print statement along with basic mathematic operators are all you really need to solve this. Good luck! Kelly -------------- next part -------------- An HTML attachment was scrubbed... URL: From computing.account at googlemail.com Sat Mar 27 16:21:26 2010 From: computing.account at googlemail.com (AG) Date: Sat, 27 Mar 2010 15:21:26 +0000 Subject: [Tutor] Introduction to modelling with Python Message-ID: <4BAE2276.5050807@gmail.com> Hi List I apologise in advance for the vagueness of this query, but I am looking for a decent modern introduction to modelling using Python. Specifically, I want something that is a good introduction (i.e. doesn't expect one to already be a maths/ statistics or a programming guru) and that has an ecology/ environmental science orientation. The latter is desirable but not essential, as I suspect that once one understands the process of data abstraction and the other steps involved in modelling processes and scenarios, the thinking and skill sets are likely transferable. However, if my assumption about this is incorrect, please let me know. If anyone knows of any resource (book or on-line) with a Python bent, please let me know. I am preparing to begin applications to Ph.D. programs and most of what I am interested in doing requires some knowledge of modelling and Python also seems to be widely accepted as a programming language, so I am happy with that as I am in the process of teaching myself Python anyway. Thanks for any help, advice, etc. Cheers AG From ydmt923 at gmail.com Sat Mar 27 23:08:54 2010 From: ydmt923 at gmail.com (yd) Date: Sat, 27 Mar 2010 17:08:54 -0500 Subject: [Tutor] Prime numbers Message-ID: Having a problem finding the first 1000 prime numbers, here is my code:- print(2) n =3 counter =1 while counter <=1000: for x in range(3,int((n**0.5)),2): if n%x != 0: print(n) n+=1 counter+=1 else: n+=1 The problem is, it prints 2 and then does nothing, yet if i try and close, it says program is still running do you want to kill it, is there a way to do this with lists, i know python has a prime function but i am not going to use it because i want to solve this problem without 'cheating'.Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Sat Mar 27 23:28:54 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Sun, 28 Mar 2010 03:58:54 +0530 Subject: [Tutor] Prime numbers In-Reply-To: References: Message-ID: >>> [x for x in range(3,int((n**0.5)),2)] [] your while loop is an infinite loop. Had you read the range documentations ? >>> range(3,int((n**0.5)),2) [] >>> n**0.5 1.7320508075688772 >>> n = 3 >>> n ** 0.5 1.7320508075688772 >>> int ( n ** 0.5) 1 >>> range ( 3, 1, 2) [] On Sun, Mar 28, 2010 at 3:38 AM, yd wrote: > > Having a problem finding the first 1000 prime numbers, here is my code:- > > print(2) > n =3 > counter =1 > while counter <=1000: > for x in range(3,int((n**0.5)),2): > if n%x != 0: > print(n) > n+=1 > counter+=1 > else: > n+=1 > > The problem is, it prints 2 and then does nothing, yet if i try and close, > it says program is still running do you want to kill it, is there a way to > do this with lists, i know python has a prime function but i am not going to > use it because i want to solve this problem without 'cheating'.Thanks. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ydmt923 at gmail.com Sat Mar 27 23:33:23 2010 From: ydmt923 at gmail.com (yd) Date: Sat, 27 Mar 2010 17:33:23 -0500 Subject: [Tutor] Odds and even exercise Message-ID: Hi, just started python at Uni and think i am in for a rough ride with zero > prior experience in programming. > Anyway my problem that i can't fix my self after googling. > > The exercise is to generate a list of odd numbers between 1-100 and the > same > for even numbers. > > So far this is what i have > way to display numbers 1 - 100 > numbers = range(100) > this gives me numbers= range(0,100) as an output in python 3.0 > I find it easy to do all this stuff with list comprehensions, but i am a beginner so this might not be the most efficient way to do it numbers=[] for x in range(1,101): numbers.append(x) > #A way to display all odd numbers > odd = numbers[::2] > instead i do this: odd=[] for y in range(1,101,2): odd.append(y) > #A way to display all even numbers > even=[] for z in range(2,101,2): even.append(z) -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Mar 28 00:23:44 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 Mar 2010 10:23:44 +1100 Subject: [Tutor] Odds and even exercise In-Reply-To: References: Message-ID: <201003281023.45372.steve@pearwood.info> On Sun, 28 Mar 2010 09:33:23 am yd wrote: > I find it easy to do all this stuff with list comprehensions, but > i am a beginner so this might not be the most efficient way to do it > > numbers=[] > for x in range(1,101): > numbers.append(x) That certainly isn't efficient! In Python 2.x, this is what it does: (1) Create an empty list and call it "numbers". (2) Create a list [1, 2, ... 100] (3) Set up a for-loop. (4) Read the first number from the list (1) and call it x. (5) Add x to the end of numbers. (6) Read the second number from the list (2) and call it x. (7) Add x to the end of numbers. (8) Read the third number from the list and call it x. (9) Add x to the end of numbers. ... (202) Read the 100th number from the list and call it x. (203) Add x to the end of numbers. (204) Finish up the for-loop. Better to just say: (1) Create a list [1, 2, ... 100] and call it "numbers". numbers = range(1, 101) In Python 3.x, it is exactly the same except for step 2, which creates a lazy range-object which only stores one item at a time. So the solution in Python 3.x is to convert it into a list: numbers = list(range(1, 101)) > > #A way to display all odd numbers > > odd = numbers[::2] > > instead i do this: > odd=[] > for y in range(1,101,2): > odd.append(y) This does just as much unnecessary work as above. -- Steven D'Aprano From modulok at gmail.com Sun Mar 28 09:36:07 2010 From: modulok at gmail.com (Modulok) Date: Sun, 28 Mar 2010 01:36:07 -0600 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <4BAE2276.5050807@gmail.com> References: <4BAE2276.5050807@gmail.com> Message-ID: <64c038661003280036g61078f2fje87676c9cf2e2c2c@mail.gmail.com> Could you further define 'modeling' in context? Are you referring to using python in the context of 3D modeling, i.e. computer aided design? If that be the case, python serves as an embedded language for many 3D computer graphics programs. Everything from Maya to Houdini use it as a command interface to automate things. See the developer's documentation for whatever software you're using. What kind of modeling? -Modulok- On 3/27/10, AG wrote: > Hi List > > I apologise in advance for the vagueness of this query, but I am looking > for a decent modern introduction to modelling using Python. > Specifically, I want something that is a good introduction (i.e. doesn't > expect one to already be a maths/ statistics or a programming guru) and > that has an ecology/ environmental science orientation. The latter is > desirable but not essential, as I suspect that once one understands the > process of data abstraction and the other steps involved in modelling > processes and scenarios, the thinking and skill sets are likely > transferable. However, if my assumption about this is incorrect, please > let me know. > > If anyone knows of any resource (book or on-line) with a Python bent, > please let me know. I am preparing to begin applications to Ph.D. > programs and most of what I am interested in doing requires some > knowledge of modelling and Python also seems to be widely accepted as a > programming language, so I am happy with that as I am in the process of > teaching myself Python anyway. > > Thanks for any help, advice, etc. > > Cheers > > AG > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From rafik at ubuntu.com Sun Mar 28 10:16:02 2010 From: rafik at ubuntu.com (Rafik Ouerchefani) Date: Sun, 28 Mar 2010 10:16:02 +0200 Subject: [Tutor] Prime numbers In-Reply-To: References: Message-ID: On Sat, Mar 27, 2010 at 11:08 PM, yd wrote: > > Having a problem finding the first 1000 prime numbers, here is my code:- > > print(2) > n =3 > counter =1 > while counter <=1000: > ? for x in range(3,int((n**0.5)),2): > ??? if n%x != 0: > ????? print(n) > ????? n+=1 > ????? counter+=1 > ??? else: > ????? n+=1 > you are dividing n by x so your while loop should be "while n <= 1000" right ? here is the idea to find prime numbers. loop n from 1 to 1000 loop x from 1 to n if n % x == 0 then increment counter if the counter is == 2, then x is dividable by only 1 and x => x is a prime number > The problem is, it prints 2 and then does nothing, yet if i try and close, > it says program is still running do you want to kill it, is there a way to > do this with lists, i know python has a prime function but i am not going to > use it because i want to solve this problem without 'cheating'.Thanks. well, asking here is cheating :-D cheers, -- Rafik From rabidpoobear at gmail.com Sun Mar 28 10:46:44 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sun, 28 Mar 2010 03:46:44 -0500 Subject: [Tutor] Prime numbers In-Reply-To: References: Message-ID: On Sat, Mar 27, 2010 at 5:08 PM, yd wrote: > > Having a problem finding the first 1000 prime numbers, here is my code:- > > print(2) > n =3 > counter =1 > while counter <=1000: > for x in range(3,int((n**0.5)),2): > if n%x != 0: > print(n) > n+=1 > counter+=1 > else: > n+=1 > > The problem is, it prints 2 and then does nothing, yet if i try and close, > it says program is still running do you want to kill it, is there a way to > do this with lists, i know python has a prime function but i am not going to > use it because i want to solve this problem without 'cheating'.Thanks. > > > What 'problem' are you trying to solve? In general, anytime you can use a premade solution, you are at an advantage, not cheating. That's one of the marks of a truly good programmer - being able to reuse as much code as possible. Unless it's a homework problem and he said "don't use the prime function" because in this case, your goal is to learn how to write a prime function, not to calculate primes. I.E. it's the doing, not the task. And if that's the case, please let us know that it's homework. We will still help you, we just follow certain guidelines when providing homework assistance so as not to "give it away" and still allow you to reason / come up with the solution on your own, as your teacher probably intended. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From computing.account at googlemail.com Sun Mar 28 11:26:43 2010 From: computing.account at googlemail.com (AG) Date: Sun, 28 Mar 2010 10:26:43 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <64c038661003280036g61078f2fje87676c9cf2e2c2c@mail.gmail.com> References: <4BAE2276.5050807@gmail.com> <64c038661003280036g61078f2fje87676c9cf2e2c2c@mail.gmail.com> Message-ID: <4BAF20D3.2040300@gmail.com> Modulok wrote: > Could you further define 'modeling' in context? > > Are you referring to using python in the context of 3D modeling, i.e. > computer aided design? If that be the case, python serves as an > embedded language for many 3D computer graphics programs. Everything > from Maya to Houdini use it as a command interface to automate things. > See the developer's documentation for whatever software you're using. > > What kind of modeling? > -Modulok- > > On 3/27/10, AG wrote: > >> Hi List >> >> I apologise in advance for the vagueness of this query, but I am looking >> for a decent modern introduction to modelling using Python. >> Specifically, I want something that is a good introduction (i.e. doesn't >> expect one to already be a maths/ statistics or a programming guru) and >> that has an ecology/ environmental science orientation. The latter is >> desirable but not essential, as I suspect that once one understands the >> process of data abstraction and the other steps involved in modelling >> processes and scenarios, the thinking and skill sets are likely >> transferable. However, if my assumption about this is incorrect, please >> let me know. >> >> If anyone knows of any resource (book or on-line) with a Python bent, >> please let me know. I am preparing to begin applications to Ph.D. >> programs and most of what I am interested in doing requires some >> knowledge of modelling and Python also seems to be widely accepted as a >> programming language, so I am happy with that as I am in the process of >> teaching myself Python anyway. >> >> Thanks for any help, advice, etc. >> >> Cheers >> >> AG >> _______________________________________________ >> > Modulok The modelling I was referring to is not about 3-D design, but about scenario modelling. For example, to understand the impacts of climate change on particular bodies of water, given different circumstances (e.g. x% of rain in the preceding year, or prevailing winds, or y number of herd animals using the water resource, and/ or upstream engineering developments, etc.), the idea would be to (a) identify those aspects most relevant and least relevant and (b) to programme those elements according to certain parameters of fluctuation (perhaps OOP might be useful here), and then (c) to manipulate those values according to different scenarios. These manipulations can then be replayed any number of times (e.g. a Monte Carlo treatment) to obtain their statistical average and probabilities. The foregoing is possibly not the most elegant example, but hopefully gives you a clearer idea of what I was asking about. Thanks for your interest. AG From denis.spir at gmail.com Sun Mar 28 11:31:57 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 28 Mar 2010 11:31:57 +0200 Subject: [Tutor] inter-module global variable Message-ID: <20100328113157.67cf7e3e@o> Hello, I have a main module importing other modules and defining a top-level variable, call it 'w' [1]. I naively thought that the code from an imported module, when called from main, would know about w, but I have name errors. The initial trial looks as follows (this is just a sketch, the original is too big and complicated): # imported "code" module __all__ = ["NameLookup", "Literal", "Assignment", ...] # main module from parser import parser from code import * from scope import Scope, World w = World() This pattern failed as said above. So, I tried to "export" w: # imported "code" module __all__ = ["NameLookup", "Literal", "Assignment", ...] # main module from parser import parser from scope import Scope, World w = World() import code # new code.w = w ### "export" from code import * And this works. I had the impression that the alteration of the "code" module object would not propagate to objects imported from "code". But it works. But I find this terribly unclear, fragile, and dangerous, for any reason. (I find this "dark", in fact ;-) Would someone try to explain what actually happens in such case? Also, why is a global variable not actually global, but in fact only "locally" global (at the module level)? It's the first time I meet such an issue. What's wrong in my design to raise such a problem, if any? My view is a follow: From the transparency point of view (like for function transparency), the classes in "code" should _receive_ as general parameter a pointer to 'w', before they do anything. In other words, the whole "code" module is like a python code chunk parameterized with w. If it would be a program, it would get w as command-line parameter, or from the user, or from a config file. Then, all instanciations should be done using this pointer to w. Meaning, as a consequence, all code objects should hold a reference to 'w'. This could be made as follows: # main module import code code.Code.w = w from code import * # "code" module class Code(object): w = None ### to be exported from importing module def __init__(self, w=Code.w): # the param allows having a different w eg for testing self.w = w # for each kind of code things class CodeThing(Code): def __init__(self, args): Code.__init__(self) ... use args ... def do(self, args): ... use args and self.w ... But the '###' line looks like an ugly trick to me. (Not the fact that it's a class attribute; as a contrary, I often use them eg for config, and find them a nice tool for clarity.) The issue is that Code.w has to be exported. Also, this scheme is heavy (all these pointers in every living object.) Actually, code objects could read Code.w directly but this does not change much (and I lose transparency). It's hard for me to be lucid on this topic. Is there a pythonic way? Denis [1] The app is a kind of interpreter for a custom language. Imported modules define classes for objects representing elements of code (literal, assignment, ...). Such objects are instanciated from parse tree nodes (conceptually, they *are* code nodes). 'w' is a kind of global scope -- say the state of the running program. Indeed, most code objects need to read/write in w. Any comments on this model welcome. I have few knowledge on implementation of languages. ________________________________ vit esse estrany ? spir.wikidot.com From steve at pearwood.info Sun Mar 28 12:50:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 Mar 2010 21:50:46 +1100 Subject: [Tutor] inter-module global variable In-Reply-To: <20100328113157.67cf7e3e@o> References: <20100328113157.67cf7e3e@o> Message-ID: <201003282150.47499.steve@pearwood.info> On Sun, 28 Mar 2010 08:31:57 pm spir ? wrote: > Hello, > > I have a main module importing other modules and defining a top-level > variable, call it 'w' [1]. I naively thought that the code from an > imported module, when called from main, would know about w, Why would it? If you write a module M, you can't control what names exist in the calling module, and you shouldn't have to. Imagine if you wrote a module containing a function f, and it was imported by another module also containing f, and then suddenly all your module's functions stopped working! This would be a disaster: # mymodule.py def f(n): return n+1 def spam(n): return "spam"*f(n) # caller.py def f(n): return range(23, 45+n, 6) from mymodule import spam print spam(2) # expect "spamspamspam", get TypeError instead > but I > have name errors. The initial trial looks as follows (this is just a > sketch, the original is too big and complicated): > > # imported "code" module > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser By the way, have you looked at PyParsing? This is considered by many to be the gold standard in Python parsing libraries. > from code import * This is discouraged strongly. What happens if the code module has something called parser? Or len? > from scope import Scope, World > w = World() > > This pattern failed as said above. What do you mean "failed"? Nothing you show is obviously broken. > So, I tried to "export" w: > > # imported "code" module > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser > from scope import Scope, World > w = World() > import code # new > code.w = w ### "export" > from code import * > > And this works. I had the impression that the alteration of the > "code" module object would not propagate to objects imported from > "code". But it works. It sounds like you are trying to write PHP code in Python. > But I find this terribly unclear, fragile, and > dangerous, for any reason. (I find this "dark", in fact ;-) Would > someone try to explain what actually happens in such case? Yep, sounds like PHP code :) Every function and class in a module stores a reference to their enclosing globals, so that when you do this: # module A.py x = "Hello world" def f(): print x # module B.py from A import f f() => prints "Hello world" as expected. You don't have to do anything to make this work: every class and function knows what namespace it belongs to. I can only imagine you're trying to do this: # module A.py x = "Hello world" def f(): print x # module B.py x = "Goodbye cruel world!" from A import f f() => prints "Goodbye cruel world!" This is bad design. You might think you need it, but in the long run you will regret it. You are mixing up arguments and globals. If you want the result of f() to depend on the local value of x, then make it take an argument: def f(x): print x and call it: f(x) http://c2.com/cgi/wiki?GlobalVariablesAreBad http://discuss.joelonsoftware.com/default.asp?design.4.249182.18 > Also, why > is a global variable not actually global, but in fact only "locally" > global (at the module level)? It's the first time I meet such an > issue. What's wrong in my design to raise such a problem, if any? In Python, that is a deliberate choice. All globals are deliberately global to the module. The closest thing to "globally global" is the builtins namespace, which is where builtins like len, map, str, etc. are found. Any design which relies on modifying global variables is flawed. Global variables are a poor design: http://weblogs.asp.net/wallen/archive/2003/05/08/6750.aspx Slightly better than global variables is a design where you use a module or class as a namespace, put all your globals in that namespace, then pass it to your other classes as an argument: class SettingsNamespace: pass settings = SettingsNamespace() settings.x = 42 settings.y = 23 settings.z = "magic" instance = MyOtherClass(a, b, c, settings) This is still problematic. For example, if I change settings.x, will the result of MyOtherClass be different? Maybe, maybe not... you have to dig deep into the code to know which settings are used and which are not, and you never know if an innocent-looking call to a function or class will modify your settings and break things. > My view is a follow: From the transparency point of view (like for > function transparency), the classes in "code" should _receive_ as > general parameter a pointer to 'w', before they do anything. Yes, this is better than "really global" globals, but not a lot better. > In other > words, the whole "code" module is like a python code chunk > parameterized with w. If it would be a program, it would get w as > command-line parameter, or from the user, or from a config file. > Then, all instanciations should be done using this pointer to w. > Meaning, as a consequence, all code objects should hold a reference > to 'w'. This could be made as follows: If every code object has a reference to the same object w, that defeats the purpose of passing it as an argument. It might be local in name, but in practice it is "really global", which is dangerous. > # main module > import code > code.Code.w = w Why not just this? code.w = w And where does w come from in the first place? Shouldn't it be defined in code.py, not the calling module? > from code import * This is generally frowned upon. You shouldn't defeat Python's encapsulation of namespaces in that way unless you absolutely have to. > # "code" module > class Code(object): > w = None ### to be exported from importing module That sets up a circular dependency that should be avoided: Code objects are broken unless the caller initialises the class first, but you can't initialise the class unless you import it. Trust me, you WILL forget to initialise it before using it, and then spend hours trying to debug the errors. > def __init__(self, w=Code.w): > # the param allows having a different w eg for testing > self.w = w This needlessly gives each instance a reference to the same w that the class already has. Inheritance makes this unnecessary. You should do this instead: class Code(object): w = None # Better to define default settings here. def __init__(self, w=None): if w is not None: self.w = w If no w is provided, then lookups for instance.w will find the shared class attribute w. [...] > But the '###' line looks like an ugly trick to me. (Not the fact > that it's a class attribute; as a contrary, I often use them eg for > config, and find them a nice tool for clarity.) The issue is that > Code.w has to be exported. It is ugly, and fragile. It means any caller is *expected* to modify the w used everywhere else, in strange and hard-to-predict ways. -- Steven D'Aprano From ydmt923 at gmail.com Sun Mar 28 12:57:58 2010 From: ydmt923 at gmail.com (yd) Date: Sun, 28 Mar 2010 05:57:58 -0500 Subject: [Tutor] Prime numbers Message-ID: > > > > What 'problem' are you trying to solve? > In general, anytime you can use a premade solution, you are at an > advantage, > not cheating. > That's one of the marks of a truly good programmer - being able to reuse as > much code as possible. > Unless it's a homework problem and he said "don't use the prime function" > because in this case, your goal is to learn how to write a prime function, > not to calculate primes. > I.E. it's the doing, not the task. And if that's the case, please let us > know that it's homework. We will still help you, we just follow certain > guidelines > when providing homework assistance so as not to "give it away" and still > allow you to reason / come up with the solution on your own, as your > teacher > probably intended. > > > -Luke > It's not homework i just want to be able to convert my algorithm into good code, and the only way to do that is by actually writing it. I'm just writing it to learn how it's done. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sun Mar 28 13:01:14 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 28 Mar 2010 06:01:14 -0500 Subject: [Tutor] inter-module global variable In-Reply-To: <20100328113157.67cf7e3e@o> References: <20100328113157.67cf7e3e@o> Message-ID: <4BAF36FA.8010606@ieee.org> spir # wrote: > Hello, > > I have a main module importing other modules and defining a top-level variable, call it 'w' [1]. I naively thought that the code from an imported module, when called from main, would know about w, but I have name errors. The initial trial looks as follows (this is just a sketch, the original is too big and complicated): > > # imported "code" module > __all__ ="NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser > from code import * > from scope import Scope, World > w = World() > > This pattern failed as said above. So, I tried to "export" w: > > # imported "code" module > __all__ ="NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser > from scope import Scope, World > w = World() > import code # new > code.w = w ### "export" > from code import * > > And this works. I had the impression that the alteration of the "code" module object would not propagate to objects imported from "code". But it works. But I find this terribly unclear, fragile, and dangerous, for any reason. (I find this "dark", in fact ;-) > Would someone try to explain what actually happens in such case? > Also, why is a global variable not actually global, but in fact only "locally" global (at the module level)? > It's the first time I meet such an issue. What's wrong in my design to raise such a problem, if any? > > My view is a follow: From the transparency point of view (like for function transparency), the classes in "code" should _receive_ as general parameter a pointer to 'w', before they do anything. In other words, the whole "code" module is like a python code chunk parameterized with w. If it would be a program, it would get w as command-line parameter, or from the user, or from a config file. > Then, all instanciations should be done using this pointer to w. Meaning, as a consequence, all code objects should hold a reference to 'w'. This could be made as follows: > > # main module > import code > code.Code.w = > from code import * > > # "code" module > class Code(object): > w =None ### to be exported from importing module > def __init__(self, w=Code.w): > # the param allows having a different w eg for testing > self.w = > # for each kind of code things > class CodeThing(Code): > def __init__(self, args): > Code.__init__(self) > ... use args ... > def do(self, args): > ... use args and self.w ... > > But the '###' line looks like an ugly trick to me. (Not the fact that it's a class attribute; as a contrary, I often use them eg for config, and find them a nice tool for clarity.) The issue is that Code.w has to be exported. > Also, this scheme is heavy (all these pointers in every living object.) Actually, code objects could read Code.w directly but this does not change much (and I lose transparency). > It's hard for me to be lucid on this topic. Is there a pythonic way? > > > Denis > > [1] The app is a kind of interpreter for a custom language. Imported modules define classes for objects representing elements of code (literal, assignment, ...). Such objects are instanciated from parse tree nodes (conceptually, they *are* code nodes). 'w' is a kind of global scope -- say the state of the running program. Indeed, most code objects need to read/write in w. > Any comments on this model welcome. I have few knowledge on implementation of languages. > ________________________________ > > vit esse estrany ? > > spir.wikidot.com > > The word 'global' is indeed unfortunate for those coming to python from other languages. In Python, it does just mean global to a single module. If code in other modules needs to access your 'global variable' they need normally need it to be passed to them. If you really need a program-global value, then create a new module just for the purpose, and define it there. Your main program can initialize it, other modules can access it in the usual way, and everybody's happy. In general, you want import and initialization to happen in a non-recursive way. So an imported module should not look back at you for values. If you want it to know about a value, pass it, or assign it for them. But Python does not have pointers. And you're using pointer terminology. Without specifying the type of w, you give us no clue whether you're setting yourself up for failure. For example, the first time somebody does a w= newvalue they have broken the connection with other module's w variable. If the object is mutable (such as a list), and somebody changes it by using w.append() or w[4] = newvalue, then no problem. You have defined a class attribute w, and an instance attribute w, and a module variable w in your main script. Do these values all want to stay in synch as you change values? Or is it a constant that's just set up once? Or some combination, where existing objects want the original value, but new ones created after you change it will themselves get the value at the time of creation? You can get any of these behaviors, but only if you know which one you want, and code for it. There's no single answer, without guessing your intent. If it's a single value that everyone should see the current value of, then put it in its own module, and have everyone access it explicitly, every time. Don't make an instance attribute or a class attribute. Just make a module called globals (or something) and say globals.w By the way, your first "export" was a hack, where you fake a global variable in another module. But the second one, where you set a class attribute's value, is perfectly reasonable. And also by the way, once you decide your real desired behavior, there still may be some syntactic sugar that's useful. For example, you might define a method (untested) class Code(object): .... @property def w: return globals.w This way every instance appears to have such an instance variable, but it in fact always referring to the single global defined in globals.py HTH DaveA From lowelltackett at yahoo.com Sun Mar 28 13:46:49 2010 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Sun, 28 Mar 2010 04:46:49 -0700 (PDT) Subject: [Tutor] python magazine In-Reply-To: <4BADD9F8.7040806@ieee.org> Message-ID: <806272.75980.qm@web110116.mail.gq1.yahoo.com> >From the virtual desk of Lowell Tackett --- On Sat, 3/27/10, Dave Angel wrote: > From: Dave Angel > Subject: Re: [Tutor] python magazine > To: "Lowell Tackett" > Cc: "Benno Lang" , tutor at python.org > Date: Saturday, March 27, 2010, 6:12 AM > > > Lowell Tackett wrote: > > >From the virtual desk of Lowell Tackett > > > > --- On Fri, 3/26/10, Benno Lang > wrote: > > > > From: Benno Lang > > Subject: Re: [Tutor] python magazine > > To: "Lowell Tackett" > > Cc: tutor at python.org, > "Bala subramanian" > > Date: Friday, March 26, 2010, 8:38 PM > > > > On 27 March 2010 00:33, Lowell Tackett > wrote: > > > >> The Python Magazine people have now got a Twitter > site--which includes a perhaps [telling] misspelling. > >> > > Obviously that's why they're looking for a chief > editor - maybe it's > > even a deliberate ploy. > > > > I'm not sure if this affects others, but to me your > replies appear > > inside the quoted section of your mail, rather than > beneath it. Would > > you mind writing plain text emails to avoid this > issue? > > > > Thanks, > > benno > > > > Like this...? > > > > > > > No, there's still a problem. You'll notice in this > message that there are ">" symbols in front of your lines > and benno's, and ">>" symbols in front of > Lowell's. (Some email readers will turn the > into > vertical bar, but the effect is the same). Your email > program should be adding those upon a reply, so that your > own message has one less > than the one to which you're > replying. Then everyone reading can see who wrote > what, based on how many ">" or bars precede the > respective lines. Quotes from older messages have more > of them. > > Are you using "Reply-All" in your email program? Or > are you constructing a new message with copy/paste? > > What email are you using? Maybe it's a configuration > setting somebody could help with. > > DaveA > > Don't really know what I'm doing wrong (or right). Just using the [email] tools that have been made available to me thru Yahoo mail and Firefox. I began this text below your submission and "signature", and I'm using plain text, as suggested by a previous comment. Don't know what else I could embellish this effort with. From denis.spir at gmail.com Sun Mar 28 13:50:30 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 28 Mar 2010 13:50:30 +0200 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <4BAF20D3.2040300@gmail.com> References: <4BAE2276.5050807@gmail.com> <64c038661003280036g61078f2fje87676c9cf2e2c2c@mail.gmail.com> <4BAF20D3.2040300@gmail.com> Message-ID: <20100328135030.0d2cf5d1@o> On Sun, 28 Mar 2010 10:26:43 +0100 AG wrote: > Modulok wrote: > > Could you further define 'modeling' in context? [...] > The modelling I was referring to is not about 3-D design, but about > scenario modelling. For example, to understand the impacts of climate > change on particular bodies of water, given different circumstances > (e.g. x% of rain in the preceding year, or prevailing winds, or y number > of herd animals using the water resource, and/ or upstream engineering > developments, etc.), the idea would be to (a) identify those aspects > most relevant and least relevant and (b) to programme those elements > according to certain parameters of fluctuation (perhaps OOP might be > useful here), and then (c) to manipulate those values according to > different scenarios. These manipulations can then be replayed any > number of times (e.g. a Monte Carlo treatment) to obtain their > statistical average and probabilities. > > The foregoing is possibly not the most elegant example, but hopefully > gives you a clearer idea of what I was asking about. > > Thanks for your interest. > > AG Note: this is personal point of view. Just hope you & others find it interesting. Programming, for me, is just modelling and coding; in a sense of "modelling" similar to the one you give, which is also close to the sense used in scientific/research activity, as I understand it. The main particuliarity of programming is that the target is a machine, which implies good (accuracy) and bad (rigidity) requirements. More precisely, I call "model" the mental picture we have of a "topic" -- more or less accurate, indeed, according to the guy's lucidity on the topic (clarity of mind) and to the requirements. A program is then a given expression of the programmer's model of the topic in a given language; and a running program is a simulation of the topic according to the model. The program may itself be more or less accurate in the sense there may be some distortion between the model and the program's actual "semantics". In programming practice, except in trivial cases, there is a constant move forth & back between modelling and coding, because while coding and testing we learn about the topic, get a clearer view, and modify the model. Which in turn requires updating the program, building a kind of feedback loop, until we are satisfied with the model, the program, the simulation (*). Like any language (--> Sapir-Worf hypothesis), a programming language certainly strongly influences the way we basically *think* about things, not only how we express it, meaning it will filter our ability to model. For the best and/or for the worse. We may simply not be able to model independently of one (or more) given language(s). After some years programming in Java, a programmer switching to Python will produce not only Java code reworded into Python (which is ugly ;-), but Java *thought* (model) (which is wrong ;-). To relate this with your request, I thus think learning to model in Python is just learning to program well in Python, which itself is just a specialisation of learning to program well. If you really want this, and have enough time, then for me you should instead learn to program using various languages, as different as possible, so as to learn various modelling patterns. And apply those languages on a variety of topics, including some in your own field. A life time's job! Again, this is just my opinion. Denis (*) I like the word simulation here because it does not imply the topic to have anything to do with reality. Instead, it can be imaginary, in most cases real is at most a referent. Think at games (they exist only in the author and players' minds) or even an editor (the legacy way of writing just provides a metaphor). In many cases, the simulation is its own sense, meaning the simulation *is* the topic, there is no referent. ________________________________ vit esse estrany ? spir.wikidot.com From lie.1296 at gmail.com Sun Mar 28 14:37:11 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 28 Mar 2010 23:37:11 +1100 Subject: [Tutor] Prime numbers In-Reply-To: References: Message-ID: On 03/28/2010 09:57 PM, yd wrote: > It's not homework i just want to be able to convert my algorithm into > good code, and the only way to do that is by actually writing it. I'm > just writing it to learn how it's done. In most cases, when: 1) the code is effective (i.e. it always gives correct answer) 2) the code is efficient (i.e. it terminates in a reasonable amount of time, and uses a reasonable amount of memory) 3) you can articulate why you write your code in a particular way, can describe why the algorithm works, and can answer when challenged 4) you and other people can read your code six months later with relatively little difficulty 5) your code is as concise as possible, without affecting #4 (e.g. leveraged most of the side work to another library, used common idioms in the appropriate situations, etc) in most case, you probably have written a good code. From davea at ieee.org Sun Mar 28 14:48:16 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 28 Mar 2010 07:48:16 -0500 Subject: [Tutor] python magazine In-Reply-To: <806272.75980.qm@web110116.mail.gq1.yahoo.com> References: <806272.75980.qm@web110116.mail.gq1.yahoo.com> Message-ID: <4BAF5010.5090504@ieee.org> Lowell Tackett wrote: > >From the virtual desk of Lowell Tackett > > > > --- On Sat, 3/27/10, Dave Angel wrote: > > >> From: Dave Angel >> Subject: Re: [Tutor] python magazine >> To: "Lowell Tackett" >> Cc: "Benno Lang" , tutor at python.org >> Date: Saturday, March 27, 2010, 6:12 AM >> >> >> Lowell Tackett wrote: >> >>> >From the virtual desk of Lowell Tackett >>> >>> --- On Fri, 3/26/10, Benno Lang >>> >> wrote: >> >>> From: Benno Lang >>> Subject: Re: [Tutor] python magazine >>> To: "Lowell Tackett" >>> Cc: tutor at python.org, >>> >> "Bala subramanian" >> >>> Date: Friday, March 26, 2010, 8:38 PM >>> >>> On 27 March 2010 00:33, Lowell Tackett >>> >> wrote: >> >>> >>> >>>> The Python Magazine people have now got a Twitter >>>> >> site--which includes a perhaps [telling] misspelling. >> >>>> >>>> >>> Obviously that's why they're looking for a chief >>> >> editor - maybe it's >> >>> even a deliberate ploy. >>> >>> I'm not sure if this affects others, but to me your >>> >> replies appear >> >>> inside the quoted section of your mail, rather than >>> >> beneath it. Would >> >>> you mind writing plain text emails to avoid this >>> >> issue? >> >>> Thanks, >>> benno >>> >>> Like this...? >>> >>> >>> >>> >> No, there's still a problem. You'll notice in this >> message that there are ">" symbols in front of your lines >> and benno's, and ">>" symbols in front of >> Lowell's. (Some email readers will turn the > into >> vertical bar, but the effect is the same). Your email >> program should be adding those upon a reply, so that your >> own message has one less > than the one to which you're >> replying. Then everyone reading can see who wrote >> what, based on how many ">" or bars precede the >> respective lines. Quotes from older messages have more >> of them. >> >> Are you using "Reply-All" in your email program? Or >> are you constructing a new message with copy/paste? >> >> What email are you using? Maybe it's a configuration >> setting somebody could help with. >> >> DaveA >> >> >> > Don't really know what I'm doing wrong (or right). Just using the [email] tools that have been made available to me thru Yahoo mail and Firefox. I began this text below your submission and "signature", and I'm using plain text, as suggested by a previous comment. Don't know what else I could embellish this effort with. > > > This time it worked great. You can see my comments at outermost level, with yours indented by one, and my previous one indented two, etc. DaveA From denis.spir at gmail.com Sun Mar 28 15:15:47 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Sun, 28 Mar 2010 15:15:47 +0200 Subject: [Tutor] inter-module global variable In-Reply-To: <201003282150.47499.steve@pearwood.info> References: <20100328113157.67cf7e3e@o> <201003282150.47499.steve@pearwood.info> Message-ID: <20100328151547.099a5462@o> On Sun, 28 Mar 2010 21:50:46 +1100 Steven D'Aprano wrote: > On Sun, 28 Mar 2010 08:31:57 pm spir ? wrote: > > Hello, > > > > I have a main module importing other modules and defining a top-level > > variable, call it 'w' [1]. I naively thought that the code from an > > imported module, when called from main, would know about w, > > Why would it? > > If you write a module M, you can't control what names exist in the > calling module, and you shouldn't have to. Imagine if you wrote a > module containing a function f, and it was imported by another module > also containing f, and then suddenly all your module's functions > stopped working! This would be a disaster: Right, this makes sense, indeed, thank you for showing it clearly. > > but I > > have name errors. The initial trial looks as follows (this is just a > > sketch, the original is too big and complicated): > > > > # imported "code" module > > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > > > # main module > > from parser import parser > > By the way, have you looked at PyParsing? This is considered by many to > be the gold standard in Python parsing libraries. Yes, I used to know pyparsing very well... (Including most of its internal arcanes. In fact I wrote several matching/parsing/processing libraries after having worked with it. This because, on one hand, I like pyparsing base approach of a "live" code grammar, but on the other hand its way of doing does not fit my brain.) > > from code import * > > This is discouraged strongly. What happens if the code module has > something called parser? Or len? Yes, I know. But as you can read above, the exported names are defined (and are all classes). Also, the importing module only does one thing, which is precisely to use thoses imported names. Similarly, a grammar/parser definition imports all pattern classes (from my own matching library, like it would from pyparsing) into a module dedicated to the definition of the language. I consider this a good practice as long as it is done consciously. There is no difference, I guess, betweeen # "matchBox" matching module __all__ = [all pattern class names + "Parser"] # parser module from matchBox import * and # parser module from matchBox import > > from scope import Scope, World > > w = World() > > > > This pattern failed as said above. > > What do you mean "failed"? Nothing you show is obviously broken. I mean the name error about 'w'. > > So, I tried to "export" w: > > > > # imported "code" module > > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > > > # main module > > from parser import parser > > from scope import Scope, World > > w = World() > > import code # new > > code.w = w ### "export" > > from code import * > > > > And this works. I had the impression that the alteration of the > > "code" module object would not propagate to objects imported from > > "code". But it works. > > It sounds like you are trying to write PHP code in Python. Know about nothing about PHP. > > But I find this terribly unclear, fragile, and > > dangerous, for any reason. (I find this "dark", in fact ;-) Would > > someone try to explain what actually happens in such case? > > Yep, sounds like PHP code :) > > Every function and class in a module stores a reference to their > enclosing globals, so that when you do this: > > # module A.py > x = "Hello world" > > def f(): > print x > > > # module B.py > from A import f > f() > => prints "Hello world" as expected. > > > You don't have to do anything to make this work: every class and > function knows what namespace it belongs to. All right. > I can only imagine you're trying to do this: > > # module A.py > x = "Hello world" > > def f(): > print x > > > # module B.py > x = "Goodbye cruel world!" > from A import f > f() > => prints "Goodbye cruel world!" Hem, more or less. but the main difference is that x is not and cannot be defined in A (except for a fake initialisation to None, like for an instance var). It's actual value can only come from the module that import A, here B. > This is bad design. You might think you need it, but in the long run you > will regret it. You are mixing up arguments and globals. If you want > the result of f() to depend on the local value of x, then make it take > an argument: > > def f(x): > print x > > and call it: > > f(x) > > > http://c2.com/cgi/wiki?GlobalVariablesAreBad > http://discuss.joelonsoftware.com/default.asp?design.4.249182.18 Gonna follow the links as soon as I have time. > > Also, why > > is a global variable not actually global, but in fact only "locally" > > global (at the module level)? It's the first time I meet such an > > issue. What's wrong in my design to raise such a problem, if any? > > In Python, that is a deliberate choice. All globals are deliberately > global to the module. The closest thing to "globally global" is the > builtins namespace, which is where builtins like len, map, str, etc. > are found. > > Any design which relies on modifying global variables is flawed. Global > variables are a poor design: > > http://weblogs.asp.net/wallen/archive/2003/05/08/6750.aspx In the general case for sure. I 100% agree, and don't do it. But what if what I model is precisely something that must have a global. Like a language. Python has globals, in which every name undefined in locals (or other englobing scopes) is silently looked up. Mine instead has a thing called 'world' that explicitely holds references to top-level things. This is this world I need to pass to code objects (via their classes). > Slightly better than global variables is a design where you use a module > or class as a namespace, put all your globals in that namespace, then > pass it to your other classes as an argument: > > class SettingsNamespace: > pass > > settings = SettingsNamespace() > settings.x = 42 > settings.y = 23 > settings.z = "magic" > > instance = MyOtherClass(a, b, c, settings) > > This is still problematic. For example, if I change settings.x, will the > result of MyOtherClass be different? Maybe, maybe not... you have to > dig deep into the code to know which settings are used and which are > not, and you never know if an innocent-looking call to a function or > class will modify your settings and break things. *** This is exactly analog to my model. And I cannot imagine a better way to do it (both in design of the language and in implementation). The only difference, which cause my issue, is that (*) the chunks of python code that use world are not in the same module. So, to make the paralell more accurate, how would you do it if MyOtherClass were defined in a separate module? And what about pointers to 'settings' in every instance of MyOtherClass? (wasting both time and space)? *** (*) Not only for better code organisation. It could be a shared module, even an external library. > > My view is a follow: From the transparency point of view (like for > > function transparency), the classes in "code" should _receive_ as > > general parameter a pointer to 'w', before they do anything. > > Yes, this is better than "really global" globals, but not a lot better. Right :-) > > In other > > words, the whole "code" module is like a python code chunk > > parameterized with w. If it would be a program, it would get w as > > command-line parameter, or from the user, or from a config file. > > Then, all instanciations should be done using this pointer to w. > > Meaning, as a consequence, all code objects should hold a reference > > to 'w'. This could be made as follows: > > If every code object has a reference to the same object w, that defeats > the purpose of passing it as an argument. It might be local in name, > but in practice it is "really global", which is dangerous. ??? I think we must speak more accurately. World (what I called 'w') represents "what is defined" by a program written in the language my code interprets. It could be eg the game of the secret number and then world would hold (references to) only 2 things, namely the player and the master. Right? (The same as in python, except that (1) there is no "magic" globals, instead there is explicite world (2) one doesn't need a class to create a given thing). The "code" module holds types of code objects (eg an assignment). When such an ibject is created and "run", it will most often read and/on write inside world. For this module itself, world is and can only be a variable, more precisely an input; it cannot be locally defined (created) in the module itself, but it needs to be known there. For every run, a world is passed to the module, it's always a variable, never defined locally. But during a given run, not only there's a single, unique, world shared by everybody, but it never changes (its reference is constant). So, what do you mean "that defeats the purpose of passing it as an argument". If ever it is "dangerous", then how could it be else? The only alternative to having refs to it in the top class, in every class, in every code instance object, is, I guess, to have it as a global of the module. (What I do in the meantime, only because this avoids having refs everywhere). > > # main module > > import code > > code.Code.w = w > > Why not just this? > > code.w = w Yo, this is what I do as of now. > And where does w come from in the first place? Shouldn't it be defined > in code.py, not the calling module? Hem, I don't think it's possible, except if merging the code module with one or more other ones (including the main module of the parser or interpreter). I guess not only "code" needs a reference to 'w'. But I may be wrong, I must examine this point more closely. Not sure. Maybe see also PS if you have time. > > # "code" module > > class Code(object): > > w = None ### to be exported from importing module > > That sets up a circular dependency that should be avoided: Code objects > are broken unless the caller initialises the class first, but you can't > initialise the class unless you import it. Trust me, you WILL forget to > initialise it before using it, and then spend hours trying to debug the > errors. Right. But how else can I do? And how can I check? Analogy: A class requires every instance to have an x. This can be done with a required param in __init__. Then, how can I require a module or a class itself to be properly initialised? [The module itself should be an instance of something requiring an init! Or the class should be an instance of a metaclass requiring it? For now, I prefere to avoid playing with metaclasses.] > > def __init__(self, w=Code.w): > > # the param allows having a different w eg for testing > > self.w = w > > This needlessly gives each instance a reference to the same w that the > class already has. Inheritance makes this unnecessary. You should do > this instead: > > class Code(object): > w = None # Better to define default settings here. > def __init__(self, w=None): > if w is not None: > self.w = w > > If no w is provided, then lookups for instance.w will find the shared > class attribute w. Right. But maybe it's better to have it at the module level if/when I don't need testing variants? > [...] > > But the '###' line looks like an ugly trick to me. (Not the fact > > that it's a class attribute; as a contrary, I often use them eg for > > config, and find them a nice tool for clarity.) The issue is that > > Code.w has to be exported. > > It is ugly, and fragile. It means any caller is *expected* to modify the > w used everywhere else, in strange and hard-to-predict ways. Then, how to do it? Anyway, thank you very much again, Steven (I really appreciate your replies, they help me thinking :-). Denis PS: Actually the parse tree builds a fully abstract representation of the source which relevant node are code objects. Each code type has both "execute" and "code" methods. When the parser itself runs in mode ON, execute() methods run (eg a code object representation an assignment will create and put a new symbol in world, from running execute methods of its target and expression child nodes). This is like an interpreter. Only in this case I need a world: if parser.MODE is ON: world = World() # --> and tell this to all modules that need it When in OFF mode, nothing runs but the representaton exists as well. Printing the top node's code() should (it's still in progress) print the object code via a cascade of calls to child nodes' code(). This is like a compiler ;-) (But the object language is python!) objectFile.write(parser.parse(source).code()) # ==> translation into python Comments welcome. I know this may look strange, but using my lib it is easy. I just need match action that pass all relevant info when creating code objects. So, why not? (except for performance, sure, but as of now I don't mind, I'm learning the domain.) ________________________________ vit esse estrany ? spir.wikidot.com From eike.welk at gmx.net Sun Mar 28 15:43:22 2010 From: eike.welk at gmx.net (Eike Welk) Date: Sun, 28 Mar 2010 14:43:22 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <4BAE2276.5050807@gmail.com> References: <4BAE2276.5050807@gmail.com> Message-ID: <201003281543.22999.eike.welk@gmx.net> On Saturday March 27 2010 16:21:26 AG wrote: > I apologise in advance for the vagueness of this query, but I am looking > for a decent modern introduction to modelling using Python. > Specifically, I want something that is a good introduction (i.e. doesn't > expect one to already be a maths/ statistics or a programming guru) and > that has an ecology/ environmental science orientation. You should look at the book "Python Scripting for Computational Science" by Hans Petter Langtangen: http://www.amazon.com/Python-Scripting-Computational-Science- Engineering/dp/3540435085 http://books.google.com/books?id=YEoiYr4H2A0C&printsec=frontcover&dq="Python+Scripting+for+Computational+Science"&source=bl&ots=ovp_JKREiY&sig=tJkigCLDqS6voOOjmL4xDxw0roM&hl=en&ei=OlWvS8PmE4r94Aa42vzgDw&sa=X&oi=book_result&ct=result&resnum=5&ved=0CBEQ6AEwBA#v=onepage&q=&f=false It is an introduction to the Python language, and to a big number of tools for numerical computations. The book assumes that you have already some practice in writing computer programs. The book is not oriented towards ecology, the examples are from mechanical engineering. The book is however a bit dated, it's from 2004. Therefore many examples will need to be slightly altered to work with the current versions of the libraries that they use. Alternatively you could ask your question on the Numpy/Scipy mailing lists. These lists are frequented by scientists that use Python for their computations. http://www.scipy.org/Mailing_Lists Eike. From kp8 at mac.com Sun Mar 28 17:44:24 2010 From: kp8 at mac.com (kevin parks) Date: Mon, 29 Mar 2010 00:44:24 +0900 Subject: [Tutor] automatic output file naming scheme Message-ID: <9026FBEE-4951-4875-A7C8-EF9C59DA25D4@mac.com> okay. I got the subprocess bit to work and i have os walk doing its walk. But now for something i did not think about until i started to think about how to fit these two bits to work together. os walk is going to traverse my dirs from some given starting point and process files that it finds that fit my criteria. So i my case it will look for all sound files in a given directly structure and then call sox and do a conversion. This part i can already probably do just by combining the working code that i have ? but now i need to have some kind of automagic naming scheme that creates an output file name that is based on the input name but is unique, either adding a number or suffix before the file extension, or even a time stamp. Since we want to create file names that are unique and not clobber existing files, but also will tells us something meaningful about how the file was created so that finding: foo01.aif or foo.1.aif would yield something like foo-out01.aif or foo01-out.aif or something similar. How can you do such a thing in python? is there some elegant way to take the input file name and combine it with some other string to create the output file name? -kp From emile at fenx.com Sun Mar 28 17:57:56 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 08:57:56 -0700 Subject: [Tutor] "IOError: [Errno 32] Broken pipe" when running python with cron (alternatives?) In-Reply-To: <534486.37306.qm@web44705.mail.sp1.yahoo.com> References: <534486.37306.qm@web44705.mail.sp1.yahoo.com> Message-ID: On 3/27/2010 5:45 AM Karjer Jdfjdf said... > I have made an extensive script that runs fine when started from the command line or IDLE. > > When I try to run it with cron it keeps giving errors: Often when I have trouble running scripts from cron it's because something in the environment is different. Try restructuring the way you start the script from cron. Say your cron entry looks like: * * * * * someone /usr/local/bin/myscript.py Then something like: env > /usr/local/bin/myscript.sh echo /usr/local/bin/myscript.py >> /usr/local/bin/myscript.sh chmod a+x /usr/local/bin/myscript.py and change cron to * * * * * someone /usr/local/bin/myscript.sh and see if that helps. Emile > > Error in sys.exitfunc: > Traceback (most recent call last): > File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs > func(*targs, **kargs) > File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown > h.flush() > File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush > self.stream.flush() > IOError: [Errno 32] Broken pipe > > > The script has the following structure: > 1. retrieves data from database 1 > 2. modifies the data > 3. inserts the modified data in another database > > In a previous script I solved this problem with directing the stdout and stderr to /dev/null, but doing this isn't possible because I use pickle and write some data to files. It seems to have anything to do with the stdout/stderr and cron, but after a lot of googling I don't have any clues how to fix this. > > How can I solve this? Does anybody know how to solve this or is there a python-friendly alternative to cron that I can use. > > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From emile at fenx.com Sun Mar 28 18:11:30 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 09:11:30 -0700 Subject: [Tutor] automatic output file naming scheme In-Reply-To: <9026FBEE-4951-4875-A7C8-EF9C59DA25D4@mac.com> References: <9026FBEE-4951-4875-A7C8-EF9C59DA25D4@mac.com> Message-ID: On 3/28/2010 8:44 AM kevin parks said... > okay. I got the subprocess bit to work and i have os walk doing its walk. But now for something i did not think about until i started to think about how to fit these two bits to work together. > > os walk is going to traverse my dirs from some given starting point and process files that it finds that fit my criteria. So i my case it will look for all sound files in a given directly structure and then call sox and do a conversion. This part i can already probably do just by combining the working code that i have ? but now i need to have some kind of automagic naming scheme that creates an output file name that is based on the input name but is unique, either adding a number or suffix before the file extension, or even a time stamp. Since we want to create file names that are unique and not clobber existing files, but also will tells us something meaningful about how the file was created so that finding: > > foo01.aif or foo.1.aif would yield something like foo-out01.aif or foo01-out.aif or something similar. > > How can you do such a thing in python? is there some elegant way to take the input file name and combine it with some other string to create the output file name? So you've really got two issues -- building a new name to use, and confirming it's unique. You can build a new name with the replace method of strings or concatenating pieces of the new name using join or string interpolation. os.path has some functions you may find helpful: > > -kp > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From emile at fenx.com Sun Mar 28 18:17:43 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 09:17:43 -0700 Subject: [Tutor] automatic output file naming scheme In-Reply-To: <9026FBEE-4951-4875-A7C8-EF9C59DA25D4@mac.com> References: <9026FBEE-4951-4875-A7C8-EF9C59DA25D4@mac.com> Message-ID: On 3/28/2010 8:44 AM kevin parks said... > okay. I got the subprocess bit to work and i have os walk doing its walk. But now for something i did not think about until i started to think about how to fit these two bits to work together. > > os walk is going to traverse my dirs from some given starting point and process files that it finds that fit my criteria. So i my case it will look for all sound files in a given directly structure and then call sox and do a conversion. This part i can already probably do just by combining the working code that i have ? but now i need to have some kind of automagic naming scheme that creates an output file name that is based on the input name but is unique, either adding a number or suffix before the file extension, or even a time stamp. Since we want to create file names that are unique and not clobber existing files, but also will tells us something meaningful about how the file was created so that finding: > > foo01.aif or foo.1.aif would yield something like foo-out01.aif or foo01-out.aif or something similar. > > How can you do such a thing in python? is there some elegant way to take the input file name and combine it with some other string to create the output file name? So you've really got two issues -- building a new name to use, and confirming it's unique. You can build a new name with the replace method of strings or concatenating pieces of the new name using join or string interpolation. >>> sourcefile = "/home/someone/somedir/foo01.aif" >>> sourcefile.replace(".aif","-001.aif") '/home/someone/somedir/foo01-001.aif' or >>> sourcefile.replace(".aif","-%03d.aif" % sequence) os.path has some functions you may find helpful: >>> os.path.split(sourcefile) ('/home/someone/somedir', 'foo01.aif') >>> os.path.basename(sourcefile) 'foo01.aif' >>> os.path.splitext(sourcefile) ('/home/someone/somedir/foo01', '.aif') >>> os.path.exists(sourcefile) False HTH, Emile From emile at fenx.com Sun Mar 28 18:25:01 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 09:25:01 -0700 Subject: [Tutor] First extension In-Reply-To: <98c3f7c51003261903h1130cb07i61985cdd56e9fb39@mail.gmail.com> References: <98c3f7c51003261903h1130cb07i61985cdd56e9fb39@mail.gmail.com> Message-ID: On 3/26/2010 7:03 PM James Reynolds said... > Hello All, > > I'm trying to write my first extension module, and I am getting the > following error in my command prompt and I was hoping you all could help me. Hi James, You'll probably get further asking on the setuptools support list. Checking http://pypi.python.org/pypi/setuptools I see links to the list at http://mail.python.org/pipermail/distutils-sig/ Those are the guys that will want this info and will most likely help you work through it. HTH, Emile > > I have taken the following steps already: > > > 1. My path is set for mingw/bin as well as python31. > 2. There is a file in my disutils folder called disutils.cfg that says > [build] compiler = mingw32 > 3. The instructions in the 3.1 documentation state the following: "These > instructions only apply if you?re using a version of Python prior to 2.4.1 > with a MinGW prior to 3.0.0 (with binutils-2.13.90-20030111- > 1. http://docs.python.org/py3k/install/index.html > 2. I am using Python 3.1 and the latest MinGW. > 4. I tested gcc/mingw by doing C:\python31>gcc -shared pdv.c -o pdv.dll > and the test was succesful (or at least I was not given any errors while > doing the compile) > 5. I searched on the internet and the closest thing I can find is the > following: http://bugs.python.org/issue4709 > > > Below you will find the following > > One, the error report > two,my setup.py file > three, the file I am trying to turn into a python extension module by > running the following two commands: > > python setup.py build > python setup.py install > > > #1 > > > Microsoft Windows [Version 6.1.7600] > > Copyright (c) 2009 Microsoft Corporation. All rights reserved. > > >> c:\Python31\Lib\finance>python setup.py build > > running build > > running build_ext > > building 'finance' extension > > creating build > > creating build\temp.win-amd64-3.1 > > creating build\temp.win-amd64-3.1\Release > > C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python31\include >> -IC:\Pytho > > n31\PC -c finance.c -o build\temp.win-amd64-3.1\Release\finance.o > > finance.c: In function `PyInit_finance': > > finance.c:31: warning: implicit declaration of function `Py_Module_Create' > > finance.c:31: warning: return makes pointer from integer without a cast > > writing build\temp.win-amd64-3.1\Release\finance.def > > creating build\lib.win-amd64-3.1 > > C:\MinGW\bin\gcc.exe -mno-cygwin -shared -s >> build\temp.win-amd64-3.1\Release\fin > > ance.o build\temp.win-amd64-3.1\Release\finance.def -LC:\Python31\libs >> -LC:\Pyth > > on31\PCbuild\amd64 -lpython31 -lmsvcr90 -o >> build\lib.win-amd64-3.1\finance.pyd > > build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x2b): undefined >> ref > > erence to `_imp__PyArg_ParseTuple' > > build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x5c): undefined >> ref > > erence to `_imp__Py_BuildValue' > > build\temp.win-amd64-3.1\Release\finance.o:finance.c:(.text+0x74): undefined >> ref > > erence to `Py_Module_Create' > > collect2: ld returned 1 exit status > > error: command 'gcc' failed with exit status 1 > > >> c:\Python31\Lib\finance> > > > > #2 > > > from distutils.core import setup, Extension > > >> setup(name = "finance", > > version = "1.0", > > ext_modules = [Extension("finance", ["finance.c"])]) > > > > #3 > > #include > > #include > > >> static PyObject * > > pdv(PyObject *self, PyObject *args) > > { > > double value, rate, timex, denom, pdvx; > > if (!PyArg_ParseTuple(args, "ddd",&value,&rate,&timex)) > > return NULL; > > denom = (double) pow ((1 + rate), (timex)); > > pdvx = value / denom; > > return Py_BuildValue("d", pdvx); > > } > > PyMethodDef pdvMethods[] = { > > {"pdv", pdv, METH_VARARGS, "Returns the Present Discounted Value given >> of a single future value"}, > > {NULL, NULL, 0, NULL} > > }; > > >> static struct PyModuleDef financemodule = { > > PyModuleDef_HEAD_INIT, > > "finance", /* name of module */ > > NULL, /* module documentation, may be NULL */ > > -1, /* size of per-interpreter state of the module, > > or -1 if the module keeps state in global variables. */ > > pdvMethods > > }; > > >> PyMODINIT_FUNC > > PyInit_finance(void) > > { > > return Py_Module_Create(&financemodule); > > } > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From shurui91 at gmail.com Sun Mar 28 18:26:21 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 12:26:21 -0400 Subject: [Tutor] ask Message-ID: <2b9003cf1003280926h24aa04e1g6d8761f92c7a4914@mail.gmail.com> Since English is not my native language, so I got confused by some requirement of my assignment. I already done, just want to make sure what I did is what the assignment required, thank you! BTW, I don't know how to do this: If I want to fix some wrong words in a text file, how can I print out the right/fixed text file? Like this: My favor colour is blue. ? My favourite color is blue. (Just print out the right thing directly) Thank you! assignment: 1 Write a Python program named british2american.py that reads the contents of a text file, looks for occurances of uniquely British spelling, and translates the occurances into the acceptes American spelling. 1 Specifically, you will be looking for the following words: colour, analyse, memorise, centre and recognise. If found, they are to be replaced with color, analyze, memorize, center and recognize. 1 The following is needed for you to write your program: 1 the name of the file containing the text to be translated is storyBrit.txt and it is to be located in your cset1100py directory. Copy it from this link to the designated location. 1 your program, british2american.py, should be located in your cset1100py directory on et791 1 the name of the file containing the translated output is storyAmer.txt and it is to located in a subdirectory off your cset1100py directory named assign19, that is ./cset1100py/assign19/storyAmer.txt 1 the original file, storyBrit.txt, should be left unchanges This is british2american.py # Translate wrong British words #Create an empty file print "\nReading characters from the file." raw_input("Press enter then we can move on:") text_file = open("storyBrit.txt", "r+") whole_thing = text_file.read() print whole_thing raw_input("Press enter then we can move on:") print "\nWe are gonna find out the wrong British words." # Press enter and change the wrong words if "colour" in whole_thing: print "The wrong word is 'colour' and the right word is 'color'" if "analyse" in whole_thing: print "the wrong word is 'analyse' and the right word is 'analyze'" if "memorise" in whole_thing: print "the wrong word is 'memorise' and the right word is 'memorize'" if "centre" in whole_thing: print "the wrong word is 'centre' and the right word is 'center'" if "recognise" in whole_thing: print "the wrong word is 'recognise' and the right word is 'recognize'" if "honour" in whole_thing: print "the wrong word is 'honour' and the right word is 'honor'" # We are gonna save the right answer to storyAmer.txt w = open('storyAmer.txt', 'w') w.write('I am really glad that I took CSET 1100.') w.write('\n') w.write('We get to analyse all sorts of real-world problems.\n') w.write('\n') w.write('We also have to memorize some programming language syntax.') w.write('\n') w.write('But, the center of our focus is game programming and it is fun.') w.write('\n') w.write('Our instructor adds color to his lectures that make them interesting.') w.write('\n') w.write('It is an honor to be part of this class!') w.close() This is storyAmer.txt(right one): I am really glad that I took CSET 1100.We get to analyze all sorts of real-world problems.We also have to memorize some programming language syntax. But, the center of our focus is game programming and it is fun.Our instructor adds colour to his lectures that make them interesting.It is an honor to be part of this class! This is storyBrit.txt(wrong one): I am really glad that I took CSET 1100.We get to analise all sorts of real-world problems.We also have to memorise some programming language syntax. But, the centre of our focus is game programming and it is fun.Our instructor adds colour to his lectures that make them interesting.It is an honour to be part of this class! Thank you very much! -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo -------------- next part -------------- An HTML attachment was scrubbed... URL: From shurui91 at gmail.com Sun Mar 28 19:28:54 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 13:28:54 -0400 Subject: [Tutor] ask In-Reply-To: <2b9003cf1003280926h24aa04e1g6d8761f92c7a4914@mail.gmail.com> References: <2b9003cf1003280926h24aa04e1g6d8761f92c7a4914@mail.gmail.com> Message-ID: <2b9003cf1003281028m2de39ea9g2fa33ad83319d4cf@mail.gmail.com> You know what, I just don't understand this line: the name of the file containing the translated output is storyAmer.txt and it is to located. I don't know what kind of translated output he need. I guess: 1. the name of the file containing the translated output is * storyAmer.txt* and it is to located in a subdirectory off your cset1100py *assign19*, that is *./cset1100py/assign19/storyAmer.txt* directory named What kind of translated output do you want? (1) A list of wrong words and a list of right words, like this: Wrong words in the text are: ...... Right words should be: ...... OR (2) The content of the text but has already been translated, like this: I am really glad that I took CSET 1100. We get to *analyze* all sorts of real-world problems. We also have to *memorize* some programming language syntax. But, the *center* of our focus is game programming and it is fun. Our instructor adds *color* to his lectures that make them interesting. It is an honour to be part of this class! ? 2010?3?28? ??12:26?Shurui Liu (Aaron Liu) ??? > Since English is not my native language, so I got confused by some > requirement of my assignment. I already done, just want to make sure what I > did is what the assignment required, thank you! > > BTW, I don't know how to do this: > If I want to fix some wrong words in a text file, how can I print out the > right/fixed text file? Like this: > My favor colour is blue. ? My favourite color is blue. (Just print out the > right thing directly) > > Thank you! > > assignment: > > 1 Write a Python program named british2american.py > that reads the contents of a text file, looks for occurances of uniquely British spelling, and translates the occurances into the acceptes American spelling. > > 1 > Specifically, you will be looking for the following words: colour, analyse, memorise, centre and recognise. If found, they are to be > replaced with color, analyze, memorize, center and recognize. > > 1 The following is needed for you to write your program: > > 1 the name of the file containing the text to be translated is > storyBrit.txt > and it is to be located in your cset1100py directory. Copy it from this > link > to the designated location. > > 1 your program, british2american.py, should be located in your cset1100py > directory on et791 > > 1 the name of the file containing the translated output is storyAmer.txt > and it is to located in a subdirectory off your cset1100py directory named > assign19, that is ./cset1100py/assign19/storyAmer.txt > > 1 the original file, storyBrit.txt, should be left unchanges > This is british2american.py > # Translate wrong British words > #Create an empty file > print "\nReading characters from the file." > raw_input("Press enter then we can move on:") > text_file = open("storyBrit.txt", "r+") > whole_thing = text_file.read() > print whole_thing > raw_input("Press enter then we can move on:") > print "\nWe are gonna find out the wrong British words." > # Press enter and change the wrong words > if "colour" in whole_thing: > print "The wrong word is 'colour' and the right word is 'color'" > if "analyse" in whole_thing: > print "the wrong word is 'analyse' and the right word is 'analyze'" > if "memorise" in whole_thing: > print "the wrong word is 'memorise' and the right word is 'memorize'" > if "centre" in whole_thing: > print "the wrong word is 'centre' and the right word is 'center'" > if "recognise" in whole_thing: > print "the wrong word is 'recognise' and the right word is 'recognize'" > if "honour" in whole_thing: > print "the wrong word is 'honour' and the right word is 'honor'" > # We are gonna save the right answer to storyAmer.txt > w = open('storyAmer.txt', 'w') > w.write('I am really glad that I took CSET 1100.') > w.write('\n') > w.write('We get to analyse all sorts of real-world problems.\n') > w.write('\n') > w.write('We also have to memorize some programming language syntax.') > w.write('\n') > w.write('But, the center of our focus is game programming and it is fun.') > w.write('\n') > w.write('Our instructor adds color to his lectures that make them > interesting.') > w.write('\n') > w.write('It is an honor to be part of this class!') > w.close() > > This is storyAmer.txt(right one): > I am really glad that I took CSET 1100.We get to analyze all sorts of > real-world problems.We also have to memorize some programming language > syntax. But, the center of our focus is game programming and it is fun.Our > instructor adds colour to his lectures that make them interesting.It is an > honor to be part of this class! > > This is storyBrit.txt(wrong one): > I am really glad that I took CSET 1100.We get to analise all sorts of > real-world problems.We also have to memorise some programming language > syntax. But, the centre of our focus is game programming and it is fun.Our > instructor adds colour to his lectures that make them interesting.It is an > honour to be part of this class! > > > Thank you very much! > > > > -- > Shurui Liu (Aaron Liu) > Computer Science & Engineering Technology > University of Toledo > -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo -------------- next part -------------- An HTML attachment was scrubbed... URL: From computing.account at googlemail.com Sun Mar 28 19:37:41 2010 From: computing.account at googlemail.com (AG) Date: Sun, 28 Mar 2010 18:37:41 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <201003281543.22999.eike.welk@gmx.net> References: <4BAE2276.5050807@gmail.com> <201003281543.22999.eike.welk@gmx.net> Message-ID: <4BAF93E5.6060102@gmail.com> Eike Welk wrote: > On Saturday March 27 2010 16:21:26 AG wrote: > >> I apologise in advance for the vagueness of this query, but I am looking >> for a decent modern introduction to modelling using Python. >> Specifically, I want something that is a good introduction (i.e. doesn't >> expect one to already be a maths/ statistics or a programming guru) and >> that has an ecology/ environmental science orientation. >> > > You should look at the book "Python Scripting for Computational Science" by > Hans Petter Langtangen: > http://www.amazon.com/Python-Scripting-Computational-Science- > Engineering/dp/3540435085 > http://books.google.com/books?id=YEoiYr4H2A0C&printsec=frontcover&dq="Python+Scripting+for+Computational+Science"&source=bl&ots=ovp_JKREiY&sig=tJkigCLDqS6voOOjmL4xDxw0roM&hl=en&ei=OlWvS8PmE4r94Aa42vzgDw&sa=X&oi=book_result&ct=result&resnum=5&ved=0CBEQ6AEwBA#v=onepage&q=&f=false > > It is an introduction to the Python language, and to a big number of tools for > numerical computations. The book assumes that you have already some practice > in writing computer programs. > > The book is not oriented towards ecology, the examples are from mechanical > engineering. > > The book is however a bit dated, it's from 2004. Therefore many examples will > need to be slightly altered to work with the current versions of the libraries > that they use. > > > > Alternatively you could ask your question on the Numpy/Scipy mailing lists. > These lists are frequented by scientists that use Python for their > computations. > http://www.scipy.org/Mailing_Lists > > > > Eike. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Now that's looking very much along the lines of what I had in mind Eike. Very pricey ... might have to sit on that one for a while and scout around for a used copy. I can certainly use the on-line resource for as many pages as it allows one to access until I either find a cheaper version or perhaps win a lottery!! But, yep, this looks like what I had in mind - so anything else like this would be good as well. Cheers AG -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sun Mar 28 19:50:59 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 10:50:59 -0700 Subject: [Tutor] ask In-Reply-To: <2b9003cf1003281028m2de39ea9g2fa33ad83319d4cf@mail.gmail.com> References: <2b9003cf1003280926h24aa04e1g6d8761f92c7a4914@mail.gmail.com> <2b9003cf1003281028m2de39ea9g2fa33ad83319d4cf@mail.gmail.com> Message-ID: On 3/28/2010 10:28 AM Shurui Liu (Aaron Liu) said... > You know what, I just don't understand this line: > the name of the file containing the translated output is storyAmer.txt > and it is to located. It sounds to me like we wants you to read in the source(british) version, swap in the american spellings, then write the resulting text to storyAmer.txt You might investigate the replace (1) method of strings and use it along the lines of: whole_thing = whole_thing.replace(british_spelling,american_spelling) Also, look up the parameters for open (2), specifically those related to enabling write mode for the new file. And the write (3) method of an open file. (1) http://docs.python.org/library/stdtypes.html?highlight=replace#str.replace (2) http://docs.python.org/library/functions.html#open (3) http://docs.python.org/library/stdtypes.html?highlight=write#file.write You're almost there it seems. HTH, Emile > > > I don't know what kind of translated output he need. I guess: > > 1. the name of the file containing the translated output is * > storyAmer.txt* and it is to located in a subdirectory off your cset1100py > *assign19*, that is *./cset1100py/assign19/storyAmer.txt* directory named > > > What kind of translated output do you want? > > (1) A list of wrong words and a list of right words, like this: > > Wrong words in the text are: ...... > > Right words should be: ...... > > > > OR > > > > (2) The content of the text but has already been translated, like this: > > > > I am really glad that I took CSET 1100. > We get to *analyze* all sorts of real-world problems. > We also have to *memorize* some programming language syntax. > But, the *center* of our focus is game programming and it is fun. > Our instructor adds *color* to his lectures that make them interesting. > It is an honour to be part of this class! > > > > ? 2010?3?28? ??12:26?Shurui Liu (Aaron Liu)??? > >> Since English is not my native language, so I got confused by some >> requirement of my assignment. I already done, just want to make sure what I >> did is what the assignment required, thank you! >> >> BTW, I don't know how to do this: >> If I want to fix some wrong words in a text file, how can I print out the >> right/fixed text file? Like this: >> My favor colour is blue. ? My favourite color is blue. (Just print out the >> right thing directly) >> >> Thank you! >> >> assignment: >> >> 1 Write a Python program named british2american.py >> that reads the contents of a text file, looks for occurances of uniquely British spelling, and translates the occurances into the acceptes American spelling. >> >> 1 >> Specifically, you will be looking for the following words: colour, analyse, memorise, centre and recognise. If found, they are to be >> replaced with color, analyze, memorize, center and recognize. >> >> 1 The following is needed for you to write your program: >> >> 1 the name of the file containing the text to be translated is >> storyBrit.txt >> and it is to be located in your cset1100py directory. Copy it from this >> link >> to the designated location. >> >> 1 your program, british2american.py, should be located in your cset1100py >> directory on et791 >> >> 1 the name of the file containing the translated output is storyAmer.txt >> and it is to located in a subdirectory off your cset1100py directory named >> assign19, that is ./cset1100py/assign19/storyAmer.txt >> >> 1 the original file, storyBrit.txt, should be left unchanges >> This is british2american.py >> # Translate wrong British words >> #Create an empty file >> print "\nReading characters from the file." >> raw_input("Press enter then we can move on:") >> text_file = open("storyBrit.txt", "r+") >> whole_thing = text_file.read() >> print whole_thing >> raw_input("Press enter then we can move on:") >> print "\nWe are gonna find out the wrong British words." >> # Press enter and change the wrong words >> if "colour" in whole_thing: >> print "The wrong word is 'colour' and the right word is 'color'" >> if "analyse" in whole_thing: >> print "the wrong word is 'analyse' and the right word is 'analyze'" >> if "memorise" in whole_thing: >> print "the wrong word is 'memorise' and the right word is 'memorize'" >> if "centre" in whole_thing: >> print "the wrong word is 'centre' and the right word is 'center'" >> if "recognise" in whole_thing: >> print "the wrong word is 'recognise' and the right word is 'recognize'" >> if "honour" in whole_thing: >> print "the wrong word is 'honour' and the right word is 'honor'" >> # We are gonna save the right answer to storyAmer.txt >> w = open('storyAmer.txt', 'w') >> w.write('I am really glad that I took CSET 1100.') >> w.write('\n') >> w.write('We get to analyse all sorts of real-world problems.\n') >> w.write('\n') >> w.write('We also have to memorize some programming language syntax.') >> w.write('\n') >> w.write('But, the center of our focus is game programming and it is fun.') >> w.write('\n') >> w.write('Our instructor adds color to his lectures that make them >> interesting.') >> w.write('\n') >> w.write('It is an honor to be part of this class!') >> w.close() >> >> This is storyAmer.txt(right one): >> I am really glad that I took CSET 1100.We get to analyze all sorts of >> real-world problems.We also have to memorize some programming language >> syntax. But, the center of our focus is game programming and it is fun.Our >> instructor adds colour to his lectures that make them interesting.It is an >> honor to be part of this class! >> >> This is storyBrit.txt(wrong one): >> I am really glad that I took CSET 1100.We get to analise all sorts of >> real-world problems.We also have to memorise some programming language >> syntax. But, the centre of our focus is game programming and it is fun.Our >> instructor adds colour to his lectures that make them interesting.It is an >> honour to be part of this class! >> >> >> Thank you very much! >> >> >> >> -- >> Shurui Liu (Aaron Liu) >> Computer Science& Engineering Technology >> University of Toledo >> > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From shurui91 at gmail.com Sun Mar 28 20:02:47 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 14:02:47 -0400 Subject: [Tutor] ask In-Reply-To: <2b9003cf1003281049w4c3eee52r43da11c7ca5fa58c@mail.gmail.com> References: <2b9003cf1003280926h24aa04e1g6d8761f92c7a4914@mail.gmail.com> <2b9003cf1003281028m2de39ea9g2fa33ad83319d4cf@mail.gmail.com> <2b9003cf1003281049w4c3eee52r43da11c7ca5fa58c@mail.gmail.com> Message-ID: <2b9003cf1003281102u25b0c540l38e2c58d5f4e59ea@mail.gmail.com> I came out with a transigent answer: save the right text file*(storyAmer.txt *) at the right place /cset1100py/assign19/ storyAmer.txt. Then I just add a command after the command which picked out wrong words. I wanna add text_file = open("storyAmer.txt", "r") But I don't know how to add its path in parentheses. What do u think? 2010/3/28 Shurui Liu (Aaron Liu) > I came out with a transigent answer: save the right text file* > (storyAmer.txt*) at the right place /cset1100py/assign19/storyAmer.txt. > Then I just add a command after the command which picked out wrong words. > > I wanna add > text_file = open("storyAmer.txt", "r") > But I don't know how to add its path in parentheses. > > What do u think? > > 2010/3/28 C M Caine > > 2010/3/28 Shurui Liu (Aaron Liu) : >> > You know what, I just don't understand this line: >> > >> the name of the file containing the translated output is storyAmer.txt and it is to located. >> > >> > I don't know what kind of translated output he need. I guess: >> > >> > the name of the file containing the translated output is storyAmer.txt >> and >> > it is to located in a subdirectory off your cset1100pyassign19, that is >> > ./cset1100py/assign19/storyAmer.txt directory named >> > >> > What kind of translated output do you want? >> > >> > (1) A list of wrong words and a list of right words, like this: >> > >> > Wrong words in the text are: ...... >> > >> > Right words should be: ...... >> > >> > >> > >> > OR >> > >> > >> > >> > (2) The content of the text but has already been translated, like this: >> >> I think he wants (2). >> >> (1) is a fairly trivial task and involves no real processing. >> > > > > -- > Shurui Liu (Aaron Liu) > Computer Science & Engineering Technology > University of Toledo > -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sun Mar 28 21:31:33 2010 From: bgailer at gmail.com (bob gailer) Date: Sun, 28 Mar 2010 15:31:33 -0400 Subject: [Tutor] inter-module global variable In-Reply-To: <20100328113157.67cf7e3e@o> References: <20100328113157.67cf7e3e@o> Message-ID: <4BAFAE95.7050704@gmail.com> On 3/28/2010 5:31 AM, spir ? wrote: > Hello, > > I have a main module importing other modules and defining a top-level variable, call it 'w' [1]. I naively thought that the code from an imported module, when called from main, would know about w, but I have name errors. The initial trial looks as follows (this is just a sketch, the original is too big and complicated): > > # imported "code" module > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser > from code import * > from scope import Scope, World > w = World() > > This pattern failed as said above. So, I tried to "export" w: > > I have read the above several times. I do not comprehend your problem. Perhaps someone else will. If not please try to explain it in a different way. ALWAYS post the traceback. > # imported "code" module > __all__ = ["NameLookup", "Literal", "Assignment", ...] > > # main module > from parser import parser > from scope import Scope, World > w = World() > import code # new > code.w = w ### "export" > from code import * > > And this works. I had the impression that the alteration of the "code" module object would not propagate to objects imported from "code". But it works. But I find this terribly unclear, fragile, and dangerous, for any reason. (I find this "dark", in fact ;-) > Would someone try to explain what actually happens in such case? > Also, why is a global variable not actually global, but in fact only "locally" global (at the module level)? > It's the first time I meet such an issue. What's wrong in my design to raise such a problem, if any? > > My view is a follow: From the transparency point of view (like for function transparency), the classes in "code" should _receive_ as general parameter a pointer to 'w', before they do anything. In other words, the whole "code" module is like a python code chunk parameterized with w. If it would be a program, it would get w as command-line parameter, or from the user, or from a config file. > Then, all instanciations should be done using this pointer to w. Meaning, as a consequence, all code objects should hold a reference to 'w'. This could be made as follows: > > # main module > import code > code.Code.w = w > from code import * > > # "code" module > class Code(object): > w = None ### to be exported from importing module > def __init__(self, w=Code.w): > # the param allows having a different w eg for testing > self.w = w > # for each kind of code things > class CodeThing(Code): > def __init__(self, args): > Code.__init__(self) > ... use args ... > def do(self, args): > ... use args and self.w ... > > But the '###' line looks like an ugly trick to me. (Not the fact that it's a class attribute; as a contrary, I often use them eg for config, and find them a nice tool for clarity.) The issue is that Code.w has to be exported. > Also, this scheme is heavy (all these pointers in every living object.) Actually, code objects could read Code.w directly but this does not change much (and I lose transparency). > It's hard for me to be lucid on this topic. Is there a pythonic way? > > > Denis > > [1] The app is a kind of interpreter for a custom language. Imported modules define classes for objects representing elements of code (literal, assignment, ...). Such objects are instanciated from parse tree nodes (conceptually, they *are* code nodes). 'w' is a kind of global scope -- say the state of the running program. Indeed, most code objects need to read/write in w. > Any comments on this model welcome. I have few knowledge on implementation of languages. > ________________________________ > > vit esse estrany ? > > spir.wikidot.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 919-636-4239 Chapel Hill NC From eike.welk at gmx.net Sun Mar 28 22:49:56 2010 From: eike.welk at gmx.net (Eike Welk) Date: Sun, 28 Mar 2010 21:49:56 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <4BAF93E5.6060102@gmail.com> References: <4BAE2276.5050807@gmail.com> <201003281543.22999.eike.welk@gmx.net> <4BAF93E5.6060102@gmail.com> Message-ID: <201003282249.56409.eike.welk@gmx.net> On Sunday March 28 2010 19:37:41 AG wrote: > Now that's looking very much along the lines of what I had in mind > Eike. Very pricey ... might have to sit on that one for a while and > scout around for a used copy. I can certainly use the on-line resource > for as many pages as it allows one to access until I either find a > cheaper version or perhaps win a lottery!! Maybe you can get it as an inter library loan. Buying it is unnecessary; once you have some knowledge of the tools, you get everywhere you want with the online documentation and some questions in the relevant mailing lists. As an alternative you could try the online documentation at the Scipy website. Numpy and Scipy are the basic libraries for numerical computation with Python. The material at the website is not as good as the Langtangen book, and it covers far fewer different subjects, but it might be good enough to get you started. First read the "Getting Started" section. Then look at the "Cookbook" articles and study some that are relevant for you. http://www.scipy.org/ Additionally subscribe to the Numpy/Scipy mailing lists. You could ask questions how to solve specific problems. The people there are usually very helpful. > > But, yep, this looks like what I had in mind - so anything else like > this would be good as well. I'm very glad that I could be helpful. Eike. From computing.account at googlemail.com Sun Mar 28 23:09:49 2010 From: computing.account at googlemail.com (AG) Date: Sun, 28 Mar 2010 22:09:49 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <201003282249.56409.eike.welk@gmx.net> References: <4BAE2276.5050807@gmail.com> <201003281543.22999.eike.welk@gmx.net> <4BAF93E5.6060102@gmail.com> <201003282249.56409.eike.welk@gmx.net> Message-ID: <4BAFC59D.7020502@gmail.com> Eike Welk wrote: > On Sunday March 28 2010 19:37:41 AG wrote: > >> Now that's looking very much along the lines of what I had in mind >> Eike. Very pricey ... might have to sit on that one for a while and >> scout around for a used copy. I can certainly use the on-line resource >> for as many pages as it allows one to access until I either find a >> cheaper version or perhaps win a lottery!! >> > Maybe you can get it as an inter library loan. Buying it is unnecessary; once > you have some knowledge of the tools, you get everywhere you want with the > online documentation and some questions in the relevant mailing lists. > > The idea of an inter-library loan is a sound one. I hadn't thought of that, so will give that a try. > As an alternative you could try the online documentation at the Scipy website. > Numpy and Scipy are the basic libraries for numerical computation with Python. > The material at the website is not as good as the Langtangen book, and it > covers far fewer different subjects, but it might be good enough to get you > started. > > I've just begun with numpy and matlibplot (pyplot), so these are good suggestions. > First read the "Getting Started" section. Then look at the "Cookbook" articles > and study some that are relevant for you. > http://www.scipy.org/ > > Thanks. These are leads worth following up on. > Additionally subscribe to the Numpy/Scipy mailing lists. You could ask > questions how to solve specific problems. The people there are usually very > helpful. > > >> But, yep, this looks like what I had in mind - so anything else like >> this would be good as well. >> > > I'm very glad that I could be helpful. > > > Eike. > Thank you again. AG -------------- next part -------------- An HTML attachment was scrubbed... URL: From shurui91 at gmail.com Sun Mar 28 23:23:35 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 17:23:35 -0400 Subject: [Tutor] how to change some words in a text file Message-ID: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> Here's the thing, I wanna change some wrong words from a text file like this: "I am a studaet." -> "I am a student." I have tried to use this command: str = "I am a studaet." newstr = str[0:8] + "ent" print newstr But the system said TypeError: 'type' object is unsubscriptable What's wrong? -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From emile at fenx.com Sun Mar 28 23:30:40 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Mar 2010 14:30:40 -0700 Subject: [Tutor] how to change some words in a text file In-Reply-To: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> References: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> Message-ID: On 3/28/2010 2:23 PM Shurui Liu (Aaron Liu) said... > Here's the thing, I wanna change some wrong words from a text file like this: > > "I am a studaet." -> "I am a student." > > I have tried to use this command: > str = "I am a studaet." > newstr = str[0:8] + "ent" > print newstr > > > But the system said TypeError: 'type' object is unsubscriptable > > What's wrong? > str is an unfortunate variable name choice -- try oldstr instead. What you've done is often called shadowing. I don't get the error here, but I'm guessing that's because I'm on 2.6 and you're on 3.x? Emile From anand.shashwat at gmail.com Sun Mar 28 23:31:24 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Mon, 29 Mar 2010 03:01:24 +0530 Subject: [Tutor] how to change some words in a text file In-Reply-To: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> References: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> Message-ID: >>> s = "I am a studaet." >>> new_s = s[0:8] + 'ent' >>> print new_s I am a sent You are using str as a variable, str is used to convert anything into string >>> a = 0 >>> b = str(a) >>> type(b) On Mon, Mar 29, 2010 at 2:53 AM, Shurui Liu (Aaron Liu) wrote: > Here's the thing, I wanna change some wrong words from a text file like > this: > > "I am a studaet." -> "I am a student." > > I have tried to use this command: > str = "I am a studaet." > newstr = str[0:8] + "ent" > print newstr > > > But the system said TypeError: 'type' object is unsubscriptable > > What's wrong? > > -- > Shurui Liu (Aaron Liu) > Computer Science & Engineering Technology > University of Toledo > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eike.welk at gmx.net Sun Mar 28 23:36:56 2010 From: eike.welk at gmx.net (Eike Welk) Date: Sun, 28 Mar 2010 22:36:56 +0100 Subject: [Tutor] Introduction to modelling with Python In-Reply-To: <201003282249.56409.eike.welk@gmx.net> References: <4BAE2276.5050807@gmail.com> <4BAF93E5.6060102@gmail.com> <201003282249.56409.eike.welk@gmx.net> Message-ID: <201003282336.56814.eike.welk@gmx.net> Also bookmark this page, it will be very useful once you have a little knowledge about Numpy: http://www.scipy.org/Numpy_Example_List_With_Doc This is the page that I use most often. Eike. From shurui91 at gmail.com Sun Mar 28 23:37:50 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 17:37:50 -0400 Subject: [Tutor] how to change some words in a text file In-Reply-To: References: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> Message-ID: <2b9003cf1003281437o61f8aabfkc21c55a58b59174f@mail.gmail.com> Tell me how to do what i want, please. 2010/3/28 Shashwat Anand : >>>> s = "I am a studaet." >>>> new_s = s[0:8] + 'ent' >>>> print new_s > I am a sent > > You are using str as a variable, str is used to convert anything into string > >>>> a? = 0 >>>> b = str(a) >>>> type(b) > > > > On Mon, Mar 29, 2010 at 2:53 AM, Shurui Liu (Aaron Liu) > wrote: >> >> Here's the thing, I wanna change some wrong words from a text file like >> this: >> >> "I am a studaet." -> "I am a student." >> >> I have tried to use this command: >> str = "I am a studaet." >> newstr = str[0:8] + "ent" >> print newstr >> >> >> But the system said ? ? TypeError: 'type' object is unsubscriptable >> >> What's wrong? >> >> -- >> Shurui Liu (Aaron Liu) >> Computer Science & Engineering Technology >> University of Toledo >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From Tom_Roche at pobox.com Mon Mar 29 01:51:23 2010 From: Tom_Roche at pobox.com (Tom Roche) Date: Sun, 28 Mar 2010 19:51:23 -0400 Subject: [Tutor] bind line-oriented device output? Message-ID: <87y6hcovic.fsf@pobox.com> I'd like to learn to pythonically redirect the output from a line-oriented character device to a particular file or process, regardless of focus, on a generic graphical OS. But I don't want to redirect stdin entirely. Here's the usecase: My school has a seminar for which we record attendance by scanning the student ID card, or rather the barcode on its back, with a handheld USB scanner. This is pretty straightforward, except that the barcode scanner is like a keyboard writing the ASCII equivalent of the barcode (== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm not sure if EOL is \r, \n, or \r\n, but I suspect the latter. Since the scanner is connected to an ordinary multiprocessing laptop on which one will likely be doing other things while scanning (notably setting up to record the presenter), it sometimes happens (especially until one learns to pay attention to this) that one writes to a frame other than the text file into which we want record attendee ID#s. This semester recurs every {fall, spring}, so someone faces this {pitfall, learning curve} at regular intervals. How to prevent writing the wrong target? One trivial solution--shlep a dedicated USB host for the scanner--is deprecated. An OS-specific solution (e.g. relying on a linux raw device) is also undesirable: I use ubuntu, but others will probably use mac or windows. Rather, It Would Be Nice, and useful for this seminar's mechanics, to be able to run some code to which one could say, see this device? and this file? Make the device's output go only to that file. For extra credit, don't let anything else write that file while this code is running. Can python do that? Or does one need to get closer to the metal? TIA, Tom Roche From wprins at gmail.com Mon Mar 29 02:02:57 2010 From: wprins at gmail.com (Walter Prins) Date: Mon, 29 Mar 2010 02:02:57 +0200 Subject: [Tutor] how to change some words in a text file In-Reply-To: <2b9003cf1003281437o61f8aabfkc21c55a58b59174f@mail.gmail.com> References: <2b9003cf1003281423g5e2b73b8r73344c0a5aaf2554@mail.gmail.com> <2b9003cf1003281437o61f8aabfkc21c55a58b59174f@mail.gmail.com> Message-ID: <7d961c461003281702g29fec73dia7fdb264eb16e4dd@mail.gmail.com> On 28 March 2010 23:37, Shurui Liu (Aaron Liu) wrote: > Tell me how to do what i want, please. > Both me and someone else have already told you how to replace a substring in a string with something else in your earlier question/posting/thread. If you've not understood what was written there, then please ask again and specify what specifically you're having difficulty with. If however you've not yet looked into/tried out what we've suggested, then please do try that first, and then come back if you have problems. -------------- next part -------------- An HTML attachment was scrubbed... URL: From shurui91 at gmail.com Mon Mar 29 02:38:22 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Sun, 28 Mar 2010 20:38:22 -0400 Subject: [Tutor] commands Message-ID: <2b9003cf1003281738m745a735cybfef99ed249909a2@mail.gmail.com> # Translate wrong British words #Create an empty file print "\nReading characters from the file." raw_input("Press enter then we can move on:") text_file = open("storyBrit.txt", "r+") whole_thing = text_file.read() print whole_thing raw_input("Press enter then we can move on:") print "\nWe are gonna find out the wrong British words." corrections = {'colour':'color', 'analyse':'analyze', 'memorise':'memorize', 'centre':'center', 'recognise':'recognize', 'honour':'honor'} texto = whole_thing for a in corrections: texto = texto.replace(a, corrections[a]) print texto # Press enter and change the wrong words if "colour" in whole_thing: print "The wrong word is 'colour' and the right word is 'color'" if "analyse" in whole_thing: print "the wrong word is 'analyse' and the right word is 'analyze'" if "memorise" in whole_thing: print "the wrong word is 'memorise' and the right word is 'memorize'" if "centre" in whole_thing: print "the wrong word is 'centre' and the right word is 'center'" if "recognise" in whole_thing: print "the wrong word is 'recognise' and the right word is 'recognize'" if "honour" in whole_thing: print "the wrong word is 'honour' and the right word is 'honor'" # We are gonna save the right answer to storyAmer.txt w = open('storyAmer.txt', 'w') w.write('I am really glad that I took CSET 1100.') w.write('\n') w.write('We get to analyse all sorts of real-world problems.\n') w.write('\n') w.write('We also have to memorize some programming language syntax.') w.write('\n') w.write('But, the center of our focus is game programming and it is fun.') w.write('\n') w.write('Our instructor adds color to his lectures that make them interesting.') w.write('\n') w.write('It is an honor to be part of this class!') w = open("assign19/storyAmer.txt", "w") w.close() This is what I have done, I don't understand why this program cannot fix "analyse". -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From davea at ieee.org Mon Mar 29 03:32:57 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 28 Mar 2010 20:32:57 -0500 Subject: [Tutor] inter-module global variable In-Reply-To: <20100328151547.099a5462@o> References: <20100328113157.67cf7e3e@o> <201003282150.47499.steve@pearwood.info> <20100328151547.099a5462@o> Message-ID: <4BB00349.7080501@ieee.org> spir # wrote: > On Sun, 28 Mar 2010 21:50:46 +1100 > Steven D'Aprano wrote: > > >> On Sun, 28 Mar 2010 08:31:57 pm spir ? wrote: >> >> I'm going to assume you really want a single global value, and that you won't regret that assumption later. We talked at length about how to access that global from everywhere that cares, and my favorite way is with a globals module. And it should be assigned something like: globals.py: class SomeClass (object): def.... def init(parameters): global world world = SomeClass(parameters, moreparamaters) Then main can do the following: import globals globals.init(argv-stuff) And other modules can then do import globals.world as world And they'll all see the same world variable. Nobody should have their own, but just import it if needed. From davea at ieee.org Mon Mar 29 04:01:36 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 28 Mar 2010 21:01:36 -0500 Subject: [Tutor] commands In-Reply-To: <2b9003cf1003281738m745a735cybfef99ed249909a2@mail.gmail.com> References: <2b9003cf1003281738m745a735cybfef99ed249909a2@mail.gmail.com> Message-ID: <4BB00A00.2040106@ieee.org> Shurui Liu (Aaron Liu) wrote: > # Translate wrong British words > > #Create an empty file > print "\nReading characters from the file." > raw_input("Press enter then we can move on:") > text_file = open("storyBrit.txt", "r+") > whole_thing = text_file.read() > print whole_thing > raw_input("Press enter then we can move on:") > print "\nWe are gonna find out the wrong British words." > corrections = {'colour':'color', 'analyse':'analyze', > 'memorise':'memorize', 'centre':'center', 'recognise':'recognize', > 'honour':'honor'} > texto = whole_thing > for a in corrections: > texto = texto.replace(a, corrections[a]) > print texto > > # Press enter and change the wrong words > if "colour" in whole_thing: > print "The wrong word is 'colour' and the right word is 'color'" > if "analyse" in whole_thing: > print "the wrong word is 'analyse' and the right word is 'analyze'" > if "memorise" in whole_thing: > print "the wrong word is 'memorise' and the right word is 'memorize'" > if "centre" in whole_thing: > print "the wrong word is 'centre' and the right word is 'center'" > if "recognise" in whole_thing: > print "the wrong word is 'recognise' and the right word is 'recognize'" > if "honour" in whole_thing: > print "the wrong word is 'honour' and the right word is 'honor'" > > # We are gonna save the right answer to storyAmer.txt > w = open('storyAmer.txt', 'w') > w.write('I am really glad that I took CSET 1100.') > w.write('\n') > w.write('We get to analyse all sorts of real-world problems.\n') > w.write('\n') > w.write('We also have to memorize some programming language syntax.') > w.write('\n') > w.write('But, the center of our focus is game programming and it is fun.') > w.write('\n') > w.write('Our instructor adds color to his lectures that make them interesting.') > w.write('\n') > w.write('It is an honor to be part of this class!') > w = open("assign19/storyAmer.txt", "w") > > w.close() > > > > This is what I have done, I don't understand why this program cannot > fix "analyse". > > You do some work in texto, and never write it to the output file. Instead you write stuff you hard-coded in literal strings in your program. And you never fixed the mode field of the first open() function, as someone hinted at you. And you don't specify the output file location, but just assume it's in the current directory. For that matter, your open of the input file assumes it's in the current directory as well. But your assignment specified where both files would/should be. DaveA From waynejwerner at gmail.com Mon Mar 29 14:03:02 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 29 Mar 2010 07:03:02 -0500 Subject: [Tutor] bind line-oriented device output? In-Reply-To: <87y6hcovic.fsf@pobox.com> References: <87y6hcovic.fsf@pobox.com> Message-ID: <333efb451003290503n1f87d24dy20e7c159b5aad885@mail.gmail.com> On Sun, Mar 28, 2010 at 6:51 PM, Tom Roche wrote: > > I'd like to learn to pythonically redirect the output from a > line-oriented character device to a particular file or process, > regardless of focus, on a generic graphical OS. But I don't want to > redirect stdin entirely. Here's the usecase: > > My school has a seminar for which we record attendance by scanning the > student ID card, or rather the barcode on its back, with a handheld > USB scanner. This is pretty straightforward, except that the barcode > scanner is like a keyboard writing the ASCII equivalent of the barcode > (== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm > not sure if EOL is \r, \n, or \r\n, but I suspect the latter. > > Since the scanner is connected to an ordinary multiprocessing laptop on > which one will likely be doing other things while scanning (notably > setting up to record the presenter), it sometimes happens (especially > until one learns to pay attention to this) that one writes to a frame > other than the text file into which we want record attendee ID#s. This > semester recurs every {fall, spring}, so someone faces this {pitfall, > learning curve} at regular intervals. > > How to prevent writing the wrong target? One trivial solution--shlep a > dedicated USB host for the scanner--is deprecated. An OS-specific > solution (e.g. relying on a linux raw device) is also undesirable: I > use ubuntu, but others will probably use mac or windows. > > Rather, It Would Be Nice, and useful for this seminar's mechanics, to > be able to run some code to which one could say, see this device? and > this file? Make the device's output go only to that file. For extra > credit, don't let anything else write that file while this code is > running. > > Can python do that? Or does one need to get closer to the metal? > It's certainly possible. Technically you can do /anything/, it's just a matter of knowing how to do it. Whether or not it's worth doing is something completely beside the point. OTOH, if you can assure that the target computer will be running a certain set of python features (i.e. Tkinter/pyGTK+/wxPython, etc) you can fairly easily write a program that will run fullscreen and continually grab focus to whatever widget (in this case a text entry field) you want. with pyGTK you could do something like this: gobject.timeout_add(100, myentrywidget.grab_focus) the function at the end may be different - perhaps you write your own function to comply with the timeout_add requirements - but it it will continually (every 100ms) grab the "keyboard" focus. OTOH, if the device is something like the CueCat and reports a control character (I think it's alt+f7 for the cuecat), then at least on linux/windows I think it's much more trivial. You just have to register a global hotkey handler that will send the focus to that particular window. There are probably a few other approaches to this problem, but those are two of the "simplest". HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From lowelltackett at yahoo.com Mon Mar 29 14:57:06 2010 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Mon, 29 Mar 2010 05:57:06 -0700 (PDT) Subject: [Tutor] python magazine Message-ID: <83948.55278.qm@web110108.mail.gq1.yahoo.com> >From the virtual desk of Lowell Tackett --- On Sun, 3/28/10, Lowell Tackett wrote: > From: Lowell Tackett > Subject: Re: [Tutor] python magazine > To: "Dave Angel" > Cc: "Benno Lang" , tutor at python.org > Date: Sunday, March 28, 2010, 7:46 AM > > From the virtual desk of Lowell Tackett > > > > --- On Sat, 3/27/10, Dave Angel > wrote: > > > From: Dave Angel > > Subject: Re: [Tutor] python magazine > > To: "Lowell Tackett" > > Cc: "Benno Lang" , > tutor at python.org > > Date: Saturday, March 27, 2010, 6:12 AM > > > > > > Lowell Tackett wrote: > > > >From the virtual desk of Lowell Tackett > > > > > > > --- On Fri, 3/26/10, Benno Lang > > wrote: > > > > > > From: Benno Lang > > > Subject: Re: [Tutor] python magazine > > > To: "Lowell Tackett" > > > Cc: tutor at python.org, > > "Bala subramanian" > > > Date: Friday, March 26, 2010, 8:38 PM > > > > > > On 27 March 2010 00:33, Lowell Tackett > > wrote: > > > > > >> The Python Magazine people have now got a > Twitter > > site--which includes a perhaps [telling] misspelling. > > >> > > > Obviously that's why they're looking for a chief > > editor - maybe it's > > > even a deliberate ploy. > > > > > > I'm not sure if this affects others, but to me > your > > replies appear > > > inside the quoted section of your mail, rather > than > > beneath it. Would > > > you mind writing plain text emails to avoid this > > issue? > > > > > > Thanks, > > > benno > > > > > > Like this...? > > > > > > > > > > > No, there's still a problem. You'll notice in > this > > message that there are ">" symbols in front of your > lines > > and benno's, and ">>" symbols in front of > > Lowell's. (Some email readers will turn the > > into > > vertical bar, but the effect is the same). Your > email > > program should be adding those upon a reply, so that > your > > own message has one less > than the one to which > you're > > replying. Then everyone reading can see who > wrote > > what, based on how many ">" or bars precede the > > respective lines. Quotes from older messages > have more > > of them. > > > > Are you using "Reply-All" in your email program? > Or > > are you constructing a new message with copy/paste? > > > > What email are you using? Maybe it's a > configuration > > setting somebody could help with. > > > > DaveA > > > > > Don't really know what I'm doing wrong (or right). > Just using the [email] tools that have been made available > to me thru Yahoo mail and Firefox. I began this text > below your submission and "signature", and I'm using plain > text, as suggested by a previous comment. Don't know > what else I could embellish this effort with. > > > > I noticed one other thing in my response (having noticed it also in someone else's recent correspondence). Apparently...my email package does not create its' own eol signals. In contrast, your (Dave Angel) message holds the same eol format as it did in the copy that came to my mail box. My messages however, stretch [each line] out to the full width of the available physical page format (to which they are submitted). For what it's worth, I am typing this in the "Plain Text" option of my email template. Gmane handles that predicament very gracefully, but over in "The Tutor Archives", my [message] lines sometime stretch [from my computer screen] halfway out into my kitchen. Occasionally, I will see that occur with others' emailed offerings. It's beyond me[?]. It's all the fault of Yahoo mail and Firefox...I'm just an innocent bystander in all of this. From Tom_Roche at pobox.com Mon Mar 29 17:56:03 2010 From: Tom_Roche at pobox.com (Tom Roche) Date: Mon, 29 Mar 2010 11:56:03 -0400 Subject: [Tutor] bind line-oriented device output? In-Reply-To: <333efb451003290503n1f87d24dy20e7c159b5aad885@mail.gmail.com> References: <87y6hcovic.fsf@pobox.com> <333efb451003290503n1f87d24dy20e7c159b5aad885@mail.gmail.com> Message-ID: <87vdcfp1f0.fsf@pobox.com> http://mail.python.org/pipermail/tutor/2010-March/075438.html >> I'd like to learn to pythonically redirect the output from a >> line-oriented character device to a particular file or process, >> regardless of focus, on a generic graphical OS. But I don't want to >> redirect stdin entirely. ... >> It Would Be Nice, and useful for this seminar's mechanics, to be >> able to run some code to which one could say, see this device? and >> this file? Make the device's output go only to that file. For extra >> credit, don't let anything else write that file while this code is >> running. http://mail.python.org/pipermail/tutor/2010-March/075443.html > if you can assure that the target computer will be running a certain > set of python features (i.e. Tkinter/pyGTK+/wxPython, etc) Presuming they're available on the user's platform and not too onerous to install, I can definitely say, "if you want , you need to install ." > you can fairly easily write a program that will run fullscreen and > continually grab focus to whatever widget (in this case a text entry > field) you want. *Continually* grabbing focus to a UI is functionally (from the user's point of view) equivalent to redirecting stdin, no? The user is made unable to do anything but take attendance for the duration of the task. In fact, for this usecase, continually grabbing focus is worse, since the user would be unable to, e.g., read the directions for setting up the audio. To clarify the usecase: The barcode scanner (here, a Wasp WLS9500) is connected to an ordinary multiprocessing laptop (which may be linux, mac, or windows) on which the user will want to do other things while taking attendance: the attendees don't come in all at once, and the user must accomplish other tasks while taking attendance (notably setting up to record the seminar audio). Hence the desired application 0 is portable. 1 does not redirect all of stdin, only the output from the scanner. The user is allowed to continue to work on other tasks (e.g. with output from the laptop's keyboard going to whatever frame), with only the output from the scanner being bound to a particular file or process. 2 does not continually grab focus. The user is allowed to continue to work on other tasks (e.g. with output from the laptop's keyboard going to whatever frame), with only scanner events being bound to a particular frame or widget. But what might satisfy the usecase is > if the device is something like the CueCat and reports a control > character (I think it's alt+f7 for the cuecat), then at least on > linux/windows I think it's much more trivial. You just have to > register a global hotkey handler that will send the focus to that > particular window. I'll read the Product Reference Guide and hope it tells me that the Wasp WLS9500 "reports a control character." Unfortunately when I search the PDF http://tinyurl.com/waspWLS9500manual for "control character" I get no hits. Are there synonyms for that term in this context? TIA, Tom Roche From waynejwerner at gmail.com Mon Mar 29 19:52:31 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 29 Mar 2010 12:52:31 -0500 Subject: [Tutor] bind line-oriented device output? In-Reply-To: <87vdcfp1f0.fsf@pobox.com> References: <87y6hcovic.fsf@pobox.com> <333efb451003290503n1f87d24dy20e7c159b5aad885@mail.gmail.com> <87vdcfp1f0.fsf@pobox.com> Message-ID: <333efb451003291052y1cf78d69n2c4227dba7f27afa@mail.gmail.com> On Mon, Mar 29, 2010 at 10:56 AM, Tom Roche wrote: > I'll read the Product Reference Guide and hope it tells me that the > Wasp WLS9500 "reports a control character." Unfortunately when I > search the PDF > > http://tinyurl.com/waspWLS9500manual > > for "control character" I get no hits. Are there synonyms for that > term in this context? > > Take a look at the bottom of 8-5, it has some prefix/suffix values. Apparently you can set it to send and it looks like by perusing the manual and probably a bit of trial and error you can set it up to send one of any number of CTRL characters as the prefix. I don't know of any windows program that uses ctrl-] as a hotkey though I'm sure there are some. For Python registering hotkeys, a quick google search pulled up these: http://www.islascruz.org/html/index.php/blog/show/HotKeysonWindows.html http://bytes.com/topic/python/answers/574341-how-create-global-hotkey (mainly lots of links to other sites) I wasn't able to find anything super definitive on mac, though, which is too bad. Anyhow, HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From fiyawerx at gmail.com Tue Mar 30 04:43:55 2010 From: fiyawerx at gmail.com (Fiyawerx) Date: Mon, 29 Mar 2010 22:43:55 -0400 Subject: [Tutor] bind line-oriented device output? In-Reply-To: <87vdcfp1f0.fsf@pobox.com> References: <87y6hcovic.fsf@pobox.com> <333efb451003290503n1f87d24dy20e7c159b5aad885@mail.gmail.com> <87vdcfp1f0.fsf@pobox.com> Message-ID: <1b31ae501003291943o7caa9f29k3adaf4ac509019a3@mail.gmail.com> On Mon, Mar 29, 2010 at 11:56 AM, Tom Roche wrote: > > http://mail.python.org/pipermail/tutor/2010-March/075438.html > >> I'd like to learn to pythonically redirect the output from a > >> line-oriented character device to a particular file or process, > >> regardless of focus, on a generic graphical OS. But I don't want to > >> redirect stdin entirely. > ... > >> It Would Be Nice, and useful for this seminar's mechanics, to be > >> able to run some code to which one could say, see this device? and > >> this file? Make the device's output go only to that file. For extra > >> credit, don't let anything else write that file while this code is > >> running. > > 1 does not redirect all of stdin, only the output from the scanner. > The user is allowed to continue to work on other tasks (e.g. with > output from the laptop's keyboard going to whatever frame), with > only the output from the scanner being bound to a particular file or > process. > > This may be out of my league, but what about pyUSB? ( http://pyusb.sourceforge.net/docs/1.0/tutorial.html) Possibly a program that just runs in the background, waits for input from the usb scanner, and appends the id to a specified file when it catches it? That way you also don't need to keep the file open the whole time, in case someone accidentally forgets to plug it in or shuts down while the program is still writing? I'm sure there are a lot of better ways to handle the actual file writing, but thought it might help. Just a noobs $.02 -------------- next part -------------- An HTML attachment was scrubbed... URL: From modi.oshan at gmail.com Mon Mar 29 04:00:45 2010 From: modi.oshan at gmail.com (Oshan Modi) Date: Mon, 29 Mar 2010 07:30:45 +0530 Subject: [Tutor] help Message-ID: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> i am only a novice and just started programming.. i am having trouble running a .py file in the command prompt.. if anyone of you could help? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Tue Mar 30 10:16:19 2010 From: dextrous85 at gmail.com (vishwajeet singh) Date: Tue, 30 Mar 2010 13:46:19 +0530 Subject: [Tutor] help In-Reply-To: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> Message-ID: <5487b3061003300116r2a5f8e86pfb55135372e8dbac@mail.gmail.com> On Mon, Mar 29, 2010 at 7:30 AM, Oshan Modi wrote: > i am only a novice and just started programming.. i am having trouble > running a .py file in the command prompt.. if anyone of you could help? > How are you trying to run it ?? -- Vishwajeet Singh +91-9657702154 | dextrous85 at gmail.com | http://singhvishwajeet.com | http://bootstraptoday.com Twitter: http://twitter.com/vishwajeets | LinkedIn: http://www.linkedin.com/in/singhvishwajeet -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Tue Mar 30 12:15:54 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 30 Mar 2010 12:15:54 +0200 Subject: [Tutor] help In-Reply-To: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> Message-ID: <4BB1CF5A.2090406@compuscan.co.za> Oshan Modi wrote: > i am only a novice and just started programming.. i am having trouble > running a .py file in the command prompt.. if anyone of you could help? > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > If you are using Windows and just installed Python and then tried to execute your script with "python script_name.py" and it tells you it cannot find the program to execute then it is likely your environment settings as the Python installer never seems to set up your path properly. Go to My Computer -> Advanced Settings -> Environment Settings -> Double-Click on path -> Add your python path eg. c:\python26 at the end -> Restart and it should work. -- Kind Regards, Christian Witts From steve at pearwood.info Tue Mar 30 12:17:25 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 30 Mar 2010 21:17:25 +1100 Subject: [Tutor] help In-Reply-To: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> Message-ID: <201003302117.26034.steve@pearwood.info> On Mon, 29 Mar 2010 01:00:45 pm Oshan Modi wrote: > i am only a novice and just started programming.. i am having trouble > running a .py file in the command prompt.. if anyone of you could > help? Are you running Linux or Mac? At the command prompt, run: python myfile.py and report any errors. You can also run: which python and see what it says. -- Steven D'Aprano From cwitts at compuscan.co.za Tue Mar 30 13:25:28 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 30 Mar 2010 13:25:28 +0200 Subject: [Tutor] help In-Reply-To: References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> <4BB1CF5A.2090406@compuscan.co.za> Message-ID: <4BB1DFA8.8080305@compuscan.co.za> Forwarding to the list. Martijn wrote: > > On Tue, Mar 30, 2010 at 12:15 PM, Christian Witts > > wrote: > > > Oshan Modi wrote: > > > i am only a novice and just started programming.. i am having > trouble running a .py file in the command prompt.. if anyone > of you could help? > > ------------------------------------------------------------------------ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > If you are using Windows and just installed Python and then tried > to execute your script with "python script_name.py" and it tells > you it cannot find the program to execute then it is likely your > environment settings as the Python installer never seems to set up > your path properly. > > Go to My Computer -> Advanced Settings -> Environment Settings -> > Double-Click on path -> Add your python path eg. c:\python26 at > the end > > > I think it should be capitalized, but I'm not sure, so: > For Python 2.6: > C:\Python26 > For Python 3.1: > C:\Python31 > If you didn't install it on the C: drive, change C: to the drive > letter you installed it on. > > Also, check if there's a semicolon ( ; ) at the end of the path before > you add the python path, if there isn't, add it first. Now you can > continue :) > > Martijn > > > -> Restart and it should work. > > -- > Kind Regards, > Christian Witts > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > Windows is case insensitive when it comes to paths and executables names so it shouldn't matter. The semi-colon I forgot to mention though. :) -- Kind Regards, Christian Witts From cwitts at compuscan.co.za Tue Mar 30 14:08:09 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Tue, 30 Mar 2010 14:08:09 +0200 Subject: [Tutor] help In-Reply-To: <2371589b1003300441w35abb9a2pacd41dcefdfa7f65@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> <4BB1CF5A.2090406@compuscan.co.za> <4BB1DFA8.8080305@compuscan.co.za> <2371589b1003300441w35abb9a2pacd41dcefdfa7f65@mail.gmail.com> Message-ID: <4BB1E9A9.6020008@compuscan.co.za> Oshan Modi wrote: > I have Windows 7 and am using python 2.6.. I added python.exe's > address in the path option under enviromental variables.. i can run > python in command prompt.. its just that i dont know to make it > execute a file directly rather than typing the whole program again and > again.. > > On Tue, Mar 30, 2010 at 4:55 PM, Christian Witts > > wrote: > > Forwarding to the list. > > Martijn wrote: > > > On Tue, Mar 30, 2010 at 12:15 PM, Christian Witts > > >> wrote: > > Oshan Modi wrote: > > i am only a novice and just started programming.. i am > having > trouble running a .py file in the command prompt.. if > anyone > of you could help? > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > > > > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > If you are using Windows and just installed Python and then > tried > to execute your script with "python script_name.py" and it > tells > you it cannot find the program to execute then it is likely > your > environment settings as the Python installer never seems to > set up > your path properly. > Go to My Computer -> Advanced Settings -> Environment > Settings -> > Double-Click on path -> Add your python path eg. c:\python26 at > the end > > I think it should be capitalized, but I'm not sure, so: > For Python 2.6: > C:\Python26 > For Python 3.1: > C:\Python31 > If you didn't install it on the C: drive, change C: to the > drive letter you installed it on. > Also, check if there's a semicolon ( ; ) at the end of the > path before you add the python path, if there isn't, add it > first. Now you can continue :) > Martijn > > > -> Restart and it should work. > -- Kind Regards, > Christian Witts > _______________________________________________ > Tutor maillist - Tutor at python.org > > > > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > Windows is case insensitive when it comes to paths and executables > names so it shouldn't matter. The semi-colon I forgot to mention > though. :) > > -- > Kind Regards, > Christian Witts > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Is this perhaps what you want to do ? http://www.python.org/doc/faq/windows/#how-do-i-make-python-scripts-executable -- Kind Regards, Christian Witts From arts.martijn at gmail.com Tue Mar 30 15:33:42 2010 From: arts.martijn at gmail.com (Martijn) Date: Tue, 30 Mar 2010 15:33:42 +0200 Subject: [Tutor] help In-Reply-To: <4BB1E9A9.6020008@compuscan.co.za> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> <4BB1CF5A.2090406@compuscan.co.za> <4BB1DFA8.8080305@compuscan.co.za> <2371589b1003300441w35abb9a2pacd41dcefdfa7f65@mail.gmail.com> <4BB1E9A9.6020008@compuscan.co.za> Message-ID: Simple; just write python program.py in your command line :) On Tue, Mar 30, 2010 at 2:08 PM, Christian Witts wrote: > Oshan Modi wrote: > >> I have Windows 7 and am using python 2.6.. I added python.exe's address in >> the path option under enviromental variables.. i can run python in command >> prompt.. its just that i dont know to make it execute a file directly rather >> than typing the whole program again and again.. >> >> >> On Tue, Mar 30, 2010 at 4:55 PM, Christian Witts > cwitts at compuscan.co.za>> wrote: >> >> Forwarding to the list. >> >> Martijn wrote: >> >> >> On Tue, Mar 30, 2010 at 12:15 PM, Christian Witts >> >> > >> wrote: >> Oshan Modi wrote: >> i am only a novice and just started programming.. >> i am >> having >> trouble running a .py file in the command prompt.. if >> anyone >> of you could help? >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> > >> > >> >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> If you are using Windows and just >> installed Python and then >> tried >> to execute your script with "python script_name.py" and it >> tells >> you it cannot find the program to execute then it is likely >> your >> environment settings as the Python installer never seems to >> set up >> your path properly. >> Go to My Computer -> Advanced Settings -> Environment >> Settings -> >> Double-Click on path -> Add your python path eg. c:\python26 at >> the end >> >> I think it should be capitalized, but I'm not sure, so: >> For Python 2.6: >> C:\Python26 >> For Python 3.1: >> C:\Python31 >> If you didn't install it on the C: drive, change C: to the >> drive letter you installed it on. >> Also, check if there's a semicolon ( ; ) at the end of the >> path before you add the python path, if there isn't, add it >> first. Now you can continue :) >> Martijn >> >> -> Restart and it should work. >> -- Kind Regards, >> Christian Witts >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> > >> > >> >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> Windows is case insensitive when it comes to paths and executables >> names so it shouldn't matter. The semi-colon I forgot to mention >> though. :) >> >> -- Kind Regards, >> Christian Witts >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> > Is this perhaps what you want to do ? > > http://www.python.org/doc/faq/windows/#how-do-i-make-python-scripts-executable > > > -- > Kind Regards, > Christian Witts > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc at marcd.org Tue Mar 30 16:01:23 2010 From: marc at marcd.org (Marc) Date: Tue, 30 Mar 2010 14:01:23 +0000 Subject: [Tutor] USB Access Message-ID: <1597897596-1269957684-cardhu_decombobulator_blackberry.rim.net-1555964798-@bda2811.bisx.prod.on.blackberry> Hi, I was wondering if anyone could point me to Python modules or example code for accessing USB connected devices. I would like to get as close to the hardware as possible with Python. I would like to be able to monitor as well as control USB connected devices. I've looked at the missile launcher code by Pedram Amini at dvlabs.tippingpoint.com, but am looking to see what else has been done in this area. thanks, Marc Sent from my Verizon Wireless BlackBerry From damontimm at gmail.com Tue Mar 30 16:27:43 2010 From: damontimm at gmail.com (Damon Timm) Date: Tue, 30 Mar 2010 10:27:43 -0400 Subject: [Tutor] Script Feedback Message-ID: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> As a self-taught Python user I am still looking for insight on the most pythonic and programmatically-friendly way of accomplishing a given task. In this case, I have written a script that will perform a ?clean bzip2? of a directory (or directories). Mac OS X (via AFP and netatalk, in my case) tends leaves a bunch of ugly files/directories hanging around and I would rather not include them in my compressed tar file. In writing the script, though, I ran into some questions and I am not sure what the recommended approach would be. The script works, as it is, but I feel its a little hacked together and also a little limited in its application. There is something to be said for programs that "just work" (this does) but I want to take it a little further as an educational endeavor and would like it to appear robust, future-thinking, and pythonic. My initial questions are: 1. Is there a better way to implement a --quiet flag? 2. I am not very clear on the use of Exceptions (or even if I am using it in a good way here) ? is what I have done the right approach? 3. Finally, in general: any feedback on how to improve this? (I am thinking, just now, that the script is only suitable for a command line usage, and couldn?t be imported by another script, for example.) Any feedback is greatly appreciated. Writing a script like this is a good learning tool (for me, at least). I have posted this email online if you want to see the script with pretty code formatting: http://blog.damontimm.com/python-script-clean-bzip/ Thanks for any insight you may provide. Damon Script follows ************************ #! /usr/bin/env python '''Script to perform a "clean" bzip2 on a directory (or directories). Removes extraneous files that are created by Apple/AFP/netatalk before compressing. ''' import os import tarfile from optparse import OptionParser IGNORE_DIRS = ( '.AppleDouble', ) IGNORE_FILES = ('.DS_Store', ) class DestinationTarFileExists(Exception): '''If the destination tar.bz2 file already exists.''' def ignore_walk(directory): '''Ignore defined files and directories when doing the walk.''' for dirpath, dirnames, filenames in os.walk(directory): dirnames[:] = [ dn for dn in dirnames if dn not in IGNORE_DIRS ] filenames[:] = [ fn for fn in filenames if fn not in IGNORE_FILES ] yield dirpath, dirnames, filenames def tar_bzip2_directories(directories): for directory in directories: file_name = '-'.join(directory.split(' ')) tar_name = file_name.replace('/','').lower() + ".tar.bz2" if os.path.exists(tar_name): raise DestinationTarFileExists() if not options.quiet: print 'Compressing files into: ' + tar_name tar = tarfile.open(tar_name, 'w:bz2') for dirpath, dirnames, filenames in ignore_walk(directory): for file in filenames: if not options.quiet: print os.path.join(dirpath, file) tar.add(os.path.join(dirpath, file)) tar.close() if __name__ == "__main__": parser = OptionParser(usage="%prog [options: -q ] [directory]") parser.add_option("-q", "--quiet", action="store_true", dest="quiet") options, args = parser.parse_args() directories = [] for arg in args: if os.path.isdir(arg): directories.append(arg) else: print "Ingoring: %s (it's not a directory)." % arg try: tar_bzip2_directories(directories) except DestinationTarFileExists: print "A tar file already exists this this directory name." print "Move or rename it and try again." From waynejwerner at gmail.com Tue Mar 30 16:33:30 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Tue, 30 Mar 2010 09:33:30 -0500 Subject: [Tutor] USB Access In-Reply-To: <1597897596-1269957684-cardhu_decombobulator_blackberry.rim.net-1555964798-@bda2811.bisx.prod.on.blackberry> References: <1597897596-1269957684-cardhu_decombobulator_blackberry.rim.net-1555964798-@bda2811.bisx.prod.on.blackberry> Message-ID: <333efb451003300733n283dac13nf5980668fbc17d97@mail.gmail.com> On Tue, Mar 30, 2010 at 9:01 AM, Marc wrote: > Hi, > I was wondering if anyone could point me to Python modules or example code > for accessing USB connected devices. I would like to get as close to the > hardware as possible with Python. I would like to be able to monitor as > well as control USB connected devices. I've looked at the missile launcher > code by Pedram Amini at dvlabs.tippingpoint.com, but am looking to see > what else has been done in this area. > > I've used pyserial, I believe, to directly access the usb. Here's the important connection bits of my code: import serial class pyDuin: ser = serial.Serial() def __init__(self, port="/dev/ttyUSB0", baud="115200", timeout=0): self.ser.baudrate = baud self.ser.port = port self.ser.timeout = timeout def ready(self): self.ser.write("%c" % 41) def open(self): self.ser.open() if not self.ser.isOpen(): print "Error, not able to open serial connection on %s" % \ (self.ser.port, ) ------------- I was using it to connect to my arduino. If you'd like the rest of the source, let me know. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From mibaker88 at gmail.com Tue Mar 30 17:23:44 2010 From: mibaker88 at gmail.com (Mike Baker) Date: Tue, 30 Mar 2010 10:23:44 -0500 Subject: [Tutor] How do I find information about a Python object. Message-ID: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> Hi, I've downloaded IDLE python for windows. I've also downloaded Eclipse with the python addition. I have simple programs that will run on both IDLE and Eclipse. How do I get more information about a object/variable, such as proc in the example below. For example, if I execute the following: >>> proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Catt'], shell=False) I can remote log into our Linux machine named 'Catt'. How do I find a list of attributes for the object/variable proc? I've tried subprocess.__doc__ and subprocess.Popen.__doc__. Random Googling shows that there are things like process identification numbers available - such as proc.pid. How do I find the other options? Thanks, Mike -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Tue Mar 30 17:30:37 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 30 Mar 2010 11:30:37 -0400 Subject: [Tutor] How do I find information about a Python object. In-Reply-To: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> References: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> Message-ID: You might want to check out the below chapter from Dive Into Python: http://diveintopython.org/power_of_introspection/index.html From fomcl at yahoo.com Tue Mar 30 17:36:47 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 30 Mar 2010 08:36:47 -0700 (PDT) Subject: [Tutor] How do I find information about a Python object. In-Reply-To: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> Message-ID: <847171.7972.qm@web110702.mail.gq1.yahoo.com> Hi, Are you looking for the inspect module? ? import inspect help(inspect) cls = str inspect.classify_class_attrs(cls) Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Tue, 3/30/10, Mike Baker wrote: From: Mike Baker Subject: [Tutor] How do I find information about a Python object. To: tutor at python.org Date: Tuesday, March 30, 2010, 5:23 PM Hi, ? I've downloaded IDLE python for windows.? I've also downloaded Eclipse with the python addition. I have simple programs that will run on both IDLE and Eclipse. How do I get more information about a object/variable, such as proc in the example below. ? For example, if I execute the following: ??? >>> proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Catt'], ??????????????????????? shell=False) ????????? I can remote log into our Linux machine named 'Catt'. ? ? How do I find a list of attributes for the object/variable proc?? I've tried subprocess.__doc__ and subprocess.Popen.__doc__. ? Random Googling shows that there?are things?like process?identification numbers available -?such as?proc.pid.? How do I find the other options? ? Thanks, ? Mike -----Inline Attachment Follows----- _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Mar 30 17:37:22 2010 From: emile at fenx.com (Emile van Sebille) Date: Tue, 30 Mar 2010 08:37:22 -0700 Subject: [Tutor] How do I find information about a Python object. In-Reply-To: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> References: <66563b0e1003300823m4af76ba1nf58b1316d48eae89@mail.gmail.com> Message-ID: On 3/30/2010 8:23 AM Mike Baker said... > Random Googling shows that there are things like process identification > numbers available - such as proc.pid. How do I find the other options? try dir(proc) or help(proc) for starters... Emile From mibaker88 at gmail.com Tue Mar 30 18:29:00 2010 From: mibaker88 at gmail.com (Mike Baker) Date: Tue, 30 Mar 2010 11:29:00 -0500 Subject: [Tutor] Remote access from Windows PC to a Linux box Message-ID: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> Hi, I'm trying to connect to a Linux box from my Windows machine and execute a series of commands - (ls, pwd, cat 'somefile', etc...). I'm using Putty to do the ssh and have set up with Putty's Pagent agent to allow me to enter a passphrase once per session to handle security keys between the two boxes (so, no passwords needed for my remote scripts). I have code that will give me a remote prompt on the Linux machine where I can manually enter commands. This works great, but I want a script to always execute the same series of commands without having to do so manually. I also have code that will execute a single command like cat a file and write the ouput to a new file. However, when I try to use the communicate object in subprocess, my window hangs. Here is my working code: # module name data_collect.y # import subprocess def simp_tst0(s_name): # Opens a remote connection to "s_name and gives a prompt. # Works great for executing linux commands. # Does not exit gracefully when you type exit. The python # prompt hangs when it gets to the r.communicate command # cmmnd_0="C:\\Progra~1\\putty\\plink %s" % s_name r = subprocess.Popen("%s" % cmmnd_0,shell=False) (r_stdout, r_stderr) = r.communicate("dir") #status=r.poll() #Locks up if you try to poll here print r_stdout return r def cat_remote(s_name, file2cat): # This simple test file opens a remote connection to "s_name", does a cat on # file "file2cat" and writes the cat to an output file (out2.txt). cmmnd_2="C:\\Progra~1\\putty\\plink %s cat %s" % (s_name, file2cat) q = subprocess.Popen("%s" % cmmnd_2, stdout=open('out2.txt','w')) def simp_tst3(s_name): # Runs the initial subprocess.Popen command - creates proc. # Hangs when you try to use proc.communicate proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Sula'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) #Either of the next two commands cause window to hang #proc.stdin.write("dir") #(stdout_value, stderr_value) = proc.communicate(input="dir")[0] return proc Thanks, Mike -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Mar 30 18:57:54 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 31 Mar 2010 03:57:54 +1100 Subject: [Tutor] Script Feedback In-Reply-To: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> References: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> Message-ID: <201003310357.55520.steve@pearwood.info> On Wed, 31 Mar 2010 01:27:43 am Damon Timm wrote: [...] > My initial questions are: > > 1. Is there a better way to implement a --quiet flag? I usually create a function "print_" or "pr", something like this: def print_(obj, verbosity=1): if verbosity > 0: print obj and then have a variable "verbosity" which defaults to 1 and is set to 0 if the user passes the --quiet flag. Then in my code, I write: print_("this is a message", verbosity) > 2. I am not very clear on the use of Exceptions (or even if I am > using it in a good way here) ? is what I have done the right > approach? Hmmm... perhaps. Usually, you want to catch an exception in order to try an alternative approach, e.g.: try: result = somefunction(x) except ValueError: # Fall back on some other approach. result = "something else" Occasionally, you want to catch an exception just to ignore it. It's generally *not* a good idea to catch an exception just to print an error message and then exit, as you do. Just let the exception propagate, and Python will print a rich and detailed traceback together with your error message. However, a reasonable exception (pun intended) for that rule is to hide the traceback from the users, who probably can't do anything about it, and would only get confused by the details. So you want to have your functions and classes raise exceptions, and the application layer (the user interface) catch them. So I would do something like this (untested). In your function code: ... ? ? if os.path.exists(tar_name): msg = "A tar file already exists this this directory name." \ ? ? ? " Move or rename it and try again." ? ? ? ? raise DestinationTarFileExists(msg) Then your application layer looks something like: if __name__ == '__main__': try: ... except KeyboardInterrupt: sys.exit(1) except DestinationTarFileExists, e: print e.message sys.exit(2) # Any other exception is unexpected, so we allow Python # to print the full traceback as normal. > 3. Finally, in general: any feedback on how to improve > this? (I am thinking, just now, that the script is only suitable for > a command line usage, and couldn?t be imported by another script, for > example.) Separate the underlying functionality from the application-level code. These functions should NEVER print anything: they do all communication through call-backs, or by returning a value, or raising an exception. E.g.: def tar_bzip2_directories(directories, callback=None): for directory in directories: file_name = '-'.join(directory.split(' ')) tar_name = file_name.replace('/','').lower() + ".tar.bz2" if os.path.exists(tar_name): raise DestinationTarFileExists(errmsg) if callback is not None: callback(directory, filename, tarname) ... Create a main function that runs your application: def main(argv=None, callback=None): if argv is None: argv = sys.argv process_command_line_options(argv) if callback is None: def callback(dirname, filename, tarname): print "Processing ..." tar_bzip2_directories(...) if __name__ == '__main__': try: main() except ... # as above Now anyone can import the module and call individual functions, or even call main, or they can run it as a script. Hope this helps, -- Steven D'Aprano From fai_991 at yahoo.com.hk Tue Mar 30 19:00:48 2010 From: fai_991 at yahoo.com.hk (Yahoo Mail) Date: Wed, 31 Mar 2010 01:00:48 +0800 Subject: [Tutor] Need some help on "How to Think Like a Computer Scientist: Learning with Python" exercise Message-ID: Hello All, I am competely new in Python programming. When i reading Chapter 4 in "How to Think Like a Computer Scientist: Learning with Python" , I am stuck in the exercise 4. Here is the question: Enter the following expressions into the Python shell: 1. True or False 2. True and False 3. not(False) and True 4. True or 7 5. False or 7 6. True and 0 7. False or 8 8. "happy" and "sad" 9. "happy" or "sad" 10. "" and "sad" 11. "happy" and "" Analyze these results. What observations can you make about values of different types and logical operators? Can you write these observations in the form of simple rules about and and or expressions? I have no problem with 1-4, but compelely wrong with the rest. Like question 5, i throught the answer is True, but when i type it in IDLE, I got 7 instead, question 8 "happy' and 'sad', my answer is True, but the answer is 'happy'. Can you please tell me why i am wrong. I really appreciate any assistance you can give. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Mar 30 19:12:18 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 30 Mar 2010 13:12:18 -0400 Subject: [Tutor] help In-Reply-To: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> Message-ID: <4BB230F2.6080209@gmail.com> On 3/28/2010 10:00 PM, Oshan Modi wrote: > i am only a novice and just started programming.. i am having trouble > running a .py file in the command prompt.. if anyone of you could help? Please learn how to ask questions. In a situation like this we'd like to know what operating system you are using, which version of Python, what you are typing at the command prompt, and what happens. Otherwise we must guess, as several of us have already done. This wastes all our time. Please post answers to: operating system version of Python what you type at the command prompt what happens -- Bob Gailer 919-636-4239 Chapel Hill NC From shantanoo at gmail.com Tue Mar 30 19:18:36 2010 From: shantanoo at gmail.com (=?utf-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Tue, 30 Mar 2010 22:48:36 +0530 Subject: [Tutor] Remote access from Windows PC to a Linux box In-Reply-To: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> References: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> Message-ID: You can have a look @ paramiko library. http://www.lag.net/paramiko/ On 30-Mar-2010, at 9:59 PM, Mike Baker wrote: > Hi, > > I'm trying to connect to a Linux box from my Windows machine and execute a series of commands - (ls, pwd, cat 'somefile', etc...). I'm using Putty to do the ssh and have set up with Putty's Pagent agent to allow me to enter a passphrase once per session to handle security keys between the two boxes (so, no passwords needed for my remote scripts). > > I have code that will give me a remote prompt on the Linux machine where I can manually enter commands. This works great, but I want a script to always execute the same series of commands without having to do so manually. I also have code that will execute a single command like cat a file and write the ouput to a new file. However, when I try to use the communicate object in subprocess, my window hangs. > > Here is my working code: > # module name data_collect.y > # > import subprocess > > def simp_tst0(s_name): > # Opens a remote connection to "s_name and gives a prompt. > # Works great for executing linux commands. > # Does not exit gracefully when you type exit. The python > # prompt hangs when it gets to the r.communicate command > # > cmmnd_0="C:\\Progra~1\\putty\\plink %s" % s_name > r = subprocess.Popen("%s" % cmmnd_0,shell=False) > (r_stdout, r_stderr) = r.communicate("dir") > #status=r.poll() #Locks up if you try to poll here > print r_stdout > return r > > def cat_remote(s_name, file2cat): > # This simple test file opens a remote connection to "s_name", does a cat on > # file "file2cat" and writes the cat to an output file (out2.txt). > cmmnd_2="C:\\Progra~1\\putty\\plink %s cat %s" % (s_name, file2cat) > q = subprocess.Popen("%s" % cmmnd_2, stdout=open('out2.txt','w')) > > > def simp_tst3(s_name): > # Runs the initial subprocess.Popen command - creates proc. > # Hangs when you try to use proc.communicate > proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Sula'], > shell=True, > stdin=subprocess.PIPE, > stdout=subprocess.PIPE, > ) > #Either of the next two commands cause window to hang > #proc.stdin.write("dir") > #(stdout_value, stderr_value) = proc.communicate(input="dir")[0] > return proc > > Thanks, > > Mike > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From CulverHC at usa-spaceops.com Tue Mar 30 14:03:30 2010 From: CulverHC at usa-spaceops.com (Culver, Hunt C) Date: Tue, 30 Mar 2010 08:03:30 -0400 Subject: [Tutor] help (Oshan Modi) with command prompt Message-ID: <88257531109DF947958A83DA4232F0FD0432095E@usaflcms05.usa-spaceops.ksc.nasa.gov> You didn't say what operating system, but in general terms, the python application has to know how to find your python module, say 'foo.py'. This means that the directory where foo.py is located must be on the system path or PYTHONPATH. One simple way to do this is to navigate at the command prompt to the directory that contains your module, and then execute "python foo.py". In this way python uses the current directory as the current working directory (cwd); python starts looking in this directory for your module. Another way is to examine what's on your system path. At the command prompt, launch python and then enter these two commands: >>> import sys >>> for i in sys.path: print i You'll get a list something like this. This list is where python is looking for foo.py. If not in one of these directories, python probably won't find your module (in general). C:\Python25\Lib\idlelib C:\Python25\lib\site-packages\ets-3.2.0-py2.5.egg C:\Python25\lib\site-packages\etsdevtools-3.0.2-py2.5-win32.egg C:\Python25\lib\site-packages\etsprojecttools-0.5.0-py2.5.egg C:\Python25\lib\site-packages\mayavi-3.2.0-py2.5-win32.egg C:\Python25\lib\site-packages\scimath-3.0.3-py2.5-win32.egg C:\Python25\lib\site-packages\setupdocs-1.0.2-py2.5.egg C:\Python25\lib\site-packages\traits-3.1.0-py2.5-win32.egg C:\WINNT\system32\python25.zip C:\Python25\DLLs C:\Python25\lib C:\Python25\lib\plat-win C:\Python25\lib\lib-tk C:\Python25 C:\Python25\lib\site-packages C:\Python25\lib\site-packages\itk\module\Python C:\Python25\lib\site-packages\PIL C:\Python25\lib\site-packages C:\Python25\lib\site-packages\gtk-2.0 C:\Python25\lib\site-packages\win32 C:\Python25\lib\site-packages\win32\lib C:\Python25\lib\site-packages\Pythonwin Hunt From denis.spir at gmail.com Tue Mar 30 19:41:34 2010 From: denis.spir at gmail.com (spir =?UTF-8?B?4pij?=) Date: Tue, 30 Mar 2010 19:41:34 +0200 Subject: [Tutor] Script Feedback In-Reply-To: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> References: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> Message-ID: <20100330194134.6ac25d76@o> On Tue, 30 Mar 2010 10:27:43 -0400 Damon Timm wrote: > As a self-taught Python user I am still looking for insight on the > most pythonic and programmatically-friendly way of accomplishing a > given task. In this case, I have written a script that will perform a > ?clean bzip2? of a directory (or directories). Mac OS X (via AFP and > netatalk, in my case) tends leaves a bunch of ugly files/directories > hanging around and I would rather not include them in my compressed > tar file. > > In writing the script, though, I ran into some questions and I am not > sure what the recommended approach would be. The script works, as it > is, but I feel its a little hacked together and also a little limited > in its application. There is something to be said for programs that > "just work" (this does) but I want to take it a little further as an > educational endeavor and would like it to appear robust, > future-thinking, and pythonic. Very good point of view, imo. Be aware that some of the notes below are personal opinions. > My initial questions are: > > 1. Is there a better way to implement a --quiet flag? I don't think so. > 2. I am not very clear on the use of Exceptions (or even if I am using > it in a good way here) ? is what I have done the right approach? You can consider exceptions, in general, as a kind of intra-program (interruption-) signal system. When an exception occurs, the normal execution flow is stopped, and the program branches to a special routine designed to cope with the signal -- if any. Else it stops with a message on the terminal (actually on stderr). There are python builtin exception types, most errors; if you don't catch them, then the user gets the kind of error message you're probably familiar with ;-) If you do, by wrapping the potentially interrupting bit of code into a try...except construct, then the occurrence of an error lets the program jump into the except routine: this is a sophisticated and specialised goto. You can design your own exception types that work the same way. Just record needed info in __init__ and write a meaningful message in __str__ (see below). Custom exception types can be used for (1) real errors = "abnormal" cases (2) exceptional, but "normal", cases (like in your case) (3) any need for signaling between separate parts of your code. > 3. Finally, in general: any feedback on how to improve this? (I am > thinking, just now, that the script is only suitable for a command > line usage, and couldn?t be imported by another script, for example.) Yo. Just do it! (the "if __name__=="__main__:" part won't be executed on import -- write a func, eg clean(), that can be called from importing code -- then when it runs stand-alone the main part could just call it after parsing the command-line args) > Any feedback is greatly appreciated. Writing a script like this is a > good learning tool (for me, at least). > > I have posted this email online if you want to see the script with > pretty code formatting: > http://blog.damontimm.com/python-script-clean-bzip/ > > Thanks for any insight you may provide. > > Damon > > Script follows > ************************ > > #! /usr/bin/env python > > '''Script to perform a "clean" bzip2 on a directory (or directories). Removes > extraneous files that are created by Apple/AFP/netatalk before compressing. > ''' > > import os > import tarfile > from optparse import OptionParser > > IGNORE_DIRS = ( '.AppleDouble', ) > IGNORE_FILES = ('.DS_Store', ) +++ for using constants > class DestinationTarFileExists(Exception): > '''If the destination tar.bz2 file already exists.''' def __init__(self, filename): self.filename = filename def __str__(self, filename): ''' (just an example) ''' return 'Destination tar file "%s" already exists.' %(self.filename) By defining the "magic" methods above, you store relevant info about an exception, and get an output format for it. __str__ is doubly magic for an exception, because (in addition to define its str(), which can often be useful), it's also automatically output when the exception falls through uncaught (written after the exception type). > def ignore_walk(directory): > '''Ignore defined files and directories when doing the walk.''' > for dirpath, dirnames, filenames in os.walk(directory): > dirnames[:] = [ dn for dn in dirnames if dn not in IGNORE_DIRS ] > filenames[:] = [ fn for fn in filenames if fn not in IGNORE_FILES ] > yield dirpath, dirnames, filenames First, it seems you use [:] only to preserves the object identity so that it remains a generator. But it may be better (at least clearer for me) to filter and transform the generation process so as to get what you actually need, I guess: iterating on (dirpath,filename) pairs. If I'm right on this, maybe try to figure out how do that. I would call the func eg "filtered_dir_walk" or "relevant_dir_walk". > def tar_bzip2_directories(directories): > for directory in directories: > file_name = '-'.join(directory.split(' ')) > tar_name = file_name.replace('/','').lower() + ".tar.bz2" > > if os.path.exists(tar_name): > raise DestinationTarFileExists() > > if not options.quiet: > print 'Compressing files into: ' + tar_name > > tar = tarfile.open(tar_name, 'w:bz2') > > for dirpath, dirnames, filenames in ignore_walk(directory): > for file in filenames: > if not options.quiet: > print os.path.join(dirpath, file) > > tar.add(os.path.join(dirpath, file)) --> see remark above. Could be: for (dirpath, filename) in ignore_walk(directory): if not options.quiet: print os.path.join(dirpath, filename) tar.add(os.path.join(dirpath, filename)) > tar.close() > > if __name__ == "__main__": > > parser = OptionParser(usage="%prog [options: -q ] [directory]") > parser.add_option("-q", "--quiet", action="store_true", dest="quiet") > options, args = parser.parse_args() > > directories = [] > > for arg in args: > if os.path.isdir(arg): > directories.append(arg) > else: > print "Ingoring: %s (it's not a directory)." % arg > > try: > tar_bzip2_directories(directories) > except DestinationTarFileExists: > print "A tar file already exists this this directory name." > print "Move or rename it and try again." Here you can use exception info and/or str(). Maybe let a choice for overwrite/skip/stop for any existing name. Denis ________________________________ vit esse estrany ? spir.wikidot.com From damontimm at gmail.com Wed Mar 31 01:54:34 2010 From: damontimm at gmail.com (Damon Timm) Date: Tue, 30 Mar 2010 19:54:34 -0400 Subject: [Tutor] Script Feedback In-Reply-To: <20100330194134.6ac25d76@o> References: <262679b51003300727q3c6c6b65p4776880633a51384@mail.gmail.com> <20100330194134.6ac25d76@o> Message-ID: <262679b51003301654r983e843m61e06887e48cf6d@mail.gmail.com> Hello Denis & Steven - Thanks for your replies. I have taken another stab at things to try and bring it a little further up to snuff ... some more comments/thoughts follow ... On Tue, Mar 30, 2010 at 12:57 PM, Steven D'Aprano wrote: > I usually create a function "print_" or "pr", something like this: > > def print_(obj, verbosity=1): > if verbosity > 0: > print obj > > and then have a variable "verbosity" which defaults to 1 and is set to 0 > if the user passes the --quiet flag. Then in my code, I write: > > print_("this is a message", verbosity) Your suggestion prompted me to remember having looked into this earlier (and found an old thread of mine) -- some folks had recommended using the logging module -- which I have implemented in round two (seems to work). I think it accomplishes the same thing that you are suggesting, only using one of Python's built-ins. On Tue, Mar 30, 2010 at 12:57 PM, Steven D'Aprano wrote: > Separate the underlying functionality from the application-level code. > These functions should NEVER print anything: they do all communication > through call-backs, or by returning a value, or raising an exception. I tried to implement this, however, I am not sure how the 'callback' works ... is that just a function that a user would pass to *my* function that gets called at the end of the script? Also, I tried to separate out the logic a little so the functions make more sense ... I think I may remove the 'ignore_walk' function and just add it to the tar_bz2_directory function (see below) ... but am still unclear about the callback concept. 2010/3/30 spir ? : > First, it seems you use [:] only to preserves the object identity so that it remains a generator. But it may be better (at least clearer for me) to filter and transform the generation process so as to get what you actually need, I guess: iterating on (dirpath,filename) pairs. If I'm right on this, maybe try to figure out how do that. > I would call the func eg "filtered_dir_walk" or "relevant_dir_walk". I am not sure where I first got this 'ignore_walk' bit but I do remember taking it from another program of mine ... to be honest, though, I am rethinking its use and may implement it using fnmatch testing so that I may implement wildcards (eg, *.pyc) ... right now, it won't match wildcards and that might be helpful. Again, thank you both for your feedback. I made some changes tonight (posted below) and also updated the changes on: http://blog.damontimm.com/python-script-clean-bzip/ (if you want pretty colors). Damon CODE BELOW ------------------- #! /usr/bin/env python '''Script to perform a "clean" bzip2 on a directory (or directories). Removes extraneous files that are created by Apple/AFP/netatalk before compressing. ''' import os import tarfile import logging from optparse import OptionParser # Default files and directories to exclude from the bzip tar IGNORE_DIRS = ('.AppleDouble',) IGNORE_FILES = ('.DS_Store',) class DestinationTarFileExists(Exception): '''If the destination tar.bz2 file already exists.''' def ignore_walk(directory, ignore_dirs=None, ignore_files=None): '''Ignore defined files and directories when doing the walk.''' # TODO: this does not currently take wild cards into account. For example, # if you wanted to exclude *.pyc files ... should fix that. Perhaps # consider moving this entirely into the below function (or making it more # reusable for other apps). for dirpath, dirnames, filenames in os.walk(directory): if ignore_dirs: dirnames[:] = [dn for dn in dirnames if dn not in ignore_dirs] if ignore_files: filenames[:] = [fn for fn in filenames if fn not in ignore_files] yield dirpath, dirnames, filenames def tar_bzip2_directory(directory, ignore_dirs=IGNORE_DIRS, ignore_files=IGNORE_FILES ): '''Takes a directory and creates a tar.bz2 file (based on the directory name). You can exclude files and sub-directories as desired.''' file_name = '-'.join(directory.split(' ')) tar_name = file_name.replace('/','').lower() + ".tar.bz2" if os.path.exists(tar_name): msg = ("The file %s already exists. " + "Please move or rename it and try again.") % tar_name raise DestinationTarFileExists(msg) tar = tarfile.open(tar_name, 'w:bz2') for dirpath, dirnames, filenames in ignore_walk(directory, ignore_dirs, ignore_files): for file in filenames: logging.info(os.path.join(dirpath, file)) tar.add(os.path.join(dirpath, file)) tar.close() def main(args=None, callback=None): directories = [] for arg in args: if os.path.isdir(arg): directories.append(arg) else: logging.ERROR("Ingoring: %s (it's not a directory)." % arg) for dir in directories: try: tar_bzip2_directory(dir) except DestinationTarFileExists, e: print e if __name__ == "__main__": parser = OptionParser(usage="%prog [options: -q ] [dir1] [...dir2]") parser.add_option("-q", "--quiet", action="store_true", dest="quiet") options, args = parser.parse_args() if options.quiet: logging.basicConfig(level=logging.ERROR, format='%(message)s') else: logging.basicConfig(level=logging.INFO, format='%(message)s') main(args) From lie.1296 at gmail.com Wed Mar 31 03:14:54 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 31 Mar 2010 12:14:54 +1100 Subject: [Tutor] Need some help on "How to Think Like a Computer Scientist: Learning with Python" exercise In-Reply-To: References: Message-ID: On 03/31/2010 04:00 AM, Yahoo Mail wrote: > Hello All, > > I am competely new in Python programming. When i reading Chapter 4 in > "How to Think Like a Computer Scientist: Learning with Python" , > I am stuck in the exercise 4. > > Here is the question: > > Enter the following expressions into the Python shell: > 1. True or False > 2. True and False > 3. not(False) and True > 4. True or 7 > 5. False or 7 > 6. True and 0 > 7. False or 8 > 8. "happy" and "sad" > 9. "happy" or "sad" > 10. "" and "sad" > 11. "happy" and "" > Analyze these results. What observations can you make about values of > different types and logical operators? Can you write these observations > in the form of simple /rules/ about and and or expressions? > > I have no problem with 1-4, but compelely wrong with the rest. Like > question 5, i throught the answer is True, but when i type it in IDLE, > I got 7 instead, question 8 "happy' and 'sad', my answer is True, but > the answer is 'happy'. Can you please tell me why i am wrong. I really > appreciate any assistance you can give. This is python's flavor of short-circuiting. Read the doc at: >>> help("BOOLEAN") From lie.1296 at gmail.com Wed Mar 31 03:40:36 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 31 Mar 2010 12:40:36 +1100 Subject: [Tutor] Remote access from Windows PC to a Linux box In-Reply-To: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> References: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> Message-ID: On 03/31/2010 03:29 AM, Mike Baker wrote: > Hi, > > I'm trying to connect to a Linux box from my Windows machine and execute > a series of commands - (ls, pwd, cat 'somefile', etc...). I'm using > Putty to do the ssh and have set up with Putty's Pagent agent to allow > me to enter a passphrase once per session to handle security keys > between the two boxes (so, no passwords needed for my remote scripts). > > I have code that will give me a remote prompt on the Linux machine where > I can manually enter commands. This works great, but I want a script to > always execute the same series of commands without having to do so > manually.. I also have code that will execute a single command like > cat a file and write the ouput to a new file. However, when I try to use > the communicate object in subprocess, my window hangs. > Seeing your case, probably a simple shell script on the server side would be an easier option. Whenever you ssh to the server, you just execute this startup script. You may also be able to configure putty to execute this script automatically, though since I never used putty I don't know if putty can do that. Or alternatively, you can create a .bashrc (or whatever the remote box's terminal default startup script is). From marcodrompre at gmail.com Wed Mar 31 04:26:22 2010 From: marcodrompre at gmail.com (=?ISO-8859-1?Q?Marco_Rompr=E9?=) Date: Tue, 30 Mar 2010 22:26:22 -0400 Subject: [Tutor] Simple bank account oriented object Message-ID: Hi im doin a programmin course at university and in one on my exercise i have to do that I had to define a class CompteBancaire(CompteBancaire is bankaccount in french), that would allow me to create objects Compte1, Compte2,etc. The following methods need to be defined - depot(somme) would allow me to add cash to my account balance - retrait(somme) would allow me to withdraw some cash from my account - ajouterInterest() would allow me to add interest - affiche() would allow me to display the account owner and his account balance the retrait(somme) method is not supposed to let the account balance being negative. 3.2 Create a sub-class to CompteBancaire and name it CompteEtudiant. Each CompteEtudiant (student account) should have a $1000CAD credit line. Write a builder or constructor for this new class. Surcharge the retrait(somme) method to make sure that the student can withdraw to their limit. Test the class Here's my code for now this is 3.1and 3.2 in the same code Please help me i think im on the right track but i need some guidance in the dark. lol Thank you tutors #Exercice 3,1 - Gestion d'un compte bancaire class CompteBancaire: "d?finition d'un compte bancaire" def __init__(self,nom,solde,interet): #Nous allons instancier et initialiser les objets ? la classe self.nom, self.solde, self.interet = nom,solde,interet def depot(self,somme=0): #D?finition des fonctions self.solde=self.solde+somme #Pour additionner les d?p?ts au compte def retrait(self,somme=0): #Pour soustraire les d?p?ts au compte if self.solde-somme<0: print "Les fonds sont insuffisants. Essayez un autre montant pour votre retrait!" else: self.solde=self.solde-somme def calculInteret(self,calculInteret=0): #Calcul des int?r?ts et du solde r?siduel self.interet=self.solde*calculInteret/100 self.solde=(self.solde*calculInteret/100)+self.solde def affiche_solde(self): print "Le solde du compte bancaire de %s est de %d $CAD" %(self.nom,self.solde) print "Vous avez r?colt? %d $CDN en int?r?t"%(self.interet) # ###################### # cr?ation de la gestion d'un compte ?tudiant autorisant une marge de cr?dit de (1 000$) class CompteEtudiant(CompteBancaire): "d?finition du compte bancaire pour ?tudiant d?riv? du compte bancaire standard" def __init__(self, nom='?tudiant', solde=200, margeCre = 1000): #Limite de marge de cr?dit fix? ? 1 000$ CompteBancaire.__init__(self, nom='Sandon', solde=800, interet=0) self.nom, self.solde, self.margeCre = nom, solde, margeCre def margeCre (self, somme=0): if somme-self.solde>margeCre: print "D?sol? vous d?passez la marge de cr?dit autoris?" else: self.solde = (self.solde-somme) margeCre=margeCre+self.solde def affiche_solde(self, somme=0): print "Le solde du compte bancaire de %s est de %d $CAD" %(self.nom,self.solde) print "Le solde de votre marge de cr?dit est de %d $CAD" %(self.margeCre) print "Vous avez r?colt? %d $CDN en int?r?t"%(self.interet) ############################################################## #jeux d'essai avec des valeurs fictives if __name__=='__main__': #R?f?rence au corps principal du programme. compte1 = CompteBancaire('Sandon',800,0) compte1.depot(0) compte1.retrait(1200) compte1.calculInteret(10) compte1.affiche_solde() compte2 = CompteEtudiant('?tudiant', 800) compte2.retrait(900) compte2.affiche_solde() ################################## -- Ocram the newbie -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Wed Mar 31 05:05:32 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 31 Mar 2010 14:05:32 +1100 Subject: [Tutor] Simple bank account oriented object In-Reply-To: References: Message-ID: On 03/31/2010 01:26 PM, Marco Rompr? wrote: > > Please help me i think im on the right track but i need some guidance in > the dark. lol And what's your question? From shurui91 at gmail.com Wed Mar 31 07:29:59 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Wed, 31 Mar 2010 01:29:59 -0400 Subject: [Tutor] characters Message-ID: In Python, could space be counted as a character same as a letter? -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From cwitts at compuscan.co.za Wed Mar 31 07:41:30 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Wed, 31 Mar 2010 07:41:30 +0200 Subject: [Tutor] characters In-Reply-To: References: Message-ID: <4BB2E08A.7070203@compuscan.co.za> Shurui Liu (Aaron Liu) wrote: > In Python, could space be counted as a character same as a letter? > > len(...) len(object) -> integer Return the number of items of a sequence or mapping. As a space is an item it will be counted. -- Kind Regards, Christian Witts From shurui91 at gmail.com Wed Mar 31 07:49:37 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Wed, 31 Mar 2010 01:49:37 -0400 Subject: [Tutor] characters In-Reply-To: <4BB2E08A.7070203@compuscan.co.za> References: <4BB2E08A.7070203@compuscan.co.za> Message-ID: yeah, thank you! On Wed, Mar 31, 2010 at 1:41 AM, Christian Witts wrote: > Shurui Liu (Aaron Liu) wrote: >> >> In Python, could space be counted as a character same as a letter? >> >> > > len(...) > ? len(object) -> integer > ? ? Return the number of items of a sequence or mapping. > > As a space is an item it will be counted. > > -- > Kind Regards, > Christian Witts > > > -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From alan.gauld at btinternet.com Wed Mar 31 09:35:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 31 Mar 2010 08:35:05 +0100 Subject: [Tutor] characters References: Message-ID: "Shurui Liu (Aaron Liu)" wrote > In Python, could space be counted as a character same as a letter? In Python the fastest and most certain way to answer such questions is to try it at the >>> prompt. >>> len('ab cd') # is it 4 or 5? 5 HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mail at timgolden.me.uk Wed Mar 31 14:42:27 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Wed, 31 Mar 2010 13:42:27 +0100 Subject: [Tutor] Remote access from Windows PC to a Linux box In-Reply-To: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> References: <66563b0e1003300929x3b16a481g7dfaab688a80d9cc@mail.gmail.com> Message-ID: <4BB34333.4040106@timgolden.me.uk> On 30/03/2010 17:29, Mike Baker wrote: > I'm trying to connect to a Linux box from my Windows machine and execute a > series of commands > > I want a script to always > execute the same series of commands without having to do so manually. I > also have code that will execute a single command like cat a file and write > the ouput to a new file. However, when I try to use the communicate object > in subprocess, my window hangs. This works for me: import os, sys import subprocess PLINK = "plink" REMOTE_USER = "tgolden at web30.webfaction.com" PIPE = subprocess.PIPE p = subprocess.Popen ([PLINK, REMOTE_USER, "ls"], stdout=PIPE) stdout, stderr = p.communicate () print "#1:", stdout.splitlines ()[0] with open ("out.txt", "w") as f: p = subprocess.Popen ([PLINK, REMOTE_USER, "cat .bashrc"], stdout=f) p.communicate () print "#2:", open ("out.txt").read ().splitlines ()[0] p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE) stdout, stderr = p.communicate ("ls\nexit\n") print "#3", stdout p = subprocess.Popen ([PLINK, REMOTE_USER], stdin=PIPE, stdout=PIPE) p.stdin.write ("ls\nexit\n") stdout, stderr = p.communicate () print "#4", stdout A few things to note, none of which I believe to be germane to the issues you're experiencing: * You almost never need to use shell=True on a Windows call to subprocess. If in doubt, don't use it. * Definitely better to pass the list-of-params style as the first param of subprocess.Popen; it sorts out issues with embedded spaces etc. * The open ("...", "w") in your second example *may* be closing the file immediately. I doubt it, since you'd expect Popen to hold a reference, but I haven't checked the implementation. TJG From bgailer at gmail.com Wed Mar 31 16:50:54 2010 From: bgailer at gmail.com (bob gailer) Date: Wed, 31 Mar 2010 10:50:54 -0400 Subject: [Tutor] help In-Reply-To: <2371589b1003301241t7b892213k75ccc51ea238b54a@mail.gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> <4BB230F2.6080209@gmail.com> <2371589b1003301241t7b892213k75ccc51ea238b54a@mail.gmail.com> Message-ID: <4BB3614E.2070406@gmail.com> Thank you for specifics. Please always reply-all so a copy goes to the list. On 3/30/2010 3:41 PM, Oshan Modi wrote: > > I have windows 7 (ultimate), python 2.6.3... > when i try to run the file echoinput.py Please post a copy of this file. > it doesnt respond to the request Actually you should say "I don't get the result I expected." Since there is no Windows error message we must conclude that the program ran. > .. and here's what the output is.. > > Microsoft Windows [Version 6.1.7600] > Copyright (c) 2009 Microsoft Corporation. All rights reserved. > > C:\Users\doggie>python echoinput.py > > C:\Users\doggie> > > > > > and i have a couple of more files in that folder and when i try to run > them this is what the error looks like.. > > Microsoft Windows [Version 6.1.7600] > Copyright (c) 2009 Microsoft Corporation. All rights reserved. > > C:\Users\doggie>python class.py > python: can't open file 'class.py': [Errno 2] No such file or directory > > C:\Users\doggie> The message seems pretty self-evident. There is no file named class.py in c:\Users\doggie. > the list does show up when i use IDLE and it is like this > ****************************** > ********************************** > > IDLE 2.6.3 > >>> import sys > >>> for i in sys.path: > print i > > > C:\Python26\Lib\idlelib > C:\Windows\system32\python26.zip > C:\Python26\DLLs > C:\Python26\lib > C:\Python26\lib\plat-win > C:\Python26\lib\lib-tk > C:\Python26 > C:\Python26\lib\site-packages > -- Bob Gailer 919-636-4239 Chapel Hill NC From inthefridge at gmail.com Wed Mar 31 17:20:19 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 31 Mar 2010 09:20:19 -0600 Subject: [Tutor] simple search and replace... Message-ID: I am writing a script to help me add line breaks at certain segments of HL7 messages. Not sure if anyone here is familiar with what an HL7 message is or not. We receive them out of our database with all the segments on one line. I am trying to write a script to allow me to separate them at a certain point and add a line break. A message would look like this: MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152||||||||||| What I want to do is where it says, PID, is add a line break right before that to knock it down to a new line. I currently have a dictionary that contains all of the segments and am wondering what the best way to approach this is. I thought about a regular expression...but...I don't think that is the best way. The basic file i/o I know...it is the search and carriage I need help with. Any ideas? I was thinking of using a 'for i in segments' statement to run through the dictionary of items. -------------- next part -------------- An HTML attachment was scrubbed... URL: From modi.oshan at gmail.com Wed Mar 31 19:50:14 2010 From: modi.oshan at gmail.com (Oshan Modi) Date: Wed, 31 Mar 2010 23:20:14 +0530 Subject: [Tutor] help In-Reply-To: <4BB3614E.2070406@gmail.com> References: <2371589b1003281900k4e7c7d64ue24cdc5411d9235@mail.gmail.com> <4BB230F2.6080209@gmail.com> <2371589b1003301241t7b892213k75ccc51ea238b54a@mail.gmail.com> <4BB3614E.2070406@gmail.com> Message-ID: <2371589b1003311050j64384fe6g1f473df36e451df3@mail.gmail.com> i understood.. its working fine now. thank u all.. ^_^ On Wed, Mar 31, 2010 at 8:20 PM, bob gailer wrote: > Thank you for specifics. > > Please always reply-all so a copy goes to the list. > > > On 3/30/2010 3:41 PM, Oshan Modi wrote: > >> >> I have windows 7 (ultimate), python 2.6.3... >> when i try to run the file echoinput.py >> > > Please post a copy of this file. > > > it doesnt respond to the request >> > > Actually you should say "I don't get the result I expected." Since there is > no Windows error message we must conclude that the program ran. > > > .. and here's what the output is.. >> >> Microsoft Windows [Version 6.1.7600] >> Copyright (c) 2009 Microsoft Corporation. All rights reserved. >> >> C:\Users\doggie>python echoinput.py >> >> C:\Users\doggie> >> >> >> >> >> and i have a couple of more files in that folder and when i try to run >> them this is what the error looks like.. >> >> Microsoft Windows [Version 6.1.7600] >> Copyright (c) 2009 Microsoft Corporation. All rights reserved. >> >> C:\Users\doggie>python class.py >> python: can't open file 'class.py': [Errno 2] No such file or directory >> >> C:\Users\doggie> >> > > The message seems pretty self-evident. There is no file named class.py in > c:\Users\doggie. > > > > the list does show up when i use IDLE and it is like this >> ****************************** >> ********************************** >> >> IDLE 2.6.3 >> >>> import sys >> >>> for i in sys.path: >> print i >> >> >> C:\Python26\Lib\idlelib >> C:\Windows\system32\python26.zip >> C:\Python26\DLLs >> C:\Python26\lib >> C:\Python26\lib\plat-win >> C:\Python26\lib\lib-tk >> C:\Python26 >> C:\Python26\lib\site-packages >> >> > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Mar 31 19:58:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 31 Mar 2010 18:58:05 +0100 Subject: [Tutor] simple search and replace... References: Message-ID: "Spencer Parker" wrote > MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152||||||||||| > > What I want to do is where it says, PID, is add a line break right before > that to knock it down to a new line. I may be missing something here but won't string.replace() do what you want? ie h7string = MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152||||||||||| h7string = h7string.replace('PID', '\nPID') > I currently have a dictionary that contains all of the segments Not sure what you use as the key but remember that dictionaries don't guarantee order so joining it all together again might be tricky. > wondering what the best way to approach this is. I thought about a > regular > expression...but...I don't think that is the best way. It might be but only if the replacement is much more complex than you suggest. > I was thinking of using a 'for i in segments' statement to run through > the > dictionary of items. If you need multiple changes you might need that but I suspect a simple replace() will do. Even for multiple places, if it's only to add a \n then use a re.sub() to do it all in one call. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From inthefridge at gmail.com Wed Mar 31 21:30:56 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 31 Mar 2010 13:30:56 -0600 Subject: [Tutor] simple search and replace... In-Reply-To: References: Message-ID: That was only a small example of a message...the message contains several elements. If I have the PID segment then I have OBR segment...an OBX, etc. So it would have to cycle through finding each element that is in the message till the end. I actually meant a list and not a dictionary...terminology confusion in my head. On Wed, Mar 31, 2010 at 11:58 AM, Alan Gauld wrote: > > "Spencer Parker" wrote > > MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152||||||||||| >> >> What I want to do is where it says, PID, is add a line break right before >> that to knock it down to a new line. >> > > I may be missing something here but won't string.replace() do > what you want? > > ie > > h7string = > MSH|~^\&|SCW|SMED|COR||20100305123742||ORU^R01|100305123742891|P|2.2|||||||PID|||001109660||TEST^Man||19920519|M||||||||||00055254593|699-63-1152||||||||||| > > h7string = h7string.replace('PID', '\nPID') > > > I currently have a dictionary that contains all of the segments >> > > Not sure what you use as the key but remember that dictionaries > don't guarantee order so joining it all together again might be tricky. > > > wondering what the best way to approach this is. I thought about a >> regular >> expression...but...I don't think that is the best way. >> > > It might be but only if the replacement is much more complex than you > suggest. > > > I was thinking of using a 'for i in segments' statement to run through the >> dictionary of items. >> > > If you need multiple changes you might need that but I suspect a simple > replace() will do. Even for multiple places, if it's only to add a \n then > use a re.sub() to do it all in one call. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Mar 31 21:51:00 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 31 Mar 2010 19:51:00 +0000 (GMT) Subject: [Tutor] simple search and replace... In-Reply-To: References: Message-ID: <340096.67805.qm@web86706.mail.ird.yahoo.com> From: Spencer Parker >Cc: tutor at python.org >Sent: Wednesday, 31 March, 2010 20:30:56 >Subject: Re: [Tutor] simple search and replace... > >That was only a small example of a message...the message contains several elements. If I have the PID segment then I have OBR segment...an OBX, etc. So it would have to cycle through finding each element that is in the message till the end. I actually meant a list and not a dictionary...terminology confusion in my head. >OK I suspected as much. But if it's fixed patterns you can either do a replace() in a loop over your patterns: for pat in ['PID','OBR',....] h7string = h7string.replace('\n'+pat, h7string) Or even build a regex that does it all in one. (But the regex could get complex quickly!) Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From shurui91 at gmail.com Wed Mar 31 22:33:42 2010 From: shurui91 at gmail.com (Shurui Liu (Aaron Liu)) Date: Wed, 31 Mar 2010 16:33:42 -0400 Subject: [Tutor] what's wrong in my command? Message-ID: # geek_translator3.py # Pickle import pickle # Open Dictionary geekfile = open('geekdictionary3.txt', 'r+') new_geeks = pickle.load(geekfile) geekterms = new_geeks.keys() geekterms.sort() # start choice = None while choice != "0": print \ """ Geek Translator 0 - Quit 1 - Look Up a Geek Term 2 - Add a Geek Term 3 - Redefine a Geek Term 4 - Delete a Geek Term 5 - List of Terms """ choice = raw_input("Choice: ") print # exit if choice == "0": print "Good-bye." # get a definition elif choice == "1": term = raw_input("What term do you want me to translate?: ") if term in new_geeks: definition = new_geeks[term] print "\n", term, "means", definition else: print "\nSorry, I don't know", term # add a term-definition pair elif choice == "2": term = raw_input("What term do you want me to add?: ") if term not in new_geeks: definition = raw_input("\nWhat's the definition?: ") new_geeks[term] = definition geekterms.append(term) geekterms.sort() print "\n", term, "has been added." else: print "\nThat term already exists! Try redefining it." # redefine an existing term elif choice == "3": term = raw_input("What term do you want me to redefine?: ") if term in new_geeks: definition = raw_input("What's the new definition?: ") new_geeks[term] = definition print "\n", term, "has been redefined." else: print "\nThat term doesn't exist! Try adding it." # delete a term-definition pair elif choice == "4": term = raw_input("What term do you want me to delete?: ") if term in new_geeks: del new_geeks[term] geekterms.remove(term) print "\nOkay, I deleted", term else: print "\nI can't do that!", term, "doesn't exist in the dictionary." # list of terms elif choice == "5": print geekterms # some unknown choice else: print "\nSorry, but", choice, "isn't a valid choice." # geek speak link print "\tTo learn to speak geek visit" print "\n\t\thttp://www.youtube.com/watch?v=7BpsXZpAARk" # 133t speak links print "\n\n\tTo learn to 1337 speak visit" print "\n\t\thttp://linuxreviews.org/howtos/l33t/" print "\n\t\t\t\tor" print "\n\t\thttp://textozor.com/hacker-text/" # save dictionary pickle.dump(ldict, open('geekdictionary.txt', 'r+')) # close file geekfile.close() raw_input("\n\nPress the enter key to exit.") When I run it, the system gave me the feedback below: Traceback (most recent call last): File "geek_translator3.py", line 4, in import pickle File "/usr/local/lib/python2.5/pickle.py", line 13, in AttributeError: 'module' object has no attribute 'dump' I don't understand, I don't write anything about pickle.py, why it mentioned? what's wrong with "import pickle"? I read many examples online whose has "import pickle", they all run very well. Thank you! -- Shurui Liu (Aaron Liu) Computer Science & Engineering Technology University of Toledo From inthefridge at gmail.com Wed Mar 31 22:34:19 2010 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 31 Mar 2010 14:34:19 -0600 Subject: [Tutor] simple search and replace... In-Reply-To: <340096.67805.qm@web86706.mail.ird.yahoo.com> References: <340096.67805.qm@web86706.mail.ird.yahoo.com> Message-ID: I was on the right track...looking more for advice than anything. Regex could get too complicated for what I am doing. I also maintain the list in a different config file because it does change from time to time...we add and delete depending on what we are doing. Something like this adds a lot more flexibility. Thanks again! On Wed, Mar 31, 2010 at 1:51 PM, ALAN GAULD wrote: > > *From:* Spencer Parker > ***Cc:* tutor at python.org > *Sent:* Wednesday, 31 March, 2010 20:30:56 > *Subject:* Re: [Tutor] simple search and replace... > > That was only a small example of a message...the message contains several > elements. If I have the PID segment then I have OBR segment...an OBX, etc. > So it would have to cycle through finding each element that is in the > message till the end. I actually meant a list and not a > dictionary...terminology confusion in my head. > > OK I suspected as much. > > But if it's fixed patterns you can either do a replace() > in a loop over your patterns: > > for pat in ['PID','OBR',....] > h7string = h7string.replace('\n'+pat, h7string) > > Or even build a regex that does it all in one. > (But the regex could get complex quickly!) > > Alan Gauld > Author of the Learn To Program website > > http://www.alan-g.me.uk/ > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: