-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_cool_library.py
42 lines (31 loc) · 1003 Bytes
/
my_cool_library.py
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
class Bunch(object):
def __getitem__(self, name):
return self.__dict__[name]
def __setitem__(self, name, item):
self.__dict__[name] = item
def __delitem__(self, name):
del self.__dict__[name]
def __contains__(self, item):
return item in self.__dict__
def __iter__(self):
return iter(self.__dict__)
def to_dict(self):
return dict(self.__dict__)
def chunkify(iterable, chunk_size):
if chunk_size < 0:
raise ValueError('Chunk size cannot be negative')
return_value = []
acc = []
for item in iterable:
acc.append(item)
if len(acc) == chunk_size:
return_value.append(acc)
acc = []
if acc:
return_value.append(acc)
return return_value
def is_listy(x):
from collections import Sized, Iterable, Mapping
return (isinstance(x, Sized) and
isinstance(x, Iterable) and
not isinstance(x, (Mapping, type(b''), type(u''))))