def punc_switch(self, string): zh2en = ['‘', '\'','“','"', '’', '\'', '”', '"', ',', ',', '。', '.', ':', ':', ';', ';', '【', '[', '】', ']',\ '—', '-', '、', '\\'] if self.wrappers['punc'] == 'en': string = ez.sub(string, *zh2en) elif self.wrappers['punc'] == 'zh': en2zh = [] for i in range(0, len(zh2en), 2): en2zh += [zh2en[i + 1], zh2en[i]] string = ez.sub(string, *en2zh) return string
def earlyDiary(startYear=0, endYear=0): d = [ 'Mon', '一', 'Tue', '二', 'Wed', '三', 'Thu', '四', 'Fri', '五', 'Sat', '六', 'Sun', '天', '年0', '年', '月0', '月' ] startYear = startYear or datetime.date.today().year endYear = endYear or startYear for year in range(startYear, endYear + 1): days = 365 + leapYear(year) t = datetime.datetime(year, 1, 1).timestamp() document = Document() for day in range(132): s = ez.sub( time.strftime('%Y年%m月%d日 星期%a 天气: 今天的心情:', time.localtime(t + 86400 * day)), *d) paragraph = document.add_paragraph(s) paragraph.style.paragraph_format.line_spacing = 1.15 paragraph.style.paragraph_format.space_after = Pt(0) paragraph.style.font.size = Pt(10.5) paragraph.style.font.name = u'宋体' table = document.add_table(9, 2, 'Table Grid') row0 = table.rows[0] row0.cells[0].merge(row0.cells[1]) for row in table.rows[-4:-1]: row.cells[0].merge(row.cells[1]) for row, text in zip(table.rows, \ ['', '今日好事', '今天的反省', '明天的目标', '今日特讯', '今天领到的钱:元', '今天花掉的钱:元', '今天剩余的钱:元', '明天的预算']): row.cells[0].text = text table.cell(2, 1).text = 'Waste time' table.cell(3, 1).text = 'Much better' for row in table.rows[1:]: row.cells[0].width = Inches(1) ## for row in table.rows: ## for cell in row.cells: ## style = cell.paragraphs[0].style ## style.font.size = Pt(10.5) ## style.font.name = u'宋体' ## cell.paragraphs[0].style = style document.add_paragraph('') ## paragraph.style.font.color = RGBColor(0xFF, 0xFF, 0xFF) document.save(f'早期日记:{year}.docx') print(f'{year} Done.')
def earlyDiary2(startYear=0, endYear=0): d = [ 'Mon', '一', 'Tue', '二', 'Wed', '三', 'Thu', '四', 'Fri', '五', 'Sat', '六', 'Sun', '天', '年0', '年', '月0', '月' ] startYear = startYear or datetime.date.today().year endYear = endYear or startYear for year in range(startYear, endYear + 1): days = 365 + leapYear(year) t = datetime.datetime(year, 1, 1).timestamp() document = Document() for day in range(2): s = ez.sub( time.strftime('%Y年%m月%d日 星期%a 天气: 今天的心情:', time.localtime(t + 86400 * day)), *d) paragraph = document.add_paragraph(s) paragraph.style.paragraph_format.line_spacing = 1.15 paragraph.style.paragraph_format.space_after = Pt(0) paragraph.style.font.size = Pt(10.5) paragraph.style.font.name = u'宋体' table = document.add_table(9, 2, 'Table Grid') table.cell(0, 0).merge(table.cell(0, 1)) table.cell(2, 0).merge(table.cell(3, 0)).merge(table.cell(4, 0)) table.cell(7, 1).merge(table.cell(8, 1)) for row in table.rows[1:]: row.cells[0].width = Inches(2) for (x, y), text in zip([(1, 0), (1, 1), (3, 1), (5, 0), (6, 0), (6, 1), (7, 0), (8, 0), (9, 0)],\ ['今日好事', '今天的反省', '明天的目标', '今日特讯', '今天领到的钱:元', '明天的预算', '今天花掉的钱:元', '今天剩余的钱:元']): table.cell(x, y).text = text document.add_paragraph('') ## paragraph.style.font.color = RGBColor(0xFF, 0xFF, 0xFF) document.save(f'早期日记:{year}.docx') print(f'{year} Done.')
def main(startYear=0, endYear=0): d = [ 'Mon', '一', 'Tue', '二', 'Wed', '三', 'Thu', '四', 'Fri', '五', 'Sat', '六', 'Sun', '天', '年0', '年', '月0', '月' ] startYear = startYear or datetime.date.today().year endYear = endYear or startYear for year in range(startYear, endYear + 1): days = 365 + leapYear(year) t = datetime.datetime(year, 1, 1).timestamp() s = '\n'.join([ time.strftime('%Y年%m月%d日 星期%a 天气:\n\n', time.localtime(t + 86400 * day)) for day in range(days) ]) s = ez.sub(s, *d) document = Document() paragraph = document.add_paragraph(s) document.styles['Normal'].paragraph_format.line_spacing = 1.15 document.styles['Normal'].paragraph_format.space_after = Pt(0) document.styles['Normal'].font.size = Pt(10.5) document.styles['Normal'].font.name = u'宋体' document.save(f'{year}.docx') print(f'{year} Done.')
def space_remover(self, string): return ez.sub(string, ' ,', ',', ' ', ' ', ' .', '.', ' 。', '。', ' ,', ',')
def utf8(byte): '''不知道怎么弄成utf8= = ''' if type(byte) == str and byte: return sub(repr(byte.encode())[2:-1], '\'', '%27', '\\x', '%', ' ', '%20') return ''
def truth_table(formula, output='a', saveAsFile=False): '''Please enter the formula in terms of a string. Use "=>" or "->" for "imply". Please use () for precendence in case of miscalculations. Default output table will be of Ts and Fs. Change the value of output to "full" to output a complete table, to "num" to output a table of 1s and 0s. Abbreviation: tt''' TF = [True, False] keyword = ['and', 'or', 'not', 'True', 'False'] new = '' ## new formula var_lst = [] ## for p, q, r compound_dict = {} ## for (p and q): "method['p'] and method['q']" col_lst = [] ## for display parentheses_stack = [] ## put parentheses' indice corresponding_stack = [] ## put parentheses' indice of new_formula variable = '' compound = '' isCompound = False formula = formula.strip() if formula[-1].isalpha(): formula += ' ' for i, ch in enumerate(formula): if ch.isalpha(): variable += ch continue elif ch == '(': parentheses_stack.append(i) corresponding_stack.append(len(new) + 1) elif ch == ')': try: start = parentheses_stack.pop() + 1 except: print( 'The numbers of left and right parentheses do not match!') return compound = formula[start:i].strip() if compound not in compound_dict: isCompound = True if variable: if variable in keyword: new += variable else: if variable not in var_lst: var_lst.append(variable) new += f'method[\'{variable}\']' variable = '' if isCompound: compound_dict[compound] = new[corresponding_stack.pop():] isCompound = False if ch in ['=', '-'] and formula[i + 1] == '>': if len(corresponding_stack) == 0: left_p = new.find('(') if left_p == -1: new = f'not {new} or ' else: prev = new[left_p:].strip() new = new[:left_p] + f'not {prev} or ' else: idx = corresponding_stack[-1] prev = new[idx:].strip() new = new[:idx] + f'not {prev} or ' continue elif ch == '>': continue new += ch var_num = len(var_lst) col_lst = var_lst + list(compound_dict.keys()) + [formula] var_len = {} first_line = '' for col in col_lst: length = (len(col) + 1) if len(col) > 5 else 6 var_len[col] = length first_line += ('{:%d}' % length).format(col) print(first_line) printout = '' file_content = ', '.join(col_lst) + '\n' table = [] ##assign values def recursive(method={}): length = len(method) if length < var_num: for tf in TF: method[var_lst[length]] = tf if length == var_num - 1: ## after appending if length == var_num table.append(repr(method)) recursive() del method[var_lst[length]] recursive() for method in table: method = eval(method) row = [] for col in col_lst: if col in compound_dict: row.append((eval(compound_dict[col]), var_len[col])) elif col == formula: row.append((eval(new), var_len[col])) else: row.append((eval(f'method[\'{col}\']'), var_len[col])) for tf, length in row: printout += ('{:%d}' % length).format(repr(tf)) file_content += f'{tf}, ' printout += '\n' file_content = file_content[:-1] + '\n' printout = printout[:-1] file_content = file_content[:-1] print(printout) if output == 'num': file_content = ez.sub(file_content, 'True', '1', 'False', '0') elif output != 'full': ## file_content = ez.sub(file_content, 'and', '∧', 'or', '∨', 'not', '¬') file_content = ez.sub(file_content, 'True', 'T', 'False', 'F') if saveAsFile: try: ez.fwrite(ez.desktop + 'TruthTable.csv', file_content) except: print('傻逼关进程啊!')