python-2.x

Is it safe to replace MacOS X default python interpreter ?

Hi guys, i'm new to the Mac OS X world so i have to ask you this. I have the default python 2.6.1 installed as /usr/bin/python and the 3.1.2 as /usr/local/bin/python3.1 . Considering that i use only 3.x syntax, is it safe to replace the default interpreter (2.6) with the 3.1 one (python-config included) using symlinks (and removing old ...

Writing a module for both Python 2.x and 3.x

I've written a pure-Python module for Python 3.0/3.1 which I'd also like to make it compatible with 2.x (probably just 2.6/2.7) in order to make it available to the widest possible audience. The module is concerned with reading and writing a set of related file formats, so the differences between 2.x and 3.x versions would be slight — e...

How do the compression codecs work in Python?

I'm querying a database and archiving the results using Python, and I'm trying to compress the data as I write it to the log files. I'm having some problems with it, though. My code looks like this: log_file = codecs.open(archive_file, 'w', 'bz2') for id, f1, f2, f3 in cursor: log_file.write('%s %s %s %s\n' % (id, f1 or 'NULL', f2...

Lisp's apply and funcall vs Python's apply

Lisp's apply is for Lisp's APPLY is for calling functions with computed argument lists stored in lists.(Modified from Rainer's comment) For example, the following code changes (list 1 2 3) to (+ 1 2 3). (apply #'+ '(1 2 3)) However, Python's apply does what Lisp's funcall does, except for some minor differences (input is given as t...

Caesar Cipher in python (Please help me where am i going wrong)

The error which i am getting is Traceback (most recent call last): File "imp.py", line 52, in <module> mode = getMode() File "imp.py", line 8, in getMode mode = input().lower() File "<string>", line 1, in <module> NameError: name 'encrypt' is not defined Below is the code. # Caesar Cipher MAX_KEY_SIZE = 26 def getMod...

Simple python regex groups can't parse date

I'm trying to parse dates with regex, using groups, but python is returning empty lists. I'm not doing anything fancy, just 12/25/10 sort of stuff. I want it to reject 12/25-10 though. date = re.compile("\d{1,2}([/.-])\d{1,2}\1\d{2}") I've tried online regex libraries, but their solutions don't seem to run either. Any ideas? Sampl...

in python 2.6 how do i change a global variable within a for loop

i want to change the value of a variable within a for loop, but then have the new preserved variable changed when the loop finishes my attempt (kinda simplified from what i'm actually doing) SNP is a list and compar_1 is a list of lists line_1 = empty_row for SNP1 in compar_1: global line_1 if SNP[3] == SNP1[3] compar...

Getting a Python function to cleanly return a scalar or list, depending on number of arguments

Disclaimer: I'm looking for a Python 2.6 solution, if there is one. I'm looking for a function that returns a single value when passed a single value, or that returns a sequence when passed multiple values: >>> a = foo(1) 2 >>> b, c = foo(2, 5) >>> b 3 >>> c 6 To be clear, this is in an effort to make some function calls simply look ...

How can "k in d" be False, but "k in d.keys()" be True?

I have some python code that's throwing a KeyError exception. So far I haven't been able to reproduce outside of the operating environment, so I can't post a reduced test case here. The code that's raising the exception is iterating through a loop like this: for k in d.keys(): if condition: del d[k] The del[k] line throw...