Skip to content

utilities module

AttrDict

Bases: dict

This class allows javascript-like attribute access for dictionaries. Pass in a nested dictionary and recursively access members as attributes.

Examples:

>>> attr_dict = AttrDict({'foo': [{'bar': [1,2]}]})
>>> attr_dict.foo[0].bar[1] == 2
True
Source code in pycocowriter/utils.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class AttrDict(dict):
    '''
    This class allows javascript-like attribute access for dictionaries.
    Pass in a nested dictionary and recursively access members as attributes.

    Examples
    --------
    >>> attr_dict = AttrDict({'foo': [{'bar': [1,2]}]})
    >>> attr_dict.foo[0].bar[1] == 2
    True
    '''
    def __init__(self, some_dict:dict):
        super().__init__(some_dict)
        self.__dict__ = {
            k: _to_attrdict(v) for k,v in some_dict.items()
        }

NPEncoder

Bases: JSONEncoder

json encoder class used to convert numpy types into plain python types during json.dumps.

Examples:

>>> json.dumps({'array': np.array([1,2,3])}, cls=NPEncoder)
'{"array": [1, 2, 3]}'
Source code in pycocowriter/utils.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class NPEncoder(json.JSONEncoder):
    '''
    json encoder class used to convert numpy types into plain python types during json.dumps.

    Examples
    --------
    >>> json.dumps({'array': np.array([1,2,3])}, cls=NPEncoder)
    '{"array": [1, 2, 3]}'

    '''
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        if isinstance(obj, np.floating):
            return float(obj)
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return super(NPEncoder, self).default(obj)

skiprows(iterable, n)

skips the first n rows of iterable. returns iterable as a convenience

Parameters:

Name Type Description Default
iterable Iterable

the iterable to skip rows of

required
n int

the number of rows to skip

required

Returns:

Name Type Description
iterable Iterable

the original iterable, but now with n rows having been skipped

Source code in pycocowriter/utils.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def skiprows(iterable: Iterable, n: int) -> Iterable:
    '''
    skips the first n rows of iterable.  returns iterable as a convenience

    Parameters
    ----------
    iterable: Iterable
        the iterable to skip rows of
    n: int
        the number of rows to skip

    Returns
    -------
    iterable: Iterable
        the original iterable, but now with n rows having been skipped
    '''
    for i in range(n):
        next(iterable)
    return iterable