Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does Callable[[bytes], bytes] define and what does calling the variable with a bytes object return?

I’m a C# developer and I’m implementing some code in C# thats based around byte operations (decryption stuff). I’ve found a python repo that does some of the things i need to do, but I’m having trouble understanding some of the code used in the script since I’m not well versed in Python.

The operation goes as follows: A method with a parameter of type Callable[[bytes], bytes] gets called with a parameter that’s (unless I’m mistaken) also a bytes. what does that represent and more importantly: how to replicate in C#?

from Crypto.Cipher import AES
import io

def do_the_thing(some_bytes: bytes,
                 interesting_var: Callable[[bytes], bytes],
                 more_bytes: bytes
                 other_data: Optional[bytes] = None) -> dict:

cipher = AES.new(some_bytes, AES.MODE_CBC, IV=b'\x00' * 16)
plaintext = cipher.decrypt(more_bytes)

pstream = io.BytesIO(plaintext)
uid = pstream.read(uid_length)

""" and now the line I'm confused about: """
result = interesting_var(uid)

Any insight into what’s going on here would be much appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

f: Callable[[bytes], bytes] means that f is a function (a callable) that expects a single argument of the type bytes (that is what [bytes] stands for) and returns bytes.

https://docs.python.org/3/library/typing.html#typing.Callable

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading