Re: Critic my module

Discussion in 'Python' started by Jason Swails, Jul 27, 2013.

  1. Jason Swails

    Jason Swails Guest

    You've gotten plenty of good advice from people discussing the coding and
    coding style itself, I'll provide some feedback from the vantage point of a
    perspective user.


    On Thu, Jul 25, 2013 at 9:24 AM, Devyn Collier Johnson <
    > wrote:

    > Aloha Python Users!
    >
    > I made a Python3 module that allows users to use certain Linux shell
    > commands from Python3 more easily than using os.system(),
    > subprocess.Popen(), or subprocess.getoutput(). This module (once placed
    > with the other modules) can be used like this
    >
    > import boash; boash.ls()
    >


    I actually wrote a program recently in which I wanted access to unix "ls"
    command, and I wanted it to behave as close to the real, UNIX "ls" as
    possible.

    This would seem like a perfect use-case for your module, but the problem is
    that the 'ls' command in your module does not behave much like the real
    'ls' command. You never let any of the 'system' commands in your module
    access any arguments. More often than not, I use "ls" with several
    command-line arguments, like:

    ls --color=auto -lthr dir_basename*/

    Even if you're just spawning 'ls' directly, this is actually non-trivial to
    implement. You need globbing on all non-option arguments, you may want to
    pass up the return code somehow, depending on what the user wants to do:

    [bash ]$ ls nodir
    ls: nodir: No such file or directory
    [bash ]$ echo $?
    1

    Also, 'ls' in the terminal behaves like "ls -C" when called from your
    module. In the framework of my program, my 'ls' command looks like this:

    class ls(Action):
    """
    Lists directory contents. Like UNIX 'ls'
    """
    needs_parm = False
    def init(self, arg_list):
    from glob import glob
    self.args = []
    # Process the argument list to mimic the real ls as much as possible
    while True:
    try:
    arg = arg_list.get_next_string()
    if not arg.startswith('-'):
    # Glob this argument
    globarg = glob(arg)
    if len(globarg) > 0:
    self.args.extend(globarg)
    else:
    self.args.append(arg)
    else:
    self.args.append(arg)
    except NoArgument:
    break

    def __str__(self):
    from subprocess import Popen, PIPE
    process = Popen(['/bin/ls', '-C'] + self.args, stdout=PIPE,
    stderr=PIPE)
    out, err = process.communicate('')
    process.wait()
    return out + err

    [I have omitted the Action base class, which processes the user
    command-line arguments and passes it to the init() method in arg_list --
    this listing was just to give you a basic idea of the complexity of getting
    a true-er 'ls' command].

    Your 'uname' command is likewise limited (and the printout looks strange:

    >>> print(platform.uname())

    ('Linux', 'Batman', '3.3.8-gentoo', '#1 SMP Fri Oct 5 14:14:57 EDT 2012',
    'x86_64', 'AMD FX(tm)-6100 Six-Core Processor')

    Whereas:

    [bash $] uname -a
    Linux Batman 3.3.8-gentoo #1 SMP Fri Oct 5 14:14:57 EDT 2012 x86_64 AMD
    FX(tm)-6100 Six-Core Processor AuthenticAMD GNU/Linux

    You may want to change that to:

    def uname():
    print(' '.join(platform.uname()))

    Although again, oftentimes people want only something specific from uname
    (like -m or -n).

    HTH,
    Jason
     
    Jason Swails, Jul 27, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Devyn Collier Johnson

    Critic my module

    Devyn Collier Johnson, Jul 25, 2013, in forum: Python
    Replies:
    13
    Views:
    130
    Dave Angel
    Jul 27, 2013
  2. Joshua Landau

    Re: Critic my module

    Joshua Landau, Jul 25, 2013, in forum: Python
    Replies:
    0
    Views:
    90
    Joshua Landau
    Jul 25, 2013
  3. Schneider

    Re: Critic my module

    Schneider, Jul 25, 2013, in forum: Python
    Replies:
    0
    Views:
    87
    Schneider
    Jul 25, 2013
  4. Devyn Collier Johnson

    Re: Critic my module

    Devyn Collier Johnson, Jul 27, 2013, in forum: Python
    Replies:
    0
    Views:
    89
    Devyn Collier Johnson
    Jul 27, 2013
  5. Jason Friedman

    Re: Critic my module

    Jason Friedman, Jul 28, 2013, in forum: Python
    Replies:
    0
    Views:
    98
    Jason Friedman
    Jul 28, 2013
Loading...

Share This Page