Skip to content

Instantly share code, notes, and snippets.

View decoupca's full-sized avatar

Austin de Coup-Crank decoupca

  • Moody's Corp
  • Staples, MN
View GitHub Profile
@decoupca
decoupca / save_excel.py
Created July 10, 2023 18:19
Save a list of Pandas DataFrames to a multi-sheet Excel workbook.
import pandas as pd
def save_excel(
data: list[pd.DataFrame],
filename: str,
sheet_names: list[str],
*,
freeze_top_row: bool,
auto_fit_columns: bool,
sort_by: Optional[Union[str, list[str]]] = None,
@decoupca
decoupca / authentication.py
Created May 31, 2023 15:14
NetBox Example: Multiple AD LDAP authentication backends
import ldap
from django_auth_ldap.config import LDAPGroupQuery, LDAPSearch, GroupOfNamesType
from django_auth_ldap.backend import LDAPSettings
from netbox.authentication import NBLDAPBackend
"""
1. Save as /opt/netbox/local/authentication.py
2. Add the following config in /opt/netbox/netbox/netbox/configuration.py:
REMOTE_AUTH_BACKEND = ('local.authentication.ActiveDirectory1', 'local.authentication.ActiveDirectory2')
@decoupca
decoupca / custom_validators.py
Created September 20, 2022 20:48
NetBox custom validator for requiring a primary IPv4 when device set to active or staged
from extras.validators import CustomValidator
import re
class PrimaryIP4Validation(CustomValidator):
"""Require primary IPv4 value when status is set to active or staged"""
def validate(self, device):
# only these roles will enforce a primary IP when set to active.
# implicitly excludes non-network devices like patch panels and storage drawers.
@decoupca
decoupca / thread_progress.py
Created August 10, 2022 15:44
Run a task in parallel with progress - ThreadPoolExecutor with TQDM
from typing import Callable, Iterable, Dict, List
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
def thread_progress(
worker: Callable,
items: Iterable,
threads: int = None,
result_handler: Callable = None,
worker_kwargs: Dict = None,