Pure python module for (de)serialization to and from VDF that works just like json.
Tested and works on py2.7, py3.3+, pypy and pypy3.
VDF is Valve’s KeyValue text file format
https://developer.valvesoftware.com/wiki/KeyValues
Install
You can grab the latest release from https://pypi.org/project/vdf/ or via pip
pip install vdf
Install the current dev version from github
pip install git+https://github.com/ValvePython/vdf
Problems & solutions
There are known files that contain duplicate keys. This is supported the format and makes mapping to dict impossible. For this case the module provides vdf.VDFDict that can be used as mapper instead of dict. See the example section for details.
By default de-serialization will return a dict, which doesn’t preserve nor guarantee key order on Python versions prior to 3.6, due to hash randomization. If key order is important on old Pythons, I suggest using collections.OrderedDict, or vdf.VDFDict.
Example usage
For text representation
import vdf
# parsing vdf from file or string
d = vdf.load(open('file.txt'))
d = vdf.loads(vdf_text)
d = vdf.parse(open('file.txt'))
d = vdf.parse(vdf_text)
# dumping dict as vdf to string
vdf_text = vdf.dumps(d)
indented_vdf = vdf.dumps(d, pretty=True)
# dumping dict as vdf to file
vdf.dump(d, open('file2.txt','w'), pretty=True)
For binary representation
d = vdf.binary_loads(vdf_bytes)
b = vdf.binary_dumps(d)
# alternative format - VBKV
d = vdf.binary_loads(vdf_bytes, alt_format=True)
b = vdf.binary_dumps(d, alt_format=True)
# VBKV with header and CRC checking
d = vdf.vbkv_loads(vbkv_bytes)
b = vdf.vbkv_dumps(d)
Using an alternative mapper
d = vdf.loads(vdf_string, mapper=collections.OrderedDict)
d = vdf.loads(vdf_string, mapper=vdf.VDFDict)
VDFDict works much like the regular dict, except it handles duplicates and remembers insert order. Additionally, keys can only be of type str. The most important difference is that when trying to assigning a key that already exist it will create a duplicate instead of reassign the value to the existing key.
>>> d = vdf.VDFDict()
>>> d['key'] = 111
>>> d['key'] = 222
>>> d
VDFDict([('key', 111), ('key', 222)])
>>> d.items()
[('key', 111), ('key', 222)]
>>> d['key']
111
>>> d[(0, 'key')] # get the first duplicate
111
>>> d[(1, 'key')] # get the second duplicate
222
>>> d.get_all_for('key')
[111, 222]
>>> d[(1, 'key')] = 123 # reassign specific duplicate
>>> d.get_all_for('key')
[111, 123]
>>> d['key'] = 333
>>> d.get_all_for('key')
[111, 123, 333]
>>> del d[(1, 'key')]
>>> d.get_all_for('key')
[111, 333]
>>> d[(1, 'key')]
333
>>> print vdf.dumps(d)
"key" "111"
"key" "333"
>>> d.has_duplicates()
True
>>> d.remove_all_for('key')
>>> len(d)
0
>>> d.has_duplicates()
False
Metadata
Release files for vdf 3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| vdf-3.4.tar.gz | 11.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| vdf-3.4-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 21.5 kB
Release files / vdf-3.4.tar.gz
| Download URL | vdf-3.4.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd5419f41e07a1009e5ffd027c7dcbe43d1f7e8ef453aeaa90d9d04b807de2af
|
|
BLAKE2b-256 checksum How to use checksums |
447f74192f47d67c8bf3c47bf0d8487b3457614c2c98d58b6617721d217f3f79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9
|
Release files / vdf-3.4-py2.py3-none-any.whl
| Download URL | vdf-3.4-py2.py3-none-any.whl |
|---|---|
| Size | 10.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
68c1a125cc49e343d535af2dd25074e9cb0908c6607f073947c4a04bbe234534
|
|
BLAKE2b-256 checksum How to use checksums |
96606456b687cf55cf60020dcd01f9bc51561c3cc84f05fd8e0feb71ce60f894
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9
|
Security for vdf 3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.