import pywikibot
site = pywikibot.Site('test', 'wikipedia')
site
APISite("test", "wikipedia")
page = pywikibot.Page(site, 'test')
page.exists()
True
page.text
'Hello world'
page.text = 'Hello world'
page.save()
Page [[Test]] saved
import mwapi
session = mwapi.Session('https://en.wikipedia.org') #TODO add user agent
Sending requests with default User-Agent.  Set 'user_agent' on mwapi.Session to quiet this message.
print(session.get(action='query', meta='userinfo'))
{'query': {'userinfo': {'anon': '', 'name': 'Jtmorgan', 'id': 0}},
 'batchcomplete': ''}
{'batchcomplete': '', 'query': {'userinfo': {'id': 0, 'name': '172.16.1.161', 'anon': ''}}}
{'query': {'userinfo': {'anon': '', 'name': 'Jtmorgan', 'id': 0}},
 'batchcomplete': ''}
#you may have to install the library before using it... until Yuvi gets around to installing it by default
!pip install git+https://github.com/mediawiki-utilities/python-mwviews.git
Collecting git+https://github.com/mediawiki-utilities/python-mwviews.git
  Cloning https://github.com/mediawiki-utilities/python-mwviews.git to /tmp/pip-tofzjwb4-build
Requirement already satisfied: mwapi in /srv/paws/lib/python3.6/site-packages (from mwviews==0.1.0)
Requirement already satisfied: mwcli in /srv/paws/lib/python3.6/site-packages (from mwviews==0.1.0)
Requirement already satisfied: requests in /srv/paws/lib/python3.6/site-packages (from mwviews==0.1.0)
Requirement already satisfied: para in /srv/paws/lib/python3.6/site-packages (from mwcli->mwviews==0.1.0)
Requirement already satisfied: docopt in /srv/paws/lib/python3.6/site-packages (from mwcli->mwviews==0.1.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /srv/paws/lib/python3.6/site-packages (from requests->mwviews==0.1.0)
Requirement already satisfied: idna<2.9,>=2.5 in /srv/paws/lib/python3.6/site-packages (from requests->mwviews==0.1.0)
Requirement already satisfied: certifi>=2017.4.17 in /srv/paws/lib/python3.6/site-packages (from requests->mwviews==0.1.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /srv/paws/lib/python3.6/site-packages (from requests->mwviews==0.1.0)
Installing collected packages: mwviews
  Running setup.py install for mwviews ... done
Successfully installed mwviews-0.1.0
import mwviews
from mwviews.api import PageviewsClient
p = PageviewsClient()

p.article_views('en.wikipedia', ['Selfie', 'Cat', 'Dog'])
p.project_views(['ro.wikipedia', 'de.wikipedia', 'commons.wikimedia'])
p.top_articles('en.wikipedia', limit=10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-2e02614b46d0> in <module>
----> 1 p = PageviewsClient()
      2 
      3 p.article_views('en.wikipedia', ['Selfie', 'Cat', 'Dog'])
      4 p.project_views(['ro.wikipedia', 'de.wikipedia', 'commons.wikimedia'])
      5 p.top_articles('en.wikipedia', limit=10)

TypeError: __init__() missing 1 required positional argument: 'user_agent'
import wdqs
import requests
import csv
import encoding_fix
import json
from operator import itemgetter
import requests
import time

#load in the JSON file we created with download_building_permit_data.py
with open("data/residential_permits_2010-2016.json") as json_infile:
    bp_api_data = json.load(json_infile)

json_infile.close()
"""
Example query: https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&result_type=street_address
"""
#create an empty dictionary that will hold our per-neighborhood counts
permits_by_neighborhood = {}

#go through each permit record and get the neighborhood for each address.
for x in bp_api_data:
#     print(x)
    #we're only interested in MULTIFAMILY construction right now, not SINGLE FAMILY / DUPLEX
    if x['category'] == "MULTIFAMILY":
        # if a row doesn't contain lat/long, 
        # OR if Google Maps API can't find a neighborhood for a given lat/long, 
        # we're going to skip it
        try:
            #add the city and state to the end of each address before querying the Google Maps API
            gmaps_api_params = {'latlng' : x['latitude'] + "," + x["longitude"]}
            gmaps_api_req = requests.get("http://maps.googleapis.com/maps/api/geocode/json", params=gmaps_api_params)
            # save the content of the request to a Python dict and start parsing it
            gmaps_api_data = gmaps_api_req.json()
            neighborhood = gmaps_api_data['results'][0]['address_components'][2]['long_name']
            if neighborhood in permits_by_neighborhood:
                permits_by_neighborhood[neighborhood] += 1
            else:
                permits_by_neighborhood[neighborhood] = 1
        except:
            pass
            
        #slow down our API requests, to avoid getting blocked by Google    
        time.sleep(0.2)
    else:
        continue
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-19-921e36de1f67> in <module>
      1 import csv
----> 2 import encoding_fix
      3 import json
      4 from operator import itemgetter
      5 import requests

ModuleNotFoundError: No module named 'encoding_fix'
import os 
import pymysql
conn = pymysql.connect(
    host=os.environ['MYSQL_HOST'],
    user=os.environ['MYSQL_USERNAME'],
    password=os.environ['MYSQL_PASSWORD'],
    database='enwiki_p',
    charset='utf8'
)
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""SELECT * from user where user_name = 'Jtmorgan'""")
    jtmorgan = cur.fetchall()
    cur.execute("""SELECT * from user where user_name = 'HostBot'""")
    hostbot = cur.fetchall()
    print(jtmorgan)
    print(hostbot)
((7878375, b'Jtmorgan', b'', None, None, None, None, None, None, None, None, None, b'20080917143125', None, 5630, None),)
((16596082, b'HostBot', b'', None, None, None, None, None, None, None, None, None, b'20120404202435', None, 312291, None),)
username = jtmorgan[0][1]
#decode byte string to UTF to make it compatible with most other strings you'll be using
print(username.decode("utf8") + ' is great')
Jtmorgan is great
#you can access the CSCW workshop datasets, including the Teahouse dataset
conn2 = pymysql.connect(
    host=os.environ['MYSQL_HOST'],
    user=os.environ['MYSQL_USERNAME'],
    password=os.environ['MYSQL_PASSWORD'],
    database='datasets_p',
    charset='utf8' #doesn't actually do anything right now. still returns byte strings
)
with conn2.cursor() as cur:
    cur.execute('use datasets_p')
    cur.execute('show tables')
    dbs = cur.fetchall()
    print(dbs)
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-26-6f1cc64d9fc3> in <module>
      1 with conn2.cursor() as cur:
----> 2     cur.execute('use datasets_p')
      3     cur.execute('show tables')
      4     dbs = cur.fetchall()
      5     print(dbs)

/srv/paws/lib/python3.6/site-packages/pymysql/cursors.py in execute(self, query, args)
    168         query = self.mogrify(query, args)
    169 
--> 170         result = self._query(query)
    171         self._executed = query
    172         return result

/srv/paws/lib/python3.6/site-packages/pymysql/cursors.py in _query(self, q)
    326         self._last_executed = q
    327         self._clear_result()
--> 328         conn.query(q)
    329         self._do_get_result()
    330         return self.rowcount

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in query(self, sql, unbuffered)
    515                 sql = sql.encode(self.encoding, 'surrogateescape')
    516         self._execute_command(COMMAND.COM_QUERY, sql)
--> 517         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    518         return self._affected_rows
    519 

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in _read_query_result(self, unbuffered)
    730         else:
    731             result = MySQLResult(self)
--> 732             result.read()
    733         self._result = result
    734         if result.server_status is not None:

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in read(self)
   1073     def read(self):
   1074         try:
-> 1075             first_packet = self.connection._read_packet()
   1076 
   1077             if first_packet.is_ok_packet():

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in _read_packet(self, packet_type)
    682 
    683         packet = packet_type(buff, self.encoding)
--> 684         packet.check_error()
    685         return packet
    686 

/srv/paws/lib/python3.6/site-packages/pymysql/protocol.py in check_error(self)
    218             errno = self.read_uint16()
    219             if DEBUG: print("errno =", errno)
--> 220             err.raise_mysql_exception(self._data)
    221 
    222     def dump(self):

/srv/paws/lib/python3.6/site-packages/pymysql/err.py in raise_mysql_exception(data)
    107         errval = data[3:].decode('utf-8', 'replace')
    108     errorclass = error_map.get(errno, InternalError)
--> 109     raise errorclass(errno, errval)

OperationalError: (1044, "Access denied for user 's52771'@'%' to database 'datasets_p'")
#you can access hostbot tables this way, too
conn3 = pymysql.connect(
    host=os.environ['MYSQL_HOST'],
    user=os.environ['MYSQL_USERNAME'],
    password=os.environ['MYSQL_PASSWORD'],
    database='p50380g50728__hostbot',
    charset='utf8'
)

with conn3.cursor() as cur:
    cur.execute('use p50380g50728__hostbot')
    cur.execute('show tables')
    tables = cur.fetchall()
    print(tables)#won't work, user access denied
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-27-b5a97c70a0d8> in <module>
      9 
     10 with conn3.cursor() as cur:
---> 11     cur.execute('use p50380g50728__hostbot')
     12     cur.execute('show tables')
     13     tables = cur.fetchall()

/srv/paws/lib/python3.6/site-packages/pymysql/cursors.py in execute(self, query, args)
    168         query = self.mogrify(query, args)
    169 
--> 170         result = self._query(query)
    171         self._executed = query
    172         return result

/srv/paws/lib/python3.6/site-packages/pymysql/cursors.py in _query(self, q)
    326         self._last_executed = q
    327         self._clear_result()
--> 328         conn.query(q)
    329         self._do_get_result()
    330         return self.rowcount

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in query(self, sql, unbuffered)
    515                 sql = sql.encode(self.encoding, 'surrogateescape')
    516         self._execute_command(COMMAND.COM_QUERY, sql)
--> 517         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    518         return self._affected_rows
    519 

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in _read_query_result(self, unbuffered)
    730         else:
    731             result = MySQLResult(self)
--> 732             result.read()
    733         self._result = result
    734         if result.server_status is not None:

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in read(self)
   1073     def read(self):
   1074         try:
-> 1075             first_packet = self.connection._read_packet()
   1076 
   1077             if first_packet.is_ok_packet():

/srv/paws/lib/python3.6/site-packages/pymysql/connections.py in _read_packet(self, packet_type)
    682 
    683         packet = packet_type(buff, self.encoding)
--> 684         packet.check_error()
    685         return packet
    686 

/srv/paws/lib/python3.6/site-packages/pymysql/protocol.py in check_error(self)
    218             errno = self.read_uint16()
    219             if DEBUG: print("errno =", errno)
--> 220             err.raise_mysql_exception(self._data)
    221 
    222     def dump(self):

/srv/paws/lib/python3.6/site-packages/pymysql/err.py in raise_mysql_exception(data)
    107         errval = data[3:].decode('utf-8', 'replace')
    108     errorclass = error_map.get(errno, InternalError)
--> 109     raise errorclass(errno, errval)

OperationalError: (1044, "Access denied for user 's52771'@'%' to database 'p50380g50728__hostbot'")
NAMES_LIST = "namedata_test.txt"

boys = {}
girls = {}

for line in open(NAMES_LIST, 'r').readlines():
    print(line)
    name, gender, count = line.strip().split(",")
    count = int(count)

    if gender == "F":
        girls[name.lower()] = count
    elif gender == "M":
        boys[name.lower()] = count
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-28-b620e7833216> in <module>
      4 girls = {}
      5 
----> 6 for line in open(NAMES_LIST, 'r').readlines():
      7     print(line)
      8     name, gender, count = line.strip().split(",")

FileNotFoundError: [Errno 2] No such file or directory: 'namedata_test.txt'
import csv

responses = []
notifications_data = csv.DictReader(open("test_data.csv"), delimiter=',', quotechar='"')

for row in notifications_data:
#     print(type(row)) 
    responses.append(row)
print(responses)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-29-a10b4d5b46d6> in <module>
      2 
      3 responses = []
----> 4 notifications_data = csv.DictReader(open("test_data.csv"), delimiter=',', quotechar='"')
      5 
      6 for row in notifications_data:

FileNotFoundError: [Errno 2] No such file or directory: 'test_data.csv'
print 
<function print>
help
Type help() for interactive help, or help(object) for help about object.
ver
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-32-a1b04ca79c16> in <module>
----> 1 ver

NameError: name 'ver' is not defined
version
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-33-605b5d1778ad> in <module>
----> 1 version

NameError: name 'version' is not defined