def get_youtube_link(search_query): # Get youtube url for search term say("Trying to find " + search_query, True) # Say trying to find and search_query query_string = urllib.parse.urlencode({"search_query": search_query}) # Url encode search_query html_content = urllib.request.urlopen( "http://www.youtube.com/results?" + query_string) # Get html content from search results search_results = refindall( r'href=\"\/watch\?v=(.{11})', html_content.read().decode()) # Get video urls from search results print("http://www.youtube.com/watch?v=" + search_results[0]) # Print url of video say("Found it") # Say "Found it" return ("http://www.youtube.com/watch?v=" + search_results[0]) # Return url of video
def string_to_dict(text: str, rtn_on_fail=None) -> dict: if type(text) != str: return rtn_on_fail key_value = refindall("[a-zA-Z 0-9\-\:\_]+", text) if not key_value: return rtn_on_fail else: key_value = list( map(lambda x: list(map(lambda y: y.strip(), x.split(":"))), key_value)) return dict(key_value)
def replacing(what_to_replace: str, for_what: str, full_string: str) -> str: try: upper_list = [ True if letter.isupper() else False for letter in refindall( what_to_replace, full_string, flags=reIGNORECASE)[0] ] except IndexError: return "False" if all(upper_list): return resub(what_to_replace, for_what.upper(), full_string, flags=reIGNORECASE) replaced_word = ''.join(letter.upper() if is_upper else letter.lower() for letter, is_upper in zip_longest( for_what, upper_list, fillvalue=False)) return resub(what_to_replace, replaced_word, full_string, flags=reIGNORECASE)
def download(data_, PASTA_): """ Download de PDFs do sítio da Imprensa Nacional. Alvo: DOU 1, 2, 3, Extra e Suplementar completos. ** NOTA: Não faz o download de arquivos já existentes. ** NOTA: DOU Extra e Suplementar somente serão baixados no dia seguinte à sua data de publicação. Versão do Python: 3.6 Pacotes nativos utilizados: os | locale | urllib | datetime | calendar | re Pacotes não nativos necessários: 1. BeautifulSoup 4 | Instalação: pip install beautifulsoup To do: Receber argumento data para download Receber argumento da pasta de destino do download """ import os from locale import setlocale from locale import LC_TIME from urllib.request import urlopen from datetime import date from datetime import timedelta from calendar import day_abbr from time import sleep from random import random from re import findall as refindall from bs4 import BeautifulSoup def check_dou_dirs(dou_path_, day_, month_, year_): """ Checagem/Criação da estrutura de diretórios dos downloads. """ import os if not os.path.exists(dou_path_): os.makedirs(dou_path_) dou_path_ = os.path.join(dou_path_, year_) if not os.path.exists(dou_path_): os.makedirs(dou_path_) dou_path_ = os.path.join(dou_path_, month_) if not os.path.exists(dou_path_): os.makedirs(dou_path_) dou_path_ = os.path.join(dou_path_, day_) if not os.path.exists(dou_path_): os.makedirs(dou_path_) return dou_path_ def ult_dia_semana(dia_, mes_, ano_): """ Retorna o primeiro dia anterior ao input que é dia de semana. Tipo do retorno: <class 'datatime.date'> """ from datetime import date from datetime import timedelta day_before = 1 tday = date(int(ano_), int(mes_), int(dia_)) while True: yday = tday - timedelta(day_before) yday_abbr = yday.strftime("%a") if yday_abbr not in WEEKDAYS: day_before += 1 else: return yday # Definição da língua de criação de pastas para a padrão da máquina setlocale(LC_TIME, "") # Constantes gerais data = data_ dia, mes, ano = data.split("/") HOJE = date(int(ano), int(mes), int(dia)) DT_HOJE = HOJE.strftime("%d/%m/%Y") NUM_ANO = HOJE.strftime("%Y") NM_MES = HOJE.strftime("%B") NUM_MES = HOJE.strftime("%m") NUM_DIA = HOJE.strftime("%d") DT_FILE = HOJE.strftime("%Y_%m_%d") # Lista de dias da semana WEEKDAYS = list(day_abbr)[:-2] # URLs - Constantes MAINDOWN_URL = "http://pesquisa.in.gov.br/imprensa/servlet/INPDFViewer?" MAINVISU_URL = "http://pesquisa.in.gov.br/imprensa/jsp/visualiza/index.jsp?" DAY_URL = "data=" + DT_HOJE END_URL = "captchafield=firstAccess" PASTA = PASTA_ # Números dos diários 1 e 2 na URL da Imprensa Nacional DATA_TROCA = date(2017, 10, 25) if HOJE < DATA_TROCA: JORNAIS = ["1", "2"] else: JORNAIS = ["515", "529"] if HOJE.strftime("%a") in WEEKDAYS: JORNAL_CONT = 0 for i in JORNAIS: JORNAL_CONT += 1 print("Download DOU%s iniciado..." % str(JORNAL_CONT), end='\r') # Verificação/Montagem dos paths de download (DOU, ANO, MÊS, DIA) dou_dir = os.path.join(PASTA, "DOU" + str(JORNAL_CONT)) # URL - Número do Jornal jornal_url = "jornal=" + str(i) # Definição do número máximo de páginas do DOU. pag_num = 1 pag_url = "pagina=" + str(pag_num) pag_url = MAINVISU_URL + jornal_url + chr(38) + pag_url + chr( 38) + DAY_URL url = urlopen(pag_url) soup = BeautifulSoup(url, "html5lib") soup = soup.find("frame", {"name": "controlador"}) if soup == None: print('Houve algum erro ao abrir o Diário de código: ', i) continue pag_max = str(soup).split(";")[-1] pag_max = refindall(r"\d+", pag_max) pag_max = int(pag_max[0]) dou_dir = check_dou_dirs(dou_dir, NUM_DIA, NM_MES, NUM_ANO) # Lista dos arquivos no diretório alvo (evitar duplicidade de download) files_lst = os.listdir(dou_dir) for j in range(1, pag_max + 1): # URLs - Número da página e endereço completo pag_url = "pagina=" + str(j) down_url = (MAINDOWN_URL + jornal_url + chr(38) + pag_url + chr(38) + DAY_URL + chr(38) + END_URL) # Montagem do nome do arquivo num_pag = "00" + str(j) num_pag = num_pag[-3:] pdfname = "DOU" + str( JORNAL_CONT) + "_" + DT_FILE + "_pg" + num_pag + ".pdf" if pdfname in files_lst: # Download ignorado print( "Progresso: DOU%s -- %s de %s | Download ignorado (duplicidade)" % (str(JORNAL_CONT), str(j), str(pag_max)), end="\r") else: download_status = False while download_status is False: try: # Download do arquivo pdf_path = dou_dir + "\\" + pdfname with open(pdf_path, "wb") as pdf_file: pdf_file.write(urlopen(down_url).read()) print( "Progresso: DOU%s -- %s de %s | Download feito" % (str(JORNAL_CONT), str(j), str(pag_max)), end="\r") download_status = True sleep(round(random() * 3, 2)) except: print( "Progresso: DOU%s -- %s de %s | Erro download: Tentando novamente..." % (str(JORNAL_CONT), str(j), str(pag_max)), end="\r") sleep(round(random() * 5, 2)) print("Download DOU%s completo!" % str(JORNAL_CONT), end='\n') else: print("Não há diários no dia %s \n" % str(DT_HOJE))
def __init__(self, body, value, description = None): """ constructor with : - body : object body or CartesianMesh - value : a PhysicalQuantity, a list of tuples (PhysicalQuantity,species) a ChemicalState or a tuple to introduce a function on a specific variable """ if type(body) is types.ListType: verifyClassList(body,[CartesianMesh]) pass else: memberShip(body,[CartesianMesh, Body]) pass self.zone = body self.body = body self.value_species = None self.value_property = None self.value = None self.headValue = None # # Linked to the treatment of a wellbore # self.enthalpyInitialCondition = None self.wellFeedZoneInitialCondition = None self.temperatureInitialCondition = None self.wellMassFlowInitialCondition = None self.wellPressureInitialCondition = None # if type(value) is types.ListType: for i in value: print ("dbg commonmodel",type(i)) pass verifyClassList(value, [ Head, ChemicalState, Displacement, types.TupleType]) for ic in value: if isinstance(ic, Head): self.headValue = ic # It should be the charge pass elif isinstance(ic, (Displacement, ChemicalState)) : self.value = ic # It should be chemistry or a displacement pass elif isinstance(ic, types.TupleType): #print("debug commonmodel ic %s\n"%(ic[0].lower())) if ic[0].lower() == "enthalpy": # It can also be an enthalpy in the # case of a well if type(ic[1]) == types.StringType: #raw_input("common model debug") self.enthalpyInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'), ic[1]) pass pass elif ic[0].lower().replace(" ","") == "feedzoneheatsource": # We introduce here a heat source linked to a feed zone. if type(ic[1]) in [types.TupleType, types.ListType]: # It should be a tuple: position and value of the source term. self.wellFeedZoneInitialCondition = ic[1] pass elif type(ic[1]) is types.StringType: # It should be a tuple: position and value of the source term. self.wellFeedZoneInitialCondition = refindall(recompile(r'([ifelsxyzXYZ0-9.*;()/+-<>=])'), ic[1]) pass #print("in commonmodel ",self.wellFeedZoneInitialCondition) #raw_input() pass elif ic[0].lower() == "temperature": # It should be temperature otherwise a warning # is raised. We extract the formula thanks to != # regular expressions modules. if type(ic[1]) == types.StringType: self.temperatureInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'), ic[1]) pass pass elif ic[0].lower().replace(" ","") == "wellmassflow": # It can also be the mass flow in the # case of a well if type(ic[1]) == types.StringType: self.wellMassFlowInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'), ic[1]) pass elif type(ic[1]) in [types.FloatType,types.IntType]: self.wellMassFlowInitialCondition = ic[1] pass pass elif ic[0].lower().replace(" ","") == "wellpressure": # It can also be the pressure in the # case of a well if type(ic[1]) == types.StringType: self.wellPressureInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'), ic[1]) pass elif type(ic[1]) in [types.FloatType, types.IntType]: self.wellPressureInitialCondition = ic[1] #print("commonmodel well pressure debug yes\n") #raw_input() pass pass else: raise Warning, "check the name of the variable " pass else: if (isinstance(ic, PhysicalQuantity) or type(ic) is types.ListType): self.value_species, self.value_property = createList(ic, PhysicalQuantity) pass else: self.value = ic pass pass pass pass else: memberShip(value,[PhysicalQuantity,ChemicalState]) if (isinstance(value, PhysicalQuantity) or type(value) is types.ListType): self.value_species,self.value_property = createList(value, PhysicalQuantity) pass else: self.value = value pass pass self.description = description return None
def __init__(self, boundary, btype, value = None, massTCoef = None, velocity = None, flowRate = None, porosity = None, timeVariation = None, description = None): """ Constructor with : - boundary : a mesh part element of type Cartesian or Unstructured ( made of bodies) - btype : is a string and should be "Dirichlet", "Flux", "Mixed", "Neumann" For a "symmetry", a Neumann boundary condition with g = 0 must be specified - OPTIONAL : --> value : a PhysicalQuantity or a list of tuples (PhysicalQuantity,species) or a ChemicalState --> massTCoef : float : mass transfer coefficient or set to zero --> velocity : object Velocity --> porosity : a scalar. --> flowRate : a Flowrate, see PhysicalQuantities --> timeVariation : a list of tuples [(time,chemical state)] , [(time,(list of species and eventually temperature))]; the temperature can also be introduced through a file. -- description a string which will be eventually set as a support for the model comprehension """ bcDico = makeDico(Dirichlet = [ChemicalState, Head, Displacement, NormalForce],\ Flux = [ChemicalState, HeadGradient],\ Neumann = [ChemicalState, HeadGradient]) CommonBoundaryCondition.__init__(self,boundary, btype, value, bcDico, description) # print "dbg commonmodel CommonBoundaryCondition1" if type(boundary) is types.ListType: # print "type of boundary is list type " #raw_input("type of boundary is list type ") verifyClassList(boundary,[ CartesianMesh, Body]) pass else: memberShip(boundary,[ CartesianMesh, Body]) #raw_input("membership ") pass #raw_input("dbg commonmodel CommonBoundaryCondition2") self.boundary = boundary if type(btype) != types.StringType: raise TypeError, " problem on the definition of the boundary type " if btype.lower() not in ["dirichlet","symmetry","flux","mixed","neumann","noflux"]: raise Exception, " check the boundary condition kind" self.btype = btype self.chemicalStateValue = None self.headValue = None self.massTCoef = 0. self.value_species = None self.value_property = None self.value = None # # the next ones are linked to a well sim. # self.enthalpyBoundaryCondition = None self.wellMassFlowBoundaryCondition = None self.wellPressureBoundaryCondition = None # # We treat B.C. # by default, a chemical state is introduced # and in the case of a transient flow, eventually a list # made of a chemical state, a displacement, a head. # if type(value) is types.ListType: # # useful for debugging # #for i in value: # print "dbg commonmodel",type(i) # pass verifyClassList(value, [ Head, ChemicalState, Displacement, NormalForce, TupleType]) for bc in value: if isinstance(bc, Head): self.headValue = bc # it should be the charge pass elif isinstance(bc, NormalForce): self.normalForceValue = bc # it should be NormalForce pass elif isinstance(bc, Displacement): self.displacementValue = bc # it should be Displacement pass elif isinstance(bc, ChemicalState): self.value = bc self.chemicalStateValue = bc # it should be ChemicalState pass elif bc[0].lower() == "enthalpy": # it can also be an enthalpy in the # case of a well # if type(bc[1]) == types.StringType: self.enthalpyBoundaryCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.enthalpyBoundaryCondition = bc[1] pass elif bc[0].lower() == "wellpressure": # it can also be the pressure in the # case of a well # if type(bc[1]) == types.StringType: self.wellPressureBoundaryCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.wellPressureBoundaryCondition = bc[1] #print("commonmodel well pressure debug yes\n") #raw_input() pass pass elif bc[0].lower() == "wellmassflow": # it can also be the mass flow in the # case of a well # if type(bc[1]) == types.StringType: self.wellMassFlowBoundaryCondition = refindall(recompile(r'([$mfunction()ifelse{} ><_=xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.wellMassFlowBoundaryCondition = bc[1] pass elif type(bc[1]) is tuple: self.wellMassFlowBoundaryCondition = bc[1] pass pass else: #self.value = bc # it should be chemistry pass pass pass else: memberShip(value,[PhysicalQuantity, ChemicalState, Displacement, NormalForce]) if (isinstance(value, PhysicalQuantity) or type(value) is types.ListType): self.value_species, self.value_property = createList(value, PhysicalQuantity) pass else: self.value = value self.chemicalStateValue = value pass pass print "massTCoef",massTCoef,type(massTCoef) if massTCoef: memberShip(massTCoef,[types.FloatType]) if (type(massTCoef) is types.FloatType): self.massTCoef = massTCoef pass else: self.massTCoef = 0.0 pass print " common model mass transfer coefficient ",self.massTCoef pass if porosity: self.porosity = porosity pass if velocity: memberShip(velocity,Velocity) pass self.velocity = velocity if flowRate: if flowRate.__class__.__name__=="FlowRate": pass else: flowrate = FlowRate(flowrate,"m**3/s") # the flow rate is supposed to be in m**3/s pass self.flowRate = flowRate if timeVariation: if type(timeVariation) != types.ListType: raise typeError, " Time variation should be a list" for item in timeVariation: if type(item[0]) not in [types.FloatType,types.IntType]: raise typeError, "item[@] should be a list" memberShip(item[1],[ChemicalState]) pass pass self.timeVariation = timeVariation return None
def __init__(self, body, value, description = None): """ constructor with : - body : object body or CartesianMesh - value : a PhysicalQuantity, a list of tuples (PhysicalQuantity,species) a ChemicalState or a tuple to introduce a function on a specific variable """ if type(body) is types.ListType: verifyClassList(body,[CartesianMesh]) pass else: memberShip(body,[CartesianMesh, Body]) pass self.zone = body self.body = body self.value_species = None self.value_property = None self.value = None self.enthalpyInitialCondition = None self.headValue = None self.temperatureInitialCondition = None self.wellMassFlowInitialCondition = None self.wellPressureInitialCondition = None if type(value) is types.ListType: for i in value: print ("dbg commonmodel",type(i)) pass verifyClassList(value, [ Head, ChemicalState, Displacement, types.TupleType]) for ic in value: if isinstance(ic, Head): self.headValue = ic # it should be the charge pass elif isinstance(ic, (Displacement,ChemicalState)) : self.value = ic # it should be chemistry or a displacement pass elif isinstance(ic, types.TupleType): #print("debug commonmodel ic %s\n"%(ic[0].lower())) if ic[0].lower() =="temperature": # it should be temperature otherwise a warning # is raised. we extract the formula thanks to != # regular expressions modules. # if type(ic[1]) == types.StringType: self.temperatureInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),ic[1]) pass pass elif ic[0].lower() =="enthalpy": # it can also be an enthalpy in the # case of a well # if type(ic[1]) == types.StringType: #raw_input("common model debug") self.enthalpyInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),ic[1]) pass pass elif ic[0].lower() =="wellpressure": # it can also be the pressure in the # case of a well # if type(ic[1]) == types.StringType: self.wellPressureInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),ic[1]) pass elif type(ic[1]) in [types.FloatType,types.IntType]: self.wellPressureInitialCondition = ic[1] #print("commonmodel well pressure debug yes\n") #raw_input() pass pass elif ic[0].lower() =="wellmassflow": # it can also be the mass flow in the # case of a well # if type(ic[1]) == types.StringType: self.wellMassFlowInitialCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),ic[1]) pass elif type(ic[1]) in [types.FloatType,types.IntType]: self.wellMassFlowInitialCondition = ic[1] pass pass else: raise Warning, "check the name of the vriable " pass else: if (isinstance(ic, PhysicalQuantity) or type(ic) is types.ListType): self.value_species, self.value_property = createList(ic, PhysicalQuantity) pass else: self.value = ic pass pass pass pass else: memberShip(value,[PhysicalQuantity,ChemicalState]) if (isinstance(value, PhysicalQuantity) or type(value) is types.ListType): self.value_species,self.value_property = createList(value, PhysicalQuantity) pass else: self.value = value pass pass self.description = description return None
def __init__(self, boundary, btype, value = None, massTCoef = None, velocity = None, flowRate = None, porosity = None, timeVariation = None, description = None): """ Constructor with : - boundary : a mesh part element of type Cartesian or Unstructured ( made of bodies) - btype : is a string and should be "Dirichlet", "Flux", "Mixed", "Neumann" For a "symmetry", a Neumann boundary condition with g = 0 must be specified - OPTIONAL : --> value : a PhysicalQuantity or a list of tuples (PhysicalQuantity,species) or a ChemicalState --> massTCoef : float : mass transfer coefficient or set to zero --> velocity : object Velocity --> porosity : a scalar. --> flowRate : a Flowrate, see PhysicalQuantities --> timeVariation : a list of tuples [(time,chemical state)] , [(time,(list of species and eventually temperature))]; the temperature can also be introduced through a file. -- description a string which will be eventually set as a support for the model comprehension """ bcDico = makeDico(Dirichlet = [ChemicalState, Head, Displacement, NormalForce],\ Flux = [ChemicalState, HeadGradient],\ Neumann = [ChemicalState, HeadGradient]) CommonBoundaryCondition.__init__(self,boundary, btype, value, bcDico, description) # print "dbg commonmodel CommonBoundaryCondition1" if type(boundary) is types.ListType: # print "type of boundary is list type " #raw_input("type of boundary is list type ") verifyClassList(boundary,[ CartesianMesh, Body]) pass else: memberShip(boundary,[ CartesianMesh, Body]) #raw_input("membership ") pass #raw_input("dbg commonmodel CommonBoundaryCondition2") self.boundary = boundary if type(btype) != types.StringType: raise TypeError, " problem on the definition of the boundary type " if btype.lower() not in ["dirichlet","symmetry","flux","mixed","neumann","noflux"]: raise Exception, " check the boundary condition kind" self.btype = btype self.chemicalStateValue = None self.headValue = None self.massTCoef = 0. self.value_species = None self.value_property = None self.value = None # # the next ones are linked to a well sim. # self.enthalpyBoundaryCondition = None self.wellMassFlowBoundaryCondition = None self.wellPressureBoundaryCondition = None # # We treat B.C. # by default, a chemical state is introduced # and in the case of a transient flow, eventually a list # made of a chemical state, a displacement, a head. # if type(value) is types.ListType: # # useful for debugging # #for i in value: # print "dbg commonmodel",type(i) # pass verifyClassList(value, [ Head, ChemicalState, Displacement, NormalForce, TupleType]) for bc in value: if isinstance(bc, Head): self.headValue = bc # it should be the charge pass elif isinstance(bc, NormalForce): self.normalForceValue = bc # it should be NormalForce pass elif isinstance(bc, Displacement): self.displacementValue = bc # it should be Displacement pass elif isinstance(bc, ChemicalState): self.value = bc self.chemicalStateValue = bc # it should be ChemicalState pass elif bc[0].lower() =="enthalpy": # it can also be an enthalpy in the # case of a well # if type(bc[1]) == types.StringType: self.enthalpyBoundaryCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.enthalpyBoundaryCondition = bc[1] pass elif bc[0].lower() =="wellpressure": # it can also be the pressure in the # case of a well # if type(bc[1]) == types.StringType: self.wellPressureBoundaryCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.wellPressureBoundaryCondition = bc[1] #print("commonmodel well pressure debug yes\n") #raw_input() pass pass elif bc[0].lower() =="wellmassflow": # it can also be the mass flow in the # case of a well # if type(bc[1]) == types.StringType: self.wellMassFlowBoundaryCondition = refindall(recompile(r'([xyzXYZ0-9.*/+-])'),bc[1]) pass elif type(bc[1]) in [types.FloatType,types.IntType]: self.wellMassFlowBoundaryCondition = bc[1] pass pass else: #self.value = bc # it should be chemistry pass pass pass else: memberShip(value,[PhysicalQuantity, ChemicalState, Displacement, NormalForce]) if (isinstance(value, PhysicalQuantity) or type(value) is types.ListType): self.value_species, self.value_property = createList(value, PhysicalQuantity) pass else: self.value = value self.chemicalStateValue = value pass pass print "massTCoef",massTCoef,type(massTCoef) if massTCoef: memberShip(massTCoef,[types.FloatType]) if (type(massTCoef) is types.FloatType): self.massTCoef = massTCoef pass else: self.massTCoef = 0.0 pass print " common model mass transfer coefficient ",self.massTCoef pass if porosity: self.porosity = porosity pass if velocity: memberShip(velocity,Velocity) pass self.velocity = velocity if flowRate: if flowRate.__class__.__name__=="FlowRate": pass else: flowrate = FlowRate(flowrate,"m**3/s") # the flow rate is supposed to be in m**3/s pass self.flowRate = flowRate if timeVariation: if type(timeVariation) != types.ListType: raise typeError, " Time variation should be a list" for item in timeVariation: if type(item[0]) not in [types.FloatType,types.IntType]: raise typeError, "item[@] should be a list" memberShip(item[1],[ChemicalState]) pass pass self.timeVariation = timeVariation return None
def searchdepth(search_dir, search_point, end_at_first, search_by_pattern, search_hidden, lowercase, excluded_dirs, exclude_pattern, already_found, recursion): """ Recursive function to depth search. :param search_dir: str destination directory name or pattern :param search_point: str directory where to search at this iteration :param end_at_first: bool will stop search at first found directory :param search_by_pattern: bool interprete search_dir ad pattern :param search_hidden: bool include also hidden files to search :param lowercase: bool same as case-sensitive :param already_found: internal variable. Some dirs were already found :param recursion: internal variable. Need to set search depth limit """ if (end_at_first and already_found) or (recursion <= 0): return [] e_d = excluded_dirs.split(':') try: dir_list = [] for f in listdir(path=search_dir): path = osjoin(search_dir, f) if isdir(path) and (path not in e_d) and (len( refindall(exclude_pattern, path)) == 0): dir_list.append(f) except PermissionError: return [] if not search_hidden: dir_list = [f for f in dir_list if f[0] != '.'] if search_by_pattern and lowercase: found_list = [ f for f in dir_list if len(refindall(search_point.lower(), f.lower())) > 0 ] elif search_by_pattern: found_list = [ f for f in dir_list if len(refindall(search_point, f)) > 0 ] elif lowercase: found_list = [f for f in dir_list if f.lower() == search_point.lower()] else: found_list = [f for f in dir_list if f == search_point] # if not search_hidden: # found_list = [f for f in found_list if f[0] != '.'] if len(found_list) > 0 and end_at_first: already_found = True return [osjoin(search_dir, sorted(found_list)[0])] elif len(found_list) > 0: already_found = True for d in dir_list: found_list += searchdepth(osjoin(search_dir, d), search_point, end_at_first, search_by_pattern, search_hidden, lowercase, excluded_dirs, exclude_pattern, already_found, recursion - 1) return [osjoin(search_dir, f) for f in found_list]