11
11
12
12
import copy
13
13
14
- from typing import Dict , Any , TYPE_CHECKING
14
+ from typing import Dict , Any , TYPE_CHECKING , Optional
15
15
16
16
del absolute_import , division , print_function
17
17
18
18
19
+ def _process_metadata_dict (metadata , __dict__ ):
20
+ # type: (Optional[Any], Optional[Dict[str, Any]]) -> Dict[str, Any]
21
+ """
22
+ Provide standardized handling for keywords related to metadata.
23
+ """
24
+ if __dict__ is None :
25
+ __dict__ = {}
26
+
27
+ if "metadata" in __dict__ and metadata is not None :
28
+ raise KeyError ("Cannot provide metadata by keyword and in __dict__" )
29
+
30
+ __dict__ ["metadata" ] = metadata
31
+ return __dict__
32
+
33
+
19
34
def _isstr (value ):
20
35
"""
21
36
Check to see if this is a stringlike or a (nested) iterable of stringlikes
@@ -54,6 +69,7 @@ def __copy__(self):
54
69
other ._ax = copy .copy (self ._ax )
55
70
return other
56
71
72
+ # Required for Python 2 + __dict__
57
73
def __setstate__ (self , state ):
58
74
self ._ax = state ["_ax" ]
59
75
@@ -293,12 +309,7 @@ def __init__(self, bins, start, stop, **kwargs):
293
309
underflow = True , overflow = True , growth = False , circular = False
294
310
)
295
311
296
- if __dict__ is None :
297
- __dict__ = {}
298
-
299
- if "metadata" in __dict__ and metadata is not None :
300
- raise KeyError ("Cannot provide metadata by keyword and in __dict__" )
301
- __dict__ ["metadata" ] = metadata
312
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
302
313
303
314
if transform is not None :
304
315
if options != {"underflow" , "overflow" }:
@@ -372,7 +383,7 @@ class Variable(Axis):
372
383
__slots__ = ()
373
384
374
385
@inject_signature (
375
- "self, edges, *, metadata=None, underflow=True, overflow=True, growth=False"
386
+ "self, edges, *, metadata=None, underflow=True, overflow=True, growth=False, __dict__=None "
376
387
)
377
388
def __init__ (self , edges , ** kwargs ):
378
389
"""
@@ -394,13 +405,18 @@ def __init__(self, edges, **kwargs):
394
405
growth : bool = False
395
406
Allow the axis to grow if a value is encountered out of range.
396
407
Be careful, the axis will grow as large as needed.
408
+ __dict__: Optional[Dict[str, Any]] = None
409
+ The full metadata dictionary
397
410
"""
398
411
with KWArgs (kwargs ) as k :
399
412
metadata = k .optional ("metadata" )
413
+ __dict__ = k .optional ("__dict__" )
400
414
options = k .options (
401
415
underflow = True , overflow = True , circular = False , growth = False
402
416
)
403
417
418
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
419
+
404
420
if options == {"growth" , "underflow" , "overflow" }:
405
421
self ._ax = ca .variable_uoflow_growth (edges )
406
422
elif options == {"underflow" , "overflow" }:
@@ -447,7 +463,7 @@ class Integer(Axis):
447
463
__slots__ = ()
448
464
449
465
@inject_signature (
450
- "self, start, stop, *, metadata=None, underflow=True, overflow=True, growth=False"
466
+ "self, start, stop, *, metadata=None, underflow=True, overflow=True, growth=False, __dict__=None "
451
467
)
452
468
def __init__ (self , start , stop , ** kwargs ):
453
469
"""
@@ -470,13 +486,18 @@ def __init__(self, start, stop, **kwargs):
470
486
growth : bool = False
471
487
Allow the axis to grow if a value is encountered out of range.
472
488
Be careful, the axis will grow as large as needed.
489
+ __dict__: Optional[Dict[str, Any]] = None
490
+ The full metadata dictionary
473
491
"""
474
492
with KWArgs (kwargs ) as k :
475
493
metadata = k .optional ("metadata" )
494
+ __dict__ = k .optional ("__dict__" )
476
495
options = k .options (
477
496
underflow = True , overflow = True , circular = False , growth = False
478
497
)
479
498
499
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
500
+
480
501
# underflow and overflow settings are ignored, integers are always
481
502
# finite and thus cannot end up in a flow bin when growth is on
482
503
if "growth" in options and "circular" not in options :
@@ -528,7 +549,7 @@ def _repr_kwargs(self):
528
549
@set_module ("boost_histogram.axis" )
529
550
@register ({ca .category_str_growth , ca .category_str })
530
551
class StrCategory (BaseCategory ):
531
- @inject_signature ("self, categories, *, metadata=None, growth=False" )
552
+ @inject_signature ("self, categories, *, metadata=None, growth=False, __dict__=None " )
532
553
def __init__ (self , categories , ** kwargs ):
533
554
"""
534
555
Make a category axis with strings; items will
@@ -545,11 +566,16 @@ def __init__(self, categories, **kwargs):
545
566
growth : bool = False
546
567
Allow the axis to grow if a value is encountered out of range.
547
568
Be careful, the axis will grow as large as needed.
569
+ __dict__: Optional[Dict[str, Any]] = None
570
+ The full metadata dictionary
548
571
"""
549
572
with KWArgs (kwargs ) as k :
550
573
metadata = k .optional ("metadata" )
574
+ __dict__ = k .optional ("__dict__" )
551
575
options = k .options (growth = False )
552
576
577
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
578
+
553
579
# henryiii: We currently expand "abc" to "a", "b", "c" - some
554
580
# Python interfaces protect against that
555
581
@@ -586,7 +612,7 @@ def _repr_args(self):
586
612
@set_module ("boost_histogram.axis" )
587
613
@register ({ca .category_int , ca .category_int_growth })
588
614
class IntCategory (BaseCategory ):
589
- @inject_signature ("self, categories, *, metadata=None, growth=False" )
615
+ @inject_signature ("self, categories, *, metadata=None, growth=False, __dict__=None " )
590
616
def __init__ (self , categories , ** kwargs ):
591
617
"""
592
618
Make a category axis with ints; items will
@@ -603,11 +629,16 @@ def __init__(self, categories, **kwargs):
603
629
growth : bool = False
604
630
Allow the axis to grow if a value is encountered out of range.
605
631
Be careful, the axis will grow as large as needed.
632
+ __dict__: Optional[Dict[str, Any]] = None
633
+ The full metadata dictionary
606
634
"""
607
635
with KWArgs (kwargs ) as k :
608
636
metadata = k .optional ("metadata" )
637
+ __dict__ = k .optional ("__dict__" )
609
638
options = k .options (growth = False )
610
639
640
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
641
+
611
642
if options == {"growth" }:
612
643
self ._ax = ca .category_int_growth (tuple (categories ))
613
644
elif options == set ():
@@ -630,7 +661,7 @@ def _repr_args(self):
630
661
class Boolean (Axis ):
631
662
__slots__ = ()
632
663
633
- @inject_signature ("self, *, metadata=None" )
664
+ @inject_signature ("self, *, metadata=None, __dict__=None " )
634
665
def __init__ (self , ** kwargs ):
635
666
"""
636
667
Make an axis for boolean values.
@@ -639,9 +670,14 @@ def __init__(self, **kwargs):
639
670
----------
640
671
metadata : object
641
672
Any Python object to attach to the axis, like a label.
673
+ __dict__: Optional[Dict[str, Any]] = None
674
+ The full metadata dictionary
642
675
"""
643
676
with KWArgs (kwargs ) as k :
644
677
metadata = k .optional ("metadata" )
678
+ __dict__ = k .optional ("__dict__" )
679
+
680
+ __dict__ = _process_metadata_dict (metadata , __dict__ )
645
681
646
682
self ._ax = ca .boolean ()
647
683
self .metadata = metadata
0 commit comments