Skip to content
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

Reorganise the standard library #227

Merged
merged 6 commits into from
May 18, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

__pycache__/

env/
.env/
dist/
docs/_build
htmlcov
Expand All @@ -25,7 +25,6 @@ arlunio/tutorial/
build
*.pyc
pip-wheel-metadata
.dev
*.mo
*.xml
*.snap
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://gitlab.com/pycqa/flake8
rev: master
hooks:
- id: flake8
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.pythonPath": "${workspaceFolder}/.dev/bin/python",
"python.pythonPath": "${workspaceFolder}/.env/bin/python",
"python.terminal.activateEnvInCurrentTerminal": true,
"python.terminal.activateEnvironment": true,
"python.testing.pytestEnabled": true,
Expand Down
2 changes: 1 addition & 1 deletion arlunio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._core import Defn, DefnInput, Mask, definition # noqa: F401
from ._core import Defn, DefnInput, definition # noqa: F401
from ._expressions import all, any, clamp, invert, lerp, normalise # noqa: F401
from ._images import Image, Resolutions, colorramp, encode, fill, save # noqa: F401
from ._version import __version__ # noqa: F401
5 changes: 0 additions & 5 deletions arlunio/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
logger = logging.getLogger(__name__)


class Mask:
"""Currently just a type alias for boolean numpy arrays but gives us the flexibility
to add smarts later."""


def _format_type(obj: Optional[Any] = None, type_: Optional[Any] = None) -> str:
"""Given an object, return an appropriate representation for its type."""

Expand Down
4 changes: 2 additions & 2 deletions arlunio/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def _process_docstring(

if inherits is not None:

for l in reversed(inherits):
lines.insert(0, l)
for line in reversed(inherits):
lines.insert(0, line)


def setup(app: Sphinx) -> Dict[str, Any]:
Expand Down
27 changes: 14 additions & 13 deletions arlunio/doc/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import textwrap
import traceback

from typing import List
from typing import List, Tuple

import arlunio as ar

Expand Down Expand Up @@ -84,7 +84,9 @@ def parse_content(state, content: StringList) -> List[nodes.Node]:
return section.children


def render_image(src: str, smooth: bool = True) -> List[nodes.Node]:
def render_image(
src: str, smooth: bool = True, location: Tuple[str, int] = None
) -> List[nodes.Node]:
"""Given the source code for an image return a doctree that when rendered by
Sphinx will insert that image into a HTML page.

Expand All @@ -97,23 +99,20 @@ def render_image(src: str, smooth: bool = True) -> List[nodes.Node]:
algorithm that smooths out the edges of the image.
"""
doctree = []
environment = {}

try:
code = compile(src, "<string>", "exec")
exec(code, environment)
except Exception:
message = nodes.Text("Unable to render image: Invalid code")
err = nodes.literal_block("", traceback.format_exc())
doctree.append(nodes.error("", message, err))

return doctree
tback = traceback.format_exc()

environment = {}
# Flag the issue to the user to the issue in the log
logger.warning("Unable to render image\n%s", tback, location=location)

try:
exec(code, environment)
except Exception:
# But also make the error obvious in the docs.
message = nodes.Text("Unable to render image: Error in code")
err = nodes.literal_block("", traceback.format_exc())
err = nodes.literal_block("", tback)
doctree.append(nodes.error("", message, err))

return doctree
Expand Down Expand Up @@ -179,7 +178,9 @@ def run(self):

src = "\n".join(self.content)
smooth = not ("disable-smoothing" in self.options.keys())
nodelist = render_image(src, smooth)

location = self.state_machine.get_source_and_line(self.lineno)
nodelist = render_image(src, smooth, location=location)

if "include-code" in self.options.keys():

Expand Down
12 changes: 0 additions & 12 deletions arlunio/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
from .operators import MaskAdd, MaskMul, MaskSub # noqa: F401
from .parameters import R, T, X, Y # noqa: F401
from .pattern import Grid, Map, Pixelize # noqa: F401
from .shapes import ( # noqa: F401
Circle,
Ellipse,
Empty,
Full,
Rectangle,
Square,
SuperEllipse,
)
17 changes: 17 additions & 0 deletions arlunio/lib/mask/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .mask import Mask
from .pattern import Grid, Map, Pixelize
from .shape import Circle, Ellipse, Empty, Full, Rectangle, Square, SuperEllipse

__all__ = [
"Mask",
"Circle",
"Ellipse",
"Empty",
"Full",
"Grid",
"Map",
"Pixelize",
"Rectangle",
"Square",
"SuperEllipse",
]
17 changes: 10 additions & 7 deletions arlunio/lib/operators.py → arlunio/lib/mask/mask.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import arlunio as ar

from arlunio import Defn, Mask

class Mask:
"""Currently just a type alias for boolean numpy arrays but gives us the flexibility
to add smarts later."""

@ar.definition(operation=Defn.OP_ADD)

@ar.definition(operation=ar.Defn.OP_ADD)
def MaskAdd(
width: int, height: int, *, a: Defn[Mask] = None, b: Defn[Mask] = None
width: int, height: int, *, a: ar.Defn[Mask] = None, b: ar.Defn[Mask] = None
) -> Mask:
"""Add any two mask producing definitions together.

Expand Down Expand Up @@ -53,9 +56,9 @@ def MaskAdd(
return ar.any(a(width=width, height=height), b(width=width, height=height))


@ar.definition(operation=Defn.OP_SUB)
@ar.definition(operation=ar.Defn.OP_SUB)
def MaskSub(
width: int, height: int, *, a: Defn[Mask] = None, b: Defn[Mask] = None
width: int, height: int, *, a: ar.Defn[Mask] = None, b: ar.Defn[Mask] = None
) -> Mask:
"""Subtract one mask away from another mask.

Expand Down Expand Up @@ -109,9 +112,9 @@ def MaskSub(
)


@ar.definition(operation=Defn.OP_MUL)
@ar.definition(operation=ar.Defn.OP_MUL)
def MaskMul(
width: int, height: int, *, a: Defn[Mask] = None, b: Defn[Mask] = None
width: int, height: int, *, a: ar.Defn[Mask] = None, b: ar.Defn[Mask] = None
) -> Mask:
"""Muliply any two mask producing definitions together.

Expand Down
25 changes: 15 additions & 10 deletions arlunio/lib/pattern.py → arlunio/lib/mask/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import arlunio as ar
import numpy as np

from .shape import Mask

logger = logging.getLogger(__name__)


@ar.definition
def Grid(width: int, height: int, *, n=4, m=None, defn=None) -> ar.Mask:
def Grid(width: int, height: int, *, n=4, m=None, defn=None) -> Mask:
"""
.. arlunio-image::

import arlunio as ar
from arlunio.lib import Circle, Grid
from arlunio.lib.mask import Circle, Grid

pattern = Grid(defn=Circle())
image = ar.fill(pattern(width=1920, height=1080))
Expand Down Expand Up @@ -49,7 +51,8 @@ def Grid(width: int, height: int, *, n=4, m=None, defn=None) -> ar.Mask:
import arlunio as ar
import numpy as np

from arlunio.lib import Circle, Grid, X, Y
from arlunio.lib.mask import Circle, Grid
from arlunio.lib.math import X, Y

@ar.definition
def Template(x:X, y: Y):
Expand All @@ -69,7 +72,8 @@ def Template(x:X, y: Y):
import arlunio as ar
import numpy as np

from arlunio.lib import Grid, X, Y
from arlunio.lib.mask import Grid
from arlunio.lib.math import X, Y

@ar.definition
def Template(x: X, y: Y):
Expand Down Expand Up @@ -101,7 +105,7 @@ def Template(x: X, y: Y):


@ar.definition
def Map(width: int, height: int, *, layout=None, legend=None) -> ar.Mask:
def Map(width: int, height: int, *, layout=None, legend=None) -> Mask:
"""For more complex layouts.

.. note::
Expand All @@ -125,7 +129,7 @@ def Map(width: int, height: int, *, layout=None, legend=None) -> ar.Mask:
import arlunio as ar
import numpy as np

from arlunio.lib import Empty, Map, Rectangle
from arlunio.lib.mask import Empty, Map, Rectangle

@ar.definition
def Wall(width: int, height: int, *, sides=None):
Expand Down Expand Up @@ -182,12 +186,12 @@ def Wall(width: int, height: int, *, sides=None):
@ar.definition
def Pixelize(
width: int, height: int, *, pixels=None, defn=None, n=None, m=None
) -> ar.Mask:
) -> Mask:
"""
.. arlunio-image::

import arlunio as ar
from arlunio.lib import Circle, Pixelize
from arlunio.lib.mask import Circle, Pixelize

pix = Pixelize(defn=Circle(), n=32, m=32)
image = ar.fill(pix(width=1920, height=1080))
Expand Down Expand Up @@ -231,7 +235,7 @@ def Pixelize(
import arlunio as ar
import numpy as np

from arlunio.lib import Pixelize
from arlunio.lib.mask import Pixelize

pixels = np.array([
[False, True, True, False],
Expand All @@ -250,7 +254,8 @@ def Pixelize(
import arlunio as ar
import numpy as np

from arlunio.lib import Circle, X, Y, Pixelize
from arlunio.lib.mask import Circle, Pixelize
from arlunio.lib.math import X, Y

@ar.definition
def Ghost(x: X, y: Y):
Expand Down