A collection of convenient parsers for Advent of Code problems.
Project description
Advent of Code Parsers
A collection of convenient Python parsers for Advent of Code problems.
Installation
pip install aocp
Quickstart
You can import parsers from the base module. There are two main types of parsers:
- Iterable parsers, which return a sequence of elements from parsing a
str
, such aslist
,tuple
ordict
- Transform parsers, which return a single object from parsing a
str
, such as anint
,bool
or anotherstr
Iterable parsers can be composed with other parsers nested within, including Transform parsers and other Iterable parsers. They can also be nested with some base types such as int
.
Transform parsers cannot have nested parsers, but they can be composed with other parsers in a sequence using ChainParser
.
This way, the structure of the output data mirrors that of the expression used to instantiate the parser transform.
Here is a basic usage example:
raw_data = "46,79,77,45,57,34,44,13,32,88,86,82,91,97"
parser = ListParser(IntParser)
parser.parse(raw_data)
Which results in:
[46, 79, 77, 45, 57, 34, 44, 13, 32, 88, 86, 82, 91, 97]
And here is a more advanced example, from AoC 2021 day 4:
raw_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7"""
parser = TupleParser(
(
IntListParser(),
ListParser(ListParser(IntListParser())),
)
)
parser.parse(raw_data)
Which results in:
(
[7, 4, 9, 5, 11, 17, 23, 2, 0, 14, 21, 24, 10, 16, 13, 6, 15, 25, 12, 22, 18, 20, 8, 19, 3, 26, 1],
[[[22, 13, 17, 11, 0],
[8, 2, 23, 4, 24],
[21, 9, 14, 16, 7],
[6, 10, 3, 18, 5],
[1, 12, 20, 15, 19]],
[[3, 15, 0, 2, 22],
[9, 18, 13, 17, 5],
[19, 8, 7, 25, 23],
[20, 11, 10, 24, 4],
[14, 21, 16, 12, 6]],
[[14, 21, 17, 24, 4],
[10, 16, 15, 9, 19],
[18, 8, 23, 26, 20],
[22, 11, 13, 6, 5],
[2, 0, 12, 3, 7]]]
)
By default, the splitting elements in an iterable are guessed from the string provided. However, you can provide them through the splitter
argument. This can be a strings or a sequence of strings, which will all act as splitters.
A notebook with more examples from Aoc 2021 is available here.
To be done
- Full testing coverage
- Documentation page (full docstrings are available, though)
- More examples from other previous years
- Regex Parser
- Defaults in tuple parser in case of missing components in origin string
Source code
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aocp-1.0.0.tar.gz
.
File metadata
- Download URL: aocp-1.0.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.9.8 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28ca6503577f97e9af99fdde7e67e20e8fb065f8a96fad58af4b082b1da205b9 |
|
MD5 | 7a3b910d9d5459e946d54321ed8658ef |
|
BLAKE2b-256 | 698289e095f77f4e8b319b566a3d45e799407010766eaffe2fbe0a94f8de3909 |
File details
Details for the file aocp-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: aocp-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.9.8 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f164d4f35d7397005fdd086e175ead88885cd9602aae568f1c22df192e35f60d |
|
MD5 | 4b98a4db0284e90ee2d46838d145f265 |
|
BLAKE2b-256 | e51c3118c4af83210ff65c0b1eaa3b066baab1457265822a5750208898db7255 |