Skip to content

Instantly share code, notes, and snippets.

View lebedov's full-sized avatar

Lev E. Givon lebedov

View GitHub Profile
@lebedov
lebedov / jpype_pdf_text_stripper.py
Created April 28, 2021 12:29
How to use pdfbox's PDFTextStripper class in Python.
#!/usr/bin/env python3
"""
How to use pdfbox's PDFTextStripper class in Python.
"""
import pathlib
import pkg_resources
import re
import urllib.request
@lebedov
lebedov / jpype_api_demo.py
Last active July 10, 2023 14:04
How to call pdfbox's API with JPype.
#!/usr/bin/env python3
"""
How to call pdfbox's API with JPype.
"""
import pathlib
import pkg_resources
import re
import urllib.request
@lebedov
lebedov / pytorch_get_used_mem.py
Created June 24, 2019 18:09
Compute total memory consumed by PyTorch tensors
#!/usr/bin/env python3
"""
Compute total memory consumed by PyTorch tensors.
"""
import gc
import torch
@lebedov
lebedov / .bashrc
Created May 2, 2019 20:11
Bash tab completion of environment names when using conda activate
#!/bin/bash
# Add the following lines to .bashrc to enable tab completion of available conda
# environment names when using conda activate. Assumes conda and jq are in one's PATH:
export CONDA_ENV_LIST=`conda env list --json | jq -r '[ .envs[1:][] | split("/")[-1] ] | join(" ")'`
complete -W "$CONDA_ENV_LIST" conda activate
@lebedov
lebedov / nbshow-colorize.py
Created February 26, 2019 21:51
Add syntax highlighting to nbshow output.
#!/usr/bin/env python
"""
Add syntax highlighting to nbshow output.
"""
import argparse
import re
import pygments
@lebedov
lebedov / panel_boundedintinput.ipynb
Created January 7, 2019 20:06
Panel integer input widget with bounds
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lebedov
lebedov / ri2py_recursive.py
Created November 1, 2018 17:00
Recursively convert rpy2 objects to nested Python data structures.
#!/usr/bin/env python3
"""
Recursively convert rpy2 objects to nested Python data structures.
"""
import numpy as np
from rpy2.robjects import default_converter, globalenv, \
NULL, numpy2ri, pandas2ri, ListVector, r
@lebedov
lebedov / multi_video_reader.py
Created July 25, 2018 14:21
Class that provides an ImageIO-like interface to a sequence of videos treated as a single video.
#!/usr/bin/env python3
"""
Class that provides an ImageIO-like interface to a sequence of videos treated as a single video.
"""
import bisect
import copy
import itertools
@lebedov
lebedov / find_lub_index_cumsum.py
Created July 25, 2018 14:09
Given a list of integers, determine the index of the least upper bound of some value that is in the cumulative sum array of the list.
import bisect
import itertools
def find_lub_cumsum_index(a, x):
a_cumsum = list(itertools.accumulate(a))
i = bisect.bisect_right(a_cumsum, x)
if i != len(a):
return i
raise ValueError
@lebedov
lebedov / browse_video.py
Created May 31, 2018 17:41
Display movie file with support for browsing forward and backward through frames with a slider or arrow keys.
#!/usr/bin/env python3
"""
Display movie file with support for browsing forward and backward through frames
with a slider or arrow keys.
"""
import argparse
import datetime
import os