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

PythonHost.ir
Download Telegram
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‌
‌
چیدن متن در عرض مشخص (wrap) #پایتون

import textwrap
print(textwrap.fill('این یک متن خیلی طولانی است', width=10))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش escape HTML #پایتون

import html
print(html.escape('<a href="x">hi</a>'))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش diff دو متن با difflib #پایتون

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 #پایتون

import heapq
xs = [5, 1, 9, 2, 7]
print(heapq.nlargest(3, xs))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش binary search با bisect #پایتون

import bisect
xs = [1, 3, 5, 7]
print(bisect.bisect_left(xs, 6))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش Counter: پرتکرارترین‌ها #پایتون

from collections import Counter
print(Counter('aabccc').most_common(2))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش deque برای sliding window #پایتون

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 #پایتون

import itertools
print(list(itertools.combinations([1, 2, 3, 4], 2)))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش itertools: product #پایتون

import itertools
print(list(itertools.product([1, 2], ['a', 'b'])))


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش itertools: groupby پشت‌سرهم #پایتون

import itertools
s = 'aaabcc'
print([(k, len(list(g))) for k, g in itertools.groupby(s)])


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش argparse مینیمال #پایتون

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 با فرمت #پایتون

import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logging.info('ready')


pythonhost.ir | پایتون هاست
@pythonhost_ir‌
‌
آموزش traceback تمیز #پایتون

import traceback
try:
1 / 0
except ZeroDivisionError:
print(traceback.format_exc().splitlines()[-1])


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