Utility Methods#

A collection of convenient helper-functions.

class rethon.util.RethonJSONEncoder#

Methods

default(o)

An implemention of JSONEncoder.default().

encode(o)

Return a JSON string representation of a Python data structure.

iterencode(o[, _one_shot])

Encode the given object and yield each string representation as available.

__init__(serialize_implementation=False, **kwargs)#

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(o)#

An implemention of JSONEncoder.default().

This implementation handles the serialization of Position-, DialecticalStructure-, REState- and ReflectiveEquilibrium instances.

rethon.util.local_re_model_params_varied_alphas(alpha_resolution, with_extremes=False)#

Model parameters for Local RE with varied alphas.

Convenience method that uses varied_alphas() to generate a list of model parameters for the locally searching standard model (see LocalReflectiveEquilibrium) as dict. Alphas are varied; all other parameters a set to default (see basics.ReflectiveEquilibrium for details about model parameters).

Return type:

Iterator[Dict]

rethon.util.re_from_text_file(dir, file_name, re_implementations=[('rethon', 'BitarrayReflectiveEquilibrium')], ds_module_name='theodias', ds_class_name='BitarrayDialecticalStructure', position_module_name='theodias', position_class_name='BitarrayPosition')#

Creates a list of RE classes from a text-file.

The text file consists of a list (i.e. […]) of dictionaries (i.e. {…}) and commented lines used for the description of the example (starting with #). The dictionaries have to contain comma separated entries for the number of unnegated sentences (key: ‘n’, e.g. ‘n’:7) and the arguments (key: ‘arguments’) as a list of lists of numbers (e.g.’arguments’:[[1,-3], [2,3,5]]).

Optionally, the dictionary may contain entries ‘account_penalties’, ‘faithfulness_penalties’ and ‘weights’ if non-standard values should be considered. Finally, an entry of ‘initial_commitments’ may be provided, which is returned as a list of positions in the desired implementation.

A simple schematic example is:

[
# the standard examples (cases A,B,C and D) contain only arguments with one premise.
{
    'n': 7,
    'arguments': [
        [1,3],
        [1,4],
        [1,5],
        [1,-6],
        [2,-4],
        [2,5],
        [2,6],
        [2,7]
    ],
    'initial_commitments': [{3,4,5}, {2,3,4,5}, {3,4,5,6,7}, {3,4,5,-6,7}],
    'weights' : {'account': 0.35, 'systematicity': 0.55, 'faithfulness': 0.1},
    'account_penalties' : [0, 0.3, 1, 1],
    'faithfulness_penalties' : [0, 0, 1, 1]
},
{ # some other dialectical structure ...
}
]
Parameters:
  • dir (str) – Directory of the text-file.

  • file_name (str) – Name of the text-file to be used.

  • re_implementations (List[Tuple[str, str]]) – String-representation of the implementation to be used (defined by module- and class name).

  • ds_module_name (str) – Modulename of the implementation of DialecticalStructure to be used.

  • ds_class_name (str) – Classname of the implementation of DialecticalStructure to be used.

  • position_module_name (str) – Module name of the implementation of Position to be used.

  • position_class_name (str) – Class name of the implementation of Position to be used.

Return type:

List[Tuple[ReflectiveEquilibrium, List[Position]]]

Returns:

List of the form [(ReflectiveEquilibrium,[Position,Position,...]),(ReflectiveEquilibrium,[Position,Position,...]),...] with the inner lists of Position possibly being None.

rethon.util.rethon_decoder(json_obj, use_json_specified_type=False, position_module='theodias', position_class='BitarrayPosition', dialectical_structure_module='theodias', dialectical_structure_class='BitarrayDialecticalStructure', reflective_equilibrium_module='rethon', reflective_equilibrium_class='BitarrayReflectiveEquilibrium')#

Object hook for json.loads() and json.load().

Parameters:

use_json_specified_type – If True the methods used the implementation details (module name and class name) that are specified in the json string, if there are any. Otherwise, the methode uses implementation details as given by the other parameters.

rethon.util.rethon_dump(re_object, fp, cls=<class 'rethon.util.RethonJSONEncoder'>, serialize_implementation=False, **kwargs)#

Saving an object as JSON-String in a file.

This is a convenient method that calls json.dump() with RethonJSONEncoder as its default encoder, which will handle the JSON serialization of Position-, DialecticalStructure-, REState- and ReflectiveEquilibrium instances.

**kwargs will be given to json.dumps()

Parameters:

serialize_implementation – If True implementation details (module name and class name) will be serialized.

Returns:

The object as a JSON string.

rethon.util.rethon_dumps(re_object, cls=<class 'rethon.util.RethonJSONEncoder'>, serialize_implementation=False, **kwargs)#

Getting an object as JSON-String.

This is a convenient method that calls json.dumps() with RethonJSONEncoder as its default encoder, which will handle the JSON serialization of Position-, DialecticalStructure-, REState- and ReflectiveEquilibrium instances.

**kwargs will be given to json.dumps()

Parameters:

serialize_implementation – If True implementation details (module name and class name) will be serialized.

Returns:

The object as a JSON string.

rethon.util.rethon_load(fp, use_json_specified_type=False, position_module='theodias', position_class='BitarrayPosition', dialectical_structure_module='theodias', dialectical_structure_class='BitarrayDialecticalStructure', reflective_equilibrium_module='rethon', reflective_equilibrium_class='BitarrayReflectiveEquilibrium')#

Loading an object from a JSON file.

This is a convenient method that calls json.load() and uses rethon_decoder() as object hook to handle the instantiation of Position-, DialecticalStructure-, REState- and ReflectiveEquilibrium objects. Desired implementation details can be given by parameter values (see rethon_decoder()).

rethon.util.rethon_loads(json_obj, use_json_specified_type=False, position_module='theodias', position_class='BitarrayPosition', dialectical_structure_module='theodias', dialectical_structure_class='BitarrayDialecticalStructure', reflective_equilibrium_module='rethon', reflective_equilibrium_class='BitarrayReflectiveEquilibrium')#

Loading an object from a JSON string.

This is a convenient method that calls json.loads() and uses rethon_decoder() as object hook to handle the instantiation of Position-, DialecticalStructure-, REState- and ReflectiveEquilibrium objects. Desired implementation details can be given by parameter values (see rethon_decoder()).

rethon.util.standard_model_params_varied_alphas(alpha_resolution, with_extremes=False)#

Standard model parameters with varied alphas.

Convenience method that uses varied_alphas() to generate a list of model parameters for the standard model (see StandardReflectiveEquilibrium) as dict. Alphas are varied; all other parameters a set to default (see basics.ReflectiveEquilibrium for details about model parameters).

Return type:

Iterator[Dict]

rethon.util.varied_alphas(alpha_resolution, with_extremes=False)#

A list of alpha values that can be used as model parameters.

Return type:

Iterator[List[float]]