cdbx package

Module contents

Copyright:

Copyright 2016 - 2018 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(file)

Create a CDB instance from a file.

‘file’ can be either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

__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 ascii encoding.

Parameters:
key : basestring

Key to look up

Return:

Does the key exists?

Rtype:

bool

__getattribute__

x.__getattribute__(‘name’) <==> x.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 ascii encoding.

Parameters:
key : basestring

Key to look up

Return:

The first value of the key

Rtype:

bytes

Exceptions:
  • KeyError : Key not found
__hash__ = None
__iter__(self)

Create an iterator over unique keys - in insertion order

Return:The key iterator
Rtype:iterable
__len__(self)

Count the number of unique keys

Return:The number of unique keys
Rtype:int
static __new__(cls, file)

Create a CDB instance.

Parameters:
file : file or str or fd

Either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

Return:

New CDB instance

Rtype:

CDB

close(self)

Close the CDB.

fileno(self)

Find the underlying file descriptor

Return:The underlying file descriptor
Rtype: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 ascii encoding.

Parameters:
key : bytes

Key to lookup

default : any

Default value to pass back if the key was not found

all : bool

Return all values instead of only the first? Default: False

Return:

The value(s) or the default

Rtype:

any

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 ascii encoding.

Return:Does the key exists?
Rtype:bool
items(self, all=False)

Create key/value pair iterator

Parameters:
all : bool

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

Return:

Iterator over items

Rtype:

iterable

keys(self, all=False)

Create key iterator

Parameters:
all : bool

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

Return:

Iterator over keys

Rtype:

iterable

make(cls, file)

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

Parameters:
file : file or str or fd

Either a (binary) python stream (providing fileno()) or a filename or an integer (representing a filedescriptor).

Return:

New maker instance

Rtype:

CDBMaker

class cdbx.CDBMaker

CDBMaker - use CDB.make to create instance

__getattribute__

x.__getattribute__(‘name’) <==> x.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 ascii encoding.

Parameters:
key : bytes

Key

value : bytes

Value

close(self)

Close the CDBMaker and destroy the file (if it was created by the maker)

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.

Return:New CDB instance
Rtype:CDB
fileno(self)

Find underlying file descriptor

Return:The file descriptor
Rtype:int