Problem with Type error object take no parameters

1

I have the following script:

from record import record


class cliente(record):
  nombre = ""
  codigo = 0
  ultimapelicula= None
  habilitado = True
class pelicula (record):
  nombre=""
  codigo = 0
  ultimocliente = 0
  habilitada = True

def abrirbasedatos():
  listaclientes = []
  listapeliculas = []

  ficheroclientes = abrirfichero ("Clientes.txt")
  if ficheroclientes != None:
    poblarlista(listaclientes, ficheroclientes,1)
    ficheroclientes.close()

  ficheropeliculas = abrirfichero ("C:\Users\IndyCarpl\Desktop\Menuppal\pythong\Peliculas.txt")
  if ficheropeliculas != None:
    poblarlista(listapeliculas, ficheropeliculas,2)
    ficheropeliculas.close()
  return listaclientes, listapeliculas

def abrirfichero (nombrearchivo):
  try:
    return open (nombrearchivo, "r")
  except:
    print ("\n\n No se puedo abrir el fichero", nombrearchivo)
    return

def poblarlista(lista, fichero, numerolista):
    for linea in fichero:
        registro = linea.rstrip().split(",")
        if numerolista == 1:
            lista.append(cliente(nombre=registro[0], codigo = int(registro [1]), ultimapelicula = int(registro[2]), habilitado = convertirVF(registro[3])))
        elif numerolista == 2:
            lista.append(pelicula(nombre=registro [0], codigo = int(registro [1]), ultimocliente = int (registro [2]), habilitada = convertirVF(registro [3])))
    return
def convertirVF (cadena):
  if cadena == "True":
    return True
  elif cadena == "False":
    return False

listaclientes, listapeliculas = abrirbasedatos()

print(listaclientes,listapeliculas)

When I run it, I get the following error:

Traceback (most recent call last):
  File "club.py", line 49, in <module>
    listaclientes, listapeliculas = abrirbasedatos()
  File "club.py", line 19, in abrirbasedatos
    poblarlista(listaclientes, ficheroclientes,1)
  File "club.py", line 39, in poblarlista
    lista.append(cliente(nombre=registro[0], codigo = int(registro [1]), ultimapelicula = int(registro[2]), habilitado = convertirVF(registro[3])))
TypeError: object() takes no parameters

Edit:

I add my script record.py :

import warnings

class metaMetaBunch(type): 

  def __new__(cls, classname, bases, classdict):

    def __init__(self, **kw):

      for k in self.__dflts__: setattr(self, k, self.__dflts__[k])
      for k in kw: setattr(self, k, kw[k])

    def __repr__(self): 

      rep = [ '%s=%r' % (k, getattr(self, k)) for k in self.__dflts__
              if getattr(self, k) != self.__dflts__[k]]
      return '%s(%s)' % (classname, ', '.join(rep))

      # Build the newdict that we'll use as class-dict for the new class
    newdict = {'__slots__':[], '__dflts__':{}, '__init__':__init__, '__repr__':__repr__}

    for k in classdict:
      if k.startswith('__'):
        # Special methods: copy to \emph{newdict}, warn about conflicts.
        if k in newdict:
          warnings.warn("Can't set attr %r in bunch-class %r" % (k, classname))
        else:
          newdict[k] = classdict[k]
      else:
        # Class variables, store name in \texttt{__}\emph{slots}\texttt{__} and name and value as an item in \texttt{__}\emph{dflts}\texttt{__}.
        newdict['__slots__'].append(k)
        newdict['__dflts__'][k] = classdict[k]
    # Finally delegate the rest of the work to \emph{type}.\texttt{__}\emph{new}\texttt{__}
    return type.__new__(cls, classname, bases, newdict)

class record(object):
  # For convenience: inheriting from \emph{record} can be used to get the new metaclass (same as 
  # defining \texttt{__}\emph{metaclass}\texttt{__} yourself).
  __metaclass__ = metaMetaBunch


if __name__ == "__main__":
    class Point(record):
        x = 0.0
        y = 0.0
        color = 'gray'
        q = Point()
        print (q) 

        p = Point(x=1.2, y=3.4)
        print (p)

        r = Point(x=2.0, color='blue')
        print (r)
        print (r.x, r.y)
    
asked by Marco Salazar 17.09.2017 в 05:42
source

1 answer

0

The problem is in your class record . It is correct but it is syntax of Python 2.x, incompatible with Python 3.x.

To define a metaclass assignment to a class in Python 2.x , the following syntax is used:

class Form(BaseForm):
    __metaclass__ = FormType
    pass

In Python 3.x it must be done:

class Form(BaseForm, metaclass=FormType):
    pass

That is, your class record in record.py must be:

class record(object,  metaclass= metaMetaBunch):
    pass

The rest of the code is correct. I advise you to always devise using 4 spaces according to PEP-8.

Your record.py should be like this:

import warnings

class metaMetaBunch(type):
    def __new__(cls, classname, bases, classdict):

        def __init__(self, **kw):
            for k in self.__dflts__: setattr(self, k, self.__dflts__[k])
            for k in kw: setattr(self, k, kw[k])

        def __repr__(self):
            rep = [ '%s=%r' % (k, getattr(self, k)) for k in self.__dflts__
                    if getattr(self, k) != self.__dflts__[k]
                  ]
            return '%s(%s)' % (classname, ', '.join(rep))

        newdict = { '__slots__':[], '__dflts__':{},
                    '__init__':__init__, '__repr__':__repr__, }

        for k in classdict:
            if k.startswith('__'):
                if k in newdict:
                    warnings.warn("Can't set attr %r in bunch-class %r" % (
                        k, classname))
                else:
                    newdict[k] = classdict[k]
            else:
                newdict['__slots__'].append(k)
                newdict['__dflts__'][k] = classdict[k]

        return type.__new__(cls, classname, bases, newdict)


class record(object,  metaclass= metaMetaBunch):
    pass

To see this and other differences between Python 2 and Python 3 you can look at yourself:

What's New In Python 3.0

    
answered by 17.09.2017 / 07:58
source