I need to determine if a given Python variable is an instance of native type: str, int, float, bool, list, dict and so on. Is there elegant way to doing it? Or is this the only way: Answer The best way to achieve this is to collect the types in a list of tuple called primitiveTypes and: The types module
Tag: python
sqlalchemy flush() and get inserted id?
I want to do something like this: But f.id is None when I try it. How can I get this to work? Answer Your sample code should have worked as it is. SQLAlchemy should be providing a value for f.id, assuming its an autogenerating primary-key column. Primary-key attributes are populated immediately within the flu…
What is the most efficient string concatenation method in Python?
Is there an efficient mass string concatenation method in Python (like StringBuilder in C# or StringBuffer in Java)? I found following methods here: Simple concatenation using + Using a string list and the join method Using UserString from the MutableString module Using a character array and the array module …
How to convert a nested Python dict to object?
I’m searching for an elegant way to get data using attribute access on a dict with some nested dicts and lists (i.e. javascript-style object syntax). For example: Should be accessible in this way: I think, this is not possible without recursion, but what would be a nice way to get an object style for di…
Why does integer division yield a float instead of another integer?
Consider this division in Python 3: Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast? In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding do…
Making sure that psycopg2 database connection alive
I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won’t work with OperationalError exception. So I’m looking for any reliable method to “ping” the datab…
Can’t import Numpy in Python
I’m trying to write some code that uses Numpy. However, I can’t import it: I tried the suggestions in this question: and I searched for files named numpy in that path: But nothing came up. So… Are there any other places in which Python modules are commonly installed? How can I install numpy …
How can I force division to be floating point? Division keeps rounding down to 0?
I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I’ll always get 0 with a remainder of a. How can I force c to be a floating point number in Python 2 in
How can I check if a string represents an int, without using try/except?
Is there any way to tell whether a string represents an integer (e.g., ‘3’, ‘-17’ but not ‘3.14’ or ‘asfasfas’) Without using a try/except mechanism? Answer If you’re really just annoyed at using try/excepts all over the place, please just write a helper f…
Is it safe to replace a self object by another object of the same type in a method?
I would like to replace an object instance by another instance inside a method like this: The object is retrieved from a database. Answer It is unlikely that replacing the ‘self’ variable will accomplish whatever you’re trying to do, that couldn’t just be accomplished by storing the re…