[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: adding multiple users, newuser? and vim



On Sun, Aug 26, 2001 at 02:15:38PM -0700, Osamu Aoki wrote:
...
> Took me a while to get this working.  Not bad excersize reading "Lerning
> Python".  Does any one have suggestions of better python coding practice?

for real advice you should try the python mailing list. But for starters:
don't use tabs for indentation, use 4 explicit space. Besides I lost the
original postings, so I can't check whether this does what was asked for.

> #! /usr/bin/env python
> import sys, string
> 
> # (C) Osmu Aoki Sun Aug 26 16:53:55 UTC 2001 Public Domain
> # Ported from awk script by KMSelf Sat Aug 25 20:47:38 PDT 2001
> # This program is distributed WITHOUT ANY WARRANTY.
> 
> def usages():
> 		print \

Python has triple quoted multi line strings and fprint functionality,
so this could become:

        print """Usage:  %s start_UID [filename]
	startUID is the starting userid to add.
	filename is input file name. If not specified, standard input.

Input file format:
	tfirstname lastname password""" % sys.argv[0]

> 		return 1

there is no need for this return statement

> def parsefile(startuid):
> 	#
> 	# main filtering
> 	#
> 	uid = startuid
> 	while 1:
> 		line = infile.readline()
> 		if not line:
> 			break
> 		(first, last, passwd) = string.split(string.lower(line))

no need to enclose those in (), remember the comma is the tuple maker.

> 		# above crash with wrong # of parameters :-)
> 		user = first[0] + last
> 		gid = uid
> 		lineout = "%s:%s:%d:%d:%s %s,,/home/%s:/bin/bash\n" %  \
> 			(user, passwd, uid, gid, first, last, user)
> 		sys.stdout.write(lineout)
> 		++uid

I doubt this does what you think it does, better try:

                uid = uid + 1
 
> if __name__ == '__main__':
> 	if len(sys.argv) == 1:
> 		usages()
> 	else:
> 		uid = int(sys.argv[1])
> 		#print "# UID start from: %d\n" % uid
> 		if len(sys.argv) > 1:
> 			infilename   = string.join(sys.argv[2:])

I don't get what join was ment to do here

> 			infile = open(infilename, 'r')
> 			#print "# Read file from: %s\n\n" % infilename
> 		else:
> 			infile = sys.stdin
> 		parsefile(uid)


-- 
groetjes, carel



Reply to: