My code is simply as follows: When I run this program, a Windows error is raised: can’t find the file specified. I found out the problem has to with the whitespace in the middle of “First Version”. How could I find a way to circumvent the problem? P.S.: what if the variable ‘file’…
Tag: python
Changing PyScripter to work with different Python Versions
I’m using PyScripter for writing python programs and I can’t figure out how to make it work with Python26 on my computer. When I installed ArcGIS, by default it installs Python26. I then installed PyScripter which found that installation and it worked fine. Later, I installed Python27, and subsequ…
Using the __call__ method of a metaclass instead of __new__?
When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. [Editor’s note: This was remov…
In django, how to delete all related objects when deleting a certain type of instances?
I first tried to override the delete() method but that doesn’t work for QuerySet’s bulk delete method. It should be related to pre_delete signal but I can’t figure it out. My code is as following: But this method seems to be called infinitely and the program runs into a dead loop. Can someon…
Python: how to compute date ranges from a list of dates?
I have a list of dates, for example: How do I find the contiguous date ranges contained within those dates? In the above example, the ranges should be: Thanks. Answer This works, but I’m not happy with it, will work on a cleaner solution an edit the answer. Done, here is a clean, working solution: And t…
How to pass arguments to a Button command in Tkinter?
Suppose I have the following Button made with Tkinter in Python: The method action is called when I press the button, but what if I wanted to pass some arguments to the method action? I have tried with the following code: This just invokes the method immediately, and pressing the button does nothing. See Pyth…
In python, why use logging instead of print?
For simple debugging in a complex project is there a reason to use the python logger instead of print? What about other use-cases? Is there an accepted best use-case for each (especially when you’re only looking for stdout)? I’ve always heard that this is a “best practice” but I haven&…
Cannot Import GST in Python
I’m in a quandary. I’m following the following tutorial (specifically, 6.4.2) http://majorsilence.com/pygtk_audio_and_video_playback_gstreamer I’m running Python 2.7, PyGTK 2.24, and GStreamer WinBuilds 10.7 (the main installer and the SDK). When I try and compile the program, I get the foll…
Comparable classes in Python 3
What is the standard way of making a class comparable in Python 3? (For example, by id.) Answer To make classes comparable, you only need to implement __lt__ and decorate the class with functools.total_ordering. You should also provide an __eq__ method if possible. This provides the rest of the comparison ope…
How to get the return value from a thread?
The function foo below returns a string ‘foo’. How can I get the value ‘foo’ which is returned from the thread’s target? The “one obvious way to do it”, shown above, doesn’t work: thread.join() returned None. Answer In Python 3.2+, stdlib concurrent.futures modu…