Commit MetaInfo

Revision0cd962ec592faaa6e6045e5229855f1e255221f2 (tree)
Time2022-07-17 20:30:45
AuthorAntoon Pardon <aej@pard...>
CommiterAntoon Pardon

Log Message

The pass_defaults_parameter decorator added

Change Summary

Incremental Difference

diff -r dfd25e05df71 -r 0cd962ec592f source/decorators.py
--- a/source/decorators.py Mon Jul 11 15:41:48 2022 +0200
+++ b/source/decorators.py Sun Jul 17 13:30:45 2022 +0200
@@ -4,9 +4,16 @@
44 from functools import wraps
55 from copy import copy
66
7+default = object()
8+"""
9+This value kan be passed as an argument, where the functions provides a default value.
10+If you used the pass_defaults decorator, the function will use its default for that
11+parameter
12+"""
13+
714 def copy_defaults(func):
815 """
9- first approximation for copying default argument values.
16+ This decorator makes that defaults values are copied on a call.
1017 """
1118
1219 signature = signature_of(func)
@@ -29,3 +36,20 @@
2936 return func(*newargs, **kwds)
3037
3138 return wrapper
39+
40+def pass_defaults(func):
41+ """
42+ This decorator allows to pass a specific "default" value, to
43+ have the function use the default value
44+ """
45+
46+ @wraps(func)
47+ def wrapper(*args):
48+ try:
49+ default_index = args.index(default)
50+ newargs = args[:default_index]
51+ except ValueError:
52+ newargs = args
53+ return func(*newargs)
54+
55+ return wrapper
diff -r dfd25e05df71 -r 0cd962ec592f source/release.cfg
--- a/source/release.cfg Mon Jul 11 15:41:48 2022 +0200
+++ b/source/release.cfg Sun Jul 17 13:30:45 2022 +0200
@@ -1,3 +1,3 @@
1-version = '0.01.09d'
2-modified = '2022-07-11 @ 15:38:48'
1+version = '0.01.10k'
2+modified = '2022-07-17 @ 13:30:45'
33 installed = '*********************'
diff -r dfd25e05df71 -r 0cd962ec592f tests/decorators.py
--- a/tests/decorators.py Mon Jul 11 15:41:48 2022 +0200
+++ b/tests/decorators.py Sun Jul 17 13:30:45 2022 +0200
@@ -1,8 +1,8 @@
11 #pylint: disable=invalid-name,no-self-argument,missing-function-docstring,(missing-module-docstring,dangerous-default-value)
22
3-from unittest import TestCase, main as unitmain
3+from unittest import TestCase, main as unitmain, skip
44
5-from AGPlib.decorators import copy_defaults
5+from AGPlib.decorators import copy_defaults, pass_defaults, default
66
77 @copy_defaults
88 def append_range(nr, lst = []):
@@ -13,12 +13,19 @@
1313 def ident(a = 1):
1414 return a
1515
16-class InspectCase(TestCase):
16+df2 = 2
17+df3 = 4
18+
19+@pass_defaults
20+def multi_return(p1, p2 = df2, p3 = df3):
21+ return p1, p2, p3
22+
23+class InspectCopyDefaults(TestCase):
1724 """
18- class for inspecting decorators
25+ class for testing copy_defaults
1926 """
2027
21- def test_copy_defaults(s):
28+ def test_normal_usage(s):
2229 s.assertEqual(1, ident())
2330 s.assertEqual([0, 1, 2], append_range(3))
2431 s.assertEqual([0, 1, 2, 3, 4], append_range(5))
@@ -37,4 +44,33 @@
3744 with s.assertRaises(TypeError):
3845 append_range()
3946
47+class InspectPassDefaults(TestCase):
48+ """
49+ class for testing copy_defaults
50+ """
51+
52+ def test_no_defaults(s):
53+ s.assertEqual((3, 5, 8), multi_return(3, 5, 8))
54+
55+ def test_normal_default(s):
56+ s.assertEqual((3, 5, df3), multi_return(3, 5))
57+ s.assertEqual((3, df2, df3), multi_return(3))
58+
59+ def test_default_value_behind(s):
60+ s.assertEqual((3, 5, df3), multi_return(3, 5, default))
61+ s.assertEqual((3, df2, df3), multi_return(3, default, default))
62+
63+ def test_too_few_arguments(s):
64+ # pylint: disable=no-value-for-parameter
65+ with s.assertRaises(TypeError):
66+ multi_return()
67+
68+ def test_default_value_too_soon(s):
69+ with s.assertRaises(TypeError):
70+ multi_return(default)
71+
72+ @skip("This is beyond normal default behaviour")
73+ def test_default_value_in_the_middle(s):
74+ s.assertEqual((3, df2, 8), multi_return(3, default, 8))
75+
4076 unitmain()
Show on old repository browser