Skip to content

Commit 4bc8859

Browse files
committed
Added Usage and Examples section.
1 parent ee4630d commit 4bc8859

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,63 @@
11
# valve-keyvalues-python
2-
Python class for manipulation with Valve KeyValues files (VDF format). Provides the parsing of VDF files to objects with dict interface, editing of this object keys and values and writing of any object with dict interface to VDF file format.
2+
Python class for manipulation with Valve KeyValues files ([VDF format](https://developer.valvesoftware.com/wiki/KeyValues)). Provides the parsing of VDF files to objects with dict interface, editing of this object keys and values and writing of any object with dict interface to VDF file.
33

44
# Installation
55
Just copy the `valve-keyvalues-python` folder wherever you need, for example to your Python's site-packages: `<Python>/Lib/site-packages/` and then inside your program import the class by `from valve-keyvalues-python import KeyValues`
66

77
**Requires Python 3!**
88

99
# Usage
10+
### Instantiation
1011

11-
TODO
12+
To create **empty** `KeyValues` instance:
13+
14+
```python
15+
kv = KeyValues()
16+
```
17+
18+
To create `KeyValues` instance **from VDF file**:
19+
20+
```python
21+
kv = KeyValues(filename="")
22+
```
23+
24+
When you create `KeyValues` instance from VDF file you can specify these optional parameters:
25+
26+
* `encoding=""` - input VDF file encoding. Default: `utf-8`
27+
* `mapper_type=` - mapper type for storing KeyValues. It must have the `dict` interface, i.e. allow to do `mapper[key] = value` operations. For example you can use the `dict` type. Instance's attribute. Default: `collections.OrderedDict` (stores the keys in the order they have been added)
28+
* `key_modifier=` - functions for modifying the key before its additions. For example `key_modifier=str.lower` will make all the keys to be lowercase. Instance's attribute. Default: `None`
29+
30+
To create `KeyValues` instance **from your own object with `dict` interface** (and instance's attribute `mapper_type` will be set to type of passed `mapper=` object):
31+
32+
```python
33+
kv = KeyValues(mapper=)
34+
```
35+
36+
All these instantiation variants have common optional parameter `key_sorter=`. It's a sorting function which will be applied to keys when you use methods `dump()` or `write()`. For example you can use `key_sorter=sorted` and keys will be represented in alphabetical order; `key_sorter=reversed` for reverse order. Instance's attribute. Default: `None`
37+
38+
### Methods
39+
* `parse(filename)` - parses the VDF file to `dict` interface representation, i.e. KeyValues can be accessed and modified by `mapper[key] = value` operations. Optional arguments:
40+
* `encoding=""` - input VDF file encoding. Default: `utf-8`
41+
* `mapper_type=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `mapper_type`. Default: `collections.OrderedDict` (stores the keys in the order they have been added)
42+
* `key_modifier=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_modifier`. Default: `None`
43+
44+
* `dump()` - returns the string representation of stored KeyValues, i.e. string in correct VDF format. Optional parameters:
45+
* `mapper=` - dumps the passed object (it must have the `dict` interface!). Default: `None`
46+
* `key_sorter=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_sorter`. Default: `None`
47+
48+
* `write(filename)` - writes the dump to file. Optional parameters:
49+
* `encoding=""` - output file encoding. Default: `utf-8`
50+
* `mapper=` - writes the dump of passed object (it must have the `dict` interface!). Default: `None`
51+
* `key_sorter=` - [see Instantiation section](README.md#instantiation). This will override the instance's attribute `key_sorter`. Default: `None`
52+
53+
Of course the class KeyValues also provides the standard `dict` interface methods like `keys()` or `key in d`. [See dict](https://docs.python.org/3.5/library/stdtypes.html#dict)
54+
55+
*Note*: when you `print(kv)` your KeyValues instance, it's in fact a shortcut for `print(kv.dump())` method (because magic method `__str__()` is defined that way).
56+
57+
# [Examples - here](examples/)
58+
59+
*example_01.py* - basic operations
60+
61+
*example_02.py* - using the optional functions
62+
63+
*example_03.py* - "advanced" uses

0 commit comments

Comments
 (0)