0

This is my first question on StackOverflow. I am very nervous. I am new to programming, and doing my best to learn and improve.

I am currently working on a project using Beautiful Table. However, when I run my code, I keep getting this deprecation warning:

C:\Users\Piko...\venv\Lib\site-packages\beautifultable\utils.py:125: FutureWarning: 'BeautifulTable.getitem' has been deprecated in 'v1.0.0' and will be removed in 'v1.2.0'. Use 'BeautifulTable.{columns|rows}[key]' instead.

from beautifultable import BeautifulTable

table = BeautifulTable()

    table.rows.append([" ", " ", " "])
    table.rows.append([" ", " ", " "])
    table.rows.append([" ", " ", " "])
    table.columns.header = ["A", "B", "C"]
    table.rows.header = ["1", "2", "3"]


#below is an example of code that triggers the warning:
table.rows[0]['A'] = "Oops"

I have gone through the BeautifulTable docs and I can't seem to figure out the proper syntax of 'BeautifulTable.{columns|rows}[key]'.

Could you help me rewrite this code so I no longer get this warning message?

Also, I have used the following code as a temporary fix because the warning's red font makes me nervous:

import warnings



warnings.simplefilter(action='ignore', category=FutureWarning)
Piko
  • 1
  • 2
  • 1
    It seems the warning is happening internally, likely because you are running an older version of BeautifulSoup. I would recommend updating your installed version. Doesn't seem like anything to worry about though, supressing the warning seems like an adequate fix. – B Remmelzwaal Feb 24 '23 at 22:12
  • 1
    Thank you for the reply, but I am not sure if BeautifulSoup is related to BeautifulTable. Here is a link to the docs: https://beautifultable.readthedocs.io/en/latest/index.html That's a link to the Changelog that lists the deprecated functions: https://beautifultable.readthedocs.io/en/latest/changelog.html?highlight=deprecated – Piko Feb 24 '23 at 22:20
  • 1
    Whoops, fully autocompleted the wrong module. My mistake. The same should hold though :) – B Remmelzwaal Feb 24 '23 at 22:21

1 Answers1

0

Don't be nervous and welcome to the community!

I can't recreate your issue directly. The __getitem__ method is known as a dunder or magic method in Python. This method is called when an item is accessed with the self[key] syntax. You can read more about this here.

That being said, I don't get a warning with:

table.rows[0]['A'] = "Oops"

But I can trigger the warning with:

table[0]['A'] = "Oops"

Essentially, in this second code snippet, we are accessing our table row directly with the [0] syntax (which internally, calls __getitem__ directly on the beautifultable object). The warning is notifying us that this method is deprecated and will stop working in v1.2.0 of this package.

To stop the warning, continue to use the syntax table.rows to access the rows or table.columns to access the columns before using the square bracket notation. Read more here.

Good luck with your coding adventure!

Tom
  • 635
  • 7
  • 1
    You are brilliant!! Thank you sooo much! There were a couple of instances in my code where I forgot to add the .rows/.columns and did not realize it. Also, thanks a lot for the explanation. :) I really appreciate it. – Piko Feb 24 '23 at 23:16