- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 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("<","<").replace(">",">")
def _unescape_xml(self,string):
return string.replace("<","<").replace(">",">")
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)
RaZeR 26.07.2011 12:13 # 0
JeremyW 26.07.2011 12:32 # 0
TheHamstertamer 26.07.2011 13:10 # +2
guest 26.07.2011 15:24 # 0
Lure Of Chaos 26.07.2011 16:01 # +2
Govnocoder#0xFF 26.07.2011 16:04 # 0
Lure Of Chaos 26.07.2011 16:06 # 0
Govnocoder#0xFF 26.07.2011 16:13 # 0
TheHamstertamer 26.07.2011 19:25 # +1
TheHamstertamer 26.07.2011 16:10 # 0
Govnocoder#0xFF 26.07.2011 16:12 # 0
Это Hello World без цикла.
TheHamstertamer 26.07.2011 19:14 # 0
ничего не выводит
TheHamstertamer 26.07.2011 16:12 # 0
guest 26.07.2011 18:48 # −1
По поводу try/except. Обычно это более очевидно чем 5 проверок что вся цепочка элементов существует.
Питон может выглядеть странно:)
guest8 09.04.2019 11:13 # −999