- Iterator obyektini qaytaruvchi funksiyaning maxsus turi.
- Qo'ng'iroq qiluvchi tomonidan so'ralganda birma-bir qiymatlar ketma-ketligini ishlab chiqaradi.
- Generatorlar ijroni to'xtatib turish va ketma-ketlikda keyingi qiymatni qaytarish uchun "yield" kalit so'zi yordamida aniqlanadi.
- Bu xotiradan yanada samarali foydalanish imkonini beradi va cheksiz ketma-ketlikni yaratishi mumkin.
'''
Iterator
'''
import time
def answer():
result = []
time.sleep(3)
result.append(14)
time.sleep(3)
result.append(44)
time.sleep(3)
result.append(75)
return result
for x in answer():
print(x)
'''
Generator
'''
import time
def answer():
time.sleep(3)
yield 14
time.sleep(3)
yield 44
time.sleep(3)
yield 75
for x in answer():
print(x)
1677122996544.gif.mp4
'''
Generator range()
'''
import time
# def answer():
# while True:
# time.sleep(3)
# yield 75
#
#
# for x in answer():
# print(x * x)
def reverse(nums: list):
n = len(nums)
for x in range(n - 1, -1, -1):
yield nums[x]
# for x in reverse([1, 2, 3]):
# print(x)
# print(list(reverse([1, 2, 3])))
iterator = iter(reverse([1, 2, 3, 4, 5]))
while True:
try:
n = next(iterator)
except StopIteration:
break
print(n)
for r in reverse([1, 2, 3, 4, 5]):
print(r)
for x in range(100000000): # range() generator
time.sleep(1)
print(x)
Dekoratorlar yuqori darajadagi funktsiyalarni chaqirish uchun oddiy sintaksisni ta'minlaydi. Ta'rifga ko'ra, dekorator boshqa funktsiyani qabul qiladigan va ikkinchi funktsiyaning harakatini aniq o'zgartirmasdankengaytiradigan funktsiyadir.Bu chalkash tuyuladi, lekin aslida unday emas, ayniqsa dekorativlar qanday ishlashiga oid bir nechta misollarni ko'rganingizdan keyin. Ushbu maqoladagi barcha misollarni bu erda topishingiz mumkin.
def hello(name):
# ichki funksiya
def get_name():
return f"Hello, {name}!"
# ichki funksiyani qaytarish
return get_name
hello_func = hello("Sarvar")
print(hello_func())
def make_decorator(func):
def inner():
# Ichki funksiya orqali func xususiyatlarini o'zgartirishimiz mumkin
print("Dekorator ishlayapti")
func()
# Ichki funksiya qaytariladi
return inner
def my_func():
print("Dekorator uchun ishlatiladigan funksiya")
decorated_func = make_decorator(my_func)
decorated_func()
# Dekorator ishlayapti
# Dekorator uchun ishlatiladigan funksiya
Bitta funksiyada bir nechta decorator ishlatishimiz ham mumkin, bunda birinchisidan boshlab decoratorlar ishlab keladi.
def divide_decorator(func):
def divide_inner(a, b):
try:
return func(a, b)
except ZeroDivisionError:
print("Nolga bo'lish mumkin emas!")
return divide_inner
def second_zero(func): # zero 0 - > 1 function
def inner(a, b):
if b == 0:
b += 1
return func(a, b)
return inner
@second_zero
@divide_decorator
def divider(a, b):
return a / b
print(divider(10, 5)) # 2.0
print(divider(10, 0)) # 10.0
'''
Decorator
'''
import time
# def add(a, b):
# return a + b
#
#
# # print(add.__call__(1, 2))
# # print(add(1, 2))
#
#
# def calc(func, a, b):
# return func(a, b)
#
#
# result = calc(add, 1, 4)
# print(result)
# def adder(n):
# def inner(m):
# return 5 + m
# return inner
#
#
# add_5 = adder(5)
# def func():
# def another_func():
# return 1
# return another_func
#
# print(func()())
# def do():
# started = time.time()
# print(started)
# time.sleep(3)
# print("done")
# finished = time.time()
# print(finished)
# print(f"took {finished - started} seconds")
#
#
# do()
# def do1():
# started = time.time()
# print(started)
# time.sleep(3)
# print("done")
# finished = time.time()
# print(finished)
# print(f"took {finished - started} seconds")
#
#
# do1()
# def timer(func):
# started = time.time()
# func()
# fnished = time.time()
# print(f"took {fnished - started} seconds")
#
# timer(do())
# timer(do2())
# not called
# def timer(func):
# def inner():
# started = time.time()
# func()
# fnished = time.time()
# print(f"took {fnished - started} seconds")
#
# return inner
#
#
# # do = timer(do())
# # do2 timer(do2())
#
# do()
# do2()
# @timer
# def dos():
# time.sleep(3)
# print("done")
# def star(func):
# def inner():
# print("*" * 12)
# func()
# print("*" * 12)
#
# return inner
# def presnet(func):
# def inner():
# print("%" * 12)
# func()
# print("%" * 12)
#
# return inner
#
# @star
# @presnet
# def hello():
# print("Hello World")
#
#
# hello()
# def wrap(char):
# def wrapper(func):
# def inner():
# print(char * 12)
# func()
# print(char * 12)
#
# return inner
#
# return wrapper
#
#
# @wrap("%")
# @wrap("*")
# def hello():
# print("Hello World")
#
#
# hello()
'''
log nomli dekorator yarating, u qaysi funksiya qachon chaqirilganini va qachon funksiya tugagani vaqtnini ekranga chiqarsin. Vaqtni formatlashni https://strftime.org/ saytidan ko'rishingiz mumkin.
Misol:
@log
def hello():
print("hello")
hello()
Output:
- called function: hello at 8:43:35
hello
- finished function: hello at 8:43:36
'''
'''Context Managers
'''
# with open('file.txt', 'w') as file:
# file.write("Hello")
#
# file = open('file,txt', 'w')
# file.write('All Nc')
# file.close()
# class FileOpener:
# def __init__(self, filename, mode='r'):
# self.filename = filename,
# self.mode = mode
# self.opened_file = None
#
# def __enter__(self):
# print("Entered")
# return 1
#
# def __exit__(self, exc_type, exc_val, exc_tb):
# print('Exited')
#
#
# with FileOpener('file,txt', 'w') as file:
# print('file', file)
# print('nima_dir boldi')
# class FileOpener:
# def __init__(self, filename, mode='r'):
# self.filename = filename
# self.mode = mode
# self.opened_file = None
#
# def __enter__(self):
# print("Entered")
# self.opened_file = open(self.filename, self.mode)
# return self.opened_file
#
# def __exit__(self, exc_type, exc_val, exc_tb):
# print('Exited')
# if self.opened_file:
# self.opened_file.close()
#
#
# with FileOpener('file.txt', 'w') as file:
# print('file', file)
# file.write('All Nc')
# raise Exception
# print('nima_dir boldi')
from contextlib import contextmanager
@contextmanager
def file_opener(filename, mode):
file = open(filename, mode)
print('open file')
try:
yield file
finally:
file.close()
print('closed file')
with file_opener('files.txt', 'w') as file:
print('file', file)
raise Exception
'''
log nomli kontekst meneger yarating, u qaysi kontext qachon ochilganini va qachon kontext yopilganini vaqtnini ekranga chiqarsin.
Misol:
with log():
print("hello")
Output:
- context manager opened at 8:43:35
hello
- context manager closed at 8:43:36
'''
int
: Butun sonfloat
: O'nli sonbool
: Mantiqiy qiymat (True/False)str
: Matn
List[int]
: Butun sonlardan iborat ro'yxatDict[str, int]
: Kalitlari matn, qiymatlari butun son bo'lgan lug'at
from typing import Union
def foo(x: Union[int, str]) -> None:
print(x)
O'zgaruvchi ma'lum bir turda yoki None bo'lishi mumkinligini belgilash uchun Optional dan foydalaniladi.
from typing import Optional
def foo(x: Optional[int]) -> None:
print(x)
Aniq uzunlikdagi va ma'lum turlardagi elementlardan iborat tuple belgilash uchun Tuple dan foydalaniladi.
from typing import Tuple
def foo(x: Tuple[int, float, str]) -> None:
print(x)
from typing import Any
def foo(x: Any) -> None:
print(x)
from typing import List
Vector = List[float]
def foo(v: Vector) -> None:
print(v)
from typing import List, Dict, Union, Optional
def process_data(data: List[Dict[str, Union[int, float]]]) -> Optional[float]:
if not data:
return None
return sum(item['value'] for item in data) / len(data)
typing moduli Python kodlaringizni tushunarliroq, qulayroq va xatolarni kamaytirish uchun juda foydali. Bu modul bilan yozilgan kodlar kelajakdagi o'zgarishlar uchun yanada aniqroq tuzilmani ta'minlaydi.
Unpacking va packing - Python dasturlash tilida juda foydali texnikalar bo‘lib, ular ob’ektlarni ochish va yig‘ish jarayonlarini osonlashtiradi.
Unpacking ob’ektni bir necha qismlarga ajratish va har bir qismni alohida o‘zgaruvchiga tayinlash imkonini beradi. Bu ko‘pincha ro‘yxatlar va tuple (to‘plam)lar bilan ishlatiladi.
my_tuple = (1, 2, 3)
a, b, c = my_tuple
print(a) # 1
print(b) # 2
print(c) # 3
my_list = [4, 5, 6]
x, y, z = my_list
print(x) # 4
print(y) # 5
print(z) # 6
def get_coordinates():
return (50.4501, 30.5234)
lat, lon = get_coordinates()
print(f"Latitude: {lat}, Longitude: {lon}")
Packing esa aksincha, bir nechta qiymatlarni bitta tuple yoki ro‘yxatga yig‘ish imkonini beradi.
a, b, c = 1, 2, 3
my_tuple = (a, b, c)
print(my_tuple) # (1, 2, 3)
x, y, z = 4, 5, 6
my_list = [x, y, z]
print(my_list) # [4, 5, 6]
def create_point(x, y, z):
return (x, y, z)
point = create_point(7, 8, 9)
print(point) # (7, 8, 9)
def my_function(*args, **kwargs):
print(args)
print(kwargs)
my_function(1, 2, 3, name="Alice", age=30)
# (1, 2, 3)
# {'name': 'Alice', 'age': 30}
def calculate_sum(a, b, c):
return a + b + c
my_numbers = [1, 2, 3]
result = calculate_sum(*my_numbers)
print(result) # 6
# **kwargs bilan unpacking # noqa
def print_info(name, age):
print(f"Name: {name}, Age: {age}")
info = {"name": "Alice", "age": 30}
print_info(**info)
# Name: Alice, Age: 30
Python'da hashable va unhashable tushunchalari muhim ahamiyatga ega, ayniqsa, ma'lumotlar tuzilmalari va lug'atlar bilan ishlashda.
- Hashable obyektlar - bu o'zlarining hash() qiymatiga ega bo'lgan obyektlar. Bu qiymat doimiy bo'lib, obyektning umri davomida o'zgarmaydi. Hashable obyektlarni lug'at (dictionary) kalitlari yoki set elementlari sifatida ishlatish mumkin.
- Integer (int)
- Float (float)
- String (str)
- Tuple (tuple), agar uning ichidagi barcha elementlar ham hashable bo'lsa
- Frozen set (frozenset) Misol:
hash(42) # Integer hashable
hash("hello") # String hashable
hash((1, 2, 3)) # Tuple hashable
- Unhashable obyektlar - bu o'zlarining hash() qiymatiga ega bo'lmagan obyektlar. Bunday obyektlarni lug'at kalitlari yoki set elementlari sifatida ishlatib bo'lmaydi, chunki ular mutable (o'zgartirilishi mumkin) va ularning qiymatlari o'zgarishi mumkin.
- List (list)
- Dictionary (dict)
- Set (set)
- Tuple (tuple), agar uning ichidagi biror element unhashable bo'lsa Misol:
hash([1, 2, 3]) # List unhashable
hash({1: "one", 2: "two"}) # Dictionary unhashable
hash({1, 2, 3}) # Set unhashable
Hashable obyektlar ma'lumotlar tuzilmalarida, ayniqsa lug'atlar va setlarda, samarali indekslash va izlashni ta'minlaydi. Unhashable obyektlar esa o'zgaruvchan bo'lgani uchun ularni kalit yoki element sifatida ishlatish xavfli, chunki bu obyektlar o'zgarganda hash qiymati ham o'zgaradi va noto'g'ri xatti-harakatlar yuzaga kelishi mumkin.
- Yaxshi dasturlash amaliyotlaridan biri sifatida, kalit yoki set elementi sifatida hashable obyektlarni tanlash kerak, bu esa kodingizni ishonchli va samarali qiladi.
Django'da select_related va prefetch_related optimizatsiya metodlari bo'lib, ular ORM (Object-Relational Mapping) so'rovlarining samaradorligini oshirish uchun ishlatiladi.
- Ular har xil turdagi ma'lumotlar bazasi so'rovlarini oldindan yuklab olish orqali so'rovlar sonini kamaytiradi, bu esa bajarilish vaqtini tezlashtiradi.
Nimani ishlatadi: select_related odatda ForeignKey yoki OneToOneField maydonlar bilan ishlatiladi. Qanday ishlaydi: Bu metod bog'langan modelni JOIN operatori yordamida bitta so'rovda oladi. Bu esa bog'liq bo'lgan ma'lumotlarni olish uchun kerak bo'ladigan alohida so'rovlar sonini kamaytiradi. Qachon ishlatish kerak: Agar sizda ForeignKey yoki OneToOneField bilan bog'langan ma'lumotlar mavjud bo'lsa va siz ularga tez-tez murojaat qilmoqchi bo'lsangiz, select_relatedni ishlatishingiz kerak. Misol:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
# Query with select_related
books = Book.objects.select_related('author').all()
for book in books:
print(book.author.name) # Author ma'lumotlari bitta JOIN so'rovda keladi
Nimani ishlatadi: prefetch_related ManyToManyField yoki ForeignKey maydonlar uchun ishlatiladi, lekin u select_relateddan farqli o'laroq, so'rovlarni ikkita alohida so'rov sifatida yuboradi, keyin esa natijalarni Python tarafida bog'laydi. Qanday ishlaydi: U bog'langan ob'ektlarni alohida so'rovda yuklab oladi va asosiy so'rov bilan natijalarni bog'laydi. Bu katta hajmdagi bog'langan ob'ektlar mavjud bo'lgan holatlarda yaxshi ishlaydi. Qachon ishlatish kerak: Agar sizda ManyToMany yoki katta hajmdagi ForeignKey bog'lanishlar mavjud bo'lsa, prefetch_relatedni ishlatish samarali bo'ladi. Misol:
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
#Query with prefetch_related
books = Book.objects.prefetch_related('authors').all()
for book in books:
for author in book.authors.all():
print(author.name) # Authors alohida so'rovda yuklab olinadi
select_related
bir-to-bir yoki bir-to-ko'p bog'lanishlar uchun optimal va bitta so'rovda ma'lumotlarni olib keladi.prefetch_related
ko'p-to-ko'p bog'lanishlar yoki katta hajmdagi bog'langan ob'ektlar uchun ishlatiladi va so'rovlar sonini kamaytiradi, lekin ularni alohida-alohida bajaradi.
import queue
import threading
import time
# Telegram botiga yuborilayotgan buyruqlar navbati (queue)
command_queue = queue.Queue()
# Bu funksiya botga kelayotgan buyruqlarni navbatga qo'shadi
def add_command_to_queue(command):
print(f"Buyruq qo'shildi: {command}")
command_queue.put(command)
# Bu funksiya navbatdagi buyruqlarni ketma-ket bajaradi
def process_commands():
while True:
# Navbatdan buyruqni olish
command = command_queue.get()
if command is None:
break
print(f"Buyruqni bajarish: {command}")
time.sleep(2) # Buyruqni bajarishga sarflanadigan vaqt
command_queue.task_done()
print(f"Buyruq bajarildi: {command}")
# Buyruqlarni qabul qilish uchun yangi threading
threading.Thread(target=process_commands, daemon=True).start()
# Botga kelayotgan buyruqlar
add_command_to_queue("/start")
add_command_to_queue("/help")
add_command_to_queue("/settings")
# Buyruqlar navbatda bajarilguncha kutish
command_queue.join()
print("Barcha buyruqlar bajarildi!")```