python-2.4

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

ぃ、小莉子 提交于 2019-12-06 06:36:55
问题 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 to use dictionaries, but they were the intuitive choice... Also, a dictionary where I could do this d = { 1..16 = "RESERVED", 17 : "Foo", 18 : "Bar, 19..255:

How to use __del__ in a reliable way?

断了今生、忘了曾经 提交于 2019-12-06 01:37:38
问题 I have learned that python does not guarantee that __del__ is called whenever an object is deleted. In other words, del x does not necessarily invoke its destructor x.__del__() . If I want to ensure proper object cleanup, I should use a context manager (in a with statement). I know it's stupid, but for a couple of reasons (please don't ask why) I am tied to a system with Python 2.4; therefore context managers are out of question (they were introduced in Python 2.5) So I need a an alternative

Change in Python built in round() function between 2.4 and 2.7

。_饼干妹妹 提交于 2019-12-05 17:08:26
Has the built in round() function in Python changed between 2.4 and 2.7? Python 2.4: Python 2.4.6 (#1, Feb 12 2009, 14:52:44) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = 1480.39499999999998181010596454143524169921875 >>> round(f,2) 1480.4000000000001 >>> Python 2.7: Python 2.7.1 (r271:86832, May 13 2011, 08:14:41) [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = 1480.39499999999998181010596454143524169921875 >>> round(f, 2) 1480.39

Workaround for python 2.4's yield not allowed in try block with finally clause

混江龙づ霸主 提交于 2019-12-05 11:53:57
I'm stuck on python2.4, so I can't use a finally clause with generators or yield . Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable time) aren't very appealing. You can duplicate code to avoid the finally block: try: yield 42 finally: do_something() Becomes: try: yield 42 except: # bare except, catches *anything* do_something() raise # re-raise same exception do_something() (I've not tried this

reusable function: substituting the values returned by another function

荒凉一梦 提交于 2019-12-05 05:02:15
问题 Below is the snippet: I'm parsing job log and the output is the formatted result. def job_history(f): def get_value(j,n): return j[n].split('=')[1] lines = read_file(f) for line in lines: if line.find('Exit_status=') != -1: nLine = line.split(';') jobID = '.'.join(nLine[2].split('.',2)[:-1] jData = nLine[3].split(' ') jUsr = get_value(jData,0) jHst = get_value(jData,9) jQue = get_value(jData,3) eDate = job_value(jData,14) global LJ,LU,LH,LQ,LE LJ = max(LJ, len(jobID)) LU = max(LU, len(jUsr))

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

亡梦爱人 提交于 2019-12-04 16:10:52
问题 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 for char in line: if not trig and char != ';': rtr += char else: trig = True if rtr[max(rtr)] != '\n': rtr += '\n' 回答1: I'd recommend saying line.split(";")[0] which will give you a string of all

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

不打扰是莪最后的温柔 提交于 2019-12-04 14:48:29
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 to use dictionaries, but they were the intuitive choice... Also, a dictionary where I could do this d = { 1..16 = "RESERVED", 17 : "Foo", 18 : "Bar, 19..255: "INVALID" } Would be the preferred solution Tuples could be another alternative, but then I'm prone to

How to use __del__ in a reliable way?

北城余情 提交于 2019-12-04 06:40:06
I have learned that python does not guarantee that __del__ is called whenever an object is deleted. In other words, del x does not necessarily invoke its destructor x.__del__() . If I want to ensure proper object cleanup, I should use a context manager (in a with statement). I know it's stupid, but for a couple of reasons (please don't ask why) I am tied to a system with Python 2.4; therefore context managers are out of question (they were introduced in Python 2.5) So I need a an alternative solution, and hence my question: are there best practices that would help me to use __del__ reliably? I

reusable function: substituting the values returned by another function

本秂侑毒 提交于 2019-12-03 21:57:25
Below is the snippet: I'm parsing job log and the output is the formatted result. def job_history(f): def get_value(j,n): return j[n].split('=')[1] lines = read_file(f) for line in lines: if line.find('Exit_status=') != -1: nLine = line.split(';') jobID = '.'.join(nLine[2].split('.',2)[:-1] jData = nLine[3].split(' ') jUsr = get_value(jData,0) jHst = get_value(jData,9) jQue = get_value(jData,3) eDate = job_value(jData,14) global LJ,LU,LH,LQ,LE LJ = max(LJ, len(jobID)) LU = max(LU, len(jUsr)) LH = max(LH, len(jHst)) LQ = max(LQ, len(jQue)) LE = max(LE, len(eDate)) print "%-14s%-12s%-14s%-12s%

Doctests that contain string literals

吃可爱长大的小学妹 提交于 2019-12-03 21:29:24
I have a unit test that I'd like to write for a function that takes XML as a string. It's a doctest and I'd like the XML in-line with the tests. Since the XML is multi-line, I tried a string literal within the doctest, but no success. Here's simplified test code: def test(): """ >>> config = \"\"\"\ <?xml version="1.0"?> <test> <data>d1</data> <data>d2</data> </test>\"\"\" """ if __name__ == "__main__": import doctest doctest.testmod(name='test') The error I get is File "<doctest test.test[0]>", line 1 config = """ <?xml version="1.0"?> ^ SyntaxError: EOF while scanning triple-quoted string I