docker-tidy/dockertidy/utils.py

28 lines
592 B
Python
Raw Normal View History

2020-03-01 17:42:29 +00:00
#!/usr/bin/env python3
"""Global utility methods and classes."""
from distutils.util import strtobool
def to_bool(string):
return bool(strtobool(str(string)))
def dict_intersect(d1, d2):
return {
k: dict_intersect(d1[k], d2[k]) if isinstance(d1[k], dict) else d1[k]
for k in d1.keys() & d2.keys()
}
2020-03-01 17:42:29 +00:00
class Singleton(type):
2020-03-06 21:39:32 +00:00
"""Singleton metaclass."""
2020-03-01 17:42:29 +00:00
_instances = {}
def __call__(cls, *args, **kwargs):
2020-03-06 21:39:32 +00:00
2020-03-01 17:42:29 +00:00
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
2020-03-01 17:42:29 +00:00
return cls._instances[cls]