Skip to content
Advertisement

Get selected subcommand with argparse

When I use subcommands with python argparse, I can get the selected arguments. So args doesn’t contain ‘foo’. Simply writing sys.argv[1] doesn’t work because of the possible global args. How can I get the subcommand itself? Answer The very bottom of the Python docs on argparse sub-commands explains how to do this: You can also use the set_defaults() method referenced

about Speed: Python VS Java

Just curious about speed of Python and Java.. Intuitively, Python should be much slower than java, but I want to know more…Could anybody give me more? or introduce some nice post to read? Answer The current standard implementation of Python (CPython) is slower than Java because the standard CPython implementation doesn’t have a powerful JIT compiler. Yet. There have been

Get legend as a separate picture in Matplotlib

I’m developing a Web application and want to display a figure and its legend in different locations on the page. Which means I need to save the legend as a separate png file. Is this possible in Matplotlib in a more or less straightforward way? Answer This could work:

create zip of complete directory using zipfile python module

Above is the code that I am using, and here “source” is the path of the directory. But when I run this code it just zips the source folder and not the files and and folders contained in it. I want it to compress the source folder recursively. Using tarfile module I can do this without passing any additional information.

Python virtualenv questions

I’m using VirtualEnv on Windows XP. I’m wondering if I have my brain wrapped around it correctly: I ran virtualenv ENV and it created C:WINDOWSsystem32ENV. I then changed my PATH variable to include C:WINDOWSsystem32ENVScripts instead of C:Python27Scripts. Then, I checked out Django into C:WINDOWSsystem32ENVLibsite-packagesdjango-trunk, updated my PYTHON_PATH variable to point the new Django directory, and continued to easy_install other things

Check if module exists, if not install it

I want to check if a module exists, if it doesn’t I want to install it. How should I do this? So far I have this code which correctly prints f if the module doesn’t exist. Answer Here is how it should be done, and if I am wrong, please correct me. However, Noufal seems to confirm it in another

Trying to mock datetime.date.today(), but not working

Can anyone tell me why this isn’t working? Perhaps someone could suggest a better way? Answer There are a few problems. First of all, the way you’re using mock.patch isn’t quite right. When used as a decorator, it replaces the given function/class (in this case, datetime.date.today) with a Mock object only within the decorated function. So, only within your today()

Advertisement