ljson.base package

Submodules

ljson.base.disk module

An on-disk ljson implementation.

class ljson.base.disk.QueryResult(table, selector, selected, list_)[source]

Bases: ljson.base.generic.LjsonQueryResult

See ljson.base.generic.LjsonQueryResult.

class ljson.base.disk.Selector(header, dct, table)[source]

Bases: ljson.base.generic.LjsonSelector

getone(column=None)[source]
class ljson.base.disk.Table(header, file_, is_headless=False)[source]

Bases: ljson.base.generic.LjsonTable

An on-disk ljson table.

One should not use the constructor to open a file but instead use the static method from_file.

WARNING: file_ must be opened in w+ mode!

additem(row)[source]

Add a new row.

close()[source]

Close the underlaying file.

static from_file(fin)[source]

WARNING: file_ must be opened in r+ mode!

static open(filename)[source]

Equivalent to Table.from_file(open(filename, "r+"))

save(fout)[source]

Save this table to the file fout.

ljson.base.generic module

Some generic functions and classes for ljson.

They are used by the memory and the disk implementation.

class ljson.base.generic.Header(descriptor)[source]

Bases: object

check_data(key, value)[source]

Typecheck the given key value pair

static from_file(fin)[source]

Construct the header from the file.

get_header()[source]

Return a JSON header.

class ljson.base.generic.LjsonQueryResult(table, selector, selected, list_)[source]

Bases: object

This is the class that is used to handle query results for LJSON.

Until v0.2.0 query results were just lists or scalar objects (returned by LjsonSelector.getone(column != 0)).

This resulted in undefined (and unwanted) behaviour in many use cases, like table[{"some_field": some_value}]["some_field"] += another_value.

Therefore the LjsonQueryResult has been introduced, behaving in the most cases like a list (like before) but providing a new way of item assignment.

This class should be new in v0.3.0.

*Warning*:

This class will behave most propably differently from what one would expect:
Only __i*__ methods are overwritten!
All other methods are mapped to the underlaying list representation.
This leads to the fact that
table[{"some_field": some_value}][some_value] += another_value
# and
table[{"some_field": some_value}][some_value] = another_value + table[{"some_field": some_value}][some_value]

Are not the same!

In particular: The first sample will produce a valid result and modify the table, while the latter leads to undefined behaviour.

*FIXME*:

There should a method LjsonTable.apply(selector, func, args) that bypasses the problem!

class ljson.base.generic.LjsonSelector[source]

Bases: object

getone(column=None)[source]

Return exactly one element. If the selector matches multiple rows the first row is choosen.

If column is None the entired row will be returned.

Returns None if no matching rows were found.

class ljson.base.generic.LjsonTable[source]

Bases: object

additem(row)[source]

Add the row to the table.

close()[source]

This is only used in ljson.base.disk and just for compability here.

static from_file(fin)[source]

Construct the table from the given file.

static open(filename)[source]

Equivalent to Table.from_file(open(filename, "r+"))

save(fout)[source]

Save the table to the given file.

ljson.base.generic.row_matches(row, dct)[source]

ljson.base.mem module

An in-memory ljson implementation.

This module might be used if the entired set is needed or the set is small.

class ljson.base.mem.QueryResult(table, selector, selected, list_)[source]

Bases: ljson.base.generic.LjsonQueryResult

See ljson.base.generic.LjsonQueryResult.

class ljson.base.mem.Selector(header, dct, table)[source]

Bases: ljson.base.generic.LjsonSelector

getone(column=None)[source]
class ljson.base.mem.Table(header, rows)[source]

Bases: ljson.base.generic.LjsonTable

A memory ljson table.

One should not use the constructor to open a file but instead use the static method from_file.

additem(row)[source]
static from_file(fin)[source]
static open(filename)[source]

Equivalent to Table.from_file(open(filename, "r"))

save(fout)[source]

Module contents

The base ljson implementation.

This package provides base functionality, like getter and setter interface.

Creating a ljson table is pretty easy.

>>> from ljson import base
>>> header = base.Header({"id": {"type": "int", "modifiers":["unique"]}, "name": {"type": "str", "modifiers": []}})
>>> table = base.Table(header, [{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}, {"id": 3, "name": "bar"}])
*NOTE*: The default implementation is the in-memory implementation. If one wants to use the on-disk implementation he has to use ljson.base.disk instead. The on disk implementation does not support instantiation with data, you have to use a file containing at least a header instead.

Accessing Fields form the ljson table is pretty easy, too:

>>> table[{"id": 1}]["name"]
['foo']
>>> table[{"id": 3}]["name"] = "baz"
>>> table[{"id": 3}]["name"]
['baz']

Now you might have recognized that the first “index” is a dict and the result is a list. This is because the rows might be not unique:

>>> table[{"id": 3}]["name"] = "bar"
>>> table[{"name": "bar"}]["id"]
[2, 3]