Skip to content

Instantly share code, notes, and snippets.

View delannoy's full-sized avatar
💭

Andres G. Delannoy delannoy

💭
View GitHub Profile
@delannoy
delannoy / spotify.py
Last active July 11, 2023 01:52
minimal wrapper for spotify API
#!/usr/bin/env python3
from __future__ import annotations
import json
import awkward
import pandas
import requests
# [Spotify Web API](https://developer.spotify.com/documentation/web-api/reference/)
@delannoy
delannoy / genProduction.sh
Last active July 19, 2022 12:55
vbfhn gen-production
#!/usr/bin/env bash
# [vbfhn - Notes of gen-production](https://docs.google.com/document/d/1_QVUkCEOtk3ZRdC1dl6DSHnPOGmct64vPnZTc0CuqEs/)
WORKDIR="${WORK:-${PWD}}"
MADGRAPHDIR="${WORKDIR}/GenProductions/bin/MadGraph5_aMCatNLO"
CARDPROCESS='SingleVectorLQ_M500'
CARDDIR="cards/production/2017/13TeV/SingleVectorLQ/PdfLo/${CARDPROCESS}"
GRIDPACK="${MADGRAPHDIR}/${CARDPROCESS}_slc7_amd64_gcc700_CMSSW_10_6_19_tarball.tar.xz"
@delannoy
delannoy / extractImagesPDF.py
Created February 15, 2022 22:04
Extract all Images from PDF (PyMuPDF, Pillow)
#!/usr/bin/env python3
# [Extract all Images from PDF in Python](https://aliarefwriorr.medium.com/extract-all-images-from-pdf-in-python-cda3dc195abd)
import io
import fitz
import os
import PIL.Image
import requests
@delannoy
delannoy / AutoCorrect.py
Last active November 16, 2021 19:20
parse Wikipedia's list of common misspellings
#!/usr/bin/env python3
from lxml.html import fromstring
from pandas import concat, DataFrame, Series
from requests import get
from string import ascii_uppercase
def parseListOfCommonMisspellings(char:str) -> DataFrame:
'''Scrape and parse [Wikipedia's list of common misspellings](https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings) and return as a pandas.DataFrame'''
url = f'https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/{char}'
@delannoy
delannoy / juliaUnicode.py
Last active September 17, 2023 04:25
Writes AutoHotkey hotkey-based script from JuliaLang tab completions for Unicode characters
#!/usr/bin/env python3
# Writes AutoHotkey hotkey-based script from JuliaLang tab completions for Unicode characters
# Inspired by:
# [I created a (Linux) script to easily type Unicode math everywhere](https://www.reddit.com/r/Physics/comments/pc08mg/i_created_a_linux_script_to_easily_type_unicode/)
from lxml.html import fromstring
from numpy import array_split
from requests import get
from typing import TextIO
@delannoy
delannoy / slackAPI.py
Last active May 26, 2021 15:29
Pandas wrapper for slack API
#!/usr/bin/env python3
import json, os, pandas, requests, sys, typing
def getToken(jsonFilePath:str) -> str:
'''Read "token" key from json file'''
try:
with open(jsonFilePath, 'r') as jsonFile: token = json.load(jsonFile).get('token')
if token == 'api_key': raise Exception
except:
@delannoy
delannoy / brilcalcDF.py
Last active April 17, 2021 01:56
Return arbitrary `brilcalc lumi` query as a pandas.DataFrame
#!/usr/bin/env python3
import os
import pandas
import shlex
def brilcalcLumiQuery(outputstyle:str='csv', cerntime:bool=False, tssec:bool=True, unit:str='/fb', **kwargs) -> str:
'''
Format brilcalc query string. Keyword arguments are expected to specify the query.
See [https://cms-service-lumi.web.cern.ch/cms-service-lumi/brilwsdoc.html#brilcalc] for more info.
@delannoy
delannoy / color.py
Created March 23, 2021 03:20
Python color with ANSI escape sequences (via tput)
#!/usr/bin/env python3
class C:
# [https://stackoverflow.com/a/287944/13019084]
# [https://janakiev.com/blog/python-shell-commands/]
# [https://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html]
reset = os.popen('tput sgr 0 0').read() # Turn off all attributes
bold = os.popen('tput bold').read() # Begin double intensity mode
uline = os.popen('tput smul').read() # Begin underscore mode
class F: # foreground
@delannoy
delannoy / vegaDatasets.py
Last active March 21, 2021 17:11
Return vega dataset as a pandas.DataFrame. Parse and print available datasets if none are provided.
#!/usr/bin/env python3
# [https://github.com/vega/vega-datasets]
import lxml.html, lxml.cssselect, pandas, requests
# [https://lxml.de/] [https://lxml.de/cssselect.html]
def vegaDatasets(dataset:str=None) -> pandas.DataFrame:
def checkExt(dataset:str, ext:str): return dataset.split('.')[-1] == ext
def availableDatasets(url:str):
@delannoy
delannoy / args.sh
Last active June 5, 2021 10:17
BASH metafunction to handle assignment of postional parameters (arguments). Prints usage and breaks if required arguments are not provided. Supports optional arguments with default values.
#!/usr/bin/env bash
Args(){
# Metafunction that expects "$FUNCNAME" (name of parent function), the positional parameters passed to a parent function, an ASCII "record separator" delimiter, and the variable names for the required & optional arguments
# Variable names for optional arguments should be indicated by wrapping them in square brackets. A default value for optional arguments may be indicated by an equal sign, e.g. '[optionalVariable=defaultValue]'
# The passed positional parameters are assigned to the variable names and returned as global variables using `printf -v` (optional positional parameters may be skipped with a quoted empty string, '')
# If any required parameters are missing, printUsage() is executed (prints the expected positional parameters) and an exit code of 111 is returned
local functionName posParam varName sqRegex eqRegex red reset # [https://wiki.bash-hackers.org/commands/builtin/local]
functionName="$1"; shift # [https://wiki.bash-hackers.org/syntax/shellvar