Skip to content

Instantly share code, notes, and snippets.

View ProbonoBonobo's full-sized avatar

Kevin Zeidler ProbonoBonobo

View GitHub Profile
@ProbonoBonobo
ProbonoBonobo / gist:59071ea680995df6f788fd1dada9eb2d
Last active August 8, 2017 01:20
Python utility methods for opening files (Mac only)
#!/usr/bin/env python3
""" Some Mac-specific utility methods I have found useful for opening files. Uses mdfind, a built-in command-line
interface to spotlight's desktop cache, so results are usually instantaneous.
Demo:
In [390]: %timeit pprint(conjure('LCD Soundsystem'))
['/Users/kz/Desktop/artists/metacritic/lcd-soundsystem.json',
'/Users/kz/Desktop/artists/metacritic/http:⚡︎⚡︎metacritic.com⚡︎music⚡︎lcd-soundsystem⚡︎lcd-soundsystem⚡︎user-reviews',
'/Users/kz/Desktop/artists/metacritic/http:⚡︎⚡︎www.trouserpress.com⚡︎entry.php?a=lcd_soundsystem',
'/Users/kz/Desktop/artists/metacritic/lcd-soundsystem.pkl',
@ProbonoBonobo
ProbonoBonobo / index.html
Created April 4, 2019 00:50
Vue-plotly component
<div id="plotlyExample" style="grid-template-columns: 50% 50%; display: grid; grid-template-rows: 400px 100px">
<plotly-graph @hover="hover" v-bind:data="linePlotData" div-id="plot1" v-bind:height="90" :column-number="1"></plotly-graph>
<plotly-graph @hover="hover" v-bind:data="scatterPlotData" div-id="plot2" v-bind:height="90" :column-number="2"></plotly-graph>
<input type="range" min="-10" max="10" v-model="currentVal" @change="sliderUpdate" style="grid-column-start: 1; grid-column-end: 3; width: 50%; margin-left: auto; margin-right: auto" />
</div>
@ProbonoBonobo
ProbonoBonobo / ptpython_async.py
Created December 12, 2019 22:08
Interactively run a python file from within the ptpython REPL and periodically poll for changes. When a change is detected, the file will be reexecuted automatically in the REPL's global scope. In the event that there's a syntax error, the error traceback will be printed to the REPL.
#!/usr/bin/env python
"""
(Python >3.3, ptpython>=2.0.6)
Interactively run a python file from within the ptpython REPL and periodically
poll for changes. When a change is detected, the file will be reexecuted
automatically in the REPL's global scope. In the event that there's a syntax
error, the error traceback will be printed to the REPL.
Based on the example of embedding a Python REPL into an asyncio
application (see: https://github.com/prompt-toolkit/ptpython/blob/master/examples/asyncio-python-embed.py)
"""crawl.py
This is an exercise in using Trio to asynchronously request a website's sitemaps in a large batch, recursively
visit new sitemaps as revealed in the sitemap structure, then add discovered URLs to a database along with a
flag that indicates those URLs need to be crawled. When all sitemaps have been parsed, the crawler requests a
bunch of URLs (50 or so) at a time and parses the HTTP responses as they are received. The crawler requires a
website to use embed a JSON object that uses either the Article or NewsArticle schema.org convention; I haven't
yet found a news website that doesn't use this convention.
"""
import os
import re
from unittest import TestCase
import unittest
import subprocess
from subprocess import PIPE
import time
import json
#!/usr/bin/env python
""" Simple example of a Python program that initializes and saves a persistent 'high score' variable
by reading/writing a value to a file. """
high_score_file_name = "hiscore"
try:
with open(high_score_file_name, "r") as f: #open the file 'hiscore' in read mode
old_score = int(f.read())
#!/usr/bin/env python
# suppose we have a key:value mapping saved to "/tmp/my_obj.json". (Note: 'json' is a file serialization format that works in a similar way as Python `dict` objects)
# first, import the built-in `json` module
import json
with open("/tmp/my_obj.json", "r") as f: # open the file in "read" mode (indicated by the second argument, "r")...
my_obj = json.load(f) # and create a Python dictionary object `my_obj` from the key:value pairs in the file using the `json.load` function.
# transcript of live repl session for week 3's lecture
>>> l = [0,1,2,3,4,5]
>>> l[0]
0
>>> l[-1] # negative indices are allowed, and assumed to be relative to the end of the sequence. l[-1] == l[len(l)] == l[5]
5
>>> l[-2] # the second-to-last element...
4
>>> l[-3] # and so on.
@ProbonoBonobo
ProbonoBonobo / battleship.py
Created February 24, 2022 18:49
starter code to initialize an n-dimensional grid and populate it with variable-sized, non-intersecting ships
# battleship.py
# starter code to initialize an n-dimensional grid and populate it with variable-sized, non-intersecting ships
import random
class Board:
def __init__(self, dim=9):
self.dim = dim
self.grid = [[None for i in range(self.dim)] for j in range(self.dim)]
self.piece_sizes = (5, 4, 3, 3, 2)