• Re: Testing list sequence question

    From Stefan Ram@21:1/5 to Gabor Urban on Sat Mar 4 16:47:58 2023
    Gabor Urban <[email protected]> writes:
    I am running Python 3.3 on MS Windows. Any idea why is this?

    IIRC Raymond Hettinger spoke about how the rules for the ordering of
    dictionaries have been changed during the development of Python.

    The most recent versions of Python might retain the insertation order
    while older version might follow different rules.

    (Right now I cannot access the resources I usually use to
    check the accuracy of what I write, so what I wrote above
    has not been checked by me and might be wrong.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gabor Urban@21:1/5 to All on Sat Mar 4 17:38:50 2023
    Hi guys,

    I have a strange problem that I do not understand. I am testing function
    which returns a dictionary. The code should ensure that the keys of the dictionary are generated in a given order.

    I am testing the function with the standard unittest module and use the assertListEqual statement to verify the sequence. Sometimes this test
    fails, sometimes passes without any change neither in the code, nor in the testcase. I am using "list(myDict.keys())" to create the list of the keys
    of the dictionary.

    I am running Python 3.3 on MS Windows. Any idea why is this?

    Thx, Gabor

    --
    Urbán Gábor

    Linux is like a wigwam: no Gates, no Windows and an Apache inside.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to Gabor Urban on Sat Mar 4 17:59:32 2023
    Gabor Urban schreef op 4/03/2023 om 17:38:
    Hi guys,

    I have a strange problem that I do not understand. I am testing function which returns a dictionary. The code should ensure that the keys of the dictionary are generated in a given order.

    I am testing the function with the standard unittest module and use the assertListEqual statement to verify the sequence. Sometimes this test
    fails, sometimes passes without any change neither in the code, nor in the testcase. I am using "list(myDict.keys())" to create the list of the keys
    of the dictionary.

    I am running Python 3.3 on MS Windows. Any idea why is this?
    Prior to Python 3.6 (de facto) and Python 3.7 (officially), there was no guarantee at all about the order of keys in a dict. If you need the keys
    to be in the same order they were inserted, either use
    collections.OrderedDict or upgrade to Python 3.7 or later.

    --
    "Life ain't no fairy tale
    Just give me another ale
    And I'll drink to Rock 'n Roll"
    -- Barkeep (The Scabs)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Gabor Urban on Sat Mar 4 12:49:45 2023
    On 3/4/2023 11:38 AM, Gabor Urban wrote:
    Hi guys,

    I have a strange problem that I do not understand. I am testing function which returns a dictionary. The code should ensure that the keys of the dictionary are generated in a given order.

    I am testing the function with the standard unittest module and use the assertListEqual statement to verify the sequence. Sometimes this test
    fails, sometimes passes without any change neither in the code, nor in the testcase. I am using "list(myDict.keys())" to create the list of the keys
    of the dictionary.

    I am running Python 3.3 on MS Windows. Any idea why is this?

    List order would not be guaranteed. Sort the list first. Then your
    problem should clear up.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to Thomas Passin on Sat Mar 4 19:42:37 2023
    Thomas Passin schreef op 4/03/2023 om 18:49:
    On 3/4/2023 11:38 AM, Gabor Urban wrote:
    Hi guys,

    I have a strange problem that I do not understand. I am testing function which returns a dictionary. The code should ensure that the keys of the dictionary are generated in a given order.

    I am testing the function with the standard unittest module and use the assertListEqual statement to verify the sequence. Sometimes this test fails, sometimes passes without any change neither in the code, nor in the testcase. I am using "list(myDict.keys())" to create the list of the keys of the dictionary.

    I am running Python 3.3 on MS Windows. Any idea why is this?

    List order would not be guaranteed. Sort the list first. Then your
    problem should clear up.
    How would that enable you to check that the keys in the dict are in a
    specific order?

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Roel Schroeven on Sat Mar 4 14:59:42 2023
    On 3/4/2023 1:42 PM, Roel Schroeven wrote:
    Thomas Passin schreef op 4/03/2023 om 18:49:
    On 3/4/2023 11:38 AM, Gabor Urban wrote:
       Hi guys,
    I have a strange problem that I do not understand. I am testing
    function
    which returns a dictionary. The code should ensure that the keys of the
    dictionary are generated in a given order.
    I am testing the function with the standard unittest module and
    use the
    assertListEqual statement to verify the sequence. Sometimes this test
    fails, sometimes passes without any change neither in the code, nor
    in the
    testcase. I am using "list(myDict.keys())" to create the list of the
    keys
    of the dictionary.
    I am running Python 3.3 on MS Windows. Any idea why is this?

    List order would not be guaranteed.  Sort the list first.  Then your
    problem should clear up.
    How would that enable you to check that the keys in the dict are in a specific order?

    I probably misunderstood. I thought you needed a list of keys in a
    reproducible order, which sorting the list would achieve.

    Let's back up. Why do you care about having the keys in some particular
    order? And what is that order? Perhaps some other code design will
    satisfy the real purpose.

    Otherwise, in Python 3.7 and up dicts maintain the insertion order of
    the keys. If you cannot use those versions of Python for some reason,
    here's a python 2.7-compatible backport of an ordered dictionary that
    you could probably adapt:

    https://code.activestate.com/recipes/576693/

    But note: "Dictionaries compare equal if and only if they have the same
    (key, value) pairs (regardless of ordering)" (from the Python docs).

    If the order you want is not the original insertion order, then you will perhaps need to adapt the ordered dict idea to your needs.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Roel Schroeven on Sun Mar 5 06:37:51 2023
    On Sun, 5 Mar 2023 at 05:44, Roel Schroeven <[email protected]> wrote:

    Thomas Passin schreef op 4/03/2023 om 18:49:
    On 3/4/2023 11:38 AM, Gabor Urban wrote:
    Hi guys,

    I have a strange problem that I do not understand. I am testing function which returns a dictionary. The code should ensure that the keys of the dictionary are generated in a given order.

    I am testing the function with the standard unittest module and use the assertListEqual statement to verify the sequence. Sometimes this test fails, sometimes passes without any change neither in the code, nor in the
    testcase. I am using "list(myDict.keys())" to create the list of the keys of the dictionary.

    I am running Python 3.3 on MS Windows. Any idea why is this?

    List order would not be guaranteed. Sort the list first. Then your problem should clear up.
    How would that enable you to check that the keys in the dict are in a specific order?


    In Python 3.3, the keys in a dict are not in a specific order. There
    is nothing to test.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)