This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Iterable, Any | |
def first(predicate: Callable, collection: Iterable) -> Any: | |
for x in collection: | |
if predicate(x): | |
return x | |
def first(predicate: Callable, collection: Iterable) -> Any: | |
return next((x for x in collection if predicate(x)), None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterator, Any, Iterable, TypeVar, Tuple | |
T_ = TypeVar('T_') | |
Pairs_Iter = Iterator[Tuple[T_, T_]] | |
def legs(iterable: Iterator[T_]) -> Pairs_Iter: | |
begin = next(iterable) | |
for end in iterable: | |
yield begin, end | |
begin = end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transform(data): | |
return [list(x) for x in zip(*data)] | |
data = [[*range(3)], [*range(3, 6)], [*range(6, 9)]] | |
# [[0, 1, 2], [3, 4, 5], [6, 7, 8]] | |
data_t = transform(data) | |
# [[0, 3, 6], [1, 4, 7], [2, 5, 8]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def couple(iterable, *, first=None): | |
"""Yield each iterable item along with the item before it.""" | |
prev = first | |
for item in iterable: | |
yield prev, item | |
prev = item | |
# >>> list(couple(range(5))) | |
# >>> [(None, 0), (0, 1), (1, 2), (2, 3), (3, 4)] | |
# >>> list(couple(range(5), first=10)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import time | |
@contextmanager | |
def timer(): | |
start = time.time() | |
yield | |
print(f'\N{greek capital letter delta}t \N{rightwards arrow} {time.time() - start:.2f}s') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
import aiopg | |
import asyncio | |
CONN_INFO = { | |
'host': 'POSTGRESQL_SERVER', | |
'user': 'user_name', | |
'port': 1234, | |
'database': 'some_dabase', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
from functools import reduce | |
from itertools import chain, repeat | |
from typing import List | |
def calc(times: List[str]) -> timedelta: | |
'''Calculates total time from the list of times''' | |
result = timedelta() | |
for t in times: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas_datareader as pdr | |
import pandas as pd | |
import datetime | |
def get(tickers, start_date, end_date=None): | |
def data(ticker): | |
return pdr.get_data_yahoo(ticker, start=start_date, end=end_date) | |
datas = map(data, tickers) | |
return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date'])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas_datareader as pdr | |
import datetime | |
aapl = pdr.get_data_yahoo('AAPL', | |
start=datetime.datetime(2007, 1, 1), | |
end=datetime.datetime(2018, 1, 1)) | |
# resample 'aapl' to business months, take last observation | |
monthly = aapl.resample('BM').apply(lambda x: x[-1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas_datareader as web | |
uah = web.DataReader('UAH=X', 'yahoo', start='2017-01-01') |