This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
forked from Manishearth/ChatExchange
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Manishearth#100 from ArtOfCode-/master
Documentation #1: _utils.py
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Documentation: `_utils.py` | ||
Contains utility functions for use within ChatExchange. Not originally indented for external use, but are accessible from external code. | ||
|
||
#### `log_and_ignore_exceptions(f, exceptions=Exception, logger=logging.getLogger('exceptions'))` | ||
Wraps a function to catch and log any exceptions it throws. | ||
|
||
**`f`** is the function to wrap. Required. | ||
**`exceptions`** are the exceptions to catch from `f`. An array of exception types. Optional. | ||
**`logger`** is the logging manager. You should not need to specify this. Optional. | ||
|
||
----- | ||
|
||
### `class HTMLTextExtractor(HTMLParser)` | ||
Extends the `HTMLParser` class, which provides methods for working with raw HTML. Adapted from a [Stack Overflow post](http://stackoverflow.com/a/7778368) by [Søren Løvborg](http://stackoverflow.com/u/13679) and Eloff. | ||
|
||
#### `handle_data(self, d)` | ||
Appends the given data, **`d`**, to the class' result. | ||
|
||
#### `handle_charref(self, number)` | ||
Finds the codepoint specified by **`number`** and appends the Unicode character at this codepoint to the class' result. | ||
|
||
#### `handle_entityref(self, name)` | ||
Finds a codepoint based on the HTML entity provided in **`name`** and appends it to the class' result. | ||
|
||
----- | ||
|
||
#### `html_to_text(html)` | ||
When given a string of valid HTML in **`html`**, returns the text contained in it. Internally, uses the `HTMLTextExtractor` class. | ||
|
||
----- | ||
|
||
### `class LazyFrom(object)` | ||
A descriptor used when multiple lazy attributes depend on a common source of data. This class lazily extracts data from the specified object and returns it. Only special methods are defined in this class and should not be directly used. For an example of usage, see [`messages.py`](https://github.com/Manishearth/ChatExchange/blob/master/chatexchange/messages.py). |