GithubHelp home page GithubHelp logo

telegram_type_parser's Introduction

telegram-type-parser

Library that simply parses telegram types, for example:

  • Integer: parses as int
  • String: str
  • Boolean: bool
  • True: True
  • False: False
  • Array of <tail>: as list[<tail>] (parsed recursively, for example: Array of Array of Integer would be list[list[int]])
  • Others are kept as-is (considered composite types)

Quickstart

from telegram_type_parser import parse

tps = [
    "Array of Message",
    "Array of Array of Message",
    "Integer",
    "String",
    "Boolean",
]

for tp in tps:
    print(tp, "is", parse(tp))

# Expected output is:
# Array of Message is list[Message]
# Array of Array of Message is list[list[Message]]
# Integer is int
# String is str
# Boolean is bool

Need more?

We have more. If you translating telegram types into something that is not the python type hints, then use telegram_type_parser.parse_ex and pass custom Folder to it, for example:

from typing import Optional, Callable
from telegram_type_parser import parse_ex, Folder


# IDK what to type so example is pretty made up
class AsArray(Folder[int | list[int]]):
    def string(self) -> int | list[int]:
        return 0

    def integer(self) -> int | list[int]:
        return 1

    def boolean(self, preset: Optional[bool] = None) -> int | list[int]:
        if preset is None:
            return 2
        return 3 + preset

    def composite(self, tp: str) -> int | list[int]:
        return [0, int.from_bytes(tp.encode(), "little")]

    def array_of(self, argument: Callable[[], int | list[int]]):
        return [1, argument()]


tps = [
    "Array of Message",
    "Array of Array of Message",
    "Integer",
    "String",
    "Boolean",
]

creepycode = AsArray()
for tp in tps:
    print(tp, "is", parse_ex(tp, creepycode))

# Expected output is:
# Array of Message is [1, [0, 28542640894207309]]
# Array of Array of Message is [1, [1, [0, 28542640894207309]]]
# Integer is 1
# String is 0
# Boolean is 2

telegram_type_parser's People

Contributors

nerodono avatar

Watchers

 avatar

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.