예제 #1
0
파일: __init__.py 프로젝트: plazar/TOASTER
def fancy_getitem(self, key):
    filterfunc = null
    if (type(key) in (type('str'), type(u'str'))) and key.endswith("_L"):
        filterfunc = string.lower
        key = key[:-2]
    elif (type(key) in (type('str'), type(u'str'))) and key.endswith("_U"):
        filterfunc = string.upper
        key = key[:-2]
    elif (type(key) in (type('str'), type(u'str'))) and toround_re.search(key):
        head, sep, tail = key.rpartition('_R')
        digits = int(tail) if tail else 0
        filterfunc = lambda x: round(x, digits)
        key = head
    elif (type(key) in (type('str'), type(u'str'))) and key.startswith("date:"):
        fmt = key[5:]
        key = 'mjd'
        filterfunc = lambda mjd: utils.mjd_to_datetime(mjd).strftime(fmt)
    if key in self:
        return filterfunc(super(self.__class__, self).__getitem__(key))
    else:
        matches = [k for k in self.keys() if k.startswith(key)]
        if len(matches) == 1:
            return filterfunc(super(self.__class__, self).__getitem__(matches[0]))
        elif len(matches) > 1:
            raise errors.BadColumnNameError("The column abbreviation "
                                            "'%s' is ambiguous. "
                                            "('%s' all match)" %
                                            (key, "', '".join(matches)))
        else:
            raise errors.BadColumnNameError("The column '%s' doesn't exist! "
                                            "(Valid column names: '%s')" %
                                            (key, "', '".join(sorted(self.keys()))))
예제 #2
0
파일: datafile.py 프로젝트: plazar/TOASTER
 def __getitem__(self, key):
     if (type(key) in (type('str'), type(u'str'))) and key.endswith("_L"):
         filterfunc = str.lower
         key = key[:-2]
     elif (type(key) in (type('str'), type(u'str'))) and key.endswith("_U"):
         filterfunc = str.upper
         key = key[:-2]
     elif (type(key) in (type('str'), type(u'str'))) and toround_re.search(key):
         head, sep, tail = key.rpartition('_R')
         digits = int(tail) if tail else 0
         filterfunc = lambda x: round(x, digits)
         key = head
     elif (type(key) in (type('str'), type(u'str'))) and key.startswith("date:"):
         fmt = key[5:]
         key = 'mjd'
         filterfunc = lambda mjd: utils.mjd_to_datetime(mjd).strftime(fmt)
     else:
         filterfunc = null
     if self.has_key(key):
         val = self.get_value(key)
         return filterfunc(val)
     else:
         matches = [k for k in self.keys() if k.startswith(key)]
         if len(matches) == 1:
             val = self.get_value(matches[0])
             if type(val) in (type('str'), type(u'str')):
                 return filterfunc(val)
             else:
                 return val
         elif len(matches) > 1:
             raise errors.UnrecognizedValueError("The header parameter "
                                                 "abbreviation '%s' is ambiguous. ('%s' "
                                                 "all match)" %
                                                 (key, "', '".join(matches)))
         else:
             val = self.get_value(key)
             if type(val) in (type('str'), type(u'str')):
                 return filterfunc(val)
             else:
                 return val