Skip to content

Instantly share code, notes, and snippets.

@dcbark01
dcbark01 / pdf2img.py
Created December 10, 2021 16:16
Create an image from a PDF file
from pdf2image import convert_from_path
from PyPDF2 import PdfFileReader
def pdf_to_image(file: str, page_num=0, output_file=None) -> Image:
""" Convert a PDF to a PIL Image. """
pdf_page = PdfFileReader(open(file, 'rb')).getPage(page_num)
pdf_shape = pdf_page.mediaBox
pdf_height = pdf_shape[3] - pdf_shape[1]
pdf_width = pdf_shape[2] - pdf_shape[0]
@dcbark01
dcbark01 / listdirs.py
Created December 10, 2021 16:14
listdirs for all files of specific type (walking through all subdirectories)
import os
def listdirs(path='./', filetype='.*'):
""" List all files in directory. """
output = []
for root, dirs, files in os.walk(path):
for file in files:
if filetype in ['.*', '*']:
@dcbark01
dcbark01 / get_gpu_mem_usage.py
Created December 10, 2021 16:13
Get printout of PyTorch tensor GPU memory usage
def pretty_size(size):
"""Pretty prints a torch.Size object"""
assert(isinstance(size, torch.Size))
return " × ".join(map(str, size))
def bytes2human(n, format='%(value).1f %(symbol)s', symbols='customary'):
"""
Convert n bytes into a human readable string based on format.
symbols can be either "customary", "customary_ext", "iec" or "iec_ext",
@dcbark01
dcbark01 / ffprobe_vid_metadata.py
Created November 16, 2019 03:18
Convenient class for getting video metadata
"""
@author: dcbark01
@date: Nov 2019
@license: MIT
"""
import os
import json
import shlex
import subprocess
@dcbark01
dcbark01 / plot_dates.py
Created June 26, 2019 19:16
plot_dates function in Matplotlib for Python - super useful
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# if your dates are strings you need this step
df.Date = pd.to_datetime(df.Date)
fig,ax = plt.subplots()
ax.plot_date(df.Date,df.A)
ax.plot_date(df.Date,df.B)
@dcbark01
dcbark01 / install.sh
Created April 22, 2019 20:59
Automated install of Anaconda3 on AWS EC2 and setup of AWS CLI/Boto3
#!/usr/bin/env bash
# ---------------------------- INSTALL NOTES ----------------------------
# To properly setup the AWS CLI, make sure that this shell script is run with the two required positional args:
# $1 :: (string) AWS Access Key ID
# $2 :: (string) AWS Secret Key
#
# Also make sure to give the script permission to run!:
# chmod 755 /home/ec2-user/install.sh
#
import pandas as pd
import matplotlib.pyplot as plt
from FloodScraper import FloodScraper
# User Inputs
gage_number = 475
reported_from = '09/02/2017 07:31:00 AM'
last = '7 Days'
# Create scraper interest and run a query
# Import the FloodScraper Module
from FloodScraper import FloodScraper
# User inputs
gage_number = 520
reported_from = '09/02/2017 07:31:00 AM'
last = '7 Days'
filename = 'gage_data.json'
# Create scraper interest and run a query
# Import the FloodScraper Module
from FloodScraper import FloodScraper
# User inputs
gage_number = 520
reported_from = '09/02/2017 07:31:00 AM'
last = '6 Hours'
# Create scraper interest and run a query
scraper = FloodScraper()
value
2018-08-20 12:05:10.000000 0
2018-08-20 12:05:19.888677 1
2018-08-20 12:05:24.758246 1
2018-08-20 12:05:25.584516 0
2018-08-20 12:05:32.897718 1
2018-08-20 12:05:36.799978 1
2018-08-20 12:05:44.782406 1
2018-08-20 12:05:45.493593 0
2018-08-20 12:05:52.628990 1