optimization_metadata.paths   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 29
dl 0
loc 44
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A HypermemoryPaths.subdirs() 0 2 1
A HypermemoryPaths.__init__() 0 6 1
A HypermemoryPaths._function_id() 0 2 1
A HypermemoryPaths._function_string() 0 2 1
A HypermemoryPaths.add_directory() 0 5 1
A HypermemoryPaths._array_id() 0 2 1
A HypermemoryPaths._object_hash() 0 2 1
A HypermemoryPaths._dictionary_id() 0 2 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import os
6
import glob
7
import dill
8
import hashlib
9
import inspect
10
11
12
class HypermemoryPaths:
13
    def __init__(self, main_path):
14
        self.path_dict = {"main_path": main_path}
15
        self.get_id = {
16
            "array": self._array_id,
17
            "function": self._function_id,
18
            "dictionary": self._dictionary_id,
19
        }
20
21
    def _object_hash(self, object):
22
        return hashlib.sha1(object).hexdigest()
23
24
    def _function_string(self, function):
25
        return inspect.getsource(function)
26
27
    def _array_id(self, array_):
28
        return str(self._object_hash(array_))
29
30
    def _function_id(self, function_):
31
        return str(self._object_hash(self._function_string(function_).encode("utf-8")))
32
33
    def _dictionary_id(self, dictionary_):
34
        return str(self._object_hash(dill.dumps(dictionary_)))
35
36
    def subdirs(self, name):
37
        return glob.glob(self.path_dict[name] + "*/")
38
39
    def add_directory(self, name, id_type, object_, prefix="", sufix=""):
40
        path_new = str(prefix) + self.get_id[id_type](object_) + str(sufix) + "/"
41
42
        last_path = list(self.path_dict.values())[-1]
43
        self.path_dict[name] = last_path + path_new
44
45