I’m trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I’m using python 3.4.0. Following is the project outline: Write a function named collatz() that has one parameter named number. If the number is even, t…
Tag: python
Read in all csv files from a directory using Python
I hope this is not trivial but I am wondering the following: If I have a specific folder with n csv files, how could I iteratively read all of them, one at a time, and perform some calculations on their values? For a single file, for example, I do something like this and perform some calculations on the x arr…
Viewing .npy images
How can I view images stored with a .npy extension and save my own files in that format? Answer .npy is the file extension for numpy arrays – you can read them using numpy.load: One of the easiest ways to view them is using matplotlib’s imshow function: You could also use PIL or pillow: These func…
How to convert datetime object to milliseconds
I am parsing datetime values as follows: How can I convert this datetime objects to milliseconds? I didn’t see mention of milliseconds in the doc of to_datetime. Update (Based on feedback): This is the current version of the code that provides error TypeError: Cannot convert input to Timestamp. The colu…
Django Rest Framework Database Error Exception Handling
Is there any way to have Django Rest Framework automatically respond with HTTP_400_STATUS’s when there are database exceptions? (IntegrityError and so on) Example: I have a model with a unique username field and I’m trying to use a generic rest_framework.ListCreateAPIView. HTTP_400_STATUS’s …
Launch program with python on a specific core
there is any way to launch program by its API using python and run it on a specified core? i need to launch a cpu-expensive application about 5 times and run it on different core in order to save time. i’m using windows Answer The process can set its own affinity, here is what I do (you can change the
How to remove string value from column in pandas dataframe
I am trying to write some code that splits a string in a dataframe column at comma (so it becomes a list) and removes a certain string from that list if it is present. after removing the unwanted string I want to join the list elements again at comma. My dataframe looks like this: So basically my goal is to
Problem of “Memory limit exceeded” and “Time limit exceeded” in Python
I have a problem to solve on the online judge. Its solution is to make the sum of the integer for example input 4, so 1+2+3+4 the output 10. For another example, the input is 10, so get sum of 1 to 10 to get in the output 55 and so on. But when I used a list to store
Django change database field from integer to CharField
I have a Django app with a populated (Postgres) database that has an integer field that I need to change to a CharField. I need to start storing data with leading zeros in this field. If I run migrate (Django 1.8.4), I get the following error: I tried searching Google, but didn’t really find much help. …
Django Model Field Uniqueness Validation
I have a django user model that has fields ID, PW and DeviceID. The database I’m using is MySQL. In this model a user can have many different devices, so there can be many identical ID & PW (same user), with different DeviceIDs in the table. But if all the 3 fields are the same, then it is considere…