One Line Punch¶
Overview¶
One line punch is a 1 line command in python to do various tasks.
Code¶
Print the python version¶
python -c "import sys; print(sys.version)"
# 3.12.8 (main, Jan 14 2025, 22:49:36) [MSC v.1942 64 bit (AMD64)]
Print the system timestamp¶
Hash an target IP¶
python -c "print('.'.join([val if ind != 2 else '___' for ind, val in enumerate('196.168.5.3'.split('.'))]))"
# yield '196.168.___.3'
Lambda with default argument¶
To declare lambda with default argument, using following syntax
lambda arg1=value1, arg2=value2, ..., argN=valueN : expression
# with:
# arg1, arg2, argN – the names of the arguments that the lambda expression uses;
# value1, value2, valueN – values that are assigned respectively to the arguments arg1, arg2, argN;
# expression – the lambda expression itself.
For example:
summ = lambda a=1, b=2, c=3: a+b+c
print("summ() = ", summ()) # 1+2+3 = 6
print("summ(10) = ", summ(10)) # 10+2+3 = 15
print("summ(10, 20) = ", summ(10,20)) # 10+20+3 = 33
print("summ(10, 20, 30) = ", summ(10,20,30)) # 10+20+30 = 60
If you do not specify the values of the arguments when calling the lambda expression, then they will be assigned the default values value1, …, valueN. If you specify only part of the arguments, then this part will replace the default value starting from the leftmost argument (arg1).
If you declare a lambda expression that takes 3 arguments by default, and call the lambda expression with 1 argument, then the changes will affect the leftmost argument. The following example demonstrates the rule for overriding default arguments when invoking a lambda expression.
Reference: https://www.bestprog.net/en/2021/06/17/python-default-arguments-in-lambda-expressions/