[Tutor] Problem with file()

Bernard Lebel python at bernardlebel.com
Sat Aug 21 16:52:48 CEST 2004


> You don't seem to have included the code containing the problem. The
> traceback shows the error occurring at line 104 of __init__.py and you
seem
> to have included ClientsManager.py.

The code is from __init__.py. The module is called ClientsManager. Right now
there is only a __init__.py file, containing all classes and functions.


>
> Have you perhaps redefined 'file' in __init__.py? It's easy to do...
>

You are absolutely right. I have a class named file. Stupid me. When renamed
to cmFile, it works now.


> BTW from what you have shown here, using a class to hold the getextlist()
> function may be overkill. Classes are great when you have several
functions
> that have to work together, for example sharing common data in fields. If
> you have independent functions that you want to group together for
> convenience/modularity/testing/reuse, you can make them global functions
in
> a separate module without creating a class.

Well you might be right. The classes are there mostly to encapsulate few
functions instead of having them all under the same level. But the module
has versatile functions: one is to work on a "client list", wich is the
cmClient class, while the other classes use the list. For instance, the
cmFile class is to copy, move and delete files over a large number of
computers, and the cmServices class is to handle remote processes of this
list.

Like I said right now everything sits in the __init__.py file. I will
definitely give a try to your separte module setup suggestion.


Thanks
Bernard



>
> Kent
>
> At 12:58 PM 8/21/2004 +0100, Bernard Lebel wrote:
> >Hello,
> >
> >I'm writing a little module that has few classes, and each classes few
> >functions. The first class has a function that allows to read a text file
> >and add the entries of the text to a list, and the list is returned.
> >
> >When I do the operation with command line in PythonWin, without the class
> >and function definitions, no problem. When I import the module and run th
e
> >function, I get an error:
> >
> >oExtList = file( sListPath, 'r' )
> >
> >Traceback (most recent call last):
> >   File "<interactive input>", line 1, in ?
> >   File "C:\Python23\lib\ClientsManager\__init__.py", line 104, in
getextlist
> >     oExtList = file( sListPath, 'r' )
> >TypeError: this constructor takes no arguments
> >
> >I call the function this way:
> >import ClientsManager as cm
> >cl = cm.client()
> >aClients = cl.getextlist()
> >
> >
> >
> >Duh?
> >
> >
> >Here is the full code:
> >
> >
> >
> >class client:
> >
> >  def getextlist( self, aClients = [], sListPath = '', iOp = 2 ):
> >
> >   """
> >   Will modify an existing client list using an external file list.
> >   You can choose to add to the list or remove from the list the clients
> >   listed in the external list.
> >
> >   For instances, user computers on a floor generally give a non-standard
> >   name to their computer, so you might find convenient to add/remove
their
> >names
> >   to the aClients list by querying an external file with the list of
> >computer names.
> >
> >   iOp specifies the operation to perform:
> >   0: append
> >   1: remove
> >   """
> >
> >   if sListPath == '':
> >    sListPath = raw_input( 'Full path to external list (use "\\\\" to
> >separate directories): ' )
> >
> >   if iOp < 0 or iOp > 1:
> >    iOp = int( raw_input( 'Type of operation (0-append, 1-remove): ' ) )
> >
> >   if iOp < 0 or iOp > 1:
> >    print ( 'You must enter 0 or 1!' )
> >   else:
> >    if not os.path.exists( sListPath ):
> >     print 'Provided list path invalid'
> >    else:
> >     # Get list file
> >     oExtList = file( sListPath )
> >
> >     # Get list elements
> >     aExtList = oExtList.readlines()
> >
> >     # Remove end of lines characters
> >     for sClient1 in aExtList:
> >      sClient2 = sClient1.strip( '\n' )
> >
> >      if iOp == 0:
> >       aClients.append( sClient2 )
> >      else:
> >       aClients.remove( sClient2 )
> >
> >     return aClients
> >
> >
> >
> >Thanks
> >Bernard
> >
> >_______________________________________________
> >Tutor maillist  -  Tutor at python.org
> >http://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



More information about the Tutor mailing list