def explore(self): """ Function to conduct the exploration process """ # Conduct max_iter iterations for iter_num in tqdm.tqdm(range(self.max_iter)): # If stat_collection_freq, run an exploit if (iter_num + 1) % self.stat_collection_freq == 0: logger.info("Collecting stats...") self.collect_stats(iter_num) self.single_iteration() self.collect_stats(iter_num) # Dump the results to file if self.fname: json.dump( { "policy": dicttoolz.keymap( str, dicttoolz.valmap( lambda d: dicttoolz.keymap(str, d), self.policy.policy, ), ), "stats": self.stats, }, open(self.fname, "w"), ) logger.info(msg=self.stats)
def load_from_file(self, filename): """ Function to load policy from file """ from_file = json.load(open(filename)) self.policy.policy = dicttoolz.valmap( lambda d: dicttoolz.keymap(eval, d), dicttoolz.keymap(eval, from_file.get("policy")), ) self.stats = from_file.get("stats")
def decode(data: dict) -> Any: def split_path(s): return tuple(s.split(".")) nested = nest(keymap(split_path, data)) encoded = _translate(nested) return base_decode(encoded)
def verify_and_create_pagination_params(*args, **kw): """Validate pagination parameters and pass them to wrapped function.""" pagination_params = dicttoolz.keymap( # Drop leading underscore from keys lambda key: key.lstrip('_'), schema(request.args), ) result = func(pagination=pagination_params, *args, **kw) return ListResponse(items=result.items, metadata=result.metadata)
def command(*args, **kwargs): #fixedargs = keymap("-{}".format, kwargs) bools = valfilter(lambda x: type(x) is bool, kwargs) vargs = keymap( "-{}".format, keyfilter(lambda x: x not in ['_err', '_out'], valfilter(lambda x: not type(x) is bool, kwargs))) #bools.update(vargs) fixedargs = chain(vargs.items()) getattr(sh, attr)(*(list(args) + list(fixedargs)), **bools)
def __init__(self, *, compare_with_fpath: Union[str, Path, None] = None, zip_output: bool = None, dump_yaml: bool = None): "Intercept schedula and open new & old co2mparable files." ## TODO: more sanity checks on the subclass. self.funcs_to_exclude = dtz.keymap(re.compile, self.funcs_to_exclude) self._dump_yaml = bool( compare_with_fpath and '.yaml' in compare_with_fpath.lower() or dump_yaml) suffix = '%s%s' % ('.yaml' if self._dump_yaml else '.txt', '.xz' if zip_output else '') _fd, fpath = tempfile.mkstemp(suffix=suffix, prefix=CO2MPARABLE_FNAME_PREFIX, text=False) self._ckfile = self._open_file(fpath, 'wt', errors='ignore') if compare_with_fpath: m = re.match('<LATEST(?::([^>]+))?>', compare_with_fpath, re.IGNORECASE) if m: import glob search_dir = m.group(1) or tempfile.gettempdir() old_co2mparable_pattern = osp.join(search_dir, CO2MPARABLE_FNAME_PREFIX) if not (set('*?[]') & set(old_co2mparable_pattern)): old_co2mparable_pattern += '*' files = glob.glob(old_co2mparable_pattern) if not files: log.warning('No <latest> *co2mparable* found in %s', old_co2mparable_pattern) compare_with_fpath = None else: compare_with_fpath = max(files, key=osp.getctime) if compare_with_fpath: self._old_ckfile = self._open_file(compare_with_fpath, 'rt') compare_with_fpath = "\n while comparing with '%s'" % compare_with_fpath else: compare_with_fpath = '' log.info("Writing *co2mparable* to '%s'%s.", fpath, compare_with_fpath) ## Intercept Schedula. # self._org_eval_fun = sol.Solution._evaluate_function ## `self` will bind to 2nd arg, `solution` to 1st sol.Solution._evaluate_function = my_eval_fun
def fit(self, X, y=None, exposure=None, xlabels=None): if xlabels is not None: self.xlabels_ = xlabels else: self.xlabels_ = safe_column_names(X) self.clean_transformations_ = keymap(clean_column_name(self.xlabels_), self.transformations) if not self.strict: input_variables = set(self.xlabels_) self.clean_transformations_ = valfilter( compose( curry(__ge__)(input_variables), methodcaller('inputs')), self.clean_transformations_) return self
def __dl_over_time(self): """ get a sorted dictionary with month as key """ # XXX can we do this more functional? def update_dic(obj): dates = annotations(obj) for key in dates.keys(): count = len(dates[key]) dl[key]['Sum'] += count dl[key][obj.portal_type] += count dl[key]['new'] += self.__was_new(dates[key], obj.created()) annotations = lambda obj: IAnnotations(obj)['hbxt.clickdates'] dates = map(annotations, self.get_clickdates_objects()) keyse = concat(map(lambda date: map(lambda k: k, date.keys()), dates)) dl = {k: {'Sum': 0, 'new': 0, 'JournalPaper': 0, 'DiscussionPaper': 0} for k in keyse} map(update_dic, self.get_clickdates_objects()) return keymap(add_leading_zero, dl)
def build_query(endpoint, **kwargs): kwargs = dicttoolz.keymap(us_to_cc, kwargs) # translate parameters from image_url into camelCase for magicthegatheringio query = parse.urljoin(API_URL, endpoint) + '?' + parse.urlencode(kwargs) return query
def test_keymap(): assert keymap(inc, {1: 1, 2: 2}) == {2: 1, 3: 2}
def test_keymap(self): D, kw = self.D, self.kw assert keymap(inc, D({1: 1, 2: 2}), **kw) == D({2: 1, 3: 2})
async def type_category_summary(self) -> Dict[Tuple[str, str], Dict]: results = keymap(reversed, await self.category_type_summary()) return {tuple(k): v for k, v in results.items()}
def test_environ(): # See: https://github.com/pytoolz/cytoolz/issues/127 assert keymap(identity, os.environ) == os.environ assert valmap(identity, os.environ) == os.environ assert itemmap(identity, os.environ) == os.environ
def blastdbcmd(**opts): cmd_opts = keymap('-{}'.format, opts).items() process = plumbum.local['blastdbcmd'][cmd_opts] print process for line in process.popen().iter_lines(retcode=None): yield line[0]
def _inner(dict_) -> cls: return cls(**dicttoolz.keymap(cc_to_us, dict_))
def keymap(self, f: Callable[[A], C]) -> 'Map[C, B]': return Map(dicttoolz.keymap(f, dict(self)))