| | 59 | for object_type in type(object).__mro__: |
| | 60 | if object_type.__module__ == '__builtin__': |
| | 61 | name = object_type.__name__ |
| | 62 | else: |
| | 63 | name = '%s.%s' % (object_type.__module__, object_type.__name__) |
| | 64 | method = 'represent_%s' % name.replace('.', '_') |
| | 65 | if hasattr(self, method): |
| | 66 | return getattr(self, method)(object) |
| | 68 | |
| | 69 | def represent_object(self, object): |
| | 70 | return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:str") |
| | 71 | |
| | 72 | def represent_NoneType(self, object): |
| | 73 | return _syck.Scalar('~', tag="tag:yaml.org,2002:null") |
| | 74 | |
| | 75 | def represent_bool(self, object): |
| | 76 | return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:bool") |
| | 77 | |
| | 78 | def represent_str(self, object): |
| | 79 | return _syck.Scalar(str(object), tag="tag:yaml.org,2002:str") |
| | 80 | |
| | 81 | def represent_list(self, object): |
| | 82 | return _syck.Seq(object[:], tag="tag:yaml.org,2002:seq") |
| | 83 | |
| | 84 | def represent_dict(self, object): |
| | 85 | return _syck.Map(object.copy(), tag="tag:yaml.org,2002:map") |
| | 86 | |
| | 87 | def represent_int(self, object): |
| | 88 | return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") |
| | 89 | |
| | 90 | def represent_int(self, object): |
| | 91 | return _syck.Scalar(repr(object), tag="tag:yaml.org,2002:int") |
| | 92 | |
| | 93 | def represent_float(self, object): |
| | 94 | value = repr(object) |
| | 95 | if value == repr(INF): |
| | 96 | value = '.inf' |
| | 97 | elif value == repr(NEGINF): |
| | 98 | value = '-.inf' |
| | 99 | elif value == repr(NAN): |
| | 100 | value = '.nan' |
| | 101 | return _syck.Scalar(value, tag="tag:yaml.org,2002:float") |
| | 102 | |
| | 103 | def represent_sets_Set(self, object): |
| | 104 | return _syck.Seq(list(object), tag="tag:yaml.org,2002:set") |
| | 105 | |
| | 106 | def represent_datetime_datetime(self, object): |
| | 107 | return _syck.Scalar(object.isoformat(), tag="tag:yaml.org,2002:timestamp") |
| | 108 | |
| | 109 | def allow_aliases(self, object): |
| | 110 | if object is None or type(object) in [int, bool, float]: |
| | 111 | return False |
| | 112 | if type(object) is str and (not object or object.isalnum()): |
| | 113 | return False |
| | 114 | return True |