Skip to content

Commit

Permalink
tox cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
annatisch committed May 17, 2016
1 parent c951cb8 commit 00ff9b3
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions ClientRuntimes/Python/msrest/msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,29 @@ def _classify(cls, response, objects):
def _convert_to_datatype(params, localtype):
"""Convert a dict-like object to the given datatype
"""
return _recursive_convert_to_datatype(params, localtype.__name__, importlib.import_module('..', localtype.__module__))
return _recursive_convert_to_datatype(
params,
localtype.__name__,
importlib.import_module('..', localtype.__module__))


def _recursive_convert_to_datatype(params, str_localtype, models_module):
"""Convert a dict-like object to the given datatype
"""
if isinstance(params, list):
return [_recursive_convert_to_datatype(data, str_localtype[1:-1], models_module) for data in params]
localtype = getattr(models_module, str_localtype) if hasattr(models_module, str_localtype) else None
return [_recursive_convert_to_datatype(
data,
str_localtype[1:-1],
models_module) for data in params]
localtype = getattr(models_module, str_localtype, None)
if not localtype:
return params
result = {key: _recursive_convert_to_datatype(params[key], localtype._attribute_map[key]['type'], models_module) for key in params}
result = {
key: _recursive_convert_to_datatype(
params[key],
localtype._attribute_map[key]['type'],
models_module) for key in params
}
return localtype(**result)


Expand Down Expand Up @@ -579,7 +590,8 @@ def serialize_rfc(attr, **kwargs):
"""
try:
if not attr.tzinfo:
_LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
_LOGGER.warning(
"Datetime with no tzinfo will be considered UTC.")
utc = attr.utctimetuple()
except AttributeError:
raise TypeError("RFC1123 object must be valid Datetime object.")
Expand All @@ -602,7 +614,8 @@ def serialize_iso(attr, **kwargs):
try:
try:
if not attr.tzinfo:
_LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
_LOGGER.warning(
"Datetime with no tzinfo will be considered UTC.")
utc = attr.utctimetuple()
except AttributeError:
raise TypeError(
Expand Down Expand Up @@ -631,9 +644,10 @@ def serialize_unix(attr, **kwargs):
if isinstance(attr, int):
return attr
try:
if not attr.tzinfo:
_LOGGER.warning("Datetime with no tzinfo will be considered UTC.")
return int(calendar.timegm(attr.utctimetuple()))
if not attr.tzinfo:
_LOGGER.warning(
"Datetime with no tzinfo will be considered UTC.")
return int(calendar.timegm(attr.utctimetuple()))
except AttributeError:
raise TypeError("Unix time object must be valid Datetime object.")

Expand Down Expand Up @@ -1088,10 +1102,9 @@ def deserialize_unix(attr):
:raises: DeserializationError if format invalid
"""
try:
date_obj = datetime.datetime.fromtimestamp(attr, TZ_UTC)
date_obj = datetime.datetime.fromtimestamp(attr, TZ_UTC)
except ValueError as err:
msg = "Cannot deserialize to unix datetime object."
raise_with_traceback(DeserializationError, msg, err)
else:
return date_obj

0 comments on commit 00ff9b3

Please sign in to comment.