<commit_before>from zipfile import ZipFile
from cStringIO import StringIO

import lxml.etree


class ODFFile(ZipFile):
    def __init__(self, file, mode='r', compression=0, allowZip64=False):
        ZipFile.__init__(self, file, mode='r', compression=0, allowZip64=False)
        self.__unchanged = self.namelist()
        self.__updated = {}

    def get_xml(self, name):
        fp  = self.open('%s.xml' % name)
        return lxml.etree.parse(fp)

    def set_xml(self, name, tree):
        name = '%s.xml' % name
        if name in self.__unchanged:
            self.__unchanged.remove(name)
        if tree is not None:
            self.__updated[name] = tree

    def save_changes(self, fp=None):
        # TODO: fails second time called, WHY?
        if fp is None:
            fp = StringIO()
        zo = ZipFile(fp, mode='w')
        updated = dict(self.__updated)
        for zinfo in self.infolist():
            name = zinfo.filename
            # just copy the unchanged
            if name in self.__unchanged:
                zo.writestr(zinfo, self.read(zinfo))
            # write the changed in the same order
            elif name in updated:
                s = lxml.etree.tostring(updated.pop(name))
                zo.writestr(name, s)
        # append the possible remaining (new?)
        for name, tree in updated.items():
            s = lxml.etree.tostring(tree)
            zo.writestr(name, s)
        zo.close()
        return fp<commit_msg>A bit of changes to properly initialize and reuse some attributes<commit_after>from zipfile import ZipFile
from cStringIO import StringIO

import lxml.etree


class ODFFile(ZipFile):
    def __init__(self, file, mode='r', compression=0, allowZip64=False):
        ZipFile.__init__(self, file, mode=mode, \
                compression=compression, allowZip64=allowZip64)
        self.__unchanged = self.namelist()
        self.__updated = {}

    def get_xml(self, name):
        fp  = self.open('%s.xml' % name)
        return lxml.etree.parse(fp)

    def set_xml(self, name, tree):
        name = '%s.xml' % name
        if name in self.__unchanged:
            self.__unchanged.remove(name)
        if tree is not None:
            self.__updated[name] = tree

    def save_changes(self, fp=None):
        # TODO: fails second time called, WHY?
        if fp is None:
            fp = StringIO()
        zo = ZipFile(fp, mode='w', compression=self.compression)
        updated = dict(self.__updated)
        for zinfo in self.infolist():
            name = zinfo.filename
            # just copy the unchanged
            if name in self.__unchanged:
                zo.writestr(zinfo, self.read(zinfo))
            # write the changed in the same order
            elif name in updated:
                s = lxml.etree.tostring(updated.pop(name))
                zo.writestr(name, s)
        # append the possible remaining (new?)
        for name, tree in updated.items():
            s = lxml.etree.tostring(tree)
            zo.writestr(name, s)
        zo.close()
        return fp
{'fraction_non_alphanumeric': 0.10130718954248366, 'fraction_numerical': 0.004248366013071895, 'mean_word_length': 2.0039254170755645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7420063', 'n_tokens_mistral': 934, 'n_tokens_neox': 900, 'n_words': 252}