@@ -11,6 +11,7 @@ class Config:
1111 configuration needs to be validated before the user can modify it, add your
1212 validation into the `_validators` dictionary.
1313 """
14+
1415 def __init__ (self ):
1516 self .truncate_len = 10
1617 self .dynamic_truncate = True
@@ -20,24 +21,18 @@ def __init__(self):
2021 self ._validators = {
2122 "truncate_len" : (lambda val : isinstance (val , int ) and val >= 1 ,
2223 "truncate_len must be a positive integer" ),
24+ "dynamic_truncate" : (lambda val : isinstance (val , bool ),
25+ "dynamic_truncate must be a boolean" )
2326 }
2427
2528 # Dynamically generates setter functions based on variable names and
2629 # the type of the default values
2730 self ._generate_setters ()
2831
2932 def _generate_setters (self ):
30- def create_method (name , func ):
31- setattr (self .__class__ , name , func )
32-
33- def make_toggle (attr ):
34- """Factory for making functions like `toggle_<attr_name>()`"""
35- def toggler (self ):
36- setattr (self , attr , not getattr (self , attr ))
37- return toggler
38-
3933 def make_setter (attr ):
4034 """Factory for making functions like `set_<attr_name>(arg)`"""
35+
4136 def setter (self , value ):
4237 # Get the entry in the dict of validators.
4338 # Check to see if the value passes the validator, and if it
@@ -59,19 +54,16 @@ def setter(self, value):
5954 setattr (self , attr , value )
6055 return setter
6156
62- for attribute_name in dir (self ):
57+ # Iterate through the names of every instantiated variable
58+ for attribute_name in self .__dict__ :
6359 if attribute_name .startswith ('_' ):
64- continue # skip "private" attributes (denoted with a prefix `_`)
60+ continue # skip "private" attributes (denoted with a prefix `_`)
6561 value = getattr (self , attribute_name )
6662 if callable (value ):
67- continue # skip functions/methods
63+ continue # skip functions/methods
6864
69- # For variables with the default boolean type, make a setter that
70- # starts with `toggle_`. Otherwise, have it start with `set_`.
71- if isinstance (value , bool ):
72- create_method (f"toggle_{ attribute_name } " , make_toggle (attribute_name ))
73- else :
74- create_method (f"set_{ attribute_name } " , make_setter (attribute_name ))
65+ # Create a class method with the given name and function
66+ setattr (self .__class__ , f"set_{ attribute_name } " , make_setter (attribute_name ))
7567
7668
7769config = Config ()
0 commit comments