text
stringlengths
0
828
raise NoConverterFoundForObjectType.create(conversion_finder,
attr_value,
desired_attr_type)
else:
# we can safely use the value: it is already of the correct type
return attr_value"
511,"def register_converter(self, converter: Converter[S, T]):
""""""
Utility method to register any converter. Converters that support any type will be stored in the ""generic""
lists, and the others will be stored in front of the types they support
:return:
""""""
check_var(converter, var_types=Converter, var_name='converter')
# (0) sanity check : check that parser handles jokers properly
res = converter.is_able_to_convert_detailed(from_type=JOKER, to_type=JOKER, strict=True)
if not (res[0] is True and res[1] is None and res[2] is None):
raise ValueError('Converter ' + str(converter) + ' can not be registered since it does not handle the JOKER'
' cases correctly')
# compute all possible chains and save them
generic_chains, generic_nonstrict_chains, specific_chains, specific_nonstrict_chains \
= self._create_all_new_chains(converter)
self._generic_nonstrict_conversion_chains += generic_nonstrict_chains
self._generic_conversion_chains += generic_chains
self._specific_non_strict_conversion_chains += specific_nonstrict_chains
self._specific_conversion_chains += specific_chains
# sort all lists by length
self._generic_nonstrict_conversion_chains = sorted(self._generic_nonstrict_conversion_chains, key=len,
reverse=True)
self._generic_conversion_chains = sorted(self._generic_conversion_chains, key=len, reverse=True)
self._specific_non_strict_conversion_chains = sorted(self._specific_non_strict_conversion_chains, key=len,
reverse=True)
self._specific_conversion_chains = sorted(self._specific_conversion_chains, key=len, reverse=True)"
512,"def _create_all_new_chains(self, converter) -> Tuple[List[Converter], List[Converter],
List[Converter], List[Converter]]:
""""""
Creates all specific and generic chains that may be built by adding this converter to the existing chains.
:param converter:
:return: generic_chains, generic_nonstrict_chains, specific_chains, specific_nonstrict_chains
""""""
specific_chains, specific_nonstrict_chains, generic_chains, generic_nonstrict_chains = [], [], [], []
if converter.is_generic():
# the smaller chain : the singleton :)
generic_chains.append(ConversionChain(initial_converters=[converter], strict_chaining=True))
else:
specific_chains.append(ConversionChain(initial_converters=[converter], strict_chaining=True))
# 1) create new specific chains by adding this converter at the beginning or end of all *non-generic* ones
# -- non-strict
new_c_at_end_ns = []
new_c_at_beginning_ns = []
if not self.strict:
# then there are non-strict chains already. Try to connect to them
for existing_specific_nonstrict in self._specific_non_strict_conversion_chains:
if converter.can_be_appended_to(existing_specific_nonstrict, strict=False):
if ConversionChain.are_worth_chaining(existing_specific_nonstrict, converter):
new_c_at_end_ns.append(ConversionChain.chain(existing_specific_nonstrict, converter,
strict=False))
if existing_specific_nonstrict.can_be_appended_to(converter, strict=False):
if ConversionChain.are_worth_chaining(converter, existing_specific_nonstrict):
new_c_at_beginning_ns.append(ConversionChain.chain(converter, existing_specific_nonstrict,
strict=False))
# -- strict
new_c_at_end = []
new_c_at_beginning = []
for existing_specific in self._specific_conversion_chains:
# first try *strict* mode
if converter.can_be_appended_to(existing_specific, strict=True):
if ConversionChain.are_worth_chaining(existing_specific, converter):
new_c_at_end.append(ConversionChain.chain(existing_specific, converter, strict=True))
elif (not self.strict) and converter.can_be_appended_to(existing_specific, strict=False):
if ConversionChain.are_worth_chaining(existing_specific, converter):
new_c_at_end_ns.append(ConversionChain.chain(existing_specific, converter, strict=False))
if existing_specific.can_be_appended_to(converter, strict=True):
if ConversionChain.are_worth_chaining(converter, existing_specific):
# TODO this is where when chaining a generic to a specific, we would actually have to restrict it
# note: but maybe we dont care since now this is checked and prevented in the convert() method
new_c_at_beginning.append(ConversionChain.chain(converter, existing_specific, strict=True))
elif (not self.strict) and existing_specific.can_be_appended_to(converter, strict=False):
if ConversionChain.are_worth_chaining(converter, existing_specific):
# TODO this is where when chaining a generic to a specific, we would actually have to restrict it
# note: but maybe we dont care since now this is checked and prevented in the convert() method
new_c_at_beginning_ns.append(ConversionChain.chain(converter, existing_specific, strict=False))
# append to the right list depending on the nature of this converter
if converter.is_generic():
generic_chains += new_c_at_end
generic_nonstrict_chains += new_c_at_end_ns
else:
specific_chains += new_c_at_end
specific_nonstrict_chains += new_c_at_end_ns
# common for both types