示例#1
0
文件: type.py 项目: jordancode/phil
 def coerce_type(cls, value, dest_type):
     
     if value is not None:
         
         if cls.is_array_type(dest_type):
             base_type = cls.get_base_type(dest_type)
             return [ cls.coerce_type(v, base_type) for v in value ]
         elif cls.is_dict_type(dest_type):
             base_type = cls.get_base_type(dest_type)
             return { k : cls.coerce_type(v, base_type) for k,v in value.items() }
         
         
         if dest_type == cls.INT:
             return int(value)
         elif dest_type == cls.FLOAT:
             return float(value)
         elif dest_type == cls.STRING:
             if isinstance(value, bytes):
                 value = value.decode("utf-8")
             return str(value)
         elif dest_type == cls.ASCII:
             if isinstance(value, str):
                 value = value.encode("ascii","ignore")
             return value.decode("ascii","ignore")
             
         elif dest_type == cls.BOOL:
             return value in cls.TRUTHY_VALUES
         elif dest_type == cls.UNIXTIME:
             if isinstance(value, datetime.datetime):
                 return value
             
             #cast to float from string then to int to remove decimal
             try: 
                 ts = int(float(value))
                 if ts > 0:
                     return DateUtils.unix_to_datetime(ts)
             except (ValueError, TypeError):
                 pass
             try:
                 return DateUtils.mysql_to_datetime(value)
             except (ValueError, TypeError):
                 pass
             
             return None
         elif dest_type == cls.JSON:
             return json.loads(value)
     
     #unknown type
     return value
示例#2
0
文件: type.py 项目: jordancode/phil
 def coerce_type(cls, value, dest_type):
     
     if value is not None:
         
         if cls.is_array_type(dest_type):
             base_type = cls.get_base_type(dest_type)
             return [ cls.coerce_type(v, base_type) for v in value ]
         elif cls.is_dict_type(dest_type):
             base_type = cls.get_base_type(dest_type)
             return { k : cls.coerce_type(v, base_type) for k,v in value.items() }
         
         
         if dest_type == cls.INT:
             return int(value)
         elif dest_type == cls.FLOAT:
             return float(value)
         elif dest_type == cls.STRING:
             if isinstance(value, bytes):
                 value = value.decode("utf-8")
             return str(value)
         elif dest_type == cls.BOOL:
             return value in cls.TRUTHY_VALUES
         elif dest_type == cls.UNIXTIME:
             if isinstance(value, datetime.datetime):
                 return value
             
             #cast to float from string then to int to remove decimal
             try: 
                 ts = int(float(value))
                 if ts > 0:
                     return DateUtils.unix_to_datetime(ts)
             except (ValueError, TypeError):
                 pass
             try:
                 return DateUtils.mysql_to_datetime(value)
             except (ValueError, TypeError):
                 pass
             
             return None
         elif dest_type == cls.JSON:
             return json.loads(value)
     
     #unknown type
     return value
    def _coerce_type(self, value, type):
        truthy_values = ["true", "True", "1", 1, True]

        if value is not None:
            if type == "int":
                return int(value)
            elif type == "float":
                return float(value)
            elif type == "string":
                return str(value)
            elif type == "bool":
                return value in truthy_values
            elif type == "unixtime":
                #cast to float from string then to int to remove decimal
                return DateUtils.unix_to_datetime(int(float(value)))
            elif type == "json" or type == "dict":
                if isinstance(value, list) or isinstance(value, dict):
                    return value  #return already parsed JSON

                #else parse it
                return json.loads(value)

        #unknown type
        return value
 def _coerce_type(self, value, type):
     truthy_values = ["true", "True", "1", 1, True]
     
     if value is not None:
         if type == "int":
             return int(value)
         elif type == "float":
             return float(value)
         elif type == "string":
             return str(value)
         elif type == "bool":
             return value in truthy_values
         elif type == "unixtime":
             #cast to float from string then to int to remove decimal 
             return DateUtils.unix_to_datetime(int(float(value)))
         elif type == "json":
             if isinstance(value,list) or isinstance(value, dict):
                 return value #return already parsed JSON
             
             #else parse it
             return json.loads(value)
     
     #unknown type
     return value
示例#5
0
 def get_date(self):
     return DateUtils.unix_to_datetime(self._ts)
示例#6
0
 def get_date(self):
     return DateUtils.unix_to_datetime(self._ts)