Re: Iterating over dict is slower than iterating over iter(dict)?

2020-07-18 Thread Chris Angelico
On Sun, Jul 19, 2020 at 8:56 AM Marco Sulla wrote: > > ... oh my ... Sure, thank you. > Thinking positive, I wasted a lot of hours, but I discovered > timeit.Timer.autorange > Nah... You *spent* a lot of hours, almost certainly enjoyably, and along the way, you learned things! That's not wasted

Re: Iterating over dict is slower than iterating over iter(dict)?

2020-07-18 Thread Marco Sulla
x in dict", "stmt": "for x in it: pass", "setup": """ > > o = {k:k for k in range($size)} > > it = o > > """}, > > {"name": "for x in iter(dict)", "stmt": "for x

Re: Iterating over dict is slower than iterating over iter(dict)?

2020-07-18 Thread Chris Angelico
aningless :) > benchmarks = ( > {"name": "for x in dict", "stmt": "for x in it: pass", "setup": """ > o = {k:k for k in range($size)} > it = o > """}, > {"name": "for x in iter

Iterating over dict is slower than iterating over iter(dict)?

2020-07-18 Thread Marco Sulla
: 1.008e-05 Name: `for x in iter(dict)`;Size:8; Time: 1.845e-08 Name: `for x in iter(dict)`;Size: 1000; Time: 1.844e-08 Name: `iter(dict)`; Size:8; Time: 5.260e-08 Name: `iter(dict)`; Size: 1000; Time: 5.262e-08 Environment: marco@buzz:~/sources/cpython

Re: Why inspect.isclass says iter() a class?

2019-04-11 Thread Terry Reedy
On 4/10/2019 3:17 PM, Arup Rakshit wrote: From docs https://docs.python.org/3/library/itertools.html#itertools.chain I see that itertools.chain is defined as a function. Because that is what the itertools functions are. (The chain entry does not use 'function', but the beginning of the doc do

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread Chris Angelico
On Thu, Apr 11, 2019 at 3:31 PM Gregory Ewing wrote: > > Chris Angelico wrote: > > At the moment, it isn't defined particularly as either a function or a > > class, > > Well, it's listed under a section called "Functions", so the reader > could be forgiven for assuming that it's a function. Ah, f

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread Gregory Ewing
Chris Angelico wrote: At the moment, it isn't defined particularly as either a function or a class, Well, it's listed under a section called "Functions", so the reader could be forgiven for assuming that it's a function. From a high level point of view, it is -- you call it and it returns somet

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread Chris Angelico
On Thu, Apr 11, 2019 at 3:02 PM dieter wrote: > > Arup Rakshit writes: > > From docs https://docs.python.org/3/library/itertools.html#itertools.chain > > I see that itertools.chain is defined as a function. > > Maybe, it would have been better to state that "chain" > is a "callable": something w

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread dieter
Arup Rakshit writes: > From docs https://docs.python.org/3/library/itertools.html#itertools.chain I > see that itertools.chain is defined as a function. Maybe, it would have been better to state that "chain" is a "callable": something which can be called on arguments. For many people, "callable

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread Arup Rakshit
Well. I thought so far, all class in python is defined as CamelCase. A function can be a class to is something I am surprised. So does this mean, any callable function if produce an instance is called class in Python? Thanks, Arup Rakshit a...@zeit.io > On 11-Apr-2019, at 12:53 AM, Calvin

Re: Why inspect.isclass says iter() a class?

2019-04-10 Thread Calvin Spealman
Because it is. Many things are classes. calling itertools.chain(a, b) creates an itertools.chain instance that you can iterate over. What else did you think it would be? On Wed, Apr 10, 2019 at 3:17 PM Arup Rakshit wrote: > From docs https://docs.python.org/3/library/itertools.html#itertools.cha

Why inspect.isclass says iter() a class?

2019-04-10 Thread Arup Rakshit
>From docs https://docs.python.org/3/library/itertools.html#itertools.chain I >see that itertools.chain is defined as a function. But then why >inspect.isclass(chain) is saying it as class. from itertools import chain inspect.isclass(chain) # True Thanks, Arup Rakshit a...@zeit.io -- htt

RE: for loop iter next if file bad

2016-12-21 Thread Joaquin Alzola
>def return_files(file_list): >""" >Take a list of files and return file when called. > >Calling function to supply attributes >""" >for file in file_list: >with open(os.path.join(dir_path, file), 'rb') as fd: >if os.stat(fd.name).st_size == 0: >

Re: for loop iter next if file bad

2016-12-21 Thread Sayth Renshaw
Ah yes. Thanks ChrisA http://www.tutorialspoint.com/python/python_loop_control.htm The continue Statement: The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop an

Re: for loop iter next if file bad

2016-12-21 Thread Chris Angelico
On Wed, Dec 21, 2016 at 8:47 PM, Sayth Renshaw wrote: > def return_files(file_list): > """ > Take a list of files and return file when called. > > Calling function to supply attributes > """ > for file in file_list: > with open(os.path.join(dir_path, file), 'rb') as fd:

for loop iter next if file bad

2016-12-21 Thread Sayth Renshaw
Hi I am looping a list of files and want to skip any empty files. I get an error that str is not an iterator which I sought of understand but can't see a workaround for. How do I make this an iterator so I can use next on the file if my test returns true. Currently my code is. for dir_path, s

Re: if iter(iterator) is iterator

2016-11-13 Thread eryk sun
t use isinstance() with collections ABC classes. Except objects that use the legacy __getitem__ iterator aren't instances of collections.Iterable: class C: def __getitem__(self, index): return index o = C() >>> isinstance(o, collections.Iterable)

Re: if iter(iterator) is iterator

2016-11-13 Thread Serhiy Storchaka
On 14.11.16 02:40, Steve D'Aprano wrote: I'm surprised that the inspect module doesn't appear to have isiterable and isiterator functions. Here's my first attempt at both: Just use isinstance() with collections ABC classes. -- https://mail.python.org/mailman/listinfo/python-list

Re: if iter(iterator) is iterator

2016-11-13 Thread Steven D'Aprano
appears to just fall through. There doesn't seem to be any special handling: py> class X: ... def __iter__(self): ... raise TypeError("foo") ... py> iter(X()) Traceback (most recent call last): File "", line 1, in File "", li

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 3:54 PM, Steven D'Aprano wrote: >> Any particular reason to write it that way, rather than: >> >> def isiterable(obj): >> try: >> iter(obj) >> return True >> except TypeError: >> return F

Re: if iter(iterator) is iterator

2016-11-13 Thread Steven D'Aprano
turn hasattr(T, '__iter__') or hasattr(T, '__getitem__') > > Any particular reason to write it that way, rather than: > > def isiterable(obj): > try: > iter(obj) > return True > except TypeError: > return False class BadIterab

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
arguments 0, 1, 2, 3 ... until an IndexError exception is > raised (the Sequence Protocol). > > """ > T = type(obj) > return hasattr(T, '__iter__') or hasattr(T, '__getitem__') Any particular reason to write it that way, rather than: de

Re: if iter(iterator) is iterator

2016-11-13 Thread Steve D'Aprano
On Mon, 14 Nov 2016 07:02 am, Antoon Pardon wrote: > > Some time ago I read a text or saw a video on iterators and one thing > I remember from it, is that you should do something like the following > when working with iterators, to avoid some pitt falls. > > def bar(iterat

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 8:57 AM, Gregory Ewing wrote: > Antoon Pardon wrote: > >> def bar(iterator): >> if iter(iterator) is iterator: >> ... > > > That's a way of finding out whether you can safely iterate > over something more than onc

Re: if iter(iterator) is iterator

2016-11-13 Thread Gregory Ewing
Antoon Pardon wrote: def bar(iterator): if iter(iterator) is iterator: ... That's a way of finding out whether you can safely iterate over something more than once. If the object is already an iterator, applying iter() to it will return the same object, and iterating over it

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 7:02 AM, Antoon Pardon wrote: > Some time ago I read a text or saw a video on iterators and one thing > I remember from it, is that you should do something like the following > when working with iterators, to avoid some pitt falls. > > def bar(iterator)

if iter(iterator) is iterator

2016-11-13 Thread Antoon Pardon
Some time ago I read a text or saw a video on iterators and one thing I remember from it, is that you should do something like the following when working with iterators, to avoid some pitt falls. def bar(iterator): if iter(iterator) is iterator: ... However I can't relocate i

Re: generator no iter - how do I call it from another function

2016-10-01 Thread Gregory Ewing
Sayth Renshaw wrote: def dataAttr(roots): """Get the root object and iter items.""" with open("output/first2.csv", 'w', newline='') as csvf: race_writer = csv.writer(csvf, delimiter=',') for meet in r

Re: generator no iter - how do I call it from another function

2016-10-01 Thread Sayth Renshaw
> Steve > “Cheer up,” they said, “things could be worse.” So I cheered up, and sure > enough, things got worse. Loving life. I first started with a simple with open on a file, which allowed me to use code that "iter"'s. for example: for meet in root.iter("m

Re: generator no iter - how do I call it from another function

2016-10-01 Thread Steve D'Aprano
On Sat, 1 Oct 2016 06:52 pm, Sayth Renshaw wrote: > Evening > > My file list handler I have created a generator. > > Before I created it as a generator I was able to use iter on my lxml root > objects, now I cannot iter. lxml root objects have an iter method. Other objects d

Re: generator no iter - how do I call it from another function

2016-10-01 Thread Vlastimil Brom
need to be able to reference the node, how do I > achieve this? > > Sayth > -- Hi, i think, you identified the problem correctly in the previous mail: >> Before I created it as a generator I was able to use iter on my lxml root >> objects, now I cannot iter. It is not clear, what

Re: generator no iter - how do I call it from another function

2016-10-01 Thread Sayth Renshaw
My main issue is that usually its just x in ,,, for a generator. But if I change the code for meet in roots.iter("meeting"): to for meet in roots("meeting"): Well its invalid but I need to be able to reference the node, how do I achieve this? Sayth -- https://mail.python.org/mailman/listinfo/

generator no iter - how do I call it from another function

2016-10-01 Thread Sayth Renshaw
Evening My file list handler I have created a generator. Before I created it as a generator I was able to use iter on my lxml root objects, now I cannot iter. ± |master U:2 ?:1 ✗| → python3 race.py data/ -e *.xml Traceback (most recent call last): File "race.py", line 83, in

Re: iter

2010-08-24 Thread Aahz
structure. That in turn depends on the >history of additions and deletions. Although I agree in general with your warning, you are factually incorrect about dicts: >>> d = {1:2, 3:4} >>> i = iter(d) >>> i.next() 1 >>> d[1] = 'foo' >>> d {1:

Re: iter

2010-08-09 Thread Steven D'Aprano
On Mon, 09 Aug 2010 09:11:37 -0700, daryn wrote: > I'm just playing around with the iter function and I realize that I can > use the iterator returned by it long after the original object has any > name bound to it. Yes, the same as everything else in Python. Iterators aren't

Re: iter

2010-08-09 Thread Terry Reedy
On 8/9/2010 12:11 PM, daryn wrote: I'm just playing around with the iter function and I realize that I can use the iterator returned by it long after the original object has any name bound to it. Example: a=[1,2,3,4] b=iter(a) b.next() 1 a[1]=99 Changing a list while iterating throu

Re: iter

2010-08-09 Thread Peter Otten
daryn wrote: > I'm just playing around with the iter function and I realize that I > can use the iterator returned by it long after the original object has > any name bound to it. Example: > >>>>a=[1,2,3,4] >>>>b=iter(a) >>>>b.next() >

iter

2010-08-09 Thread daryn
I'm just playing around with the iter function and I realize that I can use the iterator returned by it long after the original object has any name bound to it. Example: >>>a=[1,2,3,4] >>>b=iter(a) >>>b.next() 1 >>>a[1]=99 >>>a[3]=101 >>

Re: slicing return iter?

2009-10-18 Thread Gabriel Genellina
En Sun, 18 Oct 2009 23:07:40 -0200, Raymond Hettinger escribió: [Raymond] > If it really is a sequence (with len and getitem), you can write your > own indexing iterator: >   def myslice(seq, start, stop, step): >        'Allow forward or backwards iteration over a subslice' >        for i in

Re: slicing return iter?

2009-10-18 Thread Raymond Hettinger
[Raymond] > > If it really is a sequence (with len and getitem), you can write your > > own indexing iterator: > > >   def myslice(seq, start, stop, step): > >        'Allow forward or backwards iteration over a subslice' > >        for i in range(start, stop, step): > >            yield seq[i] [S

Re: slicing return iter?

2009-10-18 Thread Gabriel Genellina
's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index -- that's supported by slice. so I need > something, bind object with slice, and return a iter. I can find > anything like it...:-( If it really is

Re: slicing return iter?

2009-10-17 Thread StarWing
(. but > > what i could find the nearest thing is itertools.islice. but it can't > > process negative index -- that's supported by slice. so I need > > something, bind object with slice, and return a iter. I can find > > anything like it...:-( > > If it really is a seque

Re: slicing return iter?

2009-10-17 Thread Raymond Hettinger
27;t > process negative index -- that's supported by slice. so I need > something, bind object with slice, and return a iter. I can find > anything like it...:-( If it really is a sequence (with len and getitem), you can write your own indexing iterator: def myslice(seq, start, stop, step)

Re: slicing return iter?

2009-10-17 Thread Terry Reedy
StarWing wrote: > I had checked it for serval times. maybe it's my inattention :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index A negative index -n is an abbreviation for len(sequence) - n. Since iterables in general do not have a length,

Re: slicing return iter?

2009-10-17 Thread Arnaud Delobelle
ion :-(. but > what i could find the nearest thing is itertools.islice. but it can't > process negative index -- that's supported by slice. so I need > something, bind object with slice, and return a iter. I can find > anything like it...:-( Sorry, I read your post too quickly. I d

Re: slicing return iter?

2009-10-17 Thread StarWing
> # now a is a list, and a[:] is another list, and so a[m:n] > > # now we do something with the 0~len(a)-3 elements of a > > for val in a[:-2]: > > #do something > > > but, this will cause a copy on a, has some convenience way to get a > > iter to iterate

Re: slicing return iter?

2009-10-17 Thread Arnaud Delobelle
with the 0~len(a)-3 elements of a > for val in a[:-2]: >     #do something > > but, this will cause a copy on a, has some convenience way to get a > iter to iterate on a part of list? > > i made this: > class iterslice: >     def __init__(self, list): >        

slicing return iter?

2009-10-17 Thread StarWing
but, this will cause a copy on a, has some convenience way to get a iter to iterate on a part of list? i made this: class iterslice: def __init__(self, list): self.list = list def __len__(self): return len(self.list) def __getitem__(self, slice): import

Re: Help with dict and iter

2009-04-02 Thread mattia
Il Thu, 02 Apr 2009 13:44:38 +, Sion Arrowsmith ha scritto: > mattia wrote: >> So, I'm looking for a way to "reset" the next() value every >>time i complete the scan of a list. > > itertools.cycle ? Perfect, thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: Help with dict and iter

2009-04-02 Thread Sion Arrowsmith
mattia wrote: > So, I'm looking for a way to "reset" the next() value every >time i complete the scan of a list. itertools.cycle ? -- \S under construction -- http://mail.python.org/mailman/listinfo/python-list

Re: Help with dict and iter

2009-03-29 Thread mattia
Il Sun, 29 Mar 2009 12:00:38 -0400, andrew cooke ha scritto: > mattia wrote: >>[i wrote]: >>> don't you just want to have a new job machine? >>> >>> for job_list in job_list_list: >>> job_machine = dict((x+1, iter(JOBS[x])) for x in range(N

Re: Help with dict and iter

2009-03-29 Thread andrew cooke
mattia wrote: >[i wrote]: >> don't you just want to have a new job machine? >> >> for job_list in job_list_list: >> job_machine = dict((x+1, iter(JOBS[x])) for x in range(NJOBS)) for x >> in job_list: >> print(next(job_machine[x])) ok - btw you

Re: Help with dict and iter

2009-03-29 Thread mattia
job number and >> an iterator associated by the list of machines: job_machine = >> dict((x+1, iter(JOBS[x])) for x in range(NJOBS)) Now, something like: >> for x in job_list: >> print(next(job_machine[x])) >> Works good, but imagine I have a list of job_list, now obv

Re: Help with dict and iter

2009-03-29 Thread andrew cooke
I have a list of jobs and I want to have the associated list of > machines, e.g: > [JOB1, JOB1, JOB2] --> [3, 1, 2] > My original idea was to have a dict with associated the job number and an > iterator associated by the list of machines: > job_machine = dict((x+1, iter(JOBS[x]))

Help with dict and iter

2009-03-29 Thread mattia
of machines, e.g: [JOB1, JOB1, JOB2] --> [3, 1, 2] My original idea was to have a dict with associated the job number and an iterator associated by the list of machines: job_machine = dict((x+1, iter(JOBS[x])) for x in range(NJOBS)) Now, something like: for x in job_list: print(n

Re: iter with stepsize

2008-10-15 Thread Alan G Isaac
Alan G Isaac <[EMAIL PROTECTED]> writes: If I have a sequence, I can get every other or every fifth element by slicing. Is there an equivalent for iterators? On 10/15/2008 11:12 PM Paul Rubin apparently wrote: itertools.islice Oh, of course. I'm a bit embarrassed not to have thought to lo

Re: iter with stepsize

2008-10-15 Thread Paul Rubin
Alan G Isaac <[EMAIL PROTECTED]> writes: > If I have a sequence, I can get every other or every fifth > element by slicing. Is there an equivalent for iterators? itertools.islice -- http://mail.python.org/mailman/listinfo/python-list

iter with stepsize

2008-10-15 Thread Alan G Isaac
If I have a sequence, I can get every other or every fifth element by slicing. Is there an equivalent for iterators? More specifically, I want every fifth line of a big file. What is the most efficient way to get them? Thanks, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list

Re: Early halt for iterating a_list and iter(a_list)

2008-08-22 Thread Steven D'Aprano
redibly useful pattern. I often find myself converting sequences to iterators, so I can handle both types identically: def treat_first_item_specially(iterator_or_sequence): it = iter(iterator_or_sequence) try: first_item(it.next) except StopIteration: pass else

Re: Early halt for iterating a_list and iter(a_list)

2008-08-22 Thread Lie
gt; def looper(a, b, c): > >     for a_ in a: > >         for b_ in b: > >             for c_ in c: > >                 print a_, b_, c_ > > > looper(a, b, c)  # Intended behavior [1] > > a, b, c = iter(a), b, iter(c)  # b is intentionally not iter()-ed > >

Re: Early halt for iterating a_list and iter(a_list)

2008-08-15 Thread Fredrik Lundh
_, c_ looper(a, b, c) # Intended behavior [1] a, b, c = iter(a), b, iter(c) # b is intentionally not iter()-ed looper(a, b, c) # Inner StopIteration prematurely halt outer loop [2] iterators are once-only objects. there's nothing left in "c" when you enter the inner loop the second

Early halt for iterating a_list and iter(a_list)

2008-08-15 Thread Lie
b, c) # Intended behavior [1] a, b, c = iter(a), b, iter(c) # b is intentionally not iter()-ed looper(a, b, c) # Inner StopIteration prematurely halt outer loop [2] [1] 1 1 1 1 1 2 ... a very long result ... 3 4 4 3 4 5 [2] 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 Why is this behavior? Or is it a bug? This

Re: iter(lambda:f.read(8192),'')

2008-02-25 Thread Gabriel Genellina
En Sun, 24 Feb 2008 10:18:31 -0200, Dustan <[EMAIL PROTECTED]> escribió: > On Feb 24, 5:11 am, gert <[EMAIL PROTECTED]> wrote: >> what is the difference between iter(lambda:f.read(8192), ') and >> iter(f.read(8192),'') ? > > iter(lambda:f.read

Re: iter(lambda:f.read(8192),'')

2008-02-24 Thread gert
aha ok got it :) -- http://mail.python.org/mailman/listinfo/python-list

Re: iter(lambda:f.read(8192),'')

2008-02-24 Thread Dustan
On Feb 24, 5:11 am, gert <[EMAIL PROTECTED]> wrote: > what is the difference between iter(lambda:f.read(8192), ') and > iter(f.read(8192),'') ? One does not work, and one is syntactically incorrect: >>> iter(f.read(8192),'') Traceback (most rece

iter(lambda:f.read(8192),'')

2008-02-24 Thread gert
what is the difference between iter(lambda:f.read(8192), ') and iter(f.read(8192),'') ? -- http://mail.python.org/mailman/listinfo/python-list

Re: help - iter & dict

2006-08-05 Thread Alistair King
thanks to Simon Forman, his solution worked, the key value pairs were entered the wrong way round in the dictionary...Doh! -- Dr. Alistair King Research Chemist, Laboratory of Organic Chemistry, Department of Chemistry, Faculty of Science P.O. Box 55 (A.I. Virtasen aukio 1) FIN-00014 University

Re: help - iter & dict

2006-08-04 Thread Alistair King
taleinat wrote: > mappi.helsinki.fi> writes: > > >> CDSitdict = {28.473823598317392: "'2.48699832'", 40.06163037274758: >> "'0.2912'", 27.756248559438422: "'2.83499964'", >> 33.2299196586726: "'1.12499962'", 29.989685187220061: >> "'1.91399677'", 31.

Re: help - iter & dict

2006-08-04 Thread Alistair King
Simon Forman wrote: > [EMAIL PROTECTED] wrote: > >> Dear Python people, >> >> im a newbie to python and here...so hello! >> > > Hi Ali, and welcome. > > >> Im trying to iterate through values in a dictionary so i can find the >> closest value and then extract the key for that valuewh

Re: help - iter & dict

2006-08-04 Thread taleinat
mappi.helsinki.fi> writes: > CDSitdict = {28.473823598317392: "'2.48699832'", 40.06163037274758: > "'0.2912'", 27.756248559438422: "'2.83499964'", > 33.2299196586726: "'1.12499962'", 29.989685187220061: > "'1.91399677'", 31.502319473614037: "'1.490

Re: help - iter & dict

2006-08-03 Thread John Machin
Something strange possessed Tim Chase and caused him to write: > def findClosest(dataset, target): [subtly *BUGGY* and overly verbose] > def findClosest2(dataset, target): [evil] Try this (tested to the extent shown): C:\junk>type dict_closest.py def findClosest(dataset, target): [snip] def find

Re: help - iter & dict

2006-08-03 Thread aking
Quoting taleinat <[EMAIL PROTECTED]>: > Ali writes: > > >> Im trying to iterate through values in a dictionary so i can find the > >> closest value and then extract the key for that valuewhat ive done > so far: > >> > >> def pcloop(dictionary, exvalue): > >> z = dictionary.itervalues

Re: help - iter & dict

2006-08-03 Thread Simon Forman
[EMAIL PROTECTED] wrote: > Dear Python people, > > im a newbie to python and here...so hello! Hi Ali, and welcome. > Im trying to iterate through values in a dictionary so i can find the > closest value and then extract the key for that valuewhat ive done so far: > > def pcloop(dictionary, ex

Re: help - iter & dict

2006-08-03 Thread taleinat
Ali writes: >> Im trying to iterate through values in a dictionary so i can find the >> closest value and then extract the key for that valuewhat ive done so >> far: >> >> def pcloop(dictionary, exvalue): >> z = dictionary.itervalues() >> y = z - exvalue >> v = (y*y)*

Re: help - iter & dict

2006-08-03 Thread Justin Azoff
[EMAIL PROTECTED] wrote: > Im trying to iterate through values in a dictionary so i can find the > closest value and then extract the key for that valuewhat ive done so far: [snip] > short time. I was trying to define a function (its my first!) so that i > could apply to several 'dictionary's a

Re: help - iter & dict

2006-08-03 Thread Tim Chase
> Im trying to iterate through values in a dictionary so i can find the > closest value and then extract the key for that valuewhat ive done so far: > > def pcloop(dictionary, exvalue): > z = dictionary.itervalues() > y = z - exvalue > v = (y*y)**1/2 > if v < 0.

help - iter & dict

2006-08-03 Thread aking
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that valuewhat ive done so far: def pcloop(dictionary, exvalue): z = dictionary.itervalues() y = z

Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman
Will McGugan wrote: > Hi, > > I've been using Python for years, but I recently encountered something > in the docs I wasnt familar with. That is, using two arguements for > iter(). Could someone elaborate on the docs and maybe show a typical use > case for it? > &g

Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman
Will McGugan wrote: > Hi, > > I've been using Python for years, but I recently encountered something > in the docs I wasnt familar with. That is, using two arguements for > iter(). Could someone elaborate on the docs and maybe show a typical use > case for it? > &g

Re: iter(callable, sentinel)

2006-07-27 Thread Simon Forman
Will McGugan wrote: > Hi, > > I've been using Python for years, but I recently encountered something > in the docs I wasnt familar with. That is, using two arguements for > iter(). Could someone elaborate on the docs and maybe show a typical use > case for it? > &g

iter(callable, sentinel)

2006-07-27 Thread Will McGugan
Hi, I've been using Python for years, but I recently encountered something in the docs I wasnt familar with. That is, using two arguements for iter(). Could someone elaborate on the docs and maybe show a typical use case for it? Thanks, Will McGugan -- work: http://www.kelpiesoft.com

Re: New-style classes, iter and PySequence_Check

2005-04-14 Thread Raymond Hettinger
n't walked the relevant iter() code. > That would be too easy ;-) The actual code for Object/abstract.c's PyObject_GetIter() is somewhat more straight-forward and less mysterious than those experiments suggest. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: New-style classes, iter and PySequence_Check

2005-04-14 Thread Bengt Richter
print "__getattr__:", attr >... return getattr(args,attr) >... >py> class ClassicArgs: >... def __getattr__(self, attr): >... print "__getattr__:", attr >... return getattr(args, attr) >... >py> c = ClassicArgs() >p

Re: New-style classes, iter and PySequence_Check

2005-04-14 Thread Raymond Hettinger
ce_Check simply checks to see if an object defines __getitem__ and that check entails an attribute lookup. The posted code snippet uses a __getattr__ gimmick to supply an affirmative answer to that check. The docs do not make guarantees about how iter(o) will determine whether object o is a sequence. C

Re: New-style classes, iter and PySequence_Check

2005-04-14 Thread Steven Bethard
return getattr(args, attr) ... py> c = ClassicArgs() py> i = c.__iter__() __getattr__: __iter__ py> print i py> i = iter(c) __getattr__: __iter__ py> print i py> a = Args() py> i = a.__iter__() __getattr__: __iter__ py> print i py> i = iter(a) Traceback (most recent call la

New-style classes, iter and PySequence_Check

2005-04-14 Thread Tuure Laurinolli
", attr return getattr(args,attr) class ClassicArgs: def __getattr__(self, attr): print "__getattr__:", attr return getattr(args, attr) if __name__ == '__main__': c = ClassicArgs() i = c.__iter__() print i i = iter(c) print

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-31 Thread Greg Ewing
Ville Vainio wrote: The issue that really bothers me here is bloating the builtin space. We already have an uncomfortable amount of builtin functions. Maybe what we're really after here is the notion of a builtin module that's pre-imported into the builtin namespace. -- Greg Ewing, Computer Science

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-31 Thread Greg Ewing
Steven Bethard wrote: I'd argue that for the same reasons that dict.fromkeys is a dict classmethod, the itertools methods could be iter classmethods (or staticmethods). The basic idea being that it's nice to place the methods associated with a type in that type's definiton. Th

Re: itertools to iter transition

2005-03-30 Thread David M. Cooke
Steven Bethard <[EMAIL PROTECTED]> writes: > Terry Reedy wrote: >>>But if classmethods are intended to provide alternate constructors >> But I do not remember that being given as a reason for >> classmethod(). But I am not sure what was. > > Well I haven't searched thoroughly, but I know one plac

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-30 Thread Steven Bethard
some precedent. If it's necessary to leave anything in itertools, my suggestion would be that the documentation for the iter "type" have a clear "see also" link to the itertools module. One thing that might be worth keeping in mind is that some of itertools functionality is

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-30 Thread Ville Vainio
t such operations should not be dumped into the builtin iter. -- Ville Vainio http://tinyurl.com/2prnb -- http://mail.python.org/mailman/listinfo/python-list

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-30 Thread Steven Bethard
Ville Vainio wrote: The issue that really bothers me here is bloating the builtin space. We already have an uncomfortable amount of builtin functions. Of course the additions that have been suggested would not pollute the builtin namespace, but they would still be there, taking space. I'd rather se

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Ville Vainio
to fix the Steven> documentation, not refuse to add builtin types. Yep - that's why we should perhaps fix the documentation first :-). Steven> I guess the real questions are[1]: Steven> * How much does iter feel like a type? Guess this depends on the person. I've never tho

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Steven Bethard
is the only reason though, and even if it is, it might not be entirely applicable because while the itertools functions may be supplying alternate constructors, it's not clear why anyone would subclass iter[2], so the constructors aren't likely to be inherited. STeVe [1] http://www.pyt

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Terry Reedy
'd argue that for the same reasons that >>>dict.fromkeys is a dict classmethod, the itertools methods could be iter >>>classmethods (or staticmethods). >> >> As near as I could tell from the doc, .fromkeys is the only dict method >> that is a classmethod (better, t

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Steven Bethard
Terry Reedy wrote: "Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] True it's not a huge win. But I'd argue that for the same reasons that dict.fromkeys is a dict classmethod, the itertools methods could be iter classmethods (or staticme

Re: itertools to iter transition (WAS: Pre-PEP: Dictionaryaccumulator methods)

2005-03-29 Thread George Sakkis
"Terry Reedy" <[EMAIL PROTECTED]> wrote: > > "Steven Bethard" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > True it's not a huge win. But I'd argue that for the same reasons that > > dict.fromkeys is a dict classm

Re: automatically mapping builtins (WAS: itertools to iter transition)

2005-03-29 Thread Michael Spencer
Steven Bethard wrote: Michael Spencer wrote: > While we're on the topic, what do you think of having unary, > non-summary builtins automatically map themselves when called with an > iterable that would otherwise be an illegal argument: I personally don't much like the idea because I expect 'int'

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Terry Reedy
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > True it's not a huge win. But I'd argue that for the same reasons that > dict.fromkeys is a dict classmethod, the itertools methods could be iter > classmethods (or staticmethods)

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Bengt Richter
On Tue, 29 Mar 2005 11:32:33 -0800, Michael Spencer <[EMAIL PROTECTED]> wrote: [...] >While we're on the topic, what do you think of having unary, non-summary >builtins automatically map themselves when called with an iterable that would >otherwise be an illegal argument: That last "otherwise" is

  1   2   >