Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
script="run.sh"
function example {
# specifies a script usage example
echo -e "example: sh $script -n Louis -b 20210709 -a 27 -l -d"
}
function usage {
minimum_pre_commit_version: 2.13.0
default_stages: [commit, push]
exclude: '^$'
default_language_version:
python: python3.7
repos:
- repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
with open(script, "r") as file:
script_lines = file.readlines()
function_docs.sort(key=lambda x: x[0], reverse=True)
for (idx, docstring_attr) in function_docs:
script_lines = (
script_lines[: docstring_attr["begin_lineno"] - 1]
+ [f'{docstring_attr["value"]}\n']
+ script_lines[docstring_attr["end_lineno"] :]
if type_hints:
docstring = f'"""{ast.get_docstring(child, clean=True)}\n"""'
docstring_lines = docstring.split("\n")
if docstring:
args = re.search(
r'Args:(.*?)(Example[s]?:|Return[s]?:|""")',
docstring,
from automate import docstring_from_type_hints
function_argument_type_hints = get_type_hints(docstring_from_type_hints)
function_return_argument_hint = type_hints.pop("return", None)
print(function_argument_type_hints)
>>> {'repo_dir': <class 'pathlib.Path'>, 'overwrite_script': <class 'bool'>, 'test': <class 'bool'>, 'return': <class 'str'>}
print(function_return_argument_hint)
>>> <class 'str'>
import ast
import astpretty
import importlib
script = pathlib.Path("path_to_a_python_file")
with open(script, "r") as source:
tree = ast.parse(source.read())
import pandas as pd
from typing import Dict, List, Optional, Sequence, Tuple, Union
class DataProcessor:
"""Read and process data."""
@classmethod
def train_test_sets(
cls,
train_paths: Sequence[Union[Path, str]],
@LouisdeBruijn
LouisdeBruijn / .pre-commit-config_docs.yaml
Last active May 31, 2021 13:29
pre-commit hooks for docstrings documentation and MkDocs build
- id: automate_functions
name: Add functions to mkgendocs configurations YAML
entry: bash -c "python3 Python\ tips/automate.py; git add -u" --
language: system
- id: mkgendocs
name: Generate markdown from docstrings
entry: bash -c "gendocs --config 'mkgendocs.yml'; git add -u" --
language: system
- id: mkbuild
name: Build the markdown documentation, run with '$ mkdocs serve' at http://127.0.0.1:8000/
mkdocs==1.1.2 # static site generator for documentation in Markdown
mkdocs-material==7.1.5 # Material support for MkDocs
mkdocs-minify-plugin==0.4.0 # strips whitespaces in HTML markdown documentation pages
mkdocs-git-revision-date-localized-plugin==0.9.2 # displays the date of the last git modification of a markdown page
mkdocs-git-authors-plugin==0.3.3 # displays git authors of a markdown page
mkdocs-table-reader-plugin==0.5 # enables automatic CSV imports as a table into a markdown page
mkdocs-img2fig-plugin==0.9.3 # converts markdown encoded images to HTML figures
mknotebooks==0.7.0 # enables inclusion of Jupyter notebook in markdown page
mkdocs-awesome-pages-plugin==2.5.0 # enables naive configuration of page titles and their order
mkdocs-enumerate-headings-plugin==0.4.4 # enumerates headings across markdown pages
@LouisdeBruijn
LouisdeBruijn / .pre-commit-config.yaml
Last active January 8, 2024 09:33
autoflake, isort, black and flake8 pre-commits
repos:
- repo: local
hooks:
- id: autoflake
name: Remove unused variables and imports
entry: bash -c 'autoflake "$@"; git add -u' -- # This runs autoflake and stages the reformatted python files before running autoflake again, which should then "pass"
language: python
args: ["--in-place", "--remove-all-unused-imports", "--remove-unused-variables", "--expand-star-imports", "--ignore-init-module-imports"]
files: \.py$
- id: isort