-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(shortfin_apps): moves rgb to png conversion to server-side
- Loading branch information
1 parent
c6c8439
commit f398448
Showing
4 changed files
with
129 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
shortfin/python/shortfin_apps/types/Base64CharacterEncodedByteSequence.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import re | ||
import binascii | ||
|
||
|
||
class Base64CharacterEncodedByteSequence(str): | ||
""" | ||
A sequence of 8-bit integers encoded using the [Base64 alphabet](https://www.rfc-editor.org/rfc/rfc4648.html#section-4). | ||
""" | ||
|
||
pattern = ( | ||
r"^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{3}={1}|[A-Za-z0-9+\/]{2}={2})?$" | ||
) | ||
|
||
def __new__(Self, given_subject: str): | ||
if re.match(Self.pattern, given_subject) == None: | ||
raise ValueError("String cannot be interpreted as a byte sequence") | ||
|
||
return super().__new__(Self, given_subject) | ||
|
||
@property | ||
def as_bytes(self): | ||
base64_integer_encoded_byte_sequence = self.encode() | ||
|
||
derived_raw_byte_sequence = binascii.a2b_base64( | ||
base64_integer_encoded_byte_sequence | ||
) | ||
|
||
return derived_raw_byte_sequence | ||
|
||
@classmethod | ||
def decoded_from(Self, given_raw_byte_sequence: bytes): | ||
base64_integer_encoded_byte_sequence = binascii.b2a_base64( | ||
given_raw_byte_sequence, newline=False | ||
) | ||
|
||
base64_character_encoded_byte_sequence = ( | ||
base64_integer_encoded_byte_sequence.decode() | ||
) | ||
|
||
return Self(base64_character_encoded_byte_sequence) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters