15.01.2018 г.

Pytnon tkinter sum completed Bulgarian money

from tkinter import *
from tkinter import ttk

# Функцията за събиране на резултата


def sumarize():
    ''' Първо трие резултата ако е бил натиснат преди това после се проверява
всяко поле дали е различно от празно, ако не е се събира стоиността към тотала '''
    Result.delete(0, END)
    total = 0.00
    if entry_100.get() != '':
        total += (float(entry_100.get()) * 100.00)
    if entry_50.get() != '':
        total += (float(entry_50.get()) * 50.00)
    if entry_20.get() != '':
        total += (float(entry_20.get()) * 20.00)
    if entry_10.get() != '':
        total += (float(entry_10.get()) * 10.00)
    if entry_5.get() != '':
        total += (float(entry_5.get()) * 5.00)
    if entry_2.get() != '':
        total += (float(entry_2.get()) * 2.00)
    if entry_1.get() != '':
        total += (float(entry_1.get()) * 1.00)
    if entry_0.get() != '':
        total += (float(entry_0.get()) * 0.50)
    Result.insert(END, total)

# Фунцкия за добавяне към търсеното към всяко от полетата


def add_to_100():
    total = 0.00
    for i in entry_100.get().split('+'):
        total += float(i)
    entry_100.delete(0, END)
    entry_100.insert(END, total)


def add_to_50():
    total = 0.00
    for i in entry_50.get().split('+'):
        total += float(i)
    entry_50.delete(0, END)
    entry_50.insert(END, total)


def add_to_20():
    total = 0.00
    for i in entry_20.get().split('+'):
        total += float(i)
    entry_20.delete(0, END)
    entry_20.insert(END, total)


def add_to_10():
    total = 0.00
    for i in entry_10.get().split('+'):
        total += float(i)
    entry_10.delete(0, END)
    entry_10.insert(END, total)


def add_to_5():
    total = 0.00
    for i in entry_5.get().split('+'):
        total += float(i)
    entry_5.delete(0, END)
    entry_5.insert(END, total)


def add_to_2():
    total = 0.00
    for i in entry_2.get().split('+'):
        total += float(i)
    entry_2.delete(0, END)
    entry_2.insert(END, total)


def add_to_1():
    total = 0.00
    for i in entry_1.get().split('+'):
        total += float(i)
    entry_1.delete(0, END)
    entry_1.insert(END, total)


def add_to_0():
    total = 0.00
    for i in entry_0.get().split('+'):
        total += float(i)
    entry_0.delete(0, END)
    entry_0.insert(END, total)

# Настрйка на шоркътите през стрелките на клавиатурата
# Не работят
# def up_arrow(event):
# Tab

# def down_arrow(event):
# print(current.position())

# Започваме с настройките на прозореца
root = Tk()
root.title('Money check')
root.geometry('350x370+100+100')
root.maxsize(350, 370)

# Бинд конфигурацията
# root.bind('<Up>', up_arrow)
# root.bind('<Down>', down_arrow)

# 100
label_100 = ttk.Label(root, text='Пари по {:>3}'.format(100))
label_100.grid(row=0, column=0, sticky=W)

entry_100 = Entry()
entry_100.focus()
entry_100.grid(row=0, column=1, padx=5)

B_100 = Button(root, text='+ Добавя към 100', command=add_to_100, width=15,
               font=("Times New Roman", 10, "bold"))
B_100.grid(row=0, column=2)

# 50
label_50 = ttk.Label(root, text='Пари по {:>4}'.format(50))
label_50.grid(row=1, column=0, sticky=W)

entry_50 = Entry()
entry_50.grid(row=1, column=1, padx=5)

B_50 = Button(root, text='+ Добавя към 50', command=add_to_50, width=15,
              font=("Times New Roman", 10, "bold"))
B_50.grid(row=1, column=2)

# 20
label_20 = ttk.Label(root, text='Пари по {:>4}'.format(20))
label_20.grid(row=2, column=0, sticky=W)

entry_20 = Entry()
entry_20.grid(row=2, column=1, padx=5)

B_20 = Button(root, text='+ Добавя към 20', command=add_to_20, width=15,
              font=("Times New Roman", 10, "bold"))
B_20.grid(row=2, column=2)

# 10
label_10 = ttk.Label(root, text='Пари по {:>4}'.format(10))
label_10.grid(row=3, column=0, sticky=W)

entry_10 = Entry()
entry_10.grid(row=3, column=1, padx=5)

B_10 = Button(root, text='+ Добавя към 10', command=add_to_10, width=15,
              font=("Times New Roman", 10, "bold"))
B_10.grid(row=3, column=2)

# 5
label_5 = ttk.Label(root, text='Пари по {:>5}'.format(5))
label_5.grid(row=4, column=0, sticky=W)

entry_5 = Entry()
entry_5.grid(row=4, column=1, padx=5)

B_5 = Button(root, text='+ Добавя към 5', command=add_to_5, width=15,
             font=("Times New Roman", 10, "bold"))
B_5.grid(row=4, column=2)

# 2
label_2 = ttk.Label(root, text='Пари по {:>5}'.format(2))
label_2.grid(row=5, column=0, sticky=W)

entry_2 = Entry()
entry_2.grid(row=5, column=1, padx=5)

B_2 = Button(root, text='+ Добавя към 2', command=add_to_2, width=15,
             font=("Times New Roman", 10, "bold"))
B_2.grid(row=5, column=2)

# 1
label_1 = ttk.Label(root, text='Пари по {:>5}'.format(1))
label_1.grid(row=6, column=0, sticky=W)

entry_1 = Entry()
entry_1.grid(row=6, column=1, padx=5)

B_1 = Button(root, text='+ Добавя към 1', command=add_to_1, width=15,
             font=("Times New Roman", 10, "bold"))
B_1.grid(row=6, column=2)

# 0.5
label_0 = ttk.Label(root, text='Пари по {:>4}'.format(0.5))
label_0.grid(row=7, column=0, sticky=W)

entry_0 = Entry()
entry_0.grid(row=7, column=1, padx=5)

B_0 = Button(root, text='+ Добавя към 0.5', command=add_to_0, width=15,
             font=("Times New Roman", 10, "bold"))
B_0.grid(row=7, column=2)

# Бутона за сметката
B = Button(root, text='Гранд Тотал', command=sumarize, width=15,
           font=("Times New Roman", 10, "bold"))
B.grid(row=8, column=2, padx=5, sticky=N, pady=5)

# Резулатата
Result = Listbox(root, height=9)
Result.grid(row=8, column=0, columnspan=2, rowspan=2,
            sticky=W + E, padx=5, pady=5)


if __name__ == '__main__':
    root.mainloop()

1.01.2018 г.

Python split one big file to many small, and return many small to one big

#IMPORT SOME MODULES
import os        #OPEN FILE
import sys        #GET PARAM FROM CONSOLE AND USE TO EXIT IF ERROR RAISE
import re         #REGULAR EKSPRESION

#HARDCODE PARAM IS HERE
#PATH TO SEARCH BIG FILE
PATH = r'C:\\Users\\Borko\\Desktop\\split_files\\Errors.log'

#PATH TO FOLDER WHERE PUT RESULT AND FROM WHERE CREATE FILE BACK
PATH2 = r'C:\\Users\\Borko\\Desktop\\'

#CHUNK SIZE TO READ FILE 1Mb
CHUNK_SIZE = (1024*1024)

#NAME TO SMALL FILES
some_name = 'some_name'

#Check Path exists close program if not
try:
    open(PATH, 'r')
except FileNotFoundError as fr :
    print(fr)
    sys.exit()


#OPEND FILE USE CHUNK AND ITER RESULT WITH YIELD
def read_file(PATH):
    lines = open(PATH, 'br').read()
    while lines:
        chunk = lines[:CHUNK_SIZE]
        lines = lines[CHUNK_SIZE:]
        yield chunk


#WRITE SMALL CHUNK TO NEW SMALL FILES WITH NOT EXTENSION
#EXTENSION FOR SMALL FILES COME FROM PARAM count
def write_to_files(PATH2):
    count = 1
    for res in read_file(PATH):
        name = '{}{}'.format(some_name, count)
        w = open(os.path.join(PATH2, name), 'bw')
        w.write(res)
        w.close()
        count += 1


#READ FOLDER PATH2 AND SEARCH FOR FILES'S THEN RETURN LIST WITH THEM
def concat_chunk_file_to_one():
    found_chink_files = []
    for file in os.listdir(PATH2):
        if re.search('({})\d+'.format(some_name), file):
            found_chink_files.append(os.path.join(PATH2, file))
    return sorted(found_chink_files)


#CONCAT ALL FOUND SMALL FILES TO ONE BIG
def write_concat_to_one_big():
    w = open(PATH2+'\\'+'BIG_FILE.txt', 'a')
    for line in concat_chunk_file_to_one():
        w.write(open(line, 'r').read())
        #print(line)
    w.close()


if __name__ == '__main__':
    #UNCOMENT TO SPLIT FILE TO SMALL
    #write_to_files(PATH2)
   
    #UNCOMENT TO JOIN SMALL FILES TO ONE BIG
    #write_concat_to_one_big()