Difference between revisions of "Yaml"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
import sys
 
import sys
 
from pprint import pprint
 
from pprint import pprint
 +
# n = None
 +
#count = 1
 
# def Merge(dict1, dict2):
 
# def Merge(dict1, dict2):
 
#    return(dict2.update(dict1))
 
#    return(dict2.update(dict1))
y_count = "${count}"
+
def count_dict(d):
d_base = {'count': 1}
+
    y_count = "${count}"
 +
    # d_base = {'y_count': 1}
 +
    if 'y_count' not in d:
 +
        d['y_count'] = 1
 +
    # d_base.update(d)
 +
    n = {}
 +
    l = []
 +
    for i in range(1, d['y_count']+1):
 +
        l.append(i)
 +
    for i in range(1, d['y_count']+1):
 +
        n = {}
 +
        for k,v in d.items():
 +
            if y_count in str(k):
 +
                k = k.replace(y_count, str(i))
 +
            if y_count in str(v):
 +
                v = v.replace(y_count, str(i))
 +
            n[k] = v
 +
        n['y_count.index'] = i
 +
        l[i-1]=n
 +
    return l
 +
 
 +
 
 
d = {'acount-${count}': 5, 'one': '${count}-one', 'two': 'twoval', 'y_count': 3}
 
d = {'acount-${count}': 5, 'one': '${count}-one', 'two': 'twoval', 'y_count': 3}
d_base.update(d)
+
# d = {'acount-${count}': 5, 'one': '${count}-one', 'two': 'twoval'}
n = {}
+
d = count_dict(d)
l = []
+
pprint(d)
for i in range(d['y_count']):
+
```
    l.append(i)
+
 
for i in range(d['y_count']):
+
 
    n = {}
+
 
    for k,v in d.items():
+
# More
        if y_count in str(k):
+
```
            k = k.replace(y_count, str(i))
+
import yaml
        if y_count in str(v):
+
from yaml.resolver import *
            v = v.replace(y_count, str(i))
+
from jinja2 import Template, Undefined
        n[k] = v
+
 
    n['y_count.index'] = i
+
class NullUndefined(Undefined):
    l[i]=n
+
  def __getattr__(self, key):
pprint(l)
+
    return ''
 +
 
 +
source = '''
 +
{'y': {'a1': 'foo', 'a2': 'bar{{a1}}', 'a3': {'b1': 'bird', 'b2': 'red{{b1}}'}},
 +
'x': {'c1': 'turtle'}}
 +
'''
 +
 
 +
def resolve_in_dict(loader, node):
 +
  assert isinstance(node, yaml.MappingNode)
 +
  values = loader.construct_mapping(node, deep=True)
 +
  for key, value in values.items():
 +
    if isinstance(value, str):
 +
      t = Template(value, undefined=NullUndefined)
 +
      values[key] = t.render(values)
 +
  return values
  
 +
yaml.SafeLoader.add_constructor(BaseResolver.DEFAULT_MAPPING_TAG, resolve_in_dict)
  
sys.exit()
+
print(yaml.safe_load(source))
 
```
 
```
 +
https://stackoverflow.com/questions/62963366/using-recursion-to-string-format-yaml-file

Latest revision as of 14:35, 22 February 2022

https://idratherbewriting.com/learnapidoc/pubapis_yaml.html

templating counts

import sys
from pprint import pprint
# n = None
#count = 1
# def Merge(dict1, dict2):
#     return(dict2.update(dict1))
def count_dict(d):
    y_count = "${count}"
    # d_base = {'y_count': 1}
    if 'y_count' not in d:
        d['y_count'] = 1
    # d_base.update(d)
    n = {}
    l = []
    for i in range(1, d['y_count']+1):
        l.append(i)
    for i in range(1, d['y_count']+1):
        n = {}
        for k,v in d.items():
            if y_count in str(k):
                k = k.replace(y_count, str(i))
            if y_count in str(v):
                v = v.replace(y_count, str(i))
            n[k] = v
        n['y_count.index'] = i
        l[i-1]=n
    return l


d = {'acount-${count}': 5, 'one': '${count}-one', 'two': 'twoval', 'y_count': 3}
# d = {'acount-${count}': 5, 'one': '${count}-one', 'two': 'twoval'}
d = count_dict(d)
pprint(d)

More

import yaml
from yaml.resolver import *
from jinja2 import Template, Undefined

class NullUndefined(Undefined):
   def __getattr__(self, key):
     return ''

source = '''
{'y': {'a1': 'foo', 'a2': 'bar{{a1}}', 'a3': {'b1': 'bird', 'b2': 'red{{b1}}'}},
 'x': {'c1': 'turtle'}}
'''

def resolve_in_dict(loader, node):
  assert isinstance(node, yaml.MappingNode)
  values = loader.construct_mapping(node, deep=True)
  for key, value in values.items():
    if isinstance(value, str):
      t = Template(value, undefined=NullUndefined)
      values[key] = t.render(values)
  return values

yaml.SafeLoader.add_constructor(BaseResolver.DEFAULT_MAPPING_TAG, resolve_in_dict)

print(yaml.safe_load(source))

https://stackoverflow.com/questions/62963366/using-recursion-to-string-format-yaml-file