Pyjo.String.Unicode - String of characters

from Pyjo.String.Unicode import Unicode

# Manipulate string
string = Unicode('foo_bar_baz')
print(string.camelize())

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

import Pyjo.String.Unicode

# Manipulate string
string = Pyjo.String.Unicode.new('foo_bar_baz')
print(string.camelize())

# Chain methods
string = Pyjo.String.Unicode.new('foo bar baz').quote()
string = string.unquote().encode('UTF-8').b64_encode('')
print(string)

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

Pyjo.String.Unicode is a str that provides a more friendly API for many of the functions in Pyjo.Util. Its counterpart for bytes is Pyjo.String.Bytes, and both have every method: a method that needs bytes encodes the string with UTF-8. A method that turns a string into another one returns a Pyjo.String.Unicode object, and one that makes bytes, like md5_bytes(), a Pyjo.String.Bytes object.

# Use it as a str
string = Pyjo.String.Unicode.new('foo')
print(string.upper())

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::ByteStream of Mojolicious 9.49.

Functions

class Pyjo.String.Unicode.Pyjo_String_Unicode(*parts: Anything)[source]

Pyjo.String.Unicode inherits all methods from str and implements the following new ones.

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

Return repr(self).

b64_decode() → Pyjo_String_Bytes[source]
data = string.b64_decode()

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

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

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

# "Zm9vIGJhciBiYXo="
u('foo bar baz').b64_encode('')
b64url_decode() → Pyjo_String_Bytes[source]
data = string.b64url_decode()

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

b64url_encode() → Self[source]
string = string.b64url_encode()

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

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

Camelize string with Pyjo.Util.camelize().

clone() → Self[source]
string2 = string.clone()

Return a new Pyjo.String.Unicode object cloned from this string.

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

Decamelize string with Pyjo.Util.decamelize().

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

Decode string of the characters 0-255, as the bytes they are, defaults to using UTF-8.

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

Encode string to a Pyjo.String.Bytes object, defaults to using UTF-8.

# b"%E2%99%A5"
u('♥').encode().url_escape()
gunzip() → Pyjo_String_Bytes[source]
data = string.gunzip()

Uncompress string with Pyjo.Util.gunzip().

gzip() → Pyjo_String_Bytes[source]
data = string.gzip()

Compress string with Pyjo.Util.gzip().

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

md5_bytes() → Pyjo_String_Bytes[source]
data = string.md5_bytes()

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

md5_sum() → Self[source]
string = string.md5_sum()

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

classmethod new(*parts: Anything) → Self[source]
string = Pyjo.String.Unicode.new('test123')

Construct a new Pyjo.String.Unicode object.

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

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

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

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

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

Quote string with Pyjo.Util.quote().

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

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

secure_compare(string: str | bytes) → bool[source]
boolean = string.secure_compare(string2)

Compare string with Pyjo.Util.secure_compare().

sha1_bytes() → Pyjo_String_Bytes[source]
data = string.sha1_bytes()

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

sha1_sum() → Self[source]
string = string.sha1_sum()

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

sha256_bytes() → Pyjo_String_Bytes[source]
data = string.sha256_bytes()

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

sha256_sum() → Self[source]
string = string.sha256_sum()

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

sha512_bytes() → Pyjo_String_Bytes[source]
data = string.sha512_bytes()

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

sha512_sum() → Self[source]
string = string.sha512_sum()

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

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

Size of string.

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

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

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

Turn string into Pyjo.Collection object containing Pyjo.String.Unicode objects. The pattern is a regular expression, and the fields at the end that are empty are left out, unless the limit is negative.

# "One,Two,Three"
u('one,two,three').split(',').map('camelize').join(',')

# "One,Two,Three,,,"
u('one,two,three,,,').split(',', -1).map('camelize').join(',')
tap(cb: Callable[[...], Any] | str, *args: Any, **kwargs: Any) → Self[source]
string = string.tap(lambda s: ...)

Alias for Pyjo.Base.tap().

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

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

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

Turn string into bytes, encoded with UTF-8.

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

Turn string into str.

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

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

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

Unindent string with Pyjo.Util.unindent().

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

Unquote string with Pyjo.Util.unquote().

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

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

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

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

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

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

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

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

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

Construct a new Pyjo.String.Unicode object.

Pyjo.String.Unicode.split(pattern: str | Pattern[str], string: str, limit: int = 0) → list[str | None][source]
fields = split(',', '1,2,3')

Split string as Perl’s split does: the pattern is a regular expression, a match of no length at the beginning of the string or right after another match makes no field, the groups of the pattern are fields too, and the fields at the end that are empty are left out, unless the limit is negative. A positive limit is the largest number of fields, the pattern ' ' splits on whitespace after leading whitespace, and '^' matches at the beginning of every line.

Pyjo.String.Unicode.u(*parts: Anything) → Pyjo_String_Unicode[source]
string = u('test123')

Construct a new Pyjo.String.Unicode object.