What I’m trying to do:
I am using two server callable functions:
- ‘tod_preprocess’
- ‘yaml_process’
The subject of this topic is the second function, ‘yaml_process’ which essentially uses an output from function 1 and then proceeds to manipulate a YAML file uploaded by the user. For brevity, I will only include the applicable portion of the ‘yaml_process’ function where the error is occurring:
import anvil.media
@anvil.server.callable
def yaml_process(file, unq_act_ser):
unq_act_deser = json.loads(unq_act_ser)
#importing the yaml
yml_file = file
import ruamel.yaml
def unique_lists(lst_of_lsts): #defining unique lists for unq_act so that we iterate over them and create plans.
unique = set()
result = []
for lst in lst_of_lsts:
if tuple(lst) not in unique:
unique.add(tuple(lst))
result.append(lst)
return result
unq_act_patt = unique_lists(unq_act_deser)
# defining function that finds source pattern
def subtract_30(number):
while number >= 30:
number -= 30
return number
# Create a new YAML object
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True # added in ruamel.yaml
yaml.explicit_start = True # control also available in PyYAML
yaml.explicit_end = True # control also available in PyYAML
# Open the YAML file in read-write mode
with open(yml_file[0], 'r+') as file:
# Load the YAML documents using the ruamel.yaml.safe_load() method
data = list(yaml.load_all(file))
# Get the third document
doc3 = data[2]
#first, ensuring that we wipe out all the existing PFs and SFs since the converter is inputting erroneous data
doc3["etcsAscTBCPhsFunction"] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
doc3["dayPlanHour"]["xy"][0]['1'] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] #deleting TOD entries created by converter (dp 1/2)
doc3["dayPlanHour"]["xy"][1]['2'] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] #deleting TOD entries created by converter
doc3["dayPlanActionNumberOID"]['xy'][0]['1'] = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], []] #deleting TOD actions created by converter (dp 1/2)
doc3["dayPlanActionNumberOID"]['xy'][1]['2'] = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
What I’ve tried and what’s not working:
I receive this error when trying to call the function from the client sde:
TypeError: ‘StreamingMedia’ object is not subscriptable
at C:/Users/hossa/AppData/Local/Temp/ipykernel_2488/1857508715.py:38
called from Form1, line 24
I believe I am not interacting correctly with the Media Object. Speifically, it looks like I can’t subscript a media object although it is of text type that can be subscripted. I would appreciate any pointers, thank you.
Clone link:
share a copy of your app