Skip to content

Python-ga chuqurroq sho'ng'ing va kodlash mahoratingizni oshiring.📡

Notifications You must be signed in to change notification settings

themusharraf/Python-Advanced

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced Python

Python-ga chuqurroq sho'ng'ing va kodlash mahoratingizni oshiring.

Python generatori:

  • 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)

Decorator

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())
image

Decorator funksiya argument sifatida funksiya qabul qiladi.

   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 Mangers

'''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
'''

Typing modul

Asosiy turlar

  • int: Butun son
  • float: O'nli son
  • bool: Mantiqiy qiymat (True/False)
  • str: Matn

Ro'yxatlar va Lug'atlar

  • List[int]: Butun sonlardan iborat ro'yxat
  • Dict[str, int]: Kalitlari matn, qiymatlari butun son bo'lgan lug'at

Union

O'zgaruvchining bir nechta turda bo'lishi mumkinligini belgilash uchun Union dan foydalaniladi.

from typing import Union


def foo(x: Union[int, str]) -> None:
    print(x)

Optional

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)

Tuple

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)

Any

Har qanday turdagi bo'lishi mumkin bo'lgan o'zgaruvchi belgilash uchun Any dan foydalaniladi.

from typing import Any


def foo(x: Any) -> None:
    print(x)

Type Aliases

Murakkab turlarni o'qilishi osonroq qilish uchun tur ta'riflari ishlatilishi mumkin.

from typing import List

Vector = List[float]


def foo(v: Vector) -> None:
    print(v)

Misol

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)

Xulosa

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 and packing

Unpacking va packing - Python dasturlash tilida juda foydali texnikalar bo‘lib, ular ob’ektlarni ochish va yig‘ish jarayonlarini osonlashtiradi.

Unpacking

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.

Tuple bilan unpacking

my_tuple = (1, 2, 3)
a, b, c = my_tuple
print(a)  # 1
print(b)  # 2
print(c)  # 3

Ro'yxat bilan unpacking

my_list = [4, 5, 6]
x, y, z = my_list
print(x)  # 4
print(y)  # 5
print(z)  # 6

Funksiya chaqirilishi natijasini unpacking qilish

def get_coordinates():
    return (50.4501, 30.5234)


lat, lon = get_coordinates()
print(f"Latitude: {lat}, Longitude: {lon}")

Packing

Packing esa aksincha, bir nechta qiymatlarni bitta tuple yoki ro‘yxatga yig‘ish imkonini beradi.

Tuple bilan packing

a, b, c = 1, 2, 3
my_tuple = (a, b, c)
print(my_tuple)  # (1, 2, 3)

Ro'yxat bilan packing

x, y, z = 4, 5, 6
my_list = [x, y, z]
print(my_list)  # [4, 5, 6]

Funksiya orqali packing

def create_point(x, y, z):
    return (x, y, z)


point = create_point(7, 8, 9)
print(point)  # (7, 8, 9)

*args va **kwargs bilan ishlash

Unpacking va packing ko‘pincha *args va **kwargs bilan birgalikda ishlatiladi.

- *args funksiyaga kiritilgan argumentlarni tuple ko‘rinishida qabul qiladi,

- **kwargs esa kalit-qiymat juftliklarini dictionary ko‘rinishida qabul qiladi.

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}

Unpacking va packingni birgalikda ishlatish

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 (Hashlanadigan)

  • 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.

Hashable obyektlar:

  1. Integer (int)
  2. Float (float)
  3. String (str)
  4. Tuple (tuple), agar uning ichidagi barcha elementlar ham hashable bo'lsa
  5. Frozen set (frozenset) Misol:
hash(42)  # Integer hashable
hash("hello")  # String hashable
hash((1, 2, 3))  # Tuple hashable

Unhashable (Hashlanmaydigan)

  • 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.

Unhashable obyektlar:

  1. List (list)
  2. Dictionary (dict)
  3. Set (set)
  4. 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

Nima uchun bu muhim?

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.

select_related

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:

Model example

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

prefetch_related

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:

Model example

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

Xulosa

  • 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.

Python Queue Example

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!")```

About

Python-ga chuqurroq sho'ng'ing va kodlash mahoratingizni oshiring.📡

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages