I’m trying to use substitutions in a parsed literal block in my Sphinx documentation like this: Which gets rendered like this: Where what I want is this: If I add spaces around the substitution in the source, I get this: So I know release is defined as I expect. How can I get rid of the spaces? Answer T…
Tag: python
Using Python to add a list of files into a zip file
I want to write a script to add all the ‘.py’ files into a zip file. Here is what I have: However it gives an error: so seems the file names given is not correct. Answer You need to pass in the compression type as a keyword argument: Without the keyword argument, you are giving ZipFile.write() an integer arcn…
Custom transformer for sklearn Pipeline that alters both X and y
I want to create my own transformer for use with the sklearn Pipeline. I am creating a class that implements both fit and transform methods. The purpose of the transformer will be to remove rows from the matrix that have more than a specified number of NaNs. The issue I am facing is how can I change both the …
How to get the Signal-to-Noise-Ratio from an image in Python?
I am filtering an image and I would like to know the SNR. I tried with the scipy function scipy.stats.signaltonoise() but I get an array of numbers and I don’t really know what I am getting. Is there any other way to get te SNR of my image? Answer UPDATE: (for those who don’t read linked material …
OpenCV Python: cv2.findContours – ValueError: too many values to unpack
I’m writing an opencv program and I found a script on another stackoverflow question: Computer Vision: Masking a human hand When I run the scripted answer, I get the following error: The code: Any help is appreciated! Answer I got the answer from the OpenCV Stack Exchange site. Answer THE ANSWER: I bet …
Print not showing in ipython notebook
I am using ipython notebook (http://ipython.org/notebook.html) to do a demo and it seems like the print function is not working: The commands in the above picture are rewritten here for clarity. In short, there’s no print output from the [2]. Does anyone know whether it’s a known bug? And does any…
Python3: JSON POST Request WITHOUT requests library
I want to send JSON encoded data to a server using only native Python libraries. I love requests but I simply can’t use it because I can’t use it on the machine which runs the script. I need to do it without. My server is a local WAMP server. I always get an urllib.error.HTTPError: HTTP Error 500:…
Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?
What’s the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data? Question: Will an OrderedDict preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc. passed at the time of initialization (2nd & 3rd example above…
ZipFile does not store any text inside an archived file
The following code results in the file found in the zip being empty, instead of having some text in it: Answer Add flush to the file object: This problem also affects normal (non-temporary) files, so they’ll also need the flush treatment: Alternatively, de-denting the 2nd with block would avoid having t…
Print raw HTTP request in Flask or WSGI
I am debugging a microcontroller I’ve built which is writing raw HTTP requests line by line. I am using Flask for my backend and I would like to see the entire request as it appears in this format: I know Flask is based on WSGI. Is there anyway to get this to work with Flask? Answer With flask you have