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

# Custom module
import lightCommon as lc

# Raspberry Pi GPIO
import RPi.GPIO as gpio

import sys,os, socket, struct
from time import sleep

class momentaryHandler():
    def callback(self,pin):

        print('Callback called for pin '+str(pin))

        # This next piece of logic could be better written as a do...while
        # Oh well. There isn't much of a performance hit because of it.
        # This is a simple application after all

        # Debounce
        sleep(0.2)

        # This is another "debounce" of sorts. Because we are using the raspberry pi
        # as a VERY SMALL current source it may fluctuate at times. This makes sure
        # that if there is a dip in the supply but the switch hasn't changed that the
        # light doesn't toggle

        #self.status = gpio.input(self.switch_pin)

        if self.read() == gpio.LOW:

            try:
                self.status = not self.status
                print('Actually triggering pin '+ str(self.light) + ' status ' + str(self.status))
                light_num = self.light
                light_status = self.status
                req_type = lc.msg_set

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.node, lc.port()))
                s.sendall(struct.pack(lc.packString,req_type,light_num,light_status,0))
                s.close()

            except socket.error as e:
                print 'Socket error: ',e

    def read(self):
        return gpio.input(self.switch_pin)

    def __init__(self,switch_pin,switch_type,node,light_num):
        self.switch_pin = switch_pin
        self.switch_type = switch_type
        self.status = not self.read()
        self.node = lc.getIpFromName(node)
        self.light = light_num
        self.first_run = True
        self.callback(self.switch_pin)

class toggleHandler():
    def callback(self,pin):

        print('Callback called for pin '+str(pin))

        # Because this is a toggle just take the value of the switch and
        # apply it to the light
        sleep(0.2)
        if self.read() == True:
            self.status = lc.on
        else:
            self.status = lc.off

        try:
            print('Actually triggering pin '+ str(self.light) + ' status ' + str(self.status))
            light_num = self.light
            light_status = self.status
            req_type = lc.msg_set

            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((self.node, lc.port()))
            s.sendall(struct.pack(lc.packString,req_type,light_num,light_status,0))
            s.close()

        except socket.error as e:
            print 'Socket error: ',e

    def read(self):
        if gpio.input(self.switch_pin) == self.switch_active:
            return True
        else:
            return False

    def __init__(self,switch_dict):
        self.switch_pin = switch_dict['switch_pin']
        self.switch_type = switch_dict['switch_type']
        self.switch_active = switch_dict['switch_active']
        self.status = self.read()
        self.node = lc.getIpFromName(switch_dict['node_name'])
        self.light = switch_dict['node_light']
        # Now that the class is set up act on the status of the switch
        self.callback(self.switch_pin)

# First figure out what node this is so we can make sure this program actually
# has to run. We'll check for switches
node_props = lc.getNodeProps()
if 'switches' in node_props:
    switches = node_props['switches']
else:
    print('No switches attached to this node')
    exit()

# The node we are connecting to ( localhost? )
node = lc.getIpFromName(node_props['node'])

# Set up GPIO on the raspberry pi
gpio.setmode(gpio.BCM)

# Now for every switch set up the GPIO
for switch in switches:
    gpio.setup(switch['switch_pin'], gpio.IN, pull_up_down=gpio.PUD_UP)

    if switch['switch_type'] == 'momentary':
        new_handler = momentaryHandler(switch['switch_pin'],switch['switch_type'],switch['node_name'],switch['node_light'])
        gpio.add_event_detect(switch['switch_pin'], gpio.FALLING, callback=new_handler.callback, bouncetime=500)

    elif switch['switch_type'] == 'toggle':
        new_handler = toggleHandler(switch)
        gpio.add_event_detect(switch['switch_pin'], gpio.BOTH, callback=new_handler.callback, bouncetime=500)

print 'GPIO set up'

try:
    input() # blocks forever
except KeyboardInterrupt:
    print('Exiting..')

'''
# Enumerate lights on the local device
local_lights=[]

req_type = lc.msg_dump
light_num = 0
light_status = 0

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((node, lc.port()))
    s.sendall(struct.pack(lc.packString,req_type,light_num,light_status))

    recv_msg = s.recv(struct.calcsize(lc.queryPackString))
    req_type,light_num,light_status,light_name = struct.unpack(lc.queryPackString,recv_msg)

    while req_type is not lc.msg_done:

        local_lights.append(light_num)

        recv_msg = s.recv(struct.calcsize(lc.queryPackString))
        req_type,light_num,light_status,light_name = struct.unpack(lc.queryPackString,recv_msg)

    s.close()

except socket.error as e:
    print 'Error:',e

# Now that lights are enumerated wait for button to be pressed

# Set up for main loop
on_or_off = lc.off
first_run = True
switch_pin=26

gpio.setup(switch_pin, gpio.IN, pull_up_down=gpio.PUD_UP)

if True:

    while end_it == False:

        # This next piece of logic could be better written as a do...while
        # Oh well. There isn't much of a performance hit because of it.
        # This is a simple application after all

        # Wait for button press
        if first_run == False:
            gpio.wait_for_edge(switch_pin, gpio.BOTH)
            # Debounce
            sleep(0.2)
        else:
            first_run = False
            # set to "not" to force the next piece of logic
            switch_status = not gpio.input(switch_pin)

        # This is another "debounce" of sorts. Because we are using the raspberry pi
        # as a VERY SMALL current source it may fluctuate at times. This makes sure
        # that if there is a dip in the supply but the switch hasn't changed that the
        # light doesn't toggle

        # Not equal means it really changed.
        if gpio.input(switch_pin) != switch_status:

            # lc.switch_off is defined in light common.
            if gpio.input(switch_pin) == lc.switch_off:
                on_or_off = lc.off
            else:
                on_or_off = lc.on

            switch_status = gpio.input(switch_pin)

            for light in local_lights:
                try:

                    req_type = lc.msg_set
                    light_num = light
                    light_status = on_or_off

                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.connect((node, lc.port()))
                    s.sendall(struct.pack(lc.packString,req_type,light_num,light_status))
                    s.close()

                except socket.error as e:
                    print 'Error:',e

else:
    pass

'''

gpio.cleanup()

print('Exiting localToggle')
<commit_msg>Removed debounce by default because I added a hardware debounce. You would want to enable if you didn't have a hardware debounce circuit.<commit_after>#!/usr/bin/env python

# Custom module
import lightCommon as lc

# Raspberry Pi GPIO
import RPi.GPIO as gpio

import sys,os, socket, struct
from time import sleep

class momentaryHandler():
    def callback(self,pin):

        print('Callback called for pin '+str(pin))

        # This next piece of logic could be better written as a do...while
        # Oh well. There isn't much of a performance hit because of it.
        # This is a simple application after all

        # Debounce JUST a little more
        if self.debounce:
            sleep(0.2)

        # This is another "debounce" of sorts. Because we are using the raspberry pi
        # as a VERY SMALL current source it may fluctuate at times. This makes sure
        # that if there is a dip in the supply but the switch hasn't changed that the
        # light doesn't toggle

        #self.status = gpio.input(self.switch_pin)

        if self.read() == gpio.LOW:

            try:
                self.status = not self.status
                print('Actually triggering pin '+ str(self.light) + ' status ' + str(self.status))
                light_num = self.light
                light_status = self.status
                req_type = lc.msg_set

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.node, lc.port()))
                s.sendall(struct.pack(lc.packString,req_type,light_num,light_status,0))
                s.close()

            except socket.error as e:
                print 'Socket error: ',e

    def read(self):
        return gpio.input(self.switch_pin)

    def __init__(self,switch_pin,switch_type,node,light_num,debounce=False):
        self.switch_pin = switch_pin
        self.switch_type = switch_type
        self.status = not self.read()
        self.node = lc.getIpFromName(node)
        self.light = light_num
        self.first_run = True
        self.debounce = debounce
        self.callback(self.switch_pin)

class toggleHandler():
    def callback(self,pin):

        print('Callback called for pin '+str(pin))

        # Because this is a toggle just take the value of the switch and
        # apply it to the light
        # Debounce JUST a little more
        if self.debounce:
            sleep(0.2)

        if self.read() == True:
            self.status = lc.on
        else:
            self.status = lc.off

        try:
            print('Actually triggering pin '+ str(self.light) + ' status ' + str(self.status))
            light_num = self.light
            light_status = self.status
            req_type = lc.msg_set

            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((self.node, lc.port()))
            s.sendall(struct.pack(lc.packString,req_type,light_num,light_status,0))
            s.close()

        except socket.error as e:
            print 'Socket error: ',e

    def read(self):
        if gpio.input(self.switch_pin) == self.switch_active:
            return True
        else:
            return False

    def __init__(self,switch_dict,debounce=False):
        self.switch_pin = switch_dict['switch_pin']
        self.switch_type = switch_dict['switch_type']
        self.switch_active = switch_dict['switch_active']
        self.status = self.read()
        self.node = lc.getIpFromName(switch_dict['node_name'])
        self.light = switch_dict['node_light']
        self.debounce=debounce
        # Now that the class is set up act on the status of the switch
        self.callback(self.switch_pin)

# First figure out what node this is so we can make sure this program actually
# has to run. We'll check for switches
node_props = lc.getNodeProps()
if 'switches' in node_props:
    switches = node_props['switches']
else:
    print('No switches attached to this node')
    exit()

# The node we are connecting to ( localhost? )
node = lc.getIpFromName(node_props['node'])

# Set up GPIO on the raspberry pi
gpio.setmode(gpio.BCM)

# Now for every switch set up the GPIO
for switch in switches:
    gpio.setup(switch['switch_pin'], gpio.IN, pull_up_down=gpio.PUD_UP)

    if switch['switch_type'] == 'momentary':
        new_handler = momentaryHandler(switch['switch_pin'],switch['switch_type'],switch['node_name'],switch['node_light'])
        gpio.add_event_detect(switch['switch_pin'], gpio.FALLING, callback=new_handler.callback, bouncetime=500)

    elif switch['switch_type'] == 'toggle':
        new_handler = toggleHandler(switch)
        gpio.add_event_detect(switch['switch_pin'], gpio.BOTH, callback=new_handler.callback, bouncetime=500)

print 'GPIO set up'

try:
    input() # blocks forever
except KeyboardInterrupt:
    print('Exiting..')

'''
# Enumerate lights on the local device
local_lights=[]

req_type = lc.msg_dump
light_num = 0
light_status = 0

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((node, lc.port()))
    s.sendall(struct.pack(lc.packString,req_type,light_num,light_status))

    recv_msg = s.recv(struct.calcsize(lc.queryPackString))
    req_type,light_num,light_status,light_name = struct.unpack(lc.queryPackString,recv_msg)

    while req_type is not lc.msg_done:

        local_lights.append(light_num)

        recv_msg = s.recv(struct.calcsize(lc.queryPackString))
        req_type,light_num,light_status,light_name = struct.unpack(lc.queryPackString,recv_msg)

    s.close()

except socket.error as e:
    print 'Error:',e

# Now that lights are enumerated wait for button to be pressed

# Set up for main loop
on_or_off = lc.off
first_run = True
switch_pin=26

gpio.setup(switch_pin, gpio.IN, pull_up_down=gpio.PUD_UP)

if True:

    while end_it == False:

        # This next piece of logic could be better written as a do...while
        # Oh well. There isn't much of a performance hit because of it.
        # This is a simple application after all

        # Wait for button press
        if first_run == False:
            gpio.wait_for_edge(switch_pin, gpio.BOTH)
            # Debounce
            sleep(0.2)
        else:
            first_run = False
            # set to "not" to force the next piece of logic
            switch_status = not gpio.input(switch_pin)

        # This is another "debounce" of sorts. Because we are using the raspberry pi
        # as a VERY SMALL current source it may fluctuate at times. This makes sure
        # that if there is a dip in the supply but the switch hasn't changed that the
        # light doesn't toggle

        # Not equal means it really changed.
        if gpio.input(switch_pin) != switch_status:

            # lc.switch_off is defined in light common.
            if gpio.input(switch_pin) == lc.switch_off:
                on_or_off = lc.off
            else:
                on_or_off = lc.on

            switch_status = gpio.input(switch_pin)

            for light in local_lights:
                try:

                    req_type = lc.msg_set
                    light_num = light
                    light_status = on_or_off

                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.connect((node, lc.port()))
                    s.sendall(struct.pack(lc.packString,req_type,light_num,light_status))
                    s.close()

                except socket.error as e:
                    print 'Error:',e

else:
    pass

'''

gpio.cleanup()

print('Exiting localToggle')
<|endoftext|>


<commit_before>import discord
import json
from discord.ext import commands
from datetime import datetime

from utils import aiohttp_wrap as aw

class Game:
    """ Cog which allows fetching of video game information """
    IG_URL = 'https://api-2445582011268.apicast.io/{}/'
    with open('data/apikeys.json') as f:
        KEY = json.load(f)['igdb']

    def __init__(self, bot):
        self.bot = bot
        self.session = bot.aio_session

    @commands.comand(aliases=['games'])
    async def game(self, ctx, *, query: str):
        """ Search for some information about a game """
        url = self.IG_URL.format('games')
        headers = {'user-key': self.KEY}
        params = {'search': query,
                  'fields': 'name,summary,first_release_date,aggregated_rating,cover'}

        resp = await aw.session_get(self.session, url, headers=headers, params=params).json()

        await ctx.send(f'{resp}'[:500])

def setup(bot):
    bot.add_cog(Game(bot))
<commit_msg>typos galore<commit_after>import discord
import json
from discord.ext import commands
from datetime import datetime

from utils import aiohttp_wrap as aw

class Game:
    """ Cog which allows fetching of video game information """
    IG_URL = 'https://api-2445582011268.apicast.io/{}/'
    with open('data/apikeys.json') as f:
        KEY = json.load(f)['igdb']

    def __init__(self, bot):
        self.bot = bot
        self.session = bot.aio_session

    @commands.command(aliases=['games'])
    async def game(self, ctx, *, query: str):
        """ Search for some information about a game """
        url = self.IG_URL.format('games')
        headers = {'user-key': self.KEY}
        params = {'search': query,
                  'fields': 'name,summary,first_release_date,aggregated_rating,cover'}

        resp = await aw.session_get(self.session, url, headers=headers, params=params).json()

        await ctx.send(f'{resp}'[:500])

def setup(bot):
    bot.add_cog(Game(bot))
<|endoftext|>


<commit_before># coding: utf-8

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os
import copy
import inspect
import logging
import functools

# Third-party
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import astropy.units as u
from astropy.utils import isiterable

# Project
from ..integrate import *

__all__ = ["Potential", "CartesianPotential", "CompositePotential"]

class Potential(object):
    """
    A baseclass for representing gravitational potentials. You must specify
    a function that evaluates the potential value (func). You may also
    optionally add a function that computes derivatives (gradient), and a
    function to compute the Hessian of the potential.

    Parameters
    ----------
    func : function
        A function that computes the value of the potential.
    gradient : function (optional)
        A function that computes the first derivatives (gradient) of the potential.
    hessian : function (optional)
        A function that computes the second derivatives (Hessian) of the potential.
    parameters : dict (optional)
        Any extra parameters that the functions (func, gradient, hessian)
        require. All functions must take the same parameters.

    """

    def __init__(self, func, gradient=None, hessian=None, parameters=dict()):
        # store parameters
        self.parameters = parameters

        # Make sure the functions are callable
        for f in [func, gradient, hessian]:
            if f is not None and not hasattr(f, '__call__'):
                raise TypeError("'{}' must be callable! You passed "
                                "in a '{}'".format(f.func_name, f.__class__))

        self._value = func
        self._gradient = gradient
        self._hessian = hessian

    def value(self, x):
        """
        Compute the value of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the value of the potential.
        """
        return self._value(np.array(x), **self.parameters)

    def gradient(self, x):
        """
        Compute the gradient of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the gradient.
        """
        if self._gradient is None:
            raise NotImplementedError("No gradient function was specified when"
                                      " the object was created!")
        return self._gradient(np.array(x), **self.parameters)

    def hessian(self, x):
        """
        Compute the Hessian of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the Hessian.
        """
        if self._hessian is None:
            raise NotImplementedError("No Hessian function was specified when"
                                      " the object was created!")
        return self._hessian(np.array(x), **self.parameters)

    # Other useful functions to compute
    def __call__(self, x):
        return self.value(x)

    def acceleration(self, x):
        """
        Compute the acceleration due to the potential at the given
        position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the acceleration at.
        """
        return -self.gradient(np.array(x))

    def __repr__(self):
        pars = ""
        for k,v in self.parameters.items():
            par_fmt = "{}"
            post = ""

            if hasattr(v,'unit'):
                post = " {}".format(v.unit)
                v = v.value

            if isinstance(v, float):
                if np.log10(v) < -2 or np.log10(v) > 5:
                    par_fmt = "{:.2e}"
                else:
                    par_fmt = "{:.2f}"

            elif isinstance(v, int) and np.log10(v) > 5:
                par_fmt = "{:.2e}"

            pars += ("{}=" + par_fmt + post).format(k,v) + ", "

        return "<{}: {}>".format(self.__class__.__name__, pars.rstrip(", "))

    def __str__(self):
        return self.__class__.__name__

    def plot_contours(self, grid, ax=None, labels=None, subplots_kw=dict(), **kwargs):
        """
        Plot equipotentials contours. Computes the potential value on a grid
        (specified by the array `grid`).

        Parameters
        ----------
        grid : tuple
            Coordinate grids or slice value for each dimension. Should be a
            tuple of 1D arrays or numbers.
        ax : matplotlib.Axes (optional)
        labels : iterable (optional)
            List of axis labels.
        subplots_kw : dict
            kwargs passed to matplotlib's subplots() function if an axes object
            is not specified.
        kwargs : dict
            kwargs passed to either contourf() or plot().

        """

        # figure out which elements are iterable, which are numeric
        _grids = []
        _slices = []
        for ii,g in enumerate(grid):
            if isiterable(g):
                _grids.append((ii,g))
            else:
                _slices.append((ii,g))

        # figure out the dimensionality
        ndim = len(_grids)

        # if ndim > 2, don't know how to handle this!
        if ndim > 2:
            raise ValueError("ndim > 2: you can only make contours on a 2D grid. For other "
                             "dimensions, you have to specify values to slice.")

        if ax is None:
            # default figsize
            fig, ax = plt.subplots(1, 1, **subplots_kw)
        else:
            fig = ax.figure

        if ndim == 1:
            # 1D curve
            x1 = _grids[0][1]
            r = np.zeros((len(x1), len(_grids) + len(_slices)))
            r[:,_grids[0][0]] = x1

            for ii,slc in _slices:
                r[:,ii] = slc

            Z = self.value(r)
            ax.plot(x1, Z, **kwargs)

            if labels is not None:
                ax.set_xlabel(labels[0])
                ax.set_ylabel("potential")
        else:
            # 2D contours
            x1,x2 = np.meshgrid(_grids[0][1], _grids[1][1])
            shp = x1.shape
            x1,x2 = x1.ravel(), x2.ravel()

            r = np.zeros((len(x1), len(_grids) + len(_slices)))
            r[:,_grids[0][0]] = x1
            r[:,_grids[1][0]] = x2

            for ii,slc in _slices:
                r[:,ii] = slc

            Z = self.value(r)

            # make default colormap not suck
            cmap = kwargs.pop('cmap', cm.Blues)
            cs = ax.contourf(x1.reshape(shp), x2.reshape(shp), Z.reshape(shp),
                             cmap=cmap, **kwargs)

            if labels is not None:
                ax.set_xlabel(labels[0])
                ax.set_ylabel(labels[1])

        return fig,ax

    def integrate_orbit(self, w0, Integrator=LeapfrogIntegrator, **time_spec):
        """
        Integrate an orbit in the current potential using the integrator class
        provided. Uses same time specification as `Integrator.run()` -- see
        the documentation for `streamteam.integrate` for more information.

        Parameters
        ----------
        w0 : array_like
            Initial conditions.
        Integrator : class
            Integrator class to use.

        Other Parameters
        ----------------
        (see Integrator documentation)

        """

        if Integrator == LeapfrogIntegrator:
            acc = lambda t,w: self.acceleration(w)
        else:
            acc = lambda t,w: np.hstack((w[...,3:],self.acceleration(w[...,:3])))

        integrator = Integrator(acc)
        return integrator.run(w0, **time_spec)

class CartesianPotential(Potential):
    """
    A baseclass for representing Cartesian gravitational potentials. You must
    specify a function that evaluates the potential value (func). You may also
    optionally add a function that computes derivatives (gradient), and a
    function to compute the Hessian of the potential.

    Parameters
    ----------
    func : function
        A function that computes the value of the potential.
    gradient : function (optional)
        A function that computes the first derivatives (gradient) of the potential.
    hessian : function (optional)
        A function that computes the second derivatives (Hessian) of the potential.
    parameters : dict (optional)
        Any extra parameters that the functions (func, gradient, hessian)
        require. All functions must take the same parameters.

    """

    def energy(self, x, v):
        """
        Compute the total energy (per unit mass) of a point in phase-space
        in this potential. Assumes the last axis of the input position /
        velocity is the dimension axis, e.g., for 100 points in 3-space,
        the arrays should have shape (100,3).

        Parameters
        ----------
        x : array_like, numeric
            Position.
        v : array_like, numeric
            Velocity.
        """

        return self.value(x) + 0.5*(np.sum(v**2,axis=-1))

class CompositePotential(dict, Potential):
    """
    A potential composed of several distinct components. For example,
    two point masses or a galactic disk and halo, each with their own
    potential model.

    A `CompositePotential` is created like a Python dictionary, e.g.::

        >>> p1 = Potential(func1)
        >>> p2 = Potential(func2)
        >>> cp = CompositePotential(component1=p1, component2=p2)

    or equivalently::

        >>> cp = CompositePotential()
        >>> cp['component1'] = p1
        >>> cp['component2'] = p2

    You can also use any of the built-in `Potential` classes as
    components::

        >>> from streamteam.potential import HernquistPotential
        >>> cp = CompositePotential()
        >>> cp['spheroid'] = HernquistPotential(m=1E11, c=10., usys=[u.kpc,u.Myr,u.Msun])

    """
    def __init__(self, **kwargs):
        for v in kwargs.values():
            self._check_component(v)

        dict.__init__(self, **kwargs)

    def __setitem__(self, key, value):
        self._check_component(value)
        super(CompositePotential, self).__setitem__(key, value)

    def _check_component(self, p):
        # TODO: just check for value/gradient/hessian functions?
        if not isinstance(p, Potential):
            raise TypeError("Potential components may only be Potential "
                            "objects, not {0}.".format(type(p)))

    def value(self, x):
        """
        Compute the value of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the value of the potential.
        """
        return np.array([p.value(x) for p in self.values()]).sum(axis=0)

    def gradient(self, x):
        """
        Compute the gradient of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the gradient.
        """
        return np.array([p.gradient(x) for p in self.values()]).sum(axis=0)

    def hessian(self, x):
        """
        Compute the Hessian of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the Hessian.
        """
        return np.array([p.hessian(x) for p in self.values()]).sum(axis=0)<commit_msg>add cartesian composite<commit_after># coding: utf-8

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os
import copy
import inspect
import logging
import functools

# Third-party
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import astropy.units as u
from astropy.utils import isiterable

# Project
from ..integrate import *

__all__ = ["Potential", "CartesianPotential", "CompositePotential", "CartesianCompositePotential"]

class Potential(object):
    """
    A baseclass for representing gravitational potentials. You must specify
    a function that evaluates the potential value (func). You may also
    optionally add a function that computes derivatives (gradient), and a
    function to compute the Hessian of the potential.

    Parameters
    ----------
    func : function
        A function that computes the value of the potential.
    gradient : function (optional)
        A function that computes the first derivatives (gradient) of the potential.
    hessian : function (optional)
        A function that computes the second derivatives (Hessian) of the potential.
    parameters : dict (optional)
        Any extra parameters that the functions (func, gradient, hessian)
        require. All functions must take the same parameters.

    """

    def __init__(self, func, gradient=None, hessian=None, parameters=dict()):
        # store parameters
        self.parameters = parameters

        # Make sure the functions are callable
        for f in [func, gradient, hessian]:
            if f is not None and not hasattr(f, '__call__'):
                raise TypeError("'{}' must be callable! You passed "
                                "in a '{}'".format(f.func_name, f.__class__))

        self._value = func
        self._gradient = gradient
        self._hessian = hessian

    def value(self, x):
        """
        Compute the value of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the value of the potential.
        """
        return self._value(np.array(x), **self.parameters)

    def gradient(self, x):
        """
        Compute the gradient of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the gradient.
        """
        if self._gradient is None:
            raise NotImplementedError("No gradient function was specified when"
                                      " the object was created!")
        return self._gradient(np.array(x), **self.parameters)

    def hessian(self, x):
        """
        Compute the Hessian of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the Hessian.
        """
        if self._hessian is None:
            raise NotImplementedError("No Hessian function was specified when"
                                      " the object was created!")
        return self._hessian(np.array(x), **self.parameters)

    # Other useful functions to compute
    def __call__(self, x):
        return self.value(x)

    def acceleration(self, x):
        """
        Compute the acceleration due to the potential at the given
        position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the acceleration at.
        """
        return -self.gradient(np.array(x))

    def __repr__(self):
        pars = ""
        for k,v in self.parameters.items():
            par_fmt = "{}"
            post = ""

            if hasattr(v,'unit'):
                post = " {}".format(v.unit)
                v = v.value

            if isinstance(v, float):
                if np.log10(v) < -2 or np.log10(v) > 5:
                    par_fmt = "{:.2e}"
                else:
                    par_fmt = "{:.2f}"

            elif isinstance(v, int) and np.log10(v) > 5:
                par_fmt = "{:.2e}"

            pars += ("{}=" + par_fmt + post).format(k,v) + ", "

        return "<{}: {}>".format(self.__class__.__name__, pars.rstrip(", "))

    def __str__(self):
        return self.__class__.__name__

    def plot_contours(self, grid, ax=None, labels=None, subplots_kw=dict(), **kwargs):
        """
        Plot equipotentials contours. Computes the potential value on a grid
        (specified by the array `grid`).

        Parameters
        ----------
        grid : tuple
            Coordinate grids or slice value for each dimension. Should be a
            tuple of 1D arrays or numbers.
        ax : matplotlib.Axes (optional)
        labels : iterable (optional)
            List of axis labels.
        subplots_kw : dict
            kwargs passed to matplotlib's subplots() function if an axes object
            is not specified.
        kwargs : dict
            kwargs passed to either contourf() or plot().

        """

        # figure out which elements are iterable, which are numeric
        _grids = []
        _slices = []
        for ii,g in enumerate(grid):
            if isiterable(g):
                _grids.append((ii,g))
            else:
                _slices.append((ii,g))

        # figure out the dimensionality
        ndim = len(_grids)

        # if ndim > 2, don't know how to handle this!
        if ndim > 2:
            raise ValueError("ndim > 2: you can only make contours on a 2D grid. For other "
                             "dimensions, you have to specify values to slice.")

        if ax is None:
            # default figsize
            fig, ax = plt.subplots(1, 1, **subplots_kw)
        else:
            fig = ax.figure

        if ndim == 1:
            # 1D curve
            x1 = _grids[0][1]
            r = np.zeros((len(x1), len(_grids) + len(_slices)))
            r[:,_grids[0][0]] = x1

            for ii,slc in _slices:
                r[:,ii] = slc

            Z = self.value(r)
            ax.plot(x1, Z, **kwargs)

            if labels is not None:
                ax.set_xlabel(labels[0])
                ax.set_ylabel("potential")
        else:
            # 2D contours
            x1,x2 = np.meshgrid(_grids[0][1], _grids[1][1])
            shp = x1.shape
            x1,x2 = x1.ravel(), x2.ravel()

            r = np.zeros((len(x1), len(_grids) + len(_slices)))
            r[:,_grids[0][0]] = x1
            r[:,_grids[1][0]] = x2

            for ii,slc in _slices:
                r[:,ii] = slc

            Z = self.value(r)

            # make default colormap not suck
            cmap = kwargs.pop('cmap', cm.Blues)
            cs = ax.contourf(x1.reshape(shp), x2.reshape(shp), Z.reshape(shp),
                             cmap=cmap, **kwargs)

            if labels is not None:
                ax.set_xlabel(labels[0])
                ax.set_ylabel(labels[1])

        return fig,ax

    def integrate_orbit(self, w0, Integrator=LeapfrogIntegrator, **time_spec):
        """
        Integrate an orbit in the current potential using the integrator class
        provided. Uses same time specification as `Integrator.run()` -- see
        the documentation for `streamteam.integrate` for more information.

        Parameters
        ----------
        w0 : array_like
            Initial conditions.
        Integrator : class
            Integrator class to use.

        Other Parameters
        ----------------
        (see Integrator documentation)

        """

        if Integrator == LeapfrogIntegrator:
            acc = lambda t,w: self.acceleration(w)
        else:
            acc = lambda t,w: np.hstack((w[...,3:],self.acceleration(w[...,:3])))

        integrator = Integrator(acc)
        return integrator.run(w0, **time_spec)

class CartesianPotential(Potential):
    """
    A baseclass for representing Cartesian gravitational potentials. You must
    specify a function that evaluates the potential value (func). You may also
    optionally add a function that computes derivatives (gradient), and a
    function to compute the Hessian of the potential.

    Parameters
    ----------
    func : function
        A function that computes the value of the potential.
    gradient : function (optional)
        A function that computes the first derivatives (gradient) of the potential.
    hessian : function (optional)
        A function that computes the second derivatives (Hessian) of the potential.
    parameters : dict (optional)
        Any extra parameters that the functions (func, gradient, hessian)
        require. All functions must take the same parameters.

    """

    def energy(self, x, v):
        """
        Compute the total energy (per unit mass) of a point in phase-space
        in this potential. Assumes the last axis of the input position /
        velocity is the dimension axis, e.g., for 100 points in 3-space,
        the arrays should have shape (100,3).

        Parameters
        ----------
        x : array_like, numeric
            Position.
        v : array_like, numeric
            Velocity.
        """

        return self.value(x) + 0.5*(np.sum(v**2,axis=-1))

class CompositePotential(dict, Potential):
    """
    A potential composed of several distinct components. For example,
    two point masses or a galactic disk and halo, each with their own
    potential model.

    A `CompositePotential` is created like a Python dictionary, e.g.::

        >>> p1 = Potential(func1)
        >>> p2 = Potential(func2)
        >>> cp = CompositePotential(component1=p1, component2=p2)

    or equivalently::

        >>> cp = CompositePotential()
        >>> cp['component1'] = p1
        >>> cp['component2'] = p2

    You can also use any of the built-in `Potential` classes as
    components::

        >>> from streamteam.potential import HernquistPotential
        >>> cp = CompositePotential()
        >>> cp['spheroid'] = HernquistPotential(m=1E11, c=10., usys=[u.kpc,u.Myr,u.Msun])

    """
    def __init__(self, **kwargs):
        for v in kwargs.values():
            self._check_component(v)

        dict.__init__(self, **kwargs)

    def __setitem__(self, key, value):
        self._check_component(value)
        super(CompositePotential, self).__setitem__(key, value)

    def _check_component(self, p):
        # TODO: just check for value/gradient/hessian functions?
        if not isinstance(p, Potential):
            raise TypeError("Potential components may only be Potential "
                            "objects, not {0}.".format(type(p)))

    def value(self, x):
        """
        Compute the value of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the value of the potential.
        """
        return np.array([p.value(x) for p in self.values()]).sum(axis=0)

    def gradient(self, x):
        """
        Compute the gradient of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the gradient.
        """
        return np.array([p.gradient(x) for p in self.values()]).sum(axis=0)

    def hessian(self, x):
        """
        Compute the Hessian of the potential at the given position(s).

        Parameters
        ----------
        x : array_like, numeric
            Position to compute the Hessian.
        """
        return np.array([p.hessian(x) for p in self.values()]).sum(axis=0)

class CartesianCompositePotential(CompositePotential, CartesianPotential):
    pass<|endoftext|>


<commit_before>from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Travel_group(models.Model):
	group_name = models.CharField(max_length = 200)
	text = models.TextField()
	created_date = models.DateTimeField(default=timezone.now)

	def __str__(self):              # __unicode__ on Python 2
		return self.group_name
	def publish(self):
		self.save()

	class Meta:
		ordering = ('group_name',)

class Profile(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE)
	bio = models.TextField(max_length=500, blank=True)
	location = models.CharField(max_length=30, blank=True)
	birth_date = models.DateField(null=True, blank=True)
	travel_groups = models.ManyToManyField(Travel_group)

@receiver(post_save, sender=User) #декоратор для обновления
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)#автоматически обновляться
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()<commit_msg>fix for django 2.0<commit_after>from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Travel_group(models.Model):
	group_name = models.CharField(max_length = 200)
	text = models.TextField()
	created_date = models.DateTimeField(default=timezone.now)

	def __str__(self):              # __unicode__ on Python 2
		return self.group_name
	def publish(self):
		self.save()

	class Meta:
		ordering = ('group_name',)

class Profile(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE)
	bio = models.TextField(max_length=500, blank=True)
	location = models.CharField(max_length=30, blank=True)
	birth_date = models.DateField(null=True, blank=True)
	travel_groups = models.ManyToManyField(Travel_group)

@receiver(post_save, sender=User) #декоратор для обновления
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)#автоматически обновляться
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
<|endoftext|>


<commit_before>"""
Flask-restful based API implementation for re:dash.

Currently the Flask server is used to serve the static assets (and the Angular.js app),
but this is only due to configuration issues and temporary.
"""
import csv
import hashlib
import json
import numbers
import cStringIO
import datetime
import dateutil.parser

from flask import g, render_template, send_from_directory, make_response, request, jsonify
from flask.ext.restful import Resource, abort

import sqlparse
from redash import settings, utils
from redash import data

from redash import app, auth, api, redis_connection, data_manager
from redash import models

@app.route('/ping', methods=['GET'])
def ping():
    return 'PONG.'

@app.route('/admin/<anything>')
@app.route('/dashboard/<anything>')
@app.route('/queries')
@app.route('/queries/<anything>')
@app.route('/')
@auth.required
def index(anything=None):
    email_md5 = hashlib.md5(g.user['email'].lower()).hexdigest()
    gravatar_url = "https://www.gravatar.com/avatar/%s?s=40" % email_md5

    user = {
        'gravatar_url': gravatar_url,
        'is_admin': g.user['email'] in settings.ADMINS,
        'name': g.user['email']
    }

    return render_template("index.html", user=json.dumps(user), analytics=settings.ANALYTICS)

@app.route('/status.json')
@auth.required
def status_api():
    status = {}
    info = redis_connection.info()
    status['redis_used_memory'] = info['used_memory_human']

    status['queries_count'] = models.Query.select().count()
    status['query_results_count'] = models.QueryResult.select().count()
    status['dashboards_count'] = models.Dashboard.select().count()
    status['widgets_count'] = models.Widget.select().count()

    status['workers'] = [redis_connection.hgetall(w)
                         for w in redis_connection.smembers('workers')]

    manager_status = redis_connection.hgetall('manager:status')
    status['manager'] = manager_status
    status['manager']['queue_size'] = redis_connection.zcard('jobs')

    return jsonify(status)

@app.route('/api/queries/format', methods=['POST'])
@auth.required
def format_sql_query(self):
    arguments = json.loads(self.request.body)
    query = arguments.get("query", "")

    return sqlparse.format(query, reindent=True, keyword_case='upper')

class BaseResource(Resource):
    decorators = [auth.required]

    @property
    def current_user(self):
        return g.user['email']

class DashboardListAPI(BaseResource):
    def get(self):
        dashboards = [d.to_dict() for d in
                      models.Dashboard.select().where(models.Dashboard.is_archived==False)]

        return dashboards

    def post(self):
        dashboard_properties = request.get_json(force=True)
        dashboard = models.Dashboard(name=dashboard_properties['name'],
                                     user=self.current_user,
                                     layout='[]')
        dashboard.save()
        return dashboard.to_dict()

class DashboardAPI(BaseResource):
    def get(self, dashboard_slug=None):
        # TODO: prefetching of widgets and queries?
        try:
            dashboard = models.Dashboard.get_by_slug(dashboard_slug)
        except models.Dashboard.DoesNotExist:
            abort(404)

        return dashboard.to_dict(with_widgets=True)

    def post(self, dashboard_slug):
        # TODO: either convert all requests to use slugs or ids
        dashboard_properties = request.get_json(force=True)
        dashboard = models.Dashboard.get(models.Dashboard.id == dashboard_slug)
        dashboard.layout = dashboard_properties['layout']
        dashboard.name = dashboard_properties['name']
        dashboard.save()

        return dashboard.to_dict(with_widgets=True)

    def delete(self, dashboard_slug):
        dashboard = models.Dashboard.get_by_slug(dashboard_slug)
        dashboard.is_archived = True
        dashboard.save()

api.add_resource(DashboardListAPI, '/api/dashboards', endpoint='dashboards')
api.add_resource(DashboardAPI, '/api/dashboards/<dashboard_slug>', endpoint='dashboard')

class WidgetListAPI(BaseResource):
    def post(self):
        widget_properties = request.get_json(force=True)
        widget_properties['options'] = json.dumps(widget_properties['options'])
        widget = models.Widget(**widget_properties)
        widget.save()

        layout = json.loads(widget.dashboard.layout)
        new_row = True

        if len(layout) == 0 or widget.width == 2:
            layout.append([widget.id])
        elif len(layout[-1]) == 1:
            neighbour_widget = models.Widget.get(models.Widget.id == layout[-1][0])
            if neighbour_widget.width == 1:
                layout[-1].append(widget.id)
                new_row = False
            else:
                layout.append([widget.id])
        else:
            layout.append([widget.id])

        widget.dashboard.layout = json.dumps(layout)
        widget.dashboard.save()

        return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row}

class WidgetAPI(BaseResource):
    def delete(self, widget_id):
        widget = models.Widget.get(models.Widget.id == widget_id)
        # TODO: reposition existing ones
        layout = json.loads(widget.dashboard.layout)
        layout = map(lambda row: filter(lambda w: w != widget_id, row), layout)
        layout = filter(lambda row: len(row) > 0, layout)
        widget.dashboard.layout = json.dumps(layout)
        widget.dashboard.save()

        widget.delete()

api.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')
api.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')

class QueryListAPI(BaseResource):
    def post(self):
        query_def = request.json
        if 'created_at' in query_def:
            query_def['created_at'] = dateutil.parser.parse(query_def['created_at'])

        query_def.pop('latest_query_data', None)

        query_def['user'] = self.current_user
        query = models.Query(**query_def)
        query.save()

        return query.to_dict(with_result=False)

    def get(self):
        return [q.to_dict(with_result=False, with_stats=True) for q in models.Query.all_queries()]

class QueryAPI(BaseResource):
    def post(self, query_id):
        query_def = request.json
        if 'created_at' in query_def:
            query_def['created_at'] = dateutil.parser.parse(query_def['created_at'])

        query_def.pop('latest_query_data', None)

        query = models.Query(**query_def)
        fields = query_def.keys()
        fields.remove('id')
        # model.save(only=model.dirty_fields)
        query.save(only=fields)

        return query.to_dict(with_result=False)

    def get(self, query_id):
        q = models.Query.get(models.Query.id == query_id)
        if q:
            return q.to_dict()
        else:
            abort(404, message="Query not found.")

api.add_resource(QueryListAPI, '/api/queries', endpoint='queries')
api.add_resource(QueryAPI, '/api/queries/<query_id>', endpoint='query')

class QueryResultListAPI(BaseResource):
    def post(self):
        params = request.json

        if params['ttl'] == 0:
            query_result = None
        else:
            query_result = data_manager.get_query_result(params['query'], int(params['ttl']))

        if query_result:
            return {'query_result': query_result.to_dict(parse_data=True)}
        else:
            job = data_manager.add_job(params['query'], data.Job.HIGH_PRIORITY)
            return {'job': job.to_dict()}

class QueryResultAPI(BaseResource):
    def get(self, query_result_id):
        query_result = data_manager.get_query_result_by_id(query_result_id)
        if query_result:
            return {'query_result': query_result.to_dict(parse_data=True)}
        else:
            abort(404)

class CsvQueryResultsAPI(BaseResource):
    def get(self, query_id, query_result_id=None):
        if not query_result_id:
            query = models.Query.get(models.Query.id == query_id)
            if query:
                query_result_id = query.latest_query_data_id

        query_result = query_result_id and data_manager.get_query_result_by_id(query_result_id)
        if query_result:
            s = cStringIO.StringIO()

            query_data = json.loads(query_result.data)
            writer = csv.DictWriter(s, fieldnames=[col['name'] for col in query_data['columns']])
            writer.writer = utils.UnicodeWriter(s)
            writer.writeheader()
            for row in query_data['rows']:
                for k, v in row.iteritems():
                    if isinstance(v, numbers.Number) and (v > 1000 * 1000 * 1000 * 100):
                        row[k] = datetime.datetime.fromtimestamp(v/1000.0)

                writer.writerow(row)

            return make_response(s.getvalue(), 200, {'Content-Type': "text/csv; charset=UTF-8"})
        else:
            abort(404)

api.add_resource(CsvQueryResultsAPI, '/api/queries/<query_id>/results/<query_result_id>.csv',
                 '/api/queries/<query_id>/results.csv',
                 endpoint='csv_query_results')
api.add_resource(QueryResultListAPI, '/api/query_results', endpoint='query_results')
api.add_resource(QueryResultAPI, '/api/query_results/<query_result_id>', endpoint='query_result')

class JobAPI(BaseResource):
    def get(self, job_id):
        # TODO: if finished, include the query result
        job = data.Job.load(data_manager.redis_connection, job_id)
        return {'job': job.to_dict()}

    def delete(self, job_id):
        job = data.Job.load(data_manager.redis_connection, job_id)
        job.cancel()

api.add_resource(JobAPI, '/api/jobs/<job_id>', endpoint='job')

@app.route('/<path:filename>')
@auth.required
def send_static(filename):
    return send_from_directory(settings.STATIC_ASSETS_PATH, filename)

if __name__ == '__main__':
    app.run(debug=True)

<commit_msg>Visualization handlers.<commit_after>"""
Flask-restful based API implementation for re:dash.

Currently the Flask server is used to serve the static assets (and the Angular.js app),
but this is only due to configuration issues and temporary.
"""
import csv
import hashlib
import json
import numbers
import cStringIO
import datetime
import dateutil.parser

from flask import g, render_template, send_from_directory, make_response, request, jsonify
from flask.ext.restful import Resource, abort

import sqlparse
from redash import settings, utils
from redash import data

from redash import app, auth, api, redis_connection, data_manager
from redash import models

@app.route('/ping', methods=['GET'])
def ping():
    return 'PONG.'

@app.route('/admin/<anything>')
@app.route('/dashboard/<anything>')
@app.route('/queries')
@app.route('/queries/<anything>')
@app.route('/')
@auth.required
def index(anything=None):
    email_md5 = hashlib.md5(g.user['email'].lower()).hexdigest()
    gravatar_url = "https://www.gravatar.com/avatar/%s?s=40" % email_md5

    user = {
        'gravatar_url': gravatar_url,
        'is_admin': g.user['email'] in settings.ADMINS,
        'name': g.user['email']
    }

    return render_template("index.html", user=json.dumps(user), analytics=settings.ANALYTICS)

@app.route('/status.json')
@auth.required
def status_api():
    status = {}
    info = redis_connection.info()
    status['redis_used_memory'] = info['used_memory_human']

    status['queries_count'] = models.Query.select().count()
    status['query_results_count'] = models.QueryResult.select().count()
    status['dashboards_count'] = models.Dashboard.select().count()
    status['widgets_count'] = models.Widget.select().count()

    status['workers'] = [redis_connection.hgetall(w)
                         for w in redis_connection.smembers('workers')]

    manager_status = redis_connection.hgetall('manager:status')
    status['manager'] = manager_status
    status['manager']['queue_size'] = redis_connection.zcard('jobs')

    return jsonify(status)

@app.route('/api/queries/format', methods=['POST'])
@auth.required
def format_sql_query(self):
    arguments = json.loads(self.request.body)
    query = arguments.get("query", "")

    return sqlparse.format(query, reindent=True, keyword_case='upper')

class BaseResource(Resource):
    decorators = [auth.required]

    @property
    def current_user(self):
        return g.user['email']

class DashboardListAPI(BaseResource):
    def get(self):
        dashboards = [d.to_dict() for d in
                      models.Dashboard.select().where(models.Dashboard.is_archived==False)]

        return dashboards

    def post(self):
        dashboard_properties = request.get_json(force=True)
        dashboard = models.Dashboard(name=dashboard_properties['name'],
                                     user=self.current_user,
                                     layout='[]')
        dashboard.save()
        return dashboard.to_dict()

class DashboardAPI(BaseResource):
    def get(self, dashboard_slug=None):
        # TODO: prefetching of widgets, visualizations and query results?
        try:
            dashboard = models.Dashboard.get_by_slug(dashboard_slug)
        except models.Dashboard.DoesNotExist:
            abort(404)

        return dashboard.to_dict(with_widgets=True)

    def post(self, dashboard_slug):
        # TODO: either convert all requests to use slugs or ids
        dashboard_properties = request.get_json(force=True)
        dashboard = models.Dashboard.get(models.Dashboard.id == dashboard_slug)
        dashboard.layout = dashboard_properties['layout']
        dashboard.name = dashboard_properties['name']
        dashboard.save()

        return dashboard.to_dict(with_widgets=True)

    def delete(self, dashboard_slug):
        dashboard = models.Dashboard.get_by_slug(dashboard_slug)
        dashboard.is_archived = True
        dashboard.save()

api.add_resource(DashboardListAPI, '/api/dashboards', endpoint='dashboards')
api.add_resource(DashboardAPI, '/api/dashboards/<dashboard_slug>', endpoint='dashboard')

class WidgetListAPI(BaseResource):
    def post(self):
        widget_properties = request.get_json(force=True)
        widget_properties['options'] = json.dumps(widget_properties['options'])
        widget = models.Widget(**widget_properties)
        widget.save()

        layout = json.loads(widget.dashboard.layout)
        new_row = True

        if len(layout) == 0 or widget.width == 2:
            layout.append([widget.id])
        elif len(layout[-1]) == 1:
            neighbour_widget = models.Widget.get(models.Widget.id == layout[-1][0])
            if neighbour_widget.width == 1:
                layout[-1].append(widget.id)
                new_row = False
            else:
                layout.append([widget.id])
        else:
            layout.append([widget.id])

        widget.dashboard.layout = json.dumps(layout)
        widget.dashboard.save()

        return {'widget': widget.to_dict(), 'layout': layout, 'new_row': new_row}

class WidgetAPI(BaseResource):
    def delete(self, widget_id):
        widget = models.Widget.get(models.Widget.id == widget_id)
        # TODO: reposition existing ones
        layout = json.loads(widget.dashboard.layout)
        layout = map(lambda row: filter(lambda w: w != widget_id, row), layout)
        layout = filter(lambda row: len(row) > 0, layout)
        widget.dashboard.layout = json.dumps(layout)
        widget.dashboard.save()

        widget.delete()

api.add_resource(WidgetListAPI, '/api/widgets', endpoint='widgets')
api.add_resource(WidgetAPI, '/api/widgets/<int:widget_id>', endpoint='widget')

class QueryListAPI(BaseResource):
    def post(self):
        query_def = request.json
        if 'created_at' in query_def:
            query_def['created_at'] = dateutil.parser.parse(query_def['created_at'])

        query_def.pop('latest_query_data', None)
        query_def.pop('visualizations', None)

        query_def['user'] = self.current_user
        query = models.Query(**query_def)
        query.save()

        return query.to_dict(with_result=False)

    def get(self):
        return [q.to_dict(with_result=False, with_stats=True) for q in models.Query.all_queries()]

class QueryAPI(BaseResource):
    def post(self, query_id):
        query_def = request.json
        if 'created_at' in query_def:
            query_def['created_at'] = dateutil.parser.parse(query_def['created_at'])

        query_def.pop('latest_query_data', None)

        query = models.Query(**query_def)
        fields = query_def.keys()
        fields.remove('id')
        # model.save(only=model.dirty_fields)
        query.save(only=fields)

        return query.to_dict(with_result=False)

    def get(self, query_id):
        q = models.Query.get(models.Query.id == query_id)
        if q:
            return q.to_dict(with_visualizations=True)
        else:
            abort(404, message="Query not found.")

api.add_resource(QueryListAPI, '/api/queries', endpoint='queries')
api.add_resource(QueryAPI, '/api/queries/<query_id>', endpoint='query')

class VisualizationListAPI(BaseResource):
    def post(self):
        kwargs = request.get_json(force=True)
        kwargs['options'] = json.dumps(kwargs['options'])

        vis = data.models.Visualization(**kwargs)
        vis.save()

        return vis.to_dict(with_query=False)

class VisualizationAPI(BaseResource):
    def post(self, visualization_id):
        kwargs = request.get_json(force=True)
        kwargs['options'] = json.dumps(kwargs['options'])

        vis = models.Visualization(**kwargs)
        fields = kwargs.keys()
        fields.remove('id')
        vis.save(only=fields)

        return vis.to_dict(with_query=False)

    def delete(self, visualization_id):
        vis = models.Visualization.get(models.Visualization.id == visualization_id)
        vis.delete()

api.add_resource(VisualizationListAPI, '/api/visualizations', endpoint='visualizations')
api.add_resource(VisualizationAPI, '/api/visualizations/<visualization_id>', endpoint='visualization')

class QueryResultListAPI(BaseResource):
    def post(self):
        params = request.json

        if params['ttl'] == 0:
            query_result = None
        else:
            query_result = data_manager.get_query_result(params['query'], int(params['ttl']))

        if query_result:
            return {'query_result': query_result.to_dict(parse_data=True)}
        else:
            job = data_manager.add_job(params['query'], data.Job.HIGH_PRIORITY)
            return {'job': job.to_dict()}

class QueryResultAPI(BaseResource):
    def get(self, query_result_id):
        query_result = data_manager.get_query_result_by_id(query_result_id)
        if query_result:
            return {'query_result': query_result.to_dict(parse_data=True)}
        else:
            abort(404)

class CsvQueryResultsAPI(BaseResource):
    def get(self, query_id, query_result_id=None):
        if not query_result_id:
            query = models.Query.get(models.Query.id == query_id)
            if query:
                query_result_id = query.latest_query_data_id

        query_result = query_result_id and data_manager.get_query_result_by_id(query_result_id)
        if query_result:
            s = cStringIO.StringIO()

            query_data = json.loads(query_result.data)
            writer = csv.DictWriter(s, fieldnames=[col['name'] for col in query_data['columns']])
            writer.writer = utils.UnicodeWriter(s)
            writer.writeheader()
            for row in query_data['rows']:
                for k, v in row.iteritems():
                    if isinstance(v, numbers.Number) and (v > 1000 * 1000 * 1000 * 100):
                        row[k] = datetime.datetime.fromtimestamp(v/1000.0)

                writer.writerow(row)

            return make_response(s.getvalue(), 200, {'Content-Type': "text/csv; charset=UTF-8"})
        else:
            abort(404)

api.add_resource(CsvQueryResultsAPI, '/api/queries/<query_id>/results/<query_result_id>.csv',
                 '/api/queries/<query_id>/results.csv',
                 endpoint='csv_query_results')
api.add_resource(QueryResultListAPI, '/api/query_results', endpoint='query_results')
api.add_resource(QueryResultAPI, '/api/query_results/<query_result_id>', endpoint='query_result')

class JobAPI(BaseResource):
    def get(self, job_id):
        # TODO: if finished, include the query result
        job = data.Job.load(data_manager.redis_connection, job_id)
        return {'job': job.to_dict()}

    def delete(self, job_id):
        job = data.Job.load(data_manager.redis_connection, job_id)
        job.cancel()

api.add_resource(JobAPI, '/api/jobs/<job_id>', endpoint='job')

@app.route('/<path:filename>')
@auth.required
def send_static(filename):
    return send_from_directory(settings.STATIC_ASSETS_PATH, filename)

if __name__ == '__main__':
    app.run(debug=True)

<|endoftext|>


<commit_before>from .base import BaseChart
import json

from django.template.loader import render_to_string
from ..utils import JSONEncoderForHTML

class BaseHighCharts(BaseChart):
    def get_html_template(self):
        return "graphos/highcharts/html.html"

    def get_js_template(self):
        return "graphos/highcharts/js.html"

    def get_series(self):
        data = self.get_data()
        series_names = data[0][1:]
        serieses = []
        for i, name in enumerate(series_names):
            serieses.append({"name": name, "data": column(data, i+1)[1:]})
        return json.dumps(serieses, cls=JSONEncoderForHTML)

    def get_categories(self):
        return json.dumps(column(self.get_data(), 0)[1:], cls=JSONEncoderForHTML)

    def get_x_axis_title(self):
        return self.get_data()[0][0]

class LineChart(BaseHighCharts):
    def get_chart_type(self):
        return "line"

class BarChart(BaseHighCharts):
    def get_chart_type(self):
        return "bar"

class ColumnChart(BaseHighCharts):
    def get_chart_type(self):
        return "column"

class PieChart(BaseHighCharts):
    def get_series(self):
        data = self.get_data()
        series_names = data[0][1:]
        serieses = []
        for i, name in enumerate(series_names):
            serieses.append({"name": name, "data": pie_column(data, i+1)[1:]})
        return json.dumps(serieses)

    def get_chart_type(self):
        return "pie"

class AreaChart(BaseHighCharts):
    def get_chart_type(self):
        return "area"

class DonutChart(BaseHighCharts):
    def get_series(self):
        _data = super(DonutChart, self).get_data()
        return json.dumps(_data[1:])

    def get_js_template(self):
        return "graphos/highcharts/js_donut.html"

    def get_chart_type(self):
        return "pie"

class ScatterChart(BaseHighCharts):
    def get_chart_type(self):
        return "scatter"

class LogarithmicChart(BaseHighCharts):
    def get_series(self):
        data = super(LogarithmicChart, self).get_series()
        data = json.loads(data)[0].get('data')
        return json.dumps(data)

    def get_js_template(self):
        return "graphos/highcharts/js_log.html"

    def get_chart_type(self):
        return "log_chart"

class MultiAxisChart(BaseHighCharts):
    def get_series(self):
        data = super(MultiAxisChart, self).get_series()
        return [x.get('data') for x in json.loads(data)]

    def get_y_axis_titles(self):
        data = super(MultiAxisChart, self).get_series()
        return [x.get('name') for x in json.loads(data)]

    def get_js_template(self):
        return "graphos/highcharts/js_dual_axis.html"

    def get_chart_type(self):
        return "multi_axis"

def column(matrix, i):
    return [row[i] for row in matrix]

def pie_column(matrix, i):
    return [{'name':row[0],'y':row[1]} for row in matrix]
<commit_msg>Adding json dumps to MultiAxis Highchart<commit_after>from .base import BaseChart
import json

from django.template.loader import render_to_string
from ..utils import JSONEncoderForHTML

class BaseHighCharts(BaseChart):
    def get_html_template(self):
        return "graphos/highcharts/html.html"

    def get_js_template(self):
        return "graphos/highcharts/js.html"

    def get_series(self):
        data = self.get_data()
        series_names = data[0][1:]
        serieses = []
        for i, name in enumerate(series_names):
            serieses.append({"name": name, "data": column(data, i+1)[1:]})
        return json.dumps(serieses, cls=JSONEncoderForHTML)

    def get_categories(self):
        return json.dumps(column(self.get_data(), 0)[1:], cls=JSONEncoderForHTML)

    def get_x_axis_title(self):
        return self.get_data()[0][0]

class LineChart(BaseHighCharts):
    def get_chart_type(self):
        return "line"

class BarChart(BaseHighCharts):
    def get_chart_type(self):
        return "bar"

class ColumnChart(BaseHighCharts):
    def get_chart_type(self):
        return "column"

class PieChart(BaseHighCharts):
    def get_series(self):
        data = self.get_data()
        series_names = data[0][1:]
        serieses = []
        for i, name in enumerate(series_names):
            serieses.append({"name": name, "data": pie_column(data, i+1)[1:]})
        return json.dumps(serieses)

    def get_chart_type(self):
        return "pie"

class AreaChart(BaseHighCharts):
    def get_chart_type(self):
        return "area"

class DonutChart(BaseHighCharts):
    def get_series(self):
        _data = super(DonutChart, self).get_data()
        return json.dumps(_data[1:])

    def get_js_template(self):
        return "graphos/highcharts/js_donut.html"

    def get_chart_type(self):
        return "pie"

class ScatterChart(BaseHighCharts):
    def get_chart_type(self):
        return "scatter"

class LogarithmicChart(BaseHighCharts):
    def get_series(self):
        data = super(LogarithmicChart, self).get_series()
        data = json.loads(data)[0].get('data')
        return json.dumps(data)

    def get_js_template(self):
        return "graphos/highcharts/js_log.html"

    def get_chart_type(self):
        return "log_chart"

class MultiAxisChart(BaseHighCharts):
    def get_series(self):
        data = super(MultiAxisChart, self).get_series()
        return json.dumps([x.get('data') for x in json.loads(data)])

    def get_y_axis_titles(self):
        data = super(MultiAxisChart, self).get_series()
        return [x.get('name') for x in json.loads(data)]

    def get_js_template(self):
        return "graphos/highcharts/js_dual_axis.html"

    def get_chart_type(self):
        return "multi_axis"

def column(matrix, i):
    return [row[i] for row in matrix]

def pie_column(matrix, i):
    return [{'name':row[0],'y':row[1]} for row in matrix]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from blog.models import post, tag

def feed(request):
    posts = post.objects.all()
    updated = post.objects.latest('pub_date').pub_date
    return render_to_response('feed.xml', {'posts': posts, 'updated': updated})

def archive(request):
    posts = post.objects.all()
    return render_to_response('archive.html', {'posts': posts, 'title':'Archive'})

class index_view(ListView):
    model = post
    template_name = 'index.html'
    context_object_name = 'posts'
    paginate_by = 3

    def get_queryset(self, **kwargs):
        return post.objects.all()

    def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        title = ''
        context['title'] = title
        return context

class tag_view(ListView):
    model = post
    template_name = 'tag.html'
    context_object_name = 'posts'
    paginate_by = 3

    def get_queryset(self, **kwargs):
        t = tag.objects.get(name=self.kwargs.get('slug'))
        return post.objects.filter(tag=t).all()

    def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        title = self.kwargs.get('slug')
        context['tag'] = tag.objects.get(name=title)
        context['title'] = title
        return context

class post_view(DetailView):
    model = post
    template_name = 'post.html'
    context_object_name = 'post'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        current = context['post']
        current.click += 1
        current.save()

        try:
            next_post = current.get_next_by_pub_date()
        except:
            next_post = None

        try:
            previous_post = current.get_previous_by_pub_date()
        except:
            previous_post = None

        context['next'] = next_post
        context['previous'] = previous_post
        context['title'] = current.title

        return context
<commit_msg>Update views.py<commit_after># -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from blog.models import post, tag

def feed(request):
    posts = post.objects.hidden(hidden=False).all()
    updated = post.objects.latest('pub_date').pub_date
    return render_to_response('feed.xml', {'posts': posts, 'updated': updated})

def archive(request):
    posts = post.objects.filter(hidden=False).all()
    return render_to_response('archive.html', {'posts': posts, 'title':'Archive'})

class index_view(ListView):
    model = post
    template_name = 'index.html'
    context_object_name = 'posts'
    paginate_by = 3

    def get_queryset(self, **kwargs):
        return post.objects.filter(hidden=False).all()

    def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        title = ''
        context['title'] = title
        return context

class tag_view(ListView):
    model = post
    template_name = 'tag.html'
    context_object_name = 'posts'
    paginate_by = 3

    def get_queryset(self, **kwargs):
        t = tag.objects.get(name=self.kwargs.get('slug'))
        return post.objects.filter(tag=t, hidden=False).all()

    def get_context_data(self, **kwargs):
        context = super(ListView, self).get_context_data(**kwargs)
        title = self.kwargs.get('slug')
        context['tag'] = tag.objects.get(name=title)
        context['title'] = title
        return context

class post_view(DetailView):
    model = post
    template_name = 'post.html'
    context_object_name = 'post'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        current = context['post']
        current.click += 1
        current.save()

        try:
            next_post = current.get_next_by_pub_date()
        except:
            next_post = None

        try:
            previous_post = current.get_previous_by_pub_date()
        except:
            previous_post = None

        context['next'] = next_post
        context['previous'] = previous_post
        context['title'] = current.title

        return context
<|endoftext|>


<commit_before>"""
Owner stuff

Originally made by luna for Jose, the best bot
"""
import traceback
import asyncio
# noinspection PyPackageRequirements
import discord

# noinspection PyPackageRequirements
from discord.ext import commands

class Owner:
    def __init__(self, bot):
        self.bot = bot

    @commands.command(hidden=True)
    @commands.is_owner()
    async def game(self, ctx, *, game: str):
        """Change playing status"""
        haha = ctx.bot.config['prefix']
        try:
            await self.bot.change_presence(
                game=discord.Game(name=f'{game} | {haha}help | v1.1', type=1, url='https://twitch.tv/monstercat'))
        except Exception:
            await ctx.send(f'```\n{traceback.format_exc()}\n```')
        else:
            await ctx.send(':white_check_mark: Successfully changed game')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def shutdown(self, ctx):
        await ctx.send(":wave:")
        await self.bot.session.close()
        await self.bot.logout()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def load(self, ctx, extension_name: str):
        """Loads a cog"""
        try:
            self.bot.load_extension('cogs.' + extension_name)
        except ModuleNotFoundError:
            await ctx.send(f':x: Cog `{extension_name}` not found.')
            return
        except Exception:
            await ctx.send(f'```py\n{traceback.format_exc()}\n```')
            return
        logging.info(f'owner[load]: cog {extension_name} loaded')
        await ctx.send(f':ok_hand: `{extension_name}` loaded.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def unload(self, ctx, extension_name: str):
        """Unloads a cog"""
        self.bot.unload_extension('cogs.' + extension_name)
        logging.info(f'owner[unload]: cog {extension_name} unloaded')
        await ctx.send(f':ok_hand: `{extension_name}` unloaded.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def reload(self, ctx, extension_name: str):
        """Reloads a cog"""
        try:
            self.bot.unload_extension('cogs.' + extension_name)
            self.bot.load_extension('cogs.' + extension_name)
        except ModuleNotFoundError:
            await ctx.send(f':x: Cog `{extension_name}` not found.')
            return
        except Exception:
            await ctx.send(f'```{traceback.format_exc()}```')
            return
        logging.info(f'owner[reload]: cog {extension_name} reloaded')
        await ctx.send(f':ok_hand: Reloaded `{extension_name}`')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def shell(self, ctx, *, command: str):
        """Run stuff"""
        with ctx.typing():
            p = await asyncio.create_subprocess_shell(command,
                                                      stderr=asyncio.subprocess.PIPE,
                                                      stdout=asyncio.subprocess.PIPE,
                                                      )

            out, err = map(lambda s: s.decode('utf-8'), await p.communicate())

        result = f'{out}{err}'
        await ctx.send(f"`{command}`: ```{result}```\n")

    @commands.command(hidden=True)
    @commands.is_owner()
    async def update(self, ctx):
        """Lazy much? Whatever..."""
        await ctx.invoke(self.bot.get_command('shell'), command='git pull')

def setup(bot):
    bot.add_cog(Owner(bot))
<commit_msg>cogs[owner]: add reboot command<commit_after>"""
Owner stuff

Originally made by luna for Jose, the best bot
"""
import os
import traceback
import asyncio
# noinspection PyPackageRequirements
import discord

# noinspection PyPackageRequirements
from discord.ext import commands

async def run_cmd(cmd: str) -> str:
    """Runs a subprocess and returns the output."""
    process = await asyncio.create_subprocess_shell(
        cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
    results = await process.communicate()
    return "".join(x.decode("utf-8") for x in results)

class Owner:
    def __init__(self, bot):
        self.bot = bot

    @commands.command(hidden=True)
    @commands.is_owner()
    async def game(self, ctx, *, game: str):
        """Change playing status"""
        haha = ctx.bot.config['prefix']
        try:
            await self.bot.change_presence(
                game=discord.Game(name=f'{game} | {haha}help | v1.1', type=1, url='https://twitch.tv/monstercat'))
        except Exception:
            await ctx.send(f'```\n{traceback.format_exc()}\n```')
        else:
            await ctx.send(':white_check_mark: Successfully changed game')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def shutdown(self, ctx):
        """Shuts down the bot"""
        await self.bot.session.close()
        await self.bot.logout()

    @commands.command(hidden=True)
    @commands.is_owner()
    async def reboot(self, ctx):
        """Reboots the bot.
        Can only be used with PM2"""
        restart_land = discord.Embed(title='Restarting', description='Please wait...')
        restart_msg = await ctx.send(embed=restart_land)
        pm2_id = os.environ.get['pm_id']
        if pm2_id:
            await run_cmd(f'pm2 restart {pm2_id}')
            await restart_msg.edit(content='Sending message to pm2. Bye!')
        else:
            await restart_message.edit(content=':warning: pm2 not detected, invoking `shutdown` command')
            await ctx.invoke(self.bot.get_command('shutdown'))

    @commands.command(hidden=True)
    @commands.is_owner()
    async def load(self, ctx, extension_name: str):
        """Loads a cog"""
        try:
            self.bot.load_extension('cogs.' + extension_name)
        except ModuleNotFoundError:
            await ctx.send(f':x: Cog `{extension_name}` not found.')
            return
        except Exception:
            await ctx.send(f'```py\n{traceback.format_exc()}\n```')
            return
        logging.info(f'owner[load]: cog {extension_name} loaded')
        await ctx.send(f':ok_hand: `{extension_name}` loaded.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def unload(self, ctx, extension_name: str):
        """Unloads a cog"""
        self.bot.unload_extension('cogs.' + extension_name)
        logging.info(f'owner[unload]: cog {extension_name} unloaded')
        await ctx.send(f':ok_hand: `{extension_name}` unloaded.')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def reload(self, ctx, extension_name: str):
        """Reloads a cog"""
        try:
            self.bot.unload_extension('cogs.' + extension_name)
            self.bot.load_extension('cogs.' + extension_name)
        except ModuleNotFoundError:
            await ctx.send(f':x: Cog `{extension_name}` not found.')
            return
        except Exception:
            await ctx.send(f'```{traceback.format_exc()}```')
            return
        logging.info(f'owner[reload]: cog {extension_name} reloaded')
        await ctx.send(f':ok_hand: Reloaded `{extension_name}`')

    @commands.command(hidden=True)
    @commands.is_owner()
    async def shell(self, ctx, *, command: str):
        """Run stuff"""
        with ctx.typing():
            p = await asyncio.create_subprocess_shell(command,
                                                      stderr=asyncio.subprocess.PIPE,
                                                      stdout=asyncio.subprocess.PIPE,
                                                      )

            out, err = map(lambda s: s.decode('utf-8'), await p.communicate())

        result = f'{out}{err}'
        await ctx.send(f"`{command}`: ```{result}```\n")

    @commands.command(hidden=True)
    @commands.is_owner()
    async def update(self, ctx):
        """Lazy much? Whatever..."""
        await ctx.invoke(self.bot.get_command('shell'), command='git pull')

def setup(bot):
    bot.add_cog(Owner(bot))
<|endoftext|>