[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby vs Python (IMHO

Daniel Carrera

11/29/2003 6:48:00 PM

Hi all,

I guess I just want to share a tiny "case study" from a project that I'm
doing in Python and which I wish I could do in Ruby instead.

I am writing a BibTeX filter for the OpenOffice project (BibTeX is the
bibliographic cousin of LaTeX). I am doing it in Python because, of the
languages with stable bridges to the OOo component model (UNO) Python is
the one I like best.

My requirements are mostly related to regular expressions and string
methods. This is an area where Ruby really shines and is light-years
ahead of the competition.

Regular expressions in Ruby are infinitely eaiser and more sensible to
use than in Python.

Ruby:
str =~ /^(#{char})\s*{\s*(#{char}+),(.*)}/


Python:
regex = re.compile(r"^(" + char + "+)\s*{\s*(" + char + "+),(.*)}")
regex.match(str)


Like this, there are many exaples. Quite simply, strings are much easier
to manipulate in Ruby. Regexes are superior, there is varible
substitution, and the string methods are much better.

To make a long story short. If I could do this in Ruby the parser would
be easier to write, more understandable, and I would be more confident of
its correctness.

Cheers,
--
Daniel Carrera | Top 100 things you don't want the sysadmin to say...
PhD student. |
Math Dept. UMD | 19. hey, what does mkfs do?

11 Answers

ptkwt

11/29/2003 7:04:00 PM

0

In article <20031129184817.GA1426@math.umd.edu>,
Daniel Carrera <dcarrera@math.umd.edu> wrote:
>Hi all,
>
>I guess I just want to share a tiny "case study" from a project that I'm
>doing in Python and which I wish I could do in Ruby instead.
>
>I am writing a BibTeX filter for the OpenOffice project (BibTeX is the
>bibliographic cousin of LaTeX). I am doing it in Python because, of the
>languages with stable bridges to the OOo component model (UNO) Python is
>the one I like best.

Hmmm.... so what would it take to include Ruby in the set of languages
with 'stable bridges to the OOo component model'?

>
>My requirements are mostly related to regular expressions and string
>methods. This is an area where Ruby really shines and is light-years
>ahead of the competition.
>
>Regular expressions in Ruby are infinitely eaiser and more sensible to
>use than in Python.
>
>Ruby:
> str =~ /^(#{char})\s*{\s*(#{char}+),(.*)}/
>
>
>Python:
> regex = re.compile(r"^(" + char + "+)\s*{\s*(" + char + "+),(.*)}")
> regex.match(str)
>

Ouch, that's painful.

Phil

Mark J. Reed

11/29/2003 7:41:00 PM

0

DC = Daniel Carrera
PT = Phil Tomson

DC> Ruby:
DC> str =~ /^(#{char})\s*{\s*(#{char}+),(.*)}/
DC>
DC>
DC> Python:
DC> regex = re.compile(r"^(" + char + "+)\s*{\s*(" + char + "+),(.*)}")
DC> regex.match(str)
DC>

PT> Ouch, that's painful.

Eh, it's typical of languages that don't have a regex literal syntax,
which would be most languages. Although most regex APIs do provide
a one-step version that takes a string, compiles it for you, and
then does the match; and Python is no exception:

re.match("^(" + char + r"+)\s*{\s*(" + char + "+),(.*)}"

Of course, you'd want to use the two-step version if you were going to
use the same regex multiple times, for efficiency's sake.

That leaves the lack of string interpolation as the main annoyance, and many
people, especially those who learned to program with classical languages,
explicit appends much clearer than interpolation, because their eyes sort
of skip right over string literals in "nothing to see here" mode.

(r"..." is Python for '...', btw: strings without special character
sequence processing. The 'r' stands for "raw" strings.)

-Mark

Mark J. Reed

11/29/2003 7:42:00 PM

0

A couple insertions below:

On Sat, Nov 29, 2003 at 07:40:45PM +0000, Mark J. Reed wrote:
> re.match("^(" + char + r"+)\s*{\s*(" + char + "+),(.*)}"
,str)

> That leaves the lack of string interpolation as the main annoyance, and many
> people, especially those who learned to program with classical languages,
find
> explicit appends much clearer than interpolation, because their eyes sort
> of skip right over string literals in "nothing to see here" mode.

-Mark

Daniel Carrera

11/29/2003 8:43:00 PM

0

On Sun, Nov 30, 2003 at 04:27:11AM +0900, Phil Tomson wrote:

> >I am writing a BibTeX filter for the OpenOffice project (BibTeX is the
> >bibliographic cousin of LaTeX). I am doing it in Python because, of the
> >languages with stable bridges to the OOo component model (UNO) Python is
> >the one I like best.
>
> Hmmm.... so what would it take to include Ruby in the set of languages
> with 'stable bridges to the OOo component model'?

I'm not entirely sure because I know so little about UNO.

I asked around and they said that the easiest way would involve using the
Java implementation of Ruby, alongside the scripting framework. The
scripting framework is just about to be added to the development branch of
OOo. There's not much information about it just yet, but I plan on
bugging the framework people about it.


> >Ruby:
> > str =~ /^(#{char})\s*{\s*(#{char}+),(.*)}/
> >
> >
> >Python:
> > regex = re.compile(r"^(" + char + "+)\s*{\s*(" + char + "+),(.*)}")
> > regex.match(str)
> >
>
> Ouch, that's painful.


Tha's what I thought. And regexes make up the bulk of the filter (the
hardest part is parsing BibTeX).

Cheers,
--
Daniel Carrera | Top 100 things you don't want the sysadmin to say...
PhD student. |
Math Dept. UMD | 19. hey, what does mkfs do?


Daniel Carrera

11/29/2003 8:44:00 PM

0

On Sun, Nov 30, 2003 at 04:42:08AM +0900, Mark J. Reed wrote:

> (r"..." is Python for '...', btw: strings without special character
> sequence processing. The 'r' stands for "raw" strings.)

I had been wondering about that. Thanks.

--
Daniel Carrera | Top 100 things you don't want the sysadmin to say...
PhD student. |
Math Dept. UMD | 19. hey, what does mkfs do?


Nikolai Weibull

11/29/2003 9:45:00 PM

0

* Mark J. Reed <markjreed@mail.com> [Nov, 29 2003 20:50]:
> (r"..." is Python for '...', btw: strings without special character
> sequence processing. The 'r' stands for "raw" strings.)
yeah, isn't it obvious? ;-). it really stands out well. this is just
one example of things that seem to be hacks in Python,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Joey Gibson

11/30/2003 1:16:00 AM

0

On 11/29/2003 3:43 PM, Daniel Carrera wrote:

>I'm not entirely sure because I know so little about UNO.
>
>I asked around and they said that the easiest way would involve using the
>Java implementation of Ruby, alongside the scripting framework. The
>scripting framework is just about to be added to the development branch of
>OOo. There's not much information about it just yet, but I plan on
>bugging the framework people about it.
>
>

Do you have a URL to check this out? I could take a look at what might
be involved with using JRuby like you said...

--
Never trust a girl with your mother's cow
never let your trousers go falling down in the green grass...

http://www.joeygibso...
http://www.joeygibso.../life/Wisdom.html





Daniel Carrera

11/30/2003 1:19:00 AM

0

> >I'm not entirely sure because I know so little about UNO.
> >
> >I asked around and they said that the easiest way would involve using the
> >Java implementation of Ruby, alongside the scripting framework. The
> >scripting framework is just about to be added to the development branch of
> >OOo. There's not much information about it just yet, but I plan on
> >bugging the framework people about it.
>
> Do you have a URL to check this out? I could take a look at what might
> be involved with using JRuby like you said...

They've promised to have one up "any day now". In the mean time, take a
look at http://framework.open....

When the more relevant documentation gets written I'll give a shout at
ruby-talk.

--
Daniel Carrera | Top 100 things you don't want the sysadmin to say...
PhD student. |
Math Dept. UMD | 19. hey, what does mkfs do?

Charles Hixson

11/30/2003 3:05:00 AM

0

Nikolai Weibull wrote:

>* Mark J. Reed <markjreed@mail.com> [Nov, 29 2003 20:50]:
>
>
>>(r"..." is Python for '...', btw: strings without special character
>>sequence processing. The 'r' stands for "raw" strings.)
>>
>>
>yeah, isn't it obvious? ;-). it really stands out well. this is just
>one example of things that seem to be hacks in Python,
> nikolai
>
That's not a very good example. Ruby Q & q strings are just as bad,
even if they were imports from Perl.
For that matter Ruby has a simply HUGE number of ways of specifying
strings. This isn't usually a problem, but it could be seen as such.
My real problem with Python is that I simply hate using the amount white
space as semantically significant. Ruby has other conveniences. But on
the standard run of problems I deal with the difference isn't huge.



David Garamond

11/30/2003 5:06:00 AM

0

Nikolai Weibull wrote:
>>(r"..." is Python for '...', btw: strings without special character
>>sequence processing. The 'r' stands for "raw" strings.)
>
> yeah, isn't it obvious? ;-). it really stands out well. this is just
> one example of things that seem to be hacks in Python,

I don't see r"" as more hackish than Ruby's %Q(), %q(), %r() or Perl's
``, q(), qq(). It's just an alternative way to express literals.

--
dave