YAML file as a Media Object

What I’m trying to do:
I am using two server callable functions:

  1. ‘tod_preprocess’
  2. ‘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,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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, 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

A media object is not a text type, it’s a media object. See Anvil Docs | anvil for the API available.

I suspect what you want is to use get_bytes to pull the text out of the media object, and then process that.

1 Like

Thank you. I am trying to find the correct way to call the get_bytes() method. I tried using

media.get_bytes(file)

But it obviously doesn’t work. How do I properly call this method?

Thanks,
Hossam

You call it on whatever object is the media object. The object you were trying to subscript that gave you the error.

1 Like