0

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 most 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.

edit: The error falls when I call 'created' - so on the logger.debug(office.name section, for example.

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.

Code is as below:

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
2
  • 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.
    – Withnail
    May 28, 2014 at 11:04
  • 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) anywhere. May 28, 2014 at 11:20

1 Answer 1

1

I think your diagnosis of this problem is wrong. The error is telling you that a tuple is not callable. 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

instead of

logging.info(my_tuple)

which is causing info to refer to a tuple from that point on, rather than the original method.

2
  • 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
    May 29, 2014 at 8:36
  • 1
    It happens. The downsides of a completely dynamic language, unfortunately. May 29, 2014 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.