Add linkutils

This commit is contained in:
Tristan Daniël Maat 2022-04-09 22:33:43 +01:00
parent 90b338945e
commit 65bf00f452
Signed by: tlater
GPG key ID: 49670FD774E43268
4 changed files with 30 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__/

View file

@ -15,6 +15,10 @@
pkgs = import nixpkgs {inherit system;};
in {
devShell = pkgs.mkShell {
shellHook = ''
export PYTHONPATH="$(pwd)"
'';
nativeBuildInputs = with pkgs; [
zip
unzip

0
utils/__init__.py Normal file
View file

25
utils/linkutils.py Normal file
View file

@ -0,0 +1,25 @@
"""Utility functions for handling links."""
import csv
from typing import List, NamedTuple, TextIO
class Link(NamedTuple):
"""A type for links - contains its url and date."""
url: str
date: str
def dump_links(links: List[Link], f: TextIO):
"""Dump links to a file in csv format."""
writer = csv.writer(f)
writer.writerow(["index", "link", "date"])
for i, link in enumerate(links):
writer.writerow([i, link[0], link[1]])
def read_links(f: TextIO) -> List[Link]:
"""Read links from a csv format."""
reader = csv.reader(f)
next(reader) # Skip the header
return [Link(link[1], link[2]) for link in reader]