<commit_before>from latexify import latexify, figure, fig_size, savefig
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import pymc3 as pm
import seaborn.apionly as sns

from analysis import (
    analyze_data,
    analyze_differences,
    load_best_result,
    load_surveys,
    plot_detailed,
    plot_distribution,
    plot_overview,
    ExperimentType,
)

# Colours
default_cycler = plt.rcParamsDefault["axes.prop_cycle"]
colorblind_cmaps = ["Dark2", "Set2"]
cmap_main, cmap_complement = colorblind_cmaps
# cmap_main, cmap_complement = cmap_complement, cmap_main
colorblind_cyclers = {cmap: plt.cycler("color", plt.cm.get_cmap(cmap).colors)
                      for cmap in colorblind_cmaps}
plt.rcParams["axes.prop_cycle"] = colorblind_cyclers[cmap_main]

def do_paths():
    with figure("paths_overview"):
        ax1 = plt.subplot("121")
        plot_overview(results, ExperimentType.Onboard, color="C0")
        ax2 = plt.subplot("122", sharex=ax1, sharey=ax1)
        plot_overview(results, ExperimentType.Spirit, color="C1", ylabel="")
        plt.setp(ax2.get_yticklabels(), visible=False)

    with figure("paths_detailed"):
        ax1 = plt.subplot("121")
        plot_detailed(results, ExperimentType.Onboard, color="C0",
                      crosshair=True)
        ax2 = plt.subplot("122", sharex=ax1, sharey=ax1)
        plot_detailed(results, ExperimentType.Spirit, color="C1",
                      crosshair=True, ylabel="")
        plt.setp(ax2.get_yticklabels(), visible=False)

def do_distributions():
    with figure("distribution_onboard"):
        plot_distribution(results, ExperimentType.Onboard, color="C0",
                          crosshair=True)

    with figure("distribution_spirit"):
        plot_distribution(results, ExperimentType.Spirit, color="C1",
                          crosshair=True)

def do_durations():
    with figure("duration"):
        sns.boxplot(x="experiment", y="duration", data=analyses)
        sns.swarmplot(x="experiment", y="duration", split=True, data=analyses,
                      palette=cmap_complement)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("duration (s)")

    with figure("duration_runs"):
        sns.factorplot(x="order", y="duration", hue="experiment", data=analyses,
                       capsize=0.2)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("duration (s)")
        plt.xlabel("run")

def do_errors():
    with figure("rms"):
        g = sns.factorplot(x="experiment", y="value", col="variable",
                           data=pd.melt(analyses, id_vars=["user", "experiment",
                                                           "order", "group"],
                                        value_vars=["rms", "rms_x", "rms_y"]),
                                        kind="box")
        g.fig.axes[0].set_title("RMS Error")
        g.fig.axes[1].set_title("RMS Error in $x$")
        g.fig.axes[2].set_title("RMS Error in $y$")
        g.fig.axes[0].set_ylabel("error (m)")

    with figure("rms_runs"):
        g = sns.factorplot(x="order", y="value", hue="experiment",
                           col="variable",
                           data=pd.melt(analyses, id_vars=["user","experiment",
                                                           "order", "group"],
                                        value_vars=["rms", "rms_x", "rms_y"]),
                                        capsize=0.2)
        g.fig.axes[0].set_title("RMS Error")
        g.fig.axes[1].set_title("RMS Error in $x$")
        g.fig.axes[2].set_title("RMS Error in $y$")
        g.fig.axes[0].set_ylabel("error (m)")
        g.fig.axes[0].set_xlabel("run")
        g.fig.axes[1].set_xlabel("run")
        g.fig.axes[2].set_xlabel("run")

    with figure("distance"):
        g = sns.factorplot(x="experiment", y="value", col="variable",
                           data=pd.melt(analyses, id_vars=["user", "experiment",
                                                           "order", "group"],
                                        value_vars=[r"dist_err", r"x_err",
                                                    r"y_err"]), kind="box")
        g.fig.axes[0].set_title("Distance from target")
        g.fig.axes[1].set_title("Distance from target in $x$")
        g.fig.axes[2].set_title("Distance from target in $y$")
        g.fig.axes[0].set_ylabel("distance (m)")
        g.axes[0][0].axhline(0, color="black", linewidth=1, zorder=-1)
        g.axes[0][1].axhline(0, color="black", linewidth=1, zorder=-1)
        g.axes[0][2].axhline(0, color="black", linewidth=1, zorder=-1)

def do_surveys():
    with figure("tlx_results"):
        sns.boxplot(x="experiment", y="tlx", data=tlx)
        sns.swarmplot(x="experiment", y=r"tlx",
                      data=tlx, palette=cmap_complement, split=True)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("NASA-TLX weighted score")

    with figure("tlx_components"):
        molten = pd.melt(tlx, id_vars=["user", "experiment", "order"],
                         value_vars=["mental", "physical", "temporal",
                                     "performance", "effort", "frustration"],
                         var_name="component", value_name="score")
        g = sns.barplot(x=r"component", y="score", hue="experiment",
                        data=molten)
        sns.swarmplot(x="component", y=r"score", data=molten, hue="experiment",
                      split=True, palette=cmap_complement)
        handles, labels = g.get_legend_handles_labels()
        plt.legend(handles[2:], labels[2:])
        plt.ylabel("NASA-TLX component score")

    with figure("survey_overview"):
        molten = pd.melt(surveys, id_vars=["user", "experiment", "order"],
                         value_vars=[r"orientation_understanding",
                                     r"orientation_control",
                                     r"position_understanding",
                                     r"position_control",
                                     r"spacial_understanding",
                                     r"spacial_control"],
                         var_name="question", value_name="rating")
        g = sns.barplot(x=r"rating", y=r"question", hue="experiment",
                        data=molten)
        plt.gca().set_yticklabels(
                ["orientation understanding", "orientation control",
                 "absolute position understanding", "absolute positional control",
                 "relative position understanding", "relative position control"])

        sns.swarmplot(x="rating", y=r"question", data=molten, hue="experiment",
                      split=True, palette=cmap_complement)
        handles, labels = g.get_legend_handles_labels()
        plt.legend(handles[2:], labels[2:])
        plt.xlabel("rating")
        plt.title("Survey results")

def do_differences(recalculate=False):
    trace_cols = ["duration", "dist_err", "x_err", "y_err", "rms_x", "rms_y"]
    if recalculate:
        traces = analyze_differences(analyses, trace_cols,
                                     [[(5, 50), 50]] + [[(0, 2), 0.5]]*5)
    else:
        traces = {col: load_best_result(col) for col in trace_cols}
    for col, best_result in traces.items():
        trace = best_result.trace
        with figure(f"mean_std_{col}"):
            ax = pm.plot_posterior(trace[100:],
                                   varnames=[r"group1_mean", r"group2_mean",
                                             r"group1_std", "group2_std"],
                                   kde_plot=True, color="C0")
            for a in (1, 3):
                ax[a].lines[0].set_color("C1")

        with figure(f"difference_{col}"):
            pm.plot_posterior(trace[1000:],
                              varnames=["difference of means", "effect size"],
                              ref_val=0, kde_plot=True, color="C2")

if __name__ == "__main__":
    latexify()

    results, analyses = analyze_data()
    print("Loaded files")

    # do_paths()
    # do_distributions()
    # do_durations()
    # do_errors()

    # users, tlx, surveys = load_surveys()
    # print("Loaded surveys")

    # do_surveys()

    # WARNING: Takes a long time with recalculate.
    # do_differences()

<commit_msg>Force size constraints on figures.<commit_after>from latexify import latexify, figure, fig_size, savefig
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import pymc3 as pm
import seaborn.apionly as sns

from analysis import (
    analyze_data,
    analyze_differences,
    load_best_result,
    load_surveys,
    plot_detailed,
    plot_distribution,
    plot_overview,
    ExperimentType,
)

# Colours
default_cycler = plt.rcParamsDefault["axes.prop_cycle"]
colorblind_cmaps = ["Dark2", "Set2"]
cmap_main, cmap_complement = colorblind_cmaps
# cmap_main, cmap_complement = cmap_complement, cmap_main
colorblind_cyclers = {cmap: plt.cycler("color", plt.cm.get_cmap(cmap).colors)
                      for cmap in colorblind_cmaps}
plt.rcParams["axes.prop_cycle"] = colorblind_cyclers[cmap_main]

def do_paths():
    with figure("paths_overview", figsize=fig_size(0.44, 1.2)):
        ax1 = plt.subplot("121")
        plot_overview(results, ExperimentType.Onboard, color="C0")
        ax2 = plt.subplot("122", sharex=ax1, sharey=ax1)
        plot_overview(results, ExperimentType.Spirit, color="C1", ylabel="")
        plt.setp(ax2.get_yticklabels(), visible=False)

    with figure("paths_detailed", figsize=fig_size(0.44, 1.2)):
        ax1 = plt.subplot("121")
        plot_detailed(results, ExperimentType.Onboard, color="C0",
                      crosshair=True)
        ax2 = plt.subplot("122", sharex=ax1, sharey=ax1)
        plot_detailed(results, ExperimentType.Spirit, color="C1",
                      crosshair=True, ylabel="")
        plt.setp(ax2.get_yticklabels(), visible=False)

def do_distributions():
    with figure("distribution_onboard", figsize=fig_size(0.44, 1)):
        plot_distribution(results, ExperimentType.Onboard, color="C0",
                          crosshair=True)

    with figure("distribution_spirit", figsize=fig_size(0.44, 1)):
        plot_distribution(results, ExperimentType.Spirit, color="C1",
                          crosshair=True)

def do_durations():
    with figure("duration", figsize=fig_size(0.44, 1)):
        sns.boxplot(x="experiment", y="duration", data=analyses)
        sns.swarmplot(x="experiment", y="duration", split=True, data=analyses,
                      palette=cmap_complement)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("duration (s)")

    with figure("duration_runs", figsize=fig_size(0.44, 1)):
        sns.factorplot(x="order", y="duration", hue="experiment", data=analyses,
                       capsize=0.2)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("duration (s)")
        plt.xlabel("run")

def do_errors():
    with figure("rms", figsize=fig_size(0.9, 0.4)):
        g = sns.factorplot(x="experiment", y="value", col="variable",
                           data=pd.melt(analyses, id_vars=["user", "experiment",
                                                           "order", "group"],
                                        value_vars=["rms", "rms_x", "rms_y"]),
                                        kind="box")
        g.fig.axes[0].set_title("RMS Error")
        g.fig.axes[1].set_title("RMS Error in $x$")
        g.fig.axes[2].set_title("RMS Error in $y$")
        g.fig.axes[0].set_ylabel("error (m)")

    with figure("rms_runs", figsize=fig_size(0.9, 0.4)):
        g = sns.factorplot(x="order", y="value", hue="experiment",
                           col="variable",
                           data=pd.melt(analyses, id_vars=["user","experiment",
                                                           "order", "group"],
                                        value_vars=["rms", "rms_x", "rms_y"]),
                                        capsize=0.2)
        g.fig.axes[0].set_title("RMS Error")
        g.fig.axes[1].set_title("RMS Error in $x$")
        g.fig.axes[2].set_title("RMS Error in $y$")
        g.fig.axes[0].set_ylabel("error (m)")
        g.fig.axes[0].set_xlabel("run")
        g.fig.axes[1].set_xlabel("run")
        g.fig.axes[2].set_xlabel("run")

    with figure("distance", figsize=fig_size(0.9, 0.4)):
        g = sns.factorplot(x="experiment", y="value", col="variable",
                           data=pd.melt(analyses, id_vars=["user", "experiment",
                                                           "order", "group"],
                                        value_vars=[r"dist_err", r"x_err",
                                                    r"y_err"]), kind="box")
        g.fig.axes[0].set_title("Distance from target")
        g.fig.axes[1].set_title("Distance from target in $x$")
        g.fig.axes[2].set_title("Distance from target in $y$")
        g.fig.axes[0].set_ylabel("distance (m)")
        g.axes[0][0].axhline(0, color="black", linewidth=1, zorder=-1)
        g.axes[0][1].axhline(0, color="black", linewidth=1, zorder=-1)
        g.axes[0][2].axhline(0, color="black", linewidth=1, zorder=-1)

def do_surveys():
    with figure("tlx_results", figsize=fig_size(0.44, 1)):
        sns.boxplot(x="experiment", y="tlx", data=tlx)
        sns.swarmplot(x="experiment", y=r"tlx",
                      data=tlx, palette=cmap_complement, split=True)
        plt.ylim(0, plt.ylim()[1])
        plt.ylabel("NASA-TLX weighted score")

    with figure("tlx_components", figsize=fig_size(0.44, 1)):
        molten = pd.melt(tlx, id_vars=["user", "experiment", "order"],
                         value_vars=["mental", "physical", "temporal",
                                     "performance", "effort", "frustration"],
                         var_name="component", value_name="score")
        g = sns.barplot(x=r"component", y="score", hue="experiment",
                        data=molten)
        sns.swarmplot(x="component", y=r"score", data=molten, hue="experiment",
                      split=True, palette=cmap_complement)
        handles, labels = g.get_legend_handles_labels()
        plt.legend(handles[2:], labels[2:])
        plt.ylabel("NASA-TLX component score")

    with figure("survey_overview", figsize=fig_size(0.9, 0.5)):
        molten = pd.melt(surveys, id_vars=["user", "experiment", "order"],
                         value_vars=[r"orientation_understanding",
                                     r"orientation_control",
                                     r"position_understanding",
                                     r"position_control",
                                     r"spacial_understanding",
                                     r"spacial_control"],
                         var_name="question", value_name="rating")
        g = sns.barplot(x=r"rating", y=r"question", hue="experiment",
                        data=molten)
        plt.gca().set_yticklabels(
                ["orientation understanding", "orientation control",
                 "absolute position understanding", "absolute positional control",
                 "relative position understanding", "relative position control"])

        sns.swarmplot(x="rating", y=r"question", data=molten, hue="experiment",
                      split=True, palette=cmap_complement)
        handles, labels = g.get_legend_handles_labels()
        plt.legend(handles[2:], labels[2:])
        plt.xlabel("rating")
        plt.title("Survey results")

def do_differences(recalculate=False):
    trace_cols = ["duration", "dist_err", "x_err", "y_err", "rms_x", "rms_y"]
    if recalculate:
        traces = analyze_differences(analyses, trace_cols,
                                     [[(5, 50), 50]] + [[(0, 2), 0.5]]*5)
    else:
        traces = {col: load_best_result(col) for col in trace_cols}
    for col, best_result in traces.items():
        trace = best_result.trace
        with figure(f"mean_std_{col}"):
            ax = pm.plot_posterior(trace[100:],
                                   varnames=[r"group1_mean", r"group2_mean",
                                             r"group1_std", "group2_std"],
                                   kde_plot=True, color="C0")
            for a in (1, 3):
                ax[a].lines[0].set_color("C1")

        with figure(f"difference_{col}"):
            pm.plot_posterior(trace[1000:],
                              varnames=["difference of means", "effect size"],
                              ref_val=0, kde_plot=True, color="C2")

if __name__ == "__main__":
    latexify()

    results, analyses = analyze_data()
    print("Loaded files")

    # do_paths()
    # do_distributions()
    # do_durations()
    # do_errors()

    # users, tlx, surveys = load_surveys()
    # print("Loaded surveys")

    # do_surveys()

    # WARNING: Takes a long time with recalculate.
    # do_differences()

<|endoftext|>


<commit_before>from __future__ import absolute_import

import sys
import time
import ctypes
import ctypes.util

def load_libc():
    libc_path = ctypes.util.find_library("libc")
    libc = ctypes.cdll.LoadLibrary(libc_path)
    return libc

class FallbackTime(object):
    _time = staticmethod(time.time)

    def __init__(self):
        self._elapsed = 0
        self._origin = self._time()
        self._previous = self._origin

    def monotonic(self):
        now = self._time()
        if now < self._previous:
            self._elapsed += self._previous - self._origin
            self._origin = now
        self._previous = now
        return self._elapsed + (now - self._origin)

class DarwinTime(object):
    def __init__(self):
        class mach_timebase_info_t(ctypes.Structure):
            _fields_ = [
                ("numerator", ctypes.c_uint32),
                ("denominator", ctypes.c_uint32)
            ]

        libc = load_libc()

        mach_timebase_info = libc.mach_timebase_info
        mach_timebase_info.restype = None
        mach_timebase_info.argtypes = [ctypes.POINTER(mach_timebase_info_t)]

        self._timebase = mach_timebase_info_t()
        mach_timebase_info(self._timebase)

        self._mach_absolute_time = libc.mach_absolute_time
        self._mach_absolute_time.restype = ctypes.c_uint64
        self._mach_absolute_time.argtypes = []

        self._start = self._mach_absolute_time()

    def monotonic(self):
        elapsed = self._mach_absolute_time() - self._start
        ns = elapsed * self._timebase.numerator / self._timebase.denominator
        return ns * (10 ** -9)

if sys.platform == "darwin":
    _global = DarwinTime()
else:
    _global = FallbackTime()
monotonic = _global.monotonic
<commit_msg>idiokit._time: Use clock_gettime(CLOCK_MONOTONIC, ...) for _time.monotonic in Linux.<commit_after>from __future__ import absolute_import

import sys
import time
import ctypes
import ctypes.util

def load_libc():
    libc_path = ctypes.util.find_library("libc")
    libc = ctypes.cdll.LoadLibrary(libc_path)
    return libc

class FallbackTime(object):
    _time = staticmethod(time.time)

    def __init__(self):
        self._elapsed = 0
        self._origin = self._time()
        self._previous = self._origin

    def monotonic(self):
        now = self._time()
        if now < self._previous:
            self._elapsed += self._previous - self._origin
            self._origin = now
        self._previous = now
        return self._elapsed + (now - self._origin)

class DarwinTime(object):
    def __init__(self):
        class mach_timebase_info_t(ctypes.Structure):
            _fields_ = [
                ("numerator", ctypes.c_uint32),
                ("denominator", ctypes.c_uint32)
            ]

        libc = load_libc()

        mach_timebase_info = libc.mach_timebase_info
        mach_timebase_info.restype = None
        mach_timebase_info.argtypes = [ctypes.POINTER(mach_timebase_info_t)]

        self._timebase = mach_timebase_info_t()
        mach_timebase_info(self._timebase)

        self._mach_absolute_time = libc.mach_absolute_time
        self._mach_absolute_time.restype = ctypes.c_uint64
        self._mach_absolute_time.argtypes = []

        self._start = self._mach_absolute_time()

    def monotonic(self):
        elapsed = self._mach_absolute_time() - self._start
        ns = elapsed * self._timebase.numerator / self._timebase.denominator
        return ns * (10 ** -9)

class LinuxTime(object):
    CLOCK_MONOTONIC = 1

    def __init__(self):
        time_t = ctypes.c_long

        class timespec(ctypes.Structure):
            _fields_ = [
                ("tv_sec", time_t),
                ("tv_nsec", ctypes.c_long)
            ]
        self._timespec = timespec

        libc = load_libc()
        self._clock_gettime = libc.clock_gettime
        self._clock_gettime.restype = ctypes.c_int
        self._clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]

        res = self._clock_gettime(self.CLOCK_MONOTONIC, ctypes.byref(timespec()))
        if res == -1:
            raise RuntimeError("can not use CLOCK_MONOTONIC")

    def monotonic(self):
        spec = self._timespec()
        self._clock_gettime(self.CLOCK_MONOTONIC, ctypes.byref(spec))
        return spec.tv_sec + spec.tv_nsec * (10 ** -9)

if sys.platform == "darwin":
    _global = DarwinTime()
elif sys.platform.startswith("linux"):
    _global = LinuxTime()
else:
    _global = FallbackTime()
monotonic = _global.monotonic
<|endoftext|>


<commit_before># Time:  O(n^3 * 4^n), n = 4
# Space: O(n^2)

from fractions import Fraction
from operator import *

class Solution(object):
    def judgePoint24(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        def dfs(nums):
            if len(nums) == 1:
                return nums[0] == 24
            ops = [add, sub, mul, div]
            for i in xrange(len(nums)):
                for j in xrange(len(nums)):
                    if i == j:
                        continue
                    for op in ops:
                        if op == div and nums[j] == 0:
                            continue
                        next_nums = [nums[k] for k in xrange(len(nums)) if k not in [i, j]]
                        next_nums.append(op(nums[i], nums[j]))
                        if dfs(next_nums):
                            return True
            return False

        return dfs(map(lambda x: Fraction(x, 1), nums))
<commit_msg>Update 24-game.py<commit_after># Time:  O(n^3 * 4^n), n = 4
# Space: O(n^2)

from fractions import Fraction
from operator import *

class Solution(object):
    def judgePoint24(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        def dfs(nums):
            if len(nums) == 1:
                return nums[0] == 24
            ops = [add, sub, mul, div]
            for i in xrange(len(nums)):
                for j in xrange(len(nums)):
                    if i == j:
                        continue
                    for op in ops:
                        if op == div and nums[j] == 0:
                            continue
                        next_nums = [nums[k] for k in xrange(len(nums)) if k not in [i, j]]
                        next_nums.append(op(nums[i], nums[j]))
                        if dfs(next_nums):
                            return True
            return False

        return dfs(map(Fraction, nums))
<|endoftext|>


<commit_before>from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

from tastypie.api import Api
from people.api import ArtistResource, StaffResource
<<<<<<< 9100186f39179ceb3ce72db4f7a6dfbc735d4b79
from production.api import InstallationResource, InstallationGenreResource, FilmResource, FilmGenreResource, PerformanceResource, EventResource, ExhibitionResource, ItineraryResource, ArtworkResource, ProductionStaffTaskResource, StaffTaskResource, ProductionOrganizationTaskResource, OrganizationTaskResource
=======
from production.api import InstallationResource, InstallationGenreResource, FilmResource, FilmGenreResource, PerformanceResource, EventResource, ExhibitionResource, ItineraryResource, ArtworkResource
>>>>>>> display installation / film  genre
from diffusion.api import PlaceResource
from school.api import PromotionResource, StudentResource, StudentApplicationResource

v1_api = Api(api_name='v1')
v1_api.register(InstallationGenreResource())
v1_api.register(InstallationResource())
v1_api.register(FilmResource())
v1_api.register(FilmGenreResource())
v1_api.register(PerformanceResource())
v1_api.register(EventResource())
v1_api.register(PromotionResource())
v1_api.register(StudentResource())
v1_api.register(StudentApplicationResource())
v1_api.register(ArtistResource())
v1_api.register(StaffResource())
v1_api.register(ProductionStaffTaskResource())
v1_api.register(StaffTaskResource())
v1_api.register(OrganizationTaskResource())
v1_api.register(ProductionOrganizationTaskResource())
v1_api.register(PlaceResource())
v1_api.register(ExhibitionResource())
v1_api.register(ItineraryResource())
v1_api.register(ArtworkResource())

urlpatterns = patterns('',
                       # Examples:
                       # url(r'^$', 'ifresnoy.views.home', name='home'),
                       # url(r'^blog/', include('blog.urls')),
                       #(r'^v2/', include(v2_api.urls)),
                       (r'^', include(v1_api.urls)),
                       (r'^grappelli/', include('grappelli.urls')),
                       url('^markdown/', include('django_markdown.urls')),
                       url(r'v1/doc/',
                           include('tastypie_swagger.urls', namespace='ifresnoy_tastypie_swagger'),
                           kwargs={"tastypie_api_module": "ifresnoy.urls.v1_api",
                                   "namespace": "ifresnoy_tastypie_swagger"}),
                       url(r'^admin/', include(admin.site.urls)) \
                       ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<commit_msg>url conflict<commit_after>from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

from tastypie.api import Api
from people.api import ArtistResource, StaffResource

from production.api import InstallationResource, InstallationGenreResource, FilmResource, FilmGenreResource, PerformanceResource, EventResource, ExhibitionResource, ItineraryResource, ArtworkResource, ProductionStaffTaskResource, StaffTaskResource, ProductionOrganizationTaskResource, OrganizationTaskResource

from diffusion.api import PlaceResource
from school.api import PromotionResource, StudentResource, StudentApplicationResource

v1_api = Api(api_name='v1')
v1_api.register(InstallationGenreResource())
v1_api.register(InstallationResource())
v1_api.register(FilmResource())
v1_api.register(FilmGenreResource())
v1_api.register(PerformanceResource())
v1_api.register(EventResource())
v1_api.register(PromotionResource())
v1_api.register(StudentResource())
v1_api.register(StudentApplicationResource())
v1_api.register(ArtistResource())
v1_api.register(StaffResource())
v1_api.register(ProductionStaffTaskResource())
v1_api.register(StaffTaskResource())
v1_api.register(OrganizationTaskResource())
v1_api.register(ProductionOrganizationTaskResource())
v1_api.register(PlaceResource())
v1_api.register(ExhibitionResource())
v1_api.register(ItineraryResource())
v1_api.register(ArtworkResource())

urlpatterns = patterns('',
                       # Examples:
                       # url(r'^$', 'ifresnoy.views.home', name='home'),
                       # url(r'^blog/', include('blog.urls')),
                       #(r'^v2/', include(v2_api.urls)),
                       (r'^', include(v1_api.urls)),
                       (r'^grappelli/', include('grappelli.urls')),
                       url('^markdown/', include('django_markdown.urls')),
                       url(r'v1/doc/',
                           include('tastypie_swagger.urls', namespace='ifresnoy_tastypie_swagger'),
                           kwargs={"tastypie_api_module": "ifresnoy.urls.v1_api",
                                   "namespace": "ifresnoy_tastypie_swagger"}),
                       url(r'^admin/', include(admin.site.urls)) \
                       ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<|endoftext|>


<commit_before># Time:  O(nlogn)
# Space: O(1)

# Given an array of citations (each citation is a non-negative integer)
# of a researcher, write a function to compute the researcher's h-index.
#
# According to the definition of h-index on Wikipedia:
# "A scientist has index h if h of his/her N papers have
# at least h citations each, and the other N − h papers have
# no more than h citations each."
#
# For example, given citations = [3, 0, 6, 1, 5],
# which means the researcher has 5 papers in total
# and each of them had received 3, 0, 6, 1, 5 citations respectively.
# Since the researcher has 3 papers with at least 3 citations each and
# the remaining two with no more than 3 citations each, his h-index is 3.
#
# Note: If there are several possible values for h, the maximum one is taken as the h-index.
#

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        citations.sort(reverse=True)
        h = 0
        for i, x in enumerate(citations):
            if x >= i + 1:
                h += 1
            else:
                break
        return h

# Time:  O(nlogn)
# Space: O(n)
class Solution2(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        return sum(x >= i + 1 for i, x in enumerate(sorted(citations, reverse=True)))

<commit_msg>Update h-index.py<commit_after># Time:  O(nlogn)
# Space: O(1)

# Given an array of citations (each citation is a non-negative integer)
# of a researcher, write a function to compute the researcher's h-index.
#
# According to the definition of h-index on Wikipedia:
# "A scientist has index h if h of his/her N papers have
# at least h citations each, and the other N − h papers have
# no more than h citations each."
#
# For example, given citations = [3, 0, 6, 1, 5],
# which means the researcher has 5 papers in total
# and each of them had received 3, 0, 6, 1, 5 citations respectively.
# Since the researcher has 3 papers with at least 3 citations each and
# the remaining two with no more than 3 citations each, his h-index is 3.
#
# Note: If there are several possible values for h, the maximum one is taken as the h-index.
#

class Solution(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        citations.sort(reverse=True)
        h = 0
        for x in citations:
            if x >= h + 1:
                h += 1
            else:
                break
        return h

# Time:  O(nlogn)
# Space: O(n)
class Solution2(object):
    def hIndex(self, citations):
        """
        :type citations: List[int]
        :rtype: int
        """
        return sum(x >= i + 1 for i, x in enumerate(sorted(citations, reverse=True)))

<|endoftext|>


<commit_before>import time
from base import BasePart

class Test(BasePart):
    """POST /companies/:id - 20 %"""
    tests = [
        ('verify_company_not_found_404', 5),
        ('verify_company_is_deleted', 5),
        ('verify_company_is_deleted_in_es', 10)
    ]

    def __init__(self, base_url, admin_token):
        super(self.__class__, self).__init__(base_url, admin_token)
        self.url = self.base_url + '/companies'
        self.company = None

        try:
            # Create five test_companies
            headers = {
                'admin_token': self.admin_token,
                'content-type': 'application/json'
            }

            self.company = self.make_request('post', self.url, data={
                'title': self.random_string(10),
                'description': 'url',
                'url': 'http://www.foo.is',
            }, headers=headers).json()
        except Exception as ex:
            print 'Unable to create test companies', ex.message

        if not self.company.get('id'):
            raise Exception('Unable to fetch company id for created company')

    def verify_company_not_found_404(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }
        resp = self.make_request('delete', self.url + '/dummy-id', headers=headers, data=data)
        return resp.status_code == 404

    def verify_company_is_deleted(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }

        resp = self.make_request('post', self.url, headers=headers, data=data)
        company_id = resp.json().get('id')

        # Next we update the company
        self.make_request('delete', self.url + '/' + company_id, headers=headers)

        r = self.make_request('get', self.url + '/' + company_id, headers=headers)
        return r.status_code == 404

    def verify_company_is_deleted_in_es(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }

        resp = self.make_request('post', self.url, headers=headers, data=data)
        company_id = resp.json().get('id')
        self.make_request('delete', self.url + '/' + company_id, headers=headers)

        time.sleep(5)

        companies = self.make_request('get', self.url + '?max=500', headers=headers).json()
        c = filter(lambda x: x.get('title') == data.get('title'), companies)
        return len(c) == 0
<commit_msg>Fixed printing typo<commit_after>import time
from base import BasePart

class Test(BasePart):
    """DELETE /companies/:id - 20 %"""
    tests = [
        ('verify_company_not_found_404', 5),
        ('verify_company_is_deleted', 5),
        ('verify_company_is_deleted_in_es', 10)
    ]

    def __init__(self, base_url, admin_token):
        super(self.__class__, self).__init__(base_url, admin_token)
        self.url = self.base_url + '/companies'
        self.company = None

        try:
            # Create five test_companies
            headers = {
                'admin_token': self.admin_token,
                'content-type': 'application/json'
            }

            self.company = self.make_request('post', self.url, data={
                'title': self.random_string(10),
                'description': 'url',
                'url': 'http://www.foo.is',
            }, headers=headers).json()
        except Exception as ex:
            print 'Unable to create test companies', ex.message

        if not self.company.get('id'):
            raise Exception('Unable to fetch company id for created company')

    def verify_company_not_found_404(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }
        resp = self.make_request('delete', self.url + '/dummy-id', headers=headers, data=data)
        return resp.status_code == 404

    def verify_company_is_deleted(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }

        resp = self.make_request('post', self.url, headers=headers, data=data)
        company_id = resp.json().get('id')

        # Next we update the company
        self.make_request('delete', self.url + '/' + company_id, headers=headers)

        r = self.make_request('get', self.url + '/' + company_id, headers=headers)
        return r.status_code == 404

    def verify_company_is_deleted_in_es(self):
        headers = {
            'admin_token': self.admin_token,
            'content-type': 'application/json'
        }
        data = {
            'title': self.random_string(10),
            'description': 'url',
            'url': 'http://www.foo.is',
        }

        resp = self.make_request('post', self.url, headers=headers, data=data)
        company_id = resp.json().get('id')
        self.make_request('delete', self.url + '/' + company_id, headers=headers)

        time.sleep(5)

        companies = self.make_request('get', self.url + '?max=500', headers=headers).json()
        c = filter(lambda x: x.get('title') == data.get('title'), companies)
        return len(c) == 0
<|endoftext|>


<commit_before>import pandas as pd
import numpy as np
import argparse

def fix_tumor_sample(tsample):
    if 'tumor' in tsample.lower():
        return tsample.strip('-Tumor')
    else:
        return tsample

def fix_tumor_type(ttype):
    ttype_dict = {'OV': 'Ovarian',
                  'ESO': 'Esophageal Adenocarcinoma',
                  'CRC': 'Colorectal',
                  'NB': 'Neuroblastoma',
                  'GBM': 'Glioblastoma Multiforme',
                  'PRAD': 'Prostate Adenocarcinoma',
                  'UCEC': 'Endometrial Carcinoma',
                  'BLCA': 'Bladder Urothelial Carcinoma',
                  'BRCA': 'Breast Adenocarcinoma',
                  'MEL': 'Melanoma',
                  'LUSC': 'Lung Squamous Cell Carcinoma',
                  'MED': 'Medulloblastoma',
                  'HNSC': 'Head and Neck Squamous Cell Carcinoma',
                  'LUAD': 'Lung Adenocarcinoma',
                  'KIRC': 'Kidney Clear Cell Carcinoma'}
    if ttype_dict.has_key(ttype):
        return ttype_dict[ttype]
    else:
        return ttype

def fix_variant_type(var_clf):
    if 'missense' in var_clf.lower():
        return 'Missense_Mutation'
    elif 'splice_site' in var_clf.lower():
        return 'Splice_Site'
    elif 'frame_shift' in var_clf.lower():
        return 'Frame_Shift_Indel'
    elif 'in_frame' in var_clf.lower():
        return 'In_Frame_Indel'
    elif 'synonymous' in var_clf.lower():
        return 'Silent'
    elif 'nonsense' in var_clf.lower():
        return 'Nonsense_Mutation'
    elif 'silent' in var_clf.lower():
        return 'Silent'
    elif 'nonstop' in var_clf.lower():
        return 'Nonstop_Mutation'
    elif 'translation_start_site' in var_clf.lower():
        return 'Translation_Start_Site'
    return var_clf

def generate_hgvs_syntax(df):
    hgvs_list = []
    for row in df.iterrows():
        row = row[1]  # grab the pandas series object
        seq_ont = row.ix['Sequence Ontology']
        nulls_in_row = row.isnull()

        # fix bug related to pandas forcing conversion of
        # dtype to float if there exists a NaN
        if not nulls_in_row['Amino acid position']:
            pos = int(row.ix['Amino acid position'])
        else:
            pos = row.ix['Amino acid position']

        if seq_ont in ['SY', 'SL', 'SG', 'MS', 'CS']:
            ref = row.ix['Reference amino acid(s)']
            alt = row.ix['Alternate amino acid(s)']
            hgvs_string = 'p.{0}{1}{2}'.format(ref, pos, alt)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'II':
            ins_len = len(row.ix['Alternate base'][:-1]) / 3
            hgvs_string = 'p.K{0}_K{1}ins{2}'.format(pos, pos+1, 'K'*ins_len)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'ID':
            del_len = len(row.ix['Reference base']) / 3
            hgvs_string = 'p.K{0}_K{1}del{2}'.format(pos, pos+del_len-1, 'K'*del_len)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'FI' or seq_ont == 'FD':
            hgvs_string = 'p.K{0}fs*'.format(pos)
            hgvs_list.append(hgvs_string)
        elif seq_ont is np.nan:
            hgvs_list.append('p.?')
    return hgvs_list

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--maf',
                        type=str, action='store',
                        help='Filtered MAF file from broad website')
    parser.add_argument('-n', '--non-coding',
                        type=str, default=None,
                        help='Non-coding portion of MAF file from broad'
                        ' website. Only needed if want non-coding.')
    parser.add_argument('-c', '--cravat',
                        type=str, action='store',
                        help='Cravat output which includes amino '
                        'acid info (Variant_Analysis.tsv)')
    parser.add_argument('-o', '--output',
                        type=str, action='store',
                        help='Modified MAF format acceptable for input')
    parser.add_argument('-no', '--non-coding-output',
                        type=str, default=None,
                        help='Modified MAF format for non-coding output')
    args = parser.parse_args()
    return vars(args)

def main(opts):
    # read in data
    cravat_df = pd.read_csv(opts['cravat'], sep='\t')
    broad_df = pd.read_csv(opts['maf'], sep='\t')
    prev_len = len(broad_df)

    # read non-coding
    if opts['non_coding'] is not None:
        non_coding_df = pd.read_csv(opts['non_coding'], sep='\t')

    # get hgvs strings from cravat output
    hgvs_list = generate_hgvs_syntax(cravat_df)
    broad_df['Protein_Change'] = hgvs_list

    # filter out variants with mappability warning
    broad_df = broad_df[cravat_df['Mappability Warning'].isnull()]
    after_len = len(broad_df)
    print('Before mappability filtering: {0} lines'.format(prev_len))
    print('After mappability filtering: {0} lines'.format(after_len))
    print('Line difference: {0}'.format(prev_len-after_len))

    # rename headers
    rename_cols = {'ttype': 'Tumor_Type',
                   'patient': 'Tumor_Sample',
                   'gene': 'Gene_Symbol',
                   'type': 'Variant_Classification',
                   'chr': 'Chromosome',
                   'ref_allele': 'Reference_Allele',
                   'newbase': 'Tumor_Allele',
                   'pos': 'Start_Position'}
    broad_df = broad_df.rename(columns=rename_cols)

    # add end position column
    broad_df['End_Position'] = broad_df['Start_Position'] + broad_df['Tumor_Allele'].apply(lambda x: len(x) - 1)

    # fix variant classigfication column
    broad_df['Variant_Classification'] = broad_df['Variant_Classification'].apply(fix_variant_type)

    # add chr to chromosome names
    broad_df['Chromosome'] = broad_df['Chromosome'].astype(str).apply(lambda x: 'chr' + x)

    # fix tumor names
    broad_df['Tumor_Type'] = broad_df['Tumor_Type'].apply(fix_tumor_type)
    broad_df['Tumor_Sample'] = broad_df['Tumor_Sample'].apply(fix_tumor_sample)

    cols_of_interest = ['Gene_Symbol', 'Tumor_Sample', 'Tumor_Type',
                        'Chromosome', 'Start_Position',
                        'End_Position', 'Variant_Classification',
                        'Reference_Allele', 'Tumor_Allele',
                        'Protein_Change']

    if opts['non_coding'] is not None:
        non_coding_df = non_coding_df.rename(columns=rename_cols)

        # add end position column
        non_coding_df['End_Position'] = non_coding_df['Start_Position'] + non_coding_df['Tumor_Allele'].apply(lambda x: len(x) - 1)

        # fix variant classigfication column
        non_coding_df['Variant_Classification'] = non_coding_df['Variant_Classification'].apply(fix_variant_type)

        # add chr to chromosome names
        non_coding_df['Chromosome'] = non_coding_df['Chromosome'].astype(str).apply(lambda x: 'chr' + x)

        # fix tumor names
        non_coding_df['Tumor_Type'] = non_coding_df['Tumor_Type'].apply(fix_tumor_type)
        non_coding_df['Tumor_Sample'] = non_coding_df['Tumor_Sample'].apply(fix_tumor_sample)

        # save non-coding
        non_coding_df[cols_of_interest[:-1]].to_csv(opts['non_coding_output'], sep='\t', index=False)

    # output results to file
    broad_df[cols_of_interest].to_csv(opts['output'], sep='\t', index=False)

if __name__ == "__main__":
    opts = parse_arguments()
    main(opts)
<commit_msg>Updated lawrence to maf script<commit_after>import pandas as pd
import numpy as np
import argparse

def fix_tumor_sample(tsample):
    if 'tumor' in tsample.lower():
        return tsample.strip('-Tumor')
    else:
        return tsample

def fix_tumor_type(ttype):
    ttype_dict = {'OV': 'Ovarian',
                  'ESO': 'Esophageal Adenocarcinoma',
                  'CRC': 'Colorectal',
                  'NB': 'Neuroblastoma',
                  'GBM': 'Glioblastoma Multiforme',
                  'PRAD': 'Prostate Adenocarcinoma',
                  'UCEC': 'Endometrial Carcinoma',
                  'BLCA': 'Bladder Urothelial Carcinoma',
                  'BRCA': 'Breast Adenocarcinoma',
                  'MEL': 'Melanoma',
                  'LUSC': 'Lung Squamous Cell Carcinoma',
                  'MED': 'Medulloblastoma',
                  'HNSC': 'Head and Neck Squamous Cell Carcinoma',
                  'LUAD': 'Lung Adenocarcinoma',
                  'KIRC': 'Kidney Clear Cell Carcinoma'}
    if ttype_dict.has_key(ttype):
        return ttype_dict[ttype]
    else:
        return ttype

def fix_variant_type(var_clf):
    if 'missense' in var_clf.lower():
        return 'Missense_Mutation'
    elif 'splice_site' in var_clf.lower():
        return 'Splice_Site'
    elif 'frame_shift' in var_clf.lower():
        return 'Frame_Shift_Indel'
    elif 'in_frame' in var_clf.lower():
        return 'In_Frame_Indel'
    elif 'synonymous' in var_clf.lower():
        return 'Silent'
    elif 'nonsense' in var_clf.lower():
        return 'Nonsense_Mutation'
    elif 'silent' in var_clf.lower():
        return 'Silent'
    elif 'nonstop' in var_clf.lower():
        return 'Nonstop_Mutation'
    elif 'translation_start_site' in var_clf.lower():
        return 'Translation_Start_Site'
    return var_clf

def generate_hgvs_syntax(df):
    hgvs_list = []
    for row in df.iterrows():
        row = row[1]  # grab the pandas series object
        seq_ont = row.ix['Sequence Ontology']
        nulls_in_row = row.isnull()

        # fix bug related to pandas forcing conversion of
        # dtype to float if there exists a NaN
        if not nulls_in_row['Amino acid position']:
            pos = int(row.ix['Amino acid position'])
        else:
            pos = row.ix['Amino acid position']

        if seq_ont in ['SY', 'SL', 'SG', 'MS', 'CS']:
            ref = row.ix['Reference amino acid(s)']
            alt = row.ix['Alternate amino acid(s)']
            hgvs_string = 'p.{0}{1}{2}'.format(ref, pos, alt)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'II':
            ins_len = len(row.ix['Alternate base'][:-1]) / 3
            hgvs_string = 'p.K{0}_K{1}ins{2}'.format(pos, pos+1, 'K'*ins_len)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'ID':
            del_len = len(row.ix['Reference base']) / 3
            hgvs_string = 'p.K{0}_K{1}del{2}'.format(pos, pos+del_len-1, 'K'*del_len)
            hgvs_list.append(hgvs_string)
        elif seq_ont == 'FI' or seq_ont == 'FD':
            hgvs_string = 'p.K{0}fs*'.format(pos)
            hgvs_list.append(hgvs_string)
        elif seq_ont is np.nan:
            hgvs_list.append('p.?')
    return hgvs_list

def parse_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--maf',
                        type=str, action='store',
                        help='Filtered MAF file from broad website')
    parser.add_argument('-n', '--non-coding',
                        type=str, default=None,
                        help='Non-coding portion of MAF file from broad'
                        ' website. Only needed if want non-coding.')
    parser.add_argument('-c', '--cravat',
                        type=str, action='store',
                        help='Cravat output which includes amino '
                        'acid info (Variant_Analysis.tsv)')
    parser.add_argument('-o', '--output',
                        type=str, action='store',
                        help='Modified MAF format acceptable for input')
    parser.add_argument('-no', '--non-coding-output',
                        type=str, default=None,
                        help='Modified MAF format for non-coding output')
    args = parser.parse_args()
    return vars(args)

def main(opts):
    # read in data
    cravat_df = pd.read_csv(opts['cravat'], sep='\t')
    broad_df = pd.read_csv(opts['maf'], sep='\t')
    prev_len = len(broad_df)

    # read non-coding
    if opts['non_coding'] is not None:
        non_coding_df = pd.read_csv(opts['non_coding'], sep='\t')

    # get hgvs strings from cravat output
    hgvs_list = generate_hgvs_syntax(cravat_df)
    broad_df['Protein_Change'] = hgvs_list

    # filter out variants with mappability warning
    broad_df = broad_df[cravat_df['Mappability Warning'].isnull()]
    after_len = len(broad_df)
    print('Before mappability filtering: {0} lines'.format(prev_len))
    print('After mappability filtering: {0} lines'.format(after_len))
    print('Line difference: {0}'.format(prev_len-after_len))

    # rename headers
    rename_cols = {'ttype': 'Tumor_Type',
                   'patient': 'Tumor_Sample',
                   'gene': 'Gene_Symbol',
                   'type': 'Variant_Classification',
                   'chr': 'Chromosome',
                   'ref_allele': 'Reference_Allele',
                   'newbase': 'Tumor_Allele',
                   'pos': 'Start_Position'}
    broad_df = broad_df.rename(columns=rename_cols)

    # add end position column
    #broad_df['End_Position'] = broad_df['Start_Position'] + broad_df['Tumor_Allele'].apply(lambda x: len(x) - 1)
    broad_df['End_Position'] = broad_df['Start_Position']
    del_variants = ['In_Frame_Del', 'Frame_Shift_Del',]
    is_del = broad_df['Variant_Classification'].isin(del_variants)
    broad_df['End_Position'][is_del] = broad_df['End_Position'][is_del] + broad_df['Reference_Allele'][is_del].apply(lambda x: len(x) - 1)
    ins_variants = ['In_Frame_Ins', 'Frame_Shift_Ins',]
    is_ins = broad_df['Variant_Classification'].isin(ins_variants)
    broad_df['End_Position'][is_ins] = broad_df['End_Position'][is_ins] + 1

    # fix variant classigfication column
    broad_df['Variant_Classification'] = broad_df['Variant_Classification'].apply(fix_variant_type)

    # add chr to chromosome names
    broad_df['Chromosome'] = broad_df['Chromosome'].astype(str).apply(lambda x: 'chr' + x)

    # fix tumor names
    broad_df['Tumor_Type'] = broad_df['Tumor_Type'].apply(fix_tumor_type)
    broad_df['Tumor_Sample'] = broad_df['Tumor_Sample'].apply(fix_tumor_sample)

    cols_of_interest = ['Gene_Symbol', 'Tumor_Sample', 'Tumor_Type',
                        'Chromosome', 'Start_Position',
                        'End_Position', 'Variant_Classification',
                        'Reference_Allele', 'Tumor_Allele',
                        'Protein_Change']

    if opts['non_coding'] is not None:
        non_coding_df = non_coding_df.rename(columns=rename_cols)

        # add end position column
        # non_coding_df['End_Position'] = non_coding_df['Start_Position'] + non_coding_df['Tumor_Allele'].apply(lambda x: len(x) - 1)
        non_coding_df['End_Position'] = non_coding_df['Start_Position']
        is_del = non_coding_df['Tumor_Allele'] == '-'
        non_coding_df['End_Position'][is_del] += non_coding_df['Reference_Allele'][is_del].apply(lambda x: len(x) - 1)
        is_ins = non_coding_df['Reference_Allele'] == '-'
        non_coding_df['End_Position'][is_ins] += 1

        # fix variant classigfication column
        non_coding_df['Variant_Classification'] = non_coding_df['Variant_Classification'].apply(fix_variant_type)

        # add chr to chromosome names
        non_coding_df['Chromosome'] = non_coding_df['Chromosome'].astype(str).apply(lambda x: 'chr' + x)

        # fix tumor names
        non_coding_df['Tumor_Type'] = non_coding_df['Tumor_Type'].apply(fix_tumor_type)
        non_coding_df['Tumor_Sample'] = non_coding_df['Tumor_Sample'].apply(fix_tumor_sample)

        # save non-coding
        non_coding_df[cols_of_interest[:-1]].to_csv(opts['non_coding_output'], sep='\t', index=False)

    # output results to file
    broad_df[cols_of_interest].to_csv(opts['output'], sep='\t', index=False)

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


<commit_before>from microcosm_flask.audit import parse_response
from microcosm_flask.errors import extract_status_code

try:
    from microcosm_metrics.classifier import Classifier
except ImportError:
    raise Exception("Route metrics require 'microcosm-metrics'")

class StatusCodeClassifier(Classifier):
    """
    Label route result/error with its status code.

    """
    def label_result(self, result):
        _, status_code, _ = parse_response(result)
        return self.normalize_status_code(status_code)

    def label_error(self, error):
        status_code = extract_status_code(error)
        return self.normalize_status_code(status_code)

    # Return only the first digit of the status code followed by xx as a string
    # Example: 403 becomes "4xx"
    # This will reduce the cardinality of metrics collected which will reduce cost
    # For detailed status code errors we have logs
    def normalize_status_code(self, status_code: int) -> str:
        return str(status_code)[0] + "xx"
<commit_msg>linted again<commit_after>from microcosm_flask.audit import parse_response
from microcosm_flask.errors import extract_status_code

try:
    from microcosm_metrics.classifier import Classifier
except ImportError:
    raise Exception("Route metrics require 'microcosm-metrics'")

class StatusCodeClassifier(Classifier):
    """
    Label route result/error with its status code.

    """
    def label_result(self, result):
        _, status_code, _ = parse_response(result)
        return self.normalize_status_code(status_code)

    def label_error(self, error):
        status_code = extract_status_code(error)
        return self.normalize_status_code(status_code)

    # Return only the first digit of the status code followed by xx as a string
    # Example: 403 becomes "4xx"
    # This will reduce the cardinality of metrics collected which will reduce cost
    # For detailed status code errors we have logs
    def normalize_status_code(self, status_code: int) -> str:
        return str(status_code)[0] + "xx"
<|endoftext|>


<commit_before>#------------------------------------------------------------------------------
# Copyright (c) 2015, 2016, The University of Manchester, UK.
#
# Licenced under BSD. See LICENCE for details.
#
# Authors: Arturo Wong, Robert Haines
#------------------------------------------------------------------------------

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def regression(file_name, second_index_quality, order):

    COLUMN_SEPARATOR = ','
    metrics_data = pd.DataFrame.from_csv(file_name, sep=COLUMN_SEPARATOR, header=None)
    labels = ['',
              'Test Coverage',
              'Branch Coverage',
              'McCabe\'s Cyclomatic Complexity (CC)',
              'Lines of Code (LOC)',
              'Number of local Methods (NOM)',
              'Improvement of Lack of Cohesion in Methods (ILCOM)',
              'Lack of Documentation (LOD)',
              'Depth of Inheritance Tree (DIT)',
              'Coupling Between Objects (CBO)'
             ]

    x = [0,0]
    ys = [0,0]
    for i in range(0, 2):
        x[i] = metrics_data[i + 1]
        y = metrics_data[second_index_quality]

        coeffs = np.polyfit(x[i], y, order)
        polynomial = np.poly1d(coeffs)
        ys[i] = polynomial(x[i])

    plt.figure(second_index_quality - 2)
    plt.xlabel("Coverage")
    plt.ylabel(labels[second_index_quality])
    plt.title(labels[second_index_quality])
    plt.plot(x[0], ys[0])
    plt.plot(x[1], ys[1])
    plt.legend([labels[1], labels[2]], loc='upper right')

file_path = '../data/combined/Merge/Merge all projects.csv'
for x in range(3,10):
    regression(file_path, x, 1)
    plt.show()
<commit_msg>Zero the y-axis in output graphs.<commit_after>#------------------------------------------------------------------------------
# Copyright (c) 2015, 2016, The University of Manchester, UK.
#
# Licenced under BSD. See LICENCE for details.
#
# Authors: Arturo Wong, Robert Haines
#------------------------------------------------------------------------------

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def regression(file_name, second_index_quality, order):

    COLUMN_SEPARATOR = ','
    metrics_data = pd.DataFrame.from_csv(file_name, sep=COLUMN_SEPARATOR, header=None)
    labels = ['',
              'Test Coverage',
              'Branch Coverage',
              'McCabe\'s Cyclomatic Complexity (CC)',
              'Lines of Code (LOC)',
              'Number of local Methods (NOM)',
              'Improvement of Lack of Cohesion in Methods (ILCOM)',
              'Lack of Documentation (LOD)',
              'Depth of Inheritance Tree (DIT)',
              'Coupling Between Objects (CBO)'
             ]

    x = [0,0]
    ys = [0,0]
    for i in range(0, 2):
        x[i] = metrics_data[i + 1]
        y = metrics_data[second_index_quality]

        coeffs = np.polyfit(x[i], y, order)
        polynomial = np.poly1d(coeffs)
        ys[i] = polynomial(x[i])

    plt.figure(second_index_quality - 2)
    plt.xlabel("Coverage")
    plt.ylabel(labels[second_index_quality])
    plt.title(labels[second_index_quality])
    plt.plot(x[0], ys[0])
    plt.plot(x[1], ys[1])
    plt.ylim(ymin=0)
    plt.legend([labels[1], labels[2]], loc='upper right')

file_path = '../data/combined/Merge/Merge all projects.csv'
for x in range(3,10):
    regression(file_path, x, 1)
    plt.show()
<|endoftext|>


<commit_before># Copyright 2010 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# This is the server part of the package dashboard.
# It must be run by App Engine.

from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.api import urlfetch
import datetime
import logging
import os
import re
import urllib2
import sets

# local imports
import toutf8
import const

template.register_template_library('toutf8')

# Storage model for package info recorded on server.
# Just path, count, and time of last install.
class Package(db.Model):
    path = db.StringProperty()
    web_url = db.StringProperty()  # derived from path
    count = db.IntegerProperty()
    last_install = db.DateTimeProperty()

class Project(db.Model):
    name = db.StringProperty(indexed=True)
    descr = db.StringProperty()
    web_url = db.StringProperty()
    package = db.ReferenceProperty(Package)
    category = db.StringProperty(indexed=True)
    tags = db.ListProperty(str)
    approved = db.BooleanProperty(indexed=True)

re_bitbucket = re.compile(r'^bitbucket\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+$')
re_googlecode = re.compile(r'^[a-z0-9\-]+\.googlecode\.com/(svn|hg)$')
re_github = re.compile(r'^github\.com/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+$')
re_launchpad = re.compile(r'^launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?$')

def vc_to_web(path):
    if re_bitbucket.match(path):
        check_url = 'http://' + path + '/?cmd=heads'
        web = 'http://' + path + '/'
    elif re_github.match(path):
        # github doesn't let you fetch the .git directory anymore.
        # fetch .git/info/refs instead, like git clone would.
        check_url = 'http://'+path+'.git/info/refs'
        web = 'http://' + path
    elif re_googlecode.match(path):
        check_url = 'http://'+path
        web = 'http://code.google.com/p/' + path[:path.index('.')]
    elif re_launchpad.match(path):
        check_url = web = 'https://'+path
    else:
        return False, False
    return web, check_url

re_bitbucket_web = re.compile(r'bitbucket\.org/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)')
re_googlecode_web = re.compile(r'code.google.com/p/([a-z0-9\-]+)')
re_github_web = re.compile(r'github\.com/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)')
re_launchpad_web = re.compile(r'launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?')
re_striphttp = re.compile(r'https?://(www\.)?')

def web_to_vc(url):
    url = re_striphttp.sub('', url)
    m = re_bitbucket_web.match(url)
    if m:
        return 'bitbucket.org/'+m.group(1)+'/'+m.group(2)
    m = re_github_web.match(url)
    if m:
        return 'github.com/'+m.group(1)+'/'+m.group(2)
    m = re_googlecode_web.match(url)
    if m:
        path = m.group(1)+'.googlecode.com/'
        # perform http request to path/hg to check if they're using mercurial
        vcs = 'svn'
        try:
            response = urlfetch.fetch('http://'+path+'hg', deadline=1)
            if response.status_code == 200:
                vcs = 'hg'
        except: pass
        return path + vcs
    m = re_launchpad_web.match(url)
    if m:
        return m.group(0)
    return False

MaxPathLength = 100
CacheTimeout = 3600

class PackagePage(webapp.RequestHandler):
    def get(self):
        if self.request.get('fmt') == 'json':
            return self.json()

        html = memcache.get('view-package')
        if not html:
            q = Package.all()
            q.order('-last_install')
            by_time = q.fetch(100)

            q = Package.all()
            q.order('-count')
            by_count = q.fetch(100)

            self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
            path = os.path.join(os.path.dirname(__file__), 'package.html')
            html = template.render(
                path,
                {"by_time": by_time, "by_count": by_count}
            )
            memcache.set('view-package', html, time=CacheTimeout)

        self.response.out.write(html)

    def json(self):
        json = memcache.get('view-package-json')
        if not json:
            self.response.set_status(200)
            self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
            q = Package.all()
            s = '{"packages": ['
            sep = ''
            for r in q.fetch(1000):
                s += '%s\n\t{"path": "%s", "last_install": "%s", "count": "%s"}' % (sep, r.path, r.last_install, r.count)
                sep = ','
            s += '\n]}\n'
            json = s
            memcache.set('view-package-json', json, time=CacheTimeout)
        self.response.out.write(json)

    def can_get_url(self, url):
        try:
            req = urllib2.Request(url)
            response = urllib2.urlopen(req)
            return True
        except:
            return False

    def is_valid_package_path(self, path):
        return (re_bitbucket.match(path) or
            re_googlecode.match(path) or
            re_github.match(path) or
            re_launchpad.match(path))

    def record_pkg(self, path):
        # sanity check string
        if not path or len(path) > MaxPathLength or not self.is_valid_package_path(path):
            return False

        # look in datastore
        key = 'pkg-' + path
        p = Package.get_by_key_name(key)
        if p is None:
            # not in datastore - verify URL before creating
            web, check_url = vc_to_web(path)
            if not web:
                logging.error('unrecognized path: %s', path)
                return False
            if not self.can_get_url(check_url):
                logging.error('cannot get %s', check_url)
                return False
            p = Package(key_name = key, path = path, count = 0, web_url = web)

        # update package object
        p.count += 1
        p.last_install = datetime.datetime.utcnow()
        p.put()
        return True

    def post(self):
        path = self.request.get('path')
        ok = self.record_pkg(path)
        if ok:
            self.response.set_status(200)
            self.response.out.write('ok')
        else:
            logging.error('invalid path in post: %s', path)
            self.response.set_status(500)
            self.response.out.write('not ok')

class ProjectPage(webapp.RequestHandler):

    def get(self):
        admin = users.is_current_user_admin()
        if self.request.path == "/project/login":
            self.redirect(users.create_login_url("/project"))
        elif self.request.path == "/project/logout":
            self.redirect(users.create_logout_url("/project"))
        elif self.request.path == "/project/edit" and admin:
            self.edit()
        elif self.request.path == "/project/assoc" and admin:
            self.assoc()
        else:
            self.list()

    def assoc(self):
        projects = Project.all()
        for p in projects:
            if p.package:
                continue
            path = web_to_vc(p.web_url)
            if not path:
                continue
            pkg = Package.get_by_key_name("pkg-"+path)
            if not pkg:
                self.response.out.write('no: %s %s
' % (p.web_url, path))
                continue
            p.package = pkg
            p.put()
            self.response.out.write('yes: %s %s
' % (p.web_url, path))

    def post(self):
        if self.request.path == "/project/edit":
            self.edit(True)
        else:
            data = dict(map(lambda x: (x, self.request.get(x)), ["name","descr","web_url"]))
            if reduce(lambda x, y: x or not y, data.values(), False):
                data["submitMsg"] = "You must complete all the fields."
                self.list(data)
                return
            p = Project.get_by_key_name("proj-"+data["name"])
            if p is not None:
                data["submitMsg"] = "A project by this name already exists."
                self.list(data)
                return
            p = Project(key_name="proj-"+data["name"], **data)
            p.put()

            path = os.path.join(os.path.dirname(__file__), 'project-notify.txt')
            mail.send_mail(
                sender=const.mail_from,
                to=const.mail_submit_to,
                subject=const.mail_submit_subject,
                body=template.render(path, {'project': p}))

            self.list({"submitMsg": "Your project has been submitted."})

    def list(self, additional_data={}):
        cache_key = 'view-project-data'
        tag = self.request.get('tag', None)
        if tag:
            cache_key += '-'+tag
        data = memcache.get(cache_key)
        admin = users.is_current_user_admin()
        if admin or not data:
            projects = Project.all().order('category').order('name')
            if not admin:
                projects = projects.filter('approved =', True)
            projects = list(projects)

            tags = sets.Set()
            for p in projects:
                for t in p.tags:
                    tags.add(t)

            if tag:
                projects = filter(lambda x: tag in x.tags, projects)

            data = {}
            data['tag'] = tag
            data['tags'] = tags
            data['projects'] = projects
            data['admin']= admin
            if not admin:
                memcache.set(cache_key, data, time=CacheTimeout)

        for k, v in additional_data.items():
            data[k] = v

        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        path = os.path.join(os.path.dirname(__file__), 'project.html')
        self.response.out.write(template.render(path, data))

    def edit(self, save=False):
        if save:
            name = self.request.get("orig_name")
        else:
            name = self.request.get("name")

        p = Project.get_by_key_name("proj-"+name)
        if not p:
            self.response.out.write("Couldn't find that Project.")
            return

        if save:
            if self.request.get("do") == "Delete":
                p.delete()
            else:
                pkg_name = self.request.get("package", None)
                if pkg_name:
                    pkg = Package.get_by_key_name("pkg-"+pkg_name)
                    if pkg:
                        p.package = pkg.key()
                for f in ['name', 'descr', 'web_url', 'category']:
                    setattr(p, f, self.request.get(f, None))
                p.approved = self.request.get("approved") == "1"
                p.tags = filter(lambda x: x, self.request.get("tags", "").split(","))
                p.put()
            memcache.delete('view-project-data')
            self.redirect('/project')
            return

        # get all project categories and tags
        cats, tags = sets.Set(), sets.Set()
        for r in Project.all():
            cats.add(r.category)
            for t in r.tags:
                tags.add(t)

        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        path = os.path.join(os.path.dirname(__file__), 'project-edit.html')
        self.response.out.write(template.render(path, {
            "taglist": tags, "catlist": cats, "p": p, "tags": ",".join(p.tags) }))

    def redirect(self, url):
        self.response.set_status(302)
        self.response.headers.add_header("Location", url)

def main():
    app = webapp.WSGIApplication([
        ('/package', PackagePage),
        ('/project.*', ProjectPage),
        ], debug=True)
    run_wsgi_app(app)

if __name__ == '__main__':
    main()
<commit_msg>misc/godashboard: Accept sub-directories for goinstall's report. Fixed issue 1155.<commit_after># Copyright 2010 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# This is the server part of the package dashboard.
# It must be run by App Engine.

from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.api import urlfetch
import datetime
import logging
import os
import re
import urllib2
import sets

# local imports
import toutf8
import const

template.register_template_library('toutf8')

# Storage model for package info recorded on server.
# Just path, count, and time of last install.
class Package(db.Model):
    path = db.StringProperty()
    web_url = db.StringProperty()  # derived from path
    count = db.IntegerProperty()
    last_install = db.DateTimeProperty()

class Project(db.Model):
    name = db.StringProperty(indexed=True)
    descr = db.StringProperty()
    web_url = db.StringProperty()
    package = db.ReferenceProperty(Package)
    category = db.StringProperty(indexed=True)
    tags = db.ListProperty(str)
    approved = db.BooleanProperty(indexed=True)

re_bitbucket = re.compile(r'^bitbucket\.org/[a-z0-9A-Z_.\-]+/[a-z0-9A-Z_.\-]+$')
re_googlecode = re.compile(r'^[a-z0-9\-]+\.googlecode\.com/(svn|hg)$')
re_github = re.compile(r'^github\.com/[a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)+$')
re_launchpad = re.compile(r'^launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?$')

def vc_to_web(path):
    if re_bitbucket.match(path):
        check_url = 'http://' + path + '/?cmd=heads'
        web = 'http://' + path + '/'
    elif re_github.match(path):
        m = re_github_web.match(path)
        check_url = 'https://raw.github.com/' + m.group(1) + '/' + m.group(2) + '/master/'
        web = 'http://github.com/' + m.group(1) + '/' + m.group(2)
    elif re_googlecode.match(path):
        check_url = 'http://'+path
        web = 'http://code.google.com/p/' + path[:path.index('.')]
    elif re_launchpad.match(path):
        check_url = web = 'https://'+path
    else:
        return False, False
    return web, check_url

re_bitbucket_web = re.compile(r'bitbucket\.org/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)')
re_googlecode_web = re.compile(r'code.google.com/p/([a-z0-9\-]+)')
re_github_web = re.compile(r'github\.com/([a-z0-9A-Z_.\-]+)/([a-z0-9A-Z_.\-]+)')
re_launchpad_web = re.compile(r'launchpad\.net/([a-z0-9A-Z_.\-]+(/[a-z0-9A-Z_.\-]+)?|~[a-z0-9A-Z_.\-]+/(\+junk|[a-z0-9A-Z_.\-]+)/[a-z0-9A-Z_.\-]+)(/[a-z0-9A-Z_.\-/]+)?')
re_striphttp = re.compile(r'https?://(www\.)?')

def web_to_vc(url):
    url = re_striphttp.sub('', url)
    m = re_bitbucket_web.match(url)
    if m:
        return 'bitbucket.org/'+m.group(1)+'/'+m.group(2)
    m = re_github_web.match(url)
    if m:
        return 'github.com/'+m.group(1)+'/'+m.group(2)
    m = re_googlecode_web.match(url)
    if m:
        path = m.group(1)+'.googlecode.com/'
        # perform http request to path/hg to check if they're using mercurial
        vcs = 'svn'
        try:
            response = urlfetch.fetch('http://'+path+'hg', deadline=1)
            if response.status_code == 200:
                vcs = 'hg'
        except: pass
        return path + vcs
    m = re_launchpad_web.match(url)
    if m:
        return m.group(0)
    return False

MaxPathLength = 100
CacheTimeout = 3600

class PackagePage(webapp.RequestHandler):
    def get(self):
        if self.request.get('fmt') == 'json':
            return self.json()

        html = memcache.get('view-package')
        if not html:
            q = Package.all()
            q.order('-last_install')
            by_time = q.fetch(100)

            q = Package.all()
            q.order('-count')
            by_count = q.fetch(100)

            self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
            path = os.path.join(os.path.dirname(__file__), 'package.html')
            html = template.render(
                path,
                {"by_time": by_time, "by_count": by_count}
            )
            memcache.set('view-package', html, time=CacheTimeout)

        self.response.out.write(html)

    def json(self):
        json = memcache.get('view-package-json')
        if not json:
            self.response.set_status(200)
            self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
            q = Package.all()
            s = '{"packages": ['
            sep = ''
            for r in q.fetch(1000):
                s += '%s\n\t{"path": "%s", "last_install": "%s", "count": "%s"}' % (sep, r.path, r.last_install, r.count)
                sep = ','
            s += '\n]}\n'
            json = s
            memcache.set('view-package-json', json, time=CacheTimeout)
        self.response.out.write(json)

    def can_get_url(self, url):
        try:
            req = urllib2.Request(url)
            response = urllib2.urlopen(req)
            return True
        except:
            return False

    def is_valid_package_path(self, path):
        return (re_bitbucket.match(path) or
            re_googlecode.match(path) or
            re_github.match(path) or
            re_launchpad.match(path))

    def record_pkg(self, path):
        # sanity check string
        if not path or len(path) > MaxPathLength or not self.is_valid_package_path(path):
            return False

        # look in datastore
        key = 'pkg-' + path
        p = Package.get_by_key_name(key)
        if p is None:
            # not in datastore - verify URL before creating
            web, check_url = vc_to_web(path)
            if not web:
                logging.error('unrecognized path: %s', path)
                return False
            if not self.can_get_url(check_url):
                logging.error('cannot get %s', check_url)
                return False
            p = Package(key_name = key, path = path, count = 0, web_url = web)

        # update package object
        p.count += 1
        p.last_install = datetime.datetime.utcnow()
        p.put()
        return True

    def post(self):
        path = self.request.get('path')
        ok = self.record_pkg(path)
        if ok:
            self.response.set_status(200)
            self.response.out.write('ok')
        else:
            logging.error('invalid path in post: %s', path)
            self.response.set_status(500)
            self.response.out.write('not ok')

class ProjectPage(webapp.RequestHandler):

    def get(self):
        admin = users.is_current_user_admin()
        if self.request.path == "/project/login":
            self.redirect(users.create_login_url("/project"))
        elif self.request.path == "/project/logout":
            self.redirect(users.create_logout_url("/project"))
        elif self.request.path == "/project/edit" and admin:
            self.edit()
        elif self.request.path == "/project/assoc" and admin:
            self.assoc()
        else:
            self.list()

    def assoc(self):
        projects = Project.all()
        for p in projects:
            if p.package:
                continue
            path = web_to_vc(p.web_url)
            if not path:
                continue
            pkg = Package.get_by_key_name("pkg-"+path)
            if not pkg:
                self.response.out.write('no: %s %s
' % (p.web_url, path))
                continue
            p.package = pkg
            p.put()
            self.response.out.write('yes: %s %s
' % (p.web_url, path))

    def post(self):
        if self.request.path == "/project/edit":
            self.edit(True)
        else:
            data = dict(map(lambda x: (x, self.request.get(x)), ["name","descr","web_url"]))
            if reduce(lambda x, y: x or not y, data.values(), False):
                data["submitMsg"] = "You must complete all the fields."
                self.list(data)
                return
            p = Project.get_by_key_name("proj-"+data["name"])
            if p is not None:
                data["submitMsg"] = "A project by this name already exists."
                self.list(data)
                return
            p = Project(key_name="proj-"+data["name"], **data)
            p.put()

            path = os.path.join(os.path.dirname(__file__), 'project-notify.txt')
            mail.send_mail(
                sender=const.mail_from,
                to=const.mail_submit_to,
                subject=const.mail_submit_subject,
                body=template.render(path, {'project': p}))

            self.list({"submitMsg": "Your project has been submitted."})

    def list(self, additional_data={}):
        cache_key = 'view-project-data'
        tag = self.request.get('tag', None)
        if tag:
            cache_key += '-'+tag
        data = memcache.get(cache_key)
        admin = users.is_current_user_admin()
        if admin or not data:
            projects = Project.all().order('category').order('name')
            if not admin:
                projects = projects.filter('approved =', True)
            projects = list(projects)

            tags = sets.Set()
            for p in projects:
                for t in p.tags:
                    tags.add(t)

            if tag:
                projects = filter(lambda x: tag in x.tags, projects)

            data = {}
            data['tag'] = tag
            data['tags'] = tags
            data['projects'] = projects
            data['admin']= admin
            if not admin:
                memcache.set(cache_key, data, time=CacheTimeout)

        for k, v in additional_data.items():
            data[k] = v

        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        path = os.path.join(os.path.dirname(__file__), 'project.html')
        self.response.out.write(template.render(path, data))

    def edit(self, save=False):
        if save:
            name = self.request.get("orig_name")
        else:
            name = self.request.get("name")

        p = Project.get_by_key_name("proj-"+name)
        if not p:
            self.response.out.write("Couldn't find that Project.")
            return

        if save:
            if self.request.get("do") == "Delete":
                p.delete()
            else:
                pkg_name = self.request.get("package", None)
                if pkg_name:
                    pkg = Package.get_by_key_name("pkg-"+pkg_name)
                    if pkg:
                        p.package = pkg.key()
                for f in ['name', 'descr', 'web_url', 'category']:
                    setattr(p, f, self.request.get(f, None))
                p.approved = self.request.get("approved") == "1"
                p.tags = filter(lambda x: x, self.request.get("tags", "").split(","))
                p.put()
            memcache.delete('view-project-data')
            self.redirect('/project')
            return

        # get all project categories and tags
        cats, tags = sets.Set(), sets.Set()
        for r in Project.all():
            cats.add(r.category)
            for t in r.tags:
                tags.add(t)

        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        path = os.path.join(os.path.dirname(__file__), 'project-edit.html')
        self.response.out.write(template.render(path, {
            "taglist": tags, "catlist": cats, "p": p, "tags": ",".join(p.tags) }))

    def redirect(self, url):
        self.response.set_status(302)
        self.response.headers.add_header("Location", url)

def main():
    app = webapp.WSGIApplication([
        ('/package', PackagePage),
        ('/project.*', ProjectPage),
        ], debug=True)
    run_wsgi_app(app)

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