| | 475 | Each style has its own quirks. A plain scalar does not use indicators to denote its |
| | 476 | start and end, therefore it's the most restricted style. Its natural applications are |
| | 477 | names of attributes and parameters. |
| | 478 | |
| | 479 | Using single-quoted scalars, you may express any value that does not contain special characters. |
| | 480 | No escaping occurs for single quoted scalars except that duplicate quotes '''`''`''' are replaced |
| | 481 | with a single quote '''`'`'''. |
| | 482 | |
| | 483 | Double-quoted is the most powerful style and the only style that can express any scalar value. |
| | 484 | Double-quoted scalars allow ''escaping''. Using escaping sequences '''`\x**`''' and '''`\u****`''', |
| | 485 | you may express any ASCII or Unicode character. |
| | 486 | |
| | 487 | There are two kind of block scalar styles: '''literal''' and '''folded'''. The literal style is |
| | 488 | the most suitable style for large block of text such as source code. The folded style is similar |
| | 489 | to the literal style, but two consequent non-empty lines are joined to a single line separated |
| | 490 | by a space character. |
| | 491 | |
| | 544 | The following table describes how nodes with different tags are converted |
| | 545 | to Python objects. |
| | 546 | |
| | 547 | || '''YAML tag''' || '''Python type''' || |
| | 548 | || ''Standard YAML tags'' || || |
| | 549 | || `!!null` || `None` || |
| | 550 | || `!!bool` || `bool` || |
| | 551 | || `!!int` || `int` or `long` || |
| | 552 | || `!!float` || `float` || |
| | 553 | || `!!binary` || `str` || |
| | 554 | || `!!timestamp` || `datetime.datetime` || |
| | 555 | || `!!omap`, ``!!pairs`` || `list` of pairs || |
| | 556 | || `!!set` || `set` || |
| | 557 | || `!!str` || `str` or `unicode` || |
| | 558 | || `!!seq` || `list` || |
| | 559 | || `!!map` || `dict` || |
| | 560 | || ''Python-specific tags'' || || |
| | 561 | || `!!python/none` || `None` || |
| | 562 | || `!!python/bool` || `bool` || |
| | 563 | || `!!python/str` || `str` || |
| | 564 | || `!!python/unicode` || `unicode` || |
| | 565 | || `!!python/int` || `int` || |
| | 566 | || `!!python/long` || `long` || |
| | 567 | || `!!python/float` || `float` || |
| | 568 | || `!!python/complex` || `complex` || |
| | 569 | || `!!python/list` || `list` || |
| | 570 | || `!!python/tuple` || `tuple` || |
| | 571 | || `!!python/dict` || `dict` || |
| | 572 | || ''Complex Python tags'' || || |
| | 573 | || `!!python/name:module.name` || `module.name` || |
| | 574 | || `!!python/module:package.module` || `package.module` || |
| | 575 | || `!!python/object:module.cls` || `module.cls` instance || |
| | 576 | || `!!python/object/new:module.cls` || `module.cls` instance || |
| | 577 | || `!!python/object/apply:module.f` || value of `f(...)` || |
| | 578 | |
| | 579 | |
| | 580 | === String conversion === |
| | 581 | |
| | 582 | There are four tags that are converted to `str` and `unicode` values: |
| | 583 | `!!str`, `!!binary`, `!!python/str`, and `!!python/unicode`. |
| | 584 | |
| | 585 | `!!str`-tagged scalars are converted to `str` objects if its value is ''ASCII''. Otherwise it is converted to `unicode`. |
| | 586 | `!!binary`-tagged scalars are converted to `str` objects with its value decoded using the ''base64'' encoding. |
| | 587 | `!!python/str` scalars are converted to `str` objects encoded with ''utf-8'' encoding. |
| | 588 | `!!python/unicode` scalars are converted to `unicode` objects. |
| | 589 | |
| | 590 | Conversely, a `str` object is converted to |
| | 591 | 1. a `!!str` scalar if its value is ''ASCII''. |
| | 592 | 2. a `!!python/str` scalar if its value is a correct ''utf-8'' sequence. |
| | 593 | 3. a `!!binary` scalar otherwise. |
| | 594 | |
| | 595 | A `unicode` object is converted to |
| | 596 | 1. a `!!python/unicode` scalar if its value is ''ASCII''. |
| | 597 | 2. a `!!str` scalar otherwise. |
| | 598 | |
| | 599 | === Names and modules === |
| | 600 | |
| | 601 | In order to represent static Python objects like functions or classes, you need to use |
| | 602 | a complex '''`!!python/name`''' tag. For instance, the function '''`yaml.dump`''' can be represented as |
| | 603 | {{{ |
| | 604 | !!python/name:yaml.dump |
| | 605 | }}} |
| | 606 | |
| | 607 | |
| | 608 | Similarly, modules are represented using the tag '''`!python/module`''': |
| | 609 | {{{ |
| | 610 | !!python/module:yaml |
| | 611 | }}} |
| | 612 | |
| | 613 | === Objects === |
| | 614 | |
| | 615 | Any pickleable object can be serialized using the '''`!!python/object`''' tag: |
| | 616 | {{{ |
| | 617 | !!python/object:module.Class { attribute: value, ... } |
| | 618 | }}} |
| | 619 | |
| | 620 | In order to support the pickle protocol, two additional forms of the '''`!!python/object`''' tag |
| | 621 | are provided: |
| | 622 | {{{ |
| | 623 | !!python/object/new:module.Class |
| | 624 | args: [argument, ...] |
| | 625 | kwds: {key: value, ...} |
| | 626 | state: ... |
| | 627 | listitems: [item, ...] |
| | 628 | dictitems: [key: value, ...] |
| | 629 | }}} |
| | 630 | {{{ |
| | 631 | !!python/object/apply:module.function |
| | 632 | args: [argument, ...] |
| | 633 | kwds: {key: value, ...} |
| | 634 | state: ... |
| | 635 | listitems: [item, ...] |
| | 636 | dictitems: [key: value, ...] |
| | 637 | }}} |
| | 638 | |
| | 639 | If only the '''`args`''' field is non-empty, the above records can be shortened: |
| | 640 | {{{ |
| | 641 | !!python/object/new:module.Class [argument, ...] |
| | 642 | }}} |
| | 643 | {{{ |
| | 644 | !!python/object/apply:module.function [argument, ...] |
| | 645 | }}} |