I’ve got a Python script that sometimes displays images to the user. The images can, at times, be quite large, and they are reused often. Displaying them is not critical, but displaying the message associated with them is. I’ve got a function that downloads the image needed and saves it locally. R…
Is there a Java equivalent of Python’s ‘enumerate’ function?
In Python, the enumerate function allows you to iterate over a sequence of (index, value) pairs. For example: Is there any way of doing this in Java? Answer For collections that implement the List interface, you can call the listIterator() method to get a ListIterator. The iterator has (amongst others) two me…
Python: if not val, vs if val is None
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…
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 …