10
10
11
11
import executing
12
12
13
- __version__ = "0.5.2 "
13
+ __version__ = "0.5.3 "
14
14
__all__ = [
15
15
"VarnameRetrievingError" , "varname" , "will" ,
16
- "inject" , "nameof" , "namedtuple" , "Wrapper"
16
+ "inject" , "nameof" , "namedtuple" , "Wrapper" , "debug"
17
17
]
18
18
19
19
class VarnameRetrievingError (Exception ):
@@ -267,6 +267,42 @@ def nameof(var, *more_vars, # pylint: disable=unused-argument
267
267
268
268
return ret [0 ] if not more_vars else tuple (ret )
269
269
270
+ def debug (var , * more_vars ,
271
+ prefix : str = 'DEBUG: ' ,
272
+ merge : bool = False ,
273
+ repr : bool = True ) -> None : # pylint: disable=redefined-builtin
274
+ """Print variable names and values.
275
+
276
+ Examples:
277
+ >>> a = 1
278
+ >>> b = object
279
+ >>> print(f'a={a}') # previously, we have to do
280
+ >>> print(f'{a=}') # or with python3.8
281
+ >>> # instead we can do:
282
+ >>> debug(a) # DEBUG: a=1
283
+ >>> debug(a, prefix='') # a=1
284
+ >>> debug(a, b, merge=True) # a=1, b=<object object at 0x2b9a4c89cf00>
285
+
286
+ Args:
287
+ var: The variable to print
288
+ *more_vars: Other variables to print
289
+ prefix: A prefix to print for each line
290
+ merge: Whether merge all variables in one line or not
291
+ repr: Print the value as `repr(var)`? otherwise `str(var)`
292
+ """
293
+ var_names = nameof (var , * more_vars , caller = 2 , full = True )
294
+ if not isinstance (var_names , tuple ):
295
+ var_names = (var_names , )
296
+ variables = (var , * more_vars )
297
+ name_and_values = [f"{ var_name } ={ variables [i ]!r} " if repr
298
+ else f"{ var_name } ={ variables [i ]} "
299
+ for i , var_name in enumerate (var_names )]
300
+ if merge :
301
+ print (f"{ prefix } { ', ' .join (name_and_values )} " )
302
+ else :
303
+ for name_and_value in name_and_values :
304
+ print (f"{ prefix } { name_and_value } " )
305
+
270
306
def namedtuple (* args , ** kwargs ) -> type :
271
307
"""A shortcut for namedtuple
272
308
@@ -289,6 +325,9 @@ def namedtuple(*args, **kwargs) -> type:
289
325
Returns:
290
326
The namedtuple you desired.
291
327
"""
328
+ warnings .warn ("Shortcut for namedtuple is deprecated and "
329
+ "will be removed in 0.6.0. Use the standard way instead." ,
330
+ DeprecationWarning )
292
331
typename = varname (raise_exc = True )
293
332
return standard_namedtuple (typename , * args , ** kwargs )
294
333
0 commit comments