PythonHost.ir | پایتون هاست
60 subscribers
51 photos
51 links
پایتون هاست | اولین و تنها میزبان تخصصی پروژه ها و ربات های پایتونی کشور

PythonHost.ir
Download Telegram
کپی پوشه با shutil #پایتون

import shutil
shutil.copytree('src','dst', dirs_exist_ok=True)
print('done')


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
فایل موقت (TemporaryDirectory) #پایتون

import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'a.txt'
p.write_text('hi')
print(p.read_text())


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
هش متن با SHA256 #پایتون

import hashlib
h=hashlib.sha256('hi'.encode()).hexdigest()
print(h)


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
هش فایل با SHA256 #پایتون

import hashlib
from pathlib import Path
p=Path('out.txt')
print(hashlib.sha256(p.read_bytes()).hexdigest())


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
مقایسه امن رشته‌ها با hmac #پایتون

import hmac
print(hmac.compare_digest('a'*10, 'a'*10))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
ساخت UUID #پایتون

import uuid
print(uuid.uuid4())


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
Base64 encode/decode #پایتون

import base64
b=base64.b64encode('سلام'.encode())
print(b.decode())
print(base64.b64decode(b).decode())


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
فشرده‌سازی سریع با gzip #پایتون

import gzip
data=b'hello'*200
z=gzip.compress(data)
print(len(data),'->',len(z))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
ساخت zip داخل حافظه #پایتون

import io, zipfile
buf=io.BytesIO()
with zipfile.ZipFile(buf,'w') as z:
z.writestr('a.txt','hello')
print('bytes:', len(buf.getvalue()))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
ساخت tar.gz #پایتون

import tarfile
with tarfile.open('a.tar.gz','w:gz') as t:
t.add('out.txt')
print('ok')


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
پار‌س URL #پایتون

from urllib.parse import urlparse
u=urlparse('https://example.com/a?x=1')
print(u.netloc, u.path, u.query)


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
ساخت Query String #پایتون

from urllib.parse import urlencode
print(urlencode({'q':'python','page':2}))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
join امن URL #پایتون

from urllib.parse import urljoin
print(urljoin('https://site.com/api/','v1/users'))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
درخواست ساده HTTP با urllib #پایتون

from urllib.request import urlopen
print(urlopen('https://example.com').status)


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
پارس JSON #پایتون

import json
data=json.loads('{"a":1,"b":2}')
print(data['a'])


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
Pretty JSON #پایتون

import json
print(json.dumps({'a':1,'b':[1,2]}, ensure_ascii=False, indent=2))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
خواندن CSV با DictReader #پایتون

import csv, io
f=io.StringIO('a,b\n1,2\n')
print(next(csv.DictReader(f)))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
نوشتن CSV #پایتون

import csv, io
buf=io.StringIO()
w=csv.writer(buf)
w.writerow(['name','age'])
w.writerow(['Ali',20])
print(buf.getvalue())


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
❤1
SQLite: ساخت جدول و درج #پایتون

import sqlite3
con=sqlite3.connect(':memory:')
con.execute('create table t(x int)')
con.execute('insert into t values (1)')
print(con.execute('select x from t').fetchone()[0])


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
SQLite: Row به شکل dict #پایتون

import sqlite3
con=sqlite3.connect(':memory:')
con.row_factory=sqlite3.Row
row=con.execute('select 1 as a, 2 as b').fetchone()
print(dict(row))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌