Explore google protobuf in python - python

From dialogflows API I received a protobuf.
Even after reading all documentation I have no clue how to explore the object and extract values (e.g. a string out of it)
I show you what I did:
(Pdb) dir(parameter)
['ByteSize', 'Clear', 'ClearExtension', 'ClearField', 'CopyFrom', 'DESCRIPTOR', 'DiscardUnknownFields', 'Extensions', 'FieldsEntry', 'FindInitializationErrors', 'FromString', 'HasExtension', 'HasField', 'IsInitialized', 'ListFields', 'MergeFrom', 'MergeFromString', 'ParseFromString', 'RegisterExtension', 'SerializePartialToString', 'SerializeToString', 'SetInParent', 'UnknownFields', 'WhichOneof', '_CheckCalledFromGeneratedFile', '_SetListener', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__unicode__', '_extensions_by_name', '_extensions_by_number', 'fields', 'get_or_create_list', 'get_or_create_struct', 'items', 'keys', 'update', 'values']
(Pdb) parameter.ListFields
<built-in method ListFields of Struct object at 0x7f5e0c17e920>
(Pdb) parameter.ListFields()
[(<google.protobuf.pyext._message.FieldDescriptor object at 0x7f5e0c191a10>, {'endDate': string_value: "2019-07-31T23:59:59+02:00"
, 'startDate': string_value: "2019-07-01T00:00:00+02:00"
})]
(Pdb) parameter.endDate
*** AttributeError: endDate
(Pdb) parameter
fields {
key: "endDate"
value {
string_value: "2019-07-31T23:59:59+02:00"
}
}
fields {
key: "startDate"
value {
string_value: "2019-07-01T00:00:00+02:00"
}
}
(Pdb) parameter.fields
{'endDate': string_value: "2019-07-31T23:59:59+02:00"
, 'startDate': string_value: "2019-07-01T00:00:00+02:00"
}
(Pdb) type(parameter.fields)
<class 'google.protobuf.pyext._message.MessageMapContainer'>
(Pdb) parameter.fields.get('endDate')
string_value: "2019-07-31T23:59:59+02:00"
My goal is to extract startDate and endDate.
Can anybody help to explain how to systematically expore such an undocumented protobuf and get the values out of it?

Related

python-docx: How to get the document properties into an object

I'm using python-docx and I can list the properties with doc.core_properties.__dir__() as in this question.
But assigning prs.core_properties to a python variable doesn't give a usable object.
How do I get the document properties into a python object?
Here is what I tried:
from pptx import Presentation
import os
pptFile = "myfile.pptx"
prs = Presentation(os.path.realpath(pptFile))
prsProps = prs.core_properties
print(prs.core_properties.__dir__())
print(prs.core_properties.modified)
prsProps
yields
['_partname', '_content_type', '_package', '_blob', '_element', '_rels', '__module__', '__doc__', 'default', 'author', 'category', 'comments', 'content_status', 'created', 'identifier', 'keywords', 'language', 'last_modified_by', 'last_printed', 'modified', 'revision', 'subject', 'title', 'version', '_new', '__init__', 'load', 'blob', 'part', 'content_type', 'drop_rel', 'load_rels_from_xml', 'package', 'partname', 'rels', '_blob_from_file', '_rel_ref_count', 'part_related_by', 'relate_to', 'related_part', 'target_ref', '__dict__', '__weakref__', '__new__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__reduce_ex__', '__reduce__', '__getstate__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
2019-11-15 12:30:37
<pptx.parts.coreprops.CorePropertiesPart at 0x1d9a3fa6fd0>
I expected an object in prsProps.

Cannot find "items" attribute in Pagination in Flask

I am very new to Flask and pagination in general.
I need to paginate a list which is as follows:
campaign_brief_result = [
{'id': 112233, 'name': 'DSR-335', 'description': 'Desc', 'currency': '0'},
{'id': 11223344, 'name': 'DSR-336', 'description': 'Desc2', 'currency': '2'}
]
When I run the code below I am unable to find the items attribute. How can I access the data that is being paginated here?
Here is my code snippet that i am using:
paginated_data = Pagination( page = 1,
per_page = 2,
total = len(campaign_brief_result),
search = False,
record_name = 'campaign_brief_result')
print(dir(paginated_data))
the print shows me the following attributes of 'paginated_data':
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_single_page_link', 'alignment', 'anchor', 'args', 'bs_version', 'bulma_style', 'css_end_fmt', 'css_framework', 'current_page_fmt', 'display_msg', 'endpoint', 'first_page', 'format_number', 'format_total', 'found', 'gap_marker_fmt', 'has_next', 'has_prev', 'href', 'info', 'init_values', 'inner_window', 'is_disabled', 'last_page', 'link', 'link_css_fmt', 'link_size', 'links', 'next_disabled_page_fmt', 'next_label', 'next_page', 'next_page_fmt', 'outer_window', 'page', 'page_href', 'page_parameter', 'pages', 'per_page', 'per_page_parameter', 'prev_disabled_page_fmt', 'prev_label', 'prev_page', 'prev_page_fmt', 'record_name', 'search', 'search_msg', 'show_single_page', 'single_page', 'skip', 'total', 'total_pages', 'url_coding']

Accessing LayerFieldsContainer using variable in pyshark

I'm using pyshark to parse pcap files. I want to access layer fields using variable as shown in simple example below:
For example to access ntp server ip:
p = cap[0]
print(p.bootp.option_ntp_server)
However, I want to access it like this:
option_list = {
"12": "option_hostname",
"60": "option_vendor_class_id",
"43": "option_ntp_server"
}
p = cap[0]
print(p.bootp.%s %(option_list["43"]))
Of course this kind of access is not possible. So I tried to use getattr() like this:
getOption = getattr(p.bootp, option_list["43"])
getOption()
And it gave me following error:
'LayerFieldsContainer' object is not callable
It seems pyshark packet or layer classes is not callable.
How I can access layer fields using variable? Or can you suggest me another method to access options using option type numbers? Because I want to access this fields using option type numbers (Like option 12, option 43), not using titles.
i think OBJ pyshark.packet.fields.LayerFieldsContainer have format special, you can't use it as string.
dir (LayerFieldsContaniner) have:
['__add__', '__class__', '__class__', '__contains__', '__delattr__', '__delattr__', '__dict__', '__dir__', '__doc__', '__doc__', '__eq__', '__format__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__getstate__', '__getstate__', '__gt__', '__hash__', '__hash__', '__init__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__module__', '__mul__', '__ne__', '__new__', '__new__', '__reduce__', '__reduce__', '__reduce_ex__', '__reduce_ex__', '__repr__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__setattr__', '__setstate__', '__setstate__', '__sizeof__', '__sizeof__', '__slots__', '__str__', '__str__', '__subclasshook__', '__subclasshook__', '__weakref__', '_formatter_field_name_split', '_formatter_parser', 'add_field', 'all_fields', 'alternate_fields', 'base16_value', 'binary_value', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'fields', 'find', 'format', 'get_default_value', 'hex_value', 'hide', 'index', 'int_value', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'main_field', 'name', 'partition', 'pos', 'raw_value', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'show', 'showname', 'showname_key', 'showname_value', 'size', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'unmaskedvalue', 'upper', 'zfill']
you can use it.

Iterate Dependency Graph items generated by Stanford Dependency Parser

I am struggling to go over the results of Stanford Dependency Parser. It prints the information successfully but I can't access it. This is what I see when I print the results.
defaultdict(<function DependencyGraph.__init__.<locals>.<lambda> at 0x075078A0>,
{0: {'address': 0,
'ctag': 'TOP',
'deps': defaultdict(<class 'list'>, {'root': [2]}),
'feats': None,
'head': None,
'lemma': None,
'rel': None,
'tag': 'TOP',
'word': None},
1: {'address': 1,
'ctag': 'CD',
'deps': defaultdict(<class 'list'>, {}),
'feats': '_',
'head': 2,
'lemma': '_',
'rel': 'nummod',
'tag': 'CD',
'word': 'seven'},
How can I acess these items?
The output of the dependency graph is an object of DependencyGraph class. If you run
dir(result) # assuming result contains the parser output
You would be able to see all the properties and methods of the result object.
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__unicode__',
'__weakref__',
'_hd',
'_parse',
'_rel',
'_repr_svg_',
'_tree',
'_word',
'add_arc',
'add_node',
'connect_graph',
'contains_address',
'contains_cycle',
'get_by_address',
'get_cycle_path',
'left_children',
'load',
'nodes',
'nx_graph',
'redirect_arcs',
'remove_by_address',
'right_children',
'root',
'to_conll',
'to_dot',
'top_relation_label',
'tree',
'triples',
'unicode_repr']
Then you can iterate over all nodes to access your desired items.
for i in range(len(result.nodes)):
print(f"Word: {result.nodes[i]['word']}\tLemma: {result.nodes[i]['lemma']}\tTag: {result.nodes[i]['tag']}\tHead: {result.nodes[i]['head']}\tRel: {result.nodes[i]['rel']}")

How to get HTTP version in Flask

I want to get HTTP version like 'HTTP/1.1', 'HTTP/1.0' in flask. I have check all var in request, but found nothing.
['__class__', '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_file_stream', '_get_stream_for_parsing', '_is_old_module', '_load_form_data', '_parse_content_type', 'accept_charsets', 'accept_encodings', 'accept_languages', 'accept_mimetypes', 'access_route', 'application', 'args', 'authorization', 'base_url', 'blueprint', 'cache_control', 'charset', 'close', 'content_encoding', 'content_length', 'content_md5', 'content_type', 'cookies', 'data', 'date', 'dict_storage_class', 'disable_data_descriptor', 'encoding_errors', 'endpoint', 'environ', 'files', 'form', 'form_data_parser_class', 'from_values', 'full_path', 'get_data', 'get_json', 'headers', 'host', 'host_url', 'if_match', 'if_modified_since', 'if_none_match', 'if_range', 'if_unmodified_since', 'input_stream', 'is_multiprocess', 'is_multithread', 'is_run_once', 'is_secure', 'is_xhr', 'json', 'list_storage_class', 'make_form_data_parser', 'max_content_length', 'max_form_memory_size', 'max_forwards', 'method', 'mimetype', 'mimetype_params', 'module', 'on_json_loading_failed', 'parameter_storage_class', 'path', 'pragma', 'query_string', 'range', 'referrer', 'remote_addr', 'remote_user', 'routing_exception', 'scheme', 'script_root', 'shallow', 'stream', 'trusted_hosts', 'url', 'url_charset', 'url_root', 'url_rule', 'user_agent', 'values', 'view_args', 'want_form_data_parsed']
Other side, the log has the HTTP version
2013-09-23 06:55:42 208.115.113.85 - - [23/Sep/2013 06:55:42] "GET /robots.txt HTTP/1.1" 404 -
So, how can I get HTTP version in flask?
From request.environ:
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def hello():
return request.environ.get('SERVER_PROTOCOL')

Categories