Changes between Version 19 and Version 20 of PyYAMLDocumentation
- Timestamp:
- 05/07/06 05:08:48 (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PyYAMLDocumentation
v19 v20 660 660 '''Warning: API stability is not guaranteed!''' 661 661 662 === The yaml package === 663 664 {{{ 665 #!python 666 scan(stream, Loader=Loader) 667 }}} 668 669 '''`scan(stream)`''' scans the given `stream` and produces a sequence of tokens. 670 671 {{{ 672 #!python 673 parse(stream, Loader=Loader) 674 675 emit(events, stream=None, Dumper=Dumper, 676 canonical=None, 677 indent=None, 678 width=None, 679 allow_unicode=None, 680 line_break=None) 681 }}} 682 683 '''`parse(stream)`''' parses the given `stream` and produces a sequence of parsing events. 684 685 '''`emit(events, stream=None)`''' serializes the given sequence of parsing `events` and 686 write them to the `stream`. if `stream` is `None`, it returns the produced stream. 687 688 {{{ 689 #!python 690 compose(stream, Loader=Loader) 691 compose_all(stream, Loader=Loader) 692 693 serialize(node, stream=None, Dumper=Dumper, 694 encoding='utf-8', 695 explicit_start=None, 696 explicit_end=None, 697 version=None, 698 tags=None, 699 canonical=None, 700 indent=None, 701 width=None, 702 allow_unicode=None, 703 line_break=None) 704 serialize_all(nodes, stream=None, Dumper=Dumper, ...) 705 }}} 706 707 '''`compose(stream)`''' parses the given `stream` and returns the root of the representation graph 708 for the first document in the stream. If there are no documents in the stream, it returns `None`. 709 710 '''`compose_all(stream)`''' parses the given `stream` and returns a sequence of representation graphs 711 corresponding to the documents in the stream. 712 713 '''`serialize(node, stream=None)`''' serializes the given representation graph into the `stream`. 714 If `stream` is `None`, it returns the produced stream. 715 716 '''`serialize_all(node, stream=None)`''' serializes the given sequence of representation graphs 717 into the given `stream`. If `stream` is `None`, it returns the produced stream. 718 719 {{{ 720 #!python 721 load(stream, Loader=Loader) 722 load_all(stream, Loader=Loader) 723 724 safe_load(stream) 725 safe_load_all(stream) 726 727 dump(data, stream=None, Dumper=Dumper, 728 default_style=None, 729 default_flow_style=None, 730 encoding='utf-8', 731 explicit_start=None, 732 explicit_end=None, 733 version=None, 734 tags=None, 735 canonical=None, 736 indent=None, 737 width=None, 738 allow_unicode=None, 739 line_break=None) 740 dump_all(data, stream=None, Dumper=Dumper, ...) 741 742 safe_dump(data, stream=None, ...) 743 safe_dump_all(data, stream=None, ...) 744 }}} 745 746 '''`load(stream)`''' parses the given `stream` and returns a Python object constructed from 747 for the first document in the stream. If there are no documents in the stream, it returns `None`. 748 749 '''`load_all(stream)`''' parses the given `stream` and returns a sequence of Python objects 750 corresponding to the documents in the stream. 751 752 '''`safe_load(stream)`''' parses the given `stream` and returns a Python object constructed from 753 for the first document in the stream. If there are no documents in the stream, it returns `None`. 754 `safe_load` recognizes only standard YAML tags and cannot construct an arbitrary Python object. 755 756 '''`safe_load_all(stream)`''' parses the given `stream` and returns a sequence of Python objects 757 corresponding to the documents in the stream. `safe_load_all` recognizes only standard YAML tags 758 and cannot construct an arbitrary Python object. 759 760 '''`dump(data, stream=None)`''' serializes the given Python object into the `stream`. 761 If `stream` is `None`, it returns the produced stream. 762 763 '''`dump_all(data, stream=None)`''' serializes the given sequence of Python objects 764 into the given `stream`. If `stream` is `None`, it returns the produced stream. 765 Each object is represented as a YAML document. 766 767 '''`safe_dump(data, stream=None)`''' serializes the given Python object into the `stream`. 768 If `stream` is `None`, it returns the produced stream. `safe_dump` produces only standard YAML 769 tags and cannot represent an arbitrary Python object. 770 771 '''`safe_dump_all(data, stream=None)`''' serializes the given sequence of Python objects 772 into the given `stream`. If `stream` is `None`, it returns the produced stream. 773 Each object is represented as a YAML document. `safe_dump_all` produces only standard YAML 774 tags and cannot represent an arbitrary Python object. 775 776 {{{ 777 #!python 778 def constructor(loader, node): 779 # ... 780 return data 781 782 def multi_constructor(loader, tag_suffix, node): 783 # ... 784 return data 785 786 add_constructor(tag, constructor, Loader=Loader) 787 add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader) 788 }}} 789 790 '''`add_constructor(tag, constructor)`''' allows to specify a `constructor` for the given `tag`. 791 A constructor is a function that converts a node of a YAML representation graph to a native Python object. 792 A constructor accepts an instance of `Loader` and a node and returns a Python object. 793 794 '''`add_multi_constructor(tag_prefix, multi_constructor)`''' allows to specify a `multi_constructor` 795 for the given `tag_prefix`. A multi-constructor is a function that converts a node of a YAML 796 representation graph to a native Python object. A multi-constructor accepts an instance of `Loader`, 797 the suffix of the node tag, and a node and returns a Python object. 798 799 {{{ 800 #!python 801 def representer(dumper, data): 802 # ... 803 return node 804 805 def multi_representer(dumper, data): 806 # ... 807 return node 808 809 add_representer(data_type, representer, Dumper=Dumper) 810 add_multi_representer(base_data_type, multi_representer, Dumper=Dumper) 811 }}} 812 813 '''`add_representer(data_type, representer)`''' allows to specify a `representer` for Python objects 814 of the given `data_type`. A representer is a function that converts a native Python object to a node 815 of a YAML representation graph. A representer accepts an instance of `Dumper` and an object and returns a node. 816 817 '''`add_multi_representer(base_data_type, multi_representer)`''' allows to specify a `multi_representer` 818 for Python objects of the given `base_data_type` or any of its subclasses. A multi-representer is 819 a function that converts a native Python object to a node of a YAML representation graph. 820 A multi-representer accepts an instance of `Dumper` and an object and returns a node. 821 822 {{{ 823 #!python 824 add_implicit_resolver(tag, regexp, first, Loader=Loader, Dumper=Dumper) 825 add_path_resolver(tag, path, kind, Loader=Loader, Dumper=Dumper) 826 }}} 827 828 '''`add_implicit_resolver(tag, regexp, first)`''' adds an implicit tag resolver for plain scalars. 829 If the scalar value is matched the given `regexp`, it is assigned the `tag`. `first` is a 830 list of possible initial characters or `None`. 831 832 '''`add_path_resolver(tag, path, kind)`''' adds a path-based implicit tag resolver. 833 A `path` is a list of keys that form a path to a node in the representation graph. 834 Paths elements can be string values, integers, or `None`. The `kind` of a node can 835 be `str`, `list`, `dict`, or `None`. 662 836 663 837 === YAMLError === … … 764 938 BlockMappingStartToken() 765 939 940 766 941 KeyToken() 767 942 ScalarToken(plain=True, style=None, value=u'KeyToken') … … 961 1136 }}} 962 1137 963 '''`BaseLoader`''', '''`SafeLoader`''', and '''`Loader`''' provide the following interfaces:964 ''Scanner'', ''Parser'', ''Composer'', ''Constructor'', ''Resolver''.965 966 1138 '''`Loader(stream)`''' is the most common of the above classes and should be used in most cases. 967 1139 `stream` is an input YAML stream. It can be a string, a Unicode string, an open file, an open Unicode file. … … 978 1150 lists, dictionaries and Unicode strings. 979 1151 980 981 ==== Scanner interface ==== 982 983 {{{ 984 #!python 985 scan(stream, Loader=Loader) 986 1152 {{{ 1153 #!python 987 1154 Loader.check_token(*TokenClasses) 988 1155 Loader.peek_token() … … 990 1157 }}} 991 1158 992 '''`scan(stream)`''' scans the given `stream` and produces a sequence of tokens.993 994 1159 '''`Loader.check_token(*TokenClasses)`''' returns `True` if the next token in the stream 995 1160 is an instance of one of the given `TokenClasses`. Otherwise it returns `False`. … … 1001 1166 it from the internal token queue. The function returns `None` at the end of the stream. 1002 1167 1003 1004 ==== Parser interface ==== 1005 1006 {{{ 1007 #!python 1008 parse(stream, Loader=Loader) 1009 1168 {{{ 1169 #!python 1010 1170 Loader.check_event(*EventClasses) 1011 1171 Loader.peek_event() … … 1013 1173 }}} 1014 1174 1015 '''`parse(stream)`''' parses the given `stream` and produces a sequence of parsing events.1016 1017 1175 '''`Loader.check_event(*EventClasses)`''' returns `True` if the next event in the stream 1018 1176 is an instance of one of the given `EventClasses`. Otherwise it returns `False`. … … 1024 1182 it from the internal event queue. The function returns `None` at the end of the stream. 1025 1183 1026 1027 ==== Composer interface ==== 1028 1029 {{{ 1030 #!python 1031 compose(stream, Loader=Loader) 1032 compose_all(stream, Loader=Loader) 1033 1184 {{{ 1185 #!python 1034 1186 Loader.check_node() 1035 1187 Loader.get_node() 1036 1188 }}} 1037 1189 1038 '''`compose(stream)`''' parses the given `stream` and returns the root of the representation graph1039 for the first document in the stream. If there are no documents in the stream, it returns `None`.1040 1041 '''`compose_all(stream)`''' parses the given `stream` and returns a sequence of representation graphs1042 corresponding to the documents in the stream.1043 1044 1190 '''`Loader.check_node()`''' returns `True` is there are more documents available in the stream. Otherwise 1045 1191 it returns `False`. … … 1048 1194 returns its root node. 1049 1195 1050 1051 ==== Constructor interface ==== 1052 1053 {{{ 1054 #!python 1055 load(stream, Loader=Loader) 1056 load_all(stream, Loader=Loader) 1057 1058 safe_load(stream) 1059 safe_load_all(stream) 1060 1196 {{{ 1197 #!python 1061 1198 Loader.check_data() 1062 1199 Loader.get_data() 1063 1064 def constructor(loader, node):1065 # ...1066 return data1067 1068 def multi_constructor(loader, tag_suffix, node):1069 # ...1070 return data1071 1072 add_constructor(tag, constructor, Loader=Loader)1073 add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader)1074 1200 1075 1201 Loader.add_constructor(tag, constructor) # Loader.add_constructor is a class method. … … 1081 1207 }}} 1082 1208 1083 '''`load(stream)`''' parses the given `stream` and returns a Python object constructed from1084 for the first document in the stream. If there are no documents in the stream, it returns `None`.1085 1086 '''`load_all(stream)`''' parses the given `stream` and returns a sequence of Python objects1087 corresponding to the documents in the stream.1088 1089 '''`safe_load(stream)`''' parses the given `stream` and returns a Python object constructed from1090 for the first document in the stream. If there are no documents in the stream, it returns `None`.1091 `safe_load` recognizes only standard YAML tags and cannot construct an arbitrary Python object.1092 1093 '''`safe_load_all(stream)`''' parses the given `stream` and returns a sequence of Python objects1094 corresponding to the documents in the stream. `safe_load_all` recognizes only standard YAML tags1095 and cannot construct an arbitrary Python object.1096 1097 1209 '''`Loader.check_data()`''' returns `True` is there are more documents available in the stream. Otherwise 1098 1210 it returns `False`. … … 1101 1213 in the stream. 1102 1214 1103 '''`add_constructor(tag, constructor)`''' and '''`Loader.add_constructor(tag, constructor)`''' allow 1104 to specify a `constructor` for the given `tag`. A constructor is a function that converts a node 1105 of a YAML representation graph to a native Python object. A constructor accepts an instance of `Loader` 1106 and a node and returns a Python object. 1107 1108 '''`add_multi_constructor(tag_prefix, multi_constructor)`''' and '''`Loader.add_multi_constructor(tag_prefix, multi_constructor)`''' 1109 allow to specify a `multi_constructor` for the given `tag_prefix`. A multi-constructor is a function that converts a node 1110 of a YAML representation graph to a native Python object. A multi-constructor accepts an instance of `Loader`, 1111 the suffix of the node tag, and a node and returns a Python object. 1215 '''`Loader.add_constructor(tag, constructor)`''': see `add_constructor`. 1216 1217 '''`Loader.add_multi_constructor(tag_prefix, multi_constructor)`''': see `add_multi_constructor`. 1112 1218 1113 1219 '''`Loader.construct_scalar(node)`''' checks that the given `node` is a scalar and returns its value. … … 1120 1226 of Python objects corresponding to the node keys and values. This function is intended to be used in constructors. 1121 1227 1122 1123 ==== Resolver interface ==== 1124 1125 The ''Resolver'' interfaces of '''`Loader`''' and '''`Dumper`''' are identical. 1126 1127 {{{ 1128 #!python 1129 add_implicit_resolver(tag, regexp, first, Loader=Loader, Dumper=Dumper) 1130 add_path_resolver(tag, path, kind, Loader=Loader, Dumper=Dumper) 1131 1228 {{{ 1229 #!python 1132 1230 Loader.add_implicit_resolver(tag, regexp, first) # Loader.add_implicit_resolver is a class method. 1133 1231 Loader.add_path_resolver(tag, path, kind) # Loader.add_path_resolver is a class method. 1134 1135 Dumper.add_implicit_resolver(tag, regexp, first) # Dumper.add_implicit_resolver is a class method. 1136 Dumper.add_path_resolver(tag, path, kind) # Dumper.add_path_resolver is a class method. 1137 }}} 1138 1139 '''`add_implicit_resolver(tag, regexp, first)`''' adds an implicit tag resolver for plain scalars. 1140 If the scalar value is matched the given `regexp`, it is assigned the `tag`. `first` is a 1141 list of possible initial characters or `None`. 1142 1143 '''`add_path_resolver(tag, path, kind)`''' adds a path-based implicit tag resolver. 1144 A `path` is a list of keys that form a path to a node in the representation graph. 1145 Paths elements can be string values, integers, or `None`. The `kind` of a node can 1146 be `str`, `list`, `dict`, or `None`. 1232 }}} 1233 1234 '''`Loader.add_implicit_resolver(tag, regexp, first)`''': see `add_implicit_resolver`. 1235 1236 '''`Loader.add_path_resolver(tag, path, kind)`''': see `add_path_resolver`. 1147 1237 1148 1238 … … 1168 1258 }}} 1169 1259 1170 '''`BaseDumper`''', '''`SafeDumper`''', and '''`Dumper`''' provide the following interfaces:1171 ''Emitter'', ''Serializer'', ''Representer'', ''Resolver''.1172 1173 1260 '''`Dumper(stream)`''' is the most common of the above classes and should be used in most cases. 1174 1261 `stream` is an output YAML stream. It can be an open file or an open Unicode file. … … 1184 1271 '''`BaseDumper(stream)`''' does not support any tags and is useful only for subclassing. 1185 1272 1186 1187 ==== Emitter interface ==== 1188 1189 {{{ 1190 #!python 1191 emit(events, stream=None, Dumper=Dumper, 1192 canonical=None, 1193 indent=None, 1194 width=None, 1195 allow_unicode=None, 1196 line_break=None) 1197 1198 Emitter.emit(event) 1199 }}} 1200 1201 '''`emit(events, stream=None)`''' serializes the given sequence of parsing `events` and 1202 write them to the `stream`. if `stream` is `None`, it returns the produced stream. 1203 1204 '''`Emitter.emit(event)`''' serializes the given `event` and write it to the output stream. 1205 1206 1207 ==== Serializer interface ==== 1208 1209 {{{ 1210 #!python 1211 serialize(node, stream=None, Dumper=Dumper, 1212 encoding='utf-8', 1213 explicit_start=None, 1214 explicit_end=None, 1215 version=None, 1216 tags=None, 1217 canonical=None, 1218 indent=None, 1219 width=None, 1220 allow_unicode=None, 1221 line_break=None) 1222 serialize_all(nodes, stream=None, Dumper=Dumper, ...) 1223 1273 {{{ 1274 #!python 1275 Dumper.emit(event) 1276 }}} 1277 1278 '''`Dumper.emit(event)`''' serializes the given `event` and write it to the output stream. 1279 1280 {{{ 1281 #!python 1224 1282 Dumper.open() 1225 1283 Dumper.serialize(node) … … 1227 1285 }}} 1228 1286 1229 '''`serialize(node, stream=None)`''' serializes the given representation graph into the `stream`.1230 If `stream` is `None`, it returns the produced stream.1231 1232 1287 '''`Dumper.open()`''' emits `StreamStartEvent`. 1233 1288 … … 1236 1291 '''`Dumper.close()`''' emits `StreamEndEvent`. 1237 1292 1238 1239 ==== Representer interface ==== 1240 1241 {{{ 1242 #!python 1243 dump(data, stream=None, Dumper=Dumper, 1244 default_style=None, 1245 default_flow_style=None, 1246 encoding='utf-8', 1247 explicit_start=None, 1248 explicit_end=None, 1249 version=None, 1250 tags=None, 1251 canonical=None, 1252 indent=None, 1253 width=None, 1254 allow_unicode=None, 1255 line_break=None) 1256 dump_all(data, stream=None, Dumper=Dumper, ...) 1257 1258 safe_dump(data, stream=None, ...) 1259 safe_dump_all(data, stream=None, ...) 1260 1293 {{{ 1294 #!python 1261 1295 Dumper.represent(data) 1262 1263 def representer(dumper, data):1264 # ...1265 return node1266 1267 def multi_representer(dumper, data):1268 # ...1269 return node1270 1271 add_representer(data_type, representer, Dumper=Dumper)1272 add_multi_representer(base_data_type, multi_representer, Dumper=Dumper)1273 1296 1274 1297 Dumper.add_representer(data_type, representer) # Dumper.add_representer is a class method. … … 1280 1303 }}} 1281 1304 1282 '''`dump(data, stream=None)`''' serializes the given Python object into the `stream`.1283 If `stream` is `None`, it returns the produced stream.1284 1285 '''`dump_all(data, stream=None)`''' serializes the given sequence of Python objects1286 into the given `stream`. If `stream` is `None`, it returns the produced stream.1287 Each object is represented as a YAML document.1288 1289 '''`safe_dump(data, stream=None)`''' serializes the given Python object into the `stream`.1290 If `stream` is `None`, it returns the produced stream. `safe_dump` produces only standard YAML1291 tags and cannot represent an arbitrary Python object.1292 1293 '''`safe_dump_all(data, stream=None)`''' serializes the given sequence of Python objects1294 into the given `stream`. If `stream` is `None`, it returns the produced stream.1295 Each object is represented as a YAML document. `safe_dump_all` produces only standard YAML1296 tags and cannot represent an arbitrary Python object.1297 1298 1305 '''`Dumper.represent(data)`''' serializes the given Python object to the output YAML stream. 1299 1306 1300 '''`add_representer(data_type, representer)`''' and '''`Dumper.add_representer(data_type, representer)`''' 1301 allow to specify a `representer` for Python objects of the given `data_type`. A representer is a function 1302 that converts a native Python object to a node of a YAML representation graph. A representer accepts 1303 an instance of `Dumper` and an object and returns a node. 1304 1305 '''`add_multi_representer(base_data_type, multi_representer)`''' and '''`Dumper.add_multi_representer(base_data_type, multi_representer)`''' 1306 allow to specify a `multi_representer` for Python objects of the given `base_data_type` or any of its subclasses. 1307 A multi-representer is a function that converts a native Python object to a node of a YAML representation graph. 1308 A multi-representer accepts an instance of `Dumper` and an object and returns a node. 1307 '''`Dumper.add_representer(data_type, representer)`''': see `add_representer`. 1308 1309 '''`Dumper.add_multi_representer(base_data_type, multi_representer)`''': see `add_multi_representer`. 1309 1310 1310 1311 '''`Dumper.represent_scalar(tag, value, style=None)`''' returns a scalar node with the given `tag`, `value`, and `style`. … … 1317 1318 and subnodes generated from the keys and values of the given `mapping`. 1318 1319 1319 1320 ==== Resolver interface ==== 1321 1322 The ''Resolver'' interfaces of '''`Loader`''' and '''`Dumper`''' are identical. Check the corresponding section 1323 of the '''`Loader`''' reference. 1320 {{{ 1321 #!python 1322 Dumper.add_implicit_resolver(tag, regexp, first) # Dumper.add_implicit_resolver is a class method. 1323 Dumper.add_path_resolver(tag, path, kind) # Dumper.add_path_resolver is a class method. 1324 }}} 1325 1326 '''`Dumper.add_implicit_resolver(tag, regexp, first)`''': see `add_implicit_resolver`. 1327 1328 '''`Dumper.add_path_resolver(tag, path, kind)`''': see `add_path_resolver`. 1324 1329 1325 1330 1326 1331 === YAMLObject === 1327 1332 1328 1329 === The yaml package ===1330 1333 1331 1334
