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
آموزش diff دو متن با difflib #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import difflib
a = 'hello\nworld\n'
b = 'hello\nWORLD\n'
print(''.join(difflib.unified_diff(a.splitlines(True), b.splitlines(True))))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش Top-N با heapq #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import heapq
xs = [5, 1, 9, 2, 7]
print(heapq.nlargest(3, xs))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش binary search با bisect #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import bisect
xs = [1, 3, 5, 7]
print(bisect.bisect_left(xs, 6))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش Counter: پرتکرارترینها #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
from collections import Counter
print(Counter('aabccc').most_common(2))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش deque برای sliding window #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
from collections import deque
d = deque(maxlen=3)
for x in [1, 2, 3, 4]:
d.append(x)
print(list(d))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: combinations #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
print(list(itertools.combinations([1, 2, 3, 4], 2)))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: product #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
print(list(itertools.product([1, 2], ['a', 'b'])))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: groupby پشتسرهم #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
s = 'aaabcc'
print([(k, len(list(g))) for k, g in itertools.groupby(s)])
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش argparse مینیمال #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import argparse
p = argparse.ArgumentParser()
p.add_argument('--n', type=int, default=3)
args = p.parse_args([])
print(args.n)
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش logging با فرمت #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logging.info('ready')
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش traceback تمیز #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import traceback
try:
1 / 0
except ZeroDivisionError:
print(traceback.format_exc().splitlines()[-1])
pythonhost.ir | پایتون هاست
@pythonhost_ir