<commit_before>import requests, iso8601, os, json, logging

from flask import Flask, jsonify, json, request, redirect, abort, make_response
from flask import render_template, flash
from libsaas.services import mixpanel

from totalimpactwebapp import app, util
from totalimpactwebapp.models import Github
from totalimpactwebapp import pretty_date

logger = logging.getLogger("tiwebapp.views")
mymixpanel = mixpanel.Mixpanel(token=os.getenv("MIXPANEL_TOKEN"))
logger.debug("ok, just checking that this refreshed.")

@app.before_request
def log_ip_address():
    if request.endpoint != "static":
        ip_address = request.remote_addr
        logger.info("%30s IP address calling %s %s" % (ip_address, request.method, request.url))

# static pages
@app.route('/')
def home():
    return render_template(
    'index.html',
    page_title="tell the full story of your research impact",
    body_class="homepage",
    mixpanel_token=os.environ["MIXPANEL_TOKEN"],
    api_root=os.environ["API_ROOT"]
    )

@app.route("/embed/impactstory.js")
@app.route("/embed/v1/impactstory.js")
def impactstory_dot_js():

    badges_template = render_template("js-template-badges.html").replace("\n", "")
    ich_def = open(os.path.dirname(__file__) + "/static/js/icanhaz.min.js", "r").read()
    ti_item_def = open(os.path.dirname(__file__) + "/static/js/ti-item.js", "r").read()

    rendered = render_template(
        "impactstory.js",
        ich=ich_def,
        ti_item=ti_item_def,
        badges_template=badges_template,
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        webapp_root=os.environ["WEBAPP_ROOT"]
    )
    resp = make_response(rendered)
    """
    There is no standard way to indicate you're sending back javascript;
    This seems the most recommended one, though. See
    http://stackoverflow.com/questions/2706290/why-write-script-type-text-javascript-when-the-mime-type-is-set-by-the-serve
    and http://www.ietf.org/rfc/rfc4329.txt
     """
    resp.headers["Content-Type"] = "application/javascript; charset=utf-8"
    return resp

@app.route("/embed/test")
def embed_test():
    return render_template(
        "sample-embed-internal-test.html",
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        webapp_root = os.environ["WEBAPP_ROOT"]
    )

@app.route('/about')
def about():
    return render_template(
        'about.html',
        page_title="about",
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"]
        )

@app.route('/faq')
def faq():
    # get the table of items and identifiers
    which_items_loc = os.path.join(
        os.path.dirname(__file__),
        "static",
        "whichartifacts.html"
        )
    which_item_types = open(which_items_loc).read()

    # get the static_meta info for each metric
    try:
        r = requests.get("http://" + os.environ["API_ROOT"] +'/provider')
        metadata = json.loads(r.text)
    except requests.ConnectionError:
        metadata = {}

    return render_template(
        'faq.html',
        page_title="faq",
        which_artifacts=which_item_types,
        provider_metadata=metadata,
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"]
        )

@app.route('/api-docs')
def apidocs():
    return render_template(
        'api-docs.html',
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        webapp_root = os.environ["WEBAPP_ROOT"],
        page_title="api & embed code"
        )

@app.route('/create')
def collection_create():
    return render_template(
        'create-collection.html',
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        page_title="create collection",
        body_class="create-collection"
        )

@app.route('/collection/<collection_id>')
def collection_report(collection_id):
    url = "http://{api_root}/collection/{collection_id}?include_items=0".format(
        api_root=os.getenv("API_ROOT"),
        collection_id=collection_id
    )

    r = requests.get(url)
    if r.status_code == 200:
        collection = json.loads(r.text)
        return render_template(
            'report.html',
            mixpanel_token=os.environ["MIXPANEL_TOKEN"],
            api_root=os.environ["API_ROOT"],
            api_key=os.environ["API_KEY"],
            request_url=request.url,
            page_title=collection["title"],
            body_class="report",
            report_id=collection["_id"],
            report_id_namespace="impactstory_collection_id",
            api_query="collection/"+collection["_id"]
        )
    else:
        abort(404, "This collection doesn't seem to exist yet. "+url)

@app.route('/item/<ns>/<path:id>')
def item_report(ns, id):
    url = "http://{api_root}/v1/item/{ns}/{id}?key={api_key}".format(
        api_root=os.getenv("API_ROOT"),
        ns=ns,
        id=id,
        api_key=os.environ["API_KEY"]
    )

    r = requests.get(url)
    if r.status_code <= 210: # allow unfinished items
        item = json.loads(r.text)
        return render_template(
            'report.html',
            mixpanel_token=os.environ["MIXPANEL_TOKEN"],
            api_root=os.environ["API_ROOT"],
            api_key=os.environ["API_KEY"],
            request_url=request.url,
            page_title="",
            body_class="report",
            report_id=id,
            report_id_namespace=ns,
            api_query="item/{ns}/{id}".format(ns=ns, id=id)
        )
    else:
        abort(404, "This item doesn't seem to exist yet. "+url)

@app.route("/vitals", methods=['POST'])
def vitals():
    logger.debug("in /vitals.")
    resp = make_response("i am in the new vitals", 200)
    return resp

@app.route('/myvitals', methods=["POST, GET"])
def myvitals():
    """
    Logs reporting stats from the embed code to mixpanel

    Gets a "vitals" object that has a url and a list of "params" objects.
    Each widget on a page sends its own params object; in practice, these will
    almost certainly be identical since users aren't likely to set different
    options for widgets on the same page. It's a good way to count widgets on
    that page, though.

    For documentation on the keys in the the params object, see the default
    params listed at the head of impactstory.js's main() function, and also the
    api-docs page.
    """
    logger.debug("in /myvitals.")
#    vitals = request.json

#    embeds_per_page = len(vitals["allParams"])
#    # heather does awesome things with the vitals and mixpanel here.
#    logger.info("Got vitals message with embeds_per_page={embeds_per_page}".format(
#        embeds_per_page=embeds_per_page))
#    logger.debug("Vitals = {vitals}".format(
#        vitals=vitals))
#
#    mymixpanel.track("Impression:embed", properties={
#        "Host page": vitals["url"],
#        "API Key": vitals["allParams"][0]["api-key"],
#        "Embeds per page": embeds_per_page}, ip=False)

    resp = make_response("duly noted. carry on.", 200)
    # let js clients get this from the browser, regardless of their domain origin.
#    resp.headers['Access-Control-Allow-Origin'] = "*"
#    resp.headers['Access-Control-Allow-Methods'] = "POST, GET, OPTIONS, PUT, DELETE"
#    resp.headers['Access-Control-Allow-Headers'] = "Content-Type"
    return resp

@app.route('/wospicker', methods=["GET"])
def wospicker():
    num_total = int(request.args.get("total"))
    num_subset = int(request.args.get("subset"))

    pages_and_ids = util.pickWosItems(num_total, num_subset)

    resp = make_response(json.dumps(pages_and_ids, indent=4), 200)
    resp.mimetype = "application/json"
    return resp

<commit_msg>new and old vitals are, best i can tell, identical.<commit_after>import requests, iso8601, os, json, logging

from flask import Flask, jsonify, json, request, redirect, abort, make_response
from flask import render_template, flash
from libsaas.services import mixpanel

from totalimpactwebapp import app, util
from totalimpactwebapp.models import Github
from totalimpactwebapp import pretty_date

logger = logging.getLogger("tiwebapp.views")
mymixpanel = mixpanel.Mixpanel(token=os.getenv("MIXPANEL_TOKEN"))
logger.debug("ok, just checking that this refreshed.")

@app.before_request
def log_ip_address():
    if request.endpoint != "static":
        ip_address = request.remote_addr
        logger.info("%30s IP address calling %s %s" % (ip_address, request.method, request.url))

# static pages
@app.route('/')
def home():
    return render_template(
    'index.html',
    page_title="tell the full story of your research impact",
    body_class="homepage",
    mixpanel_token=os.environ["MIXPANEL_TOKEN"],
    api_root=os.environ["API_ROOT"]
    )

@app.route("/embed/impactstory.js")
@app.route("/embed/v1/impactstory.js")
def impactstory_dot_js():

    badges_template = render_template("js-template-badges.html").replace("\n", "")
    ich_def = open(os.path.dirname(__file__) + "/static/js/icanhaz.min.js", "r").read()
    ti_item_def = open(os.path.dirname(__file__) + "/static/js/ti-item.js", "r").read()

    rendered = render_template(
        "impactstory.js",
        ich=ich_def,
        ti_item=ti_item_def,
        badges_template=badges_template,
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        webapp_root=os.environ["WEBAPP_ROOT"]
    )
    resp = make_response(rendered)
    """
    There is no standard way to indicate you're sending back javascript;
    This seems the most recommended one, though. See
    http://stackoverflow.com/questions/2706290/why-write-script-type-text-javascript-when-the-mime-type-is-set-by-the-serve
    and http://www.ietf.org/rfc/rfc4329.txt
     """
    resp.headers["Content-Type"] = "application/javascript; charset=utf-8"
    return resp

@app.route("/embed/test")
def embed_test():
    return render_template(
        "sample-embed-internal-test.html",
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        webapp_root = os.environ["WEBAPP_ROOT"]
    )

@app.route('/about')
def about():
    return render_template(
        'about.html',
        page_title="about",
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"]
        )

@app.route('/faq')
def faq():
    # get the table of items and identifiers
    which_items_loc = os.path.join(
        os.path.dirname(__file__),
        "static",
        "whichartifacts.html"
        )
    which_item_types = open(which_items_loc).read()

    # get the static_meta info for each metric
    try:
        r = requests.get("http://" + os.environ["API_ROOT"] +'/provider')
        metadata = json.loads(r.text)
    except requests.ConnectionError:
        metadata = {}

    return render_template(
        'faq.html',
        page_title="faq",
        which_artifacts=which_item_types,
        provider_metadata=metadata,
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"]
        )

@app.route('/api-docs')
def apidocs():
    return render_template(
        'api-docs.html',
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        webapp_root = os.environ["WEBAPP_ROOT"],
        page_title="api & embed code"
        )

@app.route('/create')
def collection_create():
    return render_template(
        'create-collection.html',
        mixpanel_token=os.environ["MIXPANEL_TOKEN"],
        api_root=os.environ["API_ROOT"],
        page_title="create collection",
        body_class="create-collection"
        )

@app.route('/collection/<collection_id>')
def collection_report(collection_id):
    url = "http://{api_root}/collection/{collection_id}?include_items=0".format(
        api_root=os.getenv("API_ROOT"),
        collection_id=collection_id
    )

    r = requests.get(url)
    if r.status_code == 200:
        collection = json.loads(r.text)
        return render_template(
            'report.html',
            mixpanel_token=os.environ["MIXPANEL_TOKEN"],
            api_root=os.environ["API_ROOT"],
            api_key=os.environ["API_KEY"],
            request_url=request.url,
            page_title=collection["title"],
            body_class="report",
            report_id=collection["_id"],
            report_id_namespace="impactstory_collection_id",
            api_query="collection/"+collection["_id"]
        )
    else:
        abort(404, "This collection doesn't seem to exist yet. "+url)

@app.route('/item/<ns>/<path:id>')
def item_report(ns, id):
    url = "http://{api_root}/v1/item/{ns}/{id}?key={api_key}".format(
        api_root=os.getenv("API_ROOT"),
        ns=ns,
        id=id,
        api_key=os.environ["API_KEY"]
    )

    r = requests.get(url)
    if r.status_code <= 210: # allow unfinished items
        item = json.loads(r.text)
        return render_template(
            'report.html',
            mixpanel_token=os.environ["MIXPANEL_TOKEN"],
            api_root=os.environ["API_ROOT"],
            api_key=os.environ["API_KEY"],
            request_url=request.url,
            page_title="",
            body_class="report",
            report_id=id,
            report_id_namespace=ns,
            api_query="item/{ns}/{id}".format(ns=ns, id=id)
        )
    else:
        abort(404, "This item doesn't seem to exist yet. "+url)

@app.route("/vitals", methods=['POST'])
def vitals():
    logger.debug("in /vitals.")
    resp = make_response("i am in the new vitals", 200)
    return resp

@app.route('/myvitals', methods=["POST"])
def myvitals():
    logger.debug("in /myvitals.")
    resp = make_response("duly noted. carry on.", 200)
    return resp

@app.route('/wospicker', methods=["GET"])
def wospicker():
    num_total = int(request.args.get("total"))
    num_subset = int(request.args.get("subset"))

    pages_and_ids = util.pickWosItems(num_total, num_subset)

    resp = make_response(json.dumps(pages_and_ids, indent=4), 200)
    resp.mimetype = "application/json"
    return resp

<|endoftext|>


<commit_before>#!/usr/bin/env python

"""
Script to test writing GW Input for VASP.
Reads the POSCAR_name in the the current folder and outputs GW input to
subfolders name
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os

from pymatgen.io.abinitio.netcdf import NetcdfReader
from pymatgen.io.vaspio.vasp_output import Vasprun
from pymatgen.core.units import Ha_to_eV
#from numpy import linspace
#from scipy.interpolate import UnivariateSpline

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

class GWConvergenceData():
    def __init__(self, structure, spec):
        self.structure = structure
        self.spec = spec
        self.data = {}
        self.name = structure.composition.reduced_formula

    def read(self):
        if self.spec['code'] == 'ABINIT':
            read_next = True
            n = 3
            while read_next:
                output = os.path.join(self.name,  'work_0', 'task_' + str(n), 'outdata', 'out_SIGRES.nc')
                if os.path.isfile(output):
                    n += 1
                    data = NetcdfReader(output)
                    data.print_tree()
                    self.data.update({n: {'ecuteps': Ha_to_eV * data.read_value('ecuteps'), 'nbands': data.read_value('sigma_nband'), 'gwgap': data.read_value('egwgap')[0][0]}})
                    data.close()
                else:
                    read_next = False
        elif self.spec['code'] == 'VASP':
            tree = os.walk('.')
            n = 0
            for dirs in tree:
                if "/" + self.name + "." in dirs[0] and ('G0W0' in dirs[0] or 'GW0' in dirs[0] or 'scGW0' in dirs[0]):
                    run = os.path.join(dirs[0], 'vasprun.xml')
                    print run
                    kpoints = os.path.join(dirs[0], 'IBZKPT')
                    if os.path.isfile(run):
                        try:
                            data = Vasprun(run, ionic_step_skip=1)
                            parameters = data.__getattribute__('incar').to_dict
                            bandstructure = data.get_band_structure(kpoints)
                            self.data.update({n: {'ecuteps': parameters['ENCUTGW'], 'nbands': parameters['NBANDS'],
                                              'nomega': parameters['NOMEGA'], 'gwgap': bandstructure.get_band_gap()['energy']}})
                            n += 1
                        except BaseException:
                            pass

    def get_var_range(self, var):
        var_range = []
        for data_point in self.data.values():
            if data_point[var] not in var_range:
                var_range.append(data_point[var])
        return sorted(var_range)

    def get_conv_pars(self, tol=0.01):
        ecuteps_l = False
        nbands_l = False
        ecuteps_c = 0
        nbands_c = 0
        xs = self.get_var_range('nbands')
        print xs
        ys = self.get_var_range('ecuteps')
        print ys
        zd = self.get_data_array()
        print zd
        zs = []
        for x in xs:
            for y in ys:
                zs.append(zd[x][y])

            print ys, zs

        return {'control': {'ecuteps': ecuteps_l, 'nbands': nbands_l}, 'values': {'ecuteps': ecuteps_c, 'nbands': nbands_c}}

    def get_sorted_data_list(self):
        data_list = []
        for k in self.data:
            if self.spec['code'] == 'VASP':
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['nomega'], self.data[k]['gwgap']])
            else:
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['gwgap']])
        return sorted(data_list)

    def get_data_array(self):
        data_array = {}
        for k in self.data:
            try:
                data_array[self.data[k]['nbands']].update({self.data[k]['ecuteps']: self.data[k]['gwgap']})
            except KeyError:
                data_array.update({self.data[k]['nbands']: {self.data[k]['ecuteps']: self.data[k]['gwgap']}})
        return data_array

    def print_plot_data(self):
        data_file = self.name + '.data'
        f = open(data_file, mode='w')
        try:
            tmp = self.get_sorted_data_list()[0][0]
        except IndexError:
            tmp = 0
            pass
        for data in self.get_sorted_data_list():
            if tmp != data[0]:
                f.write('\n')
            tmp = data[0]
            if self.spec['code'] == 'VASP':
                f.write(str(data[0]) + ' ' + str(data[1]) + ' ' + str(data[2]) + ' ' + str(data[3]) + '\n')
            else:
                f.write(str(data[0]) + ' ' + str(data[1]) + ' ' + str(data[2]) + '\n')
        f.close()

        '''
        [u'space_group', u'primitive_vectors', u'reduced_symmetry_matrices', u'reduced_symmetry_translations',
         u'atom_species', u'reduced_atom_positions', u'valence_charges', u'atomic_numbers', u'atom_species_names',
         u'chemical_symbols', u'pseudopotential_types', u'symafm', u'kpoint_grid_shift', u'kpoint_grid_vectors',
         u'monkhorst_pack_folding', u'reduced_coordinates_of_kpoints', u'kpoint_weights', u'number_of_electrons',
         u'exchange_functional', u'correlation_functional', u'fermi_energy', u'smearing_scheme', u'smearing_width',
          u'number_of_states', u'eigenvalues', u'occupations', u'tphysel', u'occopt', u'istwfk', u'shiftk',
         u'kptrlatt', u'ngkpt_shiftk', u'ecutwfn', u'ecuteps', u'ecutsigx', u'sigma_nband', u'gwcalctyp',
         u'omegasrdmax', u'deltae', u'omegasrmax', u'scissor_ene', u'usepawu', u'kptgw', u'minbnd', u'maxbnd',
         u'degwgap', u'egwgap', u'en_qp_diago', u'e0', u'e0gap', u'sigxme', u'vxcme', u'vUme', u'degw', u'dsigmee0',
         u'egw', u'eigvec_qp', u'hhartree', u'sigmee', u'sigcmee0', u'sigcmesi', u'sigcme4sd', u'sigxcme4sd',
         u'ze0', u'omega4sd'] for later use
        '''<commit_msg>developing convergence test<commit_after>#!/usr/bin/env python

"""
Script to test writing GW Input for VASP.
Reads the POSCAR_name in the the current folder and outputs GW input to
subfolders name
"""

from __future__ import division

__author__ = "Michiel van Setten"
__copyright__ = " "
__version__ = "0.9"
__maintainer__ = "Michiel van Setten"
__email__ = "mjvansetten@gmail.com"
__date__ = "Oct 23, 2013"

import os

from pymatgen.io.abinitio.netcdf import NetcdfReader
from pymatgen.io.vaspio.vasp_output import Vasprun
from pymatgen.core.units import Ha_to_eV
#from numpy import linspace
#from scipy.interpolate import UnivariateSpline

MODULE_DIR = os.path.dirname(os.path.abspath(__file__))

class GWConvergenceData():
    def __init__(self, structure, spec):
        self.structure = structure
        self.spec = spec
        self.data = {}
        self.name = structure.composition.reduced_formula

    def read(self):
        if self.spec['code'] == 'ABINIT':
            read_next = True
            n = 3
            while read_next:
                output = os.path.join(self.name,  'work_0', 'task_' + str(n), 'outdata', 'out_SIGRES.nc')
                if os.path.isfile(output):
                    n += 1
                    data = NetcdfReader(output)
                    data.print_tree()
                    self.data.update({n: {'ecuteps': Ha_to_eV * data.read_value('ecuteps'), 'nbands': data.read_value('sigma_nband'), 'gwgap': data.read_value('egwgap')[0][0]}})
                    data.close()
                else:
                    read_next = False
        elif self.spec['code'] == 'VASP':
            tree = os.walk('.')
            n = 0
            for dirs in tree:
                if "/" + self.name + "." in dirs[0] and ('G0W0' in dirs[0] or 'GW0' in dirs[0] or 'scGW0' in dirs[0]):
                    run = os.path.join(dirs[0], 'vasprun.xml')
                    print run
                    kpoints = os.path.join(dirs[0], 'IBZKPT')
                    if os.path.isfile(run):
                        try:
                            data = Vasprun(run, ionic_step_skip=1)
                            parameters = data.__getattribute__('incar').to_dict
                            bandstructure = data.get_band_structure(kpoints)
                            self.data.update({n: {'ecuteps': parameters['ENCUTGW'], 'nbands': parameters['NBANDS'],
                                              'nomega': parameters['NOMEGA'], 'gwgap': bandstructure.get_band_gap()['energy']}})
                            n += 1
                        except BaseException:
                            pass

    def get_var_range(self, var):
        var_range = []
        for data_point in self.data.values():
            if data_point[var] not in var_range:
                var_range.append(data_point[var])
        return sorted(var_range)

    def get_conv_pars(self, tol=0.01):
        ecuteps_l = False
        nbands_l = False
        ecuteps_c = 0
        nbands_c = 0
        xs = self.get_var_range('nbands')
        print xs
        ys = self.get_var_range('ecuteps')
        print ys
        zd = self.get_data_array()
        print zd
        for x in xs:
            zs = []
            for y in ys:
                zs.append(zd[x][y])

            print x, ys, zs

        return {'control': {'ecuteps': ecuteps_l, 'nbands': nbands_l}, 'values': {'ecuteps': ecuteps_c, 'nbands': nbands_c}}

    def get_sorted_data_list(self):
        data_list = []
        for k in self.data:
            if self.spec['code'] == 'VASP':
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['nomega'], self.data[k]['gwgap']])
            else:
                data_list.append([self.data[k]['nbands'], self.data[k]['ecuteps'], self.data[k]['gwgap']])
        return sorted(data_list)

    def get_data_array(self):
        data_array = {}
        for k in self.data:
            try:
                data_array[self.data[k]['nbands']].update({self.data[k]['ecuteps']: self.data[k]['gwgap']})
            except KeyError:
                data_array.update({self.data[k]['nbands']: {self.data[k]['ecuteps']: self.data[k]['gwgap']}})
        return data_array

    def print_plot_data(self):
        data_file = self.name + '.data'
        f = open(data_file, mode='w')
        try:
            tmp = self.get_sorted_data_list()[0][0]
        except IndexError:
            tmp = 0
            pass
        for data in self.get_sorted_data_list():
            if tmp != data[0]:
                f.write('\n')
            tmp = data[0]
            if self.spec['code'] == 'VASP':
                f.write(str(data[0]) + ' ' + str(data[1]) + ' ' + str(data[2]) + ' ' + str(data[3]) + '\n')
            else:
                f.write(str(data[0]) + ' ' + str(data[1]) + ' ' + str(data[2]) + '\n')
        f.close()

        '''
        [u'space_group', u'primitive_vectors', u'reduced_symmetry_matrices', u'reduced_symmetry_translations',
         u'atom_species', u'reduced_atom_positions', u'valence_charges', u'atomic_numbers', u'atom_species_names',
         u'chemical_symbols', u'pseudopotential_types', u'symafm', u'kpoint_grid_shift', u'kpoint_grid_vectors',
         u'monkhorst_pack_folding', u'reduced_coordinates_of_kpoints', u'kpoint_weights', u'number_of_electrons',
         u'exchange_functional', u'correlation_functional', u'fermi_energy', u'smearing_scheme', u'smearing_width',
          u'number_of_states', u'eigenvalues', u'occupations', u'tphysel', u'occopt', u'istwfk', u'shiftk',
         u'kptrlatt', u'ngkpt_shiftk', u'ecutwfn', u'ecuteps', u'ecutsigx', u'sigma_nband', u'gwcalctyp',
         u'omegasrdmax', u'deltae', u'omegasrmax', u'scissor_ene', u'usepawu', u'kptgw', u'minbnd', u'maxbnd',
         u'degwgap', u'egwgap', u'en_qp_diago', u'e0', u'e0gap', u'sigxme', u'vxcme', u'vUme', u'degw', u'dsigmee0',
         u'egw', u'eigvec_qp', u'hhartree', u'sigmee', u'sigcmee0', u'sigcmesi', u'sigcme4sd', u'sigxcme4sd',
         u'ze0', u'omega4sd'] for later use
        '''<|endoftext|>


<commit_before>import os
import autopep8
import nbformat

from great_expectations.render.renderer.renderer import Renderer

class NotebookRenderer(Renderer):
    """
    Render a notebook that can re-create or edit the suite.

    Possible use cases:
    - Make an easy path to edit a suite that a Profiler created.
    - Make it easy to edit a suite where only JSON exists.
    """

    @classmethod
    def _get_expectations_by_column(cls, expectations):
        # TODO probably replace this with Suite logic at some point
        expectations_by_column = {"table_expectations": []}
        for exp in expectations:
            if "_table_" in exp["expectation_type"]:
                expectations_by_column["table_expectations"].append(exp)
            else:
                col = exp["kwargs"]["column"]

                if col not in expectations_by_column.keys():
                    expectations_by_column[col] = []
                expectations_by_column[col].append(exp)
        return expectations_by_column

    @classmethod
    def _build_kwargs_string(cls, expectation):
        kwargs = []
        for k, v in expectation["kwargs"].items():
            if k == "column":
                # make the column a positional argument
                kwargs.append("'{}'".format(v))
            elif isinstance(v, str):
                # Put strings in quotes
                kwargs.append("{}='{}'".format(k, v))
            else:
                # Pass other types as is
                kwargs.append("{}={}".format(k, v))

        return ", ".join(kwargs)

    def add_header(self, data_asset_name, suite_name, batch_kwargs):
        self.add_markdown_cell(
            """# Edit Your Expectation Suite
Use this notebook to recreate and modify your expectation suite for:

**Data Asset**: `{}`

**Expectation Suite Name**: `{}`

We'd love it if you **reach out to us on** the [**Great Expectations Slack Channel**](https://greatexpectations.io/slack)""".format(
                data_asset_name, suite_name
            )
        )

        # TODO such brittle hacks to fix paths
        if "path" in batch_kwargs.keys():
            base_dir = batch_kwargs["path"]
            if not base_dir.startswith("/"):
                batch_kwargs["path"] = os.path.join("../../", base_dir)

        self.add_code_cell(
            """\
from datetime import datetime
import great_expectations as ge
import great_expectations.jupyter_ux

context = ge.data_context.DataContext()

expectation_suite_name = "{}"  # Feel free to change the name of your suite here. Renaming this will not remove the other one.
context.create_expectation_suite("{}", expectation_suite_name, overwrite_existing=True)

batch_kwargs = {}
batch = context.get_batch("{}", expectation_suite_name, batch_kwargs)
batch.head()""".format(
                suite_name, data_asset_name, batch_kwargs, data_asset_name
            )
        )

    def add_footer(self):
        self.add_markdown_cell(
            """\
## Save & Review Your Expectations

Let's save the expectation suite as a JSON file in the `great_expectations/expectations` directory of your project.
If you decide not to save some expectations that you created, use [remove_expectaton method](https://docs.greatexpectations.io/en/latest/module_docs/data_asset_module.html?highlight=remove_expectation&utm_source=notebook&utm_medium=edit_expectations#great_expectations.data_asset.data_asset.DataAsset.remove_expectation).

Let's now rebuild your Data Docs, which helps you communicate about your data with both machines and humans."""
        )
        # TODO this may become confusing for users depending on what they are trying
        #  to accomplish in their dev loop
        self.add_code_cell(
            """\
batch.save_expectation_suite(discard_failed_expectations=False)

# Let's make a simple sortable timestamp. Note this could come from your pipeline runner.
run_id = datetime.utcnow().isoformat().replace(":", "") + "Z"

results = context.run_validation_operator("action_list_operator", assets_to_validate=[batch], run_id=run_id)
context.build_data_docs()
context.open_data_docs()"""
        )

    def add_code_cell(self, code, lint=False):
        """
        Add the given code as a new code cell.
        """
        if lint:
            try:
                code = autopep8.fix_code(code, options={"aggressive": 2}).rstrip("\n")
            except RuntimeError:
                pass

        cell = nbformat.v4.new_code_cell(code)
        self.notebook["cells"].append(cell)

    def add_markdown_cell(self, markdown):
        """
        Add the given markdown as a new markdown cell.
        """
        cell = nbformat.v4.new_markdown_cell(markdown)
        self.notebook["cells"].append(cell)

    def add_expectation_cells_from_suite(self, expectations):
        expectations_by_column = self._get_expectations_by_column(expectations)
        self.add_markdown_cell("### Table Expectation(s)")
        if expectations_by_column["table_expectations"]:
            for exp in expectations_by_column["table_expectations"]:
                kwargs_string = self._build_kwargs_string(exp)
                code = "batch.{}({})".format(exp["expectation_type"], kwargs_string)
                self.add_code_cell(code, lint=True)
        else:
            self.add_markdown_cell(
                "No table level expectations are in this suite. Feel free to "
                "add some here. The all begin with `batch.expect_table_...`."
            )

        # Remove the table expectations since they are dealt with
        expectations_by_column.pop("table_expectations")

        self.add_markdown_cell("### Column Expectation(s)")

        for column, expectations in expectations_by_column.items():
            self.add_markdown_cell("#### `{}`".format(column))

            for exp in expectations:
                kwargs_string = self._build_kwargs_string(exp)
                meta_args = ", meta={}".format(exp.meta) if exp.meta else ""
                code = "batch.{}({}{})".format(
                    exp["expectation_type"], kwargs_string, meta_args
                )
                self.add_code_cell(code, lint=True)

    @classmethod
    def _write_notebook_to_disk(cls, notebook, notebook_file_path):
        with open(notebook_file_path, "w") as f:
            nbformat.write(notebook, f)

    def render(self, suite, batch_kwargs):
        """
        Render a notebook dict from an expectation suite.
        """
        if not isinstance(suite, ExpectationSuite):
            raise RuntimeWarning(
                "render must be given an ExpectationSuite."
            )
        if not isinstance(batch_kwargs, dict):
            raise RuntimeWarning("render must be given a dictionary of batch_kwargs.")

        self.notebook = nbformat.v4.new_notebook()

        data_asset_name = suite.data_asset_name
        suite_name = suite.expectation_suite_name

        self.add_header(data_asset_name, suite_name, batch_kwargs)
        self.add_authoring_intro()
        self.add_expectation_cells_from_suite(suite.expectations)
        self.add_footer()

        return self.notebook

    def render_to_disk(self, suite, batch_kwargs, notebook_file_path):
        """
        Render a notebook to disk from an expectation suite.

        :param batch_kwargs:
        :type suite: dict
        :type notebook_file_path: str
        """
        self.render(suite, batch_kwargs)
        self._write_notebook_to_disk(self.notebook, notebook_file_path)

    def add_authoring_intro(self):
        self.add_markdown_cell(
            """\
## Create & Edit Expectations

Add expectations by calling specific expectation methods on the `batch` object. They all begin with `.expect_` which makes autocompleting easy using tab.

You can see all the available expectations in the **[expectation glossary](https://docs.greatexpectations.io/en/latest/expectation_glossary.html?utm_source=notebook&utm_medium=create_expectations)**."""
        )
<commit_msg>Update last cell to open data docs to validation result page (#1016)<commit_after>import os
import autopep8
import nbformat

from great_expectations.render.renderer.renderer import Renderer

class NotebookRenderer(Renderer):
    """
    Render a notebook that can re-create or edit the suite.

    Possible use cases:
    - Make an easy path to edit a suite that a Profiler created.
    - Make it easy to edit a suite where only JSON exists.
    """

    @classmethod
    def _get_expectations_by_column(cls, expectations):
        # TODO probably replace this with Suite logic at some point
        expectations_by_column = {"table_expectations": []}
        for exp in expectations:
            if "_table_" in exp["expectation_type"]:
                expectations_by_column["table_expectations"].append(exp)
            else:
                col = exp["kwargs"]["column"]

                if col not in expectations_by_column.keys():
                    expectations_by_column[col] = []
                expectations_by_column[col].append(exp)
        return expectations_by_column

    @classmethod
    def _build_kwargs_string(cls, expectation):
        kwargs = []
        for k, v in expectation["kwargs"].items():
            if k == "column":
                # make the column a positional argument
                kwargs.append("'{}'".format(v))
            elif isinstance(v, str):
                # Put strings in quotes
                kwargs.append("{}='{}'".format(k, v))
            else:
                # Pass other types as is
                kwargs.append("{}={}".format(k, v))

        return ", ".join(kwargs)

    def add_header(self, data_asset_name, suite_name, batch_kwargs):
        self.add_markdown_cell(
            """# Edit Your Expectation Suite
Use this notebook to recreate and modify your expectation suite for:

**Data Asset**: `{}`

**Expectation Suite Name**: `{}`

We'd love it if you **reach out to us on** the [**Great Expectations Slack Channel**](https://greatexpectations.io/slack)""".format(
                data_asset_name, suite_name
            )
        )

        # TODO such brittle hacks to fix paths
        if "path" in batch_kwargs.keys():
            base_dir = batch_kwargs["path"]
            if not base_dir.startswith("/"):
                batch_kwargs["path"] = os.path.join("../../", base_dir)

        self.add_code_cell(
            """\
from datetime import datetime
import great_expectations as ge
import great_expectations.jupyter_ux
from great_expectations.data_context.types.resource_identifiers import ValidationResultIdentifier

context = ge.data_context.DataContext()

expectation_suite_name = "{}"  # Feel free to change the name of your suite here. Renaming this will not remove the other one.
context.create_expectation_suite("{}", expectation_suite_name, overwrite_existing=True)

batch_kwargs = {}
batch = context.get_batch("{}", expectation_suite_name, batch_kwargs)
batch.head()""".format(
                suite_name, data_asset_name, batch_kwargs, data_asset_name
            )
        )

    def add_footer(self):
        self.add_markdown_cell(
            """\
## Save & Review Your Expectations

Let's save the expectation suite as a JSON file in the `great_expectations/expectations` directory of your project.
If you decide not to save some expectations that you created, use [remove_expectaton method](https://docs.greatexpectations.io/en/latest/module_docs/data_asset_module.html?highlight=remove_expectation&utm_source=notebook&utm_medium=edit_expectations#great_expectations.data_asset.data_asset.DataAsset.remove_expectation).

Let's now rebuild your Data Docs, which helps you communicate about your data with both machines and humans."""
        )
        # TODO this may become confusing for users depending on what they are trying
        #  to accomplish in their dev loop
        self.add_code_cell(
            """\
batch.save_expectation_suite(discard_failed_expectations=False)

# Let's make a simple sortable timestamp. Note this could come from your pipeline runner.
run_id = datetime.utcnow().isoformat().replace(":", "") + "Z"

results = context.run_validation_operator("action_list_operator", assets_to_validate=[batch], run_id=run_id)
expectation_suite_identifier = list(results["details"].keys())[0]
validation_result_identifier = ValidationResultIdentifier(
    expectation_suite_identifier=expectation_suite_identifier,
    run_id=run_id
)
context.build_data_docs()
context.open_data_docs(validation_result_identifier)"""
        )

    def add_code_cell(self, code, lint=False):
        """
        Add the given code as a new code cell.
        """
        if lint:
            try:
                code = autopep8.fix_code(code, options={"aggressive": 2}).rstrip("\n")
            except RuntimeError:
                pass

        cell = nbformat.v4.new_code_cell(code)
        self.notebook["cells"].append(cell)

    def add_markdown_cell(self, markdown):
        """
        Add the given markdown as a new markdown cell.
        """
        cell = nbformat.v4.new_markdown_cell(markdown)
        self.notebook["cells"].append(cell)

    def add_expectation_cells_from_suite(self, expectations):
        expectations_by_column = self._get_expectations_by_column(expectations)
        self.add_markdown_cell("### Table Expectation(s)")
        if expectations_by_column["table_expectations"]:
            for exp in expectations_by_column["table_expectations"]:
                kwargs_string = self._build_kwargs_string(exp)
                code = "batch.{}({})".format(exp["expectation_type"], kwargs_string)
                self.add_code_cell(code, lint=True)
        else:
            self.add_markdown_cell(
                "No table level expectations are in this suite. Feel free to "
                "add some here. The all begin with `batch.expect_table_...`."
            )

        # Remove the table expectations since they are dealt with
        expectations_by_column.pop("table_expectations")

        self.add_markdown_cell("### Column Expectation(s)")

        for column, expectations in expectations_by_column.items():
            self.add_markdown_cell("#### `{}`".format(column))

            for exp in expectations:
                kwargs_string = self._build_kwargs_string(exp)
                meta_args = ", meta={}".format(exp.meta) if exp.meta else ""
                code = "batch.{}({}{})".format(
                    exp["expectation_type"], kwargs_string, meta_args
                )
                self.add_code_cell(code, lint=True)

    @classmethod
    def _write_notebook_to_disk(cls, notebook, notebook_file_path):
        with open(notebook_file_path, "w") as f:
            nbformat.write(notebook, f)

    def render(self, suite, batch_kwargs):
        """
        Render a notebook dict from an expectation suite.
        """
        if not isinstance(suite, ExpectationSuite):
            raise RuntimeWarning(
                "render must be given an ExpectationSuite."
            )
        if not isinstance(batch_kwargs, dict):
            raise RuntimeWarning("render must be given a dictionary of batch_kwargs.")

        self.notebook = nbformat.v4.new_notebook()

        data_asset_name = suite.data_asset_name
        suite_name = suite.expectation_suite_name

        self.add_header(data_asset_name, suite_name, batch_kwargs)
        self.add_authoring_intro()
        self.add_expectation_cells_from_suite(suite.expectations)
        self.add_footer()

        return self.notebook

    def render_to_disk(self, suite, batch_kwargs, notebook_file_path):
        """
        Render a notebook to disk from an expectation suite.

        :param batch_kwargs:
        :type suite: dict
        :type notebook_file_path: str
        """
        self.render(suite, batch_kwargs)
        self._write_notebook_to_disk(self.notebook, notebook_file_path)

    def add_authoring_intro(self):
        self.add_markdown_cell(
            """\
## Create & Edit Expectations

Add expectations by calling specific expectation methods on the `batch` object. They all begin with `.expect_` which makes autocompleting easy using tab.

You can see all the available expectations in the **[expectation glossary](https://docs.greatexpectations.io/en/latest/expectation_glossary.html?utm_source=notebook&utm_medium=create_expectations)**."""
        )
<|endoftext|>


<commit_before>from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime
import pytz

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('.')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    """Shows basic usage of the Google Calendar API.

    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    remember_hours =2
    #From this time to remember_hour later - 5 minutes (trying avoid duplicates messages
    now = datetime.datetime.utcnow() + datetime.timedelta(hours=remember_hours)
    now_1_hour = now+datetime.timedelta(hours=1) - datetime.timedelta(minutes=5)
    now = now.isoformat() + 'Z'
    now_1_hour = now_1_hour.isoformat() +'Z'
    #ADD more calendars
    calendars = ['a1nvjeb8qbs6d496aeo84i43r8@group.calendar.google.com']
    events = []
    for calendar in calendars:
    	eventsResult = service.events().list(
            calendarId=calendar, timeMin=now, timeMax=now_1_hour, singleEvents=True,
            orderBy='startTime').execute()
    	events = events + eventsResult.get('items', [])

    for event in events:
	if 'description' in event:
	    details = event['description'].split('\n')
	    for detail in details:
		detail = detail.strip()
	        if "Name:" in detail:
	    	    client= (detail.split(':')[1]).strip()
		if "Phone:" in detail:
		    phone= (detail.split(':')[1]).strip()
	    if 'phone' in locals():
	        print('{0}:"Hola, Te recordamos que hoy tienes cita con nosotros"'.format(phone))

if __name__ == '__main__':
    main()
<commit_msg>replace from min 0 to min 59<commit_after>from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime
import pytz

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('.')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    """Shows basic usage of the Google Calendar API.

    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    remember_hours =2
    #From this time to remember_hour later - 5 minutes (trying avoid duplicates messages
    now = datetime.datetime.utcnow() + datetime.timedelta(hours=remember_hours)
    now = now.replace(minute=0, second=0, microsecond=0)
    now_1_hour = now+datetime.timedelta(hours=1) - datetime.timedelta(minutes=1)
    now = now.isoformat() + 'Z'
    now_1_hour = now_1_hour.isoformat() +'Z'
    #ADD more calendars
    calendars = ['a1nvjeb8qbs6d496aeo84i43r8@group.calendar.google.com']
    events = []
    for calendar in calendars:
    	eventsResult = service.events().list(
            calendarId=calendar, timeMin=now, timeMax=now_1_hour, singleEvents=True,
            orderBy='startTime').execute()
    	events = events + eventsResult.get('items', [])

    for event in events:
	if 'description' in event:
	    details = event['description'].split('\n')
	    for detail in details:
		detail = detail.strip()
	        if "Name:" in detail:
	    	    client= (detail.split(':')[1]).strip()
		if "Phone:" in detail:
		    phone= (detail.split(':')[1]).strip()
	    if 'phone' in locals():
	        print('{0}:"Hola, Te recordamos que hoy tienes cita con nosotros"'.format(phone))

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from __future__ import absolute_import, division
from datetime import datetime
import re
import time

from bs4 import BeautifulSoup
from django.contrib.auth.models import User
import dateutil.parser
from django.utils import timezone
from pledges.models import Pledge

from users.models import Course, Quiz, Progress

class CourseraScraper:
    def __init__(self, ):
        from selenium import webdriver
        from pyvirtualdisplay import Display

        self.display = Display(visible=0, size=(1024, 768))
        self.display.start()
        self.driver = webdriver.Firefox()
        self.courses = []

    def login(self, EMAIL, PASSWORD):
        try:
            self.driver.get('https://www.coursera.org/account/signin')
            email_field = self.driver.find_element_by_id("signin-email")
            password_field = self.driver.find_element_by_id("signin-password")
            email_field.send_keys(EMAIL)
            password_field.send_keys(PASSWORD)
            password_field.submit()
        except:
            pass

    def get_courses(self):
        try:
            if self.driver.current_url != 'https://www.coursera.org/':
                return [], [], [], [], [], [], [], "Incorrect Login"
            soup = BeautifulSoup(self.driver.page_source)
            users_courses = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-name')
            info_links = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-links .internal-home')
            dates = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-progress .progress-label')
            start_dates = dates[::2]
            end_dates = dates[1::2]
            course_ids = soup.select('#coursera-feed-tabs-current .coursera-dashboard-course-listing-box')
            course_ids = map(lambda x: int(x.attrs['data-course-id']), course_ids)
            image_links = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-icon')
            image_links = map(lambda x: x.attrs['src'], image_links)
            return map(lambda x: x.contents[0].contents[0], users_courses), map(lambda x: x.contents[0].attrs['href'],
                                                                                users_courses), map(
                lambda x: x.attrs['href'],
                info_links), map(lambda x: str(x.contents[0] + ' 2014'), start_dates), map(
                lambda x: str(x.contents[0] + ' 2014'), end_dates), course_ids, image_links, None
        except:
            return [], [], [], [], [], [], [], None

    def get_quiz_link(self, course, link):
        if course.info_link:
            self.driver.get('https://www.coursera.org/' + course.info_link)
            soup = BeautifulSoup(self.driver.page_source)
            p_description = soup.select('.coursera-course-content p:first')
            if p_description:
                course.description = p_description[0].contents[0]
        self.driver.get(link)
        soup = BeautifulSoup(self.driver.page_source)
        link = soup.find('a', {'data-ab-user-convert': 'navclick_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Homework_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Data_Sets_/_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Review_Questions'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Homework'}, href=True)
        if link and link['href'] and link['href'] != '':
            if link['href'].startswith('/'):
                link['href'] = 'https://class.coursera.org' + link['href']
            course.quiz_link = link['href']
            self.driver.get(link['href'])
            soup = BeautifulSoup(self.driver.page_source)
            quiz_list = soup.select('div.course-item-list .course-item-list-header')
            quiz_details = soup.select('ul.course-item-list-section-list')
            for i, quiz_coursera in enumerate(quiz_list):
                heading = quiz_coursera.select('h3')[0].find(text=True, recursive=False)
                deadline = None
                try:
                    deadline = dateutil.parser.parse(str(
                        quiz_details[i].select('.course-quiz-item-softdeadline .course-assignment-deadline')[
                            0].contents[
                            0].replace('\n', '')))
                except IndexError:
                    pass
                hard_deadline = None
                try:
                    hard_deadline = dateutil.parser.parse(quiz_details[i].select(
                        '.course-quiz-item-harddeadline .course-assignment-deadline')[0].contents[
                                                              0].replace('\n', ''))
                except IndexError:
                    pass
                if hard_deadline is None:
                    hard_deadline = timezone.now()

                if deadline is None:
                    deadline = hard_deadline
                try:
                    Quiz.objects.get(heading=heading, course=course)
                except Quiz.DoesNotExist:
                    Quiz.objects.create(heading=heading,
                                        deadline=deadline,
                                        hard_deadline=hard_deadline,
                                        course=course)
        course.save()

    def get_course_progress(self, user, course):
        if course.quiz_link and course.quiz_link != '':
            self.driver.get(course.quiz_link)
            soup = BeautifulSoup(self.driver.page_source)
            quiz_list = soup.select('div.course-item-list .course-item-list-header')
            quiz_details = soup.select('ul.course-item-list-section-list')
            for i, quiz_coursera in enumerate(quiz_list):
                try:
                    quiz = Quiz.objects.get(heading=quiz_coursera.select('h3')[0].find(text=True, recursive=False),
                                            course=course)
                    try:
                        progress = Progress.objects.get(quiz=quiz, user=user.userprofile)
                    except Progress.DoesNotExist:
                        progress = Progress.objects.create(quiz=quiz, user=user.userprofile)
                    progress.score = quiz_details[i].select(
                        '.course-quiz-item-score td span')[0].contents[0]
                    progress.save()
                except Quiz.DoesNotExist:
                    print "Not found"

    def get_course_completion(self, courseraprofile, pledges):
        self.driver.get('https://www.coursera.org/account/records')
        btn = self.driver.find_element_by_xpath(
            '//*[@id="origami"]/div/div/div[1]/div[3]/div[1]/div/div[2]/div[1]/div/a')
        btn.click()
        soup = BeautifulSoup(self.driver.page_source)
        course_ids = soup.select('.coursera-records-course-listing-box')
        course_ids = map(lambda x: int(x.attrs['data-course-id']), course_ids)
        grades = soup.select(
            '.coursera-course-records-listings-without .coursera-course-listing-grade-section div[class~=hide] span')[
                 ::2]
        for i, grade in enumerate(grades):
            if str(course_ids[i]) not in courseraprofile.counted_as_completed.split(','):
                your_pledges = pledges.filter(course__course_id=course_ids[i])
                for pledge in your_pledges:
                    mark = re.findall("\d+.\d+", grade.contents[0])
                    if mark:
                        pledge.actual_mark = float(mark[0]) / 100
                        pledge.is_complete = True
                        pledge.complete_date = timezone.now()
                        pledge.save()
                if str(courseraprofile.counted_as_completed) != '':
                    courseraprofile.counted_as_completed += ',' + str(course_ids[i])
                else:
                    courseraprofile.counted_as_completed += str(course_ids[i])

        courseraprofile.save()

    def end(self):
        self.driver.close()
        self.display.stop()

def get_courses(user_id):
    scraper = CourseraScraper()
    user = User.objects.get(pk=user_id)
    print user
    if str(user.courseraprofile.username) != '':
        scraper.driver.implicitly_wait(5)
        scraper.login(str(user.courseraprofile.username), str(user.courseraprofile.password))
        time.sleep(3)
        courses, course_links, internal_links, start_dates, end_dates, course_ids, image_links, error = scraper.get_courses()
        if error is not None:
            user.courseraprofile.incorrect_login = True
            user.courseraprofile.last_updated = timezone.now()
            user.courseraprofile.save()
            return
        else:
            user.courseraprofile.incorrect_login = False
        print courses, image_links
        django_courses = []
        for i, course in enumerate(courses):
            try:
                get_course = Course.objects.get(title=course)
                django_courses.append(get_course)
                get_course.course_link = course_links[i]
                get_course.info_link = internal_links[i]
                get_course.course_id = course_ids[i]
                get_course.image_link = image_links[i]
                get_course.start_date = datetime.strptime(
                    start_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', ''),
                    "%b %d %Y").date()
                get_course.end_date = datetime.strptime(
                    end_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', ''),
                    "%b %d %Y").date()
                get_course.save()
                user.courseraprofile.courses.add(get_course)
            except Course.DoesNotExist:
                get_course = Course.objects.create(title=course, course_link=course_links[i], course_id=course_ids[i],
                                                   info_link=internal_links[i], start_date=
                    datetime.strptime(
                        str(start_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', '')),
                        '%b %d %Y').date(),
                                                   end_date=datetime.strptime(str(
                                                       end_dates[i].replace('th', '').replace('st', '').replace('nd',
                                                                                '').replace(
                                                           'rd', '')), '%b %d %Y').date(),
                                                   image_link=image_links[i])
                user.courseraprofile.courses.add(get_course)
        user.courseraprofile.last_updated = timezone.now()
        user.courseraprofile.save()
        for i, course in enumerate(django_courses):
            get_course = course
            if get_course.end_date >= timezone.now().date():
                scraper.get_quiz_link(get_course, course_links[i])
                scraper.get_course_progress(user, get_course)
        scraper.get_course_completion(user.courseraprofile,
                                      Pledge.objects.filter(user=user.userprofile, is_complete=False))
        print "Done"
    scraper.end()<commit_msg>temp fix<commit_after>from __future__ import absolute_import, division
from datetime import datetime
import re
import time

from bs4 import BeautifulSoup
from django.contrib.auth.models import User
import dateutil.parser
from django.utils import timezone
from pledges.models import Pledge

from users.models import Course, Quiz, Progress

class CourseraScraper:
    def __init__(self, ):
        from selenium import webdriver
        from pyvirtualdisplay import Display

        self.display = Display(visible=0, size=(1024, 768))
        self.display.start()
        self.driver = webdriver.Firefox()
        self.courses = []

    def login(self, EMAIL, PASSWORD):
        try:
            self.driver.get('https://www.coursera.org/account/signin')
            email_field = self.driver.find_element_by_id("signin-email")
            password_field = self.driver.find_element_by_id("signin-password")
            email_field.send_keys(EMAIL)
            password_field.send_keys(PASSWORD)
            password_field.submit()
        except:
            pass

    def get_courses(self):
        try:
            if self.driver.current_url != 'https://www.coursera.org/':
                return [], [], [], [], [], [], [], "Incorrect Login"
            soup = BeautifulSoup(self.driver.page_source)
            users_courses = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-name')
            info_links = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-links .internal-home')
            dates = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-progress .progress-label')
            start_dates = dates[::2]
            end_dates = dates[1::2]
            course_ids = soup.select('#coursera-feed-tabs-current .coursera-dashboard-course-listing-box')
            course_ids = map(lambda x: int(x.attrs['data-course-id']), course_ids)
            image_links = soup.select(
                '#coursera-feed-tabs-current .coursera-dashboard-course-listing-box .coursera-dashboard-course-listing-box-icon')
            image_links = map(lambda x: x.attrs['src'], image_links)
            return map(lambda x: x.contents[0].contents[0], users_courses), map(lambda x: x.contents[0].attrs['href'],
                                                                                users_courses), map(
                lambda x: x.attrs['href'],
                info_links), map(lambda x: str(x.contents[0] + ' 2014'), start_dates), map(
                lambda x: str(x.contents[0] + ' 2014'), end_dates), course_ids, image_links, None
        except:
            return [], [], [], [], [], [], [], None

    def get_quiz_link(self, course, link):
        if course.info_link:
            self.driver.get('https://www.coursera.org/' + course.info_link)
            soup = BeautifulSoup(self.driver.page_source)
            p_description = soup.select('.coursera-course-content p:first')
            if p_description:
                course.description = p_description[0].contents[0]
        self.driver.get(link)
        soup = BeautifulSoup(self.driver.page_source)
        link = soup.find('a', {'data-ab-user-convert': 'navclick_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Homework_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Data_Sets_/_Quizzes'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Review_Questions'}, href=True)
        if not link:
            link = soup.find('a', {'data-ab-user-convert': 'navclick_Homework'}, href=True)
        if link and link['href'] and link['href'] != '':
            if link['href'].startswith('/'):
                link['href'] = 'https://class.coursera.org' + link['href']
            course.quiz_link = link['href']
            self.driver.get(link['href'])
            soup = BeautifulSoup(self.driver.page_source)
            quiz_list = soup.select('div.course-item-list .course-item-list-header')
            quiz_details = soup.select('ul.course-item-list-section-list')
            for i, quiz_coursera in enumerate(quiz_list):
                heading = quiz_coursera.select('h3')[0].find(text=True, recursive=False)
                deadline = None
                try:
                    deadline = dateutil.parser.parse(str(
                        quiz_details[i].select('.course-quiz-item-softdeadline .course-assignment-deadline')[
                            0].contents[
                            0].replace('\n', '')))
                except IndexError:
                    pass
                hard_deadline = None
                try:
                    hard_deadline = dateutil.parser.parse(quiz_details[i].select(
                        '.course-quiz-item-harddeadline .course-assignment-deadline')[0].contents[
                                                              0].replace('\n', ''))
                except IndexError:
                    pass
                if hard_deadline is None:
                    hard_deadline = timezone.now()

                if deadline is None:
                    deadline = hard_deadline
                try:
                    Quiz.objects.get(heading=heading, course=course)
                except Quiz.DoesNotExist:
                    Quiz.objects.create(heading=heading,
                                        deadline=deadline,
                                        hard_deadline=hard_deadline,
                                        course=course)
        course.save()

    def get_course_progress(self, user, course):
        if course.quiz_link and course.quiz_link != '':
            self.driver.get(course.quiz_link)
            soup = BeautifulSoup(self.driver.page_source)
            quiz_list = soup.select('div.course-item-list .course-item-list-header')
            quiz_details = soup.select('ul.course-item-list-section-list')
            for i, quiz_coursera in enumerate(quiz_list):
                try:
                    quiz = Quiz.objects.get(heading=quiz_coursera.select('h3')[0].find(text=True, recursive=False),
                                            course=course)
                    try:
                        progress = Progress.objects.get(quiz=quiz, user=user.userprofile)
                    except Progress.DoesNotExist:
                        progress = Progress.objects.create(quiz=quiz, user=user.userprofile)
                    progress.score = quiz_details[i].select(
                        '.course-quiz-item-score td span')[0].contents[0]
                    progress.save()
                except Quiz.DoesNotExist:
                    print "Not found"

    def get_course_completion(self, courseraprofile, pledges):
        self.driver.get('https://www.coursera.org/account/records')
        btn = self.driver.find_element_by_xpath(
            '//*[@id="origami"]/div/div/div[1]/div[3]/div[1]/div/div[2]/div[1]/div/a')
        btn.click()
        soup = BeautifulSoup(self.driver.page_source)
        course_ids = soup.select('.coursera-records-course-listing-box')
        course_ids = map(lambda x: int(x.attrs['data-course-id']), course_ids)
        grades = soup.select(
            '.coursera-course-records-listings-without .coursera-course-listing-grade-section div[class~=hide] span')[
                 ::2]
        for i, grade in enumerate(grades):
            if str(course_ids[i]) not in courseraprofile.counted_as_completed.split(','):
                your_pledges = pledges.filter(course__course_id=course_ids[i])
                for pledge in your_pledges:
                    mark = re.findall("\d+.\d+", grade.contents[0])
                    if mark:
                        pledge.actual_mark = float(mark[0]) / 100
                        pledge.is_complete = True
                        pledge.complete_date = timezone.now()
                        pledge.save()
                if str(courseraprofile.counted_as_completed) != '':
                    courseraprofile.counted_as_completed += ',' + str(course_ids[i])
                else:
                    courseraprofile.counted_as_completed += str(course_ids[i])

        courseraprofile.save()

    def end(self):
        self.driver.close()
        self.display.stop()

def get_courses(user_id):
    scraper = CourseraScraper()
    user = User.objects.get(pk=user_id)
    print user
    if str(user.courseraprofile.username) != '':
        scraper.driver.implicitly_wait(5)
        scraper.login(str(user.courseraprofile.username), str(user.courseraprofile.password))
        time.sleep(3)
        courses, course_links, internal_links, start_dates, end_dates, course_ids, image_links, error = scraper.get_courses()
        if error is not None:
            user.courseraprofile.incorrect_login = True
            user.courseraprofile.last_updated = timezone.now()
            user.courseraprofile.save()
            return
        else:
            user.courseraprofile.incorrect_login = False
        print courses, image_links
        django_courses = []
        try:
            for i, course in enumerate(courses):
                try:
                    get_course = Course.objects.get(title=course)
                    django_courses.append(get_course)
                    get_course.course_link = course_links[i]
                    get_course.info_link = internal_links[i]
                    get_course.course_id = course_ids[i]
                    get_course.image_link = image_links[i]
                    get_course.start_date = datetime.strptime(
                        start_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', ''),
                        "%b %d %Y").date()
                    get_course.end_date = datetime.strptime(
                        end_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', ''),
                        "%b %d %Y").date()
                    get_course.save()
                    user.courseraprofile.courses.add(get_course)
                except Course.DoesNotExist:
                    get_course = Course.objects.create(title=course, course_link=course_links[i], course_id=course_ids[i],
                                                       info_link=internal_links[i], start_date=
                        datetime.strptime(
                            str(start_dates[i].replace('th', '').replace('st', '').replace('nd', '').replace('rd', '')),
                            '%b %d %Y').date(),
                                                       end_date=datetime.strptime(str(
                                                           end_dates[i].replace('th', '').replace('st', '').replace('nd',
                                                                                '').replace(
                                                               'rd', '')), '%b %d %Y').date(),
                                                       image_link=image_links[i])
                    user.courseraprofile.courses.add(get_course)
        except IndexError:
            pass
        except Exception as e:
            print e, "Inside"
        user.courseraprofile.last_updated = timezone.now()
        user.courseraprofile.save()
        for i, course in enumerate(django_courses):
            get_course = course
            if get_course.end_date >= timezone.now().date():
                scraper.get_quiz_link(get_course, course_links[i])
                scraper.get_course_progress(user, get_course)
        scraper.get_course_completion(user.courseraprofile,
                                      Pledge.objects.filter(user=user.userprofile, is_complete=False))
        print "Done"
    scraper.end()<|endoftext|>


<commit_before>#! /usr/bin/python2.7
# -*- coding: utf-8 -*-

'''
Work with errorbars on your data:
 value +- error
'''

import numpy as np

class errval(object):
    def __init__(self,val,err=0,printout='latex'):
        '''
        val = the value (number or errval (see below))
        err = the corresponding error
        printout = what 'print' should look like

        if you initiate with val=errval a copy of that input errval is provided
        if you then want to change the printout value for that copy you have
        two choices:
            (1) a posteriori with .printout('mynewopt')
            (2) by specifying the new option with a !bang:
                cp = errval(orig,printout='mynewopt!')
        '''
        if isinstance(val,errval): # return copy
            self.__val = val.val()
            self.__assign_err(val.err())
            self.__printout = val.printout()
            if printout.endswith('!'): self.__printout = printout
        elif ( isinstance(val,tuple) and len(val)==2
               and isinstance(val[0],(int,float,long)) and isinstance(val[1],(int,float,long)) ):
            self.__val = val[0]
            self.__assign_err(val[1])
            self.__printout = printout
        elif ( isinstance(val,tuple) and len(val)==3
               and isinstance(val[0],(int,float,long)) and isinstance(val[1],(int,float,long))
               and isinstance(val[2],str) ):
            self.__val = val[0]
            self.__assign_err(val[1])
            self.__printout = val[2]
        elif isinstance(val,(int,float,long)):
            self.__val = val
            self.__assign_err(err)
            self.__printout = printout
        else:
            raise ValueError, 'Cannot assign input data'

    def val(self):
        return self.__val
    def err(self):
        return self.__err
    def v(self):
        return self.__val
    def e(self):
        return self.__err
    def printout(self,change=''):
        if change!='':
            self.__printout = change
        return self.__printout

    def __str__(self):
        '''
        called by print
        output depends on initialization
        when two errvals are put together the result has the value of the left one
        '''
        if self.__printout == '+-':
            return "{0} +- {1}".format(self.__val,self.__err)
        if self.__printout == 'cp': # make it easier to copy paste...
            return "errval({0},{1})".format(self.val(),self.err())
        if self.__printout == 'cpp': # make it easier to copy paste...
            return "errval({0},{1},errvalmode)".format(self.val(),self.err())
        else: # default = latex
            return "{0} \pm {1}".format(self.__val,self.__err)

    def __assign_err(self,err):
        if err<0: raise ValueError, 'Cannot assign negative error'
        else: self.__err = err

    def __add__(self,other):
        if isinstance(other,errval):
            nval = self.val() + other.val()
            nerr = np.sqrt( self.err()**2 + other.err()**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() + other
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for +: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __radd__(self,other):
        return self.__add__(other)

    def __sub__(self,other):
        if isinstance(other,errval):
            nval = self.val() - other.val()
            nerr = np.sqrt( self.err()**2 + other.err()**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() - other
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for -: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rsub__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other - self.val()
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for -: {0} with errval'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __mul__(self,other):
        if isinstance(other,errval):
            nval = self.val() * other.val()
            nerr = np.sqrt( (other.val()*self.err())**2 + (self.val()*other.err())**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() * other
            nerr = self.err() * abs(other)
        else:
            raise TypeError, 'unsupported operand type(s) for *: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rmul__(self,other):
        return self.__mul__(other)

    def __div__(self,other):
        if isinstance(other,errval):
            nval = self.val() *1.0 / other.val()
            nerr = np.sqrt( (1.0/other.val()*self.err())**2 + (self.val()*1.0/(other.val()**2)*other.err())**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() *1.0 / other
            nerr = self.err() *1.0/ abs(other)
        else:
            raise TypeError, 'unsupported operand type(s) for /: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rdiv__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other *1.0/self.val()
            nerr = abs(other)*1.0/self.val()**2 * self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for /: {0} with errval'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __pow__(self,other):
        if isinstance(other,errval):
            nval = self.val() ** other.val()
            nerr = np.sqrt( ( other.val() * self.val()**(other.val()-1) * self.err() )**2
                            + ( np.log(self.val()) * self.val()**other.val() * other.err() )**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() ** other
            nerr = abs( other * self.val()**(other-1) * self.err() )
        else:
            raise TypeError, 'unsupported operand type(s) for **: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rpow__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other ** self.val()
            nerr = abs( np.log(other) * other**self.val() * self.err() )
        else:
            raise TypeError, 'unsupported operand type(s) for **: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __abs__(self):
        return errval(abs(self.val()), self.err(), self.printout())

    def sqrt(self):
        return self**0.5

    def round(self,n=0):
        # returns new instance
        return errval(np.around(self.val(),n),np.around(self.err(),n),self.printout())

<commit_msg>printout overwrite with !, but then dont store the ! (!)<commit_after>#! /usr/bin/python2.7
# -*- coding: utf-8 -*-

'''
Work with errorbars on your data:
 value +- error
'''

import numpy as np

class errval(object):
    def __init__(self,val,err=0,printout='latex'):
        '''
        val = the value (number or errval (see below))
        err = the corresponding error
        printout = what 'print' should look like

        if you initiate with val=errval a copy of that input errval is provided
        if you then want to change the printout value for that copy you have
        two choices:
            (1) a posteriori with .printout('mynewopt')
            (2) by specifying the new option with a !bang:
                cp = errval(orig,printout='mynewopt!')
        '''
        if isinstance(val,errval): # return copy
            self.__val = val.val()
            self.__assign_err(val.err())
            self.__printout = val.printout()
            if printout.endswith('!'): self.__printout = printout[:-1] # forget the !
        elif ( isinstance(val,tuple) and len(val)==2
               and isinstance(val[0],(int,float,long)) and isinstance(val[1],(int,float,long)) ):
            self.__val = val[0]
            self.__assign_err(val[1])
            self.__printout = printout
        elif ( isinstance(val,tuple) and len(val)==3
               and isinstance(val[0],(int,float,long)) and isinstance(val[1],(int,float,long))
               and isinstance(val[2],str) ):
            self.__val = val[0]
            self.__assign_err(val[1])
            self.__printout = val[2]
        elif isinstance(val,(int,float,long)):
            self.__val = val
            self.__assign_err(err)
            self.__printout = printout
        else:
            raise ValueError, 'Cannot assign input data'

    def val(self):
        return self.__val
    def err(self):
        return self.__err
    def v(self):
        return self.__val
    def e(self):
        return self.__err
    def printout(self,change=''):
        if change!='':
            self.__printout = change
        return self.__printout

    def __str__(self):
        '''
        called by print
        output depends on initialization
        when two errvals are put together the result has the value of the left one
        '''
        if self.__printout == '+-':
            return "{0} +- {1}".format(self.__val,self.__err)
        if self.__printout == 'cp': # make it easier to copy paste...
            return "errval({0},{1})".format(self.val(),self.err())
        if self.__printout == 'cpp': # make it easier to copy paste...
            return "errval({0},{1},errvalmode)".format(self.val(),self.err())
        else: # default = latex
            return "{0} \pm {1}".format(self.__val,self.__err)

    def __assign_err(self,err):
        if err<0: raise ValueError, 'Cannot assign negative error'
        else: self.__err = err

    def __add__(self,other):
        if isinstance(other,errval):
            nval = self.val() + other.val()
            nerr = np.sqrt( self.err()**2 + other.err()**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() + other
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for +: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __radd__(self,other):
        return self.__add__(other)

    def __sub__(self,other):
        if isinstance(other,errval):
            nval = self.val() - other.val()
            nerr = np.sqrt( self.err()**2 + other.err()**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() - other
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for -: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rsub__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other - self.val()
            nerr = self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for -: {0} with errval'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __mul__(self,other):
        if isinstance(other,errval):
            nval = self.val() * other.val()
            nerr = np.sqrt( (other.val()*self.err())**2 + (self.val()*other.err())**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() * other
            nerr = self.err() * abs(other)
        else:
            raise TypeError, 'unsupported operand type(s) for *: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rmul__(self,other):
        return self.__mul__(other)

    def __div__(self,other):
        if isinstance(other,errval):
            nval = self.val() *1.0 / other.val()
            nerr = np.sqrt( (1.0/other.val()*self.err())**2 + (self.val()*1.0/(other.val()**2)*other.err())**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() *1.0 / other
            nerr = self.err() *1.0/ abs(other)
        else:
            raise TypeError, 'unsupported operand type(s) for /: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rdiv__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other *1.0/self.val()
            nerr = abs(other)*1.0/self.val()**2 * self.err()
        else:
            raise TypeError, 'unsupported operand type(s) for /: {0} with errval'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __pow__(self,other):
        if isinstance(other,errval):
            nval = self.val() ** other.val()
            nerr = np.sqrt( ( other.val() * self.val()**(other.val()-1) * self.err() )**2
                            + ( np.log(self.val()) * self.val()**other.val() * other.err() )**2 )
        elif isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = self.val() ** other
            nerr = abs( other * self.val()**(other-1) * self.err() )
        else:
            raise TypeError, 'unsupported operand type(s) for **: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)
    def __rpow__(self,other):
        if isinstance(other,(int,float,long)):
            # a value with zero error attached
            nval = other ** self.val()
            nerr = abs( np.log(other) * other**self.val() * self.err() )
        else:
            raise TypeError, 'unsupported operand type(s) for **: errval with {0}'.format(type(other))
        return errval(nval, nerr, self.__printout)

    def __abs__(self):
        return errval(abs(self.val()), self.err(), self.printout())

    def sqrt(self):
        return self**0.5

    def round(self,n=0):
        # returns new instance
        return errval(np.around(self.val(),n),np.around(self.err(),n),self.printout())

<|endoftext|>