8000 add config file for pre-commit by pww217 · Pull Request #235 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

add config file for pre-commit #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# This is the configuration for pre-commit, a local framework for managing pre-commit hooks
# Check out the docs at: https://pre-commit.com/

repos: []
default_stages: [commit]
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
26 changes: 16 additions & 10 deletions pyscriptjs/examples/antigravity.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import random
import sys

from js import document, DOMParser, setInterval
from js import DOMParser, document, setInterval
from pyodide import create_proxy
from pyodide.http import open_url

class Antigravity():

url = './antigravity.svg'

class Antigravity:

url = "./antigravity.svg"

def __init__(self, target=None, interval=10, append=True, fly=False):
target = target or sys.stdout._out
self.target = document.getElementById(target) if isinstance(target, str) else target
doc = DOMParser.new().parseFromString(open_url(self.url).read(), "image/svg+xml")
self.target = (
document.getElementById(target) if isinstance(target, str) else target
)
doc = DOMParser.new().parseFromString(
open_url(self.url).read(), "image/svg+xml"
)
self.node = doc.documentElement
if append:
self.target.append(self.node)
Expand All @@ -27,13 +32,14 @@ def fly(self):
setInterval(create_proxy(self.move), self.interval)

def move(self):
char = self.node.getElementsByTagName('g')[1]
char.setAttribute('transform', f'translate({self.xoffset}, {-self.yoffset})')
self.xoffset += random.normalvariate(0, 1)/20
char = self.node.getElementsByTagName("g")[1]
char.setAttribute("transform", f"translate({self.xoffset}, {-self.yoffset})")
self.xoffset += random.normalvariate(0, 1) / 20
if self.yoffset < 50:
self.yoffset += 0.1
else:
self.yoffset += random.normalvariate(0, 1)/20
self.yoffset += random.normalvariate(0, 1) / 20


_auto = Antigravity(append=True)
fly = _auto.fly
76 changes: 54 additions & 22 deletions pyscriptjs/examples/fractals.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
from typing import Tuple

import numpy as np
from numpy.polynomial import Polynomial

def mandelbrot(width: int, height: int, *,
x: float = -0.5, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:

def mandelbrot(
width: int,
height: int,
*,
x: float = -0.5,
y: float = 0,
zoom: int = 1,
max_iterations: int = 100
) -> np.array:
"""
From https://www.learnpythonwithrune.org/numpy-compute-mandelbrot-set-by-vectorization/.
"""
# To make navigation easier we calculate these values
x_width, y_height = 1.5, 1.5*height/width
x_from, x_to = x - x_width/zoom, x + x_width/zoom
y_from, y_to = y - y_height/zoom, y + y_height/zoom
x_width, y_height = 1.5, 1.5 * height / width
x_from, x_to = x - x_width / zoom, x + x_width / zoom
y_from, y_to = y - y_height / zoom, y + y_height / zoom

# Here the actual algorithm starts
x = np.linspace(x_from, x_to, width).reshape((1, width))
y = np.linspace(y_from, y_to, height).reshape((height, 1))
c = x + 1j*y
c = x + 1j * y

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

return div_time

def julia(width: int, height: int, *,
c: complex = -0.4 + 0.6j, x: float = 0, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:

def julia(
width: int,
height: int,
*,
c: complex = -0.4 + 0.6j,
x: float = 0,
y: float = 0,
zoom: int = 1,
max_iterations: int = 100
) -> np.array:
"""
From https://www.learnpythonwithrune.org/numpy-calculate-the-julia-set-with-vectorization/.
"""
# To make navigation easier we calculate these values
x_width, y_height = 1.5, 1.5*height/width
x_from, x_to = x - x_width/zoom, x + x_width/zoom
y_from, y_to = y - y_height/zoom, y + y_height/zoom
x_width, y_height = 1.5, 1.5 * height / width
x_from, x_to = x - x_width / zoom, x + x_width / zoom
y_from, y_to = y - y_height / zoom, y + y_height / zoom

# Here the actual algorithm starts
x = np.linspace(x_from, x_to, width).reshape((1, width))
y = np.linspace(y_from, y_to, height).reshape((height, 1))
z = x + 1j*y
z = x + 1j * y

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

return div_time


Range = Tuple[float, float]

def newton(width: int, height: int, *,
p: Polynomial, a: complex, xr: Range = (-2.5, 1), yr: Range = (-1, 1), max_iterations: int = 100) -> (np.array, np.array):

def newton(
width: int,
height: int,
*,
p: Polynomial,
a: complex,
xr: Range = (-2.5, 1),
yr: Range = (-1, 1),
max_iterations: int = 100
) -> (np.array, np.array):
""" """
# To make navigation easier we calculate these values
x_from, x_to = xr
Expand All @@ -75,7 +105,7 @@ def newton(width: int, height: int, *,
# Here the actual algorithm starts
x = np.linspace(x_from, x_to, width).reshape((1, width))
y = np.linspace(y_from, y_to, height).reshape((height, 1))
z = x + 1j*y
z = x + 1j * y

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

for i in range(max_iterations):
z[m] = z[m] - a[m]*p(z[m])/dp(z[m])
z[m] = z[m] - a[m] * p(z[m]) / dp(z[m])

for j, root in enumerate(roots):
converged = (np.abs(z.real - root.real) < epsilon) & (np.abs(z.imag - root.imag) < epsilon)
converged = (np.abs(z.real - root.real) < epsilon) & (
np.abs(z.imag - root.imag) < epsilon
)
m[converged] = False
r[converged] = j + 1

Expand Down
Loading
0