Changes in trunk/lib/syck/__init__.py [25:19]
- File:
-
- 1 edited
-
trunk/lib/syck/__init__.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/syck/__init__.py
r25 r19 1 """2 YAML is a data serialization format designed for human readability and3 interaction with scripting languages.4 5 Syck is an extension for reading and writing YAML in scripting languages.6 7 PySyck provides Python bindings for Syck YAML parser and emitter.8 9 To start working with PySyck, import the package 'syck':10 >>> from syck import *11 12 To parse a YAML document into a Python object, use the function 'load()':13 >>> load('''14 ... - Mark McGwire15 ... - Sammy Sosa16 ... - Ken Griffey17 ... ''')18 ['Mark McGwire', 'Sammy Sosa', 'Ken Griffey']19 20 To emit a Python object into a YAML document, use the function 'dump()':21 >>> print dump(['Mark McGwire', 'Sammy Sosa', 'Ken Griffey'])22 ---23 - Mark McGwire24 - Sammy Sosa25 - Ken Griffey26 27 You may get access to the YAML parser tree using the function 'parse()':28 >>> root_node = parse('''29 ... - Mark McGwire30 ... - Sammy Sosa31 ... - Ken Griffey32 ... ''')33 >>> root_node34 <_syck.Seq object at 0xb7a1f874>35 >>> root_node.kind36 'seq'37 >>> root_node.value38 [<_syck.Scalar object at 0xb7a1e5fc>, <_syck.Scalar object at 0xb7a1e65c>, <_syck.Scalar object at 0xb7a1e6bc>]39 40 You may now use the function 'emit()' to obtain the YAML document again:41 >>> print emit(root_node)42 ---43 - Mark McGwire44 - Sammy Sosa45 - Ken Griffey46 47 What do you get if you apply the function 'dump()' to root_node? Let's try it:48 >>> print dump(root_node)49 --- !python/object:_syck.Seq50 value:51 - !python/object:_syck.Scalar52 value: Mark McGwire53 tag: tag:yaml.org,2002:str54 - !python/object:_syck.Scalar55 value: Sammy Sosa56 tag: tag:yaml.org,2002:str57 - !python/object:_syck.Scalar58 value: Ken Griffey59 tag: tag:yaml.org,2002:str60 61 As you can see, PySyck allow you to represent complex Python objects.62 63 You can also dump the generated YAML output into any file-like object:64 >>> import os65 >>> stream = os.tmpfile()66 >>> object = ['foo', 'bar', ['baz']]67 >>> dump(object, stream)68 >>> stream.seek(0)69 >>> print stream.read()70 ---71 - foo72 - bar73 - - baz74 75 To load several documents from a single YAML stream, use the function76 'load_documents()':77 >>> source = '''78 ... ---79 ... american:80 ... - Boston Red Sox81 ... - Detroit Tigers82 ... - New York Yankees83 ... national:84 ... - New York Mets85 ... - Chicago Cubs86 ... - Atlanta Braves87 ... ---88 ... - [name , hr, avg ]89 ... - [Mark McGwire, 65, 0.278]90 ... - [Sammy Sosa , 63, 0.288]91 ... '''92 >>> for document in load_documents(source):93 ... print document94 ...95 {'national': ['New York Mets', 'Chicago Cubs', 'Atlanta Braves'], 'american': ['Boston Red Sox', 'Detroit Tigers', 'New York Yankees']}96 [['name', 'hr', 'avg'], ['Mark McGwire', 65, 0.27800000000000002], ['Sammy Sosa', 63, 0.28799999999999998]]97 98 See the source code for more details.99 """100 101 1 102 2 from _syck import *
Note: See TracChangeset
for help on using the changeset viewer.
