jazzband / prettytable

Display tabular data in a visually appealing ASCII table format

Home Page:https://pypi.org/project/PrettyTable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PrettyTable 3.4.1 KeyError in _stringify_header + self._justify(fieldname, width, self._align[field])

alejocp00 opened this issue · comments

Is this my error or from PrettyTable?

What did you do?

  • Try to print my PrettyTable instance

What did you expect to happen?

  • Get a string value similar to
+-----+-------------+-------------+-------+-------------+
|  x  | y (h=val_0) | y (h=val_1) |  ...  | y (h=val_n) |
+-----+-------------+-------------+-------+-------------+
| x_0 |     y_0     |     y_1     |  ...  |     y_n     |
+-----+-------------+-------------+-------+-------------+
| x_1 |     y_0     |     y_1     |  ...  |     y_n     |
+-----+-------------+-------------+-------+-------------+
| ... |     ...     |     ...     |  ...  |     ...     |
+-----+-------------+-------------+-------+-------------+
| x_k |     y_0     |     y_1     |  ...  |     y_n     |
+-----+-------------+-------------+-------+-------------+

What actually happened?

Traceback (most recent call last):
  File "/home/alejandro/Desktop/test.py", line 236, in <module>
    Create_Table(values)
  File "/home/alejandro/Desktop/test.py", line 205, in Create_Table
    print(table)
  File "/home/alejandro/.local/lib/python3.10/site-packages/prettytable/prettytable.py", line 327, in __str__
    return self.get_string()
  File "/home/alejandro/.local/lib/python3.10/site-packages/prettytable/prettytable.py", line 1717, in get_string
    lines.append(self._stringify_header(options))
  File "/home/alejandro/.local/lib/python3.10/site-packages/prettytable/prettytable.py", line 1854, in _stringify_header
    + self._justify(fieldname, width, self._align[field])
KeyError: 'y (h=0.1)'

What versions are you using?

  • OS: Ubuntu 22.04
  • Python: 3.10
  • PrettyTable: 3.4.1

Please include code that reproduces the issue.

The best reproductions
are
self-contained scripts
with minimal dependencies.

from prettytable import PrettyTable

# values is a dict, where keys are floats and values are lists of tuples
def Create_Table(values: dict):

    table = PrettyTable()

    table.field_names = ["x"]

    for x in values:
        row_temp = [x]

        for y_h in values[x]:
           # add y value
            row_temp.append(y_h[0])
            column_name = "y (h={})".format(str(y_h[1]))
            #check if the value is currently added to names
            if column_name not in table.field_names:
                table.field_names.append(column_name)
        table.add_row(row_temp)

    print(table)

[I transferred this issue from https://github.com/jazzband/help to the PrettyTable repo]

How are you calling Create_Table? Please provide a self-contained script.

[I transferred this issue from https://github.com/jazzband/help to the PrettyTable repo]

How are you calling Create_Table? Please provide a self-contained script.

Hi 👋 , this is the full code that produce the error.

This method return a list with float values

def Euler_Method(function, x, y, max, h, d):

    coordinates = []

    while(Less_Than(x, max) or Equal_To(x, max)):

        # Save values
        coordinates.append(round(y,d))

        # Update y value
        y = y+h*function(x, y)

        # Update x value
        x = x+h

    return coordinates

I use this one as to process any function whit the selected method.

  • This is the one who returns the Create_Table parameter: { float: [ ( float , float ) ] }
  • I post just the Euler method because I'm getting the same issue with the others two.
import numpy as np
def Calculate(function, y, min, max, h_values, method, d, n):
    
        result = {}

        # Calculate x values
        x_values = np.linspace(min, max, num=n)
        
        # Calculate y values
        y_value = y
        for x in x_values[1:]:
            # Here will all pairs of y and h values
            y_h_list = []
           
            # Calculate y value of the current x value according to each one h vale 
            for h in h_values:
                
                # Selecting the method to use 
                if method == Method.EULER:
                    y_value = Euler_Method(function, x, y, max, h, d)[-1]
                elif method == Method.EULER_IMPROVED:
                    y_value = Euler_Method_Improved(function, x, y, max, h, d)[-1]
                elif method == Method.RUNGE_KUTTA:
                    y_value = Runge_Kutta_Method(function, x, y, max, h, d)[-1]
                
                y_h_list.append((y_value, h))

            result[x] = y_h_list

        return result

And this is the method that I post before.

I don't include anything related with the 'method' parameter, because I got the same error with or with out it.

from prettytable import PrettyTable

def Create_Table(values: dict, method):
        
    table = PrettyTable()
    
    # Selecting table title
    if method == Method.EULER:
        table.title = "Euler Method"
    elif method == Method.EULER_IMPROVED:
        table.title = "Euler Improved Method"
    elif method == Method.RUNGE_KUTTA:
        table.title = "Runge-Kutta Method"
    
    # Initialize the field names with x tag
    table.field_names = ["x"] 
    
    # Adding a row for each x value, stored as keys
    for x in values:
        # The first value of the row will be always the x value
        row_temp = [x]
        
        for y_h in values[x]:
            # Add y value
            row_temp.append(y_h[0])

            column_name = "y (h={})".format(str(y_h[1]))

            # Check if I need to add the current y(h) to the columns
            if column_name not in table.field_names:
                table.field_names.append(column_name)

        # Add the full row created to the table
        table.add_row(row_temp)
            
    print(table)

I'm doing something like

# Define function
def function(x, y):
    return x/(1+y**2)

values = []

# Define initial values
x = -1
y = 1
max = 1

# Define h values
h_list = [0.1, 0.02, 0.004, 0.0008]

# Define number of decimals
d = 5

# Define number of equidistant points
n = 10

# Calculate values
values = Calculate(function, y, x, max, h_list, Method.EULER, d, n)

# Create table.
Create_Table(values, Method.EULER)

There's no code there :)

There's no code there :)

Sorry for that, my pc don't handle very well the web explorer open with the vscode at the same time, and I accidentally click Close with comment