Skip to content

Commit

Permalink
grass.jupyter: Add docstrings, f-strings, fix var names, imports (#2200)
Browse files Browse the repository at this point in the history
* Add missing docstrings.
* Reorder imports (with both pylint and isort).
* Use full-word variable names.
* Use f-strings if possible. Here it replaces smart, but hard to decode, dictionary usage in format, by simple, although longer, direct use of the dictionary.
  • Loading branch information
wenzeslaus committed Feb 13, 2022
1 parent 0129d7f commit ffc874d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion python/grass/benchmark/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"""Basic functions for benchmarking modules"""

import shutil
import random
import shutil
from types import SimpleNamespace

import grass.script as gs
Expand Down
6 changes: 5 additions & 1 deletion python/grass/jupyter/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.

"""2D rendering and display functionality"""

import os
import shutil
import tempfile

import grass.script as gs

from .region import RegionManagerFor2D
Expand Down Expand Up @@ -160,6 +163,7 @@ def wrapper(**kwargs):

def show(self):
"""Displays a PNG image of map"""
from IPython.display import Image
# Lazy import to avoid an import-time dependency on IPython.
from IPython.display import Image # pylint: disable=import-outside-toplevel

return Image(self._filename)
4 changes: 3 additions & 1 deletion python/grass/jupyter/interact_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
import tempfile
import weakref
from pathlib import Path

import grass.script as gs

from .display import GrassRenderer
from .region import RegionManagerForInteractiveMap
from .utils import (
estimate_resolution,
get_location_proj_string,
get_region,
reproject_region,
setup_location,
)
from .region import RegionManagerForInteractiveMap


class InteractiveMap:
Expand Down
38 changes: 22 additions & 16 deletions python/grass/jupyter/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.

"""Manage computational or display region settings for display (render) classes."""

import grass.script as gs
from grass.exceptions import CalledModuleError

from .utils import (
get_location_proj_string,
get_map_name_from_d_command,
get_region,
reproject_region,
get_map_name_from_d_command,
)


class RegionManagerForInteractiveMap:
"""Region manager for an interactive map (gets region from raster and vector)"""

def __init__(self, use_region, saved_region, src_env, tgt_env):
"""Manages region during rendering for interactive map.
Expand All @@ -40,9 +44,7 @@ def __init__(self, use_region, saved_region, src_env, tgt_env):

@property
def bbox(self):
"""Bbox property for accessing maximum
bounding box of all rendered layers.
"""
"""Bbox property for accessing maximum bounding box of all rendered layers."""
return self._bbox

def set_region_from_raster(self, raster):
Expand Down Expand Up @@ -91,21 +93,23 @@ def set_bbox_vector(self, vector):

def _set_bbox(self, env):
bbox = gs.parse_command("g.region", flags="bg", env=env)
s = float(bbox["ll_s"])
w = float(bbox["ll_w"])
n = float(bbox["ll_n"])
e = float(bbox["ll_e"])
if self._bbox[0][0] > s:
self._bbox[0][0] = s
if self._bbox[0][1] > w:
self._bbox[0][1] = w
if self._bbox[1][0] < n:
self._bbox[1][0] = n
if self._bbox[1][1] < e:
self._bbox[1][1] = e
south = float(bbox["ll_s"])
west = float(bbox["ll_w"])
north = float(bbox["ll_n"])
east = float(bbox["ll_e"])
if self._bbox[0][0] > south:
self._bbox[0][0] = south
if self._bbox[0][1] > west:
self._bbox[0][1] = west
if self._bbox[1][0] < north:
self._bbox[1][0] = north
if self._bbox[1][1] < east:
self._bbox[1][1] = east


class RegionManagerFor2D:
"""Region manager for 2D displays (gets region from display commands)"""

def __init__(self, use_region, saved_region, env):
"""Manages region during rendering.
Expand Down Expand Up @@ -176,6 +180,8 @@ def set_region_from_command(self, module, **kwargs):


class RegionManagerFor3D:
"""Region manager for 3D displays (gets region from m.nviz.image command)"""

def __init__(self, use_region, saved_region):
"""Manages region during rendering.
Expand Down
2 changes: 1 addition & 1 deletion python/grass/jupyter/render3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import tempfile

import grass.script as gs
from grass.jupyter import GrassRenderer

from .display import GrassRenderer
from .region import RegionManagerFor3D


Expand Down
5 changes: 3 additions & 2 deletions python/grass/jupyter/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def active(self):
return self._finalizer.alive


_global_session_handle = None
# Pylint 2.12.2 identifies this a constant (although it is not), so it wants uppercase.
_global_session_handle = None # pylint: disable=invalid-name


def init(path, location=None, mapset=None, grass_path=None):
Expand Down Expand Up @@ -158,7 +159,7 @@ def init(path, location=None, mapset=None, grass_path=None):
:param str location: name of GRASS location within the database
:param str mapset: name of mapset within location
"""
global _global_session_handle # pylint: disable=global-statement
global _global_session_handle # pylint: disable=global-statement,invalid-name
if not _global_session_handle or not _global_session_handle.active:
# Create a GRASS session.
gsetup.init(path, location=location, mapset=mapset, grass_path=grass_path)
Expand Down
3 changes: 2 additions & 1 deletion python/grass/jupyter/testsuite/grassrenderer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#############################################################################

import os
import unittest
import sys
import unittest
from pathlib import Path

import grass.jupyter as gj
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
Expand Down
3 changes: 2 additions & 1 deletion python/grass/jupyter/testsuite/interactivemap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@


import os
import unittest
import sys
import unittest
from pathlib import Path

import grass.jupyter as gj
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
Expand Down
3 changes: 2 additions & 1 deletion python/grass/jupyter/testsuite/test_render3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"""Test of 3D renderer"""

import os
import unittest
import sys
import unittest
from pathlib import Path

import grass.jupyter as gj
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
Expand Down
12 changes: 9 additions & 3 deletions python/grass/jupyter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
#
# PURPOSE: This module contains utility functions for InteractiveMap.
#
# COPYRIGHT: (C) 2021 Caitlin Haedrich, and by the GRASS Development Team
# COPYRIGHT: (C) 2021-2022 Caitlin Haedrich, and by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.

"""Utility functions warpping existing processes in a suitable way"""

import os

import grass.script as gs


def get_region(env=None):
"""Returns current computational region as dictionary.
Adds long key names.
Additionally, it adds long key names.
"""
region = gs.region(env=env)
region["east"] = region["e"]
Expand Down Expand Up @@ -43,7 +47,9 @@ def reproject_region(region, from_proj, to_proj):
:return dict region: reprojected region as a dictionary with long key names
"""
region = region.copy()
proj_input = "{east} {north}\n{west} {south}".format(**region)
proj_input = (
f"{region['east']} {region['north']}\n{region['west']} {region['south']}"
)
proc = gs.start_command(
"m.proj",
input="-",
Expand Down

0 comments on commit ffc874d

Please sign in to comment.