3
3
"""
4
4
5
5
import dataclasses
6
- import inspect
7
6
from collections import defaultdict
8
7
from collections .abc import Mapping , Sequence
9
8
from typing import Any , Literal
@@ -210,14 +209,14 @@ class AliasSystem:
210
209
... ):
211
210
... alias = AliasSystem(
212
211
... A=[
213
- ... Alias("par1"),
214
- ... Alias("par2", prefix="+j"),
215
- ... Alias("par3", prefix="+o", separator="/"),
212
+ ... Alias("par1", value=par1 ),
213
+ ... Alias("par2", prefix="+j", value=par2 ),
214
+ ... Alias("par3", prefix="+o", separator="/", value=par3 ),
216
215
... ],
217
- ... B=Alias("frame"),
218
- ... c=Alias("panel", separator=","),
216
+ ... B=Alias("frame", value=frame ),
217
+ ... c=Alias("panel", separator=",", value=panel ),
219
218
... )
220
- ... return build_arg_list(alias.kwdict)
219
+ ... return build_arg_list(alias.kwdict | kwargs )
221
220
>>> func(
222
221
... "infile",
223
222
... par1="mytext",
@@ -235,33 +234,26 @@ def __init__(self, **kwargs):
235
234
"""
236
235
self .options = {}
237
236
for option , aliases in kwargs .items ():
238
- if isinstance (aliases , list ):
239
- self .options [option ] = aliases
240
- elif isinstance (aliases , str ): # Support shorthand like 'J="projection"'
241
- self .options [option ] = [Alias (aliases )]
242
- else :
243
- self .options [option ] = [aliases ]
237
+ match aliases :
238
+ case list ():
239
+ self .options [option ] = aliases
240
+ case str (): # Support shorthand like 'J="projection"'
241
+ self .options [option ] = [Alias (aliases )]
242
+ case _:
243
+ self .options [option ] = [aliases ]
244
244
245
245
@property
246
246
def kwdict (self ):
247
247
"""
248
248
A keyword dictionary that stores the current parameter values.
249
249
"""
250
- # Get the local variables from the calling function.
251
- p_locals = inspect .currentframe ().f_back .f_locals
252
- # Get parameters/arguments from **kwargs of the calling function.
253
- p_kwargs = p_locals .get ("kwargs" , {})
254
-
255
- params = p_locals | p_kwargs
256
250
# Default value is an empty string to simplify code logic.
257
251
kwdict = defaultdict (str )
258
252
for option , aliases in self .options .items ():
259
253
for alias in aliases :
260
- alias .value = params .get (alias .name )
261
254
# value can be a string, a sequence of strings or None.
262
255
if alias .value is None :
263
256
continue
264
-
265
257
# Special handing of repeatable parameter like -B/frame.
266
258
if is_nonstr_iter (alias .value ):
267
259
kwdict [option ] = alias .value
@@ -270,24 +262,24 @@ def kwdict(self):
270
262
271
263
kwdict [option ] += alias .value
272
264
273
- # Support short-form parameter names specified in kwargs.
274
- # Short-form parameters can be either one-letter (e.g., '-B'), or two-letters
275
- # (e.g., '-Td').
276
- for option , value in p_kwargs .items ():
277
- # Here, we assume that long-form parameters specified in kwargs are longer
278
- # than two characters. Sometimes, we may use parameter like 'az', but it's
279
- # not specified in kwargs. So, the assumption is still valid.
280
- if len (option ) > 2 :
281
- continue
282
-
283
- # Two cases for short-form parameters:
284
- #
285
- # If it has an alias and the long-form parameter is also specified, (e.g.,
286
- # 'projection="X10c", J="X10c"'), then we silently ignore the short-form
287
- # parameter.
288
- #
289
- # If it has an alias but the long-form parameter is not specified, or it
290
- # doesn't has an alias, then we use the value of the short-form parameter.
291
- if option not in self .options or option not in kwdict :
292
- kwdict [option ] = value
265
+ # # Support short-form parameter names specified in kwargs.
266
+ # # Short-form parameters can be either one-letter (e.g., '-B'), or two-letters
267
+ # # (e.g., '-Td').
268
+ # for option, value in self.options .items():
269
+ # # Here, we assume that long-form parameters specified in kwargs are longer
270
+ # # than two characters. Sometimes, we may use parameter like 'az', but it's
271
+ # # not specified in kwargs. So, the assumption is still valid.
272
+ # if len(option) > 2:
273
+ # continue
274
+
275
+ # # Two cases for short-form parameters:
276
+ # #
277
+ # # If it has an alias and the long-form parameter is also specified, (e.g.,
278
+ # # 'projection="X10c", J="X10c"'), then we silently ignore the short-form
279
+ # # parameter.
280
+ # #
281
+ # # If it has an alias but the long-form parameter is not specified, or it
282
+ # # doesn't has an alias, then we use the value of the short-form parameter.
283
+ # if option not in self.options or option not in kwdict:
284
+ # kwdict[option] = value
293
285
return kwdict
0 commit comments