Skip to content
Discussion options

You must be logged in to vote

Hi @qasikfwn ,

I think what you are looking for is the Transformer class:

from caterpillar.py import Transformer, f, uint64

class Custom64bitHash:
	h: int

	def __init__(self, h: int) -> None:
		# do some other stuff with `h`
		# ...
		self.h = h

class _64bitHash(Transformer[Custom64bitHash, int, Custom64bitHash, int]):
    def __init__(self):
        # parses a uint64 first
        super().__init__(uint64)

    def encode(self, obj: Custom64bitHash, context) -> int:
        return obj.h

    def decode(self, parsed: int, context) -> Custom64bitHash:
          return Custom64bitHash(parsed)


Custom64bitHash_t = f[Custom64bitHash, _64bitHash()]

Here, uint64is parsed first and you can de…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@qasikfwn
Comment options

Answer selected by qasikfwn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants