Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local variable 'target' referenced before assignment #268

Open
Poikilos opened this issue Feb 11, 2025 · 2 comments
Open

local variable 'target' referenced before assignment #268

Poikilos opened this issue Feb 11, 2025 · 2 comments

Comments

@Poikilos
Copy link

Poikilos commented Feb 11, 2025

I'm having trouble getting the current cell color after I highlight it. I am trying to get cell options and ChatGPT is doing guesswork:
highlighted_cells = self.sheet.get_cell_options("highlight", "table")

I know I'm using the module incorrectly due to the error, but if you could improve the error state to warn me that I've given get_cell_options a bad key, that would be helpful--in other words, please add bounds checking (such as an else case or wherever is appropriate to raise a KeyError that explains the issue) in:

        if canvas == "table":
            target = self.MT.cell_options
        elif canvas in ("row_index", "index"):
            target = self.RI.cell_options
        elif canvas == "header":
            target = self.CH.cell_options
        if key is None:
            return target

Instead of allowing "UnboundLocalError: local variable 'target' referenced before assignment" maybe add an exception that leads me to the correct usage, such as:

        else:
            raise KeyError("{} is not a valid canvas name.".format(repr(canvas)))

Regarding my actual problem of getting cell bg color, explain if that is possible or whether that would be a feature request. I am setting it via sheet.highlight_cells(row=row, column=column, bg='lightgreen')

@ragardner
Copy link
Owner

Hello,

I agree about better argument checking, unfortunately there are probably many functions in Sheet() with the same issue at the moment. I will probably go over this at some point and see where i can improve things.

I will probably also improve the docs and remove the info on the older functions which are there for backwards compatibility but maybe I'm not so happy with.

To get cell properties/options, if you're using a version of tksheet >= 7.2.19 you can use these functions:

https://github.com/ragardner/tksheet/wiki/Version-7#getting-cell-properties

If you're using an older version then get_cell_options() is ok but perhaps not very performant if you're repeatedly calling it.

It might be better to just get the internal dict which is my_sheet.MT.cell_options

The dict is key: tuple[row (int), column (int)], value: dict. The value dict has str keys, e.g.

{
    (0, 0): {
        "highlight": Highlight(
            bg="light blue",
            fg="black",
            end=False,
        )
    }
}

You could check if a cell is highlighted very quickly by doing e.g.

(row, column) in my_sheet.MT.cell_options and "highlight" in my_sheet.MT.cell_options[(row, column)]

Or you could iterate over the dict.items() and check if "highlight" is in the value

Other dicts that hold similar info:

# all int keys
my_sheet.MT.row_options
my_sheet.MT.col_options
my_sheet.RI.cell_options
my_sheet.CH.cell_options

Let me know if something isn't working with the above

Kind regards

@Poikilos
Copy link
Author

Poikilos commented Feb 12, 2025

Thank you. Yes, highlight = my_sheet.MT.cell_options[(row, column)].get("highlight") if (row, column) in my_sheet.MT.cell_options else None works and if set, gets a Highlight (namedtuple apparently) with a bg attribute that is the color I set.

Version: 7.3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants