Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfleischer committed Dec 27, 2023
2 parents ee5419b + b0e4493 commit a95e742
Show file tree
Hide file tree
Showing 33 changed files with 1,685 additions and 1,351 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,9 @@ from cloudmesh.common.StopWatch import StopWatch
import time

StopWatch.start("a")

time.sleep(3)

StopWatch.stop("a")

StopWatch.status("a", True)

StopWatch.benchmark()
```

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.44
5.0.48
5 changes: 5 additions & 0 deletions makefile.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
UNAME=$(shell uname)
VERSION=`head -1 VERSION`
COMMAND := $(subst cloudmesh-,,$(package))

define banner
@echo
Expand All @@ -19,6 +20,10 @@ source: welcome ## Install the package in source mode
pip: welcome ## Install the package in pip mode
pip install -e . --config-settings editable_mode=strict


readme:
cms man readme -p --command=${COMMAND}

##############################################################################
# CHECK
##############################################################################
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires = [

[project]
name = "cloudmesh-common"
version = "5.0.44"
version = "5.0.48"
description = "A set of useful APIs for cloudmesh"
readme = "README.md"
requires-python = ">=3.8"
Expand Down Expand Up @@ -75,4 +75,4 @@ Changelog = "https://github.com/cloudmesh/cloudmesh-common/blob/main/CHANGELOG.m

[tool.setuptools.packages.find]
where = ["src"]
include = ["cloudmesh.common"]
include = ["cloudmesh.common", "cloudmesh.common.*"]
26 changes: 11 additions & 15 deletions src/cloudmesh/common/Benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from cloudmesh.common.StopWatch import StopWatch
from cloudmesh.common.util import path_expand
from cloudmesh.common.variables import Variables
#
#


# pylint: disable=C0103
# noinspection PyPep8Naming
Expand All @@ -31,7 +30,6 @@ class Benchmark(object):
This class relies on the StopWatch class for timer functionality.
"""


@staticmethod
def debug():
"""Sets the CMS shell variables for trace, debug, and verbosity.
Expand Down Expand Up @@ -107,11 +105,13 @@ def Stop():
StopWatch.status(Benchmark.name(with_class=True), True)

@staticmethod
def print(sysinfo=True,
csv=True,
tag=None,
node=None,
user=None, ):
def print(
sysinfo=True,
csv=True,
tag=None,
node=None,
user=None,
):
"""prints the benchmark information with all timers
Args:
Expand Down Expand Up @@ -142,17 +142,13 @@ def yaml(path, n):
Usage:
Benchmark.yaml("./example.yaml", 10)
"""
cm = {
"cloudmesh": {}
}
cm = {"cloudmesh": {}}
for i in range(0, n):
cm["cloudmesh"][f"service{i}"] = {
"attribute": f"service{i}"
}
cm["cloudmesh"][f"service{i}"] = {"attribute": f"service{i}"}
pprint(cm)
location = path_expand(path)

with open(location, 'w') as yaml_file:
with open(location, "w") as yaml_file:
yaml.dump(cm, yaml_file, default_flow_style=False)

# noinspection SpellCheckingInspection
Expand Down
58 changes: 47 additions & 11 deletions src/cloudmesh/common/DateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class DateTime(object):
@staticmethod
def now():
return str(TIME.datetime.now())

@staticmethod
def utc_now():
return str(TIME.datetime.utcnow())
return str(TIME.datetime.now(TIME.UTC))

@staticmethod
def natural(time):
Expand All @@ -52,7 +52,11 @@ def natural(time):

@staticmethod
def words(time):
return TIME.datetime.strftime(time, "%a %w %b %Y, %H:%M:%S UTC")
if type(time) == TIME.datetime:
t = time
else:
t = DateTime.datetime(time)
return TIME.datetime.strftime(t, "%a %w %b %Y, %H:%M:%S UTC")

@staticmethod
def datetime(time):
Expand All @@ -67,14 +71,30 @@ def humanize(time):

@staticmethod
def string(time):
if type(time) == str:
d = parser.parse(time)
if type(time) == TIME:
d = str(time)
else:
d = time
return d
try:
d = parser.parse(time)
except:
d = DateTime.utc_to_local(time)
return str(d)

@staticmethod
def get(time_str):
return parser.parse(time_str)

@staticmethod
def delta(n):
"""
Returns a timedelta object representing a time duration of `n` seconds.
Parameters:
n (int): The number of seconds for the time duration.
Returns:
timedelta: A timedelta object representing the time duration in datetime and not string format.
"""
return TIME.timedelta(seconds=n)

@staticmethod
Expand All @@ -87,18 +107,34 @@ def local(time):

@staticmethod
def utc_to_local(time):
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%f'
if "." in str(time):
TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"
else:
TIME_FORMAT = "%Y-%m-%d %H:%M:%S"

utc = TIME.datetime.strptime(str(time), TIME_FORMAT)
local_dt = utc.replace(tzinfo=ZoneInfo('UTC')).astimezone(tzlocal.get_localzone())
local_dt = utc.replace(tzinfo=ZoneInfo("UTC")).astimezone(
tzlocal.get_localzone()
)
return local_dt.strftime(TIME_FORMAT) + " " + str(DateTime.timezone)

def datetime(time_str):
d = parser.parse(time_str)
return d

@staticmethod
def add(one, two):
return DateTime.datetime(DateTime.datetime(one)) + DateTime.datetime(two)


if __name__ == "__main__":
start = DateTime.now()
stop = DateTime.now() + DateTime.delta(1)
stop = DateTime.add(DateTime.now(), DateTime.delta(1))

stop2 = DateTime.datetime(DateTime.now()), DateTime.datetime(DateTime.delta(2))

print("START", start)
print("STOP", stop)
print("STOP", stop, stop2)
print("HUMANIZE STOP", DateTime.humanize(stop - start))
print("LOCAL", DateTime.local(start))
print("UTC", DateTime.utc(start))
Expand Down
38 changes: 20 additions & 18 deletions src/cloudmesh/common/FlatDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def flatme(d):
return o


def flatten(d, parent_key='', sep='__'):
def flatten(d, parent_key="", sep="__"):
"""flattens the dict into a one dimensional dictionary
Args:
Expand Down Expand Up @@ -299,17 +299,19 @@ def load(self, content=None, expand=True, sep="."):
elif type(content) == dict:
self.loadd(content=content, sep=".")
elif os.path.isfile(str(content)):
print ("file")
print("file")
self.loadf(filename=content, sep=".")
elif type(content) == str:
self.loads(content=content, sep=".")
else:
config = None
self.__init__(config, sep=sep)
e = expand_config_parameters(flat=self.__dict__,
expand_yaml=True,
expand_os=self.expand_os,
expand_cloudmesh=self.expand_cloudmesh or self.expand_cm)
e = expand_config_parameters(
flat=self.__dict__,
expand_yaml=True,
expand_os=self.expand_os,
expand_cloudmesh=self.expand_cloudmesh or self.expand_cm,
)
self.__dict__ = e

def apply_in_string(self, content):
Expand Down Expand Up @@ -398,8 +400,7 @@ def object_to_dict(cls, obj):
return dict_obj


def read_config_parameters(filename=None,
d=None):
def read_config_parameters(filename=None, d=None):
"""This file reads in configuration parameters defined in a yaml file and
produces a flattend dict. It reads in the yaml date from a filename and/or
a string. If both are specified the data in the filename will be read first
Expand Down Expand Up @@ -538,12 +539,14 @@ def read_config_parameters_from_dict(content=None, d=None):
return config


def expand_config_parameters(flat=None,
expand_yaml=True,
expand_os=True,
expand_cloudmesh=True,
debug=False,
depth=100):
def expand_config_parameters(
flat=None,
expand_yaml=True,
expand_os=True,
expand_cloudmesh=True,
debug=False,
depth=100,
):
"""expands all variables in the flat dict if they are specified in the values of the flatdict.
Args:
Expand Down Expand Up @@ -588,7 +591,7 @@ def expand_config_parameters(flat=None,

if expand_yaml:
found = True
for i in range (0,depth):
for i in range(0, depth):
for variable in flat.keys():
name = "{" + variable + "}"
value = flat[variable]
Expand All @@ -597,7 +600,6 @@ def expand_config_parameters(flat=None,
print("found", variable, "->", value)
txt = txt.replace(name, str(value))


if "{os." in values and expand_os:
for variable in os.environ:
if variable != "_":
Expand Down Expand Up @@ -648,7 +650,7 @@ def expand_config_parameters(flat=None,
return config


'''
"""
def main():
Expand Down Expand Up @@ -736,4 +738,4 @@ def main():
if __name__ == "__main__":
main()
'''
"""
Loading

0 comments on commit a95e742

Please sign in to comment.