Lewis909 Lewis909 - 1 year ago 89
Python Question

Python - is it possible to dynamically spawn functions

Is it possible to dynamically spawn functions?

I have written a function that parses this example.xml file and returns the attributes for the segment_1 element as a list.
Now this example contains four segments however this will change for different assets, one could only have 1 segment and another could have 10+.

Here is the example XML it has four segments:

<?xml version="1.0" encoding="utf-8"?>
<manifest task_id="00000000112">
<series_title>test asset 1</series_title>
<season_title>Number 1</season_title>
<episode_title>ET 1</episode_title>
<synopsis>This is a test asset</synopsis>
<segment_1 seg_1_in="00:00:00.000" seg_1_out="00:01:00.000" seg_1_dur="00:01:00.000"/>
<segment_2 seg_2_in="00:02:00.000" seg_2_out="00:03:00.000" seg_2_dur="00:01:00.000"/>
<segment_3 seg_3_in="00:04:00.000" seg_3_out="00:05:00.000" seg_3_dur="00:01:00.000"/>
<segment_4 seg_4_in="00:06:00.000" seg_4_out="00:07:00.000" seg_4_dur="00:01:00.000"/>
<conform_profile definition="hd" aspect_ratio="16f16">ffmpeg -progress LOG_FILE.txt -i S_PATH/F_NAME.mp4 SEG_CONFORM</conform_profile>
<transcode_profile profile_name="amazon" package_type="tar">ffmpeg -safe 0 -progress LOG_FILE.txt -f concat -i T_PATH/CONFORM_LIST TRC_PATH/F_NAME.mp4</transcode_profile>

I have written a 70 line if statement that will process assets with up to 4 segment's and I can always add to that code for assets that have more segments, however that is not very elegant so I am working on a function to dynamically create the segment lists based on this element in the XML:


Ideally it would generate x amount of:

return seg_element(root, path)

in the parse_xml() function based on how many segments the asset has.

Here are the functions:

import xml.etree.ElementTree as et
file = 'example.xml'

# Seg_element
def seg_element(xml_root, element_path):
list_a = []
for elem in xml_root.iterfind(element_path):
a = elem.attrib
for i in a:
list_a.append([i + ' = ' + a[i]])
return list_a

# Parse_xml
def parse_xml(file_input, number_of_segments):
tree = et.parse(file_input)
root = tree.getroot()
path = 'file_info/segment_1'

return seg_element(root, path)

for i in parse_xml(file, 1):

Here is the output:

['seg_1_dur = 00:01:00.000']
['seg_1_in = 00:00:00.000']
['seg_1_out = 00:01:00.000']

Of the name of the list created by each iteration of seg_element() would be different.

The reason for doing this to create an command to submit to FFMPEG, here is the 70 line if statement that creates the command http://pastebin.com/jgawC48Y

Answer Source

There is no need to generate functions, you just want to do a loop over the number of segments, and call the seg_element function accordingly:

def parse_xml(file_input, number_of_segments):
    tree = et.parse(file_input)
    root = tree.getroot()
    segments_no = int(root.find('file_info/number_of_segments').text)
    segments = []   

    for i in range(segments_no):
        path = 'file_info/segment_%d' % (i+1)
        segments.append(seg_element(root, path))

    return segments

(code above is not tested, but I think you'll get it)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download