<commit_before>#!/usr/bin/python3

# This is the updater for the Wren blogging platform.
# Before running this script please familiarise your-
# self with the instructions in the readme.md file.

from datetime import datetime
from math import floor
from os import getcwd, makedirs, path, walk
from re import search, sub
from shutil import copy
from time import sleep

# These functions allow for timeout when the Wren
# updater is run via the "Run In Terminal" command

def sucess(message):
    print(message + "\n")
    sleep(3)
    exit(0)

def gerror(message):
    print("ERROR: {0}".format(message))
    sleep(3)
    exit(1)

# At various points it's needed to have a function
# that replaces all instances of a word in a file.
# The easy solution would be to os.call 'sed' but
# I wanted to avoid relying on outside programs.

def sed(find, replace, target):
    with open(target, "r", encoding='utf-8') as file:
        lines = file.readlines()
    with open(target, "w", encoding='utf-8') as file:
        for line in lines:
            file.write(sub(find, replace, line))

def supsed(find, replace):
    for root, dirs, files in walk("../", topdown=False):
        for name in files:
            target = path.join(root, name)
            if (".git" in target) or ("images" in target):
                pass
            else:
                sed(find, replace, target)

# Checks running from the .wren directory so that the
# relative locations of files can be known.

if ".wren" not in getcwd():
    gerror("please run from the .Wren directory")

# If the Wren has not been run before the following code
# will run. This sets up all the files with any variables
# that are user modified. WARNING: that this code does not
# currently check if the URL is correctly formatted, nor
# does it check if it actually exists.

if not path.isfile("catdat.csv"):
    print("Setting up Wren...")

    blogger = input("What is your full name? ")
    supsed("BLOGGER", blogger)

    blogurl = input("What is your website url? ")
    supsed("BLOGURL", blogurl)

    # Wren can integrate Disqus as a comments system if
    # the user so wants. In future versions I'm going to
    # have a look around for alternative systems.
    q = 0
    while q == 0:
        ans = input("Do you wish to use Disqus for comments? (y/n) ")
        if ans == "y":
            print("If you don't have a Disqus account go to")
            print("https://disqus.com/profile/signup/ now!")
            shortname = input("What is your Disqus shortname? ")
            sed("DSHORTNAME", shortname, "../php/comments.php")
            q = 1
            pass
        elif ans == "n":
            with open("../php/comments.php", "w+") as file:
                file.write('
\n')
            q = 1
            pass

    with open("catdat.csv", "w+") as file:
        file.write("Category,Count")

    sucess("Setup complete!")

# Extracts the blog body from the input file. It's fixed
# so that the level of indentation doesn't need changing.

with open("blog-new.html", 'r') as file:
    record, HTMLcontent = False, []
    for line in file:
        if "
" in line:
            record = True
        elif "
" in line:
            record = False
        elif record is True:
            HTMLcontent.append(line)

# Strips out the HTML tags and white space in order
# to calculate the approximate reading time.

TXTcontent = []
for line in HTMLcontent:
    raw = ''
    record = True
    for i in line:
        if i == "<":
            record = False
        elif i == ">":
            record = True
        elif record is True:
            raw += i
    TXTcontent.append(raw.strip())
text = " ".join(TXTcontent)

# This calculates the approximate reading time for the blog.
# The calculation is based upon an average screen-reading
# rate of ~200 words per minute.

if len(text)/200 < 1:
    secs = int(float(len(text))*0.3)
    time = "~{0} seconds".format(secs)
else:
    mins = int(floor(len(text)/float(200)))
    secs = int(((len(text)/float(200))-mins)*60)
    time = "~{0} minutes {1} seconds".format(mins, secs)

readtime = time + ' reading'

print("Please enter the following data as prompted")
title = input("\nTitle:\n")
catagories = input("\nCatagories:\n").split(" ")
summary = input("\nSummary:\n")

# The time of the blogs creation is needed in a variety of
# formats, not all provide by the datetime module.

# YYYY-MM-DD
datesmll = str(datetime.date(datetime.now()))
# YYYY/MM/DD
filedate = sub(r"-", "/", datesmll)
# Day DD Mon YYYY HH:MM:SS
datelong = str(datetime.ctime(datetime.now()))
# Day, DD Mon YYYY HH:MM:SS TMZ
datelong = ", ".join(datelong.split(" ", 1)) + " GMT"

# Data used in URLs has to be devoid of any special characters
# that could cause problems. Because of that, characters not
# in the range a-z or 0-1 are removed from certain values.

def clean(string):
    tmp = list(string.lower().strip())
    tmp = [i for i in tmp if search("[^a-z0-9]", i) is None]
    return "".join(tmp)

urltitle = clean(title)
url = "http://BLOGURL/blog/" + filedate + "/" + urltitle

tmp = []
for item in catagories:
    item = clean(item)
    if len(item) != 0 and item not in tmp:
        tmp.append(item)
catagories = tmp

print("\nGIVEN INFORMATION")
print("Title:", title)
print("URL:", url)
print("Time:", datelong)
print("Reading time:", time)
print("Catagories:", catagories)
print("Summary:", summary, "\n")

q = 0
while q == 0:
    ans = input("Are you happy with the above? (y/n) ")
    if ans == "y":
        q = 1
        pass
    elif ans == "n":
        gerror("meta data rejected")
print("Meta data accepted\n")

# Copies the blog data to the template file and then moves
# it into place. The formatting for blog-template.html is
# set for the destination rather than its current location

dest = "../blog/{0}/{1}.html".format(filedate, urltitle)
if not path.exists(path.dirname(dest)):
    makedirs(path.dirname(dest))
copy("blog-template.html", dest)
sed("BLOGDATE", datesmll, dest)
sed("BLOGTITLE", title, dest)
sed("READINGTIME", readtime, dest)
sed("BLOGCONTENT", "".join(HTMLcontent), dest)

# When a new catagory is created it needs all the files
# associated with it creating too, along with the existing
# files that need to refrence it updating.

with open("catdat.csv", "r") as file:
    catdat = [line.strip() for line in file]
catdat.pop(0)
catnames = [line.split(",")[0] for line in catdat]
catcount = [int(line.split(",")[1]) for line in catdat]

newcat = []
for item in catagories:
    if item not in catnames:
        newcat.append(item)

for cat in newcat:
    copy("feed-template.xml", "../blog/feed/" + cat + ".xml")
    copy("cat-template.html", "../blog/cat/" + cat + ".html")
    sed("TEMPLATE", cat, "../blog/feed/" + cat + ".xml")
    sed("TEMPLATE", cat, "../blog/cat/" + cat + ".html")
    catnames.append(cat)
    catcount.append(0)
    print("Created", cat, "catagory")

for cat in catagories:
    pos = catnames.index(cat)
    count = catcount[pos] + 1
    catcount.pop(pos)
    catcount.insert(pos, count)

with open("catdat.csv", "w") as file:
    file.write("Category,Count\n")
    for x in range(0, len(catnames)):
        file.write("{0},{1}\n".format(catnames[x], catcount[x]))

# This inserts a single line of HTML which links to the new
# blog into each of the catagory lists which it falls into.

newline = '
{1} - {2}
'
newline = newline.format(url, datesmll, title)

for cat in (catagories + ["all"]):
    with open("../blog/cat/" + cat + ".html", "r") as file:
        lines = [line for line in file]
    for line in lines:
        if "" in line:
            n = lines.index(line)
            lines.insert(n + 1, ' ' * 12 + newline + "\n")
    catfile = open("../blog/cat/" + cat + ".html", "w+")
    for line in lines:
        catfile.write(line)
    catfile.close()
    print("Added blog to", cat, "catagory list")

# Inserts a the nessisary lines into the XML files so that
# the new blog shows up in the RSS feeds it alls into. Note
# that this process doesn't currently remove any blogs from
# the RSS file. This is recommended as it prevents the file
# from becoming too big. I'll do some research and see what
# others consider to be an acceptable cutoff point.

newlines = [
    "<item>",
    "    <title>" + title + "</title>",
    "    <link>" + url + "</link>",
    "    <guid>" + url + "</guid>",
    "    <pubDate>" + datelong + "</pubDate>",
    "    <description>" + summary + "</description>",
    "</item>\n"
]

for cat in (catagories + ["all"]):
    with open("../blog/feed/" + cat + ".xml", "r") as file:
        lines = [line for line in file]
    for line in lines:
        if "" in line:
            n = lines.index(line)
            for x in range(0, 7):
                lines.insert(n + x + 1, " " * 8 + newlines[x] + "\n")
    catfeed = open("../blog/feed/" + cat + ".xml", "w+")
    for line in lines:
        catfeed.write(line)
    catfeed.close()
    print("Added blog to", cat, "RSS feed")

# The main blogs page lists the 5 most recent blogs posted so
# adding this new one means also removing the least recent.
# IMPORTANT: this code currently just removes the least recent
# post without checking if 5 blogs have actually been posted
# yet. I'll try to correct this in future versions.

newlines = [
    "
",
    '
{1} - {2}
'.format(url, title, datesmll),
    "
" + summary + "
",
    "
\n",
    '
\n'
]

with open("../blog/index.html", 'r') as file:
    lines = [line for line in file]
for line in lines:
    if "" in line:
        n = lines.index(line)
        for x in range(0, 5):
            lines.insert(n + x + 1, " " * 8 + newlines[x] + "\n")
    elif "" in line:
        n = lines.index(line)
        for x in range(1, 8):
            lines.pop(n-x)
blogfile = open("../blog/index.html", "w+")
for line in lines:
    blogfile.write(line)
blogfile.close()
print("Updated the main blogs page")

# The following code will update the catagory word cloud. The
# design is inspired by that used by TagCrowd.com but isn't
# disimilar to the ones commonly used on Wordpress. Note that
# this code does not yet emulate the colouring aspect of the
# word clouds as this requires a more sophisticated algorithm.
# The unit used for font size is 'em'

smallsize = 1.0
largesize = 5.0
sizerange = largesize - smallsize
countrange = max(catcount)-min(catcount)

try:
    ratio = sizerange/countrange
except:
    ratio = 1

catsizes = [count * ratio for count in catcount]

string = "
"
for cat in catnames:
    style = "font-size: {0}em;".format(catsizes[catnames.index(cat)])
    string += ''.format(cat, style)
    string += cat + " "
string += "
\n"

with open("../blog/cat/index.html", "r") as file:
    lines = [line for line in file]
for line in lines:
    if "" in line:
        index = lines.index(line) + 1
        lines.pop(index)
        lines.insert(index, string)
cloudfile = open("../blog/cat/index.html", "w+")
for line in lines:
    cloudfile.write(line)
cloudfile.close()
print("Updated the catagory wordcloud")

sucess("Done!")
<commit_msg>Sorts catagories alphabetically<commit_after>#!/usr/bin/python3

# This is the updater for the Wren blogging platform.
# Before running this script please familiarise your-
# self with the instructions in the readme.md file.

from datetime import datetime
from math import floor
from os import getcwd, makedirs, path, walk
from re import search, sub
from shutil import copy
from time import sleep

# These functions allow for timeout when the Wren
# updater is run via the "Run In Terminal" command

def sucess(message):
    print(message + "\n")
    sleep(3)
    exit(0)

def gerror(message):
    print("ERROR: {0}".format(message))
    sleep(3)
    exit(1)

# At various points it's needed to have a function
# that replaces all instances of a word in a file.
# The easy solution would be to os.call 'sed' but
# I wanted to avoid relying on outside programs.

def sed(find, replace, target):
    with open(target, "r", encoding='utf-8') as file:
        lines = file.readlines()
    with open(target, "w", encoding='utf-8') as file:
        for line in lines:
            file.write(sub(find, replace, line))

def supsed(find, replace):
    for root, dirs, files in walk("../", topdown=False):
        for name in files:
            target = path.join(root, name)
            if (".git" in target) or ("images" in target):
                pass
            else:
                sed(find, replace, target)

# Checks running from the .wren directory so that the
# relative locations of files can be known.

if ".wren" not in getcwd():
    gerror("please run from the .Wren directory")

# If the Wren has not been run before the following code
# will run. This sets up all the files with any variables
# that are user modified. WARNING: that this code does not
# currently check if the URL is correctly formatted, nor
# does it check if it actually exists.

if not path.isfile("catdat.csv"):
    print("Setting up Wren...")

    blogger = input("What is your full name? ")
    supsed("BLOGGER", blogger)

    blogurl = input("What is your website url? ")
    supsed("BLOGURL", blogurl)

    # Wren can integrate Disqus as a comments system if
    # the user so wants. In future versions I'm going to
    # have a look around for alternative systems.
    q = 0
    while q == 0:
        ans = input("Do you wish to use Disqus for comments? (y/n) ")
        if ans == "y":
            print("If you don't have a Disqus account go to")
            print("https://disqus.com/profile/signup/ now!")
            shortname = input("What is your Disqus shortname? ")
            sed("DSHORTNAME", shortname, "../php/comments.php")
            q = 1
            pass
        elif ans == "n":
            with open("../php/comments.php", "w+") as file:
                file.write('
\n')
            q = 1
            pass

    with open("catdat.csv", "w+") as file:
        file.write("Category,Count")

    sucess("Setup complete!")

# Extracts the blog body from the input file. It's fixed
# so that the level of indentation doesn't need changing.

with open("blog-new.html", 'r') as file:
    record, HTMLcontent = False, []
    for line in file:
        if "
" in line:
            record = True
        elif "
" in line:
            record = False
        elif record is True:
            HTMLcontent.append(line)

# Strips out the HTML tags and white space in order
# to calculate the approximate reading time.

TXTcontent = []
for line in HTMLcontent:
    raw = ''
    record = True
    for i in line:
        if i == "<":
            record = False
        elif i == ">":
            record = True
        elif record is True:
            raw += i
    TXTcontent.append(raw.strip())
text = " ".join(TXTcontent)

# This calculates the approximate reading time for the blog.
# The calculation is based upon an average screen-reading
# rate of ~200 words per minute.

if len(text)/200 < 1:
    secs = int(float(len(text))*0.3)
    time = "~{0} seconds".format(secs)
else:
    mins = int(floor(len(text)/float(200)))
    secs = int(((len(text)/float(200))-mins)*60)
    time = "~{0} minutes {1} seconds".format(mins, secs)

readtime = time + ' reading'

print("Please enter the following data as prompted")
title = input("\nTitle:\n")
catagories = input("\nCatagories:\n").split(" ")
summary = input("\nSummary:\n")

# The time of the blogs creation is needed in a variety of
# formats, not all provide by the datetime module.

# YYYY-MM-DD
datesmll = str(datetime.date(datetime.now()))
# YYYY/MM/DD
filedate = sub(r"-", "/", datesmll)
# Day DD Mon YYYY HH:MM:SS
datelong = str(datetime.ctime(datetime.now()))
# Day, DD Mon YYYY HH:MM:SS TMZ
datelong = ", ".join(datelong.split(" ", 1)) + " GMT"

# Data used in URLs has to be devoid of any special characters
# that could cause problems. Because of that, characters not
# in the range a-z or 0-1 are removed from certain values.

def clean(string):
    tmp = list(string.lower().strip())
    tmp = [i for i in tmp if search("[^a-z0-9]", i) is None]
    return "".join(tmp)

urltitle = clean(title)
url = "http://BLOGURL/blog/" + filedate + "/" + urltitle

tmp = []
for item in catagories:
    item = clean(item)
    if len(item) != 0 and item not in tmp:
        tmp.append(item)
catagories = tmp

print("\nGIVEN INFORMATION")
print("Title:", title)
print("URL:", url)
print("Time:", datelong)
print("Reading time:", time)
print("Catagories:", catagories)
print("Summary:", summary, "\n")

q = 0
while q == 0:
    ans = input("Are you happy with the above? (y/n) ")
    if ans == "y":
        q = 1
        pass
    elif ans == "n":
        gerror("meta data rejected")
print("Meta data accepted\n")

# Copies the blog data to the template file and then moves
# it into place. The formatting for blog-template.html is
# set for the destination rather than its current location

dest = "../blog/{0}/{1}.html".format(filedate, urltitle)
if not path.exists(path.dirname(dest)):
    makedirs(path.dirname(dest))
copy("blog-template.html", dest)
sed("BLOGDATE", datesmll, dest)
sed("BLOGTITLE", title, dest)
sed("READINGTIME", readtime, dest)
sed("BLOGCONTENT", "".join(HTMLcontent), dest)

# When a new catagory is created it needs all the files
# associated with it creating too, along with the existing
# files that need to refrence it updating.

with open("catdat.csv", "r") as file:
    catdat = [line.strip() for line in file]
catdat.pop(0)
catnames = [line.split(",")[0] for line in catdat]
catcount = [int(line.split(",")[1]) for line in catdat]

newcat = []
for item in catagories:
    if item not in catnames:
        newcat.append(item)

for cat in newcat:
    copy("feed-template.xml", "../blog/feed/" + cat + ".xml")
    copy("cat-template.html", "../blog/cat/" + cat + ".html")
    sed("TEMPLATE", cat, "../blog/feed/" + cat + ".xml")
    sed("TEMPLATE", cat, "../blog/cat/" + cat + ".html")
    catnames.append(cat)
    catcount.append(0)
    print("Created", cat, "catagory")

for cat in catagories:
    pos = catnames.index(cat)
    newcount = catcount[pos] + 1
    catcount.pop(pos)
    catcount.insert(pos, newcount)

combined = sorted(list(zip(catnames, catcount)))
catnames = [name for (name, count) in combined]
catcount = [count for (name, count) in combined]

with open("catdat.csv", "w") as file:
    file.write("Category,Count\n")
    for x in range(0, len(catnames)):
        file.write("{0},{1}\n".format(catnames[x], catcount[x]))

# This inserts a single line of HTML which links to the new
# blog into each of the catagory lists which it falls into.

newline = '
{1} - {2}
'
newline = newline.format(url, datesmll, title)

for cat in (catagories + ["all"]):
    with open("../blog/cat/" + cat + ".html", "r") as file:
        lines = [line for line in file]
    for line in lines:
        if "" in line:
            n = lines.index(line)
            lines.insert(n + 1, ' ' * 12 + newline + "\n")
    catfile = open("../blog/cat/" + cat + ".html", "w+")
    for line in lines:
        catfile.write(line)
    catfile.close()
    print("Added blog to", cat, "catagory list")

# Inserts a the nessisary lines into the XML files so that
# the new blog shows up in the RSS feeds it alls into. Note
# that this process doesn't currently remove any blogs from
# the RSS file. This is recommended as it prevents the file
# from becoming too big. I'll do some research and see what
# others consider to be an acceptable cutoff point.

newlines = [
    "<item>",
    "    <title>" + title + "</title>",
    "    <link>" + url + "</link>",
    "    <guid>" + url + "</guid>",
    "    <pubDate>" + datelong + "</pubDate>",
    "    <description>" + summary + "</description>",
    "</item>\n"
]

for cat in (catagories + ["all"]):
    with open("../blog/feed/" + cat + ".xml", "r") as file:
        lines = [line for line in file]
    for line in lines:
        if "" in line:
            n = lines.index(line)
            for x in range(0, 7):
                lines.insert(n + x + 1, " " * 8 + newlines[x] + "\n")
    catfeed = open("../blog/feed/" + cat + ".xml", "w+")
    for line in lines:
        catfeed.write(line)
    catfeed.close()
    print("Added blog to", cat, "RSS feed")

# The main blogs page lists the 5 most recent blogs posted so
# adding this new one means also removing the least recent.
# IMPORTANT: this code currently just removes the least recent
# post without checking if 5 blogs have actually been posted
# yet. I'll try to correct this in future versions.

newlines = [
    "
",
    '
{1} - {2}
'.format(url, title, datesmll),
    "
" + summary + "
",
    "
\n",
    '
\n'
]

with open("../blog/index.html", 'r') as file:
    lines = [line for line in file]
for line in lines:
    if "" in line:
        n = lines.index(line)
        for x in range(0, 5):
            lines.insert(n + x + 1, " " * 8 + newlines[x] + "\n")
    elif "" in line:
        n = lines.index(line)
        for x in range(1, 8):
            lines.pop(n-x)
blogfile = open("../blog/index.html", "w+")
for line in lines:
    blogfile.write(line)
blogfile.close()
print("Updated the main blogs page")

# The following code will update the catagory word cloud. The
# design is inspired by that used by TagCrowd.com but isn't
# disimilar to the ones commonly used on Wordpress. Note that
# this code does not yet emulate the colouring aspect of the
# word clouds as this requires a more sophisticated algorithm.
# The unit used for font size is 'em'

smallsize = 1.0
largesize = 5.0
sizerange = largesize - smallsize
countrange = max(catcount)-min(catcount)

try:
    ratio = sizerange/countrange
except:
    ratio = 1

catsizes = [count * ratio for count in catcount]

string = "
"
for cat in catnames:
    style = "font-size: {0}em;".format(catsizes[catnames.index(cat)])
    string += ''.format(cat, style)
    string += cat + " "
string += "
\n"

with open("../blog/cat/index.html", "r") as file:
    lines = [line for line in file]
for line in lines:
    if "" in line:
        index = lines.index(line) + 1
        lines.pop(index)
        lines.insert(index, string)
cloudfile = open("../blog/cat/index.html", "w+")
for line in lines:
    cloudfile.write(line)
cloudfile.close()
print("Updated the catagory wordcloud")

sucess("Done!")
<|endoftext|>


<commit_before>from apiclient import discovery
from datetime import datetime
from dateutil import parser

from reservas.settings.base import GOOGLE_CALENDAR_TOKEN

def crear_servicio():
    return discovery.build('calendar', 'v3', developerKey=GOOGLE_CALENDAR_TOKEN)

def get_fecha_actual():
    now = datetime.utcnow().isoformat() + 'Z'
    return now

def generar_lista_eventos(eventos):
    lista_eventos = []

    for evento in eventos:
        titulo = evento['summary']
        inicio = evento['start'].get('dateTime', evento['start'].get('date'))
        fin = evento['end'].get('dateTime', evento['end'].get('date'))
        inicio = parser.parse(inicio)
        fin = parser.parse(fin)

        evento = {
            'titulo': titulo,
            'inicio': inicio,
            'fin': fin,
            'inicio_str': inicio.strftime("%Y-%m-%dT%H:%M:%S"),
            'fin_str': fin.strftime("%Y-%m-%dT%H:%M:%S"),
        }
        lista_eventos.append(evento)

    return lista_eventos

def obtener_eventos(calendar_id):
    service = crear_servicio()

    events_result = service.events().list(
        calendarId=calendar_id,
        maxResults=2500,
        singleEvents=True,
        orderBy='startTime'
    ).execute()

    eventos = events_result.get('items', [])
    return generar_lista_eventos(eventos)

def obtener_eventos_dia(calendar_id):
    service = crear_servicio()

    events_result = service.events().list(
        calendarId=calendar_id,
        timeMin=get_fecha_actual(),
        singleEvents=True,
        orderBy='startTime'
    ).execute()

    eventos = events_result.get('items', [])
    return generar_lista_eventos(eventos)
<commit_msg>Manejo de eventos de Google Calendar sin título<commit_after># coding=utf-8

from apiclient import discovery
from datetime import datetime
from dateutil import parser

from reservas.settings.base import GOOGLE_CALENDAR_TOKEN

def crear_servicio():
    return discovery.build('calendar', 'v3', developerKey=GOOGLE_CALENDAR_TOKEN)

def get_fecha_actual():
    now = datetime.utcnow().isoformat() + 'Z'
    return now

def generar_lista_eventos(eventos):
    lista_eventos = []

    for evento in eventos:
        if 'summary' in evento:
            titulo = evento['summary']
        else:
            titulo = u'Evento sin título'
        inicio = evento['start'].get('dateTime', evento['start'].get('date'))
        fin = evento['end'].get('dateTime', evento['end'].get('date'))
        inicio = parser.parse(inicio)
        fin = parser.parse(fin)

        evento = {
            'titulo': titulo,
            'inicio': inicio,
            'fin': fin,
            'inicio_str': inicio.strftime("%Y-%m-%dT%H:%M:%S"),
            'fin_str': fin.strftime("%Y-%m-%dT%H:%M:%S"),
        }
        lista_eventos.append(evento)

    return lista_eventos

def obtener_eventos(calendar_id):
    service = crear_servicio()

    events_result = service.events().list(
        calendarId=calendar_id,
        maxResults=2500,
        singleEvents=True,
        orderBy='startTime'
    ).execute()

    eventos = events_result.get('items', [])
    return generar_lista_eventos(eventos)

def obtener_eventos_dia(calendar_id):
    service = crear_servicio()

    events_result = service.events().list(
        calendarId=calendar_id,
        timeMin=get_fecha_actual(),
        singleEvents=True,
        orderBy='startTime'
    ).execute()

    eventos = events_result.get('items', [])
    return generar_lista_eventos(eventos)
<|endoftext|>


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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.eager import context
from tensorflow.python.framework import ops
from tensorflow.python.platform import googletest

class OpsEnableEagerTest(googletest.TestCase):

  def test_enable_eager_execution_multiple_times(self):
    ops.enable_eager_execution()
    self.assertTrue(context.executing_eagerly())

    # Calling enable eager execution a second time should not cause an error.
    ops.enable_eager_execution()
    self.assertTrue(context.executing_eagerly())

if __name__ == '__main__':
  googletest.main()
<commit_msg>TF Framework ops_eager_test missing test case add 1-disable_eager_execution api test case<commit_after># Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests enabling eager execution at process level."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.eager import context
from tensorflow.python.framework import ops
from tensorflow.python.platform import googletest

class OpsEnableAndDisableEagerTest(googletest.TestCase):

  def setUp(self):
    # test for enable eager test
    ops.enable_eager_execution()
    self.assertTrue(context.executing_eagerly())

    # Calling enable eager execution a second time should not cause an error.
    ops.enable_eager_execution()
    self.assertTrue(context.executing_eagerly())

  def tearDown(self):
    # test for disable eager test
    ops.disable_eager_execution()
    self.assertFalse(context.executing_eagerly())

    # Calling disable eager execution a second time should not cause an error.
    ops.disable_eager_execution()
    self.assertFalse(context.executing_eagerly())

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


<commit_before>#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
from argparse import ArgumentParser
import numpy as nm

import sys
sys.path.append('.')

from sfepy.base.base import IndexedStruct
from sfepy.discrete import (FieldVariable, Material, Integral, Function,
                            Equation, Equations, Problem)
from sfepy.discrete.fem import Mesh, FEDomain, Field
from sfepy.terms import Term
from sfepy.discrete.conditions import Conditions, EssentialBC
from sfepy.solvers.ls import ScipyDirect
from sfepy.solvers.nls import Newton
from sfepy.postprocess.viewer import Viewer
from sfepy.mechanics.matcoefs import stiffness_from_lame

def shift_u_fun(ts, coors, bc=None, problem=None, shift=0.0):
    """
    Define a displacement depending on the y coordinate.
    """
    val = shift * coors[:,1]**2

    return val

helps = {
    'show' : 'show the results figure',
}

def main():
    from sfepy import data_dir

    parser = ArgumentParser()
    parser.add_argument('--version', action='version', version='%(prog)s')
    parser.add_argument('-s', '--show',
                      action="store_true", dest='show',
                      default=False, help=helps['show'])
    options = parser.parse_args()

    mesh = Mesh.from_file(data_dir + '/meshes/2d/rectangle_tri.mesh')
    domain = FEDomain('domain', mesh)

    min_x, max_x = domain.get_mesh_bounding_box()[:,0]
    eps = 1e-8 * (max_x - min_x)
    omega = domain.create_region('Omega', 'all')
    gamma1 = domain.create_region('Gamma1',
                                  'vertices in x < %.10f' % (min_x + eps),
                                  'facet')
    gamma2 = domain.create_region('Gamma2',
                                  'vertices in x > %.10f' % (max_x - eps),
                                  'facet')

    field = Field.from_args('fu', nm.float64, 'vector', omega, approx_order=2)

    u = FieldVariable('u', 'unknown', field)
    v = FieldVariable('v', 'test', field, primary_var_name='u')

    m = Material('m', D=stiffness_from_lame(dim=2, lam=1.0, mu=1.0))
    f = Material('f', val=[[0.02], [0.01]])

    integral = Integral('i', order=3)

    t1 = Term.new('dw_lin_elastic(m.D, v, u)',
         integral, omega, m=m, v=v, u=u)
    t2 = Term.new('dw_volume_lvf(f.val, v)', integral, omega, f=f, v=v)
    eq = Equation('balance', t1 + t2)
    eqs = Equations([eq])

    fix_u = EssentialBC('fix_u', gamma1, {'u.all' : 0.0})

    bc_fun = Function('shift_u_fun', shift_u_fun, extra_args={'shift' : 0.01})
    shift_u = EssentialBC('shift_u', gamma2, {'u.0' : bc_fun})

    ls = ScipyDirect({})

    nls_status = IndexedStruct()
    nls = Newton({}, lin_solver=ls, status=nls_status)

    pb = Problem('elasticity', equations=eqs, nls=nls, ls=ls)
    pb.save_regions_as_groups('regions')

    pb.time_update(ebcs=Conditions([fix_u, shift_u]))

    vec = pb.solve()
    print(nls_status)

    pb.save_state('linear_elasticity.vtk', vec)

    if options.show:
        view = Viewer('linear_elasticity.vtk')
        view(vector_mode='warp_norm', rel_scaling=2,
             is_scalar_bar=True, is_wireframe=True)

if __name__ == '__main__':
    main()
<commit_msg>Updated linear_elasticity_interactive.py according to tutorial.rst<commit_after>#!/usr/bin/env python
from __future__ import print_function
from __future__ import absolute_import
from argparse import ArgumentParser
import numpy as nm

import sys
sys.path.append('.')

from sfepy.base.base import IndexedStruct
from sfepy.discrete import (FieldVariable, Material, Integral, Function,
                            Equation, Equations, Problem)
from sfepy.discrete.fem import Mesh, FEDomain, Field
from sfepy.terms import Term
from sfepy.discrete.conditions import Conditions, EssentialBC
from sfepy.solvers.ls import ScipyDirect
from sfepy.solvers.nls import Newton
from sfepy.postprocess.viewer import Viewer

def shift_u_fun(ts, coors, bc=None, problem=None, shift=0.0):
    """
    Define a displacement depending on the y coordinate.
    """
    val = shift * coors[:,1]**2

    return val

helps = {
    'show' : 'show the results figure',
}

def main():
    from sfepy import data_dir

    parser = ArgumentParser()
    parser.add_argument('--version', action='version', version='%(prog)s')
    parser.add_argument('-s', '--show',
                        action="store_true", dest='show',
                        default=False, help=helps['show'])
    options = parser.parse_args()

    mesh = Mesh.from_file(data_dir + '/meshes/2d/rectangle_tri.mesh')
    domain = FEDomain('domain', mesh)

    min_x, max_x = domain.get_mesh_bounding_box()[:,0]
    eps = 1e-8 * (max_x - min_x)
    omega = domain.create_region('Omega', 'all')
    gamma1 = domain.create_region('Gamma1',
                                  'vertices in x < %.10f' % (min_x + eps),
                                  'facet')
    gamma2 = domain.create_region('Gamma2',
                                  'vertices in x > %.10f' % (max_x - eps),
                                  'facet')

    field = Field.from_args('fu', nm.float64, 'vector', omega,
                            space='H1', poly_space_base='lagrange',
                            approx_order=2)

    u = FieldVariable('u', 'unknown', field)
    v = FieldVariable('v', 'test', field, primary_var_name='u')

    m = Material('m', lam=1.0, mu=1.0)
    f = Material('f', val=[[0.02], [0.01]])

    integral = Integral('i', order=3)

    t1 = Term.new('dw_lin_elastic_iso(m.lam, m.mu, v, u)',
                  integral, omega, m=m, v=v, u=u)
    t2 = Term.new('dw_volume_lvf(f.val, v)', integral, omega, f=f, v=v)
    eq = Equation('balance', t1 + t2)
    eqs = Equations([eq])

    fix_u = EssentialBC('fix_u', gamma1, {'u.all' : 0.0})

    bc_fun = Function('shift_u_fun', shift_u_fun,
                      extra_args={'shift' : 0.01})
    shift_u = EssentialBC('shift_u', gamma2, {'u.0' : bc_fun})

    ls = ScipyDirect({})

    nls_status = IndexedStruct()
    nls = Newton({}, lin_solver=ls, status=nls_status)

    pb = Problem('elasticity', equations=eqs, nls=nls, ls=ls)
    pb.save_regions_as_groups('regions')

    pb.time_update(ebcs=Conditions([fix_u, shift_u]))

    vec = pb.solve()
    print(nls_status)

    pb.save_state('linear_elasticity.vtk', vec)

    if options.show:
        view = Viewer('linear_elasticity.vtk')
        view(vector_mode='warp_norm', rel_scaling=2,
             is_scalar_bar=True, is_wireframe=True)

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


<commit_before>import os
import binascii

FILE_PATH = "./00.bin"	#目标文件路径
UNIT_BYTE = 2	#头文件 UNIT_BYTE 字节为一组

if __name__ == '__main__':
	print("\r\nThe function is bin to .h file.\r\n")
	if os.path.exists(FILE_PATH):
		f_i = open(FILE_PATH,"rb")
		f_o = open("00.h", "r+")	#可读；可写；可追加

		if UNIT_BYTE == 2:
			f_o.write("unsigned short const array[] = {\n")
		lines = f_i.readlines()
		print(lines)
		for line in lines:
			#print("line:",line)
			line_str = str(binascii.b2a_hex(line))[2:-1]
			print(line_str)
			lens = len(line_str)
			unit_count = UNIT_BYTE*2
			lens_for = int(lens/unit_count)
			print("len:",lens,unit_count,lens_for)
			for i in range(0,lens_for):
				unit_byte = line_str[:4]
				line_str = line_str[4:]
				print(unit_byte)
				f_o.write(unit_byte+",\n")

		f_o.write("};\n")
	else:
		print("please edit the input file name as 00.bin.")<commit_msg>add search bin file in courrent file.<commit_after>import os
import binascii

UNIT_BYTE = 2	#头文件 UNIT_BYTE 字节为一组

def Find_Bin_File():
	filenames = []
	cur_dir = os.getcwd()
	for file in os.listdir(cur_dir):
		if file[-4:] == ".bin":
			filename = file[:-4]
			#print(filename)
			filenames.append(filename)
	return filenames

if __name__ == '__main__':
	print("\r\nThe function is bin to .h file.\r\n")
	filenames = Find_Bin_File()
	if filenames == []:
		print("ERROR: not find the file in courrent list.")
	else:
		for filename in filenames:
			print("file name:",filename)
			f_i = open(filename+".bin","rb")
			f_o = open(filename+".h", "w+")

			if UNIT_BYTE == 2:
				f_o.write("unsigned short const array[] = {\n")
			lines = f_i.read()
			print(lines)
			#for line in lines:
			#print("line:",line)
			line_str = str(binascii.b2a_hex(lines))[2:-1]
			print(line_str)
			lens = len(line_str)
			unit_count = UNIT_BYTE*2
			lens_for = int(lens/unit_count)
			for i in range(0,lens_for):
				unit_byte = line_str[:4]
				line_str = line_str[4:]
				print(unit_byte)
				f_o.write("0x"+unit_byte+",\n")

			f_o.write("};\n")
			print("len:",lens,unit_count,lens_for)<|endoftext|>


<commit_before>from django.core.management import base, get_commands, color
from django.utils import termcolors

def color_style():
    style = color.color_style()
    style.PROVIDER = termcolors.make_style(fg='green', opts=('bold',))
    style.COMMAND = termcolors.make_style(opts=('bold',))
    style.UNKNOWN = termcolors.make_style(fg='red')
    return style

def group_commands():
    grouped = {}
    for command, provider in get_commands().items():
        if isinstance(provider, basestring):
            key = provider
        elif isinstance(provider, base.BaseCommand):
            mod = provider.__module__
            idx = mod.find('management') - 1
            key = mod[:idx]
        else:
            key = '?unknown'
        grouped[key] = sorted(grouped.get(key, []) + [command])
    return grouped

def print_commands(grouped):
    style = color_style()
    for provider in sorted(grouped.keys()):
        if provider.startswith('?'):
            pstyle = style.UNKNOWN
        else:
            pstyle = style.PROVIDER
        print pstyle(provider) + " =>"
        for command in grouped[provider]:
            print "\t" + style.COMMAND(command)

def explain_commands():
    print_commands(group_commands())

class Command(base.NoArgsCommand):
    help = "Explain where each command coming from"

    def handle_noargs(self, **options):
        explain_commands()
<commit_msg>Show short help for each command.<commit_after>from django.core.management import base, get_commands, load_command_class, color
from django.utils import termcolors

def color_style():
    style = color.color_style()
    style.PROVIDER = termcolors.make_style(fg='green', opts=('bold',))
    style.COMMAND = termcolors.make_style(opts=('bold',))
    style.UNKNOWN = termcolors.make_style(fg='red')
    style.HELP = termcolors.make_style()
    return style

def group_commands():
    grouped = {}
    for command, provider in get_commands().items():
        if isinstance(provider, basestring):
            key = provider
            cls = load_command_class(provider, command)
        elif isinstance(provider, base.BaseCommand):
            cls = provider
            mod = provider.__module__
            idx = mod.find('management') - 1
            key = mod[:idx]
        else:
            key = '?unknown'
        value = (command, getattr(cls, 'help', ''))
        grouped[key] = sorted(grouped.get(key, []) + [value])
    return grouped

def print_commands(grouped):
    style = color_style()
    for provider in sorted(grouped.keys()):
        if provider.startswith('?'):
            pstyle = style.UNKNOWN
        else:
            pstyle = style.PROVIDER
        print pstyle(provider) + " =>"
        for command, helpstr in grouped[provider]:
            if len(helpstr) > 50:
                helpstr = helpstr[:47] + '...'
            if helpstr:
                helpstr = ' -- ' + helpstr

            print "\t" + \
                style.COMMAND(command) + \
                style.HELP(helpstr)

def explain_commands():
    print_commands(group_commands())

class Command(base.NoArgsCommand):
    help = "Explain where each command coming from"

    def handle_noargs(self, **options):
        explain_commands()
<|endoftext|>


<commit_before>import csv
import errno
import hashlib
import magic
import mimetypes
import os
from os.path import dirname, join, exists
import requests

from django.core.management.base import BaseCommand
from django.core.files.storage import FileSystemStorage

from candidates.static_data import MapItData
from official_documents.models import OfficialDocument

CSV_URL = 'https://docs.google.com/a/mysociety.org/spreadsheets/d/1jvWaQSENnASZfGne1IWRbDATMH2NT2xutyPEbZ5Is-8/export?format=csv&id=1jvWaQSENnASZfGne1IWRbDATMH2NT2xutyPEbZ5Is-8&gid=0'

allowed_mime_types = set([
    'application/pdf',
    'application/msword',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
])

def download_file_cached(url):
    url_hash = hashlib.md5(url).hexdigest()
    directory = join(dirname(__file__), '.noms-cache')
    try:
        os.mkdir(directory)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise
    filename = join(directory, url_hash)
    if exists(filename):
        return filename
    r = requests.get(url, verify=False)
    with open(filename, 'w') as f:
        f.write(r.content)
    return filename

class Command(BaseCommand):

    def handle(self, **options):

        mime_type_magic = magic.Magic(mime=True)
        storage = FileSystemStorage()

        r = requests.get(CSV_URL, stream=True)
        reader = csv.DictReader(r.raw)
        for row in reader:
            name = row['Constituency'].decode('utf-8')
            cons_data = MapItData.constituencies_2010_name_map[name]
            document_url = row['URL']
            if not document_url:
                print u"No URL for {0}".format(name)
                continue
            existing_documents = OfficialDocument.objects.filter(
                mapit_id=cons_data['id']
            )
            if existing_documents.count() > 0:
                print u"Skipping {0} since it already had documents".format(name)
                continue
            try:
                downloaded_filename = download_file_cached(document_url)
            except requests.exceptions.ConnectionError:
                print u"Connection failed for {0}".format(name)
                print u"The URL was:", document_url
                continue
            mime_type = mime_type_magic.from_file(downloaded_filename)
            extension = mimetypes.guess_extension(mime_type)
            if mime_type not in allowed_mime_types:
                print "Ignoring unknown MIME type {0} for {1}".format(
                    mime_type,
                    name,
                )
                continue
            filename = "official_documents/{mapit_id}/statement-of-persons-nominated{extension}".format(
                mapit_id=cons_data['id'],
                extension=extension,
            )
            with open(downloaded_filename, 'rb') as f:
                storage_filename = storage.save(filename, f)
            OfficialDocument.objects.create(
                document_type=OfficialDocument.NOMINATION_PAPER,
                uploaded_file=storage_filename,
                mapit_id=cons_data['id'],
                source_url=document_url
            )
<commit_msg>Add an indication that consituency's paper was imported fine<commit_after>import csv
import errno
import hashlib
import magic
import mimetypes
import os
from os.path import dirname, join, exists
import requests

from django.core.management.base import BaseCommand
from django.core.files.storage import FileSystemStorage

from candidates.static_data import MapItData
from official_documents.models import OfficialDocument

CSV_URL = 'https://docs.google.com/a/mysociety.org/spreadsheets/d/1jvWaQSENnASZfGne1IWRbDATMH2NT2xutyPEbZ5Is-8/export?format=csv&id=1jvWaQSENnASZfGne1IWRbDATMH2NT2xutyPEbZ5Is-8&gid=0'

allowed_mime_types = set([
    'application/pdf',
    'application/msword',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
])

def download_file_cached(url):
    url_hash = hashlib.md5(url).hexdigest()
    directory = join(dirname(__file__), '.noms-cache')
    try:
        os.mkdir(directory)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise
    filename = join(directory, url_hash)
    if exists(filename):
        return filename
    r = requests.get(url, verify=False)
    with open(filename, 'w') as f:
        f.write(r.content)
    return filename

class Command(BaseCommand):

    def handle(self, **options):

        mime_type_magic = magic.Magic(mime=True)
        storage = FileSystemStorage()

        r = requests.get(CSV_URL, stream=True)
        reader = csv.DictReader(r.raw)
        for row in reader:
            name = row['Constituency'].decode('utf-8')
            cons_data = MapItData.constituencies_2010_name_map[name]
            document_url = row['URL']
            if not document_url:
                print u"No URL for {0}".format(name)
                continue
            existing_documents = OfficialDocument.objects.filter(
                mapit_id=cons_data['id']
            )
            if existing_documents.count() > 0:
                print u"Skipping {0} since it already had documents".format(name)
                continue
            try:
                downloaded_filename = download_file_cached(document_url)
            except requests.exceptions.ConnectionError:
                print u"Connection failed for {0}".format(name)
                print u"The URL was:", document_url
                continue
            mime_type = mime_type_magic.from_file(downloaded_filename)
            extension = mimetypes.guess_extension(mime_type)
            if mime_type not in allowed_mime_types:
                print "Ignoring unknown MIME type {0} for {1}".format(
                    mime_type,
                    name,
                )
                continue
            filename = "official_documents/{mapit_id}/statement-of-persons-nominated{extension}".format(
                mapit_id=cons_data['id'],
                extension=extension,
            )
            with open(downloaded_filename, 'rb') as f:
                storage_filename = storage.save(filename, f)
            OfficialDocument.objects.create(
                document_type=OfficialDocument.NOMINATION_PAPER,
                uploaded_file=storage_filename,
                mapit_id=cons_data['id'],
                source_url=document_url
            )
            message = "Successfully added the Statement of Persons Nominated for {0}"
            print message.format(name)
<|endoftext|>


<commit_before># !/usr/bin/python
# -*- coding: cp1252 -*-
#
##################################################################################
#
#    Copyright 2016 Félix Brezo and Yaiza Rubio (i3visio, contacto@i3visio.com)
#
#    This program is part of OSRFramework. You can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##################################################################################

__author__ = "Felix Brezo, Yaiza Rubio  <contacto@i3visio.com>"
__version__ = "1.0"

import argparse
import json
import re
import sys
import urllib2

import osrframework.utils.browser as browser
from osrframework.utils.platforms import Platform

class OpenBugBounty(Platform):
    """
        A <Platform> object for OpenBugBounty.
    """
    def __init__(self):
        """
            Constructor...
        """
        self.platformName = "OpenBugBounty"
        self.tags = ["hacking"]

        ########################
        # Defining valid modes #
        ########################
        self.isValidMode = {}
        self.isValidMode["phonefy"] = False
        self.isValidMode["usufy"] = True
        self.isValidMode["searchfy"] = False

        ######################################
        # Search URL for the different modes #
        ######################################
        # Strings with the URL for each and every mode
        self.url = {}
        #self.url["phonefy"] = "http://anyurl.com//phone/" + "<phonefy>"
        self.url["usufy"] = "https://www.openbugbounty.org/researchers/" +  "<usufy>" + "/"
        #self.url["searchfy"] = "http://anyurl.com/search/" + "<searchfy>"

        ######################################
        # Whether the user needs credentials #
        ######################################
        self.needsCredentials = {}
        #self.needsCredentials["phonefy"] = False
        self.needsCredentials["usufy"] = False
        #self.needsCredentials["searchfy"] = False

        #################
        # Valid queries #
        #################
        # Strings that will imply that the query number is not appearing
        self.validQuery = {}
        # The regular expression '.+' will match any query
        #self.validQuery["phonefy"] = ".*"
        self.validQuery["usufy"] = ".+"
        #self.validQuery["searchfy"] = ".*"

        ###################
        # Not_found clues #
        ###################
        # Strings that will imply that the query number is not appearing
        self.notFoundText = {}
        #self.notFoundText["phonefy"] = []
        self.notFoundText["usufy"] = [ "
404 Error: Page Not found
" ]
        #self.notFoundText["searchfy"] = []

        #########################
        # Fields to be searched #
        #########################
        self.fieldsRegExp = {}

        # Definition of regular expressions to be searched in phonefy mode
        #self.fieldsRegExp["phonefy"] = {}
        # Example of fields:
        #self.fieldsRegExp["phonefy"]["i3visio.location"] = ""

        # Definition of regular expressions to be searched in usufy mode
        self.fieldsRegExp["usufy"] = {}
        # Example of fields:
        #self.fieldsRegExp["usufy"]["i3visio.location"] = ""
        # Definition of regular expressions to be searched in searchfy mode
        #self.fieldsRegExp["searchfy"] = {}
        # Example of fields:
        #self.fieldsRegExp["searchfy"]["i3visio.location"] = ""

        ################
        # Fields found #
        ################
        # This attribute will be feeded when running the program.
        self.foundFields = {}
<commit_msg>Fix OpenBugBounty wrapper<commit_after># !/usr/bin/python
# -*- coding: cp1252 -*-
#
##################################################################################
#
#    Copyright 2016 Félix Brezo and Yaiza Rubio (i3visio, contacto@i3visio.com)
#
#    This program is part of OSRFramework. You can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##################################################################################

__author__ = "Felix Brezo, Yaiza Rubio  <contacto@i3visio.com>"
__version__ = "1.0"

import argparse
import json
import re
import sys
import urllib2

import osrframework.utils.browser as browser
from osrframework.utils.platforms import Platform

class OpenBugBounty(Platform):
    """
        A <Platform> object for OpenBugBounty.
    """
    def __init__(self):
        """
            Constructor...
        """
        self.platformName = "OpenBugBounty"
        self.tags = ["hacking"]

        ########################
        # Defining valid modes #
        ########################
        self.isValidMode = {}
        self.isValidMode["phonefy"] = False
        self.isValidMode["usufy"] = True
        self.isValidMode["searchfy"] = False

        ######################################
        # Search URL for the different modes #
        ######################################
        # Strings with the URL for each and every mode
        self.url = {}
        #self.url["phonefy"] = "http://anyurl.com//phone/" + "<phonefy>"
        self.url["usufy"] = "https://www.openbugbounty.org/researchers/" +  "<usufy>" + "/"
        #self.url["searchfy"] = "http://anyurl.com/search/" + "<searchfy>"

        ######################################
        # Whether the user needs credentials #
        ######################################
        self.needsCredentials = {}
        #self.needsCredentials["phonefy"] = False
        self.needsCredentials["usufy"] = False
        #self.needsCredentials["searchfy"] = False

        #################
        # Valid queries #
        #################
        # Strings that will imply that the query number is not appearing
        self.validQuery = {}
        # The regular expression '.+' will match any query
        #self.validQuery["phonefy"] = ".*"
        self.validQuery["usufy"] = ".+"
        #self.validQuery["searchfy"] = ".*"

        ###################
        # Not_found clues #
        ###################
        # Strings that will imply that the query number is not appearing
        self.notFoundText = {}
        #self.notFoundText["phonefy"] = []
        self.notFoundText["usufy"] = [ "/images/404.jpg" ]
        #self.notFoundText["searchfy"] = []

        #########################
        # Fields to be searched #
        #########################
        self.fieldsRegExp = {}

        # Definition of regular expressions to be searched in phonefy mode
        #self.fieldsRegExp["phonefy"] = {}
        # Example of fields:
        #self.fieldsRegExp["phonefy"]["i3visio.location"] = ""

        # Definition of regular expressions to be searched in usufy mode
        self.fieldsRegExp["usufy"] = {}
        # Example of fields:
        #self.fieldsRegExp["usufy"]["i3visio.location"] = ""
        # Definition of regular expressions to be searched in searchfy mode
        #self.fieldsRegExp["searchfy"] = {}
        # Example of fields:
        #self.fieldsRegExp["searchfy"]["i3visio.location"] = ""

        ################
        # Fields found #
        ################
        # This attribute will be feeded when running the program.
        self.foundFields = {}
<|endoftext|>


<commit_before><commit_msg>Implement WINFF.FT.S.SIQ.20<commit_after><|endoftext|>


<commit_before># Copyright (c) 2019 Open Source Integrators
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, fields, models

class StockRequestOrder(models.Model):
    _inherit = "stock.request.order"

    direction = fields.Selection(
        [("outbound", "Outbound"), ("inbound", "Inbound")],
        string="Direction",
        states={"draft": [("readonly", False)]},
        readonly=True,
    )

    @api.onchange("direction")
    def _onchange_location_id(self):
        if self.direction == "outbound":
            # Stock Location set to Partner Locations/Customers
            self.location_id = self.company_id.partner_id.property_stock_customer.id
        else:
            # Otherwise the Stock Location of the Warehouse
            self.location_id = self.warehouse_id.lot_stock_id.id

    @api.onchange('warehouse_id')
    def _onchange_warehouse_id(self):
        if self.direction:
            self.direction = False
        for stock_request in self.stock_request_ids:
            if stock_request.route_id:
                stock_request.route_id = False

    def change_childs(self):
        super().change_childs()
        if not self._context.get("no_change_childs", False):
            for line in self.stock_request_ids:
                line.direction = self.direction
<commit_msg>[IMP] Add warehouse_id to existing onchange.<commit_after># Copyright (c) 2019 Open Source Integrators
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import api, fields, models

class StockRequestOrder(models.Model):
    _inherit = "stock.request.order"

    direction = fields.Selection(
        [("outbound", "Outbound"), ("inbound", "Inbound")],
        string="Direction",
        states={"draft": [("readonly", False)]},
        readonly=True,
    )

    @api.onchange("warehouse_id", "direction")
    def _onchange_location_id(self):
        if self.direction == "outbound":
            # Stock Location set to Partner Locations/Customers
            self.location_id = self.company_id.partner_id.property_stock_customer.id
        else:
            # Otherwise the Stock Location of the Warehouse
            self.location_id = self.warehouse_id.lot_stock_id.id
        for stock_request in self.stock_request_ids:
            if stock_request.route_id:
                stock_request.route_id = False

    def change_childs(self):
        super().change_childs()
        if not self._context.get("no_change_childs", False):
            for line in self.stock_request_ids:
                line.direction = self.direction
<|endoftext|>


<commit_before>from django.conf.urls import patterns, url
from django.views.generic import TemplateView

from studentvoice import views

urlpatterns = patterns('',
    url(r'^$', views.home, name='home'),
    url(r'^create/$', views.create, name='create'),
    url(r'^search/', views.search, name='search'),
    url(r'^(?P<voice_id>\d+)/$', views.show, name='show'),
    url(r'^/vote/$', views.vote, name='vote'),
    url(r'^(?P<voice_id>\d+)/report/$', views.report, name='report'),
    url(r'^(?P<voice_id>\d+)/create_comment/$', views.create_comment, name='create_comment'),
    url(r'^delete/(?P<voice_id>\d+)/$', views.delete, name='delete'),
    url(r'^(?P<voice_id>\d+)/edit/$', views.edit, name='edit'),
)
<commit_msg>Fix studentvoice urls.<commit_after>from django.conf.urls import patterns, url
from django.views.generic import TemplateView

from studentvoice import views

urlpatterns = patterns('',
    url(r'^$', views.home, name='home'),
    url(r'^create/$', views.create, name='create'),
    url(r'^search/', views.search, name='search'),
    url(r'^(?P<voice_id>\d+)/$', views.show, name='show'),
    url(r'^vote/$', views.vote, name='vote'),
    url(r'^(?P<voice_id>\d+)/report/$', views.report, name='report'),
    url(r'^(?P<voice_id>\d+)/create_comment/$', views.create_comment, name='create_comment'),
    url(r'^delete/(?P<voice_id>\d+)/$', views.delete, name='delete'),
    url(r'^(?P<voice_id>\d+)/edit/$', views.edit, name='edit'),
)
<|endoftext|>


<commit_before>from django.contrib import admin

# Register your models here.
from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Reminder
from studygroups.models import Profile
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation

class ApplicationInline(admin.TabularInline):
    model = Application

class StudyGroupAdmin(admin.ModelAdmin):
    inlines = [ApplicationInline]

    list_display = ['course', 'city', 'facilitator', 'start_date', 'day', 'signup_open']

class TeamMembershipInline(admin.TabularInline):
    model = TeamMembership
    raw_id_fields = ("user",)

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'page_slug', 'members')
    inlines = [TeamMembershipInline]

    def members(self, obj):
        return obj.teammembership_set.active().count()

class ApplicationAdmin(admin.ModelAdmin):
    list_display = ('name', 'study_group', 'email', 'mobile', 'created_at')
    search_fields = ['name', 'email', 'mobile']

def reminder_course_title(obj):
    return obj.study_group.course.title

class ReminderAdmin(admin.ModelAdmin):
    list_display = (reminder_course_title, 'email_subject', 'sent_at')

class StudyGroupInline(admin.TabularInline):
    model = StudyGroup
    fields = ('venue_name', 'city', 'start_date', 'day')
    readonly_fields = fields

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class CourseAdmin(admin.ModelAdmin):

    def get_queryset(self, request):
        qs = super(CourseAdmin, self).get_queryset(request)
        return qs.active()

    def created_by(course):
        def display_user(user):
            return '{} {}'.format(user.first_name, user.last_name)
        return display_user(course.created_by) if course.created_by else 'P2PU'

    def email(course):
        return course.created_by.email if course.created_by else '-'

    def learning_circles(course):
        return course.studygroup_set.active().count()

    def listed(course):
        return not course.unlisted
    listed.boolean = True

    list_display = ('id', 'title', 'provider', 'on_demand', 'topics', learning_circles, created_by, email, listed, 'license')
    exclude = ('deleted_at',)
    inlines = [StudyGroupInline]
    search_fields = ['title', 'provider', 'topics', 'created_by__email', 'license']

class ProfileAdmin(admin.ModelAdmin):
    def user(profile):
        return " ".join([profile.user.first_name, profile.user.last_name])
    list_display = [user, 'mailing_list_signup', 'communication_opt_in']
    search_fields = ['user__email']

admin.site.register(Course, CourseAdmin)
admin.site.register(StudyGroup, StudyGroupAdmin)
admin.site.register(Meeting)
admin.site.register(Application, ApplicationAdmin)
admin.site.register(Reminder, ReminderAdmin)
admin.site.register(Team, TeamAdmin)
admin.site.register(TeamInvitation)
admin.site.register(Profile, ProfileAdmin)
<commit_msg>Filter out deleted learning circles on django admin. Closes #562<commit_after>from django.contrib import admin

# Register your models here.
from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Reminder
from studygroups.models import Profile
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation

class ApplicationInline(admin.TabularInline):
    model = Application

class StudyGroupAdmin(admin.ModelAdmin):
    inlines = [ApplicationInline]

    list_display = ['course', 'city', 'facilitator', 'start_date', 'day', 'signup_open']

    def get_queryset(self, request):
        return super().get_queryset(request).active()

class TeamMembershipInline(admin.TabularInline):
    model = TeamMembership
    raw_id_fields = ("user",)

class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'page_slug', 'members')
    inlines = [TeamMembershipInline]

    def members(self, obj):
        return obj.teammembership_set.active().count()

class ApplicationAdmin(admin.ModelAdmin):
    list_display = ('name', 'study_group', 'email', 'mobile', 'created_at')
    search_fields = ['name', 'email', 'mobile']

def reminder_course_title(obj):
    return obj.study_group.course.title

class ReminderAdmin(admin.ModelAdmin):
    list_display = (reminder_course_title, 'email_subject', 'sent_at')

class StudyGroupInline(admin.TabularInline):
    model = StudyGroup
    fields = ('venue_name', 'city', 'start_date', 'day')
    readonly_fields = fields

    def has_add_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

class CourseAdmin(admin.ModelAdmin):

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.active()

    def created_by(course):
        def display_user(user):
            return '{} {}'.format(user.first_name, user.last_name)
        return display_user(course.created_by) if course.created_by else 'P2PU'

    def email(course):
        return course.created_by.email if course.created_by else '-'

    def learning_circles(course):
        return course.studygroup_set.active().count()

    def listed(course):
        return not course.unlisted
    listed.boolean = True

    list_display = ('id', 'title', 'provider', 'on_demand', 'topics', learning_circles, created_by, email, listed, 'license')
    exclude = ('deleted_at',)
    inlines = [StudyGroupInline]
    search_fields = ['title', 'provider', 'topics', 'created_by__email', 'license']

class ProfileAdmin(admin.ModelAdmin):
    def user(profile):
        return " ".join([profile.user.first_name, profile.user.last_name])
    list_display = [user, 'mailing_list_signup', 'communication_opt_in']
    search_fields = ['user__email']

admin.site.register(Course, CourseAdmin)
admin.site.register(StudyGroup, StudyGroupAdmin)
admin.site.register(Meeting)
admin.site.register(Application, ApplicationAdmin)
admin.site.register(Reminder, ReminderAdmin)
admin.site.register(Team, TeamAdmin)
admin.site.register(TeamInvitation)
admin.site.register(Profile, ProfileAdmin)
<|endoftext|>


<commit_before>'machine leaning'
import abc
import copy
import numpy as np
import pdb
import unittest

class Model(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def predict(self, w, x):
        'return np.array'
        pass

    @abc.abstractmethod
    def gradient_prediction_wrt_features(self, features, prediction, weights):
        'return np.array'
        pass

    @abc.abstractmethod
    def gradient_prediction_wrt_weights(self, features, prediction, weights):
        'return np.array'
        pass

    @abc.abstractmethod
    def n_weights(self):
        'return length of w vector: Number'
        pass

class Criterion(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def loss(self, prediction, target):
        'return Number'
        pass

    @abc.abstractmethod
    def derivative_loss_wrt_prediction(self, loss, prediction, target):
        'return np.array'
        pass

    @abc.abstractmethod
    def derivative_loss_wrt_target(self, loss, prediction, target):
        'return np.array'
        pass

class Linear(Model):
    'y = bias + coef * x'
    def __init__(self, n_inputs):
        self.n_inputs = n_inputs

    def predict(self, features, weights):
        # bias = w[0]
        # coef = w[1:]
        assert len(features) == self.n_inputs
        assert len(weights) == self.n_inputs + 1

        result_scalar = weights[0] + np.dot(features, weights[1:])
        result = np.array((result_scalar,))
        return result

    def gradient_prediction_wrt_weights(self, features, prediction, weights):
        assert len(features) == self.n_inputs
        assert len(prediction) == 1
        assert len(weights) == self.n_inputs + 1

        result = np.empty(self.n_inputs + 1)
        result[0] = 1
        result[1:] = copy.copy(features)
        return result

    def gradient_prediction_wrt_features(self, features, prediction, weights):
        assert len(prediction) == 1
        assert len(features) == self.n_inputs
        assert len(weights) == self.n_inputs + 1

        result = copy.copy(weights[1:])
        return result

    def n_weights(self):
        return self.n_inputs + 1

class UnittestLinear(unittest.TestCase):
    def setUp(self):
        self.w = np.array((-7, 3, 6))

    def test_construction_nweights(self):
        tests = (
            0,
            1,
            2,
        )
        for test in tests:
            model = Linear(test)
            self.assertTrue(isinstance(model, Linear))
            self.assertEqual(test + 1, model.n_weights())

    def test_predict(self):
        weights = np.array((1, 2, 3))
        tests = (
            (0, np.empty((0)), 1),
            (1, np.array((1,)), 3),
            (2, np.array((1, 2)), 9),
        )
        for test in tests:
            n_inputs, features, prediction_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            self.assertEqual(prediction_expected, prediction_actual)
            self.assertTrue(isinstance(prediction_actual, np.ndarray))

    def test_gradient_prediction_wrt_weights(self):
        weights = np.array((10, 20, 30))
        tests = (
            (0, np.empty((0)), np.array((1))),
            (1, np.array((1,)), np.array((1, 1))),
            (2, np.array((1, 2)), np.array((1, 1, 2))),
        )
        for test in tests:
            n_inputs, features, gradient_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            gradient_actual = model.gradient_prediction_wrt_weights(features, prediction_actual, weights_test)
            self.assertTrue((gradient_expected == gradient_actual).all())

    def test_gradient_prediction_wrt_features(self):
        weights = np.array((1, 2, 3))
        tests = (
            # (0, np.empty((0)), np.empty((0))),
            (1, np.array((1,)), np.array((2,))),
            (2, np.array((1, 2)), np.array((2, 3))),
        )
        for test in tests:
            n_inputs, features, gradient_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            gradient_actual = model.gradient_prediction_wrt_features(features, prediction_actual, weights_test)
            self.assertTrue((gradient_expected == gradient_actual).all())

class SquaredLoss(Criterion):
    def __init__(self):
        pass

    def loss(self, prediction, target):
        difference = prediction - target
        result = difference * difference
        return result

    def derivative_loss_wrt_prediction(self, loss, prediction, target):
        return 2.0 * (prediction - target)

    def derivative_loss_wrt_target(self, loss, prediction, target):
        return - 2.0 * (prediction - target)

class TestSquaredLoss(unittest.TestCase):
    def test_construction(self):
        criterion = SquaredLoss()
        self.assertTrue(isinstance(criterion, SquaredLoss))

    def test_loss(self):
        tests = (
            (10, 12, 4),
            (12, 10, 4),
            (10, 10, 0),
        )
        for test in tests:
            prediction, target, loss_expected = test
            criterion = SquaredLoss()
            loss_actual = criterion.loss(prediction, target)
            self.assertEqual(loss_expected, loss_actual)

    def test_derivative_loss_wrt_prediction(self):
        tests = (
            (4, 10, 12, -4),
            (9, 10, 13, -6),
        )
        for test in tests:
            loss, prediction, target, derivative_expected = test
            criterion = SquaredLoss()
            derivative_actual = criterion.derivative_loss_wrt_prediction(loss, prediction, target)
            self.assertEqual(derivative_expected, derivative_actual)

    def test_derivative_loss_wrt_target(self):
        tests = (
            (4, 10, 12, 4),
            (9, 10, 13, 6),
        )
        for test in tests:
            loss, prediction, target, derivative_expected = test
            criterion = SquaredLoss()
            derivative_actual = criterion.derivative_loss_wrt_target(loss, prediction, target)
            self.assertEqual(derivative_expected, derivative_actual)

if __name__ == '__main__':
    unittest.main()
    if False:
        pdb
<commit_msg>add ModelCriterion class<commit_after>'machine leaning'
import abc
import copy
import numpy as np
import pdb
import unittest

class Model(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def predict(self, w, x):
        'return np.array'
        pass

    @abc.abstractmethod
    def gradient_prediction_wrt_features(self, features, prediction, weights):
        'return np.array'
        pass

    @abc.abstractmethod
    def gradient_prediction_wrt_weights(self, features, prediction, weights):
        'return np.array'
        pass

    @abc.abstractmethod
    def n_weights(self):
        'return length of w vector: Number'
        pass

class Criterion(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def loss(self, prediction, target):
        'return Number'
        pass

    @abc.abstractmethod
    def derivative_loss_wrt_prediction(self, loss, prediction, target):
        'return np.array'
        pass

    @abc.abstractmethod
    def derivative_loss_wrt_target(self, loss, prediction, target):
        'return np.array'
        pass

class ModelCriterion(object):
    def __init__(self, model=None, criterion=None):
        assert isinstance(model, machine_learning.Model)
        assert isinstance(criterion, machine_learning.Criterion)
        self.model = model
        self.criterion = criterion

    def loss_prediction(self, features, target, weights):
        prediction = self.model.predict(features, weights)
        loss = self.criterion.loss(prediction, target)
        return loss, predictiont

    def gradient_wrt_weights(self, features, loss, prediction, target, weights):
        'using the chain rule'
        gradient_prediction_wrt_weights = self.model.gradient_prediction_wrt_weights(features, prediction, weights)
        derivate_loss_wrt_prediction = self.criterion.derivative_loss_wrt_prediction(loss, prediction, target)
        result = gradient_prediction_wrt_weights * derivate_loss_wrt_prediction
        return result

class Linear(Model):
    'y = bias + coef * x'
    def __init__(self, n_inputs):
        self.n_inputs = n_inputs

    def predict(self, features, weights):
        # bias = w[0]
        # coef = w[1:]
        assert len(features) == self.n_inputs
        assert len(weights) == self.n_inputs + 1

        result_scalar = weights[0] + np.dot(features, weights[1:])
        result = np.array((result_scalar,))
        return result

    def gradient_prediction_wrt_weights(self, features, prediction, weights):
        assert len(features) == self.n_inputs
        assert len(prediction) == 1
        assert len(weights) == self.n_inputs + 1

        result = np.empty(self.n_inputs + 1)
        result[0] = 1
        result[1:] = copy.copy(features)
        return result

    def gradient_prediction_wrt_features(self, features, prediction, weights):
        assert len(prediction) == 1
        assert len(features) == self.n_inputs
        assert len(weights) == self.n_inputs + 1

        result = copy.copy(weights[1:])
        return result

    def n_weights(self):
        return self.n_inputs + 1

class UnittestLinear(unittest.TestCase):
    def setUp(self):
        self.w = np.array((-7, 3, 6))

    def test_construction_nweights(self):
        tests = (
            0,
            1,
            2,
        )
        for test in tests:
            model = Linear(test)
            self.assertTrue(isinstance(model, Linear))
            self.assertEqual(test + 1, model.n_weights())

    def test_predict(self):
        weights = np.array((1, 2, 3))
        tests = (
            (0, np.empty((0)), 1),
            (1, np.array((1,)), 3),
            (2, np.array((1, 2)), 9),
        )
        for test in tests:
            n_inputs, features, prediction_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            self.assertEqual(prediction_expected, prediction_actual)
            self.assertTrue(isinstance(prediction_actual, np.ndarray))

    def test_gradient_prediction_wrt_weights(self):
        weights = np.array((10, 20, 30))
        tests = (
            (0, np.empty((0)), np.array((1))),
            (1, np.array((1,)), np.array((1, 1))),
            (2, np.array((1, 2)), np.array((1, 1, 2))),
        )
        for test in tests:
            n_inputs, features, gradient_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            gradient_actual = model.gradient_prediction_wrt_weights(features, prediction_actual, weights_test)
            self.assertTrue((gradient_expected == gradient_actual).all())

    def test_gradient_prediction_wrt_features(self):
        weights = np.array((1, 2, 3))
        tests = (
            # (0, np.empty((0)), np.empty((0))),
            (1, np.array((1,)), np.array((2,))),
            (2, np.array((1, 2)), np.array((2, 3))),
        )
        for test in tests:
            n_inputs, features, gradient_expected = test
            model = Linear(n_inputs)
            weights_test = weights[:model.n_weights()]
            prediction_actual = model.predict(features, weights_test)
            gradient_actual = model.gradient_prediction_wrt_features(features, prediction_actual, weights_test)
            self.assertTrue((gradient_expected == gradient_actual).all())

class SquaredLoss(Criterion):
    def __init__(self):
        pass

    def loss(self, prediction, target):
        difference = prediction - target
        result = difference * difference
        return result

    def derivative_loss_wrt_prediction(self, loss, prediction, target):
        return 2.0 * (prediction - target)

    def derivative_loss_wrt_target(self, loss, prediction, target):
        return - 2.0 * (prediction - target)

class TestSquaredLoss(unittest.TestCase):
    def test_construction(self):
        criterion = SquaredLoss()
        self.assertTrue(isinstance(criterion, SquaredLoss))

    def test_loss(self):
        tests = (
            (10, 12, 4),
            (12, 10, 4),
            (10, 10, 0),
        )
        for test in tests:
            prediction, target, loss_expected = test
            criterion = SquaredLoss()
            loss_actual = criterion.loss(prediction, target)
            self.assertEqual(loss_expected, loss_actual)

    def test_derivative_loss_wrt_prediction(self):
        tests = (
            (4, 10, 12, -4),
            (9, 10, 13, -6),
        )
        for test in tests:
            loss, prediction, target, derivative_expected = test
            criterion = SquaredLoss()
            derivative_actual = criterion.derivative_loss_wrt_prediction(loss, prediction, target)
            self.assertEqual(derivative_expected, derivative_actual)

    def test_derivative_loss_wrt_target(self):
        tests = (
            (4, 10, 12, 4),
            (9, 10, 13, 6),
        )
        for test in tests:
            loss, prediction, target, derivative_expected = test
            criterion = SquaredLoss()
            derivative_actual = criterion.derivative_loss_wrt_target(loss, prediction, target)
            self.assertEqual(derivative_expected, derivative_actual)

if __name__ == '__main__':
    unittest.main()
    if False:
        pdb
<|endoftext|>


<commit_before># Author: Gil Cukierman
# gil.cukierman (at) gmail.com
# Creation Date: 5/4/15
# (c) All Rights Reserved

from PIL import Image
from sets import Set
import numpy as np

class Mask(object):
	"""
	A mask object is just a Pillow Image object wrapped up
	in our own concept of a mask. It's a way to logically
	separate the Mask images from the Marker images
	"""
	def __init__(self,img,name,threshold):
		self._img=img
		# Close this immediately... We don't need it anymore
		# and having two copies of it KILLS runtime
		img.close()

		self.name = name
		self.threshold = threshold

		# a mask is responsible for keeping track of its
		# own threshold set of pixels and its
		# "Not set of pixels"

		# all our pixels
		all_pixels = np.array(self._img)

		self.positive_set = Set()
		self.negative_set = Set()

		# iterate through our pixel map and compare to threshold
		for index, value in np.ndenumerate(all_pixels):
			if (value >= self.threshold):
				self.positive_set.add(index)
			else:
				self.negative_set.add(index)

	# Delegate all of Mask's inner functions to Image
	# e.g. im.__getattr__('size') == im.size if there was no wrapper
	def __getattr__(self,key):
		if key == '_img':
			raise AttributeError()
		return getattr(self._img,key)

class Marker(object):
	"""
	A marker object is just a Pillow Image object wrapped up
	in our own concept of a marker. It's a way to logically
	separate the Marker images from the Mask images
	"""
	def __init__(self,img,name,threshold):
		self._img=img
		# Close this immediately. DRY
		img.close()

		self.name = name
		self.threshold = threshold

	# Delegate all of Marker's inner functions to Image
	# e.g. im.__getattr__('size') == im.size if there was no wrapper
	def __getattr__(self,key):
		if key == '_img':
			raise AttributeError()
		return getattr(self._img,key)

class BatchImage():
	"""
	A BatchImage object aggregates a set of Mask
	and Marker objects into one clearly defined
	batch of images. It defines all the operations
	that should be performed on masks and markers.
	"""

	def __init__(self, mask_list, mark_list, num_pics, mask_opts,mark_opts):

		# as we iterate here - raise exception if these aren't masks
		# and markers respectively
		self.masks = []
		for mask in mask_list:
			if type(mask) is not Mask:
				raise ValueError("all values is mask_list parameter must be masks. " + type(mask) + " passed as part of list.")
			else:
				self.masks.append(mask)
		self.markers = []

		# and let's do the same with our markers
		for marker in mark_list:
			if type(marker) is not Marker:
				raise ValueError("all values is mark_list parameter must be markers. " + type(marker) + " passed as part of list.")
			else:
				self.markers.append(marker)

		# Let's just make sure everything is here and consistent
		# before we assign each value
		if num_pics != (len(masks) + len(markers)):
			raise ValueError("num_pics does not match the cumulative number of masks and markers passed into BatchImage instance.")
		else:
			self.num_pics = num_pics

		# our operations for this batch...
		self.mask_opts = mask_opts
		self.mark_opts = mark_opts

	def PerformOps():
		"""
		Uses the mask_opts and mark_opts to decide which
		operations to perform. Performs them, and returns a
		dictionary of key,value pairs giving with the results.
		Key = Column Heading, Value = Data
		"""

		# This function really just parses the operation lists and chooses which functions to delegate to. It then returns those values.
2<commit_msg>implemented logic for overlaying mask on marker - generic function<commit_after># Author: Gil Cukierman
# gil.cukierman (at) gmail.com
# Creation Date: 5/4/15
# (c) All Rights Reserved

from PIL import Image
from sets import Set
import numpy as np

class Mask(object):
	"""
	A mask object is just a Pillow Image object wrapped up
	in our own concept of a mask. It's a way to logically
	separate the Mask images from the Marker images
	"""
	def __init__(self,img,name,threshold):
		self._img=img
		# Close this immediately... We don't need it anymore
		# and having two copies of it KILLS runtime
		img.close()

		self.name = name
		self.threshold = threshold

		# a mask is responsible for keeping track of its
		# own threshold set of pixels and its
		# "Not set of pixels"

		# all our pixels
		self.all_pixels = np.array(self._img)

		self.positive_set = Set()
		self.negative_set = Set()

		# iterate through our pixel map and compare to threshold
		for index, value in np.ndenumerate(self.all_pixels):
			if (value >= self.threshold):
				self.positive_set.add(index)
			else:
				self.negative_set.add(index)

	# Delegate all of Mask's inner functions to Image
	# e.g. im.__getattr__('size') == im.size if there was no wrapper
	def __getattr__(self,key):
		if key == '_img':
			raise AttributeError()
		return getattr(self._img,key)

class Marker(object):
	"""
	A marker object is just a Pillow Image object wrapped up
	in our own concept of a marker. It's a way to logically
	separate the Marker images from the Mask images
	"""
	def __init__(self,img,name,threshold):
		self._img=img
		# Close this immediately. DRY
		img.close()

		self.name = name
		self.threshold = threshold

		self.all_pixels = np.array(_img)

	# Delegate all of Marker's inner functions to Image
	# e.g. im.__getattr__('size') == im.size if there was no wrapper
	def __getattr__(self,key):
		if key == '_img':
			raise AttributeError()
		return getattr(self._img,key)

class BatchImage():
	"""
	A BatchImage object aggregates a set of Mask
	and Marker objects into one clearly defined
	batch of images. It defines all the operations
	that should be performed on masks and markers.
	"""

	def __init__(self, mask_list, mark_list, num_pics, mask_opts,mark_opts):

		# as we iterate here - raise exception if these aren't masks
		# and markers respectively
		self.masks = []
		for mask in mask_list:
			if type(mask) is not Mask:
				raise ValueError("all values is mask_list parameter must be masks. " + type(mask) + " passed as part of list.")
			else:
				self.masks.append(mask)
		self.markers = []

		# and let's do the same with our markers
		for marker in mark_list:
			if type(marker) is not Marker:
				raise ValueError("all values is mark_list parameter must be markers. " + type(marker) + " passed as part of list.")
			else:
				self.markers.append(marker)

		# Let's just make sure everything is here and consistent
		# before we assign each value
		if num_pics != (len(masks) + len(markers)):
			raise ValueError("num_pics does not match the cumulative number of masks and markers passed into BatchImage instance.")
		else:
			self.num_pics = num_pics

		# our operations for this batch...
		self.mask_opts = mask_opts
		self.mark_opts = mark_opts

	def PerformOps():
		"""
		Uses the mask_opts and mark_opts to decide which
		operations to perform. Performs them, and returns a
		dictionary of key,value pairs giving with the results.
		Key = Column Heading, Value = Data
		"""

		# This function really just parses the operation lists and chooses which functions to delegate to. It then returns those values.

	def GetOverlayLocations(maskset,markerobj):
		"""
		Takes a set of pixel locations and a marker object.
		Returns a set of of pixel values for which the marker
		is under the given maskset.
		"""

		mask = maskset
		marker = Set()

		# make a Set of pixel locations for the marker
		for index, value in np.ndenumerate(markerobj.all_pixels):
			marker.add(index)

		# set intersection between marker and mask gives us the locations we need
		overlay = mask.intersection(marker)

		return overlay<|endoftext|>