nustack
ReferenceNu STDmem

refs.items

Dict substrate item refs: typed value holders in nested dicts.

Module nustd.mem.refs.items.

Dict substrate item refs: typed value holders in nested dicts.

ItemRef combines the shape MutableItemRef blueprint (slot-level CRUD) with RefBase (dict navigation). Typed refs (IntRef, StrRef, ...) add the matching primitive Form so the value carries its full operator interface.

NameSortCallMeaning
BoolRefrefBoolRef(address, parent_ref=None, owner_shape=None)A bool slot in the dict substrate, carrying the whole Bool surface.
BytesRefrefBytesRef(address, parent_ref=None, owner_shape=None)A bytes slot in the dict substrate, carrying the whole Bytes surface.
FloatRefrefFloatRef(address, parent_ref=None, owner_shape=None)A float slot in the dict substrate, carrying the whole Float surface.
IntRefrefIntRef(address, parent_ref=None, owner_shape=None)An int slot in the dict substrate, carrying the whole Int surface.
ItemRefrefItemRef(address, value_type, value_value_type, parent_ref=None, owner_shape=None)A single stored value in the dict substrate, with no value interface.
StrRefrefStrRef(address, parent_ref=None, owner_shape=None)A str slot in the dict substrate, carrying the whole Str surface.

BoolRef

A bool slot in the dict substrate, carrying the whole Bool surface.

BoolRef(address, parent_ref=None, owner_shape=None)

Path nustd.mem.BoolRef. Kind Ref, sort ref, cardinality scalar.

Notes

  • An unwritten slot reads EMPTY, which is not False; use .exists() when the difference matters.

Example

class User(nu.Shape):
    active = nustd.mem.BoolRef.slot()
ctx = nu.Context().bind(dict, {"active": True}, User)
nu.run(User.active.not_(), ctx)[0]
False

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.forms.primitives.bool_.Bool:

CallBuildsMeaning
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.
a > bBoolSelf strictly greater than other.
a < bBoolSelf strictly less than other.
a >= bBoolSelf greater than or equal to other.
a <= bBoolSelf less than or equal to other.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

BytesRef

A bytes slot in the dict substrate, carrying the whole Bytes surface.

BytesRef(address, parent_ref=None, owner_shape=None)

Path nustd.mem.BytesRef. Kind Ref, sort ref, cardinality scalar.

Notes

  • Stored as raw bytes, so a data dict holding one is no longer JSON-serialisable as it stands.

Example

class Blob(nu.Shape):
    body = nustd.mem.BytesRef.slot()
ctx = nu.Context().bind(dict, {"body": b"hi"}, Blob)
nu.run(Blob.body.decode(), ctx)[0]
'hi'

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.forms.primitives.bytes_.Bytes:

CallBuildsMeaning
a + bBytesConcatenation of self and other.
a[key]Bytes | IntByte at an index, or a sub-range by slice.
a > bBoolSelf strictly greater than other.
a < bBoolSelf strictly less than other.
a >= bBoolSelf greater than or equal to other.
a <= bBoolSelf less than or equal to other.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.
.decode(encoding='utf-8')StrDecode self to a string using the given encoding.
.hex_()StrHex string of self, two digits per byte.
.upper()BytesSelf with ASCII letters uppercased.
.lower()BytesSelf with ASCII letters lowercased.
.strip(chars=None)BytesSelf with leading and trailing bytes removed.
.lstrip(chars=None)BytesSelf with leading bytes removed.
.rstrip(chars=None)BytesSelf with trailing bytes removed.
.split_bytes(sep=None, maxsplit=-1)ListSelf split into a List of Bytes on sep.
.find_bytes(sub, start=0, end=None)IntLowest index of sub in self, or -1 if absent.
.count_bytes(sub)IntNumber of non-overlapping occurrences of sub in self.
.startswith(prefix)BoolSelf starts with prefix.
.endswith(suffix)BoolSelf ends with suffix.
.replace(old, new, count=-1)BytesSelf with occurrences of old replaced by new.
.removeprefix(prefix)BytesSelf with prefix removed if present.
.removesuffix(suffix)BytesSelf with suffix removed if present.
.translate(table, delete=b'')BytesSelf translated through a 256-byte table, with bytes in delete dropped first.
.title()BytesSelf titlecased: each word's first cased byte upper, the rest lower.
.capitalize()BytesSelf with the first byte uppercased and the rest lowercased.
.swapcase()BytesSelf with uppercase and lowercase bytes swapped.
.rsplit_bytes(sep=None, maxsplit=-1)ListSelf split into a List of Bytes on sep, counting maxsplit from the right.
.splitlines(keepends=False)ListSelf split into a List of Bytes at line boundaries.
.partition(sep)TupleSelf split around the first occurrence of sep.
.rpartition(sep)TupleSelf split around the last occurrence of sep.
.rfind_bytes(sub, start=0, end=None)IntHighest index of sub in self, or -1 if absent.
.index_bytes(sub, start=0, end=None)IntLowest index of sub in self.
.rindex_bytes(sub, start=0, end=None)IntHighest index of sub in self.
.isascii()BoolSelf has only ASCII bytes.
.isdigit()BoolSelf has only ASCII digit bytes, and at least one.
.isalpha()BoolSelf has only ASCII letter bytes, and at least one.
.isalnum()BoolSelf has only ASCII alphanumeric bytes, and at least one.
.isspace()BoolSelf has only ASCII whitespace bytes, and at least one.
.istitle()BoolSelf is titlecased, with at least one cased byte.
.isupper()BoolSelf has all cased bytes uppercase, and at least one cased byte.
.islower()BoolSelf has all cased bytes lowercase, and at least one cased byte.
.center(width, fillbyte=b' ')BytesSelf centered in a field of width, padded with fillbyte.
.ljust(width, fillbyte=b' ')BytesSelf left-justified in a field of width, padded with fillbyte.
.rjust(width, fillbyte=b' ')BytesSelf right-justified in a field of width, padded with fillbyte.
.zfill(width)BytesSelf padded with leading zero bytes to width.
.expandtabs(tabsize=8)BytesSelf with tab bytes expanded to spaces.
.join(iterable)BytesSelf used as separator between the elements of iterable.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

FloatRef

A float slot in the dict substrate, carrying the whole Float surface.

FloatRef(address, parent_ref=None, owner_shape=None)

Path nustd.mem.FloatRef. Kind Ref, sort ref, cardinality scalar.

Notes

  • Nothing coerces on write: an int written here comes back an int.

Example

class User(nu.Shape):
    score = nustd.mem.FloatRef.slot()
ctx = nu.Context().bind(dict, {"score": 1.5}, User)
nu.run(User.score * 2, ctx)[0]
3.0

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.forms.primitives.float_.Float:

CallBuildsMeaning
a + bFloatSum of self and other.
a - bFloatSelf minus other.
a * bFloatProduct of self and other.
a / bFloatSelf divided by other.
a // bFloatSelf floor-divided by other.
a % bFloatSelf modulo other.
a ** bFloatSelf raised to the other power.
-aFloatNegation of self.
+aFloatSelf unchanged.
abs(a)FloatAbsolute value of self.
a > bBoolSelf strictly greater than other.
a < bBoolSelf strictly less than other.
a >= bBoolSelf greater than or equal to other.
a <= bBoolSelf less than or equal to other.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

IntRef

An int slot in the dict substrate, carrying the whole Int surface.

IntRef(address, parent_ref=None, owner_shape=None)

Path nustd.mem.IntRef. Kind Ref, sort ref, cardinality scalar.

Notes

  • Every Int call is available on it, so the ref itself is an operand: User.age + 1 builds arithmetic over the read.
  • The value is stored as a plain int, so the data dict stays JSON-shaped.

Example

class User(nu.Shape):
    age = nustd.mem.IntRef.slot()
ctx = nu.Context().bind(dict, {"age": 41}, User)
nu.run(User.age + 1, ctx)[0]
42

Methods

.inc(step=1)

Add step to the stored int and write the result back.

Builds None_.

Arguments

NameTypeDefaultMeaning
stepIntArg1

Notes

  • Read and write are two touches of the slot, not one atomic step; wrap it in a transaction when something else may write in between.
  • On an unwritten slot the read is EMPTY, so the addition is INVALID and that is what gets stored.

Example

class User(nu.Shape):
    age = nustd.mem.IntRef.slot()
data = {"age": 41}
ctx = nu.Context().bind(dict, data, User)
_ = nu.run(User.age.inc(), ctx)
data
{'age': 42}

.dec(step=1)

Subtract step from the stored int and write the result back.

Builds None_.

Arguments

NameTypeDefaultMeaning
stepIntArg1

Notes

  • Same two-touch read-then-write as inc.

Example

class User(nu.Shape):
    age = nustd.mem.IntRef.slot()
data = {"age": 41}
ctx = nu.Context().bind(dict, data, User)
_ = nu.run(User.age.dec(2), ctx)
data
{'age': 39}

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.forms.primitives.int_.Int:

CallBuildsMeaning
a + bInt | FloatSum of self and other.
a - bInt | FloatSelf minus other.
a * bInt | FloatProduct of self and other.
a / bFloatSelf divided by other.
a // bInt | FloatSelf floor-divided by other.
a % bInt | FloatSelf modulo other.
a ** bInt | FloatSelf raised to the other power.
-aIntNegation of self.
+aIntSelf unchanged.
abs(a)IntAbsolute value of self.
a > bBoolSelf strictly greater than other.
a < bBoolSelf strictly less than other.
a >= bBoolSelf greater than or equal to other.
a <= bBoolSelf less than or equal to other.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.
.bitand(other)IntBitwise AND: self & other.
.bitor(other)IntBitwise OR: self | other.
a ^ bIntBitwise XOR: self ^ other.
.bitnot()IntBitwise NOT: ~self.
a << bIntLeft shift: self shifted left by other bits.
a >> bIntRight shift: self shifted right by other bits.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

ItemRef

A single stored value in the dict substrate, with no value interface.

ItemRef(address, value_type, value_value_type, parent_ref=None, owner_shape=None)

Path nustd.mem.ItemRef. Kind Ref, sort ref, cardinality scalar.

The untyped leaf: it reads, writes and erases one key, and carries the element type as metadata for whoever needs it, but exposes none of the operators a typed ref does. Reach for it when the held type is decided by a container above (ListRef[i] and DictRef[k] both descend into one) rather than declared on a Shape.

Notes

  • The type carried is metadata only: nothing coerces or rejects a value on write, and nothing checks what comes back on read.

Example

class Port(nu.Shape):
    tags = nustd.mem.ListRef.slot(str)
ctx = nu.Context().bind(dict, {"tags": ["a", "b"]}, Port)
nu.run(Port.tags[1], ctx)[0]
'b'

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

StrRef

A str slot in the dict substrate, carrying the whole Str surface.

StrRef(address, parent_ref=None, owner_shape=None)

Path nustd.mem.StrRef. Kind Ref, sort ref, cardinality scalar.

Notes

  • Every Str call is available on it, so User.name.upper() reads the slot and builds the string op over it.

Example

class User(nu.Shape):
    name = nustd.mem.StrRef.slot()
ctx = nu.Context().bind(dict, {"name": "ada"}, User)
nu.run(User.name.upper(), ctx)[0]
'ADA'

Inherited methods

From nu.domains.shape.forms.item.MutableItemForm:

CallBuildsMeaning
.set(value)SetCmdBuild a SetCmd.
.erase()EraseBuild an Erase.
.init(value)IfDoSet value iff the leaf is currently missing.

From nu.domains.shape.forms.item.ItemForm:

CallBuildsMeaning
.exists()ExistsBuild an Exists query.
.missing()MissingBuild a Missing query.

From nu.forms.primitives.str_.Str:

CallBuildsMeaning
a + bStrConcatenation of self and other.
a[key]StrCharacter at an int index, or substring for a slice.
a > bBoolSelf strictly greater than other, lexicographically.
a < bBoolSelf strictly less than other, lexicographically.
a >= bBoolSelf greater than or equal to other, lexicographically.
a <= bBoolSelf less than or equal to other, lexicographically.
a == bBoolSelf equal to other by value.
a != bBoolSelf not equal to other by value.
.is_(other)BoolIdentity comparison: self is other.
.and_(other)BoolLogical AND of self and other.
.or_(other)BoolLogical OR of self and other.
.not_()BoolLogical NOT of self.
.bool_()BoolCast self to Bool.
.upper()StrSelf converted to uppercase.
.lower()StrSelf converted to lowercase.
.title()StrSelf converted to title case.
.capitalize()StrSelf with only the first character capitalized.
.swapcase()StrSelf with uppercase and lowercase characters swapped.
.casefold()StrSelf folded for aggressive, caseless matching.
.strip(chars=None)StrSelf with leading and trailing characters removed.
.lstrip(chars=None)StrSelf with leading characters removed.
.rstrip(chars=None)StrSelf with trailing characters removed.
.split(sep=None, maxsplit=-1)ListSelf split into a list on sep, from the left.
.rsplit(sep=None, maxsplit=-1)ListSelf split into a list on sep, from the right.
.splitlines(keepends=False)ListSelf split into a list at line boundaries.
.partition(sep)TupleSelf split around the first occurrence of sep into a 3-tuple.
.rpartition(sep)TupleSelf split around the last occurrence of sep into a 3-tuple.
.find(sub, start=0, end=None)IntLowest index in self where sub is found, searching from the left.
.rfind(sub, start=0, end=None)IntHighest index in self where sub is found, searching from the right.
.index(sub, start=0, end=None)IntLowest index in self where sub is found, searching from the left.
.rindex(sub, start=0, end=None)IntHighest index in self where sub is found, searching from the right.
.count_substring(sub)IntCount of non-overlapping occurrences of sub in self.
.startswith(prefix)BoolWhether self starts with prefix.
.endswith(suffix)BoolWhether self ends with suffix.
.isdigit()BoolWhether every character in self is a digit.
.isalpha()BoolWhether every character in self is alphabetic.
.isalnum()BoolWhether every character in self is alphanumeric.
.isspace()BoolWhether every character in self is whitespace.
.isnumeric()BoolWhether every character in self is numeric.
.isdecimal()BoolWhether every character in self is a decimal character.
.isidentifier()BoolWhether self is a valid Python identifier.
.isprintable()BoolWhether every character in self is printable.
.istitle()BoolWhether self is titlecased.
.isupper()BoolWhether every cased character in self is uppercase.
.islower()BoolWhether every cased character in self is lowercase.
.isascii()BoolWhether every character in self is ASCII.
.center(width, fillchar=' ')StrSelf centered in a field of the given width.
.ljust(width, fillchar=' ')StrSelf left-justified in a field of the given width.
.rjust(width, fillchar=' ')StrSelf right-justified in a field of the given width.
.zfill(width)StrSelf padded with leading zeros to the given width.
.expandtabs(tabsize=8)StrSelf with tab characters expanded to spaces.
.replace(old, new, count=-1)StrSelf with occurrences of old replaced by new.
.removeprefix(prefix)StrSelf with the given prefix removed if present.
.removesuffix(suffix)StrSelf with the given suffix removed if present.
.translate(table)StrSelf with characters mapped through a translation table.
.format_map(mapping)StrSelf formatted with {field} placeholders filled from mapping.
.encode(encoding='utf-8')BytesSelf encoded to Bytes.
.join(iterable)StrElements of iterable joined together with self as separator.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

On this page