Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mikel

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

XTG generator

// description of your code here

   1  
   2      def getAcceptedClassifieds(self, session):
   3      
   4          context = utils.context(self)
   5          
   6          ret = []
   7          for elem in session:
   8              if elem['status'] == 'True':
   9                  el = getattr(context, elem['id'].split('/')[-1], None)
  10                  if el is not None:
  11  
  12                      yield el
  13                      
  14  
  15  
  16      def __call__(self, *args, **kwargs):
  17          """ call method """
  18          from StringIO import StringIO
  19          from logging import getLogger
  20          log = getLogger('xtg')
  21  
  22          
  23          context = utils.context(self)
  24          request = self.request
  25          session = request.SESSION
  26          response = request.response
  27  
  28          xtg = StringIO()
  29          xtg.write('<v2.00><e1>\r')
  30  
  31          
  32          classifieds = self.getAcceptedClassifieds(request.form['ids'])
  33          class2 = self.getAcceptedClassifieds(request.form['ids'])
  34      
  35  
  36          import time
  37  
  38          # NEW CODE
  39          from sailkatuak import prepareBrainsForDict
  40          start = time.time()
  41          classified_list = prepareBrainsForDict(classifieds, obj=1)
  42          xtg_new = self.convertToXtg(classified_list)
  43          end = time.time()
  44          log.info('Berria: %s' % str(end - start))
  45          
  46  
  47          # OLD CODE
  48          def ordena (x,y):
  49              if x.getSaila()==y.getSaila():
  50                  return cmp(self.xtg_testua(x).lower(), self.xtg_testua(y).lower())
  51              else:
  52                  return cmp(x.getSaila(), y.getSaila())
  53  
  54          # We need the element in a list. Sort doesn't
  55          # generators cannot be sorted
  56          classs = list(class2)
  57          start = time.time()
  58          classs.sort(ordena)
  59          for classified in classs:
  60              classified_xtg = self.getClassifiedXtg(classified)
  61              xtg.write(classified_xtg)
  62          end = time.time()
  63          log.info('Zaharra: %s' % str(end - start))
  64  
  65  
  66      def convertToXtg(self, classifieds):
  67          def sailarenIzena(datua):
  68              return datua.split('-')
  69  
  70          from StringIO import StringIO
  71          xtg = StringIO()
  72          xtg.write('<v2.00><e1>\r')
  73          for section, secinfo in classifieds:
  74              section_s = section.split('-', 1)
  75              xtg.write(GOI_IZENBURU_ESTILOA)
  76              xtg.write(section_s[0])
  77              xtg.write(GOI_IZENBURU_TARTEKO_MARRA)
  78              xtg.write(section_s[1])
  79              xtg.write(LERRO_SALTOA)
  80              for subsection, announces in secinfo:
  81                  subsection_s = subsection.split('-', 1)
  82                  xtg.write(BEHE_IZENBURU_AURRETIK)
  83                  xtg.write(LERRO_SALTOA)
  84                  xtg.write(BEHE_IZENBURU_ESTILOA)
  85                  xtg.write(subsection_s[0])
  86                  xtg.write('. ')
  87                  xtg.write(subsection_s[1])
  88                  xtg.write(BEHE_IZENBURU_BUKAERA)
  89                  xtg.write(LERRO_SALTOA)
  90                  for announce in announces:
  91                      if announces.index(announce) == 0:
  92                          xtg.write(TESTU_LEHEN_LERROA_ESTILOA)
  93                      elif announces.index(announce) == 1:
  94                          xtg.write(TESTU_LERROA_ESTILOA)
  95  
  96                      if announces.index(announce) == 1:
  97                          xtg.write(self.xtg_berria_testua(announce))
  98                      else:
  99                          xtg.write(self.xtg_testua(announce))
 100                          
 101                      xtg.write(LERRO_SALTOA)
 102                                
 103  
 104          return xtg
 105  
 106      def getClassifiedXtg(self, classified):
 107          def sailarenIzena(datua):
 108              return datua.split('-')
 109  
 110          """
 111          self.lehen_lerroa='Bai'
 112          self.ataleko_lehen_subsaila='Bai'
 113          self.goi_izenburu_zaharra='Bai'
 114          self.izenburu_zaharra='Bai'
 115          self.lehena=0
 116          """
 117          
 118          p = ''
 119          sail_izena = sailarenIzena(classified.getSaila())
 120          if self.goi_izenburu_zaharra!=sail_izena[1]:
 121              if self.lehena==1:
 122                  p=p+GOI_IZENBURU_AURRETIK+LERRO_SALTOA
 123  
 124              p=p+GOI_IZENBURU_ESTILOA+sail_izena[0][0]+GOI_IZENBURU_TARTEKO_MARRA+sail_izena[1]+LERRO_SALTOA
 125              self.izenburu_zaharra='Goiena'
 126              self.goi_izenburu_zaharra=sail_izena[1]
 127              self.lehena=1
 128          # Bigarren mailako Sailaren izenburua
 129          if self.izenburu_zaharra!=sail_izena[0]:
 130              p=p+BEHE_IZENBURU_AURRETIK+LERRO_SALTOA
 131              p=p+BEHE_IZENBURU_ESTILOA+sail_izena[0]+'. '+sail_izena[2]+BEHE_IZENBURU_BUKAERA+LERRO_SALTOA
 132              self.izenburu_zaharra=sail_izena[0]
 133              self.lehen_lerroa='1'
 134          # Sailkatuen lehenengo lerroa bada...
 135          if self.lehen_lerroa=='1':
 136              p=p+TESTU_LEHEN_LERROA_ESTILOA+self.xtg_testua(classified)+LERRO_SALTOA
 137              self.lehen_lerroa='2'
 138          # Bigarren lerroa bada
 139          elif self.lehen_lerroa == '2' :
 140              p=p+TESTU_LERROA_ESTILOA+self.xtg_berria_testua(classified)+LERRO_SALTOA
 141              self.lehen_lerroa='3'
 142          #bestela....
 143          else:
 144              p=p+self.xtg_testua(classified)+LERRO_SALTOA
 145          return p
 146  
 147  
 148      def xtg_testua(self, classified):
 149  
 150          titulua = classified.Title()
 151          deskribapena = classified.Description().strip()
 152          kontaktua = classified.getTelephone1()
 153          
 154          testua=''
 155          
 156          if len(deskribapena)>0:
 157              if deskribapena[-1]!='.':
 158                  deskribapena=deskribapena+'.'
 159                  
 160          if len(kontaktua)>0:
 161              if kontaktua[-1]!='.':
 162                  kontaktua=kontaktua+'.'
 163                          
 164          testua='<B>'+titulua+'<$>'+' '+deskribapena+' '+kontaktua
 165                          
 166          return testua
 167  
 168      def xtg_berria_testua(self, classified):
 169  
 170          TESTU_LEHEN_HITZA_ESTILOA='@TXT-Sailaktuak/Lehena/Hitza'
 171  
 172          xtg_t = self.xtg_testua(classified)
 173          xtg_t = xtg_t[3:]
 174          
 175          return TESTU_LEHEN_HITZA_ESTILOA + xtg_t
 176  
 177  
 178  def prepareBrainsForDict(brains, sectionname=0, obj=0):
 179      """ Prepare a dict to get the list we need to return """
 180  
 181      dic = {}
 182      for brain in brains:
 183          if obj:
 184              if sectionname:
 185                  section = brain.Title()
 186              else:
 187                  section = brain.getSaila()
 188  
 189          else:
 190              if sectionname:
 191                  section = brain.Title
 192              else:
 193                  section = brain.getSaila
 194  
 195  
 196          type, subtype, name = section.split('-')
 197  
 198          tip = type[0] + '-' + subtype
 199          subtip = type + '-' + name
 200  
 201          dict_type = dic.get(tip, None)
 202          if dict_type is not None:
 203              dict_type_subtype = dict_type.get(subtip, None)
 204              if dict_type_subtype is not None:
 205                  dic[tip][subtip].append(brain)
 206              else:
 207                  dic[tip][subtip] = [brain]
 208          else:
 209              dic[tip] = {subtip: [brain]}
 210  
 211      ret = []
 212      for k,v in dic.items():
 213          l = []
 214          for i,j in v.items():
 215              l.append((i,j))
 216          ret.append((k,l))
 217  
 218      def sortBySection(c1, c2):
 219          return cmp(c1[0], c2[0])
 220  
 221      ret2 = []
 222      for k,v in ret:
 223          v.sort(sortBySection)
 224          ret2.append((k,v))
 225  
 226      ret2.sort(sortBySection)
 227  
 228      return ret2
 229  
 230  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS