Skip to main content

Library for working with Valve's VDF text format

Project description

Latest version released on PyPi MIT License Test coverage Scrutinizer score Build status of master branch

Pure python module for (de)serialization to and from VDF that works just like json.

Tested and works on python2.7, python3.3+, pypy and pypy3.

VDF is Valve’s KeyValue text file format

https://developer.valvesoftware.com/wiki/KeyValues

Supported versions: kv1
Unsupported: kv2 and kv3

Install

You can grab the latest release from https://pypi.python.org/pypi/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 due to hash randomization. If key order is important then 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vdf-2.5.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vdf-2.5-py2.py3-none-any.whl (11.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file vdf-2.5.tar.gz.

File metadata

  • Download URL: vdf-2.5.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.8.1 pkginfo/1.4.1 requests/2.13.0 setuptools/34.3.2 requests-toolbelt/0.7.1 clint/0.5.1 CPython/2.7.13 CYGWIN_NT-10.0/2.7.0(0.306/5/3)

File hashes

Hashes for vdf-2.5.tar.gz
Algorithm Hash digest
SHA256 d8d6329ec45347576576f79f0831334aae5fbf1a0d235d3eb9e2892018c55bf3
MD5 56b25e9193f72ac2cb9c70cac4d114a5
BLAKE2b-256 720ea98a25f20e9ef388b09308cf267815f61991df942b45612948d15a02fa83

See more details on using hashes here.

File details

Details for the file vdf-2.5-py2.py3-none-any.whl.

File metadata

  • Download URL: vdf-2.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.8.1 pkginfo/1.4.1 requests/2.13.0 setuptools/34.3.2 requests-toolbelt/0.7.1 clint/0.5.1 CPython/2.7.13 CYGWIN_NT-10.0/2.7.0(0.306/5/3)

File hashes

Hashes for vdf-2.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e521ef9c0cf167bfc574648e9a16228b2877bfc0076b4945cf1c0e850e03a4d3
MD5 3283dfdf0a8c297350c26a4d9709fc3a
BLAKE2b-256 46bc8b7b9b8e5e24a844af011a5fe83995d0255adef9ccaf911cc512f8609650

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page