Skip to content

Commit 980e938

Browse files
authored
style: Format Python code in docs for ruff 0.17 (#8131)
CI(deps): Format Python code in docs and keep datetime import style for ruff 0.17
1 parent be12151 commit 980e938

19 files changed

Lines changed: 198 additions & 159 deletions

File tree

‎display/displaydrivers.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ import sys
3535
import grass.script as gs
3636
from grass.script import task as gtask
3737

38-
os.environ['GRASS_RENDER_IMMEDIATE'] = 'default'
39-
os.environ['GRASS_RENDER_FILE'] = 'output.png'
38+
os.environ["GRASS_RENDER_IMMEDIATE"] = "default"
39+
os.environ["GRASS_RENDER_FILE"] = "output.png"
4040

4141
cmd, dcmd = gtask.cmdstring_to_tuple(sys.argv[1])
4242

43-
gs.run_command('d.text', text="Test of GRASS_RENDER_COMMAND redirection")
43+
gs.run_command("d.text", text="Test of GRASS_RENDER_COMMAND redirection")
4444

45-
os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
45+
os.environ["GRASS_RENDER_FILE_READ"] = "TRUE"
4646
gs.run_command(cmd, **dcmd)
4747
```
4848

‎doc/development/style_guide.md‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ def func(arg1, arg2):
217217
218218
Example:
219219
220-
>>> a=1
221-
>>> b=2
222-
>>> func(a,b)
220+
>>> a = 1
221+
>>> b = 2
222+
>>> func(a, b)
223223
True
224224
"""
225225

226226
if arg1 == arg2:
227-
raise ValueError('arg1 must not be equal to arg2')
227+
raise ValueError("arg1 must not be equal to arg2")
228228

229229
return True
230230
```
@@ -575,7 +575,9 @@ If different subprocesses need different regions, use different environments:
575575

576576
```python
577577
with gs.RegionManager(raster=input_raster, env=os.environ.copy()) as manager:
578-
gs.run_command("r.slope.aspect", elevation=input_raster, slope=slope, env=manager.env)
578+
gs.run_command(
579+
"r.slope.aspect", elevation=input_raster, slope=slope, env=manager.env
580+
)
579581
```
580582

581583
This approach makes the computational region completely safe for parallel
@@ -587,7 +589,7 @@ If you can't use a context manager, you can use `gs.use_temp_region()` and
587589
```python
588590
gs.use_temp_region() # From now on, use a separate region in the script.
589591
# Set the computational region with g.region as needed.
590-
grass.run_command('g.region', raster='input')
592+
grass.run_command("g.region", raster="input")
591593
gs.del_temp_region()
592594
# Original region applies now.
593595
```
@@ -689,7 +691,6 @@ def remove(name):
689691
)
690692

691693

692-
693694
def main():
694695
temporary = gs.append_node_pid("tmp_mapcalc")
695696
atexit.register(remove, temporary)
@@ -707,13 +708,13 @@ Use gs.findfile() when there is a need to test if a map exists.
707708

708709
```python
709710
# test for input raster map
710-
result = gs.find_file(map_name, element='raster')
711-
if not result['file']:
711+
result = gs.find_file(map_name, element="raster")
712+
if not result["file"]:
712713
gs.fatal(_("Raster map <{}> not found").format(map_name))
713714

714715
# test for input vector map
715-
result = gs.find_file(map_name, element='vector')
716-
if not result['file']:
716+
result = gs.find_file(map_name, element="vector")
717+
if not result["file"]:
717718
gs.fatal(_("Vector map <{}> not found").format(map_name))
718719
```
719720

@@ -922,7 +923,7 @@ def main():
922923
try:
923924
import pandas as pd # noqa: E402
924925
except ModuleNotFoundError:
925-
gs.fatal(_("Pandas library not installed"))
926+
gs.fatal(_("Pandas library not installed"))
926927
```
927928

928929
#### Tool name
@@ -1011,9 +1012,9 @@ instead of constructing string from several parts (bad example):
10111012
```python
10121013
# don't do this
10131014
if ...:
1014-
maplabel = 'raster map'
1015+
maplabel = "raster map"
10151016
else:
1016-
maplabel = '3D raster map'
1017+
maplabel = "3D raster map"
10171018
win.SetLabel(_("Name for new {} to create").format(maplabel))
10181019
```
10191020

@@ -1024,9 +1025,11 @@ string. The comment must start with GTC keyword and must be on a line before
10241025
string:
10251026

10261027
```python
1027-
self.bwizard = wx.Button(...,
1028+
self.bwizard = wx.Button(
1029+
...,
10281030
# GTC New location
1029-
label = _("N&ew"))
1031+
label=_("N&ew"),
1032+
)
10301033

10311034
# GTC %s will be replaced with name of current shell
10321035
gs.message(_("Running through {}").format(shellname))

‎doc/development/testing.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ environment. Mark such a test so it is run on its own:
167167

168168
```python
169169
@pytest.mark.needs_solo_run
170-
def test_something_which_changes_the_environment(xy_dataset_session):
171-
...
170+
def test_something_which_changes_the_environment(xy_dataset_session): ...
172171
```
173172

174173
## More information

‎doc/interfaces_overview.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ gs.create_project(path="path/to/my_project", epsg="3358")
6666

6767
# Initialize the GRASS session
6868
with gs.setup.init("path/to/my_project") as session:
69-
7069
# Run GRASS tools
7170
tools = Tools(session=session)
7271
tools.r_import(input="/path/to/elevation.tif", output="elevation")

‎doc/jupyter_intro.md‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ m.d_legend(
109109
at=(1, 50, 1, 8),
110110
title="Elevation (m)",
111111
border_color="none",
112-
flags="bt"
112+
flags="bt",
113113
)
114114

115115
# Add a scale bar to the map
@@ -161,12 +161,11 @@ elevation_3dmap.render(
161161
perspective=10,
162162
height=5000,
163163
resolution_fine=1,
164-
zexag=5
164+
zexag=5,
165+
)
166+
elevation_3dmap.overlay.d_legend(
167+
raster="landuse", at=(0, 30, 87, 100), flags="b", border_color="none"
165168
)
166-
elevation_3dmap.overlay.d_legend(raster="landuse",
167-
at=(0, 30, 87, 100),
168-
flags="b",
169-
border_color="none")
170169
elevation_3dmap.show()
171170
```
172171

@@ -184,10 +183,7 @@ maps and play a continuous loop.
184183
# Create a series of relief maps with different angles
185184
directions = [0, 90, 180, 270]
186185
for azimuth in directions:
187-
tools.r_relief(
188-
input="elevation",
189-
output=f"relief_{azimuth}",
190-
azimuth=azimuth)
186+
tools.r_relief(input="elevation", output=f"relief_{azimuth}", azimuth=azimuth)
191187
m = gj.SeriesMap()
192188
m.add_rasters(f"relief_{azimuth}" for azimuth in directions)
193189
m.d_vect(map="roads")
@@ -212,19 +208,16 @@ tools.g_region(n=226040, s=223780, e=639170, w=636190)
212208
tools.r_slope_aspect(elevation="elevation", dx="dx", dy="dy")
213209
# Compute overland flow
214210
tools.r_sim_water(
215-
flags="t",
216-
elevation="elevation",
217-
dx="dx",
218-
dy="dy",
219-
depth="depth",
220-
duration=30)
211+
flags="t", elevation="elevation", dx="dx", dy="dy", depth="depth", duration=30
212+
)
221213

222214
# Create a time series
223215
tools.t_create(
224216
output="depth",
225217
temporaltype="relative",
226218
title="Overland flow depth",
227-
description="Overland flow depth")
219+
description="Overland flow depth",
220+
)
228221
# Register the time series
229222
maps = [
230223
item["fullname"]
@@ -243,11 +236,13 @@ flow_map.add_raster_series("depth", values="0.025-1.25")
243236
flow_map.d_vect(map="streets")
244237

245238
# Add map features
246-
flow_map.d_legend(raster="depth.30",
247-
title="Depth [m]",
248-
flags="bt",
249-
border_color="none",
250-
at=(1, 50, 0, 5))
239+
flow_map.d_legend(
240+
raster="depth.30",
241+
title="Depth [m]",
242+
flags="bt",
243+
border_color="none",
244+
at=(1, 50, 0, 5),
245+
)
251246

252247
# Display the map
253248
flow_map.show()

‎doc/migration_to_grass_tools_python_api.md‎

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,7 @@ result = gs.list_strings("raster")
386386
Extract the *fullname* field from the JSON output:
387387

388388
```python
389-
result = [
390-
item["fullname"]
391-
for item in tools.g_list(type="raster", format="json")
392-
]
389+
result = [item["fullname"] for item in tools.g_list(type="raster", format="json")]
393390
```
394391

395392
The *list_pairs* returns tuples:
@@ -456,20 +453,16 @@ configure the *Tools* object accordingly.
456453
The *run_command* approach uses *errors="status"*:
457454

458455
```python
459-
returncode = gs.run_command(
460-
"r.mask.status",
461-
flags="t",
462-
errors="status"
463-
)
456+
returncode = gs.run_command("r.mask.status", flags="t", errors="status")
464457
# Returns: 1 (if no mask exists)
465458
```
466459

467460
Configure the *Tools* object to ignore errors and always return result objects:
468461

469462
```python
470463
tools_with_returncode = Tools(
471-
errors="ignore",
472-
consistent_return_value=True,
464+
errors="ignore",
465+
consistent_return_value=True,
473466
)
474467
result = tools_with_returncode.r_mask_status(flags="t")
475468
returncode = result.returncode
@@ -630,27 +623,21 @@ import grass.script as gs
630623
with gs.setup.init("path/to/project") as session:
631624
# Import raster
632625
gs.run_command(
633-
"r.import",
634-
input="/path/to/elevation.tif",
635-
output="elevation",
636-
env=session.env
626+
"r.import", input="/path/to/elevation.tif", output="elevation", env=session.env
637627
)
638-
628+
639629
# Set region
640630
gs.run_command("g.region", raster="elevation", env=session.env)
641-
631+
642632
# Calculate slope
643633
gs.run_command(
644-
"r.slope.aspect",
645-
elevation="elevation",
646-
slope="slope",
647-
env=session.env
634+
"r.slope.aspect", elevation="elevation", slope="slope", env=session.env
648635
)
649-
636+
650637
# Get region info
651638
region_info = gs.region(env=session.env)
652639
print(f"Rows: {region_info['rows']}")
653-
640+
654641
# List rasters
655642
rasters = gs.list_strings("raster", env=session.env)
656643
print(f"Rasters: {rasters}")
@@ -664,25 +651,22 @@ from grass.tools import Tools
664651

665652
with gs.setup.init("path/to/project") as session:
666653
tools = Tools(session=session)
667-
654+
668655
# Import raster
669656
tools.r_import(input="/path/to/elevation.tif", output="elevation")
670-
657+
671658
# Set region
672659
tools.g_region(raster="elevation")
673-
660+
674661
# Calculate slope
675662
tools.r_slope_aspect(elevation="elevation", slope="slope")
676-
663+
677664
# Get region info
678665
region_info = tools.g_region(flags="p", format="json")
679666
print(f"Rows: {region_info['rows']}")
680-
667+
681668
# List rasters
682-
rasters = [
683-
item["fullname"]
684-
for item in tools.g_list(type="raster", format="json")
685-
]
669+
rasters = [item["fullname"] for item in tools.g_list(type="raster", format="json")]
686670
print(f"Rasters: {rasters}")
687671
```
688672

0 commit comments

Comments
 (0)