0

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 ?

SDRJ
  • 532
  • 3
  • 11

1 Answers1

0

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
MLRAL
  • 51
  • 7
  • Getting the following error when I do the above. I am trying to read from a csv file and create the index object and set values. – SDRJ Apr 10 '22 at 11:59
  • def read_csv(fname, sheet): with open(fname) as f: reader = csv.reader(f) for row, values in enumerate(reader): for col, value in enumerate(values): app.set(models.Index(row, col), value) error - Traceback app.set(models.Index(row, col), value) index.val = raw AttributeError: can't set attribute – SDRJ Apr 10 '22 at 11:59
  • It is difficult to help, when I don't have the full code. Do you have a git with the files or something? – MLRAL Apr 10 '22 at 12:13
  • Is there a way we can have a chat or smething ? It's a private repo. So, can't share it – SDRJ Apr 10 '22 at 12:15
  • how many files do you have? not sure what the best approach is – MLRAL Apr 10 '22 at 12:21