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

Multi-Configuration/Multi-Installation to Built-ins #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ or a function.
```pycon
>>> import time
>>> from icecream import ic
>>>
>>>
>>> def unixTimestamp():
>>> return '%i |> ' % int(time.time())
>>>
Expand Down Expand Up @@ -368,6 +368,32 @@ ic| example.py:18 in foo()- i: 3

`contextAbsPath` is False by default.

#### Multi-Configuration
You might want to create multiple configurations bound to different names.
For example, `ic()` with context for usual output in terminal,
and `ik()` without context for piping to logger which usually has context already.
The `ik` name is just an example (e.g. short for `ice-kream`).

```python
from icecream import IceCreamDebugger, ic, install

# Usual instance, for reference.
ic.configureOutput(includeContext=True)
install()

# Second instance/configuration.
ik = IceCreamDebugger()
ik.configureOutput(includeContext=False, prefix='')
install(attribute='ik', value=ik)

# Pipe IceCream output to a logger.
# Logger usually has context already, so the IceCream's context is redundant.
# So, we could just redirect the IceCream contents without context.
# Likewise, this makes it possible to control IceCream with log level.
foo = 'bar'
logger.debug(ik.format(foo))
```

### Installation

Installing IceCream with pip is easy.
Expand Down
8 changes: 4 additions & 4 deletions icecream/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
builtins = __import__('builtins')


def install(ic='ic'):
setattr(builtins, ic, icecream.ic)
def install(attribute='ic', value=icecream.ic):
setattr(builtins, attribute, value)


def uninstall(ic='ic'):
delattr(builtins, ic)
def uninstall(attribute='ic'):
delattr(builtins, attribute)
7 changes: 6 additions & 1 deletion tests/install_test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
# License: MIT
#

def runMe():
def runMeDefault():
x = 3
ic(x)


def runMeCustom():
x = 4
ik(x)
10 changes: 5 additions & 5 deletions tests/test_icecream.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def isatty(self):


@contextmanager
def disableColoring():
originalOutputFunction = ic.outputFunction
def disableColoring(icecream_instance=ic):
originalOutputFunction = icecream_instance.outputFunction

ic.configureOutput(outputFunction=stderrPrint)
icecream_instance.configureOutput(outputFunction=stderrPrint)
yield
ic.configureOutput(outputFunction=originalOutputFunction)
icecream_instance.configureOutput(outputFunction=originalOutputFunction)


@contextmanager
Expand Down Expand Up @@ -505,7 +505,7 @@ def testContextAbsPathMultiLine(self):

pair = parseOutputIntoPairs(out, err, 3)[1][0]
assert pair == ('multilineStr', ic.argToStringFunction(multilineStr))

def testFormat(self):
with disableColoring(), captureStandardStreams() as (out, err):
"""comment"""; noop(); ic( # noqa
Expand Down
43 changes: 36 additions & 7 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,55 @@
import unittest

import icecream
from install_test_import import (
runMeCustom,
runMeDefault,
)
from test_icecream import (
disableColoring, captureStandardStreams, parseOutputIntoPairs)

from install_test_import import runMe
captureStandardStreams,
disableColoring,
parseOutputIntoPairs,
)


class TestIceCreamInstall(unittest.TestCase):
def testInstall(self):

def testInstallDefault(self):
icecream.install()
with disableColoring(), captureStandardStreams() as (out, err):
runMe()
runMeDefault()

assert parseOutputIntoPairs(out, err, 1)[0][0] == ('x', '3')
icecream.uninstall() # Clean up builtins.

def testUninstall(self):
def testUninstallDefault(self):
try:
icecream.uninstall()
except AttributeError: # Already uninstalled.
pass

# NameError: global name 'ic' is not defined.
with self.assertRaises(NameError):
runMe()
runMeDefault()

def testInstallCustom(self):
# Create new instance of IceCream and install it.
ik = icecream.IceCreamDebugger()
ik.configureOutput(includeContext=False, prefix='')
icecream.install(attribute='ik', value=ik)

with disableColoring(icecream_instance=ik), captureStandardStreams() as (out, err):
runMeCustom()

assert parseOutputIntoPairs(out, err, 1)[0][0] == ('x', '4')
icecream.uninstall(attribute='ik') # Clean up builtins.

def testUninstallCustom(self):
try:
icecream.uninstall('ik')
except AttributeError: # Already uninstalled.
pass

# NameError: global name 'ik' is not defined.
with self.assertRaises(NameError):
runMeCustom()