Re: How to show a dictionary sorted on a value within its data?

2014-10-03 Thread Chris Angelico
On Fri, Oct 3, 2014 at 5:55 PM, Steven D'Aprano  wrote:
>> which return non-list iterables. There's no really convenient equivalent
>> to the map() usage of "call this function with each of these args, and
>> discard the return values", as it looks very odd to do a list comp for
>> nothing:
>
> map doesn't discard the return values. It accumulates them in a list, or
> yields them in a list comp.

Sure, but I've seen it used in various places to call functions where
you don't care about the return values. Technically it's identical to
the list comp, but somehow it's seen as less of an abuse.

>> Short of borrowing Pike's automap syntax:
>>
>> print(list_of_strings[*])
>
> you mean something like this perhaps?
>
> print(*list_of_strings, sep='\n')

Well, no. In that one specific example, it happens to be possible to
pass all the args to one print() call and get the same effect, but
imagine some function that doesn't have that particular feature. (I
just used print for simplicity.)

>> there's really not much that's better than the original map() call. On
>> the other hand, there aren't actually all that many times when I've
>> needed to do this, and the simple for loop generally suffices:
>>
>> for x in list_of_strings: print(x)
>
> Exactly.

Yeah. That's the normal way to do things. I'm not entirely sure why I
keep seeing map() for that; there's probably a justification in
functional programming languages, or something.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-03 Thread Steven D'Aprano
On Fri, 03 Oct 2014 12:44:32 +1000, Chris Angelico wrote:

> On Fri, Oct 3, 2014 at 9:16 AM, Steven D'Aprano
>  wrote:
>>> Anyway, pylint doesn't complain about a bare use of lambda, but it
>>> does complain about a map applied to a lambda or a filter applied to a
>>> lambda.  Pylint says they could be replaced by a list comprehension,
>>> with the warning "deprecated-lambda".
>>
>> The warning name is misleading, lambda is not deprecated. But
>> stylistically, many people prefer to use a generator expression in
>> place of map or filter:
>>
>> # instead of this
>> map(lambda x: 2*x+1, values)
>>
>> # use this
>> (2*x+1 for x in values)
>>
>> # instead of this
>> filter(lambda x: x > 23, values)
>>
>> # use this
>> (x for x in values if x > 23)
> 
> Don't forget that your equivalencies are based on the Py3 map/filter,

Dan did ask about Python 3.x :-)


> which return non-list iterables. There's no really convenient equivalent
> to the map() usage of "call this function with each of these args, and
> discard the return values", as it looks very odd to do a list comp for
> nothing:

map doesn't discard the return values. It accumulates them in a list, or 
yields them in a list comp.

> [print(x) for x in list_of_strings]

That would be an abuse of a list comprehension, generator expression or 
map since it will produce a whole lot of None values. If you really want 
to discard the return values, put them in a for-loop:

for s in list_of_strings:
print(s)

> Short of borrowing Pike's automap syntax:
> 
> print(list_of_strings[*])


you mean something like this perhaps?

print(*list_of_strings, sep='\n')


> there's really not much that's better than the original map() call. On
> the other hand, there aren't actually all that many times when I've
> needed to do this, and the simple for loop generally suffices:
> 
> for x in list_of_strings: print(x)

Exactly.


-- 
Steven
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Chris Angelico
On Fri, Oct 3, 2014 at 9:16 AM, Steven D'Aprano
 wrote:
>> Anyway, pylint doesn't complain about a bare use of lambda, but it
>> does complain about a map applied to a lambda or a filter applied to a
>> lambda.  Pylint says they could be replaced by a list comprehension,
>> with the warning "deprecated-lambda".
>
> The warning name is misleading, lambda is not deprecated. But stylistically,
> many people prefer to use a generator expression in place of map or filter:
>
> # instead of this
> map(lambda x: 2*x+1, values)
>
> # use this
> (2*x+1 for x in values)
>
> # instead of this
> filter(lambda x: x > 23, values)
>
> # use this
> (x for x in values if x > 23)

Don't forget that your equivalencies are based on the Py3 map/filter,
which return non-list iterables. There's no really convenient
equivalent to the map() usage of "call this function with each of
these args, and discard the return values", as it looks very odd to do
a list comp for nothing:

[print(x) for x in list_of_strings]

Short of borrowing Pike's automap syntax:

print(list_of_strings[*])

there's really not much that's better than the original map() call. On
the other hand, there aren't actually all that many times when I've
needed to do this, and the simple for loop generally suffices:

for x in list_of_strings: print(x)

So it's not a huge deal.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Steven D'Aprano
Dan Stromberg wrote:

> On Thu, Oct 2, 2014 at 12:15 PM, Ian Kelly  wrote:
>> On Thu, Oct 2, 2014 at 10:33 AM,   wrote:
>>> Ah, so at least there is a reason for it, I'm far from being a
>>> mathematician though so it's not particularly obvious (for me anyway).
>>
>> You're not alone; a lot of people find the terminology not intuitive.
>> Even GvR has publicly lamented the choice of keyword.
> 
> Was lambda almost removed from CPython 3.x?

I'm not sure about "almost", but there was serious discussion about removing
it.


> Anyway, pylint doesn't complain about a bare use of lambda, but it
> does complain about a map applied to a lambda or a filter applied to a
> lambda.  Pylint says they could be replaced by a list comprehension,
> with the warning "deprecated-lambda".

The warning name is misleading, lambda is not deprecated. But stylistically,
many people prefer to use a generator expression in place of map or filter:

# instead of this
map(lambda x: 2*x+1, values)

# use this
(2*x+1 for x in values)

# instead of this
filter(lambda x: x > 23, values)

# use this
(x for x in values if x > 23)



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Chris Angelico
On Fri, Oct 3, 2014 at 6:06 AM, Dan Stromberg  wrote:
> Anyway, pylint doesn't complain about a bare use of lambda, but it
> does complain about a map applied to a lambda or a filter applied to a
> lambda.  Pylint says they could be replaced by a list comprehension,
> with the warning "deprecated-lambda".

That's not because lambda is a poor choice, but because map() in Py2
code can often be replaced with a list comp. (And map() in Py3 code
can often be replaced by a genexp.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Dan Stromberg
On Thu, Oct 2, 2014 at 12:15 PM, Ian Kelly  wrote:
> On Thu, Oct 2, 2014 at 10:33 AM,   wrote:
>> Ah, so at least there is a reason for it, I'm far from being a
>> mathematician though so it's not particularly obvious (for me anyway).
>
> You're not alone; a lot of people find the terminology not intuitive.
> Even GvR has publicly lamented the choice of keyword.

Was lambda almost removed from CPython 3.x?

Anyway, pylint doesn't complain about a bare use of lambda, but it
does complain about a map applied to a lambda or a filter applied to a
lambda.  Pylint says they could be replaced by a list comprehension,
with the warning "deprecated-lambda".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Grant Edwards
On 2014-10-02, c...@isbd.net  wrote:
> Grant Edwards  wrote:
>> On 2014-10-02, c...@isbd.net  wrote:
>>
>> > It throws me because 'lambda' simply has no meaning whatsoever for me,
>> > i.e. it's just a greek letter.
>> >
>> > So from my point of view it's like seeing 'epsilon' stuck in the
>> > middle of some code. 
>> >
>> > It's not as if I'm new to programming either, I've been writing
>> > software professionally since the early 1970s, now retired.
>> 
>> The use of "lamba" as a keyword to define an anonymous function is
>> borrowed from Lisp which got it from Lambda calculus.
>> 
>> http://en.wikipedia.org/wiki/Lambda_calculus
>
> Ah, so at least there is a reason for it, I'm far from being a
> mathematician though so it's not particularly obvious (for me
> anyway).

We EE types did get complex number notation using "j" which the
mathematicians find not particularly obvious.

-- 
Grant Edwards   grant.b.edwardsYow! It's a hole all the
  at   way to downtown Burbank!
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Ian Kelly
On Thu, Oct 2, 2014 at 10:33 AM,   wrote:
> Ah, so at least there is a reason for it, I'm far from being a
> mathematician though so it's not particularly obvious (for me anyway).

You're not alone; a lot of people find the terminology not intuitive.
Even GvR has publicly lamented the choice of keyword.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Rustom Mody
On Thursday, October 2, 2014 4:47:50 PM UTC+5:30, Marko Rauhamaa wrote:

> > It's not as if I'm new to programming either, I've been writing
> > software professionally since the early 1970s, now retired. I have no
> > formal computer training, there wasn't much in the way of university
> > courses on computing in the 1960s, I have a degree in Electrical
> > Engineering. Maybe that's why 'lambda' means nothing to me! :-)

> Lambda is special because it was thought of in the 1920's, well before
> there were any general-purpose computers.

> http://www.users.waitrose.com/~hindley/SomePapers_PDFs/2006CarH

If you find that heavy going, here's an intro lecture on λ-calculus
-- part of lectures on discrete math I've been preparing

https://www.youtube.com/watch?v=FZoq-GlOdSs&feature=youtu.be

Comments/criticisms welcome though do remember I am as new to
film-making today as I was to programming 30 years ago :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread cl
Grant Edwards  wrote:
> On 2014-10-02, c...@isbd.net  wrote:
> > Travis Griggs  wrote:
> >> 
> >> 
> >> Sent from my iPhone
> >> 
> >> > On Oct 1, 2014, at 04:12, Peter Otten <__pete...@web.de> wrote:
> >> > 
> >> > `lambda` is just a fancy way to define a function inline
> >> 
> >> Not sure "fancy" is the correct adjective; more like syntactic tartness 
> >> (a less sweet version of syntactic sugar). 
> >> 
> > It throws me because 'lambda' simply has no meaning whatsoever for me,
> > i.e. it's just a greek letter.
> >
> > So from my point of view it's like seeing 'epsilon' stuck in the
> > middle of some code. 
> >
> > It's not as if I'm new to programming either, I've been writing
> > software professionally since the early 1970s, now retired.
> 
> The use of "lamba" as a keyword to define an anonymous function is
> borrowed from Lisp which got it from Lambda calculus.
> 
> http://en.wikipedia.org/wiki/Lambda_calculus
> 
Ah, so at least there is a reason for it, I'm far from being a
mathematician though so it's not particularly obvious (for me anyway).

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Grant Edwards
On 2014-10-02, c...@isbd.net  wrote:
> Travis Griggs  wrote:
>> 
>> 
>> Sent from my iPhone
>> 
>> > On Oct 1, 2014, at 04:12, Peter Otten <__pete...@web.de> wrote:
>> > 
>> > `lambda` is just a fancy way to define a function inline
>> 
>> Not sure "fancy" is the correct adjective; more like syntactic tartness 
>> (a less sweet version of syntactic sugar). 
>> 
> It throws me because 'lambda' simply has no meaning whatsoever for me,
> i.e. it's just a greek letter.
>
> So from my point of view it's like seeing 'epsilon' stuck in the
> middle of some code. 
>
> It's not as if I'm new to programming either, I've been writing
> software professionally since the early 1970s, now retired.

The use of "lamba" as a keyword to define an anonymous function is
borrowed from Lisp which got it from Lambda calculus.

http://en.wikipedia.org/wiki/Lambda_calculus

-- 
Grant Edwards   grant.b.edwardsYow! Is this going to
  at   involve RAW human ecstasy?
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread Marko Rauhamaa
c...@isbd.net:

> It's not as if I'm new to programming either, I've been writing
> software professionally since the early 1970s, now retired. I have no
> formal computer training, there wasn't much in the way of university
> courses on computing in the 1960s, I have a degree in Electrical
> Engineering. Maybe that's why 'lambda' means nothing to me! :-)

Lambda is special because it was thought of in the 1920's, well before
there were any general-purpose computers.

http://www.users.waitrose.com/~hindley/SomePapers_PDFs/2006CarH
in,HistlamRp.pdf>

See Chapter 4.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-02 Thread cl
Travis Griggs  wrote:
> 
> 
> Sent from my iPhone
> 
> > On Oct 1, 2014, at 04:12, Peter Otten <__pete...@web.de> wrote:
> > 
> > `lambda` is just a fancy way to define a function inline
> 
> Not sure "fancy" is the correct adjective; more like syntactic tartness 
> (a less sweet version of syntactic sugar). 
> 
It throws me because 'lambda' simply has no meaning whatsoever for me,
i.e. it's just a greek letter.

So from my point of view it's like seeing 'epsilon' stuck in the
middle of some code. 

It's not as if I'm new to programming either, I've been writing
software professionally since the early 1970s, now retired.  I have no
formal computer training, there wasn't much in the way of university
courses on computing in the 1960s, I have a degree in Electrical
Engineering.  Maybe that's why 'lambda' means nothing to me!  :-)

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Travis Griggs


Sent from my iPhone

> On Oct 1, 2014, at 04:12, Peter Otten <__pete...@web.de> wrote:
> 
> `lambda` is just a fancy way to define a function inline

Not sure "fancy" is the correct adjective; more like syntactic tartness (a less 
sweet version of syntactic sugar).

:)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread cl
Joel Goldstick  wrote:
> On Wed, Oct 1, 2014 at 5:58 AM,   wrote:
> > I have a dictionary as follows:-
> >
> > {
> > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1', 
> > conv=6834.374834509803, 
> Description=u'Starter Amps'), 
> > u'LeisureVolts': Row(id=1, ain=u'AIN0', name=u'LeisureVolts', 
> > conv=29.01374215995874, 
> Description=u'Leisure Volts'), 
> > u'RudderPos': Row(id=6, ain=u'AIN5', name=u'RudderPos', conv=0.028125, 
> Description=u'Rudder Position'), 
> > u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
> > u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1', 
> > conv=32.727273081945, 
> Description=u'Leisure Amps'), 
> > u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts', 
> > conv=28.94469628911757, 
> Description=u'Starter Volts') 
> > }
> 
> Is Row a function?  Or do you have a key with a tuple as the value?

Row is a namedtuple, the dictionary is extracted from a database with
a row factory so the namedtuple names are the column names.  It means
that if I change a column name then most things continue to work
without any code changes, and also I can refer to items in the tuple
by name of course.

I have the answer to my question in other responses.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread cl
Joel Goldstick  wrote:
> On Wed, Oct 1, 2014 at 6:45 AM,   wrote:
> > Peter Otten <__pete...@web.de> wrote:
> >> c...@isbd.net wrote:
> >>
> >> > I have a dictionary as follows:-
> >> >
> >> > {
> >> > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1',
> >> > conv=6834.374834509803, Description=u'Starter Amps'), u'LeisureVolts':
> >> > Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874,
> >> > Description=u'Leisure Volts'), u'RudderPos': Row(id=6, ain=u'AIN5',
> >> > name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'), u'xx':
> >> > Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
> >> > u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1',
> >> > conv=32.727273081945, Description=u'Leisure Amps'), u'StarterVolts':
> >> > Row(id=2, ain=u'AIN1', name=u'StarterVolts', conv=28.94469628911757,
> >> > Description=u'Starter Volts') }
> >> >
> >> > I want to output a menu to a user comprising some parts of the
> >> > dictionary (ain and Description) sorted by ain.
> >> >
> >> > Is there some incantation of sorted() that will do what I want?  I
> >> > can't quite fathom out the 'key=' parameter needed to sort it by the
> >> > tuple item.  Maybe I need a cmp= ?
> >> >
> >> > E.g. I want to do something like:-
> >> >
> >> > for meas in sorted(adc.cfg, key=???):
> >> > print(adc.cfg[meas].ain, adc.cfg[meas].Description)
> >> >
> >> > What's needed in the ???
> >>
> >> for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain):
> >> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
> >>
> > Brilliant, worked perfectly, thank you.  The bit I didn't understamd was
> > that 'lambda' bit, but I just looked it up and I'm a bit clearer now.
> >
> >> or simpler
> >>
> >> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain"))
> >> print(row.ain, row.Description)
> >>
> > I tried this, I got:-
> >
> > Traceback (most recent call last):
> >   File "/home/chris/bin/calibrate.py", line 24, in 
> >   for meas in sorted(adc.cfg.values,
> >   key=operator.attrgetter("ain")):
> >   NameError: name 'operator' is not defined
> > I must admit that it's the bits like 'operator' in the parameters that
> > I can't really understand where they come from.
> >
> 
> operator is a module. See
> https://docs.python.org/2/library/operator.html#module-operator

Ah, OK, as it sort of might be a keyword I hadn't thought of that. :-)

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Peter Otten
c...@isbd.net wrote:

> Peter Otten <__pete...@web.de> wrote:
>> c...@isbd.net wrote:
>> 
>> > I have a dictionary as follows:-
>> > 
>> > {
>> > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1',
>> > conv=6834.374834509803, Description=u'Starter Amps'), u'LeisureVolts':
>> > Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874,
>> > Description=u'Leisure Volts'), u'RudderPos': Row(id=6, ain=u'AIN5',
>> > name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'),
>> > u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125,
>> > Description=u''), u'LeisureAmps1': Row(id=3, ain=u'AIN2',
>> > name=u'LeisureAmps1', conv=32.727273081945, Description=u'Leisure
>> > Amps'), u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts',
>> > conv=28.94469628911757, Description=u'Starter Volts') }
>> > 
>> > I want to output a menu to a user comprising some parts of the
>> > dictionary (ain and Description) sorted by ain.
>> > 
>> > Is there some incantation of sorted() that will do what I want?  I
>> > can't quite fathom out the 'key=' parameter needed to sort it by the
>> > tuple item.  Maybe I need a cmp= ?
>> > 
>> > E.g. I want to do something like:-
>> > 
>> > for meas in sorted(adc.cfg, key=???):
>> > print(adc.cfg[meas].ain, adc.cfg[meas].Description)
>> > 
>> > What's needed in the ???
>> 
>> for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain):
>> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
>> 
> Brilliant, worked perfectly, thank you.  The bit I didn't understamd was
> that 'lambda' bit, but I just looked it up and I'm a bit clearer now.

`lambda` is just a fancy way to define a function inline. With a normal 
`def` it would be

def get_ain(key):
return adc.cfg[key].ain

or meas in sorted(adc.cfg, key=get_ain):
   print(adc.cfg[meas].ain, adc.cfg[meas].Description)

> 
>> or simpler
>> 
>> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain"))
>> print(row.ain, row.Description)
>> 
> I tried this, I got:-
> 
> Traceback (most recent call last):
>   File "/home/chris/bin/calibrate.py", line 24, in 
>   for meas in sorted(adc.cfg.values,
>   key=operator.attrgetter("ain")):
>   NameError: name 'operator' is not defined
> I must admit that it's the bits like 'operator' in the parameters that
> I can't really understand where they come from.

As Joel says, operator is a module. To use it add

import operator

at the beginning of your module. To check if you've really understood the 
lambda you can try to replace the operator.attrgetter("ain") in

>> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain")):

with 

for row in sorted(adc.cfg.values(), key=lambda ...):

>> or even
>> 
>> for row in sorted(
>> map(operator.attrgetter("ain", "Description"),
>> adc.cfg.values())):
>> print(*row)


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Joel Goldstick
On Wed, Oct 1, 2014 at 6:45 AM,   wrote:
> Peter Otten <__pete...@web.de> wrote:
>> c...@isbd.net wrote:
>>
>> > I have a dictionary as follows:-
>> >
>> > {
>> > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1',
>> > conv=6834.374834509803, Description=u'Starter Amps'), u'LeisureVolts':
>> > Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874,
>> > Description=u'Leisure Volts'), u'RudderPos': Row(id=6, ain=u'AIN5',
>> > name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'), u'xx':
>> > Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
>> > u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1',
>> > conv=32.727273081945, Description=u'Leisure Amps'), u'StarterVolts':
>> > Row(id=2, ain=u'AIN1', name=u'StarterVolts', conv=28.94469628911757,
>> > Description=u'Starter Volts') }
>> >
>> > I want to output a menu to a user comprising some parts of the
>> > dictionary (ain and Description) sorted by ain.
>> >
>> > Is there some incantation of sorted() that will do what I want?  I
>> > can't quite fathom out the 'key=' parameter needed to sort it by the
>> > tuple item.  Maybe I need a cmp= ?
>> >
>> > E.g. I want to do something like:-
>> >
>> > for meas in sorted(adc.cfg, key=???):
>> > print(adc.cfg[meas].ain, adc.cfg[meas].Description)
>> >
>> > What's needed in the ???
>>
>> for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain):
>> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
>>
> Brilliant, worked perfectly, thank you.  The bit I didn't understamd was
> that 'lambda' bit, but I just looked it up and I'm a bit clearer now.
>
>> or simpler
>>
>> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain"))
>> print(row.ain, row.Description)
>>
> I tried this, I got:-
>
> Traceback (most recent call last):
>   File "/home/chris/bin/calibrate.py", line 24, in 
>   for meas in sorted(adc.cfg.values,
>   key=operator.attrgetter("ain")):
>   NameError: name 'operator' is not defined
> I must admit that it's the bits like 'operator' in the parameters that
> I can't really understand where they come from.
>

operator is a module. See
https://docs.python.org/2/library/operator.html#module-operator
>
>> or even
>>
>> for row in sorted(
>> map(operator.attrgetter("ain", "Description"), adc.cfg.values())):
>> print(*row)
>>
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread cl
Peter Otten <__pete...@web.de> wrote:
> c...@isbd.net wrote:
> 
> > I have a dictionary as follows:-
> > 
> > {
> > u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1',
> > conv=6834.374834509803, Description=u'Starter Amps'), u'LeisureVolts':
> > Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874,
> > Description=u'Leisure Volts'), u'RudderPos': Row(id=6, ain=u'AIN5',
> > name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'), u'xx':
> > Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
> > u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1',
> > conv=32.727273081945, Description=u'Leisure Amps'), u'StarterVolts':
> > Row(id=2, ain=u'AIN1', name=u'StarterVolts', conv=28.94469628911757,
> > Description=u'Starter Volts') }
> > 
> > I want to output a menu to a user comprising some parts of the
> > dictionary (ain and Description) sorted by ain.
> > 
> > Is there some incantation of sorted() that will do what I want?  I
> > can't quite fathom out the 'key=' parameter needed to sort it by the
> > tuple item.  Maybe I need a cmp= ?
> > 
> > E.g. I want to do something like:-
> > 
> > for meas in sorted(adc.cfg, key=???):
> > print(adc.cfg[meas].ain, adc.cfg[meas].Description)
> > 
> > What's needed in the ???
> 
> for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain):
> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
> 
Brilliant, worked perfectly, thank you.  The bit I didn't understamd was
that 'lambda' bit, but I just looked it up and I'm a bit clearer now.

> or simpler
> 
> for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain"))
> print(row.ain, row.Description)
> 
I tried this, I got:-

Traceback (most recent call last):
  File "/home/chris/bin/calibrate.py", line 24, in 
  for meas in sorted(adc.cfg.values,
  key=operator.attrgetter("ain")):
  NameError: name 'operator' is not defined
I must admit that it's the bits like 'operator' in the parameters that
I can't really understand where they come from.


> or even
> 
> for row in sorted(
> map(operator.attrgetter("ain", "Description"), adc.cfg.values())):
> print(*row)
> 

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Joel Goldstick
On Wed, Oct 1, 2014 at 5:58 AM,   wrote:
> I have a dictionary as follows:-
>
> {
> u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1', 
> conv=6834.374834509803, Description=u'Starter Amps'),
> u'LeisureVolts': Row(id=1, ain=u'AIN0', name=u'LeisureVolts', 
> conv=29.01374215995874, Description=u'Leisure Volts'),
> u'RudderPos': Row(id=6, ain=u'AIN5', name=u'RudderPos', conv=0.028125, 
> Description=u'Rudder Position'),
> u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
> u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1', 
> conv=32.727273081945, Description=u'Leisure Amps'),
> u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts', 
> conv=28.94469628911757, Description=u'Starter Volts')
> }

Is Row a function?  Or do you have a key with a tuple as the value?
>
> I want to output a menu to a user comprising some parts of the
> dictionary (ain and Description) sorted by ain.
>
> Is there some incantation of sorted() that will do what I want?  I
> can't quite fathom out the 'key=' parameter needed to sort it by the
> tuple item.  Maybe I need a cmp= ?
>
> E.g. I want to do something like:-
>
> for meas in sorted(adc.cfg, key=???):
> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
>
> What's needed in the ???
>
> --
> Chris Green
> ·

You might read here:
http://www.pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Mark Lawrence

On 01/10/2014 10:58, c...@isbd.net wrote:

I have a dictionary as follows:-

{
u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1', 
conv=6834.374834509803, Description=u'Starter Amps'),
u'LeisureVolts': Row(id=1, ain=u'AIN0', name=u'LeisureVolts', 
conv=29.01374215995874, Description=u'Leisure Volts'),
u'RudderPos': Row(id=6, ain=u'AIN5', name=u'RudderPos', conv=0.028125, 
Description=u'Rudder Position'),
u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1', 
conv=32.727273081945, Description=u'Leisure Amps'),
u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts', 
conv=28.94469628911757, Description=u'Starter Volts')
}

I want to output a menu to a user comprising some parts of the
dictionary (ain and Description) sorted by ain.

Is there some incantation of sorted() that will do what I want?  I
can't quite fathom out the 'key=' parameter needed to sort it by the
tuple item.  Maybe I need a cmp= ?

E.g. I want to do something like:-

 for meas in sorted(adc.cfg, key=???):
 print(adc.cfg[meas].ain, adc.cfg[meas].Description)

What's needed in the ???



IIRC one method involves using itemgetter from 
https://docs.python.org/3/library/operator.html#module-operator


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

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: How to show a dictionary sorted on a value within its data?

2014-10-01 Thread Peter Otten
c...@isbd.net wrote:

> I have a dictionary as follows:-
> 
> {
> u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1',
> conv=6834.374834509803, Description=u'Starter Amps'), u'LeisureVolts':
> Row(id=1, ain=u'AIN0', name=u'LeisureVolts', conv=29.01374215995874,
> Description=u'Leisure Volts'), u'RudderPos': Row(id=6, ain=u'AIN5',
> name=u'RudderPos', conv=0.028125, Description=u'Rudder Position'), u'xx':
> Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
> u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1',
> conv=32.727273081945, Description=u'Leisure Amps'), u'StarterVolts':
> Row(id=2, ain=u'AIN1', name=u'StarterVolts', conv=28.94469628911757,
> Description=u'Starter Volts') }
> 
> I want to output a menu to a user comprising some parts of the
> dictionary (ain and Description) sorted by ain.
> 
> Is there some incantation of sorted() that will do what I want?  I
> can't quite fathom out the 'key=' parameter needed to sort it by the
> tuple item.  Maybe I need a cmp= ?
> 
> E.g. I want to do something like:-
> 
> for meas in sorted(adc.cfg, key=???):
> print(adc.cfg[meas].ain, adc.cfg[meas].Description)
> 
> What's needed in the ???

for meas in sorted(adc.cfg, key=lambda key: adc.cfg[key].ain):
print(adc.cfg[meas].ain, adc.cfg[meas].Description)

or simpler

for row in sorted(adc.cfg.values(), key=operator.attrgetter("ain"))
print(row.ain, row.Description)

or even

for row in sorted(
map(operator.attrgetter("ain", "Description"), adc.cfg.values())):
print(*row)

-- 
https://mail.python.org/mailman/listinfo/python-list


How to show a dictionary sorted on a value within its data?

2014-10-01 Thread cl
I have a dictionary as follows:-

{
u'StarterAmps1': Row(id=4, ain=u'AIN3', name=u'StarterAmps1', 
conv=6834.374834509803, Description=u'Starter Amps'), 
u'LeisureVolts': Row(id=1, ain=u'AIN0', name=u'LeisureVolts', 
conv=29.01374215995874, Description=u'Leisure Volts'),
u'RudderPos': Row(id=6, ain=u'AIN5', name=u'RudderPos', conv=0.028125, 
Description=u'Rudder Position'),
u'xx': Row(id=7, ain=u'AIN6', name=u'xx', conv=0.028125, Description=u''),
u'LeisureAmps1': Row(id=3, ain=u'AIN2', name=u'LeisureAmps1', 
conv=32.727273081945, Description=u'Leisure Amps'),
u'StarterVolts': Row(id=2, ain=u'AIN1', name=u'StarterVolts', 
conv=28.94469628911757, Description=u'Starter Volts')
}

I want to output a menu to a user comprising some parts of the
dictionary (ain and Description) sorted by ain.

Is there some incantation of sorted() that will do what I want?  I
can't quite fathom out the 'key=' parameter needed to sort it by the
tuple item.  Maybe I need a cmp= ?

E.g. I want to do something like:-

for meas in sorted(adc.cfg, key=???):
print(adc.cfg[meas].ain, adc.cfg[meas].Description)

What's needed in the ???

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list