I have a pretty simple Django app, that I am trying to run unit tests on. In my tests.py file I am trying to import the parent apps views file. I tried from . import views but got an error: I read that when a relative path does not work, you can try using an absolute path so I tried
Formatting output of CSV file in Python
I am creating a very rudimentary “Address Book” program in Python. I am grabbing contact data from a CSV file, the contents of which looks like the following example: I am trying to format the output so that it looks cleaner and more readable, i.e. everything lined up in rows and columns, like thi…
Pandas: convert dtype ‘object’ to int
I’ve read an SQL query into Pandas and the values are coming in as dtype ‘object’, although they are strings, dates and integers. I am able to convert the date ‘object’ to a Pandas datetime dtype, but I’m getting an error when trying to convert the string and integers. Here…
Attribute Error : none type object has no attribute ‘ shape ‘
I keep encountering an attribute error trying to read some frames from a piCamera on a raspberry pi Here is the error: Answer It seems that frame was returned as None in this line as if you camera couldn’t read an image: Then, when resizing a None object, the program blows up as we described in the erro…
How can I use “e” (Euler’s number) and power operation?
How can I write 1-e^(-value1^2/2*value2^2) in Python? I don’t know how to use power operator and e. Answer You can use exp(x) function of math library, which is same as e^x. Hence you may write your code as: I have modified the equation by replacing 1/2 as 0.5. Else for Python <2.7, we’ll have …
Accessing the dataLayer (JS variable) when scraping with python
I’m using beautiful soup to scrape a webpages. I want to access the dataLayer (a javascript variable) that is present on this webpage? How can I retrieve it using python? Answer You can parse it from the source with the help of re and json.loads to find the correct script tag that contains the json: Run…
how to add a package to sys path for testing
This question is occasioned by instructions in the python guide for adding a project to sys path to use in tests, which do not seem to work unless I am misunderstanding the instructions I have a directory structure for a python project like this According to the python guide, I should create a test/context.py…
Infinite loop while adding two integers using bitwise operations?
I am trying to solve a problem, using python code, which requires me to add two integers without the use of ‘+’ or ‘-‘ operators. I have the following code which works perfectly for two positive numbers: This piece of code works perfectly if the input is two positive integers or two ne…
Stack columns above value labels in pandas pivot table
Given a dataframe that looks like: Key1 Key2 Value1 Value2 0 one A 1.405817 1.307511 1 one B -0.037627 -0.215800 2 two C -0.116591 -1.195066 3 three A 2.044775 -1.207433 4 one B -1.109636 0.031521 5 one C -1.529597 1.761366 6 two A -1.349865 0.321454 7 three B 0.814374 2.285579 8 one C 0.178702 0.479210 9 one…
How to parse raw HTTP request in Python 3?
I am looking for a native way to parse an http request in Python 3. This question shows a way to do it in Python 2, but uses now deprecated modules, (and Python 2) and I am looking for a way to do it in Python 3. I would mainly like to just figure out what resource is requested and