Coming from C/C++ background, I am aware of coding standards that apply for Safety Critical applications (like the classic trio Medical-Automotive-Aerospace) in the context of embedded systems , such as MISRA, SEI CERT, Barr etc. Skipping the question if it should or if it is applicable as a language, I want …
Python: numpy.sum returns wrong ouput (numpy version 1.21.3)
Here I have a 1D array: And the sum of all elements in the array should be 75491328*8*8 = 4831444992. However, when I use np.sum, I get a different output. That’s what happens on my Jupyter Notebook using the latest version of Numpy. But when I use Jupyter Notebook of Coursera using old version 1.18.4 o…
How do I mock a file open for a specific path in python?
So I know that in my unit test I can mock a context manager open(), i.e.: mocked with but is there a way for me to only mock it for a specific file path? Or maybe some other way to ensure that the context manager gets called with the correct path in a unit test? Answer To mock open only
Calculating the number of starts, by customer using Pandas
I have DataFrame that looks like: df I’m trying to calculate the beginning and end date for each, which I think is pretty straight forward (i.e., first time each customer customer and last time, as defined by amt > 0). What I need help with is calculating the number of new acquisitions, whether it…
How to pytest that a decorator is assigned to multiple functions
I read somewhere on SO that you should not test the decorator but the functionality of the wrapped function. Nevertheless there might be a shorter way of testing whether a certain decorator is assigned to multiple functions. I have this decorator: I have some tests to test the decorator function itself, e.g.:…
Heroku: FileNotFoundError [Errno 2] during file IO operations on deployment – Python
I have successfully deployed a webapp to Heroku. However, my file IO operations are not happening because Heroku is unable to recognize the files in the folders. My folder structure is: When I use df = pd.read_csv(‘userdata.csv’) all works fine, because this is in the root directory as the main fi…
How to change the values of instance attributes in a loop
I have a work implantation of python which has some inbuilt dialog boxes so I cannot share the original code here. The problem I have is I want to update the values of some instance attributes. I made a dictionary from zipping 2 lists together so that in a for loop I could mention to the user the string that
How to fix transparency overlaps in Matplotlib when plotting multiple figures?
I have a function that inputs a string (the name of the dataframe we’re visualizing) and returns two histograms that visualize that data. The first plot (on the left) is the raw data, the one on the right is it after being normalized (same, just plotted using the matplotlib parameter density=True). But …
Python – Ordering a dictionary with the array from file
I have been trying to fetch the last and first field from the /etc/passwd file in Linux. I want to fetch the last fields of the file(the shell which users are using) and the first field(number of users using the shell) and add it to a list. For eg my final output must be something like Basically, I want the
memory leak while adding elements from text file to dictionary in python
I am running a very simple code to read into txt files and add them to an existing dictionary. With htop I see that the used memory linearly increases until I run out of memory. Here is a simplified version of the code: I tried to delete the output and added a garbage collector in the loop and it has