Skip to content

Commit

Permalink
disabled cleanups (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
raubitsj authored Feb 11, 2021
1 parent 458585e commit 32dbe67
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 116 deletions.
92 changes: 92 additions & 0 deletions tests/test_mode_disabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
disabled mode test.
"""

from __future__ import division

import pytest # type: ignore

import wandb
import pickle
import os


def test_disabled_noop():
"""Make sure that all objects are dummy objects in noop case."""
run = wandb.init(mode="disabled")
run.log(dict(this=2))
run.finish()


def test_disabled_ops():
run = wandb.init(mode="disabled")
print(len(run))
print(abs(run))
print(~run)
print(run + 10)
print(run - 10)
print(run * 10)
print(run / 1.2)
print(run // 10)
print(run % 10)
print(run ** 10)
print(run << 10)
print(run >> 10)
print(run & 2)
print(run ^ 2)
print(run | 2)
print(+run)
print(-run)
run += 1
run -= 1
run *= 1
run /= 1.2
run //= 1
run **= 1
run <<= 1
run >>= 1
run |= 1
run %= 1
run ^= 1
run &= 1
run()
print(run.attrib)
print(run["item"])
run["3"] = 3
print(run["3"])
print(run[3])
print(int(run))
print(float(run))
print(run < 2)
print(run <= 2)
print(run == 2)
print(run > 2)
print(run >= 2)
print(run != 2)
print(run)
print(str(run))
print(repr(run))
if run:
print(run)
print(bool(run))


def test_disabled_summary():
run = wandb.init(mode="disabled")
run.summary["cat"] = 2
run.summary["nested"] = dict(level=3)
print(run.summary["cat"])
print(run.summary.cat)
with pytest.raises(KeyError):
print(run.summary["dog"])
run.summary["nested"]["level"] = 3


def test_disabled_can_pickle():
"""Will it pickle?"""
# This case comes up when using wandb in disabled mode, with keras
# https://wandb.atlassian.net/browse/WB-3981
obj = wandb.wandb_sdk.lib.RunDisabled()
with open("test.pkl", "wb") as file:
pickle.dump(obj, file)
os.remove("test.pkl")
25 changes: 0 additions & 25 deletions tests/test_mode_noop.py

This file was deleted.

7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,15 @@ commands =

[testenv:cover]
skip_install = true
# basepython = python3.6
deps =
pytest
coverage
commands =
/usr/bin/env bash -c '{envpython} -m coverage combine {toxworkdir}/py*/.coverage'
coverage report --fail-under 80 --skip-covered --include "tests/*"
coverage report --fail-under 50 --skip-covered
coverage report -m --ignore-errors --skip-covered --omit "wandb/vendor/*"
coverage report --ignore-errors --skip-covered --fail-under 90 --include "tests/*"
coverage report --ignore-errors --skip-covered --fail-under 60 --include "wandb/sdk/"
coverage report --ignore-errors --skip-covered --fail-under 50 --include "wandb/" --exclude "wandb/sdk*"

[coverage:run]
omit =
Expand Down
6 changes: 6 additions & 0 deletions wandb/sdk/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
#
from . import lazyloader # noqa: F401
from .disabled import RunDisabled, SummaryDisabled

__all__ = [
"RunDisabled",
"SummaryDisabled",
]
71 changes: 37 additions & 34 deletions wandb/dummy.py → wandb/sdk/lib/disabled.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,93 @@
class Dummy(str):
#


class RunDisabled(str):
def __init__(self, *args, **kwargs):
object.__setattr__(self, "___dict", {})

def __add__(self, other):
return Dummy()
return self

def __sub__(self, other):
return Dummy()
return self

def __mul__(self, other):
return Dummy()
return self

def __truediv__(self, other):
return Dummy()
return self

def __floordiv__(self, other):
return Dummy()
return self

def __mod__(self, other):
return Dummy()
return self

def __pow__(self, other, modulo=None):
return Dummy()
return self

def __lshift__(self, other):
return Dummy()
return self

def __rshift__(self, other):
return Dummy()
return self

def __and__(self, other):
return Dummy()
return self

def __xor__(self, other):
return Dummy()
return self

def __or__(self, other):
return Dummy()
return self

def __iadd__(self, other):
pass
return self

def __isub__(self, other):
pass
return self

def __imul__(self, other):
pass
return self

def __idiv__(self, other):
pass
return self

def __ifloordiv__(self, other):
pass
return self

def __imod__(self, other):
pass
return self

def __ipow__(self, other, modulo=None):
pass
return self

def __ilshift__(self, other):
pass
return self

def __irshift__(self, other):
pass
return self

def __iand__(self, other):
pass
return self

def __ixor__(self, other):
pass
return self

def __ior__(self, other):
pass
return self

def __neg__(self):
return Dummy()
return self

def __pos__(self):
return Dummy()
return self

def __abs__(self):
return Dummy()
return self

def __invert__(self):
return Dummy()
return self

def __complex__(self):
return 1 + 0j
Expand Down Expand Up @@ -134,7 +137,7 @@ def __getitem__(self, key):
key = str(key)
if key in d:
return d[key]
dummy = Dummy()
dummy = RunDisabled()
d[key] = dummy
return dummy

Expand All @@ -145,7 +148,7 @@ def __setattr__(self, key, value):
self[key] = value

def __call__(self, *args, **kwargs):
return Dummy()
return RunDisabled()

def __len__(self):
return 1
Expand All @@ -172,7 +175,7 @@ def __getstate__(self):
return 1


class DummyDict(dict):
class SummaryDisabled(dict):
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__

Expand All @@ -181,7 +184,7 @@ def __getattr__(self, key):

def __getitem__(self, key):
val = dict.__getitem__(self, key)
if isinstance(val, dict) and not isinstance(val, DummyDict):
val = DummyDict(val)
if isinstance(val, dict) and not isinstance(val, SummaryDisabled):
val = SummaryDisabled(val)
self[key] = val
return val
Loading

0 comments on commit 32dbe67

Please sign in to comment.