Skip to content

Commit c715bc0

Browse files
committed
docs
1 parent cfa3d55 commit c715bc0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

twarc/expansions.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""
22
This module contains a list of the known Twitter V2+ API expansions and fields
3-
for each expansion, and a function for "flattening" a result set, including all
4-
expansions inline
3+
for each expansion, and a function flatten() for "flattening" a result set,
4+
including all expansions inline.
55
6+
ensure_flattened() can be used in tweet processing programs that need to make
7+
sure that data is flattened.
68
"""
79

810
from collections import defaultdict
@@ -116,11 +118,12 @@ def extract_includes(response, expansion, _id="id"):
116118
def flatten(response):
117119
"""
118120
Flatten an API response by moving all "included" entities inline with the
119-
tweets they are referenced from. Expects an entire page response from the
120-
API (data, includes, meta) and will raise a ValueError if what is passed in
121-
does not appear to be an API response. Returns empty objects for things
122-
missing in includes. Returns a list of dictionaries where each dictionary
123-
is a tweet.
121+
tweets they are referenced from. flatten expects an entire page response
122+
from the API (data, includes, meta) and will raise a ValueError if what is
123+
passed in does not appear to be an API response. It will return a list of
124+
dictionaries where each dictionary represents a tweet. Empty objects will
125+
be returned for things that are missing in includes, which can happen when
126+
protected or delete users or tweets are referenced.
124127
"""
125128

126129
# Users extracted both by id and by username for expanding mentions
@@ -220,10 +223,16 @@ def expand_payload(payload):
220223

221224
def ensure_flattened(data):
222225
"""
223-
Will ensure that the supplied data is "flattened". The supplied data can be a
226+
Will ensure that the supplied data is "flattened". The input data can be a
224227
response from the Twitter API, a list of tweet dictionaries, or a single tweet
225-
dictionary. An exception will be thrown if the supplied data is not
226-
recognizable or it cannot be flattened.
228+
dictionary. It will always return a list of tweet dictionaries. A ValueError
229+
will be thrown if the supplied data is not recognizable or it cannot be
230+
flattened.
231+
232+
ensure_flattened is designed for use in twarc plugins and other tweet
233+
processing applications that want to operate on a stream of tweets, and
234+
examine included entities like users and tweets without hunting and
235+
pecking in the response data.
227236
"""
228237
if isinstance(data, dict) and 'data' in data:
229238
return flatten(data)

0 commit comments

Comments
 (0)