python-2.5

How to obtain image size using standard Python class (without using external library)?

瘦欲@ 提交于 2019-11-26 12:37:15
问题 I am using Python 2.5. And using the standard classes from Python, I want to determine the image size of a file. I\'ve heard PIL (Python Image Library), but it requires installation to work. How might I obtain an image\'s size without using any external library, just using Python 2.5\'s own modules? Note I want to support common image formats, particularly JPG and PNG. 回答1: Here's a python 3 script that returns a tuple containing an image height and width for .png, .gif and .jpeg without

Read data from CSV file and transform from string to correct data-type, including a list-of-integer column

陌路散爱 提交于 2019-11-26 11:13:33
问题 When I read data back in from a CSV file, every cell is interpreted as a string. How can I automatically convert the data I read in into the correct type? Or better: How can I tell the csv reader the correct data-type of each column? (I wrote a 2-dimensional list, where each column is of a different type (bool, str, int, list of integer), out to a CSV file.) Sample data (in CSV file): IsActive,Type,Price,States True,Cellphone,34,\"[1, 2]\" ,FlatTv,3.5,[2] False,Screen,100.23,\"[5, 1]\" True

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

谁说胖子不能爱 提交于 2019-11-26 10:57:58
问题 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 over. [\'KEY3\',\'KEY1\',\'KEY2\'] I can already sort the list by

Tell if Python is in interactive mode

蓝咒 提交于 2019-11-26 09:46:40
问题 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). I\'ve looked at tell whether python is in -i mode and tried the code there, however, that function only returns true if Python has been invoked with the -i flag and not when the command used to invoke interactive mode is python

Multiple (asynchronous) connections with urllib2 or other http library?

别等时光非礼了梦想. 提交于 2019-11-26 09:35:36
问题 I have code like this. for p in range(1,1000): result = False while result is False: ret = urllib2.Request(\'http://server/?\'+str(p)) try: result = process(urllib2.urlopen(ret).read()) except (urllib2.HTTPError, urllib2.URLError): pass results.append(result) I would like to make two or three request at the same time to accelerate this. Can I use urllib2 for this, and how? If not which other library should I use? Thanks. 回答1: You can use asynchronous IO to do this. requests + gevent =

How does Python's “super” do the right thing?

佐手、 提交于 2019-11-26 07:59:08
问题 I\'m running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandparent. I\'m familiar with Python\'s MRO; that\'s not my question. I\'m curious how the object returned from super actually manages to communicate to calls of

Subclassing int in Python

眉间皱痕 提交于 2019-11-26 07:37:58
问题 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() >>> a 0 where I\'d expect the result to be 5 . What am I doing wrong? Google, so far, hasn\'t been very helpful, but I\'m not really sure what I should be searching for

Python: avoid new line with print command [duplicate]

折月煮酒 提交于 2019-11-26 05:19:04
问题 This question already has answers here : How to print without newline or space? (21 answers) Closed 6 years ago . I\'ve started programming today and have this issue with Python. It\'s pretty dumb but I can\'t figure out how to do it. 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 line More

How to decorate a class?

三世轮回 提交于 2019-11-26 03:48:02
问题 In Python 2.5, is there a way to create a decorator that decorates a class? Specifically, I want to use a decorator to add a member to a class and change the constructor to take a value for that member. Looking for something like the following (which has a syntax error on \'class Foo:\': def getId(self): return self.__id class addID(original_class): def __init__(self, id, *args, **kws): self.__id = id self.getId = getId original_class.__init__(self, *args, **kws) @addID class Foo: def __init_

How to generate all permutations of a list in Python

孤人 提交于 2019-11-26 03:12:48
问题 How do you generate all the permutations of a list in Python, independently of the type of elements in that list? For example: permutations([]) [] permutations([1]) [1] permutations([1, 2]) [1, 2] [2, 1] permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] 回答1: Starting with Python 2.6 (and if you're on Python 3) you have a standard-library tool for this: itertools.permutations. import itertools list(itertools.permutations([1, 2, 3])) If you're using an older