-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[WIP][Showcase] dvc: use dynamic scope to stop callback/flag passing #2957
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,9 @@ def __init__( | |
if file is None: | ||
file = sys.stderr | ||
self.desc_persist = desc | ||
# disable by context flag | ||
if flags.tqdm_disable: | ||
disable = True | ||
# auto-disable based on `logger.level` | ||
if disable is None: | ||
disable = logger.getEffectiveLevel() > level | ||
|
@@ -131,3 +134,40 @@ def format_dict(self): | |
d["ncols_desc"] = 1 | ||
d["prefix"] = "" | ||
return d | ||
|
||
|
||
# Adding this here for showcase only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
from contextlib import contextmanager # noqa | ||
|
||
|
||
class ContextFlags: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Suor , what about using this instead? from contextlib import contextmanager
from collections import UserDict
class ContextFlags(UserDict):
def __getattr__(self, name):
return self.data.get(name)
@contextmanager
def __call__(self, **flags):
self.data.update(flags)
yield
for key in flags.keys():
del self.data[key] |
||
def __init__(self): | ||
self._stack = [] | ||
|
||
def __call__(self, **values): | ||
return _flags(self._stack, values) | ||
|
||
def __getattr__(self, name): | ||
for d in reversed(self._stack): | ||
if name in d: | ||
return d[name] | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this indented correctly? |
||
return None | ||
|
||
|
||
flags = ContextFlags() | ||
|
||
|
||
@contextmanager | ||
def _flags(stack, values): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. woah, this is clever 👌 |
||
stack.append(values) | ||
yield | ||
stack.pop() | ||
|
||
|
||
class Noop: | ||
def __getattr__(self, name): | ||
return lambda self, *a, **kw: None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the same as doing? def __getattr__(self, name):
pass |
||
|
||
|
||
noop = Noop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would make sense to just have
pbar
as a part offlags
or some other structure instead of doingflags.tqdm or noop
everywhere?