def __init__(self, record): tokens = re.split("\s+", record.strip()) self.data = {} if len(tokens) == self.AUTH_COLCOUNT: if not ( (re.fullmatch(self.AUTH_TAGNO_VALID_REGEX, tokens[self.AUTH_NDX_TAGNO]) != None) and (re.fullmatch(self.AUTH_SN_VALID_REGEX, tokens[self.AUTH_NDX_SN]) != None) and (re.fullmatch(self.AUTH_AUTHCODE_VALID_REGEX, tokens[self.AUTH_NDX_DP1_AUTH]) != None) ): raise AuthMalformedRecordExcept self.data[self.AUTH_KEY_TAGNO] = tokens[self.AUTH_NDX_TAGNO] self.data[self.AUTH_KEY_SN] = tokens[self.AUTH_NDX_SN] self.data[self.AUTH_KEY_CFG_AUTH] = tokens[self.AUTH_NDX_CFG_AUTH] self.data[self.AUTH_KEY_SFN_AUTH] = tokens[self.AUTH_NDX_SFN_AUTH] self.data[self.AUTH_KEY_DP1_AUTH] = tokens[self.AUTH_NDX_DP1_AUTH] self.data[self.AUTH_KEY_DP2_AUTH] = tokens[self.AUTH_NDX_DP2_AUTH] self.data[self.AUTH_KEY_DP3_AUTH] = tokens[self.AUTH_NDX_DP3_AUTH] self.data[self.AUTH_KEY_DP4_AUTH] = tokens[self.AUTH_NDX_DP4_AUTH] else: raise AuthBadColumnCountExcept
def spawn_missing_classes(self): """ Creates the appropriate python user relation classes from tables in the database and places them in the context. """ tables = [ row[0] for row in self.connection.query('SHOW TABLES in `%s`' % self.database) if lookup_class_name('`{db}`.`{tab}`'.format(db=self.database, tab=row[0]), self.context, 0) is None] master_classes = (Lookup, Manual, Imported, Computed) part_tables = [] for table_name in tables: class_name = to_camel_case(table_name) if class_name not in self.context: try: cls = next(cls for cls in master_classes if re.fullmatch(cls.tier_regexp, table_name)) except StopIteration: if re.fullmatch(Part.tier_regexp, table_name): part_tables.append(table_name) else: # declare and decorate master relation classes self.context[class_name] = self(type(class_name, (cls,), dict())) # attach parts to masters for table_name in part_tables: groups = re.fullmatch(Part.tier_regexp, table_name).groupdict() class_name = to_camel_case(groups['part']) try: master_class = self.context[to_camel_case(groups['master'])] except KeyError: raise DataJointError('The table %s does not follow DataJoint naming conventions' % table_name) part_class = type(class_name, (Part,), dict(definition=...)) part_class._master = master_class self.process_relation_class(part_class, context=self.context, assert_declared=True) setattr(master_class, class_name, part_class)
def get_url(args): term = args.term if not args.search: if term.startswith('myanimelist.net'): term = "http://" + term if re.fullmatch(r"\d+", term): term = "http://myanimelist.net/anime/{}".format(term) if not re.fullmatch(r"http://myanimelist.net/anime/\d+/?(/.*)?", term): print("invaild url {}".format(term)) sys.exit(2) return term # search all # search_url = "http://myanimelist.net/search/all" # page = requests.get(search_url, params={"q": term}) # soup = BeautifulSoup(page.content, "html.parser") # anime_url = soup.select_one('article > div').select_one('a.hoverinfo_trigger')['href'] search_url = "http://myanimelist.net/anime.php" page = requests.get(search_url, params={"q": term}) soup = BeautifulSoup(page.content, "html.parser") anime_url = soup.select_one('a.hoverinfo_trigger')['href'] print(" ", anime_url) return anime_url
def read_spatiocyte_input(filename): with open(filename, 'r') as f: header = f.readline().rstrip().split(',') if len(header) < 5: raise RuntimeError('The file given [{}] was invalid: "{}"'.format(filename, header)) interval, lz, ly, lx, voxel_radius = [float(_) for _ in header[: 5]] species_info = header[5: ] lengths = numpy.array([lx, ly, lz], dtype=numpy.float64) * 2 * voxel_radius # meter species_ids = {} for sp in species_info: # ex) [/Cell/Membrane:S1][0]=2e-08 mobj = re.fullmatch(r'\[(.*)\]\[(\d+)\]=(.*)', sp) if mobj is None: warnings.warn('An invalid species info [{}] was given. Check [{}]'.format(sp, filename)) continue sp_id = mobj.group(1) sp_idx = int(mobj.group(2)) radius = float(mobj.group(3)) if re.fullmatch(r'(\/|(\/[^\/]+)+):([^\/:]+)', sp_id) is None: warnings.warn('A species ID is invalid [{}]. Ignored'.format(sp)) continue _log.info(" Species [{}] => ID={}, Radius={}".format(sp_idx, sp_id, radius)) species_ids[sp_idx] = (sp_id, radius) _log.info(' Time Interval = {} sec'.format(interval)) _log.info(' Voxel radius = {} m'.format(voxel_radius)) _log.info(' Compartment lengths: {} nm x {} nm x {} nm'.format(lengths[0], lengths[1], lengths[2])) _log.info(' Species IDs: {}'.format(', '.join([sp_id for sp_id, _ in species_ids.values()]))) return dict(interval=interval, lengths=lengths, voxel_radius=voxel_radius, species_ids=species_ids)
def handle_request(self, data=None): """Handles both POST and GET reqs. In case of GET there's no data. It also extracts data from the url path (regex groups) and passes it to the appropriate end-handler func. """ thread_name = threading.current_thread().name print(thread_name, self.raw_requestline) # resolve request path to end-handler function # (url) unquote the request path so that eventual unicode codes (%<code>) are converted back to unicode chars delegations = [(re.fullmatch(url_pattern, unquote(self.path)), action) for url_pattern, action in BackOfficeReqHandler.REQUEST_HANDLERS.items() if re.fullmatch(url_pattern, unquote(self.path)) is not None] # for an existing request path there should be exactly one handler func. if len(delegations) == 1: delegate = delegations[0] args = self, if data is not None: # if there is POST data args = args + (data,) for group in delegate[0].groups(): # if there are more args to be extracted from the request url (e.g. user, month, year) args = args + (group,) try: return delegate[1](*args) # call the appropriate handler func finally: self.wfile.flush() else: # error: page doesn't exist self.send_response(404) self.end_headers() self.wfile.write(str.encode("The requested page {page} is not found!".format(page=self.path), 'utf-8')) self.wfile.flush() return
def select(self, station=None, channel=None, location=None, component=None): # A new Stream object is returned but the traces it contains are just aliases to the traces of the original stream. # Does not copy the data but only passes a reference. # all matching done using regular expressions (module re) traces = [] for trace in self: # skip trace if any given criterion is not matched if station is not None: station = station.upper() if not re.fullmatch(station, trace.station): continue if location is not None: if not re.fullmatch(location, trace.location): continue if channel is not None: channel = channel.upper() if not re.fullmatch(channel, trace.channel): continue if component is not None: component = component.upper() if len(trace.channel) < 3: continue if not (trace.channel[-1] == component): continue traces.append(trace) return self.__class__(traces=traces)
def _last_rid_from_results(self): r = -1 try: day_folders = os.listdir(self.results_dir) except: return r day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x), day_folders) for df in day_folders: day_path = os.path.join(self.results_dir, df) try: minute_folders = os.listdir(day_path) except: continue minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x), minute_folders) for mf in minute_folders: minute_path = os.path.join(day_path, mf) try: h5files = os.listdir(minute_path) except: continue for x in h5files: m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x) if m is None: continue rid = int(m.group(1)) if rid > r: r = rid return r
def isemail(str): if str == "": return 0 else: if re.match(r"[a-zA-Z_0-9$?&]+@", str): str1, str2 = str.split("@", 1) # str1 is before '@', str2 is after '@' if re.fullmatch(r"[a-zA-Z0-9]+.[a-zA-Z0-9]+", str2) or re.fullmatch( r"[a-zA-Z0-9]+.[a-zA-Z0-9]+.[a-zA-Z0-9]+", str2 ): domainList = str2.split(".") domainValidation = [ map( lambda x: 1 if len(x) <= 63 and x[0].isalpha() and (x[-1].isalpha() or x[-1].isdigit()) else 0, domainList, ) ] if domainValidation.count(0) == 0: return 1 else: return 0 else: return 0 else: return 0
def helper_cassini_valid_obs_name(obs_name): """Check a Cassini observation name to see if it is parsable. Such a name will have four parts separated by _: <PRIME> _ <REVNO> <TARGETCODE> _ <ACTIVITYNAME> <ACTIVITYNUMBER> _ <INST> or, in the case of VIMS (sometimes): <PRIME> _ <REVNO> <TARGETCODE> _ <ACTIVITYNAME> <ACTIVITYNUMBER> <PRIME> can be: ([A-Z]{2,5}|22NAV) - '18ISS', '22NAV', 'CIRS', 'IOSIC', 'IOSIU', 'IOSIV', 'ISS', 'NAV', 'UVIS', 'VIMS' - If <INST> is 'PRIME' or 'PIE' then <PRIME> can only be one of: '22NAV', 'CIRS', 'ISS', 'NAV', 'UVIS', 'VIMS' <REVNO> can be: ([0-2]\d\d|00[A-C]|C\d\d) - 000 to 299 - 00A to 00C - C00 to C99 <TARGETCODE> can be: [A-Z]{2} - See _CASSINI_TARGET_CODE_MAPPING <ACTIVITYNAME> can be: [0-9A-Z]+ - Everything except the final three digits <ACTIVITYNUMBER> can be: \d\d\d - Last final three digits <INST> can be one of: [A-Z]{2,7} - 'PRIME', 'PIE' (prime inst is in <PRIME>) - 'CAPS', 'CDA', 'CIRS', 'INMS', 'ISS', 'MAG', 'MIMI', 'NAV', 'RADAR', 'RPWS', 'RSS', 'SI', 'UVIS', 'VIMS' - Even though these aren't instruments, it can also be: 'AACS' (reaction wheel assembly), 'ENGR' (engineering), 'IOPS', 'MP' (mission planning), 'RIDER', 'SP', 'TRIGGER' If <INST> is missing but everything else is OK, we assume it's PRIME """ if obs_name is None: return False ret = re.fullmatch( '([A-Z]{2,5}|22NAV)_([0-2]\d\d|00[A-C]|C\d\d)[A-Z]{2}_[0-9A-Z]+\d\d\d_[A-Z]{2,7}', obs_name) if ret: return True # Try without _INST ret = re.fullmatch( '([A-Z]{2,5}|22NAV)_([0-2]\d\d|00[A-C]|C\d\d)[A-Z]{2}_[0-9A-Z]+\d\d\d', obs_name) if ret: return True return False
def get_last_rid(): r = -1 try: day_folders = os.listdir("results") except: return r day_folders = filter(lambda x: re.fullmatch('\d\d\d\d-\d\d-\d\d', x), day_folders) for df in day_folders: day_path = os.path.join("results", df) try: minute_folders = os.listdir(day_path) except: continue minute_folders = filter(lambda x: re.fullmatch('\d\d-\d\d', x), minute_folders) for mf in minute_folders: minute_path = os.path.join(day_path, mf) try: h5files = os.listdir(minute_path) except: continue for x in h5files: m = re.fullmatch('(\d\d\d\d\d\d\d\d\d)-.*\.h5', x) rid = int(m.group(1)) if rid > r: r = rid return r
def inputConsole(inputVerseFilePath): consoleInput = input('Please write one or several verse numbers separated by a comma to see their variance in Goethes Faust') #do stuff with consoleInput inputLineList = [] fileList=[] #delete either the global variable fileList or the local one with open (inputVerseFilePath,'r', encoding='utf-8') as f: verseFilePathDict = json.load(f, encoding='utf-8') if (re.fullmatch('\d{1,6}', consoleInput)): if not (verseFilePathDict.get(consoleInput)): print('Can not find line ' + consoleInput) else: fileList = verseFilePathDict[consoleInput] inputLineList.append(consoleInput) else: inputTempList = re.split('\d{1,6}', consoleInput) for inputTemp in inputTempList: if (re.fullmatch('\d{1,6}', inputTemp)): if not (verseFilePathDict.get(consoleInput)): print('Can not find line ' + consoleInput) else: fileList.append(verseFilePathDict[inputTemp]) inputLineList.append(inputTemp) parseXML(fileList,inputLineList)
def detect_language(title): """ Detect language of a given title. The matching is case-sensitive and spaces are treated the same way as underscores. :param title: page title to work with :returns: a ``(pure, lang)`` tuple, where ``pure`` is the pure page title without the language suffix and ``lang`` is the detected language in long, localized form """ title_regex = r"(?P<pure>.*?)[ _]\((?P<lang>[^\(\)]+)\)" pure_suffix = "" # matches "Page name/Subpage (Česky)" match = re.fullmatch(title_regex, title) # matches "Page name (Česky)/Subpage" if not match and "/" in title: base, pure_suffix = title.split("/", maxsplit=1) pure_suffix = "/" + pure_suffix match = re.fullmatch(title_regex, base) # matches "Category:Česky" if not match: match = re.fullmatch(r"(?P<pure>[Cc]ategory[ _]?\:[ _]?(?P<lang>[^\(\)]+))", title) if match: lang = match.group("lang") if lang in get_language_names(): return match.group("pure") + pure_suffix, lang return title, get_local_language()
def match_date(arg, data): op, rest = _get_comparison_function(arg.lower(), keepspaces=True) yearrx = re.fullmatch(r'(19|20)?(\d\d)', rest.strip()) monthyearrx = re.fullmatch(r'(\w+)\s*(\d{4})', rest.strip()) currentyear = date.today().year if yearrx: century, tens = yearrx.groups() if yearrx.group(1) is None: century = '19' if int('20'+tens) > currentyear else '20' year = int(century + tens) return op(data.year, year) elif monthyearrx: monthname, year = monthyearrx.groups() try: month = _monthabbrs.index(monthname)+1 except ValueError: raise SyntaxError('Invalid month') return op(data.year*12+data.month, int(year)*12+month) else: try: fulldate = datetime.strptime(rest.strip(), '%Y-%m-%d').date() except ValueError: raise SyntaxError('Invalid date match expression') else: return op(data, fulldate)
def _last_rid_from_results(self): r = -1 try: day_folders = os.listdir(self.results_dir) except: return r day_folders = filter( lambda x: re.fullmatch("\\d\\d\\d\\d-\\d\\d-\\d\\d", x), day_folders) for df in day_folders: day_path = os.path.join(self.results_dir, df) try: hm_folders = os.listdir(day_path) except: continue hm_folders = filter(lambda x: re.fullmatch("\\d\\d(-\\d\\d)?", x), hm_folders) for hmf in hm_folders: hm_path = os.path.join(day_path, hmf) try: h5files = os.listdir(hm_path) except: continue for x in h5files: m = re.fullmatch( "(\\d\\d\\d\\d\\d\\d\\d\\d\\d)-.*\\.h5", x) if m is None: continue rid = int(m.group(1)) if rid > r: r = rid return r
def __call__(self, stream, meta): match = re.fullmatch(r'\|\|(.+)', stream.peek()) if not match: return False, None dl = nodes.Container('dl') while stream.has_next(): match = re.fullmatch(r'\|\|(.+)', stream.peek()) if match: stream.next() term = match.group(1).strip('|').strip() dt = nodes.Leaf('dt').append(nodes.Text(term)) dl.append(dt) definition = utils.LineStream() while stream.has_next(): if re.fullmatch(r'\|\|(.+)', stream.peek()): break elif stream.peek().startswith(' ') or stream.peek() == '': definition.append(stream.next()) else: break dd = nodes.Container('dd') dd.children = ContainerParser().parse(definition.dedent(), meta) dl.append(dd) else: break return True, dl
def db_fix(fingerprint): # add colons to fingerprints bihex = "[0-9A-Fa-f]{2}" if bool(re.fullmatch("(" + bihex + "[:]){0,}" + bihex, fingerprint)): return fingerprint elif bool(re.fullmatch("(" + bihex + "){1,}", fingerprint)): return ":".join(chop(fingerprint, 2)) else: assert False, "Error: fingerprint is invalid"
def __call__(self, stream, meta): match = re.fullmatch(r'[ ]{0,3}([*•+-])([ ].+)?', stream.peek()) if not match: return False, None # A new marker means a new list. marker = match.group(1) re_header = r'[ ]{0,3}[%s]([ ].+)?' % marker # Read in each individual list item as a new LineStream instance. items = [] while stream.has_next(): match = re.fullmatch(re_header, stream.peek()) if match: stream.next() item = utils.LineStream() items.append(item) # Read in any indented content. while stream.has_next(): if re.fullmatch(re_header, stream.peek()): break elif stream.peek().startswith(' ') or stream.peek() == '': item.append(stream.next()) else: break # We need to dedent the content before prepending the header. item.dedent() # Prepend the content of the header line. if match.group(1): item.prepend(match.group(1).strip()) else: break # Determine if we're dealing with a block or compact-style list. if len(items) > 1: if items[0].has_trailing_blank(): listtype = 'block' else: listtype = 'compact' else: listtype = 'block' # Assemble the node tree representing the list. ul = nodes.Container('ul') for item in items: item = item.trim() li = nodes.Container('li') if listtype == 'block': children = ContainerParser().parse(item, meta) else: parsers = (ULParser(), OLParser(), TextParser()) children = Parser(parsers).parse(item, meta) li.children = children ul.append(li) return True, ul
def is_realizable(test): spec_status = stripped_non_empty(readfile(test).splitlines())[-1] if re.fullmatch('-- *realizable', spec_status): return True if re.fullmatch('-- *unrealizable', spec_status): return False assert 0, 'spec status is unknown'
def make_token(part): if isinstance(part, Token): return part elif re.fullmatch("[0-9]*", part): # tokenize integers return Token("integer", part) elif re.fullmatch("[a-zA-Z_][a-zA-Z0-9_]*", part): # tokenize valid names return Token("name", part) else: # we've found something unexpected! complain. raise TokenException(part)
def _parseHeader(self): """ https://en.wikipedia.org/wiki/FASTQ_format """ self.instrument = '' self.runID = '' self.flowcellID = '' self.lane = '' self.pair = '' # Strip header string h = self._header.strip() # Pre CASAVA 1.8 # @HWUSI-EAS100R:6:73:941:1973#0/1 pattern = '(\@.*?):(\d+):\d+:\d+:\d+\#(.*)\/(\d)' if re.fullmatch(pattern, h): m = re.match(pattern, h) self.instrument = m.groups()[0] self.lane = m.groups()[1] self.pair = m.groups()[3] return # CASAVA 1.8 # @EAS139:136:FC706VJ:2:2104:15343:197393 1:Y:18:ATCACG pattern = '(\@.*?):(\d+):(.*?):(\d+):\d+:\d+:\d+ (\d):[Y,N]:\d+:(.*)' if re.fullmatch(pattern, h): m = re.match(pattern, h) self.instrument = m.groups()[0] self.runID = m.groups()[1] self.flowcellID = m.groups()[2] self.lane = m.groups()[3] self.pair = m.groups()[4] return # CASAVA 1.8 Truncated # @EAS139:136:FC706VJ:2:2104:15343:197393 pattern = '(\@.*?):(\d+):(.*?):(\d+):\d+:\d+:\d+' if re.fullmatch(pattern, h): m = re.match(pattern, h) self.instrument = m.groups()[0] self.runID = m.groups()[1] self.flowcellID = m.groups()[2] self.lane = m.groups()[3] return # Probably SRA # @SRR001666.1 071112_SLXA-EAS1_s_7:5:1:817:345 length=36 #TODO: look at a bunch of SRAs and figure out pattern pattern = '(\@SRR.*?).*' if re.match(pattern, h): self.note.append("WARNINING: SRA header parsing is not implemented.") return self.note.append("ERROR: Header does not fit known patterns.") return
def should_override(key): title_match = re.match(r'[ekdcb]_((?!_adj($|_)).)*', key) if title_match is not None: title = title_match.group() return (title in titles and title[0] != 'b' and re.fullmatch(r'c_((?!_adj($|_)).)*', key) is None) if key in keys_to_override: return True noble_match = re.fullmatch(noble_regex, key) return noble_match is not None
def _parse_raw(line): tokens = line.replace(';', ' ').split() cards = [token for token in tokens if re.fullmatch(CARD_RE, token, re.IGNORECASE)] joined = ''.join(cards) cards = list(zip(joined[::2], joined[1::2])) params = [token for token in tokens if re.fullmatch(NUM_RE, token)] pots = [float(param) for param in params if '.' in param] player_nums = [int(param) for param in params if '.' not in param] return cards, player_nums, pots
def __init__(self, f): self.title = '' self.subtitle = '' self.author = '' self.style = Style() self.note = '' self.sections = [] self.contents = [] config = parse_file(f) directory = '.' if 'title' in config: self.title = config.pop('title') else: raise ContentError("Songbook attribute 'title' not specified.", f.name) if 'subtitle' in config: self.subtitle = config.pop('subtitle') if 'author' in config: self.author = config.pop('author') if 'directory' in config: directory = config.pop('directory') if 'style' in config: style_filename = config.pop('style') fl = open(style_filename, encoding='utf-8') self.style = Style(fl) fl.close() if 'note' in config: self.note = config.pop('note') if 'contents' in config: contents = config.pop('contents').split(sep='\n') for line in contents: cmd = line.strip() if cmd == '': continue R = re.fullmatch('section\s(?P<file>.+)', cmd) if R != None: filename = R.group('file').strip() fl = open(directory + os.sep + filename, encoding='utf-8') s = Section(fl) fl.close() self.sections.append(s) self.contents.append(s) continue R = re.fullmatch('table\s+of\s+contents\s+with\s+(?P<map>.+)', cmd) if R != None: s = TableOfContents(self, eval(R.group('map'))) self.contents.append(s) continue R = re.fullmatch('title index\s+with\s+(?P<map>.+)', cmd) if R != None: s = TitleIndex(self, eval(R.group('map'))) self.contents.append(s) continue # and so on... if len(config) > 0: print("Unused labels in "+f.name+": " + str(config.keys()), file=sys.stderr)
def read_entries(fname: Path): import re with open(fname) as f: for line in f.read().split('\n'): m = re.fullmatch(r'\s*.*<(.*\.rst)>', line) if m: yield m.group(1) m = re.fullmatch(r'\s*(.*\.rst)', line) if m: yield m.group(1)
def collect(self): path = self.args['path'].replace('"', '\\"') if 'hidden' in self.args and self.args['hidden']: cmd = 'ls -1A --color=never --full-time "' + path + '"' else: cmd = 'ls -1 --color=never --full-time "' + path + '"' return_code, out_lines, err_lines = self.host.exec_command(cmd) if return_code != 0: raise FileNotFoundError('Unable to collect the contents of ' + self.args['path']) entries = [] for l in out_lines: if re.fullmatch(r'\w+\s+[0-9]+', l): # total line continue #2011-11-08 18:02:08.954092000 -0700 m = re.fullmatch(r'([-a-z])([-sStTrwx]{3})([-sStTrwx]{3})([-rwx]{3})(\.)?\s+([0-9]+)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+:[0-9.]+\s[-+][0-9]+)\s+(.*)( -> (.*))?', l) if m: mtime = m.group(10) logger.debug('original mtime: ' + str(mtime)) # an unfortunate circumstance of the mtime %f not always being 6 or less chars mtime = m.group(10).partition('.') msecs = mtime[2] mtime = mtime[0] + '.' msecs = msecs.partition(' ') mtime = mtime + msecs[0][:5] + ' ' + msecs[2] logger.debug('reduced precision mtime: ' + str(mtime)) mtime = datetime.datetime.strptime(mtime, '%Y-%m-%d %H:%M:%S.%f %z') logger.debug('parsed mtime: ' + str(mtime)) entry = { 'type': DirectoryContentsCollector.TYPE_MAP[m.group(1)], 'user_mode': m.group(2), 'group_mode': m.group(3), 'other_mode': m.group(4), 'link_count': m.group(6), 'owner': m.group(7), 'group_owner': m.group(8), 'size': int(m.group(9)), 'modified_raw': m.group(10), 'modified': mtime, 'name': m.group(11), } if m.group(5) is not None: entry['has_security_context'] = True if m.group(12) is not None: entry['link_target'] = m.group(12) entries.append(entry) else: raise ValueError('Unable to parse line from ls: ' + l) return entries
def search(cls,query=None): if query is None:return cls.find() if fullmatch('1[3578]\d{1,9}',query): return cls.find({'mobile':'/^%s/'%(query)}) elif fullmatch('9\d{3}',query): return cls.find({'telephone':'/%s$/'%(query)}) elif fullmatch('[a-zA-Z]{1,}',query): return cls.find({'email':'/%s/'%(query)}) elif fullmatch('\d{1,}',query): return cls.find({'telephone':'/%s/'%(query)}) else: return cls.find({'name':'/%s/'%(query)})
def __iter__(self): for elem in self._parent.children: if self._TAG_REGEX: if not re.fullmatch(self._TAG_REGEX, elem.tag): continue # skip this element, it doesn't match tag_regex matches = lambda x, y, rx, ry: re.fullmatch(rx, x) and re.fullmatch(ry, y) for regK, regV in self._ATTRIB_REGEX.items(): match = [k for k, v in elem.items() if matches(k, v, regK, regV)] if not match: break # skip element that can't match one of our attribs else: # only get here if we didn't break attrib matching (always works if no attrib_regex) yield elem # yield element only if it matches tag and attrib regex
def check_phone_number(number): back_number = number[-8:] space_or_dash = number[-9] front_number = number[:-9] if not re.fullmatch(r'^[0-9]{3}\-[0-9]{4}$', back_number): return None if space_or_dash == " ": return re.fullmatch(r'^\([0-9]{3}\)$', front_number) elif space_or_dash == "-": return re.fullmatch(r'^[0-9]{3}$', front_number) else: return None
def tag_nonword(self, string): size = str(len(string)) if re.fullmatch('\d+', string): category = 'number' elif re.fullmatch('[^a-zA-Z0-9]+', string): category = 'special' elif re.fullmatch('[a-zA-z]+', string): category = 'char' else: category = 'mixed' return category + size
def __init__(self, text: str, start_location: Location, end_location: Location): super().__init__(text, start_location, end_location) m = re.fullmatch(self._regex1, self._clean_text) if m is None: m = re.fullmatch(self._regex2, self._clean_text) if m is None: raise SyntacticalError(self, 'Malformed {}'.format(self.short_name())) self._value = float(m.group(1)) suffix = m.group(2) if suffix is not None: self._kind = FloatKind(suffix) else: self._kind = FloatKind.NONE
def has_output_format(out_str): # example: "A1f, 1, 3\n B33m, 4" m = re.fullmatch(r"(\s*[A-Z][0-9]+[fm]\s*,\s*[0-9]+\s*\n)*\s*[A-Z][0-9]+[fm]\s*,\s*[0-9]+\s*\n?", out_str) return m is not None
def test_run(self): """ Verify that discover creates the appropriate catalog, schema, metadata, etc. • Verify number of actual streams discovered match expected • Verify the stream names discovered were what we expect • Verify stream names follow naming convention streams should only have lowercase alphas and underscores • verify there is only 1 top level breadcrumb • verify replication key(s) • verify primary key(s) • verify that if there is a replication key we are doing INCREMENTAL otherwise FULL • verify the actual replication matches our expected replication method • verify that primary, replication and foreign keys are given the inclusion of automatic (metadata and annotated schema). • verify that all other fields have inclusion of available (metadata and schema) """ conn_id = self.create_connection() # Verify number of actual streams discovered match expected found_catalogs = menagerie.get_catalogs(conn_id) self.assertGreater( len(found_catalogs), 0, msg="unable to locate schemas for connection {}".format(conn_id)) self.assertEqual( len(found_catalogs), len(self.expected_streams()), msg="Expected {} streams, actual was {} for connection {}," " actual {}".format(len(self.expected_streams()), len(found_catalogs), found_catalogs, conn_id)) # Verify the stream names discovered were what we expect found_catalog_names = {c['tap_stream_id'] for c in found_catalogs} self.assertEqual(set(self.expected_streams()), set(found_catalog_names), msg="Expected streams don't match actual streams") # Verify stream names follow naming convention # streams should only have lowercase alphas and underscores self.assertTrue(all( [re.fullmatch(r"[a-z_]+", name) for name in found_catalog_names]), msg="One or more streams don't follow standard naming") for stream in self.expected_streams(): with self.subTest(stream=stream): catalog = next( iter([ catalog for catalog in found_catalogs if catalog["stream_name"] == stream ])) assert catalog # based on previous tests this should always be found schema_and_metadata = menagerie.get_annotated_schema( conn_id, catalog['stream_id']) metadata = schema_and_metadata["metadata"] schema = schema_and_metadata["annotated-schema"] # verify the stream level properties are as expected # verify there is only 1 top level breadcrumb stream_properties = [ item for item in metadata if item.get("breadcrumb") == [] ] self.assertTrue( len(stream_properties) == 1, msg="There is more than one top level breadcrumb") # verify replication key(s) actual = set(stream_properties[0].get("metadata", { self.REPLICATION_KEYS: [] }).get(self.REPLICATION_KEYS) or []) expected = self.expected_replication_keys()[stream] or set() self.assertEqual( actual, expected, msg="expected replication key {} but actual is {}".format( expected, actual)) # verify primary key(s) self.assertEqual( set(stream_properties[0].get("metadata", { self.PRIMARY_KEYS: [] }).get(self.PRIMARY_KEYS, [])), self.expected_primary_keys()[stream], msg="expected primary key {} but actual is {}".format( self.expected_primary_keys()[stream], set(stream_properties[0].get("metadata", { self.PRIMARY_KEYS: None }).get(self.PRIMARY_KEYS, [])))) # verify that if there is a replication key we are doing INCREMENTAL otherwise FULL actual_replication_method = stream_properties[0].get( "metadata", { self.REPLICATION_METHOD: None }).get(self.REPLICATION_METHOD) # invoice_line_items has no bookmark, but is an incremental child of invoices if stream == 'invoice_line_items' or stream_properties[0].get( "metadata", { self.REPLICATION_KEYS: [] }).get(self.REPLICATION_KEYS, []): self.assertTrue( actual_replication_method == self.INCREMENTAL, msg="Expected INCREMENTAL replication " "since there is a replication key") else: self.assertTrue(actual_replication_method == self.FULL, msg="Expected FULL replication " "since there is no replication key") # verify the actual replication matches our expected replication method self.assertEqual( self.expected_replication_method().get(stream, None), actual_replication_method, msg= "The actual replication method {} doesn't match the expected {}" .format( actual_replication_method, self.expected_replication_method().get(stream, None))) expected_automatic_fields = self.expected_automatic_fields( )[stream] or set() # verify that primary, replication and foreign keys # are given the inclusion of automatic in annotated schema. actual_automatic_fields = { key for key, value in schema["properties"].items() if value.get("inclusion") == "automatic" } self.assertEqual( expected_automatic_fields, actual_automatic_fields, msg="expected {} automatic fields but got {}".format( expected_automatic_fields, actual_automatic_fields)) # verify that all other fields have inclusion of available # This assumes there are no unsupported fields for SaaS sources self.assertTrue( all({ value.get("inclusion") == "available" for key, value in schema["properties"].items() if key not in actual_automatic_fields }), msg= "Not all non key properties are set to available in annotated schema" ) # verify that primary, replication and foreign keys # are given the inclusion of automatic in metadata. actual_automatic_fields = \ {item.get("breadcrumb", ["properties", None])[1] for item in metadata if item.get("metadata").get("inclusion") == "automatic"} self.assertEqual( expected_automatic_fields, actual_automatic_fields, msg="expected {} automatic fields but got {}".format( expected_automatic_fields, actual_automatic_fields)) # verify that all other fields have inclusion of available # This assumes there are no unsupported fields for SaaS sources self.assertTrue( all({ item.get("metadata").get("inclusion") == "available" for item in metadata if item.get("breadcrumb", []) != [] and item.get("breadcrumb", ["properties", None])[1] not in actual_automatic_fields }), msg= "Not all non key properties are set to available in metadata" )
def is_number(mystr): #regex to match if number is exact match match = re.fullmatch('-?\d{3,}(\.[0-9]{0,})?', mystr) return match is not None
import re password = input('Enter a password to check:\n') if re.fullmatch(r'[A-Za-z0-9@#$%^&+=]{8,}', password): print('Yay! you have a secure password!') else: print('Your password needs your attention!')
def __validate_email(self, email: str): if not re.fullmatch(r"[^@]+@[^@]+\.[^@]+", email): raise NotAValidEmailError(email)
def expect_regex(self, *expected_patterns): messages = self._pop_messages() for pattern in expected_patterns: assert any( re.fullmatch(pattern, message, flags=re.DOTALL) for message in messages)
def _extract_info_from_section_title(section, pattern_results): # Look for the first pattern in `pattern_results` # that matches (via re.fullmatch) `section.section_title`. for (pattern, result) in pattern_results: pattern = pattern.replace('<PARAMETER_LIST>', r'\((?P<params_str>[^()]*)\)') mo = re.fullmatch(pattern, section.section_title) if mo: break else: assert 0, section.section_title assert isinstance(result, str) section.section_kind = result # "ste" = section-title extractions section.ste = mo.groupdict() # --------------------------------- if 'params_str' in section.ste: parameter_listing = section.ste['params_str'].strip() if parameter_listing == '. . .': assert section.section_kind == 'function_property' # _xref # Doesn't mean anything, might as wel not be there. del section.ste['params_str'] else: params_info = OrderedDict() if parameter_listing != '': if parameter_listing == '_value1_, _value2_, ..._values_': # Math.{hypot,max,min} parameter_listing = '..._values_' elif parameter_listing in [ '_p1_, _p2_, ..., _pn_, _body_', # old '_p1_, _p2_, … , _pn_, _body_' # new ]: # Function, GeneratorFunction, AsyncGeneratorFunction, AsyncFunction parameter_listing = '..._args_ [ , _body_ ]' param_strs = parameter_listing.split(', ') subsequent_are_optional = False for param_str in param_strs: if param_str.startswith('[ '): subsequent_are_optional = True param_str = param_str[2:] mo = re.match(r'^(\.\.\.)?(_\w+_)(.*)$', param_str) assert mo, section.section_title (opt_dots, param_name, rest) = mo.groups() assert param_name not in params_info assert not (opt_dots and subsequent_are_optional) if opt_dots: param_punct = '...' elif subsequent_are_optional: param_punct = '[]' else: param_punct = '' params_info[param_name] = param_punct if re.match(r'^( \])*$', rest): pass elif rest == ' [ ': subsequent_are_optional = True else: assert 0, (section.section_title, repr(param_str)) section.ste['parameters'] = params_info
def is_number(s): return re.fullmatch("[0-9]+(\.[0-9]+)?", s)
def clean_nickname(self): nickname = self.cleaned_data['nickname'].strip() if nickname: if not re.fullmatch(r'[\w.@_-]+', nickname): raise ValidationError('昵称包含不可识别字符', code='invalid') return nickname
#min length8 and max length 15 (no nos:) import re data=input("enter the data:") rule="^[A-Z][a-z]+$" matcher=re.fullmatch(rule,data) if matcher is not None: print("valid") else: print("invalid")
def load_tf_weights_in_openai_gpt(model, config, openai_checkpoint_folder_path): """Load tf pre-trained weights in a pytorch model (from NumPy arrays here)""" import re import numpy as np if ".ckpt" in openai_checkpoint_folder_path: openai_checkpoint_folder_path = os.path.dirname(openai_checkpoint_folder_path) logger.info("Loading weights from {}".format(openai_checkpoint_folder_path)) with open(openai_checkpoint_folder_path + "/parameters_names.json", "r", encoding="utf-8") as names_handle: names = json.load(names_handle) with open(openai_checkpoint_folder_path + "/params_shapes.json", "r", encoding="utf-8") as shapes_handle: shapes = json.load(shapes_handle) offsets = np.cumsum([np.prod(shape) for shape in shapes]) init_params = [np.load(openai_checkpoint_folder_path + "/params_{}.npy".format(n)) for n in range(10)] init_params = np.split(np.concatenate(init_params, 0), offsets)[:-1] init_params = [param.reshape(shape) for param, shape in zip(init_params, shapes)] # This was used when we had a single embedding matrix for positions and tokens # init_params[0] = np.concatenate([init_params[1], init_params[0]], 0) # del init_params[1] init_params = [arr.squeeze() for arr in init_params] try: assert model.tokens_embed.weight.shape == init_params[1].shape assert model.positions_embed.weight.shape == init_params[0].shape except AssertionError as e: e.args += (model.tokens_embed.weight.shape, init_params[1].shape) e.args += (model.positions_embed.weight.shape, init_params[0].shape) raise model.tokens_embed.weight.data = torch.from_numpy(init_params[1]) model.positions_embed.weight.data = torch.from_numpy(init_params[0]) names.pop(0) # Pop position and token embedding arrays init_params.pop(0) init_params.pop(0) for name, array in zip(names, init_params): # names[1:n_transfer], init_params[1:n_transfer]): name = name[6:] # skip "model/" assert name[-2:] == ":0" name = name[:-2] name = name.split("/") pointer = model for m_name in name: if re.fullmatch(r"[A-Za-z]+\d+", m_name): scope_names = re.split(r"(\d+)", m_name) else: scope_names = [m_name] if scope_names[0] == "g": pointer = getattr(pointer, "weight") elif scope_names[0] == "b": pointer = getattr(pointer, "bias") elif scope_names[0] == "w": pointer = getattr(pointer, "weight") else: pointer = getattr(pointer, scope_names[0]) if len(scope_names) >= 2: num = int(scope_names[1]) pointer = pointer[num] try: assert ( pointer.shape == array.shape ), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" except AssertionError as e: e.args += (pointer.shape, array.shape) raise try: assert ( pointer.shape == array.shape ), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched" except AssertionError as e: e.args += (pointer.shape, array.shape) raise logger.info("Initialize PyTorch weight {}".format(name)) pointer.data = torch.from_numpy(array) return model
def run(unfurl, node): def parse_protobuf_into_nodes(pb_value_dict, pb_types, edge_type=None): assert isinstance(pb_value_dict, dict), \ f'"parse_protobuf_into_nodes" expects a dict, but got {type(pb_value_dict)} as input' if len(pb_value_dict) > 0: for field_number, field_value in pb_value_dict.items(): if isinstance(field_value, (str, int, float, bytes, bytearray)): unfurl.add_to_queue( data_type='proto', key=field_number, value=str(field_value), hover=f'Field number <b>{field_number}</b> has a wire ' f'type of {wire_types[pb_types[field_number]["type"]]}', parent_id=node.node_id, incoming_edge_config=edge_type) elif isinstance(field_value, dict): unfurl.add_to_queue( data_type='proto.dict', key=field_number, value={'field_values': field_value, 'field_types': pb_types[field_number]["message_typedef"]}, label=f'{field_number}: {field_value}', hover=f'Field number <b>{field_number}</b> ' f'is a nested message; it will be parsed further into more nodes', parent_id=node.node_id, incoming_edge_config=edge_type) elif isinstance(field_value, list): nested_types = pb_types[field_number] if pb_types[field_number].get("message_typedef"): nested_types = pb_types[field_number]["message_typedef"] unfurl.add_to_queue( data_type='proto.list', key=field_number, value={'field_values': field_value, 'field_types': nested_types}, label=f'{field_number}: {field_value}', hover=f'Field number <b>{field_number}</b> ' f'is a nested message; it will be parsed further into more nodes', parent_id=node.node_id, incoming_edge_config=edge_type) if node.data_type == 'proto.dict': parse_protobuf_into_nodes(node.value.get('field_values'), node.value.get('field_types'), proto_edge) return if node.data_type == 'proto.list': for field_value in node.value['field_values']: field_types = node.value.get('field_types') if not isinstance(field_value, dict): field_value = {node.key: field_value} field_types = {node.key: field_types} parse_protobuf_into_nodes(field_value, field_types, proto_edge) return if node.data_type == 'bytes': try: protobuf_values, protobuf_values_types = blackboxprotobuf.decode_message(node.value) parse_protobuf_into_nodes(protobuf_values, protobuf_values_types, proto_edge) return # This will often fail for a wide array of reasons when it tries to parse a non-pb as a pb except Exception: pass if not isinstance(node.value, str): return False if len(node.value) % 4 == 1: # A valid b64 string will not be this length return False if node.data_type.startswith(('uuid', 'hash')): return False urlsafe_b64_m = re.fullmatch(r'[A-Za-z0-9_=\-]{16,}', node.value) standard_b64_m = re.fullmatch(r'[A-Za-z0-9+/=]{16,}', node.value) hex_m = re.fullmatch(r'([A-Fa-f0-9]{2} ?){8,}', node.value) long_int = re.fullmatch(r'\d{16,}', node.value) if hex_m and not long_int: decoded = bytes.fromhex(node.value) try: protobuf_values, protobuf_values_types = blackboxprotobuf.decode_message(decoded) parse_protobuf_into_nodes(protobuf_values, protobuf_values_types, hex_proto_edge) return # This will often fail for a wide array of reasons when it tries to parse a non-pb as a pb except Exception: return elif urlsafe_b64_m and not long_int: try: decoded = base64.urlsafe_b64decode(unfurl.add_b64_padding(node.value)) protobuf_values, protobuf_values_types = blackboxprotobuf.decode_message(decoded) parse_protobuf_into_nodes(protobuf_values, protobuf_values_types, b64_proto_edge) return # This will often fail for a wide array of reasons when it tries to parse a non-pb as a pb except Exception: return elif standard_b64_m and not long_int: try: decoded = base64.b64decode(unfurl.add_b64_padding(node.value)) protobuf_values, protobuf_values_types = blackboxprotobuf.decode_message(decoded) parse_protobuf_into_nodes(protobuf_values, protobuf_values_types, b64_proto_edge) return # This will often fail for a wide array of reasons when it tries to parse a non-pb as a pb except Exception: return
def parse(self, specs): """Transform list of dependencies (strings) to list of Dependency. https://docs.microsoft.com/en-us/nuget/create-packages/dependency-versions#version-ranges :param specs: list of dependencies (strings) :return: list of Dependency """ deps = [] for spec in specs: name, version_range = spec.split(' ', 1) # 1.0 -> 1.0≤x if re.search('[,()\[\]]', version_range) is None: dep = Dependency(name, [('>=', version_range)]) # [1.0,2.0] -> 1.0≤x≤2.0 elif re.fullmatch(r'\[(.+),(.+)\]', version_range): m = re.fullmatch(r'\[(.+),(.+)\]', version_range) dep = Dependency(name, [[('>=', m.group(1)), ('<=', m.group(2))]]) # (1.0,2.0) -> 1.0<x<2.0 elif re.fullmatch(r'\((.+),(.+)\)', version_range): m = re.fullmatch(r'\((.+),(.+)\)', version_range) dep = Dependency(name, [[('>', m.group(1)), ('<', m.group(2))]]) # The following one is not in specification, # so we can just guess what was the intention. # Seen in NLog:5.0.0-beta08 dependencies # [1.0, ) -> 1.0≤x elif re.fullmatch(r'\[(.+), \)', version_range): m = re.fullmatch(r'\[(.+), \)', version_range) dep = Dependency(name, [('>=', m.group(1))]) # [1.0,2.0) -> 1.0≤x<2.0 elif re.fullmatch(r'\[(.+),(.+)\)', version_range): m = re.fullmatch(r'\[(.+),(.+)\)', version_range) dep = Dependency(name, [[('>=', m.group(1)), ('<', m.group(2))]]) # (1.0,) -> 1.0<x elif re.fullmatch(r'\((.+),\)', version_range): m = re.fullmatch(r'\((.+),\)', version_range) dep = Dependency(name, [('>', m.group(1))]) # [1.0] -> x==1.0 elif re.fullmatch(r'\[(.+)\]', version_range): m = re.fullmatch(r'\[(.+)\]', version_range) dep = Dependency(name, [('==', m.group(1))]) # (,1.0] -> x≤1.0 elif re.fullmatch(r'\(,(.+)\]', version_range): m = re.fullmatch(r'\(,(.+)\]', version_range) dep = Dependency(name, [('<=', m.group(1))]) # (,1.0) -> x<1.0 elif re.fullmatch(r'\(,(.+)\)', version_range): m = re.fullmatch(r'\(,(.+)\)', version_range) dep = Dependency(name, [('<', m.group(1))]) elif re.fullmatch(r'\((.+)\)', version_range): raise ValueError("invalid version range %r" % version_range) deps.append(dep) return deps
import time import re def check_my_date(date): try: time.strptime(date, '%d/%m/%Y') # strtime - конвертирующая функция # %d/%m/%Y - задаем формат времни описателями строк # %d - день 1-31 # %m - месяц 1-12 # %Y - год в четырехзначном формате YYYY # Выбрал этот вариант ввиду того, что он простой и короткий # Как вариант иожно было написать код с конструкциями if/elif/else проверяя каждое значение отдельно # Для дней это прверка на тип(инта), 1-31, 28-29 для февраля будет зависеть от проверки высокосный год или нет # Для месяца тип(инта)б 1-12 # Для года тип, 1-inf print(True) except ValueError: print(False) date = input('Enter your value at dd/mm/yyyy format ') #date = '32/01/2020' match = re.fullmatch(r'\d\d/\d\d/\d{4}', date) if not match: raise ValueError('Incorrect value') check_my_date(date)
def solve(N): return re.fullmatch(r'[+-]?\d*\.\d+', N) is not None
input("PRESS ENTER TO START") os.system('cls') # Create socket called clientSocket and establish a TCP connection with mailServer print(f'{COLOR["YELLOW"]}CREATING CONNECTION SOCKET...{COLOR["END"]}') clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.settimeout(1) clientSocketSLL = ssl.wrap_socket(clientSocket) # Choose a mail server (e.g. Google mail server) and call it mailServer while True: mailServer = 'smtp.gmail.com' port = 465 input_server = input(f'{COLOR["YELLOW"]}\nMAIL SERVER [press enter to use default]: {COLOR["END"]}') if input_server != '': verify = re.fullmatch(r'^(([a-zA-Z0-9\-]*)(\.)){2,}([a-zA-Z]{2,3})$', input_server) if verify is None: print(f'{COLOR["RED"]}\nERROR: INVALID MAIL SERVER{COLOR["END"]}') continue else: mailServer = input_server.lower() while True: input_port = input(f'{COLOR["YELLOW"]}\nPORT [press enter to use default]: {COLOR["END"]}') if input_port != '': verify = re.fullmatch(r'^\d{3,4}$', input_port) if verify is None: print(f'{COLOR["RED"]}\nERROR: INVALID PORT NUMBER{COLOR["END"]}') continue else: port = int(input_port) break
def test_username_custom_template(self, _person): result = _person.username(template='d-U.l_d') assert re.fullmatch(r'[0-9]+\-[A-Z][a-z]+\.[a-z]+\_[0-9]+', result)
def is_valid_shop_name(shop_name): return re.fullmatch(r"[A-Za-z0-9]*", shop_name)
# 9. Write a Python program to find the sequences of one upper # case letter followed by lower case letters? #============================================================== import re s = input("enter a word:") x = '[A-Z]{1}[a-z]' match = re.fullmatch(x, s) if match is not None: print("yes") else: print("no")
def branch_matches(self, branch): for b in self.branches(): if re.fullmatch(b, branch): return True return False
def is_alphanumeric(self, in_string): return re.fullmatch(r'^[A-Za-z0-9]+$', in_string)
def _version(version_: str): match = re.fullmatch(regex.Regex.VERSION, version_) if (not match): raise RuntimeError("invalid version") return version(*match.groups())
def is_integer(mystr): match = re.fullmatch('-?\d+', mystr) return match is not None
def signUp(self): # 회원 정보 입력 while True: info = input("회원 정보를 입력해 주세요 : ") if not len(info) >= 16: print("잘못된 회원정보입니다.") continue if not info[-15].isspace(): print("잘못된 회원정보입니다.") continue name = "" iden_num = info[-14:] for i in range(-16, -(len(info) + 1), -1): if info[i].isprintable() and not info[i].isspace(): name = info[0:i + 1] break # 이름 문법 규칙 : 길이 >=1, 첫/마지막은 실상 문자, 탭/개행 X if not (len(name) >= 1 and (name[0].isprintable() and not name[0].isspace() and name[-1].isprintable() and not name[-1].isspace()) and not any(ch in name for ch in [u'\u0009', u'\u000A', u'\u000D'])): print("잘못된 이름입니다. 다시 입력해주세요") continue # 주민등록번호 문법 규칙 : <6자리 생년월일><표준 공백><1,2,3,4 중 1개><6자리 숫자> # 14글자가 아니거나, 숫자와 표준 공백 외 다른 문자가 있을 경우 if not len(iden_num) == 14 or not iden_num[6] == ' ': print("잘못된 주민등록번호 형식입니다.") continue for ch in iden_num[0:6] + iden_num[7:15]: if ch not in map(lambda ch: str(ch), range(0, 10)): print("잘못된 주민등록번호 형식입니다.") continue # 문법 규칙1 : 세대 generation = 0 if iden_num[7] in ['1', '2']: generation = 19 elif iden_num[7] in ['3', '4']: generation = 20 else: print("잘못된 출생 세대입니다.") continue # 문법 규칙2 : 출생 연도(1900~2002) if generation == 19: if not (iden_num[0:2] in map( lambda m: '0' + str(m) if m < 10 else str(m), range(0, 99))): print("잘못된 출생연도입니다.") continue elif generation == 20: if not iden_num[0:2] in ["00", "01", "02"]: print("잘못된 출생연도입니다.") continue birth_year = iden_num[0:2] # 문법 규칙3 : 월(01~12) if iden_num[2] == '0': if not (iden_num[3] in map(lambda m: str(m), range(1, 10))): print("잘못된 출생월입니다.") continue elif iden_num[2] == '1': if not (iden_num[3] in map(lambda m: str(m), range(0, 3))): print("잘못된 출생월입니다.") continue else: print("잘못된 출생월입니다.") continue birth_month = iden_num[2:4] # 문법 규칙4 : 일(각 월의 일 수 + 2월 윤년 처리) year = generation + int(birth_year) isLeap = (year % 4 == 0 and (year % 100 != 0) or (year % 400 == 0)) birth_day = iden_num[4:6] if birth_month in ["01", "03", "05", "07", "08", "10", "12"]: if not (birth_day in map( lambda d: '0' + str(d) if d < 10 else str(d), range(1, 32))): print("잘못된 출생일입니다.") continue elif birth_month in ["04", "06", "09", "11"]: if not (birth_day in map( lambda d: '0' + str(d) if d < 10 else str(d), range(1, 31))): print("잘못된 출생일입니다.") continue elif birth_month == "02": if isLeap: if not (birth_day in map( lambda d: '0' + str(d) if d < 10 else str(d), range(1, 30))): print("잘못된 출생일입니다.") continue else: if not (birth_day in map( lambda d: '0' + str(d) if d < 10 else str(d), range(1, 29))): print("잘못된 출생일입니다.") continue # 여기까지 왔으면, 주민등록번호 문법 규칙은 통과 # 회원 관리 파일 해당 회원 찾기 file = open(UserManager.user_file, 'r', encoding='ANSI') info = file.readlines() file.close() find_index = -1 for index, user in enumerate(info): # 회원 정보를 담고 있는 행에서만 검색 if index % 4 == 0: user = user.rstrip() f_name, f_iden_num = user.split('\t', 1) # 회원일 경우 if f_name == name and f_iden_num == iden_num: find_index = index break # 이미 회원인 경우 if not find_index == -1: # 제대로 된 입력을 받을 때까지 반복 while (True): ans = input("이미 가입된 회원이십니다. 계정 찾기를 원하십니까?(y/n) : ") if ans == "y": account_info = info[find_index + 1].rstrip() account_id, account_pw = account_info.split(' ') account_id = account_id[0:len(account_id) - 2] + "***" account_pw = account_pw[0:len(account_pw) - 2] + "***" # 특정 인덱스를 가린 계정 출력 print(f"ID : {account_id}\n비밀번호 : {account_pw}") # 메서드 종료 return elif ans == "n": # 회원 정보 입력으로 돌아가기 break else: print("정확히 y 혹은 n으로만 입력해주시기 바랍니다.") # 신규 회원인 경우 else: # 주민등록번호 의미 규칙 : 다른 회원들의 주민등록번호와 중복되지 않아야 함 # 회원관리파일에서 모든 주민등록번호 불러오기 find_index = -1 for index, user in enumerate(info): # 회원 정보를 담고 있는 행에서만 검색 if index % 4 == 0: user = user.rstrip() f_iden_num = user.split('\t', 1)[1] # 중복되는 주민등록번호인지 확인 if f_iden_num == iden_num: find_index = index break if find_index != -1: print("이미 등록된 주민등록번호입니다.") continue # 여기까지 왔으면, 진짜 신규 회원인 것으로 확인 print("신규 회원이십니다! 계정 생성을 위해 ID와 비밀번호를 생성해주세요") ID = "" PW = "" # ID 문법 규칙 while (True): ID = input("ID : ") r1 = True r2 = True r3 = True if not (len(ID) >= 5 and len(ID) <= 20): r1 = False if not (re.fullmatch(UserManager.ID_match1, ID) or re.fullmatch(UserManager.ID_match2, ID) or re.fullmatch(UserManager.ID_match3, ID) or re.fullmatch(UserManager.ID_match4, ID) or re.fullmatch(UserManager.ID_match5, ID) or re.fullmatch(UserManager.ID_match6, ID) or re.fullmatch(UserManager.ID_match7, ID)): r2 = False if not ID[0] not in ['-', '_']: r3 = False # 문법 규칙 틀렸을 경우 안내 if not (r1 == True and r2 == True and r3 == True): numbering = 1 print("사용할 수 없는 ID입니다. 아래의 사항을 확인해주세요.") if r1 == False: print(f"{numbering}. 5~20자리로 입력해주세요.") numbering += 1 if r2 == False: print( f"{numbering}. 영문 소문자 및 숫자, 특수 기호(_),(-)로만 구성해주세요." ) numbering += 1 if r3 == False: print(f"{numbering}. 첫 문자로 특수 기호가 올 수 없습니다") continue # 문법 규칙 맞았을 경우, 의미 규칙 확인 else: find_index = -1 for index, user_account in enumerate(info): # ID를 담는 행에서만 검색 if index % 4 == 1: user_id = user_account.rstrip() user_id = user_id.split(' ', 1)[0] # ID가 중복인지 확인 if ID == user_id: find_index = index break # ID가 중복일 경우 if not find_index == -1: print("이미 사용중인 ID입니다") continue # 중복이 아닌 경우 else: break # 비밀번호 문법 규칙 while (True): PW = input("비밀번호 : ") r1 = True r2 = True if not (len(PW) >= 8 and len(PW) <= 20): r1 = False if not (re.fullmatch(UserManager.PW_match1, PW) or re.fullmatch(UserManager.PW_match2, PW) or re.fullmatch(UserManager.PW_match3, PW) or re.fullmatch(UserManager.PW_match4, PW) or re.fullmatch(UserManager.PW_match5, PW)): r2 = False # 문법 규칙 틀렸을 경우 안내 if not (r1 == True and r2 == True): numbering = 1 print("사용할 수 없는 비밀번호입니다. 아래의 사항을 확인해주세요.") if r1 == False: print(f"{numbering}. 8~20자리로 입력해주세요.") numbering += 1 if r2 == False: print( f"{numbering}. 영문 대문자, 소문자, 숫자, 특수문자 중 3종류 이상의 조합으로 구성해주세요." ) continue # 문법 규칙 맞았을 경우, 의미 규칙 확인 else: if not len(set(ID) & set(PW)) / len(set(ID) | set(PW)) <= 0.4: print("ID와 너무 비슷합니다.") continue else: break ######## 계정 생성 완료 ######## balance = 0 while True: try: balance = int( input( "계정 생성이 완료되었습니다. 기본 개인 가계부의 시작 잔고를 입력해주세요 : ")) if balance < 0: print("잔고는 0원보다 적을 수 없습니다.") continue else: break except: print("유효한 금액(숫자)로 입력해주세요.") continue # 계좌번호 생성 과정 account_num_list = [] for index, account_num in enumerate(info): # 계좌번호를 담고 있는 행에서만 검색 if index % 4 == 2: account_num = account_num.rstrip() account_nums = account_num.split(' ') account_num_list += account_nums # 계좌번호는 6자리 랜덤 숫자 new_account_num = random.randint(100000, 999999) # 기존 계좌번호와 중복되지 않게하기 while new_account_num in account_num_list: new_account_num = random.randint(100000, 999999) ######## 계좌파일 만들기 + 회원정보파일에 해당 회원 정보/계정/계좌번호 기록 ######## f = open(UserManager.user_file, 'a', encoding='ANSI') f.write(name + "\t" + iden_num + "\n") f.write(ID + " " + PW + "\n") f.write(str(new_account_num) + "\n\n") f.close() f = open(UserManager.account_folder + f"\\{new_account_num}.txt", 'w', encoding='ANSI') f.write(f"1{name}({ID})" + "\n\n") f.write("음식(까페/식사/간식) 공부(책/인강/필기구) 수입(알바/용돈) 선물(반지)" + "\n\n") now = datetime.datetime.now().strftime('%Y.%m.%d') f.write(f"[계좌 생성] +{balance} {now} {balance}") f.close() ######## 생성된 계좌 파일 무결성 확인 ######## return
import re PATTERN = r"^https://github\.com/(?P<user>[A-Za-z0-9-]+)/(?P<repo>[A-Za-z_-]+)/" \ r"commit/(?P<hash>[0-9A-Fa-f]{40}),(?P<message>[^\n]+),(?P<additions>[0-9]+),(?P<deletions>[0-9]+)$" users = {} while True: input_line = input() if input_line == "git push": break match = re.fullmatch(PATTERN, input_line) if not match: continue username = match.group("user") repo = match.group("repo") commit_hash = match.group("hash") message = match.group("message") additions = match.group("additions") deletions = match.group("deletions") if username not in users: users[username] = {} if repo not in users[username]: users[username][repo] = [] commit = {
def has_input_format(in_str): # example: "A1f, B2m \nB3f" m = re.fullmatch(r"(((\s*[A-Z][0-9]+[fm]\s*,)*\s*[A-Z][0-9]+[fm]\s*\n)|(\s*(b|i)\s*\n))*(((\s*[A-Z][0-9]+[fm]\s*,)*\s*[A-Z][0-9]+[fm]\s*\n?)|(\s*(b|i)\s*\n?))", in_str) return m is not None
def consolidate_inputs(screenshot): #tesseract data (when ok) has 12 columns #the important stuff is the four coordinates and the value itself #coordinates are given as (left, top, width, height) screenshot = screenshot.split("\n")[1:] filtered_data = [] #only meaningful data from tesseract #in rare cases a number may be split. we try to concatenate its parts #each array element will contain all parts that belong to an input #e.g. "-" and "10.0" inside the area of the same input filtered_lines_per_input = [[], [], [], [], [], [], []] for line in screenshot: columns = line.split("\t") if len(columns) < 12: #this means tesseract could not read correctly #only angle rates can be estimated precisely #but i usually do not need these estimations #so, if a sensor is unreliable, i keep previous value return if (columns[11] == "text"): #first line (only labels) continue if (columns[10] == "-1"): #line without meaningful data (confidence = -1) continue #all remaining data is part of a possibly valid line #columns 6-9 are the coordinates #column 11 is the value filtered_data.append(columns[6:]) #in the next for, i append all info for each desired input for i in range(0, len(filtered_lines_per_input)): for j in range(0, len(filtered_data)): #first, check if data belongs to one of the inputs i want #it may be one of the angle rates, or even garbage box = (int(filtered_data[j][0]), int(filtered_data[j][1]), int(filtered_data[j][2]), int(filtered_data[j][3])) #looking for concatenated data (all fit in the same box) if box_inside_box(box, inputs_positions[i]): filtered_lines_per_input[i].append(j) #now, we can look at filtered_lines_per_input #and try to assemble the actual input new_input = "" #finally, concatenating (usually only one line) for j in range(0, len(filtered_lines_per_input[i])): new_input += filtered_data[filtered_lines_per_input[i][j]][5] #removing all unit symbols and whitespace new_input = new_input.replace("°", "").replace("m", "") new_input = new_input.replace("/", "").replace("s", "") new_input = new_input.replace(" ", "") #good case: a healthy number if re.fullmatch("-{0,1}[0-9]+\.[0-9]", new_input): new_input = float(new_input) #a missing dot can be replaced #in rate, we have 3 decimal digits #in the others, we have one elif re.fullmatch("-{0,1}[0-9]+[0-9]", new_input): if i == 6: new_input = float(new_input) * 0.001 else: new_input = float(new_input) * 0.1 #bad case: we cannot recognize a number else: new_input = current_values[i] current_values[i] = new_input
def login(self): ID = "" correspond_PW = "" # ID 문법 규칙 while True: ID = input("ID : ") if (len(ID) >= 5 and len(ID) <= 20) and ( re.fullmatch(UserManager.ID_match1, ID) or re.fullmatch(UserManager.ID_match2, ID) or re.fullmatch(UserManager.ID_match3, ID) or re.fullmatch(UserManager.ID_match4, ID) or re.fullmatch(UserManager.ID_match5, ID) or re.fullmatch(UserManager.ID_match6, ID) or re.fullmatch(UserManager.ID_match7, ID)) and ID[0] not in ['-', '_']: # 문법 규칙 맞았을 경우, 해당 아이디가 유효한지 확인 file = open(UserManager.user_file, 'r', encoding='ANSI') info = file.readlines() file.close() find_index = -1 for index, user_account in enumerate(info): # ID를 담는 행에서만 검색 if index % 4 == 1: user_id = user_account.rstrip() user_id = user_id.split(' ', 1)[0] if ID == user_id: find_index = index correspond_PW = user_account.rstrip() correspond_PW = correspond_PW.split(' ', 1)[1] break # ID가 유효할 경우 if not find_index == -1: break else: print("ID를 다시 확인해주세요") continue else: print("ID를 다시 확인해주세요") continue # 비밀번호 문법 규칙 while True: PW = input("비밀번호 : ") # 비밀번호 문법 규칙 if (len(PW) >= 8 and len(PW) <= 20) and ( re.fullmatch(UserManager.PW_match1, PW) or re.fullmatch(UserManager.PW_match2, PW) or re.fullmatch(UserManager.PW_match3, PW) or re.fullmatch(UserManager.PW_match4, PW) or re.fullmatch(UserManager.PW_match5, PW)): # ID에 해당하는 비밀번호인가? if PW == correspond_PW: ########## 로그인 완료 ########## print("로그인 완료") ####### 파일 무결성 체크2 ####### return ID # 일단 로그인 완료한 ID를 반환함으로써 메소드가 종료된다. else: print("비밀번호가 올바르지 않습니다.") continue else: print("비밀번호를 다시 확인해주세요") continue
def is_path_prop(self, p): return fullmatch(r"[a-z_]+_path", p) is not None
def validateEmailAddressFor(cls, anEmailAddress): if not re.fullmatch(r"[^@]+@[^@]+\.[^@]+", anEmailAddress): logger.info('Invalid email address ' + anEmailAddress) raise InstanceCreationFailed('Invalid email address') else: return anEmailAddress