I am able to parse strings containing date/time with time.strptime How can I parse a time string that contains milliseconds? Answer Python 2.6 added a new strftime/strptime macro %f. The docs are a bit misleading as they only mention microseconds, but %f actually parses any decimal fraction of seconds with up…
Tag: python
How do you determine if an IP address is private, in Python?
In Python, what is the best way to determine if an IP address (e.g., ‘127.0.0.1’ or ‘10.98.76.6’) is on a private network? The code does not sound difficult to write. But there may be more edge cases than are immediately apparent, and there’s IPv6 support to consider, etc. Is the…
Python and MySQL: is there an alternative to MySQLdb?
Is there a module written purely in Python that will allow a script to communicate with a MySQL database? I’ve already tried MySQLdb without success. It requires too much: GCC, zlib, and openssl. I do not have access to these tools; even if I did, I don’t want to waste time getting them to work to…
How can I convert a dictionary into a list of tuples?
If I have a dictionary like: How can I convert it to this? And how can I convert it to this? Answer For Python 3.6 and later, the order of the list is what you would expect. In Python 2, you don’t need list.
How to create a class that doesn’t re-create an object with identical input parameters
I am trying to create a class that doesn’t re-create an object with the same input parameters. When I try to instantiate a class with the same parameters that were used to create an already-existing object, I just want my new class to return a pointer to the already-created (expensively-created) object.…
Django form – set label
I have a form that inherits from 2 other forms. In my form, I want to change the label of a field that was defined in one of the parent forms. Does anyone know how this can be done? I’m trying to do it in my __init__, but it throws an error saying that “‘RegistrationFormTOS’ object has…
Best Python templating library to facilitate code generation [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 6 years ago. Improve this ques…
How to remove the left part of a string?
I have some simple python code that searches files for a string e.g. path=c:path, where the c:path part may vary. The current code is: What is a simple way to get the text after Path=? Answer Starting in Python 3.9, you can use removeprefix:
Simple prime number generator in Python
Could someone please tell me what I’m doing wrong with this code? It is just printing ‘count’ anyway. I just want a very simple prime generator (nothing fancy). Answer There are some problems: Why do you print out count when it didn’t divide by x? It doesn’t mean it’s prime…
Subtracting 2 lists in Python
Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like Should I use tuples? If none of them defines these operands on these types, can I define it instead? If not, should I create a new vector3 class? Answer If this is something you end up doing f…