Get_or_create arroja un error de tupla intermitente no invocable

I'm using get_or_create as I iterate through a CSV file to read them into the database. With logging enabled I use the object, created tuple to note how it's getting along in the logfile - that all works MEJOR DE TU of the time, except occasionally it throws a 'tuple object not callable' on the 'created' variable returned by get_or_create. It doesn't seem to be consistent in where it throws it - if I run the same code on the same file, sometimes it goes past it and works fine, other times it sticks on (different) earlier lines of the CSV.

editar: The error falls when I call 'created' - so on the logger.debug(office.name sección, por ejemplo.

As a workaround, I have a catchall exception that, as part of the 'except' function, retries the same get_or_create. This fallback is working, and hasn't excepted yet (I'm up to 67k records, and it's thrown a different error), but it's a hatchet job, and very unpythonic. I'd much rather fix the fundamental problem - it feels like get_or_create is silently failing, which means that the tuple is broken, but I can't find any evidence of that; surely if that's the case, an exceptable (sp?) error would be thrown. (There are no duplicates that would result in MultipleObjectsReturned, and I'm not getting an exception anyway).

Tl; dr - get_or_create works 99% of the time, and all the time if I put a fallback in, but doesn't fail in a helpful way.

Suggestions warmly welcomed.

El código es el siguiente:

try:
    office, created = Office.objects.get_or_create(name=office_name)
    logger.debug(office)
    logger.debug(created)

    if created:
        logger.info("Office added to database:")
        logger.info(office.name)
    else:
        logger.info("Office already exists:")
        logger.info(office.name)
except: #catchall exception for now 
    logger.error("Could not create: %s", office_name)
    office, created = Office.objects.get_or_create(name=office_name)
    if created:
         logger.info("Office added to database:")
         logger.info(str(office.name))
    else:
         logger.info("Office already exists:")
         logger.info(str(office.name))
         return office
    return office

Traceback (from logs only, apologies):

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/~/Django/OMS/core/views.py", line 28, in loadinitialstats
    load_stats_from_csv(location)
  File "/Users/~/Django/OMS/core/filehandling.py", line 139, in load_stats_from_csv
    home_office = fetch_or_create_office(row[2])
  File "/Users/~/Django/OMS/core/filehandling.py", line 13, in fetch_or_create_office
    logger.info(created)
TypeError: 'tuple' object is not callable

preguntado el 28 de mayo de 14 a las 11:05

Sorry, that's my fault, I edited the text to take out identifiable names, it's a capital in the original. Have added an edit to the question re: where the error is triggered. -

it would really help to add a line numbers and function names + code where it actually raises that exception. Eg. I dont see that incriminated line logger.info(created) en cualquier lugar. -

1 Respuestas

I think your diagnosis of this problem is wrong. The error is telling you that a tuple is not invocable. Logging something does not mean calling it, and created is not a tuple (it's a bool).

I suspect that somewhere in your code you are accidentally doing something like this:

logging.info = my_tuple

en lugar de

logging.info(my_tuple)

lo que está causando info to refer to a tuple from that point on, rather than the original method.

contestado el 28 de mayo de 14 a las 12:05

There's something in this - having changed the level that the logger calls at initially, it's stopped throwing that error: I can't find anywhere that i've accidentally assigned a tuple to logging.info though, but at least it's giving me a starting point to work back from. Thanks. - Withnail

It happens. The downsides of a completely dynamic language, unfortunately. - daniel rosaman

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.