From a35c041b79d953d00dc2a09cf8206956ca269bef Mon Sep 17 00:00:00 2001 From: Erkan Ozgur Yilmaz Date: Tue, 28 Sep 2021 23:00:07 +0300 Subject: [PATCH] * Added ``ScheduleMixin.to_unit`` that converts the given ``seconds`` to the given ``unit`` in consideration of the given ``schedule_model``. --- CHANGELOG.rst | 3 ++ stalker/__init__.py | 2 +- stalker/models/mixins.py | 44 ++++++++++++++++++++++++++ tests/mixins/test_scheduleMixin.py | 50 ++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8972f38..761685d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,9 @@ Stalker Changes causes the attribute to be filled with parent data. This is a slight change, but may break some workflows. +* Added ``ScheduleMixin.to_unit`` that converts the given ``seconds`` to the + given ``unit`` in consideration of the given ``schedule_model``. + 0.2.26 ====== diff --git a/stalker/__init__.py b/stalker/__init__.py index f68e1b8..ae44d1d 100644 --- a/stalker/__init__.py +++ b/stalker/__init__.py @@ -11,7 +11,7 @@ __title__ = "stalker" __description__ = 'A Production Asset Management (ProdAM) System' -__uri__ = 'http://github.com/eoyilmaz/stalker' +__uri__ = 'https://github.com/eoyilmaz/stalker' __doc__ = "%s <%s>" % (__description__, __uri__) __author__ = "Erkan Ozgur Yilmaz" diff --git a/stalker/models/mixins.py b/stalker/models/mixins.py index e2f0c32..e244770 100644 --- a/stalker/models/mixins.py +++ b/stalker/models/mixins.py @@ -1329,6 +1329,50 @@ def to_seconds(cls, timing, unit, model): return timing * lut[unit] + @classmethod + def to_unit(cls, seconds, unit, model): + """converts the ``seconds`` value to the given ``unit``, depending on + to the ``schedule_model`` the value will differ. So if the + ``schedule_model`` is 'effort' or 'length' then the ``seconds`` and + ``schedule_unit`` values are interpreted as work time, if the + ``schedule_model`` is 'duration' then the ``seconds`` and + ``schedule_unit`` values are considered as calendar time. + + :param int seconds: The seconds to convert + :param str unit: The unit value, one of 'min', 'h', 'd', 'w', 'm', 'y' + :param str model: The schedule model, one of 'effort', 'length' or + 'duration' + """ + if not unit: + return None + + lut = { + 'min': 60, + 'h': 3600, + 'd': 86400, + 'w': 604800, + 'm': 2419200, + 'y': 31536000 + } + + if model in ['effort', 'length']: + from stalker import defaults + day_wt = defaults.daily_working_hours * 3600 + week_wt = defaults.weekly_working_days * day_wt + month_wt = 4 * week_wt + year_wt = int(defaults.yearly_working_days) * day_wt + + lut = { + 'min': 60, + 'h': 3600, + 'd': day_wt, + 'w': week_wt, + 'm': month_wt, + 'y': year_wt + } + + return seconds / lut[unit] + @property def schedule_seconds(self): """Returns the schedule values as seconds, depending on to the diff --git a/tests/mixins/test_scheduleMixin.py b/tests/mixins/test_scheduleMixin.py index 637a11d..22f0a03 100644 --- a/tests/mixins/test_scheduleMixin.py +++ b/tests/mixins/test_scheduleMixin.py @@ -458,6 +458,56 @@ def test_to_seconds_is_working_properly(self): self.test_obj.schedule_model ) + def test_to_unit_is_working_properly(self): + """testing if the to_unit method is working properly + """ + from stalker import defaults + defaults.daily_working_hours = 9 + defaults.weekly_working_days = 5 + defaults.weekly_working_hours = 45 + defaults.yearly_working_days = 52.1428 * 5 + + test_values = [ + # effort values + ['effort', 1, 'min', 60], + ['effort', 10, 'min', 600], + ['effort', 20, 'min', 1200], + ['effort', 1, 'h', 3600], + ['effort', 1.01, 'h', 3636], + ['effort', 2, 'h', 7200], + ['effort', 1, 'd', 32400], + ['effort', 1, 'w', 162000], + ['effort', 1, 'm', 648000], + ['effort', 1, 'y', 8424000], + + # length values + ['length', 1, 'min', 60], + ['length', 540, 'min', 32400], + ['length', 1, 'h', 3600], + ['length', 1, 'd', 32400], + ['length', 1, 'w', 162000], + ['length', 1, 'm', 648000], + ['length', 1, 'y', 8424000], + + # duration values + ['duration', 1, 'min', 60], + ['duration', 60, 'min', 3600], + ['duration', 1440, 'min', 86400], + ['duration', 1, 'h', 3600], + ['duration', 1.5, 'h', 5400], + ['duration', 2, 'h', 7200], + ['duration', 1, 'd', 86400], + ['duration', 1, 'w', 604800], + ['duration', 1, 'm', 2419200], + ['duration', 1, 'y', 31536000] + ] + + for test_value in test_values: + schedule_model = test_value[0] + seconds = test_value[3] + schedule_unit = test_value[2] + assert test_value[1] == self.test_obj.to_unit(seconds, schedule_unit, schedule_model) + def test_schedule_seconds_is_working_properly(self): """testing if the schedule_seconds property is working properly """