Elron commited on
Commit
8ae904e
·
verified ·
1 Parent(s): 3157b84

Upload utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils.py +34 -0
utils.py CHANGED
@@ -1,6 +1,8 @@
1
  import json
2
  from typing import Any, Dict
3
 
 
 
4
 
5
  class Singleton(type):
6
  _instances = {}
@@ -42,3 +44,35 @@ def save_json(path, data):
42
  dumped = json.dumps(data, indent=4, ensure_ascii=False)
43
  f.write(dumped)
44
  f.write("\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
2
  from typing import Any, Dict
3
 
4
+ import pkg_resources
5
+
6
 
7
  class Singleton(type):
8
  _instances = {}
 
44
  dumped = json.dumps(data, indent=4, ensure_ascii=False)
45
  f.write(dumped)
46
  f.write("\n")
47
+
48
+
49
+ def is_package_installed(package_name):
50
+ """Check if a package is installed.
51
+
52
+ Parameters:
53
+ - package_name (str): The name of the package to check.
54
+
55
+ Returns:
56
+ - bool: True if the package is installed, False otherwise.
57
+ """
58
+ try:
59
+ pkg_resources.get_distribution(package_name)
60
+ return True
61
+ except pkg_resources.DistributionNotFound:
62
+ return False
63
+
64
+
65
+ def is_module_available(module_name):
66
+ """Check if a module is available in the current Python environment.
67
+
68
+ Parameters:
69
+ - module_name (str): The name of the module to check.
70
+
71
+ Returns:
72
+ - bool: True if the module is available, False otherwise.
73
+ """
74
+ try:
75
+ __import__(module_name)
76
+ return True
77
+ except ImportError:
78
+ return False