From 19a84731d8644a16111203961e6062edd043e3f1 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sat, 24 Feb 2024 21:26:12 +0300 Subject: [PATCH] Python: generate_C_lang_token.py: support STDIN as an argument --- python-programming/generate_C_lang_token.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python-programming/generate_C_lang_token.py b/python-programming/generate_C_lang_token.py index fdd6270..a463418 100644 --- a/python-programming/generate_C_lang_token.py +++ b/python-programming/generate_C_lang_token.py @@ -7,6 +7,7 @@ from random import choice from string import ascii_lowercase, ascii_uppercase, digits +from sys import argv, stdin first_char_alphabet = ascii_uppercase + ascii_lowercase + "_" alphabet = first_char_alphabet + digits @@ -14,6 +15,10 @@ alphabet = first_char_alphabet + digits random_token = lambda n: \ "".join( \ [choice(first_char_alphabet)] + \ - [choice(alphabet) for i in range(n - 1)]) if n > 0 else "" + [choice(alphabet) for i in range(n - 1)]) if (n := int(n)) > 0 else "" + +if "-" in argv[1:] and stdin: + print(random_token(list(stdin)[0])) +else: + print("Random token:", random_token(input("Token length? "))) -print("Random token:", random_token(int(input("Token length? "))))