Skip to content

Commit

Permalink
Use functools for codeTiming decorator. (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjarrett committed Nov 8, 2023
1 parent 6a546dd commit d803a71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions armi/utils/codeTiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import copy
import os
import time
import functools


def timed(*args):
Expand All @@ -36,9 +37,7 @@ def mymethod2(stuff)
"""

def time_decorator(func):
time_decorator.__doc__ = func.__doc__
time_decorator.__name__ = func.__name__

@functools.wraps(func)
def time_wrapper(*args, **kwargs):
generated_name = "::".join(
[
Expand Down
14 changes: 14 additions & 0 deletions armi/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
getPreviousTimeNode,
getCumulativeNodeNum,
hasBurnup,
codeTiming,
)


Expand Down Expand Up @@ -154,6 +155,19 @@ def test_classesInHierarchy(self):
self.assertGreater(len(r.core.getAssemblies()), 50)
self.assertGreater(len(r.core.getBlocks()), 200)

def test_codeTiming(self):
"""
Test that codeTiming preserves function attributes when it wraps a function
"""

@codeTiming.timed
def testFunc():
"""Test function docstring"""
pass

self.assertEqual(getattr(testFunc, "__doc__"), "Test function docstring")
self.assertEqual(getattr(testFunc, "__name__"), "testFunc")


class CyclesSettingsTests(unittest.TestCase):
"""
Expand Down
1 change: 1 addition & 0 deletions doc/release/0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ What's new in ARMI
#. Removed all old ARMI requirements, to start the work fresh. (`PR#1438 <https://github.com/terrapower/armi/pull/1438>`_)
#. Downgrading Draft PRs as policy. (`PR#1444 <https://github.com/terrapower/armi/pull/1444>`_)
#. Attempt to set representative block number densities by component if possible. (`PR#1412 <https://github.com/terrapower/armi/pull/1412>`_)
#. Use functools to preserve function attributes when wrapping with codeTiming.timed (`PR#1466 <https://github.com/terrapower/armi/pull/1466>`_)
#. TBD

Bug fixes
Expand Down

0 comments on commit d803a71

Please sign in to comment.