python-2.5

What does from __future__ import absolute_import actually do?

What does from __future__ import absolute_import actually do? Question: I have answered a question regarding absolute imports in Python, which I thought I understood based on reading the Python 2.5 changelog and accompanying PEP. However, upon installing Python 2.5 and attempting to craft an example of properly using from __future__ import absolute_import, I realize things …

Total answers: 2

Python: avoid new line with print command

Python: avoid new line with print command Question: When I use the print command, it prints whatever I want and then goes to a different line. For example: print "this should be"; print "on the same line" Should return: this should be on the same line but instead returns: this should be on the same …

Total answers: 5

How to properly use python's isinstance() to check if a variable is a number?

How to properly use python's isinstance() to check if a variable is a number? Question: I found some old Python code that was doing something like: if type(var) is type(1): … As expected, pep8 complains about this recommending usage of isinstance(). Now, the problem is that the numbers module was added in Python 2.6 and …

Total answers: 4

string of kwargs to kwargs

string of kwargs to kwargs Question: I have a string like s = “title=’bah’ name=’john and jill’ purple=’haze’ none=None i=1” I am looking for a pythonic way of putting that into a dictionary (and a solution that does not choke on extra white spaces)? Suggestions ? Asked By: Nix || Source Answers: >>> from ast …

Total answers: 6

How to: Macports select python

How to: Macports select python Question: When I enter: port select –list python This is the result: Available versions for python: none python25 (active) python25-apple python26-apple python27 python27-apple I thought when I use python I would be using version 2.5. Instead when I enter “python”, version 2.7 seems to be active. How do I change …

Total answers: 5

Sort nested dictionary by value, and remainder by another value, in Python

Sort nested dictionary by value, and remainder by another value, in Python Question: Consider this dictionary format. {‘KEY1’:{‘name’:’google’,’date’:20100701,’downloads’:0}, ‘KEY2’:{‘name’:’chrome’,’date’:20071010,’downloads’:0}, ‘KEY3’:{‘name’:’python’,’date’:20100710,’downloads’:100}} I’d like the dictionary sorted by downloads first, and then all items with no downloads sorted by date. Obviously a dictionary cannot be sorted, I just need a sorted listed of keys I can iterate …

Total answers: 5

Print variable in python without space or newline

Print variable in python without space or newline Question: print a variable without newline or space python3 does it by print (x,end=”) how to do it in python 2.5 Asked By: san8055 || Source Answers: Easy: print x, Note comma at the end. Answered By: Vladimir Mihailenco sys.stdout.write writes (only) strings without newlines unless specified. …

Total answers: 2

Subclassing int in Python

Subclassing int in Python Question: I’m interested in subclassing the built-in int type in Python (I’m using v. 2.5), but having some trouble getting the initialization working. Here’s some example code, which should be fairly obvious. class TestClass(int): def __init__(self): int.__init__(self, 5) However, when I try to use this I get: >>> a = TestClass() …

Total answers: 2

Tell if Python is in interactive mode

Tell if Python is in interactive mode Question: In a Python script, is there any way to tell if the interpreter is in interactive mode? This would be useful so that, for instance, when you run an interactive Python session and import a module, slightly different code is executed (for example, logging is turned off). …

Total answers: 7