Usage without a context manager
See original GitHub issueI’d like to use this progress bar with a framework, but that framework provides a mechanism for updating a progress indicator that the framework calls. Ie it’s kinda like:
class MyProgressIndicator(BaseClassFromFramework):
def update(self, current_count, total):
# update the progress bar here
The idea is that I register MyProgressIndicator
with the framework, and it instantiates MyProgressIndicator
for me, and calls update()
each time it processes an item. Ideally I’d like to use alive-progress here, but the problem is that there’s no place in the way this framework works for registering a context manager. Ideally, what I’d like to do is something like:
class MyProgressIndicator(BaseClassFromFramework):
def __init__(self, total_to_process):
self.alive_bar = alive_bar(total_to_process)
def update(self, current_count, total):
self.alive_bar() # update the progress bar
Is there some way to achieve that?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using context managers without "with" block
If we design my_create like above, we can use it normally without a block: new_obj = my_create(a=10) new_obj.a # => 10 new_obj.is_saved() ...
Read more >contextlib — Utilities for with-statement contexts ... - Python Docs
Distinct from both single use and reentrant context managers are “reusable” context managers (or, to be completely explicit, “reusable, but not reentrant” ...
Read more >[Python] Can a context manager be used as a regular function?
I have a function that sets some values and I sometimes want to use it in a with-statement (so it restores the original...
Read more >Context Managers and Python's with Statement
In this step-by-step tutorial, you'll learn what the Python with statement is and how to use it with existing context managers.
Read more >27. Context Managers — Python Tips 0.1 documentation
Context managers allow you to allocate and release resources precisely when you want to. The most widely used example of context managers is...
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, of course it is an option! It is actually to use a variable number of context managers, but could be used with just one for this purpose. I didn’t suggest it because it is for Python 3 only, and prefer solutions that work anywhere. Thanks.
No problem man, glad to see it did work! You’re welcome 👍