[Tutor] __getitem__

2016-11-23 Thread monik...@netzero.net
Hi:
Can you please explain __getitem__? My understanding is that it brings back 
dictionary's value. Is this correct? If so which value does it bring? Does it 
look up this value by using a key? Where is this key specified in  " 
numbers.__getitem__"   ?

The below supposedly brings back dictionary's keys list  sorted by values. But 
how does it do it?

numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
sorted(numbers, key=numbers.__getitem__)

Thank you very much
Monika



3 Signs You May Have a Fatty Liver [Watch]
livecellresearch.com
http://thirdpartyoffers.netzero.net/TGL3241/583532f442b7c32f440b1st04duc
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] __getitem__ another problem

2016-11-23 Thread monik...@netzero.net
Hi:
Can you please explain what is going on below? I do not understand how 
numbermap.__getitem__ brings back month's key.
Does numbermap.__getitem__ bring back numbermap key or value? If key then it is 
not consistent with my understanding of problem in my previous email.  So month 
is sorted by numbermap values or keys?


month = dict(one='January',
 two='February',
 three='March',
 four='April',
 five='May')
numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
sorted(month, key=numbermap.__getitem__)
['one', 'two', 'three', 'four', 'five']
This is from:
http://pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/

Which Haircuts Look Exceptional on Older Women?
starsgossip.com
http://thirdpartyoffers.netzero.net/TGL3241/583536c537dff36c4036dst01duc
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getitem__

2016-11-23 Thread Alan Gauld via Tutor
On 23/11/16 06:09, monik...@netzero.net wrote:

> Can you please explain __getitem__? 

__getitem__ is the operator overload for indexing.
It is like the __add__() method which overloads the + operator.
So if you imple,ent __add__() in your class you can add two instances
together using + and Python calls your __add__() method behind the scenes.
In the same way when you index a collection object (with []) Python
calls the __getitem__ method behind the scenes. So for a dictionary
you can access the values of the dictionary by indexing it with a key:

d = {1:2,3:4}
n = d[1]   # calls d.__getitem__(1) resulting in n = 2

> My understanding is that it brings back dictionary's value. 
> Is this correct?

It brings back the value of the provided key.

> If so which value does it bring? 
> Does it look up this value by using a key?

Yes.

> Where is this key specified in  " numbers.__getitem__"   ?

Either by using indexing like numbers[somekey] or by
someone explicitly calling numbers.__getitem__(aKey)

In your example below sorted accesses the values
using the supplied function (which can be any arbitrary
function that accepts an argument and returns a value.)
By providing __getitem__ as the input function sorted
effectively uses the dictionary values.

> The below supposedly brings back dictionary's keys list  sorted by values. 
> But how does it do it?

By making the sort key the value of each dictionary key in turn

> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
> sorted(numbers, key=numbers.__getitem__)

If you try it at the prompt:

>>> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
>>> sorted(numbers, key=numbers.__getitem__)
['first', 'second', 'third', 'Fourth']

You see it works. Now try without getitem:

>>> sorted(numbers)
['Fourth', 'first', 'second', 'third']

This is sorted by the keys. Now lets use a different
sort method to get the values:

def getValue(aKey): return numbers[aKey]

This uses the more familiar indexing technique
to retrieve the value for a given key. We can
now use this function with sorted to get the
original result:

>>> sorted(numbers, key = getValue)
['first', 'second', 'third', 'Fourth']


And we can miss out the separate function definition
by using a lambda:

>>> sorted(numbers, key = lambda ky: numbers[ky])
['first', 'second', 'third', 'Fourth']
>>>

But the authors of your example have used __getitem__
directly because it's already available...(and the indexing
technique calls __getitem__ indirectly anyway).

HTH
-- 
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] __getitem__ another problem

2016-11-23 Thread Alan Gauld via Tutor
On 23/11/16 06:26, monik...@netzero.net wrote:

> I do not understand how numbermap.__getitem__ brings back month's key.

numbermap returns the integer corresponding to the key.
That number is then used by sorted as the basis for
sorting month. So for the first entry sorted receives
the value 1, for the second it gets 2. and so on.
It then prints the keys corresponding to those
values.

> month = dict(one='January',
>  two='February',
>  three='March',
>  four='April',
>  five='May')
> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
> sorted(month, key=numbermap.__getitem__)
> ['one', 'two', 'three', 'four', 'five']

-- 
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] Query regarding using coverage.py for sub-processes

2016-11-23 Thread anish p.k
Hi,
I had a query regarding the code coverage for pyhton subprocess.I was
referring the below link

https://coverage.readthedocs.io/en/coverage-4.2/subprocess.html
I created a site customize file
./usr/lib/python2.7/site-packages/sitecustomize.py

with the coed snippet

import coveragecoverage.process_startup()

i am wondering what is the value of COVERAGE_PROCESS_START which needs
to be set.

Please do let me know about the same as i am stuck with subprocess
coverage generation

Thanks and Regards

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


Re: [Tutor] __getitem__

2016-11-23 Thread monik...@netzero.net
Hi:
Thank you very much for your explanation. Just to confirm when using 
__getitem__ sort will go thru every key in dict and get its value and then sort 
according to it. Correct?

Also, you wrote:

">>> sorted(numbers, key = lambda ky: numbers[ky])  "

ky is input to lambda. Where does lambda get ky? what is ky value?
Thank you very much
Monika

-- Original Message --
From: Alan Gauld via Tutor 
To: tutor@python.org
Subject: Re: [Tutor] __getitem__
Date: Wed, 23 Nov 2016 10:05:50 +

On 23/11/16 06:09, monik...@netzero.net wrote:

> Can you please explain __getitem__? 

__getitem__ is the operator overload for indexing.
It is like the __add__() method which overloads the + operator.
So if you imple,ent __add__() in your class you can add two instances
together using + and Python calls your __add__() method behind the scenes.
In the same way when you index a collection object (with []) Python
calls the __getitem__ method behind the scenes. So for a dictionary
you can access the values of the dictionary by indexing it with a key:

d = {1:2,3:4}
n = d[1]   # calls d.__getitem__(1) resulting in n = 2

> My understanding is that it brings back dictionary's value. 
> Is this correct?

It brings back the value of the provided key.

> If so which value does it bring? 
> Does it look up this value by using a key?

Yes.

> Where is this key specified in  " numbers.__getitem__"   ?

Either by using indexing like numbers[somekey] or by
someone explicitly calling numbers.__getitem__(aKey)

In your example below sorted accesses the values
using the supplied function (which can be any arbitrary
function that accepts an argument and returns a value.)
By providing __getitem__ as the input function sorted
effectively uses the dictionary values.

> The below supposedly brings back dictionary's keys list  sorted by values. 
> But how does it do it?

By making the sort key the value of each dictionary key in turn

> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
> sorted(numbers, key=numbers.__getitem__)

If you try it at the prompt:

>>> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
>>> sorted(numbers, key=numbers.__getitem__)
['first', 'second', 'third', 'Fourth']

You see it works. Now try without getitem:

>>> sorted(numbers)
['Fourth', 'first', 'second', 'third']

This is sorted by the keys. Now lets use a different
sort method to get the values:

def getValue(aKey): return numbers[aKey]

This uses the more familiar indexing technique
to retrieve the value for a given key. We can
now use this function with sorted to get the
original result:

>>> sorted(numbers, key = getValue)
['first', 'second', 'third', 'Fourth']


And we can miss out the separate function definition
by using a lambda:

>>> sorted(numbers, key = lambda ky: numbers[ky])
['first', 'second', 'third', 'Fourth']
>>>

But the authors of your example have used __getitem__
directly because it's already available...(and the indexing
technique calls __getitem__ indirectly anyway).

HTH
-- 
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



Affordable Wireless Plans
Set up is easy. Get online in minutes.
Starting at only $14.95 per month! 
www.netzero.net?refcd=nzmem0216
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getitem__

2016-11-23 Thread monik...@netzero.net

Hi:
I have two questions in regards to below code:
1. largest is a list, not a list of lists. 
[('deit', 4), ('acer', 3), ('aceilmr', 2), ('arst', 2)]
so why when I do largest[0] I get the whole list again, not just the first item 
from the list. To get the first item I have to do largest[0][0].

2. largest = [sorted(analist, key=lambda analist: analist[1], reverse=True)]
brings back the same result as:
largest = [sorted(analist, key=lambda d: d[1], reverse=True)]
and the same result as:
largest = [sorted(analist, key=lambda x: x[1], reverse=True)]
The result is:
[('deit', 4), ('acer', 3), ('aceilmr', 2), ('arst', 2)]

I really do not understand why and how this works. Could you please explain?
in lambda x: x[1] I pass x to lambda and it does calculation of x[1] but where 
does it get the x, what is the value of x? 
why lambda x: x[1] brings the same result as lambda d: d[1] and lambda analist: 
analist[1]

#question. have a list of words. check for anagrams
#count how many anagrams there are.
#do it thru dictionary
#then get anagaram list which has the biggest amount of words
words = ["miracle", "claimer", "care", "race", "arts", "rats", "acre","diet", 
"edit", "tide", "tied"]
def anagram(words):
d = {}
for word in words:
wordl = list(word)
print "word as list:  " , wordl
wordlsorted = sorted(wordl)
print "word lsit sorted:  " , wordlsorted
wordsorted = ''.join(wordlsorted)
print "word sorted as string:   ", wordsorted
d[wordsorted] = d.get(wordsorted, []) + [word]

print d
analist = [(key , len(value)) for key, value in d.items()]
print analist
print "largest:  ", [sorted(analist, key=lambda analist: analist[1], 
reverse=True)][0]
largest = [sorted(analist, key=lambda analist: analist[1], reverse=True)]
print type(largest)
print largest[0][0]

anagram(words)

Thank you very much in advance for explaining this.
Monika

Eat This Junk Food To "Reverse" Dementia
Nutrition and Healing
http://thirdpartyoffers.netzero.net/TGL3241/58358b1017902b0f1d93st02duc
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getitem__ another problem

2016-11-23 Thread monik...@netzero.net
So numbermap.__getitem__ brings back 1, then 2,then 3, then 4. 
Then it looks up 1 ,2, 3, 4 in month but there is no  key with value 1, 2, or 
or in 4. 
What am I missing?
Thank you very much
Monika

-- Original Message --
From: Alan Gauld via Tutor 
To: tutor@python.org
Subject: Re: [Tutor] __getitem__ another problem
Date: Wed, 23 Nov 2016 10:09:46 +

On 23/11/16 06:26, monik...@netzero.net wrote:

> I do not understand how numbermap.__getitem__ brings back month's key.

numbermap returns the integer corresponding to the key.
That number is then used by sorted as the basis for
sorting month. So for the first entry sorted receives
the value 1, for the second it gets 2. and so on.
It then prints the keys corresponding to those
values.

> month = dict(one='January',
>  two='February',
>  three='March',
>  four='April',
>  five='May')
> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
> sorted(month, key=numbermap.__getitem__)
> ['one', 'two', 'three', 'four', 'five']

-- 
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


7-Time Lotto Winner Reveals The Truth How To Win Any Lottery
MNT
http://thirdpartyoffers.netzero.net/TGL3241/58358cba68d69cba49c6st04duc
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __getitem__ another problem

2016-11-23 Thread Alan Gauld via Tutor
On 23/11/16 12:33, monik...@netzero.net wrote:
> So numbermap.__getitem__ brings back 1, then 2,then 3, then 4. 
> Then it looks up 1 ,2, 3, 4 in month but there is no  key with value 1, 2, or 
> or in 4. 
> What am I missing?

Your problem is not with getitem but with sorted.
You need to read up on how sorted uses the key parameter.

It basiocally iterates over the collection to be sorted applying the key
function to each item in the collection in turn.

It then sorts the collection based on the results. You can think of it
as turning a collection of values like

[v1,v2,v3]

into a collection of pairs where the  second item
of each pair is the result of applying the key function
to the value, like this:

[(v1,key(v1)),(v2,key(v2)),(v3,key(v3))]

And then sorting based on the second value of the pair.
Finally it returns the first value of the sorted
collection. Lets take an example where we define
a key function called mag() for magnitude:

def mag(n):
   return n if n>=0 else -n

numbers = [1,-5,2,-4]

Now if we apply sorted(numbers,mag)

sorted iterates over numbers calling mag(n) for
each number to get:

[(1,1),(-5,5),(2,2),(-4,4)]

And then sorts that list based on the second
value of each pair:

[(1,1),(2,2),(-4,4),(-5,5)]

And finally returns the first values of that sorted list:

[1,2,-4,-5]


Now your situation with a dictionary is slightly
more complex because of the added mapping between
keys and values. But for a dict it works the same
except that the key function is passed the
dictionary key each time, but the basic idea is
identical.

Does that help?

-- 
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] __getitem__

2016-11-23 Thread Alan Gauld via Tutor
On 23/11/16 12:25, monik...@netzero.net wrote:

> I have two questions in regards to below code:
> 1. largest is a list, not a list of lists. 
> [('deit', 4), ('acer', 3), ('aceilmr', 2), ('arst', 2)]
> so why when I do largest[0] I get the whole list again, 

I don't know you will need to show us some real code.
Ideally input at the >>> prompt.

> 2. largest = [sorted(analist, key=lambda analist: analist[1], reverse=True)]
> brings back the same result as:
> largest = [sorted(analist, key=lambda d: d[1], reverse=True)]
> and the same result as:
> largest = [sorted(analist, key=lambda x: x[1], reverse=True)]

Yes because it doesn't matter what you call the parameter of the lambda,
it's like any other function:

def add2(x): return x+2

def add2(y): return y+2

def add2(z): return z+2

All of these functions are identical they always do the same
regardless of what you call the parameter. Remember a lambda
is just a shortcut for a function

key = lambda d: d[1]

is identical to

def key(d): return d[1]

and

key = lambda analist: analist[1]

is identical to

def key(analist): return analist[1]

Just like the add2() examples it doesn't matter what
name you use for the parameter.

> ...but where does it get the x, what is the value of x? 

See my other post about how sorted() works.


-- 
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