improve this newbie code/nested functions in Python?

pruebauno at latinmail.com pruebauno at latinmail.com
Fri Mar 20 14:02:08 EDT 2009


On Mar 19, 10:21 pm, Esmail <ebo... at gmail.com> wrote:
> Hi,
>
> I'm new to writing Python code. This is a simple client I wrote, it
> works, but I feel it doesn't look as clean as it could. Can anyone
> make suggestions how to streamline this code?
>
> Also, I am using two nested functions, it seems that nested functions
> aren't used that much in Python - is that correct? And if so, how
> come?
>
> thanks,
>
> Esmail
>
> ps: I realize there is minimal error checking/exception handling.
>
> #!/usr/bin/env python
>
> import sys
> from socket import *
> from threading import Thread
>
> class Client(object):
>     def __init__(self, host="localhost", port=5555, name = "esmail"):
>         self._host = host
>         self._port = port
>         self._name = name
>         self._address=(self._host, self._port)
>         self._sock=socket(AF_INET, SOCK_STREAM)
>         self._sock.connect(self._address)
>         self._parent = self
>         self._keepGoing = True
>
>     def info(self):
>         return self._host, self._port, self._name
>
>     class Listener(Thread):
>        def __init__(self, parent, tname="listener"):
>            Thread.__init__(self,name = tname)
>            self._parent = parent
>            print self._parent._host
>
>        def run(self):
>
>            while self._parent._keepGoing:
>                m = self._parent._sock.recvfrom(1024)
>                print m[0]
>
>     class Speaker(Thread):
>        def __init__(self, parent, tname = "speaker"):
>            Thread.__init__(self,name = tname)
>            self._parent = parent
>            self._parent._sock.send(self._parent._name + "\n")
>
>        def run(self):
>
>            while(True):
>                m = raw_input("-> ")
>                if m == "bye":
>                    self._parent._sock.send(self._parent._name + " is
> signing off.\n")
>                    self._parent._sock.send("bye\n")
>                    self._parent._keepGoing = False
>                    break;
>                else:
>                    self._parent._sock.send(m + "\n")
>
> def main():
>
>     if len(sys.argv) == 4:  # prog name + 3 args
>         host = sys.argv[1]
>         port = int(sys.argv[2])
>         name = sys.argv[3]
>         c = Client(host, port, name)
>     else:
>         c = Client()
>
>     print "Client connecting to - host=%s  port=%d   name=%s" % c.info
> ()
>
>     s = Client.Speaker(c)
>     s.start()
>
>     l = Client.Listener(c)
>     l.start()
>
> main()

How about renaming the Client to SocketConfig, Listener to
ClientListener and Speaker to ClientSpeaker and put all at the same
level. The you can do this:

 c = SocketConfig(host, port, name)
 s = ClientSpeaker(c)
 l = ClientListener(c)

the other option would be to create a Speaker and Listener factory in
Client that returns Speakers and Listeners so you can do:

c = Client(host, port, name)
s = c.Speaker()
l = c.Listener()




More information about the Python-list mailing list