Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding python code

Tags:

python

ab.py

from ddr import Options
debugElmo     = int(Options.env['GG_DEBUG_ELMO'])
postdevElmo   = int(Options.env['GG_POST_DEV_ELMO'])

Options.py

vars_of_interest = (
    'AA_PYTHON',
    'GG_POST_DEV_ELMO',
    'GG_DEBUG_ELMO',
    )
env = dict((var, os.getenv(var, 0)) for var in vars_of_interest)

I am not sure what is going on for env = dict((var, os.getenv(var, 0)) for var in vars_of_interest) as I am fairly new to python

is env a function in python in Options.py?
what is dict()?
Is var the variable from int(Options.env['GG_DEBUG_ELMO'])?

like image 847
ealeon Avatar asked Jun 27 '13 19:06

ealeon


2 Answers

You can often learn what Python code is doing by looking at its pieces in the Python interpreter:

>>> vars_of_interest
('AA_PYTHON', 'GG_POST_DEV_ELMO', 'GG_DEBUG_ELMO')

>>> import os

>>> [(var, os.getenv(var, 0)) for var in vars_of_interest]
[('AA_PYTHON', 0), ('GG_POST_DEV_ELMO', 0), ('GG_DEBUG_ELMO', 0)]

>>> env = dict((var, os.getenv(var, 0)) for var in vars_of_interest)
>>> env
{'AA_PYTHON': 0, 'GG_DEBUG_ELMO': 0, 'GG_POST_DEV_ELMO': 0}

env = dict(...) makes env a dict. If you are ever unsure what an object is, you can always ask it what its type is:

>>> type(env)
dict

A dict is a mapping between keys and values. In this case, env is a mapping between strings such as 'AA_PYTHON' and values, such as 0.

var is a temporary variable used in the generator expression

((var, os.getenv(var, 0)) for var in vars_of_interest)

The for var in vars_of_interest in the generator expression tells Python to iterate over the items in the tuple vars_of_interest, and assign the values to var one-by-one as it iterates through the loop. The generator expression is an iterator. The iterator yields the values of (var, os.getenv(var, 0)) for each var.

The expression (var, os.getenv(var, 0)) is a tuple which can be thought of as a key-value pair. var is the key, os.getenv(var, 0) is the value. os.getenv looks up the environment variable var (e.g. 'AA_PYTHON') and returns the value of the environment variable if it exists, otherwise, it returns 0.

When dict is passed an iterator of key-value pairs, as is being done in the expression

dict((var, os.getenv(var, 0)) for var in vars_of_interest)

it returns a dict which maps the given keys to the given values.

See here for more information on Python dicts.

like image 169
unutbu Avatar answered Sep 30 '22 06:09

unutbu


The second example creates a dictionary named env using a list comprehension.

What's a dictionary? It's an associative list. One way to think of dictionaries is that they are like arrays, except that instead of being indexed by numbers they are indexed by something else. In this case, they are indexed by a string, the name of an environment variable.

What's a list comprehension? It's a way to create lists. The list that is created by that list comprehension is a list of pairs, environment variable name and the value for that environment variable. The builtin function dict creates a dictionary from such a list of pairs.

like image 36
David Hammen Avatar answered Sep 30 '22 07:09

David Hammen