[Python-porting] Port of psycopg2
"Martin v. Löwis"
martin at v.loewis.de
Fri Dec 12 00:01:17 CET 2008
> It looks like I'm going to need to abandon the "one timemachine fits
> all" idea, and use customised ones: timemachine_21, ..._22_26, ..._3x.
> That leaves me with one distutils-related problem: it insists on
> compiling all included files when you do "setup.py install"; I don't
> want it to compile timemachine_21.py with Python 3.0 -- it would barf
> on "False = 0" :-) -- and vice versa. But I also don't want to have
> tarballs/installers that depend on the Python version, if possible.
I recommend to use build_py_2to3. This will convert your 2.x source
files into 3.x syntax *at installation time*, and thus should solve
this problem.
> Another possibility is writing it as b("foo"), with the timemachine
> defining an appropriate b function:
> b = lambda x: x # 2.x
> b = lambda x: x.encode('ascii') # 3.x
> but the runtime overhead could be painful, especially in 3.x.
You could use interning if you are worried about the runtime overhead:
_intern = {}
def b(s):
try:
return _intern[s]
except KeyError:
res = _intern[s] = s.encode("ascii")
return res
This will be still somewhat slower, but not much.
If you are really worried about performance, you could provide an
addition 2to3 fixer which unwraps the b() calls when converting to
3.x code. However, I'd rather make sure they aren't used in tight loops,
and just stop worrying.
Regards,
Martin
More information about the Python-porting
mailing list