[Tutor] OT: list as newsgroup (was: Please submit to tutor list: dictionary update prob)
Jacob S. wrote: Hi everyone, sent this on to the list as told to. cc to eri to verify my sending to list... ;-) Jacob dear jacob, sorry to send this to you but if you may, kindly send to tutor list as im no longer subscribed. my problem is in the update dict portion: it just doesnt update regardless how many contacts i add. kindly advise where my mistake is or code gone wrong. the rest of the options i will do on my own so just hold off the helps for now. appreciate all your good help. please cc to this account. -- regards, erimendz You don't need to be subscribed to the list, you can also use it as a newsgroup via gmane: news://news.gmane.org (for more details: http://news.gmane.org) It works fantastic, I use it for a lot of mailinglists. HTH, Wolfram ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clash of the Titans and Mundane Matters
Michael Powe wrote: Clash of the Titans snip constructor discussions Pilgrim is pedantically correct but Alan's comment matches how most of us think about it. Mundane Matters I'm having a hard time with classes in python, but it's coming slowly. One thing that I think is generally difficult is to parse a task into objects. Here's an example: in Java, I wrote an application to track my travelling expenses (I'm a consultant; this tracking of expenses is the itch I am constantly scratching. ;-) I've also written this application in a perl/CGI web application as well.) It's easy to see the outline of this task: create an abstract class for expense and then extend it for the particular types of expenses -- travel, food, transportation, lodging and so forth. In python, I guess I'd create a class and then subclass it. while that is a valid approach, it is not how most of us would do it. By subclassing you have to edit the code every time a new expense type is added. Ever used MS Money or Quicken? Imagine if the type of each item was a subclass. Use a string. A similar problem occurs with my HTML-parsing routine that I brought to the list recently. Use of HTMLParser was suggested. I've looked into this and usage means subclassing HTMLParser in order to implement the methods in the way that will accomplish my task. Conceptually, I'm having a hard time with the object here. (The fairly poor documentation for HTMLParser doesn't help.) Apparently, I'm creating a parser object and feeding it data. At least, that's the closest I can get to understanding this process. How I'm actually feeding data to the parser object and retrieving the results are matters open to discussion. I'll be working on that when I get another chance. This counts the tags in a html file piped in on stdin. #!/usr/bin/python import sys, HTMLParser class TagCounter(HTMLParser.HTMLParser): def __init__(self): HTMLParser.HTMLParser.__init__(self) self.tags = {} def handle_starttag(self, tag, attrs): self.tags[tag] = self.tags.setdefault(tag, 0) + 1 if __name__ == '__main__': counter = TagCounter() for line in sys.stdin.xreadlines(): counter.feed(line) counter.close() print counter.tags Finally, in terms of understanding python, the question I keep coming up against is: why do we have both functions and methods? What is the rationale for making join() a string method and a os.path function? a method is a function bound to a class. Nothing super special. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clash of the Titans and Mundane Matters
Michael Powe wrote: Here's an example: in Java, I wrote an application to track my travelling expenses (I'm a consultant; this tracking of expenses is the itch I am constantly scratching. ;-) I've also written this application in a perl/CGI web application as well.) It's easy to see the outline of this task: create an abstract class for expense and then extend it for the particular types of expenses -- travel, food, transportation, lodging and so forth. In python, I guess I'd create a class and then subclass it. But ... what are reading/writing to files and printing? I'm not sure exactly what output you're after ... But what about something like this? class Expense(object): def __init__(self, amount): self.amount = amount class Travel(Expense): def __str__(self): return 'Travel: $%.2f' % float(self.amount) class Food(Expense): def __str__(self): return 'Food: $%.2f' % float(self.amount) class Accommodation(Expense): def __str__(self): return 'Accommodation: $%.2f' % float(self.amount) myExpenses = [Travel(2300), Accommodation(200), Food(12.50), Food(19.95), Food(2.35), Travel(500)] for e in myExpenses: print e out = file('myExpenses.txt', 'w') for e in myExpenses: out.write(str(e) + '\n') out.close() - This produces output: Travel: $2300.00 Accommodation: $200.00 Food: $12.50 Food: $19.95 Food: $2.35 Travel: $500.00 and the same in the file 'myExpenses.txt'. The str() function automatically calls .__str__() on its argument (if you don't define __str__, it will call a boring default one). And the print command automatically calls str() on its arguments. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Fw: Please submit to tutor list: dictionary update prob
Kent Johnson kent37 at tds.net writes: Jacob S. wrote: sorry to send this to you but if you may, kindly send to tutor list as im no longer subscribed. my problem is in the update dict portion: it just doesnt update regardless how many contacts i add. kindly advise where my mistake is or code gone wrong. the rest of the options i will do on my own so just hold off the helps for now. appreciate all your good help. def update_dict(d, f): ''' update the saved dictionary file ''' read = open(f, 'rb') newdic = cPickle.load(read) newdic.update(d) read.close() You don't do anything with newdic. My guess is you want to dump it back to the file so it is saved. this is what i tried: read = open(f, 'rb') newdic = cPickle.load(read) newdic.update(d) read.close() write = open(f, 'wb') cPickle.dump(f, write) write.close() but it 'overwrites' the saved dic. can you rw in one go, dump and load the updated dict instantly?? sending this in gmane mail2news gateway. -- regards, erimendz ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] counting no of words
Title: counting no of words hi all, Is it possible to write a program in python to calculate the number of words in a MS-Word document Page Regards gopi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting no of words
Sure, What you need are the win32 extensions for python (http://starship.python.net/crew/mhammond/win32/Downloads.html), which contain win32com. Through com, you can access almost anything in windows. Check out http://aspn.activestate.com/ASPN/docs/ActivePython/2.2/PyWin32/html/com/win32com/HTML/QuickStartClientCom.html for a short com tutorial, and http://starship.python.net/crew/pirx/spam7/ for a presentation on com in python. Once you're connected to a word document, you'll have to figure out what the command to count the words in the document is, but that's just a matter of recording a macro in word where you count the words, then repeating it in python. I'd help you with that, but I'm on linux. Peace Bill Mill bill.mill at gmail.com On Thu, 20 Jan 2005 16:37:06 +0530, Gopinath V, ASDC Chennai [EMAIL PROTECTED] wrote: hi all, Is it possible to write a program in python to calculate the number of words in a MS-Word document Page Regards gopi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] need help planning website updates
I have a sort of simple CMS system on my website made from a conglomeration of scripts. On the left column, I want to add a feature that shows the last five items updated (only html exe files in the /var/www/html/ for example) directory that I have updated, with each item as a link to the page. You can see what this is supposed to look like at http://jayloden.com (right now it's being done by hand) I've been thinking of having a crontab run a Python script, which logs checks a file with something along the lines of: file.foo = 12:20-1/20/05 for each file, containing the date and time the file was last modified. Then I would have the crontab script check the date in the file versus the dates on the current files, and if the current files have been updated, to add them to the html on the side of the page. I have no trouble setting up the crontab, or editing the html template for my page (it's all created from php on the fly) but I wanted to know if I am going about this a semi-intelligent and/or efficient way, or if there is some incredibly better way that I could do this with Python. For example, is there some other way to notify my script that a file has been modified, rather than run a crontab a couple times an hour. Is there maybe a better way to store and check dates, etc. Thanks! -Jay ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help planning website updates
It seems to me that if you want the five most recent changes, you don't have to keep a list of modified dates. Just get the modified date for all the files of interest and sort by date, then pick the top five. You could do this as part of your process to build the web site maybe? Kent Jay Loden wrote: I have a sort of simple CMS system on my website made from a conglomeration of scripts. On the left column, I want to add a feature that shows the last five items updated (only html exe files in the /var/www/html/ for example) directory that I have updated, with each item as a link to the page. You can see what this is supposed to look like at http://jayloden.com (right now it's being done by hand) I've been thinking of having a crontab run a Python script, which logs checks a file with something along the lines of: file.foo = 12:20-1/20/05 for each file, containing the date and time the file was last modified. Then I would have the crontab script check the date in the file versus the dates on the current files, and if the current files have been updated, to add them to the html on the side of the page. I have no trouble setting up the crontab, or editing the html template for my page (it's all created from php on the fly) but I wanted to know if I am going about this a semi-intelligent and/or efficient way, or if there is some incredibly better way that I could do this with Python. For example, is there some other way to notify my script that a file has been modified, rather than run a crontab a couple times an hour. Is there maybe a better way to store and check dates, etc. Thanks! -Jay ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help planning website updates
Adding it into the PHP that creates the html would create too much overhead since it loads each page individually upon request, and that would mean running the modified time check on every page load. But I was thinking about this after I sent the mail, and I think you have a point with just outputting the five last modified out of all files. This was one of those times when your brain fails you and you think up an overly complicated solution to a simple problem. This way the script just has to run every 15 minutes or so and give me the five most recent files for use in the PHP. Thanks! -Jay On Thursday 20 January 2005 12:45, Kent Johnson wrote: It seems to me that if you want the five most recent changes, you don't have to keep a list of modified dates. Just get the modified date for all the files of interest and sort by date, then pick the top five. You could do this as part of your process to build the web site maybe? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting no of words
Rumor has it that Bill Mill may have mentioned these words: [snip] Once you're connected to a word document, you'll have to figure out what the command to count the words in the document is, but that's just a matter of recording a macro in word where you count the words, then repeating it in python. Another option would be to write the python program from within OpenOffice.org if it's available -- it has facilities of opening and accessing word documents as well. I'd help you with that, but I'm on linux. Most things I do are on Linux as well; but the OpenOffice solution should work equally well on either platform. HTH, Roger Merch Merchberger -- Roger Merch Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting no of words
I'd take the easy way out and use winPython's COM objects to open the doc in word, and save it as .txt and then - f=file(doc.txt,'r') j=f.read() j=j.split( ) print len(j) On Thu, 20 Jan 2005 13:59:16 -0500, Roger Merchberger [EMAIL PROTECTED] wrote: Rumor has it that Bill Mill may have mentioned these words: [snip] Once you're connected to a word document, you'll have to figure out what the command to count the words in the document is, but that's just a matter of recording a macro in word where you count the words, then repeating it in python. Another option would be to write the python program from within OpenOffice.org if it's available -- it has facilities of opening and accessing word documents as well. I'd help you with that, but I'm on linux. Most things I do are on Linux as well; but the OpenOffice solution should work equally well on either platform. HTH, Roger Merch Merchberger -- Roger Merch Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to print on screen and to a file, meanwhile?
Hi, I'm now writing a simulation program in Python. Indeed, it's a time-wasting program, a complete simulation will last several days. Of course, the simulation result is easily stored in files, through file class of Python. Now, my partners require the simulation result to be printed on screen and to be stored in file as well, therefore they can monitor the result in time. I hope these two output method should be used in the mean time -- not a 'print' + a 'file.write'. :) So what's the single method to hold this requirement? Wish it's quite basic. By the way, the platform is FreeBSD. Juan Shen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Syntactical question / OT Lisp
Hi all, (side note - the net is not a luxury when attempting to learn to code) Just pondering my coding efforts, and just wanted to clarify something. I've got a module called foo.py foo.py - import parrot class Bar(model.Background): def __initialize__(self, event): #Just a pythoncard variant on init self.config=self.loadCfg() def loadCfg(): #get some cfg stuff, return as dict return cfgDict def on_aBtn_mouseClick(self, event): parrot.Sketch() app=Bar(main.application) app.mainloop() If I wanted the function parrot.Sketch() to access that config dictionary, I would reference it as foo.app.config? Is that right? Regards, Liam Clarke -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Ooer, OT Lisp
Oops, and OT ~ Has anyone used Lisp? I've been reading Paul Graham's essays on how great Lisp is, and how Python is near to implementing features Lisp had in the 60's. Also found the concept of macros interesting. Queries - 1) Anyone here familiar with both? 2) If so, which would you rate as more powerful? 3) What's with all those parentheses? 4) Perhaps the powerful question's a bit vague, how about ease of use? I like that the simplest Lisp expression is - , but those brackets 5) Are you able to point me towards a simplified explanation of how the 'syntaxless' language can write programmes? Sorry to play 20 questions. Regards, Liam Clarke -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntactical question / OT Lisp
Liam Clarke wrote: Hi all, I've got a module called foo.py foo.py - import parrot class Bar(model.Background): def __initialize__(self, event): #Just a pythoncard variant on init self.config=self.loadCfg() def loadCfg(): #get some cfg stuff, return as dict return cfgDict def on_aBtn_mouseClick(self, event): parrot.Sketch() app=Bar(main.application) app.mainloop() If I wanted the function parrot.Sketch() to access that config dictionary, I would reference it as foo.app.config? Is that right? Yes, after import foo but it's really ugly and a very bad idea. You should do something different like parrot.Sketch(self.config) or even put the config into a module of its own if you really want a globally available configuration. OK, so why is this so bad? Because foo and parrot depend on each other. Neither one can be used independently. You can't test parrot.Sketch() without creating a foo.app. You can't reuse parrot.Sketch() in another module named bar. You can't even restructure foo.py to make app a local variable of a main() function, for example. Don't do this. Really. This way lies spaghetti code and intractable bugs and throwing the whole mess away and starting over doing it right this time. Try to organize your modules so the dependencies form an acyclic directed graph. In other words, don't have any dependency cycles like foo depends on parrot depends on foo. Kent Regards, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ooer, OT Lisp
Liam Clarke said unto the world upon 2005-01-20 21:46: Oops, and OT ~ Has anyone used Lisp? I've been reading Paul Graham's essays on how great Lisp is, and how Python is near to implementing features Lisp had in the 60's. Also found the concept of macros interesting. SNIP Regards, Liam Clarke Hi Liam, I've barely poked into Lisp or the book, but the Jargon File http://www.catb.org/~esr/jargon/html/W/Wizard-Book.html has this to say: Wizard Book: n Structure and Interpretation of Computer Programs (Hal Abelson, Jerry Sussman and Julie Sussman; MIT Press, 1984, 1996; ISBN 0-262-01153-0), an excellent computer science text used in introductory courses at MIT. So called because of the wizard on the jacket. One of the bibles of the LISP/Scheme world. Also, less commonly, known as the Purple Book. Now available on the http://mitpress.mit.edu/sicp/; Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ooer, OT Lisp
Liam, On Fri, 21 Jan 2005 15:46:19 +1300, Liam Clarke [EMAIL PROTECTED] wrote: Oops, and OT ~ Has anyone used Lisp? I've been reading Paul Graham's essays on how great Lisp is, and how Python is near to implementing features Lisp had in the 60's. Also found the concept of macros interesting. Queries - 1) Anyone here familiar with both? I used Lisp at school, so my answers should be taken with a whole bag of salt, but I kind of dug it. 2) If so, which would you rate as more powerful? Lisp. No question - if it can be done, it can be done in lisp. With that power comes a price, though - lisp is nowhere near as intuitive as Python. Lispers won't agree (of course) but I am really and truly convinced that it's true. At the time I learned lisp, I didn't know too much python, so I don't think I was that biased either. 3) What's with all those parentheses? They stop bothering you after a while. Use a good editor in a lisp setting, and there's no worries. Many will highlight matching parens, which helps out. 4) Perhaps the powerful question's a bit vague, how about ease of use? I like that the simplest Lisp expression is - , but those brackets Once you wrap your head around lisp, it's not too hard to use. You just have to think in a different way - recursion is good, variable declarations are bad. Every s-expression starts with a function unless you say otherwise. There is no standard implementation of lisp, so sockets and os access all vary by implementation. Furthermore, the docs are sketchy and hard to read with all of the lisps I've tried. The one I liked most was allegro common lisp (http://www.franz.com/), but I'm very far from a power user. 5) Are you able to point me towards a simplified explanation of how the 'syntaxless' language can write programmes? Hmmm, not sure what you're getting at here. Lisp isn't syntaxless, it just has a really simple syntax with little sugar. Here's pseudo-lisp (no interpreter handy) for a factorial function: (defun fact (n) (cond ((= n 0) 1) (t (* n (fact (- n 1)) Which translates into the Python: def fact(n): if n == 0: return 1 else: return n * fact(n-1) Unless otherwise specified, everything in lisp takes the form (function arguments); many things that are syntax in python are functions in lisp. Also, you should know that lisp pioneered the interactive interpreter; python got that idea from lisp. This makes it much easer to experiment in lisp. Sorry to play 20 questions. No worries. I found the site at http://www.lisp.org/alu/home to be very helpful when I was digging around lisp-world. If you have any more questions, I'll try to help you out, but you might want to ask some more knowledgeable persons. Peace Bill Mill bill.mill at gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor