def sort_row_list_and_convert_into_cells():
        ''' this is a complex function,
            it sorts by row,
            then find each attribute,
            then looks for the attribute object,
            then sees if the value is outside the bounds of a standard deviation,
            then sets object colors
        '''
        extra_rows = 0
        extra_rows += function_globals.default_row_buffer_before_stocks

        score_avg = None
        score_std = None
        # first, get score avg and std
        for attribute_obj in attribute_list:
            if attribute_obj.name == "Score":
                score_avg = attribute_obj.avg
                score_std = attribute_obj.std

        first_iteration = True
        last_sigma_stage = None

        function_globals.row_list.sort(key = lambda x: x.Score.text, reverse=True)
        for stock_row in function_globals.row_list:
            # Now, check if we need a blank row between sigma's
            if   stock_row.Score.text > (score_avg + (score_std * 3)):
                # greater than 3 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 3

            elif stock_row.Score.text > (score_avg + (score_std * 2)):
                # greater than 2 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 2
                if last_sigma_stage > 2:
                    last_sigma_stage = 2

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "3 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 1)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 1
                if last_sigma_stage > 1:
                    last_sigma_stage = 1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "2 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 0)):
                # greater than avg
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 0
                if last_sigma_stage > 0:
                    last_sigma_stage = 0

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "1 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text >= (score_avg - (score_std * 1)):
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 0
                if last_sigma_stage > -1:
                    last_sigma_stage = -1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

                    # this is the average cell
                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    avg_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "Average")
                    function_globals.all_cells_list.append(avg_cell)
                    function_globals.avg_row_cell = avg_cell
                    extra_rows += 1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg - (score_std * 2)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -1
                if last_sigma_stage > -2:
                    last_sigma_stage = -2

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-1 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg - (score_std * 3)):
                # greater than 2 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -2
                if last_sigma_stage > -3:
                    last_sigma_stage = -3

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-2 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1



            row_num = function_globals.row_list.index(stock_row) + extra_rows

            for attribute_obj in attribute_list:
                data_cell = getattr(stock_row, attribute_obj.name)
                if data_cell.text is not None:
                    data = data_cell.text
                    background_color = None
                    if attribute_obj.avg is not None and attribute_obj.std is not None and data is not None:
                        if attribute_obj.size_factor == "big":
                            if data > (attribute_obj.avg + attribute_obj.std):
                                # better than 1 standard deviation -> green!
                                background_color = "#CCFFCC"
                            elif data < (attribute_obj.avg - (attribute_obj.std * 2)):
                                # worse than 2 standard deviations -> red :/
                                background_color = "#F78181"
                            elif data < (attribute_obj.avg - attribute_obj.std):
                                # worse than 1 standard deviation -> orange
                                background_color = "#FFB499"
                        elif attribute_obj.size_factor == "small":
                            if data < (attribute_obj.avg - attribute_obj.std):
                                # better than 1 standard deviation -> green!
                                background_color = "#CCFFCC"
                            elif data > (attribute_obj.avg + (attribute_obj.std * 2)):
                                # worse than 2 standard deviations -> red :/
                                background_color = "#F78181"
                            elif data > (attribute_obj.avg + attribute_obj.std):
                                # worse than 1 standard deviation -> orange
                                background_color = "#FFB499"

                    new_data_cell = Cell(text = data)
                    new_data_cell.row = row_num
                    new_data_cell.col = attribute_obj.col
                    new_data_cell.background_color = background_color
                    new_data_cell.text_color = data_cell.text_color
                    new_data_cell.align_right = attribute_obj.align_right

                    if attribute_obj.display == "2":
                        new_data_cell.text = "%.2f" % data
                    elif attribute_obj.display == "%":
                        try:
                            data = float(data)
                            if data.is_integer():
                                new_data_cell.text = str(int(round(float(data)))) + "%"
                            else:
                                new_data_cell.text = ("%.2f" % data) + "%"
                        except:
                            new_data_cell.text = str(data) + "%"
                    elif attribute_obj.display == "$":
                        try:
                            new_data_cell.text = config.locale.currency(float(data), grouping = True)
                        except Exception as e:
                            logging.info(e)
                            new_data_cell.text = "$" + str(data)
                    elif attribute_obj.display == "rnk":
                        try:
                            if float(data).is_integer():
                                new_data_cell.text = str(int(data))
                            else:
                                new_data_cell.text = str(data)
                        except:
                            new_data_cell.text = str(data)
                    try:
                        new_data_cell.row_title = stock_row.stock.ticker
                    except:
                        pass
                    function_globals.all_cells_list.append(new_data_cell)
    def sort_row_list_and_convert_into_cells():
        ''' this is a complex function,
            it sorts by row,
            then find each attribute,
            then looks for the attribute object,
            then sees if the value is outside the bounds of a standard deviation,
            then sets object colors
        '''
        extra_rows = 0
        extra_rows += function_globals.default_row_buffer_before_stocks

        score_avg = None
        score_std = None
        # first, get score avg and std
        for attribute_obj in attribute_list:
            if attribute_obj.name == "Score":
                score_avg = attribute_obj.avg
                score_std = attribute_obj.std

        first_iteration = True
        last_sigma_stage = None

        function_globals.row_list.sort(key = lambda x: x.Score.text, reverse=True)
        for stock_row in function_globals.row_list:
            # Now, check if we need a blank row between sigma's
            if   stock_row.Score.text > (score_avg + (score_std * 3)):
                # greater than 3 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 3

            elif stock_row.Score.text > (score_avg + (score_std * 2)):
                # greater than 2 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 2
                if last_sigma_stage > 2:
                    last_sigma_stage = 2

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "3 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 1)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 1
                if last_sigma_stage > 1:
                    last_sigma_stage = 1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "2 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 0)):
                # greater than avg
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 0
                if last_sigma_stage > 0:
                    last_sigma_stage = 0

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "1 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text >= (score_avg - (score_std * 1)):
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 0
                if last_sigma_stage > -1:
                    last_sigma_stage = -1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

                    # this is the average cell
                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    avg_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "Average")
                    function_globals.all_cells_list.append(avg_cell)
                    function_globals.avg_row_cell = avg_cell
                    extra_rows += 1

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg - (score_std * 2)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -1
                if last_sigma_stage > -2:
                    last_sigma_stage = -2

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-1 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg - (score_std * 3)):
                # greater than 2 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -2
                if last_sigma_stage > -3:
                    last_sigma_stage = -3

                    empty_row_num = function_globals.row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-2 sigma")
                    function_globals.all_cells_list.append(empty_cell)
                    extra_rows += 1



            row_num = function_globals.row_list.index(stock_row) + extra_rows

            for attribute_obj in attribute_list:
                data_cell = getattr(stock_row, attribute_obj.name)
                if data_cell.text is not None:
                    data = data_cell.text
                    background_color = None
                    if attribute_obj.avg is not None and attribute_obj.std is not None and data is not None:
                        if attribute_obj.size_factor == "big":
                            if data > (attribute_obj.avg + attribute_obj.std):
                                # better than 1 standard deviation -> green!
                                background_color = "#CCFFCC"
                            elif data < (attribute_obj.avg - (attribute_obj.std * 2)):
                                # worse than 2 standard deviations -> red :/
                                background_color = "#F78181"
                            elif data < (attribute_obj.avg - attribute_obj.std):
                                # worse than 1 standard deviation -> orange
                                background_color = "#FFB499"
                        elif attribute_obj.size_factor == "small":
                            if data < (attribute_obj.avg - attribute_obj.std):
                                # better than 1 standard deviation -> green!
                                background_color = "#CCFFCC"
                            elif data > (attribute_obj.avg + (attribute_obj.std * 2)):
                                # worse than 2 standard deviations -> red :/
                                background_color = "#F78181"
                            elif data > (attribute_obj.avg + attribute_obj.std):
                                # worse than 1 standard deviation -> orange
                                background_color = "#FFB499"

                    new_data_cell = Cell(text = data)
                    new_data_cell.row = row_num
                    new_data_cell.col = attribute_obj.col
                    new_data_cell.background_color = background_color
                    new_data_cell.text_color = data_cell.text_color
                    new_data_cell.align_right = attribute_obj.align_right

                    if attribute_obj.display == "2":
                        new_data_cell.text = "%.2f" % data
                    elif attribute_obj.display == "%":
                        try:
                            data = float(data)
                            if data.is_integer():
                                new_data_cell.text = str(int(round(float(data)))) + "%"
                            else:
                                new_data_cell.text = ("%.2f" % data) + "%"
                        except:
                            new_data_cell.text = str(data) + "%"
                    elif attribute_obj.display == "$":
                        try:
                            new_data_cell.text = config.locale.currency(float(data), grouping = True)
                        except Exception as e:
                            print line_number(), e
                            new_data_cell.text = "$" + str(data)
                    elif attribute_obj.display == "rnk":
                        try:
                            if float(data).is_integer():
                                new_data_cell.text = str(int(data))
                            else:
                                new_data_cell.text = str(data)
                        except:
                            new_data_cell.text = str(data)
                    try:
                        new_data_cell.row_title = stock_row.stock.ticker
                    except:
                        pass
                    function_globals.all_cells_list.append(new_data_cell)
    def sort_row_list_and_convert_into_cells(row_list):
        ''' this is a complex function,
            it sorts by row,
            then find each attribute,
            then looks for the attribute object,
            then sees if the value is outside the bounds of a standard deviation,
            then sets object colors
        '''
        extra_rows = 0
        extra_rows += default_row_buffer_before_stocks

        score_avg = None
        score_std = None
        # first, get score avg and std
        for attribute_obj in attribute_list:
            if attribute_obj.name == "Score":
                score_avg = attribute_obj.avg
                score_std = attribute_obj.std

        first_iteration = True
        last_sigma_stage = None

        row_list.sort(key = lambda x: x.Score.text, reverse=True)
        for stock_row in row_list:
            # Now, check if we need a blank row between sigma's
            if   stock_row.Score.text > (score_avg + (score_std * 3)):
                # greater than 3 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 3
            elif stock_row.Score.text > (score_avg + (score_std * 2)):
                # greater than 2 sigmas
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 2
                if last_sigma_stage > 2:
                    last_sigma_stage = 2

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "2 sigma")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 1)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 1
                if last_sigma_stage > 1:
                    last_sigma_stage = 1

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "1 sigma")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text > (score_avg + (score_std * 0)):
                # greater than avg
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = 0
                if last_sigma_stage > 0:
                    last_sigma_stage = 0

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "avg")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = " ")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text < (score_avg - (score_std * 1)):
                # greater than 1 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -1
                if last_sigma_stage > -1:
                    last_sigma_stage = -1

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-1 sigma")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1

            elif stock_row.Score.text < (score_avg - (score_std * 2)):
                # greater than 2 sigma
                if first_iteration:
                    first_iteration = False
                    last_sigma_stage = -2

                    empty_row_num = row_list.index(stock_row) + extra_rows
                    empty_cell = Cell(text = " ", col = 0, row = empty_row_num, row_title = "-2 sigma")
                    all_cells_list.append(empty_cell)
                    extra_rows += 1



            row_num = row_list.index(stock_row) + extra_rows


            for attribute_obj in attribute_list:
                data_cell = getattr(stock_row, attribute_obj.name)
                if data_cell.text is not None:
                    data = data_cell.text
                    background_color = None
                    if attribute_obj.avg is not None and attribute_obj.std is not None and data is not None:
                        if data > (attribute_obj.avg + attribute_obj.std):
                            # better than 1 standard deviation -> green!
                            background_color = "#CCFFCC"
                        elif data < (attribute_obj.avg - (attribute_obj.std * 2)):
                            # worse than 2 standard deviations -> red :/
                            background_color = "#F78181"
                        elif data < (attribute_obj.avg - attribute_obj.std):
                            # worse than 1 standard deviation -> orange
                            background_color = "#FFB499"
                    new_data_cell = Cell(text = data)
                    new_data_cell.row = row_num
                    new_data_cell.col = attribute_obj.col
                    new_data_cell.background_color = background_color
                    new_data_cell.text = data
                    try:
                        new_data_cell.row_title = stock_row.stock.ticker
                    except:
                        pass
                    all_cells_list.append(new_data_cell)