Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the progress without the with statement #99

Closed
RobinFrcd opened this issue Aug 11, 2021 · 8 comments
Closed

Using the progress without the with statement #99

RobinFrcd opened this issue Aug 11, 2021 · 8 comments

Comments

@RobinFrcd
Copy link

Hello,
I know with statements are great in some cases, but I also like to have my code with as less indents blocks as possible.

Is it possible to use the package without the with statement ? With a wrapper function over the generator I'm looping over.

Thanks!

@rsalmei
Copy link
Owner

rsalmei commented Aug 12, 2021

Hello @RobinFrcd,

In the new yet unreleased 2.0 you can:

from alive_progress import alive_it

for item in alive_it(items):   # <<-- wrapped items
    print(item)                # process each item

If you want to test it, before I release it, you can:

pip install git+https://github.com/rsalmei/alive-progress.git@rsa-refac

@rsalmei
Copy link
Owner

rsalmei commented Aug 18, 2021

Well, I'm going to assume it did work.
Please reopen if anything new arises.

@rsalmei rsalmei closed this as completed Aug 18, 2021
@RobinFrcd
Copy link
Author

Thank you for your answer!

Sorry for the late reply, I've only been able to test it today, but I've an issue with this version (with both alive_bar and alive_it):

Traceback (most recent call last):
  File "training/test_trainer.py", line 33, in <module>
    test_main()
  File "training/test_trainer.py", line 21, in test_main
    params=params,
  File "/Users/rfourcade/dev/research/generation/scoring/offpolicy-network/iktos/offpolicy_network/training/trainer.py", line 93, in main
    with alive_bar(params.nb_epochs, title="Epochs") as bar:
  File "/Users/rfourcade/.pyenv/versions/3.7.9/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/Users/rfourcade/Library/Caches/pypoetry/virtualenvs/iktos-offpolicy-network-Oz28eVis-py3.7/lib/python3.7/site-packages/alive_progress/core/progress.py", line 247, in __alive_bar
    start_monitoring()
  File "/Users/rfourcade/Library/Caches/pypoetry/virtualenvs/iktos-offpolicy-network-Oz28eVis-py3.7/lib/python3.7/site-packages/alive_progress/core/progress.py", line 144, in start_monitoring
    hook_manager.install()
  File "/Users/rfourcade/Library/Caches/pypoetry/virtualenvs/iktos-offpolicy-network-Oz28eVis-py3.7/lib/python3.7/site-packages/alive_progress/core/hook_manager.py", line 66, in install
    for h in root.handlers if isinstance(h, StreamHandler)})
  File "/Users/rfourcade/Library/Caches/pypoetry/virtualenvs/iktos-offpolicy-network-Oz28eVis-py3.7/lib/python3.7/site-packages/alive_progress/core/hook_manager.py", line 66, in <dictcomp>
    for h in root.handlers if isinstance(h, StreamHandler)})
  File "/Users/rfourcade/Library/Caches/pypoetry/virtualenvs/iktos-offpolicy-network-Oz28eVis-py3.7/lib/python3.7/site-packages/alive_progress/core/hook_manager.py", line 96, in _set_stream
    return handler.setStream(stream)
  File "/Users/rfourcade/.pyenv/versions/3.7.9/lib/python3.7/logging/__init__.py", line 1050, in setStream
    self.stream = stream
AttributeError: can't set attribute

Is it a known bug in this version ? Thanks

@rsalmei
Copy link
Owner

rsalmei commented Aug 19, 2021

No problem, you're welcome.

But wow, that's very weird.
This error is for when one tries to set a @property which has no setter.
But stream is a ordinary field there.

Can you please write a minimal code that triggers this error?
Did you use python or ipython?

@rsalmei rsalmei reopened this Aug 19, 2021
@RobinFrcd
Copy link
Author

RobinFrcd commented Aug 19, 2021

My company uses a custom logger wrapper. I just realized this bug happens only when I'm using this custom logger and not the vanilla one.

I guess I'll have to take a look at our own logger then! Thanks!


Alright, it's related to coloredlogs

import coloredlogs, logging
from alive_progress import alive_it

LOGGER = logging.getLogger(__name__)
coloredlogs.install(level="DEBUG")
coloredlogs.install(level="DEBUG", logger=LOGGER)

if __name__ == "__main__":
    for e in alive_it(range(5)):
        print(e)

This package seems almost abandoned now, maybe we should consider removing this dependency

@rsalmei
Copy link
Owner

rsalmei commented Aug 19, 2021

Wow, that's it.
Unfortunately it is a bad implementation this coloredlogs... I found it:
https://github.com/xolox/python-coloredlogs/blob/master/coloredlogs/__init__.py#L1336-L1337

Since Python 3.7, StreamHandler has this setStream method, and that implementation simply breaks it...

On a quick look, you should try to avoid that coloredlogs' StandardErrorHandler to be installed. It seems the easiest way is to not use stderr in the first place. Try to send your logs to stdout, or disable it any other way.

@RobinFrcd
Copy link
Author

The thing is I need this stderr too. I've opened an issue, I hope it'll be fixed soon. Otherwise, I'll make my own fork or use colorlog I guess.

Anyway, thanks for the help here ! The issue has nothing to do with alive-progress so I guess we can close it.

@rsalmei
Copy link
Owner

rsalmei commented Aug 19, 2021

Nice, hope they fix that.
But this other colorlog seems way less invasive.
You're welcome man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants