-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkwargs.py
More file actions
107 lines (89 loc) · 3.48 KB
/
kwargs.py
File metadata and controls
107 lines (89 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
# @file: kwargs.py
# @auth: Sprax Lines
# @date: 2017-12-17 14:16:37 Sun 17 Dec
# # -*- coding: utf-8 -*-
'''Example classes with args and kwargs in __init__'''
import sys
from pdb import set_trace
class CopyCtor:
'''
init can be used as a simple copy-constructor.
When args[0] is an instance, it is copied and kwargs are ignored.
Otherwise, the kwargs are used and args is ignored.
'''
def __init__(self, *args, **kwargs):
if len(args) == 1 and isinstance(args[0], type(self)) and not kwargs:
# Copy Constructor
other = args[0]
# copy all the other's attributes:
self.__dict__ = dict(other.__dict__)
if kwargs:
print("WARNING: %s.%s: ignoring kwargs: "
% (type(self).__name__, self.__init__.__name__), **kwargs)
else:
if args:
# import pdb; pdb.set_trace()
# print("WARNING: %s.%s: ignoring args: "
# % (type(self).__name__, sys._getframe().f_code.co_name),
# *args)
print("WARNING: %s.%s: ignoring args: "
% (type(self).__name__, self.__init__.__name__), *args)
self.__dict__ = kwargs
class BothCopyCtor:
'''
init can be used as a copy-constructor with updates (differences from
original).
If args[0] is an instance, it is copied and the kwargs are used to
update the new object.
Otherwise, the kwargs are used and args is ignored.
'''
def __init__(self, *args, **kwargs):
if len(args) > 0 and isinstance(args[0], type(self)):
# Copy Constructor
other = args[0]
# copy all the other's attributes:
self.__dict__ = dict(other.__dict__)
if kwargs:
self.__dict__.update(kwargs)
else:
if args:
# import pdb; pdb.set_trace()
# print("WARNING: %s.%s: ignoring args: "
# % (type(self).__name__,
# sys._getframe().f_code.co_name), *args)
print("WARNING: %s.%s: ignoring args: "
% (type(self).__name__, self.__init__.__name__), *args)
self.__dict__ = kwargs
class KwargsOnly:
'''
init takes kwargs only, and uses only the kwargs that are listed as valid.
'''
def __init__(self, **kwargs):
valid_kwargs = ['name', 'kind', 'text']
for key, val in kwargs.items():
if key not in valid_kwargs:
raise TypeError("Invalid keyword argument %s" % key)
setattr(self, key, val)
def test_kwargs(*args):
'''Test the class constructors'''
orig = CopyCtor(*args, foo="FOO", bar="BAR")
print("orig:", orig.__dict__)
copy = CopyCtor(orig)
print("copy:", copy.__dict__)
print()
both = BothCopyCtor(*args, foo="Foosball", bar="Barbell")
print("both:", both.__dict__)
diff = BothCopyCtor(both, bar="Beer", baz="Bazaar")
print("diff:", diff.__dict__)
print()
try:
bust = KwargsOnly(name='myKwargsOnly',
kind='checked', test='Four square')
print("bust:", bust.__dict__)
except TypeError as ex:
print("Caught expected TypeError from KwargsOnly(...test=...):", ex)
only = KwargsOnly(name='myKwargsOnly', kind='checked', text='Four score')
print("only:", only.__dict__)
if __name__ == '__main__':
test_kwargs(sys.argv)