markovchain.storage package

Submodules

markovchain.storage.base module

class markovchain.storage.base.Storage(settings=None)[source]

Bases: object

Storage base class.

settings
Type

dict

state_separator
Type

str

__init__(settings=None)[source]

Storage base constructor.

Parameters

settings (dict, optional) –

Add links.

Parameters
  • links (generator of (str, islice of str, str)) – Links to add.

  • dataset_prefix (str, optional) – Dataset key prefix.

abstract close()[source]

Close.

abstract do_save(fp=None)[source]

Save to file.

Parameters

fp (file or str, optional) – Output file.

Follow a link.

Parameters
  • link ((int, str, object)) – Link to follow.

  • state (object) – State.

  • backward (bool, optional) – Link direction.

Returns

New state.

Return type

object

generate(state, size, dataset, backward=False)[source]

Generate a sequence.

Parameters
  • state (str or iterable of str) – Initial state.

  • size (int) – State size.

  • dataset (str) – Dataset key.

  • backward (bool, optional) – Link direction.

Returns

Node value generator.

Return type

generator of str

abstract get_dataset(key, create=False)[source]

Get data set by key.

Parameters
  • key (str) – Dataset key.

  • create (bool, optional) – Create dataset if it does not exist.

Returns

Dataset.

Return type

object

Raises

KeyError – If dataset does not exist and create == False.

Get links.

Parameters
  • dataset (object) – Dataset from self.get_dataset().

  • state (object) – State from self.get_state().

  • backward (bool, optional) – Link direction.

Returns

Links (count, value, data).

Return type

None or list of (int, str, object)

abstract get_state(state, size)[source]

Convert strings to state.

Parameters
  • state (iterable of str) – Node values.

  • size (int) – State size.

Returns

State.

Return type

object

abstract get_states(dataset, string)[source]

Get all states containing a substring.

Parameters
  • dataset (str) – Dataset key.

  • string (str) – String to search.

Returns

States.

Return type

list of str

join_state(state)[source]

Join states.

Parameters

state (iterable of str) –

Return type

str

abstract classmethod load(fp)[source]

Load from file.

Parameters

fp (file or str, optional) – Input file or path.

Returns

Loaded storage.

Return type

markovchain.storage.Storage

Get a random link.

Parameters
  • dataset (object) – Dataset from self.get_dataset().

  • state (object) – Link source.

  • backward (bool, optional) – Link direction.

Raises

ValueError – If link count is invalid.

Returns

Link value and next state.

Return type

(str or None, object or None)

abstract replace_state_separator(old_separator, new_separator)[source]

Replace state separator.

Parameters
  • old_separator (str) – Old state separator.

  • new_separator (str) – New state separator.

save(fp=None)[source]

Update settings JSON data and save to file.

Parameters

fp (file or str, optional) – Output file.

split_state(state)[source]

Split state string.

Parameters

state (str) –

Return type

list of str

property state_separator

State separator.

Type

str

markovchain.storage.json module

class markovchain.storage.json.JsonStorage(nodes=None, backward=None, settings=None)[source]

Bases: markovchain.storage.base.Storage

JSON storage.

nodes
Type

dict of dict of ([int, str] or [list of int, list of str])

backward
Type

None or dict of dict of ([int, str] or [list of int, list of str])

__init__(nodes=None, backward=None, settings=None)[source]

JSON storage constructor.

Parameters
  • nodes (dict of dict of ([int, str] or [list of int, list of str]), optional) –

  • backward (bool or dict of dict of ([int, str] or [list of int, list of str]), optional) –

Add a link.

Parameters
  • dataset (dict of ([int, str] or [list of int, list of str])) – Dataset.

  • source (iterable of str) – Link source.

  • target (str) – Link target.

  • count (int, optional) – Link count (default: 1).

Add links.

Parameters
  • links (generator of (str, islice of str, str)) – Links to add.

  • dataset_prefix (str, optional) – Dataset key prefix.

close()[source]

Close.

static do_get_dataset(data, key, create=False)[source]

Get a dataset.

Parameters
  • data (None or dict of dict of ([int, str] or [list of int, list of str])) – Data.

  • key (str) – Dataset key.

  • create (bool, optional) – Create a dataset if it does not exist.

Return type

None or dict of ([int, str] or [list of int, list of str])

static do_replace_state_separator(data, old, new)[source]

Replace state separator.

Parameters
  • data (dict of dict of ([int, str] or [list of int, list of str])) – Data.

  • old (str) – Old separator.

  • new (str) – New separator.

do_save(fp=None)[source]

Save to file.

Parameters
  • fp (file or str, optional) –

  • (default (Output file) –

Follow a link.

Parameters
  • link ((int, str, object)) – Link to follow.

  • state (object) – State.

  • backward (bool, optional) – Link direction.

Returns

New state.

Return type

object

get_dataset(key, create=False)[source]

Get data set by key.

Parameters
  • key (str) – Dataset key.

  • create (bool, optional) – Create dataset if it does not exist.

Returns

Dataset.

Return type

object

Raises

KeyError – If dataset does not exist and create == False.

Get links.

Parameters
  • dataset (object) – Dataset from self.get_dataset().

  • state (object) – State from self.get_state().

  • backward (bool, optional) – Link direction.

Returns

Links (count, value, data).

Return type

None or list of (int, str, object)

Raises

ValueError – If backward == True and self.backward is None.

get_state(state, size)[source]

Convert strings to state.

Parameters
  • state (iterable of str) – Node values.

  • size (int) – State size.

Returns

State.

Return type

object

get_states(dataset, string)[source]

Get all states containing a substring.

Parameters
  • dataset (str) – Dataset key.

  • string (str) – String to search.

Returns

States.

Return type

list of str

classmethod load(fp)[source]

Load from file.

Parameters
  • fp (file or str, optional) –

  • path. (Input file or) –

Returns

Loaded storage.

Return type

markovchain.storage.Storage

replace_state_separator(old_separator, new_separator)[source]

Replace state separator.

Parameters
  • old_separator (str) – Old state separator.

  • new_separator (str) – New state separator.

markovchain.storage.sqlite module

class markovchain.storage.sqlite.SqliteStorage(db=':memory:', settings=None)[source]

Bases: markovchain.storage.base.Storage

SQLite storage.

db

Database connection.

Type

sqlite3.Connection

cursor

Database cursor.

__init__(db=':memory:', settings=None)[source]

SQLite storage constructor.

Parameters
  • db (str or sqlite3.Connection, optional) – Database path or connection (default: ‘:memory:’).

  • settings (dict, optional) –

Add links.

Parameters
  • links (generator of (str, islice of str, str)) – Links to add.

  • dataset_prefix (str, optional) – Dataset key prefix.

close()[source]

Close.

create_tables()[source]

Create tables if they don’t exist.

do_save(fp=None)[source]

Save.

Parameters

fp (None, optional) –

Follow a link.

Parameters
  • link ((int, str, object)) – Link to follow.

  • state (object) – State.

  • backward (bool, optional) – Link direction.

Returns

New state.

Return type

object

get_dataset(key, create=False)[source]

Get data set by key.

Parameters
  • key (str) – Dataset key.

  • create (bool, optional) – Create dataset if it does not exist.

Returns

Dataset.

Return type

object

Raises

KeyError – If dataset does not exist and create == False.

Get links.

Parameters
  • dataset (object) – Dataset from self.get_dataset().

  • state (object) – State from self.get_state().

  • backward (bool, optional) – Link direction.

Returns

Links (count, value, data).

Return type

None or list of (int, str, object)

get_node(value)[source]

Get node ID by value.

If a node with the specified value does not exist, create it and return its ID.

Parameters

value (str) – Node value.

Returns

Node ID.

Return type

int

get_state(state, size)[source]

Convert strings to state.

Parameters
  • state (iterable of str) – Node values.

  • size (int) – State size.

Returns

State.

Return type

object

get_states(dataset, string)[source]

Get all states containing a substring.

Parameters
  • dataset (str) – Dataset key.

  • string (str) – String to search.

Returns

States.

Return type

list of str

get_tables()[source]

Get all table names.

Return type

set of str

classmethod load(fp)[source]

Load from file.

Parameters
  • fp (file or str, optional) –

  • path. (Input file or) –

Returns

Loaded storage.

Return type

markovchain.storage.Storage

replace_state_separator(old_separator, new_separator)[source]

Replace state separator.

Parameters
  • old_separator (str) – Old state separator.

  • new_separator (str) – New state separator.

update_main_table()[source]

Write generator settings to database.

Module contents