Re: [Tutor] this module
On Friday 07 August 2009 21:31, Mark Young wrote: > Hi, I was reading a tutorial, and it mentioned the "import this" easter > egg. I was curious, and looked up the contents of the module, and dscovered > that it had attributes c, d, i, and s. I was wondering if anyone had any > clue what these attributes were supposed to mean. I think (this.s) is the > zen of python in some foreign language (dutch maybe?), but I have no clue > what the significance of the others are. Just wondering if anyone knew, (or > if they mean anything at all). > > Also, I was wondering, when a user first imports this, how does the module > print the zen of python? I can't figure out how it does it. "Use the source, Luke!" You can find this.py, along with the rest of the standard library (most of which is written in Python, as opposed to being compiled C code) wherever your Python was installed. For instance, /usr/lib/pythonx.y on unixy machines (including macs), and C:\Pythonxy\Lib on that other operating system. Cheers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] this module
"Mark Young" wrote I was curious, and looked up the contents of the module, and dscovered that it had attributes c, d, i, and s. I was wondering if anyone had any clue what these attributes were supposed to mean. Mostly they are just variables used to construct the message. The values are the fibnal values of loops etc. I think (this.s) is the zen of python in some foreign language (dutch maybe?), No, its not Dutch, its a caesar encrypted version of the zen and the variable d holds the translation table. significance of the others are. Just wondering if anyone knew, (or if they mean anything at all). Just look at the source of the module in this.py, its in the Libs folder Also, I was wondering, when a user first imports this, how does the module print the zen of python? I can't figure out how it does it. It builds the table then it applies it to the encrypted string. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] this module
Something else to note: you can find any module's location by looking it up in the dictionary sys.modules. For instance: Python 2.4.4 (#2, Oct 22 2008, 19:52:44) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> import sys >>> sys.modules['this'] Cheers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding MySQLdb.cursors.DictCursor.execute()
On Fri, Aug 7, 2009 at 10:18 PM, Tim Johnson wrote: > Hello: > I am currently using python 2.5 and do a lot of database programming > with MySQLdb. > > I need to tighten up control over queries since I am concerned about > malicious injections. If you use the two argument form of cursor.execute - passing the parameter values in a sequence, rather than substituting them yourself - then you have to worry about injection attacks. The DB-API module should take care of any required escaping. > It would seem to me that overriding the execute() methods for both > objects would entail the least amount of code maintenance and > modification. I've used python for a long time, but not done much > with object inheritance. > The following code: > class mysql_row_cursor(MySQLdb.cursors.DictCursor): > def __init__(self): > pass > # results in the following error message: > class mysql_row_cursor(MySQLdb.cursors.DictCursor): > AttributeError: 'module' object has no attribute 'cursors' > # say what? MySQLdb has been imported... You have to explicitly import subpackages. Try import MySQLdb.cursors Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] this module
Mark Young wrote: Hi, I was reading a tutorial, and it mentioned the "import this" easter egg. I was curious, and looked up the contents of the module, and dscovered that it had attributes c, d, i, and s. I was wondering if anyone had any clue what these attributes were supposed to mean. I think (this.s) is the zen of python in some foreign language (dutch maybe?), but I have no clue what the significance of the others are. Just wondering if anyone knew, (or if they mean anything at all). Also, I was wondering, when a user first imports this, how does the module print the zen of python? I can't figure out how it does it. Each module runs when you import it. So if there are print statements that are not either inside definitions, classes or conditionals, you'll see their output. As for this.py, take a look at it. The "dutch" is actually an encoding currently popularly referred to as ROT13, a substitution cipher where each letter is replaced by the one halfway (13 characters) around the alphabet. If you look at d, you'll see it's a dictionary with the mapping between 'a' and 'o', 'b' and 'p', and so on. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding MySQLdb.cursors.DictCursor.execute()
* Kent Johnson [090808 05:06]: > On Fri, Aug 7, 2009 at 10:18 PM, Tim Johnson wrote: > > If you use the two argument form of cursor.execute - passing the > parameter values in a sequence, rather than substituting them yourself > - then you have to worry about injection attacks. The DB-API module > should take care of any required escaping. Oh! Good to hear. Never use the two argument form. > > You have to explicitly import subpackages. Try > import MySQLdb.cursors Understood. And now probably now not necessary. thanks -- Tim t...@johnsons-web.com http://www.akwebsoft.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] building database with sqlite3 and matplotlib
On Fri, Aug 7, 2009 at 5:35 PM, David Kim wrote: > I've been learning python in a vacuum for the past few months and I > was wondering whether anyone would be willing to take a look at some > code? I've been messing around with sqlite and matplotlib, but I > couldn't get all the string substitution (using ?s). > The code can be found at > http://notestoself.posterous.com/use-python-and-sqlite3-to-build-a-database-co > A short summary of what I did is at > http://notestoself.posterous.com/use-python-and-sqlite3-to-build-a-database > > (Or should I have pasted the code in this message?) No, it's a bit long for that. pastebin.com is another option that doesn't require others to download the code. > I've been trying to learn from books, but some critique would be very > appreciated. Mostly it looks pretty good. sql = "select asset_id from assets where ticker='%s'" % ticker #change this! c.execute(sql) Try (I think, I don't have DB-API docs at the moment...) sql = "select asset_id from assets where ticker='?'" c.execute(sql, [ticker]) date_raw = datetime.datetime.fromordinal(int(quote[0])) year, month, day = date_raw.year, date_raw.month, date_raw.day date_string = str(year)+'-'+str(month)+'-'+str(day) See datetime.strptime() for simpler date formatting marks = len(data_dict['headers'])*'?,' marks_string = marks[:-1] or marks = ','.join(['?'] * len(data_dict['headers'])) if (__name__ == '__main__'): main() You don't seem to actually have a main(). Are you running this by importing it? I would make a separate function to create the database and call that from build_database(). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to Split a String
What I am trying to do is find the mail folder for our lotus notes files. I get it by doing the following: lotusnotesmaildir = glob.glob('C:\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data/'+'*mail*/') That returns: ['C:\\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data\\mail02\\'] What I am trying to do is split the mail folder, in this case it would be mail02, but not all users would have that same mail folder. How can I split it so I get mail02. Any help in greatly appreciated. Thanx, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to Split a String
"Paras K." wrote lotusnotesmaildir = glob.glob('C:\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data/'+'*mail*/') You don't need the plus. lotusnotesmaildir = glob.glob('C:\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data/'*mail*/') That returns: ['C:\\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data\\mail02\\'] Are you sure it isn't ['C:\\Documents and Settings\\pkinariwala\\Local Settings\\Application Data/lotus\\notes\\data\\mail02\\'] What I am trying to do is split the mail folder, in this case it would be mail02, but not all users would have that same mail folder. If so then you can use var[0].split('\\')[-1] to get the last item. You might also find the path manipulation and basename functions useful for mamnipulating paths. Especially if you need to do it on multiple different computers/OS. These are in the os/os.path module family. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to Split a String
On Sat, Aug 8, 2009 at 2:33 PM, Paras K. wrote: > What I am trying to do is find the mail folder for our lotus notes files. > > I get it by doing the following: > > lotusnotesmaildir = glob.glob('C:\Documents and Settings/pkinariwala/Local > Settings/Application Data/lotus/notes/data/'+'*mail*/') > > That returns: ['C:\\Documents and Settings/pkinariwala/Local > Settings/Application Data/lotus/notes/data\\mail02\\'] > > What I am trying to do is split the mail folder, in this case it would be > mail02, but not all users would have that same mail folder. See os.path.basename() Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] building database with sqlite3 and matplotlib
Thanks so much for the comments! I appreciate the look. It's hard to know what the best practices are (or if they even exist). On Sat, Aug 8, 2009 at 2:28 PM, Kent Johnson wrote: > > You don't seem to actually have a main(). Are you running this by importing > it? > > I would make a separate function to create the database and call that > from build_database(). > I was running it as a standalone to see if it worked but forgot to move the code to main(). I cut but never pasted! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] droplet like behaviour in Python
Hi I am porting some of my code over from Applescript to Python. There is one thing I am not really sure how to do. I want to be able to drop a file onto a python script (or app I guess) and have the python script use the path to the file that was dropped on it as sys.argv[1] In applescript it looks like this: on open theNukeScript tell application "Terminal" activate tell application "Terminal" do script "python /Volumes/rugged/programming/python/bin/nkDoesFileExist.py " & theNukeScript --etc Where theNukeScript is the path to of the file that was dropped. I am hoping to make the code cross platform. Any advice would be greatly appreciated. Pete ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] droplet like behaviour in Python
"pedro" wrote is one thing I am not really sure how to do. I want to be able to drop a file onto a python script (or app I guess) and have the python script use the path to the file that was dropped on it as sys.argv[1] I may be wrong but I thought that, provided you had the path and shebang etc set up properly that it just happened. Have you tried a simpe script that just prints argv say? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] this module
Thanks everybody, I didn't know modules ran code when you imported them, I just thought they defined the functions, etc. in them. Thanks for the info. I'm going to go look at the module's code now that I know where it's at. Mark Young ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor