Changeset 429

Show
Ignore:
Timestamp:
05/21/07 02:29:18 (2 years ago)
Author:
athomas
Message:

cly: Some more doctest updates, Variable.parse() is now provided with a context.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/extra.py

    r428 r429  
    4444 
    4545 
    46 def integrate(root, *functions): 
     46def integrate(root, functions): 
    4747    """Use heuristics to integrate functions into a node. 
    4848 
     
    5151    each argument with a default is converted into a non-optional Variable. 
    5252 
    53     >>> from cly import Grammar, Parser 
     53    >>> from cly import Grammar 
    5454    >>> def foo_bar(one, two, three="Baz", four="Waz"): 
    5555    ...   print one, two, three, four 
    5656    >>> grammar = Grammar() 
    57     >>> integrate(grammar, foo_bar
     57    >>> integrate(grammar, [foo_bar]
    5858    >>> for i in grammar.walk(): print i 
    5959    <Grammar:/> 
     
    107107 
    108108 
    109 def quickstart(*grammar_or_callables, **interact_args): 
     109def quickstart(grammar_or_callables, *interact_args, **interact_kwargs): 
    110110    """Start an interactive session from a grammar, or from inspecting a set of 
    111111    callables.""" 
    112     if len(grammar_or_callables) == 1 and isinstance(grammar_or_callables[0], 
    113                                                      (Grammar, Parser)): 
    114         interact = Interact(grammar_or_callables[0], **interact_args) 
     112    if isinstance(grammar_or_callables, (Grammar, Parser)): 
     113        interact = Interact(grammar_or_callables, *interact_args, 
     114                            **interact_kwargs) 
    115115    else: 
    116116        grammar = Grammar() 
    117         integrate(grammar, *grammar_or_callables) 
    118         interact = Interact(grammar, **interact_args) 
     117        integrate(grammar, grammar_or_callables) 
     118        interact = Interact(grammar, *interact_args, **interact_kwargs) 
    119119    interact.interact_loop() 
    120120 
  • cly/trunk/cly/__init__.py

    r428 r429  
    321321 
    322322    def children(self, context, follow=False): 
    323         """Iterate over child nodes, optionally follow()ing branches.""" 
     323        """Iterate over child nodes, optionally follow()ing branches. 
     324        >>> tree = Node('One')(two=Node('Two', three=Node('Three'), 
     325        ...                             four=Node('Four')), five=Alias('../two/*')) 
     326        >>> context = Context(None, None) 
     327        >>> for i in tree.children(context): print i 
     328        <Alias:/five for /two/*> 
     329        <Node:/two> 
     330        >>> for i in tree.children(context, follow=True): print i 
     331        <Node:/two/four> 
     332        <Node:/two/three> 
     333        <Node:/two> 
     334        """ 
    324335        for child in self: 
    325336            if child.valid(context): 
     
    345356 
    346357    def next(self, context): 
    347         """Return an iterable of the next potential nodes.""" 
     358        """Return an iterable over the set of next candidate nodes.""" 
    348359        for child in self.children(context, follow=True): 
    349360            yield child 
     
    652663 
    653664    def selected(self, context, match): 
    654         """Convert the match to a value with self.parse_value(), then add 
     665        """Convert the match to a value with self.parse(), then add 
    655666        the result to the context "vars" member. 
    656667 
     
    664675        """ 
    665676        try: 
    666             value = self.parse(match) 
     677            value = self.parse(context, match) 
    667678        except ValidationError, e: 
    668679            raise ValidationError(context, token=match.group(), 
     
    674685        return Node.selected(self, context, match) 
    675686 
    676     def parse(self, match): 
     687    def parse(self, context, match): 
    677688        """Parse the match and return a value. Value can be of any type: tuple, 
    678689        list, object, etc. 
     
    682693 
    683694        >>> v = Variable('Test') 
    684         >>> v.parse(re.match(r'\w+', 'test')) 
     695        >>> v.parse(None, re.match(r'\w+', 'test')) 
    685696        'test' 
    686697        """ 
     
    985996 
    986997 
    987 def quickstart(*grammar_or_callables, **interact_args): 
     998def quickstart(grammar_or_callables, *interact_args, **interact_kwargs): 
    988999    """Start an interactive session from a grammar, or from inspecting a set of 
    9891000    callables.""" 
    9901001    from cly.extra import quickstart 
    991     quickstart(*grammar_or_callables, **interact_args) 
     1002    quickstart(grammar_or_callables, *interact_args, **interact_kwargs) 
    9921003 
    9931004 
  • cly/trunk/cly/types.py

    r425 r429  
    4545    pattern = r"""(\w+)|"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'""" 
    4646 
    47     def parse(self, match): 
     47    def parse(self, context, match): 
    4848        return match.group(match.lastindex).decode('string_escape') 
    4949 
     
    6565        self.allow_fragments = allow_fragments 
    6666 
    67     #def parse(self, match): 
     67    #def parse(self, context, match): 
    6868        #import urlparse 
    6969        #return urlparse.urlparse(match.string[match.start():match.end()], self.scheme, self.allow_fragments) 
     
    9393    pattern = r'\d+' 
    9494 
    95     def parse(self, match): 
     95    def parse(self, context, match): 
    9696        return int(match.group()) 
    9797 
     
    107107    1234500000000.0 
    108108    """ 
    109     pattern = r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?' 
    110  
    111     def parse(self, match): 
     109    pattern = r'[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?' 
     110 
     111    def parse(self, context, match): 
    112112        return float(match.group()) 
    113113 
     
    125125    pattern = r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' 
    126126 
    127     def parse(self, match): 
     127    def parse(self, context, match): 
    128128        return tuple(map(int, match.groups())) 
    129129 
     
    142142    pattern = r'(?i)([A-Z0-9][A-Z0-9_-]*)(?:\.([A-Z0-9][A-Z0-9_-]*))*' 
    143143 
    144     def parse(self, match): 
     144    def parse(self, context, match): 
    145145        return tuple(match.group().split('.')) 
    146146 
     
    161161    pattern = r'(?i)(%s)|(%s)' % (IP.pattern, Hostname.pattern) 
    162162 
    163     def parse(self, match): 
     163    def parse(self, context, match): 
    164164        components = match.string[match.start():match.end()].split('.') 
    165165        if match.lastindex == 1: