Last active
June 20, 2017 14:50
-
-
Save eamonnbell/8144462204eb1a0ef080b0aca61c9a54 to your computer and use it in GitHub Desktop.
Optionset generator
This file contains 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
import collections | |
import itertools | |
def optionset_generator(optionset_schema): | |
iterables = {} | |
for key, value in optionset_schema.items(): | |
if isinstance(value, collections.Iterable): | |
iterables[key] = value | |
else: | |
continue | |
for resolved in itertools.product(*iterables.values()): | |
optionset = {} | |
for key, old_value in optionset_schema.items(): | |
if key not in iterables: | |
optionset[key] = old_value | |
else: | |
optionset[key] = resolved[list(iterables.keys()).index(key)] | |
yield optionset | |
## Usage | |
## >> optionset_schema = { | |
## 'window': range(1,4), | |
## 'chunk_size': 32, | |
## 'sg': [True, False] | |
## } | |
## | |
## >> [x for x in optionset_generator(optionset_schema)] | |
## [{'chunk_size': 32, 'sg': True, 'window': 1}, | |
## {'chunk_size': 32, 'sg': False, 'window': 1}, | |
## {'chunk_size': 32, 'sg': True, 'window': 2}, | |
## {'chunk_size': 32, 'sg': False, 'window': 2}, | |
## {'chunk_size': 32, 'sg': True, 'window': 3}, | |
## {'chunk_size': 32, 'sg': False, 'window': 3}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment