fileparse
fileparse is a package for reading the contents of a file and populating a data model with the information found.
Install
pip install fileparse-tobiasli
Usage
Say you have som text, and you have an idea of the structure of this text.
nested_text = """# This is a title.
This is contents.
And some more.
## This is a subtitle.
with subtitle contents.
# This is another title.
With some contents.
"""
You can then define some simple classes defining this content structure and patterns that match each content type. Finally we define a model.Finder, which allows us to search for the content type in the text file.
import re
from fileparse import parse, read
class Text(parse.Content):
pass
text_match = re.compile('^(?P<text>[^#].+)$')
text_finder = parse.ContentFinder(start_pattern=text_match,
content_type=Text)
class SubTitle(parse.Content):
pass
subtitle_match = re.compile('^## ?(?P<subtitle>[^#].+)$')
subtitle_finder = parse.ContentFinder(start_pattern=subtitle_match,
content_type=SubTitle,
sub_content_finders=[text_finder]
)
class Title(parse.Content):
pass
title_match = re.compile('^# ?(?P<title>[^#].+)$')
title_finder = parse.ContentFinder(start_pattern=title_match,
content_type=Title,
sub_content_finders=[subtitle_finder, text_finder])
Notice two things:
- The regex patterns are named capture groups. The named capture groups are added as property to their content type. I.e. a
SubTitleinstance will receive aSubTitle.subtitleproperty. Textcontent can be found within both aTitleand aSubTitle. And that aSubTitleonly can be found within aTitle.
Finally, we define the Parser.
file_finder = parse.Parser(finders=[title_finder])
The file_finder is now ready to parse text content.
For this specific content, we need a text stream able to parse a string. We define it like this:
stream = read.TextStream(reader=read.StringReader(string=nested_text))
We can now parse the text with the rules defined in file_finder, and se what comes out of it. To get information out of a file-object, use the file.get_contents_by_type(content_type) method.
file = file_finder.parse_stream(stream)
print(file.get_contents_by_type(SubTitle)[0].subtitle == 'This is a subtitle.')
print(file.get_contents_by_type(SubTitle)[0].contents[0].text == 'with subtitle contents.')
Happy parsing.
Metadata
Release files for fileparse-tobiasli 1.0.7
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 | |
|---|---|---|---|---|
| fileparse-tobiasli-1.0.7.tar.gz | 4.7 kB | Without provenance | Details |
Release files / fileparse-tobiasli-1.0.7.tar.gz
| Download URL | fileparse-tobiasli-1.0.7.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e24c54325209be2d7fb7db33d49ad5da2a09e5e0e9b428833274cbcf05b07ce1
|
|
BLAKE2b-256 checksum How to use checksums |
13c9f4e2c05c6bd37f53853ce3251dd8d53cbdb61aac586e309dba5085c858ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1
|
Security for fileparse-tobiasli 1.0.7
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.