Data Structures#
- class pyhmmer.easel.Bitfield#
A statically sized sequence of booleans stored as a packed bitfield.
Example
Instantiate a bitfield from an iterable, where each object will be tested for truth:
>>> bitfield = Bitfield([True, False, False]) >>> len(bitfield) 3 >>> bitfield[0] True >>> bitfield[1] False
Use
Bitfield.zerosandBitfield.onesto initialize a bitfield of a given length with all fields set to \(0\) or \(1\):>>> Bitfield.zeros(4) Bitfield([False, False, False, False]) >>> Bitfield.ones(4) Bitfield([True, True, True, True])
Use indexing to access and edit individual bits:
>>> bitfield[0] = True >>> bitfield[0] True >>> bitfield[0] = False >>> bitfield[0] False
- __init__(iterable)#
Create a new bitfield from an iterable of objects.
Objects yielded by the iterable can be of any type and will be tested for truth before setting the corresponding field.
- Raises:
ValueError – When given an empty iterable.
- copy()#
Return a copy of this bitfield object.
Added in version 0.7.0.
- count(value=1)#
Count the number occurrences of
valuein the bitfield.If no argument is given, counts the number of
Trueoccurences.Example
>>> bitfield = Bitfield.zeros(8) >>> bitfield.count(False) 8 >>> bitfield[0] = bitfield[1] = True >>> bitfield.count() 2
- classmethod ones(n)#
Create a new bitfield of size
nwith all elements set toTrue.Added in version 0.7.0.
- toggle(index)#
Switch the value of one single bit.
Example
>>> bitfield = Bitfield.zeros(8) >>> bitfield[0] False >>> bitfield.toggle(0) >>> bitfield[0] True >>> bitfield.toggle(0) >>> bitfield[0] False
- class pyhmmer.easel.KeyHash#
A dynamically resized container to store byte keys using a hash table.
Internally uses Bob Jenkins’ one at a time hash, a simple and efficient hash function published in 1997 that exhibits avalanche behaviour.
Example
Add new keys to the key hash using the
addmethod like you would with a Pythonset:>>> kh = KeyHash() >>> kh.add("key") 0
Check if a key hash contains a given key:
>>> "key" in kh True >>> "missing" in kh False
Get the index associated with a key using the indexing notation:
>>> kh["key"] 0 >>> kh["missing"] Traceback (most recent call last): ... KeyError: 'missing'
Iterate over the keys of the key hash, in the order of insertion:
>>> kh.add("key2") 1 >>> for k in kh: ... print(k) key key2
See also
The Wikipedia article for Bob Jenkins’ hash functions: https://en.wikipedia.org/wiki/Jenkins_hash_function
- __init__()#
Create a new empty key-hash collection.
- add(key)#
Add a new key to the hash table, and return its index.
If
keywas already in the hash table, the previous index is returned:>>> kh = KeyHash() >>> kh.add("first") 0 >>> kh.add("second") 1 >>> kh.add("first") 0
- Parameters:
key (
bytes) – The key to add to the hash table.- Returns:
int– The index corresponding to the addedkey.
Added in version 0.3.0.
- clear()#
Remove all entries from the collection.
- copy()#
Create and return an exact copy of this mapping.
Example
>>> kh = KeyHash() >>> kh.add("key") 0 >>> copy = kh.copy() >>> "key" in copy True