Skip to content

Instantly share code, notes, and snippets.

@Dutcho
Dutcho / PoC_AtomicString.py
Created February 17, 2023 15:54
Minimal proof of concept for atomic and iterable string types
"""Minimal proof of concept for atomic and iterable string types, 17 Feb 2023.
Illustration for Python Ideas discussion
see https://discuss.python.org/t/an-interesting-pytype-experiment-and-a-possible-extension-to-strings/23749/51
"""
from collections.abc import Container, Sequence, Sized
from typing import Final, overload, Self, TYPE_CHECKING
@Dutcho
Dutcho / AoC03.py
Created December 3, 2018 23:41
AoC03.py
'''AoC 2018 #03, safari-https://adventofcode.com/2018/day/3 - Olaf, 3 Dec 2018'''
import numpy, re
regexp = re.compile(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)')
with open('AoC03.txt') as file:
data = numpy.array(tuple(tuple(map(int, regexp.match(line).groups())) for line in file))
W, H = (max(data[..., 1 + dim] + data[..., 3 + dim]) for dim in range(2)) # max(x + w), max(y + h)
board = numpy.zeros((W + 1, H + 1), dtype=int) # size [0, W] x [0, H]
@Dutcho
Dutcho / AoC02.py
Created December 2, 2018 12:05
AoC02.py
'''AoC 2018 #02, safari-https://adventofcode.com/2018/day/2 - Olaf, 2 Dec 2018'''
import collections, functools, operator, typing
with open('AoC02.txt') as file:
ids = tuple(map(str.strip, file.readlines()))
def freq_counts(s: str) -> typing.Iterable[int]:
'''frequency counts >1 that apply to chars in string s
>>> ids = 'abcdef bababc abbcde abcccd aabcdd abcdee ababab'.split() # from AoC explanation
@Dutcho
Dutcho / AoC01.py
Created December 2, 2018 12:05
AoC01.py
'''AoC 2018 #01, https://adventofcode.com/2018/day/1 - Olaf, 1 Dec 2018'''
with open('AoC01.txt') as file:
deltas = tuple(map(int, file.readlines()))
print('first answer', sum(deltas))
frequency, previous, found = 0, set(), False
while not found:
@Dutcho
Dutcho / Appex GetFromURL.py
Created December 26, 2015 14:38
Appex GetFromURL
# coding: utf-8
# Olaf, Dec 2015, Pythonista 1.6 beta
'''Appex GetFromURL
Pythonista app extension for use on share sheet of other apps to import file from URL into Pythonista
The file is saved at HOME/DESTINATION without user interaction (no 'save as' dialog) unless duplicate'''
from __future__ import print_function
try:
import appex, console, contextlib, itertools, os, os.path, sys, time, urllib, urlparse
@Dutcho
Dutcho / Audio Recorder.py
Created December 26, 2015 14:30 — forked from omz/Audio Recorder.py
Audio Recorder.py
# Retrieved from https://gist.githubusercontent.com/omz/9882a00abf59c6009fa4/raw/139afad596c6d46f2f104b16120eefbb36e9960c/Audio%2520Recorder.py on Sat 26-Dec-2015 07:22:53
# Olaf, 26 Dec 2015, updated to use objc_util instead of ctypes; also removed seemingly superfluous AVAudioSession
import objc_util, os
def main():
NSMutableDictionary = objc_util.ObjCClass('NSMutableDictionary')
settings = NSMutableDictionary.dictionary()
kAudioFormatMPEG4AAC = 1633772320
settings.setObject_forKey_(kAudioFormatMPEG4AAC, 'AVFormatIDKey')
# 2014-08-18 New from Gist.py downloaded from https://gist.github.com/dhutchison/8528503
# 2014-08-18 added generation of header with date, name, download url
### Based on: https://gist.github.com/b0644f5ed1d94bd32805
### This version strips unicode characters from the downloaded script
### to work around the currently limited unicode support of the editor
### module.
# This script downloads and opens a Gist from a URL in the clipboard.
# It's meant to be put in the editor's actions menu.