{"id":329269,"date":"2023-08-30T12:19:10","date_gmt":"2023-08-30T12:19:10","guid":{"rendered":"https:\/\/www.blog.pythonlibrary.org\/?p=12073"},"modified":"2023-08-30T12:19:10","modified_gmt":"2023-08-30T12:19:10","slug":"an-intro-to-protocol-buffers-with-python","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/08\/30\/an-intro-to-protocol-buffers-with-python\/","title":{"rendered":"An Intro to Protocol Buffers with Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>Protocol buffers are a data serialization format that is language agnostic. They are analogous to Python&#8217;s own pickle format, but one of the advantages of protocol buffers is that they can be used by multiple programming languages.<\/p>\n<p>For example, Protocol buffers are supported in C++, C#, Dart, Go, Java, Kotlin, Objective-C, PHP, Ruby, and more in addition to Python. The biggest con for Protocol buffers is that far too often, the versions have changes that are <strong>not\u00a0<\/strong>backward compatible.<\/p>\n<p>In this article, you will learn how to do the following:<\/p>\n<ul>\n<li>Creating a Protocol format<\/li>\n<li>Compiling Your Protocol Buffers File<\/li>\n<li>Writing Messages<\/li>\n<li>Reading Messages<\/li>\n<\/ul>\n<p>Let&#8217;s get started!<\/p>\n<h2>Creating a Protocol Format<\/h2>\n<p>You&#8217;ll need your own file to create your application using protocol buffers. For this project, you will create a way to store music albums using the Protocol Buffer format.<\/p>\n<p>Create a new file named <strong>music.proto<\/strong> and enter the following into the file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">syntax = \"proto2\";\r\n\r\npackage music;\r\n\r\nmessage Music {\r\n  optional string artist_name = 1;\r\n  optional string album = 2;\r\n  optional int32 year = 3;\r\n  optional string genre = 4;\r\n\r\n}\r\n\r\nmessage Library {\r\n  repeated Music albums = 1;\r\n}<\/pre>\n<p>The first line in this code is your package syntax, &#8220;proto2&#8221;. Next is your package declaration, which is used to prevent name collisions.<\/p>\n<p>The rest of the code is made up of message definitions. These are groups of typed fields. There are quite a few differing types that you may use, \u00a0including\u00a0<code>bool<\/code>,\u00a0<code>int32<\/code>,\u00a0<code>float<\/code>,\u00a0<code>double<\/code>, and\u00a0<code>string<\/code>.<\/p>\n<p>You can set the fields to <strong>optional<\/strong>, <strong>repeated,<\/strong> or <strong>required<\/strong>. According to the documentation, you rarely want to use <strong>required<\/strong> because there&#8217;s no way to unset that. In fact, in proto3, <strong>required<\/strong> is no longer supported.<\/p>\n<h2>Compiling Your Protocol Buffers File<\/h2>\n<p>To be able to use your Protocol Buffer in Python, you will need to compile it using a program called\u00a0<strong>protoc<\/strong>. You can get it <a href=\"https:\/\/protobuf.dev\/downloads\">here<\/a>. Be sure to follow the instructions in the README file to get it installed successfully.<\/p>\n<p>Now run\u00a0<strong>protoc<\/strong> against your\u00a0<strong>proto<\/strong> file, like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">protoc --python_out=. .\\music.proto --proto_path=.<\/pre>\n<p>The command above will convert your proto file to Python code in your current working directory. This new file is named\u00a0<strong>music_pb2.py<\/strong>.<\/p>\n<p>Here are the contents of that file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># -*- coding: utf-8 -*-\r\n# Generated by the protocol buffer compiler.  DO NOT EDIT!\r\n# source: music.proto\r\n\"\"\"Generated protocol buffer code.\"\"\"\r\nfrom google.protobuf import descriptor as _descriptor\r\nfrom google.protobuf import descriptor_pool as _descriptor_pool\r\nfrom google.protobuf import symbol_database as _symbol_database\r\nfrom google.protobuf.internal import builder as _builder\r\n# @@protoc_insertion_point(imports)\r\n\r\n_sym_db = _symbol_database.Default()\r\n\r\n\r\n\r\n\r\nDESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\\n\\x0bmusic.proto\\x12\\x05music\\\"H\\n\\x05Music\\x12\\x13\\n\\x0b\\x61rtist_name\\x18\\x01 \\x01(\\t\\x12\\r\\n\\x05\\x61lbum\\x18\\x02 \\x01(\\t\\x12\\x0c\\n\\x04year\\x18\\x03 \\x01(\\x05\\x12\\r\\n\\x05genre\\x18\\x04 \\x01(\\t\\\"\\'\\n\\x07Library\\x12\\x1c\\n\\x06\\x61lbums\\x18\\x01 \\x03(\\x0b\\x32\\x0c.music.Music')\r\n\r\n_globals = globals()\r\n_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)\r\n_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'music_pb2', _globals)\r\nif _descriptor._USE_C_DESCRIPTORS == False:\r\n  DESCRIPTOR._options = None\r\n  _globals['_MUSIC']._serialized_start=22\r\n  _globals['_MUSIC']._serialized_end=94\r\n  _globals['_LIBRARY']._serialized_start=96\r\n  _globals['_LIBRARY']._serialized_end=135\r\n# @@protoc_insertion_point(module_scope)\r\n<\/pre>\n<p>There&#8217;s a lot of magic here that uses descriptors to generate classes for you. Don&#8217;t worry about how it works; the documentation doesn&#8217;t explain it well either.<\/p>\n<p>Now that your Protocol Buffer is transformed into Python code, you can start serializing data!<\/p>\n<h2>Writing Messages<\/h2>\n<p>To start serializing your data with Protocol Buffers, you must create a new Python file.<\/p>\n<p>Name your file\u00a0<strong>music_writer.py<\/strong> and enter the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from pathlib import Path\r\nimport music_pb2\r\n\r\n\r\ndef overwrite(path):\r\n    write_or_append = \"a\"\r\n    while True:\r\n        answer = input(f\"Do you want to overwrite '{path}' (Y\/N) ?\").lower()\r\n        if answer not in \"yn\":\r\n            print(\"Y or N are the only valid answers\")\r\n            continue\r\n\r\n        write_or_append = \"w\" if answer == \"y\" else \"a\"\r\n        break\r\n    return write_or_append\r\n\r\n\r\ndef music_data(path):\r\n    p = Path(path)\r\n    write_or_append = \"w\"\r\n    if p.exists():\r\n        write_or_append = overwrite(path)\r\n\r\n    library = music_pb2.Library()\r\n    new_music = library.albums.add()\r\n\r\n    while True:\r\n        print(\"Let's add some music!\\n\")\r\n        new_music.artist_name = input(\"What is the artist name? \")\r\n        new_music.album = input(\"What is the name of the album? \")\r\n        new_music.year = int(input(\"Which year did the album come out? \"))\r\n\r\n        more = input(\"Do you want to add more music? (Y\/N)\").lower()\r\n        if more == \"n\":\r\n            break\r\n\r\n    with open(p, f\"{write_or_append}b\") as f:\r\n        f.write(library.SerializeToString())\r\n\r\n    print(f\"Music library written to {p.resolve()}\")\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    music_data(\"music.pro\")\r\n<\/pre>\n<p>The meat of this program is in your <strong>music_data()\u00a0<\/strong>function. Here you check if the user wants to overwrite their music file or append to it. Then, you create a <strong>Library<\/strong> object and prompt the user for the data they want to serialize.<\/p>\n<p>For this example, you ask the user to enter an artist&#8217;s name, album, and year. You omit the genre for now, but you can add that yourself if you&#8217;d like to.<\/p>\n<p>After they enter the year of the album, you ask the user if they would like to add another album. The application will write the data to disk and end if they don&#8217;t want to continue adding music.<\/p>\n<p>Here is an example writing session:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Let's add some music!\r\n\r\nWhat is the artist name? Zahna\r\nWhat is the name of the album? Stronger Than Death\r\nWhich year did the album come out? 2023\r\nDo you want to add more music? (Y\/N)Y\r\nLet's add some music!\r\n\r\nWhat is the artist name? KB\r\nWhat is the name of the album? His Glory Alone II\r\nWhich year did the album come out? 2023\r\nDo you want to add more music? (Y\/N)N<\/pre>\n<p>Now, let&#8217;s learn how to read your data!<\/p>\n<h2>Reading Messages<\/h2>\n<p>Now that you have your Protocol Buffer Messages written to disk, you need a way to read them.<\/p>\n<p>Create a new file named\u00a0<strong>music_reader.py<\/strong> and enter the following code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from pathlib import Path\r\nimport music_pb2\r\n\r\n\r\ndef list_music(music):\r\n    for album in music.albums:\r\n        print(f\"Artist: {album.artist_name}\")\r\n        print(f\"Album: {album.album}\")\r\n        print(f\"Year: {album.year}\")\r\n        print()\r\n\r\n\r\ndef main(path):\r\n    p = Path(path)\r\n    library = music_pb2.Library()\r\n\r\n    with open(p.resolve(), \"rb\") as f:\r\n        library.ParseFromString(f.read())\r\n\r\n    list_music(library)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    main(\"music.pro\")\r\n<\/pre>\n<p>This code is a little simpler than the writing code was. Once again, you create an instance of the\u00a0<strong>Library<\/strong> class. This time, you loop over the library albums and print out all the data in the file.<\/p>\n<p>If you had added the music in this tutorial to your Protocol Buffers file, when you run your reader, the output would look like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Artist: Zahna\r\nAlbum: Stronger Than Death\r\nYear: 2023\r\n\r\nArtist: KB\r\nAlbum: His Glory Alone II\r\nYear: 2023<\/pre>\n<p>Give it a try with your custom music file!<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Now you know the basics of working with Protocol Buffers using Python. Specifically, you learned about the following:<\/p>\n<ul>\n<li>Creating a Protocol format<\/li>\n<li>Compiling Your Protocol Buffers File<\/li>\n<li>Writing Messages<\/li>\n<li>Reading Messages<\/li>\n<\/ul>\n<p>Now is your chance to consider using this type of data serialization in your application. Once you have a plan, give it a try! Protocol buffers are a great way to create data in a programming language-agnostic way.<\/p>\n<h2>Further Reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/protobuf.dev\/getting-started\/pythontutorial\/\">Protocol Buffer Basics: Python | Protocol Buffers Documentation (protobuf.dev)<\/a><\/li>\n<\/ul>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/08\/30\/an-intro-to-protocol-buffers-with-python\/\">An Intro to Protocol Buffers with Python<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Protocol buffers are a data serialization format that is language agnostic. They are analogous to Python\u2019s own pickle format, but one of the advantages of protocol buffers is that they can be used by multiple programming languages. For example, Protocol buffers are supported in C++, C#, Dart, Go, Java, Kotlin, Objective-C, PHP, Ruby, and more \u2026<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/08\/30\/an-intro-to-protocol-buffers-with-python\/\"> <span class=\"screen-reader-text\">An Intro to Protocol Buffers with Python<\/span> Read More \u00bb<\/a><\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/08\/30\/an-intro-to-protocol-buffers-with-python\/\">An Intro to Protocol Buffers with Python<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n<\/div>","protected":false},"author":2018,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"An Intro to Protocol Buffers with Python - ITTeacherITFreelance.hk","description":"Protocol buffers are a data serialization format that is language agnostic. They are analogous to Python\u2019s own pickle format, but one of the advantages of proto"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329269"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2018"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329269"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329269\/revisions"}],"predecessor-version":[{"id":329270,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329269\/revisions\/329270"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}