[Tutor] Py 2.4.4: Inheriting from ftplib.FTP()

2016-06-16 Thread boB Stepp
I am extremely gradually trying to dip my big toe into the waters of
writing classes.  I have a little bit of extra time at work today, so
I thought I would start writing a class to replace a bunch of shell
FTP scripts I've used in various programs that would be flexible
enough to be reused wherever I need it.  I immediately ran into
problems, which I have (to the best of my knowledge) resolved.
However, I am still in the early stages of writing the methods for
this class, so I thought I would ask for any likely "gotchas" that
might come my way.

Python 2.4.4 on Solaris 10:

from ftplib import FTP

# Current values for Windows-based intranet FTP server:
server_ip = 'my_default_IP'
user = 'default_user'
passwd = 'default_pw'

class FTPFiles(FTP, object):
"""FTP files to Windows server location(s)."""

def __init__(self, host=server_ip, user=user, passwd=passwd):
"""Initialize FTPFiles object.  Normally the defaults will be used."""

super(FTPFiles, self).__init__(host, user, passwd)
self.host = host
self.user = user
self.passwd = passwd

def print_welcome_msg(self):
"""Print welcome message sent by FTP server."""

print self.getwelcome()


if __name__ == '__main__':
ftp = FTPFiles()
ftp.print_welcome_msg()
ftp.quit()

What I learned today:

1)  FTP from ftplib appears to be an old-style class.

2)  I can't just use "class FTPFiles(FTP)" or I will be using old-style classes.

3)  I need to use "class FTPFiles(FTP, object)" to use new-style
classes and "object" must come AFTER "FTP".

4)  I have to use "super(FTPFiles, self).__init__(host, user, passwd)"
or I cannot successfully inherit from the FTP() class.  Also, "self"
apparently must come AFTER "FTPFiles" in the super expression.

Questions:

1)  Are there any more common "gotchas" that might be coming my way in
trying to use a new-style class inheriting from an old-style class in
ancient Py 2.4.4?

2)  I don't have much knowledge about FTP processes.  This is being
used on a secure intranet environment.  And I am using the ftplib for
the first time.  In its docs it mentioned that the quit() method is
the "polite" way to close the connection, but that an exception may be
raised if the responds with an error to the "QUIT" command the method
sends.  If this happens, will the connection close?  Or should I do
something like:

try:
ftp.quit()
except:
ftp.close()

?  It seems here (If I should use the "try" block.) that a bare
"except" is appropriate as I would think I would want to close the
connection regardless of the type of error the "QUIT" command
generates.

3)  The point of the print_welcome_msg() method is to be sure that I
have actually successfully connected to the FTP server.  Is there a
better way to check for connection success?

4)  As this class stuff is mostly treading new ground for me, am I
doing anything that I should not be doing or should do in a more
preferred way?  Keep in mind that I am just starting on this code
today.

As always, many thanks in advance!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Py 2.4.4: Inheriting from ftplib.FTP()

2016-06-16 Thread Alan Gauld via Tutor
On 16/06/16 16:38, boB Stepp wrote:

> class FTPFiles(FTP, object):
> """FTP files to Windows server location(s)."""

OK, First comment. You describe this as an action(verb)
rather than a noun. Classes should represent nouns
(objects) not actions. FTP represents a protocol
connection with which you can do things (one of
which is put/get files) you class should be the same.
(Remember inheritance m,eans you are imp0lementing
an "is a" relationship. So FTPFiles is an FTP...or
should be.)

> def __init__(self, host=server_ip, user=user, passwd=passwd):
> """Initialize FTPFiles object.  Normally the defaults will be used."""
> 
> super(FTPFiles, self).__init__(host, user, passwd)
> self.host = host
> self.user = user
> self.passwd = passwd
> 
> def print_welcome_msg(self):
> """Print welcome message sent by FTP server."""
> print self.getwelcome()


> 1)  FTP from ftplib appears to be an old-style class.

Using Python 2.4 that's not too surprising, FTP is an
old module.

> 4)  I have to use "super(FTPFiles, self).__init__(host, user, passwd)"
> or I cannot successfully inherit from the FTP() class.  Also, "self"
> apparently must come AFTER "FTPFiles" in the super expression.

That's the v2 super(). v3 super is far supeerior.

> 4)  As this class stuff is mostly treading new ground for me, am I
> doing anything that I should not be doing or should do in a more
> preferred way?  Keep in mind that I am just starting on this code
> today.

I'll leave someone with more ftplib experience to answer the other
points but the question I'd ask is what are you planning to add to FTP?
Creating a new class by inheritance implies that you are going to be
extending (or specializing) its capabilities in some way. What are you
planning to extend/specialize? For example are you going to limit it to
manipulating files only? ie prevent listing directories say?

You need to know what you are changing to warrant using inheritance.
Inheritance is a powerful tool but it carries lots of potential for
problems too, especially if you plan on mixing your new class in with
the old one (or sharing it with others who will) - you need to be
careful to abide by the Liskov substitution principle (LSP).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor