Mailing List Archive

New python user seeks comments
--Multipart_Wed_Apr_21_00:33:10_1999-1
Content-Type: text/plain; charset=US-ASCII

Er, uh, hmm.

I wrote my first Python program today. It took longer than I
expected. Who would have guessed that you couldn't give a file object
the name 'in'? Or at least that was one of the weirder obstacles.
Anyway, I am not at all used to thinking in Python. I was wondering
if anyone would care to offer comments on my Python script. It is
quite small and is responsible for my sig. Feel free to scarf it.


--Multipart_Wed_Apr_21_00:33:10_1999-1
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: attachment; filename="siggen.python"
Content-Transfer-Encoding: 7bit

#! /usr/bin/env python

# Tue Apr 20 17:31

# This is my first python program. It is intended to generate signature
# files for mail and news using the fortune program.

# The first step is to open a template file that contains the constant
# portion of the signature. For flexability, I would like to get
# the file name from the command line. So really, the first step is
# to read the command line.

# imports section
import sys
import os
import commands
import time

# Test the number of arguments to see that it is the required number.
if len (sys.argv) != 3:
print 'usage: siggen.python template pipe'
sys.exit(0)

# The first argument is the input file, the second argument is the
# output file which should be a named pipe.
template = sys.argv[1]
npipe = sys.argv[2]

if not os.path.isfile(template):
print template + ' is not a file!'
sys.exit(0)

if not os.path.exists(npipe):
os.mkfifo(npipe)

# Time to get into it. Go into an infinite loop where we open the
# template file, copy it line by line to the pipe, then execute the
# fortune program and send it out the pipe
while 1:
fin = open(template, 'r', 1)
fout = open(npipe, 'w', 0)
fout.writelines(fin.readlines())
fin.close()
fout.writelines([commands.getoutput('/usr/bin/fortune -a'),'\n'])
fout.close()
time.sleep(1)

--Multipart_Wed_Apr_21_00:33:10_1999-1
Content-Type: text/plain; charset=US-ASCII


--
David Steuber
http://www.david-steuber.com

s/trashcan/david/ to reply by mail | while you're at it, you might also
If you don't, I won't see it. | want to track down and kill bulk
| mailers, aka spammers.

Vote for ME -- I'm well-tapered, half-cocked, ill-conceived and
TAX-DEFERRED!

--Multipart_Wed_Apr_21_00:33:10_1999-1--
New python user seeks comments [ In reply to ]
David Steuber wrote:
>
> Er, uh, hmm.
>
> I wrote my first Python program today. It took longer than I
> expected. Who would have guessed that you couldn't give a file object
> the name 'in'? Or at least that was one of the weirder obstacles.

It's not that weird as 'in' is a reserved keyword in Python. :)

for i in whatever:
print "See?"

Regards,

Martijn
New python user seeks comments [ In reply to ]
On 21 Apr 1999 00:33:10 -0400, David Steuber <trashcan@david-steuber.com> wrote:

>Er, uh, hmm.
>
>I wrote my first Python program today. It took longer than I
>expected. Who would have guessed that you couldn't give a file object
>the name 'in'? Or at least that was one of the weirder obstacles.

Maybe a reserved keyword-aware editor could have saved you
from this? You could try IDLE, which comes with the lastest Python
distribution or PythonWin, if you are using Windows.
There's a very clever Python Emacs mode as well...

Stefan
New python user seeks comments [ In reply to ]
Stefan Franke <spamfranke@bigfoot.de> wrote:
: On 21 Apr 1999 00:33:10 -0400, David Steuber <trashcan@david-steuber.com> wrote:

:>Er, uh, hmm.
:>
:>I wrote my first Python program today. It took longer than I
:>expected. Who would have guessed that you couldn't give a file object
:>the name 'in'? Or at least that was one of the weirder obstacles.

: Maybe a reserved keyword-aware editor could have saved you
: from this? You could try IDLE, which comes with the lastest Python
: distribution or PythonWin, if you are using Windows.
: There's a very clever Python Emacs mode as well...

And syntax highlighting for Python in vim (Vi IMproved).

-Arcege
New python user seeks comments [ In reply to ]
Martijn Faassen <faassen@pop.vet.uu.nl> writes:

> David Steuber wrote:
> >
> > Er, uh, hmm.
> >
> > I wrote my first Python program today. It took longer than I
> > expected. Who would have guessed that you couldn't give a file object
> > the name 'in'? Or at least that was one of the weirder obstacles.
>
> It's not that weird as 'in' is a reserved keyword in Python. :)
>
> for i in whatever:
> print "See?"
>

It seems that Python could have mentioned that to the user.

--
-chad
New python user seeks comments [ In reply to ]
On Wed, 21 Apr 1999, Stefan Franke wrote:

> On 21 Apr 1999 00:33:10 -0400, David Steuber <trashcan@david-steuber.com> wrote:
>
> >Er, uh, hmm.
> >
> >I wrote my first Python program today. It took longer than I
> >expected. Who would have guessed that you couldn't give a file object
> >the name 'in'?Or at least that was one of the weirder obstacles.
>
> Maybe a reserved keyword-aware editor could have saved you
> from this? You could try IDLE, which comes with the lastest Python
> distribution or PythonWin, if you are using Windows.
> There's a very clever Python Emacs mode as well...

Just my 0.02$: Recent versions of vim are also Python syntax aware.

And, though its name is misleading, the ``cheatsheet'' is wonderful:
lists of reserved words, etc.
--
Moshe Zadka <mzadka@geocities.com>.
QOTD: What fun to me! I'm not signing permanent.
New python user seeks comments [ In reply to ]
On 21 Apr 1999, Chad McDaniel wrote:

> Martijn Faassen <faassen@pop.vet.uu.nl> writes:
> > David Steuber wrote:
<snip>
> > > expected. Who would have guessed that you couldn't give a file object
> > > thename 'in'? Or at least that was one of the weirder obstacles.
> >
> > It's not that weird as 'in' is a reserved keyword in Python. :)
> It seems that Python could have mentioned that to the user.

This is a valid criticism:
>>> in=1
File "<stdin>", line 1
in=1
^
SyntaxError: invalid syntax

An error message like 'invalid usage of reserved word' would go a long
way to help the programmer. I'm not well aquainted enough with the parser
to know whether this is possible or not...

--
Moshe Zadka <mzadka@geocities.com>.
QOTD: What fun to me! I'm not signing permanent.
New python user seeks comments [ In reply to ]
Chad McDaniel wrote:

[.'in' is a reserved word so can't be used as a variable name]

> It seems that Python could have mentioned that to the user.

True, though it does say this:

File "<stdin>", line 2
in = open("test.txt", "r")
^
SyntaxError: invalid syntax

Could be better, but easily could've been far worse..
New python user seeks comments [ In reply to ]
Pythonistas--

Chad McDaniel wrote:
>
> Martijn Faassen <faassen@pop.vet.uu.nl> writes:
>
> > David Steuber wrote:
> > >
> > > Er, uh, hmm.
> > >
> > > I wrote my first Python program today. It took longer than I
> > > expected. Who would have guessed that you couldn't give a file object
> > > the name 'in'? Or at least that was one of the weirder obstacles.
> >
> > It's not that weird as 'in' is a reserved keyword in Python. :)
> >
> > for i in whatever:
> > print "See?"
> >
>
> It seems that Python could have mentioned that to the user.
>
> --
> -chad

http://www.python.org/doc/ref/keywords.html

The interpreter also states, in response to an attempt to assign a
number to a variable named `in', that such usage is a Syntax Error.

It could be changed to ``Fruitless attempt to assign to keyword Error,''
I suppose.

<for in.in in in : panic>-ly y'rs,
Ivan =:o
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
New python user seeks comments [ In reply to ]
Martijn Faassen <faassen@pop.vet.uu.nl> writes:

> Chad McDaniel wrote:
>
> [.'in' is a reserved word so can't be used as a variable name]
>
> > It seems that Python could have mentioned that to the user.
>
> True, though it does say this:
>
> File "<stdin>", line 2
> in = open("test.txt", "r")
> ^
> SyntaxError: invalid syntax
>
> Could be better, but easily could've been far worse..

I used Perl (and still do) before Python and I think that the Perl
interpreter gives exceptionally informative error messages. Of course,
with Perl's syntax, that may be a necessity ;->

--
-chad
New python user seeks comments [ In reply to ]
Martijn Faassen <faassen@pop.vet.uu.nl> writes:

-> Chad McDaniel wrote:
->
-> [.'in' is a reserved word so can't be used as a variable name]
->
-> > It seems that Python could have mentioned that to the user.
->
-> True, though it does say this:
->
-> File "<stdin>", line 2
-> in = open("test.txt", "r")
-> ^
-> SyntaxError: invalid syntax
->
-> Could be better, but easily could've been far worse..

This is what threw me. I got the syntax error. I had no clue what
was wrong with the syntax though. I have gotten so used to using in
for the input file and out for the output file in other languages.

--
David Steuber
http://www.david-steuber.com

If you wish to reply by mail, _please_ replace 'trashcan' with 'david'
in the e-mail address. The trashcan account really is a trashcan.

.. The Anarchists' [national] anthem is an international anthem that
consists of 365 raspberries blown in very quick succession to the tune
of "Camptown Races". Nobody has to stand up for it, nobody has to
listen to it, and, even better, nobody has to play it.
-- Mike Harding, "The Armchair Anarchist's Almanac"