Re: Incorrect number of bytes returned by getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, buflen)
On Thu, 3 Dec 2009 10:29:13 -0800 (PST), Ashwin Rao ashwin.shirvan...@gmail.com wrote: tcp_info = sock.getsockopt(socket.SOL_SOCKET, socket.TCP_INFO, 104) In C, to get tcp_info, SOL_SOCKET should be IPPROTO_TCP. I'm assuming it's the same in Python, although I've never tried it. Martien -- | Martien Verbruggen | Never hire a poor lawyer. Never buy first.l...@heliotrope.com.au | from a rich salesperson. | -- http://mail.python.org/mailman/listinfo/python-list
Re: Dll in python
On Fri, 23 Oct 2009 13:09:10 -0700 (PDT), snonca luis.bi...@gmail.com wrote: hello I would like to know how to create dll in python to implement a project. NET Are you maybe looking for this: http://pythonnet.sourceforge.net/ Martien -- | Martien Verbruggen | If it isn't broken, it doesn't have first.l...@heliotrope.com.au | enough features yet. | -- http://mail.python.org/mailman/listinfo/python-list
Re: Reverse Iteration Through Integers
On Sun, 18 Oct 2009 23:04:36 -0700 (PDT), alex23 wuwe...@gmail.com wrote: On Oct 19, 3:53 pm, Jabba Laci jabba.l...@gmail.com wrote: Would someone explain how str[::-1] work? I'm new to Python and I only saw so far the str[begin:end] notation. What is the second colon? Slice notation is of the form [start:stop:step]. start defaults to the start of the sequence, stop to the end, and step to 1. So a slice of [::-1] returns the full sequence in reverse, stepping back one character at a time from the end of the sequence to the beginning. The only mention I could find was http://docs.python.org/dev/3.0/library/functions.html#slice There's also http://docs.python.org/reference/datamodel.html#index-791 Martien -- | Martien Verbruggen | There are only 10 types of people in first.l...@heliotrope.com.au | the world; those who understand binary | and those who don't. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is pythonic version of scanf() or sscanf() planned?
On Sun, 4 Oct 2009 01:17:18 + (UTC), Grant Edwards inva...@invalid.invalid wrote: On 2009-10-03, ryniek90 rynie...@gmail.com wrote: So, whether it is or has been planned the core Python implementation of *scanf()* ? One of the fist things I remember being taught as a C progrmmer was to never use scanf. Programs that use scanf tend to fail in rather spectacular ways when presented with simple typos and other forms of unexpected input. That's right. One shouldn't use scanf() if the input is unpredictable osr comes from people, because the pitfalls are many and hard to avoid. However, for input that is formatted, scanf() is perfectly fine, and nice and fast. fgets() with sscanf() is better to control if your input is not as guaranteed. Given the bad behavior and general fragility of scanf(), I doubt there's much demand for something equally broken for Python. scanf() is not broken. It's just hard to use correctly for unpredictable input. Having something equivalent in Python would be nice where most or all of your input is numerical, i.e. floats or integers. Using the re module, or splitting and converting everything with int() or float() slows down your program rather spectacularly. If those conversions could be done internally, and before storing the input as Python strings, the speed improvements could be significant. There is too much storing, splitting, regex matching and converting going on if you need to read numerical data from columns in a file. scanf() and friends make this sort of task rather quick and easy. For example, if your data is the output of a numerical analysis program or data coming from a set of measuring probes, it often takes the form of one or more columns of numbers, and there can be many of them. If you want to take one of these output files, and transform the data, Python can be terribly slow. It doesn't have to be scanf(), but something that would allow the direct reading of text input as numerical data would be nice. On the other hand, if something really needs to be fast, I generally write it in C anyway :) Martien -- | Martien Verbruggen | Unix is user friendly. It's just first.l...@heliotrope.com.au | selective about its friends. | -- http://mail.python.org/mailman/listinfo/python-list
Re: Is pythonic version of scanf() or sscanf() planned?
On Sun, 4 Oct 2009 13:18:22 -0400, Simon Forman sajmik...@gmail.com wrote: On Sun, Oct 4, 2009 at 5:29 AM, Martien Verbruggen martien.verbrug...@invalid.see.sig wrote: On Sun, 4 Oct 2009 01:17:18 + (UTC), Grant Edwards inva...@invalid.invalid wrote: On 2009-10-03, ryniek90 rynie...@gmail.com wrote: So, whether it is or has been planned the core Python implementation of *scanf()* ? Given the bad behavior and general fragility of scanf(), I doubt there's much demand for something equally broken for Python. scanf() is not broken. It's just hard to use correctly for unpredictable input. Having something equivalent in Python would be nice where most or all of your input is numerical, i.e. floats or integers. Using the re module, or splitting and converting everything with int() or float() slows down your program rather spectacularly. If those conversions could be done internally, and before storing the input as Python strings, the speed improvements could be significant. I haven't tried it but couldn't you use scanf from ctypes? I have just tried it. I wasn't aware of ctypes, being relatively new to Python. :) However, using ctypes makes the simple test program I wrote actually slower, rather than faster. Probably the extra conversions needed between ctypes internal types and Python's eat op more time. Built in scanf()-like functionality would not need to convert the same information two or three times. it would parse the bytes coming in from the input stream directly, and set the values of the appropriate Python variable directly. Contrive an example: Assume an input file with two integers, and three floats per line, separated by a space. output should be the same two integers, followed by the average of the three floats. In pure python, now, there is string manipulation (file.readline(), and split()) and conversion of floats going on: from sys import * for line in stdin: a, b, u, v, w = line.split() print a, , b, , (float(u) + float(v) + float(w))/3.0 (17.57s user 0.07s system 99% cpu 17.728 total) With ctypes, it becomes something like: from sys import * from ctypes import * from ctypes.util import find_library libc = cdll.LoadLibrary(find_library('c')) a = c_int() b = c_int() u = c_float() v = c_float() w = c_float() for line in stdin: libc.sscanf(line, %d%d%f%f%f, byref(a), byref(b), byref(u), byref(v), byref(w)) print {0} {1} {2}.format(a.value, b.value, (u.value + v.value + w.value)/3.0) (22.21s user 0.10s system 98% cpu 22.628) We no longer need split(), and the three conversions from string to float(), but now we have the 5 c_types(), and the .value dereferences at the end. And that makes it slower, unfortunately. (Maybe I'm still doing things a bit clumsily and it could be faster) It's not really a big deal: As I said before, if I really need the speed, I'll write C: #include stdio.h int main(void) { int a, b; float u, v, w; while (scanf(%d%d%f%f%f, a, b, u, v, w) == 5) printf(%d %d %f\n, a, b, (u + v + w)/3.0); return 0; } (5.96s user 0.06s system 99% cpu 6.042 total) Martien -- | Martien Verbruggen | There is no reason anyone would want a first.l...@heliotrope.com.au | computer in their home. -- Ken Olson, | president DEC, 1977 -- http://mail.python.org/mailman/listinfo/python-list
Re: Open file on remote linux server
On Wed, 23 Sep 2009 23:41:35 +0200, Diez B. Roggisch de...@nospam.web.de wrote: The Bear schrieb: Hi I'm looking to do something like this f = f.openfileobj(remotefileloc, localfilelikeobj) my remote files are on a solaris box that i can access using ssh (could prehap request othe protocols if necessary) anyone got any ideas? try paramiko. Or just use subprocess to scp and open the file locally. import paramiko ssh = paramiko.SSHClient() ssh.load_system_host_keys(os.environ['HOME'] + '/.ssh/known_hosts') ssh.connect('localhost') try: ftp = ssh.open_sftp() # To write fh = ftp.file('/tmp/foo.test', 'w') fh.write('This is a test string\nAnd this is another one') fh.close() # To read fh = ftp.file('/tmp/foo.test', 'r') for l in fh: print l, fh.close() finally: ssh.close() You may need to add some password handling in there (for me my environment takes care of that). Martien -- | Martien Verbruggen | You can't have everything, where would first.l...@heliotrope.com.au | you put it? | -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Tue, 29 Sep 2009 19:09:16 -0700 (PDT), chad cdal...@gmail.com wrote: I have a directory that contains the following login rc sum _1 _2 _3 _4 What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I don't know what you mean by sanest, but this is one way: import glob for f in glob.glob(_*): print f Martien -- | Martien Verbruggen | You can't have everything, where would first.l...@heliotrope.com.au | you put it? | -- http://mail.python.org/mailman/listinfo/python-list
Re: Searching a large dictionary
On Wed, 23 Sep 2009 14:52:56 -0700 (PDT), mike171562 support.desk@gmail.com wrote: Sorry for the confusion, Simon, this is almost exactly what I need, but i need to be able to search for a string in a given value of an item Here is an example of the dict I am working with {'252': [{'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield': '252', 'from': '9876662881', 'to': '19877760406', 'fpld': 'Foobar 9855562881', 'result': 'ANSW', 'sec': 131}, {'code': '51679', 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881', 'to': '1980391', 'fpld': 'Foobar 9876555881', 'result': 'ANSW', 'sec': 86}]} 252 being the key, I need to be able to search for a string in a given item , say 777 in the 'to' field so print wtf(dict,'to','777') And where in this do you tell wtf() that you want to look at 252 only? From your other posts it seems that the above is actually part of a larger dict with other keys besides 252. Or do you want all of those returned? I'll assume you want to look through ever key at the 'top' level, and through every dict in the list that's associated with that. would return {'252': [{'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield': '252', 'from': '9876662881', 'to': '19877760406', 'fpld': 'Foobar 9855562881', 'result': 'ANSW', 'billsec': 131}, {'code': '51679', 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881', 'to': '1980391', 'fpld': 'Foobar 9876555881', 'result': 'ANSW', 'billsec': 86}]} I hope this makes sense, sorry for not being clear Did you mean something like: top_dict = { '252': [ {'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield': '252', 'from': '9876662881', 'to': '19877760406', 'fpld': 'Foobar 9855562881', 'result': 'ANSW', 'sec': 131}, {'code': '51679', 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881', 'to': '1980391', 'fpld': 'Foobar 9876555881', 'result': 'ANSW', 'sec': 86}, {'code': 'foo', 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881', 'to': '198770391', 'fpld': 'Foobar 9876555881', 'result': 'ANSW', 'sec': 86} ], '253': [ {'code': '51679', 'date': '2009-08-01 11:35:38', 'userfield': '252', 'from': '9876662881', 'to': '19877760406', 'fpld': 'Foobar 9855562881', 'result': 'ANSW', 'sec': 131}, {'code': 'foo', 'date': '2009-08-01 14:33:55', 'userfield': '252', 'from': '9876662881', 'to': '198770391', 'fpld': 'Foobar 9876555881', 'result': 'ANSW', 'sec': 86} ]} def wtf(top_dict, field, value): new_dict = {} for key, dict_list in top_dict.iteritems(): for d in dict_list: if field in d and value in d[field]: if key not in new_dict: new_dict[key] = [] new_dict[key].append(d) return new_dict nd = wtf(top_dict, 'fto', '777') print nd Martien -- | Martien Verbruggen | That's funny, that plane's dustin' first.l...@heliotrope.com.au | crops where there ain't no crops. | -- http://mail.python.org/mailman/listinfo/python-list