text stringlengths 18 773k |
|---|
def get_posts_data(self):
"""
given a WordPress xml export file, will return list
of dictionaries with keys that match
the expected json keys of a wordpress API call
>>> xp = XML_parser('example_export.xml')
>>> json_vals = {"slug","ID", "title","description", "content", ... |
def get_comments_data(self, slug):
"""
Returns a flat list of all comments in XML dump. Formatted as the JSON
output from Wordpress API.
Keys:
('content', 'slug', 'date', 'status', 'author', 'ID', 'parent')
date format: '%Y-%m-%dT%H:%M:%S'
... |
def _format_content(self, content):
"""Returns `content` with consecutive spaces converted to non-break
spaces, and linebreak converted into HTML br elements.
:param content: text to format
:type content: `str`
:rtype: `str`
"""
content = re.sub(r'\n', '<br/>\n'... |
def _prepare_text(self, text):
"""Returns `text` with each consituent token wrapped in HTML markup
for later match annotation.
:param text: text to be marked up
:type text: `str`
:rtype: `str`
"""
# Remove characters that should be escaped for XML input (but
... |
def generate(self, output_dir, work, ngrams, labels, minus_ngrams):
"""Generates HTML reports for each witness to `work`, showing its text
with the n-grams in `ngrams` highlighted.
Any n-grams in `minus_ngrams` have any highlighting of them
(or subsets of them) removed.
:param ... |
def _highlight(self, content, ngrams, highlight):
"""Returns `content` with its n-grams from `ngrams` highlighted (if
`add_class` is True) or unhighlighted.
:param content: text to be modified
:type content: `str`
:param ngrams: n-grams to modify
:type ngrams: `list` of ... |
def generate(self, output_dir, work, matches_filename):
"""Generates HTML reports showing the text of each witness to `work`
with its matches in `matches` highlighted.
:param output_dir: directory to write report to
:type output_dir: `str`
:param work: name of work to highlight
... |
def packet_meta_data(self):
"""Pull out the metadata about each packet from the input_stream
Args:
None
Returns:
generator (dictionary): a generator that contains packet meta data in the form of a dictionary"""
# For each packet in the pcap process th... |
def print_flow_info(flow):
"""Print a summary of the flow information"""
print('Flow %s (%s)-- Packets:%d Bytes:%d Payload: %s...' % (flow['flow_id'], flow['direction'], len(flow['packet_list']),
len(flow['payload']), repr(flow['payload'])[:30])) |
def packets_to_flows(self):
"""Combine packets into flows"""
# For each packet, place it into either an existing flow or a new flow
for packet in self.input_stream:
# Compute flow tuple and add the packet to the flow
flow_id = flow_utils.flow_tuple(packet)
s... |
def _copy_static_assets(self, output_dir):
"""Copy assets for the report to `output_dir`.
:param output_dir: directory to output assets to
:type output_dir: `str`
"""
base_directory = 'assets/{}'.format(self._report_name)
for asset in resource_listdir(self._package_name... |
def _get_template(self):
"""Returns a template for this report.
:rtype: `jinja2.Template`
"""
loader = PackageLoader(self._package_name, 'assets/templates')
env = Environment(extensions=['jinja2.ext.with_'], loader=loader)
return env.get_template('{}.html'.format(self._... |
def _write(self, context, report_dir, report_name, assets_dir=None,
template=None):
"""Writes the data in `context` in the report's template to
`report_name` in `report_dir`.
If `assets_dir` is supplied, copies all assets for this report
to the specified directory.
... |
def pull(self):
"""Print out summary information about each packet from the input_stream"""
# For each packet in the pcap process the contents
for item in self.input_stream:
# Print out the timestamp in UTC
print('%s -' % item['timestamp'], end='')
# Transp... |
def pull(self):
"""Print out information about each packet from the input_stream"""
# For each packet in the pcap process the contents
for item in self.input_stream:
# Print out the timestamp in UTC
print('Timestamp: %s' % item['timestamp'])
# Unpack the Et... |
def load_backend(**orm_config):
"""
Load the client adaptor module of django_peeringdb
Assumes config is valid.
"""
settings = {}
settings['SECRET_KEY'] = orm_config.get('secret_key', '')
db_config = orm_config['database']
if db_config:
settings['DATABASES'] = {
'def... |
def add_label_count(self):
"""Adds to each result row a count of the number of occurrences of
that n-gram across all works within the label.
This count uses the highest witness count for each work.
"""
self._logger.info('Adding label count')
def add_label_count(df):
... |
def add_label_work_count(self):
"""Adds to each result row a count of the number of works within the
label contain that n-gram.
This counts works that have at least one witness carrying the
n-gram.
This correctly handles cases where an n-gram has only zero
counts for a ... |
def _annotate_bifurcated_extend_data(self, row, smaller, larger, tokenize,
join):
"""Returns `row` annotated with whether it should be deleted or not.
An n-gram is marked for deletion if:
* its label count is 1 and its constituent (n-1)-grams also
... |
def bifurcated_extend(self, corpus, max_size):
"""Replaces the results with those n-grams that contain any of the
original n-grams, and that represent points at which an n-gram
is a constituent of multiple larger n-grams with a lower label
count.
:param corpus: corpus of works t... |
def collapse_witnesses(self):
"""Groups together witnesses for the same n-gram and work that has the
same count, and outputs a single row for each group.
This output replaces the siglum field with a sigla field that
provides a comma separated list of the witness sigla. Due to
th... |
def csv(self, fh):
"""Writes the results data to `fh` in CSV format and returns `fh`.
:param fh: file to write data to
:type fh: file object
:rtype: file object
"""
self._matches.to_csv(fh, encoding='utf-8', float_format='%d',
index=False)
... |
def excise(self, ngram):
"""Removes all rows whose n-gram contains `ngram`.
This operation uses simple string containment matching. For
tokens that consist of multiple characters, this means that
`ngram` may be part of one or two tokens; eg, "he m" would
match on "she may".
... |
def extend(self, corpus):
"""Adds rows for all longer forms of n-grams in the results that are
present in the witnesses.
This works with both diff and intersect results.
:param corpus: corpus of works to which results belong
:type corpus: `Corpus`
"""
self._log... |
def _generate_extended_matches(self, extended_ngrams, highest_n, work,
siglum, label):
"""Returns extended match data derived from `extended_ngrams`.
This extended match data are the counts for all intermediate
n-grams within each extended n-gram.
:pa... |
def _generate_extended_ngrams(self, matches, work, siglum, label, corpus,
highest_n):
"""Returns the n-grams of the largest size that exist in `siglum`
witness to `work` under `label`, generated from adding
together overlapping n-grams in `matches`.
:pa... |
def _generate_filter_ngrams(self, data, min_size):
"""Returns the n-grams in `data` that do not contain any other n-gram
in `data`.
:param data: n-gram results data
:type data: `pandas.DataFrame`
:param min_size: minimum n-gram size in `data`
:type min_size: `int`
... |
def _generate_substrings(self, ngram, size):
"""Returns a list of all substrings of `ngram`.
:param ngram: n-gram to generate substrings of
:type ngram: `str`
:param size: size of `ngram`
:type size: `int`
:rtype: `list`
"""
text = Text(ngram, self._toke... |
def group_by_ngram(self, labels):
"""Groups result rows by n-gram and label, providing a single summary
field giving the range of occurrences across each work's
witnesses. Results are sorted by n-gram then by label (in the
order given in `labels`).
:param labels: labels to order... |
def group_by_witness(self):
"""Groups results by witness, providing a single summary field giving
the n-grams found in it, a count of their number, and the count of
their combined occurrences."""
if self._matches.empty:
# Ensure that the right columns are used, even though th... |
def _is_intersect_results(results):
"""Returns False if `results` has an n-gram that exists in only one
label, True otherwise.
:param results: results to analyze
:type results: `pandas.DataFrame`
:rtype: `bool`
"""
sample = results.iloc[0]
ngram = sample... |
def prune_by_ngram(self, ngrams):
"""Removes results rows whose n-gram is in `ngrams`.
:param ngrams: n-grams to remove
:type ngrams: `list` of `str`
"""
self._logger.info('Pruning results by n-gram')
self._matches = self._matches[
~self._matches[constants.N... |
def prune_by_ngram_count(self, minimum=None, maximum=None, label=None):
"""Removes results rows whose total n-gram count (across all
works bearing this n-gram) is outside the range specified by
`minimum` and `maximum`.
For each text, the count used as part of the sum across all
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.