Фишки Python
29 subscribers
2 photos
3 files
29 links
Всё что ты хотел узнать, но ленился спросить.

Декораторы, итераторы и контракты.
Download Telegram
API maker - hug

hug is a library that provides an extremely simple way to create
Internet APIs for web services.
import hug
import webcolors

@hug.get()
def hextoname(hex: hug.types.text):
return webcolors.hex_to_name('#' + hex)

@hug.get()
def nametohex(name: hug.types.text):
return webcolors.name_to_hex(name)
hug -f hugserve.py
$ curl http://localhost:8000/hextoname?hex=ff0000
"red"

$ curl http://localhost:8000/nametohex?name=lightskyblue
"#87cefa"
Date times
arrow - работа со временем без оглядки на timezones
import arrow

t0 = arrow.now()
print(t0)

t1 = arrow.utcnow()
print(t1)

difference = (t0 - t1).total_seconds()
print('Total difference: %.2f seconds' % difference)
2016-06-26T18:43:55.328561+10:00
2016-06-26T08:43:55.328630+00:00
Total difference: -0.00 seconds
Время в человеческом виде

>>> t0 = arrow.now()
>>> t0.humanize()
'just now'

>>> t0.humanize()
'seconds ago'

>>> t0 = t0.replace(hours=-3,minutes=10)
>>> t0.humanize()
'2 hours ago'
>>> t0.humanize(locale='ru')
'2 часа назад'
Парсинг даты из кучи вариантов - parsedatetime
import parsedatetime as pdt

cal = pdt.Calendar()

examples = [
"2016-07-16",
"2016/07/16",
"2016-7-16",
"2016/7/16",
"07-16-2016",
"7-16-2016",
"7-16-16",
"7/16/16",
"19 November 1975",
"19 November 75",
"19 Nov 75",
"tomorrow",
"yesterday",
"10 minutes from now",
"the first of January, 2001",
"3 days ago",
"in four days' time",
"two weeks from now",
"three months ago",
"2 weeks and 3 days in the future",
]

print('{:30s}{:>30s}'.format('Input', 'Result'))
print('=' * 60)

for e in examples:
dt, result = cal.parseDT(e)
print('{:<30s}{:>30}'.format('"' + e + '"', dt.ctime()))
Input Result
============================================
"2016-07-16" Sat Jul 16 16:25:20 2016
"2016/07/16" Sat Jul 16 16:25:20 2016
"2016-7-16" Sat Jul 16 16:25:20 2016
"2016/7/16" Sat Jul 16 16:25:20 2016
"07-16-2016" Sat Jul 16 16:25:20 2016
"7-16-2016" Sat Jul 16 16:25:20 2016
"7-16-16" Sat Jul 16 16:25:20 2016
"7/16/16" Sat Jul 16 16:25:20 2016
"19 November 1975" Wed Nov 19 08:41:38 1975
"19 November 75" Wed Nov 19 08:41:38 1975
"19 Nov 75" Wed Nov 19 08:41:38 1975
"tomorrow" Tue Jun 21 09:00:00 2016
"yesterday" Sun Jun 19 09:00:00 2016
"10 minutes from now" Mon Jun 20 08:51:38 2016
"the first of January, 2001" Mon Jan 1 08:41:38 2001
"3 days ago" Fri Jun 17 08:41:38 2016
"in four days' time" Fri Jun 24 08:41:38 2016
"two weeks from now" Mon Jul 4 08:41:38 2016
"three months ago" Sun Mar 20 08:41:38 2016
"2 weeks and 3 days in the future" Thu Jul 7 08:41:38 2016
Forwarded from Python Textbooks (Igor A. Kamyshev)
Data Visualization with Python and JS – Kyran Dale (en) 2016

Рассмотрен стек визуализации данных: Python и JS, библиотеки Pandas и D3.

Книга предоставлена @frontend_textbooks.

#middle
Scrapy - парсер всего и вся в Web
Pandas - обработка и анализ данных
Matplotlib - визуализация данных на Python 2D/3D
Seaborn - визуализация данных поверх matplotlib, красивые графики
Crossfilter - js data-processing libraries, быстрая фильтрация строк/столбцов в датасете