python-2.5

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

99封情书 提交于 2019-11-27 02:55:59
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. Here's a python 3 script that returns a tuple containing an image height and width for .png, .gif and .jpeg without using any external libraries (ie what Kurt McKee referenced above). Should be relatively easy to transfer it to

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

僤鯓⒐⒋嵵緔 提交于 2019-11-27 01:13:22
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. You can use asynchronous IO to do this. requests + gevent = grequests GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily. import

Test a string if it's Unicode, which UTF standard is and get its length in bytes?

独自空忆成欢 提交于 2019-11-27 00:57:17
问题 I need to test if a string is Unicode, and then if it whether it's UTF-8. After that, get the string's length in bytes including the BOM, if it ever uses that. How can this be done in Python? Also for didactic purposes, what does a byte list representation of a UTF-8 string look like? I am curious how a UTF-8 string is represented in Python. Latter edit: pprint does that pretty well. 回答1: try: string.decode('utf-8') print "string is UTF-8, length %d bytes" % len(string) except UnicodeError:

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

我们两清 提交于 2019-11-27 00:56:32
问题 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 I need to write code that works with Python 2.5+ So if isinstance(var, Numbers.number) is not a solution. Which would be the proper solution in this case? 回答1: In Python 2, you can use the types module: >>> import types >>> var = 1 >>> NumberTypes = (types

Chinese and Japanese character support in python

守給你的承諾、 提交于 2019-11-26 23:11:49
问题 How to read correctly japanese and chinese characters. I'm using python 2.5. Output is displayed as "E:\Test\?????????" path = r"E:\Test\は最高のプログラマ" t = path.encode() print t u = path.decode() print u t = path.encode("utf-8") print t t = path.decode("utf-8") print t 回答1: Please do read the Python Unicode HOWTO; it explains how to process and include non-ASCII text in your Python code. If you want to include Japanese text literals in your code, you have several options: Use unicode literals

How to: Macports select python

元气小坏坏 提交于 2019-11-26 22:06:03
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 that to version 2.5? Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> Why this happens MacPorts installs binaries into /opt/local by default

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

﹥>﹥吖頭↗ 提交于 2019-11-26 21:30:15
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 super in the parent classes the correct order. Consider this example code: #!/usr/bin/python class A

Subclassing int in Python

被刻印的时光 ゝ 提交于 2019-11-26 20:29:11
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 int is immutable so you can't modify it after it is created, use __new__ instead class TestClass(int): def _

How do I zip the contents of a folder using python (version 2.5)?

こ雲淡風輕ζ 提交于 2019-11-26 15:28:28
问题 Once I have all the files I require in a particular folder, I would like my python script to zip the folder contents. Is this possible? And how could I go about doing it? 回答1: Adapted version of the script is: #!/usr/bin/env python from __future__ import with_statement from contextlib import closing from zipfile import ZipFile, ZIP_DEFLATED import os def zipdir(basedir, archivename): assert os.path.isdir(basedir) with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z: for root, dirs,

Ultimate answer to relative python imports

六月ゝ 毕业季﹏ 提交于 2019-11-26 14:08:36
问题 I know that there are lots of questions about the same import issues in Python but it seems that nobody managed to provide a clear example of correct usage. Let's say that we have a package mypackage with two modules foo and bar . Inside foo we need to be able to access bar . Because we are still developing it, mypackage is not in sys.path . We want to be able to: import mypackage.foo run foo.py as a script and execute the sample usage or tests from the __main__ section. use Python 2.5 How do