Skip to content

Commit ca1de74

Browse files
committed
Update README
1 parent edd889c commit ca1de74

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,59 @@ Varformat can format and un-format (parse) strings containing various styles of
1616
'POSIX compliant dollar variables'
1717

1818
```
19+
20+
## Getting Started
21+
Varformat is available to install via pip:
22+
```
23+
pip install varformat
24+
```
25+
26+
When installed, the modules `varformat` and `varformat.formats` will be available. Global functions `format`, `vformat`, and `parse` represent the default formmatter with a `${}` style:
27+
```python
28+
>>> import varformat as vf
29+
>>> vf.format("my name ${name}", name="jeff")
30+
'my name jeff'
31+
32+
```
33+
34+
If it is necessary to specify keys which are not valid python identifiers, such as numbers or string with spaces, you can use `vformat` instead:
35+
```python
36+
>>> import varformat as vf
37+
>>> vf.vformat("My three favorite foods: ${1}, ${2}, and ${1} again",
38+
... {"1": "pizza", "2": "chocolate"})
39+
'My three favorite foods: pizza, chocolate, and pizza again'
40+
41+
```
42+
43+
`vformat` also supports keyword arguments to customize formatting behavior. `partial_ok` (default `False`) and `extra_ok` (default: `True`) control whether it is allowed to provide less (or more) arguments than the format string requires. `ambiguity_check` (default: `False`) will raise an error if your resulting string will be ambiguous:
44+
```python
45+
>>> import varformat as vf
46+
>>> vf.vformat("package-${os}-${arch}", {"os": "ubuntu-22.04", "arch": "amd64"}, ambiguity_check=True)
47+
Traceback (most recent call last):
48+
...
49+
varformat.AmbiguityError: refusing to format because parsing would be ambiguous:
50+
could be: {'os': 'ubuntu-22.04', 'arch': 'amd64'}
51+
or: {'os': 'ubuntu', 'arch': '22.04-amd64'}
52+
53+
```
54+
55+
The `parse` function, which performs the inverse of `vformat`, also supports `ambiguity_check` (default: `True`):
56+
```python
57+
>>> import varformat as vf
58+
>>> vf.parse("package-${os}-${arch}", "package-ubuntu-22.04-amd64")
59+
Traceback (most recent call last):
60+
...
61+
varformat.AmbiguityError: parsing is ambiguous:
62+
could be: {'os': 'ubuntu-22.04', 'arch': 'amd64'}
63+
or: {'os': 'ubuntu', 'arch': '22.04-amd64'}
64+
65+
```
66+
67+
You can of course set `ambiguity_check` to `False`, and `parse` will parse using the regular expression rules (greedily).
68+
69+
### Other formatters
70+
Module `varformat.formats` contains formatters with other syntaxes:
71+
- `varformat.formats.posix_shell` follows POSIX shell variable rules: it disallows numeric identifiers, identifiers with spaces, but allows referencing variables like `$var` in addition to `${var}`;
72+
- `varformat.formats.python` follows classic python format string rules (e.g. `{var}`).
73+
74+
You can define your own formatter with your own custom syntax by subclassing either `varformat.RegexFormatter` and defining a regular expression that detects placeholders, or `varformat.AbstractFormatter` and defining a parsing function. See class docstrings for more information.

0 commit comments

Comments
 (0)