Skip to content

Instantly share code, notes, and snippets.

View jlumbroso's full-sized avatar

Jérémie Lumbroso jlumbroso

View GitHub Profile
@jlumbroso
jlumbroso / genanki-media-test.py
Created September 23, 2018 04:55
Minimal example to build an Anki deck with an image, with genanki
import genanki
my_model = genanki.Model(
1380120064,
'Example',
fields=[
{'name': 'Object'},
{'name': 'Image'},
],
templates=[
@jlumbroso
jlumbroso / genanki-media-test.py
Created September 23, 2018 14:23
Minimal example of how to create a working Anki deck with a media file embedded.
import genanki
my_model = genanki.Model(
1380120064,
'Example',
fields=[
{'name': 'Object'},
{'name': 'Image'},
],
templates=[
@jlumbroso
jlumbroso / SimpleColorLogging.py
Created April 21, 2019 22:49
Short snippet showing how to have colored terminal logging output in Python.
import os
import logging
class _Color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
@jlumbroso
jlumbroso / MissingFormatKey.py
Created July 5, 2019 02:41
Python method to format a string with missing keys.
"""
MissingFormatKey.py
Author: Jérémie Lumbroso <lumbroso@cs.princeton.edu>
Date: 2019-07-03
Usage:
# Define replacement f"..." compatible with Python 2 and 3
_f = _make_f(globals=lambda: globals(), locals=lambda: locals())
@jlumbroso
jlumbroso / clevercsv_probe_sniff.py
Last active May 19, 2020 15:40
Helper code for the `clevercsv` package to help sniff the dialect of very large files.
"""
Helper code for the `clevercsv` package to help sniff the dialect of very large files.
Author: lumbroso@cs.princeton.edu
Date: 2020-05-19
"""
import clevercsv
THRESHOLD_WHOLE_FILE = 10000
@jlumbroso
jlumbroso / pandas_question_time_gaps.py
Created August 15, 2020 20:38
Minimum reproducible code for a Pandas question on how to plot time series with missing data
import random
import pandas as pd
# Generate a random time series
def gapped_time_series(start=None, span=datetime.timedelta(days=30)):
if start is None:
start = datetime.datetime.now()
series = []
@jlumbroso
jlumbroso / loguru_call_decorator.py
Created November 16, 2020 04:43
Decorator to log function calls with loguru in Python (written for the https://github.com/jlumbroso/restsdk project)
# Snippet licensed under The Unlicense (https://unlicense.org/)
# Originally part of https://github.com/jlumbroso/restsdk (which is LGPLv3 licensed)
import functools
import inspect
import os
import sys
import time
import typing
@jlumbroso
jlumbroso / pyppeteer_spa_screenshot_example.py
Created March 24, 2021 02:31
Using pyppeteer to screenshot a Single-Page App
# How to use pyppeteer to screenshot an SPA
# Joseph Lou & Jérémie Lumbroso, 2021
#
# This example code is hardcoded for https://codepost.io but should
# show how to use pyppeteer for other SPAs
import asyncio
# ref: https://github.com/miyakogi/pyppeteer/issues/219#issuecomment-563077061
import pyppdf.patch_pyppeteer # needed to avoid chromium errors
@jlumbroso
jlumbroso / codePost-finalize-all.py
Last active March 13, 2022 20:09
codePost snippet to automatically finalize all submissions of an assignment
# the codePost Python SDK must be installed: pip install codepost
import codepost
# variable parameters
# get the API key here: https://codepost.io/settings
API_KEY = "... see above where to get this ..."
COURSE_NAME = "COS126"
COURSE_TERM = "S2022"
ASSIGNMENT = "Programming Exam 1"
@jlumbroso
jlumbroso / codePost-finalize-all-with-comment.py
Last active March 13, 2022 20:09
codePost snippet to automatically finalize all submissions of an assignment, and automatically add a comment that sets the grade of the submission
import codepost
# variable parameters
# get the API key here: https://codepost.io/settings
API_KEY = "... see above where to get this ..."
COURSE_NAME = "COS126"
COURSE_TERM = "S2022"
ASSIGNMENT = "Programming Exam 1"
# authenticate