Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python -Vs- Ruby: A regexp match to the death!

Reply
Thread Tools

Python -Vs- Ruby: A regexp match to the death!

 
 
rantingrick
Guest
Posts: n/a
 
      08-09-2010
Hello folks,

You all know i been forced to use Ruby and i am not happy about that.
But i thought i would share more compelling evidence of the moronicity
of the Ruby language syntax from the perspective of regexp's.

I recently built myself a nice little Ruby script editor because i
hate everything else out there. Whist writing the Colorizer i realized
(again) just how beautifully elegant Python is and how crufty and
asinine Ruby is. Anyhow my point is that by looking at the regexp's
you can clearly see that parsing Ruby syntax is BF and Python syntax
is elegant! Here are a few examples: Note i used look back assertions
for clarity.
--------------------
Modules
--------------------
Python does not have a module syntax (an thank Guido for that!)
because we have a much better system of using the file as a module and
not introducing more cruft into our scripts. Anyway if Python *did*
have a module syntax it would look better than this crap!

Python: N/A
Ruby: r'(?<=module )(:?(\w+(:?)*'

--------------------
Classes
--------------------
Python and Ruby class definitions are almost the same except for the
module cruft getting in the way again.

Python: r'(?<=class )\w+'
Ruby: r'(?<=class )(:?(\w+(:?)*'

---------------------
Defs
---------------------
HaHa, you're going to poop yourself when you see this! No introduction
needed .

Python: r'(?<=def )\w+'
Ruby: r'(?<=def )(self\.)?((\w+::\w+)|(\w+\.\w+)|(\w+))([?|!])?'

---------------------
Strings
---------------------
Single line strings are exactly the same in both languages except in
Ruby double quoted strings are backslash interpreted and single quote
strings are basically raw. Except Ruby introduces more cruft (as
usual) in the form of what i call "lazy man" stings....

>>> a = %w{ one two three}

["one", "two", "three"]
>>> s = %{one two three}

one two three
>>> repat = %r{one two three}

/one two three/

.... only good for hand coding!

----------------------
Multi Line Strings
----------------------
Ha. Ruby does not really have multi line strings. Ruby has what they
call a "Here Doc". Besides picking the most boneheaded name for such
an object they also introduced and even more boneheaded syntax. To
define a "Here Doc" (god i hate that name!) you start with double
greater than ">>" and immediately follow with an identifier token of
you choice (it can be anything your dirty little mind can come up
with.

>>HEREDOC

this is the body
of a
here doc. Why the
hell did they not just
use triple quotes like Python did.
Now i will need to remember some token to know where'
i stopped
HEREDOC

As you can see it is another example of tacked on functionality that
was not carefully considered before hand. Anyway here are the
regexp's...

Python: r'""".*?"""'
Python: r"'''.*?'''"
Ruby: r'<<(\w+).*?(\1)'

--------------------------
Comments
--------------------------
Ruby and Python single line comments are the same. Use the hash char.
However Ruby introduces multi line comment blocks delimited by the
tokens "=begin" and "=end".

Python: r"#.*"
Ruby: r"=begin.*?=end"
Ruby: r"#.*"

-------------------------
Conculsion
-------------------------
I just want to take this opportunity to thank Mr. Van Rossum and the
Python dev team for creating a truly revolutionary 21st century
language that no other language can hold a candle to. Without Python
we would be force to use these "other" monstrosities" on a daily basis
-- and i just don't think i could bear it! Keep up the good work!

 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      08-09-2010
rantingrick wrote:
> Hello folks,
>

[snip]
> ---------------------
> Strings
> ---------------------
> Single line strings are exactly the same in both languages except in
> Ruby double quoted strings are backslash interpreted and single quote
> strings are basically raw. Except Ruby introduces more cruft (as
> usual) in the form of what i call "lazy man" stings....
>
>>>> a = %w{ one two three}

> ["one", "two", "three"]
>>>> s = %{one two three}

> one two three
>>>> repat = %r{one two three}

> /one two three/
>
> ... only good for hand coding!
>

From Perl.

> ----------------------
> Multi Line Strings
> ----------------------
> Ha. Ruby does not really have multi line strings. Ruby has what they
> call a "Here Doc". Besides picking the most boneheaded name for such
> an object they also introduced and even more boneheaded syntax. To
> define a "Here Doc" (god i hate that name!) you start with double
> greater than ">>" and immediately follow with an identifier token of
> you choice (it can be anything your dirty little mind can come up
> with.
>
>>> HEREDOC

> this is the body
> of a
> here doc. Why the
> hell did they not just
> use triple quotes like Python did.
> Now i will need to remember some token to know where'
> i stopped
> HEREDOC
>
> As you can see it is another example of tacked on functionality that
> was not carefully considered before hand. Anyway here are the
> regexp's...
>
> Python: r'""".*?"""'
> Python: r"'''.*?'''"
> Ruby: r'<<(\w+).*?(\1)'
>

Also from Perl.

I don't know what the point of your post was. We already know that we
prefer Python; that's why we're here!

And anyway, being nasty about other languages feels unPythonic to me...
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-09-2010
On Sun, 08 Aug 2010 17:43:03 -0700, rantingrick wrote:

> Ha. Ruby does not really have multi line strings.


Except, of course, it does, as you go on to show.


> Ruby has what they
> call a "Here Doc". Besides picking the most boneheaded name for such an
> object


It's standard terminology that has been around for a long time in many
different languages.

http://en.wikipedia.org/wiki/Here_document


> they also introduced and even more boneheaded syntax. To define a
> "Here Doc" (god i hate that name!) you start with double greater than
> ">>" and immediately follow with an identifier token of you choice (it
> can be anything your dirty little mind can come up with.
>
>>>HEREDOC

> this is the body
> of a
> here doc. Why the
> hell did they not just
> use triple quotes like Python did.
> Now i will need to remember some token to know where' i stopped
> HEREDOC



Incorrect.

[steve@sylar ~]$ irb
irb(main):001:0> s = >>END
SyntaxError: compile error
(irb):1: syntax error
s = >>END
^
from (irb):1
irb(main):002:0> s = <<-END
irb(main):003:0" Multi-line text
irb(main):004:0" goes here
irb(main):005:0" END
=> "Multi-line text\ngoes here\n"
irb(main):006:0> puts s
Multi-line text
goes here
=> nil
irb(main):007:0>



> As you can see it is another example of tacked on functionality that was
> not carefully considered before hand.


I disagree. It's an old and venerable technique, and very useful on the
rare occasion that you have lots of quotation marks in a string.

Whether those rare occasions are common enough to require specialist
syntax is another question. In Python, the idea is that two heredocs ('''
and """) is enough for anybody. That makes it difficult to write a string
literal like, e.g.:

Python strings have four delimiters:
(1) single quote '
(2) double quote "
(3) single-quote here-doc '''
(4) double-quote here-doc """

plus equivalent raw-strings of each kind.

Trying writing that as a single literal in Python without escapes. There
are work-arounds, of course, like using implicit concatenation, but
they're ugly.

In Ruby they decided to be more general, so you can define whatever
heredoc you need to quote whatever literal string you need. That's not
bone-headed.



--
Steven
 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      08-09-2010
On Aug 8, 8:15*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:

> In Ruby they decided to be more general, so you can define whatever
> heredoc you need to quote whatever literal string you need. That's not
> bone-headed.


Devils Advocate!

PS: Man you're irb main was so full of cobweb i could barley see the
code... haa... haaaa... hachew!.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-09-2010
On Mon, 09 Aug 2010 00:29:19 -0700, rantingrick wrote:

> On Aug 8, 8:15*pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>
>> In Ruby they decided to be more general, so you can define whatever
>> heredoc you need to quote whatever literal string you need. That's not
>> bone-headed.

>
> Devils Advocate!
>
> PS: Man you're irb main was so full of cobweb i could barley see the
> code... haa... haaaa... hachew!.


irb's default prompt is a bit too verbose for my tastes, but Python
allows you to customise its prompt too. You'll often see people here
posting copy/pastes with a customised prompt, so obviously some people
like that sort of thing.

Me, my biggest gripe with the interactive interpreter is that using >>>
as a prompt clashes with > as the standard quoting character in email and
news, but Guido has refused to even consider changing it.

And that it's quite finicky about blank lines between methods and inside
functions. Makes it hard to paste code directly into the interpreter.

And that pasting doesn't strip out any leading prompts. It needs a good
doctest mode.


--
Steven
 
Reply With Quote
 
Stefan Schwarzer
Guest
Posts: n/a
 
      08-09-2010
Hi Steven,

On 2010-08-09 10:21, Steven D'Aprano wrote:
> And that it's quite finicky about blank lines between methods and inside
> functions. Makes it hard to paste code directly into the interpreter.
>
> And that pasting doesn't strip out any leading prompts. It needs a good
> doctest mode.


ipython [1] should help here:

IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: %paste?
Type: Magic function
Base Class: <type 'instancemethod'>
String Form: <bound method InteractiveShell.magic_paste of <IPython.iplib.InteractiveShell object at 0xb740096c>>
Namespace: IPython internal
File: /usr/lib/pymodules/python2.6/IPython/Magic.py
Definition: %paste(self, parameter_s='')
Docstring:
Allows you to paste & execute a pre-formatted code block from clipboard.

The text is pulled directly from the clipboard without user
intervention.

The block is dedented prior to execution to enable execution of method
definitions. '>' and '+' characters at the beginning of a line are
ignored, to allow pasting directly from e-mails, diff files and
doctests (the '...' continuation prompt is also stripped). The
executed block is also assigned to variable named 'pasted_block' for
later editing with '%edit pasted_block'.

You can also pass a variable name as an argument, e.g. '%paste foo'.
This assigns the pasted block to variable 'foo' as string, without
dedenting or executing it (preceding >>> and + is still stripped)

'%paste -r' re-executes the block previously entered by cpaste.

IPython statements (magics, shell escapes) are not supported (yet).

See also
--------
cpaste: manually paste code into terminal until you mark its end.

Unfortunatey, when I enter

In [2]: %paste

at the prompt it gives me (before I pasted anything)

In [2]: %paste
------------------------------------------------------------
File "<string>", line 1
http://pypi.python.org/pypi/ipython/0.10
^
SyntaxError: invalid syntax

So far, I couldn't find anything on the net on this.

[1] http://pypi.python.org/pypi/ipython

Stefan
 
Reply With Quote
 
Mike Kent
Guest
Posts: n/a
 
      08-09-2010
On Aug 8, 8:43*pm, rantingrick <(E-Mail Removed)> wrote:
> Hello folks,
>
> You all know i been forced to use Ruby and i am not happy about that.


***Blablabla cut long rant***

Xah, this is really you, isn't it. Come on, confess.
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      08-09-2010
On 2010-08-09 06:42 , Stefan Schwarzer wrote:
> Hi Steven,
>
> On 2010-08-09 10:21, Steven D'Aprano wrote:
>> And that it's quite finicky about blank lines between methods and inside
>> functions. Makes it hard to paste code directly into the interpreter.
>>
>> And that pasting doesn't strip out any leading prompts. It needs a good
>> doctest mode.

>
> ipython [1] should help here:
>
> IPython 0.10 -- An enhanced Interactive Python.
> ? -> Introduction and overview of IPython's features.
> %quickref -> Quick reference.
> help -> Python's own help system.
> object? -> Details about 'object'. ?object also works, ?? prints more.
> In [1]: %paste?
> Type: Magic function
> Base Class:<type 'instancemethod'>
> String Form:<bound method InteractiveShell.magic_paste of<IPython.iplib.InteractiveShell object at 0xb740096c>>
> Namespace: IPython internal
> File: /usr/lib/pymodules/python2.6/IPython/Magic.py
> Definition: %paste(self, parameter_s='')
> Docstring:
> Allows you to paste& execute a pre-formatted code block from clipboard.
>
> The text is pulled directly from the clipboard without user
> intervention.
>
> The block is dedented prior to execution to enable execution of method
> definitions. '>' and '+' characters at the beginning of a line are
> ignored, to allow pasting directly from e-mails, diff files and
> doctests (the '...' continuation prompt is also stripped). The
> executed block is also assigned to variable named 'pasted_block' for
> later editing with '%edit pasted_block'.
>
> You can also pass a variable name as an argument, e.g. '%paste foo'.
> This assigns the pasted block to variable 'foo' as string, without
> dedenting or executing it (preceding>>> and + is still stripped)
>
> '%paste -r' re-executes the block previously entered by cpaste.
>
> IPython statements (magics, shell escapes) are not supported (yet).
>
> See also
> --------
> cpaste: manually paste code into terminal until you mark its end.
>
> Unfortunatey, when I enter
>
> In [2]: %paste
>
> at the prompt it gives me (before I pasted anything)
>
> In [2]: %paste
> ------------------------------------------------------------
> File "<string>", line 1
> http://pypi.python.org/pypi/ipython/0.10
> ^
> SyntaxError: invalid syntax


Yes, that's because you had that URL in your clipboard, not Python code. What
were you expecting to happen?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Stefan Schwarzer
Guest
Posts: n/a
 
      08-09-2010
Hi Robert,

On 2010-08-09 22:23, Robert Kern wrote:
> On 2010-08-09 06:42 , Stefan Schwarzer wrote:
>> Unfortunatey, when I enter
>>
>> In [2]: %paste
>>
>> at the prompt it gives me (before I pasted anything)
>>
>> In [2]: %paste
>> ------------------------------------------------------------
>> File "<string>", line 1
>> http://pypi.python.org/pypi/ipython/0.10
>> ^
>> SyntaxError: invalid syntax

>
> Yes, that's because you had that URL in your clipboard, not Python code. What
> were you expecting to happen?


I got that traceback as soon as I typed in "%paste" and
pressed enter, without pasting anything in the terminal.
I had assumed it works like aste in Vim, activating a
kind of "paste mode" where everything pasted into the
terminal is modified as the help text suggests.

Ok, I just noticed I should have actually _read_ the
help text, not just scanned it. Sorry for the
confusion.

Stefan



 
Reply With Quote
 
Stefan Schwarzer
Guest
Posts: n/a
 
      08-09-2010
On 2010-08-09 23:43, Stefan Schwarzer wrote:
> I got that traceback as soon as I typed in "%paste" and
> pressed enter, without pasting anything in the terminal.
> I had assumed it works like aste in Vim, activating a


I meant ":set paste" of course.

Stefan
 
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
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments