Category talk:Programming Tasks: Difference between revisions

Undo revision 276551 by Anthon (talk)
(→‎YAML: new section)
(Undo revision 276551 by Anthon (talk))
Line 37:
 
This task demonstrates examples of the producer-consumer pattern in the chosen language. There are several variations on the producer-consumer pattern including synchronous, asynchronous, bounded queue, and unbounded queue. The simplest implementation of a producer-consumer has one producing task or thread and one consuming task or thread. More complex implementations may have 1..P producers and 1..C consumers. The number of producers and consumers need not be the same. A producer-consumer implementation may require that every data item produced by the producer(s) must be consumed exactly once by one of the consumers, or it may require that the consumer(s) only sample the data produced by the producer(s). An example of a sampling application might be an automated weather station. The weather station may produce temperature readings at a rate of 100Hz while the consumer displaying those readings on a website may only display changes at a rate of 1Hz. The consumer only needs to display one reading per second and can ignore 99 readings every second.
 
== YAML ==
 
{{task}}
Load a [[wp:YAML|YAML 1.2]] document from a file ('input.yaml') into a data structure.
Modify the data structure, by changing the decimal 42 to 196, the string "plain" to "without quotes" and by reordering the sequence of Boolean values. Serialize the modified data back to YAML in a file ("output.yaml").
 
Indicate if external libraries need to be installed, if any.
 
Use the following implicit YAML 1.2 document, stored in a file named "input.yaml":
<pre>
scalar numbers:
- decimal: 196 # the most common number formats
octal: 0o52
hex: 0x2a
- float: 3.14
scientific: 6.02214086E23
scalar strings:
- without quotes
- "double quoted"
- 'single quoted'
- |
literal multi line
block style scalar
- >
folded multi line
block style scalar
other scalars:
boolean: [false, true]
date: 2011-10-02
anchors and aliases:
- &abc Hello
- [*abc, World!] # Hello World! in YAML
[1, 2]: first two natural numbers
</pre>
 
Indicate if any constructs in "input.yaml" are not supported and in how far input.yaml and output.yaml differ (besides the intended changes).
 
=={{header|Python}}==
{{works with|Python|2.7}}{{works with|Python|3.3+}}
 
Python has no support for YAML in the standard library. The YAML standard (1.2, released 2009) is currently (2019) only supported by [https://yaml.readthedocs.io/en/latest/ ruamel.yaml]. Install this library by using "pip install ruamel.yaml"
 
<lang Python>
yaml = YAML()
yaml.preserve_quotes = True
with open("input.yaml", "rb") as fp:
data = yaml.load(fp)
data['scalar numbers'][0]['decimal'] = 196
data['scalar strings'][0] = 'without quotes'
data['other scalars']['boolean'][:] = [False, True]
with open("output.yaml", "wb") as fp:
yaml.dump(data, fp)
</lang>
 
The output file only differs for the specified three values, formatting and comments and all other elements are preserved as-is.
Anonymous user