1 3
import time
2 3
from timeit import default_timer
3 3
import logging
4 3
logger = logging.getLogger(__file__)
5 3
from collections import OrderedDict, Counter
6

7

8 3
def isnotebook():
9
    """ returns True if Jupyter notebook is runnung """
10
    # from https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook
11 0
    try:
12 0
        shell = get_ipython().__class__.__name__
13 0
        if shell == 'ZMQInteractiveShell':
14 0
            return True   # Jupyter notebook or qtconsole
15 0
        elif shell == 'TerminalInteractiveShell':
16 0
            return False  # Terminal running IPython
17
        else:
18 0
            return False  # Other type (?)
19 0
    except NameError:
20 0
        return False      # Probably standard Python interpreter
21

22

23 3
def time():
24
    """ returns the time. used instead of time.time for rapid portability"""
25 3
    return default_timer()
26

27 3
def get_unique_name_list_from_class_list(cls_list):
28
    """
29
    returns a list of names using cls.name if unique or cls.name1, cls.name2... otherwise.
30
    Order of the name list matches order of cls_list, such that iterating over zip(cls_list, name_list) is OK
31
    """
32
    # cls_list is typically
33
    # cls_modules = [rp.HK, rp.AMS, rp.Scope, rp.Sampler, rp.Asg1, rp.Asg2] + \
34
    #              [rp.AuxOutput] * 2 + [rp.IQ] * 3 + [rp.Pid] * 4 + [rp.IIR]
35

36
    # first, map from list of classes to a list of corresponding names
37
    # e.g. all_names = ['hk, ..., 'pwm', 'pwm', ...
38 0
    all_names = [cls.__name__.lower() for cls in cls_list]
39 0
    final_names = []
40 0
    for name in all_names:
41
        # how many times does the name occur?
42 0
        occurences = all_names.count(name)
43 0
        if occurences == 1:
44
            # for single names, leave as-is
45 0
            final_names.append(name)
46
        else:
47
            # for multiple name, assign name+str(lowest_free_number)
48 0
            for i in range(occurences):
49 0
                if not name+str(i) in final_names:
50 0
                    final_names.append(name+str(i))
51 0
                    break
52 0
    return final_names
53

54

55 3
def get_class_name_from_module_name(module_name):
56
    """ returns the class name corresponding to a module_name """
57 0
    return module_name[0].upper() + (module_name[1:]).rstrip('1234567890')
58

59

60 3
def get_base_module_class(module):
61
    """ returns the base class of module that has the same name as module """
62 0
    base_module_class_name = get_class_name_from_module_name(module.name)
63 0
    for base_module_class in type(module).__mro__:
64 0
        if base_module_class.__name__ == base_module_class_name:
65 0
            return base_module_class
66

67

68
# see http://stackoverflow.com/questions/3862310/how-can-i-find-all-subclasses-of-a-class-given-its-name
69 3
def all_subclasses(cls):
70
    """ returns a list of all subclasses of cls """
71 0
    return cls.__subclasses__() + [g for s in cls.__subclasses__()
72
                                   for g in all_subclasses(s)]
73

74

75 3
def recursive_getattr(root, path):
76
    """ returns root.path (i.e. root.attr1.attr2) """
77 3
    attribute = root
78 3
    for name in path.split('.'):
79 3
        if name != "":
80 3
            attribute = getattr(attribute, name)
81 3
    return attribute
82

83

84 3
def recursive_setattr(root, path, value):
85
    """ returns root.path = value (i.e. root.attr1.attr2 = value) """
86 3
    attribute = root
87 3
    names = path.split('.')
88 3
    for name in names[:-1]:
89 3
        attribute = getattr(attribute, name)
90 3
    setattr(attribute, names[-1], value)
91

92

93 3
def setloglevel(level='info', loggername='pyrpl'):
94
    """ sets the log level to the one specified in config file"""
95 3
    try:
96 3
        loglevels = {"notset": logging.NOTSET,
97
                     "debug": logging.DEBUG,
98
                     "info": logging.INFO,
99
                     "warning": logging.WARNING,
100
                     "error": logging.ERROR,
101
                     "critical": logging.CRITICAL}
102 3
        level = loglevels[level]
103 0
    except:
104 0
        pass
105
    else:
106 3
        logging.getLogger(name=loggername).setLevel(level)
107

108

109 3
class DuplicateFilter(logging.Filter):
110
    """
111
    Prevent multiple repeated logging message from polluting the console
112
    """
113 3
    def filter(self, record):
114
        # add other fields if you need more granular comparison, depends on your app
115 0
        current_log = (record.module, record.levelno, record.msg)
116 0
        if current_log != getattr(self, "last_log", None):
117 0
            self.last_log = current_log
118 0
            return True
119 0
        return False
120

121

122 3
def sorted_dict(dict_to_sort=None, sort_by_values=True, **kwargs):
123 3
    if dict_to_sort is None:
124 3
        dict_to_sort = kwargs
125 3
    if not sort_by_values:
126 0
        return OrderedDict(sorted(dict_to_sort.items()))
127
    else:
128 3
        return OrderedDict(sorted(dict_to_sort.items(), key=lambda x: x[1]))
129

130

131 3
def update_with_typeconversion(dictionary, update):
132 3
    for k, v in update.items():
133 0
        if k in dictionary:
134
            # perform type conversion if appropriate
135 0
            v = type(dictionary[k])(v)
136 0
        dictionary[k] = v
137 3
    return dictionary
138

139

140 3
def unique_list(nonunique_list):
141
    """ Returns a list where each element of nonunique_list occurs exactly once.
142
    The last occurence of an element defines its position in the returned list.
143
    """
144 3
    unique_list = []
145 3
    for attr in reversed(nonunique_list):
146
        # remove all previous occurences
147 3
        if attr not in unique_list:
148 3
            unique_list.insert(0, attr)
149 3
    return unique_list
150

151

152 3
class Bijection(dict):
153
    """ This class defines a bijection object based on dict
154

155
    It can be used exactly like dict, but additionally has a property
156
    'inverse' which contains the inverted {value: key} dict. """
157

158 3
    def __init__(self, *args, **kwargs):
159 3
        super(Bijection, self).__init__(*args, **kwargs)
160 3
        self.inverse = {v: k for k, v in self.items()}
161

162 3
    def __setitem__(self, key, value):
163 0
        super(Bijection, self).__setitem__(key, value)
164 0
        self.inverse[value] = key
165

166 3
    def __delitem__(self, key):
167 0
        self.inverse.__delitem__(self.__getitem__(key))
168 0
        super(Bijection, self).__delitem__(key)
169

170 3
    def pop(self, key):
171 0
        self.inverse.pop(self.__getitem__(key))
172 0
        super(Bijection, self).pop(key)
173

174 3
    def update(self, *args, **kwargs):
175 0
        super(Bijection, self).update(*args, **kwargs)
176 0
        self.inverse = {v: k for k, v in self.items()}

Read our documentation on viewing source code .

Loading