Re: [Tutor] Alice_in_wonderland Question

2014-05-05 Thread Jake Blank
Hi,

I finally got it.
This was the code:
for k in sorted(word_count, key=lambda x:word_count[x], reverse=True):
print (k, word_count[k])

The only question i have now is how to limit the amount of returns the
program runs to the first 15 results.




On Sun, May 4, 2014 at 10:19 PM, Brian van den Broek <
brian.van.den.br...@gmail.com> wrote:

> Hi Jake,
>
> Please do be sure to use Reply All rather than just Reply. I'm sending
> my reply and and quotes from yours to the list; that way, others can
> follow along, learn and help.
>
> Also, in general, reply under the messages to which you respond,
> ideally trimming what isn't needed away. (You will see that is what I
> have done below.)  Yes, that's not how email is used outside of
> technical circles. I'd maintain the technical circles's preference for
> not top posting is right. But, right or wrong, it is what those whom
> you are asking for free help prefer, so it is prudent to do it,
> gritting your teeth if you must :-)
>
> On 4 May 2014 21:36, Jake Blank  wrote:
> > Hey Thanks for responding.
> >
> > So now my code looks like this:
> > from wordtools import extract_words
> >
> > source_filepath=input("Enter the path to the source file:")
> > dest_filepath =input("Enter the path to the destination file:")
> >
> > sourcef=open(source_filepath, 'r')
> > destf=open(dest_filepath, 'w')
> > for line in sourcef:
> > destf.write(line)
> > file=input ("Would you like to process another file?(Y/N):")
> > if file== "Y":
> > source_filepath=input("Enter the path to the source file:")
> > dest_filepath =input("Enter the path to the destination file:")
> > else:
> > word_count = {}
> > file = open (source_filepath, 'r')
> > full_text = file.read().replace('--',' ')
> > full_text_words = full_text.split()
> >
> > for words in full_text_words:
> > stripped_words = words.strip(".,!?'`\"- ();:")
> > try:
> > word_count[stripped_words] += 1
> > except KeyError:
> > word_count[stripped_words] = 1
> >
> > ordered_keys = word_count.keys()
> > sorted(ordered_keys)
> > print ('This is the output file for Alice in Wonderland')
> > for k in sorted(ordered_keys):
> > print (k, word_count[k])
> >
> > The first part about the user specifying the file is a little off but
> > besides that I am able to return all of the words in the story with the
> > number of times they occur alphabetically.  In order to return the sorted
> > list by number of times that each word occurs I am a little confused if i
> > have to change something in my print statement?  I understand how i have
> to
> > sort the words by their associated values i'm confused where in my code i
> > would do that.
> >
> > Thanks, Jake
>
> > On Sun, May 4, 2014 at 9:16 PM, Brian van den Broek
> >  wrote:
> >>
> >> On May 4, 2014 8:31 PM, "Jake Blank"  wrote:
>
>
> 
>
>
> >> Hi Jake,
> >>
> >> You are sorting the dictionary keys by the keys themselves, whereas
> >> what you want is the keys sorted by their associated values.
> >>
> >> Look at the key parameter in
> >> https://docs.python.org/3.4/library/functions.html#sorted.
> >>
> >> To get you started, here is an example in the vicinity:
> >>
> >> >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh']
> >> >>> sorted(data)
> >> ['abiab', 'cdocd', 'efaef', 'ghbgh']
> >> >>> sorted(data, key=lambda x:x[2])
> >> ['efaef', 'ghbgh', 'abiab', 'cdocd']
> >> >>> def get_third(x): return x[2]
> >> ...
> >> >>> sorted(data, key=get_third)
> >> ['efaef', 'ghbgh', 'abiab', 'cdocd']
> >> >>>
> >>
> >> In case the lambda version is confusing, it is simply a way of doing
> >> the get_third version without having to create a function outside of
> >> the context of the sorted expression.
> >>
> >> If that sorts you, great. If not, please do ask a follow-up. (I was
> >> trying not to do it for you, but also not to frustrate by giving you
> >> too little of a push.)
>
>
> So, the code in your second message didn't seem to reflect any changes
> in light of the hint I gave. Did you not see how to apply it? If so,
> that's fine. But, rather than leave me guessing, it would be better to
> say; that way I, or others in the thread, can better direct efforts to
> help.
>
> Does this help more?
>
> >>> data = {'a':2, 'b':4, 'c':3, 'd':1}
> >>> sorted(data,key=lambda x:data[x])
> ['d', 'a', 'c', 'b']
> >>> sorted(data,key=lambda x:data[x])
> ['d', 'a', 'c', 'b']
> >>> data = {'a':2, 'b':4, 'c':3, 'd':1}
> >>> sorted(data)
> ['a', 'b', 'c', 'd']
> >>> sorted(data,key=lambda x:data[x])
> ['d', 'a', 'c', 'b']
> >>> for letter in sorted(data,key=lambda x:data[x]):
> ...   print(letter, data[letter])
> ...
> d 1
> a 2
> c 3
> b 4
> >>> for letter in sorted(data,key=lambda x:data[x],reverse=True):
> ...   print(letter, data[letter])
> ...
> b 4
> c 3
> a 2
> d 1
> >>>
>
> Can you see how key=lambda x:data[x] is forcing the sort to consider
> not the keys of the dictionary, but their associated values?
>
> Best,
>
> Bri

Re: [Tutor] Alice_in_wonderland Question

2014-05-05 Thread Jake Blank
To figure that last part out I just did a simple if statement.
for k in sorted(word_count, key=lambda x:word_count[x], reverse=True):
if word_count[k] >=300:
print (k, word_count[k])
And the output was correct.

I did have one more question though.

import os
from wordtools import extract_words

source_filepath=input("Enter the path to the source file:")
dest_filepath =input("Enter the path to the destination file:")

sourcef=open(source_filepath, 'r')
destf=open(dest_filepath, 'w')
for line in sourcef:
destf.write(line)
file=input ("Would you like to process another file?(Y/N):")
if file== "Y":
source_filepath=input("Enter the path to the source file:")
dest_filepath =input("Enter the path to the destination file:")
else:

This code asks the user for a source/dest_filepath.
I'm wondering how I can make it so the program can tell if the
source/dest_filepath the user entered is actually a program on the computer.

Also i have to ask the user if they would like to "process another
file(Y/N)?" and I'm not sure where to put that.


On Sun, May 4, 2014 at 10:38 PM, Jake Blank  wrote:

> Hi,
>
> I finally got it.
> This was the code:
> for k in sorted(word_count, key=lambda x:word_count[x], reverse=True):
> print (k, word_count[k])
>
> The only question i have now is how to limit the amount of returns the
> program runs to the first 15 results.
>
>
>
>
> On Sun, May 4, 2014 at 10:19 PM, Brian van den Broek <
> brian.van.den.br...@gmail.com> wrote:
>
>> Hi Jake,
>>
>> Please do be sure to use Reply All rather than just Reply. I'm sending
>> my reply and and quotes from yours to the list; that way, others can
>> follow along, learn and help.
>>
>> Also, in general, reply under the messages to which you respond,
>> ideally trimming what isn't needed away. (You will see that is what I
>> have done below.)  Yes, that's not how email is used outside of
>> technical circles. I'd maintain the technical circles's preference for
>> not top posting is right. But, right or wrong, it is what those whom
>> you are asking for free help prefer, so it is prudent to do it,
>> gritting your teeth if you must :-)
>>
>> On 4 May 2014 21:36, Jake Blank  wrote:
>> > Hey Thanks for responding.
>> >
>> > So now my code looks like this:
>> > from wordtools import extract_words
>> >
>> > source_filepath=input("Enter the path to the source file:")
>> > dest_filepath =input("Enter the path to the destination file:")
>> >
>> > sourcef=open(source_filepath, 'r')
>> > destf=open(dest_filepath, 'w')
>> > for line in sourcef:
>> > destf.write(line)
>> > file=input ("Would you like to process another file?(Y/N):")
>> > if file== "Y":
>> > source_filepath=input("Enter the path to the source file:")
>> > dest_filepath =input("Enter the path to the destination file:")
>> > else:
>> > word_count = {}
>> > file = open (source_filepath, 'r')
>> > full_text = file.read().replace('--',' ')
>> > full_text_words = full_text.split()
>> >
>> > for words in full_text_words:
>> > stripped_words = words.strip(".,!?'`\"- ();:")
>> > try:
>> > word_count[stripped_words] += 1
>> > except KeyError:
>> > word_count[stripped_words] = 1
>> >
>> > ordered_keys = word_count.keys()
>> > sorted(ordered_keys)
>> > print ('This is the output file for Alice in Wonderland')
>> > for k in sorted(ordered_keys):
>> > print (k, word_count[k])
>> >
>> > The first part about the user specifying the file is a little off but
>> > besides that I am able to return all of the words in the story with the
>> > number of times they occur alphabetically.  In order to return the
>> sorted
>> > list by number of times that each word occurs I am a little confused if
>> i
>> > have to change something in my print statement?  I understand how i
>> have to
>> > sort the words by their associated values i'm confused where in my code
>> i
>> > would do that.
>> >
>> > Thanks, Jake
>>
>> > On Sun, May 4, 2014 at 9:16 PM, Brian van den Broek
>> >  wrote:
>> >>
>> >> On May 4, 2014 8:31 PM, "Jake Blank"  wrote:
>>
>>
>> 
>>
>>
>> >> Hi Jake,
>> >>
>> >> You are sorting the dictionary keys by the keys themselves, whereas
>> >> what you want is the keys sorted by their associated values.
>> >>
>> >> Look at the key parameter in
>> >> https://docs.python.org/3.4/library/functions.html#sorted.
>> >>
>> >> To get you started, here is an example in the vicinity:
>> >>
>> >> >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh']
>> >> >>> sorted(data)
>> >> ['abiab', 'cdocd', 'efaef', 'ghbgh']
>> >> >>> sorted(data, key=lambda x:x[2])
>> >> ['efaef', 'ghbgh', 'abiab', 'cdocd']
>> >> >>> def get_third(x): return x[2]
>> >> ...
>> >> >>> sorted(data, key=get_third)
>> >> ['efaef', 'ghbgh', 'abiab', 'cdocd']
>> >> >>>
>> >>
>> >> In case the lambda version is confusing, it is simply a way of doing
>> >> the get_third version without having to create a function outside of
>> >> the context

Re: [Tutor] sending email via SMTP: code review requested

2014-05-05 Thread Japhy Bartlett
The "from" quirk is because it gets parsed as a header, I think.

Sending is pretty simple, you should be OK.  It may be worth setting up an
outgoing-only mail server like postfix that only listens in localhost,
gmail can be fussy about quotas.

On Sunday, May 4, 2014, Brian van den Broek 
wrote:

> Hi all,
>
> I am playing with the smtp and email modules from the standard library
> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the
> going easy; the SMTP and RFC 2822 standards are not ones I have worked
> with before. I have something that works, but I am not confident I am
> doing the right thing. For that matter, I am not very confident that I
> am not doing the wrong thing.
>
> I would very much appreciate some more experienced eyes on the code below.
> In addition to any outright errors concerning interaction with an SMTP
> server and constructing a MIME message, I would of course also welcome
> style comments. (Preemptively, I will note it isn't obvious I ought to
> have gone OOP with this.)
>
> I should also mention that I am writing this code as part of some
> tools to send myself and others reminder emails, the tools to be run
> from a cron job. I am storing an actual email account password in
> plaintext in my code. But, the account in question is one established
> just for the purpose of the reminder project and similar projects; it
> is not an account which houses my plans for world domination or the
> like. That said, I have removed the account name and password string
> below; it will thus require some adjustments to run for testing.
>
> And, as I side note, could anyone explain why changing a first world
> of a body line 'From' to '>From' is the preferred standard? I
> understand what the problem is that is being solved, but as most email
> clients interpret a leading '>' as an indication of quoting, I would
> have thought ' From' or something like '-From' would have been better.
> If I have my own code deal with the problem in one of these ways, will
> I be breaking anything?
>
> Anyway, thanks and best,
>
> Brian vdB
>
> import smtplib
>
> class SMTPSender(object):
> def __init__(self, server, port, sender, password, messages):
> self.server = server
> self.port = port
> self.sender = sender
> self.password = password
> self.messages = messages
>
> self._connect()
> try:
> self._send()
> finally:
> self._logout()
>
> def _connect(self):
> self.session = smtplib.SMTP(server, port)
> self.session.ehlo()
> self.session.starttls()
> self.session.ehlo
> self.session.login(sender, password)
>
> def _send(self):
> for message in self.messages:
> to_addresses = message["To"].split(",")
> self.session.sendmail(sender, to_addresses,
> message.as_string())
>
> def _logout(self):
> self.session.quit()
>
> if __name__ == "__main__":
> server = "smtp.gmail.com"
> port = 587
> sender = "myfunnyhan...@gmail.com "
> password = "mysecret"
>
> from email.mime.text import MIMEText
> from email.mime.multipart import MIMEMultipart
>
> # Quick and dirty test message
> msg = MIMEMultipart("alternative")
> msg["Subject"] = "SMTP Test MIMEText plain"
> msg["From"] = sender # Setting to anything but sender gets removed by
> gmail.
> msg["To"] = "some...@example.com ,
> someonee...@example.com "
> msg["Reply-to"] = "answerh...@example.com "
> body = "\n\n".join(["Test msg MIME Text",
>"From is a problem when occuring as the first word of a line."])
> msg.attach(MIMEText(body, "plain"))
>
> sender = SMTPSender(server, port, sender, password, [msg,])
> ___
> Tutor maillist  -  Tutor@python.org 
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem"

2014-05-05 Thread Walter Prins
Hi Stephen,

Please see below:

On 5 May 2014 00:17, Stephen Mik  wrote:
> Dear Python World:
> I am almost brand new to Python 3.4.0 and am taking a beginning Python
> Programming class at the nearby Community College. One major problem I have
> is time management with beginning pseudo code and coding for my Class
> Assignments.

I'm not sure we can help you with your time management skills, except
that I'd like to gently suggest the obvious: a) the sooner you get
started the better and b) regularly doing small amounts of work is
better than leaving things until the last minute.

As for getting started with pseudo code, this can be done right now.
Pseudo code is simply starting to think about and write down the steps
and procedures in your own words of what you'd like to happen to solve
whatever problem you're working on.

> The instructor prefers Office Hour help,and does not respond to
> email.

I'm not surprised.  I imagine if she allowed students to contact her
out of office hours and over email she'd do little else but deal with
student queries 24 hours a day. ;)

Are you allowed to collaborate with other students?  **If** (and only
if) you're allowed to do so, I'd suggest you could consider doing
this.

It's often beneficial to talk to your fellow students about the
programming problems to gain a better understanding and some
inspiration, and it also practices your programming communication
skills.  (It is often the case in programming courses that you're
allowed (encouraged even) to help each other at some, usually
conceptual level, provided that you still produce and submit your
*own* solutions to the problem(s), obviously you are NOT allowed to
directly share or copy each others solutions.  But please clarify what
is allowed before you do anything.  I don't want to get you into
trouble for illegal collaboration.)

> One of my Class Assignments due on May 9,2014 deals with making a
> version of the "Hangman Game" which I'm sure somebody out there is familiar
> with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded
> While lists 4.for loops and more.

Can you post the exact problem statement please?

> I can refer to the Textbook for the class
> to get a basic idea of what some of the code will look like,but I cannot
> fathom how to use a Dictionary or List  in order to enhance the Basic
> Hangman Game Problem in the text  (written by Michael Dawson).

OK.  I don't have this book but managed to track down excerpts from it
on Google books:
http://books.google.co.uk/books?id=-WULQBAJ&printsec=frontcover#v=onepage&q&f=false

I also managed to track down the web downloadable content here:
http://www.programgames.com/page4.html

I see there's 3 editions of this book.  Which version are you using?

> The Text does
> cover "Dictionaries","Lists","While Loops","For Loops" but doesn't "amplify"
> and show with examples how some of these Data Structures can be used.

Right, I've downloaded the 3rd edition source code for the book and
under the "chapter 5" folder, alongside the "hangman.py" program
you're apparently expected to enhance, you're also actually given the
other example source code from this chapter, e.g.
"geek_translator.py", "hero's_inventory3.py", "high_scores.py",
"high_scores2.py".

These programs contain examples of the use of dictionaries, lists,
while loops and for loops.

So I suggest your book does in fact show examples of how these
structures can be used?

Have you looked at these programs at all and do you follow what they do?


> If I can't get some ideas and hints from the Python Community by
> Monday,May 5,2014, I will have to hold off this difficult  programming
> assignment until Tuesday,May 6,2014 when the instructor will be available to
> help.

You only posted this request for help *on* the 5th, this leaves
yourself and us (a volunteer group not paid to do support) less than
24h to help you, going by your own timeline above.

It would be better time management next time, if you get started on
your assignments the day they're released, or shortly thereafter,
especially if you're struggling and especially if you intend to to ask
for help from third parties who might take an unpredictable amount of
time to get back to you.

In software it's always a good idea to leave yourself some
"contingency" time to deal with the unexpected and with difficult
problems.

> That leaves me just 3 days to pseudocode,code in Python,fix syntax and
> run-time and Logical errors before I can turn it in by Friday,May 16,2014.

Good luck!

> The instructor will only probably give me a suggestion and not some tangible
> ideas.Any thoughts out there about how to implement the Loops and Data
> Structures into a "Hangman Game" programming problem?

I'd like to see the actual assignment question/requirement in order to
better understand what exactly is required, given that the existing
hangman program in fact already uses:
a) a list
b) a while loop
c) a for loop (nested in an if 

Re: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem"

2014-05-05 Thread C Smith
Stephen, respond to the list so someone with more experience can help
you. I am still learning a lot about Python, but when over-generalized
questions are asked, I can tell that more experienced tutor-members
will not be able to help you much unless you can learn to frame the
questions correctly. You will notice this trend on many forums or
mailing lists not even related to Python. Sites like stackoverflow.com
(another great resource) will follow similar trends.

One thing you could do is post the code from hangman.py. Some people
will be averse to downloading even a .py file for various reasons, so
you are more likely to get a response by posting the entire code in
PLAIN TEXT. Right now your text is not plain text and will make it
hard to copy/paste or indent code properly.

If the instructor said you can use the code, use it. Changing variable
names should take about 5 seconds, as long as you have at least
feigned interest in the class. If changing variable names sounds
daunting, don't feel hopeless, you have just not been exposed to it
before. Adding a dictionary does not have to change the program
dramatically. You could even just add a minor functionality like
keeping track of score for players and not change the major parts of
the program.

Earlier you said: 'embedded while lists'.
Did you mean 'embedded while loop'?
If you can have a flash drive for the final, could you load it up with
Python reference material? If you can, make sure it is in a format
that you are comfortable searching and understanding. If you try to
cheat in programming, it will be easy to tell if the instructor
decides to ask you about your code.

On Mon, May 5, 2014 at 12:19 PM, Stephen Mik  wrote:
> Stephen Mik-"hung-up" on Hangman Python code, pseudo code,worries about
> using Dictionaries,Lists.embedded while lists,for loops:
> Thank you,. C. Smith for responding to my help plea on Python-Tutor.org. One
> version of the "Hang Man" problem is listed in the textbook,but it doesn't
> use Dictionaries,Lists,embedded while lists;so I am fairly flummoxed as to
> what is needed in the pseudo code,or Python code. If I just copy the code
> from the Textbook,I am afraid that I will not learn very much about the
> Algorithm used to guess the scrambled word in Hang Man.The program
> assignment does not require a GUI to the screen,just printing words and some
> primitive ASCII Graphics of a man being hung if the word is not guessed in
> time. The instructor says we can use the code for the Hangman.py at the back
> of the chapter,but must of course change the variable names and add
> Dictionaries,Lists,etc. Another program is due by May 13,2014(A Tic-Tac-Toe
> program,which has MANY functions. And to top it off,the Final Exam is
> scheduled for Thursday,May 15,2014. In the Final we are NOT allowed textbook
> or class notes,just a Flash Drive and a Python interpreter. Anything else
> ,including looking up help on the Internet,is strictly forbidden.You talk
> about pressure,I sure feel it now! SWM
> On Sunday, May 4, 2014 4:45 PM, C Smith 
> wrote:
> Hey, you will want to include some code to show your progress so far.
> Can you write a basic program and then work the requirements into it?
> Do you have some idea of where to start? Are you supposed to modify a
> completed version of "hangman" that is in your text, or come up with
> an original 'hangman' program? Does the hangman game need a graphical
> interface, or is just printing words to the screen fine?
>
> On Sun, May 4, 2014 at 7:17 PM, Stephen Mik
>  wrote:
>> Dear Python World:
>>I am almost brand new to Python 3.4.0 and am taking a beginning Python
>> Programming class at the nearby Community College. One major problem I
>> have
>> is time management with beginning pseudo code and coding for my Class
>> Assignments. The instructor prefers Office Hour help,and does not respond
>> to
>> email.
>>One of my Class Assignments due on May 9,2014 deals with making a
>> version of the "Hangman Game" which I'm sure somebody out there is
>> familiar
>> with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded
>> While lists 4.for loops and more. I can refer to the Textbook for the
>> class
>> to get a basic idea of what some of the code will look like,but I cannot
>> fathom how to use a Dictionary or List  in order to enhance the Basic
>> Hangman Game Problem in the text  (written by Michael Dawson). The Text
>> does
>> cover "Dictionaries","Lists","While Loops","For Loops" but doesn't
>> "amplify"
>> and show with examples how some of these Data Structures can be used. And
>> my
>> instructor's Assignment directions are very terse and "you do it by
>> yourself".
>>If I can't get some ideas and hints from the Python Community by
>> Monday,May 5,2014, I will have to hold off this difficult  programming
>> assignment until Tuesday,May 6,2014 when the instructor will be available
>> to
>> help. That leaves me just 3 days to pseudocode,code in Python,fix syntax
>> and
>

Re: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem"

2014-05-05 Thread Steven D'Aprano
On Sun, May 04, 2014 at 04:17:57PM -0700, Stephen Mik wrote:

> Any thoughts out there about 
> how to implement the Loops and Data Structures into a "Hangman Game" 
> programming problem?

Yes. Consider the basic structure of a single game:


Guess a letter.
Is it correct? Do something.
Otherwise it is wrong, do another thing.
And repeat until done.

So there's your basic loop structure. You would write it something like 
this:

while True:  # Loop forever. We'll break out of the loop when done.
guess a letter
if letter in secret_word:
# handle a correct guess
# if all the letters are shown, break out of the loop
else:
# handle a wrong guess
# if the guy has been hanged, break out of the loop

if won:
print("Yay, you won!")
else:
print("You got hanged!")


Use the "break" command to escape the infinite loop, e.g. something like 
this:

while True:
# more code goes here...
if won:
break



That should give you somewhere to start.


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


Re: [Tutor] sending email via SMTP: code review requested

2014-05-05 Thread Steven D'Aprano
On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote:
> Hi all,
> 
> I am playing with the smtp and email modules from the standard library
> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the
> going easy; the SMTP and RFC 2822 standards are not ones I have worked
> with before. 

Neither have I :-(

> I have something that works, but I am not confident I am
> doing the right thing. For that matter, I am not very confident that I
> am not doing the wrong thing.

The code seems nicely written, it's understandable and easy to read. I 
haven't tried running it yet, but nothing stands out as obviously wrong.


> I would very much appreciate some more experienced eyes on the code below.
> In addition to any outright errors concerning interaction with an SMTP
> server and constructing a MIME message, I would of course also welcome
> style comments. (Preemptively, I will note it isn't obvious I ought to
> have gone OOP with this.)

Having the SMTPSender object send a message automatically on 
instantiation strikes me as a bit wiffy. I'm not sure if it's a good 
design or not. But for a simple cron job, it may be fine.

[...]
> And, as I side note, could anyone explain why changing a first world
> of a body line 'From' to '>From' is the preferred standard?

Because it's a dirty, nasty hack invented by somebody who wasn't 
thinking very carefully at the time, and now everybody does it. Bleh.

> I
> understand what the problem is that is being solved, but as most email
> clients interpret a leading '>' as an indication of quoting, I would
> have thought ' From' or something like '-From' would have been better.
> If I have my own code deal with the problem in one of these ways, will
> I be breaking anything?

Yes. The idea is that your email client should recognise the hack when 
it sees a line ">From ..." and hide the leading ">". So if you use some 
other character, say, "!From ...", other people's mail clients won't 
know to hide the "!".


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


Re: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem"

2014-05-05 Thread Walter Prins
Hi Stephen,

Firstly 2 requests:

1)  Please do not respond to me personally; instead when interacting
with a mailing list please use "reply-all", removing me (or other
individuals) from the recipient list. You can also use "reply-list" if
your mail program has that option.  There are several reasons for
requesting that you keep the communication on the list, one of which
is that the exchanges between tutors and students are valuable to to
other students, and taking them offline by restricting responses to
one-on-one communication means other can't benefit from seeing these
exchanges.  Another reason is that if you have a one-on-one
conversation with several people, then you end up possibly wasting
several people's time by having multiple individuals potentially doing
the same work by explaining the same things multiple times, not least
due to them not being aware of each other.  This can annoy and waste
people's valuable time and may also deprive other students from having
their questions answered, which is why you should always keep
communications going back to the list whenever possible.  (Imagine you
were trying to help someone with some problem, and you generously
spend an hour crafting a reply, only to find out after the fact that 2
other people have already done the same thing!  You've just
effectively wasted an hour that could've been spent helping someone
else or with your family etc. due to someone not being considerate by
keeping their communications on the mailing list.)

2) Please set your email client to use plain text, and ensure you
properly format your emails with copious use of paragraphs.   Your
emails are coming through on my mail client as essentially one big
paragraph.  This makes it harder than it needs to be to read and
respond to.

In response to your email, see below:

On 5 May 2014 17:58, Stephen Mik  wrote:
> However,time wise I am "up against it". The Hangman program is due by
> Friday,May 9,2014. The next assignment;assignment 7,a Tic-Tac-Toe game with
> a LOT of functions is due sometime in the week of May11-May 15,2014.

There's a fully working Tic-Tac-Toe game presented in the textbook.
What are you supposed to do for the next assignment?  Get started on
it now and it may well be finished well before this deadline. :)   As
with my previous email: PLEASE POST THE EXACT ASSIGNMENT TEXT.  Do not
paraphrase, do not summarize.  Give as much information as possible
about what is expected of you.

> And the
> Final Exam,which we are NOT to use the Textbook or written notes,or the
> Internet, is scheduled for Friday morning,May 15,2014. It is comprehensive
> and covers the first 6 chapters of the Textbook.We are only allowed a Flash
> Drive,a "toolkit" of programs from the Textbook,and a computer in which
> Python 3.4.0 is to be run. As you can see that I am under a LOT of PRESSURE
> in the next 11 days!

Then you need to practice as much as you can in the next 11 days.
Write your own programs using all the constructs you've learned so far
until you're totally comfortable with all the material.  Play with the
Python interpreter.  Be totally comfortable with how it works etc.  If
you do that, you should have no problem with the exam, I would've
thought.

Also, start creating your toolkit *now*.  Put Python on there (if
you're expected and/or allowed to), and start working with your
toolkit now.  Use it as much as you can until it becomes a familiar
and relatively comfortable place to be.  Put snippets of code from the
book on there , possibly put the entire source library from the book
(which I posted earlier) on there, if you're allowed to.  (Be sure to
ask exactly what is and isn't allowed on your flash drive when you see
the instructor.)

> Personally,I don't think the Final Exam (25%of the class
> grade) is fair,but the instructor is strict and I dare not question her
> judgement or teaching pedagogy.  Since I attend a Community College where
> there is little interaction from the students,I have to "go it alone" much
> of the time. There truly is little interaction between students,I couldn't
> get a Tutor,about 90% of the Community College Lab staff doesn't know
> Python,the list goes on. I think that I will consult with the instructor on
> Tuesday,May 6,2014 in her crowded office hour for suggestions on how to
> proceed.  I still don't understand how a "Dictionary" could be incorporated
> into an existing "hangman" program which doesn't apparently need it to
> operate.

One way would be to invent a new feature for the game that would
require it.  For example you could enhance the game to  a) support
multiple players and b) store some statistics (or maybe the game
outcomes themselves) by user.  One could use a dictionary to keep
track of each players data for example.  But as I requested before:
Can you please post the actual homework/assignment instructions
please?

> Again,thanks for your help,concern and programming experience and
> all the time you have volunteered to look

Re: [Tutor] sending email via SMTP: code review requested

2014-05-05 Thread Brian van den Broek
On 5 May 2014 13:53, Steven D'Aprano  wrote:
> On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote:
>> Hi all,
>>
>> I am playing with the smtp and email modules from the standard library
>> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the
>> going easy; the SMTP and RFC 2822 standards are not ones I have worked
>> with before.
>
> Neither have I :-(


Hi All,

Thanks for the feedback, Steven. (And to Japhy for the earlier reply).

>> I have something that works, but I am not confident I am
>> doing the right thing. For that matter, I am not very confident that I
>> am not doing the wrong thing.
>
> The code seems nicely written, it's understandable and easy to read. I
> haven't tried running it yet, but nothing stands out as obviously wrong.

Well, gosh'n'golly-gee.

>> I would very much appreciate some more experienced eyes on the code below.
>> In addition to any outright errors concerning interaction with an SMTP
>> server and constructing a MIME message, I would of course also welcome
>> style comments. (Preemptively, I will note it isn't obvious I ought to
>> have gone OOP with this.)
>
> Having the SMTPSender object send a message automatically on
> instantiation strikes me as a bit wiffy. I'm not sure if it's a good
> design or not. But for a simple cron job, it may be fine.

That is a pattern I often have and for which I often have an
associated spidey-tingle. If I have a class that exists to handle some
processing and then be heard from no more, it always seems a bit funny
to do:

my_thing = MyOneTimeTaskClass(vars)
my_thing.do_it()

as every time I instantiate MyOneTimeTaskClass I am going to
immediately ask the instance to do the things it does.

Either way feels, as you say `wiffy.' (I think I shall steal that.)


> [...]
>> And, as I side note, could anyone explain why changing a first world
>> of a body line 'From' to '>From' is the preferred standard?
>
> Because it's a dirty, nasty hack invented by somebody who wasn't
> thinking very carefully at the time, and now everybody does it. Bleh.
>
>> I
>> understand what the problem is that is being solved, but as most email
>> clients interpret a leading '>' as an indication of quoting, I would
>> have thought ' From' or something like '-From' would have been better.
>> If I have my own code deal with the problem in one of these ways, will
>> I be breaking anything?
>
> Yes. The idea is that your email client should recognise the hack when
> it sees a line ">From ..." and hide the leading ">". So if you use some
> other character, say, "!From ...", other people's mail clients won't
> know to hide the "!".


Oh dear.

I developed my code mostly checking the resulting email with the gmail
app on Android. It doesn't handle things this way; instead, some
(variable! No, really) portion of the message body gets displayed as a
quote. I would declare my surprise, but little about the act of malice
that is the gmail Android app could surprise me now. (Unlike the web
interface, there is quite literally no way to send plain text from the
app.)

Anyway, thanks again,

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


Re: [Tutor] sending email via SMTP: code review requested

2014-05-05 Thread Japhy Bartlett
If you get deeper into processing emails, you might check out
http://lamsonproject.org/ .  I wasn't fond of the whole thing, but if you
dig into the src there is some pretty good code for handling malformed MIME
structures and unicode issues in a sane way.


On Mon, May 5, 2014 at 4:26 PM, Brian van den Broek <
brian.van.den.br...@gmail.com> wrote:

> On 5 May 2014 13:53, Steven D'Aprano  wrote:
> > On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote:
> >> Hi all,
> >>
> >> I am playing with the smtp and email modules from the standard library
> >> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the
> >> going easy; the SMTP and RFC 2822 standards are not ones I have worked
> >> with before.
> >
> > Neither have I :-(
>
>
> Hi All,
>
> Thanks for the feedback, Steven. (And to Japhy for the earlier reply).
>
> >> I have something that works, but I am not confident I am
> >> doing the right thing. For that matter, I am not very confident that I
> >> am not doing the wrong thing.
> >
> > The code seems nicely written, it's understandable and easy to read. I
> > haven't tried running it yet, but nothing stands out as obviously wrong.
>
> Well, gosh'n'golly-gee.
>
> >> I would very much appreciate some more experienced eyes on the code
> below.
> >> In addition to any outright errors concerning interaction with an SMTP
> >> server and constructing a MIME message, I would of course also welcome
> >> style comments. (Preemptively, I will note it isn't obvious I ought to
> >> have gone OOP with this.)
> >
> > Having the SMTPSender object send a message automatically on
> > instantiation strikes me as a bit wiffy. I'm not sure if it's a good
> > design or not. But for a simple cron job, it may be fine.
>
> That is a pattern I often have and for which I often have an
> associated spidey-tingle. If I have a class that exists to handle some
> processing and then be heard from no more, it always seems a bit funny
> to do:
>
> my_thing = MyOneTimeTaskClass(vars)
> my_thing.do_it()
>
> as every time I instantiate MyOneTimeTaskClass I am going to
> immediately ask the instance to do the things it does.
>
> Either way feels, as you say `wiffy.' (I think I shall steal that.)
>
>
> > [...]
> >> And, as I side note, could anyone explain why changing a first world
> >> of a body line 'From' to '>From' is the preferred standard?
> >
> > Because it's a dirty, nasty hack invented by somebody who wasn't
> > thinking very carefully at the time, and now everybody does it. Bleh.
> >
> >> I
> >> understand what the problem is that is being solved, but as most email
> >> clients interpret a leading '>' as an indication of quoting, I would
> >> have thought ' From' or something like '-From' would have been better.
> >> If I have my own code deal with the problem in one of these ways, will
> >> I be breaking anything?
> >
> > Yes. The idea is that your email client should recognise the hack when
> > it sees a line ">From ..." and hide the leading ">". So if you use some
> > other character, say, "!From ...", other people's mail clients won't
> > know to hide the "!".
>
>
> Oh dear.
>
> I developed my code mostly checking the resulting email with the gmail
> app on Android. It doesn't handle things this way; instead, some
> (variable! No, really) portion of the message body gets displayed as a
> quote. I would declare my surprise, but little about the act of malice
> that is the gmail Android app could surprise me now. (Unlike the web
> interface, there is quite literally no way to send plain text from the
> app.)
>
> Anyway, thanks again,
>
> Brian vdB
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Final review

2014-05-05 Thread Scott W Dunning

On May 1, 2014, at 5:30 AM, Steven D'Aprano  wrote:

Awesome, thanks everyone!  I understand lists a lot better now.  

I have another question.  I don’t understand why below would give an error?

>>>  greeting = 'Hello World’
>>>  greeting [len(greeting)]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Final review

2014-05-05 Thread meenu ravi
Hi Scott,

The variable greeting is of type "string".

>>> greeting = "Hello world"

>>> type(greeting)




The len(string) will count each character in the value of variable
"greeting" starting from '1'.

H - 1

e - 2

l - 3

l - 4

0 - 5

space - 6(Space and special characters are also counted)

w - 7

o - 8

r - 9

l - 10

d - 11



So the len(greeting) returns 11.

>>> len(greeting)

11

But while accessing with the index, indexing starts with '0'. Like,

>>> greeting.index('H')

0

>>> greeting.index('e')

1

>>> greeting.index('l')

2


Likewise, the index of d, which is the last word in the word "Hello world"
is 10.

So, the maximum index you can access in the word "Hello world" is 10. But
when you try to give the command,

>>> greeting [len(greeting)]

It is trying to access the character at the position "11", where the string
"Hello world" doesn't contain any value in the index "11" and the maximum
index is 10. So it throws the following error.

Traceback (most recent call last):

  File "", line 1, in 

IndexError: string index out of range

So always the maximum index will be length - 1. So if you want to access
the last character of the string "Hello world", give the command:

>>> greeting[len(greeting)-1]

'd'

Hope this helps

Thanks
On 6 May 2014 09:35, "Scott W Dunning"  wrote:

>
> On May 1, 2014, at 5:30 AM, Steven D'Aprano  wrote:
>
> Awesome, thanks everyone!  I understand lists a lot better now.
>
> I have another question.  I don’t understand why below would give an error?
>
> >>>  greeting = 'Hello World’
> >>>  greeting [len(greeting)]
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor