[1]:
import quanguru as qg

keySearch#

keySearch method searches for a key in a dictionary.keys(). This method is mainly used in aliasDict class (extending dict class) to find the actual key when using aliasClass as the key, which returns equal for a specific string (its name) or any other string in its list of aliases.

[2]:
# an instance of an aliasClass with a name and a list of aliases
key1 = qg.base.aliasClass(name="k1", alias=[1, 4, "elias", "string"])

# a dictionary with an aliasClass instance used as the first key
exampleDict = {key1: 1, "key2": "two"}

# keySearch method returns the key itself regardless of whether the key is in the dictionary or not
print(qg.base.keySearch(exampleDict, "someKey"))
print(qg.base.keySearch(exampleDict, "key2"))

# if the key is an object, both the object itself and its name can be passed
print(qg.base.keySearch(exampleDict, key1))
print(qg.base.keySearch(exampleDict, "k1"))

# also, any of the aliases from the obeject's alias list can be used
print(qg.base.keySearch(exampleDict, 1))
print(qg.base.keySearch(exampleDict, "elias"))
someKey
key2
k1
k1
k1
k1

Since two aliasClass objects are considered equal when their names are the same, or at least one of their aliases are the same, a different instance of aliasClass can be passed to the keySearch method.

[3]:
# a different instance of an aliasClass with its alias ("elias") equal to that of key1 object
exampleKey = qg.base.aliasClass(name="exK", alias=["example", "elias"])

# since exampleKey and key1 objects are equal, keySearch method returns key1.name
print(qg.base.keySearch(exampleDict, exampleKey))
k1

aliasDict#

aliasDict class extends regular dict class, by treating keys satisfying key1 == keys2 as the same key. aliasClass objects can be used as keys, and thus the value of the key can be accessed by passing the name or any of the aliases of the aliasClass object.

aliasDict class can be used in various dictionaries in the library such as _allInstacesDict, subSys, allResults, etc.

[4]:
# an instance of an aliasDict class with the first key as an aliasClass object
alDict = qg.base.aliasDict([(key1, 1), ("key2", "two")])

# aliasDict method works like a regular dictionary
print(alDict[key1])
print(alDict["key2"])

# since key1 is an aliasClass object, its name and any alias can be passed
print(alDict[key1.name])
print(alDict[key1.alias[0]])
print(alDict["k1"])
print(alDict["elias"])
1
two
1
1
1
1
[5]:
# if some other aliasClass object has the same name as the name of the key
# in a dictionary, this object can also be used to get the value of this key
someOther = qg.base.aliasClass(name="key2", alias=["are", "you"])
print(alDict[someOther])

# this works for aliases too
# an aliasClass object with the same alias as that of key1 object
withAlias = qg.base.aliasClass(name="withAlias", alias=["elias", "you"])
print(alDict[withAlias])
two
1
[6]:
# get() method works as usual
print(alDict.get(key1))
print(alDict.get("key2"))
print(alDict.get("randomString"))

# the name and any alias can be used if the key is an object
print(alDict.get("k1"))
print(alDict.get("string"))
1
two
None
1
1
[7]:
# a new key-value pair can be added to a dictionary
alDict["third"] = "string3"
print(alDict)

# a value can also be updated
alDict["third"] = "str3"
print(alDict)

# the same can be done for a key which is an aliasClass object
alDict[key1] = "str1"
print(alDict)

# name and any alias can also be used
alDict["k1"] = "str1name"
print(alDict)

alDict["elias"] = "str1alias"
print(alDict)

# and any other object which is equal to a key can also be passed
alDict[exampleKey] = "str1example"
print(alDict)
{'k1': 1, 'key2': 'two', 'third': 'string3'}
{'k1': 1, 'key2': 'two', 'third': 'str3'}
{'k1': 'str1', 'key2': 'two', 'third': 'str3'}
{'k1': 'str1name', 'key2': 'two', 'third': 'str3'}
{'k1': 'str1alias', 'key2': 'two', 'third': 'str3'}
{'k1': 'str1example', 'key2': 'two', 'third': 'str3'}
[8]:
# any item can be deleted as follows
print(alDict)
del alDict["third"]
print(alDict)

# an object itself can also be used for deleting
del alDict[key1]
print(alDict)

# here we add it again to show more examples
alDict[key1] = "str1"
print(alDict)

# a name of the object can be used
del alDict["k1"]
print(alDict)

# add again and use an alias instead
alDict[key1] = "str1"
print(alDict)
del alDict["string"]
print(alDict)


{'k1': 'str1example', 'key2': 'two', 'third': 'str3'}
{'k1': 'str1example', 'key2': 'two'}
{'key2': 'two'}
{'key2': 'two', 'k1': 'str1'}
{'key2': 'two'}
{'key2': 'two', 'k1': 'str1'}
{'key2': 'two'}
[9]:
# let's add key1 back again
alDict[key1] = "str1"

# we can check whether aliasDict contains a specific key or not
print("key2" in alDict)

# aliasClass object, its name and any of its aliases can be used as well
print(key1 in alDict)
print("k1" in alDict)
print("elias" in alDict)

# when aliasDict object does not contain a specific key if shows False
print("someKey" in alDict)

# if some other object of the aliasClass (exampleKey) is equal to a key (key1) in aliasDict object
print(exampleKey in alDict)
True
True
True
True
False
True
[10]:
print(alDict)

# let us define various aliasDict objects
otherDict1 = qg.base.aliasDict([("ok1", "v1"), ("key2", "v2")])
otherDict2 = qg.base.aliasDict([("k1", "odv2")])
otherDict3 = qg.base.aliasDict([("elias", "odv3")])
otherDict4 = qg.base.aliasDict([(exampleKey, "odv4")])

# we can merge any of these objects to our previous alDict object
# new key-value pair will be added if the original dictionary doesn't contain this pair
# otherwise, the value of the key will be updated if this key is present.
alDict.update(otherDict1)
print(alDict)

# update() method works if name, alias, or any aliasClass is equal to a key in the dictionary
alDict.update(otherDict2)
print(alDict)

alDict.update(otherDict3)
print(alDict)

alDict.update(otherDict4)
print(alDict)

{'key2': 'two', 'k1': 'str1'}
{'key2': 'v2', 'k1': 'str1', 'ok1': 'v1'}
{'key2': 'v2', 'k1': 'odv2', 'ok1': 'v1'}
{'key2': 'v2', 'k1': 'odv3', 'ok1': 'v1'}
{'key2': 'v2', 'k1': 'odv4', 'ok1': 'v1'}
[12]:
# aliasDict class has a setdefault method similar to that of regular dictionary's method
print(alDict.setdefault("key2"))

# if the key is an aliasClass object, its name, alias and object itself can be passed
print(alDict.setdefault(key1))
print(alDict.setdefault("k1"))
print(alDict.setdefault("elias"))

# it returns None if the key is not present in the aliasDict
print(alDict.setdefault("someKey"))
# and this key is added with None value to the aliasDict
print(alDict)

# however, if the value is passed in addition to a key, which is not
# present in the aliasDict, this value is returned. This key-value pair becomes part of aliasDict
print(alDict.setdefault("newKey", 7))
print(alDict)



v2
odv4
odv4
odv4
None
{'key2': 'v2', 'k1': 'odv4', 'ok1': 'v1', 'someKey': None}
7
{'key2': 'v2', 'k1': 'odv4', 'ok1': 'v1', 'someKey': None, 'newKey': 7}
[13]:
# pop() method can be applied as well
print(alDict.pop("newKey"))
print(alDict)
print(alDict.pop("someKey"))
print(alDict)


# it can be used for keys which are aliasClass objects
print(alDict.pop(key1))
print(alDict)

# let's add this key back again to show more examples
alDict[key1] = "v1"
print(alDict)

# object's name can be passed
print(alDict.pop("k1"))
print(alDict)

# add this key back to show one more example
alDict[key1] = "v1"

# and any alias too
print(alDict.pop("elias"))
print(alDict)
7
{'key2': 'v2', 'k1': 'odv4', 'ok1': 'v1', 'someKey': None}
None
{'key2': 'v2', 'k1': 'odv4', 'ok1': 'v1'}
odv4
{'key2': 'v2', 'ok1': 'v1'}
{'key2': 'v2', 'ok1': 'v1', 'k1': 'v1'}
v1
{'key2': 'v2', 'ok1': 'v1'}
v1
{'key2': 'v2', 'ok1': 'v1'}
[14]:
# copy() method is applicable as well
aldCopy = alDict.copy()
print(aldCopy)
{'key2': 'v2', 'ok1': 'v1'}