Python noob SOS (any [former?] Perlheads out there?)

K

kj

For many months now I've been trying to learn Python, but I guess
I'm too old a dog trying to learn new tricks... For better or
worse, I'm so used to Perl when it comes to scripting, that I'm
just having a very hard time getting a hang of "The Python Way."

It's not the Python syntax that I'm having problems with, but rather
with larger scale issues such as the structuring of packages,
techniques for code reuse, test suites, the structure of
distributions,... Python and Perl seem to come from different
galaxies altogether...

Be that as it may, the activation barrier to using Python for my
scripting remains too high.

I'd written a Perl module to facilitate the writing of scripts.
It contained all my boilerplate code for parsing and validating
command-line options, generating of accessor functions for these
options, printing of the help message and of the full documentation,
testing, etc.

Of course, for Python now I don't have any of this, so I must write
it all from scratch, and the thing is *I don't even know where to
begin*! And meanwhile works needs to get done, projects finished,
etc. So naturally I revert to Perl, yadda-yadda-yadda, and the
Python project gets pushed back another week...

In a way it's the usual story with learning a new language, but
I've taught myself new languages before. (After all, there was a
time when I didn't know Perl.) It's harder this time, somehow...

Anyway, pardon the ramble.

Is there any good reading (to ease the transition) for Perl
programmers trying to learn Python?

Thanks!

kynn
 
B

Bruno Desthuilliers

kj a écrit :
For many months now I've been trying to learn Python, but I guess
I'm too old a dog trying to learn new tricks... For better or
worse, I'm so used to Perl when it comes to scripting, that I'm
just having a very hard time getting a hang of "The Python Way."
(snip)
>
I'd written a Perl module to facilitate the writing of scripts.
It contained all my boilerplate code for parsing and validating
command-line options, generating of accessor functions for these
options, printing of the help message and of the full documentation,
testing, etc.

Of course, for Python now I don't have any of this, so I must write
it all from scratch,

Hem... What about the optparse module ? (nb: it's in the standard lib).
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of kj
Sent: Tuesday, January 29, 2008 11:39 AM
To: (e-mail address removed)
Subject: Python noob SOS (any [former?] Perlheads out there?)



For many months now I've been trying to learn Python, but I guess
I'm too old a dog trying to learn new tricks... For better or
worse, I'm so used to Perl when it comes to scripting, that I'm
just having a very hard time getting a hang of "The Python Way."

It's not the Python syntax that I'm having problems with, but rather
with larger scale issues such as the structuring of packages,
techniques for code reuse, test suites, the structure of
distributions,... Python and Perl seem to come from different
galaxies altogether...


It sound like less of a "How to do Things the Python Way" problem, a
more of a "How to do Object Oriented Programming" problem.

Coming from a C++/Perl background, I found the O'Reilly 'Learning
Python' book to be useful. It has a section on OOP, which covers basic
OO theory that you may find useful. I can't think of a decent OO book
to recommend though.

Be that as it may, the activation barrier to using Python for my
scripting remains too high.

I'd written a Perl module to facilitate the writing of scripts.
It contained all my boilerplate code for parsing and validating
command-line options, generating of accessor functions for these
options, printing of the help message and of the full documentation,
testing, etc.

Bleh. Perl and Python have really good libraries. Why waste time
rolling your own when you can use Python's getopt or optparse, or Perl's
Getopt and Getopt::Long?
 
P

Paddy

It's not the Python syntax that I'm having problems with, but rather
with larger scale issues such as the structuring of packages,
techniques for code reuse, test suites, the structure of
distributions,... Python and Perl seem to come from different
galaxies altogether...

Maybe if someone could point Kynn at a suitable project where he could
look at the complete repository and see how its successfully done?
It would need I guess to employ:
* Setuptools
* modules/package(s)
* Doctest/unit tests
And not be too large I guess. Anyone wish to put forward their
project?

- Paddy.


- Paddy.
 
W

Wildemar Wildenburger

kj said:
Is there any good reading (to ease the transition) for Perl
programmers trying to learn Python?

www.diveintopython.org

While it is a bit dated by now (Python 2.2), that thing worked wonders
for me. Shows you Python in action and presents a fair amount of its
philosophy along the way.

Maybe(!) that helps a bit.

/W
 
K

kj

Bleh. Perl and Python have really good libraries. Why waste time
rolling your own when you can use Python's getopt or optparse, or Perl's
Getopt and Getopt::Long?

No, no "bleh". My module in fact uses Getopt::Long behind the
scenes, but customizes the heck out of it and adds a ton of
functionality I wanted not available in Getopt::Long.

kynn
 
G

grflanagan

It's not the Python syntax that I'm having problems with, but rather
with larger scale issues such as the structuring of packages,
techniques for code reuse, test suites, the structure of
distributions,... Python and Perl seem to come from different
galaxies altogether...
[...]

I'd written a Perl module to facilitate the writing of scripts.
It contained all my boilerplate code for parsing and validating
command-line options, generating of accessor functions for these
options, printing of the help message and of the full documentation,
testing, etc.

Of course, for Python now I don't have any of this, so I must write
it all from scratch, and the thing is *I don't even know where to
begin*! And meanwhile works needs to get done, projects finished,
etc. So naturally I revert to Perl, yadda-yadda-yadda, and the
Python project gets pushed back another week...

In a way it's the usual story with learning a new language, but
I've taught myself new languages before. (After all, there was a
time when I didn't know Perl.) It's harder this time, somehow...

The journey of a thousand miles etc.

For command line options I get a long way with this:

[code python]
def _getargs():
allargs = sys.argv[1:]
args = []
kwargs = {}
key = None
while allargs:
arg = allargs.pop(0)
if arg.startswith('--'):
key, arg = arg.split('=', 1)
key = key[2:]
elif arg.startswith('-'):
key = arg[1:]
if not allargs or allargs[0].startswith('-'):
allargs.insert(0, 'yes')
continue
if key is None:
args.append(arg)
else:
kwargs[key] = arg
key = None
return args, kwargs

ARGS, KWARGS = _getargs()
[/code]

though obviously there's no validation.

For testing see doctest module. For file and directory manipulation
see os and shutil modules.

The following is an abuse of docstrings, but shows some OO techniques:

[code python]
import inspect

linesep = '\n'

class TemplateBase(object):
factory = None
template = None
verb = 'substitute'

@classmethod
def render(cls, **kws):
if cls.template is None:
cls.template = cls.factory(inspect.getdoc(cls))
return getattr(cls.template, cls.verb)(**kws)

@classmethod
def write(cls, fd, **kws):
fd.write(linesep)
fd.write(cls.render(**kws))
fd.write(linesep)

@classmethod
def writeiter(cls, fd, seq):
for context in seq:
cls.write(fd, **context)

class StringTemplate(TemplateBase):
import string
factory = string.Template

class SafeStringTemplate(StringTemplate):
verb = 'safe_substitute'

try:

class TempitaTemplate(TemplateBase):
import tempita
factory = tempita.Template

except ImportError:
pass

try:

class MakoTemplate(TemplateBase):
from mako.template import Template
factory = Template
verb = 'render'

except ImportError:
pass

class StartJythonUnix(SafeStringTemplate):
r"""
#!/usr/bin/ksh

java \
-Dpython.home=$jythonhome \
-classpath $jythonhome/jython.jar:$CLASSPATH \
org.python.util.jython.class

"""

class DefineLocalQueue(StringTemplate):
"""
DEFINE QLOCAL($qname) +
DEFPSIST(YES) +
DESCR('$descr') +
STGCLASS('$stgclass') +
MAXDEPTH ($maxdepth)
"""

class DeleteLocalQueue(TempitaTemplate):
"""
DELETE QLOCAL({{qname}})
"""

import sys
StartJythonUnix.write(sys.stdout, jythonhome="/home/dev/jython/
jython-2.1")

from cStringIO import StringIO

s = StringIO()

DefineLocalQueue.write(s, name="AAA", descr="AAA", stgclass="AAA",
maxdepth=100)

DeleteLocalQueue.write(s, name="BBB", descr="BBB", stgclass="BBB",
maxdepth=100)

print s.getvalue()

[/code]

HTH

Gerard
 
A

A.T.Hofkamp

For command line options I get a long way with this:

[code python]
def _getargs():
allargs = sys.argv[1:]
args = []
kwargs = {}
key = None
while allargs:
arg = allargs.pop(0)
if arg.startswith('--'):
key, arg = arg.split('=', 1)
key = key[2:]
elif arg.startswith('-'):
key = arg[1:]
if not allargs or allargs[0].startswith('-'):
allargs.insert(0, 'yes')
continue
if key is None:
args.append(arg)
else:
kwargs[key] = arg
key = None
return args, kwargs

ARGS, KWARGS = _getargs()
[/code]

Have a look at getopt (old) or optparse (newer) packages in the Python library
instead.

Sincerely,
Albert
 
G

grflanagan

For command line options I get a long way with this:
[code python]
def _getargs():
allargs = sys.argv[1:]
args = []
kwargs = {}
key = None
while allargs:
arg = allargs.pop(0)
if arg.startswith('--'):
key, arg = arg.split('=', 1)
key = key[2:]
elif arg.startswith('-'):
key = arg[1:]
if not allargs or allargs[0].startswith('-'):
allargs.insert(0, 'yes')
continue
if key is None:
args.append(arg)
else:
kwargs[key] = arg
key = None
return args, kwargs
ARGS, KWARGS = _getargs()
[/code]

Have a look at getopt (old) or optparse (newer) packages in the Python library
instead.

Sincerely,
Albert

Admittedly I haven't used either of them, but getopt and optparse have
always looked like too much work to me. 90% of the time my '_getargs'
does what I need, and when it's insufficient I go straight to a config
file (ConfigParser or ConfigObj).

Best Regards

Gerard
 
S

samwyse

kj said:
I'd written a Perl module to facilitate the writing of scripts.
It contained all my boilerplate code for parsing and validating
command-line options, generating of accessor functions for these
options, printing of the help message and of the full documentation,
testing, etc.

A lot of people seem to have gotten hung up on this paragraph. kj
remarks in one reply that he *does* use optparse, but there still seems
to be much confusion. I've long done something similar, so I'll attempt
to clarify. For most of the programming languages that I use, my first
exercise is to write a program that accepts a 'getopt' string and
creates a program around it. Here's a simple version that I just
whipped out; the version that I commonly use is a bit more complicated.

"""%s [-h] [optionstring]

The program generates boilerplate for a Python script. It accepts a
single argument consisting of a string of option letters that you
wish the script to recognize, with options that require an argument
followed by a colon (i.e., the same format that Unix getopt() uses).

Written by samwyse
Created on Feb 02, 2008
"""

import sys
import getopt

class UserError(Exception):
def __init__(self, msg):
self.msg = msg

class Usage(UserError):
pass

def main(argv=None):
if argv is None:
argv = sys.argv
# parse command line options
try:
try:
opts, args = getopt.getopt(argv[1:], "h")
except getopt.error, msg:
raise Usage(msg)
# process options
for o, a in opts:
if o == '-h':
print __doc__ % argv[0]
return 0
except UserError, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
if type(err) is Usage:
return 2
else:
return 1

# process arguments
for arg in args:
# process() is defined elsewhere
process(arg)

#----#----#----#----#----#----#

def process(shortopts):
from time import strftime

prolog = '''\
"""Module docstring.

This serves as a long usage message.

Written by %(author)s
Created on %(date)s
"""

import sys
import getopt

class UserError(Exception):
def __init__(self, msg):
self.msg = msg

class Usage(UserError):
pass

def main(argv=None):
if argv is None:
argv = sys.argv
# parse command line options
try:
try:
opts, args = getopt.getopt(argv[1:], "%(opts)s")
except getopt.error, msg:
raise Usage(msg)
# process options
for o, a in opts:'''

has_arg = '''\
if o == '-%(opt)s':
opt_%(opt)s = a'''

no_arg = '''\
if o == '-%(opt)s':
opt_%(opt)s = True'''

epilog = '''\
except UserError, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
if type(err) is Usage:
return 2
else:
return 1

# process arguments
for arg in args:
# process() is defined elsewhere
process(arg)

if __name__ == "__main__":
sys.exit(main())
'''

values = {
'date': strftime('%b %d, %Y'),
'author': 'samwyse',
'opts': shortopts
}
print prolog % values
for i in range(len(shortopts)):
c = shortopts
if c != ':':
if shortopts.startswith(':', i+1):
print has_arg % { 'opt': c }
else:
print no_arg % { 'opt': c }
print epilog % values

#----#----#----#----#----#----#

if __name__ == "__main__":
sys.exit(main())
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top