This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
from time import perf_counter as clock | |
from itertools import count | |
import numpy as np | |
import pandas as pd | |
def f1(a): | |
return s.factorize()[0] | |
def f2(s): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> s = pd.Series([1, 3, 20, 2, 10]) | |
>>> for k, v in s.groupby(s//10, sort=False): | |
print(k, v.tolist()) | |
0 [1, 3, 2] | |
2 [20] | |
1 [10] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bokeh.models as bm | |
from bokeh.layouts import row | |
from bokeh.plotting import show | |
data = bm.TextInput(width=100) | |
go = bm.Button(label='Go', width=50) | |
result = bm.TextInput(width=100) | |
callback = bm.CustomJS(args={'data': data, 'result': result}, code=""" | |
var g = window.pyodide.globals; | |
g.set('data', data.value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%javascript | |
async function prepare() { | |
await window.pyodide.loadPackage("numpy"); | |
alert(window.pyodide.runPython(` | |
import numpy as np | |
np.array([3, 4]).sum() | |
`)); | |
} | |
prepare(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%javascript | |
require([ | |
'//cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js', | |
], function() { | |
async function main() { | |
let pyodide = await loadPyodide(); | |
window.pyodide = pyodide; | |
alert('pyodide ready'); | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = [0, 1, 2, 3, 4, 5] | |
y = [8.1, 10.2, 15.4, 24.6, 29.8, 31.9] | |
from scipy.interpolate import CubicSpline | |
f = CubicSpline(x, y) | |
f(1.25) # 11.05833333 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point(tuple): | |
def __new__(cls, x, y): | |
return super().__new__(cls, (x, y)) | |
@property | |
def x(self): | |
return self[0] | |
@property | |
def y(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point(list): | |
def __init__(self, x, y): | |
super().__init__((x, y)) | |
>>> points = [Point(1,2), Point(2,3), Point(1,2)] | |
>>> Counter(points) | |
TypeError: unhashable type: 'Point' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> Point = namedtuple('Point', ('x', 'y')) | |
>>> points = [Point(1,2), Point(2,3), Point(1,2)] | |
>>> Counter(points) | |
Counter({Point(x=1, y=2): 2, Point(x=2, y=3): 1}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
def __init__(self, x, y): | |
super().__setattr__('x', x) # workaround for the | |
super().__setattr__('y', y) # rule below | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
def __eq__(self, other): | |
return self.x == other.x and self.y == other.y |
NewerOlder