code: def filter_record(records):
      
  
      def scheme(r):
          if r.interaction is None:
              call_duration_ok = True
          elif r.interaction == 'call':
              call_duration_ok = isinstance(r.call_duration, (int, float))
          else:
              call_duration_ok = True
  
          callandtext = r.interaction in ['call', 'text']
          not_callandtext = not callandtext
  
          return {
              'interaction': r.interaction in ['call', 'text', 'gps', None],
              'direction': (not_callandtext and r.direction is None) or r.direction in ['in', 'out'],
              'correspondent_id': not_callandtext or (r.correspondent_id not in [None, '']),
              'datetime': isinstance(r.datetime, datetime),
              'call_duration': call_duration_ok,
              'location': callandtext or r.position.type() is not None
          }
  
      ignored = OrderedDict([
          ('all', 0),
          ('interaction', 0),
          ('direction', 0),
          ('correspondent_id', 0),
          ('datetime', 0),
          ('call_duration', 0),
          ('location', 0),
      ])
  
      bad_records = []
  
      def _filter(records):
          for r in records:
              valid = True
              for key, valid_key in scheme(r).items():
                  if not valid_key:
                      ignored[key] += 1
                      bad_records.append(r)
                      # Not breaking, to count all fields with errors
                      valid = False
  
              if valid:
                  yield r
              else:
                  ignored['all'] += 1
  
      return list(_filter(records)), ignored, bad_records
docs: Filter records and remove items with missing or inconsistent fields
  
      Parameters
      ----------
  
      records : list
          A list of Record objects
  
      Returns
      -------
      records, ignored : (Record list, dict)
          A tuple of filtered records, and a dictionary counting the
          missings fields