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

PythonHost.ir
Download Telegram
فشرده‌سازی سریع با 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
‌
shelve: ذخیره key/value #پایتون

import shelve
with shelve.open('kv.db') as db:
db['token']='abc'
print(db['token'])


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
pickle: سریال‌سازی #پایتون

import pickle
b=pickle.dumps({'a':1})
print(pickle.loads(b))


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

from decimal import Decimal
print(Decimal('0.1')+Decimal('0.2'))


PythonHost.ir | پایتون هاست
@pythonhost_ir
‌
آموزش pickle برای داده قابل‌اعتماد محلی #پایتون

import pickle

payload = {"a": 1, "items": [1, 2, 3]}
blob = pickle.dumps(payload, protocol=pickle.HIGHEST_PROTOCOL)
print(pickle.loads(blob))

# هشدار: pickle داده‌ی ناشناس یا اینترنتی را load نکنید.


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش Regex: استخراج ساده الگوهای شبیه ایمیل #پایتون

import re

text = "a@x.com test b@y.org"
pattern = r"[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}"
print(re.findall(pattern, text))

# برای اعتبارسنجی واقعی ایمیل، Regex ساده کافی نیست.


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش Regex: replace چند فاصله #پایتون

import re
s = 'a b c'
print(re.sub('\\s+', ' ', s).strip())


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
نرمال‌سازی یونیکد NFKC #پایتون

import unicodedata
print(unicodedata.normalize('NFKC', 'ABC'))


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