anschelburk / nutrient_guide

Apache License 2.0
0 stars 0 forks source link

use setdefault #45

Open bbelderbos opened 8 months ago

bbelderbos commented 8 months ago
          nice, apart from pythonic this is now copied by value (not reference), see also below

_Originally posted by @bbelderbos in https://github.com/anschelburk/PDM-Nutrient_Guide/pull/44#discussion_r1519613438_

bbelderbos commented 8 months ago

Actually I misstated .get() in a previous comment, that is to only retrieve values, to remove the bunch of if statements in the related comment you can do something like this:

st.session_state.setdefault("api_search_results_name", "")
st.session_state.setdefault("api_search_results_nutrients", [])
st.session_state.setdefault("cached_ingredient_names_and_nutrients", {})
st.session_state.setdefault("nutrients_i_need", recommended_daily_nutrients)
st.session_state.setdefault("nutrients_i_have", recommended_daily_nutrients_empty)

See: https://www.w3schools.com/python/ref_dictionary_setdefault.asp

bbelderbos commented 8 months ago

@anschelburk another easy one to quickly add today if you can ...