Perl to Python conversion

126 views
Skip to first unread message

Martin Schöön

unread,
Dec 9, 2009, 3:33:56 PM12/9/09
to
First off: I am new here and this is my first post after
lurking for quite some time.

Second off: I don't know much Python---yet.

Problem: I have come across a small open source application
that I find quite useful. It does have one major flaw though.
Its output is in imperial units. Converting isn't a big deal
for occasional use but if I start to use this stuff on a
regular basis...

So I down-loaded the source code and found this thing is written
in Perl.

Should I learn enough Perl to add the conversion? Probably
but this may be a nice excuse to get my Python education
going and if I do I might as well re-do the user interface.

If I do re-write this thing in Python I might need to learn both
Perl and Python...

Hence, are there any Perl to Python converters? So far I
have only found bridgekeeper which really is (was?) consultancy.
Apart from that I only find people recommending a manual re-write.

Any thoughts/recommendations?

TIA,

/Martin

zeph

unread,
Dec 9, 2009, 4:05:33 PM12/9/09
to
Python and Perl often have different design idioms - learning to write
*well* in a language involves understanding those idioms, and being
able to translate between languages involves understanding the source
language well enough to understand the intent of the program's code
(even if its poorly written), and understanding the target language
well enough to translate the intent into a design fitting the
language.

Perl and Python have enough syntactic similarities that you could, if
you wanted, just translate the program verbatim.

Overall, I would recommend adding your imperial->metric functionality
to the Perl code for now, and on your free time work on translating to
Python, so you don't feel rushed to get it finished. You will
probably come to understand the Perl code better after having worked
with it and been involved in a hands-on way with it. Likewise, you
will become even more familiar with it and with Python as you learn
how to translate the application.

Intchanter / Daniel Fackrell

unread,
Dec 9, 2009, 4:10:34 PM12/9/09
to

Martin,

A full answer will depend a lot on several different factors,
including the length of the Perl code, what style it was written in
(there seem to be uncountably many possibilities), your experience
with languages in general, and with that style in particular.

In general, though, if your primary purpose is to learn Python and
ending up with a useful tool is secondary, I'd recommend rewriting the
tool from scratch, possibly keeping the Perl source handy. If the
existing tool is command-line based, you might also be able to write a
short script through which you can pipe the output of the original
program to handle the conversion.

Intchanter
Daniel Fackrell

Peter Chant

unread,
Dec 9, 2009, 5:46:15 PM12/9/09
to
Martin Sch��n wrote:

> Hence, are there any Perl to Python converters? So far I
> have only found bridgekeeper which really is (was?) consultancy.
> Apart from that I only find people recommending a manual re-write.
>
> Any thoughts/recommendations?

Voice of almost no experience. I once ran a fortran programme through a
fortran to c converter and when I saw the result I ran away screaming - it
did not look very human friendly.


--
http://www.petezilla.co.uk

J Kenneth King

unread,
Dec 10, 2009, 2:04:52 PM12/10/09
to
martin...@gmail.com (Martin Schöön) writes:

> First off: I am new here and this is my first post after
> lurking for quite some time.

Hi.

> Second off: I don't know much Python---yet.

It's not a very big language. If you have experience programming in
other languages, you can probably pick it up in a day or two.

> Problem: I have come across a small open source application
> that I find quite useful. It does have one major flaw though.
> Its output is in imperial units. Converting isn't a big deal
> for occasional use but if I start to use this stuff on a
> regular basis...
>
> So I down-loaded the source code and found this thing is written
> in Perl.
>
> Should I learn enough Perl to add the conversion? Probably
> but this may be a nice excuse to get my Python education
> going and if I do I might as well re-do the user interface.

Well you can always call it from Python via subprocess (which basically
wraps a shell and has fancy ways putting data in and extracting data
out).

> If I do re-write this thing in Python I might need to learn both
> Perl and Python...

You'll need to know one of them rather well and enough of the other to
get by. It's probably easier to know more Perl than Python since Perl
is a lot more expressive than Python (in the TMTOWTDI sense). Frankly I
learned Perl before Python and find it rather easy to go between the
two. YMMV.

> Hence, are there any Perl to Python converters? So far I
> have only found bridgekeeper which really is (was?) consultancy.
> Apart from that I only find people recommending a manual re-write.

It depends where the two languages vary from one another.

If the script your translating uses basic types or even simple classes
and typical control structures and operations then translating from one
to the other is a simple matter of copy-pasting the code and translating
the syntax and small bits of grammar.

However, in areas where there are high variations; you'll probably want
to stay away from it. Perl has a lot of freedom to manipulate
references and the programmer can modify the language to suit their
needs. So just be careful of code that uses these features as they are
difficult to translate into Python.

> Any thoughts/recommendations?

Depends:

- If you needed it done yesterday to get some work done, wrap the Perl
script in a subprocess and buy yourself some time to think it over.
- If your purpose is to learn Python, then start from scratch. Use the
Perl as a guide if there are any maths or algorithms you are unsure
about.
- If you're just hacking around to learn stuff, learn a little of both.
It will make you smarter if it doesn't confuse the heck out of you and
make you quit before you finish. ;)

>
> TIA,
>
> /Martin

HTH

Martin Schöön

unread,
Dec 10, 2009, 3:06:14 PM12/10/09
to
Thanks all, great response!

A little more background:

I am not a programmer but I have done some programming in the past.
This was all humble number crunching as part of my PhD project using
FORTRAN. I also did some Rocky Mountain Basic coding for programs
manipulating measurement instruments. And I did a minute amount
of Turbo Pascal code, too little and too many years ago to count.

Since then I have done some stuff in Matlab and (very basic) UNIX
scripts.

Does HTML, css and LaTeX count?

So why Python? Well, I thought it would be fun to learn a little
about GUI programming and a friend who is a real programmer recommended
Python + PyQt. I have bought some books and lurked here for about
a year but haven't managed to get going yet. I figured I needed
some kind of project for that and now I have two.

Learning Python and PyQt is spare time killer/brain teaser activity.

The thermal contact conductance stuff is something that come in
handy at work.

So here is what I plan to do based on your kind advice and some
thinking of my own.

1) I fix the unit thing by adding a conversion to the results
presentation routine.

2) Recreating the functionality of the program in Python and PyQt
will be a 'long term' educational project at home. There are
various bits and pieces there: data base handling, GUI design, math,
logic, error/exception handling...

We have long, dark winters where I live :-)

All the best,

/Martin

Colin W.

unread,
Dec 12, 2009, 12:22:34 PM12/12/09
to

Martin,

If you convert the Perl, you continue the other fellow's errors. If you
do it yourself, you'll be able to make your own - there should be fewer
of them.

Google: unit conversion python

you'll have lots of offers.

Colin W.

r0g

unread,
Dec 13, 2009, 4:08:50 PM12/13/09
to


I'd recommend you start from scratch and refer to the perl version if
and when you need to. You'll probably find the majority of code in a GUI
app is boring window handling stuff rather, often this is machine
generated (by progs like glade) rather than hand coded anyway so it will
probably provide little insight. Also, they probably didn't make it with
QT which is fairly different from GTK. It's like the housing industry,
demolition + newbuild is almost always cheaper than renovation. If you
can spare yourself the ordeal of learning perl and the chore of
interpreting somebody else's perl then grasp it with both hands!

Good luck. I'm sure you'll be fine - python is a deeply pleasing
language to work in :)

Roger.

Martin Schöön

unread,
Dec 13, 2009, 5:23:04 PM12/13/09
to
r0g <aioe...@technicalbloke.com> writes:
>
> I'd recommend you start from scratch and refer to the perl version if
> and when you need to.

That is my plan.

> You'll probably find the majority of code in a GUI
> app is boring window handling stuff rather, often this is machine
> generated (by progs like glade) rather than hand coded anyway so it will
> probably provide little insight. Also, they probably didn't make it with
> QT which is fairly different from GTK.

Tk is what they used.

/Martin

Aahz

unread,
Dec 25, 2009, 1:35:57 PM12/25/09
to
In article <87zl5rn...@crunchbang.Belkin>,

Martin =?utf-8?B?U2Now7bDtm4=?= <martin...@gmail.com> wrote:
>
>Problem: I have come across a small open source application that I find
>quite useful. It does have one major flaw though. Its output is in
>imperial units. Converting isn't a big deal for occasional use but if I
>start to use this stuff on a regular basis...

I'd write an imperial to metric converter in Python ;-)
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Looking back over the years, after I learned Python I realized that I
never really had enjoyed programming before.

John Yeung

unread,
Dec 25, 2009, 2:54:45 PM12/25/09
to
On Dec 13, 5:23 pm, martin.sch...@gmail.com (Martin Schöön) wrote:

> r0g <aioe....@technicalbloke.com> writes:
> > You'll probably find the majority of code in a GUI
> > app is boring window handling stuff [...]

> > Also, they probably didn't make it with
> > QT which is fairly different from GTK.
>
> Tk is what they used.

Well, on the surface then, it would seem that it might be easier to
just use Python's included Tkinter for the GUI bits. However, PyQt is
widely regarded as a nicer and more capable package.

John

Simon Brunning

unread,
Dec 28, 2009, 7:38:18 AM12/28/09
to Python List
2009/12/25 Aahz <aa...@pythoncraft.com>:

>
> I'd write an imperial to metric converter in Python  ;-)

Should be possible to use unum (<http://bit.ly/4X0PwR>) to do the
conversions. The SI units are already defined - adding in any
necessary imperial units should be easy enough.

--
Cheers,
Simon B.

Reply all
Reply to author
Forward
0 new messages