Add linkutils
This commit is contained in:
parent
90b338945e
commit
65bf00f452
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
__pycache__/
|
|
@ -15,6 +15,10 @@
|
||||||
pkgs = import nixpkgs {inherit system;};
|
pkgs = import nixpkgs {inherit system;};
|
||||||
in {
|
in {
|
||||||
devShell = pkgs.mkShell {
|
devShell = pkgs.mkShell {
|
||||||
|
shellHook = ''
|
||||||
|
export PYTHONPATH="$(pwd)"
|
||||||
|
'';
|
||||||
|
|
||||||
nativeBuildInputs = with pkgs; [
|
nativeBuildInputs = with pkgs; [
|
||||||
zip
|
zip
|
||||||
unzip
|
unzip
|
||||||
|
|
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
25
utils/linkutils.py
Normal file
25
utils/linkutils.py
Normal 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]
|
Loading…
Reference in a new issue