-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
093e26a
commit 2658737
Showing
51 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
## Using YAML with Python | ||
|
||
### Introduction to YAML | ||
|
||
YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It is commonly used for configuration files, data exchange between applications, and also as a language-independent alternative to XML or JSON. YAML's simplicity and readability make it popular among developers. | ||
|
||
### YAML Syntax | ||
|
||
YAML uses indentation and whitespace to represent data structures. It supports various data types including mappings (key-value pairs), sequences (arrays/lists), strings, integers, floats, booleans, null values, and more. | ||
|
||
Here's a basic example of YAML syntax: | ||
|
||
```yaml | ||
key1: value1 | ||
key2: value2 | ||
nested_key: | ||
- item1 | ||
- item2 | ||
``` | ||
### Installing PyYAML | ||
PyYAML is a Python library that allows you to work with YAML files. To install PyYAML, you can use pip, Python's package manager: | ||
```bash | ||
pip install pyyaml | ||
``` | ||
|
||
### Reading YAML Files in Python | ||
|
||
You can use the `yaml` module in Python to read YAML files. The `yaml.safe_load()` function is commonly used to safely load YAML data from a file or a string. | ||
|
||
```python | ||
import yaml | ||
|
||
with open('data.yaml', 'r') as file: | ||
data = yaml.safe_load(file) | ||
|
||
print(data) | ||
``` | ||
|
||
### Writing YAML Files in Python | ||
|
||
To write data to a YAML file in Python, you can use the `yaml.dump()` function. It serializes Python data into a YAML formatted string. | ||
|
||
```python | ||
import yaml | ||
|
||
data = { | ||
'key1': 'value1', | ||
'key2': [1, 2, 3], | ||
'key3': {'nested_key': 'nested_value'} | ||
} | ||
|
||
with open('output.yaml', 'w') as file: | ||
yaml.dump(data, file) | ||
``` | ||
|
||
### Advanced Usage | ||
|
||
#### Handling Custom Objects | ||
|
||
PyYAML supports serialization and deserialization of custom objects. You can define custom `representer` and `constructor` functions to handle serialization and deserialization of custom objects respectively. | ||
|
||
#### Handling Anchors and Aliases | ||
|
||
YAML allows the use of anchors (`&`) and aliases (`*`) to reference the same data elsewhere in the document. PyYAML provides support for handling anchors and aliases during parsing and dumping YAML data. | ||
|
||
### Conclusion | ||
|
||
YAML is a versatile data serialization format that is widely used in the software development community. With PyYAML, you can easily work with YAML files in Python, enabling seamless integration of YAML data into your Python applications. Whether you're reading configuration files, exchanging data between systems, or storing structured data, YAML combined with Python offers a powerful and flexible solution. |