Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 incremental testing in all()
#1
i have a huge list that i want to test that everything is an expected type. i could test it like this:
    if all(isinstance(x,types) for x in huge_list):
        ... # what to do if all items are a type of one of "types"
    else:
        ... # what to do if one or more items are not a type of one of "types"
what i want to know is if all() quits early if one of its tests comes up False. documentation shows code that returns early. i want to know if "equivalent" really means that behavior or if it just means the result is "equivalent".
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
According to the Python docs, all() does quit early.
Quote:all(iterable)¶
Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
Quote
#3
i see you didn't get the semantics of my post.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
Or you didn't get the semantics of the docs? It quit early. if it does not, it would not be "Equivalent" to the code in the docs.
Quote
#5
Everyone has access not only 'equivalent' but also 'exact' code.

Go to https://github.com/python/cpython/blob/m...inmodule.c and look for builtin_all. For your convenience it's provided below (I am not C-man myself so I have no idea what does it mean Smile ):

static PyObject *
builtin_all(PyObject *module, PyObject *iterable)
/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
{
    PyObject *it, *item;
    PyObject *(*iternext)(PyObject *);
    int cmp;

    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;
    iternext = *Py_TYPE(it)->tp_iternext;

    for (;;) {
        item = iternext(it);
        if (item == NULL)
            break;
        cmp = PyObject_IsTrue(item);
        Py_DECREF(item);
        if (cmp < 0) {
            Py_DECREF(it);
            return NULL;
        }
        if (cmp == 0) {
            Py_DECREF(it);
            Py_RETURN_FALSE;
        }
    }
    Py_DECREF(it);
    if (PyErr_Occurred()) {
        if (PyErr_ExceptionMatches(PyExc_StopIteration))
            PyErr_Clear();
        else
            return NULL;
    }
    Py_RETURN_TRUE;
}
buran and Gribouillis like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#6
The C code proves that it returns early. Note that the macro Py_RETURN_FALSE used at line 26 above is defined in Include/boolobject.h

#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
Quote
#7
(Aug-17-2019, 06:01 AM)buran Wrote: Or you didn't get the semantics of the docs? It quit early. if it does not, it would not be "Equivalent" to the code in the docs.

it's saying that in the context of describing the value being returned. so, it can still be either way and the wording of the document would still be correct. some code that is able to test this would be nice, if the document doesn't have a clarification.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#8
(Aug-18-2019, 07:10 PM)Skaperen Wrote: it's saying that in the context of describing the value being returned. so, it can still be either way and the wording of the document would still be correct.
I beg to disagree. If it does not quit early it would not be equivalent to provided code. But as others pointed out, you can check the source code to your satisfaction.
Quote
#9
if it does not quit early it can still be equivalent to that code in terms of what is returned. that semantic is the effect of a sentence in context. this was learned in my basic journalism writing class (a good class to take for documentive writing). perhaps i should write a couple replacement paragraphs. but, this is not the only ambiguity i have seen in the documentation. English is an expressive language, not an inflexive one, such as Norwegian or Spanish. word order and context are part of the language, not the culture as is the case with most others. try Esperanto, it is the most inflexive language i have seen, despite being artificial.

i did put together a little brute-force test with a 268435460 size tuple. it did show the early quit. i saw the early quit in the C code. despite the high probability of it being so, that does not prove that the binary i'm running matches that source (Ubuntu has changed things before).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#10
Here the proof without have to read C.

deadeye@nexus ~ $ ipython                                                                                                       
Python 3.7.4 (default, Jul 10 2019, 23:32:01) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.7.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: class Iterator: 
   ...:     def __init__(self): 
   ...:         self.last = None 
   ...:          
   ...:     def __iter__(self): 
   ...:         for i in range(10): 
   ...:             print(i) 
   ...:             if i < 5: 
   ...:                 yield True 
   ...:             else: 
   ...:                 yield False 
   ...:                                                                                                                         

In [2]: all(Iterator())                                                                                                         
0
1
2
3
4
5
Out[2]: False
The loop is stopped, when False is yielded.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How I can do performance testing on my API a21250450 0 57 Jul-18-2019, 09:29 AM
Last Post: a21250450
  unit testing a method that asks two user inputs() in console gebel 0 195 Apr-03-2019, 07:59 PM
Last Post: gebel
  Testing an API a21250450 1 204 Mar-29-2019, 05:10 PM
Last Post: gruntfutuk
  Editing and testing a package amasta 2 259 Mar-19-2019, 12:36 PM
Last Post: buran
  testing if open to a terminal Skaperen 0 240 Feb-01-2019, 04:48 AM
Last Post: Skaperen
  testing if this is a container type Skaperen 7 381 Jan-25-2019, 07:12 AM
Last Post: buran
  Testing function calls jenselme 1 527 Jul-25-2018, 10:33 AM
Last Post: Larz60+
  testing if a character in a string is an intended one Skaperen 2 621 Feb-27-2018, 02:51 AM
Last Post: Skaperen
  Testing library egslava 2 706 Jan-28-2018, 06:27 AM
Last Post: snippsat
  Unit testing mp3909 1 987 Oct-15-2017, 03:48 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)