ruamel - block_seq_indent and indent - yaml

I have been struggling with getting my YAML file with correct indentation after using yaml.round_trip_dump. I am trying to figure what is the difference between block_seq_indent and indent. Couldn't really find anything useful in the documentation as well.

indent is the normal indent that ruamel.yaml inherited from PyYAML. It affects both mapping keys and sequences elements. For sequences that means it doesn't affect the hash ('-') before a sequence element.
So if you run:
import sys
import ruamel.yaml
d = dict(a=1, b=[1, 2, {3: [3.1, 3.2, 3.3]}], c=dict(d=1, e=2))
ruamel.yaml.safe_dump(d, sys.stdout, default_flow_style=False, explicit_start=True)
ruamel.yaml.safe_dump(d, sys.stdout, default_flow_style=False, indent=4, explicit_start=True)
The output will be:
---
a: 1
b:
- 1
- 2
- 3:
- 3.1
- 3.2
- 3.3
c:
d: 1
e: 2
---
a: 1
b:
- 1
- 2
- 3:
- 3.1
- 3.2
- 3.3
c:
d: 1
e: 2
If you also provide block_seq_indent you can do:
ruamel.yaml.safe_dump(d, sys.stdout, default_flow_style=False, indent=4,
block_seq_indent=3, explicit_start=True)
to get:
a: 1
b:
- 1
- 2
- 3:
- 3.1
- 3.2
- 3.3
c:
d: 1
e: 2
To have even more control you should use the new ruamel.yaml API where you can do:
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=3, sequence=5, offset=2)
yaml.explicit_start = True
yaml.dump(d, sys.stdout)
to get:
a: 1
b:
- 1
- 2
- 3:
- 3.1
- 3.2
- 3.3
c:
d: 1
e: 2
i.e. you can use offset to position the dash within the spaces that are the indent for the sequence elements.
This is documented here

Related

Smart tables for Sphinx (reStructuredText)

Are there any smart table extensions for using tables in RST?
It is too tedious to use tables with multiple columns having to type each row and column.
Especially if I want to merge or split the cells in the table as shown below:
I am trying to get the above table using flat-table. Can anyone suggest on how to get this?
.. flat-table:: Characteristics of the BLE badge
:header-rows: 1
* - Col 1
- Col 2
- Col 3
* - :rspan:`2` 0xfee7
- 0xfec7
- WRITE
* - 0xfec8
- INDICATE
* - 0xfec9
- READ
* - 0xfee0
- 0xfee1
- NOTIFY, READ, WRITE
Using the Linuxdoc extension to use the flat-tables.
First, install the linuxdoc Python package:
pip install linuxdoc
Next, add linuxdoc.rstFlatTable to the list of extensions in your Sphinx project's conf.py:
extensions = [
'linuxdoc.rstFlatTable',
]
The linuxdoc manual contains an example that features combinations of row and column spans.
The following reStructuredText produces your table:
.. flat-table:: Spanning table cells
:header-rows: 2
* - :rspan:`1` Col 1
- :rspan:`1` Col 2
- :rspan:`1` Col 3
- :cspan:`2` Col 4
* - Col 4a
- Col 4b
- Col 4c
* - 1
- 2
- 3
- 4a
- 4b
- 4c
Note that if you set header-rows option to 1, linuxdoc produces a malformed table.

Hierarchical list of name value pairs in YAML

What's the best way to represent a hierarchical list of name value pairs like the following in YAML:
name_1: value_1
subName1_1: subValue1_1
subName1_2: subValue1_2
name_2: value_2
subName2_1: subValue2_1
subName2_2: subValue2_2
name_3: value_3
subName3_1: subValue3_1
subName3_2: subValue3_2
name_4: value_4
subName4_1: subValue4_1
subName4_2: subValue4_2
I am thinking of the following but not sure if this is the best way or not:
- name_1:
ID: 1
subNames:
- subName1_1:
ID: 1
- subName1_2:
ID: 2
- name_2:
ID: 2
subNames:
- subName2_1:
ID: 1
- subName2_2:
ID: 2
or I could also do:
- Name: Name_1
ID: 1
SubNames:
- SubName: subName1_1
ID: 1
- SubName: subName1_2
ID: 2
- Name: Name_2
ID: 2
SubNames:
- SubName: subName2_1
ID: 1
- SubName: subName2_2
ID: 2
I need the name_* to be unique as well as their corresponding values as well so I'd prefer something which python can easily consume to validate there are no duplicates.
Well there's the value key type. It's not part of the standard and defined for YAML 1.1, but it has been designed to solve this problem. It suggests you basically have a value in your mapping named = which contains the default value:
name_1:
=: value_1
subName1_1: subValue1_1
subName1_2: subValue1_2
name_2:
=: value_2
subName2_1: subValue2_1
subName2_2: subValue2_2
name_3:
=: value_3
subName3_1: subValue3_1
subName3_2: subValue3_2
name_4:
=: value_4
subName4_1: subValue4_1
subName4_2: subValue4_2
Alternatively, you could make the values a list with single key_value pairs:
name_1:
- value_1
- subName1_1: subValue1_1
- subName1_2: subValue1_2
name_2:
- value_2
- subName2_1: subValue2_1
- subName2_2: subValue2_2
name_3:
- value_3
- subName3_1: subValue3_1
- subName3_2: subValue3_2
name_4:
- value_4
- subName4_1: subValue4_1
- subName4_2: subValue4_2
You can write this with flow sequences since YAML allows flow sequences to contain single key-value pairs which will be interpreted as implicit mappings:
name_1: [value_1,
subName1_1: subValue1_1,
subName1_2: subValue1_2]
name_2: [value_2,
subName2_1: subValue2_1,
subName2_2: subValue2_2]
name_3: [value_3,
subName3_1: subValue3_1,
subName3_2: subValue3_2]
name_4: [value_4,
subName4_1: subValue4_1,
subName4_2: subValue4_2]
Be aware that when you do this, you can't have any kind of block-style nodes in the subnames, but other flow nodes will be fine.

Configuring Sass Lint for BEM

So i'm moving to writing my sass to the BEM convention. I've used the sass-lint configuration generator to create my config and only edited the class-name-format's - convention: to strictbem however I'm still having some issues with it.
Maybe I'm misinterpreting BEM?
Error:
[sass-lint] Class '.bus__tyre--front' should be written in BEM (Block
Element Modifier) format (class-name-format)
<element class="bus__tyre--front">
Sass:
.bus {
position: relative;
&__tyre {
position: absolute;
&--front {
bottom: -22px;
right: 3%;
width: 17%;
}
}
}
sass-lint.yml:
# sass-lint config generated by make-sass-lint-config v0.1.2
#
# The following scss-lint Linters are not yet supported by sass-lint:
# DisableLinterReason, ElsePlacement, PropertyCount, SelectorDepth
# SpaceAroundOperator, TrailingWhitespace, UnnecessaryParentReference, Compass::*
#
# The following settings/values are unsupported by sass-lint:
# Linter Indentation, option "allow_non_nested_indentation"
# Linter Indentation, option "character"
# Linter NestingDepth, option "ignore_parent_selectors"
# Linter PropertySortOrder, option "min_properties"
# Linter PropertySortOrder, option "separate_groups"
# Linter SpaceBeforeBrace, option "allow_single_line_padding"
# Linter VendorPrefix, option "identifier_list"
files:
include: '**/*.scss'
options:
formatter: stylish
merge-default-rules: false
rules:
bem-depth:
- 0
- max-depth: 1
border-zero:
- 1
- convention: zero
brace-style:
- 1
- allow-single-line: true
class-name-format:
- 1
- convention: strictbem
clean-import-paths:
- 1
- filename-extension: false
leading-underscore: false
empty-line-between-blocks:
- 1
- ignore-single-line-rulesets: true
extends-before-declarations: 1
extends-before-mixins: 1
final-newline:
- 1
- include: true
force-attribute-nesting: 1
force-element-nesting: 1
force-pseudo-nesting: 1
function-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
hex-length:
- 1
- style: short
hex-notation:
- 1
- style: lowercase
id-name-format:
- 1
- convention: hyphenatedlowercase
indentation:
- 1
- size: 2
leading-zero:
- 1
- include: false
mixin-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
mixins-before-declarations: 1
nesting-depth:
- 1
- max-depth: 3
no-color-keywords: 1
no-color-literals: 1
no-css-comments: 1
no-debug: 1
no-duplicate-properties: 1
no-empty-rulesets: 1
no-extends: 0
no-ids: 1
no-important: 1
no-invalid-hex: 1
no-mergeable-selectors: 1
no-misspelled-properties:
- 1
- extra-properties: []
no-qualifying-elements:
- 1
- allow-element-with-attribute: false
allow-element-with-class: false
allow-element-with-id: false
no-trailing-zero: 1
no-transition-all: 0
no-url-protocols: 1
no-vendor-prefixes:
- 1
- additional-identifiers: []
excluded-identifiers: []
placeholder-in-extend: 1
placeholder-name-format:
- 1
- convention: hyphenatedlowercase
property-sort-order:
- 1
- ignore-custom-properties: false
property-units:
- 1
- global:
- ch
- em
- ex
- rem
- cm
- in
- mm
- pc
- pt
- px
- q
- vh
- vw
- vmin
- vmax
- deg
- grad
- rad
- turn
- ms
- s
- Hz
- kHz
- dpi
- dpcm
- dppx
- '%'
per-property: {}
quotes:
- 1
- style: single
shorthand-values:
- 1
- allowed-shorthands:
- 1
- 2
- 3
single-line-per-selector: 1
space-after-bang:
- 1
- include: false
space-after-colon:
- 1
- include: true
space-after-comma:
- 1
- include: true
space-before-bang:
- 1
- include: true
space-before-brace:
- 1
- include: true
space-before-colon: 1
space-between-parens:
- 1
- include: false
trailing-semicolon: 1
url-quotes: 1
variable-for-property:
- 0
- properties: []
variable-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
zero-unit: 1
Judging by #335 and #319 in the scss-lint repo, it looks like you need to change:
class-name-format:
- 1
- convention: strictbem
to:
class-name-format:
- 1
- convention: hyphenatedbem
I hope that helps!
Instead of hyphenated_BEM it should be hyphenatedbem.
Example from the sass-lint docs

YAML parsing - c++

I'm very new to YAML and what to parse a file I have made and verified is of correct structure. Here is the YAML:
---
DocType: DAQ Configuration File
# Creation Date: Wed Sep 21 10:34:06 2016
File Settings:
- Configuration Path: /mnt/sigma/daqengine/config/
- Configuration Name: daqengine.yaml
- Build File Path: ./bld
- Log File Path: ./log
- Record H5 Files: true
Engine Data:
- Make: EOS
- Model: M280
- Number of DAQs: 1
DAQ01 Settings:
- IP Address: 192.168.116.223
- Log DAQ Internal Temps: true
- T0 Temp. Limit: 55
- T0 Clear OTC Level Temp.: 50
- T0 Sample Averaging: 5
- T1 Temp. Limit: 60
- T1 Clear OTC Level Temp.: 55
- T1 Sample Averaging: 2
- No. Layers Used: 1
DAQ01 Layer 0 Settings:
- Model: AI-218
- Sample Rate: 50000
- Channels Used: 8
- Channel01: HF Temp
- Channel02: On-Axis PD
- Channel03: Off-Axis PD
- Channel04: X-Position
- Channel05: Y-Position
- Channel06: LDS
- Channel07: Laser Power
- Channel08: Unused
...
I need to extract the sequenced values to each key it is tagged to in C++.

Ruby's YAML only loads first Records from a large File, why?

Okay, I have the following YAML file that was generated by using yaml_db for Rails. So this is basically an autogenerated export of my Rails database:
---
admins:
columns:
- id
- username
- email
- encrypted_password
- password_salt
- sign_in_count
- current_sign_in_at
- last_sign_in_at
- current_sign_in_ip
- last_sign_in_ip
- failed_attempts
- unlock_token
- locked_at
- created_at
- updated_at
records:
- - 1
-
- lalala#lalala.at
- $2a$10$dZU50HD6paWS7EjKuWAruOFdwt9eqxiNTRh/D4sj8cqSzy5gjYd2i
- $2a$10$dZU50HD6paWS7EjKuWAruO
- 86
- 2011-01-27 07:37:45 Z
- 2011-01-26 13:27:13 Z
- 12.34.56.78
- 12.34.56.78
- 0
-
-
- 2010-12-23 09:20:46 Z
- 2011-01-27 07:37:45 Z
- - 2
- admin
- lalalalala#lalalala.com
- $2a$10$3DML64hdCCvG90bnhIpN/unEEm6C.a9FqGrAFlFHU0.2D54DSQ1Ni
- $2a$10$3DML64hdCCvG90bnhIpN/u
- 1
- 2011-01-21 09:52:14 Z
- 2011-01-21 09:52:14 Z
- 12.34.56.78
- 12.34.56.78
- 0
-
-
- 2011-01-05 14:29:49 Z
- 2011-01-21 09:52:14 Z
---
experiments:
columns:
- id
- description
- startdate
- enddate
- maps_base_URI
- maps_count
- queries_count
- proposals_count
- created_at
- updated_at
.......
Now when I try to load this YAML file in Ruby with:
file = YAML.load(File.open("data-2011-01-27.yml"))
It doesn't load more than the first admin, not even the experiments:
ruby-1.9.2-p0 > file.keys
=> ["admins"]
ruby-1.9.2-p0 > file["admins"]["records"].count
=> 1
Why is that? I would assume that the autogenerated .yml file is syntactically correct? When I run rake:db:dump and rake:db:load it works just fine.
Three hyphens (---) separate multiple documents. See YAML.load_stream to load them all:
documents = YAML.load_stream(open("data-2011-01-27.yml")).documents
documents.map(&:keys)
#=> [["admins"], ["experiments"]]

Resources