Total Complexity | 8 |
Total Lines | 63 |
Duplicated Lines | 0 % |
Coverage | 83.33% |
Changes | 0 |
1 | 1 | from __future__ import absolute_import |
|
|
|||
2 | 1 | from __future__ import print_function |
|
3 | |||
4 | 1 | import multiprocessing |
|
5 | |||
6 | |||
7 | 1 | class SSGError(RuntimeError): |
|
8 | 1 | pass |
|
9 | |||
10 | |||
11 | 1 | def required_key(_dict, _key): |
|
12 | """ |
||
13 | Returns the value of _key if it is in _dict; otherwise, raise an |
||
14 | exception stating that it was not found but is required. |
||
15 | """ |
||
16 | |||
17 | 1 | if _key in _dict: |
|
18 | 1 | return _dict[_key] |
|
19 | |||
20 | 1 | raise ValueError("%s is required but was not found in:\n%s" % |
|
21 | (_key, repr(_dict))) |
||
22 | |||
23 | |||
24 | 1 | def get_cpu_count(): |
|
25 | """ |
||
26 | Returns the most likely estimate of the number of CPUs in the machine |
||
27 | for threading purposes, gracefully handling errors and possible |
||
28 | exceptions. |
||
29 | """ |
||
30 | |||
31 | try: |
||
32 | return max(1, multiprocessing.cpu_count()) |
||
33 | |||
34 | except NotImplementedError: |
||
35 | # 2 CPUs is the most probable |
||
36 | return 2 |
||
37 | |||
38 | |||
39 | 1 | def merge_dicts(left, right): |
|
40 | """ |
||
41 | Merges two dictionaries, keeing left and right as passed. If there are any |
||
42 | common keys between left and right, the value from right is use. |
||
43 | |||
44 | Returns the merger of the left and right dictionaries |
||
45 | """ |
||
46 | 1 | result = left.copy() |
|
47 | 1 | result.update(right) |
|
48 | 1 | return result |
|
49 | |||
50 | |||
51 | 1 | def subset_dict(dictionary, keys): |
|
52 | """ |
||
53 | Restricts dictionary to only have keys from keys. Does not modify either |
||
54 | dictionary or keys, returning the result instead. |
||
55 | """ |
||
56 | |||
57 | 1 | result = dictionary.copy() |
|
58 | 1 | for original_key in dictionary: |
|
59 | 1 | if original_key not in keys: |
|
60 | 1 | del result[original_key] |
|
61 | |||
62 | return result |
||
63 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.