structa.analyzer
The structa.analyzer
module contains the Analyzer
class which
is the primary entry point for using structa’s as an API. It can be constructed
without any arguments, and the analyze()
method can be
immediately used to determine the structure of some data. The
merge()
method can be used to further refine the returned
structure, and measure()
can be used before-hand if you wish to
use the progress callback to track the progress of long analysis runs.
A typical example of basic usage would be:
from structa.analyzer import Analyzer
data = {
str(i): i
for i in range(1000)
}
an = Analyzer()
structure = an.analyze(data)
print(structure)
The structure returned by analyze()
(and by
merge()
) will be an instance of one of the classes in the
structa.types
module, all of which have sensible str
and
repr()
output.
A more complete example, using Source
to figure out
the source format and encoding:
from structa.analyzer import Analyzer
from structa.source import Source
from urllib.request import urlopen
with urlopen('https://usn.ubuntu.com/usn-db/database-all.json') as f:
src = Source(data)
an = Analyzer()
an.measure(src.data)
structure = an.analyze(src.data)
structure = an.merge(structure)
print(structure)