python - How to list attributes that you defined in google protocol buffers? -


I have a Google protocol buffer definition that looks like a compliance:

  Message Foo {required string name = 1; Alternative string address = 2; Alternative string nickname = 3; Optional int32 height = 4; }  

Now in Python, I want to list all the above features, but only those features, though, interactive dragon, I think there are many fields defined by Google, so it seems problematic

I have seen some stack overflow posts on introspection.

What is the way to do this?

What I want to do here here I have Python utility that fills the command line above field I I use argparse, and I will:

  parser = argparse.ArgumentParser (...) parser.add_argument ( "- -attribute_name ", blah) I want to place add_argument () in a loop and makes it dynamic based on proto file definition. Actually, I do not want to modify the utility code, whenever I change the proto file. It seems that I should be able to do this in dragon. I do not know how to  

Anyone have suggestions?

Thank you.

For additional information, I took the above example, protoc. Here is the interactive output:

  & gt; & Gt; & Gt; Import hello_pb2 & gt; & Gt; & Gt; H = hello_pb2.Foo ()> & gt; & Gt; & Gt; dir (h) [ 'ADDRESS_FIELD_NUMBER', 'Address',' ByteSize ',' clean ',' ClearExtension ',' Clearfield ',' CopyFrom ',' descriptors', 'FindInitializationErrors',' FromString ',' HEIGHT_FIELD_NUMBER ',' HasExtension 'HasField' IsInitialized 'ListFields' MergeFrom' MergeFromString 'NAME_FIELD_NUMBER' NICKNAME_FIELD_NUMBER ',' name ',' Alias', 'ParseFromString' RegisterExtension 'SerializePartialToString 'Sserializetostring' Setinprent '_internalprse' _internlsserialize '_modifaid' _setlistener '__clas__' __dipakpi__ '__delttr__' __do__ '__ak__' __formet__ ' __gatttribute__ '__has__' __init__ '__mtclass__' __modl__ '__ne__' __ni__ '__redke__' __redke_aks__ '__repr__' __satttr__ '__sizof__' __slots__ ' , '__stratched', '__script__', '__syncode__', '__weakref__', '_cached_byte_size', '_c ached_byte_size_dirty ',' _decoders_by_tag ',' _extensions_by_name ',' _extensions_by_number ',' _fields ',' _is_present_in_parent ',' _listener  

as I have identified some great areas _ field, but It was empty.

Here is the answer:

/ strong> (given by the response of the canton vera)

  import hello_pb2h = hello_pb2.Foo ( ) F = hello_pb2.Foo () f.DESCRIPTOR.fields_by_name.keys ()  

You want to repeat on Foo.DESCRIPTOR.fields . See descriptor class:

Every message group has a static member DESCRIPTOR , which is the descriptor for that type.


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -