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.
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 inw+
mode!
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.
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 underlayinglist
representation.This leads to the fact thattable[{"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!
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.
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.
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 useljson.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]