On Wed, Apr 1, 2009 at 3:30 AM, zieglerk <[email protected]> wrote:
>
> Hi,
>
>>
>> Even better, of course, is [a..b] and (a...b).
>>
>
> this seems quite intuitive and handy, but I haven't found the
> documentation for both anywhere.  Neither among the topic "lists" nor
> when searching for "intervals".
>
> Can you give me a hint.

This is a Sage-specific feature that isn't in Python.  It was proposed
in a Python PEP but that didn't get into Python for some reason.
Below is the very minimal docstring and the actual source code.   I
recommend you just try it out.  In short [a..b] makes a list from a to
b,
[a,a+eps,..,b] makes a list in steps of eps, and (a..b),
(a,a+eps,..,b) are similar, but give a generator (lazy list) instead
of a list.


def parse_ellipsis(code, preparse_step=True):
    """
    Preparse [0,2,..,n] notation.

    EXAMPLES:
        sage: from sage.misc.preparser import parse_ellipsis
        sage: parse_ellipsis("[1,2,..,n]")
        '(ellipsis_range(1,2,Ellipsis,n))'
        sage: parse_ellipsis("for i in (f(x) .. L[10]):")
        'for i in (ellipsis_iter(f(x) ,Ellipsis, L[10])):'
    """
    ix = code.find('..')
    while ix != -1:
        if ix == 0:
            raise SyntaxError, "Cannot start line with ellipsis."
        elif code[ix-1]=='.':
            # '...' be valid Python in index slices
            code = code[:ix-1] + "Ellipsis" + code[ix+2:]
        elif len(code) >= ix+3 and code[ix+2]=='.':
            # '...' be valid Python in index slices
            code = code[:ix] + "Ellipsis" + code[ix+3:]
        else:
            start_list, end_list = containing_block(code, ix, ['()','[]'])
            arguments = code[start_list+1:end_list-1].replace('...',
',Ellipsis,').replace('..', ',Ellipsis,')
            arguments = re.sub(r',\s*,', ',', arguments)
            if preparse_step:
                arguments = arguments.replace(';', ', step=')
            range_or_iter = 'range' if code[start_list]=='[' else 'iter'
            code = "%s(ellipsis_%s(%s))%s" %  (code[:start_list],
                                               range_or_iter,
                                               arguments,
                                               code[end_list:])
        ix = code.find('..')
    return code

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to