Bind compiled code to name?

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

    Bind compiled code to name?

    If string `source_code` contains Python source code, how can I execute
    that code, and bind it to some name? I tried compiling with:

    code_object = compile(source_ code, 'errorfile', 'exec')

    but then what to do with `code_object`?

    P.S.
    If my intentions aren't that clear, this is what I'm trying to do. I want
    to emulate this:

    import some_module as some_name

    but with my code in `some_module` string, and not in file.


    Thanks...

    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
    | || _ | _ | Parce mihi domine quia Dalmata sum.
    |__|_|__||_____ |_____|
  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: Bind compiled code to name?

    If string `source_code` contains Python source code, how can I execute
    that code, and bind it to some name? I tried compiling with:
    >
    code_object = compile(source_ code, 'errorfile', 'exec')
    >
    but then what to do with `code_object`?
    >
    P.S.
    If my intentions aren't that clear, this is what I'm trying to do. I want
    to emulate this:
    >
    import some_module as some_name
    >
    but with my code in `some_module` string, and not in file.
    >
    d = {}
    exec source_code in d
    some_name = d['some_name']

    HTH,
    Martin

    Comment

    • Karlo Lozovina

      #3
      Re: Bind compiled code to name?

      "Martin v. Löwis" <martin@v.loewi s.dewrote in
      news:485e9aea$0 $637$9b622d9e@n ews.freenet.de:
      d = {}
      exec source_code in d
      some_name = d['some_name']
      This works quite well! I can't believe after googling for half on hour I
      didn't notice this "exec ... in ..." syntax.
      One more thing though, is there a way to access "some_name" as a
      attribute, instead as a dictionary:

      some_name = d.some_name

      ?

      Thanks...

      --
      _______ Karlo Lozovina - Mosor
      | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
      | || _ | _ | Parce mihi domine quia Dalmata sum.
      |__|_|__||_____ |_____|

      Comment

      • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

        #4
        Re: Bind compiled code to name?

        >d = {}
        >exec source_code in d
        >some_name = d['some_name']
        >
        This works quite well! I can't believe after googling for half on hour I
        didn't notice this "exec ... in ..." syntax.
        One more thing though, is there a way to access "some_name" as a
        attribute, instead as a dictionary:
        >
        some_name = d.some_name
        Sure:

        class D:pass
        d = D()
        exec source_code in d.__dict__
        print d.some_name

        Notice that this will also give you d.__builtins__, which you might
        want to del afterwards.

        Regards,
        Martin

        Comment

        • Giuseppe Ottaviano

          #5
          Re: Bind compiled code to name?

          class D:pass
          d = D()
          exec source_code in d.__dict__
          print d.some_name
          >
          Notice that this will also give you d.__builtins__, which you might
          want to del afterwards.

          If you want to mimic an import you can also do this:

          import types
          D = types.ModuleTyp e('D')
          exec source_code in D.__dict__
          print D.some_name

          This way D is a module (don't know if there are real differences with
          the class approach, though)


          Comment

          • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

            #6
            Re: Bind compiled code to name?

            >d = {}
            >exec source_code in d
            >some_name = d['some_name']
            >
            This works quite well! I can't believe after googling for half on hour I
            didn't notice this "exec ... in ..." syntax.
            One more thing though, is there a way to access "some_name" as a
            attribute, instead as a dictionary:
            >
            some_name = d.some_name
            Sure:

            class D:pass
            d = D()
            exec source_code in d.__dict__
            print d.some_name

            Notice that this will also give you d.__builtins__, which you might
            want to del afterwards.

            Regards,
            Martin

            Comment

            • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

              #7
              Re: Bind compiled code to name?

              >d = {}
              >exec source_code in d
              >some_name = d['some_name']
              >
              This works quite well! I can't believe after googling for half on hour I
              didn't notice this "exec ... in ..." syntax.
              One more thing though, is there a way to access "some_name" as a
              attribute, instead as a dictionary:
              >
              some_name = d.some_name
              Sure:

              class D:pass
              d = D()
              exec source_code in d.__dict__
              print d.some_name

              Notice that this will also give you d.__builtins__, which you might
              want to del afterwards.

              Regards,
              Martin

              Comment

              Working...