from __future__ import unicode_literals
from .base import Always, Never
from .types import SimpleFilter, CLIFilter

__all__ = (
    'to_cli_filter',
    'to_simple_filter',
)


def to_simple_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a SimpleFilter.
    """
    assert isinstance(bool_or_filter, (bool, SimpleFilter)), \
        TypeError('Expecting a bool or a SimpleFilter instance. Got %r' % bool_or_filter)

    return {
        True: Always(),
        False: Never()
    }.get(bool_or_filter, bool_or_filter)


def to_cli_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a CLIFilter.
    """
    assert isinstance(bool_or_filter, (bool, CLIFilter)), \
        TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)

    return {
        True: Always(),
        False: Never()
    }.get(bool_or_filter, bool_or_filter)

from __future__ import unicode_literals
from .base import Always, Never
from .types import SimpleFilter, CLIFilter

__all__ = (
    'to_cli_filter',
    'to_simple_filter',
)

_always = Always()
_never = Never()


def to_simple_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a SimpleFilter.
    """
    assert isinstance(bool_or_filter, (bool, SimpleFilter)), \
        TypeError('Expecting a bool or a SimpleFilter instance. Got %r' % bool_or_filter)

    return {
        True: _always,
        False: _never,
    }.get(bool_or_filter, bool_or_filter)


def to_cli_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a CLIFilter.
    """
    assert isinstance(bool_or_filter, (bool, CLIFilter)), \
        TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)

    return {
        True: _always,
        False: _never,
    }.get(bool_or_filter, bool_or_filter)

Reduce amount of Always/Never instances. (The are stateless.)
Reduce amount of Always/Never instances. (The are stateless.)

  from __future__ import unicode_literals
  from .base import Always, Never
  from .types import SimpleFilter, CLIFilter
  
  __all__ = (
      'to_cli_filter',
      'to_simple_filter',
  )
+ 
+ _always = Always()
+ _never = Never()
  
  
  def to_simple_filter(bool_or_filter):
      """
      Accept both booleans and CLIFilters as input and
      turn it into a SimpleFilter.
      """
      assert isinstance(bool_or_filter, (bool, SimpleFilter)), \
          TypeError('Expecting a bool or a SimpleFilter instance. Got %r' % bool_or_filter)
  
      return {
-         True: Always(),
+         True: _always,
-         False: Never()
+         False: _never,
      }.get(bool_or_filter, bool_or_filter)
  
  
  def to_cli_filter(bool_or_filter):
      """
      Accept both booleans and CLIFilters as input and
      turn it into a CLIFilter.
      """
      assert isinstance(bool_or_filter, (bool, CLIFilter)), \
          TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)
  
      return {
-         True: Always(),
+         True: _always,
-         False: Never()
+         False: _never,
      }.get(bool_or_filter, bool_or_filter)
  
Reduce amount of Always/Never instances. (The are stateless.)
## Code Before:
from __future__ import unicode_literals
from .base import Always, Never
from .types import SimpleFilter, CLIFilter

__all__ = (
    'to_cli_filter',
    'to_simple_filter',
)


def to_simple_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a SimpleFilter.
    """
    assert isinstance(bool_or_filter, (bool, SimpleFilter)), \
        TypeError('Expecting a bool or a SimpleFilter instance. Got %r' % bool_or_filter)

    return {
        True: Always(),
        False: Never()
    }.get(bool_or_filter, bool_or_filter)


def to_cli_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a CLIFilter.
    """
    assert isinstance(bool_or_filter, (bool, CLIFilter)), \
        TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)

    return {
        True: Always(),
        False: Never()
    }.get(bool_or_filter, bool_or_filter)

## Instruction:
Reduce amount of Always/Never instances. (The are stateless.)
## Code After:
from __future__ import unicode_literals
from .base import Always, Never
from .types import SimpleFilter, CLIFilter

__all__ = (
    'to_cli_filter',
    'to_simple_filter',
)

_always = Always()
_never = Never()


def to_simple_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a SimpleFilter.
    """
    assert isinstance(bool_or_filter, (bool, SimpleFilter)), \
        TypeError('Expecting a bool or a SimpleFilter instance. Got %r' % bool_or_filter)

    return {
        True: _always,
        False: _never,
    }.get(bool_or_filter, bool_or_filter)


def to_cli_filter(bool_or_filter):
    """
    Accept both booleans and CLIFilters as input and
    turn it into a CLIFilter.
    """
    assert isinstance(bool_or_filter, (bool, CLIFilter)), \
        TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)

    return {
        True: _always,
        False: _never,
    }.get(bool_or_filter, bool_or_filter)
