From 32b1d056ea4fbfc4a69a4b94f995cdb639d66128 Mon Sep 17 00:00:00 2001 From: Jono Xia Date: Wed, 6 Feb 2019 18:44:57 -0800 Subject: [PATCH] Issue #78 removed default decimals in fact constructor, removed 'units=pure' edge case. --- oblib/data_model.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/oblib/data_model.py b/oblib/data_model.py index b51d6b1..bef06cf 100644 --- a/oblib/data_model.py +++ b/oblib/data_model.py @@ -466,9 +466,9 @@ def __init__(self, concept, context, unit, value, decimals=None, precision=None) raise OBException("Fact given both precision and decimals - use only one.") self.decimals = decimals self.precision = precision - if decimals is None and precision is None: - # Default to 2 decimals: - self.decimals = 2 + # FUTURE TODO: decide if the Concept is numeric or non-numeric. If numeric, + # either require a decimals/precision or provide a default. If non-numeric, + # don't allow decimals/precision to be set. # Fill in the id property with a UUID: self.id = identifier.identifier() # Only used when exporting JSON @@ -478,17 +478,13 @@ def _toXML(self): Return the Fact as an XML element. """ attribs = {"contextRef": self.context.get_id()} - # TODO the "pure" part is probably wrong now. - # also the self.unit may not be correct unitRef? not sure + # TODO the self.unit may not be correct unitRef? not sure if self.unit is not None: attribs["unitRef"] = self.unit - if self.unit == "pure": - attribs["decimals"] = "0" - else: - if self.decimals is not None: - attribs["decimals"] = str(self.decimals) - elif self.precision is not None: - attribs["precision"] = str(self.precision) + if self.decimals is not None: + attribs["decimals"] = str(self.decimals) + elif self.precision is not None: + attribs["precision"] = str(self.precision) elem = Element(self.concept, attrib=attribs) if self.unit == "pure": elem.text = "%d" % self.value @@ -505,13 +501,10 @@ def _toJSON(self): aspects["concept"] = self.concept if self.unit is not None: aspects["unit"] = self.unit - if self.unit == "pure": - aspects["decimals"] = "0" - else: - if self.decimals is not None: - aspects["decimals"] = str(self.decimals) - elif self.precision is not None: - aspects["precision"] = str(self.precision) + if self.decimals is not None: + aspects["decimals"] = str(self.decimals) + elif self.precision is not None: + aspects["precision"] = str(self.precision) if isinstance( self.value, datetime.datetime): value_str = self.value.strftime("%Y-%m-%dT%H:%M:%S")