Skip to content

Instantly share code, notes, and snippets.

View eric-wieser's full-sized avatar

Eric Wieser eric-wieser

View GitHub Profile
@eric-wieser
eric-wieser / pprint_numpy.py
Created November 6, 2018 08:18
A super janky example of how _repr_html_ could be implemented
import numpy as np
def _html_repr_helper(contents, index=()):
dims_left = contents.ndim - len(index)
if dims_left == 0:
s = contents[index]
else:
s = ''.join(_html_repr_helper(contents, index + (i,)) for i in range(contents.shape[len(index)]))
return "<div class='numpy-array-ndim-{} numpy-array-ndim-m{}' title='[{}]'>{}</div>".format(
@eric-wieser
eric-wieser / numpy_list.py
Created November 3, 2018 06:31
An ArrayList backed by numpy
from collections import MutableSequence
import sys
import numpy as np
class NumpyList(MutableSequence):
"""
An implementation of an arraylist, that reallocates whenever an append
would not fit within the existing allocation
In the interest of preventing memory leaks, the array is resized in place,
@eric-wieser
eric-wieser / __main__.py
Last active April 18, 2018 06:37
Proposal for `np.field_info`
import numpy as np
from field_info import field_info
dt = np.dtype([('time', [('subtime', np.int32, (30,20))], 200), ('value', np.float32, (10,2))])
assert field_info(dt)['time'].offset == 0
assert field_info(dt)['time'][1].offset == 30 * 20 * 4
# etc
@eric-wieser
eric-wieser / unicode-tree.py
Created July 18, 2017 23:04
Print out a nested list as a tree with line-drawing characters
#! python3
def first_then(first, then):
yield first
while True:
yield then
prefix_one = lambda: first_then('━━', ' ')
prefix_many_first = lambda: first_then('┳━', '┃ ')
prefix_many_rest = lambda: first_then('┣━', '┃ ')
@eric-wieser
eric-wieser / async_pipe.py
Created May 29, 2017 15:25
An async iterable that can be consumed from one end, and inserted from the other
import asyncio
class FuturePipe:
"""
An async iterable that can be consumed from one end, and inserted from the other:
>>> fp = FuturePipe()
>>> async def put_things():
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@eric-wieser
eric-wieser / README.md
Last active October 5, 2016 18:24
Engineering lecture calendar tool

A tiny script to open a pair of web calendars for your course choices

  1. Visit http://www3.eng.cam.ac.uk/comet/student/y4/module_selection/index.html
  2. Open the web inspector (F12 usually), paste the code below into the javascript console, and hit enter
  3. Two windows should appear, one for each of lent and michaelmas. With any luck, these will open your calendar program, and ask you to add the calendars!
@eric-wieser
eric-wieser / ROS msg.sublime-syntax
Created May 8, 2016 15:19
Basic syntax highlighting for message files
%YAML 1.2
---
name: ROS message definition
file_extensions: [msg]
scope: source.rosmsg
contexts:
main:
- match: \#.*$
scope: comment.line.rosmsg
@eric-wieser
eric-wieser / supplementary.py
Created November 11, 2015 01:48
For the 6.438 project part I
"""Translated to python by Eric Wieser. Use with caution - untested"""
import numpy as np
def bin2gray(x):
return x ^ (x >> 1)
def gray2bin(x):
out = x
mask = x >> 1
while np.any(mask != 0):
@eric-wieser
eric-wieser / README.md
Last active September 30, 2015 02:08
6.004 File downloader and uploader

Use bookmarklet.js to get files in and out of the browser, and convert.py to convert them into separate files per module

Downloading

  1. hit F12 to get the console and paste in the code below
  2. Click the download button that appeared on the toolbar (the cloud icon)
  3. (optional) drag the result onto convert.py to separate the modules

Uploading

  1. hit F12 to get the console and paste in the code below
  2. (optional) after making changes to the separated modules, drag the folder back onto convert.py