'Simplify over-complex Django filter/list/np.array code

My code is a hybrid (swamp) of snippets from Django documentation, online tutorials and stackexchange answers.

I have a model that contains x and y coordinates of points. One of the x or y coordinates will be +/- 1. The other coordinate will be a real value between -1 and +1. The "other" coordinate values may not be unique. For example, the following are valid coordinates : (0.7,1) , (1, -1), (-1,-0.45) , (0.7, -1), whereas the following are invalid: (0.2,0.3), (3, -1), (1,3).

I have been able to “extract” the coordinates into 4 separate lists when x= +/- 1 or y = +/- 1 so that I can work out the mean of the “other” coordinate. For example, if my filtered list for x = -1 is: [[-1,0.4],[-1,-0.37],[-1,0.78]], I can get the mean of: 0.4, -0.37 and 0.78. In other words, I’m try to get the mean of the values in a particular column in a Python array, where the data has been extracted from a Django model.

I have code in my view that works, but is fantastically complex, and my gut tells me there is probably a sleeker, more efficient and easier to understand solution than the one I have cobbled together. My code is:

#models.py

 class CC_pts_to_avg(models.Model):
    x_coord = models.DecimalField(max_digits=3, decimal_places=2)
    y_coord = models.DecimalField(max_digits=3, decimal_places=2)
#
    def __str__(self):
        return self.x_coord # Get a better name than this?

#urls.py

from .models import ….  CC_pts_to_avg

#views.py

def x_pos1(var_in):
    if var_in[0] == 1:
        return True
    else:
        return False
#
def x_neg1(var_in):
    if var_in[0] == -1:
        return True
    else:
        return False
#
def y_pos1(var_in):
    if var_in[1] == 1:
        return True
    else:
        return False
#
def y_neg1(var_in):
    if var_in[1] == -1:
        return True
    else:
        return False
#
from statistics import mean
import numpy as np

def p2points_avg_calc(request):

    pollsx3 = CC_pts_to_avg.objects.all() # extract all fields from dbase table to Queryset
    list_x_y_coords = list(CC_pts_to_avg.objects.values_list('x_coord','y_coord'))
    xpos1_list = np.array(list(filter(x_pos1,list_x_y_coords)))
    xneg1_list = np.array(list(filter(x_neg1,list_x_y_coords)))
    ypos1_list = np.array(list(filter(y_pos1,list_x_y_coords)))
    yneg1_list = np.array(list(filter(y_neg1,list_x_y_coords)))
            
    mean_of_y_for_x_neg1 = mean(xneg1_list[:,1])
    mean_of_x_for_x_neg1 = -1
    mean_of_y_for_x_pos1 = mean(xpos1_list[:,1])
    mean_of_x_for_x_pos1 = 1
    context = {
        'polls36' : pollsx3, # this is just to display all the data below the template
        'mean_of_x_for_x_neg1' : mean_of_x_for_x_neg1,
        'mean_of_y_for_x_neg1' : mean_of_y_for_x_neg1
 
    }

    return render(request, 'pollapp2/p2pt_avg_calc.html', context) # second parameter is location of HTML source

#p2pt_avg_calc.html template

The following displays the correct values for mean_of_x_for_x_neg1 and mean_of_y_for_x_neg1 passed from the view : p2points_avg_calc (see above)

<!-- p2pt_avg_calc.html -->
{% extends "pollapp2/base.html" %}
<!-- block Title is the name in the tab -->
{% block title %}Average from all points along edge{% endblock %}

{% block main %}
<div class="row">
    <div class="col-lg-8 col-lg-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">The points</h3>
            </div>
            <strong>x mean:</strong>&nbsp;&nbsp;{{ mean_of_x_for_x_neg1}}&nbsp;<strong>y mean:</strong>&nbsp;&nbsp;{{ mean_of_y_for_x_neg1}}&nbsp;
        <ul class="list-group">
        {% for pollabcxz in polls36 %}
        <li class="list-group-item">

            <strong>x:</strong>&nbsp;&nbsp;{{ pollabcxz.x_coord}}&nbsp;<strong>y:</strong>&nbsp;&nbsp;{{ pollabcxz.y_coord}}&nbsp;

        </li>
        {% endfor %}
        </ul>

        </div>
    </div>
</div>
{% endblock %}

Can you help me make some improvements to my code to make it more efficient, but more importantly more readable, understandable and maintainable should I need to modify it later ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source