Last active
November 17, 2021 23:33
-
-
Save asdkant/02a76d0a839425596ecc3e656f22beb0 to your computer and use it in GitHub Desktop.
Small example on "global" variables and passing things "as reference"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import exit | |
from textwrap import fill as tw | |
fail = [False] | |
def varcheck(var_name, var_value, optional = False): | |
is_empty = len(var_value) == 0 | |
if optional: | |
symbol = "π²" | |
elif is_empty or "$(" in var_value: | |
symbol = "β" | |
fail[0] = True | |
else: | |
symbol = "β" | |
print(tw(f"""{symbol} {var_name}{': ' if not is_empty else ''}{var_value}""", subsequent_indent=' ')) | |
varcheck("foo1","bar1") | |
varcheck("foo2", "", optional=True) | |
varcheck("foo3", "$(SOMETHING)") | |
#varcheck("foo0","") | |
print('') | |
if fail[0]: | |
print("FAIL") | |
else: | |
print("PASS") | |
if fail[0]: exit(1) |
facundobatista
commented
Nov 17, 2021
from sys import exit
from textwrap import fill as tw
def varcheck(var_name, var_value, optional = False):
is_empty = len(var_value) == 0
fail = False
if optional:
symbol = "π²"
elif is_empty or "$(" in var_value:
symbol = "β"
fail = True
else:
symbol = "β"
print(tw(f"""{symbol} {var_name}{': ' if not is_empty else ''}{var_value}""", subsequent_indent=' '))
return fail
print()
calls_results = [
varcheck("foo1","bar1")
varcheck("foo2", "", optional=True)
varcheck("foo3", "$(SOMETHING)")
#varcheck("foo0","")
]
if any(calls_results):
print("FAIL")
exit(1)
print("PASS")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment