This question already has answers here: Check if a given key already exists in a dictionary (16 answers) Closed last month. Given: Which of the following is the best way to check if ‘a’ is in d? Answer in is definitely more pythonic. In fact has_key() was removed in Python 3.x.
In Python, how to check if a string only contains certain characters?
In Python, how to check if a string only contains certain characters? I need to check a string containing only a..z, 0..9, and . (period) and no other character. I could iterate over each character and check the character is a..z or 0..9, or . but that would be slow. I am not clear now how to do it with
Determine if Python variable is an instance of a built-in type
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
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 flush() process as they are generated, and
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 Using cStringIO from the StringIO module What should be used and
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 dicts? Answer Update: In Python 2.6
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 down to 0? for the opposite, 2.x-specific
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 database and know that connection is alive. I’ve checked a psycopg2 documentation but can’t
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 locally in my account, if it turns
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