partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
valid
OutstationCommandHandler.Select
The Master sent a Select command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :return: CommandStatus
examples/outstation.py
def Select(self, command, index): """ The Master sent a Select command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :return: CommandStatus """ OutstationApplication.process_point_value('Select', command, index, None) return opendnp3.CommandStatus.SUCCESS
def Select(self, command, index): """ The Master sent a Select command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :return: CommandStatus """ OutstationApplication.process_point_value('Select', command, index, None) return opendnp3.CommandStatus.SUCCESS
[ "The", "Master", "sent", "a", "Select", "command", "to", "the", "Outstation", ".", "Handle", "it", "." ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation.py#L231-L241
[ "def", "Select", "(", "self", ",", "command", ",", "index", ")", ":", "OutstationApplication", ".", "process_point_value", "(", "'Select'", ",", "command", ",", "index", ",", "None", ")", "return", "opendnp3", ".", "CommandStatus", ".", "SUCCESS" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCommandHandler.Operate
The Master sent an Operate command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :param op_type: OperateType :return: CommandStatus
examples/outstation.py
def Operate(self, command, index, op_type): """ The Master sent an Operate command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :param op_type: OperateType :return: CommandStatus """ OutstationApplication.process_point_value('Operate', command, index, op_type) return opendnp3.CommandStatus.SUCCESS
def Operate(self, command, index, op_type): """ The Master sent an Operate command to the Outstation. Handle it. :param command: ControlRelayOutputBlock, AnalogOutputInt16, AnalogOutputInt32, AnalogOutputFloat32, or AnalogOutputDouble64. :param index: int :param op_type: OperateType :return: CommandStatus """ OutstationApplication.process_point_value('Operate', command, index, op_type) return opendnp3.CommandStatus.SUCCESS
[ "The", "Master", "sent", "an", "Operate", "command", "to", "the", "Outstation", ".", "Handle", "it", "." ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation.py#L243-L254
[ "def", "Operate", "(", "self", ",", "command", ",", "index", ",", "op_type", ")", ":", "OutstationApplication", ".", "process_point_value", "(", "'Operate'", ",", "command", ",", "index", ",", "op_type", ")", "return", "opendnp3", ".", "CommandStatus", ".", "SUCCESS" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_a
Send the Master an AnalogInput (group 32) value. Command syntax is: a index value
examples/outstation_cmd.py
def do_a(self, line): """Send the Master an AnalogInput (group 32) value. Command syntax is: a index value""" index, value_string = self.index_and_value_from_line(line) if index and value_string: try: self.application.apply_update(opendnp3.Analog(float(value_string)), index) except ValueError: print('Please enter a floating-point value as the second argument.')
def do_a(self, line): """Send the Master an AnalogInput (group 32) value. Command syntax is: a index value""" index, value_string = self.index_and_value_from_line(line) if index and value_string: try: self.application.apply_update(opendnp3.Analog(float(value_string)), index) except ValueError: print('Please enter a floating-point value as the second argument.')
[ "Send", "the", "Master", "an", "AnalogInput", "(", "group", "32", ")", "value", ".", "Command", "syntax", "is", ":", "a", "index", "value" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L37-L44
[ "def", "do_a", "(", "self", ",", "line", ")", ":", "index", ",", "value_string", "=", "self", ".", "index_and_value_from_line", "(", "line", ")", "if", "index", "and", "value_string", ":", "try", ":", "self", ".", "application", ".", "apply_update", "(", "opendnp3", ".", "Analog", "(", "float", "(", "value_string", ")", ")", ",", "index", ")", "except", "ValueError", ":", "print", "(", "'Please enter a floating-point value as the second argument.'", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_b
Send the Master a BinaryInput (group 2) value. Command syntax is: 'b index true' or 'b index false
examples/outstation_cmd.py
def do_b(self, line): """Send the Master a BinaryInput (group 2) value. Command syntax is: 'b index true' or 'b index false'""" index, value_string = self.index_and_value_from_line(line) if index and value_string: if value_string.lower() == 'true' or value_string.lower() == 'false': self.application.apply_update(opendnp3.Binary(value_string == 'true'), index) else: print('Please enter true or false as the second argument.')
def do_b(self, line): """Send the Master a BinaryInput (group 2) value. Command syntax is: 'b index true' or 'b index false'""" index, value_string = self.index_and_value_from_line(line) if index and value_string: if value_string.lower() == 'true' or value_string.lower() == 'false': self.application.apply_update(opendnp3.Binary(value_string == 'true'), index) else: print('Please enter true or false as the second argument.')
[ "Send", "the", "Master", "a", "BinaryInput", "(", "group", "2", ")", "value", ".", "Command", "syntax", "is", ":", "b", "index", "true", "or", "b", "index", "false" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L50-L57
[ "def", "do_b", "(", "self", ",", "line", ")", ":", "index", ",", "value_string", "=", "self", ".", "index_and_value_from_line", "(", "line", ")", "if", "index", "and", "value_string", ":", "if", "value_string", ".", "lower", "(", ")", "==", "'true'", "or", "value_string", ".", "lower", "(", ")", "==", "'false'", ":", "self", ".", "application", ".", "apply_update", "(", "opendnp3", ".", "Binary", "(", "value_string", "==", "'true'", ")", ",", "index", ")", "else", ":", "print", "(", "'Please enter true or false as the second argument.'", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_b0
Send the Master a BinaryInput (group 2) value of False at index 6. Command syntax is: b0
examples/outstation_cmd.py
def do_b0(self, line): """Send the Master a BinaryInput (group 2) value of False at index 6. Command syntax is: b0""" self.application.apply_update(opendnp3.Binary(False), index=6)
def do_b0(self, line): """Send the Master a BinaryInput (group 2) value of False at index 6. Command syntax is: b0""" self.application.apply_update(opendnp3.Binary(False), index=6)
[ "Send", "the", "Master", "a", "BinaryInput", "(", "group", "2", ")", "value", "of", "False", "at", "index", "6", ".", "Command", "syntax", "is", ":", "b0" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L59-L61
[ "def", "do_b0", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "apply_update", "(", "opendnp3", ".", "Binary", "(", "False", ")", ",", "index", "=", "6", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_c
Send the Master a Counter (group 22) value. Command syntax is: c index value
examples/outstation_cmd.py
def do_c(self, line): """Send the Master a Counter (group 22) value. Command syntax is: c index value""" index, value_string = self.index_and_value_from_line(line) if index and value_string: try: self.application.apply_update(opendnp3.Counter(int(value_string)), index) except ValueError: print('Please enter an integer value as the second argument.')
def do_c(self, line): """Send the Master a Counter (group 22) value. Command syntax is: c index value""" index, value_string = self.index_and_value_from_line(line) if index and value_string: try: self.application.apply_update(opendnp3.Counter(int(value_string)), index) except ValueError: print('Please enter an integer value as the second argument.')
[ "Send", "the", "Master", "a", "Counter", "(", "group", "22", ")", "value", ".", "Command", "syntax", "is", ":", "c", "index", "value" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L63-L70
[ "def", "do_c", "(", "self", ",", "line", ")", ":", "index", ",", "value_string", "=", "self", ".", "index_and_value_from_line", "(", "line", ")", "if", "index", "and", "value_string", ":", "try", ":", "self", ".", "application", ".", "apply_update", "(", "opendnp3", ".", "Counter", "(", "int", "(", "value_string", ")", ")", ",", "index", ")", "except", "ValueError", ":", "print", "(", "'Please enter an integer value as the second argument.'", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_d
Send the Master a DoubleBitBinaryInput (group 4) value of DETERMINED_ON. Command syntax is: d index
examples/outstation_cmd.py
def do_d(self, line): """Send the Master a DoubleBitBinaryInput (group 4) value of DETERMINED_ON. Command syntax is: d index""" index = self.index_from_line(line) if index: self.application.apply_update(opendnp3.DoubleBitBinary(opendnp3.DoubleBit.DETERMINED_ON), index)
def do_d(self, line): """Send the Master a DoubleBitBinaryInput (group 4) value of DETERMINED_ON. Command syntax is: d index""" index = self.index_from_line(line) if index: self.application.apply_update(opendnp3.DoubleBitBinary(opendnp3.DoubleBit.DETERMINED_ON), index)
[ "Send", "the", "Master", "a", "DoubleBitBinaryInput", "(", "group", "4", ")", "value", "of", "DETERMINED_ON", ".", "Command", "syntax", "is", ":", "d", "index" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L72-L76
[ "def", "do_d", "(", "self", ",", "line", ")", ":", "index", "=", "self", ".", "index_from_line", "(", "line", ")", "if", "index", ":", "self", ".", "application", ".", "apply_update", "(", "opendnp3", ".", "DoubleBitBinary", "(", "opendnp3", ".", "DoubleBit", ".", "DETERMINED_ON", ")", ",", "index", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.do_menu
Display a menu of command-line options. Command syntax is: menu
examples/outstation_cmd.py
def do_menu(self, line): """Display a menu of command-line options. Command syntax is: menu""" print('\ta\t\tAnalog measurement.\tEnter index and value as arguments.') print('\ta2\t\tAnalog 2 for MMDC.Vol (index 4).') print('\tb\t\tBinary measurement.\tEnter index and value as arguments.') print('\tb0\t\tBinary False for MMDC1.Amp.range (index 6).') print('\tc\t\tCounter measurement.\tEnter index and value as arguments.') print('\td\t\tDoubleBit DETERMINED_ON.\tEnter index as an argument.') print('\thelp\t\tDisplay command-line help.') print('\tmenu\t\tDisplay this menu.') print('\tquit')
def do_menu(self, line): """Display a menu of command-line options. Command syntax is: menu""" print('\ta\t\tAnalog measurement.\tEnter index and value as arguments.') print('\ta2\t\tAnalog 2 for MMDC.Vol (index 4).') print('\tb\t\tBinary measurement.\tEnter index and value as arguments.') print('\tb0\t\tBinary False for MMDC1.Amp.range (index 6).') print('\tc\t\tCounter measurement.\tEnter index and value as arguments.') print('\td\t\tDoubleBit DETERMINED_ON.\tEnter index as an argument.') print('\thelp\t\tDisplay command-line help.') print('\tmenu\t\tDisplay this menu.') print('\tquit')
[ "Display", "a", "menu", "of", "command", "-", "line", "options", ".", "Command", "syntax", "is", ":", "menu" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L78-L88
[ "def", "do_menu", "(", "self", ",", "line", ")", ":", "print", "(", "'\\ta\\t\\tAnalog measurement.\\tEnter index and value as arguments.'", ")", "print", "(", "'\\ta2\\t\\tAnalog 2 for MMDC.Vol (index 4).'", ")", "print", "(", "'\\tb\\t\\tBinary measurement.\\tEnter index and value as arguments.'", ")", "print", "(", "'\\tb0\\t\\tBinary False for MMDC1.Amp.range (index 6).'", ")", "print", "(", "'\\tc\\t\\tCounter measurement.\\tEnter index and value as arguments.'", ")", "print", "(", "'\\td\\t\\tDoubleBit DETERMINED_ON.\\tEnter index as an argument.'", ")", "print", "(", "'\\thelp\\t\\tDisplay command-line help.'", ")", "print", "(", "'\\tmenu\\t\\tDisplay this menu.'", ")", "print", "(", "'\\tquit'", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.index_and_value_from_line
Parse an index (integer) and value (string) from command line args and return them.
examples/outstation_cmd.py
def index_and_value_from_line(line): """Parse an index (integer) and value (string) from command line args and return them.""" try: index = int(line.split(' ')[0]) except (ValueError, IndexError): print('Please enter an integer index as the first argument.') index = None try: value_string = line.split(' ')[1] except (ValueError, IndexError): print('Please enter a second argument.') value_string = None return index, value_string
def index_and_value_from_line(line): """Parse an index (integer) and value (string) from command line args and return them.""" try: index = int(line.split(' ')[0]) except (ValueError, IndexError): print('Please enter an integer index as the first argument.') index = None try: value_string = line.split(' ')[1] except (ValueError, IndexError): print('Please enter a second argument.') value_string = None return index, value_string
[ "Parse", "an", "index", "(", "integer", ")", "and", "value", "(", "string", ")", "from", "command", "line", "args", "and", "return", "them", "." ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L96-L108
[ "def", "index_and_value_from_line", "(", "line", ")", ":", "try", ":", "index", "=", "int", "(", "line", ".", "split", "(", "' '", ")", "[", "0", "]", ")", "except", "(", "ValueError", ",", "IndexError", ")", ":", "print", "(", "'Please enter an integer index as the first argument.'", ")", "index", "=", "None", "try", ":", "value_string", "=", "line", ".", "split", "(", "' '", ")", "[", "1", "]", "except", "(", "ValueError", ",", "IndexError", ")", ":", "print", "(", "'Please enter a second argument.'", ")", "value_string", "=", "None", "return", "index", ",", "value_string" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
OutstationCmd.index_from_line
Parse an index (integer) from command line args and return it.
examples/outstation_cmd.py
def index_from_line(line): """Parse an index (integer) from command line args and return it.""" try: index = int(line.split(' ')[0]) except (ValueError, IndexError): print('Please enter an integer index as the first argument.') index = None return index
def index_from_line(line): """Parse an index (integer) from command line args and return it.""" try: index = int(line.split(' ')[0]) except (ValueError, IndexError): print('Please enter an integer index as the first argument.') index = None return index
[ "Parse", "an", "index", "(", "integer", ")", "from", "command", "line", "args", "and", "return", "it", "." ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/outstation_cmd.py#L111-L118
[ "def", "index_from_line", "(", "line", ")", ":", "try", ":", "index", "=", "int", "(", "line", ".", "split", "(", "' '", ")", "[", "0", "]", ")", "except", "(", "ValueError", ",", "IndexError", ")", ":", "print", "(", "'Please enter an integer index as the first argument.'", ")", "index", "=", "None", "return", "index" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_menu
Display a menu of command-line options. Command syntax is: menu
examples/master_cmd.py
def do_menu(self, line): """Display a menu of command-line options. Command syntax is: menu""" print('\tchan_log_all\tSet the channel log level to ALL_COMMS.') print('\tchan_log_normal\tSet the channel log level to NORMAL.') print('\tdisable_unsol\tPerform the function DISABLE_UNSOLICITED.') print('\thelp\t\tDisplay command-line help.') print('\tmast_log_all\tSet the master log level to ALL_COMMS.') print('\tmast_log_normal\tSet the master log level to NORMAL.') print('\tmenu\t\tDisplay this menu.') print('\to1\t\tSend a DirectOperate LATCH_ON command.') print('\to2\t\tSend a DirectOperate analog value.') print('\to3\t\tSend a DirectOperate CommandSet.') print('\tquit') print('\trestart\t\tRequest an outstation cold restart.') print('\ts1\t\tSend a SelectAndOperate LATCH_ON command.') print('\ts2\t\tSend a SelectAndOperate CommandSet.') print('\tscan_all\tRead data from the outstation (ScanAllObjects).') print('\tscan_fast\tDemand immediate execution of the fast (every 1 mins) Class 1 scan.') print('\tscan_range\tPerform an ad-hoc scan (ScanRange) of GroupVariation 1.2, range 0..3.') print('\tscan_slow\tDemand immediate execution of the slow (every 30 mins) All-Classes scan.') print('\twrite_time\tWrite a TimeAndInterval to the outstation.')
def do_menu(self, line): """Display a menu of command-line options. Command syntax is: menu""" print('\tchan_log_all\tSet the channel log level to ALL_COMMS.') print('\tchan_log_normal\tSet the channel log level to NORMAL.') print('\tdisable_unsol\tPerform the function DISABLE_UNSOLICITED.') print('\thelp\t\tDisplay command-line help.') print('\tmast_log_all\tSet the master log level to ALL_COMMS.') print('\tmast_log_normal\tSet the master log level to NORMAL.') print('\tmenu\t\tDisplay this menu.') print('\to1\t\tSend a DirectOperate LATCH_ON command.') print('\to2\t\tSend a DirectOperate analog value.') print('\to3\t\tSend a DirectOperate CommandSet.') print('\tquit') print('\trestart\t\tRequest an outstation cold restart.') print('\ts1\t\tSend a SelectAndOperate LATCH_ON command.') print('\ts2\t\tSend a SelectAndOperate CommandSet.') print('\tscan_all\tRead data from the outstation (ScanAllObjects).') print('\tscan_fast\tDemand immediate execution of the fast (every 1 mins) Class 1 scan.') print('\tscan_range\tPerform an ad-hoc scan (ScanRange) of GroupVariation 1.2, range 0..3.') print('\tscan_slow\tDemand immediate execution of the slow (every 30 mins) All-Classes scan.') print('\twrite_time\tWrite a TimeAndInterval to the outstation.')
[ "Display", "a", "menu", "of", "command", "-", "line", "options", ".", "Command", "syntax", "is", ":", "menu" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L41-L61
[ "def", "do_menu", "(", "self", ",", "line", ")", ":", "print", "(", "'\\tchan_log_all\\tSet the channel log level to ALL_COMMS.'", ")", "print", "(", "'\\tchan_log_normal\\tSet the channel log level to NORMAL.'", ")", "print", "(", "'\\tdisable_unsol\\tPerform the function DISABLE_UNSOLICITED.'", ")", "print", "(", "'\\thelp\\t\\tDisplay command-line help.'", ")", "print", "(", "'\\tmast_log_all\\tSet the master log level to ALL_COMMS.'", ")", "print", "(", "'\\tmast_log_normal\\tSet the master log level to NORMAL.'", ")", "print", "(", "'\\tmenu\\t\\tDisplay this menu.'", ")", "print", "(", "'\\to1\\t\\tSend a DirectOperate LATCH_ON command.'", ")", "print", "(", "'\\to2\\t\\tSend a DirectOperate analog value.'", ")", "print", "(", "'\\to3\\t\\tSend a DirectOperate CommandSet.'", ")", "print", "(", "'\\tquit'", ")", "print", "(", "'\\trestart\\t\\tRequest an outstation cold restart.'", ")", "print", "(", "'\\ts1\\t\\tSend a SelectAndOperate LATCH_ON command.'", ")", "print", "(", "'\\ts2\\t\\tSend a SelectAndOperate CommandSet.'", ")", "print", "(", "'\\tscan_all\\tRead data from the outstation (ScanAllObjects).'", ")", "print", "(", "'\\tscan_fast\\tDemand immediate execution of the fast (every 1 mins) Class 1 scan.'", ")", "print", "(", "'\\tscan_range\\tPerform an ad-hoc scan (ScanRange) of GroupVariation 1.2, range 0..3.'", ")", "print", "(", "'\\tscan_slow\\tDemand immediate execution of the slow (every 30 mins) All-Classes scan.'", ")", "print", "(", "'\\twrite_time\\tWrite a TimeAndInterval to the outstation.'", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_chan_log_all
Set the channel log level to ALL_COMMS. Command syntax is: chan_log_all
examples/master_cmd.py
def do_chan_log_all(self, line): """Set the channel log level to ALL_COMMS. Command syntax is: chan_log_all""" self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS)) print('Channel log filtering level is now: {0}'.format(opendnp3.levels.ALL_COMMS))
def do_chan_log_all(self, line): """Set the channel log level to ALL_COMMS. Command syntax is: chan_log_all""" self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS)) print('Channel log filtering level is now: {0}'.format(opendnp3.levels.ALL_COMMS))
[ "Set", "the", "channel", "log", "level", "to", "ALL_COMMS", ".", "Command", "syntax", "is", ":", "chan_log_all" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L63-L66
[ "def", "do_chan_log_all", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "channel", ".", "SetLogFilters", "(", "openpal", ".", "LogFilters", "(", "opendnp3", ".", "levels", ".", "ALL_COMMS", ")", ")", "print", "(", "'Channel log filtering level is now: {0}'", ".", "format", "(", "opendnp3", ".", "levels", ".", "ALL_COMMS", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_chan_log_normal
Set the channel log level to NORMAL. Command syntax is: chan_log_normal
examples/master_cmd.py
def do_chan_log_normal(self, line): """Set the channel log level to NORMAL. Command syntax is: chan_log_normal""" self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL)) print('Channel log filtering level is now: {0}'.format(opendnp3.levels.NORMAL))
def do_chan_log_normal(self, line): """Set the channel log level to NORMAL. Command syntax is: chan_log_normal""" self.application.channel.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL)) print('Channel log filtering level is now: {0}'.format(opendnp3.levels.NORMAL))
[ "Set", "the", "channel", "log", "level", "to", "NORMAL", ".", "Command", "syntax", "is", ":", "chan_log_normal" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L68-L71
[ "def", "do_chan_log_normal", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "channel", ".", "SetLogFilters", "(", "openpal", ".", "LogFilters", "(", "opendnp3", ".", "levels", ".", "NORMAL", ")", ")", "print", "(", "'Channel log filtering level is now: {0}'", ".", "format", "(", "opendnp3", ".", "levels", ".", "NORMAL", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_disable_unsol
Perform the function DISABLE_UNSOLICITED. Command syntax is: disable_unsol
examples/master_cmd.py
def do_disable_unsol(self, line): """Perform the function DISABLE_UNSOLICITED. Command syntax is: disable_unsol""" headers = [opendnp3.Header().AllObjects(60, 2), opendnp3.Header().AllObjects(60, 3), opendnp3.Header().AllObjects(60, 4)] self.application.master.PerformFunction("disable unsolicited", opendnp3.FunctionCode.DISABLE_UNSOLICITED, headers, opendnp3.TaskConfig().Default())
def do_disable_unsol(self, line): """Perform the function DISABLE_UNSOLICITED. Command syntax is: disable_unsol""" headers = [opendnp3.Header().AllObjects(60, 2), opendnp3.Header().AllObjects(60, 3), opendnp3.Header().AllObjects(60, 4)] self.application.master.PerformFunction("disable unsolicited", opendnp3.FunctionCode.DISABLE_UNSOLICITED, headers, opendnp3.TaskConfig().Default())
[ "Perform", "the", "function", "DISABLE_UNSOLICITED", ".", "Command", "syntax", "is", ":", "disable_unsol" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L73-L81
[ "def", "do_disable_unsol", "(", "self", ",", "line", ")", ":", "headers", "=", "[", "opendnp3", ".", "Header", "(", ")", ".", "AllObjects", "(", "60", ",", "2", ")", ",", "opendnp3", ".", "Header", "(", ")", ".", "AllObjects", "(", "60", ",", "3", ")", ",", "opendnp3", ".", "Header", "(", ")", ".", "AllObjects", "(", "60", ",", "4", ")", "]", "self", ".", "application", ".", "master", ".", "PerformFunction", "(", "\"disable unsolicited\"", ",", "opendnp3", ".", "FunctionCode", ".", "DISABLE_UNSOLICITED", ",", "headers", ",", "opendnp3", ".", "TaskConfig", "(", ")", ".", "Default", "(", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_mast_log_all
Set the master log level to ALL_COMMS. Command syntax is: mast_log_all
examples/master_cmd.py
def do_mast_log_all(self, line): """Set the master log level to ALL_COMMS. Command syntax is: mast_log_all""" self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS)) _log.debug('Master log filtering level is now: {0}'.format(opendnp3.levels.ALL_COMMS))
def do_mast_log_all(self, line): """Set the master log level to ALL_COMMS. Command syntax is: mast_log_all""" self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.ALL_COMMS)) _log.debug('Master log filtering level is now: {0}'.format(opendnp3.levels.ALL_COMMS))
[ "Set", "the", "master", "log", "level", "to", "ALL_COMMS", ".", "Command", "syntax", "is", ":", "mast_log_all" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L83-L86
[ "def", "do_mast_log_all", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "master", ".", "SetLogFilters", "(", "openpal", ".", "LogFilters", "(", "opendnp3", ".", "levels", ".", "ALL_COMMS", ")", ")", "_log", ".", "debug", "(", "'Master log filtering level is now: {0}'", ".", "format", "(", "opendnp3", ".", "levels", ".", "ALL_COMMS", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_mast_log_normal
Set the master log level to NORMAL. Command syntax is: mast_log_normal
examples/master_cmd.py
def do_mast_log_normal(self, line): """Set the master log level to NORMAL. Command syntax is: mast_log_normal""" self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL)) _log.debug('Master log filtering level is now: {0}'.format(opendnp3.levels.NORMAL))
def do_mast_log_normal(self, line): """Set the master log level to NORMAL. Command syntax is: mast_log_normal""" self.application.master.SetLogFilters(openpal.LogFilters(opendnp3.levels.NORMAL)) _log.debug('Master log filtering level is now: {0}'.format(opendnp3.levels.NORMAL))
[ "Set", "the", "master", "log", "level", "to", "NORMAL", ".", "Command", "syntax", "is", ":", "mast_log_normal" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L88-L91
[ "def", "do_mast_log_normal", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "master", ".", "SetLogFilters", "(", "openpal", ".", "LogFilters", "(", "opendnp3", ".", "levels", ".", "NORMAL", ")", ")", "_log", ".", "debug", "(", "'Master log filtering level is now: {0}'", ".", "format", "(", "opendnp3", ".", "levels", ".", "NORMAL", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_o1
Send a DirectOperate BinaryOutput (group 12) index 5 LATCH_ON to the Outstation. Command syntax is: o1
examples/master_cmd.py
def do_o1(self, line): """Send a DirectOperate BinaryOutput (group 12) index 5 LATCH_ON to the Outstation. Command syntax is: o1""" self.application.send_direct_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 5, command_callback)
def do_o1(self, line): """Send a DirectOperate BinaryOutput (group 12) index 5 LATCH_ON to the Outstation. Command syntax is: o1""" self.application.send_direct_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 5, command_callback)
[ "Send", "a", "DirectOperate", "BinaryOutput", "(", "group", "12", ")", "index", "5", "LATCH_ON", "to", "the", "Outstation", ".", "Command", "syntax", "is", ":", "o1" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L93-L97
[ "def", "do_o1", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "send_direct_operate_command", "(", "opendnp3", ".", "ControlRelayOutputBlock", "(", "opendnp3", ".", "ControlCode", ".", "LATCH_ON", ")", ",", "5", ",", "command_callback", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_o3
Send a DirectOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: o3
examples/master_cmd.py
def do_o3(self, line): """Send a DirectOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: o3""" self.application.send_direct_operate_command_set(opendnp3.CommandSet( [ opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 0), opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_OFF), 1) ]), command_callback )
def do_o3(self, line): """Send a DirectOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: o3""" self.application.send_direct_operate_command_set(opendnp3.CommandSet( [ opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 0), opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_OFF), 1) ]), command_callback )
[ "Send", "a", "DirectOperate", "BinaryOutput", "(", "group", "12", ")", "CommandSet", "to", "the", "Outstation", ".", "Command", "syntax", "is", ":", "o3" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L105-L113
[ "def", "do_o3", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "send_direct_operate_command_set", "(", "opendnp3", ".", "CommandSet", "(", "[", "opendnp3", ".", "WithIndex", "(", "opendnp3", ".", "ControlRelayOutputBlock", "(", "opendnp3", ".", "ControlCode", ".", "LATCH_ON", ")", ",", "0", ")", ",", "opendnp3", ".", "WithIndex", "(", "opendnp3", ".", "ControlRelayOutputBlock", "(", "opendnp3", ".", "ControlCode", ".", "LATCH_OFF", ")", ",", "1", ")", "]", ")", ",", "command_callback", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_restart
Request that the Outstation perform a cold restart. Command syntax is: restart
examples/master_cmd.py
def do_restart(self, line): """Request that the Outstation perform a cold restart. Command syntax is: restart""" self.application.master.Restart(opendnp3.RestartType.COLD, restart_callback)
def do_restart(self, line): """Request that the Outstation perform a cold restart. Command syntax is: restart""" self.application.master.Restart(opendnp3.RestartType.COLD, restart_callback)
[ "Request", "that", "the", "Outstation", "perform", "a", "cold", "restart", ".", "Command", "syntax", "is", ":", "restart" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L123-L125
[ "def", "do_restart", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "master", ".", "Restart", "(", "opendnp3", ".", "RestartType", ".", "COLD", ",", "restart_callback", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_s1
Send a SelectAndOperate BinaryOutput (group 12) index 8 LATCH_ON to the Outstation. Command syntax is: s1
examples/master_cmd.py
def do_s1(self, line): """Send a SelectAndOperate BinaryOutput (group 12) index 8 LATCH_ON to the Outstation. Command syntax is: s1""" self.application.send_select_and_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 8, command_callback)
def do_s1(self, line): """Send a SelectAndOperate BinaryOutput (group 12) index 8 LATCH_ON to the Outstation. Command syntax is: s1""" self.application.send_select_and_operate_command(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 8, command_callback)
[ "Send", "a", "SelectAndOperate", "BinaryOutput", "(", "group", "12", ")", "index", "8", "LATCH_ON", "to", "the", "Outstation", ".", "Command", "syntax", "is", ":", "s1" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L127-L131
[ "def", "do_s1", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "send_select_and_operate_command", "(", "opendnp3", ".", "ControlRelayOutputBlock", "(", "opendnp3", ".", "ControlCode", ".", "LATCH_ON", ")", ",", "8", ",", "command_callback", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_s2
Send a SelectAndOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: s2
examples/master_cmd.py
def do_s2(self, line): """Send a SelectAndOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: s2""" self.application.send_select_and_operate_command_set(opendnp3.CommandSet( [ opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 0) ]), command_callback )
def do_s2(self, line): """Send a SelectAndOperate BinaryOutput (group 12) CommandSet to the Outstation. Command syntax is: s2""" self.application.send_select_and_operate_command_set(opendnp3.CommandSet( [ opendnp3.WithIndex(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON), 0) ]), command_callback )
[ "Send", "a", "SelectAndOperate", "BinaryOutput", "(", "group", "12", ")", "CommandSet", "to", "the", "Outstation", ".", "Command", "syntax", "is", ":", "s2" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L133-L140
[ "def", "do_s2", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "send_select_and_operate_command_set", "(", "opendnp3", ".", "CommandSet", "(", "[", "opendnp3", ".", "WithIndex", "(", "opendnp3", ".", "ControlRelayOutputBlock", "(", "opendnp3", ".", "ControlCode", ".", "LATCH_ON", ")", ",", "0", ")", "]", ")", ",", "command_callback", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_scan_all
Call ScanAllObjects. Command syntax is: scan_all
examples/master_cmd.py
def do_scan_all(self, line): """Call ScanAllObjects. Command syntax is: scan_all""" self.application.master.ScanAllObjects(opendnp3.GroupVariationID(2, 1), opendnp3.TaskConfig().Default())
def do_scan_all(self, line): """Call ScanAllObjects. Command syntax is: scan_all""" self.application.master.ScanAllObjects(opendnp3.GroupVariationID(2, 1), opendnp3.TaskConfig().Default())
[ "Call", "ScanAllObjects", ".", "Command", "syntax", "is", ":", "scan_all" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L142-L144
[ "def", "do_scan_all", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "master", ".", "ScanAllObjects", "(", "opendnp3", ".", "GroupVariationID", "(", "2", ",", "1", ")", ",", "opendnp3", ".", "TaskConfig", "(", ")", ".", "Default", "(", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_scan_range
Do an ad-hoc scan of a range of points (group 1, variation 2, indexes 0-3). Command syntax is: scan_range
examples/master_cmd.py
def do_scan_range(self, line): """Do an ad-hoc scan of a range of points (group 1, variation 2, indexes 0-3). Command syntax is: scan_range""" self.application.master.ScanRange(opendnp3.GroupVariationID(1, 2), 0, 3, opendnp3.TaskConfig().Default())
def do_scan_range(self, line): """Do an ad-hoc scan of a range of points (group 1, variation 2, indexes 0-3). Command syntax is: scan_range""" self.application.master.ScanRange(opendnp3.GroupVariationID(1, 2), 0, 3, opendnp3.TaskConfig().Default())
[ "Do", "an", "ad", "-", "hoc", "scan", "of", "a", "range", "of", "points", "(", "group", "1", "variation", "2", "indexes", "0", "-", "3", ")", ".", "Command", "syntax", "is", ":", "scan_range" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L150-L152
[ "def", "do_scan_range", "(", "self", ",", "line", ")", ":", "self", ".", "application", ".", "master", ".", "ScanRange", "(", "opendnp3", ".", "GroupVariationID", "(", "1", ",", "2", ")", ",", "0", ",", "3", ",", "opendnp3", ".", "TaskConfig", "(", ")", ".", "Default", "(", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
MasterCmd.do_write_time
Write a TimeAndInterval to the Outstation. Command syntax is: write_time
examples/master_cmd.py
def do_write_time(self, line): """Write a TimeAndInterval to the Outstation. Command syntax is: write_time""" millis_since_epoch = int((datetime.now() - datetime.utcfromtimestamp(0)).total_seconds() * 1000.0) self.application.master.Write(opendnp3.TimeAndInterval(opendnp3.DNPTime(millis_since_epoch), 100, opendnp3.IntervalUnits.Seconds), 0, # index opendnp3.TaskConfig().Default())
def do_write_time(self, line): """Write a TimeAndInterval to the Outstation. Command syntax is: write_time""" millis_since_epoch = int((datetime.now() - datetime.utcfromtimestamp(0)).total_seconds() * 1000.0) self.application.master.Write(opendnp3.TimeAndInterval(opendnp3.DNPTime(millis_since_epoch), 100, opendnp3.IntervalUnits.Seconds), 0, # index opendnp3.TaskConfig().Default())
[ "Write", "a", "TimeAndInterval", "to", "the", "Outstation", ".", "Command", "syntax", "is", ":", "write_time" ]
ChargePoint/pydnp3
python
https://github.com/ChargePoint/pydnp3/blob/5bcd8240d1fc0aa1579e71f2efcab63b4c61c547/examples/master_cmd.py#L158-L165
[ "def", "do_write_time", "(", "self", ",", "line", ")", ":", "millis_since_epoch", "=", "int", "(", "(", "datetime", ".", "now", "(", ")", "-", "datetime", ".", "utcfromtimestamp", "(", "0", ")", ")", ".", "total_seconds", "(", ")", "*", "1000.0", ")", "self", ".", "application", ".", "master", ".", "Write", "(", "opendnp3", ".", "TimeAndInterval", "(", "opendnp3", ".", "DNPTime", "(", "millis_since_epoch", ")", ",", "100", ",", "opendnp3", ".", "IntervalUnits", ".", "Seconds", ")", ",", "0", ",", "# index", "opendnp3", ".", "TaskConfig", "(", ")", ".", "Default", "(", ")", ")" ]
5bcd8240d1fc0aa1579e71f2efcab63b4c61c547
valid
with_bloomberg
Wrapper function for Bloomberg connection Args: func: function to wrap
xbbg/core/conn.py
def with_bloomberg(func): """ Wrapper function for Bloomberg connection Args: func: function to wrap """ @wraps(func) def wrapper(*args, **kwargs): scope = utils.func_scope(func=func) param = inspect.signature(func).parameters port = kwargs.pop('port', _PORT_) timeout = kwargs.pop('timeout', _TIMEOUT_) restart = kwargs.pop('restart', False) all_kw = { k: args[n] if n < len(args) else v.default for n, (k, v) in enumerate(param.items()) if k != 'kwargs' } all_kw.update(kwargs) log_level = kwargs.get('log', logs.LOG_LEVEL) for to_list in ['tickers', 'flds']: conv = all_kw.get(to_list, None) if hasattr(conv, 'tolist'): all_kw[to_list] = getattr(conv, 'tolist')() if isinstance(conv, str): all_kw[to_list] = [conv] cached_data = [] if scope in ['xbbg.blp.bdp', 'xbbg.blp.bds']: to_qry = cached.bdp_bds_cache(func=func.__name__, **all_kw) cached_data += to_qry.cached_data if not (to_qry.tickers and to_qry.flds): if not cached_data: return pd.DataFrame() res = pd.concat(cached_data, sort=False).reset_index(drop=True) if not all_kw.get('raw', False): res = assist.format_output( data=res, source=func.__name__, col_maps=all_kw.get('col_maps', dict()) ) return res all_kw['tickers'] = to_qry.tickers all_kw['flds'] = to_qry.flds if scope in ['xbbg.blp.bdib']: data_file = storage.hist_file( ticker=all_kw['ticker'], dt=all_kw['dt'], typ=all_kw['typ'], ) if files.exists(data_file): logger = logs.get_logger(func, level=log_level) if all_kw.get('batch', False): return logger.debug(f'reading from {data_file} ...') return assist.format_intraday(data=pd.read_parquet(data_file), **all_kw) _, new = create_connection(port=port, timeout=timeout, restart=restart) res = func(**{ k: v for k, v in all_kw.items() if k not in ['raw', 'col_maps'] }) if new: delete_connection() if scope.startswith('xbbg.blp.') and isinstance(res, list): final = cached_data + res if not final: return pd.DataFrame() res = pd.DataFrame(pd.concat(final, sort=False)) if (scope in ['xbbg.blp.bdp', 'xbbg.blp.bds']) \ and (not all_kw.get('raw', False)): res = assist.format_output( data=res.reset_index(drop=True), source=func.__name__, col_maps=all_kw.get('col_maps', dict()), ) return res return wrapper
def with_bloomberg(func): """ Wrapper function for Bloomberg connection Args: func: function to wrap """ @wraps(func) def wrapper(*args, **kwargs): scope = utils.func_scope(func=func) param = inspect.signature(func).parameters port = kwargs.pop('port', _PORT_) timeout = kwargs.pop('timeout', _TIMEOUT_) restart = kwargs.pop('restart', False) all_kw = { k: args[n] if n < len(args) else v.default for n, (k, v) in enumerate(param.items()) if k != 'kwargs' } all_kw.update(kwargs) log_level = kwargs.get('log', logs.LOG_LEVEL) for to_list in ['tickers', 'flds']: conv = all_kw.get(to_list, None) if hasattr(conv, 'tolist'): all_kw[to_list] = getattr(conv, 'tolist')() if isinstance(conv, str): all_kw[to_list] = [conv] cached_data = [] if scope in ['xbbg.blp.bdp', 'xbbg.blp.bds']: to_qry = cached.bdp_bds_cache(func=func.__name__, **all_kw) cached_data += to_qry.cached_data if not (to_qry.tickers and to_qry.flds): if not cached_data: return pd.DataFrame() res = pd.concat(cached_data, sort=False).reset_index(drop=True) if not all_kw.get('raw', False): res = assist.format_output( data=res, source=func.__name__, col_maps=all_kw.get('col_maps', dict()) ) return res all_kw['tickers'] = to_qry.tickers all_kw['flds'] = to_qry.flds if scope in ['xbbg.blp.bdib']: data_file = storage.hist_file( ticker=all_kw['ticker'], dt=all_kw['dt'], typ=all_kw['typ'], ) if files.exists(data_file): logger = logs.get_logger(func, level=log_level) if all_kw.get('batch', False): return logger.debug(f'reading from {data_file} ...') return assist.format_intraday(data=pd.read_parquet(data_file), **all_kw) _, new = create_connection(port=port, timeout=timeout, restart=restart) res = func(**{ k: v for k, v in all_kw.items() if k not in ['raw', 'col_maps'] }) if new: delete_connection() if scope.startswith('xbbg.blp.') and isinstance(res, list): final = cached_data + res if not final: return pd.DataFrame() res = pd.DataFrame(pd.concat(final, sort=False)) if (scope in ['xbbg.blp.bdp', 'xbbg.blp.bds']) \ and (not all_kw.get('raw', False)): res = assist.format_output( data=res.reset_index(drop=True), source=func.__name__, col_maps=all_kw.get('col_maps', dict()), ) return res return wrapper
[ "Wrapper", "function", "for", "Bloomberg", "connection" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/conn.py#L26-L102
[ "def", "with_bloomberg", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "scope", "=", "utils", ".", "func_scope", "(", "func", "=", "func", ")", "param", "=", "inspect", ".", "signature", "(", "func", ")", ".", "parameters", "port", "=", "kwargs", ".", "pop", "(", "'port'", ",", "_PORT_", ")", "timeout", "=", "kwargs", ".", "pop", "(", "'timeout'", ",", "_TIMEOUT_", ")", "restart", "=", "kwargs", ".", "pop", "(", "'restart'", ",", "False", ")", "all_kw", "=", "{", "k", ":", "args", "[", "n", "]", "if", "n", "<", "len", "(", "args", ")", "else", "v", ".", "default", "for", "n", ",", "(", "k", ",", "v", ")", "in", "enumerate", "(", "param", ".", "items", "(", ")", ")", "if", "k", "!=", "'kwargs'", "}", "all_kw", ".", "update", "(", "kwargs", ")", "log_level", "=", "kwargs", ".", "get", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", "for", "to_list", "in", "[", "'tickers'", ",", "'flds'", "]", ":", "conv", "=", "all_kw", ".", "get", "(", "to_list", ",", "None", ")", "if", "hasattr", "(", "conv", ",", "'tolist'", ")", ":", "all_kw", "[", "to_list", "]", "=", "getattr", "(", "conv", ",", "'tolist'", ")", "(", ")", "if", "isinstance", "(", "conv", ",", "str", ")", ":", "all_kw", "[", "to_list", "]", "=", "[", "conv", "]", "cached_data", "=", "[", "]", "if", "scope", "in", "[", "'xbbg.blp.bdp'", ",", "'xbbg.blp.bds'", "]", ":", "to_qry", "=", "cached", ".", "bdp_bds_cache", "(", "func", "=", "func", ".", "__name__", ",", "*", "*", "all_kw", ")", "cached_data", "+=", "to_qry", ".", "cached_data", "if", "not", "(", "to_qry", ".", "tickers", "and", "to_qry", ".", "flds", ")", ":", "if", "not", "cached_data", ":", "return", "pd", ".", "DataFrame", "(", ")", "res", "=", "pd", ".", "concat", "(", "cached_data", ",", "sort", "=", "False", ")", ".", "reset_index", "(", "drop", "=", "True", ")", "if", "not", "all_kw", ".", "get", "(", "'raw'", ",", "False", ")", ":", "res", "=", "assist", ".", "format_output", "(", "data", "=", "res", ",", "source", "=", "func", ".", "__name__", ",", "col_maps", "=", "all_kw", ".", "get", "(", "'col_maps'", ",", "dict", "(", ")", ")", ")", "return", "res", "all_kw", "[", "'tickers'", "]", "=", "to_qry", ".", "tickers", "all_kw", "[", "'flds'", "]", "=", "to_qry", ".", "flds", "if", "scope", "in", "[", "'xbbg.blp.bdib'", "]", ":", "data_file", "=", "storage", ".", "hist_file", "(", "ticker", "=", "all_kw", "[", "'ticker'", "]", ",", "dt", "=", "all_kw", "[", "'dt'", "]", ",", "typ", "=", "all_kw", "[", "'typ'", "]", ",", ")", "if", "files", ".", "exists", "(", "data_file", ")", ":", "logger", "=", "logs", ".", "get_logger", "(", "func", ",", "level", "=", "log_level", ")", "if", "all_kw", ".", "get", "(", "'batch'", ",", "False", ")", ":", "return", "logger", ".", "debug", "(", "f'reading from {data_file} ...'", ")", "return", "assist", ".", "format_intraday", "(", "data", "=", "pd", ".", "read_parquet", "(", "data_file", ")", ",", "*", "*", "all_kw", ")", "_", ",", "new", "=", "create_connection", "(", "port", "=", "port", ",", "timeout", "=", "timeout", ",", "restart", "=", "restart", ")", "res", "=", "func", "(", "*", "*", "{", "k", ":", "v", "for", "k", ",", "v", "in", "all_kw", ".", "items", "(", ")", "if", "k", "not", "in", "[", "'raw'", ",", "'col_maps'", "]", "}", ")", "if", "new", ":", "delete_connection", "(", ")", "if", "scope", ".", "startswith", "(", "'xbbg.blp.'", ")", "and", "isinstance", "(", "res", ",", "list", ")", ":", "final", "=", "cached_data", "+", "res", "if", "not", "final", ":", "return", "pd", ".", "DataFrame", "(", ")", "res", "=", "pd", ".", "DataFrame", "(", "pd", ".", "concat", "(", "final", ",", "sort", "=", "False", ")", ")", "if", "(", "scope", "in", "[", "'xbbg.blp.bdp'", ",", "'xbbg.blp.bds'", "]", ")", "and", "(", "not", "all_kw", ".", "get", "(", "'raw'", ",", "False", ")", ")", ":", "res", "=", "assist", ".", "format_output", "(", "data", "=", "res", ".", "reset_index", "(", "drop", "=", "True", ")", ",", "source", "=", "func", ".", "__name__", ",", "col_maps", "=", "all_kw", ".", "get", "(", "'col_maps'", ",", "dict", "(", ")", ")", ",", ")", "return", "res", "return", "wrapper" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
create_connection
Create Bloomberg connection Returns: (Bloomberg connection, if connection is new)
xbbg/core/conn.py
def create_connection(port=_PORT_, timeout=_TIMEOUT_, restart=False): """ Create Bloomberg connection Returns: (Bloomberg connection, if connection is new) """ if _CON_SYM_ in globals(): if not isinstance(globals()[_CON_SYM_], pdblp.BCon): del globals()[_CON_SYM_] if (_CON_SYM_ in globals()) and (not restart): con = globals()[_CON_SYM_] if getattr(con, '_session').start(): con.start() return con, False else: con = pdblp.BCon(port=port, timeout=timeout) globals()[_CON_SYM_] = con con.start() return con, True
def create_connection(port=_PORT_, timeout=_TIMEOUT_, restart=False): """ Create Bloomberg connection Returns: (Bloomberg connection, if connection is new) """ if _CON_SYM_ in globals(): if not isinstance(globals()[_CON_SYM_], pdblp.BCon): del globals()[_CON_SYM_] if (_CON_SYM_ in globals()) and (not restart): con = globals()[_CON_SYM_] if getattr(con, '_session').start(): con.start() return con, False else: con = pdblp.BCon(port=port, timeout=timeout) globals()[_CON_SYM_] = con con.start() return con, True
[ "Create", "Bloomberg", "connection" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/conn.py#L105-L125
[ "def", "create_connection", "(", "port", "=", "_PORT_", ",", "timeout", "=", "_TIMEOUT_", ",", "restart", "=", "False", ")", ":", "if", "_CON_SYM_", "in", "globals", "(", ")", ":", "if", "not", "isinstance", "(", "globals", "(", ")", "[", "_CON_SYM_", "]", ",", "pdblp", ".", "BCon", ")", ":", "del", "globals", "(", ")", "[", "_CON_SYM_", "]", "if", "(", "_CON_SYM_", "in", "globals", "(", ")", ")", "and", "(", "not", "restart", ")", ":", "con", "=", "globals", "(", ")", "[", "_CON_SYM_", "]", "if", "getattr", "(", "con", ",", "'_session'", ")", ".", "start", "(", ")", ":", "con", ".", "start", "(", ")", "return", "con", ",", "False", "else", ":", "con", "=", "pdblp", ".", "BCon", "(", "port", "=", "port", ",", "timeout", "=", "timeout", ")", "globals", "(", ")", "[", "_CON_SYM_", "]", "=", "con", "con", ".", "start", "(", ")", "return", "con", ",", "True" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
delete_connection
Stop and destroy Bloomberg connection
xbbg/core/conn.py
def delete_connection(): """ Stop and destroy Bloomberg connection """ if _CON_SYM_ in globals(): con = globals().pop(_CON_SYM_) if not getattr(con, '_session').start(): con.stop()
def delete_connection(): """ Stop and destroy Bloomberg connection """ if _CON_SYM_ in globals(): con = globals().pop(_CON_SYM_) if not getattr(con, '_session').start(): con.stop()
[ "Stop", "and", "destroy", "Bloomberg", "connection" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/conn.py#L128-L134
[ "def", "delete_connection", "(", ")", ":", "if", "_CON_SYM_", "in", "globals", "(", ")", ":", "con", "=", "globals", "(", ")", ".", "pop", "(", "_CON_SYM_", ")", "if", "not", "getattr", "(", "con", ",", "'_session'", ")", ".", "start", "(", ")", ":", "con", ".", "stop", "(", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
bdp_bds_cache
Find cached `BDP` / `BDS` queries Args: func: function name - bdp or bds tickers: tickers flds: fields **kwargs: other kwargs Returns: ToQuery(ticker, flds, kwargs)
xbbg/io/cached.py
def bdp_bds_cache(func, tickers, flds, **kwargs) -> ToQuery: """ Find cached `BDP` / `BDS` queries Args: func: function name - bdp or bds tickers: tickers flds: fields **kwargs: other kwargs Returns: ToQuery(ticker, flds, kwargs) """ cache_data = [] log_level = kwargs.get('log', logs.LOG_LEVEL) logger = logs.get_logger(bdp_bds_cache, level=log_level) kwargs['has_date'] = kwargs.pop('has_date', func == 'bds') kwargs['cache'] = kwargs.get('cache', True) tickers = utils.flatten(tickers) flds = utils.flatten(flds) loaded = pd.DataFrame(data=0, index=tickers, columns=flds) for ticker, fld in product(tickers, flds): data_file = storage.ref_file( ticker=ticker, fld=fld, ext='pkl', **{ k: v for k, v in kwargs.items() if k not in EXC_COLS } ) if not files.exists(data_file): continue logger.debug(f'reading from {data_file} ...') cache_data.append(pd.read_pickle(data_file)) loaded.loc[ticker, fld] = 1 to_qry = loaded.where(loaded == 0)\ .dropna(how='all', axis=1).dropna(how='all', axis=0) return ToQuery( tickers=to_qry.index.tolist(), flds=to_qry.columns.tolist(), cached_data=cache_data )
def bdp_bds_cache(func, tickers, flds, **kwargs) -> ToQuery: """ Find cached `BDP` / `BDS` queries Args: func: function name - bdp or bds tickers: tickers flds: fields **kwargs: other kwargs Returns: ToQuery(ticker, flds, kwargs) """ cache_data = [] log_level = kwargs.get('log', logs.LOG_LEVEL) logger = logs.get_logger(bdp_bds_cache, level=log_level) kwargs['has_date'] = kwargs.pop('has_date', func == 'bds') kwargs['cache'] = kwargs.get('cache', True) tickers = utils.flatten(tickers) flds = utils.flatten(flds) loaded = pd.DataFrame(data=0, index=tickers, columns=flds) for ticker, fld in product(tickers, flds): data_file = storage.ref_file( ticker=ticker, fld=fld, ext='pkl', **{ k: v for k, v in kwargs.items() if k not in EXC_COLS } ) if not files.exists(data_file): continue logger.debug(f'reading from {data_file} ...') cache_data.append(pd.read_pickle(data_file)) loaded.loc[ticker, fld] = 1 to_qry = loaded.where(loaded == 0)\ .dropna(how='all', axis=1).dropna(how='all', axis=0) return ToQuery( tickers=to_qry.index.tolist(), flds=to_qry.columns.tolist(), cached_data=cache_data )
[ "Find", "cached", "BDP", "/", "BDS", "queries" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/cached.py#L13-L53
[ "def", "bdp_bds_cache", "(", "func", ",", "tickers", ",", "flds", ",", "*", "*", "kwargs", ")", "->", "ToQuery", ":", "cache_data", "=", "[", "]", "log_level", "=", "kwargs", ".", "get", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", "logger", "=", "logs", ".", "get_logger", "(", "bdp_bds_cache", ",", "level", "=", "log_level", ")", "kwargs", "[", "'has_date'", "]", "=", "kwargs", ".", "pop", "(", "'has_date'", ",", "func", "==", "'bds'", ")", "kwargs", "[", "'cache'", "]", "=", "kwargs", ".", "get", "(", "'cache'", ",", "True", ")", "tickers", "=", "utils", ".", "flatten", "(", "tickers", ")", "flds", "=", "utils", ".", "flatten", "(", "flds", ")", "loaded", "=", "pd", ".", "DataFrame", "(", "data", "=", "0", ",", "index", "=", "tickers", ",", "columns", "=", "flds", ")", "for", "ticker", ",", "fld", "in", "product", "(", "tickers", ",", "flds", ")", ":", "data_file", "=", "storage", ".", "ref_file", "(", "ticker", "=", "ticker", ",", "fld", "=", "fld", ",", "ext", "=", "'pkl'", ",", "*", "*", "{", "k", ":", "v", "for", "k", ",", "v", "in", "kwargs", ".", "items", "(", ")", "if", "k", "not", "in", "EXC_COLS", "}", ")", "if", "not", "files", ".", "exists", "(", "data_file", ")", ":", "continue", "logger", ".", "debug", "(", "f'reading from {data_file} ...'", ")", "cache_data", ".", "append", "(", "pd", ".", "read_pickle", "(", "data_file", ")", ")", "loaded", ".", "loc", "[", "ticker", ",", "fld", "]", "=", "1", "to_qry", "=", "loaded", ".", "where", "(", "loaded", "==", "0", ")", ".", "dropna", "(", "how", "=", "'all'", ",", "axis", "=", "1", ")", ".", "dropna", "(", "how", "=", "'all'", ",", "axis", "=", "0", ")", "return", "ToQuery", "(", "tickers", "=", "to_qry", ".", "index", ".", "tolist", "(", ")", ",", "flds", "=", "to_qry", ".", "columns", ".", "tolist", "(", ")", ",", "cached_data", "=", "cache_data", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
parse_version
Parse versions
setup.py
def parse_version(package): """ Parse versions """ init_file = f'{PACKAGE_ROOT}/{package}/__init__.py' with open(init_file, 'r', encoding='utf-8') as f: for line in f.readlines(): if '__version__' in line: return line.split('=')[1].strip()[1:-1] return ''
def parse_version(package): """ Parse versions """ init_file = f'{PACKAGE_ROOT}/{package}/__init__.py' with open(init_file, 'r', encoding='utf-8') as f: for line in f.readlines(): if '__version__' in line: return line.split('=')[1].strip()[1:-1] return ''
[ "Parse", "versions" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/setup.py#L15-L24
[ "def", "parse_version", "(", "package", ")", ":", "init_file", "=", "f'{PACKAGE_ROOT}/{package}/__init__.py'", "with", "open", "(", "init_file", ",", "'r'", ",", "encoding", "=", "'utf-8'", ")", "as", "f", ":", "for", "line", "in", "f", ".", "readlines", "(", ")", ":", "if", "'__version__'", "in", "line", ":", "return", "line", ".", "split", "(", "'='", ")", "[", "1", "]", ".", "strip", "(", ")", "[", "1", ":", "-", "1", "]", "return", "''" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
parse_markdown
Parse markdown as description
setup.py
def parse_markdown(): """ Parse markdown as description """ readme_file = f'{PACKAGE_ROOT}/README.md' if path.exists(readme_file): with open(readme_file, 'r', encoding='utf-8') as f: long_description = f.read() return long_description
def parse_markdown(): """ Parse markdown as description """ readme_file = f'{PACKAGE_ROOT}/README.md' if path.exists(readme_file): with open(readme_file, 'r', encoding='utf-8') as f: long_description = f.read() return long_description
[ "Parse", "markdown", "as", "description" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/setup.py#L27-L35
[ "def", "parse_markdown", "(", ")", ":", "readme_file", "=", "f'{PACKAGE_ROOT}/README.md'", "if", "path", ".", "exists", "(", "readme_file", ")", ":", "with", "open", "(", "readme_file", ",", "'r'", ",", "encoding", "=", "'utf-8'", ")", "as", "f", ":", "long_description", "=", "f", ".", "read", "(", ")", "return", "long_description" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
parse_description
Parse the description in the README file
setup.py
def parse_description(markdown=True): """ Parse the description in the README file """ if markdown: return parse_markdown() try: from pypandoc import convert readme_file = f'{PACKAGE_ROOT}/docs/index.rst' if not path.exists(readme_file): raise ImportError return convert(readme_file, 'rst') except ImportError: return parse_markdown()
def parse_description(markdown=True): """ Parse the description in the README file """ if markdown: return parse_markdown() try: from pypandoc import convert readme_file = f'{PACKAGE_ROOT}/docs/index.rst' if not path.exists(readme_file): raise ImportError return convert(readme_file, 'rst') except ImportError: return parse_markdown()
[ "Parse", "the", "description", "in", "the", "README", "file" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/setup.py#L38-L53
[ "def", "parse_description", "(", "markdown", "=", "True", ")", ":", "if", "markdown", ":", "return", "parse_markdown", "(", ")", "try", ":", "from", "pypandoc", "import", "convert", "readme_file", "=", "f'{PACKAGE_ROOT}/docs/index.rst'", "if", "not", "path", ".", "exists", "(", "readme_file", ")", ":", "raise", "ImportError", "return", "convert", "(", "readme_file", ",", "'rst'", ")", "except", "ImportError", ":", "return", "parse_markdown", "(", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
proc_ovrds
Bloomberg overrides Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_ovrds(DVD_Start_Dt='20180101') [('DVD_Start_Dt', '20180101')] >>> proc_ovrds(DVD_Start_Dt='20180101', cache=True, has_date=True) [('DVD_Start_Dt', '20180101')]
xbbg/core/assist.py
def proc_ovrds(**kwargs): """ Bloomberg overrides Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_ovrds(DVD_Start_Dt='20180101') [('DVD_Start_Dt', '20180101')] >>> proc_ovrds(DVD_Start_Dt='20180101', cache=True, has_date=True) [('DVD_Start_Dt', '20180101')] """ return [ (k, v) for k, v in kwargs.items() if k not in list(ELEM_KEYS.keys()) + list(ELEM_KEYS.values()) + PRSV_COLS ]
def proc_ovrds(**kwargs): """ Bloomberg overrides Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_ovrds(DVD_Start_Dt='20180101') [('DVD_Start_Dt', '20180101')] >>> proc_ovrds(DVD_Start_Dt='20180101', cache=True, has_date=True) [('DVD_Start_Dt', '20180101')] """ return [ (k, v) for k, v in kwargs.items() if k not in list(ELEM_KEYS.keys()) + list(ELEM_KEYS.values()) + PRSV_COLS ]
[ "Bloomberg", "overrides" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L59-L78
[ "def", "proc_ovrds", "(", "*", "*", "kwargs", ")", ":", "return", "[", "(", "k", ",", "v", ")", "for", "k", ",", "v", "in", "kwargs", ".", "items", "(", ")", "if", "k", "not", "in", "list", "(", "ELEM_KEYS", ".", "keys", "(", ")", ")", "+", "list", "(", "ELEM_KEYS", ".", "values", "(", ")", ")", "+", "PRSV_COLS", "]" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
proc_elms
Bloomberg overrides for elements Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_elms(PerAdj='A', Per='W') [('periodicityAdjustment', 'ACTUAL'), ('periodicitySelection', 'WEEKLY')] >>> proc_elms(Days='A', Fill='B') [('nonTradingDayFillOption', 'ALL_CALENDAR_DAYS'), ('nonTradingDayFillMethod', 'NIL_VALUE')] >>> proc_elms(CshAdjNormal=False, CshAdjAbnormal=True) [('adjustmentNormal', False), ('adjustmentAbnormal', True)] >>> proc_elms(Per='W', Quote='Average', start_date='2018-01-10') [('periodicitySelection', 'WEEKLY'), ('overrideOption', 'OVERRIDE_OPTION_GPA')] >>> proc_elms(QuoteType='Y') [('pricingOption', 'PRICING_OPTION_YIELD')] >>> proc_elms(QuoteType='Y', cache=True) [('pricingOption', 'PRICING_OPTION_YIELD')]
xbbg/core/assist.py
def proc_elms(**kwargs) -> list: """ Bloomberg overrides for elements Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_elms(PerAdj='A', Per='W') [('periodicityAdjustment', 'ACTUAL'), ('periodicitySelection', 'WEEKLY')] >>> proc_elms(Days='A', Fill='B') [('nonTradingDayFillOption', 'ALL_CALENDAR_DAYS'), ('nonTradingDayFillMethod', 'NIL_VALUE')] >>> proc_elms(CshAdjNormal=False, CshAdjAbnormal=True) [('adjustmentNormal', False), ('adjustmentAbnormal', True)] >>> proc_elms(Per='W', Quote='Average', start_date='2018-01-10') [('periodicitySelection', 'WEEKLY'), ('overrideOption', 'OVERRIDE_OPTION_GPA')] >>> proc_elms(QuoteType='Y') [('pricingOption', 'PRICING_OPTION_YIELD')] >>> proc_elms(QuoteType='Y', cache=True) [('pricingOption', 'PRICING_OPTION_YIELD')] """ return [ (ELEM_KEYS.get(k, k), ELEM_VALS.get(ELEM_KEYS.get(k, k), dict()).get(v, v)) for k, v in kwargs.items() if (k in list(ELEM_KEYS.keys()) + list(ELEM_KEYS.values())) and (k not in PRSV_COLS) ]
def proc_elms(**kwargs) -> list: """ Bloomberg overrides for elements Args: **kwargs: overrides Returns: list of tuples Examples: >>> proc_elms(PerAdj='A', Per='W') [('periodicityAdjustment', 'ACTUAL'), ('periodicitySelection', 'WEEKLY')] >>> proc_elms(Days='A', Fill='B') [('nonTradingDayFillOption', 'ALL_CALENDAR_DAYS'), ('nonTradingDayFillMethod', 'NIL_VALUE')] >>> proc_elms(CshAdjNormal=False, CshAdjAbnormal=True) [('adjustmentNormal', False), ('adjustmentAbnormal', True)] >>> proc_elms(Per='W', Quote='Average', start_date='2018-01-10') [('periodicitySelection', 'WEEKLY'), ('overrideOption', 'OVERRIDE_OPTION_GPA')] >>> proc_elms(QuoteType='Y') [('pricingOption', 'PRICING_OPTION_YIELD')] >>> proc_elms(QuoteType='Y', cache=True) [('pricingOption', 'PRICING_OPTION_YIELD')] """ return [ (ELEM_KEYS.get(k, k), ELEM_VALS.get(ELEM_KEYS.get(k, k), dict()).get(v, v)) for k, v in kwargs.items() if (k in list(ELEM_KEYS.keys()) + list(ELEM_KEYS.values())) and (k not in PRSV_COLS) ]
[ "Bloomberg", "overrides", "for", "elements" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L81-L110
[ "def", "proc_elms", "(", "*", "*", "kwargs", ")", "->", "list", ":", "return", "[", "(", "ELEM_KEYS", ".", "get", "(", "k", ",", "k", ")", ",", "ELEM_VALS", ".", "get", "(", "ELEM_KEYS", ".", "get", "(", "k", ",", "k", ")", ",", "dict", "(", ")", ")", ".", "get", "(", "v", ",", "v", ")", ")", "for", "k", ",", "v", "in", "kwargs", ".", "items", "(", ")", "if", "(", "k", "in", "list", "(", "ELEM_KEYS", ".", "keys", "(", ")", ")", "+", "list", "(", "ELEM_KEYS", ".", "values", "(", ")", ")", ")", "and", "(", "k", "not", "in", "PRSV_COLS", ")", "]" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
format_earning
Standardized earning outputs and add percentage by each blocks Args: data: earning data block header: earning headers Returns: pd.DataFrame Examples: >>> format_earning( ... data=pd.read_pickle('xbbg/tests/data/sample_earning.pkl'), ... header=pd.read_pickle('xbbg/tests/data/sample_earning_header.pkl') ... ).round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04
xbbg/core/assist.py
def format_earning(data: pd.DataFrame, header: pd.DataFrame) -> pd.DataFrame: """ Standardized earning outputs and add percentage by each blocks Args: data: earning data block header: earning headers Returns: pd.DataFrame Examples: >>> format_earning( ... data=pd.read_pickle('xbbg/tests/data/sample_earning.pkl'), ... header=pd.read_pickle('xbbg/tests/data/sample_earning_header.pkl') ... ).round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04 """ if data.dropna(subset=['value']).empty: return pd.DataFrame() res = pd.concat([ grp.loc[:, ['value']].set_index(header.value) for _, grp in data.groupby(data.position) ], axis=1) res.index.name = None res.columns = res.iloc[0] res = res.iloc[1:].transpose().reset_index().apply( pd.to_numeric, downcast='float', errors='ignore' ) res.rename( columns=lambda vv: '_'.join(vv.lower().split()).replace('fy_', 'fy'), inplace=True, ) years = res.columns[res.columns.str.startswith('fy')] lvl_1 = res.level == 1 for yr in years: res.loc[:, yr] = res.loc[:, yr].round(1) pct = f'{yr}_pct' res.loc[:, pct] = 0. res.loc[lvl_1, pct] = res.loc[lvl_1, pct].astype(float).round(1) res.loc[lvl_1, pct] = res.loc[lvl_1, yr] / res.loc[lvl_1, yr].sum() * 100 sub_pct = [] for _, snap in res[::-1].iterrows(): if snap.level > 2: continue if snap.level == 1: if len(sub_pct) == 0: continue sub = pd.concat(sub_pct, axis=1).transpose() res.loc[sub.index, pct] = \ res.loc[sub.index, yr] / res.loc[sub.index, yr].sum() * 100 sub_pct = [] if snap.level == 2: sub_pct.append(snap) res.set_index('segment_name', inplace=True) res.index.name = None return res
def format_earning(data: pd.DataFrame, header: pd.DataFrame) -> pd.DataFrame: """ Standardized earning outputs and add percentage by each blocks Args: data: earning data block header: earning headers Returns: pd.DataFrame Examples: >>> format_earning( ... data=pd.read_pickle('xbbg/tests/data/sample_earning.pkl'), ... header=pd.read_pickle('xbbg/tests/data/sample_earning_header.pkl') ... ).round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04 """ if data.dropna(subset=['value']).empty: return pd.DataFrame() res = pd.concat([ grp.loc[:, ['value']].set_index(header.value) for _, grp in data.groupby(data.position) ], axis=1) res.index.name = None res.columns = res.iloc[0] res = res.iloc[1:].transpose().reset_index().apply( pd.to_numeric, downcast='float', errors='ignore' ) res.rename( columns=lambda vv: '_'.join(vv.lower().split()).replace('fy_', 'fy'), inplace=True, ) years = res.columns[res.columns.str.startswith('fy')] lvl_1 = res.level == 1 for yr in years: res.loc[:, yr] = res.loc[:, yr].round(1) pct = f'{yr}_pct' res.loc[:, pct] = 0. res.loc[lvl_1, pct] = res.loc[lvl_1, pct].astype(float).round(1) res.loc[lvl_1, pct] = res.loc[lvl_1, yr] / res.loc[lvl_1, yr].sum() * 100 sub_pct = [] for _, snap in res[::-1].iterrows(): if snap.level > 2: continue if snap.level == 1: if len(sub_pct) == 0: continue sub = pd.concat(sub_pct, axis=1).transpose() res.loc[sub.index, pct] = \ res.loc[sub.index, yr] / res.loc[sub.index, yr].sum() * 100 sub_pct = [] if snap.level == 2: sub_pct.append(snap) res.set_index('segment_name', inplace=True) res.index.name = None return res
[ "Standardized", "earning", "outputs", "and", "add", "percentage", "by", "each", "blocks" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L113-L175
[ "def", "format_earning", "(", "data", ":", "pd", ".", "DataFrame", ",", "header", ":", "pd", ".", "DataFrame", ")", "->", "pd", ".", "DataFrame", ":", "if", "data", ".", "dropna", "(", "subset", "=", "[", "'value'", "]", ")", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "res", "=", "pd", ".", "concat", "(", "[", "grp", ".", "loc", "[", ":", ",", "[", "'value'", "]", "]", ".", "set_index", "(", "header", ".", "value", ")", "for", "_", ",", "grp", "in", "data", ".", "groupby", "(", "data", ".", "position", ")", "]", ",", "axis", "=", "1", ")", "res", ".", "index", ".", "name", "=", "None", "res", ".", "columns", "=", "res", ".", "iloc", "[", "0", "]", "res", "=", "res", ".", "iloc", "[", "1", ":", "]", ".", "transpose", "(", ")", ".", "reset_index", "(", ")", ".", "apply", "(", "pd", ".", "to_numeric", ",", "downcast", "=", "'float'", ",", "errors", "=", "'ignore'", ")", "res", ".", "rename", "(", "columns", "=", "lambda", "vv", ":", "'_'", ".", "join", "(", "vv", ".", "lower", "(", ")", ".", "split", "(", ")", ")", ".", "replace", "(", "'fy_'", ",", "'fy'", ")", ",", "inplace", "=", "True", ",", ")", "years", "=", "res", ".", "columns", "[", "res", ".", "columns", ".", "str", ".", "startswith", "(", "'fy'", ")", "]", "lvl_1", "=", "res", ".", "level", "==", "1", "for", "yr", "in", "years", ":", "res", ".", "loc", "[", ":", ",", "yr", "]", "=", "res", ".", "loc", "[", ":", ",", "yr", "]", ".", "round", "(", "1", ")", "pct", "=", "f'{yr}_pct'", "res", ".", "loc", "[", ":", ",", "pct", "]", "=", "0.", "res", ".", "loc", "[", "lvl_1", ",", "pct", "]", "=", "res", ".", "loc", "[", "lvl_1", ",", "pct", "]", ".", "astype", "(", "float", ")", ".", "round", "(", "1", ")", "res", ".", "loc", "[", "lvl_1", ",", "pct", "]", "=", "res", ".", "loc", "[", "lvl_1", ",", "yr", "]", "/", "res", ".", "loc", "[", "lvl_1", ",", "yr", "]", ".", "sum", "(", ")", "*", "100", "sub_pct", "=", "[", "]", "for", "_", ",", "snap", "in", "res", "[", ":", ":", "-", "1", "]", ".", "iterrows", "(", ")", ":", "if", "snap", ".", "level", ">", "2", ":", "continue", "if", "snap", ".", "level", "==", "1", ":", "if", "len", "(", "sub_pct", ")", "==", "0", ":", "continue", "sub", "=", "pd", ".", "concat", "(", "sub_pct", ",", "axis", "=", "1", ")", ".", "transpose", "(", ")", "res", ".", "loc", "[", "sub", ".", "index", ",", "pct", "]", "=", "res", ".", "loc", "[", "sub", ".", "index", ",", "yr", "]", "/", "res", ".", "loc", "[", "sub", ".", "index", ",", "yr", "]", ".", "sum", "(", ")", "*", "100", "sub_pct", "=", "[", "]", "if", "snap", ".", "level", "==", "2", ":", "sub_pct", ".", "append", "(", "snap", ")", "res", ".", "set_index", "(", "'segment_name'", ",", "inplace", "=", "True", ")", "res", ".", "index", ".", "name", "=", "None", "return", "res" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
format_output
Format `pdblp` outputs to column-based results Args: data: `pdblp` result source: `bdp` or `bds` col_maps: rename columns with these mappings Returns: pd.DataFrame Examples: >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_bdp.pkl'), ... source='bdp' ... ).reset_index() ticker name 0 QQQ US Equity INVESCO QQQ TRUST SERIES 1 1 SPY US Equity SPDR S&P 500 ETF TRUST >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_dvd.pkl'), ... source='bds', col_maps={'Dividend Frequency': 'dvd_freq'} ... ).loc[:, ['ex_date', 'dividend_amount', 'dvd_freq']].reset_index() ticker ex_date dividend_amount dvd_freq 0 C US Equity 2018-02-02 0.32 Quarter
xbbg/core/assist.py
def format_output(data: pd.DataFrame, source, col_maps=None) -> pd.DataFrame: """ Format `pdblp` outputs to column-based results Args: data: `pdblp` result source: `bdp` or `bds` col_maps: rename columns with these mappings Returns: pd.DataFrame Examples: >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_bdp.pkl'), ... source='bdp' ... ).reset_index() ticker name 0 QQQ US Equity INVESCO QQQ TRUST SERIES 1 1 SPY US Equity SPDR S&P 500 ETF TRUST >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_dvd.pkl'), ... source='bds', col_maps={'Dividend Frequency': 'dvd_freq'} ... ).loc[:, ['ex_date', 'dividend_amount', 'dvd_freq']].reset_index() ticker ex_date dividend_amount dvd_freq 0 C US Equity 2018-02-02 0.32 Quarter """ if data.empty: return pd.DataFrame() if source == 'bdp': req_cols = ['ticker', 'field', 'value'] else: req_cols = ['ticker', 'field', 'name', 'value', 'position'] if any(col not in data for col in req_cols): return pd.DataFrame() if data.dropna(subset=['value']).empty: return pd.DataFrame() if source == 'bdp': res = pd.DataFrame(pd.concat([ pd.Series({**{'ticker': t}, **grp.set_index('field').value.to_dict()}) for t, grp in data.groupby('ticker') ], axis=1, sort=False)).transpose().set_index('ticker') else: res = pd.DataFrame(pd.concat([ grp.loc[:, ['name', 'value']].set_index('name') .transpose().reset_index(drop=True).assign(ticker=t) for (t, _), grp in data.groupby(['ticker', 'position']) ], sort=False)).reset_index(drop=True).set_index('ticker') res.columns.name = None if col_maps is None: col_maps = dict() return res.rename( columns=lambda vv: col_maps.get( vv, vv.lower().replace(' ', '_').replace('-', '_') ) ).apply(pd.to_numeric, errors='ignore', downcast='float')
def format_output(data: pd.DataFrame, source, col_maps=None) -> pd.DataFrame: """ Format `pdblp` outputs to column-based results Args: data: `pdblp` result source: `bdp` or `bds` col_maps: rename columns with these mappings Returns: pd.DataFrame Examples: >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_bdp.pkl'), ... source='bdp' ... ).reset_index() ticker name 0 QQQ US Equity INVESCO QQQ TRUST SERIES 1 1 SPY US Equity SPDR S&P 500 ETF TRUST >>> format_output( ... data=pd.read_pickle('xbbg/tests/data/sample_dvd.pkl'), ... source='bds', col_maps={'Dividend Frequency': 'dvd_freq'} ... ).loc[:, ['ex_date', 'dividend_amount', 'dvd_freq']].reset_index() ticker ex_date dividend_amount dvd_freq 0 C US Equity 2018-02-02 0.32 Quarter """ if data.empty: return pd.DataFrame() if source == 'bdp': req_cols = ['ticker', 'field', 'value'] else: req_cols = ['ticker', 'field', 'name', 'value', 'position'] if any(col not in data for col in req_cols): return pd.DataFrame() if data.dropna(subset=['value']).empty: return pd.DataFrame() if source == 'bdp': res = pd.DataFrame(pd.concat([ pd.Series({**{'ticker': t}, **grp.set_index('field').value.to_dict()}) for t, grp in data.groupby('ticker') ], axis=1, sort=False)).transpose().set_index('ticker') else: res = pd.DataFrame(pd.concat([ grp.loc[:, ['name', 'value']].set_index('name') .transpose().reset_index(drop=True).assign(ticker=t) for (t, _), grp in data.groupby(['ticker', 'position']) ], sort=False)).reset_index(drop=True).set_index('ticker') res.columns.name = None if col_maps is None: col_maps = dict() return res.rename( columns=lambda vv: col_maps.get( vv, vv.lower().replace(' ', '_').replace('-', '_') ) ).apply(pd.to_numeric, errors='ignore', downcast='float')
[ "Format", "pdblp", "outputs", "to", "column", "-", "based", "results" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L178-L229
[ "def", "format_output", "(", "data", ":", "pd", ".", "DataFrame", ",", "source", ",", "col_maps", "=", "None", ")", "->", "pd", ".", "DataFrame", ":", "if", "data", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "if", "source", "==", "'bdp'", ":", "req_cols", "=", "[", "'ticker'", ",", "'field'", ",", "'value'", "]", "else", ":", "req_cols", "=", "[", "'ticker'", ",", "'field'", ",", "'name'", ",", "'value'", ",", "'position'", "]", "if", "any", "(", "col", "not", "in", "data", "for", "col", "in", "req_cols", ")", ":", "return", "pd", ".", "DataFrame", "(", ")", "if", "data", ".", "dropna", "(", "subset", "=", "[", "'value'", "]", ")", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "if", "source", "==", "'bdp'", ":", "res", "=", "pd", ".", "DataFrame", "(", "pd", ".", "concat", "(", "[", "pd", ".", "Series", "(", "{", "*", "*", "{", "'ticker'", ":", "t", "}", ",", "*", "*", "grp", ".", "set_index", "(", "'field'", ")", ".", "value", ".", "to_dict", "(", ")", "}", ")", "for", "t", ",", "grp", "in", "data", ".", "groupby", "(", "'ticker'", ")", "]", ",", "axis", "=", "1", ",", "sort", "=", "False", ")", ")", ".", "transpose", "(", ")", ".", "set_index", "(", "'ticker'", ")", "else", ":", "res", "=", "pd", ".", "DataFrame", "(", "pd", ".", "concat", "(", "[", "grp", ".", "loc", "[", ":", ",", "[", "'name'", ",", "'value'", "]", "]", ".", "set_index", "(", "'name'", ")", ".", "transpose", "(", ")", ".", "reset_index", "(", "drop", "=", "True", ")", ".", "assign", "(", "ticker", "=", "t", ")", "for", "(", "t", ",", "_", ")", ",", "grp", "in", "data", ".", "groupby", "(", "[", "'ticker'", ",", "'position'", "]", ")", "]", ",", "sort", "=", "False", ")", ")", ".", "reset_index", "(", "drop", "=", "True", ")", ".", "set_index", "(", "'ticker'", ")", "res", ".", "columns", ".", "name", "=", "None", "if", "col_maps", "is", "None", ":", "col_maps", "=", "dict", "(", ")", "return", "res", ".", "rename", "(", "columns", "=", "lambda", "vv", ":", "col_maps", ".", "get", "(", "vv", ",", "vv", ".", "lower", "(", ")", ".", "replace", "(", "' '", ",", "'_'", ")", ".", "replace", "(", "'-'", ",", "'_'", ")", ")", ")", ".", "apply", "(", "pd", ".", "to_numeric", ",", "errors", "=", "'ignore'", ",", "downcast", "=", "'float'", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
format_intraday
Format intraday data Args: data: pd.DataFrame from bdib ticker: ticker Returns: pd.DataFrame Examples: >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', ... ).xs('close', axis=1, level=1, drop_level=False) ticker SPY US Equity field close 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86 >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', price_only=True ... ) ticker SPY US Equity 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86
xbbg/core/assist.py
def format_intraday(data: pd.DataFrame, ticker, **kwargs) -> pd.DataFrame: """ Format intraday data Args: data: pd.DataFrame from bdib ticker: ticker Returns: pd.DataFrame Examples: >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', ... ).xs('close', axis=1, level=1, drop_level=False) ticker SPY US Equity field close 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86 >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', price_only=True ... ) ticker SPY US Equity 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86 """ if data.empty: return pd.DataFrame() data.columns = pd.MultiIndex.from_product([ [ticker], data.rename(columns=dict(numEvents='num_trds')).columns ], names=['ticker', 'field']) data.index.name = None if kwargs.get('price_only', False): kw_xs = dict(axis=1, level=1) close = data.xs('close', **kw_xs) volume = data.xs('volume', **kw_xs).iloc[:, 0] return close.loc[volume > 0] if volume.min() > 0 else close else: return data
def format_intraday(data: pd.DataFrame, ticker, **kwargs) -> pd.DataFrame: """ Format intraday data Args: data: pd.DataFrame from bdib ticker: ticker Returns: pd.DataFrame Examples: >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', ... ).xs('close', axis=1, level=1, drop_level=False) ticker SPY US Equity field close 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86 >>> format_intraday( ... data=pd.read_parquet('xbbg/tests/data/sample_bdib.parq'), ... ticker='SPY US Equity', price_only=True ... ) ticker SPY US Equity 2018-12-28 09:30:00-05:00 249.67 2018-12-28 09:31:00-05:00 249.54 2018-12-28 09:32:00-05:00 249.22 2018-12-28 09:33:00-05:00 249.01 2018-12-28 09:34:00-05:00 248.86 """ if data.empty: return pd.DataFrame() data.columns = pd.MultiIndex.from_product([ [ticker], data.rename(columns=dict(numEvents='num_trds')).columns ], names=['ticker', 'field']) data.index.name = None if kwargs.get('price_only', False): kw_xs = dict(axis=1, level=1) close = data.xs('close', **kw_xs) volume = data.xs('volume', **kw_xs).iloc[:, 0] return close.loc[volume > 0] if volume.min() > 0 else close else: return data
[ "Format", "intraday", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L232-L276
[ "def", "format_intraday", "(", "data", ":", "pd", ".", "DataFrame", ",", "ticker", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "if", "data", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "data", ".", "columns", "=", "pd", ".", "MultiIndex", ".", "from_product", "(", "[", "[", "ticker", "]", ",", "data", ".", "rename", "(", "columns", "=", "dict", "(", "numEvents", "=", "'num_trds'", ")", ")", ".", "columns", "]", ",", "names", "=", "[", "'ticker'", ",", "'field'", "]", ")", "data", ".", "index", ".", "name", "=", "None", "if", "kwargs", ".", "get", "(", "'price_only'", ",", "False", ")", ":", "kw_xs", "=", "dict", "(", "axis", "=", "1", ",", "level", "=", "1", ")", "close", "=", "data", ".", "xs", "(", "'close'", ",", "*", "*", "kw_xs", ")", "volume", "=", "data", ".", "xs", "(", "'volume'", ",", "*", "*", "kw_xs", ")", ".", "iloc", "[", ":", ",", "0", "]", "return", "close", ".", "loc", "[", "volume", ">", "0", "]", "if", "volume", ".", "min", "(", ")", ">", "0", "else", "close", "else", ":", "return", "data" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
info_qry
Logging info for given tickers and fields Args: tickers: tickers flds: fields Returns: str Examples: >>> print(info_qry( ... tickers=['NVDA US Equity'], flds=['Name', 'Security_Name'] ... )) tickers: ['NVDA US Equity'] fields: ['Name', 'Security_Name']
xbbg/core/assist.py
def info_qry(tickers, flds) -> str: """ Logging info for given tickers and fields Args: tickers: tickers flds: fields Returns: str Examples: >>> print(info_qry( ... tickers=['NVDA US Equity'], flds=['Name', 'Security_Name'] ... )) tickers: ['NVDA US Equity'] fields: ['Name', 'Security_Name'] """ full_list = '\n'.join([f'tickers: {tickers[:8]}'] + [ f' {tickers[n:(n + 8)]}' for n in range(8, len(tickers), 8) ]) return f'{full_list}\nfields: {flds}'
def info_qry(tickers, flds) -> str: """ Logging info for given tickers and fields Args: tickers: tickers flds: fields Returns: str Examples: >>> print(info_qry( ... tickers=['NVDA US Equity'], flds=['Name', 'Security_Name'] ... )) tickers: ['NVDA US Equity'] fields: ['Name', 'Security_Name'] """ full_list = '\n'.join([f'tickers: {tickers[:8]}'] + [ f' {tickers[n:(n + 8)]}' for n in range(8, len(tickers), 8) ]) return f'{full_list}\nfields: {flds}'
[ "Logging", "info", "for", "given", "tickers", "and", "fields" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/assist.py#L279-L300
[ "def", "info_qry", "(", "tickers", ",", "flds", ")", "->", "str", ":", "full_list", "=", "'\\n'", ".", "join", "(", "[", "f'tickers: {tickers[:8]}'", "]", "+", "[", "f' {tickers[n:(n + 8)]}'", "for", "n", "in", "range", "(", "8", ",", "len", "(", "tickers", ")", ",", "8", ")", "]", ")", "return", "f'{full_list}\\nfields: {flds}'" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
bdp
Bloomberg reference data Args: tickers: tickers flds: fields to query **kwargs: bbg overrides Returns: pd.DataFrame Examples: >>> bdp('IQ US Equity', 'Crncy', raw=True) ticker field value 0 IQ US Equity Crncy USD >>> bdp('IQ US Equity', 'Crncy').reset_index() ticker crncy 0 IQ US Equity USD
xbbg/blp.py
def bdp(tickers, flds, **kwargs): """ Bloomberg reference data Args: tickers: tickers flds: fields to query **kwargs: bbg overrides Returns: pd.DataFrame Examples: >>> bdp('IQ US Equity', 'Crncy', raw=True) ticker field value 0 IQ US Equity Crncy USD >>> bdp('IQ US Equity', 'Crncy').reset_index() ticker crncy 0 IQ US Equity USD """ logger = logs.get_logger(bdp, level=kwargs.pop('log', logs.LOG_LEVEL)) con, _ = create_connection() ovrds = assist.proc_ovrds(**kwargs) logger.info( f'loading reference data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) data = con.ref(tickers=tickers, flds=flds, ovrds=ovrds) if not kwargs.get('cache', False): return [data] qry_data = [] for r, snap in data.iterrows(): subset = [r] data_file = storage.ref_file( ticker=snap.ticker, fld=snap.field, ext='pkl', **kwargs ) if data_file: if not files.exists(data_file): qry_data.append(data.iloc[subset]) files.create_folder(data_file, is_file=True) data.iloc[subset].to_pickle(data_file) return qry_data
def bdp(tickers, flds, **kwargs): """ Bloomberg reference data Args: tickers: tickers flds: fields to query **kwargs: bbg overrides Returns: pd.DataFrame Examples: >>> bdp('IQ US Equity', 'Crncy', raw=True) ticker field value 0 IQ US Equity Crncy USD >>> bdp('IQ US Equity', 'Crncy').reset_index() ticker crncy 0 IQ US Equity USD """ logger = logs.get_logger(bdp, level=kwargs.pop('log', logs.LOG_LEVEL)) con, _ = create_connection() ovrds = assist.proc_ovrds(**kwargs) logger.info( f'loading reference data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) data = con.ref(tickers=tickers, flds=flds, ovrds=ovrds) if not kwargs.get('cache', False): return [data] qry_data = [] for r, snap in data.iterrows(): subset = [r] data_file = storage.ref_file( ticker=snap.ticker, fld=snap.field, ext='pkl', **kwargs ) if data_file: if not files.exists(data_file): qry_data.append(data.iloc[subset]) files.create_folder(data_file, is_file=True) data.iloc[subset].to_pickle(data_file) return qry_data
[ "Bloomberg", "reference", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L30-L72
[ "def", "bdp", "(", "tickers", ",", "flds", ",", "*", "*", "kwargs", ")", ":", "logger", "=", "logs", ".", "get_logger", "(", "bdp", ",", "level", "=", "kwargs", ".", "pop", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", ")", "con", ",", "_", "=", "create_connection", "(", ")", "ovrds", "=", "assist", ".", "proc_ovrds", "(", "*", "*", "kwargs", ")", "logger", ".", "info", "(", "f'loading reference data from Bloomberg:\\n'", "f'{assist.info_qry(tickers=tickers, flds=flds)}'", ")", "data", "=", "con", ".", "ref", "(", "tickers", "=", "tickers", ",", "flds", "=", "flds", ",", "ovrds", "=", "ovrds", ")", "if", "not", "kwargs", ".", "get", "(", "'cache'", ",", "False", ")", ":", "return", "[", "data", "]", "qry_data", "=", "[", "]", "for", "r", ",", "snap", "in", "data", ".", "iterrows", "(", ")", ":", "subset", "=", "[", "r", "]", "data_file", "=", "storage", ".", "ref_file", "(", "ticker", "=", "snap", ".", "ticker", ",", "fld", "=", "snap", ".", "field", ",", "ext", "=", "'pkl'", ",", "*", "*", "kwargs", ")", "if", "data_file", ":", "if", "not", "files", ".", "exists", "(", "data_file", ")", ":", "qry_data", ".", "append", "(", "data", ".", "iloc", "[", "subset", "]", ")", "files", ".", "create_folder", "(", "data_file", ",", "is_file", "=", "True", ")", "data", ".", "iloc", "[", "subset", "]", ".", "to_pickle", "(", "data_file", ")", "return", "qry_data" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
bds
Bloomberg block data Args: tickers: ticker(s) flds: field(s) **kwargs: other overrides for query -> raw: raw output from `pdbdp` library, default False Returns: pd.DataFrame: block data Examples: >>> import os >>> >>> pd.options.display.width = 120 >>> s_dt, e_dt = '20180301', '20181031' >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, raw=True, ... ) >>> dvd.loc[:, ['ticker', 'name', 'value']].head(8) ticker name value 0 NVDA US Equity Declared Date 2018-08-16 1 NVDA US Equity Ex-Date 2018-08-29 2 NVDA US Equity Record Date 2018-08-30 3 NVDA US Equity Payable Date 2018-09-21 4 NVDA US Equity Dividend Amount 0.15 5 NVDA US Equity Dividend Frequency Quarter 6 NVDA US Equity Dividend Type Regular Cash 7 NVDA US Equity Declared Date 2018-05-10 >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, ... ) >>> dvd.reset_index().loc[:, ['ticker', 'ex_date', 'dividend_amount']] ticker ex_date dividend_amount 0 NVDA US Equity 2018-08-29 0.15 1 NVDA US Equity 2018-05-23 0.15 >>> if not os.environ.get('BBG_ROOT', ''): ... os.environ['BBG_ROOT'] = f'{files.abspath(__file__, 1)}/tests/data' >>> idx_kw = dict(End_Dt='20181220', cache=True) >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).tail().reset_index(drop=True) index_member percent_weight 0 V UN 3.82 1 VZ UN 1.63 2 WBA UW 2.06 3 WMT UN 2.59 4 XOM UN 2.04 >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).head().reset_index(drop=True) index_member percent_weight 0 AAPL UW 4.65 1 AXP UN 2.84 2 BA UN 9.29 3 CAT UN 3.61 4 CSCO UW 1.26
xbbg/blp.py
def bds(tickers, flds, **kwargs): """ Bloomberg block data Args: tickers: ticker(s) flds: field(s) **kwargs: other overrides for query -> raw: raw output from `pdbdp` library, default False Returns: pd.DataFrame: block data Examples: >>> import os >>> >>> pd.options.display.width = 120 >>> s_dt, e_dt = '20180301', '20181031' >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, raw=True, ... ) >>> dvd.loc[:, ['ticker', 'name', 'value']].head(8) ticker name value 0 NVDA US Equity Declared Date 2018-08-16 1 NVDA US Equity Ex-Date 2018-08-29 2 NVDA US Equity Record Date 2018-08-30 3 NVDA US Equity Payable Date 2018-09-21 4 NVDA US Equity Dividend Amount 0.15 5 NVDA US Equity Dividend Frequency Quarter 6 NVDA US Equity Dividend Type Regular Cash 7 NVDA US Equity Declared Date 2018-05-10 >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, ... ) >>> dvd.reset_index().loc[:, ['ticker', 'ex_date', 'dividend_amount']] ticker ex_date dividend_amount 0 NVDA US Equity 2018-08-29 0.15 1 NVDA US Equity 2018-05-23 0.15 >>> if not os.environ.get('BBG_ROOT', ''): ... os.environ['BBG_ROOT'] = f'{files.abspath(__file__, 1)}/tests/data' >>> idx_kw = dict(End_Dt='20181220', cache=True) >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).tail().reset_index(drop=True) index_member percent_weight 0 V UN 3.82 1 VZ UN 1.63 2 WBA UW 2.06 3 WMT UN 2.59 4 XOM UN 2.04 >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).head().reset_index(drop=True) index_member percent_weight 0 AAPL UW 4.65 1 AXP UN 2.84 2 BA UN 9.29 3 CAT UN 3.61 4 CSCO UW 1.26 """ logger = logs.get_logger(bds, level=kwargs.pop('log', logs.LOG_LEVEL)) con, _ = create_connection() ovrds = assist.proc_ovrds(**kwargs) logger.info( f'loading block data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) data = con.bulkref(tickers=tickers, flds=flds, ovrds=ovrds) if not kwargs.get('cache', False): return [data] qry_data = [] for (ticker, fld), grp in data.groupby(['ticker', 'field']): data_file = storage.ref_file( ticker=ticker, fld=fld, ext='pkl', has_date=kwargs.get('has_date', True), **kwargs ) if data_file: if not files.exists(data_file): qry_data.append(grp) files.create_folder(data_file, is_file=True) grp.reset_index(drop=True).to_pickle(data_file) return qry_data
def bds(tickers, flds, **kwargs): """ Bloomberg block data Args: tickers: ticker(s) flds: field(s) **kwargs: other overrides for query -> raw: raw output from `pdbdp` library, default False Returns: pd.DataFrame: block data Examples: >>> import os >>> >>> pd.options.display.width = 120 >>> s_dt, e_dt = '20180301', '20181031' >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, raw=True, ... ) >>> dvd.loc[:, ['ticker', 'name', 'value']].head(8) ticker name value 0 NVDA US Equity Declared Date 2018-08-16 1 NVDA US Equity Ex-Date 2018-08-29 2 NVDA US Equity Record Date 2018-08-30 3 NVDA US Equity Payable Date 2018-09-21 4 NVDA US Equity Dividend Amount 0.15 5 NVDA US Equity Dividend Frequency Quarter 6 NVDA US Equity Dividend Type Regular Cash 7 NVDA US Equity Declared Date 2018-05-10 >>> dvd = bds( ... 'NVDA US Equity', 'DVD_Hist_All', ... DVD_Start_Dt=s_dt, DVD_End_Dt=e_dt, ... ) >>> dvd.reset_index().loc[:, ['ticker', 'ex_date', 'dividend_amount']] ticker ex_date dividend_amount 0 NVDA US Equity 2018-08-29 0.15 1 NVDA US Equity 2018-05-23 0.15 >>> if not os.environ.get('BBG_ROOT', ''): ... os.environ['BBG_ROOT'] = f'{files.abspath(__file__, 1)}/tests/data' >>> idx_kw = dict(End_Dt='20181220', cache=True) >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).tail().reset_index(drop=True) index_member percent_weight 0 V UN 3.82 1 VZ UN 1.63 2 WBA UW 2.06 3 WMT UN 2.59 4 XOM UN 2.04 >>> idx_wt = bds('DJI Index', 'Indx_MWeight_Hist', **idx_kw) >>> idx_wt.round(2).head().reset_index(drop=True) index_member percent_weight 0 AAPL UW 4.65 1 AXP UN 2.84 2 BA UN 9.29 3 CAT UN 3.61 4 CSCO UW 1.26 """ logger = logs.get_logger(bds, level=kwargs.pop('log', logs.LOG_LEVEL)) con, _ = create_connection() ovrds = assist.proc_ovrds(**kwargs) logger.info( f'loading block data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) data = con.bulkref(tickers=tickers, flds=flds, ovrds=ovrds) if not kwargs.get('cache', False): return [data] qry_data = [] for (ticker, fld), grp in data.groupby(['ticker', 'field']): data_file = storage.ref_file( ticker=ticker, fld=fld, ext='pkl', has_date=kwargs.get('has_date', True), **kwargs ) if data_file: if not files.exists(data_file): qry_data.append(grp) files.create_folder(data_file, is_file=True) grp.reset_index(drop=True).to_pickle(data_file) return qry_data
[ "Bloomberg", "block", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L76-L158
[ "def", "bds", "(", "tickers", ",", "flds", ",", "*", "*", "kwargs", ")", ":", "logger", "=", "logs", ".", "get_logger", "(", "bds", ",", "level", "=", "kwargs", ".", "pop", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", ")", "con", ",", "_", "=", "create_connection", "(", ")", "ovrds", "=", "assist", ".", "proc_ovrds", "(", "*", "*", "kwargs", ")", "logger", ".", "info", "(", "f'loading block data from Bloomberg:\\n'", "f'{assist.info_qry(tickers=tickers, flds=flds)}'", ")", "data", "=", "con", ".", "bulkref", "(", "tickers", "=", "tickers", ",", "flds", "=", "flds", ",", "ovrds", "=", "ovrds", ")", "if", "not", "kwargs", ".", "get", "(", "'cache'", ",", "False", ")", ":", "return", "[", "data", "]", "qry_data", "=", "[", "]", "for", "(", "ticker", ",", "fld", ")", ",", "grp", "in", "data", ".", "groupby", "(", "[", "'ticker'", ",", "'field'", "]", ")", ":", "data_file", "=", "storage", ".", "ref_file", "(", "ticker", "=", "ticker", ",", "fld", "=", "fld", ",", "ext", "=", "'pkl'", ",", "has_date", "=", "kwargs", ".", "get", "(", "'has_date'", ",", "True", ")", ",", "*", "*", "kwargs", ")", "if", "data_file", ":", "if", "not", "files", ".", "exists", "(", "data_file", ")", ":", "qry_data", ".", "append", "(", "grp", ")", "files", ".", "create_folder", "(", "data_file", ",", "is_file", "=", "True", ")", "grp", ".", "reset_index", "(", "drop", "=", "True", ")", ".", "to_pickle", "(", "data_file", ")", "return", "qry_data" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
bdh
Bloomberg historical data Args: tickers: ticker(s) flds: field(s) start_date: start date end_date: end date - default today adjust: `all`, `dvd`, `normal`, `abn` (=abnormal), `split`, `-` or None exact match of above words will adjust for corresponding events Case 0: `-` no adjustment for dividend or split Case 1: `dvd` or `normal|abn` will adjust for all dividends except splits Case 2: `adjust` will adjust for splits and ignore all dividends Case 3: `all` == `dvd|split` == adjust for all Case 4: None == Bloomberg default OR use kwargs **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = bdh( ... tickers='VIX Index', flds=['High', 'Low', 'Last_Price'], ... start_date='2018-02-05', end_date='2018-02-07', ... ).round(2).transpose() >>> res.index.name = None >>> res.columns.name = None >>> res 2018-02-05 2018-02-06 2018-02-07 VIX Index High 38.80 50.30 31.64 Low 16.80 22.42 21.17 Last_Price 37.32 29.98 27.73 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140605', end_date='20140610', adjust='-' ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-05 647.35 2014-06-06 645.57 2014-06-09 93.70 2014-06-10 94.25 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140606', end_date='20140609', ... CshAdjNormal=False, CshAdjAbnormal=False, CapChg=False, ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-06 645.57 2014-06-09 93.70
xbbg/blp.py
def bdh( tickers, flds=None, start_date=None, end_date='today', adjust=None, **kwargs ) -> pd.DataFrame: """ Bloomberg historical data Args: tickers: ticker(s) flds: field(s) start_date: start date end_date: end date - default today adjust: `all`, `dvd`, `normal`, `abn` (=abnormal), `split`, `-` or None exact match of above words will adjust for corresponding events Case 0: `-` no adjustment for dividend or split Case 1: `dvd` or `normal|abn` will adjust for all dividends except splits Case 2: `adjust` will adjust for splits and ignore all dividends Case 3: `all` == `dvd|split` == adjust for all Case 4: None == Bloomberg default OR use kwargs **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = bdh( ... tickers='VIX Index', flds=['High', 'Low', 'Last_Price'], ... start_date='2018-02-05', end_date='2018-02-07', ... ).round(2).transpose() >>> res.index.name = None >>> res.columns.name = None >>> res 2018-02-05 2018-02-06 2018-02-07 VIX Index High 38.80 50.30 31.64 Low 16.80 22.42 21.17 Last_Price 37.32 29.98 27.73 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140605', end_date='20140610', adjust='-' ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-05 647.35 2014-06-06 645.57 2014-06-09 93.70 2014-06-10 94.25 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140606', end_date='20140609', ... CshAdjNormal=False, CshAdjAbnormal=False, CapChg=False, ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-06 645.57 2014-06-09 93.70 """ logger = logs.get_logger(bdh, level=kwargs.pop('log', logs.LOG_LEVEL)) # Dividend adjustments if isinstance(adjust, str) and adjust: if adjust == 'all': kwargs['CshAdjNormal'] = True kwargs['CshAdjAbnormal'] = True kwargs['CapChg'] = True else: kwargs['CshAdjNormal'] = 'normal' in adjust or 'dvd' in adjust kwargs['CshAdjAbnormal'] = 'abn' in adjust or 'dvd' in adjust kwargs['CapChg'] = 'split' in adjust con, _ = create_connection() elms = assist.proc_elms(**kwargs) ovrds = assist.proc_ovrds(**kwargs) if isinstance(tickers, str): tickers = [tickers] if flds is None: flds = ['Last_Price'] if isinstance(flds, str): flds = [flds] e_dt = utils.fmt_dt(end_date, fmt='%Y%m%d') if start_date is None: start_date = pd.Timestamp(e_dt) - relativedelta(months=3) s_dt = utils.fmt_dt(start_date, fmt='%Y%m%d') logger.info( f'loading historical data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) logger.debug( f'\nflds={flds}\nelms={elms}\novrds={ovrds}\nstart_date={s_dt}\nend_date={e_dt}' ) res = con.bdh( tickers=tickers, flds=flds, elms=elms, ovrds=ovrds, start_date=s_dt, end_date=e_dt ) res.index.name = None if (len(flds) == 1) and kwargs.get('keep_one', False): return res.xs(flds[0], axis=1, level=1) return res
def bdh( tickers, flds=None, start_date=None, end_date='today', adjust=None, **kwargs ) -> pd.DataFrame: """ Bloomberg historical data Args: tickers: ticker(s) flds: field(s) start_date: start date end_date: end date - default today adjust: `all`, `dvd`, `normal`, `abn` (=abnormal), `split`, `-` or None exact match of above words will adjust for corresponding events Case 0: `-` no adjustment for dividend or split Case 1: `dvd` or `normal|abn` will adjust for all dividends except splits Case 2: `adjust` will adjust for splits and ignore all dividends Case 3: `all` == `dvd|split` == adjust for all Case 4: None == Bloomberg default OR use kwargs **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = bdh( ... tickers='VIX Index', flds=['High', 'Low', 'Last_Price'], ... start_date='2018-02-05', end_date='2018-02-07', ... ).round(2).transpose() >>> res.index.name = None >>> res.columns.name = None >>> res 2018-02-05 2018-02-06 2018-02-07 VIX Index High 38.80 50.30 31.64 Low 16.80 22.42 21.17 Last_Price 37.32 29.98 27.73 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140605', end_date='20140610', adjust='-' ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-05 647.35 2014-06-06 645.57 2014-06-09 93.70 2014-06-10 94.25 >>> bdh( ... tickers='AAPL US Equity', flds='Px_Last', ... start_date='20140606', end_date='20140609', ... CshAdjNormal=False, CshAdjAbnormal=False, CapChg=False, ... ).round(2) ticker AAPL US Equity field Px_Last 2014-06-06 645.57 2014-06-09 93.70 """ logger = logs.get_logger(bdh, level=kwargs.pop('log', logs.LOG_LEVEL)) # Dividend adjustments if isinstance(adjust, str) and adjust: if adjust == 'all': kwargs['CshAdjNormal'] = True kwargs['CshAdjAbnormal'] = True kwargs['CapChg'] = True else: kwargs['CshAdjNormal'] = 'normal' in adjust or 'dvd' in adjust kwargs['CshAdjAbnormal'] = 'abn' in adjust or 'dvd' in adjust kwargs['CapChg'] = 'split' in adjust con, _ = create_connection() elms = assist.proc_elms(**kwargs) ovrds = assist.proc_ovrds(**kwargs) if isinstance(tickers, str): tickers = [tickers] if flds is None: flds = ['Last_Price'] if isinstance(flds, str): flds = [flds] e_dt = utils.fmt_dt(end_date, fmt='%Y%m%d') if start_date is None: start_date = pd.Timestamp(e_dt) - relativedelta(months=3) s_dt = utils.fmt_dt(start_date, fmt='%Y%m%d') logger.info( f'loading historical data from Bloomberg:\n' f'{assist.info_qry(tickers=tickers, flds=flds)}' ) logger.debug( f'\nflds={flds}\nelms={elms}\novrds={ovrds}\nstart_date={s_dt}\nend_date={e_dt}' ) res = con.bdh( tickers=tickers, flds=flds, elms=elms, ovrds=ovrds, start_date=s_dt, end_date=e_dt ) res.index.name = None if (len(flds) == 1) and kwargs.get('keep_one', False): return res.xs(flds[0], axis=1, level=1) return res
[ "Bloomberg", "historical", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L162-L256
[ "def", "bdh", "(", "tickers", ",", "flds", "=", "None", ",", "start_date", "=", "None", ",", "end_date", "=", "'today'", ",", "adjust", "=", "None", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "logger", "=", "logs", ".", "get_logger", "(", "bdh", ",", "level", "=", "kwargs", ".", "pop", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", ")", "# Dividend adjustments", "if", "isinstance", "(", "adjust", ",", "str", ")", "and", "adjust", ":", "if", "adjust", "==", "'all'", ":", "kwargs", "[", "'CshAdjNormal'", "]", "=", "True", "kwargs", "[", "'CshAdjAbnormal'", "]", "=", "True", "kwargs", "[", "'CapChg'", "]", "=", "True", "else", ":", "kwargs", "[", "'CshAdjNormal'", "]", "=", "'normal'", "in", "adjust", "or", "'dvd'", "in", "adjust", "kwargs", "[", "'CshAdjAbnormal'", "]", "=", "'abn'", "in", "adjust", "or", "'dvd'", "in", "adjust", "kwargs", "[", "'CapChg'", "]", "=", "'split'", "in", "adjust", "con", ",", "_", "=", "create_connection", "(", ")", "elms", "=", "assist", ".", "proc_elms", "(", "*", "*", "kwargs", ")", "ovrds", "=", "assist", ".", "proc_ovrds", "(", "*", "*", "kwargs", ")", "if", "isinstance", "(", "tickers", ",", "str", ")", ":", "tickers", "=", "[", "tickers", "]", "if", "flds", "is", "None", ":", "flds", "=", "[", "'Last_Price'", "]", "if", "isinstance", "(", "flds", ",", "str", ")", ":", "flds", "=", "[", "flds", "]", "e_dt", "=", "utils", ".", "fmt_dt", "(", "end_date", ",", "fmt", "=", "'%Y%m%d'", ")", "if", "start_date", "is", "None", ":", "start_date", "=", "pd", ".", "Timestamp", "(", "e_dt", ")", "-", "relativedelta", "(", "months", "=", "3", ")", "s_dt", "=", "utils", ".", "fmt_dt", "(", "start_date", ",", "fmt", "=", "'%Y%m%d'", ")", "logger", ".", "info", "(", "f'loading historical data from Bloomberg:\\n'", "f'{assist.info_qry(tickers=tickers, flds=flds)}'", ")", "logger", ".", "debug", "(", "f'\\nflds={flds}\\nelms={elms}\\novrds={ovrds}\\nstart_date={s_dt}\\nend_date={e_dt}'", ")", "res", "=", "con", ".", "bdh", "(", "tickers", "=", "tickers", ",", "flds", "=", "flds", ",", "elms", "=", "elms", ",", "ovrds", "=", "ovrds", ",", "start_date", "=", "s_dt", ",", "end_date", "=", "e_dt", ")", "res", ".", "index", ".", "name", "=", "None", "if", "(", "len", "(", "flds", ")", "==", "1", ")", "and", "kwargs", ".", "get", "(", "'keep_one'", ",", "False", ")", ":", "return", "res", ".", "xs", "(", "flds", "[", "0", "]", ",", "axis", "=", "1", ",", "level", "=", "1", ")", "return", "res" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
bdib
Bloomberg intraday bar data Args: ticker: ticker name dt: date to download typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] **kwargs: batch: whether is batch process to download data log: level of logs Returns: pd.DataFrame
xbbg/blp.py
def bdib(ticker, dt, typ='TRADE', **kwargs) -> pd.DataFrame: """ Bloomberg intraday bar data Args: ticker: ticker name dt: date to download typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] **kwargs: batch: whether is batch process to download data log: level of logs Returns: pd.DataFrame """ from xbbg.core import missing logger = logs.get_logger(bdib, level=kwargs.pop('log', logs.LOG_LEVEL)) t_1 = pd.Timestamp('today').date() - pd.Timedelta('1D') whole_day = pd.Timestamp(dt).date() < t_1 batch = kwargs.pop('batch', False) if (not whole_day) and batch: logger.warning(f'querying date {t_1} is too close, ignoring download ...') return pd.DataFrame() cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') asset = ticker.split()[-1] info_log = f'{ticker} / {cur_dt} / {typ}' if asset in ['Equity', 'Curncy', 'Index', 'Comdty']: exch = const.exch_info(ticker=ticker) if exch.empty: return pd.DataFrame() else: logger.error(f'unknown asset type: {asset}') return pd.DataFrame() time_fmt = '%Y-%m-%dT%H:%M:%S' time_idx = pd.DatetimeIndex([ f'{cur_dt} {exch.allday[0]}', f'{cur_dt} {exch.allday[-1]}'] ).tz_localize(exch.tz).tz_convert(DEFAULT_TZ).tz_convert('UTC') if time_idx[0] > time_idx[1]: time_idx -= pd.TimedeltaIndex(['1D', '0D']) q_tckr = ticker if exch.get('is_fut', False): if 'freq' not in exch: logger.error(f'[freq] missing in info for {info_log} ...') is_sprd = exch.get('has_sprd', False) and (len(ticker[:-1]) != exch['tickers'][0]) if not is_sprd: q_tckr = fut_ticker(gen_ticker=ticker, dt=dt, freq=exch['freq']) if q_tckr == '': logger.error(f'cannot find futures ticker for {ticker} ...') return pd.DataFrame() info_log = f'{q_tckr} / {cur_dt} / {typ}' miss_kw = dict(ticker=ticker, dt=dt, typ=typ, func='bdib') cur_miss = missing.current_missing(**miss_kw) if cur_miss >= 2: if batch: return pd.DataFrame() logger.info(f'{cur_miss} trials with no data {info_log}') return pd.DataFrame() logger.info(f'loading data from Bloomberg: {info_log} ...') con, _ = create_connection() try: data = con.bdib( ticker=q_tckr, event_type=typ, interval=1, start_datetime=time_idx[0].strftime(time_fmt), end_datetime=time_idx[1].strftime(time_fmt), ) except KeyError: # Ignores missing data errors from pdblp library # Warning msg will be displayed later data = pd.DataFrame() if not isinstance(data, pd.DataFrame): raise ValueError(f'unknown output format: {type(data)}') if data.empty: logger.warning(f'no data for {info_log} ...') missing.update_missing(**miss_kw) return pd.DataFrame() data = data.tz_localize('UTC').tz_convert(exch.tz) storage.save_intraday(data=data, ticker=ticker, dt=dt, typ=typ) return pd.DataFrame() if batch else assist.format_intraday(data=data, ticker=ticker)
def bdib(ticker, dt, typ='TRADE', **kwargs) -> pd.DataFrame: """ Bloomberg intraday bar data Args: ticker: ticker name dt: date to download typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] **kwargs: batch: whether is batch process to download data log: level of logs Returns: pd.DataFrame """ from xbbg.core import missing logger = logs.get_logger(bdib, level=kwargs.pop('log', logs.LOG_LEVEL)) t_1 = pd.Timestamp('today').date() - pd.Timedelta('1D') whole_day = pd.Timestamp(dt).date() < t_1 batch = kwargs.pop('batch', False) if (not whole_day) and batch: logger.warning(f'querying date {t_1} is too close, ignoring download ...') return pd.DataFrame() cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') asset = ticker.split()[-1] info_log = f'{ticker} / {cur_dt} / {typ}' if asset in ['Equity', 'Curncy', 'Index', 'Comdty']: exch = const.exch_info(ticker=ticker) if exch.empty: return pd.DataFrame() else: logger.error(f'unknown asset type: {asset}') return pd.DataFrame() time_fmt = '%Y-%m-%dT%H:%M:%S' time_idx = pd.DatetimeIndex([ f'{cur_dt} {exch.allday[0]}', f'{cur_dt} {exch.allday[-1]}'] ).tz_localize(exch.tz).tz_convert(DEFAULT_TZ).tz_convert('UTC') if time_idx[0] > time_idx[1]: time_idx -= pd.TimedeltaIndex(['1D', '0D']) q_tckr = ticker if exch.get('is_fut', False): if 'freq' not in exch: logger.error(f'[freq] missing in info for {info_log} ...') is_sprd = exch.get('has_sprd', False) and (len(ticker[:-1]) != exch['tickers'][0]) if not is_sprd: q_tckr = fut_ticker(gen_ticker=ticker, dt=dt, freq=exch['freq']) if q_tckr == '': logger.error(f'cannot find futures ticker for {ticker} ...') return pd.DataFrame() info_log = f'{q_tckr} / {cur_dt} / {typ}' miss_kw = dict(ticker=ticker, dt=dt, typ=typ, func='bdib') cur_miss = missing.current_missing(**miss_kw) if cur_miss >= 2: if batch: return pd.DataFrame() logger.info(f'{cur_miss} trials with no data {info_log}') return pd.DataFrame() logger.info(f'loading data from Bloomberg: {info_log} ...') con, _ = create_connection() try: data = con.bdib( ticker=q_tckr, event_type=typ, interval=1, start_datetime=time_idx[0].strftime(time_fmt), end_datetime=time_idx[1].strftime(time_fmt), ) except KeyError: # Ignores missing data errors from pdblp library # Warning msg will be displayed later data = pd.DataFrame() if not isinstance(data, pd.DataFrame): raise ValueError(f'unknown output format: {type(data)}') if data.empty: logger.warning(f'no data for {info_log} ...') missing.update_missing(**miss_kw) return pd.DataFrame() data = data.tz_localize('UTC').tz_convert(exch.tz) storage.save_intraday(data=data, ticker=ticker, dt=dt, typ=typ) return pd.DataFrame() if batch else assist.format_intraday(data=data, ticker=ticker)
[ "Bloomberg", "intraday", "bar", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L260-L347
[ "def", "bdib", "(", "ticker", ",", "dt", ",", "typ", "=", "'TRADE'", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "from", "xbbg", ".", "core", "import", "missing", "logger", "=", "logs", ".", "get_logger", "(", "bdib", ",", "level", "=", "kwargs", ".", "pop", "(", "'log'", ",", "logs", ".", "LOG_LEVEL", ")", ")", "t_1", "=", "pd", ".", "Timestamp", "(", "'today'", ")", ".", "date", "(", ")", "-", "pd", ".", "Timedelta", "(", "'1D'", ")", "whole_day", "=", "pd", ".", "Timestamp", "(", "dt", ")", ".", "date", "(", ")", "<", "t_1", "batch", "=", "kwargs", ".", "pop", "(", "'batch'", ",", "False", ")", "if", "(", "not", "whole_day", ")", "and", "batch", ":", "logger", ".", "warning", "(", "f'querying date {t_1} is too close, ignoring download ...'", ")", "return", "pd", ".", "DataFrame", "(", ")", "cur_dt", "=", "pd", ".", "Timestamp", "(", "dt", ")", ".", "strftime", "(", "'%Y-%m-%d'", ")", "asset", "=", "ticker", ".", "split", "(", ")", "[", "-", "1", "]", "info_log", "=", "f'{ticker} / {cur_dt} / {typ}'", "if", "asset", "in", "[", "'Equity'", ",", "'Curncy'", ",", "'Index'", ",", "'Comdty'", "]", ":", "exch", "=", "const", ".", "exch_info", "(", "ticker", "=", "ticker", ")", "if", "exch", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "else", ":", "logger", ".", "error", "(", "f'unknown asset type: {asset}'", ")", "return", "pd", ".", "DataFrame", "(", ")", "time_fmt", "=", "'%Y-%m-%dT%H:%M:%S'", "time_idx", "=", "pd", ".", "DatetimeIndex", "(", "[", "f'{cur_dt} {exch.allday[0]}'", ",", "f'{cur_dt} {exch.allday[-1]}'", "]", ")", ".", "tz_localize", "(", "exch", ".", "tz", ")", ".", "tz_convert", "(", "DEFAULT_TZ", ")", ".", "tz_convert", "(", "'UTC'", ")", "if", "time_idx", "[", "0", "]", ">", "time_idx", "[", "1", "]", ":", "time_idx", "-=", "pd", ".", "TimedeltaIndex", "(", "[", "'1D'", ",", "'0D'", "]", ")", "q_tckr", "=", "ticker", "if", "exch", ".", "get", "(", "'is_fut'", ",", "False", ")", ":", "if", "'freq'", "not", "in", "exch", ":", "logger", ".", "error", "(", "f'[freq] missing in info for {info_log} ...'", ")", "is_sprd", "=", "exch", ".", "get", "(", "'has_sprd'", ",", "False", ")", "and", "(", "len", "(", "ticker", "[", ":", "-", "1", "]", ")", "!=", "exch", "[", "'tickers'", "]", "[", "0", "]", ")", "if", "not", "is_sprd", ":", "q_tckr", "=", "fut_ticker", "(", "gen_ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "freq", "=", "exch", "[", "'freq'", "]", ")", "if", "q_tckr", "==", "''", ":", "logger", ".", "error", "(", "f'cannot find futures ticker for {ticker} ...'", ")", "return", "pd", ".", "DataFrame", "(", ")", "info_log", "=", "f'{q_tckr} / {cur_dt} / {typ}'", "miss_kw", "=", "dict", "(", "ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "typ", "=", "typ", ",", "func", "=", "'bdib'", ")", "cur_miss", "=", "missing", ".", "current_missing", "(", "*", "*", "miss_kw", ")", "if", "cur_miss", ">=", "2", ":", "if", "batch", ":", "return", "pd", ".", "DataFrame", "(", ")", "logger", ".", "info", "(", "f'{cur_miss} trials with no data {info_log}'", ")", "return", "pd", ".", "DataFrame", "(", ")", "logger", ".", "info", "(", "f'loading data from Bloomberg: {info_log} ...'", ")", "con", ",", "_", "=", "create_connection", "(", ")", "try", ":", "data", "=", "con", ".", "bdib", "(", "ticker", "=", "q_tckr", ",", "event_type", "=", "typ", ",", "interval", "=", "1", ",", "start_datetime", "=", "time_idx", "[", "0", "]", ".", "strftime", "(", "time_fmt", ")", ",", "end_datetime", "=", "time_idx", "[", "1", "]", ".", "strftime", "(", "time_fmt", ")", ",", ")", "except", "KeyError", ":", "# Ignores missing data errors from pdblp library", "# Warning msg will be displayed later", "data", "=", "pd", ".", "DataFrame", "(", ")", "if", "not", "isinstance", "(", "data", ",", "pd", ".", "DataFrame", ")", ":", "raise", "ValueError", "(", "f'unknown output format: {type(data)}'", ")", "if", "data", ".", "empty", ":", "logger", ".", "warning", "(", "f'no data for {info_log} ...'", ")", "missing", ".", "update_missing", "(", "*", "*", "miss_kw", ")", "return", "pd", ".", "DataFrame", "(", ")", "data", "=", "data", ".", "tz_localize", "(", "'UTC'", ")", ".", "tz_convert", "(", "exch", ".", "tz", ")", "storage", ".", "save_intraday", "(", "data", "=", "data", ",", "ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "typ", "=", "typ", ")", "return", "pd", ".", "DataFrame", "(", ")", "if", "batch", "else", "assist", ".", "format_intraday", "(", "data", "=", "data", ",", "ticker", "=", "ticker", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
intraday
Bloomberg intraday bar data within market session Args: ticker: ticker dt: date session: examples include day_open_30, am_normal_30_30, day_close_30, allday_exact_0930_1000 **kwargs: ref: reference ticker or exchange for timezone keep_tz: if keep tz if reference ticker / exchange is given start_time: start time end_time: end time typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: pd.DataFrame
xbbg/blp.py
def intraday(ticker, dt, session='', **kwargs) -> pd.DataFrame: """ Bloomberg intraday bar data within market session Args: ticker: ticker dt: date session: examples include day_open_30, am_normal_30_30, day_close_30, allday_exact_0930_1000 **kwargs: ref: reference ticker or exchange for timezone keep_tz: if keep tz if reference ticker / exchange is given start_time: start time end_time: end time typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: pd.DataFrame """ from xbbg.core import intervals cur_data = bdib(ticker=ticker, dt=dt, typ=kwargs.get('typ', 'TRADE')) if cur_data.empty: return pd.DataFrame() fmt = '%H:%M:%S' ss = intervals.SessNA ref = kwargs.get('ref', None) exch = pd.Series() if ref is None else const.exch_info(ticker=ref) if session: ss = intervals.get_interval( ticker=kwargs.get('ref', ticker), session=session ) start_time = kwargs.get('start_time', None) end_time = kwargs.get('end_time', None) if ss != intervals.SessNA: start_time = pd.Timestamp(ss.start_time).strftime(fmt) end_time = pd.Timestamp(ss.end_time).strftime(fmt) if start_time and end_time: kw = dict(start_time=start_time, end_time=end_time) if not exch.empty: cur_tz = cur_data.index.tz res = cur_data.tz_convert(exch.tz).between_time(**kw) if kwargs.get('keep_tz', False): res = res.tz_convert(cur_tz) return pd.DataFrame(res) return pd.DataFrame(cur_data.between_time(**kw)) return cur_data
def intraday(ticker, dt, session='', **kwargs) -> pd.DataFrame: """ Bloomberg intraday bar data within market session Args: ticker: ticker dt: date session: examples include day_open_30, am_normal_30_30, day_close_30, allday_exact_0930_1000 **kwargs: ref: reference ticker or exchange for timezone keep_tz: if keep tz if reference ticker / exchange is given start_time: start time end_time: end time typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: pd.DataFrame """ from xbbg.core import intervals cur_data = bdib(ticker=ticker, dt=dt, typ=kwargs.get('typ', 'TRADE')) if cur_data.empty: return pd.DataFrame() fmt = '%H:%M:%S' ss = intervals.SessNA ref = kwargs.get('ref', None) exch = pd.Series() if ref is None else const.exch_info(ticker=ref) if session: ss = intervals.get_interval( ticker=kwargs.get('ref', ticker), session=session ) start_time = kwargs.get('start_time', None) end_time = kwargs.get('end_time', None) if ss != intervals.SessNA: start_time = pd.Timestamp(ss.start_time).strftime(fmt) end_time = pd.Timestamp(ss.end_time).strftime(fmt) if start_time and end_time: kw = dict(start_time=start_time, end_time=end_time) if not exch.empty: cur_tz = cur_data.index.tz res = cur_data.tz_convert(exch.tz).between_time(**kw) if kwargs.get('keep_tz', False): res = res.tz_convert(cur_tz) return pd.DataFrame(res) return pd.DataFrame(cur_data.between_time(**kw)) return cur_data
[ "Bloomberg", "intraday", "bar", "data", "within", "market", "session" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L350-L398
[ "def", "intraday", "(", "ticker", ",", "dt", ",", "session", "=", "''", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "from", "xbbg", ".", "core", "import", "intervals", "cur_data", "=", "bdib", "(", "ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "typ", "=", "kwargs", ".", "get", "(", "'typ'", ",", "'TRADE'", ")", ")", "if", "cur_data", ".", "empty", ":", "return", "pd", ".", "DataFrame", "(", ")", "fmt", "=", "'%H:%M:%S'", "ss", "=", "intervals", ".", "SessNA", "ref", "=", "kwargs", ".", "get", "(", "'ref'", ",", "None", ")", "exch", "=", "pd", ".", "Series", "(", ")", "if", "ref", "is", "None", "else", "const", ".", "exch_info", "(", "ticker", "=", "ref", ")", "if", "session", ":", "ss", "=", "intervals", ".", "get_interval", "(", "ticker", "=", "kwargs", ".", "get", "(", "'ref'", ",", "ticker", ")", ",", "session", "=", "session", ")", "start_time", "=", "kwargs", ".", "get", "(", "'start_time'", ",", "None", ")", "end_time", "=", "kwargs", ".", "get", "(", "'end_time'", ",", "None", ")", "if", "ss", "!=", "intervals", ".", "SessNA", ":", "start_time", "=", "pd", ".", "Timestamp", "(", "ss", ".", "start_time", ")", ".", "strftime", "(", "fmt", ")", "end_time", "=", "pd", ".", "Timestamp", "(", "ss", ".", "end_time", ")", ".", "strftime", "(", "fmt", ")", "if", "start_time", "and", "end_time", ":", "kw", "=", "dict", "(", "start_time", "=", "start_time", ",", "end_time", "=", "end_time", ")", "if", "not", "exch", ".", "empty", ":", "cur_tz", "=", "cur_data", ".", "index", ".", "tz", "res", "=", "cur_data", ".", "tz_convert", "(", "exch", ".", "tz", ")", ".", "between_time", "(", "*", "*", "kw", ")", "if", "kwargs", ".", "get", "(", "'keep_tz'", ",", "False", ")", ":", "res", "=", "res", ".", "tz_convert", "(", "cur_tz", ")", "return", "pd", ".", "DataFrame", "(", "res", ")", "return", "pd", ".", "DataFrame", "(", "cur_data", ".", "between_time", "(", "*", "*", "kw", ")", ")", "return", "cur_data" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
earning
Earning exposures by Geo or Products Args: ticker: ticker name by: [G(eo), P(roduct)] typ: type of earning, start with `PG_` in Bloomberg FLDS - default `Revenue` ccy: currency of earnings level: hierarchy level of earnings Returns: pd.DataFrame Examples: >>> data = earning('AMD US Equity', Eqy_Fund_Year=2017, Number_Of_Periods=1) >>> data.round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04
xbbg/blp.py
def earning( ticker, by='Geo', typ='Revenue', ccy=None, level=None, **kwargs ) -> pd.DataFrame: """ Earning exposures by Geo or Products Args: ticker: ticker name by: [G(eo), P(roduct)] typ: type of earning, start with `PG_` in Bloomberg FLDS - default `Revenue` ccy: currency of earnings level: hierarchy level of earnings Returns: pd.DataFrame Examples: >>> data = earning('AMD US Equity', Eqy_Fund_Year=2017, Number_Of_Periods=1) >>> data.round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04 """ ovrd = 'G' if by[0].upper() == 'G' else 'P' new_kw = dict(raw=True, Product_Geo_Override=ovrd) header = bds(tickers=ticker, flds='PG_Bulk_Header', **new_kw, **kwargs) if ccy: kwargs['Eqy_Fund_Crncy'] = ccy if level: kwargs['PG_Hierarchy_Level'] = level data = bds(tickers=ticker, flds=f'PG_{typ}', **new_kw, **kwargs) return assist.format_earning(data=data, header=header)
def earning( ticker, by='Geo', typ='Revenue', ccy=None, level=None, **kwargs ) -> pd.DataFrame: """ Earning exposures by Geo or Products Args: ticker: ticker name by: [G(eo), P(roduct)] typ: type of earning, start with `PG_` in Bloomberg FLDS - default `Revenue` ccy: currency of earnings level: hierarchy level of earnings Returns: pd.DataFrame Examples: >>> data = earning('AMD US Equity', Eqy_Fund_Year=2017, Number_Of_Periods=1) >>> data.round(2) level fy2017 fy2017_pct Asia-Pacific 1.0 3540.0 66.43    China 2.0 1747.0 49.35    Japan 2.0 1242.0 35.08    Singapore 2.0 551.0 15.56 United States 1.0 1364.0 25.60 Europe 1.0 263.0 4.94 Other Countries 1.0 162.0 3.04 """ ovrd = 'G' if by[0].upper() == 'G' else 'P' new_kw = dict(raw=True, Product_Geo_Override=ovrd) header = bds(tickers=ticker, flds='PG_Bulk_Header', **new_kw, **kwargs) if ccy: kwargs['Eqy_Fund_Crncy'] = ccy if level: kwargs['PG_Hierarchy_Level'] = level data = bds(tickers=ticker, flds=f'PG_{typ}', **new_kw, **kwargs) return assist.format_earning(data=data, header=header)
[ "Earning", "exposures", "by", "Geo", "or", "Products" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L402-L436
[ "def", "earning", "(", "ticker", ",", "by", "=", "'Geo'", ",", "typ", "=", "'Revenue'", ",", "ccy", "=", "None", ",", "level", "=", "None", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "ovrd", "=", "'G'", "if", "by", "[", "0", "]", ".", "upper", "(", ")", "==", "'G'", "else", "'P'", "new_kw", "=", "dict", "(", "raw", "=", "True", ",", "Product_Geo_Override", "=", "ovrd", ")", "header", "=", "bds", "(", "tickers", "=", "ticker", ",", "flds", "=", "'PG_Bulk_Header'", ",", "*", "*", "new_kw", ",", "*", "*", "kwargs", ")", "if", "ccy", ":", "kwargs", "[", "'Eqy_Fund_Crncy'", "]", "=", "ccy", "if", "level", ":", "kwargs", "[", "'PG_Hierarchy_Level'", "]", "=", "level", "data", "=", "bds", "(", "tickers", "=", "ticker", ",", "flds", "=", "f'PG_{typ}'", ",", "*", "*", "new_kw", ",", "*", "*", "kwargs", ")", "return", "assist", ".", "format_earning", "(", "data", "=", "data", ",", "header", "=", "header", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
dividend
Bloomberg dividend / split history Args: tickers: list of tickers typ: dividend adjustment type `all`: `DVD_Hist_All` `dvd`: `DVD_Hist` `split`: `Eqy_DVD_Hist_Splits` `gross`: `Eqy_DVD_Hist_Gross` `adjust`: `Eqy_DVD_Adjust_Fact` `adj_fund`: `Eqy_DVD_Adj_Fund` `with_amt`: `DVD_Hist_All_with_Amt_Status` `dvd_amt`: `DVD_Hist_with_Amt_Status` `gross_amt`: `DVD_Hist_Gross_with_Amt_Stat` `projected`: `BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann` start_date: start date end_date: end date **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = dividend( ... tickers=['C US Equity', 'NVDA US Equity', 'MS US Equity'], ... start_date='2018-01-01', end_date='2018-05-01' ... ) >>> res.index.name = None >>> res.loc[:, ['ex_date', 'rec_date', 'dvd_amt']].round(2) ex_date rec_date dvd_amt C US Equity 2018-02-02 2018-02-05 0.32 MS US Equity 2018-04-27 2018-04-30 0.25 MS US Equity 2018-01-30 2018-01-31 0.25 NVDA US Equity 2018-02-22 2018-02-23 0.15
xbbg/blp.py
def dividend( tickers, typ='all', start_date=None, end_date=None, **kwargs ) -> pd.DataFrame: """ Bloomberg dividend / split history Args: tickers: list of tickers typ: dividend adjustment type `all`: `DVD_Hist_All` `dvd`: `DVD_Hist` `split`: `Eqy_DVD_Hist_Splits` `gross`: `Eqy_DVD_Hist_Gross` `adjust`: `Eqy_DVD_Adjust_Fact` `adj_fund`: `Eqy_DVD_Adj_Fund` `with_amt`: `DVD_Hist_All_with_Amt_Status` `dvd_amt`: `DVD_Hist_with_Amt_Status` `gross_amt`: `DVD_Hist_Gross_with_Amt_Stat` `projected`: `BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann` start_date: start date end_date: end date **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = dividend( ... tickers=['C US Equity', 'NVDA US Equity', 'MS US Equity'], ... start_date='2018-01-01', end_date='2018-05-01' ... ) >>> res.index.name = None >>> res.loc[:, ['ex_date', 'rec_date', 'dvd_amt']].round(2) ex_date rec_date dvd_amt C US Equity 2018-02-02 2018-02-05 0.32 MS US Equity 2018-04-27 2018-04-30 0.25 MS US Equity 2018-01-30 2018-01-31 0.25 NVDA US Equity 2018-02-22 2018-02-23 0.15 """ if isinstance(tickers, str): tickers = [tickers] tickers = [t for t in tickers if ('Equity' in t) and ('=' not in t)] fld = { 'all': 'DVD_Hist_All', 'dvd': 'DVD_Hist', 'split': 'Eqy_DVD_Hist_Splits', 'gross': 'Eqy_DVD_Hist_Gross', 'adjust': 'Eqy_DVD_Adjust_Fact', 'adj_fund': 'Eqy_DVD_Adj_Fund', 'with_amt': 'DVD_Hist_All_with_Amt_Status', 'dvd_amt': 'DVD_Hist_with_Amt_Status', 'gross_amt': 'DVD_Hist_Gross_with_Amt_Stat', 'projected': 'BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann', }.get(typ, typ) if (fld == 'Eqy_DVD_Adjust_Fact') and ('Corporate_Actions_Filter' not in kwargs): kwargs['Corporate_Actions_Filter'] = 'NORMAL_CASH|ABNORMAL_CASH|CAPITAL_CHANGE' if fld in [ 'DVD_Hist_All', 'DVD_Hist', 'Eqy_DVD_Hist_Gross', 'DVD_Hist_All_with_Amt_Status', 'DVD_Hist_with_Amt_Status', ]: if start_date: kwargs['DVD_Start_Dt'] = utils.fmt_dt(start_date, fmt='%Y%m%d') if end_date: kwargs['DVD_End_Dt'] = utils.fmt_dt(end_date, fmt='%Y%m%d') kwargs['col_maps'] = { 'Declared Date': 'dec_date', 'Ex-Date': 'ex_date', 'Record Date': 'rec_date', 'Payable Date': 'pay_date', 'Dividend Amount': 'dvd_amt', 'Dividend Frequency': 'dvd_freq', 'Dividend Type': 'dvd_type', 'Amount Status': 'amt_status', 'Adjustment Date': 'adj_date', 'Adjustment Factor': 'adj_factor', 'Adjustment Factor Operator Type': 'adj_op', 'Adjustment Factor Flag': 'adj_flag', 'Amount Per Share': 'amt_ps', 'Projected/Confirmed': 'category', } return bds(tickers=tickers, flds=fld, raw=False, **kwargs)
def dividend( tickers, typ='all', start_date=None, end_date=None, **kwargs ) -> pd.DataFrame: """ Bloomberg dividend / split history Args: tickers: list of tickers typ: dividend adjustment type `all`: `DVD_Hist_All` `dvd`: `DVD_Hist` `split`: `Eqy_DVD_Hist_Splits` `gross`: `Eqy_DVD_Hist_Gross` `adjust`: `Eqy_DVD_Adjust_Fact` `adj_fund`: `Eqy_DVD_Adj_Fund` `with_amt`: `DVD_Hist_All_with_Amt_Status` `dvd_amt`: `DVD_Hist_with_Amt_Status` `gross_amt`: `DVD_Hist_Gross_with_Amt_Stat` `projected`: `BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann` start_date: start date end_date: end date **kwargs: overrides Returns: pd.DataFrame Examples: >>> res = dividend( ... tickers=['C US Equity', 'NVDA US Equity', 'MS US Equity'], ... start_date='2018-01-01', end_date='2018-05-01' ... ) >>> res.index.name = None >>> res.loc[:, ['ex_date', 'rec_date', 'dvd_amt']].round(2) ex_date rec_date dvd_amt C US Equity 2018-02-02 2018-02-05 0.32 MS US Equity 2018-04-27 2018-04-30 0.25 MS US Equity 2018-01-30 2018-01-31 0.25 NVDA US Equity 2018-02-22 2018-02-23 0.15 """ if isinstance(tickers, str): tickers = [tickers] tickers = [t for t in tickers if ('Equity' in t) and ('=' not in t)] fld = { 'all': 'DVD_Hist_All', 'dvd': 'DVD_Hist', 'split': 'Eqy_DVD_Hist_Splits', 'gross': 'Eqy_DVD_Hist_Gross', 'adjust': 'Eqy_DVD_Adjust_Fact', 'adj_fund': 'Eqy_DVD_Adj_Fund', 'with_amt': 'DVD_Hist_All_with_Amt_Status', 'dvd_amt': 'DVD_Hist_with_Amt_Status', 'gross_amt': 'DVD_Hist_Gross_with_Amt_Stat', 'projected': 'BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann', }.get(typ, typ) if (fld == 'Eqy_DVD_Adjust_Fact') and ('Corporate_Actions_Filter' not in kwargs): kwargs['Corporate_Actions_Filter'] = 'NORMAL_CASH|ABNORMAL_CASH|CAPITAL_CHANGE' if fld in [ 'DVD_Hist_All', 'DVD_Hist', 'Eqy_DVD_Hist_Gross', 'DVD_Hist_All_with_Amt_Status', 'DVD_Hist_with_Amt_Status', ]: if start_date: kwargs['DVD_Start_Dt'] = utils.fmt_dt(start_date, fmt='%Y%m%d') if end_date: kwargs['DVD_End_Dt'] = utils.fmt_dt(end_date, fmt='%Y%m%d') kwargs['col_maps'] = { 'Declared Date': 'dec_date', 'Ex-Date': 'ex_date', 'Record Date': 'rec_date', 'Payable Date': 'pay_date', 'Dividend Amount': 'dvd_amt', 'Dividend Frequency': 'dvd_freq', 'Dividend Type': 'dvd_type', 'Amount Status': 'amt_status', 'Adjustment Date': 'adj_date', 'Adjustment Factor': 'adj_factor', 'Adjustment Factor Operator Type': 'adj_op', 'Adjustment Factor Flag': 'adj_flag', 'Amount Per Share': 'amt_ps', 'Projected/Confirmed': 'category', } return bds(tickers=tickers, flds=fld, raw=False, **kwargs)
[ "Bloomberg", "dividend", "/", "split", "history" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L439-L512
[ "def", "dividend", "(", "tickers", ",", "typ", "=", "'all'", ",", "start_date", "=", "None", ",", "end_date", "=", "None", ",", "*", "*", "kwargs", ")", "->", "pd", ".", "DataFrame", ":", "if", "isinstance", "(", "tickers", ",", "str", ")", ":", "tickers", "=", "[", "tickers", "]", "tickers", "=", "[", "t", "for", "t", "in", "tickers", "if", "(", "'Equity'", "in", "t", ")", "and", "(", "'='", "not", "in", "t", ")", "]", "fld", "=", "{", "'all'", ":", "'DVD_Hist_All'", ",", "'dvd'", ":", "'DVD_Hist'", ",", "'split'", ":", "'Eqy_DVD_Hist_Splits'", ",", "'gross'", ":", "'Eqy_DVD_Hist_Gross'", ",", "'adjust'", ":", "'Eqy_DVD_Adjust_Fact'", ",", "'adj_fund'", ":", "'Eqy_DVD_Adj_Fund'", ",", "'with_amt'", ":", "'DVD_Hist_All_with_Amt_Status'", ",", "'dvd_amt'", ":", "'DVD_Hist_with_Amt_Status'", ",", "'gross_amt'", ":", "'DVD_Hist_Gross_with_Amt_Stat'", ",", "'projected'", ":", "'BDVD_Pr_Ex_Dts_DVD_Amts_w_Ann'", ",", "}", ".", "get", "(", "typ", ",", "typ", ")", "if", "(", "fld", "==", "'Eqy_DVD_Adjust_Fact'", ")", "and", "(", "'Corporate_Actions_Filter'", "not", "in", "kwargs", ")", ":", "kwargs", "[", "'Corporate_Actions_Filter'", "]", "=", "'NORMAL_CASH|ABNORMAL_CASH|CAPITAL_CHANGE'", "if", "fld", "in", "[", "'DVD_Hist_All'", ",", "'DVD_Hist'", ",", "'Eqy_DVD_Hist_Gross'", ",", "'DVD_Hist_All_with_Amt_Status'", ",", "'DVD_Hist_with_Amt_Status'", ",", "]", ":", "if", "start_date", ":", "kwargs", "[", "'DVD_Start_Dt'", "]", "=", "utils", ".", "fmt_dt", "(", "start_date", ",", "fmt", "=", "'%Y%m%d'", ")", "if", "end_date", ":", "kwargs", "[", "'DVD_End_Dt'", "]", "=", "utils", ".", "fmt_dt", "(", "end_date", ",", "fmt", "=", "'%Y%m%d'", ")", "kwargs", "[", "'col_maps'", "]", "=", "{", "'Declared Date'", ":", "'dec_date'", ",", "'Ex-Date'", ":", "'ex_date'", ",", "'Record Date'", ":", "'rec_date'", ",", "'Payable Date'", ":", "'pay_date'", ",", "'Dividend Amount'", ":", "'dvd_amt'", ",", "'Dividend Frequency'", ":", "'dvd_freq'", ",", "'Dividend Type'", ":", "'dvd_type'", ",", "'Amount Status'", ":", "'amt_status'", ",", "'Adjustment Date'", ":", "'adj_date'", ",", "'Adjustment Factor'", ":", "'adj_factor'", ",", "'Adjustment Factor Operator Type'", ":", "'adj_op'", ",", "'Adjustment Factor Flag'", ":", "'adj_flag'", ",", "'Amount Per Share'", ":", "'amt_ps'", ",", "'Projected/Confirmed'", ":", "'category'", ",", "}", "return", "bds", "(", "tickers", "=", "tickers", ",", "flds", "=", "fld", ",", "raw", "=", "False", ",", "*", "*", "kwargs", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
active_futures
Active futures contract Args: ticker: futures ticker, i.e., ESA Index, Z A Index, CLA Comdty, etc. dt: date Returns: str: ticker name
xbbg/blp.py
def active_futures(ticker: str, dt) -> str: """ Active futures contract Args: ticker: futures ticker, i.e., ESA Index, Z A Index, CLA Comdty, etc. dt: date Returns: str: ticker name """ t_info = ticker.split() prefix, asset = ' '.join(t_info[:-1]), t_info[-1] info = const.market_info(f'{prefix[:-1]}1 {asset}') f1, f2 = f'{prefix[:-1]}1 {asset}', f'{prefix[:-1]}2 {asset}' fut_2 = fut_ticker(gen_ticker=f2, dt=dt, freq=info['freq']) fut_1 = fut_ticker(gen_ticker=f1, dt=dt, freq=info['freq']) fut_tk = bdp(tickers=[fut_1, fut_2], flds='Last_Tradeable_Dt', cache=True) if pd.Timestamp(dt).month < pd.Timestamp(fut_tk.last_tradeable_dt[0]).month: return fut_1 d1 = bdib(ticker=f1, dt=dt) d2 = bdib(ticker=f2, dt=dt) return fut_1 if d1[f1].volume.sum() > d2[f2].volume.sum() else fut_2
def active_futures(ticker: str, dt) -> str: """ Active futures contract Args: ticker: futures ticker, i.e., ESA Index, Z A Index, CLA Comdty, etc. dt: date Returns: str: ticker name """ t_info = ticker.split() prefix, asset = ' '.join(t_info[:-1]), t_info[-1] info = const.market_info(f'{prefix[:-1]}1 {asset}') f1, f2 = f'{prefix[:-1]}1 {asset}', f'{prefix[:-1]}2 {asset}' fut_2 = fut_ticker(gen_ticker=f2, dt=dt, freq=info['freq']) fut_1 = fut_ticker(gen_ticker=f1, dt=dt, freq=info['freq']) fut_tk = bdp(tickers=[fut_1, fut_2], flds='Last_Tradeable_Dt', cache=True) if pd.Timestamp(dt).month < pd.Timestamp(fut_tk.last_tradeable_dt[0]).month: return fut_1 d1 = bdib(ticker=f1, dt=dt) d2 = bdib(ticker=f2, dt=dt) return fut_1 if d1[f1].volume.sum() > d2[f2].volume.sum() else fut_2
[ "Active", "futures", "contract" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L516-L542
[ "def", "active_futures", "(", "ticker", ":", "str", ",", "dt", ")", "->", "str", ":", "t_info", "=", "ticker", ".", "split", "(", ")", "prefix", ",", "asset", "=", "' '", ".", "join", "(", "t_info", "[", ":", "-", "1", "]", ")", ",", "t_info", "[", "-", "1", "]", "info", "=", "const", ".", "market_info", "(", "f'{prefix[:-1]}1 {asset}'", ")", "f1", ",", "f2", "=", "f'{prefix[:-1]}1 {asset}'", ",", "f'{prefix[:-1]}2 {asset}'", "fut_2", "=", "fut_ticker", "(", "gen_ticker", "=", "f2", ",", "dt", "=", "dt", ",", "freq", "=", "info", "[", "'freq'", "]", ")", "fut_1", "=", "fut_ticker", "(", "gen_ticker", "=", "f1", ",", "dt", "=", "dt", ",", "freq", "=", "info", "[", "'freq'", "]", ")", "fut_tk", "=", "bdp", "(", "tickers", "=", "[", "fut_1", ",", "fut_2", "]", ",", "flds", "=", "'Last_Tradeable_Dt'", ",", "cache", "=", "True", ")", "if", "pd", ".", "Timestamp", "(", "dt", ")", ".", "month", "<", "pd", ".", "Timestamp", "(", "fut_tk", ".", "last_tradeable_dt", "[", "0", "]", ")", ".", "month", ":", "return", "fut_1", "d1", "=", "bdib", "(", "ticker", "=", "f1", ",", "dt", "=", "dt", ")", "d2", "=", "bdib", "(", "ticker", "=", "f2", ",", "dt", "=", "dt", ")", "return", "fut_1", "if", "d1", "[", "f1", "]", ".", "volume", ".", "sum", "(", ")", ">", "d2", "[", "f2", "]", ".", "volume", ".", "sum", "(", ")", "else", "fut_2" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
fut_ticker
Get proper ticker from generic ticker Args: gen_ticker: generic ticker dt: date freq: futures contract frequency log: level of logs Returns: str: exact futures ticker
xbbg/blp.py
def fut_ticker(gen_ticker: str, dt, freq: str, log=logs.LOG_LEVEL) -> str: """ Get proper ticker from generic ticker Args: gen_ticker: generic ticker dt: date freq: futures contract frequency log: level of logs Returns: str: exact futures ticker """ logger = logs.get_logger(fut_ticker, level=log) dt = pd.Timestamp(dt) t_info = gen_ticker.split() asset = t_info[-1] if asset in ['Index', 'Curncy', 'Comdty']: ticker = ' '.join(t_info[:-1]) prefix, idx, postfix = ticker[:-1], int(ticker[-1]) - 1, asset elif asset == 'Equity': ticker = t_info[0] prefix, idx, postfix = ticker[:-1], int(ticker[-1]) - 1, ' '.join(t_info[1:]) else: logger.error(f'unkonwn asset type for ticker: {gen_ticker}') return '' month_ext = 4 if asset == 'Comdty' else 2 months = pd.date_range(start=dt, periods=max(idx + month_ext, 3), freq=freq) logger.debug(f'pulling expiry dates for months: {months}') def to_fut(month): return prefix + const.Futures[month.strftime('%b')] + \ month.strftime('%y')[-1] + ' ' + postfix fut = [to_fut(m) for m in months] logger.debug(f'trying futures: {fut}') # noinspection PyBroadException try: fut_matu = bdp(tickers=fut, flds='last_tradeable_dt', cache=True) except Exception as e1: logger.error(f'error downloading futures contracts (1st trial) {e1}:\n{fut}') # noinspection PyBroadException try: fut = fut[:-1] logger.debug(f'trying futures (2nd trial): {fut}') fut_matu = bdp(tickers=fut, flds='last_tradeable_dt', cache=True) except Exception as e2: logger.error(f'error downloading futures contracts (2nd trial) {e2}:\n{fut}') return '' sub_fut = fut_matu[pd.DatetimeIndex(fut_matu.last_tradeable_dt) > dt] logger.debug(f'futures full chain:\n{fut_matu.to_string()}') logger.debug(f'getting index {idx} from:\n{sub_fut.to_string()}') return sub_fut.index.values[idx]
def fut_ticker(gen_ticker: str, dt, freq: str, log=logs.LOG_LEVEL) -> str: """ Get proper ticker from generic ticker Args: gen_ticker: generic ticker dt: date freq: futures contract frequency log: level of logs Returns: str: exact futures ticker """ logger = logs.get_logger(fut_ticker, level=log) dt = pd.Timestamp(dt) t_info = gen_ticker.split() asset = t_info[-1] if asset in ['Index', 'Curncy', 'Comdty']: ticker = ' '.join(t_info[:-1]) prefix, idx, postfix = ticker[:-1], int(ticker[-1]) - 1, asset elif asset == 'Equity': ticker = t_info[0] prefix, idx, postfix = ticker[:-1], int(ticker[-1]) - 1, ' '.join(t_info[1:]) else: logger.error(f'unkonwn asset type for ticker: {gen_ticker}') return '' month_ext = 4 if asset == 'Comdty' else 2 months = pd.date_range(start=dt, periods=max(idx + month_ext, 3), freq=freq) logger.debug(f'pulling expiry dates for months: {months}') def to_fut(month): return prefix + const.Futures[month.strftime('%b')] + \ month.strftime('%y')[-1] + ' ' + postfix fut = [to_fut(m) for m in months] logger.debug(f'trying futures: {fut}') # noinspection PyBroadException try: fut_matu = bdp(tickers=fut, flds='last_tradeable_dt', cache=True) except Exception as e1: logger.error(f'error downloading futures contracts (1st trial) {e1}:\n{fut}') # noinspection PyBroadException try: fut = fut[:-1] logger.debug(f'trying futures (2nd trial): {fut}') fut_matu = bdp(tickers=fut, flds='last_tradeable_dt', cache=True) except Exception as e2: logger.error(f'error downloading futures contracts (2nd trial) {e2}:\n{fut}') return '' sub_fut = fut_matu[pd.DatetimeIndex(fut_matu.last_tradeable_dt) > dt] logger.debug(f'futures full chain:\n{fut_matu.to_string()}') logger.debug(f'getting index {idx} from:\n{sub_fut.to_string()}') return sub_fut.index.values[idx]
[ "Get", "proper", "ticker", "from", "generic", "ticker" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L546-L603
[ "def", "fut_ticker", "(", "gen_ticker", ":", "str", ",", "dt", ",", "freq", ":", "str", ",", "log", "=", "logs", ".", "LOG_LEVEL", ")", "->", "str", ":", "logger", "=", "logs", ".", "get_logger", "(", "fut_ticker", ",", "level", "=", "log", ")", "dt", "=", "pd", ".", "Timestamp", "(", "dt", ")", "t_info", "=", "gen_ticker", ".", "split", "(", ")", "asset", "=", "t_info", "[", "-", "1", "]", "if", "asset", "in", "[", "'Index'", ",", "'Curncy'", ",", "'Comdty'", "]", ":", "ticker", "=", "' '", ".", "join", "(", "t_info", "[", ":", "-", "1", "]", ")", "prefix", ",", "idx", ",", "postfix", "=", "ticker", "[", ":", "-", "1", "]", ",", "int", "(", "ticker", "[", "-", "1", "]", ")", "-", "1", ",", "asset", "elif", "asset", "==", "'Equity'", ":", "ticker", "=", "t_info", "[", "0", "]", "prefix", ",", "idx", ",", "postfix", "=", "ticker", "[", ":", "-", "1", "]", ",", "int", "(", "ticker", "[", "-", "1", "]", ")", "-", "1", ",", "' '", ".", "join", "(", "t_info", "[", "1", ":", "]", ")", "else", ":", "logger", ".", "error", "(", "f'unkonwn asset type for ticker: {gen_ticker}'", ")", "return", "''", "month_ext", "=", "4", "if", "asset", "==", "'Comdty'", "else", "2", "months", "=", "pd", ".", "date_range", "(", "start", "=", "dt", ",", "periods", "=", "max", "(", "idx", "+", "month_ext", ",", "3", ")", ",", "freq", "=", "freq", ")", "logger", ".", "debug", "(", "f'pulling expiry dates for months: {months}'", ")", "def", "to_fut", "(", "month", ")", ":", "return", "prefix", "+", "const", ".", "Futures", "[", "month", ".", "strftime", "(", "'%b'", ")", "]", "+", "month", ".", "strftime", "(", "'%y'", ")", "[", "-", "1", "]", "+", "' '", "+", "postfix", "fut", "=", "[", "to_fut", "(", "m", ")", "for", "m", "in", "months", "]", "logger", ".", "debug", "(", "f'trying futures: {fut}'", ")", "# noinspection PyBroadException", "try", ":", "fut_matu", "=", "bdp", "(", "tickers", "=", "fut", ",", "flds", "=", "'last_tradeable_dt'", ",", "cache", "=", "True", ")", "except", "Exception", "as", "e1", ":", "logger", ".", "error", "(", "f'error downloading futures contracts (1st trial) {e1}:\\n{fut}'", ")", "# noinspection PyBroadException", "try", ":", "fut", "=", "fut", "[", ":", "-", "1", "]", "logger", ".", "debug", "(", "f'trying futures (2nd trial): {fut}'", ")", "fut_matu", "=", "bdp", "(", "tickers", "=", "fut", ",", "flds", "=", "'last_tradeable_dt'", ",", "cache", "=", "True", ")", "except", "Exception", "as", "e2", ":", "logger", ".", "error", "(", "f'error downloading futures contracts (2nd trial) {e2}:\\n{fut}'", ")", "return", "''", "sub_fut", "=", "fut_matu", "[", "pd", ".", "DatetimeIndex", "(", "fut_matu", ".", "last_tradeable_dt", ")", ">", "dt", "]", "logger", ".", "debug", "(", "f'futures full chain:\\n{fut_matu.to_string()}'", ")", "logger", ".", "debug", "(", "f'getting index {idx} from:\\n{sub_fut.to_string()}'", ")", "return", "sub_fut", ".", "index", ".", "values", "[", "idx", "]" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
check_hours
Check exchange hours vs local hours Args: tickers: list of tickers tz_exch: exchange timezone tz_loc: local timezone Returns: Local and exchange hours
xbbg/blp.py
def check_hours(tickers, tz_exch, tz_loc=DEFAULT_TZ) -> pd.DataFrame: """ Check exchange hours vs local hours Args: tickers: list of tickers tz_exch: exchange timezone tz_loc: local timezone Returns: Local and exchange hours """ cols = ['Trading_Day_Start_Time_EOD', 'Trading_Day_End_Time_EOD'] con, _ = create_connection() hours = con.ref(tickers=tickers, flds=cols) cur_dt = pd.Timestamp('today').strftime('%Y-%m-%d ') hours.loc[:, 'local'] = hours.value.astype(str).str[:-3] hours.loc[:, 'exch'] = pd.DatetimeIndex( cur_dt + hours.value.astype(str) ).tz_localize(tz_loc).tz_convert(tz_exch).strftime('%H:%M') hours = pd.concat([ hours.set_index(['ticker', 'field']).exch.unstack().loc[:, cols], hours.set_index(['ticker', 'field']).local.unstack().loc[:, cols], ], axis=1) hours.columns = ['Exch_Start', 'Exch_End', 'Local_Start', 'Local_End'] return hours
def check_hours(tickers, tz_exch, tz_loc=DEFAULT_TZ) -> pd.DataFrame: """ Check exchange hours vs local hours Args: tickers: list of tickers tz_exch: exchange timezone tz_loc: local timezone Returns: Local and exchange hours """ cols = ['Trading_Day_Start_Time_EOD', 'Trading_Day_End_Time_EOD'] con, _ = create_connection() hours = con.ref(tickers=tickers, flds=cols) cur_dt = pd.Timestamp('today').strftime('%Y-%m-%d ') hours.loc[:, 'local'] = hours.value.astype(str).str[:-3] hours.loc[:, 'exch'] = pd.DatetimeIndex( cur_dt + hours.value.astype(str) ).tz_localize(tz_loc).tz_convert(tz_exch).strftime('%H:%M') hours = pd.concat([ hours.set_index(['ticker', 'field']).exch.unstack().loc[:, cols], hours.set_index(['ticker', 'field']).local.unstack().loc[:, cols], ], axis=1) hours.columns = ['Exch_Start', 'Exch_End', 'Local_Start', 'Local_End'] return hours
[ "Check", "exchange", "hours", "vs", "local", "hours" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/blp.py#L607-L634
[ "def", "check_hours", "(", "tickers", ",", "tz_exch", ",", "tz_loc", "=", "DEFAULT_TZ", ")", "->", "pd", ".", "DataFrame", ":", "cols", "=", "[", "'Trading_Day_Start_Time_EOD'", ",", "'Trading_Day_End_Time_EOD'", "]", "con", ",", "_", "=", "create_connection", "(", ")", "hours", "=", "con", ".", "ref", "(", "tickers", "=", "tickers", ",", "flds", "=", "cols", ")", "cur_dt", "=", "pd", ".", "Timestamp", "(", "'today'", ")", ".", "strftime", "(", "'%Y-%m-%d '", ")", "hours", ".", "loc", "[", ":", ",", "'local'", "]", "=", "hours", ".", "value", ".", "astype", "(", "str", ")", ".", "str", "[", ":", "-", "3", "]", "hours", ".", "loc", "[", ":", ",", "'exch'", "]", "=", "pd", ".", "DatetimeIndex", "(", "cur_dt", "+", "hours", ".", "value", ".", "astype", "(", "str", ")", ")", ".", "tz_localize", "(", "tz_loc", ")", ".", "tz_convert", "(", "tz_exch", ")", ".", "strftime", "(", "'%H:%M'", ")", "hours", "=", "pd", ".", "concat", "(", "[", "hours", ".", "set_index", "(", "[", "'ticker'", ",", "'field'", "]", ")", ".", "exch", ".", "unstack", "(", ")", ".", "loc", "[", ":", ",", "cols", "]", ",", "hours", ".", "set_index", "(", "[", "'ticker'", ",", "'field'", "]", ")", ".", "local", ".", "unstack", "(", ")", ".", "loc", "[", ":", ",", "cols", "]", ",", "]", ",", "axis", "=", "1", ")", "hours", ".", "columns", "=", "[", "'Exch_Start'", ",", "'Exch_End'", ",", "'Local_Start'", ",", "'Local_End'", "]", "return", "hours" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
hist_file
Data file location for Bloomberg historical data Args: ticker: ticker name dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: file location Examples: >>> os.environ['BBG_ROOT'] = '' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') '/data/bbg/Index/ES1 Index/TRADE/2018-08-01.parq'
xbbg/io/storage.py
def hist_file(ticker: str, dt, typ='TRADE') -> str: """ Data file location for Bloomberg historical data Args: ticker: ticker name dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: file location Examples: >>> os.environ['BBG_ROOT'] = '' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') '/data/bbg/Index/ES1 Index/TRADE/2018-08-01.parq' """ data_path = os.environ.get(assist.BBG_ROOT, '').replace('\\', '/') if not data_path: return '' asset = ticker.split()[-1] proper_ticker = ticker.replace('/', '_') cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') return f'{data_path}/{asset}/{proper_ticker}/{typ}/{cur_dt}.parq'
def hist_file(ticker: str, dt, typ='TRADE') -> str: """ Data file location for Bloomberg historical data Args: ticker: ticker name dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Returns: file location Examples: >>> os.environ['BBG_ROOT'] = '' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> hist_file(ticker='ES1 Index', dt='2018-08-01') '/data/bbg/Index/ES1 Index/TRADE/2018-08-01.parq' """ data_path = os.environ.get(assist.BBG_ROOT, '').replace('\\', '/') if not data_path: return '' asset = ticker.split()[-1] proper_ticker = ticker.replace('/', '_') cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') return f'{data_path}/{asset}/{proper_ticker}/{typ}/{cur_dt}.parq'
[ "Data", "file", "location", "for", "Bloomberg", "historical", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/storage.py#L11-L36
[ "def", "hist_file", "(", "ticker", ":", "str", ",", "dt", ",", "typ", "=", "'TRADE'", ")", "->", "str", ":", "data_path", "=", "os", ".", "environ", ".", "get", "(", "assist", ".", "BBG_ROOT", ",", "''", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "not", "data_path", ":", "return", "''", "asset", "=", "ticker", ".", "split", "(", ")", "[", "-", "1", "]", "proper_ticker", "=", "ticker", ".", "replace", "(", "'/'", ",", "'_'", ")", "cur_dt", "=", "pd", ".", "Timestamp", "(", "dt", ")", ".", "strftime", "(", "'%Y-%m-%d'", ")", "return", "f'{data_path}/{asset}/{proper_ticker}/{typ}/{cur_dt}.parq'" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
ref_file
Data file location for Bloomberg reference data Args: ticker: ticker name fld: field has_date: whether add current date to data file cache: if has_date is True, whether to load file from latest cached ext: file extension **kwargs: other overrides passed to ref function Returns: file location Examples: >>> import shutil >>> >>> os.environ['BBG_ROOT'] = '' >>> ref_file('BLT LN Equity', fld='Crncy') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> ref_file('BLT LN Equity', fld='Crncy', cache=True) '/data/bbg/Equity/BLT LN Equity/Crncy/ovrd=None.parq' >>> ref_file('BLT LN Equity', fld='Crncy') '' >>> cur_dt = utils.cur_time(tz=utils.DEFAULT_TZ) >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, cache=True, ... ).replace(cur_dt, '[cur_date]') '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], ovrd=None.parq' >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, ... cache=True, DVD_Start_Dt='20180101', ... ).replace(cur_dt, '[cur_date]')[:-5] '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], DVD_Start_Dt=20180101' >>> sample = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> root_path = 'xbbg/tests/data' >>> sub_path = f'{root_path}/Equity/AAPL US Equity/DVD_Hist_All' >>> os.environ['BBG_ROOT'] = root_path >>> for tmp_file in files.all_files(sub_path): os.remove(tmp_file) >>> files.create_folder(sub_path) >>> sample in shutil.copy(f'{root_path}/{sample}', sub_path) True >>> new_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> new_file.split('/')[-1] == f'asof={cur_dt}, DVD_Start_Dt=20180101.pkl' True >>> old_file = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> old_full = '/'.join(new_file.split('/')[:-1] + [old_file]) >>> updated_file = old_full.replace('2018-11-02', cur_dt) >>> updated_file in shutil.copy(old_full, updated_file) True >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file False >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... DVD_End_Dt='20180501', has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file True
xbbg/io/storage.py
def ref_file( ticker: str, fld: str, has_date=False, cache=False, ext='parq', **kwargs ) -> str: """ Data file location for Bloomberg reference data Args: ticker: ticker name fld: field has_date: whether add current date to data file cache: if has_date is True, whether to load file from latest cached ext: file extension **kwargs: other overrides passed to ref function Returns: file location Examples: >>> import shutil >>> >>> os.environ['BBG_ROOT'] = '' >>> ref_file('BLT LN Equity', fld='Crncy') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> ref_file('BLT LN Equity', fld='Crncy', cache=True) '/data/bbg/Equity/BLT LN Equity/Crncy/ovrd=None.parq' >>> ref_file('BLT LN Equity', fld='Crncy') '' >>> cur_dt = utils.cur_time(tz=utils.DEFAULT_TZ) >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, cache=True, ... ).replace(cur_dt, '[cur_date]') '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], ovrd=None.parq' >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, ... cache=True, DVD_Start_Dt='20180101', ... ).replace(cur_dt, '[cur_date]')[:-5] '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], DVD_Start_Dt=20180101' >>> sample = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> root_path = 'xbbg/tests/data' >>> sub_path = f'{root_path}/Equity/AAPL US Equity/DVD_Hist_All' >>> os.environ['BBG_ROOT'] = root_path >>> for tmp_file in files.all_files(sub_path): os.remove(tmp_file) >>> files.create_folder(sub_path) >>> sample in shutil.copy(f'{root_path}/{sample}', sub_path) True >>> new_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> new_file.split('/')[-1] == f'asof={cur_dt}, DVD_Start_Dt=20180101.pkl' True >>> old_file = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> old_full = '/'.join(new_file.split('/')[:-1] + [old_file]) >>> updated_file = old_full.replace('2018-11-02', cur_dt) >>> updated_file in shutil.copy(old_full, updated_file) True >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file False >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... DVD_End_Dt='20180501', has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file True """ data_path = os.environ.get(assist.BBG_ROOT, '').replace('\\', '/') if (not data_path) or (not cache): return '' proper_ticker = ticker.replace('/', '_') cache_days = kwargs.pop('cache_days', 10) root = f'{data_path}/{ticker.split()[-1]}/{proper_ticker}/{fld}' if len(kwargs) > 0: info = utils.to_str(kwargs)[1:-1].replace('|', '_') else: info = 'ovrd=None' # Check date info if has_date: cur_dt = utils.cur_time() missing = f'{root}/asof={cur_dt}, {info}.{ext}' to_find = re.compile(rf'{root}/asof=(.*), {info}\.pkl') cur_files = list(filter(to_find.match, sorted( files.all_files(path_name=root, keyword=info, ext=ext) ))) if len(cur_files) > 0: upd_dt = to_find.match(cur_files[-1]).group(1) diff = pd.Timestamp('today') - pd.Timestamp(upd_dt) if diff >= pd.Timedelta(days=cache_days): return missing return sorted(cur_files)[-1] else: return missing else: return f'{root}/{info}.{ext}'
def ref_file( ticker: str, fld: str, has_date=False, cache=False, ext='parq', **kwargs ) -> str: """ Data file location for Bloomberg reference data Args: ticker: ticker name fld: field has_date: whether add current date to data file cache: if has_date is True, whether to load file from latest cached ext: file extension **kwargs: other overrides passed to ref function Returns: file location Examples: >>> import shutil >>> >>> os.environ['BBG_ROOT'] = '' >>> ref_file('BLT LN Equity', fld='Crncy') == '' True >>> os.environ['BBG_ROOT'] = '/data/bbg' >>> ref_file('BLT LN Equity', fld='Crncy', cache=True) '/data/bbg/Equity/BLT LN Equity/Crncy/ovrd=None.parq' >>> ref_file('BLT LN Equity', fld='Crncy') '' >>> cur_dt = utils.cur_time(tz=utils.DEFAULT_TZ) >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, cache=True, ... ).replace(cur_dt, '[cur_date]') '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], ovrd=None.parq' >>> ref_file( ... 'BLT LN Equity', fld='DVD_Hist_All', has_date=True, ... cache=True, DVD_Start_Dt='20180101', ... ).replace(cur_dt, '[cur_date]')[:-5] '/data/bbg/Equity/BLT LN Equity/DVD_Hist_All/asof=[cur_date], DVD_Start_Dt=20180101' >>> sample = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> root_path = 'xbbg/tests/data' >>> sub_path = f'{root_path}/Equity/AAPL US Equity/DVD_Hist_All' >>> os.environ['BBG_ROOT'] = root_path >>> for tmp_file in files.all_files(sub_path): os.remove(tmp_file) >>> files.create_folder(sub_path) >>> sample in shutil.copy(f'{root_path}/{sample}', sub_path) True >>> new_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> new_file.split('/')[-1] == f'asof={cur_dt}, DVD_Start_Dt=20180101.pkl' True >>> old_file = 'asof=2018-11-02, DVD_Start_Dt=20180101, DVD_End_Dt=20180501.pkl' >>> old_full = '/'.join(new_file.split('/')[:-1] + [old_file]) >>> updated_file = old_full.replace('2018-11-02', cur_dt) >>> updated_file in shutil.copy(old_full, updated_file) True >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file False >>> exist_file = ref_file( ... 'AAPL US Equity', 'DVD_Hist_All', DVD_Start_Dt='20180101', ... DVD_End_Dt='20180501', has_date=True, cache=True, ext='pkl' ... ) >>> exist_file == updated_file True """ data_path = os.environ.get(assist.BBG_ROOT, '').replace('\\', '/') if (not data_path) or (not cache): return '' proper_ticker = ticker.replace('/', '_') cache_days = kwargs.pop('cache_days', 10) root = f'{data_path}/{ticker.split()[-1]}/{proper_ticker}/{fld}' if len(kwargs) > 0: info = utils.to_str(kwargs)[1:-1].replace('|', '_') else: info = 'ovrd=None' # Check date info if has_date: cur_dt = utils.cur_time() missing = f'{root}/asof={cur_dt}, {info}.{ext}' to_find = re.compile(rf'{root}/asof=(.*), {info}\.pkl') cur_files = list(filter(to_find.match, sorted( files.all_files(path_name=root, keyword=info, ext=ext) ))) if len(cur_files) > 0: upd_dt = to_find.match(cur_files[-1]).group(1) diff = pd.Timestamp('today') - pd.Timestamp(upd_dt) if diff >= pd.Timedelta(days=cache_days): return missing return sorted(cur_files)[-1] else: return missing else: return f'{root}/{info}.{ext}'
[ "Data", "file", "location", "for", "Bloomberg", "reference", "data" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/storage.py#L39-L134
[ "def", "ref_file", "(", "ticker", ":", "str", ",", "fld", ":", "str", ",", "has_date", "=", "False", ",", "cache", "=", "False", ",", "ext", "=", "'parq'", ",", "*", "*", "kwargs", ")", "->", "str", ":", "data_path", "=", "os", ".", "environ", ".", "get", "(", "assist", ".", "BBG_ROOT", ",", "''", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "(", "not", "data_path", ")", "or", "(", "not", "cache", ")", ":", "return", "''", "proper_ticker", "=", "ticker", ".", "replace", "(", "'/'", ",", "'_'", ")", "cache_days", "=", "kwargs", ".", "pop", "(", "'cache_days'", ",", "10", ")", "root", "=", "f'{data_path}/{ticker.split()[-1]}/{proper_ticker}/{fld}'", "if", "len", "(", "kwargs", ")", ">", "0", ":", "info", "=", "utils", ".", "to_str", "(", "kwargs", ")", "[", "1", ":", "-", "1", "]", ".", "replace", "(", "'|'", ",", "'_'", ")", "else", ":", "info", "=", "'ovrd=None'", "# Check date info", "if", "has_date", ":", "cur_dt", "=", "utils", ".", "cur_time", "(", ")", "missing", "=", "f'{root}/asof={cur_dt}, {info}.{ext}'", "to_find", "=", "re", ".", "compile", "(", "rf'{root}/asof=(.*), {info}\\.pkl'", ")", "cur_files", "=", "list", "(", "filter", "(", "to_find", ".", "match", ",", "sorted", "(", "files", ".", "all_files", "(", "path_name", "=", "root", ",", "keyword", "=", "info", ",", "ext", "=", "ext", ")", ")", ")", ")", "if", "len", "(", "cur_files", ")", ">", "0", ":", "upd_dt", "=", "to_find", ".", "match", "(", "cur_files", "[", "-", "1", "]", ")", ".", "group", "(", "1", ")", "diff", "=", "pd", ".", "Timestamp", "(", "'today'", ")", "-", "pd", ".", "Timestamp", "(", "upd_dt", ")", "if", "diff", ">=", "pd", ".", "Timedelta", "(", "days", "=", "cache_days", ")", ":", "return", "missing", "return", "sorted", "(", "cur_files", ")", "[", "-", "1", "]", "else", ":", "return", "missing", "else", ":", "return", "f'{root}/{info}.{ext}'" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
save_intraday
Check whether data is done for the day and save Args: data: data ticker: ticker dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Examples: >>> os.environ['BBG_ROOT'] = 'xbbg/tests/data' >>> sample = pd.read_parquet('xbbg/tests/data/aapl.parq') >>> save_intraday(sample, 'AAPL US Equity', '2018-11-02') >>> # Invalid exchange >>> save_intraday(sample, 'AAPL XX Equity', '2018-11-02') >>> # Invalid empty data >>> save_intraday(pd.DataFrame(), 'AAPL US Equity', '2018-11-02') >>> # Invalid date - too close >>> cur_dt = utils.cur_time() >>> save_intraday(sample, 'AAPL US Equity', cur_dt)
xbbg/io/storage.py
def save_intraday(data: pd.DataFrame, ticker: str, dt, typ='TRADE'): """ Check whether data is done for the day and save Args: data: data ticker: ticker dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Examples: >>> os.environ['BBG_ROOT'] = 'xbbg/tests/data' >>> sample = pd.read_parquet('xbbg/tests/data/aapl.parq') >>> save_intraday(sample, 'AAPL US Equity', '2018-11-02') >>> # Invalid exchange >>> save_intraday(sample, 'AAPL XX Equity', '2018-11-02') >>> # Invalid empty data >>> save_intraday(pd.DataFrame(), 'AAPL US Equity', '2018-11-02') >>> # Invalid date - too close >>> cur_dt = utils.cur_time() >>> save_intraday(sample, 'AAPL US Equity', cur_dt) """ cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') logger = logs.get_logger(save_intraday, level='debug') info = f'{ticker} / {cur_dt} / {typ}' data_file = hist_file(ticker=ticker, dt=dt, typ=typ) if not data_file: return if data.empty: logger.warning(f'data is empty for {info} ...') return exch = const.exch_info(ticker=ticker) if exch.empty: return end_time = pd.Timestamp( const.market_timing(ticker=ticker, dt=dt, timing='FINISHED') ).tz_localize(exch.tz) now = pd.Timestamp('now', tz=exch.tz) - pd.Timedelta('1H') if end_time > now: logger.debug(f'skip saving cause market close ({end_time}) < now - 1H ({now}) ...') return logger.info(f'saving data to {data_file} ...') files.create_folder(data_file, is_file=True) data.to_parquet(data_file)
def save_intraday(data: pd.DataFrame, ticker: str, dt, typ='TRADE'): """ Check whether data is done for the day and save Args: data: data ticker: ticker dt: date typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK] Examples: >>> os.environ['BBG_ROOT'] = 'xbbg/tests/data' >>> sample = pd.read_parquet('xbbg/tests/data/aapl.parq') >>> save_intraday(sample, 'AAPL US Equity', '2018-11-02') >>> # Invalid exchange >>> save_intraday(sample, 'AAPL XX Equity', '2018-11-02') >>> # Invalid empty data >>> save_intraday(pd.DataFrame(), 'AAPL US Equity', '2018-11-02') >>> # Invalid date - too close >>> cur_dt = utils.cur_time() >>> save_intraday(sample, 'AAPL US Equity', cur_dt) """ cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d') logger = logs.get_logger(save_intraday, level='debug') info = f'{ticker} / {cur_dt} / {typ}' data_file = hist_file(ticker=ticker, dt=dt, typ=typ) if not data_file: return if data.empty: logger.warning(f'data is empty for {info} ...') return exch = const.exch_info(ticker=ticker) if exch.empty: return end_time = pd.Timestamp( const.market_timing(ticker=ticker, dt=dt, timing='FINISHED') ).tz_localize(exch.tz) now = pd.Timestamp('now', tz=exch.tz) - pd.Timedelta('1H') if end_time > now: logger.debug(f'skip saving cause market close ({end_time}) < now - 1H ({now}) ...') return logger.info(f'saving data to {data_file} ...') files.create_folder(data_file, is_file=True) data.to_parquet(data_file)
[ "Check", "whether", "data", "is", "done", "for", "the", "day", "and", "save" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/storage.py#L137-L183
[ "def", "save_intraday", "(", "data", ":", "pd", ".", "DataFrame", ",", "ticker", ":", "str", ",", "dt", ",", "typ", "=", "'TRADE'", ")", ":", "cur_dt", "=", "pd", ".", "Timestamp", "(", "dt", ")", ".", "strftime", "(", "'%Y-%m-%d'", ")", "logger", "=", "logs", ".", "get_logger", "(", "save_intraday", ",", "level", "=", "'debug'", ")", "info", "=", "f'{ticker} / {cur_dt} / {typ}'", "data_file", "=", "hist_file", "(", "ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "typ", "=", "typ", ")", "if", "not", "data_file", ":", "return", "if", "data", ".", "empty", ":", "logger", ".", "warning", "(", "f'data is empty for {info} ...'", ")", "return", "exch", "=", "const", ".", "exch_info", "(", "ticker", "=", "ticker", ")", "if", "exch", ".", "empty", ":", "return", "end_time", "=", "pd", ".", "Timestamp", "(", "const", ".", "market_timing", "(", "ticker", "=", "ticker", ",", "dt", "=", "dt", ",", "timing", "=", "'FINISHED'", ")", ")", ".", "tz_localize", "(", "exch", ".", "tz", ")", "now", "=", "pd", ".", "Timestamp", "(", "'now'", ",", "tz", "=", "exch", ".", "tz", ")", "-", "pd", ".", "Timedelta", "(", "'1H'", ")", "if", "end_time", ">", "now", ":", "logger", ".", "debug", "(", "f'skip saving cause market close ({end_time}) < now - 1H ({now}) ...'", ")", "return", "logger", ".", "info", "(", "f'saving data to {data_file} ...'", ")", "files", ".", "create_folder", "(", "data_file", ",", "is_file", "=", "True", ")", "data", ".", "to_parquet", "(", "data_file", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
exch_info
Exchange info for given ticker Args: ticker: ticker or exchange Returns: pd.Series Examples: >>> exch_info('SPY US Equity') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object >>> exch_info('ES1 Index') tz America/New_York allday [18:00, 17:00] day [08:00, 17:00] dtype: object >>> exch_info('Z 1 Index') tz Europe/London allday [01:00, 21:00] day [01:00, 21:00] dtype: object >>> exch_info('TESTTICKER Corp').empty True >>> exch_info('US') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object
xbbg/const.py
def exch_info(ticker: str) -> pd.Series: """ Exchange info for given ticker Args: ticker: ticker or exchange Returns: pd.Series Examples: >>> exch_info('SPY US Equity') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object >>> exch_info('ES1 Index') tz America/New_York allday [18:00, 17:00] day [08:00, 17:00] dtype: object >>> exch_info('Z 1 Index') tz Europe/London allday [01:00, 21:00] day [01:00, 21:00] dtype: object >>> exch_info('TESTTICKER Corp').empty True >>> exch_info('US') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object """ logger = logs.get_logger(exch_info, level='debug') if ' ' not in ticker.strip(): ticker = f'XYZ {ticker.strip()} Equity' info = param.load_info(cat='exch').get( market_info(ticker=ticker).get('exch', ''), dict() ) if ('allday' in info) and ('day' not in info): info['day'] = info['allday'] if any(req not in info for req in ['tz', 'allday', 'day']): logger.error(f'required exchange info cannot be found in {ticker} ...') return pd.Series() for ss in ValidSessions: if ss not in info: continue info[ss] = [param.to_hour(num=s) for s in info[ss]] return pd.Series(info)
def exch_info(ticker: str) -> pd.Series: """ Exchange info for given ticker Args: ticker: ticker or exchange Returns: pd.Series Examples: >>> exch_info('SPY US Equity') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object >>> exch_info('ES1 Index') tz America/New_York allday [18:00, 17:00] day [08:00, 17:00] dtype: object >>> exch_info('Z 1 Index') tz Europe/London allday [01:00, 21:00] day [01:00, 21:00] dtype: object >>> exch_info('TESTTICKER Corp').empty True >>> exch_info('US') tz America/New_York allday [04:00, 20:00] day [09:30, 16:00] pre [04:00, 09:30] post [16:01, 20:00] dtype: object """ logger = logs.get_logger(exch_info, level='debug') if ' ' not in ticker.strip(): ticker = f'XYZ {ticker.strip()} Equity' info = param.load_info(cat='exch').get( market_info(ticker=ticker).get('exch', ''), dict() ) if ('allday' in info) and ('day' not in info): info['day'] = info['allday'] if any(req not in info for req in ['tz', 'allday', 'day']): logger.error(f'required exchange info cannot be found in {ticker} ...') return pd.Series() for ss in ValidSessions: if ss not in info: continue info[ss] = [param.to_hour(num=s) for s in info[ss]] return pd.Series(info)
[ "Exchange", "info", "for", "given", "ticker" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/const.py#L16-L71
[ "def", "exch_info", "(", "ticker", ":", "str", ")", "->", "pd", ".", "Series", ":", "logger", "=", "logs", ".", "get_logger", "(", "exch_info", ",", "level", "=", "'debug'", ")", "if", "' '", "not", "in", "ticker", ".", "strip", "(", ")", ":", "ticker", "=", "f'XYZ {ticker.strip()} Equity'", "info", "=", "param", ".", "load_info", "(", "cat", "=", "'exch'", ")", ".", "get", "(", "market_info", "(", "ticker", "=", "ticker", ")", ".", "get", "(", "'exch'", ",", "''", ")", ",", "dict", "(", ")", ")", "if", "(", "'allday'", "in", "info", ")", "and", "(", "'day'", "not", "in", "info", ")", ":", "info", "[", "'day'", "]", "=", "info", "[", "'allday'", "]", "if", "any", "(", "req", "not", "in", "info", "for", "req", "in", "[", "'tz'", ",", "'allday'", ",", "'day'", "]", ")", ":", "logger", ".", "error", "(", "f'required exchange info cannot be found in {ticker} ...'", ")", "return", "pd", ".", "Series", "(", ")", "for", "ss", "in", "ValidSessions", ":", "if", "ss", "not", "in", "info", ":", "continue", "info", "[", "ss", "]", "=", "[", "param", ".", "to_hour", "(", "num", "=", "s", ")", "for", "s", "in", "info", "[", "ss", "]", "]", "return", "pd", ".", "Series", "(", "info", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
market_info
Get info for given market Args: ticker: Bloomberg full ticker Returns: dict Examples: >>> info = market_info('SHCOMP Index') >>> info['exch'] 'EquityChina' >>> info = market_info('ICICIC=1 IS Equity') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('INT1 Curncy') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('CL1 Comdty') >>> info['freq'], info['is_fut'] ('M', True) >>> # Wrong tickers >>> market_info('C XX Equity') {} >>> market_info('XXX Comdty') {} >>> market_info('Bond_ISIN Corp') {} >>> market_info('XYZ Index') {} >>> market_info('XYZ Curncy') {}
xbbg/const.py
def market_info(ticker: str) -> dict: """ Get info for given market Args: ticker: Bloomberg full ticker Returns: dict Examples: >>> info = market_info('SHCOMP Index') >>> info['exch'] 'EquityChina' >>> info = market_info('ICICIC=1 IS Equity') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('INT1 Curncy') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('CL1 Comdty') >>> info['freq'], info['is_fut'] ('M', True) >>> # Wrong tickers >>> market_info('C XX Equity') {} >>> market_info('XXX Comdty') {} >>> market_info('Bond_ISIN Corp') {} >>> market_info('XYZ Index') {} >>> market_info('XYZ Curncy') {} """ t_info = ticker.split() assets = param.load_info('assets') # ========================== # # Equity # # ========================== # if (t_info[-1] == 'Equity') and ('=' not in t_info[0]): exch = t_info[-2] for info in assets.get('Equity', [dict()]): if 'exch_codes' not in info: continue if exch in info['exch_codes']: return info return dict() # ============================ # # Currency # # ============================ # if t_info[-1] == 'Curncy': for info in assets.get('Curncy', [dict()]): if 'tickers' not in info: continue if (t_info[0].split('+')[0] in info['tickers']) or \ (t_info[0][-1].isdigit() and (t_info[0][:-1] in info['tickers'])): return info return dict() if t_info[-1] == 'Comdty': for info in assets.get('Comdty', [dict()]): if 'tickers' not in info: continue if t_info[0][:-1] in info['tickers']: return info return dict() # =================================== # # Index / Futures # # =================================== # if (t_info[-1] == 'Index') or ( (t_info[-1] == 'Equity') and ('=' in t_info[0]) ): if t_info[-1] == 'Equity': tck = t_info[0].split('=')[0] else: tck = ' '.join(t_info[:-1]) for info in assets.get('Index', [dict()]): if 'tickers' not in info: continue if (tck[:2] == 'UX') and ('UX' in info['tickers']): return info if tck in info['tickers']: if t_info[-1] == 'Equity': return info if not info.get('is_fut', False): return info if tck[:-1].rstrip() in info['tickers']: if info.get('is_fut', False): return info return dict() if t_info[-1] == 'Corp': for info in assets.get('Corp', [dict()]): if 'ticker' not in info: continue return dict()
def market_info(ticker: str) -> dict: """ Get info for given market Args: ticker: Bloomberg full ticker Returns: dict Examples: >>> info = market_info('SHCOMP Index') >>> info['exch'] 'EquityChina' >>> info = market_info('ICICIC=1 IS Equity') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('INT1 Curncy') >>> info['freq'], info['is_fut'] ('M', True) >>> info = market_info('CL1 Comdty') >>> info['freq'], info['is_fut'] ('M', True) >>> # Wrong tickers >>> market_info('C XX Equity') {} >>> market_info('XXX Comdty') {} >>> market_info('Bond_ISIN Corp') {} >>> market_info('XYZ Index') {} >>> market_info('XYZ Curncy') {} """ t_info = ticker.split() assets = param.load_info('assets') # ========================== # # Equity # # ========================== # if (t_info[-1] == 'Equity') and ('=' not in t_info[0]): exch = t_info[-2] for info in assets.get('Equity', [dict()]): if 'exch_codes' not in info: continue if exch in info['exch_codes']: return info return dict() # ============================ # # Currency # # ============================ # if t_info[-1] == 'Curncy': for info in assets.get('Curncy', [dict()]): if 'tickers' not in info: continue if (t_info[0].split('+')[0] in info['tickers']) or \ (t_info[0][-1].isdigit() and (t_info[0][:-1] in info['tickers'])): return info return dict() if t_info[-1] == 'Comdty': for info in assets.get('Comdty', [dict()]): if 'tickers' not in info: continue if t_info[0][:-1] in info['tickers']: return info return dict() # =================================== # # Index / Futures # # =================================== # if (t_info[-1] == 'Index') or ( (t_info[-1] == 'Equity') and ('=' in t_info[0]) ): if t_info[-1] == 'Equity': tck = t_info[0].split('=')[0] else: tck = ' '.join(t_info[:-1]) for info in assets.get('Index', [dict()]): if 'tickers' not in info: continue if (tck[:2] == 'UX') and ('UX' in info['tickers']): return info if tck in info['tickers']: if t_info[-1] == 'Equity': return info if not info.get('is_fut', False): return info if tck[:-1].rstrip() in info['tickers']: if info.get('is_fut', False): return info return dict() if t_info[-1] == 'Corp': for info in assets.get('Corp', [dict()]): if 'ticker' not in info: continue return dict()
[ "Get", "info", "for", "given", "market" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/const.py#L74-L166
[ "def", "market_info", "(", "ticker", ":", "str", ")", "->", "dict", ":", "t_info", "=", "ticker", ".", "split", "(", ")", "assets", "=", "param", ".", "load_info", "(", "'assets'", ")", "# ========================== #", "# Equity #", "# ========================== #", "if", "(", "t_info", "[", "-", "1", "]", "==", "'Equity'", ")", "and", "(", "'='", "not", "in", "t_info", "[", "0", "]", ")", ":", "exch", "=", "t_info", "[", "-", "2", "]", "for", "info", "in", "assets", ".", "get", "(", "'Equity'", ",", "[", "dict", "(", ")", "]", ")", ":", "if", "'exch_codes'", "not", "in", "info", ":", "continue", "if", "exch", "in", "info", "[", "'exch_codes'", "]", ":", "return", "info", "return", "dict", "(", ")", "# ============================ #", "# Currency #", "# ============================ #", "if", "t_info", "[", "-", "1", "]", "==", "'Curncy'", ":", "for", "info", "in", "assets", ".", "get", "(", "'Curncy'", ",", "[", "dict", "(", ")", "]", ")", ":", "if", "'tickers'", "not", "in", "info", ":", "continue", "if", "(", "t_info", "[", "0", "]", ".", "split", "(", "'+'", ")", "[", "0", "]", "in", "info", "[", "'tickers'", "]", ")", "or", "(", "t_info", "[", "0", "]", "[", "-", "1", "]", ".", "isdigit", "(", ")", "and", "(", "t_info", "[", "0", "]", "[", ":", "-", "1", "]", "in", "info", "[", "'tickers'", "]", ")", ")", ":", "return", "info", "return", "dict", "(", ")", "if", "t_info", "[", "-", "1", "]", "==", "'Comdty'", ":", "for", "info", "in", "assets", ".", "get", "(", "'Comdty'", ",", "[", "dict", "(", ")", "]", ")", ":", "if", "'tickers'", "not", "in", "info", ":", "continue", "if", "t_info", "[", "0", "]", "[", ":", "-", "1", "]", "in", "info", "[", "'tickers'", "]", ":", "return", "info", "return", "dict", "(", ")", "# =================================== #", "# Index / Futures #", "# =================================== #", "if", "(", "t_info", "[", "-", "1", "]", "==", "'Index'", ")", "or", "(", "(", "t_info", "[", "-", "1", "]", "==", "'Equity'", ")", "and", "(", "'='", "in", "t_info", "[", "0", "]", ")", ")", ":", "if", "t_info", "[", "-", "1", "]", "==", "'Equity'", ":", "tck", "=", "t_info", "[", "0", "]", ".", "split", "(", "'='", ")", "[", "0", "]", "else", ":", "tck", "=", "' '", ".", "join", "(", "t_info", "[", ":", "-", "1", "]", ")", "for", "info", "in", "assets", ".", "get", "(", "'Index'", ",", "[", "dict", "(", ")", "]", ")", ":", "if", "'tickers'", "not", "in", "info", ":", "continue", "if", "(", "tck", "[", ":", "2", "]", "==", "'UX'", ")", "and", "(", "'UX'", "in", "info", "[", "'tickers'", "]", ")", ":", "return", "info", "if", "tck", "in", "info", "[", "'tickers'", "]", ":", "if", "t_info", "[", "-", "1", "]", "==", "'Equity'", ":", "return", "info", "if", "not", "info", ".", "get", "(", "'is_fut'", ",", "False", ")", ":", "return", "info", "if", "tck", "[", ":", "-", "1", "]", ".", "rstrip", "(", ")", "in", "info", "[", "'tickers'", "]", ":", "if", "info", ".", "get", "(", "'is_fut'", ",", "False", ")", ":", "return", "info", "return", "dict", "(", ")", "if", "t_info", "[", "-", "1", "]", "==", "'Corp'", ":", "for", "info", "in", "assets", ".", "get", "(", "'Corp'", ",", "[", "dict", "(", ")", "]", ")", ":", "if", "'ticker'", "not", "in", "info", ":", "continue", "return", "dict", "(", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
ccy_pair
Currency pair info Args: local: local currency base: base currency Returns: CurrencyPair Examples: >>> ccy_pair(local='HKD', base='USD') CurrencyPair(ticker='HKD Curncy', factor=1.0, power=1) >>> ccy_pair(local='GBp') CurrencyPair(ticker='GBP Curncy', factor=100, power=-1) >>> ccy_pair(local='USD', base='GBp') CurrencyPair(ticker='GBP Curncy', factor=0.01, power=1) >>> ccy_pair(local='XYZ', base='USD') CurrencyPair(ticker='', factor=1.0, power=1) >>> ccy_pair(local='GBP', base='GBp') CurrencyPair(ticker='', factor=0.01, power=1) >>> ccy_pair(local='GBp', base='GBP') CurrencyPair(ticker='', factor=100.0, power=1)
xbbg/const.py
def ccy_pair(local, base='USD') -> CurrencyPair: """ Currency pair info Args: local: local currency base: base currency Returns: CurrencyPair Examples: >>> ccy_pair(local='HKD', base='USD') CurrencyPair(ticker='HKD Curncy', factor=1.0, power=1) >>> ccy_pair(local='GBp') CurrencyPair(ticker='GBP Curncy', factor=100, power=-1) >>> ccy_pair(local='USD', base='GBp') CurrencyPair(ticker='GBP Curncy', factor=0.01, power=1) >>> ccy_pair(local='XYZ', base='USD') CurrencyPair(ticker='', factor=1.0, power=1) >>> ccy_pair(local='GBP', base='GBp') CurrencyPair(ticker='', factor=0.01, power=1) >>> ccy_pair(local='GBp', base='GBP') CurrencyPair(ticker='', factor=100.0, power=1) """ ccy_param = param.load_info(cat='ccy') if f'{local}{base}' in ccy_param: info = ccy_param[f'{local}{base}'] elif f'{base}{local}' in ccy_param: info = ccy_param[f'{base}{local}'] info['factor'] = 1. / info.get('factor', 1.) info['power'] = -info.get('power', 1) elif base.lower() == local.lower(): info = dict(ticker='') info['factor'] = 1. if base[-1].lower() == base[-1]: info['factor'] /= 100. if local[-1].lower() == local[-1]: info['factor'] *= 100. else: logger = logs.get_logger(ccy_pair) logger.error(f'incorrect currency - local {local} / base {base}') return CurrencyPair(ticker='', factor=1., power=1) if 'factor' not in info: info['factor'] = 1. if 'power' not in info: info['power'] = 1 return CurrencyPair(**info)
def ccy_pair(local, base='USD') -> CurrencyPair: """ Currency pair info Args: local: local currency base: base currency Returns: CurrencyPair Examples: >>> ccy_pair(local='HKD', base='USD') CurrencyPair(ticker='HKD Curncy', factor=1.0, power=1) >>> ccy_pair(local='GBp') CurrencyPair(ticker='GBP Curncy', factor=100, power=-1) >>> ccy_pair(local='USD', base='GBp') CurrencyPair(ticker='GBP Curncy', factor=0.01, power=1) >>> ccy_pair(local='XYZ', base='USD') CurrencyPair(ticker='', factor=1.0, power=1) >>> ccy_pair(local='GBP', base='GBp') CurrencyPair(ticker='', factor=0.01, power=1) >>> ccy_pair(local='GBp', base='GBP') CurrencyPair(ticker='', factor=100.0, power=1) """ ccy_param = param.load_info(cat='ccy') if f'{local}{base}' in ccy_param: info = ccy_param[f'{local}{base}'] elif f'{base}{local}' in ccy_param: info = ccy_param[f'{base}{local}'] info['factor'] = 1. / info.get('factor', 1.) info['power'] = -info.get('power', 1) elif base.lower() == local.lower(): info = dict(ticker='') info['factor'] = 1. if base[-1].lower() == base[-1]: info['factor'] /= 100. if local[-1].lower() == local[-1]: info['factor'] *= 100. else: logger = logs.get_logger(ccy_pair) logger.error(f'incorrect currency - local {local} / base {base}') return CurrencyPair(ticker='', factor=1., power=1) if 'factor' not in info: info['factor'] = 1. if 'power' not in info: info['power'] = 1 return CurrencyPair(**info)
[ "Currency", "pair", "info" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/const.py#L169-L218
[ "def", "ccy_pair", "(", "local", ",", "base", "=", "'USD'", ")", "->", "CurrencyPair", ":", "ccy_param", "=", "param", ".", "load_info", "(", "cat", "=", "'ccy'", ")", "if", "f'{local}{base}'", "in", "ccy_param", ":", "info", "=", "ccy_param", "[", "f'{local}{base}'", "]", "elif", "f'{base}{local}'", "in", "ccy_param", ":", "info", "=", "ccy_param", "[", "f'{base}{local}'", "]", "info", "[", "'factor'", "]", "=", "1.", "/", "info", ".", "get", "(", "'factor'", ",", "1.", ")", "info", "[", "'power'", "]", "=", "-", "info", ".", "get", "(", "'power'", ",", "1", ")", "elif", "base", ".", "lower", "(", ")", "==", "local", ".", "lower", "(", ")", ":", "info", "=", "dict", "(", "ticker", "=", "''", ")", "info", "[", "'factor'", "]", "=", "1.", "if", "base", "[", "-", "1", "]", ".", "lower", "(", ")", "==", "base", "[", "-", "1", "]", ":", "info", "[", "'factor'", "]", "/=", "100.", "if", "local", "[", "-", "1", "]", ".", "lower", "(", ")", "==", "local", "[", "-", "1", "]", ":", "info", "[", "'factor'", "]", "*=", "100.", "else", ":", "logger", "=", "logs", ".", "get_logger", "(", "ccy_pair", ")", "logger", ".", "error", "(", "f'incorrect currency - local {local} / base {base}'", ")", "return", "CurrencyPair", "(", "ticker", "=", "''", ",", "factor", "=", "1.", ",", "power", "=", "1", ")", "if", "'factor'", "not", "in", "info", ":", "info", "[", "'factor'", "]", "=", "1.", "if", "'power'", "not", "in", "info", ":", "info", "[", "'power'", "]", "=", "1", "return", "CurrencyPair", "(", "*", "*", "info", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
market_timing
Market close time for ticker Args: ticker: ticker name dt: date timing: [EOD (default), BOD] tz: conversion to timezone Returns: str: date & time Examples: >>> market_timing('7267 JT Equity', dt='2018-09-10') '2018-09-10 14:58' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz=timezone.TimeZone.NY) '2018-09-10 01:58:00-04:00' >>> market_timing('7267 JT Equity', dt='2018-01-10', tz='NY') '2018-01-10 00:58:00-05:00' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz='SPX Index') '2018-09-10 01:58:00-04:00' >>> market_timing('8035 JT Equity', dt='2018-09-10', timing='BOD') '2018-09-10 09:01' >>> market_timing('Z 1 Index', dt='2018-09-10', timing='FINISHED') '2018-09-10 21:00' >>> market_timing('TESTTICKER Corp', dt='2018-09-10') ''
xbbg/const.py
def market_timing(ticker, dt, timing='EOD', tz='local') -> str: """ Market close time for ticker Args: ticker: ticker name dt: date timing: [EOD (default), BOD] tz: conversion to timezone Returns: str: date & time Examples: >>> market_timing('7267 JT Equity', dt='2018-09-10') '2018-09-10 14:58' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz=timezone.TimeZone.NY) '2018-09-10 01:58:00-04:00' >>> market_timing('7267 JT Equity', dt='2018-01-10', tz='NY') '2018-01-10 00:58:00-05:00' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz='SPX Index') '2018-09-10 01:58:00-04:00' >>> market_timing('8035 JT Equity', dt='2018-09-10', timing='BOD') '2018-09-10 09:01' >>> market_timing('Z 1 Index', dt='2018-09-10', timing='FINISHED') '2018-09-10 21:00' >>> market_timing('TESTTICKER Corp', dt='2018-09-10') '' """ logger = logs.get_logger(market_timing) exch = pd.Series(exch_info(ticker=ticker)) if any(req not in exch.index for req in ['tz', 'allday', 'day']): logger.error(f'required exchange info cannot be found in {ticker} ...') return '' mkt_time = { 'BOD': exch.day[0], 'FINISHED': exch.allday[-1] }.get(timing, exch.day[-1]) cur_dt = pd.Timestamp(str(dt)).strftime('%Y-%m-%d') if tz == 'local': return f'{cur_dt} {mkt_time}' return timezone.tz_convert(f'{cur_dt} {mkt_time}', to_tz=tz, from_tz=exch.tz)
def market_timing(ticker, dt, timing='EOD', tz='local') -> str: """ Market close time for ticker Args: ticker: ticker name dt: date timing: [EOD (default), BOD] tz: conversion to timezone Returns: str: date & time Examples: >>> market_timing('7267 JT Equity', dt='2018-09-10') '2018-09-10 14:58' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz=timezone.TimeZone.NY) '2018-09-10 01:58:00-04:00' >>> market_timing('7267 JT Equity', dt='2018-01-10', tz='NY') '2018-01-10 00:58:00-05:00' >>> market_timing('7267 JT Equity', dt='2018-09-10', tz='SPX Index') '2018-09-10 01:58:00-04:00' >>> market_timing('8035 JT Equity', dt='2018-09-10', timing='BOD') '2018-09-10 09:01' >>> market_timing('Z 1 Index', dt='2018-09-10', timing='FINISHED') '2018-09-10 21:00' >>> market_timing('TESTTICKER Corp', dt='2018-09-10') '' """ logger = logs.get_logger(market_timing) exch = pd.Series(exch_info(ticker=ticker)) if any(req not in exch.index for req in ['tz', 'allday', 'day']): logger.error(f'required exchange info cannot be found in {ticker} ...') return '' mkt_time = { 'BOD': exch.day[0], 'FINISHED': exch.allday[-1] }.get(timing, exch.day[-1]) cur_dt = pd.Timestamp(str(dt)).strftime('%Y-%m-%d') if tz == 'local': return f'{cur_dt} {mkt_time}' return timezone.tz_convert(f'{cur_dt} {mkt_time}', to_tz=tz, from_tz=exch.tz)
[ "Market", "close", "time", "for", "ticker" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/const.py#L221-L264
[ "def", "market_timing", "(", "ticker", ",", "dt", ",", "timing", "=", "'EOD'", ",", "tz", "=", "'local'", ")", "->", "str", ":", "logger", "=", "logs", ".", "get_logger", "(", "market_timing", ")", "exch", "=", "pd", ".", "Series", "(", "exch_info", "(", "ticker", "=", "ticker", ")", ")", "if", "any", "(", "req", "not", "in", "exch", ".", "index", "for", "req", "in", "[", "'tz'", ",", "'allday'", ",", "'day'", "]", ")", ":", "logger", ".", "error", "(", "f'required exchange info cannot be found in {ticker} ...'", ")", "return", "''", "mkt_time", "=", "{", "'BOD'", ":", "exch", ".", "day", "[", "0", "]", ",", "'FINISHED'", ":", "exch", ".", "allday", "[", "-", "1", "]", "}", ".", "get", "(", "timing", ",", "exch", ".", "day", "[", "-", "1", "]", ")", "cur_dt", "=", "pd", ".", "Timestamp", "(", "str", "(", "dt", ")", ")", ".", "strftime", "(", "'%Y-%m-%d'", ")", "if", "tz", "==", "'local'", ":", "return", "f'{cur_dt} {mkt_time}'", "return", "timezone", ".", "tz_convert", "(", "f'{cur_dt} {mkt_time}'", ",", "to_tz", "=", "tz", ",", "from_tz", "=", "exch", ".", "tz", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
flatten
Flatten any array of items to list Args: iterable: any array or value maps: map items to values unique: drop duplicates Returns: list: flattened list References: https://stackoverflow.com/a/40857703/1332656 Examples: >>> flatten('abc') ['abc'] >>> flatten(1) [1] >>> flatten(1.) [1.0] >>> flatten(['ab', 'cd', ['xy', 'zz']]) ['ab', 'cd', 'xy', 'zz'] >>> flatten(['ab', ['xy', 'zz']], maps={'xy': '0x'}) ['ab', '0x', 'zz']
xbbg/core/utils.py
def flatten(iterable, maps=None, unique=False) -> list: """ Flatten any array of items to list Args: iterable: any array or value maps: map items to values unique: drop duplicates Returns: list: flattened list References: https://stackoverflow.com/a/40857703/1332656 Examples: >>> flatten('abc') ['abc'] >>> flatten(1) [1] >>> flatten(1.) [1.0] >>> flatten(['ab', 'cd', ['xy', 'zz']]) ['ab', 'cd', 'xy', 'zz'] >>> flatten(['ab', ['xy', 'zz']], maps={'xy': '0x'}) ['ab', '0x', 'zz'] """ if iterable is None: return [] if maps is None: maps = dict() if isinstance(iterable, (str, int, float)): return [maps.get(iterable, iterable)] else: x = [maps.get(item, item) for item in _to_gen_(iterable)] return list(set(x)) if unique else x
def flatten(iterable, maps=None, unique=False) -> list: """ Flatten any array of items to list Args: iterable: any array or value maps: map items to values unique: drop duplicates Returns: list: flattened list References: https://stackoverflow.com/a/40857703/1332656 Examples: >>> flatten('abc') ['abc'] >>> flatten(1) [1] >>> flatten(1.) [1.0] >>> flatten(['ab', 'cd', ['xy', 'zz']]) ['ab', 'cd', 'xy', 'zz'] >>> flatten(['ab', ['xy', 'zz']], maps={'xy': '0x'}) ['ab', '0x', 'zz'] """ if iterable is None: return [] if maps is None: maps = dict() if isinstance(iterable, (str, int, float)): return [maps.get(iterable, iterable)] else: x = [maps.get(item, item) for item in _to_gen_(iterable)] return list(set(x)) if unique else x
[ "Flatten", "any", "array", "of", "items", "to", "list" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/utils.py#L12-L47
[ "def", "flatten", "(", "iterable", ",", "maps", "=", "None", ",", "unique", "=", "False", ")", "->", "list", ":", "if", "iterable", "is", "None", ":", "return", "[", "]", "if", "maps", "is", "None", ":", "maps", "=", "dict", "(", ")", "if", "isinstance", "(", "iterable", ",", "(", "str", ",", "int", ",", "float", ")", ")", ":", "return", "[", "maps", ".", "get", "(", "iterable", ",", "iterable", ")", "]", "else", ":", "x", "=", "[", "maps", ".", "get", "(", "item", ",", "item", ")", "for", "item", "in", "_to_gen_", "(", "iterable", ")", "]", "return", "list", "(", "set", "(", "x", ")", ")", "if", "unique", "else", "x" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
_to_gen_
Recursively iterate lists and tuples
xbbg/core/utils.py
def _to_gen_(iterable): """ Recursively iterate lists and tuples """ from collections import Iterable for elm in iterable: if isinstance(elm, Iterable) and not isinstance(elm, (str, bytes)): yield from flatten(elm) else: yield elm
def _to_gen_(iterable): """ Recursively iterate lists and tuples """ from collections import Iterable for elm in iterable: if isinstance(elm, Iterable) and not isinstance(elm, (str, bytes)): yield from flatten(elm) else: yield elm
[ "Recursively", "iterate", "lists", "and", "tuples" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/utils.py#L50-L59
[ "def", "_to_gen_", "(", "iterable", ")", ":", "from", "collections", "import", "Iterable", "for", "elm", "in", "iterable", ":", "if", "isinstance", "(", "elm", ",", "Iterable", ")", "and", "not", "isinstance", "(", "elm", ",", "(", "str", ",", "bytes", ")", ")", ":", "yield", "from", "flatten", "(", "elm", ")", "else", ":", "yield", "elm" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
cur_time
Current time Args: typ: one of ['date', 'time', 'time_path', 'raw', ''] tz: timezone Returns: relevant current time or date Examples: >>> cur_dt = pd.Timestamp('now') >>> cur_time(typ='date') == cur_dt.strftime('%Y-%m-%d') True >>> cur_time(typ='time') == cur_dt.strftime('%Y-%m-%d %H:%M:%S') True >>> cur_time(typ='time_path') == cur_dt.strftime('%Y-%m-%d/%H-%M-%S') True >>> isinstance(cur_time(typ='raw', tz='Europe/London'), pd.Timestamp) True >>> cur_time(typ='') == cur_dt.date() True
xbbg/core/utils.py
def cur_time(typ='date', tz=DEFAULT_TZ) -> (datetime.date, str): """ Current time Args: typ: one of ['date', 'time', 'time_path', 'raw', ''] tz: timezone Returns: relevant current time or date Examples: >>> cur_dt = pd.Timestamp('now') >>> cur_time(typ='date') == cur_dt.strftime('%Y-%m-%d') True >>> cur_time(typ='time') == cur_dt.strftime('%Y-%m-%d %H:%M:%S') True >>> cur_time(typ='time_path') == cur_dt.strftime('%Y-%m-%d/%H-%M-%S') True >>> isinstance(cur_time(typ='raw', tz='Europe/London'), pd.Timestamp) True >>> cur_time(typ='') == cur_dt.date() True """ dt = pd.Timestamp('now', tz=tz) if typ == 'date': return dt.strftime('%Y-%m-%d') if typ == 'time': return dt.strftime('%Y-%m-%d %H:%M:%S') if typ == 'time_path': return dt.strftime('%Y-%m-%d/%H-%M-%S') if typ == 'raw': return dt return dt.date()
def cur_time(typ='date', tz=DEFAULT_TZ) -> (datetime.date, str): """ Current time Args: typ: one of ['date', 'time', 'time_path', 'raw', ''] tz: timezone Returns: relevant current time or date Examples: >>> cur_dt = pd.Timestamp('now') >>> cur_time(typ='date') == cur_dt.strftime('%Y-%m-%d') True >>> cur_time(typ='time') == cur_dt.strftime('%Y-%m-%d %H:%M:%S') True >>> cur_time(typ='time_path') == cur_dt.strftime('%Y-%m-%d/%H-%M-%S') True >>> isinstance(cur_time(typ='raw', tz='Europe/London'), pd.Timestamp) True >>> cur_time(typ='') == cur_dt.date() True """ dt = pd.Timestamp('now', tz=tz) if typ == 'date': return dt.strftime('%Y-%m-%d') if typ == 'time': return dt.strftime('%Y-%m-%d %H:%M:%S') if typ == 'time_path': return dt.strftime('%Y-%m-%d/%H-%M-%S') if typ == 'raw': return dt return dt.date()
[ "Current", "time" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/utils.py#L82-L113
[ "def", "cur_time", "(", "typ", "=", "'date'", ",", "tz", "=", "DEFAULT_TZ", ")", "->", "(", "datetime", ".", "date", ",", "str", ")", ":", "dt", "=", "pd", ".", "Timestamp", "(", "'now'", ",", "tz", "=", "tz", ")", "if", "typ", "==", "'date'", ":", "return", "dt", ".", "strftime", "(", "'%Y-%m-%d'", ")", "if", "typ", "==", "'time'", ":", "return", "dt", ".", "strftime", "(", "'%Y-%m-%d %H:%M:%S'", ")", "if", "typ", "==", "'time_path'", ":", "return", "dt", ".", "strftime", "(", "'%Y-%m-%d/%H-%M-%S'", ")", "if", "typ", "==", "'raw'", ":", "return", "dt", "return", "dt", ".", "date", "(", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
to_str
Convert dict to string Args: data: dict fmt: how key and value being represented sep: how pairs of key and value are seperated public_only: if display public members only Returns: str: string representation of dict Examples: >>> test_dict = dict(b=1, a=0, c=2, _d=3) >>> to_str(test_dict) '{b=1, a=0, c=2}' >>> to_str(test_dict, sep='|') '{b=1|a=0|c=2}' >>> to_str(test_dict, public_only=False) '{b=1, a=0, c=2, _d=3}'
xbbg/core/utils.py
def to_str( data: dict, fmt='{key}={value}', sep=', ', public_only=True ) -> str: """ Convert dict to string Args: data: dict fmt: how key and value being represented sep: how pairs of key and value are seperated public_only: if display public members only Returns: str: string representation of dict Examples: >>> test_dict = dict(b=1, a=0, c=2, _d=3) >>> to_str(test_dict) '{b=1, a=0, c=2}' >>> to_str(test_dict, sep='|') '{b=1|a=0|c=2}' >>> to_str(test_dict, public_only=False) '{b=1, a=0, c=2, _d=3}' """ if public_only: keys = list(filter(lambda vv: vv[0] != '_', data.keys())) else: keys = list(data.keys()) return '{' + sep.join([ to_str(data=v, fmt=fmt, sep=sep) if isinstance(v, dict) else fstr(fmt=fmt, key=k, value=v) for k, v in data.items() if k in keys ]) + '}'
def to_str( data: dict, fmt='{key}={value}', sep=', ', public_only=True ) -> str: """ Convert dict to string Args: data: dict fmt: how key and value being represented sep: how pairs of key and value are seperated public_only: if display public members only Returns: str: string representation of dict Examples: >>> test_dict = dict(b=1, a=0, c=2, _d=3) >>> to_str(test_dict) '{b=1, a=0, c=2}' >>> to_str(test_dict, sep='|') '{b=1|a=0|c=2}' >>> to_str(test_dict, public_only=False) '{b=1, a=0, c=2, _d=3}' """ if public_only: keys = list(filter(lambda vv: vv[0] != '_', data.keys())) else: keys = list(data.keys()) return '{' + sep.join([ to_str(data=v, fmt=fmt, sep=sep) if isinstance(v, dict) else fstr(fmt=fmt, key=k, value=v) for k, v in data.items() if k in keys ]) + '}'
[ "Convert", "dict", "to", "string" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/utils.py#L151-L181
[ "def", "to_str", "(", "data", ":", "dict", ",", "fmt", "=", "'{key}={value}'", ",", "sep", "=", "', '", ",", "public_only", "=", "True", ")", "->", "str", ":", "if", "public_only", ":", "keys", "=", "list", "(", "filter", "(", "lambda", "vv", ":", "vv", "[", "0", "]", "!=", "'_'", ",", "data", ".", "keys", "(", ")", ")", ")", "else", ":", "keys", "=", "list", "(", "data", ".", "keys", "(", ")", ")", "return", "'{'", "+", "sep", ".", "join", "(", "[", "to_str", "(", "data", "=", "v", ",", "fmt", "=", "fmt", ",", "sep", "=", "sep", ")", "if", "isinstance", "(", "v", ",", "dict", ")", "else", "fstr", "(", "fmt", "=", "fmt", ",", "key", "=", "k", ",", "value", "=", "v", ")", "for", "k", ",", "v", "in", "data", ".", "items", "(", ")", "if", "k", "in", "keys", "]", ")", "+", "'}'" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
load_module
Load module from full path Args: full_path: module full path name Returns: python module References: https://stackoverflow.com/a/67692/1332656 Examples: >>> import os >>> >>> cur_file = os.path.abspath(__file__).replace('\\\\', '/') >>> cur_path = '/'.join(cur_file.split('/')[:-1]) >>> load_module(f'{cur_path}/timezone.py').__name__ 'timezone' >>> load_module(f'{cur_path}/timezone.pyc') Traceback (most recent call last): ImportError: not a python file: timezone.pyc
xbbg/core/utils.py
def load_module(full_path): """ Load module from full path Args: full_path: module full path name Returns: python module References: https://stackoverflow.com/a/67692/1332656 Examples: >>> import os >>> >>> cur_file = os.path.abspath(__file__).replace('\\\\', '/') >>> cur_path = '/'.join(cur_file.split('/')[:-1]) >>> load_module(f'{cur_path}/timezone.py').__name__ 'timezone' >>> load_module(f'{cur_path}/timezone.pyc') Traceback (most recent call last): ImportError: not a python file: timezone.pyc """ from importlib import util file_name = full_path.replace('\\', '/').split('/')[-1] if file_name[-3:] != '.py': raise ImportError(f'not a python file: {file_name}') module_name = file_name[:-3] spec = util.spec_from_file_location(name=module_name, location=full_path) module = util.module_from_spec(spec=spec) spec.loader.exec_module(module=module) return module
def load_module(full_path): """ Load module from full path Args: full_path: module full path name Returns: python module References: https://stackoverflow.com/a/67692/1332656 Examples: >>> import os >>> >>> cur_file = os.path.abspath(__file__).replace('\\\\', '/') >>> cur_path = '/'.join(cur_file.split('/')[:-1]) >>> load_module(f'{cur_path}/timezone.py').__name__ 'timezone' >>> load_module(f'{cur_path}/timezone.pyc') Traceback (most recent call last): ImportError: not a python file: timezone.pyc """ from importlib import util file_name = full_path.replace('\\', '/').split('/')[-1] if file_name[-3:] != '.py': raise ImportError(f'not a python file: {file_name}') module_name = file_name[:-3] spec = util.spec_from_file_location(name=module_name, location=full_path) module = util.module_from_spec(spec=spec) spec.loader.exec_module(module=module) return module
[ "Load", "module", "from", "full", "path", "Args", ":", "full_path", ":", "module", "full", "path", "name", "Returns", ":", "python", "module", "References", ":", "https", ":", "//", "stackoverflow", ".", "com", "/", "a", "/", "67692", "/", "1332656", "Examples", ":", ">>>", "import", "os", ">>>", ">>>", "cur_file", "=", "os", ".", "path", ".", "abspath", "(", "__file__", ")", ".", "replace", "(", "\\\\\\\\", "/", ")", ">>>", "cur_path", "=", "/", ".", "join", "(", "cur_file", ".", "split", "(", "/", ")", "[", ":", "-", "1", "]", ")", ">>>", "load_module", "(", "f", "{", "cur_path", "}", "/", "timezone", ".", "py", ")", ".", "__name__", "timezone", ">>>", "load_module", "(", "f", "{", "cur_path", "}", "/", "timezone", ".", "pyc", ")", "Traceback", "(", "most", "recent", "call", "last", ")", ":", "ImportError", ":", "not", "a", "python", "file", ":", "timezone", ".", "pyc" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/utils.py#L204-L235
[ "def", "load_module", "(", "full_path", ")", ":", "from", "importlib", "import", "util", "file_name", "=", "full_path", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "if", "file_name", "[", "-", "3", ":", "]", "!=", "'.py'", ":", "raise", "ImportError", "(", "f'not a python file: {file_name}'", ")", "module_name", "=", "file_name", "[", ":", "-", "3", "]", "spec", "=", "util", ".", "spec_from_file_location", "(", "name", "=", "module_name", ",", "location", "=", "full_path", ")", "module", "=", "util", ".", "module_from_spec", "(", "spec", "=", "spec", ")", "spec", ".", "loader", ".", "exec_module", "(", "module", "=", "module", ")", "return", "module" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
load_info
Load parameters for assets Args: cat: category Returns: dict Examples: >>> import pandas as pd >>> >>> assets = load_info(cat='assets') >>> all(cat in assets for cat in ['Equity', 'Index', 'Curncy', 'Corp']) True >>> os.environ['BBG_PATH'] = '' >>> exch = load_info(cat='exch') >>> pd.Series(exch['EquityUS']).allday [400, 2000] >>> test_root = f'{PKG_PATH}/tests' >>> os.environ['BBG_PATH'] = test_root >>> ovrd_exch = load_info(cat='exch') >>> # Somehow os.environ is not set properly in doctest environment >>> ovrd_exch.update(_load_yaml_(f'{test_root}/markets/exch.yml')) >>> pd.Series(ovrd_exch['EquityUS']).allday [300, 2100]
xbbg/io/param.py
def load_info(cat): """ Load parameters for assets Args: cat: category Returns: dict Examples: >>> import pandas as pd >>> >>> assets = load_info(cat='assets') >>> all(cat in assets for cat in ['Equity', 'Index', 'Curncy', 'Corp']) True >>> os.environ['BBG_PATH'] = '' >>> exch = load_info(cat='exch') >>> pd.Series(exch['EquityUS']).allday [400, 2000] >>> test_root = f'{PKG_PATH}/tests' >>> os.environ['BBG_PATH'] = test_root >>> ovrd_exch = load_info(cat='exch') >>> # Somehow os.environ is not set properly in doctest environment >>> ovrd_exch.update(_load_yaml_(f'{test_root}/markets/exch.yml')) >>> pd.Series(ovrd_exch['EquityUS']).allday [300, 2100] """ res = _load_yaml_(f'{PKG_PATH}/markets/{cat}.yml') root = os.environ.get('BBG_ROOT', '').replace('\\', '/') if not root: return res for cat, ovrd in _load_yaml_(f'{root}/markets/{cat}.yml').items(): if isinstance(ovrd, dict): if cat in res: res[cat].update(ovrd) else: res[cat] = ovrd if isinstance(ovrd, list) and isinstance(res[cat], list): res[cat] += ovrd return res
def load_info(cat): """ Load parameters for assets Args: cat: category Returns: dict Examples: >>> import pandas as pd >>> >>> assets = load_info(cat='assets') >>> all(cat in assets for cat in ['Equity', 'Index', 'Curncy', 'Corp']) True >>> os.environ['BBG_PATH'] = '' >>> exch = load_info(cat='exch') >>> pd.Series(exch['EquityUS']).allday [400, 2000] >>> test_root = f'{PKG_PATH}/tests' >>> os.environ['BBG_PATH'] = test_root >>> ovrd_exch = load_info(cat='exch') >>> # Somehow os.environ is not set properly in doctest environment >>> ovrd_exch.update(_load_yaml_(f'{test_root}/markets/exch.yml')) >>> pd.Series(ovrd_exch['EquityUS']).allday [300, 2100] """ res = _load_yaml_(f'{PKG_PATH}/markets/{cat}.yml') root = os.environ.get('BBG_ROOT', '').replace('\\', '/') if not root: return res for cat, ovrd in _load_yaml_(f'{root}/markets/{cat}.yml').items(): if isinstance(ovrd, dict): if cat in res: res[cat].update(ovrd) else: res[cat] = ovrd if isinstance(ovrd, list) and isinstance(res[cat], list): res[cat] += ovrd return res
[ "Load", "parameters", "for", "assets" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/param.py#L12-L48
[ "def", "load_info", "(", "cat", ")", ":", "res", "=", "_load_yaml_", "(", "f'{PKG_PATH}/markets/{cat}.yml'", ")", "root", "=", "os", ".", "environ", ".", "get", "(", "'BBG_ROOT'", ",", "''", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "not", "root", ":", "return", "res", "for", "cat", ",", "ovrd", "in", "_load_yaml_", "(", "f'{root}/markets/{cat}.yml'", ")", ".", "items", "(", ")", ":", "if", "isinstance", "(", "ovrd", ",", "dict", ")", ":", "if", "cat", "in", "res", ":", "res", "[", "cat", "]", ".", "update", "(", "ovrd", ")", "else", ":", "res", "[", "cat", "]", "=", "ovrd", "if", "isinstance", "(", "ovrd", ",", "list", ")", "and", "isinstance", "(", "res", "[", "cat", "]", ",", "list", ")", ":", "res", "[", "cat", "]", "+=", "ovrd", "return", "res" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
_load_yaml_
Load assets infomation from file Args: file_name: file name Returns: dict
xbbg/io/param.py
def _load_yaml_(file_name): """ Load assets infomation from file Args: file_name: file name Returns: dict """ if not os.path.exists(file_name): return dict() with open(file_name, 'r', encoding='utf-8') as fp: return YAML().load(stream=fp)
def _load_yaml_(file_name): """ Load assets infomation from file Args: file_name: file name Returns: dict """ if not os.path.exists(file_name): return dict() with open(file_name, 'r', encoding='utf-8') as fp: return YAML().load(stream=fp)
[ "Load", "assets", "infomation", "from", "file" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/param.py#L51-L64
[ "def", "_load_yaml_", "(", "file_name", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "file_name", ")", ":", "return", "dict", "(", ")", "with", "open", "(", "file_name", ",", "'r'", ",", "encoding", "=", "'utf-8'", ")", "as", "fp", ":", "return", "YAML", "(", ")", ".", "load", "(", "stream", "=", "fp", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
to_hour
Convert YAML input to hours Args: num: number in YMAL file, e.g., 900, 1700, etc. Returns: str Examples: >>> to_hour(900) '09:00' >>> to_hour(1700) '17:00'
xbbg/io/param.py
def to_hour(num) -> str: """ Convert YAML input to hours Args: num: number in YMAL file, e.g., 900, 1700, etc. Returns: str Examples: >>> to_hour(900) '09:00' >>> to_hour(1700) '17:00' """ to_str = str(int(num)) return pd.Timestamp(f'{to_str[:-2]}:{to_str[-2:]}').strftime('%H:%M')
def to_hour(num) -> str: """ Convert YAML input to hours Args: num: number in YMAL file, e.g., 900, 1700, etc. Returns: str Examples: >>> to_hour(900) '09:00' >>> to_hour(1700) '17:00' """ to_str = str(int(num)) return pd.Timestamp(f'{to_str[:-2]}:{to_str[-2:]}').strftime('%H:%M')
[ "Convert", "YAML", "input", "to", "hours" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/param.py#L67-L84
[ "def", "to_hour", "(", "num", ")", "->", "str", ":", "to_str", "=", "str", "(", "int", "(", "num", ")", ")", "return", "pd", ".", "Timestamp", "(", "f'{to_str[:-2]}:{to_str[-2:]}'", ")", ".", "strftime", "(", "'%H:%M'", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
abspath
Absolute path Args: cur_file: __file__ or file or path str parent: level of parent to look for Returns: str
xbbg/io/files.py
def abspath(cur_file, parent=0) -> str: """ Absolute path Args: cur_file: __file__ or file or path str parent: level of parent to look for Returns: str """ file_path = os.path.abspath(cur_file).replace('\\', '/') if os.path.isdir(file_path) and parent == 0: return file_path adj = 1 - os.path.isdir(file_path) return '/'.join(file_path.split('/')[:-(parent + adj)])
def abspath(cur_file, parent=0) -> str: """ Absolute path Args: cur_file: __file__ or file or path str parent: level of parent to look for Returns: str """ file_path = os.path.abspath(cur_file).replace('\\', '/') if os.path.isdir(file_path) and parent == 0: return file_path adj = 1 - os.path.isdir(file_path) return '/'.join(file_path.split('/')[:-(parent + adj)])
[ "Absolute", "path" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L21-L35
[ "def", "abspath", "(", "cur_file", ",", "parent", "=", "0", ")", "->", "str", ":", "file_path", "=", "os", ".", "path", ".", "abspath", "(", "cur_file", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "os", ".", "path", ".", "isdir", "(", "file_path", ")", "and", "parent", "==", "0", ":", "return", "file_path", "adj", "=", "1", "-", "os", ".", "path", ".", "isdir", "(", "file_path", ")", "return", "'/'", ".", "join", "(", "file_path", ".", "split", "(", "'/'", ")", "[", ":", "-", "(", "parent", "+", "adj", ")", "]", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
create_folder
Make folder as well as all parent folders if not exists Args: path_name: full path name is_file: whether input is name of file
xbbg/io/files.py
def create_folder(path_name: str, is_file=False): """ Make folder as well as all parent folders if not exists Args: path_name: full path name is_file: whether input is name of file """ path_sep = path_name.replace('\\', '/').split('/') for i in range(1, len(path_sep) + (0 if is_file else 1)): cur_path = '/'.join(path_sep[:i]) if not os.path.exists(cur_path): os.mkdir(cur_path)
def create_folder(path_name: str, is_file=False): """ Make folder as well as all parent folders if not exists Args: path_name: full path name is_file: whether input is name of file """ path_sep = path_name.replace('\\', '/').split('/') for i in range(1, len(path_sep) + (0 if is_file else 1)): cur_path = '/'.join(path_sep[:i]) if not os.path.exists(cur_path): os.mkdir(cur_path)
[ "Make", "folder", "as", "well", "as", "all", "parent", "folders", "if", "not", "exists" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L38-L49
[ "def", "create_folder", "(", "path_name", ":", "str", ",", "is_file", "=", "False", ")", ":", "path_sep", "=", "path_name", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", ".", "split", "(", "'/'", ")", "for", "i", "in", "range", "(", "1", ",", "len", "(", "path_sep", ")", "+", "(", "0", "if", "is_file", "else", "1", ")", ")", ":", "cur_path", "=", "'/'", ".", "join", "(", "path_sep", "[", ":", "i", "]", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "cur_path", ")", ":", "os", ".", "mkdir", "(", "cur_path", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
all_files
Search all files with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search ext: file extensions, split by ',' full_path: whether return full path (default True) has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all file names with criteria fulfilled
xbbg/io/files.py
def all_files( path_name, keyword='', ext='', full_path=True, has_date=False, date_fmt=DATE_FMT ) -> list: """ Search all files with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search ext: file extensions, split by ',' full_path: whether return full path (default True) has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all file names with criteria fulfilled """ if not os.path.exists(path=path_name): return [] path_name = path_name.replace('\\', '/') if keyword or ext: keyword = f'*{keyword}*' if keyword else '*' if not ext: ext = '*' files = sort_by_modified([ f.replace('\\', '/') for f in glob.iglob(f'{path_name}/{keyword}.{ext}') if os.path.isfile(f) and (f.replace('\\', '/').split('/')[-1][0] != '~') ]) else: files = sort_by_modified([ f'{path_name}/{f}' for f in os.listdir(path=path_name) if os.path.isfile(f'{path_name}/{f}') and (f[0] != '~') ]) if has_date: files = filter_by_dates(files, date_fmt=date_fmt) return files if full_path else [f.split('/')[-1] for f in files]
def all_files( path_name, keyword='', ext='', full_path=True, has_date=False, date_fmt=DATE_FMT ) -> list: """ Search all files with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search ext: file extensions, split by ',' full_path: whether return full path (default True) has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all file names with criteria fulfilled """ if not os.path.exists(path=path_name): return [] path_name = path_name.replace('\\', '/') if keyword or ext: keyword = f'*{keyword}*' if keyword else '*' if not ext: ext = '*' files = sort_by_modified([ f.replace('\\', '/') for f in glob.iglob(f'{path_name}/{keyword}.{ext}') if os.path.isfile(f) and (f.replace('\\', '/').split('/')[-1][0] != '~') ]) else: files = sort_by_modified([ f'{path_name}/{f}' for f in os.listdir(path=path_name) if os.path.isfile(f'{path_name}/{f}') and (f[0] != '~') ]) if has_date: files = filter_by_dates(files, date_fmt=date_fmt) return files if full_path else [f.split('/')[-1] for f in files]
[ "Search", "all", "files", "with", "criteria", "Returned", "list", "will", "be", "sorted", "by", "last", "modified" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L52-L91
[ "def", "all_files", "(", "path_name", ",", "keyword", "=", "''", ",", "ext", "=", "''", ",", "full_path", "=", "True", ",", "has_date", "=", "False", ",", "date_fmt", "=", "DATE_FMT", ")", "->", "list", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "path", "=", "path_name", ")", ":", "return", "[", "]", "path_name", "=", "path_name", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "keyword", "or", "ext", ":", "keyword", "=", "f'*{keyword}*'", "if", "keyword", "else", "'*'", "if", "not", "ext", ":", "ext", "=", "'*'", "files", "=", "sort_by_modified", "(", "[", "f", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "for", "f", "in", "glob", ".", "iglob", "(", "f'{path_name}/{keyword}.{ext}'", ")", "if", "os", ".", "path", ".", "isfile", "(", "f", ")", "and", "(", "f", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "[", "0", "]", "!=", "'~'", ")", "]", ")", "else", ":", "files", "=", "sort_by_modified", "(", "[", "f'{path_name}/{f}'", "for", "f", "in", "os", ".", "listdir", "(", "path", "=", "path_name", ")", "if", "os", ".", "path", ".", "isfile", "(", "f'{path_name}/{f}'", ")", "and", "(", "f", "[", "0", "]", "!=", "'~'", ")", "]", ")", "if", "has_date", ":", "files", "=", "filter_by_dates", "(", "files", ",", "date_fmt", "=", "date_fmt", ")", "return", "files", "if", "full_path", "else", "[", "f", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "for", "f", "in", "files", "]" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
all_folders
Search all folders with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all folder names fulfilled criteria
xbbg/io/files.py
def all_folders( path_name, keyword='', has_date=False, date_fmt=DATE_FMT ) -> list: """ Search all folders with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all folder names fulfilled criteria """ if not os.path.exists(path=path_name): return [] path_name = path_name.replace('\\', '/') if keyword: folders = sort_by_modified([ f.replace('\\', '/') for f in glob.iglob(f'{path_name}/*{keyword}*') if os.path.isdir(f) and (f.replace('\\', '/').split('/')[-1][0] != '~') ]) else: folders = sort_by_modified([ f'{path_name}/{f}' for f in os.listdir(path=path_name) if os.path.isdir(f'{path_name}/{f}') and (f[0] != '~') ]) if has_date: folders = filter_by_dates(folders, date_fmt=date_fmt) return folders
def all_folders( path_name, keyword='', has_date=False, date_fmt=DATE_FMT ) -> list: """ Search all folders with criteria Returned list will be sorted by last modified Args: path_name: full path name keyword: keyword to search has_date: whether has date in file name (default False) date_fmt: date format to check for has_date parameter Returns: list: all folder names fulfilled criteria """ if not os.path.exists(path=path_name): return [] path_name = path_name.replace('\\', '/') if keyword: folders = sort_by_modified([ f.replace('\\', '/') for f in glob.iglob(f'{path_name}/*{keyword}*') if os.path.isdir(f) and (f.replace('\\', '/').split('/')[-1][0] != '~') ]) else: folders = sort_by_modified([ f'{path_name}/{f}' for f in os.listdir(path=path_name) if os.path.isdir(f'{path_name}/{f}') and (f[0] != '~') ]) if has_date: folders = filter_by_dates(folders, date_fmt=date_fmt) return folders
[ "Search", "all", "folders", "with", "criteria", "Returned", "list", "will", "be", "sorted", "by", "last", "modified" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L94-L128
[ "def", "all_folders", "(", "path_name", ",", "keyword", "=", "''", ",", "has_date", "=", "False", ",", "date_fmt", "=", "DATE_FMT", ")", "->", "list", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "path", "=", "path_name", ")", ":", "return", "[", "]", "path_name", "=", "path_name", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "keyword", ":", "folders", "=", "sort_by_modified", "(", "[", "f", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "for", "f", "in", "glob", ".", "iglob", "(", "f'{path_name}/*{keyword}*'", ")", "if", "os", ".", "path", ".", "isdir", "(", "f", ")", "and", "(", "f", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", "[", "0", "]", "!=", "'~'", ")", "]", ")", "else", ":", "folders", "=", "sort_by_modified", "(", "[", "f'{path_name}/{f}'", "for", "f", "in", "os", ".", "listdir", "(", "path", "=", "path_name", ")", "if", "os", ".", "path", ".", "isdir", "(", "f'{path_name}/{f}'", ")", "and", "(", "f", "[", "0", "]", "!=", "'~'", ")", "]", ")", "if", "has_date", ":", "folders", "=", "filter_by_dates", "(", "folders", ",", "date_fmt", "=", "date_fmt", ")", "return", "folders" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
sort_by_modified
Sort files or folders by modified time Args: files_or_folders: list of files or folders Returns: list
xbbg/io/files.py
def sort_by_modified(files_or_folders: list) -> list: """ Sort files or folders by modified time Args: files_or_folders: list of files or folders Returns: list """ return sorted(files_or_folders, key=os.path.getmtime, reverse=True)
def sort_by_modified(files_or_folders: list) -> list: """ Sort files or folders by modified time Args: files_or_folders: list of files or folders Returns: list """ return sorted(files_or_folders, key=os.path.getmtime, reverse=True)
[ "Sort", "files", "or", "folders", "by", "modified", "time" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L131-L141
[ "def", "sort_by_modified", "(", "files_or_folders", ":", "list", ")", "->", "list", ":", "return", "sorted", "(", "files_or_folders", ",", "key", "=", "os", ".", "path", ".", "getmtime", ",", "reverse", "=", "True", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
filter_by_dates
Filter files or dates by date patterns Args: files_or_folders: list of files or folders date_fmt: date format Returns: list
xbbg/io/files.py
def filter_by_dates(files_or_folders: list, date_fmt=DATE_FMT) -> list: """ Filter files or dates by date patterns Args: files_or_folders: list of files or folders date_fmt: date format Returns: list """ r = re.compile(f'.*{date_fmt}.*') return list(filter( lambda vv: r.match(vv.replace('\\', '/').split('/')[-1]) is not None, files_or_folders, ))
def filter_by_dates(files_or_folders: list, date_fmt=DATE_FMT) -> list: """ Filter files or dates by date patterns Args: files_or_folders: list of files or folders date_fmt: date format Returns: list """ r = re.compile(f'.*{date_fmt}.*') return list(filter( lambda vv: r.match(vv.replace('\\', '/').split('/')[-1]) is not None, files_or_folders, ))
[ "Filter", "files", "or", "dates", "by", "date", "patterns" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L144-L159
[ "def", "filter_by_dates", "(", "files_or_folders", ":", "list", ",", "date_fmt", "=", "DATE_FMT", ")", "->", "list", ":", "r", "=", "re", ".", "compile", "(", "f'.*{date_fmt}.*'", ")", "return", "list", "(", "filter", "(", "lambda", "vv", ":", "r", ".", "match", "(", "vv", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", ".", "split", "(", "'/'", ")", "[", "-", "1", "]", ")", "is", "not", "None", ",", "files_or_folders", ",", ")", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
file_modified_time
File modified time in python Args: file_name: file name Returns: pd.Timestamp
xbbg/io/files.py
def file_modified_time(file_name) -> pd.Timestamp: """ File modified time in python Args: file_name: file name Returns: pd.Timestamp """ return pd.to_datetime(time.ctime(os.path.getmtime(filename=file_name)))
def file_modified_time(file_name) -> pd.Timestamp: """ File modified time in python Args: file_name: file name Returns: pd.Timestamp """ return pd.to_datetime(time.ctime(os.path.getmtime(filename=file_name)))
[ "File", "modified", "time", "in", "python" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/io/files.py#L191-L201
[ "def", "file_modified_time", "(", "file_name", ")", "->", "pd", ".", "Timestamp", ":", "return", "pd", ".", "to_datetime", "(", "time", ".", "ctime", "(", "os", ".", "path", ".", "getmtime", "(", "filename", "=", "file_name", ")", ")", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
get_interval
Get interval from defined session Args: ticker: ticker session: session Returns: Session of start_time and end_time Examples: >>> get_interval('005490 KS Equity', 'day_open_30') Session(start_time='09:00', end_time='09:30') >>> get_interval('005490 KS Equity', 'day_normal_30_20') Session(start_time='09:31', end_time='15:00') >>> get_interval('005490 KS Equity', 'day_close_20') Session(start_time='15:01', end_time='15:20') >>> get_interval('700 HK Equity', 'am_open_30') Session(start_time='09:30', end_time='10:00') >>> get_interval('700 HK Equity', 'am_normal_30_30') Session(start_time='10:01', end_time='11:30') >>> get_interval('700 HK Equity', 'am_close_30') Session(start_time='11:31', end_time='12:00') >>> get_interval('ES1 Index', 'day_exact_2130_2230') Session(start_time=None, end_time=None) >>> get_interval('ES1 Index', 'allday_exact_2130_2230') Session(start_time='21:30', end_time='22:30') >>> get_interval('ES1 Index', 'allday_exact_2130_0230') Session(start_time='21:30', end_time='02:30') >>> get_interval('AMLP US', 'day_open_30') Session(start_time=None, end_time=None) >>> get_interval('7974 JP Equity', 'day_normal_180_300') is SessNA True >>> get_interval('Z 1 Index', 'allday_normal_30_30') Session(start_time='01:31', end_time='20:30') >>> get_interval('GBP Curncy', 'day') Session(start_time='17:02', end_time='17:00')
xbbg/core/intervals.py
def get_interval(ticker, session) -> Session: """ Get interval from defined session Args: ticker: ticker session: session Returns: Session of start_time and end_time Examples: >>> get_interval('005490 KS Equity', 'day_open_30') Session(start_time='09:00', end_time='09:30') >>> get_interval('005490 KS Equity', 'day_normal_30_20') Session(start_time='09:31', end_time='15:00') >>> get_interval('005490 KS Equity', 'day_close_20') Session(start_time='15:01', end_time='15:20') >>> get_interval('700 HK Equity', 'am_open_30') Session(start_time='09:30', end_time='10:00') >>> get_interval('700 HK Equity', 'am_normal_30_30') Session(start_time='10:01', end_time='11:30') >>> get_interval('700 HK Equity', 'am_close_30') Session(start_time='11:31', end_time='12:00') >>> get_interval('ES1 Index', 'day_exact_2130_2230') Session(start_time=None, end_time=None) >>> get_interval('ES1 Index', 'allday_exact_2130_2230') Session(start_time='21:30', end_time='22:30') >>> get_interval('ES1 Index', 'allday_exact_2130_0230') Session(start_time='21:30', end_time='02:30') >>> get_interval('AMLP US', 'day_open_30') Session(start_time=None, end_time=None) >>> get_interval('7974 JP Equity', 'day_normal_180_300') is SessNA True >>> get_interval('Z 1 Index', 'allday_normal_30_30') Session(start_time='01:31', end_time='20:30') >>> get_interval('GBP Curncy', 'day') Session(start_time='17:02', end_time='17:00') """ if '_' not in session: session = f'{session}_normal_0_0' interval = Intervals(ticker=ticker) ss_info = session.split('_') return getattr(interval, f'market_{ss_info.pop(1)}')(*ss_info)
def get_interval(ticker, session) -> Session: """ Get interval from defined session Args: ticker: ticker session: session Returns: Session of start_time and end_time Examples: >>> get_interval('005490 KS Equity', 'day_open_30') Session(start_time='09:00', end_time='09:30') >>> get_interval('005490 KS Equity', 'day_normal_30_20') Session(start_time='09:31', end_time='15:00') >>> get_interval('005490 KS Equity', 'day_close_20') Session(start_time='15:01', end_time='15:20') >>> get_interval('700 HK Equity', 'am_open_30') Session(start_time='09:30', end_time='10:00') >>> get_interval('700 HK Equity', 'am_normal_30_30') Session(start_time='10:01', end_time='11:30') >>> get_interval('700 HK Equity', 'am_close_30') Session(start_time='11:31', end_time='12:00') >>> get_interval('ES1 Index', 'day_exact_2130_2230') Session(start_time=None, end_time=None) >>> get_interval('ES1 Index', 'allday_exact_2130_2230') Session(start_time='21:30', end_time='22:30') >>> get_interval('ES1 Index', 'allday_exact_2130_0230') Session(start_time='21:30', end_time='02:30') >>> get_interval('AMLP US', 'day_open_30') Session(start_time=None, end_time=None) >>> get_interval('7974 JP Equity', 'day_normal_180_300') is SessNA True >>> get_interval('Z 1 Index', 'allday_normal_30_30') Session(start_time='01:31', end_time='20:30') >>> get_interval('GBP Curncy', 'day') Session(start_time='17:02', end_time='17:00') """ if '_' not in session: session = f'{session}_normal_0_0' interval = Intervals(ticker=ticker) ss_info = session.split('_') return getattr(interval, f'market_{ss_info.pop(1)}')(*ss_info)
[ "Get", "interval", "from", "defined", "session" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L13-L56
[ "def", "get_interval", "(", "ticker", ",", "session", ")", "->", "Session", ":", "if", "'_'", "not", "in", "session", ":", "session", "=", "f'{session}_normal_0_0'", "interval", "=", "Intervals", "(", "ticker", "=", "ticker", ")", "ss_info", "=", "session", ".", "split", "(", "'_'", ")", "return", "getattr", "(", "interval", ",", "f'market_{ss_info.pop(1)}'", ")", "(", "*", "ss_info", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
shift_time
Shift start time by mins Args: start_time: start time in terms of HH:MM string mins: number of minutes (+ / -) Returns: end time in terms of HH:MM string
xbbg/core/intervals.py
def shift_time(start_time, mins) -> str: """ Shift start time by mins Args: start_time: start time in terms of HH:MM string mins: number of minutes (+ / -) Returns: end time in terms of HH:MM string """ s_time = pd.Timestamp(start_time) e_time = s_time + np.sign(mins) * pd.Timedelta(f'00:{abs(mins)}:00') return e_time.strftime('%H:%M')
def shift_time(start_time, mins) -> str: """ Shift start time by mins Args: start_time: start time in terms of HH:MM string mins: number of minutes (+ / -) Returns: end time in terms of HH:MM string """ s_time = pd.Timestamp(start_time) e_time = s_time + np.sign(mins) * pd.Timedelta(f'00:{abs(mins)}:00') return e_time.strftime('%H:%M')
[ "Shift", "start", "time", "by", "mins" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L59-L72
[ "def", "shift_time", "(", "start_time", ",", "mins", ")", "->", "str", ":", "s_time", "=", "pd", ".", "Timestamp", "(", "start_time", ")", "e_time", "=", "s_time", "+", "np", ".", "sign", "(", "mins", ")", "*", "pd", ".", "Timedelta", "(", "f'00:{abs(mins)}:00'", ")", "return", "e_time", ".", "strftime", "(", "'%H:%M'", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
Intervals.market_open
Time intervals for market open Args: session: [allday, day, am, pm, night] mins: mintues after open Returns: Session of start_time and end_time
xbbg/core/intervals.py
def market_open(self, session, mins) -> Session: """ Time intervals for market open Args: session: [allday, day, am, pm, night] mins: mintues after open Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA start_time = self.exch[session][0] return Session(start_time, shift_time(start_time, int(mins)))
def market_open(self, session, mins) -> Session: """ Time intervals for market open Args: session: [allday, day, am, pm, night] mins: mintues after open Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA start_time = self.exch[session][0] return Session(start_time, shift_time(start_time, int(mins)))
[ "Time", "intervals", "for", "market", "open" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L85-L98
[ "def", "market_open", "(", "self", ",", "session", ",", "mins", ")", "->", "Session", ":", "if", "session", "not", "in", "self", ".", "exch", ":", "return", "SessNA", "start_time", "=", "self", ".", "exch", "[", "session", "]", "[", "0", "]", "return", "Session", "(", "start_time", ",", "shift_time", "(", "start_time", ",", "int", "(", "mins", ")", ")", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
Intervals.market_close
Time intervals for market close Args: session: [allday, day, am, pm, night] mins: mintues before close Returns: Session of start_time and end_time
xbbg/core/intervals.py
def market_close(self, session, mins) -> Session: """ Time intervals for market close Args: session: [allday, day, am, pm, night] mins: mintues before close Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA end_time = self.exch[session][-1] return Session(shift_time(end_time, -int(mins) + 1), end_time)
def market_close(self, session, mins) -> Session: """ Time intervals for market close Args: session: [allday, day, am, pm, night] mins: mintues before close Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA end_time = self.exch[session][-1] return Session(shift_time(end_time, -int(mins) + 1), end_time)
[ "Time", "intervals", "for", "market", "close" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L100-L113
[ "def", "market_close", "(", "self", ",", "session", ",", "mins", ")", "->", "Session", ":", "if", "session", "not", "in", "self", ".", "exch", ":", "return", "SessNA", "end_time", "=", "self", ".", "exch", "[", "session", "]", "[", "-", "1", "]", "return", "Session", "(", "shift_time", "(", "end_time", ",", "-", "int", "(", "mins", ")", "+", "1", ")", ",", "end_time", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
Intervals.market_normal
Time intervals between market Args: session: [allday, day, am, pm, night] after_open: mins after open before_close: mins before close Returns: Session of start_time and end_time
xbbg/core/intervals.py
def market_normal(self, session, after_open, before_close) -> Session: """ Time intervals between market Args: session: [allday, day, am, pm, night] after_open: mins after open before_close: mins before close Returns: Session of start_time and end_time """ logger = logs.get_logger(self.market_normal) if session not in self.exch: return SessNA ss = self.exch[session] s_time = shift_time(ss[0], int(after_open) + 1) e_time = shift_time(ss[-1], -int(before_close)) request_cross = pd.Timestamp(s_time) >= pd.Timestamp(e_time) session_cross = pd.Timestamp(ss[0]) >= pd.Timestamp(ss[1]) if request_cross and (not session_cross): logger.warning(f'end time {e_time} is earlier than {s_time} ...') return SessNA return Session(s_time, e_time)
def market_normal(self, session, after_open, before_close) -> Session: """ Time intervals between market Args: session: [allday, day, am, pm, night] after_open: mins after open before_close: mins before close Returns: Session of start_time and end_time """ logger = logs.get_logger(self.market_normal) if session not in self.exch: return SessNA ss = self.exch[session] s_time = shift_time(ss[0], int(after_open) + 1) e_time = shift_time(ss[-1], -int(before_close)) request_cross = pd.Timestamp(s_time) >= pd.Timestamp(e_time) session_cross = pd.Timestamp(ss[0]) >= pd.Timestamp(ss[1]) if request_cross and (not session_cross): logger.warning(f'end time {e_time} is earlier than {s_time} ...') return SessNA return Session(s_time, e_time)
[ "Time", "intervals", "between", "market" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L115-L141
[ "def", "market_normal", "(", "self", ",", "session", ",", "after_open", ",", "before_close", ")", "->", "Session", ":", "logger", "=", "logs", ".", "get_logger", "(", "self", ".", "market_normal", ")", "if", "session", "not", "in", "self", ".", "exch", ":", "return", "SessNA", "ss", "=", "self", ".", "exch", "[", "session", "]", "s_time", "=", "shift_time", "(", "ss", "[", "0", "]", ",", "int", "(", "after_open", ")", "+", "1", ")", "e_time", "=", "shift_time", "(", "ss", "[", "-", "1", "]", ",", "-", "int", "(", "before_close", ")", ")", "request_cross", "=", "pd", ".", "Timestamp", "(", "s_time", ")", ">=", "pd", ".", "Timestamp", "(", "e_time", ")", "session_cross", "=", "pd", ".", "Timestamp", "(", "ss", "[", "0", "]", ")", ">=", "pd", ".", "Timestamp", "(", "ss", "[", "1", "]", ")", "if", "request_cross", "and", "(", "not", "session_cross", ")", ":", "logger", ".", "warning", "(", "f'end time {e_time} is earlier than {s_time} ...'", ")", "return", "SessNA", "return", "Session", "(", "s_time", ",", "e_time", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
Intervals.market_exact
Explicitly specify start time and end time Args: session: predefined session start_time: start time in terms of HHMM string end_time: end time in terms of HHMM string Returns: Session of start_time and end_time
xbbg/core/intervals.py
def market_exact(self, session, start_time: str, end_time: str) -> Session: """ Explicitly specify start time and end time Args: session: predefined session start_time: start time in terms of HHMM string end_time: end time in terms of HHMM string Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA ss = self.exch[session] same_day = ss[0] < ss[-1] if not start_time: s_time = ss[0] else: s_time = param.to_hour(start_time) if same_day: s_time = max(s_time, ss[0]) if not end_time: e_time = ss[-1] else: e_time = param.to_hour(end_time) if same_day: e_time = min(e_time, ss[-1]) if same_day and (s_time > e_time): return SessNA return Session(start_time=s_time, end_time=e_time)
def market_exact(self, session, start_time: str, end_time: str) -> Session: """ Explicitly specify start time and end time Args: session: predefined session start_time: start time in terms of HHMM string end_time: end time in terms of HHMM string Returns: Session of start_time and end_time """ if session not in self.exch: return SessNA ss = self.exch[session] same_day = ss[0] < ss[-1] if not start_time: s_time = ss[0] else: s_time = param.to_hour(start_time) if same_day: s_time = max(s_time, ss[0]) if not end_time: e_time = ss[-1] else: e_time = param.to_hour(end_time) if same_day: e_time = min(e_time, ss[-1]) if same_day and (s_time > e_time): return SessNA return Session(start_time=s_time, end_time=e_time)
[ "Explicitly", "specify", "start", "time", "and", "end", "time" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/intervals.py#L143-L171
[ "def", "market_exact", "(", "self", ",", "session", ",", "start_time", ":", "str", ",", "end_time", ":", "str", ")", "->", "Session", ":", "if", "session", "not", "in", "self", ".", "exch", ":", "return", "SessNA", "ss", "=", "self", ".", "exch", "[", "session", "]", "same_day", "=", "ss", "[", "0", "]", "<", "ss", "[", "-", "1", "]", "if", "not", "start_time", ":", "s_time", "=", "ss", "[", "0", "]", "else", ":", "s_time", "=", "param", ".", "to_hour", "(", "start_time", ")", "if", "same_day", ":", "s_time", "=", "max", "(", "s_time", ",", "ss", "[", "0", "]", ")", "if", "not", "end_time", ":", "e_time", "=", "ss", "[", "-", "1", "]", "else", ":", "e_time", "=", "param", ".", "to_hour", "(", "end_time", ")", "if", "same_day", ":", "e_time", "=", "min", "(", "e_time", ",", "ss", "[", "-", "1", "]", ")", "if", "same_day", "and", "(", "s_time", ">", "e_time", ")", ":", "return", "SessNA", "return", "Session", "(", "start_time", "=", "s_time", ",", "end_time", "=", "e_time", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
get_tz
Convert tz from ticker / shorthands to timezone Args: tz: ticker or timezone shorthands Returns: str: Python timzone Examples: >>> get_tz('NY') 'America/New_York' >>> get_tz(TimeZone.NY) 'America/New_York' >>> get_tz('BHP AU Equity') 'Australia/Sydney'
xbbg/core/timezone.py
def get_tz(tz) -> str: """ Convert tz from ticker / shorthands to timezone Args: tz: ticker or timezone shorthands Returns: str: Python timzone Examples: >>> get_tz('NY') 'America/New_York' >>> get_tz(TimeZone.NY) 'America/New_York' >>> get_tz('BHP AU Equity') 'Australia/Sydney' """ from xbbg.const import exch_info if tz is None: return DEFAULT_TZ to_tz = tz if isinstance(tz, str): if hasattr(TimeZone, tz): to_tz = getattr(TimeZone, tz) else: exch = exch_info(ticker=tz) if 'tz' in exch.index: to_tz = exch.tz return to_tz
def get_tz(tz) -> str: """ Convert tz from ticker / shorthands to timezone Args: tz: ticker or timezone shorthands Returns: str: Python timzone Examples: >>> get_tz('NY') 'America/New_York' >>> get_tz(TimeZone.NY) 'America/New_York' >>> get_tz('BHP AU Equity') 'Australia/Sydney' """ from xbbg.const import exch_info if tz is None: return DEFAULT_TZ to_tz = tz if isinstance(tz, str): if hasattr(TimeZone, tz): to_tz = getattr(TimeZone, tz) else: exch = exch_info(ticker=tz) if 'tz' in exch.index: to_tz = exch.tz return to_tz
[ "Convert", "tz", "from", "ticker", "/", "shorthands", "to", "timezone" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/timezone.py#L11-L42
[ "def", "get_tz", "(", "tz", ")", "->", "str", ":", "from", "xbbg", ".", "const", "import", "exch_info", "if", "tz", "is", "None", ":", "return", "DEFAULT_TZ", "to_tz", "=", "tz", "if", "isinstance", "(", "tz", ",", "str", ")", ":", "if", "hasattr", "(", "TimeZone", ",", "tz", ")", ":", "to_tz", "=", "getattr", "(", "TimeZone", ",", "tz", ")", "else", ":", "exch", "=", "exch_info", "(", "ticker", "=", "tz", ")", "if", "'tz'", "in", "exch", ".", "index", ":", "to_tz", "=", "exch", ".", "tz", "return", "to_tz" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
tz_convert
Convert to tz Args: dt: date time to_tz: to tz from_tz: from tz - will be ignored if tz from dt is given Returns: str: date & time Examples: >>> dt_1 = pd.Timestamp('2018-09-10 16:00', tz='Asia/Hong_Kong') >>> tz_convert(dt_1, to_tz='NY') '2018-09-10 04:00:00-04:00' >>> dt_2 = pd.Timestamp('2018-01-10 16:00') >>> tz_convert(dt_2, to_tz='HK', from_tz='NY') '2018-01-11 05:00:00+08:00' >>> dt_3 = '2018-09-10 15:00' >>> tz_convert(dt_3, to_tz='NY', from_tz='JP') '2018-09-10 02:00:00-04:00'
xbbg/core/timezone.py
def tz_convert(dt, to_tz, from_tz=None) -> str: """ Convert to tz Args: dt: date time to_tz: to tz from_tz: from tz - will be ignored if tz from dt is given Returns: str: date & time Examples: >>> dt_1 = pd.Timestamp('2018-09-10 16:00', tz='Asia/Hong_Kong') >>> tz_convert(dt_1, to_tz='NY') '2018-09-10 04:00:00-04:00' >>> dt_2 = pd.Timestamp('2018-01-10 16:00') >>> tz_convert(dt_2, to_tz='HK', from_tz='NY') '2018-01-11 05:00:00+08:00' >>> dt_3 = '2018-09-10 15:00' >>> tz_convert(dt_3, to_tz='NY', from_tz='JP') '2018-09-10 02:00:00-04:00' """ logger = logs.get_logger(tz_convert, level='info') f_tz, t_tz = get_tz(from_tz), get_tz(to_tz) from_dt = pd.Timestamp(str(dt), tz=f_tz) logger.debug(f'converting {str(from_dt)} from {f_tz} to {t_tz} ...') return str(pd.Timestamp(str(from_dt), tz=t_tz))
def tz_convert(dt, to_tz, from_tz=None) -> str: """ Convert to tz Args: dt: date time to_tz: to tz from_tz: from tz - will be ignored if tz from dt is given Returns: str: date & time Examples: >>> dt_1 = pd.Timestamp('2018-09-10 16:00', tz='Asia/Hong_Kong') >>> tz_convert(dt_1, to_tz='NY') '2018-09-10 04:00:00-04:00' >>> dt_2 = pd.Timestamp('2018-01-10 16:00') >>> tz_convert(dt_2, to_tz='HK', from_tz='NY') '2018-01-11 05:00:00+08:00' >>> dt_3 = '2018-09-10 15:00' >>> tz_convert(dt_3, to_tz='NY', from_tz='JP') '2018-09-10 02:00:00-04:00' """ logger = logs.get_logger(tz_convert, level='info') f_tz, t_tz = get_tz(from_tz), get_tz(to_tz) from_dt = pd.Timestamp(str(dt), tz=f_tz) logger.debug(f'converting {str(from_dt)} from {f_tz} to {t_tz} ...') return str(pd.Timestamp(str(from_dt), tz=t_tz))
[ "Convert", "to", "tz" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/timezone.py#L45-L73
[ "def", "tz_convert", "(", "dt", ",", "to_tz", ",", "from_tz", "=", "None", ")", "->", "str", ":", "logger", "=", "logs", ".", "get_logger", "(", "tz_convert", ",", "level", "=", "'info'", ")", "f_tz", ",", "t_tz", "=", "get_tz", "(", "from_tz", ")", ",", "get_tz", "(", "to_tz", ")", "from_dt", "=", "pd", ".", "Timestamp", "(", "str", "(", "dt", ")", ",", "tz", "=", "f_tz", ")", "logger", ".", "debug", "(", "f'converting {str(from_dt)} from {f_tz} to {t_tz} ...'", ")", "return", "str", "(", "pd", ".", "Timestamp", "(", "str", "(", "from_dt", ")", ",", "tz", "=", "t_tz", ")", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
missing_info
Full infomation for missing query
xbbg/core/missing.py
def missing_info(**kwargs) -> str: """ Full infomation for missing query """ func = kwargs.pop('func', 'unknown') if 'ticker' in kwargs: kwargs['ticker'] = kwargs['ticker'].replace('/', '_') info = utils.to_str(kwargs, fmt='{value}', sep='/')[1:-1] return f'{func}/{info}'
def missing_info(**kwargs) -> str: """ Full infomation for missing query """ func = kwargs.pop('func', 'unknown') if 'ticker' in kwargs: kwargs['ticker'] = kwargs['ticker'].replace('/', '_') info = utils.to_str(kwargs, fmt='{value}', sep='/')[1:-1] return f'{func}/{info}'
[ "Full", "infomation", "for", "missing", "query" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/missing.py#L8-L15
[ "def", "missing_info", "(", "*", "*", "kwargs", ")", "->", "str", ":", "func", "=", "kwargs", ".", "pop", "(", "'func'", ",", "'unknown'", ")", "if", "'ticker'", "in", "kwargs", ":", "kwargs", "[", "'ticker'", "]", "=", "kwargs", "[", "'ticker'", "]", ".", "replace", "(", "'/'", ",", "'_'", ")", "info", "=", "utils", ".", "to_str", "(", "kwargs", ",", "fmt", "=", "'{value}'", ",", "sep", "=", "'/'", ")", "[", "1", ":", "-", "1", "]", "return", "f'{func}/{info}'" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
current_missing
Check number of trials for missing values Returns: int: number of trials already tried
xbbg/core/missing.py
def current_missing(**kwargs) -> int: """ Check number of trials for missing values Returns: int: number of trials already tried """ data_path = os.environ.get(BBG_ROOT, '').replace('\\', '/') if not data_path: return 0 return len(files.all_files(f'{data_path}/Logs/{missing_info(**kwargs)}'))
def current_missing(**kwargs) -> int: """ Check number of trials for missing values Returns: int: number of trials already tried """ data_path = os.environ.get(BBG_ROOT, '').replace('\\', '/') if not data_path: return 0 return len(files.all_files(f'{data_path}/Logs/{missing_info(**kwargs)}'))
[ "Check", "number", "of", "trials", "for", "missing", "values" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/missing.py#L18-L27
[ "def", "current_missing", "(", "*", "*", "kwargs", ")", "->", "int", ":", "data_path", "=", "os", ".", "environ", ".", "get", "(", "BBG_ROOT", ",", "''", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "not", "data_path", ":", "return", "0", "return", "len", "(", "files", ".", "all_files", "(", "f'{data_path}/Logs/{missing_info(**kwargs)}'", ")", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
update_missing
Update number of trials for missing values
xbbg/core/missing.py
def update_missing(**kwargs): """ Update number of trials for missing values """ data_path = os.environ.get(BBG_ROOT, '').replace('\\', '/') if not data_path: return if len(kwargs) == 0: return log_path = f'{data_path}/Logs/{missing_info(**kwargs)}' cnt = len(files.all_files(log_path)) + 1 files.create_folder(log_path) open(f'{log_path}/{cnt}.log', 'a').close()
def update_missing(**kwargs): """ Update number of trials for missing values """ data_path = os.environ.get(BBG_ROOT, '').replace('\\', '/') if not data_path: return if len(kwargs) == 0: return log_path = f'{data_path}/Logs/{missing_info(**kwargs)}' cnt = len(files.all_files(log_path)) + 1 files.create_folder(log_path) open(f'{log_path}/{cnt}.log', 'a').close()
[ "Update", "number", "of", "trials", "for", "missing", "values" ]
alpha-xone/xbbg
python
https://github.com/alpha-xone/xbbg/blob/70226eb19a72a08144b5d8cea9db4913200f7bc5/xbbg/core/missing.py#L30-L42
[ "def", "update_missing", "(", "*", "*", "kwargs", ")", ":", "data_path", "=", "os", ".", "environ", ".", "get", "(", "BBG_ROOT", ",", "''", ")", ".", "replace", "(", "'\\\\'", ",", "'/'", ")", "if", "not", "data_path", ":", "return", "if", "len", "(", "kwargs", ")", "==", "0", ":", "return", "log_path", "=", "f'{data_path}/Logs/{missing_info(**kwargs)}'", "cnt", "=", "len", "(", "files", ".", "all_files", "(", "log_path", ")", ")", "+", "1", "files", ".", "create_folder", "(", "log_path", ")", "open", "(", "f'{log_path}/{cnt}.log'", ",", "'a'", ")", ".", "close", "(", ")" ]
70226eb19a72a08144b5d8cea9db4913200f7bc5
valid
public
Decorator for public views that do not require authentication Sets an attribute in the fuction STRONGHOLD_IS_PUBLIC to True
stronghold/decorators.py
def public(function): """ Decorator for public views that do not require authentication Sets an attribute in the fuction STRONGHOLD_IS_PUBLIC to True """ orig_func = function while isinstance(orig_func, partial): orig_func = orig_func.func set_view_func_public(orig_func) return function
def public(function): """ Decorator for public views that do not require authentication Sets an attribute in the fuction STRONGHOLD_IS_PUBLIC to True """ orig_func = function while isinstance(orig_func, partial): orig_func = orig_func.func set_view_func_public(orig_func) return function
[ "Decorator", "for", "public", "views", "that", "do", "not", "require", "authentication", "Sets", "an", "attribute", "in", "the", "fuction", "STRONGHOLD_IS_PUBLIC", "to", "True" ]
mgrouchy/django-stronghold
python
https://github.com/mgrouchy/django-stronghold/blob/4b1e0dd7118fb3c2398f7d3bc93467f907b2bf67/stronghold/decorators.py#L5-L15
[ "def", "public", "(", "function", ")", ":", "orig_func", "=", "function", "while", "isinstance", "(", "orig_func", ",", "partial", ")", ":", "orig_func", "=", "orig_func", ".", "func", "set_view_func_public", "(", "orig_func", ")", "return", "function" ]
4b1e0dd7118fb3c2398f7d3bc93467f907b2bf67
valid
custom_req
Utility for sending a predefined request and printing response as well as storing messages in a list, useful for testing Parameters ---------- session: blpapi.session.Session request: blpapi.request.Request Request to be sent Returns ------- List of all messages received
pdblp/utils.py
def custom_req(session, request): """ Utility for sending a predefined request and printing response as well as storing messages in a list, useful for testing Parameters ---------- session: blpapi.session.Session request: blpapi.request.Request Request to be sent Returns ------- List of all messages received """ # flush event queue in case previous call errored out while(session.tryNextEvent()): pass print("Sending Request:\n %s" % request) session.sendRequest(request) messages = [] # Process received events while(True): # We provide timeout to give the chance for Ctrl+C handling: ev = session.nextEvent(500) for msg in ev: print("Message Received:\n %s" % msg) messages.append(msg) if ev.eventType() == blpapi.Event.RESPONSE: # Response completely received, so we could exit break return messages
def custom_req(session, request): """ Utility for sending a predefined request and printing response as well as storing messages in a list, useful for testing Parameters ---------- session: blpapi.session.Session request: blpapi.request.Request Request to be sent Returns ------- List of all messages received """ # flush event queue in case previous call errored out while(session.tryNextEvent()): pass print("Sending Request:\n %s" % request) session.sendRequest(request) messages = [] # Process received events while(True): # We provide timeout to give the chance for Ctrl+C handling: ev = session.nextEvent(500) for msg in ev: print("Message Received:\n %s" % msg) messages.append(msg) if ev.eventType() == blpapi.Event.RESPONSE: # Response completely received, so we could exit break return messages
[ "Utility", "for", "sending", "a", "predefined", "request", "and", "printing", "response", "as", "well", "as", "storing", "messages", "in", "a", "list", "useful", "for", "testing" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/utils.py#L4-L36
[ "def", "custom_req", "(", "session", ",", "request", ")", ":", "# flush event queue in case previous call errored out", "while", "(", "session", ".", "tryNextEvent", "(", ")", ")", ":", "pass", "print", "(", "\"Sending Request:\\n %s\"", "%", "request", ")", "session", ".", "sendRequest", "(", "request", ")", "messages", "=", "[", "]", "# Process received events", "while", "(", "True", ")", ":", "# We provide timeout to give the chance for Ctrl+C handling:", "ev", "=", "session", ".", "nextEvent", "(", "500", ")", "for", "msg", "in", "ev", ":", "print", "(", "\"Message Received:\\n %s\"", "%", "msg", ")", "messages", ".", "append", "(", "msg", ")", "if", "ev", ".", "eventType", "(", ")", "==", "blpapi", ".", "Event", ".", "RESPONSE", ":", "# Response completely received, so we could exit", "break", "return", "messages" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
to_dict_list
Translate a string representation of a Bloomberg Open API Request/Response into a list of dictionaries.return Parameters ---------- mystr: str A string representation of one or more blpapi.request.Request or blp.message.Message, these should be '\\n' seperated
pdblp/parser.py
def to_dict_list(mystr): """ Translate a string representation of a Bloomberg Open API Request/Response into a list of dictionaries.return Parameters ---------- mystr: str A string representation of one or more blpapi.request.Request or blp.message.Message, these should be '\\n' seperated """ res = _parse(mystr) dicts = [] for res_dict in res: dicts.append(res_dict.asDict()) return dicts
def to_dict_list(mystr): """ Translate a string representation of a Bloomberg Open API Request/Response into a list of dictionaries.return Parameters ---------- mystr: str A string representation of one or more blpapi.request.Request or blp.message.Message, these should be '\\n' seperated """ res = _parse(mystr) dicts = [] for res_dict in res: dicts.append(res_dict.asDict()) return dicts
[ "Translate", "a", "string", "representation", "of", "a", "Bloomberg", "Open", "API", "Request", "/", "Response", "into", "a", "list", "of", "dictionaries", ".", "return" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/parser.py#L51-L66
[ "def", "to_dict_list", "(", "mystr", ")", ":", "res", "=", "_parse", "(", "mystr", ")", "dicts", "=", "[", "]", "for", "res_dict", "in", "res", ":", "dicts", ".", "append", "(", "res_dict", ".", "asDict", "(", ")", ")", "return", "dicts" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
bopen
Open and manage a BCon wrapper to a Bloomberg API session Parameters ---------- **kwargs: Keyword arguments passed into pdblp.BCon initialization
pdblp/pdblp.py
def bopen(**kwargs): """ Open and manage a BCon wrapper to a Bloomberg API session Parameters ---------- **kwargs: Keyword arguments passed into pdblp.BCon initialization """ con = BCon(**kwargs) con.start() try: yield con finally: con.stop()
def bopen(**kwargs): """ Open and manage a BCon wrapper to a Bloomberg API session Parameters ---------- **kwargs: Keyword arguments passed into pdblp.BCon initialization """ con = BCon(**kwargs) con.start() try: yield con finally: con.stop()
[ "Open", "and", "manage", "a", "BCon", "wrapper", "to", "a", "Bloomberg", "API", "session" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L40-L54
[ "def", "bopen", "(", "*", "*", "kwargs", ")", ":", "con", "=", "BCon", "(", "*", "*", "kwargs", ")", "con", ".", "start", "(", ")", "try", ":", "yield", "con", "finally", ":", "con", ".", "stop", "(", ")" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.start
Start connection and initialize session services
pdblp/pdblp.py
def start(self): """ Start connection and initialize session services """ # flush event queue in defensive way logger = _get_logger(self.debug) started = self._session.start() if started: ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SESSION_STATUS: raise RuntimeError('Expected a "SESSION_STATUS" event but ' 'received a {!r}'.format(ev_name)) ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SESSION_STATUS: raise RuntimeError('Expected a "SESSION_STATUS" event but ' 'received a {!r}'.format(ev_name)) else: ev = self._session.nextEvent(self.timeout) if ev.eventType() == blpapi.Event.SESSION_STATUS: for msg in ev: logger.warning('Message Received:\n{}'.format(msg)) raise ConnectionError('Could not start blpapi.Session') self._init_services() return self
def start(self): """ Start connection and initialize session services """ # flush event queue in defensive way logger = _get_logger(self.debug) started = self._session.start() if started: ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SESSION_STATUS: raise RuntimeError('Expected a "SESSION_STATUS" event but ' 'received a {!r}'.format(ev_name)) ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SESSION_STATUS: raise RuntimeError('Expected a "SESSION_STATUS" event but ' 'received a {!r}'.format(ev_name)) else: ev = self._session.nextEvent(self.timeout) if ev.eventType() == blpapi.Event.SESSION_STATUS: for msg in ev: logger.warning('Message Received:\n{}'.format(msg)) raise ConnectionError('Could not start blpapi.Session') self._init_services() return self
[ "Start", "connection", "and", "initialize", "session", "services" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L117-L149
[ "def", "start", "(", "self", ")", ":", "# flush event queue in defensive way", "logger", "=", "_get_logger", "(", "self", ".", "debug", ")", "started", "=", "self", ".", "_session", ".", "start", "(", ")", "if", "started", ":", "ev", "=", "self", ".", "_session", ".", "nextEvent", "(", ")", "ev_name", "=", "_EVENT_DICT", "[", "ev", ".", "eventType", "(", ")", "]", "logger", ".", "info", "(", "'Event Type: {!r}'", ".", "format", "(", "ev_name", ")", ")", "for", "msg", "in", "ev", ":", "logger", ".", "info", "(", "'Message Received:\\n{}'", ".", "format", "(", "msg", ")", ")", "if", "ev", ".", "eventType", "(", ")", "!=", "blpapi", ".", "Event", ".", "SESSION_STATUS", ":", "raise", "RuntimeError", "(", "'Expected a \"SESSION_STATUS\" event but '", "'received a {!r}'", ".", "format", "(", "ev_name", ")", ")", "ev", "=", "self", ".", "_session", ".", "nextEvent", "(", ")", "ev_name", "=", "_EVENT_DICT", "[", "ev", ".", "eventType", "(", ")", "]", "logger", ".", "info", "(", "'Event Type: {!r}'", ".", "format", "(", "ev_name", ")", ")", "for", "msg", "in", "ev", ":", "logger", ".", "info", "(", "'Message Received:\\n{}'", ".", "format", "(", "msg", ")", ")", "if", "ev", ".", "eventType", "(", ")", "!=", "blpapi", ".", "Event", ".", "SESSION_STATUS", ":", "raise", "RuntimeError", "(", "'Expected a \"SESSION_STATUS\" event but '", "'received a {!r}'", ".", "format", "(", "ev_name", ")", ")", "else", ":", "ev", "=", "self", ".", "_session", ".", "nextEvent", "(", "self", ".", "timeout", ")", "if", "ev", ".", "eventType", "(", ")", "==", "blpapi", ".", "Event", ".", "SESSION_STATUS", ":", "for", "msg", "in", "ev", ":", "logger", ".", "warning", "(", "'Message Received:\\n{}'", ".", "format", "(", "msg", ")", ")", "raise", "ConnectionError", "(", "'Could not start blpapi.Session'", ")", "self", ".", "_init_services", "(", ")", "return", "self" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon._init_services
Initialize blpapi.Session services
pdblp/pdblp.py
def _init_services(self): """ Initialize blpapi.Session services """ logger = _get_logger(self.debug) # flush event queue in defensive way opened = self._session.openService('//blp/refdata') ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SERVICE_STATUS: raise RuntimeError('Expected a "SERVICE_STATUS" event but ' 'received a {!r}'.format(ev_name)) if not opened: logger.warning('Failed to open //blp/refdata') raise ConnectionError('Could not open a //blp/refdata service') self.refDataService = self._session.getService('//blp/refdata') opened = self._session.openService('//blp/exrsvc') ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SERVICE_STATUS: raise RuntimeError('Expected a "SERVICE_STATUS" event but ' 'received a {!r}'.format(ev_name)) if not opened: logger.warning('Failed to open //blp/exrsvc') raise ConnectionError('Could not open a //blp/exrsvc service') self.exrService = self._session.getService('//blp/exrsvc') return self
def _init_services(self): """ Initialize blpapi.Session services """ logger = _get_logger(self.debug) # flush event queue in defensive way opened = self._session.openService('//blp/refdata') ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SERVICE_STATUS: raise RuntimeError('Expected a "SERVICE_STATUS" event but ' 'received a {!r}'.format(ev_name)) if not opened: logger.warning('Failed to open //blp/refdata') raise ConnectionError('Could not open a //blp/refdata service') self.refDataService = self._session.getService('//blp/refdata') opened = self._session.openService('//blp/exrsvc') ev = self._session.nextEvent() ev_name = _EVENT_DICT[ev.eventType()] logger.info('Event Type: {!r}'.format(ev_name)) for msg in ev: logger.info('Message Received:\n{}'.format(msg)) if ev.eventType() != blpapi.Event.SERVICE_STATUS: raise RuntimeError('Expected a "SERVICE_STATUS" event but ' 'received a {!r}'.format(ev_name)) if not opened: logger.warning('Failed to open //blp/exrsvc') raise ConnectionError('Could not open a //blp/exrsvc service') self.exrService = self._session.getService('//blp/exrsvc') return self
[ "Initialize", "blpapi", ".", "Session", "services" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L151-L186
[ "def", "_init_services", "(", "self", ")", ":", "logger", "=", "_get_logger", "(", "self", ".", "debug", ")", "# flush event queue in defensive way", "opened", "=", "self", ".", "_session", ".", "openService", "(", "'//blp/refdata'", ")", "ev", "=", "self", ".", "_session", ".", "nextEvent", "(", ")", "ev_name", "=", "_EVENT_DICT", "[", "ev", ".", "eventType", "(", ")", "]", "logger", ".", "info", "(", "'Event Type: {!r}'", ".", "format", "(", "ev_name", ")", ")", "for", "msg", "in", "ev", ":", "logger", ".", "info", "(", "'Message Received:\\n{}'", ".", "format", "(", "msg", ")", ")", "if", "ev", ".", "eventType", "(", ")", "!=", "blpapi", ".", "Event", ".", "SERVICE_STATUS", ":", "raise", "RuntimeError", "(", "'Expected a \"SERVICE_STATUS\" event but '", "'received a {!r}'", ".", "format", "(", "ev_name", ")", ")", "if", "not", "opened", ":", "logger", ".", "warning", "(", "'Failed to open //blp/refdata'", ")", "raise", "ConnectionError", "(", "'Could not open a //blp/refdata service'", ")", "self", ".", "refDataService", "=", "self", ".", "_session", ".", "getService", "(", "'//blp/refdata'", ")", "opened", "=", "self", ".", "_session", ".", "openService", "(", "'//blp/exrsvc'", ")", "ev", "=", "self", ".", "_session", ".", "nextEvent", "(", ")", "ev_name", "=", "_EVENT_DICT", "[", "ev", ".", "eventType", "(", ")", "]", "logger", ".", "info", "(", "'Event Type: {!r}'", ".", "format", "(", "ev_name", ")", ")", "for", "msg", "in", "ev", ":", "logger", ".", "info", "(", "'Message Received:\\n{}'", ".", "format", "(", "msg", ")", ")", "if", "ev", ".", "eventType", "(", ")", "!=", "blpapi", ".", "Event", ".", "SERVICE_STATUS", ":", "raise", "RuntimeError", "(", "'Expected a \"SERVICE_STATUS\" event but '", "'received a {!r}'", ".", "format", "(", "ev_name", ")", ")", "if", "not", "opened", ":", "logger", ".", "warning", "(", "'Failed to open //blp/exrsvc'", ")", "raise", "ConnectionError", "(", "'Could not open a //blp/exrsvc service'", ")", "self", ".", "exrService", "=", "self", ".", "_session", ".", "getService", "(", "'//blp/exrsvc'", ")", "return", "self" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.bdh
Get tickers and fields, return pandas DataFrame with columns as MultiIndex with levels "ticker" and "field" and indexed by "date". If long data is requested return DataFrame with columns ["date", "ticker", "field", "value"]. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS start_date: string String in format YYYYmmdd end_date: string String in format YYYYmmdd elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set, e.g. [("periodicityAdjustment", "ACTUAL")]. Refer to the HistoricalDataRequest section in the 'Services & schemas reference guide' for more info on these values ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value longdata: boolean Whether data should be returned in long data format or pivoted
pdblp/pdblp.py
def bdh(self, tickers, flds, start_date, end_date, elms=None, ovrds=None, longdata=False): """ Get tickers and fields, return pandas DataFrame with columns as MultiIndex with levels "ticker" and "field" and indexed by "date". If long data is requested return DataFrame with columns ["date", "ticker", "field", "value"]. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS start_date: string String in format YYYYmmdd end_date: string String in format YYYYmmdd elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set, e.g. [("periodicityAdjustment", "ACTUAL")]. Refer to the HistoricalDataRequest section in the 'Services & schemas reference guide' for more info on these values ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value longdata: boolean Whether data should be returned in long data format or pivoted """ ovrds = [] if not ovrds else ovrds elms = [] if not elms else elms elms = list(elms) data = self._bdh_list(tickers, flds, start_date, end_date, elms, ovrds) df = pd.DataFrame(data, columns=['date', 'ticker', 'field', 'value']) df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date']) if not longdata: cols = ['ticker', 'field'] df = df.set_index(['date'] + cols).unstack(cols) df.columns = df.columns.droplevel(0) return df
def bdh(self, tickers, flds, start_date, end_date, elms=None, ovrds=None, longdata=False): """ Get tickers and fields, return pandas DataFrame with columns as MultiIndex with levels "ticker" and "field" and indexed by "date". If long data is requested return DataFrame with columns ["date", "ticker", "field", "value"]. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS start_date: string String in format YYYYmmdd end_date: string String in format YYYYmmdd elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set, e.g. [("periodicityAdjustment", "ACTUAL")]. Refer to the HistoricalDataRequest section in the 'Services & schemas reference guide' for more info on these values ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value longdata: boolean Whether data should be returned in long data format or pivoted """ ovrds = [] if not ovrds else ovrds elms = [] if not elms else elms elms = list(elms) data = self._bdh_list(tickers, flds, start_date, end_date, elms, ovrds) df = pd.DataFrame(data, columns=['date', 'ticker', 'field', 'value']) df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date']) if not longdata: cols = ['ticker', 'field'] df = df.set_index(['date'] + cols).unstack(cols) df.columns = df.columns.droplevel(0) return df
[ "Get", "tickers", "and", "fields", "return", "pandas", "DataFrame", "with", "columns", "as", "MultiIndex", "with", "levels", "ticker", "and", "field", "and", "indexed", "by", "date", ".", "If", "long", "data", "is", "requested", "return", "DataFrame", "with", "columns", "[", "date", "ticker", "field", "value", "]", "." ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L240-L284
[ "def", "bdh", "(", "self", ",", "tickers", ",", "flds", ",", "start_date", ",", "end_date", ",", "elms", "=", "None", ",", "ovrds", "=", "None", ",", "longdata", "=", "False", ")", ":", "ovrds", "=", "[", "]", "if", "not", "ovrds", "else", "ovrds", "elms", "=", "[", "]", "if", "not", "elms", "else", "elms", "elms", "=", "list", "(", "elms", ")", "data", "=", "self", ".", "_bdh_list", "(", "tickers", ",", "flds", ",", "start_date", ",", "end_date", ",", "elms", ",", "ovrds", ")", "df", "=", "pd", ".", "DataFrame", "(", "data", ",", "columns", "=", "[", "'date'", ",", "'ticker'", ",", "'field'", ",", "'value'", "]", ")", "df", ".", "loc", "[", ":", ",", "'date'", "]", "=", "pd", ".", "to_datetime", "(", "df", ".", "loc", "[", ":", ",", "'date'", "]", ")", "if", "not", "longdata", ":", "cols", "=", "[", "'ticker'", ",", "'field'", "]", "df", "=", "df", ".", "set_index", "(", "[", "'date'", "]", "+", "cols", ")", ".", "unstack", "(", "cols", ")", "df", ".", "columns", "=", "df", ".", "columns", ".", "droplevel", "(", "0", ")", "return", "df" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.ref
Make a reference data request, get tickers and fields, return long pandas DataFrame with columns [ticker, field, value] Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> con.ref("CL1 Comdty", ["FUT_GEN_MONTH"]) Notes ----- This returns reference data which has singleton values. In raw format the messages passed back contain data of the form fieldData = { FUT_GEN_MONTH = "FGHJKMNQUVXZ" }
pdblp/pdblp.py
def ref(self, tickers, flds, ovrds=None): """ Make a reference data request, get tickers and fields, return long pandas DataFrame with columns [ticker, field, value] Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> con.ref("CL1 Comdty", ["FUT_GEN_MONTH"]) Notes ----- This returns reference data which has singleton values. In raw format the messages passed back contain data of the form fieldData = { FUT_GEN_MONTH = "FGHJKMNQUVXZ" } """ ovrds = [] if not ovrds else ovrds logger = _get_logger(self.debug) if type(tickers) is not list: tickers = [tickers] if type(flds) is not list: flds = [flds] request = self._create_req('ReferenceDataRequest', tickers, flds, ovrds, []) logger.info('Sending Request:\n{}'.format(request)) self._session.sendRequest(request, identity=self._identity) data = self._parse_ref(flds) data = pd.DataFrame(data) data.columns = ['ticker', 'field', 'value'] return data
def ref(self, tickers, flds, ovrds=None): """ Make a reference data request, get tickers and fields, return long pandas DataFrame with columns [ticker, field, value] Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> con.ref("CL1 Comdty", ["FUT_GEN_MONTH"]) Notes ----- This returns reference data which has singleton values. In raw format the messages passed back contain data of the form fieldData = { FUT_GEN_MONTH = "FGHJKMNQUVXZ" } """ ovrds = [] if not ovrds else ovrds logger = _get_logger(self.debug) if type(tickers) is not list: tickers = [tickers] if type(flds) is not list: flds = [flds] request = self._create_req('ReferenceDataRequest', tickers, flds, ovrds, []) logger.info('Sending Request:\n{}'.format(request)) self._session.sendRequest(request, identity=self._identity) data = self._parse_ref(flds) data = pd.DataFrame(data) data.columns = ['ticker', 'field', 'value'] return data
[ "Make", "a", "reference", "data", "request", "get", "tickers", "and", "fields", "return", "long", "pandas", "DataFrame", "with", "columns", "[", "ticker", "field", "value", "]" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L322-L367
[ "def", "ref", "(", "self", ",", "tickers", ",", "flds", ",", "ovrds", "=", "None", ")", ":", "ovrds", "=", "[", "]", "if", "not", "ovrds", "else", "ovrds", "logger", "=", "_get_logger", "(", "self", ".", "debug", ")", "if", "type", "(", "tickers", ")", "is", "not", "list", ":", "tickers", "=", "[", "tickers", "]", "if", "type", "(", "flds", ")", "is", "not", "list", ":", "flds", "=", "[", "flds", "]", "request", "=", "self", ".", "_create_req", "(", "'ReferenceDataRequest'", ",", "tickers", ",", "flds", ",", "ovrds", ",", "[", "]", ")", "logger", ".", "info", "(", "'Sending Request:\\n{}'", ".", "format", "(", "request", ")", ")", "self", ".", "_session", ".", "sendRequest", "(", "request", ",", "identity", "=", "self", ".", "_identity", ")", "data", "=", "self", ".", "_parse_ref", "(", "flds", ")", "data", "=", "pd", ".", "DataFrame", "(", "data", ")", "data", ".", "columns", "=", "[", "'ticker'", ",", "'field'", ",", "'value'", "]", "return", "data" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.bulkref_hist
Make iterative calls to bulkref() and create a long DataFrame with columns [date, ticker, field, name, value, position] where each date corresponds to overriding a historical data override field. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS dates: list list of date strings in the format YYYYmmdd ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value. This should not include the date_field which will be iteratively overridden date_field: str Field to iteratively override for requesting historical data, e.g. REFERENCE_DATE, CURVE_DATE, etc. Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> dates = ["20160625", "20160626"] >>> con.bulkref_hist("BVIS0587 Index", "CURVE_TENOR_RATES", dates, ... date_field="CURVE_DATE")
pdblp/pdblp.py
def bulkref_hist(self, tickers, flds, dates, ovrds=None, date_field='REFERENCE_DATE'): """ Make iterative calls to bulkref() and create a long DataFrame with columns [date, ticker, field, name, value, position] where each date corresponds to overriding a historical data override field. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS dates: list list of date strings in the format YYYYmmdd ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value. This should not include the date_field which will be iteratively overridden date_field: str Field to iteratively override for requesting historical data, e.g. REFERENCE_DATE, CURVE_DATE, etc. Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> dates = ["20160625", "20160626"] >>> con.bulkref_hist("BVIS0587 Index", "CURVE_TENOR_RATES", dates, ... date_field="CURVE_DATE") """ ovrds = [] if not ovrds else ovrds if type(tickers) is not list: tickers = [tickers] if type(flds) is not list: flds = [flds] self._send_hist(tickers, flds, dates, date_field, ovrds) data = self._parse_bulkref(flds, keep_corrId=True, sent_events=len(dates)) data = pd.DataFrame(data) data.columns = ['ticker', 'field', 'name', 'value', 'position', 'date'] data = data.sort_values(by=['date', 'position']).reset_index(drop=True) data = data.loc[:, ['date', 'ticker', 'field', 'name', 'value', 'position']] return data
def bulkref_hist(self, tickers, flds, dates, ovrds=None, date_field='REFERENCE_DATE'): """ Make iterative calls to bulkref() and create a long DataFrame with columns [date, ticker, field, name, value, position] where each date corresponds to overriding a historical data override field. Parameters ---------- tickers: {list, string} String or list of strings corresponding to tickers flds: {list, string} String or list of strings corresponding to FLDS dates: list list of date strings in the format YYYYmmdd ovrds: list of tuples List of tuples where each tuple corresponds to the override field and value. This should not include the date_field which will be iteratively overridden date_field: str Field to iteratively override for requesting historical data, e.g. REFERENCE_DATE, CURVE_DATE, etc. Example ------- >>> import pdblp >>> con = pdblp.BCon() >>> con.start() >>> dates = ["20160625", "20160626"] >>> con.bulkref_hist("BVIS0587 Index", "CURVE_TENOR_RATES", dates, ... date_field="CURVE_DATE") """ ovrds = [] if not ovrds else ovrds if type(tickers) is not list: tickers = [tickers] if type(flds) is not list: flds = [flds] self._send_hist(tickers, flds, dates, date_field, ovrds) data = self._parse_bulkref(flds, keep_corrId=True, sent_events=len(dates)) data = pd.DataFrame(data) data.columns = ['ticker', 'field', 'name', 'value', 'position', 'date'] data = data.sort_values(by=['date', 'position']).reset_index(drop=True) data = data.loc[:, ['date', 'ticker', 'field', 'name', 'value', 'position']] return data
[ "Make", "iterative", "calls", "to", "bulkref", "()", "and", "create", "a", "long", "DataFrame", "with", "columns", "[", "date", "ticker", "field", "name", "value", "position", "]", "where", "each", "date", "corresponds", "to", "overriding", "a", "historical", "data", "override", "field", "." ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L561-L608
[ "def", "bulkref_hist", "(", "self", ",", "tickers", ",", "flds", ",", "dates", ",", "ovrds", "=", "None", ",", "date_field", "=", "'REFERENCE_DATE'", ")", ":", "ovrds", "=", "[", "]", "if", "not", "ovrds", "else", "ovrds", "if", "type", "(", "tickers", ")", "is", "not", "list", ":", "tickers", "=", "[", "tickers", "]", "if", "type", "(", "flds", ")", "is", "not", "list", ":", "flds", "=", "[", "flds", "]", "self", ".", "_send_hist", "(", "tickers", ",", "flds", ",", "dates", ",", "date_field", ",", "ovrds", ")", "data", "=", "self", ".", "_parse_bulkref", "(", "flds", ",", "keep_corrId", "=", "True", ",", "sent_events", "=", "len", "(", "dates", ")", ")", "data", "=", "pd", ".", "DataFrame", "(", "data", ")", "data", ".", "columns", "=", "[", "'ticker'", ",", "'field'", ",", "'name'", ",", "'value'", ",", "'position'", ",", "'date'", "]", "data", "=", "data", ".", "sort_values", "(", "by", "=", "[", "'date'", ",", "'position'", "]", ")", ".", "reset_index", "(", "drop", "=", "True", ")", "data", "=", "data", ".", "loc", "[", ":", ",", "[", "'date'", ",", "'ticker'", ",", "'field'", ",", "'name'", ",", "'value'", ",", "'position'", "]", "]", "return", "data" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.bdib
Get Open, High, Low, Close, Volume, and numEvents for a ticker. Return pandas DataFrame Parameters ---------- ticker: string String corresponding to ticker start_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS end_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS event_type: string {TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK} Requested data event type interval: int {1... 1440} Length of time bars elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set. Refer to the IntradayBarRequest section in the 'Services & schemas reference guide' for more info on these values
pdblp/pdblp.py
def bdib(self, ticker, start_datetime, end_datetime, event_type, interval, elms=None): """ Get Open, High, Low, Close, Volume, and numEvents for a ticker. Return pandas DataFrame Parameters ---------- ticker: string String corresponding to ticker start_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS end_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS event_type: string {TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK} Requested data event type interval: int {1... 1440} Length of time bars elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set. Refer to the IntradayBarRequest section in the 'Services & schemas reference guide' for more info on these values """ elms = [] if not elms else elms # flush event queue in case previous call errored out logger = _get_logger(self.debug) while(self._session.tryNextEvent()): pass # Create and fill the request for the historical data request = self.refDataService.createRequest('IntradayBarRequest') request.set('security', ticker) request.set('eventType', event_type) request.set('interval', interval) # bar interval in minutes request.set('startDateTime', start_datetime) request.set('endDateTime', end_datetime) for name, val in elms: request.set(name, val) logger.info('Sending Request:\n{}'.format(request)) # Send the request self._session.sendRequest(request, identity=self._identity) # Process received events data = [] flds = ['open', 'high', 'low', 'close', 'volume', 'numEvents'] for msg in self._receive_events(): d = msg['element']['IntradayBarResponse'] for bar in d['barData']['barTickData']: data.append(bar['barTickData']) data = pd.DataFrame(data).set_index('time').sort_index().loc[:, flds] return data
def bdib(self, ticker, start_datetime, end_datetime, event_type, interval, elms=None): """ Get Open, High, Low, Close, Volume, and numEvents for a ticker. Return pandas DataFrame Parameters ---------- ticker: string String corresponding to ticker start_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS end_datetime: string UTC datetime in format YYYY-mm-ddTHH:MM:SS event_type: string {TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK} Requested data event type interval: int {1... 1440} Length of time bars elms: list of tuples List of tuples where each tuple corresponds to the other elements to be set. Refer to the IntradayBarRequest section in the 'Services & schemas reference guide' for more info on these values """ elms = [] if not elms else elms # flush event queue in case previous call errored out logger = _get_logger(self.debug) while(self._session.tryNextEvent()): pass # Create and fill the request for the historical data request = self.refDataService.createRequest('IntradayBarRequest') request.set('security', ticker) request.set('eventType', event_type) request.set('interval', interval) # bar interval in minutes request.set('startDateTime', start_datetime) request.set('endDateTime', end_datetime) for name, val in elms: request.set(name, val) logger.info('Sending Request:\n{}'.format(request)) # Send the request self._session.sendRequest(request, identity=self._identity) # Process received events data = [] flds = ['open', 'high', 'low', 'close', 'volume', 'numEvents'] for msg in self._receive_events(): d = msg['element']['IntradayBarResponse'] for bar in d['barData']['barTickData']: data.append(bar['barTickData']) data = pd.DataFrame(data).set_index('time').sort_index().loc[:, flds] return data
[ "Get", "Open", "High", "Low", "Close", "Volume", "and", "numEvents", "for", "a", "ticker", ".", "Return", "pandas", "DataFrame" ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L630-L682
[ "def", "bdib", "(", "self", ",", "ticker", ",", "start_datetime", ",", "end_datetime", ",", "event_type", ",", "interval", ",", "elms", "=", "None", ")", ":", "elms", "=", "[", "]", "if", "not", "elms", "else", "elms", "# flush event queue in case previous call errored out", "logger", "=", "_get_logger", "(", "self", ".", "debug", ")", "while", "(", "self", ".", "_session", ".", "tryNextEvent", "(", ")", ")", ":", "pass", "# Create and fill the request for the historical data", "request", "=", "self", ".", "refDataService", ".", "createRequest", "(", "'IntradayBarRequest'", ")", "request", ".", "set", "(", "'security'", ",", "ticker", ")", "request", ".", "set", "(", "'eventType'", ",", "event_type", ")", "request", ".", "set", "(", "'interval'", ",", "interval", ")", "# bar interval in minutes", "request", ".", "set", "(", "'startDateTime'", ",", "start_datetime", ")", "request", ".", "set", "(", "'endDateTime'", ",", "end_datetime", ")", "for", "name", ",", "val", "in", "elms", ":", "request", ".", "set", "(", "name", ",", "val", ")", "logger", ".", "info", "(", "'Sending Request:\\n{}'", ".", "format", "(", "request", ")", ")", "# Send the request", "self", ".", "_session", ".", "sendRequest", "(", "request", ",", "identity", "=", "self", ".", "_identity", ")", "# Process received events", "data", "=", "[", "]", "flds", "=", "[", "'open'", ",", "'high'", ",", "'low'", ",", "'close'", ",", "'volume'", ",", "'numEvents'", "]", "for", "msg", "in", "self", ".", "_receive_events", "(", ")", ":", "d", "=", "msg", "[", "'element'", "]", "[", "'IntradayBarResponse'", "]", "for", "bar", "in", "d", "[", "'barData'", "]", "[", "'barTickData'", "]", ":", "data", ".", "append", "(", "bar", "[", "'barTickData'", "]", ")", "data", "=", "pd", ".", "DataFrame", "(", "data", ")", ".", "set_index", "(", "'time'", ")", ".", "sort_index", "(", ")", ".", "loc", "[", ":", ",", "flds", "]", "return", "data" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
BCon.bsrch
This function uses the Bloomberg API to retrieve 'bsrch' (Bloomberg SRCH Data) queries. Returns list of tickers. Parameters ---------- domain: string A character string with the name of the domain to execute. It can be a user defined SRCH screen, commodity screen or one of the variety of Bloomberg examples. All domains are in the format <domain>:<search_name>. Example "COMDTY:NGFLOW" Returns ------- data: pandas.DataFrame List of bloomberg tickers from the BSRCH
pdblp/pdblp.py
def bsrch(self, domain): """ This function uses the Bloomberg API to retrieve 'bsrch' (Bloomberg SRCH Data) queries. Returns list of tickers. Parameters ---------- domain: string A character string with the name of the domain to execute. It can be a user defined SRCH screen, commodity screen or one of the variety of Bloomberg examples. All domains are in the format <domain>:<search_name>. Example "COMDTY:NGFLOW" Returns ------- data: pandas.DataFrame List of bloomberg tickers from the BSRCH """ logger = _get_logger(self.debug) request = self.exrService.createRequest('ExcelGetGridRequest') request.set('Domain', domain) logger.info('Sending Request:\n{}'.format(request)) self._session.sendRequest(request, identity=self._identity) data = [] for msg in self._receive_events(to_dict=False): for v in msg.getElement("DataRecords").values(): for f in v.getElement("DataFields").values(): data.append(f.getElementAsString("StringValue")) return pd.DataFrame(data)
def bsrch(self, domain): """ This function uses the Bloomberg API to retrieve 'bsrch' (Bloomberg SRCH Data) queries. Returns list of tickers. Parameters ---------- domain: string A character string with the name of the domain to execute. It can be a user defined SRCH screen, commodity screen or one of the variety of Bloomberg examples. All domains are in the format <domain>:<search_name>. Example "COMDTY:NGFLOW" Returns ------- data: pandas.DataFrame List of bloomberg tickers from the BSRCH """ logger = _get_logger(self.debug) request = self.exrService.createRequest('ExcelGetGridRequest') request.set('Domain', domain) logger.info('Sending Request:\n{}'.format(request)) self._session.sendRequest(request, identity=self._identity) data = [] for msg in self._receive_events(to_dict=False): for v in msg.getElement("DataRecords").values(): for f in v.getElement("DataFields").values(): data.append(f.getElementAsString("StringValue")) return pd.DataFrame(data)
[ "This", "function", "uses", "the", "Bloomberg", "API", "to", "retrieve", "bsrch", "(", "Bloomberg", "SRCH", "Data", ")", "queries", ".", "Returns", "list", "of", "tickers", "." ]
matthewgilbert/pdblp
python
https://github.com/matthewgilbert/pdblp/blob/aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2/pdblp/pdblp.py#L684-L712
[ "def", "bsrch", "(", "self", ",", "domain", ")", ":", "logger", "=", "_get_logger", "(", "self", ".", "debug", ")", "request", "=", "self", ".", "exrService", ".", "createRequest", "(", "'ExcelGetGridRequest'", ")", "request", ".", "set", "(", "'Domain'", ",", "domain", ")", "logger", ".", "info", "(", "'Sending Request:\\n{}'", ".", "format", "(", "request", ")", ")", "self", ".", "_session", ".", "sendRequest", "(", "request", ",", "identity", "=", "self", ".", "_identity", ")", "data", "=", "[", "]", "for", "msg", "in", "self", ".", "_receive_events", "(", "to_dict", "=", "False", ")", ":", "for", "v", "in", "msg", ".", "getElement", "(", "\"DataRecords\"", ")", ".", "values", "(", ")", ":", "for", "f", "in", "v", ".", "getElement", "(", "\"DataFields\"", ")", ".", "values", "(", ")", ":", "data", ".", "append", "(", "f", ".", "getElementAsString", "(", "\"StringValue\"", ")", ")", "return", "pd", ".", "DataFrame", "(", "data", ")" ]
aaef49ad6fca9af6ee44739d6e7e1cc3e7b0f8e2
valid
assemble_one
Assemble one EVM instruction from its textual representation. :param asmcode: assembly code for one instruction :type asmcode: str :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An Instruction object :rtype: Instruction Example use:: >>> print assemble_one('LT')
pyevmasm/evmasm.py
def assemble_one(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble one EVM instruction from its textual representation. :param asmcode: assembly code for one instruction :type asmcode: str :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An Instruction object :rtype: Instruction Example use:: >>> print assemble_one('LT') """ try: instruction_table = instruction_tables[fork] asmcode = asmcode.strip().split(' ') instr = instruction_table[asmcode[0].upper()] if pc: instr.pc = pc if instr.operand_size > 0: assert len(asmcode) == 2 instr.operand = int(asmcode[1], 0) return instr except: raise AssembleError("Something wrong at pc %d" % pc)
def assemble_one(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble one EVM instruction from its textual representation. :param asmcode: assembly code for one instruction :type asmcode: str :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An Instruction object :rtype: Instruction Example use:: >>> print assemble_one('LT') """ try: instruction_table = instruction_tables[fork] asmcode = asmcode.strip().split(' ') instr = instruction_table[asmcode[0].upper()] if pc: instr.pc = pc if instr.operand_size > 0: assert len(asmcode) == 2 instr.operand = int(asmcode[1], 0) return instr except: raise AssembleError("Something wrong at pc %d" % pc)
[ "Assemble", "one", "EVM", "instruction", "from", "its", "textual", "representation", "." ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L332-L361
[ "def", "assemble_one", "(", "asmcode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "try", ":", "instruction_table", "=", "instruction_tables", "[", "fork", "]", "asmcode", "=", "asmcode", ".", "strip", "(", ")", ".", "split", "(", "' '", ")", "instr", "=", "instruction_table", "[", "asmcode", "[", "0", "]", ".", "upper", "(", ")", "]", "if", "pc", ":", "instr", ".", "pc", "=", "pc", "if", "instr", ".", "operand_size", ">", "0", ":", "assert", "len", "(", "asmcode", ")", "==", "2", "instr", ".", "operand", "=", "int", "(", "asmcode", "[", "1", "]", ",", "0", ")", "return", "instr", "except", ":", "raise", "AssembleError", "(", "\"Something wrong at pc %d\"", "%", "pc", ")" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
assemble_all
Assemble a sequence of textual representation of EVM instructions :param asmcode: assembly code for any number of instructions :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: generator[Instructions] Example use:: >>> assemble_one('''PUSH1 0x60\n \ PUSH1 0x40\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x108\n \ PUSH1 0x0\n \ POP\n \ SSTORE\n \ PUSH1 0x40\n \ MLOAD\n \ ''')
pyevmasm/evmasm.py
def assemble_all(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble a sequence of textual representation of EVM instructions :param asmcode: assembly code for any number of instructions :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: generator[Instructions] Example use:: >>> assemble_one('''PUSH1 0x60\n \ PUSH1 0x40\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x108\n \ PUSH1 0x0\n \ POP\n \ SSTORE\n \ PUSH1 0x40\n \ MLOAD\n \ ''') """ asmcode = asmcode.split('\n') asmcode = iter(asmcode) for line in asmcode: if not line.strip(): continue instr = assemble_one(line, pc=pc, fork=fork) yield instr pc += instr.size
def assemble_all(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble a sequence of textual representation of EVM instructions :param asmcode: assembly code for any number of instructions :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: generator[Instructions] Example use:: >>> assemble_one('''PUSH1 0x60\n \ PUSH1 0x40\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x108\n \ PUSH1 0x0\n \ POP\n \ SSTORE\n \ PUSH1 0x40\n \ MLOAD\n \ ''') """ asmcode = asmcode.split('\n') asmcode = iter(asmcode) for line in asmcode: if not line.strip(): continue instr = assemble_one(line, pc=pc, fork=fork) yield instr pc += instr.size
[ "Assemble", "a", "sequence", "of", "textual", "representation", "of", "EVM", "instructions" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L364-L398
[ "def", "assemble_all", "(", "asmcode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "asmcode", "=", "asmcode", ".", "split", "(", "'\\n'", ")", "asmcode", "=", "iter", "(", "asmcode", ")", "for", "line", "in", "asmcode", ":", "if", "not", "line", ".", "strip", "(", ")", ":", "continue", "instr", "=", "assemble_one", "(", "line", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")", "yield", "instr", "pc", "+=", "instr", ".", "size" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
disassemble_one
Disassemble a single instruction from a bytecode :param bytecode: the bytecode stream :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: an Instruction object :rtype: Instruction Example use:: >>> print disassemble_one('\x60\x10')
pyevmasm/evmasm.py
def disassemble_one(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble a single instruction from a bytecode :param bytecode: the bytecode stream :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: an Instruction object :rtype: Instruction Example use:: >>> print disassemble_one('\x60\x10') """ instruction_table = instruction_tables[fork] if isinstance(bytecode, bytes): bytecode = bytearray(bytecode) if isinstance(bytecode, str): bytecode = bytearray(bytecode.encode('latin-1')) bytecode = iter(bytecode) try: opcode = next(bytecode) except StopIteration: return assert isinstance(opcode, int) instruction = copy.copy(instruction_table.get(opcode, None)) if instruction is None: instruction = Instruction(opcode, 'INVALID', 0, 0, 0, 0, 'Unspecified invalid instruction.') instruction.pc = pc try: if instruction.has_operand: instruction.parse_operand(bytecode) except ParseError: instruction = None finally: return instruction
def disassemble_one(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble a single instruction from a bytecode :param bytecode: the bytecode stream :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: an Instruction object :rtype: Instruction Example use:: >>> print disassemble_one('\x60\x10') """ instruction_table = instruction_tables[fork] if isinstance(bytecode, bytes): bytecode = bytearray(bytecode) if isinstance(bytecode, str): bytecode = bytearray(bytecode.encode('latin-1')) bytecode = iter(bytecode) try: opcode = next(bytecode) except StopIteration: return assert isinstance(opcode, int) instruction = copy.copy(instruction_table.get(opcode, None)) if instruction is None: instruction = Instruction(opcode, 'INVALID', 0, 0, 0, 0, 'Unspecified invalid instruction.') instruction.pc = pc try: if instruction.has_operand: instruction.parse_operand(bytecode) except ParseError: instruction = None finally: return instruction
[ "Disassemble", "a", "single", "instruction", "from", "a", "bytecode" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L401-L443
[ "def", "disassemble_one", "(", "bytecode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "instruction_table", "=", "instruction_tables", "[", "fork", "]", "if", "isinstance", "(", "bytecode", ",", "bytes", ")", ":", "bytecode", "=", "bytearray", "(", "bytecode", ")", "if", "isinstance", "(", "bytecode", ",", "str", ")", ":", "bytecode", "=", "bytearray", "(", "bytecode", ".", "encode", "(", "'latin-1'", ")", ")", "bytecode", "=", "iter", "(", "bytecode", ")", "try", ":", "opcode", "=", "next", "(", "bytecode", ")", "except", "StopIteration", ":", "return", "assert", "isinstance", "(", "opcode", ",", "int", ")", "instruction", "=", "copy", ".", "copy", "(", "instruction_table", ".", "get", "(", "opcode", ",", "None", ")", ")", "if", "instruction", "is", "None", ":", "instruction", "=", "Instruction", "(", "opcode", ",", "'INVALID'", ",", "0", ",", "0", ",", "0", ",", "0", ",", "'Unspecified invalid instruction.'", ")", "instruction", ".", "pc", "=", "pc", "try", ":", "if", "instruction", ".", "has_operand", ":", "instruction", ".", "parse_operand", "(", "bytecode", ")", "except", "ParseError", ":", "instruction", "=", "None", "finally", ":", "return", "instruction" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
disassemble_all
Disassemble all instructions in bytecode :param bytecode: an evm bytecode (binary) :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: list[Instruction] Example use:: >>> for inst in disassemble_all(bytecode): ... print(instr) ... PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x2 PUSH2 0x108 PUSH1 0x0 POP SSTORE PUSH1 0x40 MLOAD
pyevmasm/evmasm.py
def disassemble_all(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble all instructions in bytecode :param bytecode: an evm bytecode (binary) :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: list[Instruction] Example use:: >>> for inst in disassemble_all(bytecode): ... print(instr) ... PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x2 PUSH2 0x108 PUSH1 0x0 POP SSTORE PUSH1 0x40 MLOAD """ if isinstance(bytecode, bytes): bytecode = bytearray(bytecode) if isinstance(bytecode, str): bytecode = bytearray(bytecode.encode('latin-1')) bytecode = iter(bytecode) while True: instr = disassemble_one(bytecode, pc=pc, fork=fork) if not instr: return pc += instr.size yield instr
def disassemble_all(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble all instructions in bytecode :param bytecode: an evm bytecode (binary) :type bytecode: str | bytes | bytearray | iterator :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: An generator of Instruction objects :rtype: list[Instruction] Example use:: >>> for inst in disassemble_all(bytecode): ... print(instr) ... PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x2 PUSH2 0x108 PUSH1 0x0 POP SSTORE PUSH1 0x40 MLOAD """ if isinstance(bytecode, bytes): bytecode = bytearray(bytecode) if isinstance(bytecode, str): bytecode = bytearray(bytecode.encode('latin-1')) bytecode = iter(bytecode) while True: instr = disassemble_one(bytecode, pc=pc, fork=fork) if not instr: return pc += instr.size yield instr
[ "Disassemble", "all", "instructions", "in", "bytecode" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L446-L488
[ "def", "disassemble_all", "(", "bytecode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "if", "isinstance", "(", "bytecode", ",", "bytes", ")", ":", "bytecode", "=", "bytearray", "(", "bytecode", ")", "if", "isinstance", "(", "bytecode", ",", "str", ")", ":", "bytecode", "=", "bytearray", "(", "bytecode", ".", "encode", "(", "'latin-1'", ")", ")", "bytecode", "=", "iter", "(", "bytecode", ")", "while", "True", ":", "instr", "=", "disassemble_one", "(", "bytecode", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")", "if", "not", "instr", ":", "return", "pc", "+=", "instr", ".", "size", "yield", "instr" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
disassemble
Disassemble an EVM bytecode :param bytecode: binary representation of an evm bytecode :type bytecode: str | bytes | bytearray :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code Example use:: >>> disassemble("\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100
pyevmasm/evmasm.py
def disassemble(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble an EVM bytecode :param bytecode: binary representation of an evm bytecode :type bytecode: str | bytes | bytearray :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code Example use:: >>> disassemble("\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100 """ return '\n'.join(map(str, disassemble_all(bytecode, pc=pc, fork=fork)))
def disassemble(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble an EVM bytecode :param bytecode: binary representation of an evm bytecode :type bytecode: str | bytes | bytearray :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code Example use:: >>> disassemble("\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100 """ return '\n'.join(map(str, disassemble_all(bytecode, pc=pc, fork=fork)))
[ "Disassemble", "an", "EVM", "bytecode" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L491-L513
[ "def", "disassemble", "(", "bytecode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "return", "'\\n'", ".", "join", "(", "map", "(", "str", ",", "disassemble_all", "(", "bytecode", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")", ")", ")" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
assemble
Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... b"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00"
pyevmasm/evmasm.py
def assemble(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... b"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00" """ return b''.join(x.bytes for x in assemble_all(asmcode, pc=pc, fork=fork))
def assemble(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... b"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00" """ return b''.join(x.bytes for x in assemble_all(asmcode, pc=pc, fork=fork))
[ "Assemble", "an", "EVM", "program" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L516-L539
[ "def", "assemble", "(", "asmcode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "return", "b''", ".", "join", "(", "x", ".", "bytes", "for", "x", "in", "assemble_all", "(", "asmcode", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")", ")" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
disassemble_hex
Disassemble an EVM bytecode :param bytecode: canonical representation of an evm bytecode (hexadecimal) :type bytecode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code :rtype: str Example use:: >>> disassemble_hex("0x6060604052600261010") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100
pyevmasm/evmasm.py
def disassemble_hex(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble an EVM bytecode :param bytecode: canonical representation of an evm bytecode (hexadecimal) :type bytecode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code :rtype: str Example use:: >>> disassemble_hex("0x6060604052600261010") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100 """ if bytecode.startswith('0x'): bytecode = bytecode[2:] bytecode = unhexlify(bytecode) return disassemble(bytecode, pc=pc, fork=fork)
def disassemble_hex(bytecode, pc=0, fork=DEFAULT_FORK): """ Disassemble an EVM bytecode :param bytecode: canonical representation of an evm bytecode (hexadecimal) :type bytecode: str :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the text representation of the assembler code :rtype: str Example use:: >>> disassemble_hex("0x6060604052600261010") ... PUSH1 0x60 BLOCKHASH MSTORE PUSH1 0x2 PUSH2 0x100 """ if bytecode.startswith('0x'): bytecode = bytecode[2:] bytecode = unhexlify(bytecode) return disassemble(bytecode, pc=pc, fork=fork)
[ "Disassemble", "an", "EVM", "bytecode" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L542-L568
[ "def", "disassemble_hex", "(", "bytecode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "if", "bytecode", ".", "startswith", "(", "'0x'", ")", ":", "bytecode", "=", "bytecode", "[", "2", ":", "]", "bytecode", "=", "unhexlify", "(", "bytecode", ")", "return", "disassemble", "(", "bytecode", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
assemble_hex
Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str | iterator[Instruction] :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble_hex('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... "0x6060604052600261010"
pyevmasm/evmasm.py
def assemble_hex(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str | iterator[Instruction] :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble_hex('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... "0x6060604052600261010" """ if isinstance(asmcode, list): return '0x' + hexlify(b''.join([x.bytes for x in asmcode])).decode('ascii') return '0x' + hexlify(assemble(asmcode, pc=pc, fork=fork)).decode('ascii')
def assemble_hex(asmcode, pc=0, fork=DEFAULT_FORK): """ Assemble an EVM program :param asmcode: an evm assembler program :type asmcode: str | iterator[Instruction] :param pc: program counter of the first instruction(optional) :type pc: int :param fork: fork name (optional) :type fork: str :return: the hex representation of the bytecode :rtype: str Example use:: >>> assemble_hex('''PUSH1 0x60\n \ BLOCKHASH\n \ MSTORE\n \ PUSH1 0x2\n \ PUSH2 0x100\n \ ''') ... "0x6060604052600261010" """ if isinstance(asmcode, list): return '0x' + hexlify(b''.join([x.bytes for x in asmcode])).decode('ascii') return '0x' + hexlify(assemble(asmcode, pc=pc, fork=fork)).decode('ascii')
[ "Assemble", "an", "EVM", "program" ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L571-L596
[ "def", "assemble_hex", "(", "asmcode", ",", "pc", "=", "0", ",", "fork", "=", "DEFAULT_FORK", ")", ":", "if", "isinstance", "(", "asmcode", ",", "list", ")", ":", "return", "'0x'", "+", "hexlify", "(", "b''", ".", "join", "(", "[", "x", ".", "bytes", "for", "x", "in", "asmcode", "]", ")", ")", ".", "decode", "(", "'ascii'", ")", "return", "'0x'", "+", "hexlify", "(", "assemble", "(", "asmcode", ",", "pc", "=", "pc", ",", "fork", "=", "fork", ")", ")", ".", "decode", "(", "'ascii'", ")" ]
d27daf19a36d630a31499e783b716cf1165798d8
valid
block_to_fork
Convert block number to fork name. :param block_number: block number :type block_number: int :return: fork name :rtype: str Example use:: >>> block_to_fork(0) ... "frontier" >>> block_to_fork(4370000) ... "byzantium" >>> block_to_fork(4370001) ... "byzantium"
pyevmasm/evmasm.py
def block_to_fork(block_number): """ Convert block number to fork name. :param block_number: block number :type block_number: int :return: fork name :rtype: str Example use:: >>> block_to_fork(0) ... "frontier" >>> block_to_fork(4370000) ... "byzantium" >>> block_to_fork(4370001) ... "byzantium" """ forks_by_block = { 0: "frontier", 1150000: "homestead", # 1920000 Dao 2463000: "tangerine_whistle", 2675000: "spurious_dragon", 4370000: "byzantium", #7280000: "constantinople", # Same Block as petersburg, commented to avoid conflicts 7280000: "petersburg", 9999999: "serenity" # to be replaced after Serenity launch } fork_names = list(forks_by_block.values()) fork_blocks = list(forks_by_block.keys()) return fork_names[bisect(fork_blocks, block_number) - 1]
def block_to_fork(block_number): """ Convert block number to fork name. :param block_number: block number :type block_number: int :return: fork name :rtype: str Example use:: >>> block_to_fork(0) ... "frontier" >>> block_to_fork(4370000) ... "byzantium" >>> block_to_fork(4370001) ... "byzantium" """ forks_by_block = { 0: "frontier", 1150000: "homestead", # 1920000 Dao 2463000: "tangerine_whistle", 2675000: "spurious_dragon", 4370000: "byzantium", #7280000: "constantinople", # Same Block as petersburg, commented to avoid conflicts 7280000: "petersburg", 9999999: "serenity" # to be replaced after Serenity launch } fork_names = list(forks_by_block.values()) fork_blocks = list(forks_by_block.keys()) return fork_names[bisect(fork_blocks, block_number) - 1]
[ "Convert", "block", "number", "to", "fork", "name", "." ]
crytic/pyevmasm
python
https://github.com/crytic/pyevmasm/blob/d27daf19a36d630a31499e783b716cf1165798d8/pyevmasm/evmasm.py#L881-L914
[ "def", "block_to_fork", "(", "block_number", ")", ":", "forks_by_block", "=", "{", "0", ":", "\"frontier\"", ",", "1150000", ":", "\"homestead\"", ",", "# 1920000 Dao ", "2463000", ":", "\"tangerine_whistle\"", ",", "2675000", ":", "\"spurious_dragon\"", ",", "4370000", ":", "\"byzantium\"", ",", "#7280000: \"constantinople\", # Same Block as petersburg, commented to avoid conflicts", "7280000", ":", "\"petersburg\"", ",", "9999999", ":", "\"serenity\"", "# to be replaced after Serenity launch", "}", "fork_names", "=", "list", "(", "forks_by_block", ".", "values", "(", ")", ")", "fork_blocks", "=", "list", "(", "forks_by_block", ".", "keys", "(", ")", ")", "return", "fork_names", "[", "bisect", "(", "fork_blocks", ",", "block_number", ")", "-", "1", "]" ]
d27daf19a36d630a31499e783b716cf1165798d8