forked from recommenders-team/recommenders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral_utils.py
46 lines (32 loc) · 1.04 KB
/
general_utils.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
43
44
45
46
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import psutil
def invert_dictionary(dictionary):
"""Invert a dictionary
.. note::
If the dictionary has unique keys and unique values, the inversion would be perfect. However, if there are
repeated values, the inversion can take different keys
Args:
dictionary (dict): A dictionary
Returns:
dict: inverted dictionary
"""
return {v: k for k, v in dictionary.items()}
def get_physical_memory():
"""Get the physical memory in GBs.
Returns:
float: Physical memory in GBs.
"""
return psutil.virtual_memory()[0] / 1073741824
def get_number_processors():
"""Get the number of processors in a CPU.
Returns:
int: Number of processors.
"""
try:
num = os.cpu_count()
except Exception:
import multiprocessing # force exception in case multiprocessing is not installed
num = multiprocessing.cpu_count()
return num