<commit_before>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame, PolygonAsset
from math import pi, cos, sin, atan2, sqrt
import time
class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, .2)
        noline = LineStyle(0, black)
        bg_asset = RectangleAsset(self.width, self.height, noline, black)

        bg = Sprite(bg_asset, (0,0))
        global SCREEN_WIDTH1, SCREEN_HEIGHT
        SCREEN_WIDTH1=self.width
        SCREEN_HEIGHT = self.height

    def step(self):
        for x in self.getSpritesbyClass(plasmaBolt):
            x.step()
        global swordlist, bulletlist, maintain
        if turn == 1:

            maintain = False
            for ship in self.getSpritesbyClass(sword):
                ship.destroy()
            for ship in self.getSpritesbyClass(bullet):
                ship.destroy()
            meleeSprite1.step()
            shootSprite1.step()
            MC1.step()

            global turn
            turn = 0
        if t == 0:
            MC1.hit2()

        if maintain == True:
            for ship in self.getSpritesbyClass(shootSprite):
                ship.reload1()

myapp = SpaceGame(0,0)
hit = 0
t=1
maintain = False
lives = 4
heartlist = list(range(4))

black = Color(0, 1)
speed = 10
speed1=4
red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
swordlist = []
bulletlist =[]
thinline = LineStyle(1, black)
white = Color(0xffffff, 1)
gray = Color(0x8c8c8c, 1)
noline = LineStyle(0, black)
thinline1 = LineStyle(1, white)
dead_asset = RectangleAsset(SCREEN_WIDTH1, SCREEN_HEIGHT, noline, red)
cf = PolygonAsset(((-10,-15),(0,-22.5),(10,-15),(10,15),(-10,15)), thinline, gray)
gameover = ImageAsset("game_over___pixel_art_by_tfcb93-d513cay.png")
ms = PolygonAsset(((-7.5,-11.5),(7.5,-11.5),(7.5,11.5),(-7.5,11.5)), thinline, red)
ss = PolygonAsset(((-7.5,-11.5),(7.5,-11.5),(7.5,11.5),(-7.5,11.5)), thinline, blue)
smlSword = PolygonAsset(((-2.5,-5),(2.5,-5),(2.5,5),(-2.5,5)), thinline, red)
smlBullet = PolygonAsset(((-2.5,0),(2.5,0),(2.5,-300),(-2.5,-300)), thinline, blue)
class MC(Sprite):
    def __init__(self, position, ls):
        super().__init__(cf, position)
        self.moves = speed1
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "j", self.jKey)
        SpaceGame.listenKeyEvent("keydown", "k", self.kKey)
        self.fxcenter = self.fycenter = 0.5
        self.lives = ls
        self.start=0
        self.end=0
        self.dead = 0
        self.go = 0
        self.Sprites = []
    def jKey(self, event):
        self.KILL()
        if self.moves >1:
            self.Sprites.append(axe((self.x-19*cos((pi/2)-self.rotation), self.y -19*sin((pi/2)-self.rotation)), self.rotation))
            self.moves -=2
    def kKey(self, event):
        self.KILL()
        if self.moves >1:
            plasmaBolt((self.x-19*cos((pi/2)-self.rotation), self.y -19*sin((pi/2)-self.rotation)), self.rotation)
            self.moves -=2
    def dKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x +speed*cos(self.rotation)<SCREEN_WIDTH1 and self.y -speed*sin(self.rotation) >0:
            self.x += speed*cos(self.rotation)
            self.y -= speed*sin(self.rotation)
            self.moves -=1
    def aKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x -speed*cos(self.rotation)>0 and self.y +speed*sin(self.rotation) <SCREEN_HEIGHT:
            self.x -= speed*cos(self.rotation)
            self.y += speed*sin(self.rotation)

            self.moves -=1
    def sKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x +speed*cos((pi/2)-self.rotation)<SCREEN_WIDTH1 and self.y +speed*sin((pi/2)-self.rotation) <SCREEN_HEIGHT:
            self.x += speed*cos((pi/2)-self.rotation)
            self.y += speed*sin((pi/2)-self.rotation)
            self.moves -=1
    def wKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x -speed*cos((pi/2)-self.rotation)>0 and self.y -speed*sin((pi/2)-self.rotation) >0:
           self.x -= speed*cos((pi/2)-self.rotation)
           self.y -= speed*sin((pi/2)-self.rotation)
           self.moves-=1
    def qKey(self, event):
        self.KILL()
        if self.rotation == 3*(pi/2):
            self.rotation = 0
        else:
            self.rotation+= pi/2
    def eKey(self, event):
        self.KILL()
        if self.rotation == -3*(pi/2):
            self.rotation = 0
        else:
            self.rotation-= pi/2
    def KILL(self):
        if len(self.Sprites)>0:
            for x in self.Sprites:
                x.destroy()

            self.Sprites = []
    def step(self):
        self.KILL()
        self.moves = speed1
    def hit(self):
        self.start = time.time()
        global heartlist
        self.lives -=1
        heartlist[self.lives].destroy()
        heartlist.remove(heartlist[self.lives])
        self.dead = Sprite(dead_asset, (1,1))
        global t
        t=0
        self.end = self.start + .5

    def hit2(self):
        elapsed = time.time()
        if elapsed > self.end:
            self.dead.destroy()
            global t
            t =1
            if self.lives == 0:
                if SCREEN_WIDTH1/1071<SCREEN_HEIGHT/571:
                    self.go=Sprite(gameover, (1,1))
                    self.go.scale = SCREEN_WIDTH1/1071
                else:
                    self.go=Sprite(gameover, (1,1))
                    self.go.scale = SCREEN_HEIGHT/571
class axe(Sprite):

    def __init__(self, position, rotation):
        asset = ImageAsset("download.png", Frame(56.25, 56.25, 56.25*2, 56.25*2.1))
        super().__init__(asset, position)
        self.scale = .3
        self.fxcenter = .5
        self.fycenter = 1
        self.rotation = rotation

class plasmaBolt(Sprite):

    def __init__(self, position, rotation):
        asset = ImageAsset("plasma-weapon-fire-animation.gif")
        super().__init__(asset, position)
        self.scale = .1
        self.fxcenter = .5
        self.fycenter = 1
        self.rotation = rotation
        self.time = time.time()
    def step(self):
        self.x +=1
        self.y +=1
        if len(self.collidingWithSprites(None))>0:
            for x in self.collidingWithSprites(None):
                x.destroy()
        if -self.time+time.time()>5:
            self.destroy()

class meleeSprite(Sprite):
    def __init__(self, position):
        super().__init__(ms, position)
        self.fxcenter = self.fycenter = 0.5
    def step(self):
        self.rotation = pi/2-atan2((self.y-MC1.y), (self.x-MC1.x))
        self.x -= 10*cos(atan2(self.y-MC1.y, self.x-MC1.x))
        self.y -= 10*sin(atan2(self.y-MC1.y, self.x-MC1.x))
        if MC1.rotation == 0 or MC1.rotation == pi:
            if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2) <= 30:
                swordlist.append(sword(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
                MC1.hit()
        if MC1.rotation == pi/2 or MC1.rotation == 3*pi/2:
            if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2) <= 30:
                swordlist.append(sword(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
                MC1.hit()
class shootSprite(Sprite):
    def __init__(self, position, ammo):
        super().__init__(ss, position)
        self.fxcenter = self.fycenter = 0.5
        self.ammo = ammo
        self.ammoMax = ammo
    def step(self):
        self.rotation = pi/2-atan2((self.y-MC1.y), (self.x-MC1.x))
        if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2)-300>1:
            self.x -= 10*cos(atan2(self.y-MC1.y, self.x-MC1.x))
            self.y -= 10*sin(atan2(self.y-MC1.y, self.x-MC1.x))
        else:
            if self.ammo>0:
                self.ammo-=1
                MC1.hit()
                bulletlist.append(bullet(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
            else:
                global maintain
                maintain = True
                self.ammo = self.ammoMax

    def reload1(self):
        self.rotation += pi/36

class bullet(Sprite):
    def __init__(self, position, rotation):
        super().__init__(smlBullet, position)
        self.fxcenter = self.fycenter = 0.5
        self.rotation = rotation

class sword(Sprite):
    def __init__(self, position, rotation):
        super().__init__(smlSword, position)
        self.fxcenter = self.fycenter = 0.5
        self.rotation = rotation
class heart(Sprite):
    asset = ImageAsset("heart-e1403272720870.png")
    def __init__(self, position, heartnumber):
        super().__init__(heart.asset, position)
        self.fxcenter = self.fycenter = 0.5
        self.scale = .05

x = 15
y=15
lp = 0
GG = 0
while GG != 1:
    heartlist[lp]=heart((x,y), lp)
    if lp == lives-1:
        GG = 1
    else:
        x+=38
        lp+=1

turn = 0
def turnProgress ():
    if MC1.lives>0:
        global turn
        turn=1
def spaceKey (event):

    turnProgress()

myapp.listenKeyEvent('keydown', 'space', spaceKey)
MC1=MC((320,240), lives)
meleeSprite1=meleeSprite((100,240))
shootSprite1=shootSprite((100,100), 4)
plasmaBolt1= plasmaBolt((200,200),4)
myapp.run()<commit_msg>Updated from Brython Server: 5/1/2017 2:45:15 PM<commit_after>from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame, PolygonAsset
from math import pi, cos, sin, atan2, sqrt
import time
class SpaceGame(App):
    """
    Tutorial4 space game example.
    """
    def __init__(self, width, height):
        super().__init__(width, height)
        black = Color(0, .2)
        noline = LineStyle(0, black)
        bg_asset = RectangleAsset(self.width, self.height, noline, black)

        bg = Sprite(bg_asset, (0,0))
        global SCREEN_WIDTH1, SCREEN_HEIGHT
        SCREEN_WIDTH1=self.width
        SCREEN_HEIGHT = self.height

    def step(self):
        for x in self.getSpritesbyClass(plasmaBolt):
            x.step()
        global swordlist, bulletlist, maintain
        if turn == 1:

            maintain = False
            for ship in self.getSpritesbyClass(sword):
                ship.destroy()
            for ship in self.getSpritesbyClass(bullet):
                ship.destroy()
            meleeSprite1.step()
            shootSprite1.step()
            MC1.step()

            global turn
            turn = 0
        if t == 0:
            MC1.hit2()

        if maintain == True:
            for ship in self.getSpritesbyClass(shootSprite):
                ship.reload1()

myapp = SpaceGame(0,0)
hit = 0
t=1
maintain = False
lives = 4
heartlist = list(range(4))

black = Color(0, 1)
speed = 10
speed1=4
red = Color(0xff0000, 1.0)
green = Color(0x00ff00, 1.0)
blue = Color(0x0000ff, 1.0)
swordlist = []
bulletlist =[]
thinline = LineStyle(1, black)
white = Color(0xffffff, 1)
gray = Color(0x8c8c8c, 1)
noline = LineStyle(0, black)
thinline1 = LineStyle(1, white)
dead_asset = RectangleAsset(SCREEN_WIDTH1, SCREEN_HEIGHT, noline, red)
cf = PolygonAsset(((-10,-15),(0,-22.5),(10,-15),(10,15),(-10,15)), thinline, gray)
gameover = ImageAsset("game_over___pixel_art_by_tfcb93-d513cay.png")
ms = PolygonAsset(((-7.5,-11.5),(7.5,-11.5),(7.5,11.5),(-7.5,11.5)), thinline, red)
ss = PolygonAsset(((-7.5,-11.5),(7.5,-11.5),(7.5,11.5),(-7.5,11.5)), thinline, blue)
smlSword = PolygonAsset(((-2.5,-5),(2.5,-5),(2.5,5),(-2.5,5)), thinline, red)
smlBullet = PolygonAsset(((-2.5,0),(2.5,0),(2.5,-300),(-2.5,-300)), thinline, blue)
class MC(Sprite):
    def __init__(self, position, ls):
        super().__init__(cf, position)
        self.moves = speed1
        SpaceGame.listenKeyEvent("keydown", "d", self.dKey)
        SpaceGame.listenKeyEvent("keydown", "a", self.aKey)
        SpaceGame.listenKeyEvent("keydown", "s", self.sKey)
        SpaceGame.listenKeyEvent("keydown", "w", self.wKey)
        SpaceGame.listenKeyEvent("keydown", "q", self.qKey)
        SpaceGame.listenKeyEvent("keydown", "e", self.eKey)
        SpaceGame.listenKeyEvent("keydown", "j", self.jKey)
        SpaceGame.listenKeyEvent("keydown", "k", self.kKey)
        self.fxcenter = self.fycenter = 0.5
        self.lives = ls
        self.start=0
        self.end=0
        self.dead = 0
        self.go = 0
        self.Sprites = []
    def jKey(self, event):
        self.KILL()
        if self.moves >1:
            self.Sprites.append(axe((self.x-19*cos((pi/2)-self.rotation), self.y -19*sin((pi/2)-self.rotation)), self.rotation))
            self.moves -=2
    def kKey(self, event):
        self.KILL()
        if self.moves >1:
            plasmaBolt((self.x-19*cos((pi/2)-self.rotation), self.y -19*sin((pi/2)-self.rotation)), self.rotation)
            self.moves -=2
    def dKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x +speed*cos(self.rotation)<SCREEN_WIDTH1 and self.y -speed*sin(self.rotation) >0:
            self.x += speed*cos(self.rotation)
            self.y -= speed*sin(self.rotation)
            self.moves -=1
    def aKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x -speed*cos(self.rotation)>0 and self.y +speed*sin(self.rotation) <SCREEN_HEIGHT:
            self.x -= speed*cos(self.rotation)
            self.y += speed*sin(self.rotation)

            self.moves -=1
    def sKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x +speed*cos((pi/2)-self.rotation)<SCREEN_WIDTH1 and self.y +speed*sin((pi/2)-self.rotation) <SCREEN_HEIGHT:
            self.x += speed*cos((pi/2)-self.rotation)
            self.y += speed*sin((pi/2)-self.rotation)
            self.moves -=1
    def wKey(self, event):
        self.KILL()
        if self.moves > 0 and self.x -speed*cos((pi/2)-self.rotation)>0 and self.y -speed*sin((pi/2)-self.rotation) >0:
           self.x -= speed*cos((pi/2)-self.rotation)
           self.y -= speed*sin((pi/2)-self.rotation)
           self.moves-=1
    def qKey(self, event):
        self.KILL()
        if self.rotation == 3*(pi/2):
            self.rotation = 0
        else:
            self.rotation+= pi/2
    def eKey(self, event):
        self.KILL()
        if self.rotation == -3*(pi/2):
            self.rotation = 0
        else:
            self.rotation-= pi/2
    def KILL(self):
        if len(self.Sprites)>0:
            for x in self.Sprites:
                x.destroy()

            self.Sprites = []
    def step(self):
        self.KILL()
        self.moves = speed1
    def hit(self):
        self.start = time.time()
        global heartlist
        self.lives -=1
        heartlist[self.lives].destroy()
        heartlist.remove(heartlist[self.lives])
        self.dead = Sprite(dead_asset, (1,1))
        global t
        t=0
        self.end = self.start + .5

    def hit2(self):
        elapsed = time.time()
        if elapsed > self.end:
            self.dead.destroy()
            global t
            t =1
            if self.lives == 0:
                if SCREEN_WIDTH1/1071<SCREEN_HEIGHT/571:
                    self.go=Sprite(gameover, (1,1))
                    self.go.scale = SCREEN_WIDTH1/1071
                else:
                    self.go=Sprite(gameover, (1,1))
                    self.go.scale = SCREEN_HEIGHT/571
class axe(Sprite):

    def __init__(self, position, rotation):
        asset = ImageAsset("download.png", Frame(56.25, 56.25, 56.25*2, 56.25*2.1))
        super().__init__(asset, position)
        self.scale = .3
        self.fxcenter = .5
        self.fycenter = 1
        self.rotation = rotation

class plasmaBolt(Sprite):

    def __init__(self, position, rotation):
        asset = ImageAsset("plasma-weapon-fire-animation.gif")
        super().__init__(asset, position)
        self.scale = .1
        self.fxcenter = .5
        self.fycenter = 1
        self.rotation = rotation
        self.time = time.time()
    def step(self):
        self.x +=cos((pi/2)-self.rotation)
        self.y +=sin((pi/2)-self.rotation)
        if len(self.collidingWithSprites(None))>1:
            for x in self.collidingWithSprites(None):
                x.destroy()
        if -self.time+time.time()>5:
            self.destroy()

class meleeSprite(Sprite):
    def __init__(self, position):
        super().__init__(ms, position)
        self.fxcenter = self.fycenter = 0.5
    def step(self):
        self.rotation = pi/2-atan2((self.y-MC1.y), (self.x-MC1.x))
        self.x -= 10*cos(atan2(self.y-MC1.y, self.x-MC1.x))
        self.y -= 10*sin(atan2(self.y-MC1.y, self.x-MC1.x))
        if MC1.rotation == 0 or MC1.rotation == pi:
            if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2) <= 30:
                swordlist.append(sword(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
                MC1.hit()
        if MC1.rotation == pi/2 or MC1.rotation == 3*pi/2:
            if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2) <= 30:
                swordlist.append(sword(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
                MC1.hit()
class shootSprite(Sprite):
    def __init__(self, position, ammo):
        super().__init__(ss, position)
        self.fxcenter = self.fycenter = 0.5
        self.ammo = ammo
        self.ammoMax = ammo
    def step(self):
        self.rotation = pi/2-atan2((self.y-MC1.y), (self.x-MC1.x))
        if sqrt((self.x-MC1.x)**2+(self.y-MC1.y)**2)-300>1:
            self.x -= 10*cos(atan2(self.y-MC1.y, self.x-MC1.x))
            self.y -= 10*sin(atan2(self.y-MC1.y, self.x-MC1.x))
        else:
            if self.ammo>0:
                self.ammo-=1
                MC1.hit()
                bulletlist.append(bullet(((self.x-15*cos(atan2(self.y-MC1.y, self.x-MC1.x))), (self.y-15*sin(atan2(self.y-MC1.y, self.x-MC1.x)))), self.rotation))
            else:
                global maintain
                maintain = True
                self.ammo = self.ammoMax

    def reload1(self):
        self.rotation += pi/36

class bullet(Sprite):
    def __init__(self, position, rotation):
        super().__init__(smlBullet, position)
        self.fxcenter = self.fycenter = 0.5
        self.rotation = rotation

class sword(Sprite):
    def __init__(self, position, rotation):
        super().__init__(smlSword, position)
        self.fxcenter = self.fycenter = 0.5
        self.rotation = rotation
class heart(Sprite):
    asset = ImageAsset("heart-e1403272720870.png")
    def __init__(self, position, heartnumber):
        super().__init__(heart.asset, position)
        self.fxcenter = self.fycenter = 0.5
        self.scale = .05

x = 15
y=15
lp = 0
GG = 0
while GG != 1:
    heartlist[lp]=heart((x,y), lp)
    if lp == lives-1:
        GG = 1
    else:
        x+=38
        lp+=1

turn = 0
def turnProgress ():
    if MC1.lives>0:
        global turn
        turn=1
def spaceKey (event):

    turnProgress()

myapp.listenKeyEvent('keydown', 'space', spaceKey)
MC1=MC((320,240), lives)
meleeSprite1=meleeSprite((100,240))
shootSprite1=shootSprite((100,100), 4)

myapp.run()<|endoftext|>


<commit_before>from collections import OrderedDict
from ..application import mongo
from ..forms import WTFCSVUpload, WTFCSVLink, WTFCSVSample
from ..logic import wtfcsvstat, filehandler, oauth
from flask import Blueprint, render_template, request, redirect, g
import os, logging

mod = Blueprint('wtfcsv', __name__, url_prefix='/<lang_code>/wtfcsv', template_folder='../templates/wtfcsv')

@mod.route('/', methods=('GET', 'POST'))
def index():

	doc_url = oauth.doc_url()
	if doc_url is not None:
		return redirect_to_results(process_link(doc_url))

	tab = 'paste' if not 'tab' in request.args else request.args['tab']
	results = None

	forms = OrderedDict()
	forms['sample'] = WTFCSVSample()
	forms['upload'] = WTFCSVUpload()
	forms['link'] = WTFCSVLink()

	if request.method == 'POST':

		btn_value = request.form['btn']

		if btn_value == 'upload':
			results = process_upload(forms['upload'].data['upload'])
		elif btn_value == 'link':
			doc = oauth.open_doc_from_url(forms['link'].data['link'], request.url)
			if doc['authenticate'] is not None:
				return redirect(doc['authenticate'])
			elif doc['doc'] is not None:
				results = process_link(doc['doc'])

		if btn_value is not None and btn_value is not u'':
			print results
			return redirect_to_results(results)

	return render_template('wtfcsv.html', forms=forms.items(), tool_name='wtfcsv')

@mod.route('/results')
def results():
	results = None
	doc_id = None if not 'id' in request.args else request.args['id']
	if doc_id is None:
		return redirect(g.current_lang + '/wtfcsv')
	else:
		results = mongo.find_document('wtfcsv', doc_id).get('results')
	return render_template('wtfcsv/results.html', results=results, tool_name='wtfcsv')

def redirect_to_results(results):
	doc_id = mongo.save_csv('wtfcsv', results)
	return redirect(request.url + 'results?id=' + doc_id)

def process_paste(text):
	file_path = filehandler.write_to_temp_file(text)
	return wtfcsvstat.get_summary(file_path)

def process_upload(csv_file):
	file_path = filehandler.open_doc(csv_file)
	file_path = filehandler.convert_to_csv(file_path)
	results = wtfcsvstat.get_summary(file_path)
	filehandler.delete_file(file_path)
	return results

def process_link(sheet):
	file_path = filehandler.open_sheet(sheet)
	results = wtfcsvstat.get_summary(file_path)
	filehandler.delete_file(file_path)
	return results
<commit_msg>support sample data for wtfcsv<commit_after>from collections import OrderedDict
from ..application import mongo
from ..forms import WTFCSVUpload, WTFCSVLink, WTFCSVSample
from ..logic import wtfcsvstat, filehandler, oauth
from flask import Blueprint, render_template, request, redirect, g
import os, logging

mod = Blueprint('wtfcsv', __name__, url_prefix='/<lang_code>/wtfcsv', template_folder='../templates/wtfcsv')

@mod.route('/', methods=('GET', 'POST'))
def index():

	doc_url = oauth.doc_url()
	if doc_url is not None:
		return redirect_to_results(process_link(doc_url))

	tab = 'paste' if not 'tab' in request.args else request.args['tab']
	results = None

	forms = OrderedDict()
	forms['sample'] = WTFCSVSample()
	forms['upload'] = WTFCSVUpload()
	forms['link'] = WTFCSVLink()

	if request.method == 'POST':

		btn_value = request.form['btn']

		if btn_value == 'upload':
			results = process_upload(forms['upload'].data['upload'])
		elif btn_value == 'link':
			doc = oauth.open_doc_from_url(forms['link'].data['link'], request.url)
			if doc['authenticate'] is not None:
				return redirect(doc['authenticate'])
			elif doc['doc'] is not None:
				results = process_link(doc['doc'])
		elif btn_value == 'sample':
			basedir = os.path.dirname(os.path.abspath(__file__))
			sample_file = forms['sample'].data['sample']
			results = wtfcsvstat.get_summary(os.path.join(basedir,'../','../',sample_file))

		if btn_value is not None and btn_value is not u'':
			return redirect_to_results(results)

	return render_template('wtfcsv.html', forms=forms.items(), tool_name='wtfcsv')

@mod.route('/results')
def results():
	results = None
	doc_id = None if not 'id' in request.args else request.args['id']
	if doc_id is None:
		return redirect(g.current_lang + '/wtfcsv')
	else:
		results = mongo.find_document('wtfcsv', doc_id).get('results')
	return render_template('wtfcsv/results.html', results=results, tool_name='wtfcsv')

def redirect_to_results(results):
	doc_id = mongo.save_csv('wtfcsv', results)
	return redirect(request.url + 'results?id=' + doc_id)

def process_paste(text):
	file_path = filehandler.write_to_temp_file(text)
	return wtfcsvstat.get_summary(file_path)

def process_upload(csv_file):
	file_path = filehandler.open_doc(csv_file)
	file_path = filehandler.convert_to_csv(file_path)
	results = wtfcsvstat.get_summary(file_path)
	filehandler.delete_file(file_path)
	return results

def process_link(sheet):
	file_path = filehandler.open_sheet(sheet)
	results = wtfcsvstat.get_summary(file_path)
	filehandler.delete_file(file_path)
	return results
<|endoftext|>


<commit_before>import tweepy

print 'An example of using the Tweepy library...'

# We need an authentication handler to tell twitter who we are.
# First let's make one using basic auth.
basic_auth = tweepy.BasicAuthHandler('tweebly', 'omega1987twitter')

# Now a handler for oauth.
oauth_auth = tweepy.OAuthHandler('consumer_key', 'consumer_secrete')

# Create an instance of the API object and use the basic auth handler.
api = tweepy.API(basic_auth)

# Let's get a list of the statuses on the public timeline
# and print the texts to the console.
public_timeline = api.public_timeline()
for status in public_timeline:
  print '%s:  %s\n   from %s posted at %s' % (
      status.user.screen_name, status.text, status.source, status.created_at)

# Now we will update our twitter status
# and print the text to the console.
update = api.update_status(status='hello!')
print 'Update: %s' % update.text

# Get the timeline for the 'twitter' user.
twitter_timeline = api.user_timeline(screen_name='twitter')

# You can also setup up a cache.
# Here we will use an in-memory cache with a timeout of 60 seconds.
cached_api = tweepy.API(basic_auth, cache=tweepy.MemoryCache(timeout=60))

# First request here will not be cached
s = cached_api.get_status(id=123)

# Now this request will be cached and won't require a trip to twitter's server.
s_again = cached_api.get_status(id=123)

<commit_msg>Minor update.<commit_after>import tweepy

print 'An example of using the Tweepy library...'

# We need an authentication handler to tell twitter who we are.
# First let's make one using basic auth.
basic_auth = tweepy.BasicAuthHandler('username', 'yourpass')

# Now a handler for oauth.
oauth_auth = tweepy.OAuthHandler('consumer_key', 'consumer_secrete')

# Create an instance of the API object and use the basic auth handler.
api = tweepy.API(basic_auth)

# Let's get a list of the statuses on the public timeline
# and print the texts to the console.
public_timeline = api.public_timeline()
for status in public_timeline:
  print '%s:  %s\n   from %s posted at %s' % (
      status.user.screen_name, status.text, status.source, status.created_at)

# Now we will update our twitter status
# and print the text to the console.
update = api.update_status(status='hello!')
print 'Update: %s' % update.text

# Get the timeline for the 'twitter' user.
twitter_timeline = api.user_timeline(screen_name='twitter')

# You can also setup up a cache.
# Here we will use an in-memory cache with a timeout of 60 seconds.
cached_api = tweepy.API(basic_auth, cache=tweepy.MemoryCache(timeout=60))

# First request here will not be cached
s = cached_api.get_status(id=123)

# Now this request will be cached and won't require a trip to twitter's server.
s_again = cached_api.get_status(id=123)

<|endoftext|>


<commit_before>import time

import gym
import gym_rover

from gym import spaces

import numpy as np

from gym_rover.learning.ccea import CCEA
from gym_rover.learning.deepq import DeepQ
from gym_rover.learning.cnn_deepq import CNNDeepQ
from gym_rover.multiagent.multi_env import MultiagentEnv

import random
from collections import deque
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop, Adam
from keras import backend as K

RENDER = True
EPISODES = 100000
NUM_POIS = 1
NUM_AGENTS = 1
TIME_LIMIT = 10
LENGTH = 10

if __name__ == "__main__":
    env = gym.make('rover-v0')
    env._max_episodes=100000

    env.world_height = LENGTH
    env.world_width = LENGTH
    env.num_agents = NUM_AGENTS
    env.num_pois = NUM_POIS
    env.time_limit = TIME_LIMIT
    env.image_width = 13

    env.observation_mode = 'image'

    env.set_observation_space()
    env.set_action_space()
    env.reset()

    state_shape = (env.image_width, env.image_width, 2)
    action_size = 9

    agent = CNNDeepQ(state_shape, action_size)
    for e in range(EPISODES):
        done = False
        score = 0
        states = env.reset()

        state = states[0]
        while not done:
            if RENDER:
                env.render()
            action = agent.get_action(state)
            next_states, reward, done, info = env.step(np.array([action]))
            next_state = next_states[0]
            if not next_state.shape == state_shape:
                continue
            agent.replay_memory(state, action, reward,
                                next_state, done)
            # every time step do the training
            agent.train_replay()
            score = reward
            state = next_state

            if done:
                agent.update_target_model()

                # every episode, plot the play time
                print("episode: {:0>4d}/{} score: {:.2f} epsilon: {:.3f}".format(e, EPISODES, score, agent.epsilon))

    # # Initialize all agents
    # agents = []
    # for i in range(NUM_AGENTS):
    #     agents.append(CNNDeepQ(state_shape, action_size))

    # for e in range(EPISODES):
    #     done = False
    #     score = 0
    #     states = env.reset()
    #     time = 0

    #     while not done:
    #         if RENDER:
    #             env.render()

    #         actions = []
    #         for i in range(NUM_AGENTS):
    #             state = states[i]
    #             #print (state.shape)
    #             #state = np.reshape(state, state_shape)
    #             agent = agents[i]

    #             # get action for the current state and go one step in environment
    #             action = agent.get_action(state)
    #             actions.append(action)

    #         next_states, reward, done, info = env.step(np.array(actions))

    #         reward -= (time * 0.1)
    #         for i in range(NUM_AGENTS):
    #             next_state = next_states[i]
    #             #print ('Next state shape')
    #             #print (next_state.shape)
    #             #next_state = np.reshape(next_state, state_shape)
    #             agent = agents[i]
    #             # save the sample <s, a, r, s'> to the replay memory
    #             state = states[i]
    #             #state = np.reshape(states[i], state_shape)
    #             print('Calling replay: ' + str(next_state.shape))
    #             agent.replay_memory(state, actions[i], reward,
    #                                 next_state, done)
    #             # every time step do the training
    #             agent.train_replay()

    #         score = reward
    #         state = next_state

    #         time += 1
    #         if done:
    #             # every episode update the target model to be same with model
    #             for agent in agents:
    #                 agent.update_target_model()

    #             # every episode, plot the play time
    #             print("episode: {:0>4d}/{} score: {:.2f} epsilon: {:.3f}".format(e, EPISODES, score, agents[0].epsilon))
    #             #, e, "  score:", score, "  memory length:", len(agent.memory),
    #             #      "  epsilon:", agent.epsilon)

<commit_msg>Render toggle<commit_after>import time

import gym
import gym_rover

from gym import spaces

import numpy as np

from gym_rover.learning.ccea import CCEA
from gym_rover.learning.deepq import DeepQ
from gym_rover.learning.cnn_deepq import CNNDeepQ
from gym_rover.multiagent.multi_env import MultiagentEnv

import random
from collections import deque
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop, Adam
from keras import backend as K

RENDER = False
EPISODES = 100000
NUM_POIS = 1
NUM_AGENTS = 1
TIME_LIMIT = 10
LENGTH = 10

if __name__ == "__main__":
    env = gym.make('rover-v0')
    env._max_episodes=100000

    env.world_height = LENGTH
    env.world_width = LENGTH
    env.num_agents = NUM_AGENTS
    env.num_pois = NUM_POIS
    env.time_limit = TIME_LIMIT
    env.image_width = 13

    env.observation_mode = 'image'

    env.set_observation_space()
    env.set_action_space()
    env.reset()

    state_shape = (env.image_width, env.image_width, 2)
    action_size = 9

    agent = CNNDeepQ(state_shape, action_size)
    for e in range(EPISODES):
        done = False
        score = 0
        states = env.reset()

        state = states[0]
        while not done:
            if RENDER:
                env.render()
            action = agent.get_action(state)
            next_states, reward, done, info = env.step(np.array([action]))
            next_state = next_states[0]
            if not next_state.shape == state_shape:
                continue
            agent.replay_memory(state, action, reward,
                                next_state, done)
            # every time step do the training
            agent.train_replay()
            score = reward
            state = next_state

            if done:
                agent.update_target_model()

                # every episode, plot the play time
                print("episode: {:0>4d}/{} score: {:.2f} epsilon: {:.3f}".format(e, EPISODES, score, agent.epsilon))

    # # Initialize all agents
    # agents = []
    # for i in range(NUM_AGENTS):
    #     agents.append(CNNDeepQ(state_shape, action_size))

    # for e in range(EPISODES):
    #     done = False
    #     score = 0
    #     states = env.reset()
    #     time = 0

    #     while not done:
    #         if RENDER:
    #             env.render()

    #         actions = []
    #         for i in range(NUM_AGENTS):
    #             state = states[i]
    #             #print (state.shape)
    #             #state = np.reshape(state, state_shape)
    #             agent = agents[i]

    #             # get action for the current state and go one step in environment
    #             action = agent.get_action(state)
    #             actions.append(action)

    #         next_states, reward, done, info = env.step(np.array(actions))

    #         reward -= (time * 0.1)
    #         for i in range(NUM_AGENTS):
    #             next_state = next_states[i]
    #             #print ('Next state shape')
    #             #print (next_state.shape)
    #             #next_state = np.reshape(next_state, state_shape)
    #             agent = agents[i]
    #             # save the sample <s, a, r, s'> to the replay memory
    #             state = states[i]
    #             #state = np.reshape(states[i], state_shape)
    #             print('Calling replay: ' + str(next_state.shape))
    #             agent.replay_memory(state, actions[i], reward,
    #                                 next_state, done)
    #             # every time step do the training
    #             agent.train_replay()

    #         score = reward
    #         state = next_state

    #         time += 1
    #         if done:
    #             # every episode update the target model to be same with model
    #             for agent in agents:
    #                 agent.update_target_model()

    #             # every episode, plot the play time
    #             print("episode: {:0>4d}/{} score: {:.2f} epsilon: {:.3f}".format(e, EPISODES, score, agents[0].epsilon))
    #             #, e, "  score:", score, "  memory length:", len(agent.memory),
    #             #      "  epsilon:", agent.epsilon)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""OpenAPI core module"""
from openapi_core.shortcuts import (
    create_spec, validate_parameters, validate_body, validate_data,
)

__author__ = 'Artur Maciag'
__email__ = 'maciag.artur@gmail.com'
__version__ = '0.11.0'
__url__ = 'https://github.com/p1c2u/openapi-core'
__license__ = 'BSD 3-Clause License'

__all__ = [
    'create_spec', 'validate_parameters', 'validate_body', 'validate_data',
]
<commit_msg>Version 0.11.1<commit_after># -*- coding: utf-8 -*-
"""OpenAPI core module"""
from openapi_core.shortcuts import (
    create_spec, validate_parameters, validate_body, validate_data,
)

__author__ = 'Artur Maciag'
__email__ = 'maciag.artur@gmail.com'
__version__ = '0.11.1'
__url__ = 'https://github.com/p1c2u/openapi-core'
__license__ = 'BSD 3-Clause License'

__all__ = [
    'create_spec', 'validate_parameters', 'validate_body', 'validate_data',
]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# EForge project management system, Copyright © 2010, Element43
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_delete
from eforge.models import Project
from datetime import datetime

class Update(models.Model):
    """
        The update is a generic class representing all project activities.
        It connects to a model which contains the actual activity data;
        the update object itself contains just the bare minimum information
        required to efficiently display and distribute the updates.

        In order to create a new update type, you must create a child class of
        your model called Update. To this, you must add methods in the form
            def summary(model_object):
                pass

        These methods will be invoked when a user queries an instance of the update for your model. You should then construct an Update object with values appropriate to the update being created.

        The update will be pushed out to any listeners when you save the object.

        .. py:attribute:: user
            The user responsible for the update

        .. py:attribute:: project
            The project the update is connected to

        .. py:attribute:: date
            The date and time at which the update was created
            (Will be set to the time when the update object was created by
            default; you can override this by providing a date function)

        .. py:attribute:: object
            The object containing the main data on the update. This is a content
            types generic relation; it is a composite of (object_type, object_id)

        .. py:attribute:: summary
            A short summary of the update, usable for titles and short
            notifications (e.g. tweets) [proxied]

        .. py:attribute:: description
            A more elaborate description of the update, for example, to be used as the body of E-Mail notifications [proxied]

        .. py:attribute:: url
            A link which the user can visit to gather more information [proxied]

        .. py:attribute:: recipients
            List of users who have expressed interest in direct notification of an update. This is optional; if not specified, none will be assumed
            [proxied]
    """

    user        = models.ForeignKey(User, null=True)
    project     = models.ForeignKey(Project, null=True)
    date       = models.DateTimeField(auto_now_add=True)

    object_type = models.ForeignKey(ContentType)
    object_id   = models.PositiveIntegerField()
    object      = generic.GenericForeignKey('object_type', 'object_id')

def _on_update(sender, instance, created, **kwargs):
    if not created:
        return

    make_update(instance)

def _on_delete(sender, instance, **kwargs):
    instance.update.delete()

def make_update(instance):
    """
        Makes a new update, based upon :instance:.
    """
    def _now(instance):
        return datetime.utcnow()

    upd = Update(user    = instance.Update.user(instance),
                 project = instance.Update.project(instance),
                 date    = getattr(instance.Update, 'date', _now)(instance),
                 object  = instance
    )
    upd.save()

def register_update_type(model):
    """
        Registers a model as being an update type. Whenever an instance of this
        model is saved, a new Update will automatically be created.

        In order to fill in the user and group properties, you must provide user and group methods (taking an instance of your model) in your Update
        class.

        The :update: property will be added to your mode, through which you can find the instance corresponding to a model.
    """

    model.update = generic.GenericRelation(Update)

    post_save.connect(_on_update, sender=model)
    pre_delete.connect(_on_delete, sender=model)

def _proxy_property(name, default=None):
    def required_property(self):
        raise TypeError('Update object %s lacks property %s' % (self, name))

    default = default or required_property

    def the_property(self):
        try:
            model = self.object_type.model_class()
            return getattr(model.Update, name, default)(self.object)
        except Exception, e:
            print e

    setattr(Update, name, property(the_property))

_proxy_property('summary')
_proxy_property('description')
_proxy_property('url')
_proxy_property('recipients', lambda o: [])<commit_msg>Bugfix update system: Had autoadd date on the Update model; therefore, Django was marking all updates as from /NOW/. Woops.<commit_after># -*- coding: utf-8 -*-
# EForge project management system, Copyright © 2010, Element43
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_delete
from eforge.models import Project
from datetime import datetime

class Update(models.Model):
    """
        The update is a generic class representing all project activities.
        It connects to a model which contains the actual activity data;
        the update object itself contains just the bare minimum information
        required to efficiently display and distribute the updates.

        In order to create a new update type, you must create a child class of
        your model called Update. To this, you must add methods in the form
            def summary(model_object):
                pass

        These methods will be invoked when a user queries an instance of the update for your model. You should then construct an Update object with values appropriate to the update being created.

        The update will be pushed out to any listeners when you save the object.

        .. py:attribute:: user
            The user responsible for the update

        .. py:attribute:: project
            The project the update is connected to

        .. py:attribute:: date
            The date and time at which the update was created
            (Will be set to the time when the update object was created by
            default; you can override this by providing a date function)

        .. py:attribute:: object
            The object containing the main data on the update. This is a content
            types generic relation; it is a composite of (object_type, object_id)

        .. py:attribute:: summary
            A short summary of the update, usable for titles and short
            notifications (e.g. tweets) [proxied]

        .. py:attribute:: description
            A more elaborate description of the update, for example, to be used as the body of E-Mail notifications [proxied]

        .. py:attribute:: url
            A link which the user can visit to gather more information [proxied]

        .. py:attribute:: recipients
            List of users who have expressed interest in direct notification of an update. This is optional; if not specified, none will be assumed
            [proxied]
    """

    user        = models.ForeignKey(User, null=True)
    project     = models.ForeignKey(Project, null=True)
    date       = models.DateTimeField()

    object_type = models.ForeignKey(ContentType)
    object_id   = models.PositiveIntegerField()
    object      = generic.GenericForeignKey('object_type', 'object_id')

def _on_update(sender, instance, created, **kwargs):
    if not created:
        return

    make_update(instance)

def _on_delete(sender, instance, **kwargs):
    instance.update.delete()

def make_update(instance):
    """
        Makes a new update, based upon :instance:.
    """
    def _now(instance):
        return datetime.utcnow()

    upd = Update(user    = instance.Update.user(instance),
                 project = instance.Update.project(instance),
                 date    = getattr(instance.Update, 'date', _now)(instance),
                 object  = instance
    )
    upd.save()

def register_update_type(model):
    """
        Registers a model as being an update type. Whenever an instance of this
        model is saved, a new Update will automatically be created.

        In order to fill in the user and group properties, you must provide user and group methods (taking an instance of your model) in your Update
        class.

        The :update: property will be added to your mode, through which you can find the instance corresponding to a model.
    """

    model.update = generic.GenericRelation(Update)

    post_save.connect(_on_update, sender=model)
    pre_delete.connect(_on_delete, sender=model)

def _proxy_property(name, default=None):
    def required_property(self):
        raise TypeError('Update object %s lacks property %s' % (self, name))

    default = default or required_property

    def the_property(self):
        try:
            model = self.object_type.model_class()
            return getattr(model.Update, name, default)(self.object)
        except Exception, e:
            print e

    setattr(Update, name, property(the_property))

_proxy_property('summary')
_proxy_property('description')
_proxy_property('url')
_proxy_property('recipients', lambda o: [])<|endoftext|>


<commit_before>from flask import Flask, Blueprint

app = Flask(__name__)

@app.route("/foo", methods=["GET"])
def get_foo():
    """Get some foo.

    Returns:
        A string contaning a nice foo.
    """
    return "foo"

@app.route("/foo/<int:id>", methods=["PATCH"])
def patch_foo(id):
    return "", 200

@app.route("/bar", methods=["GET", "POST", "PUT"])
def bar():
    return "", 501

baz = Blueprint("baz", __name__)

@baz.route("/baz", methods=["GET"])
def get_baz():
    return "baz"

app.register_blueprint(baz)

def create_app():
    return app

if __name__ == "__main__":
    app.run()
<commit_msg>Fix a typo (#12)<commit_after>from flask import Flask, Blueprint

app = Flask(__name__)

@app.route("/foo", methods=["GET"])
def get_foo():
    """Get some foo.

    Returns:
        A string containing a nice foo.
    """
    return "foo"

@app.route("/foo/<int:id>", methods=["PATCH"])
def patch_foo(id):
    return "", 200

@app.route("/bar", methods=["GET", "POST", "PUT"])
def bar():
    return "", 501

baz = Blueprint("baz", __name__)

@baz.route("/baz", methods=["GET"])
def get_baz():
    return "baz"

app.register_blueprint(baz)

def create_app():
    return app

if __name__ == "__main__":
    app.run()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Requires:
# libnfldap

import libnfldap
import sys

def main():
	uidnum = sys.argv[1]
	print("#Generating rules for user ID %s" % uidnum)

	# declare a new iptables ruleset
	ipt = libnfldap.IPTables()

	# insert sane default at the top of the ruleset
	ipt.insertSaneDefaults()

	# declare a new ipset ruleset
	ips = libnfldap.IPset()

	# find a user in ldap
	ldap = libnfldap.LDAP(LDAP_URL, LDAP_BIND_DN, LDAP_BIND_PASSWD)
	userdn,userid = ldap.getUserByNumber('o=com,dc=mozilla', uidnum)

	# create a custom chain and sets for the user
	ipt.newFilterChain(userid)
	r = "-A FORWARD -m owner --uid-owner " +  uidnum + " -m state --state NEW -j " + userid
	ipt.appendFilterRule(r)
	ips.newHashNet(userid)

	# find groups of an ldap user
	acls = ldap.getACLs('ou=groups,dc=mozilla',
						"(&(member="+userdn+")(cn=vpn_*))")

	# iterate through the ACLs
	print("#====== ACL details ======")
	for group,dests in acls.iteritems():
		for dest,desc in dests.iteritems():
			print("%s has access to %s aka '%s'" % (userid, dest, desc))
			if libnfldap.is_cidr(dest):
				ips.addCIDRToHashNet(userid, dest)
			else:
				ipt.appendFilterRule("-A " + userid + " -d " + dest + " -j ACCEPT")

	# set a default drop policy at the end of the ruleset
	ipt.appendDefaultDrop()

	# template and print the iptables rules
	print("#====== IPTABLES RULES ======\n")
	print("%s\n\n" % ipt.template())

	# template and print the ipset rules
	print("#====== IPSET RULES ======\n")
	print("%s" % ips.template())

if __name__ == "__main__":
	main()
<commit_msg>LDAP variables<commit_after>#!/usr/bin/env python
# Requires:
# libnfldap

import libnfldap
import sys

LDAP_URL='ldap://<%= ldap_server %>'
LDAP_BIND_DN='uid=<%= bind_user %>,ou=logins,dc=mozilla'
LDAP_BIND_PASSWD='<%= bind_password %>'

def main():
	uidnum = sys.argv[1]
	print("#Generating rules for user ID %s" % uidnum)

	# declare a new iptables ruleset
	ipt = libnfldap.IPTables()

	# insert sane default at the top of the ruleset
	ipt.insertSaneDefaults()

	# declare a new ipset ruleset
	ips = libnfldap.IPset()

	# find a user in ldap
	ldap = libnfldap.LDAP(LDAP_URL, LDAP_BIND_DN, LDAP_BIND_PASSWD)
	userdn,userid = ldap.getUserByNumber('o=com,dc=mozilla', uidnum)

	# create a custom chain and sets for the user
	ipt.newFilterChain(userid)
	r = "-A FORWARD -m owner --uid-owner " +  uidnum + " -m state --state NEW -j " + userid
	ipt.appendFilterRule(r)
	ips.newHashNet(userid)

	# find groups of an ldap user
	acls = ldap.getACLs('ou=groups,dc=mozilla',
						"(&(member="+userdn+")(cn=vpn_*))")

	# iterate through the ACLs
	print("#====== ACL details ======")
	for group,dests in acls.iteritems():
		for dest,desc in dests.iteritems():
			print("%s has access to %s aka '%s'" % (userid, dest, desc))
			if libnfldap.is_cidr(dest):
				ips.addCIDRToHashNet(userid, dest)
			else:
				ipt.appendFilterRule("-A " + userid + " -d " + dest + " -j ACCEPT")

	# set a default drop policy at the end of the ruleset
	ipt.appendDefaultDrop()

	# template and print the iptables rules
	print("#====== IPTABLES RULES ======\n")
	print("%s\n\n" % ipt.template())

	# template and print the ipset rules
	print("#====== IPSET RULES ======\n")
	print("%s" % ips.template())

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


<commit_before>import numpy as np
import re
import math
import random
from Tools import *

class MyRNN:
    # Parameters
    N = T = D = H = eta = n_train = 0

    # Variables
    X = Y = O = S = np.empty((N, T))

    # weights
    U = W = V = np.empty((D, H))

    # Input parameters
    dictionary = train = val = test = []

    # Main RNN constructor
    def __init__(self, dictionary, train, val, test, H, eta):
        self.dictionary, self.train, self.val, self.test = dictionary, train, val, test

        self.D = len(dictionary)
        self.T = max(len(s) for s in (train + val + test)) + 2
        self.H, self.eta = H, eta
        self.n_train = len(train)
        # print('Total number of phrases: ', self.N)

        # Weight assignment with Glorot uniform
        wgtD = self.D ** (-0.5)
        wgtH = self.H ** (-0.5)

        #self.U = np.random.uniform(-wgtD, wgtD, (self.H, self.D))  # HxD matrix
        #self.W = np.random.uniform(-wgtH, wgtH, (self.H, self.H))  # HxH matrix
        #self.V = np.random.uniform(-wgtH, wgtH, (self.D, self.H))  # DxH matrix
        self.U = np.random.randn(self.H, self.D)*0.01
        self.W = np.random.randn(self.H, self.H)*0.01
        self.V = np.random.randn(self.D, self.H)*0.01

    # Main parameter initializator
    def init_mainParam(self, data):
        self.N = len(data)
        # print('N dimension: ', self.N)
        # Preparation of X
        self.X = np.zeros((self.N, self.T, self.D))
        self.X[:, 0, 0] = 1
        #self.X[:, 1:, 2] = 1

        for s in data:
            i = data.index(s) #Index of the phrase
            j, k = 0, 0
            for j in range(len(s)):
                # j = s.index(w) + 1  # Index of the word in the phrase +1 for the <startWD> token
                if s[j] != '':
                    w = s[j]
                    j += 1
                    k = self.dictionary.index(w) #Index of the word in the dictionary
                    self.X[i, j, k] = 1
                    #self.X[i, j, 2] = 0

            #self.X[i, j + 1, 2] = 0
            #self.X[i, j + 1, 1] = 1

        # Preparation of Y
        self.Y = np.zeros((self.N, self.T, self.D))
        self.Y[:, :-1, :] = self.X[:, 1:, :]
        #self.Y[:, -1:, 2] = 1
        self.S = np.zeros((self.N, self.T, self.H))
        self.O = np.zeros((self.N, self.T, self.D))

    # Function that implements the activation of the hidden layer
    def outHL(self, x, s_prev):
        return np.tanh(np.dot(self.U, x) + np.dot(self.W, s_prev))

    # Function that implements the forward pass of the RNN
    def fwdRnn(self, X, S, O):
        # 1. s = tanh(Ux + Ws_prev)
        # 2. o = sigma(Vs)
        # 3. U,W,V = L(y,o)

        for t in range(self.T):
            S[:, t, :] = self.outHL(X[:, t, :].T, S[:, t - 1, :].T).T
            O[:, t, :] = self.softmax(self.V.dot(S[:, t, :].T)).T
        return S, O

    # Function that implements the backward pass of the RNN
    def bwRnn(self):
        # Evaluation of dLdV
        dL_dO = self.Y / self.O
        dO_dVS = self.O * (1.0 - self.O)
        dL_dV = np.tensordot(dL_dO*dO_dVS, self.S, axes=((0, 1), (0, 1)))
        c = (-self.eta) / (self.N * self.T) # Constant value including eta and 1/n
        # New matrix V
        Vnew = self.V - c * dL_dV

        # Evaluation of dLdU
        S0 = np.zeros(self.S.shape) # S(t-1)
        S0[:, 1:, :] = self.S[:, :-1, :]

        # Second version - correct
        dS_dargTanh = (1 - np.power(self.S,2))
        dL_dS = np.tensordot(dL_dO*dO_dVS, self.V, axes=(2, 0))
        dL_dU = np.tensordot(dL_dS*dS_dargTanh, self.X, axes=((0, 1), (0, 1))) # returns an HxD matrix
        Unew = self.U - c * dL_dU

        # Evaluation of dLdW
        dL_dW = np.tensordot(dL_dS*dS_dargTanh, S0, axes=((0, 1), (0, 1))) # returns an HxH matrix
        Wnew = self.W - (c * dL_dW)

        '''
        # First version - not correct
        dS_dargTanh1 = 1 - self.S # Decomposition of dSdargTanh = tanh' = 1 - tanh^2 = (1 + tanh)(1 - tanh)
        dS_dargTanh2 = 1 + self.S # Decomposition of dSdargTanh = tanh' = 1 - tanh^2 = (1 + tanh)(1 - tanh)
        dL_dS = np.tensordot(dL_dO * dO_dVS, self.V, axes=(2, 0))  # returns an NxTxH matrix
        dL_dargTanh1 = np.tensordot(dL_dS, dS_dargTanh1, axes=((0, 1), (0, 1)))  # returns an HxH matrix
        dargTanh2_dU = np.tensordot(dS_dargTanh2, self.X, axes=((0, 1), (0, 1)))  # returns an HxD matrix
        dL_dU = dL_dargTanh1.dot(dargTanh2_dU)
        # New matrix U
        Unew = self.U - c * dL_dU
        # print('U aggiornato con dimensioni = ',Unew.shape)

        # Evaluation of dLdW
        dargTanh2_dW = np.tensordot(dS_dargTanh2, S0, axes=((0, 1), (0, 1)))  # returns an HxH matrix
        dL_dW = dL_dargTanh1.dot(dargTanh2_dW)  # returns an HxH matrix
        Wnew = self.W - c * dL_dW
        # print('W aggiornato con dimensione = ',Wnew.shape)
        '''

        return (Vnew, Unew, Wnew)

    # Function that implements the softmax computation
    def softmax(self,s):
        # Softmax over 2D-matrix if D dimension is on axis = 0
        s -= np.amax(s, axis=0)
        s = np.exp(s)
        return s / np.sum(s, axis=0)

    # Function that implements the loss function computation
    def lossFunction(self):
        '''
        o = o.transpose()
        a = -y*np.log(o)
        return a.sum()
        '''
        O_ = np.log(self.O)
        c = (-1) / (self.N * self.T)
        return c * np.tensordot(self.Y, O_, axes=((0, 1, 2), (0, 1, 2)))

    # Function that implements the forward step in the RNN
    def training_step(self, K, mini_batch_size):
        lossTrain, lossVal = [], []
        lossT = 0.0
        idxTrain = list(range(self.n_train))

        for i in range(K):
            print('Epoch ', i, ':')
            # Training set computation
            random.shuffle(idxTrain)
            #self.N = 500
            self.N = mini_batch_size
            print('Train dimension: ', len(self.train))
            print('N dimension: ', self.N)
            print('Iteration range: ', len(self.train) // self.N)

            for j in range(len(self.train) // self.N):
                print('Iteration limits: [', j * self.N, ', ', j * self.N + self.N, ')')
                self.init_mainParam([self.train[idxTrain[k]] for k in range(j * self.N, j * self.N + self.N)])
                self.S, self.O = self.fwdRnn(self.X, self.S, self.O)
                self.V, self.U, self.W = self.bwRnn()
                lossT += self.lossFunction()
            #lossTrain += [lossT / 100]
            lossTrain += [lossT]
            # lossTrain += [lossT / (self.n_train // self.N)]
            lossT = 0.0

            # Validation set computation
            self.init_mainParam(self.val)
            self.fwdRnn(self.X, self.S, self.O)
            lossVal += [self.lossFunction()]

        return lossTrain, lossVal

    def test_step(self):
        self.init_mainParam(self.test)
        self.fwdRnn(self.X, self.S, self.O)
        print('N = ', self.N)
        lossTest = self.lossFunction()
        accTest = self.accuracy()
        return lossTest, accTest

    def accuracy(self):
        O_ = self.O.argmax(axis=2)
        Y_ = self.Y.argmax(axis=2)
        compRes = Y_ == O_
        return compRes.sum()/(self.N*self.T)
<commit_msg>code improvement<commit_after>import random
import numpy as np
import tools

class RNN:

    def __init__(self, dictionary, train, val, test, H, eta):
        self.dictionary, self.train, self.val, self.test = dictionary, train, val, test

        self.D = len(dictionary)
        self.T = max(len(s) for s in (train + val + test)) + 2
        self.H, self.eta = H, eta
        self.n_train = len(train)
        # print('Total number of phrases: ', self.N)

        # Weight assignment with Glorot uniform
        # wgtD = self.D ** (-0.5)
        # wgtH = self.H ** (-0.5)

        # self.U = np.random.uniform(-wgtD, wgtD, (self.H, self.D))  # HxD matrix
        # self.W = np.random.uniform(-wgtH, wgtH, (self.H, self.H))  # HxH matrix
        # self.V = np.random.uniform(-wgtH, wgtH, (self.D, self.H))  # DxH matrix

        # weight assignment with simple weights
        self.U = np.random.randn(self.H, self.D) * 0.01
        self.W = np.random.randn(self.H, self.H) * 0.01
        self.V = np.random.randn(self.D, self.H) * 0.01

    def init_main_params(self, data):
        # Set X (input)
        self.X = np.zeros((self.N, self.T, self.D))
        for n, sent in enumerate(data):
            self.X[n, range(len(sent)), [self.dictionary.index(x) for x in sent]] = 1.0

        # Set Y (labels)
        self.Y = np.zeros((self.N, self.T, self.D))
        self.Y[:, :-1, :] = self.X[:, 1:, :]
        # self.Y[:, -1:, 2] = 1

        # Set S and O (hidden output and output)
        self.S = np.zeros((self.N, self.T, self.H))
        self.O = np.zeros((self.N, self.T, self.D))

    # forward step of the RNN
    def forward(self, X, S, O):
        # 1. s = tanh(Ux + Ws_prev)
        # 2. o = sigma(Vs)
        # 3. U,W,V = L(y,o)

        for t in range(self.T):
            S[:, t, :] = self.out_HL(X[:, t, :].T, S[:, t - 1, :].T).T
            # O[:, t, :] = self.softmax(self.V.dot(S[:, t, :].T)).T
            O[:, t, :] = self.softmax(np.dot(S[:, t, :], self.V.T))
        return S, O

    # backward pass of the RNN
    def backprop(self):
        # Evaluation of dLdV
        dLdO = -self.Y / self.O
        dOdVS = self.O * (1.0 - self.O)
        dLdV = np.tensordot(dLdO * dOdVS, self.S, axes=((0, 1), (0, 1)))
        c = -self.eta / (self.N * self.T)  # Constant value including eta and 1/n
        Vnew = self.V - c * dLdV

        # Evaluation of dLdU
        S0 = np.zeros(self.S.shape)  # S(t-1)
        S0[:, 1:, :] = self.S[:, :-1, :]

        # Second version - correct
        dtanh = (1 - np.power(self.S, 2))
        dLdS = np.tensordot(dLdO * dOdVS, self.V, axes=(2, 0))
        dLdU = np.tensordot(dLdS * dtanh, self.X, axes=((0, 1), (0, 1)))  # returns an HxD matrix
        Unew = self.U - c * dLdU

        # Evaluation of dLdW
        dLdW = np.tensordot(dLdS * dtanh, S0, axes=((0, 1), (0, 1)))  # returns an HxH matrix
        Wnew = self.W - (c * dLdW)

        return Vnew, Unew, Wnew

    def training(self, K, mini_batch_size):
        loss_train, loss_val = [], []
        idx_train = list(range(self.n_train))
        self.N = mini_batch_size
        n_mini_batches = self.n_train // self.N

        print('Training set size: ', self.n_train)
        print('Mini-batch size: ', self.N)
        print('Number of mini-batches: ', n_mini_batches)

        for i in range(K):
            print('Epoch ', i, '/', K, ':')
            random.shuffle(idx_train)
            loss_t, loss_v = (0.0, 0.0)

            # forward and backprop steps
            for j in range(n_mini_batches):
                print('    Batch ', j + 1, '/', n_mini_batches)
                self.init_main_params([self.train[i] for i in idx_train[(j * self.N):((j + 1) * self.N)]])
                self.S, self.O = self.forward(self.X, self.S, self.O)
                self.V, self.U, self.W = self.backprop()
                loss_t += self.loss()
            loss_train.append(loss_t)
            print('    Loss: ', loss_t)
            # validation step
            print('Validation: ')
            for j in range(len(self.val) // self.N):
                print('    Batch ', j + 1, '/', len(self.val) // self.N)
                self.init_main_params(self.val[(j * self.N):((j + 1) * self.N)])
                self.forward(self.X, self.S, self.O)
                loss_v += self.loss()
            loss_val.append(loss_v)

        return loss_train, loss_val

    def testing(self):
        self.init_main_params(self.test)
        self.forward(self.X, self.S, self.O)
        print('N = ', self.N)
        loss_test = self.loss()
        acc_test = self.accuracy()
        return loss_test, acc_test

    # Function that implements the softmax computation
    def softmax(self, s):
        # Softmax over 2D-matrix if D dimension is on axis = 0
        s -= np.amax(s, axis=0)
        s = np.exp(s)
        return s / np.sum(s, axis=0)

    # Function that implements the activation of the hidden layer
    def out_HL(self, x, s_prev):
        return np.tanh(np.dot(self.U, x) + np.dot(self.W, s_prev))

    # Function that implements the loss function computation
    def loss(self):
        '''
        o = o.transpose()
        a = -y*np.log(o)
        return a.sum()
        '''
        O_ = np.log(self.O)
        c = -1 / (self.N * self.T)
        return c * np.tensordot(self.Y, O_, axes=((0, 1, 2), (0, 1, 2)))

    def accuracy(self):
        O_ = self.O.argmax(axis=2)
        Y_ = self.Y.argmax(axis=2)
        comp_res = Y_ == O_
        return comp_res.sum() / (self.N * self.T)
<|endoftext|>


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

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Conversion functions"""

import collections
import datetime
import json
import re
import urllib2

from biryani1.baseconv import *  # NOQA
from biryani1.bsonconv import *  # NOQA
from biryani1.datetimeconv import *  # NOQA
from biryani1.objectconv import *  # NOQA
from biryani1.jsonconv import *  # NOQA
from biryani1.states import default_state, State  # NOQA

N_ = lambda message: message
uuid_re = re.compile(ur'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$')

def data_to_simulation(data, state = None):
    from . import conf
    if data is None:
        return None, None
    if state is None:
        state = default_state

    request = urllib2.Request(conf['openfisca.api.url'], headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'OpenFisca-Notebook',
        })
    try:
        response = urllib2.urlopen(request, json.dumps(data))
    except urllib2.HTTPError as http_exc:
        return data, state._('API respond with HTTP code {}').format(http_exc.code)
    except urllib2.URLError:
        return data, state._('API didn\'t respond')
    response_dict = json.loads(response.read(), object_pairs_hook = collections.OrderedDict)
    return response_dict, None

date_to_datetime = function(lambda value: datetime.datetime(*(value.timetuple()[:6])))

datetime_to_date = function(lambda value: value.date())

input_to_uuid = pipe(
    cleanup_line,
    test(uuid_re.match, error = N_(u'Invalid UUID format')),
    )

input_to_words = pipe(
    input_to_slug,
    function(lambda slug: sorted(set(slug.split(u'-')))),
    empty_to_none,
    )

#json_to_item_attributes = pipe(
#    test_isinstance(dict),
#    struct(
#        dict(
#            id = pipe(
#                input_to_object_id,
#                not_none,
#                ),
#            ),
#        default = noop,  # TODO(rsoufflet)
#        ),
#    rename_item('id', '_id'),
#    )

def method(method_name, *args, **kwargs):
    def method_converter(value, state = None):
        if value is None:
            return value, None
        return getattr(value, method_name)(state or default_state, *args, **kwargs)
    return method_converter

def user_data_to_api_data(user_data, state = None):
    if user_data is None:
        return None, None
    if state is None:
        state = default_state

    # TODO(rsoufflet) Ask for annual revenue. Annual revenu != "net mensuel * 12"
    inputs = {
        'maxrev': sum([person['person_data'].get('maxrev') * 12 for person in user_data.get('personnes') or []]),
        'nmen': 2,
        'scenarios': [{
            'indiv': [
                {
                    'birth': person['person_data'].get('birth'),
                    'noichef': 0,
                    'noidec': 0,
                    'noipref': 0,
                    }
                for person in user_data.get('personnes') or []
                ],
            'year': 2006,
            }],
        'x_axis': 'sali',
        }
    return inputs_to_api_data(inputs, state = state)

inputs_to_api_data = struct(
    {
        'maxrev': pipe(anything_to_int, default(14000)),
        'nmen': pipe(anything_to_int, default(3)),
        'scenarios': pipe(
            uniform_sequence(
                struct(
                    {
                        'declar': default({'0': {}}),
                        'famille': default({'0': {}}),
                        'indiv': uniform_sequence(
                            struct(
                                {
                                    'birth': function(lambda d: d.isoformat()),
                                    'noichef': pipe(anything_to_int, default(0)),
                                    'noidec': pipe(anything_to_int, default(0)),
                                    'noipref': pipe(anything_to_int, default(0)),
                                    'quifam': pipe(cleanup_line, default('chef')),
                                    'quifoy': pipe(cleanup_line, default('vous')),
                                    'quimen': pipe(cleanup_line, default('pref')),
                                    },
                                drop_none_values = False,
                                ),
                            ),
                        'menage': default({'0': {}}),
                        'year': pipe(
                            anything_to_int,
                            test_greater_or_equal(1950),
                            test_less_or_equal(2015),
                            ),
                        },
                    ),
                drop_none_items = True,
                ),
            test(lambda l: len(l) > 0),
            ),
        'x_axis': pipe(default('sali'), test_in(['sali'])),
        },
    )
<commit_msg>Fix json.dumps to handle datetime objects.<commit_after># -*- coding: utf-8 -*-

# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <contact@openfisca.fr>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Conversion functions"""

import collections
import datetime
import json
import re
import urllib2

from biryani1.baseconv import *  # NOQA
from biryani1.bsonconv import *  # NOQA
from biryani1.datetimeconv import *  # NOQA
from biryani1.objectconv import *  # NOQA
from biryani1.jsonconv import *  # NOQA
from biryani1.states import default_state, State  # NOQA

N_ = lambda message: message
uuid_re = re.compile(ur'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$')

def data_to_simulation(data, state = None):
    from . import conf
    if data is None:
        return None, None
    if state is None:
        state = default_state

    request = urllib2.Request(conf['openfisca.api.url'], headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'OpenFisca-Notebook',
        })
    try:
        response = urllib2.urlopen(
            request,
            json.dumps(data, default = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else obj),
            )
    except urllib2.HTTPError as http_exc:
        return data, state._('API respond with HTTP code {}').format(http_exc.code)
    except urllib2.URLError:
        return data, state._('API didn\'t respond')
    response_dict = json.loads(response.read(), object_pairs_hook = collections.OrderedDict)
    return response_dict, None

date_to_datetime = function(lambda value: datetime.datetime(*(value.timetuple()[:6])))

datetime_to_date = function(lambda value: value.date())

input_to_uuid = pipe(
    cleanup_line,
    test(uuid_re.match, error = N_(u'Invalid UUID format')),
    )

input_to_words = pipe(
    input_to_slug,
    function(lambda slug: sorted(set(slug.split(u'-')))),
    empty_to_none,
    )

#json_to_item_attributes = pipe(
#    test_isinstance(dict),
#    struct(
#        dict(
#            id = pipe(
#                input_to_object_id,
#                not_none,
#                ),
#            ),
#        default = noop,  # TODO(rsoufflet)
#        ),
#    rename_item('id', '_id'),
#    )

def method(method_name, *args, **kwargs):
    def method_converter(value, state = None):
        if value is None:
            return value, None
        return getattr(value, method_name)(state or default_state, *args, **kwargs)
    return method_converter

def user_data_to_api_data(user_data, state = None):
    if user_data is None:
        return None, None
    if state is None:
        state = default_state

    # TODO(rsoufflet) Ask for annual revenue. Annual revenu != "net mensuel * 12"
    inputs = {
        'maxrev': sum([person['person_data'].get('maxrev') * 12 for person in user_data.get('personnes') or []]),
        'nmen': 2,
        'scenarios': [{
            'indiv': [
                {
                    'birth': person['person_data'].get('birth'),
                    'noichef': 0,
                    'noidec': 0,
                    'noipref': 0,
                    }
                for person in user_data.get('personnes') or []
                ],
            'year': 2006,
            }],
        'x_axis': 'sali',
        }
    return inputs_to_api_data(inputs, state = state)

inputs_to_api_data = struct(
    {
        'maxrev': pipe(anything_to_int, default(14000)),
        'nmen': pipe(anything_to_int, default(3)),
        'scenarios': pipe(
            uniform_sequence(
                struct(
                    {
                        'declar': default({'0': {}}),
                        'famille': default({'0': {}}),
                        'indiv': uniform_sequence(
                            struct(
                                {
                                    'birth': function(lambda d: d.isoformat()),
                                    'noichef': pipe(anything_to_int, default(0)),
                                    'noidec': pipe(anything_to_int, default(0)),
                                    'noipref': pipe(anything_to_int, default(0)),
                                    'quifam': pipe(cleanup_line, default('chef')),
                                    'quifoy': pipe(cleanup_line, default('vous')),
                                    'quimen': pipe(cleanup_line, default('pref')),
                                    },
                                drop_none_values = False,
                                ),
                            ),
                        'menage': default({'0': {}}),
                        'year': pipe(
                            anything_to_int,
                            test_greater_or_equal(1950),
                            test_less_or_equal(2015),
                            ),
                        },
                    ),
                drop_none_items = True,
                ),
            test(lambda l: len(l) > 0),
            ),
        'x_axis': pipe(default('sali'), test_in(['sali'])),
        },
    )
<|endoftext|>


<commit_before>"""Run an example script to quickly test."""
import asyncio
import logging

from aiohttp import ClientSession

from pymyq import login
from pymyq.errors import MyQError, RequestError
from pymyq.garagedoor import STATE_OPEN, STATE_CLOSED

_LOGGER = logging.getLogger()

EMAIL = "<EMAIL>"
PASSWORD = "<PASSWORD>"
ISSUE_COMMANDS = False

def print_info(number: int, device):
    print(f"      Device {number + 1}: {device.name}")
    print(f"      Device Online: {device.online}")
    print(f"      Device ID: {device.device_id}")
    print(
        f"      Parent Device ID: {device.parent_device_id}",
    )
    print(f"      Device Family: {device.device_family}")
    print(
        f"      Device Platform: {device.device_platform}",
    )
    print(f"      Device Type: {device.device_type}")
    print(f"      Firmware Version: {device.firmware_version}")
    print(f"      Open Allowed: {device.open_allowed}")
    print(f"      Close Allowed: {device.close_allowed}")
    print(f"      Current State: {device.state}")
    print("      ---------")

async def main() -> None:
    """Create the aiohttp session and run the example."""
    logging.basicConfig(level=logging.DEBUG)
    async with ClientSession() as websession:
        try:
            # Create an API object:
            print(f"{EMAIL} {PASSWORD}")
            api = await login(EMAIL, PASSWORD, websession)

            for account in api.accounts:
                print(f"Account ID: {account}")
                print(f"Account Name: {api.accounts[account]}")

                # Get all devices listed with this account – note that you can use
                # api.covers to only examine covers or api.lamps for only lamps.
                print(f"  GarageDoors: {len(api.covers)}")
                print("  ---------------")
                if len(api.covers) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.covers
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                        if ISSUE_COMMANDS:
                            try:
                                if device.open_allowed:
                                    if device.state == STATE_OPEN:
                                        print(
                                            f"Garage door {device.name} is already open"
                                        )
                                    else:
                                        print(f"Opening garage door {device.name}")
                                        try:
                                            if await device.open(wait_for_state=True):
                                                print(
                                                    f"Garage door {device.name} has been opened."
                                                )
                                            else:
                                                print(
                                                    f"Failed to open garage door {device.name}."
                                                )
                                        except MyQError as err:
                                            _LOGGER.error(
                                                f"Error when trying to open {device.name}: {str(err)}"
                                            )
                                else:
                                    print(
                                        f"Opening of garage door {device.name} is not allowed."
                                    )

                                if device.close_allowed:
                                    if device.state == STATE_CLOSED:
                                        print(
                                            f"Garage door {device.name} is already closed"
                                        )
                                    else:
                                        print(f"Closing garage door {device.name}")
                                        try:
                                            wait_task = await device.close(
                                                wait_for_state=False
                                            )
                                        except MyQError as err:
                                            _LOGGER.error(
                                                f"Error when trying to close {device.name}: {str(err)}"
                                            )

                                        print(f"Device {device.name} is {device.state}")

                                        if await wait_task:
                                            print(
                                                f"Garage door {device.name} has been closed."
                                            )
                                        else:
                                            print(
                                                f"Failed to close garage door {device.name}."
                                            )

                            except RequestError as err:
                                _LOGGER.error(err)
                    print("  ------------------------------")
                print(f"  Lamps: {len(api.lamps)}")
                print("  ---------")
                if len(api.lamps) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.lamps
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                        if ISSUE_COMMANDS:
                            try:
                                print(f"Turning lamp {device.name} on")
                                await device.turnon(wait_for_state=True)
                                await asyncio.sleep(5)
                                print(f"Turning lamp {device.name} off")
                                await device.turnoff(wait_for_state=True)
                            except RequestError as err:
                                _LOGGER.error(err)
                    print("  ------------------------------")

                print(f"  Gateways: {len(api.gateways)}")
                print("  ------------")
                if len(api.gateways) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.gateways
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                print("------------------------------")

        except MyQError as err:
            _LOGGER.error("There was an error: %s", err)

asyncio.get_event_loop().run_until_complete(main())
<commit_msg>Fix issue when failing to open cover<commit_after>"""Run an example script to quickly test."""
import asyncio
import logging

from aiohttp import ClientSession

from pymyq import login
from pymyq.errors import MyQError, RequestError
from pymyq.garagedoor import STATE_OPEN, STATE_CLOSED

_LOGGER = logging.getLogger()

EMAIL = "<EMAIL>"
PASSWORD = "<PASSWORD>"
ISSUE_COMMANDS = False

def print_info(number: int, device):
    print(f"      Device {number + 1}: {device.name}")
    print(f"      Device Online: {device.online}")
    print(f"      Device ID: {device.device_id}")
    print(
        f"      Parent Device ID: {device.parent_device_id}",
    )
    print(f"      Device Family: {device.device_family}")
    print(
        f"      Device Platform: {device.device_platform}",
    )
    print(f"      Device Type: {device.device_type}")
    print(f"      Firmware Version: {device.firmware_version}")
    print(f"      Open Allowed: {device.open_allowed}")
    print(f"      Close Allowed: {device.close_allowed}")
    print(f"      Current State: {device.state}")
    print("      ---------")

async def main() -> None:
    """Create the aiohttp session and run the example."""
    logging.basicConfig(level=logging.DEBUG)
    async with ClientSession() as websession:
        try:
            # Create an API object:
            print(f"{EMAIL} {PASSWORD}")
            api = await login(EMAIL, PASSWORD, websession)

            for account in api.accounts:
                print(f"Account ID: {account}")
                print(f"Account Name: {api.accounts[account]}")

                # Get all devices listed with this account – note that you can use
                # api.covers to only examine covers or api.lamps for only lamps.
                print(f"  GarageDoors: {len(api.covers)}")
                print("  ---------------")
                if len(api.covers) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.covers
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                        if ISSUE_COMMANDS:
                            try:
                                if device.open_allowed:
                                    if device.state == STATE_OPEN:
                                        print(
                                            f"Garage door {device.name} is already open"
                                        )
                                    else:
                                        print(f"Opening garage door {device.name}")
                                        try:
                                            if await device.open(wait_for_state=True):
                                                print(
                                                    f"Garage door {device.name} has been opened."
                                                )
                                            else:
                                                print(
                                                    f"Failed to open garage door {device.name}."
                                                )
                                        except MyQError as err:
                                            _LOGGER.error(
                                                f"Error when trying to open {device.name}: {str(err)}"
                                            )
                                else:
                                    print(
                                        f"Opening of garage door {device.name} is not allowed."
                                    )

                                if device.close_allowed:
                                    if device.state == STATE_CLOSED:
                                        print(
                                            f"Garage door {device.name} is already closed"
                                        )
                                    else:
                                        print(f"Closing garage door {device.name}")
                                        wait_task = None
                                        try:
                                            wait_task = await device.close(
                                                wait_for_state=False
                                            )
                                        except MyQError as err:
                                            _LOGGER.error(
                                                f"Error when trying to close {device.name}: {str(err)}"
                                            )

                                        print(f"Device {device.name} is {device.state}")

                                        if wait_task and await wait_task:
                                            print(
                                                f"Garage door {device.name} has been closed."
                                            )
                                        else:
                                            print(
                                                f"Failed to close garage door {device.name}."
                                            )

                            except RequestError as err:
                                _LOGGER.error(err)
                    print("  ------------------------------")
                print(f"  Lamps: {len(api.lamps)}")
                print("  ---------")
                if len(api.lamps) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.lamps
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                        if ISSUE_COMMANDS:
                            try:
                                print(f"Turning lamp {device.name} on")
                                await device.turnon(wait_for_state=True)
                                await asyncio.sleep(5)
                                print(f"Turning lamp {device.name} off")
                                await device.turnoff(wait_for_state=True)
                            except RequestError as err:
                                _LOGGER.error(err)
                    print("  ------------------------------")

                print(f"  Gateways: {len(api.gateways)}")
                print("  ------------")
                if len(api.gateways) != 0:
                    for idx, device_id in enumerate(
                        device_id
                        for device_id in api.gateways
                        if api.devices[device_id].account == account
                    ):
                        device = api.devices[device_id]
                        print_info(number=idx, device=device)

                print("------------------------------")

        except MyQError as err:
            _LOGGER.error("There was an error: %s", err)

asyncio.get_event_loop().run_until_complete(main())
<|endoftext|>


<commit_before><commit_msg>cluster<commit_after><|endoftext|>


<commit_before>"""
Examples of brutal plugins. Primarily used for testing.
"""

import time
import re
from brutal.core.plugin import BotPlugin, cmd, event, match, threaded

@cmd
def ping(event):
    print (event.meta)
    return 'pong, got {0!r}'.format(vars(event))

@cmd
def pingni_ma(event):
    print (event.meta)
    return '{1}: pong, got {0!r}'.format(vars(event), event.meta['nick'])

@cmd
def testargs(event):
    return 'you passed in args: {0!r}'.format(event.args)

# @event(thread=True)
def sleepevent(event):
    time.sleep(7)
    return 'SOOOOOO sleepy'

@cmd(thread=True)
def sleep(event):
    time.sleep(5)
    return 'im sleepy...'

@event
def test_event_parser(event):
    print ('EVENT!!! {0!r}'.format(event),
           event.event_type,
           isinstance(event.meta, dict),
           'body' in event.meta)

@event
def auto_welcome(event):
    if event.event_type == 'join':
        return event.meta['nick'] + ': hi!'

@match(regex=r'^hi$')
def matcher(event):
    return 'Hello to you!'

URL_REGEX = '((:?https?|ftp)://[^\s/$.?#].[^\s]*)'

from readability.readability import Document
import urllib

TAG_RE = re.compile(r'<[^>]+>')
WHITESPACE_RE = re.compile(r'\s\s+')

@threaded
@match(regex=URL_REGEX)
def url_matcher(event, url, *args, **kwargs):
    html = urllib.urlopen(url).read()
    readable_article = Document(html).summary().encode("ascii", "ignore")
    readable_article = TAG_RE.sub('', readable_article)
    readable_article = WHITESPACE_RE.sub(' ', readable_article)
    readable_article = readable_article.replace('\n', ' ')

    if len(readable_article) > 75:
        readable_article = readable_article[:75] + '...'

    readable_title = Document(html).short_title().encode("ascii", "ignore")

    return "> " + url + " > " + readable_title + " > " + readable_article

class TestPlugin(BotPlugin):

    def setup(self, *args, **kwargs):
        # print(self.log)
        self.log.debug('SETUP CALLED')

        self.log.debug('config: {0!r}'.format(self.config))

        self.counter = 0
        # self.loop_task(5, self.test_loop, now=False)
        # self.delay_task(10, self.future_task)

    def future_task(self):
        self.log.info('testing future task')
        return 'future!'

    @threaded
    def test_loop(self):
        self.log.info('testing looping task')
        return 'loop!'

    def say_hi(self, event=None):
        self.msg('from say_hi: {0!r}'.format(event), event=event)
        return 'hi'

    @threaded
    def say_delayed_hi(self, event=None):
        self.msg('from say_hi_threaded, sleeping for 5: {0!r}'.format(event),
                 event=event)
        time.sleep(5)
        return 'even more delayed hi'

    @cmd
    def runlater(self, event):
        self.delay_task(5, self.say_hi, event=event)
        self.delay_task(5, self.say_delayed_hi, event=event)
        return 'will say hi in 5 seconds'

    @cmd
    def count(self, event):
        self.counter += 1
        return 'count {1!r} from class! got {0!r}'.format(event, self.counter)

    @cmd(thread=True)
    def inlinemsg(self, event):
        self.msg('sleeping for 5 seconds!', event=event)
        time.sleep(5)
        return 'done sleeping!'

    @cmd(thread=True)
    def notify(self, event):
        args = event.args
        if len(args) < 2:
            return 'Sorry sir, I don\'t understand,' \
                   ' you gave me too few arguments'

        seconds = int(args[0])
        text = ' '.join(args[1:])

        def sender(x):
            def t():
                self.msg('Sir, I am notifying you as requested: {0}'.format(x),
                         event=event)
            return t

        self.delay_task(seconds, sender(text))

        return 'Will notify you in {0!r} seconds.'.format(seconds)
<commit_msg>update: Better handling of encoding<commit_after>"""
Examples of brutal plugins. Primarily used for testing.
"""

import time
import re
from brutal.core.plugin import BotPlugin, cmd, event, match, threaded

@cmd
def ping(event):
    print (event.meta)
    return 'pong, got {0!r}'.format(vars(event))

@cmd
def pingni_ma(event):
    print (event.meta)
    return '{1}: pong, got {0!r}'.format(vars(event), event.meta['nick'])

@cmd
def testargs(event):
    return 'you passed in args: {0!r}'.format(event.args)

# @event(thread=True)
def sleepevent(event):
    time.sleep(7)
    return 'SOOOOOO sleepy'

@cmd(thread=True)
def sleep(event):
    time.sleep(5)
    return 'im sleepy...'

@event
def test_event_parser(event):
    print ('EVENT!!! {0!r}'.format(event),
           event.event_type,
           isinstance(event.meta, dict),
           'body' in event.meta)

@event
def auto_welcome(event):
    if event.event_type == 'join':
        return event.meta['nick'] + ': hi!'

@match(regex=r'^hi$')
def matcher(event):
    return 'Hello to you!'

URL_REGEX = '((:?https?|ftp)://[^\s/$.?#].[^\s]*)'

from readability.readability import Document
import urllib

TAG_RE = re.compile(r'<[^>]+>')
WHITESPACE_RE = re.compile(r'\s\s+')

@threaded
@match(regex=URL_REGEX)
def url_matcher(event, url, *args, **kwargs):
    html = urllib.urlopen(url).read()
    readable_article = Document(html).summary().encode("utf-8")
    readable_article = TAG_RE.sub('', readable_article)
    readable_article = WHITESPACE_RE.sub(' ', readable_article)
    readable_article = readable_article.replace('\n', ' ')

    if len(readable_article) > 75:
        readable_article = readable_article[:75] + '...'

    readable_title = Document(html).short_title().encode("utf-8")

    return "> " + url + " > " + readable_title + " > " + readable_article

class TestPlugin(BotPlugin):

    def setup(self, *args, **kwargs):
        # print(self.log)
        self.log.debug('SETUP CALLED')

        self.log.debug('config: {0!r}'.format(self.config))

        self.counter = 0
        # self.loop_task(5, self.test_loop, now=False)
        # self.delay_task(10, self.future_task)

    def future_task(self):
        self.log.info('testing future task')
        return 'future!'

    @threaded
    def test_loop(self):
        self.log.info('testing looping task')
        return 'loop!'

    def say_hi(self, event=None):
        self.msg('from say_hi: {0!r}'.format(event), event=event)
        return 'hi'

    @threaded
    def say_delayed_hi(self, event=None):
        self.msg('from say_hi_threaded, sleeping for 5: {0!r}'.format(event),
                 event=event)
        time.sleep(5)
        return 'even more delayed hi'

    @cmd
    def runlater(self, event):
        self.delay_task(5, self.say_hi, event=event)
        self.delay_task(5, self.say_delayed_hi, event=event)
        return 'will say hi in 5 seconds'

    @cmd
    def count(self, event):
        self.counter += 1
        return 'count {1!r} from class! got {0!r}'.format(event, self.counter)

    @cmd(thread=True)
    def inlinemsg(self, event):
        self.msg('sleeping for 5 seconds!', event=event)
        time.sleep(5)
        return 'done sleeping!'

    @cmd(thread=True)
    def notify(self, event):
        args = event.args
        if len(args) < 2:
            return 'Sorry sir, I don\'t understand,' \
                   ' you gave me too few arguments'

        seconds = int(args[0])
        text = ' '.join(args[1:])

        def sender(x):
            def t():
                self.msg('Sir, I am notifying you as requested: {0}'.format(x),
                         event=event)
            return t

        self.delay_task(seconds, sender(text))

        return 'Will notify you in {0!r} seconds.'.format(seconds)
<|endoftext|>