Is it possible to get the current user in a model serializer? I’d like to do so without having to branch away from generics, as it’s an otherwise simple task that must be done. My model: My serializer: And my view: How can I get the model returned, with an additional field user such that my respon…
Tag: python
How can you slice with string keys instead of integers on a python OrderedDict?
Since an OrderedDict has the features of both a list (with ordered elements), and a dictionary (with keys instead of indexes), it would seem natural that you could slice using keys. What’s interesting about this is that it’s not the error you’d see due to OrderedDictionary.__getslice__ not b…
How to pass arguments from vimscript functions to python interface?
For example, processing positional arguments: or the … list: Is there anyway I can do this? Answer You can retrieve the function arguments just like any other Vimscript expression through vim.eval():
How to replace negative numbers in Pandas Data Frame by zero
I would like to know if there is someway of replacing all DataFrame negative numbers by zeros? Answer If all your columns are numeric, you can use boolean indexing: For the more general case, this answer shows the private method _get_numeric_data: With timedelta type, boolean indexing seems to work on separat…
error installing nltk supporting packages : nltk.download()
I have installed the nltk package. Following that I am trying to download the supporting packages using nltk.download() and am getting error: [Errno 11001] getaddrinfo My machine / software details are: OS: Windows 8.1 Python: 3.3.4 NLTK Package: 3.0 Below are the commands run in python: It looks like it is g…
How to save a list to a file and read it as a list type?
Say I have the list score = [1,2,3,4,5] and it gets changed while my program is running. How could I save it to a file so that next time the program is run I can access the changed list as a list type? I have tried: But this results in the elements in the list being strings not integers. Answer
Sending MIDI messages using Python (on Ubuntu)
I am trying to send a basic MIDI message to a synthesizer, using Python. I know that the PC-Synthesizer link is functional because the Rosegarden application can be configured to play MIDI files on the device, when the MIDI output is set to ‘DigitalKBD 20:0’ port. I have discovered this Python lib…
Hide traceback unless a debug flag is set
What is the idiomatic python way to hide traceback errors unless a verbose or debug flag is set? Example code: Existing output now, but only desired when called with foo.py –debug: Desired normal output: Here’s a test script: https://gist.github.com/maphew/e3a75c147cca98019cd8 Answer The short way…
How to convert bytearray with non-ASCII bytes to string in python?
I don’t know how to convert Python’s bitarray to string if it contains non-ASCII bytes. Example: In my example, I just want to somehow get a string ‘x9f’ back from the bytearray. Is that possible? Answer In Python 2, just pass it to str(): In Python 3, you’d want to convert it ba…
GtkWarning: could not open display
I am trying to run a spider on a vps (using scrapyjs which uses python-gtk2). On running the spider I am getting the error How do I run this in a headless setup? Answer First of all, you didn’t specify if you have a desktop environment (or X) installed on your server? Regardless of that, you can achieve…