caching with comprehension list with if
See original GitHub issueSummary
Caching a function with a comprehension list with an if inside seem creates an error message.
Steps to reproduce
What are the steps we should take to reproduce the bug: Here’s a sample code:
import streamlit as st
@st.cache()
def create_raw_data():
global production
production=[[10 for m in range(3) if w>2] for w in range(5)]
create_raw_data()
st.write(production)
Expected behavior:
Actual behavior:
KeyError: 'w'
Streamlit encountered an error while caching the body of create_raw_data(). This is likely due to a bug in C:\Vincent\PycharmProjects\Streamlit_trials\Sugar\generator.py near line 6:
production=[[10 for m in range(3) if w>2] for w in range(5)]
Is this a regression?
I doesn’t seem so since I tried this with several versions of streamlit and still got the same error message.
Debug info
- Streamlit version: (get it with
$ streamlit version
): 0.64 - Python version: (get it with
$ python --version
) Python 3.7 and Python 3.8 - Using Conda? PipEnv? PyEnv? Pex? Tried with conda and pipenv
- OS version: windows 10
- Browser version: chrome 84.0
Additional information
It seems that the if statement is the culprit here, if removed the code is cached nicely.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Caching values in Python list comprehensions - Stack Overflow
Is there a way to "cache" the value of obj.get("file") when it's checked in the if statement so that it doesn't have to...
Read more >List Comprehension, Conditional, and Looping Statements ...
Loops in python are used to check if a condition is true then they execute a chunk of code within the block continuously...
Read more >Python List Comprehension: single, multiple, nested, & more
Conditional statements in list comprehensions. When using conditionals, Python checks whether our if statement returns True or False for each yield. When the...
Read more >Python Dictionary Comprehension For Multi Level Caching
A dictionary comprehension basically lets you create a dictionary out of an expression. So, you can essentially say “for each value in this...
Read more >List Comprehension as Compulsory Functional Style
If you have a lot of free memory it does not mean that it is not a problem because you are filling your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, this wasn’t solved. The go-around for me in my real code ( I just provided here a simple example) was to do what jrhone suggested:
replace: [[10 for m in range(3) if w>2] for w in range(5)] by [[10 for m in range(3)] for w in range(5) if w > 2]
although this is not exactly the same thing.
So in summary, there is a bug still it seems.
Closing as a wontfix, given the new
st.memo
andst.singleton
functionality Streamlit is moving toward. If this same issue persists in that new functionality, please let us know!