Skip to content

Commit

Permalink
python: Update black to 24.4.0 (OSGeo#3545)
Browse files Browse the repository at this point in the history
* CI(black): Upgrade black to 24.3.0 (including Jupyter notebooks)

* python: Require black version 24

* python: Update black target-version

* python(black): Use extend-exclude instead of overriding exclude

* python: Use black's default include pattern to format Jupyter notebooks

* pre-commit: Update black hook to 24.3.0 (including Jupyter notebooks)

* python: Format python files with Black 24.3.0

* python: Format Jupyter notebooks with Black 24.3.0

* python: Format new files with black

* Update black to 24.4.0

* wxGUI/wxplot: Format profile with black
  • Loading branch information
echoix authored Apr 23, 2024
1 parent 0b248ef commit f8115df
Show file tree
Hide file tree
Showing 186 changed files with 410 additions and 197 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- os: ubuntu-22.04
python-version: "3.10"
min-python-version: "3.7"
black-version: "23.1.0"
black-version: "24.4.0"
flake8-version: "3.9.2"
pylint-version: "2.12.2"
bandit-version: "1.7.8"
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
run: python -m pip install --upgrade pip

- name: Install Black only
run: pip install black==${{ matrix.black-version }}
run: pip install black[jupyter]==${{ matrix.black-version }}

- name: Run Black
run: black .
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ repos:
rev: v0.33.0
hooks:
- id: markdownlint
- repo: https://github.com/psf/black
rev: 23.1.0
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.0
hooks:
- id: black
- id: black-jupyter
exclude: |
(?x)^(
python/libgrass_interface_generator/
Expand Down
125 changes: 84 additions & 41 deletions doc/notebooks/hydrology.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
"\n",
"# Add a raster, vector and legend to the map\n",
"elev_map.d_rast(map=\"elevation\")\n",
"elev_map.d_legend(raster=\"elevation\", at=(65, 90, 85, 88), fontsize=12, flags=\"b\", title=\"DTM\")\n",
"elev_map.d_legend(\n",
" raster=\"elevation\", at=(65, 90, 85, 88), fontsize=12, flags=\"b\", title=\"DTM\"\n",
")\n",
"\n",
"# Display map\n",
"elev_map.show()"
Expand All @@ -90,9 +92,27 @@
"metadata": {},
"outputs": [],
"source": [
"gs.run_command(\"r.fill.dir\", input=\"elevation\", output=\"elev_fill1\", direction=\"dir1\", areas=\"area1\")\n",
"gs.run_command(\"r.fill.dir\", input=\"elev_fill1\", output=\"elev_fill2\", direction=\"dir2\", areas=\"area2\")\n",
"gs.run_command(\"r.fill.dir\", input=\"elev_fill2\", output=\"elev_fill3\", direction=\"dir3\", areas=\"area3\")\n",
"gs.run_command(\n",
" \"r.fill.dir\",\n",
" input=\"elevation\",\n",
" output=\"elev_fill1\",\n",
" direction=\"dir1\",\n",
" areas=\"area1\",\n",
")\n",
"gs.run_command(\n",
" \"r.fill.dir\",\n",
" input=\"elev_fill1\",\n",
" output=\"elev_fill2\",\n",
" direction=\"dir2\",\n",
" areas=\"area2\",\n",
")\n",
"gs.run_command(\n",
" \"r.fill.dir\",\n",
" input=\"elev_fill2\",\n",
" output=\"elev_fill3\",\n",
" direction=\"dir3\",\n",
" areas=\"area3\",\n",
")\n",
"gs.mapcalc(\"depr_bin = if((elevation-elev_fill3) < 0., 1, null())\")\n",
"gs.run_command(\"r.colors\", map=\"depr_bin\", color=\"blues\")"
]
Expand Down Expand Up @@ -129,13 +149,15 @@
"metadata": {},
"outputs": [],
"source": [
"gs.run_command(\"r.watershed\", \n",
" elevation=\"elevation@PERMANENT\",\n",
" drainage=\"drainage\", # Drainage Direction\n",
" accumulation=\"flowacc\", # Flow Accumulation\n",
" basin=\"watersheds\",\n",
" stream=\"streams\",\n",
" threshold=80000)\n",
"gs.run_command(\n",
" \"r.watershed\",\n",
" elevation=\"elevation@PERMANENT\",\n",
" drainage=\"drainage\", # Drainage Direction\n",
" accumulation=\"flowacc\", # Flow Accumulation\n",
" basin=\"watersheds\",\n",
" stream=\"streams\",\n",
" threshold=80000,\n",
")\n",
"\n",
"# Convert streams raster to vector\n",
"gs.run_command(\"r.to.vect\", input=\"streams\", output=\"streams\", type=\"line\")"
Expand All @@ -160,7 +182,7 @@
"\n",
"# We can modify with color table for rasters with `r.colors`.\n",
"# Note that if the raster is located in a different mapset (for example,\n",
"# elevation is in PERMANENT, not user1), the `r.colors` will not change \n",
"# elevation is in PERMANENT, not user1), the `r.colors` will not change\n",
"# the color in InteractiveMap.\n",
"gs.run_command(\"r.colors\", map=\"drainage\", color=\"aspect\")\n",
"\n",
Expand Down Expand Up @@ -193,13 +215,19 @@
"outputs": [],
"source": [
"# Count cells in each watershed\n",
"gs.run_command(\"r.stats.zonal\", base=\"watersheds\", cover=\"elevation\", method=\"count\", output=\"watersheds_count\")\n",
"gs.run_command(\n",
" \"r.stats.zonal\",\n",
" base=\"watersheds\",\n",
" cover=\"elevation\",\n",
" method=\"count\",\n",
" output=\"watersheds_count\",\n",
")\n",
"\n",
"# Get projection resolution\n",
"proj=gs.parse_command(\"g.region\", flags=\"m\")\n",
"proj = gs.parse_command(\"g.region\", flags=\"m\")\n",
"\n",
"# Multiply N-S resollution by E-W resolution to get cell area\n",
"cell_area = float(proj[\"nsres\"])*float(proj[\"ewres\"])\n",
"cell_area = float(proj[\"nsres\"]) * float(proj[\"ewres\"])\n",
"\n",
"# Calculate watersheds areas and convert from m2 to km2\n",
"gs.mapcalc(\"'watershed_area' = float('watersheds_count'*{})/1000000\".format(cell_area))"
Expand All @@ -220,18 +248,20 @@
"source": [
"# Display a map of watershed areas.\n",
"gs.run_command(\"r.colors\", map=\"watershed_area\", color=\"plasma\")\n",
" \n",
"\n",
"watershed_map = gj.Map()\n",
"watershed_map.d_rast(map=\"watershed_area\")\n",
"watershed_map.d_legend(raster=\"watershed_area\",\n",
" bgcolor=\"none\",\n",
" color=\"black\",\n",
" border_color=\"none\",\n",
" at=(3, 40, 84, 88),\n",
" lines=2,\n",
" fontsize=15,\n",
" title=\"Area\",\n",
" units=\" km2\")\n",
"watershed_map.d_legend(\n",
" raster=\"watershed_area\",\n",
" bgcolor=\"none\",\n",
" color=\"black\",\n",
" border_color=\"none\",\n",
" at=(3, 40, 84, 88),\n",
" lines=2,\n",
" fontsize=15,\n",
" title=\"Area\",\n",
" units=\" km2\",\n",
")\n",
"watershed_map.show()"
]
},
Expand Down Expand Up @@ -265,7 +295,14 @@
"# Display slope map\n",
"slope_map = gj.Map()\n",
"slope_map.d_rast(map=\"slope\")\n",
"slope_map.d_legend(raster=\"slope\", at=(65, 90, 85, 90), fontsize=15, flags=\"b\", title=\"Slope\", units=\"°\")\n",
"slope_map.d_legend(\n",
" raster=\"slope\",\n",
" at=(65, 90, 85, 90),\n",
" fontsize=15,\n",
" flags=\"b\",\n",
" title=\"Slope\",\n",
" units=\"°\",\n",
")\n",
"slope_map.show()"
]
},
Expand All @@ -284,14 +321,16 @@
"source": [
"separator = \"|\"\n",
"\n",
"columns = defaultdict(list) # each value in each column is appended to a list\n",
"columns = defaultdict(list) # each value in each column is appended to a list\n",
"\n",
"text = gs.read_command(\"r.univar\", map=\"elevation\", zones=\"watersheds\", separator=separator, flags=\"t\")\n",
"text = gs.read_command(\n",
" \"r.univar\", map=\"elevation\", zones=\"watersheds\", separator=separator, flags=\"t\"\n",
")\n",
"reader = csv.DictReader(text.splitlines(), delimiter=separator)\n",
"for row in reader: # read a row as {column1: value1, column2: value2,...}\n",
" for (k,v) in row.items(): # go over each column name and value \n",
" columns[k].append(v) # append the value into the appropriate list\n",
" # based on column name k\n",
"for row in reader: # read a row as {column1: value1, column2: value2,...}\n",
" for k, v in row.items(): # go over each column name and value\n",
" columns[k].append(v) # append the value into the appropriate list\n",
" # based on column name k\n",
"\n",
"watersheds = columns[\"zone\"]\n",
"means = np.array(columns[\"mean\"], dtype=np.float32)\n",
Expand Down Expand Up @@ -333,7 +372,9 @@
"outputs": [],
"source": [
"# Convert to vector\n",
"gs.run_command(\"r.to.vect\", flags=\"s\", input=\"watersheds\", output=\"watersheds_vector\", type=\"area\")"
"gs.run_command(\n",
" \"r.to.vect\", flags=\"s\", input=\"watersheds\", output=\"watersheds_vector\", type=\"area\"\n",
")"
]
},
{
Expand All @@ -345,14 +386,16 @@
"# Display\n",
"watershed_vect_map = gj.Map()\n",
"watershed_vect_map.d_rast(map=\"elevation\")\n",
"watershed_vect_map.d_vect(map=\"watersheds_vector\",\n",
" fill_color=\"none\",\n",
" width=1.5,\n",
" color=\"black\",\n",
" attribute_column=\"value\",\n",
" label_bgcolor=\"black\",\n",
" label_color=\"white\",\n",
" label_size=10)\n",
"watershed_vect_map.d_vect(\n",
" map=\"watersheds_vector\",\n",
" fill_color=\"none\",\n",
" width=1.5,\n",
" color=\"black\",\n",
" attribute_column=\"value\",\n",
" label_bgcolor=\"black\",\n",
" label_color=\"white\",\n",
" label_size=10,\n",
")\n",
"watershed_vect_map.show()"
]
}
Expand Down
8 changes: 6 additions & 2 deletions doc/notebooks/jupyter_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"outputs": [],
"source": [
"gs.parse_command(\"g.region\", raster=\"lakes\", flags=\"pg\")\n",
"gs.run_command(\"r.buffer\", input=\"lakes\", output=\"lakes_buff\", distances=[60, 120, 240, 500])\n",
"gs.run_command(\n",
" \"r.buffer\", input=\"lakes\", output=\"lakes_buff\", distances=[60, 120, 240, 500]\n",
")\n",
"\n",
"# Start a Map\n",
"r_buffer_map = gj.Map()\n",
Expand Down Expand Up @@ -101,7 +103,9 @@
"v_buffer_map = gj.Map()\n",
"\n",
"# Add vector layers and legend\n",
"v_buffer_map.d_vect(map=\"boundary_state\", fill_color=\"#5A91ED\", legend_label=\"State boundary\")\n",
"v_buffer_map.d_vect(\n",
" map=\"boundary_state\", fill_color=\"#5A91ED\", legend_label=\"State boundary\"\n",
")\n",
"v_buffer_map.d_vect(map=\"buffer\", fill_color=\"#F8766D\", legend_label=\"Inner portion\")\n",
"v_buffer_map.d_legend_vect(at=(10, 35))\n",
"\n",
Expand Down
20 changes: 10 additions & 10 deletions doc/notebooks/jupyter_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"source": [
"# Then, we return to the first instance and continue to modify and display it\n",
"# Notice that layers a drawn in the order they are added\n",
"example_map.run(\"d.vect\", map = \"zipcodes\", color=\"red\", fill_color=\"none\")\n",
"example_map.run(\"d.vect\", map=\"zipcodes\", color=\"red\", fill_color=\"none\")\n",
"example_map.show()"
]
},
Expand Down Expand Up @@ -277,11 +277,11 @@
"metadata": {},
"outputs": [],
"source": [
"import folium \n",
"import folium\n",
"\n",
"\n",
"# Create a map\n",
"m = folium.Map(location=[35.761168,-78.668271], zoom_start=13)\n",
"m = folium.Map(location=[35.761168, -78.668271], zoom_start=13)\n",
"\n",
"# Create and add elevation layer to map\n",
"gj.Raster(\"elevation\", opacity=0.5).add_to(m)\n",
Expand All @@ -291,7 +291,7 @@
"tooltip = \"Click me!\"\n",
"# and add a marker\n",
"folium.Marker(\n",
" [35.781608,-78.675800], popup=\"<i>Point of Interest</i>\", tooltip=tooltip\n",
" [35.781608, -78.675800], popup=\"<i>Point of Interest</i>\", tooltip=tooltip\n",
").add_to(m)\n",
"\n",
"# Display map\n",
Expand All @@ -313,10 +313,10 @@
"metadata": {},
"outputs": [],
"source": [
"import ipyleaflet \n",
"import ipyleaflet\n",
"\n",
"# Create map\n",
"m = ipyleaflet.Map(center=[35.761168,-78.668271], zoom=13)\n",
"m = ipyleaflet.Map(center=[35.761168, -78.668271], zoom=13)\n",
"\n",
"# Create and add elevation layer to map\n",
"gj.Raster(\"elevation\", opacity=0.5).add_to(m)\n",
Expand All @@ -325,12 +325,12 @@
"# Like make a tooltip\n",
"title = \"Click me!\"\n",
"# and add a marker\n",
"marker = ipyleaflet.Marker(name='marker', location=(35.781608,-78.675800), title=title)\n",
"marker = ipyleaflet.Marker(name=\"marker\", location=(35.781608, -78.675800), title=title)\n",
"\n",
"# Add the marker to the map\n",
"m.add(marker)\n",
"\n",
"control = ipyleaflet.LayersControl(position='topright')\n",
"control = ipyleaflet.LayersControl(position=\"topright\")\n",
"m.add(control)\n",
"m"
]
Expand Down Expand Up @@ -451,9 +451,9 @@
"metadata": {},
"outputs": [],
"source": [
"series = gj.SeriesMap(height = 500)\n",
"series = gj.SeriesMap(height=500)\n",
"series.add_rasters([\"elevation\", \"elevation_shade\", \"slope\"])\n",
"#series.add_vectors([\"streams\", \"streets\", \"viewpoints\"])\n",
"# series.add_vectors([\"streams\", \"streets\", \"viewpoints\"])\n",
"series.d_vect(map=\"streets\")\n",
"series.d_barscale()\n",
"series.show() # Create Slider"
Expand Down
1 change: 1 addition & 0 deletions doc/notebooks/parallelization_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"import time\n",
"from grass.pygrass.modules.grid import GridModule\n",
"import time\n",
"\n",
"start = time.time()\n",
"grid = GridModule(\n",
" \"v.surf.idw\",\n",
Expand Down
12 changes: 9 additions & 3 deletions doc/notebooks/scripting_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
"outputs": [],
"source": [
"gs.parse_command(\"g.region\", raster=\"lakes\", flags=\"pg\")\n",
"gs.run_command(\"r.buffer\", input=\"lakes\", output=\"lakes_buff\", distances=[60, 120, 240, 500])\n",
"gs.run_command(\n",
" \"r.buffer\", input=\"lakes\", output=\"lakes_buff\", distances=[60, 120, 240, 500]\n",
")\n",
"gs.run_command(\"d.erase\")\n",
"gs.run_command(\"d.rast\", map=\"lakes_buff\")\n",
"gs.run_command(\"d.legend\", raster=\"lakes_buff\", range=(2, 5), at=(80, 100, 2, 10))\n",
Expand Down Expand Up @@ -102,8 +104,12 @@
"gs.parse_command(\"g.region\", vector=\"boundary_state\", flags=\"pg\")\n",
"gs.run_command(\"d.erase\") # erase the display before drawing again\n",
"!rm -f $GRASS_LEGEND_FILE # and remove the legend file\n",
"gs.run_command(\"d.vect\", map=\"boundary_state\", fill_color=\"#5A91ED\", legend_label=\"State boundary\")\n",
"gs.run_command(\"d.vect\", map=\"buffer\", fill_color=\"#F8766D\", legend_label=\"Inner portion\")\n",
"gs.run_command(\n",
" \"d.vect\", map=\"boundary_state\", fill_color=\"#5A91ED\", legend_label=\"State boundary\"\n",
")\n",
"gs.run_command(\n",
" \"d.vect\", map=\"buffer\", fill_color=\"#F8766D\", legend_label=\"Inner portion\"\n",
")\n",
"gs.run_command(\"d.legend.vect\", at=(10, 35))\n",
"Image(filename=\"map.png\")"
]
Expand Down
Loading

0 comments on commit f8115df

Please sign in to comment.