How to pass parameter to a module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M-a-S

    How to pass parameter to a module?

    I'd like to parametrize a module. That is, to set and pass
    some values into the module, while it is being imported.
    I tried this:

    # sub_1.py -- main program
    extern = "OK"
    import sub_2
    print sub_2.noway # prints 'no extern' :-(
    # EOF

    # sub_2.py -- parametrized module, parameter is the 'extern' var
    try:
    noway = extern
    except:
    noway = 'no extern'
    # EOF

    You can guess, it doesn't work.
    How can I pass the parameter? Can I?
    M-a-S


  • Daniel Dittmar

    #2
    Re: How to pass parameter to a module?

    M-a-S wrote:[color=blue]
    > I'd like to parametrize a module. That is, to set and pass
    > some values into the module, while it is being imported.
    > I tried this:
    >
    > # sub_1.py -- main program
    > extern = "OK"
    > import sub_2
    > print sub_2.noway # prints 'no extern' :-(
    > # EOF
    >
    > # sub_2.py -- parametrized module, parameter is the 'extern' var
    > try:
    > noway = extern
    > except:
    > noway = 'no extern'
    > # EOF
    >
    > You can guess, it doesn't work.
    > How can I pass the parameter? Can I?[/color]

    Create an additional module sub_2_parameter s:
    # sub_1.py -- main program
    import sub_2_parameter s
    sub_2_parameter s.extern = "OK"
    import sub_2

    # sub_2.py -- parametrized module, parameter is the 'extern' var
    import sub_2_parameter s
    try:
    noway = sub_2_parameter s.extern
    except:
    noway = 'no extern'
    # EOF

    Note that modules are imported only once so you cannot import sub_2 with one
    set of parameters into one module and with another set of variables into
    another.

    Depending on your problem, it might be better to use the builtin execfile,
    because you can pass a dictionary with the parameters to the call.

    Daniel



    Comment

    • Ulrich Petri

      #3
      Re: How to pass parameter to a module?

      "M-a-S" <NO-MAIL@hotmail.co m> schrieb im Newsbeitrag
      news:Vwfab.2454 $9G2.2174@twist er.southeast.rr .com...[color=blue]
      > I'd like to parametrize a module. That is, to set and pass
      > some values into the module, while it is being imported.
      > I tried this:
      >
      > # sub_1.py -- main program
      > extern = "OK"
      > import sub_2
      > print sub_2.noway # prints 'no extern' :-(
      > # EOF
      >
      > # sub_2.py -- parametrized module, parameter is the 'extern' var
      > try:
      > noway = extern
      > except:
      > noway = 'no extern'
      > # EOF
      >[/color]

      It looks like you are using modules where you would rather use functions.
      exp:
      -------------------cut---------
      def sub2(blah=None) :
      if blah is None:
      return "no extern"
      else:
      return blah

      sub2('test')
      sub2()

      HTH

      Ciao Ulrich


      Comment

      • M-a-S

        #4
        Re: How to pass parameter to a module?

        I thought about the third module. It doesn't sound good.
        I hoped there're some tricks with __dict__, frames and
        other __...__ objects.

        For now, this is what does the task:

        # sub_2.py
        ''' The documentation will look like this:
        To set the size of dictionary and search depth,
        do before including (default values are shown):
        import string
        string.sub_2_di ctionary_size = 10 # in megabytes
        string.sub_2_se arch_depth = 1000 # in nodes
        string.sub_2_ma x_responce_time = 100 # seconds
        '''
        import string # it's used in sub_2 anyway
        # parameters
        try: _DICT_SZ = string.sub_2_di ctionary_size
        except: _DICT_SZ = 10
        try: _DEPTH = string.sub_2_se arch_depth
        except: _DEPTH = 1000
        try: _RTIME = string.sub_2_ma x_responce_time
        except: _RTIME = 100
        # now the stuff
        def do_the_job():
        return "Processing with dict=%d depth=%d rtime=%d" % (_DICT_SZ,_DEPT H,_RTIME)
        # EOF

        # sub_1.py
        import string # actually, some module, which is used in sub_2
        string.sub_2_di ctionary_size = 20 # megabytes
        string.sub_2_se arch_depth = 5000 # nodes
        # let's leave sub_2_max_respo nce_time default
        import sub_2
        print sub_2.do_the_jo b()
        # EOF

        M-a-S

        "Daniel Dittmar" <daniel.dittmar @sap.com> wrote in message news:bkc62g$5k0 $1@news1.wdf.sa p-ag.de...[color=blue]
        > M-a-S wrote:[color=green]
        > > I'd like to parametrize a module. That is, to set and pass[/color]
        >
        > Create an additional module sub_2_parameter s:
        > # sub_1.py -- main program
        > import sub_2_parameter s
        > sub_2_parameter s.extern = "OK"
        > import sub_2
        >
        > # sub_2.py -- parametrized module, parameter is the 'extern' var
        > import sub_2_parameter s
        > try:
        > noway = sub_2_parameter s.extern
        > except:
        > noway = 'no extern'
        > # EOF
        >
        > Note that modules are imported only once so you cannot import sub_2 with one
        > set of parameters into one module and with another set of variables into
        > another.
        >
        > Depending on your problem, it might be better to use the builtin execfile,
        > because you can pass a dictionary with the parameters to the call.
        >
        > Daniel[/color]


        Comment

        • Michael Peuser

          #5
          Re: How to pass parameter to a module?


          "M-a-S" <NO-MAIL@hotmail.co m>
          [color=blue]
          > I thought about the third module. It doesn't sound good.
          > I hoped there're some tricks with __dict__, frames and
          > other __...__ objects.
          >[/color]

          Well, there are some tricks ;-)


          myVariable='gre at surprise'
          import x
          ........

          "This is modul X"
          import sys
          print sys.modules['__main__'].myVariable


          But generally the namespace of a module is ... the module.

          Kindly
          Michael P


          Comment

          • M-a-S

            #6
            Re: How to pass parameter to a module?

            That's what I asked. Thanks!
            M-a-S

            "Michael Peuser" <mpeuser@web.de > wrote in message news:bkd0rb$qb2 $01$1@news.t-online.com...[color=blue]
            >
            > "M-a-S" <NO-MAIL@hotmail.co m>
            >[color=green]
            > > I thought about the third module. It doesn't sound good.
            > > I hoped there're some tricks with __dict__, frames and
            > > other __...__ objects.
            > >[/color]
            >
            > Well, there are some tricks ;-)
            >
            >
            > myVariable='gre at surprise'
            > import x
            > .......
            >
            > "This is modul X"
            > import sys
            > print sys.modules['__main__'].myVariable
            >
            >
            > But generally the namespace of a module is ... the module.
            >
            > Kindly
            > Michael P[/color]


            Comment

            • Hung Jung Lu

              #7
              Re: How to pass parameter to a module?

              "M-a-S" <NO-MAIL@hotmail.co m> wrote in message news:<Vwfab.245 4$9G2.2174@twis ter.southeast.r r.com>...[color=blue]
              > I'd like to parametrize a module. That is, to set and pass
              > some values into the module, while it is being imported.[/color]

              There are many ways.

              (However, classes may fit your need better. "Passing parameters to a
              module" is not a common practice, as far as I know.)

              (1) Use a built-in namespace variable.

              import __builtin__
              __builtin__.myv ar = 3
              print myvar

              This is a hack. I have been screamed at for mentioning it. :)

              (2) Hack your favorite (non-builtin) module. The module could be any
              of the standard library modules, or your own third module.

              import sys
              sys.myvar = 3

              (3) Use environmental variables.

              import os
              os.environ['myvar'] = 'hello'

              You get the idea. Python has three namespaces: built-in, global, and
              local. Since global and local namespaces won't go over to the other
              module, you need to rely on the built-in namespace, one way or
              another. How you want to structure your data (by using the built-in
              namespace, a module, a class, a dictionary or any other entities that
              can hold a name entry), it's entirely up to you.

              Hung Jung

              Comment

              • M-a-S

                #8
                Re: How to pass parameter to a module?

                Thank you!

                "Hung Jung Lu" <hungjunglu@yah oo.com> wrote in message news:8ef9bea6.0 309182328.5c040 595@posting.goo gle.com...[color=blue]
                > "M-a-S" <NO-MAIL@hotmail.co m> wrote in message news:<Vwfab.245 4$9G2.2174@twis ter.southeast.r r.com>...[color=green]
                > > I'd like to parametrize a module. That is, to set and pass
                > > some values into the module, while it is being imported.[/color]
                >
                > There are many ways.
                >
                > (However, classes may fit your need better. "Passing parameters to a
                > module" is not a common practice, as far as I know.)
                >
                > (1) Use a built-in namespace variable.
                >
                > import __builtin__
                > __builtin__.myv ar = 3
                > print myvar
                >
                > This is a hack. I have been screamed at for mentioning it. :)
                >
                > (2) Hack your favorite (non-builtin) module. The module could be any
                > of the standard library modules, or your own third module.
                >
                > import sys
                > sys.myvar = 3
                >
                > (3) Use environmental variables.
                >
                > import os
                > os.environ['myvar'] = 'hello'
                >
                > You get the idea. Python has three namespaces: built-in, global, and
                > local. Since global and local namespaces won't go over to the other
                > module, you need to rely on the built-in namespace, one way or
                > another. How you want to structure your data (by using the built-in
                > namespace, a module, a class, a dictionary or any other entities that
                > can hold a name entry), it's entirely up to you.
                >
                > Hung Jung[/color]


                Comment

                Working...