Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Please help with integrating the ```update_name``` and ```update_number``` into the ```shape_element``` function.
- Note: all of these functions work fine independently
- '''
- def std_phone(num):
- '''
- Standardizes phone number
- '''
- # Remove anything that is not a number
- num = re.sub('[^0-9]+', '', num)
- # add dashes to 10 digit nums
- if re.match(r'^\d{10}$', num) is not None:
- num = num[:3] + '-' + num[3:6]+ '-' + num[6:]
- print '1'
- # add dashes to 11 digit nums
- elif re.match(r'^\d{11}$', num) is not None:
- num = num[0] + '-' + num[1:4] + '-' + num[4:7] + '-' +num[7:]
- print '2'
- return num
- std_phone('+148059692%^#@!a a ask99')
- def update_name(name, mapping):
- '''
- 1.Removes APT and house numbers from the street address
- 2.Using a regex function to search for problematic names,
- then modifies it according to the given mapping.
- Args:
- name(string): potential problematic street name
- mapping(dict): the desired outcome of street suffixes
- Returns:
- name(string): All street names (fixed and non-fixed)
- '''
- if '#' in name:
- name = name.split('#',-1)[0].strip()
- if ',' in name:
- name = name.split(',', 1)[0].strip()
- m = street_type_re.search(name)
- if m:
- street_type = m.group()
- if street_type not in expected: # This is somewhat redundant, but it acts as a fail-safe
- if street_type in mapping:
- name= re.sub(street_type_re, mapping[street_type] , name)
- return name
- def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
- problem_chars=PROBLEMCHARS, default_tag_type='regular'):
- """Clean and shape node or way XML element to Python dict"""
- node_attribs = {}
- way_attribs = {}
- way_nodes_dict = {}
- way_nodes = []
- tags = [] # Handle secondary tags the same way for both node and way elements
- way_tag_dict = {}
- # YOUR CODE HERE
- if element.tag == 'node':
- for node in NODE_FIELDS:
- node_attribs[node] = element.attrib[node]
- for child in element:
- tag_dict = {}
- if child.tag == 'tag':
- tag_dict['id'] = element.attrib['id'] # set the id = parent id
- # and child.attrib['v'] here
- tag_dict['value'] = child.attrib['v']
- n = re.search(PROBLEMCHARS, child.attrib['k'] )
- m = re.search(LOWER_COLON, child.attrib['k'])
- if n:
- # you access child.attrib['k']
- tag_dict['key'] = child.attrib['k']
- continue
- elif m:
- # in case of colon in key, clean to standardize the data
- tag_dict['key'] = child.attrib['k'].split(':')[0]
- tag_dict['type'] = child.attrib['k'].split(':',1)[0]
- else:
- # if it does not meet the above criteria, assign it as it is
- tag_dict['key'] = child.attrib['k']
- tag_dict['type'] = 'Regular'
- #print tag_dict works fine here
- # append the result of dictionaries to tags list
- # Note it is not appending as it supposed to
- tags.append(tag_dict)
- return {'node': node_attribs, 'node_tags': tags}
- elif element.tag == 'way':
- # implement way_attribs here
- for way in WAY_FIELDS:
- way_attribs[way] = element.attrib[way]
- # implement childern in elements here (way_nodes_dict)
- counter = 0
- for child in element:
- if child.tag == 'tag':
- tag = {'id': way_attribs['id']}
- k = child.get('k')
- if not PROBLEMCHARS.search(k):
- k = k.split(':', 1)
- tag['key'] = k[-1]
- tag['value'] = child.get('v')
- if len(k) == 1:
- tag['type'] = 'regular'
- elif len(k) == 2:
- tag['type'] = k[0]
- tags.append(tag)
- if child.tag == 'nd':
- nd = {'id' : way_attribs['id']}
- nd['node_id'] = child.get('ref')
- nd['position'] = counter
- way_nodes.append(nd)
- counter += 1
- return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement