Re: [Tutor] scratching my head - still

2015-08-05 Thread Peter Otten
Cameron Simpson wrote:

> On 05Aug2015 12:46, Steven D'Aprano  wrote:
>>On Tue, Aug 04, 2015 at 05:52:15PM -0700, Clayton Kirkwood wrote:
>>> As seen below (closely), some filenames are not being removed while
>>> others are, such as in the first stanza, some pdfs are removed, some
>>> aren't. In the second stanza, Thumbs.db makes it through, but was caught
>>> in the first stanza. (Thanks for those who have proffered solutions to
>>> date!) I see no logic in the results. What am I missing???
>>
>>You are modifying the list of files while iterating over it, which plays
>>all sorts of hell with the process. Watch this:
> [... detailed explaination ...]
>>The lesson here is that you should never modify a list while iterating
>>over it. Instead, make a copy, and modify the copy.
> 
> What Steven said. Yes indeed.
> 
> Untested example suggestion:
> 
>   all_filenames = set(filenames)
>   for filename in filenames:
> if .. test here ...:
>   all_filenames.remove(filename)
>   print(all_filenames)
> 
> You could use a list instead of a set and for small numbers of files be
> fine. With large numbers of files a set is far faster to remove things
> from.

If the list size is manageable, usually the case for the names of files in 
one directory, you should not bother about removing items. Just build a new 
list:

all_filenames = [...]
matching_filenames = [name for name in all_filenames if test(name)]

If the list is huge and you expect that most items will be kept you might 
try reverse iteration:

for i in reversed(range(len(all_filenames))):
name = all_filenames[i]
if test(name):
del all_filenames[i]

This avoids both copying the list and the linear search performed by 
list.remove().

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


Re: [Tutor] About Python Module to Process Bytes

2015-08-05 Thread Michelle Meiduo Wu
I think this works for me!
Thanks a lot,Michelle

> From: d...@hashcollision.org
> Date: Tue, 4 Aug 2015 12:12:20 -0700
> Subject: Re: [Tutor] About Python Module to Process Bytes
> To: wum...@hotmail.com
> CC: tutor@python.org
> 
> On Tue, Aug 4, 2015 at 9:26 AM, Michelle Meiduo Wu  wrote:
> > Hi there,
> > I'd like to find some python module to easily process bytes array data, 
> > like encoding different types of data (char, long, short, float, etc) into 
> > a same bytes array. I checked Python built-in library and there are bytes 
> > and bytearray types which don't provide enough functions for processing 
> > bytes easily.
> > Does anybody know if there is other convenient module available for 
> > processing bytes?
> 
> 
> The 'struct' module is probably what you're looking for.  See:
> 
> https://docs.python.org/3.5/library/struct.html
> 
> Let us know if you have any questions with it.  Good luck.
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] who makes FOR loop quicker

2015-08-05 Thread John Doe
To pass by reference or by copy of - that is the question from hamlet. 
("hamlet" - a community of people smaller than a village python3.4-linux64)


xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1

So, catch the output and help, PLEASE, me improve the answer:
Does it appropriate ALWAYS reevaluate the terms of the list on each 
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from 
an output) and reduce unreasonable reevaluation, what I must to do for that?

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


Re: [Tutor] Plotting asymmetric error bars for a single point in matplotlib

2015-08-05 Thread Peter Otten
Colin Ross wrote:

> Hi all,
> 
> Goal: To plot asymmetric x error bars for a single point using errorbar. I
> am interested in displaying the inter quartile range (IQR) for a data set.
> 
> Code:
> 
> import numpy as np
> import matplotlib.pyplot as plt
> 
> y = 1.0
> data = np.random.rand(100)
> 
> median = np.median(data)
> upper_quartile = np.percentile(data, 75)
> lower_quartile = np.percentile(data, 25)
> IQR = upper_quartile - lower_quartile
> 
> plt.errorbar(median, y, xerr=[lower_quartile ,upper_quartile], fmt='k--')
> 
> plt.savefig('IQR.eps')
> plt.show()
> 
> Error:
> 
> Traceback (most recent call last):
>   File "IQR.py", line 15, in 
> plt.errorbar(median, y, xerr=[0.5,0.75], fmt='k--')
>   File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2251, in
> errorbar
> ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize,
> barsabove, lolims, uplims, xlolims, xuplims, **kwargs)
>   File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 5327, in
> errorbar
> in cbook.safezip(x,xerr)]
>   File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1294, in
> safezip
> raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))
> ValueError: In safezip, len(args[0])=1 but len(args[1])=2
> 
> My understanding is that safezip zips together the x array with the
> specified upper and lower x limits?

If you have not solved this yet: my guess is that 

xerr=[lower_quartile, upper_quartile]

is interpreted as two error values that would correspond to x and y vectors 
of length 2 with symmetrical error bars. Try passing a list of two length-
one lists two avoid the ambiguity:

plt.errorbar(
median, y, 
xerr=[[lower_quartile], [upper_quartile]], fmt='k--')


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


Re: [Tutor] who makes FOR loop quicker

2015-08-05 Thread Joel Goldstick
On Wed, Aug 5, 2015 at 3:53 AM, John Doe  wrote:
> To pass by reference or by copy of - that is the question from hamlet.
> ("hamlet" - a community of people smaller than a village python3.4-linux64)
>
> xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> i = 0
> for x in xlist:
> print(xlist)
> print("\txlist[%d] = %d" % (i, x))
> if x%2 == 0 :
> xlist.remove(x)
> print(xlist, "\n\n")
> i = i + 1
>
> So, catch the output and help, PLEASE, me improve the answer:
> Does it appropriate ALWAYS reevaluate the terms of the list on each
> iteration?
> But if I want to pass a copy to FOR instead of a reference (as seen from an
> output) and reduce unreasonable reevaluation, what I must to do for that?

You aren't passing anything.  the for statement is in the same
namespace as the rest of the code.  Passing in python is different
than in C or other languages.

A couple of comments:

setting i = 0, then incrementing at the end of the loop would more
pythonically be done with the enumerate function.
Its generally a bad idea to remove items from and iterable while
interating over it.  I'm guessing that this is what is confusing you.
One way to remove items from a list is to create a new list, and
append items you want to it, skipping the ones you don't.  You don't
really need the index at all since python interation protocol will
walk through the list for you without worrying about index values
-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] who makes FOR loop quicker

2015-08-05 Thread Steven D'Aprano
On Wed, Aug 05, 2015 at 10:53:14AM +0300, John Doe wrote:
> To pass by reference or by copy of - that is the question from hamlet. 
> ("hamlet" - a community of people smaller than a village python3.4-linux64)

Python *never* uses either pass by reference OR pass by value (copy). 
Please read this:

http://import-that.dreamwidth.org/1130.html


> xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> i = 0
> for x in xlist:
>   print(xlist)
>   print("\txlist[%d] = %d" % (i, x))
>   if x%2 == 0 :
>   xlist.remove(x)
>   print(xlist, "\n\n")
>   i = i + 1


It is risky to remove items from a list as you iterate over it. Also, 
there is no need to manually increment the index i. Instead, use 
enumerate(alist), which returns (index, value) for each value in alist. 
But before iterating over the list, make a copy. The easiest way to copy 
a list is to take a slice using alist[start:end]. If you leave both 
start and end out, it copies the entire list.

First improvement:

for i, x in enumerate(xlist[:]):  # list[:] makes a copy of the list
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")


But we can improve this even more. Instead of *removing* items we don't 
want, we should *add* items we do want. This will almost always be 
faster, especially for big lists:

new = []
for i, x in enumerate(xlist):  # No need for a copy now.
print("xlist[%d] = %d" % (i, x))
if x%2 != 0 :
new.append(x)
print(new, "\n\n")


And in fact we can write this even more compactly:

new = [x for x in xlist if x%2 != 0]

although in this case you don't get the benefit of printing. This is 
called a "list comprehension", and we can break it down:


[x  # the expression to be appended to the new list
 for x in xlist  # the "for loop" part
 if x%2 != 0  # optional condition that applies to the expression
 ]



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


Re: [Tutor] who makes FOR loop quicker

2015-08-05 Thread Mark Lawrence

On 05/08/2015 08:53, John Doe wrote:

To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village python3.4-linux64)

xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
 print(xlist)
 print("\txlist[%d] = %d" % (i, x))
 if x%2 == 0 :
 xlist.remove(x)
 print(xlist, "\n\n")
 i = i + 1

So, catch the output and help, PLEASE, me improve the answer:
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from
an output) and reduce unreasonable reevaluation, what I must to do for
that?


From https://docs.python.org/3/tutorial/introduction.html#lists, but 
chopped around a little:-



All slice operations return a new list containing the requested 
elements. This means that the following slice returns a new (shallow) 
copy of the list:


>>> squares = [1, 4, 9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]


Hence:-

for x in xlist[:]:
  etc.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


[Tutor] Problem on select esecution of object in a class

2015-08-05 Thread jarod_v6--- via Tutor
I have a class with many objects and I want to  select using opt parse some 
function
id = options.step
ena = Rnaseq(options.configura, options.rst, options.outdir)
now = datetime.datetime.now()
ena.show()
diz = {}
for i,t in enumerate(ena.steps()):
diz.setdefault(i,[]).append(t.__name__)

for i in "ena."+"".join(diz[id])+"()":

print i.command

 1 for i in "ena."+"".join(diz[id])+"()":
  2 
> 3 print i.command
  4 

AttributeError: 'str' object has no attribute 'command'


here you se what they ouptut
"ena."+"".join(diz[id])+"()"
Out[85]: 'ena.trimmomatic()

Definition: ena.trimmomatic(self)
Source:
def trimmomatic(self):

"""
Raw reads quality trimming and removing of Illumina adapters is 
performed using 
[Trimmomatic](http://www.usadellab.org/cms/index.php?page=trimmomatic).

This step takes as input files:

1. FASTQ files from the readset file if available

"""
jobs = []
for readset in self.readsets:

trim_file_prefix = os.path.join(self.pt,"trim", 
readset.sample.name, readset.name + ".trim.")
trim_log = trim_file_prefix + "log"
trim_stats = trim_file_prefix + "stats.csv"
:

Any suggestion in how to choose the function to use?
M.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem on select esecution of object in a class

2015-08-05 Thread Alan Gauld

On 05/08/15 17:04, jarod_v6--- via Tutor wrote:


 for i in "ena."+"".join(diz[id])+"()":
 print i.command


Here you are creating a string and then iterating over
the string one character at a time. But the characters
do not have a command attribute.


> 3 print i.command
   4

AttributeError: 'str' object has no attribute 'command'


As shown by the error message.


here you se what they ouptut
"ena."+"".join(diz[id])+"()"
Out[85]: 'ena.trimmomatic()

Definition: ena.trimmomatic(self)


These are not the same thing at all. The second is the
description of a method of your class not a string.
The fact that the name of the method happens to be
the same as the string contents makes no difference
to Python.


Any suggestion in how to choose the function to use?


The usual way to translate a string to a function is to
use a dictionary with name: function pairs.

But your class already builds one of those so you can use
the getattr() function to get the attribute:

myMethod = getattr(myobj, attr_name)

or

myResult = getattr(myobj, attrName)(params)

to call the method.

However, building function names as strings and then calling
them is usually a bad design pattern. Especially for large
numbers of objects. So maybe if you explain what/why you are
doing this we can suggest a better alternative.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Problem on select esecution of object in a class

2015-08-05 Thread Peter Otten
jarod_v6--- via Tutor wrote:

> I have a class with many objects and I want to  select using opt parse
> some function id = options.step
> ena = Rnaseq(options.configura, options.rst, options.outdir)
> now = datetime.datetime.now()
> ena.show()
> diz = {}
> for i,t in enumerate(ena.steps()):
> diz.setdefault(i,[]).append(t.__name__)
> 
> for i in "ena."+"".join(diz[id])+"()":
> 
> print i.command
> 
>  1 for i in "ena."+"".join(diz[id])+"()":
>   2
> > 3 print i.command
>   4
> 
> AttributeError: 'str' object has no attribute 'command'
> 
> 
> here you se what they ouptut
> "ena."+"".join(diz[id])+"()"
> Out[85]: 'ena.trimmomatic()
> 
> Definition: ena.trimmomatic(self)
> Source:
> def trimmomatic(self):

[...]

> Any suggestion in how to choose the function to use?

If I understand you correctly you want getattr():

ena = Rnaseq(options.configura, options.rst, options.outdir)
method = getattr(ena, options.step)
method()


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


[Tutor] R: Tutor Digest, Vol 138, Issue 26 Re: Problem on select esecution of object in a class (Alan Gauld)

2015-08-05 Thread jarod_v6--- via Tutor
Thanks so much fro the help. What I want to do is to obtain a selection of the 
function I want to run.

ena = Rnaseq(options.configura, options.rst, options.outdir)
cmdset = [ ena.trimmomatic,
ena.star,
ena.merge_trimmomatic_stats
]
ena.show()
1 ena.trimmomatic
2 ena.star
3 ena.merge_trimmomatic_stats
The class RNaseq have multiple function. I want a way to run or from 1 to 3 or 
from 2 to 3 or only the 2 o 3 step.

...
parser.add_option("-s", "--step",action="store", 
dest="steps",type="string",
help=" write input file: %prg -o : directory of results ")

python myscript -s 1,3 ...

At the moment the only way I found is this:
 for cmd in cmdset: 
step = cmd()
for i in step:
print i.command
but is not elegant so I want to know more what is the right way to generate a 
execution f the function of the class by select which is the step we want to 
start.





>However, building function names as strings and then calling
>them is usually a bad design pattern. Especially for large
>numbers of objects. So maybe if you explain what/why you are
>doing this we can suggest a better alternative.
>
>-- 
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.amazon.com/author/alan_gauld
>Follow my photo-blog on Flickr at:
>http://www.flickr.com/photos/alangauldphotos
>
>
>
>
>--
>
>Subject: Digest Footer
>
>___
>Tutor maillist  -  Tutor@python.org
>https://mail.python.org/mailman/listinfo/tutor
>
>
>--
>
>End of Tutor Digest, Vol 138, Issue 26
>**
>


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


Re: [Tutor] find pickle and retrieve saved data

2015-08-05 Thread Oscar Benjamin
On 4 August 2015 at 23:09, Quiles, Stephanie
 wrote:
> I am still struggling with this one.

Hi Stephanie, Alan has already raised a few issues with your code so
I'm just going to address the one that's showing in your error
message.

These two lines are generating the error message:

> infile = open("emails.dat", "r")
> name = infile.readline()
>
> This is the error i am getting:
>
> enter a name in the file for info: sarah
> Traceback (most recent call last):
>   File "/Users/stephaniequiles/Downloads/findemails.py", line 28, in 
> main()
>   File "/Users/stephaniequiles/Downloads/findemails.py", line 9, in main
> name = infile.readline()
>   File 
> "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", 
> line 319, in decode
> (result, consumed) = self._buffer_decode(data, self.errors, final)
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: 
> invalid start byte

The interpreter is assuming that your file 'emails.dat' is a text file
using the utf-8 encoding. This is because you didn't specify an
encoding when opening the file. To specify that the encoding is e.g.
ascii you would do:

infile = open('emails.dat', 'r', encoding='ascii')

The error occurs because the bytes read from the file are not valid
for utf-8. What program did you use to write the emails.dat file? Does
it look reasonable when you open it in a text editor e.g. your code
editor or notepad?

If you show the output of the following command then someone may be
able to guess the encoding that you should be using for this file:

infile = open('emails.dat', 'rb')  # b for binary mode
print(repr(infile.read(100)))

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


[Tutor] Dictionary Issue

2015-08-05 Thread Ltc Hotspot
Hi everyone:

I want to write a python program that reads through the data file of
mbox-short.txt.Mbox-short.txt, i.e.,  download is available at
http://www.py4inf.com/code/mbox-short.txt.

Secondly, I want for python to figure out who sent the greatest number of
mail messages.

The output should read: c...@iupui.edu 5


However, there is a traceback message:

In [40]: %run 9_4_4.py
  File "C:\Users\vm\Desktop\apps\docs\Python\_9_4_4.py", line 19
count = dict()
^
SyntaxError: invalid syntax


Raw data code reads:



fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
text = handle.read()

## The program looks for 'From ' lines and takes the second
## word of those lines as the person who sent the mail.

addresses = set()
for addr in [ text.split()[2]('From  ')
if fromline

## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

count = dict()
for wrd in word:
count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none
for kee, val in count.items():
if maxval == none or maxval https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Mark Lawrence

On 05/08/2015 15:15, Ltc Hotspot wrote:

Hi everyone:

I want to write a python program that reads through the data file of
mbox-short.txt.Mbox-short.txt, i.e.,  download is available at
http://www.py4inf.com/code/mbox-short.txt.

Secondly, I want for python to figure out who sent the greatest number of
mail messages.

The output should read: c...@iupui.edu 5

However, there is a traceback message:

In [40]: %run 9_4_4.py
   File "C:\Users\vm\Desktop\apps\docs\Python\_9_4_4.py", line 19
 count = dict()
 ^
SyntaxError: invalid syntax

Raw data code reads:

fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
text = handle.read()

## The program looks for 'From ' lines and takes the second
## word of those lines as the person who sent the mail.

addresses = set()
for addr in [ text.split()[2]('From  ')
 if fromline

## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

 count = dict()
 for wrd in word:
 count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none
for kee, val in count.items():
 if maxval == none or maxval 

Learning to find these syntax errors is a skill you must learn for 
yourself.  Very often the error occurs one or even more lines before the 
line on which the error is actually detected.  So please go back from 
where the error is actually reported and see what you can find.  Very 
strong hint, do not ask again until you've fixed at least two of the 
three errors, and there may be more :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Danny Yoo
> However, there is a traceback message:
>
> In [40]: %run 9_4_4.py
>   File "C:\Users\vm\Desktop\apps\docs\Python\_9_4_4.py", line 19
> count = dict()
> ^
> SyntaxError: invalid syntax

Syntax error reporting is approximate: you might need to look a few lines
earlier to get at the root problem.

... Reading...

The for loop looks odd because there's a leading open bracket that looks
out of place.

for addr in [ text.split()[2]('From  ')

The following line uses an if conditional, but needs to offset the block
with a ":" that appears to be missing.

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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Clayton Kirkwood


> -Original Message-
> From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
> Behalf Of Mark Lawrence
> Sent: Wednesday, August 05, 2015 3:23 PM
> To: tutor@python.org
> Subject: Re: [Tutor] Dictionary Issue
> 
> On 05/08/2015 15:15, Ltc Hotspot wrote:
> > Hi everyone:
> >
> > I want to write a python program that reads through the data file of
> > mbox-short.txt.Mbox-short.txt, i.e.,  download is available at
> > http://www.py4inf.com/code/mbox-short.txt.
> >
> > Secondly, I want for python to figure out who sent the greatest number
> > of mail messages.
> >
> > The output should read: c...@iupui.edu 5
> >
> > However, there is a traceback message:
> >
> > In [40]: %run 9_4_4.py
> >File "C:\Users\vm\Desktop\apps\docs\Python\_9_4_4.py", line 19
> >  count = dict()
> >  ^
> > SyntaxError: invalid syntax
> >
> > Raw data code reads:
> >
> > fname = raw_input("Enter file name: ") handle = open (fname, 'r') text
> > = handle.read()
> >
> > ## The program looks for 'From ' lines and takes the second ## word of
> > those lines as the person who sent the mail.
> >
> > addresses = set()
> > for addr in [ text.split()[2]('From  ')
> >  if fromline
> >
> > ## The program creates a Python dictionary that maps ## the sender's
> > mail address to a count of the number ## of times they appear in the
> > file.
> >
> >  count = dict()
> >  for wrd in word:
> >  count[wrd]= count.get(wrd,0) +1
> >
> > ## After the dictionary is produced, the program reads ## through the
> > dictionary using a maximum loop to ## find the greatest number of mail
> > messages.
> >
> > maxval = none
> > maxkee = none
> > for kee, val in count.items():
> >  if maxval == none or maxval  >  maxval = val
> >  maxkee = kee
> >
> > print items
> >
> > Question: What is the cause of the dictionary line message?
> >
> 
> Learning to find these syntax errors is a skill you must learn for
yourself.  Very
> often the error occurs one or even more lines before the line on which the
> error is actually detected.  So please go back from where the error is
actually
> reported and see what you can find.  Very strong hint, do not ask again
until
> you've fixed at least two of the three errors, and there may be more :)
> 

It looks like the problem is with count=dict()
Should be count=dict{}

I may be wrong - U'm still a neophyte.
crk

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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Alan Gauld

On 05/08/15 23:36, Clayton Kirkwood wrote:


It looks like the problem is with count=dict()
Should be count=dict{}

I may be wrong - U'm still a neophyte.


Yes, you're wrong! :-)

the correct form is as shown

count = dict()

Its calling the type operation which looks like
any other function and uses ().

The more common way to create an empty dictionary
is to use {} alone, like:

count = {}

Although count is probably a bad name choice since
collections usually merit a plural name, eg counts

But the OP's problems lie earlier in the code,
as Mark has suggested.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Alan Gauld

On 05/08/15 15:15, Ltc Hotspot wrote:


Raw data code reads:


Being picky here but data and code are very different
things (in most languages at least) and what you have
below is definitely code not data.

Meanwhile there are lots of issues in this code...


fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
text = handle.read()

## The program looks for 'From ' lines and takes the second
## word of those lines as the person who sent the mail.

addresses = set()
for addr in [ text.split()[2]('From  ')
 if fromline


The above looks like its supposed to be a list
comprehension embedded in a for loop. Putting too much
code in one line is usually a bad idea especially before
you have it working.

Try separating out the formation of your list from the
for loop. Once you get the comprehension working correctly
then you can consider embedding it.

As for the expression

text.split()[2]('From  ')

Can you explain how you think that works?
Try it at the >>> prompt with text set to
a sample line of data.

Try

>>> text = .. # whatever your data looks like
>>> text.split()

>>> text.split[2]

>>> text.split()[2]('From  ')

The >>> prompt is one of your most powerful tools while
writing code, you should always have one ready to try
stuff out. You can answer a lot of questions that way.


## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

 count = dict()
 for wrd in word:


What is word? You don't define it anywhere?


 count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none


none is not a value in Python.
You need to spell it None


for kee, val in count.items():
 if maxval == none or maxval 

You don't really need the maxval == None check since None
is considered less that all other numbers.


 maxval = val
 maxkee = kee

print items


What is items? It is not defined anywhere above.
count.items is not the same as items.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Pep 8, about indentation

2015-08-05 Thread D Wyatt
I clearly remember that the standard for Python was to use 2 spaces
for indentation of blocks.  Now Pep 8 says 4.  When did that change?
I hate it when things change on me like that. And what else has
changed?

Then again, maybe I dreamed it.  Am I crazy? TIA
-- 
Deb Wyatt in WA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Ltc Hotspot
Hi Mark,

Address identifies the email address with the maximum  number of sends:
c...@iupui.edu.

Secondly, we are missing a count on the number of messages sent by
c...@iupui.edu, i.e., 5.

Thirdly, maxval 'none' is not defined on line # 24

Questions: How do we define the value of none for the key maxval and
retrieve a number count on the number of messages sent by c...@iupui.edu.


NameError:

Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\assignment_9_4_5.py in ()
 22 ## find the greatest number of mail messages.
 23
---> 24 maxval = none
 25 maxkee = none
 26 for kee, val in count.items():

NameError: name 'none' is not defined

In [52]: print address
c...@iupui.edu

Revised data:


## The program looks for 'From ' lines and takes the second
## word of those lines as the person who sent the mail.

fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
for line in handle:
if line.startswith("From: "):
address = line.split()[1]


## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

count = dict()
for wrd in address:
count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none
for kee, val in count.items():
if maxval == none or maxval 
> ___
> 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] Dictionary Issue

2015-08-05 Thread Alan Gauld

On 05/08/15 23:58, Ltc Hotspot wrote:


fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
for line in handle:
 if line.startswith("From: "):
 address = line.split()[1]



So far so good.



## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

 count = dict()


But here you create a brand new dictionary.
Every time you go round the loop.
And it wipes out the old one.
You need to move that out of the loop.


 for wrd in address:


address is a string. So wrd will be set to every
character in the string. I don;t think that's what
you want?


 count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none


See my previous email. none should be None.
Case matters in Python.


for kee, val in count.items():
 if maxval == none or maxval 

Notice that address gets reset every time the loop reads
a new line so this will only print the last address.
But maybe that's what you wanted?



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Pep 8, about indentation

2015-08-05 Thread Ben Finney
D Wyatt  writes:

> I clearly remember that the standard for Python was to use 2 spaces
> for indentation of blocks.  Now Pep 8 says 4.  When did that change?

When do you remember it being as you describe?

-- 
 \ “People's Front To Reunite Gondwanaland: Stop the Laurasian |
  `\  Separatist Movement!” —wiredog, http://kuro5hin.org/ |
_o__)  |
Ben Finney

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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Alan Gauld

On 06/08/15 02:05, Ltc Hotspot wrote:

The revised output reads:

In [3]: %run assignment_9_4_9.py
Enter file name: mbox-short.txt
c...@iupui.edu  14

The desired output: c...@iupui.edu  5



See my other post.
Count the number of letters in the address.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Pep 8, about indentation

2015-08-05 Thread Alan Gauld

On 06/08/15 00:31, D Wyatt wrote:

I clearly remember that the standard for Python was to use 2 spaces


Nope, unless it was a local convention in your place of work.

But the standard for indentation has always(*) been 3 or 4 and fro PEP 8 
its 4. And thats pretty standard in any language because its the size of 
indent that gives best clarity to the code. (Based on tests of students 
reading code samples.)


(*) "Always" since the mid 1980s at least. Certainly
before the advent of Python.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Pep 8, about indentation

2015-08-05 Thread Steven D'Aprano
On Thu, Aug 06, 2015 at 02:24:49AM +0100, Alan Gauld wrote:
> On 06/08/15 00:31, D Wyatt wrote:
> >I clearly remember that the standard for Python was to use 2 spaces
> 
> Nope, unless it was a local convention in your place of work.
> 
> But the standard for indentation has always(*) been 3 or 4 and fro PEP 8 
> its 4. And thats pretty standard in any language because its the size of 
> indent that gives best clarity to the code. (Based on tests of students 
> reading code samples.)

Um, the oldest standard for TABs is *eight* spaces, not 3 or 4, and 
using eight-space indents is still very common. Vim, apparently, 
defaults to 8 space indents when you hit the tab key. Gofmt (used by the 
Go language) defaults to 8 spaces; even Python does in places:

expandtabs(...)
S.expandtabs([tabsize]) -> str

Return a copy of S where all tab characters are expanded using 
spaces. If tabsize is not given, a tab size of 8 characters is 
assumed.

The Linux kernel developers use 8 space indents, and consider that 
important enough that it is the very first item in their style guide:

https://www.kernel.org/doc/Documentation/CodingStyle

For interest, here's a survey done by Perl programmers in 2002:

http://www.perlmonks.org/?node_id=158886


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


Re: [Tutor] Dictionary Issue

2015-08-05 Thread Mark Lawrence

On 05/08/2015 23:58, Ltc Hotspot wrote:

Hi Mark,

Address identifies the email address with the maximum  number of sends:
c...@iupui.edu.

Secondly, we are missing a count on the number of messages sent by
c...@iupui.edu, i.e., 5.

Thirdly, maxval 'none' is not defined on line # 24

Questions: How do we define the value of none for the key maxval and
retrieve a number count on the number of messages sent by c...@iupui.edu.


NameError:

Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\assignment_9_4_5.py in ()
  22 ## find the greatest number of mail messages.
  23
---> 24 maxval = none
  25 maxkee = none
  26 for kee, val in count.items():

NameError: name 'none' is not defined

In [52]: print address
c...@iupui.edu

Revised data:


## The program looks for 'From ' lines and takes the second
## word of those lines as the person who sent the mail.

fname = raw_input("Enter file name: ")
handle = open (fname, 'r')
for line in handle:
 if line.startswith("From: "):
 address = line.split()[1]


## The program creates a Python dictionary that maps
## the sender's mail address to a count of the number
## of times they appear in the file.

 count = dict()
 for wrd in address:
 count[wrd]= count.get(wrd,0) +1

## After the dictionary is produced, the program reads
## through the dictionary using a maximum loop to
## find the greatest number of mail messages.

maxval = none
maxkee = none
for kee, val in count.items():
 if maxval == none or maxval 

You can greatly simplify all of the above code if you use a Counter from 
the collections module 
https://docs.python.org/3/library/collections.html#collections.Counter


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] scratching my head

2015-08-05 Thread Laura Creighton
In a message of Wed, 05 Aug 2015 08:43:45 +0200, Peter Otten writes:
>Laura Creighton wrote:
>but I don't think that's simpler. Can you enlighten me?

When I got here, I landed in the middle of a discussion on how to
use regexps for solving this.  Plus a slew of string handling
functions, none of which included endswith, which I think is a
fine idea as well.

The nice thing about fname is that it handles all the 'are your
file names case insensitive' stuff for you, which can be a problem.

Laura

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


Re: [Tutor] question / decision tree

2015-08-05 Thread Laura Creighton
In a message of Mon, 03 Aug 2015 10:38:40 +0100, matej taferner writes:
>Or maybe should I go with the tkinter?

You have to decide whether what you want is a Stand Alone GUI Application
(in which case tkinter could be a fine idea) or a web app.  It sounds
to me as if you want your customers to navigate to your site and then
fill out a questionaire.  If this is the case, then it's a web app
you want to write.

You generally write buttons and the like in web apps in javascript,
not python.  Then you connect them to whatever web framework you use.
Django, which you mentioned before, would work.  So would web2py and
there are others, many, many others see:
https://wiki.python.org/moin/WebFrameworks (which isn't completely up
to date -- I know there have been 2015 releases for most of these
Frameworks).

Which one you should use is a very personal decision.  There really
isn't anything to do but to try building a small app with every one
you are curious about and then see which you find most pleasant to
use.  People differ enormously on this issue, and indeed the very
things that make the people who love  think it is
wonderful are the things that other people cannot stand about it, and
are the reasons why they never use the thing.

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