Pull to refresh

Интересные встроенные библиотеки питона:

Tempfile

Создаёт временный файл как в linux папка tmp:

import tempfile
with tempfile.NamedTemporaryFile() as tmp:
    print(tmp.name)
    tmp.write(...)

IO

Чтобы не использовать временные файлы, можно использовать временный буфер сразу в программе:

from io import BytesIO

imagefile = BytesIO()
animage.save(imagefile, format='PNG')
imagedata = imagefile.getvalue()

Struct

Позволяет создавать бинарные структуры и парсить их. Может ускорить передачу между клиентом и сервером:

>> from struct import *
>> pack(">bhl", 1, 2, 3)
b'\x01\x00\x02\x00\x00\x00\x03'
>> unpack('>bhl', b'\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>> calcsize('>bhl')
7

CMD

С помощью этой библиотеки можно создать полноценный tui:

Welcome to the turtle shell.   Type help or ? to list commands.

(turtle) ?

Documented commands (type help <topic>):
========================================
bye     color    goto     home  playback  record  right
circle  forward  heading  left  position  reset   undo

(turtle) help forward
Move the turtle forward by the specified distance:  FORWARD 10
(turtle) record spiral.cmd
(turtle) position
Current position is 0 0

Удивительно что такие полезные пакеты находятся в стандартной комплектации питона.

И не нужно скачивать кучу пакетов из pip.

Total votes 18: ↑18 and ↓0+18
Comments0

Articles