I am trying to get python to print a hex string to the screen and not convert it to the ascii character. Is there a way to print this to the screen as ‘x5ex2ex6d’ instead of ‘^.m’ Any help will be greatly appreciated. Answer b’x5ex2ex6d’ and b’^.m’ are identical…
Tag: python
How to delete the first line of a text file?
I have been searching online, but have not found any good solution. Here is my text file: All I want is to delete the first line (which means using the second line as the first line, not filling first line with whitespace). Could anyone please help me with that? Answer Assuming you have enough memory to hold …
PostgreSQL ILIKE query with SQLAlchemy
I’d like to run a query that selects all posts, case insensitive, that have titles that match ‘%’ + [some_phrase] + ‘%’. That is, select all rows that have titles that contain some phrase, case insensitive. From the research I’ve done, it looks like I need to use Postgres&#…
How do I call a function from another .py file? [duplicate]
This question already has answers here: How do I import other Python files? (23 answers) Closed 6 months ago. file.py contains a function named function. How do I import it? The above gives an error: ImportError: No module named ‘file.py’; file is not a package Answer First, import function from f…
How to pad a string to a fixed length with spaces in Python?
I’m sure this is covered in plenty of places, but I don’t know the exact name of the action I’m trying to do so I can’t really look it up. I’ve been reading an official Python book for 30 minutes trying to find out how to do this. Problem: I need to put a string in a certain leng…
Why is Python’s ‘len’ function faster than the __len__ method?
In Python, len is a function to get the length of a collection by calling an object’s __len__ method: So I would expect direct call of __len__() to be at least as fast as len(). Demo link But results of testing with the above code shows len() to be faster. Why? Answer The builtin len() function does not…
Python ASCII plots in terminal
With Octave I am able to plot arrays to the terminal, for example, plotting an array with values for the function x^2 gives this output in my terminal: Is there some way I can do something similar in Python, specifically with matplotlib? bashplotlib seems to offer some of this functionality but appears to be …
how to execute multiple tests on multiple items with py.test
I’m a novice in python and also in py.test. I’m searching a way to run multiple tests on multiple items and cannot find it. I’m sure it’s quite simple when you know how to do it. I have simplified what I’m trying to do to make it simple to understand. If I have a Test class who d…
Python: porting to cvxopt quadratic programming from MATLAB’s quadprog
I am porting a MATLAB script to Python that uses the quadprog function. The best equivalent seems to be qp. I can work out the equivalent notation with the doc and a bit of time, but I’m lazy and it would be nice to just copy-paste some code instead of thinking about it. So: Which of the parameters in t…
Nginx is throwing an 403 Forbidden on Static Files
I have a django app, python 2.7 with gunicorn and nginx. Nginx is throwing a 403 Forbidden Error, if I try to view anything in my static folder @: nginx config(/etc/nginx/sites-enabled/myapp) contains: error.log contains: access.log contains I tried just viewing say a .css file in /static/ and it throws an er…