Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Clear interface for mail class

Reply
Thread Tools

Clear interface for mail class

 
 
Benedict Verheyen
Guest
Posts: n/a
 
      10-14-2009
Hi,


I'm trying to come up with a decent interface for my email class.
Basically, i have one email function but it has many (too many?) variables:

send_mail(self, send_from, send_to, send_cc, subject, text, separate_emails = False, files=[], inline_files=[], server="localhost",
charset="iso-8859-1")

I'm thinking on how i could simplify it and make it look nicer and more easy to use.
I can reduce the number of arguments by adding functions for the less common tasks
like adding attachement, inline files and so on.
Then i would end up with functions like this:

def __init__(self):
"""
"""
self._files=[]
self._inline_files=[]
....

def add_files(self, files=[]):
assert type(files)==list
self._files=files
....

When sending an email, i could check if files where specified and if so, send them too.

But it doesn't feel right to just have a bunch of small functions to set variables.
Calling the function would change from:
(where m is the mail class)
m.send_mail( "from_me@work",
"to_@work",
[],
"Test emailmodule ",
MSG,
separate_emails = True,
files=["attached_pic.png"],
inline_files=["inline_pic.png"],
server="mysmtpserver")

to:

m.add_files(["attached_pic.png"])
m.add_inline_files(["inline_pic.png"])
m.smtp_server("mysmtpserver")
m.send_mail( "from_me@work",
"to_@work",
[],
"Test emailmodule "",
MSG)

It looks already better and i could set the smtp server as a class variable,
but i'm not sure this will present me with the most natural interface to use.

Or should i make 1 general function that sets vars according to a type?
For instance: add_header("To",[list_of_recipients])

This kind of problems seems to happen sometimes: i need to fix something quickly,
build a script for it, and then i realise that the code i've written is not the best in
terms of reusability and has some "not so great" functions or classes.
Speedy development eh.

Any ideas are welcome.

Thanks,
Benedict

 
Reply With Quote
 
 
 
 
Marco Mariani
Guest
Posts: n/a
 
      10-14-2009
Benedict Verheyen wrote:

> Any ideas are welcome.


easy_install turbomail



 
Reply With Quote
 
 
 
 
Benedict Verheyen
Guest
Posts: n/a
 
      10-14-2009
Marco Mariani wrote:
> Benedict Verheyen wrote:
>
>> Any ideas are welcome.

>
> easy_install turbomail
>
>
>


Looks good but i'm still interested in how one would
make a clean class interface for this type of problem.

Having said that, turbomail looks quite good

Thanks

 
Reply With Quote
 
Francesco Bochicchio
Guest
Posts: n/a
 
      10-14-2009
On Oct 14, 2:39*pm, Benedict Verheyen <(E-Mail Removed)>
wrote:
> Hi,
>
> I'm trying to come up with a decent interface for my email class.
> Basically, i have one email function but it has many (too many?) variables:
>
> * * send_mail(self, send_from, send_to, send_cc, subject, text, separate_emails = False, files=[], inline_files=[], server="localhost",
> charset="iso-8859-1")
>
> I'm thinking on how i could simplify it and make it look nicer and more easy to use.
> I can reduce the number of arguments by adding functions for the less common tasks
> like adding attachement, inline files and so on.
> Then i would end up with functions like this:
>
> def __init__(self):
> * * """
> * * """
> * * self._files=[]
> * * self._inline_files=[]
> ...
>
> def add_files(self, files=[]):
> * * assert type(files)==list
> * * self._files=files
> ...
>
> When sending an email, i could check if files where specified and if so, send them too.
>
> But it doesn't feel right to just have a bunch of small functions to set variables.
> Calling the function would change from:
> *(where m is the mail class)
> *m.send_mail( "from_me@work",
> * * * * * * * "to_@work",
> * * * * * * * [],
> * * * * * * * "Test emailmodule ",
> * * * * * * * MSG,
> * * * * * * * separate_emails = True,
> * * * * * * * files=["attached_pic.png"],
> * * * * * * * inline_files=["inline_pic.png"],
> * * * * * * * server="mysmtpserver")
>
> to:
>
> m.add_files(["attached_pic.png"])
> m.add_inline_files(["inline_pic.png"])
> m.smtp_server("mysmtpserver")
> m.send_mail( "from_me@work",
> * * * * * * *"to_@work",
> * * * * * * *[],
> * * * * * * *"Test emailmodule "",
> * * * * * * *MSG)
>
> It looks already better and i could set the smtp server as a class variable,
> but i'm not sure this will present me with the most natural interface to use.
>
> Or should i make 1 general function that sets vars according to a type?
> For instance: add_header("To",[list_of_recipients])
>
> This kind of problems seems to happen sometimes: i need to fix something quickly,
> build a script for it, and then i realise that the code i've written is not the best in
> terms of reusability and has some "not so great" functions or classes.
> Speedy development eh.
>
> Any ideas are welcome.
>
> Thanks,
> Benedict


I would add a server class, maybe subclassing something in standard
library, and add to it the 'send' method, so that sending a mail would
be
something like:

myserver = MyMailServer("mysmtpserver", "localhost", ) # this only
needs to be done once, not for each mail

m = MyMail( subject, text, separate_emails = False, files=[],
inline_files=[] ) # mail creation

myserver.send( m, from= "from_me@work", # mail sending
to = "to_@work",
cc_to= None )

Note that I put sender and destination senders in the send method, not
as attributes of the mail object. It makes more sense to me, and yopu
can reuse
the same object if you want to send the same mail to many addresses
( and/or via different servers ).

IN general, un case like yours I use a lot default parameters, as you
did already. Having separate methods to setting specific part of an
object only makes
sens (to me) if you need first to create an object and later change
some of the attributes. Also, if you are just going to change the
attributes, you do not
need a method: just use the object.attribute = value syntax. If you
are going to need later to do more complex thing, you can always
transform your
attribute in a property.




 
Reply With Quote
 
Benedict Verheyen
Guest
Posts: n/a
 
      10-15-2009
Francesco Bochicchio wrote:
<snip>
>
> I would add a server class, maybe subclassing something in standard
> library, and add to it the 'send' method, so that sending a mail would
> be
> something like:
>
> myserver = MyMailServer("mysmtpserver", "localhost", ) # this only
> needs to be done once, not for each mail
>
> m = MyMail( subject, text, separate_emails = False, files=[],
> inline_files=[] ) # mail creation
>
> myserver.send( m, from= "from_me@work", # mail sending
> to = "to_@work",
> cc_to= None )
>
> Note that I put sender and destination senders in the send method, not
> as attributes of the mail object. It makes more sense to me, and yopu
> can reuse
> the same object if you want to send the same mail to many addresses
> ( and/or via different servers ).
>
> IN general, un case like yours I use a lot default parameters, as you
> did already. Having separate methods to setting specific part of an
> object only makes
> sens (to me) if you need first to create an object and later change
> some of the attributes. Also, if you are just going to change the
> attributes, you do not
> need a method: just use the object.attribute = value syntax. If you
> are going to need later to do more complex thing, you can always
> transform your
> attribute in a property.



Thanks for the input guys

Cheers,
Benedict

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
Unrecognized element 'add' after <clear></clear> InvalidLastName ASP .Net Web Services 3 03-06-2007 03:07 AM
test if obj implemenst interface (provided as interface.class) cyberco Java 8 02-25-2006 12:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
MIB to clear virtual-interface via SNMP? Yehavi Bourvine Cisco 1 02-13-2005 08:12 PM



Advertisments