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.

Usage without a context manager

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
rsalmeicommented, Jun 10, 2020

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.

0reactions
rsalmeicommented, Jun 14, 2020

No problem man, glad to see it did work! You’re welcome 👍

Read more comments on GitHub >

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

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