# 2014-09-26 11:17

# A dictionary comprehension is like a list comprehension, but
# it constructs a dictionary instead of a list.

import os, glob

metadata = [(f, os.stat(f)) for f in glob.glob('*.py')]
print(metadata[0])

metadata_dict = {f: os.stat(f) for f in glob.glob('*.py')}
import pprint
pprint.pprint(metadata_dict)

import sys
sys.path.append(os.path.realpath(r'..\chapter_01'))
import humansize

humansize_dict = {os.path.splitext(f)[0]: humansize.approximate_size(meta.st_size) \
                  for f, meta in metadata_dict.items() if meta.st_size > 1000}
print(humansize_dict)

# this only works if the values of the dictionary are immutable
a_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 3}
print({value: key for key, value in a_dict.items()})
