python-2.4

Python idiom to return first item or None

I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: list = get_list() if len(list) > 0: return list[0] return None It seem...

Help with subprocess.call on a Windows machine

I am trying to modify a trac plugin that allows downloading of wiki pages to word documents. pagetodoc.py throws an exception on this line: # Call the subprocess using convenience method retval = subprocess.call(command, shell=True, stderr=errptr, stdout=outptr, close_fds = True) Saying that close_fds is not supported on Windows. The ...

Sending email using google apps SMTP server in Python 2.4

I'm having difficulty getting python 2.4 to connect to gmail's smtp server. My below script doesn't ever get past "connection". I realise there is an SMTP_SSL class in later versions of python and it seems to work fine, but the production environment I have to deal with only has - and likely will only ever have - python 2.4. print "conn...

In Python 2.4, how can I strip out characters after ';'?

Hi, Let's say I'm parsing a file, which uses ; as the comment character. I don't want to parse comments. So if I a line looks like this: example.com. 600 IN MX 8 s1b9.example.net ; hello! Is there an easier/more-elegant way to strip chars out other than this: rtr = '' for line in file: trig = False ...

Is there a library which handles the parsing of BIND zone files in Python?

Hi, This is related to a similar question about BIND, but in this case I'm trying to see if there's any easy way to parse various zone files into a dictionary, list, or some other manageable data structure, with the final goal being committing the data to a database. I'm using BIND 8.4.7 and Python 2.4. I may be able to convince manag...

How to set smtplib sending timeout in python 2.4?

I'm having problems with smtplib tying up my program when email sending fails, because a timeout is never raised. The server I'm using does not and will never have python greater than 2.4, so I can't make use of the timeout argument to the SMTP constructor in later versions of python. Python 2.4's docs show that the SMTP class does not ...

SHA256 hash in Python 2.4

Hi, Is there a way I can calculate a SHA256 hash in Python 2.4 ? (I emphasize: Python 2.4) I know how to do it in Python 2.5 but unfortunately it's not available on my server and an upgrade will not be made. I have the same problem as the guy in this question, but using Python 2.4. Any help will be greatly appreciated. EDIT: Sorry, I me...

How to modify the Python 'default' dictionary so that it always returns a default value

I'm using all of them to print the names of assigned IANA values in a packet. So all of the dictionaries have the same default value "RESERVED". I don't want to use d.get(key,default) but access dictionaries by d[key] so that if the key is not in d, it returns the default (that is same for all dictionaries). I do not necessarily need ...

Count warnings in Python 2.4

I've got some tests that need to count the number of warnings raised by a function. In Python 2.6 this is simple, using with warnings.catch_warnings(record=True) as warn: ... self.assertEquals(len(warn), 2) Unfortunately, with is not available in Python 2.4, so what else could I use? I can't simply check if there's been a sing...

Pickling array.array in 2.4 using cPickle

Hey All, I am working on a project built on python 2.4 (It is an embedded python project, so I don't have a choice on the version of python used). Throughout the application, we use array.array to store data. Support for pickling array.array objects was added to pickle (and cPickle) in 2.5. We have a viable workaround in 2.4 when usi...

Avoiding accidentally catching KeyboardInterrupt and SystemExit in Python 2.4

In Python scripts, there are many cases where a keyboard interrupt (Ctrl-C) fails to kill the process because of a bare except clause somewhere in the code: try: foo() except: bar() The standard solution in Python 2.5 or higher is to catch Exception rather than using bare except clauses: try: foo() except Exception: b...

Python 2.4 inline if statements

I am setting up an existing django project on a dreamhost web server, so far I have got everything to work correctly. However I developed under python 2.5 and dreamhost by default uses python 2.4. The following line seems gives a syntax error because of the if keyword: 'parent': c.parent.pk if c.parent is not None else None ...

python get time in minutes

import datetime start = datetime.datetime(2009, 1, 31) end = datetime.datetime(2009, 2, 1) print end-start >>1 day, 0:00:00//output How to get the output in minutes Thanks, ...

convert float numbers to hex

I am quite new in Python and I am looking for converting numbers from decimal to hex How to convert float numbers to hex or char in Python 2.4.3? how can I keep it to write it as ("\xa5\x (here the new hex number)") any help Thanks, ...

Parsing timestamp with Python2.4

I want to parse a timestamp from a log file that has been written via datetime.datetime.now().strftime('%Y%m%d%H%M%S') and then compute the number of seconds that have passed since this timestamp. I know I could do it with datetime.datetime.strptime to get back a datetime object and then compute a timedelta. Problem is, the strptime f...

What HTML parser to choose and why BeautifulSoup doesn't work?

I need to parse a HTML-page with windows-1251 charset (it's in russian). The problem is that it is the web application and I have to use Python 2.4 without any opportunity to install modules on server. The only thing I tried to do was asking an administrator to install lxml module but nevertheless it wasn't built in the right way on 2....

JSON module for python 2.4?

I'm accustomed to doing import json in Python 2.6, however I now need to write some code for Python 2.4. Is there a JSON library with a similar interface that is available for Python 2.4? ...

XML to/from a Python dictionary

I need to use Python 2.4.4 to convert XML to and from a Python dictionary. All I need are the node names and values, I'm not worried about attributes because the XML I'm parsing doesn't have any. I can't use ElementTree because that isn't available for 2.4.4, and I can't use 3rd party libraries due to my work environment. What's the easi...