PyCharm is showing me that some code is unreachable within a method before the return statement is reached. I cannot help but wonder how is that even remotely possible? self.fail simply calls self.response.write(things). Update: Yeah, when I surround it with a try/catch clause, the issue is resolved… St…
Tag: python
Convert Django Model object to dict with all of the fields intact
How does one convert a django Model object to a dict with all of its fields? All ideally includes foreign keys and fields with editable=False. Let me elaborate. Let’s say I have a django model like the following: In the terminal, I have done the following: I want to convert this to the following diction…
Matplotlib log scale tick label number formatting
With matplotlib when a log scale is specified for an axis, the default method of labeling that axis is with numbers that are 10 to a power eg. 10^6. Is there an easy way to change all of these labels to be their full numerical representation? eg. 1, 10, 100, etc. Note that I do not know what the range
How can I simulate 2.x’s tuple unpacking for lambda parameters, using 3.x?
In Python 2, I can write: But that is not supported in 3.x: The straightforward workaround is to index explicitly into the tuple that was passed: This is very ugly. If the lambda were a function, I could do But because the lambda only supports a single expression, it’s not possible to put the x, y = p p…
Counting word frequency and making a dictionary from it
I want to take every word from a text file, and count the word frequency in a dictionary. Example: ‘this is the textfile, and it is used to take words and count’ I am not that far, but I just can’t see how to complete it. My code so far: Answer If you don’t want to use collections.Coun…
How to make uwsgi shut down if application failed to load?
I run my uwsgi in the simplest way possible, as a process managed by upstart, no emperor mode or anything. This is what I get if application fails on startup for some reason: I have no use for this mode, and would like it to shut down completely, so my upstart job shows as stopped. How can I do this?
Python ‘is’ vs JavaScript ===
The Python use of ‘is’ seems to be similar to JavaScript ‘===’ but not quite. Here they talk about exact instances: http://www.learnpython.org/en/Conditions here (for JS) they talk about “equal AND the same type.” http://www.w3schools.com/js/js_comparisons.asp SO can you ha…
How to generate pydoc documentation for Google Cloud Endpoints method?
I have a set of APIs that were developed using Google Cloud Endpoints. The API methods look something like this: I would like to use pydoc to generate documentation for the module that contains this method. However, when I do this, the docstring is not preserved due to the use of the endpoints.method decorato…
Getting started with cython on mac os
I wrote a simple program in python: Then I execute this: It was generated a file: main.c And then I tried this: And I have an error: How to compile python to c ? How to get started with cython with xcode on mac ? Answer You have to tell the gcc compiler where is the pyconfig.h file on your
Max function in python returning wrong results
I am trying to find the Max and Min in a set of numbers from a CSV file. My code keeps returning the wrong number for Max function for some rows. Here is my code: An example of my output: I am not sure what I have done wrong. Some advice would be appreciated. Answer Your list elements are strings.