How can I check for NaN values?
Use math.isnan: >>> import math >>> x = float(‘nan’) >>> math.isnan(x) True
Use math.isnan: >>> import math >>> x = float(‘nan’) >>> math.isnan(x) True
You need to read the Python Unicode HOWTO. This error is the very first example. Basically, stop using str to convert from unicode to encoded text / bytes. Instead, properly use .encode() to encode the string: p.agent_info = u’ ‘.join((agent_contact, agent_telno)).encode(‘utf-8’).strip() or work entirely in unicode.
Since this question was asked in 2010, there has been real simplification in how to do simple multithreading with Python with map and pool. The code below comes from an article/blog post that you should definitely check out (no affiliation) – Parallelism in one line: A Better Model for Day to Day Threading Tasks. I’ll … Read more
Use a Metaclass I would recommend Method #2, but you’re better off using a metaclass than a base class. Here is a sample implementation: class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] class Logger(object): __metaclass__ = Singleton Or in Python3 class Logger(metaclass=Singleton): … Read more
To summarize the contents of other (already good!) answers, isinstance caters for inheritance (an instance of a derived class is an instance of a base class, too), while checking for equality of type does not (it demands identity of types and rejects instances of subtypes, AKA subclasses). Normally, in Python, you want your code to … Read more
You can totally do that. It’s just an ordering issue: [f(x) if x is not None else ” for x in xs] In general, [f(x) if condition else g(x) for x in sequence] And, for list comprehensions with if conditions only, [f(x) for x in sequence if condition] Note that this actually uses a different … Read more
Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py explicitly exports the symbols bar and baz: __all__ = … Read more
Getting the name of the file without the extension: import os print(os.path.splitext(“/path/to/some/file.txt”)[0]) Prints: /path/to/some/file Documentation for os.path.splitext. Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example: import os print(os.path.splitext(“/path/to/some/file.txt.zip.asc”)[0]) Prints: /path/to/some/file.txt.zip See other answers below if you need to handle that case.
Use a dict comprehension (Python 2.7 and later): {key: value for (key, value) in iterable} Alternatively for simpler cases or earlier version of Python, use the dict constructor, e.g.: pairs = [(‘a’, 1), (‘b’, 2)] dict(pairs) #=> {‘a’: 1, ‘b’: 2} dict([(k, v+1) for k, v in pairs]) #=> {‘a’: 2, ‘b’: 3} Given separate … Read more
NOTE: For Python 3.3+, see The Aelfinn’s answer below. Use the –python (or short -p) option when creating a virtualenv instance to specify the Python executable you want to use, e.g.: virtualenv –python=”/usr/bin/python2.6″ “/path/to/new/virtualenv/”