Re: [Tutor] Why this error ?
Other than outdenting the whole thing, are you using a 'non-standard' character set? Are you sure that '(' isn't an alternative encoding variant that python isn't recognising as a parenthesis? I've see nthat sometimes with Japanese or unicode encoding. Just a thought... (Just to check, I tried your code in ERIC and it ran fine for me once the indenting was fixed up) I have this code: import MySQLdb conn = MySQLdb.connect (host = localhost, user = testuser, passwd = testpass, db = test) cursor = conn.cursor () cursor.execute (SELECT VERSION()) row = cursor.fetchone () print server version:, row[0] cursor.close () conn.close () that gives me the error: ./mysql.py: line 3: syntax error near unexpected token `(' ./mysql.py: line 3: ` conn = MySQLdb.connect (host = localhost, user = testuser, passwd = testpass, db = test)' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 2.5 and PHP 5.2.0
Hi Guys, I need some wisdom from you. Is it possible to have PHP pages posting to python scripts on the server side and returning values back to the calling PHP files? Like, if my enterval.php form's action=think.py, would the two scripts be able to talk to each other? Thanks and Best Regards, Shortash -- The Stupidry Foundry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 2.5 and PHP 5.2.0
OkaMthembo wrote: Hi Guys, I need some wisdom from you. Is it possible to have PHP pages posting to python scripts on the server side and returning values back to the calling PHP files? Like, if my enterval.php form's action=think.py, would the two scripts be able to talk to each other? A client that is accessing your page never sees your PHP. they just see an html file with a form in it. when they submit the form, the python file will be called and its output will be displayed in the client's browser. I can think of a roundabout way to do what you want: PHP file returns a form to the client. when this form is submitted to the py file, all of the data your py script needs is sent in the query string or whatever. the py file does whatever it does, and then inside the html it returns to the client, it includes an instant redirect back to the PHP file with whatever data you want to return. However, this is a bad way to go about it. You should be able to use python scripts on the server side without relying on the client's browser to pass data. Not exactly sure how to, though. May depend on your OS. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 2.5 and PHP 5.2.0
Luke Paireepinart schrieb: I can think of a roundabout way to do what you want: PHP file returns a form to the client. when this form is submitted to the py file, all of the data your py script needs is sent in the query string or whatever. the py file does whatever it does, and then inside the html it returns to the client, it includes an instant redirect back to the PHP file with whatever data you want to return. The Python script can access the PHP page on the server directly, by just making a request to localhost (if the PHP page is on the same server). You could use urllib or some higher level HTTP client library and pass the data to the PHP page either in a GET or POST request. The PHP page looks just like any other web service to the Python script. Based on the results from the request, it can then return HTML to the client with a redirect to the PHP script, so the user can see the updated data, or an error message. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 2.5 and PHP 5.2.0
Thank you s much gentlemen, you have come to my aid very promptly indeed :-) I have to say, this is why i love Open Source; For the People, By the People! Thankee Shortash On 2/1/07, Christopher Arndt [EMAIL PROTECTED] wrote: Luke Paireepinart schrieb: I can think of a roundabout way to do what you want: PHP file returns a form to the client. when this form is submitted to the py file, all of the data your py script needs is sent in the query string or whatever. the py file does whatever it does, and then inside the html it returns to the client, it includes an instant redirect back to the PHP file with whatever data you want to return. The Python script can access the PHP page on the server directly, by just making a request to localhost (if the PHP page is on the same server). You could use urllib or some higher level HTTP client library and pass the data to the PHP page either in a GET or POST request. The PHP page looks just like any other web service to the Python script. Based on the results from the request, it can then return HTML to the client with a redirect to the PHP script, so the user can see the updated data, or an error message. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- The Stupidry Foundry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] The best way to implement a config file ???
Hi everyone I'm learning to program python with wxpython gui. I have a application i made with several modules and i want to implement a configfile that applies to all those modules. Is there an easy way to do this ? Can anyone point me in the right direction ? Thanks Magnus ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The best way to implement a config file ???
* Magnus Wirström [EMAIL PROTECTED] [070201 14:38]: Hi everyone I'm learning to program python with wxpython gui. I have a application i made with several modules and i want to implement a configfile that applies to all those modules. Is there an easy way to do this ? Can anyone point me in the right direction ? Depending upon your tastes: ConfigFile = win.ini style config files. shlex = unix shell script style parsing. Andreas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] VOT - Similar list for Ruby?
Hello all, I may be about to switch jobs to an environment in which the main utility language is Ruby. I've found this group to be brilliant in the last few years, and wondered if anyone on the list is also a Ruby user, and could recommend a similarly helpful, patient and informative list? Thanks, S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] VOT - Similar list for Ruby?
-Original Message- Hello all, I may be about to switch jobs to an environment in which the main utility language is Ruby. I've found this group to be brilliant in the last few years, and wondered if anyone on the list is also a Ruby user, and could recommend a similarly helpful, patient and informative list? Thanks, S. When I started getting interested in Ruby, the only list I found was Ruby Talk. See this page for a list of mail lists: http://www.ruby-lang.org/en/community/mailing-lists/ Maybe someone deeper into the Ruby community might have a better suggestion. Mike - NOTICE: This e-mail transmission and any documents or files attached to it contain information for the sole use of the above-identified individual or entity. Its contents may be privileged, confidential, and exempt from disclosure under the law. Any dissemination, distribution, or copying of this communication is strictly prohibited. Please notify the sender immediately if you are not the intended recipient. FGNS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing txt files in landscape from python
Hi All, do you have any idea, how I can send a txt file to the default printer in landscape view with python on windows. I wanted to set up just the char size and the orientation of the printout. thinking about os.system('notepad.exe /pt %%%s' % filename) Yours sincerely, __ János Juhász ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
On Thu, 1 Feb 2007, Mário Gamito wrote: I'm new to Python, so forgive me the lame question. I have this code (following my signature) and i'm trying to get the value of content and insert it into a MySQL database. Hi Mario, Wait, wait, before we continue here: did you finally get things working from your last question? http://mail.python.org/pipermail/tutor/2007-February/052387.html That thread was so full of confusion and a mixture of correct and incorrect advice that I felt a bit bad about it. I'd feel better if I knew for certain that you were able to resolve what was going on. Can you summarize the situation now?___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing txt files in landscape from python
János Juhász schrieb: do you have any idea, how I can send a txt file to the default printer in landscape view with python on windows. I assume that by txt file, you mean a file containing ASCII text? I wanted to set up just the char size and the orientation of the printout. Printers normally don't understand ASCII file sent to them, unless you configure them (with some status codes) to do so. Normally, the OS converts a text file sent to its printing system to something the printer understands, like PostScript or PL5/6, and allows you to set other options, e.g. setting landscape mode or choosing the paper tray. Under windows, this is what the printer drivers are for, under MAC OS X and Linux, this is done by the CUPS system. Unfortunately, the specifics depend highly on the system, the printer driver, the printer and the application that sends the file to the print system. thinking about os.system('notepad.exe /pt %%%s' % filename) So this is actually your safest bet, but will only work under windows obviously. Under Linux, you could try to use the 'a2ps' programm, but it is not installed everywhere. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Hi Danny, Wait, wait, before we continue here: did you finally get things working from your last question? Yes, i got things working. That was just a test for python-MySQldb. Now, i'm trying to get info from a python file, but i'm unable to get the variable values to insert in MySQL. For example: # Source field: save both url and value if entry[key].has_key(value): self.set_as_string(key + _name, entry[key].value) if entry[key].has_key(url): self.set_as_string(key + _link, entry[key].url) db = MySQLdb.connect(host=localhost, user=planet, passwd=secret, db=planet_geek) cursor = db.cursor() cursor.execute(INSERT INTO blog_posts (title) VALUES ('entry[key].url')) This isn't working. It inserts nothing in the database. How to retrieve the value of url ? Warm Regards, Mário Gamito ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing txt files in landscape from python
Hi All, do you have any idea, how I can send a txt file to the default printer in landscape view with python on windows. I wanted to set up just the char size and the orientation of the printout. thinking about os.system('notepad.exe /pt %%%s' % filename) Doesn't completely answer your question, but have a look at this: http://timgolden.me.uk/python/win32_how_do_i/print.html and perhaps consider a ReportLab solution. It's ridiculously difficult to set up the printing params construct under Windows (just search for DEVMODE) so might well be easier to use a PDF approach. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Wait, wait, before we continue here: did you finally get things working from your last question? Yes, i got things working. That was just a test for python-MySQldb. Hi Mario, Ok, good. Next time, make it more clear to the group that things worked out fine, to close the thread. Remember, we can't read your mind: make it explicit. Otherwise, people will continue to try to help you with something that you don't have problems with anymore. *grin* db = MySQLdb.connect(host=localhost, user=planet, passwd=secret, db=planet_geek) cursor = db.cursor() cursor.execute(INSERT INTO blog_posts (title) VALUES ('entry[key].url')) This isn't working. It inserts nothing in the database. There are two things you need to consider: 1. Autocommit mode is off. That is, by default, your database access is transactioned. That means that if you don't commit, the database rolls any changes right back out as soon as the connection closes. See: http://www.amk.ca/python/writing/DB-API.html 2. String literals are really string literals. That is, the code as written above will insert the literal string entry[key].url as a title. Use prepared statements. See: http://mail.python.org/pipermail/tutor/2003-April/022010.html for an example. There's also a third thing I'd suggest: abstract our the database-creation into a separate function. You're probably going to be opening connections in several places in your code: keep the connection-open logic in a single place so you can more easily change configurations. If it's possible, pull out the code that touches the database into a separate module layer. People on the list can talk about this more if you'd like. If you have more questions, please free to ask. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Hi, Danny Yoo wrote: Ok, good. Next time, make it more clear to the group that things worked out fine, to close the thread. Remember, we can't read your mind: make it explicit. Otherwise, people will continue to try to help you with something that you don't have problems with anymore. *grin* Ok. 1. Autocommit mode is off. That is, by default, your database access is transactioned. That means that if you don't commit, the database rolls any changes right back out as soon as the connection closes. See: http://www.amk.ca/python/writing/DB-API.html 2. String literals are really string literals. That is, the code as written above will insert the literal string entry[key].url as a title. Yes, i know, i'm a long time PHP programmer. I was just testing. Use prepared statements. See: http://mail.python.org/pipermail/tutor/2003-April/022010.html for an example. Yes, i know too. There's also a third thing I'd suggest: abstract our the database-creation into a separate function. You're probably going to be opening connections in several places in your code: keep the connection-open logic in a single place so you can more easily change configurations. If it's possible, pull out the code that touches the database into a separate module layer. People on the list can talk about this more if you'd like. Yes, that will be for later. For now, i'd just like to pull out the variables values, and i don't know how. For those who may be interested it's about file __init.py__ from planetplanet code. Warm Regards, Mário Gamito ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Mário Gamito wrote: Hi, I'm new to Python, so forgive me the lame question. I have this code (following my signature) and i'm trying to get the value of content and insert it into a MySQL database. I've been reading the docs in www.python.org, but i can't find a solution. Can someone help me, please ? I keep looking at this and your other posts and I just don't understand what you are trying to do. What do you mean by content? Is this code part of some class? It looks like it is from a class that subclasses dict. Anyway more context both of code and of what you are trying to do might be helpful. Kent Warm Regards, Mário Gamito -- def get_date(self, key): Get (or update) the date key. We check whether the date the entry claims to have been changed is since we last updated this feed and when we pulled the feed off the site. If it is then it's probably not bogus, and we'll sort accordingly. If it isn't then we bound it appropriately, this ensures that entries appear in posting sequence but don't overlap entries added in previous updates and don't creep into the next one. for other_key in (updated, modified, published, issued, created): if self.has_key(other_key): date = self.get_as_date(other_key) break else: date = None if date is not None: if date self._channel.updated: date = self._channel.updated #elif date self._channel.last_updated: #date = self._channel.updated elif self.has_key(key) and self.key_type(key) != self.NULL: return self.get_as_date(key) else: date = self._channel.updated self.set_as_date(key, date) return date def get_content(self, key): Return the key containing the content. for key in (content, tagline, summary): if self.has_key(key) and self.key_type(key) != self.NULL: return self.get_as_string(key) db = MySQLdb.connect(host=localhost, user=planet, passwd=secret, db=planet_geek) cursor = db.cursor() cursor.execute(INSERT INTO blog_posts (title) VALUES (key)) return ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The best way to implement a config file ???
Andreas Kostyrka skrev: * Magnus Wirström [EMAIL PROTECTED] [070201 14:38]: Hi everyone I'm learning to program python with wxpython gui. I have a application i made with several modules and i want to implement a configfile that applies to all those modules. Is there an easy way to do this ? Can anyone point me in the right direction ? Depending upon your tastes: ConfigFile = win.ini style config files. shlex = unix shell script style parsing. Andreas / think i want win.ini style, I'm designing a configuration module that is going to write this file. I have not heard of shlex so i can really not judge if it suits my needs. I do like the kind of config file that can be editable with a editor if nessary. Anyone have an good example how to use ConfigFile to write a config file that stores strings and boolean settings. Sorry for my english Thanks Magnus / ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Hi, I keep looking at this and your other posts and I just don't understand what you are trying to do. What do you mean by content? Is this code part of some class? It looks like it is from a class that subclasses dict. Anyway more context both of code and of what you are trying to do might be helpful. Ok, so here it is. Planetplanet relies basicly on two files. __init.py__ http://pastebin.com/872998 and cache.py: http://pastebin.com/873004 They both interact. Planetplanet is a blog agregater, aka a planet, like http://www.planetgeek.org What i'm trying to do is to fetch the values of the variables such as the blog's URLs, title of the articles, authors names, etc. and insert them into MySQL. What i can't figure is how to get the variables values from the code. I've been studying Python during the afternoon, i know Java, so they're not that different in the concept, but i can't see how in this particular case to retrieve the values of all that variables. Any help would be appreciated. Warm Regards, Mário Gamito ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The best way to implement a config file ???
Magnus Wirström wrote: / think i want win.ini style, I'm designing a configuration module that is going to write this file. I have not heard of shlex so i can really not judge if it suits my needs. I do like the kind of config file that can be editable with a editor if nessary. Anyone have an good example how to use ConfigFile to write a config file that stores strings and boolean settings. Sorry for my english http://effbot.org/librarybook/configparser.htm There are also a bunch of third-party modules that are supposed to be better than ConfigParser in some way: http://wiki.python.org/moin/ConfigParserShootout Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
For now, i'd just like to pull out the variables values, and i don't know how. For those who may be interested it's about file __init.py__ from planetplanet code. Hi Mario, Have you read either of the following yet? http://www.amk.ca/python/writing/DB-API.html http://mail.python.org/pipermail/tutor/2003-April/022010.html The small examples in those two should be fairly direct, so I'm a little confused about why you are still stuck. Please tell me if you've read those and yet don't understand what they are saying. Alternatively, if you've read them but still feel that they are relevant to you, say that, and we'll try to elaborate why they should be applicable. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Please tell me if you've read those and yet don't understand what they are saying. Alternatively, if you've read them but still feel that they are relevant to you, say that, and we'll try to elaborate why they should be applicable. Errr.. substitute relevant with irrelevant. Sorry. *grin* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
Hi, http://www.amk.ca/python/writing/DB-API.html http://mail.python.org/pipermail/tutor/2003-April/022010.html Yes, i've read the two docs. But my problem is more related with Python. If you read my previous post with the links to the code, my doubt is how to get the values for url, content, name, etc. Warm Regards, Mário Gamito ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 2.5 Quick Reference
http://rgruet.free.fr/PQR25/PQR2.5.html Is this reliable? (Looks good to me, but...) Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values
I have this code (following my signature) and i'm trying to get the value of content and insert it into a MySQL database. From where are you trying to get the value of content? Its not clear from your code. -- def get_date(self, key): ... The fact that this has a self param,eter suggets this is a method extracted from within a class? But without knowing anything about that class its hard to know whats going on. def get_content(self, key): Return the key containing the content. This doesn't return the key. It seems to return the content corresponding to the key or an empty string. Is that what you mean? for key in (content, tagline, summary): if self.has_key(key) and self.key_type(key) != self.NULL: return self.get_as_string(key) and if this works you will never do the insert. So you only do the insert if you don't find a key, and in that case you leave the value summary as the key But that doesn't matter because you always execute exactly the same INSERT statement - since its a hard coded string - overwriting the previous one each time. db = MySQLdb.connect(host=localhost, user=planet, passwd=secret, db=planet_geek) cursor = db.cursor() cursor.execute(INSERT INTO blog_posts (title) VALUES (key)) return I think I'm confused, and I'm still not sure what you problem is. We can't answer the question of why you don't get content because thats done by the get_as_string() method which you don't include in your listing. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values [Introduction to Planet RSS news aggregator]
Yes, i've read the two docs. But my problem is more related with Python. If you read my previous post with the links to the code, my doubt is how to get the values for url, content, name, etc. Hi Mario, After taking a much closer look at the code you mentioned here: http://pastebin.com/872998 it looks like you're supposed to have a NewsItem in hand. Ok, wait. I think I have a better idea of what you're trying to do. Let me try to dissuade you from doing what you're doing. *grin* There should be no reason for mucking into the definition of Planet's implementation in order to make it do what you want: you should be able to just treat Planet as a library, and use it to do what you want. You should almost certainly not touch the internals of get_content(), if I understand what you're trying to do: that's private to the implementation of Planet and a very bad approach toward code reuse. Rather than hack at NewsItem.get_content() to get it to insert into a database, it's probably a lot better to not modify Planet, but rather write new programs that use Planet. Respect the library and treat it as if it were a resource. If Scott James Remnant and Jeff Waugh take their code at: http://www.planetplanet.org/ and update it, or correct bugs, then you do not want to have to manually update your own code to patch things up the same way. Concretely, if we want to take a feed and print out all the titles, we should not be modify the get_title() method of these news items in a private copy of the Planet library. Rather, we can more simply use Planet as an external library: # import planet import ConfigParser config = ConfigParser.ConfigParser() p = planet.Planet(config) c = planet.Channel(p, http://hashcollision.blogspot.com/feeds/posts/default;) c.update() len(c.items()) 25 # Ok, there are 25 items here. Let's take a look at the titles: ## for item in c: ... print item.title ... latex in summation... heresy debugging test-case new year [text output truncated] ## Let's look at a particular item in the channel. firstItem = c.items()[0] firstItem.title 'how not to write xml' firstItem.id 'tag:blogger.com,1999:blog-18302393.post-116249176169366001' firstItem.link 'http://hashcollision.blogspot.com/2006/11/how-not-to-write-xml.html' firstItem.summary Traceback (most recent call last): File stdin, line 1, in ? File /usr/lib/python2.4/site-packages/planet/cache.py, line 279, in __getattr__ raise AttributeError, key AttributeError: summary Ok, so some things are not defined. That's to be expected. What things are defined for my news item? firstItem.keys() ['updated', 'subtitle', 'title', 'author', 'author_name', 'order', 'content', 'link', 'published', 'date', 'id_hash', 'id'] firstItem.author 'Danny Yoo' (It really is _my_ news item. *wink*) According to the documentation of a NewsItem, you can expect to see the following (usually): # id Channel-unique identifier for this item. id_hash Relatively short, printable cryptographic hash of id dateCorrected UTC-Normalised update time, for sorting. order Order in which items on the same date can be sorted. hidden Item should be hidden (True if exists). title One-line title (*). linkLink to the original format text (*). summary Short first-page summary (*). content Full HTML content. modifiedDate the item claims to have been modified (*). issued Date the item claims to have been issued (*). created Date the item claims to have been created (*). expired Date the item claims to expire (*). author Name of the author (*). publisher Name of the publisher (*). categoryCategory name (*). commentsLink to a page to enter comments (*). license Link to the licence for the content (*). source_name Name of the original source of this item (*). source_link Link to the original source of this item (*). # In able to see help documentation on planet, use the help() function at the prompt: # import planet help(planet) # The documentation on Planet is a bit focused for developers: the authors expect you to already know Python before touching Planet, so you might have some rough going at first. Does this help you get started? Please ask more questions if you
[Tutor] adding columns of numbers
I've been reading an old copy of Programming Python and started to work on one of its challenges. I have a text file called table.txt: 1 5 10 2 1.0 2 10 20 4 2.0 3 3 15 30 8 3 2 1 4 20 40 16 4.0 I want to add each column of numbers, so the end result would be a list like so: [10, 50, 100, 30 , 10.0, 5, 1] So far, I've been able to modify some code I found in the book: #!/usr/bin/python import string def summer(fileName): for lines_in_file in open(fileName, 'r').readlines(): cols_in_file = string.split(lines_in_file) #print cols_in_file numCols = len(cols_in_file) sums = [0] * numCols #print sums cols = string.split(lines_in_file) #print cols for i in range(numCols): sums[i] = sums[i] + eval(cols[i]) return sums if __name__ == '__main__': import sys print summer(sys.argv[1]) Unfortunately, the output is: [4, 20, 40, 16, 4.0] The code can read the file, but the code doesn't sum the numbers to produce a new list. Any hints? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding columns of numbers
On 02/02/07, Christopher Spears [EMAIL PROTECTED] wrote: I've been reading an old copy of Programming Python and started to work on one of its challenges. I have a text file called table.txt: 1 5 10 2 1.0 2 10 20 4 2.0 3 3 15 30 8 3 2 1 4 20 40 16 4.0 I want to add each column of numbers, so the end result would be a list like so: [10, 50, 100, 30 , 10.0, 5, 1] So far, I've been able to modify some code I found in the book: #!/usr/bin/python import string def summer(fileName): for lines_in_file in open(fileName, 'r').readlines(): cols_in_file = string.split(lines_in_file) #print cols_in_file numCols = len(cols_in_file) sums = [0] * numCols #print sums cols = string.split(lines_in_file) #print cols for i in range(numCols): sums[i] = sums[i] + eval(cols[i]) return sums if __name__ == '__main__': import sys print summer(sys.argv[1]) Unfortunately, the output is: [4, 20, 40, 16, 4.0] Compare the output with the input. Where do you think the output came from? Can you see how this is reflected in your code? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing txt files in landscape from python
On Thu, 1 Feb 2007, [ISO-8859-1] J?nos Juh?sz wrote: do you have any idea, how I can send a txt file to the default printer in landscape view with python on windows. I wanted to set up just the char size and the orientation of the printout. I've gotten a crush on wxPython, now that it's nicely documented in the wxPython in Action book. Take a look at http://wiki.wxpython.org/index.cgi/Printing for a discussion of printing. Here's an example on printing, copying the code from Code Sample - Easy Printing on that page. ### # this part copied from URL above: from wx.html import HtmlEasyPrinting class Printer(HtmlEasyPrinting): def __init__(self): HtmlEasyPrinting.__init__(self) def GetHtmlText(self,text): Simple conversion of text. Use a more powerful version html_text = text.replace('\n\n','P') html_text = text.replace('\n', 'BR') return html_text def Print(self, text, doc_name): self.SetHeader(doc_name) self.PrintText(self.GetHtmlText(text),doc_name) def PreviewText(self, text, doc_name): self.SetHeader(doc_name) HtmlEasyPrinting.PreviewText(self, self.GetHtmlText(text)) # now, using it: text_to_print = Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances. app = wx.PySimpleApp() p = Printer() p.Print(text_to_print, Amend 1) ### This works, and gives you (well, the user) the option of printing landscape. I'm not sure how to go about specifying a font. I suspect you'll have to go with the more heavyweight Code Sample - `(wx)Printout` Printing examplefor that. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding columns of numbers
Christopher Spears wrote: I've been reading an old copy of Programming Python and started to work on one of its challenges. I have a text file called table.txt: 1 5 10 2 1.0 2 10 20 4 2.0 3 3 15 30 8 3 2 1 4 20 40 16 4.0 I want to add each column of numbers, so the end result would be a list like so: [10, 50, 100, 30 , 10.0, 5, 1] So far, I've been able to modify some code I found in the book: #!/usr/bin/python import string def summer(fileName): for lines_in_file in open(fileName, 'r').readlines(): cols_in_file = string.split(lines_in_file) #print cols_in_file numCols = len(cols_in_file) sums = [0] * numCols This creates a new sums list for each line of the file. You need to initialize sums outside the loop. It's a little tricky to figure out how long sums really needs to be, since the lines are not all the same length. #print sums cols = string.split(lines_in_file) #print cols for i in range(numCols): sums[i] = sums[i] + eval(cols[i]) Instead of eval(cols[i]) it would be better to use float(cols[i]). It's usually a good idea to avoid eval(). Extra credit: Write summer() as a one-liner. :-) (I know, I shouldn't be encouraging this. But it is a good exercise even if you wouldn't use it in production code. It would be pretty easy if the lines were all the same length...) Kent return sums if __name__ == '__main__': import sys print summer(sys.argv[1]) Unfortunately, the output is: [4, 20, 40, 16, 4.0] The code can read the file, but the code doesn't sum the numbers to produce a new list. Any hints? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Get variable values [Introduction to Planet RSS news aggregator] (fwd)
-- Forwarded message -- Date: Fri, 02 Feb 2007 00:52:49 + From: [ISO-8859-1] Mário Gamito [EMAIL PROTECTED] To: Danny Yoo [EMAIL PROTECTED] Subject: Re: [Tutor] Get variable values [Introduction to Planet RSS news aggregator] Hi Danny, Thank you for your insightful info. Now i'm getting somewhere. I've made this mysql.py teste script: import planet import ConfigParser import MySQLdb db = MySQLdb.connect(host=localhost, user=planet, passwd=secret, db=planet_geek) cursor = db.cursor() config = ConfigParser.ConfigParser() p = planet.Planet(config) c = planet.Channel(p, http://blog.gamito.org/rss.php?blogId=1profile=rss20;) c.update() print len(c.items()) for item in c: print item.title cursor.execute(INSERT INTO blog_posts (title) VALUES (item.title)) print item.content it prints the info from the feeds to the screen, but get an error from MySQL: File sql.py, line 16, in ? cursor.execute(INSERT INTO blog_posts (title) VALUES (item.title)) File /usr/local/lib/python2.4/site-packages/MySQLdb/cursors.py, line 163, in execute self.errorhandler(self, exc, value) File /usr/local/lib/python2.4/site-packages/MySQLdb/connections.py, line 35, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1109, Unknown table 'item' in field list) which means that the variable item.title is not recognized as a variable. Any ideas ? Warm Regards, Mário Gamito ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The best way to implement a config file ???
I've used two options in the past. The one is as Kent suggested, by using a module that is imported by all others and share the values. This doesn't allow for users to change the values, without changing the code. The second is to make use of a other file format. I used xml with all the info in and extracted the values from there. It can be done with any type of file, as long as you will know how to get the correct attributes for each value. HTH Johan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: 01 February 2007 04:18 PM To: Andreas Kostyrka Cc: tutor@python.org Subject: Re: [Tutor] The best way to implement a config file ??? Andreas Kostyrka wrote: * Magnus Wirström [EMAIL PROTECTED] [070201 14:38]: Hi everyone I'm learning to program python with wxpython gui. I have a application i made with several modules and i want to implement a configfile that applies to all those modules. Is there an easy way to do this ? Can anyone point me in the right direction ? Depending upon your tastes: ConfigFile = win.ini style config files. shlex = unix shell script style parsing. You can also use a python module as a shared config; just define whatever values you want in Python code and import the module where you need it. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.432 / Virus Database: 268.17.18/662 - Release Date: 2007/01/31 03:16 PM -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.432 / Virus Database: 268.17.18/662 - Release Date: 2007/01/31 03:16 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor