Extract HTML table as list of dictionaries

    Args:
        tabletag (Tag): BeautifulSoup tag

    Returns:
        str: Text of tag stripped of leading and trailing whitespace and newlines and with &nbsp replaced with space
495878
def extract_table(tabletag):
    # type: (Tag) -> List[Dict]
    
    theadtag = tabletag.find_next('thead')

    headertags = theadtag.find_all('th')
    if len(headertags) == 0:
        headertags = theadtag.find_all('td')
    headers = []
    for tag in headertags:
        headers.append(get_text(tag))

    tbodytag = tabletag.find_next('tbody')
    trtags = tbodytag.find_all('tr')

    table = list()
    for trtag in trtags:
        row = dict()
        tdtags = trtag.find_all('td')
        for i, tag in enumerate(tdtags):
            row[headers[i]] = get_text(tag)
        table.append(row)
    return table
934542
def nb_to_html_cells(nb) -> list:
    
    html_exporter = HTMLExporter()
    html_exporter.template_file = 'basic'

    (body, resources) = html_exporter.from_notebook_node(nb)
    return BeautifulSoup(body, 'html.parser').findAll('div', class_='cell')
1091783
def get_page(search_text):
    
    lst = search_aikif(search_text)
    txt = '<table class="as-table as-table-zebra as-table-horizontal">'
    for result in lst:
        txt += '<TR><TD>' + result + '</TD></TR>'
    txt += '</TABLE>\n\n'
    return txt
370307
def __extract_tags(self):
        
        tags = list()
        current = None
        for line in self._comment:
            parts = re.match(r'^@(\w+)', line)
            if parts:
                current = (parts.group(1), list())
                tags.append(current)

            if current:
                if line == '':
                    current = None
                else:
                    current[1].append(line)

        for tag in tags:
            self._tags.append((tag[0], os.linesep.join(tag[1])))
773221
def dict_to_table(header, contents):
    
    def to_text(row):
        name, value = row
        m = max_col1_size + 1 - len(name)
        spacing = ' ' * m

        return ''.join([name, spacing, value])

    thead = tuple(str(col) for col in header)
    rows = []
    for name, value in contents.items():
        # code highlighting
        if value != '':
            if isinstance(value, str):
                value = "'{}'".format(value)
            value = ':py:`{}`'.format(value)
        rows.append((name, value))

    n = np.max([len(header[0])] +
               [len(col1) for col1, _ in rows])
    hborder = tuple('='*n for col in header)
    rows = [hborder, thead, hborder] + rows + [hborder]
    max_col1_size = np.max([len(col1) for col1, _ in rows])
    table = '\n'.join([to_text(row) for row in rows])
    return table
922761
def table(sentence, fill=1, placeholder="-"):
    
    tags  = [WORD, POS, IOB, CHUNK, ROLE, REL, PNP, ANCHOR, LEMMA]
    tags += [tag for tag in sentence.token if tag not in tags]
    def format(token, tag):
        # Returns the token tag as a string.
        if   tag == WORD   : s = token.string
        elif tag == POS    : s = token.type
        elif tag == IOB    : s = token.chunk and (token.index == token.chunk.start and "B" or "I")
        elif tag == CHUNK  : s = token.chunk and token.chunk.type
        elif tag == ROLE   : s = token.chunk and token.chunk.role
        elif tag == REL    : s = token.chunk and token.chunk.relation and str(token.chunk.relation)
        elif tag == PNP    : s = token.chunk and token.chunk.pnp and token.chunk.pnp.type
        elif tag == ANCHOR : s = token.chunk and token.chunk.anchor_id
        elif tag == LEMMA  : s = token.lemma
        else               : s = token.custom_tags.get(tag)
        return s or placeholder
    def outline(column, fill=1, padding=3, align="left"):
        # Add spaces to each string in the column so they line out to the highest width.
        n = max([len(x) for x in column]+[fill])
        if align == "left"  : return [x+" "*(n-len(x))+" "*padding for x in column]
        if align == "right" : return [" "*(n-len(x))+x+" "*padding for x in column]
    
    # Gather the tags of the tokens in the sentece per column.
    # If the IOB-tag is I-, mark the chunk tag with "^".
    # Add the tag names as headers in each column.
    columns = [[format(token, tag) for token in sentence] for tag in tags]
    columns[3] = [columns[3][i]+(iob == "I" and " ^" or "") for i, iob in enumerate(columns[2])]
    del columns[2]
    for i, header in enumerate(['word', 'tag', 'chunk', 'role', 'id', 'pnp', 'anchor', 'lemma']+tags[9:]):
        columns[i].insert(0, "")
        columns[i].insert(0, header.upper())
    # The left column (the word itself) is outlined to the right,
    # and has extra spacing so that words across sentences line out nicely below each other.
    for i, column in enumerate(columns):
        columns[i] = outline(column, fill+10*(i==0), align=("left","right")[i==0])
    # Anchor column is useful in MBSP but not in pattern.en.
    if not MBSP:
        del columns[6] 
    # Create a string with one row (i.e., one token) per line.
    return "\n".join(["".join([x[i] for x in columns]) for i in range(len(columns[0]))])
55670
def extract_tags(cls, obj):
        
        return dict(uuid=obj.uuid, entrypoint=obj.__class__.path)
249263
def extract_rows(self, result={}, selector='', table_headers=[], attr='', connector='', default='', verbosity=0, *args, **kwargs):
		
		result_list = []

		try:
			values = self.get_tree_tag(selector)
			if len(table_headers) >= len(values):
				from itertools import izip_longest
				pairs = izip_longest(table_headers, values, fillvalue=default)
			else:
				from itertools import izip
				pairs = izip(table_headers, values)
			for head, val in pairs:
				if verbosity > 1:
					print("\nExtracting", head, "attribute", sep=' ', end='')
				if attr.lower() == "text":
					try:
						content = connector.join([make_ascii(x).strip() for x in val.itertext()])
					except Exception:
						content = default
					content = content.replace("\n", " ").strip()
				else:
					content = val.get(attr)
					if attr in ["href", "src"]:
						content = urljoin(self.url, content)
				result[head] = content
			result_list.append(result)
		except XPathError:
			raise Exception("Invalid %s selector - %s" % (self.__selector_type__, selector))
		except TypeError:
			raise Exception("Selector expression string to be provided. Got " + selector)

		return result_list
985779
def striptags(self):
        
        """
        stripped = u' '.join(_striptags_re.sub('', self).split())
        return Markup(stripped).unescape()
1198767
def _read_table(self, filename):
        
        filename = pkg_resources.resource_filename(
            __name__, filename)
        segments = []
        with open(filename, 'rb') as f:
            reader = csv.reader(f, encoding='utf-8')
            header = next(reader)
            names = header[1:]
            for row in reader:
                seg = row[0]
                vals = row[1:]
                specs = set(zip(vals, names))
                segments.append((seg, specs))
        seg_dict = dict(segments)
        return segments, seg_dict, names
1090154
def html(self):
        
        classes = list()

        package = ""
        if self.package is not None:
            package = "Package: " + self.package + "<br/>"

        for classname in self.classes:
            classes.append(self.classes[classname].html())

        errs = ""
        for error in self.errors:
            if not len(errs):
                errs += "<tr><th colspan='2' align='left'>Errors</th></tr>"
            for part in ["type", "message", "text"]:
                if part in error:
                    errs += "<tr><td>{}</td><td><pre>{}</pre></td></tr>".format(
                        part,
                        tag.text(error[part]))

        stdio = ""
        if self.stderr or self.stdout:
            stdio += "<tr><th colspan='2' align='left'>Output</th></tr>"
            if self.stderr:
                 stdio += "<tr><td>Stderr</td><td><pre>{}</pre></td></tr>".format(
                        tag.text(self.stderr))
            if self.stdout:
                 stdio += "<tr><td>Stdout</td><td><pre>{}</pre></td></tr>".format(
                        tag.text(self.stdout))

        props = ""
        if len(self.properties):
            props += "<table>"
            propnames = sorted(self.properties)
            for prop in propnames:
                props += "<tr><th>{}</th><td>{}</td></tr>".format(prop, self.properties[prop])
            props += "</table>"

        return """
        <div class="testsuite">
            <h2>Test Suite: {name}</h2><a name="{anchor}">
            {package}
            {properties}
            <table>
            <tr><th align="left">Duration</th><td align="right">{duration} sec</td></tr>
            <tr><th align="left">Test Cases</th><td align="right">{count}</td></tr>
            <tr><th align="left">Failures</th><td align="right">{fails}</td></tr>
            {errs}
            {stdio}
            </table>
            <a name="toc"></a>
            <h2>Results Index</h2>
            {toc}
            <hr size="2"/>
            <h2>Test Results</h2>
            <div class="testclasses">
            {classes}
            </div>
        </div>
        """.format(name=tag.text(self.name),
                   anchor=self.anchor(),
                   duration=self.duration,
                   errs=errs,
                   stdio=stdio,
                   toc=self.toc(),
                   package=package,
                   properties=props,
                   classes="".join(classes),
                   count=len(self.all()),
                   fails=len(self.failed()))
548415
def extract_table_names(query):
    
    # a good old fashioned regex. turns out this worked better than actually parsing the code
    tables_blocks = re.findall(r'(?:FROM|JOIN)\s+(\w+(?:\s*,\s*\w+)*)', query, re.IGNORECASE)
    tables = [tbl
              for block in tables_blocks
              for tbl in re.findall(r'\w+', block)]
    return set(tables)
109677
def listTables(self,walkTrace=tuple(),case=None,element=None):
        
        if case == 'sectionmain': print(walkTrace,self.title)
        if case == 'table':
            caption,tab = element
            try:
                print(walkTrace,tab._leopardref,caption)
            except AttributeError:
                tab._leopardref = next(self._reportSection._tabnr)
                print(walkTrace,tab._leopardref,caption)
1248555
def extract_metas(bs4):
    

    meta_tags = []

    metas = bs4.select('meta')

    for meta in metas:

        meta_content = {}

        meta_attrs = [
            'charset',
            'name',
            'content',
            'property',
            'http-equiv',
            'itemprop'
        ]

        for attr in meta_attrs:
            if meta.has_attr(attr):
                meta_content.update({attr: meta[attr]})

        meta_tags.append(meta_content)

    return meta_tags
627019
def html_table_from_query(rows: Iterable[Iterable[Optional[str]]],
                          descriptions: Iterable[Optional[str]]) -> str:
    
    html = u"<table>\n"

    # Header row
    html += u"<tr>"
    for x in descriptions:
        if x is None:
            x = u""
        html += u"<th>{}</th>".format(webify(x))
    html += u"</tr>\n"

    # Data rows
    for row in rows:
        html += u"<tr>"
        for x in row:
            if x is None:
                x = u""
            html += u"<td>{}</td>".format(webify(x))
        html += u"<tr>\n"

    html += u"</table>\n"
    return html
527816
def parse_table(tag):
    
    first = True
    table_header = None
    table_type = 'unknown'
    param_strings = []

    thead = tag.find('thead', recursive=False)
    theads = None  # list (items in <tr> row) of <th>/<tr> elements.
    if thead:
        theads = thead.find_all(["th", "td"])
    # end if
    tbody = tag.find('tbody', recursive=False)
    if tbody:
        tbody_rows = tbody.find_all("tr")
    else:
        tbody_rows = tag.find_all("tr")
    # end if
    tbodys = [  # list (rows) of list (items in <tr> row) of <tr> elements.
        row.find_all(["td" ,"th"]) for row in tbody_rows
    ]
    if not thead:  # so first row = header
        theads = tbody_rows[0]
        tbodys = tbody_rows[1:]
    # end if

    # TABLE HEADER

    found_columns = []
    for column in theads:
        # Either (a) `<td><strong> ... </strong></td>`
        # or new (b) `<th> ... </th>`
        col = column.find("strong")
        if col:
            # (a) `<td><strong> ... </strong></td>`
            col_text = col.text
        else:
            # (b) `<th> ... </th>`
            col_text = column.text
        # end if
        found_columns.append(col_text)
    # end def

    # if TABLE is func
    for test_columns in func_fields:
        if found_columns == test_columns:
            table_header = test_columns
            table_type = 'func'
            break
        # end if
    # end for

    # if TABLE is class
    if not table_header:  # only check if we don't have a result yet
        # search class now
        for test_columns in class_fields:
            if found_columns == test_columns:
                if table_header is not None:
                    raise AssertionError("Table detected as func and class: {!r}".format(found_columns))
                table_header = test_columns
                table_type = 'class'
                break
            # end if
        # end for
    # end if

    # TABLE is none of the above
    if not table_header:  # we don't have a result yet
        raise AssertionError("Unknown table, {!r}".format(found_columns))
    # end if

    # TABLE BODY

    for tds in tbodys:
        string = "\t".join([col.text for col in tds])
        logger.debug("t: " + string)
        param_strings.append(string)
        pass
    # end for row
    return table_type, param_strings
375018
def _get_tab(cls):
        """"""

        if not cls._tabs['dec_cobs']:
            # Compute the COBS table for decoding
            cls._tabs['dec_cobs']['\xff'] = (255, '')
            cls._tabs['dec_cobs'].update(dict((chr(l), (l, '\0'))
                                              for l in range(1, 255)))

            # Compute the COBS table for encoding
            cls._tabs['enc_cobs'] = [(255, '\xff'),
                                     dict((l, chr(l))
                                          for l in range(1, 255)),
                                     ]

        return cls._tabs['dec_cobs'], cls._tabs['enc_cobs']