Mar-02-2019, 03:46 PM
Hello there,
I am parsing an xml file for some data and want to store that in a class.
The data is in the form of 2 tuple floats and the class is supposed to hold a list of 2 tuples.
The class looks like this:
Now an example xml file :
So far so good. If I run this on one file no problemo. Output looks like this:
But if I try to loop over multiple xml files somehow time data from one instance of TimeLabelData - even after I flush() or del the instance - "spills" into another.
Here a 2nd example xml file :
Now onto the output :
It does exist in the 1st t instance/ xml file however.
So my assumption is that I somehow fail to clean up the t instance and my debugger says the same thing. But why does "del t" or "t.flush()" not work the way I expected it to ?
I am parsing an xml file for some data and want to store that in a class.
The data is in the form of 2 tuple floats and the class is supposed to hold a list of 2 tuples.
The class looks like this:
class TimeLabelData: labels = [] def __init__(self,filename): self.name = filename def addLabelPair(self,start,stop): self.labels.append((float(start),float(stop))) def getData(self): return self.labels def getLength(self): if not self.labels: return 0 return max(max(self.labels,key=lambda x:x[1])) def getSilence(self): sound = 0 for l in self.labels: sound+=l[1]-l[0] return self.getLength()-sound def flush(self): self.labels = []Here is the function I use to go trough the xml :
from xml.etree import ElementTree import TimeLabelDataModule def xml_processing(file): """Runs trough the xml(.aup file) to get time labels and sum them """ e = ElementTree.parse(file).getroot() for child in e: if child.tag == "{http://audacity.sourceforge.net/xml/}labeltrack": #found timelabel section t = TimeLabelDataModule.TimeLabelData(file) for label in child: t.addLabelPair(label.attrib['t'],label.attrib['t1']) print(label.attrib['t'],label.attrib['t1']) print(t.getData()) print(t.getLength()) print(t.getSilence()) t.flush() del tYou might see the excessive try to get rid of the data in t, those are desperate tries to solve my problem but later more to that.
Now an example xml file :
Output:<?xml version="1.0" standalone="no" ?>
<!DOCTYPE project PUBLIC "-//audacityproject-1.3.0//DTD//EN" "http://audacity.sourceforge.net/xml/audacityproject-1.3.0.dtd" >
<project xmlns="http://audacity.sourceforge.net/xml/" projname="01-AudioTrack_data" version="1.3.0" audacityversion="2.3.1-alpha-Jan 30 2019" sel0="0.0000000000" sel1="17.7371428571" vpos="0" h="0.0000000000" zoom="86.1328125000" rate="44100.0" snapto="off" selectionformat="hh:mm:ss + milliseconds" frequencyformat="Hz" bandwidthformat="octaves">
<tags>
<tag name="TITLE" value="Audio Track"/>
<tag name="TRACKNUMBER" value="1"/>
</tags>
<wavetrack name="01-AudioTrack" channel="2" linked="0" mute="0" solo="0" height="150" minimized="0" isSelected="1" rate="44100" gain="1.0" pan="0.0" colorindex="0">
<waveclip offset="0.00000000" colorindex="0">
<sequence maxsamples="262144" sampleformat="262159" numsamples="782208">
<waveblock start="0">
<simpleblockfile filename="e08089b3.au" len="262144" min="-0.359103" max="0.546143" rms="0.03846"/>
</waveblock>
<waveblock start="262144">
<simpleblockfile filename="e080893c.au" len="262144" min="-0.421618" max="0.69021" rms="0.057396"/>
</waveblock>
<waveblock start="524288">
<simpleblockfile filename="e080879a.au" len="257920" min="-0.496488" max="0.669844" rms="0.049167"/>
</waveblock>
</sequence>
<envelope numpoints="0"/>
</waveclip>
</wavetrack>
<labeltrack name="Label Track" numlabels="15" height="73" minimized="0" isSelected="0">
<label t="0.5712300000" t1="1.0100000000" title="1"/>
<label t="2.1700000000" t1="2.5900000000" title="2"/>
<label t="2.8800000000" t1="3.5900000000" title="3"/>
<label t="3.7000000000" t1="3.7100000000" title="4"/>
<label t="4.4900000000" t1="4.5200000000" title="5"/>
<label t="5.1400000000" t1="5.7100000000" title="6"/>
<label t="5.9200000000" t1="7.2300000000" title="7"/>
<label t="7.4700000000" t1="8.3100000000" title="8"/>
<label t="8.7700000000" t1="12.8600000000" title="9"/>
<label t="13.0400000000" t1="13.0500000000" title="10"/>
<label t="13.2800000000" t1="13.2900000000" title="11"/>
<label t="13.4600000000" t1="13.5100000000" title="12"/>
<label t="13.7300000000" t1="14.5400000000" title="13"/>
<label t="14.8500000000" t1="17.6000000000" title="14"/>
<label t="17.7500000000" t1="17.7500000000" title="[End]"/>
</labeltrack>
</project>
Output:C:\Users\1337\Desktop\marie\PostProjects\01-AudioTrack.aup
0.5712300000 1.0100000000
2.1700000000 2.5900000000
2.8800000000 3.5900000000
3.7000000000 3.7100000000
4.4900000000 4.5200000000
5.1400000000 5.7100000000
5.9200000000 7.2300000000
7.4700000000 8.3100000000
8.7700000000 12.8600000000
13.0400000000 13.0500000000
13.2800000000 13.2900000000
13.4600000000 13.5100000000
13.7300000000 14.5400000000
14.8500000000 17.6000000000
17.7500000000 17.7500000000
[(0.57123, 1.01), (2.17, 2.59), (2.88, 3.59), (3.7, 3.71), (4.49, 4.52), (5.14, 5.71), (5.92, 7.23), (7.47, 8.31), (8.77, 12.86), (13.04, 13.05), (13.28, 13.29), (13.46, 13.51), (13.73, 14.54), (14.85, 17.6), (17.75, 17.75)]
17.75
5.701229999999999
Everything's fine.But if I try to loop over multiple xml files somehow time data from one instance of TimeLabelData - even after I flush() or del the instance - "spills" into another.
Here a 2nd example xml file :
Output:<?xml version="1.0" standalone="no" ?>
<!DOCTYPE project PUBLIC "-//audacityproject-1.3.0//DTD//EN" "http://audacity.sourceforge.net/xml/audacityproject-1.3.0.dtd" >
<project xmlns="http://audacity.sourceforge.net/xml/" projname="02-AudioTrack_data" version="1.3.0" audacityversion="2.3.1-alpha-Jan 30 2019" sel0="0.0000000000" sel1="17.6587755102" vpos="0" h="0.0000000000" zoom="86.1328125000" rate="44100.0" snapto="off" selectionformat="hh:mm:ss + milliseconds" frequencyformat="Hz" bandwidthformat="octaves">
<tags>
<tag name="TITLE" value="Audio Track"/>
<tag name="TRACKNUMBER" value="2"/>
</tags>
<wavetrack name="02-AudioTrack" channel="2" linked="0" mute="0" solo="0" height="150" minimized="0" isSelected="1" rate="44100" gain="1.0" pan="0.0" colorindex="0">
<waveclip offset="0.00000000" colorindex="0">
<sequence maxsamples="262144" sampleformat="262159" numsamples="778752">
<waveblock start="0">
<simpleblockfile filename="e080886c.au" len="262144" min="-0.303151" max="0.584204" rms="0.059098"/>
</waveblock>
<waveblock start="262144">
<simpleblockfile filename="e0808c13.au" len="262144" min="-0.311088" max="0.660014" rms="0.049132"/>
</waveblock>
<waveblock start="524288">
<simpleblockfile filename="e08087d8.au" len="254464" min="-0.378143" max="0.584757" rms="0.06111"/>
</waveblock>
</sequence>
<envelope numpoints="0"/>
</waveclip>
</wavetrack>
<labeltrack name="Label Track" numlabels="9" height="73" minimized="0" isSelected="0">
<label t="0.0600000000" t1="4.4300000000" title="1"/>
<label t="4.7400000000" t1="6.3100000000" title="2"/>
<label t="6.9900000000" t1="7.4000000000" title="3"/>
<label t="7.7300000000" t1="10.4700000000" title="4"/>
<label t="10.9500000000" t1="11.0100000000" title="5"/>
<label t="11.3800000000" t1="13.7600000000" title="6"/>
<label t="14.3100000000" t1="14.3800000000" title="7"/>
<label t="14.7400000000" t1="17.6700000000" title="8"/>
<label t="17.6700000000" t1="17.6700000000" title="[End]"/>
</labeltrack>
</project>
Output:C:\Users\1337\Desktop\marie\PostProjects\01-AudioTrack.aup
0.5712300000 1.0100000000
2.1700000000 2.5900000000
2.8800000000 3.5900000000
3.7000000000 3.7100000000
4.4900000000 4.5200000000
5.1400000000 5.7100000000
5.9200000000 7.2300000000
7.4700000000 8.3100000000
8.7700000000 12.8600000000
13.0400000000 13.0500000000
13.2800000000 13.2900000000
13.4600000000 13.5100000000
13.7300000000 14.5400000000
14.8500000000 17.6000000000
17.7500000000 17.7500000000
[(0.57123, 1.01), (2.17, 2.59), (2.88, 3.59), (3.7, 3.71), (4.49, 4.52), (5.14, 5.71), (5.92, 7.23), (7.47, 8.31), (8.77, 12.86), (13.04, 13.05), (13.28, 13.29), (13.46, 13.51), (13.73, 14.54), (14.85, 17.6), (17.75, 17.75)]
17.75
5.701229999999999
C:\Users\1337\Desktop\marie\PostProjects\02-AudioTrack.aup
0.0600000000 4.4300000000
4.7400000000 6.3100000000
6.9900000000 7.4000000000
7.7300000000 10.4700000000
10.9500000000 11.0100000000
11.3800000000 13.7600000000
14.3100000000 14.3800000000
14.7400000000 17.6700000000
17.6700000000 17.6700000000
[(0.57123, 1.01), (2.17, 2.59), (2.88, 3.59), (3.7, 3.71), (4.49, 4.52), (5.14, 5.71), (5.92, 7.23), (7.47, 8.31), (8.77, 12.86), (13.04, 13.05), (13.28, 13.29), (13.46, 13.51), (13.73, 14.54), (14.85, 17.6), (17.75, 17.75), (0.06, 4.43), (4.74, 6.31), (6.99, 7.4), (7.73, 10.47), (10.95, 11.01), (11.38, 13.76), (14.31, 14.38), (14.74, 17.67), (17.67, 17.67)]
17.75
-8.828770000000006
As you can see parsing the 1st file works perfectly fine and the 2nd almost aswell. But the tuple (17.75, 17.75) appears in the 2nd t instance but doesnt in the xml file neither does the output suggest that it got added to said t instance.It does exist in the 1st t instance/ xml file however.
So my assumption is that I somehow fail to clean up the t instance and my debugger says the same thing. But why does "del t" or "t.flush()" not work the way I expected it to ?