'Storing value for a point using named tuple?

I am trying to design a spreadsheet app. I have been able to come up with a class for my Index which is typically the point consisting of row, col. I want to be able to assign a value to the point. The challenge is I am unable to update the value for that cell.

This is what I tried so far models.py

class Index(NamedTuple):
    row: int
    """The row (y-coordinate)"""
    col: int
    """The column (x-coordinate)"""
    val: str = None
    @property
    def column_label(self):
        nreps = (self.col // 26) + 1
        char = chr(ord("A") + self.col % 26)
        return nreps * char

    @property
    def row_label(self):
        return str(self.row + 1)

     def __str__(self):
        if self.val:
             return f"{self.val}"
        return f"{self.column_label}{self.row_label}"

app.py
 class App:
    def set(self, index, raw):
     """index (Index): the cell to update
        raw (str): the raw string
    """
    
       index._replace(val=raw)

Tried the above, but the value for that cell even after assigning it in the set() is still None. What is the best approach to assign a value for that index ?



Solution 1:[1]

I might be missing something but cant you just do

index.val = raw

instead of

index._replace(val=raw)

Again, I am not sure if I understand correctly but is a working example with just your index class:

class Index():
    row: int
    """The row (y-coordinate)"""
    col: int
    """The column (x-coordinate)"""
    val: str = None
    @property
    def column_label(self):
        nreps = (self.col // 26) + 1
        char = chr(ord("A") + self.col % 26)
        return nreps * char

    @property
    def row_label(self):
        return str(self.row + 1)

    def __str__(self):
        if self.val:
             return f"{self.val}"
        return f"{self.column_label}{self.row_label}"

d = Index()

d.val = 2

print(d.val)

d.val = 100

print(d.val)

output

2
100

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 MLRAL