Re: [Tutor] Dynamically naming functions
On 26/03/06, Kent Johnson [EMAIL PROTECTED] wrote: Ed Singleton wrote: How does one go about creating functions, classes, or callable objects when you don't know their name in advance? (For example you want to read their names in from a text file or database). I want to use this in a few different places. For example Faces, the Python Project Management Planner Tool Thingy, uses nested functions to put tasks within a project: def MyProject(): start = 2006-03-06 resource = Me def Task1(): start = 2006-03-13 def Task2(): effort = 1w I'd like to load these from a database (using SQLObject), but I'm not sure how I can define the name of the function from a filed in a database (or read in from a text file). Hi Ed, I was just wondering how this came out - did you find a way to generate these functions dynamically? Or change Faces? Or give up? I just temporarily delayed the problem. I'm going to have a look at trying to change the Faces code to use dictionaries as per your suggestion, but it would a big task for me (my biggest so far probably). (I have a new job that lets me do everything in Python, but sometimes they direct my efforts towards a particular task, which delays my working on interesting things). Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Learning Python
Hi I am a novice in Python, Which is the best source of learning python for a beginner Regards Kaushal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learning Python
Kaushal Shriyan wrote: Hi I am a novice in Python, Which is the best source of learning python for a beginner Read one of the tutorials listed here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Write simple programs to try out what you have learned. Ask questions here when you get stuck. Have fun! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learning Python
On Mon, March 27, 2006 4:39 pm, Kaushal Shriyan wrote: Hi I am a novice in Python, Which is the best source of learning python for a beginner If you're already a programmer, then the python tutorial at http://docs.python.org/tut/tut.html + a working python installation is enough IMHO. If you want to learn programming itself, perhaps you should take a look at http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Good luck! -- -NI ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learning Python
On 3/27/06, Noufal Ibrahim [EMAIL PROTECTED] wrote: On Mon, March 27, 2006 4:39 pm, Kaushal Shriyan wrote: Hi I am a novice in Python, Which is the best source of learning python for a beginner If you're already a programmer, then the python tutorial at http://docs.python.org/tut/tut.html + a working python installation is enough IMHO. If you want to learn programming itself, perhaps you should take a look at http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Good luck! -- -NI Thanks a Ton, I will definetly ping and update all of you :) Regards KaushaL ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newbie exercises
Hi,Python is my first language. I have finished loops, now I'm going to functions. I'm working with Learning Python 2ed book. Can someone give me exercises to do with loops, maybe functions to?Thanks Blab-away for as little as 1¢/min. Make PC-to-Phone Calls using Yahoo! Messenger with Voice.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie exercises
On 3/27/06, josip [EMAIL PROTECTED] wrote: Can someone give me exercises to do with loops, maybe functions to? How about a program that produces truth tables for the basic gates? AND, NAND, NOT, OR, XOR? You could write a function for each gate, and one to produce a truth table. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] using BeautifulSoup
Hi! I'm trying to use BeautifulSoup for get data from a table (on right) from: http://en.wikipedia.org/wiki/United_states i.e. i would get data from 'Calling code' that it would be '+1' -- import urllib2 from BeautifulSoup import BeautifulSoup url=http://en.wikipedia.org/wiki/United_states; html = urllib2.urlopen(url).read() soup = BeautifulSoup() soup.feed(html) mainTable = soup.first('table') rows = mainTable('tr') any help here? Thanks in advance ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using BeautifulSoup
[EMAIL PROTECTED] wrote: Hi! I'm trying to use BeautifulSoup for get data from a table (on right) from: http://en.wikipedia.org/wiki/United_states i.e. i would get data from 'Calling code' that it would be '+1' -- import urllib2 from BeautifulSoup import BeautifulSoup url=http://en.wikipedia.org/wiki/United_states; html = urllib2.urlopen(url).read() soup = BeautifulSoup() soup.feed(html) You just have to find some kind of ad hoc search that gets you to where you want to be. I would try something like this: anchor = soup.fetch('a', dict(href=/wiki/List_of_country_calling_codes)) code = anchor.findNext('code') print code.string Presumably you want this to work for other country pages as well; you will have to look at the source, see what they have in common and search on that. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compilte python to an executable file.
Hi jack, You are right. py2exe may not fit well in commercial software products that arent in the open source model. An executable that was py2exe'd could well be converted back to the source..I feel that its one of the restrictions thats in the way of enterprization of python...(agreed google uses python..but thats internal.. they dont deploy py files in customer's computers).correct me if i am wrong... CheersVarun ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compilte python to an executable file.
Keo Sophon wrote: On Monday 27 March 2006 10:38, Ars wrote: On 27/03/06, Keo Sophon [EMAIL PROTECTED] wrote: hi all, Does anyone know how to compile a python filename.py to an executable file? I don't know how python creates executable file. The purpose is to have the program written in Python can run on Linux and Windows and Mac (if possible). A well-written Python program (.py file) is very portable across Linux, Windows and Mac, if the target machine has Python installed. There is no executable format that will run unchanged on these three platforms, you have to build three executable files. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compilte python to an executable file.
Ars wrote: Would a py2exe program be practical for a commercial program? I'd think people could read your source code too easily. I think it is possible to create py2exe files that don't contain source code, just compiled pyc files. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compilte python to an executable file.
jonasmg at softhome.net wrote: Hi! I'm trying to use BeautifulSoup for get data from a table (on right) from: http://en.wikipedia.org/wiki/United_states i.e. i would get data from 'Calling code' that it would be '+1' -- import urllib2 from BeautifulSoup import BeautifulSoup url=http://en.wikipedia.org/wiki/United_states; html = urllib2.urlopen(url).read() soup = BeautifulSoup() soup.feed(html) You just have to find some kind of ad hoc search that gets you to where you want to be. I would try something like this: anchor = soup.fetch('a', dict(href=/wiki/List_of_country_calling_codes)) code = anchor.findNext('code') print code.string Presumably you want this to work for other country pages as well; you will have to look at the source, see what they have in common and search on that. Kent anchor.findNext('code') fails: anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'}) print anchor [a href=/wiki/List_of_country_calling_codes title=List of country calling codesCalling code/a] anchor.findNext('code') [] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using BeautifulSoup
jonasmg at softhome.net wrote: Hi! I'm trying to use BeautifulSoup for get data from a table (on right) from: http://en.wikipedia.org/wiki/United_states i.e. i would get data from 'Calling code' that it would be '+1' -- import urllib2 from BeautifulSoup import BeautifulSoup url=http://en.wikipedia.org/wiki/United_states; html = urllib2.urlopen(url).read() soup = BeautifulSoup() soup.feed(html) You just have to find some kind of ad hoc search that gets you to where you want to be. I would try something like this: anchor = soup.fetch('a', dict(href=/wiki/List_of_country_calling_codes)) code = anchor.findNext('code') print code.string Presumably you want this to work for other country pages as well; you will have to look at the source, see what they have in common and search on that. Kent anchor.findNext('code') fails: anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'}) print anchor [a href=/wiki/List_of_country_calling_codes title=List of country calling codesCalling code/a] anchor.findNext('code') [] P.S. : Sorry for my last email, I was wrong with the subject ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] compilte python to an executable file.
hi, I face a typical problem that most python programmers face. Most users only want a click and run kind of thing..they are uninterested to install any software on their system , be it python or any other software..so the solution is to pack the code up as a py2exe program for windows and ask them to click it and run... Is there any solution where i dont ask any software to be installed(ie., python) and still be able to assure that if they click something, they get the result that they need?... py2exe partially succeeds in answering this..by packing things up...however any compiled pyc code can easily be decompiled to get the source..its geared towards windows and in linux, we (always) assume that the users are savvy enough and interested to install python if its not already installed...so is the case with mac. Regards-- Varun ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] grab a secodary window content
Hi to all! I'm playing around with VideoCapture and vpython, both open a secondary window with nice images on it, well the one with my face is not so great, anyway I wish to grab the content and put it into a PIL Image, in VideoCapture there are a methods called saveSnapshot and getImage, but somehow they work one every 2 circa (I made a loop until it doesn't raise errors and it works) with vpython I didn't find nothing at all. Now, I know I could make a snapshot of the entire desktop and than cut what I'm looking for, but shot at butterflies' whit a bazooka is impolite. I fill I'm missing a couple of lines of python, I run on Windows. Any suggest is appreciated! Thanks. Carlo what is ITER? www.iter.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using BeautifulSoup
[EMAIL PROTECTED] writes: jonasmg at softhome.net wrote: Hi! I'm trying to use BeautifulSoup for get data from a table (on right) from: http://en.wikipedia.org/wiki/United_states i.e. i would get data from 'Calling code' that it would be '+1' -- import urllib2 from BeautifulSoup import BeautifulSoup url=http://en.wikipedia.org/wiki/United_states; html = urllib2.urlopen(url).read() soup = BeautifulSoup() soup.feed(html) You just have to find some kind of ad hoc search that gets you to where you want to be. I would try something like this: anchor = soup.fetch('a', dict(href=/wiki/List_of_country_calling_codes)) code = anchor.findNext('code') print code.string Presumably you want this to work for other country pages as well; you will have to look at the source, see what they have in common and search on that. Kent anchor.findNext('code') fails: anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'}) print anchor [a href=/wiki/List_of_country_calling_codes title=List of country calling codesCalling code/a] anchor.findNext('code') [] P.S. : Sorry for my last email, I was wrong with the subject ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Solution _there is that using findChild instead of fetch_: anchor = soup.findChild('a', dict(href=/wiki/List_of_country_calling_codes)) print anchor.findNext('code') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html and mod_python
Can you please give me a quick example of how to do this: Nevermind. I figured it out. Thanks anyway :-) Patty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Turnkey Python on a USB stick
Hi list, I like the idea of portable apps. Load a USB drive and you're ready to go. I am switching from PHP to Python and I'm curious if anyone has tried using USB sticks for either development or distribution? I was looking at purchasing Movable Python for a USB stick. I was then going to load it up with Twisted, GTK, Leo, py2exe, py2app, protable firefox, portable thunderbird and the Uniform Server. Zip the drive, backup the file and I have an instant recovery. My development environment is safe and stable. My users are not very sophisticated so I wanted to use a USB drive for distribution. They'll plug in the USB stick, which will automatically start the webserver (Uniform Server). Then they'll interact with a wiki style website. Later they can sync their personal server with an online server somewhere for publication and backup. Later I'll include a GTK frontend and twisted server. Does this sound reasonable? Are their any alternatives to Movable Python? (http://www.voidspace.org.uk/python/movpy/) Thanks, -Steve ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] setdefault method
Greetings: What is the purpose of the dictionary method setdefault(k[, x])? For example assume the following dictionary: colors = {'green':(0,255,0), 'red':(255,0,0), 'blue':(0,0,255), 'white':(255,255,255), 'black':(0,0,0)} Now, execute the statement: colors.setdefault('black') (0, 0, 0) I would expect that future references to colors, with no argument or a null argument, would return the value of colors['black'], e.g.: colors[] (0, 0, 0) colors (0, 0, 0) or some similar syntax. This is not the case, however. The actual behavior is: colors {'blue': (0, 0, 255), 'black': (0, 0, 0), 'white': (255, 255, 255), 'green': (0, 255, 0), 'red': (255, 0, 0)} colors[] File input, line 1 colors[] ^ SyntaxError: invalid syntax So, what then is the proper use of setdefault()? And, if d.setdefault does not actually assign a default value for d, is there a way to do this? As always, thanks in advance for your responses. Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turnkey Python on a USB stick
On Monday 27 March 2006 22:21, Steve Slevinski wrote: Does this sound reasonable? Are their any alternatives to Movable Python? (http://www.voidspace.org.uk/python/movpy/) Hi Steve, I've got no experience of setting up such a beast or using movable python, but I just wanted to say it sounds like an EXCELLENT idea, and I'd be interested in hearing how you get on! :) Michael. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Data Type with Dictionary and List Behavior
Greetings: I have a function that computes the checksum of an integer, including or excluding digits in the computation based on the content of a mask string. For example, cksum(123456789, '***...***') will do its computation on 123789, masking out the three non-significant digits. My question concerns assigning the value of the mask string. The class that defines the function also pre-defines several masks, say '*', '***...***', and '..***'. The masks have names: 'all', 'first-last', 'last'. Of these, 'all' is the most commonly used. The user may select one of these masks, or may supply their own, arbitrary value. Further, the user can choose to add their value to the group of pre-defines, and reuse that value later in the session. (The user-defined mask is not saved between sessions; no permanent storage is required.) So far, this structure looks like a dictionary. However, the user also wants to access the group of pre-defined masks as if they were elements of a list: masks[0] returns '*' masks[1] returns '***...***' and so on. To make matters even more complex, if the user does not specify a mask to use, the function should use the mask employed in the previous invocation, defaulting to masks[0] if this is the first invocation. Finally, the user can set a mask as 'default', which essentially marks a mask as 'last used' without invoking the function. Is there a derived type or data structure in existence that implements these capabilities (in descending order of importance? 1. Access by name (dict) 2. Access by position (list) 3. Addition of new items (dict, list) 4. Set a 'last used' item (??) 5. Set a 'default' item (dict???) Thanks in advance for your help. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] setdefault method
On Mon, 27 Mar 2006, Carroll, Barry wrote: So, what then is the proper use of setdefault()? And, if d.setdefault does not actually assign a default value for d, is there a way to do this? It's a somewhat misleading name, but setdefault a) looks up an entry in a dictionary and c) returns it... but if it's not found, between steps a c it b) sets the entry to the default value. Think of it as a way to pre-initialize dictionary entries on the fly the first time they're referenced. Here's an example, which looks at a string of text and creates a dictionary of the constituent characters' frequency: freq = {} sampletext = norwegian blue for char in sampletext: ... freq[char] = freq.setdefault(char,0)+1 ... freq {'a': 1, ' ': 1, 'b': 1, 'e': 2, 'g': 1, 'i': 1, 'l': 1, 'o': 1, 'n': 2, 'r': 1, 'u': 1, 'w': 1} ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Data Type with Dictionary and List Behavior
On 28/03/06, Carroll, Barry [EMAIL PROTECTED] wrote: 1. Access by name (dict) 2. Access by position (list) 3. Addition of new items (dict, list) 4. Set a 'last used' item (??) 5. Set a 'default' item (dict???) Probably your best bet is to define your own container class. If you define __getitem__, you can use [] with instances of your class, just like lists or dicts. Something like this, perhaps (untested): class MaskDict(UserDict.DictMixin): def __init__(self): self.keys = [] self.data = {} self.default = None def __getitem__(self, key): if isinstance(key, (int, long)): self.default = self.keys[key] return self.data[self.default] else: self.default = key return self.data[key] def __setitem__(self, key, value): try: self.keys.remove(key) except ValueError: pass self.keys.append(key) self.data[key] = value def __delitem__(self, key, value): del self.data[key] self.keys.remove(key) def keys(self): return self.keys def setDefault(self, key): self.default = key HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] setdefault method
Carroll, Barry wrote: Greetings: What is the purpose of the dictionary method setdefault(k[, x])? setdefault() is perhaps badly named, but it is very useful. It doesn't do what you think it does! From the docs: setdefault() is like get(), except that if k is missing, x is both returned and inserted into the dictionary as the value of k. x defaults to None. You could define your own like this (with an extra arg for the dict): def setdefault(d, k, x=None): if k in d: return d[k] else: d[k] = x return x If k is in d, the existing value d[k] is returned. If k is not in d, the magic happens - d[k] is set to x, and x is returned to you. I find this most often useful when I want to make a dict that maps a key to a list of values. For example I may have a list of key, value pairs and I want to accumulate the list of all values for each key. This comes up pretty often in my experience. Here is how to code it with setdefault(): d = {} for k, v in some_list: d.setdefault(k, []).append(v) Here setdefault() will return the list that k maps to, if any, or otherwise start a new list. In either case, you can append the new value to the returned list and get the effect you want. And, if d.setdefault does not actually assign a default value for d, is there a way to do this? See this recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389639 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] setdefault method
Terry Carroll wrote: freq = {} sampletext = norwegian blue for char in sampletext: ... freq[char] = freq.setdefault(char,0)+1 Although I'm a big fan of setdefault() I think this particular example is better written as freq[char] = freq.get(char,0)+1 There is no need to store the initial 0 into freq as it will immediately be overwritten. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using BeautifulSoup
[EMAIL PROTECTED] wrote: anchor.findNext('code') fails: anchor = soup.fetch('a', {'href': '/wiki/List_of_country_calling_codes'}) print anchor [a href=/wiki/List_of_country_calling_codes title=List of country calling codesCalling code/a] anchor.findNext('code') [] are you sure that's what you got? Looks like an AttributeError to me - anchor is a *list* of anchors. Try anchor[0].findNext('code') Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Object Oriented Programmin
Hi ALL I have gone through the object oriented programming in Python, I am not able to understand OOP concept in python, is there a methodical way to understand it and simplfy things Thanks in Advance Regards Kaushal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python tutor
Greetings all, Are there any programs for python that offer an interactive tutorial? Something on the lines of the builtin emacs tutorial (which is basically just a buffer that tells you to try this and try that with itself) or the Inkscape tutorial (which is an SVG document that comes along with inkscape which has instructions on manipulating it so that the reader learns stuff). Another example that comes to mind is the tcltutor program to learn TCL. It contains an instruction window, a code window and an output window. The user is told something, they try it and the output is visible. I personally used it when I was learning TCL. The python tutorial is great and probably all one needs to learn the language but I think a more interactive program to teach it might be useful. I googled a little and found Guido van Robot although I'm not sure if it's exactly like what I'm talking about. Are there any others? Do you all think it'll be a worthwhile project? Bye -- -NI ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor