RE Question
I don't understand why the two REs produce a different result. I read
the RE guide but still I can't seem to figure it out.
>>> t
'echo user=name password=pass path="/ret files"\r\n'
>>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t)
['name', 'pass', '"/ret files"']
>>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)
[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
files"', '')]
Also, does '|' char (meaning or) produces a pair for each section. I
don't understand how it works. Can someone please direct me to a place
which will explain it?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Re: RE Question
Thanks, it seems like the first answer covers the second as well.
Thank you.
Jorge Godoy wrote:
> Yoav wrote:
>
>
>>I don't understand why the two REs produce a different result. I read
>>the RE guide but still I can't seem to figure it out.
>>
>> >>> t
>>'echo user=name password=pass path="/ret files"\r\n'
>> >>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t)
>>['name', 'pass', '"/ret files"']
>> >>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)
>>[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
>>files"', '')]
>
>
> Hi Yoav.
>
> You can see at "sre" documentation (use that instead of the docs for "re")
> that using "?:" you're asking for a non-groupping version of the
> parenthesis match. When you use parenthesis, the matched expression is
> saved for using later. Using "?:" you prevent that. This is what causes
> the difference for you.
>
>
>>Also, does '|' char (meaning or) produces a pair for each section. I
>>don't understand how it works. Can someone please direct me to a place
>>which will explain it?
>
>
> I didn't get your second question. When you use "|" the first match is
> used. It is a short-circuited version of "or". I mean, it tries the first
> regexp, if it matches, the second expression is ignored. The same is true
> for "and", except that the comparisons end on the first false result.
>
>
> Be seeing you,
--
http://mail.python.org/mailman/listinfo/python-list
Re: RE Question
What is the difference between the RE module and the SRE one?
Original Message
From: Jorge Godoy <[EMAIL PROTECTED]>
To:
Subject: Re:RE Question
Date: 18/8/2005 17:44
> Yoav wrote:
>
>
>>I don't understand why the two REs produce a different result. I read
>>the RE guide but still I can't seem to figure it out.
>>
>> >>> t
>>'echo user=name password=pass path="/ret files"\r\n'
>> >>> re.findall(r'(?<=\s)[^=]+=((?:".*")|(?:\S*))(?=\s)', t)
>>['name', 'pass', '"/ret files"']
>> >>> re.findall(r'(?<=\s)[^=]+=((".*")|(\S*))(?=\s)', t)
>>[('name', '', 'name'), ('pass', '', 'pass'), ('"/ret files"', '"/ret
>>files"', '')]
>
>
> Hi Yoav.
>
> You can see at "sre" documentation (use that instead of the docs for "re")
> that using "?:" you're asking for a non-groupping version of the
> parenthesis match. When you use parenthesis, the matched expression is
> saved for using later. Using "?:" you prevent that. This is what causes
> the difference for you.
>
>
>>Also, does '|' char (meaning or) produces a pair for each section. I
>>don't understand how it works. Can someone please direct me to a place
>>which will explain it?
>
>
> I didn't get your second question. When you use "|" the first match is
> used. It is a short-circuited version of "or". I mean, it tries the first
> regexp, if it matches, the second expression is ignored. The same is true
> for "and", except that the comparisons end on the first false result.
>
>
> Be seeing you,
--
http://mail.python.org/mailman/listinfo/python-list
RE vs. SRE
What is the difference between the two? Which on is better to use and why? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Running multiple console processes and watching their output
I need to run multiple console apps in the background and to watch their output. I have no idea if this is possible, and I don't even know where to start looking. The processes are watchers, meaning that they watch folders and mail boxes and do operations on items in them . Essentially these are Java command line programs. What I need is to have them running and to take a glance at their console output once in a while to parse it. Is it possible? How? Any idea will help. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Uploading images to imageshack.us with Python
I would love a script to upload images to Imageshack.us. Any chance you
can post the latest version or email it to me?
Thanks.
Ricardo Sanchez wrote:
> I forgot to add that I'm behind a proxy, but I think that is
> irrelevant.
>
> If you are not behind a proxy replace this line:
>
> print post_multipart('proxy-a.mains.nitech.ac.jp', 8080,
> 'http://imageshack.us/index.php', params, files)
>
> with
>
> print post_multipart('imageshack.us', 80, '/index.php', params, files)
>
--
http://mail.python.org/mailman/listinfo/python-list
Variables in REs
Anyway to set variables in REs. Meaning: I have the RE re.compile(r'/[^/]*') for example and I want to use it on both Win32 machines and Unix machnes. Meaning tha tI need to be able to control the '/' before compiling. I want to create and if and decide what the system is and then put the right char in a variable and compile the RE using that varialbe. I am quite a noob, so I know the question might sound stupid, but any help will be appreciated. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Variables in REs
Such a sweet and simple way. Thanks. tooper wrote: > Use os.sep to get / or \ or whatever character used to build pathes on > the os you're working on > -- http://mail.python.org/mailman/listinfo/python-list
Getting rid of "close failed: [Errno 0] No Error" on Win32
I am using os.popen3 to call a console process and get its output and stderr. However on Win32 (and not OS X) I also get the Errno message. It's printed to the screen, which I wish to keep clean. How can disable this notification? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
RE Despair - help required
I am trying the following:
re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")
and I get a return of NoneType, and I have no idea why. I know that I
missing something here, but I really can't figure out why (I bet it's
something obvious). I also tried this RE on KODOS and it works fine
there, so I am really puzzled.
Any ideas?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Getting rid of "close failed: [Errno 0] No Error" on Win32
Ok , I tried: try: os.popen3(...) except: as someone suggested here. And on FreeBSD I don't get the error message, and it works great. However, on Win32 I do get the annoying message. Any idea why? And How I can make it go away? thanks. Yoav wrote: > I am using os.popen3 to call a console process and get its output and > stderr. However on Win32 (and not OS X) I also get the Errno message. > It's printed to the screen, which I wish to keep clean. How can disable > this notification? > > Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Thanks guys. Issue solved.
I am also going to give Microsoft a call about it. Any other issues you
want me to raise while I am talking to them?
Cheers.
Robert Kern wrote:
> Yoav wrote:
>
>>I am trying the following:
>>
>>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")
>>
>>and I get a return of NoneType, and I have no idea why. I know that I
>>missing something here, but I really can't figure out why (I bet it's
>>something obvious). I also tried this RE on KODOS and it works fine
>>there, so I am really puzzled.
>>
>>Any ideas?
>
>
> Look at the second string. It has "\r" in the middle of it where you
> really want "\\r" (or alternatively r"c:\ret_files").
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"
Fredrik Lundh wrote:
> "Yoav" wrote:
>
>>I am trying the following:
>>
>>re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")
>>
>>and I get a return of NoneType, and I have no idea why. I know that I
>>missing something here, but I really can't figure out why
>
>
> instead of struggling with weird REs, why not use Python's standard
> filename manipulation library instead?
>
> http://docs.python.org/lib/module-os.path.html
>
>
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Thank you all guys. It seems like the simpler the solution, the more I am happy about it. Sorry, for the simple question, I am quite new to this lang. Cheers. Robert Kern wrote: > Yoav wrote: > >>Don't think it will do much good. I need to get them from a file and >>extract the last folder in the path. For example: >>if I get "c:\dos\util" >>I want to extract the string "\util" > > > You mean like this: > > import os > os.path.sep + os.path.split(r"c:\dos\util")[-1] > -- http://mail.python.org/mailman/listinfo/python-list
close failed: [Errno 0] No error goes to stdout
I use the following code to the console output: def get_console(cmd): try: p_in, p_out, p_err = os.popen3(cmd) except: pass out_str = '' for obj in p_out: out_str = out_str + obj for obj in p_err: out_str = out_str + obj return out_str for some reason some exceptions (stderr outputs) are not captured by the try method, and slip to the screen. Any one has any idea on how can prevent from any output that I don't want to? The output I get on these exceptions is: close failed: [Errno 0] No error thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: close failed: [Errno 0] No error goes to stdout
I run a Java command line program. The point is, that it's not the program that output this error message for sure. And I don't expect popen3() to catch and report errors. I just want to keep my screen output clean, and I expect popen3() to run the program and not let anything slip to the screen, after all as I understood it is supposed to capture STDERR and STDOUT, but maybe I didn' t understand it right (it's quite probable). Anyway anyway I can do such a thing? Thanks. Peter Hansen wrote: > Yoav wrote: > >> I use the following code to the console output: >> >> def get_console(cmd): >> try: >> p_in, p_out, p_err = os.popen3(cmd) >> except: >> pass >> out_str = '' >> for obj in p_out: >> out_str = out_str + obj >> for obj in p_err: >> out_str = out_str + obj >> return out_str >> >> >> for some reason some exceptions (stderr outputs) are not captured by >> the try method, and slip to the screen. Any one has any idea on how >> can prevent from any output that I don't want to? >> The output I get on these exceptions is: >> close failed: [Errno 0] No error > > > What makes you think the errors are getting past the try/except? Could > they be printed by the program you are invoking with popen3() instead? > What does "cmd" equal when you get this failure? > > Maybe you are expecting that popen3() will somehow raise exceptions when > the called program fails, exceptions which the except statement would > catch. That's not the case. > > -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: close failed: [Errno 0] No error goes to stdout
Steve Holden wrote: > Yoav wrote: > >> I run a Java command line program. The point is, that it's not the >> program that output this error message for sure. And I don't expect >> popen3() to catch and report errors. I just want to keep my screen >> output clean, and I expect popen3() to run the program and not let >> anything slip to the screen, after all as I understood it is supposed >> to capture STDERR and STDOUT, but maybe I didn' t understand it right >> (it's quite probable). Anyway anyway I can do such a thing? >> > It would be helpful if you could verify your theory by running the > program from the command line with standard AND error output > redirection, verifying that no output at all apears on the console when > you type > > cmd > /tmp/stdout 2>/tmp/stderr > > You are correct in supposing that popen3 is supposed to trap all stdout > and stderr output. > > regards > Steve Thank you all for your help. I managed to get rid of it, and I have this theory which I want to hear your opinion about it: It seems like it happened because I didn't bother to close the files resulting from os.popen3(), and left Python to close them on it own. the STDERR file wasn't always created, and therefore when Python tried to close it, it generated an error. I thought that Python has better abilities at closing files on its own, but I guess I was wrong. So my conclusion is that whenever I open a file it MY job to close it. Again, thank you all, you have been a great help. So now I am closing it with a "try:... except: pass". Thanks, Yoav. -- http://mail.python.org/mailman/listinfo/python-list
defaultdicts pickling
Hello, I need to have a dictionary of dictionaries of numbers, and I would like the dictionaries to be defaultdicts, because it makes the code much nicer (I want to be able to do: d['foo']['bar']+=1 ). So naturally, I used: d = defaultdict(lambda :defaultdict(int)) It works great, but now I can not pickle it. I could ofcourse used a real function and not a lambda, but this would make things (a) somewhat slower and (b) a bit ugly. Is there another way of achieving the same behaviour, that allow for pickling? Thanks, Yoav -- http://mail.python.org/mailman/listinfo/python-list
importing a module from a specific directory
I'm using python for research related programming, and so I write many
experimental small pieces of code.
I would like to organize them into directory structure in
which there is a 'main' directory, and under it directories for
specific sub-tasks, or sub-experiments, I'm running (let's call them
'A', 'B', 'C').
I would like to have some common library code sit in 'main', and then use
it from scripts in the sub-tasks directories.
I also want the code in the sub-tasks to be quick-and-dirty fast to write,
so things like 'import sys; sys.append('..');' on the top of each script is
a bit awkward.
Is there a neat clean way of achieving the code organization?
How do you organize your code in such settings?
Thanks,
Yoav
--
http://mail.python.org/mailman/listinfo/python-list
for line in file('filename'):
I use the idiom "for line in file('filename'): do_something(line)" quite a
lot.
Does it close the opened file at the end of the loop, or do I have to
explicitly save the file object and close it afterward?
Yoav
--
http://mail.python.org/mailman/listinfo/python-list
classmethod inheritance
A lot of my code supplement/replace the constructor with class factory methods. So, instead of: > a= A(...) I would write: > a = A.from_file(...) > a = A.create(...) etc. This is implemented as follows: class A: def __init__(self, ...): pass @classmethod def from_file(cls, ): inst = cls() return inst Derived classes of A can of course use these factories to create instances of the derived class as well. The problem arise when the derived class needs a different implementation of the factory. If it was a constructor (or any method for that matter), this would be achieved as: class B(A): def __init__(self, ...): A.__init__(self, ...) However, this can not be done with the factory: class B(A): @classmethod def from_file(cls, ...) inst = A.from_file(...) return inst will result with the type of the created object being A and not B. This can be overcome in two ways. The first (neat hack) is due to a colleague of mine, using static methods and default arguments: class A: @staticmethod def from_file(cls=None, ...) if not cls: cls = A inst = cls() ... class B(A): @staticmethod def from_file(cls=None, ...) if not cls: cls = B inst = A.from_file(cls, ...) The second is by delegating the work of the factory to an instance method: class A: @classmethod def from_file(cls,...) inst = cls() inst._from_file(...) return inst and then overriding the instance method and never touching the factory method. Now, my questions are as follow: 1/ Am I missing a third, more "standard" way of achieving inheritance of class methods? 2/ If not, I think future python's should have a syntax for this -- the first method I proposed is a neat hack, and the second is a workaround, but I wouldn't want the first method to be the official way of doing things, and I suspect there are some use cases that can not be implemented as nicely with the second method. Yoav -- http://mail.python.org/mailman/listinfo/python-list
Re: Python & Go
On Sun, Nov 15, 2009 at 12:10 AM, Terry Reedy wrote: > Paul Rubin wrote: > > Mark Chu-Carroll has a new post about Go: >> >> >> http://scienceblogs.com/goodmath/2009/11/the_go_i_forgot_concurrency_an.php >> > > In a couple of minutes, I wrote his toy prime filter example in Python, > mostly from the text rather than the code, which I can barely stand to read. > It ran the first time without error. > > Yes, but the cool thing about the Go version is that it does each generator in a different thread, so in theory it could run twice as fast on a multi-core machine. Y -- http://mail.python.org/mailman/listinfo/python-list
Re: Python & Go
On Sun, Nov 15, 2009 at 4:00 AM, Terry Reedy wrote: > Yoav Goldberg wrote: > > >> On Sun, Nov 15, 2009 at 12:10 AM, Terry Reedy > [email protected]>> wrote: >> >>Paul Rubin wrote: >> >>Mark Chu-Carroll has a new post about Go: >> >> >> http://scienceblogs.com/goodmath/2009/11/the_go_i_forgot_concurrency_an.php >> >>In a couple of minutes, I wrote his toy prime filter example in >>Python, mostly from the text rather than the code, which I can >>barely stand to read. It ran the first time without error. >> >> Yes, but the cool thing about the Go version is that it does each >> generator in a different thread, so in theory it could run twice as fast on >> a multi-core machine. >> > > Which is why I added, in my opinion, that "It would be much better, for > instance, to tweak Python, which it has had great success with, to better > run on multiple cores." > > For instance, add a new keyword 'go' such that > go def f(): yield 1 > runs the generator in a different thread, possibly on a different core. > > [...] > I see no reason why we cannot have that with Python. I not even sure we > cannot have it with CPython, but I am not familiar enough with threads, > processes, and CPython internals. > > > Yes, this could work, and would really cool. I believe you can go a long way with annotations, and maybe even provide a full working demo. So we can definitely have this in python, and it could be a really cool feature. Having it in CPython, though, is a different story altogether -- CPython way of handling threads is problematic, and not going to change soon, see here about the problem: http://blog.snaplogic.org/?p=94 and here about it no going away soon: http://www.python.org/doc/faq/library/#can-t-we-get-rid-of-the-global-interpreter-lock http://www.artima.com/weblogs/viewpost.jsp?thread=214235 So, yes, we could have this in python, but no I don't see it happening very soon.. And the compiled Go is really different in design than python -- easy to parse, easy to compile, static, works fast, etc. Different language. Is it a good thing to have a different language and not base yourself on an existing one? Not sure. But that's a different debate. -- http://mail.python.org/mailman/listinfo/python-list
