| | 232 | A good introduction to the YAML syntax is |
| | 233 | [http://yaml.org/spec/cvs/current.html#id857168 Chapter 2 of the YAML specification]. |
| | 234 | |
| | 235 | You may also check [http://yaml4r.sourceforge.net/cookbook/ the YAML cookbook]. Note |
| | 236 | that it is focused on a Ruby implementation and uses the old YAML 1.0 syntax. |
| | 237 | |
| | 238 | Here we present most common YAML constructs together with the corresponding Python objects. |
| | 239 | |
| | 240 | === Documents === |
| | 241 | |
| | 242 | YAML stream is a collection of zero or more documents. An empty stream contains no documents. |
| | 243 | Documents are separated with '''`---`'''. Documents may optionally end with '''`...`'''. |
| | 244 | A single document may or may not be marked with '''`---`'''. |
| | 245 | |
| | 246 | Example of an implicit document: |
| | 247 | {{{ |
| | 248 | - Multimedia |
| | 249 | - Internet |
| | 250 | - Education |
| | 251 | }}} |
| | 252 | |
| | 253 | Example of an explicit document: |
| | 254 | {{{ |
| | 255 | --- |
| | 256 | - Afterstep |
| | 257 | - CTWM |
| | 258 | - Oroborus |
| | 259 | ... |
| | 260 | }}} |
| | 261 | |
| | 262 | Example of several documents in the same stream: |
| | 263 | {{{ |
| | 264 | --- |
| | 265 | - Ada |
| | 266 | - APL |
| | 267 | - ASP |
| | 268 | - Assembly |
| | 269 | - Awk |
| | 270 | --- |
| | 271 | - Basic |
| | 272 | --- |
| | 273 | - C |
| | 274 | - C# # Note that comments are denoted with ' #' (space and #). |
| | 275 | - C++ |
| | 276 | - Cold Fusion |
| | 277 | }}} |
| | 278 | |
| | 279 | === Block sequences === |
| | 280 | |
| | 281 | In the block context, sequence entries are denoted by '''`- `''' (dash and space): |
| | 282 | {{{ |
| | 283 | # YAML |
| | 284 | - The Dagger 'Narthanc' |
| | 285 | - The Dagger 'Nimthanc' |
| | 286 | - The Dagger 'Dethanc' |
| | 287 | }}} |
| | 288 | {{{ |
| | 289 | #!python |
| | 290 | # Python |
| | 291 | ["The Dagger 'Narthanc'", "The Dagger 'Nimthanc'", "The Dagger 'Dethanc'"] |
| | 292 | }}} |
| | 293 | |
| | 294 | Block sequences can be nested: |
| | 295 | {{{ |
| | 296 | # YAML |
| | 297 | - |
| | 298 | - HTML |
| | 299 | - LaTeX |
| | 300 | - SGML |
| | 301 | - VRML |
| | 302 | - XML |
| | 303 | - YAML |
| | 304 | - |
| | 305 | - BSD |
| | 306 | - GNU Hurd |
| | 307 | - Linux |
| | 308 | }}} |
| | 309 | {{{ |
| | 310 | #!python |
| | 311 | # Python |
| | 312 | [['HTML', 'LaTeX', 'SGML', 'VRML', 'XML', 'YAML'], ['BSD', 'GNU Hurd', 'Linux']] |
| | 313 | }}} |
| | 314 | |
| | 315 | It's not necessary to start a nested sequence with a new line: |
| | 316 | {{{ |
| | 317 | # YAML |
| | 318 | - 1.1 |
| | 319 | - - 2.1 |
| | 320 | - 2.2 |
| | 321 | - - - 3.1 |
| | 322 | - 3.2 |
| | 323 | - 3.3 |
| | 324 | }}} |
| | 325 | {{{ |
| | 326 | #!python |
| | 327 | # Python |
| | 328 | [1.1, [2.1, 2.2], [[3.1, 3.2, 3.3]]] |
| | 329 | }}} |
| | 330 | |
| | 331 | A block sequence may be nested to a block mapping. Note that in this |
| | 332 | case it is not necessary to indent the sequence. |
| | 333 | {{{ |
| | 334 | # YAML |
| | 335 | left hand: |
| | 336 | - Ring of Teleportation |
| | 337 | - Ring of Speed |
| | 338 | right hand: |
| | 339 | - Ring of Resist Fire |
| | 340 | - Ring of Resist Cold |
| | 341 | - Ring of Resist Poison |
| | 342 | }}} |
| | 343 | {{{ |
| | 344 | #!python |
| | 345 | # Python |
| | 346 | {'right hand': ['Ring of Resist Fire', 'Ring of Resist Cold', 'Ring of Resist Poison'], |
| | 347 | 'left hand': ['Ring of Teleportation', 'Ring of Speed']} |
| | 348 | }}} |
| | 349 | |
| | 350 | === Block mappings === |
| | 351 | |
| | 352 | In the block context, keys and values of mappings are separated by '''`: `''' (colon and space): |
| | 353 | {{{ |
| | 354 | # YAML |
| | 355 | base armor class: 0 |
| | 356 | base damage: [4,4] |
| | 357 | plus to-hit: 12 |
| | 358 | plus to-dam: 16 |
| | 359 | plus to-ac: 0 |
| | 360 | }}} |
| | 361 | {{{ |
| | 362 | #!python |
| | 363 | # Python |
| | 364 | {'plus to-hit': 12, 'base damage': [4, 4], 'base armor class': 0, 'plus to-ac': 0, 'plus to-dam': 16} |
| | 365 | }}} |
| | 366 | |
| | 367 | Complex keys are denoted with '''`? `''' (question mark and space): |
| | 368 | {{{ |
| | 369 | # YAML |
| | 370 | ? !!python/tuple [0,0] |
| | 371 | : The Hero |
| | 372 | ? !!python/tuple [0,1] |
| | 373 | : Treasure |
| | 374 | ? !!python/tuple [1,0] |
| | 375 | : Treasure |
| | 376 | ? !!python/tuple [1,1] |
| | 377 | : The Dragon |
| | 378 | }}} |
| | 379 | {{{ |
| | 380 | #!python |
| | 381 | # Python |
| | 382 | {(0, 1): 'Treasure', (1, 0): 'Treasure', (0, 0): 'The Hero', (1, 1): 'The Dragon'} |
| | 383 | }}} |
| | 384 | |
| | 385 | Block mapping can be nested: |
| | 386 | {{{ |
| | 387 | # YAML |
| | 388 | hero: |
| | 389 | hp: 34 |
| | 390 | sp: 8 |
| | 391 | level: 4 |
| | 392 | orc: |
| | 393 | hp: 12 |
| | 394 | sp: 0 |
| | 395 | level: 2 |
| | 396 | }}} |
| | 397 | {{{ |
| | 398 | #!python |
| | 399 | # Python |
| | 400 | {'hero': {'hp': 34, 'sp': 8, 'level': 4}, 'orc': {'hp': 12, 'sp': 0, 'level': 2}} |
| | 401 | }}} |
| | 402 | |
| | 403 | A block mapping may be nested in a block sequence: |
| | 404 | {{{ |
| | 405 | # YAML |
| | 406 | - name: PyYAML |
| | 407 | status: 4 |
| | 408 | license: MIT |
| | 409 | language: Python |
| | 410 | - name: PySyck |
| | 411 | status: 5 |
| | 412 | license: BSD |
| | 413 | language: Python |
| | 414 | }}} |
| | 415 | {{{ |
| | 416 | #!python |
| | 417 | # Python |
| | 418 | [{'status': 4, 'language': 'Python', 'name': 'PyYAML', 'license': 'MIT'}, |
| | 419 | {'status': 5, 'license': 'BSD', 'name': 'PySyck', 'language': 'Python'}] |
| | 420 | }}} |
| | 421 | |
| | 422 | === Flow collections === |
| | 423 | |
| | 424 | The syntax of flow collections in YAML is very close to the syntax of list and |
| | 425 | dictionary constructors in Python: |
| | 426 | {{{ |
| | 427 | # YAML |
| | 428 | { str: [15, 17], con: [16, 16], dex: [17, 18], wis: [16, 16], int: [10, 13], chr: [5, 8] } |
| | 429 | }}} |
| | 430 | {{{ |
| | 431 | #!python |
| | 432 | # Python |
| | 433 | {'dex': [17, 18], 'int': [10, 13], 'chr': [5, 8], 'wis': [16, 16], 'str': [15, 17], 'con': [16, 16]} |
| | 434 | }}} |
| | 435 | |
| | 436 | === Scalars === |
| | 437 | |
| | 438 | === Aliases === |
| | 439 | |
| | 440 | === Tags === |
| | 441 | |