I’ve always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems: It’s not completely readable; if value is None is surely more understandable. This can have implications later (and cause subtle bu…
Tag: python
Subclass dict: UserDict, dict or ABC?
What’s the difference between UserDict, dict and ABC and which one is recommended? The docs seem to deprecate UserDict? Also it seems UserDict’s update() would use my setitem method whereas dict doesn’t? Which methods are really essential to override given I want custom setitem and getitem f…
Build the full path filename in Python
I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string? The directory may or may not exist at the time of call. For example: I need to create a string ‘/home/me/dev/my_reports/daily_report.pdf’ Concatenating the piec…
How to Query model where name contains any word in python list?
Aim to Achieve: I want all objects where name attribute contains any word from the list. I have: For example: if name=”this is word2″: Then object with such a name should be returned since word2 is in the list. Please help! Answer You could use Q objects to constuct a query like this: Edit: is a f…
Make Sqlalchemy Use Date In Filter Using Postgresql
I’m trying to perform the following query in Sqlalchemy. I have tried several methods. Some here on SO. But none seems “realistic” since some do Casts Other String Formating ?!?! and not a plain simple Date() ( By Date i Mean the Postgresql Date not the Python One) at the ORM Field. Is it po…
how to substract two datetime.time values in django template,and how to format a duration as hour,minutes
In a django app ,I am sending a list of Entry objects to the template.Each Entry object has a start, end times which are datetime.time values(from TimeFields on the form).While listing the Entry objects,I need to show the duration for each entry.Putting a duration field in model seemed to be reduntant since ,…
Write image to Windows clipboard in python with PIL and win32clipboard?
I’m trying to open an image file and copy the image to the Windows clipboard. Is there a way to fix this: I could install PythonMagick, etc., but would prefer not installing yet another library for a one-off program Answer
PIL’s colour space conversion YCbCr -> RGB
The algorithm used by PIL v1.1.7 gives ‘washed out’ looking results. When converting the same source data using ffmpeg it looks correct. Using mplayer gives identical results to ffmpeg (perhaps they use the same library underneath). This leads me to believe PIL may be stuffing up the colour space …
Is it reasonable to use None as a dictionary key in Python?
None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works: The actual data I am working with is educational standards. Every standard is associated with a content area. Some standards are also associated with content subareas. I would like to m…
ctypes variable length structures
Ever since I read Dave Beazley’s post on binary I/O handling (http://dabeaz.blogspot.com/2009/08/python-binary-io-handling.html) I’ve wanted to create a Python library for a certain wire protocol. However, I can’t find the best solution for variable length structures. Here’s what I wan…