Advertisement
dikkini

Data Quality

Jan 17th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.88 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import wx
  3. import os
  4. import fileinput
  5. import sys
  6.  
  7.  
  8.  
  9. def clearfile(path):
  10.     file = open(path, 'w')
  11.     file.write(u' ')
  12.     file.close()
  13.  
  14. def del_regexp(file, string):
  15.     rst = []
  16.     with open(file) as fd:
  17.         t = fd.read()
  18.         for line in t.splitlines():
  19.             if line != string:
  20.                 rst.append(line)
  21.  
  22.     with open(file, 'w') as fd:
  23.         fd.write(u'\n'.join(rst))
  24.         fd.write(u'\n')
  25.  
  26. def replaceAll(file, searchExp,replaceExp):
  27.     for line in fileinput.input(file, inplace=1):
  28.         if searchExp in line:
  29.             line = line.replace(searchExp,replaceExp)
  30.         sys.stdout.write(line)
  31.  
  32. class StrangeFileWorker(dict):
  33.     def load_file(self, file_name):
  34.         with open(file_name) as fd:
  35.             self.load_string(fd.read())
  36.  
  37.     def load_string(self, data):
  38.         cur_key = ''
  39.         for ln in filter(lambda a: a, data.split('\n')):
  40.             if ln.endswith(':'):
  41.                 cur_key = ln.strip(':')
  42.                 self.setdefault(cur_key, [])
  43.             elif cur_key:
  44.                 self[cur_key].append(ln)
  45.  
  46.     def __repr__(self):
  47.         #return '\n'.join(map(lambda kv: '%s:\n%s\n' % (kv[0], '\n'.join(kv[1])), self.iteritems()))
  48.         return '\n'.join(map(lambda kv: '\n%s:\n%s\n' % (kv[0], '\n'.join(kv[1])), self.iteritems()))
  49.    
  50. def write_regexp(file, table, regexp):
  51.     sfw = StrangeFileWorker()
  52.    
  53.     fd = open(file)
  54.     data = fd.read()
  55.     #data = data.decode('cp1251')
  56.     sfw.load_string(data)
  57.    
  58.     sfw[table].append(regexp)
  59.    
  60.     data = repr(sfw) # получили представление объекта
  61.     #data = data.encode('cp1251') # вспомнили про кодировку
  62.     fd.write(data) # записали
  63.     fd.close()
  64. #===============================================================================
  65. # def write_regexp(file, table, regexp):
  66. #    result = list()
  67. #    sfw = StrangeFileWorker()
  68. #    store = False
  69. #    with open(file) as fd:
  70. #        for line in fd:
  71. #            line = line.strip(' \n\r:')
  72. #            if result and not line:
  73. #                break
  74. #            if line == table:
  75. #                store = True
  76. #                continue
  77. #            if line == ' ':
  78. #                file.write(regexp)
  79. #===============================================================================
  80. def get_regexp(file, table):
  81.     sfw = StrangeFileWorker()
  82.     list = []
  83.     fd = open(file)
  84.     data = fd.read()
  85.     #data = data.decode('cp1251')
  86.     sfw.load_string(data)
  87.     if table in sfw:
  88.         for reg_exp in sfw[table]:
  89.             list.append(reg_exp)
  90.     else:
  91.         list = []
  92.     return list
  93.     data = repr(sfw) # получили представление объекта
  94.     #data = data.encode('cp1251') # вспомнили про кодировку
  95.     fd.write(data) # записали
  96.     fd.close()
  97. #===============================================================================
  98. # def get_regexp(file, table):
  99. #    sfw = StrangeFileWorker()
  100. #    result = list()
  101. #    store = False
  102. #    fd = open(file) # Открыли файл
  103. #    data = fd.read() #прочитали
  104. #    data = data.decode('utf8') #преобразовали кодировку
  105. #    sfw.load_string(data) #скормили в обьект
  106. #    for line in data:
  107. #        line = line.strip(' \n\r:')
  108. #        if result and not line:
  109. #            break
  110. #        if line == table:
  111. #            store = True
  112. #            continue
  113. #        if store:
  114. #            result = repr(sfw) # получили представление объекта
  115. #            result = result.encode('utf8') # вспомнили про кодировку
  116. #            result.append(line)
  117. #    return result
  118. #===============================================================================
  119.  
  120. def readfile(path):
  121.     file = open(path, 'r')
  122.     file = file.readlines()
  123.     for i in xrange(len(file) - 1): file[i] = file[i][0:-1]
  124.     return file
  125.  
  126. def writefile(table, regexp, path):
  127.     file = open(path, 'a')
  128.     file.write((u'\n%s:' % table))
  129.     file.write((u'\n%s' % regexp))
  130.     return file    
  131.    
  132. class dictpick_files(wx.Dialog):
  133.     def __init__(self):          
  134.         wildcard = 'All files (*.*)|*.*'
  135.         self.dialog = wx.FileDialog(None, 'Choose a file', os.getcwd(),
  136.             '', wildcard, wx.OPEN)
  137.         if self.dialog.ShowModal() == wx.ID_OK:
  138.             self.Path = self.dialog.GetPath()
  139.             self.dialog.Destroy()
  140.         self.dialog.Destroy()
  141.            
  142.     def readfile_dlg(self, file, path):
  143.         file = open(path, 'r')
  144.         file = file.readlines()
  145.         for i in xrange(len(file) - 1): file[i] = file[i][0:-1]
  146.         return file
  147.    
  148.     def writefile_dlg(self, regexp):
  149.         self.file = open(('%s' % (self.Path)), 'a')
  150.         self.file.write((u'\n%s' % regexp))
  151.         return self.file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement