Pyjo.String.Bytes - String of bytes

from Pyjo.String.Bytes import Bytes

# Manipulate bytes
data = Bytes(b'foo bar baz')
print(data.md5_sum())

# Or import the module, as in Google Python Style Guide

import Pyjo.String.Bytes

# Manipulate bytes
data = Pyjo.String.Bytes.new(b'foo bar baz')
print(data.md5_sum())

# Chain methods
data = Pyjo.String.Bytes.new(b'foo bar baz').quote()
string = data.unquote().b64_encode('')
print(string)

# Use the alternative constructor
from Pyjo.String.Bytes import b
string = b(b'foobarbaz').b64_encode('').say()

Pyjo.String.Bytes is a bytes that provides a more friendly API for many of the functions in Pyjo.Util. Its counterpart for characters is Pyjo.String.Unicode, and both have every method: a method that needs characters decodes the bytes with UTF-8, or as the characters 0-255 when they are not UTF-8, and encodes the result the same way. A method that turns bytes into other bytes returns a Pyjo.String.Bytes object, and one that makes characters, like md5_sum(), a Pyjo.String.Unicode object.

# Use it as bytes
data = Pyjo.String.Bytes.new(b'foo')
print(data.upper())

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::ByteStream of Mojolicious 9.49.

Functions

class Pyjo.String.Bytes.Pyjo_String_Bytes(*parts: Anything)[source]

Pyjo.String.Bytes inherits all methods from bytes and implements the following new ones.

static __new__(cls, *parts: Anything) → Self[source]
__repr__() → str[source]

Return repr(self).

__str__() → str[source]
string = str(data)

Alias for to_str().

b64_decode() → Self[source]
data = data.b64_decode()

Base64 decode bytes with Pyjo.Util.b64_decode().

b64_encode(eol: str = '\n') → Pyjo_String_Unicode[source]
string = data.b64_encode()
string = data.b64_encode("\n")

Base64 encode bytes with Pyjo.Util.b64_encode().

# "Zm9vIGJhciBiYXo="
b(b'foo bar baz').b64_encode('')
b64url_decode() → Self[source]
data = data.b64url_decode()

Base64 decode bytes in the variant for URLs with Pyjo.Util.b64url_decode().

b64url_encode() → Pyjo_String_Unicode[source]
string = data.b64url_encode()

Base64 encode bytes in the variant for URLs with Pyjo.Util.b64url_encode().

# "Zm9vIGJhciBiYXo"
b(b'foo bar baz').b64url_encode()
camelize() → Self[source]
data = data.camelize()

Camelize bytes with Pyjo.Util.camelize().

clone() → Self[source]
data2 = data.clone()

Return a new Pyjo.String.Bytes object cloned from these bytes.

decamelize() → Self[source]
data = data.decamelize()

Decamelize bytes with Pyjo.Util.decamelize().

decode(encoding: str = 'UTF-8', errors: str = 'strict') → Pyjo_String_Unicode[source]
string = data.decode()
string = data.decode('iso-8859-1')

Decode bytes to a Pyjo.String.Unicode object, defaults to using UTF-8.

# "♥"
b(b'%E2%99%A5').url_unescape().decode()
encode(encoding: str = 'UTF-8', errors: str = 'strict') → Self[source]
data = data.encode()
data = data.encode('iso-8859-1')

Encode the characters of bytes, defaults to using UTF-8.

gunzip() → Self[source]
data = data.gunzip()

Uncompress bytes with Pyjo.Util.gunzip().

gzip() → Self[source]
data = data.gzip()

Compress bytes with Pyjo.Util.gzip().

hmac_sha1_bytes(secret: str | bytes) → Self[source]
data = data.hmac_sha1_bytes(b'passw0rd')

Generate binary HMAC-SHA1 checksum for bytes with Pyjo.Util.hmac_sha1_bytes().

hmac_sha1_sum(secret: str | bytes) → Pyjo_String_Unicode[source]
string = data.hmac_sha1_sum(b'passw0rd')

Generate HMAC-SHA1 checksum for bytes with Pyjo.Util.hmac_sha1_sum().

# "7fbdc89263974a89210ea71f171c77d3f8c21471"
b(b'foo bar baz').hmac_sha1_sum(b'secr3t')
hmac_sha256_bytes(secret: str | bytes) → Self[source]
data = data.hmac_sha256_bytes(b'passw0rd')

Generate binary HMAC-SHA256 checksum for bytes with Pyjo.Util.hmac_sha256_bytes().

hmac_sha256_sum(secret: str | bytes) → Pyjo_String_Unicode[source]
string = data.hmac_sha256_sum(b'passw0rd')

Generate HMAC-SHA256 checksum for bytes with Pyjo.Util.hmac_sha256_sum().

# "cfd435b7e14cb5ed539ee154ce73ef0edea9ad480d54d2ca8894075b79386471"
b(b'foo bar baz').hmac_sha256_sum(b'secr3t')
hmac_sha512_bytes(secret: str | bytes) → Self[source]
data = data.hmac_sha512_bytes(b'passw0rd')

Generate binary HMAC-SHA512 checksum for bytes with Pyjo.Util.hmac_sha512_bytes().

hmac_sha512_sum(secret: str | bytes) → Pyjo_String_Unicode[source]
string = data.hmac_sha512_sum(b'passw0rd')

Generate HMAC-SHA512 checksum for bytes with Pyjo.Util.hmac_sha512_sum().

html_unescape() → Self[source]
data = data.html_unescape()

Unescape all HTML entities in bytes with Pyjo.Util.html_unescape().

# b"%3Chtml%3E"
b(b'<html>').html_unescape().url_escape()
humanize_bytes() → Pyjo_String_Unicode[source]
string = data.humanize_bytes()

Turn number of bytes into a simplified human readable format for bytes with Pyjo.Util.humanize_bytes().

md5_bytes() → Self[source]
data = data.md5_bytes()

Generate binary MD5 checksum for bytes with Pyjo.Util.md5_bytes().

md5_sum() → Pyjo_String_Unicode[source]
string = data.md5_sum()

Generate MD5 checksum for bytes with Pyjo.Util.md5_sum().

classmethod new(*parts: Anything) → Self[source]
data = Pyjo.String.Bytes.new(b'test123')

Construct a new Pyjo.String.Bytes object. A str is encoded with UTF-8.

punycode_decode() → Self[source]
data = data.punycode_decode()

Punycode decode bytes with Pyjo.Util.punycode_decode().

punycode_encode() → Self[source]
data = data.punycode_encode()

Punycode encode bytes with Pyjo.Util.punycode_encode().

quote() → Self[source]
data = data.quote()

Quote bytes with Pyjo.Util.quote().

say(handle: IO[Any] | None = None) → Self[source]
data = data.say()
data = data.say(sys.stderr)

Print bytes to handle and append a newline, defaults to using sys.stdout.

secure_compare(data: str | bytes) → bool[source]
boolean = data.secure_compare(data2)

Compare bytes with Pyjo.Util.secure_compare().

sha1_bytes() → Self[source]
data = data.sha1_bytes()

Generate binary SHA1 checksum for bytes with Pyjo.Util.sha1_bytes().

sha1_sum() → Pyjo_String_Unicode[source]
string = data.sha1_sum()

Generate SHA1 checksum for bytes with Pyjo.Util.sha1_sum().

sha256_bytes() → Self[source]
data = data.sha256_bytes()

Generate binary SHA256 checksum for bytes with Pyjo.Util.sha256_bytes().

sha256_sum() → Pyjo_String_Unicode[source]
string = data.sha256_sum()

Generate SHA256 checksum for bytes with Pyjo.Util.sha256_sum().

sha512_bytes() → Self[source]
data = data.sha512_bytes()

Generate binary SHA512 checksum for bytes with Pyjo.Util.sha512_bytes().

sha512_sum() → Pyjo_String_Unicode[source]
string = data.sha512_sum()

Generate SHA512 checksum for bytes with Pyjo.Util.sha512_sum().

property size: int[source]
size = data.size

Size of bytes.

slugify(allow_unicode: bool = False) → Self[source]
data = data.slugify()
data = data.slugify(boolean)

Generate URL slug for bytes with Pyjo.Util.slugify().

split(pattern: str | bytes | Pattern[str] | Pattern[bytes], limit: int = 0) → Pyjo_Collection[Self | None][source]
collection = data.split(b',')
collection = data.split(b',', -1)

Turn bytes into Pyjo.Collection object containing Pyjo.String.Bytes objects, as Pyjo.String.Unicode.Pyjo_String_Unicode.split() does.

# b"One,Two,Three"
b(b'one,two,three').split(b',').map('camelize').join(',').encode()
tap(cb: Callable[[...], Any] | str, *args: Any, **kwargs: Any) → Self[source]
data = data.tap(lambda d: ...)

Alias for Pyjo.Base.tap().

term_escape() → Self[source]
data = data.term_escape()

Escape POSIX control characters in bytes with Pyjo.Util.term_escape().

# Print binary checksum to terminal
b(b'foo').sha1_bytes().term_escape().say()
to_bytes() → bytes[source]
data = data.to_bytes()

Turn bytes into bytes.

to_str() → str[source]
string = data.to_str()

Turn bytes into str, decoded with UTF-8, or as the characters 0-255 when they are not UTF-8.

trim() → Self[source]
data = data.trim()

Trim whitespace characters from both ends of bytes with Pyjo.Util.trim().

unindent() → Self[source]
data = data.unindent()

Unindent bytes with Pyjo.Util.unindent().

unquote() → Self[source]
data = data.unquote()

Unquote bytes with Pyjo.Util.unquote().

url_escape(pattern: bytes | None = None) → Self[source]
data = data.url_escape()
data = data.url_escape(rb'^A-Za-z0-9\-._~')

Percent encode all unsafe characters in bytes with Pyjo.Util.url_escape().

# b"%E2%98%83"
b('☃').url_escape()
url_unescape() → Self[source]
data = data.url_unescape()

Decode percent encoded characters in bytes with Pyjo.Util.url_unescape().

# b"<html>"
b(b'%3Chtml%3E').url_unescape().xml_escape()
xml_escape() → Self[source]
data = data.xml_escape()

Escape only the characters &, <, >, " and ' in bytes with Pyjo.Util.xml_escape().

xor_encode(key: str | bytes) → Self[source]
data = data.xor_encode(key)

XOR encode bytes with Pyjo.Util.xor_encode().

# b"%04%0E%15B%03%1B%10"
b(b'foo bar').xor_encode(b'baz').url_escape()
Pyjo.String.Bytes.b(*parts: Anything) → Pyjo_String_Bytes[source]
data = b(b'test123')

Construct a new Pyjo.String.Bytes object.

Pyjo.String.Bytes.new(*parts: Anything) → Self[source]
data = Pyjo.String.Bytes.new(b'test123')

Construct a new Pyjo.String.Bytes object. A str is encoded with UTF-8.