cgpotts / cs224u

Code for Stanford CS224u

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider using os.environ.get() instead of test ENV presence

qnu opened this issue · comments

The code base thoroughly uses in idiom to test environment variable presence as a flag, like

if 'IS_GRADESCOPE_ENV' not in os.environ:
    pass

To enable above condition, one has to unset IS_GRADESCOPE_ENV. Flipping the value has not effect, such as IS_GRADESCOPE_ENV=0 or IS_GRADESCOPE_ENV=1.

However, it is usually to use os.environ.get() as a better alternative in production systems. For example,

if not os.environ.get('IS_GRADESCOPE_ENV', False):
    pass

# or
if not os.environ.get('IS_GRADESCOPE_ENV', None):
    pass

# or strictly checking against pre-defined value
if not os.environ.get('IS_GRADESCOPE_ENV', '0') == '1':
    pass

Because in shell script, we usually test if an environment variable flag is on by checking if it is present or if it is non-empty (i.e., [[ ! -z "$VAR" ]]). In this way, either of following will be evaluated as false as expected and follows the convention, just in case users forgot to unset the value from the environment.

IS_GRADESCOPE_ENV=
unset IS_GRADESCOPE_ENV
export IS_GRADESCOPE_ENV=''

Just a minor issue, feel free to ignore. 😄

@qnu Sure! Do you separately have an environment variable IS_GRADESCOPE_ENV for some reason?

No really, but plan to 1) set this variable to make sure nothing breaks before submission and 2) not sure if I can use it as a flag to mock the grade or use my own grade code.

Thanks for this feedback @qnu! For now, I am going to leave these conditionals as-is, since a lot of grading infrastructure depends on it. The expectation is that IS_GRADESCOPE_ENV will not be in the user's environment at all, which seems reasonable.

Definitely, makes sense!