@@ -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
9999def 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+
224269def _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+
259314if __name__ == '__main__' :
260315 data = normalized_data (company_identifiers = ['ibm' , 'msft' ],
261316 metrics = ['revenue' , 'assets' , ],
0 commit comments