You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,3 +16,59 @@ Varformat can format and un-format (parse) strings containing various styles of
16
16
'POSIX compliant dollar variables'
17
17
18
18
```
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:
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