KBD

Keith Devens .com

Friday, August 29, 2008 Flag waving
C++ is history repeated as tragedy. Java is history repeated as farce. – Scott McKay
← Perl is difficult to program correctly inThe truth about the Baghdad museum →

Daily link icon Thursday, June 12, 2003

Python rocks, yet again

I'm converting that Perl code I've been writing to Python, since I'm writing a library I want to be available in lots of languages. Python is such a joy. It's so funny because most of the conversion from Perl to Python is simply removing things.

For instance, I just converted:

$self->{line_count} = scalar(@{$self->{lines}});

into:

self.line_count = len(self.lines)

Look how much nicer that is Smiley

And again...

#key
$_[1] = $self->trim(substr($_[0], 0, $n));

#value
$_[2] = $self->trim(substr($_[0], $n+1, $len-($n+1)));

turned into:

key   = str[:n].strip()
value = str[n+1:].strip()

(I think that's right). And trim was defined to be this:

sub trim{
    shift;
    my $string = shift;
    for ($string){
        s/^\s+//;
        s/\s+$//;
    }
    return $string;
}

Ok, well I finished converting that code. I haven't run it yet, so there are definitely errors, but I don't think it would significantly affect the fact that just after doing a braindead simple port of the code from Perl to Python I went from 287 lines to 225 lines.

Ok, take it back. I thought you could do more inline in Python than you can actually do. So, that expanded it to 235 lines, and now it compiles Smiley Still a big improvement.

← Perl is difficult to program correctly inThe truth about the Baghdad museum →

Comments XML gif

anonymous wrote:

why u should use this :
$self->{line_count} = scalar(@{$self->{lines}});

when u can do this :

$$self{line_count} = @{$$self{lines}};

why this :
#key
$_[1] = $self->trim(substr($_[0], 0, $n));
#value
$_[2] = $self->trim(substr($_[0], $n+1, $len-($n+1)));

and not this :
($_[1],$_[2]) = $_[0] =~ /^\s*(.+?)\s+(.+?)\s*$/

seems to me u have made it bigger on purpose to mislead
the ppl reading this :")... Or the other way is that u
think better in Python rather in Perl terms which is not in any way worse, just different way..

∴ anonymous | 12-Jul-2003 9:42am est | #2408

Keith (http://www.keithdevens.com/) wrote:

For the first example you bring up, I dunno, I'll have to see if that's the same. However, I guarantee you the second example is not the same. In fact, my trim code is taken straight from perlfaq4 (although someone may have found a more Perlish way to do it). Besides the trim though, how could you possibly know what I'm substringing?

Keith | 12-Jul-2003 11:32am est | http://www.keithdevens.com/ | #2410

raptor wrote:

u are correct i was guessing... :")
anyway probably a simple change to the regex would solve your problem...
My point was that a simple one-liner by one-liner comparison is neither fair nor correct when someone have to compare languages...
I'm sure I can find many such cases that Python will look much badly, even that I'm not Python programmer..
It is in most of the cases personal feeling and "acustumisation" which language he will choose...
In your case .strip() was a unfair comparison 'cause it is not something that show something fundamentaly advantegous over perl.

just my 5c, dont take it personal :")

PS. in the past i was reading somewhere for program written in Java (around 30-40 lines of code) that the author calculated was faster and shorter than similar program written in Perl(afaik he wrote it).. what came out during a discussion of perl monks was a several perl solutions to the same problem written in 2-3 lines of perl-code which was faster.. so u see even a litlle longer program can't be trusted for such comparison..
so all depend on the context..etc..etc..

∴ raptor | 12-Jul-2003 12:54pm est | #2411

Keith (http://www.keithdevens.com/) wrote:

what came out during a discussion of perl monks was a several perl solutions to the same problem written in 2-3 lines of perl-code which was faster

Yeah, it's amazing what they can do sometimes.

In your case .strip() was a unfair comparison 'cause it is not something that show something fundamentaly advantegous over perl.

Well, it wasn't just the strip. The code also highlighted A. named function parameters, and B. that strings have list operators like ranges, in addition to the strip() trim() comparison. In fact, Python is pretty good that way in that many different containers (strings, lists, dictionaries, etc.) use list syntax. I've complained about this with Perl in the past: that because Perl uses different syntax for everything, it often makes it very hard to abstract things.

You're right though, these examples don't necessarily show that Python is "better" than Perl. Python is usually much more comfortable to me though. And I did wind up with a much shorter program, though by the time I finished it wasn't as much shorter as when I wrote this post.

Keith | 12-Jul-2003 1:05pm est | http://www.keithdevens.com/ | #2412

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Johnny Walker Blue Label

Wow, thanks for the scotch review​:D

Lagavulin and Laphroaig are​some of...

Keith: Aug 29, 3:35pm

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.142s.

(Used 8 db queries)

mobile phone