Skip to content

Commit

Permalink
Refactor logging
Browse files Browse the repository at this point in the history
Refactor message logging into _receive_events, add event type logging.
Minor style changes to logging and added consistent request logging.
  • Loading branch information
matthewgilbert committed Sep 25, 2018
1 parent 297b40f commit e93e0e5
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions pdblp/pdblp.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ def _create_req(self, rtype, tickers, flds, ovrds, setvals):
return request

def _receive_events(self, sent_events=1):
logger = _get_logger(self.debug)
while True:
# We provide timeout to give the chance for Ctrl+C handling:
ev = self.session.nextEvent(self.timeout)
logger.info("Event Type: %s" % _EVENT_DICT[ev.eventType()])
if ev.eventType() in RESPONSE_TYPES:
for msg in ev:
logger.info("Message Received:\n%s" % msg)
yield msg
if ev.eventType() == blpapi.Event.RESPONSE:
sent_events -= 1
Expand Down Expand Up @@ -268,13 +270,12 @@ def _bdh_list(self, tickers, flds, start_date, end_date, elms,

request = self._create_req("HistoricalDataRequest", tickers, flds,
ovrds, setvals)
logger.info("Sending Request:\n %s" % request)
logger.info("Sending Request:\n%s" % request)
# Send the request
self.session.sendRequest(request)
data = []
# Process received events
for msg in self._receive_events():
logger.info("Message Received:\n %s" % msg)
has_security_error = (msg.getElement('securityData')
.hasElement('securityError'))
has_field_exception = (msg.getElement('securityData')
Expand Down Expand Up @@ -337,19 +338,17 @@ def ref(self, tickers, flds, ovrds=None):
flds = [flds]
request = self._create_req("ReferenceDataRequest", tickers, flds,
ovrds, [])
logger.info("Sending Request:\n %s" % request)
logger.info("Sending Request:\n%s" % request)
self.session.sendRequest(request)
data = self._parse_ref(flds)
data = pd.DataFrame(data)
data.columns = ["ticker", "field", "value"]
return data

def _parse_ref(self, flds, keep_corrId=False, sent_events=1):
logger = _get_logger(self.debug)
data = []
# Process received events
for msg in self._receive_events(sent_events):
logger.info("Message Received:\n %s" % msg)
if keep_corrId:
corrId = [msg.correlationIds()[0].value()]
else:
Expand Down Expand Up @@ -442,19 +441,17 @@ def bulkref(self, tickers, flds, ovrds=None):
setvals = []
request = self._create_req("ReferenceDataRequest", tickers, flds,
ovrds, setvals)
logger.info("Sending Request:\n %s" % request)
logger.info("Sending Request:\n%s" % request)
self.session.sendRequest(request)
data = self._parse_bulkref(flds)
data = pd.DataFrame(data)
data.columns = ["ticker", "field", "name", "value", "position"]
return data

def _parse_bulkref(self, flds, keep_corrId=False, sent_events=1):
logger = _get_logger(self.debug)
data = []
# Process received events
for msg in self._receive_events(sent_events):
logger.info("Message Received:\n %s" % msg)
if keep_corrId:
corrId = [msg.correlationIds()[0].value()]
else:
Expand Down Expand Up @@ -624,7 +621,7 @@ def _send_hist(self, tickers, flds, dates, date_field, ovrds):
# CorrelationID used to keep track of which response coincides with
# which request
cid = blpapi.CorrelationId(dt)
logger.info("Sending Request:\n %s" % request)
logger.info("Sending Request:\n%s" % request)
self.session.sendRequest(request, correlationId=cid)

def bdib(self, ticker, start_datetime, end_datetime, event_type, interval,
Expand Down Expand Up @@ -668,15 +665,14 @@ def bdib(self, ticker, start_datetime, end_datetime, event_type, interval,
for name, val in elms:
request.set(name, val)

logger.info("Sending Request:\n %s" % request)
logger.info("Sending Request:\n%s" % request)
# Send the request
self.session.sendRequest(request)
# defaultdict - later convert to pandas
data = defaultdict(dict)
# Process received events
flds = ['open', 'high', 'low', 'close', 'volume', 'numEvents']
for msg in self._receive_events():
logger.info("Message Received:\n %s" % msg)
barTick = (msg.getElement('barData')
.getElement('barTickData'))
for i in range(barTick.numValues()):
Expand Down Expand Up @@ -712,10 +708,10 @@ def bsrch(self, domain):
logger = _get_logger(self.debug)
request = self.exrService.createRequest("ExcelGetGridRequest")
request.set("Domain", domain)
logger.info("Sending Request:\n%s" % request)
self.session.sendRequest(request)
data = []
for msg in self._receive_events():
logger.info("Message Received:\n %s" % msg)
for v in msg.getElement("DataRecords").values():
for f in v.getElement("DataFields").values():
data.append(f.getElementAsString("StringValue"))
Expand Down

0 comments on commit e93e0e5

Please sign in to comment.