Help with regex search-and-replace (Perl to Python)

2010-02-06 Thread Schif Schaf
Hi,

I've got some text that looks like this:


Lorem [ipsum] dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut [labore] et [dolore] magna aliqua.

and I want to make it look like this:


Lorem {ipsum} dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut {labore} et {dolore} magna aliqua.

(brackets replaced by braces). I can do that with Perl pretty easily:


for (<>) {
s/\[(.+?)\]/\{$1\}/g;
print;
}


but am not able to figure out how to do it with Python. I start out
trying something like:


import re, sys
withbracks = re.compile(r'\[(.+?)\]')
for line in sys.stdin:
mat = withbracks.search(line)
if mat:
# Well, this line has at least one.
# Should be able to use withbracks.sub()
# and mat.group() maybe ... ?
line = withbracks.sub('{' + mat.group(0) + '}', line)
# No, that's not working right.

sys.stdout.write(line)


but then am not sure where to go with that.

How would you do it?

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Your beloved python features

2010-02-06 Thread Schif Schaf
On Feb 5, 8:49 am, Roald de Vries  wrote:

> My reasoning: I needed a language more powerful than bash, but more  
> portable and faster to develop (at least small scripts) than C/C++. So  
> I needed a scripting language. Python, Ruby, Perl, Tcl, ...?
>
> Python seems to be the language with the most libraries available,

I like Python a lot. That said, it looks like Perl's CPAN currently
has approximately double the number of packages in the Cheeseshop.

I'm guessing the CPAN is still growing, but I don't know how fast it
is growing compared to the Cheeseshop.

I won't venture a guess as to which has a higher percentage of older
unmaintained packages.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with regex search-and-replace (Perl to Python)

2010-02-06 Thread Schif Schaf
On Feb 7, 12:19 am, "Alf P. Steinbach"  wrote:

>
> I haven't used regexps in Python before, but what I did was (1) look in the
> documentation,

Hm. I checked in the repl, running `import re; help(re)` and the docs
on the `sub()` method didn't say anything about using back-refs in the
replacement string. Neat feature though.

> (2) check that it worked.
>
> 
> import re
>
> text = (
>      "Lorem [ipsum] dolor sit amet, consectetur",
>      "adipisicing elit, sed do eiusmod tempor",
>      "incididunt ut [labore] et [dolore] magna aliqua."
>      )
>
> withbracks = re.compile( r'\[(.+?)\]' )
> for line in text:
>      print( re.sub( withbracks, r'{\1}', line) )
> 
>

Seems like there's magic happening here. There's the `withbracks`
regex that applies itself to `line`. But then when `re.sub()` does the
replacement operation, it appears to consult the `withbracks` regex on
the most recent match it just had.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with regex search-and-replace (Perl to Python)

2010-02-07 Thread Schif Schaf
On Feb 7, 8:57 am, Tim Chase  wrote:
> Steve Holden wrote:
>
> > Really? Under what circumstances does a simple one-for-one character
> > replacement operation fail?
>
> Failure is only defined in the clarified context of what the OP
> wants :)  Replacement operations only fail if the OP's desired
> output from the above mess doesn't change *all* of the ]/[
> characters, but only those with some form of parity (nested or
> otherwise).  But if the OP *does* want all of the ]/[ characters
> replaced regardless of contextual nature, then yes, replace is a
> much better solution than regexps.
>

I need to do the usual "pipe text through and do various search/
replace" thing fairly often. The above case of having to replace
brackets with braces is only one example. Simple string methods run
out of steam pretty quickly and much of my work relies on using
regular expressions. Yes, I try to keep focused on simplicity, and
often regexes are the simplest solution for my day-to-day needs.
-- 
http://mail.python.org/mailman/listinfo/python-list


recommendation for webapp testing?

2009-09-15 Thread Schif Schaf
Hi,

I need to do some basic website testing (log into account, add item to
cart, fill out and submit forms, check out, etc.). What modules would
be good to use for webapp testing like this?

>From a bit of searching, it looks like twill was used for this, but it
hasn't been updated in some time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recommendation for webapp testing?

2009-09-16 Thread Schif Schaf
On Sep 16, 8:55 am, Simon Brunning  wrote:
> 2009/9/16 Schif Schaf :
>
>
> > I need to do some basic website testing (log into account, add item to
> > cart, fill out and submit forms, check out, etc.). What modules would
> > be good to use for webapp testing like this?
>
> <http://code.google.com/p/webdriver/> might be worth a look.

Thanks.

What's the difference between WebDriver and Selenium?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recommendation for webapp testing?

2009-09-16 Thread Schif Schaf
On Sep 16, 12:19 pm, Michele Simionato 
wrote:
>
> twill is still good.

Well, this http://twill.idyll.org/ seems to be the twill website, but
it looks pretty out of date.

I also found this http://code.google.com/p/twill/ , which is somewhat
newer. No activity in the last 3 and a half months though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recommendation for webapp testing?

2009-09-16 Thread Schif Schaf
After some more searching I found Mechanize (a Python version of
Perl's WWW::Mechanize):

http://wwwsearch.sourceforge.net/mechanize/

Anyone here tried it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Am I doing this wrong? Why does this seem so clumsy (time, datetime vs. DateTime)

2009-09-19 Thread Schif Schaf
The other day I needed to convert a date like "August 2009" into a
"seconds-since-epoch" value (this would be for the first day of that
month, at the first second of that day).

In Python, I came up with this:


#!/usr/bin/env python

import datetime
import time

time_in_sse = time.mktime(
datetime.datetime(2009, 8, 1).timetuple()
)

print time_in_sse


I *wanted* to just use time.mktime(), but it wouldn't work unless I
could specify the *complete* time tuple value (who would have all that
handy?!). I also wanted to then just do datetime.datetime
(...).secs_since_epoch(), but it didn't support a function like that
-- not one I could find anyway.

Note, to arrive at that above solution, I had to spend a fair amount
of time reading the docs on both the time and datetime modules, and
then wondering why the methods I wanted weren't there. Am I missing
something and maybe used the wrong methods/modules here?

Contrast this to Perl, where the solution I came up with in about 5
minutes was:


#!/usr/bin/env perl

use DateTime;
my $dt = DateTime->new(year => 2009, month => 8);
print $dt->epoch, "\n";


(it only took 5 minutes because the docs for DateTime tell you exactly
what you want to know right at the top on the first page of the docs.)
-- 
http://mail.python.org/mailman/listinfo/python-list