-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·50 lines (36 loc) · 1.28 KB
/
utils.py
File metadata and controls
executable file
·50 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
__all__ = [
"read_data",
"get_lines",
"current_file",
"day_number",
]
import re
import os.path
from os import linesep
from pathlib import Path
from config import DATA_FOLDER
def read_data(filename: str, aoc_year: int = 2020, return_bytes: bool = False):
"""Function to read data file based on filename and AoC competition year."""
subpath = os.path.join(str(aoc_year), filename)
with open(DATA_FOLDER.joinpath(subpath), "rb") as f:
data = f.read()
if not return_bytes:
data = data.decode("utf-8")
return data
def get_lines(string: str, newline: str = "\n"):
"""Simple function to parse text document and split sections by newline."""
string = string.replace(linesep, newline)
return [i.strip() for i in string.split(newline) if len(i.strip()) > 0]
def current_file(specification):
"""Return the current filepath with filename as pathlib.Path object."""
return Path(os.path.abspath(specification))
def day_number(filename: str):
"""Return day number from a filepath string (e.g. - Path.stem, os.path.abspath()).
>>> day_number(Path(r"C:\\Users\day_1.txt").stem)
'1'
"""
return re.sub(r"\D", "", filename)
if __name__ == "__main__":
import doctest
doctest.testmod()
print(current_file())