Skip to content

Commit 6f6efa4

Browse files
add config file for pre-commit (pyscript#235)
* add config file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add isort * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d89e8fb commit 6f6efa4

File tree

11 files changed

+460
-203
lines changed

11 files changed

+460
-203
lines changed

Diff for: .pre-commit-config.yaml

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
# This is the configuration for pre-commit, a local framework for managing pre-commit hooks
2-
# Check out the docs at: https://pre-commit.com/
3-
4-
repos: []
1+
default_stages: [commit]
2+
repos:
3+
- repo: https://github.com/psf/black
4+
rev: 22.3.0
5+
hooks:
6+
- id: black
7+
- repo: https://github.com/pycqa/isort
8+
rev: 5.10.1
9+
hooks:
10+
- id: isort
11+
name: isort (python)

Diff for: pyscriptjs/examples/antigravity.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import random
22
import sys
33

4-
from js import document, DOMParser, setInterval
4+
from js import DOMParser, document, setInterval
55
from pyodide import create_proxy
66
from pyodide.http import open_url
77

8-
class Antigravity():
98

10-
url = './antigravity.svg'
11-
9+
class Antigravity:
10+
11+
url = "./antigravity.svg"
12+
1213
def __init__(self, target=None, interval=10, append=True, fly=False):
1314
target = target or sys.stdout._out
14-
self.target = document.getElementById(target) if isinstance(target, str) else target
15-
doc = DOMParser.new().parseFromString(open_url(self.url).read(), "image/svg+xml")
15+
self.target = (
16+
document.getElementById(target) if isinstance(target, str) else target
17+
)
18+
doc = DOMParser.new().parseFromString(
19+
open_url(self.url).read(), "image/svg+xml"
20+
)
1621
self.node = doc.documentElement
1722
if append:
1823
self.target.append(self.node)
@@ -27,13 +32,14 @@ def fly(self):
2732
setInterval(create_proxy(self.move), self.interval)
2833

2934
def move(self):
30-
char = self.node.getElementsByTagName('g')[1]
31-
char.setAttribute('transform', f'translate({self.xoffset}, {-self.yoffset})')
32-
self.xoffset += random.normalvariate(0, 1)/20
35+
char = self.node.getElementsByTagName("g")[1]
36+
char.setAttribute("transform", f"translate({self.xoffset}, {-self.yoffset})")
37+
self.xoffset += random.normalvariate(0, 1) / 20
3338
if self.yoffset < 50:
3439
self.yoffset += 0.1
3540
else:
36-
self.yoffset += random.normalvariate(0, 1)/20
41+
self.yoffset += random.normalvariate(0, 1) / 20
42+
3743

3844
_auto = Antigravity(append=True)
3945
fly = _auto.fly

Diff for: pyscriptjs/examples/fractals.py

+54-22
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
from typing import Tuple
2+
23
import numpy as np
34
from numpy.polynomial import Polynomial
45

5-
def mandelbrot(width: int, height: int, *,
6-
x: float = -0.5, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:
6+
7+
def mandelbrot(
8+
width: int,
9+
height: int,
10+
*,
11+
x: float = -0.5,
12+
y: float = 0,
13+
zoom: int = 1,
14+
max_iterations: int = 100
15+
) -> np.array:
716
"""
817
From https://www.learnpythonwithrune.org/numpy-compute-mandelbrot-set-by-vectorization/.
918
"""
1019
# To make navigation easier we calculate these values
11-
x_width, y_height = 1.5, 1.5*height/width
12-
x_from, x_to = x - x_width/zoom, x + x_width/zoom
13-
y_from, y_to = y - y_height/zoom, y + y_height/zoom
20+
x_width, y_height = 1.5, 1.5 * height / width
21+
x_from, x_to = x - x_width / zoom, x + x_width / zoom
22+
y_from, y_to = y - y_height / zoom, y + y_height / zoom
1423

1524
# Here the actual algorithm starts
1625
x = np.linspace(x_from, x_to, width).reshape((1, width))
1726
y = np.linspace(y_from, y_to, height).reshape((height, 1))
18-
c = x + 1j*y
27+
c = x + 1j * y
1928

2029
# Initialize z to all zero
2130
z = np.zeros(c.shape, dtype=np.complex128)
@@ -26,27 +35,38 @@ def mandelbrot(width: int, height: int, *,
2635
# To keep track on which points did not converge so far
2736
m = np.full(c.shape, True, dtype=bool)
2837
for i in range(max_iterations):
29-
z[m] = z[m]**2 + c[m]
30-
diverged = np.greater(np.abs(z), 2, out=np.full(c.shape, False), where=m) # Find diverging
31-
div_time[diverged] = i # set the value of the diverged iteration number
32-
m[np.abs(z) > 2] = False # to remember which have diverged
38+
z[m] = z[m] ** 2 + c[m]
39+
diverged = np.greater(
40+
np.abs(z), 2, out=np.full(c.shape, False), where=m
41+
) # Find diverging
42+
div_time[diverged] = i # set the value of the diverged iteration number
43+
m[np.abs(z) > 2] = False # to remember which have diverged
3344

3445
return div_time
3546

36-
def julia(width: int, height: int, *,
37-
c: complex = -0.4 + 0.6j, x: float = 0, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:
47+
48+
def julia(
49+
width: int,
50+
height: int,
51+
*,
52+
c: complex = -0.4 + 0.6j,
53+
x: float = 0,
54+
y: float = 0,
55+
zoom: int = 1,
56+
max_iterations: int = 100
57+
) -> np.array:
3858
"""
3959
From https://www.learnpythonwithrune.org/numpy-calculate-the-julia-set-with-vectorization/.
4060
"""
4161
# To make navigation easier we calculate these values
42-
x_width, y_height = 1.5, 1.5*height/width
43-
x_from, x_to = x - x_width/zoom, x + x_width/zoom
44-
y_from, y_to = y - y_height/zoom, y + y_height/zoom
62+
x_width, y_height = 1.5, 1.5 * height / width
63+
x_from, x_to = x - x_width / zoom, x + x_width / zoom
64+
y_from, y_to = y - y_height / zoom, y + y_height / zoom
4565

4666
# Here the actual algorithm starts
4767
x = np.linspace(x_from, x_to, width).reshape((1, width))
4868
y = np.linspace(y_from, y_to, height).reshape((height, 1))
49-
z = x + 1j*y
69+
z = x + 1j * y
5070

5171
# Initialize z to all zero
5272
c = np.full(z.shape, c)
@@ -57,16 +77,26 @@ def julia(width: int, height: int, *,
5777
# To keep track on which points did not converge so far
5878
m = np.full(c.shape, True, dtype=bool)
5979
for i in range(max_iterations):
60-
z[m] = z[m]**2 + c[m]
80+
z[m] = z[m] ** 2 + c[m]
6181
m[np.abs(z) > 2] = False
6282
div_time[m] = i
6383

6484
return div_time
6585

86+
6687
Range = Tuple[float, float]
6788

68-
def newton(width: int, height: int, *,
69-
p: Polynomial, a: complex, xr: Range = (-2.5, 1), yr: Range = (-1, 1), max_iterations: int = 100) -> (np.array, np.array):
89+
90+
def newton(
91+
width: int,
92+
height: int,
93+
*,
94+
p: Polynomial,
95+
a: complex,
96+
xr: Range = (-2.5, 1),
97+
yr: Range = (-1, 1),
98+
max_iterations: int = 100
99+
) -> (np.array, np.array):
70100
""" """
71101
# To make navigation easier we calculate these values
72102
x_from, x_to = xr
@@ -75,7 +105,7 @@ def newton(width: int, height: int, *,
75105
# Here the actual algorithm starts
76106
x = np.linspace(x_from, x_to, width).reshape((1, width))
77107
y = np.linspace(y_from, y_to, height).reshape((height, 1))
78-
z = x + 1j*y
108+
z = x + 1j * y
79109

80110
# Compute the derivative
81111
dp = p.deriv()
@@ -97,10 +127,12 @@ def newton(width: int, height: int, *,
97127
r = np.full(a.shape, 0, dtype=int)
98128

99129
for i in range(max_iterations):
100-
z[m] = z[m] - a[m]*p(z[m])/dp(z[m])
130+
z[m] = z[m] - a[m] * p(z[m]) / dp(z[m])
101131

102132
for j, root in enumerate(roots):
103-
converged = (np.abs(z.real - root.real) < epsilon) & (np.abs(z.imag - root.imag) < epsilon)
133+
converged = (np.abs(z.real - root.real) < epsilon) & (
134+
np.abs(z.imag - root.imag) < epsilon
135+
)
104136
m[converged] = False
105137
r[converged] = j + 1
106138

0 commit comments

Comments
 (0)