Formatron v0.4.2
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.formats.json.JsonExtractor Class Reference

An extractor that loads json data to an object from a string. More...

Inheritance diagram for formatron.formats.json.JsonExtractor:
formatron.extractor.NonterminalExtractor formatron.extractor.Extractor

Public Member Functions

 __init__ (self, str nonterminal, typing.Optional[str] capture_name, schemas.schema.Schema schema, typing.Callable[[str], schemas.schema.Schema] to_object)
 Create a json extractor from a given schema.
 
typing.Optional[tuple[str, schemas.schema.Schema]] extract (self, str input_str)
 Extract a schema instance from a string.
 
- Public Member Functions inherited from formatron.extractor.NonterminalExtractor
str nonterminal (self)
 Get the nonterminal of the extractor.
 
str kbnf_reference (self)
 
- Public Member Functions inherited from formatron.extractor.Extractor
 __str__ (self)
 
typing.Optional[str] capture_name (self)
 Get the name of the capture, or None if the extractor does not capture.
 

Protected Attributes

 _to_object
 
 _rule_str
 
- Protected Attributes inherited from formatron.extractor.NonterminalExtractor
 _nonterminal
 
- Protected Attributes inherited from formatron.extractor.Extractor
 _capture_name
 

Properties

 kbnf_definition (self)
 

Detailed Description

An extractor that loads json data to an object from a string.

Definition at line 253 of file json.py.

Constructor & Destructor Documentation

◆ __init__()

formatron.formats.json.JsonExtractor.__init__ ( self,
str nonterminal,
typing.Optional[str] capture_name,
schemas.schema.Schema schema,
typing.Callable[[str], schemas.schema.Schema] to_object )

Create a json extractor from a given schema.

   Currently, the following data types are supported:
- bool
- int
- float
- string
- NoneType
- typing.Any
  • Subclasses of collections.abc.Mapping[str,T] and typing.Mapping[str,T] where T is a supported type,
  • Subclasses of collections.abc.Sequence[T] and typing.Sequence[T] where T is a supported type.
  • tuple[T1,T2,...] where T1,T2,... are supported types. The order, type and number of elements will be preserved.
  • typing.Literal[x1,x2,...] where x1, x2, ... are instances of int, string, bool or NoneType, or another typing.Literal[y1,y2,...]
  • typing.Union[T1,T2,...] where T1,T2,... are supported types.
  • schemas.Schema where all its fields' data types are supported. Recursive schema definitions are supported as well.
Parameters
nonterminalThe nonterminal representing the extractor.
capture_nameThe capture name of the extractor, or None if the extractor does not capture.
to_objectA callable to convert the extracted string to a schema instance.

Reimplemented from formatron.extractor.NonterminalExtractor.

Definition at line 280 of file json.py.

Member Function Documentation

◆ extract()

typing.Optional[tuple[str, schemas.schema.Schema]] formatron.formats.json.JsonExtractor.extract ( self,
str input_str )

Extract a schema instance from a string.

Parameters
input_strThe input string to extract from.
Returns
A tuple of the remaining string and the extracted schema instance, or None if extraction failed.

Reimplemented from formatron.extractor.Extractor.

Definition at line 294 of file json.py.

◆ kbnf_definition()

formatron.formats.json.JsonExtractor.kbnf_definition ( self)

Reimplemented from formatron.extractor.Extractor.

Definition at line 346 of file json.py.

Member Data Documentation

◆ _rule_str

formatron.formats.json.JsonExtractor._rule_str
protected

Definition at line 284 of file json.py.

◆ _to_object

formatron.formats.json.JsonExtractor._to_object
protected

Definition at line 283 of file json.py.


The documentation for this class was generated from the following file: