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
- abstract add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generator
of (str
,islice
ofstr
,str
)) – Links to add.dataset_prefix (
str
, optional) – Dataset key prefix.
- abstract do_save(fp=None)[source]¶
Save to file.
- Parameters
fp (
file
orstr
, 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 (
str
oriterable
ofstr
) – Initial state.size (
int
) – State size.dataset (
str
) – Dataset key.backward (
bool
, optional) – Link direction.
- Returns
Node value generator.
- Return type
generator
ofstr
- 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
None
orlist
of (int
,str
,object
)
- abstract get_state(state, size)[source]¶
Convert strings to state.
- Parameters
state (
iterable
ofstr
) – 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
ofstr
- abstract classmethod load(fp)[source]¶
Load from file.
- Parameters
fp (
file
orstr
, 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
(
str
orNone
,object
orNone
)
- 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
orstr
, 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.Storage
JSON storage.
- nodes¶
- Type
dict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
])
- backward¶
- Type
None
ordict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
])
- __init__(nodes=None, backward=None, settings=None)[source]¶
JSON storage constructor.
- Parameters
nodes (
dict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
]), optional) –backward (
bool
ordict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
]), optional) –
- static add_link(dataset, source, target, count=1)[source]¶
Add a link.
- Parameters
dataset (
dict
of ([int
,str
] or [list
ofint
,list
ofstr
])) – Dataset.source (
iterable
ofstr
) – Link source.target (
str
) – Link target.count (
int
, optional) – Link count (default: 1).
- add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generator
of (str
,islice
ofstr
,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 (
None
ordict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
])) – Data.key (
str
) – Dataset key.create (
bool
, optional) – Create a dataset if it does not exist.
- Return type
None
ordict
of ([int
,str
] or [list
ofint
,list
ofstr
])
- static do_replace_state_separator(data, old, new)[source]¶
Replace state separator.
- Parameters
data (
dict
ofdict
of ([int
,str
] or [list
ofint
,list
ofstr
])) – Data.old (
str
) – Old separator.new (
str
) – New separator.
- do_save(fp=None)[source]¶
Save to file.
- Parameters
fp (
file
orstr
, 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
None
orlist
of (int
,str
,object
)- Raises
ValueError – If backward ==
True
and self.backward isNone
.
- get_state(state, size)[source]¶
Convert strings to state.
- Parameters
state (
iterable
ofstr
) – 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
ofstr
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
orsqlite3.Connection
, optional) – Database path or connection (default: ‘:memory:’).settings (
dict
, optional) –
- add_links(links, dataset_prefix='')[source]¶
Add links.
- Parameters
links (
generator
of (str
,islice
ofstr
,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
None
orlist
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
ofstr
) – 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
ofstr
- classmethod load(fp)[source]¶
Load from file.
- Parameters
fp (
file
orstr
, optional) –path. (Input file or) –
- Returns
Loaded storage.
- Return type
markovchain.storage.Storage