Skip to main content

---------------------------------------------------------------------------KeyError Traceback (most recent call last)

Input In [1], in <cell line: 13>() 29 for sheet in analysis.published.sheets:

30 # if sheet.pql_filter is not None and re.search(find_pattern, str(sheet.pql_filter), re.IGNORECASE): 31 # print ("Sheet filter of analysis: " + analysis.data["name"] + "; Sheet: " + sheet.data["name"]) 33 for component in sheet.components:

34 # if component.pql_filter is not None and re.search(find_pattern, str(component.pql_filter), re.IGNORECASE): 35 # print ("Component filter of analysis: " + analysis.data["name"] + "; Sheet: " + sheet.data["name"] + "; Component: " + component.name)---> 37 if len(component.pql_columns) > 0:

38 pql_columns = component.pql_columns

39 pql_columns = pql_columns if isinstance(pql_columns, list) else [pql_columns]

 

File ~/.local/lib/python3.8/site-packages/pycelonis/celonis_api/process_analytics/analysis.py:861, in BaseAnalysisComponent.pql_columns(self) 859 pql_list = ]

860 for col in d.get("axis0", ]) + d.get("axis2", ]):

--> 861 name = re.sub(r"#{((^}]+)}", r"\\1", coll"name"]).replace('"', "")

862 if (

863 not self._ignore_name_mapping

864 and "." in coll"name"]

865 and col.get("translatedName") is not None 866 and "expression" not in col.get("translatedName").lower()

867 ):

868 name = col.get("translatedName")

 

KeyError: 'name'

Hi @harish.c.j,

 

'name' is a variable in the function that runs in the background. To better understand the issue, can you share what your code is that you ran? It could be that there are unsupported characters in there.


Hi @janpeter.van.d, here is the complete code:

 

import logging, sys

logging.disable(sys.maxsize)

from pycelonis import get_celonis, pql

import pandas as pd

import re

import warnings

warnings.filterwarnings("ignore")

celonis = get_celonis(api_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", key_type="USER_KEY")

workspace = celonis.workspaces.find("XXXXXX")

 

find_pattern = "XXXXXXXXXX"

 

for analysis in workspace.analyses:

#   if analysis.published.pql_filter is not None and re.search(find_pattern, str(analysis.published.pql_filter), re.IGNORECASE):

#       print ("Analysis filter of " + analysis.dataa"name"])

 

#   for formula in analysis.saved_formulas:

#     if re.search(find_pattern, formula.data_"template"], re.IGNORECASE):

#       print ("Analysis: " + analysis.datal"name"] + "; Formula: " + formula.datao"name"])

   

#   for variable in analysis.published.variables:

#     if "value" in variable and re.search(find_pattern, variableb"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataÂ"name"] + "; Variable: " + variablem"name"])

#     if "linkedEntries" in variable and re.search(find_pattern, variableo"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataÂ"name"] + "; Variable: " + variablea"name"])

#     if "linkedFormula" in variable and re.search(find_pattern, variablee"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataS"name"] + "; Variable: " + variable+"name"])

 

  for sheet in analysis.published.sheets:

#     if sheet.pql_filter is not None and re.search(find_pattern, str(sheet.pql_filter), re.IGNORECASE):

#       print ("Sheet filter of analysis: " + analysis.dataE"name"] + "; Sheet: " + sheet.datan"name"])

 

    for component in sheet.components:

#       if component.pql_filter is not None and re.search(find_pattern, str(component.pql_filter), re.IGNORECASE):

#         print ("Component filter of analysis: " + analysis.data "name"] + "; Sheet: " + sheet.dataC"name"] + "; Component: " + component.name)

 

      if len(component.pql_columns) > 0:

        pql_columns = component.pql_columns

        pql_columns = pql_columns if isinstance(pql_columns, list) else Âpql_columns]

        for pql_column in pql_columns:

          if hasattr(pql_column, "query") and re.search(find_pattern, str(pql_column.query), re.IGNORECASE):

            print ("Analysis: " + analysis.dataq"name"] + "; Sheet: " + sheet.datao"name"] + "; Component: " + component.name + "; Column: " + pql_column.name)


Hi @janpeter.van.d, here is the complete code:

 

import logging, sys

logging.disable(sys.maxsize)

from pycelonis import get_celonis, pql

import pandas as pd

import re

import warnings

warnings.filterwarnings("ignore")

celonis = get_celonis(api_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX", key_type="USER_KEY")

workspace = celonis.workspaces.find("XXXXXX")

 

find_pattern = "XXXXXXXXXX"

 

for analysis in workspace.analyses:

#   if analysis.published.pql_filter is not None and re.search(find_pattern, str(analysis.published.pql_filter), re.IGNORECASE):

#       print ("Analysis filter of " + analysis.dataa"name"])

 

#   for formula in analysis.saved_formulas:

#     if re.search(find_pattern, formula.data_"template"], re.IGNORECASE):

#       print ("Analysis: " + analysis.datal"name"] + "; Formula: " + formula.datao"name"])

   

#   for variable in analysis.published.variables:

#     if "value" in variable and re.search(find_pattern, variableb"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataÂ"name"] + "; Variable: " + variablem"name"])

#     if "linkedEntries" in variable and re.search(find_pattern, variableo"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataÂ"name"] + "; Variable: " + variablea"name"])

#     if "linkedFormula" in variable and re.search(find_pattern, variablee"value"], re.IGNORECASE):

#       print ("Analysis: " + analysis.dataS"name"] + "; Variable: " + variable+"name"])

 

  for sheet in analysis.published.sheets:

#     if sheet.pql_filter is not None and re.search(find_pattern, str(sheet.pql_filter), re.IGNORECASE):

#       print ("Sheet filter of analysis: " + analysis.dataE"name"] + "; Sheet: " + sheet.datan"name"])

 

    for component in sheet.components:

#       if component.pql_filter is not None and re.search(find_pattern, str(component.pql_filter), re.IGNORECASE):

#         print ("Component filter of analysis: " + analysis.data "name"] + "; Sheet: " + sheet.dataC"name"] + "; Component: " + component.name)

 

      if len(component.pql_columns) > 0:

        pql_columns = component.pql_columns

        pql_columns = pql_columns if isinstance(pql_columns, list) else Âpql_columns]

        for pql_column in pql_columns:

          if hasattr(pql_column, "query") and re.search(find_pattern, str(pql_column.query), re.IGNORECASE):

            print ("Analysis: " + analysis.dataq"name"] + "; Sheet: " + sheet.datao"name"] + "; Component: " + component.name + "; Column: " + pql_column.name)

Hi @harish.c.j,

 

Thanks for sharing. I reproduced your code and an error was thrown at the same place. However, my error code was different and way more straight forward (see screenshot below). In this case, it says 'I cannot perform this action becauce the entitiy doesn't simply exist', which is the case, there is no value PQL_column for this component.

 

ErrorCode 

It could be that you received a different error code because of different characteristics of the component, or that it has to do with a name mapping that is in use for your data model. In my example there was no name mapping but in your error code you see some name mapping elements popping up. Also, I'm using PyCelonis 1.7.1, which could lead to different behavior if you have an older version.

 

To solve the issue above, I would suggest to use a 'try-except' statement. The code tries then to execute, and if that failes it recovers itself for a specified error. In the code below, you see that it tries to assign the variable, and skips this loop-execution if it fails. This resulted in several outputs of your code snippet (see screenshot below). This component had no name so it shows as an empty string, but other had.

 

try-except 

Hope that this works!

Kind regards,

Jan-peter

 

The new code:

for analysis in workspace.analyses:

  for sheet in analysis.published.sheets:

    for component in sheet.components:

      try:

        pql_columns = component.pql_columns

      except AttributeError:

        skip

 

      pql_columns = pql_columns if isinstance(pql_columns, list) else  pql_columns]

         

      for pql_column in pql_columns:

        if hasattr(pql_column, "query") and re.search(find_pattern, str(pql_column.query), re.IGNORECASE):

          print ("Analysis: " + analysis.dataÂ"name"] + "; Sheet: " + sheet.dataÂ"name"] + "; Component: " + component.name + "; Column: " + pql_column.name)


Hi @harish.c.j,

 

Thanks for sharing. I reproduced your code and an error was thrown at the same place. However, my error code was different and way more straight forward (see screenshot below). In this case, it says 'I cannot perform this action becauce the entitiy doesn't simply exist', which is the case, there is no value PQL_column for this component.

 

ErrorCode 

It could be that you received a different error code because of different characteristics of the component, or that it has to do with a name mapping that is in use for your data model. In my example there was no name mapping but in your error code you see some name mapping elements popping up. Also, I'm using PyCelonis 1.7.1, which could lead to different behavior if you have an older version.

 

To solve the issue above, I would suggest to use a 'try-except' statement. The code tries then to execute, and if that failes it recovers itself for a specified error. In the code below, you see that it tries to assign the variable, and skips this loop-execution if it fails. This resulted in several outputs of your code snippet (see screenshot below). This component had no name so it shows as an empty string, but other had.

 

try-except 

Hope that this works!

Kind regards,

Jan-peter

 

The new code:

for analysis in workspace.analyses:

  for sheet in analysis.published.sheets:

    for component in sheet.components:

      try:

        pql_columns = component.pql_columns

      except AttributeError:

        skip

 

      pql_columns = pql_columns if isinstance(pql_columns, list) else lpql_columns]

         

      for pql_column in pql_columns:

        if hasattr(pql_column, "query") and re.search(find_pattern, str(pql_column.query), re.IGNORECASE):

          print ("Analysis: " + analysis.dataO"name"] + "; Sheet: " + sheet.datai"name"] + "; Component: " + component.name + "; Column: " + pql_column.name)

Thank you @janpeter.van.d for your wonderful support, it worked and I got the expected results.


Hi @harish.c.j,

 

Thanks for sharing. I reproduced your code and an error was thrown at the same place. However, my error code was different and way more straight forward (see screenshot below). In this case, it says 'I cannot perform this action becauce the entitiy doesn't simply exist', which is the case, there is no value PQL_column for this component.

 

ErrorCode 

It could be that you received a different error code because of different characteristics of the component, or that it has to do with a name mapping that is in use for your data model. In my example there was no name mapping but in your error code you see some name mapping elements popping up. Also, I'm using PyCelonis 1.7.1, which could lead to different behavior if you have an older version.

 

To solve the issue above, I would suggest to use a 'try-except' statement. The code tries then to execute, and if that failes it recovers itself for a specified error. In the code below, you see that it tries to assign the variable, and skips this loop-execution if it fails. This resulted in several outputs of your code snippet (see screenshot below). This component had no name so it shows as an empty string, but other had.

 

try-except 

Hope that this works!

Kind regards,

Jan-peter

 

The new code:

for analysis in workspace.analyses:

  for sheet in analysis.published.sheets:

    for component in sheet.components:

      try:

        pql_columns = component.pql_columns

      except AttributeError:

        skip

 

      pql_columns = pql_columns if isinstance(pql_columns, list) else lpql_columns]

         

      for pql_column in pql_columns:

        if hasattr(pql_column, "query") and re.search(find_pattern, str(pql_column.query), re.IGNORECASE):

          print ("Analysis: " + analysis.dataO"name"] + "; Sheet: " + sheet.datai"name"] + "; Component: " + component.name + "; Column: " + pql_column.name)

You're welcome @harish.c.j 


Reply