Skip to content

Instantly share code, notes, and snippets.

@k-saka
Created December 8, 2016 08:30
Show Gist options
  • Save k-saka/4451eeae35b0cbd99eb60ab9041a0a49 to your computer and use it in GitHub Desktop.
Save k-saka/4451eeae35b0cbd99eb60ab9041a0a49 to your computer and use it in GitHub Desktop.
from pathlib import Path
from typing import Iterable, Iterator
import tempfile
import shutil
_basepath = Path("")
def glob_files() -> Iterator[Path]:
files = _basepath.rglob("*.py")
return files
def annotate_init(pyfile: Path) -> None:
with tempfile.NamedTemporaryFile("w") as tmp:
with pyfile.open("r") as f:
for line in f:
if "def __init__(" in line and line.endswith(":\n") and "-> None:" not in line:
print(line)
line = line.replace("):", ") -> None:")
print(line)
tmp.write(line)
tmp.flush()
shutil.copy2(tmp.name, str(pyfile))
# print(tmp.name, str(pyfile))
def annotate(files: Iterable[Path]):
for pyfile in files:
annotate_init(pyfile)
files = glob_files()
annotate(files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment