If you get an exception, the “except” clause will drop down into the “return” 
statement, and classification_serializer will be referenced before it’s 
assigned to. (Because it never was assigned to.)

John

> On Apr 22, 2015, at 2:36 PM, Cristian Javier Martinez 
> <[email protected]> wrote:
> 
> Hi! I'm using the django rest framework and successfully deployed an 
> application in a production environment but I'm having an strange error 
> related to threads when I call a method that performs a simple classification 
> task (using a scikit learn classifier) and I have no idea what is causing the 
> error. This is the method in views.py
> 
> @api_view(['GET', 'POST'])
> def classify_item(request):
>         """
>         Classify item, or list classifications.
>         """
>         if request.method == 'GET':
>                 items = Item.objects.all()
>                 serializer = ItemSerializer(items, many=True)
>                 return Response(serializer.data)
> 
>         elif request.method == 'POST':
>                 serializer = ItemSerializer(data=request.data['item'])
>                 if serializer.is_valid():
>                         try:
>                                 item = serializer.data
>                                 filter_name = request.data['filter']
> 
> 
>                                 # fix encoding
>                                 item_dict =  {}
>                                 for key in item:
>                                         value = item[key]
>                                         if isinstance(value, unicode):
>                                                 value = value.encode('utf-8')
>                                         item_dict[key] = value
> 
>                                 classifier_name = 
> CLASSIFIER_NAME_BY_FILTER[filter_name]
> 
>                                 logger.debug("retrieving the persisted 
> classifier and classifing the item")
>                                 clf = 
> joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, classifier_name))
> 
>                                 logger.debug("predicting probabilities")
>                                 data = pd.DataFrame([item_dict])
> 
>                                 logger.debug("scoring item")
>                                 score = clf.predict(data)[0][1]
> 
>                                 logger.debug("score: {}".format(score))
> 
> 
>                                 # create and save classification
>                                 classification = 
> Classification(classifier_name=classifier_name,score=score,item_id=item['_id'])
>                                 classification_serializer = 
> ClassificationSerializer(classification)
>                                 #classification_serializer.save()
>                         except Exception as e:
>                                 logger.error("Ocurrio un error al intentar 
> parsear el item: {}".format(e))
>                         return Response(classification_serializer.data, 
> status=status.HTTP_201_CREATED)
> 
>                 return Response(serializer.errors, 
> status=status.HTTP_400_BAD_REQUEST)
> 
> And the output is:
> 
> [22/Apr/2015 21:14:56] DEBUG [classifiers.views:63] retrieve the classifier 
> for the given sited_id and classify item
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:66] predicting probabilities
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:69] scoring item
> [22/Apr/2015 21:15:14] ERROR [classifiers.views:80] Ocurrio un error al 
> intentar parsear el item: 'Thread' object has no attribute '_children'
> [22/Apr/2015 21:15:14] ERROR [django.request:256] Internal Server Error: 
> /items/
> Traceback (most recent call last):
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/core/handlers/base.py",
>  line 132, in get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newrelic/hooks/framework_django.py",
>  line 499, in wrapper
>     return wrapped(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py",
>  line 58, in wrapped_view
>     return view_func(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/generic/base.py",
>  line 71, in view
>     return self.dispatch(request, *args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newrelic/hooks/component_djangorestframework.py",
>  line 27, in _nr_wrapper_APIView_dispatch_
>     return wrapped(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/rest_framework/views.py",
>  line 452, in dispatch
>     response = self.handle_exception(exc)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/rest_framework/views.py",
>  line 449, in dispatch
>     response = handler(request, *args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/rest_framework/decorators.py",
>  line 50, in handler
>     return func(*args, **kwargs)
>   File "/home/keepcon/meli_filters/classifiers/views.py", line 81, in 
> classify_item
>     return Response(classification_serializer.data, 
> status=status.HTTP_201_CREATED)
> UnboundLocalError: local variable 'classification_serializer' referenced 
> before assignment
> 
> I was having an UnicodeEncodeError but I fixed it by adding "the fix 
> encoding" step. I'll appreciate any cue about what could be causing this 
> error, I don't understand why the error says something about threads since 
> I'm not using threads. 
> 
> Thanks in advance!
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/django-users 
> <http://groups.google.com/group/django-users>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/da84ec7a-24a2-46b5-8d72-2d46c2d5a304%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/da84ec7a-24a2-46b5-8d72-2d46c2d5a304%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8CB6BEE4-BCDB-4430-989C-4973A4C0A3B2%40solinea.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to