Is this horrible python code?

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

    Is this horrible python code?

    Hi,

    I am working on a small project as well as trying to learn python. I parse a
    text file like

    def some_user_func( ):
    # <name> Some user function
    #<description> The description
    pass

    so that 'name', 'description' get put into self.info and the code gets
    compilied into a runnable function in self.func. This works but is it
    pythonic?
    Thanks alot for helping out! matthew.

    def print_message() :
    print "I am a message"

    def print_num(num=0 ):
    print 'Number: ', num

    class call_me(object) :
    def __init__(self, func, *args, **kw):
    self.func = func
    self.args = args
    self.kw = kw
    def __call__(self, *args, **kw):
    print "Execing... "
    return self.func(*self .args, **self.kw)

    class a_func(object):
    def __init__(self, func, info = {}): # name, description, authour, etc
    self.func = func
    self.info = info
    print 'finished a_func.__init__ ()'
    def __call__(self):
    print self.info
    self.func()
    print 'exiting a_func.__call__ ()'

    my_func = a_func(call_me( print_message), {'name': 'Print a message'})
    my_func()
    another = a_func(call_me( print_num, 42), {'name': 'Print a number'})
    another()


  • Matthew

    #2
    Re: Is this horrible python code?

    Sorry, I should add that the code fragment below is just trying to work out
    howto construct the python object to hold the runnable and its information.
    To test I fudged what would be the results of parsing in 'my_func' and
    'another' in __main__.
    ----- Original Message -----
    From: "Matthew" <matthew@newsgr oups.com>
    Newsgroups: comp.lang.pytho n
    Sent: Thursday, November 06, 2003 8:47 AM
    Subject: Is this horrible python code?



    "Matthew" <matthew@newsgr oups.com> wrote in message
    news:bobjp2$u73 $1@lust.ihug.co .nz...[color=blue]
    > Hi,
    >
    > I am working on a small project as well as trying to learn python. I parse[/color]
    a[color=blue]
    > text file like
    >
    > def some_user_func( ):
    > # <name> Some user function
    > #<description> The description
    > pass
    >
    > so that 'name', 'description' get put into self.info and the code gets
    > compilied into a runnable function in self.func. This works but is it
    > pythonic?
    > Thanks alot for helping out! matthew.
    >
    > def print_message() :
    > print "I am a message"
    >
    > def print_num(num=0 ):
    > print 'Number: ', num
    >
    > class call_me(object) :
    > def __init__(self, func, *args, **kw):
    > self.func = func
    > self.args = args
    > self.kw = kw
    > def __call__(self, *args, **kw):
    > print "Execing... "
    > return self.func(*self .args, **self.kw)
    >
    > class a_func(object):
    > def __init__(self, func, info = {}): # name, description, authour, etc
    > self.func = func
    > self.info = info
    > print 'finished a_func.__init__ ()'
    > def __call__(self):
    > print self.info
    > self.func()
    > print 'exiting a_func.__call__ ()'
    >
    > my_func = a_func(call_me( print_message), {'name': 'Print a message'})
    > my_func()
    > another = a_func(call_me( print_num, 42), {'name': 'Print a number'})
    > another()
    >
    >[/color]


    Comment

    • Christian Seberino

      #3
      Re: Is this horrible python code?

      I'd be happy to help except I cannot decipher what you are asking.

      Chris


      "Matthew" <matthew@newsgr oups.com> wrote in message news:<bobklj$um m$1@lust.ihug.c o.nz>...[color=blue]
      > Sorry, I should add that the code fragment below is just trying to work out
      > howto construct the python object to hold the runnable and its information.
      > To test I fudged what would be the results of parsing in 'my_func' and
      > 'another' in __main__.
      > ----- Original Message -----
      > From: "Matthew" <matthew@newsgr oups.com>
      > Newsgroups: comp.lang.pytho n
      > Sent: Thursday, November 06, 2003 8:47 AM
      > Subject: Is this horrible python code?
      >
      >
      >
      > "Matthew" <matthew@newsgr oups.com> wrote in message
      > news:bobjp2$u73 $1@lust.ihug.co .nz...[color=green]
      > > Hi,
      > >
      > > I am working on a small project as well as trying to learn python. I parse[/color]
      > a[color=green]
      > > text file like
      > >
      > > def some_user_func( ):
      > > # <name> Some user function
      > > #<description> The description
      > > pass
      > >
      > > so that 'name', 'description' get put into self.info and the code gets
      > > compilied into a runnable function in self.func. This works but is it
      > > pythonic?
      > > Thanks alot for helping out! matthew.
      > >
      > > def print_message() :
      > > print "I am a message"
      > >
      > > def print_num(num=0 ):
      > > print 'Number: ', num
      > >
      > > class call_me(object) :
      > > def __init__(self, func, *args, **kw):
      > > self.func = func
      > > self.args = args
      > > self.kw = kw
      > > def __call__(self, *args, **kw):
      > > print "Execing... "
      > > return self.func(*self .args, **self.kw)
      > >
      > > class a_func(object):
      > > def __init__(self, func, info = {}): # name, description, authour, etc
      > > self.func = func
      > > self.info = info
      > > print 'finished a_func.__init__ ()'
      > > def __call__(self):
      > > print self.info
      > > self.func()
      > > print 'exiting a_func.__call__ ()'
      > >
      > > my_func = a_func(call_me( print_message), {'name': 'Print a message'})
      > > my_func()
      > > another = a_func(call_me( print_num, 42), {'name': 'Print a number'})
      > > another()
      > >
      > >[/color][/color]

      Comment

      • Michael George Lerner

        #4
        Re: Is this horrible python code?

        Matthew <matthew@newsgr oups.com> wrote:[color=blue]
        > Hi,[/color]
        [color=blue]
        > class call_me(object) :
        > def __init__(self, func, *args, **kw):
        > self.func = func
        > self.args = args
        > self.kw = kw
        > def __call__(self, *args, **kw):
        > print "Execing... "
        > return self.func(*self .args, **self.kw)[/color]

        I'm not sure what you're trying to do with the rest of this, but
        I think that your call_me class isn't doing what you want.

        asteroids% python
        Python 2.2 (#1, Feb 18 2002, 14:48:51) [C] on sunos5
        Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
        >>> class A(object):[/color][/color][/color]
        .... def __init__(self,a ):
        .... self.a = a
        .... def __call__(self,a ):
        .... print "self.a is '%s' and a is '%s'" % (self.a,a)
        ....[color=blue][color=green][color=darkred]
        >>> a = A('the first thing')
        >>> a('the second thing')[/color][/color][/color]
        self.a is 'the first thing' and a is 'the second thing'[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        so, i don't think your __call__ method will do what you
        want. then again, i'm not exactly sure what you're trying to
        do, so i can't figure out why you want to initialize things
        your objects with func, args and kw *and* want to be able
        to specify args and kw at call time.

        -michael

        Comment

        • MetalOne

          #5
          Re: Is this horrible python code?

          I think it is simpler using closures.

          def auditFunc(info, func, *args, **kw):
          def inner():
          print 'finished a_func.__init__ ()'
          print info
          print "Execing... "
          ret = func(*args, **kw)
          print 'exiting a_func.__call__ ()'
          return ret

          return inner

          f = auditFunc({'nam e': 'Print a message'}, print_message)
          f()
          another = auditFunc({'nam e': 'Print a number'}, print_num, 42)
          another()

          Comment

          Working...