44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
#
|
|
# carkov markov chain library
|
|
# © Copyright 2026 by Aldercone Studio <alderconestudio@gmail.com>
|
|
# This is free software, see the included LICENSE for terms and conditions.
|
|
#
|
|
|
|
"""
|
|
Serialize chain as a python structure (slower load time but more efficient compilation).
|
|
"""
|
|
|
|
from io import TextIOBase
|
|
from . import version
|
|
from .chain import Chain
|
|
|
|
template = """
|
|
# serialized from version {version}
|
|
from carkov.chain import Chain
|
|
from carkov.abstracts import NUMBER, TERMINAL, Abstract
|
|
|
|
DATA={data}
|
|
|
|
def get_chainer():
|
|
chain = Chain({order}, "{analyzer}")
|
|
chain.data = {{}}
|
|
for chain_data in DATA:
|
|
chain.data[chain_data[0]] = {{x[0]: x[1] for x in chain_data[1]}}
|
|
return chain
|
|
"""
|
|
|
|
|
|
def dump_chainer(chain: Chain, outfile: TextIOBase):
|
|
"""
|
|
Serialize a chainer to an open IO stream
|
|
|
|
Arguments:
|
|
chain: A Chain object
|
|
outfile: An open IO stream in text mode that will be writen to
|
|
"""
|
|
outfile.write(template.format(version=version,
|
|
order=chain.order,
|
|
analyzer=chain.analyzer_class,
|
|
data=repr(tuple([(item[0], tuple(item[1].items())) for item in chain.items()])).replace(")),", ")),\n")
|
|
))
|