KeyValues are defined recursively as sections and pairs of named keys and values. See the VDC Article on KeyValues
Creates a new KeyValues object from a file or file-like object.
| Parameters: |
|
|---|---|
| Return type: | |
| Returns: | A valid KeyValues object on success, None otherwise. |
KeyValues objects are essentially dictionaries, and thus are accessed in the same way. They can be instantiated by passing a dictionary, which is then transformed behind the scenes into a Valve KeyValues object.:
from sourcemod.keyvalues import KeyValues
kv = KeyValues('root_section_name', {
'key1': 'value1',
'subsection1': {
'subkey1': 'subvalue1',
'subkey2': 2,
'subkey3': 4.7123
}
})
>>> kv['key1']
'value1'
>>> kv['subsection1']
subsection1
{
"subkey1" "subvalue1"
"subkey2" "2"
"subkey3" "4.7123"
}
>>> kv['subsection1']['subkey2']
2
>>> kv['subsection1']['subkey2'] = 83
83
>>> kv['subsection1']
subsection1
{
"subkey1" "subvalue1"
"subkey2" "83"
"subkey3" "4.7123"
}
With no arguments, removes all sub-keys. With key, this clears the value of kv[key], turning it into an empty section. If key is already a section, this is the same as kv[key].clear()
| Parameter: | key (str) – The name of the key to clear. |
|---|
Deep copies the current KeyValues into a completely new KeyValues.
| Return type: | KeyValues |
|---|---|
| Returns: | A new KeyValues object with the same structure as this KeyValues. |
Parses a KeyValues structure from a string into the KeyValues object.
| Parameter: | string (str) – The string value to parse |
|---|
NOT IMPLEMENTED YET
Save this KeyValues to a file
| Parameter: | file (str or file) – A filename to save to, or a file-like object (must have a write() method) |
|---|---|
| Return type: | bool |
| Returns: | True on successful save, False otherwise. |
The KeyValues object supports the dict object protocol, so almost everything you can do with a dict, you can do with a KeyValues.