Function metadata (like Java annotations) in Python

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

    Function metadata (like Java annotations) in Python

    Hi,

    I'm trying to attach some attributes to functions and methods, similar
    to Java annotations and .NET attributes.
    I also want to use a convenient decorator for it, something along the
    lines of

    @attr(name="xan der", age=10)
    def foo():
    ...

    Assigning attributes to the function will work, as will assigning keys
    and values to a dictionary in an attribute. But if there are more
    decorators in the way, this could fail:

    @onedec
    @attr(...)
    @twodec
    def foo():
    ...

    Given 'foo' now, how do I find the attributes?

    Assigning to a global attribute registry (some interpreter-global
    dictionary), although less desirable, might have been acceptable, but
    then how do I identify the function after it's been wrapped in more
    decorators?

    Also, it may be nice to have the following work as well:

    @attr(name="Xan der")
    @attr(age=10)
    @somedec
    @attr(hobby="kn itting")
    def foo():
    ...


    Any thoughts? Am I rehashing something old that my search skills didn't
    uncover?

    Thanks!
    Ori.

  • bearophileHUGS@lycos.com

    #2
    Re: Function metadata (like Java annotations) in Python

    oripel:

    Maybe this is a silly suggestion, the docstring is already overloaded,
    but it may be used for this too:

    def foo():
    """
    ...
    ...
    @ATTR name="Xander"
    @ATTR age=10
    @ATTR hobby="knitting "
    """
    ...

    (Or somethins similar without the @). Later you can retrive the
    attributes from the docstring, for example using a verbose RE like:
    r"\s* @ATTR \s+ (\w*) \s* = \s* (.*)"
    And you use it to create a dict of attributes.
    The decorators you apply to foo() must keep its docstring too.

    Bye,
    bearophile

    Comment

    • fumanchu

      #3
      Re: Function metadata (like Java annotations) in Python

      oripel wrote:
      I'm trying to attach some attributes to functions and methods, similar
      to Java annotations and .NET attributes.
      ...
      Assigning attributes to the function will work, as will assigning keys
      and values to a dictionary in an attribute. But if there are more
      decorators in the way, this could fail:
      >
      @onedec
      @attr(...)
      @twodec
      def foo():
      ...
      >
      Given 'foo' now, how do I find the attributes?
      ...
      Any thoughts? Am I rehashing something old that my search skills didn't
      uncover?
      There are past discussions about this; google for "python-dev decorator
      metadata". For example:



      Robert Brewer
      System Architect
      Amor Ministries
      fumanchu@amor.o rg

      Comment

      • Paddy

        #4
        Re: Function metadata (like Java annotations) in Python

        oripel wrote:
        Hi,
        >
        I'm trying to attach some attributes to functions and methods, similar
        to Java annotations and .NET attributes.
        I also want to use a convenient decorator for it, something along the
        lines of
        >
        @attr(name="xan der", age=10)
        def foo():
        ...
        >
        Assigning attributes to the function will work, as will assigning keys
        and values to a dictionary in an attribute. But if there are more
        decorators in the way, this could fail:
        >
        @onedec
        @attr(...)
        @twodec
        def foo():
        ...
        >
        Given 'foo' now, how do I find the attributes?
        >
        Assigning to a global attribute registry (some interpreter-global
        dictionary), although less desirable, might have been acceptable, but
        then how do I identify the function after it's been wrapped in more
        decorators?
        >
        Also, it may be nice to have the following work as well:
        >
        @attr(name="Xan der")
        @attr(age=10)
        @somedec
        @attr(hobby="kn itting")
        def foo():
        ...
        >
        >
        Any thoughts? Am I rehashing something old that my search skills didn't
        uncover?
        >
        Thanks!
        Ori.
        I wrote up my investigation into function attributes and decorators on
        my blog:

        Prompted by a presentation were someone said Python doesn't have full closures (noodle.odp), I looked at their example given and thought fir...

        It appears to be straight forward to initialize function attributes by a decorator (the setup function). The new function factory looks a lo...


        What do you think?

        - Paddy.

        Comment

        • oripel

          #5
          Re: Function metadata (like Java annotations) in Python

          Thanks bearophile,

          I prefer not to use docstrings for metadata.

          1. Not interfering with the other accepted docstring uses may be
          difficult (doctests, epydoc)

          2. It's impractical for attributes generated by code:

          @attr(reference _profile_stats= pstats.Stats("f oo.profile"))
          def foo():
          ...

          Regards,
          Ori.

          bearophileHUGS@ lycos.com wrote:
          oripel:
          >
          Maybe this is a silly suggestion, the docstring is already overloaded,
          but it may be used for this too:
          >
          def foo():
          """
          ...
          ...
          @ATTR name="Xander"
          @ATTR age=10
          @ATTR hobby="knitting "
          """
          ...
          >
          (Or somethins similar without the @). Later you can retrive the
          attributes from the docstring, for example using a verbose RE like:
          r"\s* @ATTR \s+ (\w*) \s* = \s* (.*)"
          And you use it to create a dict of attributes.
          The decorators you apply to foo() must keep its docstring too.
          >
          Bye,
          bearophile

          Comment

          • oripel

            #6
            Re: Function metadata (like Java annotations) in Python

            Thanks!

            Now I see it's accepted to assume nice decorators that update __dict__.
            At least until __decorates__ or something similar is added...

            fumanchu wrote:
            oripel wrote:
            I'm trying to attach some attributes to functions and methods, similar
            to Java annotations and .NET attributes.
            ...
            Assigning attributes to the function will work, as will assigning keys
            and values to a dictionary in an attribute. But if there are more
            decorators in the way, this could fail:

            @onedec
            @attr(...)
            @twodec
            def foo():
            ...

            Given 'foo' now, how do I find the attributes?
            ...
            Any thoughts? Am I rehashing something old that my search skills didn't
            uncover?
            >
            There are past discussions about this; google for "python-dev decorator
            metadata". For example:

            >
            >
            Robert Brewer
            System Architect
            Amor Ministries
            fumanchu@amor.o rg

            Comment

            • oripel

              #7
              Re: Function metadata (like Java annotations) in Python

              Thanks Paddy - you're showing normal use of function attributes.
              They're still hidden when wrapped by an uncooperative decorator.

              Paddy wrote:
              oripel wrote:
              Hi,

              I'm trying to attach some attributes to functions and methods, similar
              to Java annotations and .NET attributes.
              I also want to use a convenient decorator for it, something along the
              lines of

              @attr(name="xan der", age=10)
              def foo():
              ...

              Assigning attributes to the function will work, as will assigning keys
              and values to a dictionary in an attribute. But if there are more
              decorators in the way, this could fail:

              @onedec
              @attr(...)
              @twodec
              def foo():
              ...

              Given 'foo' now, how do I find the attributes?

              Assigning to a global attribute registry (some interpreter-global
              dictionary), although less desirable, might have been acceptable, but
              then how do I identify the function after it's been wrapped in more
              decorators?

              Also, it may be nice to have the following work as well:

              @attr(name="Xan der")
              @attr(age=10)
              @somedec
              @attr(hobby="kn itting")
              def foo():
              ...


              Any thoughts? Am I rehashing something old that my search skills didn't
              uncover?

              Thanks!
              Ori.
              >
              I wrote up my investigation into function attributes and decorators on
              my blog:
              >
              Prompted by a presentation were someone said Python doesn't have full closures (noodle.odp), I looked at their example given and thought fir...

              It appears to be straight forward to initialize function attributes by a decorator (the setup function). The new function factory looks a lo...

              >
              What do you think?
              >
              - Paddy.

              Comment

              • George Sakkis

                #8
                Re: Function metadata (like Java annotations) in Python

                oripel wrote:
                Thanks Paddy - you're showing normal use of function attributes.
                They're still hidden when wrapped by an uncooperative decorator.
                The decorator module may be helpful in defining cooperative decorators:


                George

                Comment

                • oripel

                  #9
                  Re: Function metadata (like Java annotations) in Python

                  Thanks,
                  In Python 2.5 there are also functools.wraps and
                  functools.updat e_wrapper:


                  George Sakkis wrote:
                  oripel wrote:
                  Thanks Paddy - you're showing normal use of function attributes.
                  They're still hidden when wrapped by an uncooperative decorator.
                  >
                  The decorator module may be helpful in defining cooperative decorators:

                  >
                  George

                  Comment

                  Working...