Re: Deploying on Windows servers : advice sought a module
On 08/03/11, Waldemar Osuch (waldemar.os...@gmail.com) wrote: At my work place I still use py2exe but I do not rely on its automatic discovery and packaging. The setup.py lists all the dependencies explicitly in packages and includes parameters. These end up in library.zip. Then the source file paths with the actual business logic are gathered with os.walk and passed in as data_files parameter. Finally the actual service executable is generated from the very minimal script. This executable is registered only once and as long as you do not move it to a different directory Windows will find it and start it up for you. The service script that gets used by py2exe is truly minimal. It just changes working directory to where the executable sits, adds current directory to the sys.path and loads the main script. ... Hi Waldemar. Thanks for your advice. I'll have another play with py2exe. Many thanks Rory -- Rory Campbell-Lange r...@campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Deploying on Windows servers : advice sought a module
We have written a cross-platform monitoring system that we have deployed on our Linux servers and wish to put on our Windows servers too. In the past I've played with py2exe and similar packages. However the frequent updates to the monitoring suite mean that reinstalling an exe for each update would quickly become onerous. As we are used to the excellent versioning and updates provided by Debian I'm wary of installing different packages around the internet onto each Windows server. Is there a good way of combining packages and dependencies into an easily-deployable, and easily-updateable unit? Advice gratefully received. Notes about our setup: The Linux servers all have local installs of mercurial, python-yaml and Python 2.6. The advantage of having mercurial locally installed on each server is that we are making fairly frequent updates to the system and we can easily automate the hg pull/update process. Windows servers use Tim Golden's WMI modules and the pywin32 extensions. -- Rory Campbell-Lange r...@campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Get name of file from directory into variable
On 03/08/10, Alex Willmer (a...@moreati.org.uk) wrote: On Aug 3, 11:21?am, loial jldunn2...@gmail.com wrote: In a unix shell script I can do something like this to look in a directory and get the name of a file or files into a variable : MYFILE=`ls /home/mydir/JOHN*.xml` Can I do this in one line in python? import glob my_files = glob.glob('/home/mydir/JOHN*.xml') import os; my_files = [f for f in os.listdir('/home/mydir/') if 'JOHN' in f and 'xml' in f] But in fact glob uses os.listdir and fnmatch.fnmatch functions internally, so is definitely the way to go. http://docs.python.org/library/glob.html -- Rory Campbell-Lange r...@campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Sort the values of a dict
mylist = [z for z in zip(list(d), list(d.values()))] The Pythonic way for the above line is: mylist = list(d.items()) Quite right. Thanks for pointing that out. I liked Chris Kaynor's solution: s = sorted(d.items(), key=lambda i: i[1][2]) I'm a bit rusty, and have just picked up the new Learning Python book but I'm finding that it isn't providing a useful refresher to the language or a particularly good introduction to version 3. Have you any suggestions. Regards Rory -- Rory Campbell-Lange r...@campbell-lange.net Campbell-Lange Workshop www.campbell-lange.net 0207 6311 555 3 Tottenham Street London W1T 2AF Registered in England No. 04551928 -- http://mail.python.org/mailman/listinfo/python-list
Re: Line indexing in Python
On 18/12/09, seafoid (fitzp...@tcd.ie) wrote: http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html Your specification is confusing. However I suggest you break it down the code so that the steps in your programme are logical. Good luck. # example psuedocode headers = {} header_clauses = {} current_header = None def header_parser (input): split input into parts make unique header desciptor check not in headers else abort with error (?) add descriptor to headers hash # eg descriptor 1 = [attrib1, attrib2, attrib3] return descriptor def clause_parser (input, current_header): if current_header is None: abort split clause into parts store in array in header_clauses [current_header] # this will make a data structure like this: # header_clauses = { # descriptor1 = {[ clause parts ], [ clause parts ], ... } # descriptor2 = {[ clause parts ], [ clause parts ], ... } def comment_parser (input) pass # now run over the file for l in lines: if l[0] == 'c': comment_parser(l) elif l[0] == 'p': current_header = header_parser(l) else: clause_parser(l, current_header) # now that we have stored everything, check the data for h in headers: attrib1, attrib2, attrib3 = headers[h] for c in header_clauses: iterate over the arrays of clause parts either adding them up or comparing them to the header attributes -- Rory Campbell-Lange Director r...@campbell-lange.net Campbell-Lange Workshop www.campbell-lange.net 0207 6311 555 3 Tottenham Street London W1T 2AF Registered in England No. 04551928 -- http://mail.python.org/mailman/listinfo/python-list
Re: Sort the values of a dict
On 18/12/09, mattia (ger...@gmail.com) wrote: Hi all, I have a dictionary that uses dates and a tuples ad key, value pairs. I need to sort the values of the dict and insert everything in a tuple. The additional problem is that I need to sort the values looking at the i-th element of the list. I'm not that good at python (v3.1), but this is my solution: d = {1:('a', 1, 12), 5:('r', 21, 10), 2:('u', 9, 8)} t = [x for x in d.values()] def third(mls): ... return mls[2] ... s = sorted(t, key=third) pres = [] for x in s: ... for k in d.keys(): ... if d[k] == x: ... pres.append(k) ... break ... res = [] for x in pres: ... res.append((x, d[x])) ... res [(2, ('u', 9, 8)), (5, ('r', 21, 10)), (1, ('a', 1, 12))] How about mylist = [z for z in zip(list(d), list(d.values()))] and then sort on your sort criteria, either i[0], i[0][1], i[0][2] or i[0][3]. Rory -- Rory Campbell-Lange Director r...@campbell-lange.net Campbell-Lange Workshop www.campbell-lange.net 0207 6311 555 3 Tottenham Street London W1T 2AF Registered in England No. 04551928 -- http://mail.python.org/mailman/listinfo/python-list
Re: Local class variables? (mod_python problem)
On 23/02/07, Diez B. Roggisch ([EMAIL PROTECTED]) wrote: It is not desirable for the class variable to keep incrementing outside of invocations of '__main__', as is the case when it is loaded under mod_python under apache2 on linux. I'm still not clear on what you want to accomplish. In the end it boils down to who is supposed to share that information in the variables, or in other words: which scope has it. Is it per request? Then using some thread-local storage would be in order, or abusing a possible request-object. Is it per user, over several requests? Then you need a session-mechanism. Is it per application, for several users, over several requests? Then your approach is ok, but needs guarding against concurrrent access using threading.Lock for example. However, I presume that is not the desired usecase, from what I can extract from your posts I presume it's case two. Many thanks for your reply. The use case is per request, and I would be grateful to learn more about thread-local storage. Kind regards Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Local class variables? (mod_python problem)
We have a set of classes using static methods to retain reference variables between operations. The problem is that the static variables are not reset between operations when used through mod_python. Although it is possible to reset the class variables between invocations of the system, this has the potential of 'wiping out' these variables when another user is using the system. Is there a way of getting the equivalent of 'local class variables'? In other words, a way of making 'print a' and 'print b' below provide the same output? Regards Rory class TryMe(object): x = 0 y = 0 def __init__(self): self.a = 0, self.b = 0 @staticmethod def incrementer(): TryMe.x += 1, TryMe.y += 1 def addone (self): TryMe.x += 1, TryMe.y += 1 self.a , += 1 self.b += 1 def __repr__(self): return TryMe.x = %d TryMe.y = %d self.a = %d self.b = %d % (TryMe.x, TryMe.y, self.a, self.b) if __name__ == '__main__': a = TryMe() a.incrementer() a.addone() b = TryMe() b.incrementer() b.addone() print 'a:', a print 'b:', b -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Local class variables? (mod_python problem)
Apologies to Piet and Diez for the lack of clarity in my previous post (and the broken code). In essence we use class variables as follows: class Part (object): totalgia = 0 def __init__(self, gia): self.gia = gia # gross internal area self.giaratio = 0 Part.totalgia += self.gia def addavgbm(self): self.giaratio = float(self.gia)/float(Part.totalgia) def __repr__(self): return gia: %0.1f giaratio: %0.2f % (self.gia, self.giaratio) if __name__ == '__main__': p1 = Part(20) p2 = Part(30) for p in p1, p2: p.addavgbm() print p totalgia keeps incrementing when this code is used under mod_python. We most certainly are in 'murky waters of accidental concurrent access'. A life vest would be gratefully received. Kind regards Rory On 22/02/07, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: We have a set of classes using static methods to retain reference variables between operations. The problem is that the static variables are not reset between operations when used through mod_python. Although it is possible to reset the class variables between invocations of the system, this has the potential of 'wiping out' these variables when another user is using the system. Is there a way of getting the equivalent of 'local class variables'? In other words, a way of making 'print a' and 'print b' below provide the same output? On 22/02/07, Piet van Oostrum ([EMAIL PROTECTED]) wrote: Rory Campbell-Lange [EMAIL PROTECTED] (RC) wrote: There are several errors in your python code: quite a number of comma's have to be replaced by semicolons (or newlines), and there is a spurious comma. On 22/02/07, Diez B. Roggisch ([EMAIL PROTECTED]) wrote: Rory Campbell-Lange wrote: It's very unclear what you mean here, and I'm additionally under the impression that you are deep in the murky waters of accidential concurrent access errors here. -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Local class variables? (mod_python problem)
On 22/02/07, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: In essence we use class variables as follows: class Part (object): totalgia = 0 def __init__(self, gia): self.gia = gia # gross internal area self.giaratio = 0 Part.totalgia += self.gia def addavgbm(self): self.giaratio = float(self.gia)/float(Part.totalgia) def __repr__(self): return gia: %0.1f giaratio: %0.2f % (self.gia, self.giaratio) if __name__ == '__main__': p1 = Part(20) p2 = Part(30) for p in p1, p2: p.addavgbm() print p totalgia keeps incrementing when this code is used under mod_python. On 22/02/07, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: On 22/02/07, Piet van Oostrum ([EMAIL PROTECTED]) wrote: Rory Campbell-Lange [EMAIL PROTECTED] (RC) wrote: RC totalgia keeps incrementing when this code is used under mod_python. And also when used outside of mod_python. It is because it is a class level variable. In fact I think under certain circumstances in mod_python it will not do that because different requests can run in different Apache processes (on Linux, Unix, Mac OS X etc.). So it this desired behaviour or not? Your post isn't clear about that. And if it isn't what is the desired behaviour? And you certainly should do something about the concurrent access. It is not desirable for the class variable to keep incrementing outside of invocations of '__main__', as is the case when it is loaded under mod_python under apache2 on linux. I would be grateful for pointers on dealing with concurrent access. Regards Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Maths error
(1.0/10.0) + (2.0/10.0) + (3.0/10.0) 0.60009 6.0/10.0 0.59998 Is using the decimal module the best way around this? (I'm expecting the first sum to match the second). It seem anachronistic that decimal takes strings as input, though. Help much appreciated; Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
read sys.stdin, then raw_input
I'm stumped. I'm piping some content to a python program, but then want the python program to prompt me for an answer. It goes on reading the pipe and gives a EOFError: EOF when reading a line. eg: #!/usr/bin/python import sys text = sys.stdin.read() sel = raw_input('Selection? : ') Selection? : Traceback (most recent call last): File /tmp/z.py, line 5, in ? sel = raw_input('Selection? : ') EOFError: EOF when reading a line Advice much appreciated. Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: read sys.stdin, then raw_input
To clarify, how can I get a normal terminal prompt for raw_input to enable me to insert a value into variable 'sel'? The program wants to keep reading from the piped file, it seems. Rory On 01/03/06, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: I'm stumped. I'm piping some content to a python program, but then want the python program to prompt me for an answer. It goes on reading the pipe and gives a EOFError: EOF when reading a line. eg: #!/usr/bin/python import sys text = sys.stdin.read() sel = raw_input('Selection? : ') Selection? : Traceback (most recent call last): File /tmp/z.py, line 5, in ? sel = raw_input('Selection? : ') EOFError: EOF when reading a line Advice much appreciated. -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie errors :-( Need help
There is no variable called result in your function or, presumably, in the global scope of the programme. As you aren't returning anything you may as well omit that line. On 02/03/06, - C Saha - ([EMAIL PROTECTED]) wrote: I am getting the result but along with this error, any idea what could be the reason? fibo.fib(1000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987Traceback (most recent call last): File interactive input, line 1, in ? File fibo.py, line 8, in fib return result NameError: global name 'result' is not defined -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: read sys.stdin, then raw_input
That works great for me on my unixoid system. Thanks, Fredrik. On 02/03/06, Fredrik Lundh ([EMAIL PROTECTED]) wrote: since there's only one input channel for a program (stdin), and raw_input reads from that channel, I don't think there's a portable way to do this. ... # rebind sys.stdin to my tty sys.stdin = open(/dev/tty) -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
newbie : prune os.walk
Hi. How can I list root and only one level down? I've tried setting dirs = [] if root != start root, but it doesn't work. I clearly don't understand how the function works. I'd be grateful for some pointers. Thanks Rory /tmp/test |-- 1 |-- 2 |-- 3 |-- 4 |-- one | |-- 1 | |-- 2 | |-- 3 | |-- 4 | `-- subone - dont want to see this `-- two |-- 5 |-- 6 |-- 7 `-- 8 3 directories, 12 files for root, dirs, files in os.walk('/tmp/test', True): ... print root, dirs, files ... /tmp/test ['one', 'two'] ['1', '2', '3', '4'] /tmp/test/one ['subone'] ['1', '2', '3', '4'] /tmp/test/one/subone [] [] /tmp/test/two [] ['5', '6', '7', '8'] -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Sorting dictionary by 'sub' value
I have a dictionary of images. I wish to sort the dictionary 'v' by a dictionary value using python 2.3. The dictionary value is the date attribute as shown here: v[imagename][9]['date'] This attribute is an extracted EXIF value from the following set: data element [9] of v[imagename]: {'now': datetime.date(2005, 3, 7), 'y' : (0x011B) Ratio=72 @ 182, 'ctime' : datetime.date(2005, 3, 7), 'width' : (0xA002) Long=1024 @ 434, 'length' : (0xA003) Long=768 @ 446, 'date': (0x9004) ASCII=2004:12:07 00:18:20 @ 514, 'x' : (0x011A) Ratio=72 @ 174, 'model' : (0x0110) ASCII=PENTAX Optio 330 @ 156, 'size': 367415L, 'orientation' : (0x0112) Short=1 @ 42} Thanks, Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting dictionary by 'sub' value
Thank you all very much for your help. I did the following and it works: imgs=v.keys() imgs.sort(lambda a,b: cmp( time.strptime(str(v[a][9]['date']), '%Y:%m:%d %H:%M:%S'), time.strptime(str(v[b][9]['date']), '%Y:%m:%d %H:%M:%S')) ) for i in imgs: ... Regards, Rory On 08/03/05, Diez B. Roggisch ([EMAIL PROTECTED]) wrote: l = v.items() l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date']) On 08/03/05, Scott David Daniels ([EMAIL PROTECTED]) wrote: You can't sort dicts - they don't impose an order on either key or value. There are ordered dict implementations out there, but AFAIK the only keep the keys sorted, or maybe the (key,values) in the insertion order. But maybe this helps you: l = v.items() l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date']) In 2.4, this is simple: ordered_keys = sorted(v, key=lambda name: v[name][9]['date']) In 2.3, or earlier, use decorate-sort-undecorate: decorated = [(value[9]['date'], key) for key, value in v.iteritems()] decorated.sort() result = [key for key, date in decorated] On 08/03/05, Batista, Facundo ([EMAIL PROTECTED]) wrote: temp_list = [ (x[1][1], x[0]) for x in d.items() ] ... temp_list.sort() for (tmp, key) in temp_list: -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie CGI problem
On 18/02/05, Peter Otten ([EMAIL PROTECTED]) wrote: Rory Campbell-Lange wrote: #!/usr/bin/python import cgi print Content-type: text/html\n\n print hi Gives me the following in my browser: ''' hi Content-type: text/html hi ''' Why are there two 'hi's? You have chosen a bad name for your script: cgi.py. It is now self-importing. Rename it to something that doesn't clash with the standard library, and all should be OK. Thanks very much. Ouch! Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Newbie CGI problem
#!/usr/bin/python import cgi print Content-type: text/html\n\n print hi Gives me the following in my browser: ''' hi Content-type: text/html hi ''' Why are there two 'hi's? Thanks, Rory -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question about class operator overloading
Hi Steve I've been playing around with your two suggestions. The Record class is an elegant solution. It doesn't however help in the case where the class has the following general data structure (something I should have stated originally): class.config1 = param class.config2 = param class.data= { 'this' : [] 'that' : [] ... } The __setitem__ and __getitem__ methods allow the class.data data structure to be dealt with easily as self.data[key] = val without worrying about getting involved with other variables such as config1 and config2 (because potentially a data key could be called 'config1' for example. So the __getitem__ and __setitem__ give data hiding properties when I inherit from this class. I think! Anyway, I'm very grateful for your help. Rory On 15/02/05, Steven Bethard ([EMAIL PROTECTED]) wrote: Rory Campbell-Lange wrote: I am anxious about how best to set and access items one level down in a data structure if I am using __setitem__ and __getitem__. ... object['three'] = [0, 'val0'] # set x = object['three'][0] # get ... py data['one'].foo = 'val0' py data {'two': Record(5, 6, {}, 8), 'one': Record('val0', 2, {}, 4)} py data['one'].foo 'val0' ... ... def __setitem__(self, x, value): ... try: ... name, index = x ... self.data.setdefault(name, {})[index] = value ... except ValueError: ... self.data[x] = value ... py c['one', 0] = 1 This does seem a lot more logical than my object['three'] = [0, 'val0']. Thanks for this (and using try against a possible ValueError). ... As you can see, Python has builin syntax support to allow tuples to be used as dict keys. (The parentheses can be omitted.) Still, I think the class-based solution is much better than the __getitem__/__setitem__ one. -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Newbie question about class operator overloading
Hi. I'm just starting to use python. I am anxious about how best to set and access items one level down in a data structure if I am using __setitem__ and __getitem__. At the moment I can do for a data structure Data: object.Data = { 'one' : [1, 2, {}, 4], 'two' : [5, 6, {}, 8]} I can use normal __setitem__ and __getitem__ to address Data keys very easily However, if I wish to access object.Data['one'][0] for instance, I am using the following: object['three'] = [0, 'val0'] # set x = object['three'][0] # get Is this advisable? I'm worried the syntax is very odd. Extract from an example class: def __setitem__ (self,key,value): if type(value) == list and type(value[0]) == int: if key not in self.data: self.data[key] = {} self.data[key][value[0]] = value[1] else: self.data[key] = value def __getitem__ (self,key,value=None): if not value==None: return self.data[key][value] else: return self.data[key] -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question about class operator overloading
Anyone out there? Criticism about the objective of my question, not just the execution, gratefully received! Basically, if I have a class This: def __init__(self, x, y): self.x=x self.y=y self.data = {} and then make all my setitem and getitem calls refer to self.data; is it sensible and right to be able to refer to self.data[n]? Rory On 15/02/05, Rory Campbell-Lange ([EMAIL PROTECTED]) wrote: Hi. I'm just starting to use python. I am anxious about how best to set and access items one level down in a data structure if I am using __setitem__ and __getitem__. At the moment I can do for a data structure Data: object.Data = { 'one' : [1, 2, {}, 4], 'two' : [5, 6, {}, 8]} I can use normal __setitem__ and __getitem__ to address Data keys very easily However, if I wish to access object.Data['one'][0] for instance, I am using the following: object['three'] = [0, 'val0'] # set x = object['three'][0] # get Is this advisable? I'm worried the syntax is very odd. Extract from an example class: def __setitem__ (self,key,value): if type(value) == list and type(value[0]) == int: if key not in self.data: self.data[key] = {} self.data[key][value[0]] = value[1] else: self.data[key] = value def __getitem__ (self,key,value=None): if not value==None: return self.data[key][value] else: return self.data[key] -- Rory Campbell-Lange [EMAIL PROTECTED] www.campbell-lange.net -- http://mail.python.org/mailman/listinfo/python-list