python-3.2

Combining inplace filtering and the setting of encoding in the fileinput module

依然范特西╮ 提交于 2019-12-12 08:48:20
问题 I am attempting to use the fileinput module's inplace filtering feature to rewrite an input file in place. Needed to set encoding (both for read and write) to latin-1 and attempted to pass openhook=fileinput.hook_encoded('latin-1') to fileinput.input but was thwarted by the error ValueError: FileInput cannot use an opening hook in inplace mode Upon closer inspection I see that the fileinput documentation clearly states this: You cannot use inplace and openhook together How can I get around

How to override python builtins with an import statement?

我只是一个虾纸丫 提交于 2019-12-11 10:52:25
问题 I want to log to a text file whatever is print ed to sys.stdout . While I understand smarter people can come up with more elegant and Pythonic solutions, here is my solution class logger: def __init__(self, filename='log.txt'): self.logf = open(filename, 'a') global print self.__print = print print = self.lognprint def __del__(self): self.logf.close() def lognprint(self, *args, **keywords): self.__print(*args, file = self.logf, **keywords) self.__print(*args, **keywords) now if anywhere in my

Strange error with range type in list assignment

不打扰是莪最后的温柔 提交于 2019-12-10 13:44:34
问题 r = range(10) for j in range(maxj): # get ith number from r... i = randint(1,m) n = r[i] # remove it from r... r[i:i+1] = [] The traceback I am getting a strange error: r[i:i+1] = [] TypeError: 'range' object does not support item assignment Not sure why it is throwing this exception, did they change something in Python 3.2? 回答1: Good guess: they did change something. Range used to return a list, and now it returns an iterable range object, very much like the old xrange. >>> range(10) range(0

How do I gracefully include Python 3.3 from None exception syntax in a Python 3.2 program?

半腔热情 提交于 2019-12-10 09:30:14
问题 I'm trying to re-raise an exception to give the user better information about the actual error. Python 3.3 includes PEP 409. It adds the raise NewException from None syntax to suppress the context of the original exception. However, I am targeting Python 3.2. The Python script will parse, but at runtime if it encounters the from None syntax it will produce TypeError: exception causes must derive from BaseException . For example: try: regex_c = re.compile('^{}$'.format(regex)) except re.error

Why do I get “ImportError: cannot import name find_spec” when I start a new Django project?

回眸只為那壹抹淺笑 提交于 2019-12-10 01:08:25
问题 I'm learning Python in tandem with Django. I initially installed Python 3 on my machine (Debian Wheezy), but read about possible conflicts and removed it with some difficulty. Now I'm using virtualenv and installed python3 within the env and Django using pip. Django and Python seem to have installed correctly: # python -c "import django; print(django.get_version())" 1.9.1 # python -V Python 3.2.3` but when I try to start a new Django project, I get the following: # django-admin.py

Is this a python 3 file bug?

半城伤御伤魂 提交于 2019-12-08 18:52:40
Is this a bug? It demonstrates what happens when you use libtiff to extract an image from an open tiff file handle. It works in python 2.x and does not work in python 3.2.3 import os # any file will work here, since it's not actually loading the tiff # assuming it's big enough for the seek filename = "/home/kostrom/git/wiredfool-pillow/Tests/images/multipage.tiff" def test(): fp1 = open(filename, "rb") buf1 = fp1.read(8) fp1.seek(28) fp1.read(2) for x in range(16): fp1.read(12) fp1.read(4) fd = os.dup(fp1.fileno()) os.lseek(fd, 28, os.SEEK_SET) os.close(fd) # this magically fixes it: fp1.tell(

Is this a python 3 file bug?

荒凉一梦 提交于 2019-12-08 06:40:36
问题 Is this a bug? It demonstrates what happens when you use libtiff to extract an image from an open tiff file handle. It works in python 2.x and does not work in python 3.2.3 import os # any file will work here, since it's not actually loading the tiff # assuming it's big enough for the seek filename = "/home/kostrom/git/wiredfool-pillow/Tests/images/multipage.tiff" def test(): fp1 = open(filename, "rb") buf1 = fp1.read(8) fp1.seek(28) fp1.read(2) for x in range(16): fp1.read(12) fp1.read(4) fd

fatal error: 'string.h' file not found after upgrading to python 3.2 and installing certain modules from a virtualenv

你。 提交于 2019-12-08 05:31:54
问题 I'm on Mac OSX 10.8 (Mountain Lion) and have just installed Python 3.2.3. When installing pycrypto from a virtual environment using that version of python: $ virtualenv --no-site-packages -p /usr/local/bin/python3.2-32 venv $ source venv/bin/activate $ pip install pycrypto I get the error: gcc-4.2 not found, using clang instead building 'Crypto.Hash._MD2' extension warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. Compiling with an SDK that doesn't seem to exist

Object pool under memory constraints

好久不见. 提交于 2019-12-08 00:53:52
问题 We use a number of large objects. Ideally, we'd like to make all of them permanently available to client code, but they don't fit in physical memory all at once. So when we approach memory limits, we'll need to destroy some of the objects in pool (probably, we'll destroy the least-recently-used object). (Using virtual memory on disk is tantamount to a complete system freeze.) For concreteness, the memory per object varies between ~100MB and ~10GB; the total amount of RAM we have is 32GB.

can't not install Distribute, zlib

一世执手 提交于 2019-12-07 05:53:48
问题 At first, I only want to use install feedparser with python3.2, while it need Distribute. When I install Distribute with python3.2 setup.py install I got File "/usr/local/lib/python3.2/zipfile.py", line 687, in __init__ "Compression requires the (missing) zlib module") RuntimeError: Compression requires the (missing) zlib module Then I downloaded zlib and installed it with ./configure --prefix=/usr/local/python3.2 make sudo make install After the installation, and tried to install Distribute,