Re: Need help initializing a list or tuple.
How about: a = [ [i**2 for j in range(256)] for i in range(256) ] b = sum(a, []) c = [ b[slice(i,i+256)] for i in range(0,256*256,256) ] a is c False a == c True BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: Implementing class methods in C
[EMAIL PROTECTED] wrote: Am I misunderstanding the purpose of PyObject* self? No. I think you do everything right, but it still doesn't work. I have tried to implement it in Python: _test.py: def func2(self, *args): print type(self) print args then all of this works: import _test class Test1: func2 = _test.func2 class Test2: pass Test2.func2 = _test.func2 class Test3: pass import new Test3.func2 = new.instancemethod(_test.func2, None, Test3) del new Test1().func2(1, 2, 3) Test2().func2(1, 2, 3) Test3().func2(1, 2, 3) If you implement _test in C, works none of the above. The only difference I can see is that: type(_test.func2) type 'function' is for Python implemented function and type(_test.func2) type 'builtin_function_or_method' for C implementation I would really like to know the answer too. How do you implement some methods in C without subclassing ? BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie Question
[EMAIL PROTECTED] wrote: x = '22,44,66,88,asd,asd,23,43,55' y = eval(x) y (22, 44, 66, 88, 'asd,asd', '23,43,55') And now, a question for the experts. I'm no expert, just experienced. Does anyone have a pointer as to why my code might be dangerous? Well, the smallest problem you have here is that you will get an SyntaxError exception on badly formated input. x = 'z,22,44,66,88,asd,asd,23,43,55' eval(x) NameError: name 'z' is not defined In worse case, somebody will send you a carefuly formated input that you will run blindy (just like in case of buffer overflows). CSV is easy with the module.. import csv cr = csv.reader((x,)) print cr.next() ['z', '22', '44', '66', '88', 'asd,asd', '23,43,55'] Usually, you will use the csv module, like this: import csv, sys for line in csv.reader(sys.stdin): # print line[3] BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: __del__ pattern?
Bryan Olson wrote: Use file that is writeable by A and B in a directory that is writeable only by root. Is that portable? I have the feeling that you are asking if it works on Windows. No idea! I have only user experience with Windows. On UNIX it is as portable as 'flock', which means all modern Unices (be careful about NFS). What's the sequence the program should try? 1. open a file, which name was previously agreed on (like /var/tmp/prog-name-user-name) If it fails, report error and exit. System error or somebody has created unaccessible file by the same name. 2. Try to aquire a flock on the descriptor from step 1. If it fails, some running process already has the lock, exit 3. lock will be released and lockfile closed automaticaly by OS on process exit. import sys, fcntl try: lockfile=open('/var/tmp/test1', 'w') fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: print sys.exc_info()[1] sys.exit(-1) You can flock any open file, no matter if it is read/write/append. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: List of string
Dennis Lee Bieber wrote: On Thu, 18 Aug 2005 13:30:45 +0200, Mohammed Altaj [EMAIL PROTECTED] declaimed the following in comp.lang.python: Thanks , but , this work for an ordered substrings , just like what we had ['0132442\n', '13\n', '24\n'] , I would like to remove all substrings from the list , example ['0134314244133', '132443', '234'] 2nd and 3rd strings are also substrings from the 1st one , so it should be removed One of us must be using a non-standard definition of substring as 234 does not appear anywhere within either 132443 or 0134314244133. I don't understand 234 either, but I can see some pattern in 132443. 132443 is a 'subsubstring' 0134314244133 because: 0134314244133 -#####-#- Maybe 234 is just a typo, and it should be 243. def subsubstr(a, b): if b == '': return True if a == '': return False else: if a[0] == b[0]: return subsubstr(a[1:], b[1:]) else: return subsubstr(a[1:], b) I can give you more efficient, index-based sulution, but this one looks nicer. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: extending: new type instance
Michael Hudson wrote: PyObject_New is the usual way, although there are others -- Thanks for an answer ! MyType.tp_new, PyObject_Call ... Does this mean, that I can call my newly defined MyType_new, MyType_init directly ?? I was afraid that Python might do some pretty important housekeeping stuff around them. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: determine variable type
Paul Rubin wrote: [EMAIL PROTECTED] writes: i tried using type(var) but that only seemed to produce a response in the command line. is there a built in python function to determine if a variable is an integer? type(var) returns the type. For example: if type(x) == type(3): print 'x is an integer' or x = 5 isinstance(x, int) True BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: Lists of list
Mohammed Altaj wrote: Hi All I am having problem with delete line if its belong to another one , example I think, you mean to remove all lines that are substrings of another line. l = ['0132442\n', '13\n', '24\n'] l = [e.strip() for e in l] i = 0 while True: try: for j in range(len(l)): if i == j: continue if l[j].find(l[i]) = 0: # line 'j' is superstring of line 'i' del l[i] break else: # doesn't have superstring i += 1 except IndexError: break Basically, I try all n*n combinations, and remove substring lines in-place. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
extending: new type instance
I'm writing my own (list-like) type in C. It is implementing a Sequence Protocol. In 'sq_slice' method I would like to return a new instance of my class/type. How do I create (and initialize) an instance of a given PyTypeObject MyType ? I have tried to provide (PyObject *)MyType as 'class' argument to: PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw) It failed the PyClass_Check. I have also found a couple of PyObject_New functions which accept PyTypeObject as an argument. They look very low-level and obviously don't call __init__. Should I do it myself manualy ? BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: get the return code when piping something to a python script?
mhenry1384 wrote: On WinXP, I am doing this nant.exe | python MyFilter.py How do I set the return code from MyFilter.py based on the return of nant.exe? Is this possible? I don't know how it is on WinXP, but in UNIX you IMHO cannot easily get the retcode of the peer _if_ the pipe was created by your common parent, the shell. Only the parent knows retcodes of its children. You can communicate the status over the pipe. E.g. when producer will successufuly finish it will write (let say) last line with only one '.' Didn't help you much.. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: __del__ pattern?
[EMAIL PROTECTED] wrote: For a reasonably portable solution, leave the lock file open. On most systems, you cannot delete an open file,.. On most UNIXes, you can delete an open file. Even flock-ed. This is BTW also an hack around flock. 1. Process A opens file /var/tmp/test1, and flocks descriptor. 2. Process H unlinks /var/tmp/test1 3. Process B opens file /var/tmp/test1, and flocks _another_ descriptor 4. Processes A and B are running simultaneously Do you need protection agains H ? Use file that is writeable by A and B in a directory that is writeable only by root. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: __del__ pattern?
So when my class is instantiated, I create a little lock file, and I have a __del__ method that deletes the lock file. Unfortunately, there seem to be some circumstances where my lock file is not getting deleted. Maybe the interpreter died by the signal.. in that case the __del__ is not called. You can try 'flock', instead of lock files. import fcntl class Test1(object): def __init__(self): self.lock=open('/var/tmp/test1', 'w') fcntl.flock(self.lock.fileno(), fcntl.LOCK_EX) print 'Lock aquired!' def __del__(self): fcntl.flock(self.lock.fileno(), fcntl.LOCK_UN) self.lock.close() In this case, if interpreter dies, the lock is released by OS. If you try to create another instance in the same interpreter or another, the call will block in __init__. You can change it to raise an exception instead. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: seeking Python developers
If anyone has ideas for how to find such people within this geographic area, I'd much appreciate suggestions.. Try to post it at http://www.python.org/Jobs.html BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: Using for in one-liner
Paul Watson wrote: Can a for loop be used in a one-liner? What am I missing? $ python -c import sys;for i in range(5): print i, File string, line 1 import sys;for i in range(5): print i, ^ SyntaxError: invalid syntax This was tricky.. python -c $'import sys;\nfor i in range(5): print i,' Separate statements with newline and enclose it in $'string'. BranoZ -- http://mail.python.org/mailman/listinfo/python-list
Re: Using for in one-liner
In man python Here command may contain multiple statements separated by newlines. Leading whitespace is significant in Python statements! In man bash search for \n (/\\n) Frankly, I know bash for 10 years, but this has surprised me, too. BranoZ -- http://mail.python.org/mailman/listinfo/python-list