markovchain.storage package¶
Submodules¶
markovchain.storage.base module¶
- class markovchain.storage.base.Storage(settings=None)[source]¶
Bases:
objectStorage base class.
- settings¶
- Type
dict
- state_separator¶
- Type
str
- abstract add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generatorof (str,isliceofstr,str)) – Links to add.dataset_prefix (
str, optional) – Dataset key prefix.
- abstract do_save(fp=None)[source]¶
Save to file.
- Parameters
fp (
fileorstr, optional) – Output file.
- abstract follow_link(link, state, backward=False)[source]¶
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 (
stroriterableofstr) – Initial state.size (
int) – State size.dataset (
str) – Dataset key.backward (
bool, optional) – Link direction.
- Returns
Node value generator.
- Return type
generatorofstr
- 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.
- abstract get_links(dataset, state, backward=False)[source]¶
Get links.
- Parameters
dataset (
object) – Dataset fromself.get_dataset().state (
object) – State fromself.get_state().backward (
bool, optional) – Link direction.
- Returns
Links (count, value, data).
- Return type
Noneorlistof (int,str,object)
- abstract get_state(state, size)[source]¶
Convert strings to state.
- Parameters
state (
iterableofstr) – 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
listofstr
- abstract classmethod load(fp)[source]¶
Load from file.
- Parameters
fp (
fileorstr, optional) – Input file or path.- Returns
Loaded storage.
- Return type
markovchain.storage.Storage
- random_link(dataset, state, backward=False)[source]¶
Get a random link.
- Parameters
dataset (
object) – Dataset fromself.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
(
strorNone,objectorNone)
- 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 (
fileorstr, optional) – Output file.
- 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.StorageJSON storage.
- nodes¶
- Type
dictofdictof ([int,str] or [listofint,listofstr])
- backward¶
- Type
Noneordictofdictof ([int,str] or [listofint,listofstr])
- __init__(nodes=None, backward=None, settings=None)[source]¶
JSON storage constructor.
- Parameters
nodes (
dictofdictof ([int,str] or [listofint,listofstr]), optional) –backward (
boolordictofdictof ([int,str] or [listofint,listofstr]), optional) –
- static add_link(dataset, source, target, count=1)[source]¶
Add a link.
- Parameters
dataset (
dictof ([int,str] or [listofint,listofstr])) – Dataset.source (
iterableofstr) – Link source.target (
str) – Link target.count (
int, optional) – Link count (default: 1).
- add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generatorof (str,isliceofstr,str)) – Links to add.dataset_prefix (
str, optional) – Dataset key prefix.
- static do_get_dataset(data, key, create=False)[source]¶
Get a dataset.
- Parameters
data (
Noneordictofdictof ([int,str] or [listofint,listofstr])) – Data.key (
str) – Dataset key.create (
bool, optional) – Create a dataset if it does not exist.
- Return type
Noneordictof ([int,str] or [listofint,listofstr])
- static do_replace_state_separator(data, old, new)[source]¶
Replace state separator.
- Parameters
data (
dictofdictof ([int,str] or [listofint,listofstr])) – Data.old (
str) – Old separator.new (
str) – New separator.
- do_save(fp=None)[source]¶
Save to file.
- Parameters
fp (
fileorstr, optional) –(default (Output file) –
- follow_link(link, state, backward=False)[source]¶
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(dataset, state, backward=False)[source]¶
Get links.
- Parameters
dataset (
object) – Dataset fromself.get_dataset().state (
object) – State fromself.get_state().backward (
bool, optional) – Link direction.
- Returns
Links (count, value, data).
- Return type
Noneorlistof (int,str,object)- Raises
ValueError – If backward ==
Trueand self.backward isNone.
- get_state(state, size)[source]¶
Convert strings to state.
- Parameters
state (
iterableofstr) – 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
listofstr
markovchain.storage.sqlite module¶
- class markovchain.storage.sqlite.SqliteStorage(db=':memory:', settings=None)[source]¶
Bases:
markovchain.storage.base.StorageSQLite storage.
- db¶
Database connection.
- Type
sqlite3.Connection
- cursor¶
Database cursor.
- __init__(db=':memory:', settings=None)[source]¶
SQLite storage constructor.
- Parameters
db (
strorsqlite3.Connection, optional) – Database path or connection (default: ‘:memory:’).settings (
dict, optional) –
- add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generatorof (str,isliceofstr,str)) – Links to add.dataset_prefix (
str, optional) – Dataset key prefix.
- follow_link(link, state, backward=False)[source]¶
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(dataset, state, backward=False)[source]¶
Get links.
- Parameters
dataset (
object) – Dataset fromself.get_dataset().state (
object) – State fromself.get_state().backward (
bool, optional) – Link direction.
- Returns
Links (count, value, data).
- Return type
Noneorlistof (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 (
iterableofstr) – 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
listofstr
- classmethod load(fp)[source]¶
Load from file.
- Parameters
fp (
fileorstr, optional) –path. (Input file or) –
- Returns
Loaded storage.
- Return type
markovchain.storage.Storage