[1]:
import quanguru as qg
aliasClass
#
aliasClass
is introduced for the naming functionality, that is every qObject
has a unique default name, and users can assign any number of aliases to any qObject
, but the aliases also need to be unique.
Uniqueness of the default names and the user defined aliases are ensured by the named
class (first class in the qObject
inheritance tree), which has aliasClass
as its name
attribute.
The name attribute of the named
class, which is an aliasClass
instance, is also used internally as dictionary keys, e.g. in subSys
dictionary (to be precise subSys
is an extended dictionary class called aliasDict
to work with aliasClass
).
aliasClass
itself is simply a container of the name and aliases.
[2]:
# set a name and a list of aliases at instantiation. aliases can be anything, e.g. even another instance of aliasClass
ac1 = qg.base.aliasClass(name="ac1", alias=["string", 2, qg.base.aliasClass()])
print(ac1.name, ac1.alias)
# name and alias can also be set after the instantiation through the corresponding properties
ac2 = qg.base.aliasClass()
ac2.name = "some name"
ac2.alias = [1, "2"]
print(ac2.name, ac2.alias)
ac1 ['string', 2, None]
some name [1, '2']
[3]:
# names have to be string. name setter raises a TypeError for any other type
try:
ac3 = qg.base.aliasClass(name=1)
except TypeError as te:
print(te)
try:
ac4 = qg.base.aliasClass()
ac4.name = 2
except TypeError as te:
print(te)
name should be an instance of <class 'str'>, <class 'NoneType'>, but <class 'int'> is given
0th argument should be an instance of <class 'str'>, <class 'NoneType'>, but <class 'int'> is given
[4]:
# you cannot change the name (through the setter) once its set. It will raise a warning
ac2.name = "some other name"
# but, if you really want to change the name, you can do it through the name mangled _aliasClass__name attribute
ac2._aliasClass__name = "changed name"
print(ac2.name)
changed name
/Volumes/T7/Dropbox/Codes/QuanGuru/src/quanguru/classes/base.py:114: UserWarning: name cannot be changed
warnings.warn("name cannot be changed")
[5]:
# since alias getter returns a list, you can call usual list method to modify the aliases
# append an alias
ac2.alias.append("new alias")
print(ac2.alias)
# remove an alias
ac2.alias.remove(1)
print(ac2.alias)
# change the alias a specific index
ac2.alias[0] = 1
print(ac2.alias)
# but note that if you want to completely replace the list of aliases, below will not do it (alias setter ALWAYS appends)
# this calls the alias setter, and the setter just appends the elements of given list to the new list
ac2.alias = ["3" , "changed"]
print(ac2.alias)
# this means that you can also use the setter to add a single alias to existing list
# below will not replace the list with the given string, it will append the given string into the list
ac2.alias = "through setter"
print(ac2.alias)
# if you really want to completely replace the list of aliases with another
# the first option is to clear the existing list through the getter and append the elements of the new list with the setter
ac2.alias.clear()
ac2.alias = ["a", "b"]
print(ac2.alias)
# alternatively, you can directly change the name mangled _aliasClass_alias attribute
ac2._aliasClass__alias = ["c", "d"]
print(ac2.alias)
[1, '2', 'new alias']
['2', 'new alias']
[1, 'new alias']
[1, 'new alias', '3', 'changed']
[1, 'new alias', '3', 'changed', 'through setter']
['a', 'b']
['c', 'd']
[6]:
# two aliasClass objects are equal if they contain at least one common element
# they are equal, if their names are the same (even if they have different aliases)
ac5 = qg.base.aliasClass(name="same name")
ac6 = qg.base.aliasClass(name="same name", alias="different alias")
print(ac5 == ac6, ac5.alias, ac6.alias)
print(ac5 is not ac6)
# they are equal, if they contain at least one common alias
ac7 = qg.base.aliasClass(name="dif name 1", alias=["dif alias 1", "same alias"])
ac8 = qg.base.aliasClass(name="dif name 2", alias=["same alias", "dif alias 2"])
print(ac7 == ac8)
# they are equal, if the name of one is contained in the alias list of the other
ac9 = qg.base.aliasClass(name="name and alias", alias=["a", "b"])
ac10 = qg.base.aliasClass(name="diff name 3", alias=["name and alias"])
print(ac9 == ac10)
print(ac9 != "diff name 3")
True [] ['different alias']
True
True
True
True
[7]:
# hash value of an aliasClass is equal to the hash of its name
print(hash(qg.base.aliasClass(name="this string", alias=["e", "f"])) == hash("this string"))
# so its __repr__ return repr(self.name)
print(qg.base.aliasClass(name="repr of name", alias="repr of alias").__repr__() == repr("repr of name"))
print(qg.base.aliasClass(name="repr of name", alias="repr of alias").__repr__() != repr("repr of alias"))
True
True
True
[8]:
# also the string representation of an aliasClass is its name
print(qg.base.aliasClass(name="str of name", alias="str of alias"))
str of name
[ ]: