Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41110

Re: Running external module and accessing the created objects

Newsgroups comp.lang.python
Date 2013-03-11 23:40 -0700
References <mailman.3110.1362798411.2939.python-list@python.org> <513aecd3$0$6512$c3e8da3$5496439d@news.astraweb.com> <mailman.3210.1363046277.2939.python-list@python.org>
Message-ID <e98e436e-7eda-42d2-b8e4-6461ab7c055f@googlegroups.com> (permalink)
Subject Re: Running external module and accessing the created objects
From Rick Johnson <rantingrickjohnson@gmail.com>

Show all headers | View raw


On Monday, March 11, 2013 6:57:28 PM UTC-5, Kene Meniru wrote:
>
> ------------------------------------------
> # contents of myapp.py
> import math
>
> class MyApp(object):
>     def __init__(self):
>         super(MyApp, self).__init__()
>         self.name = "MyAppName"
>
>
> def testFunction():
>     boke = "Smilling"
>     print math.sin(1), boke
> -----------------------------------------
> # contents of myappwin
> def test():
>     dic = {}
>     execfile("myapp.py", dic)
>     testObj = dic["MyApp"]() # access MyApp class
>     dic["testFunction"]()    # execute testFunction
>     print testObj.name       # print string
>
>
> test()
> -----------------------------------------
> # OUTPUT
> $ python myappwin.py
> 0.841470984808 Smilling
> MyAppName

Hmm. I don't understand why you think a simple old import won't work. Here is code (with names slightly adjusted for sanity).

============================================================
 Contents of "mymodule.py"
============================================================

import math

class Foo(object):
    def __init__(self):
        super(Foo, self).__init__()
        self.name = "MyAppName"

def foo():
    boke = "Smilling"
    print math.sin(1), boke

============================================================
 Contents of "myscript.py"
============================================================

# This next import statement requires that a script named
# "myapp.py" exist on the Python search path. If you cannot
# bring the script into the search path, you can bring the
# search path to the script by editing "sys.path".
#
# import sys
# sys.path.append('foderContainingMyModule')
# del sys
#
# But i would suggest placing the module on search path.

from mymodule import Foo, foo

def test():
##    dic = {}
##    execfile("myapp.py", dic)
##    testObj = dic["MyApp"]() # access MyApp class
    instance = Foo()
##    dic["testFunction"]()    # execute testFunction
    foo()
##    print testObj.name       # print string
    print instance.name

if __name__ == '__main__':
    test()

============================================================
 Results of running "myscript"
============================================================

0.841470984808 Smilling
MyAppName

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Running external module and accessing the created objects  Kene Meniru <Kene.Meniru@illom.org> - 2013-03-08 22:06 -0500
  Re: Running external module and accessing the created objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-09 08:03 +0000
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 06:05 -0500
      Re: Running external module and accessing the created objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-09 11:34 +0000
    Re: Running external module and accessing the created objects Chris Angelico <rosuav@gmail.com> - 2013-03-09 22:47 +1100
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 07:02 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 10:34 -0500
      Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-09 08:51 -0800
        Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 12:21 -0500
          Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-09 10:33 -0800
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 11:12 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 11:56 -0500
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 12:20 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 12:39 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 13:18 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 19:57 -0400
      Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-11 23:40 -0700
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 20:48 -0400
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 21:23 -0400
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 21:58 -0400
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 22:16 -0400
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 22:11 -0400
    Re: Running external module and accessing the created objects Michael Torrie <torriem@gmail.com> - 2013-03-11 22:05 -0600
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-12 07:20 -0400
    Re: Running external module and accessing the created objects Kene Meniru <kemeniru@gmail.com> - 2013-03-12 13:38 +0000
    Re: Running external module and accessing the created objects Kene Meniru <kemeniru@gmail.com> - 2013-03-12 13:28 +0000

csiph-web