Exemple #1
0
def shallow_xml_repr(value, host = None, memo = None):

    if memo is None:
        memo = list()
    cycle = value in memo
    if value not in memo:
        memo.append(value)

    if any(value is x for x in (None, True, False)):
        return tags.span(value)
    if hasattr(value, '__class__') and value.__class__ in (int, bool, float, object):
        return tags.span(value)
    if hasattr(value, '__class__') and value.__class__ in (str, unicode):
        return shallow_str_xml_repr(value)
    if cycle:
        return name_repr(value, memo)
    if hasattr(value, '__class__') and hasattr(value.__class__, '__rich_shallow_xml_repr__'):
        return value.__rich_shallow_xml_repr__(host, memo)
    if hasattr(value, '__class__') and hasattr(value.__class__, '__shallow_xml_repr__'):
        return value.__shallow_xml_repr__()
    if  hasattr(value, '__class__') and hasattr(value.__class__, '__xml_repr__'):
        return getattr(value, '__xml_repr__')()
    if isinstance(value, dict):
        return shallow_dict_xml_repr(value, host, memo)
    if isinstance(value, tuple):
        return shallow_tuple_xml_repr(value, host, memo)
    if isinstance(value, list):
        return shallow_list_xml_repr(value, host, memo)

    return name_repr(value, memo)
Exemple #2
0
 def __contains__(self, key):
     if isinstance(key, Name):
         return any(
             element.name == key
             for element in self.elements
             if isinstance(element, Tag)
         )
     elif isinstance(key, Identifier):
         return any(
             'id' in element.attributes and
             element.attributes['id'] == key
             for element in self.elements
             if isinstance(element, Tag)
         )
     elif isinstance(key, str):
         return key in self.attributes
     elif isinstance(key, int):
         return key in self.elements
     else:
         raise TypeError()
Exemple #3
0
def parse_rules(labels, rules_files):

    edges = tuple(
        chain(*[
            blocks(uncomment(rules_file))
            for rules_file in rules_files
        ])
    )

    labels = set(labels)
    for row in edges:
        for expression in row:
            if re.match(r'^[\w\d_]+$', expression):
                labels.add(expression)

    # table of nodes to nodes that are less than that node
    lt_table = Rules()

    for row in edges: 

        equivalences = []
        for expression in row:
            matchers = tuple(
                re.compile('^%s$' % part)
                for part in expression.split(' ')
            )
            matches = tuple(
                label
                for label in labels
                if any(
                    matcher.match(label)
                    for matcher in matchers
                )
            )
            if not matches:
                #print 'no labels match the expression', `expression`
                pass
            equivalences.append(matches)

        for index in range(1, len(equivalences)):
            lessers = equivalences[index - 1]
            greaters = equivalences[index]
            for lesser in lessers:
                for greater in greaters:
                    lt_table[greater].add(lesser)

    return lt_table
Exemple #4
0
 def next():
     try:
         result = generator.next()
         if any(
             isinstance(result, klass)
             for klass in (int, float)
         ):
             reactor.callLater(result, next)
         else:
             raise TypeError(
                 'anynchronous generators must yield ' + 
                 'seconds to wait as numeric ' +
                 'values.  got %s.' % 
                 `result.__class__.__name__`
             )
     except StopIteration:
         pass