nustack

refs.items

mem leaves: one value at one key, read and written whole.

Module nu.mem.refs.items.

mem leaves: one value at one key, read and written whole.

ItemRef is the leaf itself (slot-level read, write and erase over one key of a nested dict) and declares a slot once for every leaf. Each value leaf mixes one value form in beside it, so the ref is an operand of the value it names; the form decides the surface, the leaf decides where the value lives.

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.
ObjectRefrefObjectRef(address, parent_ref=None, owner_shape=None)A single stored value in the dict substrate, of no declared type.
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 nu.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 = nu.BoolRef.slot()
ctx = nu.Context().bind(dict, {"active": True}, User)
nu.run(User.active.not_(), ctx)[0]
False

Inherited methods

From nu.domains.shape.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

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.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

BytesRef

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

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

Path nu.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 = nu.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.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

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.
.len()IntLength of self in bytes.
.contains(item)BoolSubstring test: item in 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.
.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.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

FloatRef

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

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

Path nu.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 = nu.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.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

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.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

IntRef

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

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

Path nu.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 = nu.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 EMPTY and the write raises.

Example

class User(nu.Shape):
    age = nu.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 = nu.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.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

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 of self and other.
.bitor(other)IntBitwise OR of self and other.
.bitxor(other)IntBitwise XOR of self and other.
.bitnot()IntBitwise NOT of self.
.lshift(other)IntSelf shifted left by other bits.
.rshift(other)IntSelf shifted right by other bits.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

ObjectRef

A single stored value in the dict substrate, of no declared type.

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

Path nu.mem.ObjectRef. Kind Ref, sort ref, cardinality scalar.

What a container descends to when its value is object, undeclared, or a type with no mem leaf of its own. It carries the whole Object surface, so the ref is still an operand: == builds an Eq, attribute and subscript access descend into the value read, and a cast narrows it.

Example

class Port(nu.Shape):
    meta = nu.DictRef.slot(object)
ctx = nu.Context().bind(dict, {"meta": {"title": "hi"}}, Port)
nu.run(Port.meta["title"] == "hi", ctx)[0]
True

Inherited methods

From nu.domains.shape.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

From nu.forms.primitives.object_.Object:

CallBuildsMeaning
a + bObjectSum of self and other.
a - bObjectSelf minus other.
a * bObjectProduct of self and other.
a @ bObjectMatrix multiplication: self @ other.
a / bObjectSelf divided by other.
a // bObjectSelf floor-divided by other.
a % bObjectSelf modulo other.
a ** bObjectSelf raised to the other power.
-aObjectNegation of self.
+aObjectSelf unchanged.
abs(a)ObjectAbsolute 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)ObjectPython's & on the values: bits for ints, intersection for sets.
.bitor(other)ObjectPython's | on the values: bits for ints, union for sets, merge for dicts.
.bitxor(other)ObjectPython's ^ on the values: bits for ints, symmetric difference for sets.
.bitnot()ObjectPython's ~ on the value.
.lshift(other)ObjectSelf shifted left by other bits: Python's << on the values.
.rshift(other)ObjectSelf shifted right by other bits: Python's >> on the values.
.union(other)ObjectSet union of self and other.
.intersection(other)ObjectSet intersection of self and other.
.difference(other)ObjectElements of self that are not in other.
.symmetric_difference(other)ObjectElements in exactly one of self and other.
.merge(other)ObjectSelf and other merged into a new mapping: Python's dict |.
a[key]ObjectSubscript access: self[key].
.len()IntLength of self.
.contains(item)BoolMembership test: item in self.
.iter()IteratorLazy iterator stream over self's elements.
.has_attr(name)BoolAttribute presence: hasattr(self, name).

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

StrRef

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

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

Path nu.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 = nu.StrRef.slot()
ctx = nu.Context().bind(dict, {"name": "ada"}, User)
nu.run(User.name.upper(), ctx)[0]
'ADA'

Inherited methods

From nu.domains.shape.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.item.ItemForm:

CallBuildsMeaning
.exists()BoolWhether this slot holds a value, as a Bool.
.missing()BoolWhether this slot holds no value, as a Bool: the negation of exists().

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.
.len()IntLength of self in characters.
.contains(item)BoolSubstring test: item in self.
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.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

On this page