Skip to content

Instantly share code, notes, and snippets.

View traviskaufman's full-sized avatar

Travis Kaufman traviskaufman

View GitHub Profile
@traviskaufman
traviskaufman / 04-demystifying-rxjs-from.ts
Last active November 21, 2019 18:57
Demystifying RxJS, Part I: from()
function from<T>(convertible: Promise<T> | ArrayLike<T>): Observable<T> {
if (typeof (convertible as any).length === "number") {
const arrayLike = convertible as ArrayLike<T>;
return new Observable(obs => {
for (let i = 0; i < arrayLike.length; i++) {
obs.next(arrayLike[i]);
}
obs.complete();
});
}
@traviskaufman
traviskaufman / 03-demystifying-rxjs-of.ts
Created October 18, 2019 18:33
Demystifying RxJS, Part I: of()
function of<T>(...values: T[]): Observable<T> {
return new Observable(obs => {
for (const value of values) {
obs.next(value);
}
obs.complete();
});
}
// Logs:
@traviskaufman
traviskaufman / 02-demystifying-rxjs-observable-test.ts
Created October 18, 2019 18:00
Demystifying RxJS, Part I: Basic Observable Test
const observable = new Observable(subscriber => {
subscriber.next(1);
subscriber.next(2);
subscriber.next(3);
setTimeout(() => {
subscriber.next(4);
subscriber.complete();
}, 1000);
});
@traviskaufman
traviskaufman / 01-demystifying-rxjs-observable.ts
Last active October 18, 2019 17:56
Demystifying RxJS Part I: Observable Implementation
interface Observer<T> {
next(value: T): void;
error(err: any): void;
complete(): void;
}
interface Subscription {
unsubscribe(): void;
}
# ipython-utils/researchenv/__init__.py
# imports...
from researchenv.magics import ResearchEnvMagics
from google.cloud import bigquery
def load_ipython_extension(ipython: InteractiveShell):
# previous logic...
bq = bigquery.Client(project='example-project-id')
ipython.register_magics(ResearchEnvMagics(shell=ipython, bq=bq))
# ipython-utils/researchenv/magics.py
from IPython.core.magic import Magics, magics_class, cell_magic, needs_local_scope
from IPython.core.magic_arguments import magic_arguments, argument, parse_argstring
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.display import display
from google.cloud import bigquery
@magics_class
class ResearchEnvMagics(Magics):
%%bq --name hn_daily_comment_counts
SELECT
TIMESTAMP_TRUNC(time_ts, DAY) as day,
COUNT(0) as comment_count
FROM
`bigquery-public-data.hacker_news.comments`
GROUP BY
day
ORDER BY
day ASC;
# ipython-utils/setup.py
setup(
name='ipython-utils',
packages=find_packages(),
install_requires=[
'ipython >= 4',
'notebook >= 4.3.1',
'jupyter',
'numpy',
'pandas',
# ipython-utils/researchenv/__init__.py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.core.interactiveshell import InteractiveShell
def load_ipython_extension(ipython: InteractiveShell):
print('%matplotlib inline')
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns