Re: Help with python-list archives
On Thu, Jan 5, 2012 at 9:08 PM, random joe wrote: > PS: I wonder why no one has added a note to the Python-list archives > to advise people about the bug? Probably nobody has noticed it until now. It seems to be a quirk of the archive files that they are double-gzipped, and most people probably just use gunzip or gzcat (or a higher-level tool that invokes those) to extract them, which seems to be smart enough to handle it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Thu, Jan 5, 2012 at 8:00 PM, MRAB wrote: > import gzip > > in_file = gzip.open(r"C:\2012-January.txt.gz") > out_file = open(r"C:\2012-January.txt.tmp", "wb") > out_file.write(in_file.read()) > in_file.close() > out_file.close() > > in_file = gzip.open(r"C:\2012-January.txt.tmp") > out_file = open(r"C:\2012-January.txt", "wb") > out_file.write(in_file.read()) > in_file.close() > out_file.close() One could also avoid creating the intermediate file by using a StringIO to keep it in memory instead: import gzip from cStringIO import StringIO in_file = gzip.open('2012-January.txt.gz') tmp_file = StringIO(in_file.read()) in_file.close() in_file = gzip.GzipFile(fileobj=tmp_file) out_file = open('2012-January.txt', 'wb') out_file.write(in_file.read()) in_file.close() out_file.close() Sadly, GzipFile won't read directly from another GzipFile instance (ValueError: Seek from end not supported), so some sort of intermediate is necessary. -- http://mail.python.org/mailman/listinfo/python-list
Re: Spamming PyPI with stupid packages
On Jan 1, 5:24 pm, Alexander Kapps wrote: > Uh oh, should I really send this? ... Yes. Yes, I should! Sorry, I > cannot resists. > > >>> allow everyone to do "import girlfriend" > > I'm betting on a joke, like antigravity only significantly less > > funny and more sexist. > > Absolutely not funny. I hope that someday people will understand > that sexism is just another form of racism. I want everyone to realize that just because you are offended by someones statements, actions, or behavior, DOES NOT mean those statements, actions or behaviors are wrong. Example: I have a right to buy the flag of your country and burn it. To you the flag is a symbol to be worshiped. SO WHAT! To me your flag is a worthless piece of cloth! And since i bought and own this worthless piece of cloth, i can burn it, stomp on it, poke it with a stick, and even wipe my kulo with it! Sure, doing all these things may make me look like a complete idiot, but it is my god given right to act like an idiot! Now, you could get all upset and call in the gestapo, or you could just have a giggle and go about your day. Choice is yours. Anecdote: A while back i read a story about some kids gesturing their middle fingers to on-duty policemen. As you might have guessed the kids were arrested, handled roughly, and charged with disorderly conduct. Soon after the kids sued the police department and received a nice amount of money for their false arrest. Just like the police, all the people getting upset over this light- hearted joke are over-reacting and trying to abuse power. They believe they have the power to stop other people from offending them. No, the police DO NOT have that power, and YOU don't not have that power. However, you DO have the power to ignore. And that is what the gestapo *should* have done. Now, if someone were making jokes about handicapped or mentally challenged people (or anyone who does not have the capacity to defend themselves) THEN you would be justified to make an argument. This case warrants no such justification. Heck, there is not even a specific victim to protect -- well, except for the poor guy who has to import everything. Nobody cares about him :'-( -- http://mail.python.org/mailman/listinfo/python-list
Re: Spamming PyPI with stupid packages
On Jan 3, 8:37 pm, Chris Angelico wrote: > On Wed, Jan 4, 2012 at 12:54 PM, Ben Finney > wrote: > > It objectifies women. If you can't see how that's harmful to women, I > > haven't the stamina to educate you. > > And "import pickle" objectifies pickles. It's deplorable how few > gherkins become programmers, and I think it's because of these > immature jokes about pickles that have become entrenched in the Python > community. No, no, "import pickle" is not about objectifying pickles, it's a sub- conscience Freudian phallic symbol! import pickle pickle.LONG4 # Only the blind would miss that one! pickle.load pickle.dump pickle.EMPTY_DICT # Using GvR's pronunciation But don't worry, *some group* is sure to be offended. -- http://mail.python.org/mailman/listinfo/python-list
Re: Spamming PyPI with stupid packages
On Jan 3, 8:42 pm, Ben Finney wrote: > Steven D'Aprano writes: > > On Wed, 04 Jan 2012 12:54:09 +1100, Ben Finney wrote: > > > It objectifies women. > > > So you claim. > > I'm sure you have a hundred ready rationalisations for why a joke that > has “girlfriend” as a fungible object, together with “car” and “house” > as things to mechanically import into one's life, is somehow not > objectifying women. > > But, while those rationalisations may satisfy you, I'm not interested in > hearing them. If you don't see that the joke objectifies women, that > tells me quite a lot about how blinkered you are to the problem. > > For what it's worth, I'm very familiar with such rationalisations, > having employed them many times myself. Fortunately there are women who > will speak up against it and encourage men to do the same > http://tldp.org/HOWTO/Encourage-Women-Linux-HOWTO/x168.html>. > > > Since you can't or won't persuade me (and anyone else reading) that > > this sort of joke is harmful, does that mean that you will stop > > claiming that it is harmful? > > I have no idea what it would take to persuade you in particular. I do > know that the combined privileges of being white, male, not-poor, and > English-fluent (and many more privileges, I'm sure) grant both of us the > luxury of barely even perceiving the harm done by a pervasive atmosphere > of even low-level prejudice against any given group of people. > > Women (to return to the people in question), on the other hand, do not > have that luxury. In this community they have no option but to be aware > of the privileges we males have here. > > We have very effective cognitive blinders, merely because we have never > needed to know what it's like being a woman in this prevalently-male > field. The onus is on us to try hard to see, despite those blinders and > easy rationalisations, that there is a lot we allow from our fellows > which is perpetuating a hostile environment. > > I'm not making a fuss about one sexist joke, which has already been > retracted by its author. I'm making a fuss about allowing and, worse, > defending such jokes as a tacitly-accepted norm of our community. And I > hope those of us who prefer to think of ourselves as not-sexist will act > to clean up our house more. Your points are all valid, except, not valid in this particular argument. import girlfriend is not sexist, no more than import car is selfish, and import house is greedy. I think the joke is on the poor guy who needs this module. He is a poor soul with nothing, who needs to import everything. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Fri, Jan 6, 2012 at 3:08 PM, random joe wrote: > Nevermind. Notepad was the problem. After using a real editor the text > is displayed correctly! Thanks for help everyone! ... or that could be your problem :) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Fri, Jan 6, 2012 at 3:01 PM, random joe wrote: > THis works however there is one more tiny hiccup. The text has lost > all significant indention and newlines. Was this intended or is this a > result of another bug? I'm seeing it as plain text, with proper newlines. There's no indentation as it just runs straight through, top-to-bottom; but you should be able to see line breaks. Check your mail reader in case something's getting botched there. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Jan 5, 10:01 pm, random joe wrote: > On Jan 5, 9:00 pm, MRAB wrote: > > import gzip > > > in_file = gzip.open(r"C:\2012-January.txt.gz") > > out_file = open(r"C:\2012-January.txt.tmp", "wb") > > out_file.write(in_file.read()) > > in_file.close() > > out_file.close() > > > in_file = gzip.open(r"C:\2012-January.txt.tmp") > > out_file = open(r"C:\2012-January.txt", "wb") > > out_file.write(in_file.read()) > > in_file.close() > > out_file.close() > > EXCELLENT! Thanks. > > THis works however there is one more tiny hiccup. The text has lost > all significant indention and newlines. Was this intended or is this a > result of another bug? Nevermind. Notepad was the problem. After using a real editor the text is displayed correctly! Thanks for help everyone! PS: I wonder why no one has added a note to the Python-list archives to advise people about the bug? -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Jan 5, 9:00 pm, MRAB wrote: > On 06/01/2012 02:14, random joe wrote: > > > On Jan 5, 7:27 pm, MRAB wrote: > > >> I've found that if I gunzip it twice (gunzip it and then gunzip the > >> result) using the gzip module I get the text file. > > > On a windows machine? If so, can you post a code snippet please? > > Thanks > > import gzip > > in_file = gzip.open(r"C:\2012-January.txt.gz") > out_file = open(r"C:\2012-January.txt.tmp", "wb") > out_file.write(in_file.read()) > in_file.close() > out_file.close() > > in_file = gzip.open(r"C:\2012-January.txt.tmp") > out_file = open(r"C:\2012-January.txt", "wb") > out_file.write(in_file.read()) > in_file.close() > out_file.close() EXCELLENT! Thanks. THis works however there is one more tiny hiccup. The text has lost all significant indention and newlines. Was this intended or is this a result of another bug? -- http://mail.python.org/mailman/listinfo/python-list
回复: problem to install Flask
yes i run python3,how to install flask in python3? -- 原始邮件 -- 发件人: "MRAB"; 发送时间: 2012年1月6日(星期五) 中午11:05 收件人: "python-list"; 主题: Re: problem to install Flask On 06/01/2012 02:24, 水静流深 wrote: > ~$ sudo easy_install Flask > Searching for Flask > Reading http://pypi.python.org/simple/Flask/ > Reading http://github.com/mitsuhiko/flask/ > Best match: Flask 0.8 > Downloading > http://pypi.python.org/packages/source/F/Flask/Flask-0.8.tar.gz#md5=a5169306cfe49b3b369086f2a63816ab > Processing Flask-0.8.tar.gz > Running Flask-0.8/setup.py -q bdist_egg --dist-dir > /tmp/easy_install-tnoyjj/Flask-0.8/egg-dist-tmp-pq04_a > Traceback (most recent call last): > File "/usr/local/bin/easy_install", line 9, in > load_entry_point('distribute==0.6.24', 'console_scripts', 'easy_install')() > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 1883, in main > with_ei_usage(lambda: > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 1864, in with_ei_usage > return f() > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 1887, in > distclass=DistributionWithoutHelpCommands, **kw > File "/usr/local/lib/python3.2/distutils/core.py", line 148, in setup > dist.run_commands() > File "/usr/local/lib/python3.2/distutils/dist.py", line 917, in run_commands > self.run_command(cmd) > File "/usr/local/lib/python3.2/distutils/dist.py", line 936, in run_command > cmd_obj.run() > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 349, in run > self.easy_install(spec, not self.no_deps) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 589, in easy_install > return self.install_item(spec, dist.location, tmpdir, deps) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 619, in install_item > dists = self.install_eggs(spec, download, tmpdir) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 809, in install_eggs > return self.build_and_install(setup_script, setup_base) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 1086, in build_and_install > self.run_setup(setup_script, setup_base, args) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", > line 1075, in run_setup > run_setup(setup_script, args) > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", > line 31, in run_setup > lambda: exec(compile(open( > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", > line 73, in run > return func() > File > "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", > line 33, in > ).read(), "setup.py", 'exec'), > File "setup.py", line 62 > print "Audit requires PyFlakes installed in your system.""" > ^ > SyntaxError: invalid syntax > > > what is the matter? > The syntax of this line: print "Audit requires PyFlakes installed in your system." tells me that it's written for Python 2, but this: /usr/local/lib/python3.2 tells me that you're using Python 3. In Python 2, "print" is a statement; in Python 3 it's a function. -- http://mail.python.org/mailman/listinfo/python-list-- http://mail.python.org/mailman/listinfo/python-list
Re: problem to install Flask
On 06/01/2012 02:24, 水静流深 wrote: ~$ sudo easy_install Flask Searching for Flask Reading http://pypi.python.org/simple/Flask/ Reading http://github.com/mitsuhiko/flask/ Best match: Flask 0.8 Downloading http://pypi.python.org/packages/source/F/Flask/Flask-0.8.tar.gz#md5=a5169306cfe49b3b369086f2a63816ab Processing Flask-0.8.tar.gz Running Flask-0.8/setup.py -q bdist_egg --dist-dir /tmp/easy_install-tnoyjj/Flask-0.8/egg-dist-tmp-pq04_a Traceback (most recent call last): File "/usr/local/bin/easy_install", line 9, in load_entry_point('distribute==0.6.24', 'console_scripts', 'easy_install')() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1883, in main with_ei_usage(lambda: File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1864, in with_ei_usage return f() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1887, in distclass=DistributionWithoutHelpCommands, **kw File "/usr/local/lib/python3.2/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/local/lib/python3.2/distutils/dist.py", line 917, in run_commands self.run_command(cmd) File "/usr/local/lib/python3.2/distutils/dist.py", line 936, in run_command cmd_obj.run() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 349, in run self.easy_install(spec, not self.no_deps) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 589, in easy_install return self.install_item(spec, dist.location, tmpdir, deps) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 619, in install_item dists = self.install_eggs(spec, download, tmpdir) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 809, in install_eggs return self.build_and_install(setup_script, setup_base) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1086, in build_and_install self.run_setup(setup_script, setup_base, args) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1075, in run_setup run_setup(setup_script, args) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 31, in run_setup lambda: exec(compile(open( File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 73, in run return func() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 33, in ).read(), "setup.py", 'exec'), File "setup.py", line 62 print "Audit requires PyFlakes installed in your system.""" ^ SyntaxError: invalid syntax what is the matter? The syntax of this line: print "Audit requires PyFlakes installed in your system." tells me that it's written for Python 2, but this: /usr/local/lib/python3.2 tells me that you're using Python 3. In Python 2, "print" is a statement; in Python 3 it's a function. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On 06/01/2012 02:14, random joe wrote: On Jan 5, 7:27 pm, MRAB wrote: I've found that if I gunzip it twice (gunzip it and then gunzip the result) using the gzip module I get the text file. On a windows machine? If so, can you post a code snippet please? Thanks import gzip in_file = gzip.open(r"C:\2012-January.txt.gz") out_file = open(r"C:\2012-January.txt.tmp", "wb") out_file.write(in_file.read()) in_file.close() out_file.close() in_file = gzip.open(r"C:\2012-January.txt.tmp") out_file = open(r"C:\2012-January.txt", "wb") out_file.write(in_file.read()) in_file.close() out_file.close() -- http://mail.python.org/mailman/listinfo/python-list
problem to install Flask
~$ sudo easy_install Flask Searching for Flask Reading http://pypi.python.org/simple/Flask/ Reading http://github.com/mitsuhiko/flask/ Best match: Flask 0.8 Downloading http://pypi.python.org/packages/source/F/Flask/Flask-0.8.tar.gz#md5=a5169306cfe49b3b369086f2a63816ab Processing Flask-0.8.tar.gz Running Flask-0.8/setup.py -q bdist_egg --dist-dir /tmp/easy_install-tnoyjj/Flask-0.8/egg-dist-tmp-pq04_a Traceback (most recent call last): File "/usr/local/bin/easy_install", line 9, in load_entry_point('distribute==0.6.24', 'console_scripts', 'easy_install')() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1883, in main with_ei_usage(lambda: File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1864, in with_ei_usage return f() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1887, in distclass=DistributionWithoutHelpCommands, **kw File "/usr/local/lib/python3.2/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/local/lib/python3.2/distutils/dist.py", line 917, in run_commands self.run_command(cmd) File "/usr/local/lib/python3.2/distutils/dist.py", line 936, in run_command cmd_obj.run() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 349, in run self.easy_install(spec, not self.no_deps) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 589, in easy_install return self.install_item(spec, dist.location, tmpdir, deps) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 619, in install_item dists = self.install_eggs(spec, download, tmpdir) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 809, in install_eggs return self.build_and_install(setup_script, setup_base) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1086, in build_and_install self.run_setup(setup_script, setup_base, args) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/command/easy_install.py", line 1075, in run_setup run_setup(setup_script, args) File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 31, in run_setup lambda: exec(compile(open( File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 73, in run return func() File "/usr/local/lib/python3.2/site-packages/distribute-0.6.24-py3.2.egg/setuptools/sandbox.py", line 33, in ).read(), "setup.py", 'exec'), File "setup.py", line 62 print "Audit requires PyFlakes installed in your system.""" ^ SyntaxError: invalid syntax what is the matter?-- http://mail.python.org/mailman/listinfo/python-list
Re: virtualenv wrecked my Django+modules install
On Jan 6, 5:01 am, Alec Taylor wrote: > Does this mean I need to install everything required by my project all > over again? - If so, should I take a virtualenv approach (and what > would the advantages be of doing so)? Virtualenv should _never_ clobber any non-virtualenv installs, that's the whole purpose of it. > Not only could I not get Pinax running, but when I went back to work > on my Satchmo-based project[...] Did you de-activate the Pinax virtualenv before trying to work on your other project? That's the only guess I have as to why you couldn't see your original installation. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Jan 5, 7:27 pm, MRAB wrote: > I've found that if I gunzip it twice (gunzip it and then gunzip the > result) using the gzip module I get the text file. On a windows machine? If so, can you post a code snippet please? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On 06/01/2012 00:10, Ian Kelly wrote: On Thu, Jan 5, 2012 at 4:52 PM, random joe wrote: Sure. Take the most recent file as example. "2012 - January.txt.gz". If you use the python doc example this is the result. If i use "r" or "rb" the result is the same. import gzip f1 = gzip.open('C:\\2012-January.txt.gz', 'rb') data = f1.read() data[:100] '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' f2 = gzip.open('C:\\2012-January.txt.gz', 'r') data = f2.read() data[:100] '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' The docs and google provide no clear answer. I even tried 7zip and ended up with nothing but gibberish characters. There must be levels of compression or something. Why could they not simply use the tar format? Is there anywhere else one can download the archives? Interesting. I tried this on a Linux system using both gunzip and your code, and both worked fine to extract that file. I also tried your code on a Windows system, and I get the same result that you do. This appears to be a bug in the gzip module under Windows. I think there may be something peculiar about the archive files that the module is not handling correctly. If I gunzip the file locally and then gzip it again before trying to open it in Python, then everything seems to be fine. I've found that if I gunzip it twice (gunzip it and then gunzip the result) using the gzip module I get the text file. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Jan 5, 6:10 pm, Ian Kelly wrote: > Interesting. I tried this on a Linux system using both gunzip and > your code, and both worked fine to extract that file. I also tried > your code on a Windows system, and I get the same result that you do. > This appears to be a bug in the gzip module under Windows. > > I think there may be something peculiar about the archive files that > the module is not handling correctly. If I gunzip the file locally > and then gzip it again before trying to open it in Python, then > everything seems to be fine. That is interesting. I wonder if anyone else has had the same issue? Just to be thorough I tried to uncompress using both python 2.x and 3.x and the results are unreadable text files in both cases. I have no idea what the problem could be. Especially without some way to compare my files to the gunzip'ed files on a linux machine. -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
On 01/05/2012 11:46 AM, Ian Kelly wrote: > On Thu, Jan 5, 2012 at 11:14 AM, Ian Kelly wrote: >> On Thu, Jan 5, 2012 at 1:05 AM, ru...@yahoo.com wrote: >>> I have optparse code that parses a command line containing >>> intermixed positional and optional arguments, where the optional >>> arguments set the context for the following positional arguments. >>> For example, >>> >>> myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 >>> >>> 'arg1' is processed in a default context, 'args2' and 'arg3' in >>> context '33', and 'arg4' in context '44'. >>> >>> I am trying to do the same using argparse but it appears to be >>> not doable in a documented way. >[...] > > Sorry, I missed the second part of that. You seem to be right, as far > as I can tell from tinkering with it, all the positional arguments > have to be in a single group. If you have some positional arguments > followed by an option followed by more positional arguments, and any > of the arguments have a loose nargs quantifier ('?' or '*' or '+'), > then you get an error. OK, thanks for the second confirmation. I was hoping there was something I missed or some undocumented option to allow intermixed optional and positional arguments with Argparse but it appears not. I notice that Optparse seems to intentionally provide this capability since it offers a "disable_interspersed_args()" method. It is unfortunate that Argparse chose to not to provide backward compatibility for this thus forcing some users to continue using a deprecated module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Thu, Jan 5, 2012 at 4:52 PM, random joe wrote: > Sure. Take the most recent file as example. "2012 - January.txt.gz". > If you use the python doc example this is the result. If i use "r" or > "rb" the result is the same. > import gzip f1 = gzip.open('C:\\2012-January.txt.gz', 'rb') data = f1.read() data[:100] > '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- > list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ > \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' f2 = gzip.open('C:\\2012-January.txt.gz', 'r') data = f2.read() data[:100] > '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- > list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ > \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' > > The docs and google provide no clear answer. I even tried 7zip and > ended up with nothing but gibberish characters. There must be levels > of compression or something. Why could they not simply use the tar > format? Is there anywhere else one can download the archives? Interesting. I tried this on a Linux system using both gunzip and your code, and both worked fine to extract that file. I also tried your code on a Windows system, and I get the same result that you do. This appears to be a bug in the gzip module under Windows. I think there may be something peculiar about the archive files that the module is not handling correctly. If I gunzip the file locally and then gzip it again before trying to open it in Python, then everything seems to be fine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Thu, Jan 5, 2012 at 4:39 PM, Miki Tebeka wrote: > Is there Google groups search not good enough? > (groups.google.com/group/comp.lang.python) My experience with the Google groups search (and Google groups in general) in the past has been terrible. If you're looking for a specific thread, it can actually be quite hard to find. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
On Jan 5, 5:39 pm, Miki Tebeka wrote: > Is the Google groups search not good enough? That works but i would like to do some regexes and set up some defaults. > Also, can you give an example of the code and an input file? Sure. Take the most recent file as example. "2012 - January.txt.gz". If you use the python doc example this is the result. If i use "r" or "rb" the result is the same. >>> import gzip >>> f1 = gzip.open('C:\\2012-January.txt.gz', 'rb') >>> data = f1.read() >>> data[:100] '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' >>> f2 = gzip.open('C:\\2012-January.txt.gz', 'r') >>> data = f2.read() >>> data[:100] '\x1f\x8b\x08\x08x\n\x05O\x02\xff/srv/mailman/archives/private/python- list/2012-January.txt\x00\xec\xbdy\x7f\xdb\xc6\xb50\xfcw\xf0)\xa6z|+ \xaa!!l\xdc\x14[\x8b-;V\xe2-\x92\x12' The docs and google provide no clear answer. I even tried 7zip and ended up with nothing but gibberish characters. There must be levels of compression or something. Why could they not simply use the tar format? Is there anywhere else one can download the archives? -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with python-list archives
Is there Google groups search not good enough? (groups.google.com/group/comp.lang.python) Also, can you give an example of the code and an input file? -- http://mail.python.org/mailman/listinfo/python-list
Help with python-list archives
Hi. I am new to python and wanted to search the python-list archives for answers to my many questions but i can't seem to get the archive files to uncompressed? What gives? From what i understand they are gzip files so i assumed the gzip module would work, but no! The best i could do was to get a ton of chinese chars using gzip and zlib.uncompress(). I would like to be courteous and search for my answers before asking so as not to waste anyones time. Does anyone know how to uncompress these files into a readable text form? -- http://mail.python.org/mailman/listinfo/python-list
socketserver question
Once I've instantiated my server class, along with a handler class, called server.serve_forever(), handler.handle() has been called, I've done my work, and I'm ready to shut the whole thing down... How do I do that? The doc says server.shutdown(), but if I call self.server.shutdown() from within handler.handle(), I seem to get a deadlock, which is exactly what I'd expect in a single threaded system with no way to "signal" the server.server_forever() loop which is several frames up the stack. I've also tried sys.exit() but it seems that the server object is catching that as an exception. How is this expected to work? How do I terminate the server.serve_forever() loop? --rich -- http://mail.python.org/mailman/listinfo/python-list
Re: virtualenv wrecked my Django+modules install
Use co-linux or VMware to do some experiment first. This is better than those old days of workstations or the mainframes from 5 to 10 vendors 20 years ago. -- http://mail.python.org/mailman/listinfo/python-list
Re: help me get excited about python 3
You get some of the good stuff by importing future, unicode literals which essentially means you're working in unicode by default most of the time, and print function, (a small fix but long overdue). I try to write python3 whenever I can. It's rare that dependencies keep me back. More often it's debugger problems or lack of distributions, (python3 doesn't entirely build on MacOsX Lion and the distributed binaries can't download third party code that requires compilation). If you do anything with raw data, the bytes/unicode upgrades are a god send. The old way was workable, (although I still don't understand string quoting). The new way is much better, more rational, easier to understand, more consistent, better documented, closer to intuitive expectations, and expressively clearer. Trying to find idioms that work for both is horrendous, though. Unicode rocks. This change alone is leading me to use python in many places where, in the past, I would have used /bin/sh for portability. Utf-8 file names, file names with spaces and other "special characters", and user entered data fields with diacriticals are all difficult to handle in /bin/sh, awkward in python2, but near trivial in python3. Classic classes are finally dead. Range now works like xrange used to. This is great, although a bit clumsy when trying to write for both 2 and 3 concurrently. In practice, most of the library changes are 1:1 renames, which are both worthwhile and easy enough to work with. Most of the other interesting features, (imo), have been backported to 2.7. Context managers, "with", str.format(), etc. I'm currently writing in both more or less concurrently most of the time, (can't afford to live without the debugger), and I'm really, REALLY looking forward to the day when I can drop the python2 idioms. I really hate adding crap to python3 clean code in order to backport support for python2. Really, the biggest win to 3, aside from the unicode/bytes change, is the fact that a lot of outdated stuff is finally getting flushed. IMO, it's not so much about the new features, (context managers are big, but have been backported), as it is about the lack of pollution from ancient ones. Many of the new changes are ramifications of these removals. --rich -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to install xmldiff package on WIndows7
On 05/01/2012 19:34, John Gordon wrote: In hisan writes: Can some one help me out here Its High Priority GCC is failing with a "No such file or directory" error. I assume this means either that gcc is not installed on your computer, or that it couldn't find one of the files you passed on the command line. The first case seems less likely, as I would expect a "Command not found" error instead of "No such file or directory". The second case seems more likely, especially since one of the directories GCC is trying to use contains embedded spaces in the name: -ID:\Python26\PC - c extensions/maplookup.c Can you rename that directory to something that does not contain spaces and try it again? Alternatively, enclose it in quotes. -- http://mail.python.org/mailman/listinfo/python-list
Re: a little help
On 01/06/2012 03:04 AM, Andres Soto wrote: Please, see my comments between your lines. Thank you very much for your explanation! * * *From:* Lie Ryan *To:* python-list@python.org *Sent:* Thursday, January 5, 2012 2:30 AM *Subject:* Re: a little help On 01/05/2012 11:29 AM, Andres Soto wrote: > my mistake is because I have no problem to do that using Prolog which > use an interpreter as Python. I thought that the variables in the main > global memory space (associated with the command line environment) were > kept, although the code that use it could change. > As you explain me, Python behave like a compiled language: any time I > make a change in the code, I have to "compile" it again, and re-run (and > re-load the data). There is nothing to do. it is usually trivial to redefine state-free functions, you just need to copy and paste the new code into the shell. &&&yes, I am already using that, but I thought that maybe there were a more elegant way. In Prolog, you just have to reload the code and nothing happens with the global variables Alternative to copy pasting is to reload the module; but that comes with the caution that the old function/class definition may still be lying around in the global namespace if you imported them into your global namespace, so you had to either restrict yourself to using class/function using the module namespace or you had to remember to reimport them into your global namespace. You also need to be careful if you passes a module function as callbacks, as the callback will not be automatically replaced with the new definition. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to install xmldiff package on WIndows7
In hisan writes: > Can some one help me out here Its High Priority GCC is failing with a "No such file or directory" error. I assume this means either that gcc is not installed on your computer, or that it couldn't find one of the files you passed on the command line. The first case seems less likely, as I would expect a "Command not found" error instead of "No such file or directory". The second case seems more likely, especially since one of the directories GCC is trying to use contains embedded spaces in the name: -ID:\Python26\PC - c extensions/maplookup.c Can you rename that directory to something that does not contain spaces and try it again? -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- http://mail.python.org/mailman/listinfo/python-list
virtualenv wrecked my Django+modules install
Good morning, On my system (Windows 8 Developer Preview x64 with Python 2.7.2 x64) I painstakingly installed all the dependencies for Django, Satchmo and a few other large scale projects. (about 10% couldn't be installed with pip, which is why I use the word 'painstakingly') Recently though I needed Pinax for something, so following there guide (http://pinax.readthedocs.org/en/latest/gettingstarted.html) I setup a virtualenv for it. Not only could I not get Pinax running, but when I went back to work on my Satchmo-based project, and ran "manage.py runserver" I got the following error: Traceback (most recent call last): File "P:\Prototype\Django\store\manage.py", line 18, in from django.core.management import execute_manager ImportError: No module named django.core.management Typing in `import django` from the interpreter gives me: Traceback (most recent call last): File "", line 1, in ImportError: No module named django Does this mean I need to install everything required by my project all over again? - If so, should I take a virtualenv approach (and what would the advantages be of doing so)? Thanks for all suggestions, Alec Taylor -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
On Thu, Jan 5, 2012 at 11:14 AM, Ian Kelly wrote: > On Thu, Jan 5, 2012 at 1:05 AM, ru...@yahoo.com wrote: >> I have optparse code that parses a command line containing >> intermixed positional and optional arguments, where the optional >> arguments set the context for the following positional arguments. >> For example, >> >> myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 >> >> 'arg1' is processed in a default context, 'args2' and 'arg3' in >> context '33', and 'arg4' in context '44'. >> >> I am trying to do the same using argparse but it appears to be >> not doable in a documented way. >> >> Here is the working optparse code (which took 30 minutes to write >> using just the optparse docs): >> >> import optparse >> def append_with_pos (option, opt_str, value, parser): >> if getattr (parser.values, option.dest, None) is None: >> setattr (parser.values, option.dest, []) >> getattr (parser.values, option.dest).append ((value, len >> (parser.largs))) >> def opt_parse(): >> p = optparse.OptionParser() >> p.add_option ("-c", type=int, >> action='callback', callback=append_with_pos) >> opts, args = p.parse_args() >> return args, opts >> if __name__ == '__main__': >> args, opts = opt_parse() >> print args, opts >> >> Output from the command line above: >> ['arg1', 'arg2', 'arg3', 'arg4'] {'c': [(33, 1), (44, 3)]} >> The -c values are stored as (value, arglist_position) tuples. >> >> Here is an attempt to convert to argparse using the guidelines >> in the argparse docs: >> >> import argparse >> class AppendWithPos (argparse.Action): >> def __call__ (self, parser, namespace, values, >> option_string=None): >> if getattr (namespace, self.dest, None) is None: >> setattr (namespace, self.dest, []) >> getattr (namespace, self.dest).extend ((values, len >> (parser.largs))) >> def arg_parse(): >> p = argparse.ArgumentParser (description='description') >> p.add_argument ('src', nargs='*') >> p.add_argument ('-c', type=int, action=AppendWithPos) >> opts = p.parse_args() >> return opts >> if __name__ == '__main__': >> opts = arg_parse() >> print opts >> >> This fails with, >> AttributeError: 'ArgumentParser' object has no attribute 'largs' >> and of course, the argparse.parser is not documented beyond how >> to instantiate it. Even were that not a problem, argparse complains >> about "unrecognised arguments" for any positional arguments that >> occur after an optional one. I've been farting with this code for >> a day now. >> >> Any suggestions on how I can convince argparse to do what optparse >> does easily will be very welcome. (I tried parse_known_args() but >> that breaks help and requires me to detect truly unknown arguments.) >> >> (Python 2.7.1 if it matters and apologies if Google mangles >> the formatting of this post.) > > You have the namespace object in your custom action. Instead of > "len(parser.largs)", couldn't you just do "len(namespace.src)"? Sorry, I missed the second part of that. You seem to be right, as far as I can tell from tinkering with it, all the positional arguments have to be in a single group. If you have some positional arguments followed by an option followed by more positional arguments, and any of the arguments have a loose nargs quantifier ('?' or '*' or '+'), then you get an error. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to install xmldiff package on WIndows7
On Thu, 5 Jan 2012 10:03:32 -0800 (PST) hisan wrote: > Can some one help me out here Its High Priority > > > > On Jan 4, 12:47 am, hisan wrote: > > Hi All > > > > i have downloaded "xmldiff-0.6.10" from their official site > > (http://www.logilab.org/859). I have tried installing the same on > > my 32 bit Windows 7 OS using the command "setup.py install" but > > below exceptions are thrown in the console. > > please help me out in installing this package on Windows > > > > Exceptions thrown while installing > > > > C:\Users\santosh\Downloads\xmldiff-0.6.10>setup.py build > > running build > > running build_py > > package init file '.\test\__init__.py' not found (or not a regular > > file) > > package init file '.\test\__init__.py' not found (or not a regular > > file) > > running build_ext > > building 'xmldiff.maplookup' extension > > gcc -mno-cygwin -mdll -O -Wall -ID:\Python26\include > > -ID:\Python26\PC - c extensions/maplookup.c -o b > > uild\temp.win32-2.6\Release\extensions\maplookup.o > > error: command 'gcc' failed: No such file or directory > It trying to compile this extension using GNU gcc. You'd need that to compile this one windows. Don't know if there is a windows version but check http://gcc.gnu.org -- Rod Person http://www.rodperson.com rodper...@rodperson.com 'Silence is a fence around wisdom' -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
On Thu, Jan 5, 2012 at 1:05 AM, ru...@yahoo.com wrote: > I have optparse code that parses a command line containing > intermixed positional and optional arguments, where the optional > arguments set the context for the following positional arguments. > For example, > > myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 > > 'arg1' is processed in a default context, 'args2' and 'arg3' in > context '33', and 'arg4' in context '44'. > > I am trying to do the same using argparse but it appears to be > not doable in a documented way. > > Here is the working optparse code (which took 30 minutes to write > using just the optparse docs): > > import optparse > def append_with_pos (option, opt_str, value, parser): > if getattr (parser.values, option.dest, None) is None: > setattr (parser.values, option.dest, []) > getattr (parser.values, option.dest).append ((value, len > (parser.largs))) > def opt_parse(): > p = optparse.OptionParser() > p.add_option ("-c", type=int, > action='callback', callback=append_with_pos) > opts, args = p.parse_args() > return args, opts > if __name__ == '__main__': > args, opts = opt_parse() > print args, opts > > Output from the command line above: > ['arg1', 'arg2', 'arg3', 'arg4'] {'c': [(33, 1), (44, 3)]} > The -c values are stored as (value, arglist_position) tuples. > > Here is an attempt to convert to argparse using the guidelines > in the argparse docs: > > import argparse > class AppendWithPos (argparse.Action): > def __call__ (self, parser, namespace, values, > option_string=None): > if getattr (namespace, self.dest, None) is None: > setattr (namespace, self.dest, []) > getattr (namespace, self.dest).extend ((values, len > (parser.largs))) > def arg_parse(): > p = argparse.ArgumentParser (description='description') > p.add_argument ('src', nargs='*') > p.add_argument ('-c', type=int, action=AppendWithPos) > opts = p.parse_args() > return opts > if __name__ == '__main__': > opts = arg_parse() > print opts > > This fails with, > AttributeError: 'ArgumentParser' object has no attribute 'largs' > and of course, the argparse.parser is not documented beyond how > to instantiate it. Even were that not a problem, argparse complains > about "unrecognised arguments" for any positional arguments that > occur after an optional one. I've been farting with this code for > a day now. > > Any suggestions on how I can convince argparse to do what optparse > does easily will be very welcome. (I tried parse_known_args() but > that breaks help and requires me to detect truly unknown arguments.) > > (Python 2.7.1 if it matters and apologies if Google mangles > the formatting of this post.) You have the namespace object in your custom action. Instead of "len(parser.largs)", couldn't you just do "len(namespace.src)"? Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to install xmldiff package on WIndows7
Can some one help me out here Its High Priority On Jan 4, 12:47 am, hisan wrote: > Hi All > > i have downloaded "xmldiff-0.6.10" from their official site > (http://www.logilab.org/859). > I have tried installing the same on my 32 bit Windows 7 OS using the > command "setup.py install" but below exceptions are thrown in the > console. > please help me out in installing this package on Windows > > Exceptions thrown while installing > > C:\Users\santosh\Downloads\xmldiff-0.6.10>setup.py build > running build > running build_py > package init file '.\test\__init__.py' not found (or not a regular > file) > package init file '.\test\__init__.py' not found (or not a regular > file) > running build_ext > building 'xmldiff.maplookup' extension > gcc -mno-cygwin -mdll -O -Wall -ID:\Python26\include -ID:\Python26\PC - > c extensions/maplookup.c -o b > uild\temp.win32-2.6\Release\extensions\maplookup.o > error: command 'gcc' failed: No such file or directory -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
On 01/05/2012 02:19 AM, Ulrich Eckhardt wrote: > Am 05.01.2012 09:05, schrieb ru...@yahoo.com: >> I have optparse code that parses a command line containing >> intermixed positional and optional arguments, where the optional >> arguments set the context for the following positional arguments. >> For example, >> >>myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 >> >> 'arg1' is processed in a default context, 'args2' and 'arg3' in >> context '33', and 'arg4' in context '44'. > > Question: How would you e.g. pass the string "-c33" as first argument, > i.e. to be parsed in the default context? There will not be a need for that. > The point is that you separate the parameters in a way that makes it > possible to parse them in a way that works 100%, not just a way that > works in 99% of all cases. I agree that one should strive for a syntax that "works 100%" but in this case, the simplicity and intuitiveness of the existing command syntax outweigh by far the need for having it work in very improbable corner cases. (And I'm sure I've seen this syntax used in other unix command line tools in the past though I don't have time to look for examples now.) If argparse does not handle this syntax for some such purity reason (as opposed to, for example. it is hard to do in argparse's current design) then argparse is mistakenly putting purity before practicality. > For that reason, many commandline tools > accept "--" as separator, so that "cp -- -r -x" will copy the file "-r" > to the folder "-x". In that light, I would consider restructuring your > commandline. In my case that's not possible since I am replacing an existing tool with a Python application and changing the command line syntax is not an option. >> I am trying to do the same using argparse but it appears to be >> not doable in a documented way. > > As already hinted at, I don't think this is possible and that that is so > by design. Thanks for the confirmation. I guess that shows that optparse has a reason to exist beyond backwards compatibility. -- http://mail.python.org/mailman/listinfo/python-list
Re: Typed python comparison / code analysis questions
On 05/01/2012 06:37, Terry Reedy wrote: On 1/4/2012 3:42 PM, Lucas Vickers wrote: At the moment python3 isn't an option. There's a variety of dependencies I'm working around. Please consider telling the authors of libraries you need that you would like a Python 3 version and say why. One reason given for not upgrading packages is 'lack of demand'. I could also add that if you find a bug in a library, report it. As an example, some months ago I read on StackOverflow an old answer where someone mentioned trying the "regex" module but had found that some Unicode properties were unreliable. That came as a surprise to me because no-one had reported such a problem (and I still don't know what that problem was). Compare that with a recent bug report that it didn't work correctly on Mac OS X. The reporter was very helpful and that bug is now fixed. -- http://mail.python.org/mailman/listinfo/python-list
Re: a little help
Please, see my comments between your lines. Thank you very much for your explanation! > > >From: Lie Ryan >To: python-list@python.org >Sent: Thursday, January 5, 2012 2:30 AM >Subject: Re: a little help > >On 01/05/2012 11:29 AM, Andres Soto wrote: >> my mistake is because I have no problem to do that using Prolog which >> use an interpreter as Python. I thought that the variables in the main >> global memory space (associated with the command line environment) were >> kept, although the code that use it could change. >> As you explain me, Python behave like a compiled language: any time I >> make a change in the code, I have to "compile" it again, and re-run (and >> re-load the data). There is nothing to do. > >it is usually trivial to redefine state-free functions, you just need to copy >and paste the new code into the shell. > > >&&&yes, I am already using that, but I thought that maybe there were a more >elegant way. In Prolog, you just have to reload the code and nothing happens >with the global variables > > > Redefining a class is a bit more complicated, while you can redefine a class >by the same technique (copy pasting the new class definition to the shell), it >will not modify the class definition for existing instances of that class. >Worst comes to worst, you could end up with a list of instances where half of >the items come from the old definition and the other half from the new >definition. > > >&&&I tried to use classes but I got not good results so I left it for a while > >If your global data are only of native types (e.g. list, dict, int, float), >then you usually can safely carry your data between redefinitions; > > >&&&up to now, I am just using native types (e.g. list, dict, int, float). How >can I carry my data between redefinitions? copying and pasting the new code >into the shell? OK, that I am doing > > >if you have objects in your global data that you want to preserve, you need to >be really careful not to confuse instances from old definitions with instances >from new definitions. > > >&&&yes, I understand that > >Also, reload() will reload a module with the new definition, but it does not >touch existing function definitions in the global namespace; therefore if you >want to use reload(), you probably should avoid "from ... import ..." (if you >want to import module functions into your global namespace, then you'll need >to reimport them after you reload the module). > > >So here's the gotchas to be aware of when reloading modules: > > >1. import is cached, if you want to reimport a changed module you have to call >reload() >2. reload does not modify anything in existing global namespace, if you have >imported functions/class definition to your global namespace, you will need to >reimport them after reloading. >3. be careful if you mix instances made from old definitions with instances >made from new definitions, python does not modify the class definition of >existing instances >4. be careful when reloading functions that have function attributes. The same >caution applies when reloading class that have class attributes. > >-- http://mail.python.org/mailman/listinfo/python-list > > >-- http://mail.python.org/mailman/listinfo/python-list
Re: a little help
could you be a little bit more explicit. I am a begginer and I don't understand you quite well Thanks Andres Soto > > From: 8 Dihedral >To: python-list@python.org >Cc: python-list@python.org >Sent: Thursday, January 5, 2012 2:48 AM >Subject: Re: a little help > >Chris Angelico於 2012年1月5日星期四UTC+8上午7時29分21秒寫道: >> On Thu, Jan 5, 2012 at 10:25 AM, Andres Soto wrote: >> > My situation is the following: I am developing some code. I use the IDLE >> > Editor to write it down. Then, I save it and import it from the command >> > line >> > interface, so it is already available from the prompt. >> > Then I load (read) some data from files using that code. Let suppose that >> > after that I make some changes in the code using again the IDLE Editor, >> > save >> > the program code, and…what else? The updated code is not already available >> > from the command line interface. If I run the module, I lose the data >> > already loaded (and it is a big amount). If I re-import it, the new code is >> > not available >> >> Re-importing modules is a bit messy. The usual way to do this sort of >> thing would be to run the program directly from the command line, and >> terminate it when you're done. Is there a particular reason for >> wanting to import it that way? >> >> Chris Angelico > >Thus you are developing a module in python. >Just use module_name_v??? in those experiments. > >The version compatible problem is developer's job. >-- >http://mail.python.org/mailman/listinfo/python-list > > >-- http://mail.python.org/mailman/listinfo/python-list
Re: argparse and default values
On 01/05/2012 01:24 PM, Andrea Crotti wrote: There's one thing I don't understand about argparse, why doesn't --help show what is the default value?? I mean if I add an option that can be customized it would be good for the user to know what is the current value in my opinion. Is there a way to make it show it? Looking at the code I found it already parser = argparse.ArgumentParser(description=description, formatter_class=argparse.ArgumentDefaultsHelpFormatter) the only thing is that it displays also "default: None" which isn't probably very useful, but it would easy to write my own formatter and pass it if I don't like this.. -- http://mail.python.org/mailman/listinfo/python-list
argparse and default values
There's one thing I don't understand about argparse, why doesn't --help show what is the default value?? I mean if I add an option that can be customized it would be good for the user to know what is the current value in my opinion. Is there a way to make it show it? -- http://mail.python.org/mailman/listinfo/python-list
Re: can a subclass method determine if called by superclass?
Jean-Michel Pichavant wrote: Peter wrote: Situation: I am subclassing a class which has methods that call other class methods (and without reading the code of the superclass I am discovering these by trial and error as I build the subclass - this is probably why I may have approached the problem from the wrong viewpoint :-)). Problem: when overriding one of these "indirectly called" superclass methods I would like to take differing actions (in the subclass instance) depending on whether it is the superclass or the subclass instance performing the call. Question: Is there any way to determine in a method whether it is being called by the superclass or by a method of the subclass instance? Now I suspect that what I am doing is actually very muddy thinking :-) and I don't want to attempt to explain why I am approaching the design this way as an explanation would require too much work - I will consider an alternative inheritance approach while waiting an answer, but the answer to the question interested me (even if I do a redesign and come up with a more "elegant" approach to the problem). Thanks Peter As you suspected, this is probably the wrong approach. However since you asked for a solution anyway :o) class Parent(object): def foo(self): # implementation by subclasses is still REQUIRED if self.__class__ is Parent: raise NotImplementedError() # common code for all foo methods print "calling foo" class Child(Parent): def foo(self): # You can still call the virtual method which contains some code Parent.foo(self) # here the custom code p = Parent() c = Child() c.foo() p.foo() Note that this is not the best approach, still acceptable because there is no code specific to a subclass in the base class. JM I just realized I didn't addressed the problem you described, sorry, just ignore my mail. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: can a subclass method determine if called by superclass?
Peter wrote: Situation: I am subclassing a class which has methods that call other class methods (and without reading the code of the superclass I am discovering these by trial and error as I build the subclass - this is probably why I may have approached the problem from the wrong viewpoint :-)). Problem: when overriding one of these "indirectly called" superclass methods I would like to take differing actions (in the subclass instance) depending on whether it is the superclass or the subclass instance performing the call. Question: Is there any way to determine in a method whether it is being called by the superclass or by a method of the subclass instance? Now I suspect that what I am doing is actually very muddy thinking :-) and I don't want to attempt to explain why I am approaching the design this way as an explanation would require too much work - I will consider an alternative inheritance approach while waiting an answer, but the answer to the question interested me (even if I do a redesign and come up with a more "elegant" approach to the problem). Thanks Peter As you suspected, this is probably the wrong approach. However since you asked for a solution anyway :o) class Parent(object): def foo(self): # implementation by subclasses is still REQUIRED if self.__class__ is Parent: raise NotImplementedError() # common code for all foo methods print "calling foo" class Child(Parent): def foo(self): # You can still call the virtual method which contains some code Parent.foo(self) # here the custom code p = Parent() c = Child() c.foo() p.foo() Note that this is not the best approach, still acceptable because there is no code specific to a subclass in the base class. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
Am 05.01.2012 09:05, schrieb ru...@yahoo.com: I have optparse code that parses a command line containing intermixed positional and optional arguments, where the optional arguments set the context for the following positional arguments. For example, myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 'arg1' is processed in a default context, 'args2' and 'arg3' in context '33', and 'arg4' in context '44'. Question: How would you e.g. pass the string "-c33" as first argument, i.e. to be parsed in the default context? The point is that you separate the parameters in a way that makes it possible to parse them in a way that works 100%, not just a way that works in 99% of all cases. For that reason, many commandline tools accept "--" as separator, so that "cp -- -r -x" will copy the file "-r" to the folder "-x". In that light, I would consider restructuring your commandline. I am trying to do the same using argparse but it appears to be not doable in a documented way. As already hinted at, I don't think this is possible and that that is so by design. Sorry.. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: help me get excited about python 3
On 01/05/2012 03:41 PM, Evan Driscoll wrote: On 1/4/2012 9:56 AM, Sean Wolfe wrote: I am still living in the 2.x world because all the things I want to do right now in python are in 2 (django, pygame). But I want to be excited about the future of the language. I understand the concept of needing to break backwards compatibility. But it's not particularly exciting to think about. What are the cool new bits I should be reading up on? This should be enough to convince you: ~ : python Python 2.7.1 (r271:86832, May 3 2011, 10:31:28) 1< "1" True ~ : python3 Python 3.2 (r32:88445, May 3 2011, 13:26:55) 1< "1" Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: int()< str() Maybe with Python 4, '1< True' will give a TypeError too ;-). Or if that's not enough, ~ : python Python 2.7.1 (r271:86832, May 3 2011, 10:31:28) True, False = False, True "True is " + ("True" if True else "False") 'True is False' ~ : python3 Python 3.2 (r32:88445, May 3 2011, 13:26:55) True, False = False, True File "", line 1 SyntaxError: assignment to keyword Somehow I could hear Sean saying something like: "Yeah... and so?" -- http://mail.python.org/mailman/listinfo/python-list
Re: a little help
Chris Angelico於 2012年1月5日星期四UTC+8上午7時29分21秒寫道: > On Thu, Jan 5, 2012 at 10:25 AM, Andres Soto wrote: > > My situation is the following: I am developing some code. I use the IDLE > > Editor to write it down. Then, I save it and import it from the command line > > interface, so it is already available from the prompt. > > Then I load (read) some data from files using that code. Let suppose that > > after that I make some changes in the code using again the IDLE Editor, save > > the program code, and…what else? The updated code is not already available > > from the command line interface. If I run the module, I lose the data > > already loaded (and it is a big amount). If I re-import it, the new code is > > not available > > Re-importing modules is a bit messy. The usual way to do this sort of > thing would be to run the program directly from the command line, and > terminate it when you're done. Is there a particular reason for > wanting to import it that way? > > Chris Angelico Thus you are developing a module in python. Just use module_name_v??? in those experiments. The version compatible problem is developer's job. -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse missing optparse capabilities?
On Jan 5, 1:05 am, "ru...@yahoo.com" wrote: > class AppendWithPos (argparse.Action): > def __call__ (self, parser, namespace, values, > option_string=None): > if getattr (namespace, self.dest, None) is None: > setattr (namespace, self.dest, []) > getattr (namespace, self.dest).extend ((values, len (parser.largs))) I realized right after posting that the above line should be I think, getattr (namespace, self.dest).extend ((values, len (namespace.src))) but that still doesn't help with the "unrecognised arguments" problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: a little help
On 01/05/2012 11:29 AM, Andres Soto wrote: my mistake is because I have no problem to do that using Prolog which use an interpreter as Python. I thought that the variables in the main global memory space (associated with the command line environment) were kept, although the code that use it could change. As you explain me, Python behave like a compiled language: any time I make a change in the code, I have to "compile" it again, and re-run (and re-load the data). There is nothing to do. it is usually trivial to redefine state-free functions, you just need to copy and paste the new code into the shell. Redefining a class is a bit more complicated, while you can redefine a class by the same technique (copy pasting the new class definition to the shell), it will not modify the class definition for existing instances of that class. Worst comes to worst, you could end up with a list of instances where half of the items come from the old definition and the other half from the new definition. If your global data are only of native types (e.g. list, dict, int, float), then you usually can safely carry your data between redefinitions; if you have objects in your global data that you want to preserve, you need to be really careful not to confuse instances from old definitions with instances from new definitions. Also, reload() will reload a module with the new definition, but it does not touch existing function definitions in the global namespace; therefore if you want to use reload(), you probably should avoid "from ... import ..." (if you want to import module functions into your global namespace, then you'll need to reimport them after you reload the module). So here's the gotchas to be aware of when reloading modules: 1. import is cached, if you want to reimport a changed module you have to call reload() 2. reload does not modify anything in existing global namespace, if you have imported functions/class definition to your global namespace, you will need to reimport them after reloading. 3. be careful if you mix instances made from old definitions with instances made from new definitions, python does not modify the class definition of existing instances 4. be careful when reloading functions that have function attributes. The same caution applies when reloading class that have class attributes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding an interface to existing classes
(I'm sorry for my delayed response -- I've been travelling and not had reliable Internet access.) On 2011-12-25, Ian Kelly wrote: > On Thu, Dec 22, 2011 at 1:21 AM, Spencer Pearson > wrote: >> I see a problem with this, though. The intersection of two lines is >> (usually) an object of type Point. Since DrawableLine inherits from >> Line, this means that unless I redefine the "intersect" method in >> DrawableLine, the intersection of two DrawableLines will be a Point >> object, not a DrawablePoint. I don't want to saddle the user with the >> burden of converting every method output into its corresponding >> Drawable subclass, and I don't want to redefine every method to return >> an instance of said subclass. I see no other options if I continue >> down the subclassing path. Am I missing something? > > You could solve this with a factory. Instead of having > Line.intersection() create a Point directly, have it call > self.factory.create_point(). In the drawing module, replace the > factory for the subclasses with one that creates drawable classes > instead. Oh, that's a neat idea. Yes, I think that does exactly what I want! Thanks very much! > This will also work, but inheritance complicates things a > little. ... > ... Probably the easiest way to do this correctly is to follow the > MRO that Python has conveniently already built for you. Something > like: > > def draw(x, *args, **kwargs ): > for class_ in type(x).__mro__: > if class_ in draw_functions: > draw_functions[class_](*args, **kwargs) > break > else: > raise TypeError("don't know how to draw things of type " > "{0}".format(type(x))) You're right, you're right. My implementation was sloppy. I think I'll go with your factory solution, but thanks for the fixed version of draw() -- I've never seen __mro__ before, and it seems to be just the tool for this job! -Spencer -- http://mail.python.org/mailman/listinfo/python-list
argparse missing optparse capabilities?
I have optparse code that parses a command line containing intermixed positional and optional arguments, where the optional arguments set the context for the following positional arguments. For example, myprogram.py arg1 -c33 arg2 arg3 -c44 arg4 'arg1' is processed in a default context, 'args2' and 'arg3' in context '33', and 'arg4' in context '44'. I am trying to do the same using argparse but it appears to be not doable in a documented way. Here is the working optparse code (which took 30 minutes to write using just the optparse docs): import optparse def append_with_pos (option, opt_str, value, parser): if getattr (parser.values, option.dest, None) is None: setattr (parser.values, option.dest, []) getattr (parser.values, option.dest).append ((value, len (parser.largs))) def opt_parse(): p = optparse.OptionParser() p.add_option ("-c", type=int, action='callback', callback=append_with_pos) opts, args = p.parse_args() return args, opts if __name__ == '__main__': args, opts = opt_parse() print args, opts Output from the command line above: ['arg1', 'arg2', 'arg3', 'arg4'] {'c': [(33, 1), (44, 3)]} The -c values are stored as (value, arglist_position) tuples. Here is an attempt to convert to argparse using the guidelines in the argparse docs: import argparse class AppendWithPos (argparse.Action): def __call__ (self, parser, namespace, values, option_string=None): if getattr (namespace, self.dest, None) is None: setattr (namespace, self.dest, []) getattr (namespace, self.dest).extend ((values, len (parser.largs))) def arg_parse(): p = argparse.ArgumentParser (description='description') p.add_argument ('src', nargs='*') p.add_argument ('-c', type=int, action=AppendWithPos) opts = p.parse_args() return opts if __name__ == '__main__': opts = arg_parse() print opts This fails with, AttributeError: 'ArgumentParser' object has no attribute 'largs' and of course, the argparse.parser is not documented beyond how to instantiate it. Even were that not a problem, argparse complains about "unrecognised arguments" for any positional arguments that occur after an optional one. I've been farting with this code for a day now. Any suggestions on how I can convince argparse to do what optparse does easily will be very welcome. (I tried parse_known_args() but that breaks help and requires me to detect truly unknown arguments.) (Python 2.7.1 if it matters and apologies if Google mangles the formatting of this post.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding an interface to existing classes
On Dec 25 2011, 2:58 pm, Terry Reedy wrote: > On 12/24/2011 6:49 PM,SpencerPearsonwrote: > > > On Dec 23, 9:13 am, Terry Reedy wrote: > >> On 12/22/2011 3:21 AM,SpencerPearsonwrote: > > >>> I'm writing a geometry package, with Points and Lines and Circles and > >>> so on, and eventually I want to be able to draw these things on the > >>> screen. I have two options so far for how to accomplish this, but > >>> neither of them sits quite right with me, and I'd like the opinion of > >>> comp.lang.python's wizened elders. > > >>> Option 1. Subclassing. > >>> The most Pythonic way would seem to be writing subclasses for the > >>> things I want to display, adding a ".draw(...)" method to each one, > >>> like this: > > There are people who would advocate a Drawable base class with a virtual > or abstract .draw method and that DrawablePoint, etc, inherit from > Drawable and Point. > > >>> Option 2. A "draw" function, with a function dictionary. > > >> Option 3? Add a draw method to existing classes, rather than subclassing? > > Thanks for the response! Do you mean something like this? > > class Point(GeometricObject): > > def intersect(self, other): > > ... > > I am interpreting this to mean that you have a world coordinate system > for instances that have location and size. > > > def union(self, other): > > ... > > def draw(self, ...): > > ... > > Yes. I would consider that Option 0, the default, unless you have good > reason to choose another. I would certainly include it on a list of options. > > > I'd like to avoid this, because... well, what I want is a geometry > > package, and it seems to me that whistles and bells like GUI support > > ought to be confined to subpackages. I'd look at this and think, "the > > rest of the Point class deals with fundamental geometric reality. > > What's this GUI method doing mixed in with my beautiful mathematical > > purity?" > > By default, all Python objects have a text representation method. I do > not see that giving all concrete geometric objects (with a location and > size) a visual representation is much different. I would use drawing > functions that accept the coordinates and distances of your geometry > world and translate to low-level pixel functions for a particular gui > system. I agree that your geometrical objects should not know about > pixels, screens, windows, and aspect ratios. > > > Is this the wrong attitude to take? > > It depends on *your* goal and values. > > > Or did you mean this? > > In file "geometry/gui.py": > > def draw_point(point, ...): > > ... > > Point.draw = draw_point > > > I've never modified an existing class before, and I fear the > > unfamiliar. If that's what you meant... it's really an acceptable > > thing to do? > > Yes, in my opinion. The advantage of this is putting all the draw > methods together, and possibly having more than one one set. On the > other hand, one needs to know the data attributes of each class to > understand its draw method. > > > It seems like somebody might see "some_point.draw(...)" > > and be confused by the method's absence in the Point class definition. > > With either suboption, you should put an abstract .draw method in the > GeometricObject base class. > > I would look at various game, graph, geometry, and gui packages handle > drawing for more ideas. > > -- > Terry Jan Reedy (I'm sorry for my delayed response -- I've been travelling and not had reliable Internet access.) On 2011-12-25, Terry Reedy wrote: > There are people who would advocate a Drawable base class with a virtual > or abstract .draw method and that DrawablePoint, etc, inherit from > Drawable and Point. Yes... yes, that makes sense to me. > By default, all Python objects have a text representation method. I do > not see that giving all concrete geometric objects (with a location and > size) a visual representation is much different. I would use drawing > functions that accept the coordinates and distances of your geometry > world and translate to low-level pixel functions for a particular gui > system. I agree that your geometrical objects should not know about > pixels, screens, windows, and aspect ratios. Ha! Oh, I've been being silly. I was going to claim that since there is no standard Python GUI, I ought not chain myself to any one of the candidates. Then I learned that Tkinter comes standard with Python. Oops. All right, now that I know that, the comparison to having a text representation seems very reasonable. I'll definitely reconsider making the draw() method a requirement for all GeometricObjects. >> I've never modified an existing class before, and I fear the >> unfamiliar. If that's what you meant... it's really an acceptable >> thing to do? > > Yes, in my opinion. The advantage of this is putting all the draw > methods together, and possibly having more than one one set. On the > other hand, one needs to know the data attributes of each class to > understand its draw me
Re: Adding an interface to existing classes
(I'm sorry for my delayed response -- I've been travelling and not had reliable Internet access.) >> Spencer, i would re-think this entire project from the >> beginning. You are trying to make an object out of everything. You >> don't need to make an object of EVERYTHING. > > Very true. I'm not sure I understand. Surely you're not both saying that I shouldn't write a Point class? Here's an expression I'd like very much to be able to type: sphere1.intersect(sphere2).rotated(angle, axis) If I represented points with something already defined, maybe a tuple or a numpy array, then that would risk that the spheres were tangent, that their intersection was not a Circle (a GeometricObject, with a .rotated method), but a point (a tuple, without one). On 2011-12-25, Steven D'Aprano wrote: > class Line: > intersection_kind = Point > def intersection(self, other): > blah() > return self.intersection_kind(a, b) > > class DrawableLine(Line): > intersection_kind = DrawablePoint If I objected that the intersection of two Lines might, once in a blue moon, be a Line (because they're the same line), this seems like it would edge towards the factory solution that Ian Kelly suggested (i.e. not "return self.intersection_kind(...)", but "return self.factory.create_point(...)" or something similar). Is that so? -Spencer -- http://mail.python.org/mailman/listinfo/python-list