From bvande at po-box.mcgill.ca Sat Jan 1 02:30:28 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Sat Jan 1 02:56:10 2005 Subject: [Tutor] O.T. In-Reply-To: <000501c4ec5b$6ec59c80$8c5428cf@JSLAPTOP> References: <000501c4ec5b$6ec59c80$8c5428cf@JSLAPTOP> Message-ID: <41D5FD34.1030709@po-box.mcgill.ca> Jacob S. said unto the world upon 2004-12-27 16:31: > I hate to sound weird... > > But who are you all, what are you're ages, what do you do, marriage > status, etc? You obviously don't have to answer, I'm just curious who > I'm boldly sending emails to. > > Jacob Schmidt > > P.S. I'm a student. 14 years. Play the piano better than I write > scripts. Single. etc. > Hi all, thanks for posting that, Jacob. Many things about the 'net are wonderful, but the relative anonymity isn't (always) among them! I'm closing out 31, and as married as a consistent philosophical anarchist without present need of EU working papers can be ;-) I'm Canadian, living in Montreal, and finishing up a PhD in the philosophy of logic. (So, I can clear a dinner-party in pretty good time.) The PhD thing leaves me with only Python and photography as hobbies. Came to Python via an ESR essay; its my sole programming language past some BASIC back in high-school. While I've yet to explore python's logic related tools, it has been quite illuminating for my research interests to see recursion theory 'in action' rather than in the more purely abstract context that prevails in mathematical and philosophical treatments of logic. Happy New Year, all, Brian vdB From kent37 at tds.net Sat Jan 1 04:00:36 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 1 04:00:48 2005 Subject: [Tutor] Fwd: ASCII encoding for a =?ISO-8859-1?Q?=A3_sign?= In-Reply-To: <20041231164210.49306.qmail@web25403.mail.ukl.yahoo.com> References: <20041231164210.49306.qmail@web25403.mail.ukl.yahoo.com> Message-ID: <41D61254.3010002@tds.net> David Holland wrote: > Apologies, > I did not see that Kent had already answered this ! > Thanks Kent I put that encoding and it works fine. > Although surprisingly the other idea :- > write the string with '\xc2' did not work. Well I was guessing so I am happy that one of the guesses was correct. What happened when you tried '\xc2'? Kent > > --- David Holland wrote: > >>Date: Fri, 31 Dec 2004 16:28:59 +0000 (GMT) >>From: David Holland >>Subject: ASCII encoding for a ? sign >>To: tutor@python.org >> >>I ran a program with a "?" sign, and I got this >>message :- >> >>"sys:1: DeprecationWarning: Non-ASCII character >>'\xc2' >>in file birdgame32a.py on line 93, but no encoding >>declared; see >>http://www.python.org/peps/pep-0263.html >>for details' >>I looked at the message but I am not sure what >>encoding to use for "?". >> >> >> >> >> >> > > ___________________________________________________________ > >>ALL-NEW Yahoo! Messenger - all new features - even >>more fun! http://uk.messenger.yahoo.com >> > > > > > > > ___________________________________________________________ > ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From dyoo at hkn.eecs.berkeley.edu Sat Jan 1 06:44:37 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sat Jan 1 06:44:41 2005 Subject: [Tutor] Parsing a block of XML text In-Reply-To: <20041231221543.64372.qmail@web53707.mail.yahoo.com> Message-ID: On Fri, 31 Dec 2004, kumar s wrote: > I am trying to parse BLAST output (Basic Local Alignment Search Tool, > size around more than 250 KB ). [xml text cut] Hi Kumar, Just as a side note: have you looked at Biopython yet? http://biopython.org/ I mention this because Biopython comes with parsers for BLAST; it's possible that you may not even need to touch XML parsing if the BLAST parsers in Biopython are sufficiently good. Other people have already solved the parsing problem for BLAST: you may be able to take advantage of that work. > I wanted to parse out : > > > Ok, I see that you are trying to get the content of the High Scoring Pair (HSP) query and hit coordinates. > I wrote a ver small 4 line code to obtain it. > > for bls in doc.getElementsByTagName('Hsp_num'): > bls.normalize() > if bls.firstChild.data >1: > print bls.firstChild.data This might not work. 'bls.firstChild.data' is a string, not a number, so the expression: bls.firstChild.data > 1 is most likely buggy. Here, try using this function to get the text out of an element: ### def get_text(node): """Returns the child text contents of the node.""" buffer = [] for c in node.childNodes: if c.nodeType == c.TEXT_NODE: buffer.append(c.data) return ''.join(buffer) ### (code adapted from: http://www.python.org/doc/lib/dom-example.html) For example: ### >>> doc = xml.dom.minidom.parseString("helloworld") >>> for bnode in doc.getElementsByTagName('b'): ... print "I see:", get_text(bnode) ... I see: hello I see: world ### > Could any one help me directing how to get the elements in that tag. One way to approach structured parsing problems systematically is to write a function for each particular element type that you're trying to parse. >From the sample XML that you've shown us, it appears that your document consists of a single 'Hit' root node. Each 'Hit' appears to have a 'Hit_hsps' element. A 'Hit_hsps' element can have several 'Hsp's associated to it. And a 'Hsp' element contains those coordinates that you are interested in. More formally, we can structure our parsing code to match the structure of the data: ### pseudocode ### def parse_Hsp(node): ## get at the Hit_hsps element, and call parse_Hit_hsps() on it. def parse_Hit_hsps(node): ## get all of the Hsp elements, and call parse_Hsp() on each one of ## them. def parse_Hsp(node): ## extract the query and hit coordinates out of the node. ###### To see another example of this kind of program structure, see: http://www.python.org/doc/lib/dom-example.html Please feel free to ask more questions. Good luck to you. From ps_python at yahoo.com Sat Jan 1 07:57:18 2005 From: ps_python at yahoo.com (kumar s) Date: Sat Jan 1 07:57:21 2005 Subject: [Tutor] Parsing a block of XML text In-Reply-To: Message-ID: <20050101065718.85669.qmail@web53706.mail.yahoo.com> Hi Danny, Thanks for your reply. I have been using BioPython for long time. I found their BLAST parser buggy (IMPO), otherwise BioPython has more cool modules. In my case Parser did not iterate over hits and it turned out difficult for me to look into it in detail. Also, I wanted to work more on my own so that I get more understanding over parsing XML documents. I was about to post to the list seeking some systematic explanation to the example on PLR. http://www.python.org/doc/lib/dom-example.html Frankly it looked more complex. could I request you to explain your pseudocode. It is confusing when you say call a function within another function. ### pseudocode ### > def parse_Hsp(node): > ## get at the Hit_hsps element, and call > parse_Hit_hsps() on it. > > > def parse_Hit_hsps(node): > ## get all of the Hsp elements, and call > parse_Hsp() on each one of > ## them. > > > def parse_Hsp(node): > ## extract the query and hit coordinates out of > the node. > ###### After posting my previous question, I have been working to get the output. I wrote the following lines. (Ref: Jones and Drakes - Python & XML) from xml.dom import minidom import sys def findTextnodes(nodeList): for subnode in nodeList: if subnode.nodeType == subnode.ELEMENT_NODE: print "Element node: " + subnode.tagName findTextnodes(subnode.childNodes) elif subnode.nodeType == subnode.TEXT_NODE: print "text node:" + subnode.data doc = minidom.parse(sys.stdin) findTextnodes(doc.childNodes) My Aim: I wanted all to extract HSP that are more than 1. The significance of this is that I can create an exon structure based on the result. My intended output is a tab delim. txt with: Hit_id Hsp_evalue Hsp_query-from Hsp_query-to Hsp_hit-from Hsp_hit-to I will work on along your suggestions. I have looked at the example that you asked me to look at. I did not understand that. I will post my questions in my next e-mail. Thanks. -K My output: Although this is not what I wanted :-( Element node: BlastOutput_query-ID text node:lcl|1_4694 text node: Element node: BlastOutput_query-def text node:gi|4508026|ref|NM_003423.1| Homo sapiens zinc finger protein 43 (HTF6) (ZNF43), mRNA text node: Element node: BlastOutput_query-len text node:3003 text node: Element node: BlastOutput_param text node: Element node: Parameters text node: Element node: Parameters_expect text node:10 text node: Element node: Parameters_sc-match text node:1 text node: Element node: Parameters_sc-mismatch text node:-3 text node: Element node: Parameters_gap-open text node:5 text node: Element node: Parameters_gap-extend text node:2 text node: text node: text node: Element node: BlastOutput_iterations text node: Element node: Iteration text node: Element node: Iteration_iter-num text node:1 text node: Element node: Iteration_hits text node: Element node: Hit text node: Element node: Hit_num text node:1 text node: Element node: Hit_id text node:gi|22814739|gb|BU508506.1| text node: Element node: Hit_def text node:AGENCOURT_10094591 NIH_MGC_71 Homo sapiens cDNA clone IMAGE:6502598 5', mRNA sequence. text node: Element node: Hit_accession text node:BU508506 text node: Element node: Hit_len text node:912 text node: Element node: Hit_hsps text node: Element node: Hsp text node: Element node: Hsp_num text node:1 text node: Element node: Hsp_bit-score text node:1485.28 text node: Element node: Hsp_score text node:749 text node: Element node: Hsp_evalue text node:0 text node: Element node: Hsp_query-from text node:715 text node: Element node: Hsp_query-to text node:1513 text node: Element node: Hsp_hit-from text node:1 text node: Element node: Hsp_hit-to text node:804 text node: Element node: Hsp_query-frame text node:1 text node: Element node: Hsp_hit-frame text node:1 text node: Element node: Hsp_identity text node:794 text node: Element node: Hsp_positive text node:794 text node: Element node: Hsp_gaps text node:5 text node: Element node: Hsp_align-len text node:804 text node: Element node: Hsp_qseq text node:TGGATTTAACCAATGTTTGCCAGCTACCCAGAGCAAAATATTTCTATTTGATAAATGTGTGAAAGCCTTTCATAAATTTTCAAATTCAAACAGACATAAGATAAGCCATACTGAAAAAAAACTTTTCAAATGCAAAGAATGTGGCAAATCATTTTGCATGCTTCCACATCTAGCTCAACATAAAATAATTCATACCAGAGTGAATTTCTGCAAATGTGAAAAATGTGGAAAAGCTTTTAACTGCCCTTCAATCATCACTAAACATAAGAGAATTAATACTGGAGAGAAACCCTACACATGTGAAGAATGTGGCAAAGTCTTTAATTGGTCCTCACGCCTTACTACACATAAAAAAAATTATACTAGATACAAACTCTACAAATGTGAAGAATGTGGCAAAGCTTTTAACAAGTCCTCAATCCTTACTACCCATAAGATAATTCGCACTGGAGAGAAATTCTACAAATGTAAAGAATGTGCCAAAGCTTTTAACCAATCCTCAAACCTTACTGAACATAAGAAAATTCATCCTGGAGAGAAACCTTACAAATGTGAAGAATGTGGCAAAGCCTTTAACTGGCCCTCAACTCTTACTAAACATAAGAGAATTCATACTGGAGAGAAACCCTACACATGTGAAGAATGTGGCAAAGCTTTTAACCAGTTCTCAAACCTTACTACACATAAGAGAATCCATACTGCAGAGAAATTCTATAAATGTACAGAATGT-GGTGAAGCTTTT-AGCCGGTCCTCAAACCTTACTAAACAT-AAGAAAATTCATACT--GAAAAGAAACCCTAC text node: Element node: Hsp_hseq text node:TGGATTTAACCAATGTTTGCCAGCTACCCAGAGCAAAATATTTCTATTTGATAAATGTGTGAAAGCCTTTCATAAATTTTCAAATTCAAACAGACATAAGATAAGCCATACTGAAAAAAAACTTTTCAAATGCAAAGAATGTGGCAAATCATTTTGCATGCTTCCACATCTAGCTCAACATAAAATAATTCATACCAGAGTGAATTTCTGCAAATGTGAAAAATGTGGAAAAGCTTTTAACTGCCCTTCAATCATCACTAAACATAAGAGAATTAATACTGGAGAGAAACCCTACACATGTGAAGAATGTGGCAAAGTCTTTAATTGGTCCTCACGCCTTACTACACATAAAAAAAATTATACTAGATACAAACTCTACAAATGTGAAGAATGTGGCAAAGCTTTTAACAAGTCCTCAATCCTTACTACCCATAAGATAATTCGCACTGGAGAGAAATTCTACAAATGTAAAGAATGTGCCAAAGCTTTTAACCAATCCTCAAACCTTACTGAACATAAGAAAATTCATCCTGGAGAGAAACCTTACAAATGTGAAGAATGTGGCAAAGCCTTTAACTGGCCCTCAACTCTTACTAAACATAAGAGAATTCATACTGGAGAGAAACCCTACACATGTGAAGAATGTGGCAAAGCCTTTAACCAGTTCTCAAACCTTACTACACATAAGAGAATCCATACTGCAGAGAAATTCTATAAATGTACAGAATGTGGGTGAAGCTTTTAACCCGGCCCTCAAACCTTACTAAACATAAAAAAAATTCATACTTGAAAAAGAAACCCTAC text node: Element node: Hsp_midline text node:|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||| | |||| |||||||||||||||||||| || |||||||||||| |||||||||||||| text node: text node: Element node: Hsp text node: Element node: Hsp_num text node:2 text node: --- Danny Yoo wrote: > > > On Fri, 31 Dec 2004, kumar s wrote: > > > I am trying to parse BLAST output (Basic Local > Alignment Search Tool, > > size around more than 250 KB ). > > [xml text cut] > > > Hi Kumar, > > Just as a side note: have you looked at Biopython > yet? > > http://biopython.org/ > > I mention this because Biopython comes with parsers > for BLAST; it's > possible that you may not even need to touch XML > parsing if the BLAST > parsers in Biopython are sufficiently good. Other > people have already > solved the parsing problem for BLAST: you may be > able to take advantage of > that work. > > > > I wanted to parse out : > > > > > > > > > Ok, I see that you are trying to get the content of > the High Scoring Pair > (HSP) query and hit coordinates. > > > > > I wrote a ver small 4 line code to obtain it. > > > > for bls in doc.getElementsByTagName('Hsp_num'): > > bls.normalize() > > if bls.firstChild.data >1: > > print bls.firstChild.data > > This might not work. 'bls.firstChild.data' is a > string, not a number, so > the expression: > > bls.firstChild.data > 1 > > is most likely buggy. Here, try using this function > to get the text out > of an element: > > ### > def get_text(node): > """Returns the child text contents of the > node.""" > buffer = [] > for c in node.childNodes: > if c.nodeType == c.TEXT_NODE: > buffer.append(c.data) > return ''.join(buffer) > ### > > (code adapted from: > http://www.python.org/doc/lib/dom-example.html) > > > > For example: > > ### > >>> doc = > xml.dom.minidom.parseString("helloworld") > >>> for bnode in doc.getElementsByTagName('b'): > ... print "I see:", get_text(bnode) > ... > I see: hello > I see: world > ### > > > > > > Could any one help me directing how to get the > elements in that tag. > > One way to approach structured parsing problems > systematically is to write > a function for each particular element type that > you're trying to parse. > > From the sample XML that you've shown us, it appears > that your document > consists of a single 'Hit' root node. Each 'Hit' > appears to have a > 'Hit_hsps' element. A 'Hit_hsps' element can have > several 'Hsp's > associated to it. And a 'Hsp' element contains > those coordinates that you > are interested in. > > > More formally, we can structure our parsing code to > match the structure > of the data: > > ### pseudocode ### > def parse_Hsp(node): > ## get at the Hit_hsps element, and call > parse_Hit_hsps() on it. > > > def parse_Hit_hsps(node): > ## get all of the Hsp elements, and call > parse_Hsp() on each one of > ## them. > > > def parse_Hsp(node): > ## extract the query and hit coordinates out of > the node. > ###### > > > To see another example of this kind of program > structure, see: > > http://www.python.org/doc/lib/dom-example.html > > > Please feel free to ask more questions. Good luck > to you. > > __________________________________ Do you Yahoo!? Yahoo! Mail - You care about security. So do we. http://promotions.yahoo.com/new_mail From davholla2002 at yahoo.co.uk Sat Jan 1 12:05:25 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sat Jan 1 12:05:29 2005 Subject: [Tutor] =?iso-8859-1?q?Using_=A3_in_python?= In-Reply-To: <20050101030048.E42D51E400F@bag.python.org> Message-ID: <20050101110525.18404.qmail@web25402.mail.ukl.yahoo.com> Kent, When I tried '\xc2' nothing happened. I should have said that is a game using pygame. Kent --- tutor-request@python.org wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body > 'help' to > tutor-request@python.org > > You can reach the person managing the list at > tutor-owner@python.org > > When replying, please edit your Subject line so it > is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: O.T. (Michael Lange) > 2. OT (David Holland) > 3. ASCII encoding for a ? sign (David Holland) > 4. Fwd: ASCII encoding for a ? sign (David > Holland) > 5. Parsing a block of XML text (kumar s) > 6. Parsing a block of XML text (kumar s) > 7. Re: cgi.FieldStorage and dictionary.get(' ') > (Patric Michael) > 8. Re: O.T. (Brian van den Broek) > 9. Re: Fwd: ASCII encoding for a ? sign (Kent > Johnson) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 31 Dec 2004 16:01:10 +0100 > From: Michael Lange > Subject: Re: [Tutor] O.T. > To: tutor@python.org > Message-ID: > <20041231160110.51e519e8.klappnase@freenet.de> > Content-Type: text/plain; charset=US-ASCII > > > * Jacob S. [041228 01:42]: > > > But who are you all, what are you're ages, what > do you do, marriage status, > > > etc? > > 37, no kids but a girlfriend with a cat. I work at a > clinical laboratory in a hospital in germany. > I'm just a hobby programmer and started with python > about 2 years ago; I chose python as my first > (and yet only) language because I happened to find a > cheap german version of Ivan van Laningham's > "Teach yourself python in 24 hours" in a local > bookstore when I was looking for something to > start with. The book had a sticker on it that said > something like "No programming knowledge required!" > which looked very promising to me back then. > > A happy new year to all of you > > Michael > > > ------------------------------ > > Message: 2 > Date: Fri, 31 Dec 2004 16:16:35 +0000 (GMT) > From: David Holland > Subject: [Tutor] OT > To: tutor@python.org > Message-ID: > <20041231161635.52669.qmail@web25406.mail.ukl.yahoo.com> > Content-Type: text/plain; charset=iso-8859-1 > > I am 31 married, living in London UK. > I can play guitar but I am not that good. > I work in IT as Oracle support but the only > languages > I know apart from basic python is basic PL/SQL and > of > course SQL. Of course I know how to use various > Oracle applications. > I studied Chemistry but sadly there were no jobs so > I > moved in IT. > > > > > > ___________________________________________________________ > > ALL-NEW Yahoo! Messenger - all new features - even > more fun! http://uk.messenger.yahoo.com > > > ------------------------------ > > Message: 3 > Date: Fri, 31 Dec 2004 16:28:59 +0000 (GMT) > From: David Holland > Subject: [Tutor] ASCII encoding for a ? sign > To: tutor@python.org > Message-ID: > <20041231162859.54839.qmail@web25406.mail.ukl.yahoo.com> > Content-Type: text/plain; charset=iso-8859-1 > > I ran a program with a "?" sign, and I got this > message :- > > "sys:1: DeprecationWarning: Non-ASCII character > '\xc2' > in file birdgame32a.py on line 93, but no encoding > declared; see > http://www.python.org/peps/pep-0263.html > for details' > I looked at the message but I am not sure what > encoding to use for "?". > > > > > > ___________________________________________________________ > > ALL-NEW Yahoo! Messenger - all new features - even > more fun! http://uk.messenger.yahoo.com > > > ------------------------------ > > Message: 4 > Date: Fri, 31 Dec 2004 16:42:10 +0000 (GMT) > From: David Holland > Subject: [Tutor] Fwd: ASCII encoding for a ? sign > To: tutor@python.org > Message-ID: > <20041231164210.49306.qmail@web25403.mail.ukl.yahoo.com> > Content-Type: text/plain; charset=iso-8859-1 > > Apologies, > I did not see that Kent had already answered this ! > Thanks Kent I put that encoding and it works fine. > Although surprisingly the other idea :- > write the string with '\xc2' did not work. > > --- David Holland wrote: > > > Date: Fri, 31 Dec 2004 16:28:59 +0000 (GMT) > > From: David Holland > > Subject: ASCII encoding for a ? sign > > To: tutor@python.org > > > > I ran a program with a "?" sign, and I got this > > message :- > > > > "sys:1: DeprecationWarning: Non-ASCII character > > '\xc2' > > in file birdgame32a.py on line 93, but no encoding > > declared; see > > http://www.python.org/peps/pep-0263.html > > for details' > > I looked at the message but I am not sure what > > encoding to use for "?". > > > > > > > > > > > > > ___________________________________________________________ > > > > ALL-NEW Yahoo! Messenger - all new features - even > > more fun! http://uk.messenger.yahoo.com > > > > > > > > ___________________________________________________________ > > ALL-NEW Yahoo! Messenger - all new features - even > more fun! http://uk.messenger.yahoo.com > > > ------------------------------ > > Message: 5 > Date: Fri, 31 Dec 2004 14:15:42 -0800 (PST) > From: kumar s > Subject: [Tutor] Parsing a block of XML text > To: tutor@python.org > Message-ID: > <20041231221543.64372.qmail@web53707.mail.yahoo.com> > Content-Type: text/plain; charset=us-ascii > > === message truncated === ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From keridee at jayco.net Sat Jan 1 19:29:29 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 1 20:59:04 2005 Subject: [Tutor] O.T. References: <000501c4ec5b$6ec59c80$8c5428cf@JSLAPTOP> <41D2C580.5090903@tds.net> Message-ID: <006701c4f03c$5b541e80$4b5428cf@JSLAPTOP> > In love with computers and programming since forever (well, maybe only since 1968 or so...) > Working as a professional programmer mostly since 1977. Languages I have actually been paid to > program in include It seems like forever to someone who hasn't seen before 1990. : ) > Python is a lot of fun. The downside of that is that it makes Java and C++ (both of which I used to > like a lot) look like a lot of work for little result. I seem to hear that a lot on the list... > I taught an Introduction to Programming with Python course for a local adult ed program last fall > and I will be teaching Python for Programmers in the spring. I might need that... > I like answering questions on the Tutor list because > - They are like little puzzles to solve, and I like puzzles > - I learn a lot - many times a question will be about a part of Python that I don't know, so I learn > enough to answer the questions > - OK I admit it, it makes me feel smart :-) That describes my experience on the tutor list exactly! > If you really want to know more about me see my web site > http://www.kentsjohnson.com > > Kent > > Jacob S. wrote: > > I hate to sound weird... > > > > But who are you all, what are you're ages, what do you do, marriage status, > > etc? > > You obviously don't have to answer, I'm just curious who I'm boldly sending > > emails to. > > > > Jacob Schmidt > > > > P.S. > > I'm a student. 14 years. Play the piano better than I write scripts. Single. > > etc. > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Sat Jan 1 21:57:43 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 1 21:57:40 2005 Subject: [Tutor] Help with testing. Message-ID: <00a301c4f044$8bcbad00$4b5428cf@JSLAPTOP> Hi, A little while back somebody suggested doctesting, I think it was. Well, anyway, it was the testing that took testing material from the doc strings of functions in the format of an interpreter and the desired result (example below). Could anyone help by pointing me to structured documentation on how to use this, or even the name of the testing module(s)? Even nicer, a way to put the testing code in a seperate module so all I have to do is something like... pseudocode def test(obj): ## Testing particulars -- parameter is a module.function that has a docstring with testing material m = raw_input('What is the module? ') n = raw_input('What is the function? ') obj = eval(m+'.'+n) ## Given that I'm the only one who will be using it and I will not try to crack my computer... test(obj) Ohhhhh.. and the doc string example... def function(a,b): """A function to do what you want it to. >>> function('3','a') '3a' >>> function('ask','too') 'asktoo' """ return a+b From keridee at jayco.net Sat Jan 1 22:10:36 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 1 22:10:37 2005 Subject: [Tutor] How to put my functions in an array References: <1104347290.31902.28.camel@6-allhosts> Message-ID: <00bb01c4f046$5a765d20$4b5428cf@JSLAPTOP> Hello. I believe it was Danny Yoo who told me about mapping functions a while back on the list... It goes along these lines... funct = {'Add Virt':addvirt,'Remove Virt':remvirt,'More Stuff':more,"Extras":extra} def addvirt(): pass def remvirt(): pass def more(): pass def extra(): pass def dispatch(name): if name: funct['name']() else: pass print """\ Actions include: Add Virt Remove Virt More Stuff Extras quit """ while 1: command = raw_input("What do you want to do? ") if command == 'quit': break dispatch(command) HTH, Jacob Schmidt > def addvirt(): > pass > def remvirt(): > pass > > PROVISION_ACTIONS=[('addvirt','Add Virt'),('remvirt','Remove Virt'),] > formhandlers={} > > # this works > formhandlers["addvirt"]=addvirt > formhandlers["remvirt"]=remvirt > > # this does not work: > for verb,verb_desc in PROVISION_ACTIONS: > if callable(verb): > formhandlers[verb]=verb > > I tried a few different syntaxes but to no avail... do I need things > like: getattr()? > > Thanks alot > Mohamed~ > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Sat Jan 1 22:16:31 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 1 22:16:29 2005 Subject: [Tutor] How to substitute an element of a list as a pattern forre.compile() References: <20041230010123.91028.qmail@web53709.mail.yahoo.com> Message-ID: <00ce01c4f047$2d223fa0$4b5428cf@JSLAPTOP> Nobody explicitly mentioned that you're trying to make a pattern from an integer and not a string, which, I believe is required. Also, Rich needs to make the % formatting "%d"%x instead of "%s"%x because he showed that x is an integer not a string. There's my two bits. Jacob Schmidt > Hi Group: > > I have Question: > How can I substitute an object as a pattern in making > a pattern. > > >>> x = 30 > >>> pattern = re.compile(x) > > > > > My situation: > > I have a list of numbers that I have to match in > another list and write them to a new file: > > List 1: range_cors > >>> range_cors[1:5] > ['161:378', '334:3', '334:4', '65:436'] > > List 2: seq > >>> seq[0:2] > ['>probe:HG-U133A_2:1007_s_at:416:177; > Interrogation_Position=3330; Antisense;', > 'CACCCAGCTGGTCCTGTGGATGGGA'] > > > A slow method: > >>> sequences = [] > >>> for elem1 in range_cors: > for index,elem2 in enumerate(seq): > if elem1 in elem2: > sequences.append(elem2) > sequences.append(seq[index+1]) > > This process is very slow and it is taking a lot of > time. I am not happy. > > > > A faster method (probably): > > >>> for i in range(len(range_cors)): > for index,m in enumerate(seq): > pat = re.compile(i) > if re.search(pat,seq[m]): > p.append(seq[m]) > p.append(seq[index+1]) > > > I am getting errors, because I am trying to create an > element as a pattern in re.compile(). > > > Questions: > > 1. Is it possible to do this. If so, how can I do > this. > > Can any one help correcting my piece of code and > suggesting where I went wrong. > > Thank you in advance. > > > -K > > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From tegmine at gmail.com Sat Jan 1 22:19:45 2005 From: tegmine at gmail.com (Luis N) Date: Sat Jan 1 22:19:47 2005 Subject: [Tutor] *args, **kwargs Message-ID: <77bfa81a0501011319304788aa@mail.gmail.com> Hi, I gave this a quick read: http://aspn.activestate.com/ASPN/Mail/Message/573292 I'm wondering how I can turn a variable number of keyword arguments passed to a class into variables for use in said class: #This so doesn't work class SomethingLikeThis: def __init__(self, **kwargs): self.kwargs = kwargs def show(self): for k in self.kwargs.keys(): v = selfkwargs.get(k) print v I'm probably misunderstanding the purpose of *args and **kwargs totally. From kent37 at tds.net Sat Jan 1 22:21:18 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 1 22:21:25 2005 Subject: [Tutor] Help with testing. In-Reply-To: <00a301c4f044$8bcbad00$4b5428cf@JSLAPTOP> References: <00a301c4f044$8bcbad00$4b5428cf@JSLAPTOP> Message-ID: <41D7144E.60102@tds.net> Jacob, The module is doctest, if you look at the docs you will see examples of how to use it. I'm not sure if there is a way to run the doctests for a single function - the doctest functions seem to be oriented toward testing entire modules. To test a module selected by user input, you could do something like this: import doctest m = raw_input('What is the module? ') exec 'import %s as _test_module' % m doctest.testmod(_test_module) However, I suggest that you set up your tests so you can run them without having to type the name of the module. If you run your tests much, it will get tedious to have to keep entering the name. One way to do this is to put the test code in the main function of the module, so when you run the module it runs the tests. This works well for library modules that are intended to be imported. If you are writing an application module that is to be run as main, then you need to put the test driver in a separate module. I generally write a separate test module for each module of my applications. This lets me ship the modules to customers without the test code. Kent Jacob S. wrote: > Hi, > > A little while back somebody suggested doctesting, I think it was. Well, > anyway, it was the testing that took testing material from the doc strings > of functions in the format of an interpreter and the desired result (example > below). Could anyone help by pointing me to structured documentation on how > to use this, or even the name of the testing module(s)? > Even nicer, a way to put the testing code in a seperate module so all I have > to do is something like... > > pseudocode > > def test(obj): > ## Testing particulars -- parameter is a module.function that has a > docstring with testing material > > m = raw_input('What is the module? ') > n = raw_input('What is the function? ') > obj = eval(m+'.'+n) ## Given that I'm the only one who will be using it and > I will not try to crack my computer... > test(obj) > > Ohhhhh.. > and the doc string example... > > > def function(a,b): > """A function to do what you want it to. > > >>> function('3','a') > '3a' > >>> function('ask','too') > 'asktoo' > """ > return a+b > > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Sat Jan 1 22:25:24 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 1 22:25:32 2005 Subject: [Tutor] How to substitute an element of a list as a pattern forre.compile() In-Reply-To: <00ce01c4f047$2d223fa0$4b5428cf@JSLAPTOP> References: <20041230010123.91028.qmail@web53709.mail.yahoo.com> <00ce01c4f047$2d223fa0$4b5428cf@JSLAPTOP> Message-ID: <41D71544.3090903@tds.net> Jacob S. wrote: > Rich needs to > make the % formatting "%d"%x instead of "%s"%x because he showed that x > is an integer not a string. Actually %s formatting is very flexible and forgiving, it outputs str(x) whatever x is. For example: >>> for x in ['string', 10, True, [1,2,3] ]: ... print '%s' %x ... string 10 True [1, 2, 3] Kent From kent37 at tds.net Sat Jan 1 22:32:25 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 1 22:32:33 2005 Subject: [Tutor] *args, **kwargs In-Reply-To: <77bfa81a0501011319304788aa@mail.gmail.com> References: <77bfa81a0501011319304788aa@mail.gmail.com> Message-ID: <41D716E9.7070909@tds.net> You seem to be on the right track Luis N wrote: > I'm wondering how I can turn a variable number of keyword arguments > passed to a class into variables for use in said class: > > #This so doesn't work It is helpful if you tell us what you tried and how it failed... > > class SomethingLikeThis: > def __init__(self, **kwargs): > self.kwargs = kwargs > > def show(self): > for k in self.kwargs.keys(): > v = selfkwargs.get(k) This line does have an error; it is missing a '.': v = self.kwargs.get(k) > print v This works for me: class SomethingLikeThis: def __init__(self, **kwargs): self.kwargs = kwargs def show(self): for k, v in self.kwargs.items(): print k, '=', v s = SomethingLikeThis(a='abc', num=1, phone='888-555-1212') s.show() If that doesn't do what you want then give us more details. Kent From keridee at jayco.net Sat Jan 1 22:35:31 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 1 22:35:30 2005 Subject: [Tutor] How to substitute an element of a list as apattern forre.compile() References: <20041230010123.91028.qmail@web53709.mail.yahoo.com><00ce01c4f047$2d223fa0$4b5428cf@JSLAPTOP> <41D71544.3090903@tds.net> Message-ID: <00e601c4f049$d4df0140$4b5428cf@JSLAPTOP> > Actually %s formatting is very flexible and forgiving, it outputs str(x) whatever x is. For example: > >>> for x in ['string', 10, True, [1,2,3] ]: > ... print '%s' %x > ... > string > 10 > True > [1, 2, 3] > > Kent That's cool! I didn't know that. I guess I'm crazy... : ) (We already knew that here.) Jacob From alan.gauld at freenet.co.uk Sun Jan 2 00:01:45 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 2 00:01:47 2005 Subject: [Tutor] *args, **kwargs References: <77bfa81a0501011319304788aa@mail.gmail.com> Message-ID: <001b01c4f055$de76cab0$62bc8651@xp> > class SomethingLikeThis: > def __init__(self, **kwargs): > self.kwargs = kwargs > > def show(self): > for k in self.kwargs.keys(): > v = selfkwargs.get(k) is this a typo in the mail or a real error? v = self.kwargs.get(k) > print v > > I'm probably misunderstanding the purpose of *args and **kwargs totally. Don't think so, here's my session: >>> class C: ... def __init__(s,**kw): ... s.kw = kw ... def show(s): ... for k in s.kw.keys(): ... print s.kw[k] ... >>> c = C(a=42, b=54) >>> c.show() 42 54 >>> HTH, Alan G. From keridee at jayco.net Sun Jan 2 03:21:39 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 2 03:21:38 2005 Subject: [Tutor] doctest Message-ID: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP> Hi. Okay, so I look at the documentation at it says (in my words): "First Class - DocTest -- Make a test object with such and such attributes that you can test. Second Class - i don't remember the name - Make Jacob look stupid with big words Third Class - DocTestSuite - Convert a doctest object to a unittest object - Okay... so how does that help? Fourth Class - DocTestFinder - Find the docstrings that contain test code and extract them." So, my question, I guess, is How did the documentation help, and, How do I have doctest test all of my module's function's docstrings? Thanks in advance, Jacob Schmidt From kent37 at tds.net Sun Jan 2 03:42:25 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 2 03:42:34 2005 Subject: [Tutor] doctest In-Reply-To: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP> References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP> Message-ID: <41D75F91.7020907@tds.net> What docs are you looking at?? The module docs at http://docs.python.org/lib/module-doctest.html have a complete example of testing a module with a main function. Or you can use the code in my last post. Kent Jacob S. wrote: > Hi. > > Okay, so I look at the documentation at it says (in my words): > > "First Class - DocTest -- Make a test object with such and such attributes > that you can test. > Second Class - i don't remember the name - Make Jacob look stupid with big > words > Third Class - DocTestSuite - Convert a doctest object to a unittest object - > Okay... so how does that help? > Fourth Class - DocTestFinder - Find the docstrings that contain test code > and extract them." > > So, my question, I guess, is How did the documentation help, and, How do I > have doctest test all of my module's function's docstrings? > > Thanks in advance, > Jacob Schmidt > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Sun Jan 2 04:21:22 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 2 04:21:22 2005 Subject: [Tutor] doctest References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP> <41D75F91.7020907@tds.net> Message-ID: <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> I think I'm losing my mind... Maybe it's because I go back to school the day after tomorrow? The thing that helped the most was the -v parameter... Even so, doctest doesn't seem to recognize the module level docstring. It will run the test inside the functions, but it says there isn't a test on the module level. I put the docstring just like in the example at the link you provided... Also, anything I can do... Presently, since I'm running windows xp, I would have to hunt for the command prompt and type in the command '"C:\python24\python.exe" "C:\documents and settings\jacob\desktop\working python programs\testmodules.py" -v' ...or make a batch file to do it for me... How can I make testmodules.py (shown below) append the -v to itself? Is there a self.results or something in testmod? ## testmodules.py ########### import doctest modtotest = 'FractionReducer2' exec "import %s" % modtotest doctest.testmod(eval(modtotest)) raw_input() ############################# Thanks, Jacob Schmidt > What docs are you looking at?? The module docs at http://docs.python.org/lib/module-doctest.html > have a complete example of testing a module with a main function. Or you can use the code in my last > post. > > Kent > > Jacob S. wrote: > > Hi. > > > > Okay, so I look at the documentation at it says (in my words): > > > > "First Class - DocTest -- Make a test object with such and such attributes > > that you can test. > > Second Class - i don't remember the name - Make Jacob look stupid with big > > words > > Third Class - DocTestSuite - Convert a doctest object to a unittest object - > > Okay... so how does that help? > > Fourth Class - DocTestFinder - Find the docstrings that contain test code > > and extract them." > > > > So, my question, I guess, is How did the documentation help, and, How do I > > have doctest test all of my module's function's docstrings? > > > > Thanks in advance, > > Jacob Schmidt > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Sun Jan 2 04:27:24 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 2 04:27:27 2005 Subject: [Tutor] doctest References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP><41D75F91.7020907@tds.net> <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> Message-ID: <001001c4f07a$fbabc070$915328cf@JSLAPTOP> I forgot to mention... When I explicitly define the variable __doc__ at the module level, it *does* recognize the module level docstring. > I think I'm losing my mind... > > Maybe it's because I go back to school the day after tomorrow? > The thing that helped the most was the -v parameter... > > Even so, doctest doesn't seem to recognize the module level docstring. > It will run the test inside the functions, but it says there isn't a test on > the module level. > I put the docstring just like in the example at the link you provided... > > Also, anything I can do... Presently, since I'm running windows xp, I would > have to hunt for the command prompt and type in the command > > '"C:\python24\python.exe" "C:\documents and settings\jacob\desktop\working > python programs\testmodules.py" -v' > > ...or make a batch file to do it for me... > How can I make testmodules.py (shown below) append the -v to itself? Is > there a self.results or something in testmod? > > ## testmodules.py ########### > import doctest > > modtotest = 'FractionReducer2' > > exec "import %s" % modtotest > doctest.testmod(eval(modtotest)) > raw_input() > ############################# > > > Thanks, > Jacob Schmidt > > > > What docs are you looking at?? The module docs at > http://docs.python.org/lib/module-doctest.html > > have a complete example of testing a module with a main function. Or you > can use the code in my last > > post. > > > > Kent > > > > Jacob S. wrote: > > > Hi. > > > > > > Okay, so I look at the documentation at it says (in my words): > > > > > > "First Class - DocTest -- Make a test object with such and such > attributes > > > that you can test. > > > Second Class - i don't remember the name - Make Jacob look stupid with > big > > > words > > > Third Class - DocTestSuite - Convert a doctest object to a unittest > object - > > > Okay... so how does that help? > > > Fourth Class - DocTestFinder - Find the docstrings that contain test > code > > > and extract them." > > > > > > So, my question, I guess, is How did the documentation help, and, How do > I > > > have doctest test all of my module's function's docstrings? > > > > > > Thanks in advance, > > > Jacob Schmidt > > > > > > _______________________________________________ > > > 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 maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From patric at usa.net Sun Jan 2 05:01:51 2005 From: patric at usa.net (Patric Michael) Date: Sun Jan 2 04:59:53 2005 Subject: [Tutor] doctest In-Reply-To: <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> Message-ID: <41D701AF.14878.A11BC3D@localhost> > Also, anything I can do... Presently, since I'm running windows xp, I > would have to hunt for the command prompt and type in the command Click Start, click Run, type CMD, press Enter. Voila! :) Patric > > '"C:\python24\python.exe" "C:\documents and > settings\jacob\desktop\working python programs\testmodules.py" -v' > > ...or make a batch file to do it for me... > How can I make testmodules.py (shown below) append the -v to itself? > Is there a self.results or something in testmod? > > ## testmodules.py ########### > import doctest > > modtotest = 'FractionReducer2' > > exec "import %s" % modtotest > doctest.testmod(eval(modtotest)) > raw_input() > ############################# > > > Thanks, > Jacob Schmidt > > > > What docs are you looking at?? The module docs at > http://docs.python.org/lib/module-doctest.html > > have a complete example of testing a module with a main function. Or > > you > can use the code in my last > > post. > > > > Kent > > > > Jacob S. wrote: > > > Hi. > > > > > > Okay, so I look at the documentation at it says (in my words): > > > > > > "First Class - DocTest -- Make a test object with such and such > attributes > > > that you can test. > > > Second Class - i don't remember the name - Make Jacob look stupid > > > with > big > > > words > > > Third Class - DocTestSuite - Convert a doctest object to a > > > unittest > object - > > > Okay... so how does that help? > > > Fourth Class - DocTestFinder - Find the docstrings that contain > > > test > code > > > and extract them." > > > > > > So, my question, I guess, is How did the documentation help, and, > > > How do > I > > > have doctest test all of my module's function's docstrings? > > > > > > Thanks in advance, > > > Jacob Schmidt > > > > > > _______________________________________________ > > > 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 maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From dyoo at hkn.eecs.berkeley.edu Sun Jan 2 08:31:45 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 2 08:31:49 2005 Subject: [Tutor] Parsing a block of XML text In-Reply-To: <20050101065718.85669.qmail@web53706.mail.yahoo.com> Message-ID: On Fri, 31 Dec 2004, kumar s wrote: > http://www.python.org/doc/lib/dom-example.html > > Frankly it looked more complex. could I request you to explain your > pseudocode. It is confusing when you say call a function within another > function. Hi Kumar, A question, though: can you try to explain what part feels weird about having a function call another function? Is it something specific to XML processing, or a more general problem? That is, do you already feel comfortable with writing and using "helper" functions? If you're feeling uncomfortable with the idea of functions calling functions, then that's something we should probably concentrate on, because it's really crucial to use this technique, especially on structured data like XML. As a concrete toy example of a function that calls another function, we can use the overused hypotenuse function. Given right triangle leg lengths 'a' and 'b', this function returns the length of the hypotenuse: ### def hypotenuse(a, b): return (a**2 + b**2)**0.5 ### This definition works, but we can use helper functions to make the hypotenuse function a little bit more like English: ### def sqrt(x): return x ** 0.5 def square(x): return x * x def hypotenuse(a, b): return sqrt(square(a) + square(b)) ### In this variation, the rewritten hypotenuse() function uses the other two functions as "helpers". The key idea is that the functions that we write can then be used by anything that needs it. Another thing that happens is that hypotenuse() doesn't have to know how sqrt() and square() are defined: it just depends on the fact that sqrt() and square() are out there, and it can just use these as tools. Computer scientists call this "abstraction". Here is another example of another "helper" function that comes in handy when we do XML parsing: ### def get_children(node, tagName): """Returns the children elements of the node that have this particular tagName. This is different from getElementsByTagName() because we only look shallowly at the immediate children of the given node.""" children = [] for n in node.childNodes: if n.nodeType == n.ELEMENT_NODE and n.tagName == tagName: children.append(n) return children ### For example: ### >>> import xml.dom.minidom >>> dom = xml.dom.minidom.parseString("

helloworld

") >>> >>> dom.firstChild >>> >>> get_children(dom.firstChild, "a") [, ] ### > It is confusing when you say call a function within another function. Here's a particular example that uses this get_children() function and that get_text() function that we used in the earlier part of this thread. ### def parse_Hsp(hsp_node): """Prints out the query-from and query-to of an Hsp node.""" query_from = get_text(get_children(hsp_node, "Hsp_query-from")[0]) query_to = get_text(get_children(hsp_node, "Hsp_query-to")[0]) print query_from print query_to ### This function only knows how to deal with Hsp_node elements. As soon as we can dive through our DOM tree into an Hsp element, we should be able to extract the data we need. Does this definition of parse_Hsp() make sense? You're not going to be able to use it immediately for your real problem yet, but you can try it on a sample subset of your XML data: ### sampleData = """ 1 1164.13 587 0 1 587 """ doc = xml.dom.minidom.parseString(sampleData) parse_Hsp(doc.firstChild) ### to see how it works so far. This example tries to show the power of being able to call helper functions. If we were to try to write this all using DOM primitives, the end result would look too ugly for words. But let's see it anyway. *grin* ### def parse_Hsp(hsp_node): ## without using helper functions: """Prints out the query-from and query-to of an Hsp node.""" query_from, query_to = "", "" for child in hsp_node.childNodes: if (child.nodeType == child.ELEMENT_NODE and child.tagName == "Hsp_query-from"): for n in child.childNodes: if n.nodeType == n.TEXT_NODE: query_from += n.data if (child.nodeType == child.ELEMENT_NODE and child.tagName == "Hsp_query-to"): for n in child.childNodes: if n.nodeType == n.TEXT_NODE: query_to += n.data print query_from print query_to ### This is exactly the kind of code we want to avoid. It works, but it's so fragile and hard to read that I just don't trust it. It just burns my eyes. *grin* By using "helper" functions, we're extending Python's vocabulary of commands. We can then use those functions to help solve our problem with less silliness. This is a reason why knowing how to write and use functions is key to learning how to program: this principle applies regardless of what particular programming language we're using. If you have questions on any of this, please feel free to ask. Good luck! From dyoo at hkn.eecs.berkeley.edu Sun Jan 2 08:39:01 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 2 08:39:08 2005 Subject: [Tutor] O.T. In-Reply-To: <41D3D42C.4080401@mn.rr.com> Message-ID: On Thu, 30 Dec 2004, Anna Ravenscroft wrote: > Anna Martelli Ravenscroft > 42, 2 children (13 and 11) live with their dad > Married this July to the martelli-bot (we read The Zen of Python at our > wedding!). We currently live in Bologna, Italy. Hi Anna, Congratulations! Say hi to Alex for me; it was a pleasure to see him a few days ago in Palo Alto. I'm 25 years old. I don't have any children yet. I'm currently working at the Carnegie Institution of Washington, and appear somewhere on the staff page here: http://carnegiedpb.stanford.edu/dpb/dpb.php *grin* I'm currently planning to run away from work, and attend graduate school soon. But I'll try not to disappear from Python-Tutor; it's been too much fun for me to give it up. From dyoo at hkn.eecs.berkeley.edu Sun Jan 2 08:43:13 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 2 08:43:19 2005 Subject: [Tutor] How to put my functions in an array In-Reply-To: <00bb01c4f046$5a765d20$4b5428cf@JSLAPTOP> Message-ID: On Sat, 1 Jan 2005, Jacob S. wrote: > funct = {'Add Virt':addvirt,'Remove Virt':remvirt,'More > Stuff':more,"Extras":extra} > def addvirt(): > pass > def remvirt(): > pass > def more(): > pass Hi Jacob, Quick gotcha note: the definition of the 'funct' dictionary has to go after the defintion of the hander functions: otherwise, Python has no clue what 'addvirt' and 'remvirt' are. Don't worry: it happens to me too. *grin* Hope this helps! From alan.gauld at freenet.co.uk Sun Jan 2 09:26:09 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 2 09:26:11 2005 Subject: [Tutor] How to substitute an element of a list asapattern forre.compile() References: <20041230010123.91028.qmail@web53709.mail.yahoo.com><00ce01c4f047$2d223fa0$4b5428cf@JSLAPTOP><41D71544.3090903@tds.net> <00e601c4f049$d4df0140$4b5428cf@JSLAPTOP> Message-ID: <003501c4f0a4$b6a7ce90$62bc8651@xp> > > >>> for x in ['string', 10, True, [1,2,3] ]: > > ... print '%s' %x > > ... > > string > > 10 > > True > > [1, 2, 3] > > That's cool! I didn't know that. I guess I'm crazy... : ) > (We already knew that here.) The difference is in how the formatting details work. For numbers the formatting digits indicate precision and for strings length. >>> print "%5.3f" % 123.456 123.4560 >>> print "%5.3s" % 123.456 123. produce very different results. Alan G. From alan.gauld at freenet.co.uk Sun Jan 2 09:30:49 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 2 09:30:45 2005 Subject: [Tutor] doctest References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP><41D75F91.7020907@tds.net> <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> Message-ID: <004001c4f0a5$5dc554e0$62bc8651@xp> > Also, anything I can do... Presently, since I'm running windows xp, I would > have to hunt for the command prompt and type in the command Start->Run Type cmd, Hit OK :-) Or drag the icon from accessories into the start menu or to the desktop. > '"C:\python24\python.exe" "C:\documents and settings\jacob\desktop\working > python programs\testmodules.py" -v' Why not create a shortcut and add that to your right click menu? Or to your desktop if you want - but I hate cluttered desktops! > ...or make a batch file to do it for me... A shortcut will suffice, a batch job is overkill for a one liner Alan G. From kent37 at tds.net Sun Jan 2 15:51:24 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 2 15:51:31 2005 Subject: [Tutor] doctest In-Reply-To: <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP> <41D75F91.7020907@tds.net> <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> Message-ID: <41D80A6C.2070504@tds.net> Jacob S. wrote: > Even so, doctest doesn't seem to recognize the module level docstring. > It will run the test inside the functions, but it says there isn't a test on > the module level. > I put the docstring just like in the example at the link you provided... Please post the code for the module you are testing. > > Also, anything I can do... Presently, since I'm running windows xp, I would > have to hunt for the command prompt and type in the command > > '"C:\python24\python.exe" "C:\documents and settings\jacob\desktop\working > python programs\testmodules.py" -v' You should put C:\python24 in your PATH environment variable. Not sure how to do that on XP; on Win2K I - right-click My Computer and select Properties - click the Advanced tab - click Environment Variables - find the System variable Path and edit it - append ";C:\python24" to Path (no quotes; the semicolon is needed, it is a separator for the Path variable) - click OK a few times - restart your DOS shell so it gets the new variable Now you should be able to open a command line in the directory containing your program and type > python testmodules.py -v Oh, the light goes on...you want to be able to just double-click testmodules.py. See below for the fix for -v > > ...or make a batch file to do it for me... > How can I make testmodules.py (shown below) append the -v to itself? Is > there a self.results or something in testmod? From the docs at http://docs.python.org/lib/doctest-basic-api.html: "Optional argument verbose prints lots of stuff if true" So try doctest.testmod(eval(modtotest), verbose=True) > > ## testmodules.py ########### > import doctest > > modtotest = 'FractionReducer2' > > exec "import %s" % modtotest > doctest.testmod(eval(modtotest)) > raw_input() > ############################# Since you will be editing this to change the module under test, I don't think there is any benefit to putting the module name in a string. I would write it like this: import doctest import FractionReducer2 as test_module doctest.testmod(test_module, verbose=True) raw_input() This preserves the single point of change when you want to test a different module, but it is IMO much more readable. Kent From kent37 at tds.net Sun Jan 2 16:01:01 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 2 16:01:10 2005 Subject: [Tutor] doctest In-Reply-To: <004001c4f0a5$5dc554e0$62bc8651@xp> References: <000801c4f071$cd4e06b0$915328cf@JSLAPTOP><41D75F91.7020907@tds.net> <000b01c4f07a$24c47e30$915328cf@JSLAPTOP> <004001c4f0a5$5dc554e0$62bc8651@xp> Message-ID: <41D80CAD.30905@tds.net> Alan Gauld wrote: >>Also, anything I can do... Presently, since I'm running windows xp, > > I would > >>have to hunt for the command prompt and type in the command > > > Start->Run > Type cmd, > Hit OK > > :-) > > Or drag the icon from accessories into the start menu or to > the desktop. Or drag the icon to the left side of the task bar Or, my favorite, add a 'Command Prompt' item to the right-click menu so you can right-click a directory and open a command prompt at that directory. The recipe is here: http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm Kent From pythontut at pusspaws.net Sun Jan 2 18:14:59 2005 From: pythontut at pusspaws.net (Dave S) Date: Sun Jan 2 18:15:07 2005 Subject: [Tutor] simple list query Message-ID: <41D82C13.7010202@pusspaws.net> OK simple query, I have a list consisting of about 250 items, I need to know if a particular item is in the list. I know this is better suited to a dictionary but thats not the way it ended up ;-) I could do a for loop to scan the list & compare each one, but I have a suspission that there is a better way ? Any suggestions Dave From billk at fastmail.fm Sun Jan 2 18:31:23 2005 From: billk at fastmail.fm (Bill Kranec) Date: Sun Jan 2 18:31:26 2005 Subject: [Tutor] simple list query In-Reply-To: <41D82C13.7010202@pusspaws.net> References: <41D82C13.7010202@pusspaws.net> Message-ID: <41D82FEB.9000005@fastmail.fm> You might want to try: x in list this will return true if, for example, list = [x,y,z,w], false if list = [y,y,y,y] Bill Dave S wrote: > OK simple query, > > I have a list consisting of about 250 items, I need to know if a > particular item is in the list. I know this is better suited to a > dictionary but thats not the way it ended up ;-) > > I could do a for loop to scan the list & compare each one, but I have > a suspission that there is a better way ? > > Any suggestions > > Dave > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From pathall at gmail.com Sun Jan 2 18:34:00 2005 From: pathall at gmail.com (Patrick Hall) Date: Sun Jan 2 18:34:03 2005 Subject: [Tutor] simple list query In-Reply-To: <41D82C13.7010202@pusspaws.net> References: <41D82C13.7010202@pusspaws.net> Message-ID: <6465924d0501020934dd1a5b9@mail.gmail.com> Hi Dave, > I have a list consisting of about 250 items, I need to know if a > particular item is in the list. I know this is better suited to a > dictionary but thats not the way it ended up ;-) > I could do a for loop to scan the list & compare each one, but I have a > suspission that there is a better way ? Indeed there is: just use the built-in "in": >>> li = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'] >>> 'a' in li True >>> 'z' in li False That's a small example, but it will work equally well with a long list. For instance, we can check to see if the words "Ahab", "whale", and "pizza" are in the text of "Moby Dick" (I have the text in a file called "moby.txt".) >>> moby = open('moby.txt').read() # Moby Dick, the whole thing! >>> mobywords = moby.split() # now mobywords has all the words in the text >>> 'Ahab' in mobywords True >>> 'whale' in mobywords True >>> 'pizza' in mobywords False These results are unsurprising. 8^) A list of 250 words is no problem -- "Moby Dick" has a couple hundred thousand: >>> len(mobywords) 214112 I'm not sure I understand why you think a dictionary would be better in this case, a list seems fine to me. Best, Pat From pythontut at pusspaws.net Sun Jan 2 18:53:49 2005 From: pythontut at pusspaws.net (Dave S) Date: Sun Jan 2 18:53:56 2005 Subject: [Tutor] simple list query In-Reply-To: <6465924d0501020934dd1a5b9@mail.gmail.com> References: <41D82C13.7010202@pusspaws.net> <6465924d0501020934dd1a5b9@mail.gmail.com> Message-ID: <41D8352D.9080400@pusspaws.net> Patrick Hall wrote: >Hi Dave, > > > >>I have a list consisting of about 250 items, I need to know if a >>particular item is in the list. I know this is better suited to a >>dictionary but thats not the way it ended up ;-) >> >> > > > >>I could do a for loop to scan the list & compare each one, but I have a >>suspission that there is a better way ? >> >> > >Indeed there is: just use the built-in "in": > > > >>>>li = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'] >>>>'a' in li >>>> >>>> >True > > >>>>'z' in li >>>> >>>> >False > >That's a small example, but it will work equally well with a long >list. For instance, we can check to see if the words "Ahab", "whale", >and "pizza" are in the text of "Moby Dick" (I have the text in a file >called "moby.txt".) > > > >>>>moby = open('moby.txt').read() # Moby Dick, the whole thing! >>>>mobywords = moby.split() # now mobywords has all the words in the text >>>>'Ahab' in mobywords >>>> >>>> >True > > >>>>'whale' in mobywords >>>> >>>> >True > > >>>>'pizza' in mobywords >>>> >>>> >False > >These results are unsurprising. 8^) > >A list of 250 words is no problem -- "Moby Dick" has a couple hundred thousand: > > > >>>>len(mobywords) >>>> >>>> >214112 > >I'm not sure I understand why you think a dictionary would be better >in this case, a list seems fine to me. > >Best, >Pat >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Thanks guys, something was rattling round by brain that there was a way, but I just could not work it out :-) Dave From pythontut at pusspaws.net Sun Jan 2 19:38:54 2005 From: pythontut at pusspaws.net (Dave S) Date: Sun Jan 2 19:39:01 2005 Subject: [Tutor] Am I storeing up problems ? Message-ID: <41D83FBE.3040009@pusspaws.net> Hi there again, My matrix 'self.data' consists of a list of 110 items, each item is a dictionary of 250 keys, each key holds two lists, one of four items, one of 12 items. I needed to copy this matrix to 'self.old_data', so I have been using .deepcopy(), which works OK but is SLOW .... (12+ secs) Once the matrix is copied, 'self.data' is re-constructed as the programme gathers data. To speed things up I changed my code to # This is the speeded up deepcopy() self.old_data = self.data self.data = [] for i in range(110): self.data.append({}) Query: Is all this OK, it works and quick as well, but I am concerned I may be leaving garbage in the Python PVM since I am shrugging off old 'self.old_data's which may be mounting up ? Dave From dyoo at hkn.eecs.berkeley.edu Sun Jan 2 23:00:01 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 2 23:00:11 2005 Subject: [Tutor] Am I storeing up problems ? In-Reply-To: <41D83FBE.3040009@pusspaws.net> Message-ID: On Sun, 2 Jan 2005, Dave S wrote: > My matrix 'self.data' consists of a list of 110 items, each item is a > dictionary of 250 keys, each key holds two lists, one of four items, one > of 12 items. Hi Dave, Hmmm... what kind of data is being copied here? Python's data structures are desigined for flexibility, but sometimes that flexibility comes at the cost of some space. If we knew more about the data that's being stored, maybe we can think of a more compact representation. > I needed to copy this matrix to 'self.old_data', so I have been using > .deepcopy(), which works OK but is SLOW .... (12+ secs) Perhaps we can avoid making a separate copy of the data? Copying data structures can often be avoided. Why does your program try to copy the data structure? (Aside: one nonobvious example where copying can be avoided is in Conway's Game of Life: when we calculate what cells live and die in the next generation, we can actually use the 'Command' design pattern to avoid making a temporary copy of the world. We can talk about this in more detail if anyone is interested.) > Once the matrix is copied, 'self.data' is re-constructed as the > programme gathers data. > > To speed things up I changed my code to > > # This is the speeded up deepcopy() > self.old_data = self.data > self.data = [] > for i in range(110): > self.data.append({}) > > Query: Is all this OK, it works and quick as well, but I am concerned I > may be leaving garbage in the Python PVM since I am shrugging off old > 'self.old_data's which may be mounting up ? Whenever we reassign to self.old_data: self.old_data = self.data then whatever self.old_data was pointing at, before the assignment, should get garbage collected, if there are no other references to the old data. The code there isn't really doing any copying at all, but is rather just constructing a whole new empty data structure into 'self.data'. If so, then it does seem that you can avoid copying. I'd love to know a little bit more about the data that the program is collecting; if you have time, please feel free to tell us more details. Good luck to you! From python at bernardlebel.com Sun Jan 2 23:43:07 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Sun Jan 2 23:43:16 2005 Subject: [Tutor] Basic question -> How to use a class from a file? Message-ID: <41D878FB.8000101@bernardlebel.com> Hello, An easy one to start the year. Trying to write my first class, already running into problems after 3 lines! :-( So have this little class: class rt: def walk(self): print 'yeah' So if I write this in the Python shell, instantiate rt and call walk(), I get the proper result. yeah gets printed. Now let say I put this into a file. I want to import that class and use its methods, but this is were I get lost. import rt rt.walk() Returns: Traceback (most recent call last): File "", line 1, in ? AttributeError: 'module' object has no attribute 'walk' When I try to instantiate the class, I get an non-callable module object error. Sooooo how to use a class from a file? Thanks Bernard From tanja.pislar at gmail.com Sun Jan 2 23:55:04 2005 From: tanja.pislar at gmail.com (tanja pislar) Date: Sun Jan 2 23:55:09 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: <41D878FB.8000101@bernardlebel.com> References: <41D878FB.8000101@bernardlebel.com> Message-ID: hi Bernard, you have to specify the module as well: let's say your module is called rtModule.py, then in your case you'd do: import rtModule testClass = rtModule.rt() testClass.walk() or: from rtModule import rt testClass = rt() testClass.walk() regards, tanja On Sun, 02 Jan 2005 17:43:07 -0500, Bernard Lebel wrote: > Hello, > > An easy one to start the year. > > Trying to write my first class, already running into problems after 3 > lines! :-( > > So have this little class: > > class rt: > def walk(self): > print 'yeah' > > So if I write this in the Python shell, instantiate rt and call walk(), > I get the proper result. yeah gets printed. > > Now let say I put this into a file. I want to import that class and use > its methods, but this is were I get lost. > > import rt > rt.walk() > > Returns: > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'module' object has no attribute 'walk' > > When I try to instantiate the class, I get an non-callable module object > error. > > Sooooo how to use a class from a file? > > Thanks > Bernard > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- www.klaustrofobik.org From python at bernardlebel.com Mon Jan 3 00:01:34 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Mon Jan 3 00:01:43 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: References: <41D878FB.8000101@bernardlebel.com> Message-ID: <41D87D4E.8000206@bernardlebel.com> All righty, thanks a lot! Bernard tanja pislar wrote: > hi Bernard, > > you have to specify the module as well: > > let's say your module is called rtModule.py, > then in your case you'd do: > > import rtModule > > testClass = rtModule.rt() > testClass.walk() > > or: > from rtModule import rt > > testClass = rt() > testClass.walk() > > regards, > tanja > > > On Sun, 02 Jan 2005 17:43:07 -0500, Bernard Lebel > wrote: > >>Hello, >> >>An easy one to start the year. >> >>Trying to write my first class, already running into problems after 3 >>lines! :-( >> >>So have this little class: >> >>class rt: >> def walk(self): >> print 'yeah' >> >>So if I write this in the Python shell, instantiate rt and call walk(), >>I get the proper result. yeah gets printed. >> >>Now let say I put this into a file. I want to import that class and use >>its methods, but this is were I get lost. >> >>import rt >>rt.walk() >> >>Returns: >>Traceback (most recent call last): >> File "", line 1, in ? >>AttributeError: 'module' object has no attribute 'walk' >> >>When I try to instantiate the class, I get an non-callable module object >>error. >> >>Sooooo how to use a class from a file? >> >>Thanks >>Bernard From patric at usa.net Mon Jan 3 00:09:55 2005 From: patric at usa.net (Patric Michael) Date: Mon Jan 3 00:08:00 2005 Subject: [Tutor] A not-so-basic question... Message-ID: <41D80EC3.4463.E2CFAE3@localhost> Hi folks... I was thinking about this the other day while prepping to write up another CGI thing. It occurred to me to wonder how other folks prepare an app, script, whatever. So the question is, and I am not looking for a "right answer" here. (I doubt ther eis one, to be honest.) How do you go about setting up a new application? For example, suppose I need a script that will collect order information for a set of items ona page. Its output will go to a mail program so the artist can be notified. I know that the script needs to be somewhat customizable since more than one person will use it, and I want to look to the future where such a script might get superceded for something like credit card orders, or possible integration with a larger order processing scheme. I'd start out by commenting goals in a new file, like so: ------------------------------ # Initialize variables # Gather form data # Output results -------------------------------------- Then I'd go back through each comment and either call modules I've prewritten, or write new defs. Then I'd add import statements to cover the modules and the defs: ---------------------------------- from datetime import now import cgi from mailman import send # Initialize variables form = cgi.FieldStorage() # Gather form data for each in form: blah blah # Output results result = {} send(result) ---------------------------------------- And so on. So the question is, how do you do it? Is there a method you prefer? Something tried and true, or maybe foolproof? Just curious... And maybe a bit hopeful someone has a better way than mine. Seems like I do an awful lot of testing... :) Patric From maxnoel_fr at yahoo.fr Mon Jan 3 01:43:35 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 3 01:43:50 2005 Subject: [Tutor] Am I storeing up problems ? In-Reply-To: References: Message-ID: <8029EB8E-5D20-11D9-BD2B-000393CBC88E@yahoo.fr> On Jan 2, 2005, at 23:00, Danny Yoo wrote: > (Aside: one nonobvious example where copying can be avoided is in > Conway's > Game of Life: when we calculate what cells live and die in the next > generation, we can actually use the 'Command' design pattern to avoid > making a temporary copy of the world. We can talk about this in more > detail if anyone is interested.) I am. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From alan.gauld at freenet.co.uk Mon Jan 3 01:50:06 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 01:49:55 2005 Subject: [Tutor] simple list query References: <41D82C13.7010202@pusspaws.net> Message-ID: <007801c4f12e$2de8aef0$62bc8651@xp> > I have a list consisting of about 250 items, I need to know if a > particular item is in the list. I know this is better suited to a > dictionary but thats not the way it ended up ;-) > > I could do a for loop to scan the list & compare each one, but I have a > suspission that there is a better way ? In principle you are right but due to the way Python works you might be faster doing a sort on the list first, then you only need to search until you pass the place where the entry should be... This will be faster if you have to search for several items, and might be faster even for one since the sort is written in C but the for loop will be in Python... Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Mon Jan 3 01:51:41 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 01:51:21 2005 Subject: [Tutor] simple list query References: <41D82C13.7010202@pusspaws.net> <41D82FEB.9000005@fastmail.fm> Message-ID: <007f01c4f12e$6421c1a0$62bc8651@xp> > You might want to try: > > x in list Blush, I forgot about 'in'. And being in C it should be faster than the sort/loop combination. Silly me... :-( Alan G. From alan.gauld at freenet.co.uk Mon Jan 3 01:53:42 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 01:53:26 2005 Subject: [Tutor] Am I storeing up problems ? References: <41D83FBE.3040009@pusspaws.net> Message-ID: <008401c4f12e$abe2be90$62bc8651@xp> > I needed to copy this matrix to 'self.old_data', so I have been using > .deepcopy(), which works OK but is SLOW .... (12+ secs) Are you sure you need to copy it./ A simple reassignment should work and then reconstruct the structure using fresh lists/dictionary etc. That should work faster. Alan G. From lists at movingimagearts.com Mon Jan 3 02:15:20 2005 From: lists at movingimagearts.com (Rafal Kaniewski) Date: Mon Jan 3 02:15:39 2005 Subject: [Tutor] My 2nd ever .py script [XML, makedirs, copytree] In-Reply-To: <41D80EC3.4463.E2CFAE3@localhost> Message-ID: <006501c4f131$b1bf43d0$0301a8c0@MIALID> This is my second script (if you don't include the last time I programmed (20 years ago...) it's a nasty hack but it works... ------------------------------------------------------------------- - - 0 Workflow01.fp7 - yourSoLuckyJob c:\BaldricsWorkFolder FRENCH - yourSoLuckyJob c:\BaldricsWorkFolder MT - yourSoLuckyJob c:\BaldricsWorkFolder SPANISH ------------------------------------------------------------------- from xml.sax import make_parser from xml.sax.handler import ContentHandler import os import shutil class FolderHandler(ContentHandler): def __init__ (self, searchTerm): self.searchTerm= searchTerm; self.isA_5Element, self.ishomePathElement, self.isA_1Element, self.isProjectElement, self.isA_4Element, self.isA_0Element = 0, 0, 0, 0, 0, 0; def startElement(self, name, attrs): if name == 'ROW': self.MODID = attrs.get('MODID',"") self.RECORDID = attrs.get('RECORDID',"") elif name == 'pce.project.g': self.isProjectElement= 1; self.PROJECT = ""; elif name == 'pce.homePath.g': self.ishomePathElement= 1; self.homePath = ""; elif name == 'pea.elementA_0': self.isA_0Element = 1; self.A_0 = ""; def characters (self, ch): if self.isProjectElement == 1: self.PROJECT += ch if self.ishomePathElement== 1: self.homePath += ch if self.isA_0Element == 1: self.A_0 += ch class DirectoryException(Exception): pass FilmScansFolders = self.homePath + '\\' + self.PROJECT + '\\' + 'FILM' + '\\' + 'SCANS' + '\\' + self.A_0 FilmTemplFolders = self.homePath + '\\' + self.PROJECT + '\\' + 'TEMPLATES' dirpath = os.path.join(FilmScansFolders) try: os.makedirs(dirpath) except OSError, e: if e.errno == 17: if not os.path.isdir(dirpath): raise DirectoryException, "'%s' can neither be created nor is it an existing directory" % dirpath else: raise # cannot handle it here try: shutil.copytree('C:\Program Files\Python\TEMPLATES', FilmTemplFolders) print '' except os.error, why: print '' def endElement(self, name): if name == 'pce.project.g': self.isProjectElement= 0 if name == 'pce.homePath.g': self.ishomePathElement= 0 if name == 'pea.elementA_0': self.isA_0Element = 0 searchTerm= '*' parser = make_parser() curHandler = FolderHandler(searchTerm) parser.setContentHandler(curHandler) parser.parse(open('FM_folders.xml')) ------------------------------------------------------------------- The aim (create folders on a network from a database with xml & Python): 1) extract data from xml file (generated by Filemaker database) 2) create complex folder structure (each row of the xml is set of folders, all rows reside in the same home 'project folder' 3) copy a folder into the 'project folder' 4)the python file is called/executed by Filemaker (multi-user) I am hoping people can feedback on areas that this script that clearly needs tidying up. Thanks all, - Rafal Kaniewski EU:UK From python at bernardlebel.com Mon Jan 3 03:35:32 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Mon Jan 3 03:35:40 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: References: <41D878FB.8000101@bernardlebel.com> Message-ID: <41D8AF74.6070706@bernardlebel.com> Okay here comes the next question. In that class, I have 3 functions. Actually, one of them calls the two others. However when I call these functions (wich are placed before the caller in the file), I get an error saying that the global name x (the function name) is not defined. What am I doing wrong? Thanks Bernard From rmkrauter at yahoo.com Mon Jan 3 04:45:23 2005 From: rmkrauter at yahoo.com (Rich Krauter) Date: Mon Jan 3 04:45:21 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: <41D8AF74.6070706@bernardlebel.com> References: <41D878FB.8000101@bernardlebel.com> <41D8AF74.6070706@bernardlebel.com> Message-ID: <41D8BFD3.3010904@yahoo.com> Bernard Lebel wrote: > Okay here comes the next question. > > In that class, I have 3 functions. Actually, one of them calls the two > others. > > However when I call these functions (wich are placed before the caller > in the file), I get an error saying that the global name x (the function > name) is not defined. > > What am I doing wrong? > > Hi Bernard, This is just a guess since you didn't post your code, but did you call the 'bare' methods in your class definition, rather than calling the methods through an instance (which is usually called 'self' inside class definitions)? I think the following may be similar to what you wanted to do: class A(object): def __init__(self,data=None): self.data = data def methodA(self): self.methodB() self.methodC() def methodB(self): print "in methodB" print self.data def methodC(self): print "in methodC" print self.data if __name__ == '__main__': a1 = A(5) a1.methodA() a2 = A([1,2,3]) a2.methodA() Hope this helps. Rich From neal at bcn.boulder.co.us Mon Jan 3 05:57:08 2005 From: neal at bcn.boulder.co.us (Neal McBurnett) Date: Mon Jan 3 05:57:39 2005 Subject: [Tutor] dumping .pck files Message-ID: <200501030457.j034v8We019029@lock.gotdns.org> I want to explore some mailman config files, e.g. config.pck. Some quick searches informed me that these contain pickled configuration info. My first naive attempt to dump what was in them failed: >>> import pickle >>> pickle.load(open("config.pck")) traceback.... ImportError: No module named Mailman.Bouncer It seems that to do a good job of dumping the data, I need to tell it what this class looks like. Are there alternatives? Does the pickle format really not provide a way to inspect the data without the class definitions? E.g. if I didn't have the source code or didn't want to dig around in it? Is there a simple way to dump everything it does understand, and just print stubs for the parts it doesn't? Or is there some nice software to do this all for me? Or perhaps does mailman itself have handy command-line tools to aid in comparing configurations across mailing lists? Are there other serialization options that would make this easier? Many thanks, Neal McBurnett http://bcn.boulder.co.us/~neal/ Signed and/or sealed mail encouraged. GPG/PGP Keyid: 2C9EBA60 From patric at usa.net Mon Jan 3 06:50:30 2005 From: patric at usa.net (Patric Michael) Date: Mon Jan 3 06:48:34 2005 Subject: [Tutor] dumping .pck files In-Reply-To: <200501030457.j034v8We019029@lock.gotdns.org> Message-ID: <41D86CA6.21475.F9BC473@localhost> > I want to explore some mailman config files, e.g. > config.pck. Some quick searches informed me that these > contain pickled configuration info. > > My first naive attempt to dump what was in them failed: > > >>> import pickle > >>> pickle.load(open("config.pck")) > traceback.... > ImportError: No module named Mailman.Bouncer > > It seems that to do a good job of dumping the data, I need to tell it > what this class looks like. > > Are there alternatives? Does the pickle format really not provide a > way to inspect the data without the class definitions? E.g. if I > didn't have the source code or didn't want to dig around in it? > > Is there a simple way to dump everything it does understand, and > just print stubs for the parts it doesn't? > > Or is there some nice software to do this all for me? > > Or perhaps does mailman itself have handy command-line tools to aid in > comparing configurations across mailing lists? In the bin directory, find the script called "config_list". From the help file (./config_list --help): --outputfile filename -o filename Instead of configuring the list, print out a list's configuration variables in a format suitable for input using this script. In this way, you can easily capture the configuration settings for a particular list and imprint those settings on another list. filename is the file to output the settings to. If filename is `-', standard out is used. Dont forget to supply the list name in the command line. For example: config_list -o outfile.txt mylist Patric > > Are there other serialization options that would make this easier? > > Many thanks, > > Neal McBurnett http://bcn.boulder.co.us/~neal/ > Signed and/or sealed mail encouraged. GPG/PGP Keyid: 2C9EBA60 > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From billburns at pennswoods.net Mon Jan 3 07:47:34 2005 From: billburns at pennswoods.net (Bill Burns) Date: Mon Jan 3 07:37:53 2005 Subject: [Tutor] Sorting/filtering data, dictionary question & Re:OT Message-ID: <200501030147.34968.billburns@pennswoods.net> Hi, I have a couple of questions about a program I've been working on. Here's a overview of what the program does. Calculates the following data about HVAC/Plumbing pipe: A). Total volume of water inside the pipe (US gallons). B). The weight of the water inside the pipe. C). The actual weight of the pipe, itself. There's a total of six different types of pipe to choose from and within that group of six, there's a total of 100 different sizes. Sizes range from 1/2" to 48". The user enters pipe lengths into a GUI and then clicks on a pushButton to write the data out to a csv file. A dialog then pops up and gives the user the option to open the csv file (with OpenOffice Calc). Using suggestions I received from the Tutor List, I created a dictionary for the program that holds various data about the pipes. The dict holds pipe sizes, inside diameters, names of the lineEdits on the GUI, weight of the pipe per foot, etc. So, the user enters various pipe lengths, and using the dictionary the program calculates the totals, the totals go into a list, the list gets sorted and then the sorted list is written to csv file. One problem I ran into was sorting my lists. The dictionary I initially came up with contained a pipe size designation in this format: 1/2", 3/4", 1", etc. This format is the standard way to write pipe sizes (at least in the US anyway). When I sorted the data (using this format), I found that the inches didn't sort the way I wanted them to. For example: >>>sizes = ['1/2"', '3/4"', '1"', '1-1/4"', '1-1/2"'] >>>sizes.sort() >>>sizes ['1"', '1-1/2"', '1-1/4"', '1/2"', '3/4"'] >>> Although Python is sorting exactly as it is supposed to, I needed the inches to be in sequential order. Not knowing what to do, I added another size to the dictionary before each of the "inch sizes". This new "size" has this type of format: .5, .75, 1, 1.25, 1.5, etc. And when the data is sorted, it works perfectly. But now I was left with both a "size" and an "inch size" and I only wanted the "inch size" in the pipe reports (the csv file). Below is a small section of the dictionary the program uses (pipeDict). I'm also providing two functions to give you a general idea of how I'm: 1). Creating and sorting the pipe data and 2). How I'm filtering the "size" out of the list. pipeDict = \ {('lineEdit1','Steel(Std)',.5,'1/2"',.85): .622, ('lineEdit2','Steel(Std)',.75,'3/4"',1.13): .824, ('lineEdit3','Steel(Std)',1,'1"',1.678): 1.049, ('lineEdit4','Steel(Std)',1.25,'1-1/4"',2.272): 1.38, ('lineEdit5','Steel(Std)',1.5,'1-1/2"',2.717): 1.61, ('lineEdit6','Steel(Std)',2,'2"',3.652): 2.067, ('lineEdit7','Steel(Std)',2.5,'2-1/2"',5.79): 2.469, ('lineEdit8','Steel(Std)',3,'3"',7.57): 3.068, ('lineEdit9','Steel(Std)',3.5,'3-1/2"',9.11): 3.548, ('lineEdit10','Steel(Std)',4,'4"',10.79): 4.026, ('lineEdit11','Steel(Std)',5,'5"',14.62): 5.047, ('lineEdit12','Steel(Std)',6,'6"',18.97): 6.065, ('lineEdit13','Steel(Std)',8,'8"',28.55): 7.981, ('lineEdit14','Steel(Std)',10,'10"',40.48): 10.02} def somePipeReport(): report = [] for name, typ, size, inchSize, weight in pipeDict: report.append((typ,size,inchSize,weight)) report.sort() newReport = filterPipeData(report) print newReport def filterPipeData(data): filteredData = [] for typ, size, inchSize, weight in data: filteredData.append((typ,inchSize,weight)) return filteredData The two functions above are not *exactly* what I'm using, but it's close enough to give you an idea of how I'm removing the "size" data. The reason I created a separate function to filter the "size" out is because I currently have two reports within the program. I figured it would be better to create one function that each report could call to filter data. I also thought that if I create any additional reports in the future, those reports could utilize the same function as well. Question #1: Am I going about this sorting and filtering thing correctly or have I just gone insane? My gut feeling is, there's probably an easier/smarter way to do this. Question #2: As I mentioned above, this program calculates the water volume inside of the pipe. I do that using this function: def volCalc(ID, length): from math import pi gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) return gal The ID (inside diameter) is pulled from pipeDict (it's the value in the dictionary) and the length comes from user input. What I'm wondering is, would it be a better idea to store in the dictionary a "gallon per foot value" for each pipe? For example, looking at 10" Steel(Std) pipe in the above dictionary we find that this type & size of pipe has an ID of 10.02 (inches). When we plug this ID and a length of 1 foot into the volCalc() function it returns a total of 4.10 gallons (rounded). Would it be better to store this "gallon per foot value" (4.10) in the dictionary (as calculated for each pipe) vs. calculating the gallons the way I'm currently doing it? I guess the real question is, which method will return the most accurate results? I'm not looking for a speed improvement. Any suggestions are appriecated. Thanks, Bill P.S. Regarding Jacob's OT post: I'm 34 years old, married 10 yrs w/three kids (boy-8yrs old (soon to be 9), girl-7yrs old & boy-3yrs old). As you may have gathered from the domain name "Penn's Woods" I'm from PA . And If you didn't figure it out from my post above, I'm in the HVAC industry. I'm not a programmer at all. I just stumbled across Python one day and thought, wow, this is cool, I'm gonna try it out! I've been *slowly* learning ever since. From bvande at po-box.mcgill.ca Mon Jan 3 08:27:21 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Mon Jan 3 08:28:48 2005 Subject: [Tutor] Am I storeing up problems ? In-Reply-To: <8029EB8E-5D20-11D9-BD2B-000393CBC88E@yahoo.fr> References: <8029EB8E-5D20-11D9-BD2B-000393CBC88E@yahoo.fr> Message-ID: <41D8F3D9.4020100@po-box.mcgill.ca> Max Noel said unto the world upon 2005-01-02 19:43: > > On Jan 2, 2005, at 23:00, Danny Yoo wrote: > >> (Aside: one nonobvious example where copying can be avoided is in >> Conway's >> Game of Life: when we calculate what cells live and die in the next >> generation, we can actually use the 'Command' design pattern to avoid >> making a temporary copy of the world. We can talk about this in more >> detail if anyone is interested.) > > > I am. > > -- Max Seconded. (Thanks for offering, Danny.) Best to all, Brian vdB From dyoo at hkn.eecs.berkeley.edu Mon Jan 3 10:11:49 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 3 10:11:57 2005 Subject: [Tutor] The Game of Life In-Reply-To: <41D8F3D9.4020100@po-box.mcgill.ca> Message-ID: On Mon, 3 Jan 2005, Brian van den Broek wrote: > >> (Aside: one nonobvious example where copying can be avoided is in > >> Conway's Game of Life: when we calculate what cells live and die in > >> the next generation, we can actually use the 'Command' design pattern > >> to avoid making a temporary copy of the world. We can talk about > >> this in more detail if anyone is interested.) > > > Seconded. (Thanks for offering, Danny.) [Long post up ahead.] Just as a warning, none of what I'm going to code here is original at all: I'm rehashing a main idea off of a paper called "Using the Game of Life to Introduce Freshman Students to the Power and Elegance of Design Patterns": http://portal.acm.org/citation.cfm?id=1035292.1028706 Just for reference, the Game of Life is described in Wikipedia here: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Ok, let's start hacking. The Game of Life involves a 2-D matrix of cells, where any cell can be 'LIVE' or 'DEAD'. ### LIVE, DEAD = '*', '.' ### And just to make this quick-and-dirty, let's simulate this matrix with a dictionary whose keys are 2-tuples (x, y), and whose values are either LIVE or DEAD. To reduce the complexity of the example, I'll even avoid using classes or data abstraction. *grin* Here's our setup: ### def make_random_world(M, N): """Constructs a new random game world of size MxN.""" world = {} for j in range(N): for i in range(M): world[i, j] = random.choice([LIVE, DEAD]) world['dimensions'] = (M, N) return world def print_world(world): """Prints out a string representation of a world.""" M, N = world['dimensions'] for j in range(N): for i in range(M): print world[i, j], print ### (If I had more space, I'd rather do this as a real data structure instead of hacking up a dictionary.) For example: ### >>> print_world(make_random_world(10, 10)) . * * * . . * * . * . . * * . . * . * . . * * . . * . * * * * * . * * * * * . * . . * . * * * * . . * * . . . * * . * * * . * . . * * * . . * . * * * . . * . . * * . . . * * . . . . * . . * . . * * * >>> >>> >>> small_world = make_random_world(4, 4) >>> print_world(small_world) * . . * * * . . . * * * . . . . ### For the example ahead, I'll use the small world. Ok, this looks good so far. So now we have something that shows a single generation of a game world. How does the world run? Well, between each generation, each cell can either live or die according to the rule: A dead cell with exactly 3 live neighbors becomes alive (or is "born"). A live cell with 2 or 3 live neighbors stays alive; otherwise it dies (from "loneliness"). For example, when we look back at the small world: ### * . . * * * . . . * * * . . . . ### The cell at the upper left corner (0, 0) has 2 neighbors, so it'll survive. The cell at the upper right corner (3, 0) has no neighbors, so it'll die. The cell at the bottom left (0, 3) has one neighbor, so it stays dead, but the cell near the bottom right (2, 3) has three neighbors, so it'll spring to life. Let's code these up. ### def get_state(world, i, j): """Returns the state of the cell at position (i, j). If we're out of bounds, just returns DEAD to make calculations easier.""" return world.get((i, j), DEAD) def count_live_neighbors(world, i, j): """Returns the number of live neighbors to this one.""" live_count = 0 for i_delta in [-1, 0, 1]: for j_delta in [-1, 0, 1]: if (i_delta, j_delta) == (0, 0): continue if get_state(world, i+i_delta, j+j_delta) == LIVE: live_count += 1 return live_count def is_born(world, i, j): """Returns True if the cell at (i, j) is currently dead, but will be born in the next generation.""" return (get_state(world, i, j) == DEAD and count_live_neighbors(world, i ,j) == 3) def is_deceased(world, i, j): """Returns True if the cell at (i, j) is currently alive, but will die in the next generation.""" return (get_state(world, i, j) == LIVE and not (2 <= count_live_neighbors(world, i ,j) <= 3)) ### And again, let's make sure these rules work by just spot checking them: ### >>> is_deceased(small_world, 0, 0) False >>> is_deceased(small_world, 3, 0) True >>> is_born(small_world, 0, 3) False >>> is_born(small_world, 2, 3) True ### Ok, at least it's not blatently buggy. *grin* Now the problem is: given the current state of the world, how do we calculate the next state of the world? One way is to make a temporary copy of the world, and apply changes to it, consulting the original world for the rules: ### def apply_next_generation(world): """Destructively mutate the world so it reflect the next generation.""" M, N = world['dimensions'] new_world = copy.copy(world) for j in range(N): for i in range(M): if is_born(world, i, j): new_world[i, j] = LIVE if is_deceased(world, i, j): new_world[i, j] = DEAD for j in range(N): for i in range(M): world[i, j] = new_world[i, j] ### And this does work: ### >>> print_world(small_world) * . . * * * . . . * * * . . . . >>> apply_next_generation(small_world) >>> print_world(small_world) * * . . * . . * * * * . . . * . ### The unsatisfying thing, though, is that we use an explicit copying step and the temporary scratch space. But we can actually get away from making temporary scratch space by using this cute idea: instead of directly applying status changes immediately, we store a list of "Commands" that we can execute after we figure out who lives and who dies: ### def make_rise_command(world, i, j): """Produces a callable function that, when called, will apply a change to the world to make the cell at (i, j) live.""" def cmd(): world[i, j] = LIVE return cmd def make_die_command(world, i, j): """Produces a callable function that, when called, will apply a change to the world to make the cell at (i, j) die.""" def cmd(): world[i, j] = DEAD return cmd ### Let's see how this works on our small world: ### >>> print_world(small_world) * . . * * * . . . * * * . . . . >>> some_cmd = make_die_command(small_world, 0, 0) >>> some_cmd >>> print_world(small_world) * . . * * * . . . * * * . . . . >>> some_cmd() >>> print_world(small_world) . . . * * * . . . * * * . . . . ### Notice that the change to our world does not take place until we execute the some_cmd command. This is, conceptually, an example of a "Command": it's a callable that applies a state change when we finally call it. Stripped of all the OOP/Class baggage, that's pretty much all it is. Punchline time! ### def apply_next_generation_revised(world): """Destructively mutate the world so it reflect the next generation.""" M, N = world['dimensions'] commands = [] for j in range(N): for i in range(M): if is_born(world, i, j): commands.append(make_rise_command(world, i, j)) if is_deceased(world, i, j): commands.append(make_die_command(world, i, j)) for cmd in commands: cmd() ### Here is sample output from the program: ### >>> print_world(small_world) * . . * * * . . . * * * . . . . >>> apply_next_generation_revised(small_world) >>> print_world(small_world) * * . . * . . * * * * . . . * . >>> apply_next_generation_revised(small_world) >>> print_world(small_world) * * . . . . . . * . * * . . * . ### I know I rushed the heck out of this program. *grin* If you have any questions on it, please feel free to ask. Talk to you later! From alan.gauld at freenet.co.uk Mon Jan 3 10:43:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 10:42:42 2005 Subject: [Tutor] A not-so-basic question... References: <41D80EC3.4463.E2CFAE3@localhost> Message-ID: <009f01c4f178$9f8fd6b0$62bc8651@xp> Patric, > How do you go about setting up a new application? Thats a great question, and I hope we get a few responses. > For example, suppose I need a script that will collect order information > for a set of items ona page. Its output will go to a mail program so the > artist can be notified. I would start by abstracting the problem as much as possible and identifying the classes/objects (I think in OOP by default for anything non trivial) In this case I see an order and and order item (or possibly several) Because I try to separate UI from application as much as possible I'd start with these and build the two objects in a module and test them using the >>> prompt. Order depends on order items so I'd build item first. Then I'd test it - can I create an item? Can I print an item? Because this is targetted at the web I might right a dedicated str/repr method to grenerate an XML representation... Then I'd build the orderand test it. Can I add items to it? etc. Then I'd think about how to extend the order items. I don't want to use subclassing since that means custom code for every new product we sell, better to use a dictionary of attributes I think... Lets try that out, back to item again... OK, It all works from the >>> prompt lets think about the UI. (Here I confess that I do very little web programming) I need to - present the items for selection/browsing - offer the opportunity to create an order(add to basket) - present an order summary - capture customer details - allow confirmation of the order - process the order This points out that I also need a customer object! Back to the application construction..... OK I now have a customer object, back to the UI. I'll build the UI in generic terms using the bullets above as hook functions - so I can change behaviour easily later. Thus calling Order.process(customer) will initially generate an email, but later I may subclass order to provide a direct link to the suppliers via a B2B gateway or Web Service... I'll have to think about the order process at this point and join the bits up in the right order. Back to the >>> prompt for some experimenting. Now I design the HTML and set up my web server environment. I write my CGI handling parts to call the application objects as required. It all works, but nothing is persisted. Build a persistence mechanism, how many orders? Maybe store the data in XML files somewhere for a few orders? Or go for growth and build a full database and write persistence methods to match. I think I'm ready for accepance testing now. In terms of modules, I'll probably put the order, item, and customer in separate modules. I'll put the UI stuff in a single module. The database bits will probably go along with the objects themselves. If its for production use rather than a prototype I'd document the class structure, the order processing sequence and the database schema and persistence mechanism. I'd also document the file structure on the web server. The rest of the documentation I'd leave as comments and docstrings since its a very small application. In total I'd expect a design document of 5-7 pages or so. Alan G. From alan.gauld at freenet.co.uk Mon Jan 3 10:49:56 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 10:49:39 2005 Subject: [Tutor] Basic question -> How to use a class from a file? References: <41D878FB.8000101@bernardlebel.com> <41D8AF74.6070706@bernardlebel.com> Message-ID: <00ae01c4f179$95396540$62bc8651@xp> > Okay here comes the next question. > > In that class, I have 3 functions. Actually, one of them calls the two > others. You have 3 methods, functions and methods are similar but different! If you think of them asthe same thing you will probably get confused by OOP later on! > However when I call these functions (wich are placed before the caller > in the file), I get an error saying that the global name x (the function > name) is not defined. WE could do with some code to show how you do that, and definitely cut n paste the error report. Python error reports are very helpful in tracing issues. > What am I doing wrong? Without code or error report I can only make a wild guess. Are you remembering to precede the method name with the object name? ie from myModule import MyClass # get access to the class myobject = MyCLass() # create an object from the class myobject.myMethod() # send message to the object to call myMethod Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Mon Jan 3 10:53:53 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 10:53:33 2005 Subject: [Tutor] dumping .pck files References: <200501030457.j034v8We019029@lock.gotdns.org> Message-ID: <00b501c4f17a$24e925e0$62bc8651@xp> > It seems that to do a good job of dumping the data, I need to tell it > what this class looks like. > > Are there alternatives? Does the pickle format really not provide a > way to inspect the data without the class definitions? I suspect that loading the file into a good text editror like vim or emacs will be more helpful. But pickle files are not self describing like XML etc. This is because they are designed to be compact and XML typically multiplies the raw data size by 10! Pickle files are not intended to be read by humans merely a way for a program to make objects persistent between runs. Alan G. From pythontut at pusspaws.net Mon Jan 3 11:04:28 2005 From: pythontut at pusspaws.net (Dave S) Date: Mon Jan 3 11:04:38 2005 Subject: [Tutor] Am I storeing up problems ? In-Reply-To: References: Message-ID: <41D918AC.30803@pusspaws.net> Danny Yoo wrote: >On Sun, 2 Jan 2005, Dave S wrote: > > > >>My matrix 'self.data' consists of a list of 110 items, each item is a >>dictionary of 250 keys, each key holds two lists, one of four items, one >>of 12 items. >> >> > >Hi Dave, > >Hmmm... what kind of data is being copied here? Python's data structures >are desigined for flexibility, but sometimes that flexibility comes at the >cost of some space. If we knew more about the data that's being stored, >maybe we can think of a more compact representation. > > > OK my app is tracking the ftse 250, the 110 items are time slots through a trading day, the 250 keys are ftse company names added dynamicly , the two lists are (4) a series of linked cookies to transmit info forward per company, and (12) financial stats for the particular company at a particular time. Companies in the 250 change randomly and there are not always exactly 250 (realy!), a linked cookie system is needed for a variety of info going forward not least as a count of how long any particular company has been on the ftse 250. The copy is needed so two days ftse figures can be viewed at any one time. Thurther apps can pull out ftse figures for a particular company going backwards .. you get the idea ! It all works & its been a great learning experience to code. >>I needed to copy this matrix to 'self.old_data', so I have been using >>.deepcopy(), which works OK but is SLOW .... (12+ secs) >> >> > >Perhaps we can avoid making a separate copy of the data? Copying data >structures can often be avoided. Why does your program try to copy the >data structure? > >(Aside: one nonobvious example where copying can be avoided is in Conway's >Game of Life: when we calculate what cells live and die in the next >generation, we can actually use the 'Command' design pattern to avoid >making a temporary copy of the world. We can talk about this in more >detail if anyone is interested.) > > > > > >>Once the matrix is copied, 'self.data' is re-constructed as the >>programme gathers data. >> >>To speed things up I changed my code to >> >># This is the speeded up deepcopy() >>self.old_data = self.data >>self.data = [] >>for i in range(110): >> self.data.append({}) >> >> Query: Is all this OK, it works and quick as well, but I am concerned I >>may be leaving garbage in the Python PVM since I am shrugging off old >>'self.old_data's which may be mounting up ? >> >> > > >Whenever we reassign to self.old_data: > > self.old_data = self.data > >then whatever self.old_data was pointing at, before the assignment, should >get garbage collected, if there are no other references to the old data. > >The code there isn't really doing any copying at all, but is rather just >constructing a whole new empty data structure into 'self.data'. If so, >then it does seem that you can avoid copying. > >I'd love to know a little bit more about the data that the program is >collecting; if you have time, please feel free to tell us more details. >Good luck to you! > > > > > From pythontut at pusspaws.net Mon Jan 3 11:07:49 2005 From: pythontut at pusspaws.net (Dave S) Date: Mon Jan 3 11:07:56 2005 Subject: [Tutor] Am I storeing up problems ? In-Reply-To: <008401c4f12e$abe2be90$62bc8651@xp> References: <41D83FBE.3040009@pusspaws.net> <008401c4f12e$abe2be90$62bc8651@xp> Message-ID: <41D91975.6040905@pusspaws.net> Alan Gauld wrote: >>I needed to copy this matrix to 'self.old_data', so I have been >> >> >using > > >>.deepcopy(), which works OK but is SLOW .... (12+ secs) >> >> > >Are you sure you need to copy it./ A simple reassignment should work >and then reconstruct the structure using fresh lists/dictionary etc. >That should work faster. > >Alan G. > > > Yep this is what I ended up doing, 12+ secs becomes 0.5 sec :-) My query was To speed things up I changed my code to # This is the speeded up deepcopy() self.old_data = self.data self.data = [] for i in range(110): self.data.append({}) Query: Is all this OK, it works and quick as well, but I am concerned I may be leaving garbage in the Python PVM since I am shrugging off old 'self.old_data's which may be mounting up ? But apparently this is ok (as he cancelles order gor 1GB RAM ;-) ) Dave From flamesrock at gmail.com Mon Jan 3 11:33:30 2005 From: flamesrock at gmail.com (Aaron Elbaz) Date: Mon Jan 3 11:33:36 2005 Subject: [Tutor] Extracting a PNG Image File from a Binary File.. Message-ID: <2c2812b605010302334b0b8b5b@mail.gmail.com> Hi, My question is sort of on the difficult side, but I promise I'm a newb ;) So maybe it isn't.. Frederick Lundh himself gave me this chunk of code..and I can't get it to work. I was hoping someone could spot the error. The goal is to extract a png image file from a binary simcity 4 file. #import struct # #def pngcopy(infile, outfile): # # # copy header # header = infile.read(8) # if header != "\211PNG\r\n\032\n": # raise IOError("not a valid PNG file") # outfile.write(header) # # # copy chunks, until IEND # while 1: # chunk = infile.read(8) # size, cid = struct.unpack("!l4s", chunk) # outfile.write(chunk) # outfile.write(infile.read(size)) # outfile.write(infile.read(4)) # checksum # if cid == "IEND": # break # #infile = open("/home/flamesrock/Desktop/peachville.sc4", "rb") #infile.seek(0x50) #outfile = open("myimage.png", "wb") #pngcopy(infile, outfile) #outfile.close() #infile.close() It returns the IO error when I try it on this and other files: http://simcitysphere.com/peachville.sc4 I've confirmed the position using a hex editor (its the first 'P' or 0x50 in the file) http://simcitysphere.com/peachville.png The idea comes from this php code which works -if only I could understand how to translate it. The code may or may not also extract data which denotes a relative position on an image map: also available at http://simcitysphere.com/regionFileDecode_inc.php.txt >2)+3; $offset=(($packcode>>5)<<8)+$a+1; mmemcpy($outbuf,$outpos,$outbuf,$outpos-$offset,$lenb); $outpos+=$lenb; //decho ("Code $packcode len plain: $lena len: $lenb offset: $offset outpos: $outpos"); } else if (!($packcode&0x40)) { //decho('match 0x40'); $lena=($a>>6)&3; mmemcpy($outbuf,$outpos,$inbuf,$inpos+3,$lena); $inpos+=$lena+3; $outpos+=$lena; $lenb=($packcode&0x3f)+4; $offset=($a&0x3f)*256+$b+1; mmemcpy($outbuf,$outpos,$outbuf,$outpos-$offset,$lenb); $outpos+=$lenb; //decho ("Code $packcode len plain: $lena len: $lenb offset: $offset outpos: $outpos"); } else if (!($packcode&0x20)) { //decho('match 0x20'); $c=unpack1($inbuf[$inpos+3],'C'); $lena=$packcode&3; mmemcpy($outbuf,$outpos,$inbuf,$inpos+4,$lena); $inpos+=$lena+4; $outpos+=$lena; $lenb=(($packcode>>2)&3)*256+$c+5; $offset=(($packcode&0x10)<<12)+256*$a+$b+1; mmemcpy($outbuf,$outpos,$outbuf,$outpos-$offset,$lenb); $outpos+=$lenb; //decho ("Code $packcode len plain: $lena len: $lenb offset: $offset outpos: $outpos"); } else { //decho('match 0x1f'); $len=($packcode&0x1f)*4+4; mmemcpy($outbuf,$outpos,$inbuf,$inpos+1,$len); $inpos+=$len+1; $outpos+=$len; //decho ("Code $packcode Plain Chars: $len outpos: $outpos"); //Code 224 Plain Chars: 4 outpos: 0 } } /* trailing bytes if (($inpos<$inlen)&&($outpos<$outlen)) { mmemcpy($outbuf,$outpos,$inbuf,$inpos+1,unpack1($inbuf[$inpos],'C')&3); $outpos+=unpack1($inbuf[$inpos],'C')&3; } */ /* if ($outpos!=$outlen) decho("Warning: bad length ? {$outpos} instead of {$outlen} with {$Cname}"); */ $buflen=$outlen; return $outbuf; } function mmemcpy(&$dest, $destpos, &$src, $srcpos, $len) { while ($len--) { //decho ("destpos: $destpos | srcpos: {$src[$srcpos]}"); $dest[$destpos] = $src[$srcpos]; $destpos++; $srcpos++; } } function unpack1($bytes, $type=false) { if($type) { $dat = unpack("{$type}int",$bytes); return $dat['int']; } else { $dat = unpack('Vint',$bytes); return $dat['int']; } } ?> ANY replies are welcome. Even if you don't know the answer, something to point me in the right direction would be greatly appreciated. I'm completely stuck. -thanks From kent37 at tds.net Mon Jan 3 12:03:28 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 3 12:03:38 2005 Subject: [Tutor] Sorting/filtering data, dictionary question & Re:OT In-Reply-To: <200501030147.34968.billburns@pennswoods.net> References: <200501030147.34968.billburns@pennswoods.net> Message-ID: <41D92680.6060509@tds.net> See comments inline... Bill Burns wrote: > One problem I ran into was sorting my lists. The dictionary I initially came > up with contained a pipe size designation in this format: 1/2", 3/4", 1", etc. > This format is the standard way to write pipe sizes (at least in the US > anyway). When I sorted the data (using this format), I found that the inches > didn't sort the way I wanted them to. > For example: > >>>sizes = ['1/2"', '3/4"', '1"', '1-1/4"', '1-1/2"'] > >>>sizes.sort() > >>>sizes > ['1"', '1-1/2"', '1-1/4"', '1/2"', '3/4"'] > >>> > Although Python is sorting exactly as it is supposed to, I needed the inches > to be in sequential order. Not knowing what to do, I added another size to the > dictionary before each of the "inch sizes". This new "size" has this type of > format: .5, .75, 1, 1.25, 1.5, etc. And when the data is sorted, it works > perfectly. But now I was left with both a "size" and an "inch size" and I only > wanted the "inch size" in the pipe reports (the csv file). > > Below is a small section of the dictionary the program uses (pipeDict). I'm > also providing two functions to give you a general idea of how I'm: > 1). Creating and sorting the pipe data and > 2). How I'm filtering the "size" out of the list. > > > pipeDict = \ > {('lineEdit1','Steel(Std)',.5,'1/2"',.85): .622, > ('lineEdit2','Steel(Std)',.75,'3/4"',1.13): .824, > > def somePipeReport(): > report = [] > for name, typ, size, inchSize, weight in pipeDict: > report.append((typ,size,inchSize,weight)) > report.sort() > newReport = filterPipeData(report) > print newReport > > def filterPipeData(data): > filteredData = [] > for typ, size, inchSize, weight in data: > filteredData.append((typ,inchSize,weight)) > return filteredData > > > Question #1: > Am I going about this sorting and filtering thing correctly or have I just > gone insane? My gut feeling is, there's probably an easier/smarter way to do > this. This is actually a common approach to sorting a list in Python - add enough fields to the list so it sorts the way you want, then filter out the fields you don't need any more. It even has a name, it's called Decorate - Sort - Undecorate. In your case the 'decorate' step is built-in to the dictionary, you are just doing the sort and undecorate. There is a shorter way to write filterPipeData() using a list comprehension, which is just a shortcut for what you wrote: def filterPipeData(data): return [ (typ,inchSize,weight) for typ, size, inchSize, weight in data ] When you get used to it, this form is more readable; it will also be faster though I don't think you will notice. > > Question #2: > As I mentioned above, this program calculates the water volume inside of the > pipe. I do that using this function: > > def volCalc(ID, length): > from math import pi > gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) > return gal > > The ID (inside diameter) is pulled from pipeDict (it's the value in the > dictionary) and the length comes from user input. What I'm wondering is, > would it be a better idea to store in the dictionary a "gallon per foot value" > for each pipe? For example, looking at 10" Steel(Std) pipe in the above > dictionary we find that this type & size of pipe has an ID of 10.02 (inches). > When we plug this ID and a length of 1 foot into the volCalc() function it > returns a total of 4.10 gallons (rounded). Would it be better to store this > "gallon per foot value" (4.10) in the dictionary (as calculated for each pipe) > vs. calculating the gallons the way I'm currently doing it? I guess the real > question is, which method will return the most accurate results? I'm not > looking for a speed improvement. If you calculate gallons per foot from your current ID values and put those numbers in the dict, the new numbers will not be any more accurate than your current calculations. If you have another source for gal/foot that you think is more accurate then you could use those numbers. But I think what you have is probably good enough and if it is working there is no need to change it. Kent From alan.gauld at freenet.co.uk Mon Jan 3 12:23:04 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 12:22:41 2005 Subject: [Tutor] Extracting a PNG Image File from a Binary File.. References: <2c2812b605010302334b0b8b5b@mail.gmail.com> Message-ID: <00ff01c4f186$9852a040$62bc8651@xp> > # if header != "\211PNG\r\n\032\n": > # raise IOError("not a valid PNG file") > # outfile.write(header) > > It returns the IO error when I try it on this and other files: > http://simcitysphere.com/peachville.sc4 If you are sure the file is OK then it looks like the signature definition test must be wrong. Check the file header against Freds string and modify the strong to match. ( You could of course try commenting out the test and see if the main code works as expected... backing up the data first of course!) HTH Alan G. From kent37 at tds.net Mon Jan 3 12:38:20 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 3 12:38:27 2005 Subject: [Tutor] The Game of Life In-Reply-To: References: Message-ID: <41D92EAC.1060905@tds.net> Danny Yoo wrote: >>>>(Aside: one nonobvious example where copying can be avoided is in >>>>Conway's Game of Life: when we calculate what cells live and die in >>>>the next generation, we can actually use the 'Command' design pattern >>>>to avoid making a temporary copy of the world. We can talk about >>>>this in more detail if anyone is interested.) > > Just as a warning, none of what I'm going to code here is original at all: > I'm rehashing a main idea off of a paper called "Using the Game of Life to > Introduce Freshman Students to the Power and Elegance of Design Patterns": > > http://portal.acm.org/citation.cfm?id=1035292.1028706 Uh, with all respect Danny, this is a nice example of the Command pattern, but to me this example would better be called "Using the Power and Elegance of Design Patterns to Complicate the Game of Life". > Now the problem is: given the current state of the world, how do we > calculate the next state of the world? One way is to make a temporary > copy of the world, and apply changes to it, consulting the original > world for the rules: > > ### > def apply_next_generation(world): > """Destructively mutate the world so it reflect the next > generation.""" > M, N = world['dimensions'] > new_world = copy.copy(world) > for j in range(N): > for i in range(M): > if is_born(world, i, j): > new_world[i, j] = LIVE > if is_deceased(world, i, j): > new_world[i, j] = DEAD > for j in range(N): > for i in range(M): > world[i, j] = new_world[i, j] > ### Why not just build a new world with the values of the next generation in it, and return that from apply_next_generation? The copying is not needed at all and the command pattern is introduced to solve a non-problem. Something like this: def apply_next_generation(world): """Destructively mutate the world so it reflect the next generation.""" new_world = {} new_world['dimensions'] = world['dimensions'] M, N = world['dimensions'] for j in range(N): for i in range(M): if is_born(world, i, j): new_world[i, j] = LIVE if is_deceased(world, i, j): new_world[i, j] = DEAD return new_world The caller would have to change slightly to accept the new world returned from the function, but there is no unnecessary copying. This is very similar to Dave's situation that kicked of the thread, where he was using a copy instead of a simple assignment and regeneration. If you want to avoid creating a new world each time you could have two worlds and bounce back and forth between them. Kent From kent37 at tds.net Mon Jan 3 13:15:39 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 3 13:15:47 2005 Subject: [Tutor] Extracting a PNG Image File from a Binary File.. In-Reply-To: <2c2812b605010302334b0b8b5b@mail.gmail.com> References: <2c2812b605010302334b0b8b5b@mail.gmail.com> Message-ID: <41D9376B.4020803@tds.net> Looking at your data, the header is at offset 0x60, not 0x50. If I use infile.seek(0x60) it works fine. Kent Aaron Elbaz wrote: > Hi, > > My question is sort of on the difficult side, but I promise I'm a newb > ;) So maybe it isn't.. > > > Frederick Lundh himself gave me this chunk of code..and I can't get it > to work. I was hoping someone could spot the error. > > The goal is to extract a png image file from a binary simcity 4 file. > #import struct > # > #def pngcopy(infile, outfile): > # > # # copy header > # header = infile.read(8) > # if header != "\211PNG\r\n\032\n": > # raise IOError("not a valid PNG file") > # outfile.write(header) > # > # # copy chunks, until IEND > # while 1: > # chunk = infile.read(8) > # size, cid = struct.unpack("!l4s", chunk) > # outfile.write(chunk) > # outfile.write(infile.read(size)) > # outfile.write(infile.read(4)) # checksum > # if cid == "IEND": > # break > # > #infile = open("/home/flamesrock/Desktop/peachville.sc4", "rb") > #infile.seek(0x50) > #outfile = open("myimage.png", "wb") > #pngcopy(infile, outfile) > #outfile.close() > #infile.close() > > It returns the IO error when I try it on this and other files: > http://simcitysphere.com/peachville.sc4 > > I've confirmed the position using a hex editor (its the first 'P' or > 0x50 in the file) > http://simcitysphere.com/peachville.png > From kent37 at tds.net Mon Jan 3 13:37:58 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 3 13:38:05 2005 Subject: [Tutor] A not-so-basic question... In-Reply-To: <41D80EC3.4463.E2CFAE3@localhost> References: <41D80EC3.4463.E2CFAE3@localhost> Message-ID: <41D93CA6.4010406@tds.net> Patric, I am a strong proponent of - incremental development - unit testing and test-first (or at least test-concurrent) programming - Don't Repeat Yourself and merciless refactoring I look for a small piece of a problem, or a simplification of the problem, and write some code. I write unit tests for the code as I go. When I have the first bit working, I add some more. I refactor to improve the clarity of the code and avoid duplication. ================ Look for a way to automate your tests. If you are writing a CGI and testing it in place with a web browser, the testing will quickly become tedious and you will rarely test more than the last code that you wrote. If your tests are automated and quick, you can run many tests at once. That way you have confidence that you haven't broken anything. In your example, automated testing may be a challenge. If I understand correctly, you are basically getting data from a form and putting it into an email. Some suggestions: - If there is any manipulation of the data in the middle, you can break that into functions and test those functions separately. - You can abstract the mailer into a generic back end. Then you can make a test version that doesn't actually send any mail, but that lets you verify that the program asked for mail to be sent. Then use an automated tester for web apps to drive the front end and make sure the back end is called correctly. - This thread on comp.lang.python about unit testing CGIs may be helpful: http://tinyurl.com/5yr4c It can take some work to set up a test jig but I find it usually pays off. Once you have done it once you will have the techniques down and you can use the same techniques in similar projects. ================ Look for duplication in your code and don't tolerate it. Not only in a single project but between projects. If you are writing many similar CGIs you should be building up a library of tested support modules that you can re-use. This will make subsequent projects easier. Whenever you are tempted to copy and paste code, look for a way to abstract the code into a function or class so the copying is not needed. Whenever you are tempted to copy and paste data, look for a way to centralize the data so all clients work from the same reference. HTH, Kent Patric Michael wrote: > Hi folks... > > I was thinking about this the other day while prepping to write up > another CGI thing. > It occurred to me to wonder how other folks prepare an app, script, > whatever. > So the question is, and I am not looking for a "right answer" here. (I > doubt ther eis one, to be honest.) > > How do you go about setting up a new application? > > For example, suppose I need a script that will collect order information > for a set of items ona page. Its output will go to a mail program so the > artist can be notified. > I know that the script needs to be somewhat customizable since more > than one person will use it, and I want to look to the future where such a > script might get superceded for something like credit card orders, or > possible integration with a larger order processing scheme. > > I'd start out by commenting goals in a new file, like so: > > ------------------------------ > # Initialize variables > > # Gather form data > > # Output results > -------------------------------------- > Then I'd go back through each comment and either call modules I've > prewritten, or write new defs. > Then I'd add import statements to cover the modules and the defs: > > ---------------------------------- > from datetime import now > import cgi > from mailman import send > > # Initialize variables > form = cgi.FieldStorage() > > # Gather form data > for each in form: > blah > blah > > # Output results > result = {} > send(result) > > ---------------------------------------- > > And so on. > > So the question is, how do you do it? > > Is there a method you prefer? Something tried and true, or maybe > foolproof? > > Just curious... And maybe a bit hopeful someone has a better way than > mine. Seems like I do an awful lot of testing... :) > > Patric > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From op73418 at mail.telepac.pt Mon Jan 3 13:44:07 2005 From: op73418 at mail.telepac.pt (=?ISO-8859-1?Q?Gon=E7alo_Rodrigues?=) Date: Mon Jan 3 13:40:38 2005 Subject: [Tutor] simple list query In-Reply-To: <6465924d0501020934dd1a5b9@mail.gmail.com> References: <41D82C13.7010202@pusspaws.net> <6465924d0501020934dd1a5b9@mail.gmail.com> Message-ID: <41D93E17.4070103@mail.telepac.pt> Patrick Hall wrote: > Hi Dave, > > >>I have a list consisting of about 250 items, I need to know if a >>particular item is in the list. I know this is better suited to a >>dictionary but thats not the way it ended up ;-) > > >>I could do a for loop to scan the list & compare each one, but I have a >>suspission that there is a better way ? > > > Indeed there is: just use the built-in "in": > > [text snipped] > > A list of 250 words is no problem -- "Moby Dick" has a couple hundred thousand: > > >>>>len(mobywords) > > 214112 > > I'm not sure I understand why you think a dictionary would be better > in this case, a list seems fine to me. > The statement my_object in my_list does under the hood a for-loop and sequentially compares every object in my_list with my_object, so, potentially it does len(my_list) comparisons (it's O(n) in computerese speak). With a dictionary, that is my_object in my_dict it is O(1), that is, it is (asymptotically) constant and independent of the number of items in the dictionary. So if you are going to do a lot of in tests, it does pay to put your objects in a dictionary. With my best regards, G. Rodrigues From python at bernardlebel.com Mon Jan 3 14:24:11 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Mon Jan 3 14:24:20 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: <00ae01c4f179$95396540$62bc8651@xp> References: <41D878FB.8000101@bernardlebel.com> <41D8AF74.6070706@bernardlebel.com> <00ae01c4f179$95396540$62bc8651@xp> Message-ID: <41D9477B.7000409@bernardlebel.com> Alan Gauld wrote: > You have 3 methods, functions and methods are similar but different! > If you think of them asthe same thing you will probably get > confused by OOP later on! [Bernard] I'm already confused! Seriously, I never done oop before, so even the Python tutorial examples are extremely confusing to me atm. Anyway I got it sorted. The two additional functions didn't needed to be methods, they were just 'helpers' called by the class method. So I have put them outside the class and now it works. >>What am I doing wrong? > > > Without code or error report I can only make a wild guess. [Bernard] Well yeah I understand that. But since most of the code I write is very application specific I'm not sure how relevant it can be to post it, this is why I generally try to simplify my problems as much as possible. Next time I'll try to cook a more generic code :-) Bernard From bgibson at us.ibm.com Mon Jan 3 14:35:41 2005 From: bgibson at us.ibm.com (Bob Gibson) Date: Mon Jan 3 14:35:46 2005 Subject: [Tutor] Re: Sorting/filtering data, dictionary question In-Reply-To: <20050103094940.ECA091E4019@bag.python.org> Message-ID: Bill: Could you have the "LineEdit#" be your sortable field? If each # after the "LineEdit" prefix were the same length, then you could easily sort on it. Bob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050103/aba1849b/attachment-0001.html From davholla2002 at yahoo.co.uk Mon Jan 3 16:25:10 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Mon Jan 3 16:25:13 2005 Subject: [Tutor] CGI help Message-ID: <20050103152510.28218.qmail@web25401.mail.ukl.yahoo.com> I have written my first cgi script :- The .html code is :- Friends CGI Demo (Static Screen)

Friends list for:New User

Enter your name>

How many friends do you have ? 0 10 25 50 100

The python code is :- #!/usr/bin/env python import cgi reshtml = '''Content-Type: text/html\n Friends CGI Demo (dynamic screen)

Friends list for: %s

Your name is: %s

You have %s friends. ''' form = cgi.FieldStorage() who = form['person'].value howmany = form['howmany'].value print reshtml % (who, who, howmany) Now when I open the page using konqueror and click on submit. Instead of the code running, it justs opens the .py program in a different window. Any ideas ? ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From kent37 at tds.net Mon Jan 3 17:09:38 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 3 17:09:47 2005 Subject: [Tutor] CGI help In-Reply-To: <20050103152510.28218.qmail@web25401.mail.ukl.yahoo.com> References: <20050103152510.28218.qmail@web25401.mail.ukl.yahoo.com> Message-ID: <41D96E42.7000709@tds.net> It sounds like maybe you are opening the HTML directly from the filesystem? You have to install the HTML and CGI into a web server and access them through the server. The details for this will depend on the web server. Kent David Holland wrote: > I have written my first cgi script :- > The .html code is :- > > Friends CGI Demo (Static Screen) > >

Friends list for:New User

>
ACTION="/home/david/Documents/pyprogramming/friends1.py"> > Enter your name> > >

How many friends do you have ? > 0 > 10 > 25 > 50 > 100 >

> The python code is :- > #!/usr/bin/env python > import cgi > reshtml = '''Content-Type: text/html\n > > Friends CGI Demo (dynamic screen) > >

Friends list for: %s

> Your name is: %s

> You have %s friends. > ''' > form = cgi.FieldStorage() > who = form['person'].value > howmany = form['howmany'].value > print reshtml % (who, who, howmany) > > > Now when I open the page using konqueror and click on > submit. Instead of the code running, it justs opens > the .py program in a different window. > Any ideas ? > > > > > > ___________________________________________________________ > ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From rmkrauter at yahoo.com Mon Jan 3 17:31:59 2005 From: rmkrauter at yahoo.com (Rich Krauter) Date: Mon Jan 3 17:31:57 2005 Subject: [Tutor] CGI help In-Reply-To: <20050103152510.28218.qmail@web25401.mail.ukl.yahoo.com> References: <20050103152510.28218.qmail@web25401.mail.ukl.yahoo.com> Message-ID: <41D9737F.9070303@yahoo.com> David Holland wrote: > I have written my first cgi script :- > The .html code is :- > > Friends CGI Demo (Static Screen) > >

Friends list for:New User

>
ACTION="/home/david/Documents/pyprogramming/friends1.py"> > Enter your name> > >

How many friends do you have ? > 0 > 10 > 25 > 50 > 100 >

> The python code is :- > #!/usr/bin/env python > import cgi > reshtml = '''Content-Type: text/html\n > > Friends CGI Demo (dynamic screen) > >

Friends list for: %s

> Your name is: %s

> You have %s friends. > ''' > form = cgi.FieldStorage() > who = form['person'].value > howmany = form['howmany'].value > print reshtml % (who, who, howmany) > > > Now when I open the page using konqueror and click on > submit. Instead of the code running, it justs opens > the .py program in a different window. > Any ideas ? > > Hi David, You can use a small python script to provide a cgi web server for testing: import BaseHTTPServer import CGIHTTPServer def run(server_class=BaseHTTPServer.HTTPServer, handler_class=CGIHTTPServer.CGIHTTPRequestHandler): server_address = ('', 8000) httpd = server_class(server_address, handler_class) httpd.serve_forever() run() Put this in a file called, say, server.py. In the same directory as server.py, put a directory called cgi-bin. Put your python cgi scripts in cgi-bin. Modify the action tag in your html form to point to /cgi-bin/friends1.py. Click on the server.py icon, or run it from the command line. Finally, point your browser to http://127.0.0.1:8000/cgi-bin/friends1.py. You'll proably want to use apache or some other server if you want to do more than just test or demo cgi scripts locally, but this can be a useful alternative for testing simple cgi programs without installing additional software on your machine. Good luck. Rich From alan.gauld at freenet.co.uk Mon Jan 3 20:23:46 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 3 20:23:20 2005 Subject: [Tutor] Basic question -> How to use a class from a file? References: <41D878FB.8000101@bernardlebel.com><41D8AF74.6070706@bernardlebel.com><00ae01c4f179$95396540$62bc8651@xp> <41D9477B.7000409@bernardlebel.com> Message-ID: <013801c4f1c9$bf78c4e0$62bc8651@xp> > I'm already confused! Seriously, I never done oop before, so even the > Python tutorial examples are extremely confusing to me atm. Which tutor are you using? If its the standard Python tutor then its not really for beginners to OOP, you might be better looking at some of the more basic tutors such as Josh C's or the Think like a CS, or mine. They all teach OOP at a more leisurely pace. For general info on OOP try the cetus links pages: http://www.cetus-liks.org Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From godoy at ieee.org Mon Jan 3 20:39:18 2005 From: godoy at ieee.org (Jorge Luiz Godoy Filho) Date: Mon Jan 3 20:40:39 2005 Subject: [Tutor] Re: Basic question -> How to use a class from a file? References: <41D878FB.8000101@bernardlebel.com><41D8AF74.6070706@bernardlebel.com><00ae01c4f179$95396540$62bc8651@xp> <41D9477B.7000409@bernardlebel.com> <013801c4f1c9$bf78c4e0$62bc8651@xp> Message-ID: <1123847.4YUDY9CaDM@strongwill.g2ctech> Alan Gauld, Segunda 03 Janeiro 2005 17:23, wrote: > http://www.cetus-liks.org Just a correction on the URL: http://www.cetus-links.org/ -- Godoy. From kilovh at gmail.com Mon Jan 3 23:17:36 2005 From: kilovh at gmail.com (kilovh) Date: Mon Jan 3 23:16:50 2005 Subject: [Tutor] Breaking down integers? Message-ID: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> I would like to be able to take an integer, break it down into individual items in a list, and then put them back together. I know how to do this last part thanks to Orri Ganel and Guillermo Fernandex, but what about taking the integer apart? Sorry if the questions have incredibly obvious answers, I'm new to this. ~kilovh -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050103/1f8cd96e/attachment.html From singingxduck at gmail.com Mon Jan 3 23:33:20 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Mon Jan 3 23:33:25 2005 Subject: [Tutor] Breaking down integers? In-Reply-To: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> References: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> Message-ID: <3449428f050103143337ada755@mail.gmail.com> On Mon, 3 Jan 2005 17:17:36 -0500, kilovh wrote: > > I would like to be able to take an integer, break it down into individual > items in a list, and then put them back together. I know how to do this last > part thanks to Orri Ganel and Guillermo Fernandex, but what about taking the > integer apart? It sounds like you're looking for something like this: ' '.join(str(integer)).split() First, you stringify the integer. Then, you separate each digit by a space (' '.join(...)). Finally, you split the result into a list (split()'s default separator is a space, which is why you separate the digits with spaces). So, for the integer 6942: >>> num = 6942 >>> numlist = ' '.join(str(num)).split() >>> numlist ['6', '9', '4', '2'] And then, if you want the digits to be integers, >>> numlist = [int(item) for item in numlist] >>> numlist [6, 9, 4, 2] This uses list comprehension. It is the equivalent of typing: for item in numlist: numlist[numlist.index(item)] = int(item) or for i in range(len(numlist)): numlist[i] = int(numlist[i]) The reason the latter two methods assign on a per - entry basis, while the first assigns the entire list, is because using a list comprehension returns a list itself (notice the list-like notation). You are assigning numlist to a list whose entries are defined as int(entry) for each entry already in numlist. One of the reasons this works is because Python first evaluates the right side of the equals sign, then assigns it. HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From bvande at po-box.mcgill.ca Mon Jan 3 23:41:49 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Mon Jan 3 23:43:46 2005 Subject: [Tutor] Breaking down integers? In-Reply-To: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> References: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> Message-ID: <41D9CA2D.7070307@po-box.mcgill.ca> kilovh said unto the world upon 2005-01-03 17:17: > I would like to be able to take an integer, break it down into > individual items in a list, and then put them back together. I know > how to do this last part thanks to Orri Ganel and Guillermo > Fernandex, but what about taking the integer apart? > > Sorry if the questions have incredibly obvious answers, I'm new to > this. > > ~kilovh > Hi, I'm not sure I've the intent of your question, but do you mean something like this: .>>> an_int = 42 .>>> an_int_as_string = str(an_int) .>>> print an_int_as_string 42 .>>> type(an_int_as_string) .>>> an_int_as_string_list = list(an_int_as_string) .>>> print an_int_as_string_list ['4', '2'] .>>> rebuilt_int_string = ''.join(an_int_as_string_list) .>>> print rebuilt_int_string 42 .>>> type(rebuilt_int_string) .>>> rebuilt_int = int(rebuilt_int_string) .>>> print rebuilt_int 42 .>>> type(rebuilt_int) .>>> Does that help? Best to all, Brian vdB From singingxduck at gmail.com Mon Jan 3 23:47:36 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Mon Jan 3 23:47:40 2005 Subject: [Tutor] Breaking down integers? In-Reply-To: <41D9CA2D.7070307@po-box.mcgill.ca> References: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> <41D9CA2D.7070307@po-box.mcgill.ca> Message-ID: <3449428f05010314471239f96c@mail.gmail.com> On Mon, 03 Jan 2005 17:41:49 -0500, Brian van den Broek wrote: > .>>> an_int_as_string_list = list(an_int_as_string) > .>>> print an_int_as_string_list > ['4', '2'] *Smacks head with palm* Of course, you're right . . . no need to go into ' '.join(str(x)).split() . . . Python's batteries sure are hidden in plain sight ::grin:: -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From michael at trollope.org Mon Jan 3 23:54:52 2005 From: michael at trollope.org (Michael Powe) Date: Mon Jan 3 23:55:02 2005 Subject: [Tutor] here documents Message-ID: <20050103225451.GA20627@titan.spiretech.com> Hello, In perl, I create variables of fairly involved text using here documents. For example, $msg = <<"EOF"; a bunch of text here. ... EOF Is there an equivalent method in python? I usually use this method when creating help messages for scripts -- put all the text into a variable and the 'print $msg' for the output. I find it an easy way to produce formatted text. Now, I'm trying to switch over to python and want to recreate or adapt my processes. Thanks. mp From bill at celestial.net Tue Jan 4 00:26:16 2005 From: bill at celestial.net (Bill Campbell) Date: Tue Jan 4 00:26:21 2005 Subject: [Tutor] here documents In-Reply-To: <20050103225451.GA20627@titan.spiretech.com> References: <20050103225451.GA20627@titan.spiretech.com> Message-ID: <20050103232616.GA84554@alexis.mi.celestial.com> On Mon, Jan 03, 2005, Michael Powe wrote: >Hello, > >In perl, I create variables of fairly involved text using here >documents. For example, > >$msg = <<"EOF"; > a bunch of text here. > ... >EOF > >Is there an equivalent method in python? I usually use this method >when creating help messages for scripts -- put all the text into a >variable and the 'print $msg' for the output. I find it an easy way >to produce formatted text. I think that triple quoting is probably what you want. msg = ''' A bunch of text here, perhaps with %s insertion ''' % 'variable' You will have to use the ``printf'' type features of the ``%'' operator in places where one could include variable names within perl's double quotes or ``HERE'' types. Bill -- INTERNET: bill@Celestial.COM Bill Campbell; Celestial Software LLC UUCP: camco!bill PO Box 820; 6641 E. Mercer Way FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 URL: http://www.celestial.com/ DOS: n., A small annoying boot virus that causes random spontaneous system crashes, usually just before saving a massive project. Easily cured by UNIX. See also MS-DOS, IBM-DOS, DR-DOS. From python at bernardlebel.com Tue Jan 4 00:43:27 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Tue Jan 4 00:43:42 2005 Subject: [Tutor] Basic question -> How to use a class from a file? In-Reply-To: <013801c4f1c9$bf78c4e0$62bc8651@xp> References: <41D878FB.8000101@bernardlebel.com><41D8AF74.6070706@bernardlebel.com><00ae01c4f179$95396540$62bc8651@xp> <41D9477B.7000409@bernardlebel.com> <013801c4f1c9$bf78c4e0$62bc8651@xp> Message-ID: <41D9D89F.5070409@bernardlebel.com> Alan Gauld wrote: > Which tutor are you using? If its the standard Python tutor then its > not really for beginners to OOP, Yes, I was talking about the one that comes with Python. Now I understand why I couldn't figure much of it! > you might be better looking at some > of the more basic tutors such as Josh C's or the Think like a CS, > or mine. > > They all teach OOP at a more leisurely pace. > > For general info on OOP try the cetus links pages: > > http://www.cetus-liks.org > > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld Thanks a lot for these ressources, I'll check them out. Bernard From alan.gauld at freenet.co.uk Tue Jan 4 00:54:06 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 4 00:53:38 2005 Subject: [Tutor] here documents References: <20050103225451.GA20627@titan.spiretech.com> Message-ID: <019001c4f1ef$833d58d0$62bc8651@xp> There was a detailed thread on this recently either here or on usenet group comp.lang.python... The bottom line was to use string formatting and triple quoted strings... msg = ''' A very long string that overspills onto multiple lines and includes my name which is %{name}s and a number which is my age: %{age}d ''' print msg % vars() HTH, Alan G. ----- Original Message ----- From: "Michael Powe" To: Sent: Monday, January 03, 2005 10:54 PM Subject: [Tutor] here documents > Hello, > > In perl, I create variables of fairly involved text using here > documents. For example, > > $msg = <<"EOF"; > a bunch of text here. > ... > EOF > > Is there an equivalent method in python? I usually use this method > when creating help messages for scripts -- put all the text into a > variable and the 'print $msg' for the output. I find it an easy way > to produce formatted text. > > Now, I'm trying to switch over to python and want to recreate or adapt > my processes. > > Thanks. > > mp > > From alan.gauld at freenet.co.uk Tue Jan 4 00:56:16 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 4 00:55:49 2005 Subject: [Tutor] here documents Message-ID: <019501c4f1ef$d0ffa870$62bc8651@xp> Oops, those should have been () not {} > msg = ''' > A very long string that overspills > onto multiple lines and includes > my name which is %(name)s > and a number which is my age: %(age)d > ''' > > print msg % vars() Sorry folks, I really should try these things *before* I hit the send button... Alan G. ----- Original Message ----- From: "Alan Gauld" To: "Michael Powe" ; Sent: Monday, January 03, 2005 11:54 PM Subject: Re: [Tutor] here documents > There was a detailed thread on this recently either here > or on usenet group comp.lang.python... > > The bottom line was to use string formatting and triple > quoted strings... > > msg = ''' > A very long string that overspills > onto multiple lines and includes > my name which is %{name}s > and a number which is my age: %{age}d > ''' > > print msg % vars() > > HTH, > > Alan G. > > ----- Original Message ----- > From: "Michael Powe" > To: > Sent: Monday, January 03, 2005 10:54 PM > Subject: [Tutor] here documents > > > > Hello, > > > > In perl, I create variables of fairly involved text using here > > documents. For example, > > > > $msg = <<"EOF"; > > a bunch of text here. > > ... > > EOF > > > > Is there an equivalent method in python? I usually use this method > > when creating help messages for scripts -- put all the text into a > > variable and the 'print $msg' for the output. I find it an easy way > > to produce formatted text. > > > > Now, I'm trying to switch over to python and want to recreate or > adapt > > my processes. > > > > Thanks. > > > > mp > > > > > From godoy at ieee.org Tue Jan 4 01:04:18 2005 From: godoy at ieee.org (Jorge Luiz Godoy Filho) Date: Tue Jan 4 01:05:31 2005 Subject: [Tutor] Re: here documents References: <019501c4f1ef$d0ffa870$62bc8651@xp> Message-ID: <26950783.oEXIodb7kx@strongwill.g2ctech> Alan Gauld, Segunda 03 Janeiro 2005 21:56, wrote: > Oops, those should have been () not {} I always do the same mistake ;-) Using "{}" seems more intuitive to me. -- Godoy. From michael at trollope.org Tue Jan 4 01:10:50 2005 From: michael at trollope.org (Michael Powe) Date: Tue Jan 4 01:10:55 2005 Subject: [Tutor] here documents In-Reply-To: <019001c4f1ef$833d58d0$62bc8651@xp> References: <20050103225451.GA20627@titan.spiretech.com> <019001c4f1ef$833d58d0$62bc8651@xp> Message-ID: <20050104001050.GB20627@titan.spiretech.com> On Mon, Jan 03, 2005 at 11:54:06PM -0000, Alan Gauld wrote: > There was a detailed thread on this recently either here > or on usenet group comp.lang.python... I checked the archives for this list but didn't see anything. I'll try the ng. Thanks. > The bottom line was to use string formatting and triple > quoted strings... > > msg = ''' > A very long string that overspills > onto multiple lines and includes > my name which is %{name}s > and a number which is my age: %{age}d > ''' > > print msg % vars() This is great, thanks to you and to Bill. Much appreciated. mp From michael at trollope.org Tue Jan 4 01:15:14 2005 From: michael at trollope.org (Michael Powe) Date: Tue Jan 4 01:15:17 2005 Subject: [Tutor] Re: here documents In-Reply-To: <26950783.oEXIodb7kx@strongwill.g2ctech> References: <019501c4f1ef$d0ffa870$62bc8651@xp> <26950783.oEXIodb7kx@strongwill.g2ctech> Message-ID: <20050104001514.GC20627@titan.spiretech.com> On Mon, Jan 03, 2005 at 10:04:18PM -0200, Jorge Luiz Godoy Filho wrote: > Alan Gauld, Segunda 03 Janeiro 2005 21:56, wrote: > > > Oops, those should have been () not {} > > I always do the same mistake ;-) Using "{}" seems more intuitive to me. perhaps because of ${var} shell syntax? ;-) mp From johnp at milwaukielumber.com Tue Jan 4 01:55:07 2005 From: johnp at milwaukielumber.com (John Purser) Date: Tue Jan 4 01:55:11 2005 Subject: [Tutor] here documents In-Reply-To: <20050103225451.GA20627@titan.spiretech.com> Message-ID: <200501040055.j040t7nP009137@mail.morseintranet.com> I'd like to thank everyone who posted on this thread. I was reading a Korn shell manual the other day and could not figure out what a "here" document was. I'm going to take another run at it with this conversation in mind! Sometimes I can't see the path until I know where it goes. John Purser -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Michael Powe Sent: Monday, January 03, 2005 14:55 To: tutor@python.org Subject: [Tutor] here documents Hello, In perl, I create variables of fairly involved text using here documents. For example, $msg = <<"EOF"; a bunch of text here. ... EOF Is there an equivalent method in python? I usually use this method when creating help messages for scripts -- put all the text into a variable and the 'print $msg' for the output. I find it an easy way to produce formatted text. Now, I'm trying to switch over to python and want to recreate or adapt my processes. Thanks. mp _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From billburns at pennswoods.net Tue Jan 4 02:34:39 2005 From: billburns at pennswoods.net (Bill Burns) Date: Tue Jan 4 02:24:54 2005 Subject: [Tutor] Sorting/filtering data, dictionary question & Re:OT In-Reply-To: <41D92680.6060509@tds.net> References: <200501030147.34968.billburns@pennswoods.net> <41D92680.6060509@tds.net> Message-ID: <200501032034.39539.billburns@pennswoods.net> [Kent] > This is actually a common approach to sorting a list in Python - add enough > fields to the list so it sorts the way you want, then filter out the fields > you don't need any more. It even has a name, it's called Decorate - Sort - > Undecorate. In your case the 'decorate' step is built-in to the dictionary, > you are just doing the sort and undecorate. [Bill] Thank you, I'm glad I was actually doing something correctly. I didn't realize it was Decorate - Sort - Undecorate, now I know! [Kent] > There is a shorter way to write filterPipeData() using a list > comprehension, which is just a shortcut for what you wrote: > > def filterPipeData(data): > return [ (typ,inchSize,weight) for typ, size, inchSize, weight in data > ] > > When you get used to it, this form is more readable; it will also be faster > though I don't think you will notice. [Bill] I've seen list comprehensions, but I didn't understand how they worked. Since I understand how my function works, it's not so hard to figure out what your doing with this list comp. I guess the brackets ([]) signify a list. Then the for loop is placed on the right-hand side and then on the left, you append to "the list". I think the other list comprehensions I saw before were performing more operations, but this one is not that hard to figure out. [Kent] > If you calculate gallons per foot from your current ID values and put those > numbers in the dict, the new numbers will not be any more accurate than > your current calculations. If you have another source for gal/foot that you > think is more accurate then you could use those numbers. But I think what > you have is probably good enough and if it is working there is no need to > change it. [Bill] This sounds good to me. I'll keep the dict/function as is. Again, Thank you! I appreciate your feedback!! Bill From patric at usa.net Tue Jan 4 02:45:24 2005 From: patric at usa.net (Patric Michael) Date: Tue Jan 4 02:43:30 2005 Subject: [Tutor] A not-so-basic question... In-Reply-To: <009f01c4f178$9f8fd6b0$62bc8651@xp> Message-ID: <41D984B4.3854.13E1E6EE@localhost> Hi Alan... > Thats a great question, and I hope we get a few responses. Me too! Thank you for taking the time to reply. Your response is far more complex than my original example, but its very illustrative. Not so much in what to do, but how to think about doing it, and that was my goal. To determine how others _think_ about building an app. Very cool. :) Patric > > > For example, suppose I need a script that will collect order > information > > for a set of items ona page. Its output will go to a mail program > so the > > artist can be notified. > > I would start by abstracting the problem as much as possible > and identifying the classes/objects (I think in OOP by default > for anything non trivial) In this case I see an order and > and order item (or possibly several) > > Because I try to separate UI from application as much as possible I'd > start with these and build the two objects in a module and test them > using the >>> prompt. > > Order depends on order items so I'd build item first. > Then I'd test it - can I create an item? Can I print > an item? Because this is targetted at the web I might > right a dedicated str/repr method to grenerate an XML > representation... > > Then I'd build the orderand test it. > Can I add items to it? etc. > > Then I'd think about how to extend the order items. > I don't want to use subclassing since that means custom > code for every new product we sell, better to use a > dictionary of attributes I think... Lets try that out, > back to item again... > > OK, It all works from the >>> prompt lets think about the UI. > > (Here I confess that I do very little web programming) > > I need to > - present the items for selection/browsing > - offer the opportunity to create an order(add to basket) > - present an order summary > - capture customer details > - allow confirmation of the order > - process the order > > This points out that I also need a customer object! > Back to the application construction..... > OK I now have a customer object, back to the UI. > > I'll build the UI in generic terms using the bullets above > as hook functions - so I can change behaviour easily later. > Thus calling Order.process(customer) will initially generate > an email, but later I may subclass order to provide a direct > link to the suppliers via a B2B gateway or Web Service... > > I'll have to think about the order process at this point > and join the bits up in the right order. Back to the >>> > prompt for some experimenting. > > Now I design the HTML and set up my web server environment. > > I write my CGI handling parts to call the application objects > as required. > > It all works, but nothing is persisted. > Build a persistence mechanism, how many orders? > Maybe store the data in XML files somewhere for a few orders? > Or go for growth and build a full database and write > persistence methods to match. > > I think I'm ready for accepance testing now. > > In terms of modules, I'll probably put the order, item, > and customer in separate modules. > > I'll put the UI stuff in a single module. > The database bits will probably go along with the objects > themselves. > > If its for production use rather than a prototype I'd > document the class structure, the order processing > sequence and the database schema and persistence mechanism. > I'd also document the file structure on the web server. > The rest of the documentation I'd leave as comments and > docstrings since its a very small application. In total > I'd expect a design document of 5-7 pages or so. > > Alan G. > > From patric at usa.net Tue Jan 4 03:03:30 2005 From: patric at usa.net (Patric Michael) Date: Tue Jan 4 03:01:38 2005 Subject: [Tutor] A not-so-basic question... In-Reply-To: <41D93CA6.4010406@tds.net> References: <41D80EC3.4463.E2CFAE3@localhost> Message-ID: <41D988F2.11226.13F27AA9@localhost> Hello Kent... > I am a strong proponent of > - incremental development > - unit testing and test-first (or at least test-concurrent) > programming - Don't Repeat Yourself and merciless refactoring I see we have similarities. I purely hate having to rewrite something I've already done once to satisfaction. > > I look for a small piece of a problem, or a simplification of the > problem, and write some code. I write unit tests for the code as I go. > When I have the first bit working, I add some more. I refactor to > improve the clarity of the code and avoid duplication. Again, we are similar. In my case though, its primarily because I don't do enough scripting to remember little things, like slices for example. > > ================ > > Look for a way to automate your tests. If you are writing a CGI and > testing it in place with a web browser, the testing will quickly > become tedious and you will rarely test more than the last code that > you wrote. If your tests are automated and quick, you can run many > tests at once. That way you have confidence that you haven't broken > anything. I don't quite grasp "automated testing". And yes, I test in place constantly, but it only ever gets tedious when I cant figure out why a thing doesn't work. ;) > > In your example, automated testing may be a challenge. If I understand > correctly, you are basically getting data from a form and putting it > into an email. Some suggestions: - If there is any manipulation of the > data in the middle, you can break that into functions and test those > functions separately. - You can abstract the mailer into a generic > back end. Then you can make a test version that doesn't actually send > any mail, but that lets you verify that the program asked for mail to Ah. I see what you mean. Ironically I do just that. IF the final output is supposed to go to a mailer module, I'll send it to a page to make sure that whats supposed to be there shows up in print. > be sent. Then use an automated tester for web apps to drive the front > end and make sure the back end is called correctly. - This thread on > comp.lang.python about unit testing CGIs may be helpful: > http://tinyurl.com/5yr4c > > It can take some work to set up a test jig but I find it usually pays > off. Once you have done it once you will have the techniques down and > you can use the same techniques in similar projects. > > ================ > > Look for duplication in your code and don't tolerate it. Not only in a > single project but between projects. If you are writing many similar > CGIs you should be building up a library of tested support modules > that you can re-use. This will make subsequent projects easier. It does make things MUCH easier. When I started out, I was cutting from one script to another until I figured out what a "module" was. Now the calls to the database I/O are nothing more than importing a module or function. And because they were tested when being built, if I send a dictionary to a module, I _know_ it will be inserted without checking. Thats what you meant by abstracting, right? > > Whenever you are tempted to copy and paste code, look for a way to > abstract the code into a function or class so the copying is not > needed. > > Whenever you are tempted to copy and paste data, look for a way to > centralize the data so all clients work from the same reference. Yes, thank you. I am glad to see my original thoughts are supported. Patric > > > HTH, > Kent > > Patric Michael wrote: > > Hi folks... > > > > I was thinking about this the other day while prepping to write up > > another CGI thing. It occurred to me to wonder how other folks > > prepare an app, script, whatever. So the question is, and I am not > > looking for a "right answer" here. (I doubt ther eis one, to be > > honest.) > > > > How do you go about setting up a new application? > > > > For example, suppose I need a script that will collect order > > information for a set of items ona page. Its output will go to a > > mail program so the artist can be notified. I know that the script > > needs to be somewhat customizable since more than one person will > > use it, and I want to look to the future where such a script might > > get superceded for something like credit card orders, or possible > > integration with a larger order processing scheme. > > > > I'd start out by commenting goals in a new file, like so: > > > > ------------------------------ > > # Initialize variables > > > > # Gather form data > > > > # Output results > > -------------------------------------- > > Then I'd go back through each comment and either call modules I've > > prewritten, or write new defs. Then I'd add import statements to > > cover the modules and the defs: > > > > ---------------------------------- > > from datetime import now > > import cgi > > from mailman import send > > > > # Initialize variables > > form = cgi.FieldStorage() > > > > # Gather form data > > for each in form: > > blah > > blah > > > > # Output results > > result = {} > > send(result) > > > > ---------------------------------------- > > > > And so on. > > > > So the question is, how do you do it? > > > > Is there a method you prefer? Something tried and true, or maybe > > foolproof? > > > > Just curious... And maybe a bit hopeful someone has a better way > > than mine. Seems like I do an awful lot of testing... :) > > > > Patric > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From billburns at pennswoods.net Tue Jan 4 03:26:26 2005 From: billburns at pennswoods.net (Bill Burns) Date: Tue Jan 4 03:16:40 2005 Subject: [Tutor] Re: Sorting/filtering data, dictionary question In-Reply-To: References: Message-ID: <200501032126.26725.billburns@pennswoods.net> On Monday 03 January 2005 8:35 am, Bob Gibson wrote: > Bill: > > Could you have the "LineEdit#" be your sortable field? If each # after > the "LineEdit" prefix were the same length, then you could easily sort on > it. Bob, Using your suggestion, I did a little test on a modified version of my pipe dictionary: pipeDict = \ {('lineEdit001','Steel(Std)','1/2"',.85): .622, ('lineEdit002','Steel(Std)','3/4"',1.13): .824, ('lineEdit003','Steel(Std)','1"',1.678): 1.049, ('lineEdit004','Steel(Std)','1-1/4"',2.272): 1.38, ('lineEdit005','Steel(Std)','1-1/2"',2.717): 1.61, ('lineEdit006','Steel(Std)','2"',3.652): 2.067, ('lineEdit007','Steel(Std)','2-1/2"',5.79): 2.469, ('lineEdit008','Steel(Std)','3"',7.57): 3.068, ('lineEdit009','Steel(Std)','3-1/2"',9.11): 3.548, ('lineEdit010','Steel(Std)','4"',10.79): 4.026, ('lineEdit011','Steel(Std)','5"',14.62): 5.047, ('lineEdit012','Steel(Std)','6"',18.97): 6.065, ('lineEdit013','Steel(Std)','8"',28.55): 7.981, ('lineEdit014','Steel(Std)','10"',40.48): 10.02} And using these functions again (slightly modified): def someReport(): report = [] for name, typ, size, weight in pipeDict: report.append((name,typ,size,weight)) report.sort() newReport = filterPipeData(report) print newReport def filterPipeData(data): """idea for list comp, from Kent" return [(typ,size,weight) for name, typ, size, weight in data] It works perfectly! Thank You! Notice how I removed the other "size" number that was in the previous dict. You mentioned that the #'s I use, will have to be the same length. My GUI uses 100 lineEdits, so a numbering scheme like 001 - 100 should sort correctly (I guess). I haven't tried it on the whole dictionary yet, but I will! Changing the numbering of the lineEdits, is *not* a problem at all. Thanks again, Bill From orion_val at 163.com Tue Jan 4 03:28:55 2005 From: orion_val at 163.com (=?utf-8?B?5rKI5rSB5YWD?=) Date: Tue Jan 4 03:30:08 2005 Subject: [Tutor] Breaking down integers? References: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> Message-ID: <004501c4f205$49fa5f80$b42950d3@juan> I have writen a code to do the process you need. And It is not only suitable for integers, but also decimals and negatives! The main method is type conversion functions of Python as float(), str() and so on. I wish it will give you some illumination. Juan Shen #!/usr/local/bin/python #Filename: float2list.py import sys try: a=float(raw_input('Input a number: ')) except ValueError: print 'Input is not a number.' sys.exit() print 'The input number is:',a #knock down number into list lista=list(str(a)) print 'A number as a list:',lista #Pack this number from list again #Process negative number if lista[0] is '-': sign=-1 lista=lista[1:] size1=lista.index('.') size2=len(lista)-lista.index('.')-1 integer=sum([int(lista[i])*10**(size1-i-1) for i in range(0,size1)]) decimal=sum([float(lista[lista.index('.')+i])*10**(-i) for i in range(1,size2+1)]) a2=sign*(integer+decimal) print 'The number is packed again:',a2 ----- Original Message ----- From: kilovh To: tutor@python.org Sent: Tuesday, January 04, 2005 6:17 AM Subject: [Tutor] Breaking down integers? I would like to be able to take an integer, break it down into individual items in a list, and then put them back together. I know how to do this last part thanks to Orri Ganel and Guillermo Fernandex, but what about taking the integer apart? Sorry if the questions have incredibly obvious answers, I'm new to this. ~kilovh -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050104/18df610d/attachment.html From keridee at jayco.net Tue Jan 4 05:09:31 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 4 05:10:09 2005 Subject: [Tutor] Sorting/filtering data, dictionary question & Re:OT References: <200501030147.34968.billburns@pennswoods.net><41D92680.6060509@tds.net> <200501032034.39539.billburns@pennswoods.net> Message-ID: <008801c4f213$3ff49330$105428cf@JSLAPTOP> > [Bill] > I've seen list comprehensions, but I didn't understand how they worked. > Since I understand how my function works, it's not so hard to figure out > what your doing with this list comp. I guess the brackets ([]) signify a list. > Then the for loop is placed on the right-hand side and then on the left, you > append to "the list". I think the other list comprehensions I saw before > were performing more operations, but this one is not that hard to figure out. You're thinking right. The syntax is supposed to be something like [ altereditem for item in iterable if condition ] So, to get all the even numbers between 1 & 10 >>> a = [x for x in range(1,11) if x % 2 == 0] >>> print a [2,4,6,8,10] For a much, much better explanation, I recommend Alan Gauld's tutorial, the section which is relevant can be found here http://www.freenetpages.co.uk/hp/alan.gauld/tutfctnl.htm HTH, Jacob Schmidt From mario_rol at hotmail.com Tue Jan 4 09:57:02 2005 From: mario_rol at hotmail.com (Mario Rol) Date: Tue Jan 4 09:58:05 2005 Subject: [Tutor] Breaking down integers? In-Reply-To: <20050103225502.17BB91E400F@bag.python.org> Message-ID: First, convert the integer to a string by using str(), then convert the string to a list by using list() >------------------------------ > >Message: 6 >Date: Mon, 3 Jan 2005 17:17:36 -0500 >From: "kilovh" >Subject: [Tutor] Breaking down integers? >To: >Message-ID: <001a01c4f1e2$08858ed0$2002a8c0@TSVI> >Content-Type: text/plain; charset="iso-8859-1" > >I would like to be able to take an integer, break it down into individual >items in a list, and then put them back together. I know how to do this >last part thanks to Orri Ganel and Guillermo Fernandex, but what about >taking the integer apart? > >Sorry if the questions have incredibly obvious answers, I'm new to this. > >~kilovh _________________________________________________________________ Talk with your online friends with MSN Messenger http://messenger.msn.nl/ From alan.gauld at freenet.co.uk Tue Jan 4 13:28:17 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 4 13:27:43 2005 Subject: [Tutor] A not-so-basic question... References: <41D80EC3.4463.E2CFAE3@localhost> <41D988F2.11226.13F27AA9@localhost> Message-ID: <01d701c4f258$e37906e0$62bc8651@xp> > I don't quite grasp "automated testing". And yes, I test in place > constantly, but it only ever gets tedious when I cant figure out why a > thing doesn't work. ;) Think about writing a moduule and testing it at the >>> prompt. What you are doing is typing a series of python commands into the prompt to excercise the code you wrote and checking that the result is what you expect. So why not write a program to issue those same commands and check the results match what you expect? Then anytime you change your module you can run all the tests again in a second or two instead of retyping all the tests over again. Now if you run the original tets in something like IDLEs shell window you can save the session and quickly edit it into a set of tests with the results already there. For example here is a fictitious session testing a fictitious module: >>> import mymodule as m >>> testobject = m.myClass() >>> type(testobject) # I said it was fictitious!! >>> testobject.doit() 'lots of interesting output' >>> testobject.doit(42) # valid value ' more interesting stuff' >>> testobject('ghfghfghf') #invalid value , exception raised by MyClass code' Now if I save that and edit the file I can easily turn it into: ############ import time print 'Starting tests for module MyClass at: ', time.time() import mymodule as m testobject = m.MyClass() if type(testobject) == type(m.MyClass(): print 'instantiation worked' else: print 'ERROR: failed to instantiate correctly if testobject.doit() == 'lots of interesting output': print 'default doit passed' else: print 'ERROR: default doit failed' if testobject.doit(42) == ' more interesting stuff': print 'doit(42) passed' else: print 'ERROR: doit(42) failed' try: testobject.doit('ghfghfghf') #invalid value print 'ERROR: doit failed to detect faulty data' except MyClassException: print 'bad data detected OK' print 'End of tests for module MyClass ################# And after any changes to MyClass I can run this program and get (hopefully!) output like: Starting tests for module MyClass at 12:34pm instantiation worked default doit passed doit(42) passed Bad data detected OK End of tests for module MyClass Some people prefer to skip the print outs and just bomb out with an error if a test fails or print a single "All tests passed" at the end. Personally I like a little feedback, but if there's too much output you have to analyse it for errors - Thats why the error results have ERROR in big letters to spot it easily. If you are making lots of changes this saves a lot of time and effort and you are much more likely to do the tests than if you had to type them all by hand each time! This is whats referred to as regression testing - repeating past tests to make sure changes haven't broken what used to work! Now what I did is doing it the hard way, there are various tools around to make the task easier, look at the unittest, doctest and assert features of Python for some more ideas. HTH Alan G. From bgibson at us.ibm.com Tue Jan 4 14:10:53 2005 From: bgibson at us.ibm.com (Bob Gibson) Date: Tue Jan 4 14:10:59 2005 Subject: [Tutor] Re: Sorting/filtering data, dictionary question In-Reply-To: <20050104110231.877F21E402F@bag.python.org> Message-ID: Bill: Sometimes seeing something simple like this can make all the difference. Glad to be able to help. Bob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050104/e0698203/attachment.html From flaxeater at yahoo.com Tue Jan 4 16:41:29 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Tue Jan 4 16:41:31 2005 Subject: [Tutor] Breaking down integers? Message-ID: <20050104154129.76117.qmail@web54301.mail.yahoo.com> aint=1077 list_of_aint_as_string=list(str(1077)) print list_of_aint_as_string #['1', '0', '7', '7'] #turn it into a list of integers #doc for map() found on http://docs.python.org/lib/built-in-funcs.html list_of_aint_as_integer=map(int,list_of_aint_as_string) print list_of_aint_as_integer #[1, 0, 7, 7] #or #a list comprehension tutorial found #http://nltk.sourceforge.net/tutorial/advpython/nochunks.html#list_comprehensions list_of_aint_as_integer=[int(x) for x in list_of_aint_as_string] print list_of_aint_as_integer #[1, 0, 7, 7] #now put it into one operation broken_int=map(int,list(str(1077))) print broken_int def return_a_broken_int(aint): return [int(x) for x in list(str(1077))] print return_a_broken_int(aint) #[1, 0, 7, 7] Now just put what you want or understand into a function and you can use this. HTH kilovh wrote: > I would like to be able to take an integer, break it down into > individual items in a list, and then put them back together. I know > how to do this last part thanks to Orri Ganel and Guillermo Fernandex, > but what about taking the integer apart? > > Sorry if the questions have incredibly obvious answers, I'm new to this. > > ~kilovh > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 From davholla2002 at yahoo.co.uk Tue Jan 4 22:08:32 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Tue Jan 4 22:08:34 2005 Subject: [Tutor] CGI help Message-ID: <20050104210832.44614.qmail@web25406.mail.ukl.yahoo.com> Rich, That worked although I had to set the address to :- http://127.0.0.1:8000/friends1.html Thanks. ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From python at bernardlebel.com Tue Jan 4 22:47:23 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Tue Jan 4 22:47:24 2005 Subject: [Tutor] How to run a script file Message-ID: Hi, Sorry if I missed something obvious, but how do I execute a python script file in the interpreter? I have "Using the Python Interpreter" in the Python tutorial but not much is said... (this might be a lame quesiton but so far I always used either the PythonWin interpreter wich has the Import function, or I ran Python code in an application. Now I'm on Linux so I have to learn the hard way!) Thanks Bernard From patric at usa.net Tue Jan 4 23:55:54 2005 From: patric at usa.net (Patric Michael) Date: Tue Jan 4 23:53:49 2005 Subject: [Tutor] How to run a script file In-Reply-To: Message-ID: <41DAAE7A.5981.302B1FB@localhost> Hi Bernard... The most basic form is to type "python" followed by the script you want to run. If your script is not in the system path, you'll either need to cd to the directory, or give a full pathname: (the pythonpath doesn't come into play until the interperter is running. python /usr/local/share/filename.py Remember that the script will inherit whatever permissions you currently have, so either log in or su to the user that's expected to run the script. Oh, and in case python itself isnt in your system path, (it probably is) you can find it by typing "which python" at the shell prompt. Patric > Hi, > > Sorry if I missed something obvious, but how do I execute a python > script file in the interpreter? I have "Using the Python Interpreter" > in the Python tutorial but not much is said... > > (this might be a lame quesiton but so far I always used either the > PythonWin interpreter wich has the Import function, or I ran Python > code in an application. Now I'm on Linux so I have to learn the hard > way!) > > > Thanks > Bernard > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From johnp at milwaukielumber.com Wed Jan 5 00:04:12 2005 From: johnp at milwaukielumber.com (John Purser) Date: Wed Jan 5 00:04:23 2005 Subject: [Tutor] How to run a script file In-Reply-To: <41DAAE7A.5981.302B1FB@localhost> Message-ID: <200501042304.j04N4C8p015761@mail.morseintranet.com> Bernard, If you're new to Linux you might not be aware of an additional method to run python scripts. If the first line of your script is: #!/usr/bin/python And you've set your script permissions to be executable (chmod 700 myscript.py) then you can run your script just like any other program. You can double click on it in a GUI environment or run it from a command prompt by just typing the script name. Or depending on the value of $PATH variable you might need to type "./myscript.py". Those first characters have to be right though. I'm assuming your python is in /usr/bin. And that is a hash mark followed by an exclamation point. This is called "hash bang" in uningo. John Purser -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Patric Michael Sent: Tuesday, January 04, 2005 14:56 To: tutor@python.org Subject: Re: [Tutor] How to run a script file Hi Bernard... The most basic form is to type "python" followed by the script you want to run. If your script is not in the system path, you'll either need to cd to the directory, or give a full pathname: (the pythonpath doesn't come into play until the interperter is running. python /usr/local/share/filename.py Remember that the script will inherit whatever permissions you currently have, so either log in or su to the user that's expected to run the script. Oh, and in case python itself isnt in your system path, (it probably is) you can find it by typing "which python" at the shell prompt. Patric > Hi, > > Sorry if I missed something obvious, but how do I execute a python > script file in the interpreter? I have "Using the Python Interpreter" > in the Python tutorial but not much is said... > > (this might be a lame quesiton but so far I always used either the > PythonWin interpreter wich has the Import function, or I ran Python > code in an application. Now I'm on Linux so I have to learn the hard > way!) > > > Thanks > Bernard > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From python at bernardlebel.com Wed Jan 5 00:32:54 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Wed Jan 5 00:33:00 2005 Subject: [Tutor] How to run a script file In-Reply-To: <200501042304.j04N4C8p015761@mail.morseintranet.com> References: <200501042304.j04N4C8p015761@mail.morseintranet.com> Message-ID: <41DB27A6.10701@bernardlebel.com> Okay sorry I meant once you're in Python. I'm in Bash console, type Python, enter the Python interpreter. Then I add my custom path to the sys.path list (because my user permissions do not allow my to put anything in the Lib directory) and then I try an import /home/bernardl/python/myScript.py but of course if fails as soon the first slash is read. Thanks Bernard John Purser wrote: > Bernard, > > If you're new to Linux you might not be aware of an additional method to run > python scripts. If the first line of your script is: > #!/usr/bin/python > > And you've set your script permissions to be executable (chmod 700 > myscript.py) then you can run your script just like any other program. You > can double click on it in a GUI environment or run it from a command prompt > by just typing the script name. Or depending on the value of $PATH variable > you might need to type "./myscript.py". Those first characters have to be > right though. I'm assuming your python is in /usr/bin. And that is a hash > mark followed by an exclamation point. This is called "hash bang" in > uningo. > > John Purser > > -----Original Message----- > From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf > Of Patric Michael > Sent: Tuesday, January 04, 2005 14:56 > To: tutor@python.org > Subject: Re: [Tutor] How to run a script file > > Hi Bernard... > > The most basic form is to type "python" followed by the script you want > to run. If your script is not in the system path, you'll either need to cd > to > the directory, or give a full pathname: (the pythonpath doesn't come into > play until the interperter is running. > > python /usr/local/share/filename.py > > Remember that the script will inherit whatever permissions you currently > have, so either log in or su to the user that's expected to run the script. > > Oh, and in case python itself isnt in your system path, (it probably is) > you can find it by typing "which python" at the shell prompt. > > > Patric > > > > >>Hi, >> >>Sorry if I missed something obvious, but how do I execute a python >>script file in the interpreter? I have "Using the Python Interpreter" >>in the Python tutorial but not much is said... >> >>(this might be a lame quesiton but so far I always used either the >>PythonWin interpreter wich has the Import function, or I ran Python >>code in an application. Now I'm on Linux so I have to learn the hard >>way!) >> >> >>Thanks >>Bernard From michael at trollope.org Wed Jan 5 00:39:18 2005 From: michael at trollope.org (Michael Powe) Date: Wed Jan 5 00:39:23 2005 Subject: [Tutor] regex problem Message-ID: <20050104233917.GA29986@titan.spiretech.com> Hello, I'm having erratic results with a regex. I'm hoping someone can pinpoint the problem. This function removes HTML formatting codes from a text email that is poorly exported -- it is supposed to be a text version of an HTML mailing, but it's basically just a text version of the HTML page. I'm not after anything elaborate, but it has gotten to be a bit of an itch. ;-) def parseFile(inFile) : import re bSpace = re.compile("^ ") multiSpace = re.compile(r"\s\s+") nbsp = re.compile(r" ") HTMLRegEx = re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) ",re.I) f = open(inFile,"r") lines = f.readlines() newLines = [] for line in lines : line = HTMLRegEx.sub(' ',line) line = bSpace.sub('',line) line = nbsp.sub(' ',line) line = multiSpace.sub(' ',line) newLines.append(line) f.close() return newLines Now, the main issue I'm looking at is with the multiSpace regex. When applied, this removes some blank lines but not others. I don't want it to remove any blank lines, just contiguous multiple spaces in a line. BTB, this also illustrates a difference between python and perl -- in perl, i can change "line" and it automatically changes the entry in the array; this doesn't work in python. A bit annoying, actually. ;-) Thanks for any help. If there's a better way to do this, I'm open to suggestions on that regard, too. mp From billburns at pennswoods.net Wed Jan 5 01:00:53 2005 From: billburns at pennswoods.net (Bill Burns) Date: Wed Jan 5 00:51:10 2005 Subject: [Tutor] Sorting/filtering data, dictionary question & Re:OT In-Reply-To: <008801c4f213$3ff49330$105428cf@JSLAPTOP> References: <200501030147.34968.billburns@pennswoods.net> <200501032034.39539.billburns@pennswoods.net> <008801c4f213$3ff49330$105428cf@JSLAPTOP> Message-ID: <200501041900.53864.billburns@pennswoods.net> On Monday 03 January 2005 11:09 pm, Jacob S. wrote: > You're thinking right. The syntax is supposed to be something like > > [ altereditem for item in iterable if condition ] > > So, > to get all the even numbers between 1 & 10 > > >>> a = [x for x in range(1,11) if x % 2 == 0] > >>> print a > > [2,4,6,8,10] > > For a much, much better explanation, I recommend Alan Gauld's tutorial, the > section which is relevant can be found here > http://www.freenetpages.co.uk/hp/alan.gauld/tutfctnl.htm > Jacob, Thank you for the information & link, I'll definitely check it out. Bill From johnp at milwaukielumber.com Wed Jan 5 00:57:22 2005 From: johnp at milwaukielumber.com (John Purser) Date: Wed Jan 5 00:57:27 2005 Subject: [Tutor] How to run a script file In-Reply-To: <41DB27A6.10701@bernardlebel.com> Message-ID: <200501042357.j04NvL8p018107@mail.morseintranet.com> I'm not sure why it fails "of course". How do you know it's failing at the first slash? Also you might want to look at your .profile file in your home directory and modify your path there. John Purser -----Original Message----- From: tutor-bounces+johnp=milwaukielumber.com@python.org [mailto:tutor-bounces+johnp=milwaukielumber.com@python.org] On Behalf Of Bernard Lebel Sent: Tuesday, January 04, 2005 15:33 To: tutor@python.org Subject: Re: [Tutor] How to run a script file Okay sorry I meant once you're in Python. I'm in Bash console, type Python, enter the Python interpreter. Then I add my custom path to the sys.path list (because my user permissions do not allow my to put anything in the Lib directory) and then I try an import /home/bernardl/python/myScript.py but of course if fails as soon the first slash is read. Thanks Bernard John Purser wrote: > Bernard, > > If you're new to Linux you might not be aware of an additional method to run > python scripts. If the first line of your script is: > #!/usr/bin/python > > And you've set your script permissions to be executable (chmod 700 > myscript.py) then you can run your script just like any other program. You > can double click on it in a GUI environment or run it from a command prompt > by just typing the script name. Or depending on the value of $PATH variable > you might need to type "./myscript.py". Those first characters have to be > right though. I'm assuming your python is in /usr/bin. And that is a hash > mark followed by an exclamation point. This is called "hash bang" in > uningo. > > John Purser > > -----Original Message----- > From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf > Of Patric Michael > Sent: Tuesday, January 04, 2005 14:56 > To: tutor@python.org > Subject: Re: [Tutor] How to run a script file > > Hi Bernard... > > The most basic form is to type "python" followed by the script you want > to run. If your script is not in the system path, you'll either need to cd > to > the directory, or give a full pathname: (the pythonpath doesn't come into > play until the interperter is running. > > python /usr/local/share/filename.py > > Remember that the script will inherit whatever permissions you currently > have, so either log in or su to the user that's expected to run the script. > > Oh, and in case python itself isnt in your system path, (it probably is) > you can find it by typing "which python" at the shell prompt. > > > Patric > > > > >>Hi, >> >>Sorry if I missed something obvious, but how do I execute a python >>script file in the interpreter? I have "Using the Python Interpreter" >>in the Python tutorial but not much is said... >> >>(this might be a lame quesiton but so far I always used either the >>PythonWin interpreter wich has the Import function, or I ran Python >>code in an application. Now I'm on Linux so I have to learn the hard >>way!) >> >> >>Thanks >>Bernard _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From patric at usa.net Wed Jan 5 01:46:12 2005 From: patric at usa.net (Patric Michael) Date: Wed Jan 5 01:44:07 2005 Subject: [Tutor] How to run a script file In-Reply-To: <41DB27A6.10701@bernardlebel.com> References: <200501042304.j04N4C8p015761@mail.morseintranet.com> Message-ID: <41DAC854.3137.367B336@localhost> HI Bernard... I think I see what you might mean.... I'm guessing your session goes something like this: >>> import sys >>> sys.path.append(' /home/bernardl/python/') >>> import /home/bernardl/python/myScript File "", line 1 import /home/bernardl/python/myScript ^ SyntaxError: invalid syntax Right? If so, its because the full pathname is no longer necessary after you've added it to the path. Just type >>>import myScript and that will bring it in. :) Let us know if we've still misunderstood... Patric > Okay sorry I meant once you're in Python. > > I'm in Bash console, type Python, enter the Python interpreter. > > Then I add my custom path to the sys.path list (because my user > permissions do not allow my to put anything in the Lib directory) and > then I try an import /home/bernardl/python/myScript.py but of course > if fails as soon the first slash is read. > > > Thanks > Bernard > > > John Purser wrote: > > Bernard, > > > > If you're new to Linux you might not be aware of an additional > > method to run python scripts. If the first line of your script is: > > #!/usr/bin/python > > > > And you've set your script permissions to be executable (chmod 700 > > myscript.py) then you can run your script just like any other > > program. You can double click on it in a GUI environment or run it > > from a command prompt by just typing the script name. Or depending > > on the value of $PATH variable you might need to type > > "./myscript.py". Those first characters have to be right though. > > I'm assuming your python is in /usr/bin. And that is a hash mark > > followed by an exclamation point. This is called "hash bang" in > > uningo. > > > > John Purser > > > > -----Original Message----- > > From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On > > Behalf Of Patric Michael Sent: Tuesday, January 04, 2005 14:56 To: > > tutor@python.org Subject: Re: [Tutor] How to run a script file > > > > Hi Bernard... > > > > The most basic form is to type "python" followed by the script you > > want to run. If your script is not in the system path, you'll > > either need to cd to the directory, or give a full pathname: (the > > pythonpath doesn't come into play until the interperter is running. > > > > python /usr/local/share/filename.py > > > > Remember that the script will inherit whatever permissions you > > currently have, so either log in or su to the user that's expected > > to run the script. > > > > Oh, and in case python itself isnt in your system path, (it probably > > is) you can find it by typing "which python" at the shell prompt. > > > > > > Patric > > > > > > > > > >>Hi, > >> > >>Sorry if I missed something obvious, but how do I execute a python > >>script file in the interpreter? I have "Using the Python > >>Interpreter" in the Python tutorial but not much is said... > >> > >>(this might be a lame quesiton but so far I always used either the > >>PythonWin interpreter wich has the Import function, or I ran Python > >>code in an application. Now I'm on Linux so I have to learn the hard > >>way!) > >> > >> > >>Thanks > >>Bernard > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From rmkrauter at yahoo.com Wed Jan 5 02:43:19 2005 From: rmkrauter at yahoo.com (Rich Krauter) Date: Wed Jan 5 02:43:23 2005 Subject: [Tutor] CGI help In-Reply-To: <20050104210832.44614.qmail@web25406.mail.ukl.yahoo.com> References: <20050104210832.44614.qmail@web25406.mail.ukl.yahoo.com> Message-ID: <41DB4637.1030904@yahoo.com> David Holland wrote: > Rich, > > That worked although I had to set the address to :- > http://127.0.0.1:8000/friends1.html > Thanks. > Good catch, sorry about that. I noticed you had spaces in the "how many" field in your form. Did your cgi still work? If not, I guess you figured that out by now too. Interestingly, I had trouble getting the server/cgi to work with python 2.4/Windows XP. If I started the server with python 2.4 and put 2.4 in the cgi's #! line, the cgi didn't work. If I used 2.3 to start the server, and put 2.3 or 2.4 in the cgi's #! line, the cgi worked. I tried the same combinations on linux, and they all worked fine. Anyone else seen this problem with python 2.4 on windows xp? I looked quickly at diffs of the relevant codes between 2.3 and 2.4, and I did a quick bug search on sourceforge, but didn't turn up anything yet. Of course it may not be a bug; it may be a problem with my setup. The files I used are posted below, in case anyone wants to try to reproduce the result I saw. Thanks. Rich <> import CGIHTTPServer import BaseHTTPServer def run(server_class=BaseHTTPServer.HTTPServer, handler_class=CGIHTTPServer.CGIHTTPRequestHandler): server_address = ('', 8000) httpd = server_class(server_address, handler_class) httpd.serve_forever() run() <> <> #!C:/Python24/python.exe print "Content-Type: text/html" print import cgi,cgitb cgitb.enable() reshtml = ''' CGI Demo

Friends list for:%s

Your name is: %s
You have %s friends. ''' form = cgi.FieldStorage() who = form.getfirst('person') howmany = form.getfirst('howmany') print reshtml % (who, who, howmany) <> <> CGI Demo

Friends list for New User

Enter your name
How many friends do you have? 0 10 25 50 100>
<> From neal at bcn.boulder.co.us Wed Jan 5 03:31:24 2005 From: neal at bcn.boulder.co.us (Neal McBurnett) Date: Wed Jan 5 03:31:46 2005 Subject: pickle juice and YAML (Re: [Tutor] dumping .pck files) In-Reply-To: <00b501c4f17a$24e925e0$62bc8651@xp> <41D86CA6.21475.F9BC473@localhost> <200501030457.j034v8We019029@lock.gotdns.org> References: <200501030457.j034v8We019029@lock.gotdns.org> <00b501c4f17a$24e925e0$62bc8651@xp> <200501030457.j034v8We019029@lock.gotdns.org> <41D86CA6.21475.F9BC473@localhost> <200501030457.j034v8We019029@lock.gotdns.org> Message-ID: <20050105023124.GG1938@feynman> Thanks to Patric Michael and Alan Gauld for helpful responses. The mailman "config_list" command was perfect for my near-term needs. A bit more digging led me to juicier info on pickles, for my longer term nourishment and development. Lots of documentation is in /usr/lib/python2.3/pickletools.py What I was asking for is provided by the "dis()" routine. E.g. import pickletools print pickletools.dis(open("config.pck")) which requires no class information at all and produces a low-level byte-by-byte disassembly like this: 0: } EMPTY_DICT 1: q BINPUT 1 3: ( MARK 4: U SHORT_BINSTRING 'send_welcome_msg' 22: q BINPUT 2 .... The page at http://python.active-venture.com/lib/node63.html clarifies that in fact the default pickle format is ascii. But mine was protocol version 1 ("binary") and thus unpleasant to look at in an editor. The ascii version is safe but also probably awfully cryptic. I also learned: One of the design goals of the pickle protocol is to make pickles "context-free": as long as you have installed the modules containing the classes referenced by a pickle, you can unpickle it, without needing to import any of those classes ahead of time. and I succeeded in getting a good dump by running my original 2-liner in the directory that contained the file MailList.py, or by just doing "from Mailman import MailList" Details are in PEP 307: http://www.python.org/peps/pep-0307.html Finally, another serialization format that is eminently readable is YAML ("YAML Ain't Markup Language"). Besides Python, it works for Ruby et al., can be used as an alternative to xml, etc. http://www.yaml.org/ http://www.pyyaml.org http://www.yaml.org/spec/ yaml and xml http://www-106.ibm.com/developerworks/library/x-matters23.html http://yaml.kwiki.org/?YamlInFiveMinutes I found it a bit difficult to uncover the latest on Python and YAML. These notes should help. To get your feet wet, download from http://www.pyyaml.org/cgi-bin/trac.cgi/wiki/ArchiveTarballs The most recent seems to be Mike Orr's Work in Progess: http://python.yaml.org/dist/PyYaml_0.32_MONEW.tar.gz Unapck it, run "demo.py", and read the code to figure out what was going on :-) Don't miss the experimental "ypath" demo which is, I guess, sort of like xpath and allows you to select data items out of hierarchical data structures. There is some documentation at http://www.pyyaml.org/cgi-bin/trac.cgi/wiki/UserDocumentation But as described at http://www.pyyaml.org/cgi-bin/trac.cgi/ticket/11 there are security issues with the PyYaml implementation in terms of importing arbitrary data structures, and other limitations currently. But YAML is an interesting development. Another implementation for Ruby, Perl, Python, PHP and oCaml is syck: http://whytheluckystiff.net/syck/ but I haven't seen any demos of that for Python, just for Ruby. Cheers, Neal McBurnett http://bcn.boulder.co.us/~neal/ Signed and/or sealed mail encouraged. GPG/PGP Keyid: 2C9EBA60 On Sun, Jan 02, 2005 at 09:57:08PM -0700, Neal McBurnett wrote: > I want to explore some mailman config files, e.g. > config.pck. Some quick searches informed me that these > contain pickled configuration info. > > My first naive attempt to dump what was in them failed: > > >>> import pickle > >>> pickle.load(open("config.pck")) > traceback.... > ImportError: No module named Mailman.Bouncer > > It seems that to do a good job of dumping the data, I need to tell it > what this class looks like. > > Are there alternatives? Does the pickle format really not provide a > way to inspect the data without the class definitions? E.g. if I > didn't have the source code or didn't want to dig around in it? > > Is there a simple way to dump everything it does understand, and > just print stubs for the parts it doesn't? > > Or is there some nice software to do this all for me? > > Or perhaps does mailman itself have handy command-line tools to aid in > comparing configurations across mailing lists? > > Are there other serialization options that would make this easier? > > Many thanks, > > Neal McBurnett http://bcn.boulder.co.us/~neal/ > Signed and/or sealed mail encouraged. GPG/PGP Keyid: 2C9EBA60 From cyresse at gmail.com Wed Jan 5 03:31:59 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 5 03:32:02 2005 Subject: [Tutor] Array pointers Message-ID: Hi all, Just playing with Pygame, attempting to build a version of Scorched Earth if you remember it. I want to represent dirt, and so far I've come up with using a surface array - 0 64412 0 64412 64412 0 0 64412 64412 64412 0 64412 0 0 0 64412 64412 0 64412 0 Where a 0 indicates no pixel, 64412 is a representative pixel colour mapping. Now, that works OK, but I wanted to animate each pixel of dirt falling,so pseudocode - if there's no pixel below, move value down one, unless value is at the bottom of the column so - Frame 0 - 0 64412 0 64412 64412 0 0 64412 64412 64412 0 64412 0 0 0 64412 64412 0 64412 0 Frame 1: 0 0 0 0 0 0 64412 0 64412 64412 0 64412 64412 64412 64412 64412 64412 0 64412 0 64412 64412 0 0 becoming 0 64412 64412 0 I envisage as a two step process with the below as an intermediate. This is wasteful, I'm sure. 64412 0 64412 0 Frame 2: 0 0 0 0 0 0 64412 0 64412 0 0 64412 0 64412 64412 64412 64412 64412 64412 64412 Frame 3+ 0 0 0 0 0 0 64412 0 64412 0 0 64412 0 64412 64412 64412 64412 64412 64412 64412 Has anyone used Numeric for this kind of thing before? Any little quirk of array mathematics I could exploit? Or, should I shuffle this one to the back of the ideas folder for when I've (if ever) learn Cpp? Regards, Liam Clarke PS -- '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. From cyresse at gmail.com Wed Jan 5 04:44:37 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 5 04:44:41 2005 Subject: [Tutor] Array pointers In-Reply-To: <787687ba05010419363f9f85db@mail.gmail.com> References: <787687ba05010419363f9f85db@mail.gmail.com> Message-ID: Sorry rephrase - So I have a list - [0,0,0,0,0,1,1,1,0,1,1,0,1,0] I want to move all the 1's move to the right, 1 index at a time, preserving any spacing. i.e. [1,0,0,0,0,1,1,1,0,1,1,0,1,0] [0,1,0,0,0,0,1,1,1,0,1,1,0,1] [0,0,1,0,0,0,0,1,1,1,0,1,1,1] [0,0,0,1,0,0,0,0,1,1,1,1,1,1] [0,0,0,0,1,0,0,0,1,1,1,1,1,1] [0,0,0,0,0,1,0,0,1,1,1,1,1,1] [0,0,0,0,0,0,1,0,1,1,1,1,1,1] [0,0,0,0,0,0,0,1,1,1,1,1,1,1] Now, I have a whole list of these lists.... and I create an array of them. Is there a way to apply these changes wholescale without a large calculation time? I'm starting to think I'm going to have to go Cpp for this kind of direct pixel tweaking stuff. (640x480 (let alone 1024x768) is a lot of pixels to run through a for... loop) So yeah, anyone had this before? Regards, Liam Clarke On Tue, 4 Jan 2005 22:36:55 -0500, Byron Saltysiak wrote: > perhaps I don't understand the question exactly cause I don't remember > the game but I think a nice way to do this would be to create a list > of 5-tuples. Randomly generate a 5-tuple and push it onto the > beginning of the list... pop the last off. > > > On Wed, 5 Jan 2005 15:31:59 +1300, Liam Clarke wrote: > > Hi all, > > > > Just playing with Pygame, attempting to build a version of Scorched > > Earth if you remember it. > > > > I want to represent dirt, and so far I've come up with using a surface array - > > > > 0 64412 0 64412 64412 > > 0 0 64412 64412 64412 > > 0 64412 0 0 0 > > 64412 64412 0 64412 0 > > > > Where a 0 indicates no pixel, 64412 is a representative pixel colour mapping. > > > > Now, that works OK, but I wanted to animate each pixel of dirt > > falling,so pseudocode - > > > > if there's no pixel below, move value down one, unless value is at the > > bottom of the column > > > > so - > > > > Frame 0 - > > > > 0 64412 0 64412 64412 > > 0 0 64412 64412 64412 > > 0 64412 0 0 0 > > 64412 64412 0 64412 0 > > > > Frame 1: > > > > 0 0 0 0 0 > > 0 64412 0 64412 64412 > > 0 64412 64412 64412 64412 > > 64412 64412 0 64412 0 > > > > 64412 > > 64412 > > 0 > > 0 > > > > becoming > > > > 0 > > 64412 > > 64412 > > 0 > > > > I envisage as a two step process with the below as an intermediate. > > This is wasteful, I'm sure. > > > > 64412 > > 0 > > 64412 > > 0 > > > > Frame 2: > > > > 0 0 0 0 0 > > 0 64412 0 64412 0 > > 0 64412 0 64412 64412 > > 64412 64412 64412 64412 64412 > > > > Frame 3+ > > > > 0 0 0 0 0 > > 0 64412 0 64412 0 > > 0 64412 0 64412 64412 > > 64412 64412 64412 64412 64412 > > > > Has anyone used Numeric for this kind of thing before? Any little > > quirk of array mathematics I could exploit? > > > > Or, should I shuffle this one to the back of the ideas folder for when > > I've (if ever) learn Cpp? > > > > Regards, > > > > Liam Clarke > > > > PS > > > > -- > > '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 > > > -- '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. From cyresse at gmail.com Wed Jan 5 05:02:06 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 5 05:02:10 2005 Subject: [Tutor] regex problem In-Reply-To: <20050104233917.GA29986@titan.spiretech.com> References: <20050104233917.GA29986@titan.spiretech.com> Message-ID: Hi Michael, Is a non regex way any help? I can think of a way that uses string methods - space=" " stringStuff="Stuff with multiple spaces" indexN = 0 ranges=[] while 1: try: indexN=stringStuff.index(space, indexN) if indexN+1 == space: indexT = indexN while 1: indexT += 1 if not indexT == " ": ranges.append((indexN, indexT)) break indexN=indexT +1 else: indexN += 1 except ValueError: ranges.reverse() for (low, high) in ranges: stringStuff.replace[stringStuff[low:high], space] HTH Liam Clarke On Tue, 4 Jan 2005 15:39:18 -0800, Michael Powe wrote: > Hello, > > I'm having erratic results with a regex. I'm hoping someone can > pinpoint the problem. > > This function removes HTML formatting codes from a text email that is > poorly exported -- it is supposed to be a text version of an HTML > mailing, but it's basically just a text version of the HTML page. I'm > not after anything elaborate, but it has gotten to be a bit of an > itch. ;-) > > def parseFile(inFile) : > import re > bSpace = re.compile("^ ") > multiSpace = re.compile(r"\s\s+") > nbsp = re.compile(r" ") > HTMLRegEx = > re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) > ",re.I) > > f = open(inFile,"r") > lines = f.readlines() > newLines = [] > for line in lines : > line = HTMLRegEx.sub(' ',line) > line = bSpace.sub('',line) > line = nbsp.sub(' ',line) > line = multiSpace.sub(' ',line) > newLines.append(line) > f.close() > return newLines > > Now, the main issue I'm looking at is with the multiSpace regex. When > applied, this removes some blank lines but not others. I don't want > it to remove any blank lines, just contiguous multiple spaces in a > line. > > BTB, this also illustrates a difference between python and perl -- in > perl, i can change "line" and it automatically changes the entry in > the array; this doesn't work in python. A bit annoying, actually. > ;-) > > Thanks for any help. If there's a better way to do this, I'm open to > suggestions on that regard, too. > > mp > _______________________________________________ > 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. From rmkrauter at yahoo.com Wed Jan 5 05:11:12 2005 From: rmkrauter at yahoo.com (Rich Krauter) Date: Wed Jan 5 05:11:17 2005 Subject: [Tutor] regex problem In-Reply-To: <20050104233917.GA29986@titan.spiretech.com> References: <20050104233917.GA29986@titan.spiretech.com> Message-ID: <41DB68E0.3070907@yahoo.com> Michael Powe wrote: > Hello, > > I'm having erratic results with a regex. I'm hoping someone can > pinpoint the problem. > > This function removes HTML formatting codes from a text email that is > poorly exported -- it is supposed to be a text version of an HTML > mailing, but it's basically just a text version of the HTML page. I'm > not after anything elaborate, but it has gotten to be a bit of an > itch. ;-) > > def parseFile(inFile) : > import re > bSpace = re.compile("^ ") > multiSpace = re.compile(r"\s\s+") > nbsp = re.compile(r" ") > HTMLRegEx = > re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) > ",re.I) > > f = open(inFile,"r") > lines = f.readlines() > newLines = [] > for line in lines : > line = HTMLRegEx.sub(' ',line) > line = bSpace.sub('',line) > line = nbsp.sub(' ',line) > line = multiSpace.sub(' ',line) > newLines.append(line) > f.close() > return newLines > > Now, the main issue I'm looking at is with the multiSpace regex. When > applied, this removes some blank lines but not others. I don't want > it to remove any blank lines, just contiguous multiple spaces in a > line. > Hi Michael, If you use '\s\s+', and a line has ' \n' (space then newline) at the end, the space and the newline will match and be substituted. If the line ends in 'some chars\n' or the line is just '\n', the newline will stay. An alternate approach might be to first get rid of any leading or trailing whitespace (including \r|\n), then get rid of 'internal' repeated space, with string methods. Parsing html using regexes is likely to break easily; HTMLParser is a better solution, but may it may seem more complicated at first. It may be worthwhile for you to look into that module; someone here would be able to help if necessary. Short of the HTMLParser approach, I would try to reduce the dependence on regexes, using string methods where you can. I would try something like this (untested) in your for loop to start: for line in lines: line = line.strip() line = line.replace(' ',' ') line = HTMLRegEx.sub(' ',line) line = ' '.join(line.split()) newLines.append(line) The only pattern left is HTMLRegEx. Using HTMLParser you could probably remove regexes completely. When I used perl for most things, I don't think I wrote a single script that didn't use regexes. Now, I use python for most things, and I don't think I have a single python module that imports re. Weird. > BTB, this also illustrates a difference between python and perl -- in > perl, i can change "line" and it automatically changes the entry in > the array; this doesn't work in python. A bit annoying, actually. > ;-) > I once had the same trouble with python - some features annoyed me because they weren't like perl. Now I have the reverse problem, only this time my annoyance is justified. :) > Thanks for any help. If there's a better way to do this, I'm open to > suggestions on that regard, too. > Good luck. Rich From kent37 at tds.net Wed Jan 5 05:26:06 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 5 05:26:10 2005 Subject: [Tutor] How to run a script file In-Reply-To: References: Message-ID: <41DB6C5E.9070901@tds.net> You might want to try IDLE. You can open your file in an editing window and run it from there. I don't know how to start IDLE on Linux though...you need to run Lib/idlelib/idle.pyw Kent Bernard Lebel wrote: > Hi, > > Sorry if I missed something obvious, but how do I execute a python > script file in the interpreter? I have "Using the Python Interpreter" in > the Python tutorial but not much is said... > > (this might be a lame quesiton but so far I always used either the > PythonWin interpreter wich has the Import function, or I ran Python code > in an application. Now I'm on Linux so I have to learn the hard way!) > > > Thanks > Bernard > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From dyoo at hkn.eecs.berkeley.edu Wed Jan 5 06:15:46 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 5 06:15:50 2005 Subject: [Tutor] regex problem In-Reply-To: <20050104233917.GA29986@titan.spiretech.com> Message-ID: On Tue, 4 Jan 2005, Michael Powe wrote: > def parseFile(inFile) : > import re > bSpace = re.compile("^ ") > multiSpace = re.compile(r"\s\s+") > nbsp = re.compile(r" ") > HTMLRegEx = > re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) > ",re.I) > > f = open(inFile,"r") > lines = f.readlines() > newLines = [] > for line in lines : > line = HTMLRegEx.sub(' ',line) > line = bSpace.sub('',line) > line = nbsp.sub(' ',line) > line = multiSpace.sub(' ',line) > newLines.append(line) > f.close() > return newLines > > Now, the main issue I'm looking at is with the multiSpace regex. When > applied, this removes some blank lines but not others. I don't want it > to remove any blank lines, just contiguous multiple spaces in a line. Hi Michael, Do you have an example of a file where this bug takes place? As far as I can tell, since the processing is being done line-by-line, the program shouldn't be losing any blank lines at all. Do you mean that the 'multiSpace' pattern is eating the line-terminating newlines? If you don't want it to do this, you can modify the pattern slightly. '\s' is defined to be this group of characters: '[ \t\n\r\f\v]' (from http://www.python.org/doc/lib/re-syntax.html) So we can adjust our pattern from: r"\s\s+" to r"[ \t\f\v][ \t\f\v]+" so that we don't capture newlines or carriage returns. Regular expressions have a brace operator for dealing with repetition: if we're looking for at least 2 or more of some thing 'x', we can say: x{2,} Another approach is to always rstrip() the newlines off, do the regex processing, and then put them back in at the end. There are some assumptions that the program makes about the HTML that you might need to be careful of. What does the program do if we pass it the following string? ### from StringIO import StringIO sampleFile = """

hello world!

""" ### Issues like these are already considered in the HTML parser modules in the Standard Library, so if you can use HTMLParser, I'd strongly recommend it. Good luck to you! From jfabiani at yolo.com Wed Jan 5 06:30:00 2005 From: jfabiani at yolo.com (John Fabiani) Date: Wed Jan 5 06:30:04 2005 Subject: [Tutor] Looking for a firebird interface Message-ID: <200501042130.00116.jfabiani@yolo.com> Hi, I'm using SUSE x86_64 Linux and unable to compile the KInterbasdb interface for Python. ?I'm wondering if anyone has it compiled on either a 32bit or 64bit linux. ?Of course I'm hoping someone is willing to post it. Or tell me where I might get it. ?Thanks in advance. ? John From alan.gauld at freenet.co.uk Wed Jan 5 08:30:30 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 08:30:47 2005 Subject: [Tutor] How to run a script file References: Message-ID: <021201c4f2f8$6fe16470$62bc8651@xp> > Sorry if I missed something obvious, but how do I execute a python > script file in the interpreter? I have "Using the Python Interpreter" in > the Python tutorial but not much is said... You can import a script at the >>> prompt as you would under Pythonwin. Or you can run IDLE much as you did Pythonwin. Or you can learn emacs and run python mode in that. Personally I just open a gvim window and a terminal and edit the code in gvim and run the program in the terminal... If I need to experiment I open a third window and run the >>> prompt there. Linux is the IDE... Alan G. PS I just got my own Linux box running again 4 months after moving house! I thought I'd try a Suse distro I got with Borlands Kylix and calamity - no python! Back to Mandrake I think. From alan.gauld at freenet.co.uk Wed Jan 5 08:33:54 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 08:33:13 2005 Subject: [Tutor] How to run a script file References: <200501042304.j04N4C8p015761@mail.morseintranet.com> <41DB27A6.10701@bernardlebel.com> Message-ID: <021b01c4f2f8$e9321040$62bc8651@xp> > Then I add my custom path to the sys.path list (because my user > permissions do not allow my to put anything in the Lib directory) and YOu should be able to create a personal startup script in your homedir. I've not done it but I'm sure I remember reading about it, and its pretty standard LInux practice. > then I try an > import /home/bernardl/python/myScript.py > but of course if fails as soon the first slash is read. If you've added the path to sys.path you should only need to do import myScript no .py and no path. Remember that import does NOT import a file it imports a module object (which just happens to correspond to a file with a similar name!) HTH Alan G. From alan.gauld at freenet.co.uk Wed Jan 5 08:37:58 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 08:37:07 2005 Subject: [Tutor] regex problem References: <20050104233917.GA29986@titan.spiretech.com> Message-ID: <022001c4f2f9$7a817720$62bc8651@xp> > This function removes HTML formatting codes from a text email Using regex to remove HTML is usually the wrong approach unless you can guarantee the format of the HTML in advance. The HTMLparser is usually better and simpler. I think theres an example in the module doc of converting HTML to plain text. HTH, Alan G. From alan.gauld at freenet.co.uk Wed Jan 5 08:43:31 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 08:42:35 2005 Subject: pickle juice and YAML (Re: [Tutor] dumping .pck files) References: <200501030457.j034v8We019029@lock.gotdns.org><00b501c4f17a$24e925e0$62bc8651@xp><200501030457.j034v8We019029@lock.gotdns.org><41D86CA6.21475.F9BC473@localhost><200501030457.j034v8We019029@lock.gotdns.org> <20050105023124.GG1938@feynman> Message-ID: <023801c4f2fa$40f3ecd0$62bc8651@xp> > Finally, another serialization format that is eminently readable is > YAML ("YAML Ain't Markup Language"). Besides Python, it works for > Ruby et al., can be used as an alternative to xml, etc. A new one on me but I'll take a klook, thanks for posting. Alan G. (Who hates XML almost as much as Java! :-) From alan.gauld at freenet.co.uk Wed Jan 5 08:48:35 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 08:47:40 2005 Subject: [Tutor] Array pointers References: <787687ba05010419363f9f85db@mail.gmail.com> Message-ID: <023f01c4f2fa$f68c9290$62bc8651@xp> > I want to move all the 1's move to the right, 1 index at a time, > preserving any spacing. So you want to insert a zero at the front and delete the first zero from the back? That doesn't require iterating over all the entries just enough to find the first zero... > [1,0,0,0,0,1,1,1,0,1,1,0,1,0] > ... > [0,0,0,0,0,0,0,1,1,1,1,1,1,1] And you can go straight to the last entry but counting the ones and building a new list, but that might not be acceptable for the game I dunno. Alan G. From jfabiani at yolo.com Wed Jan 5 09:11:44 2005 From: jfabiani at yolo.com (John Fabiani) Date: Wed Jan 5 09:11:53 2005 Subject: [Tutor] Looking for a firebird interface In-Reply-To: <200501042130.00116.jfabiani@yolo.com> References: <200501042130.00116.jfabiani@yolo.com> Message-ID: <200501050011.44832.jfabiani@yolo.com> Thanks all I fixed it. John On Tuesday 04 January 2005 21:30, John Fabiani wrote: > Hi, > I'm using SUSE x86_64 Linux and unable to compile the KInterbasdb interface > for Python. ?I'm wondering if anyone has it compiled on either a 32bit or > 64bit linux. ?Of course I'm hoping someone is willing to post it. Or tell > me where I might get it. ?Thanks in advance. > John > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at freenet.co.uk Wed Jan 5 10:19:49 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 10:18:59 2005 Subject: [Tutor] Array pointers References: <787687ba05010419363f9f85db@mail.gmail.com> <023f01c4f2fa$f68c9290$62bc8651@xp> Message-ID: <026a01c4f307$b64b10f0$62bc8651@xp> > > I want to move all the 1's move to the right, 1 index at a time, > > preserving any spacing. > > So you want to insert a zero at the front and delete the first > zero from the back? That doesn't require iterating over all > the entries just enough to find the first zero... Playing with this for a bit gives me: def shiftOnesRight(aList): try: aList.insert(0,0) index = -1 while aList[index] != 0: index -= 1 del(aList[index]) except IndexError: pass return aList Now the challenge is to figure out how often to shift. We need to know when all the ones are at the end. The answer comes from counting zeros in the initial list... count = theList.count(0) # iterates the whole list once for n in range(count): theList = shiftOnesRight(theList) # ~1/4 the list count times? So the total effect is only O(count*len/4 + 1), which is better than O(len*len). We should speed things up even more by caching the last index we reached between shifts and start there next time.... def shiftOnsRight(aList,index=-1) try: aList.insert(0,0) while aList[index] != 0: index -= 1 del(aList[index]) except IndexError: pass return index, aList count = theList.count(0) index = -1 for n in range(count): index, theList = shiftOnesRight(theList, index) HTH, Alan G. From davholla2002 at yahoo.co.uk Wed Jan 5 10:19:55 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Wed Jan 5 10:19:58 2005 Subject: [Tutor] Python on linux In-Reply-To: <20050104233927.067851E400E@bag.python.org> Message-ID: <20050105091955.35446.qmail@web25405.mail.ukl.yahoo.com> You can use idle while using linux, just type in idle from a command prompt ! --------------------------------- ALL-NEW Yahoo! Messenger - all new features - even more fun! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050105/dd6ce2aa/attachment.html From kent37 at tds.net Wed Jan 5 12:10:12 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 5 12:10:18 2005 Subject: pickle juice and YAML (Re: [Tutor] dumping .pck files) In-Reply-To: <023801c4f2fa$40f3ecd0$62bc8651@xp> References: <200501030457.j034v8We019029@lock.gotdns.org><00b501c4f17a$24e925e0$62bc8651@xp><200501030457.j034v8We019029@lock.gotdns.org><41D86CA6.21475.F9BC473@localhost><200501030457.j034v8We019029@lock.gotdns.org> <20050105023124.GG1938@feynman> <023801c4f2fa$40f3ecd0$62bc8651@xp> Message-ID: <41DBCB14.9090803@tds.net> Alan Gauld wrote: > (Who hates XML almost as much as Java! :-) IMO dom4j makes XML quite easy to use from Java and really pleasant in Jython. The integrated XPath support essentially gives you a query engine on your data. http://www.dom4j.org I have written about dom4j, Jython and XPath here: http://www.pycs.net/users/0000323/stories/12.html http://www.pycs.net/users/0000323/stories/2.html Kent (Who hates Java but has come to like XML) > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Wed Jan 5 12:33:32 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 5 12:33:41 2005 Subject: [Tutor] regex problem In-Reply-To: <20050104233917.GA29986@titan.spiretech.com> References: <20050104233917.GA29986@titan.spiretech.com> Message-ID: <41DBD08C.90901@tds.net> If you search comp.lang.python for 'convert html text', the top four results all have solutions for this problem including a reference to this cookbook recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52297 comp.lang.python can be found here: http://groups-beta.google.com/group/comp.lang.python?hl=en&lr=&ie=UTF-8&c2coff=1 Kent Michael Powe wrote: > Hello, > > I'm having erratic results with a regex. I'm hoping someone can > pinpoint the problem. > > This function removes HTML formatting codes from a text email that is > poorly exported -- it is supposed to be a text version of an HTML > mailing, but it's basically just a text version of the HTML page. I'm > not after anything elaborate, but it has gotten to be a bit of an > itch. ;-) > > def parseFile(inFile) : > import re > bSpace = re.compile("^ ") > multiSpace = re.compile(r"\s\s+") > nbsp = re.compile(r" ") > HTMLRegEx = > re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) > ",re.I) > > f = open(inFile,"r") > lines = f.readlines() > newLines = [] > for line in lines : > line = HTMLRegEx.sub(' ',line) > line = bSpace.sub('',line) > line = nbsp.sub(' ',line) > line = multiSpace.sub(' ',line) > newLines.append(line) > f.close() > return newLines > > Now, the main issue I'm looking at is with the multiSpace regex. When > applied, this removes some blank lines but not others. I don't want > it to remove any blank lines, just contiguous multiple spaces in a > line. > > BTB, this also illustrates a difference between python and perl -- in > perl, i can change "line" and it automatically changes the entry in > the array; this doesn't work in python. A bit annoying, actually. > ;-) > > Thanks for any help. If there's a better way to do this, I'm open to > suggestions on that regard, too. > > mp > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From python at bernardlebel.com Wed Jan 5 15:26:00 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Wed Jan 5 15:26:10 2005 Subject: [Tutor] How to run a script file Message-ID: Thanks everyone who answered, it's sorted now :-D Bernard From askoose at sandia.gov Wed Jan 5 16:15:52 2005 From: askoose at sandia.gov (Kooser, Ara S) Date: Wed Jan 5 16:16:20 2005 Subject: [Tutor] The Game of Life question Message-ID: This is most likely a silly question and me not understanding python enough. I am a mentor for some high school kids participating in a supercomputing challenge. My background in programming is F77 (yeah laugh it up) and I want the kids to learn python and use it for the challenge. They picked a project to model the flow of smallpox in a city and surroundings areas. So I saw the game of life and thought maybe they could modify it for use as a smallpox model. My question is when I run this code as is and execute the command to generate a world, I get the following error: Traceback (most recent call last): File "", line 1, in -toplevel- print_world(make_random_world(10, 10)) File "C:\Python23\gameoflife.py", line 12, in make_random_world world[i, j] = random.choice([LIVE, DEAD]) NameError: global name 'random' is not defined >>> Does "random" need to be defined after LIVE,DEAD or am I just missing something. I was trying to run this on my work computer which is a winXP machine running python 2.4. Thanks, Ara This is the part of the game of life program I am trying to get to run LIVE, DEAD = '*', '.' def make_random_world(M, N): """Constructs a new random game world of size MxN.""" world = {} for j in range(N): for i in range(M): world[i, j] = random.choice([LIVE, DEAD]) world['dimensions'] = (M, N) return world def print_world(world): """Prints out a string representation of a world.""" M, N = world['dimensions'] for j in range(N): for i in range(M): print world[i, j], print >>> print_world(make_random_world(10, 10)) From kent37 at tds.net Wed Jan 5 16:40:47 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 5 16:40:56 2005 Subject: [Tutor] The Game of Life question In-Reply-To: References: Message-ID: <41DC0A7F.4080200@tds.net> You need to include the line import random at the beginning of your program. This gives you access to the contents of the 'random' module such as random.choice(). This chapter of the tutorial talks about modules: http://docs.python.org/tut/node8.html Kent Kooser, Ara S wrote: > This is most likely a silly question and me not understanding python > enough. I am a mentor for some high school kids participating in a > supercomputing challenge. My background in programming is F77 (yeah > laugh it up) and I want the kids to learn python and use it for the > challenge. > They picked a project to model the flow of smallpox in a city and > surroundings areas. So I saw the game of life and thought maybe they > could modify it for use as a smallpox model. My question is when I run > this code as is and execute the command to generate a world, I get the > following error: > > Traceback (most recent call last): > File "", line 1, in -toplevel- > print_world(make_random_world(10, 10)) > File "C:\Python23\gameoflife.py", line 12, in make_random_world > world[i, j] = random.choice([LIVE, DEAD]) > NameError: global name 'random' is not defined > > > Does "random" need to be defined after LIVE,DEAD or am I just missing > something. I was trying to run this on my work computer which is a winXP > machine running python 2.4. > > Thanks, > Ara > > > > This is the part of the game of life program I am trying to get to run > > LIVE, DEAD = '*', '.' > > def make_random_world(M, N): > """Constructs a new random game world of size MxN.""" > world = {} > for j in range(N): > for i in range(M): > world[i, j] = random.choice([LIVE, DEAD]) > world['dimensions'] = (M, N) > return world > > def print_world(world): > """Prints out a string representation of a world.""" > M, N = world['dimensions'] > for j in range(N): > for i in range(M): > print world[i, j], > print > > >>>>print_world(make_random_world(10, 10)) > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From bvande at po-box.mcgill.ca Wed Jan 5 16:42:52 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Wed Jan 5 16:43:00 2005 Subject: [Tutor] The Game of Life question In-Reply-To: References: Message-ID: <41DC0AFC.9070908@po-box.mcgill.ca> Kooser, Ara S said unto the world upon 2005-01-05 10:15: > This is most likely a silly question and me not understanding python > enough. I am a mentor for some high school kids participating in a > supercomputing challenge. My background in programming is F77 (yeah > laugh it up) and I want the kids to learn python and use it for the > challenge. > They picked a project to model the flow of smallpox in a city and > surroundings areas. So I saw the game of life and thought maybe they > could modify it for use as a smallpox model. My question is when I run > this code as is and execute the command to generate a world, I get the > following error: > > Traceback (most recent call last): > File "", line 1, in -toplevel- > print_world(make_random_world(10, 10)) > File "C:\Python23\gameoflife.py", line 12, in make_random_world > world[i, j] = random.choice([LIVE, DEAD]) > NameError: global name 'random' is not defined > > > Does "random" need to be defined after LIVE,DEAD or am I just missing > something. I was trying to run this on my work computer which is a winXP > machine running python 2.4. > > Thanks, > Ara Hi Ara, Not an expert, but from the traceback, my guess is that you've not included the line import random in your script before the call to random.choice. Random and other modules need to be imported before they can be used. You can think of import loosely as doing two things: 1) adding a module to the available command set, and 2) running whatever top level code is contained in the module. There is a fairly strong convention to put all imports near the beginning of a module (usually after the module docstring and any legal kung-fu). Does that clear it up? Best, Brian vdB From untezcan at seyas.com.tr Wed Jan 5 16:33:42 2005 From: untezcan at seyas.com.tr (=?iso-8859-9?Q?=FCmit_tezcan?=) Date: Wed Jan 5 16:43:32 2005 Subject: [Tutor] Lottery simulation Message-ID: <20050105153130.COQV12698.smtp3@Umit> Dear Tutor, I am new to python and only created very simple programs so far. I would like to simulate a lottery draw for 10 participants and run it for 52 weeks. The lottery numbers would have a range of 1 to 99. Each person would have just one number (random) and play for 52 weeks. Are there any starting ideas for me to get going on this project either thru the tutor or searching for snippets already created by fellow programmers?? Thanks a lot in advance for all the help, Regards, ********************************************** ?mit N Tezcan SEYA?- European Projects Coordinator Tel : +90-212-2330920 (Ext.153) Fax : +90-212-233-0936 Mob : +90-533-337-7352 E-mail: untezcan@seyas.com.tr www : seyas.com.tr ********************************************** This email/fax message is for the sole use of the intended recipient(s) and may contain confidential and privileged information, or trade secrets. Any unauthorized review, use, disclosure or distribution of this email/fax is prohibited. If you are not the intended recipient, please contact the sender by email/fax and destroy all paper and electronic copies of the original message. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050105/0728a93e/attachment.htm From maxnoel_fr at yahoo.fr Wed Jan 5 16:49:29 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 5 16:49:47 2005 Subject: [Tutor] Lottery simulation In-Reply-To: <20050105153130.COQV12698.smtp3@Umit> References: <20050105153130.COQV12698.smtp3@Umit> Message-ID: <62CCB3A5-5F31-11D9-9706-000393CBC88E@yahoo.fr> On Jan 5, 2005, at 16:33, ?mit tezcan wrote: > Are there any starting ideas for me to get going on this project > either thru the tutor or searching for snippets already created by > fellow programmers?? That's probably obvious, but you should proceed as follows: - Generate a number for each person. - For each week, generate a random number (the draw) and compare it to each person's number. - If there is a match, do something. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From orion_val at 163.com Wed Jan 5 18:07:43 2005 From: orion_val at 163.com (=?gb2312?q?=C9=F2=BD=E0=D4=AA?=) Date: Wed Jan 5 18:07:55 2005 Subject: [Tutor] Array pointers In-Reply-To: References: <787687ba05010419363f9f85db@mail.gmail.com> Message-ID: <200501060107.43589.orion_val@163.com> This code will work. a=3D[1,0,0,0,0,1,1,1,0,1,1,0,1,0] shorta=3Da[:] while 0 in shorta: shorta.reverse() shorta.remove(0) shorta.reverse() print [0,]*(len(a)-len(shorta))+shorta result=3D[0,]*(len(a)-len(shorta))+shorta Juan Shen =D4=DA 2005=C4=EA1=D4=C25=C8=D5 =D0=C7=C6=DA=C8=FD 11:44=A3=ACLiam Clarke = =D0=B4=B5=C0=A3=BA > Sorry rephrase - > > So I have a list - > > [0,0,0,0,0,1,1,1,0,1,1,0,1,0] > > I want to move all the 1's move to the right, 1 index at a time, > preserving any spacing. > > i.e. > > [1,0,0,0,0,1,1,1,0,1,1,0,1,0] > > [0,1,0,0,0,0,1,1,1,0,1,1,0,1] > > [0,0,1,0,0,0,0,1,1,1,0,1,1,1] > > [0,0,0,1,0,0,0,0,1,1,1,1,1,1] > > [0,0,0,0,1,0,0,0,1,1,1,1,1,1] > > [0,0,0,0,0,1,0,0,1,1,1,1,1,1] > > [0,0,0,0,0,0,1,0,1,1,1,1,1,1] > > [0,0,0,0,0,0,0,1,1,1,1,1,1,1] > > > Now, I have a whole list of these lists.... and I create an array of them. > Is there a way to apply these changes wholescale without a large > calculation time? > > I'm starting to think I'm going to have to go Cpp for this kind of > direct pixel tweaking stuff. > (640x480 (let alone 1024x768) is a lot of pixels to run through a for... > loop) > > So yeah, anyone had this before? > > Regards, > > Liam Clarke > From davholla2002 at yahoo.co.uk Wed Jan 5 18:19:34 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Wed Jan 5 18:19:36 2005 Subject: [Tutor] games written in jython for a web page Message-ID: <20050105171934.84527.qmail@web25408.mail.ukl.yahoo.com> Does anyone know about tutorials (or open source projects) about writing in games in pure python (ie without using pygame). The reason is because I wrote a game using pygame but I would like to convert it to a game that can be played in a webpage as a Jython, (like a Java game) of course this is harder so I wonder if anyone has any advice. --------------------------------- ALL-NEW Yahoo! Messenger - all new features - even more fun! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050105/e3031d0f/attachment.html From tegmine at gmail.com Wed Jan 5 18:53:00 2005 From: tegmine at gmail.com (Luis N) Date: Wed Jan 5 18:53:03 2005 Subject: [Tutor] CGI and cookies. Message-ID: <77bfa81a05010509532846dadc@mail.gmail.com> When the script begins with main(f), it gets a KeyError and goes to the login page, but when the form data is submitted it returns a 404. Am I not setting/getting the cookie properly? Absolutely nothing is printed until zptIO is called. import os import Cookie from spanishlabs.conf import * from spanishlabs.zpt import * from spanishlabs.sql import * def main(f): try: c = os.environ["HTTP_COOKIE"] except KeyError: login(f, fail=False) else: fetchCookie(f) def login(f, fail): if f: authenticate(f) zpt = 'login.zpt' context = {'here':Render(fail)} zptIO(zpt, context) def authenticate(f): if f.has_key('author') and f.has_key('password'): author = f.['author'].value password = f.['password'].value q = Users.select(AND(Users.q.author==author, Users.q.password==password)) if len(list(q))>0: setCookie(f, author) else: login(f, fail=True) def chapters(f, author): if f.has_key('event') and f.has_key('chapter'): event = f['event'].value chapter = f['chapter'].value q = Chapters.select(Chapters.q.chapter==chapter) if event == 'create' and len(list(q))>0: exists = True else: create = chapter if event == 'edit' and len(list(q))>0: edit = chapter zpt = 'chapters.zpt' context = {'here':Render(exists=None, create=None, edit=None)} zptIO(zpt, context) def setCookie(f, author): c1 = Cookie.SimpleCookie() c1['author'] = author c1['author']['max-age'] = 7200 c1['author']['expires'] = 7200 c1['author']['version'] = 1 print c1 chapters(f, author) def fetchCookie(f): c2 = Cookie.SimpleCookie() c2.load(os.environ["HTTP_COOKIE"]) author = c2['author'].value chapters(f, author) def zptIO(zpt, context): pt = PageTemplate() template = open(os.path.join(templates, zpt)) pt.write(template.read()) template.close() print 'Content-type: text/html\n\n' print pt(context=context) A previous message I sent to the list about **kwds mostly makes sense to me now. The missing dot was a typo. From bill at celestial.net Wed Jan 5 19:17:12 2005 From: bill at celestial.net (Bill Campbell) Date: Wed Jan 5 19:17:17 2005 Subject: [Tutor] (OT) How to run a script file In-Reply-To: <021201c4f2f8$6fe16470$62bc8651@xp> References: <021201c4f2f8$6fe16470$62bc8651@xp> Message-ID: <20050105181712.GC50269@alexis.mi.celestial.com> On Wed, Jan 05, 2005, Alan Gauld wrote: >> Sorry if I missed something obvious, but how do I execute a python >> script file in the interpreter? I have "Using the Python >Interpreter" in >> the Python tutorial but not much is said... > >You can import a script at the >>> prompt as you would under >Pythonwin. >Or you can run IDLE much as you did Pythonwin. >Or you can learn emacs and run python mode in that. > >Personally I just open a gvim window and a terminal and edit the >code in gvim and run the program in the terminal... If I need >to experiment I open a third window and run the >>> prompt there. >Linux is the IDE... > >Alan G. > >PS >I just got my own Linux box running again 4 months after >moving house! I thought I'd try a Suse distro I got with >Borlands Kylix and calamity - no python! >Back to Mandrake I think. I have yet to see a SuSE distribution without python although you may have to install it from using yast2. FWIW, I would strongly recommend the SuSE Professional rather than the Personal if you're going to be doing any serious development work. Bill -- INTERNET: bill@Celestial.COM Bill Campbell; Celestial Software LLC UUCP: camco!bill PO Box 820; 6641 E. Mercer Way FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 URL: http://www.celestial.com/ Manual, n.: A unit of documentation. There are always three or more on a given item. One is on the shelf; someone has the others. The information you need is in the others. -- Ray Simard From alan.gauld at freenet.co.uk Wed Jan 5 19:18:30 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 19:18:44 2005 Subject: pickle juice and YAML (Re: [Tutor] dumping .pck files) References: <200501030457.j034v8We019029@lock.gotdns.org><00b501c4f17a$24e925e0$62bc8651@xp><200501030457.j034v8We019029@lock.gotdns.org><41D86CA6.21475.F9BC473@localhost><200501030457.j034v8We019029@lock.gotdns.org> <20050105023124.GG1938@feynman><023801c4f2fa$40f3ecd0$62bc8651@xp> <41DBCB14.9090803@tds.net> Message-ID: <001201c4f352$f6434c90$60a08851@xp> > Alan Gauld wrote: > > (Who hates XML almost as much as Java! :-) > > IMO dom4j makes XML quite easy to use from Java and really pleasant > in Jython. The integrated XPath support essentially gives you a > query engine on your data. > http://www.dom4j.org > Kent > (Who hates Java but has come to like XML) My problems stem from the massive increase in bandwidth and CPU resource required by XML messages. Actually I don't mind XML for what it was originally intended: a cross platform self-describing data exchange mechamnism. Its the atrtempt to make XML into the foundation of the computing infrastructure I hate, its quite obvious the people pushing XML are either ignorant of the cost implications on the network and hardware or sell networks and hardware, or both! If you compare the bandwidth requirements of a binary RPC call (such as Java RMI or CORBA) they are about 1-2 *orders of magnitude* less than the equivalent SOAP web service! If your 2MB T1/E1 WAN is struggling to cope with the HTML overload now how much will an upgrade to 34MB ATM infrastructure needed for Web Service integration cost?!! You occasionally see the CPU factor mentioned (again an order of magnitude increase in CPU power) but I don't think I've ever seen a mention of the bandwidth impacts. New CPUs are relatively cheap compared to networks... And as for your work-from-home DSL users forget it! XML: A technology devised for developers by developers - and paid for by users. End of rant! :-) Alan G. From michael_strecker at moevy.net Wed Jan 5 19:42:07 2005 From: michael_strecker at moevy.net (michael) Date: Wed Jan 5 19:32:18 2005 Subject: [Tutor] German Totorial!?! Message-ID: <1104950527.13710.6.camel@localhost.localdomain> Hello, Im a German peaople whou would learn Python. But I cant find a german tutorial. So you know a German Tutorial? Daer Michael From ARobert at MFS.com Wed Jan 5 19:39:46 2005 From: ARobert at MFS.com (Robert, Andrew) Date: Wed Jan 5 19:41:34 2005 Subject: [Tutor] German Totorial!?! Message-ID: <968452DD78695147AA4A369C3DF9E40A027A2280@BOSMAILBOX3.corp.mfs.com> How about: http://starship.python.net/crew/gherman/publications/tut-de/ Thank you, Andrew Robert Systems Architect Information Technology - OpenVMS Massachusetts Financial Services Phone: 617-954-5882 Pager: 781-764-7321 E-mail: arobert@mfs.com Linux User Number: #201204 -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of michael Sent: Wednesday, January 05, 2005 1:42 PM To: tutor@python.org Subject: [Tutor] German Totorial!?! Hello, Im a German peaople whou would learn Python. But I cant find a german tutorial. So you know a German Tutorial? Daer Michael _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor "MFS Relay Service" made the following annotations on 01/05/2005 01:46:36 PM ------------------------------------------------------------------------------ This email communication and any attachments may contain proprietary, confidential, or privileged information. If you are not the intended recipient, you are hereby notified that you have received this email in error and that any review, disclosure, dissemination, distribution or copying of it or its contents is prohibited. The sender does not waive confidentiality or any privilege by mistransmission. If you have received this email in error, please notify the sender immediately, delete this email, and destroy all copies and any attachments. ============================================================================== From michael at trollope.org Wed Jan 5 20:20:49 2005 From: michael at trollope.org (Michael Powe) Date: Wed Jan 5 20:20:53 2005 Subject: [Tutor] regex problem In-Reply-To: References: <20050104233917.GA29986@titan.spiretech.com> Message-ID: <20050105192049.GC6430@titan.spiretech.com> On Tue, Jan 04, 2005 at 09:15:46PM -0800, Danny Yoo wrote: > > > On Tue, 4 Jan 2005, Michael Powe wrote: > > > def parseFile(inFile) : > > import re > > bSpace = re.compile("^ ") > > multiSpace = re.compile(r"\s\s+") > > nbsp = re.compile(r" ") > > HTMLRegEx = > > re.compile(r"(<|<)/?((!--.*--)|(STYLE.*STYLE)|(P|BR|b|STRONG))/?(>|>) > > ",re.I) > > > > f = open(inFile,"r") > > lines = f.readlines() > > newLines = [] > > for line in lines : > > line = HTMLRegEx.sub(' ',line) > > line = bSpace.sub('',line) > > line = nbsp.sub(' ',line) > > line = multiSpace.sub(' ',line) > > newLines.append(line) > > f.close() > > return newLines > > > > Now, the main issue I'm looking at is with the multiSpace regex. When > > applied, this removes some blank lines but not others. I don't want it > > to remove any blank lines, just contiguous multiple spaces in a line. > > > Hi Michael, > > Do you have an example of a file where this bug takes place? As far as I > can tell, since the processing is being done line-by-line, the program > shouldn't be losing any blank lines at all. That is what I thought. And the effect is erratic, it removes some but not all empty lines. > Do you mean that the 'multiSpace' pattern is eating the line-terminating > newlines? If you don't want it to do this, you can modify the pattern > slightly. '\s' is defined to be this group of characters: > > '[ \t\n\r\f\v]' > > (from http://www.python.org/doc/lib/re-syntax.html) > > So we can adjust our pattern from: > > r"\s\s+" > > to > > r"[ \t\f\v][ \t\f\v]+" > > so that we don't capture newlines or carriage returns. Regular > expressions have a brace operator for dealing with repetition: > if we're looking for at least 2 or more > of some thing 'x', we can say: I will take a look at this option. Thanks. mp From michael at trollope.org Wed Jan 5 20:25:05 2005 From: michael at trollope.org (Michael Powe) Date: Wed Jan 5 20:25:09 2005 Subject: [Tutor] regex problem In-Reply-To: <022001c4f2f9$7a817720$62bc8651@xp> References: <20050104233917.GA29986@titan.spiretech.com> <022001c4f2f9$7a817720$62bc8651@xp> Message-ID: <20050105192505.GD6430@titan.spiretech.com> On Wed, Jan 05, 2005 at 07:37:58AM -0000, Alan Gauld wrote: > > This function removes HTML formatting codes from a text email > Using regex to remove HTML is usually the wrong approach unless > you can guarantee the format of the HTML in advance. The > HTMLparser is usually better and simpler. I think theres an example > in the module doc of converting HTML to plain text. Thanks. This is one of those projects I've had in mind for a long time, decided it was a good way to learn some python. I will look at the HTMLParser module. But then once I get started on one of these projects, it has a way of taking over. ;-) mp From michael at trollope.org Wed Jan 5 20:26:42 2005 From: michael at trollope.org (Michael Powe) Date: Wed Jan 5 20:26:47 2005 Subject: [Tutor] regex problem In-Reply-To: <41DBD08C.90901@tds.net> References: <20050104233917.GA29986@titan.spiretech.com> <41DBD08C.90901@tds.net> Message-ID: <20050105192642.GE6430@titan.spiretech.com> On Wed, Jan 05, 2005 at 06:33:32AM -0500, Kent Johnson wrote: > If you search comp.lang.python for 'convert html text', the top four > results all have solutions for this problem including a reference to this > cookbook recipe: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52297 > > comp.lang.python can be found here: > http://groups-beta.google.com/group/comp.lang.python?hl=en&lr=&ie=UTF-8&c2coff=1 Shame on me, I have to get back into that habit. I will check these references, thanks. mp From lumbricus at gmx.net Wed Jan 5 21:07:27 2005 From: lumbricus at gmx.net (=?ISO-8859-1?Q?=22J=F6rg_W=F6lke=22?=) Date: Wed Jan 5 21:07:29 2005 Subject: [Tutor] Array pointers References: <200501060107.43589.orion_val@163.com> Message-ID: <6102.1104955647@www19.gmx.net> Hello! > > I want to move all the 1's move to the right, 1 index at a time, > > preserving any spacing. [ snip ] > > I'm starting to think I'm going to have to go Cpp for this kind of > > direct pixel tweaking stuff. > > (640x480 (let alone 1024x768) is a lot of pixels to run through a for... > > loop) Pixels - just ones and zeroes? Pack them as integers and apply the right shift operator: i>>=1 > > So yeah, anyone had this before? Some might have ;-) > > Regards, > > > > Liam Clarke HTH and Greetings, J"o! -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realit?t. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realit?t zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) +++ Sparen Sie mit GMX DSL +++ http://www.gmx.net/de/go/dsl AKTION f?r Wechsler: DSL-Tarife ab 3,99 EUR/Monat + Startguthaben From alan.gauld at freenet.co.uk Wed Jan 5 23:07:13 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 23:07:11 2005 Subject: [Tutor] (OT) How to run a script file References: <021201c4f2f8$6fe16470$62bc8651@xp> <20050105181712.GC50269@alexis.mi.celestial.com> Message-ID: <004501c4f372$e965ca00$60a08851@xp> > >I just got my own Linux box running again 4 months after > >moving house! I thought I'd try a Suse distro I got with > >Borlands Kylix and calamity - no python! > >Back to Mandrake I think. > > I have yet to see a SuSE distribution without python although you may have > to install it from using yast2. I think this is a non standard one Borland used just for those who wanted to use Kylix. It seems to be missing quite a few things I'm used to seeing - like LaTeX and Jove... I'll go back to my old faithful Mandrake when I get the time, Suse was just a disk I had handy. Ultimately I only want to use Linux as a server, in fact it won't even have a monitor attached, telnet and an xserver (cygwin) from the PC will suffice. At least I know the new motherboard I fitted works! :-) Alan G. From alan.gauld at freenet.co.uk Wed Jan 5 23:09:09 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 23:10:38 2005 Subject: [Tutor] German Totorial!?! References: <1104950527.13710.6.camel@localhost.localdomain> Message-ID: <004c01c4f373$2e98c910$60a08851@xp> > So you know a German Tutorial? There is a German translation of my tutorial and a whole bunch of dedicated Python web sites in German (I know because lots of them have links to my site and they show up on my web logs...)Try Googling for "python program german" HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld/german/ From alan.gauld at freenet.co.uk Wed Jan 5 23:13:55 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 5 23:13:48 2005 Subject: [Tutor] regex problem References: <20050104233917.GA29986@titan.spiretech.com><022001c4f2f9$7a817720$62bc8651@xp> <20050105192505.GD6430@titan.spiretech.com> Message-ID: <005501c4f373$d9715b90$60a08851@xp> > > Using regex to remove HTML is usually the wrong approach unless > > Thanks. This is one of those projects I've had in mind for a long > time, decided it was a good way to learn some python. It's a good way to write increasingly complex regex! Basically because HTML is recursive in nature it is almost impossible to reliably use regex to parse HTML files. (The latest regex syntax can cope with recursion but its horribly complicated) So unless you accept the limitations of the method you may well become more frustrated by the regex stuff than you become experienced in Python. Alan G. "When all you have is a hammer everything looks like a nail" From singingxduck at gmail.com Wed Jan 5 23:23:40 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Wed Jan 5 23:23:43 2005 Subject: [Tutor] Array pointers In-Reply-To: References: <787687ba05010419363f9f85db@mail.gmail.com> Message-ID: <3449428f0501051423781e965f@mail.gmail.com> I don't know about a large calculation time, but this seems to work: >>> def rightshift(a): ia = a[:] for i in range(len(ia)-1,0,-1): if ia[i-1] == 1 and ia[i]!=1: ia[i]=1 ia[i-1]=0 return ia >>> l = [1,0,0,0,0,1,1,1,0,1,1,0,1,0] >>> while l != rightshift(l): l = rightshift(l) print l [0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1] [0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1] [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1] [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 02:51:47 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 02:51:56 2005 Subject: [Tutor] The Game of Life In-Reply-To: <41D92EAC.1060905@tds.net> Message-ID: > > Just as a warning, none of what I'm going to code here is original at > > all: I'm rehashing a main idea off of a paper called "Using the Game > > of Life to Introduce Freshman Students to the Power and Elegance of > > Design Patterns": > > > > http://portal.acm.org/citation.cfm?id=1035292.1028706 > > Uh, with all respect Danny, this is a nice example of the Command > pattern, but to me this example would better be called "Using the Power > and Elegance of Design Patterns to Complicate the Game of Life". Hi Kent, Yeah, in retrospect, I see that the design patterns there might be overkill. There seems to be a fashionable push to introduce patterns early on in computer science education, perhaps because they are easy to put in as test questions. But despite this, I do think that there are some patterns that are worth seeing, even if they are in unrealistic toy situations. I got a kick out of seeing how 'Command' was applied in the Life example, because it shows that we can store active actions as data. > Why not just build a new world with the values of the next generation in > it, and return that from apply_next_generation? That also works, but it doesn't fit the function's description. The example that I adapted originally wanted a function that mutated the previous generation, so that's what I stuck with. > The copying is not needed at all and the command pattern is introduced > to solve a non-problem. [code cut] > The caller would have to change slightly to accept the new world > returned from the function, but there is no unnecessary copying. Sure. I didn't think of this before, but the two approaches can reflect different ways that people can represent data that changes through time: o. Making changes on a "scratch-space" copy of the data. o. Maintaining a "diff" change log. The approach with the Command pattern seems close to a diff-oriented view of applying changes to a model. If the Life example sucked, don't blame me too badly: I'm just the translator. *grin* Talk to you later! From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 08:20:31 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 08:20:36 2005 Subject: [Tutor] The Game of Life question In-Reply-To: Message-ID: On Wed, 5 Jan 2005, Kooser, Ara S wrote: > They picked a project to model the flow of smallpox in a city and > surroundings areas. So I saw the game of life and thought maybe they > could modify it for use as a smallpox model. Hi Ara, Oh! My apologies for not posting the file as a complete example! Here, let me just link it so that you don't have to type it or cut-and-paste it all out manually: http://hkn.eecs.berkeley.edu/~dyoo/python/life.py Please note that I wrote that code in a hacking session, so it might not make complete sense! *grin* I would hate to inflict it on students without having the code cleaned up a bit first. I'm not sure how easily my Life example can be adopted to simulate disease transmission. Let me do some research... Ok, there's a paper on epidemiology and cellular automata here: http://arxiv.org/abs/nlin.CG/0403035 Someone appears to have written a disease model in StarLogo here: http://biology.wsc.ma.edu/biology/courses/concepts/labs/epidemiology/ where they refer to a program called HBepidemic; it might be interesting to see if we can get the source code to HBepidemic and translate it into Python. Whoa! If you have Mathematica, take a look at: http://www.brynmawr.edu/Acads/Chem/Chem321mf/index521.html Workshop 6 on that page appears to have a cellular automaton example that models an epidemic, and sounds really interesting! Their worksheet also has the full Mathematica code needed to get the model working. I'm a Mathematica newbie, but if you want, I think I can figure out how their code works. Best of wishes to you! From wegster at mindcore.net Thu Jan 6 09:15:02 2005 From: wegster at mindcore.net (Scott W) Date: Thu Jan 6 09:15:08 2005 Subject: [Tutor] replacement for constants from other languages in Python? Message-ID: <41DCF386.9060904@mindcore.net> Hey all, I've done the usual googling, checked the Learning Python book and did some list searches, to no avail as of yet. I'm _very_ used to using C style constants (preprocessor #define directives) or C++ const keyword style, for a variety of reasons. I've yet to see anything covering 'how to work around the lack of constants in Python'...can anyone point me in the right direction here? A few examples/reasons for use: The 'need' to define a global constant in an imported module, for example- (I know about sys.version_info, but it doesn't exist in 1.5.2...don't ask ;-) I also know this could be handled via a class, but what is the equivalent of the following snippets? Not so interested in style comments (I am, but not on these/this thread ;-) as much as other ways to do this.. 1. ourversion.py: import sys MINVERSION = 1.5 def checkVersion(): """ used as an evil hack because 1.5.2 doesn't have sys.version_info """ # Not sure why, but declaring MINVERSION as repr(foo) in the # first place doesn't work, something about python type handling # I'm sure.. return repr(MINVERSION) >= repr(getVersion()) def getVersion(): return sys.version[0:3] if repr(getVersion() < 2.0) # boo, we have no builtin bool global True global False True = 1 False = 0 funkyScopeAndConstants.py: import ourversion.py import sys import os import ... ... if someOtherModule.property = True # do something You get the point. The other oddity is without being able to define a 'real' constant, as in #DEFINE MINVERSION 1.5, the scope of MINVERSION (and True/False) even using the global keyword still uses the ocal file's namespace. I don't want to debate the merits of using globals...most people that claim they never use any in other languages _still_ use constants in header files, which is the purpose I'd like to be able to do generally....not to mention the fact that I really am _not_ thrilled with the use of string literals typed in each time in code (yes, quick and dirty code but still) I see seems to be 'OK' in python for the use of comparisons...opposed to something like if(strncmp(strVal, CONSTANT_STRING_VAL_LIKE_HTTP_ACCEPT_ENCODING_HEADER, strlen(strVal) { do_something(); } or if(floatVal > PI) { do_something() } ok, hopefully that's explaining some of why I'd like a 'constant equivalent' as well as a question on global scoping/python namespaces. A last question would also be if the equivalent of __FILE__ and __LINE__ macros exist? Thanks, Scott From kent37 at tds.net Thu Jan 6 12:06:50 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 6 12:06:53 2005 Subject: [Tutor] The Game of Life In-Reply-To: References: Message-ID: <41DD1BCA.1090403@tds.net> Danny Yoo wrote: > There seems to be a fashionable push to introduce patterns early on in > computer science education, perhaps because they are easy to put in as > test questions. > > But despite this, I do think that there are some patterns that are worth > seeing, even if they are in unrealistic toy situations. I got a kick out > of seeing how 'Command' was applied in the Life example, because it shows > that we can store active actions as data. I agree that it is helpful to understand design patterns. It's also helpful to understand that applying design patterns doesn't always yield the simplest solution to a problem :-) >>Why not just build a new world with the values of the next generation in >>it, and return that from apply_next_generation? > > > That also works, but it doesn't fit the function's description. The > example that I adapted originally wanted a function that mutated the > previous generation, so that's what I stuck with. OK. I didn't have the spec available. But you can still do it with just one copy, then generate the world back into the original world. For a class problem you might have a firm requirement of a mutating method but in the real world you can often adjust your design to accomodate a more efficient algorithm. > If the Life example sucked, don't blame me too badly: I'm just the > translator. *grin* I hope this is not representative of CS education today. My subjective impression is that a lot of Java software suffers from overengineering of this sort. The Python world is refreshingly free of this. I think a lot of the difference may be due to the better tools available in Python, especially first-class functions. But there may also be a cultural bias toward heavier solutions in the Java world. Kent > > > Talk to you later! > > From kent37 at tds.net Thu Jan 6 12:50:40 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 6 12:50:45 2005 Subject: [Tutor] replacement for constants from other languages in Python? In-Reply-To: <41DCF386.9060904@mindcore.net> References: <41DCF386.9060904@mindcore.net> Message-ID: <41DD2610.8050801@tds.net> Scott W wrote: > The 'need' to define a global constant in an imported module, for > example- (I know about sys.version_info, but it doesn't exist in > 1.5.2...don't ask ;-) I also know this could be handled via a class, > but what is the equivalent of the following snippets? Not so interested > in style comments (I am, but not on these/this thread ;-) as much as > other ways to do this.. > > 1. > ourversion.py: > > import sys > > MINVERSION = 1.5 This is the usual Python way. Python's approach generally is 'treat your users like adults', i.e. give them the information needed to make sensible decisions, but don't try to keep them from doing something you think is stupid. Putting the name in upper case gives the information that this is intended to be a constant. > > def checkVersion(): > """ used as an evil hack because 1.5.2 doesn't have > sys.version_info > """ > > # Not sure why, but declaring MINVERSION as repr(foo) in the > # first place doesn't work, something about python type handling > # I'm sure.. MINVERSION = repr(1.5) should work just fine. It will give the same result as the more readable MINVERSION = '1.5' I'm not sure why you are using repr() so much. > return repr(MINVERSION) >= repr(getVersion()) > > def getVersion(): > return sys.version[0:3] > > if repr(getVersion() < 2.0) This will certainly not do what you want for two reasons. - getVersion() returns a string, you are comparing it to a float which will not give a meaningful result. - In Python < 2.3 the result of the comparison will be an integer 0 or 1. repr() converts this to a *string* '0' or '1' which will *always* evaluate as True!! > # boo, we have no builtin bool > global True > global False > True = 1 > False = 0 Rather than testing the version, you can test directly to see whether True and False are defined. If not, you can add them to the __builtin__ module and they will be globally available. Here is one way: import __builtin__ if not __builtin__.hasattr('True'): __builtin__.True = 1 if not __builtin__.hasattr('False'): __builtin__.False = 0 This suggestion is taken from this thread on comp.lang.python: http://tinyurl.com/46me3 Note that adding names to __builtin__ is NOT recommended in general! Don't use this to create your own global constants! It is OK in this case because to duplicate Python 2.3 behaviour you need a true global. > The other oddity is without being able to define a 'real' constant, as > in #DEFINE MINVERSION 1.5, > > the scope of MINVERSION (and True/False) even using the global keyword > still uses the ocal file's namespace. I don't want to debate the merits > of using globals...most people that claim they never use any in other > languages _still_ use constants in header files, which is the purpose > I'd like to be able to do generally The usual Python solution is to make a module containing constant definitions and import it where you need them. # Constants.py MINVERSION = 1.5 # Client.py import Constants if (Constants.MINVERSION == 1.5): ... or, if you want to import all the constants directly, # Client2.py from Constants import * if (MINVERSION == 1.5): ... > > A last question would also be if the equivalent of __FILE__ and __LINE__ > macros exist? See this recipe and the one linked to in its discussion: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062 Kent > > Thanks, > > Scott > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From wegster at mindcore.net Thu Jan 6 13:50:38 2005 From: wegster at mindcore.net (Scott W) Date: Thu Jan 6 13:50:47 2005 Subject: [Tutor] replacement for constants from other languages in Python? In-Reply-To: <41DD2610.8050801@tds.net> References: <41DCF386.9060904@mindcore.net> <41DD2610.8050801@tds.net> Message-ID: <41DD341E.1080707@mindcore.net> Kent Johnson wrote: > Scott W wrote: > >> The 'need' to define a global constant in an imported module, for >> example- (I know about sys.version_info, but it doesn't exist in >> 1.5.2...don't ask ;-) I also know this could be handled via a class, >> but what is the equivalent of the following snippets? Not so >> interested in style comments (I am, but not on these/this thread ;-) >> as much as other ways to do this.. >> >> 1. >> ourversion.py: >> >> import sys >> >> MINVERSION = 1.5 > > > This is the usual Python way. Python's approach generally is 'treat your > users like adults', i.e. give them the information needed to make > sensible decisions, but don't try to keep them from doing something you > think is stupid. Putting the name in upper case gives the information > that this is intended to be a constant. Sure, if I can't make it a true constant value, might as well make it 'obvious' ;-) >> >> def checkVersion(): >> """ used as an evil hack because 1.5.2 doesn't have >> sys.version_info >> """ >> >> # Not sure why, but declaring MINVERSION as repr(foo) in the >> # first place doesn't work, something about python type handling >> # I'm sure.. > > > MINVERSION = repr(1.5) > should work just fine. It will give the same result as the more readable > MINVERSION = '1.5' Ok, this would make a bit more sense RE: repr()- in one of the resources I found, it seemed to state that repr(x) was converting x into a numeric representation, ala atoi() and friends. Obviously, this is the opposite of what I'd actually wanted. The sample snippets I put in had a duplicated repr() if not more than one, which wasn't doing what I expected anyways (although at which point I was essentially comparing strings of similar length, getting 'a' result, just not the intended one ;-) > I'm not sure why you are using repr() so much. > >> return repr(MINVERSION) >= repr(getVersion()) >> >> def getVersion(): >> return sys.version[0:3] >> >> if repr(getVersion() < 2.0) > > > This will certainly not do what you want for two reasons. > - getVersion() returns a string, you are comparing it to a float which > will not give a meaningful result. > - In Python < 2.3 the result of the comparison will be an integer 0 or > 1. repr() converts this to a *string* '0' or '1' which will *always* > evaluate as True!! Yep, see above...wish I knew where I saw that 'explanation' of repr() and str() >> # boo, we have no builtin bool >> global True [snip] > Rather than testing the version, you can test directly to see whether > True and False are defined. If not, you can add them to the __builtin__ > module and they will be globally available. Here is one way: > > import __builtin__ > if not __builtin__.hasattr('True'): > __builtin__.True = 1 > if not __builtin__.hasattr('False'): > __builtin__.False = 0 OK, that's helpful. I can see the advantages of several of python's mechanisms, such as dir() as well as treating everything like an 'interactive object to determine existing methods and attributes (dir() and your example of hasattr(). Pretty cool...now to go from libc/POSIX to an entirely new and (mostly) different set of core libs and functionality.. ;-) Actually, that IS pretty nice, can perhaps take the place of some(most?) of features.h, unistd.h and friends....which is really what I want, regardless of the example...or having to 'roll my own'. Very cool. > This suggestion is taken from this thread on comp.lang.python: > http://tinyurl.com/46me3 > > Note that adding names to __builtin__ is NOT recommended in general! > Don't use this to create your own global constants! It is OK in this > case because to duplicate Python 2.3 behaviour you need a true global. > >> The other oddity is without being able to define a 'real' constant, as >> in #DEFINE MINVERSION 1.5, >> >> the scope of MINVERSION (and True/False) even using the global keyword >> still uses the ocal file's namespace. I don't want to debate the >> merits of using globals...most people that claim they never use any in >> other languages _still_ use constants in header files, which is the >> purpose I'd like to be able to do generally > > > The usual Python solution is to make a module containing constant > definitions and import it where you need them. > [snip] > or, if you want to import all the constants directly, > > # Client2.py > from Constants import * There was my catch/issue I was having. I expected the global keyword being used in my previous bool/True/False definition to put it into a global namespace, but I still had a namespace issue as you explained here- simply doing an 'import ' will allow access to variables created as globals in that module, but not without scope resolution, ie module.True, module.MINVERSION, etc...what I expected was anything declared as global to simply be in the global namespace of any other module importing the one with the declaration. > if (MINVERSION == 1.5): > ... > >> >> A last question would also be if the equivalent of __FILE__ and >> __LINE__ macros exist? > Great- looks like there's a link for python < 2.X as well, will definitely look at using that! Thanks! > See this recipe and the one linked to in its discussion: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062 > > Kent > >> >> Thanks, >> >> Scott >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Thu Jan 6 14:08:06 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 6 14:08:11 2005 Subject: [Tutor] replacement for constants from other languages in Python? In-Reply-To: <41DD341E.1080707@mindcore.net> References: <41DCF386.9060904@mindcore.net> <41DD2610.8050801@tds.net> <41DD341E.1080707@mindcore.net> Message-ID: <41DD3836.40104@tds.net> Scott W wrote: > Kent Johnson wrote: >> MINVERSION = repr(1.5) >> should work just fine. It will give the same result as the more readable >> MINVERSION = '1.5' > > > Ok, this would make a bit more sense RE: repr()- in one of the resources > I found, it seemed to state that repr(x) was converting x into a numeric > representation, ala atoi() and friends. From the Library Reference section 2.1 Built-in Functions http://docs.python.org/lib/built-in-funcs.html : repr( object) Return a string containing a printable representation of an object...For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(). So repr(x) will always be a string, and eval(repr(x)) will sometimes == x. int() and float() convert from strings to numbers. > OK, that's helpful. I can see the advantages of several of python's > mechanisms, such as dir() as well as treating everything like an > 'interactive object to determine existing methods and attributes (dir() > and your example of hasattr(). Pretty cool...now to go from libc/POSIX > to an entirely new and (mostly) different set of core libs and > functionality.. ;-) Actually, that IS pretty nice, can perhaps take the > place of some(most?) of features.h, unistd.h and friends....which is > really what I want, regardless of the example...or having to 'roll my > own'. Very cool. I'm not familiar with libc or POSIX but much of the os module is a pretty thin layer over the native C libs so it might not be so far off of what you are familiar with. OTOH the use of fundamental data types like strings and lists is (woohoo!) much different than C or C++. Kent From mark.kels at gmail.com Thu Jan 6 16:25:08 2005 From: mark.kels at gmail.com (Mark Kels) Date: Thu Jan 6 16:25:11 2005 Subject: [Tutor] Looking for a project participate in (a little OT) Message-ID: I'm learning python for a few months now, and I would like to get some experience by participating in a good, open source, and small python-CGI project. I searched freshmeat.net but couldn't find anything interesting (except __ but I think its dead...). Anyone knows of a good small and open source python-CGI project that needs people ? Thanks. And sorry if its to much OT. -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon From lordvader at gmail.com Thu Jan 6 19:29:13 2005 From: lordvader at gmail.com (Fred Lionetti) Date: Thu Jan 6 19:29:17 2005 Subject: [Tutor] automatically finding site-packages and python2.3 in a linux machine Message-ID: <4341406205010610296736bcb8@mail.gmail.com> Hi everyone, I'm working on creating an installer for my program using install shield, and I'd like to know how one can automatically determine if Python 2.3 is installed on a linux machine, and where site-packages is located (so that I can install my own files there). For my Windows version I was able to search for the python2.3 entry in the windows registry, but I don't know how do the equivalent from linux. Any ideas? Thanks Fred From alan.gauld at freenet.co.uk Thu Jan 6 19:54:56 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 6 19:54:33 2005 Subject: [Tutor] replacement for constants from other languages in Python? References: <41DCF386.9060904@mindcore.net> Message-ID: <008001c4f421$37883cf0$60a08851@xp> > I'm _very_ used to using C style constants (preprocessor #define > directives) or C++ const keyword style, for a variety of reasons. > > I've yet to see anything covering 'how to work around the lack of > constants in Python'...can anyone point me in the right direction here? Define "Constants" in Upper case letters and don't change anything that has an uppercase name... Its consistent with Python's philosophy of us all being consenting adults not idiots... > still uses the ocal file's namespace. I don't want to debate the merits > of using globals...most people that claim they never use any in other > languages _still_ use constants in header files, which is the purpose > I'd like to be able to do generally... THats fine just import the consants module. Using modules avoids name clashes and is A Good Thing - even for constants. > really am _not_ thrilled with the use of string literals typed in each > time in code (yes, quick and dirty code but still) I see seems to be > 'OK' in python for the use of comparisons...opposed to something like > > if(strncmp(strVal, > CONSTANT_STRING_VAL_LIKE_HTTP_ACCEPT_ENCODING_HEADER, > strlen(strVal) > { > do_something(); > } Not sure whatthe issue is here. You can define string constants and compare them if you wish. jUst as you can compare string literals in C... I have a feeling I'm missing your point? > if(floatVal > PI) > { > do_something() > } Again, not sure what the issue is. Use math.pi as a constant, whats the issue? > A last question would also be if the equivalent of __FILE__ and __LINE__ > macros exist? Sadly not, that's one thing I would like to see. It is possible to fake it via traceback objects but its mucky. You can get file fairly easily via the __Name__ magic variable but line number is trickier. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Thu Jan 6 19:57:00 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 6 19:57:21 2005 Subject: [Tutor] Array pointers References: <200501060107.43589.orion_val@163.com> <6102.1104955647@www19.gmx.net> Message-ID: <009301c4f421$81121300$60a08851@xp> > Pixels - just ones and zeroes? Pack them as integers and apply the > right shift operator: > i>>=1 But you have to remember to overflow right hand ones into the next integer if there are more than 32 bits... Although Python long integers migfht work, dunno what the speed of shifting a long integer is in Python.. hmmm must try it! Alan G. From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 20:12:52 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 20:12:56 2005 Subject: [Tutor] automatically finding site-packages and python2.3 in a linux machine In-Reply-To: <4341406205010610296736bcb8@mail.gmail.com> Message-ID: On Thu, 6 Jan 2005, Fred Lionetti wrote: > I'm working on creating an installer for my program using install > shield, and I'd like to know how one can automatically determine if > Python 2.3 is installed on a linux machine, and where site-packages is > located (so that I can install my own files there). For my Windows > version I was able to search for the python2.3 entry in the windows > registry, but I don't know how do the equivalent from linux. Any ideas? Hi Fred, Yes, there are some undocumented functions in the Distutils package that you can use to find where 'site-packages' lives. Let me check... ah, ok, the function that you're probably looking for is distutils.sysconfig.get_python_lib(). For example: ### >>> distutils.sysconfig.get_python_lib() '/usr/lib/python2.3/site-packages' ### def get_python_lib(plat_specific=0, standard_lib=0, prefix=None): """Return the directory containing the Python library (standard or site additions). If 'plat_specific' is true, return the directory containing platform-specific modules, i.e. any module from a non-pure-Python module distribution; otherwise, return the platform-shared library directory. If 'standard_lib' is true, return the directory containing standard Python library modules; otherwise, return the directory for site-specific modules. If 'prefix' is supplied, use it instead of sys.prefix or sys.exec_prefix -- i.e., ignore 'plat_specific'. """ So you can use Python itself to introspect where the libraries should live. I hope this helps! From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 20:19:51 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 20:19:56 2005 Subject: [Tutor] replacement for constants from other languages in Python? In-Reply-To: <008001c4f421$37883cf0$60a08851@xp> Message-ID: On Thu, 6 Jan 2005, Alan Gauld wrote: > > I'm _very_ used to using C style constants (preprocessor #define > > directives) or C++ const keyword style, for a variety of reasons. > > > > I've yet to see anything covering 'how to work around the lack of > > constants in Python'...can anyone point me in the right direction > > here? Hi Scott, There are a few recipes in the Python Cookbook that mentions how to get a "const" mechanism in Python: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197965 But I have to admit that I don't use this approach myself; I've been using the uppercase convension, and it seems to work ok. Good luck to you! From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 20:24:30 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 20:24:34 2005 Subject: [Tutor] automatically finding site-packages and python2.3 in a linux machine In-Reply-To: Message-ID: > > I'm working on creating an installer for my program using install > > shield, and I'd like to know how one can automatically determine if > > Python 2.3 is installed on a linux machine Hi Fred, Sorry about ignoring parts of your question! Unix has default places for putting binaries like Python. Check the directories '/usr/bin/' and '/usr/local/bin'. Also, the 'which' command will also tell us where Python is, if it's in the user's PATH: ### [dyoo@shoebox dyoo]$ which python /usr/bin/python ### > > and where site-packages is located (so that I can install my own files > > there). For my Windows version I was able to search for the python2.3 > > entry in the windows registry, but I don't know how do the equivalent > > from linux. Any ideas? > > Yes, there are some undocumented functions in the Distutils package that > you can use to find where 'site-packages' lives. I'm totally wrong about this. It IS documented. *grin* Here's a link to the official documentation: http://www.python.org/doc/dist/module-distutils.sysconfig.html Sorry about that; I had expected to find it in the Library Reference, but the Distutils stuff has its own separate documentation. Best of wishes to you! From kent37 at tds.net Thu Jan 6 20:46:46 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 6 20:46:34 2005 Subject: [Tutor] replacement for constants from other languages in Python? In-Reply-To: References: Message-ID: <41DD95A6.6070804@tds.net> Danny Yoo wrote: > There are a few recipes in the Python Cookbook that mentions how to get a > "const" mechanism in Python: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207 > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197965 These require Python 2.1 and 2.3 respectively; the OP indicated he needs to work with 1.5.2! > > But I have to admit that I don't use this approach myself; I've been using > the uppercase convension, and it seems to work ok. (To Scott, not Danny): Compared to Java and C++, Python has very meager facilities for controlling how code is used. There is no const, and only the slightest nod to access control with the _ and __ naming conventions. And of course the lack of static typing limits your control over function parameters. Although this might make you a bit nervous at first, in practice it doesn't cause problems and is very freeing. Later versions of Python have some methods that let you build controls, as Danny pointed out, but my suggestion is, just relax and try the Python way! Kent From bvande at po-box.mcgill.ca Thu Jan 6 21:05:14 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 6 21:11:50 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: References: Message-ID: <41DD99FA.1030207@po-box.mcgill.ca> Danny Yoo said unto the world upon 2005-01-03 04:11: > > On Mon, 3 Jan 2005, Brian van den Broek wrote: > > >>>>(Aside: one nonobvious example where copying can be avoided is in >>>>Conway's Game of Life: when we calculate what cells live and die in >>>>the next generation, we can actually use the 'Command' design pattern >>>>to avoid making a temporary copy of the world. We can talk about >>>>this in more detail if anyone is interested.) >>> >>Seconded. (Thanks for offering, Danny.) > > > [Long post up ahead.] > > Just as a warning, none of what I'm going to code here is original at all: > I'm rehashing a main idea off of a paper called "Using the Game of Life to > Introduce Freshman Students to the Power and Elegance of Design Patterns": > > http://portal.acm.org/citation.cfm?id=1035292.1028706 Hi Danny and all, Thanks for doing this, Danny. (And sorry to have taken a bit to get to it.) As an aside, Daniel Dennett, a philosopher who seems popular amongst the hacker-types, wrote some about Life some time in the 80's, I believe in the first instance in a paper called "Real Patterns". One of the neat things he toys with there is the (needless to say, abstract) possibility of instantiating a Turing-Complete computer out of the Life cells. Similarly, Wolfram's recent tome _A New Kind of Science_, from reviews and the skimming I've done, seems largely devoted to the idea that such cellular automata are a rich and untapped source of insight and scientific modelling. Wolfram's book is on my list, but, sadly, 1000'ish page books have a tendency to remain on that list ;-) OK, Danny, I get that you were quickly (and kindly) adapting an approach from somewhere else. So, this is all by way of query rather than criticism. I am having a hard time figuring out how to efficiently snip and comment, so please bear with any organizational issues. I think I will just preserve the parts relevant to what I want to talk/ask about and write at the end. > Ok, let's start hacking. The Game of Life involves a 2-D matrix of cells, > where any cell can be 'LIVE' or 'DEAD'. > > ### > LIVE, DEAD = '*', '.' > ### > def make_random_world(M, N): > """Constructs a new random game world of size MxN.""" > world = {} > for j in range(N): > for i in range(M): > world[i, j] = random.choice([LIVE, DEAD]) > world['dimensions'] = (M, N) > return world > > def print_world(world): > """Prints out a string representation of a world.""" > M, N = world['dimensions'] > for j in range(N): > for i in range(M): > print world[i, j], > print > ### > > (If I had more space, I'd rather do this as a real data structure instead > of hacking up a dictionary.) > > > So now we have something that shows a single generation of a game world. > How does the world run? Well, between each generation, each cell can > either live or die according to the rule: > > A dead cell with exactly 3 live neighbors becomes alive (or is > "born"). > > A live cell with 2 or 3 live neighbors stays alive; otherwise it dies > (from "loneliness"). > ### > def get_state(world, i, j): > """Returns the state of the cell at position (i, j). If we're out > of bounds, just returns DEAD to make calculations easier.""" > return world.get((i, j), DEAD) > > > def count_live_neighbors(world, i, j): > """Returns the number of live neighbors to this one.""" > live_count = 0 > for i_delta in [-1, 0, 1]: > for j_delta in [-1, 0, 1]: > if (i_delta, j_delta) == (0, 0): > continue > if get_state(world, i+i_delta, j+j_delta) == LIVE: > live_count += 1 > return live_count > > > def is_born(world, i, j): > """Returns True if the cell at (i, j) is currently dead, but will > be born in the next generation.""" > return (get_state(world, i, j) == DEAD and > count_live_neighbors(world, i ,j) == 3) > > > def is_deceased(world, i, j): > """Returns True if the cell at (i, j) is currently alive, but will > die in the next generation.""" > return (get_state(world, i, j) == LIVE and > not (2 <= count_live_neighbors(world, i ,j) <= 3)) > ### > Now the problem is: given the current state of the world, how do we > calculate the next state of the world? One way is to make a temporary > copy of the world, and apply changes to it, consulting the original > world for the rules: > The unsatisfying thing, though, is that we use an explicit copying step > and the temporary scratch space. But we can actually get away from making > temporary scratch space by using this cute idea: instead of directly > applying status changes immediately, we store a list of "Commands" that we > can execute after we figure out who lives and who dies: > > ### > def make_rise_command(world, i, j): > """Produces a callable function that, when called, will apply a change > to the world to make the cell at (i, j) live.""" > def cmd(): > world[i, j] = LIVE > return cmd > > > def make_die_command(world, i, j): > """Produces a callable function that, when called, will apply a change > to the world to make the cell at (i, j) die.""" > def cmd(): > world[i, j] = DEAD > return cmd > ### > Notice that the change to our world does not take place until we execute > the some_cmd command. This is, conceptually, an example of a "Command": > it's a callable that applies a state change when we finally call it. > Stripped of all the OOP/Class baggage, that's pretty much all it is. > > > Punchline time! > > ### > def apply_next_generation_revised(world): > """Destructively mutate the world so it reflect the next > generation.""" > M, N = world['dimensions'] > commands = [] > for j in range(N): > for i in range(M): > if is_born(world, i, j): > commands.append(make_rise_command(world, i, j)) > if is_deceased(world, i, j): > commands.append(make_die_command(world, i, j)) > for cmd in commands: > cmd() > ### > > > I know I rushed the heck out of this program. *grin* If you have any > questions on it, please feel free to ask. Talk to you later! OK, I've read you and Kent discussing whether the design pattern is really appropriate here. I'm pretty sure my queries are independent, but then I'm the tutee, so grain of salt, and all that. Anyway: To summarize what you code does: 1) Define world dict by filling an M by N matrix with representations of live cells and dead cells, and using the (M,N) tuples as dict keys for the representations, together with 'dimensions' as a key for (M,N). 2) Defines a print function which prints each cell's contents. 3) Defines a "count a cell's neighbours" function. 4) Defines a function for determining if a live cell is doomed. 5) Defines a function for determining if a dead cell will flourish. 6) Runs through the matrix, storing a list of alterations to apply, and then applies them en mass once the entire matrix is scanned (thereby obviating the need for a scratch copy of the matrix). I gave some thought (though produced no code) to the question of how to do a life game before you posted your code. My naive approach differs a bit, and it seems to me better. I'd like to know why I am wrong ;-) So, relying on your code unless differences specified (and, like you, not making it OOP, so having more passing of the world than I'd like, but I'm only just now groking the OOP way), I would have done something like the following. (Please be tolerant of any syntactic slips; I think and hope my aim is clear, if not the execution.): 1) Define world by filling an M by N matrix with True and False. Otherwise, as your step (1). 2) Define a print function which goes through the matrix, printing one thing for each True cell and another for each False cell. 3) Define the count_live_neighbours() roughly as you did. (The actual code below assumes your count function, modulo the change to it that you define a LIVE name and I am just using True.) 4) Define a "will be changed function" roughly as the untested code: def cell_will_change(cell): '''Returns True if a cell will change, False otherwise. (I'd actually structure it with a single exit point, but that doesn't seem a popular way to go here ;-) ''' i, j = cell if world[cell] and not (2 <= count_live_neighbors(world, i ,j) <= 3)): return True if not world[cell] and count_live_neighbors(world, i ,j) == 3): return True return False 5) Define a get_changed_cells_list function (again, untested): def get_changed_cells_list(world): '''Returns a list of cells that will change in the next generation.''' changed_cells_list = [] for c in world: if c = 'dimensions': continue if cell_will_change(c): changed_cells_list.append(c) return changed_cells_list 6) Define update_world function (again, untested): def update_world(world): '''Produces the next generation world.''' changed_cells_list = get_changed_cells_list(world) for c in changed_cells_list: if c = 'dimensions': continue world[c] = not world[c] return world Finally, def create_and_print_new_generation(world): world = update_world(world) print_world(world) return world I hope the fragmentary nature of this makes my intent clear. Anyway, as I see it, this has the following advantages over your posted code: 1) Cell representations done with built-ins seems likely to be quicker. (A minor point, though.) 2) Use of booleans for cell contents makes the change cell procedure simply saying not .> cell_contents # as in for loop of my update_world(). 3) Neither a copy of the world nor the design pattern is needed. Instead, I make a list of the cells to be changed. In the limit, where ever cell will change, this is no better than a copy of the world, but i) it often is better, and ii) I'm not sure the Life rules can create a world where every cell will change in the next generation, anyway. I don't see any disadvantages, but then I don't see too well ;-) Again, I get that your code was quick, adapting another source, and designed to showcase a pattern. But, all that aside, am I wrong in thinking that my approach has the listed advantages over your posted code? Any problems with it I am not seeing? Oh, also, when you said: > (If I had more space, I'd rather do this as a real data structure > instead of hacking up a dictionary.) did you mean make the world a class (and thus avoid the sort of passing the world dict up and down as I did here?) Either way, a quick word or two will be great; I'm not asking for you to take the time to code it up for me :-) Thanks for taking the time. Best to all, Brian vdB From maxnoel_fr at yahoo.fr Thu Jan 6 21:39:13 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 6 21:39:24 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: <41DD99FA.1030207@po-box.mcgill.ca> References: <41DD99FA.1030207@po-box.mcgill.ca> Message-ID: <0669E9B0-6023-11D9-943F-000393CBC88E@yahoo.fr> First of all, thanks for answering our questions, Danny! And sorry for the lag before my reply, but I was rather busy over the last few days (moving "back" to the UK). On Jan 6, 2005, at 20:05, Brian van den Broek wrote: > I am having a hard time figuring out how to efficiently snip and > comment, so please bear with any organizational issues. I think I will > just preserve the parts relevant to what I want to talk/ask about and > write at the end. And I hereby massively SNIP whatever remained of it. :D > I gave some thought (though produced no code) to the question of how > to do a life game before you posted your code. My naive approach > differs a bit, and it seems to me better. I'd like to know why I am > wrong ;-) > > So, relying on your code unless differences specified (and, like you, > not making it OOP, so having more passing of the world than I'd like, > but I'm only just now groking the OOP way), I would have done > something like the following. (Please be tolerant of any syntactic > slips; I think and hope my aim is clear, if not the execution.): > > 1) Define world by filling an M by N matrix with True and False. > Otherwise, as your step (1). > > 2) Define a print function which goes through the matrix, printing one > thing for each True cell and another for each False cell. > > 3) Define the count_live_neighbours() roughly as you did. (The actual > code below assumes your count function, modulo the change to it that > you define a LIVE name and I am just using True.) > > 4) Define a "will be changed function" roughly as the untested code: > 5) Define a get_changed_cells_list function (again, untested): > 6) Define update_world function (again, untested): > > I hope the fragmentary nature of this makes my intent clear. > > Anyway, as I see it, this has the following advantages over your > posted code: > > 1) Cell representations done with built-ins seems likely to be > quicker. (A minor point, though.) > > 2) Use of booleans for cell contents makes the change cell procedure > simply saying not > .> cell_contents # as in for loop of my update_world(). > > 3) Neither a copy of the world nor the design pattern is needed. > Instead, I make a list of the cells to be changed. In the limit, where > ever cell will change, this is no better than a copy of the world, but > i) it often is better, and ii) I'm not sure the Life rules can create > a world where every cell will change in the next generation, anyway. > > I don't see any disadvantages, but then I don't see too well ;-) You're wrong in that you're not wrong. I'm not very familiar with design patterns yet, but to me, what you just described looks like another Life implementation using the Command design pattern. It is, however, more efficient and IMO elegant than Danny's. Correct me if I'm wrong (I may be talking out of, er, a certain part of my anatomy that is far from my mouth), but the basic idea of the Command design pattern is to store the changes to be made (instead of the post-change states) in a disposable data structure, then to apply them to the original copy of the world. Which should be more efficient, at least memory-wise. Right? Oh, the Life rules allow a world where every cell will change in the next generation, iff your world is a torus (i.e. the lower row "touches" the upper row as if it were immediately above it, and the right column "touches" the left column as if it were immediately left of it). It is quite trivial: set all cells to LIVE. Next generation they're all DEAD. If your world is a finite rectangle, I think the best you can get is a world where all but four cells (the corners) change next generation (same method). As your rectangle stretches to infinity, obviously, the cells changed/total cells ration converges toward 1. In any case, your point still stands. > did you mean make the world a class (and thus avoid the sort of > passing the world dict up and down as I did here?) Either way, a quick > word or two will be great; I'm not asking for you to take the time to > code it up for me :-) Probably, but I don't think that's relevant. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From bvande at po-box.mcgill.ca Thu Jan 6 22:20:40 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 6 22:33:34 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: <0669E9B0-6023-11D9-943F-000393CBC88E@yahoo.fr> References: <41DD99FA.1030207@po-box.mcgill.ca> <0669E9B0-6023-11D9-943F-000393CBC88E@yahoo.fr> Message-ID: <41DDABA8.5040603@po-box.mcgill.ca> Max Noel said unto the world upon 2005-01-06 15:39: > > On Jan 6, 2005, at 20:05, Brian van den Broek wrote: > > >> I gave some thought (though produced no code) to the question of how >> to do a life game before you [Danny] posted your code. My naive approach >> differs a bit, and it seems to me better. I'd like to know why I am >> wrong ;-) >> >> So, relying on your [Danny's] code unless differences specified (and, like you, >> not making it OOP, so having more passing of the world than I'd like, >> but I'm only just now groking the OOP way), I would have done >> something like the following. (Please be tolerant of any syntactic >> slips; I think and hope my aim is clear, if not the execution.): >> >> 1) Define world by filling an M by N matrix with True and False. >> Otherwise, as your step (1). >> >> 2) Define a print function which goes through the matrix, printing one >> thing for each True cell and another for each False cell. >> >> 3) Define the count_live_neighbours() roughly as you did. (The actual >> code below assumes your count function, modulo the change to it that >> you define a LIVE name and I am just using True.) >> >> 4) Define a "will be changed function" roughly as the untested code: > > > > >> 5) Define a get_changed_cells_list function (again, untested): > > > > >> 6) Define update_world function (again, untested): >> > > > >> I hope the fragmentary nature of this makes my intent clear. >> >> Anyway, as I see it, this has the following advantages over your >> posted code: >> >> 1) Cell representations done with built-ins seems likely to be >> quicker. (A minor point, though.) >> >> 2) Use of booleans for cell contents makes the change cell procedure >> simply saying not >> .> cell_contents # as in for loop of my update_world(). >> >> 3) Neither a copy of the world nor the design pattern is needed. >> Instead, I make a list of the cells to be changed. In the limit, where >> ever cell will change, this is no better than a copy of the world, but >> i) it often is better, and ii) I'm not sure the Life rules can create >> a world where every cell will change in the next generation, anyway. >> >> I don't see any disadvantages, but then I don't see too well ;-) > > > You're wrong in that you're not wrong. I'm not very familiar with > design patterns yet, but to me, what you just described looks like > another Life implementation using the Command design pattern. It is, > however, more efficient and IMO elegant than Danny's. Well, thanks. :-) > Correct me if I'm wrong (I may be talking out of, er, a certain part > of my anatomy that is far from my mouth), but the basic idea of the > Command design pattern is to store the changes to be made (instead of > the post-change states) in a disposable data structure, then to apply > them to the original copy of the world. Which should be more efficient, > at least memory-wise. Right? I don't know design patterns beyond the concept and a teeny bit of poking about. I guess I should check, but I got the sense that the Command pattern involved storing actual instructions instead of representational data. (Ugly term, but since Python functions are first-class objects I need something for data that isn't an instruction.) The sense comes more from the name than anything else, though. > Oh, the Life rules allow a world where every cell will change in the > next generation, iff your world is a torus (i.e. the lower row "touches" > the upper row as if it were immediately above it, and the right column > "touches" the left column as if it were immediately left of it). It is > quite trivial: set all cells to LIVE. Next generation they're all DEAD. Topologist! (That's cheating!) ;-) If we are going that way, you 'iff' seems a bit hasty. Take the 1x1 matrix 'full' of live cells. Also, other 'funny' (in the sense that a torus is funny) planes could be defined (say a torus-like structure with more than 1 whole -- cannot recall the general terminology from ill-remembered topology), etc. I meant the claim for a standard non-trivial (i.e. M > 1 and N > 1) MxN euclidean plane matrix, but your correction is both amusing and welcome. Best to all, Brian vdB From wan at walrus.us Thu Jan 6 22:43:33 2005 From: wan at walrus.us (Vincent Wan) Date: Thu Jan 6 22:43:37 2005 Subject: [Tutor] trouble getting a list to update Message-ID: <030FEF2C-602C-11D9-B48B-000393B2D990@walrus.us> Dear All, I am trying to learn python and an having a problem. I wrote a program to repeatedly: a: print a list of integers eg 0 0 0 0 0 0 0 0 0 0 b: change each integer in the list to 1 with a .05% chance I run the program and over itterations more 1's appear as they should. However, the changes printed in step a don't seem to be the same as those made in step b (as shown by the debug code. Can someone tell me what I am doing wrong? import random total_time = 50 genome = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] mutation_rate = .05 def write_list(list_to_write, file_name): "Writes elements of a list, seperated by spaces, to a file" for each in list_to_write: file_name.write(str(list_to_write[each]) + ' ') file_name.write('\n') def mutate_genome(a_genome, genome_length, rate): "Randomly changes a list ellement to 1" current_base = 0 while current_base < genome_length: temp = random.random() myfile.write('base ' + str(current_base) + ' = ') # debug myfile.write(str(a_genome[current_base]) + '\n') # debug if temp <= rate: a_genome[current_base] = 1 myfile.write('base ' + str(current_base) + ' mutated to ') # debug myfile.write(str(a_genome[current_base]) + '\n') # debug current_base = current_base + 1 return a_genome myfile = open('myfile', 'w') current_time = 0 while current_time < total_time: myfile.write('\nTime is ' + str(current_time) + ' the genome is ') # debug write_list(genome, myfile) length = len(genome) genome = mutate_genome(genome, length, mutation_rate) current_time = current_time + 1 myfile.close() Thank you very much, Vincent Wan ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan@walrus.us From maxnoel_fr at yahoo.fr Thu Jan 6 22:45:02 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 6 22:45:13 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: <41DDABA8.5040603@po-box.mcgill.ca> References: <41DD99FA.1030207@po-box.mcgill.ca> <0669E9B0-6023-11D9-943F-000393CBC88E@yahoo.fr> <41DDABA8.5040603@po-box.mcgill.ca> Message-ID: <384332D2-602C-11D9-943F-000393CBC88E@yahoo.fr> On Jan 6, 2005, at 21:20, Brian van den Broek wrote: >> Oh, the Life rules allow a world where every cell will change in >> the next generation, iff your world is a torus (i.e. the lower row >> "touches" the upper row as if it were immediately above it, and the >> right column "touches" the left column as if it were immediately left >> of it). It is quite trivial: set all cells to LIVE. Next generation >> they're all DEAD. > > Topologist! (That's cheating!) ;-) > > If we are going that way, you 'iff' seems a bit hasty. Take the 1x1 > matrix 'full' of live cells. Well, if the only cell of a 1x1 torus matrix is LIVE, that means it is surrounded by 4 LIVE cells, doesn't it? :D > Also, other 'funny' (in the sense that a torus is funny) planes could > be defined (say a torus-like structure with more than 1 whole -- > cannot recall the general terminology from ill-remembered topology), > etc. I meant the claim for a standard non-trivial (i.e. M > 1 and N > > 1) MxN euclidean plane matrix, but your correction is both amusing and > welcome. Thanks :) However, the main reason why I talked about a torus is that it's one of the two obvious choices when you're implementing Life using a 2D matrix (the other being a finite rectangular plane). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From alan.gauld at freenet.co.uk Thu Jan 6 22:55:42 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 6 22:55:19 2005 Subject: [Tutor] Looking for a project participate in (a little OT) References: Message-ID: <00ac01c4f43a$785ce190$60a08851@xp> > I searched freshmeat.net but couldn't find anything interesting I guess that depends a bit on what you find intersting! But rather than frshmeat why not search source-forge? There are far more projects on source forge than those that get announced on freshmeat. And many are looking or people to do small jobs like write test scripts that don't need deep knowledge of the project. Alan G From dyoo at hkn.eecs.berkeley.edu Thu Jan 6 22:59:26 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 6 22:59:31 2005 Subject: [Tutor] trouble getting a list to update In-Reply-To: <030FEF2C-602C-11D9-B48B-000393B2D990@walrus.us> Message-ID: On Thu, 6 Jan 2005, Vincent Wan wrote: > I wrote a program to repeatedly: > a: print a list of integers eg 0 0 0 0 0 0 0 0 0 0 > b: change each integer in the list to 1 with a .05% chance > > I run the program and over itterations more 1's appear as they should. > > However, the changes printed in step a don't seem to be the same as > those made in step b (as shown by the debug code. Hi Vincent, Can you show us a snippet of the file output? I'm not immediately seeing anything particular with your debugging output statements: myfile.write('base ' + str(current_base) + ' mutated to ') myfile.write(str(a_genome[current_base]) + '\n') Like the computer, I don't yet understand what the problem is. *grin* If you can point us at the output that looks weird to you, and tell us what you expected to see instead and why, that will help us a lot to better understand the situation. Best of wishes! From cmeesters at ucdavis.edu Thu Jan 6 23:17:04 2005 From: cmeesters at ucdavis.edu (Christian Meesters) Date: Thu Jan 6 23:17:09 2005 Subject: [Tutor] German Totorial!?! Message-ID: <200501062217.j06MH4Dj025669@syrphus.ucdavis.edu> Hoi Michael, Apart from Alan's tutorial in German and the link Andrew provided you might want to have a look here: http://python.sandtner.org/ (The German Python forum.) You'll find not only links to other German sources, but also friendly support in German ;-). Gruss, Christian From alan.gauld at freenet.co.uk Thu Jan 6 23:25:51 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 6 23:25:31 2005 Subject: [Tutor] replacement for constants from other languages in Python? References: <41DD95A6.6070804@tds.net> Message-ID: <00c301c4f43e$ae3c3eb0$60a08851@xp> > Compared to Java and C++, Python has very meager facilities > for controlling how code is used. There is no const, and only > the slightest nod to access control .... my > suggestion is, just relax and try the Python way! I'll second that. I came to Python after years of C++ (and Pascal - even more strict!) and initially thought that Python's approach, while great for a scripting language, would lead to problems on bigger projects. Well, I haven't written anything truly huge yet but I haven't hit any more problems than I did in C/C++ so far - and C/C++ introduce their own problems with overly strict typing, especially in OOP work. My experience of Smalltalk at university (and in Lisp too) should have made me less concerned but years of indoctrination by the software engineering establishment had led me to believe in the idea of the language controlling the programmer rather than the other way around... Although I haven't used it on big projects either, I suspect that Objective C as used in Apple's Cocoa framework may offer a good mix of freedom and strictness for large industry strength projects. The more I use Objective C the more I like its divisio of concerns betweentraditional C style coding and the OO features where dynamic dispatch is a huge win over C++/Java's strict typing. Similarly features such as __FILE__ and __LINE__ are often used in C because the debugger and error reporting support is so poor, in a duynamic, interpreted language with full exception handling the need is restricted to log files and audit trails etc. So while I miss __LINE__ especially, I don;t need it nearly as much as I did in C. And finally, because Python operates at a much higher level you only write a fraction of the code that you do in C, so the need for these navigation aids is much less. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From wan at walrus.us Thu Jan 6 23:43:53 2005 From: wan at walrus.us (Vincent Wan) Date: Thu Jan 6 23:43:56 2005 Subject: [Tutor] trouble getting a list to update In-Reply-To: References: Message-ID: <70C519FE-6034-11D9-840A-000393B2D990@walrus.us> Dear Danny, On Jan 6, 2005, at 12:59 PM, Danny Yoo wrote: > Can you show us a snippet of the file output? I'm not immediately > seeing > anything particular with your debugging output statements: > Like the computer, I don't yet understand what the problem is. *grin* > If you can point us at the output that looks weird to you, and tell us > what you expected to see instead and why, that will help us a lot to > better understand the situation. Here is some sample output: ... Time is 2 the genome is 0 0 0 0 0 0 0 0 0 0 base 0 = 0 base 1 = 0 base 2 = 0 base 3 = 0 base 4 = 0 base 4 mutated to 1 base 5 = 0 base 6 = 0 base 7 = 0 base 8 = 0 base 9 = 0 *** The first kind of error appears here: Time is 3 the genome is 0 0 0 0 0 0 0 0 0 0 *** The line above should have read: *** Time is 3 the genome is 0 0 0 0 1 0 0 0 0 0 *** print out of genome in the main loop on pass 3 does not reflect the mutation on pass 2 base 0 = 0 base 1 = 0 base 2 = 0 base 3 = 0 base 4 = 1 base 5 = 0 base 6 = 0 base 6 mutated to 1 base 7 = 0 base 8 = 0 base 9 = 0 *** again here Time is 4 the genome is 0 0 0 0 0 0 0 0 0 0 *** should be :Time is 4 the genome is 0 0 0 0 1 0 1 0 0 0 base 0 = 0 base 1 = 0 base 2 = 0 base 3 = 0 base 4 = 1 base 5 = 0 base 6 = 1 base 7 = 0 base 8 = 0 base 9 = 0 Time is 5 the genome is 0 0 0 0 0 0 0 0 0 0 base 0 = 0 base 1 = 0 base 1 mutated to 1 base 2 = 0 base 3 = 0 base 4 = 1 base 5 = 0 base 6 = 1 base 7 = 0 base 8 = 0 base 9 = 0 *** now genome is correctly updated! Time is 6 the genome is 0 1 0 0 1 0 1 0 0 0 base 0 = 0 base 1 = 1 base 2 = 0 base 3 = 0 base 4 = 1 base 4 mutated to 1 base 5 = 0 base 6 = 1 base 7 = 0 base 8 = 0 base 9 = 0 Thank you, Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan@walrus.us From bvande at po-box.mcgill.ca Fri Jan 7 00:14:17 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Fri Jan 7 00:15:54 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: References: Message-ID: <41DDC649.2050309@po-box.mcgill.ca> Hi all, after making the sketch I posted a bit ago, I tried to turn to actual work. But, the bug had bit. ;-) So, here is an attempt at filling out that sketch in an OOP way. (It is my second OOP program, so if anyone was so inclined, I'd very much appreciate any comments. Also, I have a question about a snag I ran into while debugging.) I'm also not too used to sharing complete code. I'd put it in the public domain, but since so much of it was derived from Danny's code, I don't really know the conventions for so doing. (Were I to know it appropriate, I'd follow the copyright comment line with: # This software is released into the public domain. # Fold, spindle, or mutilate at will I've attached it rather than pasted it, as with something >100 lines I don't feel up to shortening line for email. Sorry 'bout that. The question: In an earlier version, instead of the run_world() method I now have, I put the following within my class definition and after (i.e. outside of) the method defs: .> while self.current_generation > self.total_generations: .> time.sleep(self.sleep_interval) .> self.update_world() .> self.print_world() which caused this: Traceback (most recent call last): File "D:\Python Files\foogame_of_life.py", line 3, in -toplevel- class life_world: File "D:\Python Files\foogame_of_life.py", line 76, in life_world while self.current_generation > self.total_generations: NameError: name 'self' is not defined That surprised me -- I'd have guessed that within a class, self was everywhere defined, and not just within methods. Clearly, I'm confused about something. Anyway, thanks for reading. Best to all, Brian vdB -------------- next part -------------- # pylife.py # Version 0.1 # Brian van den Broek # vanden@gmail.com # 2005-01-06 Thursday 17:41 # Copyright 2005 '''An OOP and ASCII based representation of Conway's Game of Life. Much of the code was inspired by a post of Danny Yoo's on 2005-01-03 04:11 to the Python Tutor List. (You can find that post by searching the archives at .) I make no claims to ellegance or efficency -- this is only the second OOP I have written.''' import random, time class life_world: def __init__(self, X, Y, sleep_interval = 0.5, total_generations = 20): self.X = X self.Y = Y self.world = self.seed_world() self.sleep_interval = sleep_interval self.total_generations = total_generations self.current_generation = 0 self.print_world() def seed_world(self): '''Constructs a new random world of size XxY.''' world = {} for j in range(self.X): for i in range(self.Y): world[i, j] = random.choice((True, False)) return world def print_world(self): '''Prints out a string representation of a world.''' print '\n\nGeneration Number: %s\n' %self.current_generation print '--'*(self.Y + 1) + '-' for j in range(self.X): print '|', for i in range(self.Y): if self.world[i, j]: print 'X', else: print ' ', print '|' print '--'*(self.Y + 1) + '-' def count_neighbours(self, cell): '''Returns the number of live neighbours to this one. 'neghboUrs because I'm Canadian, eh. ''' live_count = 0 i,j = cell for i_delta in [-1, 0, 1]: for j_delta in [-1, 0, 1]: if (i_delta, j_delta) == (0, 0): continue try: # To deal with the edges of the matrix, where the # deltas can take us out of bounds. if self.world[i+i_delta, j+j_delta]: live_count += 1 except KeyError: pass return live_count def cell_will_change(self, cell): '''Returns True if a cell will change, False otherwise.''' change = False if self.world[cell] and not self.count_neighbours(cell) in (2,3): change = True if not self.world[cell] and self.count_neighbours(cell) == 3: change = True return change def get_changed_cells_list(self): '''Returns a list of cells that will change in the next generation.''' changed_cells_list = [] for c in self.world: if self.cell_will_change(c): changed_cells_list.append(c) return changed_cells_list def update_world(self): '''Produces the next generation world.''' self.current_generation += 1 changed_cells_list = self.get_changed_cells_list() for c in changed_cells_list: self.world[c] = not self.world[c] def run_world(self): while self.current_generation < self.total_generations: time.sleep(self.sleep_interval) self.update_world() self.print_world() if __name__ == '__main__': world = life_world(7, 7) world.run_world() From dyoo at hkn.eecs.berkeley.edu Fri Jan 7 01:42:45 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 7 01:42:50 2005 Subject: [Tutor] trouble getting a list to update In-Reply-To: <70C519FE-6034-11D9-840A-000393B2D990@walrus.us> Message-ID: On Thu, 6 Jan 2005, Vincent Wan wrote: > On Jan 6, 2005, at 12:59 PM, Danny Yoo wrote: > > Can you show us a snippet of the file output? I'm not immediately > > seeing anything particular with your debugging output statements: > > > Like the computer, I don't yet understand what the problem is. *grin* > > > If you can point us at the output that looks weird to you, and tell us > > what you expected to see instead and why, that will help us a lot to > > better understand the situation. Hi Vincent, Ok, now I understand and see the bug. write_list() contains the problem: ### def write_list(list_to_write, file_name): "Writes elements of a list, seperated by spaces, to a file" for each in list_to_write: file_name.write(str(list_to_write[each]) + ' ') file_name.write('\n') ### The bug is here: file_name.write(str(list_to_write[each]) + ' ') Do you see why this is buggy? 'each' is not an index into list_to_write, but is itself an element of list_to_write. You probably meant to write: file_name.write(str(each) + ' ') Your list was mutating just fine: the output function simply was obscuring what was happening. Best of wishes to you! From askoose at sandia.gov Fri Jan 7 03:10:36 2005 From: askoose at sandia.gov (Kooser, Ara S) Date: Fri Jan 7 03:10:59 2005 Subject: [Tutor] German Tutorials auf Deutsch Message-ID: Pardon to the non-german speaking (or readers) on the list. Guten Tag. Mein Deutsch ist nicht so gut (ich habe keinen Deutsche in sieben Jahren geschreiben). Mann kann Python Tutorials auf Deutsch heir http://www.freenetpages.co.uk/hp/alan.gauld/german/index.htm und http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index_ger.html und http://starship.python.net/crew/gherman/publications/tut-de/ finden. I habe Alan Gauld Tutorial vorgelesen. Es is gut und es abdeckt zimliche viele Themen. Ara -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050106/716ad8d1/attachment.htm From kent37 at tds.net Fri Jan 7 03:58:22 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 7 03:58:10 2005 Subject: [Tutor] Re: The Game of Life In-Reply-To: <41DDC649.2050309@po-box.mcgill.ca> References: <41DDC649.2050309@po-box.mcgill.ca> Message-ID: <41DDFACE.90302@tds.net> Brian van den Broek wrote: > In an earlier version, instead of the run_world() method I now have, I > put the following within my class definition and after (i.e. outside of) > the method defs: > .> while self.current_generation > self.total_generations: > .> time.sleep(self.sleep_interval) > .> self.update_world() > .> self.print_world() > > which caused this: > > Traceback (most recent call last): > File "D:\Python Files\foogame_of_life.py", line 3, in -toplevel- > class life_world: > File "D:\Python Files\foogame_of_life.py", line 76, in life_world > while self.current_generation > self.total_generations: > NameError: name 'self' is not defined > > That surprised me -- I'd have guessed that within a class, self was > everywhere defined, and not just within methods. self is just another method parameter. OK not quite, but it is a method parameter and it is only bound within the scope of the method. There is nothing magic about the name, either; it is just a (strong) convention. In fact, by using a different name for self and the magic of lexical scoping and closures, you can do something very much like Java inner classes - make a nested class that has access to all the attributes of the enclosing class. This is off-topic and a bit twisted, but I'm not going to let that stop me: ''' Make a nested class that has access to the attributes of its parent class ''' class Outer: def __init__(outerSelf, x): outerSelf.x = x def makeNested(outerSelf, x): class Nested: def __init__(innerSelf, x): innerSelf.x = x def showX(innerSelf): print 'outer x is', outerSelf.x print 'inner x is', innerSelf.x return Nested(x) o = Outer(3) n = o.makeNested(5) n.showX() o.x = 22 n.showX() ################ prints: outer x is 3 inner x is 5 outer x is 22 inner x is 5 Kent > > Clearly, I'm confused about something. > > Anyway, thanks for reading. Best to all, > > Brian vdB > > > ------------------------------------------------------------------------ > > # pylife.py > # Version 0.1 > # Brian van den Broek > # vanden@gmail.com > # 2005-01-06 Thursday 17:41 > # Copyright 2005 > > '''An OOP and ASCII based representation of Conway's Game of Life. > Much of the code was inspired by a post of Danny Yoo's on 2005-01-03 04:11 > to the Python Tutor List. (You can find that post by searching the archives at > .) > > I make no claims to ellegance or efficency -- this is only the second OOP I > have written.''' > > import random, time > > class life_world: > > def __init__(self, X, Y, sleep_interval = 0.5, total_generations = 20): > self.X = X > self.Y = Y > self.world = self.seed_world() > self.sleep_interval = sleep_interval > self.total_generations = total_generations > self.current_generation = 0 > self.print_world() > > def seed_world(self): > '''Constructs a new random world of size XxY.''' > > world = {} > for j in range(self.X): > for i in range(self.Y): > world[i, j] = random.choice((True, False)) > return world > > def print_world(self): > '''Prints out a string representation of a world.''' > > print '\n\nGeneration Number: %s\n' %self.current_generation > print '--'*(self.Y + 1) + '-' > for j in range(self.X): > print '|', > for i in range(self.Y): > if self.world[i, j]: > print 'X', > else: > print ' ', > print '|' > print '--'*(self.Y + 1) + '-' > > def count_neighbours(self, cell): > '''Returns the number of live neighbours to this one. > > 'neghboUrs because I'm Canadian, eh. > ''' > live_count = 0 > i,j = cell > for i_delta in [-1, 0, 1]: > for j_delta in [-1, 0, 1]: > if (i_delta, j_delta) == (0, 0): > continue > try: > # To deal with the edges of the matrix, where the > # deltas can take us out of bounds. > if self.world[i+i_delta, j+j_delta]: > live_count += 1 > except KeyError: > pass > return live_count > > def cell_will_change(self, cell): > '''Returns True if a cell will change, False otherwise.''' > change = False > if self.world[cell] and not self.count_neighbours(cell) in (2,3): > change = True > if not self.world[cell] and self.count_neighbours(cell) == 3: > change = True > return change > > def get_changed_cells_list(self): > '''Returns a list of cells that will change in the next generation.''' > > changed_cells_list = [] > for c in self.world: > if self.cell_will_change(c): > changed_cells_list.append(c) > return changed_cells_list > > def update_world(self): > '''Produces the next generation world.''' > self.current_generation += 1 > changed_cells_list = self.get_changed_cells_list() > for c in changed_cells_list: > self.world[c] = not self.world[c] > > def run_world(self): > while self.current_generation < self.total_generations: > time.sleep(self.sleep_interval) > self.update_world() > self.print_world() > > if __name__ == '__main__': > world = life_world(7, 7) > world.run_world() > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at freenet.co.uk Fri Jan 7 09:34:00 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 7 09:33:27 2005 Subject: [Tutor] trouble getting a list to update References: <030FEF2C-602C-11D9-B48B-000393B2D990@walrus.us> Message-ID: <00dd01c4f493$a365bca0$60a08851@xp> > def write_list(list_to_write, file_name): > "Writes elements of a list, seperated by spaces, to a file" > for each in list_to_write: > file_name.write(str(list_to_write[each]) + ' ') This is the problem.(I think) You are taking each element of the list then using it(either 0 or 1) as an index into the list, which means you will only ever print the first two elements! Also you claim to be writing to a file_name, that needs to be an open file object... You need: for each in list_to_write: file_object.write(str(each) + ' ') However its much easier still to just write the list in one go: file_object.write(str(list_to_write) + '\n') > ...snipped code here... > myfile = open(....) Ah, OK you have actually opened the file, I thought you must since otherwise you;'d get no output. BUt I think you should change the variable name above... HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From mark.kels at gmail.com Fri Jan 7 15:14:12 2005 From: mark.kels at gmail.com (Mark Kels) Date: Fri Jan 7 15:14:14 2005 Subject: [Tutor] Problem with a variable in a CGI program Message-ID: HI all ! I started to read the following code (I will start working on it when this problem is fixed) and it looks OK while I read it. But for some reason it doesn't work... Here is the code: # MailList is copyright (c) 2000 of Programmed Integration # You may use this code in any way you see fit, but please # let use know where it is being used. If you make any changes to # the source code, please send us a copy so we can incorporate # the changes in future releases. If you have any other comments # please feel free to contact us at support@programmedintegration.com # MailList version 1.0.0 22 August 2000 import sys sys.path.append('/lib/python1.5') # My ISP requires this to correctly locate Python Modules import cgi, re, string # Import all the required modules try: useHead=open("head.txt", "r") # Open the HTML header file useFoot=open("foot.txt", "r") # Open the HTML footer file ContentLine="Content-type: text/html" # Standard content type for HTML files useform=cgi.FieldStorage() # Assign all variables on web form to UseForm variable email= useform["email"].value # Assign from form to local variables. Proposed email address email2= useform["email2"].value # ditto, verified email address password= useform["password"].value # ditto, proposed password password2= useform["password2"].value # ditto, verified password action=useform["action"].value # ditto, action, i.e subscribe or unsubscribe try: optout=useform["optout"].value # ditto, optout clause, yes or no except: # I've enclosed this in a try/except optout='no' # as if the checkbox is unchecked, nothing # was returned. This way 'no' is returned print ContentLine # Print standard content line print # Needs a blank line following print useHead.read() # Print HTML header if email!=email2: # Checks to see if two entered email addresses match print "Email addresses do not match" # If no match print error sys.exit(0) # Exit script with error 0 elif password!=password2: # Checks to see if two entered passwords match print "Passwords do not match" # If no match print error sys.exit(0) # Exit script with error 0 useMailList=open("maillist.txt", "r") # Open mailling list memMailList=useMailList.readlines() # Assign mailing list to internal list useMailList.close # Close mailing list found=0 # Create found variable counter=0 # Create counter variable for UseLine in memMailList: # For loop until list end is reached if string.find(string.upper(UseLine), string.upper(email))!=-1: # Checks to see if email is in mailing list found=1 # If yes, found = true (1) UseSplit=re.split(',',UseLine) # Create list of found line and delimit with a comma break # Exit for loop counter=counter+1 # If not found incrememnt count and repeat if not found: # If email address not found in mailing list if action=="unsubscribe": # And if action is unsubscribe print "Email address "+email+" does not exist on our database" # Print error message else: # Otherwise lineuse=email+","+password+','+optout+"\n" # Form valid mailing list entry memMailList.append(lineuse) # Add to internal list print "Subscription for "+email+" successful

" #Print success to HTML print "Many thanks for subscribing. Please be sure to make a note" print "of the email address you subscribed with. You will only" print "be able to unsubscribe if you use that email address." else: # Otherwise if email address not found in mailing list if action=="unsubscribe": # And if actions is unsubscribe if password==UseSplit[1]: # If password is valid memMailList[counter]='' # Empty internal mailing list entry print "Unsubscription for "+email+" successful" # Print unsubscribe success to HTML else: # Otherwise if password not valid print "Unsubscription for "+email+" unsuccessful. Password is invalid" # Print unsubscribe unsuccessful to HTML print "Remember that passwords are case sensitive. i.e. password is not the same as PassWord" else: # Otherwise if subscribe print "Subscription for "+email+" already exists" # Print subscription already exists to HTML finally: # Finally useMailList=open("maillist.txt", "w") # Open Mailing List for writing useMailList.writelines(memMailList) # Copy internal mailing list to file useMailList.close # Close mailing list print useFoot.read() # Print HTML footer useHead.close; # Close HTML header useFoot.close; # Close HTML footer When I run it on my server I get a 500 error, and when I run it as a normal python script (from windows) I get this error: Traceback (most recent call last): File "C:\maillist.py", line 80, in ? useMailList.writelines(memMailList) # Copy internal mailing list to file NameError: name 'memMailList' is not defined Whats wrong ?? Not only that memMailList is defined, its used a couple of times in previous lines in the code... BTW, the code was taken from: http://www.programmedintegration.com/cgi-bin/pages.py?pmaillist Thanks. -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon From wan at walrus.us Fri Jan 7 17:14:45 2005 From: wan at walrus.us (Vincent Wan) Date: Fri Jan 7 17:14:48 2005 Subject: [Tutor] trouble getting a list to update Message-ID: <3F0182DD-60C7-11D9-826B-000393B2D990@walrus.us> PROBLEM SOLVED THANK YOU DANNY AND ALAN To recap: My program main loop called two functions one that changed a list and one that printed the list. Turns out that print function was bad. I didn't understand how clever pythons for statement is. I wrote: def write_list(list_to_write, file_name): "Writes elements of a list, seperated by spaces, to a file" for each in list_to_write: file_name.write(str(list_to_write[each]) + ' ') The bug is here: file_name.write(str(list_to_write[each]) + ' ') should have been file_name.write(str(each) + ' ') Danny points out > Do you see why this is buggy? 'each' is not an index into > list_to_write, > but is itself an element of list_to_write. and, Alan explains > You are taking each element of the list then using > it(either 0 or 1) as an index into the list, which > means you will only ever print the first two elements! So for x in list: do_something_with_an_element(list[x]) and python keeps track of x for you. Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan@walrus.us From kent37 at tds.net Fri Jan 7 17:45:20 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 7 17:45:10 2005 Subject: [Tutor] trouble getting a list to update In-Reply-To: <3F0182DD-60C7-11D9-826B-000393B2D990@walrus.us> References: <3F0182DD-60C7-11D9-826B-000393B2D990@walrus.us> Message-ID: <41DEBCA0.3020102@tds.net> Vincent Wan wrote: > So You were doing fine up to here... > for x in list: > do_something_with_an_element(list[x]) > > and python keeps track of x for you. Now you are back into your original mistake! It should be for x in list: do_something_with_an_element(x) The variable in a for loop receives the actual list elements, not their indices. BTW it's a good idea to use a name other than 'list' for your list; 'list' is actually the name of the list type. Kent From dyoo at hkn.eecs.berkeley.edu Fri Jan 7 20:16:29 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 7 20:16:33 2005 Subject: [Tutor] Problem with a variable in a CGI program In-Reply-To: Message-ID: On Fri, 7 Jan 2005, Mark Kels wrote: > I started to read the following code (I will start working on it when > this problem is fixed) and it looks OK while I read it. But for some > reason it doesn't work... Hi Mark, Ok, let's take a look at the error message: > Traceback (most recent call last): > File "C:\maillist.py", line 80, in ? > useMailList.writelines(memMailList) # Copy internal mailing list to file > > NameError: name 'memMailList' is not defined Ok, good, so we know what we should look for: we should see where 'memMailList' is defined, and see if there's any way that the program flow can route around the definition. Ok, I see it, embedded within the context of a try/except block. ###### try: ## some code before definition of memMailList... memMailList=useMailList.readlines() ## some more code cut... finally: useMailList=open("maillist.txt", "w") useMailList.writelines(memMailList) useMailList.close print useFoot.read() useHead.close; useFoot.close; ###### There's one possibility that strikes me: it's very possible that in the code before the definition of memMailList might raise an exception, or exit out prematurely due to a sys.exit(). If that happens, then Python will finish things up by jumping into the 'finally' block. And if that happens, then we haven't reached memMailList's definition, and the NameError is inevitable. Here's a simplified example of what might be happening: ### >>> try: ... a = 42 ... 5 / 0 ... b = 17 ... finally: ... print "a is", a ... print "b is", b ... a is 42 b is Traceback (most recent call last): File "", line 7, in ? NameError: name 'b' is not defined ### A direct way to debug this is to drop the 'finally' block altogether. If we do so, like this: ###### ## some code before definition of memMailList... memMailList=useMailList.readlines() ## some more code cut... useMailList=open("maillist.txt", "w") useMailList.writelines(memMailList) useMailList.close print useFoot.read() useHead.close; useFoot.close; ###### then the real cause of the problem should pop up quickly. A few more comments: 1. The calls to close() need parentheses. Unlike some other programming languages, function calls fire off only with parens. Instead of: useFoot.close you probably want: useFoot.close() 2. I also see that the code is hardcoded to use Python 1.5. Is there any way you can use a more modern version of Python? The reason I ask is that recent versions of Python have really good modules for debugging CGI programs, including the excellent 'cgitb' module: http://www.python.org/doc/current/lib/module-cgitb.html Writing CGI programs without 'cgitb' is almost criminal. *grin* If you can, use it. 3. The program is long. Can you break it down into some functions? The program looks like one big monolith, and I'm actually quite terrified of it. *grin* Small functions are much easier to debug, and more approachable for maintainers of the code. This is a style critique, but it does matter because although computers don't care what code looks like, human programmers do. 4. Related to Comment Three is: there are way too many comments on the side margin. Comments like: ### if email != email2: # Checks to see if two entered # email addresses match print "Email addresses do not match" # If no match print error sys.exit(0) # Exit script with error 0 elif password != password2: # Checks to see if two # entered passwords match print "Passwords do not match" # If no match print error sys.exit(0) # Exit script with error 0 ### are redundant, since they basically parrot what the code is doing. Concise comments that explain the reason why the code is doing what it's doing can be more useful. Comments are meant to show us authorial intension: if we were interested in what the code was actually doing, we'd just read the code. *grin* Something like: ###### ## Check for valididated emails and passwords, and exit if either are ## invalid. if email != email2: print "Email addresses do not match" sys.exit(0) elif password != password2: print "Passwords do not match" sys.exit(0) ###### is probably sufficient. Even better would be to break this out as a function: ### def validateEmailsAndPasswords(email, email2, password, password2): """Check for valididated emails and passwords, and exit if either are invalid.""" if email != email2: print "Email addresses do not match" sys.exit(0) elif password != password2: print "Passwords do not match" sys.exit(0) ### but I guess we should take this one step at a time. If you have any questions, please feel free to ask. I hope this helps! From mark.kels at gmail.com Fri Jan 7 21:35:41 2005 From: mark.kels at gmail.com (Mark Kels) Date: Fri Jan 7 21:35:44 2005 Subject: [Tutor] Problem with a variable in a CGI program In-Reply-To: References: Message-ID: On Fri, 7 Jan 2005 11:16:29 -0800 (PST), Danny Yoo wrote: > > > On Fri, 7 Jan 2005, Mark Kels wrote: > > > I started to read the following code (I will start working on it when > > this problem is fixed) and it looks OK while I read it. But for some > > reason it doesn't work... > > Hi Mark, > > Ok, let's take a look at the error message: > > > > Traceback (most recent call last): > > File "C:\maillist.py", line 80, in ? > > useMailList.writelines(memMailList) # Copy internal mailing list to file > > > > NameError: name 'memMailList' is not defined > > Ok, good, so we know what we should look for: we should see where > 'memMailList' is defined, and see if there's any way that the program flow > can route around the definition. > > Ok, I see it, embedded within the context of a try/except block. > > ###### > try: > > ## some code before definition of memMailList... > > memMailList=useMailList.readlines() > > ## some more code cut... > > finally: > useMailList=open("maillist.txt", "w") > useMailList.writelines(memMailList) > useMailList.close > print useFoot.read() > useHead.close; > useFoot.close; > ###### > > There's one possibility that strikes me: it's very possible that in the > code before the definition of memMailList might raise an exception, or > exit out prematurely due to a sys.exit(). If that happens, then Python > will finish things up by jumping into the 'finally' block. And if that > happens, then we haven't reached memMailList's definition, and the > NameError is inevitable. > > Here's a simplified example of what might be happening: > > ### > >>> try: > ... a = 42 > ... 5 / 0 > ... b = 17 > ... finally: > ... print "a is", a > ... print "b is", b > ... > a is 42 > b is > Traceback (most recent call last): > File "", line 7, in ? > NameError: name 'b' is not defined > ### > > A direct way to debug this is to drop the 'finally' block altogether. If > we do so, like this: > > ###### > ## some code before definition of memMailList... > > memMailList=useMailList.readlines() > > ## some more code cut... > > useMailList=open("maillist.txt", "w") > useMailList.writelines(memMailList) > useMailList.close > print useFoot.read() > useHead.close; > useFoot.close; > ###### > > then the real cause of the problem should pop up quickly. > > A few more comments: > > 1. The calls to close() need parentheses. Unlike some other programming > languages, function calls fire off only with parens. Instead of: > > useFoot.close > > you probably want: > > useFoot.close() > > 2. I also see that the code is hardcoded to use Python 1.5. Is there any > way you can use a more modern version of Python? The reason I ask is that > recent versions of Python have really good modules for debugging CGI > programs, including the excellent 'cgitb' module: > > http://www.python.org/doc/current/lib/module-cgitb.html > > Writing CGI programs without 'cgitb' is almost criminal. *grin* If you > can, use it. > > 3. The program is long. Can you break it down into some functions? The > program looks like one big monolith, and I'm actually quite terrified of > it. *grin* > > Small functions are much easier to debug, and more approachable for > maintainers of the code. This is a style critique, but it does matter > because although computers don't care what code looks like, human > programmers do. > > 4. Related to Comment Three is: there are way too many comments on the > side margin. Comments like: > > ### > if email != email2: # Checks to see if two entered > # email addresses match > print "Email addresses do not match" # If no match print error > sys.exit(0) # Exit script with error 0 > elif password != password2: # Checks to see if two > # entered passwords match > print "Passwords do not match" # If no match print error > sys.exit(0) # Exit script with error 0 > ### > > are redundant, since they basically parrot what the code is doing. > > Concise comments that explain the reason why the code is doing what it's > doing can be more useful. Comments are meant to show us authorial > intension: if we were interested in what the code was actually doing, we'd > just read the code. *grin* > > Something like: > > ###### > > ## Check for valididated emails and passwords, and exit if either are > ## invalid. > if email != email2: > print "Email addresses do not match" > sys.exit(0) > elif password != password2: > print "Passwords do not match" > sys.exit(0) > ###### > > is probably sufficient. > > Even better would be to break this out as a function: > > ### > def validateEmailsAndPasswords(email, email2, password, password2): > """Check for valididated emails and passwords, and exit if either are > invalid.""" > if email != email2: > print "Email addresses do not match" > sys.exit(0) > elif password != password2: > print "Passwords do not match" > sys.exit(0) > ### > > but I guess we should take this one step at a time. > > If you have any questions, please feel free to ask. I hope this helps! > > Thank you very much !! But now I have much weirder problem... I got this error: C:\>maillist.py File "C:\maillist.py", line 84 ^ SyntaxError: invalid syntax And the weird thing is that the program has only 83 lines... For some reason python thinks that I have a ^ on the 84 code line. Whats wrong ?? Thanks ! BTW, I didn't wrote this program. I downloaded it (you can find the URL in my previous message) and started to read it so I could make some changes in it... -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon From dyoo at hkn.eecs.berkeley.edu Fri Jan 7 22:07:34 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 7 22:07:42 2005 Subject: [Tutor] Problem with a variable in a CGI program In-Reply-To: Message-ID: > But now I have much weirder problem... > I got this error: > > C:\>maillist.py > File "C:\maillist.py", line 84 > > ^ > SyntaxError: invalid syntax > > And the weird thing is that the program has only 83 lines... For some > reason python thinks that I have a ^ on the 84 code line. Whats wrong ?? Hi Mark, [meta: in your email reply, please just cut-and-paste the part that you're responding to.] Python error messages can put a carat symbol directly under the portion of the code that looks weird, as a visual aid to the programmer. For example, if we have this program: ### [dyoo@shoebox dyoo]$ cat foo.py print foobar ### then if we try to execute it, it'll break because the code is incorrectly indented: ### [dyoo@shoebox dyoo]$ python foo.py File "foo.py", line 1 print foobar ^ SyntaxError: invalid syntax ### And here, the carat is meant for us to see that things start going wrong starting at the 'print' statement. The error message that you're seeing, > C:\>maillist.py > File "C:\maillist.py", line 84 > > ^ > SyntaxError: invalid syntax is also trying to say that line 84 of the program has strange syntax that's inconsistent with the rest of the program. Most likely, it's an indentation error. Check line 84 and see if it really is just an empty line. > BTW, I didn't wrote this program. I downloaded it (you can find the URL > in my previous message) and started to read it so I could make some > changes in it... Hmmm. The code does need a revision. Maybe we can fix it up and send it back to the original authors, so that no one else has to encounter the bugs and problems that you're running into. *grin* From ryan at acceleration.net Fri Jan 7 22:08:42 2005 From: ryan at acceleration.net (Ryan Davis) Date: Fri Jan 7 22:09:00 2005 Subject: [Tutor] import question Message-ID: <20050107210857.070E41E4003@bag.python.org> Hello all, I'm just starting to use python seriously, and am having problems understanding exactly what is going on with import statements and the sys.path variable. I'm trying to import classes and functions across several directories. Here's a simple example of my filesystem: /Defs base.py /One a.py b.py /Two c.py In a.py I have: ### import b ### This results in an error: ImportError: No module named b My sys.path contains the absolute path to /Defs/One, so, if I'm understanding the import documentation (http://www.python.org/doc/current/ref/import.html), then the import statement should be searching the list of directories in sys.path, and finding it, but instead I get the ImportError. I'm using Python 2.3.4 on Windows XP, using Xemacs to develop it and debug as I go using the interpreter. Could this be something weird about how xemacs is running the code? Thanks, Ryan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050107/882b2390/attachment.html From keridee at jayco.net Fri Jan 7 22:40:23 2005 From: keridee at jayco.net (Jacob S.) Date: Fri Jan 7 22:40:57 2005 Subject: [Tutor] Lottery simulation References: <20050105153130.COQV12698.smtp3@Umit> Message-ID: <002101c4f501$92578c40$ca5328cf@JSLAPTOP> Unfortunately as I overly enjoy writing scripts, I tend to spoil the recipients of the tutor. IF!!!! you do not wish to have a complete answer, read someone else's email and not mine. ### Start code### import random participants = 10 ask = [raw_input('What is the participant\'s name? ') for x in range(participants)] for x in range(52): a = range(1,100) people = {} for i in ask: randomnum = random.choice(a) a.remove(randomnum) ## This is to keep from giving two people the same number people[i] = randomnum ##realnum = random.randint(1,100) ## Uncomment this to choose between 1&99 regardless of people realnum = random.choice(people.values()) ## Uncomment this to make sure someone wins. (default) ## Check -- did anyone win? for x,y in people.items(): if y == realnum: print "%s just won the lottery! Congratulations! " % x raw_input("Press enter to run next week's lottery. ") HTH and that I don't over help someone, Jacob Schmidt I am new to python and only created very simple programs so far.I would like to simulate a lottery draw for 10 participants and run it for 52 weeks. The lottery numbers would have a range of 1 to 99. Each person would have just one number (random) and play for 52 weeks. Are there any starting ideas for me to get going on this project either thru the tutor or searching for snippets already created by fellow programmers?? Thanks a lot in advance for all the help, Regards, ********************************************** ?mit N Tezcan SEYA?- European Projects Coordinator Tel : +90-212-2330920 (Ext.153) Fax : +90-212-233-0936 Mob : +90-533-337-7352 E-mail: untezcan@seyas.com.tr www : seyas.com.tr ********************************************** This email/fax message is for the sole use of the intended recipient(s) and may contain confidential and privileged information, or trade secrets. Any unauthorized review, use, disclosure or distribution of this email/fax is prohibited. If you are not the intended recipient, please contact the sender by email/fax and destroy all paper and electronic copies of the original message. ------------------------------------------------------------------------------ _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -------------------------------------------------------------------------------- _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050107/06ddc1c9/attachment.html From nick at javacat.f2s.com Fri Jan 7 23:32:14 2005 From: nick at javacat.f2s.com (Nick Lunt) Date: Fri Jan 7 23:29:39 2005 Subject: [Tutor] walking directories Message-ID: <20050107223214.72dcf4b6.nick@javacat.f2s.com> Hi folks, Im running python2.4 on linux. I have many python scripts in various directories of varying depth under my home directory, and I need to change one line in each of these files. I thought about using os.walk with os.path.join eg >>> for r,d,f in os.walk('.'): ... print os.path.join(r,d,f) but I get this error Traceback (most recent call last): File "", line 2, in ? File "/usr/local/lib/python2.4/posixpath.py", line 60, in join if b.startswith('/'): AttributeError: 'list' object has no attribute 'startswith' which makes sense. I know I can use glob.glob on the current directory, but Im struggling to see how I can access these files, change them and save the changes. Im also a little annoyed with myself cos I feel that I really should know how to do this by now. If anyone can just give me a pointer in the right direction I would be very grateful. Many thanks Nick . From ryan at acceleration.net Fri Jan 7 23:58:28 2005 From: ryan at acceleration.net (Ryan Davis) Date: Fri Jan 7 23:58:19 2005 Subject: [Tutor] walking directories In-Reply-To: <20050107223214.72dcf4b6.nick@javacat.f2s.com> Message-ID: <20050107225818.0D64D1E4003@bag.python.org> I think you want to be doing something like: >>>for r,d,f in os.walk('.'): ... for filename in f: ... print os.path.join(r,filename) I think that would give you the full path of every file, and then you can open it, do a regex substitution or whatever close it and keep going. Thanks, Ryan -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Nick Lunt Sent: Friday, January 07, 2005 5:32 PM To: python tutor Subject: [Tutor] walking directories Hi folks, Im running python2.4 on linux. I have many python scripts in various directories of varying depth under my home directory, and I need to change one line in each of these files. I thought about using os.walk with os.path.join eg >>> for r,d,f in os.walk('.'): ... print os.path.join(r,d,f) but I get this error Traceback (most recent call last): File "", line 2, in ? File "/usr/local/lib/python2.4/posixpath.py", line 60, in join if b.startswith('/'): AttributeError: 'list' object has no attribute 'startswith' which makes sense. I know I can use glob.glob on the current directory, but Im struggling to see how I can access these files, change them and save the changes. Im also a little annoyed with myself cos I feel that I really should know how to do this by now. If anyone can just give me a pointer in the right direction I would be very grateful. Many thanks Nick . _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From alan.gauld at freenet.co.uk Sat Jan 8 00:44:52 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 8 00:44:12 2005 Subject: [Tutor] Problem with a variable in a CGI program References: Message-ID: <012301c4f512$e2dd8000$60a08851@xp> > But now I have much weirder problem... > I got this error: > > C:\>maillist.py > File "C:\maillist.py", line 84 > > ^ > SyntaxError: invalid syntax > > And the weird thing is that the program has only 83 lines... > For some reason python thinks that I have a ^ on the 84 code line. > Whats wrong ?? It doesn't think you have a ^ anywhere, the caret(^) is pointing at what it thinks is the error, in this case a line of white space. Could it be you have an unterminated string (missing close quotes) at the end of your program? Alan G From alan.gauld at freenet.co.uk Sat Jan 8 01:05:41 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 8 01:05:01 2005 Subject: [Tutor] walking directories References: <20050107223214.72dcf4b6.nick@javacat.f2s.com> Message-ID: <014101c4f515$cb63f320$60a08851@xp> > I have many python scripts in various directories of > varying depth under my home directory, and I need to > change one line in each of these files. personally I'd use find and sed in Linux, right tool for the job etc... But if you really must wheel out a howitzer to shoot a squirrel... > I thought about using os.walk with os.path.join eg > > >>> for r,d,f in os.walk('.'): > ... print os.path.join(r,d,f) Shouldn't that be os.path.walk()? And os.path.walk takes 3 arguments: the starting point a function that it calls a placeholder for arguments to the function A simple file printer is: >>> def f(arg, dir, names): print names >>> os.path.walk('.', f, None) In your case you want to write an f() that iterates over the names argument and opens each file, does a string replacement and closs the file again. You probably should do some validation on the name and file type too... Thats why I'd use find and sed... Alan G. From alan.gauld at freenet.co.uk Sat Jan 8 01:09:18 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 8 01:08:42 2005 Subject: [Tutor] import question References: <20050107210857.070E41E4003@bag.python.org> Message-ID: <014f01c4f516$4c16e130$60a08851@xp> > I'm trying to import classes and functions across > several directories. > Here's a simple example of my filesystem: > /Defs > base.py > /One > a.py > b.py > /Two > c.py > > In a.py I have: > ### > import b > ### > This results in an error: > ImportError: No module named b > > My sys.path contains the absolute path to /Defs/One, > so, if I'm understanding the import documentation It should work as you describe it. Can you just clarify how you are doing this? Are you setting sys.path in the same program you are trying to run? Or is it set in a Python startup script? How do you run a.py? Are you importing it into an existing Python session(with sys.path set or are you ruinning the file standalone? Basically the only thing I can think of is that your setting of sys.path isn't being seen. Can you put a debug print statement to display sys.path just before the import b statement? That way we make absolutely certain Pythoncan see the folder. Alan G. > (http://www.python.org/doc/current/ref/import.html), then the import statement should be searching the list of directories in > sys.path, and finding it, but instead I get the ImportError. > > I'm using Python 2.3.4 on Windows XP, using Xemacs to develop it and debug as I go using the interpreter. Could this be something > weird about how xemacs is running the code? > Thanks, > Ryan > From tim at johnsons-web.com Sat Jan 8 03:09:52 2005 From: tim at johnsons-web.com (Tim Johnson) Date: Sat Jan 8 03:05:02 2005 Subject: [Tutor] Recommended SOAP libraries Message-ID: <20050108020952.GI5251@johnsons-web.com> What SOAP libraries might be recommended for python? ------ thanks tim -- Tim Johnson http://www.alaska-internet-solutions.com From keridee at jayco.net Sat Jan 8 03:26:33 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 8 03:27:50 2005 Subject: [Tutor] German Tutorials auf Deutsch Message-ID: <00e301c4f529$acaba1d0$ca5328cf@JSLAPTOP> ## Spoken by Ara ## Pardon to the non-german speaking (or readers) on the list. Guten Tag. Mein Deutsch ist nicht so gut (ich habe keinen Deutsche in sieben Jahren geschreiben). Mann kann Python Tutorials auf Deutsch heir http://www.freenetpages.co.uk/hp/alan.gauld/german/index.htm und http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index_ger.html und http://starship.python.net/crew/gherman/publications/tut-de/ finden. I habe Alan Gauld Tutorial vorgelesen. Es is gut und es abdeckt zimliche viele Themen. Ara ######## Done with text ########## Great! Another chance to try and decipher another language! Guten Tag = Good Morning Mein = My Deutsch = German (In *this* case, I guess) ist = is nicht = not so = so gut = good ich = I habe = have keinen = learned? Deutsche = German (I guess again) in = in sieben = seven? No, that can't be it... Mann = many kann = kind? (roughly?) auf = of heir = here OR at? und = and finden = used as end of a list Es = it That's all I can decipher.... Thanks for some fun, Jacob From keridee at jayco.net Sat Jan 8 03:37:05 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 8 03:37:03 2005 Subject: [Tutor] import question References: <20050107210857.070E41E4003@bag.python.org> <014f01c4f516$4c16e130$60a08851@xp> Message-ID: <00e901c4f52a$f49789e0$ca5328cf@JSLAPTOP> > It should work as you describe it. > Can you just clarify how you are doing this? > Are you setting sys.path in the same program you > are trying to run? Or is it set in a Python startup script? > > How do you run a.py? Are you importing it into an existing > Python session(with sys.path set or are you ruinning the > file standalone? > > Basically the only thing I can think of is that your setting > of sys.path isn't being seen. > > Can you put a debug print statement to display sys.path > just before the import b statement? That way we make > absolutely certain Pythoncan see the folder. > > Alan G. I had some similar trouble with site-packages when I tried to install psyco a little while ago. Apparently, the sys.path does not include sub folders of site-packages more than one branch deep. I fixed it by moving the main module folder up into the site-package folder. IOW, from C:\python24\lib\site-packages\psyco-1.3\psyco to C:\python24\lib\site-packages\psyco. And then it worked! So maybe Ryan has to specify the exact folder? HTH, Jacob Schmidt From keridee at jayco.net Sat Jan 8 03:38:57 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 8 03:39:29 2005 Subject: [Tutor] Problem with a variable in a CGI program References: <012301c4f512$e2dd8000$60a08851@xp> Message-ID: <00f201c4f52b$37057580$ca5328cf@JSLAPTOP> > line of white space. Could it be you have an unterminated > string (missing close quotes) at the end of your program? > > Alan G I would like to add maybe a missing or extra parenthesis? I have trouble with that alot. Jacob From keridee at jayco.net Sat Jan 8 03:58:15 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 8 03:58:23 2005 Subject: [Tutor] walking directories References: <20050107223214.72dcf4b6.nick@javacat.f2s.com> <014101c4f515$cb63f320$60a08851@xp> Message-ID: <010101c4f52d$f0de7220$ca5328cf@JSLAPTOP> > Shouldn't that be os.path.walk()? It says in the documentation (it might be the newer ones?): "Note: The newer os.walk() generator supplies similar functionality and can be easier to use." -- 6.2 os.path -- Common pathname manipulations And, yet, the os.walk() function states (condensed): blah, blah, blah Gives you three things. A root, a *list* of directories, and a *list* of files. The first I see wrong is he is not assuming a list return. Secondly, it also says in the documentation: """ dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). """ with particular attention paid to the exclusion of "." and ".." It would be my guess that what he enters for toplevel is interfering also with his output. import os directory = "." # Perhaps not a valid pathname? for root, dirs, files in os.walk(directory): for x in files: print os.path.join(root,x) yields what I believe he wants... HTH, Jacob Schmidt > And os.path.walk takes 3 arguments: > the starting point > a function that it calls > a placeholder for arguments to the function > > A simple file printer is: > > >>> def f(arg, dir, names): print names > >>> os.path.walk('.', f, None) > > In your case you want to write an f() that iterates > over the names argument and opens each file, does > a string replacement and closs the file again. > > You probably should do some validation on the name > and file type too... > > Thats why I'd use find and sed... > > Alan G. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at freenet.co.uk Sat Jan 8 09:19:12 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 8 09:19:05 2005 Subject: [Tutor] walking directories References: <20050107223214.72dcf4b6.nick@javacat.f2s.com> <014101c4f515$cb63f320$60a08851@xp> <010101c4f52d$f0de7220$ca5328cf@JSLAPTOP> Message-ID: <017001c4f55a$cce380e0$60a08851@xp> > > Shouldn't that be os.path.walk()? > > It says in the documentation (it might be the newer ones?): > > "Note: The newer os.walk() generator supplies similar functionality and can > be easier to use." > -- 6.2 os.path -- Common pathname manipulations Ah, I assume that must be a 2.3 addition, its not in my Nutshell book and I've only used os.path.walk previously. Thanks for the pointer. Alan G. From nick at javacat.f2s.com Sat Jan 8 20:44:14 2005 From: nick at javacat.f2s.com (Nick Lunt) Date: Sat Jan 8 20:41:37 2005 Subject: [Tutor] walking directories In-Reply-To: <200501072258.j07MwHmV012191@i-194-106-56-10.freedom2surf.net> Message-ID: >>From: Ryan Davis [mailto:ryan@acceleration.net] ?? >>I think you want to be doing something like: >> >>>for r,d,f in os.walk('.'): >>... for filename in f: >>... print os.path.join(r,filename) >> >>I think that would give you the full path of every file, and then you can open it, do a regex substitution or whatever close it and >>keep going. >> >>From: Jacob Schmidt >> >>for root, dirs, files in os.walk(directory): >> for x in files: >> print os.path.join(root,x) Thankyou Ryan and Jacob, I obviously did not read the docs for os.walk correctly, many thanks. >>Alan G. >>personally I'd use find and sed in Linux, right tool >>for the job etc.. Yes I could use *nix tools for the job, but I want to do it in python to see if I can :) Thanks again everyone for your help. Nick . -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005 From flaxeater at yahoo.com Sun Jan 9 00:19:21 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Sun Jan 9 00:19:24 2005 Subject: [Tutor] German Tutorials auf Deutsch Message-ID: <20050108231921.53647.qmail@web54308.mail.yahoo.com> Jacob S. wrote: >## Spoken by Ara ## >Pardon to the non-german speaking (or readers) on the list. > >Guten Tag. Mein Deutsch ist nicht so gut (ich habe keinen Deutsche in sieben > > Good Day. My German is not so good (I have writen very little german in seven years) >Jahren geschreiben). Mann kann Python Tutorials auf Deutsch heir > > One can find Python Turorials in German here .. and .. and... >http://www.freenetpages.co.uk/hp/alan.gauld/german/index.htm >und >http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index_ger.html >und >http://starship.python.net/crew/gherman/publications/tut-de/ >finden. >I habe Alan Gauld Tutorial vorgelesen. Es is gut und es abdeckt zimliche >viele Themen. > > I have learned from Alan Gaulds Tutorial. It is good and is ? ? ? ?. ###This is where my coco the gorilla size german vocabulary meets it's end.### >Ara >######## Done with text ########## >Great! Another chance to try and decipher another language! >Guten Tag = Good Morning > > Guten Tag=Good Day #coloquially >Mein = My >Deutsch = German (In *this* case, I guess) >ist = is >nicht = not >so = so >gut = good >ich = I >habe = have >keinen = learned? > > keinen=little geschrieben=writen >Deutsche = German (I guess again) >in = in >sieben = seven? No, that can't be it... >Mann = many >kann = kind? (roughly?) >auf = of > > auf=in #in this case# >heir = here OR at? >und = and >finden = used as end of a list > > finden=find/found >Es = it > >That's all I can decipher.... > >Thanks for some fun, >Jacob > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > > __________________________________ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com From jeffpeery at yahoo.com Sun Jan 9 04:19:26 2005 From: jeffpeery at yahoo.com (Jeffrey Thomas Peery) Date: Sun Jan 9 04:19:50 2005 Subject: [Tutor] (no subject) Message-ID: <20050109031949.30D231E4008@bag.python.org> Hello I can't seem to get the IDLE to start up in my windows XP by clicking on the desktop icon. To start it I have to drop a .py file on the icon. Any ideas? Also I can't seem to get xp to recognize .py files belonging to python. Right now the icons for .py is a windows default and not the python icon. So if I double click on a script that I want to work on it doesn't open the idle. I have to open the script from IDLE. Any ideas on this problem? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050108/a8bff96a/attachment.html From jeffpeery at yahoo.com Sun Jan 9 05:34:54 2005 From: jeffpeery at yahoo.com (Jeffrey Thomas Peery) Date: Sun Jan 9 05:34:59 2005 Subject: [Tutor] XP and python 2.4, some progress Message-ID: <20050109043456.B3A491E4008@bag.python.org> I wasn't able to get the IDLE started in windows XP. I had it working then I upgraded to 2.4, then it didn't work so I switched back to 2.3, still didn't work so I'm back to 2.4. I did some looking around and I was able to get the IDLE started by setting the shortcut on my desktop to: C:\Python24\python.exe C:\Python24\Lib\idlelib\idle.pyw -n Not sure what this does. But is seems to get it going. What is going on here? Also I get a console that appears with this message - I have no idea what it means: Warning: configHandler.py - IdleConf.GetThemeDict - problem retrieving theme element 'builtin-background' For theme 'Jeff' Returning default value: '#ffffff' Anyhow if anyone can explain what is going on here I would appreciate the help. and is there anyway to get the IDLE icon in my windows start menu to work? Ok thanks for the help! Jeff -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050108/4fb7ce1a/attachment.htm From dyoo at hkn.eecs.berkeley.edu Sun Jan 9 10:48:51 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 9 10:48:56 2005 Subject: [Tutor] XP and python 2.4, some progress In-Reply-To: <20050109043456.B3A491E4008@bag.python.org> Message-ID: On Sat, 8 Jan 2005, Jeffrey Thomas Peery wrote: > I wasn't able to get the IDLE started in windows XP. I had it working then I > upgraded to 2.4, then it didn't work so I switched back to 2.3, still didn't > work so I'm back to 2.4. I did some looking around and I was able to get > the IDLE started by setting the shortcut on my desktop to: > > C:\Python24\python.exe C:\Python24\Lib\idlelib\idle.pyw -n Hi Jeff, Oh no, not again! *grin* > Not sure what this does. But is seems to get it going. What is going on > here? Also I get a console that appears with this message - I have no > idea what it means: > > Warning: configHandler.py - IdleConf.GetThemeDict - problem retrieving > theme element 'builtin-background' Ok, we've seen something like this before; what you are running into is probably the same thing. Mike and Jacob have found that IDLE broke on them when upgrading from Python 2.3 to Python 2.4. See the thread starting from: http://mail.python.org/pipermail/tutor/2004-December/033672.html It turns out that the bug has to do with the way IDLE now handles its configuration files. If you've made some special configuration (like color customization), the bug causes IDLE not to start up cleanly because the customized config files aren't compatible. To work around this, rename your '.idlerc/' directory to something else temporarily. The '.idlerc' directory should be somewhere in your home within the 'Documents and Settings' directory. The '.idlerc/' directory contains all the user-defined settings that you've made to IDLE, so if we hide it, IDLE should try to regenerate a clean set. After renaming it to something else, try restarting IDLE with the icon, and see if it comes up now. I hope this helps! From alan.gauld at freenet.co.uk Sun Jan 9 16:33:28 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 9 16:33:14 2005 Subject: [Tutor] (no subject) References: <20050109031949.30D231E4008@bag.python.org> Message-ID: <001a01c4f660$91848150$16c68651@xp> > Hello I can't seem to get the IDLE to start up in my windows XP by clicking > ... > Also I can't seem to get xp to recognize .py files belonging to python. This is all fixable but it suggests maybe other problems in the installation. Personally I'd recommend reinstalling Python and that should fix all the problems. Alan G. From davholla2002 at yahoo.co.uk Sun Jan 9 22:44:03 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sun Jan 9 22:44:06 2005 Subject: [Tutor] Crossword program In-Reply-To: <20050107222942.C1DBC1E4011@bag.python.org> Message-ID: <20050109214403.98143.qmail@web25407.mail.ukl.yahoo.com> I wrote a program to create crosswords in python. It is not perfect but it works, is there any open source place I can put this for it to be used by anyone who wants it ? (Subject to the gpl licence). Here is the code in case anyone is interested. from Tkinter import * #Crossword program David Holland class Crossword(Frame): def __init__(self, master): '''Default arguments''' Frame.__init__(self, master) self.grid() self.create_widgets() NUM_SQUARES = 169 EMPTY = " " global questionanswer questionanswer = {} global dictshort dictshort = {} def readdict(self): #get the dictionary from a file try: pickle_file = open(dictfile, "rb") dictread = cPickle.load(pickle_file) except: dictread = {} return dictread def writedict(self, dictent): #write dictionary to file pickle_file = open(dictfile, "wb") cPickle.dump(dictent, pickle_file) pickle_file.close() def showclues(self): #show clues on the screen questionanswer = self.readdict() textent = self.showinfo(questionanswer) #textent = questionanswer self.putinfo(textent) def showinfo(self, dictent): #make the clues look readable i = 0 listkeys = dictent.keys() #listkeys = listkeys.sort() textdisp = '' for item in listkeys: i = i+1 istr = str(i) + " " question = dictent[item] textdisp = textdisp + istr + " the question is " + question + " the answer is " + item + "\n" return textdisp def newcrosswordboard(self): #create a crossword board board = [] for square in range (NUM_SQUARES): board.append(EMPTY) return board # def newcrosswordboard(self): # #this is create a board with the numbers # board = [] # for square in range (NUM_SQUARES): # text = str(square) # board.append(text) # return board def deleteclue(self): #delete a clue try: numberentered = self.delete_ent.get() dictent = self.readdict() numberentered = int(numberentered) listkeys = dictent.keys() i = 1 clue = '' for item in listkeys: if numberentered == i: del dictent[item] clue = item break i = i +1 text = "Clue " + clue + " has been removed the list of clues now is :-" + "\n" self.writedict(dictent) moretext = self.showinfo(dictent) text = text + moretext except: text = "Please enter a number as a figure" self.putinfo(text) def create_widgets(self): #create GUI self.question_lbl = Label(self, text = "Enter the question ") self.question_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W) self.answer_lbl = Label(self, text = "Enter the answer") self.answer_lbl.grid(row = 1, column = 0, columnspan = 2, sticky = W) self.delete_lbl = Label(self, text = "Enter the number of a clue you want deleted") self.delete_lbl.grid(row = 2, column = 0, columnspan = 2, sticky = W) #entry widget for the question self.question_ent = Entry(self) self.question_ent.grid(row=0, column = 2, columnspan = 1, sticky = W) self.answer_ent = Entry(self) self.answer_ent.grid(row=1, column = 2, columnspan = 2, sticky = W) self.delete_ent = Entry(self) self.delete_ent.grid(row=2, column = 2, columnspan = 1, sticky = W) #button to add entries Button(self, text = "Click to add clues ", command = self.create_questionsanswer).grid(row = 0, column = 3, columnspan = 3) Button(self, text = "Click to show clues ", command = self.showclues).grid(row = 1, column = 3, columnspan = 3) Button(self, text = "Click to delete clue ", command = self.deleteclue).grid(row = 2, column = 3, columnspan = 3) #button to create the cross word Button(self, text = "Click to create crossword ", command = self.crosswordcreation).grid(row = 16, column = 0, columnspan = 4) Button(self, text = "Click to get help ", command = self.helpme).grid(row = 16, column = 3, columnspan = 4) self.info_txt = Text(self, width = 80, height = 30, wrap = WORD) self.info_txt.grid(row = 20, column = 0, columnspan = 4) #show help self.helpme() def helpme(self): #get help helptext = 'To create crosswords add words by entering where it says enter the question and answer, then press the button "click to add clues." ' + '\n' helptext = helptext + 'To see all clues click "show all clues".' helptext = helptext + 'to delete a clue enter its number and then click delete clue. ' helptext = helptext + 'To create a crossword click create crossword. The crossword will be saved in a file called cross.txt. ' helptext = helptext + '\n' + 'If you like the crossword save the file with a different name, create another crossword. ' helptext = helptext + '\n' +'Author David Richard Holland, this is released under the GPL licence' self.putinfo(helptext) def create_questionsanswer(self): #this creates the question questionanswer = self.readdict() question = self.question_ent.get() question = self.converttoupper(question) answer = self.answer_ent.get() answer = self.converttoupper(answer) textent = "This has been added to list" #the code below does not work properly if answer not in questionanswer and question not in questionanswer: questionanswer[answer] = question self.writedict(questionanswer) self.putinfo(textent) elif answer in questionanswer: self.termexists(answer) #########print "line 38" elif question in questionanswer: self.termexists(question) def converttoupper(self, texttoupper): #conver string to upper text try: texttoupper = str(texttoupper) texttoupper = string.upper(texttoupper) except: texttoupper = texttoupper return texttoupper def termexists(self, answer): textent = answer, 'is already an answer for the crossword enter another' self.putinfo(textent) def putinfo(self, textent): #put the info to the GUI self.info_txt.delete(0.0, END) self.info_txt.insert(0.0,textent) def savecross(self, textent): file = open('cross.txt','w') file.write(textent) file.close() def crosswordcreation(self): #get the board info from create crossword board = self.createcrossword() board2 = self.createcrossword() numhori = 0 numvert = 0 #########print "crossword creationcalled" questionanswer = self.readdict() #here put in code to put the answers from the dictionary in the board #code goes here #first sort the dictionary into 2 one with values > 4 in len the other one everything else dictshort = {} dictshort, dictlong = self.changedict(questionanswer, dictshort) #########print "dictionary changed" #variables for the questions diagtext = 'Across ' + '\n' verttext = 'Down '+ '\n' badquestion = 'These could not be asked' + '\n' #code to populate the board board, board2,numhori, numvert, diagtext, verttext, badquestion = self.putwordboard(dictlong, board, board2, numhori, numvert, diagtext, verttext, badquestion) board2, board2, numhori, numvert, diagtext, verttext, badquestion = self.putwordboard(dictshort, board, board2, numhori, numvert, diagtext, verttext, badquestion) #code to show get the board as a picture text = self.display_board(board) text = text + "\n" textnew = self.display_board(board2) text = text + textnew text = text + diagtext + verttext + badquestion #+ str(board) #board2 is the board that shows the clues #text2 = self.display_board(board3) #text = text + text2 #save the cross word self.savecross(text) #display to screen self.putinfo(text) print "finished" def putwordboard(self, dict, board, board2, numhori, numvert, diagtext, verttext, badquestion): listkeys = dict.keys() #this takes the words and puts them in the crossword via sub functions item = random.choice(listkeys) while len(listkeys) > 0: # for item in listkeys: print item # emptylist, fulllist = self.createlist(board) board, board2, canbeentered, numhori, numvert, numbermove = self.putcharboard(item, board, board2, numhori, numvert) question = dict[item] question = string.capitalize(question) question = ' ' + question lenquestion = len(item) lenquestion = str(lenquestion) lenquestion = ' ' + lenquestion + " letters long" + '\n' if numbermove == 13 and canbeentered == 'Yes': #numvert = int(numvert) #numvert = numvert + 1 numvert = str(numvert) verttext = verttext + numvert + question + lenquestion numvert = int(numvert) textent = item + " has been entered" elif numbermove == 1 and canbeentered == 'Yes': #numhori = int(numhori) #numhori = numhori + 1 numhori = str(numhori) diagtext = diagtext + numhori + question + lenquestion numhori = int(numhori) textent = item + " has been entered" else: badquestion = badquestion + question #self.putinfo(textent) #get a new random item and remove the old one from the list item, listkeys = self.changenummove(listkeys, item) return board, board2, numhori, numvert, diagtext, verttext, badquestion def createlist(self, board): emptylist = [] fulllist = [] #create a list of squares which are empty and square which are not #######print NUM_SQUARES for square in range (NUM_SQUARES): if board[square] == EMPTY: emptylist.append(square) else: fulllist.append(square) return emptylist, fulllist def createnumbermove(self): #create the number in which direction to move listchoice = [1,13] numbermove = random.choice(listchoice) return listchoice, numbermove def changenummove(self, listchoice, numbermove): #change the number in which direction should be moved #make the list of options smaller #a = str(numbermove) #listchoice.remove(a) #the above does not work so this loop should do i = 0 for item in listchoice: if item == numbermove: break else: i = i+1 del listchoice[i] if len(listchoice) >0: numbermove = random.choice(listchoice) return numbermove, listchoice def putcharboard(self, word, board, board2, numhori, numvert): #see where the word can be put in the board #get a list of possible numbers that it can move and choose one randomly listchoice, numbermove = self.createnumbermove() j = 0 i = 0 lengthofword = len(word) canbeentered = 'No' ##########print "line 113" if len(USED_SQUARES) == 0: j = lengthofword #code to choose between going up or down #while canbeentered == 'No' and j 0 and canbeentered == 'No': ##print word, numbermove if len(full_list) > 0: try: canbeentered, char, j, square = self.canbeenteredparta( word, numbermove, canbeentered, lengthofword, board) except: square = 0 #as the word can not be entered change the direction of move if canbeentered == 'No' and len(listchoice) > 0: numbermove, listchoice = self.changenummove(listchoice, numbermove) list_choice_var = list_choice_var -1 if canbeentered == 'No': listchoice, numbermove = self.createnumbermove() ##print "listchoice is", listchoice, "numbermove is", numbermove j = 0 list_choice_var = len(listchoice) ##print "line 308 " #a loop to change numbermove randomly so that all possible positions are entered a sub function sees if this can be entered #this code is to get find an empty square randomly and see if the word can be entered k = 0 while list_choice_var >0 and canbeentered == 'No': canbeentered, word, char, j, square = self.canbeenteredpartb(j, word, numbermove, canbeentered, lengthofword, board) k = k +1 #print word, numbermove, "line 305", canbeentered if canbeentered == 'No' and len(listchoice) > 0: #if you can not enter it get a new value for the position it can be in ##print "line 316" numbermove, listchoice = self.changenummove(listchoice, numbermove) #print "line 318" #print "line 308", numbermove #reduce the variable by 1 list_choice_var = list_choice_var -1 #print "k is", k if canbeentered == 'Yes': #calls a function that puts the word in the board board = self.putwordinboard(word, board, char, square, j, numbermove, lengthofword) if numbermove == 1: numhori = numhori +1 newword = self.createword(lengthofword, j, numhori) #create a new word ie a blank word numhori and numvert are the numbers of the questions board2 = self.putwordinboard(newword, board2, numhori, square, j, numbermove, lengthofword) else: numvert = numvert + 1 newword = self.createword(lengthofword, j, numvert) board2 = self.putwordinboard(newword, board2, numvert, square, j, numbermove, lengthofword) return board, board2, canbeentered, numhori, numvert, numbermove def makecharpretty(self, char): #make char go in the crossword better in one place so that the code can be maintained better char = ' '+char+' ' return char def canbeenteredparta(self, word, numbermove, canbeentered, lengthofword, board): #this sees if the word can be enterd somewhere it already exists word = str(word) lengthofwordnew = lengthofword #is a variable to show what letter in the word is the one to start from j = 0 for char in word: if canbeentered == 'Yes': break emptylist, full_list = self.createlist(board) if len(full_list) > 0: square = random.choice(full_list) else: break while len(full_list) > 0: if canbeentered == 'Yes': break letter = self.makecharpretty(char) if board[square] == letter: #####print "square is the same as letter", char canbeentered = self.canbeentered(word, board, char, square, j, numbermove) #if it can be entered break the while loop if canbeentered == 'Yes': break ######print "line 364" elif canbeentered == 'No': square, full_list = self.changenummove(full_list, square) #######print "line 365" if canbeentered == 'Yes': break #it can be entered so break the for loop as well as the while otherwise increase j else: j = j +1 return canbeentered, char, j, square def canbeenteredpartb(self, j, word, numbermove, canbeentered, lengthofword, board): #initialise variables of course these will not be created if the loop is not called char = '' #i = 0 square = '' for char in word: i = 0 #####print "char", char, "is being tested", "canbeentered = ", canbeentered copyemptylist, fullist = self.createlist(board) #######print "char is", char,"word is", word #######print "empty list is", copyemptylist if len(copyemptylist) > 0: square = random.choice(copyemptylist) #get a random square then start the loop while len(copyemptylist) > 0: canbeentered = self.canbeentered(word, board, char, square, j, numbermove) #########print canbeentered if canbeentered == 'Yes': break ######print "canbeentered is", canbeentered else: #if it can be entered we break other wise get a new square #######print "line 428", square square, copyemptylist = self.changenummove(copyemptylist, square) i = i +1 ##print i #if this char can be entered break the for loop as well if canbeentered == 'Yes': break return canbeentered, word, char, j, square else: j = j +1 #break return canbeentered, word, char, j, square def createword(self, lengthofword, numinword, char): numforward = (lengthofword - numinword) -1 i = 0 char = str(char) charnew = '-' while i < numforward: char = char + charnew i = i + 1 i = 0 numback = numinword if numback > 0: while i < numback: char = char + charnew i = i +1 ###print "line 423" newword = char ###print "lengthofword is", lengthofword, "numinword", numinword, "new word", newword ###print "lenght of new word", len(newword) return newword def putwordinboard(self, word, board, char, numsquare, numinword, numbermove, lengthofword): #we know that this can be entered or it would not be called #enter the code #start from here #get the lengths #########print "putwordinboard called numbermove is", numbermove numforward = lengthofword - numinword numback = numinword board = self.putwordinboardpart2(word, board, numsquare, numinword, numbermove, numforward, numback) #########print "line 167" return board def putwordinboardpart2(self, word, board, numsquare, numinword, numbermove, numforward, numback): i = 0 # numinword = (int(numinword)) -1 numinword = (int(numinword)) originalnumberinword = numinword numbermove = int(numbermove) #########print "numbermove is", numbermove numsquareorig = numsquare #########print "putwordinboardpart2 called" while i < numforward: try: char = word[numinword] except: print "char", "numinword is", numinword, "lengtho of word", len(word), "word is", word char = self.makecharpretty(char) #if board[numsquare] == EMPTY: board[numsquare] = char numinword = numinword +1 i = i+1 global USED_SQUARES USED_SQUARES.append(numsquare) numsquare = numsquare + numbermove i = -1 numinword = originalnumberinword numsquare = numsquareorig if numback > 0: while i < numback: char = word[numinword] char = ' '+char+' ' numinword = numinword -1 board[numsquare] = char numsquare = numsquare - numbermove global USED_SQUARES USED_SQUARES.append(numsquare) #the above is to record all squares that have been used i = i+1 return board def canbeentered(self, word, board, char, numsquare, numinword, numbermove): #this is designed to move find out if the word can be entered in a straight #horozontial or vertical line #numbermove decides if it is horozontial or vertical, if it is 1 it is horozontial #13 if it is vertical #this is the brains of the crossword creation if this is fine everything else should be lengthofword = len(word) # positivemove = lengthofword - numinword this was changed because the first char is number 0 therefore # only have to move the same length -1 in v46 positivemove = (lengthofword - numinword)-1 negativemove = numinword #we do not want the first one to be empty because it might already be occupied numsquarenew = numsquare + numbermove #if it is horozontial we do not want it to be anything but a straight line therefore it #should only work if the first number is an exact multiple of 13 and the last number the first number + 12 if numbermove == 1: moveback = numsquare /13 moveback = moveback * 13 moveforward= moveback + 12 #changed movefoward to moveback + 12 in v30 and before to allow 13 letter words to be added else: moveback = 0 #moveforward should be changed moveforward = 168 enteredyesno = 'Yes' if numsquarenew > moveforward: enteredyesno = 'No' i = 0 if enteredyesno == 'Yes': enteredyesno = self.canbeenteredpart2(numsquare, numbermove, moveback, moveforward, positivemove, board) #if this function shows that going forward is okay #try going back back if enteredyesno == 'Yes' and negativemove > 0: numbermove = - numbermove try: enteredyesno = self.canbeenteredpart2(numsquare, numbermove, moveback, moveforward, negativemove, board) except: enteredyesno = 'No' #the constant enteredyesno means that it can or not be entered or rangesquares = NUM_SQUARES -1 numsquare = numsquare + numbermove return enteredyesno def canbeenteredpart2(self, numsquare, numbermove, moveback, moveforward, amounttomove, board): #i and j are constants that increase in the loop, everytime the loop goes i increases #j only increases if it meets certain conditions i = 0 j = 0 #code to ensure space if board[numsquare-1] != EMPTY: j = 3 numsquare = numsquare + numbermove enteredyesno = 'No' while i == j and i < amounttomove: i = i+1 #in case of error set j to 0 so the program can continue try: if board[numsquare] == EMPTY: j = j+1 #the line above is to see if it can be entered #below is to give space if board[numsquare+1] != EMPTY: j = 0 # if board[numsquare+13] != EMPTY: # j = 0 except: j = 0 #######print "error a mistake" if numsquare > moveforward: j = 0 if numsquare < moveback: j = 0 #look for the new square numsquare = numsquare + numbermove if i == j and i == amounttomove: enteredyesno = 'Yes' #code to ensure space try: if board[numsquare+1] != EMPTY and enteredyesno == 'Yes': enteredyesno = 'Yes' except: #print "errored line 578" enteredyesno = 'No' return enteredyesno def changedict(self, questionanswer, dictshort): #split the dictionary into 2 one with long answers one with short answers #this ensures the long ones get put in first lengthofdict = len(questionanswer) dictlong = questionanswer i = 0 #for key in dictlong: listkeys = dictlong.keys() #while i < lengthofdict: for item in listkeys: #######print "the loop is called" if len(item) < 10: definition = dictlong[item] #to short dictionary dictshort[item] = definition del dictlong[item] i = i + 1 return dictshort, dictlong def createcrossword(self): #this creates the crossword #first create a board board = self.newcrosswordboard() #return the board return board def display_board(self, board): #this creates the text to be displayed textline = '' i = 0 while i < 168: textline, i = self.displayboardpart2(board, i, textline) #textline = textline return textline def displayboardpart2(self, board, i, text): k = i j = i+13 while i < j: text = text + board[i] + "|" i = i+1 text = text + "\n" while k Message-ID: <002f01c4f697$9dda98e0$16c68651@xp> Hi David, Its probably the kind of thing you could put on the Useless Python website. Useless is a collection of not-too-serious software written in Python which, despite the name, is actually quite useful for beginners to download and look at and learn. They can try improving it, or using the ideas for their own programs. But you might well get some suggestions on tidying it up a little first... Alan G. ----- Original Message ----- From: "David Holland" To: Sent: Sunday, January 09, 2005 9:44 PM Subject: [Tutor] Crossword program > I wrote a program to create crosswords in python. > It is not perfect but it works, is there any open > source place I can put this for it to be used by > anyone who wants it ? (Subject to the gpl licence). > Here is the code in case anyone is interested. > > from Tkinter import * > #Crossword program David Holland > class Crossword(Frame): > > def __init__(self, master): > '''Default arguments''' > Frame.__init__(self, master) > self.grid() > self.create_widgets() > NUM_SQUARES = 169 > EMPTY = " " > global questionanswer > questionanswer = {} > global dictshort > dictshort = {} > > def readdict(self): > #get the dictionary from a file > try: > pickle_file = open(dictfile, "rb") > dictread = cPickle.load(pickle_file) > except: > dictread = {} > return dictread > > def writedict(self, dictent): > #write dictionary to file > pickle_file = open(dictfile, "wb") > cPickle.dump(dictent, pickle_file) > pickle_file.close() > > def showclues(self): > #show clues on the screen > questionanswer = self.readdict() > textent = self.showinfo(questionanswer) > #textent = questionanswer > self.putinfo(textent) > > def showinfo(self, dictent): > #make the clues look readable > i = 0 > listkeys = dictent.keys() > #listkeys = listkeys.sort() > textdisp = '' > for item in listkeys: > i = i+1 > istr = str(i) + " " > question = dictent[item] > textdisp = textdisp + istr + " the > question is " + question + " the answer is " + item + > "\n" > return textdisp > > def newcrosswordboard(self): > #create a crossword board > board = [] > for square in range (NUM_SQUARES): > board.append(EMPTY) > return board > > # def newcrosswordboard(self): > # #this is create a board with the numbers > # board = [] > # for square in range (NUM_SQUARES): > # text = str(square) > # board.append(text) > # return board > > def deleteclue(self): > #delete a clue > try: > numberentered = self.delete_ent.get() > dictent = self.readdict() > numberentered = int(numberentered) > listkeys = dictent.keys() > i = 1 > clue = '' > for item in listkeys: > if numberentered == i: > del dictent[item] > clue = item > break > i = i +1 > text = "Clue " + clue + " has been removed > the list of clues now is :-" + "\n" > self.writedict(dictent) > moretext = self.showinfo(dictent) > text = text + moretext > except: > text = "Please enter a number as a figure" > self.putinfo(text) > > def create_widgets(self): > #create GUI > self.question_lbl = Label(self, text = "Enter > the question ") > self.question_lbl.grid(row = 0, column = 0, > columnspan = 2, sticky = W) > self.answer_lbl = Label(self, text = "Enter > the answer") > self.answer_lbl.grid(row = 1, column = 0, > columnspan = 2, sticky = W) > self.delete_lbl = Label(self, text = "Enter > the number of a clue you want deleted") > self.delete_lbl.grid(row = 2, column = 0, > columnspan = 2, sticky = W) > #entry widget for the question > self.question_ent = Entry(self) > self.question_ent.grid(row=0, column = 2, > columnspan = 1, sticky = W) > self.answer_ent = Entry(self) > self.answer_ent.grid(row=1, column = 2, > columnspan = 2, sticky = W) > self.delete_ent = Entry(self) > self.delete_ent.grid(row=2, column = 2, > columnspan = 1, sticky = W) > > #button to add entries > Button(self, text = "Click to add clues ", > command = self.create_questionsanswer).grid(row = 0, > column = 3, columnspan = 3) > Button(self, text = "Click to show clues ", > command = self.showclues).grid(row = 1, column = 3, > columnspan = 3) > Button(self, text = "Click to delete clue ", > command = self.deleteclue).grid(row = 2, column = 3, > columnspan = 3) > #button to create the cross word > Button(self, text = "Click to create crossword > ", command = self.crosswordcreation).grid(row = 16, > column = 0, columnspan = 4) > Button(self, text = "Click to get help ", > command = self.helpme).grid(row = 16, column = 3, > columnspan = 4) > self.info_txt = Text(self, width = 80, height > = 30, wrap = WORD) > self.info_txt.grid(row = 20, column = 0, > columnspan = 4) > #show help > self.helpme() > > def helpme(self): > #get help > helptext = 'To create crosswords add words by > entering where it says enter the question and answer, > then press the button "click to add clues." ' + '\n' > helptext = helptext + 'To see all clues click > "show all clues".' > helptext = helptext + 'to delete a clue enter > its number and then click delete clue. ' > helptext = helptext + 'To create a crossword > click create crossword. The crossword will be saved > in a file called cross.txt. ' > helptext = helptext + '\n' + 'If you like the > crossword save the file with a different name, create > another crossword. ' > helptext = helptext + '\n' +'Author David > Richard Holland, this is released under the GPL > licence' > self.putinfo(helptext) > > def create_questionsanswer(self): > #this creates the question > questionanswer = self.readdict() > question = self.question_ent.get() > question = self.converttoupper(question) > answer = self.answer_ent.get() > answer = self.converttoupper(answer) > textent = "This has been added to list" > #the code below does not work properly > if answer not in questionanswer and question > not in questionanswer: > questionanswer[answer] = question > self.writedict(questionanswer) > self.putinfo(textent) > elif answer in questionanswer: > self.termexists(answer) > #########print "line 38" > elif question in questionanswer: > self.termexists(question) > > > > def converttoupper(self, texttoupper): > #conver string to upper text > try: > texttoupper = str(texttoupper) > texttoupper = string.upper(texttoupper) > except: > texttoupper = texttoupper > return texttoupper > > def termexists(self, answer): > textent = answer, 'is already an answer for > the crossword enter another' > self.putinfo(textent) > > def putinfo(self, textent): > #put the info to the GUI > self.info_txt.delete(0.0, END) > self.info_txt.insert(0.0,textent) > > def savecross(self, textent): > file = open('cross.txt','w') > file.write(textent) > file.close() > > def crosswordcreation(self): > #get the board info from create crossword > board = self.createcrossword() > board2 = self.createcrossword() > numhori = 0 > numvert = 0 > #########print "crossword creationcalled" > questionanswer = self.readdict() > #here put in code to put the answers from the > dictionary in the board > #code goes here > #first sort the dictionary into 2 one with > values > 4 in len the other one everything else > dictshort = {} > dictshort, dictlong = > self.changedict(questionanswer, dictshort) > #########print "dictionary changed" > #variables for the questions > diagtext = 'Across ' + '\n' > verttext = 'Down '+ '\n' > badquestion = 'These could not be asked' + > '\n' > #code to populate the board > board, board2,numhori, numvert, diagtext, > verttext, badquestion = self.putwordboard(dictlong, > board, board2, numhori, numvert, diagtext, verttext, > badquestion) > board2, board2, numhori, numvert, diagtext, > verttext, badquestion = self.putwordboard(dictshort, > board, board2, numhori, numvert, diagtext, verttext, > badquestion) > #code to show get the board as a picture > text = self.display_board(board) > text = text + "\n" > textnew = self.display_board(board2) > text = text + textnew > text = text + diagtext + verttext + > badquestion #+ str(board) > #board2 is the board that shows the clues > #text2 = self.display_board(board3) > #text = text + text2 > #save the cross word > self.savecross(text) > #display to screen > self.putinfo(text) > print "finished" > > > def putwordboard(self, dict, board, board2, > numhori, numvert, diagtext, verttext, badquestion): > listkeys = dict.keys() > #this takes the words and puts them in the > crossword via sub functions > item = random.choice(listkeys) > while len(listkeys) > 0: > # for item in listkeys: > print item > # emptylist, fulllist = > self.createlist(board) > board, board2, canbeentered, numhori, > numvert, numbermove = self.putcharboard(item, board, > board2, numhori, numvert) > question = dict[item] > question = string.capitalize(question) > question = ' ' + question > lenquestion = len(item) > lenquestion = str(lenquestion) > lenquestion = ' ' + lenquestion + " > letters long" + '\n' > if numbermove == 13 and canbeentered == > 'Yes': > #numvert = int(numvert) > #numvert = numvert + 1 > numvert = str(numvert) > > verttext = verttext + numvert + > question + lenquestion > numvert = int(numvert) > textent = item + " has been entered" > elif numbermove == 1 and canbeentered == > 'Yes': > #numhori = int(numhori) > #numhori = numhori + 1 > numhori = str(numhori) > diagtext = diagtext + numhori + > question + lenquestion > numhori = int(numhori) > textent = item + " has been entered" > else: > badquestion = badquestion + question > > #self.putinfo(textent) > #get a new random item and remove the old > one from the list > item, listkeys = > self.changenummove(listkeys, item) > return board, board2, numhori, numvert, > diagtext, verttext, badquestion > > def createlist(self, board): > emptylist = [] > fulllist = [] > #create a list of squares which are empty and > square which are not > #######print NUM_SQUARES > for square in range (NUM_SQUARES): > if board[square] == EMPTY: > emptylist.append(square) > else: > fulllist.append(square) > return emptylist, fulllist > > > def createnumbermove(self): > #create the number in which direction to move > listchoice = [1,13] > numbermove = random.choice(listchoice) > return listchoice, numbermove > > def changenummove(self, listchoice, numbermove): > #change the number in which direction should > be moved > #make the list of options smaller > #a = str(numbermove) > #listchoice.remove(a) > #the above does not work so this loop should > do > i = 0 > for item in listchoice: > if item == numbermove: > break > else: > i = i+1 > del listchoice[i] > if len(listchoice) >0: > numbermove = random.choice(listchoice) > return numbermove, listchoice > > def putcharboard(self, word, board, board2, > numhori, numvert): > #see where the word can be put in the board > #get a list of possible numbers that it can > move and choose one randomly > listchoice, numbermove = > self.createnumbermove() > j = 0 > i = 0 > lengthofword = len(word) > canbeentered = 'No' > ##########print "line 113" > if len(USED_SQUARES) == 0: > j = lengthofword > #code to choose between going up or down > #while canbeentered == 'No' and j > #while canbeentered == 'No' and j > #call a function that will see if it can be > entered where that a letter in the word already is > emptylist, full_list = self.createlist(board) > #create a variable which means that it loops > for the number of different options in numbermove > list_choice_var = len(listchoice) +1 > > while list_choice_var >0 and canbeentered == > 'No': > ##print word, numbermove > if len(full_list) > 0: > try: > canbeentered, char, j, square = > self.canbeenteredparta( word, numbermove, > canbeentered, lengthofword, board) > except: > square = 0 > #as the word can not be entered change the > direction of move > if canbeentered == 'No' and > len(listchoice) > 0: > numbermove, listchoice = > self.changenummove(listchoice, numbermove) > list_choice_var = list_choice_var -1 > if canbeentered == 'No': > listchoice, numbermove = > self.createnumbermove() > ##print "listchoice is", listchoice, > "numbermove is", numbermove > j = 0 > list_choice_var = len(listchoice) > ##print "line 308 " > #a loop to change numbermove randomly so that > all possible positions are entered a sub function > sees if this can be entered > #this code is to get find an empty square > randomly and see if the word can be entered > k = 0 > while list_choice_var >0 and canbeentered == > 'No': > canbeentered, word, char, j, square = > self.canbeenteredpartb(j, word, numbermove, > canbeentered, lengthofword, board) > k = k +1 > #print word, numbermove, "line 305", > canbeentered > if canbeentered == 'No' and > len(listchoice) > 0: > #if you can not enter it get a new > value for the position it can be in > ##print "line 316" > numbermove, listchoice = > self.changenummove(listchoice, numbermove) > #print "line 318" > #print "line 308", numbermove > #reduce the variable by 1 > list_choice_var = list_choice_var -1 > #print "k is", k > if canbeentered == 'Yes': > #calls a function that puts the word in > the board > board = self.putwordinboard(word, board, > char, square, j, numbermove, lengthofword) > > if numbermove == 1: > numhori = numhori +1 > newword = > self.createword(lengthofword, j, numhori) > #create a new word ie a blank word > numhori and numvert are the numbers of the questions > board2 = self.putwordinboard(newword, > board2, numhori, square, j, numbermove, lengthofword) > else: > numvert = numvert + 1 > newword = > self.createword(lengthofword, j, numvert) > board2 = self.putwordinboard(newword, > board2, numvert, square, j, numbermove, lengthofword) > > return board, board2, canbeentered, numhori, > numvert, numbermove > > > def makecharpretty(self, char): > #make char go in the crossword better in one > place so that the code can be maintained better > char = ' '+char+' ' > return char > > def canbeenteredparta(self, word, numbermove, > canbeentered, lengthofword, board): > #this sees if the word can be enterd somewhere > it already exists > word = str(word) > lengthofwordnew = lengthofword > #is a variable to show what letter in the word > is the one to start from > j = 0 > for char in word: > if canbeentered == 'Yes': > break > emptylist, full_list = > self.createlist(board) > if len(full_list) > 0: > square = random.choice(full_list) > else: > break > while len(full_list) > 0: > if canbeentered == 'Yes': > break > letter = self.makecharpretty(char) > if board[square] == letter: > #####print "square is the same > as letter", char > canbeentered = > self.canbeentered(word, board, char, square, j, > numbermove) > #if it can be entered break the while > loop > if canbeentered == 'Yes': > break > ######print "line 364" > elif canbeentered == 'No': > square, full_list = > self.changenummove(full_list, square) > #######print "line 365" > if canbeentered == 'Yes': > break > #it can be entered so break the for loop > as well as the while otherwise increase j > else: > j = j +1 > return canbeentered, char, j, square > > def canbeenteredpartb(self, j, word, numbermove, > canbeentered, lengthofword, board): > #initialise variables of course these will not > be created if the loop is not called > char = '' > #i = 0 > square = '' > for char in word: > i = 0 > #####print "char", char, "is being > tested", "canbeentered = ", canbeentered > copyemptylist, fullist = > self.createlist(board) > #######print "char is", char,"word is", > word > #######print "empty list is", > copyemptylist > if len(copyemptylist) > 0: > square = random.choice(copyemptylist) > #get a random square then start the loop > > while len(copyemptylist) > 0: > canbeentered = self.canbeentered(word, > board, char, square, j, numbermove) > #########print > canbeentered > if canbeentered == 'Yes': > break > ######print "canbeentered is", > canbeentered > else: > #if it can be entered we break other > wise get a new square > #######print "line 428", square > square, copyemptylist = > self.changenummove(copyemptylist, square) > i = i +1 > ##print i > #if this char can be entered break the for > loop as well > if canbeentered == 'Yes': > break > return canbeentered, word, char, > j, square > else: > j = j +1 > #break > return canbeentered, word, char, j, square > > > def createword(self, lengthofword, numinword, > char): > numforward = (lengthofword - numinword) -1 > i = 0 > char = str(char) > charnew = '-' > while i < numforward: > char = char + charnew > i = i + 1 > i = 0 > numback = numinword > if numback > 0: > while i < numback: > char = char + charnew > i = i +1 > ###print "line 423" > newword = char > ###print "lengthofword is", lengthofword, > "numinword", numinword, "new word", newword > ###print "lenght of new word", len(newword) > return newword > > > > > def putwordinboard(self, word, board, char, > numsquare, numinword, numbermove, lengthofword): > #we know that this can be entered or it would > not be called > #enter the code > #start from here > #get the lengths > #########print "putwordinboard called > numbermove is", numbermove > numforward = lengthofword - numinword > numback = numinword > board = self.putwordinboardpart2(word, board, > numsquare, numinword, numbermove, numforward, numback) > #########print "line 167" > return board > > def putwordinboardpart2(self, word, board, > numsquare, numinword, numbermove, numforward, > numback): > i = 0 > # numinword = (int(numinword)) -1 > numinword = (int(numinword)) > originalnumberinword = numinword > numbermove = int(numbermove) > #########print "numbermove is", numbermove > numsquareorig = numsquare > #########print "putwordinboardpart2 called" > while i < numforward: > try: > char = word[numinword] > except: > print "char", "numinword is", > numinword, "lengtho of word", len(word), "word is", > word > char = self.makecharpretty(char) > #if board[numsquare] == EMPTY: > board[numsquare] = char > numinword = numinword +1 > i = i+1 > global USED_SQUARES > USED_SQUARES.append(numsquare) > numsquare = numsquare + numbermove > > i = -1 > numinword = originalnumberinword > numsquare = numsquareorig > if numback > 0: > while i < numback: > char = word[numinword] > char = ' '+char+' ' > numinword = numinword -1 > board[numsquare] = char > numsquare = numsquare - numbermove > global USED_SQUARES > USED_SQUARES.append(numsquare) > #the above is to record all squares > that have been used > i = i+1 > return board > > def canbeentered(self, word, board, char, > numsquare, numinword, numbermove): > #this is designed to move find out if the word > can be entered in a straight > #horozontial or vertical line > #numbermove decides if it is horozontial or > vertical, if it is 1 it is horozontial > #13 if it is vertical > #this is the brains of the crossword creation > if this is fine everything else should be > lengthofword = len(word) > # positivemove = lengthofword - numinword this > was changed because the first char is number 0 > therefore > # only have to move the same length -1 in v46 > positivemove = (lengthofword - numinword)-1 > negativemove = numinword > #we do not want the first one to be empty > because it might already be occupied > numsquarenew = numsquare + numbermove > #if it is horozontial we do not want it to be > anything but a straight line therefore it > #should only work if the first number is an > exact multiple of 13 and the last number the first > number + 12 > if numbermove == 1: > moveback = numsquare /13 > moveback = moveback * 13 > moveforward= moveback + 12 > #changed movefoward to moveback + 12 in > v30 and before to allow 13 letter words to be added > else: > moveback = 0 > #moveforward should be changed > moveforward = 168 > enteredyesno = 'Yes' > if numsquarenew > moveforward: > enteredyesno = 'No' > i = 0 > if enteredyesno == 'Yes': > enteredyesno = > self.canbeenteredpart2(numsquare, numbermove, > moveback, moveforward, positivemove, board) > #if this function shows that going forward is > okay > #try going back back > if enteredyesno == 'Yes' and negativemove > 0: > numbermove = - numbermove > try: > enteredyesno = > self.canbeenteredpart2(numsquare, numbermove, > moveback, moveforward, negativemove, board) > except: > enteredyesno = 'No' > #the constant enteredyesno means that it can > or not be entered or > rangesquares = NUM_SQUARES -1 > numsquare = numsquare + numbermove > > return enteredyesno > > > def canbeenteredpart2(self, numsquare, numbermove, > moveback, moveforward, amounttomove, board): > #i and j are constants that increase in the > loop, everytime the loop goes i increases > #j only increases if it meets certain > conditions > i = 0 > j = 0 > #code to ensure space > if board[numsquare-1] != EMPTY: > j = 3 > numsquare = numsquare + numbermove > enteredyesno = 'No' > while i == j and i < amounttomove: > i = i+1 > #in case of error set j to 0 so the > program can continue > try: > if board[numsquare] == EMPTY: > j = j+1 > #the line above is to see if it can be > entered > #below is to give space > if board[numsquare+1] != EMPTY: > j = 0 > > # if board[numsquare+13] != EMPTY: > # j = 0 > except: > j = 0 > #######print "error a mistake" > if numsquare > moveforward: > j = 0 > if numsquare < moveback: > j = 0 > #look for the new square > numsquare = numsquare + numbermove > > if i == j and i == amounttomove: > enteredyesno = 'Yes' > #code to ensure space > try: > if board[numsquare+1] != EMPTY and > enteredyesno == 'Yes': > enteredyesno = 'Yes' > except: > #print "errored line 578" > enteredyesno = 'No' > return enteredyesno > > def changedict(self, questionanswer, dictshort): > #split the dictionary into 2 one with long > answers one with short answers > #this ensures the long ones get put in first > lengthofdict = len(questionanswer) > dictlong = questionanswer > i = 0 > #for key in dictlong: > listkeys = dictlong.keys() > #while i < lengthofdict: > for item in listkeys: > #######print "the loop is called" > if len(item) < 10: > definition = dictlong[item] > #to short dictionary > dictshort[item] = definition > del dictlong[item] > i = i + 1 > return dictshort, dictlong > > def createcrossword(self): > #this creates the crossword > #first create a board > board = self.newcrosswordboard() > #return the board > return board > > > def display_board(self, board): > #this creates the text to be displayed > textline = '' > i = 0 > while i < 168: > textline, i = > self.displayboardpart2(board, i, textline) > #textline = textline > return textline > > def displayboardpart2(self, board, i, text): > k = i > j = i+13 > while i < j: > text = text + board[i] + "|" > i = i+1 > text = text + "\n" > while k text = text + "----" > k = k +1 > text = text + "\n" > return text, i > > > > import random > import cPickle, shelve > import string > dictfile = "crossworddict.dat" > NUM_SQUARES = 169 > EMPTY = " " > global USED_SQUARES > USED_SQUARES = [] > root = Tk() > root.title('Create crosswords') > app = Crossword(root) > root.mainloop() > > > > > > > ___________________________________________________________ > ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com > > From ismaelgf at adinet.com.uy Sun Jan 9 23:33:34 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Sun Jan 9 23:33:10 2005 Subject: [Tutor] Input to python executable code and design question Message-ID: <41E1B13E.7030203@adinet.com.uy> Hello I am trying to make a program that will plot functions. For that, I need to be able to get an input (the function to be plotted) and execute it. So, my question is, how do I use the input? I have found no way to convert the string to some kind of executable code. I did research the problem. And found two things, first, an unsatisfactory solution from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52217 The code there, by means I cannot understand at all, executes a string as code. To be usable I have to make my code a huge string. It is not very elegant. The second possible solution I found was using eval, compile and/or exec. But I do not understand what do they do, or how to use them, for the matter. Related to the program I intend to do, which design would you say is more intelligent: one that first calculates the whole function, stores it, and then plots it; or one that calculates-plots-calc.-plot, and so on? Thanks for any information, and for your time. Ismael From jfouhy at paradise.net.nz Mon Jan 10 00:52:24 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Mon Jan 10 00:52:53 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <41E1B13E.7030203@adinet.com.uy> References: <41E1B13E.7030203@adinet.com.uy> Message-ID: <1105314744.41e1c3b85ae5a@www.paradise.net.nz> Quoting Ismael Garrido : > I am trying to make a program that will plot functions. For that, I need > to be able to get an input (the function to be plotted) and execute it. > So, my question is, how do I use the input? I have found no way to > convert the string to some kind of executable code. So you want the user to be able to type something like "f(x) = sin(2*x)" and then your program will plot it --- is that correct? Maybe you could parse it yourself? I have found SimpleParse quite easy to use --- http://simpleparse.sourceforge.net/ . You will need to write your own grammar to describe functions --- something like this, I guess: eqn := fname, '(', varlist, ')=', expr varlist := var, (',', var)* expr := atom, (binop, atom)? atom := var / (fun, '(', expr, ')') / num fun := 'sin' / 'cos' / 'tan' / ... var := char fname := char+ num := digit+ binop := '+' / '*' / '/' / '-' char := [a-zA-Z] digit := [0-9] although you will need to work on that to get the precedence right and avoid undesired recursion and the like. SimpleParse will give you a tree (made of nested tuples) representing your function. You can then have a go at converting the tree to a function. I guess the standard way to do this would be something like: def convert(node): functionName = node[0] children = node[1] if functionName == '*': return convert(children[0]) * convert(children[1]) elif functionName == '+': ... But you may be able to come up with something more clever. Hope this helps. -- John. From jkmacman at yahoo.com Mon Jan 10 00:55:40 2005 From: jkmacman at yahoo.com (Jim Kelly) Date: Mon Jan 10 00:55:43 2005 Subject: [Tutor] Re: Tutor Digest, Vol 11, Issue 30 In-Reply-To: <20050109220716.054E61E4014@bag.python.org> Message-ID: <20050109235540.34265.qmail@web50004.mail.yahoo.com> This script did not run properly jk nj --- tutor-request@python.org wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body > 'help' to > tutor-request@python.org > > You can reach the person managing the list at > tutor-owner@python.org > > When replying, please edit your Subject line so it > is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Crossword program (Alan Gauld) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 9 Jan 2005 22:07:31 -0000 > From: "Alan Gauld" > Subject: Re: [Tutor] Crossword program > To: "David Holland" , > > Message-ID: <002f01c4f697$9dda98e0$16c68651@xp> > Content-Type: text/plain; charset="iso-8859-1" > > Hi David, > > Its probably the kind of thing you could put on the > Useless Python website. Useless is a collection of > not-too-serious software written in Python which, > despite the name, is actually quite useful for > beginners > to download and look at and learn. They can try > improving it, or using the ideas for their own > programs. > > But you might well get some suggestions on tidying > it up a little first... > > Alan G. > > ----- Original Message ----- > From: "David Holland" > To: > Sent: Sunday, January 09, 2005 9:44 PM > Subject: [Tutor] Crossword program > > > > I wrote a program to create crosswords in python. > > It is not perfect but it works, is there any open > > source place I can put this for it to be used by > > anyone who wants it ? (Subject to the gpl > licence). > > Here is the code in case anyone is interested. > > > > from Tkinter import * > > #Crossword program David Holland > > class Crossword(Frame): > > > > def __init__(self, master): > > '''Default arguments''' > > Frame.__init__(self, master) > > self.grid() > > self.create_widgets() > > NUM_SQUARES = 169 > > EMPTY = " " > > global questionanswer > > questionanswer = {} > > global dictshort > > dictshort = {} > > > > def readdict(self): > > #get the dictionary from a file > > try: > > pickle_file = open(dictfile, "rb") > > dictread = cPickle.load(pickle_file) > > except: > > dictread = {} > > return dictread > > > > def writedict(self, dictent): > > #write dictionary to file > > pickle_file = open(dictfile, "wb") > > cPickle.dump(dictent, pickle_file) > > pickle_file.close() > > > > def showclues(self): > > #show clues on the screen > > questionanswer = self.readdict() > > textent = self.showinfo(questionanswer) > > #textent = questionanswer > > self.putinfo(textent) > > > > def showinfo(self, dictent): > > #make the clues look readable > > i = 0 > > listkeys = dictent.keys() > > #listkeys = listkeys.sort() > > textdisp = '' > > for item in listkeys: > > i = i+1 > > istr = str(i) + " " > > question = dictent[item] > > textdisp = textdisp + istr + " the > > question is " + question + " the answer is " + > item + > > "\n" > > return textdisp > > > > def newcrosswordboard(self): > > #create a crossword board > > board = [] > > for square in range (NUM_SQUARES): > > board.append(EMPTY) > > return board > > > > # def newcrosswordboard(self): > > # #this is create a board with the numbers > > # board = [] > > # for square in range (NUM_SQUARES): > > # text = str(square) > > # board.append(text) > > # return board > > > > def deleteclue(self): > > #delete a clue > > try: > > numberentered = self.delete_ent.get() > > dictent = self.readdict() > > numberentered = int(numberentered) > > listkeys = dictent.keys() > > i = 1 > > clue = '' > > for item in listkeys: > > if numberentered == i: > > del dictent[item] > > clue = item > > break > > i = i +1 > > text = "Clue " + clue + " has been > removed > > the list of clues now is :-" + "\n" > > self.writedict(dictent) > > moretext = self.showinfo(dictent) > > text = text + moretext > > except: > > text = "Please enter a number as a > figure" > > self.putinfo(text) > > > > def create_widgets(self): > > #create GUI > > self.question_lbl = Label(self, text = > "Enter > > the question ") > > self.question_lbl.grid(row = 0, column = > 0, > > columnspan = 2, sticky = W) > > self.answer_lbl = Label(self, text = > "Enter > > the answer") > > self.answer_lbl.grid(row = 1, column = 0, > > columnspan = 2, sticky = W) > > self.delete_lbl = Label(self, text = > "Enter > > the number of a clue you want deleted") > > self.delete_lbl.grid(row = 2, column = 0, > > columnspan = 2, sticky = W) > > #entry widget for the question > > self.question_ent = Entry(self) > > self.question_ent.grid(row=0, column = 2, > > columnspan = 1, sticky = W) > > self.answer_ent = Entry(self) > > self.answer_ent.grid(row=1, column = 2, > > columnspan = 2, sticky = W) > > self.delete_ent = Entry(self) > > self.delete_ent.grid(row=2, column = 2, > > columnspan = 1, sticky = W) > > > > #button to add entries > > Button(self, text = "Click to add clues ", > > command = self.create_questionsanswer).grid(row = > 0, > > column = 3, columnspan = 3) > > Button(self, text = "Click to show clues > ", > > command = self.showclues).grid(row = 1, column = > 3, > > columnspan = 3) > > Button(self, text = "Click to delete clue > ", > > command = self.deleteclue).grid(row = 2, column = > 3, > === message truncated === __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 From jkmacman at yahoo.com Mon Jan 10 01:03:25 2005 From: jkmacman at yahoo.com (Jim Kelly) Date: Mon Jan 10 01:03:28 2005 Subject: [Tutor] Re: Tutor Digest, Vol 11, Issue 29 In-Reply-To: <20050109214409.B54721E4014@bag.python.org> Message-ID: <20050110000325.3687.qmail@web50001.mail.yahoo.com> i had the same probblem with xp. on mac os x i can double click on the file and it will open. xp opens the python file and closes it immediately apon double click open the python file via the Start Menu in xp. Then hit f5 and the script will run jk nj --- tutor-request@python.org wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body > 'help' to > tutor-request@python.org > > You can reach the person managing the list at > tutor-owner@python.org > > When replying, please edit your Subject line so it > is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: (no subject) (Alan Gauld) > 2. Crossword program (David Holland) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 9 Jan 2005 15:33:28 -0000 > From: "Alan Gauld" > Subject: Re: [Tutor] (no subject) > To: "Jeffrey Thomas Peery" , > > Message-ID: <001a01c4f660$91848150$16c68651@xp> > Content-Type: text/plain; charset="iso-8859-1" > > > > Hello I can't seem to get the IDLE to start up in > my windows XP by > clicking > > ... > > Also I can't seem to get xp to recognize .py files > belonging to > python. > > This is all fixable but it suggests maybe other > problems in the > installation. Personally I'd recommend reinstalling > Python > and that should fix all the problems. > > Alan G. > > > > ------------------------------ > > Message: 2 > Date: Sun, 9 Jan 2005 21:44:03 +0000 (GMT) > From: David Holland > Subject: [Tutor] Crossword program > To: tutor@python.org > Message-ID: > <20050109214403.98143.qmail@web25407.mail.ukl.yahoo.com> > Content-Type: text/plain; charset=iso-8859-1 > > I wrote a program to create crosswords in python. > It is not perfect but it works, is there any open > source place I can put this for it to be used by > anyone who wants it ? (Subject to the gpl licence). > Here is the code in case anyone is interested. > > from Tkinter import * > #Crossword program David Holland > class Crossword(Frame): > > def __init__(self, master): > '''Default arguments''' > Frame.__init__(self, master) > self.grid() > self.create_widgets() > NUM_SQUARES = 169 > EMPTY = " " > global questionanswer > questionanswer = {} > global dictshort > dictshort = {} > > def readdict(self): > #get the dictionary from a file > try: > pickle_file = open(dictfile, "rb") > dictread = cPickle.load(pickle_file) > except: > dictread = {} > return dictread > > def writedict(self, dictent): > #write dictionary to file > pickle_file = open(dictfile, "wb") > cPickle.dump(dictent, pickle_file) > pickle_file.close() > > def showclues(self): > #show clues on the screen > questionanswer = self.readdict() > textent = self.showinfo(questionanswer) > #textent = questionanswer > self.putinfo(textent) > > def showinfo(self, dictent): > #make the clues look readable > i = 0 > listkeys = dictent.keys() > #listkeys = listkeys.sort() > textdisp = '' > for item in listkeys: > i = i+1 > istr = str(i) + " " > question = dictent[item] > textdisp = textdisp + istr + " the > question is " + question + " the answer is " + item > + > "\n" > return textdisp > > def newcrosswordboard(self): > #create a crossword board > board = [] > for square in range (NUM_SQUARES): > board.append(EMPTY) > return board > > # def newcrosswordboard(self): > # #this is create a board with the numbers > # board = [] > # for square in range (NUM_SQUARES): > # text = str(square) > # board.append(text) > # return board > > def deleteclue(self): > #delete a clue > try: > numberentered = self.delete_ent.get() > dictent = self.readdict() > numberentered = int(numberentered) > listkeys = dictent.keys() > i = 1 > clue = '' > for item in listkeys: > if numberentered == i: > del dictent[item] > clue = item > break > i = i +1 > text = "Clue " + clue + " has been > removed > the list of clues now is :-" + "\n" > self.writedict(dictent) > moretext = self.showinfo(dictent) > text = text + moretext > except: > text = "Please enter a number as a > figure" > self.putinfo(text) > > def create_widgets(self): > #create GUI > self.question_lbl = Label(self, text = > "Enter > the question ") > self.question_lbl.grid(row = 0, column = 0, > columnspan = 2, sticky = W) > self.answer_lbl = Label(self, text = "Enter > the answer") > self.answer_lbl.grid(row = 1, column = 0, > columnspan = 2, sticky = W) > self.delete_lbl = Label(self, text = "Enter > the number of a clue you want deleted") > self.delete_lbl.grid(row = 2, column = 0, > columnspan = 2, sticky = W) > #entry widget for the question > self.question_ent = Entry(self) > self.question_ent.grid(row=0, column = 2, > columnspan = 1, sticky = W) > self.answer_ent = Entry(self) > self.answer_ent.grid(row=1, column = 2, > columnspan = 2, sticky = W) > self.delete_ent = Entry(self) > self.delete_ent.grid(row=2, column = 2, > columnspan = 1, sticky = W) > > #button to add entries > Button(self, text = "Click to add clues ", > command = self.create_questionsanswer).grid(row = 0, > column = 3, columnspan = 3) > Button(self, text = "Click to show clues ", > command = self.showclues).grid(row = 1, column = 3, > columnspan = 3) > Button(self, text = "Click to delete clue ", > command = self.deleteclue).grid(row = 2, column = 3, > === message truncated === __________________________________ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. http://promotions.yahoo.com/new_mail From cyresse at gmail.com Mon Jan 10 01:04:47 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 10 01:04:50 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: References: <41E1B13E.7030203@adinet.com.uy> <1105314744.41e1c3b85ae5a@www.paradise.net.nz> Message-ID: Eep. On Mon, 10 Jan 2005 13:04:33 +1300, Liam Clarke wrote: > I think you're looking for eval() - but that's a big security hole, > and wouldn't handle f(x) notation overly well, unless you parse like > John said. > > > On Mon, 10 Jan 2005 12:52:24 +1300 (NZDT), jfouhy@paradise.net.nz > wrote: > > Quoting Ismael Garrido : > > > > > I am trying to make a program that will plot functions. For that, I need > > > to be able to get an input (the function to be plotted) and execute it. > > > So, my question is, how do I use the input? I have found no way to > > > convert the string to some kind of executable code. > > > > So you want the user to be able to type something like "f(x) = sin(2*x)" and > > then your program will plot it --- is that correct? > > > > Maybe you could parse it yourself? I have found SimpleParse quite easy to use > > --- http://simpleparse.sourceforge.net/ . You will need to write your own > > grammar to describe functions --- something like this, I guess: > > > > eqn := fname, '(', varlist, ')=', expr > > varlist := var, (',', var)* > > > > expr := atom, (binop, atom)? > > atom := var / (fun, '(', expr, ')') / num > > fun := 'sin' / 'cos' / 'tan' / ... > > > > var := char > > fname := char+ > > num := digit+ > > binop := '+' / '*' / '/' / '-' > > > > char := [a-zA-Z] > > digit := [0-9] > > > > although you will need to work on that to get the precedence right and avoid > > undesired recursion and the like. SimpleParse will give you a tree (made of > > nested tuples) representing your function. You can then have a go at converting > > the tree to a function. > > > > I guess the standard way to do this would be something like: > > > > def convert(node): > > functionName = node[0] > > children = node[1] > > if functionName == '*': > > return convert(children[0]) * convert(children[1]) > > elif functionName == '+': > > ... > > > > But you may be able to come up with something more clever. > > > > Hope this helps. > > > > -- > > John. > > _______________________________________________ > > 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. > -- '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. From cyresse at gmail.com Mon Jan 10 01:18:09 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 10 01:18:14 2005 Subject: [Tutor] Slightly OT - Python/Java Message-ID: Hi all, I've been forcing myself to learn Java, and I was wondering if anyone's used Jython. To clarify - Jython generates Java bytecode? Personally, I should've learnt Java first (although my success at that without my Python experiences would've been limited.) I find it's real nasty forcing myself to think through very (relative to Python) narrow hoops to keep the Java compiler happy. All that stuff with typing variables - I can understand that you'd want to specify when the compiler needs to reserve 64 bits for a long integer, but beyond that is the typing really necessary for performance reasons? As far as I can tell it's merely so that the compiler can check for errors which could lead to security problems. Very frustrating. As is the true/false checking... (Also very frustrating is having lists that can be comprised only of one variable type.) I wrote a simple programme in Python & Java, which iterated from 0 to 10 million, doing i % 13 to determine how many numbers were divisble by 13. (769231 between 0 & 10 million) Time to write Python programme - 3 minutes. Time to write Java programme - 30 minutes. (Why can't a non-static method comparison be called from a static reference? What does that mean anyway? Runtime in Python 17.605 seconds (avg over 10 runs) Runtime in Java 12.302 seoncds (avg over 10 runs) So yes - if Jython compiles to Java bytecode, and gives that 5 seconds faster performance, but I write it in Python, and it gives me that 27 minutes of not messing about counting my braces, I'm in heaven. ( I actually wrote a Python script to count braces.) Anyone familar? Regards, Liam Clarke PS I'm only learning Java because it's what they teach the COSC 1st year course that I plan to do. -- '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. From ismaelgf at adinet.com.uy Mon Jan 10 01:30:52 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Mon Jan 10 01:30:29 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <1105314744.41e1c3b85ae5a@www.paradise.net.nz> References: <41E1B13E.7030203@adinet.com.uy> <1105314744.41e1c3b85ae5a@www.paradise.net.nz> Message-ID: <41E1CCBC.7050809@adinet.com.uy> jfouhy@paradise.net.nz wrote: >Quoting Ismael Garrido : > > > >>I am trying to make a program that will plot functions. For that, I need >>to be able to get an input (the function to be plotted) and execute it. >> > > >So you want the user to be able to type something like "f(x) = sin(2*x)" and >then your program will plot it --- is that correct? Yes, that's what I want. >Maybe you could parse it yourself? I have found SimpleParse quite easy to use >--- http://simpleparse.sourceforge.net/ . You will need to write your own >grammar to describe functions (Newbie looking scared) That's kind of hard for me... Parsing it myself is too complex for me. Also, I hoped Python's math would do the job for me, so I wouldn't have to make what's already done in Python. For what I understood of Mr. Clarke's mail, eval() would do the job (in spite of the security problem, I'm not concerned about that). Is that correct? I guess I'll go read about that a bit more. Thanks for your replies. Ismael From alan.gauld at freenet.co.uk Mon Jan 10 01:54:14 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 10 01:53:48 2005 Subject: [Tutor] Slightly OT - Python/Java References: Message-ID: <004f01c4f6ae$e7e34470$16c68651@xp> > I've been forcing myself to learn Java, and I was wondering if > anyone's used Jython. > To clarify - Jython generates Java bytecode? I'm learning its vagaries as a way of testing objects being written by my develoment teams in Java. > Personally, I should've learnt Java first (although my success at that > without my Python experiences would've been limited.) I don;t know why you think that would help? Or do you mean Java before Jython? If so it depends what you intend using Jython for! > I find it's real nasty forcing myself to think through very (relative > to Python) narrow hoops to keep the Java compiler happy. Yep, thats the horror of most 3GL languages after Python :-) > All that stuff with typing variables - I can understand that you'd > want to specify when the compiler needs to reserve 64 bits for a long > integer, but beyond that is the typing really necessary for > performance reasons? No, its allegedly for reliability reasons - if it compiles then you should never get a runtime eror due to the wrong kind of object being passed. I used to believe passionately in that principle, now, after using Python I'm not so convinced it matters as much as I thought. THe type conversion functions in Java(and C++) can do funny things to data that bring their own problems! > (Also very frustrating is having lists that can be comprised only of > one variable type.) But the type can be object, which covers everything in Java... You just need to remember to convert the type of the object to the real class beforecalling any specific methods. > Time to write Python programme - 3 minutes. > Time to write Java programme - 30 minutes. (Why can't a non-static > method comparison be called from a static reference? What does that > mean anyway? static means its associated with the class. non static with the object. The static reference doesn't know about instances. > Runtime in Python 17.605 seconds (avg over 10 runs) > Runtime in Java 12.302 seoncds (avg over 10 runs) > > So yes - if Jython compiles to Java bytecode, and gives that 5 seconds > faster performance, Not so simple. Jython is the Python interpreter written in Java. So all the dynamic stuff that Python does has to be translated into Java code which will slow things down. In practive Jython code - even after compilation to JVM via jythonc will be slower than both CPython and Java. The exception being if you simply use Jython to glue together Java classes where the equivalent CPython modules are written in Python. In this case Jython may be slightly faster than CPython. But still slower than 100% Java. > but I write it in Python, and it gives me that 27 > minutes of not messing about counting my braces, I'm in heaven. Thats the win of Jython. You can quickly write the core code, even classes. Then compile them and use them in a HJAva program. The slow bits can be recoded in Java once you know the design works. The glue and non time critical classes can usually stay in Python. > PS I'm only learning Java because it's what they teach the COSC 1st > year course that I plan to do. Its worth learnoing at least one statically/strictly typed language. There are some advantages but for me they are outweighed by the disadvantages, but then I'm not selling commercial software that gets benchmarked against my cometitorsin PC Magazine etc... In that scenario every millisecond counts! Alan G. From jfouhy at paradise.net.nz Mon Jan 10 02:14:15 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Mon Jan 10 02:14:19 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <41E1CCBC.7050809@adinet.com.uy> References: <41E1B13E.7030203@adinet.com.uy> <1105314744.41e1c3b85ae5a@www.paradise.net.nz> <41E1CCBC.7050809@adinet.com.uy> Message-ID: <1105319655.41e1d6e77a0aa@www.paradise.net.nz> Quoting Ismael Garrido : > (Newbie looking scared) That's kind of hard for me... Parsing it myself > is too complex for me. Also, I hoped Python's math would do the job for > me, so I wouldn't have to make what's already done in Python. Writing (and understanding) grammar is probably easier than you think --- once you get over the syntax. And they are fantastically useful things for doing any sort of parsing. But no matter.. > For what I understood of Mr. Clarke's mail, eval() would do the job (in > spite of the security problem, I'm not concerned about that). Is that > correct? I guess I'll go read about that a bit more. Yeah, probably. For example: >>> from math import * >>> x = 3 >>> y = 2 >>> eval("2*x + 3*sin(x + y)") 3.1232271760105847 Note that if I had done "import math" instead of "from math import *", it would not have worked, because "sin" would not have been defined. So you could do something like: def evalAt(function, x): """ Evaluate a function (as a string) at a given point. """ return eval(function) # This is equivalent to: evalAt = lambda function, x: eval(function) myFun = "2*x + 3*sin(x + y)" evalFunction = lambda x: evalAt(myFun, x) xPoints = [x / 10000.0 for x in xrange(10000)] yPoints = map(evalFunction, xPoints) One problem with this approach is that you are required to use 'x' as the variable when you are typing in your function. Oh, to answer your other question: It is almost certainly better to calculate the points first, and then plot them. -- John. From kent37 at tds.net Mon Jan 10 02:18:44 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 02:18:49 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <41E1B13E.7030203@adinet.com.uy> References: <41E1B13E.7030203@adinet.com.uy> Message-ID: <41E1D7F4.2030402@tds.net> You can you the exec statement to execute Python code from a string. The string could be from user input. So for example a user could input 'x*x' and you could do >>> inp = 'x*x' >>> func='def f(x): return ' + inp >>> func 'def f(x): return x*x' >>> exec func >>> f(3) 9 Now you have f(x) defined as a regular function and you can plot it the same as a function you wrote in your code. The user will have to use whatever variable name you expect. And this is a huge security hole, you shouldn't do it if you don't trust the users. Kent Ismael Garrido wrote: > Hello > > I am trying to make a program that will plot functions. For that, I need > to be able to get an input (the function to be plotted) and execute it. > So, my question is, how do I use the input? I have found no way to > convert the string to some kind of executable code. > > I did research the problem. And found two things, first, an > unsatisfactory solution from: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52217 > The code there, by means I cannot understand at all, executes a string > as code. To be usable I have to make my code a huge string. It is not > very elegant. > > The second possible solution I found was using eval, compile and/or > exec. But I do not understand what do they do, or how to use them, for > the matter. > > Related to the program I intend to do, which design would you say is > more intelligent: one that first calculates the whole function, stores > it, and then plots it; or one that calculates-plots-calc.-plot, and so on? > > Thanks for any information, and for your time. > Ismael > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Mon Jan 10 02:27:16 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 02:27:24 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: References: Message-ID: <41E1D9F4.4050707@tds.net> Liam Clarke wrote: > Hi all, > > I've been forcing myself to learn Java, and I was wondering if > anyone's used Jython. Yes, quite a bit. > To clarify - Jython generates Java bytecode? Yes. Jython is a Python compiler and runtime written in Java. It integrates very well with Java libraries. > > Personally, I should've learnt Java first (although my success at that > without my Python experiences would've been limited.) Because Python has spoiled you for anything else? ;) > > I find it's real nasty forcing myself to think through very (relative > to Python) narrow hoops to keep the Java compiler happy. Yep. > PS I'm only learning Java because it's what they teach the COSC 1st > year course that I plan to do. My guess is that if they are using Java in the course, that is what you will have to use. Jython *is* an implementation of Python - a Jython program looks like Python, has the syntax and semantics of Python. Though the programs will run without installing Python - all you need is the Java runtime and Jython libraries. But I think it's a long shot and you will probably have to bite the bullet and learn Java. Kent From kent37 at tds.net Mon Jan 10 03:00:34 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 03:00:43 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <1105319655.41e1d6e77a0aa@www.paradise.net.nz> References: <41E1B13E.7030203@adinet.com.uy> <1105314744.41e1c3b85ae5a@www.paradise.net.nz> <41E1CCBC.7050809@adinet.com.uy> <1105319655.41e1d6e77a0aa@www.paradise.net.nz> Message-ID: <41E1E1C2.9050108@tds.net> jfouhy@paradise.net.nz wrote: > Quoting Ismael Garrido : > > >>(Newbie looking scared) That's kind of hard for me... Parsing it myself >>is too complex for me. Also, I hoped Python's math would do the job for >>me, so I wouldn't have to make what's already done in Python. > > > Writing (and understanding) grammar is probably easier than you think --- once > you get over the syntax. And they are fantastically useful things for doing any > sort of parsing. But no matter.. If you are interested in learning about parsing, I suggest looking at pyparsing - it is IMO the easiest of the Python parsing frameworks. It has an example that implements a four-function calculator so that would get you off in the right direction. http://pyparsing.sourceforge.net/ Kent From keridee at jayco.net Sun Jan 9 20:30:27 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 10 03:12:14 2005 Subject: [Tutor] (no subject) References: <20050109031949.30D231E4008@bag.python.org> Message-ID: <000001c4f6b9$d36830e0$3a5328cf@JSLAPTOP> >Hello I can't seem to get the IDLE to start up in my windows XP by clicking on the desktop icon. To start it I have to >drop a .py file on the icon. Any ideas? Danny just answered that, I believe... so next! >Also I can't seem to get xp to recognize .py files belonging to python. Right now the icons for .py is a windows default >and not the python icon. So if I double click on a script that I want to work on it doesn't open the idle. I have to open the >script from IDLE. Any ideas on this problem? Go to Control Panel, then to Folder Options. Click on the File Associations tab (I think that's what it's called-whatever-it's the last one) You should get a whole bunch of registered file types. Go down to PY. Click on on the advanced button Double click on Open Change the command to "C:\python24\pythonw.exe" "C:\python24\lib\idlelib\idle.pyw" -n -e "%1" (Obviously, you are either going to have to get Danny's suggestion working, or you will have to change it from pythonw.exe to python.exe) I often think it's best to present a way to run scripts straight from the interpreter. So, change Edit with IDLE's command to "C:\python24\python.exe" "%1" %* Change the name on the same thing to "Open with Console" --- or something like that.. Click Ok however many times is necessary... Now--what you've just done is to set it up so that double clicking results in IDLE starting up, and right clicking shows two commands Open & Open with console (or the equivalent) Open with console should run the script straight from the interpreter. HTH, Jacob (Yes, I'm the same Jacob that Danny mentioned) From david at graniteweb.com Mon Jan 10 03:23:22 2005 From: david at graniteweb.com (David Rock) Date: Mon Jan 10 03:23:30 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: <41E1D9F4.4050707@tds.net> References: <41E1D9F4.4050707@tds.net> Message-ID: <20050110022321.GA18518@wdfs.attbi.com> * Kent Johnson [2005-01-09 20:27]: > Liam Clarke wrote: > >Hi all, > > > >I've been forcing myself to learn Java, and I was wondering if > >anyone's used Jython. For anyone in the Chicago, IL area, the Chicago Python Users Group, ChiPy, is going to have a speaker on Jython this Thursday, January 13th. For more information, check out the group site at: http://www.chipy.org Everyone is welcome, benginner to advanced. We love all kinds of input. You'd be surprised what you can learn when you get the chance to actually TALK to someone else ;-) -- David Rock david@graniteweb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050109/260abd26/attachment.pgp From isrgish at fastem.com Mon Jan 10 03:26:59 2005 From: isrgish at fastem.com (Isr Gish) Date: Mon Jan 10 03:27:17 2005 Subject: [Tutor] XP and python 2.4, some progress Message-ID: <20050110022715.4B50B1E400A@bag.python.org> I was having the same problem. I tried Dannys idea it didnt help. It seemed to me that something was wrong with the actual shortcut. So I went to the directory with the idle.pyw (C:\Program Files\Python24\Lib\idlelib\) and made a shortcut and now it wroks fine. All the best, Isr -----Original Message----- >From: "Danny Yoo" >Sent: 1/9/05 4:48:51 AM >To: "Jeffrey Thomas Peery" >Cc: "tutor@python.org" >Subject: Re: [Tutor] XP and python 2.4, some progress > > >On Sat, 8 Jan 2005, Jeffrey Thomas Peery wrote: > >> I wasn't able to get the IDLE started in windows XP. I had it working then I >> upgraded to 2.4, then it didn't work so I switched back to 2.3, still didn't >> work so I'm back to 2.4. I did some looking around and I was able to get >> the IDLE started by setting the shortcut on my desktop to: >> >> C:\Python24\python.exe C:\Python24\Lib\idlelib\idle.pyw -n > > >Hi Jeff, > >Oh no, not again! *grin* > > >> Not sure what this does. But is seems to get it going. What is going on >> here? Also I get a console that appears with this message - I have no >> idea what it means: >> >> Warning: configHandler.py - IdleConf.GetThemeDict - problem retrieving >> theme element 'builtin-background' > > >Ok, we've seen something like this before; what you are running into is >probably the same thing. Mike and Jacob have found that IDLE broke on >them when upgrading from Python 2.3 to Python 2.4. See the thread >starting from: > > http://mail.python.org/pipermail/tutor/2004-December/033672.html > >It turns out that the bug has to do with the way IDLE now handles its >configuration files. If you've made some special configuration (like >color customization), the bug causes IDLE not to start up cleanly because >the customized config files aren't compatible. > >To work around this, rename your '.idlerc/' directory to something else >temporarily. The '.idlerc' directory should be somewhere in your home >within the 'Documents and Settings' directory. The '.idlerc/' directory >contains all the user-defined settings that you've made to IDLE, so if we >hide it, IDLE should try to regenerate a clean set. > >After renaming it to something else, try restarting IDLE with the icon, >and see if it comes up now. > > >I hope this helps! > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Mon Jan 10 03:51:55 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 10 03:52:21 2005 Subject: [Tutor] Input to python executable code and design question References: <41E1B13E.7030203@adinet.com.uy> Message-ID: <003001c4f6bf$5cdd7420$3a5328cf@JSLAPTOP> I wondered when someone would ask something like this. eval() is good and it can be done using it. I wrote a -- IMHO -- really great functiongraphing program using vpython. If you would like to see it, just reply and say so. Pros and cons of calculating all first: pro - easier to read code con - user may feel insecure while the points are being calculated -- for example, say you type in a big, complicated function, and while the computer sits there and calculates the points, the user might feel that something is wrong. On the other hand, if you calculate each point on its own, points are immediately put on the screen so your user knows that the thing is working (if you get my drift) Please tell me what you are using to plot the points. (big grin) Vpython, wxpython, what? I'm curious--it's just someone else is working on a project that I'm working on... To help you out. You need some sort of error checking to be sure that within your given range you won't get something like a math domain error. Ex. y = 'sqrt(x)' x = -15 while -15<=x<=15: print eval(y) Gives you something like Traceback blah, blah ... Math domain error If you want more suggestions, ask.... Please, tell me how you're doing. It sounds interesting. HTH, Jacob Schmidt > Hello > > I am trying to make a program that will plot functions. For that, I need > to be able to get an input (the function to be plotted) and execute it. > So, my question is, how do I use the input? I have found no way to > convert the string to some kind of executable code. > > I did research the problem. And found two things, first, an > unsatisfactory solution from: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52217 > The code there, by means I cannot understand at all, executes a string > as code. To be usable I have to make my code a huge string. It is not > very elegant. > > The second possible solution I found was using eval, compile and/or > exec. But I do not understand what do they do, or how to use them, for > the matter. > > Related to the program I intend to do, which design would you say is > more intelligent: one that first calculates the whole function, stores > it, and then plots it; or one that calculates-plots-calc.-plot, and so on? > > Thanks for any information, and for your time. > Ismael > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Mon Jan 10 04:12:55 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 10 04:12:47 2005 Subject: [Tutor] Hi. Is there another mailing list like this one? Message-ID: <003a01c4f6c2$4938e410$3a5328cf@JSLAPTOP> The subject line says it all... Okay I'll add some. I'm am bored and people are not asking enough questions/answering them to keep my mind busy. Is there any other mailing list that I can subscribe to like this one that lets anyone ask and answer questions? Thanks in advance, Jacob Schmidt From cyresse at gmail.com Mon Jan 10 04:17:39 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 10 04:17:41 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: <004f01c4f6ae$e7e34470$16c68651@xp> References: <004f01c4f6ae$e7e34470$16c68651@xp> Message-ID: > > > Personally, I should've learnt Java first (although my success at that > > without my Python experiences would've been limited.) > > I don;t know why you think that would help? > Or do you mean Java before Jython? If so it depends > what you intend using Jython for! I meant learning Java prior to Python. Then, I could've doubled my elation at discovering Python, having had something to compare it to, whereas now I've got to grit my teeth and force myself to code. > No, its allegedly for reliability reasons - if it compiles then > you should never get a runtime eror due to the wrong kind of > object being passed. I used to believe passionately in that > principle, now, after using Python I'm not so convinced it > matters as much as I thought. THe type conversion functions > in Java(and C++) can do funny things to data that bring their > own problems! So it's protecting me from my own bad programming? Does that have to be built into a compiler? Couldn't it be an optional switch and I wear my bad code if it fails? *mutter* > Its worth learnoing at least one statically/strictly typed language. > There are some advantages but for me they are outweighed by > the disadvantages, but then I'm not selling commercial software > that gets benchmarked against my cometitorsin PC Magazine etc... > In that scenario every millisecond counts! I do need to learn a... commercially wide spread language.... but I think if I ever got that serious about milliseconds, it'd be time to learn Cpp & mesh it with Python. (5 seconds difference over 10 million iterations, That's 0.0000005 seconds difference per iteration. That's not overly bad.) Actually, I have the Intel assembler manuals at home, haven't even looked at 'em. If we're going for speed... Ah pity. I was hoping I could code for the JVM in Python style; I'd have to learn Java anyway, but I was thinking long, long term, beyond my public service walls. 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. From ismaelgf at adinet.com.uy Mon Jan 10 04:19:43 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Mon Jan 10 04:19:09 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <003001c4f6bf$5cdd7420$3a5328cf@JSLAPTOP> References: <41E1B13E.7030203@adinet.com.uy> <003001c4f6bf$5cdd7420$3a5328cf@JSLAPTOP> Message-ID: <41E1F44F.9010604@adinet.com.uy> Jacob S. wrote: >eval() is good and it can be done using it. >I wrote a -- IMHO -- really great functiongraphing program using vpython. >If you would like to see it, just reply and say so. > > Out of curiosity, I would like to see your program. There's always something to learn (and even more so for me, being a newbie) >Please tell me what you are using to plot the points. (big grin) Vpython, >wxpython, what? >I'm curious--it's just someone else is working on a project that I'm working >on... > > At the moment, nothing :-s I'm learning Phyton, and I thought that this would be an interesting challenge. For what I've seen, Tkinter's Canvas 'could possibly' do the job. I still have to try it out. In case that didn't work, I was thinking in looking through wxpython. >To help you out. >You need some sort of error checking to be sure that within your given range >you >won't get something like a math domain error. > > Yes, I thought that: try: #function exception: pass >If you want more suggestions, ask.... >Please, tell me how you're doing. It sounds interesting. > > At the moment, I have almost nothing. After John Fouhy's replies I have rewritten the few lines I had at least three times :o) It will be simple, I intend to support viewing specific parts of the function (instead of a fixed view), multiple graphs, perhaps an option to save/load functions. I first made a program like this in Qbasic 4.5, and thought doing it again in Python with an interface and more advanced options may be very entretaining. :-) I can send you the source/exe if you want (sadly you can't choose what you want to see in the exe version, the function must be hard-coded). Thanks Ismael From keridee at jayco.net Mon Jan 10 04:35:54 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 10 04:35:59 2005 Subject: [Tutor] Input to python executable code and design question References: <41E1B13E.7030203@adinet.com.uy><003001c4f6bf$5cdd7420$3a5328cf@JSLAPTOP> <41E1F44F.9010604@adinet.com.uy> Message-ID: <004301c4f6c5$80cec040$3a5328cf@JSLAPTOP> I have grown to like VPython as the curve attribute really seems to do the trick. If you get it working on a Tkinter canvas, I would like to see the code as I haven't quite found a way to plot points on to one of those. A simple graph function in VPython... (it isn't the whole thing, believe me...) It already is more powerful than most graphing calculators--see except comment. def graphit(function): m = curve(color=color.blue) x = -15 while -15<=x<=15: try: m.append(pos=(x,eval(function),0)) except: m = curve(color=color.red) # This is to catch domain errors and keep the curve from connecting points across asymptotes x = x+0.05 # Yes it might be too high a precision, but I usually use 0.005 I would like to see the executable. I don't know anything about Qbasic 4.5, so I don't know if I could view the source, etc... Tell me if you want cut and paste or attachment to see the program. By the way, I don't want to give the impression that I'm anything much better than a newbie myself. I just have a big mouth.... It might help setting it up. It supports x functions and polar graphs as well. Perhaps for the future I will try 3d graphs, since VPython supports 3d. Hah! There's something I don't remember Tkinter Canvas being able to do. Jacob > Jacob S. wrote: > > >eval() is good and it can be done using it. > >I wrote a -- IMHO -- really great functiongraphing program using vpython. > >If you would like to see it, just reply and say so. > > > > > Out of curiosity, I would like to see your program. There's always > something to learn (and even more so for me, being a newbie) > > >Please tell me what you are using to plot the points. (big grin) Vpython, > >wxpython, what? > >I'm curious--it's just someone else is working on a project that I'm working > >on... > > > > > At the moment, nothing :-s > I'm learning Phyton, and I thought that this would be an interesting > challenge. For what I've seen, Tkinter's Canvas 'could possibly' do the > job. I still have to try it out. In case that didn't work, I was > thinking in looking through wxpython. > > > >To help you out. > >You need some sort of error checking to be sure that within your given range > >you > >won't get something like a math domain error. > > > > > Yes, I thought that: > try: > #function > exception: > pass > > > >If you want more suggestions, ask.... > >Please, tell me how you're doing. It sounds interesting. > > > > > At the moment, I have almost nothing. After John Fouhy's replies I have > rewritten the few lines I had at least three times :o) > It will be simple, I intend to support viewing specific parts of the > function (instead of a fixed view), multiple graphs, perhaps an option > to save/load functions. I first made a program like this in Qbasic 4.5, > and thought doing it again in Python with an interface and more advanced > options may be very entretaining. :-) I can send you the source/exe if > you want (sadly you can't choose what you want to see in the exe > version, the function must be hard-coded). > > Thanks > Ismael > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From nick at javacat.f2s.com Mon Jan 10 08:30:31 2005 From: nick at javacat.f2s.com (Nick Lunt) Date: Mon Jan 10 08:27:50 2005 Subject: FW: [Tutor] walking directories Message-ID: Hello, I sent this reply a few days ago, but it doesn't seem to have appeared on the list, so Im resending it. Many thanks Nick . -----Original Message----- From: Nick Lunt [mailto:nick@javacat.f2s.com] Sent: 08 January 2005 19:44 To: 'python tutor' Subject: RE: [Tutor] walking directories >>From: Ryan Davis [mailto:ryan@acceleration.net] ?? >>I think you want to be doing something like: >> >>>for r,d,f in os.walk('.'): >>... for filename in f: >>... print os.path.join(r,filename) >> >>I think that would give you the full path of every file, and then you can open it, do a regex substitution or whatever close it and >>keep going. >> >>From: Jacob Schmidt >> >>for root, dirs, files in os.walk(directory): >> for x in files: >> print os.path.join(root,x) Thankyou Ryan and Jacob, I obviously did not read the docs for os.walk correctly, many thanks. >>Alan G. >>personally I'd use find and sed in Linux, right tool >>for the job etc.. Yes I could use *nix tools for the job, but I want to do it in python to see if I can :) Thanks again everyone for your help. Nick . -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005 -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005 From pierre.barbier at cirad.fr Mon Jan 10 09:16:07 2005 From: pierre.barbier at cirad.fr (Pierre Barbier de Reuille) Date: Mon Jan 10 09:14:14 2005 Subject: [Tutor] Hi. Is there another mailing list like this one? In-Reply-To: <003a01c4f6c2$4938e410$3a5328cf@JSLAPTOP> References: <003a01c4f6c2$4938e410$3a5328cf@JSLAPTOP> Message-ID: <41E239C7.6070302@cirad.fr> If you're "bored" you can try the newsgroup comp.lang.python ... there's huge lot more questions ! Yet, this forum is dedicated to beginner, the newsgroup not ! Pierre Jacob S. a ?crit : > The subject line says it all... Okay I'll add some. > > I'm am bored and people are not asking enough questions/answering them to > keep my mind busy. Is there any other mailing list that I can subscribe to > like this one that lets anyone ask and answer questions? > > Thanks in advance, > Jacob Schmidt > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68 From glingl at aon.at Mon Jan 10 11:02:38 2005 From: glingl at aon.at (Gregor Lingl) Date: Mon Jan 10 11:01:50 2005 Subject: [Tutor] German Tutorials auf Deutsch In-Reply-To: References: Message-ID: <41E252BE.6020308@aon.at> Hallo Michael - und alle! Hello Mike - and all! You can find several links to Python ressources for beginners in German on the website http://python4kids.net (or http://www.rg16.asn-wien.ac.at/~python/ ) (Du kannst mehrere Links zu deutschen Python-Einf?hrungen auf oben erw?hnter Website finden.) Moreover there you will find information about and sample-chapters of my book "Python for Kids". If you are just beginning to learn how to program, this may be useful for you? (Au?erdem findest du dort Informationen und Probekapitel zu meinem Buch "Python f?r Kids". Wenn du ein Programmieranf?nger bist, ist das vielleicht was f?r dich?) Best wishes, Gregor (Beste Gr??e Gregor) Kooser, Ara S schrieb: > Pardon to the non-german speaking (or readers) on the list. > > Guten Tag. Mein Deutsch ist nicht so gut (ich habe keinen Deutsche in > sieben Jahren geschreiben). Mann kann Python Tutorials auf Deutsch heir > http://www.freenetpages.co.uk/hp/alan.gauld/german/index.htm > und > http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index_ger.html > und > http://starship.python.net/crew/gherman/publications/tut-de/ > finden. > I habe Alan Gauld Tutorial vorgelesen. Es is gut und es abdeckt zimliche > viele Themen. > > Ara > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27 Autor von "Python f?r Kids" Website: python4kids.net From maxnoel_fr at yahoo.fr Mon Jan 10 02:33:39 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 10 11:29:49 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: References: Message-ID: On Jan 10, 2005, at 00:18, Liam Clarke wrote: > Hi all, > > I've been forcing myself to learn Java, and I was wondering if > anyone's used Jython. > To clarify - Jython generates Java bytecode? I've also learnt Java in roughly 1 week last autumn, because that's what's used at the University of Leeds. All in all, it's a language I quite like, much superior to C++, although it does lack the elegance of Python and Ruby. I also found that the Swing API (for building GUIs) was remarkably easy to use. I can't compare, though, as I've never written GUI'd Python or Ruby programs (and if I did, I'd probably be using PyObjC or RubyCocoa anyway, so that'd be equivalent to using Objective-C). > All that stuff with typing variables - I can understand that you'd > want to specify when the compiler needs to reserve 64 bits for a long > integer, but beyond that is the typing really necessary for > performance reasons? As far as I can tell it's merely so that the > compiler can check for errors which could lead to security problems. > Very frustrating. As is the true/false checking... Well, it has ups and downs... As you said, strong typing reduces the chances that a security problem will occur, and is slightly faster. Also, Java has something which Python lacks (yet should have): private/protected/public class members. In Python, everything is public, which I consider to be a Bad Thing(TM). > (Also very frustrating is having lists that can be comprised only of > one variable type.) First things first: don't use lists/arrays. Use Collections instead (classes like ArrayList, HashMap, etc.). They're much more versatile, and akin to lists and dictionaries in Python (sometimes even better). Iterators over them are supported. And while they can indeed only be comprised of one variable type as well, said variable type is Object, from which all classes derive, so that's not a problem (you only have to remember to cast as you get() something from a Collection). > (Why can't a non-static > method comparison be called from a static reference? What does that > mean anyway? Er... What was your code like? (before and after correcting the error) > Runtime in Python 17.605 seconds (avg over 10 runs) > Runtime in Java 12.302 seoncds (avg over 10 runs) Does runtime in Java include startup? Every time you start a java program, it has to load a BIG runtime in memory (roughly 30 megs of classes -- that's both Java's curse and blessing). Also, this test is not a very good way to measure performance of both languages. Java really shines when you start going haywire with classes and objects -- and the program goes faster as it runs (thanks to the Hotspot JIT compiler, that optimizes and caches stuff in ways I don't really understand) > So yes - if Jython compiles to Java bytecode, and gives that 5 seconds > faster performance, but I write it in Python, and it gives me that 27 > minutes of not messing about counting my braces, I'm in heaven. ( I > actually wrote a Python script to count braces.) Oh Ghost. You didn't actually write a Java program using a regular text editor, did you? Go and download Eclipse. Now. It's the best IDE in the world, and probably the main reason why I like Java. It has features you can't live without once you've tried them -- spellchecking with smart correction, auto-completion, pop-up help dialogs... And most importantly, code auto-formatting and automatic brace matching. And of course, it's free (and written in Java). http://www.eclipse.org I'm also told people are currently developing a Python plugin for Eclipse. That'd be the Best Thing Ever(TM). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Mon Jan 10 11:47:07 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 11:47:12 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: References: <004f01c4f6ae$e7e34470$16c68651@xp> Message-ID: <41E25D2B.80605@tds.net> Liam Clarke wrote: >>No, its allegedly for reliability reasons - if it compiles then >>you should never get a runtime eror due to the wrong kind of >>object being passed. I used to believe passionately in that >>principle, now, after using Python I'm not so convinced it >>matters as much as I thought. THe type conversion functions >>in Java(and C++) can do funny things to data that bring their >>own problems! > > > So it's protecting me from my own bad programming? > Does that have to be built into a compiler? Couldn't it be an optional > switch and I wear my bad code if it fails? *mutter* This is kind of a hot topic in the world of programming right now - the question of whether static typing and type declarations actually do lead to more correct programming. The jury is still out. Actually it's hot in the Python world as well - Guido has proposed some kind of *optional* type declarations for Python. > Ah pity. I was hoping I could code for the JVM in Python style; I'd > have to learn Java anyway, but I was thinking long, long term, beyond > my public service walls. That's exactly what Jython lets you do - code in Python style while targetting the Java VM at runtime. It has some warts, but generally it works well and is suitable for production use. Kent > > Regards, > > Liam Clarke > > From james.homme at highmark.com Mon Jan 10 17:12:51 2005 From: james.homme at highmark.com (james.homme@highmark.com) Date: Mon Jan 10 17:16:10 2005 Subject: [Tutor] Making Windows Text Editors Compile and Run Python Message-ID: <200501101616.j0AGGFZk002736@igate.highmark.com> Hi, Idol is not an option for me since it does not work with my screen reader. I am trying to use Notetab and Textpad to compile and run Python. Does anyone have directions for either program to do this? Thanks. Jim James D Homme, Information Design + Development Highmark Inc. james.homme@highmark.com 412-544-0527 "A gentle answer turns away wrath, but a harsh word stirs up anger. " From ryan at acceleration.net Mon Jan 10 17:23:39 2005 From: ryan at acceleration.net (Ryan Davis) Date: Mon Jan 10 17:23:25 2005 Subject: [Tutor] import question (solved) In-Reply-To: <00e901c4f52a$f49789e0$ca5328cf@JSLAPTOP> Message-ID: <20050110162324.4CD761E400A@bag.python.org> The problem was with how I was starting the interpreter. The sys.path didn't have all the needed folders. I was running my scripts in xemacs, using C-c C-c to re-run my script after making a change. The problem turned out to be I wasn't using the package names in the import statements. Once I used the package names and started the interpreter from the root directory of my project, it all worked as I expected. So, to import class 'a' from /One/A.py, I had to use: >>> import One.A.a which seems a lot cleaner than having things like this at the top of each script: >>> import sys >>> sys.path.append('../') Thank you all very much for you help. Thanks, Ryan -----Original Message----- From: Jacob S. [mailto:keridee@jayco.net] Sent: Friday, January 07, 2005 9:37 PM To: Alan Gauld; Ryan Davis; Tutor@python.org Subject: Re: [Tutor] import question > It should work as you describe it. > Can you just clarify how you are doing this? > Are you setting sys.path in the same program you > are trying to run? Or is it set in a Python startup script? > > How do you run a.py? Are you importing it into an existing > Python session(with sys.path set or are you ruinning the > file standalone? > > Basically the only thing I can think of is that your setting > of sys.path isn't being seen. > > Can you put a debug print statement to display sys.path > just before the import b statement? That way we make > absolutely certain Pythoncan see the folder. > > Alan G. I had some similar trouble with site-packages when I tried to install psyco a little while ago. Apparently, the sys.path does not include sub folders of site-packages more than one branch deep. I fixed it by moving the main module folder up into the site-package folder. IOW, from C:\python24\lib\site-packages\psyco-1.3\psyco to C:\python24\lib\site-packages\psyco. And then it worked! So maybe Ryan has to specify the exact folder? HTH, Jacob Schmidt From flaxeater at yahoo.com Mon Jan 10 17:44:03 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 10 17:44:06 2005 Subject: [Tutor] Input to python executable code and design question Message-ID: <20050110164403.57212.qmail@web54304.mail.yahoo.com> Ismael Garrido wrote: > jfouhy@paradise.net.nz wrote: > >> Quoting Ismael Garrido : >> >> >> >>> I am trying to make a program that will plot functions. For that, I >>> need >>> to be able to get an input (the function to be plotted) and execute it. >>> > > > > > >So you want the user to be able to type something like "f(x) = > sin(2*x)" and > >then your program will plot it --- is that correct? > > Yes, that's what I want. I can understand you fear. Check out this post that Danny made in October I found it very helpful. http://mail.python.org/pipermail/tutor/2004-October/032364.html This one by kent at the same time was good too. http://mail.python.org/pipermail/tutor/2004-October/032340.html I felt this was a very good and helpful. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From askoose at sandia.gov Mon Jan 10 18:53:37 2005 From: askoose at sandia.gov (Kooser, Ara S) Date: Mon Jan 10 18:54:02 2005 Subject: [Tutor] Modifying game of life Message-ID: I was trying to modify the Game of Life to a percolation model (you can pick what the probability of each point being occupied). I wrote the def for the percolation however when I print the world all I get is NONE NONE NONE NONE Etc... I know my problem is in the line world[i, j] = percolation(perc). The original code that Danny wrote had the line world[i,j]= random.choice([LIVE,DEAD]). I am guessing that Danny's code give a 50/50 chance of a site being occupied and assigns that to i,j . I do not have the correct syntax to make the world[i,j] go through the percolation def. I think i and j are not getting assigned a value. Does anyone have an suggestions, explanations, websites? Thanks. Ara import random perc = raw_input("Please enter a threshold between 0-1. ") raw_input("Press return to make a world") PERSON, EMPTY = '*', '.' def percolation(perc): randval = random.random() PERSON, EMPTY = '*', '.' if randval > perc: EMPTY if randval < perc: PERSON def make_random_world(M, N): world = {} for j in range(N): for i in range(M): world[i, j] = percolation(perc) world['dimensions'] = (M, N) return world def print_world(world): M, N = world['dimensions'] for j in range(N): for i in range(M): print world[i, j], print print_world(make_random_world(10, 10)) ### From maxnoel_fr at yahoo.fr Mon Jan 10 19:02:57 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 10 19:03:00 2005 Subject: [Tutor] Modifying game of life In-Reply-To: References: Message-ID: On Jan 10, 2005, at 17:53, Kooser, Ara S wrote: > Does anyone have an suggestions, explanations, websites? Thanks. > > > Ara > > > > > import random > > perc = raw_input("Please enter a threshold between 0-1. ") > raw_input("Press return to make a world") > PERSON, EMPTY = '*', '.' > > def percolation(perc): > randval = random.random() > PERSON, EMPTY = '*', '.' > if randval > perc: > EMPTY > if randval < perc: > PERSON I think the problem is there. The function doesn't return anything. Chances are you should use "return EMPTY" and "return PERSON" instead of what you have right now (have you been using Ruby recently?). Also, the function still doesn't return anything when randwal == perc. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From dyoo at hkn.eecs.berkeley.edu Mon Jan 10 19:47:06 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 10 19:47:12 2005 Subject: [Tutor] Input to python executable code and design question In-Reply-To: <41E1F44F.9010604@adinet.com.uy> Message-ID: > >To help you out. You need some sort of error checking to be sure that > >within your given range you won't get something like a math domain > >error. > > > > > Yes, I thought that: > try: > #function > exception: > pass Hi Ismael, Python's keyword for exception handling is 'except', so this can be something like: ### try: some_function() except: pass ### ... Except that the 'except' block should seldom be so vacant like that. *grin* There are two things we should try to do when exception handling: handle only what we need, and report exception information when we do. We really want to get the system to handle only domain errors, and otherwise let the exception raise errors. But because the block above catches every Python error, even silly things like misspellings, it will sweep programmer errors under the carpet. For example, the code snippet: ### >>> def sqrt(x): ... return y**0.5 ## bug: typo, meant to type 'x' ... >>> try: ... sqrt("hello") ... except: ... pass ... ### tries to make it look like we're catching domain errors, but the overzealous exception catching disguises a really silly bug. We can make the exception more stringent, so that we only catch domain errors. According to: http://www.python.org/doc/lib/module-exceptions.html there's are a few standard exceptions that deal with domains, like ArithmeticError, ValueError, or TypeError. Let's adjust the code block above so we capture only those three: ### >>> try: ... sqrt("hello") ... except (ArithmeticError, ValueError, TypeError): ... pass ... Traceback (most recent call last): File "", line 2, in ? File "", line 2, in sqrt NameError: global name 'y' is not defined ### Also, it might be a good idea to print out that a certain exception happened, just so that you can tell the user exactly what's causing the domain error. The 'traceback' module can help with this: http://www.python.org/doc/lib/module-traceback.html For example: ### try: 1 / 0 except: print "There's an error!" ### tells us that some wacky thing happened, but: ### import traceback try: 1 / 0 except: print "Error:", traceback.format_exc() ### tells us that the error was caused by a ZeroDivisionError. Best of wishes to you! From kent37 at tds.net Mon Jan 10 20:19:11 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 20:19:16 2005 Subject: [Tutor] Modifying game of life In-Reply-To: References: Message-ID: <41E2D52F.4010105@tds.net> Kooser, Ara S wrote: > I was trying to modify the Game of Life to a percolation model (you can > pick what the probability of each point being occupied). I wrote the def > for the percolation however when I print the world all I get is > NONE NONE NONE NONE > Etc... > > I know my problem is in the line world[i, j] = percolation(perc). The > original code that Danny wrote had the line > world[i,j]= random.choice([LIVE,DEAD]). I am guessing that Danny's code > give a 50/50 chance of a site being occupied and assigns that to i,j . I > do not have the correct syntax to make the world[i,j] go through the > percolation def. I think i and j are not getting assigned a value. > > Does anyone have an suggestions, explanations, websites? Thanks. > > > Ara > > > > > import random > > perc = raw_input("Please enter a threshold between 0-1. ") Here perc will be a string. You need to convert it to a float using perc = float(perc) Kent > raw_input("Press return to make a world") > PERSON, EMPTY = '*', '.' > > def percolation(perc): > randval = random.random() > PERSON, EMPTY = '*', '.' > if randval > perc: > EMPTY > if randval < perc: > PERSON > > def make_random_world(M, N): > world = {} > for j in range(N): > for i in range(M): > world[i, j] = percolation(perc) > world['dimensions'] = (M, N) > return world > > def print_world(world): > > M, N = world['dimensions'] > for j in range(N): > for i in range(M): > print world[i, j], > print > > print_world(make_random_world(10, 10)) > ### > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Mon Jan 10 20:26:06 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 20:26:09 2005 Subject: [Tutor] Making Windows Text Editors Compile and Run Python In-Reply-To: <200501101616.j0AGGFZk002736@igate.highmark.com> References: <200501101616.j0AGGFZk002736@igate.highmark.com> Message-ID: <41E2D6CE.9060408@tds.net> I use TextPad for Python and Jython development. There is a Python syntax highlighter available on the TextPad web site you may be interested in. It's pretty easy to create a new tool in TextPad to run the current window. Here is a summary of my setup: In TextPad preferences, create a new tool. Go to the configure screen for the tool and enter Command: C:\Python24\python.exe or whatever the path is to your installation Parameters: $File Initial Folder: $FileDir Regular expression: ^.*"([^"]+)", *line ([0-9]+) File: 1 Line: 2 You can't run an interactive program from TextPad, you have to go to the command line for that. Kent james.homme@highmark.com wrote: > > > > Hi, > Idol is not an option for me since it does not work with my screen reader. > I am trying to use Notetab and Textpad to compile and run Python. Does > anyone have directions for either program to do this? > > Thanks. > > Jim > > > James D Homme, > Information Design + Development > Highmark Inc. > james.homme@highmark.com > 412-544-0527 > "A gentle answer turns away wrath, but a harsh word stirs up anger. > " > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From askoose at sandia.gov Mon Jan 10 20:29:38 2005 From: askoose at sandia.gov (Kooser, Ara S) Date: Mon Jan 10 20:30:02 2005 Subject: [Tutor] Modifying game of life Message-ID: Kent and Max. Thank you very much. It works fine now. Ara perc = raw_input("Please enter a threshold between 0-1. ") perc = float(perc) def percolation(perc): randval = random.random() print randval PERSON, EMPTY = '*', '.' if randval > perc: EMPTY return EMPTY elif randval < perc: PERSON return PERSON elif randval == perc: PERSON return PERSON From kent37 at tds.net Mon Jan 10 20:48:47 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 10 20:48:52 2005 Subject: [Tutor] Modifying game of life In-Reply-To: References: Message-ID: <41E2DC1F.7040901@tds.net> if randval > perc: EMPTY <= you don't need the lines like this, they don't do anything return EMPTY And you can test for less-than-or-equal in one test elif randval <= perc: return PERSON in fact since one of the tests has to be true you can just use else: if randval > perc: return EMPTY else: return PERSON Kent Kooser, Ara S wrote: > Kent and Max. Thank you very much. It works fine now. > > Ara > > > > perc = raw_input("Please enter a threshold between 0-1. ") > perc = float(perc) > > def percolation(perc): > randval = random.random() > print randval > PERSON, EMPTY = '*', '.' > if randval > perc: > EMPTY > return EMPTY > elif randval < perc: > PERSON > return PERSON > elif randval == perc: > PERSON > return PERSON > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at freenet.co.uk Mon Jan 10 20:51:19 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 10 20:50:48 2005 Subject: [Tutor] Slightly OT - Python/Java References: <004f01c4f6ae$e7e34470$16c68651@xp> Message-ID: <009a01c4f74d$c1cf0950$16c68651@xp> > Actually, I have the Intel assembler manuals at home, haven't even > looked at 'em. If we're going for speed... Actually modern C compilers usually mean that well written C can outperform even assembler, to write good assembler is just so hard that very few people can outsmart a good comiler... The real reason for still using assembler is to access bits of the machine that C just won't let you near - raw addresses and device ports for example. Using C the OS will map them to relative addresses... > Ah pity. I was hoping I could code for the JVM in Python style You can it just won't run as fast as native Java. But you can, for example, write classes in Python that can be used by Java programmers and you can write applets that can be downloaded and run in any Java aware browser. If ultimate performance isn't the differentiator - think text editor or card game - then Jython may be just fine. Alan G. From flaxeater at yahoo.com Mon Jan 10 21:01:41 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 10 21:01:44 2005 Subject: [Tutor] Slightly OT - Python/Java Message-ID: <20050110200141.86121.qmail@web54302.mail.yahoo.com> I just wanted to let everyone know a detail about Jython. It is not in fact an interpreter written in Java it is a dynamic Python to JavaByteCode compiler. At least that's how I understand this document. http://www.jython.org/applets/issues.html I found this interesting and I thought you all might find it useful. __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From alan.gauld at freenet.co.uk Mon Jan 10 21:04:29 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 10 21:04:05 2005 Subject: [Tutor] Slightly OT - Python/Java References: Message-ID: <00a901c4f74f$98a73af0$16c68651@xp> > ...on Java ... > quite like, much superior to C++, although it does lack the elegance of > Python and Ruby. Superior is a relative term. Java has lots of huge restrictions compared to C++ - the lack of operator overloading being maybe the biggest, since it prevents true sub typing of builtins and primitives - thus resulting in extra(and horrible) code. And the lack of functionpointers (inner classes not withstanding) is a big hit, and what about multiple inheritance - interfaces suck! Its also much slower - JIT compilers, and even native compilers, notwithstanding. I could go on...but... It is however, a simpler language to learn, and I must admit that having got used to garbage collection in Python going back top C and C++ is a shock... And the large standard library (even if the design is awful in places) is an advantage. > RubyCocoa anyway, so that'd be equivalent to using Objective-C). Have you used Objective C itself? I like it a lot. It combines much of what I like about both C++ and Python. > Also, Java has something which Python lacks (yet should have): > private/protected/public class members. In Python, everything is > public, which I consider to be a Bad Thing(TM). Why do you think it should have it? Have you run into a lot of problems with inappropriate access in Python programs? This often comes up from ex Java/C++ programmers yet access control like this was not part of any of the earliest OOP languages and nobody ever discussed it much(SMalltalk took one extreme - all data private, and Lisp the other - all public) But I don't recall have any big issues with lack of control, and I don't find it an issue in Python. What exactly do you find to be such a problem with the lack in Python? > Does runtime in Java include startup? Every time you start a java > program, it has to load a BIG runtime in memory (roughly 30 megs of > classes -- that's both Java's curse and blessing). True of Python too of course, you have the initial interpretation/compile stage before execution starts. > And of course, it's free (and written in Java). http://www.eclipse.org So many editors, so many languages :-) Alan G. From ps_python at yahoo.com Mon Jan 10 21:15:18 2005 From: ps_python at yahoo.com (kumar s) Date: Mon Jan 10 21:15:23 2005 Subject: [Tutor] Something is wrong in file input output functions. Message-ID: <20050110201518.1475.qmail@web53708.mail.yahoo.com> Dear group, I have written a small piece of code that takes a file and selects the columns that I am interested in and checks the value of the column on a condition (value that eqauls 25) and then write it the to another file. Code: import sys from string import split import string print "enter the file name" ### Takes the file name### psl = sys.stdin.readline() ### psl has the file object### f2 = sys.stdout.write("File name to write") def extCor(psl): ''' This function, splits the file and writes the desired columns to to another file only if the first column value equals 25.''' str_psl = psl.split('\n') str_psl = str_psl[5:] for ele in range(len(str_psl)): cols = split(str_psl[ele],'\t') des_cols = cols[0]+'\t'+cols[1]+'\t'+cols[8]+'\t'+cols[9]+'\t'+cols[11]+'\t'+cols[12]+'\t'+cols[13]+'\t'+cols[15]+'\t'+cols[16]+'\t'+cols[17]) if cols[0] == 25: '''This condition checks if the first column value == 25, then it writes it to the file, if not then it does not''' f2.write(des_cols) f2.write("\n") extCor(psl) Question: when i give it the file name that it should parse, I do not get to asked the file name i am interested in it gives me nothing. Please help me. Thanks K __________________________________ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com From michael at trollope.org Mon Jan 10 22:06:39 2005 From: michael at trollope.org (Michael Powe) Date: Mon Jan 10 22:06:44 2005 Subject: [Tutor] Something is wrong in file input output functions. In-Reply-To: <20050110201518.1475.qmail@web53708.mail.yahoo.com> References: <20050110201518.1475.qmail@web53708.mail.yahoo.com> Message-ID: <20050110210639.GA16093@titan.spiretech.com> On Mon, Jan 10, 2005 at 12:15:18PM -0800, kumar s wrote: > Dear group, > I have written a small piece of code that takes a file > and selects the columns that I am interested in and > checks the value of the column on a condition (value > that eqauls 25) and then write it the to another file. > > > > Code: > import sys > from string import split > import string > print "enter the file name" ### Takes the file name### > psl = sys.stdin.readline() ### psl has the file > object### I may be wrong but it does not appear to me that you open the files for reading/writing. The variable psl does not contain the file object, it contains the file name. To create a file object, you have to open it. E.g., f = open(psl,"r") w = open(out,"w") Now str_psl = f.readlines() creates an array of strings -- what you are trying to do with psl.split? I don't know what sys.stdout.write returns (and I'm not looking it up), but my guess would be something like the number of characters written. As a matter of form, I suggest writing all function definitions and then follow with execution code (input and function calls) -- makes it easier to read and follow what you're doing. I think it's unfortunate that python does not allow us to put function defs at the end of the file, so we can put execution code at the top ... but that's the way of it. I put my execution in a main function, and then call that. Seems tidier. HTH mp > > f2 = sys.stdout.write("File name to write") > def extCor(psl): > ''' This function, splits the file and writes the > desired columns to > to another file only if the first column value equals > 25.''' > str_psl = psl.split('\n') > str_psl = str_psl[5:] > for ele in range(len(str_psl)): > cols = split(str_psl[ele],'\t') > des_cols = > cols[0]+'\t'+cols[1]+'\t'+cols[8]+'\t'+cols[9]+'\t'+cols[11]+'\t'+cols[12]+'\t'+cols[13]+'\t'+cols[15]+'\t'+cols[16]+'\t'+cols[17]) > if cols[0] == 25: > '''This condition checks if the first > column value == 25, then it writes it to the file, if > not then it does not''' > f2.write(des_cols) > f2.write("\n") > > extCor(psl) > > > > Question: > when i give it the file name that it should parse, I > do not get to asked the file name i am interested in > it gives me nothing. Please help me. > Thanks > K > > > > __________________________________ > Do you Yahoo!? > The all-new My Yahoo! - Get yours free! > http://my.yahoo.com > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From cyresse at gmail.com Mon Jan 10 23:00:09 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 10 23:00:14 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: References: Message-ID: On Mon, 10 Jan 2005 01:33:39 +0000, Max Noel wrote: > > > (Also very frustrating is having lists that can be comprised only of > > one variable type.) > > First things first: don't use lists/arrays. Use Collections instead > (classes like ArrayList, HashMap, etc.). They're much more versatile, > and akin to lists and dictionaries in Python (sometimes even better). > Iterators over them are supported. > And while they can indeed only be comprised of one variable type as > well, said variable type is Object, from which all classes derive, so > that's not a problem (you only have to remember to cast as you get() > something from a Collection). Hehe, I'm not up to collections yet... working through Learning Java by O'Reilly. > > (Why can't a non-static > > method comparison be called from a static reference? What does that > > mean anyway? > > Er... What was your code like? (before and after correcting the error) it was (off top of head here) public class dude { public static void main(String [] args) { System.out.print(args[0]); if (doComp(args[0])); { System.out.println(" is true"); } else { System.out.println(" is false"); } private boolean doComp(String x) { int j = new int(x); #I can't quite remember how I did this precisely. if (j==1) { return True; } else { return False; } } } Getting more OT now. > > Runtime in Python 17.605 seconds (avg over 10 runs) > > Runtime in Java 12.302 seoncds (avg over 10 runs) > > Does runtime in Java include startup? Every time you start a java > program, it has to load a BIG runtime in memory (roughly 30 megs of > classes -- that's both Java's curse and blessing). No - it's just - create a datetime object, run iterations, create new datetime object and compare. I have no startup lag really. > > So yes - if Jython compiles to Java bytecode, and gives that 5 seconds > > faster performance, but I write it in Python, and it gives me that 27 > > minutes of not messing about counting my braces, I'm in heaven. ( I > > actually wrote a Python script to count braces.) > > Oh Ghost. You didn't actually write a Java program using a regular > text editor, did you? > And of course, it's free (and written in Java). http://www.eclipse.org > *sigh* I have no net at home at moment, which is very frustrating when I want to d/l documentation & editors. For the mo, it's all Notepad. Ick. Alan Gauld wrote: >Actually modern C compilers usually mean that well written C >can outperform even assembler, to write good assembler is just >so hard that very few people can outsmart a good comiler... Good to hear, means I can put off the asm adventure for a looong time. Of course, that will involve C.... :/ Cheers, 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 ba sic human duty, to take the consequences. From davholla2002 at yahoo.co.uk Mon Jan 10 23:31:21 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Mon Jan 10 23:31:24 2005 Subject: [Tutor] CGI problem Message-ID: <20050110223121.59703.qmail@web25402.mail.ukl.yahoo.com> I am trying to write a CGI program here is the code of the HTML

> > > Here is the python > #!/usr/bin/env python > > boroughdict = { > 'BROMLEY': 0.5, > 'LEWISHAM':0.1 > } > > > class dummy: #mocked up input obj > def __init__(self, str): > self.value = str > > import cgi, string > form = cgi.FieldStorage() > > def showboroughinfo(form): > try: > choice = form[inputkey].value > except: > choice = 'BROMLEY' > print "not getting from form" > choice = string.upper(choice) > info = boroughdict[choice] > #this is not being called > newinfo = calcinfo(info) > print "

"
>     print cgi.escape (newinfo) 
>     print "


" > #print '
' > > def calcinfo(info): > #this is calcuate stuff > info = str(info) > return info > > print "Content-type: text/html\n" > print "Languages" > print "

This will happen


" > > > showboroughinfo(form) > #print "Hi" > print '
' > > For some reason it is not get the info in the try > block. > If I do not have the try block I get this message :- > Traceback (most recent call last): > File > "/home/david/Documents/pyprogramming/cgi-bin/party2.py", > line 45, in ? > showboroughinfo(form) > File > "/home/david/Documents/pyprogramming/cgi-bin/party2.py", > line 19, in showboroughinfo > choice = form[inputkey].value > File "/usr/lib/python2.3/cgi.py", line 552, in > __getitem__ > raise KeyError, key > KeyError: 'borough' > > > Why is the input key not working ? > Thanks in advance. > > > > > > ___________________________________________________________ > ALL-NEW Yahoo! Messenger - all new features - even more fun! > http://uk.messenger.yahoo.com > _______________________________________________ Tutor maillist - > Tutor@python.org http://mail.python.org/mailman/listinfo/tutor > From maxnoel_fr at yahoo.fr Tue Jan 11 01:23:58 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 11 01:24:04 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: References: Message-ID: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> On Jan 10, 2005, at 22:00, Liam Clarke wrote: > Hehe, I'm not up to collections yet... working through Learning Java > by O'Reilly. If you already know a bit about OOP, I would recommend that you read bruce Eckel's "Thinking in Java". An excellent book, freely available on-line (do a quick Google search), and it taught me in 1 week all I needed to understand my courses (which dealt with OO analysis and design, Swing GUIs and some ore advanced topics). A good follow-up to that would be McMillan & Wiggleswrorth's "Java Programming - Advanced Topics", through which I'm currently reading. It has some really good stuff, including things about XML parsing with SAX and DOM... I may actually be about to understand how to use SAX and DOM, w00t! (*prepares a bottle of Champagne*) >>> (Why can't a non-static >>> method comparison be called from a static reference? What does that >>> mean anyway? >> >> Er... What was your code like? (before and after correcting >> the error) > > > it was (off top of head here) > > public class dude > > { > public static void main(String [] args) { > > System.out.print(args[0]); > if (doComp(args[0])); > { > System.out.println(" is true"); > } > else > { > System.out.println(" is false"); > } > > private boolean doComp(String x) { > int j = new int(x); #I can't quite remember how I did > this precisely. > > if (j==1) > { > return True; > } > else > { > return False; > } > } > > } Okay, I understand the problem, and it's quite logical once you know the reason. Basically, static methods and variables are class methods/variables. That is, they're bound to the class as a whole and not to any instance of it (in fact, they can be used without instanciating the class at all). Non-static members are instance members. They are bound to and act upon specific instances of the class. Thus, you can't use them before instanciating the class first. So, if I have a class called Foo which has the following: public static void bar() public void baz() I can use bar() straight away, by calling Foo.bar(). But if I want to use baz(), I first have to instanciate Foo, with something like Foo blah = new Foo(); and then call blah.baz(), which will then return whatever baz() computes based on blah's state. I guess that if you want your program to be wholly procedural, you need to make all your functions (methods) static, then. But if you're doing that, you're misusing Java. OO programming is where it shines. > I have no startup lag really. I stand corrected. But my other point is still valid -- see the Great Language Shootout Benchmarks for "better" (and customizable) language benchmarks. > *sigh* I have no net at home at moment, which is very frustrating when > I want to d/l documentation & editors. For the mo, it's all Notepad. > Ick. I feel your pain. Coding in any language is a chore with Notepad -- it comes down to whichever language requires the least (keyboard) typing being the least unpleasant to use. And Java, unlike Python, requires a lot of typing. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Tue Jan 11 01:24:58 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 11 01:25:01 2005 Subject: Fwd: [Tutor] Slightly OT - Python/Java Message-ID: <39E7F7D1-6367-11D9-8A9A-000393CBC88E@yahoo.fr> (*bangs head on keyboard* gah, I clicked Reply instead of Reply to All again -- sorry!) Begin forwarded message: > From: Max Noel > Date: January 11, 2005 00:09:11 GMT > To: "Alan Gauld" > Subject: Re: [Tutor] Slightly OT - Python/Java > > > On Jan 10, 2005, at 20:04, Alan Gauld wrote: > >> Superior is a relative term. Java has lots of huge restrictions >> compared to C++ - the lack of operator overloading being maybe >> the biggest, since it prevents true sub typing of builtins and >> primitives - thus resulting in extra(and horrible) code. > > Well, I've very rarely (if ever) used operator overloading, so I > can't say I miss it much ;) > However, you have a point in that I don't like the fact that > everything is not an object in Java. Leftover non-object types from > C/C++ (int, float, et al.) stand out like sore thumbs; however I can > see how the lack of operator overloading prevents them from > disappearing. I'm not sure how it works in Python, though; can you > subclass int, list, dict, etc.? Does it have operator overloading, or, > missing that, a way to extent operators into classes? > >> And the lack of functionpointers (inner classes not withstanding) >> is a big hit, and what about multiple inheritance - interfaces suck! > > True, the lack of function pointers is a problem, at least when > working procedurally. When going full-OO, I don't see where this can > be a problem (since Class itself is an object, you can create pointers > to classes). > Also, I agree that interfaces are a bizarre way of adding multiple > inheritance. I'm still wondering if there is a better way, though > (apart from just allowing multiple inheritance, which I still don't > know if it's a good idea or not). > >> Its also much slower - JIT compilers, and even native compilers, >> notwithstanding. I could go on...but... > > Of course. I never suggested using Java to write 3D state-of-the-art > games. For "standard" applications, though, it beats C/C++ any day of > the week (worst-case scenario, you can still extend it in C, can't > you? -- not sure about that). > And a JIT compiler (HotSpot) is a part of standard Java now, isn't > it? So I think it is relevant. (native compilers like GCJ, however, > aren't -- they kinda defeat the whole point of Java if you ask me) > >> It is however, a simpler language to learn, and I must admit >> that having got used to garbage collection in Python going >> back top C and C++ is a shock... And the large standard library >> (even if the design is awful in places) is an advantage. > > Yup. > >>> RubyCocoa anyway, so that'd be equivalent to using Objective-C). >> >> Have you used Objective C itself? I like it a lot. It combines >> much of what I like about both C++ and Python. > > Not yet. I tried learning it by myself last year, but the Cocoa > framework, although extremely powerful, was a bit overwhelming to me > (especially since I'd never done any real GUI programming). > I'll probably give it another shot sometime this year. > >>> Also, Java has something which Python lacks (yet should have): >>> private/protected/public class members. In Python, everything is >>> public, which I consider to be a Bad Thing(TM). >> >> Why do you think it should have it? Have you run into a lot of >> problems with inappropriate access in Python programs? This often >> comes up from ex Java/C++ programmers yet access control like >> this was not part of any of the earliest OOP languages and nobody >> ever discussed it much(SMalltalk took one extreme - all data >> private, and Lisp the other - all public) But I don't recall >> have any big issues with lack of control, and I don't find >> it an issue in Python. >> What exactly do you find to be such a problem with the lack >> in Python? > > Well, the fact that everything is public means that if you want a > certain form of encapsulation, you have to state in the docstrings > what should be used as an "external interface" (stuff like "This is an > internal method and may change/disappear in the next version! Don't > use it!") and hope that people who use your classes listen to what you > say. > I like the idea behind encapsulation, that you should be able to use > a class as a "black box" with a known interface and not worry about > the specifics. The way things are, Python gives you the possibility of > shooting yourself in the foot; or to be more accurate they prevent you > from preventing others from shooting themselves in the foot when they > use your classes. (does what I say make any sense?) > > A solution would be to do like in Ruby, where class/instance > variables have names starting with (respectively) @@ or @. I guess you > can do the same in Python (although it'd be __ and _, I suppose), but > the language doesn't enforce it. > (I know, I know, if I want Ruby, I know where to find it :p ) > >>> Does runtime in Java include startup? Every time you start a java >>> program, it has to load a BIG runtime in memory (roughly 30 megs of >>> classes -- that's both Java's curse and blessing). >> >> True of Python too of course, you have the initial >> interpretation/compile stage before execution starts. > > Indeed; however in Python this extremely fast due to the interpreter > being quite light compared to the behemoth that the JVM is (it can > take up to 10 seconds starting up on my G4 867 12" Powerbook). > Also, I suppose that the Java bytecode produced by javac is a bit > more optimized than the almost-instant bytecode compilation that > happens when you start up Python on a program, isn't it? (I mean, it's > slower to compile -- even with jikes -- , there's gotta be a reason > behind this, ne?) > >>> And of course, it's free (and written in Java). >> http://www.eclipse.org >> >> So many editors, so many languages :-) > > Hehe... So true. Eclipse is not an editor, though, it's an IDE (in > the same meaning as Visual Studio and XCode). Quite a > resource-consuming one at that, but a very efficient one. I wish Apple > could make XCode 2 as good as Eclipse is. > > Also, as I said, Eclipse can be extended with plugins, meaning it > could theoretically become as good with Python as it is with Java. > That'd be soooooo cool. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge > a perfect, immortal machine?" > > -- maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From dyoo at hkn.eecs.berkeley.edu Tue Jan 11 01:29:51 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 11 01:29:55 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: Message-ID: On Tue, 11 Jan 2005, Liam Clarke wrote: > > > (Why can't a non-static method comparison be called from a static > > > reference? What does that mean anyway? > > > > Er... What was your code like? (before and after correcting > > the error) Hi Liam, It's actually easier to see the reason if we do a comparative "buggy code" display between Java and Python. *grin* Here's an instance of what I think the error might be: /*** Java pseudocode: won't compile. ***/ public class TestStatic { public static void doSomething() { System.out.println("square(42) is " + square(42)); } public int square(int x) { return x * x; } } /******/ And here's a loose translation of the buggy Java code into buggy Python code. [meta: I'm not using Python's staticmethod() stuff yet; maybe someone else can show that approach too?] ### Python pseudocode: won't run right ### def doSomething(): print "square(42) is", TestStatic.square(42) class TestStatic: def square(self, x): return x * x ###### The bug should be more obvious now: it's a simple parameter passing problem. In Python, the 'self' parameter is explicitely defined and stated as part of the parameter list of any method. square() is actually a method that must take in an instance. Easy to see, since square takes in two parameters, but we are only passing one. In Java, the bug is harder to see, because the passing of 'this' (Java's equivalent of 'self') is all being done implicitely. In Java, static functions aren't bound to any particular instance, and since there's no 'this', we get that error about how "static member functions can't call nonstatic method functions." That being said, it's perfectly possible to do this: /******/ public class TestStatic { public static void doSomething() { TestStatic app = new TestStatic(); System.out.println("square(42) is " + app.square(42)); } public int square(int x) { return x * x; } } /******/ with its Python equivalent: ###### def doSomething(): app = TestStatic() print "square(42) is", app.square(42) class TestStatic: def square(self, x): return x * x ###### The explicitness of 'self' in Python really shines in examples like this. > > Oh Ghost. You didn't actually write a Java program using a > > regular text editor, did you? > > And of course, it's free (and written in Java). http://www.eclipse.org > > > > *sigh* I have no net at home at moment, which is very frustrating when I > want to d/l documentation & editors. For the mo, it's all Notepad. Ick. Don't inflict that kind of pain on yourself. Ask someone to burn a CD of Eclipse for you or something: just don't use bad tools like Notepad! *grin* Best of wishes to you! From amonroe at columbus.rr.com Tue Jan 11 01:39:30 2005 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue Jan 11 01:40:06 2005 Subject: [Tutor] Is there a way to force update the screen in tkinter? Message-ID: <71286861769.20050110193930@columbus.rr.com> I don't have the code here at home, but today I tried my first experiments in Tkinter. I set up a button that fired off a function to resize a rectangle in a canvas, with a for loop. Only problem is that the screen isn't repainted in all the steps of the for loop - only at the very end, when the rectangle is at its final, largest size. Can I make it repaint DURING the loop? Alan From dyoo at hkn.eecs.berkeley.edu Tue Jan 11 01:53:11 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 11 01:53:18 2005 Subject: [Tutor] Is there a way to force update the screen in tkinter? In-Reply-To: <71286861769.20050110193930@columbus.rr.com> Message-ID: On Mon, 10 Jan 2005, R. Alan Monroe wrote: > I don't have the code here at home, but today I tried my first > experiments in Tkinter. I set up a button that fired off a function to > resize a rectangle in a canvas, with a for loop. Only problem is that > the screen isn't repainted in all the steps of the for loop - only at > the very end, when the rectangle is at its final, largest size. Can I > make it repaint DURING the loop? Hi Alan, Yes, Tkinter queries up a bunch of events to refresh the window, but doesn't execute them directly until it has a chance to take control, that is, when control passes back into the mainloop. You can manually force a refresh of the window-refreshing events through the loop. Use update_idletasks() methods on the toplevel Tk, and it should refresh things properly. See: http://www.pythonware.com/library/tkinter/introduction/x9374-event-processing.htm which talks about it a little more. Hope this helps! From kent37 at tds.net Tue Jan 11 02:36:21 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 02:36:26 2005 Subject: [Tutor] Slightly OT - Python/Java In-Reply-To: <20050110200141.86121.qmail@web54302.mail.yahoo.com> References: <20050110200141.86121.qmail@web54302.mail.yahoo.com> Message-ID: <41E32D95.9060805@tds.net> Chad Crabtree wrote: > I just wanted to let everyone know a detail about Jython. It is not > in > fact an interpreter written in Java it is a dynamic Python to > JavaByteCode compiler. > At least that's how I understand this document. > http://www.jython.org/applets/issues.html > I found this interesting and I thought you all might find it useful. This is true but it misses part of the picture and to a user it is an insignificant implementation detail. Jython is a Python to Java byte code compiler plus a runtime system. Jython actually builds a whole object model to represent the Python runtime environment. There are objects representing Python classes, objects representing Python objects such as strings, lists, dicts, etc. Objects representing modules. and so on. Loosely speaking, this is similar to what the CPython runtime does. Jython code is compiled into Java byte codes that make heavy use of the runtime system. CPython code is compiled to Python byte codes that are interpreted by the CPython runtime. So it is correct to say that Jython is not an interpreter written in Java; it uses the interpreter that comes with Java (the Java VM). But to say it is (just) a dynamic compiler misses a big part of the implementation. To a user, none of this makes any difference. Jython is syntactically and semantically almost identical to CPython 2.1. You can use Jython interactively just like Python. You can run many of the same programs. The implementation details are quite different, but it is the same language. Max Noel wrote: >> On Jan 10, 2005, at 20:04, Alan Gauld wrote: >>> And the lack of functionpointers (inner classes not withstanding) >>> is a big hit, and what about multiple inheritance - interfaces suck! >> >> >> True, the lack of function pointers is a problem, at least when >> working procedurally. When going full-OO, I don't see where this can >> be a problem (since Class itself is an object, you can create pointers >> to classes). Most, if not all, uses of function references in Python could be rewritten to use interfaces and object instances in Java. But function references are way easier to read and code and *much* more concise. I use function references constantly in Python in ways I would never dream of using them in Java because it just isn't worth the hassle. >> Also, I agree that interfaces are a bizarre way of adding multiple >> inheritance. I'm still wondering if there is a better way, though >> (apart from just allowing multiple inheritance, which I still don't >> know if it's a good idea or not). Interfaces *don't* add multiple inheritance - you only get one implementation, all the others you have to code yourself or use hand-coded delegation. What a pain! Mixin classes can be very useful. >>>> Also, Java has something which Python lacks (yet should have): >>>> private/protected/public class members. In Python, everything is >>>> public, which I consider to be a Bad Thing(TM). >>> >>> >>> Why do you think it should have it? Have you run into a lot of >>> problems with inappropriate access in Python programs? This often >>> comes up from ex Java/C++ programmers yet access control like >>> this was not part of any of the earliest OOP languages and nobody >>> ever discussed it much(SMalltalk took one extreme - all data >>> private, and Lisp the other - all public) But I don't recall >>> have any big issues with lack of control, and I don't find >>> it an issue in Python. >>> What exactly do you find to be such a problem with the lack >>> in Python? >> >> >> Well, the fact that everything is public means that if you want a >> certain form of encapsulation, you have to state in the docstrings >> what should be used as an "external interface" (stuff like "This is an >> internal method and may change/disappear in the next version! Don't >> use it!") and hope that people who use your classes listen to what you >> say. >> I like the idea behind encapsulation, that you should be able to >> use a class as a "black box" with a known interface and not worry >> about the specifics. The way things are, Python gives you the >> possibility of shooting yourself in the foot; or to be more accurate >> they prevent you from preventing others from shooting themselves in >> the foot when they use your classes. (does what I say make any sense?) That's exactly it. Python doesn't give you the means to prevent others from shooting themselves in the foot. It *does* give you a few ways to point out where the gun is and where the foot is and to suggest that maybe you want to keep the one apart from the other :-) If you prefix attribute names with _ that is a clear signal to users that they shouldn't use it. The public interface of a class or module consists of all the attributes whose names don't start with _. In the case of modules, Python gives a little weight to this - module attributes whose names start with _ will not be imported with 'from foo import *', and module attributes whose names start with __ get mangled by the compiler into something else. In my work, I'm happy to not have to think about whether an attribute should be private or protected, etc etc. Python lets me focus on the problem at hand instead of on the language. Kent From kent37 at tds.net Tue Jan 11 02:38:22 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 02:38:27 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> Message-ID: <41E32E0E.7090802@tds.net> Max Noel wrote: > A good follow-up to that would be McMillan & Wiggleswrorth's "Java > Programming - Advanced Topics", through which I'm currently reading. It > has some really good stuff, including things about XML parsing with SAX > and DOM... > I may actually be about to understand how to use SAX and DOM, w00t! > (*prepares a bottle of Champagne*) If you are processing XML with Java you really owe it to yourself to learn about dom4j. Then you won't *have* to understand SAX and DOM, w00t w00t! :-) http://www.dom4j.org Kent From keridee at jayco.net Tue Jan 11 02:41:58 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 02:43:15 2005 Subject: [Tutor] Slightly OT - Python/Java References: Message-ID: <002a01c4f77e$df112800$b85428cf@JSLAPTOP> > *sigh* I have no net at home at moment, which is very frustrating when > I want to d/l documentation & editors. For the mo, it's all Notepad. > Ick. Call me stupid or whatever, but how do you send and receive mail to this list? Maybe someone on the list could send you Eclipse as an attachment maybe? Jacob Schmidt From phthenry at earthlink.net Tue Jan 11 02:50:23 2005 From: phthenry at earthlink.net (Paul Tremblay) Date: Tue Jan 11 02:51:02 2005 Subject: [Tutor] need to hide process Message-ID: <20050111015023.GC7118@localhost.localdomain> I am trying to hide a process from a python script and don't know if I can do so. The process is a pax command, and my unsucesful code is below. I am trying to read from a pax archive (similar to a tar archive) to get a list of files. As long as the pax archive is valid, the code below works fine. The problem arises when the pax archive is not valid. In this case, the pax commnad gives this message: ATTENTION! pax archive volume change required. Ready for archive volume: 1 Input archive name or "." to quit pax. Archive name > pax then waits for the user to type in a command. I would like to supress both the message and the need for user input, because whenever this message appears, it always means the pax archive is corrupt. So ther is no need to see this confusing and misleading message. However, no matter what I have tried, I can't get away from pax both displaying the message and demanding the user type in a command. Do I need to fork a process? I have never done this, and the documentation is confusing to me. Thanks Paul def make_verify_list(self, archive, write_obj): if self.__main.verbose: sys.stdout.write('Making file from archive...\n') if self.__main.platform == 'darwin': command = 'hfspax -f %s ' % archive elif self.__main.platform == 'linux': command = 'pax -f %s' % archive junk_obj, read_obj, error_obj = os.popen3(command ) errors = [] line = 1 while 1: line = read_obj.readline() if line: # add a leading /, which was stripped away line = '/%s' % line line = line.replace('//', '/') index = line.find('\/..namedfork\/rsrc$') if index > -1: continue index = line.find('\/..namedfork\/finf$') if index > -1: continue write_obj.write(line) else: break # test for errors error_result = error_obj.readlines() read_obj.close() junk_obj.close() error_obj.close() if error_result: return 1 -- ************************ *Paul Tremblay * *phthenry@earthlink.net* ************************ From maxnoel_fr at yahoo.fr Tue Jan 11 03:04:50 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 11 03:04:53 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <41E32E0E.7090802@tds.net> References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> <41E32E0E.7090802@tds.net> Message-ID: <2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr> On Jan 11, 2005, at 01:38, Kent Johnson wrote: > Max Noel wrote: >> A good follow-up to that would be McMillan & Wiggleswrorth's >> "Java Programming - Advanced Topics", through which I'm currently >> reading. It has some really good stuff, including things about XML >> parsing with SAX and DOM... >> I may actually be about to understand how to use SAX and DOM, >> w00t! (*prepares a bottle of Champagne*) > > If you are processing XML with Java you really owe it to yourself to > learn about dom4j. Then you won't *have* to understand SAX and DOM, > w00t w00t! dom4j? What is it? Is it part of the standard Java distribution? If not, where can it be found? (thanks for the pointer, anyway ^^) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Tue Jan 11 03:10:00 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 03:10:08 2005 Subject: [Tutor] My best GUI app so far. Message-ID: <000701c4f782$b1124430$425428cf@JSLAPTOP> Here's the code. If you have time, look it over and give me suggestions for improvement! (big toothy grin) ### Start of Calculator.py ### from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit0(self): self.ctb() self.distext.set(self.distext.get()+'0') def adddigit1(self): self.ctb() self.distext.set(self.distext.get()+'1') def adddigit2(self): self.ctb() self.distext.set(self.distext.get()+'2') def adddigit3(self): self.ctb() self.distext.set(self.distext.get()+'3') def adddigit4(self): self.ctb() self.distext.set(self.distext.get()+'4') def adddigit5(self): self.ctb() self.distext.set(self.distext.get()+'5') def adddigit6(self): self.ctb() self.distext.set(self.distext.get()+'6') def adddigit7(self): self.ctb() self.distext.set(self.distext.get()+'7') def adddigit8(self): self.ctb() self.distext.set(self.distext.get()+'8') def adddigit9(self): self.ctb() self.distext.set(self.distext.get()+'9') def adddigitdot(self): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self): if self.action: self.equal() self.oldnum = self.distext.get() self.action = '+' else: self.oldnum = self.distext.get() self.action = '+' self.shouldblank = True def subtract(self): if self.action: self.equal() self.oldnum = self.distext.get() self.action = '-' else: self.oldnum = self.distext.get() self.action = '-' self.shouldblank = True def multiply(self): if self.action: self.equal() self.oldnum = self.distext.get() self.action = '*' else: self.oldnum = self.distext.get() self.action = '*' self.shouldblank = True def divide(self): if self.action: self.equal() self.oldnum = self.distext.get() self.action = '/' else: self.oldnum = self.distext.get() self.action = '/' self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def createWidgets(self): self.distext = StringVar() self.display = Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.b0 = Button(self,text='0',command=self.adddigit0,width=4,height=3) self.b0.grid(row=5,column=1) self.b1 = Button(self,text='1',command=self.adddigit1,width=4,height=3) self.b1.grid(row=4,column=1) self.b2 = Button(self,text='2',command=self.adddigit2,width=4,height=3) self.b2.grid(row=4,column=2) self.b3 = Button(self,text='3',command=self.adddigit3,width=4,height=3) self.b3.grid(row=4,column=3) self.b4 = Button(self,text='4',command=self.adddigit4,width=4,height=3) self.b4.grid(row=3,column=1) self.b5 = Button(self,text='5',command=self.adddigit5,width=4,height=3) self.b5.grid(row=3,column=2) self.b6 = Button(self,text='6',command=self.adddigit6,width=4,height=3) self.b6.grid(row=3,column=3) self.b7 = Button(self,text='7',command=self.adddigit7,width=4,height=3) self.b7.grid(row=2,column=1) self.b8 = Button(self,text='8',command=self.adddigit8,width=4,height=3) self.b8.grid(row=2,column=2) self.b9 = Button(self,text='9',command=self.adddigit9,width=4,height=3) self.b9.grid(row=2,column=3) self.bdot = Button(self,text='.',command=self.adddigitdot,width=4,height=3) self.bdot.grid(row=5,column=2) self.equalsign = Button(self,text="=",command=self.equal,width=4,height=3) self.equalsign.grid(row=5,column=3) self.plussign = Button(self,text='+',command=self.add,width=4,height=3) self.plussign.grid(row=5,column=4) self.minussign = Button(self,text="-",command=self.subtract,width=4,height=3) self.minussign.grid(row=4,column=4) self.timessign = Button(self,text='x',command=self.multiply,width=4,height=3) self.timessign.grid(row=3,column=4) self.divsign = Button(self,text='/',command=self.divide,width=4,height=3) self.divsign.grid(row=2,column=4) self.clearb = Button(self,text='ON/C',command=self.clear,width=4,height=3) self.clearb.grid(row=1,column=4) self.mrc = Button(self,text='MRC',command=self.memrecall,width=4,height=3) self.mrc.grid(row=1,column=1) self.mm = Button(self,text="M-",command=self.memminus,width=4,height=3) self.mm.grid(row=1,column=2) self.mp = Button(self,text="M+",command=self.memplus,width=4,height=3) self.mp.grid(row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(fill='both') self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ### End of Calculator.py ### Is there some way that I could loop over those button definitions? proposal - code a = """ def adddigit%s(self): self.ctb() self.distext.set(self.distext.get()+'%s') """ for i in range(10): exec a % (i,i) Pretty cool, huh? I've got my thinking cap on today! Thanks in advance for suggestions, Jacob From maxnoel_fr at yahoo.fr Tue Jan 11 03:19:17 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 11 03:19:20 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr> References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> <41E32E0E.7090802@tds.net> <2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr> Message-ID: <321086EC-6377-11D9-8A9A-000393CBC88E@yahoo.fr> > dom4j? What is it? Is it part of the standard Java distribution? If > not, where can it be found? Update: Okay, looks like it's time to go to bed. The link was in bright blue and somehow I didn't see it. D'oh. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From jfouhy at paradise.net.nz Tue Jan 11 03:52:13 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Tue Jan 11 03:52:31 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <000701c4f782$b1124430$425428cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP> Message-ID: <1105411933.41e33f5d73d61@www.paradise.net.nz> Quoting "Jacob S." : > Is there some way that I could loop over those button definitions? You could try something like this: defArgs = { 'width':4, 'height':3 } # Format: ( label, callback, grid_row, grid_column ) buttons = [ ('0', self.adddigit0, 5, 1), ('1', self.adddigit1, 4, 1), ... ('x', self.multiply, 3, 4), ... ('M+', self.memplus, 1, 1) ] for label, callback, row, col in buttons: Button(self, text=label, command=callback, **defArgs).grid(row=row, column=col) Things to note with this approach: - Functions are first-class objects in python, so you can put them in lists and things. - You can convert between dictionaries and keyword arguments (with some restrictions) by using **. I like to use this as a way setting default arguments for buttons and things. Now, if you want to make your buttons bigger, you only have to change the code in one place. - I'm not "remembering" the buttons as I create them: they are created, immediately gridded, and then forgotton. Tkinter still remembers them, so everything still works. But I can't (easily) get access to them, if I wanted to change the labels or anything. You will have to think about whether this is an issue when you are coding --- but I don't think it is a problem here. Also, rather than defining functions adddigit0, adddigit1, etc, you could instead do: def addDigit(self, digit): self.ctb() self.distext.set(self.distext.get() + str(digit)) and then change your callbacks: buttons = [ ('0', lambda : self.addDigit(0), 5, 1), ('1', lambda : self.addDigit(1), 4, 1), ... ] I remember one of my lecturers saying that if you ever find yourself cutting and pasting when you are coding, there is something you could do better. Hope this helps :-) -- John. From kent37 at tds.net Tue Jan 11 04:09:02 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 04:09:05 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <000701c4f782$b1124430$425428cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP> Message-ID: <41E3434E.5030803@tds.net> My suggestions: - How about some white space! Yikes, you trying to make me go blind? - You don't actually need to save all the buttons to attributes. - Much of the button creation is common - I made a button creating function and call that. Copy / paste is not your friend, it is a danger! Refactor instead of copying! - Now you can get rid of all the adddigit functions by making a single generic adddigit and calling it with a lambda. - The four operation functions are identical except the operator; factor out handleOperation() (Copy / paste is not your friend!) - I didn't put the button generation in a loop, once you strip out the repeated arguments what is left is pretty readable as is. Kent from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit(self, digit): self.ctb() self.distext.set(self.distext.get()+digit) def adddigitdot(self): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self): self.handleOperator('+') def subtract(self): self.handleOperator('-') def multiply(self): self.handleOperator('*') def divide(self): self.handleOperator('/') def handleOperator(self, oper): if self.action: self.equal() self.oldnum = self.distext.get() self.action = oper else: self.oldnum = self.distext.get() self.action = oper self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) def createWidgets(self): self.distext = StringVar() self.display =Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.makeButton(text='0',command=lambda: self.adddigit('0'),row=5,column=1) self.makeButton(text='1',command=lambda: self.adddigit('1'),row=4,column=1) self.makeButton(text='2',command=lambda: self.adddigit('2'),row=4,column=2) self.makeButton(text='3',command=lambda: self.adddigit('3'),row=4,column=3) self.makeButton(text='4',command=lambda: self.adddigit('4'),row=3,column=1) self.makeButton(text='5',command=lambda: self.adddigit('5'),row=3,column=2) self.makeButton(text='6',command=lambda: self.adddigit('6'),row=3,column=3) self.makeButton(text='7',command=lambda: self.adddigit('7'),row=2,column=1) self.makeButton(text='8',command=lambda: self.adddigit('8'),row=2,column=2) self.makeButton(text='9',command=lambda: self.adddigit('9'),row=2,column=3) self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) self.makeButton(text="=",command=self.equal,row=5,column=3) self.makeButton(text='+',command=self.add,row=5,column=4) self.makeButton(text="-",command=self.subtract,row=4,column=4) self.makeButton(text='x',command=self.multiply,row=3,column=4) self.makeButton(text='/',command=self.divide,row=2,column=4) self.makeButton(text='ON/C',command=self.clear,row=1,column=4) self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) self.makeButton(text="M-",command=self.memminus,row=1,column=2) self.makeButton(text="M+",command=self.memplus,row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(fill='both') self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ### End of Calculator.py ### Jacob S. wrote: > Here's the code. If you have time, look it over and give me suggestions for > improvement! > (big toothy grin) > > ### Start of Calculator.py ### > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > def adddigit0(self): > self.ctb() > self.distext.set(self.distext.get()+'0') > def adddigit1(self): > self.ctb() > self.distext.set(self.distext.get()+'1') > def adddigit2(self): > self.ctb() > self.distext.set(self.distext.get()+'2') > def adddigit3(self): > self.ctb() > self.distext.set(self.distext.get()+'3') > def adddigit4(self): > self.ctb() > self.distext.set(self.distext.get()+'4') > def adddigit5(self): > self.ctb() > self.distext.set(self.distext.get()+'5') > def adddigit6(self): > self.ctb() > self.distext.set(self.distext.get()+'6') > def adddigit7(self): > self.ctb() > self.distext.set(self.distext.get()+'7') > def adddigit8(self): > self.ctb() > self.distext.set(self.distext.get()+'8') > def adddigit9(self): > self.ctb() > self.distext.set(self.distext.get()+'9') > def adddigitdot(self): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > def equal(self): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > def add(self): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = '+' > else: > self.oldnum = self.distext.get() > self.action = '+' > self.shouldblank = True > def subtract(self): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = '-' > else: > self.oldnum = self.distext.get() > self.action = '-' > self.shouldblank = True > def multiply(self): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = '*' > else: > self.oldnum = self.distext.get() > self.action = '*' > self.shouldblank = True > def divide(self): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = '/' > else: > self.oldnum = self.distext.get() > self.action = '/' > self.shouldblank = True > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > > def createWidgets(self): > self.distext = StringVar() > self.display = > Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.b0 = > Button(self,text='0',command=self.adddigit0,width=4,height=3) > self.b0.grid(row=5,column=1) > self.b1 = > Button(self,text='1',command=self.adddigit1,width=4,height=3) > self.b1.grid(row=4,column=1) > self.b2 = > Button(self,text='2',command=self.adddigit2,width=4,height=3) > self.b2.grid(row=4,column=2) > self.b3 = > Button(self,text='3',command=self.adddigit3,width=4,height=3) > self.b3.grid(row=4,column=3) > self.b4 = > Button(self,text='4',command=self.adddigit4,width=4,height=3) > self.b4.grid(row=3,column=1) > self.b5 = > Button(self,text='5',command=self.adddigit5,width=4,height=3) > self.b5.grid(row=3,column=2) > self.b6 = > Button(self,text='6',command=self.adddigit6,width=4,height=3) > self.b6.grid(row=3,column=3) > self.b7 = > Button(self,text='7',command=self.adddigit7,width=4,height=3) > self.b7.grid(row=2,column=1) > self.b8 = > Button(self,text='8',command=self.adddigit8,width=4,height=3) > self.b8.grid(row=2,column=2) > self.b9 = > Button(self,text='9',command=self.adddigit9,width=4,height=3) > self.b9.grid(row=2,column=3) > self.bdot = > Button(self,text='.',command=self.adddigitdot,width=4,height=3) > self.bdot.grid(row=5,column=2) > self.equalsign = > Button(self,text="=",command=self.equal,width=4,height=3) > self.equalsign.grid(row=5,column=3) > self.plussign = > Button(self,text='+',command=self.add,width=4,height=3) > self.plussign.grid(row=5,column=4) > self.minussign = > Button(self,text="-",command=self.subtract,width=4,height=3) > self.minussign.grid(row=4,column=4) > self.timessign = > Button(self,text='x',command=self.multiply,width=4,height=3) > self.timessign.grid(row=3,column=4) > self.divsign = > Button(self,text='/',command=self.divide,width=4,height=3) > self.divsign.grid(row=2,column=4) > self.clearb = > Button(self,text='ON/C',command=self.clear,width=4,height=3) > self.clearb.grid(row=1,column=4) > self.mrc = > Button(self,text='MRC',command=self.memrecall,width=4,height=3) > self.mrc.grid(row=1,column=1) > self.mm = > Button(self,text="M-",command=self.memminus,width=4,height=3) > self.mm.grid(row=1,column=2) > self.mp = > Button(self,text="M+",command=self.memplus,width=4,height=3) > self.mp.grid(row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(fill='both') > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ### End of Calculator.py ### > > Is there some way that I could loop over those button definitions? > > proposal - code > > a = """ > def adddigit%s(self): > self.ctb() > self.distext.set(self.distext.get()+'%s') > """ > for i in range(10): > exec a % (i,i) > > Pretty cool, huh? I've got my thinking cap on today! > > Thanks in advance for suggestions, > Jacob > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Tue Jan 11 04:39:07 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 04:39:04 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP> <41E3434E.5030803@tds.net> Message-ID: <001501c4f78f$1cd0b380$515428cf@JSLAPTOP> > - How about some white space! Yikes, you trying to make me go blind? Ahh. That's definitely a odd preference of mine. > - You don't actually need to save all the buttons to attributes. Great idea! > - Much of the button creation is common - I made a button creating function and call that. I was going to do that... but you can't send a parameter through command because you can't call it on definition. The lambda you used is perfect! > Copy / paste is not your friend, it is a danger! Refactor instead of copying! I agree -- I'm just too narrow sighted yet to see another way. You're helping! > - Now you can get rid of all the adddigit functions by making a single generic adddigit > and calling it with a lambda. Again, I needed the lambda thought. Brilliant! > - The four operation functions are identical except the operator; factor out handleOperation() > (Copy / paste is not your friend!) I didn't see that. I didn't copy/paste it. (I know, it's the same thing to type it all over again.) > - I didn't put the button generation in a loop, once you strip out the repeated arguments what is > left is pretty readable as is. > > Kent > > > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > > def adddigit(self, digit): > self.ctb() > self.distext.set(self.distext.get()+digit) > > def adddigitdot(self): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > > def equal(self): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > > def add(self): > self.handleOperator('+') > > def subtract(self): > self.handleOperator('-') > > def multiply(self): > self.handleOperator('*') > > def divide(self): > self.handleOperator('/') > > > def handleOperator(self, oper): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = oper > else: > self.oldnum = self.distext.get() > self.action = oper > self.shouldblank = True > > > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > > > def createWidgets(self): > self.distext = StringVar() > self.display =Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.makeButton(text='0',command=lambda: self.adddigit('0'),row=5,column=1) > self.makeButton(text='1',command=lambda: self.adddigit('1'),row=4,column=1) > self.makeButton(text='2',command=lambda: self.adddigit('2'),row=4,column=2) > self.makeButton(text='3',command=lambda: self.adddigit('3'),row=4,column=3) > self.makeButton(text='4',command=lambda: self.adddigit('4'),row=3,column=1) > self.makeButton(text='5',command=lambda: self.adddigit('5'),row=3,column=2) > self.makeButton(text='6',command=lambda: self.adddigit('6'),row=3,column=3) > self.makeButton(text='7',command=lambda: self.adddigit('7'),row=2,column=1) > self.makeButton(text='8',command=lambda: self.adddigit('8'),row=2,column=2) > self.makeButton(text='9',command=lambda: self.adddigit('9'),row=2,column=3) > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > self.makeButton(text="=",command=self.equal,row=5,column=3) > self.makeButton(text='+',command=self.add,row=5,column=4) > self.makeButton(text="-",command=self.subtract,row=4,column=4) > self.makeButton(text='x',command=self.multiply,row=3,column=4) > self.makeButton(text='/',command=self.divide,row=2,column=4) > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(fill='both') > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ### End of Calculator.py ### > > > > Jacob S. wrote: > > Here's the code. If you have time, look it over and give me suggestions for > > improvement! > > (big toothy grin) > > > > ### Start of Calculator.py ### > > from __future__ import division > > from Tkinter import * > > > > class Application(Frame): > > def ctb(self): > > if self.shouldblank: > > self.distext.set('') > > self.shouldblank = False > > def adddigit0(self): > > self.ctb() > > self.distext.set(self.distext.get()+'0') > > def adddigit1(self): > > self.ctb() > > self.distext.set(self.distext.get()+'1') > > def adddigit2(self): > > self.ctb() > > self.distext.set(self.distext.get()+'2') > > def adddigit3(self): > > self.ctb() > > self.distext.set(self.distext.get()+'3') > > def adddigit4(self): > > self.ctb() > > self.distext.set(self.distext.get()+'4') > > def adddigit5(self): > > self.ctb() > > self.distext.set(self.distext.get()+'5') > > def adddigit6(self): > > self.ctb() > > self.distext.set(self.distext.get()+'6') > > def adddigit7(self): > > self.ctb() > > self.distext.set(self.distext.get()+'7') > > def adddigit8(self): > > self.ctb() > > self.distext.set(self.distext.get()+'8') > > def adddigit9(self): > > self.ctb() > > self.distext.set(self.distext.get()+'9') > > def adddigitdot(self): > > if not self.distext.get().count('.'): > > self.ctb() > > self.distext.set(self.distext.get()+'.') > > def equal(self): > > if self.action: > > self.newnum = self.distext.get() > > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > > self.distext.set(self.newnum) > > self.oldnum = '0' > > self.action = '' > > self.shouldblank = True > > def add(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '+' > > else: > > self.oldnum = self.distext.get() > > self.action = '+' > > self.shouldblank = True > > def subtract(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '-' > > else: > > self.oldnum = self.distext.get() > > self.action = '-' > > self.shouldblank = True > > def multiply(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '*' > > else: > > self.oldnum = self.distext.get() > > self.action = '*' > > self.shouldblank = True > > def divide(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '/' > > else: > > self.oldnum = self.distext.get() > > self.action = '/' > > self.shouldblank = True > > def clear(self): > > self.action = '' > > self.oldnum = '0' > > self.distext.set('0') > > self.shouldblank = True > > def memrecall(self): > > self.distext.set(self.memory) > > self.shouldblank = True > > def memminus(self): > > self.memory = str(eval(self.memory+"-"+self.distext.get())) > > self.shouldblank = True > > def memplus(self): > > self.memory = str(eval(self.memory+"+"+self.distext.get())) > > self.shouldblank = True > > > > > > > > def createWidgets(self): > > self.distext = StringVar() > > self.display = > > Entry(self,textvariable=self.distext,width=22,justify='right') > > self.display.grid(row=0,column=1,columnspan=4) > > > > self.b0 = > > Button(self,text='0',command=self.adddigit0,width=4,height=3) > > self.b0.grid(row=5,column=1) > > self.b1 = > > Button(self,text='1',command=self.adddigit1,width=4,height=3) > > self.b1.grid(row=4,column=1) > > self.b2 = > > Button(self,text='2',command=self.adddigit2,width=4,height=3) > > self.b2.grid(row=4,column=2) > > self.b3 = > > Button(self,text='3',command=self.adddigit3,width=4,height=3) > > self.b3.grid(row=4,column=3) > > self.b4 = > > Button(self,text='4',command=self.adddigit4,width=4,height=3) > > self.b4.grid(row=3,column=1) > > self.b5 = > > Button(self,text='5',command=self.adddigit5,width=4,height=3) > > self.b5.grid(row=3,column=2) > > self.b6 = > > Button(self,text='6',command=self.adddigit6,width=4,height=3) > > self.b6.grid(row=3,column=3) > > self.b7 = > > Button(self,text='7',command=self.adddigit7,width=4,height=3) > > self.b7.grid(row=2,column=1) > > self.b8 = > > Button(self,text='8',command=self.adddigit8,width=4,height=3) > > self.b8.grid(row=2,column=2) > > self.b9 = > > Button(self,text='9',command=self.adddigit9,width=4,height=3) > > self.b9.grid(row=2,column=3) > > self.bdot = > > Button(self,text='.',command=self.adddigitdot,width=4,height=3) > > self.bdot.grid(row=5,column=2) > > self.equalsign = > > Button(self,text="=",command=self.equal,width=4,height=3) > > self.equalsign.grid(row=5,column=3) > > self.plussign = > > Button(self,text='+',command=self.add,width=4,height=3) > > self.plussign.grid(row=5,column=4) > > self.minussign = > > Button(self,text="-",command=self.subtract,width=4,height=3) > > self.minussign.grid(row=4,column=4) > > self.timessign = > > Button(self,text='x',command=self.multiply,width=4,height=3) > > self.timessign.grid(row=3,column=4) > > self.divsign = > > Button(self,text='/',command=self.divide,width=4,height=3) > > self.divsign.grid(row=2,column=4) > > self.clearb = > > Button(self,text='ON/C',command=self.clear,width=4,height=3) > > self.clearb.grid(row=1,column=4) > > self.mrc = > > Button(self,text='MRC',command=self.memrecall,width=4,height=3) > > self.mrc.grid(row=1,column=1) > > self.mm = > > Button(self,text="M-",command=self.memminus,width=4,height=3) > > self.mm.grid(row=1,column=2) > > self.mp = > > Button(self,text="M+",command=self.memplus,width=4,height=3) > > self.mp.grid(row=1,column=3) > > > > > > def __init__(self, master=None): > > Frame.__init__(self,master) > > self.master.title("Calculator by Jacob, Inc.") > > self.pack(fill='both') > > self.oldnum = '0' > > self.memory = '0' > > self.action = '' > > self.shouldblank = True > > self.createWidgets() > > > > app = Application() > > app.mainloop() > > ### End of Calculator.py ### > > > > Is there some way that I could loop over those button definitions? > > > > proposal - code > > > > a = """ > > def adddigit%s(self): > > self.ctb() > > self.distext.set(self.distext.get()+'%s') > > """ > > for i in range(10): > > exec a % (i,i) > > > > Pretty cool, huh? I've got my thinking cap on today! > > > > Thanks in advance for suggestions, > > Jacob > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Tue Jan 11 06:34:36 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 06:34:38 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP> <41E3434E.5030803@tds.net> Message-ID: <000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> Great! I took the improvements you gave me an added support for keys (So you can type in 1.25+2= instead of having to type the buttons.) As always, I encourage improvements to my code. Maybe that will be my disclaimer... I have always liked and wanted to adopt Liam's. Oh, by the way, I'm not trying to make you go blind, it's just I don't tend to worry about *my* eyesight and in my subconcious I'm just not considerate enough to implement healthy eye habits. sorry... Perhaps mine eyes should better feast on code of thou? (I'm trying to sound intellegent -- and probably failing.) (And no, the sentence is not mocking) Okay, Jacob, reality check -- it's after midnight... Here's the code. ###Start of Calculator.py### from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit(self, digit, *args): self.ctb() self.distext.set(self.distext.get()+digit) def adddigitdot(self,*args): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self,*args): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self,*args): self.handleOperator('+') def subtract(self,*args): self.handleOperator('-') def multiply(self,*args): self.handleOperator('*') def divide(self,*args): self.handleOperator('/') def handleOperator(self, oper): if self.action: self.equal() self.oldnum = self.distext.get() self.action = oper else: self.oldnum = self.distext.get() self.action = oper self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) def createWidgets(self): self.distext = StringVar() self.display =Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.makeButton(text='0',command=self.bl[0],row=5,column=1) self.makeButton(text='1',command=self.bl[1],row=4,column=1) self.makeButton(text='2',command=self.bl[2],row=4,column=2) self.makeButton(text='3',command=self.bl[3],row=4,column=3) self.makeButton(text='4',command=self.bl[4],row=3,column=1) self.makeButton(text='5',command=self.bl[5],row=3,column=2) self.makeButton(text='6',command=self.bl[6],row=3,column=3) self.makeButton(text='7',command=self.bl[7],row=2,column=1) self.makeButton(text='8',command=self.bl[8],row=2,column=2) self.makeButton(text='9',command=self.bl[9],row=2,column=3) self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) self.makeButton(text="=",command=self.equal,row=5,column=3) self.makeButton(text='+',command=self.add,row=5,column=4) self.makeButton(text="-",command=self.subtract,row=4,column=4) self.makeButton(text='x',command=self.multiply,row=3,column=4) self.makeButton(text='/',command=self.divide,row=2,column=4) self.makeButton(text='ON/C',command=self.clear,row=1,column=4) self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) self.makeButton(text="M-",command=self.memminus,row=1,column=2) self.makeButton(text="M+",command=self.memplus,row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(expand=True) m = lambda x: self.adddigit(x) self.bl = [lambda *x: self.adddigit('0',x), lambda *x: self.adddigit('1',x), lambda *x: self.adddigit('2',x), lambda *x: self.adddigit('3',x), lambda *x: self.adddigit('4',x), lambda *x: self.adddigit('5',x), lambda *x: self.adddigit('6',x), lambda *x: self.adddigit('7',x), lambda *x: self.adddigit('8',x), lambda *x: self.adddigit('9',x)] for y in range(10): self.bind_all(str(y),self.bl[y]) self.bind_all("+",lambda x: self.add(x)) self.bind_all("-",lambda x: self.subtract(x)) self.bind_all("*",lambda x: self.multiply(x)) self.bind_all("/",lambda x: self.divide(x)) self.bind_all("=",lambda x: self.equal(x)) self.bind_all(".",lambda x: self.adddigitdot(x)) self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ###End of Calculator.py### > My suggestions: > > - How about some white space! Yikes, you trying to make me go blind? > - You don't actually need to save all the buttons to attributes. > - Much of the button creation is common - I made a button creating function and call that. > Copy / paste is not your friend, it is a danger! Refactor instead of copying! > - Now you can get rid of all the adddigit functions by making a single generic adddigit > and calling it with a lambda. > - The four operation functions are identical except the operator; factor out handleOperation() > (Copy / paste is not your friend!) > > - I didn't put the button generation in a loop, once you strip out the repeated arguments what is > left is pretty readable as is. > > Kent > > > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > > def adddigit(self, digit): > self.ctb() > self.distext.set(self.distext.get()+digit) > > def adddigitdot(self): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > > def equal(self): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > > def add(self): > self.handleOperator('+') > > def subtract(self): > self.handleOperator('-') > > def multiply(self): > self.handleOperator('*') > > def divide(self): > self.handleOperator('/') > > > def handleOperator(self, oper): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = oper > else: > self.oldnum = self.distext.get() > self.action = oper > self.shouldblank = True > > > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > > > def createWidgets(self): > self.distext = StringVar() > self.display =Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.makeButton(text='0',command=lambda: self.adddigit('0'),row=5,column=1) > self.makeButton(text='1',command=lambda: self.adddigit('1'),row=4,column=1) > self.makeButton(text='2',command=lambda: self.adddigit('2'),row=4,column=2) > self.makeButton(text='3',command=lambda: self.adddigit('3'),row=4,column=3) > self.makeButton(text='4',command=lambda: self.adddigit('4'),row=3,column=1) > self.makeButton(text='5',command=lambda: self.adddigit('5'),row=3,column=2) > self.makeButton(text='6',command=lambda: self.adddigit('6'),row=3,column=3) > self.makeButton(text='7',command=lambda: self.adddigit('7'),row=2,column=1) > self.makeButton(text='8',command=lambda: self.adddigit('8'),row=2,column=2) > self.makeButton(text='9',command=lambda: self.adddigit('9'),row=2,column=3) > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > self.makeButton(text="=",command=self.equal,row=5,column=3) > self.makeButton(text='+',command=self.add,row=5,column=4) > self.makeButton(text="-",command=self.subtract,row=4,column=4) > self.makeButton(text='x',command=self.multiply,row=3,column=4) > self.makeButton(text='/',command=self.divide,row=2,column=4) > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(fill='both') > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ### End of Calculator.py ### > > > > Jacob S. wrote: > > Here's the code. If you have time, look it over and give me suggestions for > > improvement! > > (big toothy grin) > > > > ### Start of Calculator.py ### > > from __future__ import division > > from Tkinter import * > > > > class Application(Frame): > > def ctb(self): > > if self.shouldblank: > > self.distext.set('') > > self.shouldblank = False > > def adddigit0(self): > > self.ctb() > > self.distext.set(self.distext.get()+'0') > > def adddigit1(self): > > self.ctb() > > self.distext.set(self.distext.get()+'1') > > def adddigit2(self): > > self.ctb() > > self.distext.set(self.distext.get()+'2') > > def adddigit3(self): > > self.ctb() > > self.distext.set(self.distext.get()+'3') > > def adddigit4(self): > > self.ctb() > > self.distext.set(self.distext.get()+'4') > > def adddigit5(self): > > self.ctb() > > self.distext.set(self.distext.get()+'5') > > def adddigit6(self): > > self.ctb() > > self.distext.set(self.distext.get()+'6') > > def adddigit7(self): > > self.ctb() > > self.distext.set(self.distext.get()+'7') > > def adddigit8(self): > > self.ctb() > > self.distext.set(self.distext.get()+'8') > > def adddigit9(self): > > self.ctb() > > self.distext.set(self.distext.get()+'9') > > def adddigitdot(self): > > if not self.distext.get().count('.'): > > self.ctb() > > self.distext.set(self.distext.get()+'.') > > def equal(self): > > if self.action: > > self.newnum = self.distext.get() > > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > > self.distext.set(self.newnum) > > self.oldnum = '0' > > self.action = '' > > self.shouldblank = True > > def add(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '+' > > else: > > self.oldnum = self.distext.get() > > self.action = '+' > > self.shouldblank = True > > def subtract(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '-' > > else: > > self.oldnum = self.distext.get() > > self.action = '-' > > self.shouldblank = True > > def multiply(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '*' > > else: > > self.oldnum = self.distext.get() > > self.action = '*' > > self.shouldblank = True > > def divide(self): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = '/' > > else: > > self.oldnum = self.distext.get() > > self.action = '/' > > self.shouldblank = True > > def clear(self): > > self.action = '' > > self.oldnum = '0' > > self.distext.set('0') > > self.shouldblank = True > > def memrecall(self): > > self.distext.set(self.memory) > > self.shouldblank = True > > def memminus(self): > > self.memory = str(eval(self.memory+"-"+self.distext.get())) > > self.shouldblank = True > > def memplus(self): > > self.memory = str(eval(self.memory+"+"+self.distext.get())) > > self.shouldblank = True > > > > > > > > def createWidgets(self): > > self.distext = StringVar() > > self.display = > > Entry(self,textvariable=self.distext,width=22,justify='right') > > self.display.grid(row=0,column=1,columnspan=4) > > > > self.b0 = > > Button(self,text='0',command=self.adddigit0,width=4,height=3) > > self.b0.grid(row=5,column=1) > > self.b1 = > > Button(self,text='1',command=self.adddigit1,width=4,height=3) > > self.b1.grid(row=4,column=1) > > self.b2 = > > Button(self,text='2',command=self.adddigit2,width=4,height=3) > > self.b2.grid(row=4,column=2) > > self.b3 = > > Button(self,text='3',command=self.adddigit3,width=4,height=3) > > self.b3.grid(row=4,column=3) > > self.b4 = > > Button(self,text='4',command=self.adddigit4,width=4,height=3) > > self.b4.grid(row=3,column=1) > > self.b5 = > > Button(self,text='5',command=self.adddigit5,width=4,height=3) > > self.b5.grid(row=3,column=2) > > self.b6 = > > Button(self,text='6',command=self.adddigit6,width=4,height=3) > > self.b6.grid(row=3,column=3) > > self.b7 = > > Button(self,text='7',command=self.adddigit7,width=4,height=3) > > self.b7.grid(row=2,column=1) > > self.b8 = > > Button(self,text='8',command=self.adddigit8,width=4,height=3) > > self.b8.grid(row=2,column=2) > > self.b9 = > > Button(self,text='9',command=self.adddigit9,width=4,height=3) > > self.b9.grid(row=2,column=3) > > self.bdot = > > Button(self,text='.',command=self.adddigitdot,width=4,height=3) > > self.bdot.grid(row=5,column=2) > > self.equalsign = > > Button(self,text="=",command=self.equal,width=4,height=3) > > self.equalsign.grid(row=5,column=3) > > self.plussign = > > Button(self,text='+',command=self.add,width=4,height=3) > > self.plussign.grid(row=5,column=4) > > self.minussign = > > Button(self,text="-",command=self.subtract,width=4,height=3) > > self.minussign.grid(row=4,column=4) > > self.timessign = > > Button(self,text='x',command=self.multiply,width=4,height=3) > > self.timessign.grid(row=3,column=4) > > self.divsign = > > Button(self,text='/',command=self.divide,width=4,height=3) > > self.divsign.grid(row=2,column=4) > > self.clearb = > > Button(self,text='ON/C',command=self.clear,width=4,height=3) > > self.clearb.grid(row=1,column=4) > > self.mrc = > > Button(self,text='MRC',command=self.memrecall,width=4,height=3) > > self.mrc.grid(row=1,column=1) > > self.mm = > > Button(self,text="M-",command=self.memminus,width=4,height=3) > > self.mm.grid(row=1,column=2) > > self.mp = > > Button(self,text="M+",command=self.memplus,width=4,height=3) > > self.mp.grid(row=1,column=3) > > > > > > def __init__(self, master=None): > > Frame.__init__(self,master) > > self.master.title("Calculator by Jacob, Inc.") > > self.pack(fill='both') > > self.oldnum = '0' > > self.memory = '0' > > self.action = '' > > self.shouldblank = True > > self.createWidgets() > > > > app = Application() > > app.mainloop() > > ### End of Calculator.py ### > > > > Is there some way that I could loop over those button definitions? > > > > proposal - code > > > > a = """ > > def adddigit%s(self): > > self.ctb() > > self.distext.set(self.distext.get()+'%s') > > """ > > for i in range(10): > > exec a % (i,i) > > > > Pretty cool, huh? I've got my thinking cap on today! > > > > Thanks in advance for suggestions, > > Jacob > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From gopinathv at hcltech.com Tue Jan 11 07:09:36 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Tue Jan 11 07:09:59 2005 Subject: [Tutor] hi Message-ID: Hi Can any1 please tell me how do i open an editor in python running in linux os -----Original Message----- From: tutor-request@python.org [mailto:tutor-request@python.org] Sent: Tuesday, January 11, 2005 11:37 AM To: gopinathv@hcltech.com Subject: Welcome to the "Tutor" mailing list (Digest mode) Welcome to the Tutor@python.org mailing list! This list is for folks who want to ask (and/or answer) questions from folks who wish to learn how to program with Python. Feel free to ask even the most basic of questions -- that's what the list is for! To post to this list, send your email to: tutor@python.org General information about the mailing list is at: http://mail.python.org/mailman/listinfo/tutor If you ever want to unsubscribe or change your options (eg, switch to or from digest mode, change your password, etc.), visit your subscription page at: http://mail.python.org/mailman/options/tutor/gopinathv%40hcltech.com You can also make such adjustments via email by sending a message to: Tutor-request@python.org with the word `help' in the subject or body (don't include the quotes), and you will get back a message with instructions. You must know your password to change your options (including changing the password, itself) or to unsubscribe. It is: abc Normally, Mailman will remind you of your python.org mailing list passwords once every month, although you can disable this if you prefer. This reminder will also include instructions on how to unsubscribe or change your account options. There is also a button on your options page that will email your current password to you. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050111/84a01cc6/attachment.htm From alan.gauld at freenet.co.uk Tue Jan 11 07:12:44 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 11 07:12:46 2005 Subject: [Tutor] Slightly OT - Python/Java References: <20050110200141.86121.qmail@web54302.mail.yahoo.com> Message-ID: <00f101c4f7a4$912b7ca0$16c68651@xp> > I just wanted to let everyone know a detail about Jython. It is not > in > fact an interpreter written in Java it is a dynamic Python to > JavaByteCode compiler. Its several things, it includes jythonc (note the last letter) which is a JVM compiler. But jython is also a full implementation of the Python interpreter in Java, including the >>> prompt. Alan G. From nick at javacat.f2s.com Tue Jan 11 10:18:44 2005 From: nick at javacat.f2s.com (nick@javacat.f2s.com) Date: Tue Jan 11 10:19:01 2005 Subject: [Tutor] hi In-Reply-To: References: Message-ID: <1105435124.41e399f49daf1@webmail.freedom2surf.net> Quoting "Gopinath V, ASDC Chennai" : > Hi > Can any1 please tell me how do i open an editor in python running in > linux os > Hi Gopinath, try os.system, ie import os os.system('/usr/bin/xterm &') Regards Nick . From mi.janssen at gmail.com Tue Jan 11 10:20:54 2005 From: mi.janssen at gmail.com (Michael Janssen) Date: Tue Jan 11 10:20:57 2005 Subject: [Tutor] hi In-Reply-To: References: Message-ID: <1ff2dfbf050111012012145fa8@mail.gmail.com> On Tue, 11 Jan 2005 11:39:36 +0530, Gopinath V, ASDC Chennai wrote: > Can any1 please tell me how do i open an editor in python running in > linux os can be as easy as: os.system("editor-cmd") regards Michael From kent37 at tds.net Tue Jan 11 11:55:58 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 11:56:04 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP> <41E3434E.5030803@tds.net> <000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> Message-ID: <41E3B0BE.3030606@tds.net> Jacob S. wrote: > Great! I took the improvements you gave me an added support for keys (So you > can type in 1.25+2= instead of having to type the buttons.) As always, I > encourage improvements to my code. Maybe that will be my disclaimer... I > have always liked and wanted to adopt Liam's. > > Oh, by the way, I'm not trying to make you go blind, it's just I don't tend > to worry about *my* eyesight and in my subconcious I'm just not considerate > enough to implement healthy eye habits. sorry... I exagerated. I don't really think it is an eye health issue. I do think that careful use of white space can greatly enhance the readability of your code, which is good for others who might be reviewing it, and also for you when you come back to it later. > Okay, Jacob, reality check -- it's after midnight... > Here's the code. You can simplify the way you add in key bindings. First, you don't need to add the *args argument to all the handlers. You know what the argument will be (an event object) and you don't want to use it, so you can just throw it away in the lambdas. Kind of the opposite of what I did with adddigit - instead of adding an argument, you throw one away. So lambda *x: self.adddigit('0',x) becomes lambda event: self.adddigit('0') There is no need for an extra loop to set up the key bindings. You have all the information you need in makeButton. It could read like this: def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) if len(text) == 1 self.bind_all(text,lambda x: command()) Or you could pass in an optional argument for the key text so you can bind to 'ON/C' etc. Nice work, by the way :-) Kent > > ###Start of Calculator.py### > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > > def adddigit(self, digit, *args): > self.ctb() > self.distext.set(self.distext.get()+digit) > > def adddigitdot(self,*args): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > > def equal(self,*args): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > > def add(self,*args): > self.handleOperator('+') > > def subtract(self,*args): > self.handleOperator('-') > > def multiply(self,*args): > self.handleOperator('*') > > def divide(self,*args): > self.handleOperator('/') > > > def handleOperator(self, oper): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = oper > else: > self.oldnum = self.distext.get() > self.action = oper > self.shouldblank = True > > > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > > > def createWidgets(self): > self.distext = StringVar() > self.display > =Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.makeButton(text='0',command=self.bl[0],row=5,column=1) > self.makeButton(text='1',command=self.bl[1],row=4,column=1) > self.makeButton(text='2',command=self.bl[2],row=4,column=2) > self.makeButton(text='3',command=self.bl[3],row=4,column=3) > self.makeButton(text='4',command=self.bl[4],row=3,column=1) > self.makeButton(text='5',command=self.bl[5],row=3,column=2) > self.makeButton(text='6',command=self.bl[6],row=3,column=3) > self.makeButton(text='7',command=self.bl[7],row=2,column=1) > self.makeButton(text='8',command=self.bl[8],row=2,column=2) > self.makeButton(text='9',command=self.bl[9],row=2,column=3) > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > self.makeButton(text="=",command=self.equal,row=5,column=3) > self.makeButton(text='+',command=self.add,row=5,column=4) > self.makeButton(text="-",command=self.subtract,row=4,column=4) > self.makeButton(text='x',command=self.multiply,row=3,column=4) > self.makeButton(text='/',command=self.divide,row=2,column=4) > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(expand=True) > m = lambda x: self.adddigit(x) > self.bl = [lambda *x: self.adddigit('0',x), > lambda *x: self.adddigit('1',x), > lambda *x: self.adddigit('2',x), > lambda *x: self.adddigit('3',x), > lambda *x: self.adddigit('4',x), > lambda *x: self.adddigit('5',x), > lambda *x: self.adddigit('6',x), > lambda *x: self.adddigit('7',x), > lambda *x: self.adddigit('8',x), > lambda *x: self.adddigit('9',x)] > for y in range(10): > self.bind_all(str(y),self.bl[y]) > self.bind_all("+",lambda x: self.add(x)) > self.bind_all("-",lambda x: self.subtract(x)) > self.bind_all("*",lambda x: self.multiply(x)) > self.bind_all("/",lambda x: self.divide(x)) > self.bind_all("=",lambda x: self.equal(x)) > self.bind_all(".",lambda x: self.adddigitdot(x)) > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ###End of Calculator.py### > > > > > >>My suggestions: >> >>- How about some white space! Yikes, you trying to make me go blind? >>- You don't actually need to save all the buttons to attributes. >>- Much of the button creation is common - I made a button creating > > function and call that. > >> Copy / paste is not your friend, it is a danger! Refactor instead of > > copying! > >>- Now you can get rid of all the adddigit functions by making a single > > generic adddigit > >> and calling it with a lambda. >>- The four operation functions are identical except the operator; factor > > out handleOperation() > >> (Copy / paste is not your friend!) >> >>- I didn't put the button generation in a loop, once you strip out the > > repeated arguments what is > >>left is pretty readable as is. >> >>Kent >> >> >>from __future__ import division >>from Tkinter import * >> >>class Application(Frame): >> def ctb(self): >> if self.shouldblank: >> self.distext.set('') >> self.shouldblank = False >> >> def adddigit(self, digit): >> self.ctb() >> self.distext.set(self.distext.get()+digit) >> >> def adddigitdot(self): >> if not self.distext.get().count('.'): >> self.ctb() >> self.distext.set(self.distext.get()+'.') >> >> def equal(self): >> if self.action: >> self.newnum = self.distext.get() >> self.newnum = str(eval(self.oldnum+self.action+self.newnum)) >> self.distext.set(self.newnum) >> self.oldnum = '0' >> self.action = '' >> self.shouldblank = True >> >> def add(self): >> self.handleOperator('+') >> >> def subtract(self): >> self.handleOperator('-') >> >> def multiply(self): >> self.handleOperator('*') >> >> def divide(self): >> self.handleOperator('/') >> >> >> def handleOperator(self, oper): >> if self.action: >> self.equal() >> self.oldnum = self.distext.get() >> self.action = oper >> else: >> self.oldnum = self.distext.get() >> self.action = oper >> self.shouldblank = True >> >> >> def clear(self): >> self.action = '' >> self.oldnum = '0' >> self.distext.set('0') >> self.shouldblank = True >> >> def memrecall(self): >> self.distext.set(self.memory) >> self.shouldblank = True >> >> def memminus(self): >> self.memory = str(eval(self.memory+"-"+self.distext.get())) >> self.shouldblank = True >> >> def memplus(self): >> self.memory = str(eval(self.memory+"+"+self.distext.get())) >> self.shouldblank = True >> >> >> def makeButton(self, text, command, row, column): >> button = Button(self,text=text,command=command,width=4,height=3) >> button.grid(row=row,column=column) >> >> >> def createWidgets(self): >> self.distext = StringVar() >> self.display > > =Entry(self,textvariable=self.distext,width=22,justify='right') > >> self.display.grid(row=0,column=1,columnspan=4) >> >> self.makeButton(text='0',command=lambda: > > self.adddigit('0'),row=5,column=1) > >> self.makeButton(text='1',command=lambda: > > self.adddigit('1'),row=4,column=1) > >> self.makeButton(text='2',command=lambda: > > self.adddigit('2'),row=4,column=2) > >> self.makeButton(text='3',command=lambda: > > self.adddigit('3'),row=4,column=3) > >> self.makeButton(text='4',command=lambda: > > self.adddigit('4'),row=3,column=1) > >> self.makeButton(text='5',command=lambda: > > self.adddigit('5'),row=3,column=2) > >> self.makeButton(text='6',command=lambda: > > self.adddigit('6'),row=3,column=3) > >> self.makeButton(text='7',command=lambda: > > self.adddigit('7'),row=2,column=1) > >> self.makeButton(text='8',command=lambda: > > self.adddigit('8'),row=2,column=2) > >> self.makeButton(text='9',command=lambda: > > self.adddigit('9'),row=2,column=3) > >> self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) >> self.makeButton(text="=",command=self.equal,row=5,column=3) >> self.makeButton(text='+',command=self.add,row=5,column=4) >> self.makeButton(text="-",command=self.subtract,row=4,column=4) >> self.makeButton(text='x',command=self.multiply,row=3,column=4) >> self.makeButton(text='/',command=self.divide,row=2,column=4) >> self.makeButton(text='ON/C',command=self.clear,row=1,column=4) >> self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) >> self.makeButton(text="M-",command=self.memminus,row=1,column=2) >> self.makeButton(text="M+",command=self.memplus,row=1,column=3) >> >> >> def __init__(self, master=None): >> Frame.__init__(self,master) >> self.master.title("Calculator by Jacob, Inc.") >> self.pack(fill='both') >> self.oldnum = '0' >> self.memory = '0' >> self.action = '' >> self.shouldblank = True >> self.createWidgets() >> >>app = Application() >>app.mainloop() >>### End of Calculator.py ### >> >> >> >>Jacob S. wrote: >> >>>Here's the code. If you have time, look it over and give me suggestions > > for > >>>improvement! >>>(big toothy grin) >>> >>>### Start of Calculator.py ### >>>from __future__ import division >>>from Tkinter import * >>> >>>class Application(Frame): >>> def ctb(self): >>> if self.shouldblank: >>> self.distext.set('') >>> self.shouldblank = False >>> def adddigit0(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'0') >>> def adddigit1(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'1') >>> def adddigit2(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'2') >>> def adddigit3(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'3') >>> def adddigit4(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'4') >>> def adddigit5(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'5') >>> def adddigit6(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'6') >>> def adddigit7(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'7') >>> def adddigit8(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'8') >>> def adddigit9(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'9') >>> def adddigitdot(self): >>> if not self.distext.get().count('.'): >>> self.ctb() >>> self.distext.set(self.distext.get()+'.') >>> def equal(self): >>> if self.action: >>> self.newnum = self.distext.get() >>> self.newnum = str(eval(self.oldnum+self.action+self.newnum)) >>> self.distext.set(self.newnum) >>> self.oldnum = '0' >>> self.action = '' >>> self.shouldblank = True >>> def add(self): >>> if self.action: >>> self.equal() >>> self.oldnum = self.distext.get() >>> self.action = '+' >>> else: >>> self.oldnum = self.distext.get() >>> self.action = '+' >>> self.shouldblank = True >>> def subtract(self): >>> if self.action: >>> self.equal() >>> self.oldnum = self.distext.get() >>> self.action = '-' >>> else: >>> self.oldnum = self.distext.get() >>> self.action = '-' >>> self.shouldblank = True >>> def multiply(self): >>> if self.action: >>> self.equal() >>> self.oldnum = self.distext.get() >>> self.action = '*' >>> else: >>> self.oldnum = self.distext.get() >>> self.action = '*' >>> self.shouldblank = True >>> def divide(self): >>> if self.action: >>> self.equal() >>> self.oldnum = self.distext.get() >>> self.action = '/' >>> else: >>> self.oldnum = self.distext.get() >>> self.action = '/' >>> self.shouldblank = True >>> def clear(self): >>> self.action = '' >>> self.oldnum = '0' >>> self.distext.set('0') >>> self.shouldblank = True >>> def memrecall(self): >>> self.distext.set(self.memory) >>> self.shouldblank = True >>> def memminus(self): >>> self.memory = str(eval(self.memory+"-"+self.distext.get())) >>> self.shouldblank = True >>> def memplus(self): >>> self.memory = str(eval(self.memory+"+"+self.distext.get())) >>> self.shouldblank = True >>> >>> >>> >>> def createWidgets(self): >>> self.distext = StringVar() >>> self.display = >>>Entry(self,textvariable=self.distext,width=22,justify='right') >>> self.display.grid(row=0,column=1,columnspan=4) >>> >>> self.b0 = >>>Button(self,text='0',command=self.adddigit0,width=4,height=3) >>> self.b0.grid(row=5,column=1) >>> self.b1 = >>>Button(self,text='1',command=self.adddigit1,width=4,height=3) >>> self.b1.grid(row=4,column=1) >>> self.b2 = >>>Button(self,text='2',command=self.adddigit2,width=4,height=3) >>> self.b2.grid(row=4,column=2) >>> self.b3 = >>>Button(self,text='3',command=self.adddigit3,width=4,height=3) >>> self.b3.grid(row=4,column=3) >>> self.b4 = >>>Button(self,text='4',command=self.adddigit4,width=4,height=3) >>> self.b4.grid(row=3,column=1) >>> self.b5 = >>>Button(self,text='5',command=self.adddigit5,width=4,height=3) >>> self.b5.grid(row=3,column=2) >>> self.b6 = >>>Button(self,text='6',command=self.adddigit6,width=4,height=3) >>> self.b6.grid(row=3,column=3) >>> self.b7 = >>>Button(self,text='7',command=self.adddigit7,width=4,height=3) >>> self.b7.grid(row=2,column=1) >>> self.b8 = >>>Button(self,text='8',command=self.adddigit8,width=4,height=3) >>> self.b8.grid(row=2,column=2) >>> self.b9 = >>>Button(self,text='9',command=self.adddigit9,width=4,height=3) >>> self.b9.grid(row=2,column=3) >>> self.bdot = >>>Button(self,text='.',command=self.adddigitdot,width=4,height=3) >>> self.bdot.grid(row=5,column=2) >>> self.equalsign = >>>Button(self,text="=",command=self.equal,width=4,height=3) >>> self.equalsign.grid(row=5,column=3) >>> self.plussign = >>>Button(self,text='+',command=self.add,width=4,height=3) >>> self.plussign.grid(row=5,column=4) >>> self.minussign = >>>Button(self,text="-",command=self.subtract,width=4,height=3) >>> self.minussign.grid(row=4,column=4) >>> self.timessign = >>>Button(self,text='x',command=self.multiply,width=4,height=3) >>> self.timessign.grid(row=3,column=4) >>> self.divsign = >>>Button(self,text='/',command=self.divide,width=4,height=3) >>> self.divsign.grid(row=2,column=4) >>> self.clearb = >>>Button(self,text='ON/C',command=self.clear,width=4,height=3) >>> self.clearb.grid(row=1,column=4) >>> self.mrc = >>>Button(self,text='MRC',command=self.memrecall,width=4,height=3) >>> self.mrc.grid(row=1,column=1) >>> self.mm = >>>Button(self,text="M-",command=self.memminus,width=4,height=3) >>> self.mm.grid(row=1,column=2) >>> self.mp = >>>Button(self,text="M+",command=self.memplus,width=4,height=3) >>> self.mp.grid(row=1,column=3) >>> >>> >>> def __init__(self, master=None): >>> Frame.__init__(self,master) >>> self.master.title("Calculator by Jacob, Inc.") >>> self.pack(fill='both') >>> self.oldnum = '0' >>> self.memory = '0' >>> self.action = '' >>> self.shouldblank = True >>> self.createWidgets() >>> >>>app = Application() >>>app.mainloop() >>>### End of Calculator.py ### >>> >>>Is there some way that I could loop over those button definitions? >>> >>>proposal - code >>> >>>a = """ >>>def adddigit%s(self): >>> self.ctb() >>> self.distext.set(self.distext.get()+'%s') >>>""" >>>for i in range(10): >>> exec a % (i,i) >>> >>>Pretty cool, huh? I've got my thinking cap on today! >>> >>>Thanks in advance for suggestions, >>>Jacob >>> >>> >>>_______________________________________________ >>>Tutor maillist - Tutor@python.org >>>http://mail.python.org/mailman/listinfo/tutor >>> >> >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> >> > > > From keridee at jayco.net Tue Jan 11 14:39:31 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 14:40:10 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> <41E3B0BE.3030606@tds.net> Message-ID: <001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> Exception in Tkinter callback Traceback (most recent call last): File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__ return self.func(*args) TypeError: () takes exactly 1 argument (0 given) I got this error when trying to send command = lambda x: self.adddigit('1') to makeButton - and for all of the rest of the digits, too. The way I fixed it was to again put *x in from of the x in lambda - but I left it out in the key binding. The reason it needs that is because the key binding sends a Tkinter instance, event, to the second argument whereas the button command does not. So to allow for both of them to use the same adddigit function I had to let the lambda in the buttons to accept extra junk. I think. I also took out the list self.bl due to the fact that I am no longer using the list of lambdas in more than one place. (I'm not sure I was before either) Oh, I get your whitespace and readibility thing too. *grin* Here's the code again. ###Start of Calculator.py### from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit(self, digit): self.ctb() self.distext.set(self.distext.get()+digit) def adddigitdot(self): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self): self.handleOperator('+') def subtract(self): self.handleOperator('-') def multiply(self): self.handleOperator('*') def divide(self): self.handleOperator('/') def handleOperator(self, oper): if self.action: self.equal() self.oldnum = self.distext.get() self.action = oper else: self.oldnum = self.distext.get() self.action = oper self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) if len(text) == 1: self.bind_all(text,lambda x: command()) def createWidgets(self): self.distext = StringVar() self.display =Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.makeButton(text='0',command=lambda *x: self.adddigit('0'),row=5,column=1) self.makeButton(text='1',command=lambda *x: self.adddigit('1'),row=4,column=1) self.makeButton(text='2',command=lambda *x: self.adddigit('2'),row=4,column=2) self.makeButton(text='3',command=lambda *x: self.adddigit('3'),row=4,column=3) self.makeButton(text='4',command=lambda *x: self.adddigit('4'),row=3,column=1) self.makeButton(text='5',command=lambda *x: self.adddigit('5'),row=3,column=2) self.makeButton(text='6',command=lambda *x: self.adddigit('6'),row=3,column=3) self.makeButton(text='7',command=lambda *x: self.adddigit('7'),row=2,column=1) self.makeButton(text='8',command=lambda *x: self.adddigit('8'),row=2,column=2) self.makeButton(text='9',command=lambda *x: self.adddigit('9'),row=2,column=3) self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) self.makeButton(text="=",command=self.equal,row=5,column=3) self.makeButton(text='+',command=self.add,row=5,column=4) self.makeButton(text="-",command=self.subtract,row=4,column=4) self.makeButton(text='x',command=self.multiply,row=3,column=4) self.makeButton(text='/',command=self.divide,row=2,column=4) self.makeButton(text='ON/C',command=self.clear,row=1,column=4) self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) self.makeButton(text="M-",command=self.memminus,row=1,column=2) self.makeButton(text="M+",command=self.memplus,row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(expand=True) self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ###End of Calculator.py### > Jacob S. wrote: > > Great! I took the improvements you gave me an added support for keys (So you > > can type in 1.25+2= instead of having to type the buttons.) As always, I > > encourage improvements to my code. Maybe that will be my disclaimer... I > > have always liked and wanted to adopt Liam's. > > > > Oh, by the way, I'm not trying to make you go blind, it's just I don't tend > > to worry about *my* eyesight and in my subconcious I'm just not considerate > > enough to implement healthy eye habits. sorry... > > I exagerated. I don't really think it is an eye health issue. I do think that careful use of white > space can greatly enhance the readability of your code, which is good for others who might be > reviewing it, and also for you when you come back to it later. > > > Okay, Jacob, reality check -- it's after midnight... > > Here's the code. > > You can simplify the way you add in key bindings. > > First, you don't need to add the *args argument to all the handlers. You know what the argument will > be (an event object) and you don't want to use it, so you can just throw it away in the lambdas. > Kind of the opposite of what I did with adddigit - instead of adding an argument, you throw one away. So > lambda *x: self.adddigit('0',x) > becomes > lambda event: self.adddigit('0') > > There is no need for an extra loop to set up the key bindings. You have all the information you need > in makeButton. It could read like this: > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > if len(text) == 1 > self.bind_all(text,lambda x: command()) > > Or you could pass in an optional argument for the key text so you can bind to 'ON/C' etc. > > Nice work, by the way :-) > > Kent > > > > > ###Start of Calculator.py### > > from __future__ import division > > from Tkinter import * > > > > class Application(Frame): > > def ctb(self): > > if self.shouldblank: > > self.distext.set('') > > self.shouldblank = False > > > > def adddigit(self, digit, *args): > > self.ctb() > > self.distext.set(self.distext.get()+digit) > > > > def adddigitdot(self,*args): > > if not self.distext.get().count('.'): > > self.ctb() > > self.distext.set(self.distext.get()+'.') > > > > def equal(self,*args): > > if self.action: > > self.newnum = self.distext.get() > > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > > self.distext.set(self.newnum) > > self.oldnum = '0' > > self.action = '' > > self.shouldblank = True > > > > def add(self,*args): > > self.handleOperator('+') > > > > def subtract(self,*args): > > self.handleOperator('-') > > > > def multiply(self,*args): > > self.handleOperator('*') > > > > def divide(self,*args): > > self.handleOperator('/') > > > > > > def handleOperator(self, oper): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = oper > > else: > > self.oldnum = self.distext.get() > > self.action = oper > > self.shouldblank = True > > > > > > def clear(self): > > self.action = '' > > self.oldnum = '0' > > self.distext.set('0') > > self.shouldblank = True > > > > def memrecall(self): > > self.distext.set(self.memory) > > self.shouldblank = True > > > > def memminus(self): > > self.memory = str(eval(self.memory+"-"+self.distext.get())) > > self.shouldblank = True > > > > def memplus(self): > > self.memory = str(eval(self.memory+"+"+self.distext.get())) > > self.shouldblank = True > > > > > > def makeButton(self, text, command, row, column): > > button = Button(self,text=text,command=command,width=4,height=3) > > button.grid(row=row,column=column) > > > > > > def createWidgets(self): > > self.distext = StringVar() > > self.display > > =Entry(self,textvariable=self.distext,width=22,justify='right') > > self.display.grid(row=0,column=1,columnspan=4) > > > > self.makeButton(text='0',command=self.bl[0],row=5,column=1) > > self.makeButton(text='1',command=self.bl[1],row=4,column=1) > > self.makeButton(text='2',command=self.bl[2],row=4,column=2) > > self.makeButton(text='3',command=self.bl[3],row=4,column=3) > > self.makeButton(text='4',command=self.bl[4],row=3,column=1) > > self.makeButton(text='5',command=self.bl[5],row=3,column=2) > > self.makeButton(text='6',command=self.bl[6],row=3,column=3) > > self.makeButton(text='7',command=self.bl[7],row=2,column=1) > > self.makeButton(text='8',command=self.bl[8],row=2,column=2) > > self.makeButton(text='9',command=self.bl[9],row=2,column=3) > > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > > self.makeButton(text="=",command=self.equal,row=5,column=3) > > self.makeButton(text='+',command=self.add,row=5,column=4) > > self.makeButton(text="-",command=self.subtract,row=4,column=4) > > self.makeButton(text='x',command=self.multiply,row=3,column=4) > > self.makeButton(text='/',command=self.divide,row=2,column=4) > > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > > > > def __init__(self, master=None): > > Frame.__init__(self,master) > > self.master.title("Calculator by Jacob, Inc.") > > self.pack(expand=True) > > m = lambda x: self.adddigit(x) > > self.bl = [lambda *x: self.adddigit('0',x), > > lambda *x: self.adddigit('1',x), > > lambda *x: self.adddigit('2',x), > > lambda *x: self.adddigit('3',x), > > lambda *x: self.adddigit('4',x), > > lambda *x: self.adddigit('5',x), > > lambda *x: self.adddigit('6',x), > > lambda *x: self.adddigit('7',x), > > lambda *x: self.adddigit('8',x), > > lambda *x: self.adddigit('9',x)] > > for y in range(10): > > self.bind_all(str(y),self.bl[y]) > > self.bind_all("+",lambda x: self.add(x)) > > self.bind_all("-",lambda x: self.subtract(x)) > > self.bind_all("*",lambda x: self.multiply(x)) > > self.bind_all("/",lambda x: self.divide(x)) > > self.bind_all("=",lambda x: self.equal(x)) > > self.bind_all(".",lambda x: self.adddigitdot(x)) > > self.oldnum = '0' > > self.memory = '0' > > self.action = '' > > self.shouldblank = True > > self.createWidgets() > > > > app = Application() > > app.mainloop() > > ###End of Calculator.py### > > > > > > > > > > > >>My suggestions: > >> > >>- How about some white space! Yikes, you trying to make me go blind? > >>- You don't actually need to save all the buttons to attributes. > >>- Much of the button creation is common - I made a button creating > > > > function and call that. > > > >> Copy / paste is not your friend, it is a danger! Refactor instead of > > > > copying! > > > >>- Now you can get rid of all the adddigit functions by making a single > > > > generic adddigit > > > >> and calling it with a lambda. > >>- The four operation functions are identical except the operator; factor > > > > out handleOperation() > > > >> (Copy / paste is not your friend!) > >> > >>- I didn't put the button generation in a loop, once you strip out the > > > > repeated arguments what is > > > >>left is pretty readable as is. > >> > >>Kent > >> > >> > >>from __future__ import division > >>from Tkinter import * > >> > >>class Application(Frame): > >> def ctb(self): > >> if self.shouldblank: > >> self.distext.set('') > >> self.shouldblank = False > >> > >> def adddigit(self, digit): > >> self.ctb() > >> self.distext.set(self.distext.get()+digit) > >> > >> def adddigitdot(self): > >> if not self.distext.get().count('.'): > >> self.ctb() > >> self.distext.set(self.distext.get()+'.') > >> > >> def equal(self): > >> if self.action: > >> self.newnum = self.distext.get() > >> self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > >> self.distext.set(self.newnum) > >> self.oldnum = '0' > >> self.action = '' > >> self.shouldblank = True > >> > >> def add(self): > >> self.handleOperator('+') > >> > >> def subtract(self): > >> self.handleOperator('-') > >> > >> def multiply(self): > >> self.handleOperator('*') > >> > >> def divide(self): > >> self.handleOperator('/') > >> > >> > >> def handleOperator(self, oper): > >> if self.action: > >> self.equal() > >> self.oldnum = self.distext.get() > >> self.action = oper > >> else: > >> self.oldnum = self.distext.get() > >> self.action = oper > >> self.shouldblank = True > >> > >> > >> def clear(self): > >> self.action = '' > >> self.oldnum = '0' > >> self.distext.set('0') > >> self.shouldblank = True > >> > >> def memrecall(self): > >> self.distext.set(self.memory) > >> self.shouldblank = True > >> > >> def memminus(self): > >> self.memory = str(eval(self.memory+"-"+self.distext.get())) > >> self.shouldblank = True > >> > >> def memplus(self): > >> self.memory = str(eval(self.memory+"+"+self.distext.get())) > >> self.shouldblank = True > >> > >> > >> def makeButton(self, text, command, row, column): > >> button = Button(self,text=text,command=command,width=4,height=3) > >> button.grid(row=row,column=column) > >> > >> > >> def createWidgets(self): > >> self.distext = StringVar() > >> self.display > > > > =Entry(self,textvariable=self.distext,width=22,justify='right') > > > >> self.display.grid(row=0,column=1,columnspan=4) > >> > >> self.makeButton(text='0',command=lambda: > > > > self.adddigit('0'),row=5,column=1) > > > >> self.makeButton(text='1',command=lambda: > > > > self.adddigit('1'),row=4,column=1) > > > >> self.makeButton(text='2',command=lambda: > > > > self.adddigit('2'),row=4,column=2) > > > >> self.makeButton(text='3',command=lambda: > > > > self.adddigit('3'),row=4,column=3) > > > >> self.makeButton(text='4',command=lambda: > > > > self.adddigit('4'),row=3,column=1) > > > >> self.makeButton(text='5',command=lambda: > > > > self.adddigit('5'),row=3,column=2) > > > >> self.makeButton(text='6',command=lambda: > > > > self.adddigit('6'),row=3,column=3) > > > >> self.makeButton(text='7',command=lambda: > > > > self.adddigit('7'),row=2,column=1) > > > >> self.makeButton(text='8',command=lambda: > > > > self.adddigit('8'),row=2,column=2) > > > >> self.makeButton(text='9',command=lambda: > > > > self.adddigit('9'),row=2,column=3) > > > >> self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > >> self.makeButton(text="=",command=self.equal,row=5,column=3) > >> self.makeButton(text='+',command=self.add,row=5,column=4) > >> self.makeButton(text="-",command=self.subtract,row=4,column=4) > >> self.makeButton(text='x',command=self.multiply,row=3,column=4) > >> self.makeButton(text='/',command=self.divide,row=2,column=4) > >> self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > >> self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > >> self.makeButton(text="M-",command=self.memminus,row=1,column=2) > >> self.makeButton(text="M+",command=self.memplus,row=1,column=3) > >> > >> > >> def __init__(self, master=None): > >> Frame.__init__(self,master) > >> self.master.title("Calculator by Jacob, Inc.") > >> self.pack(fill='both') > >> self.oldnum = '0' > >> self.memory = '0' > >> self.action = '' > >> self.shouldblank = True > >> self.createWidgets() > >> > >>app = Application() > >>app.mainloop() > >>### End of Calculator.py ### > >> > >> > >> > >>Jacob S. wrote: > >> > >>>Here's the code. If you have time, look it over and give me suggestions > > > > for > > > >>>improvement! > >>>(big toothy grin) > >>> > >>>### Start of Calculator.py ### > >>>from __future__ import division > >>>from Tkinter import * > >>> > >>>class Application(Frame): > >>> def ctb(self): > >>> if self.shouldblank: > >>> self.distext.set('') > >>> self.shouldblank = False > >>> def adddigit0(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'0') > >>> def adddigit1(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'1') > >>> def adddigit2(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'2') > >>> def adddigit3(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'3') > >>> def adddigit4(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'4') > >>> def adddigit5(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'5') > >>> def adddigit6(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'6') > >>> def adddigit7(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'7') > >>> def adddigit8(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'8') > >>> def adddigit9(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'9') > >>> def adddigitdot(self): > >>> if not self.distext.get().count('.'): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'.') > >>> def equal(self): > >>> if self.action: > >>> self.newnum = self.distext.get() > >>> self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > >>> self.distext.set(self.newnum) > >>> self.oldnum = '0' > >>> self.action = '' > >>> self.shouldblank = True > >>> def add(self): > >>> if self.action: > >>> self.equal() > >>> self.oldnum = self.distext.get() > >>> self.action = '+' > >>> else: > >>> self.oldnum = self.distext.get() > >>> self.action = '+' > >>> self.shouldblank = True > >>> def subtract(self): > >>> if self.action: > >>> self.equal() > >>> self.oldnum = self.distext.get() > >>> self.action = '-' > >>> else: > >>> self.oldnum = self.distext.get() > >>> self.action = '-' > >>> self.shouldblank = True > >>> def multiply(self): > >>> if self.action: > >>> self.equal() > >>> self.oldnum = self.distext.get() > >>> self.action = '*' > >>> else: > >>> self.oldnum = self.distext.get() > >>> self.action = '*' > >>> self.shouldblank = True > >>> def divide(self): > >>> if self.action: > >>> self.equal() > >>> self.oldnum = self.distext.get() > >>> self.action = '/' > >>> else: > >>> self.oldnum = self.distext.get() > >>> self.action = '/' > >>> self.shouldblank = True > >>> def clear(self): > >>> self.action = '' > >>> self.oldnum = '0' > >>> self.distext.set('0') > >>> self.shouldblank = True > >>> def memrecall(self): > >>> self.distext.set(self.memory) > >>> self.shouldblank = True > >>> def memminus(self): > >>> self.memory = str(eval(self.memory+"-"+self.distext.get())) > >>> self.shouldblank = True > >>> def memplus(self): > >>> self.memory = str(eval(self.memory+"+"+self.distext.get())) > >>> self.shouldblank = True > >>> > >>> > >>> > >>> def createWidgets(self): > >>> self.distext = StringVar() > >>> self.display = > >>>Entry(self,textvariable=self.distext,width=22,justify='right') > >>> self.display.grid(row=0,column=1,columnspan=4) > >>> > >>> self.b0 = > >>>Button(self,text='0',command=self.adddigit0,width=4,height=3) > >>> self.b0.grid(row=5,column=1) > >>> self.b1 = > >>>Button(self,text='1',command=self.adddigit1,width=4,height=3) > >>> self.b1.grid(row=4,column=1) > >>> self.b2 = > >>>Button(self,text='2',command=self.adddigit2,width=4,height=3) > >>> self.b2.grid(row=4,column=2) > >>> self.b3 = > >>>Button(self,text='3',command=self.adddigit3,width=4,height=3) > >>> self.b3.grid(row=4,column=3) > >>> self.b4 = > >>>Button(self,text='4',command=self.adddigit4,width=4,height=3) > >>> self.b4.grid(row=3,column=1) > >>> self.b5 = > >>>Button(self,text='5',command=self.adddigit5,width=4,height=3) > >>> self.b5.grid(row=3,column=2) > >>> self.b6 = > >>>Button(self,text='6',command=self.adddigit6,width=4,height=3) > >>> self.b6.grid(row=3,column=3) > >>> self.b7 = > >>>Button(self,text='7',command=self.adddigit7,width=4,height=3) > >>> self.b7.grid(row=2,column=1) > >>> self.b8 = > >>>Button(self,text='8',command=self.adddigit8,width=4,height=3) > >>> self.b8.grid(row=2,column=2) > >>> self.b9 = > >>>Button(self,text='9',command=self.adddigit9,width=4,height=3) > >>> self.b9.grid(row=2,column=3) > >>> self.bdot = > >>>Button(self,text='.',command=self.adddigitdot,width=4,height=3) > >>> self.bdot.grid(row=5,column=2) > >>> self.equalsign = > >>>Button(self,text="=",command=self.equal,width=4,height=3) > >>> self.equalsign.grid(row=5,column=3) > >>> self.plussign = > >>>Button(self,text='+',command=self.add,width=4,height=3) > >>> self.plussign.grid(row=5,column=4) > >>> self.minussign = > >>>Button(self,text="-",command=self.subtract,width=4,height=3) > >>> self.minussign.grid(row=4,column=4) > >>> self.timessign = > >>>Button(self,text='x',command=self.multiply,width=4,height=3) > >>> self.timessign.grid(row=3,column=4) > >>> self.divsign = > >>>Button(self,text='/',command=self.divide,width=4,height=3) > >>> self.divsign.grid(row=2,column=4) > >>> self.clearb = > >>>Button(self,text='ON/C',command=self.clear,width=4,height=3) > >>> self.clearb.grid(row=1,column=4) > >>> self.mrc = > >>>Button(self,text='MRC',command=self.memrecall,width=4,height=3) > >>> self.mrc.grid(row=1,column=1) > >>> self.mm = > >>>Button(self,text="M-",command=self.memminus,width=4,height=3) > >>> self.mm.grid(row=1,column=2) > >>> self.mp = > >>>Button(self,text="M+",command=self.memplus,width=4,height=3) > >>> self.mp.grid(row=1,column=3) > >>> > >>> > >>> def __init__(self, master=None): > >>> Frame.__init__(self,master) > >>> self.master.title("Calculator by Jacob, Inc.") > >>> self.pack(fill='both') > >>> self.oldnum = '0' > >>> self.memory = '0' > >>> self.action = '' > >>> self.shouldblank = True > >>> self.createWidgets() > >>> > >>>app = Application() > >>>app.mainloop() > >>>### End of Calculator.py ### > >>> > >>>Is there some way that I could loop over those button definitions? > >>> > >>>proposal - code > >>> > >>>a = """ > >>>def adddigit%s(self): > >>> self.ctb() > >>> self.distext.set(self.distext.get()+'%s') > >>>""" > >>>for i in range(10): > >>> exec a % (i,i) > >>> > >>>Pretty cool, huh? I've got my thinking cap on today! > >>> > >>>Thanks in advance for suggestions, > >>>Jacob > >>> > >>> > >>>_______________________________________________ > >>>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 maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Tue Jan 11 14:45:03 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 14:44:55 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> <41E3B0BE.3030606@tds.net> Message-ID: <002101c4f7e3$c3421020$795428cf@JSLAPTOP> Ugggh, the code I sent looks ugly when it came back. Why does Outlook Express wait until you receive mail to wrap the text? Isn't there an on/off switch for the stupid wrapping that sucks and starts working at the wrong time? From kent37 at tds.net Tue Jan 11 14:56:34 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 14:56:40 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> <41E3B0BE.3030606@tds.net> <001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> Message-ID: <41E3DB12.9050805@tds.net> Jacob S. wrote: > Exception in Tkinter callback > Traceback (most recent call last): > File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__ > return self.func(*args) > TypeError: () takes exactly 1 argument (0 given) > > I got this error when trying to send command = lambda x: self.adddigit('1') > to makeButton - and for all of the > rest of the digits, too. The way I fixed it was to again put *x in from of > the x in lambda - but I left it out in the key binding. > The reason it needs that is because the key binding sends a Tkinter > instance, event, to the second argument whereas > the button command does not. So to allow for both of them to use the same > adddigit function I had to let the lambda in > the buttons to accept extra junk. I think. Ah, right you are. I should know better than to post untested code, it's usually buggy! I think you need to do the same thing for add(), subtract(), multiply() and divide(). For some reason I don't understand, for me add works from the keyboard and multiply doesn't!? > > I also took out the list self.bl due to the fact that I am no longer using > the list of lambdas in more than one place. > (I'm not sure I was before either) > > Oh, > > I get your > whitespace > and readibility > thing > too. *grin* Cool. Neatness counts! :-) Kent > > Here's the code again. > > ###Start of Calculator.py### > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > > def adddigit(self, digit): > self.ctb() > self.distext.set(self.distext.get()+digit) > > def adddigitdot(self): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > > def equal(self): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > > def add(self): > self.handleOperator('+') > > def subtract(self): > self.handleOperator('-') > > def multiply(self): > self.handleOperator('*') > > def divide(self): > self.handleOperator('/') > > > def handleOperator(self, oper): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = oper > else: > self.oldnum = self.distext.get() > self.action = oper > self.shouldblank = True > > > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > if len(text) == 1: > self.bind_all(text,lambda x: command()) > > > def createWidgets(self): > self.distext = StringVar() > self.display > =Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.makeButton(text='0',command=lambda *x: > self.adddigit('0'),row=5,column=1) > self.makeButton(text='1',command=lambda *x: > self.adddigit('1'),row=4,column=1) > self.makeButton(text='2',command=lambda *x: > self.adddigit('2'),row=4,column=2) > self.makeButton(text='3',command=lambda *x: > self.adddigit('3'),row=4,column=3) > self.makeButton(text='4',command=lambda *x: > self.adddigit('4'),row=3,column=1) > self.makeButton(text='5',command=lambda *x: > self.adddigit('5'),row=3,column=2) > self.makeButton(text='6',command=lambda *x: > self.adddigit('6'),row=3,column=3) > self.makeButton(text='7',command=lambda *x: > self.adddigit('7'),row=2,column=1) > self.makeButton(text='8',command=lambda *x: > self.adddigit('8'),row=2,column=2) > self.makeButton(text='9',command=lambda *x: > self.adddigit('9'),row=2,column=3) > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > self.makeButton(text="=",command=self.equal,row=5,column=3) > self.makeButton(text='+',command=self.add,row=5,column=4) > self.makeButton(text="-",command=self.subtract,row=4,column=4) > self.makeButton(text='x',command=self.multiply,row=3,column=4) > self.makeButton(text='/',command=self.divide,row=2,column=4) > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(expand=True) > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ###End of Calculator.py### From keridee at jayco.net Tue Jan 11 15:17:27 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 15:18:01 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP><41E3B0BE.3030606@tds.net><001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> <41E3DB12.9050805@tds.net> Message-ID: <000401c4f7e8$506a8cd0$6d5328cf@JSLAPTOP> > Ah, right you are. I should know better than to post untested code, it's usually buggy! > > I think you need to do the same thing for add(), subtract(), multiply() and divide(). For some > reason I don't understand, for me add works from the keyboard and multiply doesn't!? The same thing happened to me. That's when I realized that we were using 'x' for the text on the multiply button and binding 'x' to multiply(), whereas both of us were trying to use '*' to initiate multiply(). If I don't like the * in the text of the button, I'll have to set up a seperate binding. Here's the code again, a few things changed. -Added binding of spacebar to clear function -Added binding of enter key to equal - for use with the number off to the right of the keyboard. -Changed text on button so 'x' now reads '*' As always, Feel free to give suggestions. Thanks for helping out so much! Jacob Schmidt ###Start of Calculator.py### from __future__ import division from Tkinter import * class Application(Frame): def ctb(self): if self.shouldblank: self.distext.set('') self.shouldblank = False def adddigit(self, digit): self.ctb() self.distext.set(self.distext.get()+digit) def adddigitdot(self): if not self.distext.get().count('.'): self.ctb() self.distext.set(self.distext.get()+'.') def equal(self): if self.action: self.newnum = self.distext.get() self.newnum = str(eval(self.oldnum+self.action+self.newnum)) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' self.shouldblank = True def add(self): self.handleOperator('+') def subtract(self): self.handleOperator('-') def multiply(self): self.handleOperator('*') def divide(self): self.handleOperator('/') def handleOperator(self, oper): if self.action: self.equal() self.oldnum = self.distext.get() self.action = oper else: self.oldnum = self.distext.get() self.action = oper self.shouldblank = True def clear(self): self.action = '' self.oldnum = '0' self.distext.set('0') self.shouldblank = True def memrecall(self): self.distext.set(self.memory) self.shouldblank = True def memminus(self): self.memory = str(eval(self.memory+"-"+self.distext.get())) self.shouldblank = True def memplus(self): self.memory = str(eval(self.memory+"+"+self.distext.get())) self.shouldblank = True def makeButton(self, text, command, row, column): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) if len(text) == 1: self.bind_all(text,lambda x: command()) def createWidgets(self): self.distext = StringVar() self.display =Entry(self,textvariable=self.distext,width=22,justify='right') self.display.grid(row=0,column=1,columnspan=4) self.makeButton(text='0',command=lambda *x: self.adddigit('0'),row=5,column=1) self.makeButton(text='1',command=lambda *x: self.adddigit('1'),row=4,column=1) self.makeButton(text='2',command=lambda *x: self.adddigit('2'),row=4,column=2) self.makeButton(text='3',command=lambda *x: self.adddigit('3'),row=4,column=3) self.makeButton(text='4',command=lambda *x: self.adddigit('4'),row=3,column=1) self.makeButton(text='5',command=lambda *x: self.adddigit('5'),row=3,column=2) self.makeButton(text='6',command=lambda *x: self.adddigit('6'),row=3,column=3) self.makeButton(text='7',command=lambda *x: self.adddigit('7'),row=2,column=1) self.makeButton(text='8',command=lambda *x: self.adddigit('8'),row=2,column=2) self.makeButton(text='9',command=lambda *x: self.adddigit('9'),row=2,column=3) self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) self.makeButton(text="=",command=self.equal,row=5,column=3) self.makeButton(text='+',command=self.add,row=5,column=4) self.makeButton(text="-",command=self.subtract,row=4,column=4) self.makeButton(text='*',command=self.multiply,row=3,column=4) self.makeButton(text='/',command=self.divide,row=2,column=4) self.makeButton(text='ON/C',command=self.clear,row=1,column=4) self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) self.makeButton(text="M-",command=self.memminus,row=1,column=2) self.makeButton(text="M+",command=self.memplus,row=1,column=3) def __init__(self, master=None): Frame.__init__(self,master) self.master.title("Calculator by Jacob, Inc.") self.pack(expand=True) self.bind_all('',lambda x: self.clear()) self.bind_all('',lambda x: self.equal()) self.oldnum = '0' self.memory = '0' self.action = '' self.shouldblank = True self.createWidgets() app = Application() app.mainloop() ###End of Calculator.py### From keridee at jayco.net Tue Jan 11 16:25:34 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 16:25:53 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP><41E3B0BE.3030606@tds.net><001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> <41E3DB12.9050805@tds.net> Message-ID: <000301c4f7f1$d102fef0$b05428cf@JSLAPTOP> I guess my next big thing is to try to make a copy of a TI - 36X Solar -- That will be an undertaking for me. First I'll have to research the buttons to see what all they do. Then, I'll have to do all of the text and command changes on the buttons as part of the command of the second and third funtion buttons. Eww. This is quickly getting difficult - and interesting! There's probably something illegal about copying a trademarked calculator. Of course, if I make sure I don't make myself the author, or put my names on it... Oh, and posting the code is just like sharing the calculator I bought, right? Yeah, I love loopholes. Anyway, I don't think it will matter because this is too trivial. (Sound of hands rubbing together in anticipation of making such a big thing.) I'll post some code as soon as it's stable. Jacob Schmidt > Jacob S. wrote: > > Exception in Tkinter callback > > Traceback (most recent call last): > > File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__ > > return self.func(*args) > > TypeError: () takes exactly 1 argument (0 given) > > > > I got this error when trying to send command = lambda x: self.adddigit('1') > > to makeButton - and for all of the > > rest of the digits, too. The way I fixed it was to again put *x in from of > > the x in lambda - but I left it out in the key binding. > > The reason it needs that is because the key binding sends a Tkinter > > instance, event, to the second argument whereas > > the button command does not. So to allow for both of them to use the same > > adddigit function I had to let the lambda in > > the buttons to accept extra junk. I think. > > Ah, right you are. I should know better than to post untested code, it's usually buggy! > > I think you need to do the same thing for add(), subtract(), multiply() and divide(). For some > reason I don't understand, for me add works from the keyboard and multiply doesn't!? > > > > > I also took out the list self.bl due to the fact that I am no longer using > > the list of lambdas in more than one place. > > (I'm not sure I was before either) > > > > Oh, > > > > I get your > > whitespace > > and readibility > > thing > > too. *grin* > > Cool. Neatness counts! :-) > > Kent > > > > > Here's the code again. > > > > ###Start of Calculator.py### > > from __future__ import division > > from Tkinter import * > > > > class Application(Frame): > > def ctb(self): > > if self.shouldblank: > > self.distext.set('') > > self.shouldblank = False > > > > def adddigit(self, digit): > > self.ctb() > > self.distext.set(self.distext.get()+digit) > > > > def adddigitdot(self): > > if not self.distext.get().count('.'): > > self.ctb() > > self.distext.set(self.distext.get()+'.') > > > > def equal(self): > > if self.action: > > self.newnum = self.distext.get() > > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > > self.distext.set(self.newnum) > > self.oldnum = '0' > > self.action = '' > > self.shouldblank = True > > > > def add(self): > > self.handleOperator('+') > > > > def subtract(self): > > self.handleOperator('-') > > > > def multiply(self): > > self.handleOperator('*') > > > > def divide(self): > > self.handleOperator('/') > > > > > > def handleOperator(self, oper): > > if self.action: > > self.equal() > > self.oldnum = self.distext.get() > > self.action = oper > > else: > > self.oldnum = self.distext.get() > > self.action = oper > > self.shouldblank = True > > > > > > def clear(self): > > self.action = '' > > self.oldnum = '0' > > self.distext.set('0') > > self.shouldblank = True > > > > def memrecall(self): > > self.distext.set(self.memory) > > self.shouldblank = True > > > > def memminus(self): > > self.memory = str(eval(self.memory+"-"+self.distext.get())) > > self.shouldblank = True > > > > def memplus(self): > > self.memory = str(eval(self.memory+"+"+self.distext.get())) > > self.shouldblank = True > > > > > > def makeButton(self, text, command, row, column): > > button = Button(self,text=text,command=command,width=4,height=3) > > button.grid(row=row,column=column) > > if len(text) == 1: > > self.bind_all(text,lambda x: command()) > > > > > > def createWidgets(self): > > self.distext = StringVar() > > self.display > > =Entry(self,textvariable=self.distext,width=22,justify='right') > > self.display.grid(row=0,column=1,columnspan=4) > > > > self.makeButton(text='0',command=lambda *x: > > self.adddigit('0'),row=5,column=1) > > self.makeButton(text='1',command=lambda *x: > > self.adddigit('1'),row=4,column=1) > > self.makeButton(text='2',command=lambda *x: > > self.adddigit('2'),row=4,column=2) > > self.makeButton(text='3',command=lambda *x: > > self.adddigit('3'),row=4,column=3) > > self.makeButton(text='4',command=lambda *x: > > self.adddigit('4'),row=3,column=1) > > self.makeButton(text='5',command=lambda *x: > > self.adddigit('5'),row=3,column=2) > > self.makeButton(text='6',command=lambda *x: > > self.adddigit('6'),row=3,column=3) > > self.makeButton(text='7',command=lambda *x: > > self.adddigit('7'),row=2,column=1) > > self.makeButton(text='8',command=lambda *x: > > self.adddigit('8'),row=2,column=2) > > self.makeButton(text='9',command=lambda *x: > > self.adddigit('9'),row=2,column=3) > > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > > self.makeButton(text="=",command=self.equal,row=5,column=3) > > self.makeButton(text='+',command=self.add,row=5,column=4) > > self.makeButton(text="-",command=self.subtract,row=4,column=4) > > self.makeButton(text='x',command=self.multiply,row=3,column=4) > > self.makeButton(text='/',command=self.divide,row=2,column=4) > > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > > > > def __init__(self, master=None): > > Frame.__init__(self,master) > > self.master.title("Calculator by Jacob, Inc.") > > self.pack(expand=True) > > self.oldnum = '0' > > self.memory = '0' > > self.action = '' > > self.shouldblank = True > > self.createWidgets() > > > > app = Application() > > app.mainloop() > > ###End of Calculator.py### > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From mark.kels at gmail.com Tue Jan 11 17:16:20 2005 From: mark.kels at gmail.com (Mark Kels) Date: Tue Jan 11 17:16:23 2005 Subject: [Tutor] Python with MySQL ? Message-ID: Hi all. How can I send SQL querys to a MySQL database with a python-CGI program ? Thanks. -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon From kent37 at tds.net Tue Jan 11 17:46:47 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 17:46:55 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <000401c4f7e8$506a8cd0$6d5328cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP><41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP><41E3B0BE.3030606@tds.net><001601c4f7e3$091b7dd0$795428cf@JSLAPTOP> <41E3DB12.9050805@tds.net> <000401c4f7e8$506a8cd0$6d5328cf@JSLAPTOP> Message-ID: <41E402F7.6090001@tds.net> Jacob S. wrote: >> For some > >>reason I don't understand, for me add works from the keyboard and multiply > > doesn't!? > > The same thing happened to me. That's when I realized that we were using 'x' > for the text on the multiply button and binding 'x' to multiply(), whereas > both of us were trying to use '*' to initiate multiply(). Doh. Of course! BTW I tried the program with command=lambda : self.adddigit('0') etc (i.e. no *x) and it works fine. The extra (event) argument is consumed by the lambda in makeButton(). Probably it was needed in some intermediate version. > > If I don't like the * in the text of the button, I'll have to set up a > seperate binding. Here's the code again, a few things changed. -Added > binding of spacebar to clear function > -Added binding of enter key to equal - for use with the > number off to the right of the keyboard. > -Changed text on button so 'x' now reads '*' I would make these bindings in createWidgets(), that is closer to where the other bindings are made - it's helpful to keep like code together. Even better - make the extra bindings an option argument to makeButton(), like this: self.makeButton(text='ON/C',command=self.clear,row=1,column=4, extraBindings=['', 'C']) and def makeButton(self, text, command, row, column, extraBindings=[]): button = Button(self,text=text,command=command,width=4,height=3) button.grid(row=row,column=column) if len(text) == 1: self.bind_all(text,lambda x: command()) for binding in extraBindings: self.bind_all(binding,lambda x: command()) Kent > > As always, Feel free to give suggestions. > > Thanks for helping out so much! You're welcome! > Jacob Schmidt > > ###Start of Calculator.py### > from __future__ import division > from Tkinter import * > > class Application(Frame): > def ctb(self): > if self.shouldblank: > self.distext.set('') > self.shouldblank = False > > def adddigit(self, digit): > self.ctb() > self.distext.set(self.distext.get()+digit) > > def adddigitdot(self): > if not self.distext.get().count('.'): > self.ctb() > self.distext.set(self.distext.get()+'.') > > def equal(self): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True > > def add(self): > self.handleOperator('+') > > def subtract(self): > self.handleOperator('-') > > def multiply(self): > self.handleOperator('*') > > def divide(self): > self.handleOperator('/') > > > def handleOperator(self, oper): > if self.action: > self.equal() > self.oldnum = self.distext.get() > self.action = oper > else: > self.oldnum = self.distext.get() > self.action = oper > self.shouldblank = True > > > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True > > def memrecall(self): > self.distext.set(self.memory) > self.shouldblank = True > > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True > > > def makeButton(self, text, command, row, column): > button = Button(self,text=text,command=command,width=4,height=3) > button.grid(row=row,column=column) > if len(text) == 1: > self.bind_all(text,lambda x: command()) > > > def createWidgets(self): > self.distext = StringVar() > self.display > =Entry(self,textvariable=self.distext,width=22,justify='right') > self.display.grid(row=0,column=1,columnspan=4) > > self.makeButton(text='0',command=lambda *x: > self.adddigit('0'),row=5,column=1) > self.makeButton(text='1',command=lambda *x: > self.adddigit('1'),row=4,column=1) > self.makeButton(text='2',command=lambda *x: > self.adddigit('2'),row=4,column=2) > self.makeButton(text='3',command=lambda *x: > self.adddigit('3'),row=4,column=3) > self.makeButton(text='4',command=lambda *x: > self.adddigit('4'),row=3,column=1) > self.makeButton(text='5',command=lambda *x: > self.adddigit('5'),row=3,column=2) > self.makeButton(text='6',command=lambda *x: > self.adddigit('6'),row=3,column=3) > self.makeButton(text='7',command=lambda *x: > self.adddigit('7'),row=2,column=1) > self.makeButton(text='8',command=lambda *x: > self.adddigit('8'),row=2,column=2) > self.makeButton(text='9',command=lambda *x: > self.adddigit('9'),row=2,column=3) > self.makeButton(text='.',command=self.adddigitdot,row=5,column=2) > self.makeButton(text="=",command=self.equal,row=5,column=3) > self.makeButton(text='+',command=self.add,row=5,column=4) > self.makeButton(text="-",command=self.subtract,row=4,column=4) > self.makeButton(text='*',command=self.multiply,row=3,column=4) > self.makeButton(text='/',command=self.divide,row=2,column=4) > self.makeButton(text='ON/C',command=self.clear,row=1,column=4) > self.makeButton(text='MRC',command=self.memrecall,row=1,column=1) > self.makeButton(text="M-",command=self.memminus,row=1,column=2) > self.makeButton(text="M+",command=self.memplus,row=1,column=3) > > > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(expand=True) > self.bind_all('',lambda x: self.clear()) > self.bind_all('',lambda x: self.equal()) > self.oldnum = '0' > self.memory = '0' > self.action = '' > self.shouldblank = True > self.createWidgets() > > app = Application() > app.mainloop() > ###End of Calculator.py### > > From kp8 at mac.com Tue Jan 11 18:18:15 2005 From: kp8 at mac.com (kevin parks) Date: Tue Jan 11 18:18:23 2005 Subject: [Tutor] Time script help sought! In-Reply-To: <20041107171518.BAB291E400A@bag.python.org> References: <20041107171518.BAB291E400A@bag.python.org> Message-ID: I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing them in my time calculator and writing them in by hand. Now i realize, that i really need a script to do this because: 1. It turns out there are hundreds of pages of this stuff. 2. I have to do something similar in again soon. 3. By doing it by hand i am introducing wonderful new errors! 4. It all has to be typed up anyway (which means weeks of work and even more typos!) The input would like so: Item_1 TAPE_1 1 00:23 8:23 Item_2 TAPE_1 2 8:23 9:41 Item_3 TAPE_1 3 9:41 10:41 Item_3 TAPE_1 4 10:47 11:19 Item_3 TAPE_1 5 11:21 11:55 Item_3 TAPE_1 6 11:58 12:10 Item_3 TAPE_1 7 12:15 12:45 Defect in analog tape sound. Item_3 TAPE_1 8 12:58 24:20 Defect in analog tape sound. Item_4 TAPE_1 9 24:33 Item_4 TAPE_1 10 25:48 Item_4 TAPE_1 11 29:48 Item_4 TAPE_1 12 31:46 Item_4 TAPE_1 13 34:17 Electronic sounds. Item_4 TAPE_1 14 35:21 Item_4 TAPE_1 15 36:06 Item_4 TAPE_1 16 37:01 37:38 These are analog tapes that were digitized (on to CD or a digital tape) that have now been exported as individual files that are meant to be part of an on-line audio archive. The timings refer to the time display on the CD or digital tape. The now all have to adjusted so that each item starts at 0.00 since they have all been edited out of their context and are now all individual items that start at 00:00. So Item_1 which was started at 00:23 on the tape and ended at 8:23 needs to have 23 seconds subtracted to it so that it says: Item_1 TAPE_1 1 00:00 08:00 Item_2 TAPE_1 2 08:23 09:41 would change to: Item_2 TAPE_1 2 00:00 01:18 etc. but as always you may notice a wrinkle.... some items have many times (here 6) indicated: Item_3 TAPE_1 3 9:41 10:41 Item_3 TAPE_1 4 10:47 11:19 Item_3 TAPE_1 5 11:21 11:55 Item_3 TAPE_1 6 11:58 12:10 Item_3 TAPE_1 7 12:15 12:45 Defect in analog tape sound. Item_3 TAPE_1 8 12:58 24:20 Defect in analog tape sound. This is all a single sound file and these separate times mark where there was a break, defect, or edit in the individual item. These have to be adjusted as well to show where these events would appear in the new sound file which now starts at 00:00. Item_3 TAPE_1 3 00:00 01:00 ---- Item_3 TAPE_1 4 01:00 01:38 ---- Item_3 TAPE_1 5 01:38 02:14 ---- Item_3 TAPE_1 6 02:14 02:29 ---- Item_3 TAPE_1 7 02:29 03:04 Defect in analog tape sound. Item_3 TAPE_1 8 03:04 14:39 Defect in analog tape sound. Further wrinkles: Some have start and end times indicated, some only start times. I suppose that the output would ideally have both.... some have comments and others don't ... and I need these comments echo-ed or since i probably need to make a database or table eventually non comments just have some place holder. I'd have a lot of similar type calculations to do... I was hoping and praying that some one here was feeling generous and show me the way and then, of course i could modify that to do other tasks... Usually i am happy to take the long road and all but i'll be honest, i am in a big jam here and this huge task was just dumped on me. I am frankly a little desperate for help on this and hoping someone is feeling up to spoon feeding me a clear modifiable example that works. Sorry..... cheers, kevin k p 8 'at ' m a c 'dot' c o m From dyoo at hkn.eecs.berkeley.edu Tue Jan 11 19:55:31 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 11 19:55:37 2005 Subject: [Tutor] Python with MySQL ? In-Reply-To: Message-ID: On Tue, 11 Jan 2005, Mark Kels wrote: > How can I send SQL querys to a MySQL database with a python-CGI program ? Hi Mark, You'll want to grab a "Python DB API" module for MySQL. The best one I've seen for MySQL is 'MySQLdb': http://sourceforge.net/projects/mysql-python and you should probably grab that for your system. It should come with some examples to help you get started. Python.org has a section on Python's database support: http://www.python.org/topics/database/ with some documentation. There used to be a tutorial linked from Linux Journal there, but it's now restricted to subscribers! *grrrr* Here's an example program just to see how the pieces fit together: ### import MySQLdb connection = MySQLdb.connect(db="magic", user="dyoo", password="abracadabra") cursor = connection.cursor() cursor.execute("""select name from cards where tournament_type = 'restricted'""") for (name,) in cursor.fetchall(): print name cursor.close() connection.close() ### I hope this helps you get started! From phthenry at earthlink.net Tue Jan 11 20:01:02 2005 From: phthenry at earthlink.net (Paul Tremblay) Date: Tue Jan 11 20:01:40 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <20041107171518.BAB291E400A@bag.python.org> Message-ID: <20050111190102.GC7300@localhost.localdomain> Hi Kevin Is all your data in one big text file? Or do you have to type it in by hand and want some type of script to do the calcluations? I have to run right now. I'll be back in a few hours and can give you some more help then, if no one else has offered. Paul On Tue, Jan 11, 2005 at 12:18:15PM -0500, kevin parks wrote: > From: kevin parks > Date: Tue, 11 Jan 2005 12:18:15 -0500 > To: tutor@python.org > Cc: Kevin Parks > Subject: [Tutor] Time script help sought! > > I am kind of in a bit of a jam (okay a big jam) and i was hoping that > someone here could give me a quick hand. I had a few pages of time > calculations to do. So, i just started in on them typing them in my > time calculator and writing them in by hand. Now i realize, that i > really need a script to do this because: > > 1. It turns out there are hundreds of pages of this stuff. > 2. I have to do something similar in again soon. > 3. By doing it by hand i am introducing wonderful new errors! > 4. It all has to be typed up anyway (which means weeks of work and even > more typos!) > > The input would like so: > > Item_1 TAPE_1 1 00:23 8:23 > > Item_2 TAPE_1 2 8:23 9:41 > > Item_3 TAPE_1 3 9:41 10:41 > Item_3 TAPE_1 4 10:47 11:19 > Item_3 TAPE_1 5 11:21 11:55 > Item_3 TAPE_1 6 11:58 12:10 > Item_3 TAPE_1 7 12:15 12:45 Defect in analog tape sound. > Item_3 TAPE_1 8 12:58 24:20 Defect in analog tape sound. > > Item_4 TAPE_1 9 24:33 > Item_4 TAPE_1 10 25:48 > Item_4 TAPE_1 11 29:48 > Item_4 TAPE_1 12 31:46 > Item_4 TAPE_1 13 34:17 Electronic sounds. > Item_4 TAPE_1 14 35:21 > Item_4 TAPE_1 15 36:06 > Item_4 TAPE_1 16 37:01 37:38 > > These are analog tapes that were digitized (on to CD or a digital tape) > that have now been exported as individual files that are meant to be > part of an on-line audio archive. The timings refer to the time display > on the CD or digital tape. The now all have to adjusted so that each > item starts at 0.00 since they have all been edited out of their > context and are now all individual items that start at 00:00. So Item_1 > which was started at 00:23 on the tape and ended at 8:23 needs to have > 23 seconds subtracted to it so that it says: > > Item_1 TAPE_1 1 00:00 08:00 > > Item_2 TAPE_1 2 08:23 09:41 > > would change to: > > Item_2 TAPE_1 2 00:00 01:18 > > etc. > > but as always you may notice a wrinkle.... some items have many times > (here 6) indicated: > > Item_3 TAPE_1 3 9:41 10:41 > Item_3 TAPE_1 4 10:47 11:19 > Item_3 TAPE_1 5 11:21 11:55 > Item_3 TAPE_1 6 11:58 12:10 > Item_3 TAPE_1 7 12:15 12:45 Defect in analog tape sound. > Item_3 TAPE_1 8 12:58 24:20 Defect in analog tape sound. > > This is all a single sound file and these separate times mark where > there was a break, defect, or edit in the individual item. These have > to be adjusted as well to show where these events would appear in the > new sound file which now starts at 00:00. > > Item_3 TAPE_1 3 00:00 01:00 ---- > Item_3 TAPE_1 4 01:00 01:38 ---- > Item_3 TAPE_1 5 01:38 02:14 ---- > Item_3 TAPE_1 6 02:14 02:29 ---- > Item_3 TAPE_1 7 02:29 03:04 Defect in analog tape sound. > Item_3 TAPE_1 8 03:04 14:39 Defect in analog tape sound. > > Further wrinkles: Some have start and end times indicated, some only > start times. I suppose that the output would ideally have both.... some > have comments and others don't ... and I need these comments echo-ed or > since i probably need to make a database or table eventually non > comments just have some place holder. > > I'd have a lot of similar type calculations to do... I was hoping and > praying that some one here was feeling generous and show me the way and > then, of course i could modify that to do other tasks... Usually i am > happy to take the long road and all but i'll be honest, i am in a big > jam here and this huge task was just dumped on me. I am frankly a > little desperate for help on this and hoping someone is feeling up to > spoon feeding me a clear modifiable example that works. Sorry..... > > cheers, > > kevin > k p 8 'at ' m a c 'dot' c o m > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- ************************ *Paul Tremblay * *phthenry@earthlink.net* ************************ From johnp at milwaukielumber.com Tue Jan 11 20:01:17 2005 From: johnp at milwaukielumber.com (John Purser) Date: Tue Jan 11 20:01:54 2005 Subject: [Tutor] Python with MySQL ? In-Reply-To: Message-ID: <200501111901.j0BJ1Hj7016055@mail.morseintranet.com> A year of so ago I had a major data conversion project to deal with and used python, mysql, and the resources listed below to "get 'er done" (my abject apologies to the non-American readers who will not get that reference). It was very quick to get real results with this toolset. John Purser -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Danny Yoo Sent: Tuesday, January 11, 2005 10:56 To: Mark Kels Cc: tutor@python.org Subject: Re: [Tutor] Python with MySQL ? On Tue, 11 Jan 2005, Mark Kels wrote: > How can I send SQL querys to a MySQL database with a python-CGI program ? Hi Mark, You'll want to grab a "Python DB API" module for MySQL. The best one I've seen for MySQL is 'MySQLdb': http://sourceforge.net/projects/mysql-python and you should probably grab that for your system. It should come with some examples to help you get started. Python.org has a section on Python's database support: http://www.python.org/topics/database/ with some documentation. There used to be a tutorial linked from Linux Journal there, but it's now restricted to subscribers! *grrrr* Here's an example program just to see how the pieces fit together: ### import MySQLdb connection = MySQLdb.connect(db="magic", user="dyoo", password="abracadabra") cursor = connection.cursor() cursor.execute("""select name from cards where tournament_type = 'restricted'""") for (name,) in cursor.fetchall(): print name cursor.close() connection.close() ### I hope this helps you get started! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From dyoo at hkn.eecs.berkeley.edu Tue Jan 11 20:24:55 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 11 20:24:58 2005 Subject: [Tutor] Time script help sought! In-Reply-To: Message-ID: On Tue, 11 Jan 2005, kevin parks wrote: > but as always you may notice a wrinkle.... some items have many times > (here 6) indicated: > > Item_3 TAPE_1 3 9:41 10:41 > Item_3 TAPE_1 4 10:47 11:19 > Item_3 TAPE_1 5 11:21 11:55 > Item_3 TAPE_1 6 11:58 12:10 > Item_3 TAPE_1 7 12:15 12:45 Defect in analog tape sound. > Item_3 TAPE_1 8 12:58 24:20 Defect in analog tape sound. Hi Kevin, It may help make things more managable if you work on a smaller subproblem. Let's look at the time-joining problem and see if that's really as hard as it looks. Let's just select the time coordinates and concentrate on those for now. ###### 9:41 10:41 10:47 11:19 11:21 11:55 11:58 12:10 12:15 12:45 12:58 24:20 ###### I notice here that this representation is in minutes and seconds. It might be easier if we make the data all in seconds: we can do our numeric calculations much more easily if we're dealing with a single unit. ### def convertMinSecToSeconds(minSecString): """Converts a string of the form: min:sec into the integer number of seconds. """ min, sec = minSecString.split(":") return (int(min) * 60) + int(sec) ### If we need to go back from seconds back to the minute-second representation, we can write a function to go the other direction. Anyway, with that, we can now look at the problem purely in seconds: ### >>> times = """ ... 9:41 10:41 ... 10:47 11:19 ... 11:21 11:55 ... 11:58 12:10 ... 12:15 12:45 ... 12:58 24:20 ... """.split() >>> >>> times ['9:41', '10:41', '10:47', '11:19', '11:21', '11:55', '11:58', '12:10', '12:15', '12:45', '12:58', '24:20'] >>> >>> seconds = map(convertMinSecToSeconds, times) >>> seconds [581, 641, 647, 679, 681, 715, 718, 730, 735, 765, 778, 1460] ### That is, we now have some input, like: ###### initialInput = [(581, 641), (647, 679), (681, 715), (718, 730), (735, 765), (778, 1460)] ###### And now we want to turn it into something like: ### expectedResult = [(0, 60), (60, 98), (98, 134), (134, 149), (149, 184), (184, 879)] ### Can you write a function that takes this 'initialInput' and produces that 'expectedResult'? If so, your problem's pretty much solved. If you have more questions, please feel free to ask. From ps_python at yahoo.com Tue Jan 11 20:55:54 2005 From: ps_python at yahoo.com (kumar s) Date: Tue Jan 11 20:55:57 2005 Subject: [Tutor] please help: conditional statement and printing element Message-ID: <20050111195554.87122.qmail@web53707.mail.yahoo.com> Dear group, For some reason my brain cannot think of any other option than what I have in my script. Could any one please help me in suggesting. What I have : (File name : psl) 22 2 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 22 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 24 1 457:411 25 0 22 0 457:411 25 0 21 0 457:411 25 0 25 0 457:411 25 0 25 0 457:411 25 0 What to do: I want to print values that are 25 in column 1 and not the other values such as 24,22,21 etc. My script: >>> for i in range(len(psl)): col = split(psl[i],'\t') col1 = col[0] if col1 == 25: print col[0]+'\t'+col[1]+'\t'+col[17] >>> Result: I get nothing. Am I doing something very wrong. Why isnt if col1 == 25: functional. My idea is to check if col[0] == 25: then print columns 1,18 etc. Can you please help me. Thanks K __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From ismaelgf at adinet.com.uy Tue Jan 11 21:07:38 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Tue Jan 11 21:06:54 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <20041107171518.BAB291E400A@bag.python.org> Message-ID: <41E4320A.40309@adinet.com.uy> kevin parks wrote: > I am kind of in a bit of a jam (okay a big jam) and i was hoping that > someone here could give me a quick hand. I had a few pages of time > calculations to do. So, i just started in on them typing them in my > time calculator and writing them in by hand. Now i realize, that i > really need a script to do this because: [...] Ok, here you are. It isn't pretty, but it's the best I can do. I would appreciate any comments regarding how it could be done better. Im just a newbie, learning how to program and Python at the same time... BTW: This program doesn't save the result (could be solved by pickling the list?), or allow you to edit the records (re-use the last part of the code), and doesn't have item-tape awareness (so, if you make mistakes in start-end time, the program will never know). Also, the output is not like yours, because I have different print for each thing (make it only one print, add enough spacing and that's it) HTH Ismael class Track: def __init__(self, itemNumb, tapeNumb, trackNumb, startTime, endTime, comment): self.itemNumb = itemNumb self.tapeNumb = tapeNumb self.trackNumb = trackNumb self.startTime = startTime self.endTime = endTime self.comment = comment def __repr__(self): ### This determines how the object reacts to "print object" return str({"itemNumb":self.itemNumb, "tapeNumb":self.tapeNumb, "trackNumb":self.trackNumb, "startTime":self.startTime, "endTime":self.endTime, "comment":self.comment}) def time2sec(timeOrig): ''' Takes "min:sec" and converts it to seconds''' try: timeOrig = timeOrig.split(":") return int(timeOrig[0])*60+int(timeOrig[1]) except IndexError: return "Error" except ValueError: return "" def sec2time(timeOrig): ''' Takes "sec" and converts it to "min:sec"''' return str(timeOrig/60)+":"+str(timeOrig%60) def getInput(inpType): if inpType == "integer": try: value = raw_input() value = int(value) return value except ValueError: if value == "": return "break" print "Value Erorr, type again" return None if inpType == "time": try: value = raw_input() if time2sec(value) == "Error": print "Format Error, type again" return None return value except ValueError: print "Value Erorr, type again" return None if inpType == "time2": value = raw_input() if time2sec(value) == "Error": print "Format Error, type again" return None return value i = 0 Tapes = [] print "Press ENTER on Item if you wish to finish" while True: ### Inital values for each loop item = None tape = None track = None start = None end = None comment = None ### i = i+1 ### Get everything while item == None: print "Item_", item = getInput("integer") if item == "break": break while tape == None: print "TAPE_", tape = getInput("integer") while track == None: print "Track:", track = getInput("integer") while start == None: print "Start Time:", start = getInput("time") while end == None: print "End Time:", end = getInput("time2") comment = raw_input("Comment:") ###Fill it into the Tape object, into a list obj = Track(item, tape, track, start, end, comment) Tapes.append(obj) print ""*2 print "Results:" for i in range(len(Tapes)): obj = Tapes[i] start = time2sec(obj.startTime) end = time2sec(obj.endTime) if obj.endTime == "": r = i while end == "": r = r+1 obj2 = Tapes[r] end = time2sec(obj2.startTime) lenght = sec2time(end-start) print "Item_", obj.itemNumb print "TAPE_", obj.tapeNumb print "Track:", obj.trackNumb print "Start: 00:00" print "End:", lenght print "Comment:", obj.comment From ps_python at yahoo.com Tue Jan 11 21:09:42 2005 From: ps_python at yahoo.com (kumar s) Date: Tue Jan 11 21:09:45 2005 Subject: [Tutor] please help: conditional statement and printing element In-Reply-To: <20050111195554.87122.qmail@web53707.mail.yahoo.com> Message-ID: <20050111200942.91313.qmail@web53707.mail.yahoo.com> Dear group: I think I have got the answer :-)( script: >>> for i in range(len(psl)): col = split(psl[i],'\t') col1 = col[0] col1 = int(col1) col17 = int(col[17]) if col1 == 25 and col17 == 1: print col[0]+ '\t'+col[1]+'\t'+col[9]+'\t'+col[10]+'\t'+col[11] 25 0 580:683 25 0 25 0 581:687 25 0 25 0 434:9 25 0 25 0 37:141 25 0 25 0 219:629 25 0 25 0 462:87 25 0 25 0 483:409 25 0 25 0 354:323 25 0 25 0 624:69 25 0 25 0 350:239 25 0 Is this a correct approach? Thanks K. --- kumar s wrote: > Dear group, > For some reason my brain cannot think of any other > option than what I have in my script. Could any one > please help me in suggesting. > > What I have : (File name : psl) > 22 2 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 22 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 24 1 457:411 25 0 > 22 0 457:411 25 0 > 21 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > > > What to do: > I want to print values that are 25 in column 1 and > not > the other values such as 24,22,21 etc. > > > My script: > >>> for i in range(len(psl)): > col = split(psl[i],'\t') > col1 = col[0] > if col1 == 25: > print col[0]+'\t'+col[1]+'\t'+col[17] > > > >>> > > Result: I get nothing. Am I doing something very > wrong. Why isnt if col1 == 25: functional. > > My idea is to check if col[0] == 25: then print > columns 1,18 etc. > > Can you please help me. > > Thanks > K > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam > protection around > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From keridee at jayco.net Tue Jan 11 21:12:33 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 21:12:46 2005 Subject: [Tutor] please help: conditional statement and printing element References: <20050111195554.87122.qmail@web53707.mail.yahoo.com> Message-ID: <001001c4f819$f025b390$ef5428cf@JSLAPTOP> coll is a string, 25 is an integer. However, I would do it this way. file1 = open('psl.txt','r') for line in file1: if line.startwith('25'): line = line.split('\t') print "\t".join(line[0],line[1],line[17]) file1.close() This has the added advantage that if you have a big file, it will only take the time to split it if it meets the qualifications. i.e. if it starts with '25' The only way that the advantage would not work is if the string method startswith takes more time to run than splitting and searching the first index for a value. But I doubt that very much. HTH, Jacob Schmidt > Dear group, > For some reason my brain cannot think of any other > option than what I have in my script. Could any one > please help me in suggesting. > > What I have : (File name : psl) > 22 2 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 22 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 24 1 457:411 25 0 > 22 0 457:411 25 0 > 21 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > > > What to do: > I want to print values that are 25 in column 1 and not > the other values such as 24,22,21 etc. > > > My script: > >>> for i in range(len(psl)): > col = split(psl[i],'\t') > col1 = col[0] > if col1 == 25: > print col[0]+'\t'+col[1]+'\t'+col[17] > > > >>> > > Result: I get nothing. Am I doing something very > wrong. Why isnt if col1 == 25: functional. > > My idea is to check if col[0] == 25: then print > columns 1,18 etc. > > Can you please help me. > > Thanks > K > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Tue Jan 11 21:13:29 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 11 21:13:32 2005 Subject: [Tutor] please help: conditional statement and printing element In-Reply-To: <20050111195554.87122.qmail@web53707.mail.yahoo.com> References: <20050111195554.87122.qmail@web53707.mail.yahoo.com> Message-ID: <41E43369.8000901@tds.net> kumar s wrote: > Dear group, > For some reason my brain cannot think of any other > option than what I have in my script. Could any one > please help me in suggesting. > > What I have : (File name : psl) > 22 2 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 22 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > 24 1 457:411 25 0 > 22 0 457:411 25 0 > 21 0 457:411 25 0 > 25 0 457:411 25 0 > 25 0 457:411 25 0 > > > What to do: > I want to print values that are 25 in column 1 and not > the other values such as 24,22,21 etc. > > > My script: > >>>>for i in range(len(psl)): > > col = split(psl[i],'\t') > col1 = col[0] col1 is a text string, not a number, e.g. '25', not 25. The simplest thing is just to compare it to '25': > if col1 == 25: if col1 == '25': Kent > print col[0]+'\t'+col[1]+'\t'+col[17] > > > > > Result: I get nothing. Am I doing something very > wrong. Why isnt if col1 == 25: functional. > > My idea is to check if col[0] == 25: then print > columns 1,18 etc. > > Can you please help me. > > Thanks > K > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kp8 at mac.com Tue Jan 11 21:14:03 2005 From: kp8 at mac.com (kevin parks) Date: Tue Jan 11 21:14:11 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: Message-ID: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> Thanks for this Everyone! Trying to work with all the stuff folks are giving me on this i a have come across a problem... down the line i notice that some of the times will also have an hour as well as in H:M:S (e.g. 1:22:40) so in some cases i would need to convert H:M:S to sec and some just M:S or should there just be a fun that passes all the times and converts them to H:M:S first and just appends a 00: ((e.g. 00:02:07) if there is no hour value? cheers, kevin From jeff at ccvcorp.com Tue Jan 11 21:20:50 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Tue Jan 11 21:15:36 2005 Subject: [Tutor] My best GUI app so far. In-Reply-To: <000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> References: <000701c4f782$b1124430$425428cf@JSLAPTOP> <41E3434E.5030803@tds.net> <000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> Message-ID: <41E43522.1070806@ccvcorp.com> Jacob S. wrote: > Great! I took the improvements you gave me an added support for keys (So you > can type in 1.25+2= instead of having to type the buttons.) As always, I > encourage improvements to my code. Maybe that will be my disclaimer... I > have always liked and wanted to adopt Liam's. Here's a few thoughts.... :) > def equal(self,*args): > if self.action: > self.newnum = self.distext.get() > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' > self.shouldblank = True Instead of using text representations of the operators, and eval()ing a string, why not use the operator module? Your operator keys can set self.action to be operator.add, operator.subtract, etc; then your equal() function becomes def equal(self, *args): if self.action: self.newnum = self.distext.get() self.newnum= str(self.action(float(self.oldnum), \ float(self.newnum))) self.distext.set(self.newnum) self.oldnum = '0' self.action = '' # I'd actually prefer None here... self.shouldblank = True > > def add(self,*args): > self.handleOperator('+') becomes def add(self, *args): self.handleOperator(operator.add) The handleOperator() function can stay the same. > def clear(self): > self.action = '' > self.oldnum = '0' > self.distext.set('0') > self.shouldblank = True As I mentioned in a comment above, I'd prefer to use None for self.action when it's unset. There's no real practical difference, but conceptually it's more accurate to use a null-object than to use an empty string. Minor style point, I know, but you *did* ask for advice. ;) > def memminus(self): > self.memory = str(eval(self.memory+"-"+self.distext.get())) > self.shouldblank = True > > def memplus(self): > self.memory = str(eval(self.memory+"+"+self.distext.get())) > self.shouldblank = True Why use eval() here? You could just as easily do these as def memminus(self): self.memory = str(float(self.memory) - \ float(self.distext.get())) self.shouldblank = True I try to avoid using eval() wherever possible, which is almost everywhere. ;) There's a huge security hole in using eval() on arbitrary strings, and it's not the greatest performance either. (Each invocation of eval() will generate bytecode and create a code object that does essentially the same thing as my explicit conversion code does, so by doing it manually you save a parsing/compiling step.) You're not eval()ing arbitrary strings here, at least, but it's still good practice to only use eval() when absolutely necessary. > def __init__(self, master=None): > Frame.__init__(self,master) > self.master.title("Calculator by Jacob, Inc.") > self.pack(expand=True) > m = lambda x: self.adddigit(x) > self.bl = [lambda *x: self.adddigit('0',x), > lambda *x: self.adddigit('1',x), > lambda *x: self.adddigit('2',x), > lambda *x: self.adddigit('3',x), > lambda *x: self.adddigit('4',x), > lambda *x: self.adddigit('5',x), > lambda *x: self.adddigit('6',x), > lambda *x: self.adddigit('7',x), > lambda *x: self.adddigit('8',x), > lambda *x: self.adddigit('9',x)] > for y in range(10): > self.bind_all(str(y),self.bl[y]) > self.bind_all("+",lambda x: self.add(x)) > self.bind_all("-",lambda x: self.subtract(x)) > self.bind_all("*",lambda x: self.multiply(x)) > self.bind_all("/",lambda x: self.divide(x)) > self.bind_all("=",lambda x: self.equal(x)) > self.bind_all(".",lambda x: self.adddigitdot(x)) There's absolutely no point to doing lambda x: somefunc(x) -- all you're doing is adding an extra layer of function call. You can replace all of those with something like self.bind_all('+', self.add) And actually, because I'm not fond of lambda to begin with, I'd redefine your adddigit() method: def make_adddigit_callback(self, digit): def adddigit(*args): self.ctb() self.distext.set(self.distext.get()+digit) return adddigit (You may not need the *args above, if the button callback is expected to be zero parameters -- I don't use Tkinter, so I'm not sure offhand.) Then your button bindings can simply be self.bl = [ self.make_adddigit_callback('0'), self.make_adddigit_callback('1'), self.make_adddigit_callback('2'), ... ] Or even -- self.bl = [self.make_adddigit_callback(digit) \ for digit in '0123456789'] Remember, there's nothing particularly special about lambdas -- you can create and pass around regular named functions, too. Each time that make_adddigit_callback() is called, it creates and returns a new function object which captures the current value of 'digit', in exactly the same way that lambda does. A function object (whether named with def, or lambda) like this, which captures a variable's current state, is called a closure. Closures are indispensible for GUI callbacks like this, and many people automatically turn to lambda when they want a closure. For me, though, having a proper def statement somewhere feels clearer. (The merits of lambda vs. def'd functions are a frequent subject of heated debate on comp.lang.python, so if you prefer to stick with the lambdas in this case, I'm sure you'd be able to find plenty of people to support you... ;) ) Jeff Shannon Technician/Programmer Credit International From davholla2002 at yahoo.co.uk Tue Jan 11 21:17:24 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Tue Jan 11 21:17:27 2005 Subject: [Tutor] CGI problem Message-ID: <20050111201724.76644.qmail@web25403.mail.ukl.yahoo.com> Thanks for the help it solved the problem. Message: 8 Date: Mon, 10 Jan 2005 16:05:59 -0800 From: "Patric Michael" Subject: Re: [Tutor] CGI problem To: tutor@python.org Message-ID: <41E2A7E7.28591.222A37FA@localhost> Content-Type: text/plain; charset=US-ASCII Hi David... You need to explicitly name your form element to "inputkey" to make your current code work correctly. ( Based on what you have shown below.) Or, to make the code correct, change "inputkey" to "language". Remember that the name in each form element becomes the key in the key/value pairs sent via POST or GET. Patric ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From lists at procario.org Tue Jan 11 21:25:37 2005 From: lists at procario.org (Mike Procario) Date: Tue Jan 11 21:25:42 2005 Subject: [Tutor] atof error Message-ID: <20050111202537.GA4929@bquark.procario> I got an unexpected error today using string.atof. ValueError: invalid literal for float(): -17,019.797 To me -17,019.797 looks like a perfectly good floating point number. I cannot find documentation on what the allowed range is. -- Mike Procario "Another casualty of applied metaphysics" -Calvin and Hobbes From maxnoel_fr at yahoo.fr Tue Jan 11 21:31:04 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 11 21:31:17 2005 Subject: [Tutor] atof error In-Reply-To: <20050111202537.GA4929@bquark.procario> References: <20050111202537.GA4929@bquark.procario> Message-ID: On Jan 11, 2005, at 20:25, Mike Procario wrote: > > I got an unexpected error today using string.atof. > > ValueError: invalid literal for float(): -17,019.797 > > To me -17,019.797 looks like a perfectly good floating point > number. I cannot find documentation on what the allowed range is. The problem is not the range. It's the comma that's right in the middle of the number. Yuck. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Tue Jan 11 21:32:09 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 21:32:13 2005 Subject: [Tutor] please help: conditional statement and printing element References: <20050111200942.91313.qmail@web53707.mail.yahoo.com> Message-ID: <001901c4f81c$a8cd0e00$ef5428cf@JSLAPTOP> I think I speak for a few of us in the fact that we don't know if that's close to the correct approach due to the fact that you haven't given us a full line to look at. Is col[17] the last column? If so, take the code I just sent and change the line to: if line.startwith('25\t') and line.endswith('\t1'): I add the tabs in to make sure that 25 and 1 is the only thing in the column. Oh, and add col[9], col[10], etc. to the "\t".join() call. Ooops, that should also have brackets... Here it is in full. file1 = open('psl','r') for line in file1: if line.startswith('25') and line.endswith('1'): line = line.split("\t") print "\t".join([col[0],col[1],col[9],col[10]]) file1.close() An alternative that seperates the column indices and the line qualifiers from the code so they can easily be edited. Ahh, lets change line to row. ##Beginning of code## columns_to_print = [0,1,9,10,17] beginning = '25' ending = '1' file1 = open('psl','r') for row in file1: if row.startswith(beginning) and row.endswith(ending): row = row.split("\t") printlist = [row[x] for x in columns_to_print] print "\t".join(printlist) file1.close() ## Ending of code ## HTH, Jacob Schmidt From alan.gauld at freenet.co.uk Tue Jan 11 21:39:57 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 11 21:41:16 2005 Subject: [Tutor] please help: conditional statement and printing element References: <20050111200942.91313.qmail@web53707.mail.yahoo.com> Message-ID: <016a01c4f81d$b7399200$16c68651@xp> > script: > >>> for i in range(len(psl)): for line in psl: if line.split()[0].strip() == '25': print line Does that work? Alan G. From keridee at jayco.net Tue Jan 11 21:48:54 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 21:49:01 2005 Subject: [Tutor] My best GUI app so far. References: <000701c4f782$b1124430$425428cf@JSLAPTOP> <41E3434E.5030803@tds.net><000901c4f79f$42ba1a90$d25428cf@JSLAPTOP> <41E43522.1070806@ccvcorp.com> Message-ID: <001e01c4f81e$fa044e30$ef5428cf@JSLAPTOP> > > Great! I took the improvements you gave me an added support for keys (So you > > can type in 1.25+2= instead of having to type the buttons.) As always, I > > encourage improvements to my code. Maybe that will be my disclaimer... I > > have always liked and wanted to adopt Liam's. > > Here's a few thoughts.... :) > > > > > def equal(self,*args): > > if self.action: > > self.newnum = self.distext.get() > > self.newnum = str(eval(self.oldnum+self.action+self.newnum)) > > self.distext.set(self.newnum) > > self.oldnum = '0' > > self.action = '' > > self.shouldblank = True > > > Instead of using text representations of the operators, and eval()ing > a string, why not use the operator module? > > Your operator keys can set self.action to be operator.add, > operator.subtract, etc; then your equal() function becomes > > def equal(self, *args): > if self.action: > self.newnum = self.distext.get() > self.newnum= str(self.action(float(self.oldnum), \ > float(self.newnum))) > self.distext.set(self.newnum) > self.oldnum = '0' > self.action = '' # I'd actually prefer None here... > self.shouldblank = True > > > > > def add(self,*args): > > self.handleOperator('+') > > becomes > > def add(self, *args): > self.handleOperator(operator.add) > > The handleOperator() function can stay the same. > > > > def clear(self): > > self.action = '' > > self.oldnum = '0' > > self.distext.set('0') > > self.shouldblank = True > > As I mentioned in a comment above, I'd prefer to use None for > self.action when it's unset. There's no real practical difference, > but conceptually it's more accurate to use a null-object than to use > an empty string. Minor style point, I know, but you *did* ask for > advice. ;) > > > def memminus(self): > > self.memory = str(eval(self.memory+"-"+self.distext.get())) > > self.shouldblank = True > > > > def memplus(self): > > self.memory = str(eval(self.memory+"+"+self.distext.get())) > > self.shouldblank = True > > Why use eval() here? You could just as easily do these as > > def memminus(self): > self.memory = str(float(self.memory) - \ > float(self.distext.get())) > self.shouldblank = True > > I try to avoid using eval() wherever possible, which is almost > everywhere. ;) There's a huge security hole in using eval() on > arbitrary strings, and it's not the greatest performance either. > (Each invocation of eval() will generate bytecode and create a code > object that does essentially the same thing as my explicit conversion > code does, so by doing it manually you save a parsing/compiling step.) > You're not eval()ing arbitrary strings here, at least, but it's > still good practice to only use eval() when absolutely necessary. > > > > def __init__(self, master=None): > > Frame.__init__(self,master) > > self.master.title("Calculator by Jacob, Inc.") > > self.pack(expand=True) > > m = lambda x: self.adddigit(x) > > self.bl = [lambda *x: self.adddigit('0',x), > > lambda *x: self.adddigit('1',x), > > lambda *x: self.adddigit('2',x), > > lambda *x: self.adddigit('3',x), > > lambda *x: self.adddigit('4',x), > > lambda *x: self.adddigit('5',x), > > lambda *x: self.adddigit('6',x), > > lambda *x: self.adddigit('7',x), > > lambda *x: self.adddigit('8',x), > > lambda *x: self.adddigit('9',x)] > > for y in range(10): > > self.bind_all(str(y),self.bl[y]) > > self.bind_all("+",lambda x: self.add(x)) > > self.bind_all("-",lambda x: self.subtract(x)) > > self.bind_all("*",lambda x: self.multiply(x)) > > self.bind_all("/",lambda x: self.divide(x)) > > self.bind_all("=",lambda x: self.equal(x)) > > self.bind_all(".",lambda x: self.adddigitdot(x)) > > There's absolutely no point to doing lambda x: somefunc(x) -- all > you're doing is adding an extra layer of function call. You can > replace all of those with something like > > self.bind_all('+', self.add) > > And actually, because I'm not fond of lambda to begin with, I'd > redefine your adddigit() method: > > def make_adddigit_callback(self, digit): > def adddigit(*args): > self.ctb() > self.distext.set(self.distext.get()+digit) > return adddigit > > (You may not need the *args above, if the button callback is expected > to be zero parameters -- I don't use Tkinter, so I'm not sure > offhand.) Then your button bindings can simply be > > self.bl = [ self.make_adddigit_callback('0'), > self.make_adddigit_callback('1'), > self.make_adddigit_callback('2'), > ... ] > > Or even -- > > self.bl = [self.make_adddigit_callback(digit) \ > for digit in '0123456789'] > > Remember, there's nothing particularly special about lambdas -- you > can create and pass around regular named functions, too. Each time > that make_adddigit_callback() is called, it creates and returns a new > function object which captures the current value of 'digit', in > exactly the same way that lambda does. A function object (whether > named with def, or lambda) like this, which captures a variable's > current state, is called a closure. Closures are indispensible for > GUI callbacks like this, and many people automatically turn to lambda > when they want a closure. For me, though, having a proper def > statement somewhere feels clearer. (The merits of lambda vs. def'd > functions are a frequent subject of heated debate on comp.lang.python, > so if you prefer to stick with the lambdas in this case, I'm sure > you'd be able to find plenty of people to support you... ;) ) > > Jeff Shannon > Technician/Programmer > Credit International I know why people make money programming... ;-) I'm not adverse to any conventional way of getting the project to work. There are only a few exceptions like the proposal in python3 to remove raw_input(). You would have to import sys to get sys.stdin.readlines(). Just odd, quirky, whys. I digress. I think the make a new function idea will work. The only reason I used lambdas is because Kent suggested them when I pointed out that you can't call the functions when defining them in the buttons, so you can't pass parameters. Or so I thought. Yes, I think your idea will work. What am I talking about? Of course your idea will work. ;-) Thanks, Jacob Schmidt From kp8 at mac.com Tue Jan 11 21:56:48 2005 From: kp8 at mac.com (kevin parks) Date: Tue Jan 11 21:57:00 2005 Subject: [Tutor] Time script help sought! In-Reply-To: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> Message-ID: <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> I also notice that there is the is the 'datetime' module, which is new to version 2.3, which i now have access to. My feeling is that this will do much of what i want, but i can't get my head round the standard library reference stuff http://www.python.org/doc/lib/module-datetime.html I don't have any texts with me either and it probably is too new to be in the Python Standard Library book by Fredrik Lundh or the Python Essential Reference by David Beazley -kevin- From keridee at jayco.net Tue Jan 11 22:00:00 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 22:00:32 2005 Subject: [Tutor] Time script help sought! References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> Message-ID: <003501c4f820$9cbed310$ef5428cf@JSLAPTOP> Would this do the trick? stringtime = '12:34' ## This is the column with the time 12:34 is an example time = stringtime.split(":") time = [int(x) for x in time] time.reverse() seconds = time[0]+time[1] try: seconds += time[2] except IndexError: pass print seconds I'm almost sure there is a better way to do this. Jacob Schmidt > Thanks for this Everyone! > > Trying to work with all the stuff folks are giving me on this i a have > come across a problem... down > the line i notice that some of the times will also have an hour as well > as in H:M:S (e.g. 1:22:40) > > so in some cases i would need to convert H:M:S to sec and some just M:S > > > or should there just be a fun that passes all the times and converts > them to H:M:S first and > just appends a 00: ((e.g. 00:02:07) if there is no hour value? > > cheers, > > kevin > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Tue Jan 11 22:03:03 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 22:03:53 2005 Subject: [Tutor] Time script help sought! References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> Message-ID: <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> I don't think so. (correct me if I'm wrong) The datetime module is for making date and time instances that you can add and subtract to get timedelta objects. Other things involved of course, but I don't think it has anything to do with parsing and pretty printing columns of times. I'm not sure, don't quote me on it. Jacob > I also notice that there is the is the 'datetime' module, which is new > to version 2.3, which i now have access to. My feeling is that this > will do much of what i want, but i can't get my head round the standard > library reference stuff > > http://www.python.org/doc/lib/module-datetime.html > > I don't have any texts with me either and it probably is too new to be > in the Python Standard Library book by Fredrik Lundh or the Python > Essential Reference by David Beazley > > > -kevin- > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From jfouhy at paradise.net.nz Tue Jan 11 22:07:08 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Tue Jan 11 22:07:11 2005 Subject: [Tutor] please help: conditional statement and printing element In-Reply-To: <20050111195554.87122.qmail@web53707.mail.yahoo.com> References: <20050111195554.87122.qmail@web53707.mail.yahoo.com> Message-ID: <1105477628.41e43ffc38230@www.paradise.net.nz> Quoting kumar s : > What to do: > I want to print values that are 25 in column 1 and not > the other values such as 24,22,21 etc. awk '$1 == 25' psl :-) -- John. From jfouhy at paradise.net.nz Tue Jan 11 22:20:38 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Tue Jan 11 22:20:43 2005 Subject: [Tutor] Time script help sought! In-Reply-To: <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: <1105478438.41e44326f11d5@www.paradise.net.nz> Quoting "Jacob S." : > I don't think so. (correct me if I'm wrong) The datetime module is for > making date and time instances that you can add and subtract to get > timedelta objects. Other things involved of course, but I don't think it > has anything to do with parsing and > pretty printing columns of times. I'm not sure, don't quote me on it. Partially correct ... Firstly, datetime objects can produce string output using the strftime function. Example: >>> import datetime >>> d = datetime.datetime(2004, 12, 31, hour=3, minute=27) >>> d datetime.datetime(2004, 12, 31, 3, 27) >>> d.strftime("%A %B %d %Y, %I:%M%p") 'Friday December 31 2004, 03:27AM' The old mx.datetime module (on which Python's datetime module is based, I presume) had a strptime() function which would basically do the reverse (you specify a format string and it would attempt to parse the date string you give it). Unfortunately, Python's datetime module doesn't have such a function. This is the best way I have found of doing it: def strptime(s, format): return datetime.datetime.fromtimestamp(time.mktime(time.strptime(s, format))) Example: >>> import time, datetime >>> def strptime(s, format): ... return datetime.datetime.fromtimestamp(time.mktime(time.strptime(s, format))) ... >>> d = strptime('2004-12-31.23:59', '%Y-%m-%d.%H:%M') >>> d.strftime("%A %B %d %Y, %I:%M%p") 'Friday December 31 2004, 11:59PM' HTH. -- John. From keridee at jayco.net Tue Jan 11 22:34:15 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 22:34:17 2005 Subject: [Tutor] Time script help sought! References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com><4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com><003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> <1105478438.41e44326f11d5@www.paradise.net.nz> Message-ID: <005b01c4f825$521c6ed0$ef5428cf@JSLAPTOP> > The old mx.datetime module (on which Python's datetime module is based, I > presume) had a strptime() function which would basically do the reverse (you > specify a format string and it would attempt to parse the date string you give > it). Unfortunately, Python's datetime module doesn't have such a function. > This is the best way I have found of doing it: Fortunately, the time module does. time.strptime HTH, Jacob From keridee at jayco.net Tue Jan 11 22:37:04 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 11 22:36:59 2005 Subject: [Tutor] Time script help sought! References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com><4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com><003a01c4f821$11eb0280$ef5428cf@JSLAPTOP><1105478438.41e44326f11d5@www.paradise.net.nz> <005b01c4f825$521c6ed0$ef5428cf@JSLAPTOP> Message-ID: <006301c4f825$b457fc40$ef5428cf@JSLAPTOP> Forgot to mention -- screwed up before. In my proposed try/except block, etc. I forgot to do unit conversion. Jacob From jfouhy at paradise.net.nz Tue Jan 11 22:49:40 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Tue Jan 11 22:49:46 2005 Subject: [Tutor] Time script help sought! In-Reply-To: <005b01c4f825$521c6ed0$ef5428cf@JSLAPTOP> References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> <1105478438.41e44326f11d5@www.paradise.net.nz> <005b01c4f825$521c6ed0$ef5428cf@JSLAPTOP> Message-ID: <1105480180.41e449f46c85a@www.paradise.net.nz> Quoting "Jacob S." : > > The old mx.datetime module (on which Python's datetime module is based, I > > presume) had a strptime() function which would basically do the reverse (you > > specify a format string and it would attempt to parse the date string you give > > it). Unfortunately, Python's datetime module doesn't have such a function. > > This is the best way I have found of doing it: > > Fortunately, the time module does. > > time.strptime Yeah, I know. I used time.strptime in the function which you didn't quote. The point is that a hypothetical datetime.datetime.strptime function would return a datetime object, whereas time.strptime returns a struct_time, which is much less useful. Hence a couple of hoops to jump through to convert a struct_time into a datetime.. -- John. From kp8 at mac.com Tue Jan 11 22:51:31 2005 From: kp8 at mac.com (kevin parks) Date: Tue Jan 11 22:51:45 2005 Subject: [Tutor] Time script help sought! In-Reply-To: <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: I am still working on it and also fixing the input data. I think for simplicity and consistency's sake i will have *all* time values input and output as hh:mm:ss maybe that would be easier for now.. so i am editing the input stuff now... so it should all look like: Item_1, DAT_1, 1, 00:00:23, 00:08:23 Item_2, DAT_1, 2, 00:08:23, 00:09:41 Item_3, DAT_1, 3, 00:09:41, 00:10:41 Item_3, DAT_1, 4, 00:10:47, 00:11:19 Item_3, DAT_1, 5, 00:11:21, 00:11:55 Item_3, DAT_1, 6, 00:11:58, 00:12:10 Item_3, DAT_1, 7, 00:12:15, 00:12:45 Item_3, DAT_1, 8, 00:12:58, 00:24:20 Item_4, DAT_1, 9, 00:24:33 Item_4, DAT_1, 10, 00:25:48 Item_4, DAT_1, 11, 00:29:48 Item_4, DAT_1, 12, 00:31:46 Item_4, DAT_1, 13, 00:34:17 Item_4, DAT_1, 14, 00:35:21 Item_4, DAT_1, 15, 00:36:06 Item_4, DAT_1, 16, 00:37:01, 00:37:38 no comments either i can copy them later... this is kind of hard... -k From alan.gauld at freenet.co.uk Tue Jan 11 23:07:56 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 11 23:07:11 2005 Subject: [Tutor] please help: conditional statement and printing element References: <20050111195554.87122.qmail@web53707.mail.yahoo.com> <1105477628.41e43ffc38230@www.paradise.net.nz> Message-ID: <019e01c4f82a$0580d2a0$16c68651@xp> Good point John, As I pointed out in another thread its best to use the right tool for the job and in this case that is [ng]awk... Alan G. (a HUGE Awk fan! :-) ----- Original Message ----- From: To: Sent: Tuesday, January 11, 2005 9:07 PM Subject: Re: [Tutor] please help: conditional statement and printing element > Quoting kumar s : > > > What to do: > > I want to print values that are 25 in column 1 and not > > the other values such as 24,22,21 etc. > > awk '$1 == 25' psl > > :-) > > -- > John. > > From cyresse at gmail.com Wed Jan 12 00:15:06 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 12 00:15:09 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <321086EC-6377-11D9-8A9A-000393CBC88E@yahoo.fr> References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> <41E32E0E.7090802@tds.net> <2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr> <321086EC-6377-11D9-8A9A-000393CBC88E@yahoo.fr> Message-ID: Out of curiousity, having poked around XML while learning about the JScript DOM, what are you using it for? AFAIK, you make up your own tags, and then parse them and display them, and anyone else could create data using your tags. Only thing I've seen that uses XML (remember I'm a n00bie in Python, Java, Jscript and HTML, so I don't see the real indepth stuff) is MSN Messenger for it's logs. And MS IE can parse that XML. I've been curious as to how it's implemented. So yeah, if you want to share your experiences. Regards, Liam Clarke On Tue, 11 Jan 2005 02:19:17 +0000, Max Noel wrote: > > dom4j? What is it? Is it part of the standard Java distribution? If > > not, where can it be found? > > Update: Okay, looks like it's time to go to bed. The link was in > bright blue and somehow I didn't see it. D'oh. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > _______________________________________________ > 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. From maxnoel_fr at yahoo.fr Wed Jan 12 00:34:30 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 12 00:34:32 2005 Subject: Fwd: [Tutor] More and more OT - Python/Java Message-ID: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> (yes, forgot to CC the list again -- argh!) Begin forwarded message: > From: Max Noel > Date: January 11, 2005 23:33:44 GMT > To: Liam Clarke > Subject: Re: [Tutor] More and more OT - Python/Java > > > On Jan 11, 2005, at 23:15, Liam Clarke wrote: > >> Out of curiousity, having poked around XML while learning about the >> JScript DOM, what are you using it for? >> >> AFAIK, you make up your own tags, and then parse them and display >> them, and anyone else could create data using your tags. >> >> Only thing I've seen that uses XML (remember I'm a n00bie in Python, >> Java, Jscript and HTML, so I don't see the real indepth stuff) is MSN >> Messenger for it's logs. And MS IE can parse that XML. >> >> I've been curious as to how it's implemented. >> >> So yeah, if you want to share your experiences. >> >> Regards, >> >> Liam Clarke > > Well, I plan to use it as a data storage format for a university > project (crowd simulation in a shopping center -- coded in Java). I > hate binary data formats, and XML is a good unified way to store data > as ASCII text, so I figured I'd use it. > Also, XML files can be parsed without too much work, so I can write > scripts that will manipulate my data files, in any language ("any" > meaning "Python", there). > > As a bonus, I've decided to have a look at XSL, which allows me to > format a XML file for display in a web browser. It entirely changed my > perception of web programming. > I intend to program an on-line browser-based game with some friends > of mine later in the year (in Python of course -- I converted them), > and now that I've seen what XML and XSL can do, we're so going to use > them for data output: the data is in dynamically-generated XML, which > links to a (static) XSL stylesheet to tell the browser how to render > that data. > Doing things that way has many advantages: > 1) Data is separate from formatting. That's always a Good Thing(TM). > If I someday decide that I don't like the way the site looks, I > theoretically only need to recreate a stylesheet, without touching > anything else. (boom! Instant skins!) > 2) Most of the "HTML rendering" is going to be done by the user's > browser. This, and the way XSL stylesheets are constructed will > prevent many bad HTML issues. > 3) For the same reason, it will save bandwidth. The XML data will > probably take less space than the fully-formatted stuff I'd have to > spit out with "regular" HTML, and the XSL stylesheet can probably be > cached by the user's browser. > 4) In the same line of reasoning, it'll also save CPU time: XML data, > being smaller, is generated faster than the equivalent HTML. Granted, > the stylesheet is another server request, but it's static, so it puts > virtually no load on a server. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge > a perfect, immortal machine?" > > -- maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From cyresse at gmail.com Wed Jan 12 00:37:52 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 12 00:37:55 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: It's kind of easy - so that stuff you've got there is in a file called dude.inp, for argument's sake - import datetime filename=file("dude.inp","r") lineList=filename.readlines() filename.close() for line in lineList: #This is the hard bit, formatting your in dates splitLine=line.split(", ") for (item, dat, num, startTime, finishTime) in splitLine: sepStart=startTime.split(":") startHours=sepStart[0] startMin=sepStart[1] startSec=sepStart[2] finishStart= finishTime.split(":") finishHours=sepStart[0] finishMin=sepStart[1] finishSec=sepStart[2] #Now for the easy bit, manipulating the time startObj = datetime.datetime(01, 1, 1, startHours, startMin, startSec) finishObj = datetime.dateime(01, 1, 1, startHours, startMin, startSec) #If you're going to simply have all times start from 00:00, you only want the length #of the time period right? So.... lengthDelta = finishObj - startObj #Python does the time calcs for ya! lengthObj = datetime.datetime(lengthDelta) OK - so, once you've formatted the input to get the hours, mins, and secs, you create a datetime object for each time which is set for that 01/01/1901, at that particular time. Notice the 01, 1, 1, - you can't use 01 01 for the month and day \ a datetime object goes like this - datetime.datetime(year, month, day, hours, minutes, secs) So, you've got the two datetime objects, Python handles the maths, and gives a timedelta object, which you use to create a new datetime object (as a timedelta object can't use strftime) which you can then use to format your time with strftime! Real easy... if there's anything I've messed up, let me know, and I'll clarify it for you ( I coded something similar at home). Or if anything needs expansion upon... Regards, Liam Clarke On Tue, 11 Jan 2005 16:51:31 -0500, kevin parks wrote: > I am still working on it and also fixing the input data. I think for > simplicity and consistency's sake i will have *all* time values input > and output as hh:mm:ss maybe that would be easier for now.. so i am > editing the input stuff now... > > so it should all look like: > > Item_1, DAT_1, 1, 00:00:23, 00:08:23 > > Item_2, DAT_1, 2, 00:08:23, 00:09:41 > > Item_3, DAT_1, 3, 00:09:41, 00:10:41 > Item_3, DAT_1, 4, 00:10:47, 00:11:19 > Item_3, DAT_1, 5, 00:11:21, 00:11:55 > Item_3, DAT_1, 6, 00:11:58, 00:12:10 > Item_3, DAT_1, 7, 00:12:15, 00:12:45 > Item_3, DAT_1, 8, 00:12:58, 00:24:20 > > Item_4, DAT_1, 9, 00:24:33 > Item_4, DAT_1, 10, 00:25:48 > Item_4, DAT_1, 11, 00:29:48 > Item_4, DAT_1, 12, 00:31:46 > Item_4, DAT_1, 13, 00:34:17 > Item_4, DAT_1, 14, 00:35:21 > Item_4, DAT_1, 15, 00:36:06 > Item_4, DAT_1, 16, 00:37:01, 00:37:38 > > no comments either i can copy them later... > > this is kind of hard... > > -k > > _______________________________________________ > 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. From jonathan.hayward at pobox.com Wed Jan 12 00:07:41 2005 From: jonathan.hayward at pobox.com (Jonathan Hayward) Date: Wed Jan 12 01:09:45 2005 Subject: [Tutor] Re: My best GUI app so far. Message-ID: <41E45C3D.3020102@pobox.com> Jacob S wrote. > I try to avoid using eval() wherever possible, which is almost > everywhere. ;) There's a huge security hole in using eval() on > arbitrary strings, and it's not the greatest performance either. > (Each invocation of eval() will generate bytecode and create a code > object that does essentially the same thing as my explicit conversion > code does, so by doing it manually you save a parsing/compiling step.) > You're not eval()ing arbitrary strings here, at least, but it's > still good practice to only use eval() when absolutely necessary. So, then, are you of the camp that believes that "a necessary eval() is still an eval()"? Quite a lot of good philosophers would agree with you! -- ++ Jonathan Hayward, jonathan.hayward@pobox.com ** To see an award-winning website with stories, essays, artwork, ** games, and a four-dimensional maze, why not visit my home page? ** All of this is waiting for you at http://JonathansCorner.com From guillermo.fernandez.castellanos at gmail.com Wed Jan 12 01:49:47 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Wed Jan 12 01:49:54 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: <7d7029e7050111164947f4908f@mail.gmail.com> Hi, Just out of curiosity, > > Well, I plan to use it as a data storage format for a university > > project (crowd simulation in a shopping center -- coded in Java). I > > hate binary data formats, and XML is a good unified way to store data > > as ASCII text, so I figured I'd use it. > > Also, XML files can be parsed without too much work, so I can write > > scripts that will manipulate my data files, in any language ("any" > > meaning "Python", there). Does that mean that you use XML as a database to store data and make queries, or that you store your information as XML in a database (as MySQL)? I ask that because I'm writting a little program that will make queries over a 1500 entries database, with very simple queries. I need an answer in 1 or 2 seconds. I'm using SQLite now, but i wanted something that depends as little as possible of external dependencies (as a database). G From cyresse at gmail.com Wed Jan 12 02:40:02 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 12 02:40:11 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: So, you've got the XML like - You are standing in front of a stump. A path leads north. N and you have a XSL that works like a CSS? descript {font:arial, align:center} exits style:bolder Is that a good paraphrasing? How browser dependent would that be? Do most browsers support XML & XSL? PS What's SAX DOM? I know what a DOM is, but what's the SAX? I saw it in my Python docs when I was poking XMLParser. If/when I work with XML, would you recommend Python's standard modules for it? Regards, Liam Clarke On Tue, 11 Jan 2005 23:34:30 +0000, Max Noel wrote: > (yes, forgot to CC the list again -- argh!) > > Begin forwarded message: > > > From: Max Noel > > Date: January 11, 2005 23:33:44 GMT > > To: Liam Clarke > > Subject: Re: [Tutor] More and more OT - Python/Java > > > > > > On Jan 11, 2005, at 23:15, Liam Clarke wrote: > > > >> Out of curiousity, having poked around XML while learning about the > >> JScript DOM, what are you using it for? > >> > >> AFAIK, you make up your own tags, and then parse them and display > >> them, and anyone else could create data using your tags. > >> > >> Only thing I've seen that uses XML (remember I'm a n00bie in Python, > >> Java, Jscript and HTML, so I don't see the real indepth stuff) is MSN > >> Messenger for it's logs. And MS IE can parse that XML. > >> > >> I've been curious as to how it's implemented. > >> > >> So yeah, if you want to share your experiences. > >> > >> Regards, > >> > >> Liam Clarke > > > > Well, I plan to use it as a data storage format for a university > > project (crowd simulation in a shopping center -- coded in Java). I > > hate binary data formats, and XML is a good unified way to store data > > as ASCII text, so I figured I'd use it. > > Also, XML files can be parsed without too much work, so I can write > > scripts that will manipulate my data files, in any language ("any" > > meaning "Python", there). > > > > As a bonus, I've decided to have a look at XSL, which allows me to > > format a XML file for display in a web browser. It entirely changed my > > perception of web programming. > > I intend to program an on-line browser-based game with some friends > > of mine later in the year (in Python of course -- I converted them), > > and now that I've seen what XML and XSL can do, we're so going to use > > them for data output: the data is in dynamically-generated XML, which > > links to a (static) XSL stylesheet to tell the browser how to render > > that data. > > Doing things that way has many advantages: > > 1) Data is separate from formatting. That's always a Good Thing(TM). > > If I someday decide that I don't like the way the site looks, I > > theoretically only need to recreate a stylesheet, without touching > > anything else. (boom! Instant skins!) > > 2) Most of the "HTML rendering" is going to be done by the user's > > browser. This, and the way XSL stylesheets are constructed will > > prevent many bad HTML issues. > > 3) For the same reason, it will save bandwidth. The XML data will > > probably take less space than the fully-formatted stuff I'd have to > > spit out with "regular" HTML, and the XSL stylesheet can probably be > > cached by the user's browser. > > 4) In the same line of reasoning, it'll also save CPU time: XML data, > > being smaller, is generated faster than the equivalent HTML. Granted, > > the stylesheet is another server request, but it's static, so it puts > > virtually no load on a server. > > > > -- Max > > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > > "Look at you hacker... A pathetic creature of meat and bone, panting > > and sweating as you run through my corridors... How can you challenge > > a perfect, immortal machine?" > > > > > -- > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > _______________________________________________ > 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. From maxnoel_fr at yahoo.fr Wed Jan 12 03:01:28 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 12 03:01:33 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <7d7029e7050111164947f4908f@mail.gmail.com> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> Message-ID: On Jan 12, 2005, at 00:49, Guillermo Fernandez Castellanos wrote: > Does that mean that you use XML as a database to store data and make > queries, or that you store your information as XML in a database (as > MySQL)? Nope, nothing that fancy, I'm afraid. Just your run-off-the-mill "load from XML/save to XML" file operations. > I ask that because I'm writting a little program that will make > queries over a 1500 entries database, with very simple queries. I need > an answer in 1 or 2 seconds. I'm using SQLite now, but i wanted > something that depends as little as possible of external dependencies > (as a database). You should stick to SQLite. After all, it was designed exactly for what you're doing. Well, AFAIK at least. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Wed Jan 12 03:04:57 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 12 03:05:02 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: <5B9A94EA-643E-11D9-93C5-000393CBC88E@yahoo.fr> On Jan 12, 2005, at 01:40, Liam Clarke wrote: > So, you've got the XML like - > > > You are standing in front of a stump. A path leads north. > > N > > and you have a XSL that works like a CSS? > > descript {font:arial, align:center} > exits style:bolder > > Is that a good paraphrasing? How browser dependent would that be? Do > most browsers support XML & XSL? Yup, that'd be the idea. IIRC most browsers support XML and XSL. (not sure, I'll have to check) > PS > > What's SAX DOM? I know what a DOM is, but what's the SAX? I saw it in > my Python docs when I was poking XMLParser. If/when I work with XML, > would you recommend Python's standard modules for it? SAX is just another way of parsing XML. It's very sequential in nature, so it's not good if you need to re-access a previous node from the document, but since it doesn't load the entire document in memory (doesn't build a tree out of it), it uses less memory than DOM, and scales much better (obviously). I haven't tried Python's XML parsers yet, but I understand Python supports both SAX and DOM, so it should be okay... -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From david at graniteweb.com Wed Jan 12 04:03:21 2005 From: david at graniteweb.com (David Rock) Date: Wed Jan 12 04:03:28 2005 Subject: [Tutor] Python with MySQL ? In-Reply-To: References: Message-ID: <20050112030321.GA22999@wdfs.attbi.com> * Danny Yoo [2005-01-11 10:55]: > > On Tue, 11 Jan 2005, Mark Kels wrote: > > > How can I send SQL querys to a MySQL database with a python-CGI program ? > > Hi Mark, > > You'll want to grab a "Python DB API" module for MySQL. The best one I've You might want to check out sqlobject, too http://sqlobject.org/ It gives you a relatively simple way to "objectify" SQL statements. -- David Rock david@graniteweb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050111/0a263be6/attachment.pgp From billk at fastmail.fm Wed Jan 12 05:00:45 2005 From: billk at fastmail.fm (Bill Kranec) Date: Wed Jan 12 05:00:42 2005 Subject: [Tutor] flattening a list Message-ID: <41E4A0ED.9070502@fastmail.fm> Hello, I have a list of lists, for example [ [1,2] , [3,4] ], and I would like to pass all the elements of that list as arguments to a function (for example the intersection of all list elements). Is there a command in regular Python to do this? I would like to avoid the hassle and speed hit of a loop to extract all the list elements. In the past, I believe I have used something like flatten(list), which was part of Numarray (I think). Am I missing an obvious or clever solution in regular Python? Thanks for your help! Bill From jfouhy at paradise.net.nz Wed Jan 12 05:24:08 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Wed Jan 12 05:24:12 2005 Subject: [Tutor] flattening a list In-Reply-To: <41E4A0ED.9070502@fastmail.fm> References: <41E4A0ED.9070502@fastmail.fm> Message-ID: <1105503848.41e4a6682397a@www.paradise.net.nz> Quoting Bill Kranec : > I have a list of lists, for example [ [1,2] , [3,4] ], and I would like > to pass all the elements of that list as arguments to a function (for > example the intersection of all list elements). Is there a command in > regular Python to do this? I would like to avoid the hassle and speed > hit of a loop to extract all the list elements. I don't think so... There is a recipe on activestate for a flatten function. Or you could use a list comprehension: >>> arr = zip(range(10), range(10, 20)) >>> arr [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)] >>> [x for y in arr for x in y] [0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19] -- John. From gopinathv at hcltech.com Wed Jan 12 06:16:57 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Wed Jan 12 06:33:24 2005 Subject: [Tutor] FW: hi Message-ID: > ----- > i'm usinf active python 2.4 running in windows nt > > -----Original Message----- > From: Gopinath V, ASDC Chennai > Sent: Wednesday, January 12, 2005 10:46 AM > To: 'tutor@python.org' > Subject: hi > > Hi > I'm a Novice in python.can u tell me if there is a frontend available > .if yes where can i download it from > > > > > > With Regards, > Gopinath V > HCL Technologies Ltd., ASDC, > > > Mail : gopinathv@hcltech.com > > > > Disclaimer: > This message and any attachment(s) contained here are information that is > confidential, proprietary to HCL Technologies and its customers. Contents > may be privileged or otherwise protected by law. The information is solely > intended for the individual or the entity it is addressed to. If you are > not the intended recipient of this message, you are not authorized to > read, forward, print, retain, copy or disseminate this message or any part > of it. If you have received this e-mail in error, please notify the sender > immediately by return e-mail and delete it from your computer. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/fa49b291/attachment.html From gopinathv at hcltech.com Wed Jan 12 06:16:03 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Wed Jan 12 06:33:28 2005 Subject: [Tutor] hi Message-ID: > Hi > I'm a Novice in python.can u tell me if there is a frontend available > .if yes where can i download it from > > > > > > With Regards, > Gopinath V > HCL Technologies Ltd., ASDC, > > > Mail : gopinathv@hcltech.com > > > > Disclaimer: > This message and any attachment(s) contained here are information that is > confidential, proprietary to HCL Technologies and its customers. Contents > may be privileged or otherwise protected by law. The information is solely > intended for the individual or the entity it is addressed to. If you are > not the intended recipient of this message, you are not authorized to > read, forward, print, retain, copy or disseminate this message or any part > of it. If you have received this e-mail in error, please notify the sender > immediately by return e-mail and delete it from your computer. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/a9a15f57/attachment.htm From marilyn at deliberate.com Wed Jan 12 06:49:43 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Wed Jan 12 07:17:54 2005 Subject: [Tutor] flattening a list In-Reply-To: <1105503848.41e4a6682397a@www.paradise.net.nz> Message-ID: On Wed, 12 Jan 2005 jfouhy@paradise.net.nz wrote: > Quoting Bill Kranec : > > > I have a list of lists, for example [ [1,2] , [3,4] ], and I would like > > to pass all the elements of that list as arguments to a function (for > > example the intersection of all list elements). Is there a command in > > regular Python to do this? I would like to avoid the hassle and speed > > hit of a loop to extract all the list elements. > > I don't think so... > > There is a recipe on activestate for a flatten function. > > Or you could use a list comprehension: > > >>> arr = zip(range(10), range(10, 20)) > >>> arr > [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, > 18), (9, 19)] > >>> [x for y in arr for x in y] > [0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19] > Nice. And there's: >>> arr = zip(range(10), range(10, 20)) >>> arr [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)] >>> reduce(lambda x,y:x+y, arr) (0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19) >>> > -- From dyoo at hkn.eecs.berkeley.edu Wed Jan 12 07:50:13 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 12 07:50:19 2005 Subject: [Tutor] FW: hi In-Reply-To: Message-ID: On Wed, 12 Jan 2005, Gopinath V, ASDC Chennai wrote: > > I'm a Novice in python.can u tell me if there is a frontend available > > .if yes where can i download it from Hello Gopinath, Can you explain what you mean by "frontend"? The word "frontend" suffers from being too generic to be able to tell what you mean. (As a concrete example, a search for "python frontend" in Google gives me a GCC compiler frontend as its first hit, and I'm almost positively sure that's not what you're looking for. *grin*) Are you looking for an Integrated Development Environment, like Boa Constructor? http://boa-constructor.sourceforge.net/ If you are looking for IDEs, then there is a list of ones that support Python here: http://www.python.org/moin/IntegratedDevelopmentEnvironments Please tell us more what you are looking for, and we'll try to do our best to hunt that information down. Good luck to you! From dyoo at hkn.eecs.berkeley.edu Wed Jan 12 08:23:53 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 12 08:24:05 2005 Subject: [Tutor] FW: hi (fwd) Message-ID: >> Can you explain what you mean by "frontend"? The word "frontend" >> suffers from being too generic to be able to tell what you mean. > I meant a GUI like Microsofts Visual Basic [Keeping Tutor@python.org in CC. Please use Reply-to-all in replies, so that we can keep the conversation on list.] Ok, in that case, you may want to take a look at the IntegratedDevelopmentEnvironments page: http://www.python.org/moin/IntegratedDevelopmentEnvironments I can't really give personal experience about these, since I'm still using tools like Emacs. *grin* But I'm sure that others on the Tutor list can give their recommendations to you. From alan.gauld at freenet.co.uk Wed Jan 12 09:23:25 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 09:22:47 2005 Subject: [Tutor] More and more OT - Python/Java References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr><41E32E0E.7090802@tds.net><2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr><321086EC-6377-11D9-8A9A-000393CBC88E@yahoo.fr> Message-ID: <01b601c4f87f$fd30b7e0$16c68651@xp> > Only thing I've seen that uses XML (remember I'm a n00bie in Python, > Java, Jscript and HTML, so I don't see the real indepth stuff) is MSN > Messenger for it's logs. And MS IE can parse that XML. XML is rapidly becoming the de-facto standard in data exchange formats between businesses(B2B). In addition the SOAP protocol for web services (aka remote procedurecalls over the web) uses XML so more and more business applications will be using XML. The more extreme XML fans also think it can be used internally as glue between in-house applications (sales-orders-inventory-billing etc) however I personally think the heavy processing and bandwidth demands of XML might delay that vision. HTH, Alan g From alan.gauld at freenet.co.uk Wed Jan 12 09:36:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 09:35:00 2005 Subject: [Tutor] More and more OT - Python/Java References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: <01bb01c4f881$c0dfd260$16c68651@xp> > > 3) For the same reason, it will save bandwidth. The XML data will > > probably take less space than the fully-formatted stuff I'd have to > > spit out with "regular" HTML, and the XSL stylesheet can probably be > > cached by the user's browser. This is a common misperception that should have been corrected by now. XML won;t save you any bandwidth over traditional HTML, in fact it will require significantly more - 50-100% in practice. The reason for this is that you still need to send the formatting info along with the data but now it will be done as two, usually three, separate http GET messages: one for the XML, one for the XSL stytle sheet, (or two for the HTML/XHTML plus CSS style sheet) due to the duplication of tagging between the files plus the http overheads this will invariably result in extra bandwidth over a single HTML file with data embeded. On subsequent updates of the same page it might seem like a bandwidth saving is possible, and thats true compared to a complete page refresh, but best practice today refreshes web pages by using JavaScript to update the fields in life via a binary message, so copmpared to best practive XML will again increase bandwidth - typically by 5-10 times in this case. > > 4) In the same line of reasoning, it'll also save CPU time: XML data, > > being smaller, is generated faster than the equivalent HTML. Its not much smaller and requires more tagging information, plus you need to run a validation check for "well formedness" so the CPU gain is minimal at the server and the extra cost of parsing at the browser is significant... Your users could expect to see a 1-2 second slowdown in response time. This is something of a hot button for me just now -= I see XML being pushed because it makes life easier for developers but it incurs extra costs almost everywhere else. Given that in a typical project, development costs are only 20-30% of the total the net effect is to increase project costs and reduce performance. IMHO far too few software engineers have been trained to properly engineer their solutions taking account of whole-life costs, they only see the programming effort. Alan G. From alan.gauld at freenet.co.uk Wed Jan 12 09:40:12 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 09:39:10 2005 Subject: [Tutor] More and more OT - Python/Java References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> Message-ID: <01c201c4f882$5563d760$16c68651@xp> > I ask that because I'm writting a little program that will make > queries over a 1500 entries database, with very simple queries. I need > an answer in 1 or 2 seconds. I'm using SQLite now, but i wanted > something that depends as little as possible of external dependencies > (as a database). 1500 entries shouldn't be a problem for queries either using DOM (in memory) or XPath or even XQuery. If your app grows to 150000 it might stat to be a problem, and if you get over a million entries I'd definitely start thinking about regular RDBMS. The other feature is that for ~1500 entries the size of the files will be about the same but anything bigger than that and the XML version will quickly start to fill your disks... Alan G. From guillermo.fernandez.castellanos at gmail.com Wed Jan 12 09:50:36 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Wed Jan 12 09:50:40 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <01c201c4f882$5563d760$16c68651@xp> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> <01c201c4f882$5563d760$16c68651@xp> Message-ID: <7d7029e705011200505164be17@mail.gmail.com> > 1500 entries shouldn't be a problem for queries either using DOM > (in memory) or XPath or even XQuery. If your app grows to 150000 > it might stat to be a problem, and if you get over a million > entries I'd definitely start thinking about regular RDBMS. Hi, No it will not go further... let's say 2000 entries not to take any risk. I have not been able to find any recent XML/Python tutorial on the web. Does the xml.dom library have a XPath and XQuery or any SQL-like support? I've understood that it's a pretty basic library... Thanks, G From alan.gauld at freenet.co.uk Wed Jan 12 09:54:06 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 09:53:09 2005 Subject: [Tutor] More and more OT - Python/Java References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: <01c701c4f884$46628610$16c68651@xp> > You are standing in front of a stump. A path leads north. > N Probably more like: you in front of stump path leads NOrth And the XSL will join the bits together using XPathstyle references to "fill in the blanks":

are a . A

> and you have a XSL that works like a CSS? > > descript {font:arial, align:center} > exits style:bolder And yes you can have that too. > Is that a good paraphrasing? How browser dependent would that be? Do > most browsers support XML & XSL? Very few do at present, only the latest generation. But given many folks are still using Netscape 4 and IE 4 and even the latest versions have haphasard XML support we are quite a long way away from unified presentationof XML via browsers. Fortunately theres another technology which can turn XML into HTML, called XSLT and it takes the XSL above and spits out normal HTML to the browser, which is what happens most of the time and the user is blissfully ignorant that XML is underneath the covers. > What's SAX DOM? I know what a DOM is, but what's the SAX? Both are parsers. DOM reads the whole structure into memory and accesses it like a tree. SAX parses the file as its being read and recognises key elements and activates a parsing function. THuis in the XML above you could instruct the parser to call one of your functions everytime it found a tag, passing in the whole content of that tag. Your function then handles only the data inside the tag that it is intersted in. Its faster and saves memory, but is strictly read-only, and read once - you can't change the data and you can't go back a step, you need to restart from the top. Both approaches have their advantages. > my Python docs when I was poking XMLParser. If/when I work with XML, > would you recommend Python's standard modules for it? Yes, Python has good XML support and can use DOM or SAX. Alan G. From alan.gauld at freenet.co.uk Wed Jan 12 10:13:57 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 10:12:55 2005 Subject: [Tutor] More and more OT - Python/Java References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> <01c201c4f882$5563d760$16c68651@xp> <7d7029e705011200505164be17@mail.gmail.com> Message-ID: <01f301c4f887$0c812480$16c68651@xp> > I have not been able to find any recent XML/Python tutorial on the > web. Does the xml.dom library have a XPath and XQuery or any SQL-like > support? I've understood that it's a pretty basic library... I don't thoink there's XPath or XQuery in Python yet. There is a thread on this on comp.lang.python just now - try a Google Groups search - but I haven't been following it. Alan G. From yduppen at xs4all.nl Wed Jan 12 10:15:19 2005 From: yduppen at xs4all.nl (Yigal Duppen) Date: Wed Jan 12 10:15:22 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> Message-ID: <200501121015.19735.yduppen@xs4all.nl> On Wednesday 12 January 2005 00:34, Max Noel wrote: > > As a bonus, I've decided to have a look at XSL, which allows me to > > format a XML file for display in a web browser. It entirely changed my > > perception of web programming. > > I intend to program an on-line browser-based game with some friends > > of mine later in the year (in Python of course -- I converted them), > > and now that I've seen what XML and XSL can do, we're so going to use > > them for data output: the data is in dynamically-generated XML, which > > links to a (static) XSL stylesheet to tell the browser how to render > > that data. Hi Max, While I agree that the XML + XSL -> HTML architecture *sounds* very nice, in real life it's not so sweet. I've worked on some projects that used this approach and IMHO, the biggest problem is the debugging of XSL output -- XSL scripts are a horror to read, and the complex matching rules often make it very hard to discover which template caused which error. In this case I'd suggest a templating language, such as TAL (part of Zope, http://www.zope.org) or a different language. At my work (Java) we now use JSP, a Java templating language; debugging time has significantly decreased by using this instead of XSL. > > Doing things that way has many advantages: > > 1) Data is separate from formatting. That's always a Good Thing(TM). > > If I someday decide that I don't like the way the site looks, I > > theoretically only need to recreate a stylesheet, without touching > > anything else. (boom! Instant skins!) You're completely right, but as I pointed out, XSLT is not the only option. > > 2) Most of the "HTML rendering" is going to be done by the user's > > browser. This, and the way XSL stylesheets are constructed will > > prevent many bad HTML issues. HTML rendering is usually not an issue for web-based programs. And if it is, a bit of front-end caching usually can save a lot more time. After all, whether you have to render HTML or XML, you do have to render something. As for the bad HTML issues, the same issues (unclosed tags, wrong attributes) do exist when rendering XML. > > 3) For the same reason, it will save bandwidth. The XML data will > > probably take less space than the fully-formatted stuff I'd have to > > spit out with "regular" HTML, and the XSL stylesheet can probably be > > cached by the user's browser. As Alan pointed out, this might very well not be true :-) > > 4) In the same line of reasoning, it'll also save CPU time: XML data, > > being smaller, is generated faster than the equivalent HTML. Granted, > > the stylesheet is another server request, but it's static, so it puts > > virtually no load on a server. My answer to this is the same as nr 2. Yigal From yduppen at xs4all.nl Wed Jan 12 10:23:00 2005 From: yduppen at xs4all.nl (Yigal Duppen) Date: Wed Jan 12 10:23:02 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <01c201c4f882$5563d760$16c68651@xp> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> <01c201c4f882$5563d760$16c68651@xp> Message-ID: <200501121023.00527.yduppen@xs4all.nl> On Wednesday 12 January 2005 09:40, Alan Gauld wrote: > > I ask that because I'm writting a little program that will make > > queries over a 1500 entries database, with very simple queries. I > > need > > > an answer in 1 or 2 seconds. I'm using SQLite now, but i wanted > > something that depends as little as possible of external > > dependencies > > > (as a database). > > 1500 entries shouldn't be a problem for queries either using DOM > (in memory) or XPath or even XQuery. If your app grows to 150000 > it might stat to be a problem, and if you get over a million > entries I'd definitely start thinking about regular RDBMS. > > The other feature is that for ~1500 entries the size of the > files will be about the same but anything bigger than that > and the XML version will quickly start to fill your disks... I actually have some realy life experience with using XML files instead of a database, and it's a bit of a mixed blessing. A big advantage of XML over a relational database is the built-in support for hierarchical structuring. When you're mainly reasoning about trees (in this particular case, parse trees) the relational solution is a bit... messy. For example, to find all assignment statements at the top-level of a function, XPath allows you to say something like: //function-definition/statement[@type="assign"] the equivalent is SQL is not so nice. On the other hand, the amount of XML our project produced was *huge*, and much of the processing time of the project went into parsing XML. Furthermore, while XPath allows for very cute queries, no XPath implementation is as optimized as SQL. All in all, I'd never advise XML as a database. Yigal From bds at waywood.co.uk Wed Jan 12 10:35:02 2005 From: bds at waywood.co.uk (Barnaby Scott) Date: Wed Jan 12 10:36:41 2005 Subject: [Tutor] class instance with identity crisis Message-ID: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> This is probably a very easy question, and one that I was shocked to find I was stuck on, having thought I understood classes! I was wondering how you can get an instance of a class to change itself into something else (given certain circumstances), but doing so from within a method. So: class Damson: def __str__(self): return 'damson' def dry(self): self = Prune() class Prune: def __str__(self): return 'prune' weapon = Damson() weapon.dry() print weapon All the old hands will of course know this will produce the output damson but something in me suggests it should produce prune After all, 'self' refers to the instance 'weapon'. Obviously one could reassign weapon to a Prune outside the class definition, but I was hoping to write something in which, given certain circustances arising, the instance would change itself into something else. Time to go back to school! From pierre.barbier at cirad.fr Wed Jan 12 11:07:01 2005 From: pierre.barbier at cirad.fr (Pierre Barbier de Reuille) Date: Wed Jan 12 11:05:11 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> Message-ID: <41E4F6C5.9060001@cirad.fr> My opinion is : this is a very dangerous and "stupid" thing to do !!! Try to imagine the complexity of your program for someone who is trying to understand how your code is working if an object suddenly change its own type !!! Clearly, if you want to change the type of an object, you want a conversion method (or function) and creates a new object (that's what you'll do anyway). If you don't need the old object anymore, you can just reuse the variable name. Then, it's impossible to achieve in Python (at least in the general case ... you could do it with pure Python objects). And I hope it will forever remain impossible ! And last, I think you misunderstood the behaviour of the assignment statement in Python. In no way an assignement will overwrite an existing value. It will simply make the variable assigned point on another object. The consequence is, if there are many variables pointing on the same object, reassigning one of them will not affect the other ones. So when you write "self = Prune()" you don't change the variable the user holds but only the local "self" variable, variable that will disappear at the end of the method and so your "Prune" object will be discarded. I hope my last paragraph is clear (but I don't think so :D). Pierre Barnaby Scott a ?crit : > This is probably a very easy question, and one that I was shocked to find I > was stuck on, having thought I understood classes! > > I was wondering how you can get an instance of a class to change itself into > something else (given certain circumstances), but doing so from within a > method. So: > > class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() > > class Prune: > def __str__(self): > return 'prune' > > weapon = Damson() > weapon.dry() > print weapon > > All the old hands will of course know this will produce the output > > damson > > but something in me suggests it should produce > > prune > > After all, 'self' refers to the instance 'weapon'. > > Obviously one could reassign weapon to a Prune outside the class definition, > but I was hoping to write something in which, given certain circustances > arising, the instance would change itself into something else. > > Time to go back to school! > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68 From ismaelgf at adinet.com.uy Wed Jan 12 11:15:06 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Wed Jan 12 11:15:12 2005 Subject: [Tutor] FW: hi In-Reply-To: References: Message-ID: <41E4F8AA.8060309@adinet.com.uy> Danny Yoo wrote: >Are you looking for an Integrated Development Environment, like Boa >Constructor? > > http://boa-constructor.sourceforge.net/ > > Is there any way of running Boa with Python 2.4 -only-? When I tried installing it required wxWindows, but only version 2.4.2.4 (and not ver. 2.5, which supports P2.4), which I couldn't find for Python 2.4, only 2.3. Is there? Thanks Ismael From kent37 at tds.net Wed Jan 12 12:00:02 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 12 12:00:07 2005 Subject: [Tutor] FW: hi In-Reply-To: References: Message-ID: <41E50332.70802@tds.net> Active Python comes with IDLE and PythonWin, both are simple IDEs for Python. Kent Gopinath V, ASDC Chennai wrote: > > > ----- > i'm usinf active python 2.4 running in windows nt > > -----Original Message----- > *From: * Gopinath V, ASDC Chennai > *Sent: * Wednesday, January 12, 2005 10:46 AM > *To: * 'tutor@python.org' > *Subject: * hi > > Hi > I'm a Novice in python.can u tell me if there is a frontend > available .if yes where can i download it from > > > > > > */With Regards,/*// > */Gopinath V/*// > */HCL Technologies Ltd., ASDC, > /*// > > */Mail :_ gopinathv@hcltech.com_/*// > > ***//**/ /*// > > _/Disclaimer:/_// > ///This message and any attachment(s) contained here are > information that is confidential, proprietary to HCL Technologies > and its customers. Contents may be privileged or otherwise > protected by law. The information is solely intended for the > individual or the entity it is addressed to. If you are not the > intended recipient of this message, you are not authorized to > read, forward, print, retain, copy or disseminate this message or > any part of it. If you have received this e-mail in error, please > notify the sender immediately by return e-mail and delete it from > your computer./ > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From gopinathv at hcltech.com Wed Jan 12 12:07:13 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Wed Jan 12 12:07:21 2005 Subject: [Tutor] Is Tkinter the Gui in python Message-ID: Hi Is the standard Tkinter module in python does the GUI - Front -end Interface for Python -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/4c75cedc/attachment.html From kent37 at tds.net Wed Jan 12 12:10:22 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 12 12:10:27 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> Message-ID: <41E5059E.8090406@tds.net> A couple of ideas: You could have dry() return the new weapon: def dry(self): return Prune() then the client code would be weapon = weapon.dry() You could have the weapon encapsulate another object and delegate to it. Finally, you actually can change the class of an object just by assigning to self.__class__: >>> class A: ... def show(self): ... print "I'm an A" ... >>> class B: ... def show(self): ... print "I'm a B" ... >>> a=A() >>> a.__class__ >>> a.show() I'm an A >>> a.__class__ = B >>> type(a) >>> a.show() I'm a B So if you change self = Prune() to self.__class__ = Prune I think you will get the behaviour you are looking for. I would look for another way to solve your problem, though - I agree with Pierre that this is likely to be confusing. Kent Barnaby Scott wrote: > This is probably a very easy question, and one that I was shocked to find I > was stuck on, having thought I understood classes! > > I was wondering how you can get an instance of a class to change itself into > something else (given certain circumstances), but doing so from within a > method. So: > > class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() > > class Prune: > def __str__(self): > return 'prune' > > weapon = Damson() > weapon.dry() > print weapon > > All the old hands will of course know this will produce the output > > damson > > but something in me suggests it should produce > > prune > > After all, 'self' refers to the instance 'weapon'. > > Obviously one could reassign weapon to a Prune outside the class definition, > but I was hoping to write something in which, given certain circustances > arising, the instance would change itself into something else. > > Time to go back to school! > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From yduppen at xs4all.nl Wed Jan 12 12:31:37 2005 From: yduppen at xs4all.nl (Yigal Duppen) Date: Wed Jan 12 12:31:39 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <41E5059E.8090406@tds.net> References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> <41E5059E.8090406@tds.net> Message-ID: <200501121231.37382.yduppen@xs4all.nl> On Wednesday 12 January 2005 12:10, Kent Johnson wrote: > A couple of ideas: > > You could have dry() return the new weapon: > def dry(self): > return Prune() > > then the client code would be > weapon = weapon.dry() > > > You could have the weapon encapsulate another object and delegate to it. As an alternative solution, you could use the Proxy pattern; this approach has the advantage that there is no dynamic reassignment of the __class__ attribute. The following program shows what I mean: class Apple: def whoami(self): print "I am an apple!" def __str__(self): return "Apple" class Egg: def whoami(self): print "I am an egg!" def __str__(self): return "Egg" class Proxy: def __init__(self): self.delegate = Apple() self.is_apple = True def __getattr__(self, attr): return getattr(self.delegate, attr) def change(self): if self.is_apple: self.delegate = Egg() else: self.delegate = Apple() self.is_apple = not self.is_apple if __name__ == "__main__": thing = Proxy() thing.whoami() print thing thing.change() thing.whoami() print thing thing.change() thing.whoami() print thing This will give the following output: I am an apple! Apple I am an egg! Egg I am an apple! Apple The magic here lies in the __getattr__ (note the double underscores) method; whenever the Proxy is asked for an attribute (such as a method), it delegates this question to the self.delegate. Alternatively, if the __getattr__ is a bit too much magic, you could also duplicate the attributes that you actually want to expost: class Proxy: def __init__(self): # as above def whoami(self): self.delegate.whoami() def __str__(self): return str(self.delegate) def change(self): # as above As others have stated, you should use this pattern with care. On the other hand, I do believe that there are instances when this can be useful, as long as the delegates have more or less the same interface (the same attributes). Yigal From kent37 at tds.net Wed Jan 12 13:57:02 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 12 13:57:09 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <7d7029e705011200505164be17@mail.gmail.com> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> <01c201c4f882$5563d760$16c68651@xp> <7d7029e705011200505164be17@mail.gmail.com> Message-ID: <41E51E9E.7090902@tds.net> Guillermo Fernandez Castellanos wrote: > I have not been able to find any recent XML/Python tutorial on the > web. There is a Python and XML topic guide at python.org http://pyxml.sourceforge.net/topics/ Uche Ogbuji writes regularly about Python and XML for xml.com http://www.xml.com/pub/au/84 Does the xml.dom library have a XPath and XQuery or any SQL-like > support? I've understood that it's a pretty basic library... My impression is the built-in stuff is pretty basic. If you want to use SAX and DOM for XML in Python, the 4Suite add-on is pretty standard. http://4suite.org/index.xhtml I've also heard good things about libxml2; it includes XPath support http://xmlsoft.org/python.html IMO Fredrik Lundh's ElementTree package is the easiest way to work with XML from Python. It supports a subset of XPath and he has just released a compiled C version http://effbot.org/zone/element-index.htm Kent > > Thanks, > > G > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From davholla2002 at yahoo.co.uk Wed Jan 12 14:43:55 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Wed Jan 12 14:43:59 2005 Subject: [Tutor] Crossword program In-Reply-To: <002f01c4f697$9dda98e0$16c68651@xp> Message-ID: <20050112134355.16643.qmail@web25405.mail.ukl.yahoo.com> Thanks Alan for that suggestion. I will try and tidy it up myself and then ask the tutor list for another suggestions. Alan Gauld wrote:Hi David, Its probably the kind of thing you could put on the Useless Python website. Useless is a collection of not-too-serious software written in Python which, despite the name, is actually quite useful for beginners to download and look at and learn. They can try improving it, or using the ideas for their own programs. But you might well get some suggestions on tidying it up a little first... Alan G. ----- Original Message ----- From: "David Holland" To: Sent: Sunday, January 09, 2005 9:44 PM Subject: [Tutor] Crossword program > I wrote a program to create crosswords in python. > It is not perfect but it works, is there any open > source place I can put this for it to be used by > anyone who wants it ? (Subject to the gpl licence). > Here is the code in case anyone is interested. > > from Tkinter import * > #Crossword program David Holland > class Crossword(Frame): > > def __init__(self, master): > '''Default arguments''' > Frame.__init__(self, master) > self.grid() > self.create_widgets() > NUM_SQUARES = 169 > EMPTY = " " > global questionanswer > questionanswer = {} > global dictshort > dictshort = {} > > def readdict(self): > #get the dictionary from a file > try: > pickle_file = open(dictfile, "rb") > dictread = cPickle.load(pickle_file) > except: > dictread = {} > return dictread > > def writedict(self, dictent): > #write dictionary to file > pickle_file = open(dictfile, "wb") > cPickle.dump(dictent, pickle_file) > pickle_file.close() > > def showclues(self): > #show clues on the screen > questionanswer = self.readdict() > textent = self.showinfo(questionanswer) > #textent = questionanswer > self.putinfo(textent) > > def showinfo(self, dictent): > #make the clues look readable > i = 0 > listkeys = dictent.keys() > #listkeys = listkeys.sort() > textdisp = '' > for item in listkeys: > i = i+1 > istr = str(i) + " " > question = dictent[item] > textdisp = textdisp + istr + " the > question is " + question + " the answer is " + item + > "\n" > return textdisp > > def newcrosswordboard(self): > #create a crossword board > board = [] > for square in range (NUM_SQUARES): > board.append(EMPTY) > return board > > # def newcrosswordboard(self): > # #this is create a board with the numbers > # board = [] > # for square in range (NUM_SQUARES): > # text = str(square) > # board.append(text) > # return board > > def deleteclue(self): > #delete a clue > try: > numberentered = self.delete_ent.get() > dictent = self.readdict() > numberentered = int(numberentered) > listkeys = dictent.keys() > i = 1 > clue = '' > for item in listkeys: > if numberentered == i: > del dictent[item] > clue = item > break > i = i +1 > text = "Clue " + clue + " has been removed > the list of clues now is :-" + "\n" > self.writedict(dictent) > moretext = self.showinfo(dictent) > text = text + moretext > except: > text = "Please enter a number as a figure" > self.putinfo(text) > > def create_widgets(self): > #create GUI > self.question_lbl = Label(self, text = "Enter > the question ") > self.question_lbl.grid(row = 0, column = 0, > columnspan = 2, sticky = W) > self.answer_lbl = Label(self, text = "Enter > the answer") > self.answer_lbl.grid(row = 1, column = 0, > columnspan = 2, sticky = W) > self.delete_lbl = Label(self, text = "Enter > the number of a clue you want deleted") > self.delete_lbl.grid(row = 2, column = 0, > columnspan = 2, sticky = W) > #entry widget for the question > self.question_ent = Entry(self) > self.question_ent.grid(row=0, column = 2, > columnspan = 1, sticky = W) > self.answer_ent = Entry(self) > self.answer_ent.grid(row=1, column = 2, > columnspan = 2, sticky = W) > self.delete_ent = Entry(self) > self.delete_ent.grid(row=2, column = 2, > columnspan = 1, sticky = W) > > #button to add entries > Button(self, text = "Click to add clues ", > command = self.create_questionsanswer).grid(row = 0, > column = 3, columnspan = 3) > Button(self, text = "Click to show clues ", > command = self.showclues).grid(row = 1, column = 3, > columnspan = 3) > Button(self, text = "Click to delete clue ", > command = self.deleteclue).grid(row = 2, column = 3, > columnspan = 3) > #button to create the cross word > Button(self, text = "Click to create crossword > ", command = self.crosswordcreation).grid(row = 16, > column = 0, columnspan = 4) > Button(self, text = "Click to get help ", > command = self.helpme).grid(row = 16, column = 3, > columnspan = 4) > self.info_txt = Text(self, width = 80, height > = 30, wrap = WORD) > self.info_txt.grid(row = 20, column = 0, > columnspan = 4) > #show help > self.helpme() > > def helpme(self): > #get help > helptext = 'To create crosswords add words by > entering where it says enter the question and answer, > then press the button "click to add clues." ' + '\n' > helptext = helptext + 'To see all clues click > "show all clues".' > helptext = helptext + 'to delete a clue enter > its number and then click delete clue. ' > helptext = helptext + 'To create a crossword > click create crossword. The crossword will be saved > in a file called cross.txt. ' > helptext = helptext + '\n' + 'If you like the > crossword save the file with a different name, create > another crossword. ' > helptext = helptext + '\n' +'Author David > Richard Holland, this is released under the GPL > licence' > self.putinfo(helptext) > > def create_questionsanswer(self): > #this creates the question > questionanswer = self.readdict() > question = self.question_ent.get() > question = self.converttoupper(question) > answer = self.answer_ent.get() > answer = self.converttoupper(answer) > textent = "This has been added to list" > #the code below does not work properly > if answer not in questionanswer and question > not in questionanswer: > questionanswer[answer] = question > self.writedict(questionanswer) > self.putinfo(textent) > elif answer in questionanswer: > self.termexists(answer) > #########print "line 38" > elif question in questionanswer: > self.termexists(question) > > > > def converttoupper(self, texttoupper): > #conver string to upper text > try: > texttoupper = str(texttoupper) > texttoupper = string.upper(texttoupper) > except: > texttoupper = texttoupper > return texttoupper > > def termexists(self, answer): > textent = answer, 'is already an answer for > the crossword enter another' > self.putinfo(textent) > > def putinfo(self, textent): > #put the info to the GUI > self.info_txt.delete(0.0, END) > self.info_txt.insert(0.0,textent) > > def savecross(self, textent): > file = open('cross.txt','w') > file.write(textent) > file.close() > > def crosswordcreation(self): > #get the board info from create crossword > board = self.createcrossword() > board2 = self.createcrossword() > numhori = 0 > numvert = 0 > #########print "crossword creationcalled" > questionanswer = self.readdict() > #here put in code to put the answers from the > dictionary in the board > #code goes here > #first sort the dictionary into 2 one with > values > 4 in len the other one everything else > dictshort = {} > dictshort, dictlong = > self.changedict(questionanswer, dictshort) > #########print "dictionary changed" > #variables for the questions > diagtext = 'Across ' + '\n' > verttext = 'Down '+ '\n' > badquestion = 'These could not be asked' + > '\n' > #code to populate the board > board, board2,numhori, numvert, diagtext, > verttext, badquestion = self.putwordboard(dictlong, > board, board2, numhori, numvert, diagtext, verttext, > badquestion) > board2, board2, numhori, numvert, diagtext, > verttext, badquestion = self.putwordboard(dictshort, > board, board2, numhori, numvert, diagtext, verttext, > badquestion) > #code to show get the board as a picture > text = self.display_board(board) > text = text + "\n" > textnew = self.display_board(board2) > text = text + textnew > text = text + diagtext + verttext + > badquestion #+ str(board) > #board2 is the board that shows the clues > #text2 = self.display_board(board3) > #text = text + text2 > #save the cross word > self.savecross(text) > #display to screen > self.putinfo(text) > print "finished" > > > def putwordboard(self, dict, board, board2, > numhori, numvert, diagtext, verttext, badquestion): > listkeys = dict.keys() > #this takes the words and puts them in the > crossword via sub functions > item = random.choice(listkeys) > while len(listkeys) > 0: > # for item in listkeys: > print item > # emptylist, fulllist = > self.createlist(board) > board, board2, canbeentered, numhori, > numvert, numbermove = self.putcharboard(item, board, > board2, numhori, numvert) > question = dict[item] > question = string.capitalize(question) > question = ' ' + question > lenquestion = len(item) > lenquestion = str(lenquestion) > lenquestion = ' ' + lenquestion + " > letters long" + '\n' > if numbermove == 13 and canbeentered == > 'Yes': > #numvert = int(numvert) > #numvert = numvert + 1 > numvert = str(numvert) > > verttext = verttext + numvert + > question + lenquestion > numvert = int(numvert) > textent = item + " has been entered" > elif numbermove == 1 and canbeentered == > 'Yes': > #numhori = int(numhori) > #numhori = numhori + 1 > numhori = str(numhori) > diagtext = diagtext + numhori + > question + lenquestion > numhori = int(numhori) > textent = item + " has been entered" > else: > badquestion = badquestion + question > > #self.putinfo(textent) > #get a new random item and remove the old > one from the list > item, listkeys = > self.changenummove(listkeys, item) > return board, board2, numhori, numvert, > diagtext, verttext, badquestion > > def createlist(self, board): > emptylist = [] > fulllist = [] > #create a list of squares which are empty and > square which are not > #######print NUM_SQUARES > for square in range (NUM_SQUARES): > if board[square] == EMPTY: > emptylist.append(square) > else: > fulllist.append(square) > return emptylist, fulllist > > > def createnumbermove(self): > #create the number in which direction to move > listchoice = [1,13] > numbermove = random.choice(listchoice) > return listchoice, numbermove > > def changenummove(self, listchoice, numbermove): > #change the number in which direction should > be moved > #make the list of options smaller > #a = str(numbermove) > #listchoice.remove(a) > #the above does not work so this loop should > do > i = 0 > for item in listchoice: > if item == numbermove: > break > else: > i = i+1 > del listchoice[i] > if len(listchoice) >0: > numbermove = random.choice(listchoice) > return numbermove, listchoice > > def putcharboard(self, word, board, board2, > numhori, numvert): > #see where the word can be put in the board > #get a list of possible numbers that it can > move and choose one randomly > listchoice, numbermove = > self.createnumbermove() > j = 0 > i = 0 > lengthofword = len(word) > canbeentered = 'No' > ##########print "line 113" > if len(USED_SQUARES) == 0: > j = lengthofword > #code to choose between going up or down > #while canbeentered == 'No' and j > > #while canbeentered == 'No' and j > > #call a function that will see if it can be > entered where that a letter in the word already is > emptylist, full_list = self.createlist(board) > #create a variable which means that it loops > for the number of different options in numbermove > list_choice_var = len(listchoice) +1 > > while list_choice_var >0 and canbeentered == > 'No': > ##print word, numbermove > if len(full_list) > 0: > try: > canbeentered, char, j, square = > self.canbeenteredparta( word, numbermove, > canbeentered, lengthofword, board) > except: > square = 0 > #as the word can not be entered change the > direction of move > if canbeentered == 'No' and > len(listchoice) > 0: > numbermove, listchoice = > self.changenummove(listchoice, numbermove) > list_choice_var = list_choice_var -1 > if canbeentered == 'No': > listchoice, numbermove = > self.createnumbermove() > ##print "listchoice is", listchoice, > "numbermove is", numbermove > j = 0 > list_choice_var = len(listchoice) > ##print "line 308 " > #a loop to change numbermove randomly so that > all possible positions are entered a sub function > sees if this can be entered > #this code is to get find an empty square > randomly and see if the word can be entered > k = 0 > while list_choice_var >0 and canbeentered == > 'No': > canbeentered, word, char, j, square = > self.canbeenteredpartb(j, word, numbermove, > canbeentered, lengthofword, board) > k = k +1 > #print word, numbermove, "line 305", > canbeentered > if canbeentered == 'No' and > len(listchoice) > 0: > #if you can not enter it get a new > value for the position it can be in > ##print "line 316" > numbermove, listchoice = > self.changenummove(listchoice, numbermove) > #print "line 318" > #print "line 308", numbermove > #reduce the variable by 1 > list_choice_var = list_choice_var -1 > #print "k is", k > if canbeentered == 'Yes': > #calls a function that puts the word in > the board > board = self.putwordinboard(word, board, > char, square, j, numbermove, lengthofword) > > if numbermove == 1: > numhori = numhori +1 > newword = > self.createword(lengthofword, j, numhori) > #create a new word ie a blank word > numhori and numvert are the numbers of the questions > board2 = self.putwordinboard(newword, > board2, numhori, square, j, numbermove, lengthofword) > else: > numvert = numvert + 1 > newword = > self.createword(lengthofword, j, numvert) > board2 = self.putwordinboard(newword, > board2, numvert, square, j, numbermove, lengthofword) > > return board, board2, canbeentered, numhori, > numvert, numbermove > > > def makecharpretty(self, char): > #make char go in the crossword better in one > place so that the code can be maintained better > char = ' '+char+' ' > return char > > def canbeenteredparta(self, word, numbermove, > canbeentered, lengthofword, board): > #this sees if the word can be enterd somewhere > it already exists > word = str(word) > lengthofwordnew = lengthofword > #is a variable to show what letter in the word > is the one to start from > j = 0 > for char in word: > if canbeentered == 'Yes': > break > emptylist, full_list = > self.createlist(board) > if len(full_list) > 0: > square = random.choice(full_list) > else: > break > while len(full_list) > 0: > if canbeentered == 'Yes': > break > letter = self.makecharpretty(char) > if board[square] == letter: > #####print "square is the same > as letter", char > canbeentered = > self.canbeentered(word, board, char, square, j, > numbermove) > #if it can be entered break the while > loop > if canbeentered == 'Yes': > break > ######print "line 364" > elif canbeentered == 'No': > square, full_list = > self.changenummove(full_list, square) > #######print "line 365" > if canbeentered == 'Yes': > break > #it can be entered so break the for loop > as well as the while otherwise increase j > else: > j = j +1 > return canbeentered, char, j, square > > def canbeenteredpartb(self, j, word, numbermove, > canbeentered, lengthofword, board): > #initialise variables of course these will not > be created if the loop is not called > char = '' > #i = 0 > square = '' > for char in word: > i = 0 > #####print "char", char, "is being > tested", "canbeentered = ", canbeentered > copyemptylist, fullist = > self.createlist(board) > #######print "char is", char,"word is", > word > #######print "empty list is", > copyemptylist > if len(copyemptylist) > 0: > square = random.choice(copyemptylist) > #get a random square then start the loop > > while len(copyemptylist) > 0: > canbeentered = self.canbeentered(word, > board, char, square, j, numbermove) > #########print > canbeentered > if canbeentered == 'Yes': > break > ######print "canbeentered is", > canbeentered > else: > #if it can be entered we break other > wise get a new square > #######print "line 428", square > square, copyemptylist = > self.changenummove(copyemptylist, square) > i = i +1 > ##print i > #if this char can be entered break the for > loop as well > if canbeentered == 'Yes': > break > return canbeentered, word, char, > j, square > else: > j = j +1 > #break > return canbeentered, word, char, j, square > > > def createword(self, lengthofword, numinword, > char): > numforward = (lengthofword - numinword) -1 > i = 0 > char = str(char) > charnew = '-' > while i < numforward: > char = char + charnew === message truncated === --------------------------------- ALL-NEW Yahoo! Messenger - all new features - even more fun! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/074b4748/attachment-0001.html From kp8 at mac.com Wed Jan 12 16:08:02 2005 From: kp8 at mac.com (kevin parks) Date: Wed Jan 12 16:08:10 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: thanks everyone... I will look at all the various appraoches folks came up with and see what i can learnn from them. I ended doing something lame -- a brute force method. I formmatted and reformatted my input data and stuffed it in a HUGE dictionary.... it was stupid and kludgy.... i hope to study all these approaches and learn something that may help me do something that is more flexible about the input data and is more elegant..... here's what i came up with ... with my pea sized brain... #!/usr/bin/env python # New in version 2.3 is the 'datetime' module (see standard library reference) # http://www.python.org/doc/lib/module-datetime.html import datetime inseqs = { (1) : ['DAT_1', '01', '00:00:23', '00:08:23'], (2) : ['DAT_1', '02', '00:08:23', '00:09:41'], (513) : ['DAT_75', '10', '00:59:55', '01:11:05'], (514) : ['DAT_75', '11', '01:11:05', '01:16:15'], (515) : ['DAT_75', '12', '01:16:15', '01:34:15'], (516) : ['DAT_75', '13', '01:34:15', '01:45:15'], (517) : ['DAT_75', '14', '01:45:15', '01:48:00'] } mykeys = inseqs.keys() # first make a copy of the keys mykeys.sort() # now sort that copy in place for key in mykeys: event = inseqs[key] print '\n','Item #', key, event TD = datetime.timedelta h, m, s = event[2].split(':') zero_adjust = TD(hours=int(h), minutes=int(m),seconds=int(s)) # print ' Q___ ', key, event[:2], ': ', for item in event[2:]: hrs, mins, secs, = item.split(':') time1 = TD(hours=int(hrs), minutes=int(mins),seconds=int(secs)) print time1 - zero_adjust, print From kp8 at mac.com Wed Jan 12 18:17:06 2005 From: kp8 at mac.com (kevin parks) Date: Wed Jan 12 18:17:19 2005 Subject: [Tutor] spaces in print Message-ID: when i print: print '\n','siday_q', key, '.wav'# event i get: siday_q 515 .wav how can you eliminate the spaces to get: siday_q515.wav ? From maxnoel_fr at yahoo.fr Wed Jan 12 18:24:51 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 12 18:24:58 2005 Subject: [Tutor] spaces in print In-Reply-To: References: Message-ID: On Jan 12, 2005, at 17:17, kevin parks wrote: > > > when i print: > > print '\n','siday_q', key, '.wav'# event > > > i get: > > siday_q 515 .wav > > > how can you eliminate the spaces to get: > > siday_q515.wav The quick and dirty way is to do: print '\n' + 'siday_q' + key + '.wav' The elegant way is to do use string formatting: print '\nsiday_q%s.wav' % (key) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From jeff at ccvcorp.com Wed Jan 12 19:40:07 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Wed Jan 12 19:34:52 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> Message-ID: <41E56F07.30706@ccvcorp.com> Barnaby Scott wrote: > class Damson: > def __str__(self): > return 'damson' > def dry(self): > self = Prune() > > class Prune: > def __str__(self): > return 'prune' > > weapon = Damson() > weapon.dry() > print weapon > > [...] > > but something in me suggests it should produce > > prune > > After all, 'self' refers to the instance 'weapon'. Ah, but 'self' is a method-local name. You're re-binding that name, not affecting the object (previously) pointed to by the name. And of course, when the method returns, those local bindings go away. > Obviously one could reassign weapon to a Prune outside the class definition, > but I was hoping to write something in which, given certain circustances > arising, the instance would change itself into something else. As others have mentioned, you can change the type of the object by assigning to self.__class__, but this can get a bit hairy. Your class/instance knows nothing about what names it may be bound to, so it can't rebind those names to a different object (i.e. class Damson, and any instances you create, know nothing of the name 'weapon'). I tend to favor the explicit approach of having weapon.dry() *return* a different object, and then using 'weapon = weapon.dry()'. But if you really need to have objects that change their type as a side-effect of a method call, you can use a proxy or adapter -- in essence, you have a class that can *contain* either a Damson or a Prune, and which forwards method calls / attribute accesses to its contained object. Jeff Shannon Technician/Programmer Credit International From dyoo at hkn.eecs.berkeley.edu Wed Jan 12 19:50:15 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 12 19:50:22 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> Message-ID: On Wed, 12 Jan 2005, Barnaby Scott wrote: > I was wondering how you can get an instance of a class to change itself > into something else (given certain circumstances), but doing so from > within a method. So: > > class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() > > class Prune: > def __str__(self): > return 'prune' > > weapon = Damson() > weapon.dry() > print weapon Hi Scott, The issue with that, as you know, is that it conflicts with the way local variables work in functions. For example: ### >>> def strip_and_print(x): ... x = x.strip() ... print x ... >>> message = " hello " >>> strip_and_print(message) hello >>> message ' hello ' ### Our reassignment to 'x' in strip_and_print has no effect on "message"'s binding to " hello ". That's how we're able to use local variables as black boxes. For the same reasons, the reassignment to 'self in: > class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() is limited in scope to the dry() method. But can you do what you're trying to do with object composition? That is, would something like this work for you? ### class Damson: def __init__(self): self.state = DamsonState() def __str__(self): return str(self.state) def dry(self): self.state = PruneState() class DamsonState: def __str__(self): return "damson" class PruneState: def __str__(self): return 'prune' ### This structuring allows us to switch the way that str() applies to Damson. The OOP Design Pattern folks call this the "State" pattern: http://c2.com/cgi/wiki?StatePattern If you have any questions, please feel free to ask! From wan at walrus.us Wed Jan 12 19:58:45 2005 From: wan at walrus.us (Vincent Wan) Date: Wed Jan 12 19:58:50 2005 Subject: [Tutor] TypeError I don't understand Message-ID: Dear All, python gives me a type error that I don't understand running my program gives me "TypeError: len() of unsized object" but I know I that the object whose length I asked for is a list and therefor a sized type here is a sample of the output: type (living_species) : len(living_species) : 1 Traceback (most recent call last): File "new_prog1b.txt", line 15, in ? while counter < len(living_species): TypeError: len() of unsized object here is the code: import random living_species = [0] dead_species = [] tree = "*0*" current_time = 0 end_of_time = 25 current_species = 0 branching_probablity = .1 extinction_probablity = .1 while current_time < end_of_time: counter = 0 while counter < len(living_species): if random.random() < branching_probablity: current_species += 1 living_species.append(current_species) target = '*' + str(living_species[counter]) + '*' replacement = '(' + target + ',*' + str(current_species) + '*)' tree = tree.replace(target, replacement) counter += 1 # PROBLEM HERE print 'type (living_species) :',type(living_species) # proves living_species is a list print 'len(living_species) :', len(living_species) # proves length living_species is a number counter = len(living_species) - 1 # python says TypeError: len() of unsized object while counter >- 1: if random.random() < extinction_probablity: dead_species = dead_species.append(counter) living_species = living_species.remove(counter) counter -= 1 current_time += 1 print living_species print dead_species tree = tree.replace('*','') print tree Thank You, Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wanATwalrusDOTus (replace AT with "@" and dot with ".") From alan.gauld at freenet.co.uk Wed Jan 12 20:02:09 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 20:01:17 2005 Subject: [Tutor] class instance with identity crisis References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> Message-ID: <021a01c4f8d9$3827f170$16c68651@xp> > class Damson: > def __str__(self): > return 'damson' > > def dry(self): > self = Prune() You'll neeed to return it for the outside world to use it... So try def dry(self): return Prune() > class Prune: > def __str__(self): > return 'prune' > > weapon = Damson() > weapon.dry() > print weapon Should work as expected... Alan G. From alan.gauld at freenet.co.uk Wed Jan 12 20:08:19 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 20:10:00 2005 Subject: [Tutor] class instance with identity crisis References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> <41E4F6C5.9060001@cirad.fr> Message-ID: <024001c4f8da$78390be0$16c68651@xp> > My opinion is : this is a very dangerous and "stupid" thing to do !!! No its quite common. Its why C++ for example allows you to write your own type convcersion functions! One area where I've used this is to convert faults to orders in a service management application. Its fairly common for a customer to report a fault which then turns out to be due to a missing extra that theynneed to order. The conversion function then copies all the captured order details (cust ID and description etc) into a new fault object and returns a reference to that. > own type !!! Clearly, if you want to change the type of an object, you > want a conversion method (or function) and creates a new object (that's But this is true, changing the actual instance under the covers is a bad idea. > And last, I think you misunderstood the behaviour of the assignment > statement in Python. In no way an assignement will overwrite an existing > value. It will .... > > I hope my last paragraph is clear (but I don't think so :D). Well I understood it OK :-) Alan G. From alan.gauld at freenet.co.uk Wed Jan 12 20:11:06 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 20:10:01 2005 Subject: [Tutor] Is Tkinter the Gui in python References: Message-ID: <024101c4f8da$78ad2a70$16c68651@xp> > Is the standard Tkinter module in python does the GUI - Front -end > Interface for Python Its the library used for building GUI applications. IDLE the Python IDE is built using Tkinter. Is that what you mean? Alan g. From dyoo at hkn.eecs.berkeley.edu Wed Jan 12 20:17:33 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 12 20:17:38 2005 Subject: [Tutor] TypeError I don't understand In-Reply-To: Message-ID: On Wed, 12 Jan 2005, Vincent Wan wrote: > running my program gives me "TypeError: len() of unsized object" Hi Vincent: Ah! Whenever you see something like this, check to see where reassignments to the variable occur: it's possible that one of the assignments isn't doing what you think it's doing. The one that's causing the problem is this: ### living_species = living_species.remove(counter) ### remove() does not return back the modified list: it makes changes directly on the list. Just do: ### living_species.remove(counter) ### and that should correct the problem. A similar bug occurs on the line right above that one: append() also does not return a modified list, but does mutations on the given list instead. You'll need to fix that too, even though you're not getting an immediate error message out of it. Let's go back and look at the error message, so that when you see it, it isn't mysterious. *grin* Now that you know that remove() doesn't return a list, the error message should make more sense, because remove() returns None. ### >>> len(None) Traceback (most recent call last): File "", line 1, in ? TypeError: len() of unsized object ### Best of wishes to you! From ps_python at yahoo.com Wed Jan 12 20:49:08 2005 From: ps_python at yahoo.com (kumar s) Date: Wed Jan 12 20:49:11 2005 Subject: [Tutor] How to create a key-value pairs with alternative elements in a list ... please help. Message-ID: <20050112194908.3899.qmail@web53707.mail.yahoo.com> Dear group, I am frustrated to ask some questions on topics that I thought covered well. my logic is not correct. I have a simple list: >>> a ['a', 'apple', 'b', 'boy', 'c', 'cat'] I want to create a dictionary: dict = {'a':'apple', 'b':'boy', 'c':'cat'} my way of doing this : >>> keys = [] # create a list of all keys i.e a,b,c) >>> vals = [] # create a list of all values i.e #appele,boy,cat etc. >>> dict = {} >>> dict = zip(keys,vals) Problem: How do i capture every alternative element in list a: I am unable to pump the a,b, and c into keys list and apple, boy,cat into vals list. Trial 1: >>> while i >= len(a): print a[i] i = i+2 -- I thought i+2 will give me alternative elements Trial 2: >>> for index,i in enumerate(range(len(a))): print a[i] print a[index+1] a apple apple b b boy boy c c cat cat Please help me. It is also time for me to refer my prev. notes :-( thanks K __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From alan.gauld at freenet.co.uk Wed Jan 12 21:06:23 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 21:06:16 2005 Subject: [Tutor] spaces in print References: Message-ID: <001301c4f8e2$318b0bf0$40b68651@xp> > print '\n','siday_q', key, '.wav'# event > > > i get: > > siday_q 515 .wav > > > how can you eliminate the spaces to get: > > siday_q515.wav Use the os path facilities to join file names etc together, its portable and safer(more reliable). However to join strings without spaces I find it easiest to use the string format operator: print "%s%s%s" % (str1, str, str3) You can also write to sys.stdout if you prefer. HTH Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Wed Jan 12 21:12:39 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 21:12:35 2005 Subject: [Tutor] class instance with identity crisis References: <002001c4f889$fe1fee50$7c00a8c0@frankbruno> <021a01c4f8d9$3827f170$16c68651@xp> Message-ID: <002201c4f8e3$11da99a0$40b68651@xp> Whoops, I forgot to do an assignment... > So try > > def dry(self): return Prune() > > > class Prune: > > def __str__(self): > > return 'prune' > > > > weapon = Damson() > > weapon.dry() weapon = weapon.dry() > > print weapon > > Should work as expected... Alan G From alan.gauld at freenet.co.uk Wed Jan 12 21:28:05 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 12 21:27:55 2005 Subject: [Tutor] How to create a key-value pairs with alternative elementsin a list ... please help. References: <20050112194908.3899.qmail@web53707.mail.yahoo.com> Message-ID: <002d01c4f8e5$392f58e0$40b68651@xp> > >>> a > ['a', 'apple', 'b', 'boy', 'c', 'cat'] > > I want to create a dictionary: > dict = {'a':'apple', > 'b':'boy', > 'c':'cat'} > > > Problem: > How do i capture every alternative element in list a: >From first principles: n = 0 while n < len(a): keys.append(a[n]) vals.append(a[n+1]) n += 2 Or using Python a little more: tups = [] for n in range(0,len(a),2): # step 2 tups.append( (a[n],a[n+1]) ) #list of tuples d = dict(tups) Or using it a bit more: d = dict([(a[n],a[n+1]) for n in range(0,len(a),2)]) For readability's sake I'd use the middle version. HTH Alan G. > > I am unable to pump the a,b, and c into keys list > and apple, boy,cat into vals list. > > Trial 1: > >>> while i >= len(a): > print a[i] > i = i+2 -- I thought i+2 will give me alternative > elements > > Trial 2: > >>> for index,i in enumerate(range(len(a))): > print a[i] > print a[index+1] > > > a > apple > apple > b > b > boy > boy > c > c > cat > cat > > Please help me. It is also time for me to refer my > prev. notes :-( > > thanks > K > > > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - Easier than ever with enhanced search. Learn more. > http://info.mail.yahoo.com/mail_250 > > From jeff at ccvcorp.com Wed Jan 12 21:49:59 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Wed Jan 12 21:44:45 2005 Subject: [Tutor] How to create a key-value pairs with alternative elements in a list ... please help. In-Reply-To: <20050112194908.3899.qmail@web53707.mail.yahoo.com> References: <20050112194908.3899.qmail@web53707.mail.yahoo.com> Message-ID: <41E58D77.9030109@ccvcorp.com> kumar s wrote: > Problem: > How do i capture every alternative element in list a: > > I am unable to pump the a,b, and c into keys list > and apple, boy,cat into vals list. In a sufficiently recent version of Python, you should be able to use an extended slice with a stride -- keys = a[::2] vals = a[1::2] (Note that this is untested, as I don't have a recent version of Python handy at the moment; I'm on 2.2 here, which doesn't have extended slices.) Jeff Shannon Technician/Programmer Credit International From johnp at milwaukielumber.com Wed Jan 12 22:16:27 2005 From: johnp at milwaukielumber.com (John Purser) Date: Wed Jan 12 22:16:32 2005 Subject: [Tutor] Python with MySQL ? In-Reply-To: <20050112030321.GA22999@wdfs.attbi.com> Message-ID: <200501122116.j0CLGRjV030610@mail.morseintranet.com> Great Link. Thanks for the reference! -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of David Rock Sent: Tuesday, January 11, 2005 19:03 To: tutor@python.org Subject: Re: [Tutor] Python with MySQL ? * Danny Yoo [2005-01-11 10:55]: > > On Tue, 11 Jan 2005, Mark Kels wrote: > > > How can I send SQL querys to a MySQL database with a python-CGI program ? > > Hi Mark, > > You'll want to grab a "Python DB API" module for MySQL. The best one I've You might want to check out sqlobject, too http://sqlobject.org/ It gives you a relatively simple way to "objectify" SQL statements. -- David Rock david@graniteweb.com From ps_python at yahoo.com Wed Jan 12 22:26:15 2005 From: ps_python at yahoo.com (kumar s) Date: Wed Jan 12 22:26:17 2005 Subject: [Tutor] How to create a key-value pairs with alternative elements in a list ... please help. In-Reply-To: <41E58D77.9030109@ccvcorp.com> Message-ID: <20050112212615.83500.qmail@web53703.mail.yahoo.com> Thanks for this trick. Can I call this as category thermo-NUKE of list functions. -K --- Jeff Shannon wrote: > kumar s wrote: > > > Problem: > > How do i capture every alternative element in list > a: > > > > I am unable to pump the a,b, and c into keys list > > and apple, boy,cat into vals list. > > In a sufficiently recent version of Python, you > should be able to use > an extended slice with a stride -- > > keys = a[::2] > vals = a[1::2] > > (Note that this is untested, as I don't have a > recent version of > Python handy at the moment; I'm on 2.2 here, which > doesn't have > extended slices.) > > Jeff Shannon > Technician/Programmer > Credit International > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From bds at waywood.co.uk Wed Jan 12 22:49:41 2005 From: bds at waywood.co.uk (Barnaby Scott) Date: Wed Jan 12 22:50:59 2005 Subject: [Tutor] class instance with identity crisis In-Reply-To: <002201c4f8e3$11da99a0$40b68651@xp> Message-ID: <000001c4f8f0$9f4fe7b0$7c00a8c0@frankbruno> Thanks to all who responded. I see my biggest mistake was not spotting the assignment of 'self' being local to the method - very dumb indeed, especially as I have seen this many times before. However I am glad I was not necessarily dumb to want to do what I was thinking of. I am learning lots! Thanks > -----Original Message----- > From: tutor-bounces@python.org [mailto:tutor-bounces@python.org]On > Behalf Of Alan Gauld > Sent: 12 January 2005 20:13 > To: Alan Gauld; Barnaby Scott; 'Tutor' > Subject: Re: [Tutor] class instance with identity crisis > > > Whoops, I forgot to do an assignment... > > > So try > > > > def dry(self): return Prune() > > > > > class Prune: > > > def __str__(self): > > > return 'prune' > > > > > > weapon = Damson() > > > weapon.dry() > > weapon = weapon.dry() > > > > print weapon > > > > Should work as expected... > > Alan G > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From T.Chern at unibas.ch Wed Jan 12 23:05:04 2005 From: T.Chern at unibas.ch (Tzu-Ming Chern) Date: Wed Jan 12 23:05:10 2005 Subject: [Tutor] Matching multiple regular expressions Message-ID: Hi Python Tutors, Is there a more elegant solution to matching multiple regular expressions? An example would be: lista = ['hello','goodbye','bubblegum'] # lista would contain my regular expressions I want to match My file would look something like this: line1: blah blah aldkfslfjlajf hello line2: blah blah dkajfldjfjkdsfalj zippo line3: blah blah lkdsjalkfjkj bubblegum line4: blah blah skdjflsdjlkj ditto What I want to do is to only print out those lines that don't contain those patterns included in lista. So that would be lines 2 and 4. Any suggestions? cheers, tzuming From singingxduck at gmail.com Wed Jan 12 23:26:05 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Wed Jan 12 23:26:21 2005 Subject: [Tutor] flattening a list In-Reply-To: <41E4A0ED.9070502@fastmail.fm> References: <41E4A0ED.9070502@fastmail.fm> Message-ID: <41E5A3FD.4050405@gmail.com> Bill Kranec wrote: > Hello, > > I have a list of lists, for example [ [1,2] , [3,4] ], and I would > like to pass all the elements of that list as arguments to a function > (for example the intersection of all list elements). Is there a > command in regular Python to do this? I would like to avoid the > hassle and speed hit of a loop to extract all the list elements. > > In the past, I believe I have used something like flatten(list), which > was part of Numarray (I think). Am I missing an obvious or clever > solution in regular Python? > > Thanks for your help! > > Bill > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Well, here's something i put together (works for lists and tuples, ignores strings and dicts): ### Start Code ### def flatten(sequence): def rflat(seq2): seq = [] for entry in seq2: if '__contains__' in dir(entry) and \ type(entry) != str and \ type(entry)!=dict: seq.extend([i for i in entry]) else: seq.append(entry) return seq def seqin(sequence): for i in sequence: if '__contains__' in dir(i) and \ type(i) != str and \ type(i) != dict: return True return False seq = sequence[:] while seqin(seq): seq = rflat(seq) return seq ### End Code ### Tested it on a few different scenarios, all performed as expected: >>> flatten([1]) [1] >>> flatten([1,[1]]) [1, 1] >>> flatten(['ab',1]) ['ab', 1] >>> flatten([10,(34,42),54,'abc']) [10, 34, 42, 54, 'abc'] >>> flatten([{'abc':[1,2,3]},[{'a':'1'},{'[1,2]':'ab'}]]) [{'abc': [1, 2, 3]}, {'a': '1'}, {'[1,2]': 'ab'}] Cheers, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From kent37 at tds.net Wed Jan 12 23:29:42 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 12 23:29:49 2005 Subject: [Tutor] Matching multiple regular expressions In-Reply-To: References: Message-ID: <41E5A4D6.3000103@tds.net> Just join the individual strings together with | to make a single regex, then search each line for a match: >>> import re >>> lista = ['hello','goodbye','bubblegum'] >>> regex = re.compile('|'.join(lista)) >>> lines = '''line1: blah blah aldkfslfjlajf hello ... line2: blah blah dkajfldjfjkdsfalj zippo ... line3: blah blah lkdsjalkfjkj bubblegum ... line4: blah blah skdjflsdjlkj ditto''' >>> lines = lines.split('\n') >>> for line in lines: ... if not regex.search(line): ... print line ... line2: blah blah dkajfldjfjkdsfalj zippo line4: blah blah skdjflsdjlkj ditto >>> Kent Tzu-Ming Chern wrote: > Hi Python Tutors, > > Is there a more elegant solution to matching multiple regular expressions? > > An example would be: > > lista = ['hello','goodbye','bubblegum'] # lista would contain my regular > expressions I want to match > > My file would look something like this: > > line1: blah blah aldkfslfjlajf hello > line2: blah blah dkajfldjfjkdsfalj zippo > line3: blah blah lkdsjalkfjkj bubblegum > line4: blah blah skdjflsdjlkj ditto > > What I want to do is to only print out those lines that don't contain > those patterns included in lista. So that would be lines 2 and 4. > > Any suggestions? > > cheers, > tzuming > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From singingxduck at gmail.com Wed Jan 12 23:35:25 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Wed Jan 12 23:35:40 2005 Subject: [Tutor] Matching multiple regular expressions In-Reply-To: <41E5A4D6.3000103@tds.net> References: <41E5A4D6.3000103@tds.net> Message-ID: <41E5A62D.505@gmail.com> Kent Johnson wrote: > Just join the individual strings together with | to make a single > regex, then search each line for a match: > > >>> import re > >>> lista = ['hello','goodbye','bubblegum'] > >>> regex = re.compile('|'.join(lista)) > >>> lines = '''line1: blah blah aldkfslfjlajf hello > ... line2: blah blah dkajfldjfjkdsfalj zippo > ... line3: blah blah lkdsjalkfjkj bubblegum > ... line4: blah blah skdjflsdjlkj ditto''' > >>> lines = lines.split('\n') > >>> for line in lines: > ... if not regex.search(line): > ... print line > ... > line2: blah blah dkajfldjfjkdsfalj zippo > line4: blah blah skdjflsdjlkj ditto > >>> > > Kent > > Tzu-Ming Chern wrote: > >> Hi Python Tutors, >> >> Is there a more elegant solution to matching multiple regular >> expressions? >> An example would be: >> >> lista = ['hello','goodbye','bubblegum'] # lista would contain my >> regular >> expressions I want to match >> >> My file would look something like this: >> >> line1: blah blah aldkfslfjlajf hello >> line2: blah blah dkajfldjfjkdsfalj zippo >> line3: blah blah lkdsjalkfjkj bubblegum >> line4: blah blah skdjflsdjlkj ditto >> >> What I want to do is to only print out those lines that don't contain >> those patterns included in lista. So that would be lines 2 and 4. >> Any suggestions? >> >> cheers, >> tzuming >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > There's also a non-re way available: >>> lista = ['hello','goodbye','bubblegum'] >>> line1 = "blah blah aldkfslfjlajf hello" >>> line2 = "blah blah dkajfldjfjkdsfalj zippo" >>> line3 = "blah blah lkdsjalkfjkj bubblegum" >>> line4 = "blah blah skdjflsdjlkj ditto" >>> filea = [line1, line2, line3, line4] >>> for line in filea: pflag = True for entry in lista: if entry in line: pflag = False break if pflag: print line blah blah dkajfldjfjkdsfalj zippo blah blah skdjflsdjlkj ditto -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From wan at walrus.us Wed Jan 12 23:37:34 2005 From: wan at walrus.us (Vincent Wan) Date: Wed Jan 12 23:37:51 2005 Subject: [Tutor] what's a concise way to print the first elements in a nested list Message-ID: <8D67A4DE-64EA-11D9-9FAC-000393B2D990@walrus.us> If I have a list stuff = [[0,sdfsd,wrtew], [1, rht,erterg]] whats the most concise way to print the first elements: [0, 1] if tried print stuff[:][0] but that just prints the whole list. Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan AT walrus DOT us (change CAPS to @ and . ) From singingxduck at gmail.com Wed Jan 12 23:44:07 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Wed Jan 12 23:44:19 2005 Subject: [Tutor] what's a concise way to print the first elements in a nested list In-Reply-To: <8D67A4DE-64EA-11D9-9FAC-000393B2D990@walrus.us> References: <8D67A4DE-64EA-11D9-9FAC-000393B2D990@walrus.us> Message-ID: <41E5A837.70800@gmail.com> Vincent Wan wrote: > If I have a list stuff = [[0,sdfsd,wrtew], [1, rht,erterg]] whats the > most concise way to print the first elements: > > [0, 1] > > if tried print stuff[:][0] but that just prints the whole list. > > Vincent > > ------------------------------------------------------------------------ > -------------- > PhD Candidate > Committee on the Conceptual and Historical Studies of Science > University of Chicago > > PO Box 73727 > Fairbanks, AK 99707 > > wan AT walrus DOT us (change CAPS to @ and . ) > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] >>> stuff [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] >>> print [stuff[i][0] for i in range(len(stuff))] [0, 1] List comprehensions - cool stuff ;-) -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From cyresse at gmail.com Wed Jan 12 23:48:03 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 12 23:48:07 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: I'm real sorry. My standard disclaimer is now amended to include - "Never post untested code" Disregard everything I wrote above. I feel real bad, so bad that I did something I very rarely do... write complete code. See attached txt file. Guaranteed to work, if you still want to turn Item_4 DAT 5 12:32:05 12:37:13 Funny crackling noise into Item_4 DAT 5 00:00:00 00:05:08 Funny crackling noise Please note comment can be included. Not overly hard. HTH, (And sorry for erroneous datetime stuff. Particularly datetime.datetime(timedelta object) That definitely doesn't work.) Never post untested code Liam Clarke On Wed, 12 Jan 2005 10:08:02 -0500, kevin parks wrote: > thanks everyone... I will look at all the various appraoches folks came > up with and see what i can learnn from them. I ended doing something > lame -- a brute force method. I formmatted and reformatted my input > data and stuffed it in a HUGE dictionary.... it was stupid and > kludgy.... i hope to study all these approaches and learn something > that may help me do something that is more flexible about the input > data and is more elegant..... here's what i > came up with ... with my pea sized brain... > > #!/usr/bin/env python > > # New in version 2.3 is the 'datetime' module (see standard library > reference) > # http://www.python.org/doc/lib/module-datetime.html > > import datetime > > inseqs = { (1) : ['DAT_1', '01', '00:00:23', '00:08:23'], > (2) : ['DAT_1', '02', '00:08:23', '00:09:41'], > (513) : ['DAT_75', '10', '00:59:55', '01:11:05'], > (514) : ['DAT_75', '11', '01:11:05', '01:16:15'], > (515) : ['DAT_75', '12', '01:16:15', '01:34:15'], > (516) : ['DAT_75', '13', '01:34:15', '01:45:15'], > (517) : ['DAT_75', '14', '01:45:15', '01:48:00'] } > > mykeys = inseqs.keys() # first make a copy of the keys > mykeys.sort() # now sort that copy in place > > for key in mykeys: > event = inseqs[key] > print '\n','Item #', key, event > TD = datetime.timedelta > h, m, s = event[2].split(':') > zero_adjust = TD(hours=int(h), minutes=int(m),seconds=int(s)) > # > print ' Q___ ', key, event[:2], ': ', > for item in event[2:]: > hrs, mins, secs, = item.split(':') > time1 = TD(hours=int(hrs), minutes=int(mins),seconds=int(secs)) > print time1 - zero_adjust, > > print > > _______________________________________________ > 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. -------------- next part -------------- # By Liam Clarke # A wee bitty of text processing code. # Written in apology for some very wrong info I gave about using datetime objects. # Golden rule - never post untested code. # # Caveats # # This code will always be expecting input like - # item_1, DAT, 3, 00:00:00, 12:32:00<, optional comment> # Data should be separated by a comma and a space # Of course, you can just change the constant sep to whatever you want. # # While the value of the first 3 values doesn't matter # The fourth and fifth have to be start and finish # and the two time values must be in format HH:MM:SS, 24 hour. # # IMPORTANT - This was written in Notepad (!), so the TABS are all funky. # If you want to copy and paste, detabbify and then retab using proper 4 space tabs. # (And of course, change inpFile and outpFile to appropriate names.) # Python likes forward slashes in paths, they work on all systems. import datetime # init - inpFile is the data to be processed, sep is what separates the values on each line, # in this case a comma followed by a space. # I've read from the file using readlines() instead of read(). This gives a list, each line # being a separate list item. Which makes it easy to step through. def parseTime(timeStr): # I love split and a lot of the other string methods splitTime=timeStr.split(":") return splitTime inpFile = "dump.txt" outpFile = "redumped.txt" sep = ", " openFile = file(inpFile, "r") listOfLines = openFile.readlines() openFile.close() newTimes = [] for line in listOfLines: #Remove superfluous newlines if line.endswith("\n"): line=line.rstrip("\n") #So "a, b, c, d, e" will become ['a', 'b', 'c', 'd', 'e'] splitLine = line.split(sep) #Just assign friendlier names (item, dat, num, start, finish) = ( splitLine[0], splitLine[1], splitLine[2], splitLine[3], splitLine[4] ) if len(splitLine)=5: #Comments are optional, #so if line splits into 6 elements, # there's a comment, so grab it. comment=splitLine[5] else: #As I'll be boolean testing for a comment, it needs a value comment=None splitStart = parseTime(start) splitFinish = parseTime(finish) #Just a wee explanation about datetime # # Datetime.time objects are easier - but.... # They don't support time arithmetic. # Hence the use of datetime.datetime objects. # # datetime objects are initialised like so - # datetime.datetime(year, month, day, hour, minute, second) # Hour is 24 hour. All values must be integers # And day and month cannot be 01 or similar, they must be 1. startDateObj = datetime.datetime(1901, 1, 1, int(splitStart[0]), int(splitStart[1]), int(splitStart[2]) ) finishDateObj = datetime.datetime(1901, 1, 1, int(splitFinish[0]), int(splitFinish[1]), int(splitFinish[2]) ) # Arithmetic on datetime objects returns a timedelta object. # Unfortunately, a timedelta object doesn't have the strftime method # so a new datetime object is needed. diffDeltaObj = finishDateObj - startDateObj deltaStr = str(diffDeltaObj) splitDelta = parseTime(deltaStr) deltaObj = datetime.datetime(1901, 1, 1, int(splitDelta[0]), int(splitDelta[1]), int(splitDelta[2]) ) #So, now we've got a datetime object equivalent to the time difference #between start and finish, so output it as HH:MM:SS endTime = deltaObj.strftime("%H:%M:%S") #Now chuck all back together like we found it, just with changed times lineToWrite = item + sep +dat + sep + num + sep + "00:00:00" + sep + endTime if comment: #If there is a comment, add it too. lineToWrite += sep + comment lineToWrite += '\n' #Just add a newline to keep it organised. newTimes.append(lineToWrite) #Append finished line to list to write when done saveFile = file(outpFile, "w") for item in newTimes: saveFile.write(item) saveFile.close() From jfouhy at paradise.net.nz Wed Jan 12 23:48:20 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Wed Jan 12 23:48:25 2005 Subject: [Tutor] what's a concise way to print the first elements in a nested list In-Reply-To: <8D67A4DE-64EA-11D9-9FAC-000393B2D990@walrus.us> References: <8D67A4DE-64EA-11D9-9FAC-000393B2D990@walrus.us> Message-ID: <1105570100.41e5a9340cf1f@www.paradise.net.nz> Quoting Vincent Wan : > If I have a list stuff = [[0,sdfsd,wrtew], [1, rht,erterg]] whats the > most concise way to print the first elements: > > [0, 1] > > if tried print stuff[:][0] but that just prints the whole list. List comprehension will do it: >>> stuff = [ [ 0, "sdfsd", "wrtew"], [1, "rht", "erterg" ] ] >>> [x[0] for x in stuff] [0, 1] stuff[:] is just a shallow copy of stuff. So stuff[:][0] is exactly the same as stuff[0]. >>> stuff[:][0] is stuff[0] True -- John. From cyresse at gmail.com Wed Jan 12 23:50:13 2005 From: cyresse at gmail.com (Liam Clarke) Date: Wed Jan 12 23:50:38 2005 Subject: [Tutor] Time script help sought! In-Reply-To: References: <56784A22-640D-11D9-A0C2-003065555ABC@mac.com> <4FA30A6F-6413-11D9-A0C2-003065555ABC@mac.com> <003a01c4f821$11eb0280$ef5428cf@JSLAPTOP> Message-ID: And I just noticed an error in my correction code! if len(splitLine) = 5 should be - if len(splitLine) == 6 Gah On Thu, 13 Jan 2005 11:48:03 +1300, Liam Clarke wrote: > I'm real sorry. > > My standard disclaimer is now amended to include - "Never post untested code" > > Disregard everything I wrote above. I feel real bad, so bad that I did > something I very rarely do... write complete code. > > See attached txt file. Guaranteed to work, if you still want to turn > > Item_4 DAT 5 12:32:05 12:37:13 Funny crackling noise > > into Item_4 DAT 5 00:00:00 00:05:08 Funny crackling noise > > Please note comment can be included. Not overly hard. > > HTH, > > (And sorry for erroneous datetime stuff. Particularly > datetime.datetime(timedelta object) > That definitely doesn't work.) > > Never post untested code > > Liam Clarke > > On Wed, 12 Jan 2005 10:08:02 -0500, kevin parks wrote: > > thanks everyone... I will look at all the various appraoches folks came > > up with and see what i can learnn from them. I ended doing something > > lame -- a brute force method. I formmatted and reformatted my input > > data and stuffed it in a HUGE dictionary.... it was stupid and > > kludgy.... i hope to study all these approaches and learn something > > that may help me do something that is more flexible about the input > > data and is more elegant..... here's what i > > came up with ... with my pea sized brain... > > > > #!/usr/bin/env python > > > > # New in version 2.3 is the 'datetime' module (see standard library > > reference) > > # http://www.python.org/doc/lib/module-datetime.html > > > > import datetime > > > > inseqs = { (1) : ['DAT_1', '01', '00:00:23', '00:08:23'], > > (2) : ['DAT_1', '02', '00:08:23', '00:09:41'], > > (513) : ['DAT_75', '10', '00:59:55', '01:11:05'], > > (514) : ['DAT_75', '11', '01:11:05', '01:16:15'], > > (515) : ['DAT_75', '12', '01:16:15', '01:34:15'], > > (516) : ['DAT_75', '13', '01:34:15', '01:45:15'], > > (517) : ['DAT_75', '14', '01:45:15', '01:48:00'] } > > > > mykeys = inseqs.keys() # first make a copy of the keys > > mykeys.sort() # now sort that copy in place > > > > for key in mykeys: > > event = inseqs[key] > > print '\n','Item #', key, event > > TD = datetime.timedelta > > h, m, s = event[2].split(':') > > zero_adjust = TD(hours=int(h), minutes=int(m),seconds=int(s)) > > # > > print ' Q___ ', key, event[:2], ': ', > > for item in event[2:]: > > hrs, mins, secs, = item.split(':') > > time1 = TD(hours=int(hrs), minutes=int(mins),seconds=int(secs)) > > print time1 - zero_adjust, > > > > print > > > > _______________________________________________ > > 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. > > > -- '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. From bill at celestial.net Wed Jan 12 23:52:50 2005 From: bill at celestial.net (Bill Campbell) Date: Wed Jan 12 23:52:56 2005 Subject: [Tutor] Matching multiple regular expressions In-Reply-To: References: Message-ID: <20050112225250.GA38707@alexis.mi.celestial.com> On Wed, Jan 12, 2005, Tzu-Ming Chern wrote: > >Hi Python Tutors, > >Is there a more elegant solution to matching multiple regular expressions? > >An example would be: > >lista = ['hello','goodbye','bubblegum'] # lista would contain my regular >expressions I want to match > >My file would look something like this: > >line1: blah blah aldkfslfjlajf hello >line2: blah blah dkajfldjfjkdsfalj zippo >line3: blah blah lkdsjalkfjkj bubblegum >line4: blah blah skdjflsdjlkj ditto > >What I want to do is to only print out those lines that don't contain >those patterns included in lista. So that would be lines 2 and 4. > >Any suggestions? If the list of regular expressions is at all large, or you want a general solution, something like this might be appropriate: import re relist = ( re.compile(r'pattern1'), re.compile(r'pattern2'), ... ) fh = open('somefile') for line in fh.xreadlines(): for regex in relist: if regex.search(line): break else: print line Bill -- INTERNET: bill@Celestial.COM Bill Campbell; Celestial Software LLC UUCP: camco!bill PO Box 820; 6641 E. Mercer Way FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 http://www.celestial.com/ "I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forego their use." -- Galileo Galilei From mario_rol at hotmail.com Thu Jan 13 00:08:23 2005 From: mario_rol at hotmail.com (Mario Rol) Date: Thu Jan 13 00:10:23 2005 Subject: [Tutor] flattening a list Message-ID: nice and concise, found on comp.lang.python: def flatten(a): if not isinstance(a,(tuple,list)): return [a] if len(a)==0: return [] return flatten(a[0])+flatten(a[1:]) _________________________________________________________________ Play online games with your friends with MSN Messenger http://messenger.msn.nl/ From cyresse at gmail.com Thu Jan 13 00:30:24 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 13 00:30:28 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: <41E51E9E.7090902@tds.net> References: <578DC866-6429-11D9-8989-000393CBC88E@yahoo.fr> <7d7029e7050111164947f4908f@mail.gmail.com> <01c201c4f882$5563d760$16c68651@xp> <7d7029e705011200505164be17@mail.gmail.com> <41E51E9E.7090902@tds.net> Message-ID: XML, XPath, XML Schema all have basic tutorials at www.w3schools.org Out of curiosity, how does a node function in a DOM? I'm not very good at manipulating DOM's, I can do the basics, mainly by passing object IDs to JS functions, which feels like cheating. On Wed, 12 Jan 2005 07:57:02 -0500, Kent Johnson wrote: > Guillermo Fernandez Castellanos wrote: > > I have not been able to find any recent XML/Python tutorial on the > > web. > > There is a Python and XML topic guide at python.org > http://pyxml.sourceforge.net/topics/ > > Uche Ogbuji writes regularly about Python and XML for xml.com > http://www.xml.com/pub/au/84 > > Does the xml.dom library have a XPath and XQuery or any SQL-like > > support? I've understood that it's a pretty basic library... > > My impression is the built-in stuff is pretty basic. If you want to use SAX and DOM for XML in > Python, the 4Suite add-on is pretty standard. > http://4suite.org/index.xhtml > > I've also heard good things about libxml2; it includes XPath support > http://xmlsoft.org/python.html > > IMO Fredrik Lundh's ElementTree package is the easiest way to work with XML from Python. It supports > a subset of XPath and he has just released a compiled C version > http://effbot.org/zone/element-index.htm > > Kent > > > > > Thanks, > > > > G > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > 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. From cyresse at gmail.com Thu Jan 13 00:32:12 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 13 00:32:15 2005 Subject: [Tutor] Is Tkinter the Gui in python In-Reply-To: <024101c4f8da$78ad2a70$16c68651@xp> References: <024101c4f8da$78ad2a70$16c68651@xp> Message-ID: I believe Tkinter + Tix come with your Python install. wxPython is another popular GUI, but it is 3rd party. On Wed, 12 Jan 2005 19:11:06 -0000, Alan Gauld wrote: > > Is the standard Tkinter module in python does the GUI - Front -end > > Interface for Python > > Its the library used for building GUI applications. > IDLE the Python IDE is built using Tkinter. > > Is that what you mean? > > Alan g. > > _______________________________________________ > 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. From cyresse at gmail.com Thu Jan 13 00:37:22 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 13 00:37:33 2005 Subject: [Tutor] List comprehensions Message-ID: Hi, Am I able to achieve something like this - def foobar(); # stuff return x=[1,....1000] for j=foobar(item) for item in x: As a comprehension, if you get what I mean... Can I build a for loop with a function in for x in x part? Ack. Having difficulty summing it up properly. -- '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. From joerihonnef at gmail.com Thu Jan 13 00:38:21 2005 From: joerihonnef at gmail.com (joeri honnef) Date: Thu Jan 13 00:38:25 2005 Subject: [Tutor] communication between java and python? Message-ID: Dear Tutor, I'm trying to communicate between Python and Java and using os.popen(). But thing dont work...The Java program reads strings from stdin and the python program just writes to stdout. -first call the java program using: handlers = os.popen('java StdIn) -then: handlers[0].write('string') -and: return_value_from_java = handlers[1].read() this does not work on WindowsXP using python 2.34 and the attached Java program. thanks, JH -------------- next part -------------- A non-text attachment was scrubbed... Name: StdIn.java Type: application/octet-stream Size: 3396 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050113/937052de/StdIn.obj From jfouhy at paradise.net.nz Thu Jan 13 00:47:24 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Thu Jan 13 00:47:28 2005 Subject: [Tutor] List comprehensions In-Reply-To: References: Message-ID: <1105573644.41e5b70c8d4dd@www.paradise.net.nz> Quoting Liam Clarke : > As a comprehension, if you get what I mean... Can I build a for loop > with a function in for x in x part? > > Ack. Having difficulty summing it up properly. I'm not sure exactly what you mean, so my answer is "probably". >>> from math import sin >>> arr = range(10) >>> arr [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [sin(float(x)/10) for x in arr] [0.0, 0.099833416646828155, 0.19866933079506122, 0.29552020666133955, 0.38941834230865052, 0.47942553860420301, 0.56464247339503537, 0.64421768723769102, 0.71735609089952279, 0.78332690962748341] >>> def biggerThanHalf(x): ... return x > 0.5 ... >>> [biggerThanHalf(sin(float(x)/10)) for x in arr] [False, False, False, False, False, False, True, True, True, True] >>> [biggerThanHalf(sin(float(x)/10)) for x in map(lambda y: 2*y + 3, arr)] [False, False, True, True, True, True, True, True, True, True] Hopefully one of these examples answers your question :-) -- John. From kent37 at tds.net Thu Jan 13 00:48:39 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 13 00:48:45 2005 Subject: [Tutor] List comprehensions In-Reply-To: References: Message-ID: <41E5B757.30903@tds.net> If you mean for j to be a list of foobar(item) then use j=[foobar(item) for item in x] The first part of the list comp can be any valid expression. Kent Liam Clarke wrote: > Hi, > > Am I able to achieve something like this - > > def foobar(); > # stuff > return > > x=[1,....1000] > > for j=foobar(item) for item in x: > > As a comprehension, if you get what I mean... Can I build a for loop > with a function in for x in x part? > > Ack. Having difficulty summing it up properly. > > > From kent37 at tds.net Thu Jan 13 01:03:59 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 13 01:04:06 2005 Subject: [Tutor] More and more OT - Python/Java In-Reply-To: References: <15E0C70A-6367-11D9-8A9A-000393CBC88E@yahoo.fr> <41E32E0E.7090802@tds.net> <2D2F0EDA-6375-11D9-8A9A-000393CBC88E@yahoo.fr> <321086EC-6377-11D9-8A9A-000393CBC88E@yahoo.fr> Message-ID: <41E5BAEF.1030904@tds.net> Liam Clarke wrote: > Out of curiousity, having poked around XML while learning about the > JScript DOM, what are you using it for? Lots...one project I worked on, we used XML as the native file format and dom4j trees as the object model. In other words, when you saved a document, you got an XML file; when you read a document, it was stored internally as a dom4j tree. (The program was written in Jython so it used Java libraries.) The project was basically a domain-specific editor. Using XML and dom4j worked out great. One thing I really like is that when the domain model is a dom model, you can use XPath as a query language on your model. Big chunks of the application were data-driven and most of the data tables used XPath to describe the piece of the model they needed. I have written quite a bit about this project on my blog - start at www.kentsjohnson.com and look for blog entries and essays about dom4j, xpath and Meccano (the name of the project). Another project I worked on had to manipulate a tree structure stored in a database. I built a dom4j tree in memory to represent the database tree and did my analysis on that. Very handy. Kent From cyresse at gmail.com Thu Jan 13 01:42:30 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 13 01:42:33 2005 Subject: [Tutor] List comprehensions In-Reply-To: <41E5B757.30903@tds.net> References: <41E5B757.30903@tds.net> Message-ID: Aah, thank you both - I knew there was a way to do it. Regards, Liam Clarke PS John - another Kiwi, seems to be a lot of NZers involved in Python & Python projects. Is it the No. 8 wire freedom of Python? ; ) On Wed, 12 Jan 2005 18:48:39 -0500, Kent Johnson wrote: > If you mean for j to be a list of foobar(item) then use > j=[foobar(item) for item in x] > > The first part of the list comp can be any valid expression. > > Kent > > Liam Clarke wrote: > > Hi, > > > > Am I able to achieve something like this - > > > > def foobar(); > > # stuff > > return > > > > x=[1,....1000] > > > > for j=foobar(item) for item in x: > > > > As a comprehension, if you get what I mean... Can I build a for loop > > with a function in for x in x part? > > > > Ack. Having difficulty summing it up properly. > > > > > > > _______________________________________________ > 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. From marilyn at deliberate.com Thu Jan 13 02:01:26 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Thu Jan 13 02:05:04 2005 Subject: [Tutor] sockets, files, threads Message-ID: Hello Tutors, I've been banging my head against this bug for 4 days. I can't think of how to ask for help except to give a birds eye view of the situation. I have a python daemon that is listening on a UNIX socket for communication with exim, my Mail Transfer Agent. Each mail message has its own exim process. The exim process speaks to my daemon, who starts a thread which reads everything there is on the socket, and sends and 'OK' to send exim on its way. That exim process finishes quickly because that 'OK' means to dump the message, I am taking responsibility for it from here. When stuff was read from the exim socket, it was stored in a tempfile, so that I could release the exim process, then I lseek to the front of the tempfile and have it handy. I see from all my debugging and logging that the file descriptor for this tempfile is 9. The program then opens a pipe to exim to send mail. I see that the popen2.popen3 call returns 9 for the stdin file descriptor for the pipe. The tempfile (also file descriptor 9) is read for piping into exim and it errors with "Bad file descriptor". Worse yet, the first 5 messages of my test go through the entire process without a problem, and then # 6 hits this -- but only if # 1 message is really big. I'm at a loss. Does anyone have an idea? Marilyn Davis From bgailer at alum.rpi.edu Thu Jan 13 02:13:13 2005 From: bgailer at alum.rpi.edu (Bob Gailer) Date: Thu Jan 13 02:08:57 2005 Subject: [Tutor] List comprehensions In-Reply-To: <41E5B757.30903@tds.net> References: <41E5B757.30903@tds.net> Message-ID: <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> At 04:48 PM 1/12/2005, Kent Johnson wrote: >If you mean for j to be a list of foobar(item) then use >j=[foobar(item) for item in x] > >The first part of the list comp can be any valid expression. Does that mean that there are invalid expressions? I'd enjoy seeing an example. Bob Gailer mailto:bgailer@alum.rpi.edu 303 442 2625 home 720 938 2625 cell From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 02:13:08 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 02:13:17 2005 Subject: [Tutor] what's a concise way to print the first elements in a nested list In-Reply-To: <41E5A837.70800@gmail.com> Message-ID: On Wed, 12 Jan 2005, Orri Ganel wrote: > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] > >>> stuff > [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] > >>> print [stuff[i][0] for i in range(len(stuff))] > [0, 1] Hi Orri, An alternative way to write this is: ### print [row[0] for row in stuff] ### which extracts the first element out of every "row" sublist in 'stuff'. Best of wishes! From singingxduck at gmail.com Thu Jan 13 02:19:11 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Thu Jan 13 02:19:27 2005 Subject: [Tutor] what's a concise way to print the first elements in a nested list In-Reply-To: References: Message-ID: <41E5CC8F.10604@gmail.com> Danny Yoo wrote: >On Wed, 12 Jan 2005, Orri Ganel wrote: > > > >> >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] >> >>> stuff >>[[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] >> >>> print [stuff[i][0] for i in range(len(stuff))] >>[0, 1] >> >> > > >Hi Orri, > >An alternative way to write this is: > >### >print [row[0] for row in stuff] >### > >which extracts the first element out of every "row" sublist in 'stuff'. > > >Best of wishes! > > > > True. The only difference is the use of "for i in range(len(...))" instead of "for i in ...". I suppose either works, but since Python only allows you to edit the elements of a sequence using the "for i in ..." notation (and have the changes stick) inside of a list comprehension (and perhaps a generator, as well, I don't know), I try to stay away from it so I don't get confused when it doesn't work outside of one. Cheers, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/744d72d7/attachment.htm From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 02:32:07 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 02:32:12 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Wed, 12 Jan 2005, Marilyn Davis wrote: > When stuff was read from the exim socket, it was stored in a tempfile, > so that I could release the exim process, then I lseek to the front of > the tempfile and have it handy. I see from all my debugging and logging > that the file descriptor for this tempfile is 9. Hi Marilyn, Question: do you really need to use a tempfile? If your daemon is persistent in memory, can it just write to a StringIO object? StringIO.StringIO elements act like files too, and may be easier to maintain than a tempfile.TemporaryFile. If you can show us how you're constructing and using the TemporaryFile, we can try to trace any problematic usage. > The program then opens a pipe to exim to send mail. I see that the > popen2.popen3 call returns 9 for the stdin file descriptor for the pipe. > > The tempfile (also file descriptor 9) is read for piping into exim and > it errors with "Bad file descriptor". Oh! This situation sounds like the 'tempfile' is being closed at some point, releasing the file descriptor back to the system. If that is what is happening, then that's why the pipe has the same descriptor id: the call to pipe() just reuses a free file descriptor. I'd look for places where the tempfile might be close()d. I'd also look for places where your reference to tempfile is reassigned, since that would also signal a resource collection. > Worse yet, the first 5 messages of my test go through the entire process > without a problem, and then # 6 hits this -- but only if # 1 message is > really big. Just double checking something: are you dealing with threads? Best of wishes to you! From keridee at jayco.net Thu Jan 13 01:40:13 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 13 02:35:14 2005 Subject: [Tutor] spaces in print References: Message-ID: <003101c4f910$26f77100$845328cf@JSLAPTOP> Don't forget the string method join()! print "".join(['\n','siday_q',key,'.wav']) It works too ;-) (though I like the string formatting better for this.) Jacob > > > when i print: > > print '\n','siday_q', key, '.wav'# event > > > i get: > > siday_q 515 .wav > > > how can you eliminate the spaces to get: > > siday_q515.wav > > > ? > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Thu Jan 13 02:33:59 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 13 02:35:19 2005 Subject: [Tutor] How to create a key-value pairs with alternative elementsin a list ... please help. References: <20050112194908.3899.qmail@web53707.mail.yahoo.com> Message-ID: <003301c4f910$2b2207e0$845328cf@JSLAPTOP> > Problem: > How do i capture every alternative element in list a: Use Jeff Shannon's approach to get keys and vals. > >>> keys = [] # create a list of all keys i.e a,b,c) > >>> vals = [] # create a list of all values i.e > #appele,boy,cat etc. > > >>> dict = {} > > >>> dict = zip(keys,vals) What is that?!? your dict thing ain't workin' This is verified code... HA,HA,HA,Ha, ha, ha Well... okay, it's only tested, not verified. fulllist = ['a','apple','b','boy','c','cat'] keys = fulllist[::2] vals = fulllist[1::2] dictionary = dict(zip(keys,vals)) HTH, Jacob From keridee at jayco.net Thu Jan 13 02:17:52 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 13 02:35:21 2005 Subject: [Tutor] TypeError I don't understand References: Message-ID: <003201c4f910$29f68030$845328cf@JSLAPTOP> Danny Yoo solved the problem, but I would like to suggest something. target = '*' + str(living_species[counter]) + '*' replacement = '(' + target + ',*' + str(current_species) + '*)' change these lines to target = "*%s*" % (living_species[counter]) replacement = "(%s*%s*)" % (target,current_species) It will (obviously) work your way, but I think that string formatting simplifies and improves readability, IMHO. HTH, Jacob Schmidt > Dear All, > > python gives me a type error that I don't understand > > running my program gives me "TypeError: len() of unsized object" > but I know I that the object whose length I asked for is a list and > therefor a sized type > > here is a sample of the output: > > type (living_species) : > len(living_species) : 1 > Traceback (most recent call last): > File "new_prog1b.txt", line 15, in ? > while counter < len(living_species): > TypeError: len() of unsized object > > here is the code: > > import random > > living_species = [0] > dead_species = [] > tree = "*0*" > > current_time = 0 > end_of_time = 25 > current_species = 0 > branching_probablity = .1 > extinction_probablity = .1 > > while current_time < end_of_time: > counter = 0 > while counter < len(living_species): > if random.random() < branching_probablity: > current_species += 1 > living_species.append(current_species) > target = '*' + str(living_species[counter]) + '*' > replacement = '(' + target + ',*' + str(current_species) + > '*)' > tree = tree.replace(target, replacement) > counter += 1 > # PROBLEM HERE > print 'type (living_species) :',type(living_species) # proves > living_species is a list > print 'len(living_species) :', len(living_species) # proves > length living_species is a number > counter = len(living_species) - 1 # python says TypeError: len() > of unsized object > while counter >- 1: > if random.random() < extinction_probablity: > dead_species = dead_species.append(counter) > living_species = living_species.remove(counter) > counter -= 1 > current_time += 1 > > print living_species > print dead_species > tree = tree.replace('*','') > print tree > > Thank You, > > Vincent > > ------------------------------------------------------------------------ > -------------- > PhD Candidate > Committee on the Conceptual and Historical Studies of Science > University of Chicago > > PO Box 73727 > Fairbanks, AK 99707 > > wanATwalrusDOTus (replace AT with "@" and dot with ".") > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 02:42:14 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 02:42:17 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: > Just double checking something: are you dealing with threads? Hi Marilyn, Argh, that was a dumb question. Pretend I didn't ask it that way. *grin* I meant to ask: How do you deal with threads? Is the temporary file a global resource that the threads all touch? If so, have you done any synchronization to make sure that at most one thread can touch the temporary file at a time? What are the shared resources for the threads? The situation you mentioned, > > Worse yet, the first 5 messages of my test go through the entire > > process without a problem, and then # 6 hits this -- but only if # 1 > > message is really big. is exactly the sort of thing I'd expect if two threads were contending for the same resource, so let's see if the bug has to do with this. Best of wishes to you! From maxnoel_fr at yahoo.fr Thu Jan 13 02:41:42 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 13 02:44:32 2005 Subject: [Tutor] List comprehensions In-Reply-To: <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> Message-ID: <4692AFB0-6504-11D9-BB2B-000393CBC88E@yahoo.fr> On Jan 13, 2005, at 01:13, Bob Gailer wrote: > At 04:48 PM 1/12/2005, Kent Johnson wrote: >> If you mean for j to be a list of foobar(item) then use >> j=[foobar(item) for item in x] >> >> The first part of the list comp can be any valid expression. > > Does that mean that there are invalid expressions? I'd enjoy seeing an > example. Here's an obvious one: j = [foobar(item)/0 for item in x] -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Thu Jan 13 03:05:24 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 13 03:05:29 2005 Subject: [Tutor] List comprehensions In-Reply-To: <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> Message-ID: <41E5D764.7080008@tds.net> I suppose if it's an expression, it must be valid, eh? Otherwise it's something else. Bob Gailer wrote: > At 04:48 PM 1/12/2005, Kent Johnson wrote: > >> If you mean for j to be a list of foobar(item) then use >> j=[foobar(item) for item in x] >> >> The first part of the list comp can be any valid expression. > > > Does that mean that there are invalid expressions? I'd enjoy seeing an > example. > > Bob Gailer > mailto:bgailer@alum.rpi.edu > 303 442 2625 home > 720 938 2625 cell > From marilyn at deliberate.com Thu Jan 13 03:14:22 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Thu Jan 13 03:18:00 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Wed, 12 Jan 2005, Danny Yoo wrote: Thank you so much for thinking about this Danny. > > When stuff was read from the exim socket, it was stored in a tempfile, > > so that I could release the exim process, then I lseek to the front of > > the tempfile and have it handy. I see from all my debugging and logging > > that the file descriptor for this tempfile is 9. > > Hi Marilyn, > > Question: do you really need to use a tempfile? If your daemon is > persistent in memory, can it just write to a StringIO object? Mail messages can be really big. I was thinking that I'd be better off if I put them on the disk. Am I wrong? Are we suspecting tempfile? Might it have a shared resource? > StringIO.StringIO elements act like files too, and may be easier to > maintain than a tempfile.TemporaryFile. If you can show us how you're > constructing and using the TemporaryFile, we can try to trace any > problematic usage. OK. I'll try to cut-and-paste together something tomorrow. I have to quit now. > > The program then opens a pipe to exim to send mail. I see that the > > popen2.popen3 call returns 9 for the stdin file descriptor for the pipe. > > > > The tempfile (also file descriptor 9) is read for piping into exim and > > it errors with "Bad file descriptor". > > Oh! This situation sounds like the 'tempfile' is being closed at some > point, releasing the file descriptor back to the system. If that is what > is happening, then that's why the pipe has the same descriptor id: the > call to pipe() just reuses a free file descriptor. Yes. So in my logfile, I log every creation, opening and closing of everything and every write and read and seek. And I just don't see it. > > I'd look for places where the tempfile might be close()d. I'd also look > for places where your reference to tempfile is reassigned, since that Hmmmm. I'll look for reassignments. This is likely. You mean the tempfile would think its time to close if it got reassigned? > would also signal a resource collection. > > > How do you deal with threads? Is the temporary file a global resource > that the threads all touch? If so, have you done any synchronization to No. Only the thread that creates it reads it. > make sure that at most one thread can touch the temporary file at a time? > What are the shared resources for the threads? I can't think of any -- unless there are hidden things in os.popen? or tempfile.mkstemp or open. They do share a logfile. But each entry opens it, appends it, and closes it. My threads seem to me to be very independent. > > > The situation you mentioned, > > > > Worse yet, the first 5 messages of my test go through the entire > > > process without a problem, and then # 6 hits this -- but only if # 1 > > > message is really big. > > is exactly the sort of thing I'd expect if two threads were contending for > the same resource, so let's see if the bug has to do with this. "Resource". That's a file, a socket, a pipe, what else? Well, I'll sleep and do some code review with your questions in mind. > > Best of wishes to you! Thank you! I was looking at my use of file objects and file descriptors and I wrote this sample program and was very surprised by the result -- which makes me think there's something here that I don't understand. Where did my 'ooo' go? #! /usr/bin/env python import os fobj = open('/tmp/xxx','w') fobj.write('ooo\n') fp = fobj.fileno() os.write(fp,'x\n') os.close(fp) ########## # OUTPUT: [root@maildance tmp]# ./file.py [root@maildance tmp]# cat xxx x [root@maildance tmp]# Thank you so much for your questions and thoughts. We'll see what the morning brings. Marilyn From flaxeater at yahoo.com Thu Jan 13 03:28:27 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 13 03:28:32 2005 Subject: [Tutor] flattening a list Message-ID: <20050113022828.39088.qmail@web54310.mail.yahoo.com> The only problem with this if it is to big or to deeply nested then it will overflow the stack? Mario Rol wrote: > nice and concise, found on comp.lang.python: > > def flatten(a): > if not isinstance(a,(tuple,list)): return [a] > if len(a)==0: return [] > return flatten(a[0])+flatten(a[1:]) > > _________________________________________________________________ > Play online games with your friends with MSN Messenger > http://messenger.msn.nl/ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > __________________________________ Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less. http://info.mail.yahoo.com/mail_250 From bgailer at alum.rpi.edu Thu Jan 13 05:13:09 2005 From: bgailer at alum.rpi.edu (Bob Gailer) Date: Thu Jan 13 05:08:56 2005 Subject: [Tutor] List comprehensions In-Reply-To: <4692AFB0-6504-11D9-BB2B-000393CBC88E@yahoo.fr> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> <4692AFB0-6504-11D9-BB2B-000393CBC88E@yahoo.fr> Message-ID: <6.1.2.0.0.20050112211009.03a3c478@mail.mric.net> At 07:05 PM 1/12/2005, Kent Johnson wrote: >I suppose if it's an expression, it must be valid, eh? Otherwise it's >something else. At 06:41 PM 1/12/2005, Max Noel wrote: >On Jan 13, 2005, at 01:13, Bob Gailer wrote: > >>At 04:48 PM 1/12/2005, Kent Johnson wrote: >>>If you mean for j to be a list of foobar(item) then use >>>j=[foobar(item) for item in x] >>> >>>The first part of the list comp can be any valid expression. >> >>Does that mean that there are invalid expressions? I'd enjoy seeing an >>example. > >Here's an obvious one: > >j = [foobar(item)/0 for item in x] I like Kent's response. foobar(item)/0 is a "valid" expression. It fits the grammar of expressions. The fact that it raises an exception does not make it an invalid expression. Consider foobar(item)/xyz. It is valid. If xyz == 0 then it will also raise an exception. Bob Gailer mailto:bgailer@alum.rpi.edu 303 442 2625 home 720 938 2625 cell From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 06:29:52 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 06:29:58 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Wed, 12 Jan 2005, Marilyn Davis wrote: > I was looking at my use of file objects and file descriptors and I wrote > this sample program and was very surprised by the result -- which makes > me think there's something here that I don't understand. Where did my > 'ooo' go? > > #! /usr/bin/env python > import os > > fobj = open('/tmp/xxx','w') > fobj.write('ooo\n') > fp = fobj.fileno() > os.write(fp,'x\n') > os.close(fp) Hi Marilyn, Oh! Can you explain why you're mixing the low-level 'os.write()' and 'os.close()' stuff with the high-level file methods? The 'os' functions work at a different level of abstraction than the file object methods, so there's no guarantee that: os.close(fp) will do the proper flushing of the file object's internal character buffers. Try this instead: ### fobj = open('/tmp/xxx','w') fobj.write('ooo\n') fobj.write('x\n') fobj.close() ### The documentation on os.write() says: """Note: This function is intended for low-level I/O and must be applied to a file descriptor as returned by open() or pipe(). To write a ``file object'' returned by the built-in function open() or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method.""" (http://www.python.org/doc/lib/os-fd-ops.html#l2h-1555) I think the documentation is trying to say: "don't mix high-level and low-level IO". For most purposes, we can usually avoid using the low-level IO functions os.open() and os.write(). If we're using the low-level file functions because of pipes, then we can actually turn pipes into file-like objects by using os.fdopen(). os.fdopen() is a bridge that transforms file descriptors into file-like objects. See: http://www.python.org/doc/lib/os-newstreams.html for more information on os.fdopen(). I hope this helps! From wan at walrus.us Thu Jan 13 06:59:20 2005 From: wan at walrus.us (Vincent Wan) Date: Thu Jan 13 06:59:27 2005 Subject: [Tutor] sorry, another update bug Message-ID: <4420DB5C-6528-11D9-99A3-000393B2D990@walrus.us> sorry to inflict another dumb bug on you all so soon... I have written a program to replicated simulate the evolution of species and their genomes based on David Raup and Stephen Jay Gould's 1974 paper in systematic zoology 23: 305-322. The program correctly evolves species and keeps track of relationships. The genome of the species is mutated correctly (shown by the debug write lines) but all species in the genomes in the living_species are updated to the last mutated genome. sample output: parent species genome: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] species 1 genome after mutation: [1, 1, 1, 1, 0, 1, 1, 0, 1, 1] parent species genome: [1, 1, 1, 1, 0, 1, 1, 0, 1, 1] species 2 genome after mutation: [2, 1, 2, 1, 1, 1, 1, 0, 2, 2] parent species genome: [2, 1, 2, 1, 1, 1, 1, 0, 2, 2] species 3 genome after mutation: [2, 2, 3, 1, 2, 1, 2, 0, 3, 2] parent species genome: [2, 2, 3, 1, 2, 1, 2, 0, 3, 2] species 4 genome after mutation: [2, 2, 3, 2, 3, 1, 3, 0, 4, 2] parent species genome: [2, 2, 3, 2, 3, 1, 3, 0, 4, 2] species 2 genome after mutation: [2, 2, 3, 2, 3, 2, 3, 1, 5, 3] parent species genome: [2, 2, 3, 2, 3, 2, 3, 1, 5, 3] species 2 genome after mutation: [2, 3, 3, 2, 3, 3, 3, 2, 6, 4] parent species genome: [2, 3, 3, 2, 3, 3, 3, 2, 6, 4] species 2 genome after mutation: [2, 3, 3, 2, 4, 4, 3, 3, 6, 5] parent species genome: [2, 3, 3, 2, 4, 4, 3, 3, 6, 5] species 3 genome after mutation: [2, 4, 4, 2, 5, 4, 3, 4, 6, 5] parent species genome: [2, 4, 4, 2, 5, 4, 3, 4, 6, 5] species 4 genome after mutation: [3, 4, 4, 2, 5, 4, 4, 4, 7, 5] parent species genome: [3, 4, 4, 2, 5, 4, 4, 4, 7, 5] species 4 genome after mutation: [3, 4, 5, 2, 5, 5, 4, 4, 8, 6] parent species genome: [3, 4, 5, 2, 5, 5, 4, 4, 8, 6] species 4 genome after mutation: [3, 5, 6, 2, 5, 6, 5, 4, 9, 7] parent species genome: [3, 5, 6, 2, 5, 6, 5, 4, 9, 7] species 5 genome after mutation: [3, 6, 6, 2, 5, 7, 6, 5, 10, 7] parent species genome: [3, 6, 6, 2, 5, 7, 6, 5, 10, 7] species 5 genome after mutation: [4, 7, 7, 3, 6, 7, 7, 6, 11, 7] parent species genome: [4, 7, 7, 3, 6, 7, 7, 6, 11, 7] species 6 genome after mutation: [4, 8, 8, 4, 6, 8, 8, 6, 11, 8] parent species genome: [4, 8, 8, 4, 6, 8, 8, 6, 11, 8] species 7 genome after mutation: [4, 9, 8, 5, 7, 8, 8, 6, 12, 8] parent species genome: [4, 9, 8, 5, 7, 8, 8, 6, 12, 8] species 6 genome after mutation: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] 15 species evolved living_species: species: 6 born time: 18 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 8 born time: 29 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 10 born time: 31 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 14 born time: 39 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 15 born time: 41 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 16 born time: 43 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] dead_species: species: 4 born time: 3 died time: 5 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 3 born time: 1 died time: 8 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 2 born time: 1 died time: 9 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 1 born time: 1 died time: 17 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 5 born time: 13 died time: 20 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 0 born time: 0 died time: 30 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 7 born time: 27 died time: 32 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 9 born time: 30 died time: 36 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 12 born time: 36 died time: 41 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 11 born time: 33 died time: 42 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] species: 13 born time: 37 died time: 43 genome: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] tree: (((0,((6,9),((7,((10,((13,16),14)),(11,(12,15)))),8))),4),((1,5),(2,3))) code: import random debug = 1 living_species = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]] dead_species = [] tree = "*0*" current_time = 0 current_species_counter = 0 max_species = 16 # the number of species to be evolved + 1 branching_probablity = .1 extinction_probablity = .1 mutation_rate = .5 def mutate_genome(a_genome, rate): "For each base in a genome there is a rate chance it is set to 1" current_base = 0 while current_base < len(a_genome): if random.random() <= rate: a_genome[current_base] = a_genome[current_base]+ 1 current_base += 1 return a_genome while current_species_counter < max_species: counter = 0 while counter < len(living_species): if random.random() < branching_probablity: current_species_counter += 1 current_species = [current_species_counter, current_time, 0, living_species[counter][3]] if debug: print 'parent species genome:', living_species[counter][3] living_species.append(current_species) living_species[len(living_species)-1][3] = mutate_genome(current_species[3], mutation_rate) if debug: print 'species ', len(living_species)-1, 'genome after mutation:', living_species[len(living_species)-1][3] # updates species tree target = '*' + str(living_species[counter][0]) + '*' replacement = '(' + target + ',*' + str(current_species[0]) + '*)' tree = tree.replace(target, replacement) counter += 1 # if debug: print 'at time ', current_time, ' living_species: ', [row[0] for row in living_species] print 'at time ', current_time, ' living_species: ', living_species counter = 0 while counter < len(living_species): if random.random() < extinction_probablity: newly_dead = living_species[counter] newly_dead[2] = current_time dead_species.append(newly_dead) living_species.remove(living_species[counter]) if len(living_species) == 0: # when the last living_species goes extinct exit break counter += 1 # if debug: print 'at time ', current_time, ' dead_species : ', [row[0] for row in dead_species] if len(living_species) == 0: # when the last living_species goes extinct exit break current_time += 1 if current_species_counter < max_species: print '\nall extinct with only ', current_species_counter + 1, ' species of ', max_species else: print max_species - 1, ' species evolved' print 'living_species:' for each in living_species: print 'species:', each[0], 'born time: ', each[1], 'died time: ', each[2] print 'genome:', each[3] print '\ndead_species: ' for each in dead_species: print 'species:', each[0], 'born time: ', each[1], 'died time: ', each[2] print 'genome:', each[3] tree = tree.replace('*','') print 'tree: ', tree Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan AT walrus DOT us (change CAPS to @ and . ) From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 07:15:24 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 07:15:35 2005 Subject: [Tutor] flattening a list In-Reply-To: <20050113022828.39088.qmail@web54310.mail.yahoo.com> Message-ID: > > def flatten(a): > > if not isinstance(a,(tuple,list)): return [a] > > if len(a)==0: return [] > > return flatten(a[0])+flatten(a[1:]) > The only problem with this if it is to big or to deeply nested then it > will overflow the stack? Yes, it can overflow in its current incarnation. There is a way to fix that. [WARNING WARNING: The code presented below is very evil, but I just can't resist. *grin* Please do not try to understand the code below, as it is not meant to be read by humans. If you are just learning Python, please skip this message.] There's a way to systematically transform it so that it doesn't overflow, by using "trampolining-style" programming. This technique turns any recursive function into one that only consumes a constant amount of stack space. It's used by programming language theory folks, despite being utterly weird. *grin* I think I wrote a brief introduction to the topic on Python-Tutor list, and have also applied it in a small project (PyScheme). For your (or my?) amusement, here's the transformed flatten() function in trampolined style: ### def flatten(a): """Flatten a list.""" return bounce(flatten_k(a, lambda x: x)) def bounce(thing): """Bounce the 'thing' until it stops being a callable.""" while callable(thing): thing = thing() return thing def flatten_k(a, k): """CPS/trampolined version of the flatten function. The original function, before the CPS transform, looked like this: def flatten(a): if not isinstance(a,(tuple,list)): return [a] if len(a)==0: return [] return flatten(a[0])+flatten(a[1:]) The following code is not meant for human consumption. """ if not isinstance(a,(tuple,list)): return lambda: k([a]) if len(a)==0: return lambda: k([]) def k1(v1): def k2(v2): return lambda: k(v1 + v2) return lambda: flatten_k(a[1:], k2) return lambda: flatten_k(a[0], k1) ### This actually does something useful. ### >>> flatten([1, [2, [3, 4]]]) [1, 2, 3, 4] ### Best of all, it does not stack-overflow. We can test this on an evil constructed example: ### >>> def makeEvilList(n): ... result = [] ... for i in xrange(n): ... result = [[result], i] ... return result ... >>> evilNestedList = makeEvilList(sys.getrecursionlimit()) >>> assert range(sys.getrecursionlimit()) == flatten(evilNestedList) >>> ### And it does work. That being said, "trampolined-style" is just evil in Python: I would not want to inflict that code on anyone, save in jest. *grin* From alan.gauld at freenet.co.uk Thu Jan 13 10:21:32 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 13 10:21:13 2005 Subject: [Tutor] sockets, files, threads References: Message-ID: <00b801c4f951$4626c670$40b68651@xp> > Where did my 'ooo' go? > > #! /usr/bin/env python > import os > > fobj = open('/tmp/xxx','w') > fobj.write('ooo\n') fobj.flush() File objects use buffered IO and by changing from using file objects to file descriptors half way through the buffer could be in any state. You need to flush() to force it to the file before doing anything weird. As was said in a recent thread, mixing file objects and file descriptors is a bad idea. The OS can get confused, let alone the programmer! > os.close(fp) And especially if you open the file as one thing and close it as another! Be consistent, either work with file descriptors throughout or file objects. If you must mix them be prepared for strangeness. Alan G. From guillermo.fernandez.castellanos at gmail.com Thu Jan 13 12:00:03 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Thu Jan 13 12:00:12 2005 Subject: [Tutor] Tix and Table printing Message-ID: <7d7029e70501130300313c39ea@mail.gmail.com> Hi, I have a table of information that look like this (only 3 lines here): Gibraltar Division BR INF D 095 59 GAR None 10-7-4-6 Gibraltar Kuwait Division BR INF D 148 71GAR None 2-1-0-4 Kuwait Somaliland Division BR INF D 139 84 GAR None 2-1-0-3 None I would like to show this in a nice table format, like you could expect in a program like excell. I tried to do this in a ScrolledText.ScrolledText frame. I take each of the columns and make sure they all occupy the same number of characters, by filling with spaces. I do this with each of the columns: output.insert("%d.%d" % (line,column), mcountry+" "*(maxlength-len(mcountry)) That way I ensure that all the columns start at the same... well... column :-) The thing is, when I print to the terminal I obtain beautiful and well defined columns. But when I print to the frame, the starting column and number of spaces are correct, but there's not the output I expect. It's because all the letters do not seem to have the same width. Is there any "table" frame that I am unaware of? or a way of changing the type of character so I can have a font with characters of equal width? I hope my explanation was clear enough... Thanks, G From kent37 at tds.net Thu Jan 13 12:07:54 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 13 12:08:03 2005 Subject: [Tutor] sorry, another update bug In-Reply-To: <4420DB5C-6528-11D9-99A3-000393B2D990@walrus.us> References: <4420DB5C-6528-11D9-99A3-000393B2D990@walrus.us> Message-ID: <41E6568A.4080706@tds.net> The problem is your mutate_genome() function which mutates the genome in place. So all the living_species are using the same genome (they all reference the same list). The solution is to have mutate_genome() copy the genome. You can do this by adding the line a_genome = a_genome[:] to the start of the function. You could also rewrite mutate_genome() to use a list comprehension (which creates a new list). This might clean up the function a bit also: def mutate_gene(gene, rate): if random.random() <= rate: return gene + 1 else: return gene def mutate_genome(a_genome, rate): "For each base in a genome there is a rate chance it is set to 1" new_genome = [ mutate_gene(gene, rate) for gene in a_genome ] return new_genome Kent Vincent Wan wrote: > sorry to inflict another dumb bug on you all so soon... > > I have written a program to replicated simulate the evolution of > species and their genomes based on David Raup and Stephen Jay Gould's > 1974 paper in systematic zoology 23: 305-322. The program correctly > evolves species and keeps track of relationships. The genome of the > species is mutated correctly (shown by the debug write lines) but all > species in the genomes in the living_species are updated to the last > mutated genome. > > sample output: > > parent species genome: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] > species 1 genome after mutation: [1, 1, 1, 1, 0, 1, 1, 0, 1, 1] > parent species genome: [1, 1, 1, 1, 0, 1, 1, 0, 1, 1] > species 2 genome after mutation: [2, 1, 2, 1, 1, 1, 1, 0, 2, 2] > parent species genome: [2, 1, 2, 1, 1, 1, 1, 0, 2, 2] > species 3 genome after mutation: [2, 2, 3, 1, 2, 1, 2, 0, 3, 2] > parent species genome: [2, 2, 3, 1, 2, 1, 2, 0, 3, 2] > species 4 genome after mutation: [2, 2, 3, 2, 3, 1, 3, 0, 4, 2] > parent species genome: [2, 2, 3, 2, 3, 1, 3, 0, 4, 2] > species 2 genome after mutation: [2, 2, 3, 2, 3, 2, 3, 1, 5, 3] > parent species genome: [2, 2, 3, 2, 3, 2, 3, 1, 5, 3] > species 2 genome after mutation: [2, 3, 3, 2, 3, 3, 3, 2, 6, 4] > parent species genome: [2, 3, 3, 2, 3, 3, 3, 2, 6, 4] > species 2 genome after mutation: [2, 3, 3, 2, 4, 4, 3, 3, 6, 5] > parent species genome: [2, 3, 3, 2, 4, 4, 3, 3, 6, 5] > species 3 genome after mutation: [2, 4, 4, 2, 5, 4, 3, 4, 6, 5] > parent species genome: [2, 4, 4, 2, 5, 4, 3, 4, 6, 5] > species 4 genome after mutation: [3, 4, 4, 2, 5, 4, 4, 4, 7, 5] > parent species genome: [3, 4, 4, 2, 5, 4, 4, 4, 7, 5] > species 4 genome after mutation: [3, 4, 5, 2, 5, 5, 4, 4, 8, 6] > parent species genome: [3, 4, 5, 2, 5, 5, 4, 4, 8, 6] > species 4 genome after mutation: [3, 5, 6, 2, 5, 6, 5, 4, 9, 7] > parent species genome: [3, 5, 6, 2, 5, 6, 5, 4, 9, 7] > species 5 genome after mutation: [3, 6, 6, 2, 5, 7, 6, 5, 10, 7] > parent species genome: [3, 6, 6, 2, 5, 7, 6, 5, 10, 7] > species 5 genome after mutation: [4, 7, 7, 3, 6, 7, 7, 6, 11, 7] > parent species genome: [4, 7, 7, 3, 6, 7, 7, 6, 11, 7] > species 6 genome after mutation: [4, 8, 8, 4, 6, 8, 8, 6, 11, 8] > parent species genome: [4, 8, 8, 4, 6, 8, 8, 6, 11, 8] > species 7 genome after mutation: [4, 9, 8, 5, 7, 8, 8, 6, 12, 8] > parent species genome: [4, 9, 8, 5, 7, 8, 8, 6, 12, 8] > species 6 genome after mutation: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9] > > 15 species evolved > living_species: > species: 6 born time: 18 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > species: 8 born time: 29 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > species: 10 born time: 31 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > species: 14 born time: 39 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > species: 15 born time: 41 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > species: 16 born time: 43 died time: 0 genome: [5, 10, 8, 6, 8, 9, 9, > 7, 13, 9] > > dead_species: > species: 4 born time: 3 died time: 5 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 3 born time: 1 died time: 8 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 2 born time: 1 died time: 9 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 1 born time: 1 died time: 17 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 5 born time: 13 died time: 20 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 0 born time: 0 died time: 30 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 7 born time: 27 died time: 32 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 9 born time: 30 died time: 36 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 12 born time: 36 died time: 41 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 11 born time: 33 died time: 42 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > species: 13 born time: 37 died time: 43 genome: [5, 10, 8, 6, 8, 9, > 9, 7, 13, 9] > > tree: > (((0,((6,9),((7,((10,((13,16),14)),(11,(12,15)))),8))),4),((1,5),(2,3))) > > code: > > import random > > debug = 1 > > living_species = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]] > dead_species = [] > tree = "*0*" > > current_time = 0 > current_species_counter = 0 > max_species = 16 # the number of species to be evolved + 1 > branching_probablity = .1 > extinction_probablity = .1 > mutation_rate = .5 > > def mutate_genome(a_genome, rate): > "For each base in a genome there is a rate chance it is set to 1" > current_base = 0 > while current_base < len(a_genome): > if random.random() <= rate: > a_genome[current_base] = a_genome[current_base]+ 1 > current_base += 1 > return a_genome > > while current_species_counter < max_species: > counter = 0 > while counter < len(living_species): > if random.random() < branching_probablity: > current_species_counter += 1 > current_species = [current_species_counter, current_time, > 0, living_species[counter][3]] > if debug: print 'parent species genome:', > living_species[counter][3] > living_species.append(current_species) > living_species[len(living_species)-1][3] = > mutate_genome(current_species[3], mutation_rate) > if debug: print 'species ', len(living_species)-1, 'genome > after mutation:', living_species[len(living_species)-1][3] > > # updates species tree > target = '*' + str(living_species[counter][0]) + '*' > replacement = '(' + target + ',*' + str(current_species[0]) > + '*)' > tree = tree.replace(target, replacement) > counter += 1 > > # if debug: print 'at time ', current_time, ' living_species: ', > [row[0] for row in living_species] > print 'at time ', current_time, ' living_species: ', > living_species > > counter = 0 > while counter < len(living_species): > if random.random() < extinction_probablity: > newly_dead = living_species[counter] > newly_dead[2] = current_time > dead_species.append(newly_dead) > living_species.remove(living_species[counter]) > if len(living_species) == 0: # when the last > living_species goes extinct exit > break > counter += 1 > > # if debug: print 'at time ', current_time, ' dead_species : ', > [row[0] for row in dead_species] > if len(living_species) == 0: # when the last living_species goes > extinct exit > break > current_time += 1 > > if current_species_counter < max_species: > print '\nall extinct with only ', current_species_counter + 1, ' > species of ', max_species > else: > print max_species - 1, ' species evolved' > print 'living_species:' > for each in living_species: > print 'species:', each[0], 'born time: ', each[1], 'died time: ', > each[2] > print 'genome:', each[3] > print '\ndead_species: ' > for each in dead_species: > print 'species:', each[0], 'born time: ', each[1], 'died time: ', > each[2] > print 'genome:', each[3] > tree = tree.replace('*','') > print 'tree: ', tree > > > Vincent > > ------------------------------------------------------------------------ > -------------- > PhD Candidate > Committee on the Conceptual and Historical Studies of Science > University of Chicago > > PO Box 73727 > Fairbanks, AK 99707 > > wan AT walrus DOT us (change CAPS to @ and . ) > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From dimitri.dor at fssintl.com Thu Jan 13 13:20:11 2005 From: dimitri.dor at fssintl.com (Dimitri D'Or) Date: Thu Jan 13 13:20:22 2005 Subject: [Tutor] reinitializing namespace Message-ID: <200501131320.11138.dimitri.dor@fssintl.com> Hello list, For some purpose, I would like to reinitialize the main namespace, i.e. I want to delete all the variables I have created through the use of functions or keyboard entries. At the same time, I want to keep all the modules I have imported so that I can use their functions again without having to reimport them. Would you please tell me possible solutions for achieving this task ? Thank you, Dimitri D'Or From maxnoel_fr at yahoo.fr Thu Jan 13 13:23:00 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 13 13:23:12 2005 Subject: [Tutor] List comprehensions In-Reply-To: <6.1.2.0.0.20050112211009.03a3c478@mail.mric.net> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> <4692AFB0-6504-11D9-BB2B-000393CBC88E@yahoo.fr> <6.1.2.0.0.20050112211009.03a3c478@mail.mric.net> Message-ID: On Jan 13, 2005, at 04:13, Bob Gailer wrote: > I like Kent's response. > > foobar(item)/0 is a "valid" expression. It fits the grammar of > expressions. The fact that it raises an exception does not make it an > invalid expression. > > Consider foobar(item)/xyz. It is valid. If xyz == 0 then it will also > raise an exception. You have a point, I hadn't thought of it that way. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From op73418 at mail.telepac.pt Thu Jan 13 14:04:37 2005 From: op73418 at mail.telepac.pt (=?ISO-8859-1?Q?Gon=E7alo_Rodrigues?=) Date: Thu Jan 13 14:00:58 2005 Subject: [Tutor] flattening a list In-Reply-To: <20050113022828.39088.qmail@web54310.mail.yahoo.com> References: <20050113022828.39088.qmail@web54310.mail.yahoo.com> Message-ID: <41E671E5.6010802@mail.telepac.pt> Chad Crabtree wrote: > The only problem with this if it is to big or to deeply nested then > it > will overflow the stack? > Danny Yoo has given a mind-blowing continuation implementation that will not overflow the stack. Below goes a recursive-iterator implementation. To avoid deep recursion the code can simluate its own stack (or hope that Python gains tail-call optimization *grin*) but for simplicity's sake we just use recursion. def isIterable(iterable): """Test for iterable-ness.""" try: iter(iterable) except TypeError: return False return True def isBadIterable(iterable): """Return True if it's a 'bad' iterable. Note: string's are bad because, when iterated they return strings making itterflatten loop infinitely. """ return isinstance(iterable, basestring) def iterflatten(iterable): """Return a flattened iterator.""" it = iter(iterable) for e in it: if isIterable(e) and not isBadIterable(e): #Recurse into iterators. for f in iterflatten(e): yield f else: yield e A test: for elem in iterflatten([1, 2, [3, 4, (5, 6), 7], 8, [9], [10, 11, iter([12, 13])]]): print elem And it gives: >>> 1 2 3 4 5 6 7 8 9 10 11 12 13 Best regards, G. Rodrigues From bwinton at latte.ca Thu Jan 13 15:03:15 2005 From: bwinton at latte.ca (Blake Winton) Date: Thu Jan 13 15:03:16 2005 Subject: [Tutor] List comprehensions In-Reply-To: <41E5D764.7080008@tds.net> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> <41E5D764.7080008@tds.net> Message-ID: <41E67FA3.8000201@latte.ca> Kent Johnson wrote: >>> If you mean for j to be a list of foobar(item) then use >>> j=[foobar(item) for item in x] >>> The first part of the list comp can be any valid expression. >> Does that mean that there are invalid expressions? I'd enjoy seeing an >> example. > I suppose if it's an expression, it must be valid, eh? Otherwise it's > something else. I don't think I entirely agree... What about "x === item" It's certainly not a statement, and I would wager that Python was in the middle of its expression parsing code when it threw the SyntaxError. (Or how about "x == item ="?) Perhaps someone more in touch with the compiler internals will chip in here. It is an interesting philosophical question, though. Later, Blake. From kent37 at tds.net Thu Jan 13 16:52:40 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 13 16:52:43 2005 Subject: [Tutor] List comprehensions In-Reply-To: <41E67FA3.8000201@latte.ca> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> <41E5D764.7080008@tds.net> <41E67FA3.8000201@latte.ca> Message-ID: <41E69948.4000402@tds.net> Blake Winton wrote: > Kent Johnson wrote: > >>>> If you mean for j to be a list of foobar(item) then use >>>> j=[foobar(item) for item in x] >>>> The first part of the list comp can be any valid expression. >>> >>> Does that mean that there are invalid expressions? I'd enjoy seeing >>> an example. >> >> I suppose if it's an expression, it must be valid, eh? Otherwise it's >> something else. > > > I don't think I entirely agree... What about "x === item" It's > certainly not a statement, and I would wager that Python was in the > middle of its expression parsing code when it threw the SyntaxError. (Or > how about "x == item ="?) Perhaps someone more in touch with the > compiler internals will chip in here. We're talking about angels and pins, here, but I would say that the only meaningful interpretation of 'x is an expression' is 'x is parseable according to the syntax rules for an expression'. So if it doesn't parse it's not an expression. How can you say "x === item" is not a statement? Because it doesn't parse as a statement. By the same logic it isn't an expression either. Kent > > It is an interesting philosophical question, though. > > Later, > Blake. > From abli at freemail.hu Thu Jan 13 18:15:45 2005 From: abli at freemail.hu (Abel Daniel) Date: Thu Jan 13 18:15:35 2005 Subject: [Tutor] Re: Is Tkinter the Gui in python In-Reply-To: (Gopinath V.'s message of "Wed, 12 Jan 2005 16:37:13 +0530") References: Message-ID: "Gopinath V, ASDC Chennai" writes: > Hi > Is the standard Tkinter module in python does the GUI - Front -end > Interface for Python Well, if you mean "standard" as in included by default, then yes. Tkinter however is a sort-of outdated gui. If you only want some basic widgets, like entry boxes, buttons and such, and need to make a cross-platform gui that can be used on as many platforms as possible, then use tkinter. As far as I know, tkinter isn't really developed nowdays. For more complicated guis, for example using tree widgets, you might be better of with say, pygtk. However, pygtk afaict isn't available on as many platforms as tkinter. So tkinter is a good module to use if you only want simple widgets, but be prepared to switch to something newer when you hit it's limitations. -- Abel Daniel From jfouhy at paradise.net.nz Thu Jan 13 19:18:49 2005 From: jfouhy at paradise.net.nz (John Fouhy) Date: Thu Jan 13 19:18:51 2005 Subject: [Tutor] Tix and Table printing In-Reply-To: <7d7029e70501130300313c39ea@mail.gmail.com> References: <7d7029e70501130300313c39ea@mail.gmail.com> Message-ID: <41E6BB89.6050404@paradise.net.nz> Guillermo Fernandez Castellanos wrote: > Is there any "table" frame that I am unaware of? or a way of changing > the type of character so I can have a font with characters of equal > width? You might see if this does what you want: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52266 -- John. From mi.janssen at gmail.com Thu Jan 13 19:28:51 2005 From: mi.janssen at gmail.com (Michael Janssen) Date: Thu Jan 13 19:28:55 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <200501131320.11138.dimitri.dor@fssintl.com> References: <200501131320.11138.dimitri.dor@fssintl.com> Message-ID: <1ff2dfbf05011310281ef9ea00@mail.gmail.com> On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or wrote: > For some purpose, I would like to reinitialize the main namespace, i.e. I want > to delete all the variables I have created through the use of functions or > keyboard entries. Hello Dimiti, sound like you're talking about an interactive session. Otherwise (within a script) it would be a really bad idea to try this (better put your stuff into functions, that don't create global variables). Even in an interactive session it sounds like a not that brilliant idea, especially since I can't think of a way other than using exec "del %s" % key for appropriate keys from globals(). Finding "appropiate" keys is one tricky thing. Why not end your ugly python session and start a new one? You can define all your imports in the python startfile (on Linux, consult python manpage. On Windows, I don't know). You can also define useful functions or variables in your python startfile. This way, you're really shure that all ugly variables are away without del'iting anything important. regards Michael From bgailer at alum.rpi.edu Thu Jan 13 19:40:39 2005 From: bgailer at alum.rpi.edu (Bob Gailer) Date: Thu Jan 13 19:37:38 2005 Subject: [Tutor] List comprehensions In-Reply-To: <41E67FA3.8000201@latte.ca> References: <41E5B757.30903@tds.net> <6.1.2.0.0.20050112181227.039e7880@mail.mric.net> <41E5D764.7080008@tds.net> <41E67FA3.8000201@latte.ca> Message-ID: <6.1.2.0.0.20050113113545.034bf020@mail.mric.net> At 07:03 AM 1/13/2005, Blake Winton wrote: What about "x === item" Perhaps we should propose an extension to Python: Assignment Statement: name = [[name] = ] ... expression-list. The absence of a name would create an anonymous variable just as lambda creates anonymous functions. Reminds me of a proposal a long time ago for a COME FROM statement in FORTRAN. Bob Gailer mailto:bgailer@alum.rpi.edu 303 442 2625 home 720 938 2625 cell From alan.gauld at freenet.co.uk Thu Jan 13 19:49:28 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 13 19:49:01 2005 Subject: [Tutor] Re: Is Tkinter the Gui in python References: Message-ID: <00ee01c4f9a0$9c91f030$40b68651@xp> > cross-platform gui that can be used on as many platforms as possible, > then use tkinter. As far as I know, tkinter isn't really developed nowdays. I assume Tkinter is tracking Tk and Tk is certainly still being developed. A new version was released quite recently I believe. > So tkinter is a good module to use if you only want simple widgets, > but be prepared to switch to something newer when you hit it's limitations. This I agree with totally, fortunately I've yet to hit itslimitations in my fairly simple GUIs. Alan G. From flaxeater at yahoo.com Thu Jan 13 19:53:28 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 13 19:53:32 2005 Subject: [Tutor] flattening a list Message-ID: <20050113185329.54736.qmail@web54309.mail.yahoo.com> You should post this on the Python Cookbook http://aspn.activestate.com/ASPN/Python/Cookbook/ Orri Ganel wrote > Bill Kranec wrote: > > > >> Hello, > > >> > >> I have a list of lists, for example [ [1,2] , [3,4] ], and I would > > >> like to pass all the elements of that list as arguments to a function > > >> (for example the intersection of all list elements). Is there a > > >> command in regular Python to do this? I would like to avoid the > > >> hassle and speed hit of a loop to extract all the list elements. > > >> > >> In the past, I believe I have used something like flatten(list), which > > >> was part of Numarray (I think). Am I missing an obvious or clever > > >> solution in regular Python? > > >> > >> Thanks for your help! > > >> > >> Bill > > >> _______________________________________________ > > >> Tutor maillist - Tutor@python.org > > >> http://mail.python.org/mailman/listinfo/tutor > > >> > > Well, here's something i put together (works for lists and tuples, > ignores strings and dicts): > > > > ### Start Code ### > > def flatten(sequence): > > > > def rflat(seq2): > > seq = [] > > for entry in seq2: > > if '__contains__' in dir(entry) and \ > > type(entry) != str and \ > > type(entry)!=dict: > > seq.extend([i for i in entry]) > > else: > > seq.append(entry) > > return seq > > > > def seqin(sequence): > > for i in sequence: > > if '__contains__' in dir(i) and \ > > type(i) != str and \ > > type(i) != dict: > > return True > > return False > > > > seq = sequence[:] > > while seqin(seq): > > seq = rflat(seq) > > return seq > > ### End Code ### > > > > Tested it on a few different scenarios, all performed as expected: > > > > >>> flatten([1]) > > [1] > > >>> flatten([1,[1]]) > > [1, 1] > > >>> flatten(['ab',1]) > > ['ab', 1] > > >>> flatten([10,(34,42),54,'abc']) > > [10, 34, 42, 54, 'abc'] > > >>> flatten([{'abc':[1,2,3]},[{'a':'1'},{'[1,2]':'ab'}]]) > > [{'abc': [1, 2, 3]}, {'a': '1'}, {'[1,2]': 'ab'}] > > > > Cheers, > > Orri > __________________________________ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com From ps_python at yahoo.com Thu Jan 13 19:56:00 2005 From: ps_python at yahoo.com (kumar s) Date: Thu Jan 13 19:56:03 2005 Subject: [Tutor] Regular expression re.search() object . Please help Message-ID: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Dear group: My list looks like this: List name = probe_pairs Name=AFFX-BioB-5_at Cell1=96 369 N control AFFX-BioB-5_at Cell2=96 370 N control AFFX-BioB-5_at Cell3=441 3 N control AFFX-BioB-5_at Cell4=441 4 N control AFFX-BioB-5_at Name=223473_at Cell1=307 87 N control 223473_at Cell2=307 88 N control 223473_at Cell3=367 84 N control 223473_at My Script: >>> name1 = '[N][a][m][e][=]' >>> for i in range(len(probe_pairs)): key = re.match(name1,probe_pairs[i]) key <_sre.SRE_Match object at 0x00E37A68> <_sre.SRE_Match object at 0x00E37AD8> <_sre.SRE_Match object at 0x00E37A68> <_sre.SRE_Match object at 0x00E37AD8> <_sre.SRE_Match object at 0x00E37A68> ..................................... (cont. 10K lines) Here it prints a bunch of reg.match objects. However when I say group() it prints only one object why? Alternatively: >>> for i in range(len(probe_pairs)): key = re.match(name1,probe_pairs[i]) key.group() 'Name=' 1. My aim: To remove those Name=**** lines from my probe_pairs list with name1 as the pattern, I asked using re.match() method to identify the lines and then remove by using re.sub(pat,'',string) method. I want to substitute Name=*** line by an empty string. After I get the reg.match object, I tried to remove that match object like this: >>> for i in range(len(probe_pairs)): key = re.match(name1,probe_pairs[i]) del key print probe_pairs[i] Name=AFFX-BioB-5_at Cell1=96 369 N control AFFX-BioB-5_at Cell2=96 370 N control AFFX-BioB-5_at Cell3=441 3 N control AFFX-BioB-5_at Result shows that that Name** line has not been deleted. Is the way I am doing a good one. Could you please suggest a good simple method. Thanks in advance K __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 20:54:16 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 20:54:20 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Message-ID: On Thu, 13 Jan 2005, kumar s wrote: > My list looks like this: List name = probe_pairs > Name=AFFX-BioB-5_at > Cell1=96 369 N control AFFX-BioB-5_at > Cell2=96 370 N control AFFX-BioB-5_at > Cell3=441 3 N control AFFX-BioB-5_at > Cell4=441 4 N control AFFX-BioB-5_at > Name=223473_at > Cell1=307 87 N control 223473_at > Cell2=307 88 N control 223473_at > Cell3=367 84 N control 223473_at > > My Script: > >>> name1 = '[N][a][m][e][=]' Hi Kumar, The regular expression above can be simplified to: 'Name=' The character-class operator that you're using, with the brackets '[]', is useful when we want to allow different kind of characters. Since the code appears to be looking at a particular string, the regex can be greatly simplified by not using character classes. > >>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > key > > > <_sre.SRE_Match object at 0x00E37A68> > <_sre.SRE_Match object at 0x00E37AD8> > <_sre.SRE_Match object at 0x00E37A68> > <_sre.SRE_Match object at 0x00E37AD8> > <_sre.SRE_Match object at 0x00E37A68> > ..................................... (cont. 10K > lines) > > Here it prints a bunch of reg.match objects. However when I say group() > it prints only one object why? Is it possible that the edited code may have done something like this? ### for i in range(len(probe_pairs)): key = re.match(name1, probe_pairs[i]) print key ### Without seeing what the literal code looks like, we're doomed to use our imaginations and make up a reasonable story. *grin* > >>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > key.group() Ok, I think I see what you're trying to do. You're using the interactive interpreter, which tries to be nice when we use it as a calculator. The interactive interpreter has a special feature that prints out the result of expressions, even though we have not explicitely put in a "print" statement. When we using a loop, like: ### >>> for i in range(10): ... i, i*2, i*3 ... (0, 0, 0) (1, 2, 3) (2, 4, 6) (3, 6, 9) (4, 8, 12) (5, 10, 15) (6, 12, 18) (7, 14, 21) (8, 16, 24) (9, 18, 27) ### If the body of the loop contains a single expression, then Python's interactive interpreter will try to be nice and print that expression through each iteration. The automatic expression-printing feature of the interactive interpreter is only for our convenience. If we're not running in interactive mode, Python will not automatically print out the values of expressions! So in a real program, it is much better to explicity write out the command statement to 'print' the expression to screen, if that's what you want: ### >>> for i in range(10): ... print (i, i*2, i*3) ... (0, 0, 0) (1, 2, 3) (2, 4, 6) (3, 6, 9) (4, 8, 12) (5, 10, 15) (6, 12, 18) (7, 14, 21) (8, 16, 24) (9, 18, 27) ### > After I get the reg.match object, I tried to remove > that match object like this: > >>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > del key > print probe_pairs[i] The match object has a separate existance from the string 'probe_pairs[i]'. Your code does drop the 'match' object, but this has no effect in making a string change in probe_pairs[i]. The code above, removing those two lines that play with the 'key', reduces down back to: ### for i in range(len(probe_pairs)): print probe_pairs[i] ### which is why you're not seeing any particular change in the output. I'm not exactly sure you really need to do regular expression stuff here. Would the following work for you? ### for probe_pair in probe_pairs: if not probe_pair.startswith('Name='): print probe_pair ### > Name=AFFX-BioB-5_at > Cell1=96 369 N control AFFX-BioB-5_at > Cell2=96 370 N control AFFX-BioB-5_at > Cell3=441 3 N control AFFX-BioB-5_at > > Result shows that that Name** line has not been deleted. What do you want to see? Do you want to see: ### AFFX-BioB-5_at Cell1=96 369 N control AFFX-BioB-5_at Cell2=96 370 N control AFFX-BioB-5_at Cell3=441 3 N control AFFX-BioB-5_at ### or do you want to see this instead? ### Cell1=96 369 N control AFFX-BioB-5_at Cell2=96 370 N control AFFX-BioB-5_at Cell3=441 3 N control AFFX-BioB-5_at ### Good luck to you! From dyoo at hkn.eecs.berkeley.edu Thu Jan 13 21:06:51 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 13 21:06:55 2005 Subject: [Tutor] Tix and Table printing In-Reply-To: <7d7029e70501130300313c39ea@mail.gmail.com> Message-ID: On Thu, 13 Jan 2005, Guillermo Fernandez Castellanos wrote: > Is there any "table" frame that I am unaware of? Hi Guillermo, There are some recipes for making a Tkinter table widget; here's one: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52266 There's a Tkinter wiki page that has links to a few more Table implementations: http://tkinter.unpythonic.net/wiki/FrontPage Hope this helps! From cyresse at gmail.com Thu Jan 13 21:49:29 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 13 21:49:36 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Message-ID: ...as do I. openFile=file("probe_pairs.txt","r") probe_pairs=openFile.readlines() openFile.close() indexesToRemove=[] for lineIndex in range(len(probe_pairs)): if probe_pairs[lineIndex].startswith("Name="): indexesToRemove.append(lineIndex) for index in indexesToRemove: probe_pairs[index]='"" Could just be openFile=file("probe_pairs.txt","r") probe_pairs=openFile.readlines() openFile.close() indexesToRemove=[] for lineIndex in range(len(probe_pairs)): if probe_pairs[lineIndex].startswith("Name="): probe_pairs[lineIndex]='' On Fri, 14 Jan 2005 09:38:17 +1300, Liam Clarke wrote: > > >>> name1 = '[N][a][m][e][=]' > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > key > > > > <_sre.SRE_Match object at 0x00E37A68> > > <_sre.SRE_Match object at 0x00E37AD8> > > <_sre.SRE_Match object at 0x00E37A68> > > <_sre.SRE_Match object at 0x00E37AD8> > > <_sre.SRE_Match object at 0x00E37A68> > > > You are overwriting key each time you iterate. key.group() gives the > matched characters in that object, not a group of objects!!! > > You want > > >>> name1 = '[N][a][m][e][=]' > > >>> keys=[] > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > keys.append[key] > > >>> print keys > > > 'Name=' > > > > 1. My aim: > > To remove those Name=**** lines from my probe_pairs > > list > > Why are you deleting the object key? > > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > del key > > print probe_pairs[i] > > Here's the easy way. Assuming that probe_pairs is stored in a file callde > probe_pairs.txt > > openFile=file("probe_pairs.txt","r") > probe_pairs=openFile.readlines() > > openFile.close() > > indexesToRemove=[] > > for lineIndex in range(len(probe_pairs)): > > if probe_pairs[lineIndex].startswith("Name="): > indexesToRemove.append(lineIndex) > > for index in indexesToRemove: > probe_pairs[index]='"" > > Try that. > > Argh, my head. You do some strange things to Python. > > Liam Clarke > > On Thu, 13 Jan 2005 10:56:00 -0800 (PST), kumar s wrote: > > Dear group: > > > > My list looks like this: List name = probe_pairs > > Name=AFFX-BioB-5_at > > Cell1=96 369 N control AFFX-BioB-5_at > > Cell2=96 370 N control AFFX-BioB-5_at > > Cell3=441 3 N control AFFX-BioB-5_at > > Cell4=441 4 N control AFFX-BioB-5_at > > Name=223473_at > > Cell1=307 87 N control 223473_at > > Cell2=307 88 N control 223473_at > > Cell3=367 84 N control 223473_at > > > > My Script: > > >>> name1 = '[N][a][m][e][=]' > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > key > > > > <_sre.SRE_Match object at 0x00E37A68> > > <_sre.SRE_Match object at 0x00E37AD8> > > <_sre.SRE_Match object at 0x00E37A68> > > <_sre.SRE_Match object at 0x00E37AD8> > > <_sre.SRE_Match object at 0x00E37A68> > > ..................................... (cont. 10K > > lines) > > > > Here it prints a bunch of reg.match objects. However > > when I say group() it prints only one object why? > > > > Alternatively: > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > key.group() > > > > 'Name=' > > > > 1. My aim: > > To remove those Name=**** lines from my probe_pairs > > list > > > > with name1 as the pattern, I asked using re.match() > > method to identify the lines and then remove by using > > re.sub(pat,'',string) method. I want to substitute > > Name=*** line by an empty string. > > > > After I get the reg.match object, I tried to remove > > that match object like this: > > >>> for i in range(len(probe_pairs)): > > key = re.match(name1,probe_pairs[i]) > > del key > > print probe_pairs[i] > > > > Name=AFFX-BioB-5_at > > Cell1=96 369 N control AFFX-BioB-5_at > > Cell2=96 370 N control AFFX-BioB-5_at > > Cell3=441 3 N control AFFX-BioB-5_at > > > > Result shows that that Name** line has not been > > deleted. > > > > Is the way I am doing a good one. Could you please > > suggest a good simple method. > > > > Thanks in advance > > K > > > > > > __________________________________ > > Do you Yahoo!? > > Yahoo! Mail - Easier than ever with enhanced search. Learn more. > > http://info.mail.yahoo.com/mail_250 > > _______________________________________________ > > 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. > -- '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. From jeff at ccvcorp.com Thu Jan 13 22:29:38 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Thu Jan 13 22:24:24 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Message-ID: <41E6E842.2040206@ccvcorp.com> Liam Clarke wrote: > openFile=file("probe_pairs.txt","r") > probe_pairs=openFile.readlines() > > openFile.close() > > indexesToRemove=[] > > for lineIndex in range(len(probe_pairs)): > > if probe_pairs[lineIndex].startswith("Name="): > probe_pairs[lineIndex]='' If the intent is simply to remove all lines that begin with "Name=", and setting those lines to an empty string is just shorthand for that, it'd make more sense to do this with a filtering list comprehension: openfile = open("probe_pairs.txt","r") probe_pairs = openfile.readlines() openfile.close() probe_pairs = [line for line in probe_pairs \ if not line.startswith('Name=')] (The '\' line continuation isn't strictly necessary, because the open list-comp will do the same thing, but I'm including it for readability's sake.) If one wants to avoid list comprehensions, you could instead do: openfile = open("probe_pairs.txt","r") probe_pairs = [] for line in openfile.readlines(): if not line.startswith('Name='): probe_pairs.append(line) openfile.close() Either way, lines that start with 'Name=' get thrown away, and all other lines get kept. Jeff Shannon Technician/Programmer Credit International From jfouhy at paradise.net.nz Thu Jan 13 22:32:38 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Thu Jan 13 22:32:42 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <41E6E842.2040206@ccvcorp.com> References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> <41E6E842.2040206@ccvcorp.com> Message-ID: <1105651958.41e6e8f6d1add@www.paradise.net.nz> Quoting Jeff Shannon : > If the intent is simply to remove all lines that begin with "Name=", > and setting those lines to an empty string is just shorthand for that, > it'd make more sense to do this with a filtering list comprehension: [...] > If one wants to avoid list comprehensions, you could instead do: [...] Or, since we're filtering, we could use the filter() function! probe_pairs = filter(lambda x: not x.startswith('Name='), probe_pairs) (hmm, I wonder which is the faster option...) -- John. From flaxeater at yahoo.com Thu Jan 13 22:38:50 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 13 22:38:53 2005 Subject: [Tutor] Referer file from import Message-ID: <20050113213850.57705.qmail@web54305.mail.yahoo.com> Is there a way to know what the path of the file is that imported a module? I've tried __file__ and playing with globals() but I can't seem to crack this. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From lumbricus at gmx.net Thu Jan 13 23:00:42 2005 From: lumbricus at gmx.net (=?ISO-8859-1?Q?=22J=F6rg_W=F6lke=22?=) Date: Thu Jan 13 23:00:44 2005 Subject: [Tutor] Regular expression re.search() object . Please help References: <1105651958.41e6e8f6d1add@www.paradise.net.nz> Message-ID: <19614.1105653642@www74.gmx.net> > Quoting Jeff Shannon : [ snip ] > (hmm, I wonder which is the faster option...) Propably "grep -v ^Name= filename" > -- > John. 0.2 EUR, J"o! -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realit?t. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realit?t zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) +++ GMX - die erste Adresse f?r Mail, Message, More +++ 1 GB Mailbox bereits in GMX FreeMail http://www.gmx.net/de/go/mail From ismaelgf at adinet.com.uy Thu Jan 13 23:32:40 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Thu Jan 13 23:32:29 2005 Subject: [Tutor] Jythonc compile error Message-ID: <41E6F708.3000204@adinet.com.uy> Hello. I don't know what's wrong. Any help is welcome. Thanks Ismael testui.py ####### import Java #Tried with and without this line print "Hello World!" ####### H:\ARCHIV~1\Jython>jythonc -c -d -j test testui.py processing testui Required packages: Creating adapters: Creating .java files: testui module Compiling .java to .class... Compiling with args: ['H:\\Archivos de programa\\Java\\jre1.5.0_01\\bin\\javac', '-classpath', 'H:\\Archivos de programa\\Jython\\jython.jar;;.\\jpywork;;H:\\Ar chivos de programa\\Jython\\Tools\\jythonc;H:\\ARCHIV~1\\Jython\\.;H:\\Archivos de programa\\Jython\\Lib;H:\\Archivos de programa\\Jython', '.\\jpywork\\testui. java'] 1 java.io.IOException: CreateProcess: "H:\Archivos de programa\Java\jre1.5.0_01 \bin\javac" -classpath "H:\Archivos de programa\Jython\jython.jar;;.\jpywork;;H: \Archivos de programa\Jython\Tools\jythonc;H:\ARCHIV~1\Jython\.;H:\Archivos de p rograma\Jython\Lib;H:\Archivos de programa\Jython" .\jpywork\testui.java error=2 Consider using the -C/--compiler command line switch, or setting the property python.jythonc.compiler in the registry. ERROR DURING JAVA COMPILATION... EXITING From ps_python at yahoo.com Thu Jan 13 23:57:24 2005 From: ps_python at yahoo.com (kumar s) Date: Thu Jan 13 23:57:28 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: Message-ID: <20050113225724.49815.qmail@web53704.mail.yahoo.com> Hello group: thank you for the suggestions. It worked for me using if not line.startswith('Name='): expression. I have been practising regular expression problems. I tumle over one simple thing always. After obtaining either a search object or a match object, I am unable to apply certain methods on these objects to get stuff. I have looked into many books including my favs( Larning python and Alan Gaulds Learn to program using python) I did not find the basic question, how can I get what I intend to do with returned reg.ex match object (search(), match()). For example: I have a simple list like the following: >>> seq ['>probe:HG-U133B:200000_s_at:164:623; Interrogation_Position=6649; Antisense;', 'TCATGGCTGACAACCCATCTTGGGA'] Now I intend to extract particular pattern and write to another list say: desired[] What I want to extract: I want to extract 164:623: Which always comes after _at: and ends with ; 2. The second pattern/number I want to extract is 6649: This always comes after position=. How I want to put to desired[]: >>> desired ['>164:623|6649', 'TCATGGCTGACAACCCATCTTGGGA'] I write a pattern: pat = '[0-9]*[:][0-9]*' pat1 = '[_Position][=][0-9]*' >>> for line in seq: pat = '[0-9]*[:][0-9]*' pat1 = '[_Position][=][0-9]*' print (re.search(pat,line) and re.search(pat1,line)) <_sre.SRE_Match object at 0x163CAF00> None Now I know that I have a hit in the seq list evident by <_sre.SRE_Match object at 0x163CAF00>. Here is the black box: What kind of operations can I do on this to get those two matches: 164:623 and 6649. I read http://www.python.org/doc/2.2.3/lib/re-objects.html This did not help me to progress further. May I request tutors to give a small note explaining things. In Alan Gauld's book, most of the explanation stopped at <_sre.SRE_Match object at 0x163CAF00> this level. After that there is no example where he did some operations on these objects. If I am wrong, I might have skipped/missed to read it. Aplogies for that. Thank you very much in advance. K --- Liam Clarke wrote: > ...as do I. > > openFile=file("probe_pairs.txt","r") > probe_pairs=openFile.readlines() > > openFile.close() > > indexesToRemove=[] > > for lineIndex in range(len(probe_pairs)): > > if > probe_pairs[lineIndex].startswith("Name="): > > indexesToRemove.append(lineIndex) > > for index in indexesToRemove: > probe_pairs[index]='"" > > Could just be > > openFile=file("probe_pairs.txt","r") > probe_pairs=openFile.readlines() > > openFile.close() > > indexesToRemove=[] > > for lineIndex in range(len(probe_pairs)): > > if > probe_pairs[lineIndex].startswith("Name="): > probe_pairs[lineIndex]='' > > > > > > On Fri, 14 Jan 2005 09:38:17 +1300, Liam Clarke > wrote: > > > >>> name1 = '[N][a][m][e][=]' > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > key > > > > > > <_sre.SRE_Match object at 0x00E37A68> > > > <_sre.SRE_Match object at 0x00E37AD8> > > > <_sre.SRE_Match object at 0x00E37A68> > > > <_sre.SRE_Match object at 0x00E37AD8> > > > <_sre.SRE_Match object at 0x00E37A68> > > > > > > You are overwriting key each time you iterate. > key.group() gives the > > matched characters in that object, not a group of > objects!!! > > > > You want > > > >>> name1 = '[N][a][m][e][=]' > > > >>> keys=[] > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > keys.append[key] > > > > >>> print keys > > > > > 'Name=' > > > > > > 1. My aim: > > > To remove those Name=**** lines from my > probe_pairs > > > list > > > > Why are you deleting the object key? > > > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > del key > > > print probe_pairs[i] > > > > Here's the easy way. Assuming that probe_pairs is > stored in a file callde > > probe_pairs.txt > > > > openFile=file("probe_pairs.txt","r") > > probe_pairs=openFile.readlines() > > > > openFile.close() > > > > indexesToRemove=[] > > > > for lineIndex in range(len(probe_pairs)): > > > > if > probe_pairs[lineIndex].startswith("Name="): > > > indexesToRemove.append(lineIndex) > > > > for index in indexesToRemove: > > probe_pairs[index]='"" > > > > Try that. > > > > Argh, my head. You do some strange things to > Python. > > > > Liam Clarke > > > > On Thu, 13 Jan 2005 10:56:00 -0800 (PST), kumar s > wrote: > > > Dear group: > > > > > > My list looks like this: List name = probe_pairs > > > Name=AFFX-BioB-5_at > > > Cell1=96 369 N control > AFFX-BioB-5_at > > > Cell2=96 370 N control > AFFX-BioB-5_at > > > Cell3=441 3 N control > AFFX-BioB-5_at > > > Cell4=441 4 N control > AFFX-BioB-5_at > > > Name=223473_at > > > Cell1=307 87 N control > 223473_at > > > Cell2=307 88 N control > 223473_at > > > Cell3=367 84 N control > 223473_at > > > > > > My Script: > > > >>> name1 = '[N][a][m][e][=]' > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > key > > > > > > <_sre.SRE_Match object at 0x00E37A68> > > > <_sre.SRE_Match object at 0x00E37AD8> > > > <_sre.SRE_Match object at 0x00E37A68> > > > <_sre.SRE_Match object at 0x00E37AD8> > > > <_sre.SRE_Match object at 0x00E37A68> > > > ..................................... (cont. 10K > > > lines) > > > > > > Here it prints a bunch of reg.match objects. > However > > > when I say group() it prints only one object > why? > > > > > > Alternatively: > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > key.group() > > > > > > 'Name=' > > > > > > 1. My aim: > > > To remove those Name=**** lines from my > probe_pairs > > > list > > > > > > with name1 as the pattern, I asked using > re.match() > > > method to identify the lines and then remove by > using > > > re.sub(pat,'',string) method. I want to > substitute > > > Name=*** line by an empty string. > > > > > > After I get the reg.match object, I tried to > remove > > > that match object like this: > > > >>> for i in range(len(probe_pairs)): > > > key = re.match(name1,probe_pairs[i]) > > > del key > > > print probe_pairs[i] > > > > > > Name=AFFX-BioB-5_at > > > Cell1=96 369 N control > AFFX-BioB-5_at > > > Cell2=96 370 N control > AFFX-BioB-5_at > > > Cell3=441 3 N control > AFFX-BioB-5_at > > > > > > Result shows that that Name** line has not been > > > deleted. > > > > > > Is the way I am doing a good one. Could you > please > > > suggest a good simple method. > > > > > > Thanks in advance > > > K > > > > > > > > > __________________________________ > > > Do you Yahoo!? > > > Yahoo! Mail - Easier than ever with enhanced > search. Learn more. > > > http://info.mail.yahoo.com/mail_250 > === message truncated === __________________________________ Do you Yahoo!? All your favorites on one personal page – Try My Yahoo! http://my.yahoo.com From jfouhy at paradise.net.nz Fri Jan 14 00:17:46 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Fri Jan 14 00:17:51 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <20050113225724.49815.qmail@web53704.mail.yahoo.com> References: <20050113225724.49815.qmail@web53704.mail.yahoo.com> Message-ID: <1105658266.41e7019ad4c87@www.paradise.net.nz> Quoting kumar s : > For example: > > I have a simple list like the following: > > >>> seq > ['>probe:HG-U133B:200000_s_at:164:623; > Interrogation_Position=6649 ; Antisense;', > 'TCATGGCTGACAACCCATCTTGGGA'] > > > Now I intend to extract particular pattern and write > to another list say: desired[] > > What I want to extract: > I want to extract 164:623: > Which always comes after _at: and ends with ; > 2. The second pattern/number I want to extract is > 6649: > This always comes after position=. > > How I want to put to desired[]: > > >>> desired > ['>164:623|6649', 'TCATGGCTGACAACCCATCTTGGGA'] You need to look into groups, or (even better) named groups. Look at the re syntax. Example: >>> import re >>> s = 'probe:HG-U133B:200000_s_at:164:623;\nnterrogation_Position=6649 ; Antisense;' >>> regEx = r'_at:(?P\d+):(?P\d+);.*?_Position=(?P\d+)' >>> m = re.search(regEx, s, re.DOTALL) >>> m.group('num1'), m.group('num2'), m.group('pos') ('164', '623', '6649') The (?Pbar) syntax creates a group which will match the regular expression 'bar', and then give it the name 'foo'. A simpler-looking regular expression would be: >>> regEx = r'_at:(\d+):(\d+);.*?_Position=(\d+)' The parentheses still create groups, but now you have to access them using their index (from left to right, counting from 1). But I think named groups are nicer in terms of self-documenting code :-) -- John. From sigurd at 12move.de Thu Jan 13 23:03:07 2005 From: sigurd at 12move.de (Karl =?iso-8859-1?Q?Pfl=E4sterer?=) Date: Fri Jan 14 00:21:26 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <20050113185600.85462.qmail@web53707.mail.yahoo.com> (kumar s.'s message of "Thu, 13 Jan 2005 10:56:00 -0800 (PST)") References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Message-ID: On 13 Jan 2005, ps_python@yahoo.com wrote: > My list looks like this: List name = probe_pairs > Name=AFFX-BioB-5_at > Cell1=96 369 N control AFFX-BioB-5_at > Cell2=96 370 N control AFFX-BioB-5_at > Cell3=441 3 N control AFFX-BioB-5_at > Cell4=441 4 N control AFFX-BioB-5_at > Name=223473_at > Cell1=307 87 N control 223473_at > Cell2=307 88 N control 223473_at > Cell3=367 84 N control 223473_at > > My Script: >>>> name1 = '[N][a][m][e][=]' >>>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > key Here I see two things: (a) The regexp is not what you possibly wanted: if you want a match for a simple string then use just that string (b) The common iterate idiom in Python is: for elem in lst: .... range() is not necesssary. [...] > Here it prints a bunch of reg.match objects. However > when I say group() it prints only one object why? You don't say how you write group(). > Alternatively: >>>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > key.group() > > > 'Name=' This is clear. The group is always the same: ?Name=? > 1. My aim: > To remove those Name=**** lines from my probe_pairs > list This is easy. Since you always want to remove the samesimple string you don't need a regexp; the string methgods will do a fine job here. A slice will also be fine. [...] > After I get the reg.match object, I tried to remove > that match object like this: >>>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > del key > print probe_pairs[i] [...] > Result shows that that Name** line has not been > deleted. You don't alter the original list so how could it be changed. > Is the way I am doing a good one. Could you please > suggest a good simple method. A very simple one: [entry.startswith('Name=') and entry[5:] or entry for entry in probe_pairs] or: map(lambda entry: entry.startswith('Name=) and entry[5:] or entry, probe_pairs) or with Python >= 2.4 as generator expression: (entry.startswith('Name=) and entry[5:] or entry for entry in probe_pairs) The last one is nice; you could write the generator object to a file with: outfile = file('probe_pairs_without_Name', 'w') outfile.writelines(entry.startswith('Name=') and entry[5:] or entry for entry in probe_pairs) probe_pairs could also be a file object. That allows to work with huge files without consuming too much memory. inf = file('probe_pairs') out = file('probe_pairs_without_Name', 'w') out.writelines(entry.startswith('Name=') and entry[5:] or entry for entry in inf) inf.close() out.close() Karl -- Please do *not* send copies of replies to me. I read the list From alan.gauld at freenet.co.uk Fri Jan 14 00:27:56 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 00:27:27 2005 Subject: [Tutor] Regular expression re.search() object . Please help References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> Message-ID: <010901c4f9c7$83838820$40b68651@xp> > My list looks like this: List name = probe_pairs > Name=AFFX-BioB-5_at > Cell1=96 369 N control AFFX-BioB-5_at > Cell2=96 370 N control AFFX-BioB-5_at > Cell3=441 3 N control AFFX-BioB-5_at > Cell4=441 4 N control AFFX-BioB-5_at > ... > My Script: > >>> name1 = '[N][a][m][e][=]' Why not just: 'Name=' - the result is the same. > >>> for i in range(len(probe_pairs)): andwhy not just for line in probe_pairs: key = re.match(name1,line) Although I suspect it would be easier still to use line.startswith('Name=') especially combined with the fileinput module. It is really quite good for line by line matching/processing of files, and I assume this data comes from a file originally?. > key = re.match(name1,probe_pairs[i]) > key > <_sre.SRE_Match object at 0x00E37A68> One per line that matches. > when I say group() it prints only one object why? Because the group is the string you are looking for. But I may be missing something since there is no indentation in the post, its hard to tell whats inside and whats outside the loop. > 1. My aim: > To remove those Name=**** lines from my probe_pairs > list > >>> for i in range(len(probe_pairs)): > key = re.match(name1,probe_pairs[i]) > del key That will remove the match object, not the line from the list! To filter the list I'd have thought you'd be better using a list comprehension: filtered = [line for line in probe_pairs if not line.startswith('Name=')] HTH, Alan G. From missive at hotmail.com Fri Jan 14 00:43:11 2005 From: missive at hotmail.com (Lee Harr) Date: Fri Jan 14 00:44:35 2005 Subject: [Tutor] Re: communication between java and python? Message-ID: >I'm trying to communicate between Python and Java and using >os.popen(). But thing dont work...The Java program reads strings from >stdin and the python program just writes to stdout. > >-first call the java program using: > handlers = os.popen('java StdIn) > >-then: > handlers[0].write('string') >-and: > return_value_from_java = >handlers[1].read() > >From the docs on popen: """ popen(command[, mode[, bufsize]]) Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. """ So, I do not believe that you can both write to and read from a file handle opened with popen. Check the docs for popen2 and see if that does what you want. _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/ From alan.gauld at freenet.co.uk Fri Jan 14 01:50:14 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 01:49:47 2005 Subject: [Tutor] Regular expression re.search() object . Please help References: <20050113225724.49815.qmail@web53704.mail.yahoo.com> Message-ID: <012a01c4f9d3$02f0d5d0$40b68651@xp> > I have looked into many books including my favs( > Larning python and Alan Gaulds Learn to program using Yes this is pushing regex a bit further than I show in my book. > What I want to extract: > I want to extract 164:623: > Which always comes after _at: and ends with ; You should be able to use the group() method to extract the matching string out of the match object. > 2. The second pattern/number I want to extract is > 6649: > This always comes after position=. > > How I want to put to desired[]: > > >>> desired > ['>164:623|6649', 'TCATGGCTGACAACCCATCTTGGGA'] > > I write a pattern: > > > pat = '[0-9]*[:][0-9]*' > pat1 = '[_Position][=][0-9]*' > > >>> for line in seq: > pat = '[0-9]*[:][0-9]*' > pat1 = '[_Position][=][0-9]*' pat1 = [_Position] will match any *one* of the characters in _Position, is that really what you want? I suspect its: '_Position=[0-9]*' Which is the fixed string followed by any number(including zero) of digits. > print (re.search(pat,line) and re.search(pat1,line)) This is asking print to print the boolean value of your expression which if the first search fails will be that failure and if it succeeeds will be the result of the second search. Check the section on Functional Programming in my tutor to see why. > <_sre.SRE_Match object at 0x163CAF00> > None Looks like your searches worked but of course you don't have the match objects stored so you can't use them for anything. But I'm suspicious of where the None is coming from... Again without any indentation showing its not totally clear what your code looks like. > What kind of operations can I do on this to get those > two matches: > 164:623 and 6649. I think that if you keep the match objects you can use group() to extract the thing that matched which should be close to what you want. > In Alan Gauld's book, most of the explanation stopped > at > <_sre.SRE_Match object at 0x163CAF00> this level. Yep, personally I use regex to find the line then extract the data I need from the line "manually". Messing around with match objects is something I try to avoid and so left it out of the tutor. :-) Alan G. From guillermo.fernandez.castellanos at gmail.com Fri Jan 14 01:51:48 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Fri Jan 14 01:51:51 2005 Subject: [Tutor] Re: Is Tkinter the Gui in python In-Reply-To: <00ee01c4f9a0$9c91f030$40b68651@xp> References: <00ee01c4f9a0$9c91f030$40b68651@xp> Message-ID: <7d7029e7050113165118ce525e@mail.gmail.com> > > So tkinter is a good module to use if you only want simple widgets, > > but be prepared to switch to something newer when you hit it's > limitations. > This I agree with totally, fortunately I've yet to hit itslimitations > in my fairly simple GUIs. Well... there's also Tix... but I don't know if you consider it as part of Tk or not. It's a bit complicated to get the feel from due to a lack of explicit documentation for python, but once you get the tric about the Tcl->Python conversion, things get pretty smooth. G From wan at walrus.us Fri Jan 14 01:58:42 2005 From: wan at walrus.us (Vincent Wan) Date: Fri Jan 14 01:58:51 2005 Subject: [Tutor] please help me improve my python style Message-ID: <6F3A6630-65C7-11D9-8302-000393B2D990@walrus.us> I have my first python program working (thank you for the help all). Will anyone give me hints on how my program could be improved and on how I could improve my programing style? I want it to be well written as it can be but also as close to self documenting and understandable to the average non-programer biologist. Here is the first draft of my code: # This program simulates the random branching and extinction of linages. # It also mutates a series of characters representing morphology at each branch point # The program replicates the program first described by D.M. Raup and S.G. Gould # 1974 Systematic Zoology 23: 305-322. # written by Vincent Wan with help from tutor@python.org import random debug = 0 # turns debug diagnostic printing on = 1 or off = 0 #constants that control the simulation max_linages = 201 # the number of linage to be evolved + 1 branching_probablity = .1 extinction_probablity = .1 mutation_rate = .5 # initalize living_linages = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]] # the ancestor dead_linages = [] tree = "*0*" time = 0 next_linage = 0 def Mutate_morphology(a_morphology, rate): "Each morphological character has a rate chance it is increased or decreased by 1" a_morphology = a_morphology[:] character = 0 while character < len(a_morphology): if random.random() <= rate: if random.random() < .5: a_morphology[character] = a_morphology[character] - 1 else:a_morphology[character] = a_morphology[character] + 1 character += 1 return a_morphology def Braching(debug, next_linage, time, living_linages, mutation_rate, tree): "With branching_probablity creates a new linage, mutates its morphology, and updates tree." counter = 0 while counter < len(living_linages): if random.random() < branching_probablity: # creates new linage next_linage += 1 new_linage = [next_linage, time, 0, Mutate_morphology(living_linages[counter][3], mutation_rate)] living_linages.append(new_linage) # updates linage tree target = '*' + str(living_linages[counter][0]) + '*' replacement = '(' + target + ',*' + str(new_linage[0]) + '*)' tree = tree.replace(target, replacement) counter += 1 if debug: print 'at time ', time, ' living_linages: ', [linage[0] for linage in living_linages] return (next_linage, living_linages, tree) def Extinction(debug, extinction_probablity, living_linages, time, dead_linages): "With extinction_probablity kills living species and adds them to the dead list" counter = 0 while counter < len(living_linages): if random.random() < extinction_probablity: newly_dead = living_linages[counter] newly_dead[2] = time dead_linages.append(newly_dead) living_linages.remove(living_linages[counter]) if len(living_linages) == 0: break # when the last living_linages goes extinct exit counter += 1 if debug: print 'at time ', time, ' dead_linages : ', [linage[0] for linage in dead_linages] return (living_linages, dead_linages) def Print_results(next_linage, max_linages, living_linages, dead_linages, tree): "prints number of linages, the data about the linages living and dead, and the tree" if next_linage < max_linages: print '\nall extinct with only ', next_linage + 1, ' linage of ', max_linages else: print '\n', max_linages - 1, ' linages evolved' print '\nliving linages:' for each in living_linages: print 'linage', each[0], 'born', each[1], 'morphology:', each[3] print '\nextinct linages: ' for each in dead_linages: print 'linage', each[0], 'born', each[1], '- died', each[2] print 'morphology:', each[3] tree = tree.replace('*','') print '\ntree (in New Hampshire form: )', tree # main loop while next_linage < max_linages: # handles branching (next_linage, living_linages, tree) = Braching(debug, next_linage, time, living_linages, mutation_rate, tree) # handles extinction (living_linages, dead_linages) = Extinction(debug, extinction_probablity, living_linages, time, dead_linages) if len(living_linages) == 0: break # when the last living_linages goes extinct exit time += 1 Print_results(next_linage, max_linages, living_linages, dead_linages, tree) Thank you, Vincent ------------------------------------------------------------------------ -------------- PhD Candidate Committee on the Conceptual and Historical Studies of Science University of Chicago PO Box 73727 Fairbanks, AK 99707 wan AT walrus DOT us (change CAPS to @ and . ) From guillermo.fernandez.castellanos at gmail.com Fri Jan 14 02:07:14 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Fri Jan 14 02:07:17 2005 Subject: [Tutor] Tix and Table printing In-Reply-To: References: <7d7029e70501130300313c39ea@mail.gmail.com> Message-ID: <7d7029e705011317073c11f67a@mail.gmail.com> Hi, Exactly what I was looking for! I'm not yet used to search in the cookbook... and I though such "basic" widget would have been implemented directly in Tk or Tix... Thanks a lot to both of you! G On Thu, 13 Jan 2005 12:06:51 -0800 (PST), Danny Yoo wrote: > > > On Thu, 13 Jan 2005, Guillermo Fernandez Castellanos wrote: > > > Is there any "table" frame that I am unaware of? > > > Hi Guillermo, > > There are some recipes for making a Tkinter table widget; here's one: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52266 > > > There's a Tkinter wiki page that has links to a few more Table > implementations: > > http://tkinter.unpythonic.net/wiki/FrontPage > > Hope this helps! > > From jeff at ccvcorp.com Fri Jan 14 03:33:51 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Fri Jan 14 03:28:38 2005 Subject: [Tutor] Re: communication between java and python? In-Reply-To: References: Message-ID: <41E72F8F.90706@ccvcorp.com> Lee Harr wrote: >> I'm trying to communicate between Python and Java and using >> os.popen(). But thing dont work...The Java program reads strings from >> stdin and the python program just writes to stdout. >> >> [...] > > """ > popen(command[, mode[, bufsize]]) > Open a pipe to or from command. The return value is an open file > object connected to the pipe, which can be read or written > depending on whether mode is 'r' (default) or 'w'. > """ > > So, I do not believe that you can both write to and read from > a file handle opened with popen. Not only that, but access to the file-like pipe objects is probably buffered. You'll want to call pipe.flush() after writing to it, or the child process won't actually see what you've written. Jeff Shannon Technician/Programmer Credit International From cyresse at gmail.com Fri Jan 14 03:52:15 2005 From: cyresse at gmail.com (Liam Clarke) Date: Fri Jan 14 03:52:18 2005 Subject: [Tutor] Re: Is Tkinter the Gui in python In-Reply-To: <7d7029e7050113165118ce525e@mail.gmail.com> References: <00ee01c4f9a0$9c91f030$40b68651@xp> <7d7029e7050113165118ce525e@mail.gmail.com> Message-ID: On Fri, 14 Jan 2005 09:51:48 +0900, Guillermo Fernandez Castellanos wrote: > > > So tkinter is a good module to use if you only want simple widgets, > > > but be prepared to switch to something newer when you hit it's > > limitations. > > This I agree with totally, fortunately I've yet to hit itslimitations > > in my fairly simple GUIs. > Well... there's also Tix... but I don't know if you consider it as > part of Tk or not. > > It's a bit complicated to get the feel from due to a lack of explicit > documentation for python, but once you get the tric about the > Tcl->Python conversion, things get pretty smooth. Spectix is pretty usable. -- '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. From keridee at jayco.net Fri Jan 14 04:02:13 2005 From: keridee at jayco.net (Jacob S.) Date: Fri Jan 14 04:03:17 2005 Subject: [Tutor] Regular expression re.search() object . Please help References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> <41E6E842.2040206@ccvcorp.com> Message-ID: <003b01c4f9e5$991dcab0$375428cf@JSLAPTOP> I assume that both you and Liam are using previous-er versions of python? Now files are iterators by line and you can do this. openFile = open("probe_pairs.txt","r") indexesToRemove = [] for line in openFile: if line.startswith("Name="): line = '' ## Ooops, this won't work because it just changes what line references or points to or whatever, it doesn't ##actually change the object openFile.close() I think this is a great flaw with using for element in list instead of for index in range(len(list)) Of course you later put IMHO better solutions (list comprehensions,etc.) -- I just wanted to point out that files have been iterators for a few versions now and that is being used more and more. It also saves the memory problem of using big files and reading them all at once with readlines. Jacob > Liam Clarke wrote: > > > openFile=file("probe_pairs.txt","r") > > probe_pairs=openFile.readlines() > > > > openFile.close() > > > > indexesToRemove=[] > > > > for lineIndex in range(len(probe_pairs)): > > > > if probe_pairs[lineIndex].startswith("Name="): > > probe_pairs[lineIndex]='' > > If the intent is simply to remove all lines that begin with "Name=", > and setting those lines to an empty string is just shorthand for that, > it'd make more sense to do this with a filtering list comprehension: > > openfile = open("probe_pairs.txt","r") > probe_pairs = openfile.readlines() > openfile.close() > > probe_pairs = [line for line in probe_pairs \ > if not line.startswith('Name=')] > > > (The '\' line continuation isn't strictly necessary, because the open > list-comp will do the same thing, but I'm including it for > readability's sake.) > > If one wants to avoid list comprehensions, you could instead do: > > openfile = open("probe_pairs.txt","r") > probe_pairs = [] > > for line in openfile.readlines(): > if not line.startswith('Name='): > probe_pairs.append(line) > > openfile.close() > > Either way, lines that start with 'Name=' get thrown away, and all > other lines get kept. > > Jeff Shannon > Technician/Programmer > Credit International > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From cyresse at gmail.com Fri Jan 14 04:17:28 2005 From: cyresse at gmail.com (Liam Clarke) Date: Fri Jan 14 04:17:31 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <003b01c4f9e5$991dcab0$375428cf@JSLAPTOP> References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> <41E6E842.2040206@ccvcorp.com> <003b01c4f9e5$991dcab0$375428cf@JSLAPTOP> Message-ID: I'm using 2.3, I was trying to be as explicit as possible. *grin* On Thu, 13 Jan 2005 22:02:13 -0500, Jacob S. wrote: > I assume that both you and Liam are using previous-er versions of python? > Now files are iterators by line and you can do this. > > openFile = open("probe_pairs.txt","r") > indexesToRemove = [] > for line in openFile: > if line.startswith("Name="): > line = '' ## Ooops, this won't work because it just changes what > line references or points to or whatever, it doesn't > ##actually change the object > openFile.close() > > I think this is a great flaw with using for element in list instead of for > index in range(len(list)) > > Of course you later put IMHO better solutions (list comprehensions,etc.) -- > I just wanted to point out that files have been > iterators for a few versions now and that is being used more and more. It > also saves the memory problem of using big files > and reading them all at once with readlines. > > Jacob > > > Liam Clarke wrote: > > > > > openFile=file("probe_pairs.txt","r") > > > probe_pairs=openFile.readlines() > > > > > > openFile.close() > > > > > > indexesToRemove=[] > > > > > > for lineIndex in range(len(probe_pairs)): > > > > > > if probe_pairs[lineIndex].startswith("Name="): > > > probe_pairs[lineIndex]='' > > > > If the intent is simply to remove all lines that begin with "Name=", > > and setting those lines to an empty string is just shorthand for that, > > it'd make more sense to do this with a filtering list comprehension: > > > > openfile = open("probe_pairs.txt","r") > > probe_pairs = openfile.readlines() > > openfile.close() > > > > probe_pairs = [line for line in probe_pairs \ > > if not line.startswith('Name=')] > > > > > > (The '\' line continuation isn't strictly necessary, because the open > > list-comp will do the same thing, but I'm including it for > > readability's sake.) > > > > If one wants to avoid list comprehensions, you could instead do: > > > > openfile = open("probe_pairs.txt","r") > > probe_pairs = [] > > > > for line in openfile.readlines(): > > if not line.startswith('Name='): > > probe_pairs.append(line) > > > > openfile.close() > > > > Either way, lines that start with 'Name=' get thrown away, and all > > other lines get kept. > > > > Jeff Shannon > > Technician/Programmer > > Credit International > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > 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. From dyoo at hkn.eecs.berkeley.edu Fri Jan 14 07:38:09 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 14 07:38:12 2005 Subject: [Tutor] communication between java and python? In-Reply-To: Message-ID: On Thu, 13 Jan 2005, joeri honnef wrote: > I'm trying to communicate between Python and Java and using os.popen(). > But thing dont work...The Java program reads strings from stdin and the > python program just writes to stdout. Hi Joeri, You may want to look at: http://www.python.org/doc/lib/popen2-flow-control.html which explains some of the tricky flow-control issues that can happen during inter-process communication. As far as I can tell, the Java code that you have to read a character from standard input: /******/ // return EOF if end of file or IO error private static void readC() { try { c = System.in.read(); } catch(IOException e) { c = EOF; } } /******/ has the possibility of blocking if the input stream has not been closed. Other folks have already commented that, in the interaction that you're doing with the program: > handlers = os.popen('java StdIn) > handlers[0].write('string') > return_value_from_java = handlers[1].read() your program needs to use something like popen2 to get separate input and output files. But the program also needs to be aware to close the streams, or else risk the potential of deadlocking. If it helps to see why, imagine that we modify: > handlers[0].write('string') > return_value_from_java = handlers[1].read() to something like: ### handlers[0].write('s') handlers[0].write('t') handlers[0].write('rin') handlers[0].write('g') ### We can call write() several times, and as far as the Java process knows, it might need to expect more input. That's why closing the input stream is necessary, to signal to the Java program, through EOF, that there's no more characters to read. If you have more questions, please feel free to ask! From alan.gauld at freenet.co.uk Fri Jan 14 09:18:33 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 09:17:56 2005 Subject: [Tutor] Re: Is Tkinter the Gui in python References: <00ee01c4f9a0$9c91f030$40b68651@xp> <7d7029e7050113165118ce525e@mail.gmail.com> Message-ID: <014a01c4fa11$a416c440$40b68651@xp> > Well... there's also Tix... but I don't know if you consider it as > part of Tk or not. > > It's a bit complicated to get the feel from due to a lack of explicit > documentation for python, but once you get the tric about the > Tcl->Python conversion, things get pretty smooth. The Tcl-Python conversion isn't a problem, I use the O'Reilly "Tcl/Tk in a Nutshell" that for standard Tkinter anyway. But the Python WegaWidgets is probably the first port of call for extensions to Tkinter. Tix would be my third option. But in practice I've never even used PMW! Alan G. From dimitri.dor at fssintl.com Fri Jan 14 09:30:46 2005 From: dimitri.dor at fssintl.com (Dimitri D'Or) Date: Fri Jan 14 09:31:08 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <1ff2dfbf05011310281ef9ea00@mail.gmail.com> Message-ID: <200501140830.j0E8UjtC028188@outmx009.isp.belgacom.be> Hello Michael, Thank you for your answer. Actually, my question is not specific to interactive sessions. I've written a script that loads some modules, create variables and show figures. What I would like to find, is a command for clearing all the created variables and close all figures before beginning the execution of the script. This command will be placed at the beginning of the script and automatically reinitialize the namespace. The utility of this command is to guarantee that all the variables that are available in the namespace after the execution of the script were created by it and are not remainders from older commands or scripts. Do you think it is possible to do such a thing? I'm coming from the Matlab world and I want to find equivalents for the "clear" and "close" matlab commands. Thank you for your help, Dimitri -----Message d'origine----- De?: Michael Janssen [mailto:mi.janssen@gmail.com] Envoy??: jeudi 13 janvier 2005 19:29 ??: Dimitri D'Or Cc?: tutor@python.org Objet?: Re: [Tutor] reinitializing namespace On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or wrote: > For some purpose, I would like to reinitialize the main namespace, i.e. I want > to delete all the variables I have created through the use of functions or > keyboard entries. Hello Dimiti, sound like you're talking about an interactive session. Otherwise (within a script) it would be a really bad idea to try this (better put your stuff into functions, that don't create global variables). Even in an interactive session it sounds like a not that brilliant idea, especially since I can't think of a way other than using exec "del %s" % key for appropriate keys from globals(). Finding "appropiate" keys is one tricky thing. Why not end your ugly python session and start a new one? You can define all your imports in the python startfile (on Linux, consult python manpage. On Windows, I don't know). You can also define useful functions or variables in your python startfile. This way, you're really shure that all ugly variables are away without del'iting anything important. regards Michael From alan.gauld at freenet.co.uk Fri Jan 14 09:41:48 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 09:41:11 2005 Subject: [Tutor] please help me improve my python style References: <6F3A6630-65C7-11D9-8302-000393B2D990@walrus.us> Message-ID: <014f01c4fa14$e36455b0$40b68651@xp> > # This program simulates the random branching and extinction of linages. > # It also mutates a series of characters representing morphology at > each branch point > # The program replicates the program first described by D.M. Raup and > S.G. Gould > # 1974 Systematic Zoology 23: 305-322. > # written by Vincent Wan with help from tutor@python.org Put the above set of comments in triple quotes and it will become a *docstring* and hence accessible via Pythons help() function... > import random > > debug = 0 # turns debug diagnostic printing on = 1 or off = 0 > > #constants that control the simulation > max_linages = 201 # the number of linage to be evolved + 1 > branching_probablity = .1 > extinction_probablity = .1 > mutation_rate = .5 Constants should be in UPPER_CASE by convention. > # initalize > living_linages = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]] # the > ancestor > dead_linages = [] > tree = "*0*" > time = 0 > next_linage = 0 > > def Mutate_morphology(a_morphology, rate): the fashion for function naming is to separewt words with capitals so this would be mutateMorphology(...) Names starting upper case are conventionally classes. So a Python reader may think the function call is a class instatiation as it is. > "Each morphological character has a rate chance it is increased or > decreased by 1" > a_morphology = a_morphology[:] > character = 0 > while character < len(a_morphology): > if random.random() <= rate: > if random.random() < .5: .5 would be clearer as 0.5, the point on its own is easily missed. > a_morphology[character] = a_morphology[character] - 1 > else:a_morphology[character] = a_morphology[character] + 1 You could use += and -+ here to save a wee bit of space. > character += 1 > return a_morphology > > def Braching(debug, next_linage, time, living_linages, mutation_rate, > tree): > "With branching_probablity creates a new linage, mutates its > morphology, and updates tree." > counter = 0 > while counter < len(living_linages): > if random.random() < branching_probablity: > # creates new linage > next_linage += 1 > new_linage = [next_linage, time, 0, > Mutate_morphology(living_linages[counter][3], mutation_rate)] better to line this up with the other parameters: new_linage = [next_linage, time, 0, Mutate_morphology(living_linages[counter][3], mutation_rate)] it keeps the block structure clear. > living_linages.append(new_linage) > # updates linage tree > target = '*' + str(living_linages[counter][0]) + '*' > replacement = '(' + target + ',*' + str(new_linage[0]) + > '*)' You could use string formatting to do both lines in one: replacement = "(*%s*,*%s*)" % (living_linages[counter][0], new_linage[0]) it will be faster too. > tree = tree.replace(target, replacement) Ah, but here you use both bits.... Ok, but you could still use formatting to create the two strings. > counter += 1 > if debug: print 'at time ', time, ' living_linages: ', > [linage[0] for linage in living_linages] > return (next_linage, living_linages, tree) > > def Extinction(debug, extinction_probablity, living_linages, time, > dead_linages): > "With extinction_probablity kills living species and adds them to > the dead list" > counter = 0 > while counter < len(living_linages): > if random.random() < extinction_probablity: > newly_dead = living_linages[counter] > newly_dead[2] = time > dead_linages.append(newly_dead) > living_linages.remove(living_linages[counter]) > if len(living_linages) == 0: break # when the last > living_linages goes extinct exit > counter += 1 there may be a subtle bug here. IF you remnove an entry above then all the elements shuffle up one. If you now increment counter you will in effect step over one entry without checking it. Is that what you want? > if debug: print 'at time ', time, ' dead_linages : ', [linage[0] > for linage in dead_linages] > return (living_linages, dead_linages) > > def Print_results(next_linage, max_linages, living_linages, > dead_linages, tree): > "prints number of linages, the data about the linages living and > dead, and the tree" > if next_linage < max_linages: > print '\nall extinct with only ', next_linage + 1, ' linage of > ', max_linages > else: > print '\n', max_linages - 1, ' linages evolved' > print '\nliving linages:' > for each in living_linages: > print 'linage', each[0], 'born', each[1], 'morphology:', each[3] > print '\nextinct linages: ' > for each in dead_linages: > print 'linage', each[0], 'born', each[1], '- died', each[2] > print 'morphology:', each[3] > tree = tree.replace('*','') > print '\ntree (in New Hampshire form: )', tree > > # main loop > while next_linage < max_linages: > # handles branching > (next_linage, living_linages, tree) = Braching(debug, next_linage, > time, living_linages, mutation_rate, tree) > # handles extinction > (living_linages, dead_linages) = Extinction(debug, > extinction_probablity, living_linages, time, dead_linages) > if len(living_linages) == 0: break # when the last > living_linages goes extinct exit > time += 1 > > Print_results(next_linage, max_linages, living_linages, dead_linages, > tree) might be nicer to put the main code in a main function and then use the magic incantation if __name__ == '__main__': main() to execute it. It allows for easier extension of your code by using your file as a module, and also allows you to experiment with it from the Python prompt by importing it without the main code executing. These are nearly all style comments and as such are subjective and open do debate, but I hope they are helpful. Looks like another good candidate for Useless Python... Alan G. From alan.gauld at freenet.co.uk Fri Jan 14 09:47:49 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 09:47:12 2005 Subject: [Tutor] Tix and Table printing References: <7d7029e70501130300313c39ea@mail.gmail.com> <7d7029e705011317073c11f67a@mail.gmail.com> Message-ID: <015401c4fa15$bad3b3b0$40b68651@xp> > I'm not yet used to search in the cookbook... and I though such > "basic" widget would have been implemented directly in Tk or Tix... A bit of historic perspective. John Ousterhout invented TCl/Tk to provide a control language for his electrical engineering projects. Thus its focus is on GUIs to control hardware rather than display data, so a table widget wasn't an obvious requirement. In fact table widgets didn't become common in GUI toolkits till the mid '90's when Visual Basic introduced one, then Delphi, then everyone started adding them when it became obvious how useful they were. But the first version of MFC (the Windows native GUI) and also the Smalltalk GUI (one of the first ever) had no table widget, programmers had to craft their own! Tk was written in the 80's so given its origins was not likely to have a table. Of course it would be nice if they added one now!!! Alan G. From kent37 at tds.net Fri Jan 14 12:02:36 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 14 12:02:46 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <200501140830.j0E8UjtC028188@outmx009.isp.belgacom.be> References: <200501140830.j0E8UjtC028188@outmx009.isp.belgacom.be> Message-ID: <41E7A6CC.7000600@tds.net> I think you may be looking for something that is not needed in Python or that you can easily do another way. If you are running a script from the command line, e.g. > python myscript.py then myscript.py will have a completely fresh runtime environment every time you call it. If you are running the script by importing in another module, then you can use reload() to reload the imported script. That will reinitialize the namespace of the module, which for your purposes *is* the global namespace of the module. Please give us more details about how you will run the script and what kind of problem you anticipate. Kent Dimitri D'Or wrote: > Hello Michael, > > Thank you for your answer. Actually, my question is not specific to > interactive sessions. I've written a script that loads some modules, create > variables and show figures. What I would like to find, is a command for > clearing all the created variables and close all figures before beginning > the execution of the script. This command will be placed at the beginning of > the script and automatically reinitialize the namespace. The utility of this > command is to guarantee that all the variables that are available in the > namespace after the execution of the script were created by it and are not > remainders from older commands or scripts. Do you think it is possible to do > such a thing? > > I'm coming from the Matlab world and I want to find equivalents for the > "clear" and "close" matlab commands. > > Thank you for your help, > > Dimitri > > > -----Message d'origine----- > De : Michael Janssen [mailto:mi.janssen@gmail.com] > Envoy? : jeudi 13 janvier 2005 19:29 > ? : Dimitri D'Or > Cc : tutor@python.org > Objet : Re: [Tutor] reinitializing namespace > > On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or > wrote: > > >>For some purpose, I would like to reinitialize the main namespace, i.e. I > > want > >>to delete all the variables I have created through the use of functions or >>keyboard entries. > > > Hello Dimiti, > > sound like you're talking about an interactive session. Otherwise > (within a script) it would be a really bad idea to try this (better > put your stuff into functions, that don't create global variables). > > Even in an interactive session it sounds like a not that brilliant > idea, especially since I can't think of a way other than using exec > "del %s" % key for appropriate keys from globals(). Finding > "appropiate" keys is one tricky thing. > > Why not end your ugly python session and start a new one? You can > define all your imports in the python startfile (on Linux, consult > python manpage. On Windows, I don't know). You can also define useful > functions or variables in your python startfile. This way, you're > really shure that all ugly variables are away without del'iting > anything important. > > regards > Michael > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From dimitri.dor at fssintl.com Fri Jan 14 14:01:50 2005 From: dimitri.dor at fssintl.com (Dimitri D'Or) Date: Fri Jan 14 14:02:06 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <41E7A6CC.7000600@tds.net> Message-ID: <200501141301.j0ED1o9Z024691@outmx015.isp.belgacom.be> Dear Kent, Consider I'm working with an interactive session during which I have already run some scripts. Those scripts have produced several variables, say, e.g., a and b. Now I execute myscript which also creates variables named a and b, but with a possibly different type or content. To be sure that the variables I find in my workspace after the execution of myscript were created by this script and not by another one, I want to reset the workspace. Otherwise, how can I make a difference between variables having the same name but created by two different scripts? Maybe this question is too much related to my Matlab experience and not relevant into Python's philosophy. Please tell me what I should do. Dimitri -----Message d'origine----- De?: tutor-bounces+dimitri.dor=fssintl.com@python.org [mailto:tutor-bounces+dimitri.dor=fssintl.com@python.org] De la part de Kent Johnson Envoy??: vendredi 14 janvier 2005 12:03 Cc?: tutor@python.org Objet?: Re: [Tutor] reinitializing namespace I think you may be looking for something that is not needed in Python or that you can easily do another way. If you are running a script from the command line, e.g. > python myscript.py then myscript.py will have a completely fresh runtime environment every time you call it. If you are running the script by importing in another module, then you can use reload() to reload the imported script. That will reinitialize the namespace of the module, which for your purposes *is* the global namespace of the module. Please give us more details about how you will run the script and what kind of problem you anticipate. Kent Dimitri D'Or wrote: > Hello Michael, > > Thank you for your answer. Actually, my question is not specific to > interactive sessions. I've written a script that loads some modules, create > variables and show figures. What I would like to find, is a command for > clearing all the created variables and close all figures before beginning > the execution of the script. This command will be placed at the beginning of > the script and automatically reinitialize the namespace. The utility of this > command is to guarantee that all the variables that are available in the > namespace after the execution of the script were created by it and are not > remainders from older commands or scripts. Do you think it is possible to do > such a thing? > > I'm coming from the Matlab world and I want to find equivalents for the > "clear" and "close" matlab commands. > > Thank you for your help, > > Dimitri > > > -----Message d'origine----- > De : Michael Janssen [mailto:mi.janssen@gmail.com] > Envoy? : jeudi 13 janvier 2005 19:29 > ? : Dimitri D'Or > Cc : tutor@python.org > Objet : Re: [Tutor] reinitializing namespace > > On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or > wrote: > > >>For some purpose, I would like to reinitialize the main namespace, i.e. I > > want > >>to delete all the variables I have created through the use of functions or >>keyboard entries. > > > Hello Dimiti, > > sound like you're talking about an interactive session. Otherwise > (within a script) it would be a really bad idea to try this (better > put your stuff into functions, that don't create global variables). > > Even in an interactive session it sounds like a not that brilliant > idea, especially since I can't think of a way other than using exec > "del %s" % key for appropriate keys from globals(). Finding > "appropiate" keys is one tricky thing. > > Why not end your ugly python session and start a new one? You can > define all your imports in the python startfile (on Linux, consult > python manpage. On Windows, I don't know). You can also define useful > functions or variables in your python startfile. This way, you're > really shure that all ugly variables are away without del'iting > anything important. > > regards > Michael > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From stbru at teksavvy.com Fri Jan 14 14:11:54 2005 From: stbru at teksavvy.com (=?ISO-8859-1?Q?St=E9phane_Brunet?=) Date: Fri Jan 14 14:11:58 2005 Subject: [Tutor] l10n and Windows Message-ID: <41E7C51A.8000808@teksavvy.com> Hi, After searching a while for a solution on the web and the archive of this newgroups, I haven't found any answer to my questions... So here are they... Here is a command line session of Python on a Windows XP computer : Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import locale >>> locale.getdefaultlocale() ('fr_CA', 'cp1252') >>> locale.setlocale(locale.LC_ALL, '') 'French_Canada.1252' >>> locale.setlocale(locale.LC_ALL, 'en_CA') Traceback (most recent call last): File "", line 1, in ? File "C:\Python24\Lib\locale.py", line 379, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting >>> locale.setlocale(locale.LC_ALL, 'English_Canada') 'English_Canada.1252' >>> locale.getlocale() ['English_Canada', '1252'] >>> The name of the default locale conform to RFC-1766 (http://www.faqs.org/rfcs/rfc1766.html) when returned by locale.getdefaultlocale whereas setting the locale as 'en_CA' isn't supported (on Windows). Afterwards, locale.getlocale() isn't returning a "standard" RFC-1766 locale at all... Is this a "feature" or a bug ? Where has portability gone ? Moreover on the documentation on locale.getlocale(), it is written: """ Except for the code 'C', the language code corresponds to RFC 1766 """ Thanks for you help. St?phane From kent37 at tds.net Fri Jan 14 14:44:41 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 14 14:44:54 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <200501141301.j0ED1o9Z024691@outmx015.isp.belgacom.be> References: <200501141301.j0ED1o9Z024691@outmx015.isp.belgacom.be> Message-ID: <41E7CCC9.70005@tds.net> I'm still not sure why there is a problem. How are you running the scripts from the interactive session? If you are importing the script, its variables will be in its own namespace. If you are creating variables interactively yourself, your best bet is probably to just restart the interpreter. (If you use IDLE you can reset the interpreter from a menu without having to quit IDLE.) It sounds like you are just coming to Python from Matlab and you are anticipating the same kinds of problems in Python as you are used to in Matlab. Have you actually worked with a script in the interpreter? Can you show us a sample interaction? Are you having a problem with name collisions in Python? It really sounds to me like this is either a non-issue or that you should just restart the interpreter when you need a clean slate. Here is an example of isolation between modules and reloading. Suppose A1.py and A2.py both contain this code (except A2 prints 'A2.a ='): ## A1.py a = 0 def bumpA(): global a a += 1 print 'A1.a =', a I can use these modules interactively like this: >>> import A1 >>> A1.bumpA() A1.a = 1 >>> A1.bumpA() A1.a = 2 >>> A1.bumpA() A1.a = 3 >>> A1.bumpA() A1.a = 4 >>> import A2 >>> A2.bumpA() A2.a = 1 >>> >>> A2.bumpA() A2.a = 2 Notice that A1.a and A2.a are different - each module has its own namespace, there is no collision. If I reload a module it gets reset to its initial state: >>> reload(A2) >>> A2.bumpA() A2.a = 1 Kent Dimitri D'Or wrote: > Dear Kent, > > Consider I'm working with an interactive session during which I have already > run some scripts. Those scripts have produced several variables, say, e.g., > a and b. Now I execute myscript which also creates variables named a and b, > but with a possibly different type or content. To be sure that the variables > I find in my workspace after the execution of myscript were created by this > script and not by another one, I want to reset the workspace. > > Otherwise, how can I make a difference between variables having the same > name but created by two different scripts? > > Maybe this question is too much related to my Matlab experience and not > relevant into Python's philosophy. Please tell me what I should do. > > Dimitri > > -----Message d'origine----- > De : tutor-bounces+dimitri.dor=fssintl.com@python.org > [mailto:tutor-bounces+dimitri.dor=fssintl.com@python.org] De la part de Kent > Johnson > Envoy? : vendredi 14 janvier 2005 12:03 > Cc : tutor@python.org > Objet : Re: [Tutor] reinitializing namespace > > I think you may be looking for something that is not needed in Python or > that you can easily do > another way. > > If you are running a script from the command line, e.g. > > python myscript.py > > then myscript.py will have a completely fresh runtime environment every time > you call it. > > If you are running the script by importing in another module, then you can > use reload() to reload > the imported script. That will reinitialize the namespace of the module, > which for your purposes > *is* the global namespace of the module. > > Please give us more details about how you will run the script and what kind > of problem you anticipate. > > Kent > > Dimitri D'Or wrote: > >>Hello Michael, >> >>Thank you for your answer. Actually, my question is not specific to >>interactive sessions. I've written a script that loads some modules, > > create > >>variables and show figures. What I would like to find, is a command for >>clearing all the created variables and close all figures before beginning >>the execution of the script. This command will be placed at the beginning > > of > >>the script and automatically reinitialize the namespace. The utility of > > this > >>command is to guarantee that all the variables that are available in the >>namespace after the execution of the script were created by it and are not >>remainders from older commands or scripts. Do you think it is possible to > > do > >>such a thing? >> >>I'm coming from the Matlab world and I want to find equivalents for the >>"clear" and "close" matlab commands. >> >>Thank you for your help, >> >>Dimitri >> >> >>-----Message d'origine----- >>De : Michael Janssen [mailto:mi.janssen@gmail.com] >>Envoy? : jeudi 13 janvier 2005 19:29 >>? : Dimitri D'Or >>Cc : tutor@python.org >>Objet : Re: [Tutor] reinitializing namespace >> >>On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or >> wrote: >> >> >> >>>For some purpose, I would like to reinitialize the main namespace, i.e. I >> >>want >> >> >>>to delete all the variables I have created through the use of functions or >>>keyboard entries. >> >> >>Hello Dimiti, >> >>sound like you're talking about an interactive session. Otherwise >>(within a script) it would be a really bad idea to try this (better >>put your stuff into functions, that don't create global variables). >> >>Even in an interactive session it sounds like a not that brilliant >>idea, especially since I can't think of a way other than using exec >>"del %s" % key for appropriate keys from globals(). Finding >>"appropiate" keys is one tricky thing. >> >>Why not end your ugly python session and start a new one? You can >>define all your imports in the python startfile (on Linux, consult >>python manpage. On Windows, I don't know). You can also define useful >>functions or variables in your python startfile. This way, you're >>really shure that all ugly variables are away without del'iting >>anything important. >> >>regards >>Michael >> >> >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > From flaxeater at yahoo.com Fri Jan 14 17:24:21 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Fri Jan 14 17:24:24 2005 Subject: [Tutor] file-like object Message-ID: <20050114162421.74326.qmail@web54304.mail.yahoo.com> I have created a file-like object out of a triple quoted string. I was wondering if there is a better way to implement readline than what I have below? It just doesn't seem like a very good way to do this. class _macroString(object): def __init__(self,s): self.macro=s self.list=self.macro.split("\n") for n,v in enumerate(self.list): self.list[n]=v+'\n' def readline(self,n=[-1]): n[0]+=1 return self.list[n[0]] def __str__(self): return str(self.list) def __len__(self): return len(self.list) __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From isrgish at fastem.com Fri Jan 14 17:42:32 2005 From: isrgish at fastem.com (Isr Gish) Date: Fri Jan 14 17:42:59 2005 Subject: [Tutor] Referer file from import Message-ID: <20050114164257.DFD061E400F@bag.python.org> Chad Crabtree wrote: >Is there a way to know what the path of the file is that imported a >module? I've tried __file__ and playing with globals() but I can't >seem >to crack this. > There most be a way because the Tracebacks give us this info. You want to take a look at the 'inspect' module. I belive looking at the source of the traceback module may help alot. All the best, Isr From kent37 at tds.net Fri Jan 14 17:52:25 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 14 17:52:34 2005 Subject: [Tutor] file-like object In-Reply-To: <20050114162421.74326.qmail@web54304.mail.yahoo.com> References: <20050114162421.74326.qmail@web54304.mail.yahoo.com> Message-ID: <41E7F8C9.6050701@tds.net> Best: use the StringIO or cStringIO module instead, this is exactly what it is for. If you really need len() you could maybe subclass StringIO to do what you want. Next best: Use an iterator. Something like this (Warning! not tested!): class _macroString(object): def __init__(self,s): self.macro=s self.list=[ line+'\n' for line in self.macro.split("\n") ] self._iter = iter(self.list) def readline(self): try: return self._iter.next() except StopIteration: return '' def __str__(self): return str(self.list) def __len__(self): return len(self.list) Note that your implementation of readline will raise IndexError when there are no more lines which is not correct behaviour. Kent Chad Crabtree wrote: > I have created a file-like object out of a triple quoted string. I > was > wondering if there is a better way to implement readline than what I > have below? It just doesn't seem like a very good way to do this. > > class _macroString(object): > def __init__(self,s): > self.macro=s > self.list=self.macro.split("\n") > for n,v in enumerate(self.list): > self.list[n]=v+'\n' > def readline(self,n=[-1]): > n[0]+=1 > return self.list[n[0]] > def __str__(self): > return str(self.list) > def __len__(self): > return len(self.list) > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Fri Jan 14 17:56:42 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 14 17:56:46 2005 Subject: [Tutor] Referer file from import In-Reply-To: <20050114164257.DFD061E400F@bag.python.org> References: <20050114164257.DFD061E400F@bag.python.org> Message-ID: <41E7F9CA.8020301@tds.net> You could use trackback.extract_stack() to get the current stack trace. If you inspect this from within the imported module you could probably figure out who is importing you. Do you really want the module where the import was done (the place where the import statement is)? Or are you trying to find the file containing the imported module? Kent Isr Gish wrote: > Chad Crabtree wrote: > >Is there a way to know what the path of the file is that imported a > >module? I've tried __file__ and playing with globals() but I can't > >seem > >to crack this. > > > > There most be a way because the Tracebacks give us this info. > You want to take a look at the 'inspect' module. I belive looking at the source of the traceback module may help alot. > > All the best, > Isr > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From flaxeater at yahoo.com Fri Jan 14 18:05:58 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Fri Jan 14 18:06:02 2005 Subject: [Tutor] Referer file from import Message-ID: <20050114170558.87952.qmail@web54304.mail.yahoo.com> Ok I will investigate this. Thank you that is probably what I needed. I am trying to make a macro expander for python based on BOO's facility for this. I thought it was neat. In addition I think it would be helpful to simulate adding keywords so that all these bloggers talking about proposed syntax could actually try it. But mostly I want to implement the with keyword with g: append(1) append(2) append('woah') Kent Johnson wrote: > You could use trackback.extract_stack() to get the current stack > trace. If you inspect this from within the imported module you could > probably figure out who is importing you. > > Do you really want the module where the import was done (the place > where the import statement is)? Or are you trying to find the file > containing the imported module? > > Kent > > Isr Gish wrote: > >> Chad Crabtree wrote: >> >Is there a way to know what the path of the file is that imported >> a >module? I've tried __file__ and playing with globals() but I >> can't >> >seem >to crack this. >> > > __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo From flaxeater at yahoo.com Fri Jan 14 18:08:21 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Fri Jan 14 18:08:24 2005 Subject: [Tutor] file-like object Message-ID: <20050114170821.36395.qmail@web54307.mail.yahoo.com> Thank you KentBot. That was what I wanted. Kent Johnson wrote: > Best: use the StringIO or cStringIO module instead, this is exactly > what it is for. If you really need len() you could maybe subclass > StringIO to do what you want. > > Next best: Use an iterator. Something like this (Warning! not tested!): > class _macroString(object): > def __init__(self,s): > self.macro=s > self.list=[ line+'\n' for line in self.macro.split("\n") ] > self._iter = iter(self.list) > def readline(self): > try: > return self._iter.next() > except StopIteration: > return '' > def __str__(self): > return str(self.list) > def __len__(self): > return len(self.list) > > Note that your implementation of readline will raise IndexError when > there are no more lines which is not correct behaviour. > > Kent > > __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo From alan.gauld at freenet.co.uk Fri Jan 14 18:20:15 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 14 18:19:45 2005 Subject: [Tutor] file-like object References: <20050114162421.74326.qmail@web54304.mail.yahoo.com> Message-ID: <018a01c4fa5d$56f8b960$40b68651@xp> > class _macroString(object): > def __init__(self,s): > self.macro=s > self.list=self.macro.split("\n") > for n,v in enumerate(self.list): > self.list[n]=v+'\n' > def readline(self,n=[-1]): > n[0]+=1 > return self.list[n[0]] Why not just create a current pointer as a clas attribute? Increment or reset as required. after all maintaining object state is what classes and objects are for! HTH, Alan G. From carroll at tjc.com Fri Jan 14 18:46:15 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 14 18:46:27 2005 Subject: [Tutor] file-like object In-Reply-To: <20050114162421.74326.qmail@web54304.mail.yahoo.com> Message-ID: On Fri, 14 Jan 2005, Chad Crabtree wrote: > class _macroString(object): > def __init__(self,s): > self.macro=s > self.list=self.macro.split("\n") > for n,v in enumerate(self.list): > self.list[n]=v+'\n' Is this for loop a safe technique, where the list you're enumerating over in the for statement is the same as the one being updated in the loop body? I always avoid things like that. From phthenry at earthlink.net Fri Jan 14 19:35:07 2005 From: phthenry at earthlink.net (Paul Tremblay) Date: Fri Jan 14 19:35:11 2005 Subject: [Tutor] style question: when to "hide" variable, modules Message-ID: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> During the recent discussion on jython, a poster brought up the good point that one should hide variables and modules to indicate that they are not for public use: self.__for_internal_purposes = 5 __internal_stuff.py """ This module only makes sense for use with the parent module. """ So one could write several modules using these guidelines. One could then issue the command from a shell pydoc internal_stuff, and sure enough, one gets a nice listing of all the methods with the __methods listed first, signalling to someone who hasn't written the program (or to the author who returns to it a few years later), that one shouldn't look at these methods when looking what is useful from the script. My question is, how far should one take these guidlines? I am working on an OO script with several modules that backs up a hardrive. There will be about 10 modules, but really only *one* of them, called backup, would be used as a pubilc interface. Should I name the rest of them things like __handle_archive.py, __file_system.py, and so on? That seems odd, since I have never seen this done before. However, it does have an elegance of clearly telling someone how to hook into the modules. Also, maybe more importantly, I want to know how to handle attributes that need to be shared. Imagine that there are around 20 attributes, such as the level the program runs at, and the number of the disk being copied that need to be shared with different modules. Right now, my setup looks like this: # this module is called backup.py class Backup: __init__(self, verbose, level ): self.__make_objects() self.verbose = verbose self.level = level def __make_objects(self): self.burn_obj = paxbac.burn.Burn(self) self.archive_obj = paxbac.archive.Archive(self) def get_disk(self): return self.__disk def set_disk(self, the_num): self.__disk = the_num def backup(self): archive_obj.archive() burn_obj.burn() ***** #this is aother module called burn.py class Burn: def __init__(self, main): self.__main = main def burn(self): cd_num = self.main.get_disk() if self__main.level > 3: sys.stdout.write('Burning disk %s\n' %cd_num) **** The main module backukp provides a number of public methods that are really not public. For examle, no one is going to want to use this module to find out what disk the method is on. If I wanted to be very strict with public and private variables and methods, I would have to: 1. change burn.py to __burn.py 2. create a module called __attributes.py, create an object in burn.py called self.__attributes_obj, and pass this object to each method. These two steps seem to take things a bit far. On the other hand, one could look at the set of modules and immediately know how to use them. Thanks Paul From mi.janssen at gmail.com Fri Jan 14 20:21:48 2005 From: mi.janssen at gmail.com (Michael Janssen) Date: Fri Jan 14 20:21:51 2005 Subject: [Tutor] reinitializing namespace In-Reply-To: <200501140830.j0E8UjtC028188@outmx009.isp.belgacom.be> References: <1ff2dfbf05011310281ef9ea00@mail.gmail.com> <200501140830.j0E8UjtC028188@outmx009.isp.belgacom.be> Message-ID: <1ff2dfbf0501141121668983b5@mail.gmail.com> On Fri, 14 Jan 2005 09:30:46 +0100, Dimitri D'Or wrote: > Thank you for your answer. Actually, my question is not specific to > interactive sessions. I've written a script that loads some modules, create > variables and show figures. What I would like to find, is a command for > clearing all the created variables and close all figures before beginning > the execution of the script. This command will be placed at the beginning of > the script and automatically reinitialize the namespace. The utility of this > command is to guarantee that all the variables that are available in the > namespace after the execution of the script were created by it and are not > remainders from older commands or scripts. In Python you do this by mentally tracking when and by which code variables get created and by careful programm design. Most the time it's as easy as: var1 = func1() var2 = func2() It's obviously easy to track which variable gets created by which function ;-) It's harder when func1 for example looks like: def func1(): global var3 var3 = "something" return "somthing" this way func1 has the *sideeffect* of setting var3. Sideeffects are sometimes really bad and it's a good idea to a) document them and b) use them seldom. Furthermore, reusing old variables can happen and results in hard to debug bugs. Look at this for-loop: meaning_full_variable = None for thing in list_of_things: if strange_condition(): meaning_full_variable = check(thing) print thing, meaning_full_variable Of course, you will reuse the meaning_full_variable whenever strange_condition() is not met. The print statment will print out the actual "thing" but possibly the "meaning_full_variable" from an earlier run. You solve this issue by setting meaning_full_variable in every iteration of this loop: for thing in list_of_things: meaning_full_variable = None if strange_condition(): meaning_full_variable = check(thing) print thing, meaning_full_variable which is a slightly saner version of the code ;-) Since such problems are a little hard to debug, you should learn to get wary when you see code like the above example (feel the problems before running into them). The main tactic to minimize namespace problems is to use functions and classes which comes all with their own namespaces. Perhaps you should post code, you find problematic, and we might find strategies to restructure it, so that namespace problems are claryfied. regards Michael From jeff at ccvcorp.com Fri Jan 14 22:15:52 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Fri Jan 14 22:10:41 2005 Subject: [Tutor] Regular expression re.search() object . Please help In-Reply-To: <003b01c4f9e5$991dcab0$375428cf@JSLAPTOP> References: <20050113185600.85462.qmail@web53707.mail.yahoo.com> <41E6E842.2040206@ccvcorp.com> <003b01c4f9e5$991dcab0$375428cf@JSLAPTOP> Message-ID: <41E83688.70106@ccvcorp.com> Jacob S. wrote: > I assume that both you and Liam are using previous-er versions of python? > Now files are iterators by line and you can do this. > > openFile = open("probe_pairs.txt","r") > indexesToRemove = [] > for line in openFile: > # [...] My version of Python isn't *that* old (IIRC this works since 2.2, which is what I usually use), I'm just in the habit of using readlines(). I suppose that, in a case like this where the filesize is not insignificant, it *would* be worthwhile to take advantage of the file iterator to avoid having that fairly large list in-memory... Jeff Shannon Technician/Programmer Credit International From dyoo at hkn.eecs.berkeley.edu Fri Jan 14 22:15:12 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 14 22:15:16 2005 Subject: [Tutor] file-like object In-Reply-To: <20050114162421.74326.qmail@web54304.mail.yahoo.com> Message-ID: On Fri, 14 Jan 2005, Chad Crabtree wrote: > I have created a file-like object out of a triple quoted string. I was > wondering if there is a better way to implement readline than what I > have below? It just doesn't seem like a very good way to do this. > > class _macroString(object): > def __init__(self,s): > self.macro=s > self.list=self.macro.split("\n") > for n,v in enumerate(self.list): > self.list[n]=v+'\n' > def readline(self,n=[-1]): > n[0]+=1 > return self.list[n[0]] > def __str__(self): > return str(self.list) > def __len__(self): > return len(self.list) Using the default parameter 'n' in the readline() method isn't safe: all class instances will end up using the same 'n'. You may want to put the current line number as part of an instance's state, since two instances of a macroString should be able to keep track of their line positions independently. But that being said, there's already a module in the Standard Library that turns strings into file-like objects. Can you use StringIO.StringIO? Best of wishes to you! From singingxduck at gmail.com Fri Jan 14 22:16:30 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Fri Jan 14 22:16:45 2005 Subject: [Tutor] flattening a list Message-ID: <41E836AE.10300@gmail.com> One final note to wrap things up. I posted a slightly cleaner version of my code on the Python Cookbook, with a reference to the solutions of Gon?alo and Danny via the tutor archives here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/363051 -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From dyoo at hkn.eecs.berkeley.edu Fri Jan 14 22:19:24 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 14 22:19:30 2005 Subject: [Tutor] file-like object In-Reply-To: Message-ID: On Fri, 14 Jan 2005, Terry Carroll wrote: > > class _macroString(object): > > def __init__(self,s): > > self.macro=s > > self.list=self.macro.split("\n") > > for n,v in enumerate(self.list): > > self.list[n]=v+'\n' > > Is this for loop a safe technique, where the list you're enumerating > over in the for statement is the same as the one being updated in the > loop body? I always avoid things like that. Hi Terry, The 'for' loop itself should be ok, as it isn't a change that causes elements to shift around. The Reference Manual tries to explain the issue on in-place list mutation here: http://docs.python.org/ref/for.html So as long as the structure of the list isn't changing, we should be ok to do mutations on the list elements. Best of wishes! From jeff at ccvcorp.com Fri Jan 14 22:28:42 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Fri Jan 14 22:23:30 2005 Subject: [Tutor] file-like object In-Reply-To: References: Message-ID: <41E8398A.6030505@ccvcorp.com> Terry Carroll wrote: > On Fri, 14 Jan 2005, Chad Crabtree wrote: > > >>class _macroString(object): >> def __init__(self,s): >> self.macro=s >> self.list=self.macro.split("\n") >> for n,v in enumerate(self.list): >> self.list[n]=v+'\n' > > > > Is this for loop a safe technique, where the list you're enumerating over > in the for statement is the same as the one being updated in the loop > body? I always avoid things like that. In this case it should be safe. This changes the string at each index in the list, but it doesn't change the length or ordering of the list. That's where the problems come in, because the for loop doesn't know that the list "shape" has changed. But your caution is generally a good idea. :) I'd have probably written the above as: self.list = [line+'\n' for line in self.list] Well, actually, I'd have avoided saving the intermediary state and simply done all the list processing at once: self.list = [line+'\n' for line in self.macro.split('\n')] Either way, though, I'm creating a new list rather than modifying the old list in-place, which avoids the risk of accidentally changing the list's "shape" and throwing off the loop. Jeff Shannon Technician/Programmer Credit International From ps_python at yahoo.com Sat Jan 15 00:28:07 2005 From: ps_python at yahoo.com (kumar s) Date: Sat Jan 15 00:28:11 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help Message-ID: <20050114232807.48286.qmail@web53709.mail.yahoo.com> Hi group: I have two lists a. 'my_report' and b. 'what'. In list 'what', I want to take 6649 (element1: 164:623\t6649) and write to a new list ( although I printed the result, my intension is to list.append(result). I took column 1 value of element 1 in what, which is 164:623 and checked in column 1 value in list my_report, if it matches I asked it to write the all columns of my_report along with column 2 value in what list. (with my explanation, I feel I made it complex). Here is what I did: >>> what[0:4] ['164:623\t6649', '484:11\t6687', '490:339\t6759', '247:57\t6880', '113:623\t6901'] >>>my_report[0:4] ['164:623\tTCATGGCTGACAACCCATCTTGGGA\t200000_s_at', '484:11\tATTATCATCACATGCAGCTTCACGC\t200000_s_at', '490:339\tGAATGGGGCCGCCAGAACACAGACA\t200000_s_at', '247:57\tAGTCCTCGTGGAACTACAACTTCAT\t200000_s_at', '113:623\tTCATGGGTGTTCGGCATGACCCCAA\t200000_s_at'] >>>for i in range(len(what)): ele = split(what[i],'\t') cor1 = ele[0] for k in range(len(my_report)): cols = split(my_report[k],'\t') cor = cols[0] if cor1 == cor: print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] 164:623 6649 TCATGGCTGACAACCCATCTTGGGA 484:11 6687 ATTATCATCACATGCAGCTTCACGC 490:339 6759 GAATGGGGCCGCCAGAACACAGACA 247:57 6880 AGTCCTCGTGGAACTACAACTTCAT 113:623 6901 TCATGGGTGTTCGGCATGACCCCAA PROBLEM: This process is very very slow. I have 249502 elements in each list. The process has been running for over 30 min. Could any one suggest a better fast procedure, to save time. Thank you in advance. K __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From dyoo at hkn.eecs.berkeley.edu Sat Jan 15 00:51:33 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sat Jan 15 00:51:38 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: <20050114232807.48286.qmail@web53709.mail.yahoo.com> Message-ID: On Fri, 14 Jan 2005, kumar s wrote: > >>>for i in range(len(what)): > ele = split(what[i],'\t') > cor1 = ele[0] > for k in range(len(my_report)): > cols = split(my_report[k],'\t') > cor = cols[0] > if cor1 == cor: > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] Hi Kumar, Ok, this calls for the use of an "associative map" or "dictionary". The main time sink is the loop here: > for k in range(len(my_report)): > cols = split(my_report[k],'\t') > cor = cols[0] > if cor1 == cor: > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] Conceptually, my_report can be considered a list of key/value pairs. For each element in 'my_report', the "key" is the first column (cols[0]), and the "value" is the rest of the columns (cols[1:]). The loop above can, in a pessimistic world, require a search across the whole of 'my_report'. This can take time that is proportional to the length of 'my_report'. You mentioned earlier that each list might be of length 249502, so we're looking into a process whose overall cost is gigantic. [Notes on calculating runtime cost: when the structure of the code looks like: for element1 in list1: for element2 in list2: some_operation_that_costs_K_time() then the overall cost of running this loop will be K * len(list1) * len(list2) ] We can do much better than this if we use a "dictionary" data structure. A "dictionary" can reduce the time it takes to do a lookup search down from a linear-time operation to an atomic-time one. Do you know about dictionaries yet? You can take a look at: http://www.ibiblio.org/obp/thinkCSpy/chap10.htm which will give an overview of a dictionary. It doesn't explain why dictionary lookup is fast, but we can talk about that later if you want. Please feel free to ask any questions about dictionaries and their use. Learning how to use a dictionary data structure is a skill that pays back extraordinarily well. Good luck! From maxnoel_fr at yahoo.fr Sat Jan 15 01:04:16 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sat Jan 15 01:04:21 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: <20050114232807.48286.qmail@web53709.mail.yahoo.com> References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> Message-ID: On Jan 14, 2005, at 23:28, kumar s wrote: >>>> for i in range(len(what)): > ele = split(what[i],'\t') > cor1 = ele[0] > for k in range(len(my_report)): > cols = split(my_report[k],'\t') > cor = cols[0] > if cor1 == cor: > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] > > > 164:623 6649 TCATGGCTGACAACCCATCTTGGGA > 484:11 6687 ATTATCATCACATGCAGCTTCACGC > 490:339 6759 GAATGGGGCCGCCAGAACACAGACA > 247:57 6880 AGTCCTCGTGGAACTACAACTTCAT > 113:623 6901 TCATGGGTGTTCGGCATGACCCCAA Okay, so the idea is, the first column of each row is a key, and you want to display only the rows whose key is the first column (key?) of a row in my_report, right? As Danny said, you should use dictionaries for this, with a structure in the lines of: what = { '164:623': '6649 TCATGGCTGACAACCCATCTTGGGA', '484:11': '6687 ATTATCATCACATGCAGCTTCACGC', '490:339': '6759 GAATGGGGCCGCCAGAACACAGACA', } (etc.) Lacking that, as Danny said, nested loops are a huge time sink. Also, you should avoid using C-style for loops -- Python-style for loops (equivalent to Perl's foreach) are much more elegant (and probably faster) in that case. Here's how I would do it with your data structures (warning, untested code, test before use): # First, create a list where each element is one of the keys in my_report # Also, strings have a split() method, which by default splits on any whitespace # (tabs included) headers = [element.split()[0] for element in my_report] for element in what: # Okay, the nested loop is still (more or less) there, but it occurs within a # 'in' operator, and is therefore executed in C -- much faster. if element.split()[0] in headers: print element Also, it's shorter -- 4 lines, comments aside. Nevertheless, as Danny suggested, an approach using dictionaries would blow this away, speed-wise. Hope that helps, -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From flaxeater at yahoo.com Sat Jan 15 01:36:21 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Sat Jan 15 01:36:25 2005 Subject: [Tutor] file-like object Message-ID: <20050115003621.24651.qmail@web54303.mail.yahoo.com> Danny Yoo wrote: >Using the default parameter 'n' in the readline() method isn't safe: all >class instances will end up using the same 'n'. You may want to put the >current line number as part of an instance's state, since two instances of >a macroString should be able to keep track of their line positions >independently. > >But that being said, there's already a module in the Standard Library that >turns strings into file-like objects. Can you use StringIO.StringIO? > > I used kent's solution at first but I'm just using StringIO now I didn't realize what that module did. Thank you all for your suggestion. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From klappnase at freenet.de Sat Jan 15 01:36:06 2005 From: klappnase at freenet.de (Michael Lange) Date: Sat Jan 15 01:36:47 2005 Subject: [Tutor] Tix and Table printing In-Reply-To: <015401c4fa15$bad3b3b0$40b68651@xp> References: <7d7029e70501130300313c39ea@mail.gmail.com> <7d7029e705011317073c11f67a@mail.gmail.com> <015401c4fa15$bad3b3b0$40b68651@xp> Message-ID: <20050115013606.3024ff28.klappnase@freenet.de> On Fri, 14 Jan 2005 08:47:49 -0000 "Alan Gauld" wrote: > Tk was written in the 80's so given > its origins was not likely to have a table. > > Of course it would be nice if they added one now!!! > It looks like they are already working on it: http://wiki.tcl.tk/12753 Regards Michael From alan.gauld at freenet.co.uk Sat Jan 15 01:41:02 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 01:40:21 2005 Subject: [Tutor] file-like object References: Message-ID: <01b901c4fa9a$e3e3ef10$40b68651@xp> > > for n,v in enumerate(self.list): > > self.list[n]=v+'\n' > > > Is this for loop a safe technique, where the list you're enumerating over > in the for statement is the same as the one being updated in the loop > body? I always avoid things like that. Its not changing the list, its changing the list contents. If it were adding or re,moving items from the list that would be dodgy, but modifying a existing element doesn't really change the list itself in any significant way. Alan G. From alan.gauld at freenet.co.uk Sat Jan 15 01:44:54 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 01:44:06 2005 Subject: [Tutor] style question: when to "hide" variable, modules References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> Message-ID: <01be01c4fa9b$6e8bb120$40b68651@xp> > My question is, how far should one take these guidlines? My response is, as far as you need and no further. In other words if it would actually cause problems for clients to access those variables disguise them, but otherwise trust your fellow programmers not to be stupid. Its the same principle as the recent discussion about private/public keywords etc. Comments and docstrings can help too. Alan G. From kent37 at tds.net Sat Jan 15 02:11:32 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 15 02:11:38 2005 Subject: [Tutor] style question: when to "hide" variable, modules In-Reply-To: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> Message-ID: <41E86DC4.6070102@tds.net> A few thoughts: - you might want to make a configuration object that you can pass around, this is probably better than passing around an instance of the main Burn class. - typical Python style is *not* to define setter and getter functions. If you need to mediate attribute access you can do it later using properties. - you might want to consolidate your classes into a small number of modules. This is the style of much of the Python standard library. Look at optparse.py for an example (picked more-or-less at random). - if one class has a helper class or function that isn't used anywhere else, put the helper into the same module as the client and name the helper starting with _. - I tend to worry more about naming with _ in a module that is likely to be reused. For a module that will probably be only be used once, I don't use _ at all. In a module that has a public API I try to use _ to distinguish the implementation details from the public stuff. HTH Kent Paul Tremblay wrote: > During the recent discussion on jython, a poster > brought up the good point that one should hide > variables and modules to indicate that they are > not for public use: > > self.__for_internal_purposes = 5 > > __internal_stuff.py > """ > This module only makes sense for use with > the parent module. > """ > > So one could write several modules using these > guidelines. One could then issue the command > from a shell pydoc internal_stuff, and sure enough, > one gets a nice listing of all the methods with the > __methods listed first, signalling to someone who > hasn't written the program (or to the author who > returns to it a few years later), that one shouldn't > look at these methods when looking what is useful > from the script. > > My question is, how far should one take these guidlines? > > I am working on an OO script with several modules that > backs up a hardrive. There will be about 10 modules, but > really only *one* of them, called backup, would be used > as a pubilc interface. Should I name the rest of them things > like __handle_archive.py, __file_system.py, and so on? That > seems odd, since I have never seen this done before. However, > it does have an elegance of clearly telling someone how to > hook into the modules. > > Also, maybe more importantly, I want to know how to handle > attributes that need to be shared. Imagine that there are around > 20 attributes, such as the level the program runs at, and the number > of the disk being copied that need to be shared with different > modules. Right now, my setup looks like this: > > # this module is called backup.py > > class Backup: > > __init__(self, verbose, level ): > self.__make_objects() > self.verbose = verbose > self.level = level > > > def __make_objects(self): > self.burn_obj = paxbac.burn.Burn(self) > self.archive_obj = paxbac.archive.Archive(self) > > def get_disk(self): > return self.__disk > > def set_disk(self, the_num): > self.__disk = the_num > > def backup(self): > archive_obj.archive() > burn_obj.burn() > > ***** > > #this is aother module called burn.py > > class Burn: > def __init__(self, main): > self.__main = main > > def burn(self): > cd_num = self.main.get_disk() > if self__main.level > 3: > sys.stdout.write('Burning disk %s\n' %cd_num) > > **** > > The main module backukp provides a number of public > methods that are really not public. For examle, no > one is going to want to use this module to find > out what disk the method is on. If I wanted to > be very strict with public and private variables > and methods, I would have to: > > 1. change burn.py to __burn.py > > 2. create a module called __attributes.py, create an > object in burn.py called self.__attributes_obj, and pass > this object to each method. > > These two steps seem to take things a bit far. On the other > hand, one could look at the set of modules and immediately > know how to use them. > > Thanks > > Paul > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at freenet.co.uk Mon Jan 10 20:45:15 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 02:39:25 2005 Subject: [Tutor] Hi. Is there another mailing list like this one? References: <003a01c4f6c2$4938e410$3a5328cf@JSLAPTOP> Message-ID: <009501c4f74c$e8b70460$16c68651@xp> > I'm am bored and people are not asking enough questions/answering them to > keep my mind busy. Is there any other mailing list that I can subscribe to > like this one that lets anyone ask and answer questions? I assume you'vve checked the Python newsgroup? It should be busy enough for anyone! Of course the subject area is much more diverse... but you can join a mailing list variant I believe? And of course there are all the sig groups and the specialist mailing lists. Try looking through the other areas on the Community page of the web site... Alan G. From dranor0 at lycos.com Mon Jan 10 15:31:03 2005 From: dranor0 at lycos.com (john stanley) Date: Sat Jan 15 02:39:48 2005 Subject: [Tutor] Help Message-ID: <20050110143103.91B0E3384B@ws7-3.us4.outblaze.com> This is my first attempt at programing and my first program sort of did work hear is the program and if any one can tell me what i did wrong or forgot i would appreciate it a = input("Type in the Grose: ") b = input("type in the Miles: ") print "a * 0.74 / b is" a*0.74/b as you can see it should be realy easy program once it works -- _______________________________________________ Find what you are looking for with the Lycos Yellow Pages http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10 From hugonz at h-lab.net Thu Jan 13 06:00:14 2005 From: hugonz at h-lab.net (=?ISO-8859-1?Q?Hugo_Gonz=E1lez_Monteverde?=) Date: Sat Jan 15 02:42:50 2005 Subject: [Tutor] Matrix In-Reply-To: <41C5D6F5.3050804@po-box.mcgill.ca> References: <5a00f624041219073357d58e88@mail.gmail.com> <41C5D6F5.3050804@po-box.mcgill.ca> Message-ID: <41E6005E.1020606@h-lab.net> Brian van den Broek wrote: > 2) To get around that, and be more efficient with matricies with many > empty cells: > .>>> my_matrix_as_dict = {(1,1):4, (1,2):6, (1,3):8, > (2,1):56, (2,3):12, > (3,1):3, (3,2):3} > > .>>> my_matrix_as_dict[(3,1)] > 3 > .>>> my_matrix_as_dict[(2,1)] > 56 > > So, you just can use the tuple co-ordinates you've defined in order to > access cells. Note also that the list way you'd have to represent empty > cells with a standard null value -- None is the usual choice. But this > way, you just don't define some tuples as keys, as I didn't define (2,2) > as a key. Thus: > > .>>> my_matrix_as_dict[(2,2)] > > Traceback (most recent call last): > File "", line 1, in -toplevel- > my_matrix_as_dict[(2,2)] > KeyError: (2, 2) > > You can make that more graceful with a try/except block: > > .>>> try: > my_matrix_as_dict[(2,2)] > except KeyError: > print "That cell is empty" > > That cell is empty > .>>> > if you want None to be returned for an empty cell, you can just do: my_matrix_as_dict.get((2,2), None) so that the None object will de returned for all inexistant cells... Hugo From swidneylv at yahoo.com Wed Jan 12 23:39:41 2005 From: swidneylv at yahoo.com (Scott Widney) Date: Sat Jan 15 02:45:20 2005 Subject: [Tutor] How to create a key-value pairs with alternative elements in a list ... please help. Message-ID: <20050112223941.37634.qmail@web90104.mail.scd.yahoo.com> > I have a simple list: > ['a', 'apple', 'b', 'boy', 'c', 'cat'] > I want to create a dictionary: > dict = {'a':'apple', 'b':'boy', 'c':'cat'} >From the Quick and Dirty Department: If you have Python version 2.3 or later, you can use 'itertools' to unflatten the list in a very concise manner. Here is an interpreter session using your example data above. >>> l = ['a', 'apple', 'b', 'boy', 'c', 'cat'] >>> d = {} >>> import itertools >>> slice0 = itertools.islice(l,0,None,2) >>> slice1 = itertools.islice(l,1,None,2) >>> while True: ... try: ... d[slice0.next()] = slice1.next() ... except: StopIteration ... break ... >>> d {'a': 'apple', 'c': 'cat', 'b': 'boy'} This should be fairly space-efficient. As another approach, you could use extended slicing to break the list apart (creating two new lists could be expensive) and then patch it back together again: >>> slice0 = l[::2] >>> slice1 = l[1::2] >>> slice0 ['a', 'b', 'c'] >>> slice1 ['apple', 'boy', 'cat'] >>> d = dict(zip(slice0,slice1)) >>> d {'a': 'apple', 'c': 'cat', 'b': 'boy'} >>> In both cases, we get slice0 by starting at position 0 in the list and taking every other item and we get get slice1 by starting at position 1 in the list and again taking every other item. Hope this helps! --------------------------------- Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050112/13a71a6a/attachment.html From carroll at tjc.com Sat Jan 15 03:45:22 2005 From: carroll at tjc.com (Terry Carroll) Date: Sat Jan 15 03:45:25 2005 Subject: [Tutor] file-like object In-Reply-To: Message-ID: On Fri, 14 Jan 2005, Terry Carroll wrote: > Is this for loop a safe technique, where the list you're enumerating over > in the for statement is the same as the one being updated in the loop > body? Rather than cluttering the list by making three replies, I'd just like to thank Danny, Alan and Jeff each for their answers to this. Clears it right up for me. From maxnoel_fr at yahoo.fr Sat Jan 15 03:49:16 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sat Jan 15 03:49:20 2005 Subject: [Tutor] Help In-Reply-To: <20050110143103.91B0E3384B@ws7-3.us4.outblaze.com> References: <20050110143103.91B0E3384B@ws7-3.us4.outblaze.com> Message-ID: <0BC0C4FE-66A0-11D9-AF6C-000393CBC88E@yahoo.fr> On Jan 10, 2005, at 14:31, john stanley wrote: > This is my first attempt at programing and my first program sort of > did work hear is the program and if any one can tell me what i did > wrong or forgot i would appreciate it > > > > a = input("Type in the Grose: ") > b = input("type in the Miles: ") > print "a * 0.74 / b is" a*0.74/b > > as you can see it should be realy easy program once it works The first thing to do is to look at the error that was caused, and what caused it. Python is good at it, as it shows you the exact line, and even the exact character where the error happened. Now, this example is trivial, but in the future, when you have such a problem, please include the traceback in your message, thanks. Anyway, the error happens in the last line, just before the last expression (a*0.74/b). Which is logical, because that expression is in no way linked to the string that's before it. I suppose you want to display the result of the expression right after the string, don't you? Then you might want to use the string concatenation operator: + It should be easy to correct, now... -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From ps_python at yahoo.com Sat Jan 15 04:12:03 2005 From: ps_python at yahoo.com (kumar s) Date: Sat Jan 15 04:12:07 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: Message-ID: <20050115031204.9290.qmail@web53705.mail.yahoo.com> Hi Danny: Thank you for your suggestion. I tried creating a dictionary of 'what' list and searched keys with has_key method and it is pretty fast. Thanks again. following is the piece of code. K >>> cors = [] >>> intr = [] >>> for i in range(len(what)): ele = split(what[i],'\t') cors.append(ele[0]) intr.append(ele[1]) >>> what_dict = dict(zip(cors,intr)) >>> for i in range(len(my_report)): cols = split(my_report[i],'\t') cor = cols[0] if what_dict.has_key(cor): intr = what_dict[cor] my_final_report.append(cols[0]+'\t'+intr+'\t'+cols[1]+'\t'+cols[2]) --- Danny Yoo wrote: > > > On Fri, 14 Jan 2005, kumar s wrote: > > > >>>for i in range(len(what)): > > ele = split(what[i],'\t') > > cor1 = ele[0] > > for k in range(len(my_report)): > > cols = split(my_report[k],'\t') > > cor = cols[0] > > if cor1 == cor: > > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] > > > > Hi Kumar, > > > Ok, this calls for the use of an "associative map" > or "dictionary". > > > The main time sink is the loop here: > > > for k in range(len(my_report)): > > cols = split(my_report[k],'\t') > > cor = cols[0] > > if cor1 == cor: > > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] > > Conceptually, my_report can be considered a list of > key/value pairs. For > each element in 'my_report', the "key" is the first > column (cols[0]), and > the "value" is the rest of the columns (cols[1:]). > > > The loop above can, in a pessimistic world, require > a search across the > whole of 'my_report'. This can take time that is > proportional to the > length of 'my_report'. You mentioned earlier that > each list might be of > length 249502, so we're looking into a process whose > overall cost is > gigantic. > > > [Notes on calculating runtime cost: when the > structure of the code looks > like: > > for element1 in list1: > for element2 in list2: > some_operation_that_costs_K_time() > > then the overall cost of running this loop will be > > K * len(list1) * len(list2) > ] > > > We can do much better than this if we use a > "dictionary" data structure. A > "dictionary" can reduce the time it takes to do a > lookup search down from > a linear-time operation to an atomic-time one. Do > you know about > dictionaries yet? You can take a look at: > > http://www.ibiblio.org/obp/thinkCSpy/chap10.htm > > which will give an overview of a dictionary. It > doesn't explain why > dictionary lookup is fast, but we can talk about > that later if you want. > > > Please feel free to ask any questions about > dictionaries and their use. > Learning how to use a dictionary data structure is a > skill that pays back > extraordinarily well. > > > Good luck! > > __________________________________ Do you Yahoo!? Meet the all-new My Yahoo! - Try it today! http://my.yahoo.com From carroll at tjc.com Sat Jan 15 04:46:10 2005 From: carroll at tjc.com (Terry Carroll) Date: Sat Jan 15 04:46:13 2005 Subject: [Tutor] Intro for interfacing with Microsoft Access? Message-ID: Does anyone know of any online resource that explains how to interface to Microsoft Access via Python, where the intended audience is someone who knows Python, but not the Microsoft innards? I've found http://starship.python.net/crew/bwilk/access.html (which doesn't work for me, and presumably is out of date) and http://www.ecp.cc/pyado.html (which mostly works well enough, but assumes you know Microsoft's Data Access Objects (DAO), and points to the overwhelming Microsoft ADO API Reference. I don't want to do anything too fancy, just read data from my database, make a few calculations and print some summaries obtained from it. I know Python, but not Access (it's my company's DB, not mine) or DAO. From kent37 at tds.net Sat Jan 15 05:17:11 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 15 05:17:20 2005 Subject: [Tutor] Intro for interfacing with Microsoft Access? In-Reply-To: References: Message-ID: <41E89947.90709@tds.net> A couple of minutes of googling for 'python odbc' finds the ODBC driver that comes with win32all. It seems to have a fairly simple interface. The download from this page has an example: http://py.vaults.ca/apyllo2.py/D906422565 HTH Kent Terry Carroll wrote: > Does anyone know of any online resource that explains how to interface to > Microsoft Access via Python, where the intended audience is someone who > knows Python, but not the Microsoft innards? > > I've found http://starship.python.net/crew/bwilk/access.html (which > doesn't work for me, and presumably is out of date) and > http://www.ecp.cc/pyado.html (which mostly works well enough, but assumes > you know Microsoft's Data Access Objects (DAO), and points to the > overwhelming Microsoft ADO API Reference. > > I don't want to do anything too fancy, just read data from my database, > make a few calculations and print some summaries obtained from it. I know > Python, but not Access (it's my company's DB, not mine) or DAO. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From singingxduck at gmail.com Sat Jan 15 06:11:38 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sat Jan 15 06:11:53 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box Message-ID: <41E8A60A.9080203@gmail.com> Hello all, I'm trying to use Python to start the dos-box ("cmd.exe") and be able to call commands on it and receive output from it. However, none of the documentation for popen and spawn cover this . . . Any help would be appreciated. Thanks in advance, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From carroll at tjc.com Sat Jan 15 08:32:29 2005 From: carroll at tjc.com (Terry Carroll) Date: Sat Jan 15 08:32:32 2005 Subject: [Tutor] Intro for interfacing with Microsoft Access? In-Reply-To: <41E89947.90709@tds.net> Message-ID: On Fri, 14 Jan 2005, Kent Johnson wrote: > A couple of minutes of googling for 'python odbc' finds the ODBC driver > that comes with win32all. It seems to have a fairly simple interface. > The download from this page has an example: > http://py.vaults.ca/apyllo2.py/D906422565 Thanks, Kent. I'm not familiar with OBDC, but I'll look into it. From alan.gauld at freenet.co.uk Sat Jan 15 10:17:33 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 10:17:39 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> Message-ID: <01e101c4fae3$0c78a590$40b68651@xp> > >>>for i in range(len(what)): > ele = split(what[i],'\t') > cor1 = ele[0] It will be faster if you stop using len() in your for loops! for item in what: > for k in range(len(my_report)): > cols = split(my_report[k],'\t') > cor = cols[0] And again: for item in my_report: > if cor1 == cor: > print cor+'\t'+ele[1]+'\t'+cols[1]+'\t'+cols[2] And it will be faster if you stop adding the strings together. Each addition creates a new string. Use the formatting operation instead: print "%s\t%s\t%s\t%s" % (cor,ele[1],cols[1],cols[2]) The other thing to consider is using a dictionary. You are effectively using the cor bit as a key, so if instead of creating two lists ytou create two dictionaries you will avoid all the splitting stuff and have instant access: for key in what.keys(): try: print "%s\t%s\t%s\t%s" % (key, what[key][0], report[key][0],report[key][1]) except KeyError: pass # or print an error message By missing out a loop(*) and some splits it should speed up significantly for the cost of some small added complexity in building the dictionaries in the first case. (*)In fact 3 loops because you aren't doing len() which effectively loops over the collection too. HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Sat Jan 15 10:25:17 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 10:25:22 2005 Subject: [Tutor] Help References: <20050110143103.91B0E3384B@ws7-3.us4.outblaze.com> Message-ID: <020501c4fae4$20e5e000$40b68651@xp> > a = input("Type in the Grose: ") > b = input("type in the Miles: ") > print "a * 0.74 / b is" a*0.74/b Try adding a comma between the message and the calculation. print "a * 0.74 / b is", a*0.74/b It tells print that there are two separate things to print. Also when posting problems it is always best to include the actual error you get. In this case its simple enough but anything beyond that and the error report helps a lot. Also, while input() is easy to use it is considered slightly dangerous since a malicious user could type python commands in at the prompt, so its an open door for security problems. But while you are learning don;t worry too much about that, just bear it in mind for the future. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Sat Jan 15 10:28:14 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 10:28:24 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help References: <20050115031204.9290.qmail@web53705.mail.yahoo.com> Message-ID: <021801c4fae4$8a46a6b0$40b68651@xp> > Thank you for your suggestion. I tried creating a > dictionary of 'what' list and searched keys with > has_key method and it is pretty fast. Use try/except and it will be faster still. The except only gets triggered if theres a missing entry - which in your case should be hardly ever! So you save a function call per item... Alan G. From alan.gauld at freenet.co.uk Sat Jan 15 13:09:55 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 13:09:59 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box References: <41E8A60A.9080203@gmail.com> Message-ID: <022d01c4fafb$205197d0$40b68651@xp> > I'm trying to use Python to start the dos-box ("cmd.exe") and be able to > call commands on it and receive output from it. The Command window is just a terminal, there is no output to be had other than an exit code when it closes. > However, none of the documentation for popen and > spawn cover this . . . Nope, there is nothing to be received from the terminal, you might as well open Notepad and try to read output from it (in fact that would probably work better!). What is it you are trying to do using the DOS box? Its probably easier to run the commands that you would run inside the box from Python. But if you are trying to monitor what *a user* is doing then it gets more tricky. Alan G. From kent37 at tds.net Sat Jan 15 13:28:12 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 15 13:28:19 2005 Subject: [Tutor] Intro for interfacing with Microsoft Access? In-Reply-To: References: Message-ID: <41E90C5C.30904@tds.net> You have to set up your Access database as an ODBC data source. I can't give you all the details but on my Win2k system if I go to Start / Control Panels / Administrative Tools / Data Sources (ODBC) that is the place to set it up. IIRC you give the data source a name. Then in the ODBC driver (the win32all odbc package) you create a connection giving it the data source name and probably a user name and password. When it's all set up you can use SQL to talk to the database. You might have to fiddle a bit to get all the pieces set up :-) It looks like the win32all package uses the Python DB-API so you can read about that (find the database SIG at python.org) for some help in how to use it. I did this long ago from Java so I'm pretty sketchy on the details, maybe someone else is more current on it than me. Kent Terry Carroll wrote: > On Fri, 14 Jan 2005, Kent Johnson wrote: > > >>A couple of minutes of googling for 'python odbc' finds the ODBC driver >>that comes with win32all. It seems to have a fairly simple interface. >>The download from this page has an example: >>http://py.vaults.ca/apyllo2.py/D906422565 > > > Thanks, Kent. I'm not familiar with OBDC, but I'll look into it. > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Sat Jan 15 16:07:24 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 15 16:08:12 2005 Subject: [Tutor] style question: when to "hide" variable, modules References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> Message-ID: <005b01c4fb14$0b698940$d35428cf@JSLAPTOP> I'm not too sure about this... Couldn't you make that a package? Rename Backup.py to __init__.py Put all of the modules in a folder named Backup in your sys.path - Question: Does it have to be in site-packages? Well, there's my two bits, Jacob > During the recent discussion on jython, a poster > brought up the good point that one should hide > variables and modules to indicate that they are > not for public use: > > self.__for_internal_purposes = 5 > > __internal_stuff.py > """ > This module only makes sense for use with > the parent module. > """ > > So one could write several modules using these > guidelines. One could then issue the command > from a shell pydoc internal_stuff, and sure enough, > one gets a nice listing of all the methods with the > __methods listed first, signalling to someone who > hasn't written the program (or to the author who > returns to it a few years later), that one shouldn't > look at these methods when looking what is useful > from the script. > > My question is, how far should one take these guidlines? > > I am working on an OO script with several modules that > backs up a hardrive. There will be about 10 modules, but > really only *one* of them, called backup, would be used > as a pubilc interface. Should I name the rest of them things > like __handle_archive.py, __file_system.py, and so on? That > seems odd, since I have never seen this done before. However, > it does have an elegance of clearly telling someone how to > hook into the modules. > > Also, maybe more importantly, I want to know how to handle > attributes that need to be shared. Imagine that there are around > 20 attributes, such as the level the program runs at, and the number > of the disk being copied that need to be shared with different > modules. Right now, my setup looks like this: > > # this module is called backup.py > > class Backup: > > __init__(self, verbose, level ): > self.__make_objects() > self.verbose = verbose > self.level = level > > > def __make_objects(self): > self.burn_obj = paxbac.burn.Burn(self) > self.archive_obj = paxbac.archive.Archive(self) > > def get_disk(self): > return self.__disk > > def set_disk(self, the_num): > self.__disk = the_num > > def backup(self): > archive_obj.archive() > burn_obj.burn() > > ***** > > #this is aother module called burn.py > > class Burn: > def __init__(self, main): > self.__main = main > > def burn(self): > cd_num = self.main.get_disk() > if self__main.level > 3: > sys.stdout.write('Burning disk %s\n' %cd_num) > > **** > > The main module backukp provides a number of public > methods that are really not public. For examle, no > one is going to want to use this module to find > out what disk the method is on. If I wanted to > be very strict with public and private variables > and methods, I would have to: > > 1. change burn.py to __burn.py > > 2. create a module called __attributes.py, create an > object in burn.py called self.__attributes_obj, and pass > this object to each method. > > These two steps seem to take things a bit far. On the other > hand, one could look at the set of modules and immediately > know how to use them. > > Thanks > > Paul > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From singingxduck at gmail.com Sat Jan 15 17:56:26 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sat Jan 15 17:56:40 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box In-Reply-To: <022d01c4fafb$205197d0$40b68651@xp> References: <41E8A60A.9080203@gmail.com> <022d01c4fafb$205197d0$40b68651@xp> Message-ID: <41E94B3A.6090000@gmail.com> Alan Gauld wrote: >>I'm trying to use Python to start the dos-box ("cmd.exe") and be >> >> >able to > > >>call commands on it and receive output from it. >> >> > >The Command window is just a terminal, there is no output to >be had other than an exit code when it closes. > > > >>However, none of the documentation for popen and >>spawn cover this . . . >> >> > >Nope, there is nothing to be received from the terminal, >you might as well open Notepad and try to read output >from it (in fact that would probably work better!). > >What is it you are trying to do using the DOS box? >Its probably easier to run the commands that you >would run inside the box from Python. But if you >are trying to monitor what *a user* is doing then >it gets more tricky. > >Alan G. > > > > Well, ultimately, what I want to be able to do is open Audacity (http://audacity.sourceforge.net/) at a predetermined time and have it begin recording wave mix out (the default i have it set to anyway), and then have it stop again when i send it a command. This is basically to automate recording once a song begins playing in a certain media player, and stop recording when it's over. I don't know if it would be easier to do any or all of it in Python than in the DOS box, but since I have trouble with sending commands to the DOS box, I imagine sending commands to Audacity will be even more troublesome. Right now I have little to no idea where to go from here. I have the idea of what I want to do, but no knowledge on how to do it. Once again, any help would be appreciated. Thanks in advance, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050115/6398bde1/attachment.htm From op73418 at mail.telepac.pt Sat Jan 15 19:11:56 2005 From: op73418 at mail.telepac.pt (=?ISO-8859-1?Q?Gon=E7alo_Rodrigues?=) Date: Sat Jan 15 19:08:20 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: <01e101c4fae3$0c78a590$40b68651@xp> References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> <01e101c4fae3$0c78a590$40b68651@xp> Message-ID: <41E95CEC.9070406@mail.telepac.pt> Alan Gauld wrote: > By missing out a loop(*) and some splits it should speed up > significantly for the cost of some small added complexity in building > the dictionaries in the first case. > > (*)In fact 3 loops because you aren't doing len() which effectively > loops over the collection too. > It this correct? Python lists are not linked-lists (as in Scheme, for example). They are more like arrays (or vectors in C++/Java) with a little more sofistication built into them to allow, for example, to amortize over time a sequence of append operations. But in a nutshell, len is actually a field in the underlying C object so len() is a constant (O(1)) and as-fast-as-it-can-be operation. Someone correct me, please, If I'm wrong. Best regards, G. Rodrigues From marilyn at deliberate.com Sat Jan 15 23:15:57 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Sat Jan 15 23:19:49 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: <00b801c4f951$4626c670$40b68651@xp> Message-ID: Whew! What a trip this bug has been! Danny was exactly right here: > is exactly the sort of thing I'd expect if two threads were > contending for the same resource, so let's see if the bug has to do > with this. This bug drove me nuts. (it's a short drive) So I started wrapping my file openings in lock.acquire(), lock.release() and nothing seemed to change. Imagine my discouragement. So I knuckled down and began to prepare some code for posting here, documenting, cleaning, generating a pleasant log file, ... While doing this, I became more and more convinced that my code is good. Then it hit me. Maybe I should upgrade to 2.4. Maybe I don't have thread-safety happening in my current situation. Maybe I wasn't planning for threads when I compiled 2.3. The new installation MySQL-python tells me that the mysqlclient library is mostly, but not guaranteed, thread-safe and I should use mysqlclient_s. That didn't exist before. So I bring up the latest of everything and things are much worse. My updates to mysql are ignored without an error. I can't run *anything* Now, at that point, on Thursday night, I was so frustrated and discouraged. I was thinking that I should start over in C, that python is a great teaching language, as originally intended, but for production work, forget it. I was so wrong! I don't know how Charlie, my boss and my son, found this, because I googled and googled and couldn't find it, even after I knew what I was looking for. But, the new MySQL-python has changed its behavior with respect to InnoDB tables. This is some relatively new MySQL table type that allows foreign key constraints -- and upon which we totally depend. Now I must do a commit after each update or insert. Then things started to clear up. Adding locks started to make a difference. It turns out that the shared resource causing all my trouble was, indeed, the operating system's file descriptor generation process. I wrapped the same lock around file openings, socket creations, popen calls, and MySQL connectings and my troubles disappeared. However, when, for curiosity, I ran my stuff under 2.3 again, it worked! So I must have missed it that the first few locks did indeed help my situation. It's easy to miss because if any file-descriptor-dependent operation is left outside the lock, it wrecks havoc in all file-descriptor-dependent operations. So the errors looked the same at first glance. But everything is groovy-to-the-max now. I have my MTA deferring messages because of MySQL's "Too many connections" and the python has stepped out of the performance picture for being so fast, so so fast. Or so it looks today. And thank you Alan, for this reminder: > As was said in a recent thread, mixing file objects and > file descriptors is a bad idea. The OS can get confused, > let alone the programmer! Yeh. Gee. I think I started that thread too. Speaking about confused! Thank you so much again, Alan and Danny and other tutors. Knowing that I have this list to turn to got me through this. Marilyn From alan.gauld at freenet.co.uk Sat Jan 15 23:20:02 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sat Jan 15 23:20:01 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box References: <41E8A60A.9080203@gmail.com> <022d01c4fafb$205197d0$40b68651@xp> <41E94B3A.6090000@gmail.com> Message-ID: <024501c4fb50$5d8b9240$40b68651@xp> > Well, ultimately, what I want to be able to do is open Audacity > (http://audacity.sourceforge.net/) at a predetermined time and have it > begin recording wave mix out (the default i have it set to anyway), and > then have it stop again when i send it a command. This will be much easier from python than using the DOS box as an intermediate. I use Audacity but have no idea whether it has any kind of message interface, but at the worst case you can generate windows message events and use the Win32 API to send them (using PostMessage() API call) to the Audacity window. But since Audacity is quite a sophisticated programme I wouldn't be surprised if it has a message based API of its own. > Right now I have little to no idea where to go from here. I have the > idea of what I want to do, but no knowledge on how to do it. Once again, > any help would be appreciated. If you have to use the WIin32 api then its faurly messy and will require some experimenting from the >>> prompt to work out exactly what you need to do. BUt its not impossibly difficult. If Audacity exposes an interface of its own then it shouldn't be too hard at all, you'll need to check the Audacity docs and web site closely. Alan G. From tim.peters at gmail.com Sun Jan 16 00:59:33 2005 From: tim.peters at gmail.com (Tim Peters) Date: Sun Jan 16 00:59:37 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: <41E95CEC.9070406@mail.telepac.pt> References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> <01e101c4fae3$0c78a590$40b68651@xp> <41E95CEC.9070406@mail.telepac.pt> Message-ID: <1f7befae0501151559459a4f72@mail.gmail.com> [Gon?alo Rodrigues] > It this correct? Python lists are not linked-lists (as in Scheme, for > example). They are more like arrays (or vectors in C++/Java) with a > little more sofistication built into them to allow, for example, to > amortize over time a sequence of append operations. But in a nutshell, > len is actually a field in the underlying C object so len() is a > constant (O(1)) and as-fast-as-it-can-be operation. > > Someone correct me, please, If I'm wrong. That's all correct. It remains more idiomatic to do for thing in a_list: ... than for i in range(len(a_list)): thing = a_list[i] ... and the former is in fact a bit faster (or possibly a lot, if a_list is very large, because range(n) actually constructs a list containing n integers), but (ignoring the range() complication) there's no difference in O() behavior between the two. From alan.gauld at freenet.co.uk Sun Jan 16 01:41:17 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 01:41:12 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help References: <20050114232807.48286.qmail@web53709.mail.yahoo.com><01e101c4fae3$0c78a590$40b68651@xp> <41E95CEC.9070406@mail.telepac.pt> Message-ID: <026601c4fb64$185fa800$40b68651@xp> > It this correct? Python lists are not linked-lists (as in Scheme, for > example). They are more like arrays (or vectors in C++/Java) with a > little more sofistication built into them to allow, for example, to > amortize over time a sequence of append operations. But in a nutshell, > len is actually a field in the underlying C object so len() is a > constant (O(1)) and as-fast-as-it-can-be operation. > > Someone correct me, please, If I'm wrong. I thought that too, but after a recent thread when I said the same I got an off list mail assuring me that len() was calculated at run time by iterating over the collection! I guess I should go and look at the code! However in either case missing out the two function calls to len() would help improve the speed... Alan G. From alan.gauld at freenet.co.uk Sun Jan 16 01:44:38 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 01:44:33 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help References: <20050114232807.48286.qmail@web53709.mail.yahoo.com><01e101c4fae3$0c78a590$40b68651@xp> <41E95CEC.9070406@mail.telepac.pt> <1f7befae0501151559459a4f72@mail.gmail.com> Message-ID: <027d01c4fb64$8f905a00$40b68651@xp> > GR> len is actually a field in the underlying C object so len() is a > GR> constant (O(1)) and as-fast-as-it-can-be operation. > TP> ...n integers), but (ignoring the range() complication) there's no TP> difference in O() behavior between the two. OK, The timbot's word is good enough for me, I won't bother looking at the code, I'll revert to my previous assumption! :-) Alan G. From tim.peters at gmail.com Sun Jan 16 01:56:06 2005 From: tim.peters at gmail.com (Tim Peters) Date: Sun Jan 16 01:56:10 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help In-Reply-To: <027d01c4fb64$8f905a00$40b68651@xp> References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> <01e101c4fae3$0c78a590$40b68651@xp> <41E95CEC.9070406@mail.telepac.pt> <1f7befae0501151559459a4f72@mail.gmail.com> <027d01c4fb64$8f905a00$40b68651@xp> Message-ID: <1f7befae05011516564e8c7407@mail.gmail.com> [Alan Gauld] > OK, The timbot's word is good enough for me, I won't bother > looking at the code, I'll revert to my previous assumption! :-) It's educational to look at the code anyway . Here it is, from Python's listobject.c: static int list_length(PyListObject *a) { return a->ob_size; } People shouldn't be afraid to look at C code -- it won't bite them, and it's often the easiest way to get a definitive answer about the implementation. OTOH, people should be afraid to _write_ C code: it will bite you every time <0.9 wink>. From marilyn at deliberate.com Sun Jan 16 03:09:20 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Sun Jan 16 03:13:06 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: Dearest Tutors, Bah! It's not over yet. I don't know why, but again my file descriptors are being trampled upon now and then. This time I can see in my log that I'm not trampling on them myself, like I used to do, unless I'm making calls to the system that I'm not aware of. And, first I get this worrisome output: close failed: [Errno 9] Bad file descriptor close failed: [Errno 9] Bad file descriptor close failed: [Errno 9] Bad file descriptor coming from nowhere. No traceback, nothing. I traced this down once to discover that it came out of python *after* a raise, after a failed call to open a connection to mysql. In that particular raise clause, I called my facility to pipe mail to exim, opening 3 descriptors. I guessed then that when closing down, it closes down the pipe descriptors that I had already closed? So, if it does this, if it closes down descriptors that I've already closed, it's possible that it's closing down descriptors that have also already been reopened because I'm running this thing like lightning? And the other thought is that there is some other call I make that uses a file descriptor that I'm not thinking of. os.listdir? os.anything? Any new thoughts? Marilyn From singingxduck at gmail.com Sun Jan 16 05:49:41 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 05:49:56 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box In-Reply-To: <024501c4fb50$5d8b9240$40b68651@xp> References: <41E8A60A.9080203@gmail.com> <022d01c4fafb$205197d0$40b68651@xp> <41E94B3A.6090000@gmail.com> <024501c4fb50$5d8b9240$40b68651@xp> Message-ID: <41E9F265.8070200@gmail.com> Alan Gauld wrote: >>Well, ultimately, what I want to be able to do is open Audacity >>(http://audacity.sourceforge.net/) at a predetermined time and have >> >> >it > > >>begin recording wave mix out (the default i have it set to anyway), >> >> >and > > >>then have it stop again when i send it a command. >> >> > >This will be much easier from python than using the DOS box as >an intermediate. I use Audacity but have no idea whether it >has any kind of message interface, but at the worst case you >can generate windows message events and use the Win32 API >to send them (using PostMessage() API call) to the Audacity >window. > >But since Audacity is quite a sophisticated programme I >wouldn't be surprised if it has a message based API of >its own. > > > >>Right now I have little to no idea where to go from here. I have the >>idea of what I want to do, but no knowledge on how to do it. Once >> >> >again, > > >>any help would be appreciated. >> >> > >If you have to use the WIin32 api then its faurly messy >and will require some experimenting from the >>> prompt >to work out exactly what you need to do. BUt its not impossibly >difficult. > >If Audacity exposes an interface of its own then it shouldn't >be too hard at all, you'll need to check the Audacity docs >and web site closely. > >Alan G. > > > > Hoo boy . . . Looking at the 'help' of the win32api has left this newbie hopelessly confused . . . Anyone who knows of a good Win32 tutorial, please let me know! Many thanks, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050115/f09e2418/attachment.html From marilyn at deliberate.com Sun Jan 16 06:43:42 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Sun Jan 16 06:47:31 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: p.p.s. I have only wrapped my lock around file-descriptor creations. Should I wrap it around closings too? Or the whole open -> close transaction? It sounds like error-prone work to do the latter. What am I missing? What should I be reading to get a better clue? I'll do some googling. Thank you. On Sat, 15 Jan 2005, Marilyn Davis wrote: > Dearest Tutors, > > Bah! It's not over yet. I don't know why, but again my file > descriptors are being trampled upon now and then. > > This time I can see in my log that I'm not trampling on them myself, > like I used to do, unless I'm making calls to the system that I'm not > aware of. > > And, first I get this worrisome output: > > close failed: [Errno 9] Bad file descriptor > close failed: [Errno 9] Bad file descriptor > close failed: [Errno 9] Bad file descriptor > > coming from nowhere. No traceback, nothing. > > I traced this down once to discover that it came out of python *after* > a raise, after a failed call to open a connection to mysql. In that > particular raise clause, I called my facility to pipe mail to exim, > opening 3 descriptors. I guessed then that when closing down, it > closes down the pipe descriptors that I had already closed? > > So, if it does this, if it closes down descriptors that I've already > closed, it's possible that it's closing down descriptors that have > also already been reopened because I'm running this thing like > lightning? > > And the other thought is that there is some other call I make that > uses a file descriptor that I'm not thinking of. os.listdir? > os.anything? > > Any new thoughts? > > Marilyn > > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- From singingxduck at gmail.com Sun Jan 16 06:51:08 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 06:51:25 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Using os.popen*() and os.spawn*() to interact with a DOS box) Message-ID: <41EA00CC.4040105@gmail.com> I did some googling, and found this in the archives of this mailing list: import os os.system('c:\\abaqus\\5.8-14\\abaqus.exe post') , where post was a command, *not* a file. Now, I tried something similar, since essentially what I wish to be able to do is have Audacity think I typed the 'R' key: os.system(r'c:\progra~1\audacity/audacity.exe R') All this managed to accomplish was Audacity opening (good) and a message from Audacity (not good): ("Could not open file: R" since i had to make it small to fit in the email message comfortably) Any ideas on a) why this didn't work and b) what to do to make something similar work are greatly appreciated. Thanks in advance, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- Skipped content of type multipart/related From singingxduck at gmail.com Sun Jan 16 07:22:29 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 07:22:47 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Using os.popen*() and os.spawn*() to interact with a DOS box) In-Reply-To: <41EA00CC.4040105@gmail.com> References: <41EA00CC.4040105@gmail.com> Message-ID: <41EA0825.1090804@gmail.com> Orri Ganel wrote: > I did some googling, and found this in the archives of this mailing list: > > import os > os.system('c:\\abaqus\\5.8-14\\abaqus.exe post') > > , where post was a command, *not* a file. Now, I tried something > similar, since essentially what I wish to be able to do is have > Audacity think I typed the 'R' key: > > os.system(r'c:\progra~1\audacity/audacity.exe R') > > All this managed to accomplish was Audacity opening (good) and a > message from Audacity (not good): > > > > ("Could not open file: R" since i had to make it small to fit in the > email message comfortably) > > Any ideas on a) why this didn't work and b) what to do to make > something similar work are greatly appreciated. > > Thanks in advance, > Orri > >-- >Email: singingxduck AT gmail DOT com >AIM: singingxduck >Programming Python for the fun of it. > Well, now at least I know why that didn't work, if not what to do about it: http://cvs.sourceforge.net/viewcvs.py/audacity/audacity-src/help/audacity.1?rev=1.5 "The only command-line arguments Audacity takes are the names of audio files to open." -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- Skipped content of type multipart/related From dyoo at hkn.eecs.berkeley.edu Sun Jan 16 07:40:33 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 16 07:40:39 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: > I have only wrapped my lock around file-descriptor creations. Should I > wrap it around closings too? Or the whole open -> close transaction? > It sounds like error-prone work to do the latter. What am I missing? Hi Marilyn, Can you send a link to the source code to the Tutor list? I'm getting the feeling that there's might be a design problem. Just adding locks whenever something doesn't work is not a sustainable way to write a multithreaded application. We have to see why your file descriptors being are being shared between threads. Is there a reason why you need to share them as global resources? From billburns at pennswoods.net Sun Jan 16 08:39:20 2005 From: billburns at pennswoods.net (Bill Burns) Date: Sun Jan 16 08:28:55 2005 Subject: [Tutor] Posting a large amount of code? Message-ID: <200501160239.20111.billburns@pennswoods.net> Hello, I've been working on a small GUI program (on & off) for a little while now and I'd love to have the group (or anyone for that matter) take a look at it and give me some pointers. Since I'm not a programmer and I don't do this continuously, I don't now if I'm on the right track or not. I'd love to have some feedback! I had originally posted my program to the list back in Nov. 04. The post can be found here: http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/2230994 Since that time it has changed significantly and the amount of code has grown. The program is now 552 lines long. According to pycount.py, there's 328 lines of code and the rest are docstrings, other comments and blank lines. I guess my question is, would it be acceptable to post this much code to the list? Maybe this really isn't a lot of code, but it seems like a lot to me, so I figured I'd ask first. If it is OK, would it be better to post the code in the body of the message or as an attachment? I would think an attachment would be better but then maybe attachments are not allowed on the list? I've used PyQt and Qt Designer to create this program. The main GUI and one other dialog were created with Designer and compiled to python with pyuic. These two separate modules are imported into a third module (the main program). I'd only send the 'main module' as the other two just contain auto-generated code from Designer & pyuic. The 'main module' contains the code I've written. Of course, the program will not run unless you have all three modules (and PyQt installed). I've probably been a little verbose with the comments in the code, but I figured if I post it and you don't have the GUI to go along with it, you could still get a feel for what the program is doing? Let me know. Comments accepted on or off the list. Thanks, Bill From ismaelgf at adinet.com.uy Sun Jan 16 08:30:14 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Sun Jan 16 08:29:47 2005 Subject: [Tutor] Lights game Message-ID: <41EA1806.2040104@adinet.com.uy> Hello list. I'd really appreciate any comments, particulary regarding style corrections. I'm a newbie... Thanks! Ismael import random import tkMessageBox from Tkinter import * class GUI: def __init__(self): self._crearGUI() def _crearGUI(self): self.root = Tk() self.root.title("Lights") self.botones = [[0,0,0],[0,0,0],[0,0,0]] for i in range(3): for j in range(3): action = lambda n=i, m=j: self._accion(n,m) self.botones[i][j] = Button(self.root, height=5, width=10, command=action) self.botones[i][j].grid(row=i, column=j, padx= 4, pady=4) self._updateBotones() self.root.mainloop() def _accion(self, i, j): self._click(i, j) try: self._checkWin() except ValueError: tkMessageBox.showinfo(title = "You won!", message = "You rock, kid!") if True == tkMessageBox.askyesno(title="What shall we do?", message="Play another?"): self.root.destroy() lights.__init__() ## Is this... too ugly? Is there a beter way? self.__init__() else: self.root.destroy() def _updateBotones(self): for i in range(3): for j in range(3): if lights.tablero[i][j] == True: self.botones[i][j].configure(bg="red") else: self.botones[i][j].configure(bg="grey") def _click(self, i, j): lights.click(i,j) self._updateBotones() def _checkWin(self): conteo = 0 for i in range(3): for j in range(3): if lights.tablero[i][j] == True: conteo +=1 if conteo == 9: print "GANO" raise ValueError return class Luces: def __init__(self): self.tablero = [[0,0,0],[0,0,0],[0,0,0]] self._hacerTablero() def _hacerTablero(self): for i in range(3): for j in range(3): self.tablero[i][j] = not random.randint(0,1) def _cambiarCelda(self, i, j): self.tablero[i][j] = not self.tablero[i][j] def _descartarNegativos(self, n): if n < 0: raise IndexError return n def click(self, i,j): self._cambiarCelda(i,j) try: self._cambiarCelda(self._descartarNegativos(i-1), j) except IndexError: pass try: self._cambiarCelda(self._descartarNegativos(i+1), j) except IndexError: pass try: self._cambiarCelda(i, self._descartarNegativos(j-1)) except IndexError: pass try: self._cambiarCelda(i, self._descartarNegativos(j+1)) except IndexError: pass if __name__ == '__main__': lights = Luces() lightsGUI = GUI() From alan.gauld at freenet.co.uk Sun Jan 16 09:36:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 09:35:53 2005 Subject: [Tutor] Faster procedure to filter two lists . Please help References: <20050114232807.48286.qmail@web53709.mail.yahoo.com> <01e101c4fae3$0c78a590$40b68651@xp> <41E95CEC.9070406@mail.telepac.pt> <1f7befae0501151559459a4f72@mail.gmail.com> <027d01c4fb64$8f905a00$40b68651@xp> <1f7befae05011516564e8c7407@mail.gmail.com> Message-ID: <028201c4fba6$6ab27640$40b68651@xp> > It's educational to look at the code anyway . Here it is, from > Python's listobject.c: Its been on my list of things to do for a long time. The biggest problem with C code is usually finding the bit you want, unless there is a good roadmap (aka design document) > static int > list_length(PyListObject *a) > { > return a->ob_size; > } > implementation. OTOH, people should be afraid to _write_ C code: it > will bite you every time <0.9 wink>. Yep, having run the maintenance team for a 3.5 milion line project in C/C++ its truly amazing the number of ways you can screw up in C! Its one of the reasons I now use Python and leave the C/Java stuff to others... Alan G. From alan.gauld at freenet.co.uk Sun Jan 16 09:39:09 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 09:39:41 2005 Subject: [Tutor] Using os.popen*() and os.spawn*() to interact with a dos-box References: <41E8A60A.9080203@gmail.com> <022d01c4fafb$205197d0$40b68651@xp> <41E94B3A.6090000@gmail.com> <024501c4fb50$5d8b9240$40b68651@xp> <41E9F265.8070200@gmail.com> Message-ID: <029401c4fba6$e168c820$40b68651@xp> > Hoo boy . . . Looking at the 'help' of the win32api has left this newbie > hopelessly confused . . . Anyone who knows of a good Win32 tutorial, > please let me know! Frankly, anyone doing anything serious on Win32 in Python should buy Mark Hammond's book. It covers most things to do with Windows and doesn't have too much overlap with the more general Python books. Alan G. From alan.gauld at freenet.co.uk Sun Jan 16 09:45:14 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 09:45:07 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Usingos.popen*() and os.spawn*() to interact with a DOS box) References: <41EA00CC.4040105@gmail.com> Message-ID: <02c501c4fba7$b3265300$40b68651@xp> > import os > os.system('c:\\abaqus\\5.8-14\\abaqus.exe post') > > , where post was a command, *not* a file. Now, I tried something > similar, since essentially what I wish to be able to do is have Audacity > think I typed the 'R' key: > > os.system(r'c:\progra~1\audacity/audacity.exe R') > > All this managed to accomplish was Audacity opening (good) and a message > from Audacity (not good): os.system() simply executes a command like you would in a dOS box. No magic. Thus is Audacity can do what you want from typing manual commands att a DOS ptrompt then you can use os.system() to replicate that. But if you can't do it from DOS then you can't use os.system() either. You need to look at the Audacity help files to see what options are available both from the command line and as an API to interact with Audacity. Until you know that you can't begin to know how to solve the problem in PYthon. Does Audacity have a mailing list? Why not try asking there what options are available? Alan G. From kent37 at tds.net Sun Jan 16 13:53:16 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 16 13:53:21 2005 Subject: [Tutor] Lights game In-Reply-To: <41EA1806.2040104@adinet.com.uy> References: <41EA1806.2040104@adinet.com.uy> Message-ID: <41EA63BC.7030701@tds.net> Nice work overall! A few comments inline... Kent Ismael Garrido wrote: > Hello list. > > I'd really appreciate any comments, particulary regarding style > corrections. I'm a newbie... > > Thanks! > Ismael > > > import random > import tkMessageBox > from Tkinter import * > lights is only needed by GUI(), it is a helper class. It would be better style to construct Luces() in GUI.__init__() and save it as an attribute. Then GUI stands by itself - it doesn't rely on a global variable or any outside setup. (You will have to change all accesses of lights to use self.lights) > class GUI: > def __init__(self): self.lights = Luces() > self._crearGUI() > > def _crearGUI(self): > self.root = Tk() > self.root.title("Lights") > self.botones = [[0,0,0],[0,0,0],[0,0,0]] > for i in range(3): > for j in range(3): > action = lambda n=i, m=j: self._accion(n,m) > self.botones[i][j] = Button(self.root, height=5, > width=10, command=action) > self.botones[i][j].grid(row=i, column=j, padx= 4, pady=4) > > self._updateBotones() > self.root.mainloop() > > def _accion(self, i, j): > self._click(i, j) > try: > self._checkWin() > except ValueError: This is a little strange. I would have _checkWin() return True or False instead of throwing an exception when the user wins! Then you would write if self._checkWin(): > tkMessageBox.showinfo(title = "You won!", message = "You > rock, kid!") > > if True == tkMessageBox.askyesno(title="What shall we do?", > message="Play another?"): > self.root.destroy() > lights.__init__() ## Is this... too ugly? Is there a > beter way? > self.__init__() You could use lights._hacerTablero() self._crearGUI() I agree it is little wierd to call __init__ from another function. Better to have a separate method that does exactly what you want. Then if the requirements of __init__ change you will still be doing the right thing here. Instead of destroying the window, why not just update it? if True == tkMessageBox.askyesno(title="What shall we do?", message="Play another?"): lights._hacerTablero() self._updateBotones() else: self.root.destroy() > else: > self.root.destroy() > def _updateBotones(self): > for i in range(3): > for j in range(3): > if lights.tablero[i][j] == True: > self.botones[i][j].configure(bg="red") > else: > self.botones[i][j].configure(bg="grey") > > def _click(self, i, j): > lights.click(i,j) > self._updateBotones() > > def _checkWin(self): > conteo = 0 > for i in range(3): > for j in range(3): > if lights.tablero[i][j] == True: conteo +=1 > if conteo == 9: > print "GANO" > raise ValueError > return if conteo == 9: print "GANO" return True else: return False or maybe even just return conteo == 9 > > class Luces: > def __init__(self): > self.tablero = [[0,0,0],[0,0,0],[0,0,0]] > self._hacerTablero() > > def _hacerTablero(self): > for i in range(3): > for j in range(3): > self.tablero[i][j] = not random.randint(0,1) > > def _cambiarCelda(self, i, j): > self.tablero[i][j] = not self.tablero[i][j] If you make _cambiarCelda() check for out-of-range indices then you could get rid of _descartarNegativos() and clean up click() considerably. def _cambiarCelda(self, i, j): if not (0<=i<3 and 0<=j<3): return self.tablero[i][j] = not self.tablero[i][j] def click(self, i,j): self._cambiarCelda(i,j) self._cambiarCelda(i-1, j) self._cambiarCelda(i+1, j) self._cambiarCelda(i, j-1) self._cambiarCelda(i, j+1) or if you prefer: def click(self, i,j): for ii, jj in [ (i,j), (i-1, j), (i+1, j), (i, j-1), (i, j+1) ]: self._cambiarCelda(ii, jj) > > def _descartarNegativos(self, n): > if n < 0: > raise IndexError > return n > > def click(self, i,j): > self._cambiarCelda(i,j) > try: > self._cambiarCelda(self._descartarNegativos(i-1), j) > except IndexError: > pass > try: > self._cambiarCelda(self._descartarNegativos(i+1), j) > except IndexError: > pass > try: > self._cambiarCelda(i, self._descartarNegativos(j-1)) > except IndexError: > pass > try: > self._cambiarCelda(i, self._descartarNegativos(j+1)) > except IndexError: > pass > > if __name__ == '__main__': > lights = Luces() Not needed if you make the change I suggest at the top. > lightsGUI = GUI() > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Sun Jan 16 14:10:18 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 16 14:10:24 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Using os.popen*() and os.spawn*() to interact with a DOS box) In-Reply-To: <41EA00CC.4040105@gmail.com> References: <41EA00CC.4040105@gmail.com> Message-ID: <41EA67BA.2020707@tds.net> Can you say something about why you want to do this? If you are trying to turn Audacity into a programmable sound processor, you might do better with something that is designed as a library for programmatic use. Googling 'python sound library' gives some promising hints. The Snack Sound Toolkit in particular. http://www.speech.kth.se/snack/ OTOH if what you want is some kind of macro system for Audacity then this won't help. Kent Orri Ganel wrote: > I did some googling, and found this in the archives of this mailing list: > > import os > os.system('c:\\abaqus\\5.8-14\\abaqus.exe post') > > , where post was a command, *not* a file. Now, I tried something > similar, since essentially what I wish to be able to do is have Audacity > think I typed the 'R' key: > > os.system(r'c:\progra~1\audacity/audacity.exe R') > > All this managed to accomplish was Audacity opening (good) and a message > from Audacity (not good): > > > > ("Could not open file: R" since i had to make it small to fit in the > email message comfortably) > > Any ideas on a) why this didn't work and b) what to do to make something > similar work are greatly appreciated. > > Thanks in advance, > Orri > > -- > Email: singingxduck AT gmail DOT com > AIM: singingxduck > Programming Python for the fun of it. > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at freenet.co.uk Sun Jan 16 16:07:46 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 16:07:30 2005 Subject: [Tutor] Posting a large amount of code? References: <200501160239.20111.billburns@pennswoods.net> Message-ID: <02fb01c4fbdd$23204c80$40b68651@xp> > I guess my question is, would it be acceptable to post this much code to the > list? Probably about the limit for posting, but if you can put it on a web site somewhere and post a URL that would be fine... > the body of the message or as an attachment? I would think an attachment > would be better but then maybe attachments are not allowed on the list? Since there are several files attachments would make more sense but if only posting main.py (How big is it? Is that the 500 lines? If so its a biggish single module.) then inline is OK. > I've probably been a little verbose with the comments in the code, but I > figured if I post it and you don't have the GUI to go along with it, you could > still get a feel for what the program is doing? That makes sense. Alan G. From billburns at pennswoods.net Sun Jan 16 16:49:30 2005 From: billburns at pennswoods.net (Bill Burns) Date: Sun Jan 16 16:39:11 2005 Subject: [Tutor] Posting a large amount of code? Message-ID: <200501161049.30727.billburns@pennswoods.net> I accidentally sent my reply to Kent only. So I'm forwarding it to the list. Bill ---------- Forwarded Message ---------- Subject: Re: [Tutor] Posting a large amount of code? Date: Sunday 16 January 2005 10:14 am From: Bill Burns To: Kent Johnson [Bill] > I guess my question is, would it be acceptable to post this much code to > the list? Maybe this really isn't a lot of code, but it seems like a lot to > me, so I figured I'd ask first. [Kent] > I don't know if there is any convention for how much code is too much to > post. If you have a web site you could put it on that is a good > alternative. I don't mind if you just post it... [Bill] Thanks for the reply Kent. I don't have a web site so I guess that option is ruled out. So if you don't mind the post (and hopefully nobody else minds) here we go............ I appreciate all feedback. Thanks, Bill #! /usr/bin/env python """ pipeCalc.py - Calculates various data about HVAC/Plumbing pipe. This program calculates the following data: A). Total volume (US gallons) of water inside the pipe. B). The weight of the water inside the pipe (weight based on 60 F). C). The actual weight of the pipe, itself. The only input required from the user, is a pipe length (in feet). Currently the program works on the following types of pipe: A). Carbon Steel (Standard) B). Carbon Steel (Extra Strong) C). Copper (Type L) D). PVC (Schedule 40) E). PVC (Schedule 80) F). PEX tubing. All of the pipe calculations and totals can be written out to a csv file. After the report is written to disk, the user is given the option to open the report. I've also provided a method to write out the data that is used in making all of the calculations. This method writes to disk a file called PipeData.csv. I built the GUI using QT Designer and pyuic. The GUI is a separate module which is imported into this module. There's also a separate dialog which was created and gets imported in the same manner. The GUI contains a tabbed widget with multiple pages (tabs). Each page corresponds to a specific type of pipe. There are multiple lineEdits on each page, with each lineEdit representing a different size of pipe. The user selects a page representing a certain type of pipe and then enters the pipe length into the lineEdit corresponding to the size they want. The GUI also has a lineEdit separate from the others which displays the total gallons of water. This total is continuously updated as the user enters pipe lengths. The GUI also contains four pushButtons. They are labeled and function as follows: A). Report - after all data is entered, the user clicks this button and the PipeReport is written. A dialog will then popup allowing the report to be opened. B). Pipe Data - writes to disk the pipe specifications that are used to calculate all of the data. A dialog will popup allowing the user to write out the data for *all* pipes or the user has the option to choose any combination of individual pipe(s). C). Clear - clears the user input from all lineEdits. D). Exit - exits the program. I've also provided access to all of the above functions/methods via a popup menu that is shown when the user right-clicks on the form. The dictionary (pipeDict) holds all of the various data pertaining to the pipes. The dict *key* holds the following information (in this order): 0). lineEdit - This is the name of the the lineEdit which corresponds to a specific type and size of pipe as shown on the GUI. The numbering scheme provides the correct sort - Thanks, Bob ;) 1). material type - The type of pipe, I.e., Steel, Copper, Plastic. 2). size - The size designation of the pipe, expressed in inches. 3). weight - The weight of the pipe per foot (LBS). The dict *value* holds the inside diameter of each pipe. This value is used for calculating volume. """ pipeDict = \ { ('lineEdit001','Copper(Type L)','1/2"',.285): .585, ('lineEdit002','Copper(Type L)','3/4"',.455): .83, ('lineEdit003','Copper(Type L)','1"',.655): 1.075, ('lineEdit004','Copper(Type L)','1-1/4"',.884): 1.32, ('lineEdit005','Copper(Type L)','1-1/2"',1.14): 1.565, ('lineEdit006','Copper(Type L)','2"',1.75): 2.055, ('lineEdit007','Copper(Type L)','2-1/2"',2.48): 2.545, ('lineEdit008','Copper(Type L)','3"',3.33): 3.035, ('lineEdit009','Copper(Type L)','3-1/2"',4.29): 3.525, ('lineEdit010','Copper(Type L)','4"',5.38): 4.015, ('lineEdit011','Copper(Type L)','5"',7.61): 5.00, ('lineEdit012','Copper(Type L)','6"',10.2): 5.985, ('lineEdit013','Copper(Type L)','8"',19.3): 7.925, ('lineEdit014','Copper(Type L)','10"',30.1): 9.875, ('lineEdit015','Copper(Type L)','12"',40.4): 11.845, ('lineEdit016','PEX Tubing','1/4"',.0261): .25, ('lineEdit017','PEX Tubing','3/8"',.0413): .35, ('lineEdit018','PEX Tubing','1/2"',.0535): .475, ('lineEdit019','PEX Tubing','5/8"',.0752): .574, ('lineEdit020','PEX Tubing','3/4"',.1023): .671, ('lineEdit021','PEX Tubing','1"',.1689): .863, ('lineEdit022','PEX Tubing','1-1/4"',.2523): 1.053, ('lineEdit023','PEX Tubing','1-1/2"',.3536): 1.243, ('lineEdit024','PEX Tubing','2"',.6010): 1.629, ('lineEdit025','PVC(Sch40)','1/2"',.16): .731, ('lineEdit026','PVC(Sch40)','3/4"',.22): .937, ('lineEdit027','PVC(Sch40)','1"',.32): 1.182, ('lineEdit028','PVC(Sch40)','1-1/4"',.43): 1.52, ('lineEdit029','PVC(Sch40)','1-1/2"',.52): 1.755, ('lineEdit030','PVC(Sch40)','2"',.7): 2.221, ('lineEdit031','PVC(Sch40)','2-1/2"',1.1): 2.672, ('lineEdit032','PVC(Sch40)','3"',1.44): 3.284, ('lineEdit033','PVC(Sch40)','4"',2.05): 4.263, ('lineEdit034','PVC(Sch40)','6"',3.61): 6.345, ('lineEdit035','PVC(Sch40)','8"',5.45): 8.303, ('lineEdit036','PVC(Sch40)','10"',7.91): 10.385, ('lineEdit037','PVC(Sch40)','12"',10.35): 12.344, ('lineEdit038','PVC(Sch80)','1/2"',.21): .693, ('lineEdit039','PVC(Sch80)','3/4"',.28): .896, ('lineEdit040','PVC(Sch80)','1"',.4): 1.136, ('lineEdit041','PVC(Sch80)','1-1/4"',.57): 1.469, ('lineEdit042','PVC(Sch80)','1-1/2"',.69): 1.70, ('lineEdit043','PVC(Sch80)','2"',.95): 2.157, ('lineEdit044','PVC(Sch80)','2-1/2"',1.45): 2.599, ('lineEdit045','PVC(Sch80)','3"',1.94): 3.20, ('lineEdit046','PVC(Sch80)','4"',2.83): 4.163, ('lineEdit047','PVC(Sch80)','6"',5.41): 6.193, ('lineEdit048','PVC(Sch80)','8"',8.22): 8.125, ('lineEdit049','PVC(Sch80)','10"',12.28): 10.157, ('lineEdit050','PVC(Sch80)','12"',17.1): 12.063, ('lineEdit051','Steel(Std)','1/2"',.85): .622, ('lineEdit052','Steel(Std)','3/4"',1.13): .824, ('lineEdit053','Steel(Std)','1"',1.678): 1.049, ('lineEdit054','Steel(Std)','1-1/4"',2.272): 1.38, ('lineEdit055','Steel(Std)','1-1/2"',2.717): 1.61, ('lineEdit056','Steel(Std)','2"',3.652): 2.067, ('lineEdit057','Steel(Std)','2-1/2"',5.79): 2.469, ('lineEdit058','Steel(Std)','3"',7.57): 3.068, ('lineEdit059','Steel(Std)','3-1/2"',9.11): 3.548, ('lineEdit060','Steel(Std)','4"',10.79): 4.026, ('lineEdit061','Steel(Std)','5"',14.62): 5.047, ('lineEdit062','Steel(Std)','6"',18.97): 6.065, ('lineEdit063','Steel(Std)','8"',28.55): 7.981, ('lineEdit064','Steel(Std)','10"',40.48): 10.02, ('lineEdit065','Steel(Std)','12"',49.6): 12.00, ('lineEdit066','Steel(Std)','14"',55): 13.25, ('lineEdit067','Steel(Std)','16"',63): 15.25, ('lineEdit068','Steel(Std)','18"',71): 17.25, ('lineEdit069','Steel(Std)','20"',79): 19.25, ('lineEdit070','Steel(Std)','22"',87): 21.25, ('lineEdit071','Steel(Std)','24"',95): 23.25, ('lineEdit072','Steel(Std)','30"',119): 29.25, ('lineEdit073','Steel(Std)','36"',143): 35.25, ('lineEdit074','Steel(Std)','42"',167): 41.25, ('lineEdit075','Steel(Std)','48"',191): 47.25, ('lineEdit076','Steel(XS)','1/2"',1.087): .546, ('lineEdit077','Steel(XS)','3/4"',1.473): .742, ('lineEdit078','Steel(XS)','1"',2.171): .957, ('lineEdit079','Steel(XS)','1-1/4"',2.996): 1.278, ('lineEdit080','Steel(XS)','1-1/2"',3.631): 1.50, ('lineEdit081','Steel(XS)','2"',5.022): 1.939, ('lineEdit082','Steel(XS)','2-1/2"',7.66): 2.323, ('lineEdit083','Steel(XS)','3"',10.25): 2.90, ('lineEdit084','Steel(XS)','3-1/2"',12.51): 3.364, ('lineEdit085','Steel(XS)','4"',14.98): 3.826, ('lineEdit086','Steel(XS)','5"',20.78): 4.813, ('lineEdit087','Steel(XS)','6"',28.57): 5.761, ('lineEdit088','Steel(XS)','8"',43.39): 7.625, ('lineEdit089','Steel(XS)','10"',54.74): 9.75, ('lineEdit090','Steel(XS)','12"',65.4): 11.75, ('lineEdit091','Steel(XS)','14"',72): 13.00, ('lineEdit092','Steel(XS)','16"',83): 15.00, ('lineEdit093','Steel(XS)','18"',93): 17.00, ('lineEdit094','Steel(XS)','20"',105): 19.00, ('lineEdit095','Steel(XS)','22"',115): 21.00, ('lineEdit096','Steel(XS)','24"',125): 23.00, ('lineEdit097','Steel(XS)','30"',158): 29.00, ('lineEdit098','Steel(XS)','36"',190): 35.00, ('lineEdit099','Steel(XS)','42"',222): 41.00, ('lineEdit100','Steel(XS)','48"',254): 47.50 } import sys import os import types import csv from qt import * try: import psyco psyco.full() except ImportError: pass from pipeCalcGUI import PipeForm from dataform import DataForm class PipeConnector(PipeForm): """This class contains all of the methods used to make all of the pipe calculations. First, some connections are made between the buttons on the main form and their methods. Then I set the text alignment for lineEditTotal. Provide a 'connection' to the DataDialog class and the EventFilter class. Create a list (self.lineEdits) to hold all of the various data about the lineEdits and pipe, then create a second list (self.lineEditIns) to hold all lineEdit instances. The lists are then populated. Next, a connection is made to the galCalc() method, the text alignment is set on the remaining lineEdits and a validator is set that only allows numbers to be entered into the lineEdits. """ def __init__(self, parent=None): PipeForm.__init__(self, parent) self.connect(self.buttonClear,SIGNAL("clicked()"), self.clearLineEdits) self.connect(self.buttonExit,SIGNAL("clicked()"),self,SLOT("close()")) self.connect(self.buttonPrint,SIGNAL("clicked()"),self.pipeReport) self.lineEditTotal.setAlignment(QLineEdit.AlignRight) self.dataDialog = DataDialog(self, parent) self.connect(self.buttonData,SIGNAL("clicked()"), self.dataDialog.showDialog) self.eventfilter = EventFilter(self) self.installEventFilter(self.eventfilter) self.lineEdits = [] self.lineEditIns = [] for name, typ, size, weight in pipeDict: ins = getattr(self, name) ID = pipeDict[name, typ, size, weight] self.lineEdits.append([ins,name,typ,size,weight,ID]) self.lineEditIns.append(ins) self.connect(ins,SIGNAL("textChanged(const QString &)"), self.galCalc) ins.setAlignment(QLineEdit.AlignRight) validator=QIntValidator(0.00, 9999999.00, ins) ins.setValidator(validator) def volCalc(self, ID, length): """Calculates the volume/gallons of water inside of the various pipe. """ from math import pi gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) return gal def galCalc(self): """Displays a running tally of the total gallons for all pipe entered into each lineEdit. Recalculates whenever the data changes in any of the lineEdits. """ tmp = [] for ins, name, typ, size, weight, ID in self.lineEdits: if name != "lineEditTotal": length = ins.displayText() if length: length = int(str(length),10) gal = self.volCalc(ID, length) tmp.append(gal) total = sum(tmp) total = "%0.2f" % total self.lineEditTotal.setText(total) def pipeReport(self): """Calculates the gallons of water, water weight and weight of the pipe for each pipe entered. Provides a summation of total gallons, total water weight and total pipe weight. Sends the data to writeReport() to be written to a csv file. """ if self.lineEditTotal.displayText(): data = [] waterWeightTotal = [] pipeWeightTotal = [] for ins, name, typ, size, weight, ID in self.lineEdits: if name != "lineEditTotal": length = ins.displayText() if length: length = int(str(length)) gal = self.volCalc(ID, length) waterWeight = (8.338*gal) waterWeightTotal.append(waterWeight) waterWeightSum = sum(waterWeightTotal) pipeWeight = (weight*length) pipeWeightTotal.append(pipeWeight) pipeWeightSum = sum(pipeWeightTotal) data.append([name,typ,size,ID,length, gal,waterWeight,pipeWeight]) data.sort() filteredData = self.filterPipeData(data) self.writeReport('PipeReport.csv',filteredData, waterWeightSum,pipeWeightSum) def pipeSpecs(self, pipes): """Method to write out the pipe specifications, I.e., material, size ID, gallons, water weight & pipe weight. All data is based on one (1) foot of pipe. Essentially, I wanted a method that allowed the user to see what data is being used for the various calculations. The data is then sent to writeReport() to be written to csv file. This method is called from within the DataDialog class below. Specifically it's called from writeData(). """ specs = [] for pipe in pipes: for name, typ, size, weight in pipeDict: if pipe == typ: ID = pipeDict[name, typ, size, weight] length = None gal = self.volCalc(ID,1) waterWeight = (8.338*gal) specs.append([name,typ,size,ID,length, gal,waterWeight,weight]) specs.sort() filteredPipeSpec = self.filterPipeData(specs) self.writeReport('PipeData.csv',filteredPipeSpec,None,None) def writeReport(self, fname, data, wwSum, pwSum): """Writes the csv file for both pipeReport() and pipeSpecs(), then calls openReport() which prompts the user to open the files. The data generated from pipeSpecs() does *not* contain a water weight sum (wwSum), pipe weight sum (pwSum) or a pipe length. I need to distinguish between the two reports. If 'wwSum == None', the method was called from pipeSpecs(), and we write the report one way, else it was called from pipeReport() and it is written a different way. """ report = file(fname, 'w') writer = csv.writer(report) #,dialect='excel') if wwSum == None: fields = ("Material Type","Size","ID","Gallons/Ft", "Water Weight/Ft", "Pipe Weight/Ft") writer.writerow(fields) for row in data: writer.writerow(row) report.close() self.openReport(fname) else: fields = ("Material Type","Size","ID","Pipe Length", "Gallons","Water Weight", "Pipe Weight") writer.writerow(fields) for row in data: writer.writerow(row) galTotal = self.lineEditTotal.displayText() writer.writerow(("Totals:","","","",galTotal,wwSum,pwSum)) report.close() self.openReport(fname) def openReport(self, file): """Method to open either PipeReport.csv or PipeData.csv. Opens the reports using OpenOffice Calc. """ msgBox = QMessageBox.question(self, "Report", "Report written. Open the Report?", "Yes","No","Cancel",2,-1) if msgBox == 0: os.popen2('/opt/OpenOffice.org/program/soffice -calc %s' % file) #I use CrossOver Office, so I can optionally open the csv files #with Excel. #os.popen2('~/cxoffice/bin/excel %s' % file) else: pass def filterPipeData(self, data): """Method to remove the lineEdit name from the lists. If called by pipeSpecs(), also removes the length, as the length is not needed in that report. I need to incorporate Kent's list comprehension into this! """ fData = [] for name,typ,size,ID,length,gal,waterWeight,pipeWeight in data: if length == None: #True, if called from PipeSpecs() fData.append([typ,size,ID,gal,waterWeight,pipeWeight]) else: fData.append([typ,size,ID,length,gal,waterWeight,pipeWeight]) return fData def clearLineEdits(self): """Clears the data in all lineEdits. """ for ins in self.lineEditIns: ins.clear() self.lineEditTotal.setText("") class DataDialog(DataForm): """A class for a dialog which displays options for writing the pipe specifications. The dialog contains two radioButtons, six checkboxes and two pushButtons. The first radioButton is tagged All Pipe Data and is used to write out all of the pipe data (go figure). The second is tagged Individual Pipe Data and when it is selected the six checkboxes become enabled (they are initally 'grayed out' and not active). Each checkbox represents a specific type of pipe and the user has the option to select any combination of the six pipe to write to file. """ def __init__(self,parent=None,name = None,modal=1,fl=0): DataForm.__init__(self, parent,name,modal,fl) self.connect(self.radioButtonAll,SIGNAL("clicked()"), self.selectAll) self.connect(self.radioButtonIndivid,SIGNAL("clicked()"), self.selectIndividual) self.connect(self.cancelButton,SIGNAL("clicked()"), self,SLOT("close()")) self.connect(self.printButton,SIGNAL("clicked()"),self.writeData) checkBoxes = ['checkBox1','checkBox2', 'checkBox3','checkBox4', 'checkBox5','checkBox6'] self.checkBoxIns = [] for checkBox in checkBoxes: ins = getattr(self, checkBox) self.checkBoxIns.append(ins) def showDialog(self): """This method will 'show' the pipe data dialog. When the dialog is shown, the checkBoxes should *not* be checked or enabled and radioButtonAll should be checked. """ for checkBox in self.checkBoxIns: checkBox.setChecked(0) self.radioButtonAll.setChecked(1) self.selectAll() self.show() def selectAll(self): """User is selecting all pipe. The checkBoxes are disabled and they should not be checked. """ for checkBox in self.checkBoxIns: checkBox.setEnabled(0) checkBox.setChecked(0) def selectIndividual(self): """User is selecting individual pipe so enable the checkBoxes. """ for checkBox in self.checkBoxIns: checkBox.setEnabled(1) def writeData(self): """Used to write out the pipe data. This calls the pipeSpecs() method in the PipeConnector class. If radioButtonAll is checked then all pipe data is written out. Else radioButtonIndivid and the user can make individual pipe selections via the one of the six checkBoxes. """ self.pipeCon = PipeConnector() if self.radioButtonAll.isChecked(): self.pipeCon.pipeSpecs(('Steel(Std)', 'Steel(XS)', 'Copper(Type L)', 'PVC(Sch40)', 'PVC(Sch80)', 'PEX Tubing')) self.close() else: try: pipes = [] for checkBox in self.checkBoxIns: if checkBox.isChecked(): pipe = str(checkBox.text()) pipes.append(pipe) self.pipeCon.pipeSpecs(pipes) self.close() except UnboundLocalError: QMessageBox.warning(self, "Selection Error", "Please select a pipe.", "OK") class PopupMenu(QPopupMenu): """class for a pop-up menu used in the EventFilter class. The popup will be displayed at the current cursor position. """ def __init__(self, parent=None): QPopupMenu.__init__(self, parent) def showMenu(self): self.exec_loop(QCursor.pos()) class EventFilter(PipeConnector): """This will show the popup menu, when a right mouse click event occurs. The pop-up displays various functions available to the user. Most of these functions are also available via buttons on the 'main' form. Two functions that are only available here are fill() and writeDict(). """ def __init__(self, parent): PipeForm.__init__(self, parent) self.parent = parent def eventFilter(self, object, event): if event.type() == QEvent.MouseButtonPress and \ event.button() == QMouseEvent.RightButton: pm = PopupMenu() pm.insertItem("Pipe Report", self.report) pm.insertItem("Pipe Data", self.pipeData) pm.insertItem("Clear", self.clr) pm.insertItem("Fill w/100's", self.fill) pm.insertItem("Write Dict to File", self.writeDict) pm.insertItem("Exit Pipe Calc", self.closePipeCalc) pm.showMenu() return 1 return 0 def report(self): """Calls the pipeReport() method in the PipeConnector class. """ self.parent.pipeReport() def pipeData(self): """Calls showDialog() in the DataDialog class, which in turn calls pipeSpecs() in the PipeConnector class. """ dd = DataDialog(self) dd.showDialog() def clr(self): """Calls the clearLineEdits() method in the PipeConnector class. """ self.parent.clearLineEdits() def fill(self): """Fills all lineEdits will a value of '100'. Used just for testing. """ for ins in self.parent.lineEditIns: ins.setText("100") def writeDict(self): """Writes the dict (pipeDict) to a text file. The last line (which is commented out) can 'import' the file as 'importedpipeDict. Found this on comp.lang.python. Not sure where I'm going with this but I'm thinking of taking the pipeDict out of this module and putting it into a separate config file/module. Then at runtime, I'll load the dict from that file. I'm thinking if the dictionary is separate from this module, I can provide user access to the data. Then maybe thru an interface, the user could see, adjust, or change the pipe data. Who knows? """ print >>file('pipeDict.txt','w+'),str(pipeDict) #exec('importedpipeDict=dict('+file('pipeDict.txt','r').read()+')') def closePipeCalc(self): """Exit the program.""" self.parent.close() if __name__ == "__main__": app = QApplication(sys.argv) QObject.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()")) win = PipeConnector() app.setMainWidget(win) win.show() app.exec_loop() ------------------------------------------------------- From singingxduck at gmail.com Sun Jan 16 16:50:47 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 16:51:02 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Using os.popen*() and os.spawn*() to interact with a DOS box) In-Reply-To: <41EA67BA.2020707@tds.net> References: <41EA00CC.4040105@gmail.com> <41EA67BA.2020707@tds.net> Message-ID: <41EA8D57.3080902@gmail.com> Kent Johnson wrote: > Can you say something about why you want to do this? If you are trying > to turn Audacity into a programmable sound processor, you might do > better with something that is designed as a library for programmatic > use. Googling 'python sound library' gives some promising hints. The > Snack Sound Toolkit in particular. http://www.speech.kth.se/snack/ > > OTOH if what you want is some kind of macro system for Audacity then > this won't help. > > Kent > > > Orri Ganel wrote: > >> I did some googling, and found this in the archives of this mailing >> list: >> >> import os >> os.system('c:\\abaqus\\5.8-14\\abaqus.exe post') >> >> , where post was a command, *not* a file. Now, I tried something >> similar, since essentially what I wish to be able to do is have >> Audacity think I typed the 'R' key: >> >> os.system(r'c:\progra~1\audacity/audacity.exe R') >> >> All this managed to accomplish was Audacity opening (good) and a >> message from Audacity (not good): >> >> >> >> ("Could not open file: R" since i had to make it small to fit in the >> email message comfortably) >> >> Any ideas on a) why this didn't work and b) what to do to make >> something similar work are greatly appreciated. >> >> Thanks in advance, >> Orri >> >> -- >> Email: singingxduck AT gmail DOT com >> AIM: singingxduck >> Programming Python for the fun of it. >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Actually, what I want to do is set Audacity up so that it automatically begins recording when a song plays in Windows Media Player (to begin with) and stops when the song is finished playing. I've already figured out how to force it to record and stop when I want to (though in a kind of ugly hackish way)(*), and now I'm working on learning the controls of WMP so I can test things like time left in the song, etc. I dl'ed the WMP 10 SDK but haven't really had a chance to take a thorough look at it yet, so hopefully it'll have what I need. Cheers, Orri (*) class autoRecord: """Uses Audacity(TM) to automatically start recording when a song starts playing in the media player of your choice (including Launch(TM), Realplayer(TM), Quicktime(TM), Musicmatch(TM) and Windows Media Player(TM)). Support for more will be added as input is received. Requirements: - Audacity(TM) for Windows (Mac and/or Linux versions pending) - Python 2.4 or higher - Launch(TM), Realplayer(TM), Quicktime(TM), Musicmatch(TM), or Windows Media Player(TM) - Windows XP Home Edition (Mac/Linux/Windows 95/98/ME/NT pending) """ ## eventually (i hope), autoRecord will do all of the above . . . def __init__(self): import time def record(self): import os, win32api, thread, time thread.start_new_thread(os.system,(r'c:\progra~1\audacity/audacity.exe',)) time.sleep(1) thread.start_new_thread(win32api.keybd_event, (82,1)) ## simulates keypress 'r' (which is audacity's shortcut for recording) thread.start_new_thread(self.stopRecord,()) def stopRecord(self): import time, win32api while wmp.currentMedia.duration: ## doesn't work yet, since I'm still looking into WMP controls pass time.sleep(2) ## makes sure all of the sound is captured by waiting a bit longer win32api.keybd_event(83,1) ## simulates keypress 's' (which is audacity's shortcut for stopping) aR = autoRecord() aR.record() -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From singingxduck at gmail.com Sun Jan 16 16:53:55 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 16:54:10 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <200501161049.30727.billburns@pennswoods.net> References: <200501161049.30727.billburns@pennswoods.net> Message-ID: <41EA8E13.9010309@gmail.com> Bill Burns wrote: >I accidentally sent my reply to Kent only. So I'm forwarding it to the list. >Bill >---------- Forwarded Message ---------- > >Subject: Re: [Tutor] Posting a large amount of code? >Date: Sunday 16 January 2005 10:14 am >From: Bill Burns >To: Kent Johnson > >[Bill] > > > > >>I guess my question is, would it be acceptable to post this much code to >>the list? Maybe this really isn't a lot of code, but it seems like a lot to >>me, so I figured I'd ask first. >> >> > > > >[Kent] > > > >>I don't know if there is any convention for how much code is too much to >>post. If you have a web site you could put it on that is a good >>alternative. I don't mind if you just post it... >> >> > >[Bill] >Thanks for the reply Kent. I don't have a web site so I guess that option is >ruled out. So if you don't mind the post (and hopefully nobody else minds) >here we go............ I appreciate all feedback. > >Thanks, > >Bill > For future reference, you can always post code to paste: http://rafb.net/paste/ Cheers, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050116/bcce9994/attachment.html From billburns at pennswoods.net Sun Jan 16 17:20:48 2005 From: billburns at pennswoods.net (Bill Burns) Date: Sun Jan 16 17:10:22 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <02fb01c4fbdd$23204c80$40b68651@xp> References: <200501160239.20111.billburns@pennswoods.net> <02fb01c4fbdd$23204c80$40b68651@xp> Message-ID: <200501161120.48010.billburns@pennswoods.net> [Bill] > I guess my question is, would it be acceptable to post this much code to > the list? [Alan] > Probably about the limit for posting, but if you can put it on > a web site somewhere and post a URL that would be fine... [Bill] Kent had made the same suggestion as well but alas I have no site I can post to... [Bill] > the body of the message or as an attachment? I would think an attachment > would be better but then maybe attachments are not allowed on the list? [Alan] > Since there are several files attachments would make more sense > but if only posting main.py (How big is it? Is that the 500 lines? > If so its a biggish single module.) then inline is OK. [Bill] Yes, it is the 500+ line module and I've posted it inline in my reply to Kent. Glad to hear that it's OK post since I've already sent it, D'oh! I guess like the try, except idiom says, "It's better to ask for forgiveness, than ask for permission." :-) [Bill] > I've probably been a little verbose with the comments in the code, but I > figured if I post it and you don't have the GUI to go along with it, you > could still get a feel for what the program is doing? [Alan] > That makes sense. [Bill] I'm glad to hear that! Hopefully that's what I've achieved. Thank you for the feedback! Bill From billburns at pennswoods.net Sun Jan 16 17:27:27 2005 From: billburns at pennswoods.net (Bill Burns) Date: Sun Jan 16 17:17:00 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <41EA8E13.9010309@gmail.com> References: <200501161049.30727.billburns@pennswoods.net> <41EA8E13.9010309@gmail.com> Message-ID: <200501161127.27104.billburns@pennswoods.net> On Sunday 16 January 2005 10:53 am, Orri Ganel wrote: > For future reference, you can always post code to paste: > > http://rafb.net/paste/ > > Cheers, > Orri Thanks Orri! I didn't know there was a site like that. I'll check it out and see how it works! Thanks again. Bill From alan.gauld at freenet.co.uk Sun Jan 16 19:28:36 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 16 19:28:18 2005 Subject: [Tutor] Posting a large amount of code? References: <200501161049.30727.billburns@pennswoods.net> Message-ID: <031801c4fbf9$31c24420$40b68651@xp> Some quick feedback based on a quick scim through. It looks like your pipe logic is all mixed up with the GUI vpde. It would be a god excercise to extract all the p[ipe code into a separate class and the GUI methods call those class methods to get the work done. THis would allow for a fairly easy port to a different GUI or even the creation of a command line version, so you could do: bash$ pipestats.py --content-weight 15 12 bash$ pipestats.py --summary 22 1/2 If you then put that pipe object in a separate module you could extend it for future projects... Separating all GUI code from the logical functions is always a good idea. Simply get the GUI to reference an instance of the pipe(having selected the sizes and passed them in as constructor values maybe?) > def galCalc(self): > """Displays a running tally of the total gallons for all > pipe entered into each lineEdit. Recalculates whenever > the data changes in any of the lineEdits. > """ > tmp = [] > for ins, name, typ, size, weight, ID in self.lineEdits: > if name != "lineEditTotal": > length = ins.displayText() > if length: > length = int(str(length),10) > gal = self.volCalc(ID, length) > tmp.append(gal) > total = sum(tmp) > total = "%0.2f" % total > self.lineEditTotal.setText(total) This is just an example, see how both the calculation and the display of the result(*) are mixed up in the same method? It would be good to split up the code to have a method that does the calculation and returns the result and another that calls the first and puts the result into the GUI. This may require that you pass more parameters into the calculation metjods but the gain in separation is worth it IMHO. (*)Not knowing Qt I'm assuming that the setText() is a GUI method! If it isn't I may be drawing invalid conclusions. Alan G. From singingxduck at gmail.com Sun Jan 16 20:51:07 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 16 20:51:26 2005 Subject: [Tutor] Setting the focus to a window Message-ID: <41EAC5AB.5000901@gmail.com> Hello all, As part of a project I'm working on (see archive of previous emails from me in the past 2 days or so), I need to change the focus from the current window to another window (from Windows Media Player to Audacity so that the simulated keypress will have an effect). I have googled and looked in the docs and all, and I can't seem to find a way to do this (at least, not with Windows). I found something that sounds about right in MacPython, but that doesn't help me . . . Any suggestions on how to do this are greatly appreciated. Thanks in advance, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From cyresse at gmail.com Sun Jan 16 21:41:40 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 16 21:41:43 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <200501160239.20111.billburns@pennswoods.net> References: <200501160239.20111.billburns@pennswoods.net> Message-ID: http://www.rafb.net/paste/ is good for when you need to make your code accessible - best of all it formats the code for you. Just copy & paste your code, and distribute the link it gives you. Your 'pasted' code expires after 24 hours though. Regards, Liam Clarke On Sun, 16 Jan 2005 02:39:20 -0500, Bill Burns wrote: > Hello, > > I've been working on a small GUI program (on & off) for a little while now and > I'd love to have the group (or anyone for that matter) take a look at it and > give me some pointers. Since I'm not a programmer and I don't do this > continuously, I don't now if I'm on the right track or not. I'd love to have > some feedback! > > I had originally posted my program to the list back in Nov. 04. > > The post can be found here: > http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/2230994 > > Since that time it has changed significantly and the amount of code has grown. > The program is now 552 lines long. According to pycount.py, there's 328 lines > of code and the rest are docstrings, other comments and blank lines. > > I guess my question is, would it be acceptable to post this much code to the > list? Maybe this really isn't a lot of code, but it seems like a lot to me, so > I figured I'd ask first. If it is OK, would it be better to post the code in > the body of the message or as an attachment? I would think an attachment > would be better but then maybe attachments are not allowed on the list? > > I've used PyQt and Qt Designer to create this program. The main GUI and one > other dialog were created with Designer and compiled to python with pyuic. > These two separate modules are imported into a third module (the main > program). I'd only send the 'main module' as the other two just contain > auto-generated code from Designer & pyuic. The 'main module' contains > the code I've written. Of course, the program will not run unless you have all > three modules (and PyQt installed). > > I've probably been a little verbose with the comments in the code, but I > figured if I post it and you don't have the GUI to go along with it, you could > still get a feel for what the program is doing? > > Let me know. Comments accepted on or off the list. > > Thanks, > > Bill > _______________________________________________ > 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. From cyresse at gmail.com Sun Jan 16 21:50:05 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 16 21:50:08 2005 Subject: [Tutor] Setting the focus to a window In-Reply-To: References: <41EAC5AB.5000901@gmail.com> Message-ID: Oops, to the list also. ---------- Forwarded message ---------- From: Liam Clarke Date: Mon, 17 Jan 2005 09:49:48 +1300 Subject: Re: [Tutor] Setting the focus to a window To: Orri Ganel Hi Orri, What Alan said earlier in the topic. You want to use the Win32UI API, which Mark Hammond has documented in a book. You can also download his WinPython, which includes some documentation, and a link to a tutorial. http://starship.python.net/crew/skippy/ Regards, Liam Clarke PS the Windows API's are also good for manipulating MS software using COM objects, don't know how Audacity handles COM/ActiveX On Sun, 16 Jan 2005 14:51:07 -0500, Orri Ganel wrote: > Hello all, > > As part of a project I'm working on (see archive of previous emails from > me in the past 2 days or so), I need to change the focus from the > current window to another window (from Windows Media Player to Audacity > so that the simulated keypress will have an effect). I have googled and > looked in the docs and all, and I can't seem to find a way to do this > (at least, not with Windows). I found something that sounds about right > in MacPython, but that doesn't help me . . . Any suggestions on how to > do this are greatly appreciated. > > Thanks in advance, > Orri > > -- > Email: singingxduck AT gmail DOT com > AIM: singingxduck > Programming Python for the fun of it. > > _______________________________________________ > 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. -- '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. From billburns at pennswoods.net Sun Jan 16 22:05:21 2005 From: billburns at pennswoods.net (Bill Burns) Date: Sun Jan 16 21:55:00 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <031801c4fbf9$31c24420$40b68651@xp> References: <200501161049.30727.billburns@pennswoods.net> <031801c4fbf9$31c24420$40b68651@xp> Message-ID: <200501161605.21589.billburns@pennswoods.net> On Sunday 16 January 2005 1:28 pm, Alan Gauld wrote: > Some quick feedback based on a quick scim through. > > It looks like your pipe logic is all mixed up with the GUI vpde. > > It would be a god excercise to extract all the p[ipe code into > a separate class and the GUI methods call those class methods > to get the work done. THis would allow for a fairly easy port > to a different GUI or even the creation of a command line > version, so you could do: > > bash$ pipestats.py --content-weight 15 12 > bash$ pipestats.py --summary 22 1/2 > > If you then put that pipe object in a separate module you > could extend it for future projects... > > Separating all GUI code from the logical functions is > always a good idea. Simply get the GUI to reference an > instance of the pipe(having selected the sizes and passed > them in as constructor values maybe?) > > > def galCalc(self): > > """Displays a running tally of the total gallons for all > > pipe entered into each lineEdit. Recalculates whenever > > the data changes in any of the lineEdits. > > """ > > tmp = [] > > for ins, name, typ, size, weight, ID in self.lineEdits: > > if name != "lineEditTotal": > > length = ins.displayText() > > if length: > > length = int(str(length),10) > > gal = self.volCalc(ID, length) > > tmp.append(gal) > > total = sum(tmp) > > total = "%0.2f" % total > > self.lineEditTotal.setText(total) > > This is just an example, see how both the calculation and the > display of the result(*) are mixed up in the same method? > It would be good to split up the code to have a method > that does the calculation and returns the result and > another that calls the first and puts the result into > the GUI. This may require that you pass more parameters > into the calculation metjods but the gain in separation > is worth it IMHO. > > (*)Not knowing Qt I'm assuming that the setText() is a GUI method! > If it isn't I may be drawing invalid conclusions. Alan, You're exactly right setText() is a GUI method. I do have the pipe logic all mixed up with the GUI code. Now that you point it out, most of my methods don't have a return statement. They are completely dependant on the GUI and wouldn't function without it. Virtually all the methods act directly upon the GUI. If I'm understanding you correctly, I should have methods more along the lines of this one (which is in my code now): def volCalc(self, ID, length): """Calculates the volume/gallons of water inside of the various pipe. """ from math import pi gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) return gal It does it's 'job', return a result and is not effected by the GUI at all. I guess you could actually import this function into the interpreter and use it without problems. But if you tried doing that with the other function above (galCalc()) it wouldn't work out quite so well.... I'll have to see what I can do to take a step in this direction. It might take my a little while :-) Thank you for your help! Bill From cyresse at gmail.com Sun Jan 16 22:13:27 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 16 22:13:31 2005 Subject: [Tutor] Objects, persistence & getting Message-ID: Hi all, Well, one thing learning Java is good for is for thoroughly demystifying OOP. It's not some magical acronym of programming goodness, it's just an 'organic' way to organise code. That, and it looks good on your CV, even if you won't be using it. Like XML. It's got me thinking about object persistence, though, and plotting as to how I can use this for my own nefarious ends. If I understand correctly, once an object is created, as long as references to it exist, it isn't garbage collected. So, if module a.py creates an instance of class foo, can method bar in module b.py access foo without foo being passed directly to bar? Does that make sense? So, foo is floating around in the namespace, and bar just wants to grab a field of foo. Can it? I had a poke around the namespace yesterday, and got lost in hordes of methods that look like __this__, which is ugly. 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. From kent37 at tds.net Sun Jan 16 22:14:23 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 16 22:14:28 2005 Subject: [Tutor] Lights game In-Reply-To: <41EAA457.3010703@adinet.com.uy> References: <41EA1806.2040104@adinet.com.uy> <41EA63BC.7030701@tds.net> <41EAA457.3010703@adinet.com.uy> Message-ID: <41EAD92F.5030509@tds.net> Ismael Garrido wrote: > Kent Johnson wrote: >> If you make _cambiarCelda() check for out-of-range indices then you >> could get rid of _descartarNegativos() and clean up click() considerably. >> >> def _cambiarCelda(self, i, j): >> if not (0<=i<3 and 0<=j<3): >> return > > > Without the 'not' (it was pretty funny to see it do only the wrong > moves!) :-D I think the 'not' is correct as I wrote it but a simpler way would be def _cambiarCelda(self, i, j): if 0<=i<3 and 0<=j<3: self.tablero[i][j] = not self.tablero[i][j] Kent From kent37 at tds.net Sun Jan 16 23:40:34 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 16 23:40:39 2005 Subject: [Tutor] Objects, persistence & getting In-Reply-To: References: Message-ID: <41EAED62.70106@tds.net> I'm not sure I understand your question. but if I do, the answer is, sometimes you can do it, but it is rarely a good idea. Liam Clarke wrote: > If I understand correctly, once an object is created, as long as > references to it exist, it isn't garbage collected. That's right. > > So, if module a.py creates an instance of class foo, can method bar in > module b.py access foo without foo being passed directly to bar? I think you mean, can bar access the instance of foo created in a.py? Probably you can access it. Probably it's not a good idea. Somehow you need to access the reference to a foo that is being kept in a.py. If a.py looks like this: # a.py class foo: pass a_foo = foo() then a_foo is a module-level variable and it is accessible from other modules as a.a_foo: # b.py import a def bar(): print a.a_foo On the other hand, the foo instance might have been created as a local variable in some function that (calls a function that) calls bar(). To access this instance of foo you need to dig around in the stack frames and you would need some information about where the foo was created. This is in general a bad idea... For some examples of how to play with stack frames, search for sys._getframe() in the Python cookbook or in comp.lang.python. The contents of a stack frame are documented in the Language Reference section 3.2. > > Does that make sense? So, foo is floating around in the namespace, and > bar just wants to grab a field of foo. Can it? I had a poke around the > namespace yesterday, and got lost in hordes of methods that look like > __this__, which is ugly. Although this is probably possible, there aren't very many good reasons for doing it. It's better to just pass a reference to the object to the functions that need it. Kent > > Regards, > > Liam Clarke From maxnoel_fr at yahoo.fr Sun Jan 16 23:42:48 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 16 23:42:54 2005 Subject: [Tutor] Objects, persistence & getting In-Reply-To: References: Message-ID: On Jan 16, 2005, at 21:13, Liam Clarke wrote: > If I understand correctly, once an object is created, as long as > references to it exist, it isn't garbage collected. Correct, more or less (in the exception case where a references b, b references a but nothing else references either, both are GC'd if the implementation is sound). > So, if module a.py creates an instance of class foo, can method bar in > module b.py access foo without foo being passed directly to bar? Well, if you don't pass at least a reference to what you want the method/function to act on (assuming bar is not a method of Foo, of course -- or else, you should know that splitting the implementation of a class across multiple files, let alone modules, is a Bad Thing(TM)), how do you expect it to know? > Does that make sense? So, foo is floating around in the namespace, and > bar just wants to grab a field of foo. Can it? I had a poke around the > namespace yesterday, and got lost in hordes of methods that look like > __this__, which is ugly. Watch out, you seem to be confusing classes and objects, there. What are you trying to achieve there, exactly? Could you give us an example? -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From alan.gauld at freenet.co.uk Mon Jan 17 00:13:28 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 17 00:13:07 2005 Subject: [Tutor] Setting the focus to a window References: <41EAC5AB.5000901@gmail.com> Message-ID: <033d01c4fc20$fe5c9590$40b68651@xp> > me in the past 2 days or so), I need to change the focus from the > current window to another window (from Windows Media Player to Audacity > so that the simulated keypress will have an effect). There are a couple of options and its a wee while since I did any deep windows hacking. But I think the functions you need are called something like GetActiveWindow(title), and SetActiveWindow(handle). Theres also SetForegroundWindow... One of the wacky things about Windows is that there are usually about 3 ways to do anything and only one of them actually works the way you expect so you have to experiment a lot! ISTR You can also GetDesktopWindow() and front that get all the child windows which includes most of the running apps. ie You can get the window handle(aka identifier) using the title in the title bar, and then you make it the active window using the handle. However I believe the Windows PostMessage will actually work from the handle directly. But you will need to check the docs, thisis all from very olfd memories! The XP API may even be very different to the Win95 that I used to work on... Alan G. From keridee at jayco.net Mon Jan 17 00:34:17 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 17 00:34:35 2005 Subject: [Tutor] Lights game References: <41EA1806.2040104@adinet.com.uy> Message-ID: <002d01c4fc23$f46bd8e0$025328cf@JSLAPTOP> Kent's suggestions are great, but I wanted to add two. > Hello list. > > I'd really appreciate any comments, particulary regarding style > corrections. I'm a newbie... > > Thanks! > Ismael > > > import random > import tkMessageBox > from Tkinter import * > > class GUI: > def __init__(self): > self._crearGUI() > > def _crearGUI(self): > self.root = Tk() > self.root.title("Lights") > self.botones = [[0,0,0],[0,0,0],[0,0,0]] > for i in range(3): > for j in range(3): > action = lambda n=i, m=j: self._accion(n,m) > self.botones[i][j] = Button(self.root, height=5, > width=10, command=action) > self.botones[i][j].grid(row=i, column=j, padx= 4, pady=4) > > self._updateBotones() > self.root.mainloop() > > def _accion(self, i, j): > self._click(i, j) > try: > self._checkWin() > except ValueError: > tkMessageBox.showinfo(title = "You won!", message = "You > rock, kid!") > > if True == tkMessageBox.askyesno(title="What shall we do?", > message="Play another?"): > self.root.destroy() > lights.__init__() ## Is this... too ugly? Is there a > beter way? > self.__init__() > else: > self.root.destroy() > def _updateBotones(self): > for i in range(3): > for j in range(3): > if lights.tablero[i][j] == True: > self.botones[i][j].configure(bg="red") > else: > self.botones[i][j].configure(bg="grey") > > def _click(self, i, j): > lights.click(i,j) > self._updateBotones() > > def _checkWin(self): > conteo = 0 > for i in range(3): > for j in range(3): > if lights.tablero[i][j] == True: conteo +=1 if lights.tablero[i][j] == True: can be simplified to if lights.tablero[i][j]: > if conteo == 9: > print "GANO" > raise ValueError > > return > > class Luces: > def __init__(self): > self.tablero = [[0,0,0],[0,0,0],[0,0,0]] > self._hacerTablero() > > def _hacerTablero(self): > for i in range(3): > for j in range(3): > self.tablero[i][j] = not random.randint(0,1) Why use not random.randint(0,1)? If it's random, wouldn't just random.randint(0,1) give the same result? Why get a random number and then immediately return the opposite? It's like resolving to write down heads if you flip tails, and tails if you flip heads when you flip a coin! Why bother? > def _cambiarCelda(self, i, j): > self.tablero[i][j] = not self.tablero[i][j] > > def _descartarNegativos(self, n): > if n < 0: > raise IndexError > return n > > def click(self, i,j): > self._cambiarCelda(i,j) > try: > self._cambiarCelda(self._descartarNegativos(i-1), j) > except IndexError: > pass > try: > self._cambiarCelda(self._descartarNegativos(i+1), j) > except IndexError: > pass > try: > self._cambiarCelda(i, self._descartarNegativos(j-1)) > except IndexError: > pass > try: > self._cambiarCelda(i, self._descartarNegativos(j+1)) > except IndexError: > pass > > if __name__ == '__main__': > lights = Luces() > lightsGUI = GUI() > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Mon Jan 17 01:09:38 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 17 01:09:31 2005 Subject: [Tutor] Posting a large amount of code? References: <200501161049.30727.billburns@pennswoods.net><031801c4fbf9$31c24420$40b68651@xp> <200501161605.21589.billburns@pennswoods.net> Message-ID: <005401c4fc28$d82c2680$025328cf@JSLAPTOP> > > It would be a god excercise to extract all the p[ipe code into > > a separate class and the GUI methods call those class methods > > to get the work done. THis would allow for a fairly easy port > > to a different GUI or even the creation of a command line > > version, so you could do: Actually according to the above paragraph, he suggests putting them all in a seperate class. So pseudo-code... class Pipe: All things related to pipe program here. class GUI: def __init__(self): self stuff defined etc. def makeGuiStuff(self): connetion = Pipe(initial variables) make gui stuff here put all button events such as calculate(command = connection.calculate) (I told you it was pseudo-code) HTH, Jacob > You're exactly right setText() is a GUI method. I do have the pipe logic all > mixed up with the GUI code. Now that you point it out, most of my methods > don't have a return statement. They are completely dependant on the GUI > and wouldn't function without it. Virtually all the methods act directly upon > the GUI. > > If I'm understanding you correctly, I should have methods more along the > lines of this one (which is in my code now): > > def volCalc(self, ID, length): > """Calculates the volume/gallons of water inside of > the various pipe. > """ > from math import pi > gal = ((ID*.5)**2)*pi*(12*length)/(230.9429931) > return gal > > It does it's 'job', return a result and is not effected by the GUI at all. I > guess you could actually import this function into the interpreter and use it > without problems. But if you tried doing that with the other function above > (galCalc()) it wouldn't work out quite so well.... > > I'll have to see what I can do to take a step in this direction. It might take > my a little while :-) > > Thank you for your help! > > Bill > > > > > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From pathall at gmail.com Mon Jan 17 03:05:36 2005 From: pathall at gmail.com (Patrick Hall) Date: Mon Jan 17 03:05:40 2005 Subject: [Tutor] How to create a key-value pairs with alternative elements in a list ... please help. In-Reply-To: <20050112223941.37634.qmail@web90104.mail.scd.yahoo.com> References: <20050112223941.37634.qmail@web90104.mail.scd.yahoo.com> Message-ID: <6465924d05011618057ffb0bfb@mail.gmail.com> Hi! >>> while True: > ... try: > ... d[slice0.next()] = slice1.next() > ... except: StopIteration > ... break > ... ### while True: try: d[s0.next()] = s1.next() except StopIteration: # wherein we point out a wayward colon break ### Took me a while to figure out why I kept getting error messages about "break": try: something() except YourExceptionHere: something_else() Misplaced colon. Why those sneaky little... Cheers, Pat From ismaelgf at adinet.com.uy Mon Jan 17 03:36:27 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Mon Jan 17 03:35:51 2005 Subject: [Tutor] Tkinter Radiobutton Message-ID: <41EB24AB.3060700@adinet.com.uy> Hello. I can't figure out what's wrong in here! For some reason I can't understand, the radiobuttons don't work. The code itself is correct, the problem arises when I tell them that their master is self.configurar. I don't know why, but they don't like it. Any help is welcome Thanks Ismael def _configurar(self): self.configurar = Tk() self.configurar.title("Configurar") self.opciones = [] textos = ["Tama?o vertical", "Tama?o horizontal", "Tama?o botones"] valores = [self.x, self.y, self.tama] for i in range(3): Label(self.configurar, text=textos[i]).grid(row =i, column = 0) entr= Entry(self.configurar) entr.grid(row = i, column = 1) entr.insert(0, valores[i]) self.opciones.append(entr) self.valor = StringVar() self.valor.set('pibe') #### Here self.H = Radiobutton(self.configurar, text="Hombre", variable=self.valor, value='pibe') self.H.grid(row=3, column=0) self.M = Radiobutton(self.configurar, text="Mujer", variable=self.valor, value='piba') self.M.grid(row=3, column=1) if self.mensaje == "pibe": self.H.select() else: self.M.select() Button(self.configurar, command=lambda: self._configurarOk(), text="Ok").grid(row=4, column=0 ) Button(self.configurar, command=lambda: self.configurar.destroy(), text="Cancelar").grid(row=4, column=1) def _configurarOk(self): for i in range(self.x): for j in range(self.y): self.botones[i][j].destroy() self.salir.destroy() self.rein.destroy() self.mensaje = self.valor.get() print self.mensaje self.x = int(self.opciones[0].get()) self.y = int(self.opciones[1].get()) self.tama = int(self.opciones[2].get()) self._crearBotones() self.lights.x = self.x self.lights.y = self.y self.lights._hacerTablero() self.lights._llenarTablero() self._updateBotones() self.configurar.destroy() From jfouhy at paradise.net.nz Mon Jan 17 03:56:26 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Mon Jan 17 03:56:30 2005 Subject: [Tutor] Tkinter Radiobutton In-Reply-To: <41EB24AB.3060700@adinet.com.uy> References: <41EB24AB.3060700@adinet.com.uy> Message-ID: <1105930586.41eb295a1cdca@www.paradise.net.nz> Quoting Ismael Garrido : > I can't figure out what's wrong in here! For some reason I can't > understand, the radiobuttons don't work. The code itself is correct, the What is the error message? > Button(self.configurar, command=lambda: self._configurarOk(), > text="Ok").grid(row=4, column=0 ) > Button(self.configurar, command=lambda: > self.configurar.destroy(), text="Cancelar").grid(row=4, column=1) An unrelated comment: This would be better as command=self._configurarOK and command=self.configurar.destroy . -- John. From marilyn at deliberate.com Mon Jan 17 04:59:14 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Mon Jan 17 05:03:10 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Sat, 15 Jan 2005, Danny Yoo wrote: > > > > I have only wrapped my lock around file-descriptor creations. Should I > > wrap it around closings too? Or the whole open -> close transaction? > > It sounds like error-prone work to do the latter. What am I missing? > > Hi Marilyn, > > Can you send a link to the source code to the Tutor list? I'm getting the > feeling that there's might be a design problem. Just adding locks > whenever something doesn't work is not a sustainable way to write a > multithreaded application. > > We have to see why your file descriptors being are being shared between > threads. Is there a reason why you need to share them as global > resources? No. And I don't. They are often attributes of instantiations of classes; or they come and go quickly. Thank you for offering to look at the code. Start at: http://www.maildance.com/python/doorman/README Then, the daemon that creates the threads is: http://www.maildance.com/python/doorman/py_daemon.py I'm testing using calls to: http://www.maildance.com/python/doorman/route_mail.py http://www.maildance.com/python/doorman/doorman.py Other modules that open and close file descriptors from there are: http://www.maildance.com/python/doorman/db.py http://www.maildance.com/python/doorman/doorman_log.py http://www.maildance.com/python/doorman/exim.py http://www.maildance.com/python/doorman/move.py I'll be grateful for any improvements you suggest. But, I do know that some modules aren't well-documented, or are hardly documented at all yet. And db.py seems like a mess to me. But I'm not ready to straighten it up yet. But the most important one, py_daemon.py, I hope is very readable. Still though, I should confess, I am feeling a bit dismal about the thread situation, especially since searching around for info on critical code. The examples I found, before I gave up, were all trivial. Also, there is this article: http://linuxgazette.net/107/pai.html Which says that the performance is almost the same with threads as with single-threading. Boo. So suddenly, I have no idea why I'm down this road. We have a huge performance savings from setting up a daemon that reads a socket. But, unless something changes our minds soon, I'll rip threading out of our code. Still though, I am a teacher too, and a student. So any thoughts you have about our code will be treated like the pearls they are. Thank you again. Marilyn > > -- From ismaelgf at adinet.com.uy Mon Jan 17 06:02:00 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Mon Jan 17 06:01:42 2005 Subject: [Tutor] Tkinter Radiobutton In-Reply-To: <41EB24AB.3060700@adinet.com.uy> References: <41EB24AB.3060700@adinet.com.uy> Message-ID: <41EB46C8.1010308@adinet.com.uy> Ismael Garrido wrote: > Hello. > > I can't figure out what's wrong in here! For some reason I can't > understand, the radiobuttons don't work. The code itself is correct, > the problem arises when I tell them that their master is > self.configurar. I don't know why, but they don't like it. After changing this: > def _configurar(self): > self.configurar = Tk() To this: self.configurar= Toplevel(self.root) Works.... I don't understand why it didn't work the other way around. Is it some kind of bug or strange 'feature'? Or was I making a silly mistake? Thanks Ismael From guillermo.fernandez.castellanos at gmail.com Mon Jan 17 08:07:02 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Mon Jan 17 08:07:05 2005 Subject: [Tutor] py2exe Message-ID: <7d7029e705011623076936f353@mail.gmail.com> Hi, I've done a GUI program and it works perfectly. but when I try to package it with py2exe, I obtain the following result: The following modules appear to be missing ['mx'] And when i run it, it crashes with this error message: Traceback (most recent call last): File "openwar.py", line 41, in ? File "Tix.pyc", line 210, in __init__ _tkinter.TclError: can't find package Tix I guess the two errors are not related (mx is an optional package to pysqlite, but you don't *need* it), but I'm surprised there's an error due to Tix, a standard package in the python distribution... specially that my Python can work with Tix without problems! Does anyone have a guess about what's happening? Thanks a lot, G From flaxeater at yahoo.com Mon Jan 17 08:12:26 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 17 08:12:29 2005 Subject: [Tutor] py2exe Message-ID: <20050117071226.72097.qmail@web54306.mail.yahoo.com> Try something like. import mx import Tix Sometimes py2exe will not gather up all the modules that it depends on. It does an admirable job but still needs some help. Perhaps you are in advertently using mx? Well try that and see what happens. Guillermo Fernandez Castellanos wrote: >Hi, > > > >I've done a GUI program and it works perfectly. > > > >but when I try to package it with py2exe, I obtain the following result: > >The following modules appear to be missing > >['mx'] > > > >And when i run it, it crashes with this error message: > >Traceback (most recent call last): > > File "openwar.py", line 41, in ? > > File "Tix.pyc", line 210, in __init__ > >_tkinter.TclError: can't find package Tix > > > >I guess the two errors are not related (mx is an optional package to > >pysqlite, but you don't *need* it), but I'm surprised there's an error > >due to Tix, a standard package in the python distribution... specially > >that my Python can work with Tix without problems! > > > >Does anyone have a guess about what's happening? > > > >Thanks a lot, > > > >G > >_______________________________________________ > >Tutor maillist - Tutor@python.org > >http://mail.python.org/mailman/listinfo/tutor > > > > > __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 From guillermo.fernandez.castellanos at gmail.com Mon Jan 17 08:32:29 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Mon Jan 17 08:32:32 2005 Subject: [Tutor] py2exe In-Reply-To: <20050117071226.72097.qmail@web54306.mail.yahoo.com> References: <20050117071226.72097.qmail@web54306.mail.yahoo.com> Message-ID: <7d7029e705011623327bd5b784@mail.gmail.com> Hi, Thanks for the quick answer. Unfortunatelly, it does not work... I still have the same problem. I though Tix was shipped with the Python distribution. And i have the Tix.pyc file in my computer, so I don't really undesrtand... My setup.py: from distutils.core import setup import py2exe import Tix # To run: # python setup.py py2exe setup(windows=['openwar.py'], name='openwar', version='0.1', description="Openwar unit management program", author="Guille", ) Thanks, Guille On Sun, 16 Jan 2005 23:12:26 -0800 (PST), Chad Crabtree wrote: > Try something like. > import mx > import Tix > > Sometimes py2exe will not gather up all the modules that it depends > on. > It does an admirable job but still needs some help. Perhaps you are > in > advertently using mx? Well try that and see what happens. > > Guillermo Fernandez Castellanos wrote: > > >Hi, > > > > > > > >I've done a GUI program and it works perfectly. > > > > > > > >but when I try to package it with py2exe, I obtain the following > result: > > > >The following modules appear to be missing > > > >['mx'] > > > > > > > >And when i run it, it crashes with this error message: > > > >Traceback (most recent call last): > > > > File "openwar.py", line 41, in ? > > > > File "Tix.pyc", line 210, in __init__ > > > >_tkinter.TclError: can't find package Tix > > > > > > > >I guess the two errors are not related (mx is an optional package to > > > >pysqlite, but you don't *need* it), but I'm surprised there's an > error > > > >due to Tix, a standard package in the python distribution... > specially > > > >that my Python can work with Tix without problems! > > > > > > > >Does anyone have a guess about what's happening? > > > > > > > >Thanks a lot, > > > > > > > >G > > > >_______________________________________________ > > > >Tutor maillist - Tutor@python.org > > > >http://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - Find what you need with new enhanced search. > http://info.mail.yahoo.com/mail_250 > From ismaelgf at adinet.com.uy Mon Jan 17 08:48:02 2005 From: ismaelgf at adinet.com.uy (Ismael Garrido) Date: Mon Jan 17 08:47:26 2005 Subject: [Fwd: Re: [Tutor] py2exe] Message-ID: <41EB6DB2.8040202@adinet.com.uy> Forgot to forward it to the list... Here it goes: -------- Original Message -------- Subject: Re: [Tutor] py2exe Date: Mon, 17 Jan 2005 05:40:44 -0200 From: Ismael Garrido To: Guillermo Fernandez Castellanos References: <20050117071226.72097.qmail@web54306.mail.yahoo.com> <7d7029e705011623327bd5b784@mail.gmail.com> Guillermo Fernandez Castellanos wrote: >Unfortunatelly, it does not work... I still have the same problem. > >My setup.py: > >from distutils.core import setup >import py2exe > >import Tix > > No, put "import Tix" in openwar.py. The modules you import on setup.py don't get "carried over" to the rest of your code. Bye Ismael From alan.gauld at freenet.co.uk Mon Jan 17 08:48:28 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 17 08:48:01 2005 Subject: [Tutor] Objects, persistence & getting References: Message-ID: <036c01c4fc68$ef39afb0$40b68651@xp> > Well, one thing learning Java is good for is for thoroughly > demystifying OOP. I'd have to disagree here because Java's version of OOP has very little to do with real OOP. Java just uss classes as a kind of modularisation mechanism and does not make much use of tthe real OO features. In fact it doesn't even support several of the things that enable real OO programming. And its class library, a strong feature because it is a standard, is dreadful from an OOP p[erspective. In fact I usually refer to Java as a Class Oriented Programming rather than Object Oriented. It is possible to use Java in an OOP way (read Brice Eckel's "Thinking in Java" to see how) but the language itself encourages a style of programming that is much more like Pythons modules than true OOP. > It's not some magical acronym of programming > goodness, it's just an 'organic' way to organise code. Certainly in Java thats true, and indeed even at the higher level OOP is a way of organizing code - by finding high level abstractions and building tree structures based on common intefaces. But Java doesn't encourage that structuring as much as Python does! > If I understand correctly, once an object is created, as long as > references to it exist, it isn't garbage collected. Yes. > Does that make sense? So, foo is floating around in the namespace, and > bar just wants to grab a field of foo. Can it? It shouldn't - it should be sending a message. One of the bad things about Java is it encourages the use of getXXX and setXXX style methods which are a total anathema to real OOP. They break the Law of Demeter. Alan G. From alan.gauld at freenet.co.uk Mon Jan 17 08:52:33 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 17 08:52:15 2005 Subject: [Tutor] Posting a large amount of code? References: <200501161049.30727.billburns@pennswoods.net><031801c4fbf9$31c24420$40b68651@xp> <200501161605.21589.billburns@pennswoods.net> <005401c4fc28$d82c2680$025328cf@JSLAPTOP> Message-ID: <037d01c4fc69$80e36b90$40b68651@xp> > Actually according to the above paragraph, he suggests putting them all in > a seperate class. So pseudo-code... > > class Pipe: > All things related to pipe program here. > > class GUI: Exactly so. The rationale for that was that you could work with a collection of pipes each with their own dimensions etc. I forgot to explain that part.... Alan G. From kent37 at tds.net Mon Jan 17 12:01:18 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 17 12:01:24 2005 Subject: [Tutor] Tkinter Radiobutton In-Reply-To: <41EB46C8.1010308@adinet.com.uy> References: <41EB24AB.3060700@adinet.com.uy> <41EB46C8.1010308@adinet.com.uy> Message-ID: <41EB9AFE.50908@tds.net> Is this code running in a larger program that has a window? You are only supposed to have one root window (Tk()) in a program, maybe that is the problem. Kent Ismael Garrido wrote: > Ismael Garrido wrote: > >> Hello. >> >> I can't figure out what's wrong in here! For some reason I can't >> understand, the radiobuttons don't work. The code itself is correct, >> the problem arises when I tell them that their master is >> self.configurar. I don't know why, but they don't like it. > > > > After changing this: > >> def _configurar(self): >> self.configurar = Tk() > > > To this: self.configurar= Toplevel(self.root) > Works.... > > I don't understand why it didn't work the other way around. Is it some > kind of bug or strange 'feature'? Or was I making a silly mistake? > > Thanks > Ismael > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From flaxeater at yahoo.com Mon Jan 17 17:52:24 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 17 17:52:26 2005 Subject: [Tutor] py2exe Message-ID: <20050117165224.37967.qmail@web54306.mail.yahoo.com> Right what he said. I'm sorry I didn't make that clear. This way py2exe can look and see import Tix and see it's needed. Py2exe does not ship the whole python distribution, rather only what is needed. Indeed it tends to ship many things that are not needed but it's better than the whole distribution, especially if you are using several libraries and such. __________________________________ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com From jcruzan at gmail.com Mon Jan 17 19:36:42 2005 From: jcruzan at gmail.com (Jack Cruzan) Date: Mon Jan 17 19:32:47 2005 Subject: [Tutor] Objects & Classes... Message-ID: <1105987002.6784.14.camel@localhost.localdomain> Hello! I am writing (or at least attempting) to write a Character generation utility for Shadowrun in Python of course! After reading about other attempts to make and RPG dealing with with character generation it looks like OOP is the best way to go. I have no experiance with OOP and its kinda throwing me for a loop. Here is what I mean... I make a class class Character: def __init__(self, name = ' ', race = 'Human', magic = 'None'): self.name=name self.race=race self.magic=magic Great, now I need some from the user to create the character. This is what I have. def createChar(book): name = raw_input("Enter your character's name. ") race = int(raw_input("What race? (1: Human, 2: Elf, 3: Ork, 4: Troll, 5: Dwarf,)")) book[name] = race What am I doing wrong? Oh and one other thing I can't load my data. I can create a new file, but not load it. Here is the code for that... def loadChar(book): import os filename = 'SRchargen.dat' if os.path.exists(filename): store = open(filename,'r') while store: name = store.readline().strip() race = store.readline().strip() book[name] = race else: store = open(filename, 'w') store.close Any help would be greatly appreciated! From elh at outreachnetworks.com Mon Jan 17 19:48:34 2005 From: elh at outreachnetworks.com (Eric L. Howard) Date: Mon Jan 17 19:50:15 2005 Subject: [Tutor] need advice on streamlining code... Message-ID: <20050117184833.GA2763@outreachnetworks.com> The following block of code works, and provides the necessary output I'm looking for...but I have a feeling that it's working through sheer brute force and could be better: insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") insideipgrep = insideipgrepfd.readlines() insideipfield, insideip = string.split(string.strip(insideipgrep[0]), "=") insideipsplit = string.split(insideip, " ") insideipquads = string.split(insideipsplit[1], ".") insidemaskquads = string.split(insideipsplit[4], ".") the line in /etc/rc.conf looks like: ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" Any and all thoughts/pointers are appreciated. ~elh -- Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m ------------------------------------------------------------------------ www.OutreachNetworks.com 313.297.9900 ------------------------------------------------------------------------ JabberID: elh@jabber.org Advocate of the Theocratic Rule From flaxeater at yahoo.com Mon Jan 17 19:59:15 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 17 19:59:18 2005 Subject: [Tutor] Objects & Classes... Message-ID: <20050117185915.17158.qmail@web54308.mail.yahoo.com> Jack Cruzan wrote: >class Character: > > def __init__(self, name = ' ', race = 'Human', magic = 'None'): > > self.name=name > > self.race=race > > self.magic=magic > > I know your going to need some other stuff. class NewCharacter(Character): def __init__(self,stats,*args,**kwds): super(Character,self).__init__(*args,**kwds) self.stats=stats super is a function that calls a specific function from a parent class. This way you can still use the previous __init__ code and then extend it. *args represents a tuple of arguments of unspecified length or type, **kwds is a dictionary of named arguments, like name='' like above. That way you capture the keywords needed to pass to the parent class so that name race magic is still updated at class instantiation. This way you can extend classes. So you *could* subclass Character as a Dwarf, a Troll etc so that each class already knows about being a Dwarf, eg special abilities skill bonuses and such. If you don't understand this, that's ok it took me quite a while. However once I got this it made certain tasks much easier. I could figure out how to do something, then never need to think about how it works later in the project. >def createChar(book): > > name = raw_input("Enter your character's name. ") > > race = int(raw_input("What race? (1: Human, 2: Elf, 3: Ork, 4: Troll, > >5: Dwarf,)")) > > book[name] = race > > > try this (untested) races={1:'Human',2:'Elf',3:'Ork',4:'Troll',5:'Dwarf'} #this is a dictionary book[name]=races[race] print "The Name is " + name print "The Race is " + book[name] >def loadChar(book): > > import os > > filename = 'SRchargen.dat' > > if os.path.exists(filename): > > store = open(filename,'r') > > while store: > > name = store.readline().strip() > > race = store.readline().strip() > > book[name] = race > > else: > > store = open(filename, 'w') > > store.close > > I'm not sure why this doesn't work, perhaps you should post what is in 'SRchargen.dat'. You do know that this format will only work with one character? Do you get an error? If so post that traceback message. Anyway good luck. __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 From flaxeater at yahoo.com Mon Jan 17 20:05:59 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 17 20:06:03 2005 Subject: [Tutor] need advice on streamlining code... Message-ID: <20050117190559.34567.qmail@web54305.mail.yahoo.com> I can't really think of a more elegant solution than what you have, maybe regex's but I hate those. You *can* reduce the number of lines by two, and there was a variable you never used. HTH Eric L. Howard wrote: >The following block of code works, and provides the necessary output I'm >looking for...but I have a feeling that it's working through sheer brute >force and could be better: > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > insideipgrep = insideipgrepfd.readlines() > > insideipgrep=os.popen("grep ifconfig_fxp0 /etc/rc.conf").readlines() > insideipfield, insideip = string.split(string.strip(insideipgrep[0]), "=") > > insideip = string.split(string.strip(insideipgrep[0]), "=")[1] > insideipsplit = string.split(insideip, " ") > insideipquads = string.split(insideipsplit[1], ".") > insidemaskquads = string.split(insideipsplit[4], ".") > > insideipquads=string.split(string.split(insideip, " ")[1],".") insidemaskquads = string.split(string.split(insideip, " ")[4], ".") >the line in /etc/rc.conf looks like: > >ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" > >Any and all thoughts/pointers are appreciated. > > ~elh > > > __________________________________ Do you Yahoo!? Meet the all-new My Yahoo! - Try it today! http://my.yahoo.com From artooro at gmail.com Mon Jan 17 20:58:14 2005 From: artooro at gmail.com (Arthur Wiebe) Date: Mon Jan 17 20:58:18 2005 Subject: [Tutor] Walking a directory Message-ID: What I want to do is rather simple, but I cannot find any good documentation for something like this. The directory structure is sort of like this: >Aircraft >>A-10 +A-10cl-set.xml +A-10fg-set.xml For example Aircraft/A-10/A-10cl-set.xml Now I want to loop though each aircrafts folder (such as A-10) and find each *-set.xml file, get the aircraft part (such as A-10cl) and add it to a list. While this does seem simple, how is the question. I know about the walk function, but cannot find any good examples for it. -- From cyresse at gmail.com Mon Jan 17 21:13:23 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 17 21:13:26 2005 Subject: [Tutor] Objects, persistence & getting In-Reply-To: <036c01c4fc68$ef39afb0$40b68651@xp> References: <036c01c4fc68$ef39afb0$40b68651@xp> Message-ID: Law of Demeter? And, OK.... I'll just pass references, it was a passing idle thought. But thanks : ) What kind of languages espouse real OOP? Smalltalk gets mentioned a lot. Ruby? Regards, Liam Clarke On Mon, 17 Jan 2005 07:48:28 -0000, Alan Gauld wrote: > > Well, one thing learning Java is good for is for thoroughly > > demystifying OOP. > > > I'd have to disagree here because Java's version of OOP has > very little to do with real OOP. Java just uss classes as > a kind of modularisation mechanism and does not make much > use of tthe real OO features. In fact it doesn't even > support several of the things that enable real OO > programming. > > And its class library, a strong feature because it is a > standard, is dreadful from an OOP p[erspective. In fact > I usually refer to Java as a Class Oriented Programming > rather than Object Oriented. > > It is possible to use Java in an OOP way (read Brice Eckel's > "Thinking in Java" to see how) but the language itself > encourages a style of programming that is much more like > Pythons modules than true OOP. > > > It's not some magical acronym of programming > > goodness, it's just an 'organic' way to organise code. > > Certainly in Java thats true, and indeed even at the > higher level OOP is a way of organizing code - by > finding high level abstractions and building tree > structures based on common intefaces. But Java doesn't > encourage that structuring as much as Python does! > > > > If I understand correctly, once an object is created, as long as > > references to it exist, it isn't garbage collected. > > Yes. > > > Does that make sense? So, foo is floating around in the namespace, > and > > bar just wants to grab a field of foo. Can it? > > It shouldn't - it should be sending a message. One of > the bad things about Java is it encourages the use > of getXXX and setXXX style methods which are a total > anathema to real OOP. They break the Law of Demeter. > > Alan G. > > -- '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. From kent37 at tds.net Mon Jan 17 21:21:33 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 17 21:21:40 2005 Subject: [Tutor] Walking a directory In-Reply-To: References: Message-ID: <41EC1E4D.40305@tds.net> Something like this (not tested!): import os for dirpath, dirnames, filenames in os.walk('Aircraft'): for filename in filenames: if filename.endswith('-set.xml'): print filename[:-8] This is probably a good time to mention Jason Orendorff's wonderful path module, which makes this even simpler: import path for filepath in path.path('Aircraft').walkfiles(pattern='*-set.xml'): print filepath.name[:-8] http://www.jorendorff.com/articles/python/path/ (though the server seems to be down at the moment...) Kent Arthur Wiebe wrote: > What I want to do is rather simple, but I cannot find any good > documentation for something like this. > > The directory structure is sort of like this: > >>Aircraft >> >>>A-10 > > +A-10cl-set.xml > +A-10fg-set.xml > > For example Aircraft/A-10/A-10cl-set.xml > > Now I want to loop though each aircrafts folder (such as A-10) and > find each *-set.xml file, get the aircraft part (such as A-10cl) and > add it to a list. > > While this does seem simple, how is the question. > > I know about the walk function, but cannot find any good examples for it. From jcruzan at gmail.com Mon Jan 17 21:51:04 2005 From: jcruzan at gmail.com (Jack Cruzan) Date: Mon Jan 17 21:47:04 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <20050117185915.17158.qmail@web54308.mail.yahoo.com> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> Message-ID: <1105995064.12772.6.camel@localhost.localdomain> Ok, so each character has his name, race, his stats, his skills, and his gear. since the name and character is unique there is no need for a class these things. hmmm maybe I am conceptualizing this wrong. would each new character then be a dictonary? Made up of different elements or would the character be a list? Since each character is basically just a list of stats... the stats get modified up and down by race and certain gear... am I conceptulizing this correctly? On Mon, 2005-01-17 at 10:59 -0800, Chad Crabtree wrote: > Jack Cruzan wrote: > > >class Character: > > > > def __init__(self, name = ' ', race = 'Human', magic = 'None'): > > > > self.name=name > > > > self.race=race > > > > self.magic=magic > > > > > I know your going to need some other stuff. > > class NewCharacter(Character): > def __init__(self,stats,*args,**kwds): > super(Character,self).__init__(*args,**kwds) > self.stats=stats > > super is a function that calls a specific function from a parent > class. > This way you can still use the previous __init__ code and then extend > > it. *args represents a tuple of arguments of unspecified length or > type, **kwds is a dictionary of named arguments, like name='' like > above. That way you capture the keywords needed to pass to the > parent > class so that name race magic is still updated at class > instantiation. > > This way you can extend classes. So you *could* subclass Character > as a > Dwarf, a Troll etc so that each class already knows about being a > Dwarf, > eg special abilities skill bonuses and such. If you don't understand > > this, that's ok it took me quite a while. However once I got this it > > made certain tasks much easier. I could figure out how to do > something, > then never need to think about how it works later in the project. > > >def createChar(book): > > > > name = raw_input("Enter your character's name. ") > > > > race = int(raw_input("What race? (1: Human, 2: Elf, 3: Ork, 4: > Troll, > > > >5: Dwarf,)")) > > > > book[name] = race > > > > > > > try this (untested) > > races={1:'Human',2:'Elf',3:'Ork',4:'Troll',5:'Dwarf'} #this is a > dictionary > book[name]=races[race] > print "The Name is " + name > print "The Race is " + book[name] > > >def loadChar(book): > > > > import os > > > > filename = 'SRchargen.dat' > > > > if os.path.exists(filename): > > > > store = open(filename,'r') > > > > while store: > > > > name = store.readline().strip() > > > > race = store.readline().strip() > > > > book[name] = race > > > > else: > > > > store = open(filename, 'w') > > > > store.close > > > > > I'm not sure why this doesn't work, perhaps you should post what is > in > 'SRchargen.dat'. You do know that this format will only work with > one > character? Do you get an error? If so post that traceback message. > > Anyway good luck. > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - now with 250MB free storage. Learn more. > http://info.mail.yahoo.com/mail_250 From jfouhy at paradise.net.nz Mon Jan 17 22:02:49 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Mon Jan 17 22:02:53 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <1105995064.12772.6.camel@localhost.localdomain> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> Message-ID: <1105995769.41ec27f97a2a5@www.paradise.net.nz> On Mon, 2005-01-17 at 10:59 -0800, Chad Crabtree wrote: > class NewCharacter(Character): > def __init__(self,stats,*args,**kwds): > super(Character,self).__init__(*args,**kwds) > self.stats=stats > > super is a function that calls a specific function from a parent > class. This way you can still use the previous __init__ code and then extend > it. Is that the right idiom these days? I would write: def __init__(self, stats, *args, **kw): Character.__init__(self, *args, **kw) self.stats = stats -- John. From jfouhy at paradise.net.nz Mon Jan 17 22:07:45 2005 From: jfouhy at paradise.net.nz (jfouhy@paradise.net.nz) Date: Mon Jan 17 22:07:49 2005 Subject: [Tutor] py2exe In-Reply-To: <7d7029e705011623076936f353@mail.gmail.com> References: <7d7029e705011623076936f353@mail.gmail.com> Message-ID: <1105996065.41ec2921ca2dd@www.paradise.net.nz> Quoting Guillermo Fernandez Castellanos : > but when I try to package it with py2exe, I obtain the following > result: The following modules appear to be missing > ['mx'] I've got a program that uses mx.texttools.. Whenever I freeze it (with py2exe), I get a list of about three modules that it can't find. Nevertheless, the frozen executable works fine :-) > And when i run it, it crashes with this error message: > Traceback (most recent call last): > File "openwar.py", line 41, in ? > File "Tix.pyc", line 210, in __init__ > _tkinter.TclError: can't find package Tix When I tried, I couldn't figure out how to freeze Tix. I ended up giving up and recoding all my Tix stuff to use Pmw instead... (although I was a lot more new to python in those days) (and the fact that Pmw has actual documentation is an added bonus over Tix :-) ) -- John. From cyresse at gmail.com Mon Jan 17 22:09:02 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 17 22:09:06 2005 Subject: [Tutor] need advice on streamlining code... In-Reply-To: <20050117190559.34567.qmail@web54305.mail.yahoo.com> References: <20050117190559.34567.qmail@web54305.mail.yahoo.com> Message-ID: Well, if you're looking to extract the IP & mask you could use a regEx. They're not tooooo bad.... If it's only that line that you're extracting, and it's format doesn't change import re pattern='ifconfig_fxp0="inet (?P*.?) netmask (?P*.?)" reObj=re.compile(pattern, IGNORECASE) jay = os.popen("grep ifconfig_fxp0 /etc/rc.conf").readlines() matches=reObj.search(jay[0]) ip = matches.group('ip') netmask =matches.group('mask') #You can then do your string splits, whatever now. HTH Liam Clarke regExs, can be your friends. If you KISS. On Mon, 17 Jan 2005 11:05:59 -0800 (PST), Chad Crabtree wrote: > I can't really think of a more elegant solution than what you have, > maybe regex's but I hate those. You *can* reduce the number of lines > by > two, and there was a variable you never used. > HTH > Eric L. Howard wrote: > > >The following block of code works, and provides the necessary output > I'm > >looking for...but I have a feeling that it's working through sheer > brute > >force and could be better: > > > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > > insideipgrep = insideipgrepfd.readlines() > > > > > insideipgrep=os.popen("grep ifconfig_fxp0 /etc/rc.conf").readlines() > > > insideipfield, insideip = > string.split(string.strip(insideipgrep[0]), "=") > > > > > insideip = string.split(string.strip(insideipgrep[0]), "=")[1] > > > insideipsplit = string.split(insideip, " ") > > insideipquads = string.split(insideipsplit[1], ".") > > insidemaskquads = string.split(insideipsplit[4], ".") > > > > > insideipquads=string.split(string.split(insideip, " ")[1],".") > insidemaskquads = string.split(string.split(insideip, " ")[4], ".") > > >the line in /etc/rc.conf looks like: > > > >ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" > > > >Any and all thoughts/pointers are appreciated. > > > > ~elh > > > > > > > > > __________________________________ > Do you Yahoo!? > Meet the all-new My Yahoo! - Try it today! > http://my.yahoo.com > > > _______________________________________________ > 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. From maxnoel_fr at yahoo.fr Mon Jan 17 22:39:42 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 17 22:39:46 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <1105995064.12772.6.camel@localhost.localdomain> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> Message-ID: <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 17, 2005, at 20:51, Jack Cruzan wrote: > Ok, so each character has his name, race, his stats, his skills, and > his > gear. > > since the name and character is unique there is no need for a class > these things. > > hmmm maybe I am conceptualizing this wrong. > > would each new character then be a dictonary? Made up of different > elements or would the character be a list? Since each character is > basically just a list of stats... the stats get modified up and down by > race and certain gear... am I conceptulizing this correctly? I've thought about it a few times. Actually, when I learn a language, I often try to design and implement a SR character generator. Then I give up because it's really complicated and I don't have time (other things to do using the aforementioned programming languages, mostly :D ). I'm probably gonna end up making a web-based one at some point, using either mod_python or Ruby on Rails. Gah, so many things to do, and so little time... Anyway, my view of the problem was that at least the following should be classes: - Character - Attribute - Skill (perhaps a subclass: Specialization?) - Augmentation (with subclasses like Cyberware, Bioware, AdeptPower) - GearItem Character would mostly be a container for the other classes, so most of its attributes would be dictionaries, like: class Character: def __init__(): self.attributes = {'Body': Attribute(), 'Quickness': Attribute(), 'Strength': Attribute(), 'Charisma': Attribute(), 'Intelligence': Attribute(), 'Willpower': Attribute()} Ah, things would be so much easier if McMackie would release the NSRCG source code (despite this abomination being written in Visual Basic), wouldn't they? ;) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From flaxeater at yahoo.com Mon Jan 17 23:02:05 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 17 23:02:07 2005 Subject: [Tutor] Objects & Classes... Message-ID: <20050117220205.26896.qmail@web54301.mail.yahoo.com> I was thinking (it's been a while since I played) that I would do it like this. (strawman code) class Character: def __init__(self,name,attribs,skills,cyberware): ...initialize.. and code to check attrib and skill mins and maxes physical and menatl attributes should be part of the character (why would an attri bute need to be a separate class?) def addCyberware(self...): def removeCyberware(...): etc et al. class Troll(Character): update min max code in class for each race class Skill: def __init__(self, stuff): perhaps have a subclass for each skill? in addition the class would be responsible for it's presentation class Cyberware: set attributes skill atribute mods and sanity or what ever thne subclass for each item. Also taking care of presentation class Spell: just like cyberware but perhaps one level further abstraction class Augment(Spell): class Damage(Spell): class Curse(Spell): I guess the question is should the character class be a *is a* or *has a* relationship with Race. Perhaps they could be mixins. For persistence the only reasonable solution would be an object persistence scheme of some sort. Then throw in a Top Down RPG Engine and life is good. Max Noel wrote: > > On Jan 17, 2005, at 20:51, Jack Cruzan wrote: > >> Ok, so each character has his name, race, his stats, his skills, and his >> gear. >> >> since the name and character is unique there is no need for a class >> these things. >> >> hmmm maybe I am conceptualizing this wrong. >> >> would each new character then be a dictonary? Made up of different >> elements or would the character be a list? Since each character is >> basically just a list of stats... the stats get modified up and down by >> race and certain gear... am I conceptulizing this correctly? > > > I've thought about it a few times. Actually, when I learn a > language, I often try to design and implement a SR character > generator. Then I give up because it's really complicated and I don't > have time (other things to do using the aforementioned programming > languages, mostly :D ). > I'm probably gonna end up making a web-based one at some point, > using either mod_python or Ruby on Rails. Gah, so many things to do, > and so little time... > > Anyway, my view of the problem was that at least the following > should be classes: > - Character > - Attribute > - Skill (perhaps a subclass: Specialization?) > - Augmentation (with subclasses like Cyberware, Bioware, AdeptPower) > - GearItem > > Character would mostly be a container for the other classes, so > most of its attributes would be dictionaries, like: > > class Character: > def __init__(): > self.attributes = {'Body': Attribute(), 'Quickness': > Attribute(), 'Strength': Attribute(), 'Charisma': Attribute(), > 'Intelligence': Attribute(), 'Willpower': Attribute()} > > > Ah, things would be so much easier if McMackie would release the > NSRCG source code (despite this abomination being written in Visual > Basic), wouldn't they? ;) > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge > a perfect, immortal machine?" > > > __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From singingxduck at gmail.com Mon Jan 17 23:34:35 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Mon Jan 17 23:34:43 2005 Subject: [Tutor] Getting access to the speakers Message-ID: <41EC3D7B.7030102@gmail.com> Hello all, I've googled the topic a bit, and can't seem to find any relevant information. Does anyone know how to gain access to the speakers and, for example, tell when they are playing a sound and when they are not? I would think it's something with sockets, but I don't know how to go about it. Any help would be appreciated. Thanks in advance, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From python at bernardlebel.com Mon Jan 17 23:41:57 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Mon Jan 17 23:42:05 2005 Subject: [Tutor] COP vs OOP (was: Objects, persistence & getting) In-Reply-To: <036c01c4fc68$ef39afb0$40b68651@xp> References: <036c01c4fc68$ef39afb0$40b68651@xp> Message-ID: <41EC3F35.3070007@bernardlebel.com> Alan Gauld wrote: In fact > I usually refer to Java as a Class Oriented Programming > rather than Object Oriented. If you allow me a question.... What is the fundamental difference between the two? To me this is not clear. I thought that a class was basically a programming object with properties and methods, but I never thought a class could not be an object. Thanks Bernard From maxnoel_fr at yahoo.fr Mon Jan 17 23:43:09 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 17 23:43:15 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <20050117220205.26896.qmail@web54301.mail.yahoo.com> References: <20050117220205.26896.qmail@web54301.mail.yahoo.com> Message-ID: <295202F2-68D9-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 17, 2005, at 22:02, Chad Crabtree wrote: > (why would an attri bute need to be a separate class?) So that the points or Karma (depending on the generation system) cost of the Attribute can be calculated without having to resort to procedural programming. You'd be then able to get the Karma cost of all the attributes by using a line such as: return sum([att.getKarmaCost() for att in attributes]) The following is a preliminary Attribute class I dug up from my hard drive, made for the BeCKS creation system. Feel free to use bits of it... #!/usr/bin/env python class Attribute: """Shadowrun character attribute.""" def __init__(self, baseRating = 1, racialMod = 0, augCyber = 0, augMagic = 0): """Create an Attribute.""" # Initialize the class attributes... self._baseRating = 1 self._racialMod = 0 self._augCyber = 0 self._augMagic = 0 self.setBaseRating(baseRating) self.setRacialMod(racialMod) self.setAugCyber(augCyber) self.setAugMagic(augMagic) def getRating(self): """Return the attribute's modified Rating.""" return self._baseRating + self._augCyber + self._augMagic def getBaseRating(self): """Return the attribute's base Rating (unmodified by magic or cyber).""" return self._baseRating def setBaseRating(self, baseRating): """Set the attribute's base Rating. baseRating must be an int equal to or greater than max(1, 1 + racial mod).""" if type(baseRating) != int: raise TypeError, "Attribute rating must be an int." if baseRating < 1: # Attribute rating can't be lower than 1. baseRating = 1 if baseRating < 1 + self._racialMod: # Attribute rating can't be lower than 1 + racial mod. baseRating = 1 + self._racialMod self._baseRating = baseRating def getRacialMod(self): return self._racialMod def setRacialMod(self, racialMod = 0): """Set the racial modifier for the attribute. racialMod must be an int.""" if type(racialMod) != int: raise TypeError, "Racial modifier must be an int." self._racialMod = racialMod # Re-set the base Rating to enforce the racial limits self.setBaseRating(self._baseRating) def getAugCyber(self): return self._augCyber def setAugCyber(self, augCyber): """Set the cyber augmentation(s) for the Attribute. augCyber must be an int.""" if type(augCyber) != int: raise TypeError, "Cyber augmentation must be an int." self._augCyber = augCyber def getAugMagic(self): return self._augMagic def setAugMagic(self, augMagic): """Set the magic augmentation(s) for the Attribute. augMagic must be an int.""" if type(augMagic) != int: raise TypeError, "Magic augmentation must be an int." self._augMagic = augMagic def getKarmaCost(self): """Return the BeCKS Karma cost of the Attribute.""" rating = self.getBaseRating() racialMod = self.getRacialMod() minr = max(1, 1 + racialMod) maxr = 6 + racialMod if rating <= maxr: # Could (will) be optimized for speed, but the formula is much clearer this way cost = sum([2*i for i in range(minr + 1, rating + 1)]) else: cost = sum([2*i for i in range(minr + 1, maxr + 1)]) + sum([3*i for i in range(maxr + 1, rating + 1)]) return cost if __name__ == '__main__': # Run a simple test procedure atr = Attribute() print atr.getRating() atr.setRacialMod(1) print atr.getRating() atr.setAugCyber(1) atr.setAugMagic(1) atr.setBaseRating(3) print atr.getBaseRating(), atr.getRating() print 'Karma total cost', atr.getKarmaCost() del atr -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Mon Jan 17 23:46:27 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 17 23:46:45 2005 Subject: [Tutor] COP vs OOP (was: Objects, persistence & getting) In-Reply-To: <41EC3F35.3070007@bernardlebel.com> References: <036c01c4fc68$ef39afb0$40b68651@xp> <41EC3F35.3070007@bernardlebel.com> Message-ID: <9F193668-68D9-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 17, 2005, at 22:41, Bernard Lebel wrote: > Alan Gauld wrote: > In fact >> I usually refer to Java as a Class Oriented Programming >> rather than Object Oriented. > > > If you allow me a question.... > > What is the fundamental difference between the two? To me this is not > clear. I thought that a class was basically a programming object with > properties and methods, but I never thought a class could not be an > object. if you're only using static (class) methods, then your class can't/needn't be instanciated, and then is nothing more than the equivalent of a Python module. I think that's what Alan means by class-oriented programming. However, all the Java programming I've done so far has been true OOP (hopefully; it was for a Uni module called Object-Oriented Software Engineering), and I fail to see what in its design does not encourage this practice. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From python at bernardlebel.com Tue Jan 18 00:07:27 2005 From: python at bernardlebel.com (Bernard Lebel) Date: Tue Jan 18 00:07:36 2005 Subject: [Tutor] COP vs OOP In-Reply-To: <9F193668-68D9-11D9-8CD8-000393CBC88E@yahoo.fr> References: <036c01c4fc68$ef39afb0$40b68651@xp> <41EC3F35.3070007@bernardlebel.com> <9F193668-68D9-11D9-8CD8-000393CBC88E@yahoo.fr> Message-ID: <41EC452F.9060204@bernardlebel.com> Max Noel wrote: if you're only using static (class) methods, then your class > can't/needn't be instanciated, and then is nothing more than the > equivalent of a Python module. I think that's what Alan means by > class-oriented programming. Okay... so if I follow you, a class that has methods not part of itself, it's not a static class...? So should I understand that a class that gets inherited methods can be considered OOP? Thanks for answering my questions! Bernard From mhansen at cso.atmel.com Tue Jan 18 00:40:17 2005 From: mhansen at cso.atmel.com (Mike Hansen) Date: Tue Jan 18 00:40:13 2005 Subject: [Tutor] Objects, persistence & getting In-Reply-To: <20050117074804.7E83F1E4011@bag.python.org> References: <20050117074804.7E83F1E4011@bag.python.org> Message-ID: <41EC4CE1.50901@cso.atmel.com> > Subject: > Re: [Tutor] Objects, persistence & getting > From: > "Alan Gauld" > Date: > Mon, 17 Jan 2005 07:48:28 -0000 > To: > "Liam Clarke" , "Tutor Tutor" > > To: > "Liam Clarke" , "Tutor Tutor" > > >>Well, one thing learning Java is good for is for thoroughly >>demystifying OOP. >> >> > > >I'd have to disagree here because Java's version of OOP has >very little to do with real OOP. Java just uss classes as >a kind of modularisation mechanism and does not make much >use of tthe real OO features. In fact it doesn't even >support several of the things that enable real OO >programming. > >And its class library, a strong feature because it is a >standard, is dreadful from an OOP p[erspective. In fact >I usually refer to Java as a Class Oriented Programming >rather than Object Oriented. > >It is possible to use Java in an OOP way (read Brice Eckel's >"Thinking in Java" to see how) but the language itself >encourages a style of programming that is much more like >Pythons modules than true OOP. > > > >>It's not some magical acronym of programming >>goodness, it's just an 'organic' way to organise code. >> >> > >Certainly in Java thats true, and indeed even at the >higher level OOP is a way of organizing code - by >finding high level abstractions and building tree >structures based on common intefaces. But Java doesn't >encourage that structuring as much as Python does! > > > >Alan G. > > > I've been reading The Object Oriented Thought Process, and it's been clearing up the OOP mystery for me. After reading a big chunk of it, I re-read the OOP sections in Learning Python. It's making a lot more sense now. Mike From billburns at pennswoods.net Tue Jan 18 01:32:07 2005 From: billburns at pennswoods.net (Bill Burns) Date: Tue Jan 18 01:21:37 2005 Subject: [Tutor] Posting a large amount of code? In-Reply-To: <037d01c4fc69$80e36b90$40b68651@xp> References: <200501161049.30727.billburns@pennswoods.net> <005401c4fc28$d82c2680$025328cf@JSLAPTOP> <037d01c4fc69$80e36b90$40b68651@xp> Message-ID: <200501171932.07661.billburns@pennswoods.net> [Jacob] > > Actually according to the above paragraph, he suggests putting them > > all in > > > a seperate class. So pseudo-code... > > > > class Pipe: > > All things related to pipe program here. > > > > class GUI: > [Alan] > Exactly so. The rationale for that was that you could work with > a collection of pipes each with their own dimensions etc. > I forgot to explain that part.... > Alan, Jacob,Orri, Liam, Thank you for your help! I appreciate it!! Alan, as you had suggested in an earlier post, I will put the pipe into a class of it's own. Actually I think I will make the command line version of the program. Once I have that working, then I can look at putting a GUI front-end on it. Like you said, 'Separating the GUI code from the logical functions is always a good idea'. I had just never considered doing it that way, what can I say, I'm just learning :-) It now makes complete sense to have the two separate. I like the idea of having a command line version and as you said, it would be easier to port to different GUI's. Thanks again, Bill From maxnoel_fr at yahoo.fr Tue Jan 18 01:36:56 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 18 01:37:02 2005 Subject: [Tutor] COP vs OOP In-Reply-To: <41EC452F.9060204@bernardlebel.com> References: <036c01c4fc68$ef39afb0$40b68651@xp> <41EC3F35.3070007@bernardlebel.com> <9F193668-68D9-11D9-8CD8-000393CBC88E@yahoo.fr> <41EC452F.9060204@bernardlebel.com> Message-ID: <0E9AC11A-68E9-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 17, 2005, at 23:07, Bernard Lebel wrote: > Okay... so if I follow you, a class that has methods not part of > itself, it's not a static class...? So should I understand that a > class that gets inherited methods can be considered OOP? Not exactly. Basically, object-oriented programming is just that; that is, working with objects, which are instances of classes. Now, class methods/attributes, which I call "static" because that's the C++/Java keyword that triggers them, are methods and attributes that are shared by all the instances of that class, not specific to a particular instance. The class doesn't even have to be instanciated (no objects have to be created) for them to be used. They are effectively part of the class itself, hence the name, class methods (as opposed to instance methods). A common use for class attributes is to keep track of how many times the class has been instanciated (number of objects created). So, if my class only has static methods and attributes, any object I create with that class is empty: it has no attributes of its own, and is impossible to be interacted with. Thus, you could just as well say it doesn't exist, and that the class can't be used to create objects. Therefore, you're using a class to do procedural programming. However, the class has its separate namespace: when you're calling methods from that class from outside its body, the call syntax is Class.method(). Which is exactly the same thing as using a function from Python module. I hope that answers your question... -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Tue Jan 18 03:38:37 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 18 03:38:52 2005 Subject: [Tutor] need advice on streamlining code... References: <20050117184833.GA2763@outreachnetworks.com> Message-ID: <001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> I seem to always be the one to suggest this, but -- "String methods are better than using the string module because the string module has been ?deprecated? or will be soon. I think that is the word here. So, do this instead." insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") insideipgrep = insideipgrepfd.readline() ## Says same thing below -- readline() just reads first line insideipfield, insideip = insideipgrep[0].strip().split("=") insideipsplit = insideip.split() insideipquads = insideipsplit[1].split(".") insidemaskquads = insideipsplit[4].split(".") And, heck, I know it wouldn't be that simple, but if the line stays the same but just the numbers change, you can do, insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") insideipgrep = insideipgrepfd.readlines() ##Wait, if you're just using the first line use insideipgrepfd.readline() insideipgrep = insideipgrep.lstrip("ifconfig_fxp0=\"inet ") temp = insideipgrep.split(" netmask ") insideipquads = temp[0].split(".") insideipmaskquads = temp[1].split(".") Warning, code just above is not very stable -- if the text of the line changes in anyway it won't work. HTH, Jacob Schmidt > The following block of code works, and provides the necessary output I'm > looking for...but I have a feeling that it's working through sheer brute > force and could be better: > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > insideipgrep = insideipgrepfd.readlines() > insideipfield, insideip = string.split(string.strip(insideipgrep[0]), > "=") > insideipsplit = string.split(insideip, " ") > insideipquads = string.split(insideipsplit[1], ".") > insidemaskquads = string.split(insideipsplit[4], ".") > > the line in /etc/rc.conf looks like: > > ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" > > Any and all thoughts/pointers are appreciated. > > ~elh > > -- > Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m > ------------------------------------------------------------------------ > www.OutreachNetworks.com 313.297.9900 > ------------------------------------------------------------------------ > JabberID: elh@jabber.org Advocate of the Theocratic Rule > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From jcruzan at gmail.com Tue Jan 18 03:59:56 2005 From: jcruzan at gmail.com (Jack Cruzan) Date: Tue Jan 18 03:55:56 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> Message-ID: <1106017196.26319.2.camel@localhost.localdomain> Wouldn't it though! I haven't checked but doesn't he use xml for his equipment lists - if that was the case it would be worth it to ask him for those files 'eh? Thanx for the input by the way will have to let you know how this goes... > Ah, things would be so much easier if McMackie would release the NSRCG > source code (despite this abomination being written in Visual Basic), > wouldn't they? ;) > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > From keridee at jayco.net Tue Jan 18 03:58:27 2005 From: keridee at jayco.net (Jacob S.) Date: Tue Jan 18 03:58:25 2005 Subject: [Tutor] Walking a directory References: Message-ID: <002901c4fd09$9a9670a0$6c5328cf@JSLAPTOP> Kent's suggestions are always my favorites, along with others, c. --nevermind To get the full path in your list-- change Kent's to this import os filelist = [] for root,directories,filenames in os.walk("Aircraft"): for filename in filenames: if filename.endswith("-set.xml"): filelist.append(os.path.join(root,filename)) for x in filelist: print x > What I want to do is rather simple, but I cannot find any good > documentation for something like this. > > The directory structure is sort of like this: >>Aircraft >>>A-10 > +A-10cl-set.xml > +A-10fg-set.xml > > For example Aircraft/A-10/A-10cl-set.xml > > Now I want to loop though each aircrafts folder (such as A-10) and > find each *-set.xml file, get the aircraft part (such as A-10cl) and > add it to a list. > > While this does seem simple, how is the question. > > I know about the walk function, but cannot find any good examples for it. > -- > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From maxnoel_fr at yahoo.fr Tue Jan 18 04:10:44 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 18 04:10:52 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <1106017196.26319.2.camel@localhost.localdomain> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> <1106017196.26319.2.camel@localhost.localdomain> Message-ID: <8AD1D8FA-68FE-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 18, 2005, at 02:59, Jack Cruzan wrote: > Wouldn't it though! I haven't checked but doesn't he use xml for his > equipment lists - if that was the case it would be worth it to ask him > for those files 'eh? Last time I checked, he didn't. I have the DAT files here (extracted them off a Windows installation of the program) and have been trying to write a Python script that would convert them to XML. However, the file format is exotic and inconsistent (just like VB, some would say ;) ), so I haven't found a way yet to write an "universal converter": the script has to be modified for each file. I'm pleased neither with this solution, nor with the way my script looks: it's ugly. At some point, such a converter will have to be written, though, unless you want to go through the extreme pleasure of going through 1 meg of text files by hand. Hmm, I really should try to start working again on this web-based SRCG idea of mine... The whole thing just screams "database". Daaargh, so many things to do, so little time. I suppose no good mod_python tutorials have spawned since last time I asked, right? -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From cyresse at gmail.com Tue Jan 18 04:45:09 2005 From: cyresse at gmail.com (Liam Clarke) Date: Tue Jan 18 04:45:12 2005 Subject: [Tutor] need advice on streamlining code... In-Reply-To: <001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> References: <20050117184833.GA2763@outreachnetworks.com> <001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> Message-ID: yeah, I wasn't sure about that readline/lines thing, cos I'm not sure how popen works. On Mon, 17 Jan 2005 21:38:37 -0500, Jacob S. wrote: > I seem to always be the one to suggest this, but -- > > "String methods are better than using the string module because the string > module has been ?deprecated? or will be soon. I think that is the word here. > So, do this instead." > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > insideipgrep = insideipgrepfd.readline() ## Says same thing below -- > readline() just reads first line > insideipfield, insideip = insideipgrep[0].strip().split("=") > insideipsplit = insideip.split() > insideipquads = insideipsplit[1].split(".") > insidemaskquads = insideipsplit[4].split(".") > > And, heck, I know it wouldn't be that simple, but if the line stays the same > but just the numbers change, you can do, > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > insideipgrep = insideipgrepfd.readlines() ##Wait, if you're just using the > first line use insideipgrepfd.readline() > insideipgrep = insideipgrep.lstrip("ifconfig_fxp0=\"inet ") > temp = insideipgrep.split(" netmask ") > insideipquads = temp[0].split(".") > insideipmaskquads = temp[1].split(".") > > Warning, code just above is not very stable -- if the text of the line > changes in anyway it won't work. > > HTH, > Jacob Schmidt > > > The following block of code works, and provides the necessary output I'm > > looking for...but I have a feeling that it's working through sheer brute > > force and could be better: > > > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > > insideipgrep = insideipgrepfd.readlines() > > insideipfield, insideip = string.split(string.strip(insideipgrep[0]), > > "=") > > insideipsplit = string.split(insideip, " ") > > insideipquads = string.split(insideipsplit[1], ".") > > insidemaskquads = string.split(insideipsplit[4], ".") > > > > the line in /etc/rc.conf looks like: > > > > ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" > > > > Any and all thoughts/pointers are appreciated. > > > > ~elh > > > > -- > > Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m > > ------------------------------------------------------------------------ > > www.OutreachNetworks.com 313.297.9900 > > ------------------------------------------------------------------------ > > JabberID: elh@jabber.org Advocate of the Theocratic Rule > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > _______________________________________________ > 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. From cyresse at gmail.com Tue Jan 18 04:46:34 2005 From: cyresse at gmail.com (Liam Clarke) Date: Tue Jan 18 04:46:37 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: <8AD1D8FA-68FE-11D9-8CD8-000393CBC88E@yahoo.fr> References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> <1106017196.26319.2.camel@localhost.localdomain> <8AD1D8FA-68FE-11D9-8CD8-000393CBC88E@yahoo.fr> Message-ID: Curious - what's mod_python? On Tue, 18 Jan 2005 03:10:44 +0000, Max Noel wrote: > > On Jan 18, 2005, at 02:59, Jack Cruzan wrote: > > > Wouldn't it though! I haven't checked but doesn't he use xml for his > > equipment lists - if that was the case it would be worth it to ask him > > for those files 'eh? > > Last time I checked, he didn't. I have the DAT files here (extracted > them off a Windows installation of the program) and have been trying to > write a Python script that would convert them to XML. > However, the file format is exotic and inconsistent (just like VB, > some would say ;) ), so I haven't found a way yet to write an > "universal converter": the script has to be modified for each file. I'm > pleased neither with this solution, nor with the way my script looks: > it's ugly. > > At some point, such a converter will have to be written, though, > unless you want to go through the extreme pleasure of going through 1 > meg of text files by hand. > > Hmm, I really should try to start working again on this web-based SRCG > idea of mine... The whole thing just screams "database". Daaargh, so > many things to do, so little time. I suppose no good mod_python > tutorials have spawned since last time I asked, right? > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > _______________________________________________ > 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. From gopinathv at hcltech.com Tue Jan 18 07:21:58 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Tue Jan 18 07:22:07 2005 Subject: [Tutor] RE: Message-ID: On Wed, 12 Jan 2005, Orri Ganel wrote: > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] > >>> stuff > [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] > >>> print [stuff[i][0] for i in range(len(stuff))] > [0, 1] Hi Orri, An alternative way to write this is: ### print [row[0] for row in stuff] ### which extracts the first element out of every "row" sublist in 'stuff'. Best of wishes! This is fine.i just want to know if row is a reserve word ? or is it a built in function in IDLe environment .. the word row is not highlighted ,what data type is (row) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050118/38b3d07a/attachment.htm From dyoo at hkn.eecs.berkeley.edu Tue Jan 18 08:17:34 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 18 08:17:37 2005 Subject: [Tutor] RE: In-Reply-To: Message-ID: > > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] > > >>> stuff > > [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] > > >>> print [stuff[i][0] for i in range(len(stuff))] > > [0, 1] > > > > An alternative way to write this is: > > > > ### > > print [row[0] for row in stuff] > > ### > > > > which extracts the first element out of every "row" sublist in > > 'stuff'. > This is fine. I just want to know if row is a reserve word? or is it a > built in function in IDLE environment. The word row is not highlighted. > What data type is (row)? Hello! When we have 'stuff' like this: ### >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] ### then we can ask for an element out of 'stuff'. One thing we can do is variable assignment: ### >>> row = stuff[0] ### 'row' here is just an arbitrarily chosen variable name. We can see that "row"'s value is one of the sublists in 'stuff' by trying: ### >>> print row [0, 'sdfsd', 'wrtew'] ### We can also use a 'for' loop to march --- to "iterate" --- across a list: ### >>> for row in stuff: ... print row, "is another element in 'stuff'" ... [0, 'sdfsd', 'wrtew'] is another element in 'stuff' [1, 'rht', 'erterg'] is another element in 'stuff' ### 'row' here is also used as a temporary variable name. In a 'for' loop, it is assigned to each element, as we repeat the loop's body. If you have more questions, please feel free to ask. From singingxduck at gmail.com Tue Jan 18 09:31:28 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Tue Jan 18 09:31:35 2005 Subject: [Tutor] RE: In-Reply-To: References: Message-ID: <41ECC960.60202@gmail.com> Gopinath V, ASDC Chennai wrote: > On Wed, 12 Jan 2005, Orri Ganel wrote: > > > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] > > >>> stuff > > [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] > > >>> print [stuff[i][0] for i in range(len(stuff))] > > [0, 1] > > > Hi Orri, > > An alternative way to write this is: > > ### > print [row[0] for row in stuff] > ### > > which extracts the first element out of every "row" sublist in 'stuff'. > > > Best of wishes! > > > This is fine.i just want to know if row is a reserve word ? > or is it a built in function > in IDLe environment .. the word row is not highlighted ,what data > type is (row) > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > >>> row Traceback (most recent call last): File "", line 1, in -toplevel- row NameError: name 'row' is not defined >>> import row Traceback (most recent call last): File "", line 1, in -toplevel- import row ImportError: No module named row Looks like its not a reserved word. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050118/68b5ee04/attachment.html From singingxduck at gmail.com Tue Jan 18 09:37:50 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Tue Jan 18 09:37:57 2005 Subject: [Tutor] RE: In-Reply-To: References: Message-ID: <41ECCADE.7090805@gmail.com> Gopinath V, ASDC Chennai wrote: > On Wed, 12 Jan 2005, Orri Ganel wrote: > > > >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] > > >>> stuff > > [[0, 'sdfsd', 'wrtew'], [1, 'rht', 'erterg']] > > >>> print [stuff[i][0] for i in range(len(stuff))] > > [0, 1] > > > Hi Orri, > > An alternative way to write this is: > > ### > print [row[0] for row in stuff] > ### > > which extracts the first element out of every "row" sublist in 'stuff'. > > > Best of wishes! > > > This is fine.i just want to know if row is a reserve word ? > or is it a built in function > in IDLe environment .. the word row is not highlighted ,what data > type is (row) > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > That'll teach me to read the whole question. In the above list comprehension, stuff is the defined list. row is a variable created by the list comprehension. Basically, this is another way of saying: for row in stuff: print row[0] or for i in range(len(stuff)): print stuff[i][0] In this case, row's type is whatever type that element in stuff is (which is a list). Ie: >>> stuff = [[0,'sdfsd','wrtew'], [1, 'rht','erterg']] >>> print [(row[0], type(row), type(row[0])) for row in stuff] [(0, , ), (1, , )] -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050118/83c7ec19/attachment-0001.htm From guillermo.fernandez.castellanos at gmail.com Tue Jan 18 10:22:08 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Tue Jan 18 10:22:12 2005 Subject: [Tutor] py2exe In-Reply-To: <1105996065.41ec2921ca2dd@www.paradise.net.nz> References: <7d7029e705011623076936f353@mail.gmail.com> <1105996065.41ec2921ca2dd@www.paradise.net.nz> Message-ID: <7d7029e70501180122586b79fc@mail.gmail.com> Hi, > When I tried, I couldn't figure out how to freeze Tix. I ended up giving up and > recoding all my Tix stuff to use Pmw instead... > (although I was a lot more new to python in those days) Indeed, I think I'm going to do the same. I find the situation really surprising though. When I go to the dist/tcl subdirectory I see tcl8.4, tk8.4 but no tix directory. Whatever... I think I'll do the same anc switch to pmw. The thing is, pmw does not seem to have ExFileSelectDialog. Any hint about how I could substitute this lack? Thanks, Guille Thanks, Guille > (and the fact that Pmw has actual documentation is an added bonus over Tix :-) ) > > -- > John. > From dyoo at hkn.eecs.berkeley.edu Tue Jan 18 10:52:09 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 18 10:52:14 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: Hi Marilyn, [Long program comments ahead. Please forgive me if some of the comments are overbearing; I'm trying to get over a cold, and I'm very grumpy. *grin*] Some comments on the code follow. I'll be focusing on: > http://www.maildance.com/python/doorman/py_daemon.py One of the import statements: ### from signal import * ### may not be safe. According to: http://docs.python.org/tut/node8.html#SECTION008410000000000000000 """Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions, and certain modules are designed to export only names that follow certain patterns.""" There's a block of code in Server.run() that looks problematic: ### while 1: if log.level & log.calls: log.it("fd%d:py_daemon.py: Waiting ...", self.descriptor) try: client_socket, client_addr = self.server_socket.accept() except socket.error, msg: time.sleep(.5) continue except (EOFError, KeyboardInterrupt): self.close_up() Spawn(client_socket).start() ### The potentially buggy line is the last one: Spawn(client_socket).start() The problem is that, as part of program flow, it appears to run after the try block. But in one particular case of program flow, 'client_socket' will not be set to a valid value. It is better to put that statement a few lines up, right where 'client_socket' is initialized. Like this: ### try: client_socket, client_addr = self.server_socket.accept() Spawn(client_socket).start() except socket.error, msg: time.sleep(.5) except (EOFError, KeyboardInterrupt): self.close_up() ### Not only does this make it more clear where 'client_socket' is being used, but it ends up making the code shorter. I've dropped the 'continue' statement, as it becomes superfluous when the Spawn() moves into the try's body. In fact, the placement of that statement may account partially for the error message that you were running into earlier. The earlier error message: ### close failed: [Errno 9] Bad file descriptor ### can easliy occur if there's an EOFError or KeyboardInterrupt under the original code. And although KeyboardInterrupt might be unusual, I'm not so sure if EOFError is. Furthermore, the 'except' blocks can emit more debugging information. You mentioned earlier that you're not getting good error tracebacks when exceptions occur. Let's fix that. Use the 'traceback' module here for all except blocks in your program. This will ensure that you get a good stack trace that we can inspect. Here's what the code block looks like, with those adjustments: ### try: client_socket, client_addr = self.server_socket.accept() Spawn(client_socket).start() except socket.error, msg: time.sleep(.5) log.it(traceback.format_exc()) except (EOFError, KeyboardInterrupt): self.close_up() log.it(traceback.format_exc()) ### I'm using Python 2.4's format_exc() function to get the stack trace as a string. If this version is not available to you, you can use this workaround format_exc(): ### import StringIO import traceback def format_exc(): """Returns the exception as a string.""" f = StringIO.StringIO() traceback.print_exc(file = f) return f.getvalue() ### Once you've made the correction and augmented all the except blocks with traceback logging, try running your program again. We should then be better able to trace down the root cause of those errors. Let me just look through a little bit more, just to see what else we can pick out. >From what I read of the code so far, I believe that a lot of the locking that the code is doing is also superfluous. You do not need to lock local variables, nor do you have to usually lock things during object initialization. For example, FileReader.__init__() has the following code: ### class FileReader(TokenReader): def __init__(self, file_socket): self.local_name = file_socket.local_name self.freader_name = '/tmp/fsf%s' % self.local_name file_lock.acquire() self.freader = open(self.freader_name, "w+") file_lock.release() ### The locks around the open() calls are unnecessary: you do not need to synchronize file opening here, as there's no way for another thread to get into the same initializer of the same instance. In fact, as far as I can tell, none of the Spawn() threads are communicating with each other. As long as your threads are working independently of each other --- and as long as they are not writing to global variables --- you do not need locks. In summary, a lot of that locking code isn't doing anything, and may actually be a source of problems if we're not careful. The other classes are complex enough that I actually don't trust either of them. I am a very paranoid person. *grin* FileReader appears to reimplement a temporary-file implementation: I'd strongly recommend using tempfile.TemporaryFile instead of reimplementing its functionality. Either that, or use StringIO() to suck the whole file into memory. How large do you expect a single email message to be? There appears to be missing symmetry between the open() and the close() calls in the code, and that's a sign of possible trouble. I try to structure most file-handling code with the following structure: ### f = open(somefile) try: ... do some stuff with f finally: f.close() ### Code that doesn't follow this idiom might be susceptable to resource leakage: we might forget to close() something. This idiom's probably not as important in Python, since we have garbage collection, but I still try to follow it to prevent things like running out of file handles too quickly. *grin* The eval() architecture that Spawn uses to dispatch to either calls.acl_rcpt, calls.route_mail, calls.route_errors, calls.doorman, or calls.doorman_errors is dangerous. I know that in prior email, you mentioned that you were using eval(). I had misgivings then, and I definitely have them now: the program calls eval() based on stuff that's coming from a socket, and that's just asking for trouble. I strongly recommend modifying Spawn.self() to something like this: ###### def tokenToDispatchFunction(self, prog): """Given a command, returns the correct dispatch function. If prog is incorrect, throws a LookupError.""" dispatchTable = { 'acl_rept' : calls.acl_rept, 'route_errors' : calls.route_errors, 'doorman' : calls.doorman, 'doorman_errors' : calls.doorman_errors' } return dispatchTable[token].main def run(self): """Executes one of the calls program.""" argv = [] dispatch = self.tokenToDispatchFunction(self.exim_io.call) dispatch(argv) ###### I've oversimplified ("broken") the code here; you'll need to reinsert the argument-construction stuff and the logging. But other than that, this should have the same external-command calling functionality as the original code, with additional safety. The danger is that the tokens that we are reading from the socket can be arbitrarily formed. One potential token that can be written might be: ### evil = '__name__[__import__("os").system("ls"+chr(32)+"/")].py' ### 'evil' is nonsensical, but according to the tokenization code, this is a single token, since it does not contain spaces. Imagine that that weird string is the first token that FileSocket.find_call() returns to us. Back in Spawn.run(), under the original code, we end up trying to evaluate a string that looks like: 'call.__name__[__import__("os").system("ls"+chr(32)+"/")].main(argv)' which in fact will break with a runtime error, but not before doing mischief. This is exactly the kind of devious exploit that eval() opens up. It's hard to close the bottle after the genie's out. The revised code that uses a dispatchTable method is much more resiliant to this kind of attack. If someone tries to send malformed commands, the worse that can happen is a KeyError. More than that, if we look back at that tokenToDispatchFunction() again: ### def tokenToDispatchFunction(self, prog): dispatchTable = { 'acl_rept' : calls.acl_rept, 'route_errors' : calls.route_errors, 'doorman' : calls.doorman, 'doorman_errors' : calls.doorman_errors' } return dispatchTable[token].main ### it's much more clear what possible calls can happen. Anyway, my eyes are going dim, so I'd better stop right now. *grin* Sorry for taking so much space; I hope this helps! From kent37 at tds.net Tue Jan 18 12:00:28 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 18 12:00:32 2005 Subject: [Tutor] need advice on streamlining code... In-Reply-To: <001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> References: <20050117184833.GA2763@outreachnetworks.com> <001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> Message-ID: <41ECEC4C.7050407@tds.net> Jacob S. wrote: > insideipgrep = insideipgrep.lstrip("ifconfig_fxp0=\"inet ") No! The argument to lstrip() is a list of characters, any of which will be stripped! It is *not* a prefix to remove! >>> insideipgrep='ifconfig if 00=_xxx Wow' >>> insideipgrep.lstrip("ifconfig_fxp0=\"inet ") 'Wow' You could use insideipgrep = insideipgrep[len("ifconfig_fxp0=\"inet "):] One minor suggestion - use embedded underscores or capital letters to make your variable names easier to read - instead of insideipgrep use insideIpGrep or inside_ip_grep. Kent From maxnoel_fr at yahoo.fr Tue Jan 18 14:05:06 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 18 14:05:15 2005 Subject: [Tutor] Objects & Classes... In-Reply-To: References: <20050117185915.17158.qmail@web54308.mail.yahoo.com> <1105995064.12772.6.camel@localhost.localdomain> <4C24D054-68D0-11D9-8CD8-000393CBC88E@yahoo.fr> <1106017196.26319.2.camel@localhost.localdomain> <8AD1D8FA-68FE-11D9-8CD8-000393CBC88E@yahoo.fr> Message-ID: <93329768-6951-11D9-8CD8-000393CBC88E@yahoo.fr> On Jan 18, 2005, at 03:46, Liam Clarke wrote: > Curious - what's mod_python? A Python module for the Apache web server, that among other things addresses the main shortcoming of CGI: mod_python (and mod_perl, mod_php and mod_ruby, for that matter) keeps the interpreter into memory (and as part of the server, so to speak). The server doesn't need to load and initialize a new interpreter each time a page is requested. This yields a tremendous speed increase (speaking in requests/second there, not in script execution time), and ensures that mod_python scales up *much* better than CGI. http://www.modpython.org/ -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From rowan at rjdarts.com Tue Jan 18 14:56:35 2005 From: rowan at rjdarts.com (rowan@rjdarts.com) Date: Tue Jan 18 14:56:46 2005 Subject: [Tutor] CGI help Message-ID: <200501182156.35322.rowan@rjdarts.com> Im trying to make a game where in a dictionary is a list of questions and answers. If i try to output 6 at a time to a cgi script and submit will my script be reset so my delete function of not getting the same question again not work? if this doesnt make sense ill repost From james.homme at highmark.com Tue Jan 18 15:14:33 2005 From: james.homme at highmark.com (james.homme@highmark.com) Date: Tue Jan 18 15:18:02 2005 Subject: [Tutor] Notetab and Python Message-ID: <200501181418.j0IEI8Zk029290@igate.highmark.com> Hi, Does anyone use Notetab to compile Python programs? If so, are you able to share the clip you use? Thanks. Jim James D Homme, Information Design + Development Highmark Inc. james.homme@highmark.com 412-544-0527 "A gentle answer turns away wrath, but a harsh word stirs up anger." From kent37 at tds.net Tue Jan 18 15:46:49 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 18 15:47:02 2005 Subject: [Tutor] CGI help In-Reply-To: <200501182156.35322.rowan@rjdarts.com> References: <200501182156.35322.rowan@rjdarts.com> Message-ID: <41ED2159.9000002@tds.net> Your CGI will be run fresh each time the user submits the form. If you want data to persist between submits you have to either - save the data to a persistent store - a file or database - use a different way to handle the form - I'm not sure of all the options but I think mod_python and FastCGI both keep the CGI process running - or use a server like CherryPy or TwistedMatrix. Kent rowan@rjdarts.com wrote: > Im trying to make a game where in a dictionary is a list of questions and > answers. If i try to output 6 at a time to a cgi script and submit will my > script be reset so my delete function of not getting the same question again > not work? > > if this doesnt make sense ill repost > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Tue Jan 18 18:10:38 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 18 18:10:46 2005 Subject: Sending a command to a program using os.system (was [Tutor]: Using os.popen*() and os.spawn*() to interact with a DOS box) In-Reply-To: <41EA8D57.3080902@gmail.com> References: <41EA00CC.4040105@gmail.com> <41EA67BA.2020707@tds.net> <41EA8D57.3080902@gmail.com> Message-ID: <41ED430E.4020304@tds.net> You might be interested in this: http://www.tizmoi.net/watsup/intro.html Kent Orri Ganel wrote: > Actually, what I want to do is set Audacity up so that it automatically > begins recording when a song plays in Windows Media Player (to begin > with) and stops when the song is finished playing. I've already figured > out how to force it to record and stop when I want to (though in a kind > of ugly hackish way)(*), and now I'm working on learning the controls of > WMP so I can test things like time left in the song, etc. I dl'ed the > WMP 10 SDK but haven't really had a chance to take a thorough look at it > yet, so hopefully it'll have what I need. > > Cheers, > Orri > From james.homme at highmark.com Tue Jan 18 18:26:24 2005 From: james.homme at highmark.com (james.homme@highmark.com) Date: Tue Jan 18 18:29:37 2005 Subject: [Tutor] Testing Message-ID: <200501181729.j0IHTlZk026457@igate.highmark.com> Hi, I have not been getting mail, so I thought I'd see what's up. Thanks. Jim James D Homme, Information Design + Development Highmark Inc. james.homme@highmark.com 412-544-0527 "A gentle answer turns away wrath, but a harsh word stirs up anger." From jcruzan at gmail.com Tue Jan 18 18:52:16 2005 From: jcruzan at gmail.com (Jack Cruzan) Date: Tue Jan 18 18:48:29 2005 Subject: [Tutor] Shadowrun programs Message-ID: <1106070736.27384.10.camel@localhost.localdomain> Greetings! Ok if any of you read my earlier email you would have seen that: A) I am a newbie programmer. B) A Shadowrun gamer. C) In over my head making a SRCG (Shadowrun Character Generator) so with that in mind gave up making my python based SRCG. Instead I am making python Shadowrun utilities! The first of which was a program size and cost calculator for decker's utilities and skillsofts. (I always hated flipping back and forth in the book to figure it out). Thought I would let you see it and maybe critique? Be kind its first original program I have written. (Well thats not php or shell script). code follows: ########################################################## #Module:SRprsize.py # #Description: Shadowrun program size and cost calculator # #Author: Jack Cruzan jcruzan at gmail dot com # #Date:01/18/05 # ########################################################### #get program's size rating = int(raw_input("What is the rating of the program you desire?")) ratingValue = rating**2 multiplier = int(raw_input("What is this programs multiplier?")) #determine the programs cost if rating < 4: print "The programs Availability is 2/7 days and costs:", rating*100 elif rating < 7: print "The programs Availability is 4/7 days and costs:", rating*200 elif rating < 10: print "The programs Availibility is 8/14 days and costs:", rating*500 else: print "The programs Availibility is 16/30 days and costs:", rating*1000 print "The program size is:", ratingValue*multiplier From maxnoel_fr at yahoo.fr Tue Jan 18 19:00:19 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 18 19:00:24 2005 Subject: [Tutor] Shadowrun programs In-Reply-To: <1106070736.27384.10.camel@localhost.localdomain> References: <1106070736.27384.10.camel@localhost.localdomain> Message-ID: On Jan 18, 2005, at 17:52, Jack Cruzan wrote: > Greetings! > Ok if any of you read my earlier email you would have seen that: > A) I am a newbie programmer. > B) A Shadowrun gamer. > C) In over my head making a SRCG (Shadowrun Character Generator) > so with that in mind gave up making my python based SRCG. Instead I am > making python Shadowrun utilities! > The first of which was a program size and cost calculator for decker's > utilities and skillsofts. (I always hated flipping back and forth in > the > book to figure it out). Thought I would let you see it and maybe > critique? Be kind its first original program I have written. (Well > thats > not php or shell script). > Doesn't look bad. However, you should aim to repeat yourself as little as possible, and the "The program's[...]" string is repeated 4 times. You should delay its display until the end of the program, like that: if rating < 4: availability = "2/7" cost = rating * 100 # same for other options... # [skipping to the end of the program] print "The program's Availability is ", availability, " days and costs ", cost Of course, an even better way is to use string formatting in the print line. But that's not really important. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From jfouhy at paradise.net.nz Tue Jan 18 19:10:51 2005 From: jfouhy at paradise.net.nz (John Fouhy) Date: Tue Jan 18 19:11:19 2005 Subject: [Tutor] py2exe In-Reply-To: <7d7029e70501180122586b79fc@mail.gmail.com> References: <7d7029e705011623076936f353@mail.gmail.com> <1105996065.41ec2921ca2dd@www.paradise.net.nz> <7d7029e70501180122586b79fc@mail.gmail.com> Message-ID: <41ED512B.1090201@paradise.net.nz> Guillermo Fernandez Castellanos wrote: > The thing is, pmw does not seem to have ExFileSelectDialog. Any hint > about how I could substitute this lack? Does ExFileSelectDialog do anything that the standard Tk file dialogs don't? (if you don't know about it: have a look at the tkFileDialog module) -- John. From dyoo at hkn.eecs.berkeley.edu Tue Jan 18 19:25:02 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Tue Jan 18 19:25:13 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Tue, 18 Jan 2005, Danny Yoo wrote: > In fact, as far as I can tell, none of the Spawn() threads are > communicating with each other. As long as your threads are working > independently of each other --- and as long as they are not writing to > global variables --- you do not need locks. > > In summary, a lot of that locking code isn't doing anything, and may > actually be a source of problems if we're not careful. Hi Marilyn, I thought about this a little bit more: there is one place where you do need locks. Locking needs to be done around Spawn's setting of its 'no' class attribute: ### class Spawn(threading.Thread): no = 1 def __init__(self, client_socket): Spawn.no = Spawn.no + 2 % 30000 ## some code later... self.local_name = str(Spawn.no) #### The problem is that it's possible that two Spawn threads will be created with the same local_name, because they are both using a shared resource: the class attribute 'no'. Two thread executions can interleave. Let's draw it out. Imagine we bring two threads t1 and t2 up, and that Spawn.no is set to 1. Now, as both are initializing, imagine that t1 runs it's initializer ### t1 ### def __init__(self, client_socket): Spawn.no = Spawn.no + 2 % 30000 ########## and then passes up control. At this point, Spawn.no = 3. Imagine that t2 now starts up: ### t2 ### def __init__(self, client_socket): Spawn.no = Spawn.no + 2 % 30000 ### some code later self.local_name = str(Spawn.no) ########## Now Spawn.no = 5, and that's what t2 uses to initialize itself. When t1 starts off where it left off, ### t1 ### ### some code later self.local_name = str(Spawn.no) ########## it too uses Spawn.no, which is still set to 5. That's the scenario we need to prevent. Spawn.no is a shared resource: it can have only one value, and unfortunately, both threads can use the same value for their own local_names. This is a major bug, because much of your code assumes that the local_name attribute is unique. This is a situation where synchronization locks are appropriate and necessary. We'll want to use locks around the whole access to Spawn.no. Something like: ### class Spawn(threading.Thread): no = 1 no_lock = threading.Lock() def __init__(self, client_socket): no_lock.acquire() try: Spawn.no = Spawn.no + 2 % 30000 ## ... rest of initializer body is here finally: no_lock.release() ### should do the trick. I'm being a little paranoid by using the try/finally block, but a little paranoia might be justified here. *grin* We need to be careful of how locks work. The following code is broken: ### class Spawn(threading.Thread): no = 1 no_lock = threading.Lock() def __init__(self, client_socket): ## buggy no_lock.acquire() Spawn.no = Spawn.no + 2 % 30000 no_lock.release() self.descriptor = client_socket.fileno() self.client_socket = client_socket no_lock.acquire() self.local_name = str(Spawn.no) no_lock.release() ### ... ### This code suffers from the same bug as the original code: two threads can come in, interleave, and see the same Spawn.no. It's not enough to put lock acquires() and releases() everywhere: we do have to use them carefully or else lose the benefit that they might provide. Anyway, I hope this helps! From phthenry at earthlink.net Tue Jan 18 22:20:22 2005 From: phthenry at earthlink.net (Paul Tremblay) Date: Tue Jan 18 22:21:02 2005 Subject: [Tutor] style question: when to "hide" variable, modules In-Reply-To: <41E86DC4.6070102@tds.net> References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> <41E86DC4.6070102@tds.net> Message-ID: <20050118212022.GD2111@localhost.localdomain> Thanks for your input (and others, too!). On Fri, Jan 14, 2005 at 08:11:32PM -0500, Kent Johnson wrote: > Date: Fri, 14 Jan 2005 20:11:32 -0500 > From: Kent Johnson > User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) > Cc: tutor@python.org > Subject: Re: [Tutor] style question: when to "hide" variable, modules > > A few thoughts: > - you might want to make a configuration object that you can pass around, > this is probably better than passing around an instance of the main Burn > class. I actually pass around many instances. Maybe this is bad programming, but I can't think of a better way to share attributes across modules. For example, one class splits lists. I want both my message class and my burn class to have access to the current state of the split list. > - typical Python style is *not* to define setter and getter functions. If > you need to mediate attribute access you can do it later using properties. I treid to figure out how this works with no luck. It seems that when you use property, you don't necessarily update the attribute. It seems I want the setattr() and getattr() functions? I always thought it was bad programming to alter an attribute directly in OO programming, but it seems I am mistaken? class Backup: __init__(self): self.some_var = 5 burn_obj = Burn(self) class Burn: __init__(self, main): setattr(main, 'some_var', 6) ??? > - you might want to consolidate your classes into a small number of > modules. This is the style of much of the Python standard library. Look at > optparse.py for an example (picked more-or-less at random). I couldn't find the optparse.py module. But looking at other packages and modules, it seems that the one module shouldn't run more than 500 lines. I * could* consolidate many of my classes in one file, but that would make very long files. Thanks Paul > - if one class has a helper class or function that isn't used anywhere > else, put the helper into the same module as the client and name the helper > starting with _. > - I tend to worry more about naming with _ in a module that is likely to be > reused. For a module that will probably be only be used once, I don't use _ > at all. In a module that has a public API I try to use _ to distinguish the > implementation details from the public stuff. > > HTH > Kent > > Paul Tremblay wrote: > >During the recent discussion on jython, a poster > >brought up the good point that one should hide > >variables and modules to indicate that they are > >not for public use: > > > >self.__for_internal_purposes = 5 > > > >__internal_stuff.py > >""" > >This module only makes sense for use with > >the parent module. > >""" > > > >So one could write several modules using these > >guidelines. One could then issue the command > >from a shell pydoc internal_stuff, and sure enough, > >one gets a nice listing of all the methods with the > >__methods listed first, signalling to someone who > >hasn't written the program (or to the author who > >returns to it a few years later), that one shouldn't > >look at these methods when looking what is useful > >from the script. > > > >My question is, how far should one take these guidlines? > > > >I am working on an OO script with several modules that > >backs up a hardrive. There will be about 10 modules, but > >really only *one* of them, called backup, would be used > >as a pubilc interface. Should I name the rest of them things > >like __handle_archive.py, __file_system.py, and so on? That > >seems odd, since I have never seen this done before. However, > >it does have an elegance of clearly telling someone how to > >hook into the modules. > > > >Also, maybe more importantly, I want to know how to handle > >attributes that need to be shared. Imagine that there are around > >20 attributes, such as the level the program runs at, and the number > >of the disk being copied that need to be shared with different > >modules. Right now, my setup looks like this: > > > ># this module is called backup.py > > > >class Backup: > > > > __init__(self, verbose, level ): > > self.__make_objects() > > self.verbose = verbose > > self.level = level > > > > > > def __make_objects(self): > > self.burn_obj = paxbac.burn.Burn(self) > > self.archive_obj = paxbac.archive.Archive(self) > > > > def get_disk(self): > > return self.__disk > > > > def set_disk(self, the_num): > > self.__disk = the_num > > > > def backup(self): > > archive_obj.archive() > > burn_obj.burn() > > > >***** > > > >#this is aother module called burn.py > > > >class Burn: > > def __init__(self, main): > > self.__main = main > > > > def burn(self): > > cd_num = self.main.get_disk() > > if self__main.level > 3: > > sys.stdout.write('Burning disk %s\n' %cd_num) > > > >**** > > > >The main module backukp provides a number of public > >methods that are really not public. For examle, no > >one is going to want to use this module to find > >out what disk the method is on. If I wanted to > >be very strict with public and private variables > >and methods, I would have to: > > > >1. change burn.py to __burn.py > > > >2. create a module called __attributes.py, create an > > object in burn.py called self.__attributes_obj, and pass > > this object to each method. > > > >These two steps seem to take things a bit far. On the other > >hand, one could look at the set of modules and immediately > >know how to use them. > > > >Thanks > > > >Paul > > > > > >_______________________________________________ > >Tutor maillist - Tutor@python.org > >http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- ************************ *Paul Tremblay * *phthenry@earthlink.net* ************************ From kent37 at tds.net Tue Jan 18 23:50:21 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 18 23:50:26 2005 Subject: [Tutor] style question: when to "hide" variable, modules In-Reply-To: <20050118212022.GD2111@localhost.localdomain> References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> <41E86DC4.6070102@tds.net> <20050118212022.GD2111@localhost.localdomain> Message-ID: <41ED92AD.7070502@tds.net> Paul Tremblay wrote: > On Fri, Jan 14, 2005 at 08:11:32PM -0500, Kent Johnson wrote: >>- typical Python style is *not* to define setter and getter functions. If >>you need to mediate attribute access you can do it later using properties. > > > I treid to figure out how this works with no luck. It seems that when > you use property, you don't necessarily update the attribute. It seems I > want the setattr() and getattr() functions? I always thought it was bad > programming to alter an attribute directly in OO programming, but it > seems I am mistaken? Well, in Java and C++ programming that is what I was taught. Python culture is different and it seems to work OK. > > class Backup: > > __init__(self): > self.some_var = 5 > > burn_obj = Burn(self) > > class Burn: > > __init__(self, main): > setattr(main, 'some_var', 6) main.some_var = 6 will work just fine. The reason that is given for using accessors is that it gives you a layer of flexibility; if you want to change the representation of the data, or make it a computed attribute, you can do that without impacting clients. Python, instead, lets you change what attribute access means. The way to do this is with 'properties'. This is kind of an advanced topic, here are two references: http://www.python.org/2.2.1/descrintro.html#property http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html#SECTION000340000000000000000 > > > ??? > > > >>- you might want to consolidate your classes into a small number of >>modules. This is the style of much of the Python standard library. Look at >>optparse.py for an example (picked more-or-less at random). > > > I couldn't find the optparse.py module. It's in the standard library - /lib/optparse.py Kent But looking at other packages > and modules, it seems that the one module shouldn't run more than 500 > lines. I * could* consolidate many of my classes in one file, but that > would make very long files. > > Thanks > > Paul > From maxnoel_fr at yahoo.fr Wed Jan 19 00:57:04 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 19 00:57:07 2005 Subject: [Tutor] style question: when to "hide" variable, modules In-Reply-To: <41ED92AD.7070502@tds.net> References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> <41E86DC4.6070102@tds.net> <20050118212022.GD2111@localhost.localdomain> <41ED92AD.7070502@tds.net> Message-ID: On Jan 18, 2005, at 22:50, Kent Johnson wrote: > Python, instead, lets you change what attribute access means. The way > to do this is with 'properties'. This is kind of an advanced topic, > here are two references: > http://www.python.org/2.2.1/descrintro.html#property > http://www.python.org/doc/2.2.3/whatsnew/sect- > rellinks.html#SECTION000340000000000000000 I have to admit, this is brilliant. Thanks for the pointer, Kent. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Wed Jan 19 01:17:51 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 19 01:17:54 2005 Subject: [Tutor] A somewhat easier way to parse XML Message-ID: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> Hi everyone, I've just spent the last few hours learning how to use the DOM XML API (to be more precise, the one that's in PyXML), instead of revising for my exams :p. My conclusion so far: it sucks (and so does SAX because I can't see a way to use it for OOP or "recursive" XML trees). I'm certain it can be used to do extremely powerful stuff, but as far as usability is concerned, it's ridiculously verbose and naming is inconsistent. I've had a look at Java DOM as well, and it's apparently the same. This afternoon, I read a bit about YAML and its basic philosophy that everything can be represented as a mix of lists, dictionaries and scalars. Now, setting aside the fact that one look at YAML made me want to ditch XML for data storage purposes completely (which I can't do since there's no Java YAML parser that I know of so far), it came to my mind once again that this is the one thing I want to be able to do in XML. Chances are that's all what 9 out of 10 programmers want to do with XML. In fact, I find it appalling that none of the "standard" XML parsers (DOM, SAX) provides an easy way to do that (yeah, I know that's what more or less what the shelve module does, but I want a language-independent way). So, to wrap my head around DOM, I set out to write a little script that does just that. Introducing xmldict.py and the DataNode class. For example, given the following XML file: Body 6 Quickness 9 ...the DataNode class (yeah, I think I may have implemented that in a slightly bizarre fashion) will produce the following dictionary: {u'attribute': [{u'@key': u'BOD', u'name': u'Body', u'rating': u'6'}, {u'@key': u'QCK', u'name': u'Quickness', u'rating': u'9'}]} As you can see, everything is represented in a mix of dictionaries, lists and unicode strings, and can now be used by a normal human being to write a program that uses this data. Comments, criticism, improvements, suggestions, [whatever]... Would be appreciated. Feel free to use it if you wish. Thanks for your attention. -------------- next part -------------- A non-text attachment was scrubbed... Name: xmldict.py Type: application/octet-stream Size: 2029 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050119/524093e7/xmldict.obj -------------- next part -------------- -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Wed Jan 19 02:21:31 2005 From: keridee at jayco.net (Jacob S.) Date: Wed Jan 19 02:21:27 2005 Subject: [Tutor] need advice on streamlining code... References: <20050117184833.GA2763@outreachnetworks.com><001601c4fd06$dfa2dc40$6c5328cf@JSLAPTOP> Message-ID: <001d01c4fdc5$3c915a80$a35428cf@JSLAPTOP> > yeah, I wasn't sure about that readline/lines thing, cos I'm not sure > how popen works. Well, I'm not positive about it either! But that doesn't mean that you can't comment out what you had, try this, and uncomment the previous if it doesn't work. I would imagine that it works because it seems to me if it can split the whole input, it can certainly split just one split deep!!! Anyway... Jacob > On Mon, 17 Jan 2005 21:38:37 -0500, Jacob S. wrote: >> I seem to always be the one to suggest this, but -- >> >> "String methods are better than using the string module because the >> string >> module has been ?deprecated? or will be soon. I think that is the word >> here. >> So, do this instead." >> >> insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") >> insideipgrep = insideipgrepfd.readline() ## Says same thing below -- >> readline() just reads first line >> insideipfield, insideip = insideipgrep[0].strip().split("=") >> insideipsplit = insideip.split() >> insideipquads = insideipsplit[1].split(".") >> insidemaskquads = insideipsplit[4].split(".") >> >> And, heck, I know it wouldn't be that simple, but if the line stays the >> same >> but just the numbers change, you can do, >> >> insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") >> insideipgrep = insideipgrepfd.readlines() ##Wait, if you're just using >> the >> first line use insideipgrepfd.readline() >> insideipgrep = insideipgrep.lstrip("ifconfig_fxp0=\"inet ") >> temp = insideipgrep.split(" netmask ") >> insideipquads = temp[0].split(".") >> insideipmaskquads = temp[1].split(".") >> >> Warning, code just above is not very stable -- if the text of the line >> changes in anyway it won't work. >> >> HTH, >> Jacob Schmidt >> >> > The following block of code works, and provides the necessary output >> > I'm >> > looking for...but I have a feeling that it's working through sheer >> > brute >> > force and could be better: >> > >> > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") >> > insideipgrep = insideipgrepfd.readlines() >> > insideipfield, insideip = >> > string.split(string.strip(insideipgrep[0]), >> > "=") >> > insideipsplit = string.split(insideip, " ") >> > insideipquads = string.split(insideipsplit[1], ".") >> > insidemaskquads = string.split(insideipsplit[4], ".") >> > >> > the line in /etc/rc.conf looks like: >> > >> > ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0" >> > >> > Any and all thoughts/pointers are appreciated. >> > >> > ~elh >> > >> > -- >> > Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o >> > m >> > ------------------------------------------------------------------------ >> > www.OutreachNetworks.com >> > 313.297.9900 >> > ------------------------------------------------------------------------ >> > JabberID: elh@jabber.org Advocate of the Theocratic >> > Rule >> > _______________________________________________ >> > Tutor maillist - Tutor@python.org >> > http://mail.python.org/mailman/listinfo/tutor >> > >> > >> >> _______________________________________________ >> 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 > > From marilyn at deliberate.com Wed Jan 19 02:29:17 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Wed Jan 19 02:33:16 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: Thank you so much Danny. I know how hard it is to look at and comment on other people's code. You know I teach C and Python and I have to say, though, that reading students' Python is 100 times easier than reading their C. And, I hope you're feeling better. I hate to think of you struggling through my code when you should be resting. > > Hi Marilyn, > > > [Long program comments ahead. Please forgive me if some of the comments > are overbearing; I'm trying to get over a cold, and I'm very grumpy. > *grin*] > > > Some comments on the code follow. I'll be focusing on: > > > http://www.maildance.com/python/doorman/py_daemon.py > > One of the import statements: > > ### > from signal import * > ### > > > may not be safe. According to: > > http://docs.python.org/tut/node8.html#SECTION008410000000000000000 > > """Note that in general the practice of importing * from a module or > package is frowned upon, since it often causes poorly readable code. Note that it says it is frowned upon, and I almost never do it, because it 'often causes poorly readable code' -- but not in this case. There is only signal.signal and signal.alarm and the signal numbers, none of which are easy to mistake for something else. I have never seen 'unsafe' --unless you cause yourself a bug by hiding something from yourself, which would be hard to do in this case. But, ok, to encourage you to make suggestions, I used the regular import on signals. No change to the bugs. > However, it is okay to use it to save typing in interactive sessions, and > certain modules are designed to export only names that follow certain > patterns.""" > > > There's a block of code in Server.run() that looks problematic: Darn it! I managed to put up a hybrid version. Here's what I thought I should have, to wrap a lock around the creation of the file descriptor for the client socket: self.server_socket.setblocking(0) while 1: if log.level & log.calls: log.it("fd%d:py_daemon.py: Waiting ...", self.descriptor) try: file_lock.acquire() try: client_socket, client_addr = self.server_socket.accept() finally: file_lock.release() except socket.error, msg: if str(msg).find('Resource temporarily unavailable') > -1: time.sleep(.2) continue except (EOFError, KeyboardInterrupt): self.close_up() Spawn(client_socket).start() Not that it matters to your suggestions. But without locks, this reduces to: while 1: if log.level & log.calls: log.it("fd%d:py_daemon.py: Waiting ...", self.descriptor) try: client_socket, client_addr = self.server_socket.accept() except (EOFError, KeyboardInterrupt): self.close_up() Spawn(client_socket).start() But, I take it, you want me to put Spawn(client_socket).start() in that try: clause. > > The problem is that, as part of program flow, it appears to run after the > try block. But in one particular case of program flow, 'client_socket' > will not be set to a valid value. It is better to put that statement a I don't understand. Which particular case of program flow will 'client_socket' be invalid and yet make it through the socket.accept call? > few lines up, right where 'client_socket' is initialized. Like this: > > ### > try: > client_socket, client_addr = self.server_socket.accept() > Spawn(client_socket).start() > except socket.error, msg: > time.sleep(.5) > except (EOFError, KeyboardInterrupt): > self.close_up() > ### > > Not only does this make it more clear where 'client_socket' is being used, > but it ends up making the code shorter. I've dropped the 'continue' > statement, as it becomes superfluous when the Spawn() moves into the try's > body. But but but, that wraps the whole thread in one try/except clause. That Spawn() call starts the thread. If a client_socket generates an error, we don't want the main thread to sleep. All the socket operations in Spawn.start() are also wrapped in their own (hopefully) intelligent try/excepts. I thought it was good practice to -not- put extra stuff in a try/except. > > In fact, the placement of that statement may account partially for the > error message that you were running into earlier. The earlier error > message: > > ### > close failed: [Errno 9] Bad file descriptor > ### > > can easliy occur if there's an EOFError or KeyboardInterrupt under the > original code. And although KeyboardInterrupt might be unusual, I'm not > so sure if EOFError is. I thought I listed exactly the two errors that come from pounding on the keyboard. I try to be careful not to mask any tracebacks. And, the original way, this error only gets triggered while we are waiting for a client to connect, not for anything else. > > Furthermore, the 'except' blocks can emit more debugging information. > You mentioned earlier that you're not getting good error tracebacks when > exceptions occur. Let's fix that. > > Use the 'traceback' module here for all except blocks in your program. > This will ensure that you get a good stack trace that we can inspect. > > I'm using Python 2.4's format_exc() function to get the stack trace as a > string. If this version is not available to you, you can use this Yes. I'm using 2.4. > workaround format_exc(): > > Once you've made the correction and augmented all the except blocks with ALL of them? That seems a similar practice to putting locks everywhere? Is there a sort of except clause that I'm writing that you think might be problematic? > traceback logging, try running your program again. We should then be > better able to trace down the root cause of those errors. > > > Let me just look through a little bit more, just to see what else we can > pick out. > > >From what I read of the code so far, I believe that a lot of the locking > that the code is doing is also superfluous. You do not need to lock local > variables, nor do you have to usually lock things during object Where did I lock local variables? The things I lock are all calls to operations that create file descriptors with the operating system, I think. Except I think the experiment is in there where I tried locking os.listdir, but it didn't help. > initialization. For example, FileReader.__init__() has the following > code: > > ### > class FileReader(TokenReader): > def __init__(self, file_socket): > self.local_name = file_socket.local_name > self.freader_name = '/tmp/fsf%s' % self.local_name > file_lock.acquire() > self.freader = open(self.freader_name, "w+") > file_lock.release() > ### > > The locks around the open() calls are unnecessary: you do not need to > synchronize file opening here, as there's no way for another thread to get > into the same initializer of the same instance. But I think that I'm only wrapping the calls that create file descriptors, because those get trampled on. > > In fact, as far as I can tell, none of the Spawn() threads are > communicating with each other. As long as your threads are working Yes. None communicate with each other. So, why was I doing this? I thought it was the obvious architecture for what I was doing. I didn't know it would be such a bear. > independently of each other --- and as long as they are not writing to > global variables --- you do not need locks. I took it all out and nothing seemed to change, like you said. > > In summary, a lot of that locking code isn't doing anything, and may > actually be a source of problems if we're not careful. > > > > The other classes are complex enough that I actually don't trust either of > them. I am a very paranoid person. *grin* > > FileReader appears to reimplement a temporary-file implementation: I'd > strongly recommend using tempfile.TemporaryFile instead of reimplementing > its functionality. Either that, or use StringIO() to suck the whole file > into memory. How large do you expect a single email message to be? As big as an email can be. Huge and more huge. We don't want to think about this program when big emails come in. When I first made my program to read pop-servers, I used the poplib library and Charlie immediately tested my code with some big messages and it brought the machine to its knees. So I rewrote it only using _socket for the real work, and keeping nothing in memory, and: boom! it is now so fast and takes almost no extra memory in the python part. And since then, please don't shoot me, but I don't immediately trust the modules. I read them and see how many times they loop through the data, and how many copies of the data they put into memory -- and usually decide to write the simple things I need myself, looping zero times and keeping only one block in memory. > > There appears to be missing symmetry between the open() and the close() > calls in the code, and that's a sign of possible trouble. I try to > structure most file-handling code with the following structure: > > ### > f = open(somefile) > try: > ... do some stuff with f > finally: > f.close() > ### > > Code that doesn't follow this idiom might be susceptable to resource > leakage: we might forget to close() something. This idiom's probably not > as important in Python, since we have garbage collection, but I still try > to follow it to prevent things like running out of file handles too > quickly. *grin* I try to put the closes with the opens too, but it's not often practical. I do log my openings and closings, as you can see, and count them for leakage. > > The eval() architecture that Spawn uses to dispatch to either > calls.acl_rcpt, calls.route_mail, calls.route_errors, calls.doorman, or > calls.doorman_errors is dangerous. I know that in prior email, you > mentioned that you were using eval(). > > I had misgivings then, and I definitely have them now: the program calls > eval() based on stuff that's coming from a socket, and that's just asking > for trouble. Well, remember that it's an internal socket and the calls are hardcoded in the exim configuration file. > > I strongly recommend modifying Spawn.self() to something like this: > > ###### > def tokenToDispatchFunction(self, prog): > """Given a command, returns the correct dispatch function. If prog > is incorrect, throws a LookupError.""" > dispatchTable = { 'acl_rept' : calls.acl_rept, > 'route_errors' : calls.route_errors, > 'doorman' : calls.doorman, > 'doorman_errors' : calls.doorman_errors' > } > return dispatchTable[token].main > > > def run(self): > """Executes one of the calls program.""" > argv = [] > dispatch = self.tokenToDispatchFunction(self.exim_io.call) > dispatch(argv) > ###### > > I've oversimplified ("broken") the code here; you'll need to reinsert the > argument-construction stuff and the logging. But other than that, this > should have the same external-command calling functionality as the > original code, with additional safety. > > The danger is that the tokens that we are reading from the socket can be > arbitrarily formed. One potential token that can be written might be: > > ### > evil = '__name__[__import__("os").system("ls"+chr(32)+"/")].py' > ### > > 'evil' is nonsensical, but according to the tokenization code, this is a > single token, since it does not contain spaces. > > > Imagine that that weird string is the first token that > FileSocket.find_call() returns to us. Back in Spawn.run(), under the > original code, we end up trying to evaluate a string that looks like: > > 'call.__name__[__import__("os").system("ls"+chr(32)+"/")].main(argv)' > > which in fact will break with a runtime error, but not before doing > mischief. This is exactly the kind of devious exploit that eval() opens > up. It's hard to close the bottle after the genie's out. > I'm sorry that you didn't realize that the socket input is hard-coded on the other side. To get to my socket, you must break into our machine, and by then, wrecking havoc will be much easier than this. > > The revised code that uses a dispatchTable method is much more resiliant > to this kind of attack. If someone tries to send malformed commands, the > worse that can happen is a KeyError. More than that, if we look back at > that tokenToDispatchFunction() again: > > ### > def tokenToDispatchFunction(self, prog): > dispatchTable = { 'acl_rept' : calls.acl_rept, > 'route_errors' : calls.route_errors, > 'doorman' : calls.doorman, > 'doorman_errors' : calls.doorman_errors' > } > return dispatchTable[token].main > ### > > it's much more clear what possible calls can happen. > Yes. I like it, but not because of security concerns, but because of the clarity. I also simplified my protocol so now the Spawn.start() is just this: def start(self): '''Given the command, provides the function to call.''' global TESTING function = { 'acl_rcpt.py' : calls.acl_rcpt, 'route_mail.py' : calls.route_mail, 'route_errors.py' : calls.route_errors, 'doorman.py' : calls.doorman, 'doorman_errors.py' : calls.doorman_errors, 'is_known_to.py' : is_known_to } if log.level & log.calls: log.it('fd%d: %s: %s', self.descriptor, self.exim_io.call, self.exim_io.collector_name) function[self.exim_io.call].main(self.exim_io) So nice! Thank you. But, I did take out threading and the big error went away. I'm done with threading, unless I see a big need one day. I don't know what I'll tell students from here on. > > Anyway, my eyes are going dim, so I'd better stop right now. *grin* Sorry > for taking so much space; I hope this helps! > > > I thought about this a little bit more: there is one place where you do > need locks. Locking needs to be done around Spawn's setting of its 'no' > class attribute: > > ### > class Spawn(threading.Thread): > no = 1 > def __init__(self, client_socket): > Spawn.no = Spawn.no + 2 % 30000 > ## some code later... > self.local_name = str(Spawn.no) > #### > > The problem is that it's possible that two Spawn threads will be created > with the same local_name, because they are both using a shared resource: > the class attribute 'no'. Two thread executions can interleave. Let's > draw it out. > But, notice that the call to: threading.Thread.__init__(self, name = self.local_name) came after, so the Spawn.no manipulation always happens in the main thread. OK? > Anyway, I hope this helps! Of course. Code review is always a huge help, and you gave me some good ideas. I'm very grateful. One problem remains after removing the threading stuff. I still get those pesky: close failed: [Errno 9] Bad file descriptor even though my logged openings and closings match up one-to-one. Now then, I haven't added that line of code to each except clause yet because 1) it doesn't seem to cause any problem 2) it's a bunch of busy-work and 3) I'm hoping that, when your illness clears, you'll think of something less brute-force for me to do. What about when I do an explicit call to a close inside a __del__. Is that a bad idea? Again, thank you thank you thank you. When I meet you (we are both in the Bay Area) I'll give you a huge hand-shake for sure. Marilyn From guillermo.fernandez.castellanos at gmail.com Wed Jan 19 03:12:40 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Wed Jan 19 03:12:44 2005 Subject: [Tutor] py2exe In-Reply-To: <7d7029e705011818128f24b85@mail.gmail.com> References: <7d7029e705011623076936f353@mail.gmail.com> <1105996065.41ec2921ca2dd@www.paradise.net.nz> <7d7029e70501180122586b79fc@mail.gmail.com> <41ED512B.1090201@paradise.net.nz> <7d7029e705011818128f24b85@mail.gmail.com> Message-ID: <7d7029e705011818125a310bf7@mail.gmail.com> Hi, > Does ExFileSelectDialog do anything that the standard Tk file dialogs don't? > (if you don't know about it: have a look at the tkFileDialog module) (*Ashamed*) Indeed not. Well... tkFileDialog is a bit more "bulky" that ExFileSelectDialot, but the functionalities are alike. Actually, my Tk reference is "An Introduction to Tkinter" http://www.pythonware.com/library/tkinter/introduction/index.htm I missed tkFileDialog even if I went through the doc 2 or 3 times looking for a substitute. Thanks a lot, Guille From jcruzan at gmail.com Wed Jan 19 04:38:11 2005 From: jcruzan at gmail.com (Jack Cruzan) Date: Wed Jan 19 04:34:18 2005 Subject: [Tutor] Shadowrun programs In-Reply-To: <41EDC4B1.6070408@speakeasy.net> References: <1106070736.27384.10.camel@localhost.localdomain> <41EDC4B1.6070408@speakeasy.net> Message-ID: <1106105891.16474.5.camel@localhost.localdomain> I am on the other coast but I too am looking for a group to play Shadowrun with. With at least three people interested think we could either -- 1) Port or create a python based SRCG (useless python still has a challenge for a collaberative effort.) 2) Make a SR rpg (I was think of in the style of Rogue or net hack but could be something more in depth) 3) Fun and Profit! -err uhm something like it? > man, I bought the first edition within weeks of it coming out. Spent > part of junior high, high school and a little of college playing all 3 > editions. Miss those days. > > *ANY OF YOU IN THE BAY AREA STILL RUNNING??* > > Enclosed is a cleaned up version using functions. This way, if you want > you could support differing rule versions. Or play around with unit testing. From david at graniteweb.com Wed Jan 19 04:58:33 2005 From: david at graniteweb.com (David Rock) Date: Wed Jan 19 04:58:43 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> Message-ID: <20050119035833.GA5860@wdfs.attbi.com> * Max Noel [2005-01-19 00:17]: > Hi everyone, > > I've just spent the last few hours learning how to use the DOM XML > API (to be more precise, the one that's in PyXML), instead of revising for > my exams :p. My conclusion so far: it sucks (and so does SAX because I > can't see a way to use it for OOP or "recursive" XML trees). > I'm certain it can be used to do extremely powerful stuff, but as > far as usability is concerned, it's ridiculously verbose and naming is > inconsistent. I've had a look at Java DOM as well, and it's apparently > the same. I'm kind of in the same boat as you are and I have come to the conclusion that XML is intended to answer specific questions with discreet answers, not walk the DOM to create a dictionary. I _think_ the idea behind this is that it would be redundant. You already have a "dictionary" of sorts in the XML itself, why create a new one? For me, it seems that the way you are supposed to interact with an XML DOM is to already know what you are looking for, and in theory, you _should_ know ;-) Still, I can't help wishing I had a simple way to create a dict from a DOM. From a Python perspective, that seems more "Pythonic" to me as well. I guess it's just a different way of looking at it. -- David Rock david@graniteweb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050118/025f3bf0/attachment.pgp From ps_python at yahoo.com Wed Jan 19 05:12:32 2005 From: ps_python at yahoo.com (kumar s) Date: Wed Jan 19 05:12:35 2005 Subject: [Tutor] Selecting text Message-ID: <20050119041232.6338.qmail@web53710.mail.yahoo.com> Dear group: I have two lists: 1. Lseq: >>> len(Lseq) 30673 >>> Lseq[20:25] ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380', 'NM_007032','NM_014332'] 2. refseq: >>> len(refseq) 1080945 >>> refseq[0:25] ['>gi|10047089|ref|NM_014332.1| Homo sapiens small muscle protein, X-linked (SMPX), mRNA', 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', 'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC', 'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAACCCC', 'CCAGAAGAAAAGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC', 'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTAAAAGTGAA', 'CTAAAATATGTCCCCAAAGCTGAACAGTAGTAGGAAGAAAAAAGGATTGATGTGAAGAAATAAAGAGGCA', 'GAAGATGGATTCAATAGCTCACTAAAATTTTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG', 'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT', 'ATCTTCATTTTTGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGTTTTGAG', 'GAGGGATATGATTTTATGGAGAATGATATGGCAATGTGCCTAACGATTTTGATGAAAAGTTTCCCAAGCT', 'ACTTCCTACAGTATTTTGGTCAATATTTGGAATGCGTTTTAGTTCTTCACCTTTTAAATTATGTCACTAA', 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA', '>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal protein (NP25), mRNA', 'TGTGCTGCTATTGTGTGGATGCCGCGCGTGTCTTCTCTTCTTTCCAGAGATGGCTAACAGGGGCCCGAGC', 'TATGGCTTAAGCCGAGAGGTGCAGGAGAAGATCGAGCAGAAGTATGATGCGGACCTGGAGAACAAGCTGG', 'TGGACTGGATCATCCTGCAGTGCGCCGAGGACATAGAGCACCCGCCCCCCGGCAGGGCCCATTTTCAGAA', 'ATGGTTAATGGACGGGACGGTCCTGTGCAAGCTGATAAATAGTTTATACCCACCAGGACAAGAGCCCATA', 'CCCAAGATCTCAGAGTCAAAGATGGCTTTTAAGCAGATGGAGCAAATCTCCCAGTTCCTAAAAGCTGCGG', 'AGACCTATGGTGTCAGAACCACCGACATCTTTCAGACGGTGGATCTATGGGAAGGGAAGGACATGGCAGC', 'TGTGCAGAGGACCCTGATGGCTTTAGGCAGCGTTGCAGTCACCAAGGATGATGGCTGCTATCGGGGAGAG', 'CCATCCTGGTTTCACAGGAAAGCCCAGCAGAATCGGAGAGGCTTTTCCGAGGAGCAGCTTCGCCAGGGAC', 'AGAACGTAATAGGCCTGCAGATGGGCAGCAACAAGGGAGCCTCCCAGGCGGGCATGACAGGGTACGGGAT', 'GCCCAGGCAGATCATGTTAGGACGCGGCATCCTGCCCCTGGTAGAGAGGACGAATGTTCCACACCATGGT'] If Lseq[i] is present in refseq[k], then I am interested in printing starting from refseq[k] until the element that starts with '>' sign. my Lseq has NM_014332 element and this is also present in second list refseq. I want to print starting from element where NM_014332 is present until next element that starts with '>' sign. In this case, it would be: '>gi|10047089|ref|NM_014332.1| Homo sapiens small muscle protein, X-linked (SMPX), mRNA', 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', 'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC', 'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAACCCC', 'CCAGAAGAAAAGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC', 'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTAAAAGTGAA', 'CTAAAATATGTCCCCAAAGCTGAACAGTAGTAGGAAGAAAAAAGGATTGATGTGAAGAAATAAAGAGGCA', 'GAAGATGGATTCAATAGCTCACTAAAATTTTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG', 'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT', 'ATCTTCATTTTTGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGTTTTGAG', 'GAGGGATATGATTTTATGGAGAATGATATGGCAATGTGCCTAACGATTTTGATGAAAAGTTTCCCAAGCT', 'ACTTCCTACAGTATTTTGGTCAATATTTGGAATGCGTTTTAGTTCTTCACCTTTTAAATTATGTCACTAA', 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA' I could not think of any smart way to do this, although I have tried like this: >>> for ele1 in Lseq: for ele2 in refseq: if ele1 in ele2: k = ele2 s = refseq[ele2].startswith('>') print k,s Traceback (most recent call last): File "", line 5, in -toplevel- s = refseq[ele2].startswith('>') TypeError: list indices must be integers I do not know how to dictate to python to select lines between two > symbols. Could any one help me thanks. K __________________________________ Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less. http://info.mail.yahoo.com/mail_250 From kent37 at tds.net Wed Jan 19 06:11:50 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 19 06:11:53 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> Message-ID: <41EDEC16.7010107@tds.net> Max Noel wrote: > Hi everyone, > > I've just spent the last few hours learning how to use the DOM XML > API (to be more precise, the one that's in PyXML), instead of revising > for my exams :p. My conclusion so far: it sucks (and so does SAX because > I can't see a way to use it for OOP or "recursive" XML trees). > I'm certain it can be used to do extremely powerful stuff, but as > far as usability is concerned, it's ridiculously verbose and naming is > inconsistent. I've had a look at Java DOM as well, and it's apparently > the same. I share your opinion that DOM is a pita. It's the same in Java because it is a 'language-neutral' spec - i.e. it sucks equally in every language :-) For Python, take a look at ElementTree, it is way easier to use. Amara looks interesting too. http://effbot.org/zone/element-index.htm http://uche.ogbuji.net/uche.ogbuji.net/tech/4Suite/amara/ For Java, try dom4j. http://www.dom4j.org Many people have tried to make more Pythonic XML libraries, you might want to look around before you write your own. Kent > > This afternoon, I read a bit about YAML and its basic philosophy > that everything can be represented as a mix of lists, dictionaries and > scalars. Now, setting aside the fact that one look at YAML made me want > to ditch XML for data storage purposes completely (which I can't do > since there's no Java YAML parser that I know of so far), it came to my > mind once again that this is the one thing I want to be able to do in > XML. Chances are that's all what 9 out of 10 programmers want to do with > XML. > In fact, I find it appalling that none of the "standard" XML parsers > (DOM, SAX) provides an easy way to do that (yeah, I know that's what > more or less what the shelve module does, but I want a > language-independent way). > > So, to wrap my head around DOM, I set out to write a little script > that does just that. Introducing xmldict.py and the DataNode class. > For example, given the following XML file: > > > > > > Body > 6 > > > Quickness > 9 > > > > > ...the DataNode class (yeah, I think I may have implemented that in > a slightly bizarre fashion) will produce the following dictionary: > > {u'attribute': [{u'@key': u'BOD', u'name': u'Body', u'rating': u'6'}, > {u'@key': u'QCK', u'name': u'Quickness', u'rating': u'9'}]} > > As you can see, everything is represented in a mix of dictionaries, > lists and unicode strings, and can now be used by a normal human being > to write a program that uses this data. > Comments, criticism, improvements, suggestions, [whatever]... Would > be appreciated. Feel free to use it if you wish. > > Thanks for your attention. > > > > > > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting and > sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From shaleh at speakeasy.net Wed Jan 19 07:31:18 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Wed Jan 19 07:31:53 2005 Subject: [Tutor] Shadowrun programs In-Reply-To: <1106105891.16474.5.camel@localhost.localdomain> References: <1106070736.27384.10.camel@localhost.localdomain> <41EDC4B1.6070408@speakeasy.net> <1106105891.16474.5.camel@localhost.localdomain> Message-ID: <41EDFEB6.4010204@speakeasy.net> Jack Cruzan wrote: > I am on the other coast but I too am looking for a group to play > Shadowrun with. > > With at least three people interested think we could either -- > > 1) Port or create a python based SRCG (useless python still has a > challenge for a collaberative effort.) > 2) Make a SR rpg (I was think of in the style of Rogue or net hack but > could be something more in depth) > 3) Fun and Profit! -err uhm something like it? > My time is unfortunately limited. However, I am available for reviews, ideas, playtesting, etc. For the record, I am a Linux user so it would be nice if any GUIs used a cross-platform toolkit. From dyoo at hkn.eecs.berkeley.edu Wed Jan 19 08:13:09 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 19 08:13:13 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Tue, 18 Jan 2005, Marilyn Davis wrote: > while 1: > if log.level & log.calls: > log.it("fd%d:py_daemon.py: Waiting ...", self.descriptor) > try: > client_socket, client_addr = self.server_socket.accept() > except (EOFError, KeyboardInterrupt): > self.close_up() > Spawn(client_socket).start() > > > The problem is that, as part of program flow, it appears to run after > > the try block. But in one particular case of program flow, > > 'client_socket' will not be set to a valid value. It is better to put > > that statement a > > I don't understand. Which particular case of program flow will > 'client_socket' be invalid and yet make it through the socket.accept > call? Hi Marilyn, If there is an EOFError or an KeyboardInterrupt, client_socket will maintain the same value as the previous iteration through the whole loop. What this potentially means is that, under strange situations, Spawn() could be called on the same client_socket twice. The issue is that the whole thing's in a 'while' loop, so we have to be careful that the state from the previous loop iteration doesn't leak into the current iteration. [About using the Standard Library] > And since then, please don't shoot me, but I don't immediately trust the > modules. I read them and see how many times they loop through the data, > and how many copies of the data they put into memory -- and usually > decide to write the simple things I need myself, looping zero times and > keeping only one block in memory. Hmm.. . Do you remember which Standard Library modules you were looking at earlier? Perhaps there was some funky stuff happening, in which case we should try to fix it, so that no one else runs into the same problems. > > ### > > class FileReader(TokenReader): > > def __init__(self, file_socket): > > self.local_name = file_socket.local_name > > self.fread~er_name = '/tmp/fsf%s' % self.local_name > > file_lock.acquire() > > self.freader = open(self.freader_name, "w+") > > file_lock.release() > > ### > > > > The locks around the open() calls are unnecessary: you do not need to > > synchronize file opening here, as there's no way for another thread to > > get into the same initializer of the same instance. > > But I think that I'm only wrapping the calls that create file > descriptors, because those get trampled on. I'm almost positive that the builtin open() function is thread-safe. Let me check that really fast... /*** Within Objects/fileobject.c: open_the_file() ***/ if (NULL == f->f_fp && NULL != name) { Py_BEGIN_ALLOW_THREADS f->f_fp = fopen(name, mode); Py_END_ALLOW_THREADS } /******/ Hmmm! If really depends if the underlying C's fopen() Standard C library is thread safe. This is true in modern versions of LIBC, so I don't think there's anything to worry about here, unless you're using a very ancient version of Unix. > But, I did take out threading and the big error went away. I'm done > with threading, unless I see a big need one day. I don't know what I'll > tell students from here on. I'd point them to John Ousterhout's article on "Why Threads are a Bad Idea (For Most Purposes)": http://home.pacbell.net/ouster/threads.pdf > > I thought about this a little bit more: there is one place where you > > do need locks. [text cut] > But, notice that the call to: > > threading.Thread.__init__(self, name = self.local_name) > > came after, so the Spawn.no manipulation always happens in the main > thread. You're right! I must have been completely delirious at that point. *grin* > One problem remains after removing the threading stuff. I still get > those pesky: > > close failed: [Errno 9] Bad file descriptor > > even though my logged openings and closings match up one-to-one. Ok, then that's a good thing to know: since threading is off, we now know that that error message has nothing to do with threads. > Now then, I haven't added that line of code to each except clause yet > because 1) it doesn't seem to cause any problem 2) it's a bunch of > busy-work and 3) I'm hoping that, when your illness clears, you'll think > of something less brute-force for me to do. I'd recommend using traceback.format_exc(). It's infuriating to get an error message like that, and not to know where in the world it's coming from. Python's default behavior for exceptions is to print out a good stack trace: one of the best things about Python's default exception handler it is that it gives us a local picture of the error. For the most part, we know around what line number we should be looking at. When we write our own except handlers, the responsibility falls on us to record good error messages. If anything, our own debugging systems should be even better than Python's. So make the debugging easier on yourself: bite down and add the traceback printouts. At least we should try to isolate and localize where that 'close failed: [Errno 9] Bad file descriptor' error message is coming from. I don't want to make any more flailing bug-hunting attempts until I know where that thing is coming from. *grin*. > What about when I do an explicit call to a close inside a __del__. Is > that a bad idea? I usually prefer to add a close() method to my objects that releases resources, rather than __del__(), because it's more visible. In a similar vein, in Spawn.run(), it might be a good idea to explicitely call write_and_close() on our FileSocket instance. For example, we can add a try/finally in the body of the revised start() method: ### def start(self): '''Given the command, provides the function to call.''' global TESTING function = { 'acl_rcpt.py' : calls.acl_rcpt, 'route_mail.py' : calls.route_mail, 'route_errors.py' : calls.route_errors, 'doorman.py' : calls.doorman, 'doorman_errors.py' : calls.doorman_errors, 'is_known_to.py' : is_known_to } if log.level & log.calls: log.it('fd%d: %s: %s', self.descriptor, self.exim_io.call, self.exim_io.collector_name) try: function[self.exim_io.call].main(self.exim_io) finally: self.exim_io.write_and_close() ### By using try/finally, we can guarantee that finalizers like 'write_and_close()' can be called at the end. I couldn't tell when write_and_close() was getting called in the original code; I suspected that each of the 'calls' dispatch functions was individually responsible for calling write_and_close(), but I wasn't sure. Best of wishes to you! From dyoo at hkn.eecs.berkeley.edu Wed Jan 19 08:37:53 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 19 08:38:06 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> Message-ID: On Wed, 19 Jan 2005, Max Noel wrote: > I've just spent the last few hours learning how to use the DOM XML > API (to be more precise, the one that's in PyXML), instead of revising > for my exams :p. My conclusion so far: it sucks (and so does SAX because > I can't see a way to use it for OOP or "recursive" XML trees). Hi Max, You are not alone in this restless feeling. In fact, Uche Ogbuji, one of the lead developers of 4Suite and Amara (which Kent mentioned earlier), just wrote a blog entry about his malcontent with the DOM. Here, these may interest you: http://www.oreillynet.com/pub/wlg/6224 http://www.oreillynet.com/pub/wlg/6225 > In fact, I find it appalling that none of the "standard" XML parsers > (DOM, SAX) provides an easy way to do that (yeah, I know that's what > more or less what the shelve module does, but I want a > language-independent way). For simple applications, the 'xmlrpclib' has two functions (dumps() and loads()) that we can use: http://www.python.org/doc/lib/node541.html For example: ### >>> s = xmlrpclib.dumps(({'hello': 'world'},)) >>> print s hello world >>> >>> >>> xmlrpclib.loads(s) (({'hello': 'world'},), None) ### A little bit silly, but it does work. The nice thing about this that xmlrpc is pretty much a platform-independent standard, so if we're dealing with simple values like strings, integers, lists, and dictionaries, we're all set. It is a bit verbose, though. Amara looks really interesting, especially because they have tools for doing data-binding in a Python-friendly way. Best of wishes to you! From ewald.ertl at hartter.com Wed Jan 19 09:29:26 2005 From: ewald.ertl at hartter.com (Ewald Ertl) Date: Wed Jan 19 09:29:30 2005 Subject: [Tutor] Selecting text In-Reply-To: <20050119041232.6338.qmail@web53710.mail.yahoo.com> References: <20050119041232.6338.qmail@web53710.mail.yahoo.com> Message-ID: <20050119092926.00003237@sunray1> Hi kumar If I unterstood your question correctly, you have to iterate over the refseq until you get the next entry, which starts with a '>' on Tue, 18 Jan 2005 20:12:32 -0800 (PST) kumar s wrote : --------------------------------------------------------------------------------------------- kumar s > I could not think of any smart way to do this, kumar s > although I have tried like this: kumar s > kumar s > >>> for ele1 in Lseq: kumar s > for ele2 in refseq: kumar s > if ele1 in ele2: kumar s > k = ele2 kumar s > s = refseq[ele2].startswith('>') Here you index the list with an element, but the index must be an integer kumar s > print k,s kumar s > kumar s > kumar s > kumar s > Traceback (most recent call last): kumar s > File "", line 5, in -toplevel- kumar s > s = refseq[ele2].startswith('>') kumar s > TypeError: list indices must be integers kumar s > kumar s > kumar s > I do not know how to dictate to python to select lines kumar s > between two > symbols. kumar s > ------------------- end ---------------------- Perhaps this is a possible solution: Iteration of Lseq an then looping over all elements in refseq. if an element of refseq contains the ele1 the list-entry is printed. The nested while-loop print's the refseq -elements as long as the element does not start with a '>' for ele1 in Lseq: i=0 while i < len(refseq): if ele1 in refseq[i]: print refseq[i] i +=1 while not refseq[i].startswith('>'): print refseq[i] i+=1 # increment until the next entry starting with '>' is found i +=1 # if search should continue if another element is present HTH Ewald -- Ing. Ewald Ertl HartterGruppe Phone : +43-3352-33085-558 trinomic Projektmanagement & Informationstechnik GmbH Fax : +43-3352-33085-600 Wiener Stra?e 41 mailto:ewald.ertl@trinomic.com A-7400 Oberwart http://www.trinomic.com mailto:ewald.ertl@hartter.com From kent37 at tds.net Wed Jan 19 12:36:09 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 19 12:36:11 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: References: Message-ID: <41EE4629.2000707@tds.net> Marilyn Davis wrote: >>few lines up, right where 'client_socket' is initialized. Like this: >> >>### >> try: >> client_socket, client_addr = self.server_socket.accept() >> Spawn(client_socket).start() >> except socket.error, msg: >> time.sleep(.5) >> except (EOFError, KeyboardInterrupt): >> self.close_up() >>### >> >>Not only does this make it more clear where 'client_socket' is being used, >>but it ends up making the code shorter. I've dropped the 'continue' >>statement, as it becomes superfluous when the Spawn() moves into the try's >>body. > > > But but but, that wraps the whole thread in one try/except clause. > That Spawn() call starts the thread. If a client_socket generates an > error, we don't want the main thread to sleep. All the socket > operations in Spawn.start() are also wrapped in their own (hopefully) > intelligent try/excepts. I thought it was good practice to -not- put > extra stuff in a try/except. Use try: except: else: The else clause is only executed if no exception happens, so the code is correct; the else is outside the scope of the try, so you don't catch unexpected exceptions. try: client_socket, client_addr = self.server_socket.accept() except socket.error, msg: time.sleep(.5) except (EOFError, KeyboardInterrupt): self.close_up() else: Spawn(client_socket).start() Kent From maxnoel_fr at yahoo.fr Wed Jan 19 12:48:10 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 19 12:48:15 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <20050119035833.GA5860@wdfs.attbi.com> References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> <20050119035833.GA5860@wdfs.attbi.com> Message-ID: On Jan 19, 2005, at 03:58, David Rock wrote: > For me, it seems that the way you are supposed to interact with an XML > DOM is to already know what you are looking for, and in theory, you > _should_ know ;-) Indeed. The problem is, even if I know what I'm looking for, the problem remains that given the following document, baz If I want to get "baz", the command is (assuming a DOM object has been created): doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal ue Quoting from memory there, it may not be entirely correct. However, the command has more characters than the document itself. Somehow I feel it'd be a bit more elegant to use: doc["bar"] (or depending on the implementation, doc["foo"]["bar"]) Don't you think? > Still, I can't help wishing I had a simple way to create a dict from a > DOM. From a Python perspective, that seems more "Pythonic" to me as > well. I guess it's just a different way of looking at it. I can't help but think that from the perspective of any other language, that would feel more [language]-ic as well ;) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Wed Jan 19 13:11:08 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 19 13:11:11 2005 Subject: [Tutor] Selecting text In-Reply-To: <20050119041232.6338.qmail@web53710.mail.yahoo.com> References: <20050119041232.6338.qmail@web53710.mail.yahoo.com> Message-ID: <41EE4E5C.5080606@tds.net> Kumar, You should look for a way to solve this with dictionaries or sets. If you look for each element of Lseq in each element of refseq, that is 33,155,825,985 lookups. That is a lot! Sets have a fast test for membership, look for ways to use them! In this case, the target string in refseq seems to be pretty easy to pick out with a regular expression. So you can do this: - put Lseq into a set - iterate through refseq looking for lines starting with > - use a regular expression search to pick out the NM_ code - look up the code in the Lseq set - if it is found, print out the sequence Here is an implementation that uses this idea. To print the entire group, I keep a flag that turns printing on and off. Kumar, please take the time to understand the basics about sets, dictionaries and iteration. You ask similar questions over and over and you don't seem to learn from the answers. You repeatedly make the same mistakes with indexing. If you don't understand the solutions, ask more questions. But please learn enough so you don't keep asking the same questions and making the same mistakes. Kent import re # Regular expression to pick out the NM_xxxxxx codes from refseq refseq_re = re.compile(r'(NM_\d+)\.1') Lseq = ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380', 'NM_007032','NM_014332'] refseq = ['>gi|10047089|ref|NM_014332.1| Homo sapiens small muscle protein, X-linked (SMPX), mRNA', 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', 'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC', 'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAACCCC', 'CCAGAAGAAAAGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC', 'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTAAAAGTGAA', 'CTAAAATATGTCCCCAAAGCTGAACAGTAGTAGGAAGAAAAAAGGATTGATGTGAAGAAATAAAGAGGCA', 'GAAGATGGATTCAATAGCTCACTAAAATTTTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG', 'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT', 'ATCTTCATTTTTGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGTTTTGAG', 'GAGGGATATGATTTTATGGAGAATGATATGGCAATGTGCCTAACGATTTTGATGAAAAGTTTCCCAAGCT', 'ACTTCCTACAGTATTTTGGTCAATATTTGGAATGCGTTTTAGTTCTTCACCTTTTAAATTATGTCACTAA', 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA', '>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal protein (NP25), mRNA', 'TGTGCTGCTATTGTGTGGATGCCGCGCGTGTCTTCTCTTCTTTCCAGAGATGGCTAACAGGGGCCCGAGC', 'TATGGCTTAAGCCGAGAGGTGCAGGAGAAGATCGAGCAGAAGTATGATGCGGACCTGGAGAACAAGCTGG', 'TGGACTGGATCATCCTGCAGTGCGCCGAGGACATAGAGCACCCGCCCCCCGGCAGGGCCCATTTTCAGAA', 'ATGGTTAATGGACGGGACGGTCCTGTGCAAGCTGATAAATAGTTTATACCCACCAGGACAAGAGCCCATA', 'CCCAAGATCTCAGAGTCAAAGATGGCTTTTAAGCAGATGGAGCAAATCTCCCAGTTCCTAAAAGCTGCGG', 'AGACCTATGGTGTCAGAACCACCGACATCTTTCAGACGGTGGATCTATGGGAAGGGAAGGACATGGCAGC', 'TGTGCAGAGGACCCTGATGGCTTTAGGCAGCGTTGCAGTCACCAAGGATGATGGCTGCTATCGGGGAGAG', 'CCATCCTGGTTTCACAGGAAAGCCCAGCAGAATCGGAGAGGCTTTTCCGAGGAGCAGCTTCGCCAGGGAC', 'AGAACGTAATAGGCCTGCAGATGGGCAGCAACAAGGGAGCCTCCCAGGCGGGCATGACAGGGTACGGGAT', 'GCCCAGGCAGATCATGTTAGGACGCGGCATCCTGCCCCTGGTAGAGAGGACGAATGTTCCACACCATGGT'] # Turn Lseq into a set so we can search it efficiently Lseq_set = set(Lseq) printing = False # Flag whether to print lines or not (are we in a target sequence?) for l in refseq: if l.startswith('>'): printing = False # Reset at start of a group # Look for an interesting group # First extract the code m = refseq_re.search(l) if m: nm_code = m.group(1) # This is the NM code if nm_code in Lseq_set: # We found a good one; turn on printing printing = True else: # This is an error - a line starting with > didn't match the re print "**** Error - couldn't match line:" print "****", l if printing: print l kumar s wrote: > Dear group: > > I have two lists: > > 1. Lseq: > > >>>>len(Lseq) > > 30673 > >>>>Lseq[20:25] > > ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380', > 'NM_007032','NM_014332'] > > > 2. refseq: > >>>>len(refseq) > > 1080945 > >>>>refseq[0:25] > > ['>gi|10047089|ref|NM_014332.1| Homo sapiens small > muscle protein, X-linked (SMPX), mRNA', > 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', > 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', > 'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC', > 'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAACCCC', > 'CCAGAAGAAAAGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC', > 'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTAAAAGTGAA', > 'CTAAAATATGTCCCCAAAGCTGAACAGTAGTAGGAAGAAAAAAGGATTGATGTGAAGAAATAAAGAGGCA', > 'GAAGATGGATTCAATAGCTCACTAAAATTTTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG', > 'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT', > 'ATCTTCATTTTTGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGTTTTGAG', > 'GAGGGATATGATTTTATGGAGAATGATATGGCAATGTGCCTAACGATTTTGATGAAAAGTTTCCCAAGCT', > 'ACTTCCTACAGTATTTTGGTCAATATTTGGAATGCGTTTTAGTTCTTCACCTTTTAAATTATGTCACTAA', > 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA', > '>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal > protein (NP25), mRNA', > 'TGTGCTGCTATTGTGTGGATGCCGCGCGTGTCTTCTCTTCTTTCCAGAGATGGCTAACAGGGGCCCGAGC', > 'TATGGCTTAAGCCGAGAGGTGCAGGAGAAGATCGAGCAGAAGTATGATGCGGACCTGGAGAACAAGCTGG', > 'TGGACTGGATCATCCTGCAGTGCGCCGAGGACATAGAGCACCCGCCCCCCGGCAGGGCCCATTTTCAGAA', > 'ATGGTTAATGGACGGGACGGTCCTGTGCAAGCTGATAAATAGTTTATACCCACCAGGACAAGAGCCCATA', > 'CCCAAGATCTCAGAGTCAAAGATGGCTTTTAAGCAGATGGAGCAAATCTCCCAGTTCCTAAAAGCTGCGG', > 'AGACCTATGGTGTCAGAACCACCGACATCTTTCAGACGGTGGATCTATGGGAAGGGAAGGACATGGCAGC', > 'TGTGCAGAGGACCCTGATGGCTTTAGGCAGCGTTGCAGTCACCAAGGATGATGGCTGCTATCGGGGAGAG', > 'CCATCCTGGTTTCACAGGAAAGCCCAGCAGAATCGGAGAGGCTTTTCCGAGGAGCAGCTTCGCCAGGGAC', > 'AGAACGTAATAGGCCTGCAGATGGGCAGCAACAAGGGAGCCTCCCAGGCGGGCATGACAGGGTACGGGAT', > 'GCCCAGGCAGATCATGTTAGGACGCGGCATCCTGCCCCTGGTAGAGAGGACGAATGTTCCACACCATGGT'] > > > If Lseq[i] is present in refseq[k], then I am > interested in printing starting from refseq[k] until > the element that starts with '>' sign. > > my Lseq has NM_014332 element and this is also present > in second list refseq. I want to print starting from > element where NM_014332 is present until next element > that starts with '>' sign. > > In this case, it would be: > '>gi|10047089|ref|NM_014332.1| Homo sapiens small > muscle protein, X-linked (SMPX), mRNA', > 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', > 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', > 'CCCTTAGGCAGTAAACAAATACATAAAGCAGGGATAAGACTGCATGAATATGTCGAAACAGCCAGTTTCC', > 'AATGTTAGAGCCATCCAGGCAAATATCAATATTCCAATGGGAGCCTTTCGGCCAGGAGCAGGTCAACCCC', > 'CCAGAAGAAAAGAATGTACTCCTGAAGTGGAGGAGGGTGTTCCTCCCACCTCGGATGAGGAGAAGAAGCC', > 'AATTCCAGGAGCGAAGAAACTTCCAGGACCTGCAGTCAATCTATCGGAAATCCAGAATATTAAAAGTGAA', > 'CTAAAATATGTCCCCAAAGCTGAACAGTAGTAGGAAGAAAAAAGGATTGATGTGAAGAAATAAAGAGGCA', > 'GAAGATGGATTCAATAGCTCACTAAAATTTTATATATTTGTATGATGATTGTGAACCTCCTGAATGCCTG', > 'AGACTCTAGCAGAAATGGCCTGTTTGTACATTTATATCTCTTCCTTCTAGTTGGCTGTATTTCTTACTTT', > 'ATCTTCATTTTTGGCACCTCACAGAACAAATTAGCCCATAAATTCAACACCTGGAGGGTGTGGTTTTGAG', > 'GAGGGATATGATTTTATGGAGAATGATATGGCAATGTGCCTAACGATTTTGATGAAAAGTTTCCCAAGCT', > 'ACTTCCTACAGTATTTTGGTCAATATTTGGAATGCGTTTTAGTTCTTCACCTTTTAAATTATGTCACTAA', > 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA' > > I could not think of any smart way to do this, > although I have tried like this: > > >>>>for ele1 in Lseq: > > for ele2 in refseq: > if ele1 in ele2: > k = ele2 > s = refseq[ele2].startswith('>') > print k,s > > > > Traceback (most recent call last): > File "", line 5, in -toplevel- > s = refseq[ele2].startswith('>') > TypeError: list indices must be integers > > > I do not know how to dictate to python to select lines > between two > symbols. > > Could any one help me thanks. > > K > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - 250MB free storage. Do more. Manage less. > http://info.mail.yahoo.com/mail_250 > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From mark.kels at gmail.com Wed Jan 19 14:21:34 2005 From: mark.kels at gmail.com (Mark Kels) Date: Wed Jan 19 14:21:39 2005 Subject: [Tutor] Python and IRC Message-ID: Hi all, I want to make an IRC bot with python... The problem is that I cant find any good information about this topic (not even documentation for the irclib module). Does anyone here have some experience with python programming for IRC (clients, bots, etc) and can give me some simple source code or a good tutorial (name of book will be fine too) ? Thanks. -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon From John.Gooch at echostar.com Wed Jan 19 15:59:00 2005 From: John.Gooch at echostar.com (Gooch, John) Date: Wed Jan 19 15:59:06 2005 Subject: [Tutor] Threaded Script Runs Find in IDLE, Never Terminates from Windows Command Prompt Message-ID: <15A1FDA26DAD524DA7A7AF77313EBA8F07BC0282@riv-excha5.echostar.com> I have a threaded python ( Python 2.3.4 ) script that runs perfectly on Windows 2000 Server SP4 when it is executed from IDLE ( i.e. press 'F5' from the editor ), the threads do their work, halt, and the 'join' command picks them up. When I run the same script from windows command line ( cmd.exe ), it appears to work normally until the last thread is 'joined'. What happens at that point is it does not execute the any lines after the join command, and will stay in the position indefinitely. Do I need to alter the script to run from the command line, or perhaps add some parameters to have it terminate successfully? Once this issue is resolved, the script will run daily via a scheduling program we use in our company, and the exit code of the script tells the scheduler whether the script completed its task successfully or not. Here is the 'code block' of my script that clean up the threads via .join: for workerThread in ThreadGroup: #timeout will difference between ( start time + 4 hours ) and current time #unless current time is greater than that, in which case the #thread gets one second to join #For example, if the current time if 30 minutes less than the endTime #time, then the thread has a timeout to 'join' of 30 minutes #timeout = endTime - time.time() #if ( timeout < 0 ): # timeout = 1 if debug: print "Joining "+workerThread.getName()+".\n" workerThread.join() if debug: print "Thread "+workerThread.getName()+" successfully joined." I run it with 3 threads, from IDLE it will close all 3 and exit normally, from the command prompt it prints the "successfully joined" for the first two, but hangs after printing "Joining Thread 3". The command line syntax is "c:"\python23\python.exe test.py", I have also tried "c:\python23\pythonw.exe test.py", but that just adds an entry in Windows task manager and sits there forever also. Thank You, From sigurd at 12move.de Wed Jan 19 14:24:43 2005 From: sigurd at 12move.de (Karl =?iso-8859-1?Q?Pfl=E4sterer?=) Date: Wed Jan 19 17:55:17 2005 Subject: [Tutor] Selecting text In-Reply-To: <20050119041232.6338.qmail@web53710.mail.yahoo.com> (kumar s.'s message of "Tue, 18 Jan 2005 20:12:32 -0800 (PST)") References: <20050119041232.6338.qmail@web53710.mail.yahoo.com> Message-ID: On 19 Jan 2005, ps_python@yahoo.com wrote: > I have two lists: > > 1. Lseq: > >>>> len(Lseq) > 30673 >>>> Lseq[20:25] > ['NM_025164', 'NM_025164', 'NM_012384', 'NM_006380', > 'NM_007032','NM_014332'] > > > 2. refseq: >>>> len(refseq) > 1080945 >>>> refseq[0:25] > ['>gi|10047089|ref|NM_014332.1| Homo sapiens small > muscle protein, X-linked (SMPX), mRNA', > 'GTTCTCAATACCGGGAGAGGCACAGAGCTATTTCAGCCACATGAAAAGCATCGGAATTGAGATCGCAGCT', > 'CAGAGGACACCGGGCGCCCCTTCCACCTTCCAAGGAGCTTTGTATTCTTGCATCTGGCTGCCTGGGACTT', [...] > 'ACTTTGTATGAGTTCAAATAAATATTTGACTAAATGTAAAATGTGA', > '>gi|10047091|ref|NM_013259.1| Homo sapiens neuronal > protein (NP25), mRNA', [...] > If Lseq[i] is present in refseq[k], then I am > interested in printing starting from refseq[k] until > the element that starts with '>' sign. > > my Lseq has NM_014332 element and this is also present > in second list refseq. I want to print starting from > element where NM_014332 is present until next element > that starts with '>' sign. > I could not think of any smart way to do this, > although I have tried like this: I give you the same answer I think you got the last times you asked such a question: use a dictionary if you want to search items. So how to do it? You could build a dictionary from refseq where the elements that can match the elemenst from Lseq are the keys. Then you iterate over Lseq, look if you find a key in your dictionary and if yes print the matching elemnt from the list. The next function creates a dictionary. The keys are the NM_... entries the values are the start and end indice of the corresponding entries. def build_dic (seq): keys = [] indice = [] for ind, entry in enumerate(seq): if entry.startswith('>'): key = entry.split('|')[3] keys.append(key) indice.append(ind) indice.append(-1) return dict(zip(keys, zip(indice, indice[1:]))) With that function you search for matching keys and if a match is found use the start and end index to extract the right elements from the list. def find_matching (rseq, lseq): d = build_dic(rseq) for key in lseq: if key in d: start, end = d[key] print rseq[start:end] Karl -- Please do *not* send copies of replies to me. I read the list From david at graniteweb.com Wed Jan 19 17:55:17 2005 From: david at graniteweb.com (David Rock) Date: Wed Jan 19 17:55:25 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> <20050119035833.GA5860@wdfs.attbi.com> Message-ID: <20050119165517.GA7018@wdfs.attbi.com> * Max Noel [2005-01-19 11:48]: > > On Jan 19, 2005, at 03:58, David Rock wrote: > > >For me, it seems that the way you are supposed to interact with an XML > >DOM is to already know what you are looking for, and in theory, you > >_should_ know ;-) > > Indeed. The problem is, even if I know what I'm looking for, the > problem remains that given the following document, > > > baz > > > If I want to get "baz", the command is (assuming a DOM object has > been created): > > doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal > ue > > Quoting from memory there, it may not be entirely correct. However, > the command has more characters than the document itself. Somehow I > feel it'd be a bit more elegant to use: > > doc["bar"] > > (or depending on the implementation, doc["foo"]["bar"]) > > Don't you think? Absolutely. That is exactly what I was hoping for, too. ElementTree comes close, but even that can be a bit unwieldy because of the multi-dimentional array you end up with. Still, if you know the data, doc[0][0] is a lot easier than doc.documentElement...nodeValue -- David Rock david@graniteweb.com -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050119/b03af9fa/attachment-0001.pgp From kent37 at tds.net Wed Jan 19 18:17:48 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 19 18:18:00 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <20050119165517.GA7018@wdfs.attbi.com> References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> <20050119035833.GA5860@wdfs.attbi.com> <20050119165517.GA7018@wdfs.attbi.com> Message-ID: <41EE963C.6090708@tds.net> David Rock wrote: > * Max Noel [2005-01-19 11:48]: > >>On Jan 19, 2005, at 03:58, David Rock wrote: >> >> >>>For me, it seems that the way you are supposed to interact with an XML >>>DOM is to already know what you are looking for, and in theory, you >>>_should_ know ;-) >> >> Indeed. The problem is, even if I know what I'm looking for, the >>problem remains that given the following document, >> >> >> baz >> >> >> If I want to get "baz", the command is (assuming a DOM object has >> been created): >> >>doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal >>ue >> >> Quoting from memory there, it may not be entirely correct. However, >>the command has more characters than the document itself. Somehow I >>feel it'd be a bit more elegant to use: >> >>doc["bar"] >> >>(or depending on the implementation, doc["foo"]["bar"]) >> >> Don't you think? > > > Absolutely. That is exactly what I was hoping for, too. ElementTree > comes close, but even that can be a bit unwieldy because of the > multi-dimentional array you end up with. Still, if you know the data, > > doc[0][0] is a lot easier than doc.documentElement...nodeValue Use the XPath support in ElementTree. Something like doc.find('foo/bar') If I understand correctly Amara allows something like doc.foo.bar I'll try to find the time to write up a full example using ElementTree, Amara and dom4j. Meanwhile see http://www.oreillynet.com/pub/wlg/6225 and http://www.oreillynet.com/pub/wlg/6239 Kent > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From john.ertl at fnmoc.navy.mil Wed Jan 19 18:38:49 2005 From: john.ertl at fnmoc.navy.mil (Ertl, John) Date: Wed Jan 19 18:38:04 2005 Subject: [Tutor] Popen and sending output to a file Message-ID: I am using the subprocess.Popen from 2.4. I can launch a job from python and the output from the job goes to the screen but now I would like to have the output go to a file. I could do the crude subprocess.Popen("dtg | cat > job.out", shell=True) But I would think there is a better way built into Popen but I could not figure out the documentation. Any help would be appreciated. John From dyoo at hkn.eecs.berkeley.edu Wed Jan 19 19:33:32 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 19 19:33:36 2005 Subject: [Tutor] Popen and sending output to a file In-Reply-To: Message-ID: On Wed, 19 Jan 2005, Ertl, John wrote: > I am using the subprocess.Popen from 2.4. I can launch a job from > python and the output from the job goes to the screen but now I would > like to have the output go to a file. I could do the crude > > subprocess.Popen("dtg | cat > job.out", shell=True) > > But I would think there is a better way built into Popen but I could not > figure out the documentation. Hi John, According to: http://www.python.org/doc/lib/node227.html we can redirect standard input, output, and error by calling Popen with the 'stdin', 'stdout', or 'stderr' keyword arguments. We should be able to do something like: ### job_output = open("job.out", "w") subprocess.Popen("dtg", shell=True, stdout=job_output) ### Best of wishes to you! From john.ertl at fnmoc.navy.mil Wed Jan 19 19:49:47 2005 From: john.ertl at fnmoc.navy.mil (Ertl, John) Date: Wed Jan 19 19:49:04 2005 Subject: [Tutor] Popen and sending output to a file Message-ID: That is too easy. I was looking at the examples of how to replace the old popen and I just did not get it but the page you sent is great. John -----Original Message----- From: Danny Yoo [mailto:dyoo@hkn.eecs.berkeley.edu] Sent: Wednesday, January 19, 2005 10:34 To: Ertl, John Cc: tutor@python.org Subject: Re: [Tutor] Popen and sending output to a file On Wed, 19 Jan 2005, Ertl, John wrote: > I am using the subprocess.Popen from 2.4. I can launch a job from > python and the output from the job goes to the screen but now I would > like to have the output go to a file. I could do the crude > > subprocess.Popen("dtg | cat > job.out", shell=True) > > But I would think there is a better way built into Popen but I could not > figure out the documentation. Hi John, According to: http://www.python.org/doc/lib/node227.html we can redirect standard input, output, and error by calling Popen with the 'stdin', 'stdout', or 'stderr' keyword arguments. We should be able to do something like: ### job_output = open("job.out", "w") subprocess.Popen("dtg", shell=True, stdout=job_output) ### Best of wishes to you! From marilyn at deliberate.com Wed Jan 19 20:53:24 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Wed Jan 19 20:57:24 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: <41EE4629.2000707@tds.net> Message-ID: Thank you Kent. On Wed, 19 Jan 2005, Kent Johnson wrote: > Marilyn Davis wrote: > >>few lines up, right where 'client_socket' is initialized. Like this: > >> > >>### > >> try: > >> client_socket, client_addr = self.server_socket.accept() > >> Spawn(client_socket).start() > >> except socket.error, msg: > >> time.sleep(.5) > >> except (EOFError, KeyboardInterrupt): > >> self.close_up() > >>### > >> > >>Not only does this make it more clear where 'client_socket' is being used, > >>but it ends up making the code shorter. I've dropped the 'continue' > >>statement, as it becomes superfluous when the Spawn() moves into the try's > >>body. > > > > > > But but but, that wraps the whole thread in one try/except clause. > > That Spawn() call starts the thread. If a client_socket generates an > > error, we don't want the main thread to sleep. All the socket > > operations in Spawn.start() are also wrapped in their own (hopefully) > > intelligent try/excepts. I thought it was good practice to -not- put > > extra stuff in a try/except. > > Use try: except: else: > The else clause is only executed if no exception happens, so the code is correct; the else is > outside the scope of the try, so you don't catch unexpected exceptions. > > try: > client_socket, client_addr = self.server_socket.accept() > except socket.error, msg: > time.sleep(.5) > except (EOFError, KeyboardInterrupt): > self.close_up() > else: > Spawn(client_socket).start() That makes good sense. But I'm not threading anymore and my call to server_socket.accept now blocks so I'm not sleeping. The only exceptions I want to catch now are those keyboard things, which make me stop. The socket.error I was catching was that there was no connections waiting right now. So my situation is much simpler. Marilyn > > Kent > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- From marilyn at deliberate.com Wed Jan 19 21:09:40 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Wed Jan 19 21:13:39 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: > > > What about when I do an explicit call to a close inside a __del__. Is > > that a bad idea? > > I usually prefer to add a close() method to my objects that releases > resources, rather than __del__(), because it's more visible. > OK Danny! I found it! When I was almost asleep last night, I knew what to do and this morning I whittled it down to this situation. Maybe it can be whittled further but I'm holding up our project and I can't take any more time right now. Note that the two variables TO_WHOM and MAILER, I think, can be safely changed to suit your environment, indeed MAILER='mail' should work for others. It happens on 2.4 but not on 2.3. Either 2.3 masks my error, or 2.4 has an error? I wouldn't be surprised if this is responsible for my thread problem but we're not going to find out. ---- #!/usr/bin/env python2.4 '''On my python2.4, this produces: ./test_exim.py close failed: [Errno 9] Bad file descriptor close failed: [Errno 9] Bad file descriptor close failed: [Errno 9] Bad file descriptor but it runs just fine. but if I specify python2.3, it runs cleanly. ''' TO_WHOM = 'marilyn' MAILER = 'exim' import popen2 import select import os class Exim: def __init__(self): self.fdin = None self.err_flush = [] self.stdout, self.stdin, self.stderr = popen2.popen3('%s -t' % MAILER) self.fdin = self.stdin.fileno() self.fdout = self.stdout.fileno() self.fderr = self.stderr.fileno() self.outlist = [self.fdout, self.fderr] self.inlist = [self.fdin] def __del__(self): self.close_up() def close_up(self): if not self.fdin: return count = 0 in_status = os.close(self.fdin) while 1: sel = select.select(self.outlist, [], [], 1) if sel[0] == []: break else: for fd in sel[0]: r = os.read(fd, 16384) if r: self.err_flush += [r] count = 0 count += 1 if count >= 5: break else: continue break self.err_flush = ''.join(self.err_flush) out_status = os.close(self.fdout) err_status = os.close(self.fderr) self.fdin = None if not in_status: return raise RuntimeError, self.err_flush def write(self, stuff): while 1: sel = select.select(self.outlist, self.inlist, [], 1) if sel[1] != []: os.write(sel[1][0], stuff) return ################################################################ if __name__ == '__main__': msg = '''To: %s xx''' % TO_WHOM p = Exim() p.write(msg) del p ---- On Tue, 18 Jan 2005, Danny Yoo wrote: > > [About using the Standard Library] > > > And since then, please don't shoot me, but I don't immediately trust the > > modules. I read them and see how many times they loop through the data, > > and how many copies of the data they put into memory -- and usually > > decide to write the simple things I need myself, looping zero times and > > keeping only one block in memory. > > Hmm.. . Do you remember which Standard Library modules you were looking at > earlier? Perhaps there was some funky stuff happening, in which case we > should try to fix it, so that no one else runs into the same problems. Yes. poplib and socket. But, it's not fair to assume that something funky is happening in there. A poplib should provide a set of tools for making a pop client and I'm not making a pop client, just pumping the message from a pop server through my python/mysql magic and into exim. Similarly with socket. It does buffering and prepares things that I don't need, but probably lots of people do. So when I say I don't "trust" stdlib modules, I mean I don't trust them to be simple enough for my situation. However, all that said, I do dimly remember that poplib perhaps had some extra processing that maybe is not needed -- but I could be wrong. > In a similar vein, in Spawn.run(), it might be a good idea to explicitely > call write_and_close() on our FileSocket instance. For example, we can > add a try/finally in the body of the revised start() method: > > ### > def start(self): > '''Given the command, provides the function to call.''' > global TESTING > function = { 'acl_rcpt.py' : calls.acl_rcpt, > 'route_mail.py' : calls.route_mail, > 'route_errors.py' : calls.route_errors, > 'doorman.py' : calls.doorman, > 'doorman_errors.py' : calls.doorman_errors, > 'is_known_to.py' : is_known_to > } > if log.level & log.calls: > log.it('fd%d: %s: %s', self.descriptor, self.exim_io.call, > self.exim_io.collector_name) > try: > function[self.exim_io.call].main(self.exim_io) > finally: > self.exim_io.write_and_close() > ### > > By using try/finally, we can guarantee that finalizers like > 'write_and_close()' can be called at the end. > > I couldn't tell when write_and_close() was getting called in the original > code; I suspected that each of the 'calls' dispatch functions was > individually responsible for calling write_and_close(), but I wasn't sure. > Yes, the function[..] is responsible to write_and_close at the proper time in its processing, which varies from function to function. At this point we are very intimate with exim. Right now a failure to write_and_close causes mail to be deferred, which is good, but it does it ungracefully. So maybe I will fiddle there to make it better. > > Best of wishes to you! Thank you. Things have improved. Are you well yet? A lot of sick people around these days! M. > > -- From dyoo at hkn.eecs.berkeley.edu Wed Jan 19 21:54:16 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 19 21:54:19 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Wed, 19 Jan 2005, Marilyn Davis wrote: > class Exim: > def __init__(self): > self.fdin = None > self.err_flush = [] > self.stdout, self.stdin, self.stderr = popen2.popen3('%s -t' % MAILER) > self.fdin = self.stdin.fileno() > self.fdout = self.stdout.fileno() > self.fderr = self.stderr.fileno() Hi Marilyn, You probably don't need to explicitly use the file descriptors here. I see that you're using them because of the use of select() later on: ### sel = select.select(self.outlist, [], [], 1) ### but, although select.select() can use file descriptor numbers, it can also take file-like objects. See: http://www.python.org/doc/lib/module-select.html So you can simplify this code to: ### sel = select.select([self.stdout, self.stderr], [], [], 1) ### The upside to this is that you don't need to use the low-level os.read() or os.close() functions at all. I suspect that some silliness with file descriptors is causing your bug, as the following: ### >>> os.close(42) Traceback (most recent call last): File "", line 1, in ? OSError: [Errno 9] Bad file descriptor ### shows that if we feed os.close() a nonsense file descriptor, it'll throw out a error message that you may be familiar with. The error can happen if we close the same file descriptor twice: ### >>> myin, myout, myerr = popen2.popen3('cat') >>> os.close(myin.fileno()) >>> os.close(myin.fileno()) Traceback (most recent call last): File "", line 1, in ? OSError: [Errno 9] Bad file descriptor ### Here is another particular case that might really be relevant: ### >>> myin, myout, myerr = popen2.popen3('cat') >>> os.close(myin.fileno()) >>> myin.close() Traceback (most recent call last): File "", line 1, in ? IOError: [Errno 9] Bad file descriptor ### We're getting an IOError here for exactly the same reasons: the high-level 'myin' object has no knowledge that its underlying file descriptor was closed. This is exactly why we've been saying not to mix file-descriptor stuff with high-level file-like object manipulation: it breaks the assumptions that the API expects to see. > However, all that said, I do dimly remember that poplib perhaps had some > extra processing that maybe is not needed -- but I could be wrong. Ok; I'll try to remember to look into poplib later and see if there's anything silly in there. > if __name__ == '__main__': > msg = '''To: %s > > xx''' % TO_WHOM > > p = Exim() > p.write(msg) > del p Instead of using __del__ implicitely, drop the __del__ method and try calling Exim.close_up() explicitely: ### p = Exim() p.write(msg) p.close_up() ### > Are you well yet? A lot of sick people around these days! Feeling better. From elh at outreachnetworks.com Wed Jan 19 22:26:06 2005 From: elh at outreachnetworks.com (Eric L. Howard) Date: Wed Jan 19 22:26:12 2005 Subject: [Tutor] need advice on streamlining code... In-Reply-To: <20050117184833.GA2763@outreachnetworks.com> References: <20050117184833.GA2763@outreachnetworks.com> Message-ID: <20050119212605.GA4889@outreachnetworks.com> At a certain time, now past [Jan.17.2005-01:48:34PM -0500], elh@outreachnetworks.com spake thusly: > The following block of code works, and provides the necessary output I'm > looking for...but I have a feeling that it's working through sheer brute > force and could be better: > > insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf") > insideipgrep = insideipgrepfd.readlines() Thanks to all who replied. I got some good ideas from the exchange. ~elh -- Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m ------------------------------------------------------------------------ www.OutreachNetworks.com 313.297.9900 ------------------------------------------------------------------------ JabberID: elh@jabber.org Advocate of the Theocratic Rule From marilyn at deliberate.com Wed Jan 19 22:24:26 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Wed Jan 19 22:28:24 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: On Wed, 19 Jan 2005, Danny Yoo wrote: > On Wed, 19 Jan 2005, Marilyn Davis wrote: > > > class Exim: > > def __init__(self): > > self.fdin = None > > self.err_flush = [] > > self.stdout, self.stdin, self.stderr = popen2.popen3('%s -t' % MAILER) > > self.fdin = self.stdin.fileno() > > self.fdout = self.stdout.fileno() > > self.fderr = self.stderr.fileno() > > > Hi Marilyn, > > You probably don't need to explicitly use the file descriptors here. I see > that you're using them because of the use of select() later on: Yippee! This stopped the error. Whew! Odd that it didn't happen on 2.3. Whatever. One wonders, how many times do you guys have to tell me to stop mixing file descriptors and file objects? I guess this was old code, before you told me that <-- weak excuse. Having the __del__ doesn't seem to hurt anything, which is a relief. There are times when I want to call close_up() and not __del__ in my real code. > > > > Are you well yet? A lot of sick people around these days! > > > Feeling better. > > Good. Take care. And, thank you some more. Marilyn -- From kent37 at tds.net Thu Jan 20 00:28:37 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 20 00:28:43 2005 Subject: [Tutor] A somewhat easier way to parse XML In-Reply-To: <41EE963C.6090708@tds.net> References: <8E4EA049-69AF-11D9-BC94-000393CBC88E@yahoo.fr> <20050119035833.GA5860@wdfs.attbi.com> <20050119165517.GA7018@wdfs.attbi.com> <41EE963C.6090708@tds.net> Message-ID: <41EEED25.7070204@tds.net> Kent Johnson wrote: >>> On Jan 19, 2005, at 03:58, David Rock wrote: >>> Indeed. The problem is, even if I know what I'm looking for, the >>> problem remains that given the following document, >>> >>> >>> baz >>> >>> >>> If I want to get "baz", the command is ... > I'll try to find the time to write up a full example using ElementTree, > Amara and dom4j. Meanwhile see http://www.oreillynet.com/pub/wlg/6225 > and http://www.oreillynet.com/pub/wlg/6239 OK, here is code to print 'baz' from a simple XML string using three different XML toolkits. (I added another level to the XML to make it a little more challenging.) This is pretty much a tie - it's three lines of code in each toolkit. The main difference is between the XPath access used by ElementTree and dom4j and the attribute access used by amara. Personally I find dom4j's full XPath support to be very handy - it essentially gives you a query engine built in to your data model. But it is a matter of taste, and amara has XPath support also. I put each example inside 'except ImportError' so I could have them all in one file - it's not something you would normally do. The ElementTree and amara examples are for CPython; the dom4j example is for Jython. Of course you need the corresponding toolkit to be correctly installed... Kent docText = ''' baz ''' # ElementTree try: from elementtree import ElementTree doc = ElementTree.XML(docText) # Note: doc represents the top-level ('doc') element print 'ElementTree' print doc.findtext('foo/bar') except ImportError: print 'No ElementTree' print # amara try: from amara import binderytools root = binderytools.bind_string(docText) # root is the 'root' element - the parent of the 'doc' element print 'amara' print root.doc.foo.bar except ImportError: print 'No amara' print # dom4j try: import org.dom4j as dom root = dom.DocumentHelper.parseText(docText) print 'dom4j' print root.valueOf('doc/foo/bar') except ImportError: print 'No dom4j' From keridee at jayco.net Thu Jan 20 03:27:24 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 20 03:27:27 2005 Subject: [Tutor] Fw: Please submit to tutor list: dictionary update prob Message-ID: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> 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 > > > #!/usr/bin/env python > > import cPickle, os, sys, re, stat > #import string > > ## Global variables > > > home = '~' > filename = os.path.join(os.path.expanduser(home), 'abook.dat') > data_holder = {} > > > > ## Functions > ################################################################## > > > def about_program(): > print > print '\t'*2, 'A simple address book written in Python' > raw_input('\nPress to continue...') > > > ## add_contact ## > def add_contact(d): > while True: > name = add_name() > email = add_email() > d[name] = email > print 'Add another contact? ' > ans = ask_yes_no() > if ans == 0: # no > print 'Save to address book? ' > get_ans = ask_yes_no() > if get_ans == 1: # yes > #collected = d > check = if_file_exist(filename) > if check is True: > update_dict(data_holder, filename) > else: # file no exist > write_book(data_holder, filename) > break > else: > d.clear() #clear dict > break > > def add_name(): > msg = 'Enter contact name: ' > while True: > try: > name = raw_input(msg) > if len(name) != 0: > if len(name) <= 20: > return name > else: > print 'Name too long: please limit to 20 characters' > else: > print 'Try again: blank not allowed!' > except EOFError: # catch ^C-D keypress > print > > def add_email(): > msg = 'Enter email address: ' > while True: > try: > email = raw_input(msg) > if len(email) == 0: > print 'Blank not allowed!' > else: > valid_format = > r'\w[-.\w]*\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com$|\ > edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$| > museum$|pro$|info$)' > valid_email = re.compile(valid_format) > if valid_email.match(email): > return email > else: > print '%s is not a valid address: try again!' % email > except EOFError: > print > > > def ask_yes_no(): > try: > ask = raw_input('Yes or No? [y|N] ') > if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']: > return 1 # yes > else: > return 0 # no > except EOFError: > print > > > def if_file_exist(f): > ''' test if file exists; returns boolean ''' > > return os.path.exists(os.path.join(os.path.expanduser('~'), f)) > > def get_filesize(f): > ''' check file size ''' > > return os.stat(os.path.join(os.path.expanduser('~'), f))[stat.ST_SIZE] > > > def write_book(d, f): > write = open(f, 'wb') > cPickle.dump(d, write) > write.close() > > def update_dict(d, f): > ''' update the saved dictionary file ''' > > read = open(f, 'rb') > newdic = cPickle.load(read) > newdic.update(d) > read.close() > > > > > ## view_abook() ## > def view_abook(d, f): > check = if_file_exist(f) > if check is True: > # load file and pretty print > read = open(f, 'rb') > d = cPickle.load(read) > for k, v in d.iteritems(): > print '%s\t%s' % (k, v) > read.close() > else: > print 'no contacts listed!' > > > > ## function tester, sort of ## > def ifaccessible(f): > ''' test if file is accessible by user; returns boolean ''' > > return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK) > > > > def main(): > while True: > select = main_menu() > while True: > if select in [ '1', 'p']: > about_program() > break > elif select in [ '2', 'a']: > add_contact(data_holder) > break > elif select in [ '3', 's']: > print "save_changes()" > break > elif select in [ '4', 'v']: > view_abook(data_holder, filename) > break > elif select in [ '5', 'f']: > print "find_contact()" > break > elif select in [ '6', 'e']: > print "edit_contact()" > break > elif select in [ '7', 'r']: > print "remove_contact()" > break > elif select in [ '0', 't', 'T']: > #print if_file_exist(filename) > #print get_filesize(filename) > try: > print get_filesize(filename) > except OSError: > print '%s not found!' % filename > break > elif select in [ '8', 'q']: > sys.exit('Goodbye') > else: > print "'%s' is invalid option!" % select > break > > > > def main_menu(): > '''Show menu options''' > > print """ > MENU OPTIONS > [1] About This [P]rogram > [2] [A]dd Contact > [3] [S]ave Changes > [4] [V]iew Address Book > [5] [F]ind Contact > [6] [E]dit Contact > [7] [R]emove Contact > [0] [T]est function > [8] [Q]uit > """ > > while 1: > try: > ask = '\nSelect an option: ' > opt = raw_input(ask)[0] > #if opt.isalpha() == True: # redundant True > if opt.isalpha(): > return opt.lower() > return opt > except (IndexError, EOFError): # catch , <^C-D> keys > pass > > > ## main program portion ## > if __name__ == '__main__': > main() > > > > > From keridee at jayco.net Thu Jan 20 03:54:23 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 20 03:54:17 2005 Subject: [Tutor] Unexpected result from decimal Message-ID: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> Hi all, I'm having a problem that is ticking me off. (to put it lightly) Why does decimal do this -- I thought that getcontext().prec was number of decimal places? >>> import decimal >>> decimal.getcontext().prec = 2 >>> a = decimal.Decimal(2) >>> b = decimal.Decimal(3) >>> 100*a/b Decimal("67") >>> print 100*a/b 67 >>> Why does it do this? It's really, really, messing things up for me because results are not interpreted in my programs correctly. Jacob From tim.peters at gmail.com Thu Jan 20 04:07:35 2005 From: tim.peters at gmail.com (Tim Peters) Date: Thu Jan 20 04:07:38 2005 Subject: [Tutor] Unexpected result from decimal In-Reply-To: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> References: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> Message-ID: <1f7befae05011919075447c447@mail.gmail.com> [Jacob S.] > I'm having a problem that is ticking me off. (to put it lightly) > Why does decimal do this -- I thought that getcontext().prec > was number of decimal places? It's unclear what you mean by "decimal places". From context, you _appear_ to mean "number of decimal digits after the radix point". In that case, no, that's not what precision means. decimal is a floating-point type, not a fixed-point type, and precision is the total number of significant digits; the location of the radix point is irrelevant. The rules are spelled out in great detail here: http://www2.hursley.ibm.com/decimal/ > >>> import decimal > >>> decimal.getcontext().prec = 2 > >>> a = decimal.Decimal(2) > >>> b = decimal.Decimal(3) > >>> 100*a/b > Decimal("67") > >>> print 100*a/b > 67 > >>> > > Why does it do this? It's doing what you told it to do. It would have helped if you had been specific about what you wanted it to do. For example, did you want 66.67, or what? > It's really, really, messing things up for me because results are > not interpreted in my programs correctly. Change your code . Perhaps this is what you wanted? >>> import decimal >>> pennies = decimal.Decimal("0.01") >>> a = decimal.Decimal(2) >>> b = decimal.Decimal(3) >>> print 100*a/b 66.66666666666666666666666667 >>> print (100*a/b).quantize(pennies) 66.67 >>> From cyresse at gmail.com Thu Jan 20 04:18:44 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 20 04:18:49 2005 Subject: [Tutor] Fw: Please submit to tutor list: dictionary update prob In-Reply-To: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> Message-ID: update_dict(data_holder, filename) > else: # file no exist > write_book(data_holder, filename) > break > else: > d.clear() #clear dict > break There's two elses, but no inital if:.... There's a place to start, right there. On Wed, 19 Jan 2005 21:27:24 -0500, 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 > > > > > > #!/usr/bin/env python > > > > import cPickle, os, sys, re, stat > > #import string > > > > ## Global variables > > > > > > home = '~' > > filename = os.path.join(os.path.expanduser(home), 'abook.dat') > > data_holder = {} > > > > > > > > ## Functions > > ################################################################## > > > > > > def about_program(): > > print > > print '\t'*2, 'A simple address book written in Python' > > raw_input('\nPress to continue...') > > > > > > ## add_contact ## > > def add_contact(d): > > while True: > > name = add_name() > > email = add_email() > > d[name] = email > > print 'Add another contact? ' > > ans = ask_yes_no() > > if ans == 0: # no > > print 'Save to address book? ' > > get_ans = ask_yes_no() > > if get_ans == 1: # yes > > #collected = d > > check = if_file_exist(filename) > > if check is True: > > update_dict(data_holder, filename) > > else: # file no exist > > write_book(data_holder, filename) > > break > > else: > > d.clear() #clear dict > > break > > > > def add_name(): > > msg = 'Enter contact name: ' > > while True: > > try: > > name = raw_input(msg) > > if len(name) != 0: > > if len(name) <= 20: > > return name > > else: > > print 'Name too long: please limit to 20 characters' > > else: > > print 'Try again: blank not allowed!' > > except EOFError: # catch ^C-D keypress > > print > > > > def add_email(): > > msg = 'Enter email address: ' > > while True: > > try: > > email = raw_input(msg) > > if len(email) == 0: > > print 'Blank not allowed!' > > else: > > valid_format = > > r'\w[-.\w]*\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com$|\ > > edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$| > > museum$|pro$|info$)' > > valid_email = re.compile(valid_format) > > if valid_email.match(email): > > return email > > else: > > print '%s is not a valid address: try again!' % email > > except EOFError: > > print > > > > > > def ask_yes_no(): > > try: > > ask = raw_input('Yes or No? [y|N] ') > > if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']: > > return 1 # yes > > else: > > return 0 # no > > except EOFError: > > print > > > > > > def if_file_exist(f): > > ''' test if file exists; returns boolean ''' > > > > return os.path.exists(os.path.join(os.path.expanduser('~'), f)) > > > > def get_filesize(f): > > ''' check file size ''' > > > > return os.stat(os.path.join(os.path.expanduser('~'), f))[stat.ST_SIZE] > > > > > > def write_book(d, f): > > write = open(f, 'wb') > > cPickle.dump(d, write) > > write.close() > > > > def update_dict(d, f): > > ''' update the saved dictionary file ''' > > > > read = open(f, 'rb') > > newdic = cPickle.load(read) > > newdic.update(d) > > read.close() > > > > > > > > > > ## view_abook() ## > > def view_abook(d, f): > > check = if_file_exist(f) > > if check is True: > > # load file and pretty print > > read = open(f, 'rb') > > d = cPickle.load(read) > > for k, v in d.iteritems(): > > print '%s\t%s' % (k, v) > > read.close() > > else: > > print 'no contacts listed!' > > > > > > > > ## function tester, sort of ## > > def ifaccessible(f): > > ''' test if file is accessible by user; returns boolean ''' > > > > return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK) > > > > > > > > def main(): > > while True: > > select = main_menu() > > while True: > > if select in [ '1', 'p']: > > about_program() > > break > > elif select in [ '2', 'a']: > > add_contact(data_holder) > > break > > elif select in [ '3', 's']: > > print "save_changes()" > > break > > elif select in [ '4', 'v']: > > view_abook(data_holder, filename) > > break > > elif select in [ '5', 'f']: > > print "find_contact()" > > break > > elif select in [ '6', 'e']: > > print "edit_contact()" > > break > > elif select in [ '7', 'r']: > > print "remove_contact()" > > break > > elif select in [ '0', 't', 'T']: > > #print if_file_exist(filename) > > #print get_filesize(filename) > > try: > > print get_filesize(filename) > > except OSError: > > print '%s not found!' % filename > > break > > elif select in [ '8', 'q']: > > sys.exit('Goodbye') > > else: > > print "'%s' is invalid option!" % select > > break > > > > > > > > def main_menu(): > > '''Show menu options''' > > > > print """ > > MENU OPTIONS > > [1] About This [P]rogram > > [2] [A]dd Contact > > [3] [S]ave Changes > > [4] [V]iew Address Book > > [5] [F]ind Contact > > [6] [E]dit Contact > > [7] [R]emove Contact > > [0] [T]est function > > [8] [Q]uit > > """ > > > > while 1: > > try: > > ask = '\nSelect an option: ' > > opt = raw_input(ask)[0] > > #if opt.isalpha() == True: # redundant True > > if opt.isalpha(): > > return opt.lower() > > return opt > > except (IndexError, EOFError): # catch , <^C-D> keys > > pass > > > > > > ## main program portion ## > > if __name__ == '__main__': > > main() > > > > > > > > > > > > _______________________________________________ > 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. From cyresse at gmail.com Thu Jan 20 04:21:53 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 20 04:21:56 2005 Subject: [Tutor] Unexpected result from decimal In-Reply-To: <1f7befae05011919075447c447@mail.gmail.com> References: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> <1f7befae05011919075447c447@mail.gmail.com> Message-ID: Jacob- one slight flaw/quirk in Python is if you want floating point computations you have to specify a floating point. >>> import decimal >>> decimal.getcontext().prec = 2 >>> a = decimal.Decimal(2) >>> b = decimal.Decimal(3) >>> 100*a/b Decimal("67") >>> print 100*a/b try - a=decimal.Decimal(2.0) b = decimal.Decimal(3) print 100*a/b Same as writing 100/3.0 as opposed to 100/3. Try it. On Wed, 19 Jan 2005 22:07:35 -0500, Tim Peters wrote: > [Jacob S.] > > I'm having a problem that is ticking me off. (to put it lightly) > > Why does decimal do this -- I thought that getcontext().prec > > was number of decimal places? > > It's unclear what you mean by "decimal places". From context, you > _appear_ to mean "number of decimal digits after the radix point". In > that case, no, that's not what precision means. decimal is a > floating-point type, not a fixed-point type, and precision is the > total number of significant digits; the location of the radix point is > irrelevant. The rules are spelled out in great detail here: > > http://www2.hursley.ibm.com/decimal/ > > > >>> import decimal > > >>> decimal.getcontext().prec = 2 > > >>> a = decimal.Decimal(2) > > >>> b = decimal.Decimal(3) > > >>> 100*a/b > > Decimal("67") > > >>> print 100*a/b > > 67 > > >>> > > > > Why does it do this? > > It's doing what you told it to do. It would have helped if you had > been specific about what you wanted it to do. For example, did you > want 66.67, or what? > > > It's really, really, messing things up for me because results are > > not interpreted in my programs correctly. > > Change your code . Perhaps this is what you wanted? > > >>> import decimal > >>> pennies = decimal.Decimal("0.01") > >>> a = decimal.Decimal(2) > >>> b = decimal.Decimal(3) > >>> print 100*a/b > 66.66666666666666666666666667 > >>> print (100*a/b).quantize(pennies) > 66.67 > >>> > _______________________________________________ > 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. From singingxduck at gmail.com Thu Jan 20 04:23:57 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Thu Jan 20 04:24:11 2005 Subject: [Tutor] Unexpected result from decimal In-Reply-To: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> References: <000301c4fe9b$5fa59690$e25428cf@JSLAPTOP> Message-ID: <41EF244D.8050106@gmail.com> Jacob S. wrote: > Hi all, > > I'm having a problem that is ticking me off. (to put it lightly) > Why does decimal do this -- I thought that getcontext().prec was > number of decimal places? > >>>> import decimal >>>> decimal.getcontext().prec = 2 >>>> a = decimal.Decimal(2) >>>> b = decimal.Decimal(3) >>>> 100*a/b >>> > Decimal("67") > >>>> print 100*a/b >>> > 67 > >>>> > > Why does it do this? > It's really, really, messing things up for me because results are not > interpreted in my programs correctly. > Jacob > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Well, from looking at the documentation, decimal.getcontext().prec *tells* you the precision, so assigning to it is meaningless. Let's see what you have to do to actually change it: >>> a = decimal.Decimal(2) >>> b = decimal.Decimal(3) >>> 100*a/b Decimal("66.66666666666666666666666667") >>> decimal.BasicContext Context(prec=9, rounding=ROUND_HALF_UP, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[Clamped, DivisionByZero, InvalidOperation, Overflow, Underflow]) >>> decimal.DefaultContext Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[DivisionByZero, InvalidOperation, Overflow]) >>> decimal.ExtendedContext Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[]) >>> decimal.setcontext(decimal.ExtendedContext) >>> decimal.getcontext().prec 9 >>> decimal.setcontext(decimal.Context(prec=50, rounding=decimal.ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[decimal.DivisionByZero, decimal.InvalidOperation, decimal.Overflow])) >>> decimal.getcontext().prec 50 >>> 100*a/b Decimal("66.666666666666666666666666666666666666666666666667") Hold on a second . . . it looks like prec doesn't refer to how many digits there will be after the decimal point, but how many total digits . . . bit of an odd way to do things, but that explains your problem. Scratch what i said before about assigning to decimal.getcontext().prec, by the way, it looks like that works after all: >>> decimal.getcontext().prec = 4 >>> 100 * a/b Decimal("66.67") HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050119/13aed834/attachment.htm From tameyer at ihug.co.nz Thu Jan 20 04:35:12 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Thu Jan 20 04:35:47 2005 Subject: [Tutor] Unexpected result from decimal In-Reply-To: Message-ID: > >>> import decimal > >>> decimal.getcontext().prec = 2 > >>> a = decimal.Decimal(2) > >>> b = decimal.Decimal(3) > >>> 100*a/b > Decimal("67") > >>> print 100*a/b This prints "67". > try - > > a=decimal.Decimal(2.0) This will not work. You can't convert a float directly to a decimal.Decimal (I believe this is so that you are forced to understand that there are precision issues involved). 'a = decimal.Decimal("2.0")' will do what you meant, though. > b = decimal.Decimal(3) > print 100*a/b However, this will print out "67", just as the above did. The reason is the one that Tim outlined: precision isn't the number of digits after the decimal place - when I was at school the latter was called "decimal places" and precision was "significant digits". > Jacob- one slight flaw/quirk in Python is if you want floating point > computations you have to specify a floating point. [...] > Same as writing 100/3.0 as opposed to 100/3. Try it. Note that you can do 'from __future__ import division' and 100/3 will be the same as 100/3.0 (and 100//3 will give you 3). =Tony.Meyer From kent37 at tds.net Thu Jan 20 05:25:17 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 20 05:25:21 2005 Subject: [Tutor] Fw: Please submit to tutor list: dictionary update prob In-Reply-To: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> Message-ID: <41EF32AD.8080700@tds.net> 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. Kent From michael at trollope.org Thu Jan 20 06:47:44 2005 From: michael at trollope.org (Michael Powe) Date: Thu Jan 20 06:47:48 2005 Subject: [Tutor] Clash of the Titans and Mundane Matters Message-ID: <20050120054744.GA26642@titan.spiretech.com> Clash of the Titans >From "Dive into Python": __init__ is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor of the class. It's tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it's the first piece of code executed in a newly created instance of the class), and even sounds like one ("init" certainly suggests a constructor-ish nature). Incorrect, because the object has already been constructed by the time __init__ is called, and you already have a valid reference to the new instance of the class. But __init__ is the closest thing you're going to get to a constructor in Python, and it fills much the same role. >From Alan's book "Learning to Program": One of the methods of this class is called __init__ and it is a special method called a constructor. The reason for the name is that it is called when a new object instance is created or constructed. Any variables assigned (and hence created in Python) inside this method will be unique to the new instance. There are a number of special methods like this in Python, nearly all distinguished by the __xxx__ naming format. ---------------- 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. But ... what are reading/writing to files and printing? Of course, I create a "file object" in order to accomplish these tasks -- but how is this object fit into the application design? Do I just create methods within the expense class to accomplish these parts of the task? When I tried this on, it seemed hacky. The other option seemed to be creating an I/O class and passing the expense objects to it. But, should that class be an interface or an object? The one thing you don't see in "how to program" java books is an implementation of I/O in the context of an application. 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. 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? Thanks for your time. It's late. ;-) Sometimes, I just have to get these things off my chest. mp From kraus at hagen-partner.de Thu Jan 20 09:05:30 2005 From: kraus at hagen-partner.de (Wolfram Kraus) Date: Thu Jan 20 09:04:18 2005 Subject: [Tutor] OT: list as newsgroup (was: Please submit to tutor list: dictionary update prob) In-Reply-To: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> Message-ID: <41EF664A.80805@hagen-partner.de> 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 From shaleh at speakeasy.net Thu Jan 20 09:23:46 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Thu Jan 20 09:24:24 2005 Subject: [Tutor] Clash of the Titans and Mundane Matters In-Reply-To: <20050120054744.GA26642@titan.spiretech.com> References: <20050120054744.GA26642@titan.spiretech.com> Message-ID: <41EF6A92.8020608@speakeasy.net> 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. From jfouhy at paradise.net.nz Thu Jan 20 09:34:29 2005 From: jfouhy at paradise.net.nz (John Fouhy) Date: Thu Jan 20 09:34:15 2005 Subject: [Tutor] Clash of the Titans and Mundane Matters In-Reply-To: <20050120054744.GA26642@titan.spiretech.com> References: <20050120054744.GA26642@titan.spiretech.com> Message-ID: <41EF6D15.8000509@paradise.net.nz> 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. From erimendz at gmail.com Thu Jan 20 10:07:49 2005 From: erimendz at gmail.com (Eri Mendz) Date: Thu Jan 20 10:11:31 2005 Subject: [Tutor] Re: Fw: Please submit to tutor list: dictionary update prob References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> <41EF32AD.8080700@tds.net> Message-ID: Kent Johnson 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 From kent37 at tds.net Thu Jan 20 12:06:17 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 20 12:06:23 2005 Subject: [Tutor] Re: Fw: Please submit to tutor list: dictionary update prob In-Reply-To: References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> <41EF32AD.8080700@tds.net> Message-ID: <41EF90A9.6080206@tds.net> Eri Mendz wrote: > Kent Johnson 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) You still aren't doing anything with newdic. The absence of 'newdic' in the code after 'read.close()' should be a clue :-) Check the docs on pickle.dump() (which is the same as cPickle.dump()): dump( obj, file[, protocol[, bin]]) Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol, bin).dump(obj). So to pickle newdic you should say cPickle.dump(newdic, write) > write.close() > > but it 'overwrites' the saved dic. can you rw in one go, dump and load the > updated dict instantly?? I think you want to overwrite the saved dict, but with the new dict instead of with a filename string... Kent > > sending this in gmane mail2news gateway. Thank you > From gopinathv at hcltech.com Thu Jan 20 12:07:06 2005 From: gopinathv at hcltech.com (Gopinath V, ASDC Chennai) Date: Thu Jan 20 12:07:20 2005 Subject: [Tutor] counting no of words Message-ID: 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050120/bb0690a2/attachment.htm From op73418 at mail.telepac.pt Thu Jan 20 13:49:11 2005 From: op73418 at mail.telepac.pt (=?ISO-8859-1?Q?Gon=E7alo_Rodrigues?=) Date: Thu Jan 20 13:45:53 2005 Subject: [Tutor] Clash of the Titans and Mundane Matters In-Reply-To: <20050120054744.GA26642@titan.spiretech.com> References: <20050120054744.GA26642@titan.spiretech.com> Message-ID: <41EFA8C7.2070102@mail.telepac.pt> Michael Powe wrote: > Clash of the Titans > >>From "Dive into Python": > > __init__ is called immediately after an instance of the class is > created. It would be tempting but incorrect to call this the > constructor of the class. It's tempting, because it looks like a > constructor (by convention, __init__ is the first method defined for > the class), acts like one (it's the first piece of code executed in a > newly created instance of the class), and even sounds like one ("init" > certainly suggests a constructor-ish nature). Incorrect, because the > object has already been constructed by the time __init__ is called, > and you already have a valid reference to the new instance of the > class. But __init__ is the closest thing you're going to get to a > constructor in Python, and it fills much the same role. > >>From Alan's book "Learning to Program": > > One of the methods of this class is called __init__ and it is a > special method called a constructor. The reason for the name is that > it is called when a new object instance is created or constructed. Any > variables assigned (and hence created in Python) inside this method > will be unique to the new instance. There are a number of special > methods like this in Python, nearly all distinguished by the __xxx__ > naming format. > When thinking about __init__ think not "constructor" but "initializer" (that's where the name comes from after all...). By definition, an initializer initializes something already existing, already constructed. To *construct* a new instance one implements __new__ (python >= 2.2. See the docs). [text snipped] > 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? > Because functions and methods are different objects for conceptually different things? The way I tend to think of it, is that methods are functions with a little bit of extra functionality (although in the current implementation of Python functions *are* methods, or more corectly, descriptors). For the second question, os.path would be a method of what class? Best regards, G. Rodrigues From bill.mill at gmail.com Thu Jan 20 14:01:38 2005 From: bill.mill at gmail.com (Bill Mill) Date: Thu Jan 20 14:01:40 2005 Subject: [Tutor] counting no of words In-Reply-To: References: Message-ID: <797fe3d405012005017bbd9bcd@mail.gmail.com> 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 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 > > > From kent37 at tds.net Thu Jan 20 14:40:41 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 20 14:40:45 2005 Subject: [Tutor] style question: when to "hide" variable, modules In-Reply-To: <41ED92AD.7070502@tds.net> References: <6280502.1105727707856.JavaMail.root@bigbird.psp.pas.earthlink.net> <41E86DC4.6070102@tds.net> <20050118212022.GD2111@localhost.localdomain> <41ED92AD.7070502@tds.net> Message-ID: <41EFB4D9.90704@tds.net> Kent Johnson wrote: > The reason that is given for using accessors is that it gives you a > layer of flexibility; if you want to change the representation of the > data, or make it a computed attribute, you can do that without impacting > clients. > > Python, instead, lets you change what attribute access means. The way to > do this is with 'properties'. This is kind of an advanced topic, here > are two references: http://www.python.org/2.2.1/descrintro.html#property > http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html#SECTION000340000000000000000 Here is a blog entry that clearly explains the difference between Java and Python on this point. It also has a great explanation of why Python IDEs are not as popular as Java IDEs. http://naeblis.cx/rtomayko/2005/01/20/getters-setters-fuxors Kent From python at jayloden.com Thu Jan 20 18:25:08 2005 From: python at jayloden.com (Jay Loden) Date: Thu Jan 20 18:27:41 2005 Subject: [Tutor] need help planning website updates Message-ID: <200501201225.08920.python@jayloden.com> 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 From kent37 at tds.net Thu Jan 20 18:45:46 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 20 18:45:59 2005 Subject: [Tutor] need help planning website updates In-Reply-To: <200501201225.08920.python@jayloden.com> References: <200501201225.08920.python@jayloden.com> Message-ID: <41EFEE4A.40309@tds.net> 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 > From python at jayloden.com Thu Jan 20 19:25:08 2005 From: python at jayloden.com (Jay Loden) Date: Thu Jan 20 19:27:34 2005 Subject: [Tutor] need help planning website updates In-Reply-To: <41EFEE4A.40309@tds.net> References: <200501201225.08920.python@jayloden.com> <41EFEE4A.40309@tds.net> Message-ID: <200501201325.08488.python@jayloden.com> 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 From zmerch at 30below.com Thu Jan 20 19:59:16 2005 From: zmerch at 30below.com (Roger Merchberger) Date: Thu Jan 20 19:59:08 2005 Subject: [Tutor] counting no of words In-Reply-To: <797fe3d405012005017bbd9bcd@mail.gmail.com> References: Message-ID: <5.1.0.14.2.20050120135442.0523af18@mail.30below.com> 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 zmerch@30below.com Hi! I am a .signature virus. Copy me into your .signature to join in! From cyresse at gmail.com Thu Jan 20 21:59:40 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 20 21:59:45 2005 Subject: [Tutor] counting no of words In-Reply-To: <5.1.0.14.2.20050120135442.0523af18@mail.30below.com> References: <797fe3d405012005017bbd9bcd@mail.gmail.com> <5.1.0.14.2.20050120135442.0523af18@mail.30below.com> Message-ID: 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 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 > zmerch@30below.com > > 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. From orion_val at 163.com Fri Jan 21 03:25:22 2005 From: orion_val at 163.com (=?gb2312?q?=C9=F2=BD=E0=D4=AA?=) Date: Fri Jan 21 03:25:34 2005 Subject: [Tutor] How to print on screen and to a file, meanwhile? Message-ID: <200501211025.22861.orion_val@163.com> 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 From cyresse at gmail.com Fri Jan 21 03:40:17 2005 From: cyresse at gmail.com (Liam Clarke) Date: Fri Jan 21 03:40:20 2005 Subject: [Tutor] Syntactical question / OT Lisp Message-ID: 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. From cyresse at gmail.com Fri Jan 21 03:46:19 2005 From: cyresse at gmail.com (Liam Clarke) Date: Fri Jan 21 03:46:22 2005 Subject: [Tutor] Ooer, OT Lisp Message-ID: 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. From kent37 at tds.net Fri Jan 21 04:04:47 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 04:04:53 2005 Subject: [Tutor] Syntactical question / OT Lisp In-Reply-To: References: Message-ID: <41F0714F.8050706@tds.net> 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 From kent37 at tds.net Fri Jan 21 04:53:33 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 04:53:38 2005 Subject: [Tutor] How to print on screen and to a file, meanwhile? In-Reply-To: <200501211025.22861.orion_val@163.com> References: <200501211025.22861.orion_val@163.com> Message-ID: <41F07CBD.3080703@tds.net> The logging module is good for this. You can set it up to log to the screen and a file. You have to do some setup and change your print statements to call a logger. See this section of the docs and the one following for examples. http://www.python.org/dev/doc/devel/lib/minimal-example.html It's easier than it looks, you just have to take a little time to figure out how to set it up the way you like it. Kent ??? wrote: > 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 > From bvande at po-box.mcgill.ca Fri Jan 21 05:00:54 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Fri Jan 21 05:01:29 2005 Subject: [Tutor] Ooer, OT Lisp In-Reply-To: References: Message-ID: <41F07E76.5050003@po-box.mcgill.ca> 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. > Regards, > > Liam Clarke Hi Liam, I've barely poked into Lisp or the book, but the Jargon File 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 From marilyn at deliberate.com Fri Jan 21 05:02:08 2005 From: marilyn at deliberate.com (Marilyn Davis) Date: Fri Jan 21 05:06:11 2005 Subject: [Tutor] sockets, files, threads In-Reply-To: Message-ID: Danny! I couldn't resist trying threading again, now that the mysterious single-threading behavior is gone. I didn't put any locks anywhere. And it runs like a champ. Like a super champ. Either I had to put back threading or I had to make a family of socket-readers, or lose some functionality in my test scheme. So I gave it a shot. And, I'm very happy. Thank you again. Marilyn p.s. I hope you're well. From bill.mill at gmail.com Fri Jan 21 05:08:51 2005 From: bill.mill at gmail.com (Bill Mill) Date: Fri Jan 21 05:08:54 2005 Subject: [Tutor] Ooer, OT Lisp In-Reply-To: References: Message-ID: <797fe3d40501202008166de58@mail.gmail.com> Liam, On Fri, 21 Jan 2005 15:46:19 +1300, Liam Clarke 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 From orion_val at 163.com Fri Jan 21 06:18:43 2005 From: orion_val at 163.com (=?utf-8?q?=E6=B2=88=E6=B4=81=E5=85=83?=) Date: Fri Jan 21 06:18:59 2005 Subject: [Tutor] How to print on screen and to a file, meanwhile? In-Reply-To: <41F07CBD.3080703@tds.net> References: <200501211025.22861.orion_val@163.com> <41F07CBD.3080703@tds.net> Message-ID: <200501211318.44057.orion_val@163.com> Thank you, Kent In fact, I followed the page you posted, but it seems not working at all, maybe due to version or so. But good hints, and I find it in Python Documentation 2.3.4. http://www.python.org/doc/2.3.4/lib/node301.html After some trials, I find the minimal ways to meet my need. Just define two handler, one through FileHandler and the other through StreamHandler. from logging import * import sys logger=getLogger('application') formatter=Formatter('%(asctime)s %(levelname)s %(message)s') handler1=FileHandler('application.log','w') handler1.setFormatter(formatter) handler2=StreamHandler(sys.stdout) handler2.setFormatter(formatter) logger.addHandler(handler1) logger.addHandler(handler2) logger.setLevel(DEBUG) logger.error('We have a problem.') logger.info('And some information.') Juan Shen ? 2005?1?21? ??? 11:53?Kent Johnson ??? > The logging module is good for this. You can set it up to log to the screen > and a file. You have to do some setup and change your print statements to > call a logger. > > See this section of the docs and the one following for examples. > http://www.python.org/dev/doc/devel/lib/minimal-example.html > > It's easier than it looks, you just have to take a little time to figure > out how to set it up the way you like it. > > Kent > > ??? wrote: > > 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 From dyoo at hkn.eecs.berkeley.edu Fri Jan 21 09:06:21 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 21 09:06:25 2005 Subject: [Tutor] Ooer, OT Lisp In-Reply-To: <797fe3d40501202008166de58@mail.gmail.com> Message-ID: On Thu, 20 Jan 2005, Bill Mill wrote: > 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. Hi Liam, Scheme is a recent dialect of Lisp that seems to be well-regarded. DrScheme is one of the very active implementations of Scheme: http://www.drscheme.org/ and has a comprehensive set of documentation: http://download.plt-scheme.org/doc/ > > 5) Are you able to point me towards a simplified explanation of how > > the 'syntaxless' language can write programmes? Brian mentioned one of my favorite books: "Structure and Interpretation of Computer Programs": http://mitpress.mit.edu/sicp/ If you want to do a crash course into how Lisp-languages work, I can't think of a faster way than to look at the first few pages of it. http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html pretty much shows the core of Lisp programs. There's even a set of video lectures from the SICP authors that's freely available: http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ The neat thing about Scheme is that, once you get beyond the syntax, it starts to feel a bit like Python. *grin* Perhaps that should be the other way around. Best of wishes to you! From guillermo.fernandez.castellanos at gmail.com Fri Jan 21 10:16:19 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Fri Jan 21 10:16:22 2005 Subject: [Tutor] Combination Message-ID: <7d7029e705012101165138c56@mail.gmail.com> Hi, I'm trying to take a list and find all the unique combinations of that list. I mean: if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find: (1,2,3) but then not (2,1,3), (3,1,2),... (1,2,4) (1,2,5) (2,3,5) (3,4,5) The thing is, I want to do it as a generic function, where I pass a list, and the length of the combination I want, For the pervious example, it would be: createComb([1,2,3,4,5],3) I have no idea how to do it in a generic way. For a given list and a length of 4 I did this: def createComb(positions): """Returns all possible combinations of position of nbUnit units""" uniq={} result=[] for p1 in positions: for p2 in positions: for p3 in positions: for p4 in positions: uniq[p1]=0 uniq[p2]=0 uniq[p3]=0 uniq[p4]=0 if len(uniq)==4: result.append([p1,p2,p3,p4]) uniq={} return result but is not very elegant... Any suggestion? Thanks, G From kent37 at tds.net Fri Jan 21 11:51:01 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 11:51:07 2005 Subject: [Tutor] Combination In-Reply-To: <7d7029e705012101165138c56@mail.gmail.com> References: <7d7029e705012101165138c56@mail.gmail.com> Message-ID: <41F0DE95.8000209@tds.net> This question comes up regularly on comp.lang.python. Search the archives for 'combinations' to find *many* discussions of how to do it. Also there are several recipes in the cookbook. http://groups-beta.google.com/group/comp.lang.python?hl=en&lr=&ie=UTF-8&c2coff=1 http://aspn.activestate.com/ASPN/Cookbook/Python Kent Guillermo Fernandez Castellanos wrote: > Hi, > > I'm trying to take a list and find all the unique combinations of that list. > > I mean: > if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find: > (1,2,3) but then not (2,1,3), (3,1,2),... > (1,2,4) > (1,2,5) > (2,3,5) > (3,4,5) > > The thing is, I want to do it as a generic function, where I pass a > list, and the length of the combination I want, > > For the pervious example, it would be: > createComb([1,2,3,4,5],3) > > I have no idea how to do it in a generic way. > For a given list and a length of 4 I did this: > def createComb(positions): > """Returns all possible combinations of position of nbUnit units""" > uniq={} > result=[] > for p1 in positions: > for p2 in positions: > for p3 in positions: > for p4 in positions: > uniq[p1]=0 > uniq[p2]=0 > uniq[p3]=0 > uniq[p4]=0 > if len(uniq)==4: > result.append([p1,p2,p3,p4]) > uniq={} > return result > > but is not very elegant... > > Any suggestion? > > Thanks, > > G > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Fri Jan 21 14:13:10 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 14:13:13 2005 Subject: [Tutor] Ooer, OT Lisp In-Reply-To: References: Message-ID: <41F0FFE6.4050807@tds.net> Liam Clarke 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. I've dabbled a bit with Lisp in school and I read most of the Wizard book (see other posts for references). Another good book for learning the basics is "The Little Schemer". Learning Lisp and reading the Wizard book will definitely broaden your perspective of programming languages. And using recursion will never be a problem again. My personal opinion and preference is for Python, but that shouldn't be a surprise :-) The rest of this post is my opinion as a Python bigot and Lisp newbie... Lisp is kind of minimalist. It gives you a few *extremely* powerful tools, a little bit of syntax to glue it together, and lets you build whatever you want on top of that. To me, it ended up seeming like an awful lot of work, like everything is built from scratch. Python has more syntax and less power. To me it has enough power to do what I want to do and I like the syntax. This blog entry says of Python, "It's like they stole Lisp, and give it sh*tty syntax!" and "perhaps Python was the subset of Lisp that was understandable by C++ programmers" To me it's not sh*tty syntax, it's usable syntax. And it is the subset of Lisp that I need to get real work done. I've read some of Paul Graham's stuff and I'm pretty sure a Lisp bigot would tell me I just haven't learned enough Lisp to appreciate it. I'm even willing to concede that he might be right :-) but Python works for me. This page gives a good taste of the debate... http://c2.com/cgi/wiki?SmugLispWeenie Kent > > 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 > From keridee at jayco.net Fri Jan 21 15:43:51 2005 From: keridee at jayco.net (Jacob S.) Date: Fri Jan 21 15:44:16 2005 Subject: [Tutor] Unexpected result from decimal References: Message-ID: <001201c4ffc7$aa479810$1b5328cf@JSLAPTOP> Okay, so how do I get decimal to set precision of *significant digits*? Why have a decimal.getcontext().prec if it doesn't provide a useful result? The number of digits in a number is irrelevant to that numbers value. It just doesn't make sense to me. I tried quantize the other day and it didn't work -- gave some sort of error, I don't remember what it was, and I can't get it to do it again today. So, I guess it works now and I can go back to wondering why getcontext().prec = a is not equal to quantize("0."+"0"*(a-1)+"1") Thanks, Jacob Comments to everyone's post below. >> >>> import decimal >> >>> decimal.getcontext().prec = 2 >> >>> a = decimal.Decimal(2) >> >>> b = decimal.Decimal(3) >> >>> 100*a/b >> Decimal("67") >> >>> print 100*a/b > > This prints "67". > >> try - >> >> a=decimal.Decimal(2.0) > > This will not work. You can't convert a float directly to a > decimal.Decimal > (I believe this is so that you are forced to understand that there are > precision issues involved). 'a = decimal.Decimal("2.0")' will do what you > meant, though. Decimal is a totally different type from integers and floats. It is not affected by float division problems. It is not my type, it is in the standard distribution of python 2.4 You have to represent floats as strings, because if you don't, then a float with lost precision is used instead of the exact precision. For example -- If I pass 1.1 to decimal.Decimal, it will receive 1.10000000001 or something like that so it can try to keep perfect precision on a float that started from the beginning without it. This is what decimal is trying to resolve in the first place! However, if I pass a string, strings aren't affected by binary floating point problems and they can be kept in perfect precision from the beginning. >> b = decimal.Decimal(3) >> print 100*a/b > > However, this will print out "67", just as the above did. The reason is > the > one that Tim outlined: precision isn't the number of digits after the > decimal place - when I was at school the latter was called "decimal > places" > and precision was "significant digits". > >> Jacob- one slight flaw/quirk in Python is if you want floating point >> computations you have to specify a floating point. > [...] >> Same as writing 100/3.0 as opposed to 100/3. Try it. > > Note that you can do 'from __future__ import division' and 100/3 will be > the > same as 100/3.0 (and 100//3 will give you 3). See above > =Tony.Meyer From kent37 at tds.net Fri Jan 21 16:03:50 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 16:03:54 2005 Subject: [Tutor] Unexpected result from decimal In-Reply-To: <001201c4ffc7$aa479810$1b5328cf@JSLAPTOP> References: <001201c4ffc7$aa479810$1b5328cf@JSLAPTOP> Message-ID: <41F119D6.9020909@tds.net> Jacob S. wrote: > Okay, so how do I get decimal to set precision of *significant digits*? That is exactly what it is doing. getcontext().prec sets how many digits are used to represent the mantissa of the number. Have you taken any physics classes? What is 1000/7 to two significant digits? It is 140, not 142.85, which has *5* significant digits. Changing prec has just this effect: >>> from decimal import * >>> getcontext().prec = 2 >>> one = Decimal(1) >>> seven = Decimal(7) >>> one/seven Decimal("0.14") >>> 1000 * one/seven Decimal("1.4E+2") >>> getcontext().prec = 20 >>> one/seven Decimal("0.14285714285714285714") >>> 1000 * one/seven Decimal("142.85714285714285714") > Why have a decimal.getcontext().prec if it doesn't provide a useful result? > The number of digits in a number is irrelevant to that numbers value. > It just doesn't make sense to me. It's just like choosing between float and double in Java or C - it sets the precision of the underlying representation. It is a tradeoff between accuracy, speed, and memory use. Kent From maxnoel_fr at yahoo.fr Fri Jan 21 17:33:15 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Fri Jan 21 17:33:21 2005 Subject: [Tutor] Importing multiple files as a single module? Message-ID: <268275B4-6BCA-11D9-8C0F-000393CBC88E@yahoo.fr> Hi everyone, Having learnt OOP with C++, and most of my OOP experience being with Java, I'm used to storing my classes in one file for each class. I find it tidier and easier to read/debug. However, when I try to do the same in Python, each file corresponds to a module with a single class in it. Is there a way to obtain the same result in Python as in Java? That is, I'd like to have the following on my hard drive: foo/ Bar.py Baz.py Where Bar.py and Baz.py each contain a single class (Bar and Baz). Then, from Python, I'd import the foo module (import foo) and then access the classes with foo.Bar and foo.Baz (instead of foo.Bar.Bar and foo.Baz.Baz, which is what I have now). Being able to use Bar in Baz.py would of course be a nice side effect. Any ideas? Or am I trying to do things in a very non-Pythonic way? If so, how am I supposed to organize my OOP code, especially given the absence of a "real" IDE for Python? Thanks for your attention, -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Fri Jan 21 17:53:35 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 21 17:53:29 2005 Subject: [Tutor] Importing multiple files as a single module? In-Reply-To: <268275B4-6BCA-11D9-8C0F-000393CBC88E@yahoo.fr> References: <268275B4-6BCA-11D9-8C0F-000393CBC88E@yahoo.fr> Message-ID: <41F1338F.5060901@tds.net> I think this will work: in foo/__init__.py put from Bar import Bar from Baz import Baz or whatever variations of this you like. Any names defined in the package __init__.py are available to other code as package attribute. Kent Max Noel wrote: > Hi everyone, > > Having learnt OOP with C++, and most of my OOP experience being with > Java, I'm used to storing my classes in one file for each class. I find > it tidier and easier to read/debug. > However, when I try to do the same in Python, each file corresponds > to a module with a single class in it. > > Is there a way to obtain the same result in Python as in Java? That > is, I'd like to have the following on my hard drive: > > foo/ > Bar.py > Baz.py > > Where Bar.py and Baz.py each contain a single class (Bar and Baz). > Then, from Python, I'd import the foo module (import foo) and then > access the classes with foo.Bar and foo.Baz (instead of foo.Bar.Bar and > foo.Baz.Baz, which is what I have now). Being able to use Bar in Baz.py > would of course be a nice side effect. > > Any ideas? Or am I trying to do things in a very non-Pythonic way? > If so, how am I supposed to organize my OOP code, especially given the > absence of a "real" IDE for Python? > > Thanks for your attention, > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting and > sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From ryan at acceleration.net Fri Jan 21 19:46:13 2005 From: ryan at acceleration.net (Ryan Davis) Date: Fri Jan 21 19:46:15 2005 Subject: [Tutor] Importing multiple files as a single module? In-Reply-To: <268275B4-6BCA-11D9-8C0F-000393CBC88E@yahoo.fr> Message-ID: <20050121184613.DC7711E4005@bag.python.org> I'm having the same problem, and am eager to hear the responses. As far as a "real" IDE, emacs works pretty well, and check out Komodo, the ActiveState IDE: http://www.activestate.com/Products/Komodo/ Thanks, Ryan -----Original Message----- From: tutor-bounces+ryan=acceleration.net@python.org [mailto:tutor-bounces+ryan=acceleration.net@python.org] On Behalf Of Max Noel Sent: Friday, January 21, 2005 11:33 AM To: Tutor Tutor Subject: [Tutor] Importing multiple files as a single module? Hi everyone, Having learnt OOP with C++, and most of my OOP experience being with Java, I'm used to storing my classes in one file for each class. I find it tidier and easier to read/debug. However, when I try to do the same in Python, each file corresponds to a module with a single class in it. Is there a way to obtain the same result in Python as in Java? That is, I'd like to have the following on my hard drive: foo/ Bar.py Baz.py Where Bar.py and Baz.py each contain a single class (Bar and Baz). Then, from Python, I'd import the foo module (import foo) and then access the classes with foo.Bar and foo.Baz (instead of foo.Bar.Bar and foo.Baz.Baz, which is what I have now). Being able to use Bar in Baz.py would of course be a nice side effect. Any ideas? Or am I trying to do things in a very non-Pythonic way? If so, how am I supposed to organize my OOP code, especially given the absence of a "real" IDE for Python? Thanks for your attention, -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From dyoo at hkn.eecs.berkeley.edu Fri Jan 21 19:54:38 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 21 19:54:41 2005 Subject: [Tutor] Importing multiple files as a single module? In-Reply-To: <41F1338F.5060901@tds.net> Message-ID: On Fri, 21 Jan 2005, Kent Johnson wrote: > I think this will work: > in foo/__init__.py put > from Bar import Bar > from Baz import Baz > > or whatever variations of this you like. > > Any names defined in the package __init__.py are available to other code > as package attribute. Hi Max, For more information on packages, we can take a look at: http://www.python.org/doc/tut/node8.html#SECTION008400000000000000000 Best of wishes! From dyoo at hkn.eecs.berkeley.edu Fri Jan 21 21:36:52 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 21 21:36:56 2005 Subject: [Tutor] Combination In-Reply-To: <7d7029e705012101165138c56@mail.gmail.com> Message-ID: On Fri, 21 Jan 2005, Guillermo Fernandez Castellanos wrote: > I'm trying to take a list and find all the unique combinations of that > list. > > I mean: > if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find: > (1,2,3) but then not (2,1,3), (3,1,2),... > (1,2,4) > (1,2,5) > (2,3,5) > (3,4,5) Hi Guillermo, There is a clean recursive way to define this. I'll try to sketch out how one can go about deriving the function you're thinking of. Let's say that we have a list L, [1, 2, 3] and we want to create all combinations of elements in that list. Let's call the function that does this "createComb()". How do we calculate createComb([1, 2, 3])? We can just start writing it out, but let's try a slightly different approach. Imagine for the moment that we can construct createComb([2, 3]): createComb([2, 3]) -> [[2, 3], [2], [3]] We know, just from doing things by hand, that createComb([1, 2, 3]) of the whole list will look like: createComb([1, 2, 3) -> [[1, 2, 3], [1, 2], [1, 3], [2, 3], [2], [3]] If we compare createComb([1, 2, 3]) and createComb([2, 3]), we might be able to see that they're actually very similar to each other. Does this make sense so far? Please feel free to ask questions about this. Good luck! From dyoo at hkn.eecs.berkeley.edu Fri Jan 21 21:44:54 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Fri Jan 21 21:44:57 2005 Subject: [Tutor] Combination In-Reply-To: Message-ID: On Fri, 21 Jan 2005, Danny Yoo wrote: > > I mean: > > if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find: > > (1,2,3) but then not (2,1,3), (3,1,2),... > > (1,2,4) > > (1,2,5) > > (2,3,5) > > (3,4,5) > > > There is a clean recursive way to define this. Hi Guillermo, Gaaa; I screwed up slightly. *grin* I just wanted to clarify: the function that I'm sketching out is not exactly the function that you're thinking of. I'm doing more of a "give me all the subsets of L" kind of thing. But if you can understand how to get all the subsets, you should be able to figure out how to get all the combinations of 'k' elements, because they're really similar problems. From alan.gauld at freenet.co.uk Fri Jan 21 23:25:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 21 23:24:51 2005 Subject: [Tutor] Syntactical question / OT Lisp References: Message-ID: <003a01c50008$0e3d8ba0$8f4a8651@xp> > 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? You could but it would be very bad practice and much better to pass the object reference into Sketch. def on_aBtn_mouseClick(self, event): parrot.Sketch(self) def Sketch(anObject): ....code ... configINfo = anObject.config Even better make the app object return the actual config bits that parrot needs via a method: def Sketch(anObject): ... code... myConfigItem1,mycOnfig2 = anObject.getSketchConfig() All of these options make both parrot and the foo object more easily reusable and much less liable to break when you make changes to the config data. Alan G (Back from a week's trainig camp...) From alan.gauld at freenet.co.uk Fri Jan 21 23:33:23 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 21 23:33:07 2005 Subject: [Tutor] Ooer, OT Lisp References: Message-ID: <003f01c50009$37a14a30$8f4a8651@xp> > 1) Anyone here familiar with both? Yes at least two of us - DAnny has used Lisp/Scheme. > 2) If so, which would you rate as more powerful? Lisp by a long long way. Its more mature and has every bell and whistle going. Of course its much much harder to become an expert in Lisp for the same reason. > 3) What's with all those parentheses? Read my page on Functional Programming. Basically every Lisp program statement is an expression, and like most complex expressions you need parens... Basically a Lisp program is just a whole heap of nested expressions! It doesn't really need them of course but its one of the things that makes Lisp very regular in a math sense, very pure in approach, and why the academics say its the only "beautiful" language. > 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.... Its very easy to use once you learn it. But its initially different to traditional programming languages (although since it was invented in the early 60s - late 50's even??? - it is ttraditional in itself!) > 5) Are you able to point me towards a simplified explanation of how > the 'syntaxless' language can write programmes? Try the How To Design Programs (htdp.org) and Structure & Interpretation of Computer Programs (sicp.org) web sites. Both are excellent books published by MIT for free on the web. Both use Scheme which is a close relative of Common Lisp. I strongly recommend you check them out your programming in general will improve a lot from reading either book. The first is easier for non maths folks, SICP is a software engineering classic textbook. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From johnp at milwaukielumber.com Fri Jan 21 23:59:31 2005 From: johnp at milwaukielumber.com (John Purser) Date: Fri Jan 21 23:59:39 2005 Subject: [Tutor] Ooer, OT Lisp In-Reply-To: <003f01c50009$37a14a30$8f4a8651@xp> Message-ID: <200501212259.j0LMxVjV019801@mail.morseintranet.com> Very interesting sites. Thank you. John Purser -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Alan Gauld Sent: Friday, January 21, 2005 14:33 To: Liam Clarke; Tutor Tutor Subject: Re: [Tutor] Ooer, OT Lisp > 1) Anyone here familiar with both? Yes at least two of us - DAnny has used Lisp/Scheme. > 2) If so, which would you rate as more powerful? Lisp by a long long way. Its more mature and has every bell and whistle going. Of course its much much harder to become an expert in Lisp for the same reason. > 3) What's with all those parentheses? Read my page on Functional Programming. Basically every Lisp program statement is an expression, and like most complex expressions you need parens... Basically a Lisp program is just a whole heap of nested expressions! It doesn't really need them of course but its one of the things that makes Lisp very regular in a math sense, very pure in approach, and why the academics say its the only "beautiful" language. > 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.... Its very easy to use once you learn it. But its initially different to traditional programming languages (although since it was invented in the early 60s - late 50's even??? - it is ttraditional in itself!) > 5) Are you able to point me towards a simplified explanation of how > the 'syntaxless' language can write programmes? Try the How To Design Programs (htdp.org) and Structure & Interpretation of Computer Programs (sicp.org) web sites. Both are excellent books published by MIT for free on the web. Both use Scheme which is a close relative of Common Lisp. I strongly recommend you check them out your programming in general will improve a lot from reading either book. The first is easier for non maths folks, SICP is a software engineering classic textbook. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From keridee at jayco.net Sat Jan 22 00:07:28 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 22 00:07:55 2005 Subject: [Tutor] Re: Fw: Please submit to tutor list: dictionary update prob References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP><41EF32AD.8080700@tds.net> Message-ID: <003801c5000e$1349c130$1b5328cf@JSLAPTOP> Just one question... Why are you off the list? I see no point. If you want to stop getting the mail, you can change the options of your list account online... That's the only reason I see... Let's see -- reasons 1. Cost -- No, it's free 2. Security -- If you were subscribed to it once, it's too late to worry about that (Though there is no reason to) a. There is an option (in Outlook Express at least) to make your name invisible to others when emailing b. You can make your name invisible on the subscriber's list on the website by using the options in your account page Anyone want to comment -- maybe a link to get him to the subscriber's option page? HTH, Jacob Schmidt From python at jayloden.com Sat Jan 22 05:17:23 2005 From: python at jayloden.com (Jay Loden) Date: Sat Jan 22 05:19:19 2005 Subject: [Tutor] glob or filter help Message-ID: <200501212317.23716.python@jayloden.com> I have the following code in my updates script (gets the five most recent updated files on my site) def get_fles(exts, upd_dir): '''return list of all the files matching any extensions in list exts''' fle_list = [] for each in exts: cmd = upd_dir + "*." + each ext_ls = glob.glob(cmd) fle_list = fle_list + ext_ls return filter(notlink, fle_list) I wanted to just get one list, of all the .htm and .exe files in my upd_dir. I was trying to make a far more elegant solution that what's above, that could generate a list through a filter. Is there a way to trim the code down to something that does ONE sort through the directory and picks up the .htm and .exe files? (note, it is not necessary for this to recurse through subdirectories in the upd_dir). I have cmd defined above because calling "glob.glob(upd_dir + "*." + each) returned the error "cannot concatenate string and list objects" - is this the only way around that, or is there a better way? Also in the above code, "notlink" is just a function that returns True if "islink()" returns true....there has to be a better way to use this with filter(), how can i make filter use "if islink()!=true" as its condition? The script is working now, (I know, I know, if it ain't broke, don't fix it...) but I want to be a better programmer so more elegant solutions are accepted gratefully. -Jay From jstro at swbell.net Sat Jan 22 05:22:55 2005 From: jstro at swbell.net (J. M. Strother) Date: Sat Jan 22 05:23:04 2005 Subject: [Tutor] read line x from a file Message-ID: <41F1D51F.20505@swbell.net> I have a text file containing 336 records. I can read and print out the whole file without any problem. What I want to do is read and print out one record only (chosen at random). So I need to get to record x, select it, and then print it (or store it in a variable). Can anyone tell me now to do this? I'm new to Python and programming, so sorry if this is very basic. Thanks. jon From singingxduck at gmail.com Sat Jan 22 05:38:34 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sat Jan 22 05:38:16 2005 Subject: [Tutor] read line x from a file In-Reply-To: <41F1D51F.20505@swbell.net> References: <41F1D51F.20505@swbell.net> Message-ID: <41F1D8CA.3030000@gmail.com> J. M. Strother wrote: > I have a text file containing 336 records. > I can read and print out the whole file without any problem. > What I want to do is read and print out one record only (chosen at > random). So I need to get to record x, select it, and then print it > (or store it in a variable). > Can anyone tell me now to do this? > > I'm new to Python and programming, so sorry if this is very basic. > Thanks. > > jon > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Well, one way to do this is to count newlines, assuming you know what newline character your system uses. For example (UNTESTED): >>> test = open("test.txt","r") ## open the file >>> newline = [] >>> tfile = test.read() >>> for i in range(len(tfile)): ## instead of 'for char in tfile' if tfile[i] == '\n': ## to make the index of each newline newline.append(i) ## easier to find >>> import random >>> newl = random.choice(newline) >>> print tfile[newl:tfile[newl:].index('\n')] ## prints tfile from the ## randomly selected newline index to the next newline index, or one line ## prints data here >>> test.close() Now, this might not work, depending on whether or not '\n' is treated as a single character in this case. If not, you have to do a whole lot of the same sort of slicing as i did on the print line in order to find each successive newline (with tfile.index("\n")). HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050121/0a6c8463/attachment.htm From python at jayloden.com Sat Jan 22 05:46:41 2005 From: python at jayloden.com (Jay Loden) Date: Sat Jan 22 05:48:57 2005 Subject: [Tutor] read line x from a file In-Reply-To: <41F1D51F.20505@swbell.net> References: <41F1D51F.20505@swbell.net> Message-ID: <200501212346.41349.python@jayloden.com> One simple solution is to do: fle = open(file) contents = file.readlines() file.close() print contents[x] #or store this in a variable, whatever -Jay On Friday 21 January 2005 11:22, J. M. Strother wrote: > I have a text file containing 336 records. > I can read and print out the whole file without any problem. > What I want to do is read and print out one record only (chosen at > random). So I need to get to record x, select it, and then print it (or > store it in a variable). > Can anyone tell me now to do this? > > I'm new to Python and programming, so sorry if this is very basic. Thanks. > > jon > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From singingxduck at gmail.com Sat Jan 22 05:52:32 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sat Jan 22 05:52:15 2005 Subject: [Tutor] read line x from a file In-Reply-To: <200501212346.41349.python@jayloden.com> References: <41F1D51F.20505@swbell.net> <200501212346.41349.python@jayloden.com> Message-ID: <41F1DC10.4070504@gmail.com> Jay Loden wrote: >One simple solution is to do: > >fle = open(file) >contents = file.readlines() >file.close() >print contents[x] #or store this in a variable, whatever > >-Jay > >On Friday 21 January 2005 11:22, J. M. Strother wrote: > > >>I have a text file containing 336 records. >>I can read and print out the whole file without any problem. >>What I want to do is read and print out one record only (chosen at >>random). So I need to get to record x, select it, and then print it (or >>store it in a variable). >>Can anyone tell me now to do this? >> >>I'm new to Python and programming, so sorry if this is very basic. Thanks. >> >>jon >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> >> >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > Whoops! Sorta took the hard way on that one. *readlines()* is by all means a much better way of doing this. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050121/a81e715c/attachment-0001.htm From javier at ruere.com.ar Sat Jan 22 08:10:29 2005 From: javier at ruere.com.ar (Javier Ruere) Date: Sat Jan 22 08:21:07 2005 Subject: [Tutor] Re: glob or filter help In-Reply-To: <200501212317.23716.python@jayloden.com> References: <200501212317.23716.python@jayloden.com> Message-ID: Jay Loden wrote: > I have the following code in my updates script (gets the five most recent > updated files on my site) > > def get_fles(exts, upd_dir): > '''return list of all the files matching any extensions in list exts''' > fle_list = [] > for each in exts: > cmd = upd_dir + "*." + each > ext_ls = glob.glob(cmd) > fle_list = fle_list + ext_ls > return filter(notlink, fle_list) > > I wanted to just get one list, of all the .htm and .exe files in my upd_dir. > I was trying to make a far more elegant solution that what's above, that > could generate a list through a filter. Is there a way to trim the code down > to something that does ONE sort through the directory and picks up the .htm > and .exe files? (note, it is not necessary for this to recurse through > subdirectories in the upd_dir). I have cmd defined above because calling > "glob.glob(upd_dir + "*." + each) returned the error "cannot concatenate > string and list objects" - is this the only way around that, or is there a > better way? How about: >>> import os >>> exts = ('gz', 'conf') >>> upd_dir = '.' >>> filter(lambda fn : fn.split('.')[-1] in exts, os.listdir(upd_dir)) Do you like this better. It doesn't use glob and is terse. > Also in the above code, "notlink" is just a function that returns True if > "islink()" returns true....there has to be a better way to use this with > filter(), how can i make filter use "if islink()!=true" as its condition? If the filter criteria is complex, it deserves it's own function: >>> import os >>> import os.path >>> def get_files(exts, upd_dir): ... def criteria(filename): ... return filename.split('.')[-1] in exts \ ... and not os.path.islink(filename) ... return filter(criteria, os.listdir(upd_dir)) ... >>> get_files(('gz', 'conf'), '.') ['dynfun.pdf.gz', 'twander-3.160.tar.gz', 'PyCon2004DocTestUnit.pdf.gz', 'arg23.txt.gz', '.fonts.conf'] Javier From kent37 at tds.net Sat Jan 22 14:53:01 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 22 14:53:05 2005 Subject: [Tutor] read line x from a file In-Reply-To: <200501212346.41349.python@jayloden.com> References: <41F1D51F.20505@swbell.net> <200501212346.41349.python@jayloden.com> Message-ID: <41F25ABD.20009@tds.net> Jay Loden wrote: > One simple solution is to do: > > fle = open(file) > contents = file.readlines() > file.close() > print contents[x] #or store this in a variable, whatever That is the simplest solution. If your file gets bigger and you don't want to read it all at once, you can use enumerate to iterate the lines and pick out the one you want: f = open(...) for i, line in enumerate(f): if i==targetLine: print line # or whatever break f.close() BTW try to avoid using 'file' as the name of a variable, it is a builtin function (or type, actually). Kent > > -Jay > > On Friday 21 January 2005 11:22, J. M. Strother wrote: > >>I have a text file containing 336 records. >>I can read and print out the whole file without any problem. >>What I want to do is read and print out one record only (chosen at >>random). So I need to get to record x, select it, and then print it (or >>store it in a variable). >>Can anyone tell me now to do this? >> >>I'm new to Python and programming, so sorry if this is very basic. Thanks. >> >>jon >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From maxnoel_fr at yahoo.fr Sat Jan 22 15:01:20 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sat Jan 22 15:01:23 2005 Subject: [Tutor] read line x from a file In-Reply-To: <41F25ABD.20009@tds.net> References: <41F1D51F.20505@swbell.net> <200501212346.41349.python@jayloden.com> <41F25ABD.20009@tds.net> Message-ID: <17A7F27A-6C7E-11D9-8C0F-000393CBC88E@yahoo.fr> On Jan 22, 2005, at 13:53, Kent Johnson wrote: > That is the simplest solution. If your file gets bigger and you don't > want to read it all at once, you can use enumerate to iterate the > lines and pick out the one you want: > > f = open(...) > for i, line in enumerate(f): > if i==targetLine: > print line # or whatever > break > f.close() Of course, to do that, you must know the number of lines in the file beforehand. If you're running a UNIX system (Linux, OS X, *BSD...), that's easily done by calling the command "wc -l " (use os.popen to do that). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Sat Jan 22 15:19:50 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 22 15:19:57 2005 Subject: [Tutor] Re: glob or filter help In-Reply-To: References: <200501212317.23716.python@jayloden.com> Message-ID: <41F26106.7050404@tds.net> Javier Ruere wrote: > Jay Loden wrote: > >>I have the following code in my updates script (gets the five most recent >>updated files on my site) >> >>def get_fles(exts, upd_dir): >> '''return list of all the files matching any extensions in list exts''' >> fle_list = [] >> for each in exts: >> cmd = upd_dir + "*." + each >> ext_ls = glob.glob(cmd) >> fle_list = fle_list + ext_ls >> return filter(notlink, fle_list) >> >>I wanted to just get one list, of all the .htm and .exe files in my upd_dir. >>I was trying to make a far more elegant solution that what's above, that >>could generate a list through a filter. Is there a way to trim the code down >>to something that does ONE sort through the directory and picks up the .htm >>and .exe files? (note, it is not necessary for this to recurse through >>subdirectories in the upd_dir). I have cmd defined above because calling >>"glob.glob(upd_dir + "*." + each) returned the error "cannot concatenate >>string and list objects" - is this the only way around that, or is there a >>better way? Breaking out the expression and assigning it to a variable shouldn't make any difference. Are you sure you didn't have something like this? glob.glob(upd_dir + "*." + exts) That would give the error message you cite. In general if you have a question about an error, please post the exact error message including the stack trace, it can be very helpful. > If the filter criteria is complex, it deserves it's own function: > > >>>>import os >>>>import os.path >>>>def get_files(exts, upd_dir): > > ... def criteria(filename): > ... return filename.split('.')[-1] in exts \ > ... and not os.path.islink(filename) > ... return filter(criteria, os.listdir(upd_dir)) > ... > >>>>get_files(('gz', 'conf'), '.') > > ['dynfun.pdf.gz', 'twander-3.160.tar.gz', 'PyCon2004DocTestUnit.pdf.gz', > 'arg23.txt.gz', '.fonts.conf'] Javier's solution is good. You could also make a regular expression to look for the desired extensions. Here is one way: import os, re def get_files(exts, upd_dir): extnMatch = re.compile('(%s)$' % '|'.join(map(re.escape, exts))) def criteria(filename): return extnMatch.search(filename) \ and not os.path.islink(filename) return filter(criteria, os.listdir(upd_dir)) print get_files(['.java', '.txt'], '.') I better pick apart the extnMatch line a bit...what it does is build a regular expression that matches any of the extensions if they occur at the end of the filename. >>> exts = ['.java', '.txt'] First I use map() to apply re.escape() to each extension. This escapes any characters in the extension that have special meaning in a regex; specifically the '.': >>> e1 = map(re.escape, exts) >>> e1 ['\\.java', '\\.txt'] I could also have used a list comprehension e1 = [ re.escape(ext) for ext in exts ] but for applying a function like this I usually use map() Next I join the individual extensions with '|'. In a regex this selects between alternatives. >>> e2 = '|'.join(e1) >>> e2 '\\.java|\\.txt' Finally I put the alternatives in parentheses to group them and add a '$' at the end meaning 'match the end of the string. >>> e3 = '(%s)$' % e2 >>> e3 '(\\.java|\\.txt)$' This solution is definitely harder to explain than Javier's :-) Kent From keridee at jayco.net Sat Jan 22 15:24:49 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 22 15:24:49 2005 Subject: [Tutor] glob or filter help References: <200501212317.23716.python@jayloden.com> Message-ID: <000601c5008e$29c9bc50$b55328cf@JSLAPTOP> Filter can be replaced with IMHO the more readable list comprehensions. I would try def get_fles(exts,upd_dir): "return list of all the files matching any extensions in list exts" fle_list = [] for each in exts: ext_ls = glob.glob("%s*.%s" % (upd_dir,each)) fle_list.extend(ext_ls) return [x for x in fle_list if not islink(x)] >I have the following code in my updates script (gets the five most recent > updated files on my site) > > def get_fles(exts, upd_dir): > '''return list of all the files matching any extensions in list exts''' > fle_list = [] > for each in exts: > cmd = upd_dir + "*." + each > ext_ls = glob.glob(cmd) > fle_list = fle_list + ext_ls > return filter(notlink, fle_list) > > I wanted to just get one list, of all the .htm and .exe files in my > upd_dir. > I was trying to make a far more elegant solution that what's above, that > could generate a list through a filter. Is there a way to trim the code > down > to something that does ONE sort through the directory and picks up the > .htm > and .exe files? (note, it is not necessary for this to recurse through > subdirectories in the upd_dir). I have cmd defined above because calling > "glob.glob(upd_dir + "*." + each) returned the error "cannot concatenate > string and list objects" - is this the only way around that, or is there a > better way? > > Also in the above code, "notlink" is just a function that returns True if > "islink()" returns true....there has to be a better way to use this with > filter(), how can i make filter use "if islink()!=true" as its condition? > > The script is working now, (I know, I know, if it ain't broke, don't fix > it...) but I want to be a better programmer so more elegant solutions are > accepted gratefully. > -Jay > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From javier at ruere.com.ar Sat Jan 22 17:25:10 2005 From: javier at ruere.com.ar (Javier Ruere) Date: Sat Jan 22 17:15:06 2005 Subject: [Tutor] Re: glob or filter help In-Reply-To: References: <200501212317.23716.python@jayloden.com> Message-ID: Jay Loden wrote: > Thanks! That's exactly the kind of feedback I was looking for. If it's not > too much trouble, do you think you could explain how lambda works, or just > point me towards a lambda explanation/tutorial that a new programmer can > understand? It seems to give you some great power but I really don't > understand how it works. Lambda defines anonymous functions. It has some restrictions like that the function must be expressed in one line and there can be no assigments. The syntax can be found in [1]. Though I personaly like this keyword, I must say that it can make code less readable so I tend to use it for only for extremely simple snippets. The example given in the previous mail is almost too big. But then again, one must try things out to learn so use it and find your own balance. Also the BDFL has said he is unhappy with lambda (and filter, reduce and map) so he may remove this keyword in the future (but not before Python3000)[2]. Javier [1] http://www.python.org/doc/2.4/ref/lambdas.html [2] Look in the user list if you want to learn more about this topics. From bds at waywood.co.uk Sat Jan 22 20:08:20 2005 From: bds at waywood.co.uk (Barnaby Scott) Date: Sat Jan 22 20:09:39 2005 Subject: [Tutor] Re: glob or filter help In-Reply-To: Message-ID: <000001c500b5$bcb9f030$7c00a8c0@frankbruno> For anyone who doesn't like lambda, how about import os def get_fles(exts, upd_dir): return [i for i in os.listdir(upd_dir) if i.split('.')[-1] in exts] > -----Original Message----- > From: tutor-bounces+bds=waywood.co.uk@python.org > [mailto:tutor-bounces+bds=waywood.co.uk@python.org]On > Behalf Of Javier > Ruere > Sent: 22 January 2005 16:25 > To: tutor@python.org > Subject: [Tutor] Re: glob or filter help > > > Jay Loden wrote: > > Thanks! That's exactly the kind of feedback I was looking for. If > it's not > > too much trouble, do you think you could explain how > lambda works, or > just > > point me towards a lambda explanation/tutorial that a new > programmer can > > understand? It seems to give you some great power but I > really don't > > understand how it works. > > Lambda defines anonymous functions. It has some > restrictions like that > the function must be expressed in one line and there can be no > assigments. The syntax can be found in [1]. > Though I personaly like this keyword, I must say that it > can make code > less readable so I tend to use it for only for extremely simple > snippets. The example given in the previous mail is almost > too big. But > then again, one must try things out to learn so use it and > find your own > balance. > Also the BDFL has said he is unhappy with lambda (and > filter, reduce > and map) so he may remove this keyword in the future (but not before > Python3000)[2]. > > Javier > > [1] http://www.python.org/doc/2.4/ref/lambdas.html > [2] Look in the user list if you want to learn more about > this topics. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From John.Gooch at echostar.com Sat Jan 22 22:17:09 2005 From: John.Gooch at echostar.com (Gooch, John) Date: Sat Jan 22 22:17:15 2005 Subject: [Tutor] Threaded Script Runs Fine in IDLE, Never Terminates from Windows Command Prompt Message-ID: <15A1FDA26DAD524DA7A7AF77313EBA8F07BC0294@riv-excha5.echostar.com> I have a threaded python ( Python 2.3.4 ) script that runs perfectly on Windows 2000 Server SP4 when it is executed from IDLE ( i.e. press 'F5' from the editor ), the threads do their work, halt, and the 'join' command picks them up. When I run the same script from windows command line ( cmd.exe ), it appears to work normally until the last thread is 'joined'. What happens at that point is it does not execute the any lines after the join command, and will stay in the position indefinitely. Do I need to alter the script to run from the command line, or perhaps add some parameters to have it terminate successfully? Once this issue is resolved, the script will run daily via a scheduling program we use in our company, and the exit code of the script tells the scheduler whether the script completed its task successfully or not. Here is the 'code block' of my script that clean up the threads via .join: for workerThread in ThreadGroup: #timeout will difference between ( start time + 4 hours ) and current time #unless current time is greater than that, in which case the #thread gets one second to join #For example, if the current time if 30 minutes less than the endTime #time, then the thread has a timeout to 'join' of 30 minutes #timeout = endTime - time.time() #if ( timeout < 0 ): # timeout = 1 if debug: print "Joining "+workerThread.getName()+".\n" workerThread.join() if debug: print "Thread "+workerThread.getName()+" successfully joined." I run it with 3 threads, from IDLE it will close all 3 and exit normally, from the command prompt it prints the "successfully joined" for the first two, but hangs after printing "Joining Thread 3". The command line syntax is "c:"\python23\python.exe test.py", I have also tried "c:\python23\pythonw.exe test.py", but that just adds an entry in Windows task manager and sits there forever also. Thank You, From kent37 at tds.net Sat Jan 22 22:23:16 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 22 22:23:22 2005 Subject: [Tutor] read line x from a file In-Reply-To: <17A7F27A-6C7E-11D9-8C0F-000393CBC88E@yahoo.fr> References: <41F1D51F.20505@swbell.net> <200501212346.41349.python@jayloden.com> <41F25ABD.20009@tds.net> <17A7F27A-6C7E-11D9-8C0F-000393CBC88E@yahoo.fr> Message-ID: <41F2C444.7060907@tds.net> Max Noel wrote: > > On Jan 22, 2005, at 13:53, Kent Johnson wrote: > >> That is the simplest solution. If your file gets bigger and you don't >> want to read it all at once, you can use enumerate to iterate the >> lines and pick out the one you want: >> >> f = open(...) >> for i, line in enumerate(f): >> if i==targetLine: >> print line # or whatever >> break >> f.close() > > > Of course, to do that, you must know the number of lines in the file > beforehand. If you're running a UNIX system (Linux, OS X, *BSD...), > that's easily done by calling the command "wc -l " (use > os.popen to do that). enumerate() takes an iterator as an argument. The above program will read one line of the file at a time; when the target line is reached it will be printed. The file will only be read up until the target line. Or maybe you mean that to select a target line you have to know how many lines are in the file? Yes, I suppose you do...the OP wasn't very specific about how the line is selected...in that case readlines() might be the simplest thing to do. PS random.choice() might be handy here. Kent > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting and > sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > From davholla2002 at yahoo.co.uk Sat Jan 22 22:58:01 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sat Jan 22 22:58:04 2005 Subject: [Tutor] Print record x in a file Message-ID: <20050122215801.8387.qmail@web25401.mail.ukl.yahoo.com> This will get a random record I hope you do not think the comments are patronising but you did say you are new so I did not want to give naked code. import random #the above gives the program the ability to get a #pseudo random number file = open('test.rantxt') listcontents = file.readlines() #gives you the file as a list of records or it did on #(assuming each line is a record) file.close() lenoflist = len(listcontents)-1 #find the length of the list and take one of because computers count from 0 x = random.randrange(0,lenoflist) print listcontents[x] ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From alipolatel at yahoo.com Sun Jan 23 00:47:42 2005 From: alipolatel at yahoo.com (Ali Polatel) Date: Sun Jan 23 00:47:46 2005 Subject: [Tutor] Python Scripting Message-ID: <20050122234742.33010.qmail@web61007.mail.yahoo.com> Hi all tutors, I want to ask you something that I am really curious about. Can I design web-pages with python or use py files to write html? if the answer is yes and if I upload some scripts to the web-site with* .py does someone have to ahve python interepreter in his computer to be able to view those pages? And is it possible to write a programme with python which will update a webpage by the commands it receives from a server ( I mean I am writing a bot for a chess server and I want it to be able to update a web-page about the last results of games etc.) Regards, Ali Polatel __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050122/ef04ee19/attachment.htm From dyoo at hkn.eecs.berkeley.edu Sun Jan 23 01:46:22 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 23 01:46:33 2005 Subject: [Tutor] read line x from a file In-Reply-To: <41F25ABD.20009@tds.net> Message-ID: On Sat, 22 Jan 2005, Kent Johnson wrote: > Jay Loden wrote: > > One simple solution is to do: > > > > fle = open(file) > > contents = file.readlines() > > file.close() > > print contents[x] #or store this in a variable, whatever > > That is the simplest solution. If your file gets bigger and you don't > want to read it all at once, you can use enumerate to iterate the lines > and pick out the one you want: > > f = open(...) > for i, line in enumerate(f): > if i==targetLine: > print line # or whatever > break > f.close() Hi everyone, Here's a cute variation for folks who feel comfortable about iterators: ### f = open(...) interestingLine = itertools.islice(f, targetLine, None).next() f.close() ### This uses the remarkable 'itertools' library to abstract away the explicit loop logic: http://www.python.org/doc/lib/itertools-functions.html For example: ### >>> f = open('/usr/share/dict/words') >>> print repr(itertools.islice(f, 31415, None).next()) 'cataplasm\n' >>> f.close() ### This shows us that /usr/share/dict/word's 31416th word is 'cataplasm', which we can double-check from the Unix shell here: ### [dyoo@shoebox dyoo]$ head -n 31416 /usr/share/dict/words | tail -n 1 cataplasm ### Best of wishes! From dyoo at hkn.eecs.berkeley.edu Sun Jan 23 02:24:50 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 23 02:24:54 2005 Subject: [Tutor] Python Scripting In-Reply-To: <20050122234742.33010.qmail@web61007.mail.yahoo.com> Message-ID: On Sat, 22 Jan 2005, Ali Polatel wrote: > I want to ask you something that I am really curious about. > Can I design web-pages with python or use py files to write html? Hi Ali, Almost every programming language allows us to write strings into files, so from an academic standpoint, 'yes'. *grin* >From a practical one, it also looks like 'yes', because there's quite a few good libraries to do this kind of HTML writing. Python.org has a catalog of these "HTML Preprocessors" here: http://www.python.org/moin/WebProgramming > if the answer is yes and if I upload some scripts to the web-site with* .py > does someone have to ahve python interepreter in his computer to be > able to view those pages? Since the WWW provides communication between clients and servers, programmers have developed two main strategies for playing with the WWW. o. Client-side programming: this means that the server will send over Python programs; clients then are responsible for running those programs. This means that the clients have to have Python installed. This also means that the server doesn't necessarily have to have Python installed. Less work on the server, and more work on the clients. o. Server-side programming: this means that the server will run Python programs on the server side, and send the program's printed output to the client. This means that the clients don't need to have Python installed. More work on the server, and less work on the clients. A lot of systems these days are opting toward server-side programming because it's more transparently easy for clients to use. CGI programming is server-side. In fact, as long as the server can run Python, the clients don't even have to know that it's Python that's generating a web page. As a concrete example: people who visit a url like: http://maps.yahoo.com/ may not even realize that (at least from April 2001) the maps.yahoo.com web site was being run on Python programs. (http://mail.python.org/pipermail/tutor/2001-April/004761.html) > And is it possible to write a programme with python which will update a > webpage by the commands it receives from a server ( I mean I am writing > a bot for a chess server and I want it to be able to update a web-page > about the last results of games etc.) Yes. In fact, you probably don't need to worry about CGI at all for your particular project. You can just write a program that writes out a static HTML page. If you run that program every so often, the generated page will appear to be updating periodically. For example, something like: ### import datetime f = open("last_time.html", "w") f.write("""

The last time the program was called was at %s """ % datetime.datetime.today()) f.close() ### will write out a web page 'last_time.html' whose content should change every time we run the program. So what you know already should be enough to write a program to track server statistics: your chess bot can periodically write updates as an HTML file. If you want a program to run every time a person visits a web page --- to generate a page on demand --- then that dynamic approach requires a little more work, and that's where web programming systems like CGI comes into play. The topic guide here: http://www.python.org/topics/web/ should have some tutorials that you can look at. If you have more questions, please feel free to ask! From keridee at jayco.net Sun Jan 23 02:07:46 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 23 02:45:47 2005 Subject: [Tutor] Print record x in a file References: <20050122215801.8387.qmail@web25401.mail.ukl.yahoo.com> Message-ID: <002401c500e7$f9db6e80$415328cf@JSLAPTOP> > This will get a random record > I hope you do not think the comments are patronising > but you did say you are new so I did not want to give > naked code. > > import random > #the above gives the program the ability to get a > #pseudo random number > file = open('test.rantxt') > listcontents = file.readlines() > #gives you the file as a list of records or it did on > #(assuming each line is a record) > file.close() > lenoflist = len(listcontents)-1 > #find the length of the list and take one of because > computers count from 0 Yes, but len returns counting from 1. Anyway, you would have to add one to correct that anyway, wouldn't you? If randrange is start <= x *<=* end, then you don't have to add one, you just use the length. If randrange is start<= x < end like __builtin__ range, you have to put randrange(1,lenoflist+1) > x = random.randrange(0,lenoflist) I would use randint because list indices need to be integers -- unless of course I mistaken and randrange returns an integer also. (But that would be repetitive to have to functions do the same thing) > print listcontents[x] HTH, Jacob From keridee at jayco.net Sun Jan 23 03:01:14 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 23 03:01:05 2005 Subject: [Tutor] flattening a list References: <41E836AE.10300@gmail.com> Message-ID: <003601c500ef$728e2870$415328cf@JSLAPTOP> Ahh, my pitiful form of flattening a list that cheats... def flatten(li): li = str(li) li = li.replace("[","") li = li.replace("]","") li = li.replace("(","") li = li.replace(")","") li = "[%s]"%li return eval(li) It works! It's probably just a bit slower. Jacob Schmidt From singingxduck at gmail.com Sun Jan 23 06:21:19 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 23 06:21:51 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <002401c500e7$f9db6e80$415328cf@JSLAPTOP> References: <20050122215801.8387.qmail@web25401.mail.ukl.yahoo.com> <002401c500e7$f9db6e80$415328cf@JSLAPTOP> Message-ID: <41F3344F.8030209@gmail.com> Jacob S. wrote: >> This will get a random record >> I hope you do not think the comments are patronising >> but you did say you are new so I did not want to give >> naked code. >> >> import random >> #the above gives the program the ability to get a >> #pseudo random number >> file = open('test.rantxt') >> listcontents = file.readlines() >> #gives you the file as a list of records or it did on >> #(assuming each line is a record) >> file.close() >> lenoflist = len(listcontents)-1 >> #find the length of the list and take one of because >> computers count from 0 > > > Yes, but len returns counting from 1. > Anyway, you would have to add one to correct that anyway, wouldn't you? > If randrange is start <= x *<=* end, then you don't have to add one, you > just use the length. > If randrange is start<= x < end like __builtin__ range, you have to put > randrange(1,lenoflist+1) > >> x = random.randrange(0,lenoflist) > > > I would use randint because list indices need to be integers -- unless of > course I mistaken and > randrange returns an integer also. (But that would be repetitive to > have to > functions do the same thing) > >> print listcontents[x] > > > HTH, > Jacob > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > How about random.choice() ? Ie: >>> print random.choice(f.readlines()) -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From singingxduck at gmail.com Sun Jan 23 06:26:29 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 23 06:26:51 2005 Subject: [Tutor] flattening a list In-Reply-To: <003601c500ef$728e2870$415328cf@JSLAPTOP> References: <41E836AE.10300@gmail.com> <003601c500ef$728e2870$415328cf@JSLAPTOP> Message-ID: <41F33585.1010302@gmail.com> Jacob S. wrote: > Ahh, my pitiful form of flattening a list that cheats... > > def flatten(li): > li = str(li) > li = li.replace("[","") > li = li.replace("]","") > li = li.replace("(","") > li = li.replace(")","") > li = "[%s]"%li > return eval(li) > > It works! It's probably just a bit slower. > > Jacob Schmidt > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Actually, this doesn't even work 100% of the time. If you have a list as the key or value in a dictionary, it will remove the brackets, at which point eval will fail since the dictionary will look like {7:8,9,10} (assuming an original dictionary of {7:[8,9,10]}), which results in a SyntaxError -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From kent37 at tds.net Sun Jan 23 13:46:08 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 23 13:46:11 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <002401c500e7$f9db6e80$415328cf@JSLAPTOP> References: <20050122215801.8387.qmail@web25401.mail.ukl.yahoo.com> <002401c500e7$f9db6e80$415328cf@JSLAPTOP> Message-ID: <41F39C90.60602@tds.net> Jacob S. wrote: >> import random >> #the above gives the program the ability to get a >> #pseudo random number >> file = open('test.rantxt') >> listcontents = file.readlines() >> #gives you the file as a list of records or it did on >> #(assuming each line is a record) >> file.close() >> lenoflist = len(listcontents)-1 >> #find the length of the list and take one of because >> computers count from 0 > > > Yes, but len returns counting from 1. > Anyway, you would have to add one to correct that anyway, wouldn't you? > If randrange is start <= x *<=* end, then you don't have to add one, you > just use the length. > If randrange is start<= x < end like __builtin__ range, you have to put > randrange(1,lenoflist+1) > >> x = random.randrange(0,lenoflist) > > > I would use randint because list indices need to be integers -- unless of > course I mistaken and > randrange returns an integer also. (But that would be repetitive to have to > functions do the same thing) A quick check of the module docs (Jacob, do you know where to find the docs?) gives randrange( [start,] stop[, step]) Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object. New in version 1.5.2. So the limits on randrange() are the same as for range() - it is start <= x < stop. And the returned value is an integer. Since len(listcontents) is one greater than the largest valid index of listcontents, the correct use of randrange() for this problem is x = random.randrange(0, len(listcontents)) > >> print listcontents[x] > > > HTH, > Jacob > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From python.programming at gmail.com Sun Jan 23 13:55:37 2005 From: python.programming at gmail.com (Kevin) Date: Sun Jan 23 13:55:39 2005 Subject: [Tutor] How would python messure up in performance? Message-ID: How well would a multi user texed based game created in just Python run if there were over 300 - 400 players (Just a hypathetical question) Would python be to slow to handle that amount of traffic? Thanks Kevin From maxnoel_fr at yahoo.fr Sun Jan 23 14:06:47 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 23 14:07:01 2005 Subject: [Tutor] How would python messure up in performance? In-Reply-To: References: Message-ID: On Jan 23, 2005, at 12:55, Kevin wrote: > How well would a multi user texed based game created in just Python > run if there were over 300 - 400 players (Just a hypathetical > question) Would python be to slow to handle that amount of traffic? It depends... What would be happening in said game? What would it involve, calculation-wise? What would you be running it on? In that kind of program, the core programming itself is rarely a bottleneck (IOW, Python should work just fine). The true elements on which your game's performance depend are: 1) Connection bandwidth and latency 2) Database access (especially if the DB is not on a remote server) 3) File I/O, if any Could you give us a few more specs? -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From davholla2002 at yahoo.co.uk Sun Jan 23 18:08:14 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sun Jan 23 18:08:17 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <002401c500e7$f9db6e80$415328cf@JSLAPTOP> Message-ID: <20050123170814.2083.qmail@web25402.mail.ukl.yahoo.com> --- "Jacob S." wrote: > > This will get a random record > > I hope you do not think the comments are > patronising > > but you did say you are new so I did not want to > give > > naked code. > > > > import random > > #the above gives the program the ability to get a > > #pseudo random number > > file = open('test.rantxt') > > listcontents = file.readlines() > > #gives you the file as a list of records or it did > on > > #(assuming each line is a record) > > file.close() > > lenoflist = len(listcontents)-1 > > #find the length of the list and take one of > because > > computers count from 0 > > Yes, but len returns counting from 1. > Anyway, you would have to add one to correct that > anyway, wouldn't you? As len(list) gives the length and when the elements of the list are number for element 0 onwards, the last element is number len(list) -1. Test yourself anyway. Randrange does gives an integer or it did when I tested this as otherwise it would have errored. I don't know if it always does, what do other people think ? However Kent's idea of random.choice(list) is better than my idea as it is one less line of code !! . If you have a length of 5 > If randrange is start <= x *<=* end, then you don't > have to add one, you > just use the length. > If randrange is start<= x < end like __builtin__ > range, you have to put > randrange(1,lenoflist+1) > > > x = random.randrange(0,lenoflist) > > I would use randint because list indices need to be > integers -- unless of > course I mistaken and > randrange returns an integer also. (But that would > be repetitive to have to > functions do the same thing) > > > print listcontents[x] > > HTH, > Jacob > > ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From alipolatel at yahoo.com Sun Jan 23 19:25:27 2005 From: alipolatel at yahoo.com (Ali Polatel) Date: Sun Jan 23 19:25:30 2005 Subject: [Tutor] on the way to find pi Message-ID: <20050123182528.18359.qmail@web61007.mail.yahoo.com> dear friends , I found a code which calculates pi with an interesting algorithm the programme code is below: from sys import stdout def f((q,r,t,k)): n = (3*q+r) / t if (4*q+r) / t == n: return (10*q,10*(r-n*t),t,k,n) else: return (q*k, q*(4*k+2)+r*(2*k+1),t*(2*k+1),k+1) # Call pi(20) for first 20 digits, or pi() for all digits def pi(n=-1): printed_decimal = False r = f((1,0,1,1)) while n != 0: if len(r) == 5: stdout.write(str(r[4])) if not printed_decimal: stdout.write('.') printed_decimal = True n -= 1 r = f(r[:4]) #stdout.write('\n') if __name__ == '__main__': from sys import argv try: digit_count = long(argv[1]) except: digit_count=int(raw_input('How many digits? :')) pi(digit_count) This code gives the number in an unusual format like "3.1415'None'" it has a number part and a string part . I want to seperate these from easc other but I couldn't manage. I mean when I try to turn it into string format then try to use things like [:4] or like that they don't work.Any idea how to seperate this 'None' from the number and make it a real normal number on which I can do operations like +1 -1 or like that :) Regards --------------------------------- Do you Yahoo!? The all-new My Yahoo! – What will yours do? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050123/e378f7d8/attachment.htm From maxnoel_fr at yahoo.fr Sun Jan 23 19:50:47 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 23 19:51:07 2005 Subject: [Tutor] on the way to find pi In-Reply-To: <20050123182528.18359.qmail@web61007.mail.yahoo.com> References: <20050123182528.18359.qmail@web61007.mail.yahoo.com> Message-ID: > This code gives the number in an unusual format like "3.1415'None'" > it has a number part and a string part . I want to seperate these from > easc other but I couldn't manage. I mean when I try to turn it into > string format then try to use things like [:4] or like that they don't > work.Any idea how to seperate this 'None' from the number and make it > a real normal number on which I can do operations like +1 -1 or like > that :) > Regards Well, you should try to have a look at where the "None" is appended to the string representing the number, and use a type check to decide whether or not you're appending/adding/whatever. >>> type("abc") >>> type(123) >>> type(None) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From singingxduck at gmail.com Sun Jan 23 19:59:38 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 23 19:59:56 2005 Subject: [Tutor] on the way to find pi In-Reply-To: <20050123182528.18359.qmail@web61007.mail.yahoo.com> References: <20050123182528.18359.qmail@web61007.mail.yahoo.com> Message-ID: <41F3F41A.1050202@gmail.com> Ali Polatel wrote: > dear friends , > I found a code which calculates pi with an interesting algorithm the > programme code is below: > from sys import stdout > def f((q,r,t,k)): > n = (3*q+r) / t > if (4*q+r) / t == n: > return (10*q,10*(r-n*t),t,k,n) > else: > return (q*k, q*(4*k+2)+r*(2*k+1),t*(2*k+1),k+1) > # Call pi(20) for first 20 digits, or pi() for all digits > def pi(n=-1): > printed_decimal = False > r = f((1,0,1,1)) > while n != 0: > if len(r) == 5: > stdout.write(str(r[4])) > if not printed_decimal: > stdout.write('.') > printed_decimal = True > n -= 1 > r = f(r[:4]) > #stdout.write('\n') > if __name__ == '__main__': > from sys import argv > try: > digit_count = long(argv[1]) > except: > digit_count=int(raw_input('How many digits? :')) > pi(digit_count) > > This code gives the number in an unusual format like "3.1415'None'" it > has a number part and a string part . I want to seperate these from > easc other but I couldn't manage. I mean when I try to turn it into > string format then try to use things like [:4] or like that they don't > work.Any idea how to seperate this 'None' from the number and make it > a real normal number on which I can do operations like +1 -1 or like > that :) > Regards > > ------------------------------------------------------------------------ > Do you Yahoo!? > The all-new My Yahoo! - What will yours do? > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > The reason for this is that pi() doesn't return anything (hence None). What it does do is print the result (stdout.write). So you can either use it w/o print (just pi(5)) or modify the code so it returns pi all at once instead of printing it as it goes (this will make it seem like it is taking more time since it will have to calculate the whole thing before printing anything, instead of printing the digits one and 4 at a time (I've bolded the changes): def pi(*n=1000*): *# if you set n to -1, nothing will ever be printed because #the loops waits for pi to be calculated before printing anything* *from decimal import setcontext, getcontext, Decimal, ExtendedContext* *setcontext(ExtendedContext) # No DvisionByZero Error getcontext().prec = n+1 # always just enough, and this way you can # still perform operations on the results (if you set the precision to # Infinity, it gives you an OverflowError to perform operations) pil = []* printed_decimal = False r = f((1,0,1,1)) while n != 0: if len(r) == 5: *pil.append*(str(r[4])) if not printed_decimal: *pil.append*(('.')) printed_decimal = True n -= 1 r = f(r[:4]) *return Decimal(''.join(pil)) *With Decimal(), you get an accurate string representation of floating point numbers which treats the strings as floating point numbers: >>> pi(20)-3 Decimal("0.1415926535897932384") The only reason the original pi(...) didn't have to deal with this is that it wrote directly to stdout, without formatting the text as string, float, etc. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050123/d1de8329/attachment.html From wegster at mindcore.net Sun Jan 23 20:14:38 2005 From: wegster at mindcore.net (Scott W) Date: Sun Jan 23 20:14:44 2005 Subject: [Tutor] Changing (Unix) environment for python shell/popen() commands Message-ID: <41F3F79E.70200@mindcore.net> Hey all. I'm unfortunately stuck using python 1.5.2, primarily on Linux currently, and have done the usual searching (on list, active state, google), without luck. I've got to shell out from my python code to execute a command, but _must_ set the environment at the same time (or prior to execution). I say must, because in this case the obvious answer of modifying the command to be called or wrapping it with a shell script to set the environment won't work/isn't going to happen. I saw some comments about setting os.environ[], but didn't seem to be seeing this work in subsequent calls using popen2(). Does anyone have a working piece of code setting some env variable successfully prior to(or along with) calling popen2() (or friends). Also, is there any way to get the called programs return code via popen() (again, under python 1.5.2)? I know this has been asked often enough, but once again, it seems I'm left wanting for _good_ documentation under python (ie compared to man pages, DevStudio Win32 docs, Linux info, Java docs, etc), including the half dozen python books I own (great for some things, but no reference whatsoever to other things that I'd think would warrant some coverage).. Thanks, Scott From lumbricus at gmx.net Sun Jan 23 20:37:35 2005 From: lumbricus at gmx.net (=?ISO-8859-1?Q?=22J=F6rg_W=F6lke=22?=) Date: Sun Jan 23 20:37:37 2005 Subject: [Tutor] Changing (Unix) environment for python shell/popen() commands References: <41F3F79E.70200@mindcore.net> Message-ID: <16947.1106509055@www52.gmx.net> > Hey all. Hello! > I'm unfortunately stuck using python 1.5.2, primarily on Linux > currently, and have done the usual searching (on list, active state, > google), without luck. > > I've got to shell out from my python code to execute a command, but > _must_ set the environment at the same time (or prior to execution). I > say must, because in this case the obvious answer of modifying the > command to be called or wrapping it with a shell script to set the > environment won't work/isn't going to happen. > > I saw some comments about setting os.environ[], but > didn't seem to be seeing this work in subsequent calls using popen2(). What is the error? > Does anyone have a working piece of code setting some env variable > successfully prior to(or along with) calling popen2() (or friends). > Also, is there any way to get the called programs return code via > popen() (again, under python 1.5.2)? >>> os.environ['murx']="hello world" >>> os.system("echo $murx") >>> x=os.popen("echo $murx").readline() >>> x 'hello world\n' >>> os.system("man bash") > I know this has been asked often enough, but once again, it seems I'm > left wanting for _good_ documentation under python (ie compared to man > pages, DevStudio Win32 docs, Linux info, Java docs, etc), including the > half dozen python books I own (great for some things, but no reference > whatsoever to other things that I'd think would warrant some coverage).. > > Thanks, > > Scott > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realit?t. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realit?t zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) Sparen beginnt mit GMX DSL: http://www.gmx.net/de/go/dsl From dyoo at hkn.eecs.berkeley.edu Sun Jan 23 22:22:02 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 23 22:22:11 2005 Subject: [Tutor] Changing (Unix) environment for python shell/popen() commands In-Reply-To: <41F3F79E.70200@mindcore.net> Message-ID: On Sun, 23 Jan 2005, Scott W wrote: > I've got to shell out from my python code to execute a command, but > _must_ set the environment at the same time (or prior to execution). > > I saw some comments about setting os.environ[], but > didn't seem to be seeing this work in subsequent calls using popen2(). > > Does anyone have a working piece of code setting some env variable > successfully prior to(or along with) calling popen2() (or friends). Hi Scott, Hmm... That should have worked. We should be able to assign to the os.environ dictionary: as I understand it, a child process can inherit its parent's environment. Let me check this: ### volado:~ dyoo$ cat print_name_var.py import os print "I see", os.environ["NAME"] volado:~ dyoo$ volado:~ dyoo$ volado:~ dyoo$ volado:~ dyoo$ python print_name_var.py I see Traceback (most recent call last): File "print_name_var.py", line 2, in ? print "I see", os.environ["NAME"] File "/sw/lib/python2.3/UserDict.py", line 19, in __getitem__ def __getitem__(self, key): return self.data[key] KeyError: 'NAME' ### Ok, that's expected so far. Let me see what happens if we try to set to os.environ. ### volado:~ dyoo$ python Python 2.3.3 (#1, Aug 26 2004, 23:05:50) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ['name'] = 'Scott' >>> os.popen('python print_name_var.py').read() Traceback (most recent call last): File "print_name_var.py", line 2, in ? print "I see", os.environ["NAME"] File "/sw/lib/python2.3/UserDict.py", line 19, in __getitem__ def __getitem__(self, key): return self.data[key] KeyError: 'NAME' 'I see\n' ### On thing we have to be careful of is case-sensitivity: it does matter in the case of environmental variables. ### >>> os.environ['NAME'] = 'Scott' >>> os.popen('python print_name_var.py').read() 'I see Scott\n' ### And now it works. > Also, is there any way to get the called programs return code via > popen() (again, under python 1.5.2)? Hi Scott, Yes; although it's a little hard to find it, the Python 1.52 documentation here: http://www.python.org/doc/1.5.2p2/ does have some documentation on popen2: http://www.python.org/doc/1.5.2p2/lib/module-popen2.html You can use a popen2.Popen3 instance, which should give you access to program return values. If you have more questions, please feel free to ask. From davholla2002 at yahoo.co.uk Sun Jan 23 22:55:27 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sun Jan 23 22:55:32 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <20050123190001.399081E400F@bag.python.org> Message-ID: <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> --- tutor-request@python.org wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body > 'help' to > tutor-request@python.org > > You can reach the person managing the list at > tutor-owner@python.org > > When replying, please edit your Subject line so it > is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Print record x in a file (Kent Johnson) > 2. How would python messure up in performance? > (Kevin) > 3. Re: How would python messure up in > performance? (Max Noel) > 4. Re: Print record x in a file (David Holland) > 5. on the way to find pi (Ali Polatel) > 6. Re: on the way to find pi (Max Noel) > 7. Re: on the way to find pi (Orri Ganel) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 23 Jan 2005 07:46:08 -0500 > From: Kent Johnson > Subject: Re: [Tutor] Print record x in a file > Cc: tutor python > Message-ID: <41F39C90.60602@tds.net> > Content-Type: text/plain; charset=ISO-8859-1; > format=flowed > > Since len(listcontents) is one greater than the > largest valid index of listcontents, the correct > use of randrange() for this problem is > x = random.randrange(0, len(listcontents)) > Kent, I know that you know far more about python than me but is that right ? I created file with 4 records and this code did print them all randomly:- import random i = 0 while i < 10: file = open('filename') listcontents = file.readlines() file.close() lenoflist = len(listcontents)-1 x = random.randrange(0,lenoflist) print listcontents[x], i i = i +1 While although this code below does work many times nothing is printed out. import random i = 0 while i < 10: file = open('test.rantxt') listcontents = file.readlines() file.close() lenoflist = len(listcontents)#-1 x = random.randrange(0,lenoflist) print listcontents[x], i i = i +1 ___________________________________________________________ ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com From cyresse at gmail.com Sun Jan 23 23:08:32 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 23 23:08:34 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> References: <20050123190001.399081E400F@bag.python.org> <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> Message-ID: Don't you mean x=random.randint(0, lenoflist) ?? I'm assuming you want an integer. On Sun, 23 Jan 2005 21:55:27 +0000 (GMT), David Holland wrote: > --- tutor-request@python.org wrote: > > Send Tutor mailing list submissions to > > tutor@python.org > > > > To subscribe or unsubscribe via the World Wide Web, > > visit > > http://mail.python.org/mailman/listinfo/tutor > > or, via email, send a message with subject or body > > 'help' to > > tutor-request@python.org > > > > You can reach the person managing the list at > > tutor-owner@python.org > > > > When replying, please edit your Subject line so it > > is more specific > > than "Re: Contents of Tutor digest..." > > > > > > Today's Topics: > > > > 1. Re: Print record x in a file (Kent Johnson) > > 2. How would python messure up in performance? > > (Kevin) > > 3. Re: How would python messure up in > > performance? (Max Noel) > > 4. Re: Print record x in a file (David Holland) > > 5. on the way to find pi (Ali Polatel) > > 6. Re: on the way to find pi (Max Noel) > > 7. Re: on the way to find pi (Orri Ganel) > > > > > > > ---------------------------------------------------------------------- > > > > Message: 1 > > Date: Sun, 23 Jan 2005 07:46:08 -0500 > > From: Kent Johnson > > Subject: Re: [Tutor] Print record x in a file > > Cc: tutor python > > Message-ID: <41F39C90.60602@tds.net> > > Content-Type: text/plain; charset=ISO-8859-1; > > format=flowed > > > > Since len(listcontents) is one greater than the > > largest valid index of listcontents, the correct > > use of randrange() for this problem is > > x = random.randrange(0, len(listcontents)) > > > > > Kent, > > I know that you know far more about python than me but > is that right ? > I created file with 4 records and this code did print > them all randomly:- > > import random > i = 0 > while i < 10: > file = open('filename') > listcontents = file.readlines() > file.close() > lenoflist = len(listcontents)-1 > x = random.randrange(0,lenoflist) > print listcontents[x], i > i = i +1 > > While although this code below does work many times > nothing is printed out. > import random > i = 0 > while i < 10: > file = open('test.rantxt') > listcontents = file.readlines() > file.close() > lenoflist = len(listcontents)#-1 > x = random.randrange(0,lenoflist) > print listcontents[x], i > i = i +1 > > > ___________________________________________________________ > ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com > _______________________________________________ > 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. From maxnoel_fr at yahoo.fr Sun Jan 23 23:37:54 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 23 23:38:01 2005 Subject: [Tutor] Print record x in a file In-Reply-To: References: <20050123190001.399081E400F@bag.python.org> <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> Message-ID: <6C577410-6D8F-11D9-B6B6-000393CBC88E@yahoo.fr> On Jan 23, 2005, at 22:08, Liam Clarke wrote: > Don't you mean > > x=random.randint(0, lenoflist) ?? I'm assuming you want an integer. random.randrange() returns an item (which can be a float or whatever, but by default is an int) in the specified range. In that case, an int between 0 and lenoflist. The advantage over random.randint is that you can specify a step. Thus, random.randrange(0, 257, 2) will return an even number between 0 and 256 inclusive. >> While although this code below does work many times >> nothing is printed out. >> import random >> i = 0 >> while i < 10: >> file = open('test.rantxt') >> listcontents = file.readlines() >> file.close() >> lenoflist = len(listcontents)#-1 >> x = random.randrange(0,lenoflist) >> print listcontents[x], i >> i = i +1 Anyway, the problem with this function is that len returns a number of items, but Python, like most good programming languages (and mathematicians), counts starting from 0. Thus, the first element in a list is foo[0]. foo[len(foo)] will raise an IndexError. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From kent37 at tds.net Mon Jan 24 00:07:01 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 24 00:07:05 2005 Subject: [Tutor] Re: Print record x in a file In-Reply-To: <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> References: <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> Message-ID: <41F42E15.2060002@tds.net> David Holland wrote: > Kent, > > I know that you know far more about python than me but > is that right ? > I created file with 4 records and this code did print > them all randomly:- > > import random > i = 0 > while i < 10: > file = open('filename') > listcontents = file.readlines() > file.close() > lenoflist = len(listcontents)-1 > x = random.randrange(0,lenoflist) > print listcontents[x], i > i = i +1 > > > While although this code below does work many times > nothing is printed out. > import random > i = 0 > while i < 10: > file = open('test.rantxt') > listcontents = file.readlines() > file.close() > lenoflist = len(listcontents)#-1 > x = random.randrange(0,lenoflist) > print listcontents[x], i > i = i +1 It sounds like your file has a blank line at the end. What do you get if you print len(listcontents)? Is it 4 or 5? Where do you think 'nothing' is coming from? Try this: import random listcontents = [1,2,3,4] for i in range(10): lenoflist = len(listcontents)-1 x = random.randrange(0,lenoflist) print listcontents[x] I never get a 4... Kent From kent37 at tds.net Mon Jan 24 00:09:33 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 24 00:09:37 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <6C577410-6D8F-11D9-B6B6-000393CBC88E@yahoo.fr> References: <20050123190001.399081E400F@bag.python.org> <20050123215527.89210.qmail@web25401.mail.ukl.yahoo.com> <6C577410-6D8F-11D9-B6B6-000393CBC88E@yahoo.fr> Message-ID: <41F42EAD.9070208@tds.net> Max Noel wrote: > > On Jan 23, 2005, at 22:08, Liam Clarke wrote: > >> Don't you mean >> >> x=random.randint(0, lenoflist) ?? I'm assuming you want an integer. > > > random.randrange() returns an item (which can be a float or > whatever, but by default is an int) in the specified range. In that > case, an int between 0 and lenoflist. > The advantage over random.randint is that you can specify a step. Thus, > random.randrange(0, 257, 2) will return an even number between 0 and 256 > inclusive. Also the output of randint() is inclusive of both endpoints - with the above statement you will have 0 <= x <= lenoflist. So in that case you would want lenoflist = len(listcontents) - 1 Kent From alan.gauld at freenet.co.uk Mon Jan 24 00:17:04 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 24 00:16:42 2005 Subject: [Tutor] How would python messure up in performance? References: Message-ID: <003c01c501a1$a74c3900$36bd8651@xp> > How well would a multi user texed based game created in just Python > run if there were over 300 - 400 players (Just a hypathetical > question) Would python be to slow to handle that amount of traffic? That depends entirely on how it was designed and what kind of hardware you ran it on. If you had a big mainframe or similar you could run 30000 users and it wouldn't be a problem. If you write a simple application and run it on an old 66MHZ 486PC then 3 users might be an issue! We need a bit more info about what you have in mind I think. Alan G From python.programming at gmail.com Mon Jan 24 00:57:09 2005 From: python.programming at gmail.com (Kevin) Date: Mon Jan 24 00:57:12 2005 Subject: [Tutor] How would python messure up in performance? In-Reply-To: References: Message-ID: Well not sure how to answer that but I will try. For file I/O you would have a seperat file for each character that would store HP/MANA/MOVE points, store items that the character would have on. Areas would be loaded from there own files with all the creatures and items for that area in the same file. Spells would probably have there own seperate file to load from. As far as unning the game it would be run on a redhat linux server. On Sun, 23 Jan 2005 13:06:47 +0000, Max Noel wrote: > > On Jan 23, 2005, at 12:55, Kevin wrote: > > > How well would a multi user texed based game created in just Python > > run if there were over 300 - 400 players (Just a hypathetical > > question) Would python be to slow to handle that amount of traffic? > > It depends... What would be happening in said game? What would it > involve, calculation-wise? What would you be running it on? > > In that kind of program, the core programming itself is rarely a > bottleneck (IOW, Python should work just fine). The true elements on > which your game's performance depend are: > 1) Connection bandwidth and latency > 2) Database access (especially if the DB is not on a remote server) > 3) File I/O, if any > > Could you give us a few more specs? > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > From maxnoel_fr at yahoo.fr Mon Jan 24 01:04:25 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Mon Jan 24 01:04:41 2005 Subject: [Tutor] How would python messure up in performance? In-Reply-To: References: Message-ID: <8226ACB1-6D9B-11D9-B6B6-000393CBC88E@yahoo.fr> On Jan 23, 2005, at 23:57, Kevin wrote: > Well not sure how to answer that but I will try. For file I/O you > would have a seperat file for each character that would store > HP/MANA/MOVE points, store items that the character would have on. > Areas would be loaded from there own files with all the creatures and > items for that area in the same file. Spells would probably have there > own seperate file to load from. As far as unning the game it would be > run on a redhat linux server. That's a bizarre design choice (for the characters, I mean), this just screams Database, especially since you may find yourself with concurrent write accesses on some files (e.g. a player attacks another while the other is moving). Anyway. This looks like you'll be doing lots of file I/O, with a few calculations inbetween, right? Then unless your server is really shitty, Python should handle this just fine. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From python.programming at gmail.com Mon Jan 24 01:12:30 2005 From: python.programming at gmail.com (Kevin) Date: Mon Jan 24 01:12:33 2005 Subject: [Tutor] How would python messure up in performance? In-Reply-To: <8226ACB1-6D9B-11D9-B6B6-000393CBC88E@yahoo.fr> References: <8226ACB1-6D9B-11D9-B6B6-000393CBC88E@yahoo.fr> Message-ID: Thanks for the advise. I am no ware near ready to do this as i am just learning to use python. I just thought it would be interesting to see a mud made in python rather then always in C or C++. So for now I am only going to start out making small programs to learn with. So thanks for answering the question. On Mon, 24 Jan 2005 00:04:25 +0000, Max Noel wrote: > > On Jan 23, 2005, at 23:57, Kevin wrote: > > > Well not sure how to answer that but I will try. For file I/O you > > would have a seperat file for each character that would store > > HP/MANA/MOVE points, store items that the character would have on. > > Areas would be loaded from there own files with all the creatures and > > items for that area in the same file. Spells would probably have there > > own seperate file to load from. As far as unning the game it would be > > run on a redhat linux server. > > That's a bizarre design choice (for the characters, I mean), this just > screams Database, especially since you may find yourself with > concurrent write accesses on some files (e.g. a player attacks another > while the other is moving). > > Anyway. This looks like you'll be doing lots of file I/O, with a few > calculations inbetween, right? Then unless your server is really > shitty, Python should handle this just fine. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting > and sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > From guillermo.fernandez.castellanos at gmail.com Mon Jan 24 02:20:43 2005 From: guillermo.fernandez.castellanos at gmail.com (Guillermo Fernandez Castellanos) Date: Mon Jan 24 02:20:56 2005 Subject: [Tutor] Combination In-Reply-To: References: Message-ID: <7d7029e70501231720fb78a89@mail.gmail.com> Got it. import copy def rec(list,length): result=[] if len(list)<=length: return [list] for elem in list: temp=copy.deepcopy(list) temp.remove(elem) temp=rec(temp,length) for i in temp: if i not in result: result.append(i) return result >>> rec([1,2,3,4,5],3) [[3, 4, 5], [2, 4, 5], [2, 3, 5], [2, 3, 4], [1, 4, 5], [1, 3, 5], [1, 3, 4], [1, 2, 5], [1, 2, 4], [1, 2, 3]] The logic is: 1) If I take a list L of length l > n, the n-length subgroups will be the union of all the n-length subgroups of all the possible lists of length l-1 derived from L. 2) We keep unique copies of the results (don't wanna have several times [1,3,5] for instance). Will be easier when I'll install Python 2,4 and have the sets. 3) We stop the recursion when we have a list of th length we desire Well... of course, thanks to Kent I've finded a bunch of much better implementations. I still did mine for personal pleasure :-) Thanks to all, Guille On Fri, 21 Jan 2005 12:44:54 -0800 (PST), Danny Yoo wrote: > > > On Fri, 21 Jan 2005, Danny Yoo wrote: > > > > I mean: > > > if I enter (1,2,3,4,5) and I watn combinations of 3, I want to find: > > > (1,2,3) but then not (2,1,3), (3,1,2),... > > > (1,2,4) > > > (1,2,5) > > > (2,3,5) > > > (3,4,5) > > > > > > There is a clean recursive way to define this. > > Hi Guillermo, > > Gaaa; I screwed up slightly. *grin* > > I just wanted to clarify: the function that I'm sketching out is not > exactly the function that you're thinking of. I'm doing more of a "give > me all the subsets of L" kind of thing. > > But if you can understand how to get all the subsets, you should be able > to figure out how to get all the combinations of 'k' elements, because > they're really similar problems. > > From flaxeater at yahoo.com Mon Jan 24 08:41:10 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Mon Jan 24 08:41:13 2005 Subject: [Tutor] How would python messure up in performance? Message-ID: <20050124074110.1392.qmail@web54307.mail.yahoo.com> Kevin wrote: >Thanks for the advise. I am no ware near ready to do this as i am just > >learning to use python. I just thought it would be interesting to see > >a mud made in python rather then always in C or C++. So for now I am > >only going to start out making small programs to learn with. So thanks > >for answering the question. > > There are several efforts made in mud's in python on sourceforge. Check them out, you can read thier source. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From davholla2002 at yahoo.co.uk Mon Jan 24 16:14:47 2005 From: davholla2002 at yahoo.co.uk (David Holland) Date: Mon Jan 24 16:14:57 2005 Subject: [Tutor] Print record x in a file In-Reply-To: <20050123231647.ACD481E4008@bag.python.org> Message-ID: <20050124151447.71219.qmail@web25405.mail.ukl.yahoo.com> Kent, Yes you are right. I looked at your code and that makes sense now. Thanks for explaining that. --------------------------------- ALL-NEW Yahoo! Messenger - all new features - even more fun! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050124/69a42f6b/attachment.htm From tegmine at gmail.com Tue Jan 25 00:29:54 2005 From: tegmine at gmail.com (Luis N) Date: Tue Jan 25 00:29:58 2005 Subject: [Tutor] ascii encoding Message-ID: <77bfa81a05012415294913515f@mail.gmail.com> How would I best turn this string: '2005-01-24 00:00:00.0' into this string: '2005%2D01%2D24%2000%3A00%3A00%2E0' In order to call a URL. I've hunted through the standard library, but nothing seemed to jump out. Thank You. From maxnoel_fr at yahoo.fr Tue Jan 25 00:40:00 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Tue Jan 25 00:40:05 2005 Subject: [Tutor] ascii encoding In-Reply-To: <77bfa81a05012415294913515f@mail.gmail.com> References: <77bfa81a05012415294913515f@mail.gmail.com> Message-ID: <4397CD37-6E61-11D9-BD4B-000393CBC88E@yahoo.fr> On Jan 24, 2005, at 23:29, Luis N wrote: > How would I best turn this string: > > '2005-01-24 00:00:00.0' > > into this string: > > '2005%2D01%2D24%2000%3A00%3A00%2E0' > > In order to call a URL. > > I've hunted through the standard library, but nothing seemed to jump > out. The pathname2url in urllib seems to do what you want: >>> import urllib >>> urllib.pathname2url('2005-01-24 00:00:00.0') '2005-01-24%2000%3A00%3A00.0' And urllib.url2pathname does the opposite conversion. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From flaxeater at yahoo.com Tue Jan 25 00:46:10 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Tue Jan 25 00:46:13 2005 Subject: [Tutor] ascii encoding Message-ID: <20050124234610.31283.qmail@web54306.mail.yahoo.com> I got this from spyce http://spyce.sourceforge.net _url_ch = re.compile(r'[^A-Za-z0-9_.!~*()-]') # RFC 2396 section 2.3 def url_encode(o, **kwargs): '''Return URL-encoded string.''' return _url_ch.sub(lambda match: "%%%02X" % ord(match.group(0)), str(o)) It was just the first thing I found in google "python url encode" Good Luck Luis N wrote: >How would I best turn this string: > > > >'2005-01-24 00:00:00.0' > > > >into this string: > > > >'2005%2D01%2D24%2000%3A00%3A00%2E0' > > > >In order to call a URL. > > > >I've hunted through the standard library, but nothing seemed to jump out. > > > >Thank You. > >_______________________________________________ > >Tutor maillist - Tutor@python.org > >http://mail.python.org/mailman/listinfo/tutor > > > > > __________________________________ Do you Yahoo!? Meet the all-new My Yahoo! - Try it today! http://my.yahoo.com From kent37 at tds.net Tue Jan 25 00:51:03 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 25 00:51:09 2005 Subject: [Tutor] ascii encoding In-Reply-To: <77bfa81a05012415294913515f@mail.gmail.com> References: <77bfa81a05012415294913515f@mail.gmail.com> Message-ID: <41F589E7.9090402@tds.net> Luis N wrote: > How would I best turn this string: > > '2005-01-24 00:00:00.0' > > into this string: > > '2005%2D01%2D24%2000%3A00%3A00%2E0' > > In order to call a URL. urllib.quote_plus() is intended for this purpose though it doesn't have the result you ask for: >>> import urllib >>> s='2005-01-24 00:00:00.0' >>> urllib.quote_plus(s) '2005-01-24+00%3A00%3A00.0' Are you sure you need to escape the - and . ? You can get exactly what you want with a regular expression and the sub() method. One of the cool features of re.sub() is that the replace parameter can be a *function*. The function receives the match object and returns the replacement string. This makes it easy to define replacements that are programmatically derived from the original string: >>> import re >>> def hexify(match): ... return '%%%X' % ord(match.group(0)) ... hexify is the callback function, it will get a single character match and turn it into a hex string >>> lett_num = re.compile('[^a-zA-Z0-9]') lett_num is a regular expression that matches everything except letters and digits. >>> lett_num.sub(hexify, s) '2005%2D01%2D24%2000%3A00%3A00%2E0' Kent > > I've hunted through the standard library, but nothing seemed to jump out. > > Thank You. > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Tue Jan 25 01:10:27 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 25 01:10:32 2005 Subject: [Tutor] ascii encoding In-Reply-To: <41F589E7.9090402@tds.net> References: <77bfa81a05012415294913515f@mail.gmail.com> <41F589E7.9090402@tds.net> Message-ID: <41F58E73.2080803@tds.net> Kent Johnson wrote: > >>> import re > >>> def hexify(match): > ... return '%%%X' % ord(match.group(0)) Ah, should be '%%%02X' ... Kent From jeff at ccvcorp.com Tue Jan 25 02:16:14 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Tue Jan 25 02:10:16 2005 Subject: [Tutor] Re: glob or filter help In-Reply-To: <000001c500b5$bcb9f030$7c00a8c0@frankbruno> References: <000001c500b5$bcb9f030$7c00a8c0@frankbruno> Message-ID: <41F59DDE.6010809@ccvcorp.com> Barnaby Scott wrote: > For anyone who doesn't like lambda, how about > > import os > def get_fles(exts, upd_dir): > return [i for i in os.listdir(upd_dir) if i.split('.')[-1] in exts] Better would be: def get_fles(exts, upd_dir): return [fname for fname in os.listdir(upd_dir) if \ os.path.splitext(fname)[-1] in exts \ and not fname.islink() ] (This is split into three lines for readability, but can be entered as a single line. Also, the '\' line-continuation is optional, as Python will see that the list comp is still open and automatically continue the line...) Using os.path.splitext() to get the extension is safer and more portable than simply splitting on '.', and it's self-documenting too! (A month from now, you might need to think a moment to figure out why you're splitting on '.', but the very name os.path.splitext() tells you about the intent.) Note that there's a slight difference here, in that exts will need to list extensions *with* the leading '.' because splitext() will leave it on -- >>> os.path.splitext('filename.txt') ('filename', '.txt') >>> I've also added a second clause to ensure that returned files are not links, as per the rest of the O.P.'s spec. Jeff Shannon Technician/Programmer Credit International From erimendz at gmail.com Tue Jan 25 07:07:37 2005 From: erimendz at gmail.com (Eri Mendz) Date: Tue Jan 25 07:07:55 2005 Subject: [Tutor] Seek advise to code Re: Fw: Please submit to tutor list: dictionary update prob References: <005301c4fe97$9b21d8e0$2a5428cf@JSLAPTOP> <41EF32AD.8080700@tds.net> <41EF90A9.6080206@tds.net> Message-ID: Kent Johnson tds.net> writes: [snip] > You still aren't doing anything with newdic. The absence of 'newdic' in the > code > after 'read.close()' should be a clue > > I think you want to overwrite the saved dict, but with the new dict instead of > with a filename string... Hi Kent, First off, thanks again to you and Jacob and all. I have updated the simple address book program and showing it here. To me this program seems working to my expectation. but i need good advise from the knowledgeable people here how to improve the program, comment on the coding style, syntax and other things i obviously miss being a newbie. I know this is not much of a program sans orinality as others must have been done this a thousand times much better than mine. but anyway this is good exercise for me to improve my understanding of python. TIA. And sorry for this long boring code :-)). I hope the indentation stays intact. #!/usr/bin/env python ''' CHANGELOG Tue Jan 25 08:11:07 AST 2005 1. moved all globals under if __name__ == '__main__' Mon Jan 24 13:12:39 AST 2005 1. wrap few more common codes with function call Sun Jan 23 15:06:54 AST 2005 1. address book is complete(at least to me) :-)) ''' ## Functions ################################################################## def about_program(): try: print print '\t'*2, '%s - %s' % (_name_, _version_) print '\t'*2, 'A simple address book written in Python' print '\t'*2, 'Cebwrpg Pbyynobengvba bs: ' print '\t' *2, _authors_[0], ',', _authors_[1] print '\t'*2, 'Created:', _created_ print '\t'*2, 'Revised:', _revised_ raw_input('\nPress to continue...') clear_screen() except EOFError: print ## add_contact ## def add_contact(d, f): while True: name = add_name() email = add_email() d[name] = email print 'Add another contact? ' ans = ask_yes_no() if ans == 0: # no print 'Save to address book? ' get_ans = ask_yes_no() if get_ans == 1: # yes save check = if_file_exist(f) if check == True: read = open(f, 'rb') stored = cPickle.load(read) stored.update(d) print "'%s' added to address book!\n" % d[name] read.close() write_book(stored, filename) clear_screen() else: # file not exist write_book(data_holder, filename) break else: # no save d.clear() #clear dict cache clear_screen() break def add_name(): msg = _msg_ + ' (add): ' while True: try: name = raw_input(msg) if len(name) != 0: if len(name) <= 20: return name else: print 'Name too long: please limit to 20 characters' else: print 'Try again: blank not allowed!' except EOFError: # catch ^C-D keypress print def add_email(): msg = 'Enter email address: ' while True: try: email = raw_input(msg) if len(email) == 0: print 'Blank not allowed!' else: valid_format = r'\w[-.\w]*\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com$|\ edu$|net$|gov$|mil$|org$|int$|aero$|biz$|coop$|museum$|pro$|info$)' valid_email = re.compile(valid_format) if valid_email.match(email): return email else: print '%s is not a valid address: try again!' % email except EOFError: print def ask_yes_no(): ask = raw_input('Yes or No? (y|[N]) ') if ask.lower() in ['y', 'ye', 'yes', 'yep', 'ok']: return 1 # yes else: return 0 # no def if_file_exist(f): ''' test if file exists; returns boolean ''' return os.path.exists(os.path.join(os.path.expanduser('~'), f)) def write_book(d, f): write = open(f, 'wb') cPickle.dump(d, write) write.close() def update_dict(d, f): ''' update the saved dictionary file ''' read = open(f, 'rb') newdic = cPickle.load(read) newdic.update(d) read.close() def view_abook(d, f): check = if_file_exist(f) if check is True: display_contacts(data_holder, filename) raw_input('\nPress to continue...') clear_screen() else: print 'no contacts listed!' def display_contacts(d, f): read = open(f, 'rb') d = cPickle.load(read) print 'Total contact[s]: %d\n' % len(d) while 1: index = 0 for key in d: name = key while len(name) < 25: name += '.' # append to name index += 1 print index, name, d[key] break read.close() def find_contact(d, f): msg = _msg_ + ' (find): ' while 1: try: read = open(f, 'rb') d = cPickle.load(read) find = raw_input(msg) if d.has_key(find): print 'Found:%s\temail:%s' % (find, d[find]) else: print "'%s' not found" % find print 'Try again? (find)' ans = ask_yes_no() if ans == 0: #no read.close() clear_screen() break read.close() except EOFError: print def remove_contact(d, f): msg = _msg_ + ' (remove): ' while 1: try: read = open(f, 'rb') stored = cPickle.load(read) # assign new dict name remove = raw_input(msg) if stored.has_key(remove): print "Found '%s'" % remove print "Really remove '%s'?" % remove confirm = ask_yes_no() if confirm == 1: #yes del stored[remove] stored.update(d) # update changed dict read.close() print "'%s' is removed!" % remove write_book(stored, filename) else: read.close() else: print "'%s' not found" % remove print 'Try again? (remove)' ans = ask_yes_no() if ans == 0: read.close() clear_screen() break except EOFError: print def edit_contact(d, f): msg = _msg_ + ' (edit): ' while 1: try: read = open(f, 'rb') stored = cPickle.load(read) edit = raw_input(msg) if stored.has_key(edit): print "Found '%s'" % edit print "Really edit '%s'?" % edit confirm = ask_yes_no() if confirm == 1: # yes print "'%s' with email: %s will be edited" \ % (edit, stored.pop(edit)) name = add_name() email = add_email() stored[name] = email # add new name/email stored.update(d) read.close() write_book(stored, filename) else: # no, dont edit read.close() else: print "'%s' not found!" % edit print 'Try again? (edit)' ans = ask_yes_no() if ans == 0: # no read.close() clear_screen() break except EOFError: print def clear_screen(): os.system('clear') def ifaccessible(f): ''' test if file is accessible by user; returns boolean ''' return os.access(os.path.join(os.path.expanduser('~'), f), os.F_OK) def main(): while True: select = main_menu() while True: if select in [ '1', 'p']: about_program() break elif select in [ '2', 'a']: add_contact(data_holder, filename) break elif select in [ '3', 'v']: view_abook(data_holder, filename) break elif select in [ '4', 'f']: find_contact(data_holder, filename) break elif select in [ '5', 'e']: edit_contact(data_holder, filename) break elif select in [ '6', 'r']: remove_contact(data_holder, filename) break elif select in [ '7', 'q']: sys.exit('Goodbye') else: print "'%s' is invalid option!" % select break def main_menu(): '''Show menu options''' print """ MENU OPTIONS [1] About This [P]rogram [2] [A]dd Contact [3] [V]iew Address Book [4] [F]ind Contact [5] [E]dit Contact [6] [R]emove Contact [7] [Q]uit """ while 1: try: ask = '\nSelect an option: ' opt = raw_input(ask)[0] #if opt.isalpha() == True: # truth test redundant if opt.isalpha(): return opt.lower() return opt except (IndexError, EOFError): # catch , <^C-D> keys pass ## main program portion ## if __name__ == '__main__': import cPickle, os, sys, string, re #globals _name_ = 'Py_AddBook' _version_ = '1.00' _created_ = 'December 2, 2004' _revised_ = 'Sun Jan 23 15:06:54 AST 2005' _authors_ = ['Rev Zraqm ', 'Whfgva Fgenhor '] _msg_ = 'Enter contact name' home = '~' filename = os.path.join(os.path.expanduser(home), 'abook.dat') data_holder = {} main() From melnyk at gmail.com Tue Jan 25 09:51:07 2005 From: melnyk at gmail.com (Scott Melnyk) Date: Tue Jan 25 09:51:11 2005 Subject: [Tutor] sorting a 2 gb file Message-ID: Hello! I am wondering about the best way to handle sorting some data from some of my results. I have an file in the form shown at the end (please forgive any wrapparounds due to the width of the screen here- the lines starting with ENS end with the e-12 or what have you on same line.) What I would like is to generate an output file of any other ENSE000...e-4 (or whathaveyou) lines that appear in more than one place and for each of those the queries they appear related to. So if the first line ENSE00001098330.2|ENSG00000013573.6|ENST00000350437.2 assembly=N... etc appears as a result in any other query I would like it and the queries it appears as a result to (including the score if possible). My data set the below is taken from is over 2.4 gb so speed and memory considerations come into play. Are sets more effective than lists for this? To save space in the new file I really only need the name of the result up to the | and the score at the end for each. to simplify things, the score could be dropped, and I could check it out as needed later. As always all feedback is very appreciated. Thanks, Scott FILE: This is the number 1 query tested. Results for scoring against Query= hg17_chainMm5_chr17 range=chr1:2040-3330 5'pad=0 3'pad=0 are: ENSE00001098330.2|ENSG00000013573.6|ENST00000350437.2 assembly=N... 72 1e-12 ENSE00001160046.1|ENSG00000013573.6|ENST00000251758.3 assembly=N... 72 1e-12 ENSE00001404464.1|ENSG00000013573.6|ENST00000228264.4 assembly=N... 72 1e-12 ENSE00001160046.1|ENSG00000013573.6|ENST00000290818.3 assembly=N... 72 1e-12 ENSE00001343865.2|ENSG00000013573.6|ENST00000350437.2 assembly=N... 46 8e-05 ENSE00001160049.1|ENSG00000013573.6|ENST00000251758.3 assembly=N... 46 8e-05 ENSE00001343865.2|ENSG00000013573.6|ENST00000228264.4 assembly=N... 46 8e-05 ENSE00001160049.1|ENSG00000013573.6|ENST00000290818.3 assembly=N... 46 8e-05 This is the number 2 query tested. Results for scoring against Query= hg17_chainMm5_chr1 range=chr1:82719-95929 5'pad=0 3'pad=0 are: ENSE00001373792.1|ENSG00000175182.4|ENST00000310585.3 assembly=N... 80 6e-14 ENSE00001134144.2|ENSG00000160013.2|ENST00000307155.2 assembly=N... 78 2e-13 ENSE00001433065.1|ENSG00000185480.2|ENST00000358383.1 assembly=N... 78 2e-13 ENSE00001422761.1|ENSG00000183160.2|ENST00000360503.1 assembly=N... 74 4e-12 ENSE00001431410.1|ENSG00000139631.6|ENST00000308926.3 assembly=N... 74 4e-12 ENSE00001433065.1|ENSG00000185480.2|ENST00000358383.1 assembly=N... 72 1e-11 ENSE00001411753.1|ENSG00000126882.4|ENST00000358329.1 assembly=N... 72 1e-11 ENSE00001428167.1|ENSG00000110497.4|ENST00000314823.4 assembly=N... 72 1e-11 ENSE00001401130.1|ENSG00000160828.5|ENST00000359898.1 assembly=N... 72 1e-11 ENSE00001414900.1|ENSG00000176920.4|ENST00000356650.1 assembly=N... 72 1e-11 ENSE00001428167.1|ENSG00000110497.4|ENST00000314823.4 assembly=N... 72 1e-11 ENSE00001400942.1|ENSG00000138670.5|ENST00000356373.1 assembly=N... 72 1e-11 ENSE00001400116.1|ENSG00000120907.6|ENST00000356368.1 assembly=N... 70 6e-11 ENSE00001413546.1|ENSG00000184209.6|ENST00000344033.2 assembly=N... 70 6e-11 ENSE00001433572.1|ENSG00000124243.5|ENST00000355583.1 assembly=N... 70 6e-11 ENSE00001423154.1|ENSG00000125875.4|ENST00000354200.1 assembly=N... 70 6e-11 ENSE00001400109.1|ENSG00000183785.3|ENST00000339190.2 assembly=N... 70 6e-11 ENSE00001268950.4|ENSG00000084112.4|ENST00000303438.2 assembly=N... 68 2e-10 ENSE00001057279.1|ENSG00000161270.6|ENST00000292886.2 assembly=N... 68 2e-10 ENSE00001434317.1|ENSG00000171453.2|ENST00000304004.2 assembly=N... 68 2e-10 From bds at waywood.co.uk Tue Jan 25 11:02:07 2005 From: bds at waywood.co.uk (Barnaby Scott) Date: Tue Jan 25 11:03:43 2005 Subject: [Tutor] Code review Message-ID: <000001c502c4$ee8e0ea0$7c00a8c0@frankbruno> If anyone has the time to look through an entire script, I would would be very grateful for any comments, tips or suggestions on a wiki-engine script I am working on. http://www.waywood.co.uk/cgi-bin/monkeywiki.py (this will download rather than execute) It does work, but I have not been using Python very long, and am entirely self-taught in computer programming of any sort, so I have huge doubts about my 'style'. I am also aware that I probably don't 'think like a programmer' (being, in fact a furniture maker!) I did post a previous version of this about a year(?) ago, and received some very welcome suggestions, but I have changed it quite a lot since then. Also, please ignore the licensing stuff - I do intend to make the thing available like this when I am more confident about it, and I am just getting a bit ahead of myself: you guys are the first people who know it's there. Many thanks From alan.gauld at freenet.co.uk Tue Jan 25 14:09:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue Jan 25 14:08:50 2005 Subject: [Tutor] sorting a 2 gb file References: Message-ID: <003201c502df$0bc06d50$36bd8651@xp> > My data set the below is taken from is over 2.4 gb so speed and memory > considerations come into play. To be honest, if this were my problem, I'd proably dump all the data into a database and use SQL to extract what I needed. Thats a much more effective tool for this kind of thing. You can do it with Python, but I think we need more understanding of the problem. For example what the various fields represent, how much of a comparison (ie which fields, case sensitivity etc) leads to "equality" etc. Alan G. From johnp at milwaukielumber.com Tue Jan 25 16:21:46 2005 From: johnp at milwaukielumber.com (John Purser) Date: Tue Jan 25 16:21:56 2005 Subject: [Tutor] sorting a 2 gb file In-Reply-To: <003201c502df$0bc06d50$36bd8651@xp> Message-ID: <200501251521.j0PFLkjV031715@mail.morseintranet.com> I'll just "Me Too" on Alan's Advice. I had a similar sized project only it was binary data in an ISAM file instead of flat ASCII. I tried several "pure" python methods and all took forever. Finally I used Python to read-modify-input source data into a mysql database. Then I pulled the data out via python and wrote it to a new ISAM file. The whole thing took longer to code that way but boy it sure scaled MUCH better and was much quicker in the end. John Purser -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Alan Gauld Sent: Tuesday, January 25, 2005 05:09 To: Scott Melnyk; tutor@python.org Subject: Re: [Tutor] sorting a 2 gb file > My data set the below is taken from is over 2.4 gb so speed and memory > considerations come into play. To be honest, if this were my problem, I'd proably dump all the data into a database and use SQL to extract what I needed. Thats a much more effective tool for this kind of thing. You can do it with Python, but I think we need more understanding of the problem. For example what the various fields represent, how much of a comparison (ie which fields, case sensitivity etc) leads to "equality" etc. Alan G. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From fant at pobox.com Tue Jan 25 17:05:58 2005 From: fant at pobox.com (Andrew D. Fant) Date: Tue Jan 25 17:06:02 2005 Subject: [Tutor] sorting a 2 gb file In-Reply-To: <003201c502df$0bc06d50$36bd8651@xp> References: <003201c502df$0bc06d50$36bd8651@xp> Message-ID: <41F66E66.2010606@pobox.com> Alan Gauld wrote: >>My data set the below is taken from is over 2.4 gb so speed and > > memory > >>considerations come into play. > > > To be honest, if this were my problem, I'd proably dump all the data > into a database and use SQL to extract what I needed. Thats a much > more effective tool for this kind of thing. > > You can do it with Python, but I think we need more understanding > of the problem. For example what the various fields represent, how > much of a comparison (ie which fields, case sensitivity etc) leads > to "equality" etc. > And if the idea of setting up a full-blown SQL server for the problem seems like a lot of work, you might try prototyping the sort and solutions with sqlite, and only migrate to (full-fledged RDBMS of your choice) if the prototype works as you want it too and sqlite seems too slow for your needs. Andy From melnyk at gmail.com Tue Jan 25 17:26:52 2005 From: melnyk at gmail.com (Scott Melnyk) Date: Tue Jan 25 17:26:55 2005 Subject: [Tutor] sorting a 2 gb file- i shrunk it and turned it around Message-ID: Thanks for the thoughts so far. After posting I have been thinking about how to pare down the file (much of the info in the big file was not relevant to this question at hand). After the first couple of responses I was even more motivated to shrink the file so not have to set up a db. This test will be run only now and later to verify with another test set so the db set up seemed liked more work than might be worth it. I was able to reduce my file down about 160 mb in size by paring out every line not directly related to what I want by some simple regular expressions and a couple tests for inclusion. The format and what info is compared against what is different from my original examples as I believe this is more clear. my queries are named by the lines such as: ENSE00001387275.1|ENSG00000187908.1|ENST00000339871.1 ENSE is an exon ENSG is the gene ENST is a transcript They all have the above format, they differ in in numbers above following ENS[E,G orT]. Each query is for a different exon. For background each gene has many exons and there are different versions of which exons are in each gene in this dataset. These different collections are the transcripts ie ENST00000339871.1 in short a transcript is a version of a gene here transcript 1 may be formed of exons a,b and c transcript 2 may contain exons a,b,d the other lines (results) are of the format hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=... 44 0.001 hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 44 0.001 "hg17_chainMm5_chr7_random range=chr10:124355404-124355687" is the important part here from "5'pad" on is not important at this point What I am trying to do is now make a list of any of the results that appear in more than one transcript ########## FILE SAMPLE: This is the number 1 query tested. Results for scoring against Query= ENSE00001387275.1|ENSG00000187908.1|ENST00000339871.1 are: hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=... 44 0.001 hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 44 0.001 hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad... 44 0.001 hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad... 44 0.001 hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad... 44 0.001 hg17_chainMm5_chr7_random range=chr10:124355388-124355719 5'pad=... 44 0.001 .... This is the number 3 query tested. Results for scoring against Query= ENSE00001365999.1|ENSG00000187908.1|ENST00000339871.1 are: hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 60 2e-08 hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad... 60 2e-08 hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad... 60 2e-08 hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad... 60 2e-08 ############## I would like to generate a file that looks for any results (the hg17_etc line) that occur in more than transcript (from the query line ENSE00001365999.1|ENSG00000187908.1|ENST00000339871.1) so if hg17_chainMm5_chr7_random range=chr10:124355404-124355687 shows up again later in the file I want to know and want to record where it is used more than once, otherwise I will ignore it. I am think another reg expression to capture the transcript id followed by something that captures each of the results, and writes to another file anytime a result appears more than once, and ties the transcript ids to them somehow. Any suggestions? I agree if I had more time and was going to be doing more of this the DB is the way to go. -As an aside I have not looked into sqlite, I am hoping to avoid the db right now, I'd have to get the sys admin to give me permission to install something again etc etc. Where as I am hoping to get this together in a reasonably short script. However I will look at it later (it could be helpful for other things for me. Thanks again to all, Scott From jhomme at libcom.com Tue Jan 25 19:48:19 2005 From: jhomme at libcom.com (jhomme) Date: Tue Jan 25 19:50:11 2005 Subject: [Tutor] Import Site Failed Resolution Message-ID: Hi, For no reason I can think of, Python 2.4 on Windows 2000 is suddenly complaining about "Import site" failing. I am so new to python that I have no idea what this is all about. I have been running programs successfully until today. Now, no matter what program I run, this happens. Funny thing is, if I run Python from the Run dialog, I don't see this, but perhaps it already goes by before the Python window opens. I usually get a command prompt and do python file.py. In frustration, I uninstalled Python. How can I intellegently figure out what is going on here. I follow the directions Python gives about using -v, but I don't understand what all the other output is trying to tell me. I have not touched anything in any of the Python directories. Thanks for any and all help. Jim From kent37 at tds.net Tue Jan 25 20:19:41 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 25 20:19:45 2005 Subject: [Tutor] Import Site Failed Resolution In-Reply-To: References: Message-ID: <41F69BCD.4060309@tds.net> jhomme wrote: > Hi, > For no reason I can think of, Python 2.4 on Windows 2000 is suddenly complaining about "Import > site" failing. I am so new to python that I have no idea what this is all about. When Python starts up it automatically imports the 'site' module. You should have site.py, site.pyc and/or site.pyo in your Python2.4\Lib directory, that is the file it is trying to find. > I have been running > programs successfully until today. Now, no matter what program I run, this happens. Funny thing is, > if I run Python from the Run dialog, I don't see this, but perhaps it already goes by before the > Python window opens. I usually get a command prompt and do python file.py. In frustration, I > uninstalled Python. Did you re-install Python? Are you still having the problem? > How can I intellegently figure out what is going on here. I follow the > directions Python gives about using -v, but I don't understand what all the other output is trying > to tell me. You could post the output here, maybe someone else can figure out the problem. Kent > I have not touched anything in any of the Python directories. > > Thanks for any and all help. > > Jim > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From jhomme at libcom.com Tue Jan 25 20:19:53 2005 From: jhomme at libcom.com (jhomme) Date: Tue Jan 25 20:21:45 2005 Subject: [Tutor] Re: Import Site Failed Resolution Message-ID: Hi, Here is all the information I could get from the display of the output from this error. How do I figure out what is going on and fix the problem? This is on a Windows 2000 machine. graphic 910 C:\WINNT\system32\command.com C:\PYTHON>python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # c:\python24\lib\site.pyc matches c:\python24\lib\site.py import site # precompiled from c:\python24\lib\site.pyc import os # precompiled from os.pyc 'import site' failed; traceback: Traceback (most recent call last): File "c:\python24\lib\site.py", line 61, in ? import os File "c:\python24\lib\os.py", line 4, in ? - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. AttributeError: 'module' object has no attribute 'path' # c:\python24\lib\warnings.pyc matches c:\python24\lib\warnings.py import warnings # precompiled from c:\python24\lib\warnings.pyc # c:\python24\lib\types.pyc matches c:\python24\lib\types.py import types # precompiled from c:\python24\lib\types.pyc # c:\python24\lib\linecache.pyc matches c:\python24\lib\linecache.py import linecache # precompiled from c:\python24\lib\linecache.pyc import os # precompiled from os.pyc Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> Thanks. Jim From kent37 at tds.net Tue Jan 25 20:42:09 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 25 20:42:12 2005 Subject: [Tutor] Re: Import Site Failed Resolution In-Reply-To: References: Message-ID: <41F6A111.8010705@tds.net> OK, getting closer. import site is failing because site imports os and that import is failing. The error message points to a line that should be part of the docstring at the beginning of os.py...strange. Here are the first 22 lines from my os.py - the entire docstring. If your Python2.4\Lib\os.py doesn't look like this then you could try pasting in these lines instead, or maybe reinstalling to make sure nothing else is corrupted... **** Next line is start of os.py **** r"""OS routines for Mac, DOS, NT, or Posix depending on what system we're on. This exports: - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. - os.path is one of the modules posixpath, ntpath, or macpath - os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos' - os.curdir is a string representing the current directory ('.' or ':') - os.pardir is a string representing the parent directory ('..' or '::') - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') - os.extsep is the extension separator ('.' or '/') - os.altsep is the alternate pathname separator (None or '/') - os.pathsep is the component separator used in $PATH etc - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') - os.defpath is the default search path for executables - os.devnull is the file path of the null device ('/dev/null', etc.) Programs that import and use 'os' stand a better chance of being portable between different platforms. Of course, they must then only use functions that are defined by all platforms (e.g., unlink and opendir), and leave all pathname manipulation to os.path (e.g., split and join). """ **** End of snippet from os.py **** Kent jhomme wrote: > Hi, > Here is all the information I could get from the display of the output from this error. How do I figure out what is going on and fix the problem? This is on a Windows 2000 machine. > > graphic 910 C:\WINNT\system32\command.com > C:\PYTHON>python -v > # installing zipimport hook > import zipimport # builtin > # installed zipimport hook > # c:\python24\lib\site.pyc matches c:\python24\lib\site.py > import site # precompiled from c:\python24\lib\site.pyc > import os # precompiled from os.pyc > 'import site' failed; traceback: > Traceback (most recent call last): > File "c:\python24\lib\site.py", line 61, in ? > import os > File "c:\python24\lib\os.py", line 4, in ? > - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. > AttributeError: 'module' object has no attribute 'path' > # c:\python24\lib\warnings.pyc matches c:\python24\lib\warnings.py > import warnings # precompiled from c:\python24\lib\warnings.pyc > # c:\python24\lib\types.pyc matches c:\python24\lib\types.py > import types # precompiled from c:\python24\lib\types.pyc > # c:\python24\lib\linecache.pyc matches c:\python24\lib\linecache.py > import linecache # precompiled from c:\python24\lib\linecache.pyc > import os # precompiled from os.pyc > Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > > > Thanks. > > Jim > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Tue Jan 25 21:46:47 2005 From: kent37 at tds.net (Kent Johnson) Date: Tue Jan 25 21:46:50 2005 Subject: [Tutor] Re: Import Site Failed Resolution In-Reply-To: <08a1a2a024f858489855db9e385e5f7a@libcom.com> References: <08a1a2a024f858489855db9e385e5f7a@libcom.com> Message-ID: <41F6B037.9070006@tds.net> Ah. Do you have a file called os.py or os.pyc in your python files? Taking another look at your import trace, I see that site.pyc is found at c:\python24\lib\site.pyc but os.pyc is found in the current directory. Kent jhomme wrote: > Hi Kent, > my os.py looks like what you posted below. I re-installed after deleting the registry key and the > python24 folder. I also discovered that if I type python while pointing right at the python24 directory rather than the directory my python files are in, I don't get the error. > > Weird eh? > > Thanks. > > Jim > > > -----Original message----- > From: Kent Johnson kent37@tds.net > Date: Tue, 25 Jan 2005 14:42:09 -0500 > To: > Subject: Re: [Tutor] Re: Import Site Failed Resolution > > >>OK, getting closer. import site is failing because site imports os and that import is failing. The >>error message points to a line that should be part of the docstring at the beginning of os.py...strange. >> >>Here are the first 22 lines from my os.py - the entire docstring. If your Python2.4\Lib\os.py >>doesn't look like this then you could try pasting in these lines instead, or maybe reinstalling to >>make sure nothing else is corrupted... >> >>**** Next line is start of os.py **** >>r"""OS routines for Mac, DOS, NT, or Posix depending on what system we're on. >> >>This exports: >> - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. >> - os.path is one of the modules posixpath, ntpath, or macpath >> - os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos' >> - os.curdir is a string representing the current directory ('.' or ':') >> - os.pardir is a string representing the parent directory ('..' or '::') >> - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\') >> - os.extsep is the extension separator ('.' or '/') >> - os.altsep is the alternate pathname separator (None or '/') >> - os.pathsep is the component separator used in $PATH etc >> - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n') >> - os.defpath is the default search path for executables >> - os.devnull is the file path of the null device ('/dev/null', etc.) >> >>Programs that import and use 'os' stand a better chance of being >>portable between different platforms. Of course, they must then >>only use functions that are defined by all platforms (e.g., unlink >>and opendir), and leave all pathname manipulation to os.path >>(e.g., split and join). >>""" >>**** End of snippet from os.py **** >> >>Kent >> >> >> >>jhomme wrote: >> >>>Hi, >>>Here is all the information I could get from the display of the output from this error. How do I figure out what is going on and fix the problem? This is on a Windows 2000 machine. >>> >>>graphic 910 C:\WINNT\system32\command.com >>>C:\PYTHON>python -v >>># installing zipimport hook >>>import zipimport # builtin >>># installed zipimport hook >>># c:\python24\lib\site.pyc matches c:\python24\lib\site.py >>>import site # precompiled from c:\python24\lib\site.pyc >>>import os # precompiled from os.pyc >>>'import site' failed; traceback: >>>Traceback (most recent call last): >>>File "c:\python24\lib\site.py", line 61, in ? >>>import os >>>File "c:\python24\lib\os.py", line 4, in ? >>>- all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc. >>>AttributeError: 'module' object has no attribute 'path' >>># c:\python24\lib\warnings.pyc matches c:\python24\lib\warnings.py >>>import warnings # precompiled from c:\python24\lib\warnings.pyc >>># c:\python24\lib\types.pyc matches c:\python24\lib\types.py >>>import types # precompiled from c:\python24\lib\types.pyc >>># c:\python24\lib\linecache.pyc matches c:\python24\lib\linecache.py >>>import linecache # precompiled from c:\python24\lib\linecache.pyc >>>import os # precompiled from os.pyc >>>Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 >>>Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>>Thanks. >>> >>>Jim >>> >>>_______________________________________________ >>>Tutor maillist - Tutor@python.org >>>http://mail.python.org/mailman/listinfo/tutor >>> >> >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor > > > From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 00:40:53 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 00:40:57 2005 Subject: [Tutor] sorting a 2 gb file In-Reply-To: Message-ID: On Tue, 25 Jan 2005, Scott Melnyk wrote: > I have an file in the form shown at the end (please forgive any > wrapparounds due to the width of the screen here- the lines starting > with ENS end with the e-12 or what have you on same line.) > > What I would like is to generate an output file of any other > ENSE000...e-4 (or whathaveyou) lines that appear in more than one > place and for each of those the queries they appear related to. Hi Scott, One way to do this might be to do it in two passes across the file. The first pass through the file can identify records that appear more than once. The second pass can take that knowledge, and then display those records. In pseudocode, this will look something like: ### hints = identifyDuplicateRecords(filename) displayDuplicateRecords(filename, hints) ### > My data set the below is taken from is over 2.4 gb so speed and memory > considerations come into play. > > Are sets more effective than lists for this? Sets or dictionaries make the act of "lookup" of a key fairly cheap. In the two-pass approach, the first pass can use a dictionary to accumulate the number of times a certain record's key has occurred. Note that, because your file is so large, the dictionary probably shouldn't accumulation the whole mass of information that we've seen so far: instead, it's sufficient to record the information we need to recognize a duplicate. If you have more questions, please feel free to ask! From maxnoel_fr at yahoo.fr Wed Jan 26 00:44:07 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 26 00:44:24 2005 Subject: [Tutor] sorting a 2 gb file In-Reply-To: References: Message-ID: <00F7AAD2-6F2B-11D9-AAEE-000393CBC88E@yahoo.fr> On Jan 25, 2005, at 23:40, Danny Yoo wrote: > In pseudocode, this will look something like: > > ### > hints = identifyDuplicateRecords(filename) > displayDuplicateRecords(filename, hints) > ### > > > >> My data set the below is taken from is over 2.4 gb so speed and memory >> considerations come into play. >> >> Are sets more effective than lists for this? > > Sets or dictionaries make the act of "lookup" of a key fairly cheap. > In > the two-pass approach, the first pass can use a dictionary to > accumulate > the number of times a certain record's key has occurred. > > Note that, because your file is so large, the dictionary probably > shouldn't accumulation the whole mass of information that we've seen so > far: instead, it's sufficient to record the information we need to > recognize a duplicate. However, the first pass will consume a lot of memory. Considering the worst-case scenario where each record only appears once, you'll find yourself with the whole 2GB file loaded into memory. (or do you have a "smarter" way to do this?) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From tegmine at gmail.com Wed Jan 26 01:50:27 2005 From: tegmine at gmail.com (Luis N) Date: Wed Jan 26 01:50:32 2005 Subject: [Tutor] ascii encoding In-Reply-To: <797fe3d405012415422f852093@mail.gmail.com> References: <77bfa81a05012415294913515f@mail.gmail.com> <797fe3d405012415422f852093@mail.gmail.com> Message-ID: <77bfa81a050125165067bd740c@mail.gmail.com> Ok, urllib.quote worked just fine, and of course so did urllib.pathname2url. I should have run a dir() on urllib. Those functions don't appear in http://docs.python.org/lib/module-urllib.html Now, how might one go about calculating the New York time off-set from GMT? The server is in the U.S. but time.localtime() is giving me GMT. From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 02:27:37 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 02:27:50 2005 Subject: [Tutor] sorting a 2 gb file In-Reply-To: <00F7AAD2-6F2B-11D9-AAEE-000393CBC88E@yahoo.fr> Message-ID: On Tue, 25 Jan 2005, Max Noel wrote: > >> My data set the below is taken from is over 2.4 gb so speed and > >> memory considerations come into play. > >> > >> Are sets more effective than lists for this? > > > > Sets or dictionaries make the act of "lookup" of a key fairly cheap. > > In the two-pass approach, the first pass can use a dictionary to > > accumulate the number of times a certain record's key has occurred. > > > > Note that, because your file is so large, the dictionary probably > > shouldn't accumulation the whole mass of information that we've seen > > so far: instead, it's sufficient to record the information we need to > > recognize a duplicate. > > However, the first pass will consume a lot of memory. Considering > the worst-case scenario where each record only appears once, you'll find > yourself with the whole 2GB file loaded into memory. > (or do you have a "smarter" way to do this?) Hi Max, My assumptions are that each record consists of some identifying string "key" that's associated to some "value". How are we deciding that two records are talking about the same thing? I'm hoping that the set of unique keys isn't itself very large. Under this assumption, we can do something like this: ### from sets import Set def firstPass(f): """Returns a set of the duplicate keys in f.""" seenKeys = Set() duplicateKeys = Set() for record in f: key = extractKey(record) if key in seenKeys: duplicateKeys.add(key) else: seenKeys.add(key) return duplicateKeys ### where we don't store the whole record into memory, but only the 'key' portion of the record. And if the number of unique keys is small enough, this should be fine enough to recognize duplicate records. So on the second passthrough, we can display the duplicate records on-the-fly. If this assumption is not true, then we need to do something else. *grin* One possibility might be to implement an external sorting mechanism: http://www.nist.gov/dads/HTML/externalsort.html But if we're willing to do an external sort, then we're already doing enough work that we should really consider using a DBMS. The more complicated the data management becomes, the more attractive it becomes to use a real database to handle these data management issues. We're trying to solve a problem that is already solved by a real database management system. Talk to you later! From gtsang at lnxw.com Wed Jan 26 02:33:55 2005 From: gtsang at lnxw.com (Gilbert Tsang) Date: Wed Jan 26 02:34:00 2005 Subject: [Tutor] Read file line by line Message-ID: <41F6F383.7020905@lynuxworks.com> Hey you Python coders out there: Being a Python newbie, I have this question while trying to write a script to process lines from a text file line-by-line: #!/usr/bin/python fd = open( "test.txt" ) content = fd.readline() while (content != "" ): content.replace( "\n", "" ) # process content content = fd.readline() 2 questions: 1. Why does the assignment-and-test in one line not allowed in Python? For example, while ((content = fd.readline()) != ""): 2. I know Perl is different, but there's just no equivalent of while ($line = ) { } ? I'm not asking for Python has to offer Perl already has, but simply wondering a good way to read from a file line-by-line. Regards, Gilbert. From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 02:51:26 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 02:51:31 2005 Subject: [Tutor] Read file line by line In-Reply-To: <41F6F383.7020905@lynuxworks.com> Message-ID: On Tue, 25 Jan 2005, Gilbert Tsang wrote: > Hey you Python coders out there: > > Being a Python newbie, I have this question while trying to write a > script to process lines from a text file line-by-line: > > #!/usr/bin/python > fd = open( "test.txt" ) > content = fd.readline() > while (content != "" ): > content.replace( "\n", "" ) > # process content > content = fd.readline() > > 1. Why does the assignment-and-test in one line not allowed in Python? > For example, while ((content = fd.readline()) != ""): Hi Gilbert, welcome aboard! Python's design is to make statements like assignment stand out in the source code. This is different from Perl, C, and several other languages, but I think it's the right thing in Python's case. By making it a statement, we can visually scan by eye for assignments with ease. There's nothing that really technically prevents us from doing an assignment as an expression, but Python's language designer decided that it encouraged a style of programming that made code harder to maintain. By making it a statement, it removes the possiblity of making a mistake like: ### if ((ch = getch()) = 'q') { ... } ### There are workarounds that try to reintroduce assignment as an expression: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/202234 but we strongly recommend you don't use it. *grin* > 2. I know Perl is different, but there's just no equivalent of while > ($line = ) { } ? Python's 'for' loop has built-in knowledge about "iterable" objects, and that includes files. Try using: for line in file: ... which should do the trick. Hope this helps! From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 03:01:27 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 03:04:42 2005 Subject: [Tutor] Read file line by line In-Reply-To: Message-ID: > There's nothing that really technically prevents us from doing an > assignment as an expression, but Python's language designer decided that > it encouraged a style of programming that made code harder to maintain. > By making it a statement, it removes the possiblity of making a mistake > like: > > ### > if ((ch = getch()) = 'q') { ... } > ### ....hmmm. This doesn't compile. Never mind, I screwed up. *grin* But the Python FAQ does have an entry about this topic, if you're interested: http://python.org/doc/faq/general.html#why-can-t-i-use-an-assignment-in-an-expression From maxnoel_fr at yahoo.fr Wed Jan 26 03:27:27 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 26 03:27:41 2005 Subject: [Tutor] ascii encoding In-Reply-To: <77bfa81a050125165067bd740c@mail.gmail.com> References: <77bfa81a05012415294913515f@mail.gmail.com> <797fe3d405012415422f852093@mail.gmail.com> <77bfa81a050125165067bd740c@mail.gmail.com> Message-ID: On Jan 26, 2005, at 00:50, Luis N wrote: > Ok, urllib.quote worked just fine, and of course so did > urllib.pathname2url. > > I should have run a dir() on urllib. Those functions don't appear in > http://docs.python.org/lib/module-urllib.html > > Now, how might one go about calculating the New York time off-set from > GMT? The server is in the U.S. but time.localtime() is giving me GMT. time.timezone gives you, I think, the offset between your current timezone and GMT. However, being myself in the GMT zone, I don't know exactly if the returned offset is positive or negative (it returns 0 here, which makes sense :D ). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From tameyer at ihug.co.nz Wed Jan 26 03:44:48 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Wed Jan 26 03:44:56 2005 Subject: [Tutor] ascii encoding In-Reply-To: Message-ID: > time.timezone gives you, I think, the offset between > your current timezone and GMT. However, being myself in the GMT zone, > I don't know exactly if the returned offset is positive or negative > (it returns 0 here, which makes sense :D ). Whether or not it's positive or negative depends on which side of GMT/UTC you are, of course :) Note that the result in is seconds, too: >>> import time >>> time.timezone -43200 >>> time.timezone/60/60 -12 (I'm in NZ, 12 hours ahead of GMT/UTC). =Tony.Meyer From tegmine at gmail.com Wed Jan 26 03:56:32 2005 From: tegmine at gmail.com (Luis N) Date: Wed Jan 26 03:56:41 2005 Subject: [Tutor] ascii encoding In-Reply-To: References: Message-ID: <77bfa81a050125185650bdf146@mail.gmail.com> In other words I have to do some arithmetic: >>> import time >>> time.timezone 0 The server is located in Dallas, Texas. On Wed, 26 Jan 2005 15:44:48 +1300, Tony Meyer wrote: > > time.timezone gives you, I think, the offset between > > your current timezone and GMT. However, being myself in the GMT zone, > > I don't know exactly if the returned offset is positive or negative > > (it returns 0 here, which makes sense :D ). > > Whether or not it's positive or negative depends on which side of GMT/UTC > you are, of course :) Note that the result in is seconds, too: > > >>> import time > >>> time.timezone > -43200 > >>> time.timezone/60/60 > -12 > > (I'm in NZ, 12 hours ahead of GMT/UTC). > > =Tony.Meyer > > From carroll at tjc.com Wed Jan 26 04:17:59 2005 From: carroll at tjc.com (Terry Carroll) Date: Wed Jan 26 04:18:16 2005 Subject: [Tutor] Should this be a list comprehension or something? Message-ID: The following Python code works correctly; but I can't help but wonder if my for loop is better implemented as something else: a list comprehension or something else more Pythonic. My goal here is not efficiency of the code, but efficiency in my Python thinking; so I'll be thinking, for example, "ah, this should be a list comprehension" instead of a knee-jerk reaction to use a for loop. Comments? The point of the code is to take a sequence of objects, each object representing an amount of water with a given mass and temperature, and to return another object that represents all the water ideally combined. The formulae for the combined mass and temp are respectively: combined mass = M1 + M2 + M3 (duh) combined temp = ((M1*T1) + (M2*T2) + (M3*T3)) / (M1 + M2 + M3) Here's my code: -------------------- class Water: def __init__(self, WaterMass, WaterTemperature): self.mass = WaterMass self.temperature = WaterTemperature def __repr__(self): return ("%.2f, %.2f" % (self.mass, self.temperature)) def CombineWater(WaterList): totalmass=0 numerator = 0; denominator = 0 for WaterObject in WaterList: totalmass += WaterObject.mass numerator += WaterObject.mass * WaterObject.temperature return Water(totalmass, numerator/totalmass) -------------------- Example use: -------------------- w1 = Water(50,0) w2 = Water(50,100) w3 = Water(25,50) print CombineWater((w1,w2,w3)) -------------------- prints, as expected: 125.00, 50.00 From maxnoel_fr at yahoo.fr Wed Jan 26 04:21:38 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 26 04:21:49 2005 Subject: [Tutor] ascii encoding In-Reply-To: References: Message-ID: <6449BE44-6F49-11D9-AAEE-000393CBC88E@yahoo.fr> On Jan 26, 2005, at 02:44, Tony Meyer wrote: >> time.timezone gives you, I think, the offset between >> your current timezone and GMT. However, being myself in the GMT zone, >> I don't know exactly if the returned offset is positive or negative >> (it returns 0 here, which makes sense :D ). > > Whether or not it's positive or negative depends on which side of > GMT/UTC > you are, of course :) Note that the result in is seconds, too: Of course. What I meant was that I didn't know which "side" returns a positive offset (IOW, whether Paris (GMT+1) returns -3600 or +3600 -- it's the former, it seems). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Wed Jan 26 04:26:42 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 26 04:26:50 2005 Subject: [Tutor] ascii encoding In-Reply-To: <77bfa81a050125185650bdf146@mail.gmail.com> References: <77bfa81a050125185650bdf146@mail.gmail.com> Message-ID: <1919B3E2-6F4A-11D9-AAEE-000393CBC88E@yahoo.fr> On Jan 26, 2005, at 02:56, Luis N wrote: > In other words I have to do some arithmetic: > >>>> import time >>>> time.timezone > 0 > > The server is located in Dallas, Texas. Which means it's not properly configured. On UNIX systems, to configure the timezone, you must adjust /etc/localtime so that it's a symlink that points to the appropriate timezone in /usr/share/zoneinfo . The exact layout of the /usr/share/zoneinfo folder is probably implementation-specific, but for example, here's how it is on my Mac OS X box: [max@Megaera ~]% ls -l /etc/localtime lrwxr-xr-x 1 root wheel 33 25 Jan 18:58 /etc/localtime -> /usr/share/zoneinfo/Europe/London -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From maxnoel_fr at yahoo.fr Wed Jan 26 04:39:28 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Wed Jan 26 04:39:31 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: References: Message-ID: On Jan 26, 2005, at 03:17, Terry Carroll wrote: > My goal here is not efficiency of the code, but efficiency in my Python > thinking; so I'll be thinking, for example, "ah, this should be a list > comprehension" instead of a knee-jerk reaction to use a for loop. > > Comments? > > The point of the code is to take a sequence of objects, each object > representing an amount of water with a given mass and temperature, and > to > return another object that represents all the water ideally combined. > The > formulae for the combined mass and temp are respectively: > > combined mass = M1 + M2 + M3 (duh) > combined temp = ((M1*T1) + (M2*T2) + (M3*T3)) / (M1 + M2 + M3) > > Here's my code: > -------------------- > class Water: > def __init__(self, WaterMass, WaterTemperature): > self.mass = WaterMass > self.temperature = WaterTemperature > def __repr__(self): > return ("%.2f, %.2f" % (self.mass, self.temperature)) > > def CombineWater(WaterList): > totalmass=0 > numerator = 0; denominator = 0 > for WaterObject in WaterList: > totalmass += WaterObject.mass > numerator += WaterObject.mass * WaterObject.temperature > return Water(totalmass, numerator/totalmass) Well, you can do this with list comprehensions, yeah: totalmass = sum([WaterObject.mass for WaterObject in WaterList]) totaltemp = sum([WaterObject.mass * WaterObject.temp for WaterObject in WaterList]) / totalmass return Water(totalmass, totaltemp) Doesn't seem that much more Pythonic to me. I find it about as readable as your code, but someone who isn't used to list comprehensions will find that weird-looking. However, someone who uses functional programming languages a lot (Lisp, Scheme, Haskell, ML...) will be familiar with that. The actual pros of that method is that it's a functional approach and that it has less lines than your approach (you can even reduce it to a one-liner by adding a third list comprehension, but at that point it starts to look ugly). As for the cons, as I said, it may seem less readable than the original version to the non-experienced; and chances are it's slower than the original version since it has to iterate through 4 lists instead of 2. In any case, when in doubt, do what you think will be easier to maintain. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From tameyer at ihug.co.nz Wed Jan 26 05:00:15 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Wed Jan 26 05:00:23 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: Message-ID: > Well, you can do this with list comprehensions, yeah: > > totalmass = sum([WaterObject.mass for WaterObject in WaterList]) > totaltemp = sum([WaterObject.mass * WaterObject.temp for > WaterObject in > WaterList]) / totalmass > return Water(totalmass, totaltemp) > > Doesn't seem that much more Pythonic to me. I find it about as > readable as your code, but someone who isn't used to list > comprehensions will find that weird-looking. I would agree that it doesn't seem any more Pythonic. I think either way is fairly readable (being able to use 'sum' with list comps helps readability, IMO). [...] > As for the cons, as I said, it may seem less readable than the > original version to the non-experienced; and chances are it's slower > than the original version since it has to iterate through 4 lists > instead of 2. Timeit gives me 6.73 seconds for the original, 10.70 seconds for list comps, and 10.98 for gen exps, so it does look like the original is certainly faster. =Tony.Meyer From keridee at jayco.net Wed Jan 26 05:54:02 2005 From: keridee at jayco.net (Jacob S.) Date: Wed Jan 26 05:53:59 2005 Subject: [Tutor] Advise... Message-ID: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> Hi all. Long time no see. (About five days, right?) Anyway, I know the first thing that some of you are going to say is using eval(). I don't want a whole guilt trip on security risks and all that. I do not want to share the code with anyone else while it's on my computer, and I sure don't have anyone near me that knows python. I would be surprised if more than 50 people in Portland, IN knew anything about python. So security is not a problem. I guess what I'm looking for is someone who knows the Reimann Sum better than I do and can tell me whether I can do something to make it more efficient. It's horribly slow with ten thousand steps-- I don't know the notation very well, but it loops the loop O(step*(maximum-minimum)) times, which is horribly sucky. In case anyone doesn't know, Reimann's sum is the computer's version of the definite integral, the area under a curve in a particular domain. Basic input and output. If a curve is a straight line, say y = x, the area under the line on an interval can be found by geometric means. However, if you use a parabola, or any other function, say y = 3*x**2, What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt ############################ from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) total = 0 step = 100000 x = minimum while minimum <= x <= maximum: area = eval(fofx)*1/step total = total+area x = x+1/step print total ############################# From ejp at zomething.com Wed Jan 26 06:02:34 2005 From: ejp at zomething.com (EJP) Date: Wed Jan 26 06:02:40 2005 Subject: [Tutor] Class within class, or...? In-Reply-To: References: Message-ID: <20050125210234.536387441.ejp@zomething.com> [sent previously, please ignore if already rec'd] A basic OOP question, I guess. I have a file from which I want to extract some data. The data of interest may be towards the end of the file at position A, defined by schema A; or it may be in the file at variable positions near the beginning of the file which are defined by a combination of schema B and the data in the file; or the data of interest may be in the file both ways (and the data may be different). I'd like to write a class for the data extraction. In my mind I'd have one overall class for the data extraction with attributes for the two possible sets of data extracted. I am thinking It'd be nice if these two attributes were two different classes themselves. Something like this: class Reader: def __init__(self, filePath=""): try: self.fileObj=file(filePath,"r") except: self.errorMsg="File opening error" self.dataA=SchemaA() self.dataB=SchemaB() ... class SchemaA(): def __init__(self): self.data={} ... class SchemaB(): def __init__(self): self.data={} ... I'm struggling with a mind stuck in functional programming mode and also lazy from simple scripting, thus my questions: - Is there any problem with structuring my classes like this? I assume that using SchemaA and SchemaB within Reader would be no different than using any other classes within that class (it all gets sorted out in compilation). - Is it possible, and is there any advantage to, embedding the classes (SchemaA and SchemaB) within the Reader class? The Schema classes would not be used elsewhere. - Are there performance considerations to the structure I use? Thanks in advance, Eric From tameyer at ihug.co.nz Wed Jan 26 06:09:45 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Wed Jan 26 06:09:59 2005 Subject: [Tutor] Advise... In-Reply-To: Message-ID: > Anyway, I know the first thing that some of you are going to > say is using eval(). [...] > Which, considering that it is supposed to be exactly 117, > It's darn good. > Unfortunately, it also takes about 10 seconds to do all that. > Any suggestions? Any advice? On my machine your version takes about 14 seconds. If you replace 'eval(fofx)' with '3*x*x' it takes about 0.7 seconds. Am I allowed to say "don't use eval()" if I'm saying it because it's extremely slow, rather than for security reasons? ;) If the user must be able to enter in the function, then it would be better to evaluate this once and turn it into some sort of function that you can call inside the loop (it's the eval that is so expensive). How to do that depends a lot on how complex the possible functions can be (if they'll only include 'x*+/-' and numbers, for example, it's not so tricky). =Tony.Meyer From tameyer at ihug.co.nz Wed Jan 26 06:14:33 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Wed Jan 26 06:14:38 2005 Subject: [Tutor] Advise... In-Reply-To: Message-ID: [Tony Meyer] > If the user must be able to enter in the function, then it > would be better to evaluate this once and turn it into some > sort of function that you can call inside the loop (it's the > eval that is so expensive). I should have included some code that does this: """ from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") start = time.time() minimum = float(minimum) maximum = float(maximum) total = 0 step = 100000 x = minimum a = compile(fofx, '', 'eval') while minimum <= x <= maximum: area = eval(a)/step total = total+area x = x+1/step print total """ This takes ~1.7 seconds here, so about 8 times faster than without the compile() call, but about 2.5 times slower than directly with the 3*x*x. =Tony.Meyer From nbbalane at gmail.com Wed Jan 26 08:41:43 2005 From: nbbalane at gmail.com (jrlen balane) Date: Wed Jan 26 08:41:54 2005 Subject: [Tutor] how to plot a graph Message-ID: <2cad209005012523411a57f45c@mail.gmail.com> i'm going to use now the matplotlib in plotting a graph. i'm currently using python 2.3(enthought edition) on win 2000/xp. i'm using boa constructor on the GUI part. i am using an MDIParentFrame. one of the child frame will be used for the table part. then another child frame will be used to show the graph, how am i going to do this? will i just import the child frame containing the tables and then i'll be able to just get the data from the table and use it to plot a graph? how am i going to assign to a variable each input to the table? can you please show me a sample code to do this? i'm a little lost since i'm a bit new to python. also, how am i going to assign to a variable anything that a user inputs to a wxTxtCtrl? any help would greatly be appreciated. thanks and more power From tgrimes at teleport.com Wed Jan 26 09:15:29 2005 From: tgrimes at teleport.com (TJ) Date: Wed Jan 26 09:15:33 2005 Subject: [Tutor] Advise... In-Reply-To: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> References: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> Message-ID: >What is the function? 3*x*x >What is the minimum? 2 >What is the maximum? 5 >117.000435 > >Which, considering that it is supposed to be exactly 117, It's darn >good. Unfortunately, it also takes about >10 seconds to do all that. >Any suggestions? Any advice? TIA >Jacob Schmidt > Jacob, You can get better accuracy with orders of magnitude fewer steps by evaluating the function at the midpoint of each step rather than the low value. This has the added benefit of yielding the same result when stepping x up (2 to 5) or down (5 to 2). Here's some modified code (I don't have psyco): ######################## from __future__ import division import time def reimannSum(func_x, min_x, max_x, steps): start = time.time() totalArea = 0 #precalculate step size delta_x = 1 / steps #set x to midpoint of first step x = min_x + delta_x / 2 while min_x <= x <= max_x: totalArea += eval(func_x) * delta_x x += delta_x return totalArea, steps, time.time() - start stepsList = [100000, 10000, 1000, 500, 200, 100] fmt = 'Area: %f Steps: %d Time: %f' for steps in stepsList: print fmt % reimannSum('3 * x * x', 2, 5, steps) ######################## The results on my machine are: Area: 117.000000 Steps: 100000 Time: 44.727405 Area: 117.000000 Steps: 10000 Time: 4.472391 Area: 116.999999 Steps: 1000 Time: 0.454841 Area: 116.999997 Steps: 500 Time: 0.223208 Area: 116.999981 Steps: 200 Time: 0.089651 Area: 116.999925 Steps: 100 Time: 0.044431 TJ From sigurd at 12move.de Wed Jan 26 11:53:02 2005 From: sigurd at 12move.de (Karl =?iso-8859-1?Q?Pfl=E4sterer?=) Date: Wed Jan 26 11:56:19 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: (Terry Carroll's message of "Tue, 25 Jan 2005 19:17:59 -0800 (PST)") References: Message-ID: On 26 Jan 2005, carroll@tjc.com wrote: > The following Python code works correctly; but I can't help but wonder if > my for loop is better implemented as something else: a list comprehension > or something else more Pythonic. [Code] > > -------------------- > w1 = Water(50,0) > w2 = Water(50,100) > w3 = Water(25,50) > > print CombineWater((w1,w2,w3)) > -------------------- > > prints, as expected: 125.00, 50.00 What about overloading ?+?? class Water(object): def __init__(self, mass, temp): self.mass = mass self.temp = temp def __repr__(self): return ("%.2f, %.2f" % (self.mass, self.temp)) def __add__(self, other): m = self.mass + other.mass return Water(m, (self.mass*self.temp + other.mass*other.temp)/m) .>>> w1 = Water(50,0) .>>> w2 = Water(50, 100) .>>> w3 = Water(25,50) .>>> w1 + w2 + w3 .125.00, 50.00 Karl -- Please do *not* send copies of replies to me. I read the list From kent37 at tds.net Wed Jan 26 12:07:22 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 26 12:07:27 2005 Subject: [Tutor] Advise... In-Reply-To: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> References: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> Message-ID: <41F779EA.4090007@tds.net> There was a discussion about this same question (in the context of graphing a user-supplied function) just a few weeks ago. My suggestion was to use exec to create a real function object that you can call directly; you can read it here: http://mail.python.org/pipermail/tutor/2005-January/034696.html Read the whole thread for other ideas. Kent Jacob S. wrote: > Hi all. > > Long time no see. (About five days, right?) > Anyway, I know the first thing that some of you are going to say is > using eval(). I don't want a whole > guilt trip on security risks and all that. I do not want to share the > code with anyone else while it's on my > computer, and I sure don't have anyone near me that knows python. I > would be surprised if more than 50 > people in Portland, IN knew anything about python. So security is not a > problem. I guess what I'm > looking for is someone who knows the Reimann Sum better than I do and > can tell me whether I can do > something to make it more efficient. It's horribly slow with ten > thousand steps-- I don't know the notation > very well, but it loops the loop O(step*(maximum-minimum)) times, which > is horribly sucky. > In case anyone doesn't know, Reimann's sum is the computer's version > of the definite integral, the area > under a curve in a particular domain. > > Basic input and output. > If a curve is a straight line, say y = x, the area under the line on an > interval can be found by geometric means. > However, if you use a parabola, or any other function, say y = 3*x**2, > > > What is the function? 3*x*x > What is the minimum? 2 > What is the maximum? 5 > 117.000435 > > Which, considering that it is supposed to be exactly 117, It's darn > good. Unfortunately, it also takes about > 10 seconds to do all that. > Any suggestions? Any advice? TIA > Jacob Schmidt > > > ############################ > from __future__ import division > import psyco > psyco.full() > fofx = raw_input("What is the function? ") > minimum = raw_input("What is the minimum? ") > maximum = raw_input("What is the maximum? ") > minimum = float(minimum) > maximum = float(maximum) > total = 0 > step = 100000 > x = minimum > while minimum <= x <= maximum: > area = eval(fofx)*1/step > total = total+area > x = x+1/step > print total > ############################# > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Wed Jan 26 13:01:20 2005 From: kent37 at tds.net (Kent Johnson) Date: Wed Jan 26 13:01:29 2005 Subject: [Tutor] sorting a 2 gb file- i shrunk it and turned it around In-Reply-To: References: Message-ID: <41F78690.2060804@tds.net> My guess is that your file is small enough that Danny's two-pass approach will work. You might even be able to do it in one pass. If you have enough RAM, here is a sketch of a one-pass solution: # This will map each result to a list of queries that contain that result results= {} # Iterate the file building results for line in file: if isQueryLine(line): current_query = line elif isResultLine(line): key = getResultKey(line) results.setdefault(key, []).append(current_query) # see explanation below # Now go through results looking for entries with more than one query for key, queries in results.iteritems(): if len(queries) > 1: print print key for query in queries: print query You have to fill in the details of isQueryLine(), isResultLine() and getResultKey(); from your earlier posts I'm guessing you can figure them out. What is this: results.setdefault(key, []).append(current_query) setdefault() is a handy method of a dict. It looks up the value corresponding to the given key. If there is no value, it *sets* the value to the given default, and returns that. So after results.setdefault(key, []) results[key] will have a list in it, and we will have a reference to the list. Then appending the list adds the query to the list in the dict. Please let us know what solution you end up using, and how much memory it needs. I'm interested... Kent Scott Melnyk wrote: > Thanks for the thoughts so far. After posting I have been thinking > about how to pare down the file (much of the info in the big file was > not relevant to this question at hand). > > After the first couple of responses I was even more motivated to > shrink the file so not have to set up a db. This test will be run only > now and later to verify with another test set so the db set up seemed > liked more work than might be worth it. > > I was able to reduce my file down about 160 mb in size by paring out > every line not directly related to what I want by some simple regular > expressions and a couple tests for inclusion. > > The format and what info is compared against what is different from my > original examples as I believe this is more clear. > > > my queries are named by the lines such as: > ENSE00001387275.1|ENSG00000187908.1|ENST00000339871.1 > ENSE is an exon ENSG is the gene ENST is a transcript > > They all have the above format, they differ in in numbers above > following ENS[E,G orT]. > > Each query is for a different exon. For background each gene has many > exons and there are different versions of which exons are in each gene > in this dataset. These different collections are the transcripts ie > ENST00000339871.1 > > in short a transcript is a version of a gene here > transcript 1 may be formed of exons a,b and c > transcript 2 may contain exons a,b,d > > > > the other lines (results) are of the format > hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=... 44 0.001 > hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 44 0.001 > > "hg17_chainMm5_chr7_random range=chr10:124355404-124355687" is the > important part here from "5'pad" on is not important at this point > > > What I am trying to do is now make a list of any of the results that > appear in more than one transcript > > ########## > FILE SAMPLE: > > This is the number 1 query tested. > Results for scoring against Query= > ENSE00001387275.1|ENSG00000187908.1|ENST00000339871.1 > are: > > hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=... 44 0.001 > hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 44 0.001 > hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad... 44 0.001 > hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad... 44 0.001 > hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad... 44 0.001 > hg17_chainMm5_chr7_random range=chr10:124355388-124355719 5'pad=... 44 0.001 > > .... > > This is the number 3 query tested. > Results for scoring against Query= > ENSE00001365999.1|ENSG00000187908.1|ENST00000339871.1 > are: > > hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa... 60 2e-08 > hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad... 60 2e-08 > hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad... 60 2e-08 > hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad... 60 2e-08 > > ############## > > I would like to generate a file that looks for any results (the > hg17_etc line) that occur in more than transcript (from the query > line ENSE00001365999.1|ENSG00000187908.1|ENST00000339871.1) > > > so if > hg17_chainMm5_chr7_random range=chr10:124355404-124355687 > shows up again later in the file I want to know and want to record > where it is used more than once, otherwise I will ignore it. > > I am think another reg expression to capture the transcript id > followed by something that captures each of the results, and writes > to another file anytime a result appears more than once, and ties the > transcript ids to them somehow. > > Any suggestions? > I agree if I had more time and was going to be doing more of this the > DB is the way to go. > -As an aside I have not looked into sqlite, I am hoping to avoid the > db right now, I'd have to get the sys admin to give me permission to > install something again etc etc. Where as I am hoping to get this > together in a reasonably short script. > > However I will look at it later (it could be helpful for other things for me. > > Thanks again to all, > Scott > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From billk at fastmail.fm Wed Jan 26 13:57:33 2005 From: billk at fastmail.fm (Bill Kranec) Date: Wed Jan 26 13:57:37 2005 Subject: [Tutor] Advise... In-Reply-To: References: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> Message-ID: <41F793BD.8060908@fastmail.fm> There has been alot of talk on this list about using list comprehensions lately, and this could be one of those useful places. While I don't have time to experiment with real code, I would suggest changing your function to look like: steps = [ min_x + i*delta_x for i in range(steps) ] totalarea = sum([ eval(func_x)*delta_x for x in steps ]) Since list comprehensions are significantly faster than while loops, this could be a big speed boost. There may be a mistake or two in the above code, but hopefully the idea will be helpful. Bill TJ wrote: >> What is the function? 3*x*x >> What is the minimum? 2 >> What is the maximum? 5 >> 117.000435 >> >> Which, considering that it is supposed to be exactly 117, It's darn >> good. Unfortunately, it also takes about >> 10 seconds to do all that. >> Any suggestions? Any advice? TIA >> Jacob Schmidt >> > > Jacob, > > You can get better accuracy with orders of magnitude fewer steps by > evaluating the function at the midpoint of each step rather than the > low value. This has the added benefit of yielding the same result > when stepping x up (2 to 5) or down (5 to 2). > > Here's some modified code (I don't have psyco): > > ######################## > from __future__ import division > import time > def reimannSum(func_x, min_x, max_x, steps): > start = time.time() > totalArea = 0 > #precalculate step size > delta_x = 1 / steps > #set x to midpoint of first step > x = min_x + delta_x / 2 > while min_x <= x <= max_x: > totalArea += eval(func_x) * delta_x > x += delta_x > return totalArea, steps, time.time() - start > > stepsList = [100000, 10000, 1000, 500, 200, 100] > fmt = 'Area: %f Steps: %d Time: %f' > > for steps in stepsList: > print fmt % reimannSum('3 * x * x', 2, 5, steps) > ######################## > > > The results on my machine are: > > Area: 117.000000 Steps: 100000 Time: 44.727405 > Area: 117.000000 Steps: 10000 Time: 4.472391 > Area: 116.999999 Steps: 1000 Time: 0.454841 > Area: 116.999997 Steps: 500 Time: 0.223208 > Area: 116.999981 Steps: 200 Time: 0.089651 > Area: 116.999925 Steps: 100 Time: 0.044431 > > TJ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From srini_iyyer_bio at yahoo.com Wed Jan 26 19:44:01 2005 From: srini_iyyer_bio at yahoo.com (Srinivas Iyyer) Date: Wed Jan 26 19:44:06 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: Message-ID: <20050126184401.36156.qmail@web53503.mail.yahoo.com> Hi, I am a new member to this group and relatively to python. I have a list with 4 columns and column1 elements are unique. I wanted to extract unique elements in column3 and and place the other elements of the column along with unique elements in column 4 as a tab delim text. Table: col1 col2 col3 col4 A Apple 5 Chennai B Baby 11 Delhi I Baby* 1 Delhi M Dasheri+ 5 Mumbai K Apple 12 Copenhagen * -> Baby => Infant + -> Dasheri => Mango for ele in table: col2 = ele.split('\t')[1] col2.append(col2) col2_set = sets.Set(col2) col2_set -> (Apple,Baby Dasheri) Apple A,K 5,12 Chennai, Copenhagen Baby B,I 1,11 Delhi Dasheri M 5 Mumbai. for ele in col1_set: nam = ele.strip() for k in list: m = re.search(nam,k) cols = k.split('\t') a = cols[0] n = cols[2] c = cols[3] print nam+'\t'+a+'\t'+n+'\t'+c A dictionary option does not work beca This isnt working properly. please help me any one question 2: A list with 100 unique items, repeated several times. Apples - repeated 1000 times in list Vegie - repeated 300 times in list how can I get a uniq elements that repeated several times: for item in range(len(list)): for k in range(len(list)): if item == k: if list[item] != k[list]: print item This isnt working either. logic appears correct. looking forward for help pleas. thank you Srini __________________________________ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 20:01:09 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 20:01:13 2005 Subject: [Tutor] Advise... In-Reply-To: <41F793BD.8060908@fastmail.fm> Message-ID: On Wed, 26 Jan 2005, Bill Kranec wrote: > There has been alot of talk on this list about using list comprehensions > lately, and this could be one of those useful places. While I don't > have time to experiment with real code, I would suggest changing your > function to look like: > > steps = [ min_x + i*delta_x for i in range(steps) ] > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Since list comprehensions are significantly faster than while loops, > this could be a big speed boost. > > There may be a mistake or two in the above code, but hopefully the idea > will be helpful. Calling eval() there in the inner loop might be costly, because Python needs to do extra work to tokenize and parse the string, every time through the iteration. We want to reduce the work done in tight inner loops like that. We can do some of that work up front by compiling the code. Here's some hacky code to do the compilation up front: ### >>> def makeFunction(expressionString): ... compiledExp = compile(expressionString, 'makeFunction', 'eval') ... def f(x): ... return eval(compiledExp, {}, {'x' : x}) ... return f ... ### Some of the stuff there is a bit obscure, but the idea is that we get Python to parse and compile the expression down once. Later on, we can evaluation the compiled code, and that should be faster than evaluating a string. Once we have this, we can use it like this: ### >>> myFunction = makeFunction("3*x*x") >>> myFunction(0) 0 >>> myFunction(1) 3 >>> myFunction(2) 12 >>> myFunction(3) 27 ### So although makeFunction()'s internals are weird, it shouldn't be too hard to treat it as a black box. *grin* Let's see how this performs against that 3x^2 expression we saw before. The original approach that calls eval() on the string takes time: ### >>> def timeIt(f, n=1000): ... start = time.time() ... for i in xrange(n): ... f(i) ... end = time.time() ... return end - start ... >>> def myFunctionOriginal(x): ... return eval("3*x*x") ... >>> timeIt(myFunctionOriginal) 0.036462068557739258 ### The precompiled expression can work more quickly: ### >>> timeIt(myFunction) 0.0050611495971679688 ### And we should still get the same results: ### >>> for i in range(2000): ... assert myFunction(i) == myFunctionOriginal(i) ... >>> ### I hope this helps! From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 20:07:43 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 20:07:53 2005 Subject: [Tutor] Advise... In-Reply-To: Message-ID: > > There has been alot of talk on this list about using list comprehensions > > lately, and this could be one of those useful places. While I don't > > have time to experiment with real code, I would suggest changing your > > function to look like: > > > > steps = [ min_x + i*delta_x for i in range(steps) ] > > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Calling eval() there in the inner loop might be costly, because Python > needs to do extra work to tokenize and parse the string, every time > through the iteration. We want to reduce the work done in tight inner > loops like that. > > We can do some of that work up front by compiling the code. Here's some > hacky code to do the compilation up front: > > ### > >>> def makeFunction(expressionString): > ... compiledExp = compile(expressionString, 'makeFunction', 'eval') > ... def f(x): > ... return eval(compiledExp, {}, {'x' : x}) > ... return f > ... > ### Oh! There's a slightly simpler way to write that makeFunction(): ### >>> def makeFunction(s): ... return eval("lambda x: " + s) ... ### It even has a little less overhead than the previous code. ### >>> timeIt(myFunctionOriginal) 0.035856008529663086 >>> >>> timeIt(makeFunction("3*x*x")) 0.00087714195251464844 ### Best of wishes! From alan.gauld at freenet.co.uk Wed Jan 26 23:19:52 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 26 23:19:43 2005 Subject: [Tutor] Read file line by line References: <41F6F383.7020905@lynuxworks.com> Message-ID: <008001c503f5$29ea50e0$36bd8651@xp> > 1. Why does the assignment-and-test in one line not allowed in Python? > For example, while ((content = fd.readline()) != ""): Because Guido didn't write it that way? ;-) And that may have been because it is such a common source of bugs. So common in fact that many compilers now offer to emit a warning when they detect it...just in case you meant to use ==... > 2. I know Perl is different, but there's just no equivalent of while > ($line = ) { } ? Take a look at the fileinput module and of course for line in fd: #do something works too. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Wed Jan 26 23:24:40 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 26 23:24:38 2005 Subject: [Tutor] ascii encoding References: Message-ID: <008b01c503f5$d5cb52b0$36bd8651@xp> > Whether or not it's positive or negative depends on which side of GMT/UTC > you are, of course :) Note that the result in is seconds, too: Which is insane since timezones have nothing to do with time offsets. Especially at the second level! Oh well, nothing is perfect! Alan G. (Feeling picky today) From alan.gauld at freenet.co.uk Wed Jan 26 23:27:12 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 26 23:27:04 2005 Subject: [Tutor] Should this be a list comprehension or something? References: Message-ID: <009001c503f6$3073c9e0$36bd8651@xp> Personally no I don;t think it should be a comprehension. LCs are for building lists, although they sometimes are abused for other things, but you do not appear to be building a list per se. The for loop is clearer IMHO. > Here's my code: > -------------------- > class Water: > def __init__(self, WaterMass, WaterTemperature): > self.mass = WaterMass > self.temperature = WaterTemperature > def __repr__(self): > return ("%.2f, %.2f" % (self.mass, self.temperature)) > > def CombineWater(WaterList): > totalmass=0 > numerator = 0; denominator = 0 > for WaterObject in WaterList: > totalmass += WaterObject.mass > numerator += WaterObject.mass * WaterObject.temperature > return Water(totalmass, numerator/totalmass) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From dyoo at hkn.eecs.berkeley.edu Wed Jan 26 23:32:32 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Wed Jan 26 23:32:36 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: <20050126184401.36156.qmail@web53503.mail.yahoo.com> Message-ID: On Wed, 26 Jan 2005, Srinivas Iyyer wrote: > I have a list with 4 columns and column1 elements are unique. I wanted > to extract unique elements in column3 and and place the other elements > of the column along with unique elements in column 4 as a tab delim > text. > > Table: > > col1 col2 col3 col4 > A Apple 5 Chennai > B Baby 11 Delhi > I Baby* 1 Delhi > M Dasheri+ 5 Mumbai > K Apple 12 Copenhagen [Meta: we seem to be getting a run of similar questions this week. Scott Melnyk also asked about grouping similar records together: http://mail.python.org/pipermail/tutor/2005-January/035185.html.] Hi Srinivas, I see that you are trying to group records based on some criterion. You may find the problem easier to do if you fist do a sort on that criterion column: that will make related records "clump" together. For your sample data above, if we sort against the second column, the records will end up in the following order: ### A Apple 5 Chennai K Apple 12 Copenhagen B Baby 11 Delhi I Baby 1 Delhi M Dasheri 5 Mumbai ### In this sorting approach, you can then run through the sorted list in order. Since all the related elements should be adjacent, grouping related lines together should be much easier, and you should be able to produce the final output: ### Apple A,K 5,12 Chennai,Copenhagen Baby B,I 1,11 Delhi Dasheri M 5 Mumbai ### without too much trouble. You can do this problem without dictionaries at all, although you may find the dictionary approach a little easier to implement. > A dictionary option does not work A dictionary approach is also very possible. The thing you may be stuck on is trying to make a key associate with multiple values. Most examples of dictionaries in tutorials use strings as both the keys and values, but dictionaries are more versatile: we can also make a dictionary whose values are lists. For example, here is a small program that groups words by their first letters: ### >>> def groupAlpha(words): ... groups = {} ... for w in words: ... firstLetter = w[0] ... if firstLetter not in groups: ... groups[firstLetter] = [] ... groups[firstLetter].append(w) ... return groups ... >>> groupAlpha("this is a test of the emergency broadcast system".split()) {'a': ['a'], 'b': ['broadcast'], 'e': ['emergency'], 'i': ['is'], 'o': ['of'], 's': ['system'], 't': ['this', 'test', 'the']} ### If you have more questions, please feel free to ask. From alan.gauld at freenet.co.uk Wed Jan 26 23:31:51 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 26 23:33:10 2005 Subject: [Tutor] Class within class, or...? References: <20050125210234.536387441.ejp@zomething.com> Message-ID: <00ae01c503f6$d6a91310$36bd8651@xp> class Reader: def __init__(self, filePath=""): .... self.dataA=SchemaA() self.dataB=SchemaB() ... class SchemaA(): class SchemaB(): You probaly should put the Schema definitions before the Reader definition. Otherwise what you suggest is absolutely the norm for OO programming. Alan G. From alan.gauld at freenet.co.uk Wed Jan 26 23:36:05 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Wed Jan 26 23:35:58 2005 Subject: [Tutor] Advise... References: Message-ID: <00b301c503f7$6e4aabc0$36bd8651@xp> > If the user must be able to enter in the function, then it would be better > to evaluate this once and turn it into some sort of function that you can > call inside the loop (it's the eval that is so expensive). How to do that > depends a lot on how complex the possible functions can be (if they'll only > include 'x*+/-' and numbers, for example, it's not so tricky). exp = raw_input('Type expression') func = eval('lambda x: " + exp) print func(42) etc... Or if you really can't grokm lambda: exec('def func(x): return " + exp) should do the same... Alan G. From axg4903 at gmail.com Thu Jan 27 01:31:36 2005 From: axg4903 at gmail.com (Tony Giunta) Date: Thu Jan 27 01:31:41 2005 Subject: [Tutor] Convert string to variable name Message-ID: <4453924905012616314538eefd@mail.gmail.com> Hello tutors, This is something I've been trying to figure out for some time. Is there a way in Python to take a string [say something from a raw_input] and make that string a variable name? I want to to this so that I can create class instances on-the-fly, using a user-entered string as the instance name. I can do it with an "exec" statement, but that would force me to use more "execs" to reference the newly created class instance[s] and I would rather not do that. I'm sure I could accomplish something similar using a plain old dictionary, but I was playing around with the OOP stuff and thought it might be a neat thing to try out. I've attached the small function I've come up with to the end of this message. This is just a model I created for testing purposes, so it's not the most elegant code. Any suggestions would be greatly appreciated. TIA, tony giunta def generateInstance(): class test: def __init__(self, title, price): self.title = title self.price = price def theTitle(self, title): return self.title def thePrice(self, price): return self.price myName = raw_input("Name: ") myTitle = raw_input("Title: ") myPrice = raw_input("Price: ") exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) From keridee at jayco.net Thu Jan 27 02:09:47 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 27 02:09:42 2005 Subject: [Tutor] Advise... References: <00b301c503f7$6e4aabc0$36bd8651@xp> Message-ID: <001401c5040c$ed670ec0$d55428cf@JSLAPTOP> Thanks everyone! Kent started the suggestion of making a code object, but everyone else seems to have worked their way to it. Beautiful! I only have to call exec once, and it cuts down time considerably. Here is the new code. Jacob Schmidt Please remind me if I've forgotten anything. ### Start ################### from __future__ import division from math import * import psyco psyco.full() def reimannsum(fofx,x,max1): total = 0 step = 1e-5 exec "def f(x): return %s" % fofx while x <= max1: total = total+f(x) x = x+step return abs(total*step) fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) print reimannsum(fofx,minimum,maximum) #### End ############################## >> If the user must be able to enter in the function, then it would be > better >> to evaluate this once and turn it into some sort of function that > you can >> call inside the loop (it's the eval that is so expensive). How to > do that >> depends a lot on how complex the possible functions can be (if > they'll only >> include 'x*+/-' and numbers, for example, it's not so tricky). > > exp = raw_input('Type expression') > func = eval('lambda x: " + exp) > > print func(42) > > > etc... > > Or if you really can't grokm lambda: > > exec('def func(x): return " + exp) > > should do the same... > > Alan G. > > > From dyoo at hkn.eecs.berkeley.edu Thu Jan 27 02:11:59 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 27 02:12:03 2005 Subject: [Tutor] Convert string to variable name In-Reply-To: <4453924905012616314538eefd@mail.gmail.com> Message-ID: On Wed, 26 Jan 2005, Tony Giunta wrote: > This is something I've been trying to figure out for some time. Is > there a way in Python to take a string [say something from a raw_input] > and make that string a variable name? Hi Tony, Conceptually, yes: you can collect all those on-the-fly variables in a dictionary. > I'm sure I could accomplish something similar using a plain old > dictionary, Yup. *grin* > but I was playing around with the OOP stuff and thought it might be a > neat thing to try out. We can use exec() to create dynamic variable names, but it's almost always a bad idea. Dynamic variable names make it very difficult to isolate where variables are coming from in a program. It'll also prevent us from reliably using the excellent PyChecker program: http://pychecker.sourceforge.net/ So I'd recommend just using a plain old dictionary: it's not exciting, but it's probably the right thing to do. Looking back at the code: ### def generateInstance(): class test: def __init__(self, title, price): self.title = title self.price = price def theTitle(self, title): return self.title def thePrice(self, price): return self.price myName = raw_input("Name: ") myTitle = raw_input("Title: ") myPrice = raw_input("Price: ") exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) ### In Python, we actually can avoid writing attribute getters and setters --- the "JavaBean" interface --- because Python supports a nice feature called "properties". See: http://dirtsimple.org/2004/12/python-is-not-java.html http://www.python.org/2.2.2/descrintro.html#property The upside is that we usually don't need methods like theTitle() or thePrice() in Python. Our revised code looks like: ### class test: def __init__(self, title, price): self.title = title self.price = price instances = {} def generateInstance(): myName = raw_input("Name: ") myTitle = raw_input("Title: ") myPrice = raw_input("Price: ") instances[myName] = test(myTitle, myPrice) ### If you have more questions, please feel free to ask. Best of wishes to you! From keridee at jayco.net Thu Jan 27 02:26:15 2005 From: keridee at jayco.net (Jacob S.) Date: Thu Jan 27 02:25:55 2005 Subject: [Tutor] Unique Items in Lists References: <20050126184401.36156.qmail@web53503.mail.yahoo.com> Message-ID: <001f01c5040f$34a7f450$d55428cf@JSLAPTOP> > col2_set = sets.Set(col2) > how can I get a uniq elements that repeated several > times: > > for item in range(len(list)): > for k in range(len(list)): > if item == k: > if list[item] != k[list]: > print item > First off, this would never work. You are iterating over the same list so go through each step on paper or in your head. Oh, and don't use list for a variable name--it's a builtin function that you don't want to write over. An example a = [1,1,4,2,3,1,5] for item in range(len(a)): for k in range(len(a)): if item == k: if a[item]!=a[k]: ## I think this is what you meant to put--otherwise you would be trying to index an index! print item ## This returns an index, maybe you want the element instead? Ok... here's how it runs with all the variables replaced with the values defined by the loop. if 0 == 0: if 1!=1: print 1 That is okay. if 0 == 1: if 1 != 1: ## The first 1 comes from a[0] (item=0), the second 1 from a[1] (k=1) print 1 ## This is obviously not what you want, so immediately we see a problem. Then it goes through (0,2),(0,3),......(1,0),(1,1)(1,2),........(2,0),(2,1),.... Anyway, this is all irrevelant because sets takes care of doubles. >>> import sets >>> a = sets.Set([1,2,1,4,3,5,5,2,3,3,2]) >>> a Set([1, 2, 3, 4, 5]) >>> list(a) ## This is one of the uses of the builtin list -- to make a >>> list of an iterable. [1, 2, 3, 4, 5] >>> HTH, Jacob > This isnt working either. logic appears correct. > looking forward for help pleas. > > thank you > > Srini > > > > > > > __________________________________ > Do you Yahoo!? > The all-new My Yahoo! - What will yours do? > http://my.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From axg4903 at gmail.com Thu Jan 27 03:06:12 2005 From: axg4903 at gmail.com (Tony Giunta) Date: Thu Jan 27 03:06:15 2005 Subject: [Tutor] Convert string to variable name In-Reply-To: References: <4453924905012616314538eefd@mail.gmail.com> Message-ID: <445392490501261806453eb44d@mail.gmail.com> On Wed, 26 Jan 2005 17:11:59 -0800 (PST), Danny Yoo wrote: > > > On Wed, 26 Jan 2005, Tony Giunta wrote: > > We can use exec() to create dynamic variable names, but it's almost always > a bad idea. Dynamic variable names make it very difficult to isolate > where variables are coming from in a program. Yeah, that's why I was wondering if there was a better way to do it. > So I'd recommend just using a plain old dictionary: it's not exciting, but > it's probably the right thing to do. Thanks. I thought I might have to resort to this. =) > Looking back at the code: > > ### > def generateInstance(): > class test: > def __init__(self, title, price): > self.title = title > self.price = price > def theTitle(self, title): > return self.title > def thePrice(self, price): > return self.price > > myName = raw_input("Name: ") > myTitle = raw_input("Title: ") > myPrice = raw_input("Price: ") > > exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) > ### > > In Python, we actually can avoid writing attribute getters and setters --- > the "JavaBean" interface --- because Python supports a nice feature called > "properties". See: > > http://dirtsimple.org/2004/12/python-is-not-java.html > http://www.python.org/2.2.2/descrintro.html#property Ahh. Thanks for the links, I shall check them out. > The upside is that we usually don't need methods like theTitle() or > thePrice() in Python. Our revised code looks like: > > ### > class test: > def __init__(self, title, price): > self.title = title > self.price = price > > instances = {} > > def generateInstance(): > myName = raw_input("Name: ") > myTitle = raw_input("Title: ") > myPrice = raw_input("Price: ") > instances[myName] = test(myTitle, myPrice) > ### > > If you have more questions, please feel free to ask. Best of wishes to > you! Thanks again Danny. This is exactly what I was looking for. tony From json_white at msn.com Thu Jan 27 03:09:45 2005 From: json_white at msn.com (Jason White) Date: Thu Jan 27 03:10:19 2005 Subject: [Tutor] New to Python Message-ID: An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050126/aeb4049c/attachment.html From maxnoel_fr at yahoo.fr Thu Jan 27 03:21:49 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 27 03:21:54 2005 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: <028e2313378dff6b4e28967bc1abf307@yahoo.fr> On Jan 27, 2005, at 02:09, Jason White wrote: > > I'm curious about good tutorial websites and books to buy. I learned Python (well, the basics thereof -- enough to do useful stuff on my summer job, anyway ^^) in an afternoon using the official tutorial that's found somewhere on http://www.python.org/ . It's very good provided you already have some programming experience (which seems to be your case). I hear that Alan Gauld's tutorial is also very good, but geared more towards people new to programming. You'll find the address in his sig (which itself should be in the post that he sent to the list while I was writing this one, if my predictions are accurate :p ). > I also have a development question for anybody who might know.? The > project I'm working on now to develop my python skills is a prgram to > script control another windows program.? The program doesn't have a > published API so I'll probably need to locate memory addresses data > fields and button routines.? Am I in way over my head for a Python > beginner or does anybody have any advice for where to start poking > around in memory and which python classes I should use to do so? Eeh? Peeks and pokes? Are you even allowed to do that? I mean, doesn't Windows have a protected memory mechanism, like any OS should have nowadays? -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From tameyer at ihug.co.nz Thu Jan 27 03:32:41 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Thu Jan 27 03:32:52 2005 Subject: [Tutor] New to Python In-Reply-To: Message-ID: [Jason White] >> I'm curious about good tutorial websites and books to buy. [Max Noel] > I learned Python (well, the basics thereof -- enough to do useful > stuff on my summer job, anyway ^^) in an afternoon using the official > tutorial that's found somewhere on http://www.python.org/. It's very > good provided you already have some programming experience If you (Jason) are using Windows, then you absolutely want to install Mark Hammond's pywin32 extensions as well as Python itself. If you use PythonWin as your IDE (and you might as well, at least at first), then to get to the tutorial, you can just open PythonWin, and select "Python Manuals" from the Help menu, then click "Tutorial". I absolutely agree that it's the best place to start. >> I also have a development question for anybody who might know.? The >> project I'm working on now to develop my python skills is a prgram to >> script control another windows program.? The program doesn't have a >> published API so I'll probably need to locate memory addresses data >> fields and button routines. There's a Python library for controlling Windows in this sort of way (simulating mouse clicks and so on), although the name escapes me at the moment. However, are you positive that you can't control it properly? Check to see if it has a COM interface (you can use PythonWin to do that), and use that if possible. =Tony.Meyer From python at jayloden.com Thu Jan 27 03:53:19 2005 From: python at jayloden.com (Jay Loden) Date: Thu Jan 27 03:54:58 2005 Subject: [Tutor] New to Python In-Reply-To: References: Message-ID: <200501262153.19778.python@jayloden.com> I also recommend the book "Dive Into Python" - it gets awesome reviews, and the book is under Creative Commons license, so it's free to download and distribute. http://diveintopython.org I also have the book "Core Python Programming" which is pretty good, and has a nice way of leaping right into code so that if you have any prior knowledge, you can use it to learn faster. -Jay On Wednesday 26 January 2005 09:09 pm, Jason White wrote: >



>

Greetings all, I'm new to python and thought I'd pop in here for > advice.

I've done object oriented design and programmed in perl, > java, c++, basic, etc.

I haven't done a lot of development, mostly > just glorified oject-oriented scripts.

I'm curious about good > tutorial websites and books to buy.

>

 

>

I also have a development question for anybody who might know.  The > project I'm working on now to develop my python skills is a prgram to > script control another windows program.  The program doesn't have a > published API so I'll probably need to locate memory addresses data fields > and button routines.  Am I in way over my head for a Python beginner > or does anybody have any advice for where to start poking around in memory > and which python classes I should use to do so?


>
 
From miles at mstevenson.org Thu Jan 27 05:01:19 2005 From: miles at mstevenson.org (Miles Stevenson) Date: Thu Jan 27 05:02:47 2005 Subject: [Tutor] Preffered way to search posix filesystem Message-ID: <200501262301.22914.miles@mstevenson.org> I would like to search filesystem structures using globs on Posix systems from within Python. I don't see an obvious method to do this with in the standard modules. What is the preferred way of doing this? Should I just use the find command or is there a good module out there for searching? Thanks. -- Miles Stevenson miles@mstevenson.org PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050126/eb714a47/attachment.pgp From flaxeater at yahoo.com Thu Jan 27 05:22:59 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 05:23:03 2005 Subject: [Tutor] New to Python Message-ID: <20050127042259.89192.qmail@web54306.mail.yahoo.com> I too once had trouble remembering (and finding) the name of this library so here it is. http://www.tizmoi.net/watsup/intro.html I have not used it but the documentation by example, seemed to me to be approachable. Tony Meyer wrote: >There's a Python library for controlling Windows in this sort of way >(simulating mouse clicks and so on), although the name escapes me at the >moment. However, are you positive that you can't control it properly? >Check to see if it has a COM interface (you can use PythonWin to do that), >and use that if possible. > >=Tony.Meyer > __________________________________ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com From flaxeater at yahoo.com Thu Jan 27 05:27:16 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 05:27:19 2005 Subject: [Tutor] Preffered way to search posix filesystem Message-ID: <20050127042716.1284.qmail@web54307.mail.yahoo.com> Try the os module. I think this should probably get you there. http://docs.python.org/lib/module-os.html Miles Stevenson wrote: >I would like to search filesystem structures using globs on Posix systems from >within Python. I don't see an obvious method to do this with in the standard >modules. What is the preferred way of doing this? Should I just use the find >command or is there a good module out there for searching? > >Thanks. > > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From srini_iyyer_bio at yahoo.com Thu Jan 27 07:17:48 2005 From: srini_iyyer_bio at yahoo.com (Srinivas Iyyer) Date: Thu Jan 27 07:17:51 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: <001f01c5040f$34a7f450$d55428cf@JSLAPTOP> Message-ID: <20050127061748.23946.qmail@web53507.mail.yahoo.com> Dear Jacob, thank you for your suggestion. however, i think my question was not clear. what i meant to ask in my previous question was, how to know which elements repeated and how many times they were repeated. while my question was flying, i did a small test: took a list: >>> a [1, 1, 2, 3, 4, 2, 2] wanted to know which elements repeated and how many times: for i in range(len(a)): for k in range(len(a)): if i != k: if a[i] == a[k]: print a[i] break 1 1 2 2 2 In this very huge list (:-) kidding) of 7 elements, it is easy know by the above primitive code that there 1 s repeated two times, and 2 s repeated three times and finally 1 and 2 numbers are repeated in list a: With sets option i get a list of unique elements, but by no means i get to know which elements were repeated and how many times. With the above primitive code , I know that 1 and 2 are peated. However, in case where there are thousands of entries, how can I get to kno wich elements got repeated and how many times. Do you see any flaws in this code, how can that be made to look more pyhonian way. Hope my question is clear now and appreciate your suggestions. Thank you in advance Srini > >>> import sets > >>> a = sets.Set([1,2,1,4,3,5,5,2,3,3,2]) > >>> a > Set([1, 2, 3, 4, 5]) > >>> list(a) ## This is one of the uses of the > builtin list -- to make a > >>> list of an iterable. > [1, 2, 3, 4, 5] > >>> > > HTH, > Jacob > > > This isnt working either. logic appears correct. > > looking forward for help pleas. > > > > thank you > > > > Srini > > > > > > > > > > > > > > __________________________________ > > Do you Yahoo!? > > The all-new My Yahoo! - What will yours do? > > http://my.yahoo.com > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From flaxeater at yahoo.com Thu Jan 27 07:32:56 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 07:32:59 2005 Subject: [Tutor] Unique Items in Lists Message-ID: <20050127063256.36382.qmail@web54306.mail.yahoo.com> Ok. I think I understand and I happen to be up at 1:30 my time so here is the solution as I understand the problem. This is a very common problem and has a fairly easy solution. You can then take adict.keys() which returns a list of unique elements. Good Luck import random l=[random.randrange(1,20) for x in range(100)] l [7, 18, 17, 17, 6, 11, 14, 9, 4, 16, 2, 9, 3, 13, 4, 2, 5, 15, 15, 3, 3, 11, 18, 12, 6, 8, 15, 3, 7, 9, 9, 7, 12, 11, 11, 9, 19, 19, 15, 2, 17, 18, 16, 8, 15, 3, 19, 19, 19, 1, 3, 17, 3, 8, 16, 1, 5, 19, 17, 16, 19, 6, 3, 8, 16, 11, 12, 7, 10, 13, 13, 11, 6, 2, 18, 15, 17, 8, 12, 13, 5, 12, 2, 19, 2, 19, 7, 10, 4, 14, 15, 14, 5, 1, 16, 1, 9, 10, 17, 12] adict={} for x in l: if adict.has_key(x): #then there is already an item adict[x]+=1 #increment the count by one else: #there is no key the item hasn't been seen adict[x]=1 #there is one instance so far adict {1: 4, 2: 6, 3: 8, 4: 3, 5: 4, 6: 4, 7: 5, 8: 5, 9: 6, 10: 3, 11: 6, 12: 6, 13: 4, 14: 3, 15: 7, 16: 6, 17: 7, 18: 4, 19: 9} Srinivas Iyyer wrote: >Dear Jacob, thank you for your suggestion. > >however, i think my question was not clear. what i >meant to ask in my previous question was, how to know >which elements repeated and how many times they were >repeated. > >while my question was flying, i did a small test: > >took a list: > > >>>>a >>>> >>>> >[1, 1, 2, 3, 4, 2, 2] > >wanted to know which elements repeated and how many >times: > __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 From ps_python at yahoo.com Thu Jan 27 07:35:59 2005 From: ps_python at yahoo.com (kumar s) Date: Thu Jan 27 07:36:03 2005 Subject: [Tutor] Cluster algorithms Message-ID: <20050127063559.65139.qmail@web53702.mail.yahoo.com> Hi: I am still trying to learn the OOPs side of python. however, things/circumstances dont seems to stop until I finish my practise and attaing higher understanding. may be, i am being pushed by circumstances into the stream and i am being tested if I can swim efficiently while I struggle with basic steps of swimming. The 100% analogy my perspective of learning python :-) I have a couple of questions to ask tutors: Are there any example programs depicting Clustering algorithms such as agglomerative, complete link, partional , squared error clustering, k-means or clustering algos based on Neural networks or genetic algorithm. although I just learned python, (to major extent in programming also), I need to apply some of these algos to my data. Any suggestions/recommendations? Do I have to know to code well using OOP methods to apply these algorithms? -Kumar __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 From bvande at po-box.mcgill.ca Thu Jan 27 08:06:21 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 27 08:06:48 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: <20050127061748.23946.qmail@web53507.mail.yahoo.com> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> Message-ID: <41F892ED.90909@po-box.mcgill.ca> Srinivas Iyyer said unto the world upon 2005-01-27 01:17: > Dear Jacob, thank you for your suggestion. > > however, i think my question was not clear. what i > meant to ask in my previous question was, how to know > which elements repeated and how many times they were > repeated. > > while my question was flying, i did a small test: > > took a list: > >>>>a > > [1, 1, 2, 3, 4, 2, 2] > > wanted to know which elements repeated and how many > times: > > for i in range(len(a)): > for k in range(len(a)): > if i != k: > if a[i] == a[k]: > print a[i] > break > > In this very huge list (:-) kidding) of 7 elements, it > is easy know by the above primitive code that there 1 > s repeated two times, and 2 s repeated three times and > finally 1 and 2 numbers are repeated in list a: > > With sets option i get a list of unique elements, but > by no means i get to know which elements were repeated > and how many times. With the above primitive code , I > know that 1 and 2 are peated. However, in case where > there are thousands of entries, how can I get to kno > wich elements got repeated and how many times. > > Do you see any flaws in this code, how can that be > made to look more pyhonian way. > > Hope my question is clear now and appreciate your > suggestions. > > Thank you in advance > Srini Hi Srini, for the task of finding out which items are repeated and how many times, I'd do this: def dups_in_list_report(a_list): '''Prints a duplication report for a list.''' items_dict = {} for i in a_list: if i in items_dict: items_dict[i] = items_dict[i] + 1 else: items_dict[i] = 1 for key in items_dict.copy(): # Try it without the .copy() if items_dict[key] == 1: # and see what happens. del items_dict[key] dict_keys = items_dict.keys() dict_keys.sort() for key in dict_keys: print '%s occurred %s times' %(key, items_dict[key]) f = [1,1,2,3,3,3,3,4,4,4,4,4,4,4,5] dups_in_list_report(f) And, now that I get back on-line, I see that Chad posted the same basic idea. But, perhaps the extra stuff here is of use, too. HTH, Brian vdB From shaleh at speakeasy.net Thu Jan 27 08:13:04 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Thu Jan 27 08:13:51 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: References: Message-ID: <41F89480.7020104@speakeasy.net> Terry Carroll wrote: > My goal here is not efficiency of the code, but efficiency in my Python > thinking; so I'll be thinking, for example, "ah, this should be a list > comprehension" instead of a knee-jerk reaction to use a for loop. > as Alan says, list comprehensions, like map should be used to generate new lists. What you should have thought was -- hmm, this looks like a job for reduce! def sumWater(w1, w2): total_mass = w1.mass + w2.mass numerator = (w1.mass * w1.temperature) + (w2.mass * w2.temperature) return Water(total_mass, numerator / total_mass) def CombineWater(WaterList): return reduce(sumWater, WaterList) if __name__ == '__main__': w1 = Water(50,0) w2 = Water(50,100) w3 = Water(25,50) print CombineWater2((w1,w2,w3)) Now, the sum_water function could also be folded into the Water class as an __add__ method if you like as someone else suggested. See, the problem with your old code was that the list walking and the summation were done by the same code. By pulling sumWater out you can test it separately. You could also pass different functions to CombineWater. And now, for the pedant in me. I would recommend against naming functions with initial capital letters. In many languages, this implies a new type (like your Water class). so CombineWater should be combineWater. From bvande at po-box.mcgill.ca Thu Jan 27 08:21:06 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 27 08:22:37 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: <41F89480.7020104@speakeasy.net> References: <41F89480.7020104@speakeasy.net> Message-ID: <41F89662.4040903@po-box.mcgill.ca> Sean Perry said unto the world upon 2005-01-27 02:13: > And now, for the pedant in me. I would recommend against naming > functions with initial capital letters. In many languages, this implies > a new type (like your Water class). so CombineWater should be combineWater. Do you mean implies by the dominant coding conventions, or by language syntax? (Indulging the curious pedant in me.) Best, Brian vdB From shaleh at speakeasy.net Thu Jan 27 08:57:25 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Thu Jan 27 08:58:13 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: <41F89662.4040903@po-box.mcgill.ca> References: <41F89480.7020104@speakeasy.net> <41F89662.4040903@po-box.mcgill.ca> Message-ID: <41F89EE5.9000900@speakeasy.net> Brian van den Broek wrote: > Sean Perry said unto the world upon 2005-01-27 02:13: > > > >> And now, for the pedant in me. I would recommend against naming >> functions with initial capital letters. In many languages, this implies >> a new type (like your Water class). so CombineWater should be >> combineWater. > > > Do you mean implies by the dominant coding conventions, or by language > syntax? (Indulging the curious pedant in me.) > In many OO languages, it is tradition to name types with capital letters (TFoo anyone?) and functions / methods with initial small then caps (camelCase). Haskell actually enforces this rule (nifty functional language). Python and it share a lot in common. (As an anti-example) Erlang enforces capital letters for variable names ONLY. A little odd and definitely takes some getting used to. From kraus at hagen-partner.de Thu Jan 27 09:24:57 2005 From: kraus at hagen-partner.de (Wolfram Kraus) Date: Thu Jan 27 09:25:19 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <41F892ED.90909@po-box.mcgill.ca> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> Message-ID: Brian van den Broek wrote: [...] > Hi Srini, > > for the task of finding out which items are repeated and how many times, > I'd do this: > > > def dups_in_list_report(a_list): > '''Prints a duplication report for a list.''' > > items_dict = {} > > for i in a_list: > if i in items_dict: > items_dict[i] = items_dict[i] + 1 > else: > items_dict[i] = 1 get(key, default) is your friend here: for i in a_list: items_dict[i] = items_dict.get(i, 0) + 1 get() (and his "brother" setdefault()) are mighty dictionary-methods. > for key in items_dict.copy(): # Try it without the .copy() > if items_dict[key] == 1: # and see what happens. > del items_dict[key] > > dict_keys = items_dict.keys() > dict_keys.sort() > > for key in dict_keys: > print '%s occurred %s times' %(key, items_dict[key]) This whole part can be rewritten (without sorting, but in Py2.4 you can use sorted() for this) with a list comprehension (Old Python2.1 style, with a newer version the keys() aren't needed): for k,v in [(k, items_dict[k]) \ for k in items_dict.keys() if items_dict[k] > 1]: print '%s occurred %s times' %(key, items_dict[key]) > f = [1,1,2,3,3,3,3,4,4,4,4,4,4,4,5] > > dups_in_list_report(f) > > > And, now that I get back on-line, I see that Chad posted the same basic > idea. But, perhaps the extra stuff here is of use, too. You can apply the get() there, too ;-) > HTH, > > Brian vdB HTH, Wolfram From flaxeater at yahoo.com Thu Jan 27 09:43:40 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 09:43:45 2005 Subject: [Tutor] Re: Unique Items in Lists Message-ID: <20050127084341.81039.qmail@web54306.mail.yahoo.com> Ok you got me thinking. I used the same thing I posted before but created a one liner that as a side affect makes adict like before. [adict.__setitem__(x,adict.get(x,0)+1) for x in l] I think it's kinda funny and ugly, ohh and not particuarly clear about what it does. Wolfram Kraus wrote: > get(key, default) is your friend here: > for i in a_list: > items_dict[i] = items_dict.get(i, 0) + 1 > > get() (and his "brother" setdefault()) are mighty dictionary-methods. > __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo From kraus at hagen-partner.de Thu Jan 27 09:50:19 2005 From: kraus at hagen-partner.de (Wolfram Kraus) Date: Thu Jan 27 09:50:25 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <20050127084341.81039.qmail@web54306.mail.yahoo.com> References: <20050127084341.81039.qmail@web54306.mail.yahoo.com> Message-ID: Chad Crabtree wrote: > Ok you got me thinking. I used the same thing I posted before but > created a one liner that as a side affect makes adict like before. > [adict.__setitem__(x,adict.get(x,0)+1) for x in l] > > I think it's kinda funny and ugly, ohh and not particuarly clear > about what it does. > Uhh, list-comprehensions with side effects. Must..... resist..... And now write the Zen of Python 10 times ;-) Greetings, Wolfram From bvande at po-box.mcgill.ca Thu Jan 27 09:59:05 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 27 10:03:44 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> Message-ID: <41F8AD59.9070803@po-box.mcgill.ca> Wolfram Kraus said unto the world upon 2005-01-27 03:24: > Brian van den Broek wrote: Thanks Wolfram, I knew someone would improve what I posted. (It can always be done ;-) >> for i in a_list: >> if i in items_dict: >> items_dict[i] = items_dict[i] + 1 >> else: >> items_dict[i] = 1 > > get(key, default) is your friend here: > for i in a_list: > items_dict[i] = items_dict.get(i, 0) + 1 > > get() (and his "brother" setdefault()) are mighty dictionary-methods. What a brain fart I had! I even got as far as reading then entry in the Library Ref Mapping Types table for setdefault, 'cause I knew there was a way to do this, but couldn't recall it. How they expect me to find it when they hide it on the previous line I'll never know ;-) / :-[ >> for key in items_dict.copy(): # Try it without the .copy() >> if items_dict[key] == 1: # and see what happens. >> del items_dict[key] >> >> dict_keys = items_dict.keys() >> dict_keys.sort() > > > > >> for key in dict_keys: >> print '%s occurred %s times' %(key, items_dict[key]) > > This whole part can be rewritten (without sorting, but in Py2.4 you can > use sorted() for this) with a list comprehension (Old Python2.1 style, > with a newer version the keys() aren't needed): > for k,v in [(k, items_dict[k]) \ > for k in items_dict.keys() if items_dict[k] > 1]: > print '%s occurred %s times' %(key, items_dict[key]) I knew sorted() only from the 'What's New in 2.4'. Thanks for reminding me to look closer. I get that the for loops in mine are more costly than the list comps in yours. But, I can't help but feel that the list comp isn't as clear or readable. Of course, that may say more about me (and the fact that while I can read list comps, they aren't really in my working vocabulary yet) than it does about the code. I think I like the way I wrote it in that it breaks the discrete steps apart. But, for long lists, that will be a performance bite indeed. (I went from my first posts being too concerned about speed to not giving a toss. Perhaps the pendulum needs to swing back a bit.) Anyway, thanks for showing me (and the OP) these alternatives! Best, Brian vdB From alan.gauld at freenet.co.uk Thu Jan 27 10:21:51 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 10:21:33 2005 Subject: [Tutor] Convert string to variable name References: <4453924905012616314538eefd@mail.gmail.com> Message-ID: <010f01c50451$a2c21e60$36bd8651@xp> > This is something I've been trying to figure out for some time. Is > there a way in Python to take a string [say something from a > raw_input] and make that string a variable name? I want to to this so > that I can create class instances on-the-fly, using a user-entered > string as the instance name. This comes up regularly from beginners and is nearly always a bad idea! The easy solution is to use a dictionary to store the instances. See the OOP topic in my tutor for an example using bankAccounts as the objects... > could accomplish something similar using a plain old dictionary, but I > was playing around with the OOP stuff and thought it might be a neat > thing to try out. A Dictionary is best. The problem is, once you create your new variables none of the rest of your code knows aout them so how can it access them. And what if someone enters a name that is being iused elsewhere in your code? You will overwrite a real variable with this new object! Very tricky to control. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From flaxeater at yahoo.com Thu Jan 27 10:23:12 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 10:23:15 2005 Subject: [Tutor] Syntax Check Message-ID: <20050127092312.37118.qmail@web54304.mail.yahoo.com> Does anyone happen to know how to turn of the syntax checking in python? I've been working on a module driven preprocessor but I'd like to not have to use comment strings. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From alan.gauld at freenet.co.uk Thu Jan 27 10:32:04 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 10:31:52 2005 Subject: [Tutor] New to Python References: Message-ID: <012e01c50453$10784000$36bd8651@xp> > Greetings all, I'm new to python and thought I'd pop in here for advice. Good decisions both :-) > I've done object oriented design and programmed in perl, java, c++, basic, etc. > ... > I'm curious about good tutorial websites and books to buy. With your background the standard Python tutorial that comes with the documentation should be ideal. Any extra questions just ask here. There are lists of recommended books available in several places. Personally I'd suggest trying the official docs until you know where your interests lie, except.... > ...a prgram to script control another windows program. ...doing anything on Windows you owe it to yourself to buy Mark Hammonds book "Python Programming on Win32"! > The program doesn't have a published API so I'll probably need > to locate memory addresses data fields and button routines. Usually you can drive Windows programs indirectly by posting windows messages to them. Thus sending a mouse click message to a particular control will trigger the event handler etc. You can use a Windows Spy program to moinitor the messages sent in manual processing and replicate them programatically. Its tedious and painful but more reliable than using memory locations! You should download the ActiveState version of Python too - it has several Windows biased goodies on top of the official Python distro... > Am I in way over my head for a Python beginner or does > anybody have any advice for where to start poking around Its ambitious and I'd try a few more simple things first but its not too ridiculous given your C++ background.(Especially if that includes Win32 C++) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Thu Jan 27 10:34:25 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 10:34:31 2005 Subject: [Tutor] New to Python References: <028e2313378dff6b4e28967bc1abf307@yahoo.fr> Message-ID: <013301c50453$647d7f30$36bd8651@xp> > I hear that Alan Gauld's tutorial is also very good, but geared more > towards people new to programming. Yes, I try to take folks to the point where they can understand the official tutor (well maybe a wee bit further than that, but that was the original target...) > (which itself should be in the post that he sent to the > list while I was writing this one, if my predictions are > accurate :p ). Almost, but I get the digest so my posts are usually a wee bit behind everyone else! :-) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Thu Jan 27 10:38:03 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 10:37:47 2005 Subject: [Tutor] Unique Items in Lists References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> Message-ID: <014901c50453$e66802e0$36bd8651@xp> > for i in range(len(a)): > for k in range(len(a)): for k in range(i,len(a)): is faster, and if you calculate len before starting the loops that will speed it up too. (You calculate len for each iteration of each loop!) > if i != k: > if a[i] == a[k]: > print a[i] > break HTH Alan G. From alan.gauld at freenet.co.uk Thu Jan 27 11:05:11 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 11:05:04 2005 Subject: [Tutor] Should this be a list comprehension or something? References: <41F89480.7020104@speakeasy.net> <41F89662.4040903@po-box.mcgill.ca> Message-ID: <016401c50457$b07a5440$36bd8651@xp> > > functions with initial capital letters. In many languages, this implies > > a new type (like your Water class). so CombineWater should be combineWater. > > Do you mean implies by the dominant coding conventions, or by language > syntax? (Indulging the curious pedant in me.) Coding convention. Its like using UPPERCASE for constants. If you see a name captalised it means is a class not a function. Python doesn't care but it makes it more readable. And its a convention that started in Smalltalk (I believe) but has spread to most OO languages. Alan G. From alan.gauld at freenet.co.uk Thu Jan 27 11:08:30 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 11:09:31 2005 Subject: [Tutor] Syntax Check References: <20050127092312.37118.qmail@web54304.mail.yahoo.com> Message-ID: <017501c50458$27ae1380$36bd8651@xp> > Does anyone happen to know how to turn of the syntax checking in > python? I've been working on a module driven preprocessor but I'd > like to not have to use comment strings. So don't use them! They aren't mandatory. I'm not sure I understand youir problem? Why would turning off syntax checking ever help? Alan G. From kent37 at tds.net Thu Jan 27 11:45:52 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 27 11:45:58 2005 Subject: [Tutor] Cluster algorithms In-Reply-To: <20050127063559.65139.qmail@web53702.mail.yahoo.com> References: <20050127063559.65139.qmail@web53702.mail.yahoo.com> Message-ID: <41F8C660.9030300@tds.net> Google is your friend: Googling 'python clustering algorithm' gives many hits that seem to have what you are looking for. Kent kumar s wrote: > Hi: > > I am still trying to learn the OOPs side of python. > however, things/circumstances dont seems to stop until > I finish my practise and attaing higher understanding. > may be, i am being pushed by circumstances into the > stream and i am being tested if I can swim efficiently > while I struggle with basic steps of swimming. The > 100% analogy my perspective of learning python :-) > > > I have a couple of questions to ask tutors: > > Are there any example programs depicting Clustering > algorithms such as agglomerative, complete link, > partional , squared error clustering, k-means or > clustering algos based on Neural networks or genetic > algorithm. although I just learned python, (to major > extent in programming also), I need to apply some of > these algos to my data. Any > suggestions/recommendations? > > > Do I have to know to code well using OOP methods to > apply these algorithms? > > > -Kumar > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - Easier than ever with enhanced search. Learn more. > http://info.mail.yahoo.com/mail_250 > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Thu Jan 27 11:57:20 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 27 11:57:26 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <41F8AD59.9070803@po-box.mcgill.ca> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> <41F8AD59.9070803@po-box.mcgill.ca> Message-ID: <41F8C910.1050202@tds.net> Brian van den Broek wrote: > Wolfram Kraus said unto the world upon 2005-01-27 03:24: > >> Brian van den Broek wrote: >>> for key in items_dict.copy(): # Try it without the .copy() >>> if items_dict[key] == 1: # and see what happens. >>> del items_dict[key] >>> >>> dict_keys = items_dict.keys() >>> dict_keys.sort() >> >> >>> for key in dict_keys: >>> print '%s occurred %s times' %(key, items_dict[key]) >> >> >> This whole part can be rewritten (without sorting, but in Py2.4 you >> can use sorted() for this) with a list comprehension (Old Python2.1 >> style, with a newer version the keys() aren't needed): >> for k,v in [(k, items_dict[k]) \ >> for k in items_dict.keys() if items_dict[k] > 1]: >> print '%s occurred %s times' %(key, items_dict[key]) I think it is clearer to filter the list as it is printed. And dict.iteritems() is handy here, too. for k, v in items_dict.iteritems(): if v > 1: print '%s occurred %s times' % (k, v) Kent From javier at ruere.com.ar Thu Jan 27 13:55:45 2005 From: javier at ruere.com.ar (Javier Ruere) Date: Thu Jan 27 13:45:23 2005 Subject: [Tutor] Re: Syntax Check In-Reply-To: <20050127092312.37118.qmail@web54304.mail.yahoo.com> References: <20050127092312.37118.qmail@web54304.mail.yahoo.com> Message-ID: Chad Crabtree wrote: > Does anyone happen to know how to turn of the syntax checking in > python? I've been working on a module driven preprocessor but I'd > like to not have to use comment strings. I don't think that's possible. What would the compiler do with code with an invalid syntax? What's a module driven preprocessor? Javier From jhomme at libcom.com Thu Jan 27 14:01:57 2005 From: jhomme at libcom.com (jhomme) Date: Thu Jan 27 14:03:50 2005 Subject: [Tutor] Compile Only Message-ID: -----Original message----- From: "Alan Gauld" alan.gauld@freenet.co.uk Date: Thu, 27 Jan 2005 05:08:07 -0500 To: "Chad Crabtree" flaxeater@yahoo.com Subject: Re: [Tutor] Syntax Check > > Does anyone happen to know how to turn of the syntax checking in > > python? I've been working on a module driven preprocessor but I'd > > like to not have to use comment strings. > > So don't use them! They aren't mandatory. > I'm not sure I understand youir problem? Why would turning > off syntax checking ever help? > > Alan G. > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Hi, >From the command line, is there a way to run Python against a program so that it stops after it compiles even if the syntax is OK? Thanks. Jim From kent37 at tds.net Thu Jan 27 14:20:32 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 27 14:21:30 2005 Subject: [Tutor] Compile Only In-Reply-To: References: Message-ID: <41F8EAA0.6070208@tds.net> If your program is written so it doesn't do anything when imported, you could use > python -c "import myprogram" myprogram.py should have a structure like this: def main(): # do something... if __name__ == '__main__': main() The "if __name__ ..." prevents the module from doing anything when it is imported; main() will only be run when the module is run directly. Kent jhomme wrote: > -----Original message----- > From: "Alan Gauld" alan.gauld@freenet.co.uk > Date: Thu, 27 Jan 2005 05:08:07 -0500 > To: "Chad Crabtree" flaxeater@yahoo.com > Subject: Re: [Tutor] Syntax Check > > >>>Does anyone happen to know how to turn of the syntax checking in >>>python? I've been working on a module driven preprocessor but I'd >>>like to not have to use comment strings. >> >>So don't use them! They aren't mandatory. >>I'm not sure I understand youir problem? Why would turning >>off syntax checking ever help? >> >>Alan G. >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor > > Hi, >>From the command line, is there a way to run Python against a program so that it stops after it compiles even if the syntax is OK? > > Thanks. > > Jim > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From bill.mill at gmail.com Thu Jan 27 14:47:57 2005 From: bill.mill at gmail.com (Bill Mill) Date: Thu Jan 27 14:48:01 2005 Subject: [Tutor] Cluster algorithms In-Reply-To: <20050127063559.65139.qmail@web53702.mail.yahoo.com> References: <20050127063559.65139.qmail@web53702.mail.yahoo.com> Message-ID: <797fe3d405012705471cb9ddfd@mail.gmail.com> Kumar, On Wed, 26 Jan 2005 22:35:59 -0800 (PST), kumar s wrote: > Hi: > > I am still trying to learn the OOPs side of python. > however, things/circumstances dont seems to stop until > I finish my practise and attaing higher understanding. > may be, i am being pushed by circumstances into the > stream and i am being tested if I can swim efficiently > while I struggle with basic steps of swimming. The > 100% analogy my perspective of learning python :-) > > I have a couple of questions to ask tutors: > > Are there any example programs depicting Clustering > algorithms such as agglomerative, complete link, > partional , squared error clustering, k-means or > clustering algos based on Neural networks or genetic > algorithm. although I just learned python, (to major > extent in programming also), I need to apply some of > these algos to my data. Any > suggestions/recommendations? > I wrote a tutorial on using perceptrons, and gave python sample code in the article. You can see the article at http://www.kuro5hin.org/story/2003/11/11/17383/475 . Perceptrons probably aren't *useful* to you in sorting your data, but the code should provide some insight into how you can do classification in python. Python is excellent for data mining and classification, thanks to the excellent numarray module as well as the ipython shell and several visualization libraries. I use it in place of Maple, Mathematica, Matlab, or some other specialized mathematics package. > Do I have to know to code well using OOP methods to > apply these algorithms? > I call some methods on matrix objects in the tutorial, but don't use any OOP. In fact, I often find that strict OOP is not so useful for numerical algorithms, where your code will often have to be optimized for speed. Peace Bill Mill bill.mill at gmail.com From kim.branson at gmail.com Thu Jan 27 15:49:50 2005 From: kim.branson at gmail.com (Kim Branson) Date: Thu Jan 27 15:50:01 2005 Subject: [Tutor] Cluster algorithms In-Reply-To: <797fe3d405012705471cb9ddfd@mail.gmail.com> References: <20050127063559.65139.qmail@web53702.mail.yahoo.com> <797fe3d405012705471cb9ddfd@mail.gmail.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 >> >> >> Are there any example programs depicting Clustering >> algorithms such as agglomerative, complete link, >> partional , squared error clustering, k-means or >> clustering algos based on Neural networks or genetic >> algorithm. although I just learned python, (to major >> extent in programming also), I need to apply some of >> these algos to my data. Any >> suggestions/recommendations? >> > Have you looked at the orange machine learning tool kit, many clustering, classification and learning tools are implemented in this package. PLus it has a nice widget style interface now where you can build your own tools. (if you like to use widgets, or you can just write you own code calling it). Its all GPL and its pretty fantastic stuff. http://magix.fri.uni-lj.si/orange/ good luck kim > Dr Kim Branson Diffraction and Theory CSIRO Health Sciences and Nutrition 343 Royal Parade, Parkville Melbourne Ph +613 9662 7136 kim.branson@csiro.au -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFB+P+Oer2hmGbHcokRApb0AJ91FNkREGPCawfRFQqP4/HcDp+QEACfTSTO VxKX/E3bY/Y8WKCHOM+ydQU= =fEya -----END PGP SIGNATURE----- From srini_iyyer_bio at yahoo.com Thu Jan 27 17:29:10 2005 From: srini_iyyer_bio at yahoo.com (Srinivas Iyyer) Date: Thu Jan 27 17:29:15 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: Message-ID: <20050127162911.37915.qmail@web53506.mail.yahoo.com> Dear Danny, thank you for ur help. But a basic question ? In a table, more specifically a matrix 3X3, Apple Fruit Denmark F-16 Fighter USA Taj Wonder India Mummy Antique Egypt IF I have to sort on country, it should be Apple Fruit Denmark Mummy Antique Egypt Taj Wonder India F-16 Fighter USA How can I sort by binding Denmark to fruit and apple as a string. String does not support sort function. Sincerly I dont know how to sort to follow your suggestion. can you please help.. sorry for asking basic question. thank you --- Danny Yoo wrote: > > > On Wed, 26 Jan 2005, Srinivas Iyyer wrote: > > > I have a list with 4 columns and column1 elements > are unique. I wanted > > to extract unique elements in column3 and and > place the other elements > > of the column along with unique elements in column > 4 as a tab delim > > text. > > > > Table: > > > > col1 col2 col3 col4 > > A Apple 5 Chennai > > B Baby 11 Delhi > > I Baby* 1 Delhi > > M Dasheri+ 5 Mumbai > > K Apple 12 Copenhagen > > > [Meta: we seem to be getting a run of similar > questions this week. Scott > Melnyk also asked about grouping similar records > together: > http://mail.python.org/pipermail/tutor/2005-January/035185.html.] > > > Hi Srinivas, > > I see that you are trying to group records based on > some criterion. You > may find the problem easier to do if you fist do a > sort on that criterion > column: that will make related records "clump" > together. > > > For your sample data above, if we sort against the > second column, the > records will end up in the following order: > > ### > A Apple 5 Chennai > K Apple 12 Copenhagen > B Baby 11 Delhi > I Baby 1 Delhi > M Dasheri 5 Mumbai > ### > > > In this sorting approach, you can then run through > the sorted list in > order. Since all the related elements should be > adjacent, grouping > related lines together should be much easier, and > you should be able to > produce the final output: > > ### > Apple A,K 5,12 Chennai,Copenhagen > Baby B,I 1,11 Delhi > Dasheri M 5 Mumbai > ### > > without too much trouble. You can do this problem > without dictionaries at > all, although you may find the dictionary approach a > little easier to > implement. > > > > > > A dictionary option does not work > > A dictionary approach is also very possible. The > thing you may be stuck > on is trying to make a key associate with multiple > values. Most examples > of dictionaries in tutorials use strings as both the > keys and values, but > dictionaries are more versatile: we can also make a > dictionary whose > values are lists. > > > For example, here is a small program that groups > words by their first > letters: > > ### > >>> def groupAlpha(words): > ... groups = {} > ... for w in words: > ... firstLetter = w[0] > ... if firstLetter not in groups: > ... groups[firstLetter] = [] > ... groups[firstLetter].append(w) > ... return groups > ... > >>> groupAlpha("this is a test of the emergency > broadcast system".split()) > {'a': ['a'], > 'b': ['broadcast'], > 'e': ['emergency'], > 'i': ['is'], > 'o': ['of'], > 's': ['system'], > 't': ['this', 'test', 'the']} > ### > > > If you have more questions, please feel free to ask. > > __________________________________ Do you Yahoo!? Meet the all-new My Yahoo! - Try it today! http://my.yahoo.com From flaxeater at yahoo.com Thu Jan 27 17:41:50 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 17:41:54 2005 Subject: [Tutor] Syntax Check Message-ID: <20050127164151.40660.qmail@web54309.mail.yahoo.com> I'm trying to make a macro system that's work by just doing this import macro class withMacro(prefixMacro): pass mac=withMacro() mac.pattern("item key") macro.expand() g=[] """ with g: .append('a') .append('b') .append(123) """ I would like to not have to use the comment strings. So when expand() is called it sucks in the file toeknizes it and expands the macro then runs the resulting modified code. Alan Gauld wrote: >>Does anyone happen to know how to turn of the syntax checking in >>python? I've been working on a module driven preprocessor but I'd >>like to not have to use comment strings. >> >> > >So don't use them! They aren't mandatory. >I'm not sure I understand youir problem? Why would turning >off syntax checking ever help? > >Alan G. > __________________________________ Do you Yahoo!? Meet the all-new My Yahoo! - Try it today! http://my.yahoo.com From bill.mill at gmail.com Thu Jan 27 17:44:02 2005 From: bill.mill at gmail.com (Bill Mill) Date: Thu Jan 27 17:44:06 2005 Subject: [Tutor] Unique Items in Lists In-Reply-To: <20050127162911.37915.qmail@web53506.mail.yahoo.com> References: <20050127162911.37915.qmail@web53506.mail.yahoo.com> Message-ID: <797fe3d405012708442b47f34@mail.gmail.com> Srinivas, You can't sort a string, since it's immutable. You can, however, sort a list. To sort your table by the third element, you can do something like this: >>> table = (("apple", "fruit", "denmark"), ... ("mummy", "antique", "egypt"), ... ("taj", "wonder", "india"), ... ("f-16", "fighter", "usa")) >>> sorter = [(elt[2], elt) for elt in table] >>> sorter.sort() >>> tuple([elt[1] for elt in sorter]) (('apple', 'fruit', 'denmark'), ('mummy', 'antique', 'egypt'), ('taj', 'wonder', 'india'), ('f-16', 'fighter', 'usa')) # I edited the table output edited for clarity When you sort a list of tuples, the default is to sort the list by the first element in the tuples. If you make a list where the element you want to sort on is first in all of the tuples (see the 'sorter = ...' line), then sort that list, then remove the element you added (the 'tuple([...])' line), you are left with a list which is ordered the way you want it. Peace Bill Mill bill.mill at gmail.com On Thu, 27 Jan 2005 08:29:10 -0800 (PST), Srinivas Iyyer wrote: > Dear Danny, thank you for ur help. But a basic > question ? > > In a table, more specifically a matrix 3X3, > > Apple Fruit Denmark > F-16 Fighter USA > Taj Wonder India > Mummy Antique Egypt > > IF I have to sort on country, it should be > > Apple Fruit Denmark > Mummy Antique Egypt > Taj Wonder India > F-16 Fighter USA > > How can I sort by binding Denmark to fruit and apple > as a string. String does not support sort function. > Sincerly I dont know how to sort to follow your > suggestion. can you please help.. sorry for asking > basic question. > > thank you > > > --- Danny Yoo wrote: > > > > > > > On Wed, 26 Jan 2005, Srinivas Iyyer wrote: > > > > > I have a list with 4 columns and column1 elements > > are unique. I wanted > > > to extract unique elements in column3 and and > > place the other elements > > > of the column along with unique elements in column > > 4 as a tab delim > > > text. > > > > > > Table: > > > > > > col1 col2 col3 col4 > > > A Apple 5 Chennai > > > B Baby 11 Delhi > > > I Baby* 1 Delhi > > > M Dasheri+ 5 Mumbai > > > K Apple 12 Copenhagen > > > > > > [Meta: we seem to be getting a run of similar > > questions this week. Scott > > Melnyk also asked about grouping similar records > > together: > > > http://mail.python.org/pipermail/tutor/2005-January/035185.html.] > > > > > > Hi Srinivas, > > > > I see that you are trying to group records based on > > some criterion. You > > may find the problem easier to do if you fist do a > > sort on that criterion > > column: that will make related records "clump" > > together. > > > > > > For your sample data above, if we sort against the > > second column, the > > records will end up in the following order: > > > > ### > > A Apple 5 Chennai > > K Apple 12 Copenhagen > > B Baby 11 Delhi > > I Baby 1 Delhi > > M Dasheri 5 Mumbai > > ### > > > > > > In this sorting approach, you can then run through > > the sorted list in > > order. Since all the related elements should be > > adjacent, grouping > > related lines together should be much easier, and > > you should be able to > > produce the final output: > > > > ### > > Apple A,K 5,12 Chennai,Copenhagen > > Baby B,I 1,11 Delhi > > Dasheri M 5 Mumbai > > ### > > > > without too much trouble. You can do this problem > > without dictionaries at > > all, although you may find the dictionary approach a > > little easier to > > implement. > > > > > > > > > > > A dictionary option does not work > > > > A dictionary approach is also very possible. The > > thing you may be stuck > > on is trying to make a key associate with multiple > > values. Most examples > > of dictionaries in tutorials use strings as both the > > keys and values, but > > dictionaries are more versatile: we can also make a > > dictionary whose > > values are lists. > > > > > > For example, here is a small program that groups > > words by their first > > letters: > > > > ### > > >>> def groupAlpha(words): > > ... groups = {} > > ... for w in words: > > ... firstLetter = w[0] > > ... if firstLetter not in groups: > > ... groups[firstLetter] = [] > > ... groups[firstLetter].append(w) > > ... return groups > > ... > > >>> groupAlpha("this is a test of the emergency > > broadcast system".split()) > > {'a': ['a'], > > 'b': ['broadcast'], > > 'e': ['emergency'], > > 'i': ['is'], > > 'o': ['of'], > > 's': ['system'], > > 't': ['this', 'test', 'the']} > > ### > > > > > > If you have more questions, please feel free to ask. > > > > > > __________________________________ > Do you Yahoo!? > Meet the all-new My Yahoo! - Try it today! > http://my.yahoo.com > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at freenet.co.uk Thu Jan 27 18:13:49 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Thu Jan 27 18:14:31 2005 Subject: [Tutor] Syntax Check References: <20050127164151.40660.qmail@web54309.mail.yahoo.com> Message-ID: <019301c50493$925d7870$36bd8651@xp> > I'm trying to make a macro system that's work by just doing this OK, But to get your acros to use a language extension to python (the with.... syntax) you are going to have to write your own language interpreter/parser. Even if that just turns the 'with' stuff into Python. So why is the """ marker even necessary? Your interpreter doesn't need it - although it might make things easier... I'm still confused I think. Alan G. > > import macro > class withMacro(prefixMacro): > pass > mac=withMacro() > mac.pattern("item key") > macro.expand() > > g=[] > """ > with g: > .append('a') > .append('b') > .append(123) > """ > > I would like to not have to use the comment strings. So when > expand() > is called it sucks in the file toeknizes it and expands the macro > then > runs the resulting modified code. > > Alan Gauld wrote: > > >>Does anyone happen to know how to turn of the syntax checking in > >>python? I've been working on a module driven preprocessor but I'd > >>like to not have to use comment strings. > >> > >> > > > >So don't use them! They aren't mandatory. > >I'm not sure I understand youir problem? Why would turning > >off syntax checking ever help? > > > >Alan G. > > > > > > > __________________________________ > Do you Yahoo!? > Meet the all-new My Yahoo! - Try it today! > http://my.yahoo.com > > From miles at mstevenson.org Thu Jan 27 19:08:05 2005 From: miles at mstevenson.org (Miles Stevenson) Date: Thu Jan 27 19:09:08 2005 Subject: [Tutor] Safely buffering user input Message-ID: <200501271308.10433.miles@mstevenson.org> Newbie question. I'm trying to practice safe coding techniques. I just want to make sure that a user can't supply a massive argument to my script and cause trouble. I'm just trying only accept about 256 bytes: buffer(sys.argv[1], 0, 256) searchpath = sys.argv[1] The script runs successfully, but when I give the script much more than 256 characters, it doesn't seem to chop it off. It still holds the entire long string and prints the whole thing back out again when I do a: print searchpath I assume the size arguments to buffer() are in bytes? Am I not using this correctly? Thanks in advance. -- Miles Stevenson miles@mstevenson.org PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://mail.python.org/pipermail/tutor/attachments/20050127/f75a1d92/attachment.pgp From bill.mill at gmail.com Thu Jan 27 19:17:20 2005 From: bill.mill at gmail.com (Bill Mill) Date: Thu Jan 27 19:17:25 2005 Subject: [Tutor] Safely buffering user input In-Reply-To: <200501271308.10433.miles@mstevenson.org> References: <200501271308.10433.miles@mstevenson.org> Message-ID: <797fe3d4050127101712202a3d@mail.gmail.com> Miles, On Thu, 27 Jan 2005 13:08:05 -0500, Miles Stevenson wrote: > Newbie question. > > I'm trying to practice safe coding techniques. I just want to make sure that a > user can't supply a massive argument to my script and cause trouble. I'm just > trying only accept about 256 bytes: > > buffer(sys.argv[1], 0, 256) > searchpath = sys.argv[1] I've never used buffer(); in fact, I didn't even know it existed, and I've been using python for a while now. Instead of using buffer, just do: sys.argv[1] = sys.argv[1][:255] This says "Set the second element of sys.argv equal to its first 256 characters". Peace Bill Mill bill.mill at gmail.com From maxnoel_fr at yahoo.fr Thu Jan 27 19:28:06 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Thu Jan 27 19:28:29 2005 Subject: [Tutor] Safely buffering user input In-Reply-To: <797fe3d4050127101712202a3d@mail.gmail.com> References: <200501271308.10433.miles@mstevenson.org> <797fe3d4050127101712202a3d@mail.gmail.com> Message-ID: <8cdb6f177d7f301edbb7e996d0a4ee81@yahoo.fr> On Jan 27, 2005, at 18:17, Bill Mill wrote: > I've never used buffer(); in fact, I didn't even know it existed, and > I've been using python for a while now. > > Instead of using buffer, just do: > > sys.argv[1] = sys.argv[1][:255] > > This says "Set the second element of sys.argv equal to its first 256 > characters". Also, I don't think you have to worry about buffer overflows in Python, unless you're using a seriously broken implementation of it. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From dyoo at hkn.eecs.berkeley.edu Thu Jan 27 20:54:01 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Thu Jan 27 20:54:05 2005 Subject: [Tutor] Safely buffering user input In-Reply-To: <200501271308.10433.miles@mstevenson.org> Message-ID: On Thu, 27 Jan 2005, Miles Stevenson wrote: > I'm trying to practice safe coding techniques. I just want to make sure > that a user can't supply a massive argument to my script and cause > trouble. I'm just trying only accept about 256 bytes: > > buffer(sys.argv[1], 0, 256) ^^^^^^ Hi Miles, Don't use buffer() in this way: it's not doing what you think it's doing. buffer() does not "mutate" its argument: it does not truncate sys.argv[1]. Instead, it takes an existing sequence and provides a sort of "window" view into that sequence. You can try something like: ### window = buffer(sys.argv[1], 0, 256) ### in which case 'window' here should contain the first 256 characters of sys.argv[1]. As a side note, buffer() does not appear to be used much by people: it's hardly used by the Standard Library itself, and is probably not useful for general Python programming. (In fact, the only place I see buffer() really being used is in Python's test cases!) In the case with sys.argv[1], I'd actually leave string arguments at an unrestricted length. Python is very safe when it comes to dealing with large data. For example, array access is checked at runtime: ### >>> "hello world"[400] Traceback (most recent call last): File "", line 1, in ? IndexError: string index out of range ### This is why we're not so worried about things like buffer-overflow in unsafe languages like C. If you have more questions, please feel free to ask. Hope this helps! From bvande at po-box.mcgill.ca Thu Jan 27 21:19:42 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Thu Jan 27 21:20:11 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <41F8C910.1050202@tds.net> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> <41F8AD59.9070803@po-box.mcgill.ca> <41F8C910.1050202@tds.net> Message-ID: <41F94CDE.8040101@po-box.mcgill.ca> Kent Johnson said unto the world upon 2005-01-27 05:57: > Brian van den Broek wrote: > >> Wolfram Kraus said unto the world upon 2005-01-27 03:24: >>> This whole part can be rewritten (without sorting, but in Py2.4 you >>> can use sorted() for this) with a list comprehension (Old Python2.1 >>> style, with a newer version the keys() aren't needed): >>> for k,v in [(k, items_dict[k]) \ >>> for k in items_dict.keys() if items_dict[k] > 1]: >>> print '%s occurred %s times' %(key, items_dict[key]) > > > I think it is clearer to filter the list as it is printed. And > dict.iteritems() is handy here, too. > > for k, v in items_dict.iteritems(): > if v > 1: > print '%s occurred %s times' % (k, v) > > Kent Hi all, incorporating some of Wolfram's and Kent's (hope I've missed no one) suggestions: def dups_in_list_report(a_list): '''Prints a duplication report for a list.''' items_dict = {} for i in a_list: items_dict[i] = items_dict.get(i, 0) + 1 for k, v in sorted(items_dict.iteritems()): # cf below if v > 1: print '%s occurred %s times' %(k, v) And, I can't but agree that this is much better! Thanks folks. In trying to improve the code, I first had: for key in sorted(items_dict.keys()): if items_dict[key] > 1: print '%s occurred %s times' %(key, items_dict[key]) in place of the for loop over .iteritems(). Am I right in thinking that the advantage of Kent's suggestion of .iteritems() is that it eliminates some of the dict lookups? Other advantages? Finally, in the first instance, I was aiming for the OP's stated end. To make this more general and reusable, I think I'd do: def get_list_dup_dict(a_list, threshold=1): '''Returns a dict of items in list that occur threshold many times threshold defaults to 1. The dict returned has items occurring at least threshold many times as keys, and number of occurrences as values. ''' items_dict, dup_dict = {}, {} # Question below for i in a_list: items_dict[i] = items_dict.get(i, 0) + 1 for k, v in items_dict.iteritems(): if v >= threshold: dup_dict[k] = v #Question below return dup_dict def print_list_dup_report(a_list, threshold=1): '''Prints report of items in a_list occurring at least threshold many times threshold defaults to 1. get_list_dup_dict(a_list, threshold=0) is called. returning a dict of items in list that occur at least threshold many times as keys and their number of repetitions as values. This dict is looped over to print a sorted and formatted duplication report. ''' dup_dict = get_list_dup_dict(a_list, threshold) for k, v in sorted(dup_dict.iteritems()): print '%s occurred %s times' %(k, v) My question (from comment in new code): Since I've split the task into two functions, one to return a duplication dictionary, the other to print a report based on it, I think the distinct dup_dict is needed. (I do want the get_list_dup_dict function to return a dict for possible use in other contexts.) The alternative would be to iterate over a .copy() of the items_dict and delete items not meeting the threshold from items_dict, returning the pruned items_dict at the end. But, dup_dict is guaranteed to be smaller, save for original lists with no duplications and threshold set to 1. So, the distinct dup_dict way seems better for memory. Am I overlooking yet another dict technique that would help, here? Any other improvements? Thanks and best to all, Brian vdB From flaxeater at yahoo.com Thu Jan 27 21:20:19 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 21:20:22 2005 Subject: [Tutor] Syntax Check Message-ID: <20050127202019.29848.qmail@web54302.mail.yahoo.com> Ok I'll explain what I've done so far. I'm using tokenize to take the file that imports macro; found by using stack inspection and then tokenize it. I look at all the string tokens to see if they match they pattern a macro should have. Then I pass the macro string through the tokenizer again to expand this and to maintain the indentation needed for the block. I did this as an exercise and I thought the 'with' extension would be nice for my own benefit. I thought it would be useful for all those syntax propositions in PEPs and stuff to actually implement them to see how they work in real life. However in it's current state I have to use anonymous ('''****''') strings to surround the macro. I just thought it would be nice to just have python import macro run the first few things with out looking to far into the file, at which point macro.expand() would take over. Now that you mention it and I'm thinking about it, it would probably take some sort of MacroPython->Python compiler type thing. To get the feel I'm really looking for. I guess I was thinking it would work like psyco import psyco psyco.full() I hope I made myself clearer. Alan Gauld wrote: >>I'm trying to make a macro system that's work by just doing this >> >> > >OK, But to get your acros to use a language extension to python >(the with.... syntax) you are going to have to write your own >language interpreter/parser. Even if that just turns the 'with' >stuff into Python. > >So why is the """ marker even necessary? Your interpreter doesn't >need it - although it might make things easier... I'm still >confused I think. > >Alan G. > > __________________________________ Do you Yahoo!? Read only the mail you want - Yahoo! Mail SpamGuard. http://promotions.yahoo.com/new_mail From flaxeater at yahoo.com Thu Jan 27 21:39:12 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 21:39:15 2005 Subject: [Tutor] Unique Items in Lists Message-ID: <20050127203912.19043.qmail@web54310.mail.yahoo.com> Try a simple bubble sort. This will not have great performance on big files at a worst case scenario it will take n-1 loops to sort. This is not very general and would require a substantially different implementation. def bublesort(l1,l2,l3): modified=True while modified: modified=False for number,value in enumerate(l3): if number>0:#to avoid index error if l3[number]Dear Danny, thank you for ur help. But a basic >question ? > >In a table, more specifically a matrix 3X3, > >Apple Fruit Denmark >F-16 Fighter USA >Taj Wonder India >Mummy Antique Egypt > > >IF I have to sort on country, it should be > >Apple Fruit Denmark >Mummy Antique Egypt >Taj Wonder India >F-16 Fighter USA > > __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 From kent37 at tds.net Thu Jan 27 22:08:59 2005 From: kent37 at tds.net (Kent Johnson) Date: Thu Jan 27 22:09:40 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <41F94CDE.8040101@po-box.mcgill.ca> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> <41F8AD59.9070803@po-box.mcgill.ca> <41F8C910.1050202@tds.net> <41F94CDE.8040101@po-box.mcgill.ca> Message-ID: <41F9586B.8040506@tds.net> Brian van den Broek wrote: > incorporating some of Wolfram's and Kent's (hope I've missed no one) > suggestions: > > > def dups_in_list_report(a_list): > '''Prints a duplication report for a list.''' > > items_dict = {} > > for i in a_list: > items_dict[i] = items_dict.get(i, 0) + 1 > > for k, v in sorted(items_dict.iteritems()): # cf below > if v > 1: > print '%s occurred %s times' %(k, v) > > > And, I can't but agree that this is much better! Thanks folks. > > In trying to improve the code, I first had: > > for key in sorted(items_dict.keys()): > if items_dict[key] > 1: > print '%s occurred %s times' %(key, items_dict[key]) > > in place of the for loop over .iteritems(). Am I right in thinking that > the advantage of Kent's suggestion of .iteritems() is that it eliminates > some of the dict lookups? Other advantages? iteritems() doesn't create an intermediate list so that should be a slight time and memory savings. And my guess is that it is faster because it saves the lookups but if you care you should test. Mostly I prefer iteritems() because it seems cleaner and clearer to me. > > Finally, in the first instance, I was aiming for the OP's stated end. To > make this more general and reusable, I think I'd do: > > > def get_list_dup_dict(a_list, threshold=1): > '''Returns a dict of items in list that occur threshold many times > > threshold defaults to 1. The dict returned has items occurring at least > threshold many times as keys, and number of occurrences as values. > ''' > > items_dict, dup_dict = {}, {} # Question below > > for i in a_list: > items_dict[i] = items_dict.get(i, 0) + 1 > > for k, v in items_dict.iteritems(): > if v >= threshold: > dup_dict[k] = v #Question below > > return dup_dict > > def print_list_dup_report(a_list, threshold=1): > '''Prints report of items in a_list occurring at least threshold > many times > > threshold defaults to 1. get_list_dup_dict(a_list, threshold=0) is > called. > returning a dict of items in list that occur at least threshold many > times > as keys and their number of repetitions as values. > > This dict is looped over to print a sorted and formatted duplication > report. > ''' > > dup_dict = get_list_dup_dict(a_list, threshold) > for k, v in sorted(dup_dict.iteritems()): > print '%s occurred %s times' %(k, v) > > > > My question (from comment in new code): > > Since I've split the task into two functions, one to return a > duplication dictionary, the other to print a report based on it, I think > the distinct dup_dict is needed. (I do want the get_list_dup_dict > function to return a dict for possible use in other contexts.) You could have a function that gets all of the counts, and then filter on threshold when you print. > > The alternative would be to iterate over a .copy() of the items_dict and > delete items not meeting the threshold from items_dict, returning the > pruned items_dict at the end. But, dup_dict is guaranteed to be smaller, > save for original lists with no duplications and threshold set to 1. So, > the distinct dup_dict way seems better for memory. > > Am I overlooking yet another dict technique that would help, here? Any > other improvements? In Python 2.4 I think you could do dup_dict = dict((k, v) for k, v in items_dict.iteritems() if v >= threshold) which I think is likely to be quite fast (but again if you care you should test). Kent > > Thanks and best to all, > > Brian vdB > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From jeff at ccvcorp.com Thu Jan 27 22:34:49 2005 From: jeff at ccvcorp.com (Jeff Shannon) Date: Thu Jan 27 22:28:58 2005 Subject: [Tutor] Syntax Check In-Reply-To: <20050127164151.40660.qmail@web54309.mail.yahoo.com> References: <20050127164151.40660.qmail@web54309.mail.yahoo.com> Message-ID: <41F95E79.3000208@ccvcorp.com> Chad Crabtree wrote: > I'm trying to make a macro system that's work by just doing this > > import macro > # [...] Perhaps you could turn things around, and make your macro preprocessor into an import hook? I.E., you'd use it like -- import macro module = macro.import("module_with_macros"[, macro_list]) module.do_stuff() Not sure if you'd need to have a list of macros in the module to be imported, or not. Perhaps the macro module would hold a list of currently active macros, instead... In any case, this (I think) gives you a chance to interrupt the import process and modify the target module before the Python parser gets it, which should enable you to avoid the SyntaxError problems. (Of course, I've never messed around with hooking __import__(), so I could just be talking out of my ...) Jeff Shannon Technician/Programmer Credit International From flaxeater at yahoo.com Thu Jan 27 22:36:38 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Thu Jan 27 22:36:41 2005 Subject: [Tutor] Syntax Check Message-ID: <20050127213638.57134.qmail@web54302.mail.yahoo.com> Well I don't think that it would really require that. I could just define macro's in a module and just do it like so import macro import defined_macros as m macro.expand(m.with(),m.assert()) I just thought it would be best to have definitions at the head of a script, or at least to have the option. Jeff Shannon wrote: > Perhaps you could turn things around, and make your macro preprocessor > into an import hook? I.E., you'd use it like -- > > import macro > module = macro.import("module_with_macros"[, macro_list]) > module.do_stuff() > > Not sure if you'd need to have a list of macros in the module to be > imported, or not. Perhaps the macro module would hold a list of > currently active macros, instead... > > In any case, this (I think) gives you a chance to interrupt the import > process and modify the target module before the Python parser gets it, > which should enable you to avoid the SyntaxError problems. > > (Of course, I've never messed around with hooking __import__(), so I > could just be talking out of my ...) > > __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 From ryan at acceleration.net Thu Jan 27 23:11:30 2005 From: ryan at acceleration.net (Ryan Davis) Date: Thu Jan 27 23:11:35 2005 Subject: [Tutor] Syntax Check In-Reply-To: <41F95E79.3000208@ccvcorp.com> Message-ID: <20050127221132.E3CEB1E4003@bag.python.org> Check out COG (http://www.nedbatchelder.com/code/cog/), a macro system for any language created by Ned Batchelder (http://www.nedbatchelder.com/). I'm not sure if that's what you're looking for, but allows some good macro capabilities. Thanks, Ryan -----Original Message----- From: tutor-bounces@python.org [mailto:tutor-bounces@python.org] On Behalf Of Jeff Shannon Sent: Thursday, January 27, 2005 4:35 PM To: tutor@python.org Subject: Re: [Tutor] Syntax Check Chad Crabtree wrote: > I'm trying to make a macro system that's work by just doing this > > import macro > # [...] Perhaps you could turn things around, and make your macro preprocessor into an import hook? I.E., you'd use it like -- import macro module = macro.import("module_with_macros"[, macro_list]) module.do_stuff() Not sure if you'd need to have a list of macros in the module to be imported, or not. Perhaps the macro module would hold a list of currently active macros, instead... In any case, this (I think) gives you a chance to interrupt the import process and modify the target module before the Python parser gets it, which should enable you to avoid the SyntaxError problems. (Of course, I've never messed around with hooking __import__(), so I could just be talking out of my ...) Jeff Shannon Technician/Programmer Credit International _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From cyresse at gmail.com Thu Jan 27 23:37:11 2005 From: cyresse at gmail.com (Liam Clarke) Date: Thu Jan 27 23:37:16 2005 Subject: [Tutor] Convert string to variable name In-Reply-To: <010f01c50451$a2c21e60$36bd8651@xp> References: <4453924905012616314538eefd@mail.gmail.com> <010f01c50451$a2c21e60$36bd8651@xp> Message-ID: I've used something along the lines for Pythoncard, but the names are generated within strict rules and expectations. i.e. first off ? #Some code that uses regExs, finds all headings, and # a asterisk to indicate a date value, returns a iterable object reHead using finditer() #which contains two groups, head and date #date is either a '' or '*' which evals to False/ True for my purposes #The asterisk isn't important, it's just a non-null. counter = 0 # I use nums as keys for easy sort/increment dex={} for item in reHead: dex[counter]={'title':item.group('head'), 'date': item.group('date') } counter += 1 j=cPickle.Pickler(someOpenFile) j.dump(dex) someOpenFile.close() #### x=cPickle.Unpickler(someOpenFile) dex2 = x.read() for key in dex2.keys(): buttName = '%dbutton' % key choiceName = %dchoice % key if dex2[key]['date']: choiceList=['Past', Present', 'Future'] else: choiceList = ['Highest', 'Lowest', 'Average'] self.components[buttName] = { 'type': 'button', 'name': buttName, 'label' : dex2[key]['title'] ?.. } self.components[choiceName] = { 'type': 'choice', 'name': choiceName, 'label' : "Select option", 'items' : choiceList, 'stringSelection': choiceList[0] } Only reason I do it this way, as opposed to using a list, is a stylistic choice. But yeah, you end up iterating a whole lot, and unless the variable names are generated predictably, you have a hard time referencing stuff. So, in conclusion, dynamically named variables are very limited in their usefulness. On Thu, 27 Jan 2005 09:21:51 -0000, Alan Gauld wrote: > > This is something I've been trying to figure out for some time. Is > > there a way in Python to take a string [say something from a > > raw_input] and make that string a variable name? I want to to this > so > > that I can create class instances on-the-fly, using a user-entered > > string as the instance name. > > This comes up regularly from beginners and is nearly always a bad > idea! > > The easy solution is to use a dictionary to store the instances. > See the OOP topic in my tutor for an example using bankAccounts > as the objects... > > > could accomplish something similar using a plain old dictionary, but > I > > was playing around with the OOP stuff and thought it might be a neat > > thing to try out. > > A Dictionary is best. The problem is, once you create your new > variables none of the rest of your code knows aout them so > how can it access them. And what if someone enters a name that > is being iused elsewhere in your code? You will overwrite a > real variable with this new object! Very tricky to control. > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > _______________________________________________ > 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. From keridee at jayco.net Fri Jan 28 03:10:15 2005 From: keridee at jayco.net (Jacob S.) Date: Fri Jan 28 03:10:22 2005 Subject: [Tutor] Preffered way to search posix filesystem References: <200501262301.22914.miles@mstevenson.org> Message-ID: <009501c504de$841b7550$c45328cf@JSLAPTOP> Try this def findfile(thefile, toplevel="C:\\windows",findcount=-1,subdirectories=True,returnlist=False): results = [] t = 0 if subdirectories: for root,dirs,files in os.walk(toplevel): for x in files: fullname = os.path.join(root,x) if x.lower() == thefile.lower(): print "Found %s" % fullname results.append(fullname+": 0:") t = t+1 else: print "Searching %s" % fullname if t == findcount: break print "\nFound %s matches:" % t pplist(results) else: n = 0 for x in os.listdir(toplevel): fullname = os.path.join(toplevel,x) if x.lower()==thefile.lower(): print "Found:\n%s: 0:" % fullname n = 1 results.append(fullname) break if not n: print "Found 0 matches." if returnlist: return results From tameyer at ihug.co.nz Fri Jan 28 03:46:08 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Fri Jan 28 03:46:15 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: Message-ID: [Sean Perry] >> And now, for the pedant in me. I would recommend against naming >> functions with initial capital letters. In many languages, this >> implies a new type (like your Water class). so >> CombineWater should be combineWater. [Brian van den Broek] > Do you mean implies by the dominant coding conventions, or > by language syntax? (Indulging the curious pedant in me.) You might want to read PEP 8, which is the official recommendations for Python code in terms of style. A bit more than half way down there's a "Naming Conventions" section which has this: """ Modules should have short, lowercase names, without underscores. [...] Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition. [...] Function names should be lowercase, possibly with words separated by underscores to improve readability. """ (BTW, 'CombineWater' should be 'combine_water', officially). =Tony.Meyer From bvande at po-box.mcgill.ca Fri Jan 28 05:47:01 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Fri Jan 28 05:53:16 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: References: Message-ID: <41F9C3C5.5070007@po-box.mcgill.ca> Tony Meyer said unto the world upon 2005-01-27 21:46: > [Sean Perry] > >>>And now, for the pedant in me. I would recommend against naming >>>functions with initial capital letters. In many languages, this >>>implies a new type (like your Water class). so >>>CombineWater should be combineWater. > > > [Brian van den Broek] > >>Do you mean implies by the dominant coding conventions, or >>by language syntax? (Indulging the curious pedant in me.) > > > You might want to read PEP 8, which is the official recommendations for > Python code in terms of style. A bit more than half way down there's a > "Naming Conventions" section which has this: > > > (BTW, 'CombineWater' should be 'combine_water', officially). > > =Tony.Meyer Hi, thanks for the reference and to all else who've responded. I was just curious about Sean's statement and wondered if there were languages which enforced some naming scheme. (Sean pointed to Erlang, which I don't think I'd heard of before.) I am aware of PEP 8, and have read it a few times. I don't tend to feel constrained by it though I do seem mostly in accord. And, judging by the code I've read, not feeling constrained by it seems pretty widespread in the Python community. I've only been writing classes for a month or so, and if I cannot get a good one word Name, I've been Naming_them_thusly. That works for me as I do always use names_with_underscores for functions. I don't really like them either, but, iLikeThe AlternativesEvenLess. Oh wait, I know: let's all start writing code in MS Word .doc format! Arial for functions, Times New Roman for classes. Who's with me? ;-) Best to all, Brian vdB From bvande at po-box.mcgill.ca Fri Jan 28 05:41:16 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Fri Jan 28 05:53:41 2005 Subject: [Tutor] Re: Unique Items in Lists In-Reply-To: <41F9586B.8040506@tds.net> References: <20050127061748.23946.qmail@web53507.mail.yahoo.com> <41F892ED.90909@po-box.mcgill.ca> <41F8AD59.9070803@po-box.mcgill.ca> <41F8C910.1050202@tds.net> <41F94CDE.8040101@po-box.mcgill.ca> <41F9586B.8040506@tds.net> Message-ID: <41F9C26C.3030501@po-box.mcgill.ca> Kent Johnson said unto the world upon 2005-01-27 16:08: > Brian van den Broek wrote: >> Finally, in the first instance, I was aiming for the OP's stated end. >> To make this more general and reusable, I think I'd do: >> >> >> def get_list_dup_dict(a_list, threshold=1): >> >> items_dict, dup_dict = {}, {} # Question below >> >> for i in a_list: >> items_dict[i] = items_dict.get(i, 0) + 1 >> >> for k, v in items_dict.iteritems(): >> if v >= threshold: >> dup_dict[k] = v #Question below >> >> return dup_dict >> >> def print_list_dup_report(a_list, threshold=1): >> >> dup_dict = get_list_dup_dict(a_list, threshold) >> for k, v in sorted(dup_dict.iteritems()): >> print '%s occurred %s times' %(k, v) >> >> >> >> My question (from comment in new code): >> >> Since I've split the task into two functions, one to return a >> duplication dictionary, the other to print a report based on it, I >> think the distinct dup_dict is needed. (I do want the >> get_list_dup_dict function to return a dict for possible use in other >> contexts.) > > > You could have a function that gets all of the counts, and then filter > on threshold when you print. >> Am I overlooking yet another dict technique that would help, here? Any >> other improvements? Thanks Kent. Above the last snip would indeed be one such improvement! (How'd I miss that?) Best, Brian vdB From javier at ruere.com.ar Fri Jan 28 06:32:52 2005 From: javier at ruere.com.ar (Javier Ruere) Date: Fri Jan 28 06:22:23 2005 Subject: [Tutor] Re: Syntax Check In-Reply-To: <20050127202019.29848.qmail@web54302.mail.yahoo.com> References: <20050127202019.29848.qmail@web54302.mail.yahoo.com> Message-ID: Chad Crabtree wrote: > Ok I'll explain what I've done so far. [...] > I hope I made myself clearer. Yes. To add new stuff to the language you would have to work at the interpreter level. Probably you can avoid going too deep using import hooks, as it has already been suggested, to modify what's being imported on the fly. I believe it could work like Psyco if the preprocessor installed the import hooks when imported. Are you interested only in the syntactic sugar or you plan to generate more efficient code? Javier From flaxeater at yahoo.com Fri Jan 28 07:45:19 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Fri Jan 28 07:45:22 2005 Subject: [Tutor] Re: Syntax Check Message-ID: <20050128064519.68477.qmail@web54309.mail.yahoo.com> I'm going to look more in to import hooks. I am only looking for syntactic sugar, at least at this point. It's not really my ambition to make more efficient code. To Ryan. I looked at COG before (not in this case but general looking at python packages) it doesn't really do what I was thinking, but I can see how it would be very useful for it's problem domain of code generation for other languages. Javier Ruere wrote: > Yes. To add new stuff to the language you would have to work at the >interpreter level. Probably you can avoid going too deep using import >hooks, as it has already been suggested, to modify what's being imported >on the fly. > I believe it could work like Psyco if the preprocessor installed the >import hooks when imported. > Are you interested only in the syntactic sugar or you plan to generate >more efficient code? > > __________________________________ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com From alan.gauld at freenet.co.uk Fri Jan 28 08:31:48 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 08:36:54 2005 Subject: [Tutor] Should this be a list comprehension or something? References: <41F9C3C5.5070007@po-box.mcgill.ca> Message-ID: <020b01c5050b$6e3344c0$36bd8651@xp> > Oh wait, I know: let's all start writing code in MS Word .doc format! > Arial for functions, Times New Roman for classes. Who's with me? ;-) So you've been looking at Eiffel then? :-) Alan G. From melnyk at gmail.com Fri Jan 28 14:51:15 2005 From: melnyk at gmail.com (Scott Melnyk) Date: Fri Jan 28 14:51:18 2005 Subject: [Tutor] Re: sorting a 2 gb file- i shrunk it and turned it around Message-ID: Hello again! Thank you everyone for all the help. The most important step in dealing with Blast results is getting rid of all the extraneous information. The standard result file gives you everything you could ask for when comparing DNA sequences, however at this step I am looking for specific data. Once I have my relevant matches I can go back and pull out the info for just those matches from the file. Following Kent's advice I came up with the following (I should set up defs and a modular system for renewability, that will be next) import sys #my system complained when I had them on 1 line as sys, sets, re import sets #not sure why but that's another issue (2.4 on win XP) import re result=re.compile('^(hg17.+)5\'.+') #the import part of the line is in the ( ) query=re.compile('^.+(ENST\d+\.\d)+') #matches the line up to the important transcriptID #and groups the ID info TFILE = open(sys.argv[1], 'r' ) #file to read from WFILE=open(sys.argv[2], 'w') # file to write to results={} for line in TFILE: isQueryLine= query.match(line) isResultLine= result.match(line) if isQueryLine: current_query = isQueryLine.group(1) if isResultLine: key = isResultLine.group(1) results.setdefault(key, []).append(current_query) # see explanation below # Now go through results looking for entries with more than one query for key, queries in results.iteritems(): if len(queries) > 1: print >> WFILE print >> WFILE, key for query in queries: print >> WFILE, query I am almost there the program seemed to run well will minimal swapping and finished in 5 minutes. My output file is only 45 mb. A sample of the output file is: hg17_chainMm5_chr15 range=chr7:148238502-148239073 ENST00000339563.1 ENST00000342196.1 ENST00000339563.1 ENST00000344055.1 hg17_chainMm5_chr13 range=chr5:42927967-42928726 ENST00000279800.3 ENST00000309556.3 hg17_chainMm5_chr6 range=chr1:155548627-155549517 ENST00000321157.3 ENST00000256324.4 hg17_chainMm5_chr13 range=chr1:81386270-81386967 ENST00000011649.3 ENST00000348636.1 hg17_chainMm5_chr19 range=chr11:56050656-56051559 ENST00000341231.1 ENST00000341231.1 ENST00000331792.1 ENST00000341231.1 ENST00000341231.1 ENST00000331792.1 hg17_chainMm5_chr9 range=chr11:123561223-123562097 ENST00000341493.1 ENST00000318666.4 ENST00000341493.1 I can see where any of the chains appear more than once, which is good and I am looking for situations like first example where ENST00000339563.1 is the first and third on the list or the fifth example. Next step is to cut out the ENST lines that only show up once and wind up with just the places where there are matches at least twice to a given transcript (using the ENST00000...) ids. Like in the final example I only want the first and third so I know it is twice in that transcript. Back to it and other things. Thanks for all the help so far, Scott From kent37 at tds.net Fri Jan 28 15:20:22 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 28 15:20:27 2005 Subject: [Tutor] Re: sorting a 2 gb file- i shrunk it and turned it around In-Reply-To: References: Message-ID: <41FA4A26.7090201@tds.net> Scott Melnyk wrote: > Hello again! > > Thank you everyone for all the help. Congratulations on getting this working! > I can see where any of the chains appear more than once, which is good > and I am looking for situations like first example where > ENST00000339563.1 is the first and third on the list or the fifth > example. > Next step is to cut out the ENST lines that only show up once and wind > up with just the places where there are matches at least twice to a > given transcript (using the ENST00000...) ids. Like in the final > example I only want the first and third so I know it is twice in that > transcript. See the tutor thread "Unique items in lists" if you need help with this step, we just had an extensive discussion of how to do it. Kent From miles at mstevenson.org Fri Jan 28 17:00:30 2005 From: miles at mstevenson.org (Miles Stevenson) Date: Fri Jan 28 17:01:27 2005 Subject: [Tutor] Preffered way to search posix filesystem In-Reply-To: <20050127042716.1284.qmail@web54307.mail.yahoo.com> References: <20050127042716.1284.qmail@web54307.mail.yahoo.com> Message-ID: <200501281100.31112.miles@mstevenson.org> Thanks for the advice! My problem was that the built-in Python docs in Kdevelop weren't up-to-date, and I had trouble finding walk() in the docs. Here is the approach that I used being a python newbie (improvements are welcome): def getfiles(path): """Recursively search a path and generate a lit of OGG files found Takes a filesystem path as an argument. The path is recursively searched for OGG files. Returns a list of each file found (in absolute path format) with the first element of the list set to 'start'""" filelist = ['start'] for root, dirs, files in os.walk(path): for name in files: a = os.path.join(root, name) if os.path.isfile(a) and fnmatch.fnmatch(a, '*.ogg'): filelist.append(a) return filelist What is interesting is that the latest 2.4 Python docs say that walk() returns a Tuple, which is untrue. It returns a generator object according to type(). This had me heavily confused as to how to use what was returned from walk() and it took a good hour of troubleshooting to figure it out. -Miles On Wednesday 26 January 2005 11:27 pm, you wrote: > Try the os module. I think this should probably get you there. > http://docs.python.org/lib/module-os.html > > Miles Stevenson wrote: > >I would like to search filesystem structures using globs on Posix > > systems from > > >within Python. I don't see an obvious method to do this with in the > > standard > > >modules. What is the preferred way of doing this? Should I just use > > the find > > >command or is there a good module out there for searching? > > > >Thanks. > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com -- Miles Stevenson miles@mstevenson.org PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 From kent37 at tds.net Fri Jan 28 17:23:27 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 28 17:23:30 2005 Subject: [Tutor] Preffered way to search posix filesystem In-Reply-To: <200501281100.31112.miles@mstevenson.org> References: <20050127042716.1284.qmail@web54307.mail.yahoo.com> <200501281100.31112.miles@mstevenson.org> Message-ID: <41FA66FF.3050603@tds.net> Miles Stevenson wrote: > What is interesting is that the latest 2.4 Python docs say that walk() returns a Tuple, which is untrue. > It returns a generator object according to type(). This had me heavily confused as to how to use > what was returned from walk() and it took a good hour of troubleshooting to figure it out. The docs are correct but maybe a little subtle. They say, walk( top[, topdown=True [, onerror=None]]) walk() generates the file names in a directory tree, by walking the tree either top down or bottom up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). To someone familiar with Python generators, the words "generates" and "yields" are strong clues that walk is a generator and when you iterate over it you get tuples. If you are not familiar with this terminology I can see how it would be confusing. OTOH there are two examples in the same section that show correct usage... Kent From johan.nilsson at freenet.de Fri Jan 28 18:40:37 2005 From: johan.nilsson at freenet.de (Johan Nilsson) Date: Fri Jan 28 18:40:53 2005 Subject: [Tutor] Advise... In-Reply-To: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> References: <002c01c50363$1a1ad960$745328cf@JSLAPTOP> Message-ID: <41FA7915.2030508@freenet.de> Jacob, Apart from all the other comments you received, here are my thoughts. I think you could do one more thing to speed up your calculations and that is to use a more efficient method. The Reimann sum is not a very efficient. One simple method that is rahter popular is Simpson's rule. The calculations are not much more complicated than what you already have equation347 You then you just need to make sure that the number of intervals are even. Johan Jacob S. wrote: > Hi all. > > Long time no see. (About five days, right?) > Anyway, I know the first thing that some of you are going to say is > using eval(). I don't want a whole > guilt trip on security risks and all that. I do not want to share the > code with anyone else while it's on my > computer, and I sure don't have anyone near me that knows python. I > would be surprised if more than 50 > people in Portland, IN knew anything about python. So security is not > a problem. I guess what I'm > looking for is someone who knows the Reimann Sum better than I do and > can tell me whether I can do > something to make it more efficient. It's horribly slow with ten > thousand steps-- I don't know the notation > very well, but it loops the loop O(step*(maximum-minimum)) times, > which is horribly sucky. > In case anyone doesn't know, Reimann's sum is the computer's > version of the definite integral, the area > under a curve in a particular domain. > > Basic input and output. > If a curve is a straight line, say y = x, the area under the line on > an interval can be found by geometric means. > However, if you use a parabola, or any other function, say y = 3*x**2, > > > What is the function? 3*x*x > What is the minimum? 2 > What is the maximum? 5 > 117.000435 > > Which, considering that it is supposed to be exactly 117, It's darn > good. Unfortunately, it also takes about > 10 seconds to do all that. > Any suggestions? Any advice? TIA > Jacob Schmidt > > > ############################ > from __future__ import division > import psyco > psyco.full() > fofx = raw_input("What is the function? ") > minimum = raw_input("What is the minimum? ") > maximum = raw_input("What is the maximum? ") > minimum = float(minimum) > maximum = float(maximum) > total = 0 > step = 100000 > x = minimum > while minimum <= x <= maximum: > area = eval(fofx)*1/step > total = total+area > x = x+1/step > print total > ############################# > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- Skipped content of type multipart/related From johan.nilsson at freenet.de Fri Jan 28 18:40:53 2005 From: johan.nilsson at freenet.de (Johan Nilsson) Date: Fri Jan 28 18:40:59 2005 Subject: [Tutor] How does import work? Message-ID: <41FA7925.80109@freenet.de> Hi all, I am rather new to python. I am trying to write a program using the scipy package. I have come across a problem that confuses me, and I hope that someone could give me an hint on how to solve this. Here is what I do Start idle >>> from scipy.signal.signaltools import * /Traceback (most recent call last): File "", line 1, in -toplevel- from scipy.signal.signaltools import * ImportError: No module named signaltools/ So I try the methodic way and this works, giving me access to the functions I need >>> from scipy import * >>> from scipy.signal import * >>> from scipy.signal.signaltools import * Now what confuses me is that when I put the above three lines in a file (of course without the >>>) and execute them I get a long error message. / Traceback (most recent call last): File "/home/johan/pyton/import_test.py", line 5, in -toplevel- from scipy.signal import * File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 270, in __getattr__ module = self._ppimport_importer() File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 233, in _ppimport_importer raise PPImportError,\ PPImportError: Traceback (most recent call last): File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 243, in _ppimport_importer module = __import__(name,None,None,['*']) File "/usr/lib/python2.3/site-packages/scipy/signal/__init__.py", line 11, in ? File "/usr/lib/python2.3/site-packages/scipy/signal/ltisys.py", line 14, in ? File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 270, in __getattr__ module = self._ppimport_importer() File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 233, in _ppimport_importer raise PPImportError,\ PPImportError: Traceback (most recent call last): File "/usr/local/lib/python2.3/site-packages/scipy_base/ppimport.py", line 243, in _ppimport_importer module = __import__(name,None,None,['*']) File "/usr/lib/python2.3/site-packages/Numeric/Matrix.py", line 5, in ? import LinearAlgebra File "/usr/local/lib/python2.3/site-packages/Numeric/LinearAlgebra.py", line 8, in ? import lapack_lite ImportError: /usr/local/lib/python2.3/site-packages/Numeric/lapack_lite.so: undefined symbol: dgesdd_/ What I dont understand is how can the import statements work, when I type them in manually in IDLE and not when I execute them in a file? Has anyone come across this type of behavior before? Johan From carroll at tjc.com Fri Jan 28 19:09:07 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 28 19:09:12 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: <020b01c5050b$6e3344c0$36bd8651@xp> Message-ID: On Fri, 28 Jan 2005, Alan Gauld wrote: > > Oh wait, I know: let's all start writing code in MS Word .doc > format! > > Arial for functions, Times New Roman for classes. Who's with me? > ;-) > > So you've been looking at Eiffel then? > :-) I don't get this joke, but it sounds like the basis for it would be interesting. Can you explain? From carroll at tjc.com Fri Jan 28 19:15:33 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 28 19:15:36 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: Message-ID: Sorry for not responding earlier to thank all the people who weighed in on my question. I'm glad to find that my first take on it was sufficiently pythonic. Alan, I'd never seen the criterion befor that list comprehensions should be used to generate other lists. That's helpful, and I'll keep it in mind. Thanks to Max and Tony for their comments and tests on efficiency. Although I was primarily interested in finding out if I was thinking in the right way, this was helpful. Karl, I hadn't thought about overloading, although I knew about the possibility. That's really cool, and quite clear. Sean, I wasn't even aware of reduce() before. Thanks, I'll look into that. From carroll at tjc.com Fri Jan 28 19:25:48 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 28 19:25:52 2005 Subject: [Tutor] Naming conventions (was: Should this be a list comprehension or something? In-Reply-To: <41F89480.7020104@speakeasy.net> Message-ID: On Wed, 26 Jan 2005, Sean Perry wrote: > And now, for the pedant in me. I would recommend against naming > functions with initial capital letters. In many languages, this implies > a new type (like your Water class). so CombineWater should be combineWater. I hate hate hate hate hate camelcase and will never use it. In my book, if the name has *any* capitals in it, the first letter is capitalized, too. Anything else is unaesthetic. To me, when I have names that are composed of multiple words (say, "rice quantity"), I have two approaches: distinguishing the words by case (RiceQuantity) or separating by underscores (rice_quantity). I never confuse classes/instances and methods, because I use noun phrases for classes and instances (HeatedWater, VerifiedInput) and verb phrases for the methods (CombineWater, CookRice). I suppose I could get confusion, for example, when the combination either a noun phrase or verb phrase (SoundOut: is that a name describing the Sound that's being put Out, or is it a method that's is tentatively Sounding Out somthing?) but so far that hasn't been an issue for me. Of course in my case, I write code only for myself, so I have the luxury of not worrying about what Joe in the next cubicle is doing, and what Jane will do when she's trying to read Bob's and my code together. So I have the luxury of turning my nose up at camelCase. I should add that, the one time I made changes to someone else's Python code for release (a minor patch to nntplib.py), I used the same case conventions already in place in the module. From kent37 at tds.net Fri Jan 28 19:41:16 2005 From: kent37 at tds.net (Kent Johnson) Date: Fri Jan 28 19:41:19 2005 Subject: [Tutor] Naming conventions In-Reply-To: References: Message-ID: <41FA874C.20209@tds.net> Terry Carroll wrote: > On Wed, 26 Jan 2005, Sean Perry wrote: > > >>And now, for the pedant in me. I would recommend against naming >>functions with initial capital letters. In many languages, this implies >>a new type (like your Water class). so CombineWater should be combineWater. > > > I hate hate hate hate hate camelcase and will never use it. In my book, > if the name has *any* capitals in it, the first letter is capitalized, > too. Anything else is unaesthetic. > > To me, when I have names that are composed of multiple words (say, "rice > quantity"), I have two approaches: distinguishing the words by case > (RiceQuantity) or separating by underscores (rice_quantity). Separating with underscores is quite common in the Python community, actually it is the preferred spelling for library modules. So maybe you should adopt that, just to reduce the confusion when your code does have an encounter with the outside world :-) Kent From gtsang at lnxw.com Fri Jan 28 21:02:51 2005 From: gtsang at lnxw.com (Gilbert Tsang) Date: Fri Jan 28 21:03:01 2005 Subject: [Tutor] Control flow Message-ID: <41FA9A6B.8040200@lynuxworks.com> Hi there, I have this logic that I cannot wrap my mind it: def go_jogging(): # go out and jog return if ( bad_weather =='y' ): # ask user only if weather is bad. b = input ( "Weather is really bad, still go out to jog?[y/n]" ) if b == 'y': go_jogging() else: # program should exit now else: go_jogging() #################################################### I can't get the program to stop processing further in the middle (apparently neither exit nor goto-label exist in Python, sorry for the C++ mindset) so I used exception to achieve what I want. I know in that example you could probably manipulate the logic so that program ends at the bottom of the if-tree. My question is then how to exit in the middle of a if-then-else tree? Thanks, Gilbert. try: if ( bad_weather =='y' ): b = input ( "Weather is really bad, still go out to jog?[y/n]" ) if b == 'y': go_jogging() else: raise Exception( "quit" ) else: go_jogging() except Exception, inst: print "Program exits now" From singingxduck at gmail.com Fri Jan 28 22:24:44 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Fri Jan 28 22:26:12 2005 Subject: [Tutor] Control flow In-Reply-To: <41FA9A6B.8040200@lynuxworks.com> References: <41FA9A6B.8040200@lynuxworks.com> Message-ID: <41FAAD9C.1080603@gmail.com> Gilbert Tsang wrote: > Hi there, I have this logic that I cannot wrap my mind it: > > def go_jogging(): > # go out and jog > return > > if ( bad_weather =='y' ): > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > # program should exit now > else: > go_jogging() > > #################################################### > I can't get the program to stop processing further in the middle > (apparently neither exit nor goto-label exist in Python, sorry for the > C++ mindset) so I used exception to achieve what I want. I know in > that example you could probably manipulate the logic so that program > ends at the bottom of the if-tree. My question is then how to exit in > the middle of a if-then-else tree? Thanks, Gilbert. > > try: > if ( bad_weather =='y' ): > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > raise Exception( "quit" ) > else: > go_jogging() > except Exception, inst: > print "Program exits now" > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Well, if its in a function you can just do return instead of raise Exception("quit") since the function exits once it returns something. It's sort of the same as the following: Say you have a recursive function fact(n) which takes in a number and returns the factorial of that number. One way to code this would be: def fact(n): if n > 3: return n*fact(n-1) else: return 2*n However, since the function 'exits' after a return statement, it saves space in general to do the following: def fact(n): if n > 3: return n*fact(n-1) return 2*n Because the only way return 2*n will be reached is if n is smaller than or equal to three, which is what we wanted anyway. BTW, the way this function works is like this: Let's say you want to calculate fact(10). 10 is greater than 3, so the program calculates 10*fact(9) and so on until fact(3) is reached, at which point it will return 10*(9*(8*(7*(6*(5*(4*(6))))))) (6 is 2 * 3). or the factorial of 10. I probably didn't do too good a job explaining that, and that's probably not the most efficient implementation (it breaks @ around fact(1000) by default, and around fact(11462) when you use sys.setrecursionlimit(2000) (11462 is as high as it'll go on my machine)), so please feel free to ask questions. HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From singingxduck at gmail.com Fri Jan 28 22:26:03 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Fri Jan 28 22:26:24 2005 Subject: [Tutor] Control flow In-Reply-To: <41FA9A6B.8040200@lynuxworks.com> References: <41FA9A6B.8040200@lynuxworks.com> Message-ID: <41FAADEB.5020908@gmail.com> Gilbert Tsang wrote: > Hi there, I have this logic that I cannot wrap my mind it: > > def go_jogging(): > # go out and jog > return > > if ( bad_weather =='y' ): > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > # program should exit now > else: > go_jogging() > > #################################################### > I can't get the program to stop processing further in the middle > (apparently neither exit nor goto-label exist in Python, sorry for the > C++ mindset) so I used exception to achieve what I want. I know in > that example you could probably manipulate the logic so that program > ends at the bottom of the if-tree. My question is then how to exit in > the middle of a if-then-else tree? Thanks, Gilbert. > > try: > if ( bad_weather =='y' ): > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > raise Exception( "quit" ) > else: > go_jogging() > except Exception, inst: > print "Program exits now" > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Well, if its in a function you can just do return instead of raise Exception("quit") since the function exits once it returns something. It's sort of the same as the following: Say you have a recursive function fact(n) which takes in a number and returns the factorial of that number. One way to code this would be: def fact(n): if n > 3: return n*fact(n-1) else: return 2*n However, since the function 'exits' after a return statement, it saves space in general to do the following: def fact(n): if n > 3: return n*fact(n-1) return 2*n Because the only way return 2*n will be reached is if n is smaller than or equal to three, which is what we wanted anyway. BTW, the way this function works is like this: Let's say you want to calculate fact(10). 10 is greater than 3, so the program calculates 10*fact(9) and so on until fact(3) is reached, at which point it will return 10*(9*(8*(7*(6*(5*(4*(6))))))) (6 is 2 * 3). or the factorial of 10. I probably didn't do too good a job explaining that, and that's probably not the most efficient implementation (it breaks @ around fact(1000) by default, and around fact(11462) when you use sys.setrecursionlimit(2000) (11462 is as high as it'll go on my machine)), so please feel free to ask questions. HTH, Orri -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From alan.gauld at freenet.co.uk Fri Jan 28 22:28:06 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 22:28:35 2005 Subject: [Tutor] Should this be a list comprehension or something? References: Message-ID: <022d01c50580$425b9c60$36bd8651@xp> > > So you've been looking at Eiffel then? > > :-) > > I don't get this joke, but it sounds like the basis for it > would be interesting. Can you explain? Bertrand Meyer, the inventor of Eiffel uses rich text to display code in his books. The commercial Eiffel IDE that his company ISE sells used to display the code the same way. Thus different fonts were used for comments, keywords etc as well as the usual syntax colouring. I note that the freeware version of the tool seems to be more conventional in approach! The best way to see what I mean is probably to visit http://archive.eiffel.com/doc/manuals/technology/oosc/acrobat.html and view the pdf files. Unlike most programming books the code is not in a fixed space font and the original ISE IDE copied the books style... As an aside Meyer's book Object Oriented Software Construction is huge (1200+ pages) but is also one of the very best books on OOP anywhere. (Even is all the code is in Eiffel!) Alan G. From alan.gauld at freenet.co.uk Fri Jan 28 22:34:07 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 22:33:24 2005 Subject: [Tutor] Should this be a list comprehension or something? References: Message-ID: <023201c50581$19b26950$36bd8651@xp> > I don't get this joke, but it sounds like the basis for it > would be interesting. Can you explain? After sending my last message I was browsing the Eiffel site and found an example of the IDE. Its here: http://archive.eiffel.com/eiffel/nutshell.html and you scroll down to the question: What does a class look like? The code under that is a direct cut n paste from their commercial editor. (The freeware version is in a screenshot a little bit above...) Note the use of a proportional font(Arial?) as well as the documentation like style of Eiffel in general... Alan G. From jsmith at medplus.com Fri Jan 28 22:35:07 2005 From: jsmith at medplus.com (Smith, Jeff) Date: Fri Jan 28 22:35:20 2005 Subject: [Tutor] Control flow Message-ID: One way out of the top level is to call sys.exit(1) -----Original Message----- From: Orri Ganel [mailto:singingxduck@gmail.com] Sent: Friday, January 28, 2005 4:26 PM To: Gilbert Tsang; Tutor@python.org Subject: Re: [Tutor] Control flow Gilbert Tsang wrote: > Hi there, I have this logic that I cannot wrap my mind it: > > def go_jogging(): > # go out and jog > return > > if ( bad_weather =='y' ): > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > # program should exit now > else: > go_jogging() > > #################################################### > I can't get the program to stop processing further in the middle > (apparently neither exit nor goto-label exist in Python, sorry for the > C++ mindset) so I used exception to achieve what I want. I know in > that example you could probably manipulate the logic so that program > ends at the bottom of the if-tree. My question is then how to exit in > the middle of a if-then-else tree? Thanks, Gilbert. > > try: > if ( bad_weather =='y' ): > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > else: > raise Exception( "quit" ) > else: > go_jogging() > except Exception, inst: > print "Program exits now" > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From carroll at tjc.com Fri Jan 28 22:56:57 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 28 22:57:04 2005 Subject: [Tutor] Naming conventions In-Reply-To: <41FA874C.20209@tds.net> Message-ID: On Fri, 28 Jan 2005, Kent Johnson wrote: > Separating with underscores is quite common in the Python community, > actually it is the preferred spelling for library modules. So maybe you > should adopt that, just to reduce the confusion when your code does have > an encounter with the outside world :-) I'm a lawyer by trade; my work is *supposed* to confuse the outside world! From alan.gauld at freenet.co.uk Fri Jan 28 23:00:04 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 22:59:30 2005 Subject: [Tutor] Should this be a list comprehension or something? References: Message-ID: <024f01c50584$b9235690$36bd8651@xp> > Alan, I'd never seen the criterion befor that list comprehensions should > be used to generate other lists. Its not so much a criterion that they *should* be used that way, its just that its what they do. A list comprehension creates a list! Thats why they are called *list* comprehensions. :-) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From carroll at tjc.com Fri Jan 28 23:06:36 2005 From: carroll at tjc.com (Terry Carroll) Date: Fri Jan 28 23:06:40 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: <024f01c50584$b9235690$36bd8651@xp> Message-ID: On Fri, 28 Jan 2005, Alan Gauld wrote: > Its not so much a criterion that they *should* be used that way, > its just that its what they do. A list comprehension creates a list! > Thats why they are called *list* comprehensions. :-) See, I'd always figured that the reason it was called a list comprehension was because the list comprehension operated on a list, and the operation was comprehension. From alan.gauld at freenet.co.uk Fri Jan 28 23:09:54 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 23:10:16 2005 Subject: [Tutor] Control flow References: <41FA9A6B.8040200@lynuxworks.com> Message-ID: <025801c50586$18d70b30$36bd8651@xp> SEveral solutions here. The best is to restructure the code a little: > def go_jogging(): > # go out and jog > return > > if not bad_weather == 'y': # where is this initially set BTW? go_jogging() else > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() Its shorter, simpler and makes the most common case the default (assuming that bad weather is the exception!) > I can't get the program to stop processing further in the middle Good, that would be really bad practice from a structured programming point of view. :-) But if you really, really must, you could always call raise SystemExit which bombs out more or less immediately - like exit() in C.... > C++ mindset) so I used exception to achieve what I want. Yes thats ok, and the exception to use is already there... > example you could probably manipulate the logic so that program ends at > the bottom of the if-tree. My question is then how to exit in the middle > of a if-then-else tree? You should never need to. One of the things that structured programming (Edsgar Dijkstra to be precise) showed was that you can *always* rearrange things so that goto's and intermediate exits are not needed and indeed can introduce an extra level of complexity and error. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at freenet.co.uk Fri Jan 28 23:12:44 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Jan 28 23:13:04 2005 Subject: [Tutor] Control flow References: Message-ID: <026501c50586$7eb9ceb0$36bd8651@xp> > One way out of the top level is to call > sys.exit(1) Which works, but you need to import sys first. Using raise SystemExit avoids the need for an import. Alan G. From john.ertl at fnmoc.navy.mil Fri Jan 28 23:51:14 2005 From: john.ertl at fnmoc.navy.mil (Ertl, John) Date: Fri Jan 28 23:50:17 2005 Subject: [Tutor] Diffing two files. Message-ID: All, I have two text files that should contain a section of text that is the same. Luckily the section of text has a defined beginning and end. It looks like the most straightforward thing would be to read the targeted text from each file (only 50 lines or so) into lists and then compare the lists. I would think I could use sets to find a unique list (hopefully there would not be anything)...or I could do line by line comparison. Any advise on what is the better method. Should I avoid the list comparison approach...is there a built in way of comparing entire files instead of dealing explicitly with the lines? Thanks, John Ertl From kent37 at tds.net Sat Jan 29 00:13:27 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 29 00:13:30 2005 Subject: [Tutor] Naming conventions In-Reply-To: References: Message-ID: <41FAC717.7000204@tds.net> Terry Carroll wrote: > On Fri, 28 Jan 2005, Kent Johnson wrote: > > >>Separating with underscores is quite common in the Python community, >>actually it is the preferred spelling for library modules. So maybe you >>should adopt that, just to reduce the confusion when your code does have >>an encounter with the outside world :-) > > > I'm a lawyer by trade; my work is *supposed* to confuse the outside world! Then you are using the wrong language entirely. May I suggest Brainf*ck or INTERCAL or Whitespace? http://www.thefreecountry.com/compilers/esoteric.shtml Kent From kent37 at tds.net Sat Jan 29 00:23:05 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 29 00:23:12 2005 Subject: [Tutor] Diffing two files. In-Reply-To: References: Message-ID: <41FAC959.3050803@tds.net> You don't really say what you are trying to accomplish. Do you want to identify the common text, or find the pieces that differ? If the common text is always the same and you know it ahead of time, you can just search the lines of each file to find it. If you need to identify the common part, difflib might be useful. There is an example on this page of finding matching blocks of two sequences: http://docs.python.org/lib/sequencematcher-examples.html In your case the sequences will be lists of lines rather than strings (which are sequences of characters) Kent Ertl, John wrote: > All, > > I have two text files that should contain a section of text that is the > same. Luckily the section of text has a defined beginning and end. It > looks like the most straightforward thing would be to read the targeted text > from each file (only 50 lines or so) into lists and then compare the lists. > I would think I could use sets to find a unique list (hopefully there would > not be anything)...or I could do line by line comparison. Any advise on > what is the better method. Should I avoid the list comparison approach...is > there a built in way of comparing entire files instead of dealing explicitly > with the lines? > > Thanks, > > John Ertl > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From john.ertl at fnmoc.navy.mil Sat Jan 29 01:10:27 2005 From: john.ertl at fnmoc.navy.mil (Ertl, John) Date: Sat Jan 29 01:09:35 2005 Subject: [Tutor] Diffing two files. Message-ID: Kent What I need to do is find what should be common and see if it really is. I have two output files...The output files will have a bunch of systems stuff then the text of interest and then a bunch more systems stuff. The systems stuff may be different for each file but the text of interest will always have a fixed line in front of it and behind it. The idea is to get the text of interest (using the known beginning and ending flags in the text) from each file and then check to make sure the text of interest is the same in both files. I have not done much text stuff so this is new territory for me. I will take a look at difflib. Thanks again John Ertl Simplified example of a text files. Sldfsdf Sdfsdfsf Sdfsdfsdfwefs Sdcfasdsgerg Vsadgfasgdbgdfgsdf -Beginning flag This Text Should be The Same in the other file. -Ending flag Sdfsdfsdfsd Sdfsdfsdfasd Sdfsadfsdf Sdfsadfasdf Sdfsdfasd Sdfasdf s -----Original Message----- From: Kent Johnson [mailto:kent37@tds.net] Sent: Friday, January 28, 2005 15:23 Cc: Tutor@python.org Subject: Re: [Tutor] Diffing two files. You don't really say what you are trying to accomplish. Do you want to identify the common text, or find the pieces that differ? If the common text is always the same and you know it ahead of time, you can just search the lines of each file to find it. If you need to identify the common part, difflib might be useful. There is an example on this page of finding matching blocks of two sequences: http://docs.python.org/lib/sequencematcher-examples.html In your case the sequences will be lists of lines rather than strings (which are sequences of characters) Kent Ertl, John wrote: > All, > > I have two text files that should contain a section of text that is the > same. Luckily the section of text has a defined beginning and end. It > looks like the most straightforward thing would be to read the targeted text > from each file (only 50 lines or so) into lists and then compare the lists. > I would think I could use sets to find a unique list (hopefully there would > not be anything)...or I could do line by line comparison. Any advise on > what is the better method. Should I avoid the list comparison approach...is > there a built in way of comparing entire files instead of dealing explicitly > with the lines? > > Thanks, > > John Ertl > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From butlerrodney at hotmail.com Sat Jan 29 03:14:39 2005 From: butlerrodney at hotmail.com (Rodney Butler) Date: Sat Jan 29 03:15:42 2005 Subject: [Tutor] Please Help with Python Script for Xbox Media Center Message-ID: I have written a script that is designed to load when you start your xbox, it gets a list of your music playlists then lets you select which one to load, shuffle and play, then the script exits. What I need is for the script to automatically select the highlighted playlist if there is no user input for 10 seconds. Been working on it for 3 days, no luck. Help. import os, xbmc, xbmcgui, time try: Emulating = xbmcgui.Emulating except: Emulating = False #get actioncodes from keymap.xml ACTION_PREVIOUS_MENU = 10 class MyClass(xbmcgui.Window): def __init__(self): if Emulating: xbmcgui.Window.__init__(self) self.addControl(xbmcgui.ControlImage(0,0,720,480, "c:\\scripts\\background.jpg")) # Make the List self.list = xbmcgui.ControlList(290, 300, 150, 150) self.addControl(self.list) #Define Playlist Directory here path = 'c:\\albums\\playlists\\' list = os.listdir(path) intcount = len(list) for a in list : self.list.addItem(a) self.setFocus(self.list) def onAction(self, action): if action == ACTION_PREVIOUS_MENU: self.close() def onControl(self, control): if control == self.list: item = self.list.getSelectedItem() path1 = 'c:\\albums\\playlists\\' xbmc.PlayList(0).load(path1+item.getLabel()) xbmc.PlayList(0).shuffle() xbmc.Player().play() self.close() mydisplay = MyClass() mydisplay.doModal() del mydisplay From butlerrodney at hotmail.com Sat Jan 29 03:23:26 2005 From: butlerrodney at hotmail.com (Rodney Butler) Date: Sat Jan 29 03:24:05 2005 Subject: [Tutor] XBMC Python Script HELP! Needed Message-ID: I have written a script that is designed to load when you start your xbox, it gets a list of your music playlists then lets you select which one to load, shuffle and play, then the script exits. What I need is for the script to automatically select the highlighted playlist if there is no user input for 10 seconds. Been working on it for 3 days, no luck. Help. import os, xbmc, xbmcgui, time try: Emulating = xbmcgui.Emulating except: Emulating = False #get actioncodes from keymap.xml ACTION_PREVIOUS_MENU = 10 class MyClass(xbmcgui.Window): def __init__(self): if Emulating: xbmcgui.Window.__init__(self) self.addControl(xbmcgui.ControlImage(0,0,720,480, "c:\\scripts\\background.jpg")) # Make the List self.list = xbmcgui.ControlList(290, 300, 150, 150) self.addControl(self.list) #Define Playlist Directory here path = 'c:\\albums\\playlists\\' list = os.listdir(path) intcount = len(list) for a in list : self.list.addItem(a) self.setFocus(self.list) def onAction(self, action): if action == ACTION_PREVIOUS_MENU: self.close() def onControl(self, control): if control == self.list: item = self.list.getSelectedItem() path1 = 'c:\\albums\\playlists\\' xbmc.PlayList(0).load(path1+item.getLabel()) xbmc.PlayList(0).shuffle() xbmc.Player().play() self.close() mydisplay = MyClass() mydisplay.doModal() del mydisplay From dyoo at hkn.eecs.berkeley.edu Sat Jan 29 03:54:52 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sat Jan 29 03:54:55 2005 Subject: [Tutor] Please Help with Python Script for Xbox Media Center In-Reply-To: Message-ID: On Fri, 28 Jan 2005, Rodney Butler wrote: > I have written a script that is designed to load when you start your > xbox, it gets a list of your music playlists then lets you select which > one to load, shuffle and play, then the script exits. What I need is > for the script to automatically select the highlighted playlist if there > is no user input for 10 seconds. Hi Rodney, It appears that you're using some kind of GUI interface called 'xbmcgui'. Only a few of us have done xbmc stuff; you may have better luck asking from a XBMC-specific game forum. You can set up a thread to do something in the background; I did a quick Google search and picked out: http://www.ek-clan.vxcomputers.com/flashscripts/AQTbrowser.py This appears to use threads to implement a http timeout, and you can probably adapt this to automatically select a playlist if the user hasn't selected one in time. Without having an XBOX, however, I have no way of testing this. So we recommend you talk with someone on one of the forums. Oh! I see that you've just posted on: http://www.xboxmediaplayer.de/cgi-bin/forums/ikonboard.pl?s=0d6bed0f9584b8406d5f458ffaeb187c;act=ST;f=21;t=10108 and have gotten a similar advice to use threads. Ok, good. Use threads. *grin* If you have more questions, please feel free to ask. But the folks on that forum probably know better than us, so try them first. Best of wishes to you! From keridee at jayco.net Sat Jan 29 04:37:15 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 29 04:37:27 2005 Subject: [Tutor] Should this be a list comprehension or something? References: Message-ID: <002801c505b3$e90263e0$a95328cf@JSLAPTOP> > >> Its not so much a criterion that they *should* be used that way, >> its just that its what they do. A list comprehension creates a list! >> Thats why they are called *list* comprehensions. :-) > > See, I'd always figured that the reason it was called a list > comprehension was because the list comprehension operated on a list, > and the operation was comprehension. > It can easily be checked by... Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = range(10) >>> b = [x for x in a if x % 2 == 0] >>> c = ['12','21','14','13'] >>> d = [a[2] for x in c] >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> b [0, 2, 4, 6, 8] >>> c ['12', '21', '14', '13'] >>> d [2, 2, 2, 2] >>> type(a) >>> type(b) >>> type(c) >>> type(d) >>> Everything I did with list comprehensions above shows that list comprehensions return a list. Mostly it is evidenced by the type function... Maybe, this post is silly? Oh, well. Jacob From keridee at jayco.net Sat Jan 29 04:54:08 2005 From: keridee at jayco.net (Jacob S.) Date: Sat Jan 29 04:53:52 2005 Subject: [Tutor] Naming conventions (was: Should this be a list comprehension or something? References: Message-ID: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> You're my best friend. Everyone else looves camelCase, and I hate it too. It doesn't make sense. It doesn't fit English. It doesn't fit Spanish. It doesn't fit any other language AFAIK, so why should a human (who uses spoken language) to computer interpreter use a naming convention that doesn't match spoken language? That's my opinion. Jacob Schmidt > On Wed, 26 Jan 2005, Sean Perry wrote: > >> And now, for the pedant in me. I would recommend against naming >> functions with initial capital letters. In many languages, this implies >> a new type (like your Water class). so CombineWater should be >> combineWater. > > I hate hate hate hate hate camelcase and will never use it. In my book, > if the name has *any* capitals in it, the first letter is capitalized, > too. Anything else is unaesthetic. > > To me, when I have names that are composed of multiple words (say, "rice > quantity"), I have two approaches: distinguishing the words by case > (RiceQuantity) or separating by underscores (rice_quantity). > > I never confuse classes/instances and methods, because I use noun phrases > for classes and instances (HeatedWater, VerifiedInput) and verb phrases > for the methods (CombineWater, CookRice). I suppose I could get > confusion, for example, when the combination either a noun phrase or > verb phrase (SoundOut: is that a name describing the Sound that's being > put Out, or is it a method that's is tentatively Sounding Out somthing?) > but so far that hasn't been an issue for me. > > Of course in my case, I write code only for myself, so I have the luxury > of not worrying about what Joe in the next cubicle is doing, and what Jane > will do when she's trying to read Bob's and my code together. So I have > the luxury of turning my nose up at camelCase. > > I should add that, the one time I made changes to someone else's Python > code for release (a minor patch to nntplib.py), I used the same case > conventions already in place in the module. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From bvande at po-box.mcgill.ca Sat Jan 29 10:10:17 2005 From: bvande at po-box.mcgill.ca (Brian van den Broek) Date: Sat Jan 29 10:13:31 2005 Subject: [Tutor] Should this be a list comprehension or something? In-Reply-To: <022d01c50580$425b9c60$36bd8651@xp> References: <022d01c50580$425b9c60$36bd8651@xp> Message-ID: <41FB52F9.7050903@po-box.mcgill.ca> Alan Gauld said unto the world upon 2005-01-28 16:28: >>>So you've been looking at Eiffel then? >>>:-) >> >>I don't get this joke, but it sounds like the basis for it >>would be interesting. Can you explain? > > > Bertrand Meyer, the inventor of Eiffel uses rich text to display > code in his books. The commercial Eiffel IDE that his company > ISE sells used to display the code the same way. Thus different > fonts were used for comments, keywords etc as well as the > usual syntax colouring. I note that the freeware version of the > tool seems to be more conventional in approach! > Alan G. [Alan posted the Eiffel reference in response to my 'suggestion' we switch to MS Word format for writing code.] Thanks for explaining that, Alan. I'd heard of Eiffel, but that was as far as that went. (I was just going for maximally silly with the Word suggestion.) I am a bit surprised by your description and links. I like scite a lot, but the first time I fired it up and saw my code in a non-monospace font, I just about recoiled in horror. Once I figured out how to switch that, I felt much better, as my lack of previous programming experience meant I didn't have a recoil in horror over Python's use of whitespace. I'd felt left out ;-) Best to all, Brian vdB From cyresse at gmail.com Sat Jan 29 12:43:37 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sat Jan 29 12:43:40 2005 Subject: Fwd: [Tutor] Control flow In-Reply-To: References: <026501c50586$7eb9ceb0$36bd8651@xp> Message-ID: < erk, to the list, to the List!> if ( bad_weather =='y' ): # ask user only if weather is bad. b = input ( "Weather is really bad, still go out to jog?[y/n]" ) if b == 'y': go_jogging() Anyone else notice that he's never gonna go jogging if the weather is bad? Unless I've got input() wrong, it only takes integers... ? 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. -- '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. From cyresse at gmail.com Sat Jan 29 12:46:39 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sat Jan 29 12:46:42 2005 Subject: [Tutor] Naming conventions (was: Should this be a list comprehension or something? In-Reply-To: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> References: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> Message-ID: Just please_don't_use_underscores. They_make_my_eyes_go_funny_, _and_code_hard_to_read_in_my_opinion. _u_n_d_e_r_s_c_o_r_e_s_ _a_r_e__u_g_l_y_.... I got out of the habit of using them really fast. Also, __ & _ tend to have special meaning in Python (which is bad enough as it is), so I don't use them for that reason as well. Liam Clarke On Fri, 28 Jan 2005 22:54:08 -0500, Jacob S. wrote: > You're my best friend. Everyone else looves camelCase, and I hate it too. It > doesn't make sense. It doesn't fit English. > It doesn't fit Spanish. It doesn't fit any other language AFAIK, so why > should a human (who uses spoken language) to computer interpreter use a > naming convention that doesn't match spoken language? That's my opinion. > > Jacob Schmidt > > > > On Wed, 26 Jan 2005, Sean Perry wrote: > > > >> And now, for the pedant in me. I would recommend against naming > >> functions with initial capital letters. In many languages, this implies > >> a new type (like your Water class). so CombineWater should be > >> combineWater. > > > > I hate hate hate hate hate camelcase and will never use it. In my book, > > if the name has *any* capitals in it, the first letter is capitalized, > > too. Anything else is unaesthetic. > > > > To me, when I have names that are composed of multiple words (say, "rice > > quantity"), I have two approaches: distinguishing the words by case > > (RiceQuantity) or separating by underscores (rice_quantity). > > > > I never confuse classes/instances and methods, because I use noun phrases > > for classes and instances (HeatedWater, VerifiedInput) and verb phrases > > for the methods (CombineWater, CookRice). I suppose I could get > > confusion, for example, when the combination either a noun phrase or > > verb phrase (SoundOut: is that a name describing the Sound that's being > > put Out, or is it a method that's is tentatively Sounding Out somthing?) > > but so far that hasn't been an issue for me. > > > > Of course in my case, I write code only for myself, so I have the luxury > > of not worrying about what Joe in the next cubicle is doing, and what Jane > > will do when she's trying to read Bob's and my code together. So I have > > the luxury of turning my nose up at camelCase. > > > > I should add that, the one time I made changes to someone else's Python > > code for release (a minor patch to nntplib.py), I used the same case > > conventions already in place in the module. > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > _______________________________________________ > 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. From kent37 at tds.net Sat Jan 29 15:04:49 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 29 15:04:53 2005 Subject: [Tutor] Diffing two files. In-Reply-To: References: Message-ID: <41FB9801.6000108@tds.net> OK, that is clear. diffutils is probably overkill. A simple loop to accumulate the lines of interest should work. Here is some untested (!) code that may do what you want :-) def getCommonPart(filePath): ''' Get a list containing all the lines of a file that fall between the start and end lines. The returned list does not include the actual start and end lines. If start is not found, returns None. If end is not found, returns the lines after start. ''' start = '-Beginning flag\n' end = '-Ending flag\n' common = None # This will be the list of lines, also a flag of whether start has been seen for line in open(filePath): if common is None and line == start: common = [] elif line == end: break else: common.append(line) return common # Now it's easy to compare the two files: lines1 = getCommonPart(file1) lines2 = getCommonPart(file2) if lines1 != lines2: # Do what you need to do in case of a mismatch... # If you want details of the differences then you might want to use difflib here Kent Ertl, John wrote: > Kent > > What I need to do is find what should be common and see if it really is. I > have two output files...The output files will have a bunch of systems stuff > then the text of interest and then a bunch more systems stuff. The systems > stuff may be different for each file but the text of interest will always > have a fixed line in front of it and behind it. > > The idea is to get the text of interest (using the known beginning and > ending flags in the text) from each file and then check to make sure the > text of interest is the same in both files. > > I have not done much text stuff so this is new territory for me. I will > take a look at difflib. > > Thanks again > > John Ertl > > Simplified example of a text files. > > Sldfsdf > Sdfsdfsf > Sdfsdfsdfwefs > Sdcfasdsgerg > Vsadgfasgdbgdfgsdf > -Beginning flag > This > Text > Should be > The > Same in the other file. > -Ending flag > Sdfsdfsdfsd > Sdfsdfsdfasd > Sdfsadfsdf > Sdfsadfasdf > Sdfsdfasd > Sdfasdf > s > > > -----Original Message----- > From: Kent Johnson [mailto:kent37@tds.net] > Sent: Friday, January 28, 2005 15:23 > Cc: Tutor@python.org > Subject: Re: [Tutor] Diffing two files. > > You don't really say what you are trying to accomplish. Do you want to > identify the common text, or > find the pieces that differ? > > If the common text is always the same and you know it ahead of time, you can > just search the lines > of each file to find it. > > If you need to identify the common part, difflib might be useful. There is > an example on this page > of finding matching blocks of two sequences: > http://docs.python.org/lib/sequencematcher-examples.html > > In your case the sequences will be lists of lines rather than strings (which > are sequences of > characters) > > Kent > > Ertl, John wrote: > >>All, >> >>I have two text files that should contain a section of text that is the >>same. Luckily the section of text has a defined beginning and end. It >>looks like the most straightforward thing would be to read the targeted > > text > >>from each file (only 50 lines or so) into lists and then compare the > > lists. > >>I would think I could use sets to find a unique list (hopefully there > > would > >>not be anything)...or I could do line by line comparison. Any advise on >>what is the better method. Should I avoid the list comparison > > approach...is > >>there a built in way of comparing entire files instead of dealing > > explicitly > >>with the lines? >> >>Thanks, >> >>John Ertl >>_______________________________________________ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Sat Jan 29 15:10:49 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 29 15:10:54 2005 Subject: [Tutor] Naming conventions (was: Should this be a list comprehension or something? In-Reply-To: References: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> Message-ID: <41FB9969.4060600@tds.net> iguessthereisnooptionleftexcepttorunwordstogetherwithoutanykindofbreakatall thatshouldannoyeveryoneequally Kent Liam Clarke wrote: > Just please_don't_use_underscores. > > They_make_my_eyes_go_funny_, _and_code_hard_to_read_in_my_opinion. > > _u_n_d_e_r_s_c_o_r_e_s_ _a_r_e__u_g_l_y_.... > > I got out of the habit of using them really fast. > Also, __ & _ tend to have special meaning in Python (which is bad > enough as it is), so I don't use them for that reason as well. > > > Liam Clarke > On Fri, 28 Jan 2005 22:54:08 -0500, Jacob S. wrote: > >>You're my best friend. Everyone else looves camelCase, and I hate it too. It >>doesn't make sense. It doesn't fit English. >>It doesn't fit Spanish. It doesn't fit any other language AFAIK, so why >>should a human (who uses spoken language) to computer interpreter use a >>naming convention that doesn't match spoken language? That's my opinion. >> >>Jacob Schmidt From bgailer at alum.rpi.edu Sat Jan 29 16:43:00 2005 From: bgailer at alum.rpi.edu (Bob Gailer) Date: Sat Jan 29 16:38:20 2005 Subject: Fwd: [Tutor] Control flow In-Reply-To: References: <026501c50586$7eb9ceb0$36bd8651@xp> Message-ID: <6.1.2.0.0.20050129084204.0369ab80@mail.mric.net> At 04:43 AM 1/29/2005, Liam Clarke wrote: >< erk, to the list, to the List!> > >if ( bad_weather =='y' ): > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > >Anyone else notice that he's never gonna go jogging if the weather is bad? >Unless I've got input() wrong, it only takes integers... ? From the docs: input( [prompt]) Equivalent to eval(raw_input(prompt)). Bob Gailer mailto:bgailer@alum.rpi.edu 303 442 2625 home 720 938 2625 cell -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050129/03a209e0/attachment.htm From kent37 at tds.net Sat Jan 29 17:23:04 2005 From: kent37 at tds.net (Kent Johnson) Date: Sat Jan 29 17:23:08 2005 Subject: Fwd: [Tutor] Control flow In-Reply-To: <6.1.2.0.0.20050129084204.0369ab80@mail.mric.net> References: <026501c50586$7eb9ceb0$36bd8651@xp> <6.1.2.0.0.20050129084204.0369ab80@mail.mric.net> Message-ID: <41FBB868.6060103@tds.net> Bob Gailer wrote: > At 04:43 AM 1/29/2005, Liam Clarke wrote: > >> < erk, to the list, to the List!> >> >> if ( bad_weather =='y' ): >> # ask user only if weather is bad. >> b = input ( "Weather is really bad, still go out to jog?[y/n]" ) >> if b == 'y': >> go_jogging() >> >> Anyone else notice that he's never gonna go jogging if the weather is bad? >> Unless I've got input() wrong, it only takes integers... ? > > > From the docs: > input( [prompt]) > Equivalent to eval(raw_input(prompt)). So, it takes more than just integers, but it won't work the way the OP expects: >>> print input('Type something: ') Type something: 'spam ' * 4 spam spam spam spam >>> print input('Type something: ') Type something: y Traceback (most recent call last): File "", line 1, in ? File "", line 0, in ? NameError: name 'y' is not defined - because eval('y') looks for a variable named y >>> print input('Type something: ') Type something: 'y' y It works with the quotes - it is evaluating a string literal raw_input() would work better. Kent From singingxduck at gmail.com Sat Jan 29 23:22:35 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sat Jan 29 23:22:50 2005 Subject: Fwd: [Tutor] Control flow In-Reply-To: <41FBB868.6060103@tds.net> References: <026501c50586$7eb9ceb0$36bd8651@xp> <6.1.2.0.0.20050129084204.0369ab80@mail.mric.net> <41FBB868.6060103@tds.net> Message-ID: <41FC0CAB.8060507@gmail.com> Kent Johnson wrote: > Bob Gailer wrote: > >> At 04:43 AM 1/29/2005, Liam Clarke wrote: >> >>> < erk, to the list, to the List!> >>> >>> if ( bad_weather =='y' ): >>> # ask user only if weather is bad. >>> b = input ( "Weather is really bad, still go out to jog?[y/n]" ) >>> if b == 'y': >>> go_jogging() >>> >>> Anyone else notice that he's never gonna go jogging if the weather >>> is bad? >>> Unless I've got input() wrong, it only takes integers... ? >> >> >> >> From the docs: >> input( [prompt]) >> Equivalent to eval(raw_input(prompt)). > > > So, it takes more than just integers, but it won't work the way the OP > expects: > >>> print input('Type something: ') > Type something: 'spam ' * 4 > spam spam spam spam > >>> print input('Type something: ') > Type something: y > Traceback (most recent call last): > File "", line 1, in ? > File "", line 0, in ? > NameError: name 'y' is not defined > - because eval('y') looks for a variable named y > > >>> print input('Type something: ') > Type something: 'y' > y > > It works with the quotes - it is evaluating a string literal > > raw_input() would work better. > > Kent > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Or you could just define a variable y and a variable n which equal "y" and "n", respectively. Using raw_input() is probably easier though. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From srini_iyyer_bio at yahoo.com Sat Jan 29 23:34:32 2005 From: srini_iyyer_bio at yahoo.com (Srinivas Iyyer) Date: Sat Jan 29 23:34:36 2005 Subject: [Tutor] range function Message-ID: <20050129223432.84409.qmail@web53509.mail.yahoo.com> Hi: I have bunch of coordinates for various vectors. example: small vecs: name cord. X cord. Y Sector no. smvec1 75 100 1aa smvec2 25 50 1aa smvec3 135 155 1ab large vecs: zone Lvec1 10 50 1aa ut Lvec1 60 110 1aa cd Lvec1 130 180 1ab cd Now I am checking if small vecs are falling in which large vecs. I initially tried by taking sm X and sm Y coordinates into two variables smX smY and similarly LX and LY and checking: if smX >=lX and smY <= LY: print line in large vect +'\t' + line in small vec. Unfortunately this failed. because LX and LY are not matched together. The other way by taking tuples: for line in smallvecs: cols = line.split('\t') smvec_tup = zip(cols[1],cols[2]) for line2 in largevec: cols = line2.split('\t') lvec_tup = zip(cols[1]+'\t'+cols[2]) ## from here I have no clue, :-( ### if smvec_tup is in rane(lvec_tup): print line+'\t'+line2 ultimately I want to say: smvec1 smX smY LX LY Lvec1 cd My question here is that is there any way that i can ask python to check a tuple is falls in the range of other tuple. Also, members could u suggest more pythonian ways to deal with this kind of problem. thank you in advance Srini __________________________________ Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less. http://info.mail.yahoo.com/mail_250 From dyoo at hkn.eecs.berkeley.edu Sun Jan 30 00:41:00 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 30 00:41:04 2005 Subject: [Tutor] range function In-Reply-To: <20050129223432.84409.qmail@web53509.mail.yahoo.com> Message-ID: On Sat, 29 Jan 2005, Srinivas Iyyer wrote: > I have bunch of coordinates for various vectors. > > small vecs: > > name cord. X cord. Y Sector no. > smvec1 75 100 1aa > smvec2 25 50 1aa > smvec3 135 155 1ab > > large vecs: zone > Lvec1 10 50 1aa ut > Lvec1 60 110 1aa cd > Lvec1 130 180 1ab cd > > Now I am checking if small vecs are falling in which > large vecs. Hi Srivivas, Some of the problem statement is confusing me slightly, since there's more infomation here than just "vector" information. Is it accurate to say that the essential part of the data is something like this? ### small_vecs = [ ("smvec1", 75, 100, "1aa"), ("smvec2", 25, 50, "1aa"), ("smvec3", 135, 155, "1ab") ] large_vecs = [ ("Lvec1", 10, 50, "1aa", "ut"), ("Lvec1", 60, 110, "1aa", "cd"), ("Lvec1", 130, 180, "1ab", "cd") ] ### Or do you really just care about a portion of the data? ### small_vecs = [ ("smvec1", 75, 100), ("smvec2", 25, 50,), ("smvec3", 135, 155) ] large_vecs = [ ("Lvec1", 10, 50), ("Lvec1", 60, 110), ("Lvec1", 130, 180) ] ### I'm just trying to digest what part of the program we're trying to solve. *grin* Rather than work on text files directly as part of the algorithm, it might be easier to split the problem into two pieces: Part 1. A function that takes the text file and turns it into a data structure of Python tuples, lists, and numbers. Part 2. A function to do the matching against those data structures. The reason this breakup might be useful is because you can test out the vector-matching part of the program (Part 2) independently of the file-reading-parsing part (Part 1). And parsing files is sometimes really messy, and we often want to keep that messiness localized in one place. As a concrete example, we probably need to do something like: cols = line.split('\t') (x, y) = (int(cols[1]), int(cols[2])) where we have to sprinkle in some string-to-int stuff. > The other way by taking tuples: > > for line in smallvecs: > cols = line.split('\t') > smvec_tup = zip(cols[1],cols[2]) zip() is probably not the best tool here. We can just write out the tuple directly, like this: smvec_tup = (cols[1], cols[2]) zip() is meant for something else: here's an example: ### >>> languages = ["Python", "Perl", "Java", "Ocaml", "C"] >>> prefixes = ["py", "pl", "java", "ml", "c"] >>> languages_and_prefixes = zip(languages, prefixes) >>> >>> print languages_and_prefixes [('Python', 'py'), ('Perl', 'pl'), ('Java', 'java'), ('Ocaml', 'ml'), ('C', 'c')] ### So zip() is more of a bulk-tuple constructing function: it's not really meant to be used for making just a single tuple. It takes in lists of things, and pairs them up. Please feel free to ask more questions about this. Best of wishes to you! From billk at fastmail.fm Sun Jan 30 03:04:56 2005 From: billk at fastmail.fm (Bill Kranec) Date: Sun Jan 30 03:05:01 2005 Subject: [Tutor] carriage return on windows Message-ID: <41FC40C8.1050308@fastmail.fm> Hello, I'm trying to have a loop in a program print a message so I know it's status. Right now I'm using print "Percent completed:" + str(percent) + "\r" Which should send me back to the beginning of the line and overwrite it with a new line. But instead I get: Percent completed: 50 Percent completed: 51 Percent completed: 52 Percent completed: 53 and so on. Am I using this wrong, and if so, what is the right way to implement something like this? Thanks for any help! Bill From amonroe at columbus.rr.com Sun Jan 30 03:18:23 2005 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Sun Jan 30 03:18:58 2005 Subject: [Tutor] carriage return on windows In-Reply-To: <41FC40C8.1050308@fastmail.fm> References: <41FC40C8.1050308@fastmail.fm> Message-ID: <166443577941.20050129211823@columbus.rr.com> > print "Percent completed:" + str(percent) + "\r" Print forces a newline. Try sys.stdout.write instead. Alan From maxnoel_fr at yahoo.fr Sun Jan 30 03:23:56 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 30 03:24:28 2005 Subject: [Tutor] carriage return on windows In-Reply-To: <166443577941.20050129211823@columbus.rr.com> References: <41FC40C8.1050308@fastmail.fm> <166443577941.20050129211823@columbus.rr.com> Message-ID: <8b67b003454f617189945135968facfc@yahoo.fr> On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: > >> print "Percent completed:" + str(percent) + "\r" > > Print forces a newline. > Try sys.stdout.write instead. > > Alan You can also use the following syntax: >>> print "Percent completed:", str(percent), "\r", The trailing comma is NOT a typo, it is intentional. It prevents print from appending a newline. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Sun Jan 30 00:05:38 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 30 03:31:08 2005 Subject: [Tutor] Naming conventions (was: Should this be alist comprehension or something? References: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> <41FB9969.4060600@tds.net> Message-ID: <000101c50673$d0abe8a0$275328cf@JSLAPTOP> I just read your post a heck of alot easier than I read Liam's. ;-) Jacob Schmidt > iguessthereisnooptionleftexcepttorunwordstogetherwithoutanykindofbreakatall > > thatshouldannoyeveryoneequally > > Kent > > Liam Clarke wrote: >> Just please_don't_use_underscores. They_make_my_eyes_go_funny_, >> _and_code_hard_to_read_in_my_opinion. >> >> _u_n_d_e_r_s_c_o_r_e_s_ _a_r_e__u_g_l_y_.... >> >> I got out of the habit of using them really fast. >> Also, __ & _ tend to have special meaning in Python (which is bad >> enough as it is), so I don't use them for that reason as well. >> >> >> Liam Clarke >> On Fri, 28 Jan 2005 22:54:08 -0500, Jacob S. wrote: >> >>>You're my best friend. Everyone else looves camelCase, and I hate it too. >>>It >>>doesn't make sense. It doesn't fit English. >>>It doesn't fit Spanish. It doesn't fit any other language AFAIK, so why >>>should a human (who uses spoken language) to computer interpreter use a >>>naming convention that doesn't match spoken language? That's my opinion. >>> >>>Jacob Schmidt > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From keridee at jayco.net Sun Jan 30 00:01:29 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 30 03:31:14 2005 Subject: [Tutor] Control flow References: <026501c50586$7eb9ceb0$36bd8651@xp> Message-ID: <000001c50673$cf7bcd10$275328cf@JSLAPTOP> I noticed that too, Liam. b = input("Weather is really bad, still go out to jog? [y/n] ") # Would it kill you to have whitespace in a prompt? should really be b = raw_input("Weather is really bad, still go out to jog? [y/n] ") to get the effect he wants. input() doesn't only take integers, it takes valid python objects. Integers are objects, but so are lists, dictionaries, tuples, actually it takes everything, BUT!!! it trys to return a valid python object for input. So it will take a string--don't quote me on this--if you explicitly put the string in quotes. If you don't put the string in quotes, it instead searches the namespaces for that object. So say the user typed in bad_weather when the interpreter gave that prompt. Then, b == "y" evaluates true because bad_weather == "y". Did I explain it right? Or am I trying to explain something you already know? I know I get frustrated when people try to explain concepts that I already know... HTH, Jacob Schmidt >< erk, to the list, to the List!> > > if ( bad_weather =='y' ): > # ask user only if weather is bad. > b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > if b == 'y': > go_jogging() > > Anyone else notice that he's never gonna go jogging if the weather is bad? > Unless I've got input() wrong, it only takes integers... ? > > 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. > > > -- > '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 > > From keridee at jayco.net Sun Jan 30 03:40:10 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 30 03:39:59 2005 Subject: [Tutor] carriage return on windows References: <41FC40C8.1050308@fastmail.fm><166443577941.20050129211823@columbus.rr.com> <8b67b003454f617189945135968facfc@yahoo.fr> Message-ID: <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> I don't think that's what he wants. I think he wants to *overwrite* what's in the shell with new output. For example. Python 2.4 (#Stuff) ... >>> Percent complete: 50 becomes... Python2.4(#Stuff) ... >>> Percent complete: 51 so that the whole line is overwritten. In my experience, this is not possible and if anyone can show me how to do it, I would be grateful. HTH, Jacob > > On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: > >> >>> print "Percent completed:" + str(percent) + "\r" >> >> Print forces a newline. >> Try sys.stdout.write instead. >> >> Alan > > You can also use the following syntax: > > >>> print "Percent completed:", str(percent), "\r", > > The trailing comma is NOT a typo, it is intentional. It prevents print > from appending a newline. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting and > sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From kent37 at tds.net Sun Jan 30 04:11:54 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 30 04:11:59 2005 Subject: [Tutor] carriage return on windows In-Reply-To: <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> References: <41FC40C8.1050308@fastmail.fm><166443577941.20050129211823@columbus.rr.com> <8b67b003454f617189945135968facfc@yahoo.fr> <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> Message-ID: <41FC507A.3070402@tds.net> It seems to work fine in Win2k command shell; try this: >>> import time >>> time.sleep(1) >>> for i in range(9): ... print 'i is', i, '\r', ... time.sleep(1) I get all the output on one line. Kent Jacob S. wrote: > I don't think that's what he wants. I think he wants to *overwrite* > what's in the shell with new output. > For example. > > Python 2.4 (#Stuff) > ... > >>>> > Percent complete: 50 > > > becomes... > > Python2.4(#Stuff) > ... > >>>> > Percent complete: 51 > > so that the whole line is overwritten. In my experience, this is not > possible and if anyone can show me how to do it, > I would be grateful. > > HTH, > Jacob > > >> >> On Jan 30, 2005, at 02:18, R. Alan Monroe wrote: >> >>> >>>> print "Percent completed:" + str(percent) + "\r" >>> >>> >>> Print forces a newline. >>> Try sys.stdout.write instead. >>> >>> Alan >> >> >> You can also use the following syntax: >> >> >>> print "Percent completed:", str(percent), "\r", >> >> The trailing comma is NOT a typo, it is intentional. It prevents print >> from appending a newline. >> >> -- Max >> maxnoel_fr at yahoo dot fr -- ICQ #85274019 >> "Look at you hacker... A pathetic creature of meat and bone, panting >> and sweating as you run through my corridors... How can you challenge >> a perfect, immortal machine?" >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From maxnoel_fr at yahoo.fr Sun Jan 30 04:14:30 2005 From: maxnoel_fr at yahoo.fr (Max Noel) Date: Sun Jan 30 04:15:36 2005 Subject: [Tutor] carriage return on windows In-Reply-To: <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> References: <41FC40C8.1050308@fastmail.fm><166443577941.20050129211823@columbus.rr.com> <8b67b003454f617189945135968facfc@yahoo.fr> <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> Message-ID: <0943e4bba36230c65951716acb5f1241@yahoo.fr> On Jan 30, 2005, at 02:40, Jacob S. wrote: > I don't think that's what he wants. I think he wants to *overwrite* > what's in the shell with new output. > For example. > > > so that the whole line is overwritten. In my experience, this is not > possible and if anyone can show me how to do it, > I would be grateful. > > HTH, > Jacob It *is* possible, that's exactly what my code does (well, as long as you don't run it on Mac OS 9). The carriage return (\r, as opposed to the linefeed \n) moves the cursor to the beginning of the *current* line. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" From keridee at jayco.net Sun Jan 30 04:28:19 2005 From: keridee at jayco.net (Jacob S.) Date: Sun Jan 30 04:28:17 2005 Subject: [Tutor] carriage return on windows References: <41FC40C8.1050308@fastmail.fm><166443577941.20050129211823@columbus.rr.com> <8b67b003454f617189945135968facfc@yahoo.fr> <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> <0943e4bba36230c65951716acb5f1241@yahoo.fr> Message-ID: <000601c5067b$ca454ad0$d25428cf@JSLAPTOP> Thanks Kent and Max!!!!! Wow, I didn't know it did that. I'm too dumb to figure it out on my own I guess... Oh well! I found a cool new thing to play with at least! Thanks, Jacob > > On Jan 30, 2005, at 02:40, Jacob S. wrote: > >> I don't think that's what he wants. I think he wants to *overwrite* >> what's in the shell with new output. >> For example. >> >> >> so that the whole line is overwritten. In my experience, this is not >> possible and if anyone can show me how to do it, >> I would be grateful. >> >> HTH, >> Jacob > > It *is* possible, that's exactly what my code does (well, as long as you > don't run it on Mac OS 9). The carriage return (\r, as opposed to the > linefeed \n) moves the cursor to the beginning of the *current* line. > > -- Max > maxnoel_fr at yahoo dot fr -- ICQ #85274019 > "Look at you hacker... A pathetic creature of meat and bone, panting and > sweating as you run through my corridors... How can you challenge a > perfect, immortal machine?" > > > From singingxduck at gmail.com Sun Jan 30 04:33:30 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Sun Jan 30 04:33:47 2005 Subject: [Tutor] carriage return on windows In-Reply-To: <000601c5067b$ca454ad0$d25428cf@JSLAPTOP> References: <41FC40C8.1050308@fastmail.fm><166443577941.20050129211823@columbus.rr.com> <8b67b003454f617189945135968facfc@yahoo.fr> <001b01c50675$0a2f00c0$275328cf@JSLAPTOP> <0943e4bba36230c65951716acb5f1241@yahoo.fr> <000601c5067b$ca454ad0$d25428cf@JSLAPTOP> Message-ID: <41FC558A.7090402@gmail.com> Jacob S. wrote: > Thanks Kent and Max!!!!! > > Wow, I didn't know it did that. I'm too dumb to figure it out on my > own I guess... > Oh well! I found a cool new thing to play with at least! > > Thanks, > Jacob > > > >> >> On Jan 30, 2005, at 02:40, Jacob S. wrote: >> >>> I don't think that's what he wants. I think he wants to *overwrite* >>> what's in the shell with new output. >>> For example. >>> >>> >>> so that the whole line is overwritten. In my experience, this is not >>> possible and if anyone can show me how to do it, >>> I would be grateful. >>> >>> HTH, >>> Jacob >> >> >> It *is* possible, that's exactly what my code does (well, as long as >> you don't run it on Mac OS 9). The carriage return (\r, as opposed to >> the linefeed \n) moves the cursor to the beginning of the *current* >> line. >> >> -- Max >> maxnoel_fr at yahoo dot fr -- ICQ #85274019 >> "Look at you hacker... A pathetic creature of meat and bone, panting >> and sweating as you run through my corridors... How can you challenge >> a perfect, immortal machine?" >> >> >> > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Just a note: This does not work on IDLE, so for those who try this and are frustrated when it fails, try it in the dos-box (command prompt). -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From cyresse at gmail.com Sun Jan 30 05:43:47 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 30 05:43:51 2005 Subject: [Tutor] Naming conventions (was: Should this be a list comprehension or something? In-Reply-To: <41FB9969.4060600@tds.net> References: <005401c505b6$34cfaf60$a95328cf@JSLAPTOP> <41FB9969.4060600@tds.net> Message-ID: Don't get me wrong, underscores have their place, butNotInEveryVariableName. That_was_only_slightly_less_annoying, however. I propose a new syntax - All methods, shall be called Jacques, or a derivative thereof (Jack, Jake etc.) All variables, Claude. Oh, and you could use funny little symbols like @_> to clarify the whole situation.... (in other words, Python is always more readable then certain camels.) On Sat, 29 Jan 2005 09:10:49 -0500, Kent Johnson wrote: > iguessthereisnooptionleftexcepttorunwordstogetherwithoutanykindofbreakatall > > thatshouldannoyeveryoneequally > > Kent > > Liam Clarke wrote: > > Just please_don't_use_underscores. > > > > They_make_my_eyes_go_funny_, _and_code_hard_to_read_in_my_opinion. > > > > _u_n_d_e_r_s_c_o_r_e_s_ _a_r_e__u_g_l_y_.... > > > > I got out of the habit of using them really fast. > > Also, __ & _ tend to have special meaning in Python (which is bad > > enough as it is), so I don't use them for that reason as well. > > > > > > Liam Clarke > > On Fri, 28 Jan 2005 22:54:08 -0500, Jacob S. wrote: > > > >>You're my best friend. Everyone else looves camelCase, and I hate it too. It > >>doesn't make sense. It doesn't fit English. > >>It doesn't fit Spanish. It doesn't fit any other language AFAIK, so why > >>should a human (who uses spoken language) to computer interpreter use a > >>naming convention that doesn't match spoken language? That's my opinion. > >> > >>Jacob Schmidt > > _______________________________________________ > 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. From roydebox at gmail.com Sun Jan 30 07:47:38 2005 From: roydebox at gmail.com (Roy) Date: Sun Jan 30 07:47:40 2005 Subject: [Tutor] question regarding python exception handling In-Reply-To: <7f5f89bf05012922343cc30637@mail.gmail.com> References: <7f5f89bf05012922343cc30637@mail.gmail.com> Message-ID: <7f5f89bf050129224754bdf10c@mail.gmail.com> Hello, there: I am learning about python exception handling. I am reading "Python in a Nutshell". In the chapter of exception handling, it says: Note that the try/finally form is distinct from the try/except form: a try statement cannot have both except and finally clauses, as execution order might be ambiguous. I don't understand the reason why except and finally clauses cannot be together. I know they can be together in java. how does it cause ambiguous execution order? An example may help me understand. Thank you very much! -- Roy **May I open-source your mind?** From ps_python at yahoo.com Sun Jan 30 09:03:18 2005 From: ps_python at yahoo.com (kumar s) Date: Sun Jan 30 09:03:21 2005 Subject: [Tutor] files in a directory Message-ID: <20050130080318.33317.qmail@web53705.mail.yahoo.com> Hello. I wrote a parser to parse spot intensities. The input to this parser i am giving one single file f1 = open('my_intensity_file.dml','r') int = f1.read().split('\n') my_vals = intParser(int) intParser return a list f2 = open('myvalues.txt','w') for line in my_vals: f2.write(line) f2.write('\n') f2.close() The problem with this approach is that, i have to give on file per a run. I have 50 files to pare and i want to do that in one GO. I kepy those 50 files in one directory. Can any one suggest an approach to automate this process. I tried to use f1 = stdin(...) it did not work. i dont know , possible is that i am using incorrect syntax. Any suggestions. Thank you. K __________________________________ Do you Yahoo!? All your favorites on one personal page – Try My Yahoo! http://my.yahoo.com From python at jayloden.com Sun Jan 30 09:14:08 2005 From: python at jayloden.com (Jay Loden) Date: Sun Jan 30 09:15:37 2005 Subject: [Tutor] files in a directory In-Reply-To: <20050130080318.33317.qmail@web53705.mail.yahoo.com> References: <20050130080318.33317.qmail@web53705.mail.yahoo.com> Message-ID: <200501300314.08600.python@jayloden.com> There's a few ways to accomplish this...the way that comes to mind is: ########################################################## import glob files = glob.glob("/path/to/director/*.dml") # assuming you want only .dml def spot(file): '''search for intensity spots and report them to an output file''' f1 = open('my_intensity_file.dml','r') int = f1.read().split('\n') my_vals ?= intParser(int) intParser return a list f2 ?= open('myvalues.txt','w') # you will want to change this to output mult for line in my_vals: # files, or to at least append instead of overwriting ? f2.write(line) f2.write('\n') f2.close() def main(): for each in files: spot(each) main() ########################################################## Basically, turn the parsing into a function, then create a list of files, and perform the parsing on each file. glob() lets you grab a whole list of files matching the wildcard just like if you typed "ls *.dml" or whatever into a command prompt. There wasn't too much info about specifically how you needed this to work, so this is a rough sketch of what you want. Hopefully it helps. -Jay On Sunday 30 January 2005 03:03 am, kumar s wrote: > Hello. > > I wrote a parser to parse spot intensities. The input > to this parser i am giving one single file > > f1 = open('my_intensity_file.dml','r') > int = f1.read().split('\n') > > my_vals = intParser(int) > > intParser return a list > f2 = open('myvalues.txt','w') > for line in my_vals: > f2.write(line) > f2.write('\n') > > f2.close() > > > The problem with this approach is that, i have to give > on file per a run. I have 50 files to pare and i want > to do that in one GO. I kepy those 50 files in one > directory. Can any one suggest an approach to automate > this process. > > I tried to use f1 = stdin(...) it did not work. i dont > know , possible is that i am using incorrect syntax. > > Any suggestions. > > Thank you. > K > > > > > > > > __________________________________ > Do you Yahoo!? > All your favorites on one personal page ? Try My Yahoo! > http://my.yahoo.com > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From cyresse at gmail.com Sun Jan 30 09:41:19 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 30 09:41:22 2005 Subject: [Tutor] files in a directory In-Reply-To: <20050130080318.33317.qmail@web53705.mail.yahoo.com> References: <20050130080318.33317.qmail@web53705.mail.yahoo.com> Message-ID: Hey Kumar, You redirect stdin a lot. try this. import os def parse(filename): try: f1 = open(filename,'r') except IOError: return #Listdir returns a list of files and sub-dirs, and an attempt #to open a sub-dir raises an IOError. int = f1.read().split('\n') my_vals = intParser(int) intParser return a list f2 = open('myvalues.txt','a') # I appended this one, cos I'm assuming you wanted #all the input in one file. For sep files, I'd just do #something like f2 = open(filename+'.txt','w') for line in my_vals: f2.write(line) f2.write('\n') f2.close() return kx = os.listdir('your_directory_here') for filename in kx: parse(kx) So yeah, os.listdir() is your friend. Just remember the try - IOError block to catch sub-dirs. Standard disclaimer - prolly is a better, cleaner, simpler way to do it. But, this way works. And it's better (imao) than redirecting stdin all the time. Regards, Liam Clarke On Sun, 30 Jan 2005 00:03:18 -0800 (PST), kumar s wrote: > Hello. > > I wrote a parser to parse spot intensities. The input > to this parser i am giving one single file > > f1 = open('my_intensity_file.dml','r') > int = f1.read().split('\n') > > my_vals = intParser(int) > > intParser return a list > f2 = open('myvalues.txt','w') > for line in my_vals: > f2.write(line) > f2.write('\n') > > f2.close() > > The problem with this approach is that, i have to give > on file per a run. I have 50 files to pare and i want > to do that in one GO. I kepy those 50 files in one > directory. Can any one suggest an approach to automate > this process. > > I tried to use f1 = stdin(...) it did not work. i dont > know , possible is that i am using incorrect syntax. > > Any suggestions. > > Thank you. > K > > > __________________________________ > Do you Yahoo!? > All your favorites on one personal page ? Try My Yahoo! > http://my.yahoo.com > _______________________________________________ > 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 ba sic human duty, to take the consequences. From flaxeater at yahoo.com Sun Jan 30 09:53:37 2005 From: flaxeater at yahoo.com (Chad Crabtree) Date: Sun Jan 30 09:53:41 2005 Subject: [Tutor] files in a directory Message-ID: <20050130085337.11788.qmail@web54306.mail.yahoo.com> Well in the same vein as what the others put out there I made a verbose 'ls *.ext' so that you can see how to do it in one go. I figured this would give you enough of an example. You can hard code these things into your program. I used a construct similar to this to create an instant html photo gallery. #!/usr/bin/env python import sys,os print sys.argv if len(sys.argv)<3: print """ Please type dir.py dir= ext= Example dir.py dir=. ext=html To list all the html files in the current directory""" sys.exit() dir=sys.argv[1] ext=sys.argv[2] dir=dir.split('=') ext=ext.split('=') if not (dir[0].lower()=='dir' and ext[0].lower()=='ext'): print """ Invalide Command line systax dir.py dir= ext= """ sys.exit() #now we can get started dirlist=os.listdir(dir[1]) dirlist=[tuple(x.split('.')) for x in dirlist] for x in dirlist: if x[-1]==ext[1]: print '.'.join(x) kumar s wrote: >f2.close() > > >The problem with this approach is that, i have to give >on file per a run. I have 50 files to pare and i want >to do that in one GO. I kepy those 50 files in one >directory. Can any one suggest an approach to automate >this process. > >I tried to use f1 = stdin(...) it did not work. i dont >know , possible is that i am using incorrect syntax. > >Any suggestions. > >Thank you. > __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 From alan.gauld at freenet.co.uk Sun Jan 30 09:56:49 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 30 09:56:39 2005 Subject: [Tutor] carriage return on windows References: <41FC40C8.1050308@fastmail.fm> Message-ID: <004501c506a9$a3403160$d1b48651@xp> > print "Percent completed:" + str(percent) + "\r" > > Which should send me back to the beginning of the line and overwrite it > with a new line. But instead I get: > > Percent completed: 50 > Percent completed: 51 Print always adds a newline unless you put a comma at the end. Unfortunately that results in a character being lost too so I recommend using a tab at the front like this: for n in range(20): print "\tAt line: %5s" % n Also be aware that on sime systems \r is the newline character in which case the method I use is to backspace the number of characters occupied by my data (5 in the case above) using \010. Something like this slightly flawed example: >>> def f(): ... print "Label: ", ... for n in range(5): ... print "%s%3s" % ('\010' * 3, n), ... I'll leave debugging it as an excercise for the reader! :-) HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From dyoo at hkn.eecs.berkeley.edu Sun Jan 30 10:14:52 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Sun Jan 30 10:14:58 2005 Subject: [Tutor] question regarding python exception handling In-Reply-To: <7f5f89bf050129224754bdf10c@mail.gmail.com> Message-ID: On Sat, 29 Jan 2005, Roy wrote: > I am learning about python exception handling. I am reading "Python in a > Nutshell". In the chapter of exception handling, it says: Note that the > try/finally form is distinct from the try/except form: a try statement > cannot have both except and finally clauses, as execution order might be > ambiguous. Hi Roy, Caveat: what I write below might not actually be right! Acording to amk: http://www.amk.ca/python/writing/warts.html under the header "Catching Multiple Exceptions", the reason we can't do it is just pure laziness from Python's implementors. So I might be inventing a line of reasoning that isn't the language designer's original intention. *grin* Let's take a look at a bit of Java code, and then compare that to Python. /*** Java example ***/ PreparedStatement stmt = conn.prepareStatement ("select name from pub_article"); try { ResultSet rs = stmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)); } } catch(SQLException e) { e.printStackTrace(); } finally { stmt.close() } /*****/ Here is a rough translation of that code in Python: ### Python example ### cursor = conn.cursor() try: try: cursor.execute("select name from pub_article") for (name,) in cursor.fetchall(): print name except DatabaseError: traceback.print_exc() finally: cursor.close() ###### > I don't understand the reason why except and finally clauses cannot be > together. I know they can be together in java. how does it cause > ambiguous execution order? An example may help me understand. The block structure in the Java pseudocode: /******/ try { // just some code } catch(...) { // exception-handling block } finally { // finally block } /******/ implies a flow-of-control that is a bit deceptive. In Java, the visual block structure implies that 'catch' and 'finally' behave like peers, like the "if/else if" or "switch/case" flow-of-control statements. We know that when an exception happens, the flow of control does this thing where it goes into "exception-handling block", and then weaves around into the "finally block". But we also hit the finally block even if no exception occurs. And that's where a possible confusion can happen: the 'finally' block visually looks like a peer to the other 'catch' blocks, but it serves an entirely different kind of behavior. Conceptually, the 'finally' block "wraps" around the behavior of the 'catch': it fires off regardless if we hit a catch block or not. Python's syntax makes that the flow of control visible through its block nesting: ### try: try: ... except ...: ... finally: ... ### Here, the block structure makes it clear that the inner try/except stuff is in the context of a try/finally block. This is one case where Python is more verbose than Java. I hope this sounds reasonable. *grin* Best of wishes to you! From alan.gauld at freenet.co.uk Sun Jan 30 11:05:37 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 30 11:05:27 2005 Subject: [Tutor] question regarding python exception handling References: <7f5f89bf05012922343cc30637@mail.gmail.com> <7f5f89bf050129224754bdf10c@mail.gmail.com> Message-ID: <006b01c506b3$3f710010$d1b48651@xp> > a Nutshell". In the chapter of exception handling, it says: Note that > the try/finally form is distinct from the try/except form: a try > statement cannot have both except and finally clauses, as execution > order might be ambiguous. > > I don't understand the reason why except and finally clauses cannot be > together. I know they can be together in java. how does it cause > ambiguous execution order? An example may help me understand. To be honest I've never quite worked this out either. But it's what the language does so I don't lose sleep about it. Instead of try: # some code except anException: # some more code finally: # tidy up now Just do: try: try: # some code except anException: # some more code finally: # tidy up now. For the cost of 4 extra characters and some whitespace I can live with it! :-) FWIW Delphi does exactly the same. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld From cyresse at gmail.com Sun Jan 30 12:09:33 2005 From: cyresse at gmail.com (Liam Clarke) Date: Sun Jan 30 12:09:37 2005 Subject: [Tutor] Ports / sockets? Message-ID: Hi, I was reading an article about 'honeytrapping' ports to slow down port scanners, and it made reference to Perl, but I was wondering, how feasible in Python is a script that waits for requests to (normally unused) ports, and then malforms a response to those requests? I'm aware this is illegal in some US states, but this is more of a 'how would you do that' scenario rather than 'I need to do this'. Sockets tend to use localhost? Java interpreters and Python interpreters all seem to call localhost. So, do sockets use ports? Or, do ports use sockets? Or, do programmes create sockets which can utilise ports? If so, can you access a port directly in Python? I always wanted to be able to the nitty gritty stuff like accessing a particular port, but I always thought I'd have to learn Cpp to do it. 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. From kent37 at tds.net Sun Jan 30 15:42:02 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 30 15:42:08 2005 Subject: [Tutor] Ports / sockets? In-Reply-To: References: Message-ID: <41FCF23A.3080705@tds.net> Liam Clarke wrote: > Hi, > > I was reading an article about 'honeytrapping' ports to slow down port > scanners, and it made reference to Perl, but I was wondering, how > feasible in Python is a script that waits for requests to (normally > unused) ports, and then malforms a response to those requests? This is similar to what a web server or any other type of network server does. Yes, you certainly can do this in Python. > > I'm aware this is illegal in some US states, but this is more of a > 'how would you do that' scenario rather than 'I need to do this'. > > Sockets tend to use localhost? Java interpreters and Python > interpreters all seem to call localhost. localhost is the name of a computer, in particular the computer the program is running on. Sort of like 'self' in a way. To identify a port you need a host name and a port number. localhost is a host name. > So, do sockets use ports? Or, do ports use sockets? Or, do programmes > create sockets which can utilise ports? If so, can you access a port > directly in Python? 'port' is a low-level concept that is pretty closely tied to the actual communications protocol (TCP/IP, for what you want to do). A socket is a programming interface to a port. Sockets use ports. Ports don't use sockets. Programs create sockets either to connect directly to a port on a remote computer or to listen for connections. The first kind is a client socket, for example to fetch a web page from a web server you open up a socket to port 80 on the server. The second kind is a server socket, the web server is listening for clients to connect on port 80. > > I always wanted to be able to the nitty gritty stuff like accessing a > particular port, but I always thought I'd have to learn Cpp to do it. You can definitely do this in Python. The socket module gives you direct access to sockets. You probably don't want to use this except as a learning experience. I can't find good tutorial material on the web, but some info is at http://www.amk.ca/python/howto/sockets/ Generally you will want to program at a higher level than raw sockets. There are *lots* of Python libraries and frameworks to do this. There are three fundamentally different ways to write a server. A synchronous server is the simplest, but it only handles one request at a time so it is not really practical. An asynchronous server handles multiple requests by polling the active sockets to see which ones are ready for service. A threaded server creates a separate thread for each active socket. asyncore and asynchat are relatively simple libraries that let you create an asynchronous server. My understanding is that they are kind of limited and not really recommended for production use. Twisted is the king of Python asynchronous servers, if you are going to do much server writing it is probably worth learning it. Medusa is another robust Python async server. http://twistedmatrix.com/ http://www.amk.ca/python/code/medusa.html http://mail.python.org/pipermail/python-list/2003-January/137318.html SocketServer, BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer all let you create simple synchonous and threaded servers. If you are looking for HTTP servers specifically, there are lots, see here for a list: http://www.python.org/moin/WebProgramming > > Regards, > > Liam Clarke From ps_python at yahoo.com Sun Jan 30 19:41:43 2005 From: ps_python at yahoo.com (kumar s) Date: Sun Jan 30 19:41:47 2005 Subject: [Tutor] files in a directory In-Reply-To: <200501300314.08600.python@jayloden.com> Message-ID: <20050130184144.50211.qmail@web53709.mail.yahoo.com> Thank you Jay. It worked, I am V.V.happy. I tried Liam's suggestion also, but some weird things are going and I am not only getting results but also any error. I am working on that. Other thing. I a feeding my parser some coordinates specified by me, where I am asking the parser to extract the intensity values only for those coordinates. For exmple: Coordinates_file = open('xxx','r') def coOrs(coordinates_file): ................... .................. ## this parse extracts the my specified coordinates# ## and saves as a list for lookup in Intensity File## return my_coordinates_list def intPars er(Intensity File, my_coordinates_list): ................................ ............................... return intensities This above f(x) returns intensities and my coordinates. Now that I am reading many files at once, I wanted, to have a tab delim file op that looks like this: My_coors Int_file 1 Int_file2 IntFile3 01:26 34 235 245.45 04:42 342.4 452.4 45.5 02:56 45.4 34.5 557.8 code: files = glob.glob("My_dir\*.ext") def parSer(file): f1 = open(file,'r') seelf = f1.read().split('\n') seelfile = seelf[24:506969] my_vals = intParser(seelfile,pbs) f2 = open(file+'.txt','w') for line in my_vals: f2.write(line+'\t') => asking for tab delim.. f2.write('\n') f2.close() def main(): for each in files: parSer(each) main() => putting here a '\t' did not work.. . Am i wrong here. Any suggestions, please. Thank you in advance. --- Jay Loden wrote: > There's a few ways to accomplish this...the way that > comes to mind is: > > ########################################################## > import glob > > files = glob.glob("/path/to/director/*.dml") # > assuming you want only .dml > > def spot(file): > '''search for intensity spots and report them to > an output file''' > f1 = open('my_intensity_file.dml','r') > int = f1.read().split('\n') > > my_vals = intParser(int) > > intParser return a list > f2 = open('myvalues.txt','w') # you will want to > change this to output mult > for line in my_vals: # files, or to at least > append instead of overwriting > f2.write(line) > f2.write('\n') > f2.close() > > def main(): > for each in files: > spot(each) > > main() > > ########################################################## > > Basically, turn the parsing into a function, then > create a list of files, and > perform the parsing on each file. glob() lets you > grab a whole list of files > matching the wildcard just like if you typed "ls > *.dml" or whatever into a > command prompt. There wasn't too much info about > specifically how you needed > this to work, so this is a rough sketch of what you > want. Hopefully it helps. > > -Jay > > On Sunday 30 January 2005 03:03 am, kumar s wrote: > > Hello. > > > > I wrote a parser to parse spot intensities. The > input > > to this parser i am giving one single file > > > > f1 = open('my_intensity_file.dml','r') > > int = f1.read().split('\n') > > > > my_vals = intParser(int) > > > > intParser return a list > > f2 = open('myvalues.txt','w') > > for line in my_vals: > > f2.write(line) > > f2.write('\n') > > > > f2.close() > > > > > > The problem with this approach is that, i have to > give > > on file per a run. I have 50 files to pare and i > want > > to do that in one GO. I kepy those 50 files in > one > > directory. Can any one suggest an approach to > automate > > this process. > > > > I tried to use f1 = stdin(...) it did not work. i > dont > > know , possible is that i am using incorrect > syntax. > > > > Any suggestions. > > > > Thank you. > > K > > > > > > > > > > > > > > > > __________________________________ > > Do you Yahoo!? > > All your favorites on one personal page – Try My > Yahoo! > > http://my.yahoo.com > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 From srini_iyyer_bio at yahoo.com Sun Jan 30 21:43:06 2005 From: srini_iyyer_bio at yahoo.com (Srinivas Iyyer) Date: Sun Jan 30 21:43:09 2005 Subject: [Tutor] TypeError: can only concatenate list (not "str") to list Message-ID: <20050130204306.8324.qmail@web53510.mail.yahoo.com> Hello group, I am trying to print rows from two lists together: how can i deal with TypeError' where i have to print a list and a string. for line in pb: # tab delim text with 12 columns cols = line.split('\t') temp_seq = cols[7].split('\n') # extract 7thcol seq = temp_seq[0].split(',') #splitting it by , for nm in seq: for i in range(len(nmrows)): if nm == nmrows[i][0] and nmrows[i][3] < cols[4] and nmrows[i][4] > cols[5]: nmr = nmrows[i] pbr = cols[0] print nmrow[i] +'\t'+cols[0] I tried the following also : I created an empty list outside for loop and tried to extend the elements of the list and string nmr = nmrows[i] pbr = cols[0] result.extend(nmr+'\t'+pbr) # result is the list i created. nmr is a list, and pbr is a string. can any one plaease help. thanks Srini __________________________________ Do you Yahoo!? The all-new My Yahoo! - Get yours free! http://my.yahoo.com From ps_python at yahoo.com Sun Jan 30 21:52:45 2005 From: ps_python at yahoo.com (kumar s) Date: Sun Jan 30 21:52:49 2005 Subject: [Tutor] TypeError: can only concatenate list (not "str") to list In-Reply-To: <20050130204306.8324.qmail@web53510.mail.yahoo.com> Message-ID: <20050130205245.54260.qmail@web53707.mail.yahoo.com> >nmr = nmrows[i] > pbr = cols[0] > print nmrow[i] +'\t'+cols[0] nmr = str(nmrows[i]) pbr = cols[0] print nmrow[i]+'\t'+cols[0] will print what you want. k --- Srinivas Iyyer wrote: > Hello group, > I am trying to print rows from two lists together: > > how can i deal with TypeError' where i have to print > a > list and a string. > > for line in pb: # tab delim text with 12 columns > cols = line.split('\t') > temp_seq = cols[7].split('\n') # extract 7thcol > seq = temp_seq[0].split(',') #splitting it by , > for nm in seq: > for i in range(len(nmrows)): > if nm == nmrows[i][0] and nmrows[i][3] < cols[4] > and nmrows[i][4] > cols[5]: > nmr = nmrows[i] > pbr = cols[0] > print nmrow[i] +'\t'+cols[0] > > > > I tried the following also : > > I created an empty list outside for loop and tried > to > extend the elements of the list and string > > nmr = nmrows[i] > pbr = cols[0] > result.extend(nmr+'\t'+pbr) > > # result is the list i created. nmr is a list, and > pbr > is a string. > > can any one plaease help. > > thanks > Srini > > > > __________________________________ > Do you Yahoo!? > The all-new My Yahoo! - Get yours free! > http://my.yahoo.com > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > __________________________________ Do you Yahoo!? Yahoo! Mail - 250MB free storage. Do more. Manage less. http://info.mail.yahoo.com/mail_250 From kim.branson at gmail.com Sun Jan 30 21:57:33 2005 From: kim.branson at gmail.com (Kim Branson) Date: Sun Jan 30 21:57:42 2005 Subject: [Tutor] rounding In-Reply-To: <20050130184144.50211.qmail@web53709.mail.yahoo.com> References: <20050130184144.50211.qmail@web53709.mail.yahoo.com> Message-ID: <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, heres a quick one for you, I have a series of data that I am using dictionaries to build histograms for. I'd like to round the data to the nearest 10, i.e if the value is 15.34 should we round down to 10? and conversely rounding say 19.30 to 20. I'm thinking 15.5 and above would round up. Can anyone point me a at a quick and painless way of achieving this? cheers Kim -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFB/Uo9er2hmGbHcokRAroEAJ9ALQrg7Ku3K62RNiFy0HUki6LjZwCeL4c3 uAT7UoIMg/IzJavBMR2Wbik= =FwNi -----END PGP SIGNATURE----- From kent37 at tds.net Sun Jan 30 22:10:15 2005 From: kent37 at tds.net (Kent Johnson) Date: Sun Jan 30 22:10:22 2005 Subject: [Tutor] rounding In-Reply-To: <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> References: <20050130184144.50211.qmail@web53709.mail.yahoo.com> <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> Message-ID: <41FD4D37.7030204@tds.net> The round function will do what you want though not quite what you say you want :-) >>> round(15., -1) 20.0 >>> round(15.5, -1) 20.0 >>> round(14, -1) 10.0 15.34 will round to 20, not 10, which is the closest multiple of 10. Kent Kim Branson wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi all, > > heres a quick one for you, > I have a series of data that I am using dictionaries to build histograms > for. I'd like to round the data to the nearest 10, i.e if the value is > 15.34 should we round down to 10? and conversely rounding say 19.30 to > 20. I'm thinking 15.5 and above would round up. Can anyone point me a at > a quick and painless way of achieving this? > > cheers > > Kim > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.4 (Darwin) > > iD8DBQFB/Uo9er2hmGbHcokRAroEAJ9ALQrg7Ku3K62RNiFy0HUki6LjZwCeL4c3 > uAT7UoIMg/IzJavBMR2Wbik= > =FwNi > -----END PGP SIGNATURE----- > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From stygian at tesco.net Sun Jan 30 22:18:56 2005 From: stygian at tesco.net (Glen) Date: Sun Jan 30 22:18:53 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags Message-ID: <1107119936.4118.3.camel@localhost> As a Python/Tkinter newbie, I thought I was getting on ok... then I hit this problem. I have a canvas (c1) A group of objects are drawn on c1 and given a tag c1.addtag_all('group_A') Another group of objects are drawn, I wish to tag these 'group_B'. At the moment I 'get by' with... a=c1.find_withtag('group_A') b=c1.find_all() c=b[len(a):] for i in range(len(c)): c1.addtag_above('group_B',len(a)+i) I'm sure there's a "nicer" method than this, but I can't see it. Any help would be greatly appreciated. Thanks From shaleh at speakeasy.net Sun Jan 30 22:21:00 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Sun Jan 30 22:21:20 2005 Subject: [Tutor] rounding In-Reply-To: <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> References: <20050130184144.50211.qmail@web53709.mail.yahoo.com> <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> Message-ID: <41FD4FBC.8020602@speakeasy.net> Kim Branson wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi all, > > heres a quick one for you, > I have a series of data that I am using dictionaries to build histograms > for. I'd like to round the data to the nearest 10, i.e if the value is > 15.34 should we round down to 10? and conversely rounding say 19.30 to > 20. I'm thinking 15.5 and above would round up. Can anyone point me a at > a quick and painless way of achieving this? > def round10s(f): rem = f % 10 tens = int(f / 10) if rem >= 5.5: return (tens + 1.0) * 10.0 return tens * 10.0 if __name__ == '__main__': tests = [15.34, 15.54, 19.65, 2.34, 8.2, 0.0, 20.0] for i in tests: print "%.2f: %.2f" % (i, round10s(i)) yields 15.34: 10.00 15.54: 20.00 19.65: 20.00 2.34: 0.00 8.20: 10.00 0.00: 0.00 20.00: 20.00 From alan.gauld at freenet.co.uk Sun Jan 30 23:34:47 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 30 23:37:04 2005 Subject: [Tutor] rounding References: <20050130184144.50211.qmail@web53709.mail.yahoo.com> <9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> Message-ID: <002001c5071b$e7d710b0$d1b48651@xp> > histograms for. I'd like to round the data to the nearest 10, i.e if > the value is 15.34 should we round down to 10? and conversely rounding > say 19.30 to 20. I'm thinking 15.5 and above would round up. Can anyone > point me a at a quick and painless way of achieving this? divide by 10, round the result, multiply by 10. HTH, Alan G. From alan.gauld at freenet.co.uk Sun Jan 30 23:37:59 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Sun Jan 30 23:37:40 2005 Subject: [Tutor] rounding References: <20050130184144.50211.qmail@web53709.mail.yahoo.com><9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> <41FD4D37.7030204@tds.net> Message-ID: <002501c5071c$5abcf9a0$d1b48651@xp> > The round function will do what you want though not quite what you say you want :-) > > >>> round(15., -1) > 20.0 Interesting, I didn't remember the second parameter to round... let alone the fact it could be negative!! Batteries included once again! Alan G. From ps_python at yahoo.com Sun Jan 30 23:48:06 2005 From: ps_python at yahoo.com (kumar s) Date: Sun Jan 30 23:48:08 2005 Subject: [Tutor] Append function Message-ID: <20050130224806.51422.qmail@web53708.mail.yahoo.com> Hello: In append function instead of appending one below the other can I append one next to other. I have a bunch of files where the first column is always the same. I want to collect all those files, extract the second columns by file wise and write the first column, followed by the other columns(extracted from files) next to each other. Any tricks , tips and hints. thanks K __________________________________ Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. http://promotions.yahoo.com/new_mail From bkp_80 at hotmail.com Mon Jan 31 00:01:00 2005 From: bkp_80 at hotmail.com (Brandon) Date: Mon Jan 31 00:01:03 2005 Subject: [Tutor] Dividing 1 by another number ? Message-ID: I'm trying to write a program they may involve needing to divide 1 by another number. In the program below when I use 4 for the diameter of the bore, and 1 for the diameter of the rod, and 60 for the PSI, the force should be 706.8 . However the program keeps giving me 0 for "rodarea". If I use a value other than 1 the program works fine. am I missing something, this the 3rd program I have wrote. Thanks #program for calculating cylinder pressure #questions for the user bore = input("What is the diameter of the cylinders bore?") rod = input("what is the diameter of the cylinders rod?") psi = input("What is the operating pressure?") # formulas needed for end-result PI = 3.14 area = (bore**2/4)*PI rodarea = (rod**2/4)*PI force = (area-rodarea)*psi #prints the force the cylinder would have print "The force is", force -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050130/7314de5f/attachment.html From tameyer at ihug.co.nz Mon Jan 31 00:11:11 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Mon Jan 31 00:11:15 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: Message-ID: > I'm trying to write a program they may involve > needing to divide 1 by another number. In the > program below when I use 4 for the diameter of > the bore, and 1 for the diameter of the rod, > and 60 for the PSI, the force should be 706.8 . > However the program keeps giving me 0 for "rodarea". > If I use a value other than 1 the program works > fine. am I missing something, this the 3rd program > I have wrote. [...] > PI = 3.14 > area = (bore**2/4)*PI > rodarea = (rod**2/4)*PI Dividing two integers will give you an integer (truncated) result: >>> 1/2 0 Dividing an integer by a float (or vice-versa) will give you a float: >>> 1/2.0 0.5 >>> 1/float(2) 0.5 If you want '1/2' to give you 0.5 (throughout your script), you can do: >>> from __future__ import division >>> 1/2 0.5 >>> 1//2 0 Notice that '//' (with or without the from __future__ import) will give you the integer result. =Tony.Meyer From shaleh at speakeasy.net Mon Jan 31 00:25:31 2005 From: shaleh at speakeasy.net (Sean Perry) Date: Mon Jan 31 00:25:50 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: References: Message-ID: <41FD6CEB.6090202@speakeasy.net> Tony Meyer wrote: > Dividing two integers will give you an integer (truncated) result: > If you want '1/2' to give you 0.5 (throughout your script), you can do: >>>>from __future__ import division > Notice that '//' (with or without the from __future__ import) will give you > the integer result. > or more simply, divide by 4.0 for rod and bore: (rod**2/4.0) what happens if rodarea is bigger than area? From kent37 at tds.net Mon Jan 31 00:32:43 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 00:32:48 2005 Subject: [Tutor] Append function In-Reply-To: <20050130224806.51422.qmail@web53708.mail.yahoo.com> References: <20050130224806.51422.qmail@web53708.mail.yahoo.com> Message-ID: <41FD6E9B.6030200@tds.net> zip() is your friend. It turns rows into columns and columns into rows. I think this will do what you want, assuming all the first columns are identical (no missing rows in any files), the row values are separated by spaces or tabs, and all the data will fit in memory: def readColumns(filePath): rows = [ line.split() for line in open(filePath) ] return zip(*rows) # list of all the files to read allFiles = [ 'f1.txt', 'f2.txt' ] # both columns from all files allColumns = [ readColumns(filePath) for filePath in allFiles ] # just the second column from all files allSecondColumns = [ cols[1] for cols in allColumns ] # a representative first column col1 = allColumns[0][0] # zip it up into rows allRows = zip(col1, *allSecondColumns) for row in allRows: print '\t'.join(row) Kent kumar s wrote: > Hello: > > In append function instead of appending one below the > other can I append one next to other. > > I have a bunch of files where the first column is > always the same. I want to collect all those files, > extract the second columns by file wise and write the > first column, followed by the other columns(extracted > from files) next to each other. > > Any tricks , tips and hints. > > thanks > K > > > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - Helps protect you from nasty viruses. > http://promotions.yahoo.com/new_mail > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kim.branson at gmail.com Mon Jan 31 00:34:38 2005 From: kim.branson at gmail.com (Kim Branson) Date: Mon Jan 31 00:34:47 2005 Subject: [Tutor] rounding In-Reply-To: <002501c5071c$5abcf9a0$d1b48651@xp> References: <20050130184144.50211.qmail@web53709.mail.yahoo.com><9059255D-7301-11D9-83BF-000A9579AE94@gmail.com> <41FD4D37.7030204@tds.net> <002501c5071c$5abcf9a0$d1b48651@xp> Message-ID: <81E4BF75-7317-11D9-83BF-000A9579AE94@gmail.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 ahhh, the second argument to round. That will do what i want. It was fairly late and i didn't see that argument in the docs. although now i look at it in the light of day its there. neat. I've replaced by divide by 10 stuff with that. now i have different bugs to chase :) cheers Kim On 31/01/2005, at 9:37 AM, Alan Gauld wrote: > >> The round function will do what you want though not quite what you > say you want :-) >> >>>>> round(15., -1) >> 20.0 > > Interesting, I didn't remember the second parameter to round... > let alone the fact it could be negative!! > > Batteries included once again! > > Alan G. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Kim Branson Diffraction and Theory CSIRO Health Sciences and Nutrition 343 Royal Parade, Parkville Melbourne Ph +613 9662 7136 kim.branson@csiro.au -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFB/W8Oer2hmGbHcokRAlRwAJkB1NNVnxUN3i2/4bNyKEeiVorDzQCfXGIo JnnkKWdwrIH9XOkB6gFUFAY= =iCJq -----END PGP SIGNATURE----- From carroll at tjc.com Mon Jan 31 00:39:10 2005 From: carroll at tjc.com (Terry Carroll) Date: Mon Jan 31 00:39:17 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: Message-ID: On Sun, 30 Jan 2005, Brandon wrote: > I'm trying to write a program they may involve needing to divide 1 by > another number. In the program below when I use 4 for the diameter of > the bore, and 1 for the diameter of the rod, and 60 for the PSI, the > force should be 706.8 . However the program keeps giving me 0 for > "rodarea". In addition to the real division issue that's already been pointed out (simplest fix is to divide by 4.0 instead of 4 to avoid this), you've got another problem: > area = (bore**2/4)*PI > rodarea = (rod**2/4)*PI Both of your formulas above are the same. So, where bore = rod, as in your example where they're both equal to 1, area and rodarea are both going to evaluate to the same value: >>> bore = 1 >>> rod = 1 >>> PI = 3.14 >>> area = (bore**2/4.0)*PI >>> rodarea = (rod**2/4.0)*PI >>> area 0.78500000000000003 >>> rodarea 0.78500000000000003 Now, look at your nest line: > force = (area-rodarea)*psi since area is equal to rodarea when bore is equal to rod, area-rodarea will always = 0, and force will equal 0, too, not 706.8 as you're expecting. This isn't just a matter of when you use 1, it's true whenever you use equal values for rod and bore. From tameyer at ihug.co.nz Mon Jan 31 00:45:25 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Mon Jan 31 00:45:31 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: Message-ID: > btw is there a place I can read everyones questions, > like I posted, or does this work my emailing a few > knowledgable people? The archives of the list are available here: (The messages from the future at the top are obviously ones where people's mailers were in poor shape - ignore those). =Tony.Meyer From alan.gauld at freenet.co.uk Mon Jan 31 01:35:50 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 31 01:35:30 2005 Subject: [Tutor] Dividing 1 by another number ? References: Message-ID: <004901c5072c$d1895140$d1b48651@xp> > I'm trying to write a program they may involve needing to > divide 1 by another number. In that case you need it to use floating point numbers. The easiest way is to use 1.0 but if it comes from a table or user entry you might have to explicitly convert: one = 1 other = 42 result = float(one/other) HTH, Alan G. From alan.gauld at freenet.co.uk Mon Jan 31 01:38:16 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 31 01:37:58 2005 Subject: [Tutor] Dividing 1 by another number ? References: Message-ID: <005a01c5072d$27b31e20$d1b48651@xp> > The archives of the list are available here: > > > And on ActiveState's web site they are even searchable! Alan G. From tameyer at ihug.co.nz Mon Jan 31 01:44:49 2005 From: tameyer at ihug.co.nz (Tony Meyer) Date: Mon Jan 31 01:44:56 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: Message-ID: > In that case you need it to use floating point numbers. > The easiest way is to use 1.0 but if it comes from a table > or user entry you might have to explicitly convert: > > one = 1 > other = 42 > result = float(one/other) What Alan meant, presumably, was this: one = 1 other = 42 result = float(one)/other Otherwise the code simply gives 0.0 rather than 0, when we want ~0.0238. Note that it doesn't matter which one you convert to a float, as long as one of them is. =Tony.Meyer From andre.roberge at ns.sympatico.ca Mon Jan 31 05:00:06 2005 From: andre.roberge at ns.sympatico.ca (=?ISO-8859-1?Q?Andr=E9_Roberge?=) Date: Mon Jan 31 04:59:10 2005 Subject: [Tutor] Using exec with dict Message-ID: <41FDAD46.2040307@ns.sympatico.ca> First things first: I am writing a program which has, among other things, an embedded Python interpreter. So, before telling me that "using exec is unsafe because one could enter <...>", consider that the user could just as well enter <...> in the interpreter. (Having gotten this off my chest :-) In "Python in a Nutshell", Martelli writes, in at least two places, something like "you should use exec only with specific, explicit dictionaries". I would like to figure out how to do this [yes, for safety reasons :-), and also because I am curious] and, so far, I have been unsuccesful. I have a "robot" that can do some actions like "move()" and "turn_left()". I can program this robot using python like this: ==== .def move_and_turn(): . move() . turn_left() . .def draw_square(): . for i in range(4): . move_and_turn() . .draw_square() ======== To execute such a program within my larger program, I use exec code in globals() where "code" is the little program above, and it works as expected. The question I have is: how do I do this with an explicit dictionary. I would *guess* that this is somehow equivalent to "how do I create a dictionary that has access only to robot instructions [move(), turn_left(), etc.] and Python's basic syntax" ... but I don't know how to do this. Any help would be appreciated. Andr? From kent37 at tds.net Mon Jan 31 05:11:53 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 05:12:00 2005 Subject: [Tutor] Dividing 1 by another number ? In-Reply-To: References: Message-ID: <41FDB009.1090404@tds.net> Brandon wrote: > PI = 3.14 Incidentally the math module has a more accurate value for pi: >>> import math >>> math.pi 3.1415926535897931 From kent37 at tds.net Mon Jan 31 05:18:48 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 05:18:53 2005 Subject: [Tutor] Using exec with dict In-Reply-To: <41FDAD46.2040307@ns.sympatico.ca> References: <41FDAD46.2040307@ns.sympatico.ca> Message-ID: <41FDB1A8.10806@tds.net> Andr? Roberge wrote: > I have a "robot" that can do some actions like "move()" and > "turn_left()". I can program this robot using python like this: > ==== > .def move_and_turn(): > . move() > . turn_left() > . > .def draw_square(): > . for i in range(4): > . move_and_turn() > . > .draw_square() > ======== > To execute such a program within my larger program, I use > exec code in globals() > > where "code" is the little program above, and it works as expected. The > question I have is: how do I do this with an explicit dictionary. I > would *guess* that this is somehow equivalent to "how do I create a > dictionary that has access only to robot instructions [move(), > turn_left(), etc.] and Python's basic syntax" ... but I don't know how > to do this. myGlobals = { 'move':move, 'turn_left':turn_left } exec code in myGlobals You don't need to add built-ins to myGlobals. Add whatever of your symbols you want the code to have access to. Kent From andre.roberge at ns.sympatico.ca Mon Jan 31 06:02:18 2005 From: andre.roberge at ns.sympatico.ca (=?ISO-8859-1?Q?Andr=E9_Roberge?=) Date: Mon Jan 31 06:01:23 2005 Subject: [Tutor] Using exec with dict Message-ID: <41FDBBDA.2090509@ns.sympatico.ca> Thank you Kent. That worked. ============ Andr? Roberge wrote: >/ I have a "robot" that can do some actions like "move()" and />/ "turn_left()". I can program this robot using python like this: />/ ==== />/ .def move_and_turn(): [snip]// />/ The question I have is: how do I do this with an explicit dictionary. />/ I would *guess* that this is somehow equivalent to "how do I create a />/ dictionary that has access only to robot instructions [move(), />/ turn_left(), etc.] and Python's basic syntax" ... but I don't know how />/ to do this. / myGlobals = { 'move':move, 'turn_left':turn_left } exec code in myGlobals You don't need to add built-ins to myGlobals. Add whatever of your symbols you want the code to have access to. Kent From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 08:22:28 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 08:22:31 2005 Subject: [Tutor] Using exec with dict In-Reply-To: <41FDBBDA.2090509@ns.sympatico.ca> Message-ID: On Mon, 31 Jan 2005, [ISO-8859-1] Andr=E9 Roberge wrote: > >/ I have a "robot" that can do some actions like "move()" and > />/ "turn_left()". I can program this robot using python like this: > />/ =3D=3D=3D=3D > />/ .def move_and_turn(): > [snip]// > />/ The question I have is: how do I do this with an explicit dictionary. > />/ I would *guess* that this is somehow equivalent to "how do I create a > />/ dictionary that has access only to robot instructions [move(), > />/ turn_left(), etc.] and Python's basic syntax" ... but I don't know ho= w > />/ to do this. > / > myGlobals =3D { 'move':move, 'turn_left':turn_left } > exec code in myGlobals > > You don't need to add built-ins to myGlobals. Add whatever of your > symbols you want the code to have access to. Hello! There's one subtlety here: exec() (as well as eval()) will automagically stuff in its own version of a '__builtins__' dictionary if a binding to '__builtins__' doesn't exist. Here's a snippet from the documentation that talks about this: """If the globals dictionary is present and lacks '__builtins__', the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated.""" This is a bit surprising, and caught me off guard when I played with exec myself. There are more details here: http://www.python.org/doc/ref/exec.html http://www.python.org/doc/lib/built-in-funcs.html#l2h-23 So if we really want to limit the global names that the exec-ed code sees, you may want to add one more binding to '__builtins__': ### >>> d =3D {} >>> exec "print int('7')" in d 7 >>> >>> d.keys() ['__builtins__'] >>> >>> >>> d2 =3D {'__builtins__' : {}} >>> >>> exec "print int('7')" in d2 Traceback (most recent call last): File "", line 1, in ? File "", line 1, in ? NameError: name 'int' is not defined ### Hope this helps! From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 08:56:30 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 08:56:34 2005 Subject: [Tutor] Append function In-Reply-To: <20050130224806.51422.qmail@web53708.mail.yahoo.com> Message-ID: On Sun, 30 Jan 2005, kumar s wrote: > I have a bunch of files where the first column is always the same. I > want to collect all those files, extract the second columns by file wise > and write the first column, followed by the other columns(extracted from > files) next to each other. Hi Kumar, Can you show us an example of what you mean? I think you're looking for something takes a list of line elements, like ### lines = """ 1 Alpha 1 Beta 1 Gamma 1 Delta 1 Epsilon """.split('\n') ### and combines input lines with the same first column into something like this: ### 1 Alpha|Beta|Gamma|Delta|Epsilon ### But I'm not positive if something like this is what you're trying to do. You mentioned multiple files: can you show us a small, simple example with two files? An example like this will help clarify the problem, and help us know if we're really answering your question. Best of wishes to you! From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 09:14:16 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 09:14:21 2005 Subject: [Tutor] files in a directory In-Reply-To: <20050130184144.50211.qmail@web53709.mail.yahoo.com> Message-ID: > Now that I am reading many files at once, I wanted, to > have a tab delim file op that looks like this: > > My_coors Int_file 1 Int_file2 > IntFile3 > 01:26 34 235 > 245.45 > 04:42 342.4 452.4 45.5 > 02:56 45.4 34.5 557.8 [code cut] Hi Kumar, Ok, I think I see the source of the bug. Let's check how the code writes each of my_vals: > for line in my_vals: > f2.write(line+'\t') => asking for tab delim.. > f2.write('\n') The problematic statement is the last one: between each of the values, the code writes a newline as well as the tab character. Push the newline-writing code outside of the loop, and you should be ok. As a note: you can simplify the tab-writing code a bit more by using a string's "join()" method. For example: ### >>> "/".join(["this", "is", "a", "test"]) 'this/is/a/test' ### So we can join a bunch of elements together without using an explicit 'for' loop. In your program, you can use join() to create a large string of the my_vals elements with tabs joining them. If you'd like to see more, the library documentation: http://www.python.org/doc/lib/string-methods.html#l2h-192 mentions more information on the join() method and the other methods that strings support. Please feel free to ask more questions on any of this. (But when you reply, try to cut down on the number of lines that you quote from the previous message. I almost missed your question because I couldn't see it at first! *grin*) From alan.gauld at freenet.co.uk Mon Jan 31 09:36:43 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 31 09:36:21 2005 Subject: [Tutor] Dividing 1 by another number ? References: Message-ID: <006101c5076f$fef28c80$d1b48651@xp> > What Alan meant, presumably, was this: > > one = 1 > other = 42 > result = float(one)/other Whoops! Yes indeedy! Alan G. From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 09:48:35 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 09:48:42 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: <1107119936.4118.3.camel@localhost> Message-ID: On Sun, 30 Jan 2005, Glen wrote: > As a Python/Tkinter newbie, I thought I was getting on ok... > then I hit this problem. > > I have a canvas (c1) > A group of objects are drawn on c1 and given a tag > c1.addtag_all('group_A') > Another group of objects are drawn, I wish to tag these 'group_B'. Hi Glen, Ok, so you're looking for something like a hypothetical "find_withouttag" to define 'group_B' then. I don't think such a function exists directly, but we can make it up. > At the moment I 'get by' with... > > a=c1.find_withtag('group_A') > b=c1.find_all() > c=b[len(a):] > for i in range(len(c)): > c1.addtag_above('group_B',len(a)+i) There's one problem here: the system doesn't guarantee that all the 'group_A' elememts will end up at the beginning of 'b', so there's a very strong possibility of tagging the wrong elements here. I think that getting this right will take some more work. Here's a definition of a function called find_withouttag(): ### def find_withouttag(canvas, tag): """Returns all the objects that aren't tagged with 'tag'.""" all_objects = canvas.find_all() tagged_objects = canvas.find_withtag(tag) return subtract_instances(all_objects, tagged_objects) def subtract_instances(l1, l2): """Returns a list of all the object instances in l1 that aren't in l2.""" identity_dict = {} for x in l1: identity_dict[id(x)] = x for x in l2: if id(x) in identity_dict: del identity_dict[id(x)] return identity_dict.values() ### [Side note to other: the work that subtract_instances() does is similar to what IdentityHashMap does in Java: does anyone know if there's already an equivalent dictionary implementation of IdentityHashMap in Python?] We can see how subtract_instances() works on a simple example: ### >>> class MyClass: ... def __init__(self, name): ... self.name = name ... def __repr__(self): ... return str(self.name) ... >>> frodo, elrond, arwen, aragorn, sam, gimli = ( ... map(MyClass, "Frodo Elrond Arwen Aragorn Sam Gimli".split())) >>> hobbits = frodo, sam >>> everyone = frodo, elrond, arwen, aragorn, sam, gimli >>> >>> subtract_instances(everyone, hobbits) [Arwen, Aragorn, Gimli, Elrond] ### This subtract_instances() function should do the trick with instances of the canvas items. There's probably a simpler way to do this --- and there are things we can do to make it more efficient --- but I have to go to sleep at the moment. *grin* Best of wishes to you! From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 10:06:18 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 10:06:24 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: Message-ID: On Mon, 31 Jan 2005, Danny Yoo wrote: > I think that getting this right will take some more work. Here's a > definition of a function called find_withouttag(): [Code cut] Oh! Never mind; this can be a lot simpler. According to the "Gotchas" section of: http://tkinter.unpythonic.net/wiki/Widgets/Canvas the items in a Canvas are actually not object instances themselves, but integers. I made an assumption that canvas items were object instances, so I wrote that subtract_instances() to take care of issues with them. But if canvas items are really integers, then we don't need subtract_instances() at all. We can just use sets and subtract one set from the other. Here is a redefinition of find_withouttag() which should work better: ### from sets import Set def find_withouttag(canvas, tag): """Returns all the objects that aren't tagged with 'tag'.""" all_objects = Set(canvas.find_all()) tagged_objects = Set(canvas.find_withtag(tag)) return all_objects - tagged_objects ### Here's a small example with sets to make it more clear how this set manipulation stuff will work: ### >>> from sets import Set >>> numbers = range(20) >>> primes = [2, 3, 5, 7, 11, 13, 17, 19] >>> Set(numbers) - Set(primes) Set([0, 1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]) ### For more information on sets, see: http://www.python.org/doc/lib/module-sets.html Best of wishes to you! From kent37 at tds.net Mon Jan 31 11:58:38 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 11:58:44 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: References: Message-ID: <41FE0F5E.1060001@tds.net> Danny Yoo wrote: > Oh! Never mind; this can be a lot simpler. According to the "Gotchas" > section of: > > http://tkinter.unpythonic.net/wiki/Widgets/Canvas > > the items in a Canvas are actually not object instances themselves, but > integers. I made an assumption that canvas items were object instances, > so I wrote that subtract_instances() to take care of issues with them. ?? Sets can contain any object that is usable as a dictionary key. > > > But if canvas items are really integers, then we don't need > subtract_instances() at all. We can just use sets and subtract one set > from the other. Here is a redefinition of find_withouttag() which should > work better: > > ### > from sets import Set Note that Python 2.4 has set built-in with the name 'set'. To be compatible with both you could write try: set except NameError: from sets import Set as set > def find_withouttag(canvas, tag): > """Returns all the objects that aren't tagged with 'tag'.""" > all_objects = Set(canvas.find_all()) > tagged_objects = Set(canvas.find_withtag(tag)) > return all_objects - tagged_objects > ### or all_objects = Set(canvas.find_all()) all_objects.difference_update(canvas.find_withtag(tag)) return all_objects Kent From wegster at mindcore.net Mon Jan 31 12:16:36 2005 From: wegster at mindcore.net (Scott W) Date: Mon Jan 31 12:16:40 2005 Subject: [Tutor] help with regexps/filename parsing Message-ID: <41FE1394.8080805@mindcore.net> Hey all. I've got an issue that's been driving me a bit nuts. I'm sure it _can_ be done with a regexp, although I'm missing a piece needed to tie it together to work for all cases. I need to parse out a list of RPMs in this case, but it seems the RPM naming convention has changed, as there are files I'll need to parse that are NOT in the normal name-version-release.arch.rpm format. I need to be able to grab the 'basename' for each file, as well as the version and arch, although these can be done seperately. The problem can be shown by the following list of filenames: XFree86-ISO8859-15-75dpi-fonts-4.3.0-78.EL.i386.rpm (Note the EL embedded in name) xfig-3.2.3d-12.i386.rpm (standard naming) rhel-ig-ppc-multi-zh_tw-3-4.noarch.rpm perl-DateManip-5.42a-0.rhel3.noarch.rpm openoffice.org-style-gnome-1.1.0-16.9.EL.i386.rpm Those should represent the set of variations now possible. I can handle most, but not all of the cases...any suggestions that would cover all of the above allowing the extraction of: basename- in this case: XFree86-ISO8859-15-75dpi-fonts, xfig, rhel-ig-ppc-multi-zh_tw, perl-DateManip, openoffice.org-style-gnome version: 4.3.0-78.EL (yes, including the .EL unfortunately, although I'd be OK without it and munging it on end if needed) 3.2.3d-12 3-4 5.42a-0 1.1.0-16.9.EL arches: i386, i386, noarch, noarch, i386 respectively. Any help greatly appreciated, as I've been beating myself up on this one for a bit. Thanks, Scott From wegster at mindcore.net Mon Jan 31 12:25:22 2005 From: wegster at mindcore.net (Scott W) Date: Mon Jan 31 12:25:27 2005 Subject: [Tutor] help with regexps/filename parsing In-Reply-To: <41FE1394.8080805@mindcore.net> References: <41FE1394.8080805@mindcore.net> Message-ID: <41FE15A2.4090507@mindcore.net> Slight correction which I realized after sending, see below for version/release seperation, which I should have seen but blame lack of sleep ;-) Scott W wrote: > Hey all. > > I've got an issue that's been driving me a bit nuts. I'm sure it _can_ > be done with a regexp, although I'm missing a piece needed to tie it > together to work for all cases. > > I need to parse out a list of RPMs in this case, but it seems the RPM > naming convention has changed, as there are files I'll need to parse > that are NOT in the normal name-version-release.arch.rpm format. > > I need to be able to grab the 'basename' for each file, as well as the > version and arch, although these can be done seperately. The problem > can be shown by the following list of filenames: > > XFree86-ISO8859-15-75dpi-fonts-4.3.0-78.EL.i386.rpm (Note the EL > embedded in name) > xfig-3.2.3d-12.i386.rpm (standard naming) > rhel-ig-ppc-multi-zh_tw-3-4.noarch.rpm > perl-DateManip-5.42a-0.rhel3.noarch.rpm > openoffice.org-style-gnome-1.1.0-16.9.EL.i386.rpm > > Those should represent the set of variations now possible. I can handle > most, but not all of the cases...any suggestions that would cover all of > the above allowing the extraction of: > basename- in this case: > XFree86-ISO8859-15-75dpi-fonts, > xfig, > rhel-ig-ppc-multi-zh_tw, > perl-DateManip, > openoffice.org-style-gnome > > version: > 4.3.0-78.EL (yes, including the .EL unfortunately, although I'd > be OK without it and munging it on end if needed) > 3.2.3d-12 > 3-4 > 5.42a-0 > 1.1.0-16.9.EL corrected versions: 4.3.0 3.2.3d 3 5.42a 1.10 (new) releases: 78.EL 12 4 0 16.9.EL > arches: > i386, > i386, > noarch, > noarch, > i386 > respectively. > > Any help greatly appreciated, as I've been beating myself up on this one > for a bit. > > Thanks, > > Scott > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Mon Jan 31 13:34:21 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 13:34:25 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: <41FE0F5E.1060001@tds.net> References: <41FE0F5E.1060001@tds.net> Message-ID: <41FE25CD.8090105@tds.net> Kent Johnson wrote: > Note that Python 2.4 has set built-in with the name 'set'. To be > compatible with both you could write > try: > set > except NameError: > from sets import Set as set Clarification: you don't _have_ to do this to be compatible with 2.4. The sets module is in both 2.3 and 2.4. The difference in 2.4 is that set is also a built-in data type implemented in C with the same interface as sets.Set. The code above will take advantage of the built-in set if it is available, otherwise use sets.Set. Kent From kent37 at tds.net Mon Jan 31 13:54:33 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 13:54:36 2005 Subject: [Tutor] help with regexps/filename parsing In-Reply-To: <41FE15A2.4090507@mindcore.net> References: <41FE1394.8080805@mindcore.net> <41FE15A2.4090507@mindcore.net> Message-ID: <41FE2A89.5060609@tds.net> This works: names = [ 'XFree86-ISO8859-15-75dpi-fonts-4.3.0-78.EL.i386.rpm', # (Note the EL embedded in name) 'xfig-3.2.3d-12.i386.rpm', # (standard naming) 'rhel-ig-ppc-multi-zh_tw-3-4.noarch.rpm', 'perl-DateManip-5.42a-0.rhel3.noarch.rpm', 'openoffice.org-style-gnome-1.1.0-16.9.EL.i386.rpm', ] import re pattern = r''' (?P.+) -(?P[\w.]+) -(?P[\w.]+) \.(?P\w+) \.rpm ''' patternRe = re.compile(pattern, re.VERBOSE) for name in names: m = patternRe.search(name) if m: print m.group('base', 'version', 'release', 'arch') else: print 'No match:', name I figured this out by working from right to left: - always ends with .rpm - everything back to the next . is the arch - everything back to the first (rightmost) - is the release - everything to the next - is version - everything left is the base name Note the release for perl-DateManip-5.42a-0.rhel3.noarch.rpm is 0.rhel3 not 0 as you gave it. Kent Scott W wrote: > Slight correction which I realized after sending, see below for > version/release seperation, which I should have seen but blame lack of > sleep ;-) > > Scott W wrote: > >> Hey all. >> >> I've got an issue that's been driving me a bit nuts. I'm sure it >> _can_ be done with a regexp, although I'm missing a piece needed to >> tie it together to work for all cases. >> >> I need to parse out a list of RPMs in this case, but it seems the RPM >> naming convention has changed, as there are files I'll need to parse >> that are NOT in the normal name-version-release.arch.rpm format. >> >> I need to be able to grab the 'basename' for each file, as well as the >> version and arch, although these can be done seperately. The problem >> can be shown by the following list of filenames: >> >> XFree86-ISO8859-15-75dpi-fonts-4.3.0-78.EL.i386.rpm (Note the >> EL embedded in name) >> xfig-3.2.3d-12.i386.rpm (standard naming) >> rhel-ig-ppc-multi-zh_tw-3-4.noarch.rpm >> perl-DateManip-5.42a-0.rhel3.noarch.rpm >> openoffice.org-style-gnome-1.1.0-16.9.EL.i386.rpm >> >> Those should represent the set of variations now possible. I can >> handle most, but not all of the cases...any suggestions that would >> cover all of the above allowing the extraction of: >> basename- in this case: >> XFree86-ISO8859-15-75dpi-fonts, >> xfig, >> rhel-ig-ppc-multi-zh_tw, >> perl-DateManip, >> openoffice.org-style-gnome >> >> version: >> 4.3.0-78.EL (yes, including the .EL unfortunately, although I'd >> be OK without it and munging it on end if needed) >> 3.2.3d-12 >> 3-4 >> 5.42a-0 >> 1.1.0-16.9.EL > > > corrected versions: > 4.3.0 > 3.2.3d > 3 > 5.42a > 1.10 > > (new) releases: > 78.EL > 12 > 4 > 0 > 16.9.EL > > >> arches: >> i386, >> i386, >> noarch, >> noarch, >> i386 >> respectively. >> >> Any help greatly appreciated, as I've been beating myself up on this >> one for a bit. >> >> Thanks, >> >> Scott >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From sigurd at 12move.de Mon Jan 31 14:03:38 2005 From: sigurd at 12move.de (Karl =?iso-8859-1?Q?Pfl=E4sterer?=) Date: Mon Jan 31 14:13:37 2005 Subject: [Tutor] help with regexps/filename parsing In-Reply-To: <41FE1394.8080805@mindcore.net> (Scott W.'s message of "Mon, 31 Jan 2005 06:16:36 -0500") References: <41FE1394.8080805@mindcore.net> Message-ID: On 31 Jan 2005, wegster@mindcore.net wrote: > I've got an issue that's been driving me a bit nuts. I'm sure it _can_ > be done with a regexp, although I'm missing a piece needed to tie it > together to work for all cases. > > I need to parse out a list of RPMs in this case, but it seems the RPM > naming convention has changed, as there are files I'll need to parse > that are NOT in the normal name-version-release.arch.rpm format. > > I need to be able to grab the 'basename' for each file, as well as the > version and arch, although these can be done seperately. The problem > can be shown by the following list of filenames: > > XFree86-ISO8859-15-75dpi-fonts-4.3.0-78.EL.i386.rpm (Note the EL > embedded in name) > xfig-3.2.3d-12.i386.rpm (standard naming) > rhel-ig-ppc-multi-zh_tw-3-4.noarch.rpm > perl-DateManip-5.42a-0.rhel3.noarch.rpm > openoffice.org-style-gnome-1.1.0-16.9.EL.i386.rpm Perhaps try the following regexp. import sre reg = sre.compile( r''' (?P ^[-\w]+ # name is the match in our string which can consist of # nearly everything (\.\D[-\w]+?)?) # if it contains a point it is followed by a non-digit char # we search till we find - # a hyphen (?P \d+[-.]\d+ # version always starts with one or more digits a hyphen or a point # and one or more digits .*) # we grab everything else till \. # we find a point (?P .+?) # arch is the shortest everything between .rpm and the point \.rpm$''' , sre.X) In an interactive session I get (with names being a list with the names of the rpms): ,---- | >>> for name in names: | m = reg.search(name) | print m.groupdict() | ... ... ... | {'version': '4.3.0-78.EL', 'arch': 'i386', 'name': 'XFree86-ISO8859-15-75dpi-fonts'} | {'version': '3.2.3d-12', 'arch': 'i386', 'name': 'xfig'} | {'version': '3-4', 'arch': 'noarch', 'name': 'rhel-ig-ppc-multi-zh_tw'} | {'version': '5.42a-0.rhel3', 'arch': 'noarch', 'name': 'perl-DateManip'} | {'version': '1.1.0-16.9.EL', 'arch': 'i386', 'name': 'openoffice.org-style-gnome'} `---- I'm not sure about the version of perl-DateManip; you didn't include the trailing 'rhel3'. Did you forget it or should that be trimmed? But it shouldn't be too complicated to augment the above regexp in that way if needed. Karl -- Please do *not* send copies of replies to me. I read the list From sigurd at 12move.de Mon Jan 31 14:31:20 2005 From: sigurd at 12move.de (Karl =?iso-8859-1?Q?Pfl=E4sterer?=) Date: Mon Jan 31 14:33:52 2005 Subject: [Tutor] help with regexps/filename parsing In-Reply-To: <41FE15A2.4090507@mindcore.net> (Scott W.'s message of "Mon, 31 Jan 2005 06:25:22 -0500") References: <41FE1394.8080805@mindcore.net> <41FE15A2.4090507@mindcore.net> Message-ID: On 31 Jan 2005, wegster@mindcore.net wrote: > > Slight correction which I realized after sending, see below for > version/release seperation, which I should have seen but blame lack of > sleep ;-) > corrected versions: > 4.3.0 > 3.2.3d > 3 > 5.42a > 1.10 > > (new) releases: > 78.EL > 12 > 4 > 0 > 16.9.EL > > >> arches: >> i386, >> i386, >> noarch, >> noarch, >> i386 The regexp for the above could be: reg = sre.compile( r''' (?P ^[-\w]+ # name is the match in our string which can consist of # nearly everything (\.\D[-\w]+?)?) # if it contains a point it is followed by a non-digit char # we search till we find - # a hyphen (?P \d+(\.\w+)*) # a version consists of digits and points # we search till we find - # a hyphen (?P [\d.]+(EL)?) # release consists of digits seperated by a point and ends # optionally with EL \.(\w*\.)? # no comes a point and optionally a string (?P \w+?) # arch is the shortest everything between .rpm and the point \.rpm$''' , sre.X) ,---- | >>> for name in names: | m = reg.search(name) | print m.groupdict() | ... ... ... | {'release': '78.EL', 'version': '4.3.0', 'arch': 'i386', 'name': 'XFree86-ISO8859-15-75dpi-fonts'} | {'release': '12', 'version': '3.2.3d', 'arch': 'i386', 'name': 'xfig'} | {'release': '4', 'version': '3', 'arch': 'noarch', 'name': 'rhel-ig-ppc-multi-zh_tw'} | {'release': '0', 'version': '5.42a', 'arch': 'noarch', 'name': 'perl-DateManip'} | {'release': '16.9.EL', 'version': '1.1.0', 'arch': 'i386', 'name': 'openoffice.org-style-gnome'} | >>> `---- Karl -- Please do *not* send copies of replies to me. I read the list From keridee at jayco.net Mon Jan 31 15:05:21 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 31 15:05:32 2005 Subject: [Tutor] TypeError: can only concatenate list (not "str") to list References: <20050130205245.54260.qmail@web53707.mail.yahoo.com> Message-ID: <003e01c5079d$fd0ec270$895428cf@JSLAPTOP> You can also do... Umm, if you're going to make nmr and pbr the values you're printing, why are you printing the values? Nevermind, look at this instead. BTW, aren't rows the horizontal things on tables? nmr = nmrows[i] pbr = cols[0] print "%s\t%s" % (nmr,pbr) > >nmr = nmrows[i] >> pbr = cols[0] >> print nmrow[i] +'\t'+cols[0] > > nmr = str(nmrows[i]) > pbr = cols[0] > > print nmrow[i]+'\t'+cols[0] > > will print what you want. > > k > --- Srinivas Iyyer wrote: > >> Hello group, >> I am trying to print rows from two lists together: >> >> how can i deal with TypeError' where i have to print >> a >> list and a string. >> >> for line in pb: # tab delim text with 12 columns >> cols = line.split('\t') >> temp_seq = cols[7].split('\n') # extract 7thcol >> seq = temp_seq[0].split(',') #splitting it by , >> for nm in seq: >> for i in range(len(nmrows)): >> if nm == nmrows[i][0] and nmrows[i][3] < cols[4] >> and nmrows[i][4] > cols[5]: >> nmr = nmrows[i] >> pbr = cols[0] >> print nmrow[i] +'\t'+cols[0] >> >> >> >> I tried the following also : >> >> I created an empty list outside for loop and tried >> to >> extend the elements of the list and string >> >> nmr = nmrows[i] >> pbr = cols[0] >> result.extend(nmr+'\t'+pbr) >> >> # result is the list i created. nmr is a list, and >> pbr >> is a string. >> >> can any one plaease help. >> >> thanks >> Srini >> >> >> >> __________________________________ >> Do you Yahoo!? >> The all-new My Yahoo! - Get yours free! >> http://my.yahoo.com >> >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail - 250MB free storage. Do more. Manage less. > http://info.mail.yahoo.com/mail_250 > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > From keridee at jayco.net Mon Jan 31 15:48:10 2005 From: keridee at jayco.net (Jacob S.) Date: Mon Jan 31 15:48:14 2005 Subject: [Tutor] Better structure? Message-ID: <000301c507a3$f3828c90$7d5428cf@JSLAPTOP> I think this thing is screaming for better structure, but previous attempts at using oop for it have failed. I think Derintegral is okay, I'm focusing on FunctionGrapher5.py--but you can comment on both. (To Johan Nilsson: I haven't had time to implement Simpson's rule instead of Reimann's sum yet... but I have gotten it to work faster anyway.) I will probably expand the code yet... Add extra things like find area between curve and x axis in interval [a,b]... Any suggestions are greatly appreciated. If you want the code to work on your machine, you'll need to have my Derintegral.py sys.path, obviously. #### Start of FunctionGrapher5.py ################### from __future__ import division from visual import * import Derintegral import os, random from math import * ja = 0 scenexdefault = 375 def start(): for objects in scene.objects: objects.visible = 0 scene.title = "Function Grapher by Jacob, Inc." scene.x = scenexdefault scene.visible=1 scene.exit=0 scene.userspin = 0 scene.range=(10,10,1) scene.background=(1,1,1) global xaxis global yaxis xaxis = curve(pos=[(100,0,0),(-100,0,0)],color=color.black) yaxis = curve(pos=[(0,100,0),(0,-100,0)],color=color.black) global radiusaxis global radiusaxis2 radiusaxis = curve(pos=[(-100,-100),(100,100)],color=color.black) radiusaxis2 = curve(pos=[(-100,100),(100,-100)],color=color.black) radiusaxis.visible = 0 radiusaxis2.visible = 0 start() d = 1 print """\ List of Commands: clear quit remove lines return lines gotl # (Graph One Tangent Line) w/optional x coordinate gatl # (Graph All Tangent Lines) w/optional step gonl # (Graph One Normal Line) w/optional x coordinate ganl # (Graph All Normal Lines) w/optional step Function Syntax: [y,x,or r] = function [range] ["s" followed by float for step] Brackets mean that it's not required. So, in effect, you don't have to type a function """ def graphit(type,f,range2,step): li = curve(color=color.blue) if type in ['y','x']: x = -15 radiusaxis.visible = 0 radiusaxis2.visible = 0 while -15 <= x <= 15: if eval(range2): try: a = f(x) if -15 <= a <= 15: if type == 'y': li.append(pos=(x,a,0)) elif type == 'x': li.append(pos=(a,x,0)) else: li = curve(color=color.blue) except: pass else: li = curve(color=color.blue) x = x+step elif type == 'r': exec "def m(t): return f(t)*cos(t)" exec "def n(t): return f(t)*sin(t)" t = 0 while 0 <= t <= 10*pi: if eval(range2): try: li.append(pos=(m(t),n(t),0)) except: li = curve(color=color.blue) t = t+step print 'Please type in functions in below. ' while 1: lists=[] y = raw_input(">") if y == 'clear': scene.visible=0 start() print "-"*36 continue elif y == 'quit': scene.visible = 0 del scene break elif y == 'remove lines': a = [radiusaxis,radiusaxis2,xaxis,yaxis] for x in a: x.visible = 0 d = 0 continue elif y == 'return lines': a = [radiusaxis,radiusaxis2,xaxis,yaxis] for x in a: x.visible = 1 d = 1 continue elif y.startswith('gatl'): try: step2 = float(y.lstrip("gatl ")) except: step2 = 0.5 x = -20 while -20 <=x<= 20: try: der = Derintegral.diffatpt(f,x) if abs(der)<=25: if type == 'y': curve(pos=[(-15,eval("der*(-15-x)+f(x)")),(15,eval("der*(15-x)+f(x)"))],color=color.red) else: curve(pos=[(eval("der*(-15-x)+f(x)"),-15),(eval("der*(15-x)+f(x)"),15)],color=color.red) except: pass x = x + step2 continue elif y.startswith('ganl'): try: step2 = float(y.lstrip("ganl ")) except: step2 = 0.5 x = -20 while -20 <=x<= 20: try: der = Derintegral.diffatpt(f,x) if abs(der)<=25: if type == 'y': curve(pos=[(-15,eval("(-1/der)*(-15-x)+f(x)")),(15,eval("(-1/der)*(15-x)+f(x)"))],color = color.red) else: curve(pos=[(eval("(-1/der)*(-15-x)+f(x)"),-15),(eval("(-1/der)*(15-x)+f(x)"),15)],color = color.red) except: pass x = x + step2 continue elif y.startswith('gotl'): try: x = float(y.lstrip('gotl ')) except: x = float(raw_input('What x coordinate do you want the tangent line at? ')) der = Derintegral.diffatpt(f,x) try: if abs(der)<= 25: if type == 'y': curve(pos=[(-15,eval("der*(-15-x)+f(x)")),(15,eval("der*(15-x)+f(x)"))],color=color.red) else: curve(pos=[(eval("der*(-15-x)+f(x)"),-15),(eval("der*(15-x)+f(x)"),15)],color=color.red) except: pass continue elif y.startswith('gonl'): try: x = float(y.lstrip('gonl ')) except: x = float(raw_input('What x coordinate do you want the tangent line at? ')) der = Derintegral.diffatpt(f,x) try: if abs(der)<= 25: if type == 'y': curve(pos=[(-15,eval("(-1/der)*(-15-x)+f(x)")),(15,eval("(-1/der)*(15-x)+f(x)"))],color=color.red) else: curve(pos=[(eval("(-1/der)*(-15-x)+f(x)"),-15),(eval("(-1/der)*(15-x)+f(x)"),15)],color=color.red) except: pass continue elif y.startswith('getpt'): y = y.lstrip("getpt ") if y: m = float(eval(y)) else: m = float(eval(raw_input("f(?) "))) try: print f(m) except ValueError: print "Math Domain Error" continue elif y == 'regraph': graphit(type,f,range2,step) continue if y.count(" = ") == 1: y = y.split(" = ") type = y[0].lower() y = y[1] y = y.replace("y","x") if type == 'r': defaultrange = '0<=t<=5*pi' y = y.replace('x','t') if d == 1: radiusaxis.visible = 1 radiusaxis2.visible = 1 else: defaultrange = '-15<=x<=15' else: type = 'y' defaultrange = '-15<=x<=15' y = y.split(" ",1) tempfunct = y.pop(0) if y: y = y[0] if y.count('st'): if y.startswith('st'): y = y.lstrip('st') step = float(y) else: y = y.split(" ") step = float(y.pop().lstrip('st')) y = " ".join(y) range2 = defaultrange else: range2 = y step = 0.005 else: range2 = defaultrange step = 0.005 y = tempfunct range2 = range2.replace('and','or') range2 = range2.replace(',','<=x<=') try: exec "def f(x): return %s" % y except: continue graphit(type,f,range2,step) ###### End of FunctionGrapher5.py ########### #### Start of Derintegral.py ########## from __future__ import division import psyco psyco.full() from math import * def diffatpt(funct,pt): """Return the derivative of a function at a specific point. """ sminc = 1e-10 x = pt+sminc y2 = funct(x) x2 = x x = pt-sminc y1 = funct(x) x1 = x slope = (y2-y1)/(x2-x1) return slope def defintegral(fofx,x,max1): total = 0 step = 1e-5 exec "def f(x): return %s" % fofx while x <= max1: try: total = total+f(x) except: pass x = x+step return abs(total*step) ######End of Derintegral.py ########## Like I said, any comments appreciated. Jacob Schmidt From john.ertl at fnmoc.navy.mil Mon Jan 31 17:35:45 2005 From: john.ertl at fnmoc.navy.mil (Ertl, John) Date: Mon Jan 31 17:34:50 2005 Subject: [Tutor] Diffing two files. Message-ID: Thanks, So simple...DAAAAAA just do an equivalency on the list...no need to do sets or step through each line. John -----Original Message----- From: Kent Johnson [mailto:kent37@tds.net] Sent: Saturday, January 29, 2005 06:05 Cc: Tutor@python.org Subject: Re: [Tutor] Diffing two files. OK, that is clear. diffutils is probably overkill. A simple loop to accumulate the lines of interest should work. Here is some untested (!) code that may do what you want :-) def getCommonPart(filePath): ''' Get a list containing all the lines of a file that fall between the start and end lines. The returned list does not include the actual start and end lines. If start is not found, returns None. If end is not found, returns the lines after start. ''' start = '-Beginning flag\n' end = '-Ending flag\n' common = None # This will be the list of lines, also a flag of whether start has been seen for line in open(filePath): if common is None and line == start: common = [] elif line == end: break else: common.append(line) return common # Now it's easy to compare the two files: lines1 = getCommonPart(file1) lines2 = getCommonPart(file2) if lines1 != lines2: # Do what you need to do in case of a mismatch... # If you want details of the differences then you might want to use difflib here Kent Ertl, John wrote: > Kent > > What I need to do is find what should be common and see if it really is. I > have two output files...The output files will have a bunch of systems stuff > then the text of interest and then a bunch more systems stuff. The systems > stuff may be different for each file but the text of interest will always > have a fixed line in front of it and behind it. > > The idea is to get the text of interest (using the known beginning and > ending flags in the text) from each file and then check to make sure the > text of interest is the same in both files. > > I have not done much text stuff so this is new territory for me. I will > take a look at difflib. > > Thanks again > > John Ertl > > Simplified example of a text files. > > Sldfsdf > Sdfsdfsf > Sdfsdfsdfwefs > Sdcfasdsgerg > Vsadgfasgdbgdfgsdf > -Beginning flag > This > Text > Should be > The > Same in the other file. > -Ending flag > Sdfsdfsdfsd > Sdfsdfsdfasd > Sdfsadfsdf > Sdfsadfasdf > Sdfsdfasd > Sdfasdf > s > > > -----Original Message----- > From: Kent Johnson [mailto:kent37@tds.net] > Sent: Friday, January 28, 2005 15:23 > Cc: Tutor@python.org > Subject: Re: [Tutor] Diffing two files. > > You don't really say what you are trying to accomplish. Do you want to > identify the common text, or > find the pieces that differ? > > If the common text is always the same and you know it ahead of time, you can > just search the lines > of each file to find it. > > If you need to identify the common part, difflib might be useful. There is > an example on this page > of finding matching blocks of two sequences: > http://docs.python.org/lib/sequencematcher-examples.html > > In your case the sequences will be lists of lines rather than strings (which > are sequences of > characters) > > Kent > > Ertl, John wrote: > >>All, >> >>I have two text files that should contain a section of text that is the >>same. Luckily the section of text has a defined beginning and end. It >>looks like the most straightforward thing would be to read the targeted > > text > >>from each file (only 50 lines or so) into lists and then compare the > > lists. > >>I would think I could use sets to find a unique list (hopefully there > > would > >>not be anything)...or I could do line by line comparison. Any advise on >>what is the better method. Should I avoid the list comparison > > approach...is > >>there a built in way of comparing entire files instead of dealing > > explicitly > >>with the lines? >> >>Thanks, >> >>John Ertl >>_______________________________________________ >>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 maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor From stygian at tesco.net Mon Jan 31 18:54:00 2005 From: stygian at tesco.net (Glen) Date: Mon Jan 31 18:53:54 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags Message-ID: <1107194040.3989.1.camel@localhost> On Mon, 2005-01-31 at 12:34, Kent Johnson wrote: > Kent Johnson wrote: > > Note that Python 2.4 has set built-in with the name 'set'. To be > > compatible with both you could write > > try: > > set > > except NameError: > > from sets import Set as set > > Clarification: you don't _have_ to do this to be compatible with 2.4. The sets module is in both 2.3 > and 2.4. The difference in 2.4 is that set is also a built-in data type implemented in C with the > same interface as sets.Set. The code above will take advantage of the built-in set if it is > available, otherwise use sets.Set. > In 2.4 I tried 'from sets import set' Traceback (most recent call last): File "/home/glen/set4.py", line 1, in -toplevel- from sets import set ImportError: cannot import name set It did allow 'from sets import *' but when using the set command I got some odd output... from sets import * a=[12,54,67,47,98,76] b=[47,54] print set(a)-set(b) >>> set(['dog','sheep,'cow']) set([76, 98, 67, 12]) >>> It seems to save the output from the last use, and reproduces it later, even after a system reboot I got the same output. Using the inbuilt commands works fine. From stygian at tesco.net Mon Jan 31 18:55:24 2005 From: stygian at tesco.net (Glen) Date: Mon Jan 31 18:55:20 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags Message-ID: <1107194124.3989.3.camel@localhost> On Mon, 2005-01-31 at 09:06, Danny Yoo wrote > > Here's a small example with sets to make it more clear how this set > manipulation stuff will work: > > ### > >>> from sets import Set > >>> numbers = range(20) > >>> primes = [2, 3, 5, 7, 11, 13, 17, 19] > >>> Set(numbers) - Set(primes) > Set([0, 1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]) > ### Thanks Danny, sets work great (inbuilt for 2.4) I'd never heard of them before today. I look forward to finding other uses for them. Now I've just got to work out how to tag a list of id's... There doesn't seem to be a way to tag a known id, it has to be tagged by reference from an id above or below which seems odd! From glingl at aon.at Mon Jan 31 19:09:59 2005 From: glingl at aon.at (Gregor Lingl) Date: Mon Jan 31 19:08:37 2005 Subject: [Tutor] How to sum rows and columns of a matrix? Message-ID: <41FE7477.4020403@aon.at> Hi all of you, I'm representing a 4x4 matrix as a 16-element list, e.g. m=range(16) first 4 elements first row, second four elements second row etc. I want to sum rows and columns like i-th row: sum(m[4*i:4*i+4]) and ith column: sum(m[i::4]) This seems to be slow because of the formation of the slices. I wonder if there is a way using generators or generator-expressions (which I didn't study yet) to compute these sums without copying parts of the matrix to a new list. (I'd guess that there should exist some canonical generator for sequences, which produces their elements ..., maybe also for slices ?) All comments, hints, solutions are welcome. Regards, Gregor -- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27 Autor von "Python f?r Kids" Website: python4kids.net From cyresse at gmail.com Mon Jan 31 19:39:10 2005 From: cyresse at gmail.com (Liam Clarke) Date: Mon Jan 31 19:39:12 2005 Subject: [Tutor] How to sum rows and columns of a matrix? In-Reply-To: <41FE7477.4020403@aon.at> References: <41FE7477.4020403@aon.at> Message-ID: There's a specific package for arrays http://www.stsci.edu/resources/software_hardware/numarray that implements array mathematics. I use it for pixel map manipulation in pygame, so it's relatively fast. On Mon, 31 Jan 2005 19:09:59 +0100, Gregor Lingl wrote: > Hi all of you, > > I'm representing a 4x4 matrix as a 16-element list, e.g. > > m=range(16) > > first 4 elements first row, second four elements second row etc. > I want to sum rows and columns like > > i-th row: > > sum(m[4*i:4*i+4]) > > and ith column: > > sum(m[i::4]) > > This seems to be slow because of the formation of the slices. > I wonder if there is a way using generators or generator-expressions > (which I didn't study yet) to compute these sums without copying > parts of the matrix to a new list. (I'd guess that there should exist > some canonical generator for sequences, which produces their elements > ..., maybe also for slices ?) > > All comments, hints, solutions are welcome. > > Regards, > Gregor > > -- > Gregor Lingl > Reisnerstrasse 3/19 > A-1030 Wien > > Telefon: +43 1 713 33 98 > Mobil: +43 664 140 35 27 > > Autor von "Python f?r Kids" > Website: python4kids.net > _______________________________________________ > 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. From alan.gauld at freenet.co.uk Mon Jan 31 19:51:56 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 31 19:51:26 2005 Subject: [Tutor] Better structure? References: <000301c507a3$f3828c90$7d5428cf@JSLAPTOP> Message-ID: <00af01c507c5$f06ec470$d1b48651@xp> > def start(): .... lots of lines... > global xaxis > global yaxis Its traditional to put global statements at the top of the function. Also you only need one line to list all of the global variables > global radiusaxis > global radiusaxis2 Similarly here., and again you can put all four in one place. > radiusaxis2.visible = 0 And since there is no input parameter and no return statement and you only call start() once... > start() Why bother with defining a function? Just put all the code inline and omit the globals and it will be the same... but better is probably to jkeep the function and use if __name__ == '__main__; start() > def graphit(type,f,range2,step): > li = curve(color=color.blue) > if type in ['y','x']: > x = -15 > radiusaxis.visible = 0 > radiusaxis2.visible = 0 > while -15 <= x <= 15: > if eval(range2): > try: > a = f(x) > if -15 <= a <= 15: > if type == 'y': > li.append(pos=(x,a,0)) > elif type == 'x': > li.append(pos=(a,x,0)) > else: > li = curve(color=color.blue) > except: > pass This is iffy. If *anthing* happens you ignore it. Now suppose due to a bug you go into an infinite loop and you try to stop using CTRL-C - it won't work! If you must use a catch-all except then do something with it - print a message at least! I got a sore head after that... I'll leave the rest for others to remark upon. :-) Alan G. From stygian at tesco.net Mon Jan 31 19:58:04 2005 From: stygian at tesco.net (Glen) Date: Mon Jan 31 19:57:57 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: <41FE7700.4030609@tds.net> References: <1107194040.3989.1.camel@localhost> <41FE7700.4030609@tds.net> Message-ID: <1107197884.4239.3.camel@localhost> On Mon, 2005-01-31 at 18:20, Kent Johnson wrote: > > In 2.4 I tried 'from sets import set' > > should be 'from sets import Set' - the class in the sets module is called Set, the builtin class is set. > I tried it as 'set' and 'Set' but got the same result > > > > Traceback (most recent call last): > > File "/home/glen/set4.py", line 1, in -toplevel- > > from sets import set > > ImportError: cannot import name set > > > > It did allow 'from sets import *' but when using the set command > > I got some odd output... > > > > from sets import * > > a=[12,54,67,47,98,76] > > b=[47,54] > > print set(a)-set(b) > > > > > > set(['dog','sheep,'cow']) > > set([76, 98, 67, 12]) > > ?? Where did the animals come from?? That's the odd output, that was from a previous set command, even after a reboot it still came out the same. Inbuilt set works fine. From gtsang at lnxw.com Mon Jan 31 21:29:25 2005 From: gtsang at lnxw.com (Gilbert Tsang) Date: Mon Jan 31 21:29:44 2005 Subject: [Tutor] Control flow In-Reply-To: <000001c50673$cf7bcd10$275328cf@JSLAPTOP> References: <026501c50586$7eb9ceb0$36bd8651@xp> <000001c50673$cf7bcd10$275328cf@JSLAPTOP> Message-ID: <41FE9525.3000307@lynuxworks.com> Thanks for the enthusiasm on how input/raw_input() works - my original intention was to ask a question on control flow so I didn't spend that much time testing out this piece of input code besides typing. But I did learn a lot. Thanks! Gilbert Jacob S. wrote: > I noticed that too, Liam. > b = input("Weather is really bad, still go out to jog? [y/n] ") # > Would it kill you to have whitespace in a prompt? > should really be > b = raw_input("Weather is really bad, still go out to jog? [y/n] ") > to get the effect he wants. > > input() doesn't only take integers, it takes valid python objects. > Integers are objects, but so are lists, dictionaries, tuples, > actually it takes everything, BUT!!! it trys to return a valid python > object for input. > So it will take a string--don't quote me on this--if you explicitly > put the string in quotes. > If you don't put the string in quotes, it instead searches the > namespaces for that object. > So say the user typed in bad_weather when the interpreter gave that > prompt. Then, b == "y" evaluates true because bad_weather == "y". Did > I explain it right? Or am I trying to explain something you already > know? I know I get frustrated when people try to explain concepts that > I already know... > > HTH, > Jacob Schmidt > >> < erk, to the list, to the List!> >> >> if ( bad_weather =='y' ): >> # ask user only if weather is bad. >> b = input ( "Weather is really bad, still go out to jog?[y/n]" ) >> if b == 'y': >> go_jogging() >> >> Anyone else notice that he's never gonna go jogging if the weather is >> bad? >> Unless I've got input() wrong, it only takes integers... ? >> >> 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. >> >> >> -- >> '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 maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor From jhomme at libcom.com Mon Jan 31 21:42:00 2005 From: jhomme at libcom.com (jhomme) Date: Mon Jan 31 21:43:57 2005 Subject: [Tutor] Dictionary Nesting Message-ID: -----Original message----- From: Orri Ganel singingxduck@gmail.com Date: Sat, 29 Jan 2005 17:22:48 -0500 To: Kent Johnson kent37@tds.net Subject: Re: Fwd: [Tutor] Control flow > Kent Johnson wrote: > > > Bob Gailer wrote: > > > >> At 04:43 AM 1/29/2005, Liam Clarke wrote: > >> > >>> < erk, to the list, to the List!> > >>> > >>> if ( bad_weather =='y' ): > >>> # ask user only if weather is bad. > >>> b = input ( "Weather is really bad, still go out to jog?[y/n]" ) > >>> if b == 'y': > >>> go_jogging() > >>> > >>> Anyone else notice that he's never gonna go jogging if the weather > >>> is bad? > >>> Unless I've got input() wrong, it only takes integers... ? > >> > >> > >> > >> From the docs: > >> input( [prompt]) > >> Equivalent to eval(raw_input(prompt)). > > > > > > So, it takes more than just integers, but it won't work the way the OP > > expects: > > >>> print input('Type something: ') > > Type something: 'spam ' * 4 > > spam spam spam spam > > >>> print input('Type something: ') > > Type something: y > > Traceback (most recent call last): > > File "", line 1, in ? > > File "", line 0, in ? > > NameError: name 'y' is not defined > > - because eval('y') looks for a variable named y > > > > >>> print input('Type something: ') > > Type something: 'y' > > y > > > > It works with the quotes - it is evaluating a string literal > > > > raw_input() would work better. > > > > Kent > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > Or you could just define a variable y and a variable n which equal "y" > and "n", respectively. Using raw_input() is probably easier though. > > -- > Email: singingxduck AT gmail DOT com > AIM: singingxduck > Programming Python for the fun of it. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Hi, If I want to put a dictionary in a dictionary, does the syntax for assigning and getting at the stuff in the inner dictionary look something like this: outer_dictionary[inner_dictionary][key] = 'value' ? Thanks. Jim From dyoo at hkn.eecs.berkeley.edu Mon Jan 31 21:44:41 2005 From: dyoo at hkn.eecs.berkeley.edu (Danny Yoo) Date: Mon Jan 31 21:44:47 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: <1107192752.3546.7.camel@localhost> Message-ID: > Now I've just got to work out how to tag a list of id's... There > doesn't seem to be a way to tag a known id, it has to be tagged by > reference from an id above or below which seems odd! Hi Glen, Have you tried the addtag_withtag() method? It looks like it should be able to do what you're thinking of. The documentation here: http://tkinter.unpythonic.net/pydoc/Tkinter.Canvas.html should talk about addtag_withtag(). From kent37 at tds.net Mon Jan 31 22:07:04 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 22:07:08 2005 Subject: [Tutor] Newbie struggling with Tkinter/canvas tags In-Reply-To: References: Message-ID: <41FE9DF8.7030606@tds.net> Danny Yoo wrote: > >>Now I've just got to work out how to tag a list of id's... There >>doesn't seem to be a way to tag a known id, it has to be tagged by >>reference from an id above or below which seems odd! > > > Hi Glen, > > Have you tried the addtag_withtag() method? It looks like it should be > able to do what you're thinking of. The documentation here: > > http://tkinter.unpythonic.net/pydoc/Tkinter.Canvas.html > > should talk about addtag_withtag(). itemconfig() also looks promising. http://www.pythonware.com/library/tkinter/introduction/x2017-concepts.htm Kent From kent37 at tds.net Mon Jan 31 22:14:00 2005 From: kent37 at tds.net (Kent Johnson) Date: Mon Jan 31 22:14:03 2005 Subject: [Tutor] Dictionary Nesting In-Reply-To: References: Message-ID: <41FE9F98.8020404@tds.net> jhomme wrote: > Hi, > If I want to put a dictionary in a dictionary, does the syntax for assigning and getting at the > stuff in the inner dictionary look something like this: > outer_dictionary[inner_dictionary][key] = 'value' > ? If inner_dictionary is the key to outer_dictionary, then that is right. For example, Create the outer dict: >>> outer = {} Create an inner dict: >>> outer['outerkey'] = {} Add a key/value pair to the inner dict >>> outer['outerkey']['innerkey'] = 'myvalue' >>> outer {'outerkey': {'innerkey': 'myvalue'}} Retrieve the inner dict: >>> outer['outerkey'] {'innerkey': 'myvalue'} Retrieve an entry from the inner dict: >>> outer['outerkey']['innerkey'] 'myvalue' setdefault() is handy here because you may not know if the inner dict has been created yet when you want to add an entry. If not, setdefault will do it for you: >>> outer.setdefault('outerkey2', {})['innerkey2'] = 'anotherValue' >>> outer {'outerkey2': {'innerkey2': 'anotherValue'}, 'outerkey': {'innerkey': 'myvalue'}} If the inner dict is there already setdefault will use it: >>> outer.setdefault('outerkey2', {})['innerkey3'] = 'yetAnotherValue' >>> outer {'outerkey2': {'innerkey2': 'anotherValue', 'innerkey3': 'yetAnotherValue'}, 'outerkey': {'innerkey': 'myvalue'}} Kent > > Thanks. > > Jim > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at freenet.co.uk Mon Jan 31 22:44:11 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Mon Jan 31 22:44:07 2005 Subject: [Tutor] Dictionary Nesting References: Message-ID: <002001c507de$00b87570$68b78851@xp> > If I want to put a dictionary in a dictionary, does the syntax > for assigning and getting at the stuff in the inner dictionary > look something like this: outer_dictionary[inner_dictionary][key] = 'value' Yes. Sometimes it's easier with Python to just try it at the >>> prompt rather than ask the question... >>> d1 = {'a':1,'b':2} >>> d2 = {'alpha':d1, 'beta': {'c':3,'d':4}} >>> print d2['alpha']['a'] 1 >>> d2['beta']['d'] = 42 >>> print d2['beta']['d'] 42 HTH, Alan G From singingxduck at gmail.com Mon Jan 31 22:48:39 2005 From: singingxduck at gmail.com (Orri Ganel) Date: Mon Jan 31 22:48:53 2005 Subject: [Tutor] How to sum rows and columns of a matrix? In-Reply-To: <41FE7477.4020403@aon.at> References: <41FE7477.4020403@aon.at> Message-ID: <41FEA7B7.6010706@gmail.com> Gregor Lingl wrote: > Hi all of you, > > I'm representing a 4x4 matrix as a 16-element list, e.g. > > m=range(16) > > first 4 elements first row, second four elements second row etc. > I want to sum rows and columns like > > i-th row: > > sum(m[4*i:4*i+4]) > > and ith column: > > sum(m[i::4]) > > This seems to be slow because of the formation of the slices. > I wonder if there is a way using generators or generator-expressions > (which I didn't study yet) to compute these sums without copying > parts of the matrix to a new list. (I'd guess that there should exist > some canonical generator for sequences, which produces their elements > ..., maybe also for slices ?) > > All comments, hints, solutions are welcome. > > Regards, > Gregor > The way I usually handle this is to have a 2-d array, such that every list within the main list is a row: >>> m = list(range(4) for i in range(4)) >>> m [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] >>> m[0][0] = 1 >>> m [[1, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] >>> m = [range(4) for i in range(4)] >>> m [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] >>> m[0][0] = 1 >>> m Either of the above works. Then, to some a row, you just do this: >>> m [[1, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] >>> sum(m[0]) 7 -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. From shitizb at yahoo.com Mon Jan 31 23:50:34 2005 From: shitizb at yahoo.com (Shitiz Bansal) Date: Mon Jan 31 23:50:38 2005 Subject: [Tutor] Traffic Network simulator Message-ID: <20050131225034.38172.qmail@web53810.mail.yahoo.com> Hi, I need a traffic network simulator(for roads) for my project work.I would prefer the simulator to be in python so that i can reprogram/modify it according to my needs. does anyone know where i can find something of the sort or are there any special modules available which can help me build one? Shitiz __________________________________ Do you Yahoo!? All your favorites on one personal page – Try My Yahoo! http://my.yahoo.com From adriaan at kredcorgroup.com Fri Jan 28 08:42:53 2005 From: adriaan at kredcorgroup.com (Adriaan Louw) Date: Fri Mar 4 07:36:54 2005 Subject: [Tutor] Newbie question. Message-ID: <001601c5050c$de90dfb0$0408a8c0@Client98> Good day I'm totally new to Python. Wow, what a great language!!! I want to learn python as quick as possible, for web programming at first, and applications later on. Any leads? I have an xp windows and ms office machine. (I'm considering linux, but it's a bit daunting?) How do I run python scripts? Nothing happens when I double-click the filename, in IDLE I only get the code, and not what it does. I know that cgi-scripts must be called from html. I want to to use a python server for testing. Which is thebest, and how? Maybe my problem lies with directories? Sub-folders and paths. That's what I gather from the tutorial, but I have no success as yet. Even pygame, from Start>Programs>Examples, the games doesn't work. Sorry for all the questions, but I want to learn and grow. Thank you. Adriaan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050128/3fa6ec45/attachment.html From alan.gauld at freenet.co.uk Sat Jan 15 10:29:26 2005 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Fri Mar 4 07:37:38 2005 Subject: [Tutor] Intro for interfacing with Microsoft Access? References: Message-ID: <021d01c4fae4$b09eef70$40b68651@xp> > Does anyone know of any online resource that explains how to interface to > Microsoft Access via Python, where the intended audience is someone who > knows Python, but not the Microsoft innards? There is an ODBC module that should allow you to spit SQL at Access. Alan G From alipolatel at yahoo.com Sun Jan 30 01:23:31 2005 From: alipolatel at yahoo.com (Ali Polatel) Date: Fri Mar 4 07:38:14 2005 Subject: [Tutor] Proxy Message-ID: <20050130002329.4200.qmail@web61003.mail.yahoo.com> is it possible to connect to somewhere through a proxy while using sockets module of Python to connect? If yes can you show me some basic examples? regards --------------------------------- Do you Yahoo!? Yahoo! Search presents - Jib Jab's 'Second Term' -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20050130/4f6cfb50/attachment.htm