| | 139 | |
| | 140 | The '''`yaml.dump`''' function accepts a Python object and produces a YAML document. |
| | 141 | {{{ |
| | 142 | #!python |
| | 143 | >>> print yaml.dump({'name': 'Silenthand Olleander', 'race': 'Human', |
| | 144 | ... 'traits': ['ONE_HAND', 'ONE_EYE']}) |
| | 145 | |
| | 146 | name: Silenthand Olleander |
| | 147 | race: Human |
| | 148 | traits: [ONE_HAND, ONE_EYE] |
| | 149 | }}} |
| | 150 | |
| | 151 | '''`yaml.dump`''' accepts the second optional argument, which must be an open file. |
| | 152 | In this case, '''`yaml.dump`''' will write the produced YAML document into the file. |
| | 153 | Otherwise, '''`yaml.dump`''' returns the produced document. |
| | 154 | |
| | 155 | {{{ |
| | 156 | #!python |
| | 157 | >>> stream = file('document.yaml', 'w') |
| | 158 | >>> yaml.dump(data, stream) # Write a YAML representation of data to 'document.yaml'. |
| | 159 | >>> print yaml.dump(data) # Output the document to the screen. |
| | 160 | }}} |
| | 161 | |
| | 162 | If you need to dump several YAML documents to a single stream, use the function |
| | 163 | '''`yaml.dump_all`'''. '''`yaml.dump_all`''' accepts a list or a generator producing |
| | 164 | Python objects to be serialized into a YAML document. The second optional argument is |
| | 165 | an open file. |
| | 166 | |
| | 167 | {{{ |
| | 168 | #!python |
| | 169 | >>> print yaml.dump([1,2,3], explicit_start=True) |
| | 170 | --- [1, 2, 3] |
| | 171 | |
| | 172 | >>> print yaml.dump_all([1,2,3], explicit_start=True) |
| | 173 | --- 1 |
| | 174 | --- 2 |
| | 175 | --- 3 |
| | 176 | }}} |
| | 177 | |
| | 178 | You may even dump instances of Python classes. |
| | 179 | |
| | 180 | {{{ |
| | 181 | #!python |
| | 182 | >>> class Hero: |
| | 183 | ... def __init__(self, name, hp, sp): |
| | 184 | ... self.name = name |
| | 185 | ... self.hp = hp |
| | 186 | ... self.sp = sp |
| | 187 | ... def __repr__(self): |
| | 188 | ... return "%s(name=%r, hp=%r, sp=%r)" % ( |
| | 189 | ... self.__class__.__name__, self.name, self.hp, self.sp) |
| | 190 | |
| | 191 | >>> print yaml.dump(("Galain Ysseleg", hp=-3, sp=2)) |
| | 192 | |
| | 193 | !!python/object:__main__.Hero {hp: -3, name: Galain Ysseleg, sp: 2} |
| | 194 | }}} |
| | 195 | |
| | 196 | '''`yaml.dump`''' supports a number of keyword arguments that specify |
| | 197 | formatting details for the emitter. For instance, you may set the |
| | 198 | preferred intendation and width or use the canonical YAML format. |
| | 199 | {{{ |
| | 200 | #!python |
| | 201 | >>> print yaml.dump(range(50)) |
| | 202 | |
| | 203 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
| | 204 | 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, |
| | 205 | 43, 44, 45, 46, 47, 48, 49] |
| | 206 | |
| | 207 | >>> print yaml.dump(range(50), width=50, indent=4) |
| | 208 | |
| | 209 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| | 210 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, |
| | 211 | 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| | 212 | 40, 41, 42, 43, 44, 45, 46, 47, 48, 49] |
| | 213 | |
| | 214 | >>> print yaml.dump(range(5), canonical=True) |
| | 215 | |
| | 216 | --- |
| | 217 | !!seq [ |
| | 218 | !!int "0", |
| | 219 | !!int "1", |
| | 220 | !!int "2", |
| | 221 | !!int "3", |
| | 222 | !!int "4", |
| | 223 | ] |
| | 224 | }}} |