Re: simple string question
On Sun, Sep 6, 2009 at 10:29 PM, jwither wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? There's probably a more general method covering all the escape sequences, but for just \n: your_string = your_string.replace("\\n", "\n") Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string question
On Sep 6, 11:29 pm, "jwither" wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? > > James Withers 1) What is a "parsed string"? 2) What is a "NEWLINE token"? -- http://mail.python.org/mailman/listinfo/python-list
Re: using python interpreters per thread in C++ program
On 7 Sep, 07:59, ganesh wrote: > No, I did not use GIL. > -- For using GIL, do we need to initialize GIL at startup and destroy/ > finalize it at end? > -- Are there any configuration & build related flags that I need to > use to make this work? > > Please guide. Thanks. I just showed you how... -- http://mail.python.org/mailman/listinfo/python-list
Re: using python interpreters per thread in C++ program
> Did you remeber to acquire the GIL? The GIL is global to the process > (hence the name). No, I did not use GIL. -- For using GIL, do we need to initialize GIL at startup and destroy/ finalize it at end? -- Are there any configuration & build related flags that I need to use to make this work? Please guide. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string question
On Sep 6, 10:29 pm, "jwither" wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? > > James Withers I believe "\n" is a newline. As is "\r\n" and "\r". Choose your demon. mystring = mystring.replace("\n", demon) FYI. If you are reading from a file, you can iterate over the lines without having to worry about newlines: fi = open(path_to_file, 'r') for line in fi: process_line(line) ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: using python interpreters per thread in C++ program
> Did you remeber to acquire the GIL? The GIL is global to the process No, I did not use GIL. -- Why do we need to use GIL even though python is private to each thread? -- For using GIL, do we need to initialize GIL at startup and destroy/ finalize it at end? -- With GIL, we are not achieiving concurrent operations on python interpreters across threads. When it comes to calling Py-C APIs, program is working like in a single threaded mode. Is there any way, we can avoid the GIL locking, etc? Please guide. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: using python interpreters per thread in C++ program
On 7 Sep, 07:17, grbgooglefan wrote: > Can we not use python interpreters even private to each multiple > thread? You can use multiple interpreters, but they share GIL. For example, Python extension modules are DLLs and will be loaded only once for each process - the OS makes sure of that. Since they are objects too, there can only be one GIL per process. The same goes for files: If an interpreter calls close on a file handle, it is closed for the whole process, not just locally to the interpreter. Global synchronization is therefore needed. (Maybe not for your threaded app, but for any conceivable use of Python.) If you need two isolated interpreters, you need to run them in different processes. -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
Dennis Lee Bieber wrote: On Sun, 06 Sep 2009 20:29:47 -0700, John Nagle declaimed the following in gmane.comp.python.general: Python has the advantage that a sizable fraction of its objects, especially the very common ones like numbers and strings, are immutable. Immutable objects We must have different ideas of "sizable" Numbers, strings, and tuples are immutable... Lists, dictionaries, and pretty much anything else (functions, class instances, etc.) are mutable in one way or another... I'd say the mutables are in the majority I think it depends on whether one counts classes or instances. Typical programs have a lot of numbers and strings. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBaseA?
Sorry, that last code had a typo in it: #!/usr/bin/python def main(): foox = FooX() fooy = FooY() fooz = FooZ() foox.method_x("I", "AM", "X") print fooy.method_x("ESTOY", "Y", "!") print fooz.method_x(100, 200, 300) class MyMixin(object): def method_x(self, a, b, c): super(MyMixin, self).method_x(a, b, c) print "MyMixin.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class BaseA(object): def method_x(self, a, b, c): super(BaseA, self).method_x(a, b, c) print "BaseA.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class BaseB(object): pass class BaseC(object): def method_x(self, a, b, c): super(BaseC, self).method_x(a, b, c) print "BaseC.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class FooX(BaseA, MyMixin): def method_x(self, a, b, c): super(FooX, self).method_x(a, b, c) print "FooX.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class FooY(BaseB, MyMixin): pass class FooZ(BaseC, MyMixin): def method_x(self, a, b, c): super(FooZ, self).method_x(a, b, c) print "FooZ.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) if __name__ == '__main__': main() Traceback (most recent call last): File "foo.py", line 54, in main() File "foo.py", line 8, in main foox.method_x("I", "AM", "X") File "foo.py", line 40, in method_x super(FooX, self).method_x(a, b, c) File "foo.py", line 24, in method_x super(BaseA, self).method_x(a, b, c) File "foo.py", line 18, in method_x super(MyMixin, self).method_x(a, b, c) AttributeError: 'super' object has no attribute 'method_x' -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBaseA?
On Sat, Sep 5, 2009 at 8:41 PM, Carl Banks wrote: > Out of curiosity, did you try this and are reporting that it resulted > in an AttributeError, or did you merely deduce that it would raise > AttributeError based on your knowledge of Python's inheritance? > > I ask this rhetorically. I know that you didn't try it (or that you > tried it and made a mistake) because if you had tried it (and not made > a mistake) you would have seen that it works exactly as you want it > to. > > > Carl Banks > -- > http://mail.python.org/mailman/listinfo/python-list > This code causes an error: #!/usr/bin/python def main(): foox = FooX() fooy = FooY() fooz = FooZ() #foox.method_x("I", "AM", "X") print fooy.method_x("ESTOY", "Y", "!") print fooz.method_x(100, 200, 300) class MyMixin(object): def method_x(self, a, b, c): super(MyMixin, self).method_x(a, b, c) print "MyMixin.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class BaseA(object): def method_x(self, a, b, c): super(BaseA, self).method_x(a, b, c) print "BaseA.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class BaseB(object): pass class BaseC(object): def method_x(self, a, b, c): super(BaseC, self).method_x(a, b, c) print "BaseC.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class FooX(BaseA, MyMixin): def method_x(self, a, b, c): super(FooX, self).method_x(a, b, c) print "FooX.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) class FooY(BaseB, MyMixin): pass class FooZ(BaseC, MyMixin): def method_x(self, a, b, c): super(FooZ, self).method_x(a, b, c) print "FooZ.method_x(%s, %s, %s, %s)" % (repr(self), repr(a), repr(b), repr(c)) if __name__ == '__main__': main() Traceback (most recent call last): File "foo.py", line 54, in main() File "foo.py", line 10, in main fooy.method_x("ESTOY", "Y", "!") File "foo.py", line 18, in method_x super(MyMixin, self).method_x(a, b, c) AttributeError: 'super' object has no attribute 'method_x' -- http://mail.python.org/mailman/listinfo/python-list
Re: using python interpreters per thread in C++ program
On 7 Sep, 07:17, grbgooglefan wrote: > What is best way to embed python in multi-threaded C++ application? Did you remeber to acquire the GIL? The GIL is global to the process (hence the name). void foobar(void) { PyGILState_STATE state = PyGILState_Ensure(); /* Safe to use Python C API here */ PyGILState_Release(state); } S.M. > Please guide. -- http://mail.python.org/mailman/listinfo/python-list
simple string question
Given a string (read from a file) which contains raw escape sequences, (specifically, slash n), what is the best way to convert that to a parsed string, where the escape sequence has been replaced (specifically, by a NEWLINE token)? James Withers -- http://mail.python.org/mailman/listinfo/python-list
using python interpreters per thread in C++ program
Hi I've a multi-threaded C++ program, in which I want to use embedded python interpreter for each thread. I am using Python 2.6.2 on Linux for this. When I tried to embed python interpreter per thread, I got crash when the threads were calling Python's C APIs. Can we not use python interpreters even private to each multiple thread? What is best way to embed python in multi-threaded C++ application? Please guide. -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
On Sun, 06 Sep 2009 10:12:56 -0400, Terry Reedy wrote: > Adam Skutt wrote: >> There's nothing inappropriate about using a lambda for a function I >> don't care to give a name. That's the entire reason they exist. > > But you did give a name -- 'b' -- and that is when a lambda expression > is inappropriate and when a def statement should be used instead I think that's too strong a claim. Functions are first class objects, and there no reason why you can't do this: def f(): return None g = f So what's wrong with doing this? g = lambda: None >>> The idea that Python has 'lambda objects' had caused no end of >>> mischief over the years. >> As near as I can tell, this is because you're insisting on creating a >> semantic distinction where there just isn't one. > > To the contrary, I am objecting to the spurious distinction 'lambda > object' as people often use it. Agreed. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
On Sun, 06 Sep 2009 06:18:23 -0700, Adam Skutt wrote: > On Sep 5, 7:38 pm, Steven D'Aprano > No. Lambdas are a *syntactical construct*, not an object. You wouldn't >> talk about "while objects" and "if objects" and "comment objects" >> *because they're not objects*. > This rhetoric precludes functions objects as well and is entirely non- > compelling. Functions ARE objects in Python. They even inherit from object: >>> def f(): ... return None ... >>> isinstance(f, object) True Just because there is syntax for creating functions (at least two different syntax forms actually) doesn't "preclude function objects". There is syntax for dicts, and dict objects; syntax for lists, and list objects; syntax for strings, and string objects. But there's syntax for while loops, and no such thing as a while object. Lambda expressions are syntax for creating function objects. That's all there is to it, end of story. >> Functions created with def and functions created with lambda are >> *precisely* the same type of object. > Which means you have lambda objects. The fact they're same as any other > function is irrelevant and not especially interesting. They're *function* objects, not "lambda" objects: >>> type(lambda: None) >> There is no such thing as a "lambda >> object" which is a "special case" of ordinary functions, there are just >> functions. > Hey, I was just trying to resolve tjr's view, he seemed to think that > .__name__ is different is pretty important, and he's the one you should > take your objections up with, not me. Nice try but no. It was YOU, not Terry, claiming that lambda's are a special kind of object different from ordinary functions. -- Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
George Burdell writes: > I want to find every occurrence of "money," and for each occurrence, I > want to scan back to the first occurrence of "hello." How can this be > done? By recognising the task: not expression matching, but lexing and parsing. For which you might find the ‘pyparsing’ library of use http://pyparsing.wikispaces.com/>. -- \ “Science is a way of trying not to fool yourself. The first | `\ principle is that you must not fool yourself, and you are the | _o__) easiest person to fool.” —Richard P. Feynman, 1964 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
George Burdell wrote: On Sep 6, 10:06 pm, "Mark Tolonen" wrote: wrote in message news:f98a6057-c35f-4843-9efb-7f36b05b6...@g19g2000yqo.googlegroups.com... If I do this: import re a=re.search(r'hello.*?money', 'hello how are you hello funny money') I would expect a.group(0) to be "hello funny money", since .*? is a non-greedy match. But instead, I get the whole sentence, "hello how are you hello funny money". Is this expected behavior? How can I specify the correct regexp so that I get "hello funny money" ? A non-greedy match matches the fewest characters before matching the text *after* the non-greedy match. For example: import re a=re.search(r'hello.*?money','hello how are you hello funny money and more money') a.group(0) # non-greedy stops at the first money 'hello how are you hello funny money'>>> a=re.search(r'hello.*money','hello how are you hello funny money and more money') a.group(0) # greedy keeps going to the last money 'hello how are you hello funny money and more money' This is why it is difficult to use regular expressions to match nested objects like parentheses or XML tags. In your case you'll need something extra to not match the first hello. a=re.search(r'(? 'hello funny money' -Mark I see now. I also understand r's response. But what if there are many "hello"'s before "money," and I don't know how many there are? In other words, I want to find every occurrence of "money," and for each occurrence, I want to scan back to the first occurrence of "hello." How can this be done? This is asking for more power then regular expressions can support. However, your request reads like an algorithm. Search for an occurrence of "hello" (using the find string method), and search backwards from there for "money" (use rfind string method). Two lines of code in a loop should do it. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
On Sep 6, 10:06 pm, "Mark Tolonen" wrote: > wrote in message > > news:f98a6057-c35f-4843-9efb-7f36b05b6...@g19g2000yqo.googlegroups.com... > > > If I do this: > > > import re > > a=re.search(r'hello.*?money', 'hello how are you hello funny money') > > > I would expect a.group(0) to be "hello funny money", since .*? is a > > non-greedy match. But instead, I get the whole sentence, "hello how > > are you hello funny money". > > > Is this expected behavior? How can I specify the correct regexp so > > that I get "hello funny money" ? > > A non-greedy match matches the fewest characters before matching the text > *after* the non-greedy match. For example: > > >>> import re > >>> a=re.search(r'hello.*?money','hello how are you hello funny money and > >>> more money') > >>> a.group(0) # non-greedy stops at the first money > > 'hello how are you hello funny money'>>> a=re.search(r'hello.*money','hello > how are you hello funny money and > >>> more money') > >>> a.group(0) # greedy keeps going to the last money > > 'hello how are you hello funny money and more money' > > This is why it is difficult to use regular expressions to match nested > objects like parentheses or XML tags. In your case you'll need something > extra to not match the first hello. > > >>> a=re.search(r'(? >>> money') > >>> a.group(0) > > 'hello funny money' > > -Mark I see now. I also understand r's response. But what if there are many "hello"'s before "money," and I don't know how many there are? In other words, I want to find every occurrence of "money," and for each occurrence, I want to scan in the reverse (left) direction to the closest occurrence of "hello." How can this be done? -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
Bearophile wrote: John Nagle: The concept here is that objects have an "owner", which is either a thread or some synchronized object. Locking is at the "owner" level. This is simple until "ownership" needs to be transferred. Can this be made to work in a Pythonic way, without explicit syntax? What we want to happen, somehow, is to transfer the ownership of "words" from the calling thread to the object in "putitem", and transfer it to the calling thread in "getitem". How can this be done? There are several people that have found ways to implement such owning of variables, for example Bartosz Milewski: http://bartoszmilewski.wordpress.com/2009/06/02/race-free-multithreading-ownership/ It requires some extra complexities, so statically typed languages usually don't implement this idea, even if it avoids bugs in user code. Implementing it in Python in a simple enough way looks like a good topic for advanced research :-) I hadn't seen that implementation for D. That's an interesting idea. I'm certainly not the first to go down this road. But nobody seems to have thought hard about this for Python yet. I looked at this years ago for C++, but you get bogged down in the same problems that keep C++ "smart pointer" implementations from being airtight. In a static language like D, it takes a lot of declarations to make this go. In Python, more of the ownership decisions have to be implicit, to keep to a declaration-free Pythonic style. That article has a policy that "every object has one owner, for life". That allows compile-time checking, but it's very limiting. You can't, for example, pass an array into a member function of a synchronized object as a parameter and have the object keep a reference to it. (That's what "Queue" does. for example.) Milewski would have you make a copy in that situation. I felt that was too restrictive, but if you make that restriction, it cuts down on overhead and simplifies the implementation. The toughest part of this approach is doing a handoff from one owner to another. Python has the advantage that a sizable fraction of its objects, especially the very common ones like numbers and strings, are immutable. Immutable objects can be shared without problems (other than storage management, but there are ways to handle that.) So the programmer doesn't have to obsess over "who owns a string" when a string parameter is passed to a function. So at least we don't have to sweat the ownership issue for little stuff. It's worth looking at this for Python because CPython's current thread model behaves terribly on multiprocessors. We really do need something better. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
On Sep 6, 10:22 pm, George Burdell wrote: > On Sep 6, 10:06 pm, "Mark Tolonen" wrote: > > > > > > > wrote in message > > >news:f98a6057-c35f-4843-9efb-7f36b05b6...@g19g2000yqo.googlegroups.com... > > > > If I do this: > > > > import re > > > a=re.search(r'hello.*?money', 'hello how are you hello funny money') > > > > I would expect a.group(0) to be "hello funny money", since .*? is a > > > non-greedy match. But instead, I get the whole sentence, "hello how > > > are you hello funny money". > > > > Is this expected behavior? How can I specify the correct regexp so > > > that I get "hello funny money" ? > > > A non-greedy match matches the fewest characters before matching the text > > *after* the non-greedy match. For example: > > > >>> import re > > >>> a=re.search(r'hello.*?money','hello how are you hello funny money and > > >>> more money') > > >>> a.group(0) # non-greedy stops at the first money > > > 'hello how are you hello funny money'>>> a=re.search(r'hello.*money','hello > > how are you hello funny money and > > >>> more money') > > >>> a.group(0) # greedy keeps going to the last money > > > 'hello how are you hello funny money and more money' > > > This is why it is difficult to use regular expressions to match nested > > objects like parentheses or XML tags. In your case you'll need something > > extra to not match the first hello. > > > >>> a=re.search(r'(? > >>> money') > > >>> a.group(0) > > > 'hello funny money' > > > -Mark > > I see now. I also understand r's response. But what if there are many > "hello"'s before "money," and I don't know how many there are? In > other words, I want to find every occurrence of "money," and for each > occurrence, I want to scan back to the first occurrence of "hello." > How can this be done? I should say "closet" occurrence of "hello," to be more clear. -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
On Sep 6, 10:06 pm, "Mark Tolonen" wrote: > wrote in message > > news:f98a6057-c35f-4843-9efb-7f36b05b6...@g19g2000yqo.googlegroups.com... > > > If I do this: > > > import re > > a=re.search(r'hello.*?money', 'hello how are you hello funny money') > > > I would expect a.group(0) to be "hello funny money", since .*? is a > > non-greedy match. But instead, I get the whole sentence, "hello how > > are you hello funny money". > > > Is this expected behavior? How can I specify the correct regexp so > > that I get "hello funny money" ? > > A non-greedy match matches the fewest characters before matching the text > *after* the non-greedy match. For example: > > >>> import re > >>> a=re.search(r'hello.*?money','hello how are you hello funny money and > >>> more money') > >>> a.group(0) # non-greedy stops at the first money > > 'hello how are you hello funny money'>>> a=re.search(r'hello.*money','hello > how are you hello funny money and > >>> more money') > >>> a.group(0) # greedy keeps going to the last money > > 'hello how are you hello funny money and more money' > > This is why it is difficult to use regular expressions to match nested > objects like parentheses or XML tags. In your case you'll need something > extra to not match the first hello. > > >>> a=re.search(r'(? >>> money') > >>> a.group(0) > > 'hello funny money' > > -Mark I see now. I also understand r's response. But what if there are many "hello"'s before "money," and I don't know how many there are? In other words, I want to find every occurrence of "money," and for each occurrence, I want to scan back to the first occurrence of "hello." How can this be done? -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
EDIT: your regex matches the whole string because it means... "hello" followed by any number of *anythings* up to the first occurrence of "money") you see? -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
wrote in message news:f98a6057-c35f-4843-9efb-7f36b05b6...@g19g2000yqo.googlegroups.com... If I do this: import re a=re.search(r'hello.*?money', 'hello how are you hello funny money') I would expect a.group(0) to be "hello funny money", since .*? is a non-greedy match. But instead, I get the whole sentence, "hello how are you hello funny money". Is this expected behavior? How can I specify the correct regexp so that I get "hello funny money" ? A non-greedy match matches the fewest characters before matching the text *after* the non-greedy match. For example: import re a=re.search(r'hello.*?money','hello how are you hello funny money and more money') a.group(0) # non-greedy stops at the first money 'hello how are you hello funny money' a=re.search(r'hello.*money','hello how are you hello funny money and more money') a.group(0) # greedy keeps going to the last money 'hello how are you hello funny money and more money' This is why it is difficult to use regular expressions to match nested objects like parentheses or XML tags. In your case you'll need something extra to not match the first hello. a=re.search(r'(?money') a.group(0) 'hello funny money' -Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: Something confusing about non-greedy reg exp match
On Sep 6, 9:46 pm, "gburde...@gmail.com" wrote: > If I do this: > > import re > a=re.search(r'hello.*?money', 'hello how are you hello funny money') > > I would expect a.group(0) to be "hello funny money", since .*? is a > non-greedy match. But instead, I get the whole sentence, "hello how > are you hello funny money". > > Is this expected behavior? How can I specify the correct regexp so > that I get "hello funny money" ? heres one way, but it depends greatly on the actual pattern you seek... re.search(r'hello \w+ money', 'hello how are you hello funny money').group() your regex matches the whole string because it means ("hello" followed by any number of *anythings* up to "money") you see? wisdom = get_enlightend(http://jjsenlightenments.blogspot.com/) -- http://mail.python.org/mailman/listinfo/python-list
Re: Evil trend report - cancelled
Steven D'Aprano wrote: On Sat, 05 Sep 2009 22:43:17 -0700, John Nagle wrote: [snip] Test complete: Evil trend report Accidentally posted a private e-mail. Cancelled. Sorry. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Module for Fisher's exact test?
Before I roll my own, is there a good Python module for computing the Fisher's exact test stastics on 2 x 2 contingency tables? Many thanks in advance, Gabe -- http://mail.python.org/mailman/listinfo/python-list
Something confusing about non-greedy reg exp match
If I do this: import re a=re.search(r'hello.*?money', 'hello how are you hello funny money') I would expect a.group(0) to be "hello funny money", since .*? is a non-greedy match. But instead, I get the whole sentence, "hello how are you hello funny money". Is this expected behavior? How can I specify the correct regexp so that I get "hello funny money" ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Q on explicitly calling file.close
On Sun, Sep 6, 2009 at 4:31 PM, r wrote: > On Sep 6, 1:14 pm, "Jan Kaliszewski" wrote: > > 05-09-2009 r wrote: > > > i find the with statement (while quite useful in general > > > practice) is not a "cure all" for situations that need and exception > > > caught. > > > > In what sense? > > *ahem*! in the sense that the with statement (while quite useful in > general practice) is not a "cure all" for situations that need and > exception caught ;-) But what does that even mean? What's the -problem- that it isn't 'curing'? That 'with' doesn't completely replace all uses of 'try'? It was never meant to-- that seems like a completely different problem area. But, if you do want it to handle exception catching, that's just a question of making a context manager that does so. import contextlib @contextlib.contextmanager def file_with_errorhandling(filename, mode, exceptions=(IOError,OSError)): fp = file(filename, mode) try: yield fp except exceptions: print "Handle file-operation errors gracefully here." fp.close() with file_with_errorhandling(filename, 'r') as fp: fp.read() # or whatever True, the context manager provided by file objects just lets exceptions propagate up but that's usually the desired behavior. You can make context managers for particular problems you run across that handle exceptions if you want. The with statement isn't about never having to type try again, I don't think. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Is "#!/usr/bin/env python" the better shebang line ?
Ned Deily wrote: In article <4aa3bfdf$0$282$14726...@news.sunsite.dk>, Timothy Madden wrote: My questions is if I should use #!/usr/bin/env python as the shebang line in a portable and open python script and if it does help with portability and usage. This question came up recently on stackoverflow (alas, will result in urloverflow): http://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supp osedly-more-correct-than-just-usr-bin-pyth/1352938#1352938 Ok, thank you. There is quite a flame since May 2008 archived on mail.python.org about this issue. #!/usr/bin/env python is clearly the choice for my scripts. It helps a lot with portability, and the arguments against it are based on the idea that /usr/bin/python is somehow better than /opt/python and that /opt/python is still in front of /usr/bin/python in path. Such an argument I do not find compelling. The thing is that people writing large applications for many systems need flexibility first. Timothy Madden -- http://mail.python.org/mailman/listinfo/python-list
Re: Evil trend report
On Sep 6, 8:50 am, Grant Edwards wrote: > On 2009-09-06, John Nagle wrote: > > > > > Bing > > A 3 2.4% () > > A 1 0.8% (non_commercial) > > Q 50 40.0% () > > Q 15 12.0% (no_location) > > U 5 4.0% (no_website) > > U 33 26.4% (non_commercial) > > X 1 0.8% (negative_info) > > X 17 13.6% (no_location) > > > Google > > A 1 0.8% () > > A 4 3.3% (non_commercial) > > Q 46 38.3% () > > Q 20 16.7% (no_location) > > Q 1 0.8% (non_commercial) > > U 4 3.3% (no_website) > > U 28 23.3% (non_commercial) > > X 16 13.3% (no_location) > > Test complete: Evil trend report > > I've absolutely no clue what those tables are supposed to > represent (well, I do know what Bing and Google are, but beyond > that...). I think it's pretty obvious, Grant. Clearly, for the second of the two U's, Bing has a 33, while Google only has a 28. I mean, Google doesn't even HAVE a 2nd X (the so-called "negative_info" that we've all heard about). I haven't seen numbers and letters like this in a long time, let me tell you. CM -- http://mail.python.org/mailman/listinfo/python-list
Re: possible attribute-oriented class
06-09-2009 o 20:20:21 Ethan Furman wrote: In the dbf module I wrote, I use both the attribute access and the key lookup. The attribute access is great for interactive use, and for all the routines that play with the tables we have at work, where all the field names are indeed known at compile (aka coding) time. On the other hand, some routines don't know which fields they'll mucking about with, and so the key access is vital for them. Of course, I could have done the whole thing using key access, and I did have to impose some restrictions on method names so they wouldn't clash with possible field names, but I love being able to type current_record.full_name == last_record.full_name instead of current_record['full_name'] == last_record['full_name'] Me too, and I suppose many people too... The latter: * makes your code less readable if there is high density of such expressions; * makes typing much more strenuous/irritating -- what is not very important in case of advanced development (when time of typing is short in relation to time of thinking/reading/testing) but becomes quite important in case of scripting (which is still important area of Python usage). -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/listinfo/python-list
Re: Q on explicitly calling file.close
On Sep 6, 1:14 pm, "Jan Kaliszewski" wrote: > 05-09-2009 r wrote: > > i find the with statement (while quite useful in general > > practice) is not a "cure all" for situations that need and exception > > caught. > > In what sense? *ahem*! in the sense that the with statement (while quite useful in general practice) is not a "cure all" for situations that need and exception caught ;-) > I think that: > > with open(...) as f: > foo... > > is equivalent to: > > f = open(...) > try: > foo... > finally: > f.close() > > Obviously it doesn't substitute catching with 'except' My sentiments exactly...? Get Enlightened -> http://jjsenlightenments.blogspot.com/ -- http://mail.python.org/mailman/listinfo/python-list
xlutils 1.4.1 released!
Hi All, I'm pleased to announce a new release of xlutils. This package is a small collection of utilities that make use of both xlrd and xlwt to process Microsoft Excel files. This release includes memory and speed enhancements for xlutils.filter and xlutils.copy. To find out more, please read here: http://www.simplistix.co.uk/software/python/xlutils In case you're not aware, xlrd and xlwt are two excellent pure-python libraries for reading and writing Excel files. They run on any platform and, likely, any implementation of Python without the need for horrific things like binding to Excel via COM and so needing a Windows machine. If you use any of xlrd, xlwt or xlutils, the following google group will be of use: http://groups.google.com.au/group/python-excel Hope some of this is of interest, I'd love to hear from anyone who ends up using it! cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: possible attribute-oriented class
Steven D'Aprano wrote: On Fri, 04 Sep 2009 22:51:39 -0700, Ken Newton wrote: I would think this is much more than just copy from other language styles or 'just' a syntax change -- the apparent widespread use would hint at a deeper need. "Apparent" is the key word there. There are lots of people who *say* this this useful functionality, but how many of them *actually* use it? And of those who do use it, how many of them know what they're doing? There are an awful lot of bad programmers out there. In the dbf module I wrote, I use both the attribute access and the key lookup. The attribute access is great for interactive use, and for all the routines that play with the tables we have at work, where all the field names are indeed known at compile (aka coding) time. On the other hand, some routines don't know which fields they'll mucking about with, and so the key access is vital for them. Of course, I could have done the whole thing using key access, and I did have to impose some restrictions on method names so they wouldn't clash with possible field names, but I love being able to type current_record.full_name == last_record.full_name instead of current_record['full_name'] == last_record['full_name'] and it's much easier on my wrists, too. Hopefully-not-a-bad-programmer-ly yours, ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
TestFixtures 1.6.1 released!
Hi All, I'm pleased to announce a new release of TestFixtures. This package is a collection of helpers and mock objects that are useful when writing unit tests or doc tests. This release sees the following changes: - @replace and Replacer.replace can now replace attributes that may not be present, provided the `strict` parameter is passed as False. - should_raise now catches BaseException rather than Exception so raising of SystemExit and KeyboardInterrupt can be tested. To find out more, please read here: http://pypi.python.org/pypi/testfixtures cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: Is "#!/usr/bin/env python" the better shebang line ?
In article <4aa3bfdf$0$282$14726...@news.sunsite.dk>, Timothy Madden wrote: > My questions is if I should use >#!/usr/bin/env python > as the shebang line in a portable and open python script and if it does > help with portability and usage. This question came up recently on stackoverflow (alas, will result in urloverflow): http://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supp osedly-more-correct-than-just-usr-bin-pyth/1352938#1352938 -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
standard way to search through pdf-documents
Hi there, I'm interested in searching through a number of pdf-documents by script. I found in the internet one project named PdfSearchGui-0.3 which should be ready for this task. But I always fail because of the following error: Traceback (most recent call last): File "main.py", line 3, in from manager import conlongdos as conlongdos File "C:\ Trainings\PDF\PdfSearchGui-0.3\manager.py", line 3, in from indexer import Indexer File "C: \Trainings\PDF\PdfSearchGui-0.3\indexer.py", line 14, in from lupy.analysis.simpleanalyzer import SimpleAnalyzer ImportError: No module named analysis.simpleanalyzer My question is: does anybody know this problem and can provide me a solution, work around etc.? The version of this modul (lupy) does not contain any package like analysis.simpleanalyser Or is there any other maybe standard approch for searching through pdf- documents?? This problem makes me lupy. thanks in advance christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Q on explicitly calling file.close
05-09-2009 r wrote: i find the with statement (while quite useful in general practice) is not a "cure all" for situations that need and exception caught. In what sense? I think that: with open(...) as f: foo... is equivalent to: f = open(...) try: foo... finally: f.close() Obviously it doesn't substitute catching with 'except', but I don't see how it could disturb that. Cheers, *j -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/listinfo/python-list
Re: platform-specific overrides of functions and class methods (expanding on imputils demo code)
lkcl wrote: On Aug 21, 12:58 am, a...@pythoncraft.com (Aahz) wrote: In article <77715735-2668-43e7-95da-c91d175b3...@z31g2000yqd.googlegroups.com>, lkcl wrote: if somebody would like to add this to the python bugtracker, as a contribution, that would be great. alternatively, you might like to have a word with the python developers to get them to remove the censorship on my contributions. Excuse me? What censorship? i was ordered to cease contributing to python. As I remember from the public discussion, you were asked to cease using the bugtracker as a progress blog ... the cause was the provision of a port of python to run under mingw32, which passed all but eight of the regression tests. and wait until you had a complete patch to submit. There is also a real question of whether such ports should be folded into the main CPython code base, as some have been, or maintained as separate projects, as other have been. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Q on explicitly calling file.close
> > It's just too bad that 'with' doesn't support multiple separate "x as y" >> clauses. >> > > The developers already agreed with you ;-). > > "With more than one item, the context managers are processed as if multiple > with statements were nested: > > with A() as a, B() as b: >suite > is equivalent to > > with A() as a: >with B() as b: >suite > Changed in version 3.1: Support for multiple context expressions. > " > Doh! I hadn't noticed that. Alas, I'm /just barely now/ moving to Python 2.5, I won't be able to even consider getting near Python 3 for -- a year or so, I'm expecting. If ever! (I suspect this will also be in 2.7) > Ah, that's a beacon of hope. --S -- http://mail.python.org/mailman/listinfo/python-list
How to debug a forked process with pdb ?
Hello I am trying to write a daemon and I call os.fork() twice to detach from the terminal and other staff. Problem is after the second fork() the child immediately gives an exception and although I get a traceback displayed the process should terminate, the child proces is still live, and its parent is waiting in a loop for the child to either exit or send SIGUSR1 to it to indicate the daemon has started up and is in the running state. The problem is the daemon script has terminated with an uncaught exception and a traceback displayed, but the process is still hanging there. Do you know why would it not exit ? Trying to kill the daemon process, even as root, fails silently. Do you know why that would happen ? I would like to debug the scrip with python -m pdb pikantBlue.py but after the first fork() call the debugger prompt is unusuable. Probably the same prompt is shared by two instances of pdb, that have forked once with my script. Do you know how can I debug a script that forks ? You have my script attached if you want to look it over. The child failes and somehow freezes at the import line at the end of the file, and its parent is livelocked in the while loop. Thank you, Timothy Madden --- #!/usr/bin/env python import sys, os, signal, time from pikantBlueServer import PIDFile as PIDFile, pid_file as pid_file serverRunning = False def serverRunningSignal(usrSignal, stackFrame): global serverRunning if (usrSignal == signal.SIGUSR1): serverRunning = True wait_message = False def waitAlarm(sigAlarm, stackFrame): global wait_message sys.stdout.write('Starting daemon ... ') wait_message = True def daemonize(loop, fileLst): exitCode = False sys.stdin.flush() sys.stdout.flush() sys.stderr.flush() pid = os.fork() if pid > 0: # the main process # # returns with the exit code of the proxy child print 'Proxy proces' , pid exitCode = (os.waitpid(pid,0) == 0) else: # the proxy child # # sets cwd, umask and sid, forks the server # waits for either it's termination or the ready signal os.setsid() signal.signal(signal.SIGUSR1, serverRunningSignal) sys.stdin.flush() sys.stdout.flush() sys.stderr.flush() pid = os.fork() if pid > 0: #proxy process waits for termination of the child or the #ready signal print 'Daemon process', pid signal.signal(signal.SIGALRM, waitAlarm) signal.alarm(1) waitPid = True global serverRunning if not serverRunning: try: os.kill(pid, 0) except OSError, e: if hasattr(e, 'errno') and e.errno == errno.ESRCH: waitPid = False else: raise while waitPid and not serverRunning: time.sleep(10) if not serverRunning: try: sys.stdout.write('Checking child process ' + str(pid) + ' ... ') os.kill(pid, 0) print 'live.' except OSError, e: if hasattr(e, 'errno') and e.errno == errno.ESRCH: waitPid = False print 'dead' else: raise signal.alarm(0) global wait_message if waitPid and wait_message: print 'done' exitCode = serverRunning else: # the server process # signal.signal(signal.SIGUSR1, signal.SIG_DFL) os.chdir('/') os.umask(0) try: import pikantBlueSever exitCode = pikantBlueServer.start(loop, fileLst) except: exitCode = False return exitCode -- http://mail.python.org/mailman/listinfo/python-list
Re: Support for Windows 7 ?
On Sep 5, 4:45 pm, Pascale Mourier wrote: > YES IT IS! Sorry for the inconvenience. I usually start from this > assumption. Yesterday this new student was really agressive, and I > assumed he was right! > I suggest that (in general) you don't allow the first clause of this last sentence to lead to the second clause... J^n -- http://mail.python.org/mailman/listinfo/python-list
Re: platform-specific overrides of functions and class methods (expanding on imputils demo code)
On Aug 21, 12:58 am, a...@pythoncraft.com (Aahz) wrote: > In article > <77715735-2668-43e7-95da-c91d175b3...@z31g2000yqd.googlegroups.com>, > > lkcl wrote: > > >if somebody would like to add this to the python bugtracker, as a > >contribution, that would be great. alternatively, you might like to > >have a word with the python developers to get them to remove the > >censorship on my contributions. > > Excuse me? What censorship? i was ordered to cease contributing to python. the cause was the provision of a port of python to run under mingw32, which passed all but eight of the regression tests. l. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python or ActionScript 3.0
On Sep 6, 3:19 pm, lkcl wrote: > On Aug 16, 1:29 am, Douglas Alan wrote: > > I think the future of client-side browser programming is > > actuallyJavaScript, not ActionScript, though that future may morph into one > > that mostly usesJavaScriptas a virtual machine. This is the approach > > that Google Web Toolkit takes. It lets you write your client-side code > > in Java, and that is then compiled intoJavaScript. > > as does pyjamas.http://pyjs.org > this also compiles intojavascript. > the source language: python. oh - i forgot: there's skulpt as well. http://code.google.com/p/skulpt. skulpt aims to be a python interpreter (written in javascript) first, with an aim to be a browser-based UI toolkit second. pyjamas is the other way round (because it's more useful that way). skulpt's current python compatibility, as a less mature project, is not as good as pyjamas' "--strict" mode, where we have metaclasses and a near-full implementation of type() etc. it's fair to say that the more "python-correct" you get, the more dreadful the performance of the resultant javascript. strict typechecked prototypes (in python) a la lisp and a la compiled languages would open the floodgates to keep the same performance as the pyjamas -O option. but - basically, both projects demonstrate that the features of each language are at least interchangeable. this comes as somethin of a surprise to many people. java on the other hand is dreadful. the typechecking _seriously_ impedes progress, as the pypy developers found out and can tell you more about. l. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python or ActionScript 3.0
On Aug 15, 9:32 pm, Jaseem wrote: > Hi, > > Is python similar to actionscript 3.0 > Which is better to create a rich gui internet application? can i suggest that you read this: http://www.javalobby.org/articles/ajax-ria-overview/ and then take a look at this: http://pyjs.org pyjamas puts python onto the RIA roadmap as far as browsers are concerned, and actually makes your question "make sense". without pyjamas, your question simply wouldn't make any sense, because without pyjamas, it is impossible to "program" a web browser in the way that you envisage, leaving (as the other posters point out) either some plugins or some server-side-only options. l. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python or ActionScript 3.0
On Aug 16, 1:29 am, Douglas Alan wrote: > > But both python and AS 3.0 is almost identical. > > No, Python and ActionScript are not "almost identical". the AS 3.0 implementation is entirely missing declarative style of programming: it is purely event-driven. i.e. you cannot get an AS 3.0 "command prompt" and start executing code, you can _only_ do "stuff" as fired off and in reaction to user-driven events [in an adobe flash/ AIR application]. > it would be impossible to say which one is more similar to Lisp. In > general, Python is in my opinion more pleasant to program in than > ActionScript, but Python is not generally used for client-side browser > code. except by pyjamas developers. http://pyjs.org > I think the future of client-side browser programming is actuallyJavaScript, > not ActionScript, though that future may morph into one > that mostly usesJavaScriptas a virtual machine. This is the approach > that Google Web Toolkit takes. It lets you write your client-side code > in Java, and that is then compiled intoJavaScript. as does pyjamas. http://pyjs.org this also compiles into javascript. the source language: python. l. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python or ActionScript 3.0
On Aug 16, 5:43 am, "Michel Claveau - MVP" wrote: > Hi! > > > Python doesn't run in your typical web browser > > Yes, Python can do it... on Windows. and linux. pyxpcomext. it's a bit of a pig, but perfectly doable: http://pyxpcomext.mozdev.org/tutorials.html > Two (examples) ways: > - ActiveScripting (PythonScript), include in PyWin32 > - Gestalt (who mix Python, Ruby &Javascript, via Silverlight) michael, could you kindly update this page: http://wiki.python.org/moin/WebBrowserProgramming > And alse, these two solutions run OK in HTA (HTml Application) for use HTML > as GUI tool in local applications. > > @-salutations > -- > Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: Python- javascript
On Aug 16, 12:02 am, Mike Paul wrote: > I'm trying to scrap a dynamic page with lot ofjavascriptin it. > Inorder to get all the data from the page i need to access thejavascript. But > i've no idea how to do it. > > Say I'm scraping some site htttp://www.xyz.com/xyz > > request=urllib2.Request("htttp://www.xyz.com/xyz") > response=urllib2.urlopen(request) > data=response.read() > > So i get all the data on the initial page. Now i need to access > thejavascripton this page to get additional details. I've heard someone > telling me to use spidermonkey. But no idea on how to send javscript > as request and get the response. How hsuld i be sending thejavascript > request as ? how can it be sent? you need to actually _execute_ the web page under a browser engine. you will not be able to do what you want, using urllib. > Can anyone tell me how can i do it very clearly. I've been breaking my > head into this for the past few days with no progress. there are about four or five engines that you can use, depending on the target platform. see: http://wiki.python.org/moin/WebBrowserProgramming 1) python-khtml (pykhtml). 2) pywebkitgtk (with DOM / glib-gobject bindings patches) 3) python-hulahop and xulrunner 4) Trident (the MSHTML engine behind IE) accessed through python comtypes 5) macosx objective-c bindings and use pyobjc from there. options 2-4 i have successfully used and proven that it can be done: http://pyjamas.svn.sourceforge.net/viewvc/pyjamas/trunk/pyjd/ option 1) i haven't done due to an obscure bug in the KDE KHTML python- c++ bindings; option 2) i haven't done because there's no point: XMLHttpRequest has been deliberately excluded due to short-sightedness of the webkit developers, which has only recently been corrected (but the work still needs to be done). so, using a web browser engine, you must load and execute the page, and then you can use DOM manipulation to extract the web page text, after a certain amount of time has elapsed, and the javascript has completed execution. if you _really_ want to create your own javascript execution engine, which, my god it will be a hell of a lot of work but would be extremely beneficial, you would do well to help flier liu with pyv8, and paul bonser with pybrowser. flier is doing a web-site-scraping system, browsing millions of pages and executing the javascript under pyv8. paul is implementing a web browser in pure python (using python-cairo as the graphics engine). he's got part-way through the project, having focussed initially on a W3C standards-compliant implementation of the DOM, and less on the graphics side. that means that what paul has will be somewhat more suited to what you need, because you don't want a graphics engine at all. if paul's work isn't suitable, then the above engines you will simply have to run _without_ firing up the actual GUI window. in the GTK- based engines, you just... don't call show() or show_all(); in the MSHTML-based one, i presume you just don't fire a WM_SHOW event at it. you'll work it out. l. -- http://mail.python.org/mailman/listinfo/python-list
HTTPS on Twisted
Hi, For a financial application, I am creating a python tool which uses HTTPS to transfer the data from client to server. Now, everything works perfectly, since the SSL support comes free with Twisted. I have one problem though. As an upgrade, now, I have to send many requests as the same client to the server. Many in the range of >10 msgs every second. Now, I am creating a separate TCP connection for each and am sending the data. Is it possible to create just one SSL and TCP connection and send each message over that using Twisted? I read through Twisted, but was unable to come up with the answer though. Even if I have to use multiple TCP connections, is it possible to have just one SSL connection? I think persistent connections should be possible for TCP, but is it possible is Twisted? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is "#!/usr/bin/env python" the better shebang line ?
On Sun, Sep 6, 2009 at 10:01 AM, Timothy Madden wrote: > Hello > > Sorry if this has been discussed before, my search did not find it. > My questions is if I should use > #!/usr/bin/env python > as the shebang line in a portable and open python script and if it does help > with portability and usage. > > First, can one not find /usr/bin/python in any standard system, at least as > much as /usr/bin/env can be found ? > Not necessarily. The system python (if it exists) is usually in /usr/bin but what if there is no system-installed Python or the user would prefer to use a version they compiled themselves and is therefore in /usr/local/bin? Or what if the system decides /usr/bin is just for the default system tools and everything else should be installed in /opt/? env will always be in the same place, but Python may not be so using env makes it more portable. > Then, supposing /usr/bin/env is better than /usr/bin/python and I use it, is > it not the case that many editors and code analysing programs, that want to > know the script language for syntax highlighting, code completion, tags and > source code browsing, etc, will loose their functionality because of this > shebang line ? Is this not a good general reason to use the old one ? > > Now I know env is POSIX standard utility, and python is not, and maybe that > could be reason enough, but are there any other issues involved with using > /usr/bin/env ? > > Thank you > Timothy Madden > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to access ODBC databases ?
Martin P. Hellwig wrote: Timothy Madden wrote: [...] It has been a couple of years, but I remember vaguely that back in the days of PossgreSQL 6, if you want ODBC support you needed to compile PG a bit different then normal, I am not really sure what options those where and if this still applies, I decided to not use ODBC because it resulted in an undesirable effect somewhere else. Though I am not sure if that is still the case. Sorry to hear that. I think ODBC is an important step towards DBMS-independent applications, or even database applications portable over DBMSs. Yes, I know, everyone says such applications are so far from reality that they are not worth any effort trying. I still consider that ODBC is at least a step, and I am only sorry PostgreSQL has a poor ODBC driver (no large objects natively). Python also has a unified independent database interface with the DB API, and so does php and maybe other products. Thank you for your help, Timothy Madden -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
Adam Skutt wrote: On Sep 5, 10:34 pm, Terry Reedy wrote: Adam Skutt wrote: On Sep 5, 11:29 am, Terry Reedy wrote: This is a pointless replacement for 'def b(x): return x+a' And? That has nothing to do with anything I was saying whatsoever. Agreed. However, posts are read by newbies. Posts that promote bad habits are fair game for comment. There's nothing inappropriate about using a lambda for a function I don't care to give a name. That's the entire reason they exist. But you did give a name -- 'b' -- and that is when a lambda expression is inappropriate and when a def statement should be used instead The idea that Python has 'lambda objects' had caused no end of mischief over the years. As near as I can tell, this is because you're insisting on creating a semantic distinction where there just isn't one. To the contrary, I am objecting to the spurious distinction 'lambda object' as people often use it. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Q on explicitly calling file.close
Stephen Hansen wrote: This is precisely why the with statement exists; to provide a cleaner way to wrap a block in setup and teardown functions. Closing is one. Yeah, you get some extra indentation-- but you sorta have to live with it if you're worried about correct code. I think it's a good compromise between your examples of nasty and nice :) def compromise(from_, to_): with file(to_) as to_h: with file(from_) as from_h: for line in from_h: print >> to_h, munge(line) It's just too bad that 'with' doesn't support multiple separate "x as y" clauses. The developers already agreed with you ;-). "With more than one item, the context managers are processed as if multiple with statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with B() as b: suite Changed in version 3.1: Support for multiple context expressions. " (I suspect this will also be in 2.7) Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Is "#!/usr/bin/env python" the better shebang line ?
Hello Sorry if this has been discussed before, my search did not find it. My questions is if I should use #!/usr/bin/env python as the shebang line in a portable and open python script and if it does help with portability and usage. First, can one not find /usr/bin/python in any standard system, at least as much as /usr/bin/env can be found ? Then, supposing /usr/bin/env is better than /usr/bin/python and I use it, is it not the case that many editors and code analysing programs, that want to know the script language for syntax highlighting, code completion, tags and source code browsing, etc, will loose their functionality because of this shebang line ? Is this not a good general reason to use the old one ? Now I know env is POSIX standard utility, and python is not, and maybe that could be reason enough, but are there any other issues involved with using /usr/bin/env ? Thank you Timothy Madden -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Maggie wrote: code practice: test = open ("test.txt", "r") readData = test.readlines() #set up a sum sum = 0; for item in readData: sum += int(item) print sum test file looks something like this: 34 23 124 432 12 when i am trying to compile this it gives me the error: invalid literal for int() with base 10 i know a lot of people get this and it usually means that you try to cast a string into an integer and this string does not really contain a “digit”..so I am just not sure how to correct it in this case... You already have your specific answer, but you need a general strategy also. When you have a problem processing data from a file, you should ask: "Is the problem with the file data? Or is it with the subsequent processing?". The answers come from two different test programs. For the first: print readData #or for item in readData: print repr(item) This would have shown you that the file is not what you thought. For the second: readData = ['34', '23'] # etc #read of program and it that works, add '\n' to the end of each item. Any decent programming editor will let you comment out blocks of text without deleting them. In other words, to debug, run simple experiments. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Support for Windows 7 ? Thread closed please
Many thanks to all contributors! I learnt sth I never realized before: Windows indeed maintains a "current directory" for each drive! As you may guess, I'm not very fond of DOS / Windows. My training with those OS started with "hands-on" experience on a machine w/ a single "C:" drive (namely a 5" diskette). Well.. again, many thks to Thorsten Kampe, Tim Roberts and Grant Edwards. It seems that after all, Windows deserves a closer look! I shall grab every opportunity to learn more about it. Pascale. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to refer to data files without hardcoding paths?
Matthew Wilson wrote: When a python package includes data files like templates or images, what is the orthodox way of referring to these in code? I'm working on an application installable through the Python package index. Most of the app is just python code, but I use a few jinja2 templates. Today I realized that I'm hardcoding paths in my app. They are relative paths based on os.getcwd(), but at some point, I'll be running scripts that use this code, these open(...) calls will fail. I found several posts that talk about using __file__ and then walking to nearby directories. I also came across pkg_resources, and that seems to work, but I don't think I understand it all yet. Matt sys.path[0] should give you the path to your script. By reading the documentation I would say it would give the path to the first script passed to the interpreter at launch, but after using it I find it also gives the current script path inside an imported file. So I use it to group the script files in my application into subdirectories, and import them as necessary from there. My app works regardless of the current working directory, and can import scripts and load icons from its various subdirectories. Still I would like to know why it works in imported scripts, since the doc page says sys.path[0] is the path to the script that caused the interpreter to launch. What would that mean ? Timothy Madden -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
John Nagle: > The concept here is that objects have an "owner", which is either > a thread or some synchronized object. Locking is at the "owner" > level. This is simple until "ownership" needs to be transferred. > Can this be made to work in a Pythonic way, without explicit > syntax? > > What we want to happen, somehow, is to > transfer the ownership of "words" from the calling thread to the object > in "putitem", and transfer it to the calling thread in "getitem". > How can this be done? There are several people that have found ways to implement such owning of variables, for example Bartosz Milewski: http://bartoszmilewski.wordpress.com/2009/06/02/race-free-multithreading-ownership/ It requires some extra complexities, so statically typed languages usually don't implement this idea, even if it avoids bugs in user code. Implementing it in Python in a simple enough way looks like a good topic for advanced research :-) Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Chris Rebert wrote: On Sun, Sep 6, 2009 at 1:28 AM, Maggie wrote: On Sep 6, 4:19 am, Chris Rebert wrote: On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote: On Sep 6, 3:58 am, Chris Rebert wrote: On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: Hi sum = 0 for item in readData: try: sum += int(item) except ValueError: print "Oops! That was no valid number. Instead it was:", item So you mean to say this would ignore the bad data and continue processing ? Yes. A new "try" happens every loop iteration since the "try" is within the loop body. Cheers, Chris --http://blog.rebertia.com Wow, thanks for your suggestions, Chris. Exceptions are really useful.. when i try running the script now I get a whole bunch of errors like this one -- [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% python test1.py That was not a valid number. Instead it was: {\rtf1\ansi \ansicpg1252\cocoartf949\cocoasubrtf540 That was not a valid number. Instead it was: {\fonttbl\f0\fswiss \fcharset0 Helvetica;} That was not a valid number. Instead it was: {\colortbl; \red255\green255\blue255;} That was not a valid number. Instead it was: \margl1440\margr1440\vieww9000\viewh8400\viewkind0 That was not a valid number. Instead it was: \pard \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql \qnatural\pardirnatural That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 432 12} - why is it reading in all of the above? and how do I fix it and avoid it in the future? Your file appears to be saved in Rich Text Format (RTF) rather than plain text (.TXT), hence Python is seeing all the formatting gibberish. Re-save it in the correct file format and try again. Cheers, Chris --http://blog.rebertia.com the problem is - it is saved as a .txt and not in .rtf I assure you, it is not. You're saving it as Rich Text Format, but with a .txt extension. You need to save it as plain text with a .txt extension. -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
On Sep 5, 10:34 pm, Terry Reedy wrote: > Adam Skutt wrote: > > On Sep 5, 11:29 am, Terry Reedy wrote: > >> This is a pointless replacement for 'def b(x): return x+a' > > > And? That has nothing to do with anything I was saying whatsoever. > > Agreed. However, posts are read by newbies. > Posts that promote bad habits are fair game for comment. There's nothing inappropriate about using a lambda for a function I don't care to give a name. That's the entire reason they exist. > The idea that Python has 'lambda objects' had caused no end of mischief > over the years. As near as I can tell, this is because you're insisting on creating a semantic distinction where there just isn't one. Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: The future of Python immutability
On Sep 5, 7:38 pm, Steven D'Aprano No. Lambdas are a *syntactical construct*, not an object. You wouldn't > talk about "while objects" and "if objects" and "comment objects" > *because they're not objects*. This rhetoric precludes functions objects as well and is entirely non- compelling. > Functions created with def and functions created with lambda are > *precisely* the same type of object. Which means you have lambda objects. The fact they're same as any other function is irrelevant and not especially interesting. > There is no such thing as a "lambda > object" which is a "special case" of ordinary functions, there are just > functions. Hey, I was just trying to resolve tjr's view, he seemed to think that .__name__ is different is pretty important, and he's the one you should take your objections up with, not me. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 4:28 AM, Maggie wrote: > On Sep 6, 4:19 am, Chris Rebert wrote: >> On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote: >> > On Sep 6, 3:58 am, Chris Rebert wrote: >> >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: >> >> > Hi >> >> >> > sum = 0 >> >> > for item in readData: >> >> > try: >> >> > sum += int(item) >> >> > except ValueError: >> >> > print "Oops! That was no valid number. Instead it was:", item >> >> >> > So you mean to say this would ignore the bad data and continue >> >> > processing ? >> >> >> Yes. A new "try" happens every loop iteration since the "try" is >> >> within the loop body. >> >> >> Cheers, >> >> Chris >> >> --http://blog.rebertia.com >> >> > Wow, thanks for your suggestions, Chris. Exceptions are really >> > useful.. >> > when i try running the script now I get a whole bunch of errors like >> > this one -- >> >> > [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% >> > python test1.py >> > That was not a valid number. Instead it was: {\rtf1\ansi >> > \ansicpg1252\cocoartf949\cocoasubrtf540 >> >> > That was not a valid number. Instead it was: {\fonttbl\f0\fswiss >> > \fcharset0 Helvetica;} >> >> > That was not a valid number. Instead it was: {\colortbl; >> > \red255\green255\blue255;} >> >> > That was not a valid number. Instead it was: >> > \margl1440\margr1440\vieww9000\viewh8400\viewkind0 >> >> > That was not a valid number. Instead it was: \pard >> > \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql >> > \qnatural\pardirnatural >> >> > That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 >> > 432 12} >> >> > - >> >> > why is it reading in all of the above? and how do I fix it and avoid >> > it in the future? >> >> Your file appears to be saved in Rich Text Format (RTF) rather than >> plain text (.TXT), hence Python is seeing all the formatting >> gibberish. >> Re-save it in the correct file format and try again. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > the problem is - it is saved as a .txt and not in .rtf.. which is why > this error seems so bizarre..! The first thing you should understand about files- those extensions are absolutely meaningless. All they do is tell your OS which program to use to open it. When it boils down to it, every file is just a collection of 1s and 0s. Just because your file says ".txt" does not mean that it is a plain text file. Open up TextEdit and go to Format -> Make Plain Text and then save it again. ] -- http://mail.python.org/mailman/listinfo/python-list
Managing a Sub-Process
I am using Python 2.6 on Gentoo Linux and have a routine that gets/puts files to other servers via sftp. We have an ongoing problem with various sftp processes "hanging"; that is, it no longer transfers any data but does not shutdown/timeout. I would like to design a routine that will kick off the sftp (or whatever) module and then monitor it. If the sub-process hangs (or fails any other criteria we define) it could be killed in the main routine and an error could be logged. How can I do this? I've looked into the subprocess module as well as the Twisted package but I thought that before I launch onto building my own system to accomplish this I'd ask if it's been done before and, if so, how? What are the isues I need to worry about? Thanks, --greg -- http://mail.python.org/mailman/listinfo/python-list
Re: How to access ODBC databases ?
Timothy Madden wrote: Thank you. The precompiled psqlodbca.so driver from apt-get worked on one of the Ubuntu machines that I tried. I would still like o use the Unicode driver if possible. Do you know what the problem could be ? Or where ? pyodbc/unixODBC/psqlodbcw.so ? Thank you, Timothy Madden It has been a couple of years, but I remember vaguely that back in the days of PossgreSQL 6, if you want ODBC support you needed to compile PG a bit different then normal, I am not really sure what options those where and if this still applies, I decided to not use ODBC because it resulted in an undesirable effect somewhere else. Though I am not sure if that is still the case. Sorry I can't be of more help. -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' -- http://mail.python.org/mailman/listinfo/python-list
Evil trend report
Bing A 32.4% () A 10.8% (non_commercial) Q50 40.0% () Q15 12.0% (no_location) U 54.0% (no_website) U33 26.4% (non_commercial) X 10.8% (negative_info) X17 13.6% (no_location) Google A 10.8% () A 43.3% (non_commercial) Q46 38.3% () Q20 16.7% (no_location) Q 10.8% (non_commercial) U 43.3% (no_website) U28 23.3% (non_commercial) X16 13.3% (no_location) Test complete: Evil trend report Right now, Google is winning slightly. It changes from minute to minute, because it's based on the current list of hot search topics from Google. More on this later. John -- http://mail.python.org/mailman/listinfo/python-list
Re: Support for Windows 7 ?
Bonjour ! Plusieurs points : - Python (ainsi que Pywin32) fonctionne TRÈS bien sous Windows-7 (je l'utilise depuis plus d'un an, sur Win-7 beta, RC, RTM, en 32 bits et en 64 bits). Résultats : AUCUN problème. - Il existe des sources françaises (newsgroups, sites, forums, etc.) qui peuvent répondre aussi bien sur Python, que les newsgroups américains. Du coup, j'ai envie de déduire : - Que certains étudiants d'écoles de commerce françaises préfèrent travailler avec "l'étranger" plutôt qu'avec "le français". - Il faudra dire à d'autres étudiants d'écoles de commerce françaises que le fait de ne pas arriver/savoir installer et tester Python sous Windows-7 donne une mauvaise image de l'école... @-salutations -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does this group have so much spam?
On Thu, 03 Sep 2009 23:07:48 -0400, Terry Reedy wrote: > Suppose that all over the world, people coordinated so that one in three > households paid ISPs while a neighbor on each side piggybacked (and > perhaps paid the paying househould their one-third share). Do you > really think that would have no effect on the pricing and availability > of internet service? Are the accounts unlimited downloads, or are are they capped? Does the ISP charge for excess downloads, or do they slow the connection down to modem speed? Or even disconnect the account until the end of the month? Does the ISP offer only a single plan, or are there multiple plans with different caps? Can people cancel their accounts without notice, or are they locked in for 12 months? How many ISPs are there? If only one, does the government enforce laws against anti-monopolistic behaviour, or is it happy to look the other way? There are far too many variables to give a definitive answer to your question, but I'll try... Consider a typical set of neighbours, Fred, Barney and Wilma, all with a 10GB monthly cap, and each use 8GB of that cap in an average month. Barney gets wi-fi, and leaves it open. Fred and Wilma immediately cancel their accounts, and piggyback off Barney, and in fact increase their usage to 10GB because its not costing them anything. What the ISP sees is that their total usage goes from 24GB used out of 30GB paid for, to 28GB out of 10GB paid for. If they're charging for excess usage, they'll rub their hands with glee -- excess usage fees tend to be brutal, and pure profit. No matter how altruistic Barney is, he'll surely soon upgrade his cap to 30GB (or more). If the ISP has done their sums right, their profit on a 30GB cap will be more-or-less equal to 3 x their profit on a 10GB cap -- and very likely larger. Why? Because of fixed, per account, costs. The ISP's fixed costs (administrative costs) depend on the number of accounts, which has just dropped by two thirds. Their variable costs depend on the amount of downloads, and have increased by one sixth -- but the transmission costs themselves are quite low. It's not unreasonable to hypothesise that the decrease in per-account costs more than makes up for the increase in transmission costs. Essentially, Barney is acting as a middleman between his neighbours and the ISP. (The fact that Barney may not collect any money from Fred or Wilma is irrelevant -- he's just making a monetary loss from the deal.) Suppliers often, but not always, love middlemen, because they can palm off the least profitable and most expensive parts of their business to somebody willing to work for a smaller margin. In this case, the ISP gets to supply three customers for the administrative and help-desk costs of supplying one (Barney). It's not unreasonable for this to be a win to the ISP. Sometimes you get multiple layers of middlemen, e.g. in Australia it's not unusual to have ISPs like Telstra who deal direct with the end consumer but also sell bandwidth to smaller ISPs like Internode, who also sell to the consumer as well as selling bandwidth to tiny ISPs with a few hundred customers. Would this be viable with thousands of (effectively) nano-ISPs with two customers each? I don't know, but it could be. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Support for Windows 7 ?
Michel Claveau - MVP a écrit : Bonjour ! Plusieurs points : - Python (ainsi que Pywin32) fonctionne TRÈS bien sous Windows-7 (je l'utilise depuis plus d'un an, sur Win-7 beta, RC, RTM, en 32 bits et en 64 bits). Résultats : AUCUN problème. - Il existe des sources françaises (newsgroups, sites, forums, etc.) qui peuvent répondre aussi bien sur Python, que les newsgroups américains. Merci pour l'info et la leçon. Je m'en souviendrai. Du coup, j'ai envie de déduire : - Que certains étudiants d'écoles de commerce françaises préfèrent travailler avec "l'étranger" plutôt qu'avec "le français". - Il faudra dire à d'autres étudiants d'écoles de commerce françaises que le fait de ne pas arriver/savoir installer et tester Python sous Windows-7 donne une mauvaise image de l'école... Ayez toutes les envies de déduction qui vous passent par la tête, mais je vous suggère d'éviter de les exposer si vite au jugement de tous! Primo, je ne suis pas étudiante, deuxio nous ne sommes pas une école de commerce, tertio le cours est donné en anglais, et pour finir.. c'est vrai que j'ai bossé davantage en anglais US qu'en français, et que ma première réaction (à tort vous avez raison) est de m'exprimer en anglais. Par ailleurs le problème étant résolu (c'était une erreur de l'utilisateur) je ne vois pas l'intérêt de poursuivre ce fil. Pascale Mourier. -- http://mail.python.org/mailman/listinfo/python-list
zip a huge file into multiple small ones
I am new to dealing with zip files in python. I have a huge file which i need to zip and send as an attachment through email. My email restrictions are not allowing me to send it in one go. Is there a way to split this file into multiple zip files, so that i can mail them separately. All the individual zip files should be linked. I should be able to extract the original file by extracting the first of the small zip files. -- http://mail.python.org/mailman/listinfo/python-list
subclassing Scientific.IO.NetCDFFile
Hi all, I have come across a problem that I am unsure how to get around. What I want to do is create a subclass of Scientific.IO.NetCDFFile, but despite what the docstrings say isn't really a class at all, but a function that returns some sort of data structure from the netcdf C api. I am aware of pure python implementations such as scipy.io.netcdf_file, but these seem to have internal limitations causing overflows when opening large (e.g. several gigabytes) files. I have tried creating a class with an open NetCDFFile as a member (f). File attributes are easy enough to deal with (self.__dict__=self.f.__dict__), and I can even create callable methods such as self.dimensions which return self.f.dimensions. However, when it comes to more esoteric aspects such as multi- dimensional slicing of the variables (derived somehow from numpy.array), I am somewhat at a loss. So my question is: Is there a simple method of containing NetCDFFile in a class? If not, how can I design a class that will behave like NetCDFFile from the user perspective? Thanks, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: Turn-based game - experimental economics
Dennis, thanks. > Do the clients have to do anything between turns? If not, the > simplest thing is to just have these clients block on a read request > waiting for data to be returned from the server. the only thing that the clients do is receiving information on the action of other players, and updating some player-related and state-of-the-world related statistics. > > {BTW: I think the term you're looking for is "console" or "command > line" client ; after all -- stdout is just and I/O stream, and bash > is just a shell interpreter} thanks, exactly. My plan is to have a GUI in the end, but in the meantime - for testing - it is important for me to have a working version and I was planning to address the GUI issue later. > The only controlling aspect of your users is the initial connection > to the "game". Once connected, the users are at the mercy of the "game" > to notify them that it is time for them to enter instructions (you don't > describe enough detail to determine if a "turn" is a single transaction, > or could consist of multiple transactions [you mentioned "buying > letters" along with playing letters to extend words; can both take place > in one turn?]) This is a good idea. Details: for every turn, the player performs three operations: buy(not buy); create(a word, an extension, nothing); copyright(or not), then passes. Other players do not really need to be notified in real time, can as well be notified at the end of the turn. To give some more details, basically the game works as follows: each turn, players buy, produce, decide on Intellectual property of their creation. The words created pass through a spellchecker and other basic checks (does the player really own the letters he's using, etc...) and then is added to a public wordlist; copyrighted words entitle the owner with royalties and copylefted words do not. Every time a player extends a copyrighted word there is a flow of royalties to be allocated. > > -=-=-=-=- "Game" > ConnectionThread: > while True: > accept connection > send world state to new display/player > add connection to connection list #lock > > Main: > start connection thread > active player = 0 > while True: > if len(connection list): > send ACTIVE token to connection list [active player] > while True: > get player response > if response == QUIT: > del connection list [active player] > #lock > update world state > for p in connection list: > #lock > send world update to p > if response == EndTurn: break > active player = (active player + 1) % len(connection > list) > else: > sleep(1)#no players connected, so sleep before > repeating > > (the #lock indicate operations that may need to be protected by using a > lock object) > > > -=-=-=-=-=- "Display" > > connect to "game" > ACTIVE = False > while True: > get game data > update console display > ACTIVE = game data == active token > if ACTIVE: > get user input > if user input == EndTurn: > ACTIVE = False > send user input > if user input == QUIT: > break > disconnect from "game" > Thanks for this pseudocode - helpful in many ways. The only reason I had to structure the game as a one server-many clients was that I wanted to centralize the functions related to computing the payoffs etc in the server. I think this can be done in your architecture too. P -- Paolo Crosetto - PhD Student in Economics DEAS - Department of Economics - University of Milan - -- http://mail.python.org/mailman/listinfo/python-list
Re: Question on File Input and AST
On Sun, 06 Sep 2009 00:53:43 -0700, joy99 wrote: > Dear Group, > > I have a file "test1.txt". Now, as I do the file handling, i try to do > any one of the following operations. > > 1. open_file=open("/python26/test1.txt","r") # FOR READING 2. > open_file=open("/python26/test1.txt","r+") # FOR READING AND WRITING > BOTH > [Either of 1 or 2 to open as the need be] 3. read_file=open_file.read() > etc... > > But how to work with "fileinput" or "ast". > > I tried to read python docs and some backlog question answer in the > group but it did not help much. At the interactive interpreter, do this: >>> import fileinput >>> help(fileinput) and read the help text. It gives you an example: [quote] Typical use is: import fileinput for line in fileinput.input(): process(line) This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-' it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the argument to input(). A single file name is also allowed. [end quote] If there is something unclear about that, then please try to be specific about what you don't understand. Try some code, and report what errors you get. As far as ast, there's an example here: http://docs.python.org/dev/whatsnew/2.6.html#the-ast-module and a discussion here: http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the- ast-modify-it-then-write-back-the-modified If you explain what you want to do, perhaps there's somebody out there who knows the ast module and can answer. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 1:28 AM, Maggie wrote: > On Sep 6, 4:19 am, Chris Rebert wrote: >> On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote: >> > On Sep 6, 3:58 am, Chris Rebert wrote: >> >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: >> >> > Hi >> >> >> > sum = 0 >> >> > for item in readData: >> >> > try: >> >> > sum += int(item) >> >> > except ValueError: >> >> > print "Oops! That was no valid number. Instead it was:", item >> >> >> > So you mean to say this would ignore the bad data and continue >> >> > processing ? >> >> >> Yes. A new "try" happens every loop iteration since the "try" is >> >> within the loop body. >> >> >> Cheers, >> >> Chris >> >> --http://blog.rebertia.com >> >> > Wow, thanks for your suggestions, Chris. Exceptions are really >> > useful.. >> > when i try running the script now I get a whole bunch of errors like >> > this one -- >> >> > [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% >> > python test1.py >> > That was not a valid number. Instead it was: {\rtf1\ansi >> > \ansicpg1252\cocoartf949\cocoasubrtf540 >> >> > That was not a valid number. Instead it was: {\fonttbl\f0\fswiss >> > \fcharset0 Helvetica;} >> >> > That was not a valid number. Instead it was: {\colortbl; >> > \red255\green255\blue255;} >> >> > That was not a valid number. Instead it was: >> > \margl1440\margr1440\vieww9000\viewh8400\viewkind0 >> >> > That was not a valid number. Instead it was: \pard >> > \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql >> > \qnatural\pardirnatural >> >> > That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 >> > 432 12} >> >> > - >> >> > why is it reading in all of the above? and how do I fix it and avoid >> > it in the future? >> >> Your file appears to be saved in Rich Text Format (RTF) rather than >> plain text (.TXT), hence Python is seeing all the formatting >> gibberish. >> Re-save it in the correct file format and try again. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > the problem is - it is saved as a .txt and not in .rtf I assure you, it is not. - Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sep 6, 4:19 am, Chris Rebert wrote: > On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote: > > On Sep 6, 3:58 am, Chris Rebert wrote: > >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: > >> > Hi > > >> > sum = 0 > >> > for item in readData: > >> > try: > >> > sum += int(item) > >> > except ValueError: > >> > print "Oops! That was no valid number. Instead it was:", item > > >> > So you mean to say this would ignore the bad data and continue > >> > processing ? > > >> Yes. A new "try" happens every loop iteration since the "try" is > >> within the loop body. > > >> Cheers, > >> Chris > >> --http://blog.rebertia.com > > > Wow, thanks for your suggestions, Chris. Exceptions are really > > useful.. > > when i try running the script now I get a whole bunch of errors like > > this one -- > > > [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% > > python test1.py > > That was not a valid number. Instead it was: {\rtf1\ansi > > \ansicpg1252\cocoartf949\cocoasubrtf540 > > > That was not a valid number. Instead it was: {\fonttbl\f0\fswiss > > \fcharset0 Helvetica;} > > > That was not a valid number. Instead it was: {\colortbl; > > \red255\green255\blue255;} > > > That was not a valid number. Instead it was: > > \margl1440\margr1440\vieww9000\viewh8400\viewkind0 > > > That was not a valid number. Instead it was: \pard > > \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql > > \qnatural\pardirnatural > > > That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 > > 432 12} > > > - > > > why is it reading in all of the above? and how do I fix it and avoid > > it in the future? > > Your file appears to be saved in Rich Text Format (RTF) rather than > plain text (.TXT), hence Python is seeing all the formatting > gibberish. > Re-save it in the correct file format and try again. > > Cheers, > Chris > --http://blog.rebertia.com the problem is - it is saved as a .txt and not in .rtf.. which is why this error seems so bizarre..! -- http://mail.python.org/mailman/listinfo/python-list
Re: Evil trend report
On Sat, 05 Sep 2009 22:43:17 -0700, John Nagle wrote: [snip] > Test complete: Evil trend report > > Right now, Google is winning slightly. It changes from minute to > minute, because it's based on the current list of hot search topics from > Google. More on this later. What does this mean? What are the tables showing? How do you define evil? Why aren't you including Yahoo search in your test? (It has a much bigger market share than MSN, even rebranded as Bing). Is this relevant to Python is some way? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote: > On Sep 6, 3:58 am, Chris Rebert wrote: >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: >> > Hi >> >> > sum = 0 >> > for item in readData: >> > try: >> > sum += int(item) >> > except ValueError: >> > print "Oops! That was no valid number. Instead it was:", item >> >> > So you mean to say this would ignore the bad data and continue processing ? >> >> Yes. A new "try" happens every loop iteration since the "try" is >> within the loop body. >> >> Cheers, >> Chris >> --http://blog.rebertia.com > > Wow, thanks for your suggestions, Chris. Exceptions are really > useful.. > when i try running the script now I get a whole bunch of errors like > this one -- > > [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% > python test1.py > That was not a valid number. Instead it was: {\rtf1\ansi > \ansicpg1252\cocoartf949\cocoasubrtf540 > > That was not a valid number. Instead it was: {\fonttbl\f0\fswiss > \fcharset0 Helvetica;} > > That was not a valid number. Instead it was: {\colortbl; > \red255\green255\blue255;} > > That was not a valid number. Instead it was: > \margl1440\margr1440\vieww9000\viewh8400\viewkind0 > > That was not a valid number. Instead it was: \pard > \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql > \qnatural\pardirnatural > > That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 > 432 12} > > - > > why is it reading in all of the above? and how do I fix it and avoid > it in the future? Your file appears to be saved in Rich Text Format (RTF) rather than plain text (.TXT), hence Python is seeing all the formatting gibberish. Re-save it in the correct file format and try again. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sep 6, 3:58 am, Chris Rebert wrote: > On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: > > Hi > > > sum = 0 > > for item in readData: > > try: > > sum += int(item) > > except ValueError: > > print "Oops! That was no valid number. Instead it was:", item > > > So you mean to say this would ignore the bad data and continue processing ? > > Yes. A new "try" happens every loop iteration since the "try" is > within the loop body. > > Cheers, > Chris > --http://blog.rebertia.com Wow, thanks for your suggestions, Chris. Exceptions are really useful.. when i try running the script now I get a whole bunch of errors like this one -- [jodorowskys-macbook-pro:~/desktop/formisano_replication] Jodorowsky% python test1.py That was not a valid number. Instead it was: {\rtf1\ansi \ansicpg1252\cocoartf949\cocoasubrtf540 That was not a valid number. Instead it was: {\fonttbl\f0\fswiss \fcharset0 Helvetica;} That was not a valid number. Instead it was: {\colortbl; \red255\green255\blue255;} That was not a valid number. Instead it was: \margl1440\margr1440\vieww9000\viewh8400\viewkind0 That was not a valid number. Instead it was: \pard \tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql \qnatural\pardirnatural That was not a valid number. Instead it was: \f0\fs24 \cf0 32 123 231 432 12} - why is it reading in all of the above? and how do I fix it and avoid it in the future? thanks so much for your help, I am learning with every post -- very exciting :) -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote: > Hi > > sum = 0 > for item in readData: > try: > sum += int(item) > except ValueError: > print "Oops! That was no valid number. Instead it was:", item > > So you mean to say this would ignore the bad data and continue processing ? Yes. A new "try" happens every loop iteration since the "try" is within the loop body. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Question on File Input and AST
Dear Group, I have a file "test1.txt". Now, as I do the file handling, i try to do any one of the following operations. 1. open_file=open("/python26/test1.txt","r") # FOR READING 2. open_file=open("/python26/test1.txt","r+") # FOR READING AND WRITING BOTH [Either of 1 or 2 to open as the need be] 3. read_file=open_file.read() etc... But how to work with "fileinput" or "ast". I tried to read python docs and some backlog question answer in the group but it did not help much. If any one can kindly give one or two small examples. It would help me much. Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Hi sum = 0 for item in readData: try: sum += int(item) except ValueError: print "Oops! That was no valid number. Instead it was:", item So you mean to say this would ignore the bad data and continue processing ? regards -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 12:46 AM, hrishy wrote: > Hi Chris > > What if i want to log that bad data and continue processing is there a way to > do that ? Tighten the area included in the try...except: sum = 0 for item in readData: try: sum += int(item) except ValueError: print "Oops! That was no valid number. Instead it was:", item Also, in the future, please avoid top-posting (http://en.wikipedia.org/wiki/Top-post). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: efficiently splitting up strings based on substrings
On Sep 6, 1:23 am, 7stud wrote: > On Sep 6, 1:14 am, 7stud wrote: > > > > > On Sep 5, 5:29 pm, per wrote: > > > > On Sep 5, 7:07 pm, "Rhodri James" wrote: > > > > > On Sat, 05 Sep 2009 23:54:08 +0100, per wrote: > > > > > On Sep 5, 6:42 pm, "Rhodri James" wrote: > > > > >> On Sat, 05 Sep 2009 22:54:41 +0100, per wrote: > > > > >> > I'm trying to efficiently "split" strings based on what substrings > > > > >> > they are made up of. > > > > >> > i have a set of strings that are comprised of known substrings. > > > > >> > For example, a, b, and c are substrings that are not identical to > > > > >> > each > > > > >> > other, e.g.: > > > > >> > a = "0" * 5 > > > > >> > b = "1" * 5 > > > > >> > c = "2" * 5 > > > > > >> > Then my_string might be: > > > > > >> > my_string = a + b + c > > > > > >> > i am looking for an efficient way to solve the following problem. > > > > >> > suppose i have a short > > > > >> > string x that is a substring of my_string. I want to "split" the > > > > >> > string x into blocks based on > > > > >> > what substrings (i.e. a, b, or c) chunks of s fall into. > > > > > >> > to illustrate this, suppose x = "00111". Then I can detect where x > > > > >> > starts in my_string > > > > >> > using my_string.find(x). But I don't know how to partition x into > > > > >> > blocks depending > > > > >> > on the substrings. What I want to get out in this case is: "00", > > > > >> > "111". If x were "00122", > > > > >> > I'd want to get out "00","1", "22". > > > > > >> > is there an easy way to do this? i can't simply split x on a, b, > > > > >> > or c > > > > >> > because these might > > > > >> > not be contained in x. I want to avoid doing something inefficient > > > > >> > like looking at all substrings > > > > >> > of my_string etc. > > > > > >> > i wouldn't mind using regular expressions for this but i cannot > > > > >> > think > > > > >> > of an easy regular > > > > >> > expression for this problem. I looked at the string module in the > > > > >> > library but did not see > > > > >> > anything that seemd related but i might have missed it. > > > > > >> I'm not sure I understand your question exactly. You seem to imply > > > > >> that the order of the substrings of x is consistent. If that's the > > > > >> case, this ought to help: > > > > > >> >>> import re > > > > >> >>> x = "00122" > > > > >> >>> m = re.match(r"(0*)(1*)(2*)", x) > > > > >> >>> m.groups() > > > > > >> ('00', '1', '22')>>> y = "00111" > > > > >> >>> m = re.match(r"(0*)(1*)(2*)", y) > > > > >> >>> m.groups() > > > > > >> ('00', '111', '') > > > > > >> You'll have to filter out the empty groups for yourself, but that's > > > > >> no great problem. > > > > > > The order of the substrings is consistent but what if it's not 0, 1, 2 > > > > > but a more complicated string? e.g. > > > > > > a = 1030405, b = 1babcf, c = fUUIUP > > > > > > then the substring x might be 4051ba, in which case using a regexp > > > > > with (1*) will not work since both a and b substrings begin with the > > > > > character 1. > > > > > Right. This looks approximately nothing like what I thought your > > > > problem was. Would I be right in thinking that you want to match > > > > substrings of your potential "substrings" against the string x? > > > > > I'm sufficiently confused that I think I'd like to see what your > > > > use case actually is before I make more of a fool of myself. > > > > > -- > > > > Rhodri James *-* Wildebeest Herder to the Masses > > > > it's exactly the same problem, except there are no constraints on the > > > strings. so the problem is, like you say, matching the substrings > > > against the string x. in other words, finding out where x "aligns" to > > > the ordered substrings abc, and then determine what chunk of x belongs > > > to a, what chunk belongs to b, and what chunk belongs to c. > > > > so in the example i gave above, the substrings are: a = 1030405, b = > > > 1babcf, c = fUUIUP, so abc = 10304051babcffUUIUP > > > > given a substring like 4051ba, i'd want to split it into the chunks a, > > > b, and c. in this case, i'd want the result to be: ["405", "1ba"] -- > > > i.e. "405" is the chunk of x that belongs to a, and "1ba" the chunk > > > that belongs to be. in this case, there are no chunks of c. if x > > > instead were "4051babcffUU", the right output is: ["405", "1babcf", > > > "fUU"], which are the corresponding chunks of a, b, and c that make up > > > x respectively. > > > > i'm not sure how to approach this. any ideas/tips would be greatly > > > appreciated. thanks again. > > > a = "1030405" > > b = "1babcf" > > c = "fUUIUP" > > abc = "10304051babcffUUIUP" > > data = "4051babcffU" > > > data_start = abc.find(data) > > b_start = abc.find(b) - data_start > > c_start = abc.find(c) - data_start > > > print data[:b_start] > > print data[b_start:c_start] > > print data[c_start:] > > > --output:-- > > 405 > > 1babcf > > fU > > ...or maybe this is easier to follow: > > a = "1030405" > b =
Re: File Handling Problem
On Sep 5, 11:49 am, Chris Rebert wrote: > On Fri, Sep 4, 2009 at 11:39 PM, > SUBHABRATABANERJEE wrote: > > > > > And one small question does Python has any increment operator like ++ in C. > > No. We do x += 1 instead. > > Cheers, > Chris > --http://blog.rebertia.com Thanx for your kind reply. Warm Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Hi Chris What if i want to log that bad data and continue processing is there a way to do that ? regards --- On Sun, 6/9/09, Chris Rebert wrote: > From: Chris Rebert > Subject: Re: beginner's python help > To: "Maggie" > Cc: python-list@python.org > Date: Sunday, 6 September, 2009, 8:15 AM > On Sun, Sep 6, 2009 at 12:00 AM, > Maggie > wrote: > > code practice: > > > > test = open ("test.txt", "r") > > readData = test.readlines() > > #set up a sum > > sum = 0; > > for item in readData: > > sum += int(item) > > print sum > > A slightly better way to write this: > > test = open("test.txt", "r") > #set up a sum > the_sum = 0 #avoid shadowing the built-in function sum() > for line in test: #iterate over the file directly instead > of reading > it into a list > the_sum += int(line) > print the_sum > > > test file looks something like this: > > > > 34 > > 23 > > 124 > > 432 > > 12 > > > > when i am trying to compile > > No, the error is happening at runtime. Pretty much only > SyntaxErrors > occur at compile-time. > > > this it gives me the error: invalid > > literal for int() with base 10 > > > > i know a lot of people get this and it usually means > that you try to > > cast a string into an integer and this string does not > really contain > > a “digit”..so I am just not sure how to correct it > in this case... > > I would recommend putting a `print repr(line)` inside the > loop, before > the "+=" line. This will show the input int() is getting so > you can > see out what the bad input is that's causing the error and > thus debug > the problem. > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: efficiently splitting up strings based on substrings
On Sep 6, 1:14 am, 7stud wrote: > On Sep 5, 5:29 pm, per wrote: > > > > > On Sep 5, 7:07 pm, "Rhodri James" wrote: > > > > On Sat, 05 Sep 2009 23:54:08 +0100, per wrote: > > > > On Sep 5, 6:42 pm, "Rhodri James" wrote: > > > >> On Sat, 05 Sep 2009 22:54:41 +0100, per wrote: > > > >> > I'm trying to efficiently "split" strings based on what substrings > > > >> > they are made up of. > > > >> > i have a set of strings that are comprised of known substrings. > > > >> > For example, a, b, and c are substrings that are not identical to > > > >> > each > > > >> > other, e.g.: > > > >> > a = "0" * 5 > > > >> > b = "1" * 5 > > > >> > c = "2" * 5 > > > > >> > Then my_string might be: > > > > >> > my_string = a + b + c > > > > >> > i am looking for an efficient way to solve the following problem. > > > >> > suppose i have a short > > > >> > string x that is a substring of my_string. I want to "split" the > > > >> > string x into blocks based on > > > >> > what substrings (i.e. a, b, or c) chunks of s fall into. > > > > >> > to illustrate this, suppose x = "00111". Then I can detect where x > > > >> > starts in my_string > > > >> > using my_string.find(x). But I don't know how to partition x into > > > >> > blocks depending > > > >> > on the substrings. What I want to get out in this case is: "00", > > > >> > "111". If x were "00122", > > > >> > I'd want to get out "00","1", "22". > > > > >> > is there an easy way to do this? i can't simply split x on a, b, or > > > >> > c > > > >> > because these might > > > >> > not be contained in x. I want to avoid doing something inefficient > > > >> > like looking at all substrings > > > >> > of my_string etc. > > > > >> > i wouldn't mind using regular expressions for this but i cannot think > > > >> > of an easy regular > > > >> > expression for this problem. I looked at the string module in the > > > >> > library but did not see > > > >> > anything that seemd related but i might have missed it. > > > > >> I'm not sure I understand your question exactly. You seem to imply > > > >> that the order of the substrings of x is consistent. If that's the > > > >> case, this ought to help: > > > > >> >>> import re > > > >> >>> x = "00122" > > > >> >>> m = re.match(r"(0*)(1*)(2*)", x) > > > >> >>> m.groups() > > > > >> ('00', '1', '22')>>> y = "00111" > > > >> >>> m = re.match(r"(0*)(1*)(2*)", y) > > > >> >>> m.groups() > > > > >> ('00', '111', '') > > > > >> You'll have to filter out the empty groups for yourself, but that's > > > >> no great problem. > > > > > The order of the substrings is consistent but what if it's not 0, 1, 2 > > > > but a more complicated string? e.g. > > > > > a = 1030405, b = 1babcf, c = fUUIUP > > > > > then the substring x might be 4051ba, in which case using a regexp > > > > with (1*) will not work since both a and b substrings begin with the > > > > character 1. > > > > Right. This looks approximately nothing like what I thought your > > > problem was. Would I be right in thinking that you want to match > > > substrings of your potential "substrings" against the string x? > > > > I'm sufficiently confused that I think I'd like to see what your > > > use case actually is before I make more of a fool of myself. > > > > -- > > > Rhodri James *-* Wildebeest Herder to the Masses > > > it's exactly the same problem, except there are no constraints on the > > strings. so the problem is, like you say, matching the substrings > > against the string x. in other words, finding out where x "aligns" to > > the ordered substrings abc, and then determine what chunk of x belongs > > to a, what chunk belongs to b, and what chunk belongs to c. > > > so in the example i gave above, the substrings are: a = 1030405, b = > > 1babcf, c = fUUIUP, so abc = 10304051babcffUUIUP > > > given a substring like 4051ba, i'd want to split it into the chunks a, > > b, and c. in this case, i'd want the result to be: ["405", "1ba"] -- > > i.e. "405" is the chunk of x that belongs to a, and "1ba" the chunk > > that belongs to be. in this case, there are no chunks of c. if x > > instead were "4051babcffUU", the right output is: ["405", "1babcf", > > "fUU"], which are the corresponding chunks of a, b, and c that make up > > x respectively. > > > i'm not sure how to approach this. any ideas/tips would be greatly > > appreciated. thanks again. > > a = "1030405" > b = "1babcf" > c = "fUUIUP" > abc = "10304051babcffUUIUP" > data = "4051babcffU" > > data_start = abc.find(data) > b_start = abc.find(b) - data_start > c_start = abc.find(c) - data_start > > print data[:b_start] > print data[b_start:c_start] > print data[c_start:] > > --output:-- > 405 > 1babcf > fU ...or maybe this is easier to follow: a = "1030405" b = "1babcf" c = "fUUIUP" abc = "10304051babcffUUIUP" data = "4051babcffU" data_start = abc.find(data) new_abc = abc[data_start:] print new_abc print data print "-" * 10 --output:-- 4051babcffUUIUP 4051babcffU -- b_start = new_abc.find(b) c_sta
Re: beginner's python help
Hi I am just a python beginner What you need is exceptions http://docs.python.org/tutorial/errors.html something on the lines of since you expect a integer and you wnat to catch the exception ... try: ... sum = 0; ... for item in readData: ...sum += int(item) ... except ValueError: ... print "Oops! That was no valid number. Try again...",item --- On Sun, 6/9/09, Maggie wrote: > From: Maggie > Subject: beginner's python help > To: python-list@python.org > Date: Sunday, 6 September, 2009, 8:00 AM > code practice: > > test = open ("test.txt", "r") > readData = test.readlines() > #set up a sum > sum = 0; > for item in readData: > sum += int(item) > print sum > > test file looks something like this: > > 34 > 23 > 124 > 432 > 12 > > when i am trying to compile this it gives me the error: > invalid > literal for int() with base 10 > > i know a lot of people get this and it usually means that > you try to > cast a string into an integer and this string does not > really contain > a “digit”..so I am just not sure how to correct it in > this case... > > thanks for your input > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
On Sun, Sep 6, 2009 at 12:00 AM, Maggie wrote: > code practice: > > test = open ("test.txt", "r") > readData = test.readlines() > #set up a sum > sum = 0; > for item in readData: > sum += int(item) > print sum A slightly better way to write this: test = open("test.txt", "r") #set up a sum the_sum = 0 #avoid shadowing the built-in function sum() for line in test: #iterate over the file directly instead of reading it into a list the_sum += int(line) print the_sum > test file looks something like this: > > 34 > 23 > 124 > 432 > 12 > > when i am trying to compile No, the error is happening at runtime. Pretty much only SyntaxErrors occur at compile-time. > this it gives me the error: invalid > literal for int() with base 10 > > i know a lot of people get this and it usually means that you try to > cast a string into an integer and this string does not really contain > a “digit”..so I am just not sure how to correct it in this case... I would recommend putting a `print repr(line)` inside the loop, before the "+=" line. This will show the input int() is getting so you can see out what the bad input is that's causing the error and thus debug the problem. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: efficiently splitting up strings based on substrings
On Sep 5, 5:29 pm, per wrote: > On Sep 5, 7:07 pm, "Rhodri James" wrote: > > > > > On Sat, 05 Sep 2009 23:54:08 +0100, per wrote: > > > On Sep 5, 6:42 pm, "Rhodri James" wrote: > > >> On Sat, 05 Sep 2009 22:54:41 +0100, per wrote: > > >> > I'm trying to efficiently "split" strings based on what substrings > > >> > they are made up of. > > >> > i have a set of strings that are comprised of known substrings. > > >> > For example, a, b, and c are substrings that are not identical to each > > >> > other, e.g.: > > >> > a = "0" * 5 > > >> > b = "1" * 5 > > >> > c = "2" * 5 > > > >> > Then my_string might be: > > > >> > my_string = a + b + c > > > >> > i am looking for an efficient way to solve the following problem. > > >> > suppose i have a short > > >> > string x that is a substring of my_string. I want to "split" the > > >> > string x into blocks based on > > >> > what substrings (i.e. a, b, or c) chunks of s fall into. > > > >> > to illustrate this, suppose x = "00111". Then I can detect where x > > >> > starts in my_string > > >> > using my_string.find(x). But I don't know how to partition x into > > >> > blocks depending > > >> > on the substrings. What I want to get out in this case is: "00", > > >> > "111". If x were "00122", > > >> > I'd want to get out "00","1", "22". > > > >> > is there an easy way to do this? i can't simply split x on a, b, or c > > >> > because these might > > >> > not be contained in x. I want to avoid doing something inefficient > > >> > like looking at all substrings > > >> > of my_string etc. > > > >> > i wouldn't mind using regular expressions for this but i cannot think > > >> > of an easy regular > > >> > expression for this problem. I looked at the string module in the > > >> > library but did not see > > >> > anything that seemd related but i might have missed it. > > > >> I'm not sure I understand your question exactly. You seem to imply > > >> that the order of the substrings of x is consistent. If that's the > > >> case, this ought to help: > > > >> >>> import re > > >> >>> x = "00122" > > >> >>> m = re.match(r"(0*)(1*)(2*)", x) > > >> >>> m.groups() > > > >> ('00', '1', '22')>>> y = "00111" > > >> >>> m = re.match(r"(0*)(1*)(2*)", y) > > >> >>> m.groups() > > > >> ('00', '111', '') > > > >> You'll have to filter out the empty groups for yourself, but that's > > >> no great problem. > > > > The order of the substrings is consistent but what if it's not 0, 1, 2 > > > but a more complicated string? e.g. > > > > a = 1030405, b = 1babcf, c = fUUIUP > > > > then the substring x might be 4051ba, in which case using a regexp > > > with (1*) will not work since both a and b substrings begin with the > > > character 1. > > > Right. This looks approximately nothing like what I thought your > > problem was. Would I be right in thinking that you want to match > > substrings of your potential "substrings" against the string x? > > > I'm sufficiently confused that I think I'd like to see what your > > use case actually is before I make more of a fool of myself. > > > -- > > Rhodri James *-* Wildebeest Herder to the Masses > > it's exactly the same problem, except there are no constraints on the > strings. so the problem is, like you say, matching the substrings > against the string x. in other words, finding out where x "aligns" to > the ordered substrings abc, and then determine what chunk of x belongs > to a, what chunk belongs to b, and what chunk belongs to c. > > so in the example i gave above, the substrings are: a = 1030405, b = > 1babcf, c = fUUIUP, so abc = 10304051babcffUUIUP > > given a substring like 4051ba, i'd want to split it into the chunks a, > b, and c. in this case, i'd want the result to be: ["405", "1ba"] -- > i.e. "405" is the chunk of x that belongs to a, and "1ba" the chunk > that belongs to be. in this case, there are no chunks of c. if x > instead were "4051babcffUU", the right output is: ["405", "1babcf", > "fUU"], which are the corresponding chunks of a, b, and c that make up > x respectively. > > i'm not sure how to approach this. any ideas/tips would be greatly > appreciated. thanks again. a = "1030405" b = "1babcf" c = "fUUIUP" abc = "10304051babcffUUIUP" data = "4051babcffU" data_start = abc.find(data) b_start = abc.find(b) - data_start c_start = abc.find(c) - data_start print data[:b_start] print data[b_start:c_start] print data[c_start:] --output:-- 405 1babcf fU -- http://mail.python.org/mailman/listinfo/python-list
beginner's python help
code practice: test = open ("test.txt", "r") readData = test.readlines() #set up a sum sum = 0; for item in readData: sum += int(item) print sum test file looks something like this: 34 23 124 432 12 when i am trying to compile this it gives me the error: invalid literal for int() with base 10 i know a lot of people get this and it usually means that you try to cast a string into an integer and this string does not really contain a “digit”..so I am just not sure how to correct it in this case... thanks for your input -- http://mail.python.org/mailman/listinfo/python-list