Hardware Reference
In-Depth Information
You will notice that the
RfidFileAuthenticator
class uses a
dict
- a dictionary. A
dic-
tionary
in Python is a way of storing a set of values that have a unique identiier. he unique
identiier is called the
key.
A value is stored with its associated key. For example, the follow-
ing example creates a dictionary stored in the variable
ages
and then associates the value
21
with the key
jim
and
43
with the key
tony
:
ages = dict()
ages[“jim”] = 21
ages[“tony”] = 43
he following code looks up the value for the key
jim
in the
ages
dictionary:
print ages[“jim”]
his prints the value
21
.
Open up an interactive Python session and try it yourself. Create a dictionary, add some key-
value pairs and then retrieve them.
In the
RfidFileAuthenticator
class a dictionary is used to store who has which RFID
tag. he RFID tag is used as the key (which is unique), and the name of the tag owner is
stored as the associated value. he dictionary is populated by reading each line in the ile in a
for
loop when the object is initialised.
self.tags[tag] = id
he
check
function checks if the tag that has been read is present in the dictionary with the
following line:
if token.secret in self.tags:
If the key is present, the associated value is looked up so that the name of the owner can be
printed.
Other languages may call a dictionary an
associative array,
a
hash,
a
hash table
or
key/value
pairings,
but they all have similar functionality. They are very useful in computing.