question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

caching with comprehension list with if

See original GitHub issue

Summary

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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vhfgosselincommented, Nov 3, 2020

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.

0reactions
randyzwitchcommented, Dec 6, 2021

Closing as a wontfix, given the new st.memo and st.singleton functionality Streamlit is moving toward. If this same issue persists in that new functionality, please let us know!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found