1

I'm pretty new importing data and I´m trying to make def that reads in the presented order. The error is in the fuction, not in the data.

def read_eos(filename: str, order: dict[str, int] = {"density": 0, "pressure": 1, 
    "energy_density": 2}):
  
    # Paths to files
    current_dir = os.getcwd()
    INPUT_PATH = os.path.join(current_dir, "input")

    in_eos = np.loadtxt(os.path.join(INPUT_PATH, filename))
    eos = np.zeros(in_eos.shape)

    # Density
    eos[:, 0] = in_eos[:, order["density"]]
    eos[:, 1] = in_eos[:, order["pressure"]]
    eos[:, 2] = in_eos[:, order["energy_density"]]

    return eos
jjramsey
  • 1,131
  • 7
  • 17
SANTIAGO
  • 13
  • 1
  • 3
  • It would help to (1) have a sample input to test this function, so that you'd have at least something halfway resembling a minimal reproducible example (emphasis on the *reproducible* part), and (2) to have a traceback, not just the last error message that you got. – jjramsey May 13 '22 at 18:23
  • Traceback (most recent call last): File "/Users/santiagopendonminguillon/Desktop/Sin título4.py", line 21, in def read_eos(filename: int, order: dict[int, int] = {"density": 0, "pressure": 1, "energy_density": 2}): TypeError: 'type' object is not subscriptable – SANTIAGO May 13 '22 at 18:31
  • Add the traceback to your original post. Don't put it in a comment. – jjramsey May 13 '22 at 18:38

1 Answers1

2

Looks like the problem is right in the type hint of one of your function parameters: dict[str, int]. As far as Python is concerned, [str, int] is a subscript of the type dict, but dict can't accept that subscript, hence your error message.

The fix is fairly simple. First, if you haven't done so already, add the following import statement above your function definition:

from typing import Dict

Then, change dict[str, int] to Dict[str, int].

jjramsey
  • 1,131
  • 7
  • 17