1. Список говнокодов пользователя pythonic

    Всего: 1

  2. Python / Говнокод #7344

    −97

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    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)

    Всем xml, посоны!
    https://github.com/hashme33/BoardToJabber/blob/master/src/main/Parser.py#L24

    pythonic, 26 Июля 2011

    Комментарии (15)