Skip to content

Commit

Permalink
style: switch to ruff for linting and formatting (#220)
Browse files Browse the repository at this point in the history
* fix: update python version

* style: replace black with ruff
  • Loading branch information
domoritz authored Nov 27, 2023
1 parent 9ce845b commit a3b78fe
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 33 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ jobs:
run: |
cd packages/widget
ruff .
black . --check
ruff ../..
15 changes: 10 additions & 5 deletions dev/notebooks/motherduck.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"source": [
"import duckdb\n",
"import yaml\n",
"\n",
"from mosaic_widget import MosaicWidget"
]
},
Expand All @@ -33,7 +34,7 @@
"source": [
"con = duckdb.connect()\n",
"\n",
"share = 'md:_share/gaia-import/c551f2da-3258-4387-ac43-f4be601850ac'\n",
"share = \"md:_share/gaia-import/c551f2da-3258-4387-ac43-f4be601850ac\"\n",
"con.execute(f\"ATTACH '{share}' AS share\")\n",
"\n",
"con.execute(\"ATTACH ':memory:' AS local\")\n",
Expand Down Expand Up @@ -135,14 +136,17 @@
"\n",
"c = duckdb.connect(\"md:gaia-import\")\n",
"\n",
"c.execute(\"\"\"\n",
"c.execute(\n",
" \"\"\"\n",
"CREATE TABLE IF NOT EXISTS gaia_sample_5_percent AS\n",
" SELECT l, b, ra, dec, phot_g_mean_mag, parallax, bp_rp\n",
" FROM gaia\n",
" USING SAMPLE 5 PERCENT (bernoulli)\n",
"\"\"\")\n",
"\"\"\"\n",
")\n",
"\n",
"c.execute(\"\"\"\n",
"c.execute(\n",
" \"\"\"\n",
"CREATE TABLE IF NOT EXISTS gaia_sample_5_percent_projected AS\n",
"WITH tmp AS (\n",
" SELECT\n",
Expand All @@ -159,7 +163,8 @@
" t * (1.340264 + (-0.081106 * t2) + (t6 * (0.000893 + 0.003796 * t2))) AS v,\n",
" * EXCLUDE('t', 't2', 't6')\n",
"FROM tmp\n",
"\"\"\")"
"\"\"\"\n",
")"
]
},
{
Expand Down
24 changes: 16 additions & 8 deletions dev/notebooks/specs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
},
"outputs": [],
"source": [
"from pprint import pprint\n",
"\n",
"import duckdb\n",
"import yaml\n",
"from mosaic_widget import MosaicWidget\n",
"import ipywidgets as widgets\n",
"from pprint import pprint"
"import yaml\n",
"\n",
"from mosaic_widget import MosaicWidget"
]
},
{
Expand All @@ -27,6 +29,7 @@
"source": [
"# Optional: enable logging in Mosaic widget\n",
"import logging\n",
"\n",
"logging.basicConfig(level=logging.INFO)"
]
},
Expand Down Expand Up @@ -103,29 +106,34 @@
" (\"Symbols\", \"symbols\"),\n",
" (\"Table\", \"table\"),\n",
" (\"Voronoi\", \"voronoi\"),\n",
" (\"Wind Map\", \"wind-map\")\n",
" (\"Wind Map\", \"wind-map\"),\n",
" ],\n",
" value=\"weather\",\n",
" description=\"Example:\"\n",
" description=\"Example:\",\n",
")\n",
"\n",
"\n",
"def on_change(change):\n",
" open_spec(change[\"new\"])\n",
"\n",
"\n",
"def open_spec(spec):\n",
" with open(f\"dev/yaml/{spec}.yaml\") as f:\n",
" mosaic.spec = yaml.safe_load(f)\n",
"\n",
"\n",
"dropdown.observe(on_change, \"value\")\n",
"\n",
"open_spec(\"weather\")\n",
"\n",
"output = widgets.Output()\n",
"\n",
"\n",
"@output.capture(clear_output=True)\n",
"def handle_change(change):\n",
" pprint(change.new)\n",
"\n",
"\n",
"mosaic.observe(handle_change, names=[\"selections\"])"
]
},
Expand All @@ -144,9 +152,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "mosaic",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "mosaic"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -158,7 +166,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
8 changes: 5 additions & 3 deletions dev/notebooks/weather.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"source": [
"import pandas as pd\n",
"import yaml\n",
"\n",
"from mosaic_widget import MosaicWidget"
]
},
Expand All @@ -25,6 +26,7 @@
"source": [
"# Optional: enable logging in Mosaic widget\n",
"import logging\n",
"\n",
"logging.basicConfig(level=logging.INFO)"
]
},
Expand All @@ -37,7 +39,7 @@
},
"outputs": [],
"source": [
"weather = pd.read_csv(\"../../data/seattle-weather.csv\", parse_dates=['date'])\n",
"weather = pd.read_csv(\"../../data/seattle-weather.csv\", parse_dates=[\"date\"])\n",
"\n",
"# Load weather spec, remove data key to ensure load from Pandas\n",
"with open(\"../yaml/weather.yaml\") as f:\n",
Expand All @@ -54,7 +56,7 @@
},
"outputs": [],
"source": [
"MosaicWidget(spec, data = {\"weather\": weather})"
"MosaicWidget(spec, data={\"weather\": weather})"
]
}
],
Expand All @@ -74,7 +76,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
28 changes: 18 additions & 10 deletions packages/widget/mosaic_widget/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import duckdb
import anywidget
import traitlets
import pyarrow as pa
import logging
import pathlib
import time
import logging

import anywidget
import duckdb
import pyarrow as pa
import traitlets

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
Expand Down Expand Up @@ -37,10 +38,10 @@ class MosaicWidget(anywidget.AnyWidget):

def __init__(
self,
spec: dict = {},
con=duckdb.connect(),
spec: dict = None,
con=None,
temp_indexes=True,
data={},
data=None,
*args,
**kwargs,
):
Expand All @@ -50,11 +51,18 @@ def __init__(
spec (dict, optional): The initial Mosaic specification. Defaults to {}.
con (connection, optional): A DuckDB connection.
Defaults to duckdb.connect().
data (dict, optional): Pandas DataFrames to add to DuckDB.
The keys are used as the names of the tables. Defaults to {}.
temp_indexes (bool, optional): Whether data cube indexes should be
created as temp tables tables. Defaults to True.
data (dict, optional): Pandas DataFrames to add to DuckDB.
The keys are used as the names of the tables. Defaults to {}.
"""
if data is None:
data = {}
if spec is None:
spec = {}
if con is None:
con = duckdb.connect()

super().__init__(*args, **kwargs)
self.spec = spec
self.con = con
Expand Down
27 changes: 22 additions & 5 deletions packages/widget/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dev = [
"jupyterlab",
"pandas",
"pyyaml",
"black[jupyter]",
"ruff",
]

Expand All @@ -37,14 +36,32 @@ features = ["dev"]

[tool.hatch.envs.default.scripts]
style = [
"ruff {args:.}",
"black --check --diff {args:.}"
"ruff {args:../..}",
]
fmt = [
"black {args:.}",
"ruff --fix {args:.}"
"ruff format {args:../..}",
"ruff --fix {args:../..}"
]

[tool.hatch.version]
path = "package.json"
pattern = "\"version\": \"(?P<version>.+?)\""

[tool.ruff]
extend-include = ["*.ipynb"]
# allow long lines, allow end in ;, allow useless expressions
ignore = ["E501", "E703", "B018"]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]

0 comments on commit a3b78fe

Please sign in to comment.