def instantiate(self, context, processor): # get value(s) to format if self._value: value = Conversions.NumberValue(self._value.evaluate(context)) if not number.finite(value) or value < 0.5: # This is an error. However, recovery is to just write # the number as if the string() function was used. processor.writers[-1].text(Conversions.StringValue(value)) return else: values = [int(round(value))] else: node = context.node if self._level == SINGLE: value = self._single_value(context, node, self._count, self._from) if value == 0: values = [] else: values = [value] elif self._level == MULTIPLE: values = self._multiple_values(context, node) elif self._level == ANY: value = self._any_value(context, node) if value == 0: values = [] else: values = [value] else: # 'single' without count or from attributes value = 1 prev = node.previousSibling type = node.nodeType expanded = (node.namespaceURI, node.localName) while prev: if prev.nodeType == type and \ (prev.namespaceURI, prev.localName) == expanded: value += 1 prev = prev.previousSibling values = [value] # format the value(s) grouping_size = int(self._grouping_size.evaluate(context)) if grouping_size: grouping_separator = self._grouping_separator.evaluate(context) else: grouping_separator = None formatter = self._formatter if not formatter: format = self._format and self._format.evaluate( context) or DEFAULT_FORMAT lang = self._lang and self._lang.evaluate(context) or DEFAULT_LANG letter_value = self._letter_value.evaluate(context) or '' formatter = self.createFormatter(format, lang, letter_value) numstr = formatter.format(values, grouping_size, grouping_separator) processor.writers[-1].text(numstr) return
def instantiate(self, context, processor): # get value(s) to format if self._value: value = Conversions.NumberValue(self._value.evaluate(context)) if not number.finite(value) or value < 0.5: # This is an error. However, recovery is to just write # the number as if the string() function was used. processor.writers[-1].text(Conversions.StringValue(value)) return else: values = [int(round(value))] else: node = context.node if self._level == SINGLE: value = self._single_value(context, node, self._count, self._from) if value == 0: values = [] else: values = [value] elif self._level == MULTIPLE: values = self._multiple_values(context, node) elif self._level == ANY: value = self._any_value(context, node) if value == 0: values = [] else: values = [value] else: # 'single' without count or from attributes value = 1 prev = node.previousSibling type = node.nodeType expanded = (node.namespaceURI, node.localName) while prev: if prev.nodeType == type and \ (prev.namespaceURI, prev.localName) == expanded: value += 1 prev = prev.previousSibling values = [value] # format the value(s) grouping_size = int(self._grouping_size.evaluate(context)) if grouping_size: grouping_separator = self._grouping_separator.evaluate(context) else: grouping_separator = None formatter = self._formatter if not formatter: format = self._format and self._format.evaluate(context) or DEFAULT_FORMAT lang = self._lang and self._lang.evaluate(context) or DEFAULT_LANG letter_value = self._letter_value.evaluate(context) or '' formatter = self.createFormatter(format, lang, letter_value) numstr = formatter.format(values, grouping_size, grouping_separator) processor.writers[-1].text(numstr) return
def _strFloat(float): if number.finite(float): if float == round(float): return unicode(str(long(float))) else: # 12 digits is how many Python uses for str() return u'%0.12g' % float elif number.isnan(float): return u'NaN' elif float < 0: return u'-Infinity' else: return u'Infinity'
def Duration(context, seconds=None): """ The date:duration function returns a duration string representing the number of seconds specified by the argument string. If no argument is given, then the result of calling date:seconds without any arguments is used as a default argument. Implements version 1. """ if seconds is None: # The epoch for EXSLT is 1970-01-01T00:00:00Z # FIXME: we could code around this, but most (all?) platforms we # support have a time() epoch of 1970-01-01, so why bother. if time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0)) != time.timezone: warnings.warn("platform epoch != 1970-01-01", RuntimeWarning) # Don't use fractional seconds to keep with constructed dateTimes seconds = int(time.time()) else: seconds = Conversions.NumberValue(seconds) if not number.finite(seconds): # +/-Inf or NaN return u'' duration = _Duration(negative=(seconds < 0), seconds=abs(seconds)) return unicode(duration)
def compare(self, expected, actual, msg=None, func=cmp, diff=0, stackLevel=1, funcArgs={}): """ Uses func to compare the expected result with actual result of a regression test. diff is ignored. msg is an optional custom message to print if the comparison tests positive (i.e. the results differ). func is the comparison function to use, and must be a function that returns the same as the built-in cmp(). stackLevel affects exception reporting. funcArgs is an optional dictionary of keyword arguments that will be passed to the comparison function, if the dictionary is not empty. """ self.totalComparisons += 1 # Normalize float values if type(expected) == type(actual) == float: if number.finite(expected): expected = float(str(expected)) elif number.isnan(expected): expected = 'NaN' elif number.isinf(expected) > 0: expected = 'Inf' else: expected = '-Inf' if number.finite(actual): actual = float(str(actual)) elif number.isnan(actual): actual = 'NaN' elif number.isinf(actual) > 0: actual = 'Inf' else: actual = '-Inf' # Make sure there was a message for this comparison if not msg: if self.test: self.test.comparisons += 1 msg = 'Test %d' % (self.test.comparisons) else: msg = 'Test %d of all tests' % self.totalComparisons start = time.time() try: if funcArgs: res = func(expected, actual, **funcArgs) else: res = func(expected, actual) if res: # failure self.message(msg) if diff and self.verbose >= VERBOSE_DEBUG: self._diff(expected, actual) error = '%sExpected:%s %s\n' % (self.GREEN, self.NORMAL, repr(expected)) error += '%sCompared:%s %s' % (self.RED, self.NORMAL, repr(actual)) self.error(error, stackLevel=(stackLevel+1)) return 0 finally: end = time.time() if self.test: self.test.compareTime += (end - start) # success return 1
def compare(self, expected, actual, msg=None, func=cmp, diff=0, stackLevel=1, funcArgs={}): """ Uses func to compare the expected result with actual result of a regression test. diff is ignored. msg is an optional custom message to print if the comparison tests positive (i.e. the results differ). func is the comparison function to use, and must be a function that returns the same as the built-in cmp(). stackLevel affects exception reporting. funcArgs is an optional dictionary of keyword arguments that will be passed to the comparison function, if the dictionary is not empty. """ self.totalComparisons += 1 # Normalize float values if type(expected) == type(actual) == float: if number.finite(expected): expected = float(str(expected)) elif number.isnan(expected): expected = 'NaN' elif number.isinf(expected) > 0: expected = 'Inf' else: expected = '-Inf' if number.finite(actual): actual = float(str(actual)) elif number.isnan(actual): actual = 'NaN' elif number.isinf(actual) > 0: actual = 'Inf' else: actual = '-Inf' # Make sure there was a message for this comparison if not msg: if self.test: self.test.comparisons += 1 msg = 'Test %d' % (self.test.comparisons) else: msg = 'Test %d of all tests' % self.totalComparisons start = time.time() try: if funcArgs: res = func(expected, actual, **funcArgs) else: res = func(expected, actual) if res: # failure self.message(msg) if diff and self.verbose >= VERBOSE_DEBUG: self._diff(expected, actual) error = '%sExpected:%s %s\n' % (self.GREEN, self.NORMAL, repr(expected)) error += '%sCompared:%s %s' % (self.RED, self.NORMAL, repr(actual)) self.error(error, stackLevel=(stackLevel + 1)) return 0 finally: end = time.time() if self.test: self.test.compareTime += (end - start) # success return 1