code: def only_if(condition_expr):
      """
      Decorator to fire a callback only if the given conditional expression function returns True. The conditional
      expression can be a function of state or self and state. If the decorated function is not a class method (i.e. it
      does not take state) the decorated function will be passed instead. This enables the storing of temporary variables.
  
      Example: ::
  
          >>> import torchbearer
          >>> from torchbearer import Trial
          >>> from torchbearer.callbacks import only_if, on_step_training
  
          # Example callback to be called only when the given condition is true on each training step
          >>> @only_if(lambda state: state[torchbearer.BATCH] == 100)
          ... @on_step_training
          ... def print_callback(state):
          ...     print('This is the 100th batch')
  
          >>> trial = Trial(None, callbacks=[print_callback]).for_steps(101).run()
          This is the 100th batch
  
      Args:
          condition_expr (function(self, state) or function(self)): a function/lambda which takes state and optionally\
                          self that must evaluate to true for the decorated `torchbearer callback` to be called. The\
                          `state` object passed to the callback will be passed as an argument to the condition function.
  
      Returns:
          the decorator
      """
  
      def condition_decorator(fcn):
          if isinstance(fcn, LambdaCallback):
              fcn.func = condition_decorator(fcn.func)
              return fcn
          else:
              count = count_args(fcn)
              if count == 2 and not hasattr(
                  fcn, "__self__"
              ):  # Assume Class method
  
                  def decfcn(o, state):
                      try:
                          res = condition_expr(o, state)
                      except TypeError:
                          res = condition_expr(state)
                      if res:
                          return fcn(o, state)
  
              else:  # Assume function of state
  
                  def id_fcn(state):
                      return fcn(
                          state
                      )  # Hack to allow setting attributes of bound methods
  
                  def decfcn(state):
                      try:
                          res = condition_expr(id_fcn, state)
                      except TypeError:
                          res = condition_expr(state)
                      if res:
                          return id_fcn(state)
  
              return decfcn
  
      return condition_decorator
  
imports: import sys
  import types
  import torchbearer
  from torchbearer.callbacks import Callback
label_desc: Use image node_id 24 for calling a global function with example usage: only_if(condition_expr) and returns: condition_decorator, fcn, decfcn, fcn, fcn, id_fcn