Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace usage of datetime library types with hightime types #1449

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ All notable changes to this project will be documented in this file.
* ### ALL
* #### Added
* #### Changed
* Change the type of applicable properties and method parameters from `datetime.timedelta` to `hightime.timedelta` and from `datetime.datetime` to `hightime.datetime`. - [#744](https://github.com/ni/nimi-python/issues/744), [#1368](https://github.com/ni/nimi-python/issues/1368), [#1382](https://github.com/ni/nimi-python/issues/1382), [#1397](https://github.com/ni/nimi-python/issues/1397)
* #### Removed
* ### NI-DCPower
* #### Added
Expand Down
1 change: 0 additions & 1 deletion build/helper/codegen_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,6 @@ def get_dictionary_snippet(d, indent=4):
'type': 'ViReal64',
'use_in_python_api': True,
'python_api_converter_name': 'timedelta_converter_seconds_real64',
'python_api_converter_type': 'datetime.timedelta',
},
{ # 15
'ctypes_type': 'ViString',
Expand Down
2 changes: 1 addition & 1 deletion build/helper/metadata_add_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def _add_default_attribute_class(a, attributes):
'''Set 'attribute_class' if not set.

By default, the 'attribute_class' is only based on the 'type'.
It can be set in attributes_addon if we want to convert to/from a different datatype, such as datetime.timedelta
It can be set in attributes_addon if we want to convert to/from a different datatype, such as hightime.timedelta
'''
if 'attribute_class' not in attributes[a]:
attributes[a]['attribute_class'] = 'Attribute' + attributes[a]['type']
Expand Down
6 changes: 3 additions & 3 deletions build/templates/_attributes.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ${template_parameters['encoding_tag']}
%>\
import ${module_name}._converters as _converters

import datetime
import hightime


class Attribute(object):
Expand All @@ -28,7 +28,7 @@ class AttributeViInt32(Attribute):
class AttributeViInt32TimeDeltaMilliseconds(Attribute):

def __get__(self, session, session_type):
return datetime.timedelta(milliseconds=session._get_attribute_vi_int32(self._attribute_id))
return hightime.timedelta(milliseconds=session._get_attribute_vi_int32(self._attribute_id))

def __set__(self, session, value):
session._set_attribute_vi_int32(self._attribute_id, _converters.convert_timedelta_to_milliseconds_int32(value).value)
Expand All @@ -55,7 +55,7 @@ class AttributeViReal64(Attribute):
class AttributeViReal64TimeDeltaSeconds(Attribute):

def __get__(self, session, session_type):
return datetime.timedelta(seconds=session._get_attribute_vi_real64(self._attribute_id))
return hightime.timedelta(seconds=session._get_attribute_vi_real64(self._attribute_id))

def __set__(self, session, value):
session._set_attribute_vi_real64(self._attribute_id, _converters.convert_timedelta_to_seconds_real64(value).value)
Expand Down
45 changes: 24 additions & 21 deletions build/templates/_converters.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import ${module_name}.errors as errors

import array
import collections
import datetime
import hightime
import numbers
import pytest

from functools import singledispatch

Expand Down Expand Up @@ -149,7 +150,7 @@ def convert_repeated_capabilities_without_prefix(repeated_capability):

def _convert_timedelta(value, library_type, scaling):
try:
# We first assume it is a datetime.timedelta object
# We first assume it is a timedelta object
scaled_value = value.total_seconds() * scaling
except AttributeError:
# If that doesn't work, assume it is a value in seconds
Expand All @@ -176,15 +177,15 @@ def convert_timedeltas_to_seconds_real64(values):


def convert_seconds_real64_to_timedelta(value):
return datetime.timedelta(seconds=value)
return hightime.timedelta(seconds=value)


def convert_seconds_real64_to_timedeltas(values):
return [convert_seconds_real64_to_timedelta(i) for i in values]


def convert_month_to_timedelta(months):
return datetime.timedelta(days=(30.4167 * months))
return hightime.timedelta(days=(30.4167 * months))


# This converter is not called from the normal codegen path for function. Instead it is
Expand Down Expand Up @@ -334,11 +335,11 @@ def test_convert_init_with_options_dictionary():

# Tests - time
def test_convert_timedelta_to_seconds_double():
test_result = convert_timedelta_to_seconds_real64(datetime.timedelta(seconds=10))
test_result = convert_timedelta_to_seconds_real64(hightime.timedelta(seconds=10))
assert test_result.value == 10.0
assert isinstance(test_result, _visatype.ViReal64)
test_result = convert_timedelta_to_seconds_real64(datetime.timedelta(seconds=-1))
assert test_result.value == -1
test_result = convert_timedelta_to_seconds_real64(hightime.timedelta(nanoseconds=-0.5))
assert test_result.value == pytest.approx(-5e-10)
assert isinstance(test_result, _visatype.ViReal64)
test_result = convert_timedelta_to_seconds_real64(10.5)
assert test_result.value == 10.5
Expand All @@ -349,11 +350,11 @@ def test_convert_timedelta_to_seconds_double():


def test_convert_timedelta_to_milliseconds_int32():
test_result = convert_timedelta_to_milliseconds_int32(datetime.timedelta(seconds=10))
test_result = convert_timedelta_to_milliseconds_int32(hightime.timedelta(seconds=10))
assert test_result.value == 10000
assert isinstance(test_result, _visatype.ViInt32)
test_result = convert_timedelta_to_milliseconds_int32(datetime.timedelta(seconds=-1))
assert test_result.value == -1000
test_result = convert_timedelta_to_milliseconds_int32(hightime.timedelta(seconds=-5))
assert test_result.value == -5000
assert isinstance(test_result, _visatype.ViInt32)
test_result = convert_timedelta_to_milliseconds_int32(10.5)
assert test_result.value == 10500
Expand All @@ -364,26 +365,28 @@ def test_convert_timedelta_to_milliseconds_int32():


def test_convert_timedeltas_to_seconds_real64():
time_values = [10.5, -1]
time_values = [10.5, -5e-10]
test_result = convert_timedeltas_to_seconds_real64(time_values)
assert all([actual.value == expected for actual, expected in zip(test_result, time_values)])
assert all([actual.value == pytest.approx(expected) for actual, expected in zip(test_result, time_values)])
assert all([isinstance(i, _visatype.ViReal64) for i in test_result])
timedeltas = [datetime.timedelta(seconds=s, milliseconds=ms) for s, ms in zip([10, -1], [500, 0])]
test_result = convert_timedeltas_to_seconds_real64(timedeltas)
assert all([actual.value == expected for actual, expected in zip(test_result, time_values)])
test_input = [hightime.timedelta(seconds=10.5), hightime.timedelta(nanoseconds=-0.5)]
test_result = convert_timedeltas_to_seconds_real64(test_input)
assert all([actual.value == pytest.approx(expected) for actual, expected in zip(test_result, time_values)])
assert all([isinstance(i, _visatype.ViReal64) for i in test_result])


def test_convert_seconds_real64_to_timedelta():
time_value = 10.5
timedelta = convert_seconds_real64_to_timedelta(time_value)
assert timedelta.total_seconds() == time_value
time_value = -5e-10
test_result = convert_seconds_real64_to_timedelta(time_value)
assert test_result.total_seconds() == pytest.approx(time_value)
assert isinstance(test_result, hightime.timedelta)


def test_convert_seconds_real64_to_timedeltas():
time_values = [10.5, -1]
timedeltas = convert_seconds_real64_to_timedeltas(time_values)
assert all([actual.total_seconds() == expected for actual, expected in zip(timedeltas, time_values)])
time_values = [10.5, -5e-10]
test_result = convert_seconds_real64_to_timedeltas(time_values)
assert all([actual.total_seconds() == pytest.approx(expected) for actual, expected in zip(test_result, time_values)])
assert all([isinstance(x, hightime.timedelta) for x in test_result])


<%
Expand Down
4 changes: 2 additions & 2 deletions build/templates/conf.py.mako
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%
from datetime import datetime
current_year = datetime.today().year
import datetime
current_year = datetime.datetime.today().year

with open('./VERSION') as vf:
global_version = vf.read().strip()
Expand Down
2 changes: 1 addition & 1 deletion build/templates/session.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ${template_parameters['encoding_tag']}
%>\
import array # noqa: F401
import ctypes
import datetime # noqa: F401
% if config['use_locking']:
# Used by @ivi_synchronized
from functools import wraps
Expand All @@ -41,6 +40,7 @@ import ${module_name}.errors as errors
import ${module_name}.${c['file_name']} as ${c['file_name']} # noqa: F401
% endfor

import hightime
% if config['uses_nitclk']:
import nitclk
% endif
Expand Down
2 changes: 1 addition & 1 deletion build/templates/session.py/datetime_wrappers.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
${helper.get_function_docstring(f, False, config, indent=8)}
'''
${output_params} = self.${called_function['python_name']}(${helper.get_params_snippet(f, helper.ParameterUsageOptions.SESSION_METHOD_CALL)})
return datetime.datetime(year, month, day, hour, minute)
return hightime.datetime(year, month, day, hour, minute)

1 change: 1 addition & 0 deletions build/templates/setup.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ setup(
install_requires=[
'enum34;python_version<"3.4"',
'singledispatch;python_version<"3.4"',
'hightime',
% if config['uses_nitclk']:
'nitclk',
% endif
Expand Down
1 change: 1 addition & 0 deletions build/templates/tox-system_tests.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ deps =
${module_name}-system_tests: pytest
${module_name}-system_tests: coverage
${module_name}-system_tests: numpy
${module_name}-system_tests: hightime
${module_name}-system_tests: scipy
${module_name}-system_tests: fasteners
${module_name}-system_tests: pytest-json
Expand Down
Loading