Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python re expr from Perl to Python

Reply
Thread Tools

Python re expr from Perl to Python

 
 
Michael M.
Guest
Posts: n/a
 
      01-06-2007

In Perl, it was:


## Example: "Abc | def | ghi | jkl"
## -> "Abc ghi jkl"
## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
pipe).
$na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;

## -- remove [ and ] in text
$na =~ s/\[//g;
$na =~ s/\]//g;
# print "DEB: \"$na\"\n";


# input string
na="Abc | def | ghi | jkl [gugu]"
# output
na="Abc ghi jkl gugu"


How is it done in Python?
 
Reply With Quote
 
 
 
 
Jorge Godoy
Guest
Posts: n/a
 
      01-06-2007
"Michael M." <(E-Mail Removed)> writes:

> In Perl, it was:
>
>
> ## Example: "Abc | def | ghi | jkl"
> ## -> "Abc ghi jkl"
> ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe).
> $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
>
> ## -- remove [ and ] in text
> $na =~ s/\[//g;
> $na =~ s/\]//g;
> # print "DEB: \"$na\"\n";
>
>
> # input string
> na="Abc | def | ghi | jkl [gugu]"
> # output
> na="Abc ghi jkl gugu"
>
>
> How is it done in Python?


The simplest form:

>>> na="Abc | def | ghi | jkl [gugu]"
>>> na_out = na.replace('def', '').replace(' | ', ' ').replace(' ', ' ').replace('[', '').replace(']', '').strip()
>>> na_out

'Abc ghi jkl gugu'
>>>



Another form:

>>> na_out = ' '.join(na.split(' | ')).replace('[', '').replace(']', '').replace(' def', '')
>>> na_out

'Abc ghi jkl gugu'
>>>



There is the regular expression approach as well as several other
alternatives. I could list other (simpler, more advanced, etc.) alternatives,
but you can also play with Python by yourself. If you have a more concrete
specification, send it to the group.



--
Jorge Godoy <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Carsten Haese
Guest
Posts: n/a
 
      01-06-2007
On Sat, 2007-01-06 at 15:43 +0100, Michael M. wrote:
> In Perl, it was:
>
>
> ## Example: "Abc | def | ghi | jkl"
> ## -> "Abc ghi jkl"
> ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
> pipe).
> $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
>
> ## -- remove [ and ] in text
> $na =~ s/\[//g;
> $na =~ s/\]//g;
> # print "DEB: \"$na\"\n";
>
>
> # input string
> na="Abc | def | ghi | jkl [gugu]"
> # output
> na="Abc ghi jkl gugu"
>
>
> How is it done in Python?


Here's an almost literal translation:

##################################################
import re
na = re.sub(r"\ \|(.*?)\ \|(.*?)\ \|", r"\2", na)
na = na.replace("[", "")
na = na.replace("]", "")
##################################################

Background information on regular expressions in Python can be found
here:

http://www.amk.ca/python/howto/regex/
http://docs.python.org/lib/module-re.html

Hope this helps,

Carsten.


 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      01-06-2007

Michael M. wrote:

> In Perl, it was:
>
>
> ## Example: "Abc | def | ghi | jkl"
> ## -> "Abc ghi jkl"
> ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
> pipe).
> $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
>
> ## -- remove [ and ] in text
> $na =~ s/\[//g;
> $na =~ s/\]//g;
> # print "DEB: \"$na\"\n";
>
>
> # input string
> na="Abc | def | ghi | jkl [gugu]"
> # output
> na="Abc ghi jkl gugu"
>
>
> How is it done in Python?


Here is how to do it without regexps in python.
The first and last line below are all that are needed. The others show
intermediate expressions that lead to the result.

>>> from itertools import groupby


>>> na="Abc | def | ghi | jkl [gugu]"
>>> [(g[0], ''.join(g[1])) for g in groupby(na, lambda c: c not in ' \t|[]')]

[(True, 'Abc'), (False, ' | '), (True, 'def'), (False, ' | '), (True,
'ghi'), (False, ' | '), (True, 'jkl'), (False, ' ['), (True, 'gugu'),
(False, ']')]
>>> [''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0]]

['Abc', 'def', 'ghi', 'jkl', 'gugu']

>>> ' '.join(''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0])

'Abc def ghi jkl gugu'
>>>



- Paddy.

 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      01-06-2007

Paddy wrote:

> Michael M. wrote:
>
> > In Perl, it was:
> >
> >
> > ## Example: "Abc | def | ghi | jkl"
> > ## -> "Abc ghi jkl"
> > ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st
> > pipe).
> > $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g;
> >
> > ## -- remove [ and ] in text
> > $na =~ s/\[//g;
> > $na =~ s/\]//g;
> > # print "DEB: \"$na\"\n";
> >
> >
> > # input string
> > na="Abc | def | ghi | jkl [gugu]"
> > # output
> > na="Abc ghi jkl gugu"
> >
> >
> > How is it done in Python?

>
> Here is how to do it without regexps in python.
> The first and last line below are all that are needed. The others show
> intermediate expressions that lead to the result.
>
> >>> from itertools import groupby

>
> >>> na="Abc | def | ghi | jkl [gugu]"
> >>> [(g[0], ''.join(g[1])) for g in groupby(na, lambda c: c not in ' \t|[]')]

> [(True, 'Abc'), (False, ' | '), (True, 'def'), (False, ' | '), (True,
> 'ghi'), (False, ' | '), (True, 'jkl'), (False, ' ['), (True, 'gugu'),
> (False, ']')]
> >>> [''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0]]

> ['Abc', 'def', 'ghi', 'jkl', 'gugu']
>
> >>> ' '.join(''.join(g[1]) for g in groupby(na, lambda c: c not in ' \t|[]') if g[0])

> 'Abc def ghi jkl gugu'
> >>>

>
>
> - Paddy.


And I leave the deletion of def to the reader

(i.e: I missed that bit and adding it in would make a long
comprehension too long to comprehend).

 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      01-06-2007
I have a python (2.5) program with number of worker threads, and I want
to make sure that each of these does a context switch at appropriate
times, to avoid starvation. I know that I can do a time.sleep(0.001) to
force such a switch, but I'm wondering if this is the recommended
method.

Thanks in advance.


--
Lloyd Zusman
http://www.velocityreviews.com/forums/(E-Mail Removed)
God bless you.

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-06-2007
Lloyd Zusman <(E-Mail Removed)> wrote:

> I have a python (2.5) program with number of worker threads, and I want
> to make sure that each of these does a context switch at appropriate
> times, to avoid starvation. I know that I can do a time.sleep(0.001) to
> force such a switch, but I'm wondering if this is the recommended
> method.


The recommended method is to start a new thread rather than following up on
an existing thread with an unrelated question.

Why do you think that just letting the threads run won't have the effect
you desire? Leave it to the system to schedule the threads.
 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      01-06-2007
Duncan Booth <(E-Mail Removed)> writes:

> Lloyd Zusman <(E-Mail Removed)> wrote:
>
>> I have a python (2.5) program with number of worker threads, and I want
>> to make sure that each of these does a context switch at appropriate
>> times, to avoid starvation. I know that I can do a time.sleep(0.001) to
>> force such a switch, but I'm wondering if this is the recommended
>> method.

>
> The recommended method is to start a new thread rather than following up on
> an existing thread with an unrelated question.


I accidentally hit "a" in my mailer instead of "w" ("reply" instead of
"compose"). Geez. It was an accident. I'm sorry.


> Why do you think that just letting the threads run won't have the effect
> you desire? Leave it to the system to schedule the threads.


I can already see that they don't have the effect I desire. They are
long numerical calculations in tight loops. I have to periodically put
explicit time.sleep(0.001) calls in place to force the context
switching, and I was wondering if that's the recommended method.


--
Lloyd Zusman
(E-Mail Removed)
God bless you.

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-06-2007
Lloyd Zusman <(E-Mail Removed)> wrote:

>> Why do you think that just letting the threads run won't have the
>> effect you desire? Leave it to the system to schedule the threads.

>
> I can already see that they don't have the effect I desire. They are
> long numerical calculations in tight loops. I have to periodically
> put explicit time.sleep(0.001) calls in place to force the context
> switching, and I was wondering if that's the recommended method.
>

Not really.

If the context isn't switching enough for you then try calling
sys.setcheckinterval(n) with varying values of n until you find one which
is suitable. Calling it with a lower value of n will increase the frequency
that you switch thread contexts, although of course it will also increase
the overall runtime for your program.

Alternatively you could try splitting your processing into smaller chunks
and ensure each thread does a small chunk at a time instead of a large one.

Why does it matter whether individual threads are being 'starved'? Surely
you want them all to complete in any case, so does it matter if they run
sequentially or in parallel?

 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      01-06-2007
Duncan Booth <(E-Mail Removed)> writes:

> [ ... ]
>
> If the context isn't switching enough for you then try calling
> sys.setcheckinterval(n) with varying values of n until you find one which
> is suitable. Calling it with a lower value of n will increase the frequency
> that you switch thread contexts, although of course it will also increase
> the overall runtime for your program.


Thank you very much. The sys.setcheckinterval function is what I need.
It seems that the original writer of the app had set this interval to a
high value in a part of the code that I overlooked until you mentioned
this right now.


> [ ... ]
>
> Why does it matter whether individual threads are being 'starved'? Surely
> you want them all to complete in any case, so does it matter if they run
> sequentially or in parallel?


Because some of the threads perform monitoring and notification that
need to occur in a timely fashion. Since these threads are doing IO,
they switch context appropriately, but once one of the big
number-crunching threads gets control, it starves out the monitoring
threads, which is not a good thing for my app ... or at least it did
so with the original large checkinterval.


--
Lloyd Zusman
(E-Mail Removed)
God bless you.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
loop beats generator expr creating large dict!? George Young Python 7 10-04-2006 04:30 PM
location mailboxes Outlook Expr. in W2000P Doc Computer Support 4 01-14-2006 11:49 AM
<%= expr %> sort of thing *inside* control tags spacehopper_man@yahoo.com ASP .Net 1 09-26-2005 06:32 PM
extend for loop syntax with if expr like listcomp&genexp ? Bengt Richter Python 6 07-14-2005 07:19 AM



Advertisments