def affinity_locality_predicate(write_affinity_str):
 affinity_str = write_affinity_str.strip()
 if (not affinity_str):
  return None
 matchers = []
 pieces = [s.strip() for s in affinity_str.split(',')]
 for piece in pieces:
  match = re.match('r(\\d+)(?:z(\\d+))?$', piece)
  if match:
   (region, zone) = match.groups()
   region = int(region)
   zone = (int(zone) if zone else None)
   matcher = {'region': region}
   if (zone is not None):
    matcher['zone'] = zone
   matchers.append(matcher)
  else:
   raise ValueError(('Invalid write-affinity value: %r' % affinity_str))
 def is_local(ring_node):
  for matcher in matchers:
   if ((matcher['region'] == ring_node['region']) and (('zone' not in matcher) or (matcher['zone'] == ring_node['zone']))):
    return True
  return False
 return is_local

def affinity_locality_predicate ( write_affinity_str ) : affinity_str = write_affinity_str . strip ( ) if ( not affinity_str ) : return None matchers = [ ] pieces = [ s . strip ( ) for s in affinity_str . split ( ',' ) ] for piece in pieces : match = re . match ( 'r(\\d+)(?:z(\\d+))?$' , piece ) if match : ( region , zone ) = match . groups ( ) region = int ( region ) zone = ( int ( zone ) if zone else None ) matcher = { 'region' : region } if ( zone is not None ) : matcher [ 'zone' ] = zone matchers . append ( matcher ) else : raise ValueError ( ( 'Invalid write-affinity value: %r' % affinity_str ) ) def is_local ( ring_node ) : for matcher in matchers : if ( ( matcher [ 'region' ] == ring_node [ 'region' ] ) and ( ( 'zone' not in matcher ) or ( matcher [ 'zone' ] == ring_node [ 'zone' ] ) ) ) : return True return False return is_local
turns a write-affinity config value into a predicate function for nodes .