cdbx._cdb module

Copyright:

Copyright 2016 - 2023 André Malo or his licensors, as applicable

License:

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

cdbx - CDB Reimplementation for Python

cdbx - CDB Reimplementation for Python

class cdbx._cdb.CDB(file, close=None, mmap=None)

Create a CDB instance from a file.

Parameters:
  • file (file or str or int) – Either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

  • close (bool) – Close a passed in file automatically? This argument is only applied if file is a python stream or an integer. If omitted or None it defaults to False.

  • mmap (bool) – Access the file by mapping it into memory? If True, mmap is required. If false, mmap is not even tried. If omitted or None, it’s attempted but no error on failure.

__contains__(self, key)

Check if the key appears in the CDB.

Note that in case of a unicode key, it will be transformed to a byte string using the latin-1 encoding.

Parameters:

key (str or bytes) – Key to look up

Returns:

Does the key exist?

Return type:

bool

__getattribute__(name, /)

Return getattr(self, name).

__getitem__(self, key)

Find the first value of the passed key and return the value as bytestring

Note that in case of a unicode key, it will be transformed to a byte string using the latin-1 encoding.

Parameters:

key (str or bytes) – Key to look up

Returns:

The first value of the key

Return type:

bytes

Raises:

KeyError – Key not found

__hash__ = None
__iter__(self)

Create an iterator over unique keys - in insertion order

Returns:

The key iterator

Return type:

iterable

__len__(self)

Count the number of unique keys

Returns:

The number of unique keys

Return type:

int

static __new__(cls, file, close=None, mmap=None)

Create a CDB instance.

Parameters:
  • file (file or str or int) – Either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

  • close (bool) – Close a passed in file automatically? This argument is only applied if file is a python stream or an integer. If omitted or None it defaults to False.

  • mmap (bool) – Access the file by mapping it into memory? If True, mmap is required. If false, mmap is not even tried. If omitted or None, it’s attempted but no error on failure.

Returns:

New CDB instance

Return type:

CDB

close(self)

Close the CDB.

fileno(self)

Find the underlying file descriptor

Returns:

The underlying file descriptor

Return type:

int

get(self, key, default=None, all=False)

Return value(s) for a key

If key is not found, default is returned. If key was found, then depending on the all flag the value return is either a byte string (all == False) or a list of byte strings (all == True).

Note that in case of a unicode key, it will be transformed to a byte string using the latin-1 encoding.

Parameters:
  • key (bytes) – Key to lookup

  • default – Default value to pass back if the key was not found

  • all (bool) – Return all values instead of only the first? Default: False

Returns:

The value(s) or the default

has_key(self, key)

Check if the key appears in the CDB.

Note that in case of a unicode key, it will be transformed to a byte string using the latin-1 encoding.

Parameters:

key (str or bytes) – Key to look up

Returns:

Does the key exist?

Return type:

bool

items(self, all=False)

Create key/value pair iterator

Parameters:

all (bool) – Return all (i.e. non-unique-key) items? Default: False

Returns:

Iterator over items

Return type:

iterable

keys(self, all=False)

Create key iterator

Parameters:

all (bool) – Return all (i.e. non-unique) keys? Default: False

Returns:

Iterator over keys

Return type:

iterable

make(cls, file, close=None, mmap=None)

Create a CDB maker instance, which returns a CDB instance when done.

Parameters:
  • file (file or str or int) – Either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

  • close (bool) – Close a passed in file automatically? This argument is only applied if file is a python stream or an integer. If omitted or None it defaults to False. This argument is applied on commit.

  • mmap (bool) – Access the file by mapping it into memory? If True, mmap is required. If false, mmap is not even tried. If omitted or None, it’s attempted but no error on failure. This argument is applied on commit.

Returns:

New maker instance

Return type:

CDBMaker

class cdbx._cdb.CDBMaker

CDBMaker - use CDB.make to create instance

__getattribute__(name, /)

Return getattr(self, name).

__hash__ = None
add(self, key, value)

Add the key/value pair to the CDB-to-be.

Note that in case of a unicode key or value, it will be transformed to a byte string using the latin-1 encoding.

Parameters:
  • key (str or bytes) – Key

  • value (str or bytes) – Value

close(self)

Close the CDBMaker and destroy the file (if it was created by the maker or explicitly requested in the constructor)

commit(self)

Commit to the current dataset and finish the CDB creation.

The commit method returns a new CDB instance based on the file just committed.

Returns:

New CDB instance

Return type:

CDB

fileno(self)

Find underlying file descriptor

Returns:

The file descriptor

Return type:

int