Creating Python class wrapper for a command line tool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edvard Majakari

    Creating Python class wrapper for a command line tool


    Hi,

    I was wondering what would be the most elegant way for creating a Python class
    wrapper for a command line utility, which takes three types of arguments:

    1. options with values (--foo=bar)

    2. boolean options (--squibble)

    3. data lines (MUNGE:x:y:z:fr ob)

    So, when you call the program from command line it looks like

    command --foo=bar --hip=hurray --squibble --optimize \
    MUNGE1:x1:y:z:f rob1 \
    MUNGE2:x2:y:z:f rob2 \
    MUNGE3:x3:y:z:f rob3

    and it produces something. The idea is to make class with methods for
    setting the options and data, and then calling write() after all options
    have been set.

    My current model is like this:

    class Wrapper:

    def __init__(self, **kwargs):
    """Initiali ze object"""

    opts = []

    for key, val in kwargs.items():
    if isinstance(val, str) and val.find(' ') > -1:
    val = '"%s"' % val
    opts.append("--%s %s" % (key.replace('_ ', '-'), val))

    def setbool(self, opt):
    pass

    def data(self, data):
    pass

    def write(self):
    pass


    The init method might look a bit odd; the reason is that I thought to call
    Wrapper like this:

    obj = Wrapper(use_bar =foo, treshold=10, name="alley cat")

    and __init__() would transform those keyword parameters to long string
    of the form '--use_bar foo --threshold 10 --name "alley cat"'

    However, using **kwargs I cannot use boolean values, because a key in a
    dictionary must have a value. Using parameter boolean=True would transform
    to --boolean 1, which is not correct. That's why I added a separate method
    setbool(), but that doesn't seem nice. To wrap the command line call

    command --optimize --use_bar foo --threshold 10 --name "alley cat" \
    MUNGE1:x1:y:z:f rob1 \
    MUNGE2:x2:y:z:f rob2 \
    MUNGE3:x3:y:z:f rob3

    obj = Wrapper(use_bar =foo, treshold=10, name="alley cat")
    obj.setbool('op timize')
    obj.data("MUNGE 1:x1:y:z:frob1" )
    obj.data("MUNGE 2:x2:y:z:frob2" )
    obj.data("MUNGE 3:x3:y:z:frob3" )

    This is acceptable, but I'm sure many of you professional Pythonistas have
    a more elegant solution. What do you think?

    --
    # Edvard Majakari Software Engineer
    # PGP PUBLIC KEY available Soli Deo Gloria!

    $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";

  • Rico Huijbers

    #2
    Re: Creating Python class wrapper for a command line tool

    Edvard Majakari wrote:[color=blue]
    > Hi,
    >
    > I was wondering what would be the most elegant way for creating a Python class
    > wrapper for a command line utility, which takes three types of arguments:
    >
    > 1. options with values (--foo=bar)
    >
    > 2. boolean options (--squibble)
    >
    > 3. data lines (MUNGE:x:y:z:fr ob)
    >
    > So, when you call the program from command line it looks like
    >
    > command --foo=bar --hip=hurray --squibble --optimize \
    > MUNGE1:x1:y:z:f rob1 \
    > MUNGE2:x2:y:z:f rob2 \
    > MUNGE3:x3:y:z:f rob3
    >
    > and it produces something. The idea is to make class with methods for
    > setting the options and data, and then calling write() after all options
    > have been set.
    >
    > My current model is like this:
    >
    > class Wrapper:
    >
    > def __init__(self, **kwargs):
    > """Initiali ze object"""
    >
    > opts = []
    >
    > for key, val in kwargs.items():
    > if isinstance(val, str) and val.find(' ') > -1:
    > val = '"%s"' % val
    > opts.append("--%s %s" % (key.replace('_ ', '-'), val))
    >
    > def setbool(self, opt):
    > pass
    >
    > def data(self, data):
    > pass
    >
    > def write(self):
    > pass
    >
    >
    > The init method might look a bit odd; the reason is that I thought to call
    > Wrapper like this:
    >
    > obj = Wrapper(use_bar =foo, treshold=10, name="alley cat")
    >
    > and __init__() would transform those keyword parameters to long string
    > of the form '--use_bar foo --threshold 10 --name "alley cat"'
    >
    > However, using **kwargs I cannot use boolean values, because a key in a
    > dictionary must have a value. Using parameter boolean=True would transform
    > to --boolean 1, which is not correct. That's why I added a separate method
    > setbool(), but that doesn't seem nice. To wrap the command line call
    >
    > command --optimize --use_bar foo --threshold 10 --name "alley cat" \
    > MUNGE1:x1:y:z:f rob1 \
    > MUNGE2:x2:y:z:f rob2 \
    > MUNGE3:x3:y:z:f rob3
    >
    > obj = Wrapper(use_bar =foo, treshold=10, name="alley cat")
    > obj.setbool('op timize')
    > obj.data("MUNGE 1:x1:y:z:frob1" )
    > obj.data("MUNGE 2:x2:y:z:frob2" )
    > obj.data("MUNGE 3:x3:y:z:frob3" )
    >
    > This is acceptable, but I'm sure many of you professional Pythonistas have
    > a more elegant solution. What do you think?[/color]

    I am by no means a professional Pythonist... but it doesn't seem too bad
    to me to check for each kwarg whether it's type is boolean and if so,
    just add it to the command line as a flag instead of as a value
    parameter. And data arguments can be passed in the *args tuple.

    I'm thinking along the lines of:

    def __init__(self, *args, **kwargs):
    self.opts = []

    for key, val in kwargs:
    cmdkey = key.replace('_' , '-')

    if isinstance(val, bool):
    self.opts.appen d("--%s" % cmdkey)
    else:
    if isinstance(val, str) and val.find(' ') > -1:
    val = '"%s"' % val
    self.opts.appen d("--%s %s" % (cmdkey, val))


    Which would be called like:

    obj = Wrapper("MUNGE1 :x1:y:z:frob1", "MUNGE2:x2:y:z: frob2",
    "MUNGE3:x3:y:z: frob3", optimize=True, use_bar=foo, threshold=10,
    name="Alley cat")

    This just wraps everything in the constructor. Alternatively, you could
    go for setting each command-line parameter via a method flag.

    obj = Wrapper()
    obj.boolParam(' optimize')
    obj.valueParam( 'use_bar', 'foo')
    obj.valueParam( 'threshold', 10)
    obj.valueParam( 'name', 'alley cat')
    obj.dataParam(" MUNGE1:x1:y:z:f rob1")
    obj.dataParam(" MUNGE2:x2:y:z:f rob2")
    obj.dataParam(" MUNGE3:x3:y:z:f rob3")

    Combining the two methods seems ugly to me, though.

    - Rico

    Comment

    • Edvard Majakari

      #3
      Re: Creating Python class wrapper for a command line tool

      Rico Huijbers <E.A.M.Huijbers @REMOVEstudent. tue.nl> writes:
      [color=blue]
      > I am by no means a professional Pythonist... but it doesn't seem too bad
      > to me to check for each kwarg whether it's type is boolean and if so, just
      > add it to the command line as a flag instead of as a value parameter. And
      > data arguments can be passed in the *args tuple.[/color]

      Wow - I had to test it and didn't work. Then I remembered it would
      probably work with Python2.3, and it did. But the thingamajick must work
      with Python2.2.
      [color=blue]
      > obj = Wrapper("MUNGE1 :x1:y:z:frob1", "MUNGE2:x2:y:z: frob2",
      > "MUNGE3:x3:y:z: frob3", optimize=True, use_bar=foo, threshold=10,
      > name="Alley cat")
      >
      > This just wraps everything in the constructor. Alternatively, you could go
      > for setting each command-line parameter via a method flag.[/color]

      Yes - well, command line options are not necessary a must, so I thought to
      separate those from data (which *is* compulsory for the thing to work).
      [color=blue]
      > Combining the two methods seems ugly to me, though.[/color]

      I admit, it is not as pretty. But the command I'm using takes lots and
      lots of parameters, flags etc. and one reason in having a Python interface
      is to avoid very long parameter lists (if constructor is not given any
      keyword arguments, some sane defaults are assumed for them).

      But thanks for the follow-up!

      --
      #!/usr/bin/perl -w
      $h={23,69,28,'6 e',2,64,3,76,7, 20,13,61,8,'4d' ,24,73,10,'6a', 12,'6b',21,68,1 4,
      72,16,'2c',17,2 0,9,61,11,61,25 ,74,4,61,1,45,2 9,20,5,72,18,61 ,15,69,20,43,26 ,
      69,19,20,6,64,2 7,61,22,72};$_= join'',map{chr hex $h->{$_}}sort{$a<= >$b}
      keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(jo in('',60,28,14, 49),join'',
      map{lc}($1,subs tr $2,4,1)),2,4)." \n"; print;

      Comment

      • Peter Otten

        #4
        Re: Creating Python class wrapper for a command line tool

        Edvard Majakari wrote:
        [color=blue]
        > Rico Huijbers <E.A.M.Huijbers @REMOVEstudent. tue.nl> writes:
        >[color=green]
        >> I am by no means a professional Pythonist... but it doesn't seem too bad
        >> to me to check for each kwarg whether it's type is boolean and if so,
        >> just add it to the command line as a flag instead of as a value
        >> parameter. And data arguments can be passed in the *args tuple.[/color]
        >
        > Wow - I had to test it and didn't work. Then I remembered it would
        > probably work with Python2.3, and it did. But the thingamajick must work
        > with Python2.2.[/color]

        You could test object identity instead.
        [color=blue][color=green][color=darkred]
        >>> def isbool(b):[/color][/color][/color]
        .... return b is True or b is False
        ....[color=blue][color=green][color=darkred]
        >>> isbool(1), isbool(True)[/color][/color][/color]
        (0, 1)[color=blue][color=green][color=darkred]
        >>> isbool(0), isbool(False)[/color][/color][/color]
        (0, 1)

        Peter

        Comment

        • Edvard Majakari

          #5
          Re: Creating Python class wrapper for a command line tool

          Peter Otten <__peter__@web. de> writes:
          [color=blue]
          > You could test object identity instead.
          >[color=green][color=darkred]
          >>>> def isbool(b):[/color][/color]
          > ... return b is True or b is False[/color]

          Ah. Should have tried (I almost thought of that myself, but then I
          remembered 1 == True and thought it wouldn't work. But I forgot there's
          magic in Python ;)

          This works great with Python2.2. Thanks!

          --
          # Edvard Majakari Software Engineer
          # PGP PUBLIC KEY available Soli Deo Gloria!

          $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
          join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";

          Comment

          Working...