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:
Tag: python
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 …
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 pyth…
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 da…
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:WINDOWS…
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 t…
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 M…
PyQt: removing QTreeView columns
I am using QTreeView with QFileSystemModel. It displays columns like Size, Type, Modification Date, which I don’t need. How can I remove them from the view? I can’t find any removeColumn in model or in view. Answer Get the QHeaderView of your TreeView by calling header() on it, the headerview know…
light weight template engine for python
Which is the simplest and light weight html templating engine in Python which I can use to generate customized email newsletters. Answer For a really minor templating task, Python itself isn’t that bad. Example: In this sense, you can use string formatting in Python for simple templating. That’s a…