Python Events in 2017, Need your help.

2017-01-09 Thread Stephane Wirtel via Python-list

Dear Community,

For the PythonFOSDEM [1] on 4th and 5th February in Belgium, I would 
like to present some slides with the Python events around the World.  
Based on https://python.org/events, I have noted that there are missing 
events, for example:


* PyCon Otto: Italy
* PyCon UK: United Kingdom
* PyCon CA: Canada
* PyCon Ireland: Ireland
* PyCon France: France

Some of these events are not yet announced and I understand they are in 
the second semester, and thus, they don't know the location and the 
dates, excepted for PyCon Otto (April).


In fact, I have noted that we know some big events in the Python 
community (for example: PyCon US and EuroPython) but do you know the 
others events, maybe the local event, PyCon IE, PyCon UK or PyCon IT.


I like to know where there is a PyCon or a Django Conf or a PyData Event.

In fact, I think we can help the Python Community if we submit all the 
events in https://python.org/events.


This page has been created by the PSF and is maintained by some 
volunteers.


I know this list of events: 


* PyCon Cameroon : 20-23 Jav, Cameroon
* PythonFOSDEM : 4-5 Feb, Belgium
* PyCon Colombia : 10-12 Feb, Colombia
* PyCon Pune : 16-20 Feb, India
* Swiss Python Summit : 17-18 Feb, Switzerland
* IrPyCon : 17-18 Feb, Iran
* PyCon SK : 10-13 Mar, Slovakia
* Django Europe : 3-8 Apr, Italy
* PyCon Otto : 6-9 Apr, Italy
* Python Sudeste : 5-7 Mai, Brazil
* GeoPython : 8-11 May, Switzerland
* PyCon US : 17-26 May, USA
* EuroPython : July, Italy
* PyCon AU : 3-9 Aug, Australia
* PyCon UK : September, United Kingdom
* PyCon CA : November, Canada
* PyCon Ireland : October, Ireland
* PyCon FR : October/November, France

And you ? 

Please, could you check on https://www.python.org/events/ , if you are 
an organizer, please add your event.


If you think there is a missing event, please, send me the info via 
[email](mailto:steph...@wirtel.be) or via my [twitter 
account](https://twitter.com/matrixise) and I will add it on my slides.


I would like to present your event.

Thank you so much for your help.

Stephane Wirtel

[1] https://www.python-fosdem.org

--
Stéphane Wirtel - http://wirtel.be - @matrixise
--
https://mail.python.org/mailman/listinfo/python-list


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 07:58 schreef Deborah Swanson:
> Peter Otten wrote, on January 08, 2017 5:21 AM
>> Deborah Swanson wrote:
>>
>>> Peter Otten wrote, on January 08, 2017 3:01 AM
>>  
>> Personally I would recommend against mixing data (an actual location)
> and 
>> metadata (the column name,"Location"), but if you wish my code can be 
>> adapted as follows:
>>
>> infile = open("dictreader_demo.csv")
>> rows = csv.reader(infile)
>> fieldnames = next(rows)
>> Record = namedtuple("Record", fieldnames)
>> records = [Record._make(fieldnames)]
>> records.extend(Record._make(row) for row in rows)
> Works like a charm. I stumbled a bit changing all my subscripted
> variables to namedtuples and rewriting the inevitable places my code
> that didn't work the same. But actually it was fun, especially deleting
> all the sections and variables I no longer needed. And it executes
> correctly now too - with recognizable fieldnames instead of my quirky
> 2-letter code subscripts.  All in all a huge win!
>
> I do have two more questions.
>
> 1) I have a section that loops through the sorted data, compares two
> adjacent rows at a time, and marks one of them for deletion if the rows
> are identical.
>
> I'm using 
>
> for i in range(len(records)-1):
> r1 = records[i]
> r2 = records[i+1]
> if r1.xx = r2.xx:
>   .
>   .
> and my question is whether there's a way to work with two adjacent rows
> without using subscripts?  

for r1, r2 in zip(records[i], records[i+1]):
if r1.xx == r2.xx
.
.


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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 07:58 schreef Deborah Swanson:
> Peter Otten wrote, on January 08, 2017 5:21 AM
>> Deborah Swanson wrote:
>>
>>> Peter Otten wrote, on January 08, 2017 3:01 AM
>>  
>> Personally I would recommend against mixing data (an actual location)
> and 
>> metadata (the column name,"Location"), but if you wish my code can be 
>> adapted as follows:
>>
>> infile = open("dictreader_demo.csv")
>> rows = csv.reader(infile)
>> fieldnames = next(rows)
>> Record = namedtuple("Record", fieldnames)
>> records = [Record._make(fieldnames)]
>> records.extend(Record._make(row) for row in rows)
> Works like a charm. I stumbled a bit changing all my subscripted
> variables to namedtuples and rewriting the inevitable places my code
> that didn't work the same. But actually it was fun, especially deleting
> all the sections and variables I no longer needed. And it executes
> correctly now too - with recognizable fieldnames instead of my quirky
> 2-letter code subscripts.  All in all a huge win!
>
> I do have two more questions.
>
> 1) I have a section that loops through the sorted data, compares two
> adjacent rows at a time, and marks one of them for deletion if the rows
> are identical.
>
> I'm using 
>
> for i in range(len(records)-1):
> r1 = records[i]
> r2 = records[i+1]
> if r1.xx = r2.xx:
>   .
>   .
> and my question is whether there's a way to work with two adjacent rows
> without using subscripts?  
>
> Even better, to get hold of all the records with the same Description as
> the current row, compare them all, mark all but the different ones for
> deletion, and then resume processing the records after the last one?

If I understand correctly you want something like:

records.sort(key = lamda rec: rec.xx)

AKA 

from operator import attrgetter
records.sort(key = attrgetter('xx'))

or maybe:

records.sort(key = lambda rec: (rec.xx,) + tuple(rec))

-- 
Antoon Pardon

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka

On 09.01.17 05:53, Steven D'Aprano wrote:

Suppose you have an expensive calculation that gets used two or more times in a
loop. The obvious way to avoid calculating it twice in an ordinary loop is with
a temporary variable:

result = []
for x in data:
tmp = expensive_calculation(x)
result.append((tmp, tmp+1))


But what if you are using a list comprehension?


result = [(tmp, tmp + 1)
  for tmp in (expensive_calculation(x) for x in data)]

You could also assign an internal generator expression to temporal 
variable for readability if it is long.


gen = (expensive_calculation(x) for x in data)
result = [(tmp, tmp + 1) for tmp in gen]


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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Antoon Pardon wrote, on January 09, 2017 2:14 AM

> > 1) I have a section that loops through the sorted data, compares two

> > adjacent rows at a time, and marks one of them for deletion if the 
> > rows are identical.
> >
> > I'm using
> >
> > for i in range(len(records)-1):
> > r1 = records[i]
> > r2 = records[i+1]
> > if r1.xx = r2.xx:
> > .
> > .
> > and my question is whether there's a way to work with two adjacent 
> > rows without using subscripts?
> 
> for r1, r2 in zip(records[i], records[i+1]):
> if r1.xx == r2.xx
> .
> .

Ok, I've seen the zip function before and it might do the job, but here
I think you're suggesting:

for i in range(len(records)-1):
for r1, r2 in zip(records[i], records[i+1]):
if r1.xx == r2.xx
.
.

The hope was to do the loop without subscripts, and this may or may not
have gotchas because records is a namedtuple:

for r in records(1:):
.
.

Deborah

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Steven D'Aprano  writes:
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]

   def memoize(f):
cache = {}
def m(x):
if x in cache:
return cache[x]
a = f(x)
cache[x] = a
return a
return m

   ec = memoize(expensive_calculation)
   ...  [(ec(x), ec(x) + 1) for x in data]

Or can write:

@memoize
def expensive_calculation(x): 

Note the Haskell version of your listcomp would be:

  [(e, e+1) | x <- data_, let e = expensive_calculation x]

Maybe Python could get some version of that.  I've wanted it more than
once.  (I used "data_" because data is a Haskell keyword).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Serhiy Storchaka  writes:
> gen = (expensive_calculation(x) for x in data)
> result = [(tmp, tmp + 1) for tmp in gen]

result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Antoon Pardon wrote, on January 09, 2017 2:35 AM

> If I understand correctly you want something like:
> 
> records.sort(key = lamda rec: rec.xx)
> 
> AKA 
> 
> from operator import attrgetter
> records.sort(key = attrgetter('xx'))
> 
> or maybe:
> 
> records.sort(key = lambda rec: (rec.xx,) + tuple(rec))
> 
> -- 
> Antoon Pardon

I think you are replying to my question about sorting a namedtuple, in
this case it's called 'records'.

I think your suggestion works for lists and tuples, and probably
dictionaries. But namedtuples doesn't have a sort function.

>>> from collections import namedtuple
>>> dir(namedtuple)
['__annotations__', '__call__', '__class__', '__closure__', '__code__',
'__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__get__', '__getattribute__',
'__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__',
'__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__',
'__qualname__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__']

so nothing with records.sort will work.  :(

Deborah

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Steve D'Aprano
On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote:

[...]
> I think you are replying to my question about sorting a namedtuple, in
> this case it's called 'records'.
> 
> I think your suggestion works for lists and tuples, and probably
> dictionaries. But namedtuples doesn't have a sort function.

Tuples in general (whether named or not) represent structs or records, where
the position of the item is significant. It doesn't usually make sense to
sort individual elements of a record or tuple:

Before sorting: Record(name='George', spouse='', position='Accountant')
After sorting:  Record(name='', spouse='Accountant', position='George')


I think what you want to do is sort a list of records, not each record
itself. Or possibly you want to reorder the columns, in which case the
easiest way to do that is by editing the CSV file in LibreOffice or Excel
or another spreadsheet application.

If you have a list of records, call .sort() on the list, not the individual
records. 

But if I am wrong, and you absolutely must sort the fields of each record,
call the sorted() function, which will copy the fields into a list and sort
the list. That is:

alist = sorted(record)





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Error message IDLE

2017-01-09 Thread Gretchen Hasselbring
Hello

Trying to learn python on a laptop.  Was successful for awhile then...

Had a 'subprocess startup error'

'IDLE's subprocess didn't make connection.  Either IDLE can't start subprocess 
or personal firewall software is blocking the connection '


Doesn't appear to be firewall and I uninstalled and reinstalled python 3.6.032 
bit???

Any advice?
Thanks


Sent from my iPad
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 02:46, Paul Rubin wrote:
> > gen = (expensive_calculation(x) for x in data)
> > result = [(tmp, tmp + 1) for tmp in gen]  
> 
> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]

As charmingly expressive as map() is, the wildly different behavior in
py3 (it's a generator that evaluates lazily) vs py2 (it consumes the
entire iterable in one go) leads me to avoid it in general,
especially when Python gives me list-comprehensions and generators
that can do what I intend explicitly.  E.g., passing in
itertools.count() as an iterable to map() will kill py2 but is
perfectly fine in py3 (a horrible example):

  import itertools as i
  for x in i.takewhile(
  lambda n: n < 100, 
  map(lambda g: g**2, i.count())
  ):
print(x)

But yes, both ChrisA's pass-it-to-a-function and Serhiy's nested
generate-the-tmp-values-then-operate-on-those are good solutions
depending on how they feel in any particular context.  If I need to
use an "if" clause in the outer generator that tests the resulting
temp values, I use Serhiy's solution:

  [(tmp, tmp + 1)
for tmp in (
  expensive_calculation(x)
  for x in data
  )
if tmp > 42
]
 
Otherwise, I like the cleanliness of ChrisA's function:

  def fn(x)
tmp = expensive_calculation(x)
return x, x + 1
  [fn(x) for x in data]

-tkc




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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Steve D'Aprano wrote, on January 09, 2017 3:40 AM
> 
> On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote:
> 
> [...]
> > I think you are replying to my question about sorting a 
> namedtuple, in 
> > this case it's called 'records'.
> > 
> > I think your suggestion works for lists and tuples, and probably 
> > dictionaries. But namedtuples doesn't have a sort function.
> 
> Tuples in general (whether named or not) represent structs or 
> records, where the position of the item is significant. It 
> doesn't usually make sense to sort individual elements of a 
> record or tuple:
> 
> Before sorting: Record(name='George', spouse='', 
> position='Accountant') After sorting:  Record(name='', 
> spouse='Accountant', position='George')
> 
> 
> I think what you want to do is sort a list of records, not 
> each record itself. Or possibly you want to reorder the 
> columns, in which case the easiest way to do that is by 
> editing the CSV file in LibreOffice or Excel or another 
> spreadsheet application.
> If you have a list of records, call .sort() on the list, not 
> the individual records. 
> 

I want to sort a nametuple of records.

I could convert it to a list easy enough, with:

recs = list(records)

and then use the column copying and deleting method I described in my
previous post and use mergesort. This would give me exactly what I had
in my original code, and it would be ok. There's only one step after the
mergesort, and I could do it without the namedtuple, although I'd have
to count column indices for that section, and rewrite them whenever the
columns changed, which was what my original 2-letter codes and the
conversion to namedtuples were both meant to avoid.

So all in all, the best thing would be if there's a way to sort records
as a namedtuple. 


> But if I am wrong, and you absolutely must sort the fields of 
> each record, call the sorted() function, which will copy the 
> fields into a list and sort the list. That is:
> 
> alist = sorted(record)
> 
> -- 
> Steve
> "Cheer up," they said, "things could be worse." So I cheered 
> up, and sure enough, things got worse.

I'm not sure what you mean by sorting the fields of each record. I want
all the rows in records sorted by the Description and Date in each
record.

alist = sorted(record)

looks like it sorts one record, by what? An alphanumeric ordering of all
the fields in record?  That would be beyond useless for my purposes.

It's possible sorted() will work on namedtuples. Stackoverflow has an
example:

from operator import attrgetter
from collections import namedtuple

Person = namedtuple('Person', 'name age score')
seq = [Person(name='nick', age=23, score=100),
   Person(name='bob', age=25, score=200)]
# Sort list by name
sorted(seq, key=attrgetter('name'))
# Sort list by age
sorted(seq, key=attrgetter('age'))

So apparently it's done, and it's a keyed sort too. Although what
they're sorting is a list of namedtuples, which may or may not work on a
single namedtuple made of row (record) namedtuples. I'll definitely try
it tomorrow.

Know any way to convert a list back into a namedtuple? I suppose I could
go through all the steps used at the beginning to make records, but that
seems a waste if there's any way at all to sort the namedtuple without
converting it into a list.

Thanks Steven! Maybe sorted() is my friend here.  (haha, and maybe not.)

Deborah

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


Re: Clickable hyperlinks

2017-01-09 Thread Rhodri James

On 05/01/17 02:53, Deborah Swanson (Deborah Swanson) wrote:

Rhodri James wrote, on January 05, 2017 3:53 AM


On 05/01/17 04:52, Deborah Swanson wrote:

My original question was in fact whether there was a way to make
clickable hyperlinks in a console. I was persuaded after about 10
replies that the answer was no,


Then you were persuaded wrong; the actual answer was "this isn't a
meaningful question since it's based on incorrect assumptions."
Translating that to "No" is just as much a mistake as
translating it to
"Yes."


Actually, your statement "this isn't a meaningful question since it's based on
incorrect assumptions" is false. PyCharm outputs clickable links to the
console, but they aren't web links, they simply link to lines of code.


Nope.  PyCharm outputs text to the console that the console chooses to 
interpret as a link and makes clickable.  As Stephen pointed out right 
back at the beginning of this thread, printing the textual string that 
is a URL could do exactly the same thing *if* the console you print to 
chooses to interpret it as such.  The choice is with the console, not 
your program; that there is the incorrect assumption.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


RE: Clickable hyperlinks

2017-01-09 Thread Deborah Swanson
Rhodri James wrote, on January 09, 2017 4:28 AM
> 
> Nope.  PyCharm outputs text to the console that the console 
> chooses to 
> interpret as a link and makes clickable.  As Stephen pointed 
> out right 
> back at the beginning of this thread, printing the textual 
> string that 
> is a URL could do exactly the same thing *if* the console you 
> print to 
> chooses to interpret it as such.  The choice is with the console, not 
> your program; that there is the incorrect assumption.
> 
> -- 
> Rhodri James *-* Kynesim Ltd

Sorry, Rhodri. I don't agree with your logic. You can use tkinter (code
in a program) to make clickable links in the console, and the webbrowser
module to web-enable them so urls open in a browser when you click on
them.

I have no idea how this crowd got off on the mantra "The choice is with
the console".  Code does in fact have the power to control what happens
in the console. How do you think Linux does it on their terminals with
clickable links? Granted, the code may have to specify parameters for a
particular console, but I certainly wasn't asking for universal code
that would work with any console. That was something made up by the
responders on the thread, so they could revile me for such an
outrageously impossible demand. My original question was if Python had
anything equivalent to the hyperlink formula in Excel, which is a
program (code) feature. Nothing about doing it on any console in the
universe.

The developers who wrote PyCharm coded it for the console they were
using. The console is a dead thing, it has no mind or soul to choose
anything. Surely an educated person would know that.

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Rustom Mody
On Monday, January 9, 2017 at 5:54:15 PM UTC+5:30, Tim Chase wrote:
> On 2017-01-09 02:46, Paul Rubin wrote:
> > > gen = (expensive_calculation(x) for x in data)
> > > result = [(tmp, tmp + 1) for tmp in gen]  
> > 
> > result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]
> 
> As charmingly expressive as map() is, the wildly different behavior in
> py3 (it's a generator that evaluates lazily) vs py2 (it consumes the
> entire iterable in one go) leads me to avoid it in general,
> especially when Python gives me list-comprehensions and generators
> that can do what I intend explicitly.  E.g., passing in
> itertools.count() as an iterable to map() will kill py2 but is
> perfectly fine in py3 (a horrible example):
> 
>   import itertools as i
>   for x in i.takewhile(
>   lambda n: n < 100, 
>   map(lambda g: g**2, i.count())
>   ):
> print(x)
> 
> But yes, both ChrisA's pass-it-to-a-function and Serhiy's nested
> generate-the-tmp-values-then-operate-on-those are good solutions
> depending on how they feel in any particular context.  If I need to
> use an "if" clause in the outer generator that tests the resulting
> temp values, I use Serhiy's solution:
> 
>   [(tmp, tmp + 1)
> for tmp in (
>   expensive_calculation(x)
>   for x in data
>   )
> if tmp > 42
> ]

What happens when the expensive is on an inner generator?
Something like:

[expensive₂(y)  for x in data for y in foo(x)]

[The ₂ representing the 2 or more occurrences in Steven's eg]
-- 
https://mail.python.org/mailman/listinfo/python-list


Help with this code

2017-01-09 Thread José Manuel Suárez Sierra
Hello, I am trying to make a code wich compares between 2 or several sequences 
(lists). It compares every element in a list with another list elements. For 
example, if we have a list_a=["a","b","c","d"] and list_b=["a","b"] I want to 
obtain a new list_c containing elements that match between these lists (a and b 
here), but, if for instance list_b were ["a","c"] the program must not store 
this data because they are not in same order.

Said this, I wrote this code but it doesnt work:

if __name__ == "__main__":


def compare(a, b):

i = 0
j = 0
c1 = []

while a[i] == b[j]:
c1.append(a[i])
j = j+1
i=i+1

return c1




cadena_1=raw_input("Introduce list 1 \n")
cadena_2 = raw_input("Introduce list 2 \n")


transf1=list(cad_1) 
transf2 = list(cad_2)


print compare(transf1,transf2)




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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 04:53 schreef Steven D'Aprano:
> Suppose you have an expensive calculation that gets used two or more times in 
> a 
> loop. The obvious way to avoid calculating it twice in an ordinary loop is 
> with 
> a temporary variable:
>
> result = []
> for x in data:
> tmp = expensive_calculation(x)
> result.append((tmp, tmp+1))
>
>
> But what if you are using a list comprehension? Alas, list comps don't let 
> you 
> have temporary variables, so you have to write this:
>
>
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
>
>
> Or do you? ... no, you don't!
>
>
> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]
>
>
> I can't decide whether that's an awesome trick or a horrible hack...

Maybe this in an occasion to use your recipe.

http://code.activestate.com/recipes/580625-collection-pipeline-in-python/

result = data | Map(expensive_calculation) | Map(lambda tmp: (tmp, tmp + 1)) | 
List

-- 
Antoon.

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


RE: Help with this code

2017-01-09 Thread Deborah Swanson
José Manuel Suárez Sierra wrote, on January 09, 2017 5:09 AM
> 
> Hello, I am trying to make a code wich compares between 2 or 
> several sequences (lists). It compares every element in a 
> list with another list elements. For example, if we have a 
> list_a=["a","b","c","d"] and list_b=["a","b"] I want to 
> obtain a new list_c containing elements that match between 
> these lists (a and b here), but, if for instance list_b were 
> ["a","c"] the program must not store this data because they 
> are not in same order.
> 
> Said this, I wrote this code but it doesnt work:
> 
> if __name__ == "__main__":
> 
> 
> def compare(a, b):
> 
> i = 0
> j = 0
> c1 = []
> 
> while a[i] == b[j]:
> c1.append(a[i])
> j = j+1
> i=i+1
> 
> return c1
> 
> 
> 
> 
> cadena_1=raw_input("Introduce list 1 \n")
> cadena_2 = raw_input("Introduce list 2 \n")
> 
> 
> transf1=list(cad_1) 
> transf2 = list(cad_2)
> 
> 
> print compare(transf1,transf2)
> 
> 
> 
> 
> Thank you

I think your code assumes that a and b are the same length, and that all
the elements in the intersection (the ones in both lists) are in order
at the beginning of the list, like:

a = [1, 2, 5, 8, 22, 11, 14]
b = [1, 2, 5, 19, 12, 31, 42]

You only want to append the current list element if you test that it's
in both lists, and the answer is True. We do that with an 'if'
statement.

You need something like this, and a for loop would be the better choice
here.

cl = []
# look at every list element in 'a'
for c in a:
# is this element in 'b'?
if e in b:
# if True, then append e to the result list, or continue
if False
cl.append(e)
return cl

There's fancier ways to do it all in one line of code, but you need to
understand the fundamentals before you move on to tricks.

Deborah



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


Re: Help with this code

2017-01-09 Thread Peter Otten
José Manuel Suárez Sierra wrote:

> Hello, 

Welcome!


> I am trying to make a code wich compares between 2 or several
> sequences (lists). It compares every element in a list with another list
> elements. For example, if we have a list_a=["a","b","c","d"] and
> list_b=["a","b"] I want to obtain a new list_c containing elements that
> match between these lists (a and b here), but, if for instance list_b were
> ["a","c"] the program must not store this data because they are not in
> same order.
> 
> Said this, I wrote this code but it doesnt work:

Get into the habit of describing what "doesn't work" as precisely as you 
can. That is the first and most important step to fixing a bug. Python often 
helps you with that task by producing a "traceback", in your case something 
like

$ python tmp3.py
  File "tmp3.py", line 12
j = j+1
^
IndentationError: unexpected indent

probably with another filename and line number. This is the compiler trying 
to tell you that line 12 has more leading whitespace than the preceding one, 
something that should only occur e. g. for the body of a while-loop, not at 
random places like the

j = j+1

that follows the ordinary statement

c1.append(a[i])

Note that once you have fixed your code in this place you will run into 
another error. Read the traceback carefully and try to figure out what's 
going on. If you need help come back here or, better, ask on the tutor 
mailing list which is dedicated to newbies making their first steps with 
Python. Good luck!

> if __name__ == "__main__":
> 
> 
> def compare(a, b):
> 
> i = 0
> j = 0
> c1 = []
> 
> while a[i] == b[j]:
> c1.append(a[i])
> j = j+1
> i=i+1
> 
> return c1
> 
> 
> 
> 
> cadena_1=raw_input("Introduce list 1 \n")
> cadena_2 = raw_input("Introduce list 2 \n")
> 
> 
> transf1=list(cad_1)
> transf2 = list(cad_2)
> 
> 
> print compare(transf1,transf2)
> 
> 
> 
> 
> Thank you


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


Re: Help with this code

2017-01-09 Thread José Manuel Suárez Sierra
El lunes, 9 de enero de 2017, 14:09:09 (UTC+1), José Manuel Suárez Sierra  
escribió:
> Hello, I am trying to make a code wich compares between 2 or several 
> sequences (lists). It compares every element in a list with another list 
> elements. For example, if we have a list_a=["a","b","c","d"] and 
> list_b=["a","b"] I want to obtain a new list_c containing elements that match 
> between these lists (a and b here), but, if for instance list_b were 
> ["a","c"] the program must not store this data because they are not in same 
> order.
> 
> Said this, I wrote this code but it doesnt work:
> 
> if __name__ == "__main__":
> 
> 
> def compare(a, b):
> 
> i = 0
> j = 0
> c1 = []
> 
> while a[i] == b[j]:
> c1.append(a[i])
> j = j+1
> i=i+1
> 
> return c1
> 
> 
> 
> 
> cadena_1=raw_input("Introduce list 1 \n")
> cadena_2 = raw_input("Introduce list 2 \n")
> 
> 
> transf1=list(cad_1) 
> transf2 = list(cad_2)
> 
> 
> print compare(transf1,transf2)
> 
> 
> 
> 
> Thank you

Thanks for your reply,
I wrote this code:
def comparar(cad1, cad2):
i = 0
j = 0
cad3 = []
k = True

for cad1[i] in cad1:
k = True
for cad2[j] in cad2:
while cad1[i] == cad2[j] and i<= len(cad1) and j <= len(cad2):
cad3.append(cad1[i])
i = i + 1
j = j + 1

if cad1[i] != cad2[j]:
k = False
i = i + 1
return cad3

This is a function, another important thing is the order of elements, it must 
be ordered as it appears in one of the lists, in your example, my function must 
return a list with elements 1,2,5 in that order.

I cant find my mistake in these function I wrote you above.

Thank you very much
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Clickable hyperlinks

2017-01-09 Thread Tim Chase
On 2017-01-09 05:00, Deborah Swanson wrote:
> Code does in fact have the power to control what happens
> in the console. How do you think Linux does it on their terminals
> with clickable links? Granted, the code may have to specify
> parameters for a particular console, but I certainly wasn't asking
> for universal code that would work with any console.

Only to a degree.  Some consoles sniff the text sent to them for
particular URL-like patterns and linkify them for you.  All you have
to do is print a URL that its pattern-matching identifies:

  print("http://example.com";)

However, as Rhodri details, *this is a feature of the terminal
emulator*. I've got a smattering of terminal emulators at my disposal
and many don't auto-linkify (xterm, rxvt, the base non-X terminal)
while others do (Gnome terminal).  And in the ones where it doesn't
work, it's because it's the *terminal* that doesn't support
linkifying, so no amount of work on the application's part will make
it linkify.

> The console is a dead thing, it has no mind or soul to choose
> anything. Surely an educated person would know that.

Pretty much every quality system administrator I know uses the
terminal.  Just about all of the best devs I know use the terminal.
Microsoft added Powershell because of demand. They added Ubuntu/bash
support because of demand.  It allows for powerful automation that
would otherwise require writing full-fledged scripts.  There is
nothing dead about it.

I'm not sure where you get your baseless claim that "an educated
person would know that", since someone who had taken the time to
study the state of terminal use would see that is far from dead.

-tkc



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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Tim Chase
On 2017-01-08 22:58, Deborah Swanson wrote:
> 1) I have a section that loops through the sorted data, compares two
> adjacent rows at a time, and marks one of them for deletion if the
> rows are identical.
> 
> I'm using 
> 
> for i in range(len(records)-1):
> r1 = records[i]
> r2 = records[i+1]
> if r1.xx = r2.xx:
>   .
>   .
> and my question is whether there's a way to work with two adjacent
> rows without using subscripts?  

I usually wrap the iterable in something like

  def pairwise(it):
prev = next(it)
for thing in it:
  yield prev, thing
  prev = thing

  for prev, cur in pairwise(records):
compare(prev, cur)

which I find makes it more readable.

-tkc


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


Re: Help with this code

2017-01-09 Thread José Manuel Suárez Sierra
El lunes, 9 de enero de 2017, 14:09:09 (UTC+1), José Manuel Suárez Sierra  
escribió:
> Hello, I am trying to make a code wich compares between 2 or several 
> sequences (lists). It compares every element in a list with another list 
> elements. For example, if we have a list_a=["a","b","c","d"] and 
> list_b=["a","b"] I want to obtain a new list_c containing elements that match 
> between these lists (a and b here), but, if for instance list_b were 
> ["a","c"] the program must not store this data because they are not in same 
> order.
> 
> Said this, I wrote this code but it doesnt work:
> 
> if __name__ == "__main__":
> 
> 
> def compare(a, b):
> 
> i = 0
> j = 0
> c1 = []
> 
> while a[i] == b[j]:
> c1.append(a[i])
> j = j+1
> i=i+1
> 
> return c1
> 
> 
> 
> 
> cadena_1=raw_input("Introduce list 1 \n")
> cadena_2 = raw_input("Introduce list 2 \n")
> 
> 
> transf1=list(cad_1) 
> transf2 = list(cad_2)
> 
> 
> print compare(transf1,transf2)
> 
> 
> 
> 
> Thank you

Thank you!

Here is my code, I dont understand why it doesnt work very well:
if __name__ == "__main__":


cadena_1 = raw_input("Introduce la cadena 1 \n")
cadena_2 = raw_input("Introduce la cadena 2 \n")
# n=input("De cuantas letras quieres hacer la comparacion?\n")

transf1 = list(cadena_1)  
transf2 = list(cadena_2)
l1=len(transf1)
l2=len(transf2)
c3=[]
i=0
j=0


for transf1[i] in transf1:  
for transf2[j] in transf2:   
if transf1[i]==transf2[j]: 
c3.append(transf1[i])   
i=i+1   
j=j+1   
else:  
j=j+1   

print c3

This is the traceback:
line 18, in 
   for transf2[j] in transf2:
IndexError: list assignment index out of range

If I have initialized j=0 (such as i) why does it not work?
I want the script to read sequence 1 and compares every element inside it with 
elements in sequence 2 no mattering where it matches.

Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
Deborah Swanson wrote:

> Even better, to get hold of all the records with the same Description as
> the current row, compare them all, mark all but the different ones for
> deletion, and then resume processing the records after the last one?

When you look at all fields for deduplication anyway there's no need to 
treat one field (Description) specially. Just

records = set(records)

should be fine. As the initial order is lost* you probably want to sort 
afterwards. The code then becomes

records = sorted(
set(records), 
key=operator.attrgetter("Description")
)

Now if you want to fill in missing values, you should probably do this 
before deduplication -- and the complete() function introduced in

https://mail.python.org/pipermail/python-list/2016-December/717847.html

can be adapted to work with namedtuples instead of dicts.

(*) If you want to preserve the initial order you can use a 
collections.OrderedDict instead of the set.


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


Re: Clickable hyperlinks

2017-01-09 Thread Rhodri James

On 09/01/17 13:53, Tim Chase wrote:

On 2017-01-09 05:00, Deborah Swanson wrote:

The console is a dead thing, it has no mind or soul to choose
anything. Surely an educated person would know that.


Pretty much every quality system administrator I know uses the
terminal.  Just about all of the best devs I know use the terminal.
Microsoft added Powershell because of demand. They added Ubuntu/bash
support because of demand.  It allows for powerful automation that
would otherwise require writing full-fledged scripts.  There is
nothing dead about it.


I think you're falling foul of Deborah's inability to communicate again. 
 I think she meant to say that the console is dumb, not dead.  In a 
very strict sense that's almost true, but since we've been using 
"console" interchangeably with "terminal emulator" throughout this 
discussion, it's hilariously wrong.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Error message IDLE

2017-01-09 Thread Gretchen Hasselbring
FYI

Turns out I was saving my practice exercises as files (.py) under the python 
directory which conflicted with the running of Idle.  All cleared up now.

Sent from my iPad

> On 9 Jan 2017, at 12:16, Gretchen Hasselbring  
> wrote:
> 
> Hello
> 
> Trying to learn python on a laptop.  Was successful for awhile then...
> 
> Had a 'subprocess startup error'
> 
> 'IDLE's subprocess didn't make connection.  Either IDLE can't start 
> subprocess or personal firewall software is blocking the connection '
> 
> 
> Doesn't appear to be firewall and I uninstalled and reinstalled python 
> 3.6.032 bit???
> 
> Any advice?
> Thanks
> 
> 
> Sent from my iPad
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error message IDLE

2017-01-09 Thread Terry Reedy

On 1/9/2017 6:48 AM, Gretchen Hasselbring wrote:

Hello

Trying to learn python on a laptop.  Was successful for awhile then...

Had a 'subprocess startup error'

'IDLE's subprocess didn't make connection.  Either IDLE can't start subprocess 
or personal firewall software is blocking the connection '


Doesn't appear to be firewall and I uninstalled and reinstalled python 3.6.032 
bit???

Any advice?


When IDLE works consistently and then consistently does not work, 
something changed on your system.  Since reinstalling did not fix the 
issue, the change was not in Python and IDLE.  The first thing to do is 
to run IDLE from your system's console with '/python -m 
idlelib' and see if an error message appears.


There are at least 5 other possible causes, including

1. You created a file with the same name as a stdlib mdoule. (Rename it.)
2. There is a problem with a config file in $HOME/.idlerc/.  (Easier is 
to delete.)

3. Some network setting change cause socket connection to fail.

--
Terry Jan Reedy

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


Re: Help with this code

2017-01-09 Thread Peter Otten
José Manuel Suárez Sierra wrote:

> This is the traceback:
> line 18, in 
> for transf2[j] in transf2:
> IndexError: list assignment index out of range
> 
> If I have initialized j=0 (such as i) why does it not work?

A for loop

for x in y:
   ...

sequentually assigns every value in y to x. So with y = "ab" it executes

x = "a"
...
x = "b"
...

This is the case even when there is something more complex like transf2[j]:

for transf2[j] in y:
...

translates to

transf2[j] = "a"
...  # both transf2 and j may be rebound here and thus affect
transf2[j] = "b" # what this actually does
...

and thus can fail with an IndexError when j is bound to a value greater than 
the length of the transf2 list. 

But most likely you want to avoid this feature of Python -- even the experts 
never use it.

> I want the script to read sequence 1 and compares every element inside it
> with elements in sequence 2 no mattering where it matches.

That may be sufficient to explain the objective to a human who already has a 
rough idea of your goal, but to come up with an actual algorithm you need to 
think about -- and communicate -- a lot more detail.

There is a third party module that operates on strings, not lists which 
finds the edit operations

>>> Levenshtein.editops("abcdefgj", "axcdyezfg")
[('replace', 1, 1), ('insert', 4, 4), ('insert', 5, 6), ('delete', 7, 9)]

What should your function return given the equivalent lists

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'j']
['a', 'x', 'c', 'd', 'y', 'e', 'z', 'f', 'g']

? Try to make your plain-english description as precise as possible before 
you start coding.


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


Re: Error message IDLE

2017-01-09 Thread Peter Otten
Gretchen Hasselbring wrote:

> FYI
> 
> Turns out I was saving my practice exercises as files (.py) under the
> python directory which conflicted with the running of Idle.  All cleared
> up now.

Thanks for the update. Can you tell which file you shadowed to cause the 
error? Perhaps even if the underlying problem turns out to be hard to fix at 
least the error message could be improved.


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


enable-framework Vs. Naught

2017-01-09 Thread André Lemos
Hi,


I have a C++ module that I am compiling to use inside of my Python
installation under Mac OS.

If I compile & link it against a Framework enabled Python installation, it
works fine, but if I compile & link it against a *non* enabled Framework
installation that we use for distribution, I simply get a non inspiring:

Fatal Python error: PyThreadState_Get: no current thread


I am using python-config to get my flags on both the examples, but I simply
cannot get it to run (although it compiles fine) on a *non* enabled
Framework installation.


Thoughts/Help?



--
André Lemos
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread breamoreboy
On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote:
> On 2017-01-08 22:58, Deborah Swanson wrote:
> > 1) I have a section that loops through the sorted data, compares two
> > adjacent rows at a time, and marks one of them for deletion if the
> > rows are identical.
> > 
> > I'm using 
> > 
> > for i in range(len(records)-1):
> > r1 = records[i]
> > r2 = records[i+1]
> > if r1.xx = r2.xx:
> > .
> > .
> > and my question is whether there's a way to work with two adjacent
> > rows without using subscripts?  
> 
> I usually wrap the iterable in something like
> 
>   def pairwise(it):
> prev = next(it)
> for thing in it:
>   yield prev, thing
>   prev = thing
> 
>   for prev, cur in pairwise(records):
> compare(prev, cur)
> 
> which I find makes it more readable.
> 
> -tkc

Or from https://docs.python.org/3/library/itertools.html#itertools-recipes:-

def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)

This and many other recipes are available in the more-itertools module which is 
on pypi.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error message IDLE

2017-01-09 Thread Gretchen Hasselbring
Thank you for your responses.  The file I think is responsible was called 
types.py





Sent from my iPad

> On 9 Jan 2017, at 16:11, Peter Otten <__pete...@web.de> wrote:
> 
> Gretchen Hasselbring wrote:
> 
>> FYI
>> 
>> Turns out I was saving my practice exercises as files (.py) under the
>> python directory which conflicted with the running of Idle.  All cleared
>> up now.
> 
> Thanks for the update. Can you tell which file you shadowed to cause the 
> error? Perhaps even if the underlying problem turns out to be hard to fix at 
> least the error message could be improved.
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 04:59, Rustom Mody wrote:
> What happens when the expensive is on an inner generator?
> Something like:
> 
> [expensive₂(y)  for x in data for y in foo(x)]
> 
> [The ₂ representing the 2 or more occurrences in Steven's eg]

Well, if I understand your question correctly, the goal would be to
execute each expensive operation only once, regardless of how many
expensive functions you execute:

  [(x, a, b, x+1, a+1, b+1)
   for x, a, b
   in (
 x,
 expensive_a(x),
 expensive_b(x),
 for x
 in data
 )
   # if x > 42 # optionally test against values
   ]

-tkc



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


Re: Clickable hyperlinks

2017-01-09 Thread Michael Torrie
On 01/09/2017 06:00 AM, Deborah Swanson wrote:
> Rhodri James wrote, on January 09, 2017 4:28 AM
>>
>> Nope.  PyCharm outputs text to the console that the console 
>> chooses to 
>> interpret as a link and makes clickable.  As Stephen pointed 
>> out right 
>> back at the beginning of this thread, printing the textual 
>> string that 
>> is a URL could do exactly the same thing *if* the console you 
>> print to 
>> chooses to interpret it as such.  The choice is with the console, not 
>> your program; that there is the incorrect assumption.
> Sorry, Rhodri. I don't agree with your logic. 

You may, but that doesn't change the fact that Rhodri's post is 100%
accurate.

> You can use tkinter (code
> in a program) to make clickable links in the console, 

Unless you're talking about an implementation of a console or terminal
emulator in tkinter, this is incorrect.  Tkinter does not do anything
with standard out.

> and the webbrowser
> module to web-enable them so urls open in a browser when you click on
> them.
> 
> I have no idea how this crowd got off on the mantra "The choice is with
> the console".  

I guess things keep going in circles because people are just trying to
help you understand what these various parts of the system are and how
they fit together, particularly with regards to so-called "console
applications."  (Text-mode, terminal-based, etc.)

> Code does in fact have the power to control what happens
> in the console.

Other than a pre-arranged protocol (win32 console api, ANSI, VT100, or
similar) between the displayer of the bytes and the python program, code
does not in fact have power to control what happens in the "console."

> How do you think Linux does it on their terminals with
> clickable links? 

This is just the terminal emulator trying to be smart and assuming
meaning in the bytes given to it, which may be a valid assumption on its
part, or may  not.  This is no way implies that all terminal emulators
should do this, nor do most terminal emulators.

> Granted, the code may have to specify parameters for a
> particular console, but I certainly wasn't asking for universal code
> that would work with any console. 

Your initial query back in the beginning did not state this.  Indeed you
never stated which console you wanted to work with. We addressed several
common ones including the Win32 console window (which cmd.exe runs in)
and various terminal emulators.

> That was something made up by the
> responders on the thread, so they could revile me for such an
> outrageously impossible demand. My original question was if Python had
> anything equivalent to the hyperlink formula in Excel, which is a
> program (code) feature. Nothing about doing it on any console in the
> universe.

I read frustration in several posts, but never reviling!

> The developers who wrote PyCharm coded it for the console they were
> using. 

The PyCharm developers built their own window to display "console"
output (standard-out bytes) from the app, and they could have their own
window interpret bytes and messages however they wanted to, and added
their own PyCharm-specific feature including hot links to error
locations (I think that comes from standard-err, though, not
standard-out). So if you want the PyCharm output window to create a
clickable hot-link for you you have to modify the PyCharm code to do that.

> The console is a dead thing, it has no mind or soul to choose
> anything. Surely an educated person would know that.

Not sure what you mean by that?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Search a sequence for its minimum and stop as soon as the lowest possible value is found

2017-01-09 Thread Peter Otten
Antonio Caminero Garcia wrote:

> On Friday, January 6, 2017 at 6:04:33 AM UTC-8, Peter Otten wrote:
>> Example: you are looking for the minimum absolute value in a series of
>> integers. As soon as you encounter the first 0 it's unnecessary extra
>> work to check the remaining values, but the builtin min() will continue.
>> 
>> The solution is a minimum function that allows the user to specify a stop
>> value:
>> 
>> >>> from itertools import count, chain
>> >>> stopmin(chain(reversed(range(10)), count()), key=abs, stop=0)
>> 0
>> 
>> How would you implement stopmin()?
>> 
>> Currently I raise an exception in the key function:
>> 
>> class Stop(Exception):
>> pass
>> 
>> def stopmin(items, key, stop):
>> """
>> >>> def g():
>> ... for i in reversed(range(10)):
>> ... print(10*i)
>> ... yield str(i)
>> >>> stopmin(g(), key=int, stop=5)
>> 90
>> 80
>> 70
>> 60
>> 50
>> '5'
>> """
>> def key2(value):
>> result = key(value)
>> if result <= stop:
>> raise Stop(value)
>> return result
>> try:
>> return min(items, key=key2)
>> except Stop as stop:
>> return stop.args[0]
> 
> This is the simplest version I could come up with. I also like the classic
> 100% imperative, but it seems that is not trendy between the solutions
> given :D.
> 
> you can test it here https://repl.it/FD5A/0
> source code:
> 
> from itertools import accumulate
> 
> # stopmin calculates the greatest lower bound (infimum).
> # 
https://upload.wikimedia.org/wikipedia/commons/0/0a/Infimum_illustration.svg
> 
> def takeuntil(pred, seq):
>   for item in seq:
> yield item
> if not pred(item):

The "not": one of those decisions that can drive programmers into madness ;)

>   break
> 
> def stopmin(seq, stop=0):
>   drop_ltstop = (item for item in seq if item >= stop)
>   min_gen = (min_ for min_ in accumulate(drop_ltstop, func=min))

You don't need the genexp here; just accumulate() will do.
Using accumulate() is a nice suggestion, by the way.

>   return list(takeuntil(lambda x: x!= stop, min_gen))[-1]

 
> seq = [1, 4, 7, -8, 0, 7, -8, 9] # 0 just until zero is generated
> seq = [1, 4, 7, -8, 7, -8, 9] # 1 the entire sequence is generated
> 
> print(stopmin(seq, stop=0))

Once it passes my doctests your code isn't quite as simple, but still 
readable:

from itertools import accumulate
from operator import itemgetter
from collections import deque

firstitem = itemgetter(0)


def takeuntil(pred, items):
for item in items:
yield item
if pred(item):
break


def last(items):
tail = deque(items, maxlen=1)
if tail:
return tail.pop()
else:
raise ValueError


def minkey(a, b):
return min(a, b, key=firstitem)


def stopmin(items, *, key, stop):
"""
>>> def g():
... for i in reversed(range(10)):
... print(10*i)
... yield str(i)
>>> stopmin(g(), key=int, stop=5)
90
80
70
60
50
'5'
>>> stopmin(g(), key=int, stop=8.5)
90
80
'8'
>>> stopmin(g(), key=int, stop=9)
90
'9'
>>> stopmin([10, 9, -11, 7, -12], key=abs, stop=0)
7
>>> try: stopmin([], key=abs, stop=0)
... except ValueError: print('OK')
OK
>>> stopmin("a", key=lambda x: print(x) or "c", stop="b")
a
'a'
>>> class A:
...def __init__(self, key):
...self.key = key
>>> stopmin([A(2), A(2), A(1), A(0)], key=lambda a: a.key, stop=1.1).key
1
"""
pairs = ((key(item), item) for item in items)
descending_pairs = accumulate(pairs, func=minkey)
return last(takeuntil(lambda p: p[0] <= stop, descending_pairs))[1]


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


Re: Help with this code

2017-01-09 Thread Joel Goldstick
On Mon, Jan 9, 2017 at 1:02 PM, Gilmeh Serda
 wrote:
> On Mon, 09 Jan 2017 05:08:51 -0800, José Manuel Suárez Sierra wrote:
>
>> elements. For example, if we have a list_a=["a","b","c","d"] and
>> list_b=["a","b"] I want to obtain a new list_c containing elements that
>> match between these lists (a and b here),
>
> Perhaps this might work:
>
 list(set(list_a).intersection(set(list_b)))
> ['a', 'b']
>
> HTH
> --
> Gilmeh
> --
> https://mail.python.org/mailman/listinfo/python-list

I was confused whether the OP wanted to retain the list order.  The
spec seemed unclear. If not, this is a nice approach.  Of course this
also removes duplicate values.

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Clickable hyperlinks

2017-01-09 Thread Michael Torrie
On 01/09/2017 10:27 AM, Michael Torrie wrote:
>> You can use tkinter (code
>> in a program) to make clickable links in the console, 
> 
> Unless you're talking about an implementation of a console or terminal
> emulator in tkinter, this is incorrect.  Tkinter does not do anything
> with standard out.

To be clearer, what I mean is that using Tkinter or some other toolkit
you can indeed place links on a GUI form or window.  But this has
nothing to do with a terminal emulator or console (meaning the display
of standard-out bytes).

Using your understanding of the word, console, Tkinter could be used to
create a console of your own that displays the output from other
processes and implements clickable hyperlinks, but Tkinter itself does
not display using standard out, and thus cannot cause a terminal
emulator or Windows console to display hyperlinks in a clickable fashion.

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Antonio Caminero Garcia
On Sunday, January 8, 2017 at 7:53:37 PM UTC-8, Steven D'Aprano wrote:
> Suppose you have an expensive calculation that gets used two or more times in 
> a 
> loop. The obvious way to avoid calculating it twice in an ordinary loop is 
> with 
> a temporary variable:
> 
> result = []
> for x in data:
> tmp = expensive_calculation(x)
> result.append((tmp, tmp+1))
> 
> 
> But what if you are using a list comprehension? Alas, list comps don't let 
> you 
> have temporary variables, so you have to write this:
> 
> 
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
> 
> 
> Or do you? ... no, you don't!
> 
> 
> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]
> 
> 
> I can't decide whether that's an awesome trick or a horrible hack...
> 
> 
> -- 
> Steven
> "Ever since I learned about confirmation bias, I've been seeing 
> it everywhere." - Jon Ronson

Hello I saw some memoizing functions, in that sense you can use the 
functools.lru_cache decorator, that is even better if you have repeated 
elements in data.

@functools.lru_cache
def expensive_calculation(x):
# very NP-hard calculation
pass


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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Christian Gollwitzer

Am 09.01.17 um 04:53 schrieb Steven D'Aprano:

Or do you? ... no, you don't!

[(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]

I can't decide whether that's an awesome trick or a horrible hack...


I think this is quite clear, and a useful feature, only that Python 
makes it unnecessarily hard. In Haskell, there is a "let" clause, which 
would allow to write it as:


[let tmp = expensive_calc(x), (tmp, tmp+1)  for x in data]

or better readable using "with" or "where" as in

[(tmp, tmp + 1) with tmp = expensive_calc(x) for x in data]

or similar. So maybe that's a PEP to extend the list comprehension syntax?

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


RE: Help with this code

2017-01-09 Thread Joaquin Alzola
>> elements. For example, if we have a list_a=["a","b","c","d"] and
>> list_b=["a","b"] I want to obtain a new list_c containing elements that
>> match between these lists (a and b here),

>Perhaps this might work:

 list(set(list_a).intersection(set(list_b)))
>['a', 'b']

>>> list_a={"a","b","c","d"}
>>> list_b={"a","b"}
> sorted(list_a & list_b)
['a', 'b']

--

This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [PSF-Community] Python Events in 2017, Need your help.

2017-01-09 Thread Danny Adair
Thanks Stephane,

Kiwi PyCon 2017 will be in Auckland, New Zealand in September - exact
dates and location not yet determined. I'll submit it when they are.

Cheers,
Danny


On Mon, Jan 9, 2017 at 10:54 PM, Stephane Wirtel via PSF-Community
 wrote:
> Dear Community,
>
> For the PythonFOSDEM [1] on 4th and 5th February in Belgium, I would like to
> present some slides with the Python events around the World.  Based on
> https://python.org/events, I have noted that there are missing events, for
> example:
>
> * PyCon Otto: Italy
> * PyCon UK: United Kingdom
> * PyCon CA: Canada
> * PyCon Ireland: Ireland
> * PyCon France: France
>
> Some of these events are not yet announced and I understand they are in the
> second semester, and thus, they don't know the location and the dates,
> excepted for PyCon Otto (April).
>
> In fact, I have noted that we know some big events in the Python community
> (for example: PyCon US and EuroPython) but do you know the others events,
> maybe the local event, PyCon IE, PyCon UK or PyCon IT.
>
> I like to know where there is a PyCon or a Django Conf or a PyData Event.
>
> In fact, I think we can help the Python Community if we submit all the
> events in https://python.org/events.
>
> This page has been created by the PSF and is maintained by some volunteers.
>
> I know this list of events:
> * PyCon Cameroon : 20-23 Jav, Cameroon
> * PythonFOSDEM : 4-5 Feb, Belgium
> * PyCon Colombia : 10-12 Feb, Colombia
> * PyCon Pune : 16-20 Feb, India
> * Swiss Python Summit : 17-18 Feb, Switzerland
> * IrPyCon : 17-18 Feb, Iran
> * PyCon SK : 10-13 Mar, Slovakia
> * Django Europe : 3-8 Apr, Italy
> * PyCon Otto : 6-9 Apr, Italy
> * Python Sudeste : 5-7 Mai, Brazil
> * GeoPython : 8-11 May, Switzerland
> * PyCon US : 17-26 May, USA
> * EuroPython : July, Italy
> * PyCon AU : 3-9 Aug, Australia
> * PyCon UK : September, United Kingdom
> * PyCon CA : November, Canada
> * PyCon Ireland : October, Ireland
> * PyCon FR : October/November, France
>
> And you ?
> Please, could you check on https://www.python.org/events/ , if you are an
> organizer, please add your event.
>
> If you think there is a missing event, please, send me the info via
> [email](mailto:steph...@wirtel.be) or via my [twitter
> account](https://twitter.com/matrixise) and I will add it on my slides.
>
> I would like to present your event.
>
> Thank you so much for your help.
>
> Stephane Wirtel
>
> [1] https://www.python-fosdem.org
>
> --
> Stéphane Wirtel - http://wirtel.be - @matrixise
> ___
> PSF-Community mailing list
> psf-commun...@python.org
> https://mail.python.org/mailman/listinfo/psf-community



-- 
Kind regards,

Danny W. Adair
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Tim Chase  writes:
>> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]
>
> As charmingly expressive as map() is, the wildly different behavior in
> py3 (it's a generator that evaluates lazily) vs py2 (it consumes the
> entire iterable in one go) leads me to avoid it in general,

Well, there's itertools.imap which maps lazily in py2.

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Peter Otten wrote, on January 09, 2017 6:51 AM
> 
> Deborah Swanson wrote:
> 
> > Even better, to get hold of all the records with the same
Description 
> > as the current row, compare them all, mark all but the different
ones 
> > for deletion, and then resume processing the records after the last 
> > one?
> 
> When you look at all fields for deduplication anyway there's no need
to 
> treat one field (Description) specially. Just
> 
> records = set(records)

I haven't worked with sets before, so this would be a good time to
start.

> should be fine. As the initial order is lost* you probably want to
sort 
> afterwards. The code then becomes
> 
> records = sorted(
> set(records), 
> key=operator.attrgetter("Description")
> )

Good, this is confirmation that 'sorted()' is the way to go. I want a 2
key sort, Description and Date, but I think I can figure out how to do
that.


> Now if you want to fill in missing values, you should probably do this

> before deduplication 

That's how my original code was written, to fill in missing values as
the very last thing before saving to csv.

> -- and the complete() function introduced in
>https://mail.python.org/pipermail/python-list/2016-December/717847.html

> can be adapted to work with namedtuples instead of dicts.

Ah, your defaultdict suggestion. Since my original comprows() function
to fill in missing values is now broken after the rest of the code was
rewritten for namedtuples (I just commented it out to test the
namedtuples version), this would be a good time to look at defaultdict.

> (*) If you want to preserve the initial order you can use a 
> collections.OrderedDict instead of the set.

OrderedDict is another thing I haven't used, but would love to, so I
think I'll try both the set and the OrderedDict, and see which one is
best here.

Thanks again Peter, all your help is very much appreciated.

Deborah

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Tim Chase wrote, on January 09, 2017 6:22 AM
> 
> On 2017-01-08 22:58, Deborah Swanson wrote:
> > 1) I have a section that loops through the sorted data, compares two

> > adjacent rows at a time, and marks one of them for deletion if the 
> > rows are identical.
> > and my question is whether there's a way to work with two adjacent 
> > rows without using subscripts?
> 
> I usually wrap the iterable in something like
> 
>   def pairwise(it):
> prev = next(it)
> for thing in it:
>   yield prev, thing
>   prev = thing
> 
>   for prev, cur in pairwise(records):
> compare(prev, cur)
> 
> which I find makes it more readable.
> 
> -tkc

This looks very useful, and comparing two adjacent rows is something I
do often. Thanks Tim!

Deborah

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
breamore...@gmail.com wrote, on January 09, 2017 8:32 AM
> 
> On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote:
> > On 2017-01-08 22:58, Deborah Swanson wrote:
> > > 1) I have a section that loops through the sorted data, compares
two 
> > > adjacent rows at a time, and marks one of them for deletion if the

> > > rows are identical.
> > > and my question is whether there's a way to work with two adjacent

> > > rows without using subscripts?
> > 
> > I usually wrap the iterable in something like
> > 
> >   def pairwise(it):
> > prev = next(it)
> > for thing in it:
> >   yield prev, thing
> >   prev = thing
> > 
> >   for prev, cur in pairwise(records):
> > compare(prev, cur)
> > 
> > which I find makes it more readable.
> > 
> > -tkc
> 
> Or from 
> https://docs.python.org/3/library/itertools.ht> ml#itertools-recipes:-
> 
> def pairwise(iterable):
> "s -> (s0,s1), (s1,s2), (s2, s3), ..."
> a, b = tee(iterable)
> next(b, None)
> return zip(a, b)
> 
> This and many other recipes are available in the 
> more-itertools module which is on pypi.

Thanks, I'll keep this since I seem to do pairwise comparisons a lot.
I'm going to try using set or OrderedDict for the current problem, per
Peter's suggestion, but if I can't make that work, this surely will.

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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Rhodri James

On 09/01/17 21:40, Deborah Swanson wrote:

Peter Otten wrote, on January 09, 2017 6:51 AM


records = sorted(
set(records),
key=operator.attrgetter("Description")
)


Good, this is confirmation that 'sorted()' is the way to go. I want a 2
key sort, Description and Date, but I think I can figure out how to do
that.


There's a handy trick that you can use because the sorting algorithm is 
stable.  First, sort on your secondary key.  This will leave the data in 
the wrong order, but objects with the same primary key will be in the 
right order by secondary key relative to each other.


Then sort on your primary key.  Because the sorting algorithm is stable, 
it won't disturb the relative order of objects with the same primary 
key, giving you the sort that you want!


So assuming you want your data sorted by date, and then by description 
within the same date, it's just:


records = sorted(
sorted(
set(records),
key=operator.attrgetter("Description")
),
key=operator.attrgetter("Date")
)

--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


RE: Clickable hyperlinks

2017-01-09 Thread Deborah Swanson
Tim Chase wrote, on January 09, 2017 5:53 AM
> 
> On 2017-01-09 05:00, Deborah Swanson wrote:
> > Code does in fact have the power to control what happens
> > in the console. How do you think Linux does it on their terminals
with 
> > clickable links? Granted, the code may have to specify parameters
for 
> > a particular console, but I certainly wasn't asking for universal
code 
> > that would work with any console.
> 
> Only to a degree.  Some consoles sniff the text sent to them 
> for particular URL-like patterns and linkify them for you.  
> All you have to do is print a URL that its pattern-matching 
> identifies:
> 
>   print("http://example.com";)
> 
> However, as Rhodri details, *this is a feature of the 
> terminal emulator*. I've got a smattering of terminal 
> emulators at my disposal and many don't auto-linkify (xterm, 
> rxvt, the base non-X terminal) while others do (Gnome 
> terminal).  And in the ones where it doesn't work, it's 
> because it's the *terminal* that doesn't support linkifying, 
> so no amount of work on the application's part will make it linkify.

Ok, here is the crux of this thread's communication problem. I didn't
ask, or particularly care for all these lectures on the technology of
terminal emulators. I asked how to code Python to make clickable links.

Since all of you are quite obviously passionate about terminal
emulators, I would suggest that you start your own thread. Except that
it would have nothing to do with Python, and would be OT for this list.
What you're doing instead is thread jacking, hijacking a thread to talk
about a different topic. And in every list I've ever been on, thread
jacking is considered to be very rude. And it's also confusing if the
two topics are similar, which is exactly what happened here.

> > The console is a dead thing, it has no mind or soul to choose 
> > anything. Surely an educated person would know that.
> 
> Pretty much every quality system administrator I know uses 
> the terminal.  Just about all of the best devs I know use the 
> terminal. Microsoft added Powershell because of demand. They 
> added Ubuntu/bash support because of demand.  It allows for 
> powerful automation that would otherwise require writing 
> full-fledged scripts.  There is nothing dead about it.
> 
> I'm not sure where you get your baseless claim that "an 
> educated person would know that", since someone who had taken 
> the time to study the state of terminal use would see that is 
> far from dead.
> 
> -tkc

You are an ardently passionate and loyal bunch, aren't you.

I wasn't using "dead" in the figurative sense of no longer in use, I was
using it literally, as I clarified by saying "it has no mind or soul to
choose anything". A console (or terminal) is not a living thing with a
brain and consciousness, and its only capacity to choose anything is
dictated by the conditionals programmed into it. I've never heard of a
console (or terminal) that could choose to respond to some clicks and
not to others, have you? No, consoles have no minds of their own to
choose anything. This is what "an educated person would know" means in
the sentence I wrote.

It's not a good practice to twist other people's words to mean what you
want them to mean, while ignoring what they meant by them. Though it is
a convenient way to invent an argument where there is none, and that's
exactly what's happened here.

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Ben Bacarisse
Steven D'Aprano  writes:

> Suppose you have an expensive calculation that gets used two or more times in 
> a 
> loop. The obvious way to avoid calculating it twice in an ordinary loop is 
> with 
> a temporary variable:
>
> result = []
> for x in data:
> tmp = expensive_calculation(x)
> result.append((tmp, tmp+1))
>
>
> But what if you are using a list comprehension? Alas, list comps don't let 
> you 
> have temporary variables, so you have to write this:
>
>
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
>
>
> Or do you? ... no, you don't!
>
>
> [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]]
>
>
> I can't decide whether that's an awesome trick or a horrible hack...

Nor I (but then I'm not really a Pythonista).  However I would use a
lambda as the natural way to create a local binding:

  [(lambda tmp: (tmp, tmp+1))(expensive_calculation(x)) for x in data]

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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
Rhodri James wrote:

> On 09/01/17 21:40, Deborah Swanson wrote:
>> Peter Otten wrote, on January 09, 2017 6:51 AM
>>>
>>> records = sorted(
>>> set(records),
>>> key=operator.attrgetter("Description")
>>> )
>>
>> Good, this is confirmation that 'sorted()' is the way to go. I want a 2
>> key sort, Description and Date, but I think I can figure out how to do
>> that.
> 
> There's a handy trick that you can use because the sorting algorithm is
> stable.  First, sort on your secondary key.  This will leave the data in
> the wrong order, but objects with the same primary key will be in the
> right order by secondary key relative to each other.
> 
> Then sort on your primary key.  Because the sorting algorithm is stable,
> it won't disturb the relative order of objects with the same primary
> key, giving you the sort that you want!
> 
> So assuming you want your data sorted by date, and then by description
> within the same date, it's just:
> 
> records = sorted(
>  sorted(
>  set(records),
>  key=operator.attrgetter("Description")
>  ),
>  key=operator.attrgetter("Date")
> )

While stable sort is nice in this case you can just say

key=operator.attrgetter("Description", "Date")

Personally I'd only use sorted() once and then switch to the sort() method.


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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread breamoreboy
On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote:
> On 2017-01-09 08:31, breamoreboy wrote:
> > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote:
> > > I usually wrap the iterable in something like
> > > 
> > >   def pairwise(it):
> > > prev = next(it)
> > > for thing in it:
> > >   yield prev, thing
> > >   prev = thing
> > 
> > Or from
> > https://docs.python.org/3/library/itertools.html#itertools-recipes:-
> > 
> > def pairwise(iterable):
> > "s -> (s0,s1), (s1,s2), (s2, s3), ..."
> > a, b = tee(iterable)
> > next(b, None)
> > return zip(a, b)
> > 
> > This and many other recipes are available in the more-itertools
> > module which is on pypi. 
> 
> Ah, helpful to not have to do it from scratch each time.  Also, I see
> several others that I've coded up from scratch (particularly the
> partition() and first_true() functions).
> 
> I usually want to make sure it's tailored for my use cases. The above
> pairwise() is my most common use case, but I occasionally want N-wise
> pairing

def ntuplewise(iterable, n=2):
args = tee(iterable, n)
loops = n - 1
while loops:
for _ in range(loops):
next(args[loops], None)
loops -= 1
return zip(*args)

> 
>   s -> (s0,s1,…sN), (s1,s2,…S{N+1}), (s2,s3,…s{N+2}), …
> 
> or to pad them out so either the leader/follower gets *all* of the
> values, with subsequent values being a padding value:
> 
>   # lst = [s0, s1, s2]
>   (s0,s1), (s1, s2), (s2, PADDING)

Use zip_longest instead of zip in the example code above.

>   # or
>   (PADDING, s0), (s0, s1), (s1, s2)

Haven't a clue off of the top of my head and I'm too darn tired to think about 
it :)

> 
> but it's good to have my common cases already coded & tested.
> 
> -tkc

Kindest regards.

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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
breamore...@gmail.com wrote:

> On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote:
>> On 2017-01-09 08:31, breamoreboy wrote:
>> > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote:
>> > > I usually wrap the iterable in something like
>> > > 
>> > >   def pairwise(it):
>> > > prev = next(it)
>> > > for thing in it:
>> > >   yield prev, thing
>> > >   prev = thing
>> > 
>> > Or from
>> > https://docs.python.org/3/library/itertools.html#itertools-recipes:->> > 
>> > def pairwise(iterable):
>> > "s -> (s0,s1), (s1,s2), (s2, s3), ..."
>> > a, b = tee(iterable)
>> > next(b, None)
>> > return zip(a, b)
>> > 
>> > This and many other recipes are available in the more-itertools
>> > module which is on pypi.
>> 
>> Ah, helpful to not have to do it from scratch each time.  Also, I see
>> several others that I've coded up from scratch (particularly the
>> partition() and first_true() functions).
>> 
>> I usually want to make sure it's tailored for my use cases. The above
>> pairwise() is my most common use case, but I occasionally want N-wise
>> pairing
> 
> def ntuplewise(iterable, n=2):
> args = tee(iterable, n)
> loops = n - 1
> while loops:
> for _ in range(loops):
> next(args[loops], None)
> loops -= 1
> return zip(*args)
> 
>> 
>>   s -> (s0,s1,…sN), (s1,s2,…S{N+1}), (s2,s3,…s{N+2}), …
>> 
>> or to pad them out so either the leader/follower gets *all* of the
>> values, with subsequent values being a padding value:
>> 
>>   # lst = [s0, s1, s2]
>>   (s0,s1), (s1, s2), (s2, PADDING)
> 
> Use zip_longest instead of zip in the example code above.
> 
>>   # or
>>   (PADDING, s0), (s0, s1), (s1, s2)
> 
> Haven't a clue off of the top of my head and I'm too darn tired to think
> about it :)

In both cases modify the iterable before feeding it to ntuplewise():

>>> PADDING = None
>>> N = 3
>>> items = range(5)
>>> list(ntuplewise(chain(repeat(PADDING, N-1), items), N))
[(None, None, 0), (None, 0, 1), (0, 1, 2), (1, 2, 3), (2, 3, 4)]
>>> list(ntuplewise(chain(items, repeat(PADDING, N-1)), N))
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, None), (4, None, None)]


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


Re: Clickable hyperlinks

2017-01-09 Thread Larry Martell
On Mon, Jan 9, 2017 at 5:33 PM, Deborah Swanson
 wrote:
> Ok, here is the crux of this thread's communication problem. I didn't
> ask, or particularly care for all these lectures on the technology of
> terminal emulators. I asked how to code Python to make clickable links.
>
> Since all of you are quite obviously passionate about terminal
> emulators, I would suggest that you start your own thread. Except that
> it would have nothing to do with Python, and would be OT for this list.
> What you're doing instead is thread jacking, hijacking a thread to talk
> about a different topic. And in every list I've ever been on, thread
> jacking is considered to be very rude. And it's also confusing if the
> two topics are similar, which is exactly what happened here.

Once you start a thread and put it out in the ether, it takes on a
life of its own, and cannot be controlled by anyone. That is true more
on this list then any other of the 100's I've been on. And it's why I
love it here.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Peter Otten wrote, on January 09, 2017 3:27 PM
> 
> While stable sort is nice in this case you can just say
> 
> key=operator.attrgetter("Description", "Date")
> 
> Personally I'd only use sorted() once and then switch to the 
> sort() method.

This works perfectly, thank you.

As I read the docs, the main (only?) difference between sorted() and
.sort() is that .sort() sorts the list in place.

Since I won't change the order of the records again after the sort, I'm
using 

records.sort(key=operator.attrgetter("Description", "Date"))

once, which also works perfectly.

So both sorted() and sort() can be used to sort namedtuples.  Good to
know!

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


RE: Clickable hyperlinks

2017-01-09 Thread Deborah Swanson
Larry Martell wrote, on January 09, 2017 4:11 PM
> 
> On Mon, Jan 9, 2017 at 5:33 PM, Deborah Swanson 
>  wrote:
> > Ok, here is the crux of this thread's communication problem. I
didn't 
> > ask, or particularly care for all these lectures on the technology
of 
> > terminal emulators. I asked how to code Python to make clickable
links.
> >
> > Since all of you are quite obviously passionate about terminal 
> > emulators, I would suggest that you start your own thread. Except
that 
> > it would have nothing to do with Python, and would be OT for this 
> > list. What you're doing instead is thread jacking, hijacking a
thread 
> > to talk about a different topic. And in every list I've ever been
on, 
> > thread jacking is considered to be very rude. And it's also
confusing 
> > if the two topics are similar, which is exactly what happened here.
> 
> Once you start a thread and put it out in the ether, it takes 
> on a life of its own, and cannot be controlled by anyone. 
> That is true more on this list then any other of the 100's 
> I've been on. And it's why I love it here.

Fair enough. I only suggested that they could have started their own
thread, but mainly just to point out that they would have been off-topic
if they did. I didn't demand that they do so, I just wanted them to
think about it.

I also prefer a loosely controlled list, and a freewheeling list is
ideal, but it only works well if people exercise basic politeness and
common sense.

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


Re: Clickable hyperlinks

2017-01-09 Thread Michael Torrie
On 01/09/2017 06:02 PM, Deborah Swanson wrote:
> Fair enough. I only suggested that they could have started their own
> thread, but mainly just to point out that they would have been off-topic
> if they did. I didn't demand that they do so, I just wanted them to
> think about it.

I don't see how it would be any more off-topic than your original query.
Which is to say, talking about Python's use of the console (beit a Win32
Console API window, a terminal emulator, or a PyCharm window) is on-topic.

> I also prefer a loosely controlled list, and a freewheeling list is
> ideal, but it only works well if people exercise basic politeness and
> common sense.

I agree.  Be aware that your own posts in this thread have bordered on
inflammatory on occasion.  Certainly your remark about "any educated
person would know that" could be taken in a very negative way.  I can't
remember everything I read in this thread, but I'm hard-pressed to find
examples where people were not polite to you, even though you pressed on
with certain lines of comment, especially when it was pointed out that
your question came from the wrong premise and idea of what the "console"
is.  I don't believe that someone pointing out flaws in your
understanding is inherently rude, though I think you took it as such.


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


Announcing txAWS 0.2.3.1

2017-01-09 Thread Jean-Paul Calderone
I've just release txAWS 0.2.3.1.  txAWS is a library for interacting with
Amazon Web Services (AWS) using Twisted.

AWSServiceEndpoint's ssl_hostname_verification's parameter now defaults to
True instead of False.  This affects all txAWS APIs which issue requests to
AWS endpoints.  For any application which uses the default
AWSServiceEndpoints, the server's TLS certificate will now be verified.

This resolves a security issue in which txAWS applications were vulnerable
to man-in-the-middle attacks which could either steal sensitive information
or, possibly, alter the AWS operation requested.

The new release is available on PyPI in source and wheel forms.  You can
also find txAWS at its new home on github, .

Special thanks to Least Authority Enterprises
() for
sponsoring the work to find and fix this issue and to publish this new
release.

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


Re: Clickable hyperlinks

2017-01-09 Thread Joel Goldstick
On Mon, Jan 9, 2017 at 8:33 PM, Michael Torrie  wrote:
> On 01/09/2017 06:02 PM, Deborah Swanson wrote:
>> Fair enough. I only suggested that they could have started their own
>> thread, but mainly just to point out that they would have been off-topic
>> if they did. I didn't demand that they do so, I just wanted them to
>> think about it.
>
> I don't see how it would be any more off-topic than your original query.
> Which is to say, talking about Python's use of the console (beit a Win32
> Console API window, a terminal emulator, or a PyCharm window) is on-topic.
>
>> I also prefer a loosely controlled list, and a freewheeling list is
>> ideal, but it only works well if people exercise basic politeness and
>> common sense.
>
> I agree.  Be aware that your own posts in this thread have bordered on
> inflammatory on occasion.  Certainly your remark about "any educated
> person would know that" could be taken in a very negative way.  I can't
> remember everything I read in this thread, but I'm hard-pressed to find
> examples where people were not polite to you, even though you pressed on
> with certain lines of comment, especially when it was pointed out that
> your question came from the wrong premise and idea of what the "console"
> is.  I don't believe that someone pointing out flaws in your
> understanding is inherently rude, though I think you took it as such.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Well put Michael

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Erik

On 10/01/17 00:54, Deborah Swanson wrote:

Since I won't change the order of the records again after the sort, I'm
using

records.sort(key=operator.attrgetter("Description", "Date"))

once, which also works perfectly.

So both sorted() and sort() can be used to sort namedtuples.  Good to
know!


As people keep saying, the object you have called 'records' is a *list* 
of namedtuple objects. It is not a namedtuple.


IIRC, you create it using a list comprehension which creates the 
records. A list comprehension always creates a list.


The sorted() function and the list.sort() method can be used to sort a 
list containing any objects - it's just a case of telling them how to 
obtain the key values to compare (which, in the case of simple attribute 
access which the namedtuple objects allow, "operator.attrgetter()" will 
do that). This is why sorting the list works for you.


You could sort objects of different types - but you might need to supply 
a function instead of operator.attrgetter() which looks at the type of 
each object and returns something that's obtained differently for each 
type (but which the sort function can compare).





When you say 'Foo = namedtuple("Foo", "spam ham")', you are creating a 
"factory" which is able to generate "Foo" objects for you.


When you say "x = Foo(1, 2)" you are using the factory to create an 
object for you which has its "spam" and "ham" attributes set to the 
values 1 and 2 respectively.


When you say "records = [Foo(x, y) for x, y in some_iterable()]", you 
are creating a list of such objects. This is the thing you are then sorting.




Does that make sense?

Regards, E.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Temporary variables in list comprehensions

2017-01-09 Thread Tim Chase
On 2017-01-09 13:16, Paul Rubin wrote:
> Tim Chase  writes:
> >> result = [(tmp, tmp+1) for tmp in map(expensive_calculation,
> >> data)]
> >
> > As charmingly expressive as map() is, the wildly different
> > behavior in py3 (it's a generator that evaluates lazily) vs py2
> > (it consumes the entire iterable in one go) leads me to avoid it
> > in general,
> 
> Well, there's itertools.imap which maps lazily in py2.

Yes, but it's one of those things that I have to remember to
distinguish which one I use based on whether I'm working in py2 or
py3.  Meanwhile, using a generator expression is readable (my #1
reason for using Python is its readability) and works across 2 and 3
without changes or thinking about it further.  So that's what I tend
to use.

-tkc


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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Paul Rubin
Ben Bacarisse  writes:
>   [(lambda tmp: (tmp, tmp+1))(expensive_calculation(x)) for x in data]

Nice.  The Haskell "let" expression is implemented as syntax sugar for
that, I believe.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Erik wrote, on January 09, 2017 5:47 PM
> As people keep saying, the object you have called 'records' 
> is a *list* 
> of namedtuple objects. It is not a namedtuple.
> 
> IIRC, you create it using a list comprehension which creates the 
> records. A list comprehension always creates a list.

Well no. The list is created with:

records.extend(Record._make(row) for row in rows)

I'm new to both namedtuples and list comprehensions, so I'm not exactly
sure if this statement is a list comprehension. It looks like it could
be. In any case I recreated records in IDLE and got

>>> type(records)


So it's a class, derived from list? (Not sure what the 'list' means.)
'records' is in fact a class, it has an fget method and data members
that I've used. And it behaves like a list sometimes, but many times
not.

The only reason I've hedged away from advice to treat records as a list
for sorting until I tried it for myself, was because of an awful lot of
strange behavior I've seen, while trying to do the same things with
namedtuples as I routinely do with scalars and lists. This is all new,
and until now, unexplored territory for me. And I generally avoid saying
I'm sure or outright agreeing with something unless I really do know it.
 
> The sorted() function and the list.sort() method can be used 
> to sort a 
> list containing any objects - it's just a case of telling them how to 
> obtain the key values to compare (which, in the case of 
> simple attribute 
> access which the namedtuple objects allow, 
> "operator.attrgetter()" will 
> do that). This is why sorting the list works for you.
> 
> You could sort objects of different types - but you might 
> need to supply 
> a function instead of operator.attrgetter() which looks at 
> the type of 
> each object and returns something that's obtained differently 
> for each 
> type (but which the sort function can compare).
> 
> 
> 
> 
> When you say 'Foo = namedtuple("Foo", "spam ham")', you are 
> creating a 
> "factory" which is able to generate "Foo" objects for you.
> 
> When you say "x = Foo(1, 2)" you are using the factory to create an 
> object for you which has its "spam" and "ham" attributes set to the 
> values 1 and 2 respectively.
> 
> When you say "records = [Foo(x, y) for x, y in some_iterable()]", you 
> are creating a list of such objects. This is the thing you 
> are then sorting.
> 
> 
> 
> Does that make sense?
> 
> Regards, E.

Perfect sense. And now that I've confirmed in code that both sorted()
and 
.sort() behave as hoped for with namedtuples, I couldn't be happier.  ;)

The only thing I don't think you have 100% correct is your assertion
that records is a list. And I'm really not sure now that

records.extend(Record._make(row) for row in rows) 

is a list comprehension. 

That's the last statement in the creation of 'records', and immediately
after that statement executes, the type function says the resulting
'records' is a class, probably derived from list, but it's not a
straight up list.

'records' is enough different that you can't assume across the board
that namedtuples created this way are equivalent to a list. You do run
into problems if you assume it behaves like a list, or even like
standard tuples, because it doesn't always. Believe me, when I first
started working with namedtuples, I got plenty snarled up debugging code
that was written assuming list behavior to know that a namedtuple of
namedtuples is not exactly a list. Or even exactly like a list.

But that's just a quibble. The important thing in this context is that
both .sort() and sorted() treat it like a list and DTRT.  And that's
very nice. ;)

Deborah

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


Re: Clickable hyperlinks

2017-01-09 Thread Tim Chase
On 2017-01-09 14:33, Deborah Swanson wrote:
> Ok, here is the crux of this thread's communication problem. I
> didn't ask, or particularly care for all these lectures on the
> technology of terminal emulators. I asked how to code Python to
> make clickable links.

The crux of the problem is that words mean things.  Specific words
have *specific* meanings.  When you say "make a clickable hyperlink
in a console", those words each have meanings.  Part of helping
people is coming to a common understanding.  In this case, the common
meaning is "I want to send some magical sequence to a terminal
emulator to make it treat the item as a clickable link."

To which anybody with experience in the matter answers exactly like
happened on the list:  that depends on your terminal emulator but is
entirely the domain of that emulator, and only within the context of
knowing which terminal emulator you're talking about, can any sort of
meaningful solution be given (if any).  You used words that carry a
particular technical meaning, but didn't use them in a way that made
much sense without taking the effort to clarify.

To round it out, you brought up your lengthy experience in the
computer field:

> I've only been online since 1992 (offline computing since 1972)

This usually suggests that you have been around enough to know that
these terms have precise meanings, and that you are wielding them in
the way others in the field use them.

So the roundabout discussion finally sussed out that you meant "I
want to send information to the PyCharm output area which I'm calling
a console (but is not necessarily an actual terminal emulator as the
technical word 'console' would usually be understood) and that output
area happens to know how to interpret some sequences as things that
are clickable.  How can I do that?"  Which then became "well, I don't
really even need to make clickable links, I just want to open some
target in a web browser."

And until folks on the list could understand your *intent* despite
the words you were using, it's a frustrating experience for all
involved.  So because of imprecise language, even if you didn't ask or
care about the terminology, *it matters if you want a viable answer*.


-tkc



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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread MRAB

On 2017-01-10 03:02, Deborah Swanson wrote:

Erik wrote, on January 09, 2017 5:47 PM

As people keep saying, the object you have called 'records'
is a *list*
of namedtuple objects. It is not a namedtuple.

IIRC, you create it using a list comprehension which creates the
records. A list comprehension always creates a list.


Well no. The list is created with:

records.extend(Record._make(row) for row in rows)

I'm new to both namedtuples and list comprehensions, so I'm not exactly
sure if this statement is a list comprehension. It looks like it could
be.

>
This is a list comprehension:

[Record._make(row) for row in rows]

and this is a generator expression:

(Record._make(row) for row in rows)

It needs the outer parentheses.

The .extend method will accept any iterable, including list comprehensions:

records.extend([Record._make(row) for row in rows])

and generator expressions:

records.extend((Record._make(row) for row in rows))

In the latter case, the generator expression is the only argument of the 
.extend method, and Python lets us drop the pair of parentheses:


records.extend(Record._make(row) for row in rows)

If there were another argument, it would be ambiguous and Python would 
complain.



In any case I recreated records in IDLE and got


type(records)



So it's a class, derived from list? (Not sure what the 'list' means.)
'records' is in fact a class, it has an fget method and data members
that I've used. And it behaves like a list sometimes, but many times
not.


Its type is 'list', so it's an instance of a list, i.e. it's a list!


The only reason I've hedged away from advice to treat records as a list
for sorting until I tried it for myself, was because of an awful lot of
strange behavior I've seen, while trying to do the same things with
namedtuples as I routinely do with scalars and lists. This is all new,
and until now, unexplored territory for me. And I generally avoid saying
I'm sure or outright agreeing with something unless I really do know it.


The sorted() function and the list.sort() method can be used
to sort a
list containing any objects - it's just a case of telling them how to
obtain the key values to compare (which, in the case of
simple attribute
access which the namedtuple objects allow,
"operator.attrgetter()" will
do that). This is why sorting the list works for you.

You could sort objects of different types - but you might
need to supply
a function instead of operator.attrgetter() which looks at
the type of
each object and returns something that's obtained differently
for each
type (but which the sort function can compare).




When you say 'Foo = namedtuple("Foo", "spam ham")', you are
creating a
"factory" which is able to generate "Foo" objects for you.

When you say "x = Foo(1, 2)" you are using the factory to create an
object for you which has its "spam" and "ham" attributes set to the
values 1 and 2 respectively.

When you say "records = [Foo(x, y) for x, y in some_iterable()]", you
are creating a list of such objects. This is the thing you
are then sorting.



Does that make sense?

Regards, E.


Perfect sense. And now that I've confirmed in code that both sorted()
and
.sort() behave as hoped for with namedtuples, I couldn't be happier.  ;)

The only thing I don't think you have 100% correct is your assertion
that records is a list. And I'm really not sure now that

records.extend(Record._make(row) for row in rows)

is a list comprehension.

That's the last statement in the creation of 'records', and immediately
after that statement executes, the type function says the resulting
'records' is a class, probably derived from list, but it's not a
straight up list.

'records' is enough different that you can't assume across the board
that namedtuples created this way are equivalent to a list. You do run
into problems if you assume it behaves like a list, or even like
standard tuples, because it doesn't always. Believe me, when I first
started working with namedtuples, I got plenty snarled up debugging code
that was written assuming list behavior to know that a namedtuple of
namedtuples is not exactly a list. Or even exactly like a list.

But that's just a quibble. The important thing in this context is that
both .sort() and sorted() treat it like a list and DTRT.  And that's
very nice. ;)

The list class has the .sort method, which sorts in-place. The 'sorted' 
function is a simple function that takes an iterable, iterates over it 
to build a list, sorts that list in-place, and then returns the list.


The oft-stated rule is that not every 2- or 3-line function needs to be 
a built-in, but 'sorted' is one of those cases where it's just nice to 
have it, a case of "practicality beats purity".


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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Ethan Furman

On 01/09/2017 07:02 PM, Deborah Swanson wrote:

Erik wrote, on January 09, 2017 5:47 PM



As people keep saying, the object you have called 'records'
is a *list*
of namedtuple objects. It is not a namedtuple.

IIRC, you create it using a list comprehension which creates the
records. A list comprehension always creates a list.


Well no. The list is created with:

records.extend(Record._make(row) for row in rows)

I'm new to both namedtuples and list comprehensions, so I'm not exactly
sure if this statement is a list comprehension. It looks like it could
be. In any case I recreated records in IDLE and got

--> type(records)


So it's a class, derived from list? (Not sure what the 'list' means.)


On the one hand, Deborah, I applaud your perseverance.  On the other, it seems 
as if you trying to run before you can walk.  I know tutorials can be boring, 
but you really should go through one so you have a basic understanding of the 
fundamentals.

Working in the REPL (the python console), we can see:

Python 3.4.0 (default, Apr 11 2014, 13:05:18)
...
--> type(list)

-->
--> type(list())

--> type([1, 2, 3])


So the `list` type is 'type', and the type of list instances is 'class list'.

Your records variable is an instance of a list filled with instances of a 
namedtuple, 'Record'.  One cannot sort a namedtuple, but one can sort a list of 
namedtuples -- which is what you are doing.

As I said earlier, I admire your persistence -- but take some time and learn 
the basic vocabulary as that will make it much easier for you to ask questions, 
and for us to give you meaningful answers.

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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Erik

On 10/01/17 03:02, Deborah Swanson wrote:

Erik wrote, on January 09, 2017 5:47 PM

IIRC, you create it using a list comprehension which creates the
records. A list comprehension always creates a list.


Well no. The list is created with:

records.extend(Record._make(row) for row in rows)


No, the list is _extended_ by that code. The list is _created_ with a 
line that will say something like "records = []" or "records = list()" 
(or "records = ").


It's nice to see you agree that it's a list though. Oh, hold on ... ;)


I'm not exactly
sure if this statement is a list comprehension.


No, it's not. I was remembering an old message where someone suggested 
using the _make() method and that was expressed as a list comprehension.


What you have there is a call to list.extend() passing a _generator_ 
comprehension as its only parameter (which in this case you can consider 
to be equivalent to a list comprehension as all of the data are 
generated greedily). You see that I said "list.extend()". That's because 
'records' is a list.



type(records)




Yes, it's an instance of the list class. A list object. A list.

>>> type(list())

>>> type([])

>>> class foo: pass
...
>>> type(foo())

>>>

... type() will tell you what class your object is an instance of. 
"" tells you that your object is a list.



And it behaves like a list sometimes, but many times
not.


I think that's impossible. I'm 100% sure it's a list. Please give an 
example of when 'records' does not behave like a list.



The only thing I don't think you have 100% correct is your assertion
that records is a list.


It's a list.


But that's just a quibble. The important thing in this context is that
both .sort() and sorted() treat it like a list and DTRT.


That's because it's a list :)

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
MRAB wrote, on January 09, 2017 7:37 PM
> 
> On 2017-01-10 03:02, Deborah Swanson wrote:
> > Erik wrote, on January 09, 2017 5:47 PM
> >> As people keep saying, the object you have called 'records' is a 
> >> *list* of namedtuple objects. It is not a namedtuple.
> >>
> >> IIRC, you create it using a list comprehension which creates the 
> >> records. A list comprehension always creates a list.
> >
> > Well no. The list is created with:
> >
> > records.extend(Record._make(row) for row in rows)
> >
> > I'm new to both namedtuples and list comprehensions, so I'm not 
> > exactly sure if this statement is a list comprehension. It 
> looks like 
> > it could be.
>  >
> This is a list comprehension:
> 
>  [Record._make(row) for row in rows]
> 
> and this is a generator expression:
> 
>  (Record._make(row) for row in rows)
> 
> It needs the outer parentheses.
> 
> The .extend method will accept any iterable, including list 
> comprehensions:
> 
>  records.extend([Record._make(row) for row in rows])
> 
> and generator expressions:
> 
>  records.extend((Record._make(row) for row in rows))
> 
> In the latter case, the generator expression is the only 
> argument of the 
> .extend method, and Python lets us drop the pair of parentheses:
> 
>  records.extend(Record._make(row) for row in rows)
> 
> If there were another argument, it would be ambiguous and 
> Python would 
> complain.

Appreciate your explanation of why this statement looks like a list
comprehension, but it isn't one.
 
> > In any case I recreated records in IDLE and got
> >
>  type(records)
> > 
> >
> > So it's a class, derived from list? (Not sure what the 
> 'list' means.) 

>>> [1,2,3]
[1, 2, 3]
>>> type(_)


So it is a list, despite not being made by a list comprehension and
despite its non-listlike behaviors. Guess I've never looked at the type
of a list before, probably because lists are so obvious by looking at
them.

> > 'records' is in fact a class, it has an fget method and data members

> > that I've used. And it behaves like a list sometimes, but many times
not.
> >
> Its type is 'list', so it's an instance of a list, i.e. it's a list!

As testified by IDLE above!  ;)  A list of namedtuples may be an
instance of a list, but it doesn't always behave like a list of lists.
For example, if you want to modify an element of a record in records,
you can't just say 

'record.Location = Tulsa' 

like you can say 

'record[Location] = Tulsa'

because each record is very much like a tuple, and tuples are immutable.
You have to use the _replace function:

record._replace(Location) = Tulsa

This is very unlike a list of lists. Only the outer data structure is a
list, and inside it's all namedtuples.

So it's not a list of lists, it's a list of namedtuples.  But .sort and 
sorted() DTRT, and that's valuable.

> > The only reason I've hedged away from advice to treat records as a 
> > list for sorting until I tried it for myself, was because of an
awful 
> > lot of strange behavior I've seen, while trying to do the same
things 
> > with namedtuples as I routinely do with scalars and lists. This is
all 
> > new, and until now, unexplored territory for me. And I generally
avoid 
> > saying I'm sure or outright agreeing with something unless I really
do 
> > know it.
> >
> >> The sorted() function and the list.sort() method can be used to
sort 
> >> a list containing any objects - it's just a case of telling them
how 
> >> to obtain the key values to compare (which, in the case of
> >> simple attribute access which the namedtuple objects allow,
> >> "operator.attrgetter()" will
> >> do that). This is why sorting the list works for you.
> >>
> >> You could sort objects of different types - but you might need to 
> >> supply a function instead of operator.attrgetter() which looks at
> >> the type of
> >> each object and returns something that's obtained differently
> >> for each
> >> type (but which the sort function can compare).
> >>
> >>
> >>
> >>
> >> When you say 'Foo = namedtuple("Foo", "spam ham")', you 
> are creating 
> >> a "factory" which is able to generate "Foo" objects for you.
> >>
> >> When you say "x = Foo(1, 2)" you are using the factory to 
> create an 
> >> object for you which has its "spam" and "ham" attributes 
> set to the 
> >> values 1 and 2 respectively.
> >>
> >> When you say "records = [Foo(x, y) for x, y in 
> some_iterable()]", you 
> >> are creating a list of such objects. This is the thing you 
> are then 
> >> sorting.
> >>
> >>
> >>
> >> Does that make sense?
> >>
> >> Regards, E.
> >
> > Perfect sense. And now that I've confirmed in code that both
sorted() and
> > .sort() behave as hoped for with namedtuples, I couldn't be happier.

> > ;)
> >
> > The only thing I don't think you have 100% correct is your assertion

> > that records is a list. And I'm really not sure now that 
> > 
> > records.extend(Record._make(row) for row in rows)
> >
> > is a list comprehension.
> >
> > That's the last statement 

Enum with only a single member

2017-01-09 Thread Steven D'Aprano
Is it silly to create an enumeration with only a single member? That is, a 
singleton enum?

from enum import Enum

class Unique(Enum):
FOO = auto()


The reason I ask is that I have two functions that take an enum argument. The 
first takes one of three enums:

class MarxBros(Enum):
GROUCHO = 1
CHICO = 2
HARPO = 3

and the second takes *either* one of those three, or a fourth distinct value. 
So in my two functions, I have something like this:


def spam(arg):
if isinstance(arg, MarxBros): 
...


def ham(arg):
if isinstance(arg, MarxBros) or arg is Unique.FOO:
...


Good, bad or indifferent?



-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Ethan Furman wrote, on January 09, 2017 8:01 PM
> 
> On 01/09/2017 07:02 PM, Deborah Swanson wrote:
> > Erik wrote, on January 09, 2017 5:47 PM
> 
> >> As people keep saying, the object you have called 'records' is a 
> >> *list* of namedtuple objects. It is not a namedtuple.
> >>
> >> IIRC, you create it using a list comprehension which creates the 
> >> records. A list comprehension always creates a list.
> >
> > Well no. The list is created with:
> >
> > records.extend(Record._make(row) for row in rows)
> >
> > I'm new to both namedtuples and list comprehensions, so I'm not 
> > exactly sure if this statement is a list comprehension. It 
> looks like 
> > it could be. In any case I recreated records in IDLE and got
> >
> >--> type(records)
> > 
> >
> > So it's a class, derived from list? (Not sure what the 
> 'list' means.)
> 
> On the one hand, Deborah, I applaud your perseverance.  On 
> the other, it seems as if you trying to run before you can 
> walk.  I know tutorials can be boring, but you really should 
> go through one so you have a basic understanding of the fundamentals.

I actually have had a solid foundation of study in 2 terms of MIT's
introductory Python courses. But they can't cover everything in that
short a time.

> Working in the REPL (the python console), we can see:
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:18)
> ...
> --> type(list)
> 
> -->
> --> type(list())
> 
> --> type([1, 2, 3])
> 
> 
> So the `list` type is 'type', and the type of list instances 
> is 'class list'.

I just saw that while replying to MRAB. 'records' has type list, but
it's only the outer data structure that's a list. Inside, all the
records are namedtuples, and I think that accounts for behaviors that
are unlike a list of lists. (And the reason I was reluctant to accept
that it could be sorted until I tried it for myself.) The method calls I
was able to use were from the namedtuples, not the list of namedtuples.

> Your records variable is an instance of a list filled with 
> instances of a namedtuple, 'Record'.  One cannot sort a 
> namedtuple, but one can sort a list of namedtuples -- which 
> is what you are doing.

Yes, I think we've got that straight now.

> As I said earlier, I admire your persistence -- but take some 
> time and learn the basic vocabulary as that will make it much 
> easier for you to ask questions, and for us to give you 
> meaningful answers.
> 
> --
> ~Ethan~

As I mentioned, I have completed MIT's 2 introductory Python courses
with final grades of 98% and 97%.  What tutorials do you think would
significantly add to that introduction?

It's true that I didn't spend much time in the forums while I was taking
those courses, so this is the first time I've talked with people about
Python this intensively. But I'm a good learner and I'm picking up a lot
of it pretty quickly. People on the list also talk and comprehend
differently than people in the MIT courses did, so I have to become
accustomed to this as well. And the only place to learn that is right
here. 

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


Re: Enum with only a single member

2017-01-09 Thread Ethan Furman

On 01/09/2017 08:43 PM, Steven D'Aprano wrote:


Is it silly to create an enumeration with only a single member? That is, a
singleton enum?

from enum import Enum

class Unique(Enum):
 FOO = auto()


The reason I ask is that I have two functions that take an enum argument. The
first takes one of three enums:

class MarxBros(Enum):
 GROUCHO = 1
 CHICO = 2
 HARPO = 3

and the second takes *either* one of those three, or a fourth distinct value.
So in my two functions, I have something like this:


def spam(arg):
 if isinstance(arg, MarxBros):
 ...


def ham(arg):
 if isinstance(arg, MarxBros) or arg is Unique.FOO:
 ...


Good, bad or indifferent?


Well, the basic purpose of Enum is give meaningful names to otherwise magic 
constants, so while it certainly looks a bit unusual to have only one item in 
the enumeration, if that group naturally has only one item then sure, go for it!

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Erik wrote, on January 09, 2017 8:06 PM
> 
> On 10/01/17 03:02, Deborah Swanson wrote:
> > Erik wrote, on January 09, 2017 5:47 PM
> >> IIRC, you create it using a list comprehension which creates the 
> >> records. A list comprehension always creates a list.
> >
> > Well no. The list is created with:
> >
> > records.extend(Record._make(row) for row in rows)
> 
> No, the list is _extended_ by that code. The list is _created_ with a 
> line that will say something like "records = []" or "records 
> = list()" 
> (or "records = ").

The list was created with this code:

infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 2017
in.csv")
rows = csv.reader(infile)
fieldnames = next(rows)
Record = namedtuple("Record", fieldnames)
records = [Record._make(fieldnames)]
records.extend(Record._make(row) for row in rows)

I just pulled out the .extend statement to show you because that's what
looks like a list comprehension, but turns out not to be one.  We still
get a list though, on that we agree.  ;)
 
> It's nice to see you agree that it's a list though. Oh, hold on ... ;)
> 
> > I'm not exactly
> > sure if this statement is a list comprehension.
> 
> No, it's not. I was remembering an old message where someone 
> suggested 
> using the _make() method and that was expressed as a list 
> comprehension.
> 
> What you have there is a call to list.extend() passing a _generator_ 
> comprehension as its only parameter (which in this case you 
> can consider 
> to be equivalent to a list comprehension as all of the data are 
> generated greedily). You see that I said "list.extend()". 
> That's because 
> 'records' is a list.
> 
>  type(records)
> > 
> 
> Yes, it's an instance of the list class. A list object. A list.
> 
>  >>> type(list())
> 
>  >>> type([])
> 
>  >>> class foo: pass
> ...
>  >>> type(foo())
> 
>  >>>
> 
> ... type() will tell you what class your object is an instance of. 
> "" tells you that your object is a list.
> 
> > And it behaves like a list sometimes, but many times
> > not.
> 
> I think that's impossible. I'm 100% sure it's a list. Please give an 
> example of when 'records' does not behave like a list.

I gave an example in one of my last two replies to other people. The
thing is that it's a list, but it's not a list of lists. It's a list of
namedtuples, and the non-listlike behaviors appear when I'm directly
working with the namedtuples.

> > The only thing I don't think you have 100% correct is your assertion

> > that records is a list.
> 
> It's a list.

I agree, now. 
 
> > But that's just a quibble. The important thing in this context is
that 
> > both .sort() and sorted() treat it like a list and DTRT.
> 
> That's because it's a list :)
> 
> E.

It is!  A list of namedtuples that is, not a list of lists. sorted() and
.sort work because they only interact with the outer data structure,
which is a list.

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


Is enum iteration order guaranteed?

2017-01-09 Thread Steven D'Aprano
The docs say that enums can be iterated over, but it isn't clear to me whether 
they are iterated over in definition order or value order.

If I have:

class MarxBros(Enum):
GROUCHO = 999
CHICO = 5
HARPO = 11
ZEPPO = auto()
GUMMO = -1

GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros)


is that guaranteed to do the right thing?

i.e. GROUCHO is MarxBros.GROUCHO, and similarly for the other members.




On that related note, how would people feel about a method that injects enums 
into the given namespace?

MarxBros._inject_(globals())

although maybe 

from MarxBros import *

would be more familiar syntax :-)





-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson

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


Re: Enum with only a single member

2017-01-09 Thread Jussi Piitulainen
Steven D'Aprano writes:

> Is it silly to create an enumeration with only a single member? That
> is, a singleton enum?
>
> from enum import Enum
>
> class Unique(Enum):
> FOO = auto()
>
>
> The reason I ask is that I have two functions that take an enum
> argument. The first takes one of three enums:
>
> class MarxBros(Enum):
> GROUCHO = 1
> CHICO = 2
> HARPO = 3
>
> and the second takes *either* one of those three, or a fourth distinct
> value. So in my two functions, I have something like this:
>
>
> def spam(arg):
> if isinstance(arg, MarxBros): 
> ...
>
>
> def ham(arg):
> if isinstance(arg, MarxBros) or arg is Unique.FOO:
> ...
>
>
> Good, bad or indifferent?

With a class of its own, the single value can be identified by its class
uniformly with the other relevant values.

def ham(arg):
if insinstance(arg, (MarxBros, Unique)):
...

Seems good to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is enum iteration order guaranteed?

2017-01-09 Thread Ethan Furman

On 01/09/2017 09:18 PM, Steven D'Aprano wrote:


The docs say that enums can be iterated over, but it isn't clear to me whether
they are iterated over in definition order or value order.

If I have:

class MarxBros(Enum):
 GROUCHO = 999
 CHICO = 5
 HARPO = 11
 ZEPPO = auto()
 GUMMO = -1

GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros)


In Python 3 it is always definition order.  In Python 2 (using the enum34 [1] 
backport), the order is either by value if possible, or alphabetical  if not -- 
unless you use the _order_ attribute to set it:

class MarkBros(Enum):
_order_ = 'GROUCHO CHICO HARGPO ZEPPO GUMMO'
GROUCHO = 999
...


On that related note, how would people feel about a method that injects enums
into the given namespace?

MarxBros._inject_(globals())

although maybe

from MarxBros import *

would be more familiar syntax :-)


Or you can do globals().update(MarxBros.__members__).

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


Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Ethan Furman

On 01/09/2017 08:51 PM, Deborah Swanson wrote:

Ethan Furman wrote, on January 09, 2017 8:01 PM



As I said earlier, I admire your persistence -- but take some
time and learn the basic vocabulary as that will make it much
easier for you to ask questions, and for us to give you
meaningful answers.


As I mentioned, I have completed MIT's 2 introductory Python courses
with final grades of 98% and 97%.  What tutorials do you think would
significantly add to that introduction?


The Python version of "Think like a computer scientist" is good.  Otherwise, 
ask the list for recommendations.  I'm not suggesting more advanced topics, but rather 
basic topics such as how the REPL works, how to tell what objects you have, how to find 
the methods those objects have, etc.
 

It's true that I didn't spend much time in the forums while I was taking
those courses, so this is the first time I've talked with people about
Python this intensively. But I'm a good learner and I'm picking up a lot
of it pretty quickly. People on the list also talk and comprehend
differently than people in the MIT courses did, so I have to become
accustomed to this as well. And the only place to learn that is right
here.


Indeed.

The issue I (and others) see, though, is more along the lines of basic 
understanding: you seemed to think that a list of lists should act the same as 
a list of tuples, even though lists and tuples are not the same thing.  It's 
like expecting a basket of oranges to behave like a basket of avocados. ;)

As you say, you're making good progress.

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


Re: Is enum iteration order guaranteed?

2017-01-09 Thread Steven D'Aprano
On Tuesday 10 January 2017 16:55, Ethan Furman wrote:

> On 01/09/2017 09:18 PM, Steven D'Aprano wrote:
> 
>> The docs say that enums can be iterated over, but it isn't clear to me
>> whether they are iterated over in definition order or value order.
>>
>> If I have:
>>
>> class MarxBros(Enum):
>>  GROUCHO = 999
>>  CHICO = 5
>>  HARPO = 11
>>  ZEPPO = auto()
>>  GUMMO = -1
>>
>> GROUCHO, CHICO, HARPO, ZEPPO, GUMMO = list(MarxBros)
> 
> In Python 3 it is always definition order.

I only care about Python 3 for this.

Did I miss something in the docs, or should this be added?


[...]
>> On that related note, how would people feel about a method that injects
>> enums into the given namespace?
[...]
> Or you can do globals().update(MarxBros.__members__).

Sweet!



-- 
Steven
"Ever since I learned about confirmation bias, I've been seeing 
it everywhere." - Jon Ronson

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


RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Ethan Furman wrote, on January 09, 2017 10:06 PM
> 
> On 01/09/2017 08:51 PM, Deborah Swanson wrote:
> > Ethan Furman wrote, on January 09, 2017 8:01 PM
> 
> >> As I said earlier, I admire your persistence -- but take some time 
> >> and learn the basic vocabulary as that will make it much easier for

> >> you to ask questions, and for us to give you meaningful answers.
> >
> > As I mentioned, I have completed MIT's 2 introductory Python courses

> > with final grades of 98% and 97%.  What tutorials do you think would

> > significantly add to that introduction?
> 
> The Python version of "Think like a computer scientist" is 
> good.  Otherwise, ask the list for recommendations.  I'm not 
> suggesting more advanced topics, but rather basic topics such 
> as how the REPL works, how to tell what objects you have, how 
> to find the methods those objects have, etc.

I'm working on all of that, and I've been taking physics, chem, computer
science and theoretical mathematics (straight 4.0s my last 2 years,
graduated summa cum laude), but it's been a couple of decades since my
last brick building university course. It's coming back fast, but
there's a lot I'm still pulling out of cobwebs. I basically know the
tools to use for the things you mention, but in the online coursework
I've done in the past year, we didn't need to use them because they told
us everything. So that's another thing I'm doing catch up on. Really
shouldn't take too long, it's not that complicated or difficult.

> > It's true that I didn't spend much time in the forums while I was 
> > taking those courses, so this is the first time I've talked with 
> > people about Python this intensively. But I'm a good learner and I'm

> > picking up a lot of it pretty quickly. People on the list also talk 
> > and comprehend differently than people in the MIT courses did, so I 
> > have to become accustomed to this as well. And the only place to
learn 
> > that is right here.
> 
> Indeed.
> 
> The issue I (and others) see, though, is more along the lines 
> of basic understanding: you seemed to think that a list of 
> lists should act the same as a list of tuples, even though 
> lists and tuples are not the same thing.  It's like expecting 
> a basket of oranges to behave like a basket of avocados. ;)
> 
> As you say, you're making good progress.
> 
> --
> ~Ethan~

I'm sorry, I didn't think a list of namedtuples would be like a list of
lists when I wrote to Erik just a bit ago today, but I sort of did when
I first started trying to use them a couple days ago. And to the extent
I was pretty sure they weren't the same, I still didn't know in what
ways they were different. So when people ask me questions about why I
did things the way I did, I try to explain that I didn't know certain
things then, but I know them now. I'm guessing that you (and those who
see things like you do) might not be used to working with quick learners
who make mistakes at first but catch up with them real fast, or you're
very judgemental about people who make mistakes, period. I certainly
don't care if you want to judge me, you're entitled to your opinion. One
of my MIT professors was always screwing up little things, even things
he knew inside out. Some people are like that, and I think I might be
one of them, although I'm really not as bad as he was.

So I guess you should just do your thing and I'll do mine. I don't
promise that I'll ever get to the point where I never set a foot wrong.
I know some people can do that, and I hope someday I will. But then I
remember the MIT professor, who I really learned a lot from, despite all
his flub-ups, and that I might be a little bit like him. Takes all
kinds, and I think in the end what will count is the quality of my
finished work (which has always been excellent), and not the messy
process to get there.

It shocked me the first time someone on this list jumped down my throat
for a momentary lapse on my part, as if I was a total idiot and knew
nothing, but I'm sort of getting used to that too. It must be nice to be
so perfect, I guess.

Deborah

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


Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka

On 09.01.17 12:46, Paul Rubin wrote:

Serhiy Storchaka  writes:

gen = (expensive_calculation(x) for x in data)
result = [(tmp, tmp + 1) for tmp in gen]


result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]


Yes, of course, but only in the case of one-argument function 
expensive_calculation(). If this is just an expensive expression or need 
to pass multiple arguments to an expensive function, a generator 
expression could look simpler than a lambda or a local function.



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