Package ckbot :: Module resolver :: Class Resolver
[hide private]
[frames] | no frames]

Class Resolver

source code


The resolver class implements a persistent two-way mapping, typically between strings.

Persistence is ensured by using a YAML file to store the mapping

Resolver maps both keys to values and values to keys.

If asked to map an unknown key or value, the code treats this as a value and auto-generates a key using a hash function.

EXAMPLE OF USE:

>>> r = Resolver("parrot.yml")
>>> r.get('parrot','is dead')
'is dead'
>>> r['met'] = 'its maker'
>>> r['met']
'its maker'
>>> r.save()
>>> r2 = Resolver("parrot.yml")
>>> r2['its maker']
'met'
Instance Methods [hide private]
 
__init__(self, fn)
Resolve using mapping stored in file named fn
source code
 
load(self)
Load contents from file storage.
source code
 
put(self, key, val)
The obvious
source code
 
_autokey(self, val)
Automatically generate a key for a value
source code
 
save(self)
Save mapping to a YAML file.
source code
 
__setitem__(self, key, value)
Set up the two-way mapping key<-->value NOTE: this does not automatically save !
source code
 
get(self, key, default=None)
Find key/value associated with key.
source code
 
__getitem__(self, key) source code
 
getWithAuto(self, key, autokey=None)
Find key/value associated with key.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, fn)
(Constructor)

source code 

Resolve using mapping stored in file named fn

NOTE: the file's directory must be read-write
  Resolver will save temporary files to this directory

Overrides: object.__init__

load(self)

source code 

Load contents from file storage.

Silently returns if file is not found / inaccessible

save(self)

source code 

Save mapping to a YAML file.

EXAMPLE: >>> r = Resolver('spam') >>> r['ham']='spam' >>> r['banana']='weapon' >>> r.save()

ALGORITHM: File is first saved to a temporary filename, then the old file is removed and the new file renamed (a.k.a. 'safe save')

get(self, key, default=None)

source code 

Find key/value associated with key. If not found, return default

getWithAuto(self, key, autokey=None)

source code 

Find key/value associated with key. If not found:

  • Parameter is a value
  • A key is generated by using autokey, or self._autokey(key) if None
  • The mapping is saved to disk