@@ -884,6 +884,67 @@ def append(self, obj, strict=True):
884884
885885 self ._values .append (dtypes .get (new_value [0 ], self .dtype ))
886886
887+ def insert (self , index , obj , strict = True ):
888+ """
889+ Insert a single value to the list of stored values. Method will raise
890+ a ValueError if the passed value cannot be converted to the current dtype.
891+
892+ :param obj: the additional value.
893+ :param index: position of the new value
894+ :param strict: a Bool that controls whether dtypes must match. Default is True.
895+ """
896+
897+ # Ignore empty values before nasty stuff happens, but make sure
898+ # 0 and False get through.
899+ if obj in [None , "" , [], {}]:
900+ return
901+
902+ if not self .values :
903+ self .values = obj
904+ return
905+
906+ new_value = self ._convert_value_input (obj )
907+ if len (new_value ) > 1 :
908+ raise ValueError ("odml.property.insert: Use extend to add a list of values!" )
909+
910+ new_value = self ._convert_value_input (obj )
911+ if len (new_value ) > 1 :
912+ raise ValueError ("odml.property.insert: Use extend to add a list of values!" )
913+
914+ if self ._dtype .endswith ("-tuple" ):
915+ t_count = int (self ._dtype .split ("-" )[0 ])
916+ new_value = odml_tuple_import (t_count , new_value )
917+
918+ if len (new_value ) > 0 and strict and \
919+ dtypes .infer_dtype (new_value [0 ]) != self .dtype :
920+
921+ type_check = dtypes .infer_dtype (new_value [0 ])
922+ if not (type_check == "string" and self .dtype in dtypes .special_dtypes ) \
923+ and not self .dtype .endswith ("-tuple" ):
924+ msg = "odml.Property.insert: passed value data type found "
925+ msg += "(\" %s\" ) does not match expected dtype \" %s\" !" % (type_check ,
926+ self ._dtype )
927+ raise ValueError (msg )
928+
929+ if not self ._validate_values (new_value ):
930+ raise ValueError ("odml.Property.insert: passed value(s) cannot be converted "
931+ "to data type \' %s\' !" % self ._dtype )
932+
933+
934+ if index > len (self ._values ):
935+ warnings .warn ("odml.Property.insert: Index %i larger than length of property.values. "
936+ "Added value at end of list." % index , stacklevel = 2 )
937+ elif index < - len (self ._values ) - 1 :
938+ warnings .warn ("odml.Property.insert: Negative index %i smaller than length "
939+ "of property.values. "
940+ "Added value at beginning of list." % index , stacklevel = 2 )
941+
942+ if - len (self ._values ) <= index < 0 :
943+ index = len (self ._values ) + index + 1
944+ print ("INDEX!" , index )
945+
946+ self ._values .insert (index , dtypes .get (new_value [0 ], self .dtype ))
947+
887948 def pprint (self , indent = 2 , max_length = 80 , current_depth = - 1 ):
888949 """
889950 Pretty print method to visualize Properties and Section-Property trees.
0 commit comments