@@ -1534,8 +1534,8 @@ def _replacer(data, key):
1534
1534
return key
1535
1535
1536
1536
1537
- def unpack_labeled_data (replace_names = None , replace_all_args = False , label_namer = None ,
1538
- positional_parameter_names = None ):
1537
+ def unpack_labeled_data (replace_names = None , replace_all_args = False ,
1538
+ label_namer = None , positional_parameter_names = None ):
1539
1539
"""
1540
1540
A decorator to add a 'data' kwarg to any a function. The signature
1541
1541
of the input function must include the ax argument at the first position ::
@@ -1547,19 +1547,24 @@ def foo(ax, *args, **kwargs)
1547
1547
Parameters
1548
1548
----------
1549
1549
replace_names : list of strings, optional, default: None
1550
- The list of parameter names which arguments should be replaced by `data[name]`. If None,
1551
- all arguments are replaced if they are included in `data`.
1550
+ The list of parameter names which arguments should be replaced by
1551
+ `data[name]`. If None, all arguments are replaced if they are
1552
+ included in `data`.
1552
1553
replace_all_args : bool, default: False
1553
- If True, all arguments in *args get replaced, even if they are not in replace_names.
1554
- NOTE: this should be used only when the order of the names depends on the number of *args.
1554
+ If True, all arguments in *args get replaced, even if they are not
1555
+ in replace_names.
1556
+ NOTE: this should be used only when the order of the names depends on
1557
+ the number of *args.
1555
1558
label_namer : string, optional, default: None
1556
- The name of the parameter which argument should be used as label, if label is not set. If
1557
- None, the label keyword argument is not set.
1559
+ The name of the parameter which argument should be used as label, if
1560
+ label is not set. If None, the label keyword argument is not set.
1558
1561
positional_parameter_names : list of strings, optional, default: None
1559
- The full list of positional parameter names (excluding an explicit `ax`/'self' argument at
1560
- the first place and including all possible positional parameter in `*args`), in the right
1561
- order. Can also include all other keyword parameter. Only needed if the wrapped function
1562
- does contain `*args` and (replace_names is not None or replace_all_args is False).
1562
+ The full list of positional parameter names (excluding an explicit
1563
+ `ax`/'self' argument at the first place and including all possible
1564
+ positional parameter in `*args`), in the right order. Can also include
1565
+ all other keyword parameter. Only needed if the wrapped function does
1566
+ contain `*args` and (replace_names is not None or replace_all_args is
1567
+ False).
1563
1568
"""
1564
1569
if replace_names is not None :
1565
1570
replace_names = set (replace_names )
@@ -1570,10 +1575,11 @@ def param(func):
1570
1575
arg_spec = inspect .getargspec (func )
1571
1576
_arg_names = arg_spec .args
1572
1577
_has_no_varargs = arg_spec .varargs is None
1573
- _has_varkwargs = not arg_spec .keywords is None
1578
+ _has_varkwargs = arg_spec .keywords is not None
1574
1579
1575
- # there can't be any positional arguments behind *args and no positional args can end up
1576
- # in **kwargs, so we only need to check for varargs:
1580
+ # there can't be any positional arguments behind *args and no
1581
+ # positional args can end up in **kwargs, so we only need to check for
1582
+ # varargs:
1577
1583
# http://stupidpythonideas.blogspot.de/2013/08/arguments-and-parameters.html
1578
1584
if _has_no_varargs :
1579
1585
# remove the first "ax" / self arg
@@ -1597,32 +1603,36 @@ def param(func):
1597
1603
"need 'positional_parameter_names'!"
1598
1604
raise AssertionError (msg % func .__name__ )
1599
1605
else :
1600
- if not positional_parameter_names is None :
1606
+ if positional_parameter_names is not None :
1601
1607
arg_names = positional_parameter_names
1602
1608
else :
1603
1609
if replace_all_args :
1604
- arg_names = []
1610
+ arg_names = []
1605
1611
else :
1606
- msg = "Got 'replace_names' and wrapped function '%s' uses *args, need 'positional_parameter_names' or 'replace_all_args'!"
1612
+ msg = "Got 'replace_names' and wrapped function '%s' uses *args, " \
1613
+ "need 'positional_parameter_names' or 'replace_all_args'!"
1607
1614
raise AssertionError (msg % func .__name__ )
1608
1615
1609
1616
# compute the possible label_namer and label position in positional arguments
1610
- label_pos = 9999 # bigger than all "possible" argument lists
1611
- label_namer_pos = 9999 # bigger than all "possible" argument lists
1617
+ label_pos = 9999 # bigger than all "possible" argument lists
1618
+ label_namer_pos = 9999 # bigger than all "possible" argument lists
1612
1619
if label_namer and arg_names and (label_namer in arg_names ):
1613
1620
label_namer_pos = arg_names .index (label_namer )
1614
1621
if "label" in arg_names :
1615
1622
label_pos = arg_names .index ("label" )
1616
1623
1617
- # Check the case we know a label_namer but we can't find it the arg_names...
1618
- # Unfortunately the label_namer can be in **kwargs, which we can't detect here and which
1619
- # results in a non-set label which might surprise the user :-(
1624
+ # Check the case we know a label_namer but we can't find it the
1625
+ # arg_names... Unfortunately the label_namer can be in **kwargs,
1626
+ # which we can't detect here and which results in a non-set label
1627
+ # which might surprise the user :-(
1620
1628
if label_namer and not _has_varkwargs :
1621
1629
if not arg_names :
1622
- msg = "label_namer '%s' can't be found as the parameter without 'positional_parameter_names'."
1623
- raise AssertionError (msg % (label_namer ))
1630
+ msg = "label_namer '%s' can't be found as the parameter without " \
1631
+ "'positional_parameter_names'."
1632
+ raise AssertionError (msg % label_namer )
1624
1633
elif label_namer not in arg_names :
1625
- msg = "label_namer '%s' can't be found in the parameter names (known argnames: %s)."
1634
+ msg = "label_namer '%s' can't be found in the parameter names " \
1635
+ "(known argnames: %s)."
1626
1636
raise AssertionError (msg % (label_namer , arg_names ))
1627
1637
else :
1628
1638
# this is the case when the name is in arg_names
@@ -1662,8 +1672,8 @@ def inner(ax, *args, **kwargs):
1662
1672
# replace the label if this func "wants" a label arg and the user didn't set one
1663
1673
# Note: if the usere puts in "label=None", it does *NOT* get replaced!
1664
1674
user_supplied_label = (
1665
- (len (args ) >= label_pos ) or # label is included in args
1666
- ('label' in kwargs ) # ... or in kwargs
1675
+ (len (args ) >= label_pos ) or # label is included in args
1676
+ ('label' in kwargs ) # ... or in kwargs
1667
1677
)
1668
1678
if (label_namer and not user_supplied_label ):
1669
1679
if label_namer_pos < len (args ):
@@ -1678,10 +1688,13 @@ def inner(ax, *args, **kwargs):
1678
1688
kwargs ['label' ] = label
1679
1689
else :
1680
1690
import warnings
1681
- msg = "Tried to set a label via parameter '%s' in func '%s' but couldn't find such an argument. \n " \
1682
- "(This is a programming error, please report to the matplotlib list!)"
1683
- warnings .warn (msg % (label_namer , func .__name__ ), RuntimeWarning , stacklevel = 2 )
1684
- #raise Exception()
1691
+ msg = "Tried to set a label via parameter '%s' in " \
1692
+ "func '%s' but couldn't find such an argument. \n " \
1693
+ "(This is a programming error, please report to the " \
1694
+ "matplotlib list!)"
1695
+ warnings .warn (msg % (label_namer , func .__name__ ),
1696
+ RuntimeWarning , stacklevel = 2 )
1697
+ # raise Exception()
1685
1698
return func (ax , * args , ** kwargs )
1686
1699
return inner
1687
1700
return param
0 commit comments