how to determine an 'open' string?

Harvey Thomas hst at empolis.co.uk
Thu May 16 11:59:00 EDT 2002


Holger wrote
> 
> 
> hello, 
> 
> with my replacement rlcompleter module i'd like to
> have a *correct* check if a string is 'open'.
> examples:
> 
>     asd"""askdjalsdk      # open 
>     aksdjasd              # closed
>     asjdkk"kajsd''''      # open
>     "'asdasd"             # closed
>     """dontcountoneven"   # open
> 
> so i need a function which takes these strings as
> an argument and return 1 for 'open', 0 for a 'closed' string.
> 
> Any working ideas?
> 
>     holger
> 
I think this is OK

import re

rex = re.compile('"""|\'\'\'|"|\'')

def quotecompleted(str):
    global rex
    f = rex.findall(str)
    lf = len(f)
    if lf < 2:  #the trivial cases
        return lf
    else:
        cmp = f[0]
        i = 1
        while i < lf:
            if cmp == f[i]:
                if i + 1 == lf:
                    return 0
                else:
                    cmp = f[i + 1]
                    i += 2
            else:
                i += 1
        return 1
        
HTH

Harvey

_____________________________________________________________________
This message has been checked for all known viruses by the MessageLabs Virus Scanning Service.





More information about the Python-list mailing list