ساخت tar.gz #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import tarfile
with tarfile.open('a.tar.gz','w:gz') as t:
t.add('out.txt')
print('ok')
PythonHost.ir | پایتون هاست
@pythonhost_ir
پارس URL #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
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 #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from urllib.parse import urlencode
print(urlencode({'q':'python','page':2}))
PythonHost.ir | پایتون هاست
@pythonhost_ir
join امن URL #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from urllib.parse import urljoin
print(urljoin('https://site.com/api/','v1/users'))
PythonHost.ir | پایتون هاست
@pythonhost_ir
درخواست ساده HTTP با urllib #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from urllib.request import urlopen
print(urlopen('https://example.com').status)
PythonHost.ir | پایتون هاست
@pythonhost_ir
پارس JSON #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import json
data=json.loads('{"a":1,"b":2}')
print(data['a'])
PythonHost.ir | پایتون هاست
@pythonhost_ir
Pretty JSON #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import json
print(json.dumps({'a':1,'b':[1,2]}, ensure_ascii=False, indent=2))
PythonHost.ir | پایتون هاست
@pythonhost_ir
خواندن CSV با DictReader #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import csv, io
f=io.StringIO('a,b\n1,2\n')
print(next(csv.DictReader(f)))
PythonHost.ir | پایتون هاست
@pythonhost_ir
نوشتن CSV #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
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: ساخت جدول و درج #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
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 #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
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 #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import shelve
with shelve.open('kv.db') as db:
db['token']='abc'
print(db['token'])
PythonHost.ir | پایتون هاست
@pythonhost_ir
pickle: سریالسازی #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
import pickle
b=pickle.dumps({'a':1})
print(pickle.loads(b))
PythonHost.ir | پایتون هاست
@pythonhost_ir
Decimal برای دقت مالی #پایتون
PythonHost.ir | پایتون هاست
@pythonhost_ir
from decimal import Decimal
print(Decimal('0.1')+Decimal('0.2'))
PythonHost.ir | پایتون هاست
@pythonhost_ir
آموزش pickle برای داده قابلاعتماد محلی #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
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: استخراج ساده الگوهای شبیه ایمیل #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
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 چند فاصله #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import re
s = 'a b c'
print(re.sub('\\s+', ' ', s).strip())
pythonhost.ir | پایتون هاست
@pythonhost_ir
نرمالسازی یونیکد NFKC #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import unicodedata
print(unicodedata.normalize('NFKC', 'ABC'))
pythonhost.ir | پایتون هاست
@pythonhost_ir
چیدن متن در عرض مشخص (wrap) #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import textwrap
print(textwrap.fill('این یک متن خیلی طولانی است', width=10))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش escape HTML #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import html
print(html.escape('<a href="x">hi</a>'))
pythonhost.ir | پایتون هاست
@pythonhost_ir