473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python vs Perl (an example)

Hello,

I recently wrote a Perl version of pyAlbum.py [1] -- a
Python script to create an image album from a given
directory -- plAlbum.pl [2].

It made me realize how easy-to-use Python is.

[1]
http://aspn.activestate.com/ASPN/Coo.../Recipe/271246
[2]
http://www.premshree.resource-locato...erl/plAlbum.pl

-Premshree Pillai

=====
-Premshree
[http://www.qiksearch.com/]

_______________ _______________ _______________ _______________ ____________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more.
Go to: http://in.insurance.yahoo.com/licspecial/index.html

Jul 18 '05 #1
5 2430
Premshree Pillai wrote:
Hello,

I recently wrote a Perl version of pyAlbum.py [1] -- a
Python script to create an image album from a given
directory -- plAlbum.pl [2].

It made me realize how easy-to-use Python is.

[1]
http://aspn.activestate.com/ASPN/Coo.../Recipe/271246
[2]
http://www.premshree.resource-locato...erl/plAlbum.pl

-Premshree Pillai

=====
-Premshree
[http://www.qiksearch.com/]

_______________ _______________ _______________ _______________ ____________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more.
Go to: http://in.insurance.yahoo.com/licspecial/index.html

That will help me in my debates thnak you ;-)

Jul 18 '05 #2
Premshree Pillai wrote:
Hello,

I recently wrote a Perl version of pyAlbum.py [1] -- a
Python script to create an image album from a given
directory -- plAlbum.pl [2].

It made me realize how easy-to-use Python is.

[1]
http://aspn.activestate.com/ASPN/Coo.../Recipe/271246
[2]
http://www.premshree.resource-locato...erl/plAlbum.pl

-Premshree Pillai

=====
-Premshree
[http://www.qiksearch.com/]

_______________ _______________ _______________ _______________ ____________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more.
Go to: http://in.insurance.yahoo.com/licspecial/index.html


As a perl programmer I have to take issue with your code.

Why did you write len() when you could have used scalar which returns
the length of a list? Badly written Perl is hardly a fair comparison for
well written Python.

You might find Python easier than Perl but you are not a particulary
good Perl programmer.
Jul 18 '05 #3
Peter Hickman wrote:

[...]
You might find Python easier than Perl but you are not a particulary
good Perl programmer.


So, only good Perl programmers find Perl easier than Python? ;-)
*hides*

--
Chris Herborth ch****@cryptoca rd.com
Documentation Overlord, CRYPTOCard Corp. http://www.cryptocard.com/
Never send a monster to do the work of an evil scientist.
Postatem obscuri lateris nescitis.
Jul 18 '05 #4
Hello,

I recently wrote a Perl version of pyAlbum.py [1] -- a
Python script to create an image album from a given
directory -- plAlbum.pl [2].

It made me realize how easy-to-use Python is.

[1]
http://aspn.activestate.com/ASPN/Coo.../Recipe/271246
[2]
http://www.premshree.resource-locato...erl/plAlbum.pl

-Premshree Pillai

=====
-Premshree
[http://www.qiksearch.com/]

I cannot help, but make a couple of observations:

1. Although you point about Python being easy has some merit,
I would not bring up an awkwadly written Perl script to back up
that argument.

2. There are many things which are very easy to do in Perl, and
are very cumbersome in Python. Regular expressions is a
vivid example. Saying very generically that Python is easier
than Perl is just an invitation for flame wars.

3. Lastly, Perl is not aspiring to or has ever claimed to
be easy. That is not its strength and attraction.
(Although, on a simple example like this, a more conventional
Perl script (see below) is pretty comparable).

If you'd like to demonstrate that Python is easier than Perl, you'll
need to find a better case.

----------------- pyAlbum.pl -------------------------

=head

pyAlbum.pl

Perl version of the pyAlbum.py script
that creates an album
of images from a given directory

(c) 2004 Premshree Pillai (27/02/04)
http://www.qiksearch.com/

=cut

use Tk::Image;

my @files;
my $count = 0;
my $total = 0;
sub getDir
{
print "Enter the directory to read images from (rel/abs path): ";
$dirName = <STDIN>; chomp $dirName;
-d $dirName or warn "Directory does not exist!" and getDir();
}
sub retPrevFile
{
my $index = shift;
return "" unless $index;
return sprintf '&laquo; <a href="%s.htm">P revious</a>',
$files[$index - 1];
}
sub retNextFile
{
my $index = shift;
return "" if $index == $#files;
return sprintf '<a href="%s.htm">N ext</a> &raquo;', $files[$index
+ 1];
}

sub retPipe
{
my $index = shift;
return " | " if $index and $index < $#files;
return "";
}

sub getSlideName
{
print "Enter base name for album: ";
$slideName = <STDIN>; chomp $slideName;
-d "$dirName/$slideName" and warn "Directory $slideName exists!"
and getSlidename();
mkdir "$dirName/$slideName" or die;
}

my $format = q[
<html>
<head>
<title>%s</title>
<style type="text/css">
body {font-family:Trebuche t MS, Arial, Verdana;
font-size:10pt; font-weight:bold}
h4 {color:#CCCCCC}
a {font-family:Trebuche t MS, Arial, Verdana;
font-size:10pt; font-weight:bold;
text-decoration:none }
a:hover {font-family:Trebuche t MS, Arial, Verdana;
font-size:10pt; font-weight:bold;
text-decoration:unde rline}
img {border:#000000 solid 1px}
</style>
</head>
<body>
<center><h2>% s</h2></center>
<center><h4>% s (%s/%s)</h4></center>
<center><a href="../%s"><img src="../%s"></a></center>
<center>%s%s% s</center>
</body>
</html>
];
getDir();
getSlideName();

print "Enter a title for the album: ";
$title = <STDIN>; chomp $title;

print "Enter image scaling factor (e.g., 0.5, <enter> for default) ";
$scale = <STDIN> || '1.0'; chomp $scale;

chdir $dirName;
@files = <*.*>;
$total = scalar(@files);
chdir $slideName;

foreach $file (@files)
{
open FILE , ">$file.htm ";
printf FILE $format, $title, $title, $file, $count + 1, $total,
$file, $file,
retPrevFile($co unt), retPipe($count) , retNextFile($co unt);
close FILE;
$count++;

warn "File $file.htm created.\n"
}

print "\n", "Album created!\n";
Jul 18 '05 #5
>>>>> "Nick" == Nick Monyatovsky <mon-at-cox-net> writes:

Nick> 2. There are many things which are very easy to do in Perl, and
Nick> are very cumbersome in Python. Regular expressions is a
Nick> vivid example. Saying very generically that Python is easier
Nick> than Perl is just an invitation for flame wars.

Saying very generically that Python regexps are more cumbersome than
Perl one is just an invitation for flame wars. I have heard that
statement quite often, yet nobody ever bothers to substantiate it.

Hint - you can do

s = re.sub
m = re.match

if the function names are too long for you. And you don't need to
re.compile the regexps beforehand, just pass them as strings to the
functions. And backslash escaping behaviour can be averted by r"raw
strings".

The actual regexp syntax is the same as w/ Perl. I guess that's why
they are often called 'perl-style' regexps.

Nick> If you'd like to demonstrate that Python is easier than
Nick> Perl, you'll need to find a better case.

This is c.l.py - I don't think there are many who would claim the
opposite. Actually, I think Python and Perl are so far apart in the
ease of use/complexity/elegance, it's not even funny.

I'll give some comments on the script anyway.

Nick> my @files;
Nick> my $count = 0;
Nick> my $total = 0;

In Python there's no need for 'my' - every assignment makes the
variable local by default. This is a Good Thing, and I doubt you would
contest that.

Nick> sub retPrevFile
Nick> {
Nick> my $index = shift;

In Py, you just list the arguments in signature.

Nick> mkdir "$dirName/$slideName" or die;

This idiom is not necessary in Py. You just don't handle errors, the
program terminates w/ traceback automatically.

Nick> open FILE , ">$file.htm ";
Nick> printf FILE $format, $title, $title, $file, $count + 1, $total,

Here's a clear inconsistency (FILE doesn't have $, others do). Things
like this make a language hard to understand. In Py you just have
objects.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2687
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but not that different. I did like the indent-as-group idea, which was different. Ruby looked very cool. But it was impossible to get good...
2
1448
by: TSO | last post by:
Hi there, I've recently tried to translate some Perl code into Python - code is below. Is there a more Pythonic form?
68
5794
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
25
2408
by: rbt | last post by:
Are there any plans in the near future to support PDF files in Python as thoroughly and completely as Perl does? http://cpan.uwinnipeg.ca/search?query=pdf&mode=dist I love Python's clean syntax and ease of use, etc. But on some things (PDF for example) as barbaric as Perl's syntax is, it does outshine Python... I hate having to use Perl...
20
4030
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
1
1716
by: could.net | last post by:
I use boost 1.33_1, there's an example on boost::python named embedding.cpp. When I tried to build and run it in visual studio 2005, I got an error on this line: std::string hello() { return python::call_method<std::string>(self, "hello"); } It's a back ptr error. I don't know where to ask this question so I came to python group. Has
3
2784
by: seberino | last post by:
How similar is Python's re module (regular expressions) compared to Perl's and grep's regular expression syntaxes? I really hope regular expression syntax is sufficiently standardized that we don't have to learn new dialects everytime we move from one language or shell command to another. chris
0
1307
by: Oguz Yarimtepe | last post by:
Hi all, I need an example to adjust the backlight brightness via python-dbus. I will test it from console so no need for a gui. Will be happy at least for the name of the methods or some guidance. -- Oğuz Yarımtepe
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.