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? 13 2048
"Michael M." <mi*****@mustun.chwrites:
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 <jg****@gmail.com>
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.
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.
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).
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 lj*@asfast.com
God bless you.
Lloyd Zusman <lj*@asfast.comwrote:
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.
Duncan Booth <du**********@invalid.invalidwrites:
Lloyd Zusman <lj*@asfast.comwrote:
>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 lj*@asfast.com
God bless you.
Lloyd Zusman <lj*@asfast.comwrote:
>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?
Duncan Booth <du**********@invalid.invalidwrites:
[ ... ]
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 lj*@asfast.com
God bless you.
On 6 ene, 20:01, Lloyd Zusman <l...@asfast.comwrote:
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.
[...] 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.
This is why such settings should be in a configuration file or in a
prominent place in the application...
I had a program where, deep in an unknown function, the original coder
changed the process priority - with no valid reason, and in any case,
that should be an application-level setting. It was hard to find why,
after doing such and such things, the system responsiveness were so
slow.
--
Gabriel Genellina
"Michael M." <mi*****@mustun.chwrote:
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?
>>import re na="Abc | def | ghi | jkl [gugu]" m=re.match(r'(\w+ )\| (\w+ )\| (\w+ )\| (\w+ )\[(\w+)\]', na) na=m.expand(r'\1\2\3\5') na
'Abc def ghi gugu'
Florian
--
<http://www.florian-diesch.de/>
Florian Diesch schrieb:
"Michael M." <mi*****@mustun.chwrote:
>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?
>>>import re na="Abc | def | ghi | jkl [gugu]" m=re.match(r'(\w+ )\| (\w+ )\| (\w+ )\| (\w+ )\[(\w+)\]', na) na=m.expand(r'\1\2\3\5') na
'Abc def ghi gugu'
I'd rather have the groups grouped without the whitespaces
>>import re na="Abc | def | ghi | jkl [gugu]" m=re.match(r'(\w+) \| (\w+) \| (\w+) \| (\w+) \[(\w+)\]', na) na=m.expand(r'\1 \3 \4 \5') na
'Abc ghi jkl gugu'
Thomas th************@gmail.com 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?
You don't really need regular expressions for this simple transformation:
na="Abc | def | ghi | jkl [gugu]"
na=" ".join([x.strip() for x in na.replace("[","|").replace("]","").split("|")])
-Larry This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Robert Brewer |
last post by:
WARNING:
The following is NOT all Pythonic. However, it is Python. This is just
fun--nothing I'm going to put into production. I don't have any
questions or problems--just thought I'd share.
...
|
by: paolo veronelli |
last post by:
I've a vague idea of the differences,I don't know scheme anyway.
I'd like to see an example to show what is missing in python about
closures and
possibly understand if ruby is better in this...
|
by: Rudi Hansen |
last post by:
I dont seem to be able to find the switch statement in Python.
I would like to be able to do
switch(var)
case 1 :
print "var = 1"
case 2:
print "var = 2"
|
by: pekka niiranen |
last post by:
Hi there,
I have perl script that uses dynamically
constructed regular in this way:
------perl code starts ----
$result "";
$key = AAA\?01;
$key = quotemeta $key;
$line = " ...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: Paul Cochrane |
last post by:
Hi all,
I've got an application that I'm writing that autogenerates python code
which I then execute with exec(). I know that this is not the best way to
run things, and I'm not 100% sure as to...
|
by: Mark Tarver |
last post by:
How do you compare Python to Lisp? What specific advantages do you
think that one has over the other?
Note I'm not a Python person and I have no axes to grind here. This is
just a question for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |