I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response? Answer A view can directly return a Python dict or list and Flask will call jsonify automatically. For older Flask…
How can I create an encrypted django field that converts data when it’s retrieved from the database?
I have a custom EncryptedCharField, which I want to basically appear as a CharField when interfacing UI, but before storing/retrieving in the DB it encrypts/decrypts it. The custom fields documentation says to: add __metaclass__ = models.SubfieldBase override to_python to convert the data from it’s raw …
unable to call firefox from selenium in python on AWS machine
I am trying to use selenium from python to scrape some dynamics pages with javascript. However, I cannot call firefox after I followed the instruction of selenium on the pypi page(http://pypi.python.org/pypi/selenium). I installed firefox on AWS ubuntu 12.04. The error message I got is: I did search on the we…
Python – Flatten a dict of lists into unique values?
I have a dict of lists in python: I want to turn it into a list of the unique values I wrote a manual solution, but it’s a manual solution. I know there are more concise and more elegant way to do this with list comprehensions, map/reduce , itertools , etc. anyone have a clue ? Answer Double set compreh…
Check if variable is defined with Numpy array?
Sometimes I have a situation where I want to test whether a variable is 0 or None or not. In pure Python, this is simply but when foo is possibly a Numpy object (such as numpy.ndarray), this does not work anymore and I get the error: and in this case I want a.any(), however this fails on non-iterable objects.…
Is there a short contains function for lists?
Given a list xs and a value item, how can I check whether xs contains item (i.e., if any of the elements of xs is equal to item)? Is there something like xs.contains(item)? For performance considerations, see Fastest way to check if a value exists in a list. Answer Use: Also, inverse operation: It works fine …
Nohup is not writing log to output file
I am using the following command to run a python script in the background: But it appears that nohup is not writing anything to the log file. cmd.log is created but is always empty. In the python script, I am using sys.stdout.write instead of print to print to standard output. Am I doing anything wrong? Answe…
Displaying Matplotlib Navigation Toolbar in Tkinter via grid
I’m developing a small Tkinter GUI to draw matplotlib-plots. (It contains a few Entries and assembles the plot according to their content.) I have designed my plotting widget according to http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html, only I use grid instead of pack: That part work…
How to check if the object has property in view in Django?
I am trying to get the documents property in a general function, but a few models may not have the documents attribute. Is there any way to first check if a model has the documents property, and then conditionally run code? Answer You can use hasattr() to check to see if model has the documents property. Howe…
Reliable way to get the “build” directory from within setup.py
Inside the setup.py script I need to create some temporary files for the installation. The natural place to put them would be the “build/” directory. Is there a way to retrieve its path that works if installing via pypi, from source, easy_install, pip, …? Thanks a lot! Answer By default dist…