GithubHelp home page GithubHelp logo

protopy's Introduction

ProtoPy

tiny pure python protobuf implementation

this isn't anywhere near a good implementation (actually not complete either) of Protobufs (see Google's) but it doesn't require any non-std libraries and the runtime is around 400 lines.

Example usage

First, compile the Protobufs to Python with the protobuf_compile.py script.

python protobuf_compile.py < exampleproto.proto > exampleproto.py

Then the Protobufs can be used as follows:

from exampleproto import *

# encoding
>>> game = CMsgClientGamesPlayed.Game()
>>> game.game_id = 1234
>>> game.game_name = "blah"
>>> game.game_version = EGameVersion.k_EGameVersionNone

>>> msg = CMsgClientGamesPlayed()
>>> msg.game_ip_address = 0x11223344
>>> msg.game_port = 1337
>>> msg.games = [game]
>>> msg.token = "TOKENTOKENTOKENTOKEN"

>>> encoded = msg.encode()
>>> encoded
'\x10\xc4\xe6\x88\x89\x01\x18\xb9\n*\x14TOKENTOKENTOKENTOKENR\x11\t\xd2\x04\x00\x00\x00\x00\x00\x00\x12\x04blah(\x00'

# decoding
>>> msg = CMsgClientGamesPlayed()
>>> msg.decode(encoded)
>>> msg.games
[{'game_id': 1234L, 'game_name': 'blah', 'game_version': 0L}]
>>> msg.games[0].game_name
'blah'
>>> msg.token
'TOKENTOKENTOKENTOKEN'
>>> msg.to_dict()
{'game_port': 1337, 'token': 'TOKENTOKENTOKENTOKEN', 'game_ip_address': 287454020, 'games': [{'game_id': 1234, 'game_name': 'blah', 'game_version': 0}]}

protopy's People

Contributors

crazyhatfish avatar z888z avatar

Stargazers

Scott Ivey avatar Trevor Rudolph avatar Andrea De Pasquale avatar JT Olio avatar

Watchers

James Cloos avatar  avatar

Forkers

yoki123 triple-it

protopy's Issues

Encode raw data fast

If I already have my messages represented as list and dict, I must construct intermediate objects which waste a lot of overhead. I would like to encode by a way like

buf = encode_dict(CustomMessageType, dict_data)

or

buf = CustomMessageType.encode_dict(dict_data)

Moving __lookup__ from object variable to class variable will decrease overhead and be helpful to complete this feature.

please forgive my poor English :)

Add a LICENSE file?

If you intend for others to use this legally, it would be very helpful to choose an open source licence.

Support json-like format to print Message object

When I print the msg object in Example , the result is
<exampleproto.CMsgClientGamesPlayed instance at 0xxxxxxxx>

And the msg.__dict__ isn't friendly with embedded Message.
{'token': 'TOKENTOKENTOKENTOKEN', 'games': [<exampleproto.Game instance at 0xxxxxxxxx>], 'game_port': 1337, 'game_ip_address': 287454020}

I think the json-like/dict-like format is very helpful to debug decoding result, such as
{'game_ip_address': 287454020, 'game_port': 1337, 'token': 'TOKENTOKENTOKENTOKEN', 'games': [{'game_id': 1234, 'game_name': 'blah', 'game_version': 0}]}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.