For Linux this would give me /, for Windows on the C drive that would give me C:\. Note that python is not necessarily installed on the C drive on windows. Answer You can get the path to the Python executable using sys.executable: Then, for Windows, the drive letter will be the first part of splitdrive:
Tag: python
Using python to create an average out of a list of times
I have a huge list of times (HH:MM:SS) and I know that if I wanted to create an average I could separate the Hours, Seconds, and Minutes and average each one and then concatenate them back together. However I feel that there must be a better way to do that. Does anyone know of a better way to do this?
Overriding in Python
I want to be able to do the following I would like to get a parent function from a child class in a function that is overriding it. I am not sure how to do this. This is a little hard to explain, comment if you are having trouble understanding. Edit: Thanks for the answers everyone, I almost thought that
Generate list of numbers in specific format
I need to generate a list of numbers in a specific format. The format is I know how to generate a normal list of numbers using range So, is there any built-in way in python to generate a list of numbers in the specified format. Answer Python string formatting allows you to specify a precision: Precision (opti…
Determine if Django is running under the development server
Is there a way to determine if Django is running on localhost and setting the DEBUG variable in settings.py accordingly. So that if I run the server locally it will set DEBUG to True and otherwise set it to False. Localhost: python manage.py runserver Not localhost: python manage.py runserver 0.0.0.0:8000 Ans…
How to get month and year from Date field in sqlalchemy?
The result should be Date object Since the day cannot be ‘removed’, set it to say, 1st day of the month. Leaving only Month, Year Answer You can use following constructs to filter the Date column using either year or month: And group_by is also possible: Now I haven’t tested it, but I assume…
Python – appending to same file from multiple threads
I’m writing an app that appends lines to the same file from multiple threads. I have a problem in which some lines are appended without a new line. Any solution for this? Answer The solution is to write to the file in one thread only.
matplotlib log scales causes missing points
I’m having a really strange issue with matplotlib. Plotting some points looks like this: When I switch to a log scale on the y-axis, some of the points are not connected: Is this a bug? Am I missing something? Code is below. Comment out the log scale line to see the first graph. Answer log(0) is undefin…
Order a list of numbers without built-in sort, min, max function
If I have a list that varies in length each time and I want to sort it from lowest to highest, how would I do that? If I have: [-5, -23, 5, 0, 23, -6, 23, 67] I want: [-23, -6, -5, 0, 5, 23, 23, 67] I start with this: BUT this only goes through once and I get:
Django:any way to remove this clear field?
Is there any way I can remove this model form file field’s ‘clear’ checkbox?.I know that I can define the ‘custom’ widget for file field but how to address this checkbox in that? Answer You need to change the widget from the ClearableFileInput to Fileinput https://docs.djangoproj…