From 0e31201848f45db86bea6bf5bd196e2db88fbfbe Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 10 Nov 2009 18:35:46 +0000 Subject: Show example of how to make a sorted dictionary --- Doc/library/collections.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index cd38956..7770b89 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -862,6 +862,28 @@ semantics pass-in keyword arguments using a regular unordered dictionary. `Equivalent OrderedDict recipe `_ that runs on Python 2.4 or later. +Since an ordered dictionary remembers its insertion order, it can be used +in conjuction with sorting to make a sorted dictionary:: + + >>> # regular unsorted dictionary + >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2} + + >>> # dictionary sorted by key + >>> OrderedDict(sorted(d.items(), key=lambda t: t[0])) + OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) + + >>> # dictionary sorted by value + >>> OrderedDict(sorted(d.items(), key=lambda t: t[1])) + OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) + + >>> # dictionary sorted by length of the key string + >>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0]))) + OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)]) + +The new sorted dictionaries maintain their sort order when entries +are deleted. But when new keys are added, the keys are appended +to the end and the sort is not maintained. + :class:`UserDict` objects ------------------------- -- cgit v0.12 /oss-git/cpython.git/commit/Lib/test/test_md5.py?h=v2.4b1'>commitdiffstats
blob: 1f08568508b7e537911fb323c595e3bcc6ea045e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Testing md5 module

import unittest
from md5 import md5
from test import test_support

def hexstr(s):
    import string
    h = string.hexdigits
    r = ''
    for c in s:
        i = ord(c)
        r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
    return r

class MD5_Test(unittest.TestCase):

    def md5test(self, s, expected):
        self.assertEqual(hexstr(md5(s).digest()), expected)
        self.assertEqual(md5(s).hexdigest(), expected)

    def test_basics(self):
        eq = self.md5test
        eq('', 'd41d8cd98f00b204e9800998ecf8427e')
        eq('a', '0cc175b9c0f1b6a831c399e269772661')
        eq('abc', '900150983cd24fb0d6963f7d28e17f72')
        eq('message digest', 'f96b697d7cb7938d525a2f31aaf161d0')
        eq('abcdefghijklmnopqrstuvwxyz', 'c3fcd3d76192e4007dfb496cca67e13b')
        eq('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
           'd174ab98d277d9f5a5611c2c9f419d9f')
        eq('12345678901234567890123456789012345678901234567890123456789012345678901234567890',
           '57edf4a22be3c955ac49da2e2107b67a')

    def test_hexdigest(self):
        # hexdigest is new with Python 2.0
        m = md5('testing the hexdigest method')
        h = m.hexdigest()
        self.assertEqual(hexstr(m.digest()), h)

    def test_large_update(self):
        aas = 'a' * 64
        bees = 'b' * 64
        cees = 'c' * 64

        m1 = md5()
        m1.update(aas)
        m1.update(bees)
        m1.update(cees)

        m2 = md5()
        m2.update(aas + bees + cees)
        self.assertEqual(m1.digest(), m2.digest())

def test_main():
    test_support.run_unittest(MD5_Test)

if __name__ == '__main__':
    test_main()