def index0(self,obj): """ get the (0-based) index of the item in the list This uses a brute-force technique, and may not be suitable for large items in long lists """ item = checkobj(obj, wf=False) normed = norm_whitespace(item) #un-escape '{{' and '}} to do comparison... for chrs in ('{{','}}'): if chrs in normed: normed = normed.replace(chrs,chrs[0]) q = u' %s' % self.path s = self.cursor.execute(q, pretty_print=self.pretty_print, nsmap=self.nsmap) count = -1 found = False for k in s: count += 1 if norm_whitespace(k) == normed: found = True break if found: return count else: raise ValueError('item not in list')
def _xml_compare_text(t1, t2, strip): t1 = t1 or '' t2 = t2 or '' if strip: t1 = norm_whitespace(t1).strip() t2 = norm_whitespace(t2).strip() return t1 == t2
def test_run_ccs2ccsflat(self): testfile = join(dirname(__file__), 'empty.conf') # CHECK the auto-discovered filter properly tracked self.assertTrue('ccs2ccsflat' in self.flt_mgr.filters) out_obj = self.flt_mgr('ccs2ccsflat', ('file', testfile)) result = str_enc(out_obj('bytestring'), 'utf-8') # XXX print(result) # CHECK the externalized representation matches the original with open(testfile) as f: self.assertEqual(norm_whitespace(result), norm_whitespace(f.read()))
def test_run_double_ccs2ccsflat(self): testfile = join(dirname(__file__), 'empty.conf') # CHECK the filter defined in setUp method properly tracked self.assertTrue('double-ccs2ccsflat' in self.flt_mgr.filters) # perform the filter out_objs = self.flt_mgr('double-ccs2ccsflat', (('composite', ('file', 'file')), testfile, testfile) ) # CHECK resulting objects are not identical (separate processing) # NOTE there could be heuristic to capture duplicated work, but such # filter is phony anyway self.assertTrue(out_objs[0] is not out_objs[1]) # externalize outputs results = out_objs(('composite', ('bytestring', 'bytestring'))) # XXX print(results) # CHECK resulting externalized representation is, however, tha same self.assertEqual(*tuple(norm_whitespace for r in results)) # CHECK picked externalized representation matches the original with open(testfile) as f: self.assertEqual(norm_whitespace(str_enc(results[0], 'utf-8')), norm_whitespace(f.read()))
def index(self,obj): """ get the first (0-based) index of the item in the list """ item = checkobj(obj) normed = norm_whitespace(item) #q = u'declare namespace se = "http://www.modis.ispras.ru/sedna";' #q = u'' #q = u'declare option se:output "indent=no";' q = u' let $p := %s/* ,' % self.path q += u' $i := %s ' % item q += u' return <i>{index-of($p,$i)}</i>' try: s = self.cursor.execute(q, pretty_print=False, nsmap=self.nsmap) except DatabaseError: raise ValueError('item not in list') if s: y = fromstring(s.value) if y.text is not None: val = int(y.text.split()[0]) return val-1 raise ValueError('item not in list')
def index(self,obj): """ get the (0-based) index of the item in the list """ item = checkobj(obj, wf=False) normed = norm_whitespace(item) #q = u'declare namespace se = "http://www.modis.ispras.ru/sedna";' #q = u'' #q = u'declare option se:output "indent=no";' q = u' let $p := %s ,' % self.path q += u' $i := %s ' % item q += u' return <i>{index-of($p,$i)}</i>' try: s = self.cursor.execute(q,pretty_print=self.pretty_print, nsmap=self.nsmap) except DatabaseError: # this is probably an XQuery expression, not an XPath, so # do brute-force evaluation return self.index0(obj) if s: y = fromstring(s.value) if y.text is not None: val = int(y.text) return val-1 raise ValueError('item not in list')