جلسه سی و نهم
مثال اموزشگاه
پروژه شی گرایی آموزشگاه که شامل کلاس ها Function ها و بدنه اصلی برنامه است .
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
<xmp>mport random from colorama import Fore from string import * d1 = [] da = [] d4 = {1: 'Saturday', 2: 'Sunday', 3: 'Monday', 4: 'Tuesday', 5: 'Wednesday', 6: 'Thursday'} daneshjo = [] daneshjo1 = [] karmand = [] d_doros = {"python1": 2800000, "python2": 4800000, "c": 2200000, "c++": 3450000, "network +": 3500000, "sql": 4500000, "web1": 3500000, "web2": 4500000, "icdl": 2300000} lst_d = [] ost = [] # ------------------ Start Class and Function menu ------------------ class Person: def __init__(self): self._name = '' self._fa = '' self._dname = '' self._code = '' self._mob = '' self._jensiat = '' # -------------------------------------------------- @property def name(self): return self._name @name.setter def name(self, value): if len(value) > 2 and value.isalpha(): self._name = value else: print('Error Name !!') self._name = 'Error Name ' # -------------------------------------------------- @property def family(self): return self._fa @family.setter def family(self, value): if len(value) > 2 and value.isalpha(): daneshjo.append(value) self._fa = value else: print('Error Family !!') self._fa = 'Error Family' # -------------------------------------------------- @property def mobile(self): return self._mob @mobile.setter def mobile(self, value): if value.isdigit() and len(value) == 11: self._mob = value else: print('Error Mobile !!') # ------------------------------------------------- @property def code_mli(self): return self._code @code_mli.setter def code_mli(self, value): if len(value) == 10 and value.isdigit(): self._code = value else: print('Error Mobile !!') # -------------------------------------------------- def create_pass(self): s = ascii_lowercase + ascii_uppercase + punctuation + digits temp_aps = '' for i in range(7): temp_aps += random.choice(s) self._pas = temp_aps # -------------------------------------------------- @property def dadname(self): return self._dname @dadname.setter def dadname(self, value): if len(value) > 2 and value.isalpha(): self._dname = value else: print('Error Name !!') # ----------------------import random from colorama import Fore from string import * d1 = [] da = [] d4 = {1: 'Saturday', 2: 'Sunday', 3: 'Monday', 4: 'Tuesday', 5: 'Wednesday', 6: 'Thursday'} daneshjo = [] daneshjo1 = [] karmand = [] d_doros = {"python1": 2800000, "python2": 4800000, "c": 2200000, "c++": 3450000, "network +": 3500000, "sql": 4500000, "web1": 3500000, "web2": 4500000, "icdl": 2300000} lst_d = [] ost = [] # ------------------ Start Class and Function menu ------------------ class Person: def __init__(self): self._name = '' self._fa = '' self._dname = '' self._code = '' self._mob = '' self._jensiat = '' # -------------------------------------------------- @property def name(self): return self._name @name.setter def name(self, value): if len(value) > 2 and value.isalpha(): self._name = value else: print('Error Name !!') self._name = 'Error Name ' # -------------------------------------------------- @property def family(self): return self._fa @family.setter def family(self, value): if len(value) > 2 and value.isalpha(): daneshjo.append(value) self._fa = value else: print('Error Family !!') self._fa = 'Error Family' # -------------------------------------------------- @property def mobile(self): return self._mob @mobile.setter def mobile(self, value): if value.isdigit() and len(value) == 11: self._mob = value else: print('Error Mobile !!') # ------------------------------------------------- @property def code_mli(self): return self._code @code_mli.setter def code_mli(self, value): if len(value) == 10 and value.isdigit(): self._code = value else: print('Error Mobile !!') # -------------------------------------------------- def create_pass(self): s = ascii_lowercase + ascii_uppercase + punctuation + digits temp_aps = '' for i in range(7): temp_aps += random.choice(s) self._pas = temp_aps # -------------------------------------------------- @property def dadname(self): return self._dname @dadname.setter def dadname(self, value): if len(value) > 2 and value.isalpha(): self._dname = value else: print('Error Name !!') # -------------------------------------------------- @property def jensiat(self): return self._jensiat @jensiat.setter def jensiat(self, value): if value == 'man': self._jensiat = 'man' elif value == 'woman': self._jensiat = 'woman' else: self._jensiat = 'None' # -------------------------------------------------- def byby(): print('khoafezi kon') exit() # -------------------------------------------------- class Daneshjo(Person): def __init__(self): super().__init__() self._vaziat = '' self._doreh = "" self.mablagh_doreh = 0 self._sh_card = '' self.pas = '' # -------------------------------------------------- def create_pass(self): s = ascii_lowercase + ascii_uppercase + punctuation + digits temp_a = '' for i in range(7): temp_a += random.choice(s) self.pas = temp_a return self.pas # -------------------------------------------------- @property def Doreha (self): return self._doreh @Doreha.setter def Doreha(self, value): if value in d_doros.keys(): self._doreh = value elif value == "None": exit() def doreha(self, value): if value in d_doros.keys(): d1.append(value) else: print('Error !') # -------------------------------------------------- @property def vaziat_mali(self): return self._vaziat @vaziat_mali.setter def vaziat_mali(self, value): if value == 1: return 'ّBedehkar' elif value == 2: return 'Bestankar' else: return 'None' # -------------------------------------------------- @property def user_n(self): return self._code # -------------------------------------------------- def mablagh_class(self): pool = d_doros[(self.Doreha)] self.mablagh_doreh = int(pool) class Ostad(Person): def __init__(self): super().__init__() self._hogo = '' self._madrak = '' self.sabege = '' self._postal = '' # -------------------------------------------------- @property def code_personeli(self): return self._code # -------------------------------------------------- @property def pas_personeli(self): return self.pas # -------------------------------------------------- class Karmand(Person): def __init__(self): super().__init__() self.cs = '' self._fa = '' self._sh_card = '' self._sh_shaba = '' self._sh_hesab = '' # -------------------------------------------------- @property def cname(self): return self.cs @cname.setter def cname(self, value): if len(value) > 2 and value.isalpha(): karmand.append(value) print('dc') else: print('Error Name and Family!!') self.cs = ' Error Name and Family!! ' # -------------------------------------------------- @property def cfamily(self): return self._fa @cfamily.setter def cfamily(self, value): if len(value) > 3 and value.isalpha(): karmand.append(value) else: print('Error Family !!') self._fa = 'Error Family ' # -------------------------------------------------- @property def shomare1_card(self): return self.shomare_card @shomare1_card.setter def shomare1_card(self, value): if len(value) == 16 and value.isdigit(): self.shomare_card = value else: print('Error Card Number !!') # -------------------------------------------------- @property def shomare_shaba(self): return self._sh_shaba @shomare_shaba.setter def shomare_shaba(self, value): if len(value) == 16 and value.isdigit(): self._sh_shaba = value else: print('Error Shaba Number !!') # -------------------------------------------------- @property def shomare_hesab(self): return self._sh_hesab @shomare_hesab.setter def shomare_hesab(self, value): if len(value) == 13 and value.isdigit(): self._sh_hesab = value else: print('Error Hesab Number !!') # -------------------------------------------------- class Modiriyat(Person): def __init__(self): super().__init__() self._zamanhozor = '' @property def hozor_modir(self): return self._zamanhozor @hozor_modir.setter def hozor_modir(self, val): for e in d4: if val in e: self._zamanhozor = val else: print('None') # -------------------------------------------------- if __name__ == "__main__": while True: print(Fore.MAGENTA + '0 ==> Modiriyad') print('1 ==> Daneshjo') print('2 ==> ostad ') print('3 ==> karmand') print('5 ==> Time hozor karandan ') print('4 ==> exit') ans = int(input(Fore.BLUE + 'Menu :')) print(Fore.GREEN + 'Loading...') print(Fore.MAGENTA + 'tLoading...') print(Fore.GREEN + 'ttLoading...') print(Fore.MAGENTA + 'tttLoading...') print(Fore.GREEN + 'ttttLoading...') print(Fore.MAGENTA + 'tttttLoading...') # ----------------------------- بخش خروجی ---------------------------- if ans == 4: print(Fore.RED + '-/ '*30) print(Fore.GREEN + 'By By') break # -------------------------- بخش حضور کارمندان ------------------------ elif ans == 5: print(Fore.GREEN + 'every day t 8:30 -- 16:30n') # ----------------------------- بخش دانشجو ---------------------------- elif ans == 1: while True: print(Fore.RED + '-_' * 30) print(Fore.MAGENTA + 't 1 - add daneshjo') print('t2 - edit daneshjo ') print('t3 - delete daneshjo ') print('t4 - search daneshjo ') print('t5 - Doreha') print('t6 - print daneshjo ') print('t7 - print daneshjo list') print('t8 - exit panel student ') x = int(input('Menu (1-2-3...)?:')) if x == 1: stu = Daneshjo() stu._name = input('name :') stu._family = input('family : ') daneshjo.append((stu._name, stu._family)) daneshjo1.append(stu._family) while True: stu._mob = input('Mobile Number(09..) : ') if len(stu._mob) == 11 and stu._mob.isdigit() and stu._mob.startswith("09"): print(Fore.GREEN + 'successfully') break while True: stu._code = (input(Fore.MAGENTA + 'code Meli (10) : ')) if len(stu._code) == 10 and stu._code.isdigit(): print(Fore.GREEN + 'successfully') break stu.jensiat = int(input(Fore.MAGENTA + '1- zan t 2 - mard')) stu.create_pass() for i in d_doros.keys(): print(i, " ", d_doros[i]) while True: n = int(input('Doreh :')) if n in d_doros.keys(): lst_d.append(d_doros.get(n)) else: print('Error!') y = input('continue ?(y-n) ') if y == 'n': break print(lst_d) elif x == 5: print(Fore.YELLOW + '', d_doros.values()) elif x == 4: c = input('Name for search:') tuple = daneshjo for i in daneshjo: if c in daneshjo: print('hast') else: print('Nis') elif x == 3: print(Fore.YELLOW + f'List daneshjoha n{daneshjo}') print(Fore.GREEN + 'Which daneshjo to remove?', Fore.RED + '(Enter just Family)') de = input() daneshjo1.remove(de) print(Fore.GREEN + 'remove successfully') print(Fore.YELLOW + f'List daneshjoha n{daneshjo1}') elif x == 7: print(Fore.BLUE + 'all daneshjo name and Family') print(daneshjo) elif x == 6: print(Fore.BLUE + 'Name :', Fore.GREEN + stu._name, Fore.BLUE + 'nFamily :',Fore.GREEN + stu._fa) print(Fore.BLUE + 'jensiat :', Fore.GREEN + stu._jensiat, Fore.BLUE + 'n Doreha :',Fore.GREEN + stu.doreh, lst_d) print(Fore.BLUE + 'code Meli and your username :', Fore.GREEN + stu._code) print(Fore.BLUE + 'password :', Fore.GREEN + stu.pas) print(Fore.BLUE + 'phone Number :', Fore.GREEN + stu._mob) elif x == 8: break elif ans == 0: while True: print(Fore.LIGHTWHITE_EX + 'Welcome to the administration section') print(Fore.YELLOW + '1 - Login') print('2 - Management presence time') print('3 - Exit this menu ') b = int(input(Fore.BLUE + 'Menu :')) if b == 3: print(Fore.RED + 'Checkout done') break elif b == 1: stu = Modiriyat() stu.name = input('Name :') stu.family = input('Family : ') while True: stu.mob = input('Mobile Number(09..) : ') if len(stu.mob) == 11 and stu.mob.isdigit() and stu.mob.startswith("09"): print(Fore.GREEN + 'successfully') break while True: stu.code = (input(Fore.MAGENTA + 'Code Meli(10) : ')) if len(stu.code) == 10 and stu.code.isdigit(): print(Fore.GREEN + 'successfully') break stu.jensiat = int(input(Fore.MAGENTA + '1- zan t 2 - mard')) for i in d4: print(i, f' ', {d4[i]}) while True: ans = int(input('which => ')) for i in d4.keys(): if ans in d4.keys(): da.append(d4.get(ans)) v = input('repeat???') if v == 'n': break elif b == 2: print('Management presence time(Days)') print(da) elif ans == 3: while True: print(Fore.LIGHTWHITE_EX + 'Welcome to the employee section') print(Fore.MAGENTA + '1 - add karmand') print('2 - List karmand') print('3 - remove karmand') print('4 - exit karmand menu') an = int(input(Fore.BLUE + '1 - 2 - 3 - 4 ?')) if an == 4: print('You have left this section') break elif an == 1: stu = Karmand() stu.cs = input('Name and family:') karmand.append(stu.cs) while True: stu.mob = input('Mobile Number(09..) : ') if len(stu.mob) == 11 and stu.mob.isdigit() and stu.mob.startswith("09"): print(Fore.GREEN + 'successfully') break while True: stu.code = (input(Fore.MAGENTA + 'Code Meli(10) : ')) if len(stu.code) == 10 and stu.code.isdigit(): print(Fore.GREEN + 'successfully') break stu.jensiat = int(input(Fore.MAGENTA + '1- zan t 2 - mard')) while True: print('Enter your national bank card number:') print(Fore.RED + 'just meli bank card', Fore.GREEN + '6037') stu.shomare_card = input('==>') if len(stu.shomare_card) == 16 and stu.shomare_card.isdigit() and stu.shomare_card.startswith("6037"): print(Fore.GREEN + 'successfully') print('End') break while True: print('Enter your shaba bank number:') stu._sh_shaba = input('==> IR') if len(stu._sh_shaba) == 16 and stu._sh_shaba.isdigit(): print(Fore.GREEN + 'successfully') print('End') break while True: print('Enter your shomare Hesab bank:') stu._sh_hesab = input('==>') if len(stu._sh_hesab) == 13 and stu._sh_hesab.isdigit(): print(Fore.GREEN + 'successfully') print('End') break ee = input('Bime dard?(y-n)') if ee == 'y': z = int(input('chand sal?')) c = int(input('chand mah?')) x = (z * 12) + c print(Fore.GREEN + f'mahhay sabt shode bime tamin.ir {x}') else: print('باید حداقل29 سال سابقه داشته باشی عزیزم') elif an == 2: print(Fore.GREEN + 'Karmand List') print(karmand) elif an == 3: rem = input(Fore.RED + 'Which karmand to remove?') while True: print(Fore.LIGHTRED_EX + 'Are you sure?') ans = input('Y/N ?') if ans == 'y': karmand.remove(rem) print(Fore.GREEN + 'Deleted') break elif ans == 2: while True: print(Fore.LIGHTWHITE_EX + 'Welcome to the Teacher section') print(Fore.MAGENTA + '1 - add ostad') print('2 - List ostad') print('3 - remove ostad') print('4 - exit ostad menu') an = int(input(Fore.BLUE + '1 - 2 - 3 - 4 ?')) if an == 4: print('You have left this section') break elif an == 1: stu = Ostad() stu.cs = input('Name and Family:') ost.append(stu.cs) while True: stu.mob = input('Mobile Number(09..) : ') if len(stu.mob) == 11 and stu.mob.isdigit() and stu.mob.startswith("09"): print(Fore.GREEN + 'successfully') break while True: stu.code = (input(Fore.MAGENTA + 'Code Meli(10) : ')) if len(stu.code) == 10 and stu.code.isdigit(): print(Fore.GREEN + '~~~~~') break stu.jensiat = int(input(Fore.MAGENTA + '1- zan t 2 - mard')) while True: print('Enter your national bank card number:') print(Fore.RED + 'just meli bank card', Fore.GREEN + '6037') stu.shomare_card = input('==>') if len(stu.shomare_card) == 16 and stu.shomare_card.isdigit() and stu.shomare_card.startswith("6037"): print(Fore.GREEN + 'successfully') print('End') break while True: print('Enter your shaba bank number:') stu._sh_shaba = input('==> IR') if len(stu._sh_shaba) == 16 and stu._sh_shaba.isdigit(): print(Fore.GREEN + 'successfully') print('End') break while True: print('Enter your shomare Hesab bank:') stu._sh_hesab = input('==>') if len(stu._sh_hesab) == 13 and stu._sh_hesab.isdigit(): print(Fore.GREEN + 'successfully') print('End') break qq = int(input('sabege kari: ')) elif an == 2: print(Fore.GREEN + 'ostad List') print(ost) elif an == 3: rem = input(Fore.RED + 'Which ostad to remove?') while True: print(Fore.LIGHTRED_EX + 'Are you sure?') ans = input('Y/N ?') if ans == 'y': ost.remove(rem) print(Fore.GREEN + 'Deleted') break</xmp> |
دکوریتور ها میتوانند به متود ویژگی خاصی دهند.
Import : وارد کردن یک ماژول یا یک بسته ( package) به برنامه و استفاده از آن است.
ماژول : فایلی شامل توابع، کلاس ها، متغییر ها و …
بسته ( package ) :مجموع یک یا چند ماژول مرتبط بهم که در یک مسیر مشترک هستند.
توضیحات کد بالا :
Import ها:
|
1 2 3 |
<xmp>import random</xmp> |
random یک ماژول برای تولید اعداد تصادفی (اعداد صحیح، اعداداعشاری، عناصر از یک لیست بصورت تصادفی) و سایر اعمال تصادفی است.
|
1 2 3 |
<xmp>from colorama import Fore</xmp> |
از ماژول colorama کلاس fore را وارد برنامه کن(برای نمایش متن رنگی(رنگ دلخواه) در ترمینال).
|
1 2 3 |
<xmp>from string import *</xmp> |
از ماژول string همه موجودیت ها(توابع، کلاس ها، متغییر ها و …) را وارد برنامه کن.
ساختار کد:
|
1 2 3 |
<xmp>d4 = {1: 'Saturday', 2: 'Sunday', 3: 'Monday', 4: 'Tuesday', 5: 'Wednesday', 6: 'Thursday'}</xmp> |
دیکشنری حاوی روزهای هفته
|
1 2 3 4 |
<xmp>d_doros = {"python1": 2800000, "python2": 4800000, "c": 2200000, "c++": 3450000, "network +": 3500000, "sql": 4500000, "web1": 3500000, "web2": 4500000, "icdl": 2300000}</xmp> |
دیکشنری حاوی نام کلاس های برگزاری(کلیدها) و هزینه هر دوره (مقدارها)
7 لیست تهی در ابتدا:
|
1 2 3 |
<xmp>d1 = []</xmp> |
لیستی از روزهای هفته برای هر دانشجو
|
1 2 3 |
<xmp>da = []</xmp> |
لیستی از روزهای هفته برای مدیریت.
|
1 2 3 |
<xmp>daneshjo = []</xmp> |
لیستی از تاپل ها(اسم و فامیل دانشجو).
|
1 2 3 |
<xmp>daneshjo1 = []</xmp> |
لیستی از رشته ها.
|
1 2 3 |
<xmp>karmand = []</xmp> |
لیستی از رشته ها اطلاعات کارمند.
|
1 2 3 |
<xmp>lst_d = []</xmp> |
لیستی از int(اعداد صحیح) قیمت دروس فرد.
|
1 2 3 |
<xmp>ost = []</xmp> |
لیستی از رشته ها (نام اساتید).
5 کلاس :
|
1 2 3 |
<xmp>class Person:</xmp> |
کلاس والد (اولیه).
|
1 2 3 |
<xmp>class Daneshjo(Person):</xmp> |
کلاس فرزند دانشجو (ارث برده از کلاس class Person.
|
1 2 3 |
<xmp>class Ostad(Person):</xmp> |
کلاس فرزند استاد (ارث برده از کلاس (class Person.
|
1 2 3 |
<xmp>class Karmand(Person):</xmp> |
کلاس فرزند کارمند (ارث برده از کلاس (class Person.
|
1 2 3 |
<xmp>class Modiriyat(Person):</xmp> |
کلاس فرزند مدیریت (ارث برده از کلاس (class Person.