signature stringlengths 8 3.44k | body stringlengths 0 1.41M | docstring stringlengths 1 122k | id stringlengths 5 17 |
|---|---|---|---|
def __init__(self, *args, **kwargs): | <EOL>arg = args[<NUM_LIT:0>] if args else kwargs<EOL>is_dict = isinstance(arg, dict)<EOL>if is_dict and not all([isinstance(k, util.string) and isinstance(v, util.string) for k, v in arg.items()]):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>elif not is_dict and not all([isinstance(k, util.string) and isinstan... | Initialize. | f13612:c2:m0 |
def __init__(self, *args, **kwargs): | <EOL>arg = args[<NUM_LIT:0>] if args else kwargs<EOL>is_dict = isinstance(arg, dict)<EOL>if is_dict and not all([isinstance(k, util.string) and isinstance(v, util.string) for k, v in arg.items()]):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>elif not is_dict and not all([isinstance(k, util.string) and isinstan... | Initialize. | f13612:c3:m0 |
def __init__(<EOL>self, tag, ids, classes, attributes, nth, selectors,<EOL>relation, rel_type, contains, lang, flags<EOL>): | super(Selector, self).__init__(<EOL>tag=tag,<EOL>ids=ids,<EOL>classes=classes,<EOL>attributes=attributes,<EOL>nth=nth,<EOL>selectors=selectors,<EOL>relation=relation,<EOL>rel_type=rel_type,<EOL>contains=contains,<EOL>lang=lang,<EOL>flags=flags<EOL>)<EOL> | Initialize. | f13612:c4:m0 |
def __init__(self): | super(SelectorNull, self).__init__()<EOL> | Initialize. | f13612:c5:m0 |
def __init__(self, name, prefix): | super(SelectorTag, self).__init__(<EOL>name=name,<EOL>prefix=prefix<EOL>)<EOL> | Initialize. | f13612:c6:m0 |
def __init__(self, attribute, prefix, pattern, xml_type_pattern): | super(SelectorAttribute, self).__init__(<EOL>attribute=attribute,<EOL>prefix=prefix,<EOL>pattern=pattern,<EOL>xml_type_pattern=xml_type_pattern<EOL>)<EOL> | Initialize. | f13612:c7:m0 |
def __init__(self, text): | super(SelectorContains, self).__init__(<EOL>text=text<EOL>)<EOL> | Initialize. | f13612:c8:m0 |
def __init__(self, a, n, b, of_type, last, selectors): | super(SelectorNth, self).__init__(<EOL>a=a,<EOL>n=n,<EOL>b=b,<EOL>of_type=of_type,<EOL>last=last,<EOL>selectors=selectors<EOL>)<EOL> | Initialize. | f13612:c9:m0 |
def __init__(self, languages): | super(SelectorLang, self).__init__(<EOL>languages=tuple(languages)<EOL>)<EOL> | Initialize. | f13612:c10:m0 |
def __iter__(self): | return iter(self.languages)<EOL> | Iterator. | f13612:c10:m1 |
def __len__(self): | return len(self.languages)<EOL> | Length. | f13612:c10:m2 |
def __getitem__(self, index): | return self.languages[index]<EOL> | Get item. | f13612:c10:m3 |
def __init__(self, selectors=tuple(), is_not=False, is_html=False): | super(SelectorList, self).__init__(<EOL>selectors=tuple(selectors),<EOL>is_not=is_not,<EOL>is_html=is_html<EOL>)<EOL> | Initialize. | f13612:c11:m0 |
def __iter__(self): | return iter(self.selectors)<EOL> | Iterator. | f13612:c11:m1 |
def __len__(self): | return len(self.selectors)<EOL> | Length. | f13612:c11:m2 |
def __getitem__(self, index): | return self.selectors[index]<EOL> | Get item. | f13612:c11:m3 |
@util.lru_cache(maxsize=_MAXCACHE)<EOL>def _cached_css_compile(pattern, namespaces, custom, flags): | custom_selectors = process_custom(custom)<EOL>return cm.SoupSieve(<EOL>pattern,<EOL>CSSParser(pattern, custom=custom_selectors, flags=flags).process_selectors(),<EOL>namespaces,<EOL>custom,<EOL>flags<EOL>)<EOL> | Cached CSS compile. | f13613:m0 |
def _purge_cache(): | _cached_css_compile.cache_clear()<EOL> | Purge the cache. | f13613:m1 |
def process_custom(custom): | custom_selectors = {}<EOL>if custom is not None:<EOL><INDENT>for key, value in custom.items():<EOL><INDENT>name = util.lower(key)<EOL>if RE_CUSTOM.match(name) is None:<EOL><INDENT>raise SelectorSyntaxError("<STR_LIT>".format(name))<EOL><DEDENT>if name in custom_selectors:<EOL><INDENT>raise KeyError("<STR_LIT>".format(n... | Process custom. | f13613:m2 |
def css_unescape(content, string=False): | def replace(m):<EOL><INDENT>"""<STR_LIT>"""<EOL>if m.group(<NUM_LIT:1>):<EOL><INDENT>codepoint = int(m.group(<NUM_LIT:1>)[<NUM_LIT:1>:], <NUM_LIT:16>)<EOL>if codepoint == <NUM_LIT:0>:<EOL><INDENT>codepoint = UNICODE_REPLACEMENT_CHAR<EOL><DEDENT>value = util.uchr(codepoint)<EOL><DEDENT>elif m.group(<NUM_LIT:2>):<EOL><IN... | Unescape CSS value.
Strings allow for spanning the value on multiple strings by escaping a new line. | f13613:m3 |
def escape(ident): | string = []<EOL>length = len(ident)<EOL>start_dash = length > <NUM_LIT:0> and ident[<NUM_LIT:0>] == '<STR_LIT:->'<EOL>if length == <NUM_LIT:1> and start_dash:<EOL><INDENT>string.append('<STR_LIT>'.format(ident))<EOL><DEDENT>else:<EOL><INDENT>for index, c in enumerate(ident):<EOL><INDENT>codepoint = util.uord(c)<EOL>if ... | Escape identifier. | f13613:m4 |
def __init__(self, name, pattern): | self.name = name<EOL>self.re_pattern = re.compile(pattern, re.I | re.X | re.U)<EOL> | Initialize. | f13613:c0:m0 |
def get_name(self): | return self.name<EOL> | Get name. | f13613:c0:m1 |
def enabled(self, flags): | return True<EOL> | Enabled. | f13613:c0:m2 |
def match(self, selector, index): | return self.re_pattern.match(selector, index)<EOL> | Match the selector. | f13613:c0:m3 |
def __init__(self, patterns): | self.patterns = {}<EOL>for p in patterns:<EOL><INDENT>name = p[<NUM_LIT:0>]<EOL>pattern = SelectorPattern(name, p[<NUM_LIT:2>])<EOL>for pseudo in p[<NUM_LIT:1>]:<EOL><INDENT>self.patterns[pseudo] = pattern<EOL><DEDENT><DEDENT>self.matched_name = None<EOL>self.re_pseudo_name = re.compile(PAT_PSEUDO_CLASS_SPECIAL, re.I |... | Initialize. | f13613:c1:m0 |
def get_name(self): | return self.matched_name.get_name()<EOL> | Get name. | f13613:c1:m1 |
def enabled(self, flags): | return True<EOL> | Enabled. | f13613:c1:m2 |
def match(self, selector, index): | pseudo = None<EOL>m = self.re_pseudo_name.match(selector, index)<EOL>if m:<EOL><INDENT>name = util.lower(css_unescape(m.group('<STR_LIT:name>')))<EOL>pattern = self.patterns.get(name)<EOL>if pattern:<EOL><INDENT>pseudo = pattern.match(selector, index)<EOL>if pseudo:<EOL><INDENT>self.matched_name = pattern<EOL><DEDENT><... | Match the selector. | f13613:c1:m3 |
def enabled(self, flags): | return flags & util._QUIRKS<EOL> | Enabled if quirks flag is present. | f13613:c2:m0 |
def __init__(self, **kwargs): | self.tag = kwargs.get('<STR_LIT>', None)<EOL>self.ids = kwargs.get('<STR_LIT>', [])<EOL>self.classes = kwargs.get('<STR_LIT>', [])<EOL>self.attributes = kwargs.get('<STR_LIT>', [])<EOL>self.nth = kwargs.get('<STR_LIT>', [])<EOL>self.selectors = kwargs.get('<STR_LIT>', [])<EOL>self.relations = kwargs.get('<STR_LIT>', []... | Initialize. | f13613:c3:m0 |
def _freeze_relations(self, relations): | if relations:<EOL><INDENT>sel = relations[<NUM_LIT:0>]<EOL>sel.relations.extend(relations[<NUM_LIT:1>:])<EOL>return ct.SelectorList([sel.freeze()])<EOL><DEDENT>else:<EOL><INDENT>return ct.SelectorList()<EOL><DEDENT> | Freeze relation. | f13613:c3:m1 |
def freeze(self): | if self.no_match:<EOL><INDENT>return ct.SelectorNull()<EOL><DEDENT>else:<EOL><INDENT>return ct.Selector(<EOL>self.tag,<EOL>tuple(self.ids),<EOL>tuple(self.classes),<EOL>tuple(self.attributes),<EOL>tuple(self.nth),<EOL>tuple(self.selectors),<EOL>self._freeze_relations(self.relations),<EOL>self.rel_type,<EOL>tuple(self.c... | Freeze self. | f13613:c3:m2 |
def __str__(self): | return (<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>).format(<EOL>self.tag, self.ids, self.classes, self.attributes, self.nth, self.selectors,<EOL>self.relations, self.rel_type, self.contains, self.lang, self.flags, self.no_match<EOL>)<EOL> | String representation. | f13613:c3:m3 |
def __init__(self, selector, custom=None, flags=<NUM_LIT:0>): | self.pattern = selector.replace('<STR_LIT:\x00>', '<STR_LIT>')<EOL>self.flags = flags<EOL>self.debug = self.flags & util.DEBUG<EOL>self.quirks = self.flags & util._QUIRKS<EOL>self.custom = {} if custom is None else custom<EOL> | Initialize. | f13613:c4:m0 |
def parse_attribute_selector(self, sel, m, has_selector, quirks): | inverse = False<EOL>op = m.group('<STR_LIT>')<EOL>case = util.lower(m.group('<STR_LIT>')) if m.group('<STR_LIT>') else None<EOL>parts = [css_unescape(a) for a in m.group('<STR_LIT>').split('<STR_LIT:|>')]<EOL>ns = '<STR_LIT>'<EOL>is_type = False<EOL>pattern2 = None<EOL>if len(parts) > <NUM_LIT:1>:<EOL><INDENT>ns = part... | Create attribute selector from the returned regex match. | f13613:c4:m1 |
def parse_tag_pattern(self, sel, m, has_selector): | parts = [css_unescape(x) for x in m.group(<NUM_LIT:0>).split('<STR_LIT:|>')]<EOL>if len(parts) > <NUM_LIT:1>:<EOL><INDENT>prefix = parts[<NUM_LIT:0>]<EOL>tag = parts[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>tag = parts[<NUM_LIT:0>]<EOL>prefix = None<EOL><DEDENT>sel.tag = ct.SelectorTag(tag, prefix)<EOL>has_selector =... | Parse tag pattern from regex match. | f13613:c4:m2 |
def parse_pseudo_class_custom(self, sel, m, has_selector): | pseudo = util.lower(css_unescape(m.group('<STR_LIT:name>')))<EOL>selector = self.custom.get(pseudo)<EOL>if selector is None:<EOL><INDENT>raise SelectorSyntaxError(<EOL>"<STR_LIT>".format(pseudo, m.end(<NUM_LIT:0>)),<EOL>self.pattern,<EOL>m.end(<NUM_LIT:0>)<EOL>)<EOL><DEDENT>if not isinstance(selector, ct.SelectorList):... | Parse custom pseudo class alias.
Compile custom selectors as we need them. When compiling a custom selector,
set it to `None` in the dictionary so we can avoid an infinite loop. | f13613:c4:m3 |
def parse_pseudo_class(self, sel, m, has_selector, iselector, is_html): | complex_pseudo = False<EOL>pseudo = util.lower(css_unescape(m.group('<STR_LIT:name>')))<EOL>if m.group('<STR_LIT>'):<EOL><INDENT>complex_pseudo = True<EOL><DEDENT>if complex_pseudo and pseudo in PSEUDO_COMPLEX:<EOL><INDENT>has_selector = self.parse_pseudo_open(sel, pseudo, has_selector, iselector, m.end(<NUM_LIT:0>))<E... | Parse pseudo class. | f13613:c4:m4 |
def parse_pseudo_nth(self, sel, m, has_selector, iselector): | mdict = m.groupdict()<EOL>if mdict.get('<STR_LIT>'):<EOL><INDENT>postfix = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>postfix = '<STR_LIT>'<EOL><DEDENT>mdict['<STR_LIT:name>'] = util.lower(css_unescape(mdict['<STR_LIT:name>']))<EOL>content = util.lower(mdict.get('<STR_LIT>' + postfix))<EOL>if content == '<STR_LIT>':<EOL... | Parse `nth` pseudo. | f13613:c4:m5 |
def parse_pseudo_open(self, sel, name, has_selector, iselector, index): | flags = FLG_PSEUDO | FLG_OPEN<EOL>if name == '<STR_LIT>':<EOL><INDENT>flags |= FLG_NOT<EOL><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>flags |= FLG_RELATIVE<EOL><DEDENT>sel.selectors.append(self.parse_selectors(iselector, index, flags))<EOL>has_selector = True<EOL>return has_selector<EOL> | Parse pseudo with opening bracket. | f13613:c4:m6 |
def parse_has_combinator(self, sel, m, has_selector, selectors, rel_type, index): | combinator = m.group('<STR_LIT>').strip()<EOL>if not combinator:<EOL><INDENT>combinator = WS_COMBINATOR<EOL><DEDENT>if combinator == COMMA_COMBINATOR:<EOL><INDENT>if not has_selector:<EOL><INDENT>raise SelectorSyntaxError(<EOL>"<STR_LIT>".format(combinator, index),<EOL>self.pattern,<EOL>index<EOL>)<EOL><DEDENT>sel.rel_... | Parse combinator tokens. | f13613:c4:m7 |
def parse_combinator(self, sel, m, has_selector, selectors, relations, is_pseudo, index): | combinator = m.group('<STR_LIT>').strip()<EOL>if not combinator:<EOL><INDENT>combinator = WS_COMBINATOR<EOL><DEDENT>if not has_selector:<EOL><INDENT>if (not self.quirks or is_pseudo or combinator == COMMA_COMBINATOR or relations):<EOL><INDENT>raise SelectorSyntaxError(<EOL>"<STR_LIT>".format(combinator, index),<EOL>sel... | Parse combinator tokens. | f13613:c4:m8 |
def parse_class_id(self, sel, m, has_selector): | selector = m.group(<NUM_LIT:0>)<EOL>if selector.startswith('<STR_LIT:.>'):<EOL><INDENT>sel.classes.append(css_unescape(selector[<NUM_LIT:1>:]))<EOL><DEDENT>else:<EOL><INDENT>sel.ids.append(css_unescape(selector[<NUM_LIT:1>:]))<EOL><DEDENT>has_selector = True<EOL>return has_selector<EOL> | Parse HTML classes and ids. | f13613:c4:m9 |
def parse_pseudo_contains(self, sel, m, has_selector): | values = m.group('<STR_LIT>')<EOL>patterns = []<EOL>for token in RE_VALUES.finditer(values):<EOL><INDENT>if token.group('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>value = token.group('<STR_LIT:value>')<EOL>if value.startswith(("<STR_LIT:'>", '<STR_LIT:">')):<EOL><INDENT>value = css_unescape(value[<NUM_LIT:1>:-<NUM_... | Parse contains. | f13613:c4:m10 |
def parse_pseudo_lang(self, sel, m, has_selector): | values = m.group('<STR_LIT>')<EOL>patterns = []<EOL>for token in RE_VALUES.finditer(values):<EOL><INDENT>if token.group('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>value = token.group('<STR_LIT:value>')<EOL>if value.startswith(('<STR_LIT:">', "<STR_LIT:'>")):<EOL><INDENT>parts = css_unescape(value[<NUM_LIT:1>:-<NUM_... | Parse pseudo language. | f13613:c4:m11 |
def parse_pseudo_dir(self, sel, m, has_selector): | value = ct.SEL_DIR_LTR if util.lower(m.group('<STR_LIT>')) == '<STR_LIT>' else ct.SEL_DIR_RTL<EOL>sel.flags |= value<EOL>has_selector = True<EOL>return has_selector<EOL> | Parse pseudo direction. | f13613:c4:m12 |
def parse_selectors(self, iselector, index=<NUM_LIT:0>, flags=<NUM_LIT:0>): | sel = _Selector()<EOL>selectors = []<EOL>has_selector = False<EOL>closed = False<EOL>relations = []<EOL>rel_type = "<STR_LIT::>" + WS_COMBINATOR<EOL>is_open = bool(flags & FLG_OPEN)<EOL>is_pseudo = bool(flags & FLG_PSEUDO)<EOL>is_relative = bool(flags & FLG_RELATIVE)<EOL>is_not = bool(flags & FLG_NOT)<EOL>is_html = boo... | Parse selectors. | f13613:c4:m13 |
def selector_iter(self, pattern): | <EOL>m = RE_WS_BEGIN.search(pattern)<EOL>index = m.end(<NUM_LIT:0>) if m else <NUM_LIT:0><EOL>m = RE_WS_END.search(pattern)<EOL>end = (m.start(<NUM_LIT:0>) - <NUM_LIT:1>) if m else (len(pattern) - <NUM_LIT:1>)<EOL>if self.debug: <EOL><INDENT>if self.quirks:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>print('<STR_LIT>'.... | Iterate selector tokens. | f13613:c4:m14 |
def process_selectors(self, index=<NUM_LIT:0>, flags=<NUM_LIT:0>): | return self.parse_selectors(self.selector_iter(self.pattern), index, flags)<EOL> | Process selectors.
We do our own selectors as BeautifulSoup4 has some annoying quirks,
and we don't really need to do nth selectors or siblings or
descendants etc. | f13613:c4:m15 |
def compile(pattern, namespaces=None, flags=<NUM_LIT:0>, **kwargs): | if namespaces is not None:<EOL><INDENT>namespaces = ct.Namespaces(**namespaces)<EOL><DEDENT>custom = kwargs.get('<STR_LIT>')<EOL>if custom is not None:<EOL><INDENT>custom = ct.CustomSelectors(**custom)<EOL><DEDENT>if isinstance(pattern, SoupSieve):<EOL><INDENT>if flags:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DE... | Compile CSS pattern. | f13614:m0 |
def purge(): | cp._purge_cache()<EOL> | Purge cached patterns. | f13614:m1 |
def closest(select, tag, namespaces=None, flags=<NUM_LIT:0>, **kwargs): | return compile(select, namespaces, flags, **kwargs).closest(tag)<EOL> | Match closest ancestor. | f13614:m2 |
def match(select, tag, namespaces=None, flags=<NUM_LIT:0>, **kwargs): | return compile(select, namespaces, flags, **kwargs).match(tag)<EOL> | Match node. | f13614:m3 |
def filter(select, iterable, namespaces=None, flags=<NUM_LIT:0>, **kwargs): | return compile(select, namespaces, flags, **kwargs).filter(iterable)<EOL> | Filter list of nodes. | f13614:m4 |
@deprecated("<STR_LIT>")<EOL>def comments(tag, limit=<NUM_LIT:0>, flags=<NUM_LIT:0>, **kwargs): | return [comment for comment in cm.CommentsMatch(tag).get_comments(limit)]<EOL> | Get comments only. | f13614:m5 |
@deprecated("<STR_LIT>")<EOL>def icomments(tag, limit=<NUM_LIT:0>, flags=<NUM_LIT:0>, **kwargs): | for comment in cm.CommentsMatch(tag).get_comments(limit):<EOL><INDENT>yield comment<EOL><DEDENT> | Iterate comments only. | f13614:m6 |
def select_one(select, tag, namespaces=None, flags=<NUM_LIT:0>, **kwargs): | return compile(select, namespaces, flags, **kwargs).select_one(tag)<EOL> | Select a single tag. | f13614:m7 |
def select(select, tag, namespaces=None, limit=<NUM_LIT:0>, flags=<NUM_LIT:0>, **kwargs): | return compile(select, namespaces, flags, **kwargs).select(tag, limit)<EOL> | Select the specified tags. | f13614:m8 |
def iselect(select, tag, namespaces=None, limit=<NUM_LIT:0>, flags=<NUM_LIT:0>, **kwargs): | for el in compile(select, namespaces, flags, **kwargs).iselect(tag, limit):<EOL><INDENT>yield el<EOL><DEDENT> | Iterate the specified tags. | f13614:m9 |
def escape(ident): | return cp.escape(ident)<EOL> | Escape identifier. | f13614:m10 |
def parse_version(ver, pre=False): | m = RE_VER.match(ver)<EOL>major = int(m.group('<STR_LIT>'))<EOL>minor = int(m.group('<STR_LIT>')) if m.group('<STR_LIT>') else <NUM_LIT:0><EOL>micro = int(m.group('<STR_LIT>')) if m.group('<STR_LIT>') else <NUM_LIT:0><EOL>if m.group('<STR_LIT:type>'):<EOL><INDENT>release = PRE_REL_MAP[m.group('<STR_LIT:type>')]<EOL>pre... | Parse version into a comparable Version tuple. | f13615:m0 |
def __new__(cls, major, minor, micro, release="<STR_LIT>", pre=<NUM_LIT:0>, post=<NUM_LIT:0>, dev=<NUM_LIT:0>): | <EOL>for value in (major, minor, micro, pre, post):<EOL><INDENT>if not (isinstance(value, int) and value >= <NUM_LIT:0>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>if release not in REL_MAP:<EOL><INDENT>raise ValueError("<STR_LIT>".format(release))<EOL><DEDENT>if "<STR_LIT>" < release < "<STR_LIT>":... | Validate version info. | f13615:c0:m0 |
def _is_pre(self): | return self.pre > <NUM_LIT:0><EOL> | Is prerelease. | f13615:c0:m1 |
def _is_dev(self): | return bool(self.release < "<STR_LIT>")<EOL> | Is development. | f13615:c0:m2 |
def _is_post(self): | return self.post > <NUM_LIT:0><EOL> | Is post. | f13615:c0:m3 |
def _get_dev_status(self): | return DEV_STATUS[self.release]<EOL> | Get development status string. | f13615:c0:m4 |
def _get_canonical(self): | <EOL>if self.micro == <NUM_LIT:0>:<EOL><INDENT>ver = "<STR_LIT>".format(self.major, self.minor)<EOL><DEDENT>else:<EOL><INDENT>ver = "<STR_LIT>".format(self.major, self.minor, self.micro)<EOL><DEDENT>if self._is_pre():<EOL><INDENT>ver += '<STR_LIT>'.format(REL_MAP[self.release], self.pre)<EOL><DEDENT>if self._is_post():... | Get the canonical output string. | f13615:c0:m5 |
def __init__(self, element): | self.contents = [element]<EOL> | Initialize. | f13616:c0:m0 |
def __len__(self): | return len(self.contents)<EOL> | Length. | f13616:c0:m1 |
@classmethod<EOL><INDENT>def assert_valid_input(cls, tag):<DEDENT> | <EOL>if not cls.is_tag(tag):<EOL><INDENT>raise TypeError("<STR_LIT>".format(type(tag)))<EOL><DEDENT> | Check if valid input tag or document. | f13616:c1:m0 |
@staticmethod<EOL><INDENT>def is_doc(obj):<DEDENT> | import bs4<EOL>return isinstance(obj, bs4.BeautifulSoup)<EOL> | Is `BeautifulSoup` object. | f13616:c1:m1 |
@staticmethod<EOL><INDENT>def is_tag(obj):<DEDENT> | import bs4<EOL>return isinstance(obj, bs4.Tag)<EOL> | Is tag. | f13616:c1:m2 |
@staticmethod<EOL><INDENT>def is_comment(obj):<DEDENT> | import bs4<EOL>return isinstance(obj, bs4.Comment)<EOL> | Is comment. | f13616:c1:m3 |
@staticmethod<EOL><INDENT>def is_declaration(obj): <DEDENT> | import bs4<EOL>return isinstance(obj, bs4.Declaration)<EOL> | Is declaration. | f13616:c1:m4 |
@staticmethod<EOL><INDENT>def is_cdata(obj): <DEDENT> | import bs4<EOL>return isinstance(obj, bs4.Declaration)<EOL> | Is CDATA. | f13616:c1:m5 |
@staticmethod<EOL><INDENT>def is_processing_instruction(obj): <DEDENT> | import bs4<EOL>return isinstance(obj, bs4.ProcessingInstruction)<EOL> | Is processing instruction. | f13616:c1:m6 |
@staticmethod<EOL><INDENT>def is_navigable_string(obj):<DEDENT> | import bs4<EOL>return isinstance(obj, bs4.NavigableString)<EOL> | Is navigable string. | f13616:c1:m7 |
@staticmethod<EOL><INDENT>def is_special_string(obj):<DEDENT> | import bs4<EOL>return isinstance(obj, (bs4.Comment, bs4.Declaration, bs4.CData, bs4.ProcessingInstruction))<EOL> | Is special string. | f13616:c1:m8 |
@classmethod<EOL><INDENT>def is_content_string(cls, obj):<DEDENT> | return cls.is_navigable_string(obj) and not cls.is_special_string(obj)<EOL> | Check if node is content string. | f13616:c1:m9 |
@staticmethod<EOL><INDENT>def create_fake_parent(el):<DEDENT> | return FakeParent(el)<EOL> | Create fake parent for a given element. | f13616:c1:m10 |
@staticmethod<EOL><INDENT>def is_xml_tree(el):<DEDENT> | return el._is_xml<EOL> | Check if element (or document) is from a XML tree. | f13616:c1:m11 |
def is_iframe(self, el): | return ((el.name if self.is_xml_tree(el) else util.lower(el.name)) == '<STR_LIT>') and self.is_html_tag(el)<EOL> | Check if element is an `iframe`. | f13616:c1:m12 |
def is_root(self, el): | root = self.root and self.root is el<EOL>if not root:<EOL><INDENT>parent = self.get_parent(el)<EOL>root = parent is not None and self.is_html and self.is_iframe(parent)<EOL><DEDENT>return root<EOL> | Return whether element is a root element.
We check that the element is the root of the tree (which we have already pre-calculated),
and we check if it is the root element under an `iframe`. | f13616:c1:m13 |
def get_contents(self, el, no_iframe=False): | if not no_iframe or not self.is_iframe(el):<EOL><INDENT>for content in el.contents:<EOL><INDENT>yield content<EOL><DEDENT><DEDENT> | Get contents or contents in reverse. | f13616:c1:m14 |
def get_children(self, el, start=None, reverse=False, tags=True, no_iframe=False): | if not no_iframe or not self.is_iframe(el):<EOL><INDENT>last = len(el.contents) - <NUM_LIT:1><EOL>if start is None:<EOL><INDENT>index = last if reverse else <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>index = start<EOL><DEDENT>end = -<NUM_LIT:1> if reverse else last + <NUM_LIT:1><EOL>incr = -<NUM_LIT:1> if reverse else <... | Get children. | f13616:c1:m15 |
def get_descendants(self, el, tags=True, no_iframe=False): | if not no_iframe or not self.is_iframe(el):<EOL><INDENT>next_good = None<EOL>for child in el.descendants:<EOL><INDENT>if next_good is not None:<EOL><INDENT>if child is not next_good:<EOL><INDENT>continue<EOL><DEDENT>next_good = None<EOL><DEDENT>is_tag = self.is_tag(child)<EOL>if no_iframe and is_tag and self.is_iframe(... | Get descendants. | f13616:c1:m16 |
def get_parent(self, el, no_iframe=False): | parent = el.parent<EOL>if no_iframe and parent is not None and self.is_iframe(parent):<EOL><INDENT>parent = None<EOL><DEDENT>return parent<EOL> | Get parent. | f13616:c1:m17 |
@staticmethod<EOL><INDENT>def get_tag_name(el):<DEDENT> | return el.name<EOL> | Get tag. | f13616:c1:m18 |
@staticmethod<EOL><INDENT>def get_prefix_name(el):<DEDENT> | return el.prefix<EOL> | Get prefix. | f13616:c1:m19 |
@classmethod<EOL><INDENT>def get_next_tag(cls, el):<DEDENT> | sibling = el.next_sibling<EOL>while not cls.is_tag(sibling) and sibling is not None:<EOL><INDENT>sibling = sibling.next_sibling<EOL><DEDENT>return sibling<EOL> | Get next sibling tag. | f13616:c1:m20 |
@classmethod<EOL><INDENT>def get_previous_tag(cls, el):<DEDENT> | sibling = el.previous_sibling<EOL>while not cls.is_tag(sibling) and sibling is not None:<EOL><INDENT>sibling = sibling.previous_sibling<EOL><DEDENT>return sibling<EOL> | Get previous sibling tag. | f13616:c1:m21 |
@staticmethod<EOL><INDENT>def has_html_ns(el):<DEDENT> | ns = getattr(el, '<STR_LIT>') if el else None<EOL>return ns and ns == NS_XHTML<EOL> | Check if element has an HTML namespace.
This is a bit different than whether a element is treated as having an HTML namespace,
like we do in the case of `is_html_tag`. | f13616:c1:m22 |
@staticmethod<EOL><INDENT>def split_namespace(el, attr_name):<DEDENT> | return getattr(attr_name, '<STR_LIT>', None), getattr(attr_name, '<STR_LIT:name>', None)<EOL> | Return namespace and attribute name without the prefix. | f13616:c1:m23 |
@staticmethod<EOL><INDENT>def get_attribute_by_name(el, name, default=None):<DEDENT> | value = default<EOL>if el._is_xml:<EOL><INDENT>try:<EOL><INDENT>value = el.attrs[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for k, v in el.attrs.items():<EOL><INDENT>if util.lower(k) == name:<EOL><INDENT>value = v<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return value<EOL> | Get attribute by name. | f13616:c1:m24 |
@staticmethod<EOL><INDENT>def iter_attributes(el):<DEDENT> | for k, v in el.attrs.items():<EOL><INDENT>yield k, v<EOL><DEDENT> | Iterate attributes. | f13616:c1:m25 |
@classmethod<EOL><INDENT>def get_classes(cls, el):<DEDENT> | classes = cls.get_attribute_by_name(el, '<STR_LIT:class>', [])<EOL>if isinstance(classes, util.ustr):<EOL><INDENT>classes = RE_NOT_WS.findall(classes)<EOL><DEDENT>return classes<EOL> | Get classes. | f13616:c1:m26 |
def get_text(self, el, no_iframe=False): | return '<STR_LIT>'.join(<EOL>[node for node in self.get_descendants(el, tags=False, no_iframe=no_iframe) if self.is_content_string(node)]<EOL>)<EOL> | Get text. | f13616:c1:m27 |
@staticmethod<EOL><INDENT>def validate_day(year, month, day):<DEDENT> | max_days = LONG_MONTH<EOL>if month == FEB:<EOL><INDENT>max_days = FEB_LEAP_MONTH if ((year % <NUM_LIT:4> == <NUM_LIT:0>) and (year % <NUM_LIT:100> != <NUM_LIT:0>)) or (year % <NUM_LIT> == <NUM_LIT:0>) else FEB_MONTH<EOL><DEDENT>elif month in MONTHS_30:<EOL><INDENT>max_days = SHORT_MONTH<EOL><DEDENT>return <NUM_LIT:1> <... | Validate day. | f13616:c2:m0 |
@staticmethod<EOL><INDENT>def validate_week(year, week):<DEDENT> | max_week = datetime.strptime("<STR_LIT>".format(<NUM_LIT:12>, <NUM_LIT>, year), "<STR_LIT>").isocalendar()[<NUM_LIT:1>]<EOL>if max_week == <NUM_LIT:1>:<EOL><INDENT>max_week = <NUM_LIT><EOL><DEDENT>return <NUM_LIT:1> <= week <= max_week<EOL> | Validate week. | f13616:c2:m1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.