Introducing pywikibot

import pywikibot
site = pywikibot.Site('test','wikipedia')
page = pywikibot.Page(site,'user_talk:OrenBochman')
page.exists()
True
page.text =  page.text + '\nThis is something'
page.save()
Sleeping for 5.0 seconds, 2016-11-27 17:56:48
Page [[test:User talk:OrenBochman]] saved

Using UI to capture parameters

from ipywidgets import *
#from ipywidgets import interact_manual
from IPython.display import display
@interact_manual
def write_page(name='User_talk:OrenBochman',text=''):
    p=pywikipedia.Page(test,name)
    p.text='text'
    p.save()

Styling widgets

s = Button(description='(50% width, 80px height) button')
s.layout.width = '50%'
s.layout.height = '80px'
s
words = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed " +
         "do eiusmod tempor incididunt ut labore et dolore magna aliqua.").split(' ')
items = [Button(description=w) for w in words]

HBox([VBox([items[0], items[1]]), VBox([items[2], items[3]])])
IntSlider(description='$\int_0^t f$')
Label(value='$e=mc^2$')
def VBox(*pargs, **kwargs):
    """Displays multiple widgets vertically using the flexible box model."""
    box = Box(*pargs, **kwargs)
    box.layout.display = 'flex'
    box.layout.flex_flow = 'column'
    box.layout.align_items = 'stretch'
    return box

def HBox(*pargs, **kwargs):
    """Displays multiple widgets horizontally using the flexible box model."""
    box = Box(*pargs, **kwargs)
    box.layout.display = 'flex'
    box.layout.align_items = 'stretch'
    return box
items_layout = Layout(flex='1 1 auto',
                      width='auto')     # override the default width of the button to 'auto' to let the button grow

box_layout = Layout(display='flex',
                    flex_flow='column', 
                    align_items='stretch', 
                    border='solid',
                    width='50%')

items = [Button(description=w, layout=items_layout, button_style='danger') for w in words[:4]]
box = Box(children=items, layout=box_layout)
box
items_layout = Layout(flex='1 1 auto', width='auto')     # override the default width of the button to 'auto' to let the button grow

items = [
    Button(description='weight=1'),
    Button(description='weight=2', layout=Layout(flex='2 1 auto', width='auto')),
    Button(description='weight=1'),
 ]

box_layout = Layout(display='flex',
                    flex_flow='row', 
                    align_items='stretch', 
                    border='solid',
                    width='50%')
box = Box(children=items, layout=box_layout)
box
label_layout = Layout()

form_item_layout = Layout(
    display='flex',
    flex_flow='row',
    justify_content='space-between'
)

form_items = [
    #slider
    Box([Label(value='Age of the captain'), IntSlider(min=40, max=60)], layout=form_item_layout),
    #dropdown
    Box([Label(value='Egg style'), 
         Dropdown(options=['Scrambled', 'Sunny side up', 'Over easy'])], layout=form_item_layout),
    #text field
    Box([Label(value='Ship size'), 
         FloatText()], layout=form_item_layout),
    #text area
    Box([Label(value='Information'), 
         Textarea()], layout=form_item_layout),
    Box([Label(value='Progress'), 
         FloatProgress(
            value=7.5,
            min=5.0,
            max=10.0,
            step=0.1,
            description='Loading:')],layout=form_item_layout),
    Box([Label(value='bounded float'), 
        BoundedFloatText(
            value=7.5,
            min=5.0,
            max=10.0 )],layout=form_item_layout),
    Box([Label(value='Clicker'), 
        ToggleButton(
            description='Click me',
            value=False)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Valid(
             value=True)],layout=form_item_layout),
    Box([Label(value='Pizza topping'), 
         RadioButtons(
            options=['pepperoni', 'pineapple', 'anchovies'])],layout=form_item_layout),
    Box([Label(value='Project'), 
         Select(
            options=['Wikipedia','Wikisource', 'Wikidata','Wiktionary','Wikiquote','Wikiversity'])],layout=form_item_layout),
    Box([Label(value='Check'), 
        SelectionSlider(
            description='I like my eggs ...',
            options=['scrambled', 'sunny side up', 'poached', 'over easy'])],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    Box([Label(value='Check'), 
         Checkbox(
            description='Check me',
            value=True)],layout=form_item_layout),
    
]

form = Box(form_items, layout=Layout(
    display='flex',
    flex_flow='column',
    border='solid 2px',
    align_items='stretch',
    width='50%'
))
form
 
 
 

wikidata processing

site = pywikibot.Site("wikidata", "wikidata")
item = pywikibot.ItemPage(site, u"Q43049")
dictionary = item.get()
#print(dictionary)
print(dictionary.keys())
dict_keys(['aliases', 'descriptions', 'labels', 'claims', 'sitelinks'])
print(item)
item = pywikibot.ItemPage(site, u"Q43049")
item
dir(item)
item_dict=item.get()
item_dict.keys()
item_dict['labels']['it']
 

wikidata claims

item_dict['claims']
el = pywikibot.ItemPage(site,"Q43049")
el_dict = el.get()
el_dict.keys()
mylist =el_dict["claims"]["P47"]
for index, obj in enumerate(mylist):
    print("dict {} in list: \n{}".format(index,i.toJSON()))
claim_P1082 = el_dict["claims"]["P1082"][0]
claim_P1082
claim_P1082 = el_dict["claims"]["P1082"][0]

Convert to JSON

claim_P1082.toJSON()

Graphs

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()

Machine Learning

"""Softmax."""
scores = [3.0, 1.0, 0.2]
import numpy as np
def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    return np.exp(x)/np.sum(np.exp(x),axis=0)
print(softmax(scores))
# Plot softmax curves
x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])
plt.plot(x, softmax(scores).T, linewidth=2)
plt.show()
[ 0.8360188   0.11314284  0.05083836]
import matplotlib.pyplot as plt
from pylab import *
vals = [3,2,5,0,1]
plt.plot(vals)
plt.show()
#from math import *
x = np.linspace(0,3*pi, 500)
plt.plot(x,sin(x**2))
title("A simple chirp")
plt.show()

NameErrorTraceback (most recent call last)
<ipython-input-13-e7accbc3f1dd> in <module>()
      1 #from math import *
----> 2 x = np.linspace(0,3*pi, 500)
      3 plt.plot(x,sin(x**2))
      4 title("A simple chirp")
      5 plt.show()

NameError: name 'pi' is not defined
N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)
fig0, ax0 = plt.subplots()
strm = ax0.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
fig0.colorbar(strm.lines)
fig1, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1])
lw = 5*speed / speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
plt.show()