The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Page 2 -
Module Questions
Page 2 - Discuss Module Questions in the Python Programming forum on Dev Shed. Module Questions Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 9th, 2004, 08:56 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Strike, quick and dirty nothing more
Grim
|

February 9th, 2004, 02:53 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I wouldn't take offense to that, Grim  . I'm quit new to Python and I greatly appreciate any help. Thanks for taking the time to restructure my bot, I think it's alot better now  .
|

February 9th, 2004, 04:07 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
Quote: Originally posted by Grim Archon
Strike, quick and dirty nothing more
Grim |
Good  But only as long as it stays as "nothing more". I'm not afraid to lay the verbal smack down on using it for anything else. 
|

February 9th, 2004, 04:33 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
One more quick question  : As you can see, I pass mapCommand a bunch of arguments. What I was wondering is if it's better to use 'self.' instead of passing arguments, in this context. I pass arguments to authManagement and chanManagement too, so they would use 'self.' too.
Make that two  : Grim, you mentioned a 'send' function to keep the methods from working with the underlying socket, but would you know a way to implement this considering I have quit a range of different calls to sock.send(). (i.e. MODE, KICK, BAN, NICK, and also a varying number of arguments.)
Last edited by XxChris : February 9th, 2004 at 04:56 PM.
|

February 9th, 2004, 05:06 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Thats a good one. 
In my view you got it right the first time:
1. You are encapsulating and keeping clean interfaces.
2. It's easier to document. (Try to describe the other way!)
3. In 6 months time you don't have remember how the self.xxx got there.
4. You're putting them in different modules, it will save file hopping.

|

February 9th, 2004, 06:07 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Really you only have one call:
self.send(astring) and it is astring that has different contents dependent on the protocol requirements.
So all I propose is you replace self.sock.send with self.send
where:
Code:
#in socketDriver
def send(self,data):
self.socket.send(data)
Not a big improvement in code, just a restructure that does isolation.
It is allmost inevitable that the closer the code gets to the real world the less ideal it's structure becomes - it has to deal with human decisions that making tidy code difficult!
However, in parseCommand you might consider adding a new function like:
Code:
def transmitCommand(self,command,arg1,arg2,arg3='',arg4=''):
if command == 'PRIMSG':
self.send('%s %s : %s\n\r' % (command,arg1l, arg))
elif command in['MODE', 'KICK']:
self.sock.send('%s %s %s %s\n\r' % (command,arg1, arg2,arg3))
elif command =='QUIT':
self.send('QUIT : %s\n\r'% (command,arg1))
etc...
Then all self.sock.send(...) would be replaced with self.transmitCommand('COMMAND',arg1,arg2,arg3,ar4)
But that is just extra code that puts off dealing with the protocol.
The choice is yours
Grim
|

February 10th, 2004, 02:17 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks alot Grim! ;D
|

February 10th, 2004, 05:53 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I'm back
The OOP structure is all sorted now and everything is split into modules. It all seems to work, but I've implemented a function to reload modules so I can dynamicaly make changes without disconnecting. I added a print statement right under the class statement in one module to test if everything was reloading properly and tt seemed to work, but when I put the print in a function or edited anything in functions in general it doesn't seem to have an effect if I reload that module. Any ideas?
Edit: This came to me shortly after I posted: Correct me if I'm wrong, but I am only changing the class when I do this, so all the functions in my instance remain the same and this is why I don't see any difference. If so, is there any way around this? 
Last edited by XxChris : February 10th, 2004 at 05:56 PM.
|

February 10th, 2004, 07:14 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
As far as i know the only way you're gonna get around with is to recreate all you're instances. Possibly do the inisalising inside a function to reduce code length.
Note: i dont clame to be infalable  . Infact i may be wrong on this one.
Quote: | I've implemented a function to reload modules so I can dynamicaly make changes without disconnecting |
I don't know if you know this but Python already has a reload() function built-in which you can use to... well, reload modules  .
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : February 10th, 2004 at 07:17 PM.
|

February 11th, 2004, 03:10 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Just a few thoughts and I have'nt used IRC but I guess the thing you need to preserve is the socket instance.
You could rewrite the __init__ function to optionally take a socket object if one exists already:
Code:
def __init__(self,sock=None):
if sock:
self.sock = sock
else:
self.sock = socket....
It might be possible to do further OO by builiding a reload method in the class, this could return a new class object based on the reloaded module. There may be issues with namespace - time to experiment
Code:
def reload(self):
reload("module")
temp = parseCommand(self.sock)
#now stuff to make temp look like self
return temp
So in the main module you could step through your Bots:
Code:
for x in range(len(mybots_list)):
mybots_list[x] = mybots_list[x].reload()
Grim
|

February 11th, 2004, 02:19 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks again guys.
Grim, I'll experiment a bit with this when I get the time, and i'll get back to you when I can. It looks promising 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|