Package TEES :: Package Core :: Module IdSet :: Class IdSet
[hide private]

Class IdSet

source code

A mapping from strings to id integers. This class is used for defining the ids for classes and features of machine learning systems.

Instance Methods [hide private]
 
__init__(self, firstNumber=1, idDict=None, locked=False, filename=None, allowNewIds=True)
Creates a new IdSet or loads one from a dictionary or a file.
source code
int or None
getId(self, key, createIfNotExist=None)
Returns the id number for a name.
source code
 
__getitem__(self, name)
Calls getId through the []-operator.
source code
 
defineId(self, name, id)
Give a specific id for a certain name.
source code
str or None
getName(self, id)
Returns the name corresponding to the identifier.
source code
 
getNames(self)
Returns a sorted list of all names.
source code
 
getIds(self)
Returns a sorted list of id numbers.
source code
 
write(self, filename)
Writes the name/id pairs to a file, one pair per line, in the format "name: id".
source code
 
load(self, filename)
Loads name/id pairs from a file.
source code
Method Details [hide private]

__init__(self, firstNumber=1, idDict=None, locked=False, filename=None, allowNewIds=True)
(Constructor)

source code 

Creates a new IdSet or loads one from a dictionary or a file.

To create a new, empty set: idset = IdSet(firstNumber = x). To create a set from a str->int dictionary: idset = IdSet(idDict = x). To load a dictionary from a file: idset = IdSet(filename = x).

Parameters:
  • firstNumber (int) - The number given to the first name defined. Subsequent names will have higher numbers.
  • idDict (dictionary) - Dictionary of name / integer pairs. The integer values must be unique.
  • locked (boolean) - Whether new names can be added to the set. If set to True, getId will return None for names that are not already in the set.
  • filename (str) - load name/id pairs from a file

getId(self, key, createIfNotExist=None)

source code 

Returns the id number for a name. If the name doesn't already have an id, a new id is defined, unless createIfNotExist is set to false, in which case None is returned for these cases.

Parameters:
  • key (str) - name
  • createIfNotExist (True, False or None) - If the name doesn't have an id, define an id for it
Returns: int or None
an identifier

defineId(self, name, id)

source code 

Give a specific id for a certain name. Neither the name nor the id must exist in the set and the id must be smaller than the largest id already in the set. Usually this method is used only when inserting name/id pairs from an existing source.

getName(self, id)

source code 

Returns the name corresponding to the identifier. If the identifier doesn't exits, returns None.

Parameters:
  • id (int) - the identifier number
Returns: str or None
a name

getNames(self)

source code 

Returns a sorted list of all names. Can be slow for large IdSets.

getIds(self)

source code 

Returns a sorted list of id numbers. Can be slow for large IdSets.

load(self, filename)

source code 

Loads name/id pairs from a file. The IdSet is cleared of all existing ids before loading the ones from the file.