Mistři v programování na #hovnokod

Python #432

class Thread:
    ... other methods ...
    def SerializeXml(self,f):
        sp = sorted(self.postnumbers)
        f.write("<thread><number>%d</number>\n<posts>"%self.GetNumber())
        for i in sorted(self.postnumbers):
            f.write(self.posts[self.postnumbers_dict[i]].SerializeXml()+"\n")
        f.write("</posts></thread>")
    def CreatePost(self):
        return Parser.Post()
    def DeserializeXml(self,value):
        try:
            try:
                self.number = int(re.findall(r"""<number>(\d+)</number>""",value)[0])
            except :
                self.number = 0
            posts = re.findall(r"""<post>(.+?)</post>""",value,re.DOTALL)
            for p in posts:
                po = self.CreatePost()
                po.DeserializeXml(p)
                self.posts.append(po)
                self.postnumbers_dict[po.GetNumber()] = len(self.posts)-1
                self.postnumbers.add(po.GetNumber())
            return True
        except:
            return False
class Post:
    ... other methods ...
    def safexml(self,string):
        return string.replace("<","&lt;").replace(">","&gt;")
    def _unescape_xml(self,string):
        return string.replace("&lt;","<").replace("&gt;",">")
    def SerializeXml(self):
        return """<post><postnumber>%d</postnumber>\n<sage>%d</sage>\n<deleted>%d</deleted>\n<subj>%s</subj>\n<name>%s</name>\n<trip>%s</trip>\n<attachments>%s</attachments>\n<comment>%s</comment></post>"""% \
    (self.GetNumber(),self.GetSage(),self.GetDeleted(),self.safexml(self.GetSubject()),self.safexml(self.GetPostername()),self.safexml(self.GetPostertrip()), \
     "\n".join(["<attachment><filename>%s</filename><filelink>%s</filelink></attachment>"%(l,self.safexml(n)) for ((n,l)) in self.attachments]),(self.safexml(self.GetComment())))
    def DeserializeXml(self,string):
        try:
            self.number = int(self._GetVal(string,"postnumber")[0])
        except:
            self.number = None
        ...
        try:
            self.postertrip = self._unescape_xml(self._GetVal(string,"trip")[0])
        except:
            self.postertrip = None
        try:
            self.comment = self._unescape_xml(self._GetVal(string,"comment")[0])
        except:
            self.comment = None
        self.attachments = []
        try:
            string = self._GetVal(string,"attachments")[0]
            string = self._GetVal(string,"attachment")
            for att in string:
                (l,n) = re.findall(r"""<filename>(.+?)</filename><filelink>(.+)</filelink>""",att,re.DOTALL)[0]
                self.attachments.append((urllib2.quote(n),n))
        except :
            pass
                
    def _GetVal(self,string,value):
        return re.findall(r"""<%s>(.*?)</%s>"""%(value,value),string,re.DOTALL)

HovnoKod,