Skip to main content

A framework for creating protocols and socket servers

Project description

PTCL

Design and compose modular protocols

pip install ptcl

Currently, this library only supports finite acyclic protocols.

This library is designed to easily create, save and load protocols and implement client-server systems with them. The code given as is, even though not version 1.0, works.

A "protocol", in the eyes of ptcl, is a directed acyclic graph of Transform objects. Each Transform object either works on bytes or string. When its .transform method is called, a Transform object does something to its input and returns its changes.

Transform objects are chained with ">>" operator, to yield a directed acyclic graph whose nodes are Transform's. A Transform can have multiple parents or children. When a Transform has multiple children, the runtime expects it to return an index and a data. The runtime decides which child to continue with using the index, and passes the data to it.

Graphs of Transform's are wrapped with Protocol class. This class needs a root Transform to wrap. Save and load operations are governed with this class.

A server needs a Protocol object when initialized. When a connection is recevied, server immediatelly creates a thread and passes everything to this thread. This thread runs the run method of a Socket, whose class is also given to the server. This socket, answers its client using the Protocol object, which is also provided by the server.

Further and more complete documentation will be provided later. This is only to explain the methodology of the library.

from ptcl.transform import *
from ptcl.protocol import Protocol

example_text = "Hello World !".encode('utf-8')
root = RootTransform()
to_string = ToString()
split_text = SplitText(delimiter=" ")
extractor = ExtractToken()
router = RouteOnKeyword(["Hello", "World", "!"])
hello_counter = CountPasses()
world_counter = CountPasses()
exclamation_counter = CountPasses()

root >> to_string >> split_text >> extractor >> router
router >> hello_counter
router >> world_counter
router >> exclamation_counter

protocol = Protocol(root)

Above code implements a protocol, which gets a text and splits it with " " as the separator. The first token of this splitted text is extracted and separated from the test. The router, generates an index depending on what this extracted token is (we expect it to be "Hello"). Depending on the generated index, the rest of the data is routed into either hello_counter, world_counter or exclamation_counter. In this case, it is supposed to be hello_counter.

A counter transform, CountPasses, increments an internal integer if data is received, then simply transmits this same data. Therefore after running this protocol on the input string "Hello World!", we expect hello_counter to have an internal integer of 1, and 0 for the rest.

Here is the step by step transformation of the input string "Hello World!":

  • "Hello World!"
  • ["Hello", "World", "!"]
  • [["World", "!"], "Hello"]
  • [0, ["World", "!"]] (index given to token ["Hello"] is 0)
  • Data ["World", "!"] routed to hello_counter, which increments its counter

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ptcl-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ptcl-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file ptcl-0.1.0.tar.gz.

File metadata

  • Download URL: ptcl-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11

File hashes

Hashes for ptcl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d3af6398a21a2fa62df7a4711fa20ff733debf6edb708c981756447526113e4a
MD5 f027a8eab6431702cdf0dfa343b1ce32
BLAKE2b-256 287ccb2a3187e94a998631b59317220db3cbcddae0673c2debabe1c1ef2fa8a5

See more details on using hashes here.

File details

Details for the file ptcl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ptcl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Windows/11

File hashes

Hashes for ptcl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c14bf9c2b476fee0bf14706d64b7603495a5cb6b491b35df38eef377b63ac34
MD5 b1301e35e93d7fbf7c1cf05ad882826c
BLAKE2b-256 4f830eaf8b2fc6dc4df6cb2b14c2c4c3a768986988027f895a0615160a3ab438

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page