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

R

rantingrick

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 :-D.

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.
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!
 
M

MRAB

rantingrick said:
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.

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...
 
S

Steven D'Aprano

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.

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.
 
R

rantingrick

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!. ;-)
 
S

Steven D'Aprano

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.
 
S

Stefan Schwarzer

Hi Steven,

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
 
M

Mike Kent

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.
 
R

Robert Kern

Hi Steven,

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
 
S

Stefan Schwarzer

Hi Robert,

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 :paste 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
 
S

Stefan Schwarzer

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 :paste in Vim, activating a

I meant ":set paste" of course.

Stefan
 
S

sturlamolden

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

The combination of editor, debugger and interpreter is what I miss
most from Matlab. In Matlab we can have a function or script open in
an editor, and use it directly from the interpreter. No need to
reimport or anything: edit and invoke. It is also possible to paste
data directly from the clipboard into variables in the interpreter.

ipython does not have that annoying >>> prompt.
 
R

Robert Kern

Hi Robert,

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 :paste in Vim, activating a
kind of "paste mode" where everything pasted into the
terminal is modified as the help text suggests.

%cpaste will do that. I implemented %paste because not all terminals will
correctly paste arbitrary amounts of code correctly. Grabbing the text directly
from the clipboard is less error-prone and removes redundant user interaction.

--
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
 
R

rantingrick

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

Just because something has been around around for a long time does not
necessarily mean it's was a good idea to begin with. STRAWMAN!
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.

(...snip...)
  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.

Yes, with the choices we have today writing strings like you mention
is terribly asinine. And don't forget about filepaths and regexps too
with all the backslashing nonsense! However, there is a simple
solution to this mess. Python "double quote strings" and Python
"""multiline strings"""(that are delimited by leading and trailing
double quote triplets) should behave as they do today.

However Python 'single quote strings' and Python '''multiline
strings'''(that are delimited by leading and trailing single quote
triplets) should be raw so that they do not interpret escape
sequences. Yes i know this would break backwards compatibility *again*
but this functionality should have been made available in Py3000 since
we were already breaking it anyhow.

Why do we need both """X""" AND '''X''' this if they do exactly the
same thing? Also why do we need both "X" AND 'X' if they do exactly
the same thing. A real chance to make something special was missed and
i hope one day we come to the realization that this proposed
functionality of strings (raw and normal) is sorely needed in Python.
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.

The fact that Ruby has multi line strings (*ahem*... HEREDOC's) is not
at all the point i take issue with. I take issue with the boneheaded
syntax. Have you ever tried to grep Ruby heredocs? It would have been
so much easier if they had made a spec like this...

mystring = :{
blah blah blah
blahblah
blah blah blah
blah
}:

Or at least *some* static token instead of just creating something on
the fly each time.... now thats boneheaded!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top