def __len__(self) -> int: "Does __len__" children = self.children if fy.is_seqcoll(children): return len(cast(List[NodeDesc], children)) else: raise return len(self.children)
def check_form_fields(*fields): fields_data = [] for field in fields: if funcy.is_seqcoll(field): fields_data.extend([field.data for field in field]) else: fields_data.append(field.data) return all(fields_data)
def attempt_many(): "Does attempt_many" vars_ = lang.pubvars(obj) attrs = fnmatch.filter(vars_, idx) if fy.is_seqcoll(obj) or isinstance(obj, set): return attrs else: return [Attr.infer(attr, xget(obj, attr)) for attr in attrs]
def children(self) -> List['NodeDesc']: "NodeDesc objects for all children descending from AST node." value_or_desc = self.value if fy.is_seqcoll(value_or_desc): # XXX: typing ??? return cast(List[NodeDesc], value_or_desc) else: return []
def register_api_resource(api, resource): if funcy.is_seqcoll(resource): cls, url, endpoint = ( funcy.first(resource), funcy.second(resource), funcy.nth(2, resource), ) api.add_resource(cls, url, endpoint=endpoint)
def simpany(node_or_many: NodeOrIter) -> Union[ast.AST, Iterable[Node]]: "Converts objects or iterables to Ormsnack Node object(s)" if isinstance(node_or_many, ast.AST): return simplify(node_or_many) elif isinstance(node_or_many, Node): return node_or_many elif fy.is_seqcoll(node_or_many): return node_or_many raise TypeError(f'Unclassifiable node ({node_or_many!r})')
def pubvars(obj: Any) -> Iterable: "Returns all public variables except methods" if isdict(obj): return tuple(obj) if fy.is_seqcoll(obj) or isinstance(obj, set): return copy.copy(obj) else: return [ attr for attr in dir(obj) if not attr.startswith('__') and not callable(getattr(obj, attr)) ]
def unbox(x: Any) -> Singular: """ >>> unbox(1) 1 >>> unbox((1,)) 1 >>> unbox((1,2)) (1, 2) >>> unbox((1,2,(3,4))) (1, 2, (3, 4)) """ return x[0] if fy.is_seqcoll(x) and len(x) == 1 else x
def get_dbs( query, order=None, limit=None, cursor=None, keys_only=None, **filters ): limit = limit or flask.current_app.config.get('DEFAULT_DB_LIMIT') cursor = Cursor.from_websafe_string(cursor) if cursor else None model_class = ndb.Model._kind_map[query.kind] query_reverse = query query_forward = query if order: for o in order.split(','): if o.startswith('-'): query_reverse = query_reverse.order(model_class._properties[o[1:]]) query_forward = query_forward.order(-model_class._properties[o[1:]]) else: query_reverse = query_reverse.order(-model_class._properties[o]) query_forward = query_forward.order(model_class._properties[o]) for prop in filters: if filters.get(prop, None) is None: continue if funcy.is_seqcoll(filters[prop]): for value in filters[prop]: query_reverse = query_reverse.filter( model_class._properties[prop] == value ) query_forward = query_forward.filter( model_class._properties[prop] == value ) else: query_reverse = query_reverse.filter( model_class._properties[prop] == filters[prop] ) query_forward = query_forward.filter( model_class._properties[prop] == filters[prop] ) model_dbs, next_cursor, more = query_forward.fetch_page( limit, start_cursor=cursor, keys_only=keys_only, ) model_dbs_prev, prev_cursor, rev_more = query_reverse.fetch_page( limit, start_cursor=cursor.reversed() if cursor else None, keys_only=True ) next_cursor = next_cursor.urlsafe() if more else None prev_cursor = prev_cursor.reversed().urlsafe()\ if prev_cursor and cursor else None return list(model_dbs), next_cursor, prev_cursor
def make_meta(keywords=None, description=None, author=None): meta = {} if not keywords and description: keywords = description if keywords: if isinstance(keywords, (str, unicode)): keywords = re.sub(r'[\W_]+', ', ', keywords) if funcy.is_seqcoll(keywords): keywords = u', '.join(keywords) meta['keywords'] = keywords if description: if isinstance(description, (str, unicode)): description = description.strip() meta['description'] = description if author: meta['author'] = author return meta
def make_response(data, marshal_table, next_cursor=None, prev_cursor=None): if funcy.is_seqcoll(data): response = { 'status': 'success', 'count': len(data), 'now': datetime.utcnow().isoformat(), 'result': map(lambda l: restful.marshal(l, marshal_table), data), } if next_cursor: response['next_cursor'] = next_cursor response['next_url'] = util.generate_next_url(next_cursor) if prev_cursor: response['prev_cursor'] = prev_cursor, response['prev_url'] = util.generate_next_url(prev_cursor) return response return { 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': restful.marshal(data, marshal_table) }
def __call__(self, native: Native) -> Described: describe = super().__call__ if fy.is_seqcoll(native): return desc(native) # type: ignore else: return describe(native)
def match_subtype(cand: Any, pattern: Any) -> bool: if fy.is_seqcoll(cand): return issubclass(type(cand), pattern) else: return issubclass(cand, pattern)
def responserep(self, rep: Union[Type, Sequence[Type]], nickfunc: Callable) -> str: if fy.is_seqcoll(rep): return ','.join(map(nickfunc, cast(Sequence, rep))) else: return nickfunc(rep)