Pyjo.String.Mixin - Object with to_bytes/to_str methods

import Pyjo.Base

from Pyjo.String.Mixin import Mixin

class SubClass(Pyjo.Base.object, Mixin):
    def to_bytes(self):
        return b'value'

    def to_str(self):
        return 'value'

# Use it as a string
obj = SubClass()
print(f'{obj:>10}')
print(obj + '!')

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

import Pyjo.Base
import Pyjo.String.Mixin

class SubClass(Pyjo.Base.object, Pyjo.String.Mixin.object):
    def to_bytes(self):
        return b'value'

    def to_str(self):
        return 'value'

# Use it as a string
obj = SubClass.new()
print(f'{obj:>10}')
print(obj + '!')

Pyjo.String.Mixin is the mixin class for objects with a to_str() and/or a to_bytes() method, which turns them into strings wherever Python converts, compares or concatenates them, as Perl does with the overloaded "" of Mojo’s objects. The string form is the one of to_str(), or of to_bytes() for an object that has only it.

A class that mixes it in overrides a conversion where its value means something else than its string form, as Pyjo.Date does with __float__(), or where it is a container of its own, as Pyjo.Path is a sequence of its parts: the mixin leaves out __len__, __iter__ and __contains__, which would make every object a sequence of characters.

The object is not a str, so a function that takes only an instance of it, like str.join() or urllib.parse.quote(), gets str(obj).

# 'value/value'
'/'.join(str(obj) for obj in (SubClass.new(), SubClass.new()))

See also

Pyjoyment, https://mojolicious.org.

Classes

class Pyjo.String.Mixin.Pyjo_String_Mixin[source]

This mixin does not provide own constructor method and implements the following new methods.

__add__(other: Anything) → Pyjo_String_Unicode | Pyjo_String_Bytes[source]
string = obj + 'string'

Concatenate the string form of the object with another string, as a Pyjo.String.Unicode object, or a Pyjo.String.Bytes object for bytes.

__bool__() → bool[source]
boolean = bool(obj)

True if the string form is not empty.

__bytes__() → bytes[source]
bstring = bytes(obj)

Bytes of the object, to_bytes(), or to_str() encoded with UTF-8.

__ceil__() → int[source]
number = math.ceil(obj)

The smallest integer that is not less than float(obj).

__complex__() → complex[source]
number = complex(obj)

Convert the string form into a complex number.

__eq__(other: Anything) → bool[source]
boolean = obj == other

True if the string form of the object is equal to the other value.

__float__() → float[source]
number = float(obj)

Convert the string form into a float number.

__floor__() → int[source]
number = math.floor(obj)

The largest integer that is not greater than float(obj).

__format__(format_spec: str) → str[source]
string = format(obj, '>10')
string = f'{obj:>10}'

Format the string form, str(obj), as format() formats a str.

__ge__(other: Anything) → bool[source]
boolean = obj >= other

True if the string form of the object is equal to or greater than the other value.

__gt__(other: Anything) → bool[source]
boolean = obj > other

True if the string form of the object is greater than the other value.

__hash__() → int[source]
number = hash(obj)

Hash of the string form, so that the object and its string are the same key of a dict.

__int__() → int[source]
number = int(obj)

Convert the string form into an integer number.

__le__(other: Anything) → bool[source]
boolean = obj <= other

True if the string form of the object is equal to or less than the other value.

__lt__(other: Anything) → bool[source]
boolean = obj < other

True if the string form of the object is less than the other value.

__ne__(other: Anything) → bool[source]
boolean = obj != other

True if the string form of the object is not equal to the other value.

__radd__(other: Anything) → Pyjo_String_Unicode | Pyjo_String_Bytes[source]
string = 'string' + obj

Concatenate another string with the string form of the object, as a Pyjo.String.Unicode object, or a Pyjo.String.Bytes object for bytes.

__repr__() → str[source]
string = repr(obj)

Representation of the object, with its string form: Pyjo.URL.new('http://example.com').

__round__(ndigits: int | None = None) → int | float[source]
number = round(obj)
number = round(obj, 2)

float(obj) rounded, to an integer without ndigits.

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

String form of the object, to_str(), or the representation of the bytes of to_bytes(), as str(b'value') gives it.

__trunc__() → int[source]
number = math.trunc(obj)

float(obj) without its fraction.