Results 1 to 5 of 5

Thread: Python script symplification

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Python script symplification

    Hi all, I want a script o list all the user-names on my system.
    I saw a script that has that feature on line, and tore it down, But I'm stuck on how to simplify it any more.
    Heres where Im at:
    Code:
    #!/usr/bin/python
    import pwd
    import operator
    
    # Load all of the user data, sorted by username
    all_user_data = pwd.getpwall()
    userz = sorted((u 
        for u in all_user_data 
        if not u.pw_name.startswith('_')),
        key=operator.attrgetter('pw_name'))
    
    # Print the data
    for u in userz:
        print (u.pw_name)

    and here is where this script came from:
    http://www.doughellmann.com/PyMOTW/pwd/


    Any insight would be much appreciated.
    ~Conradin~

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: Python script symplification

    in what way do you want to simplify it more?
    its almost at the minimal line number, you could still move the pwd.getpwall() into the generator and replace the attrgetter with a lambda to save the operator import.
    if you really only want the user name you can use sorted((u.pw_name for u in pwd.getall()))
    but thats more compactification than simplification, simplification should also make the code easier to read and understand.
    Last edited by MadCow108; February 13th, 2012 at 11:34 PM.

  3. #3
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python script symplification

    Thanks mad cow, that was actually helpful. I thought about it, and said to hack with the sorting. so now Im looking at 4 lines.
    Code:
    #!/usr/bin/python
    import pwd
    # Load all of the user data, sorted by username
    for u in pwd.getpwall():
    	print (u.pw_name)
    ~Conradin~

  4. #4
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python script symplification

    Because my boss will kill me if I put zero error checking in my scripts, this is my simplified improved version.
    Code:
    #!/usr/bin/python
    import pwd
    try:
    	# Load all of the user data, sorted by username
    	for u in pwd.getpwall():
    		print (u.pw_name)
    
    except:
    	print("Something went terribly wrong.")
    ~Conradin~

  5. #5
    Join Date
    Aug 2007
    Location
    UK
    Beans
    427
    Distro
    Ubuntu UNR

    Re: Python script symplification

    $ awk -F: '{print $1}' /etc/passwd

    Quick and dirty.
    Last edited by StephenF; February 16th, 2012 at 03:19 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •