How to use print_table method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

gui.py

Source:gui.py Github

copy

Full Screen

...45 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")46 for column in self.columns:47 t.add_column(column.name, justify="center")48 return t49 def print_table(self, rows):50 table = self.table()51 for row in rows:52 table.add_row(str(row["id"]), row["name"], row["description"],53 str(row["company_id"]), str(row["category_id"]),54 f"{row['release_date']}", f"{row['active']}")55 clear_console()56 rich_print(table)57 def next(self):58 if len(self.all_rows(self.page+1)) > 0:59 self.page += 160 self.print_table(self.all_rows(self.page))61 else:62 self.print_table(self.all_rows(self.page))63 def back(self):64 if self.page != 1:65 self.page -= 166 self.print_table(self.all_rows(self.page))67 def all_rows(self, page):68 return self.marsh.dump(session.execute(select(self.base)69 .limit(5).offset(5*(page-1))).scalars(), many=True)70 def find_by_name(self):71 name = input(f"{Bcolors.BOLD}Введіть ім'я -> {Bcolors.ENDC}")72 q = session.execute(select(self.base).where(self.base.name == name)).scalars()73 self.print_table(self.marsh.dump(q, many=True))74 self.find = True75 def find_by_category(self):76 clear_console()77 all_category = session.execute(select(Category)).scalars().all()78 m = "Категорії -"79 for elem in all_category:80 m += f" {elem.name}"81 print(f"{Bcolors.HEADER}{m} {Bcolors.ENDC}")82 name = input(f"{Bcolors.BOLD}Введіть назву -> {Bcolors.ENDC}")83 ans = -184 for elem in all_category:85 if elem.name == name:86 ans = elem.id87 q = session.execute(select(self.base).where(self.base.category_id == ans)).scalars()88 self.print_table(self.marsh.dump(q, many=True))89 self.find = True90 def find_by_company(self):91 clear_console()92 all_company = session.execute(select(Company)).scalars().all()93 m = "Компанії -"94 for elem in all_company:95 m += f" {elem.name}"96 print(f"{Bcolors.HEADER}{m}{Bcolors.ENDC}")97 name = input(f"{Bcolors.BOLD}Введіть назву -> {Bcolors.ENDC}")98 ans = -199 for elem in all_company:100 if elem.name == name:101 ans = elem.id102 q = session.execute(select(self.base).where(self.base.company_id == ans)).scalars()103 self.print_table(self.marsh.dump(q, many=True))104 self.find = True105 def edit_by_name(self):106 all_films = session.execute(select(self.base)).scalars().all()107 while True:108 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")109 if idx.isnumeric():110 for elem in all_films:111 if elem.id == int(idx):112 while True:113 new_name = input(f"{Bcolors.BOLD}Нова назва -> {Bcolors.ENDC}")114 if len(new_name) > 0:115 film = session.query(self.base).where(self.base.id == idx).first()116 film.name = new_name117 session.add(film)118 session.commit()119 break120 else:121 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")122 self.print_table(self.all_rows(1))123 return124 elif all_films[-1] == elem:125 print(f"{Bcolors.FAIL}Такого фільма не існує{Bcolors.ENDC}")126 else:127 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")128 def edit_by_desc(self):129 all_films = session.execute(select(self.base)).scalars().all()130 while True:131 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")132 if idx.isnumeric():133 for elem in all_films:134 if elem.id == int(idx):135 while True:136 new_desc = input(f"{Bcolors.BOLD}Новий опис -> {Bcolors.ENDC}")137 if len(new_desc) > 0:138 film = session.query(self.base).where(self.base.id == idx).first()139 film.description = new_desc140 session.add(film)141 session.commit()142 break143 else:144 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")145 self.print_table(self.all_rows(1))146 return147 elif all_films[-1] == elem:148 print(f"{Bcolors.FAIL}Такого фільма не існує{Bcolors.ENDC}")149 else:150 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")151 def delete(self):152 all_films = session.execute(select(self.base)).scalars().all()153 while True:154 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")155 if idx.isnumeric():156 for elem in all_films:157 if elem.id == int(idx):158 session.delete(session.query(Dates).where(Dates.film_id == elem.id).first())159 session.delete(session.query(Sales).where(Sales.film_id == elem.id).first())160 session.delete(elem)161 session.commit()162 self.print_table(self.all_rows(1))163 return164 elif all_films[-1] == elem:165 print(f"{Bcolors.FAIL}Такого фільма не існує{Bcolors.ENDC}")166 else:167 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")168 def new_film(self):169 while True:170 name = input(f"{Bcolors.BOLD}Назва -> {Bcolors.ENDC}")171 if len(name) > 0:172 break173 else:174 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")175 while True:176 desc = input(f"{Bcolors.BOLD}Опис -> {Bcolors.ENDC}")177 if len(desc) > 0:178 break179 else:180 print(f"{Bcolors.FAIL}Введіть правельний опис{Bcolors.ENDC}")181 while True:182 all_category = session.execute(select(Category)).scalars().all()183 m = "Категорії -"184 for elem in all_category:185 m += f" {elem.name}"186 print(f"{Bcolors.HEADER}{m} {Bcolors.ENDC}")187 cat = input(f"{Bcolors.BOLD}Введіть назву -> {Bcolors.ENDC}")188 ans = -1189 for elem in all_category:190 if elem.name == cat:191 ans = elem.id192 if ans != -1:193 cat = ans194 break195 else:196 print(f"{Bcolors.FAIL}Такої категорії не існує{Bcolors.ENDC}")197 while True:198 all_company = session.execute(select(Company)).scalars().all()199 m = "Компанії -"200 for elem in all_company:201 m += f" {elem.name}"202 print(f"{Bcolors.HEADER}{m} {Bcolors.ENDC}")203 comp = input(f"{Bcolors.BOLD}Введіть назву -> {Bcolors.ENDC}")204 ans = -1205 for elem in all_company:206 if elem.name == comp:207 ans = elem.id208 if ans != -1:209 comp = ans210 break211 else:212 print(f"{Bcolors.FAIL}Такої компанії не існує{Bcolors.ENDC}")213 while True:214 act = input(f"{Bcolors.BOLD}Активний (1-так, 2-ні) -> {Bcolors.ENDC}")215 if act.isnumeric():216 if int(act) in list(range(1, 3)):217 act = int(act)218 break219 while True:220 print(f"{Bcolors.HEADER}Дата початку показу{Bcolors.ENDC}")221 year = input(f"{Bcolors.BOLD}Рік -> {Bcolors.ENDC}")222 if year.isnumeric():223 year = int(year)224 month = input(f"{Bcolors.BOLD}Місяць -> {Bcolors.ENDC}")225 if month.isnumeric():226 month = int(month)227 if month in list(range(1, 13)):228 day = input(f"{Bcolors.BOLD}День -> {Bcolors.ENDC}")229 if day.isnumeric():230 day = int(day)231 if day in list(range(1, 32)):232 st_date = dt.date(year, month, day)233 break234 else:235 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")236 else:237 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")238 else:239 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")240 else:241 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")242 else:243 print(f"{Bcolors.FAIL}Не правильний рік{Bcolors.ENDC}")244 while True:245 print(f"{Bcolors.HEADER}Дата кінця показу{Bcolors.ENDC}")246 year = input(f"{Bcolors.BOLD}Рік -> {Bcolors.ENDC}")247 if year.isnumeric():248 year = int(year)249 month = input(f"{Bcolors.BOLD}Місяць -> {Bcolors.ENDC}")250 if month.isnumeric():251 month = int(month)252 if month in list(range(1, 13)):253 day = input(f"{Bcolors.BOLD}День -> {Bcolors.ENDC}")254 if day.isnumeric():255 day = int(day)256 if day in list(range(1, 32)):257 end_date = dt.date(year, month, day)258 break259 else:260 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")261 else:262 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")263 else:264 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")265 else:266 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")267 else:268 print(f"{Bcolors.FAIL}Не правильний рік{Bcolors.ENDC}")269 while True:270 price = input(f"{Bcolors.BOLD}Ціна квитка -> {Bcolors.ENDC}")271 if price.isnumeric():272 price = int(price)273 break274 else:275 print(f"{Bcolors.FAIL}Введіть вірну ціну{Bcolors.ENDC}")276 film = self.base(name=name, description=desc, company_id=comp, category_id=cat, active=act)277 session.add(film)278 session.commit()279 film_sales = Sales(film_id=film.id, peoples=0, ticket_price=price)280 film_dates = Dates(film_id=film.id, start_date=st_date, end_date=end_date)281 session.add_all([film_dates, film_sales])282 session.commit()283 self.print_table(self.all_rows(1))284 def menu(self):285 self.print_table(self.all_rows(self.page))286 while True:287 if not self.find:288 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")289 for idx, elem in enumerate(self.funcs_view_main):290 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")291 func = input(" -> ")292 if func.isnumeric():293 if int(func) in list(range(1, len(self.funcs_view_main)+1)):294 if int(func) == len(self.funcs_view_main):295 break296 else:297 self.funcs_view_call_main[int(func)]()298 else:299 self.print_table(self.all_rows(self.page))300 else:301 self.print_table(self.all_rows(self.page))302 else:303 while True:304 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")305 func = input(" -> ")306 if func.isnumeric():307 if int(func) == 1:308 self.find = False309 break310 self.page = 1311 clear_console()312 self.print_table(self.all_rows(self.page))313 return314 def menu_edit(self):315 self.print_table(self.all_rows(self.page))316 while True:317 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")318 for idx, elem in enumerate(self.funcs_edit_main):319 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")320 func = input(" -> ")321 if func.isnumeric():322 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):323 if int(func) == len(self.funcs_edit_main):324 break325 else:326 self.funcs_edit_call_main[int(func)]()327 else:328 self.print_table(self.all_rows(self.page))329 else:330 self.print_table(self.all_rows(self.page))331 return332class CompanyView:333 def __init__(self):334 self.base = Company335 self.columns = self.base.__table__.columns336 self.marsh = CompanyMarsh()337 self.page = 1338 self.find = False339 self.funcs_edit_main = ["Зміна назви", "Видалити", "Створити", "Вперед", "Назад", "Вихід"]340 self.funcs_edit_call_main = {1: self.edit_by_name, 2: self.delete, 3: self.new_company,341 4: self.next, 5: self.back}342 self.funcs_view_main = ["Пошук по імені", "Вперед", "Назад", "Вихід"]343 self.funcs_view_call_main = {1: self.find_by_name, 2: self.next, 3: self.back}344 def table(self):345 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")346 for column in self.columns:347 t.add_column(column.name, justify="center")348 return t349 def print_table(self, rows):350 table = self.table()351 for row in rows:352 table.add_row(str(row["id"]), row["name"])353 clear_console()354 rich_print(table)355 def edit_by_name(self):356 all_company = session.execute(select(self.base)).scalars().all()357 while True:358 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")359 if idx.isnumeric():360 for elem in all_company:361 if elem.id == int(idx):362 while True:363 new_name = input(f"{Bcolors.BOLD}Нова назва -> {Bcolors.ENDC}")364 if len(new_name) > 0:365 company = session.query(self.base).where(self.base.id == idx).first()366 company.name = new_name367 session.add(company)368 session.commit()369 break370 else:371 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")372 self.print_table(self.all_rows(1))373 return374 elif all_company[-1] == elem:375 print(f"{Bcolors.FAIL}Такої компанії не існує{Bcolors.ENDC}")376 else:377 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")378 def delete(self):379 all_company = session.execute(select(self.base)).scalars().all()380 while True:381 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")382 if idx.isnumeric():383 for elem in all_company:384 if elem.id == int(idx):385 all_films = session.execute(select(Film).where(Film.comany_id == elem.id)).scalars()386 for film in all_films:387 session.delete(session.query(Dates).where(Dates.film_id == film.id).first())388 session.delete(session.query(Sales).where(Sales.film_id == film.id).first())389 session.delete(film)390 session.delete(elem)391 session.commit()392 self.print_table(self.all_rows(1))393 return394 elif all_company[-1] == elem:395 print(f"{Bcolors.FAIL}Такої компанії не існує{Bcolors.ENDC}")396 else:397 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")398 def new_company(self):399 while True:400 name = input(f"{Bcolors.BOLD}Назва -> {Bcolors.ENDC}")401 if len(name) > 0:402 break403 else:404 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")405 company = self.base(name=name)406 session.add(company)407 session.commit()408 self.print_table(self.all_rows(1))409 def next(self):410 if len(self.all_rows(self.page+1)) > 0:411 self.page += 1412 self.print_table(self.all_rows(self.page))413 else:414 self.print_table(self.all_rows(self.page))415 def back(self):416 if self.page != 1:417 self.page -= 1418 self.print_table(self.all_rows(self.page))419 def all_rows(self, page):420 return self.marsh.dump(session.execute(select(self.base)421 .limit(5).offset(5*(page-1))).scalars(), many=True)422 def find_by_name(self):423 name = input(f"{Bcolors.BOLD}Введіть ім'я -> {Bcolors.ENDC}")424 q = session.execute(select(self.base).where(self.base.name == name)).scalars()425 self.print_table(self.marsh.dump(q, many=True))426 self.find = True427 def menu(self):428 self.print_table(self.all_rows(self.page))429 while True:430 if not self.find:431 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")432 for idx, elem in enumerate(self.funcs_view_main):433 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")434 func = input(" -> ")435 if func.isnumeric():436 if int(func) in list(range(1, len(self.funcs_view_main)+1)):437 if int(func) == len(self.funcs_view_main):438 break439 else:440 self.funcs_view_call_main[int(func)]()441 else:442 self.print_table(self.all_rows(self.page))443 else:444 self.print_table(self.all_rows(self.page))445 else:446 while True:447 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")448 func = input(" -> ")449 if func.isnumeric():450 if int(func) == 1:451 self.find = False452 break453 self.page = 1454 clear_console()455 self.print_table(self.all_rows(self.page))456 return457 def menu_edit(self):458 self.print_table(self.all_rows(self.page))459 while True:460 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")461 for idx, elem in enumerate(self.funcs_edit_main):462 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")463 func = input(" -> ")464 if func.isnumeric():465 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):466 if int(func) == len(self.funcs_edit_main):467 break468 else:469 self.funcs_edit_call_main[int(func)]()470 else:471 self.print_table(self.all_rows(self.page))472 else:473 self.print_table(self.all_rows(self.page))474 return475class UsersView:476 def __init__(self):477 self.base = Users478 self.columns = self.base.__table__.columns479 self.marsh = UsersMarsh()480 self.page = 1481 self.find = False482 self.funcs_edit_main = ["Змінити нік", "Змінити ПІБ", "Змінити пошту", "Створити", "Видалити",483 "Вперед", "Назад", "Вихід"]484 self.funcs_edit_call_main = {1: self.edit_by_username, 2: self.edit_by_name, 3: self.edit_by_email,485 4: self.new_user, 5: self.delete, 6: self.next, 7: self.back}486 self.funcs_view_main = ["Пошук по ніку", "Пошук по іменю и прізвищу", "Пошук по пошті", "Вперед", "Назад",487 "Вихід"]488 self.funcs_view_call_main = {1: self.find_by_username, 2: self.find_by_fullname, 3: self.find_by_email,489 4: self.next, 5: self.back}490 def table(self):491 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")492 for column in self.columns:493 t.add_column(column.name, justify="center")494 return t495 def print_table(self, rows):496 table = self.table()497 for row in rows:498 table.add_row(str(row["id"]), row["username"], row["full_name"],499 row["email"], row["hashed_password"])500 clear_console()501 rich_print(table)502 def next(self):503 if len(self.all_rows(self.page+1)) > 0:504 self.page += 1505 self.print_table(self.all_rows(self.page))506 else:507 self.print_table(self.all_rows(self.page))508 def back(self):509 if self.page != 1:510 self.page -= 1511 self.print_table(self.all_rows(self.page))512 def all_rows(self, page):513 return self.marsh.dump(session.execute(select(self.base)514 .limit(5).offset(5*(page-1))).scalars(), many=True)515 def edit_by_username(self):516 all_users = session.execute(select(self.base)).scalars().all()517 while True:518 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")519 if idx.isnumeric():520 for elem in all_users:521 if elem.id == int(idx):522 while True:523 new_name = input(f"{Bcolors.BOLD}Новий нік -> {Bcolors.ENDC}")524 if len(new_name) > 0:525 check_name = session.execute(select(self.base)526 .where(self.base.username == new_name)).scalars().all()527 if len(check_name) == 0:528 user = session.query(self.base).where(self.base.id == idx).first()529 user.username = new_name530 session.add(user)531 session.commit()532 break533 else:534 print(f"{Bcolors.FAIL}Такий нік вже існує{Bcolors.ENDC}")535 else:536 print(f"{Bcolors.FAIL}Введіть правильний нік{Bcolors.ENDC}")537 self.print_table(self.all_rows(1))538 return539 elif all_users[-1] == elem:540 print(f"{Bcolors.FAIL}Такого користувача не існує{Bcolors.ENDC}")541 else:542 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")543 def edit_by_name(self):544 all_users = session.execute(select(self.base)).scalars().all()545 while True:546 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")547 if idx.isnumeric():548 for elem in all_users:549 if elem.id == int(idx):550 while True:551 new_name = input(f"{Bcolors.BOLD}Новий ПІБ -> {Bcolors.ENDC}")552 if len(new_name) > 0:553 user = session.query(self.base).where(self.base.id == idx).first()554 user.full_name = new_name555 session.add(user)556 session.commit()557 break558 else:559 print(f"{Bcolors.FAIL}Введіть правильний ПІБ{Bcolors.ENDC}")560 self.print_table(self.all_rows(1))561 return562 elif all_users[-1] == elem:563 print(f"{Bcolors.FAIL}Такого користувача не існує{Bcolors.ENDC}")564 else:565 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")566 def edit_by_email(self):567 all_users = session.execute(select(self.base)).scalars().all()568 while True:569 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")570 if idx.isnumeric():571 for elem in all_users:572 if elem.id == int(idx):573 while True:574 new_name = input(f"{Bcolors.BOLD}Нова пошта -> {Bcolors.ENDC}")575 if len(new_name) > 0:576 check_name = session.execute(select(self.base)577 .where(self.base.email == new_name)).scalars().all()578 if len(check_name) == 0:579 user = session.query(self.base).where(self.base.id == idx).first()580 user.email = new_name581 session.add(user)582 session.commit()583 break584 else:585 print(f"{Bcolors.FAIL}Така пошта вже існує{Bcolors.ENDC}")586 else:587 print(f"{Bcolors.FAIL}Введіть правильну пошту{Bcolors.ENDC}")588 self.print_table(self.all_rows(1))589 return590 elif all_users[-1] == elem:591 print(f"{Bcolors.FAIL}Такого користувача не існує{Bcolors.ENDC}")592 else:593 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")594 def new_user(self):595 while True:596 username = input(f"{Bcolors.BOLD}Нік -> {Bcolors.ENDC}")597 if len(username) > 0:598 check_username = session.execute(select(self.base)599 .where(self.base.username == username)).scalars().all()600 if len(check_username) == 0:601 break602 else:603 print(f"{Bcolors.FAIL}Користувач з таким ніком вже існує{Bcolors.ENDC}")604 else:605 print(f"{Bcolors.FAIL}Введіть правельний нік{Bcolors.ENDC}")606 while True:607 fullname = input(f"{Bcolors.BOLD}ПІБ -> {Bcolors.ENDC}")608 if len(username) > 0:609 break610 else:611 print(f"{Bcolors.FAIL}Введіть правельний ПІБ{Bcolors.ENDC}")612 while True:613 email = input(f"{Bcolors.BOLD}Пошта -> {Bcolors.ENDC}")614 if len(username) > 0:615 check_email = session.execute(select(self.base)616 .where(self.base.email == email)).scalars().all()617 if len(check_email) == 0:618 break619 else:620 print(f"{Bcolors.FAIL}Користувач з такою поштою вже існує{Bcolors.ENDC}")621 else:622 print(f"{Bcolors.FAIL}Введіть правельну пошту{Bcolors.ENDC}")623 while True:624 password_first = input(f"{Bcolors.BOLD}Введіть пароль -> {Bcolors.ENDC}")625 password_second = input(f"{Bcolors.BOLD}Введіть пароль ще раз -> {Bcolors.ENDC}")626 if password_first == password_second:627 if len(password_first) >= 5:628 break629 else:630 print(f"{Bcolors.FAIL}Довжина пароля має бути >= 5{Bcolors.ENDC}")631 else:632 print(f"{Bcolors.FAIL}Паролі не одинакові{Bcolors.ENDC}")633 while True:634 perm = input(f"{Bcolors.BOLD}Адмін (1-так, 0-ні) -> {Bcolors.ENDC}")635 if perm.isnumeric():636 if int(perm) in list(range(0, 2)):637 perm = bool(perm)638 break639 salt = uuid4().hex640 hash_password = sha256(salt.encode() + password_first.encode()).hexdigest()641 user = self.base(username=username, full_name=fullname, email=email, hashed_password=hash_password)642 session.add(user)643 session.commit()644 dates = RegDates(user_id=user.id)645 perms = Perm(user_id=user.id, admin=perm)646 salts = Salts(user_id=user.id, salt=salt)647 session.add_all([dates, perms, salts])648 session.commit()649 self.print_table(self.all_rows(1))650 def delete(self):651 all_users = session.execute(select(self.base)).scalars().all()652 while True:653 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")654 if idx.isnumeric():655 for elem in all_users:656 if elem.id == int(idx):657 session.delete(session.query(Perm).where(Perm.user_id == elem.id).first())658 session.delete(session.query(RegDates).where(RegDates.user_id == elem.id).first())659 session.delete(session.query(Salts).where(Salts.user_id == elem.id).first())660 session.delete(elem)661 session.commit()662 self.print_table(self.all_rows(1))663 return664 elif all_users[-1] == elem:665 print(f"{Bcolors.FAIL}Такого користувача не існує{Bcolors.ENDC}")666 else:667 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")668 def find_by_username(self):669 username = input(f"{Bcolors.BOLD}Введіть нік -> {Bcolors.ENDC}")670 q = session.execute(select(self.base).where(self.base.username == username)).scalars()671 self.print_table(self.marsh.dump(q, many=True))672 self.find = True673 def find_by_fullname(self):674 fullname = input(f"{Bcolors.BOLD}Введіть ім'я і прізвище -> {Bcolors.ENDC}")675 q = session.execute(select(self.base).where(self.base.full_name == fullname)).scalars()676 self.print_table(self.marsh.dump(q, many=True))677 self.find = True678 def find_by_email(self):679 email = input(f"{Bcolors.BOLD}Введіть пошту -> {Bcolors.ENDC}")680 q = session.execute(select(self.base).where(self.base.email == email)).scalars()681 self.print_table(self.marsh.dump(q, many=True))682 self.find = True683 def menu(self):684 self.print_table(self.all_rows(self.page))685 while True:686 if not self.find:687 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")688 for idx, elem in enumerate(self.funcs_view_main):689 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")690 func = input(" -> ")691 if func.isnumeric():692 if int(func) in list(range(1, len(self.funcs_view_main)+1)):693 if int(func) == len(self.funcs_view_main):694 break695 else:696 self.funcs_view_call_main[int(func)]()697 else:698 self.print_table(self.all_rows(self.page))699 else:700 self.print_table(self.all_rows(self.page))701 else:702 while True:703 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")704 func = input(" -> ")705 if func.isnumeric():706 if int(func) == 1:707 self.find = False708 break709 self.page = 1710 clear_console()711 self.print_table(self.all_rows(self.page))712 return713 def menu_edit(self):714 self.print_table(self.all_rows(self.page))715 while True:716 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")717 for idx, elem in enumerate(self.funcs_edit_main):718 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")719 func = input(" -> ")720 if func.isnumeric():721 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):722 if int(func) == len(self.funcs_edit_main):723 break724 else:725 self.funcs_edit_call_main[int(func)]()726 else:727 self.print_table(self.all_rows(self.page))728 else:729 self.print_table(self.all_rows(self.page))730 return731class CategoryView:732 def __init__(self):733 self.base = Category734 self.columns = self.base.__table__.columns735 self.marsh = CategoryMarsh()736 self.page = 1737 self.find = False738 self.funcs_edit_main = ["Зміна назви", "Видалити", "Створити", "Вперед", "Назад", "Вихід"]739 self.funcs_edit_call_main = {1: self.edit_by_name, 2: self.delete, 3: self.new_category,740 4: self.next, 5: self.back}741 self.funcs_view_main = ["Пошук по імені", "Вперед", "Назад", "Вихід"]742 self.funcs_view_call_main = {1: self.find_by_name, 2: self.next, 3: self.back}743 def table(self):744 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")745 for column in self.columns:746 t.add_column(column.name, justify="center")747 return t748 def print_table(self, rows):749 table = self.table()750 for row in rows:751 table.add_row(str(row["id"]), row["name"])752 clear_console()753 rich_print(table)754 def next(self):755 if len(self.all_rows(self.page+1)) > 0:756 self.page += 1757 self.print_table(self.all_rows(self.page))758 else:759 self.print_table(self.all_rows(self.page))760 def back(self):761 if self.page != 1:762 self.page -= 1763 self.print_table(self.all_rows(self.page))764 def all_rows(self, page):765 return self.marsh.dump(session.execute(select(self.base)766 .limit(5).offset(5*(page-1))).scalars(), many=True)767 def edit_by_name(self):768 all_category = session.execute(select(self.base)).scalars().all()769 while True:770 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")771 if idx.isnumeric():772 for elem in all_category:773 if elem.id == int(idx):774 while True:775 new_name = input(f"{Bcolors.BOLD}Нова назва -> {Bcolors.ENDC}")776 if len(new_name) > 0:777 category = session.query(self.base).where(self.base.id == idx).first()778 category.name = new_name779 session.add(category)780 session.commit()781 break782 else:783 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")784 self.print_table(self.all_rows(1))785 return786 elif all_category[-1] == elem:787 print(f"{Bcolors.FAIL}Такої категорії не існує{Bcolors.ENDC}")788 else:789 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")790 def delete(self):791 all_category = session.execute(select(self.base)).scalars().all()792 while True:793 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")794 if idx.isnumeric():795 for elem in all_category:796 if elem.id == int(idx):797 all_films = session.execute(select(Film).where(Film.category_id == elem.id)).scalars()798 for film in all_films:799 session.delete(session.query(Dates).where(Dates.film_id == film.id).first())800 session.delete(session.query(Sales).where(Sales.film_id == film.id).first())801 session.delete(film)802 session.delete(elem)803 session.commit()804 self.print_table(self.all_rows(1))805 return806 elif all_category[-1] == elem:807 print(f"{Bcolors.FAIL}Такої категорії не існує{Bcolors.ENDC}")808 else:809 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")810 def new_category(self):811 while True:812 name = input(f"{Bcolors.BOLD}Назва -> {Bcolors.ENDC}")813 if len(name) > 0:814 break815 else:816 print(f"{Bcolors.FAIL}Введіть правельну назву{Bcolors.ENDC}")817 category = self.base(name=name)818 session.add(category)819 session.commit()820 self.print_table(self.all_rows(1))821 def find_by_name(self):822 name = input(f"{Bcolors.BOLD}Введіть ім'я -> {Bcolors.ENDC}")823 q = session.execute(select(self.base).where(self.base.name == name)).scalars()824 self.print_table(self.marsh.dump(q, many=True))825 self.find = True826 def menu(self):827 self.print_table(self.all_rows(self.page))828 while True:829 if not self.find:830 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")831 for idx, elem in enumerate(self.funcs_view_main):832 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")833 func = input(" -> ")834 if func.isnumeric():835 if int(func) in list(range(1, len(self.funcs_view_main)+1)):836 if int(func) == len(self.funcs_view_main):837 break838 else:839 self.funcs_view_call_main[int(func)]()840 else:841 self.print_table(self.all_rows(self.page))842 else:843 self.print_table(self.all_rows(self.page))844 else:845 while True:846 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")847 func = input(" -> ")848 if func.isnumeric():849 if int(func) == 1:850 self.find = False851 break852 self.page = 1853 clear_console()854 self.print_table(self.all_rows(self.page))855 return856 def menu_edit(self):857 self.print_table(self.all_rows(self.page))858 while True:859 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")860 for idx, elem in enumerate(self.funcs_edit_main):861 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")862 func = input(" -> ")863 if func.isnumeric():864 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):865 if int(func) == len(self.funcs_edit_main):866 break867 else:868 self.funcs_edit_call_main[int(func)]()869 else:870 self.print_table(self.all_rows(self.page))871 else:872 self.print_table(self.all_rows(self.page))873 return874class SalesView:875 def __init__(self):876 self.base = Sales877 self.columns = self.base.__table__.columns878 self.marsh = SalesMarsh()879 self.page = 1880 self.find = False881 self.funcs_edit_main = ["Змінити ціну", "Змінити кількість людей", "Видалити", "Вперед", "Назад", "Вихід"]882 self.funcs_edit_call_main = {1: self.edit_by_price, 2: self.edit_by_peoples, 3: self.delete,883 4: self.next, 5: self.back}884 self.funcs_view_main = ["Пошук по фільму", "Пошук по кількості людей", "Вперед", "Назад", "Вихід"]885 self.funcs_view_call_main = {1: self.find_by_film, 2: self.find_by_peoples, 3: self.next, 4: self.back}886 def table(self):887 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")888 for column in self.columns:889 t.add_column(column.name, justify="center")890 return t891 def print_table(self, rows):892 table = self.table()893 for row in rows:894 table.add_row(str(row["id"]), str(row["film_id"]), str(row["peoples"]), str(row["ticket_price"]))895 clear_console()896 rich_print(table)897 def next(self):898 if len(self.all_rows(self.page+1)) > 0:899 self.page += 1900 self.print_table(self.all_rows(self.page))901 else:902 self.print_table(self.all_rows(self.page))903 def back(self):904 if self.page != 1:905 self.page -= 1906 self.print_table(self.all_rows(self.page))907 def all_rows(self, page):908 return self.marsh.dump(session.execute(select(self.base)909 .limit(5).offset(5*(page-1))).scalars(), many=True)910 def edit_by_price(self):911 all_sales = session.execute(select(self.base)).scalars().all()912 while True:913 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")914 if idx.isnumeric():915 for elem in all_sales:916 if elem.id == int(idx):917 while True:918 new_price = input(f"{Bcolors.BOLD}Нова ціна -> {Bcolors.ENDC}")919 if new_price.isnumeric():920 sales = session.query(self.base).where(self.base.id == idx).first()921 sales.ticket_price = int(new_price)922 session.add(sales)923 session.commit()924 break925 else:926 print(f"{Bcolors.FAIL}Введіть правельну ціну{Bcolors.ENDC}")927 self.print_table(self.all_rows(1))928 return929 elif all_sales[-1] == elem:930 print(f"{Bcolors.FAIL}Таких продажів не існує{Bcolors.ENDC}")931 else:932 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")933 def edit_by_peoples(self):934 all_sales = session.execute(select(self.base)).scalars().all()935 while True:936 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")937 if idx.isnumeric():938 for elem in all_sales:939 if elem.id == int(idx):940 while True:941 new_peoples = input(f"{Bcolors.BOLD}Нова кількість людей -> {Bcolors.ENDC}")942 if new_peoples.isnumeric():943 sales = session.query(self.base).where(self.base.id == idx).first()944 sales.peoples = int(new_peoples)945 session.add(sales)946 session.commit()947 break948 else:949 print(f"{Bcolors.FAIL}Введіть правельну кількість людей{Bcolors.ENDC}")950 self.print_table(self.all_rows(1))951 return952 elif all_sales[-1] == elem:953 print(f"{Bcolors.FAIL}Таких продажів не існує{Bcolors.ENDC}")954 else:955 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")956 def delete(self):957 all_sales = session.execute(select(self.base)).scalars().all()958 while True:959 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")960 if idx.isnumeric():961 for elem in all_sales:962 if elem.id == int(idx):963 film = session.query(Film).where(Film.id == elem.film_id).first()964 session.delete(session.query(Dates).where(Dates.film_id == film.id).first())965 session.delete(elem)966 session.delete(film)967 session.commit()968 self.print_table(self.all_rows(1))969 return970 elif all_sales[-1] == elem:971 print(f"{Bcolors.FAIL}Такої продажі не існує{Bcolors.ENDC}")972 else:973 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")974 def find_by_film(self):975 all_films = session.execute(select(Film)).scalars().all()976 count = 0977 m = f"{Bcolors.HEADER}Фільми:\n{Bcolors.ENDC}"978 for elem in all_films:979 count += 1980 m += f"{Bcolors.BOLD}{elem.name}{Bcolors.ENDC} "981 if count == 5:982 m += "\n"983 clear_console()984 print(m)985 film = input(f"{Bcolors.BOLD}Введіть фільм -> {Bcolors.ENDC}")986 ans = -1987 for elem in all_films:988 if elem.name == film:989 ans = elem.id990 q = session.execute(select(self.base).where(self.base.film_id == ans)).scalars()991 self.print_table(self.marsh.dump(q, many=True))992 self.find = True993 def find_by_peoples(self):994 min_num = input(f"{Bcolors.BOLD}Введіть меньше число -> {Bcolors.ENDC}")995 max_num = input(f"{Bcolors.BOLD}Введіть більше число -> {Bcolors.ENDC}")996 q = session.execute(select(self.base).where(self.base.peoples.between(min_num, max_num))).scalars()997 self.print_table(self.marsh.dump(q, many=True))998 self.find = True999 def menu(self):1000 self.print_table(self.all_rows(self.page))1001 while True:1002 if not self.find:1003 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1004 for idx, elem in enumerate(self.funcs_view_main):1005 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1006 func = input(" -> ")1007 if func.isnumeric():1008 if int(func) in list(range(1, len(self.funcs_view_main)+1)):1009 if int(func) == len(self.funcs_view_main):1010 break1011 else:1012 self.funcs_view_call_main[int(func)]()1013 else:1014 self.print_table(self.all_rows(self.page))1015 else:1016 self.print_table(self.all_rows(self.page))1017 else:1018 while True:1019 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")1020 func = input(" -> ")1021 if func.isnumeric():1022 if int(func) == 1:1023 self.find = False1024 break1025 self.page = 11026 clear_console()1027 self.print_table(self.all_rows(self.page))1028 return1029 def menu_edit(self):1030 self.print_table(self.all_rows(self.page))1031 while True:1032 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1033 for idx, elem in enumerate(self.funcs_edit_main):1034 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1035 func = input(" -> ")1036 if func.isnumeric():1037 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):1038 if int(func) == len(self.funcs_edit_main):1039 break1040 else:1041 self.funcs_edit_call_main[int(func)]()1042 else:1043 self.print_table(self.all_rows(self.page))1044 else:1045 self.print_table(self.all_rows(self.page))1046 return1047class FilmDateView:1048 def __init__(self):1049 self.base = Dates1050 self.columns = self.base.__table__.columns1051 self.marsh = DatesMarsh()1052 self.page = 11053 self.find = False1054 self.funcs_edit_main = ["Змінити початкову дату", "Змінити кінцеву дату", "Видалити",1055 "Вперед", "Назад", "Вихід"]1056 self.funcs_edit_call_main = {1: self.edit_by_start, 2: self.edit_by_end, 3: self.delete,1057 4: self.next, 5: self.back}1058 self.funcs_view_main = ["Пошук по фільму", "Пошук по даті початку", "Пошук по даті кінця",1059 "Вперед", "Назад", "Вихід"]1060 self.funcs_view_call_main = {1: self.find_by_film, 2: self.find_by_start, 3: self.find_by_end,1061 4: self.next, 5: self.back}1062 def table(self):1063 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")1064 for column in self.columns:1065 t.add_column(column.name, justify="center")1066 return t1067 def print_table(self, rows):1068 table = self.table()1069 for row in rows:1070 table.add_row(str(row["id"]), str(row["film_id"]), f'{row["start_date"]}', f'{row["end_date"]}')1071 clear_console()1072 rich_print(table)1073 def next(self):1074 if len(self.all_rows(self.page+1)) > 0:1075 self.page += 11076 self.print_table(self.all_rows(self.page))1077 else:1078 self.print_table(self.all_rows(self.page))1079 def back(self):1080 if self.page != 1:1081 self.page -= 11082 self.print_table(self.all_rows(self.page))1083 def all_rows(self, page):1084 return self.marsh.dump(session.execute(select(self.base)1085 .limit(5).offset(5*(page-1))).scalars(), many=True)1086 def edit_by_start(self):1087 all_dates = session.execute(select(self.base)).scalars().all()1088 while True:1089 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1090 if idx.isnumeric():1091 for elem in all_dates:1092 if elem.id == int(idx):1093 print(f"{Bcolors.HEADER}Дата початку показу{Bcolors.ENDC}")1094 year = input(f"{Bcolors.BOLD}Рік -> {Bcolors.ENDC}")1095 if year.isnumeric():1096 year = int(year)1097 month = input(f"{Bcolors.BOLD}Місяць -> {Bcolors.ENDC}")1098 if month.isnumeric():1099 month = int(month)1100 if month in list(range(1, 13)):1101 day = input(f"{Bcolors.BOLD}День -> {Bcolors.ENDC}")1102 if day.isnumeric():1103 day = int(day)1104 if day in list(range(1, 32)):1105 st_date = dt.date(year, month, day)1106 elem.start_date = st_date1107 session.add(elem)1108 session.commit()1109 self.print_table(self.all_rows(1))1110 return1111 else:1112 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")1113 else:1114 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")1115 else:1116 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")1117 else:1118 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")1119 else:1120 print(f"{Bcolors.FAIL}Не правильний рік{Bcolors.ENDC}")1121 elif all_dates[-1] == elem:1122 print(f"{Bcolors.FAIL}Такої дати фільму не існує{Bcolors.ENDC}")1123 else:1124 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1125 def edit_by_end(self):1126 all_dates = session.execute(select(self.base)).scalars().all()1127 while True:1128 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1129 if idx.isnumeric():1130 for elem in all_dates:1131 if elem.id == int(idx):1132 print(f"{Bcolors.HEADER}Дата кінця показу{Bcolors.ENDC}")1133 year = input(f"{Bcolors.BOLD}Рік -> {Bcolors.ENDC}")1134 if year.isnumeric():1135 year = int(year)1136 month = input(f"{Bcolors.BOLD}Місяць -> {Bcolors.ENDC}")1137 if month.isnumeric():1138 month = int(month)1139 if month in list(range(1, 13)):1140 day = input(f"{Bcolors.BOLD}День -> {Bcolors.ENDC}")1141 if day.isnumeric():1142 day = int(day)1143 if day in list(range(1, 32)):1144 end_date = dt.date(year, month, day)1145 elem.end_date = end_date1146 session.add(elem)1147 session.commit()1148 self.print_table(self.all_rows(1))1149 return1150 else:1151 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")1152 else:1153 print(f"{Bcolors.FAIL}Не правильний день{Bcolors.ENDC}")1154 else:1155 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")1156 else:1157 print(f"{Bcolors.FAIL}Не правильний місяць{Bcolors.ENDC}")1158 else:1159 print(f"{Bcolors.FAIL}Не правильний рік{Bcolors.ENDC}")1160 elif all_dates[-1] == elem:1161 print(f"{Bcolors.FAIL}Такої дати фільму не існує{Bcolors.ENDC}")1162 else:1163 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1164 def delete(self):1165 all_dates = session.execute(select(self.base)).scalars().all()1166 while True:1167 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1168 if idx.isnumeric():1169 for elem in all_dates:1170 if elem.id == int(idx):1171 film = session.query(Film).where(Film.id == elem.film_id).first()1172 session.delete(session.query(Sales).where(Dates.film_id == film.id).first())1173 session.delete(elem)1174 session.delete(film)1175 session.commit()1176 self.print_table(self.all_rows(1))1177 return1178 elif all_dates[-1] == elem:1179 print(f"{Bcolors.FAIL}Такої дати фільму не існує{Bcolors.ENDC}")1180 else:1181 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1182 def find_by_film(self):1183 all_films = session.execute(select(Film)).scalars().all()1184 count = 01185 m = f"{Bcolors.HEADER}Фільми:\n{Bcolors.ENDC}"1186 for elem in all_films:1187 count += 11188 m += f"{Bcolors.BOLD}{elem.name}{Bcolors.ENDC} "1189 if count == 5:1190 m += "\n"1191 clear_console()1192 print(m)1193 film = input(f"{Bcolors.BOLD}Введіть фільм -> {Bcolors.ENDC}")1194 ans = -11195 for elem in all_films:1196 if elem.name == film:1197 ans = elem.id1198 q = session.execute(select(self.base).where(self.base.film_id == ans)).scalars()1199 self.print_table(self.marsh.dump(q, many=True))1200 self.find = True1201 def find_by_start(self):1202 print(f"{Bcolors.FAIL}Вводити такого типу -> 2021-07-25{Bcolors.ENDC}")1203 min_date = input(f"{Bcolors.BOLD}Введіть першу дату -> {Bcolors.ENDC}")1204 max_date = input(f"{Bcolors.BOLD}Введіть другу дату -> {Bcolors.ENDC}")1205 q = session.execute(select(self.base).where(self.base.start_date.between(min_date, max_date))).scalars()1206 self.print_table(self.marsh.dump(q, many=True))1207 self.find = True1208 def find_by_end(self):1209 print(f"{Bcolors.FAIL}Вводити такого типу -> 2021-07-25{Bcolors.ENDC}")1210 min_date = input(f"{Bcolors.BOLD}Введіть першу дату -> {Bcolors.ENDC}")1211 max_date = input(f"{Bcolors.BOLD}Введіть другу дату -> {Bcolors.ENDC}")1212 q = session.execute(select(self.base).where(self.base.end_date.between(min_date, max_date))).scalars()1213 self.print_table(self.marsh.dump(q, many=True))1214 self.find = True1215 def menu(self):1216 self.print_table(self.all_rows(self.page))1217 while True:1218 if not self.find:1219 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1220 for idx, elem in enumerate(self.funcs_view_main):1221 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1222 func = input(" -> ")1223 if func.isnumeric():1224 if int(func) in list(range(1, len(self.funcs_view_main)+1)):1225 if int(func) == len(self.funcs_view_main):1226 break1227 else:1228 self.funcs_view_call_main[int(func)]()1229 else:1230 self.print_table(self.all_rows(self.page))1231 else:1232 self.print_table(self.all_rows(self.page))1233 else:1234 while True:1235 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")1236 func = input(" -> ")1237 if func.isnumeric():1238 if int(func) == 1:1239 self.find = False1240 break1241 self.page = 11242 clear_console()1243 self.print_table(self.all_rows(self.page))1244 return1245 def menu_edit(self):1246 self.print_table(self.all_rows(self.page))1247 while True:1248 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1249 for idx, elem in enumerate(self.funcs_edit_main):1250 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1251 func = input(" -> ")1252 if func.isnumeric():1253 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):1254 if int(func) == len(self.funcs_edit_main):1255 break1256 else:1257 self.funcs_edit_call_main[int(func)]()1258 else:1259 self.print_table(self.all_rows(self.page))1260 else:1261 self.print_table(self.all_rows(self.page))1262 return1263class SaltsView:1264 def __init__(self):1265 self.base = Salts1266 self.columns = self.base.__table__.columns1267 self.marsh = SaltsMarsh()1268 self.page = 11269 self.find = False1270 self.funcs_edit_main = ["Видалити", "Вперед", "Назад", "Вихід"]1271 self.funcs_edit_call_main = {1: self.delete, 2: self.next, 3: self.back}1272 self.funcs_view_main = ["Пошук по ніку", "Вперед", "Назад", "Вихід"]1273 self.funcs_view_call_main = {1: self.find_by_username, 2: self.next, 3: self.back}1274 def table(self):1275 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")1276 for column in self.columns:1277 t.add_column(column.name, justify="center")1278 return t1279 def print_table(self, rows):1280 table = self.table()1281 for row in rows:1282 table.add_row(str(row["id"]), str(row["user_id"]), row["salt"])1283 clear_console()1284 rich_print(table)1285 def next(self):1286 if len(self.all_rows(self.page+1)) > 0:1287 self.page += 11288 self.print_table(self.all_rows(self.page))1289 else:1290 self.print_table(self.all_rows(self.page))1291 def back(self):1292 if self.page != 1:1293 self.page -= 11294 self.print_table(self.all_rows(self.page))1295 def all_rows(self, page):1296 return self.marsh.dump(session.execute(select(self.base)1297 .limit(5).offset(5*(page-1))).scalars(), many=True)1298 def delete(self):1299 all_salts = session.execute(select(self.base)).scalars().all()1300 while True:1301 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1302 if idx.isnumeric():1303 for elem in all_salts:1304 if elem.id == int(idx):1305 session.delete(session.query(Perm).where(Perm.user_id == elem.user_id).first())1306 session.delete(session.query(RegDates).where(RegDates.user_id == elem.user_id).first())1307 session.delete(session.query(Users).where(Users.id == elem.user_id).first())1308 session.delete(elem)1309 session.commit()1310 self.print_table(self.all_rows(1))1311 return1312 elif all_salts[-1] == elem:1313 print(f"{Bcolors.FAIL}Такого користувача не існує{Bcolors.ENDC}")1314 else:1315 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1316 def find_by_username(self):1317 clear_console()1318 all_users = session.execute(select(Users)).scalars().all()1319 username = input(f"{Bcolors.BOLD}Введіть нік -> {Bcolors.ENDC}")1320 ans = -11321 for elem in all_users:1322 if elem.username == username:1323 ans = elem.id1324 q = session.execute(select(self.base).where(self.base.user_id == ans)).scalars()1325 self.print_table(self.marsh.dump(q, many=True))1326 self.find = True1327 def menu(self):1328 self.print_table(self.all_rows(self.page))1329 while True:1330 if not self.find:1331 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1332 for idx, elem in enumerate(self.funcs_view_main):1333 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1334 func = input(" -> ")1335 if func.isnumeric():1336 if int(func) in list(range(1, len(self.funcs_view_main)+1)):1337 if int(func) == len(self.funcs_view_main):1338 break1339 else:1340 self.funcs_view_call_main[int(func)]()1341 else:1342 self.print_table(self.all_rows(self.page))1343 else:1344 self.print_table(self.all_rows(self.page))1345 else:1346 while True:1347 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")1348 func = input(" -> ")1349 if func.isnumeric():1350 if int(func) == 1:1351 self.find = False1352 break1353 self.page = 11354 clear_console()1355 self.print_table(self.all_rows(self.page))1356 return1357 def menu_edit(self):1358 self.print_table(self.all_rows(self.page))1359 while True:1360 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1361 for idx, elem in enumerate(self.funcs_edit_main):1362 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1363 func = input(" -> ")1364 if func.isnumeric():1365 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):1366 if int(func) == len(self.funcs_edit_main):1367 break1368 else:1369 self.funcs_edit_call_main[int(func)]()1370 else:1371 self.print_table(self.all_rows(self.page))1372 else:1373 self.print_table(self.all_rows(self.page))1374 return1375class PermView:1376 def __init__(self):1377 self.base = Perm1378 self.columns = self.base.__table__.columns1379 self.marsh = PermMarsh()1380 self.page = 11381 self.find = False1382 self.funcs_edit_main = ["Змінити права", "Видалити", "Вперед", "Назад", "Вихід"]1383 self.funcs_edit_call_main = {1: self.edit_by_admin, 2: self.delete, 3: self.next, 4: self.back}1384 self.funcs_view_main = ["Пошук по ніку", "Адміни", "Користувачі", "Вперед", "Назад", "Вихід"]1385 self.funcs_view_call_main = {1: self.find_by_username, 2: self.find_by_admin, 3: self.find_by_user,1386 4: self.next, 5: self.back}1387 def table(self):1388 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")1389 for column in self.columns:1390 t.add_column(column.name, justify="center")1391 return t1392 def print_table(self, rows):1393 table = self.table()1394 for row in rows:1395 table.add_row(str(row["id"]), str(row["user_id"]), f"{row['admin']}")1396 clear_console()1397 rich_print(table)1398 def next(self):1399 if len(self.all_rows(self.page+1)) > 0:1400 self.page += 11401 self.print_table(self.all_rows(self.page))1402 else:1403 self.print_table(self.all_rows(self.page))1404 def back(self):1405 if self.page != 1:1406 self.page -= 11407 self.print_table(self.all_rows(self.page))1408 def all_rows(self, page):1409 return self.marsh.dump(session.execute(select(self.base)1410 .limit(5).offset(5*(page-1))).scalars(), many=True)1411 def delete(self):1412 all_perms = session.execute(select(self.base)).scalars().all()1413 while True:1414 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1415 if idx.isnumeric():1416 for elem in all_perms:1417 if elem.id == int(idx):1418 session.delete(session.query(Salts).where(Salts.user_id == elem.user_id).first())1419 session.delete(session.query(RegDates).where(RegDates.user_id == elem.user_id).first())1420 session.delete(session.query(Users).where(Users.id == elem.user_id).first())1421 session.delete(elem)1422 session.commit()1423 self.print_table(self.all_rows(1))1424 return1425 elif all_perms[-1] == elem:1426 print(f"{Bcolors.FAIL}Таких ключів не існує{Bcolors.ENDC}")1427 else:1428 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1429 def edit_by_admin(self):1430 all_perms = session.execute(select(self.base)).scalars().all()1431 while True:1432 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1433 if idx.isnumeric():1434 for elem in all_perms:1435 if elem.id == int(idx):1436 while True:1437 new_perm = input(f"{Bcolors.BOLD}Нові права (1-адмін, 0-користувач) -> {Bcolors.ENDC}")1438 if new_perm.isnumeric():1439 if int(new_perm) in list(range(0, 2)):1440 perm = session.query(self.base).where(self.base.id == idx).first()1441 perm.admin = int(new_perm)1442 session.add(perm)1443 session.commit()1444 self.print_table(self.all_rows(1))1445 return1446 else:1447 print(f"{Bcolors.FAIL}Введіть правельну цифру{Bcolors.ENDC}")1448 else:1449 print(f"{Bcolors.FAIL}Введіть правельну цифру{Bcolors.ENDC}")1450 elif all_perms[-1] == elem:1451 print(f"{Bcolors.FAIL}Таких ключів не існує{Bcolors.ENDC}")1452 else:1453 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1454 def find_by_username(self):1455 clear_console()1456 all_users = session.execute(select(Users)).scalars().all()1457 username = input(f"{Bcolors.BOLD}Введіть нік -> {Bcolors.ENDC}")1458 ans = -11459 for elem in all_users:1460 if elem.username == username:1461 ans = elem.id1462 q = session.execute(select(self.base).where(self.base.user_id == ans)).scalars()1463 self.print_table(self.marsh.dump(q, many=True))1464 self.find = True1465 def find_by_admin(self):1466 q = session.execute(select(self.base).where(self.base.admin == 1)).scalars()1467 self.print_table(self.marsh.dump(q, many=True))1468 self.find = True1469 def find_by_user(self):1470 q = session.execute(select(self.base).where(self.base.admin == 0)).scalars()1471 self.print_table(self.marsh.dump(q, many=True))1472 self.find = True1473 def menu(self):1474 self.print_table(self.all_rows(self.page))1475 while True:1476 if not self.find:1477 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1478 for idx, elem in enumerate(self.funcs_view_main):1479 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1480 func = input(" -> ")1481 if func.isnumeric():1482 if int(func) in list(range(1, len(self.funcs_view_main)+1)):1483 if int(func) == len(self.funcs_view_main):1484 break1485 else:1486 self.funcs_view_call_main[int(func)]()1487 else:1488 self.print_table(self.all_rows(self.page))1489 else:1490 self.print_table(self.all_rows(self.page))1491 else:1492 while True:1493 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")1494 func = input(" -> ")1495 if func.isnumeric():1496 if int(func) == 1:1497 self.find = False1498 break1499 self.page = 11500 clear_console()1501 self.print_table(self.all_rows(self.page))1502 return1503 def menu_edit(self):1504 self.print_table(self.all_rows(self.page))1505 while True:1506 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1507 for idx, elem in enumerate(self.funcs_edit_main):1508 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1509 func = input(" -> ")1510 if func.isnumeric():1511 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):1512 if int(func) == len(self.funcs_edit_main):1513 break1514 else:1515 self.funcs_edit_call_main[int(func)]()1516 else:1517 self.print_table(self.all_rows(self.page))1518 else:1519 self.print_table(self.all_rows(self.page))1520 return1521class RegDateView:1522 def __init__(self):1523 self.base = RegDates1524 self.columns = self.base.__table__.columns1525 self.marsh = RegDatesMarsh()1526 self.page = 11527 self.find = False1528 self.funcs_edit_main = ["Видалити", "Вперед", "Назад", "Вихід"]1529 self.funcs_edit_call_main = {1: self.delete}1530 self.funcs_view_main = ["Пошук по ніку", "Пошук по реєстрації", "Пошук по активності",1531 "Вперед", "Назад", "Вихід"]1532 self.funcs_view_call_main = {1: self.find_by_username, 2: self.find_by_reg, 3: self.find_by_activity,1533 4: self.next, 5: self.back}1534 def table(self):1535 t = Table(title=f"{self.base.__tablename__}\n Page: {self.page}")1536 for column in self.columns:1537 t.add_column(column.name, justify="center")1538 return t1539 def print_table(self, rows):1540 table = self.table()1541 for row in rows:1542 table.add_row(str(row["id"]), str(row["user_id"]), f"{row['reg_date']}", f"{row['last_activity']}")1543 clear_console()1544 rich_print(table)1545 def next(self):1546 if len(self.all_rows(self.page+1)) > 0:1547 self.page += 11548 self.print_table(self.all_rows(self.page))1549 else:1550 self.print_table(self.all_rows(self.page))1551 def back(self):1552 if self.page != 1:1553 self.page -= 11554 self.print_table(self.all_rows(self.page))1555 def all_rows(self, page):1556 return self.marsh.dump(session.execute(select(self.base)1557 .limit(5).offset(5*(page-1))).scalars(), many=True)1558 def delete(self):1559 all_dates = session.execute(select(self.base)).scalars().all()1560 while True:1561 idx = input(f"{Bcolors.BOLD}Введіть id -> {Bcolors.ENDC}")1562 if idx.isnumeric():1563 for elem in all_dates:1564 if elem.id == int(idx):1565 session.delete(session.query(Salts).where(Salts.user_id == elem.user_id).first())1566 session.delete(session.query(Perm).where(Perm.user_id == elem.user_id).first())1567 session.delete(session.query(Users).where(Users.id == elem.user_id).first())1568 session.delete(elem)1569 session.commit()1570 self.print_table(self.all_rows(1))1571 return1572 elif all_dates[-1] == elem:1573 print(f"{Bcolors.FAIL}Таких ключів не існує{Bcolors.ENDC}")1574 else:1575 print(f"{Bcolors.FAIL}Не правильний id{Bcolors.ENDC}")1576 def find_by_username(self):1577 clear_console()1578 all_users = session.execute(select(Users)).scalars().all()1579 username = input(f"{Bcolors.BOLD}Введіть нік -> {Bcolors.ENDC}")1580 ans = -11581 for elem in all_users:1582 if elem.username == username:1583 ans = elem.id1584 q = session.execute(select(self.base).where(self.base.user_id == ans)).scalars()1585 self.print_table(self.marsh.dump(q, many=True))1586 self.find = True1587 def find_by_reg(self):1588 print(f"{Bcolors.FAIL}Вводити такого типу -> 2021-07-25{Bcolors.ENDC}")1589 min_date = input(f"{Bcolors.BOLD}Введіть першу дату -> {Bcolors.ENDC}")1590 max_date = input(f"{Bcolors.BOLD}Введіть другу дату -> {Bcolors.ENDC}")1591 q = session.execute(select(self.base).where(self.base.reg_date.between(min_date, max_date))).scalars()1592 self.print_table(self.marsh.dump(q, many=True))1593 self.find = True1594 def find_by_activity(self):1595 print(f"{Bcolors.FAIL}Вводити такого типу -> 2021-07-25{Bcolors.ENDC}")1596 min_date = input(f"{Bcolors.BOLD}Введіть першу дату -> {Bcolors.ENDC}")1597 max_date = input(f"{Bcolors.BOLD}Введіть другу дату -> {Bcolors.ENDC}")1598 q = session.execute(select(self.base).where(self.base.last_activity.between(min_date, max_date))).scalars()1599 self.print_table(self.marsh.dump(q, many=True))1600 self.find = True1601 def menu(self):1602 self.print_table(self.all_rows(self.page))1603 while True:1604 if not self.find:1605 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1606 for idx, elem in enumerate(self.funcs_view_main):1607 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1608 func = input(" -> ")1609 if func.isnumeric():1610 if int(func) in list(range(1, len(self.funcs_view_main)+1)):1611 if int(func) == len(self.funcs_view_main):1612 break1613 else:1614 self.funcs_view_call_main[int(func)]()1615 else:1616 self.print_table(self.all_rows(self.page))1617 else:1618 self.print_table(self.all_rows(self.page))1619 else:1620 while True:1621 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}\n{Bcolors.BOLD}[1] - Повернутись{Bcolors.ENDC}")1622 func = input(" -> ")1623 if func.isnumeric():1624 if int(func) == 1:1625 self.find = False1626 break1627 self.page = 11628 clear_console()1629 self.print_table(self.all_rows(self.page))1630 return1631 def menu_edit(self):1632 self.print_table(self.all_rows(self.page))1633 while True:1634 print(f"{Bcolors.HEADER} Дії {Bcolors.ENDC}")1635 for idx, elem in enumerate(self.funcs_edit_main):1636 print(f"{Bcolors.BOLD}[{idx+1}] - {elem}{Bcolors.ENDC}")1637 func = input(" -> ")1638 if func.isnumeric():1639 if int(func) in list(range(1, len(self.funcs_edit_main)+1)):1640 if int(func) == len(self.funcs_edit_main):1641 break1642 else:1643 self.funcs_edit_call_main[int(func)]()1644 else:1645 self.print_table(self.all_rows(self.page))1646 else:1647 self.print_table(self.all_rows(self.page))...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

...3 # example 14 f1 = Factor(['A'], \5 [['t', 'f']], \6 [0.9, 0.1])7 f1.print_table()8 f2 = Factor(['A', 'B'], \9 [['t', 'f'], ['t', 'f']], \10 [0.9, 0.1, 0.4, 0.6])11 f2.print_table()12 f3 = Factor(['B', 'C'], \13 [['t', 'f'], ['t', 'f']], \14 [0.7, 0.3, 0.2, 0.8])15 f3.print_table()16 fL = [f1, f2, f3]17 qL = ['C']18 hL = ['A', 'B']19 eL = dict()20 fRes = Factor.inference(fL, qL, hL, eL)21 fRes.print_table()22def ex2():23 # f1 = Factor(['A'], \24 # [['t', 'f']], \25 # [0.6, 0.4])26 # # f1.print_table()27 # f2 = Factor(['A', 'B'], \28 # [['t', 'f'], ['t', 'f']], \29 # [0.2, 0.8, 0.75, 0.25])30 # # f2.print_table()31 f3 = Factor(['A', 'C'], \32 [['t', 'f'], ['t', 'f']], \33 [0.8, 0.2, 0.1, 0.9])34 f4 = Factor(['D', 'B', 'C'], \35 [['t', 'f'], ['t', 'f'], ['t', 'f']], \36 [0.95, 0.05, 0.9, 0.1, 0.8, 0.2, 0.0, 1.0])37 f5 = Factor(['C', 'E'], \38 [['t', 'f'], ['t', 'f']], \39 [0.7, 0.3, 0.0, 1.0])40 # res1 = Factor.multiply(f3, f5)41 # res1.print_table()42 # res2 = Factor.multiply(f5, res1)43 # res2.print_table()44 # fRes = Factor.multiply(f1, f3)45 # fRes.print_table()46 # fRes = Factor.sumout(fRes, 'A')47 # fRes.print_table()48 # fL = [f1, f2]49 # qL = ['B']50 # hL = ['A']51 # eL = dict()52 # f6 = Factor.inference(fL, qL, hL, eL)53 # f6.print_table()54 # fL = [f1, f3]55 # qL = ['C']56 # hL = ['A']57 # eL = dict()58 # f7 = Factor.inference(fL, qL, hL, eL)59 # f7.print_table()60 # fL = [f6, f7, f4]61 # qL = ['D']62 # hL = ['B', 'C']63 # eL = dict()64 # f8 = Factor.inference(fL, qL, hL, eL)65 # f8.print_table()66def q2b1():67 f1 = Factor(['Trav'], \68 [['t', 'f']], \69 [0.05, 0.95])70 f2 = Factor(['Fraud', 'Trav'], \71 [['t', 'f'], ['t', 'f']], \72 [0.01, 0.004, 0.99, 0.996])73 fL = [f1, f2]74 qL = ['Fraud']75 hL = ['Trav']76 eL = dict()77 fRes = Factor.inference(fL, qL, hL, eL)78 fRes.print_table()79def q2b2():80 f1 = Factor(['Trav'], \81 [['t', 'f']], \82 [0.05, 0.95])83 f2 = Factor(['Fraud', 'Trav'], \84 [['t', 'f'], ['t', 'f']], \85 [0.01, 0.004, 0.99, 0.996])86 f3 = Factor(['FP', 'Fraud', 'Trav'], \87 [['t', 'f'], ['t', 'f'], ['t', 'f']], \88 [0.9, 0.1, 0.9, 0.01, 0.1, 0.9, 0.1, 0.99])89 f4 = Factor(['IP', 'Fraud', 'OC'], \90 [['t', 'f'], ['t', 'f'], ['t', 'f']], \91 [0.15, 0.051, 0.1, 0.001, 0.85, 0.949, 0.9, 0.999])92 f5 = Factor(['CRP', 'OC'], \93 [['t', 'f'], ['t', 'f']], \94 [0.1, 0.01, 0.9, 0.99])95 f6 = Factor(['OC'], \96 [['t', 'f']], \97 [0.8, 0.2])98 fL = [f1, f2, f3, f4, f5, f6]99 qL = ['Fraud']100 hL = ['Trav', 'OC']101 eL = dict(FP = 't', IP = 'f', CRP = 't')102 fRes = Factor.inference(fL, qL, hL, eL)103 fRes.print_table()104def q2c():105 f1 = Factor(['Trav'], \106 [['t', 'f']], \107 [0.05, 0.95])108 f2 = Factor(['Fraud', 'Trav'], \109 [['t', 'f'], ['t', 'f']], \110 [0.01, 0.004, 0.99, 0.996])111 f3 = Factor(['FP', 'Fraud', 'Trav'], \112 [['t', 'f'], ['t', 'f'], ['t', 'f']], \113 [0.9, 0.1, 0.9, 0.01, 0.1, 0.9, 0.1, 0.99])114 f4 = Factor(['IP', 'Fraud', 'OC'], \115 [['t', 'f'], ['t', 'f'], ['t', 'f']], \116 [0.15, 0.051, 0.1, 0.001, 0.85, 0.949, 0.9, 0.999])117 f5 = Factor(['CRP', 'OC'], \118 [['t', 'f'], ['t', 'f']], \119 [0.1, 0.01, 0.9, 0.99])120 f6 = Factor(['OC'], \121 [['t', 'f']], \122 [0.8, 0.2])123 fL = [f1, f2, f3, f4, f5, f6]124 qL = ['Fraud']125 hL = ['OC']126 eL = dict(FP = 't', IP = 'f', CRP = 't', Trav = 't')127 fRes = Factor.inference(fL, qL, hL, eL)128 fRes.print_table()129def q2d():130 f1 = Factor(['Trav'], \131 [['t', 'f']], \132 [0.05, 0.95])133 f2 = Factor(['Fraud', 'Trav'], \134 [['t', 'f'], ['t', 'f']], \135 [0.01, 0.004, 0.99, 0.996])136 f3 = Factor(['FP', 'Fraud', 'Trav'], \137 [['t', 'f'], ['t', 'f'], ['t', 'f']], \138 [0.9, 0.1, 0.9, 0.01, 0.1, 0.9, 0.1, 0.99])139 f4 = Factor(['IP', 'Fraud', 'OC'], \140 [['t', 'f'], ['t', 'f'], ['t', 'f']], \141 [0.15, 0.051, 0.1, 0.001, 0.85, 0.949, 0.9, 0.999])142 f5 = Factor(['CRP', 'OC'], \143 [['t', 'f'], ['t', 'f']], \144 [0.1, 0.01, 0.9, 0.99])145 f6 = Factor(['OC'], \146 [['t', 'f']], \147 [0.8, 0.2])148 149 fL = [f1, f2, f3, f4, f5, f6]150 qL = ['Fraud']151 hL = ['Trav', 'FP', 'OC', 'CRP']152 eL = dict(IP = 't')153 fRes = Factor.inference(fL, qL, hL, eL)154 fRes.print_table()155 print('=========================================')156 fL = [f1, f2, f3, f4, f5, f6]157 qL = ['Fraud']158 hL = ['Trav', 'FP', 'OC']159 eL = dict(IP = 't', CRP = 't')160 fRes = Factor.inference(fL, qL, hL, eL)161 fRes.print_table()162def main():163 q2b1()164 q2b2()165 q2c()166 q2d()...

Full Screen

Full Screen

nQueen.py

Source:nQueen.py Github

copy

Full Screen

...10# print("\n".join(' o ' * i + ' X ' + ' o ' * (N-i-1) for i in combo) + "\n\n\n\n")11print("Iterative")12for i in range(7):13 N=i+414 def print_table():15 for row in range(N):16 print(table[row])17 def put_queen(x,y):18 if table[y][x] == 0:19 for m in range(N):20 table[y][m] = 121 table[m][x] = 122 table[y][x] = 223 if y+m <= N-1 and x+m <= N-1:24 table[y+m][x+m] = 125 if y-m >= 0 and x+m <= N-1:26 table[y-m][x+m] = 127 if y+m <= N-1 and x-m >= 0:28 table[y+m][x-m] = 129 if y-m >= 0 and x-m >= 0:30 table[y-m][x-m] = 131 return True32 else:33 return False34 start=time.perf_counter()35 table = [[0]*N for _ in range(N)]36 temp = []37 for i in range(N):38 temp.append(i) 39 perms = permutations(temp)40 num_comb = 041 for perm in perms:42 if put_queen(perm[0], 0) == True :43 if N==1:44 # print_table()45 num_comb += 146 # print(f"solution{num_comb}")47 # print(" ")48 elif N>1:49 if put_queen(perm[1], 1) == True :50 if N==2:51 # print_table()52 num_comb += 153 # print(f"solution{num_comb}")54 # print(" ")55 elif N>2: 56 if put_queen(perm[2], 2) == True:57 if N==3:58 # print_table()59 num_comb += 160 # print(f"solution{num_comb}")61 # print(" ")62 elif N>3:63 if put_queen(perm[3], 3) == True:64 if N==4:65 # print_table()66 num_comb += 167 # print(f"solution{num_comb}")68 # print(" ")69 elif N>4 :70 if put_queen(perm[4], 4) == True:71 if N==5:72 # print_table()73 num_comb += 174 # print(f"solution{num_comb}")75 # print(" ")76 elif N>5:77 if put_queen(perm[5], 5) == True:78 if N==6:79 # print_table()80 num_comb += 181 # print(f"solution{num_comb}")82 # print(" ")83 elif N>6:84 if put_queen(perm[6], 6) == True:85 if N==7 :86 # print_table()87 num_comb += 188 # print(f"solution{num_comb}")89 # print(" ")90 elif N>7:91 if put_queen(perm[7], 7) == True:92 if N==8 :93 # print_table()94 num_comb += 195 # print(f"solution{num_comb}")96 # print(" ")97 elif N>8:98 if put_queen(perm[8], 8) == True:99 if N==9 :100 # print_table()101 num_comb += 1102 # print(f"solution{num_comb}")103 # print(" ")104 elif N>9:105 if put_queen(perm[9], 9) == True:106 if N==10 :107 # print_table()108 num_comb += 1109 # print(f"solution{num_comb}")110 # print(" ")111 table = [[0] * N for _ in range(N)]112 stop=time.perf_counter()113 print("N = {}".format(N))...

Full Screen

Full Screen

queries.py

Source:queries.py Github

copy

Full Screen

...11 cursor = conn.cursor()12 return cursor13 except:14 return None15def print_table(table):16 print("\n====RESULT TABLE====")17 cols = len(table[0])18 format_string = "%s " * cols19 for row in table:20 print (format_string % tuple(row))21 print("====================\n")22def run_query(query, cursor):23 cursor.execute(query)24 rows = cursor.fetchall()25 colnames = [desc[0] for desc in cursor.description]26 rows.insert(0, colnames)27 return rows28def exmaple_one(cursor):29 query = "SELECT count(*) from actors"30 result = run_query(query, cursor)31 print_table(result)32def exmaple_two(cursor):33 year = input('Choose a year: ')34 query = "SELECT count(*) from movies where year = " + year + ";"35 result = run_query(query, cursor)36 print_table(result)37def exmaple_three(cursor):38 title = input('Choose a movie: ')39 query = "SELECT idmovies, title, year from movies where title = '" + title + "' and type = 'FF' ORDER BY year ASC;"40 movie_id_table = run_query(query, cursor)41 if len(movie_id_table) > 2:42 print_table(movie_id_table)43 movie_id = input('Pick a movie id: ')44 query = "SELECT character, fname, lname, genders FROM casts INNER JOIN actors on casts.idactors = actors.idactors where idmovies = " + movie_id + ";"45 result = run_query(query, cursor)46 print_table(result)47 else:48 print('No movie correspondes to the title: ' + title)49############################################################################50def query_one(cursor):51 query = "select actor.first_name,actor.last_name, actor_aka_names.aka_name from actor inner join actor_aka_names on actor.actor_id=actor_aka_names.actor_id where actor.first_name = 'James';"52 result = run_query(query, cursor)53 print_table(result)54def query_two(cursor):55 query = "select * from genre order by genre;"56 result = run_query(query, cursor)57 print_table(result)58def query_three(cursor):59 query = "select genre.genre, count(movie.title) from movie inner join genre_movie on movie.movie_id=genre_movie.movie_id inner join genre on genre_movie.genre_id=genre.genre_id group by genre.genre having count(movie.title) > 2500;"60 result = run_query(query, cursor)61 print_table(result)62def query_four(cursor):63 query = "select count(*) from actor where gender = 'M';"64 result = run_query(query, cursor)65 print_table(result)66def query_five(cursor):67 query = "select count(genre) from genre;"68 result = run_query(query, cursor)69 print_table(result)70def query_six(cursor):71 query = "select max(year) from movie;"72 result = run_query(query, cursor)73 print_table(result)74def query_seven(cursor):75 query = "select movie.title, aka_movie.aka_movie_name from movie right outer join aka_movie on movie.movie_id=aka_movie.movie_id where movie.year > 2014;"76 result = run_query(query, cursor)77 print_table(result)78def query_eight(cursor):79 query = "select keyword.keyword, count(movie.title) from movie inner join movie_keyword on movie.movie_id=movie_keyword.movie_id inner join keyword on movie_keyword.keyword_id=keyword.keyword_id group by keyword.keyword having count(movie.title) > 1000 order by count(movie.title);"80 result = run_query(query, cursor)81 print_table(result)82def query_nine(cursor):83 query = "select aka_movie.aka_movie_name from aka_movie where year >2016 order by year;"84 result = run_query(query, cursor)85 print_table(result)86def query_ten(cursor):87 query = "select movie.title from movie where movie.title='Interstellar';"88 result = run_query(query, cursor)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful