Skip to content

Commit 646b2dc

Browse files
add function for breakouts
1 parent 40b1826 commit 646b2dc

4 files changed

Lines changed: 72 additions & 9 deletions

File tree

README.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,29 @@ Get a free two week Calcbench trial @ https://www.calcbench.com/join.
99

1010
Your Calcbench username (your email) and Calcbench password are your credentials for this package.
1111

12-
See examples @ http://blog.calcbench.com/post/114062921353/calcbench-python-client.
12+
See examples @ https://www.calcbench.com/home/api
1313

1414
The client returns data in Pandas DataFrames.
1515

1616
To install the client with pip use:
1717

1818
pip install git+git://github.com/calcbench/python_api_client.git
1919

20-
To set your credentials either set CALCBENCH_USERNAME and CALCBENCH_PASSWORD environment variables or call
20+
To set your credentials either set CALCBENCH_USERNAME and CALCBENCH_PASSWORD environment variables or call:
2121

2222
calcbench.set_credentials({calcbench_username}, {calcbench_password})
2323

24-
To get normalized data call `get_normalized_data`, for instance
24+
To get normalized data call `normalized_dataframe`, for instance:
2525

26-
calcbench.get_normalized_data(company_identifiers=['msft', 'ibm'], metrics=['revenue', 'assets'], start_year=2010, start_period=1, end_year=2014, end_period=4)
26+
calcbench.normalized_dataframe(company_identifiers=['msft', 'ibm'], metrics=['revenue', 'assets'], start_year=2010, start_period=1, end_year=2014, end_period=4)
27+
28+
To get 'As Reported' statements, call `as_reported_raw`, for instance:
29+
30+
calcbench.as_reported('msft', 'income')
31+
32+
To get breakout/segments call `breakouts_raw`, for instance:
33+
34+
calcbench.breakouts_raw(company_identifiers=['MSFT', 'AXP'], metrics=['operatingSegmentRevenue', 'operatingSegmentAssets'])
2735

2836
Company identifiers, tickers in most cases, can be retrieved by Standard Industrial Classification (SIC) code or index, for instance
2937

calcbench/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '0.0.5'
2-
from .api_client import normalized_data, normalized, tickers, set_credentials, companies, normalized_raw, as_reported_raw
1+
__version__ = '0.0.6'
2+
from .api_client import normalized_data, normalized_dataframe, tickers, set_credentials, companies, normalized_raw, as_reported_raw, breakouts_raw

calcbench/api_client.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def set_credentials(cb_username, cb_password):
5050
_calcbench_session() #Make sure credentials work.
5151

5252

53-
def normalized(company_identifiers,
53+
def normalized_dataframe(company_identifiers,
5454
metrics,
5555
start_year,
5656
start_period,
@@ -94,7 +94,7 @@ def normalized(company_identifiers,
9494
data = data[metrics]
9595
return data
9696

97-
normalized_data = normalized # used to call it normalized_data.
97+
normalized_data = normalized_dataframe # used to call it normalized_data.
9898

9999
def normalized_raw(company_identifiers,
100100
metrics,
@@ -221,6 +221,51 @@ def as_reported_raw(company_identifier,
221221
return data
222222

223223

224+
def breakouts_raw(company_identifiers=None, metrics=[], start_year=None,
225+
start_period=None, end_year=None, end_period=None, period_type='annual'):
226+
'''
227+
Breakouts
228+
229+
Get breakouts/segments, see https://www.calcbench.com/breakout.
230+
231+
Args:
232+
company_identifiers : list of tickers or CIK codes
233+
metrics : list of breakouts, get the list @ https://www.calcbench.com/api/availableBreakouts, pass in the "databaseName".
234+
start_year: first year of data to get
235+
start_period: first period of data to get. 0 for annual data, 1, 2, 3, 4 for quarterly data.
236+
end_year: last year of data to get
237+
end_period: last period of data to get. 0 for annual data, 1, 2, 3, 4 for quarterly data.
238+
period_type: quarterly or annual, only applicable when other period data not supplied.
239+
240+
Returns:
241+
A list of breakout points. The points correspond to the lines @ https://www.calcbench.com/breakout. For each requested metric there will \
242+
be a the formatted value and the unformatted value denote by _effvalue. The label dimension label associated with the values.
243+
244+
245+
'''
246+
if len(metrics) == 0:
247+
raise(ValueError("Need to supply at least one breakout."))
248+
if period_type not in ('annual', 'quarterly'):
249+
raise(ValueError("period_type must be in ('annual', 'quarterly')"))
250+
payload = {'companiesParameters' : {'entireUniverse' : len(company_identifiers) == 0,
251+
'companyIdentifiers' : company_identifiers},
252+
'periodParameters' : {'year' : start_year,
253+
'period' : start_period,
254+
'endYear' : end_year,
255+
'endPeriod' : end_period,
256+
'periodType' : period_type},
257+
'pageParameters' : {'metrics' : metrics}}
258+
url = _CALCBENCH_API_URL_BASE.format('breakouts')
259+
response = _calcbench_session().post(url,
260+
data=json.dumps(payload),
261+
headers={'content-type' : 'application/json'},
262+
verify=_SSL_VERIFY)
263+
response.raise_for_status()
264+
data = response.json()
265+
return data
266+
267+
268+
224269
def _build_quarter_period(data_point):
225270
return pd.Period(year=data_point.pop('calendar_year'),
226271
quarter=data_point.pop('calendar_period'),
@@ -256,6 +301,16 @@ def _companies(SIC_codes, index):
256301
r.raise_for_status()
257302
return r.json()
258303

304+
305+
def _test_locally():
306+
global _CALCBENCH_API_URL_BASE
307+
global _CALCBENCH_LOGON_URL
308+
global _SSL_VERIFY
309+
_CALCBENCH_API_URL_BASE = "https://localhost:444/api/{0}"
310+
_CALCBENCH_LOGON_URL = 'https://localhost:444/account/LogOnAjax'
311+
_SSL_VERIFY = False
312+
print(breakouts_raw(['msft'], ['operatingSegmentRevenue']))
313+
259314
if __name__ == '__main__':
260315
data = normalized_data(company_identifiers=['ibm', 'msft'],
261316
metrics=['revenue', 'assets', ],

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from setuptools import setup
1010

1111
setup(name='calcbench',
12-
version='0.0.5',
12+
version='0.0.6',
1313
description='Client for Calcbench data.',
1414
author='Andrew Kittredge',
1515
author_email='andrew@calcbench.com',

0 commit comments

Comments
 (0)