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

PythonHost.ir
Download Telegram
لیست کردن فایل‌ها با glob #پایتون

import glob
print(glob.glob('*.py')[:10])


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
فیلتر نام فایل‌ها با fnmatch #پایتون

import fnmatch
names=['a.py','b.txt','c.py']
print([n for n in names if fnmatch.fnmatch(n,'*.py')])


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
❤1
ساخت پوشه اگر نبود #پایتون

from pathlib import Path
Path('data').mkdir(exist_ok=True)
print('ready')


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
👍1
حذف پوشه کامل با shutil #پایتون

import shutil
shutil.rmtree('data', ignore_errors=True)
print('deleted')


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
👍1
کپی فایل با shutil #پایتون

import shutil
shutil.copy2('out.txt','out_copy.txt')
print('copied')


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
کپی پوشه با 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
‌