Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. '''
  2. Please help with integrating the ```update_name``` and ```update_number``` into the ```shape_element``` function.
  3.  
  4. Note: all of these functions work fine independently
  5.  
  6. '''
  7.  
  8. def std_phone(num):
  9.     '''
  10.    Standardizes phone number
  11.    '''
  12.    
  13.    
  14.     # Remove anything that is not a number
  15.     num = re.sub('[^0-9]+', '', num)
  16.     # add dashes to 10 digit nums
  17.     if re.match(r'^\d{10}$', num) is not None:
  18.         num = num[:3] + '-' + num[3:6]+ '-' + num[6:]
  19.         print '1'
  20.     # add dashes to 11 digit nums
  21.     elif re.match(r'^\d{11}$', num) is not None:
  22.         num = num[0] + '-' + num[1:4] + '-' + num[4:7] + '-' +num[7:]
  23.         print '2'
  24.    
  25.     return num
  26.  
  27. std_phone('+148059692%^#@!a a ask99')
  28.  
  29.  
  30. def update_name(name, mapping):
  31.     '''
  32.    1.Removes APT and house numbers from the street address
  33.    2.Using a regex function to search for problematic names,
  34.    then modifies it according to the given mapping.
  35.    
  36.    Args:
  37.        name(string): potential problematic street name
  38.        mapping(dict): the desired outcome of street suffixes
  39.        
  40.    Returns:
  41.        name(string): All street names (fixed and non-fixed)
  42.        '''
  43.    
  44.  
  45.     if '#' in name:
  46.         name = name.split('#',-1)[0].strip()
  47.        
  48.     if ',' in name:
  49.         name = name.split(',', 1)[0].strip()
  50.    
  51.    
  52.        
  53.     m = street_type_re.search(name)
  54.    
  55.    
  56.     if m:
  57.         street_type = m.group()
  58.         if street_type not in expected: # This is somewhat redundant, but it acts as a fail-safe
  59.             if street_type in mapping:
  60.    
  61.                 name= re.sub(street_type_re, mapping[street_type] , name)
  62.        
  63.  
  64.     return name
  65.  
  66.  
  67. def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
  68.                   problem_chars=PROBLEMCHARS, default_tag_type='regular'):
  69.     """Clean and shape node or way XML element to Python dict"""
  70.    
  71.    
  72.    
  73.     node_attribs = {}
  74.     way_attribs = {}
  75.     way_nodes_dict = {}
  76.     way_nodes = []
  77.     tags = []  # Handle secondary tags the same way for both node and way elements
  78.     way_tag_dict = {}
  79.    
  80.  
  81.     # YOUR CODE HERE
  82.    
  83.    
  84.     if element.tag == 'node':
  85.         for node in NODE_FIELDS:
  86.             node_attribs[node] = element.attrib[node]
  87.         for child in element:
  88.             tag_dict = {}
  89.            
  90.             if child.tag == 'tag':
  91.                 tag_dict['id'] = element.attrib['id'] # set the id = parent id
  92.                
  93.                
  94.                
  95.                 # and child.attrib['v'] here
  96.                 tag_dict['value'] = child.attrib['v']
  97.                
  98.                 n = re.search(PROBLEMCHARS, child.attrib['k'] )
  99.                 m = re.search(LOWER_COLON, child.attrib['k'])
  100.                
  101.                 if n:
  102.                     # you access child.attrib['k']
  103.                     tag_dict['key'] = child.attrib['k']
  104.                     continue
  105.                    
  106.                    
  107.                 elif m:
  108.                     # in case of colon in key, clean to standardize the data
  109.                     tag_dict['key'] = child.attrib['k'].split(':')[0]
  110.                     tag_dict['type'] = child.attrib['k'].split(':',1)[0]
  111.                    
  112.                
  113.                 else:
  114.                     # if it does not meet the above criteria, assign it as it is
  115.                     tag_dict['key'] = child.attrib['k']
  116.                     tag_dict['type'] = 'Regular'
  117.                
  118.                 #print tag_dict works fine here
  119.                 # append the result of dictionaries to tags list
  120.                 # Note it is not appending as it supposed to
  121.                 tags.append(tag_dict)
  122.        
  123.            
  124.  
  125.         return {'node': node_attribs, 'node_tags': tags}
  126.    
  127.     elif element.tag == 'way':
  128.         # implement way_attribs here
  129.         for way in WAY_FIELDS:
  130.             way_attribs[way] = element.attrib[way]
  131.            
  132.            
  133.            
  134.         # implement childern in elements here (way_nodes_dict)
  135.         counter = 0
  136.         for child in element:
  137.             if child.tag == 'tag':
  138.                 tag = {'id': way_attribs['id']}
  139.                 k = child.get('k')
  140.                 if not PROBLEMCHARS.search(k):
  141.                     k = k.split(':', 1)
  142.                     tag['key'] = k[-1]
  143.                     tag['value'] = child.get('v')
  144.                     if len(k) == 1:
  145.                         tag['type'] = 'regular'    
  146.                     elif len(k) == 2:
  147.                         tag['type'] = k[0]
  148.                 tags.append(tag)
  149.                
  150.                
  151.             if child.tag == 'nd':
  152.                 nd = {'id' : way_attribs['id']}
  153.                 nd['node_id'] = child.get('ref')
  154.                 nd['position'] = counter
  155.                 way_nodes.append(nd)
  156.             counter += 1
  157.                
  158.                
  159.        
  160.            
  161.        
  162.            
  163.                
  164.        
  165.                
  166.         return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement