xnano.symbols
xnano.symbols
¶
xnano.symbols
Names for values: register a picture, a note, a pixel or a sound once and use it by name.
A symbol is a name, not a class. symbol='wall' on a value registers it here, and a
function that takes a str where such a value fits looks the name up, so Pixel('wall')
or play('kick') uses what was registered. The registry is shared by the whole program.
Functions:
-
register–Register
valueundername, so that functions that take a name can use it. -
lookup–Return the value registered under
name. -
names–Return the registered names, in alphabetical order.
-
remove–Forget the value registered under
name. Removing a name that is not there does nothing.
register
¶
Register value under name, so that functions that take a name can use it.
Registering a name again with a value of the same kind keeps the newer value, so code that builds the same symbol twice (a function called again, a notebook cell run again) keeps working. A name already taken by another kind of value is refused, so a lookup that worked before can never silently change.
Parameters:
-
name(str) –The name, two characters or more, without spaces. It may not be the name of a pixel preset, a note name such as
'C4','rest'or a number. -
value(object) –The value to register.
Raises:
-
ValueError–The name cannot be a symbol, or another kind of value already has it.
-
TypeError–The name is not a string.
Examples:
import xnano
xnano.symbols.register('logo', xnano.Color('#336699'))
assert xnano.symbols.lookup('logo') == xnano.Color('#336699')
xnano.symbols.remove('logo')
Source code in xnano/symbols.py
lookup
¶
Return the value registered under name.
Parameters:
-
name(str) –The registered name.
-
kind(type[ValueT] | None, default:None) –The class the value must be an instance of, such as
xnano.Image;Noneaccepts any kind.
Raises:
-
ValueError–Nothing of that kind is registered under the name. The message names the closest registered names first, then every known one.
Examples:
import xnano
xnano.symbols.register('accent', xnano.Color('#ff8800'))
assert xnano.symbols.lookup('accent', kind=xnano.Color).to_rgb() == (255, 136, 0)
xnano.symbols.remove('accent')
Source code in xnano/symbols.py
names
¶
Return the registered names, in alphabetical order.
Parameters:
-
kind(type | None, default:None) –Only the names of values that are instances of this class;
Nonefor all.
Examples:
import xnano
xnano.symbols.register('sky', xnano.Color('#88ccff'))
assert 'sky' in xnano.symbols.names(kind=xnano.Color)
xnano.symbols.remove('sky')