Critic my module

Jason Swails jason.swails at gmail.com
Sat Jul 27 11:14:57 EDT 2013


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 <
devyncjohnson at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130727/3c29ffb8/attachment.html>


More information about the Python-list mailing list