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.
calculating a gps coordinate given a point, bearing and distance
I have a problem which draws my back in some project for some time now. I’m basically looking to trap a polygon using x, y points drawn by some script I’ve written. lat, lon are the center GPS cords of the polygon and I’m looking for its surrounding polygon. here is a part of my code in python: my input
How do I get a value of datetime.today() in Python that is “timezone aware”?
I am trying to subtract one date value from the value of datetime.datetime.today() to calculate how long ago something was. But it complains: The return value from datetime.datetime.today() doesn’t seem to be “timezone aware”, while my other date value is. How do I get a return value from datetime.datetime.today() that is timezone aware? The ideal solution would be for it
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
Comparing two dictionaries and checking how many (key, value) pairs are equal
I have two dictionaries, but for simplification, I will take these two: Now, I want to compare whether each key, value pair in x has the same corresponding value in y. So I wrote this: And it works since a tuple is returned and then compared for equality. My questions: Is this correct? Is there a better way to do
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()