Skip to content

Commit 2ab9ddc

Browse files
author
Dario Varotto
committed
Version 1.0.25: don't buffer RT requests to avoid waiting for chunks
* Updated README and examples
1 parent 0d8168b commit 2ab9ddc

File tree

9 files changed

+113
-28
lines changed

9 files changed

+113
-28
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ from ravenpackapi import RPApi
2828
api = RPApi(api_key="YOUR_API_KEY")
2929
```
3030

31+
### Creating a new dataset
32+
33+
To create a dataset you can call the `create_dataset` method of the API with a Dataset instance.
34+
35+
```python
36+
ds = api.create_dataset(
37+
Dataset(
38+
name="New Dataset",
39+
filters={
40+
"relevance": {
41+
"$gte": 90
42+
}
43+
},
44+
)
45+
)
46+
print("Dataset created", ds)
47+
```
48+
3149
### Getting data from the datasets
3250

3351
In the API wrapper, there are several models that maybe used for interacting with data.

README.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ environment variable or set it in your code:
3939
4040
api = RPApi(api_key="YOUR_API_KEY")
4141
42+
Creating a new dataset
43+
~~~~~~~~~~~~~~~~~~~~~~
44+
45+
To create a dataset you can call the ``create_dataset`` method of the
46+
API with a Dataset instance.
47+
48+
.. code:: python
49+
50+
ds = api.create_dataset(
51+
Dataset(
52+
name="New Dataset",
53+
filters={
54+
"relevance": {
55+
"$gte": 90
56+
}
57+
},
58+
)
59+
)
60+
print("Dataset created", ds)
61+
4262
Getting data from the datasets
4363
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4464

ravenpackapi/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ravenpackapi.utils.constants import JSON_AVAILABLE_FIELDS, ENTITY_TYPES
1515

1616
_VALID_METHODS = ('get', 'post', 'put', 'delete')
17-
VERSION = '1.0.24'
17+
VERSION = '1.0.25'
1818

1919
logger = logging.getLogger("ravenpack.core")
2020

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from ravenpackapi import RPApi, Dataset
2+
3+
api = RPApi()
4+
5+
ds = api.create_dataset(
6+
Dataset(
7+
**{
8+
"product": "rpa",
9+
"product_version": "1.0",
10+
"name": "Events in UK - example",
11+
"fields": [
12+
"timestamp_utc",
13+
"rp_story_id",
14+
"rp_entity_id",
15+
"entity_type",
16+
"entity_name",
17+
"country_code",
18+
"relevance",
19+
"event_sentiment_score",
20+
"topic",
21+
"group",
22+
"headline"
23+
],
24+
"filters": {
25+
"$and": [
26+
{
27+
"relevance": {
28+
"$gte": 90
29+
}
30+
},
31+
{
32+
"country_code": {
33+
"$in": [
34+
"GB"
35+
]
36+
}
37+
},
38+
{
39+
"event_sentiment_score": {
40+
"$nbetween": [-0.5, 0.5]
41+
}
42+
}
43+
]
44+
},
45+
"frequency": "granular",
46+
}
47+
)
48+
)
49+
50+
print("Dataset created", ds)
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
from ravenpackapi import RPApi
22

3-
4-
class TestEntityMapping(object):
3+
if __name__ == '__main__':
4+
entities = [{'ticker': 'AAPL', 'name': 'Apple Inc.'},
5+
{'ticker': 'JPM'},
6+
{'listing': 'XNYS:DVN'}]
57
api = RPApi()
68

7-
def test_matching_entity_mapping(self):
8-
entities = [{'ticker': 'AAPL', 'name': 'Apple Inc.'},
9-
{'ticker': 'JPM'},
10-
{'listing': 'XNYS:DVN'}]
11-
api = self.api
12-
mapping = api.get_entity_mapping(entities)
13-
assert not mapping.errors
14-
assert len(mapping.matched) == len(mapping.submitted) == 3
15-
16-
# let's get the first mapped entities
17-
rp_entity_ids = [match.id for match in mapping.matched]
18-
assert rp_entity_ids == ['D8442A', '619882', '14BA06']
9+
mapping = api.get_entity_mapping(entities)
1910

20-
def test_mismatch_mapping(self):
21-
entities = ["unknown!"]
22-
api = self.api
23-
mapping = api.get_entity_mapping(entities)
24-
rp_entity_ids = [match.id for match in mapping.matched]
25-
assert rp_entity_ids == []
11+
# show the matched entities
12+
for match in mapping.matched:
13+
print(match.id, match.name, match.type, match.request)

ravenpackapi/models/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import requests
44

55
from ravenpackapi.exceptions import api_method, APIException
6+
from ravenpackapi.util import to_curl
67
from ravenpackapi.utils.constants import JSON_AVAILABLE_FIELDS
78
from ravenpackapi.utils.date_formats import as_datetime_str
8-
from ravenpackapi.util import to_curl
99
from .job import Job
1010
from .results import Results, Result
1111

@@ -240,5 +240,5 @@ def request_realtime(self):
240240
), response=response)
241241
response.encoding = 'utf-8'
242242

243-
for line in response.iter_lines(decode_unicode=True):
243+
for line in response.iter_lines(decode_unicode=True, chunk_size=1):
244244
yield Result(line)

ravenpackapi/tests/query_entity_mapping.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_mapping_example(self):
2727
universe = [
2828
"RavenPack",
2929
{'ticker': 'AAPL'},
30-
'California USA',
30+
'California, U.S.',
3131
{ # Amazon, specifying various fields
3232
"client_id": "12345-A",
3333
"date": "2017-01-01",
@@ -50,3 +50,12 @@ def test_mapping_example(self):
5050
]
5151
assert len(mapping.errors) == 1
5252
assert mapping.errors[0].request == invalid_entity_request
53+
54+
def test_matching_by_cusip(self):
55+
entities = [{'cusip': '037833100', },
56+
{'cusip': '48127A161'},
57+
{'cusip': '25179M103'}]
58+
api = self.api
59+
mapping = api.get_entity_mapping(entities)
60+
assert not mapping.errors
61+
assert len(mapping.matched) == len(mapping.submitted) == 3

ravenpackapi/tests/test_dataset_update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TestDatasetUpdate(object):
99

1010
def test_create_and_update(self):
1111
delete_all_datasets_by_name(self.api, self.dataset_name)
12-
filters = {"rp_entity_id": {"$in": ['AAAAA']}}
12+
filters = {"rp_entity_id": {"$in": ['AAAAAA']}}
1313
dataset = Dataset(
1414
name=self.dataset_name,
1515
filters=filters, # a dataset with a filter
@@ -20,14 +20,14 @@ def test_create_and_update(self):
2020
dataset_id = dataset.id
2121

2222
# change the dataset
23-
new_filters = {"rp_entity_id": {"$in": ['BBBBB']}}
23+
new_filters = {"rp_entity_id": {"$in": ['BBBBBB']}}
2424
dataset.filters = new_filters
2525
dataset.save()
2626

2727
# get the dataset again
2828
dataset = self.api.get_dataset(dataset_id)
2929
assert dataset.filters == new_filters
30-
new_filters = {"rp_entity_id": {"$in": ['CCCCC']}}
30+
new_filters = {"rp_entity_id": {"$in": ['CCCCCC']}}
3131
dataset.filters = new_filters
3232
dataset.save()
3333

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.24'
3+
VERSION = '1.0.25'
44

55
with open('README.rst') as readme_file:
66
readme = readme_file.read()

0 commit comments

Comments
 (0)