hi, I only know a little python to play with klipper but until now I found what I needed
now I need a dictionary to store a list of keys and values and like to update the items by a macro
here I have my dictionary
[gcode_macro _test_variables]
variable_feature_hops: {“feature1”: 1.0, “feature2”: 1.5}
gcode:
M115 ; must provide something
here I try to update it
[gcode_macro _set_feature_retraction_zhop]
gcode:
{% set feature_hops = printer[“gcode_macro _test_variables”].feature_hops %}
{% set feature = params.FEATURE|default(“”)|string %}
{% set distance = params.DISTANCE|default(0.0)|float %}
{% if feature != “” and distance >= 0 %}
#each of this lines results in an error
#{% set feature_hops |= {feature: distance} %}
#{% set feature_hops = {feature_hops, feature: distance} %}
#feature_hops.update(feature = distance)
#feature_hops[feature] = distance;
#while this works
#{% set feature_hops = {feature: distance} %}
#saving the dict back to the macro-variable is not working also
#SET_GCODE_VARIABLE MACRO=_test_variables VARIABLE=feature_hops VALUE={feature_hops}
{action_respond_info('feature_test_zhops: {}'.format(feature_hops))}
{% endif %}
is it possible to read and update dictionary-items or do I have to find another solution?
thanks, result sadly is
Error loading template ‘gcode_macro _set_feature_retraction_zhop:gcode’: jinja2.exceptions.TemplateSyntaxError: expected token ‘end of print statement’, got ‘:’
same result without ‘’
Yes, __getitem__ works well, and has the benefit of returning None instead of an error if the specified key isn’t in the dictionary for some reason. I don’t think it’s any slower than trying to access the value directly.