various improvements

This commit is contained in:
ge 2023-11-09 01:20:47 +03:00
parent 1dd3e9a720
commit 64bdbbd97b
4 changed files with 0 additions and 89 deletions

View File

@ -1,25 +0,0 @@
from collections import UserDict
from typing import Any
class _NotPresent:
"""
Type for representing non-existent dictionary keys.
See :class:`_FillableDict`
"""
class _FillableDict(UserDict):
"""Use :method:`fill` to add key if not present."""
def __init__(self, data: dict):
self.data = data
def fill(self, key: str, value: Any) -> None:
if self.data.get(key, _NotPresent) is _NotPresent:
self.data[key] = value
d = _FillableDict({'a': None, 'b': 'BBBB'})
d.fill('c', 'CCCCCCCCC')
d.fill('a', 'CCCCCCCCC')
d['a'].fill('gg', 'AAAAAAAA')
print(d)

View File

@ -1,10 +0,0 @@
title: dev-1
vcpus: 4
memory: 4096
volumes:
- is_system: true
type: file
target: vda
capacity:
value: 5
unit: GiB

34
pars.py
View File

@ -1,34 +0,0 @@
import re
def _split_unit(val: str) -> dict | None:
match = re.match(r'([0-9]+)([a-z]+)', val, re.I)
if match:
return {
'value': match.groups()[0],
'unit': match.groups()[1],
}
return None
def _parse_complex_arg(arg: str) -> dict:
# key=value --> {'key': 'value'}
if re.match(r'.+=.+', arg):
key, val = arg.split('=')
# system --> {'is_system': True}
# ro --> {'is_readonly': True}
elif re.match(r'^[a-z0-9_\.\-]+$', arg, re.I):
key = 'is_' + arg.replace('ro', 'readonly')
val = True
else:
raise ValueError('Invalid argument pattern')
# key=15GiB --> {'key': {'value': 15, 'unit': 'GiB'}}
if not isinstance(val, bool):
val = _split_unit(val) or val
return {key: val}
print(_parse_complex_arg('source=/volumes/50c4410b-2ef0-4ffd-a2e5-04f0212772d4.qcow2'))
print(_parse_complex_arg('capacity=15GiB'))
print(_parse_complex_arg('system'))
print(_parse_complex_arg('cpu.cores=8'))

20
pd.py
View File

@ -1,20 +0,0 @@
from pydantic import BaseModel, Extra
class EntityModel(BaseModel):
"""Basic entity model."""
aaa: int
bbb: int
class Config:
extra = Extra.forbid
class Some(EntityModel):
ooo: str
www: str
a = Some(ooo='dsda', www='wcd', sds=1)
print(a)