Skip to content

Commit 3c780d1

Browse files
authored
Merge pull request #2 from astropenguin/#1-dataarrayclass
Add class decorator
2 parents 48f2097 + f6bee19 commit 3c780d1

File tree

17 files changed

+888
-10
lines changed

17 files changed

+888
-10
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
extend-ignore = E203

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ jobs:
2121
pip install poetry
2222
poetry build
2323
poetry publish --username $USERNAME --password $PASSWORD
24+
docs:
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v2
28+
- name: Setup Python
29+
uses: actions/setup-python@v2
30+
with:
31+
python-version: 3.7
32+
- name: Build docs
33+
run: |
34+
pip install poetry
35+
poetry install
36+
poetry run sphinx-apidoc -f -o docs/_apidoc xarray_custom
37+
poetry run sphinx-build docs docs/_build
38+
- name: Deploy docs
39+
uses: peaceiris/actions-gh-pages@v3
40+
with:
41+
personal_token: ${{ secrets.PERSONAL_TOKEN }}
42+
publish_branch: gh-pages
43+
publish_dir: ./docs/_build
44+
force_orphan: true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Editor settings
22
.vscode/
33

4+
# Sphinx docs
5+
docs/_build
6+
docs/_apidoc
7+
48
# Byte-compiled / optimized / DLL files
59
__pycache__/
610
*.py[cod]

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# xarray-custom
2+
3+
[![PyPI](https://img.shields.io/pypi/v/xarray-custom.svg?label=PyPI&style=flat-square)](https://pypi.org/pypi/xarray-custom/)
4+
[![Python](https://img.shields.io/pypi/pyversions/xarray-custom.svg?label=Python&color=yellow&style=flat-square)](https://pypi.org/pypi/xarray-custom/)
5+
[![Test](https://img.shields.io/github/workflow/status/astropenguin/xarray-custom/Test?logo=github&label=Test&style=flat-square)](https://github.com/astropenguin/xarray-custom/actions)
6+
[![License](https://img.shields.io/badge/license-MIT-blue.svg?label=License&style=flat-square)](LICENSE)
7+
28
:zap: Data classes for custom xarray constructors
9+
10+
## TL;DR
11+
12+
xarray-custom is a Python package which helps to create custom DataArray classes in the same manner as [the Python's native dataclass](https://docs.python.org/3/library/dataclasses.html).
13+
Here is an introduction code of what the package provides:
14+
15+
```python
16+
from xarray_custom import coordtype, dataarrayclass
17+
18+
19+
@dataarrayclass(('x', 'y'), float, 'custom')
20+
class CustomDataArray:
21+
x: coordtype('x', int)
22+
y: coordtype('y', int)
23+
z: coordtype(('x', 'y'), str) = 'spam'
24+
25+
def double(self):
26+
"""Custom DataArray method which doubles values."""
27+
return self * 2
28+
29+
30+
dataarray = CustomDataArray([[0, 1], [2, 3]], x=[2, 2], y=[3, 3])
31+
onesarray = CustomDataArray.ones(shape=(3, 3))
32+
doubled = dataarray.custom.double()
33+
```
34+
35+
The key points are:
36+
37+
- Custom DataArray instances with fixed dimensions and coordinates can easily be created.
38+
- Default values and dtype can be specified via a class decorator and class variable annotations.
39+
- NumPy-like special factory functions like ``ones()`` are provided as class methods.
40+
- Custom DataArray methods can be used via a custom accessor.

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
106 KB
Loading

docs/conf.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# This file only contains a selection of the most common options. For a full
4+
# list see the documentation:
5+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
6+
7+
# -- Path setup --------------------------------------------------------------
8+
9+
# If extensions (or modules to document with autodoc) are in another directory,
10+
# add these directories to sys.path here. If the directory is relative to the
11+
# documentation root, use os.path.abspath to make it absolute, like shown here.
12+
#
13+
import os
14+
import sys
15+
16+
sys.path.insert(0, os.path.abspath("../"))
17+
18+
19+
# -- Project information -----------------------------------------------------
20+
21+
project = "xarray-custom"
22+
copyright = "2020, Akio Taniguchi"
23+
author = "Akio Taniguchi"
24+
25+
# The full version, including alpha/beta/rc tags
26+
release = "0.2.0"
27+
28+
29+
# -- General configuration ---------------------------------------------------
30+
31+
# Add any Sphinx extension module names here, as strings. They can be
32+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
33+
# ones.
34+
extensions = [
35+
"sphinx.ext.autodoc",
36+
"sphinx.ext.viewcode",
37+
"sphinx.ext.githubpages",
38+
"sphinx.ext.napoleon",
39+
]
40+
41+
# Add any paths that contain templates here, relative to this directory.
42+
templates_path = ["_templates"]
43+
44+
# List of patterns, relative to source directory, that match files and
45+
# directories to ignore when looking for source files.
46+
# This pattern also affects html_static_path and html_extra_path.
47+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
48+
49+
50+
# -- Options for HTML output -------------------------------------------------
51+
52+
# The theme to use for HTML and HTML Help pages. See the documentation for
53+
# a list of builtin themes.
54+
#
55+
html_theme = "pydata_sphinx_theme"
56+
57+
# Add any paths that contain custom static files (such as style sheets) here,
58+
# relative to this directory. They are copied after the builtin static files,
59+
# so a file named "default.css" will overwrite the builtin "default.css".
60+
html_static_path = ["_static"]
61+
62+
63+
# Options added by the author
64+
html_logo = "_static/dataset-diagram-logo.png"
65+
html_theme_options = {"github_url": "https://github.com/astropenguin/xarray-custom"}

docs/index.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. xarray-custom documentation master file, created by
2+
sphinx-quickstart on Sat May 9 22:13:03 2020.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
xarray-custom documentation
7+
=============================
8+
9+
**Date**: |today| **Version**: |release|
10+
11+
.. toctree::
12+
:maxdepth: 2
13+
:caption: Contents:
14+
15+
xarray-custom is a Python package which helps to create custom DataArray classes in the same manner as `the Python's native dataclass <https://docs.python.org/3/library/dataclasses.html>`__.
16+
Here is an introduction code of what the package provides::
17+
18+
from xarray_custom import coordtype, dataarrayclass
19+
20+
21+
@dataarrayclass(('x', 'y'), float, 'custom')
22+
class CustomDataArray:
23+
x: coordtype('x', int)
24+
y: coordtype('y', int)
25+
z: coordtype(('x', 'y'), str) = 'spam'
26+
27+
def double(self):
28+
"""Custom DataArray method which doubles values."""
29+
return self * 2
30+
31+
32+
dataarray = CustomDataArray([[0, 1], [2, 3]], x=[2, 2], y=[3, 3])
33+
onesarray = CustomDataArray.ones(shape=(3, 3))
34+
doubled = dataarray.custom.double()
35+
36+
The key points are:
37+
38+
* Custom DataArray instances with fixed dimensions and coordinates can easily be created.
39+
* Default values and dtype can be specified via a class decorator and class variable annotations.
40+
* NumPy-like special factory functions like ``ones()`` are provided as class methods.
41+
* Custom DataArray methods can be used via a custom accessor.
42+
43+
Contents
44+
========
45+
46+
* :ref:`genindex`
47+
* :ref:`modindex`

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=.
11+
set BUILDDIR=_build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.http://sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

etc/builddocs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
sphinx-apidoc -f -o docs/_apidoc xarray_custom
5+
sphinx-build docs docs/_build

0 commit comments

Comments
 (0)