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

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

Open
alejocp00 opened this issue Oct 26, 2022 · 4 comments
Labels
question Further information is requested

Comments

@alejocp00
Copy link

alejocp00 commented Oct 26, 2022

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)
@alejocp00 alejocp00 added the question Further information is requested label Oct 26, 2022
@hugovk hugovk changed the title PryttyTable 3.4.1 KeyError in _stringify_header + self._justify(fieldname, width, self._align[field]) PrettyTable 3.4.1 KeyError in _stringify_header + self._justify(fieldname, width, self._align[field]) Oct 26, 2022
@hugovk hugovk transferred this issue from jazzband/help Oct 26, 2022
@hugovk
Copy link
Member

hugovk commented Oct 26, 2022

[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.

@alejocp00
Copy link
Author

alejocp00 commented Oct 26, 2022

[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)

@alejocp00 alejocp00 reopened this Oct 26, 2022
@hugovk
Copy link
Member

hugovk commented Oct 26, 2022

There's no code there :)

@alejocp00
Copy link
Author

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

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

No branches or pull requests

2 participants