API¶
JDOG¶
-
class
jdog.jdog.
Jdog
(lang='en-US', strict=False)¶ Proxy class to parser. Accepts language to use, parsing scheme and generating new data
- Variables
lang (str) – Language code to use. See
faker.Faker
for supported languages.strict (boolean) – If parser should raise NoMatchingPlaceholder error when parsing placeholders.
-
add_matcher
(key, pattern, f_placeholder)¶ Add or redefine placeholder identified by KEY
- Parameters
key (str) – Unique placeholder identification.
pattern (str) – Regex pattern which detects given placeholder.
f_placeholder (func) – Function which should return
Placeholder
object. Function takes matched token and its arguments - if present.
-
generate
()¶ Generate new data instance
- Returns
Generated JSON
- Return type
str
-
parse_scheme
(scheme)¶ Parse scheme for generator
- Parameters
scheme (str) – Scheme to use and parse
- Raises
jdog.parser.NoMatchingPlaceholder
,json.JsonDecodeError
-
placeholder_keys
()¶ Returns all defined placeholder keys
- Returns
List of placeholder keys
- Return type
list
SchemeParser¶
-
exception
jdog.parser.
NoMatchingPlaceholder
(token)¶ Raised when no matching placeholder was found.
-
class
jdog.parser.
SchemeParser
(lang='en-US', strict=False)¶ Parsing provided scheme and construct node structure for later data generation. Defines predefined placeholders.
- Variables
faker (faker) – Faker instance with provided language
strict (boolean) – If parser should raise
NoMatchingPlaceholder
-
add_matcher
(key, f_matcher, f_placeholder)¶ Add new matcher for returning new placeholder
- Parameters
key – Unique matcher key. If provided existing one, old behavior is replaced.
f_matcher – Function which takes one str argument. Should return re.Match object or None if no match found.
f_placeholder – Function which takes accepted token and its parsed arguments - if present. Should return one of
Placeholder
object.
-
parse
(scheme)¶ Parse given scheme and return node structure representation of the scheme.
- Parameters
scheme (str) – Scheme to parse
- Returns
Node structure representing current scheme
-
placeholder_keys
()¶ Defined placeholder keys
- Returns
Defined placeholder keys
- Return type
list
Nodes¶
-
class
jdog.node.
ArrayNode
(children)¶ Represents JSON-Array
- Variables
properties (list) – Values to include within object.
-
class
jdog.node.
FuncNode
(f)¶ Node accepting function which will be executed later
- Variables
f (func) – Function to execute. Has no arguments.
-
exec
()¶ Executes
f
-
class
jdog.node.
ObjectNode
(properties)¶ Represents JSON-Object
- Variables
properties (list) – Properties to include within object.
-
class
jdog.node.
PlaceholderNode
(placeholder)¶ Represents node with object
- Variables
placeholder (object) –
Placeholder
object to use.
-
exec
()¶ Executes placeholder
- Returns
Value returned by placeholder
-
class
jdog.node.
PropertyNode
(name, child)¶ Represent JSON property with name and value
- Variables
name (Node) – Property name
child (Node) – Property value
-
exec
()¶ Executes name node and child if present. :return: Property representation
-
class
jdog.node.
RangeNode
(name, l, child, h=None)¶ Node which repeatedly creates sub-nodes. Combination of property with array value.
- Variables
name (str) – Represents property name which will be generated.
l (int) – How many times generation runs.
child (Node) – Object to add to the generated array.
h (int) – Optional. If specified, generation runs randomly between l (inlcusive) to h (exclusive).
-
exec
()¶ - Returns
Range representation: JSON property named by
value
with array value.- Return type
str
-
class
jdog.node.
ScalarNode
(value)¶ Represents scalar value - that is any number or any arbitrary string
- Variables
value (str|number) – Scalar value
-
exec
()¶ If value is string - return as JSON string value otherwise as is :return: Value enclosed as a string or as is
-
class
jdog.node.
_GroupNode
(begin_token, end_token, nodes)¶ Base class for nodes which groups other nodes together.
- Variables
begin_token (str) – char to print at the start of group
end_token (str) – char to print at the end of group
nodes (list) – Nodes to recursively included within group
-
exec
()¶ Join nodes together between
begin_token
andend_token
Placeholders¶
-
class
jdog.placeholder.placeholder.
FuncPlaceholder
(full_name, args, func)¶ Represents placeholder which takes function to execute later. Returned value is fully determined by func itself.
- Variables
func (func) – Function to execute. Takes provided arguments from placeholder.
args (list) – Parsed arguments as list
-
exec
()¶ Executes func and return its returned value
-
class
jdog.placeholder.placeholder.
FuncStrPlaceholder
(full_name, args, func)¶ Represents placeholder which takes function to execute later. The returned value is enclosed with double quotes to denote JSON string value.
- Variables
func (func) – Function to execute. Takes provided arguments from placeholder.
-
exec
()¶ Executes func and return its value enclosed as string