def get_required_request_query_or_header_parameters_internal( header: bool, l: lenses.ui.BaseUiLens[S, T, X, Y], oai: OpenAPIObject, p: PathItem) -> Sequence[Parameter]: # TODO: really dirty cast # is this even the case # copied from unmock, but need to investigate return [ replace( parameter, name=parameter.name if not header else parameter.name.lower(), schema=change_ref(parameter.schema) if isinstance( parameter.schema, Reference) else change_refs(parameter.schema) if parameter.schema is not None else Schema(_type="string"), ) for parameter in l.Prism( lambda s: get_parameter_from_ref(oai, ref_name(s)) if isinstance(s, Reference) else s, lambda a: a, ignore_none=True, ).Prism( lambda s: s if (s._in == ("header" if header else "query")) and s. required else None, lambda a: a, ignore_none=True, ).collect()(p) if parameter.schema is not None and (len(lens.Each().add_lens( schema_prism(oai)).collect()([parameter.schema])) > 0) ]
def lens_fields(new_data): incoming_fields = set(new_data.keys()) stage = lens.Each().Filter(lambda tup: tup[0] in incoming_fields) def update_field(field_tup): key = field_tup[0] return (key, new_data[key]) update = stage.modify(update_field) return stage.collect(), update
def test_frozendict_iter(): state = frozendict.frozendict(one=1, two=2, three=3) result = frozendict.frozendict(one=11, two=12, three=13) assert (lens.Each()[1] + 10)(state) == result
def test_lens_get_monoid(): assert lens.Each().get_monoid()([[1, 2], [3, 4]]) == [1, 2, 3, 4]
def test_lens_collect(): assert lens.Each()[1].collect()([[1, 2], [3, 4]]) == [2, 4]
cls, lvl = spec.split() if '/' in cls: cls = cls.split('/') else: cls = [cls] for c in cls: lvls[c] = int(lvl) return lvls def spellfold(spell): lvls = munge_level(spell['level']) yield {'name': spell['name'], 'level': lvls} spellfields = lens.Each()['fields'] # focus the fields of a spell cls = 'wizard' clsfilter = lens.Filter( lambda s: cls in s['level']) # filter spells in this class mutatespell = lens.Fold(spellfold) # use a fold to mutate the spell structure optic = (spellfields & clsfilter & mutatespell) # compose these lenses spells = optic.collect()(data) print('\n----\nFirst 10 %s spells:\n----' % cls) pprint([s['name'] for s in spells[:10]]) pprint(spells[:10]) # I changed my mind and want to see spells with mixed class levels optic = optic & lens.Filter(lambda s: len(set(s['level'].values())) > 1)
def test_precord_iter(): state = PairR(left=1, right=2) assert (lens.Each()[1] + 10)(state) == PairR(left=11, right=12)
def test_pset_iter(): state = pyr.s(1, 2, 3) assert (lens.Each() + 10)(state) == pyr.s(11, 12, 13)
def test_pmap_iter(): state = pyr.m(a=1, b=2, c=3) assert (lens.Each()[1] + 10)(state) == pyr.m(a=11, b=12, c=13)
def test_pvector_iter(): state = pyr.pvector([1, 2, 3]) assert (lens.Each() + 10)(state) == pyr.pvector([11, 12, 13])
def init_assign(prop): return ast.Assign( targets=[ast.Attribute(value=ast.Name(id="self"), attr=prop)], value=ast.Name(id=prop) ) # Return an argument node def init_arg(prop): return ast.arg(arg=prop, annotation=None) # Load the original file into AST tree = ast.parse(original) # Dive into tree for_each_module_body = lens.body & lens.Each() for_each_class_body = lens.body & lens.Each() function_body = lens.body function_args = lens.args # Filter for only init functions is_init_function = lens.Filter(is_init) # Compose previous optics to zoom in on the module's init function init_function = for_each_module_body & for_each_class_body & is_init_function # Construct node diff set current_properties = (init_function & function_body & lens.Each().value.id).collect()(tree) extra_properties = [k for k, v in table.items() if k not in current_properties] extra_args = [init_arg(property_) for property_ in extra_properties] extra_assign_nodes = [init_assign(property_) for property_ in extra_properties]