Skip to content

Instantly share code, notes, and snippets.

View joseberlines's full-sized avatar
💭
Python

joseberlines joseberlines

💭
Python
  • European Patent Office
  • Berlin
View GitHub Profile
import numpy as np
import pandas as pd
from ipydatagrid import DataGrid,TextRenderer
import ipydatagrid as grid
print(grid.__version__)
df = pd.DataFrame(
data={
"Col1HasAVeryLongName": ['qwerwqerqweddg d dgggfgfg dg dg drwqerwqer','qwer wer qwerdg wqer','asdf asdfasdasdfsadf'],
"Col2MediumName": [4, 5, 6],
@joseberlines
joseberlines / newspapers.py
Created January 11, 2022 09:17
open tabs from voila
import ipyvuetify as vue
from ipywidgets import Output
from IPython.display import Javascript
b1 = vue.Btn(class_='mx-2 light-blue darken-1', color='primary',children=['open lemonde'])
b2 = vue.Btn(class_='mx-2 light-blue darken-1', color='primary',children=['open nyt'])
b3 = vue.Btn(class_='mx-2 light-blue darken-1', color='primary',children=['open all'])
out=Output()
def open_lemonde(widget, event, data):
@joseberlines
joseberlines / three.py
Last active November 12, 2021 13:33
three ways of performing lookup
# THE DATA
# L is a list of dicts (all have the same keys)
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('barcelona','Vigo')} for i in range(1000000)]
# option 1: generator
next(item for item in L if item["name"] == myvalue)
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('barcelona','Vigo')} for i in range(1000000)]
#%%timeit
next(item for item in L if item["name"] == "e300000")
# 22.3 ms ± 2.58 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
#%%timeit
# 100000 elements list
L=[{'name':'e'+str(i), 'explanation':'objection1','law':(),'CS':('..claim','..notclear')} for i in range(100000)]
next(item for item in L if item["name"] == "e3")
# 716 ns ± 62.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
[d for d in L if d['name']=='e3'][0]
# 6.66 ms ± 520 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
df = pd.DataFrame(L).set_index('name')
df.loc[['e4']].reset_index().to_dict('records')
#1.09 ms ± 13.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
df.loc[['e9']].reset_index().to_dict('records')
#1.1 ms ± 22.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
import pandas as pd
# list of 1000
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('Barcelona','Vigo')} for i in range(1000)]
#%%timeit
next(item for item in L if item["name"] == "e500")
# list of 1000 dictionaries
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('Barcelona','Vigo')} for i in range(1000)]
rail_lines = [{'id': 'line1', 'source': 'BER', 'target': 'MUN', 'speed': '200km/h'},
{'id': 'line2', 'source': 'MUN', 'target': 'FRA', 'speed': '200km/h'},
{'id': 'line3', 'source': 'FRA', 'target': 'BER', 'speed': '250km/h'},
{'id': 'line4', 'source': 'BER', 'target': 'HAM', 'speed': '300km/h'},
{'id': 'line5', 'source': 'BER', 'target': 'LEP', 'speed': '300km/h'},
{'id': 'line6', 'source': 'NUR', 'target': 'LEP', 'speed': '150km/h'},
{'id': 'line7', 'source': 'NUR', 'target': 'FRA', 'speed': '150km/h'},
{'id': 'line8', 'source': 'BER', 'target': 'PAR', 'speed': '400km/h'},
{'id': 'line9', 'source': 'PAR', 'target': 'LYO', 'speed': '400km/h'},
{'id': 'line10', 'source': 'LYO', 'target': 'BAR', 'speed': '400km/h'},
rails_df['speed_int']=rails_df['speed'].str.replace('km/h','').astype('int')
min_speed = rails_df['speed_int'].min()
max_speed = rails_df['speed_int'].max()
span_speed = max_speed-min_speed
rails_df['score'] = rails_df['speed_int'].apply(lambda x: (x-min_speed)/span_speed)
rails_df['width'] = rails_df['score'] *7 +5
G=transform_into_ipycytoscape(stations_df,rails_df)
display(G)