473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '08 #1
7 1086
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
Jul 22 '08 #2
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
Jul 22 '08 #3
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
Jul 22 '08 #4
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>

Jul 22 '08 #5


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.

Jul 22 '08 #6
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.

Jul 23 '08 #7
In message
<f4************ *************** *******@p25g200 0hsf.googlegrou ps.com>, Frank
Millman wrote:
from Utils.client import *
Besides the objections that others have mentioned, I HATE seeing wildcard
imports.
Jul 23 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16963
by: Todd Perkins | last post by:
Hello all, surprisingly enough, this is my first newsgroup post, I usually rely on google. So I hope I have enough info contained. Thank you in advance for any help! Problem: I am getting this error when I try to pull up my edit page to display the current database information in the form, and then edit it on click:
10
4229
by: SpreadTooThin | last post by:
Hi I'm writing a python script that creates directories from user input. Sometimes the user inputs characters that aren't valid characters for a file or directory name. Here are the characters that I consider to be valid characters... valid = ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' if I have a string...
2
2314
by: jim-on-linux | last post by:
py help, The file below will run as a stand alone file. It works fine as it is. But, when I call it from another module it locks my computer, The off switch is the only salvation. This module when run as a stand alone, it will
0
5666
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second encryption command (line : encryptedData2 = RSAE...) i get a "Key not valid for use in specified state." exception error even though i provide a valid...
7
1792
by: tshad | last post by:
I have a page that is showing as: Page.IsValid = False on postback when my submit button is pressed but none of my Validators have fired so there is no error message. How do I find out what is making the Page invalid? Thanks,
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.