run a string as code?

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

    #1

    run a string as code?

    How can you make python interpret a string (of py code) as code. For
    example if you want a py program to modify itself as it runs. I know
    this is an advantage of interpreted languages, how is this done in
    python. Thanks.

  • mtbeedee@gmail.com

    #2
    Re: run a string as code?

    py_genetic wrote:
    How can you make python interpret a string (of py code) as code. For
    example if you want a py program to modify itself as it runs. I know
    this is an advantage of interpreted languages, how is this done in
    python. Thanks.
    This might do it...
    >>print eval.__doc__
    eval(source[, globals[, locals]]) -value

    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mappping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

    Comment

    • py_genetic

      #3
      Re: run a string as code?


      mtbeedee@gmail. com wrote:
      py_genetic wrote:
      How can you make python interpret a string (of py code) as code. For
      example if you want a py program to modify itself as it runs. I know
      this is an advantage of interpreted languages, how is this done in
      python. Thanks.
      >
      This might do it...
      >
      >print eval.__doc__
      eval(source[, globals[, locals]]) -value
      >
      Evaluate the source in the context of globals and locals.
      The source may be a string representing a Python expression
      or a code object as returned by compile().
      The globals must be a dictionary and locals can be any mappping,
      defaulting to the current globals and locals.
      If only globals is given, locals defaults to it.
      For example each time this line is interpreted I would like to use the
      new value of the state var which is a global var. How can I force
      state to be identified and used in this string.

      r_table = h5file.root.sta te_raw_records. neg_records

      r_table = eval("h5file.ro ot.state_raw_re cords.neg_recor ds") ??
      r_table = h5file.root.eva l("state")_raw_ records.neg_rec ords ?? eval is
      not a part of root

      dont think either of these is very logical? Any ideas? Possibly the
      parser mod?

      Comment

      • py_genetic

        #4
        Re: run a string as code?


        py_genetic wrote:
        mtbeedee@gmail. com wrote:
        py_genetic wrote:
        How can you make python interpret a string (of py code) as code. For
        example if you want a py program to modify itself as it runs. I know
        this is an advantage of interpreted languages, how is this done in
        python. Thanks.
        This might do it...
        >>print eval.__doc__
        eval(source[, globals[, locals]]) -value

        Evaluate the source in the context of globals and locals.
        The source may be a string representing a Python expression
        or a code object as returned by compile().
        The globals must be a dictionary and locals can be any mappping,
        defaulting to the current globals and locals.
        If only globals is given, locals defaults to it.
        >
        For example each time this line is interpreted I would like to use the
        new value of the state var which is a global var. How can I force
        state to be identified and used in this string.
        >
        r_table = h5file.root.sta te_raw_records. neg_records
        >
        r_table = eval("h5file.ro ot.state_raw_re cords.neg_recor ds") ??
        r_table = h5file.root.eva l("state")_raw_ records.neg_rec ords ?? eval is
        not a part of root
        >
        dont think either of these is very logical? Any ideas? Possibly the
        parser mod?
        Got it!

        tmp = "h5file.root."+ state+"_raw_rec ords.pos_record s"
        r_table = eval(tmp)

        works great thanks for the help!

        Comment

        • Gary Herron

          #5
          Re: run a string as code?

          py_genetic wrote:
          py_genetic wrote:
          >
          >mtbeedee@gmail. com wrote:
          >>
          >>py_genetic wrote:
          >>>
          >>>How can you make python interpret a string (of py code) as code. For
          >>>example if you want a py program to modify itself as it runs. I know
          >>>this is an advantage of interpreted languages, how is this done in
          >>>python. Thanks.
          >>>>
          >>This might do it...
          >>>
          >>>
          >>>>>print eval.__doc__
          >>>>>>
          >>eval(source[, globals[, locals]]) -value
          >>>
          >>Evaluate the source in the context of globals and locals.
          >>The source may be a string representing a Python expression
          >>or a code object as returned by compile().
          >>The globals must be a dictionary and locals can be any mappping,
          >>defaulting to the current globals and locals.
          >>If only globals is given, locals defaults to it.
          >>>
          >For example each time this line is interpreted I would like to use the
          >new value of the state var which is a global var. How can I force
          >state to be identified and used in this string.
          >>
          >r_table = h5file.root.sta te_raw_records. neg_records
          >>
          >r_table = eval("h5file.ro ot.state_raw_re cords.neg_recor ds") ??
          >r_table = h5file.root.eva l("state")_raw_ records.neg_rec ords ?? eval is
          >not a part of root
          >>
          >dont think either of these is very logical? Any ideas? Possibly the
          >parser mod?
          >>
          >
          Got it!
          >
          tmp = "h5file.root."+ state+"_raw_rec ords.pos_record s"
          r_table = eval(tmp)
          >
          works great thanks for the help!
          >
          Yes, it works, but this is not a good place to use eval. Now that we see
          how you want to use it, we can find a *much* better way to do it.

          If you want to lookup an attribute of an object, but the attribute name
          is a string in a variable, then use getattr to do the lookup.

          If in interpret your code correctly:

          attrname = state + "_raw_recor ds"
          obj = getattr(h5file. root, attrname)
          r_table = obj.pos_records

          These, of course, could be combined into a single (but not necessarily
          clearer) line.

          Gary Herron

          Comment

          • py_genetic

            #6
            Re: run a string as code?

            Gary Herron wrote:
            py_genetic wrote:
            py_genetic wrote:
            mtbeedee@gmail. com wrote:
            >
            >py_genetic wrote:
            >>
            >>How can you make python interpret a string (of py code) as code. For
            >>example if you want a py program to modify itself as it runs. I know
            >>this is an advantage of interpreted languages, how is this done in
            >>python. Thanks.
            >>>
            >This might do it...
            >>
            >>
            >>>>print eval.__doc__
            >>>>>
            >eval(source[, globals[, locals]]) -value
            >>
            >Evaluate the source in the context of globals and locals.
            >The source may be a string representing a Python expression
            >or a code object as returned by compile().
            >The globals must be a dictionary and locals can be any mappping,
            >defaulting to the current globals and locals.
            >If only globals is given, locals defaults to it.
            >>
            For example each time this line is interpreted I would like to use the
            new value of the state var which is a global var. How can I force
            state to be identified and used in this string.
            >
            r_table = h5file.root.sta te_raw_records. neg_records
            >
            r_table = eval("h5file.ro ot.state_raw_re cords.neg_recor ds") ??
            r_table = h5file.root.eva l("state")_raw_ records.neg_rec ords ?? eval is
            not a part of root
            >
            dont think either of these is very logical? Any ideas? Possibly the
            parser mod?
            >
            Got it!

            tmp = "h5file.root."+ state+"_raw_rec ords.pos_record s"
            r_table = eval(tmp)

            works great thanks for the help!
            Yes, it works, but this is not a good place to use eval. Now that we see
            how you want to use it, we can find a *much* better way to do it.
            >
            If you want to lookup an attribute of an object, but the attribute name
            is a string in a variable, then use getattr to do the lookup.
            >
            If in interpret your code correctly:
            >
            attrname = state + "_raw_recor ds"
            obj = getattr(h5file. root, attrname)
            r_table = obj.pos_records
            >
            These, of course, could be combined into a single (but not necessarily
            clearer) line.
            >
            Gary Herron
            So it is eval() is more appropriate when evalution blocks of string
            code, and getattr() is more efficient for dealing with objects such as
            h5file object above? Thanks.

            Comment

            Working...