def parse_color(self, color): hex_color = "[A-Fa-f0-9]" norm_color = comp("^#" + hex_color + "{6}$") trans_color_1 = comp("^\[(" + hex_color + "{2})\]#(" + hex_color + "{6})$") trans_color_2 = comp("^#" + hex_color + "{2}(" + hex_color + "{6})$") if (match := trans_color_1.match(color)): return '#' + match.group(1) + match.group( 2) if self.transparency else '#' + match.group(2)
def prepare_text(self): """ INPUT: A STRING ### OUTPUT: A LEMATIZED STRING WITHOUT NUMBERS, PUNCTUATIONS, AND STOP WORDS """ descriptions = [] descriptions.append(self.text) corpus = [self.normalize_text(s) for s in descriptions] descriptions = [' '.join(s) for s in corpus] new_desc = [] for t in descriptions: new_desc.append(' '.join([ word for word in t.split() if word not in stopwords.words("english") ])) new_desc2 = [sub(r'\b[0-9]+\b\s*', '', s) for s in new_desc] final = new_desc2[0] #final = "".join(l for l in final if l not in string.punctuation) regex = comp('[%s]' % escape(punctuation)) final = regex.sub(' ', final) p = set(printable) final = ''.join(filter(lambda x: x in p, final)) final = ' '.join([w for w in final.split() if len(w) > 1]) return final
def get_info(LOG_FLAG): """ Parse the configuration file. """ try: from idleoutconf import log, logsize, pid, host, port, domain from idleoutconf import group, name if LOG_FLAG != 1: logg(log, int(logsize)) # Don't open another logging instance! LOG_FLAG = 1 global smtp smtp = [host, int(port), domain] reg1 = comp('(\s+)\=(\s+)') for users in name: users = reg1.sub("=", users.strip()) usrtmp = users.split() usrname = usrtmp[0] rest = usrtmp[1:] usr_define(usrname, rest) for groups in group: groups = reg1.sub("=", groups.strip()) grtmp = groups.split() groupname = grtmp[0] rest = grtmp[1:] group_define(groupname, rest) return(pid) except Exception, err: print >> sys.stderr, "Error: %d: %s" % (err.errno, err.strerror) sys.exit(err.errno)
def match(self): """Used to get exploitable result of re.search""" # We initate this variable which gonna contain the returned data result = [] # We compile the regex string to_match = comp(self.regex) # In case we have to use the implementation of ${BASH_REMATCH} we use # re.findall otherwise, we use re.search if self.rematch: # pylint: disable=no-member pre_result = to_match.findall(self.data) else: pre_result = to_match.search(self.data) if self.return_data and pre_result is not None: # pylint: disable=no-member if self.rematch: # pylint: disable=no-member for data in pre_result: if isinstance(data, tuple): result.extend(list(data)) else: result.append(data) if self.group != 0: # pylint: disable=no-member return result[self.group] # pylint: disable=no-member else: result = pre_result.group( self.group).strip() # pylint: disable=no-member return result elif not self.return_data and pre_result is not None: # pylint: disable=no-member return True return False
def pega_eps(link_dict: dict, conteudo: bytes = b'') -> tuple or bool: """Pega os episodios da pagina e passa para pegar o link.""" try: if conteudo == b'': link_rede = link_dict['link'] requisicao_get = req_link(link_rede) if requisicao_get.status_code != 200 and requisicao_get.url != link_rede: return False requisicao_get = requisicao_get.content else: requisicao_get = conteudo req = parse_bs(requisicao_get) desc = req.find('div', {'itemprop': 'description'}) \ if req.find('div', {'itemprop': 'description'}) is not None \ else req.find(class_=comp("description")) des = desc.find() episodios = [] # pegar itens separar = des.prettify().replace('\n', '').split('<br/>') fila = [] for i in separar: parse_pag = parse_bs(i) if parse_pag.find('a') is not None: str_episodio = ' '.join( sanitizestring( parse_pag.text.replace('Assistir', '').replace( 'Dublado', '').replace('Legendado', '')).strip().split()) a_tag = parse_pag.find_all('a') if len(a_tag) == 1: str_link = tira_barra( BASE + a_tag[0].get('href').replace("%20", ' ').replace( 'redi/', '').replace('[', '').split()[0]) else: str_link = { k.text.strip(): tira_barra( BASE + k.get('href').replace("%20", ' ').replace( 'redi/', '').replace('[', '').split()[0]) for k in a_tag } ep = {str_episodio: str_link} episodios.append(ep) [ fila.append((episodios[i], link_dict, i)) for i in range(len(episodios)) ] li_ep = [None for i in range(len(episodios))] # print(fila) # [fila.put('Kill') for i in range(5)] env_alt(altera_link, fila, li_ep) # mypool = get_pool(5, altera_link, fila, li_ep) # [i.start() for i in mypool] # [i.join() for i in mypool] # pprint(link_tupla[0]) return (link_dict, episodios) except Exception as e: print(red, "pega_eps:", link_dict, '\n', e.__str__(), type(e), e.with_traceback(e.__traceback__), reset)
def match(self, regex=None, data_to_match=None): """ Used to get exploitable result of re.search Arguments: - data: str The data or a list of data to check. - regex: str The regex or a list or regex. Returns: list or bool - bool: if self.return_data is False - list: otherwise """ # We initate this variable which gonna contain the returned data result = [] if not regex: regex = self.regex if not data_to_match: data_to_match = self.data # We compile the regex string to_match = comp(regex) # In case we have to use the implementation of ${BASH_REMATCH} we use # re.findall otherwise, we use re.search if self.rematch: # pylint: disable=no-member pre_result = to_match.findall(data_to_match) else: pre_result = to_match.search(data_to_match) if self.return_data and pre_result is not None: # pylint: disable=no-member if self.rematch: # pylint: disable=no-member for data in pre_result: if isinstance(data, tuple): result.extend(list(data)) else: result.append(data) if self.group != 0: # pylint: disable=no-member return result[self.group] # pylint: disable=no-member else: result = pre_result.group( self.group # pylint: disable=no-member ).strip() return result elif not self.return_data and pre_result is not None: # pylint: disable=no-member return True return False
def matching_list(self): """ This method return a list of the string which match the given regex. """ pre_result = comp(self.regex) return list(filter(lambda element: pre_result.search(str(element)), self.data))
def _get_version(): """ This function will extract the version from PyFunceble/__init__.py """ to_match = comp(r'VERSION\s=\s"(.*)"\n') extracted = to_match.findall(open("PyFunceble/__init__.py").read())[0] return ".".join(list(filter(lambda x: x.isdigit(), extracted.split("."))))
def not_matching_list(self): """ This method return a list of string which don't match the given regex. """ to_match = comp(self.regex) return list(filterfalse(to_match.search, self.data))
def matching_list(self): """ Return a list of the string which match the given regex. """ pre_result = comp(self.regex) return [x for x in self.data if pre_result.search(str(x))]
def mapping_block_names_search(self, script): pattern = "\s*return [\"'](.*)[\"'];" regex = comp(pattern) results = [] for line in script.split("\n"): match = regex.match(line) if match: results.append(match.group(1)) return results
def get_version(): """ Extracts the version from adblock_decoder/__init__.py """ to_match = comp(r'__version__\s=\s"(.*)"') return to_match.findall( open("adblock_decoder/__init__.py", encoding="utf-8").read())[0]
def get_version(): """ Extracts the version from {NAMESPACE}/{MODULE}/__about__.py """ to_match = comp(r'__version__\s=\s"(.*)"') extracted = to_match.findall( open(f"{NAMESPACE}/{MODULE}/__about__.py", encoding="utf-8").read())[0] return ".".join(list(filter(lambda x: x.isdigit(), extracted.split("."))))
def _get_version(): """ This function will extract the version from a_john_shots/__init__.py """ to_match = comp(r'VERSION\s=\s"(.*)"\n') extracted = to_match.findall( open("a_john_shots/__init__.py", encoding="utf-8").read())[0] return ".".join([x for x in extracted.split(".") if x.isdigit()])
def _get_version(): """ Extract the version from ultimate_hosts_blacklist/comparison/__init__.py """ to_match = comp(r'VERSION\s=\s"(.*)"\n') extracted = to_match.findall( open("ultimate_hosts_blacklist/comparison/__init__.py", encoding="utf-8").read())[0] return ".".join(list(filter(lambda x: x.isdigit(), extracted.split("."))))
def not_matching_list(self): """ Return a list of string which don't match the given regex. """ pre_result = comp(self.regex) return list( filter(lambda element: not pre_result.search(str(element)), self.data))
def matching_list(self): """ This method return a list of the string which match the given regex. """ pre_result = comp(self.regex) return list( filter(lambda element: pre_result.search(str(element)), self.data) )
def build_status_element(status_string: str) -> List[Dict[str, str]]: status_list: List[Dict[str, str]] = [] regex = comp(r"^(?:\w*:(?:FAIL|PASS))(?:;(?:\w*:(?:FAIL|PASS)))*$") assert bool( regex.match(str(status_string)) ), f"Invalid status string {status_string}, must fit ^(?:\w*:(?:FAIL|PASS))(?:;(?:\w*:(?:FAIL|PASS)))*$" for status in status_string.split(";"): name: str state: str name, state = status.split(":") status_list.append({"name": name, "status": state}) return status_list
def extract(spattern): fdict = dict() pt = dict() cat = [] for name, value in spattern.items(): if name[0] == "_": value['tokens'] = spattern[name[2:]]['tokens'] pt.update({name: comp(value['regex'], MULTILINE)}) eachline = dict() replas = dict() for w in value['tokens']: if w in value: if 'eachline' in value[w]: eachline.update({w: value[w]['eachline']}) if 'replace' in value[w]: replas.update({ w: [(comp(q[0]), q[1]) if 1 < len(q) else (comp(q[0]), "") for q in value[w]['replace']] }) fdict.update({name: [eachline, replas]}) cat.append(value['tokens']) return fdict, pt, cat
def get_version(): """ This function will extract the version from domain2idna/__init__.py """ to_match = comp(r'VERSION\s=\s"(.*)"\n') try: extracted = to_match.findall( open("domain2idna/__init__.py", encoding="utf-8").read())[0] except FileNotFoundError: extracted = to_match.findall( open("../domain2idna/__init__.py", encoding="utf-8").read())[0] return ".".join([x for x in extracted.split(".") if x.isdigit()])
def _get_version(): """ This function will extract the version from PyFunceble/__init__.py """ to_match = comp(r'VERSION\s=\s"(.*)"\n') try: extracted = to_match.findall( open("PyFunceble/abstracts/package.py", encoding="utf-8").read())[0] except FileNotFoundError: # pragma: no cover extracted = to_match.findall( open("../PyFunceble/abstracts/package.py", encoding="utf-8").read())[0] return ".".join([x for x in extracted.split(".") if x.isdigit()])
def _get_version(full=False): """ This function will extract the version from PyFunceble/__init__.py Argument: - full: bool True: Return the full version name False: Return the short version """ to_match = comp(r'VERSION\s=\s"(.*)"\n') extracted = to_match.findall( open("../PyFunceble/__init__.py", encoding="utf-8").read())[0] if not full: return ".".join([x for x in extracted.split(".") if x.isdigit()]) return extracted
def match(self): """ Used to get exploitable result of re.search """ # We initate this variable which gonna contain the returned data result = [] # We compile the regex string to_match = comp(self.regex) # In case we have to use the implementation of ${BASH_REMATCH} we use # re.findall otherwise, we use re.search if self.rematch: # pylint: disable=no-member pre_result = to_match.findall(self.data) else: pre_result = to_match.search(self.data) if self.return_data and pre_result: # pylint: disable=no-member if self.rematch: # pylint: disable=no-member for data in pre_result: if isinstance(data, tuple): result.extend(list(data)) else: result.append(data) if self.group != 0: # pylint: disable=no-member return result[self.group] # pylint: disable=no-member else: result = pre_result.group( self.group # pylint: disable=no-member ).strip() return result if not self.return_data and pre_result: # pylint: disable=no-member return True return False
def match(self, data=None): """ Used to get exploitable result of re.search. :param data: The data we are working with. :type data: str """ if data is None: data = self.data # We compile the regex string to_match = comp(self.regex) # In case we have to use the implementation of ${BASH_REMATCH} we use # re.findall otherwise, we use re.search pre_result = to_match.search(data) if pre_result: if self.return_data: # pylint: disable=no-member return pre_result.group(self.group).strip() # pylint: disable=no-member return True return False
def get_exempt_urls(): """Fetches urls that do not require login""" exempt_urls = [comp(settings.LOGIN_URL.lstrip('/'))] if hasattr(settings, 'LOGIN_EXEMPT_URLS'): exempt_urls += [comp(expr) for expr in settings.LOGIN_EXEMPT_URLS] return exempt_urls
from os import listdir, getcwd, chdir from contextlib import contextmanager from re import compile as comp roteiros = 'roteiros' regex = comp(r'(#|#{2}|#{3}|#{4}) (\d\d?\.\d?\.?\d?) (.+)') @contextmanager def cd(_dir): old_dir = getcwd() chdir(_dir) yield chdir(old_dir) @cd('{}/roteiros'.format(getcwd(), roteiros)) def read_file(_file): with open(_file) as md: return regex.findall(md.read()) def order(val): for x in sorted(val): yield from x def write(_file, _dir): with open(_file, 'w') as md: for x in order([read_file(el) for el in listdir(_dir)]): md.writelines('#### {} {}\n'.format(x[1], x[2]))
from config import SPECIAL_UNITS from re import compile as comp # --------------- Helpers that build all of the responses ---------------------- tag = comp(r'<[^>]+>') def remove_tags(text): return tag.sub('', text) def build_speechlet_response(title, output, reprompt_text, should_end_session): return { 'outputSpeech': { 'type': 'SSML', 'ssml': output }, 'card': { 'type': 'Simple', 'title': title, 'content': remove_tags(output) }, 'reprompt': { 'outputSpeech': { 'type': 'SSML', 'ssml': reprompt_text } }, 'shouldEndSession': should_end_session }
def run(self): skip = False # try getting youtube video id or give error code try: url = str(self.ui.lineEdit.text()) regex = comp(r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?(?P<id>[A-Za-z0-9\-=_]{11})') match = regex.match(url) videoid = match.group('id') except: self.ui.outputBox.append("Video URL missing or incorrect.\n") skip = True # try getting video comments from API if not skip: try: coms = get_video_comments(service, videoId=videoid, part='id,snippet,replies') except: self.ui.outputBox.append("Could not find video or connection refused. Check your internet connection or video URL.\n") skip = True # count and display votes if not skip: name1 = str(self.ui.hashtag1In.text()).lower() name2 = str(self.ui.hashtag2In.text()).lower() hashtag1 = 0 hashtag2 = 0 if name1 == "": hashtag1 = -1 if name2 == "": hashtag2 = -1 for com in coms: if name1 in str(com).lower() and hashtag1 != -1: hashtag1 += 1 if name2 in str(com).lower() and hashtag2 != -1: hashtag2 += 1 if hashtag1 != -1: try: # avoid division by 0 percent1 = hashtag1 / (hashtag1 + hashtag2) * 100 except: percent1 = 0 else: percent1 = 0 hashtag1 = 0 name1 = "NONE" if hashtag2 != -1: try: # avoid division by 0 percent2 = hashtag2 / (hashtag1 + hashtag2) * 100 except: percent2 = 0 else: percent2 = 0 hashtag2 = 0 name2 = "NONE" #stop loading screen loading.stopAnimation() percent1 = "{:.2f}".format(percent1) percent2 = "{:.2f}".format(percent2) # display count results self.ui.outputBox.append(name1 + ": " + str(hashtag1) + " (" + str(percent1) + "%)" + "\n" + name2 + ": " + str(hashtag2) + " (" + str(percent2) + "%)" + "\n") # if got error wait 1sec then stop animation otherwise program crashes sleep(1) loading.stopAnimation() self.ui.pushButton.setEnabled(True) self.quit()
def numericalSort(value): ## Numerically orders files numbers = comp(r'(\d+)') parts = numbers.split(value) parts[1::2] = map(int, parts[1::2]) return parts
from config import SPECIAL_UNITS from re import compile as comp # --------------- Helpers that build all of the responses ---------------------- tag = comp(r'<[^>]+>') def remove_tags(text): return tag.sub('', text) def build_speechlet_response(title, output, reprompt_text, should_end_session): return { 'outputSpeech': { 'type': 'SSML', 'ssml': output }, 'card': { 'type': 'Simple', 'title': title, 'content':remove_tags(output) }, 'reprompt': { 'outputSpeech': { 'type': 'SSML', 'ssml': reprompt_text } }, 'shouldEndSession': should_end_session } def build_response(session_attributes, speechlet_response):
def clean_html(raw_html): """Remove tags from `raw_html`""" output = sub(comp('<.*?>'), '', raw_html).strip() output = sub(comp('\^.'), '', output) return output
def clean_html(raw_html): """Remove tags from `raw_html`""" return sub(comp('<.*?>'), '', raw_html).strip()