<commit_before># -*- coding: utf-8 -*-

import pprint
import os
import logging as logger

from .graph import Graph

def coarse_type(n):

    if n.isdigit():
        return int(n)
    return n

def _nest_flattened_dict(graph_dict, sep='.'):
    """
    Convert a dictionary that has been flattened by the
    `_flatten_nested_dict` method to a nested representation

    :param graph_dict: dictionary to nest
    :type graph_dict:  dict
    :param sep:        concatenation seperator
    :type sep:         str

    :return:           nested dictionary
    :rtype:            dict
    """

    nested_dict = {}
    for key, value in sorted(graph_dict.items()):

        splitted = key.split(sep)
        if len(splitted) == 1:
            nested_dict[key] = value

        d = nested_dict
        for k in splitted[:-1]:
            if k not in d:
                d[k] = {}
            d = d[k]

        d[splitted[-1]] = value

    return nested_dict

def dict_to_graph(dictionary, graph, keystring='key', valuestring='value'):

    assert isinstance(dictionary, dict), TypeError("Default configuration needs to be a dictionary type, got: {0}".format(type(dictionary)))

    graph.node_data_tag = keystring
    rootnid = graph.add_node('config')
    graph.root = rootnid

    def _walk_dict(key, item, rootnid):

        nid = graph.add_node(key)
        graph.add_edge(rootnid, nid)

        if isinstance(item, dict):
            for k, v in sorted(item.items()):
                _walk_dict(k, v, nid)
        else:
            graph.nodes[nid][valuestring] = item

    for k, v in sorted(dictionary.items()):
        _walk_dict(k, v, rootnid)

    return graph

def read_tgf(tgf, edge_data_label='label'):
    """
    Read graph in Trivial Graph Format

    TGF format dictates that nodes to be listed in the file first
    with each node on a new line. A '#' character signals the end
    of the node list and the start of the edge list.

    Node and edge ID's can be integers, float or strings.
    They are mapped against the Graphs internal node ID (nid).
    If edges connect nodes that do not exist, an error is logged
    and the edge is skipped.

    :param tgf: TGF graph data.
    :type tgf: File, string, stream or URL
    :param edge_data_label: Graph edge metadata dictionary key to
           use for parsed edge labels. 'label' by default.
    :type edge_data_label: string
    :return: Graph object
    """

    with open(tgf) as tgf_file:

        nodes = True
        node_dict = {}

        # Initiate new empty graph
        graph = Graph()

        # Start parsing file lines. First extract nodes
        for line in tgf_file.readlines():

            line = line.strip()
            if len(line):

                # Reading '#' character means switching from node
                # definition to edges
                if line.startswith('#'):
                    nodes = False
                    continue

                # Coarse string to types
                line = [coarse_type(n) for n in line.split()]

                # Parse nodes
                if nodes:
                    if len(line) > 1:
                        attr = ' '.join([repr(e) for e in line[1:]])
                        nid = graph.add_node(line[0], tgf=attr)
                    else:
                        nid = graph.add_node(line[0])
                    node_dict[line[0]] = nid

                # Parse edges
                else:
                    e1 = node_dict[line[0]]
                    e2 = node_dict[line[1]]
                    for node in (e1, e2):
                        if node not in graph.nodes:
                            logger.error('Node {0} in edge {1}-{2} not in graph. Skipping edge'.format(node, e1, e2))
                            continue

                    attr = None
                    if len(line) > 2:
                        attr = {edge_data_label: ' '.join([str(e) for e in line[2:]])}
                    graph.add_edge(e1, e2, attr=attr)

    return graph

def write_graph(graph, path=os.path.join(os.getcwd(), 'graph.gpf'), pickle=False):
    """
    Export graph as Graph Python Format file

    GPF format is the modules own file format consisting out of a serialized
    or pickled nodes and edges dictionary.
    The format is feature rich wth good performance but is not portable.

    :param graph:  Graph object to export
    :type graph:  Graph instance
    :param path:   File path to write to
    :type path:   path as string
    :param pickle: export graph as pickled GPF format
    :type pickle: bool
    :return: Graph instance
    """

    # Export graph as pickled Graph Python Format
    if pickle:

        import pickle
        pickle_dict = {}
        pickle_dict['nodes'] = graph.nodes.dict()
        pickle_dict['edges'] = graph.edges.dict()

        with open(path, "wb") as output:
            pickle.dump(pickle_dict, output)

        logger.info('Graph exported in pickled GPF format to file: {0}'.format(path))

    # Export graph as serialized Graph Python Format
    else:

        pp = pprint.PrettyPrinter(indent=2)
        with open(path, 'w') as output:

            # Export nodes as dictionary
            output.write('nodes = {0}\n'.format(pp.pformat(graph.nodes.dict())))

            # Export edges as dictionary
            output.write('edges = {0}\n'.format(pp.pformat(graph.edges.dict())))

        logger.info('Graph exported in GPF format to file: {0}'.format(path))

def read_graph(graph_file, graph=None, pickle=False):
    """
    Import graph from Graph Python Format file

    GPF format is the modules own file format consisting out of a serialized
    or pickled nodes and edges dictionary.
    The format is feature rich wth good performance but is not portable.

    :param path:   File path to read from
    :type path:   path as string
    :param graph:  Graph object to import to or Graph by default
    :type graph:  Graph instance
    :param pickle: Import graph as pickled GPF format
    :type pickle: bool
    :return: Graph instance
    """

    if not graph:
        graph = Graph()

    # Import graph from pickled Graph Python Format
    if pickle:
        import pickle

        with open(graph_file, mode='rb') as graph_file:
            pickle_dict = pickle.load(graph_file)

            if 'nodes' in pickle_dict and 'edges' in pickle_dict:
                graph.nodes._storage = pickle_dict['nodes']
                graph.edges._storage = pickle_dict['edges']

            adjacency = dict([(n, []) for n in pickle_dict['nodes']])
            for edge in pickle_dict['edges']:
                adjacency[edge[0]].append(edge[1])

            graph.adjacency._storage = adjacency

        logger.info('Graph imported from pickled GPF format file: {0}'.format(graph_file.name))

    # Import graph from serialized Graph Python Format
    else:
        with open(graph_file) as f:
            code = compile(f.read(), "GPF_file", 'exec')
            exec(code, global_vars, local_vars)

    return graph
<commit_msg>Move functions to io submodule<commit_after><|endoftext|>


<commit_before>#  Copyright 2008-2013 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import inspect

from robot.utils import NormalizedDict

class _RunKeywordRegister:

    def __init__(self):
        self._libs = {}

    def register_run_keyword(self, libname, keyword, args_to_process=None):
        if args_to_process is None:
            args_to_process = self._get_args_from_method(keyword)
            keyword = keyword.__name__
        if libname not in self._libs:
            self._libs[libname] = NormalizedDict(ignore=['_'])
        self._libs[libname][keyword] = int(args_to_process)

    def get_args_to_process(self, libname, kwname):
        if libname in self._libs and kwname in self._libs[libname]:
            return self._libs[libname][kwname]
        return -1

    def is_run_keyword(self, libname, kwname):
        return self.get_args_to_process(libname, kwname) >= 0

    def _get_args_from_method(self, method):
        if inspect.ismethod(method):
            return method.im_func.func_code.co_argcount - 1
        elif inspect.isfunction(method):
            return method.func_code.co_argcount
        raise ValueError('Needs function or method')

RUN_KW_REGISTER = _RunKeywordRegister()
<commit_msg>[python3] Additional workaround for missing unbound methods in Python 3<commit_after>#  Copyright 2008-2013 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import inspect

from robot.utils import NormalizedDict

class _RunKeywordRegister:

    def __init__(self):
        self._libs = {}

    def register_run_keyword(self, libname, keyword, args_to_process=None):
        if args_to_process is None:
            args_to_process = self._get_args_from_method(keyword)
            keyword = keyword.__name__
        if libname not in self._libs:
            self._libs[libname] = NormalizedDict(ignore=['_'])
        self._libs[libname][keyword] = int(args_to_process)

    def get_args_to_process(self, libname, kwname):
        if libname in self._libs and kwname in self._libs[libname]:
            return self._libs[libname][kwname]
        return -1

    def is_run_keyword(self, libname, kwname):
        return self.get_args_to_process(libname, kwname) >= 0

    def _get_args_from_method(self, method):
        # Python 3 has no unbound methods, they are just functions,
        # so ismethod won't be True...
        if inspect.ismethod(method):
            return method.im_func.func_code.co_argcount - 1
        elif inspect.isfunction(method):
            code = method.__code__
            argcount = code.co_argcount
            # ...but you can look at the args:
            #TODO: Better solution?
            if argcount and code.co_varnames[0] == 'self':
                argcount -= 1
            return argcount
        raise ValueError('Needs function or method')

RUN_KW_REGISTER = _RunKeywordRegister()
<|endoftext|>


<commit_before>from django.contrib import admin
from cropduster.models import Size, SizeSet
from django.conf import settings

#ADMIN_MEDIA_PREFIX = getattr(settings, "STANDARD_ADMIN_MEDIA_PREFIX", settings.ADMIN_MEDIA_PREFIX)

class SizeInline(admin.TabularInline):
	model = Size
	prepopulated_fields = {"slug" : ("name",)}

	fieldsets = (
		(None, {
			"fields": (
				"name",
				"slug",
				"width",
				"height",
				"auto_size",
				"size_set",
				"aspect_ratio",
				"create_on_request",
				"retina",
			)
		}),
	)
	readonly_fields = ("aspect_ratio",)

class SizeSetAdmin(admin.ModelAdmin):
	prepopulated_fields = {"slug" : ("name",)}

	inlines = [
		SizeInline,
	]

	class Media:

		js = (
			settings.STATIC_URL + "admin/cropduster/js/size_set.js",
		)
		css = {
			"all": (
				settings.STATIC_URL + "admin/cropduster/css/size_set.css",
			)
		}

admin.site.register(SizeSet, SizeSetAdmin)<commit_msg>removing commented code<commit_after>from django.contrib import admin
from cropduster.models import Size, SizeSet
from django.conf import settings

class SizeInline(admin.TabularInline):
	model = Size
	prepopulated_fields = {"slug" : ("name",)}

	fieldsets = (
		(None, {
			"fields": (
				"name",
				"slug",
				"width",
				"height",
				"auto_size",
				"size_set",
				"aspect_ratio",
				"create_on_request",
				"retina",
			)
		}),
	)
	readonly_fields = ("aspect_ratio",)

class SizeSetAdmin(admin.ModelAdmin):
	prepopulated_fields = {"slug" : ("name",)}

	inlines = [
		SizeInline,
	]

	class Media:

		js = (
			settings.STATIC_URL + "admin/cropduster/js/size_set.js",
		)
		css = {
			"all": (
				settings.STATIC_URL + "admin/cropduster/css/size_set.css",
			)
		}

admin.site.register(SizeSet, SizeSetAdmin)<|endoftext|>


<commit_before>"""max temp before jul 1 or min after"""
import datetime

import psycopg2.extras
import numpy as np
import pandas as pd
from pyiem.network import Table as NetworkTable
from pyiem.util import get_autoplot_context, get_dbconn

PDICT = {'fall': 'Minimum Temperature after 1 July',
         'spring': 'Maximum Temperature before 1 July'}
PDICT2 = {'high': 'High Temperature',
          'low': 'Low Temperature'}

def get_description():
    """ Return a dict describing how to call this plotter """
    desc = dict()
    desc['data'] = True
    desc['highcharts'] = True
    desc['description'] = """This plot presents the climatology and actual
    year's progression of warmest to date or coldest to date temperature.
    The simple average is presented along with the percentile intervals."""
    desc['arguments'] = [
        dict(type='station', name='station', default='IA0200',
             label='Select Station:', network='IACLIMATE'),
        dict(type='year', name='year', default=datetime.datetime.now().year,
             label='Year to Highlight:'),
        dict(type='select', name='half', default='fall',
             label='Option to Plot:', options=PDICT),
        dict(type='select', name='var', default='low',
             label='Variable to Plot:', options=PDICT2),
        dict(type='int', name='t', label='Highlight Temperature',
             default=32),
    ]
    return desc

def get_context(fdict):
    """ Get the raw infromations we need"""
    pgconn = get_dbconn('coop')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    today = datetime.date.today()
    thisyear = today.year
    ctx = get_autoplot_context(fdict, get_description())
    year = ctx['year']
    station = ctx['station']
    varname = ctx['var']
    half = ctx['half']
    table = "alldata_%s" % (station[:2],)
    nt = NetworkTable("%sCLIMATE" % (station[:2],))

    startyear = int(nt.sts[station]['archive_begin'].year)
    data = np.ma.ones((thisyear-startyear+1, 366)) * 199
    if half == 'fall':
        cursor.execute("""SELECT extract(doy from day), year,
            """ + varname + """ from
            """+table+""" WHERE station = %s and low is not null and
            high is not null and year >= %s""", (station, startyear))
    else:
        cursor.execute("""SELECT extract(doy from day), year,
            """ + varname + """ from
            """+table+""" WHERE station = %s and high is not null and
            low is not null and year >= %s""", (station, startyear))
    for row in cursor:
        data[int(row[1] - startyear), int(row[0] - 1)] = row[2]

    data.mask = np.where(data == 199, True, False)

    doys = []
    avg = []
    p25 = []
    p2p5 = []
    p75 = []
    p97p5 = []
    mins = []
    maxs = []
    dyear = []
    idx = year - startyear
    last_doy = int(today.strftime("%j"))
    if half == 'fall':
        for doy in range(181, 366):
            low = np.ma.min(data[:-1, 180:doy], 1)
            avg.append(np.ma.average(low))
            mins.append(np.ma.min(low))
            maxs.append(np.ma.max(low))
            p = np.percentile(low, [2.5, 25, 75, 97.5])
            p2p5.append(p[0])
            p25.append(p[1])
            p75.append(p[2])
            p97p5.append(p[3])
            doys.append(doy)
            if year == thisyear and doy > last_doy:
                continue
            dyear.append(np.ma.min(data[idx, 180:doy]))
    else:
        for doy in range(1, 181):
            low = np.ma.max(data[:-1, :doy], 1)
            avg.append(np.ma.average(low))
            mins.append(np.ma.min(low))
            maxs.append(np.ma.max(low))
            p = np.percentile(low, [2.5, 25, 75, 97.5])
            p2p5.append(p[0])
            p25.append(p[1])
            p75.append(p[2])
            p97p5.append(p[3])
            doys.append(doy)
            if year == thisyear and doy > last_doy:
                continue
            dyear.append(np.ma.max(data[idx, :doy]))

    # http://stackoverflow.com/questions/19736080
    d = dict(doy=pd.Series(doys), mins=pd.Series(mins), maxs=pd.Series(maxs),
             p2p5=pd.Series(p2p5),
             p97p5=pd.Series(p97p5), p25=pd.Series(p25),
             p75=pd.Series(p75), avg=pd.Series(avg),
             thisyear=pd.Series(dyear))
    df = pd.DataFrame(d)
    sts = datetime.date(2000, 1, 1) + datetime.timedelta(days=doys[0]-1)
    df['dates'] = pd.date_range(sts, periods=len(doys))
    df.set_index('doy', inplace=True)
    ctx['df'] = df
    ctx['year'] = year
    ctx['half'] = half
    if ctx['half'] == 'fall':
        title = "Minimum Daily %s Temperature after 1 July"
    else:
        title = "Maximum Daily %s Temperature before 1 July"
    title = title % (varname.capitalize(), )
    ctx['ylabel'] = title
    ctx['title'] = "%s-%s %s %s\n%s" % (startyear,
                                        thisyear-1, station,
                                        nt.sts[station]['name'], title)
    return ctx

def highcharts(fdict):
    """ Do highcharts """
    ctx = get_context(fdict)

    rng = ctx['df'][['dates', 'mins', 'maxs']].to_json(orient='values')
    p95 = ctx['df'][['dates', 'p2p5', 'p97p5']].to_json(orient='values')
    p50 = ctx['df'][['dates', 'p25', 'p75']].to_json(orient='values')
    mean = ctx['df'][['dates', 'avg']].to_json(orient='values')
    thisyear = ctx['df'][['dates', 'thisyear']].to_json(orient='values')
    return """
$("#ap_container").highcharts({
    title: {text: '""" + ctx['title'].replace("\n", " ") + """'},
    tooltip: {shared: true,
        xDateFormat: '%B %d'},
    xAxis: {type: 'datetime',
        dateTimeLabelFormats: {
            day: '%b %e',
            week: '%b %e',
            month: '%b %e'}},
    yAxis: {title: {text: '"""+ctx['ylabel']+"""'}},
    series: [{
        name: 'Range',
        type: 'arearange',
        color: 'pink',
        tooltip: {valueDecimals: 0},
        data: """+rng+"""
    },{
        name: '95th',
        type: 'arearange',
        color: 'tan',
        tooltip: {valueDecimals: 2},
        data: """+p95+"""
    },{
        name: '50th',
        type: 'arearange',
        color: 'gold',
        tooltip: {valueDecimals: 2},
        data: """+p50+"""
    },{
        name: 'Average',
        type: 'line',
        color: 'black',
        tooltip: {valueDecimals: 2},
        data: """+mean+"""
    },{
        name: '"""+str(ctx['year'])+"""',
        type: 'line',
        color: 'blue',
        tooltip: {valueDecimals: 0},
        shadow: {'color': 'white'},
        data: """+thisyear+"""
    }]
});
    """

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    ctx = get_context(fdict)
    df = ctx['df']

    (fig, ax) = plt.subplots(1, 1)
    doys = df.index.values
    ax.fill_between(doys, df['mins'], df['maxs'], color='pink',
                    zorder=1, label='Range')
    ax.fill_between(doys, df['p2p5'], df['p97p5'],
                    color='tan', zorder=2, label='95 tile')
    ax.fill_between(doys, df['p25'], df['p75'],
                    color='gold', zorder=3, label='50 tile')
    a = ax.plot(doys, df['avg'], zorder=4, color='k', lw=2,
                label='Average')
    series = df['thisyear'].dropna()
    ax.plot(series.index.values, series.values, color='white',
            lw=3.5, zorder=5)
    b = ax.plot(series.index.values, series.values, color='b', lw=1.5,
                zorder=6, label='%s' % (ctx['year'],))
    ax.set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365))
    ax.set_xticklabels(('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
                        'Sep', 'Oct', 'Nov', 'Dec'))
    if ctx['half'] == 'fall':
        ax.set_xlim(200, 366)
        ax.text(220, 32.4, r'32$^\circ$F', ha='left')
    else:
        ax.set_xlim(0, 181)
    ax.set_ylabel(r"%s $^\circ$F" % (ctx['ylabel'], ))
    ax.set_title(ctx['title'])
    ax.axhline(ctx['t'], linestyle='--', lw=1, color='k', zorder=6)
    ax.text(ax.get_xlim()[1], ctx['t'], r"%.0f$^\circ$F" % (ctx['t'], ),
            va='center')
    ax.grid(True)

    from matplotlib.patches import Rectangle
    r = Rectangle((0, 0), 1, 1, fc='pink')
    r2 = Rectangle((0, 0), 1, 1, fc='tan')
    r3 = Rectangle((0, 0), 1, 1, fc='gold')

    loc = 1 if ctx['half'] == 'fall' else 4
    ax.legend([r, r2, r3, a[0], b[0]], ['Range', '95$^{th}$ %tile',
                                        '50$^{th}$ %tile', 'Average',
                                        '%s' % (ctx['year'],)], loc=loc)

    return fig, df

if __name__ == '__main__':
    highcharts({'year': 2016, 'half': 'spring', 'var': 'high'})
<commit_msg>remove hard coded 32F on autoplot 26<commit_after>"""max temp before jul 1 or min after"""
import datetime

import psycopg2.extras
import numpy as np
import pandas as pd
from matplotlib.patches import Rectangle
from pyiem.network import Table as NetworkTable
from pyiem.plot.use_agg import plt
from pyiem.util import get_autoplot_context, get_dbconn

PDICT = {'fall': 'Minimum Temperature after 1 July',
         'spring': 'Maximum Temperature before 1 July'}
PDICT2 = {'high': 'High Temperature',
          'low': 'Low Temperature'}

def get_description():
    """ Return a dict describing how to call this plotter """
    desc = dict()
    desc['data'] = True
    desc['highcharts'] = True
    desc['description'] = """This plot presents the climatology and actual
    year's progression of warmest to date or coldest to date temperature.
    The simple average is presented along with the percentile intervals."""
    desc['arguments'] = [
        dict(type='station', name='station', default='IA0200',
             label='Select Station:', network='IACLIMATE'),
        dict(type='year', name='year', default=datetime.datetime.now().year,
             label='Year to Highlight:'),
        dict(type='select', name='half', default='fall',
             label='Option to Plot:', options=PDICT),
        dict(type='select', name='var', default='low',
             label='Variable to Plot:', options=PDICT2),
        dict(type='int', name='t', label='Highlight Temperature',
             default=32),
    ]
    return desc

def get_context(fdict):
    """ Get the raw infromations we need"""
    pgconn = get_dbconn('coop')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    today = datetime.date.today()
    thisyear = today.year
    ctx = get_autoplot_context(fdict, get_description())
    year = ctx['year']
    station = ctx['station']
    varname = ctx['var']
    half = ctx['half']
    table = "alldata_%s" % (station[:2],)
    nt = NetworkTable("%sCLIMATE" % (station[:2],))

    startyear = int(nt.sts[station]['archive_begin'].year)
    data = np.ma.ones((thisyear-startyear+1, 366)) * 199
    if half == 'fall':
        cursor.execute("""SELECT extract(doy from day), year,
            """ + varname + """ from
            """+table+""" WHERE station = %s and low is not null and
            high is not null and year >= %s""", (station, startyear))
    else:
        cursor.execute("""SELECT extract(doy from day), year,
            """ + varname + """ from
            """+table+""" WHERE station = %s and high is not null and
            low is not null and year >= %s""", (station, startyear))
    for row in cursor:
        data[int(row[1] - startyear), int(row[0] - 1)] = row[2]

    data.mask = np.where(data == 199, True, False)

    doys = []
    avg = []
    p25 = []
    p2p5 = []
    p75 = []
    p97p5 = []
    mins = []
    maxs = []
    dyear = []
    idx = year - startyear
    last_doy = int(today.strftime("%j"))
    if half == 'fall':
        for doy in range(181, 366):
            low = np.ma.min(data[:-1, 180:doy], 1)
            avg.append(np.ma.average(low))
            mins.append(np.ma.min(low))
            maxs.append(np.ma.max(low))
            p = np.percentile(low, [2.5, 25, 75, 97.5])
            p2p5.append(p[0])
            p25.append(p[1])
            p75.append(p[2])
            p97p5.append(p[3])
            doys.append(doy)
            if year == thisyear and doy > last_doy:
                continue
            dyear.append(np.ma.min(data[idx, 180:doy]))
    else:
        for doy in range(1, 181):
            low = np.ma.max(data[:-1, :doy], 1)
            avg.append(np.ma.average(low))
            mins.append(np.ma.min(low))
            maxs.append(np.ma.max(low))
            p = np.percentile(low, [2.5, 25, 75, 97.5])
            p2p5.append(p[0])
            p25.append(p[1])
            p75.append(p[2])
            p97p5.append(p[3])
            doys.append(doy)
            if year == thisyear and doy > last_doy:
                continue
            dyear.append(np.ma.max(data[idx, :doy]))

    # http://stackoverflow.com/questions/19736080
    d = dict(doy=pd.Series(doys), mins=pd.Series(mins), maxs=pd.Series(maxs),
             p2p5=pd.Series(p2p5),
             p97p5=pd.Series(p97p5), p25=pd.Series(p25),
             p75=pd.Series(p75), avg=pd.Series(avg),
             thisyear=pd.Series(dyear))
    df = pd.DataFrame(d)
    sts = datetime.date(2000, 1, 1) + datetime.timedelta(days=doys[0]-1)
    df['dates'] = pd.date_range(sts, periods=len(doys))
    df.set_index('doy', inplace=True)
    ctx['df'] = df
    ctx['year'] = year
    ctx['half'] = half
    if ctx['half'] == 'fall':
        title = "Minimum Daily %s Temperature after 1 July"
    else:
        title = "Maximum Daily %s Temperature before 1 July"
    title = title % (varname.capitalize(), )
    ctx['ylabel'] = title
    ctx['title'] = "%s-%s %s %s\n%s" % (startyear,
                                        thisyear-1, station,
                                        nt.sts[station]['name'], title)
    return ctx

def highcharts(fdict):
    """ Do highcharts """
    ctx = get_context(fdict)

    rng = ctx['df'][['dates', 'mins', 'maxs']].to_json(orient='values')
    p95 = ctx['df'][['dates', 'p2p5', 'p97p5']].to_json(orient='values')
    p50 = ctx['df'][['dates', 'p25', 'p75']].to_json(orient='values')
    mean = ctx['df'][['dates', 'avg']].to_json(orient='values')
    thisyear = ctx['df'][['dates', 'thisyear']].to_json(orient='values')
    return """
$("#ap_container").highcharts({
    title: {text: '""" + ctx['title'].replace("\n", " ") + """'},
    tooltip: {shared: true,
        xDateFormat: '%B %d'},
    xAxis: {type: 'datetime',
        dateTimeLabelFormats: {
            day: '%b %e',
            week: '%b %e',
            month: '%b %e'}},
    yAxis: {title: {text: '"""+ctx['ylabel']+"""'}},
    series: [{
        name: 'Range',
        type: 'arearange',
        color: 'pink',
        tooltip: {valueDecimals: 0},
        data: """+rng+"""
    },{
        name: '95th',
        type: 'arearange',
        color: 'tan',
        tooltip: {valueDecimals: 2},
        data: """+p95+"""
    },{
        name: '50th',
        type: 'arearange',
        color: 'gold',
        tooltip: {valueDecimals: 2},
        data: """+p50+"""
    },{
        name: 'Average',
        type: 'line',
        color: 'black',
        tooltip: {valueDecimals: 2},
        data: """+mean+"""
    },{
        name: '"""+str(ctx['year'])+"""',
        type: 'line',
        color: 'blue',
        tooltip: {valueDecimals: 0},
        shadow: {'color': 'white'},
        data: """+thisyear+"""
    }]
});
    """

def plotter(fdict):
    """ Go """
    ctx = get_context(fdict)
    df = ctx['df']

    (fig, ax) = plt.subplots(1, 1)
    doys = df.index.values
    ax.fill_between(doys, df['mins'], df['maxs'], color='pink',
                    zorder=1, label='Range')
    ax.fill_between(doys, df['p2p5'], df['p97p5'],
                    color='tan', zorder=2, label='95 tile')
    ax.fill_between(doys, df['p25'], df['p75'],
                    color='gold', zorder=3, label='50 tile')
    a = ax.plot(doys, df['avg'], zorder=4, color='k', lw=2,
                label='Average')
    series = df['thisyear'].dropna()
    ax.plot(series.index.values, series.values, color='white',
            lw=3.5, zorder=5)
    b = ax.plot(series.index.values, series.values, color='b', lw=1.5,
                zorder=6, label='%s' % (ctx['year'],))
    ax.set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365))
    ax.set_xticklabels(('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
                        'Sep', 'Oct', 'Nov', 'Dec'))
    if ctx['half'] == 'fall':
        ax.set_xlim(200, 366)
    else:
        ax.set_xlim(0, 181)
    ax.set_ylabel(r"%s $^\circ$F" % (ctx['ylabel'], ))
    ax.set_title(ctx['title'])
    ax.axhline(ctx['t'], linestyle='--', lw=1, color='k', zorder=6)
    ax.text(ax.get_xlim()[1], ctx['t'], r"%.0f$^\circ$F" % (ctx['t'], ),
            va='center')
    ax.grid(True)

    r = Rectangle((0, 0), 1, 1, fc='pink')
    r2 = Rectangle((0, 0), 1, 1, fc='tan')
    r3 = Rectangle((0, 0), 1, 1, fc='gold')

    loc = 1 if ctx['half'] == 'fall' else 4
    ax.legend([r, r2, r3, a[0], b[0]], ['Range', '95$^{th}$ %tile',
                                        '50$^{th}$ %tile', 'Average',
                                        '%s' % (ctx['year'],)], loc=loc)

    return fig, df

if __name__ == '__main__':
    highcharts({'year': 2016, 'half': 'spring', 'var': 'high'})
<|endoftext|>


<commit_before># Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os

from oslo_log import log
import yaml

LOG = log.getLogger(__name__)

def load_files(dir_path,
               suffix=None,
               with_pathname=False,
               with_exception=False):
    try:
        loaded_files = os.listdir(dir_path)
    except Exception as e:
        if with_exception:
            raise e
        else:
            return []
    if suffix:
        loaded_files = [dir_path + '/' + f if with_pathname else f
                        for f in loaded_files if f.endswith(suffix)]

    return loaded_files

def load_yaml_files(dir_path, with_exception=False):
    files = load_files(dir_path, '.yaml', with_pathname=True)

    yaml_files = []
    for f in files:
        config = load_yaml_file(f, with_exception)
        if config:
            yaml_files.append(config)

    return yaml_files

def load_yaml_file(full_path, with_exception=False):
    with open(full_path, 'r') as stream:
        try:
            return yaml.load(stream, Loader=yaml.BaseLoader)
        except Exception as e:
            if with_exception:
                raise e
            else:
                LOG.error("Fails to parse file: %s. %s" % (full_path, e))
                return None
<commit_msg>Correct reraising of exception<commit_after># Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os

from oslo_log import log
import yaml

LOG = log.getLogger(__name__)

def load_files(dir_path,
               suffix=None,
               with_pathname=False,
               with_exception=False):
    try:
        loaded_files = os.listdir(dir_path)
    except Exception:
        if with_exception:
            raise
        else:
            return []
    if suffix:
        loaded_files = [dir_path + '/' + f if with_pathname else f
                        for f in loaded_files if f.endswith(suffix)]

    return loaded_files

def load_yaml_files(dir_path, with_exception=False):
    files = load_files(dir_path, '.yaml', with_pathname=True)

    yaml_files = []
    for f in files:
        config = load_yaml_file(f, with_exception)
        if config:
            yaml_files.append(config)

    return yaml_files

def load_yaml_file(full_path, with_exception=False):
    with open(full_path, 'r') as stream:
        try:
            return yaml.load(stream, Loader=yaml.BaseLoader)
        except Exception as e:
            if with_exception:
                raise
            else:
                LOG.error("Fails to parse file: %s. %s" % (full_path, e))
                return None
<|endoftext|>


<commit_before>import re
from functools import update_wrapper
from django import http, template
from django.contrib.admin import ModelAdmin, actions
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.contenttypes import views as contenttype_views
from django.views.decorators.csrf import csrf_protect
from django.db.models.base import ModelBase
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.template.response import TemplateResponse
from django.utils.functional import update_wrapper
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
from django.conf import settings

LOGIN_FORM_KEY = 'this_is_the_login_form'

class AlreadyRegistered(Exception):
    pass

class NotRegistered(Exception):
    pass

class AdminSite(object):
    """
    An AdminSite object encapsulates an instance of the Django admin application, ready
    to be hooked in to your URLconf. Models are registered with the AdminSite using the
    register() method, and the get_urls() method can then be used to access Django view
    functions that present a full admin interface for the collection of registered
    models.
    """
    login_form = None
    index_template = None
    app_index_template = None
    login_template = None
    logout_template = None
    password_change_template = None
    password_change_done_template = None

    def __init__(self, name=None, app_name='admin'):
        self._registry = {} # model_class class -> admin_class instance
        self.root_path = None
        if name is None:
            self.name = 'admin'
        else:
            self.name = name
        self.app_name = app_name
        self._actions = {'delete_selected': actions.delete_selected}
        self._global_actions = self._actions.copy()

    def register(self, model_or_iterable, admin_class=None, **options):
        """
        Registers the given model(s) with the given admin class.

        The model(s) should be Model classes, not instances.

        If an admin class isn't given, it will use ModelAdmin (the default
        admin options). If keyword arguments are given -- e.g., list_display --
        they'll be applied as options to the admin class.

        If a model is already registered, this will raise AlreadyRegistered.

        If a model is abstract, this will raise ImproperlyConfigured.
        """
        if not admin_class:
            admin_class = ModelAdmin

        # Don't import the humongous validation code unless required
        if admin_class and settings.DEBUG:
            from django.contrib.admin.validation import validate
        else:
            validate = lambda model, adminclass: None

        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model._meta.abstract:
                raise ImproperlyConfigured('The model %s is abstract, so it '
                      'cannot be registered with admin.' % model.__name__)

            if model in self._registry:
                raise AlreadyRegistered('The model %s is already registered' % model.__name__)

            # If we got **options then dynamically construct a subclass of
            # admin_class with those **options.
            if options:
                # For reasons I don't quite understand, without a __module__
                # the created class appears to "live" in the wrong place,
                # which causes issues later on.
                options['__module__'] = __name__
                admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)

            # Validate (which might be a no-op)
            validate(admin_class, model)

            # Instantiate the admin class to save in the registry
            self._registry[model] = admin_class(model, self)

    def unregister(self, model_or_iterable):
        """
        Unregisters the given model(s).

        If a model isn't already registered, this will raise NotRegistered.
        """
        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model not in self._registry:
                raise NotRegistered('The model %s is not registered' % model.__name__)
            del self._registry[model]

    def add_action(self, action, name=None):
        """
        Register an action to be available globally.
        """
        name = name or action.__name__
        self._actions[name] = action
        self._global_actions[name] = action

    def disable_action(self, name):
        """
        Disable a globally-registered action. Raises KeyError for invalid names.
        """
        del self._actions[name]

    def get_action(self, name):
        """
        Explicitally get a registered global action wheather it's enabled or
        not. Raises KeyError for invalid names.
        """
        return self._global_actions[name]

    @property
    def actions(self):
        """
        Get all the enabled actions as an iterable of (name, func).
        """
        return self._actions.iteritems()

    def has_permission(self, request):
        """
        Returns True if the given HttpRequest has permission to view
        *at least one* page in the admin site.
        """
        return request.user.is_active and request.user.is_staff

    def check_dependencies(self):
        """
        Check that all things needed to run the admin have been correctly installed.

        The default implementation checks that LogEntry, ContentType and the
        auth context processor are installed.
        """
        from django.contrib.admin.models import LogEntry
        from django.contrib.contenttypes.models import ContentType

        if not LogEntry._meta.installed:
            raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
                "INSTALLED_APPS setting in order to use the admin application.")
        if not ContentType._meta.installed:
            raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
                "your INSTALLED_APPS setting in order to use the admin application.")
        if not ('django.contrib.auth.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS or
            'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS):
            raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' "
                "in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")

    def admin_view(self, view, cacheable=False):
        """
        Decorator to create an admin view attached to this ``AdminSite``. This
        wraps the view and provides permission checking by calling
        ``self.has_permission``.

        You'll want to use this from within ``AdminSite.get_urls()``:

            class MyAdminSite(AdminSite):

                def get_urls(self):
                    from django.conf.urls.defaults import patterns, url

                    urls = super(MyAdminSite, self).get_urls()
                    urls += patterns('',
                        url(r'^my_view/$', self.admin_view(some_view))
                    )
                    return urls

        By default, admin_views are marked non-cacheable using the
        ``never_cache`` decorator. If the view can be safely cached, set
        cacheable=True.
        """
        def inner(request, *args, **kwargs):
            if not self.has_permission(request):
                return self.login(request)
            return view(request, *args, **kwargs)
        if not cacheable:
            inner = never_cache(inner)
        # We add csrf_protect here so this function can be used as a utility
        # function for any view, without having to repeat 'csrf_protect'.
        if not getattr(view, 'csrf_exempt', False):
            inner = csrf_protect(inner)
        return update_wrapper(inner, view)

    def get_urls(self):
        from django.conf.urls.defaults import patterns, url, include

        if settings.DEBUG:
            self.check_dependencies()

        def wrap(view, cacheable=False):
            def wrapper(*args, **kwargs):
                return self.admin_view(view, cacheable)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        # Admin-site-wide views.
        urlpatterns = patterns('',
            url(r'^$',
                wrap(self.index),
                name='index'),
            url(r'^logout/$',
                wrap(self.logout),
                name='logout'),
            url(r'^password_change/$',
                wrap(self.password_change, cacheable=True),
                name='password_change'),
            url(r'^password_change/done/$',
                wrap(self.password_change_done, cacheable=True),
                name='password_change_done'),
            url(r'^jsi18n/$',
                wrap(self.i18n_javascript, cacheable=True),
                name='jsi18n'),
            url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$',
                wrap(contenttype_views.shortcut)),
            url(r'^(?P<app_label>\w+)/$',
                wrap(self.app_index),
                name='app_list')
        )

        # Add in each model's views.
        for model, model_admin in self._registry.iteritems():
            urlpatterns += patterns('',
                url(r'^%s/%s/' % (model._meta.app_label, model._meta.module_name),
                    include(model_admin.urls))
            )
        return urlpatterns

    @property
    def urls(self):
        return self.get_urls(), self.app_name, self.name

    def password_change(self, request):
        """
        Handles the "change password" task -- both form display and validation.
        """
        from django.contrib.auth.views import password_change
        if self.root_path is not None:
            url = '%spassword_change/done/' % self.root_path
        else:
            url = reverse('admin:password_change_done', current_app=self.name)
        defaults = {
            'current_app': self.name,
            'post_change_redirect': url
        }
        if self.password_change_template is not None:
            defaults['template_name'] = self.password_change_template
        return password_change(request, **defaults)

    def password_change_done(self, request, extra_context=None):
        """
        Displays the "success" page after a password change.
        """
        from django.contrib.auth.views import password_change_done
        defaults = {
            'current_app': self.name,
            'extra_context': extra_context or {},
        }
        if self.password_change_done_template is not None:
            defaults['template_name'] = self.password_change_done_template
        return password_change_done(request, **defaults)

    def i18n_javascript(self, request):
        """
        Displays the i18n JavaScript that the Django admin requires.

        This takes into account the USE_I18N setting. If it's set to False, the
        generated JavaScript will be leaner and faster.
        """
        if settings.USE_I18N:
            from django.views.i18n import javascript_catalog
        else:
            from django.views.i18n import null_javascript_catalog as javascript_catalog
        return javascript_catalog(request, packages=['django.conf', 'django.contrib.admin'])

    @never_cache
    def logout(self, request, extra_context=None):
        """
        Logs out the user for the given HttpRequest.

        This should *not* assume the user is already logged in.
        """
        from django.contrib.auth.views import logout
        defaults = {
            'current_app': self.name,
            'extra_context': extra_context or {},
        }
        if self.logout_template is not None:
            defaults['template_name'] = self.logout_template
        return logout(request, **defaults)

    @never_cache
    def login(self, request, extra_context=None):
        """
        Displays the login form for the given HttpRequest.
        """
        from django.contrib.auth.views import login
        context = {
            'title': _('Log in'),
            'root_path': self.root_path,
            'app_path': request.get_full_path(),
            REDIRECT_FIELD_NAME: request.get_full_path(),
        }
        context.update(extra_context or {})
        defaults = {
            'extra_context': context,
            'current_app': self.name,
            'authentication_form': self.login_form or AdminAuthenticationForm,
            'template_name': self.login_template or 'admin/login.html',
        }
        return login(request, **defaults)

    @never_cache
    def index(self, request, extra_context=None):
        """
        Displays the main admin index page, which lists all of the installed
        apps that have been registered in this site.
        """
        app_dict = {}
        user = request.user
        for model, model_admin in self._registry.items():
            app_label = model._meta.app_label
            has_module_perms = user.has_module_perms(app_label)

            if has_module_perms:
                perms = model_admin.get_model_perms(request)

                # Check whether user has any perm for this module.
                # If so, add the module to the model_list.
                if True in perms.values():
                    model_dict = {
                        'name': capfirst(model._meta.verbose_name_plural),
                        'admin_url': mark_safe('%s/%s/' % (app_label, model.__name__.lower())),
                        'perms': perms,
                    }
                    if app_label in app_dict:
                        app_dict[app_label]['models'].append(model_dict)
                    else:
                        app_dict[app_label] = {
                            'name': app_label.title(),
                            'app_url': app_label + '/',
                            'has_module_perms': has_module_perms,
                            'models': [model_dict],
                        }

        # Sort the apps alphabetically.
        app_list = app_dict.values()
        app_list.sort(key=lambda x: x['name'])

        # Sort the models alphabetically within each app.
        for app in app_list:
            app['models'].sort(key=lambda x: x['name'])

        context = {
            'title': _('Site administration'),
            'app_list': app_list,
            'root_path': self.root_path,
        }
        context.update(extra_context or {})
        return TemplateResponse(request, [
            self.index_template or 'admin/index.html',
        ], context, current_app=self.name)

    def app_index(self, request, app_label, extra_context=None):
        user = request.user
        has_module_perms = user.has_module_perms(app_label)
        app_dict = {}
        for model, model_admin in self._registry.items():
            if app_label == model._meta.app_label:
                if has_module_perms:
                    perms = model_admin.get_model_perms(request)

                    # Check whether user has any perm for this module.
                    # If so, add the module to the model_list.
                    if True in perms.values():
                        model_dict = {
                            'name': capfirst(model._meta.verbose_name_plural),
                            'admin_url': '%s/' % model.__name__.lower(),
                            'perms': perms,
                        }
                        if app_dict:
                            app_dict['models'].append(model_dict),
                        else:
                            # First time around, now that we know there's
                            # something to display, add in the necessary meta
                            # information.
                            app_dict = {
                                'name': app_label.title(),
                                'app_url': '',
                                'has_module_perms': has_module_perms,
                                'models': [model_dict],
                            }
        if not app_dict:
            raise http.Http404('The requested admin page does not exist.')
        # Sort the models alphabetically within each app.
        app_dict['models'].sort(key=lambda x: x['name'])
        context = {
            'title': _('%s administration') % capfirst(app_label),
            'app_list': [app_dict],
            'root_path': self.root_path,
        }
        context.update(extra_context or {})

        return TemplateResponse(request, self.app_index_template or [
            'admin/%s/app_index.html' % app_label,
            'admin/app_index.html'
        ], context, current_app=self.name)

# This global object represents the default admin site, for the common case.
# You can instantiate AdminSite in your own code to create a custom admin site.
site = AdminSite()
<commit_msg>Cleaned up admin sites.py imports.<commit_after>from functools import update_wrapper
from django import http
from django.contrib.admin import ModelAdmin, actions
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.contenttypes import views as contenttype_views
from django.views.decorators.csrf import csrf_protect
from django.db.models.base import ModelBase
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.template.response import TemplateResponse
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
from django.conf import settings

LOGIN_FORM_KEY = 'this_is_the_login_form'

class AlreadyRegistered(Exception):
    pass

class NotRegistered(Exception):
    pass

class AdminSite(object):
    """
    An AdminSite object encapsulates an instance of the Django admin application, ready
    to be hooked in to your URLconf. Models are registered with the AdminSite using the
    register() method, and the get_urls() method can then be used to access Django view
    functions that present a full admin interface for the collection of registered
    models.
    """
    login_form = None
    index_template = None
    app_index_template = None
    login_template = None
    logout_template = None
    password_change_template = None
    password_change_done_template = None

    def __init__(self, name=None, app_name='admin'):
        self._registry = {} # model_class class -> admin_class instance
        self.root_path = None
        if name is None:
            self.name = 'admin'
        else:
            self.name = name
        self.app_name = app_name
        self._actions = {'delete_selected': actions.delete_selected}
        self._global_actions = self._actions.copy()

    def register(self, model_or_iterable, admin_class=None, **options):
        """
        Registers the given model(s) with the given admin class.

        The model(s) should be Model classes, not instances.

        If an admin class isn't given, it will use ModelAdmin (the default
        admin options). If keyword arguments are given -- e.g., list_display --
        they'll be applied as options to the admin class.

        If a model is already registered, this will raise AlreadyRegistered.

        If a model is abstract, this will raise ImproperlyConfigured.
        """
        if not admin_class:
            admin_class = ModelAdmin

        # Don't import the humongous validation code unless required
        if admin_class and settings.DEBUG:
            from django.contrib.admin.validation import validate
        else:
            validate = lambda model, adminclass: None

        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model._meta.abstract:
                raise ImproperlyConfigured('The model %s is abstract, so it '
                      'cannot be registered with admin.' % model.__name__)

            if model in self._registry:
                raise AlreadyRegistered('The model %s is already registered' % model.__name__)

            # If we got **options then dynamically construct a subclass of
            # admin_class with those **options.
            if options:
                # For reasons I don't quite understand, without a __module__
                # the created class appears to "live" in the wrong place,
                # which causes issues later on.
                options['__module__'] = __name__
                admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)

            # Validate (which might be a no-op)
            validate(admin_class, model)

            # Instantiate the admin class to save in the registry
            self._registry[model] = admin_class(model, self)

    def unregister(self, model_or_iterable):
        """
        Unregisters the given model(s).

        If a model isn't already registered, this will raise NotRegistered.
        """
        if isinstance(model_or_iterable, ModelBase):
            model_or_iterable = [model_or_iterable]
        for model in model_or_iterable:
            if model not in self._registry:
                raise NotRegistered('The model %s is not registered' % model.__name__)
            del self._registry[model]

    def add_action(self, action, name=None):
        """
        Register an action to be available globally.
        """
        name = name or action.__name__
        self._actions[name] = action
        self._global_actions[name] = action

    def disable_action(self, name):
        """
        Disable a globally-registered action. Raises KeyError for invalid names.
        """
        del self._actions[name]

    def get_action(self, name):
        """
        Explicitally get a registered global action wheather it's enabled or
        not. Raises KeyError for invalid names.
        """
        return self._global_actions[name]

    @property
    def actions(self):
        """
        Get all the enabled actions as an iterable of (name, func).
        """
        return self._actions.iteritems()

    def has_permission(self, request):
        """
        Returns True if the given HttpRequest has permission to view
        *at least one* page in the admin site.
        """
        return request.user.is_active and request.user.is_staff

    def check_dependencies(self):
        """
        Check that all things needed to run the admin have been correctly installed.

        The default implementation checks that LogEntry, ContentType and the
        auth context processor are installed.
        """
        from django.contrib.admin.models import LogEntry
        from django.contrib.contenttypes.models import ContentType

        if not LogEntry._meta.installed:
            raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
                "INSTALLED_APPS setting in order to use the admin application.")
        if not ContentType._meta.installed:
            raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
                "your INSTALLED_APPS setting in order to use the admin application.")
        if not ('django.contrib.auth.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS or
            'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS):
            raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' "
                "in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")

    def admin_view(self, view, cacheable=False):
        """
        Decorator to create an admin view attached to this ``AdminSite``. This
        wraps the view and provides permission checking by calling
        ``self.has_permission``.

        You'll want to use this from within ``AdminSite.get_urls()``:

            class MyAdminSite(AdminSite):

                def get_urls(self):
                    from django.conf.urls.defaults import patterns, url

                    urls = super(MyAdminSite, self).get_urls()
                    urls += patterns('',
                        url(r'^my_view/$', self.admin_view(some_view))
                    )
                    return urls

        By default, admin_views are marked non-cacheable using the
        ``never_cache`` decorator. If the view can be safely cached, set
        cacheable=True.
        """
        def inner(request, *args, **kwargs):
            if not self.has_permission(request):
                return self.login(request)
            return view(request, *args, **kwargs)
        if not cacheable:
            inner = never_cache(inner)
        # We add csrf_protect here so this function can be used as a utility
        # function for any view, without having to repeat 'csrf_protect'.
        if not getattr(view, 'csrf_exempt', False):
            inner = csrf_protect(inner)
        return update_wrapper(inner, view)

    def get_urls(self):
        from django.conf.urls.defaults import patterns, url, include

        if settings.DEBUG:
            self.check_dependencies()

        def wrap(view, cacheable=False):
            def wrapper(*args, **kwargs):
                return self.admin_view(view, cacheable)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        # Admin-site-wide views.
        urlpatterns = patterns('',
            url(r'^$',
                wrap(self.index),
                name='index'),
            url(r'^logout/$',
                wrap(self.logout),
                name='logout'),
            url(r'^password_change/$',
                wrap(self.password_change, cacheable=True),
                name='password_change'),
            url(r'^password_change/done/$',
                wrap(self.password_change_done, cacheable=True),
                name='password_change_done'),
            url(r'^jsi18n/$',
                wrap(self.i18n_javascript, cacheable=True),
                name='jsi18n'),
            url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$',
                wrap(contenttype_views.shortcut)),
            url(r'^(?P<app_label>\w+)/$',
                wrap(self.app_index),
                name='app_list')
        )

        # Add in each model's views.
        for model, model_admin in self._registry.iteritems():
            urlpatterns += patterns('',
                url(r'^%s/%s/' % (model._meta.app_label, model._meta.module_name),
                    include(model_admin.urls))
            )
        return urlpatterns

    @property
    def urls(self):
        return self.get_urls(), self.app_name, self.name

    def password_change(self, request):
        """
        Handles the "change password" task -- both form display and validation.
        """
        from django.contrib.auth.views import password_change
        if self.root_path is not None:
            url = '%spassword_change/done/' % self.root_path
        else:
            url = reverse('admin:password_change_done', current_app=self.name)
        defaults = {
            'current_app': self.name,
            'post_change_redirect': url
        }
        if self.password_change_template is not None:
            defaults['template_name'] = self.password_change_template
        return password_change(request, **defaults)

    def password_change_done(self, request, extra_context=None):
        """
        Displays the "success" page after a password change.
        """
        from django.contrib.auth.views import password_change_done
        defaults = {
            'current_app': self.name,
            'extra_context': extra_context or {},
        }
        if self.password_change_done_template is not None:
            defaults['template_name'] = self.password_change_done_template
        return password_change_done(request, **defaults)

    def i18n_javascript(self, request):
        """
        Displays the i18n JavaScript that the Django admin requires.

        This takes into account the USE_I18N setting. If it's set to False, the
        generated JavaScript will be leaner and faster.
        """
        if settings.USE_I18N:
            from django.views.i18n import javascript_catalog
        else:
            from django.views.i18n import null_javascript_catalog as javascript_catalog
        return javascript_catalog(request, packages=['django.conf', 'django.contrib.admin'])

    @never_cache
    def logout(self, request, extra_context=None):
        """
        Logs out the user for the given HttpRequest.

        This should *not* assume the user is already logged in.
        """
        from django.contrib.auth.views import logout
        defaults = {
            'current_app': self.name,
            'extra_context': extra_context or {},
        }
        if self.logout_template is not None:
            defaults['template_name'] = self.logout_template
        return logout(request, **defaults)

    @never_cache
    def login(self, request, extra_context=None):
        """
        Displays the login form for the given HttpRequest.
        """
        from django.contrib.auth.views import login
        context = {
            'title': _('Log in'),
            'root_path': self.root_path,
            'app_path': request.get_full_path(),
            REDIRECT_FIELD_NAME: request.get_full_path(),
        }
        context.update(extra_context or {})
        defaults = {
            'extra_context': context,
            'current_app': self.name,
            'authentication_form': self.login_form or AdminAuthenticationForm,
            'template_name': self.login_template or 'admin/login.html',
        }
        return login(request, **defaults)

    @never_cache
    def index(self, request, extra_context=None):
        """
        Displays the main admin index page, which lists all of the installed
        apps that have been registered in this site.
        """
        app_dict = {}
        user = request.user
        for model, model_admin in self._registry.items():
            app_label = model._meta.app_label
            has_module_perms = user.has_module_perms(app_label)

            if has_module_perms:
                perms = model_admin.get_model_perms(request)

                # Check whether user has any perm for this module.
                # If so, add the module to the model_list.
                if True in perms.values():
                    model_dict = {
                        'name': capfirst(model._meta.verbose_name_plural),
                        'admin_url': mark_safe('%s/%s/' % (app_label, model.__name__.lower())),
                        'perms': perms,
                    }
                    if app_label in app_dict:
                        app_dict[app_label]['models'].append(model_dict)
                    else:
                        app_dict[app_label] = {
                            'name': app_label.title(),
                            'app_url': app_label + '/',
                            'has_module_perms': has_module_perms,
                            'models': [model_dict],
                        }

        # Sort the apps alphabetically.
        app_list = app_dict.values()
        app_list.sort(key=lambda x: x['name'])

        # Sort the models alphabetically within each app.
        for app in app_list:
            app['models'].sort(key=lambda x: x['name'])

        context = {
            'title': _('Site administration'),
            'app_list': app_list,
            'root_path': self.root_path,
        }
        context.update(extra_context or {})
        return TemplateResponse(request, [
            self.index_template or 'admin/index.html',
        ], context, current_app=self.name)

    def app_index(self, request, app_label, extra_context=None):
        user = request.user
        has_module_perms = user.has_module_perms(app_label)
        app_dict = {}
        for model, model_admin in self._registry.items():
            if app_label == model._meta.app_label:
                if has_module_perms:
                    perms = model_admin.get_model_perms(request)

                    # Check whether user has any perm for this module.
                    # If so, add the module to the model_list.
                    if True in perms.values():
                        model_dict = {
                            'name': capfirst(model._meta.verbose_name_plural),
                            'admin_url': '%s/' % model.__name__.lower(),
                            'perms': perms,
                        }
                        if app_dict:
                            app_dict['models'].append(model_dict),
                        else:
                            # First time around, now that we know there's
                            # something to display, add in the necessary meta
                            # information.
                            app_dict = {
                                'name': app_label.title(),
                                'app_url': '',
                                'has_module_perms': has_module_perms,
                                'models': [model_dict],
                            }
        if not app_dict:
            raise http.Http404('The requested admin page does not exist.')
        # Sort the models alphabetically within each app.
        app_dict['models'].sort(key=lambda x: x['name'])
        context = {
            'title': _('%s administration') % capfirst(app_label),
            'app_list': [app_dict],
            'root_path': self.root_path,
        }
        context.update(extra_context or {})

        return TemplateResponse(request, self.app_index_template or [
            'admin/%s/app_index.html' % app_label,
            'admin/app_index.html'
        ], context, current_app=self.name)

# This global object represents the default admin site, for the common case.
# You can instantiate AdminSite in your own code to create a custom admin site.
site = AdminSite()
<|endoftext|>


<commit_before>from __future__ import absolute_import, division, print_function

import os
import sys
import shutil
from os.path import dirname, isdir, isfile, join, exists

import conda.config as cc
from conda.compat import iteritems

from conda_build.config import config
from conda_build import environ
from conda_build import source
from conda_build.utils import _check_call

try:
    import psutil
except ImportError:
    psutil = None

assert sys.platform == 'win32'

def fix_staged_scripts():
    """
    Fixes scripts which have been installed unix-style to have a .bat
    helper
    """
    scripts_dir = join(config.build_prefix, 'Scripts')
    if not isdir(scripts_dir):
        return
    for fn in os.listdir(scripts_dir):
        # process all the extensionless files
        if not isfile(join(scripts_dir, fn)) or '.' in fn:
            continue

        with open(join(scripts_dir, fn)) as f:
            line = f.readline().lower()
            # If it's a #!python script
            if not (line.startswith('#!') and 'python' in line.lower()):
                continue
            print('Adjusting unix-style #! script %s, '
                  'and adding a .bat file for it' % fn)
            # copy it with a .py extension (skipping that first #! line)
            with open(join(scripts_dir, fn + '-script.py'), 'w') as fo:
                fo.write(f.read())
            # now create the .exe file
            shutil.copyfile(join(dirname(__file__),
                                 'cli-%d.exe' % (8 * tuple.__itemsize__)),
                            join(scripts_dir, fn + '.exe'))

        # remove the original script
        os.remove(join(scripts_dir, fn))

def msvc_env_cmd(override=None):
    if 'ProgramFiles(x86)' in os.environ:
        program_files = os.environ['ProgramFiles(x86)']
    else:
        program_files = os.environ['ProgramFiles']

    msvc_env_lines = []

    if config.PY3K and config.use_MSVC2015:
        version = '14.0'
    elif config.PY3K:
        version = '10.0'
    else:
        version = '9.0'

    if override is not None:
        version = override
        msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
        msvc_env_lines.append('set MSSdk=1')

    vcvarsall = os.path.join(program_files,
                             r'Microsoft Visual Studio {version}'.format(version=version),
                             'VC', 'vcvarsall.bat')

    # Try the Microsoft Visual C++ Compiler for Python 2.7
    localappdata = os.environ.get("localappdata")
    not_vcvars = not isfile(vcvarsall)
    if not_vcvars and localappdata and not config.PY3K:
        vcvarsall = os.path.join(localappdata, "Programs", "Common",
            "Microsoft", "Visual C++ for Python", "9.0", "vcvarsall.bat")
    if not_vcvars and program_files and not config.PY3K:
        vcvarsall = os.path.join(program_files, 'Common Files',
            'Microsoft', 'Visual C++ for Python', "9.0", "vcvarsall.bat")
    if not_vcvars:
        print("Warning: Couldn't find Visual Studio: %r" % vcvarsall)
        return ''

    msvc_env_lines.append('call "%s" %s' % (vcvarsall, 'x86' if cc.bits == 32 else 'amd64'))
    return '\n'.join(msvc_env_lines)

def kill_processes():
    if psutil is None:
        return
    for n in psutil.pids():
        try:
            p = psutil.Process(n)
            if p.name.lower() == 'msbuild.exe':
                print('Terminating:', p.name)
                p.terminate()
        except:
            continue

def build(m):
    env = dict(os.environ)
    env.update(environ.get_dict(m))

    for name in 'BIN', 'INC', 'LIB':
        path = env['LIBRARY_' + name]
        if not isdir(path):
            os.makedirs(path)

    src_dir = source.get_dir()
    bld_bat = join(m.path, 'bld.bat')
    if exists(bld_bat):
        with open(bld_bat) as fi:
            data = fi.read()
        with open(join(src_dir, 'bld.bat'), 'w') as fo:
            fo.write(msvc_env_cmd(override=m.get_value('build/msvc_compiler', None)))
            fo.write('\n')

            # more debuggable with echo on
            fo.write('@echo on\n')
            fo.write("REM ===== end generated header =====\n")
            fo.write(data)

        cmd = [os.environ['COMSPEC'], '/c', 'call', 'bld.bat']
        _check_call(cmd, cwd=src_dir, env={str(k): str(v) for k, v in env.items()})
        kill_processes()
        fix_staged_scripts()
<commit_msg>explicitly add LIBRARY path variables to generated header<commit_after>from __future__ import absolute_import, division, print_function

import os
import sys
import shutil
from os.path import dirname, isdir, isfile, join, exists

import conda.config as cc
from conda.compat import iteritems

from conda_build.config import config
from conda_build import environ
from conda_build import source
from conda_build.utils import _check_call

try:
    import psutil
except ImportError:
    psutil = None

assert sys.platform == 'win32'

def fix_staged_scripts():
    """
    Fixes scripts which have been installed unix-style to have a .bat
    helper
    """
    scripts_dir = join(config.build_prefix, 'Scripts')
    if not isdir(scripts_dir):
        return
    for fn in os.listdir(scripts_dir):
        # process all the extensionless files
        if not isfile(join(scripts_dir, fn)) or '.' in fn:
            continue

        with open(join(scripts_dir, fn)) as f:
            line = f.readline().lower()
            # If it's a #!python script
            if not (line.startswith('#!') and 'python' in line.lower()):
                continue
            print('Adjusting unix-style #! script %s, '
                  'and adding a .bat file for it' % fn)
            # copy it with a .py extension (skipping that first #! line)
            with open(join(scripts_dir, fn + '-script.py'), 'w') as fo:
                fo.write(f.read())
            # now create the .exe file
            shutil.copyfile(join(dirname(__file__),
                                 'cli-%d.exe' % (8 * tuple.__itemsize__)),
                            join(scripts_dir, fn + '.exe'))

        # remove the original script
        os.remove(join(scripts_dir, fn))

def msvc_env_cmd(override=None):
    if 'ProgramFiles(x86)' in os.environ:
        program_files = os.environ['ProgramFiles(x86)']
    else:
        program_files = os.environ['ProgramFiles']

    msvc_env_lines = []

    if config.PY3K and config.use_MSVC2015:
        version = '14.0'
    elif config.PY3K:
        version = '10.0'
    else:
        version = '9.0'

    if override is not None:
        version = override
        msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
        msvc_env_lines.append('set MSSdk=1')

    vcvarsall = os.path.join(program_files,
                             r'Microsoft Visual Studio {version}'.format(version=version),
                             'VC', 'vcvarsall.bat')

    # Try the Microsoft Visual C++ Compiler for Python 2.7
    localappdata = os.environ.get("localappdata")
    not_vcvars = not isfile(vcvarsall)
    if not_vcvars and localappdata and not config.PY3K:
        vcvarsall = os.path.join(localappdata, "Programs", "Common",
            "Microsoft", "Visual C++ for Python", "9.0", "vcvarsall.bat")
    if not_vcvars and program_files and not config.PY3K:
        vcvarsall = os.path.join(program_files, 'Common Files',
            'Microsoft', 'Visual C++ for Python', "9.0", "vcvarsall.bat")
    if not_vcvars:
        print("Warning: Couldn't find Visual Studio: %r" % vcvarsall)
        return ''

    msvc_env_lines.append('call "%s" %s' % (vcvarsall, 'x86' if cc.bits == 32 else 'amd64'))
    return '\n'.join(msvc_env_lines)

def kill_processes():
    if psutil is None:
        return
    for n in psutil.pids():
        try:
            p = psutil.Process(n)
            if p.name.lower() == 'msbuild.exe':
                print('Terminating:', p.name)
                p.terminate()
        except:
            continue

def build(m):
    env = dict(os.environ)
    env.update(environ.get_dict(m))

    for name in 'BIN', 'INC', 'LIB':
        path = env['LIBRARY_' + name]
        if not isdir(path):
            os.makedirs(path)

    src_dir = source.get_dir()
    bld_bat = join(m.path, 'bld.bat')
    if exists(bld_bat):
        with open(bld_bat) as fi:
            data = fi.read()
        with open(join(src_dir, 'bld.bat'), 'w') as fo:
            fo.write(msvc_env_cmd(override=m.get_value('build/msvc_compiler', None)))
            fo.write('\n')

            # more debuggable with echo on
            fo.write('@echo on\n')
            fo.write("set INCLUDE={};%INCLUDE%\n".format(env["LIBRARY_INC"]))
            fo.write("set LIB={};%LIB%\n".format(env["LIBRARY_LIB"]))
            fo.write("set PATH={};%PATH%\n".format(env["LIBRARY_BIN"]))
            fo.write("REM ===== end generated header =====\n")
            fo.write(data)

        cmd = [os.environ['COMSPEC'], '/c', 'call', 'bld.bat']
        _check_call(cmd, cwd=src_dir, env={str(k): str(v) for k, v in env.items()})
        kill_processes()
        fix_staged_scripts()
<|endoftext|>


<commit_before>import psycopg2.extras
import pyiem.nws.vtec as vtec
import numpy as np
from pyiem.network import Table as NetworkTable
import pandas as pd

def get_description():
    """ Return a dict describing how to call this plotter """
    d = dict()
    d['cache'] = 86400
    d['data'] = True
    d['description'] = """This chart presents a histogram of issuance times
    for a given watch, warning, or advisory type for a given office."""
    d['arguments'] = [
        dict(type='networkselect', name='station', network='WFO',
             default='DMX', label='Select WFO:'),
        dict(type='phenomena', name='phenomena',
             default='WC', label='Select Watch/Warning Phenomena Type:'),
        dict(type='significance', name='significance',
             default='W', label='Select Watch/Warning Significance Level:'),
    ]
    return d

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='postgis', host='iemdb', user='nobody')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    wfo = fdict.get('station', 'DMX')
    phenomena = fdict.get('phenomena', 'WC')
    significance = fdict.get('significance', 'W')

    nt = NetworkTable("WFO")

    (fig, ax) = plt.subplots(1, 1)

    tzname = nt.sts[wfo]['tzname']
    cursor.execute("""
    WITH data as (
     SELECT extract(year from issue) as yr, eventid,
     min(issue at time zone %s) as minissue from warnings WHERE
     phenomena = %s and significance = %s
     and wfo = %s GROUP by yr, eventid)

    SELECT extract(hour from minissue) as hr, count(*) from data GROUP by hr
    """, (tzname, phenomena, significance, wfo))
    if cursor.rowcount == 0:
        ax.text(0.5, 0.5, "No Results Found",
                transform=ax.transAxes, ha='center')
        return fig

    data = np.zeros((24,), 'f')
    for row in cursor:
        data[int(row[0])] = row[1]
    df = pd.DataFrame(dict(hour=pd.Series(np.arange(24)),
                           count=pd.Series(data)))

    ax.bar(np.arange(24) - 0.4, data / float(sum(data)) * 100., ec='b', fc='b')
    ax.grid()
    ax.set_xticks(range(0, 25, 1))
    ax.set_xlim(-0.5, 23.5)
    ax.set_xticklabels(["Mid", "", "", "3 AM", "", "", "6 AM", "", "", '9 AM',
                        "", "", "Noon", "", "", "3 PM", "", "", "6 PM",
                        "", "", "9 PM", "", "", "Mid"])
    ax.set_xlabel("Timezone: %s (Daylight or Standard)" % (tzname,))
    ax.set_ylabel("Frequency [%%] out of %.0f Events" % (sum(data),))
    ax.set_title(("[%s] %s :: Issuance Time Frequency\n%s %s (%s.%s)"
                  ) % (wfo, nt.sts[wfo]['name'], vtec._phenDict[phenomena],
                       vtec._sigDict[significance], phenomena, significance))

    return fig, df
<commit_msg>prevent missing issuance from tripping up freq plot<commit_after>import psycopg2.extras
import pyiem.nws.vtec as vtec
import numpy as np
from pyiem.network import Table as NetworkTable
import pandas as pd

def get_description():
    """ Return a dict describing how to call this plotter """
    d = dict()
    d['cache'] = 86400
    d['data'] = True
    d['description'] = """This chart presents a histogram of issuance times
    for a given watch, warning, or advisory type for a given office."""
    d['arguments'] = [
        dict(type='networkselect', name='station', network='WFO',
             default='DMX', label='Select WFO:'),
        dict(type='phenomena', name='phenomena',
             default='WC', label='Select Watch/Warning Phenomena Type:'),
        dict(type='significance', name='significance',
             default='W', label='Select Watch/Warning Significance Level:'),
    ]
    return d

def plotter(fdict):
    """ Go """
    import matplotlib
    matplotlib.use('agg')
    import matplotlib.pyplot as plt
    pgconn = psycopg2.connect(database='postgis', host='iemdb', user='nobody')
    cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    wfo = fdict.get('station', 'DMX')
    phenomena = fdict.get('phenomena', 'WC')
    significance = fdict.get('significance', 'W')

    nt = NetworkTable("WFO")

    (fig, ax) = plt.subplots(1, 1)

    tzname = nt.sts[wfo]['tzname']
    cursor.execute("""
    WITH data as (
     SELECT extract(year from issue) as yr, eventid,
     min(issue at time zone %s) as minissue from warnings WHERE
     phenomena = %s and significance = %s
     and wfo = %s and issue is not null GROUP by yr, eventid)

    SELECT extract(hour from minissue) as hr, count(*) from data GROUP by hr
    """, (tzname, phenomena, significance, wfo))
    if cursor.rowcount == 0:
        ax.text(0.5, 0.5, "No Results Found",
                transform=ax.transAxes, ha='center')
        return fig

    data = np.zeros((24,), 'f')
    for row in cursor:
        data[int(row[0])] = row[1]
    df = pd.DataFrame(dict(hour=pd.Series(np.arange(24)),
                           count=pd.Series(data)))

    ax.bar(np.arange(24) - 0.4, data / float(sum(data)) * 100., ec='b', fc='b')
    ax.grid()
    ax.set_xticks(range(0, 25, 1))
    ax.set_xlim(-0.5, 23.5)
    ax.set_xticklabels(["Mid", "", "", "3 AM", "", "", "6 AM", "", "", '9 AM',
                        "", "", "Noon", "", "", "3 PM", "", "", "6 PM",
                        "", "", "9 PM", "", "", "Mid"])
    ax.set_xlabel("Timezone: %s (Daylight or Standard)" % (tzname,))
    ax.set_ylabel("Frequency [%%] out of %.0f Events" % (sum(data),))
    ax.set_title(("[%s] %s :: Issuance Time Frequency\n%s %s (%s.%s)"
                  ) % (wfo, nt.sts[wfo]['name'], vtec._phenDict[phenomena],
                       vtec._sigDict[significance], phenomena, significance))

    return fig, df
<|endoftext|>


<commit_before>"""
calculatepi.py
Author: Suhan Gui
Credit: None
Assignment:

Write and submit a Python program that computes an approximate value of π by calculating the following sum:

(see: https://github.com/HHS-IntroProgramming/Calculate-Pi/blob/master/README.md)

This sum approaches the true value of π as n approaches ∞.

Your program must ask the user how many terms to use in the estimate of π, how many decimal places,
then print the estimate using that many decimal places. Exactly like this:

I will estimate pi. How many terms should I use? 100
How many decimal places should I use in the result? 7
The approximate value of pi is 3.1315929

Note: remember that the printed value of pi will be an estimate!

"""
calc= int(input("I will estimate pi. How many terms should I use? "))
decimal= int(input("How many decimal places should I use in the result? "))
pi = 1.0/sum([((-1.0)**k)/math.factorial(k) for k in range(0,n)])
print("The approximate value of pi is {0:.{1}f}".format(pi, decimals))<commit_msg>Updated from Brython Server: 9/15/2015 7:48:50 AM<commit_after>"""
calculatepi.py
Author: Suhan Gui
Credit: None
Assignment:

Write and submit a Python program that computes an approximate value of π by calculating the following sum:

(see: https://github.com/HHS-IntroProgramming/Calculate-Pi/blob/master/README.md)

This sum approaches the true value of π as n approaches ∞.

Your program must ask the user how many terms to use in the estimate of π, how many decimal places,
then print the estimate using that many decimal places. Exactly like this:

I will estimate pi. How many terms should I use? 100
How many decimal places should I use in the result? 7
The approximate value of pi is 3.1315929

Note: remember that the printed value of pi will be an estimate!

"""
import math
calc= int(input("I will estimate pi. How many terms should I use? "))
decimal= int(input("How many decimal places should I use in the result? "))
pi = 4.0*(sum([((-1.0)**k)/(2*k+1) for k in range(0,decimal)]))
print("The approximate value of pi is {0:.{1}f}".format(pi, decimal))<|endoftext|>


<commit_before>import sys

from django.test import LiveServerTestCase
from django.utils.importlib import import_module
from django.utils.unittest import SkipTest
from django.utils.translation import ugettext as _

class AdminSeleniumWebDriverTestCase(LiveServerTestCase):
    webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver'

    @classmethod
    def setUpClass(cls):
        if sys.version_info < (2, 6):
            raise SkipTest('Selenium Webdriver does not support Python < 2.6.')
        try:
            # Import and start the WebDriver class.
            module, attr = cls.webdriver_class.rsplit('.', 1)
            mod = import_module(module)
            WebDriver = getattr(mod, attr)
            cls.selenium = WebDriver()
        except Exception:
            raise SkipTest('Selenium webdriver "%s" not installed or not '
                           'operational.' % cls.webdriver_class)
        super(AdminSeleniumWebDriverTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(AdminSeleniumWebDriverTestCase, cls).tearDownClass()
        if hasattr(cls, 'selenium'):
            cls.selenium.quit()

    def wait_until(self, callback, timeout=10):
        """
        Helper function that blocks the execution of the tests until the
        specified callback returns a value that is not falsy. This function can
        be called, for example, after clicking a link or submitting a form.
        See the other public methods that call this function for more details.
        """
        from selenium.webdriver.support.wait import WebDriverWait
        WebDriverWait(self.selenium, timeout).until(callback)

    def wait_loaded_tag(self, tag_name, timeout=10):
        """
        Helper function that blocks until the element with the given tag name
        is found on the page.
        """
        self.wait_until(
            lambda driver: driver.find_element_by_tag_name(tag_name),
            timeout
        )

    def admin_login(self, username, password, login_url='/admin/'):
        """
        Helper function to log into the admin.
        """
        self.selenium.get('%s%s' % (self.live_server_url, login_url))
        username_input = self.selenium.find_element_by_name('username')
        username_input.send_keys(username)
        password_input = self.selenium.find_element_by_name('password')
        password_input.send_keys(password)
        login_text = _('Log in')
        self.selenium.find_element_by_xpath(
            '//input[@value="%s"]' % login_text).click()
        # Wait for the next page to be loaded.
        self.wait_loaded_tag('body')

    def get_css_value(self, selector, attribute):
        """
        Helper function that returns the value for the CSS attribute of an
        DOM element specified by the given selector. Uses the jQuery that ships
        with Django.
        """
        return self.selenium.execute_script(
            'return django.jQuery("%s").css("%s")' % (selector, attribute))

    def select_option(self, selector, value):
        """
        Helper function to select the <OPTION> that has the value `value` and
        that is in the <SELECT> widget identified by the CSS selector `selector`.
        """
        from selenium.common.exceptions import NoSuchElementException
        options = self.selenium.find_elements_by_css_selector('%s option' % selector)
        for option in options:
            if option.get_attribute('value') == value:
                option.click()
                return
        raise NoSuchElementException('Option "%s" not found in "%s"' % (value, selector))<commit_msg>Stopped hiding the original exception's message when a selenium driver's instantiation fails.<commit_after>import sys

from django.test import LiveServerTestCase
from django.utils.importlib import import_module
from django.utils.unittest import SkipTest
from django.utils.translation import ugettext as _

class AdminSeleniumWebDriverTestCase(LiveServerTestCase):
    webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver'

    @classmethod
    def setUpClass(cls):
        if sys.version_info < (2, 6):
            raise SkipTest('Selenium Webdriver does not support Python < 2.6.')
        try:
            # Import and start the WebDriver class.
            module, attr = cls.webdriver_class.rsplit('.', 1)
            mod = import_module(module)
            WebDriver = getattr(mod, attr)
            cls.selenium = WebDriver()
        except Exception, e:
            raise SkipTest('Selenium webdriver "%s" not installed or not '
                           'operational: %s' % (cls.webdriver_class, str(e)))
        super(AdminSeleniumWebDriverTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(AdminSeleniumWebDriverTestCase, cls).tearDownClass()
        if hasattr(cls, 'selenium'):
            cls.selenium.quit()

    def wait_until(self, callback, timeout=10):
        """
        Helper function that blocks the execution of the tests until the
        specified callback returns a value that is not falsy. This function can
        be called, for example, after clicking a link or submitting a form.
        See the other public methods that call this function for more details.
        """
        from selenium.webdriver.support.wait import WebDriverWait
        WebDriverWait(self.selenium, timeout).until(callback)

    def wait_loaded_tag(self, tag_name, timeout=10):
        """
        Helper function that blocks until the element with the given tag name
        is found on the page.
        """
        self.wait_until(
            lambda driver: driver.find_element_by_tag_name(tag_name),
            timeout
        )

    def admin_login(self, username, password, login_url='/admin/'):
        """
        Helper function to log into the admin.
        """
        self.selenium.get('%s%s' % (self.live_server_url, login_url))
        username_input = self.selenium.find_element_by_name('username')
        username_input.send_keys(username)
        password_input = self.selenium.find_element_by_name('password')
        password_input.send_keys(password)
        login_text = _('Log in')
        self.selenium.find_element_by_xpath(
            '//input[@value="%s"]' % login_text).click()
        # Wait for the next page to be loaded.
        self.wait_loaded_tag('body')

    def get_css_value(self, selector, attribute):
        """
        Helper function that returns the value for the CSS attribute of an
        DOM element specified by the given selector. Uses the jQuery that ships
        with Django.
        """
        return self.selenium.execute_script(
            'return django.jQuery("%s").css("%s")' % (selector, attribute))

    def select_option(self, selector, value):
        """
        Helper function to select the <OPTION> that has the value `value` and
        that is in the <SELECT> widget identified by the CSS selector `selector`.
        """
        from selenium.common.exceptions import NoSuchElementException
        options = self.selenium.find_elements_by_css_selector('%s option' % selector)
        for option in options:
            if option.get_attribute('value') == value:
                option.click()
                return
        raise NoSuchElementException('Option "%s" not found in "%s"' % (value, selector))<|endoftext|>


<commit_before>import pytest
from testfixtures import LogCapture

from .. import _abstract as a

def test_logging():

    v = a.AbstractVlermv()
    v.func = lambda *args: 1/0
    assert not v.cache_exceptions

    with LogCapture() as l:
        with pytest.raises(ZeroDivisionError):
            v(8,9,3)
        with pytest.raises(ZeroDivisionError):
            v(8,9,3)

    msg = 'Exception in AbstractVlermv calling this memoized function:\n<lambda>(*(8, 9, 3), *{})'
    l.check(*((('vlermv._abstract', 'ERROR', msg),) * 2))

def test_logging_cache_exceptions():
    '''
    The exception should be logged only on the initial call,
    not on the load from the cache.
    '''

    class DictVlermv(a.AbstractVlermv):
        d = {}
        def __contains__(self, key):
            return key in self.d
        def __getitem__(self, key):
            return self.d[key]
        def __setitem__(self, key, value):
            self.d[key] = value

    v = DictVlermv(cache_exceptions = True)
    v[(348203481034,)] = (EnvironmentError('Pretend that something went wrong.'), None)
    def f(*args):
        raise AssertionError('This should not run.')
    v.func = f

    with LogCapture() as l:
        with pytest.raises(EnvironmentError):
            v(348203481034)
        l.check()

def test_set_transformer():
    v = a.AbstractVlermv(key_transformer = 3)
    assert v.key_transformer == 3

def test_truthy_call():
    v = a.AbstractVlermv()

    v.func = {}
    with pytest.raises(AttributeError):
        v(8) == 11

    v.func = None
    with pytest.raises(NotImplementedError):
        v(8) == 11
<commit_msg>test iter<commit_after>import pytest
from testfixtures import LogCapture

from .. import _abstract as a

def test_logging():

    v = a.AbstractVlermv()
    v.func = lambda *args: 1/0
    assert not v.cache_exceptions

    with LogCapture() as l:
        with pytest.raises(ZeroDivisionError):
            v(8,9,3)
        with pytest.raises(ZeroDivisionError):
            v(8,9,3)

    msg = 'Exception in AbstractVlermv calling this memoized function:\n<lambda>(*(8, 9, 3), *{})'
    l.check(*((('vlermv._abstract', 'ERROR', msg),) * 2))

def test_logging_cache_exceptions():
    '''
    The exception should be logged only on the initial call,
    not on the load from the cache.
    '''

    class DictVlermv(a.AbstractVlermv):
        d = {}
        def __contains__(self, key):
            return key in self.d
        def __getitem__(self, key):
            return self.d[key]
        def __setitem__(self, key, value):
            self.d[key] = value

    v = DictVlermv(cache_exceptions = True)
    v[(348203481034,)] = (EnvironmentError('Pretend that something went wrong.'), None)
    def f(*args):
        raise AssertionError('This should not run.')
    v.func = f

    with LogCapture() as l:
        with pytest.raises(EnvironmentError):
            v(348203481034)
        l.check()

def test_set_transformer():
    v = a.AbstractVlermv(key_transformer = 3)
    assert v.key_transformer == 3

def test_truthy_call():
    v = a.AbstractVlermv()

    v.func = {}
    with pytest.raises(AttributeError):
        v(8) == 11

    v.func = None
    with pytest.raises(NotImplementedError):
        v(8) == 11

def test_iter():
    'Iter should get keys.'
    class DictVlermv(a.AbstractVlermv):
        d = {8:9}
        def keys(self):
            return self.d.keys()
    v = DictVlermv()
    assert next(iter(v)) == 8
<|endoftext|>