1

I got a strange thing I can't handle and understand. I'm really intrigue

Let me explain, I coded some stuff locally on my computer with python3 (which run like a charm !! ), I used to transfer this script to my django . And magic happen it's raised an error , I don't have when I run locally ...

here is the script part which seems to create the error :

from c***.utils import calculate_distance
from operator import itemgetter
import geopy

solution = []

def is_in_radius(elem1, elem2):
    dict_coord = {
        'pa' : (elem1[3][0],elem1[3][1]),
        'pb' : (elem2[3][0],elem2[3][1]),
    }

    distance = calculate_distance(dict_coord.get('pa'), dict_coord.get('pb'))
    # print("distance : " + str(distance) + " m - entre :  "+ str(elem1[0]) + " et " + str(elem2[0]))
    if (distance <= 1200):
        return(True)
    else:
        return(False)

def last_rad(data_info, elem1, am_act):
    global solution
    if(len(solution) + 1 == am_act):
        if(is_in_radius(elem1, data_info[1]) == True):
            return(True)
        else:
            return(False)
    else:
        return(True)

def first_rad(data_info, elem1):
    global solution
    if(len(solution) == 0):
        if(is_in_radius(data_info[0], elem1) == True):
            return(True)
        else:
            return(False)
    else:
        return(True)

Error raised is :

'function' object is not subscriptable
Request Method: POST
Request URL:    http://****.io/new_search
Django Version: 3.1
Exception Type: TypeError
Exception Value:    
'function' object is not subscriptable
Exception Location: /home/debian/***/mysite/**/backtrack.py, line 33, in first_rad
Python Executable:  /usr/bin/python3
Python Version: 3.7.3

line 33 is linked to

        if(is_in_radius(data_info[0], elem1) == True):

Does someone have an idea on how to manage the error and when it could coming from ?

Thank's a lot !

1 Answer 1

1

You get this error when you try to access an attribute from a function similar to a dict.

For example, for the builtin function max :

>>> max[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable

Based on that I would suggest that data_info[0] is the problem, and data_info might not be a list like you expect, but a function.

So the function definition is ok, but it's invoked with wrong parameters I would say. Look where you're invoking it and how you're passing the parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank's david , it was this . You open my eyes to see somewhere else it was the parameters. Have a nice day !

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.