From 67226ac7dd7af40635cd9888a760a3536b96656f Mon Sep 17 00:00:00 2001 From: Ranel Padon Date: Mon, 19 Jun 2023 07:46:14 +0800 Subject: [PATCH 1/3] Update the install/uninstall functions to make it more flexible. --- icecream/builtins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/icecream/builtins.py b/icecream/builtins.py index 67136e0..7b9afa7 100644 --- a/icecream/builtins.py +++ b/icecream/builtins.py @@ -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) From f126a613e124d95458c71ad2aa10e93ffc66c09e Mon Sep 17 00:00:00 2001 From: Ranel Padon Date: Mon, 19 Jun 2023 07:47:28 +0800 Subject: [PATCH 2/3] Update the tests. --- tests/install_test_import.py | 7 +++++- tests/test_icecream.py | 10 ++++----- tests/test_install.py | 43 ++++++++++++++++++++++++++++++------ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/tests/install_test_import.py b/tests/install_test_import.py index cf03ac0..af50f82 100644 --- a/tests/install_test_import.py +++ b/tests/install_test_import.py @@ -10,6 +10,11 @@ # License: MIT # -def runMe(): +def runMeDefault(): x = 3 ic(x) + + +def runMeCustom(): + x = 4 + ik(x) diff --git a/tests/test_icecream.py b/tests/test_icecream.py index 40497ba..71fc57c 100644 --- a/tests/test_icecream.py +++ b/tests/test_icecream.py @@ -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 @@ -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 diff --git a/tests/test_install.py b/tests/test_install.py index 61f9a79..6b8168d 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -13,21 +13,28 @@ 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. @@ -35,4 +42,26 @@ def testUninstall(self): # 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() From 9d3efaf5c725ac6db121fa9df11da5f4ec279612 Mon Sep 17 00:00:00 2001 From: Ranel Padon Date: Mon, 19 Jun 2023 08:07:12 +0800 Subject: [PATCH 3/3] Update the README. --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 778e044..f6ce0fd 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ or a function. ```pycon >>> import time >>> from icecream import ic ->>> +>>> >>> def unixTimestamp(): >>> return '%i |> ' % int(time.time()) >>> @@ -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.