Is this a valid use of 'import'?

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

    Is this a valid use of 'import'?

    Hi all

    I am familiar enough with the normal use of 'import'. However, I have
    found a use for it which seems effective, but I have not seen it used
    like this before, so I am not sure if there are any downsides.

    I know that when a module is imported the first time, it is
    'executed'. This normally entails setting up constants, classes,
    functions, etc, that you want to make available to the importer.

    In this particular case, when it is executed, it does a whole lot
    more. It reads in some parameters, establishes a socket connection,
    starts a thread, and starts monitoring the socket using select.select.
    It also exposes some functions that disguise the complexity of reading
    from and writing to the socket.

    This enables me to write a 'client' program that look like this -

    ---------------------------
    from Utils.client import *

    connect(userid= 'frank',pwd='')
    cust = getRecord(
    company='chagfo rd',table='ArCu stomers',
    column='CustNo' ,value='A001')
    print cust
    close()
    ---------------------------

    As you can see, it makes writing a client program very easy.

    Are there any problems with this approach?

    Frank Millman
  • Tim Golden

    #2
    Re: Is this a valid use of 'import'?

    Frank Millman wrote:
    In this particular case, when it is executed, it does a whole lot
    more. It reads in some parameters, establishes a socket connection,
    starts a thread, and starts monitoring the socket using select.select.
    It also exposes some functions that disguise the complexity of reading
    from and writing to the socket.
    This is not, in general, a good idea, no matter how appealing!
    There is some subtle stuff going on within the chain of imports,
    and firing off a new thread is quite likely to confuse it in
    nasty ways later on. Less attractive though it may be, you're
    better off having a "startup" function or whatever and calling
    that:

    import Utils.client
    Utils.client.st artup ()

    TJG

    Comment

    • Matimus

      #3
      Re: Is this a valid use of 'import'?

      On Jul 22, 8:12 am, Frank Millman <fr...@chagford .comwrote:
      Hi all
      >
      I am familiar enough with the normal use of 'import'. However, I have
      found a use for it which seems effective, but I have not seen it used
      like this before, so I am not sure if there are any downsides.
      >
      I know that when a module is imported the first time, it is
      'executed'. This normally entails setting up constants, classes,
      functions, etc, that you want to make available to the importer.
      >
      In this particular case, when it is executed, it does a whole lot
      more. It reads in some parameters, establishes a socket connection,
      starts a thread, and starts monitoring the socket using select.select.
      It also exposes some functions that disguise the complexity of reading
      from and writing to the socket.
      >
      This enables me to write a 'client' program that look like this -
      >
      ---------------------------
      from Utils.client import *
      >
      connect(userid= 'frank',pwd='')
      cust = getRecord(
                  company='chagfo rd',table='ArCu stomers',
                  column='CustNo' ,value='A001')
      print cust
      close()
      ---------------------------
      >
      As you can see, it makes writing a client program very easy.
      >
      Are there any problems with this approach?
      >
      Frank Millman
      If it works for you that is great. That module however probably isn't
      very readable or easy to modify however. Also, you are hiding a lot of
      the complexity in a place where it isn't expected. Chances are it
      would be easy to do the same thing at the class level instead of the
      module level by putting all of that setup into the __init__ method of
      a class, which is the proper place to do that sort of thing. Making
      your code look something like this:

      from utils.client import Connection

      conn = Connection(user id='frank', pwd='')
      cust = conn.getRecord(
      company='chagfo rd',
      table='ArCustom ers',
      column='CustNo' ,
      value='A001'
      )
      print cust
      conn.close()


      Without seeing your other code it is difficult to know what the issues
      might be. In general, this is what classes are for.

      Matt

      Comment

      • Frank Millman

        #4
        Re: Is this a valid use of 'import'?

        On Jul 22, 5:22 pm, Tim Golden <m...@timgolden .me.ukwrote:
        Frank Millman wrote:
        In this particular case, when it is executed, it does a whole lot
        more. It reads in some parameters, establishes a socket connection,
        starts a thread, and starts monitoring the socket using select.select.
        It also exposes some functions that disguise the complexity of reading
        from and writing to the socket.
        >
        This is not, in general, a good idea, no matter how appealing!
        There is some subtle stuff going on within the chain of imports,
        and firing off a new thread is quite likely to confuse it in
        nasty ways later on. Less attractive though it may be, you're
        better off having a "startup" function or whatever and calling
        that:
        >
        import Utils.client
        Utils.client.st artup ()
        >
        TJG
        Makes sense.

        Thanks, Tim.

        Frank

        Comment

        • Fredrik Lundh

          #5
          Re: Is this a valid use of 'import'?

          Frank Millman wrote:
          I know that when a module is imported the first time, it is
          'executed'. This normally entails setting up constants, classes,
          functions, etc, that you want to make available to the importer.
          >
          In this particular case, when it is executed, it does a whole lot
          more. It reads in some parameters, establishes a socket connection,
          starts a thread, and starts monitoring the socket using select.select.
          It also exposes some functions that disguise the complexity of reading
          from and writing to the socket.
          >
          This enables me to write a 'client' program that look like this -
          >
          ---------------------------
          from Utils.client import *
          >
          connect(userid= 'frank',pwd='')
          cust = getRecord(
          company='chagfo rd',table='ArCu stomers',
          column='CustNo' ,value='A001')
          print cust
          close()
          ---------------------------
          >
          As you can see, it makes writing a client program very easy.
          >
          Are there any problems with this approach?
          besides being fragile and not scalable and not thread-safe and
          incompatible with introspection tools and utterly surprising for people
          used to normal Python behaviour, and only marginally easier to write
          than, say:

          from Utils.client import Client

          c = Client()
          c.connect(useri d='frank',pwd=' ')
          cust = c.getRecord(
          company='chagfo rd',table='ArCu stomers',
          column='CustNo' ,value='A001')
          print cust
          c.close()

          and some other problems that I cannot think of right now, you mean?

          </F>

          Comment

          • Terry Reedy

            #6
            Re: Is this a valid use of 'import'?



            Frank Millman wrote:
            I know that when a module is imported the first time, it is
            'executed'. This normally entails setting up constants, classes,
            functions, etc, that you want to make available to the importer.
            And every time thereafter, the module code is not executed*. So module
            code should only be code you want executed exactly once, on first
            import, and never again.
            In this particular case, when it is executed, it does a whole lot
            more. It reads in some parameters, establishes a socket connection,
            starts a thread, and starts monitoring the socket using select.select.
            It also exposes some functions that disguise the complexity of reading
            from and writing to the socket.
            What if you want to open another connection, because you want a second,
            or because you closed the first (to help the server while the client
            does something else) or because the connection got dropped because the
            server timed it out or because of network congestion or whatever?

            tjr

            *Python2 has a built-in reload function, but in 3.0 it is removed
            because it almost never did what people wanted or expected and thus lead
            to surprising bugs. You can delete a module from sys.modules and
            re-import it, but the module object and those it created will not
            disappear as long as there are other references to them.

            Comment

            • Benjamin

              #7
              Re: Is this a valid use of 'import'?

              On Jul 22, 12:26 pm, Terry Reedy <tjre...@udel.e duwrote:
              >
              *Python2 has a built-in reload function, but in 3.0 it is removed
              because it almost never did what people wanted or expected and thus lead
              to surprising bugs.  You can delete a module from sys.modules and
              re-import it, but the module object and those it created will not
              disappear as long as there are other references to them.
              It's not gone. It's been moved to imp.

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: Is this a valid use of 'import'?

                In message
                <f403afa4-dd31-4c77-8946-db7f75525ee9@p2 5g2000hsf.googl egroups.com>, Frank
                Millman wrote:
                from Utils.client import *
                Besides the objections that others have mentioned, I HATE seeing wildcard
                imports.

                Comment

                Working...