Skip to content

Instantly share code, notes, and snippets.

@Gekonshi
Last active December 13, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gekonshi/f8aeb50dad2a8ce14874b8da7b8453ab to your computer and use it in GitHub Desktop.
Save Gekonshi/f8aeb50dad2a8ce14874b8da7b8453ab to your computer and use it in GitHub Desktop.
Скрипт для ведения ежедневного бюджета
import csv, datetime, os
FILE_NAME=os.path.abspath('daybudjet_data.csv') # При использовании скрипта на андроиде, возможно, придёться прописать путь файла полностью.
os_name=os.name
if os_name=='nt':
CLEAR_COMMAND='cls'
elif os_name=='posix':
CLEAR_COMMAND='clear'
def main_menu():
os.system(CLEAR_COMMAND)
today=datetime.date.today()
tdday=today.day
tdmonth=today.month
tdyear=today.year
file=read_csv_file()
cr,cp, s=current_remains(file)
start_p_day=cp[0].day
start_p_month=cp[0].month
start_p_year=cp[0].year
stop_p_day=cp[1].day
stop_p_month=cp[1].month
stop_p_year=cp[1].year
print(f'Добро пожаловать! Сегодня {tdday}.{tdmonth}.{tdyear}.')
print(f' Текущий период: {start_p_day}.{start_p_month}.{start_p_year}-{stop_p_day}.{stop_p_month}.{stop_p_year}.')
print(f'Сумма на период: {cp[2]} рублей.')
print(f' Сумма на день: {float(cp[3]):.{0}f} рублей.')
print(f' Потрачено: {s:.{0}f} рублей.')
print(f' Баланс: {cr:.{0}f} рублей.')
print('------------------------------------------')
print('Что вы хотите сделать?')
print('1-Начать новый период')
print('2-Ввести расходы')
print('3-Выход')
choice=input('Ваш выбор:')
if choice=='1':
new_period()
elif choice=='2':
expenditure()
elif choice=='3':
exit()
else:
print('Ошибка!!!')
input('Нажмите любую клавишу')
main_menu()
def current_remains(db):
index=0
period_index=[]
all_period_list=[]
sum_period=0
for line in db:
split_line=line[0].split(';')
if len(split_line)>2:
a=split_line[0].split('-')
b=split_line[1].split('-')
split_line[0]=datetime.date(int(a[0]),int(a[1]),int(a[2]))
split_line[1]=datetime.date(int(b[0]),int(b[1]),int(b[2]))
period_index.append(index)
sum_period=0
elif len(split_line)==2:
a=split_line[0].split('-')
split_line[0]=datetime.date(int(a[0]),int(a[1]),int(a[2]))
sum_period+=float(split_line[1])
all_period_list.append(split_line)
index+=1
current_period=all_period_list[period_index[-1]::]
day_summa=float(current_period[0][3])
start_period=current_period[0][0]
today=datetime.date.today()
delta=datetime.timedelta(days=1)
days=[[0,0]]
date=start_period
sum=day_summa
i=1
while date<=today:
de=day_expenditures(current_period,date)
day_remains=days[i-1][1]+day_summa-de
day=[date,day_remains]
days.append(day)
date+=delta
i+=1
l=len(days)
return days[l-1][1], current_period[0], sum_period
def day_expenditures(cp,current_day):
de=0
for day in cp[1::]:
if day[0]==current_day:
de+=int(day[1])
return de
def write_csv_file(data):
with open(FILE_NAME,'a',newline='') as csv_file_write:
writer=csv.writer(csv_file_write, delimiter=';', lineterminator='\r')
writer.writerow(data)
def read_csv_file():
database=[]
with open(FILE_NAME,'r') as csv_file_read:
reader=csv.reader(csv_file_read)
for line in reader:
database.append(line)
return database
def expenditure():
os.system(CLEAR_COMMAND)
b=0
while b==0:
exp_sum=int(input('Введите сумму расхода:'))
today=datetime.date.today()
a=[today,exp_sum]
write_csv_file(a)
print('--------')
print('1-Выход')
print('Enter-продолжить')
a=input()
if a=='1':
b=1
main_menu()
def new_period():
lines=[]
date1_txt=input('Введите дату начала периода в формате dd.mm.yyyy:')
date2_txt=input('Введите дату окончания периода в формате dd.mm.yyyy:')
summa=int(input('Сумма на период:'))
date1_list=date1_txt.split('.')
date2_list=date2_txt.split('.')
date1=datetime.date(int(date1_list[2]),int(date1_list[1]),int(date1_list[0]))
date2=datetime.date(int(date2_list[2]),int(date2_list[1]),int(date2_list[0]))
delta=date2-date1
daysumma=summa/(delta.days+1)
print(f'Длина периода составляет {delta.days+1} дней.')
print(f'Ваш бюджет на день составляет {daysumma:.{0}f} рублей.')
lines.append(date1)
lines.append(date2)
lines.append(summa)
lines.append(daysumma)
write_csv_file(lines)
input('Нажмите любую клавишу')
main_menu()
main_menu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment