File: use_cases.py

package info (click to toggle)
ubelt 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,180 kB
  • sloc: python: 15,487; sh: 807; makefile: 24
file content (90 lines) | stat: -rw-r--r-- 2,585 bytes parent folder | download | duplicates (3)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Ubelt Use Cases

Each use case starts with a modivation and follows with a solution. This makes
these cases perfect for presentations.
"""


def multiple_items_from_a_dictionary():
    """
    Spotlight:
        ubelt.take

    Modivation:
        Working with Lists of Dictionaries

    Requires:
        kwimage
    """
    ...

    """
    When working with data, a common pattern is to iterate through it, and
    gather information about the work to be done, so a you can make a final
    structured pass through the data.

    In python we might do this by initializing an empty list and appending
    **dictionary of information**, to the list. (or you might yield
    dictionaries of information from a generator instead, either way you have a
    flat list).

    Some people might use lists of tuples instead of Lists of dictionaries, but
    using dictionaries makes it easy to add new information later (and it works
    very will with pandas).
    """
    import ubelt as ub
    import kwimage
    kwimage_test_image_names = ['airport', 'amazon', 'astro', 'carl',
                                'lowcontrast']
    rows = []
    for test_image in kwimage_test_image_names:
        fpath = ub.Path(kwimage.grab_test_image_fpath(test_image))
        imdata = kwimage.imread(fpath)
        row = {
            'mean': imdata.mean(),
            'std': imdata.std(),
            'sum': imdata.sum(),
            'max': imdata.max(),
            'min': imdata.min(),
        }
        rows.append(row)

    """
    For each row, you might want to grab multiple specific items from it.

    But having a separate assignment on each row wastes a lot of vertical
    space.
    """

    for row in rows:
        mean = row['mean']
        std = row['std']
        sum = row['sum']
        min = row['min']
        max = row['max']

    """
    You might put them one line explicitly, but that wastes a lot of horizontal
    space
    """
    for row in rows:
        mean, std, sum, min, max = row['mean'], row['std'], row['sum'], row['min'], row['max']

    """
    What if we try to be clever? We can use a list comprehension
    """
    for row in rows:
        mean, std, sum, min, max = [row[k] for k in ['mean', 'std', 'sum', 'min', 'max']]

    """
    That's not too bad, but we can do better
    """
    for row in rows:
        mean, std, sum, min, max = ub.take(row, ['mean', 'std', 'sum', 'min', 'max'])

    """
    And now even better:
    """
    for row in map(ub.udict, rows):
        mean, std, sum, min, max = row.take(['mean', 'std', 'sum', 'min', 'max'])