From cc323ce925902fc4043ebd93876082e71058dff6 Mon Sep 17 00:00:00 2001 From: chen-li Date: Wed, 20 Jan 2016 17:49:15 +0800 Subject: [PATCH] Using 'dt' as alias for datetime imports Many well-known libraries have a conventions regarding which alias to use. Choosing a different one will not break the code but makes it less readable and harder to maintain for other developers. Unless there is specific reason, it is usually best to use the recommended alias which most people are already familiar with. For datetime this convention is dt. Change-Id: Ifba7b7beafdac8377b98ce952b7d3b9dade01670 Closes-Bug: #1535786 --- .../openstack/scenarios/ceilometer/utils.py | 6 +++--- tests/hacking/README.rst | 1 + tests/hacking/checks.py | 12 ++++++++++++ .../openstack/scenarios/ceilometer/test_utils.py | 6 +++--- tests/unit/test_hacking.py | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/rally/plugins/openstack/scenarios/ceilometer/utils.py b/rally/plugins/openstack/scenarios/ceilometer/utils.py index 2733804b..b366a63e 100644 --- a/rally/plugins/openstack/scenarios/ceilometer/utils.py +++ b/rally/plugins/openstack/scenarios/ceilometer/utils.py @@ -12,7 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime +import datetime as dt import six @@ -64,7 +64,7 @@ class CeilometerScenario(scenario.OpenStackScenario): if v: sample.update({k: v}) len_meta = len(metadata_list) if metadata_list else 0 - now = timestamp or datetime.datetime.utcnow() + now = timestamp or dt.datetime.utcnow() samples = [] for i in six.moves.xrange(count): if i and not (i % batch_size): @@ -72,7 +72,7 @@ class CeilometerScenario(scenario.OpenStackScenario): samples = [] sample_item = dict(sample) sample_item["timestamp"] = ( - now - datetime.timedelta(seconds=(interval * i)) + now - dt.timedelta(seconds=(interval * i)) ).isoformat() if metadata_list: # NOTE(idegtiarov): Adding more than one template of metadata diff --git a/tests/hacking/README.rst b/tests/hacking/README.rst index e99dbee4..d6d21a14 100644 --- a/tests/hacking/README.rst +++ b/tests/hacking/README.rst @@ -29,6 +29,7 @@ Rally Specific Commandments * [N353] - Ensure that unicode() function is not uset because of absence in py3 * [N354] - Ensure that ``:raises: Exception`` is not used * [N355] - Ensure that we use only "new-style" Python classes +* [N356] - Ensure using ``dt`` as alias for ``datetime`` * [N360-N370] - Reserved for rules related to CLI * [N360] - Ensure that CLI modules do not use ``rally.common.db`` * [N361] - Ensure that CLI modules do not use ``rally.common.objects`` diff --git a/tests/hacking/checks.py b/tests/hacking/checks.py index f67cb268..c422175a 100644 --- a/tests/hacking/checks.py +++ b/tests/hacking/checks.py @@ -64,6 +64,7 @@ re_raises = re.compile( re_db_import = re.compile(r"^from rally.common import db") re_objects_import = re.compile(r"^from rally.common import objects") re_old_type_class = re.compile(r"^\s*class \w+(\(\))?:") +re_datetime_alias = re.compile(r"^(from|import) datetime(?!\s+as\s+dt$)") def skip_ignored_lines(func): @@ -489,6 +490,16 @@ def check_old_type_class(logical_line, physical_line, filename): "``object`` or another new-style class.") +@skip_ignored_lines +def check_datetime_alias(logical_line, physical_line, filename): + """Ensure using ``dt`` as alias for ``datetime`` + + N356 + """ + if re_datetime_alias.search(logical_line): + yield (0, "N356 Please use ``dt`` as alias for ``datetime``.") + + @skip_ignored_lines def check_db_imports_in_cli(logical_line, physical_line, filename): """Ensure that CLI modules do not use ``rally.common.db`` @@ -534,6 +545,7 @@ def factory(register): register(check_dict_formatting_in_string) register(check_using_unicode) register(check_raises) + register(check_datetime_alias) register(check_db_imports_in_cli) register(check_objects_imports_in_cli) register(check_old_type_class) diff --git a/tests/unit/plugins/openstack/scenarios/ceilometer/test_utils.py b/tests/unit/plugins/openstack/scenarios/ceilometer/test_utils.py index 44050fb6..5021f33a 100644 --- a/tests/unit/plugins/openstack/scenarios/ceilometer/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/ceilometer/test_utils.py @@ -13,7 +13,7 @@ # under the License. import copy -import datetime +import datetime as dt from dateutil import parser import mock @@ -33,7 +33,7 @@ class CeilometerScenarioTestCase(test.ScenarioTestCase): def test__make_samples_no_batch_size(self): self.scenario.generate_random_name = mock.Mock( return_value="fake_resource") - test_timestamp = datetime.datetime(2015, 10, 20, 14, 18, 40) + test_timestamp = dt.datetime(2015, 10, 20, 14, 18, 40) result = list(self.scenario._make_samples(count=2, interval=60, timestamp=test_timestamp)) self.assertEqual(1, len(result)) @@ -51,7 +51,7 @@ class CeilometerScenarioTestCase(test.ScenarioTestCase): def test__make_samples_batch_size(self): self.scenario.generate_random_name = mock.Mock( return_value="fake_resource") - test_timestamp = datetime.datetime(2015, 10, 20, 14, 18, 40) + test_timestamp = dt.datetime(2015, 10, 20, 14, 18, 40) result = list(self.scenario._make_samples(count=4, interval=60, batch_size=2, timestamp=test_timestamp)) diff --git a/tests/unit/test_hacking.py b/tests/unit/test_hacking.py index d8dec906..821cda69 100644 --- a/tests/unit/test_hacking.py +++ b/tests/unit/test_hacking.py @@ -376,3 +376,17 @@ class HackingTestCase(test.TestCase): checkres = checks.check_old_type_class(line, line, "fakefile") self.assertIsNotNone(next(checkres)) self.assertEqual([], list(checkres)) + + def test_check_datetime_alias(self): + lines = ["import datetime as date", + "import datetime", + "import datetime as dto", + "from datetime import datetime as dtime"] + + for line in lines: + checkres = checks.check_datetime_alias(line, line, "fakefile") + self.assertIsNotNone(next(checkres)) + self.assertEqual([], list(checkres)) + + line = "import datetime as dt" + checkres = checks.check_datetime_alias(line, line, "fakefile")