Skip to content

Instantly share code, notes, and snippets.

@eamonnbell
Last active June 20, 2017 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamonnbell/8144462204eb1a0ef080b0aca61c9a54 to your computer and use it in GitHub Desktop.
Save eamonnbell/8144462204eb1a0ef080b0aca61c9a54 to your computer and use it in GitHub Desktop.
Optionset generator
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