Skip to content

Instantly share code, notes, and snippets.

View juandesant's full-sized avatar

Juande Santander-Vela juandesant

View GitHub Profile
#!/usr/bin/env python
import os, pty, subprocess, time
from pprint import pprint
# Ask the OS to create a new "pseudoterminal pair"
# The shell is going to have the "tty" end and the terminal emulator is going to have the
# "pseudoterminal master" ("ptmx") end
# pprint(os.environ)
ptmx, tty = pty.openpty()
@juandesant
juandesant / hexdump.py
Last active May 6, 2025 23:17
Hexdump in python
def hexdump(byte_stream, encoding='utf-8', grouping=16):
num_bytes = len(byte_stream)
padding = (grouping-num_bytes%grouping)%grouping
byte_stream += bytes(chr(0)*padding, encoding)
def map_byte_to_readable_char(the_byte, valid_range=range(32,128), invalid_char='.'):
return chr(the_byte) if the_byte in valid_range else invalid_char
for row in range(0,len(byte_stream)//grouping):
row_data = [f"{byte_stream[row*grouping+n]:02x}" for n in range(0,grouping)]
row_chars = [
f"{map_byte_to_readable_char(byte_stream[row*grouping+n])}"
@juandesant
juandesant / get_dimensions_from_image_path.py
Last active April 8, 2025 17:02
Get dimensions from an image based on file metadata, using mdls in the command line
def get_dimensions_from_image_path(path):
import os, subprocess, plistlib #os for path expansion; subprocess for Popen; plistlib for parsing the plist format
mdls_output = subprocess.Popen(
# command line arguments, including "-p -" for stdout output
['/usr/bin/mdls', '-p', '-', os.path.expandvars(path).rstrip('\n')],
stdout=subprocess.PIPE # pipe for stdout
).stdout.read().decode() # read bytes from stdout, and decode them as string
mdls_dict = plistlib.loads(mdls_output) # load string as Plist
return mdls_dict['kMDItemPixelWidth'], mdls_dict['kMDItemPixelHeight'] # keys for image width and height
@juandesant
juandesant / rename_timestamped_files.py
Last active February 3, 2025 20:17
Rename files using os.listdir and matches, then re.sub, and os.rename
# Get files
import os
import re
# os.expanduser provides the right username based on the current user
target_files = [x for x in os.listdir(os.path.expanduser('~/Downloads'))
if x.lower().endswith('.pdf')
and x.startswith('2025_') # comment out if needed
and x.find('_recepit') != -1 # comment out if needed
]
@juandesant
juandesant / irange.py
Last active January 7, 2025 13:39
Inclusive range (`irange`) using `range` as the basis
def irange(*args):
"""irange(stop) -> range object
irange(start, stop[, step]) -> range object
Returns a range object that produces a sequence of integers from start (inclusive)
to stop (also inclusive) by step, which defaults to 1
irange(i, j) produces i, i+1, i+2, ..., j. (range would stop at j-1)
start defaults to 0, and stop is omitted! irange(4) produces 0, 1, 2, 3, 4.
@juandesant
juandesant / jama_search.py
Last active September 30, 2024 20:11
Generation of URL for Jama Search for a particular input
import sys # we use sys to get the input to the service
# from sys.stdin
from urllib.parse import quote # to convert high-ASCII and other
# into URL-compatible quoting
import webbrowser # To open the web browser
# establish the base instance URL
jama_search_base_url = "https://almaobservatory.jamacloud.com/perspective.req#/"
project_id="project_id code"
#!/usr/bin/env python
import datetime as dt
def riddle_for_date(date, solutions=False):
"""
Determines the number for the Saturday Mac Riddle associated with
a given date, considering whether it is the date for the riddle
or the solutions.
@juandesant
juandesant / acronym_index.tex
Created February 26, 2024 18:04
Create glossary and index entries in LaTeX
\documentclass{article}
\usepackage{hyperref}
\usepackage{makeidx}
\usepackage{glossaries}
\makeindex
\makeglossaries
% Define your acronym
@juandesant
juandesant / export_access_tables.py
Created November 7, 2023 20:35
Export tables from an Access database
# This script assumes that [mdbtools][1] are installed, for instance with
# `brew install mdbtools`
# [1]: https://github.com/mdbtools/mdbtools
# It also assumes that [pandas][2] and [pandas_access][3] are installed,
# [2]: https://pandas.pydata.org/
# [3]: https://pypi.org/project/pandas_access/
# which you can do with
# `pip install pandas pandas_access`
# or
# `pip install pandas_access`
digraph pictionary_play {
node [fontname = "Handlee"];
edge [fontname = "Handlee"];
splines=false;
randir=LR;
draw [
label = "Draw a picture";