visitor
A tiny library to facilitate visitor implementation in Python (which are slightly peculiar due to dynamic typing). In fact, it is so small, you may just be better off copy & pasting the source straight into your project…
Example use
A simple JSON-encoder:
from visitor import Visitor
class JSONEncoder(Visitor):
def __init__(self):
self.indent = 0
def escape_str(self, s):
# note: this is not a good escape function, do not use this in
# production!
s = s.replace('\\', '\\\\')
s = s.replace('"', '\\"')
return '"' + s + '"'
def visit_list(self, node):
self.indent += 1
s = '[\n' + ' ' * self.indent
s += (',\n' + ' ' * self.indent).join(self.visit(item)
for item in node)
self.indent -= 1
s += '\n' + ' ' * self.indent + ']'
return s
def visit_str(self, node):
return self.escape_str(node)
def visit_int(self, node):
return str(node)
def visit_bool(self, node):
return 'true' if node else 'false'
def visit_dict(self, node):
self.indent += 1
s = '{\n' + ' ' * self.indent
s += (',\n' + ' ' * self.indent).join(
'{}: {}'.format(self.escape_str(key), self.visit(value))
for key, value in sorted(node.items())
)
self.indent -= 1
s += '\n' + ' ' * self.indent + '}'
return s
data = [
'List', 'of', 42, 'items', True, {
'sub1': 'some string',
'sub2': {
'sub2sub1': False,
'sub2sub2': 123,
}
}
]
print(JSONEncoder().visit(data))
Output:
[
"List",
"of",
42,
"items",
true,
{
"sub1": "some string",
"sub2": {
"sub2sub1": false,
"sub2sub2": 123
}
}
]
Metadata
Release files for visitor 0.1.3
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 | |
|---|---|---|---|---|
| visitor-0.1.3.tar.gz | 3.3 kB | Without provenance | Details |
Release files / visitor-0.1.3.tar.gz
| Download URL | visitor-0.1.3.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c737903b2b6864ebc6167eef7cf3b997126f1aa94bdf590f90f1436d23e480a
|
|
BLAKE2b-256 checksum How to use checksums |
d758785fcd6de4210049da5fafe62301b197f044f3835393594be368547142b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for visitor 0.1.3
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.