Changeset 787

Show
Ignore:
Timestamp:
04/10/07 12:03:01 (2 years ago)
Author:
rij
Message:

Added code to make the real() and int() functions more robust.
Fixes Ticket #61 "PyRAF isn't as forgiving as the cl in implementing the
'real' function"

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/iraffunctions.py

    r782 r787  
    998998def real(x): 
    999999    """Return real/float representation of x""" 
    1000     if x == INDEF: return INDEF 
    1001     try: 
     1000    if x == INDEF: 
     1001        return INDEF 
     1002    elif isinstance(x, str): 
     1003        x = x.strip() 
     1004        if x.find(':') >= 0: 
     1005            #...handle the special a:b:c case here... 
     1006            if x[0] in ["-", "+"]: 
     1007                if x[0] == "-": 
     1008                    sign = -1. 
     1009                x = x[1:] 
     1010            else: 
     1011                sign = 1. 
     1012            m = _re.search(r"[^0-9:.]", x) 
     1013            if m: 
     1014                x = x[0:m.start()] 
     1015            f = map(float,x.split(":")) 
     1016            f = map(abs, f) 
     1017            return sign*clSexagesimal(*f) 
     1018        else: 
     1019            x = _re.sub("[EdD]", "e", x, count=1) 
     1020            m = _re.search(r"[^0-9.e+-]", x) 
     1021            if m: 
     1022                x = x[0:m.start()] 
     1023            return float(x) 
     1024    else: 
    10021025        return float(x) 
    1003     except ValueError: 
    1004         #...handle the special a:b:c case here... 
    1005         f = map(float,x.split(":")) 
    1006         if x.find('-') >= 0: 
    1007             sign = -1 
    1008             f = map(abs, f) 
    1009         else: 
    1010             sign = 1 
    1011         return sign*clSexagesimal(*f) 
    10121026 
    10131027def integer(x): 
     
    10151029    if x==INDEF: 
    10161030        return INDEF 
     1031    elif isinstance(x, str): 
     1032        x = x.strip() 
     1033        i = 0 
     1034        j = len(x) 
     1035        if x[0] in ["+", "-"]: 
     1036            i = 1 
     1037        x = _re.sub("[EdD]", "e", x, count=1) 
     1038        m = _re.search(r"[^0-9.e+-]", x[i:]) 
     1039        if m: 
     1040            j = i + m.start() 
     1041        return int(float(x[:j])) 
    10171042    else: 
    10181043        return int(x)