Skip to content

Commit cdea3ac

Browse files
committed
Minor refactoring for legibility, plus fix some import statements.
1 parent 104c573 commit cdea3ac

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

nbresuse/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
from tornado import ioloop
42

53
from nbresuse.config import ResourceUseDisplay

nbresuse/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from traitlets import Bool
24
from traitlets import default
35
from traitlets import Dict
@@ -14,6 +16,7 @@
1416
except ImportError:
1517
from .utils import Callable
1618

19+
1720
class PSUtilMetric(TraitType):
1821
"""A trait describing the format to specify a metric from the psutil package"""
1922

@@ -28,6 +31,7 @@ def validate(self, obj, value):
2831
return value
2932
self.error(obj, value)
3033

34+
3135
class ResourceUseDisplay(Configurable):
3236
"""
3337
Holds server-side configuration for nbresuse

nbresuse/metrics.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ def __init__(self, nbapp: NotebookApp):
1111
self.config = nbapp.web_app.settings["nbresuse_display_config"]
1212
self.nbapp = nbapp
1313

14+
def get_process_metric_value(self, process, name, kwargs, attribute=None):
15+
try:
16+
# psutil.Process methods will either return...
17+
metric_value = getattr(process, name)(**kwargs)
18+
if attribute is not None: # ... a named tuple
19+
return getattr(metric_value, attribute)
20+
else: # ... or a number
21+
return metric_value
22+
# Avoid littering logs with stack traces
23+
# complaining about dead processes
24+
except BaseException:
25+
return 0
26+
1427
def process_metric(self, name, kwargs={}, attribute=None):
1528
if psutil is None:
1629
return None
1730
else:
1831
current_process = psutil.Process()
1932
all_processes = [current_process] + current_process.children(recursive=True)
2033

21-
def get_process_metric(process, name, kwargs, attribute=None):
22-
try:
23-
# psutil.Process methods will either return...
24-
metric_value = getattr(process, name)(**kwargs)
25-
if attribute is not None: # ... a named tuple
26-
return getattr(metric_value, attribute)
27-
else: # ... or a number
28-
return metric_value
29-
# Avoid littering logs with stack traces
30-
# complaining about dead processes
31-
except BaseException:
32-
return 0
33-
34-
process_metric_value = lambda process: get_process_metric(
34+
process_metric_value = lambda process: self.get_process_metric_value(
3535
process, name, kwargs, attribute
3636
)
3737

0 commit comments

Comments
 (0)