How to import a function from a module in Python?

Asked

Viewed 437 times

1

I’m having trouble importing the function of a module and making use of it. I already made sure to put the file __init__.py folder. But I can’t make use of the functions.

The program will download the HTML content from a web page, and cut out substrings (initial and final subs search) and cut between them. Also makes a follow-up offser of each tag’s initial and final position.

My program (already with the functions of the module, but I need to remove them). I left to test the functionality.

# -*- coding: utf-8 -*-
import requests

Texto = requests.get('https://www.panvel.com/panvel/concor-hct-5mg125mg-30-comprimidos/p-622540')

#print("Status Code :", Texto.status_code)
#print(Texto.headers)


def PosEx(Substring,Texto,Offset):# Retorna resultado de corte em textos com offset e upper ignore
    return Texto.find(Substring,Offset)

Offset = 0

def CortaFora(Offset,Texto,Inicio,Fim): # Da o que está entre o inicio e o fim em determinado texto, respeitando offset e retornando offset inclusive.
    Inicial = Final = ''
    Inicial = PosEx(Inicio,Texto,Offset)+len(Inicio)
    # print('Pos Inicial:'+str(Inicial))
    Offset = Inicial
    Final = PosEx(Fim,Texto,Offset)
    # print('Pos Final:'+str(Final))
    Offset = Final
    return Texto[Inicial:Final].strip(' '), Offset

a = Texto.text
b = PosEx('<h1 class="item-title">',a, Offset)
print('Posição antes do corta Fora do Offset:' +str(b))
print('')
d,Offset = CortaFora(Offset, a,'<h1 class="item-title">','</h1>')
print(d)
print('')
print('Posicao Após Corta Fora do Offset:'+str(Offset))

Module I want to import the functions PosEx and CortaFora:

# -*- coding: utf-8 -*-

def PosEx(Substring,Texto,Offset):# Retorna resultado de corte em textos com offset e upper ignore
   # return Texto.find(Substring,Offset)

Offset = 0

def CortaFora(Offset,Texto,Inicio,Fim): # Da o que está entre o inicio e o fim em determinado texto, respeitando offset e retornando offset inclusive.
    Inicial = Final = ''
    Inicial = PosEx(Inicio,Texto,Offset)+len(Inicio)
    # print('Pos Inicial:'+str(Inicial))
    Offset = Inicial
    Final = PosEx(Fim,Texto,Offset)
    # print('Pos Final:'+str(Final))
    Offset = Final
    return Texto[Inicial:Final].strip(' '), Offset

1 answer

2


It’s the same way you import other modules, as you did in your own code with lib request.

Suppose your programs are in the same folder.

Let’s call Function.py (suggestion) the code with the functions:

# -*- coding: utf-8 -*-
def PosEx(Substring,Texto,Offset):
    """
    Retorna resultado de corte em textos com offset e upper ignore
    """
    return Texto.find(Substring,Offset)


def CortaFora(Offset,Texto,Inicio,Fim): 
    """
    Da o que está entre o inicio e o fim em determinado texto, respeitando offset
    e retornando offset inclusive.
    """
    Inicial = Final = ''
    Inicial = PosEx(Inicio,Texto,Offset)+len(Inicio)

    Offset = Inicial
    Final = PosEx(Fim,Texto,Offset)

    Offset = Final
    return Texto[Inicial:Final].strip(' '), Offset

Then your main code would look like this:

# -*- coding: utf-8 -*-
import requests

from function import PosEx, CortaFora

Texto = requests.get('https://www.panvel.com/panvel/concor-hct-5mg125mg-30-comprimidos/p-622540')

a = Texto.text
b = PosEx('<h1 class="item-title">',a, Offset)
print('Posição antes do corta Fora do Offset:' +str(b))
print('')
d,Offset = CortaFora(Offset, a,'<h1 class="item-title">','</h1>')
print(d)
print('')
print('Posicao Após Corta Fora do Offset:'+str(Offset))
  • Thanks Octavian! , it worked, lack of attention and experience, I thought it was necessary to call the function!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.