sourcemod.keyvalues — Valve KeyValues

KeyValues are defined recursively as sections and pairs of named keys and values. See the VDC Article on KeyValues

Module Contents

keyvalues_from_file(file[, use_escape_sequences=False])

Creates a new KeyValues object from a file or file-like object.

Parameters:
  • file (str/file) – If a string, creates a new KeyValues object from the filename stored in the string. Otherwise it must be a file-like object, in that it must have a read() function.
  • use_escape_sequences (bool) – Whether or not to parse escape sequences such as \n or \\
Return type:

KeyValues

Returns:

A valid KeyValues object on success, None otherwise.

KeyValues Objects

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"
}

Attributes and Methods

KeyValues.name

The root section name of this KeyValues.

KeyValues.uses_escape_sequences

Whether or not to parse escape sequences such as \n or \\

KeyValues.clear([key])

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()

Parameters:
  • key (str) – The name of the key to clear.
KeyValues.copy()

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.
KeyValues.parse(string)

Parses a KeyValues structure from a string into the KeyValues object.

Parameters:
  • string (str) – The string value to parse
KeyValues.save(file)

Save this KeyValues to a file

Parameters:
  • file (str or file) – A file object to save to.

The KeyValues object supports the dictionary object protocol, so almost everything you can do with a dict, you can do with a KeyValues.

KeyValues.iteritems()
KeyValues.iterkeys()
KeyValues.itervalues()

Iterators for items (key and value in a tuple), keys, or values.

KeyValues.items()
KeyValues.keys()
KeyValues.values()

Returns a list of items (key and value in a tuple), keys, or values.

KeyValues.get(key[, default=None])

If key exists in the KeyValues, this returns that value. If it does not exist, it returns what is passed to default. The benefit of this function is that it never raises a KeyError.

len(kv)

Returns the number of items in the KeyValues kv

kv[key]

Returns the value of key key from KeyValues kv

kv[key] = value

Set kv[key] to value.

del kv[key]

Remove kv[key] from kv. Raises a KeyError if key is not in the KeyValues.

key in kv

Return True if kv has a key key, else False

key not in kv

Equivalent to not key in kv

iter(kv)

Return an iterator over the keys of the KeyValues. This is a shortcut for iterkeys

Table Of Contents

Previous topic

sourcemod.halflife — Engine Interaction

This Page