[issue15125] argparse: positional arguments containing - in name not handled well
Keith Hughitt added the comment: Although it would be nice if the behavior were normalized between positional and optional args, it seems like doc patch would be the most straight-forward at this point. The down-side is that I suspect many people will assume the behavior for optional args, have it fail, and then consults the docs to see what happened. I would suggest making the note pretty obvious or early on in the docs for argparse. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15125 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15125] argparse: positional arguments containing - in name not handled well
Keith Hughitt added the comment: Any progress on this issue? Still persists in Python 3.4.1. -- nosy: +khughitt ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15125 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Merge two directories together
Suppose you have two file-trees with common sub-directories but different files that you want to merge together, e.g. /test/ /test/a/ /test/a/file1 /test2/ /test2/a/ /test2/a/file2 You can easily merge the directories in Linux using the cp command: cp -r test/* test2/ While Python provides some helpful methods for moving files and directories around (shutil.move, shutil.copytree, etc), none of them seem to be able to merge two directories. I've looked around some on Google for an alternative to calling cp directly, but so far no luck. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Hi John, Thanks for the suggestions: I have finally been able to get it working :) In anyone else runs into the same problem, here is some example code that works in Python 2.4+: = Begin Example == #!/usr/bin/env python #-*- coding:utf-8 -*- import sys def main(): import MySQLdb, getpass admin = raw_input(Database admin: ) pw = getpass.getpass(Password: ) db = MySQLdb.connect(use_unicode=True, charset = utf8, user=admin, passwd=pw) cursor = db.cursor() try: cursor.execute(DROP DATABASE IF EXISTS unicode_test;) cursor.execute(CREATE DATABASE unicode_test DEFAULT CHARACTER SET utf8;) except: print cursor.execute(''' CREATE TABLE `unicode_test`.`test` ( `id` SMALLINT unsigned NOT NULL AUTO_INCREMENT, `name`VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), INDEX (`id`) ) DEFAULT CHARSET=utf8;''') cursor.execute(INSERT INTO `unicode_test`.`test` VALUES (NULL, '%s'); % uÅngström) # Test 1 print Just printing: %s % 'Ångström' # Test 2 cursor.execute(SELECT name FROM unicode_test.test;) print From database: %s % cursor.fetchone()[0] # Test 3 (Manual) print 'To verify manually: mysql -u %s -p -e SELECT name FROM unicode_test.test' % admin if __name__ == '__main__': sys.exit(main()) = End Example Thanks all for taking the time to help! Best, Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Hello, Thanks for the suggestions and information Diez! On Nov 18, 9:38 am, Diez B. Roggisch de...@nospam.web.de wrote: You are aware that the coding-declaration only affects unicode-literals (the ones like ui'm unicode)? So the above insert-statement is *not* unicode, it's a byte-string in whatever encoding your editor happens to save the file in. Thanks. I didn't know that, but that is helpful. And that's point two: make sure your editor reads and writes the file in the same encoding you specified in the comment in the beginning. That is taken care of: the files are opened/saved as UTF-8. Makes sense if the execute tries to encode to unicode first - as you didn't give it a unicode-object. In Python 2.6 where I originally developed the code, it wasn't necessary to explicitly specify the type of some text: it was basically handled for you. It does make sense though. So far I've tried a number of different things, including: 1. Using Unicode strings (e.g. u\u212B) 2. Manually specifying the encoding using sys.setdefaultencoding ('utf-8') 3. Manually enabling Unicode support in MySQLdb (use_unicode=False, charset = utf8) You *disabled* unicode here! Unicode is NOT utf-8!!! Oops. It was enabled when I ran it, I just copied the above text from somewhere else and forgot to change it. I am aware that Unicode does not equal utf-8, but utf-8 is a Unicode encoding, right? http://www.joelonsoftware.com/articles/Unicode.html Thanks! Try the above, and better yet provide self-contained examples that show the behavior. Diez Still no luck. I also tried using double-quotes instead of single- quotes around the relevant strings (as suggested over email by ThreaderSlash), however, that did not work for me. Here is a small example of what I'm trying to do. Notice that if you run it in Python 2.5-2.6, everything works fine. It is only in Python 2.4 that the below example doesn't work. = Begin Example == #!/usr/bin/env python #-*- coding:utf-8 -*- import sys def main(): import MySQLdb, getpass admin = raw_input(Database admin: ) pw = getpass.getpass(Password: ) db = MySQLdb.connect(user=admin, passwd=pw) cursor = db.cursor() cursor.execute(CREATE DATABASE IF NOT EXISTS unicode_test;) cursor.execute(''' CREATE TABLE `unicode_test`.`test` ( `id` SMALLINT unsigned NOT NULL AUTO_INCREMENT, `name`VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), INDEX (`id`) ) DEFAULT CHARSET=utf8;''') cursor.execute(''' INSERT INTO `unicode_test`.`test` VALUES (NULL, 'Ångström'); ''') # Test 1 print Just printing: %s % 'Ångström' # Test 2 cursor.execute(SELECT name FROM unicode_test.test;) print From database: %s % cursor.fetchone()[0].decode('utf-8') # Test 3 (Manual) print 'To verify manually: mysql -u %s -p -e SELECT name FROM unicode_test.test' % admin if __name__ == '__main__': sys.exit(main()) = End Example Any suggestions? Thanks! Keith -- http://mail.python.org/mailman/listinfo/python-list
Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Hi all, I ran into a problem recently when trying to add support for earlier versions of Python (2.4 and 2.5) to some database related code which uses MySQLdb, and was wondering if anyone has any suggestions. With later versions of Python (2.6), inserting Unicode is very simple, e.g.: # -*- coding: utf-8 -*- ... cursor.execute('''INSERT INTO `table` VALUES (0, 'Ångström'),...''') When the same code is run on earlier versions, however, the results is either garbled text (e.g. Ã or ? instead of Å in Python 2.5), or an exception being thrown (Python 2.4): UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 60: ordinal not in range(128) So far I've tried a number of different things, including: 1. Using Unicode strings (e.g. u\u212B) 2. Manually specifying the encoding using sys.setdefaultencoding ('utf-8') 3. Manually enabling Unicode support in MySQLdb (use_unicode=False, charset = utf8) ...but no combination of any of the above resulted in proper database content. To be certain that the issue was related to Python/MySQLdb and not MySQL itself, I manually inserted the text and it worked just fine. Furthermore, when working in a Python console, both print Å and print u\u212B display the correct output. Any ideas? The versions of the MySQLdb adapter tested were 1.2.1 (Python 2.4), and 1.2.2-10 (Python 2.5). Thanks! Keith -- http://mail.python.org/mailman/listinfo/python-list
Corrupted images after attempting to store PNG images as BLOBs in MySQL?
Hi all, I've run into a strange error while trying to store some PNG images in a MySQL database using MySQLdb. When I try to insert smaller images ( 64kb or so) everything seems to work fine. When I start trying to insert larger images (~150kb), however, the images get corrupted along the way. The result is that only part of the image, e.g. the top 30% of the image, is stored in the database, and the rest is simply transparent. Furthermore, if I attempt to view the image in mysql-query-browser, it does not display and simply states Cannot display as image data, which seems to further suggest the idea that the data is being corrupted somewhere along the way. To store the image I'm using: blob = open(img, 'rb').read() sql = INSERT INTO table VALUES('%s') % (MySQLdb.escape_string(blob)) Anyone have any ideas? Thanks, Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Corrupted images after attempting to store PNG images as BLOBs in MySQL?
Thanks Fredrik. That certainly explains things :) I also appreciate the suggestion on coding guidelines: I'm still becoming familiar with python. Originally we were not using the database to store images, but we started testing out storing images there as well as meta-data. We may end up switching back though for efficiency. For now though I think I will try mediumblob to at least see if I can fix the problem as things are. Thanks and take care! Keith On Aug 13, 10:46 am, Fredrik Lundh [EMAIL PROTECTED] wrote: Fredrik Lundh wrote: Silently truncating or otherwise mangling columns is a standard MySQL feature. What does the table definition look like? Oh, you did write BLOB in the subject. BLOB columns hold 64k (minus 2 bytes for housekeeping), and excess data is discarded, by default: If strict SQL mode is not enabled and you assign a value to a BLOB or TEXT column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. http://dev.mysql.com/doc/refman/5.0/en/blob.html Are you sure you *need* to use MySQL ;-) /F -- http://mail.python.org/mailman/listinfo/python-list
Angle brackets in command-line arguments?
Hi all, I am using someone else's script which expects input in the form of: ./script.py arg1 arg2 I was wondering if the angle-brackets here have a special meaning? It seems like they specify an input and output stream to use in place of the console. I could not find anything in the python manual or Python in a Nut-shell though. Anyone know? Thanks, Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Angle brackets in command-line arguments?
On Jul 16, 11:16 am, Gary Herron [EMAIL PROTECTED] wrote: Keith Hughitt wrote: Hi all, I am using someone else's script which expects input in the form of: ./script.py arg1 arg2 I was wondering if the angle-brackets here have a special meaning? It seems like they specify an input and output stream to use in place of the console. I could not find anything in the python manual or Python in a Nut-shell though. Anyone know? Thanks, Keith -- http://mail.python.org/mailman/listinfo/python-list In most Unix/Linux and related OS shells, the angled brackets *do* specify input and output streams as you surmise. However, they are *not* seen by the script as command line arguments. (And they are *not* brackets, and do not need to be matched. ) For any command, cmd file redirects the contents of file to cmd's standard input, which in Python is accessed by reading from sys.stdin (use input or raw_input or sys.stdin.read...) Also for any command, cmd file redirects the output of cmd to the named file. In Python this can be accessed using print, sys.stdout.write, ... Anything written to sys.stderr will not be caught by the redirection, ans so will probably end up on the screen instead of in file. Also various shells will provide similar functionality using a variety of similar syntaxes: , , , and |, and so on. Gary Herron Thanks all for the quick response. I should have known it was shell- related. I haven't ever had to use this kind of redirection before, mostly just the single-bracket version to feed the contents of a file into some command. The reason it was causing me concern in the first place was that I was trying to execute a python script from Apache Ant, and it was failing. It turned out that when I escaped the angle brackets in Ant, they were there-after treated as normal command-line arguments, so when the script was then executed, it just had some additional values in sys.argv. I ended up getting around the issue by creating a small bash-script which simply wraps input in angle brackets and then executes the python script itself. That way I didn't have to escape anything in in Ant, and things worked well. Thanks everyone for taking the time to explain things to me. I really appreciate the help :) Take care, Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting from local - UTC
Thanks Gabriel! That helps clear things up for me. The above method works very well. I only have one remaining question: How can I pass a datetime object to MySQL?' So far, what I've been doing is building the query as a string, for example: query = INSERT INTO image VALUES(%d, %d, %s, '%s') % (id, meas, date, 'jpg') cursor.execute(query) This works fine for regular datetime objects, which are passed as strings similar to: 2003-10-01 00:00:00. When incorporating a timezone, however, the resulting string is of the form 2003-10-01 00:00:00+00:00. Unfortunately, MySQL does not recognize the offset. I know you said you don't use MySQL, but how would you do something execute a similar query on the database you normally interface with? Thanks, Keith On Jul 15, 12:04 am, Gabriel Genellina [EMAIL PROTECTED] wrote: En Mon, 14 Jul 2008 12:06:30 -0300,KeithHughitt [EMAIL PROTECTED] escribió: On Jul 12, 12:52 am, Gabriel Genellina [EMAIL PROTECTED] wrote: En Fri, 11 Jul 2008 15:42:37 -0300,KeithHughitt [EMAIL PROTECTED] escribió: I am having a little trouble figuring out how to convert a python datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a UTC date so that when I send it to MySQL (which treats all dates at local dates by default), it will already have incorporated the proper UTC offset. I've tried looking through the docshttp://python.active-venture.com/lib/datetime-datetime.html), but have not had any luck. You have to use a timezone aware datetime object. If all you want is to store an UTC date, the tzinfo demo classes that you can find in the Python docs at http://docs.python.org/lib/datetime-tzinfo.html may be enough. Thanks for advice Gabriel. I downloaded the tzinfo demo class, saved it as UTC.py and imported it. I'm still not exactly sure how to use it though. It looks like the file already creates an instance of the UTC tzinfo class (line 20: utc = UTC()), however, when I try to test it out in the interpreter, it cannot be found. I'm new to python, and there is probably something obvious I'm missing, but do you have any ideas? The import statement in Python doesn't behave the same way as similar statements in other languages - and it may be confusing you. I'll try to explain it using this example. You have: - a *file* UTC.py, containing the source code for the *module* UTC. It contains: - a *class* definition (UTC) and - an *instance* of that class, utc. --- begin UTC.py ---If you pass a timezone aware datetime object as a SQL parameter class UTC(tzinfo): ... utc = UTC() ... --- end UTC.py --- Here is what I'm attempting: output begin = Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type help, copyright, credits or license for more information. import datetime, UTC Here you have imported the *module* UTC. That is, the name UTC now refers to a newly created module just loaded from the UTC.py file. t = datetime.datetime(2008, 7, 14, 00, 00, 00, UTC()) Traceback (most recent call last): File stdin, line 1, in module TypeError: 'module' object is not callable The error comes from UTC(): UTC is a module, UTC() is attempting to call it, and since modules are not callable objects, we get a TypeError. utc Traceback (most recent call last): File stdin, line 1, in module NameError: name 'utc' is not defined The *only* name we have imported so far is UTC - the module. Lowercase utc isn't defined. utc = UTC() Traceback (most recent call last): File stdin, line 1, in module TypeError: 'module' object is not callable Same as above... Ok, how to solve it? We know that UTC refers to the *module* with the same name. To get the *class* inside that module, use UTC.UTC - try again in the interpreter. To create a new instance of that class, you can use UTC.UTC(). To obtain the instance already created in the UTC module, use UTC.utc **OR** Import those names explicitely: py from UTC import UTC In this case the name UTC refers to the *class* inside the module. In this particular example it may be confusing - both have the same name. Another example from the standard library: the poplib module contains a POP3 class, so after executing this line: py from poplib import POP3 the name POP3 refers to that class. The poplib module itself isn't directly available. Back to the UTC module, you could use: py from UTC import utc and now utc refers to the *instance* already created inside the module. This last form may be the most convenient in your case: py import datetime py from UTC import utc py print datetime.datetime(2008, 7, 14, 20, 30, 0, 0, utc) 2008-07-14 20:30:00+00:00 -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Converting from local - UTC
Hi, I am having a little trouble figuring out how to convert a python datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a UTC date so that when I send it to MySQL (which treats all dates at local dates by default), it will already have incorporated the proper UTC offset. I've tried looking through the docs http://python.active-venture.com/lib/datetime-datetime.html), but have not had any luck. Does anyone have any suggestions? Any help would be greatly appreciated. Thanks, Keith -- http://mail.python.org/mailman/listinfo/python-list
Allow tab completion when inputing filepath?
Hi all, I've been looking around on the web for a way to do this, but so far have not come across anything for this particular application. I have found some ways to enable tab completion for program-related commands, but not for system filepaths. This would be nice to have when prompting the user to enter a file/directory location. Any suggestions? Thanks, Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Allow tab completion when inputing filepath?
On Jul 9, 10:18 am, Tim Golden [EMAIL PROTECTED] wrote: Keith Hughitt wrote: I've been looking around on the web for a way to do this, but so far have not come across anything for this particular application. I have found some ways to enable tab completion for program-related commands, but not for system filepaths. This would be nice to have when prompting the user to enter a file/directory location. What platform are you on? And what kind of display? (Console / GUI / wxPython / Qt / Web...) TJG Hi TJG, Currently Unix/Console. Although I don't have any plans at the moment to add a GUI, it would be great if a cross-platform solution existed. Keith -- http://mail.python.org/mailman/listinfo/python-list