Here are the examples of the python api re. taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
5 Examples
2
Example 1
View licensedef deg2rad(self, d): exp1 = re.compile('^-?[0-9]{,3}(º|ᵒ)[0-9]{,3}'[0-9]{,3}([']{2}|")$') exp2 = re.compile('^-?[0-9]{,3}.[0-9]{,6}(º|ᵒ)$') if(not exp1.match(d) and not exp2.match(d)): return None elif(exp1.match(d)): d = d.replace('º','.').replace("''",'.').replace("'",'.') d_dic = d.split('.') d_deg = float(d_dic[0]) d_min = float(d_dic[1]) d_sec = float(d_dic[2]) if(d_deg < 0): d_min = 0 - d_min; d_sec = 0 - d_sec; d_ndeg = (d_deg+(d_min/60)+(d_sec/(60**2))) else: d_ndeg = float(d.replace('º','')) if(d_ndeg < 0): d_ndeg = 360 - abs(d_ndeg); res = round((d_ndeg * math.pi) / 180, 6) if(res < 0): return '%f' % res; else: return '+%f' % res; # DºM'S'' # From radians to degrees: (rad * 180)/pi def rad2deg(self, rad): exp = re.compile('^(-?)[0-9]{1}.[0-9]{4,8}') if(not exp.match(rad)): return None r = float(rad) if(r < 0): r = (2 * math.pi) - abs(r) ndeg = (r * 180) / math.pi deg = math.floor(float(ndeg)) nmins = (ndeg - deg) * 60 mins = math.floor(nmins) secs = round( (nmins - mins) * 60 ) return "%dº%d'%d''" % (deg, mins, secs)
2
Example 2
View licensedef deg2rad(self, d): exp1 = re.compile('^-?[0-9]{,3}(º|ᵒ)[0-9]{,3}'[0-9]{,3}([']{2}|")$') exp2 = re.compile('^-?[0-9]{,3}.[0-9]{,6}(º|ᵒ)$') if(not exp1.match(d) and not exp2.match(d)): return None elif(exp1.match(d)): d = d.replace('º','.').replace("''",'.').replace("'",'.') d_dic = d.split('.') d_deg = float(d_dic[0]) d_min = float(d_dic[1]) d_sec = float(d_dic[2]) if(d_deg < 0): d_min = 0 - d_min; d_sec = 0 - d_sec; d_ndeg = (d_deg+(d_min/60)+(d_sec/(60**2))) else: d_ndeg = float(d.replace('º','')) if(d_ndeg < 0): d_ndeg = 360 - abs(d_ndeg); res = round((d_ndeg * math.pi) / 180, 6) if(res < 0): return '%f' % res; else: return '+%f' % res; # DºM'S'' # From radians to degrees: (rad * 180)/pi def rad2deg(self, rad): exp = re.compile('^(-?)[0-9]{1}.[0-9]{4,8}') if(not exp.match(rad)): return None r = float(rad) if(r < 0): r = (2 * math.pi) - abs(r) ndeg = (r * 180) / math.pi deg = math.floor(float(ndeg)) nmins = (ndeg - deg) * 60 mins = math.floor(nmins) secs = round( (nmins - mins) * 60 ) return "%dº%d'%d''" % (deg, mins, secs)
0
Example 3
View licensedef was_modified_since(header=None, mtime=0, size=0): """ Was something modified since the user last downloaded it? header This is the value of the If-Modified-Since header. If this is None, I'll just return True. mtime This is the modification time of the item we're talking about. size This is the size of the item we're talking about. """ try: if header is None: raise ValueError matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, 在 header 中用正则表达式抽取 last modified since 和 length re.IGNORECASE) header_mtime = parse_http_date(matches.group(1)) header_len = matches.group(3) if header_len and int(header_len) != size: raise ValueError if int(mtime) > header_mtime: raise ValueError except (AttributeError, ValueError, OverflowError): return True 奇怪的处理方式 return False
0
Example 4
View licensedef parse_script_groups(content, hand_tests): groups = {"0": []} cur_group = "0" test_id = 0 any = False for i in filter(None, content.splitlines()): match = re.search(rb"<#-- *group *(\d)* *-->", i) if not match: t = i.split(b'>')[-1].strip() if t == b'$': test_id += 1 while test_id in hand_tests: test_id += 1 else: test_id = int(t) assert test_id not in hand_tests groups[cur_group].append(test_id) continue cur_group = match.groups(0)[0] groups[cur_group] = [] any = True if not any: return None return groups
0
Example 5
View licensedef parse_script_groups(content, hand_tests): groups = {"0": []} cur_group = "0" test_id = 0 any = False for i in filter(None, content.splitlines()): match = re.search(rb"<#-- *group *(\d)* *-->", i) if not match: t = i.split(b'>')[-1].strip() if t == b'$': test_id += 1 while test_id in hand_tests: test_id += 1 else: test_id = int(t) assert test_id not in hand_tests groups[cur_group].append(test_id) continue cur_group = match.groups(0)[0] groups[cur_group] = [] any = True if not any: return None return groups