Re: [Tutor] Simple Question (I Hope)
On 16/01/12 01:47, Chris Kavanagh wrote: Thanks for the help. . .I think I see what you're saying. And to make it short & simple, the MIMEText Class behaves the way it does, because that's just how it works (or was designed). So just accept it, & move on, lol. Not quite. What we are saying is that yhour assumption that only dictionaries exhibited mapping behaviour was wrong. Any class can be made to look like a dictionary by implementing the set/getitem() methods. So the lesson to take away is not just to accept these things but to look under the covers to see how such anomolies are possible. Python is a wonderfully flexible language because of its combination of duck typing and operator overloading. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question (I Hope)
On 1/14/2012 11:17 PM, Modulok wrote: On 1/14/12, Chris Kavanagh wrote: I was looking at this code from the Python Docs (http://docs.python.org/library/email-examples.html), trying to learn how to send email from a Pyhton script. Anyways, part of this code confused me. Here's the script: 1 # Import smtplib for the actual sending function 2 import smtplib 3 4 # Import the email modules we'll need 5 from email.mime.text import MIMEText 6 7 # Open a plain text file for reading. For this example, assume that 8 # the text file contains only ASCII characters. 9 fp = open(textfile, 'rb') 10 # Create a text/plain message 11 msg = MIMEText(fp.read()) 12 fp.close() 13 14 # me == the sender's email address 15 # you == the recipient's email address 16 msg['Subject'] = 'The contents of %s' % textfile 17 msg['From'] = me 18 msg['To'] = you 19 20 # Send the message via our own SMTP server, but don't include the 21 # envelope header. 22 s = smtplib.SMTP('localhost') 23 s.sendmail(me, [you], msg.as_string()) 24 s.quit() What I don't understand is lines 16-18, more specifically the msg['Subject'] format. I thought this was only done with dics?? Obviously the variable msg isn't a dic, so how can this be done?? I actually put lines 11, 16,17,18, in the interpreter, then printed out msg, so I get what it's doing, but my question still stands. How can one do this, when I thought it could only be done with dictionaries??? Chris, I haven't looked at the module, but you should be aware that you can have user-defined classes which behave like builtin types, with their own customised features. You can also subclass a dict and customise it to do whatever. That said, as long as an object provides dictionary access methods, it can be treated like a dict in every respect. As far as python is concerned, if it looks like a duck and quacks like a duck - it's a duck. (Or in this case a dict.) It doesn't matter what the 'type' is, what is important is how you can access it. Here's an example:: # example.py # The following exapmle could be done more cleanly by subclassing the builtin # dict type, but for illustrative purposes this was not done. Instead, we show # several dict methods being defined on our dict-like class 'Foo': class Foo(object): '''This object behaves like a builtin dict that refuses the value "red".''' def __init__(self, x, y): self.x = x #<-- We can have our own properties too. self.y = y self.data = {} def __getitem__(self, key): '''Return 'key' when accessed like self[key]''' return self.data[key] def __setitem__(self, key, value): '''Sets self[key] = value''' if value == "red": raise ValueError("Red is not acceptable!") else: self.data[key] = value def items(self): '''These could do whatever you want.''' return self.data.items() def keys(self): '''These could do whatever you want.''' return self.data.keys() def values(self): '''These could do whatever you want.''' return self.data.values() #=== # Now let's use it! #=== a = Foo(x=3, y=5) # Is 'a' a dict? # False print type(a) # Is it an instance of a dict? # False print isinstance(a, dict) # Can we *use* a like a dict? a['sky'] = "orange" a['ocean'] = "blue" for k,v in a.items(): print k,v for v in a.values(): print v ## Yes! Yet, it has it's own set of unique features: print a.x #<-- Prints 3 print a.y #<-- Prints 5 a['blood'] = "red" #<-- Raises exception. Thanks for the help. . .I think I see what you're saying. And to make it short & simple, the MIMEText Class behaves the way it does, because that's just how it works (or was designed). So just accept it, & move on, lol. Thanks again for the reply and the example,it's most appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question (I Hope)
On 1/14/12, Chris Kavanagh wrote: > I was looking at this code from the Python Docs > (http://docs.python.org/library/email-examples.html), trying to learn > how to send email from a Pyhton script. Anyways, part of this code > confused me. Here's the script: > > 1 # Import smtplib for the actual sending function > 2 import smtplib > 3 > 4 # Import the email modules we'll need > 5 from email.mime.text import MIMEText > 6 > 7 # Open a plain text file for reading. For this example, assume that > 8 # the text file contains only ASCII characters. > 9 fp = open(textfile, 'rb') > 10 # Create a text/plain message > 11 msg = MIMEText(fp.read()) > 12 fp.close() > 13 > 14 # me == the sender's email address > 15 # you == the recipient's email address > 16 msg['Subject'] = 'The contents of %s' % textfile > 17 msg['From'] = me > 18 msg['To'] = you > 19 > 20 # Send the message via our own SMTP server, but don't include the > 21 # envelope header. > 22 s = smtplib.SMTP('localhost') > 23 s.sendmail(me, [you], msg.as_string()) > 24 s.quit() > > What I don't understand is lines 16-18, more specifically the > msg['Subject'] format. I thought this was only done with dics?? > Obviously the variable msg isn't a dic, so how can this be done?? > > I actually put lines 11, 16,17,18, in the interpreter, then printed out > msg, so I get what it's doing, but my question still stands. How can one > do this, when I thought it could only be done with dictionaries??? Chris, I haven't looked at the module, but you should be aware that you can have user-defined classes which behave like builtin types, with their own customised features. You can also subclass a dict and customise it to do whatever. That said, as long as an object provides dictionary access methods, it can be treated like a dict in every respect. As far as python is concerned, if it looks like a duck and quacks like a duck - it's a duck. (Or in this case a dict.) It doesn't matter what the 'type' is, what is important is how you can access it. Here's an example:: # example.py # The following exapmle could be done more cleanly by subclassing the builtin # dict type, but for illustrative purposes this was not done. Instead, we show # several dict methods being defined on our dict-like class 'Foo': class Foo(object): '''This object behaves like a builtin dict that refuses the value "red".''' def __init__(self, x, y): self.x = x #<-- We can have our own properties too. self.y = y self.data = {} def __getitem__(self, key): '''Return 'key' when accessed like self[key]''' return self.data[key] def __setitem__(self, key, value): '''Sets self[key] = value''' if value == "red": raise ValueError("Red is not acceptable!") else: self.data[key] = value def items(self): '''These could do whatever you want.''' return self.data.items() def keys(self): '''These could do whatever you want.''' return self.data.keys() def values(self): '''These could do whatever you want.''' return self.data.values() #=== # Now let's use it! #=== a = Foo(x=3, y=5) # Is 'a' a dict? # False print type(a) # Is it an instance of a dict? # False print isinstance(a, dict) # Can we *use* a like a dict? a['sky'] = "orange" a['ocean'] = "blue" for k,v in a.items(): print k,v for v in a.values(): print v ## Yes! Yet, it has it's own set of unique features: print a.x #<-- Prints 3 print a.y #<-- Prints 5 a['blood'] = "red" #<-- Raises exception. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question (I Hope)
Chris Kavanagh wrote: 16 msg['Subject'] = 'The contents of %s' % textfile 17 msg['From'] = me 18 msg['To'] = you What I don't understand is lines 16-18, more specifically the msg['Subject'] format. I thought this was only done with dics?? Obviously the variable msg isn't a dic, so how can this be done?? It works with anything that behaves like a mapping. That includes dicts, naturally, but also collections.OrderedDict and any other type of object that supports the special __getitem__ method. This includes MIMEText objects, which are designed to act like mappings when you set email headers: py> from email.mime.text import MIMEText py> msg = MIMEText('This is the body of your email') py> print(msg) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit This is the body of your email py> py> py> msg['X-Header'] = 'Nobody expects the Spanish Inquisition!' py> print(msg) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Header: Nobody expects the Spanish Inquisition! This is the body of your email py> py> -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Question (I Hope)
On 1/14/12, Chris Kavanagh wrote: > I was looking at this code from the Python Docs > (http://docs.python.org/library/email-examples.html), trying to learn > how to send email from a Pyhton script. Anyways, part of this code > confused me. Here's the script: > > 1 # Import smtplib for the actual sending function > 2 import smtplib > 3 > 4 # Import the email modules we'll need > 5 from email.mime.text import MIMEText > 6 > 7 # Open a plain text file for reading. For this example, assume that > 8 # the text file contains only ASCII characters. > 9 fp = open(textfile, 'rb') > 10 # Create a text/plain message > 11 msg = MIMEText(fp.read()) > 12 fp.close() > 13 > 14 # me == the sender's email address > 15 # you == the recipient's email address > 16 msg['Subject'] = 'The contents of %s' % textfile > 17 msg['From'] = me > 18 msg['To'] = you > 19 > 20 # Send the message via our own SMTP server, but don't include the > 21 # envelope header. > 22 s = smtplib.SMTP('localhost') > 23 s.sendmail(me, [you], msg.as_string()) > 24 s.quit() > > What I don't understand is lines 16-18, more specifically the > msg['Subject'] format. I thought this was only done with dics?? > Obviously the variable msg isn't a dic, so how can this be done?? > > I actually put lines 11, 16,17,18, in the interpreter, then printed out > msg, so I get what it's doing, but my question still stands. How can one > do this, when I thought it could only be done with dictionaries??? Just from looking, I can pretty much guarantee that MIMEText, when you create an instance of it by giving it that file pointer (in other words, you give it a text file), returns a dictionary automatically. I have not looked up the module, but just reading that code, I don't think I have to. Clearly, msg is a dictionary-like object, and since msg is what the constructor of MIMEText() returns when passed the content of a file, the constructor must return a dictionary-like object. > > Thanks for any help! BTW, using Python27, WinXP. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple Question (I Hope)
I was looking at this code from the Python Docs (http://docs.python.org/library/email-examples.html), trying to learn how to send email from a Pyhton script. Anyways, part of this code confused me. Here's the script: 1 # Import smtplib for the actual sending function 2 import smtplib 3 4 # Import the email modules we'll need 5 from email.mime.text import MIMEText 6 7 # Open a plain text file for reading. For this example, assume that 8 # the text file contains only ASCII characters. 9 fp = open(textfile, 'rb') 10 # Create a text/plain message 11 msg = MIMEText(fp.read()) 12 fp.close() 13 14 # me == the sender's email address 15 # you == the recipient's email address 16 msg['Subject'] = 'The contents of %s' % textfile 17 msg['From'] = me 18 msg['To'] = you 19 20 # Send the message via our own SMTP server, but don't include the 21 # envelope header. 22 s = smtplib.SMTP('localhost') 23 s.sendmail(me, [you], msg.as_string()) 24 s.quit() What I don't understand is lines 16-18, more specifically the msg['Subject'] format. I thought this was only done with dics?? Obviously the variable msg isn't a dic, so how can this be done?? I actually put lines 11, 16,17,18, in the interpreter, then printed out msg, so I get what it's doing, but my question still stands. How can one do this, when I thought it could only be done with dictionaries??? Thanks for any help! BTW, using Python27, WinXP. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor