id stringlengths 15 54 | text stringlengths 3 133k | title stringclasses 1
value |
|---|---|---|
use_sim_time/naomoveitconfig_5.txt | The moveit package must be run on a remote computer and not directly on your robot. | |
ros_yaml/pythonyaml_359.txt | if __name__ == "__main__":
print(html_tree("".join(sys.stdin.readlines())))
This HTML uses an embedded Google Font for a more pleasant look. The inline CSS styling removes bullet points from regular unordered lists because you use bullet points for key-value mappings. However, lists explicitly marked as sequences u... | |
ros_yaml/rclpyparamstutorialg_91.txt | Well, well, well. | |
ros_yaml/pythonyaml_144.txt | The next example shows one of the standard YAML tags in action: | |
ros_yaml/UsingParametersInACl_11.txt | # Using parameters in a class (Python) ï | |
ros_yaml/pythonyaml_91.txt | def main():
print("Hello world") | |
ros_yaml/pythonyaml_308.txt | if isinstance(event, ScalarEvent):
self._html.append(event.value)
elif isinstance(event, SequenceStartEvent):
self._html.append("<ul>")
self._context.append(list)
elif isinstance(event, SequenceEndEvent):
self._html.append("</ul>")
self... | |
ros_yaml/pythonyaml_52.txt | Note: This implicit typing in YAML seems convenient, but it’s like playing with fire, and it can cause serious problems in edge cases. As a result, the YAML 1.2 specification dropped support for some built-in literals like yes and no. | |
ros_yaml/pythonyaml_357.txt | def html_tree(stream, loader=yaml.SafeLoader):
body = visit(yaml.compose(stream, loader))
return (
"<!DOCTYPE html>"
"<html>"
"<head>"
" <meta charset=\"utf-8\">"
" <title>YAML Tree Preview</title>"
" <link href=\"https://fonts.googleapis.com/css2"
"?fa... | |
ros_yaml/pythonyaml_347.txt | def html_list(node):
items = "".join(f"<li>{visit(child)}</li>" for child in node.value)
return f'<ul class="sequence">{items}</ul>' | |
ros_yaml/pythonyaml_161.txt | # models.py | |
ros_yaml/yamlinpython_70.txt | Subscribe | |
ros_yaml/pythonyaml_83.txt | square: &square
a: 5 | |
ros_yaml/pythonyaml_138.txt | Loader Class Function Description
BaseLoader - Doesn’t resolve or support any tags and constructs only basic Python objects (str, list, dict)
Loader - Kept for backward compatibility but otherwise the same as UnsafeLoader
UnsafeLoader unsafe_load() Supports all standard, library, and custom tags and may construct an ar... | |
ros_yaml/yamlinpython_36.txt |
Viewing data dictionary in yaml format in the file output.yaml
Similarly, you can call the write_yaml_to_file() function with data2 as the argument to convert it to YAML and store it into a YAML file. | |
ros_yaml/pythonyaml_158.txt | # Flow style:
!!python/object/apply:models.Person [John, Doe] | |
ros_yaml/pythonyaml_229.txt | def dump(data, stream=None, Dumper=Dumper, **kwargs): ...
def safe_dump(data, stream=None, **kwargs): ...
def safe_dump_all(documents, stream=None, **kwargs): ...
The first function expects between one and three positional arguments since two of them have optional values. On the other hand, the second and the third fun... | |
ros_yaml/UsingParametersInACl_58.txt |
maintainer='YourName',
maintainer_email='you@email.com',
description='Python parameter tutorial',
license='Apache License 2.0',
| |
ros_yaml/UsingParametersInACl_96.txt | Open a console and navigate to the root of your workspace, ` ros2_ws ` , and
build your new package: | |
ros_yaml/pythonyaml_164.txt | Note that you can use !!python/object/apply against any callable object, including regular functions, and specify the arguments to pass. This lets you execute one of the built-in functions, a custom function, or even a module-level function, which PyYAML will happily import for you. That’s a huge security hole! Imagine... | |
ros_yaml/pythonyaml_17.txt | YAML was originally meant to simplify Extensible Markup Language (XML), but in reality, it has a lot more in common with JavaScript Object Notation (JSON). In fact, it’s a superset of JSON. | |
ros_yaml/UsingParametersInACl_113.txt | Built with [ Sphinx ](https://www.sphinx-doc.org/) using a [ theme
](https://github.com/readthedocs/sphinx_rtd_theme) provided by [ Read the Docs
](https://readthedocs.org) . | |
ros_yaml/yamlinpython_48.txt | script.py
Copywith open(f'{filename}.yaml','r') as f:
data = yaml.safe_load_all(f)
loaded_data = list(data)
with open('output4.yaml', 'w') as file:
yaml.dump_all(loaded_data,file, sort_keys=False)
print('done!')
And you have the complete function as follows: | |
ros_yaml/pythonyaml_56.txt | person:
firstName: John
lastName: Doe
dateOfBirth: 1969-12-31
married: true
spouse:
firstName: Jane
lastName: Smith
children:
- firstName: Bobby
dateOfBirth: 1995-01-17
- firstName: Molly
dateOfBirth: 2001-05-14
You’ve just defined a person, John Doe, who’s married to Jane Smith ... | |
ros_yaml/UsingParametersInACl_100.txt | Linux macOS Windows | |
ros_yaml/rclpyparamstutorialg_102.txt |
$ ros2 run ros2_tutorials test_params_rclpy --ros-args -p this_is_a_non_declared_param:="This will work"
[INFO] [test_params_rclpy]: Params: 8, Is there anybody out there?, [1.1, 2.2]
# In another terminal:
$ ros2 param list
/test_params_rclpy:
this_is_a_non_declared_param
... | |
ros_yaml/pythonyaml_218.txt | >>> import io
>>> stream = io.StringIO()
>>> print(yaml.dump(data, stream))
None | |
ros_yaml/UsingParametersInACl_3.txt | * [ Introducing ` tf2 ` ](../Intermediate/Tf2/Introduction-To-Tf2.html)
* [ Writing a static broadcaster (Python) ](../Intermediate/Tf2/Writing-A-Tf2-Static-Broadcaster-Py.html)
* [ Writing a static broadcaster (C++) ](../Intermediate/Tf2/Writing-A-Tf2-Static-Broadcaster-Cpp.html)
* [ W... | |
ros_yaml/pythonyaml_278.txt | if __name__ == "__main__":
print(colorize("".join(sys.stdin.readlines())))
You import the sys module from Python’s standard library and pass the sys.stdin reference to the colorize() function that you just created. Now, you may run your script in the terminal and enjoy color-coded YAML tokens: | |
ros_yaml/pythonyaml_128.txt | >>> SafeLoader
<class 'yaml.cyaml.CSafeLoader'>
First, you try importing one of the loader classes prefixed with the letter C to denote the use of the C library binding. If that fails, then you import a corresponding class implemented in Python. Unfortunately, this makes your code look more verbose and prevents you fro... | |
ros_yaml/rclpyparamstutorialg_11.txt | It’s very useful, you can start your node with different settings each time,
without having to change your Python code. | |
ros_yaml/rclpyparamstutorialg_124.txt | **
[ ROS2 For Beginners
Learn ROS2 in 1 Week ](https://rbcknd.com/ros2-for-beginners) ** | |
ros_yaml/rclpyparamstutorialg_30.txt |
def __init__(self):
super().__init__('test_params_rclpy')
self.declare_parameters(
namespace='',
parameters=[
('my_str', rclpy.Parameter.Type.STRING),
('my_int', rclpy.Parameter.Type.INTEGER),
('my_double_array', ... | |
ros_yaml/yamlinpython_73.txt | How Classes and Objects Work in Python
16 minute read
Learn how to work with classes and objects in Python in this tutorial. Discover how to define attributes and methods, use class variables and methods, and cr... | |
ros_yaml/pythonyaml_375.txt | Geir Arne Hjelle
Geir Arne | |
ros_yaml/pythonyaml_197.txt | You can also read YAML content straight from a file. Go ahead and create a file with sample YAML content in it and load it into Python using PyYAML: | |
ros_yaml/rclpyparamstutorialg_105.txt | To conclude on that: as a general rule, always declare your parameters first,
and follow the ROS2 way of doing things. Only allow undeclared parameters for
a good reason. | |
ros_yaml/pythonyaml_273.txt | # ...
This new function iterates over a tokenized text in reverse and inserts escape code sequences where indicated by start and end. Note that it’s not the most efficient way of doing this, because you essentially end up making lots of text copies due to slicing and concatenating. | |
ros_yaml/yamlinpython_25.txt | print(yaml_output)
Now, run script.py. You should see the following output: | |
ros_yaml/pythonyaml_276.txt | import sys
import yaml | |
ros_yaml/yamlinpython_3.txt | Mercy Bassey %
Mercy Bassey
This article explains how to manage Python YAML with PyYAML. Earthly greatly improves continuous integration workflows involving Python. Learn how. | |
ros_yaml/pythonyaml_318.txt | # yaml2html.py | |
ros_yaml/pythonyaml_142.txt | >>> import yaml | |
ros_yaml/pythonyaml_207.txt | >>> for document in yaml.safe_load_all(stream):
... print(document)
...
3.14
{'name': 'John Doe', 'age': 53}
['apple', 'banana', 'orange']
The individual documents must start with a triple dash (---) and can optionally end with three dots (...). | |
ros_yaml/pythonyaml_167.txt | # models.py | |
ros_yaml/pythonyaml_29.txt | Note: JSON is the only data format that doesn’t support comments. They were removed from the specification on purpose to simplify the parsers and prevent people from abusing them for custom processing instructions. | |
ros_yaml/pythonyaml_306.txt | def process(self, event): | |
ros_yaml/rclpyparamstutorialg_34.txt |
$ ros2 run ros2_tutorials test_params_rclpy | |
ros_yaml/rclpyparamstutorialg_94.txt | However, there is one case where this could lead to a problem: if, for some
reasons, your node is not aware of all parameters it should have when it is
started. | |
ros_yaml/pythonyaml_323.txt | Click the collapsible section below to reveal the complete source code: | |
ros_yaml/pythonyaml_261.txt | # colorize.py | |
ros_yaml/yamlinpython_61.txt | new-3.png
Conclusion
In this tutorial, you’ve learned how to handle YAML using Python and the PyYAML library. You’ve worked with functions like safe_load(), safe_load_all(), dump(), and dump_all() to manipulate YAML data and even convert it to JSON with Python’s json library. | |
ros_yaml/UsingParametersInACl_74.txt | Now run the node: | |
ros_yaml/rclpyparamstutorialg_5.txt | [ The Robotics Back-End ](https://roboticsbackend.com/) | |
ros_yaml/pythonyaml_297.txt | class HTMLBuilder:
def __init__(self):
self._context = []
self._html = [] | |
ros_yaml/pythonyaml_196.txt | >>> yaml.safe_load("name: Иван".encode("utf-32"))
Traceback (most recent call last):
...
yaml.reader.ReaderError: unacceptable character #x0000:
special characters are not allowed
in "<byte string>", position 1
If you try loading YAML from a text encoded with UTF-32, then you’ll get an error. However, that’s hardly... | |
ros_yaml/rclpyparamstutorialg_141.txt | Analytics | |
ros_yaml/yamlinpython_38.txt | Viewing (data2) list of dictionaries in yaml format in a file (output2.yaml)
Reading YAML in Python
The yaml module comes with a function that can be used to read YAML files. This process of YAML files with PyYAML is also referred to as loading a YAML file. | |
ros_yaml/pythonyaml_363.txt |
All right, that’s all when it comes to parsing YAML documents at a low level using the PyYAML library. The corresponding yaml.emit() and yaml.serialize() functions work the other way around by taking a sequence of events or the root node, respectively, and turning them into a YAML representation. But you’ll rarely nee... | |
ros_yaml/rclpyparamstutorialg_57.txt | Now that you’ve declared parameters in your code, let’s retrieve them in the
node’s code! | |
ros_yaml/pythonyaml_209.txt | Dumping Python Objects to YAML Documents
If you’ve worked with JSON in Python before, then serializing or “dumping” Python objects to YAML will look more familiar than loading them. The PyYAML library has an interface that’s somewhat similar to the built-in json module. It also provides fewer dumper classes and wrapper... | |
ros_yaml/UsingParametersInACl_94.txt | Now open the ` setup.py ` file. Add the ` import ` statements to the top of
the file, and the other new statement to the ` data_files ` parameter to
include all launch files: | |
ros_yaml/pythonyaml_290.txt | import yaml | |
ros_yaml/rclpyparamstutorialg_152.txt | [ 
](https://rbcknd.com/ros2-for-beginners) | |
ros_yaml/pythonyaml_329.txt | >>> value
ScalarNode(tag='tag:yaml.org,2002:float', value='3.14')
Because there are only three kinds of nodes (ScalarNode, SequenceNode, and MappingNode), you might automate their traversal with a recursive function: | |
ros_yaml/rclpyparamstutorialg_84.txt |
from rclpy.parameter import Parameter
...
def __init__(self):
super().__init__('test_params_rclpy')
self.declare_parameter('my_str', rclpy.Parameter.Type.STRING)
self.declare_parameter('my_int', rclpy.Parameter.Type.INTEGER)
self.declare_parameter('my_do... | |
ros_yaml/pythonyaml_270.txt | def colorize(text):
colors = {
yaml.KeyToken: lambda x: f"\033[34;1m{x}\033[0m",
yaml.ValueToken: lambda x: f"\033[36m{x}\033[0m",
yaml.TagToken: lambda x: f"\033[31m{x}\033[0m",
} | |
ros_yaml/rclpyparamstutorialg_140.txt | Performance cookies are used to understand and analyze the key performance
indexes of the website which helps in delivering a better user experience for
the visitors. | |
ros_yaml/pythonyaml_242.txt | Next up, you’ll learn more about dumping custom classes with PyYAML. | |
ros_yaml/pythonyaml_327.txt | >>> key, value = root.value[1].value[0] | |
ros_yaml/pythonyaml_23.txt | In practice, however, the two formats look different, as the YAML specification puts more emphasis on human readability by adding a lot more syntactic sugar and features on top of JSON. As a result, YAML is more applicable to configuration files edited by hand rather than as a transport layer. | |
ros_yaml/pythonyaml_112.txt | Install the PyYAML Library
Python’s most popular third-party YAML library by far is PyYAML, which is consistently one of the top packages downloaded from PyPI. It has an interface that looks somewhat similar to the built-in JSON module, it’s actively maintained, and it has the blessing of the official YAML website, whi... | |
ros_yaml/pythonyaml_314.txt | # ... | |
ros_yaml/pythonyaml_113.txt | To install PyYAML into your active virtual environment, type the following command in your terminal: | |
ros_yaml/pythonyaml_71.txt | person: !!python/object:package_name.module_name.ClassName
age: 42
first_name: John
last_name: Doe
The use of the !!str tag next to a date object makes YAML treat it as a regular string. Question marks (?) denote a mapping key in YAML. They’re usually unnecessary but can help you define a compound key from anothe... | |
ros_yaml/pythonyaml_228.txt | The three wrappers that delegate to yaml.dump_all() have the following function signatures, which reveal their positional arguments: | |
ros_yaml/rclpyparamstutorialg_101.txt | Let’s run this node with a non declared parameter: | |
ros_yaml/UsingParametersInACl_106.txt | ## Summary ï | |
ros_yaml/yamlinpython_4.txt |
If you’ve ever worked with Docker or Kubernetes, you’ll have likely used YAML files. From configuring an application’s services in Docker to defining Kubernetes objects like pods, services, and more—YAML is used for them all. | |
ros_yaml/pythonyaml_223.txt | Dump Multiple Documents
The two YAML-dumping functions in PyYAML, dump() and safe_dump(), have no way of knowing whether you mean to serialize multiple separate documents or a single document comprising an element sequence: | |
ros_yaml/yamlinpython_63.txt | Now, why not take your newfound knowledge a step further? Try converting JSON to CSV next. | |
ros_yaml/pythonyaml_16.txt | An actual markup language, such as Markdown or HTML, lets you annotate text with formatting or processing instructions intermixed with your content. Markup languages are, therefore, primarily concerned with text documents, whereas YAML is a data serialization format that integrates well with common data types native to... | |
ros_yaml/rclpyparamstutorialg_144.txt | Advertisement | |
ros_yaml/pythonyaml_311.txt | Lines 5 to 11 import the needed event types from PyYAML.
Lines 13 and 14 specify the event types corresponding to HTML opening and closing tags.
Lines 24 to 37 append the corresponding HTML tag and update the stack as necessary.
Lines 21, 22, 39, and 40 open or close pending tags on the stack and optionally update the ... | |
ros_yaml/yamlinpython_29.txt | script.py
Copydata2 = [
{
'apiVersion': 'v1',
'kind':'persistentVolume',
'metadata': {'name':'mongodb-pv', 'labels':{'type':'local'}},
'spec':{'storageClassName':'hostpath'},
'capacity':{'storage':'3Gi'},
'accessModes':['ReadWriteOnce'],
'hostpath':{'path':'/mnt/data'}
}, | |
ros_yaml/pythonyaml_40.txt | Ansible: Uses YAML to describe the desired state of the remote infrastructure, manage the configuration, and orchestrate IT processes
Docker Compose: Uses YAML to describe the microservices comprising your Dockerized application
Kubernetes: Uses YAML to define various objects in a computer cluster to orchestrate and ma... | |
ros_yaml/pythonyaml_241.txt | Get Source Code: Click here to get the source code you’ll use to work with YAML in Python. | |
ros_yaml/pythonyaml_6.txt | YAML: The Missing Battery in Python
YAML: The Missing Battery in Python
by Bartosz Zaczyński 2 Comments
intermediate
Share Share Email
Table of Contents | |
ros_yaml/pythonyaml_220.txt | If you want to dump your YAML into a file, then be sure to open the file in write mode. Additionally, you must specify the character encoding through an optional keyword argument to the yaml.dump() function when the file is open in binary mode: | |
ros_yaml/yamlinpython_49.txt | script.py
Copydef read_multiple_block_of_yaml_data(filename,write_file):
with open(f'{filename}.yaml','r') as f:
data = yaml.safe_load_all(f)
loaded_data = list(data)
with open(f'{write_file}.yaml', 'w') as file:
yaml.dump_all(loaded_data,file, sort_keys=False)
print('done!') | |
ros_yaml/pythonyaml_41.txt | Note: The documentation for Python’s logging framework mentions YAML despite the fact that the language doesn’t support YAML natively. | |
ros_yaml/pythonyaml_245.txt | >>> yaml.dump(complex(3, 2))
"!!python/complex '3.0+2.0j'\n"
In the first case, the safe dumper doesn’t know how to represent your complex number in YAML. On the other hand, calling yaml.dump() implicitly uses the unsafe Dump class behind the scenes, which takes advantage of the !!python/complex tag. It’s a similar sto... | |
ros_yaml/yamlinpython_34.txt | Let’s define a Python function write_yaml_to_file() that converts a Python object into YAML and writes it into a file. | |
ros_yaml/UsingParametersInACl_30.txt | ### 1 Create a package ï | |
ros_yaml/UsingParametersInACl_53.txt | Optionally, you can set a descriptor for the parameter. Descriptors allow you
to specify a text description of the parameter and its constraints, like
making it read-only, specifying a range, etc. For that to work, the ` __init__
` code has to be changed to: | |
ros_yaml/pythonyaml_0.txt | Real Python
Start Here
Learn Python
More
Search
— FREE Email Series — | |
ros_yaml/pythonyaml_99.txt | yamllint: A linter for YAML, which can check the syntax and more
yq: A command-line YAML processor based on jq, for filtering data
shyaml: An alternative command-line YAML processor
These are all Python tools, but there’s also a widespread Go implementation of yq, which has a slightly different command-line interface. ... | |
ros_yaml/pythonyaml_319.txt | import sys | |
ros_yaml/pythonyaml_67.txt | numbers: !!set
? 5
? 8
? 13 | |
ros_yaml/pythonyaml_36.txt | Fun Fact: The official yaml.org website is written as a valid YAML document. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.