Return value of a Python class

0

I am a little new in this of the classes and objects. I have a small problem I have created a class that receives data from a file that I use as a dictionary so that in another program equal in Python I can translate the data from a 4GL to SQL : Example.

4GL    
DEFINE [Var] **LIKE** [Tabla].[Campo]

SQL
[Var] [TipodeDato] [Null]

My code is as follows

import sys, os
import re

class FindTC:
    def __init__(self, Tabla="GenPrima", Columna="agte2_gp"):
        self.Columna = Columna
        self.Tabla = Tabla

        registros = self.readfile('Diccionarios\%s.txt'%(self.Tabla))
        registros = self.workfile(registros, Columna)

    def readfile(self, entrada):
        registros = []
        for linea in open(entrada, 'r', encoding='latin-1'):
        linea = linea.strip()
        registros.append(linea)
    return registros

    def workfile(self, entrada, BuscaColumna):
        aux = []
        find = re.compile(r'%s'%(BuscaColumna))
        Nulo = re.compile(r'(?P<VARIABLE>[\w\d]+)\s+(?P<TIPO>[\w]+)\s+(?P<NULL>[no|yes]+)')
        for linea in entrada:
            if find.search(linea):
                aux.append(linea)
                for lineas in aux:
                    lineas = lineas.strip()
                    if Nulo.search(lineas):
                        match = Nulo.search(lineas)
                        V = match.group('VARIABLE')
                        T = match.group('TIPO')
                        N = match.group('NULL')
                        if N == 'no':
                            N = 'NOT NULL'
                        else:
                            N = 'NULL'
                       return V,T,N


obj = FindTC()

print(obj)

This information returns to me:

<Buscar.FindTC object at 0x000002752BB6EA58>VARIABLE ALMACENADA
    
asked by Frank J. Minor 18.10.2018 в 18:09
source

1 answer

1

What do you mean by returning the value of a class? I think you're not very clear about the concepts of object-oriented programming. A class is like a scheme, something abstract. When you do obj = FindTC() you assign to the variable obj an instance of that class or object. When you make print(obj) , you use the __str__(self) method to print a text string that represents the instance of the class. In your case, since you did not define this method, use the default method.

I understand that in your method readfile(self, entrada) the last three lines have been indented badly when copying the code.

Having said all this, I suppose that you would like to obtain the value of records that you assign in the constructor, but that you do not save as an attribute of the class, so at the same moment you initialize the instance with obj = FindTC() , the two methods of the class are performed, they are saved in the variable records, and once it is finished, this variable is lost.

You have several ways to get that value, for example, the code could be something like the following:

import sys, os
import re

class FindTC:
    def __init__(self, Tabla="GenPrima", Columna="agte2_gp"):
        self.Columna = Columna
        self.Tabla = Tabla

    def readfile(self, entrada):
        registros = []
        for linea in open(entrada, 'r', encoding='latin-1'):
            linea = linea.strip()
            registros.append(linea)
        return registros

    def workfile(self, entrada, BuscaColumna):
        aux = []
        find = re.compile(r'%s'%(BuscaColumna))
        Nulo = re.compile(r'(?P<VARIABLE>[\w\d]+)\s+(?P<TIPO>[\w]+)\s+(?P<NULL>[no|yes]+)')
        for linea in entrada:
            if find.search(linea):
                aux.append(linea)
                for lineas in aux:
                    lineas = lineas.strip()
                    if Nulo.search(lineas):
                        match = Nulo.search(lineas)
                        V = match.group('VARIABLE')
                        T = match.group('TIPO')
                        N = match.group('NULL')
                        if N == 'no':
                            N = 'NOT NULL'
                        else:
                            N = 'NULL'
                        return V,T,N

    def obtener_registros(self):
        registros = self.readfile('Diccionarios\%s.txt'%(self.Tabla))
        return self.workfile(registros, Columna)

obj = FindTC()

print(obj.obtener_registros())

Now, instantiating the object with obj = FindTC() does not read or process anything, simply create the object, saving the values of the table and column as attributes, and with three methods. What we print on the screen is the result of invoking the last method.

Entering a little more into the code, instead of creating a third method. we could directly draw those two lines that do nothing in the constructor (and that we have included in another method in the previous solution) outside the class, if we edit a bit the other two methods that it calls. To the readfile method you could leave it alone with the parameter self , since in the parameter entrada you pass an attribute of the class and a fixed string, which can be obtained from within without having to pass them, replacing entrada by self.Tabla . The same goes for the workfile method, but with the BuscaColumna parameter and the other attribute. If we made these changes, without the need to add another method, we could draw the two lines of the record out of the class and call the methods:

import sys, os
import re

class FindTC:
    def __init__(self, Tabla="GenPrima", Columna="agte2_gp"):
        self.Columna = Columna
        self.Tabla = Tabla

    def readfile(self):
        registros = []
        for linea in open('Diccionarios\%s.txt'%(self.Tabla), 'r', encoding='latin-1'):
            linea = linea.strip()
            registros.append(linea)
        return registros

    def workfile(self, entrada):
        aux = []
        find = re.compile(r'%s'%(self.Columna))
        Nulo = re.compile(r'(?P<VARIABLE>[\w\d]+)\s+(?P<TIPO>[\w]+)\s+(?P<NULL>[no|yes]+)')
        for linea in entrada:
            if find.search(linea):
                aux.append(linea)
                for lineas in aux:
                    lineas = lineas.strip()
                    if Nulo.search(lineas):
                        match = Nulo.search(lineas)
                        V = match.group('VARIABLE')
                        T = match.group('TIPO')
                        N = match.group('NULL')
                        if N == 'no':
                            N = 'NOT NULL'
                        else:
                            N = 'NULL'
                        return V,T,N


obj = FindTC()

registros = obj.readfile()
print(obj.workfile(registros)

I hope that these two examples will help you understand a little better how a class works, what it is, what are its attributes and methods, and how to use them.

    
answered by 18.10.2018 / 22:22
source