<commit_before>from __future__ import absolute_import, unicode_literals

from django.contrib.auth.decorators import login_required
from django.contrib.gis.geos import GEOSGeometry, Point
from django.core.urlresolvers import reverse_lazy
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView
from json import loads, dumps

from .forms import IncidentForm
from .models import Incident, AreaOfInterest

class LoginRequiredMixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        return login_required(view)

class IncidentListOnSuccessMixin(object):
    def get_success_url(self):
        return reverse_lazy('tracker:incident-list')

class IncidentListView(LoginRequiredMixin, ListView):
    queryset = Incident.objects.all().order_by('-created')
incident_list = IncidentListView.as_view()

class IncidentDetailView(LoginRequiredMixin, DetailView):
    model = Incident
incident_detail = IncidentDetailView.as_view()

class IncidentCreateView(LoginRequiredMixin, IncidentListOnSuccessMixin, CreateView):
    model = Incident
    form_class = IncidentForm

    def get_initial(self):
        initial = super(IncidentCreateView, self).get_initial()

        initial['location_geojson'] = Point(0, 0).geojson

        return initial

    def form_valid(self, form):
        form.instance.location = GEOSGeometry(form.cleaned_data['location_geojson'])

        return super(IncidentCreateView, self).form_valid(form)
incident_create = IncidentCreateView.as_view()

class IncidentUpdateView(LoginRequiredMixin, IncidentListOnSuccessMixin, UpdateView):
    model = Incident
    form_class = IncidentForm

    def get_initial(self):
        initial = super(IncidentUpdateView, self).get_initial()

        initial['location_geojson'] = self.get_object().location.geojson

        return initial

    def form_valid(self, form):
        form.instance.location = GEOSGeometry(form.cleaned_data['location_geojson'])

        return super(IncidentUpdateView, self).form_valid(form)
incident_update = IncidentUpdateView.as_view()

class IncidentDeleteView(LoginRequiredMixin, IncidentListOnSuccessMixin, DeleteView):
    model = Incident
incident_delete = IncidentDeleteView.as_view()

class AreaOfInterestListView(LoginRequiredMixin, ListView):
    model = AreaOfInterest
area_of_interest_list = AreaOfInterestListView.as_view()

class AreaOfInterestDetailView(LoginRequiredMixin, DetailView):
    model = AreaOfInterest
area_of_interest_detail = AreaOfInterestDetailView.as_view()

class AreaOfInterestCreateView(LoginRequiredMixin, CreateView):
    model = Incident
area_of_interest_create = AreaOfInterestCreateView.as_view()

class AreaOfInterestUpdateView(LoginRequiredMixin, UpdateView):
    model = Incident
area_of_interest_update = AreaOfInterestUpdateView.as_view()

class AreaOfInterestDeleteView(LoginRequiredMixin, DeleteView):
    model = Incident
area_of_interest_delete = AreaOfInterestDeleteView.as_view()<commit_msg>Fix areas of interest views model<commit_after>from __future__ import absolute_import, unicode_literals

from django.contrib.auth.decorators import login_required
from django.contrib.gis.geos import GEOSGeometry, Point
from django.core.urlresolvers import reverse_lazy
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView
from json import loads, dumps

from .forms import IncidentForm
from .models import Incident, AreaOfInterest

class LoginRequiredMixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        return login_required(view)

class IncidentListOnSuccessMixin(object):
    def get_success_url(self):
        return reverse_lazy('tracker:incident-list')

class IncidentListView(LoginRequiredMixin, ListView):
    queryset = Incident.objects.all().order_by('-created')
incident_list = IncidentListView.as_view()

class IncidentDetailView(LoginRequiredMixin, DetailView):
    model = Incident
incident_detail = IncidentDetailView.as_view()

class IncidentCreateView(LoginRequiredMixin, IncidentListOnSuccessMixin, CreateView):
    model = Incident
    form_class = IncidentForm

    def get_initial(self):
        initial = super(IncidentCreateView, self).get_initial()

        initial['location_geojson'] = Point(0, 0).geojson

        return initial

    def form_valid(self, form):
        form.instance.location = GEOSGeometry(form.cleaned_data['location_geojson'])

        return super(IncidentCreateView, self).form_valid(form)
incident_create = IncidentCreateView.as_view()

class IncidentUpdateView(LoginRequiredMixin, IncidentListOnSuccessMixin, UpdateView):
    model = Incident
    form_class = IncidentForm

    def get_initial(self):
        initial = super(IncidentUpdateView, self).get_initial()

        initial['location_geojson'] = self.get_object().location.geojson

        return initial

    def form_valid(self, form):
        form.instance.location = GEOSGeometry(form.cleaned_data['location_geojson'])

        return super(IncidentUpdateView, self).form_valid(form)
incident_update = IncidentUpdateView.as_view()

class IncidentDeleteView(LoginRequiredMixin, IncidentListOnSuccessMixin, DeleteView):
    model = Incident
incident_delete = IncidentDeleteView.as_view()

class AreaOfInterestListView(LoginRequiredMixin, ListView):
    model = AreaOfInterest
area_of_interest_list = AreaOfInterestListView.as_view()

class AreaOfInterestDetailView(LoginRequiredMixin, DetailView):
    model = AreaOfInterest
area_of_interest_detail = AreaOfInterestDetailView.as_view()

class AreaOfInterestCreateView(LoginRequiredMixin, CreateView):
    model = AreaOfInterest
area_of_interest_create = AreaOfInterestCreateView.as_view()

class AreaOfInterestUpdateView(LoginRequiredMixin, UpdateView):
    model = AreaOfInterest
area_of_interest_update = AreaOfInterestUpdateView.as_view()

class AreaOfInterestDeleteView(LoginRequiredMixin, DeleteView):
    model = AreaOfInterest
area_of_interest_delete = AreaOfInterestDeleteView.as_view()<|endoftext|>


<commit_before># coding:utf-8
'''
Created on 2017/12/6

@author: sunyihuan
'''
from __future__ import print_function

import tensorflow as tf
from tensorflow.contrib.factorization import KMeans

# Ignore all GPUs, tf random forest does not benefit from it.
import os
import scipy.io as scio
import numpy as np
from sklearn.model_selection import train_test_split

os.environ["CUDA_VISIBLE_DEVICES"] = ""

def data_check(data):
    res = list(np.argmax(data.T, 1))
    num = len(res)
    classes = data.shape[0]
    for i in range(classes):
        print(str(i) + '的比例', round(100.0 * res.count(i) / num, 2), '%')
    print('<------------------分割线---------------------->')

def main(Xtr, Ytr, Xte, Yte):
    X = tf.placeholder(tf.float32, shape=[None, num_features])
    Y = tf.placeholder(tf.float32, shape=[None, num_classes])

    kmeans = KMeans(inputs=X, num_clusters=k,
                    distance_metric='cosine',
                    use_mini_batch=True)

    res = kmeans.training_graph()
    (all_scores, cluster_idx, scores, cluster_centers_initialized, init_op,
     train_op) = kmeans.training_graph()
    cluster_idx = cluster_idx[0]  # fix for cluster_idx being a tuple
    avg_distance = tf.reduce_mean(scores)
    tf.summary.scalar(name='avg_distance', tensor=avg_distance)
    tf.summary.histogram('cluster_idx', cluster_idx)
    merge_all_op = tf.summary.merge_all()

    # Initialize the variables (i.e. assign their default value)
    init_vars = tf.global_variables_initializer()

    # Start TensorFlow session
    sess = tf.Session()
    write = tf.summary.FileWriter('logdir/KMeans', sess.graph)

    # Run the initializer
    sess.run(init_vars, feed_dict={X: Xtr})
    sess.run(init_op, feed_dict={X: Xtr})

    # Training
    for i in range(1, num_steps + 1):
        summary_write, _, d, idx = sess.run(
            [merge_all_op, train_op, avg_distance, cluster_idx],
            feed_dict={X: Xtr})
        if i % 10 == 0 or i == 1:
            write.add_summary(summary_write, i)
            print("Step %i, Avg Distance: %f" % (i, d))

    # Assign a label to each centroid
    # Count total number of labels per centroid, using the label of each training
    # sample to their closest centroid (given by 'idx')
    counts = np.zeros(shape=(k, num_classes))
    for i in range(len(idx)):
        counts[idx[i]] += Ytr[i]
    # Assign the most frequent label to the centroid
    labels_map = [np.argmax(c) for c in counts]
    labels_map = tf.convert_to_tensor(labels_map)

    # Evaluation ops
    # Lookup: centroid_id -> label
    cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx)
    # Compute accuracy
    correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32))
    # print(correct_prediction)
    accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # Test Model
    test_x, test_y = Xte, Yte
    Y, cluster_label, correct_prediction, accuracy_op = sess.run(
        [tf.argmax(Y, 1), cluster_label, correct_prediction, accuracy_op],
        feed_dict={X: test_x, Y: test_y})
    print("Test Accuracy:", accuracy_op)
    # print('correct_prediction', correct_prediction)

    # 计算有多少小判断成大
    c = 0
    for i in range(len(correct_prediction)):
        if correct_prediction[i] == False:
            if abs(cluster_label[i] - Y[i]) > 1:
                c += 1
    print(round(100 * (c / test_y.shape[0]), 2), "%")

num_steps = 30  # Total steps to train
batch_size = 1024  # The number of samples per batch
k = 22  # The number of clusters
num_classes = 9  # The 10 digits
num_features = 128 * 128  # Each image is 28x28 pixels

if __name__ == '__main__':

    name = 'Syh'
    if name == 'Dxq':
        file = 'F:/dataSets/FaceChannel1/face_1_channel_XY'
    elif name == 'Syh':
        file = 'E:/deeplearning_Data/face_1_channel_XY'

    data_train = scio.loadmat(file)
    X_train, X_test, Y_train, Y_test = train_test_split(data_train['X'], data_train['Y'], train_size=0.8)
    X_train = X_train / 255.
    X_test = X_test / 255.

    # Parameters
    main(X_train.reshape(-1,128*128), Y_train, X_test.reshape(-1,128*128), Y_test)
<commit_msg>read_point<commit_after># coding:utf-8
'''
Created on 2017/12/6

@author: sunyihuan
'''
from __future__ import print_function

import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.contrib.factorization import KMeans

# Ignore all GPUs, tf random forest does not benefit from it.
import os
from practice_one.model.utils import *

os.environ["CUDA_VISIBLE_DEVICES"] = ""

def preprocessing(trX, teX, trY, teY):
    return trX / 255., teX / 255., trY, teY

def create_placeholders(n_x, n_y):
    X = tf.placeholder(name='X', shape=(None, n_x), dtype=tf.float32)
    Y = tf.placeholder(name='Y', shape=(None, n_y), dtype=tf.float32)

    return X, Y

def main(Xtr, Ytr, Xte, Yte):
    ops.reset_default_graph()
    m, n_x = X_train.shape
    n_y = Y_train.shape[1]

    X, Y = create_placeholders(n_x, n_y)

    kmeans = KMeans(inputs=X, num_clusters=k,
                    distance_metric='cosine',
                    use_mini_batch=True)

    kmeans.training_graph()
    (all_scores, cluster_idx, scores, cluster_centers_initialized, cluster_centers_var, init_op,
     train_op) = kmeans.training_graph()
    cluster_idx = cluster_idx[0]  # fix for cluster_idx being a tuple
    avg_distance = tf.reduce_mean(scores)
    tf.summary.scalar(name='avg_distance', tensor=avg_distance)
    tf.summary.histogram('cluster_idx', cluster_idx)
    merge_all_op = tf.summary.merge_all()

    # Initialize the variables (i.e. assign their default value)
    init_vars = tf.global_variables_initializer()

    # Start TensorFlow session
    sess = tf.Session()
    write = tf.summary.FileWriter('logdir/KMeans', sess.graph)

    # Run the initializer
    sess.run(init_vars, feed_dict={X: Xtr})
    sess.run(init_op, feed_dict={X: Xtr})

    # Training
    for i in range(1, num_steps + 1):
        summary_write, _, d, idx = sess.run(
            [merge_all_op, train_op, avg_distance, cluster_idx],
            feed_dict={X: Xtr})
        if i % 10 == 0 or i == 1:
            write.add_summary(summary_write, i)
            print("Step %i, Avg Distance: %f" % (i, d))

    # Assign a label to each centroid
    # Count total number of labels per centroid, using the label of each training
    # sample to their closest centroid (given by 'idx')
    counts = np.zeros(shape=(k, num_classes))
    for i in range(len(idx)):
        counts[idx[i]] += Ytr[i]
    # Assign the most frequent label to the centroid
    labels_map = [np.argmax(c) for c in counts]
    labels_map = tf.convert_to_tensor(labels_map)

    # Evaluation ops
    # Lookup: centroid_id -> label
    cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx)
    # Compute accuracy
    correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32))
    # print(correct_prediction)
    accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # Test Model
    test_x, test_y = Xte, Yte
    Y, cluster_label, correct_prediction, accuracy_op = sess.run(
        [tf.argmax(Y, 1), cluster_label, correct_prediction, accuracy_op],
        feed_dict={X: test_x, Y: test_y})
    print("Test Accuracy:", accuracy_op)
    # print('correct_prediction', correct_prediction)

    # 计算有多少小判断成大
    c = 0
    for i in range(len(correct_prediction)):
        if correct_prediction[i] == False:
            if abs(cluster_label[i] - Y[i]) > 1:
                c += 1
    print(round(100 * (c / test_y.shape[0]), 2), "%")

if __name__ == '__main__':
    num_steps = 30  # Total steps to train
    batch_size = 1024  # The number of samples per batch
    k = 22  # The number of clusters
    num_classes = 9  # The 10 digits

    name = 'Dxq'
    if name == 'Dxq':
        file = 'F:/dataSets/FaceChannel1/face_1_channel_XY64'
    elif name == 'Syh':
        file = 'E:/deeplearning_Data/face_1_channel_XY'

    X_train, X_test, Y_train, Y_test = load_data(file, test_size=0.2)

    X_train, X_test, Y_train, Y_test = preprocessing(X_train, X_test, Y_train, Y_test)
    # Parameters
    main(X_train, Y_train, X_test, Y_test)
<|endoftext|>


<commit_before>import glob
import namedtuple
import os
import sys
import re
from xml.dom import minidom

mcpattern = re.compile("^group(?P<year>[0-9]+).perf-tau.mc(?P<prodyear>[0-9]+)_(?P<energy>[0-9]+)TeV.(?P<run>[0-9]+).(?P<name>).(?P<tag>[^.]+).(?P<suffix>.+)$")
datapattern = re.compile("^group(?P<year>[0-9]+).(?P<group>[^.]+).(?P<run>[0-9]+).(?P<stream>[^.]+).(?P<tag>[^.]+).(?P<version>[0-9\-]+).D3PD(?:.(?P<edition>[0-9]+))?_StreamD3PD_Tau(?P<size>SMALL$|MEDIUM$)")

Dataset = namedtuple.namedtuple( 'Dataset', 'name datatype classtype treename weight files' )

classes = {
    'BACKGROUND' :0,
    'SIGNAL'     :1
}

types = {
    'DATA' :0,
    'MC'   :1
}

data_periods = {
    'AB' :xrange(152166,155161),
    'C'  :xrange(155228,156683),
    'D'  :xrange(158045,159225),
    'E'  :xrange(160387,161949),
    'F'  :xrange(162347,162883),
    'G'  :xrange(165591,166384),
    'H'  :xrange(166466,166965)
}

if not os.environ.has_key('DATAROOT'):
    sys.exit("DATAROOT not defined!")
dataroot = os.environ['DATAROOT']

def get_sample(name, periods=None):

    base = os.path.join(dataroot,name)
    if not os.path.isdir(base):
        print "Sample %s not found at %s"%(name,base)
        return None
    metafile = os.path.join(base,'meta.xml')
    if not os.path.isfile(metafile):
        print "Metadata %s not found!"%metafile
        return None
    try:
        metafile = open(metafile,'r')
        doc = minidom.parse(metafile)
        metafile.close()
        meta = doc.getElementsByTagName("meta")
        datatype = meta[0].getElementsByTagName("type")[0].childNodes[0].nodeValue.upper()
        classname = meta[0].getElementsByTagName("class")[0].childNodes[0].nodeValue.upper()
        weight = float(meta[0].getElementsByTagName("weight")[0].childNodes[0].nodeValue)
        treename = str(meta[0].getElementsByTagName("tree")[0].childNodes[0].nodeValue)
    except:
        print "Could not parse metadata!"
        return None
    if not classes.has_key(classname):
        print "Class %s is not defined!"%classname
        if len(classes) > 0:
            print "Use one of these:"
            for key in classes.keys():
                print key
        else:
            print "No classes have been defined!"
        return None
    classtype = classes[classname]
    if not types.has_key(datatype):
        print "Datatype %s is not defined!"%datatype
        if len(types) > 0:
            print "Use one of these:"
            for key in types.keys():
                print key
        else:
            print "No datatypes have been defined!"
    datatype = types[datatype]
    dirs = glob.glob(os.path.join(base,'*'))
    actualdirs = []
    for dir in dirs:
        if os.path.isdir(dir):
            actualdirs.append(dir)
    files = []
    if datatype == types['DATA']:
        # check for duplicate runs and take last edition
        runs = {}
        for dir in actualdirs:
            datasetname = os.path.basename(dir)
            match = re.match(datapattern,datasetname)
            if not match:
                print "Warning: directory %s is not a valid dataset name!"%datasetname
            else:
                runnumber = int(match.group('run'))
                if periods != None:
                    isinperiod = False
                    for period in periods:
                        if not data_periods.has_key(period):
                            print "Period %s is not defined!"%period
                            return None
                        if runnumber in data_periods[period]:
                            isinperiod = True
                            break
                    if not isinperiod:
                        continue
                edition = 0
                if match.group('edition'):
                    edition = int(match.group('edition'))
                if runs.has_key(runnumber):
                    print "Warning: multiple editions of dataset %s exist!"%datasetname
                    if edition > runs[runnumber]['edition']:
                        runs[runnumber] = {'edition':edition, 'dir':dir}
                else:
                    runs[runnumber] = {'edition':edition, 'dir':dir}
        for key,value in runs.items():
            files += glob.glob(os.path.join(value['dir'],'*root*'))
    else:
        for dir in actualdirs:
            files += glob.glob(os.path.join(dir,'*root*'))
    samplename = name
    if periods:
        samplename += "_%s"%("".join(periods))
    return Dataset(samplename,datatype,classtype,treename,weight,files)

"""
dataset = {}

#_______________________________________________________
DatasetTuple = namedtuple.namedtuple( 'DatasetTuple', 'runnumber tag generator treename weight files' )

dataset["data"]=[DatasetTuple( 111, "Data", "HCP2010", "tauPerf", 1.0, glob.glob(os.path.join(os.environ["data"],"DPD/data","group10.perf-tau.*.L1Calo-DESD_MET.*.00-06-00-02*TauMEDIUM/*root*")))]
dataset["data_small"]=[DatasetTuple( 111, "Data_small", "HCP2010", "tauPerf", 1.0, glob.glob(os.path.join(os.environ["data"],"DPD/data/small","group10.perf-tau.*.L1Calo-DESD_MET.*.00-06-00-02*TauSMALL/*root*")))]

base = os.path.join(os.environ["data"],"DPD/mc")

dataset["Ztautau"] = [
DatasetTuple( 106052, "Ztautau", "PythiaZtautau", "tauPerfSmall", 1.0, glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.106052.PythiaZtautau.AOD.e468_s765_s767_r1302_r1306.00-06-00-03.D3PD.*StreamD3PD_TauSMALL/*root*")))
]

##### DW TUNE ####################
# cross section 7.7745E+06, 361989
dataset["PythiaDW"] = [
DatasetTuple( 115859, "DWJ0", "PythiaDW", "tauPerfSmall", 21.47717, glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.115859.J0_pythia_DW.e570_s766_s767_r1303_tid150952_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 5.0477E+05, 332993
DatasetTuple( 115860, "DWJ1", "PythiaDW", "tauPerfSmall", 1.51585769 , glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.115860.J1_pythia_DW.e570_s766_s767_r1303_tid150951_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 2.9366E+04, 370993
DatasetTuple( 115861, "DWJ2", "PythiaDW", "tauPerfSmall", 0.079155132, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115861.J2_pythia_DW.e570_s766_s767_r1303_tid150950_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.5600E+03, 392997
DatasetTuple( 115862, "DWJ3", "PythiaDW", "tauPerfSmall", 0.003969496, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115862.J3_pythia_DW.e570_s766_s767_r1303_tid150949_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 6.4517E+01, 397986
DatasetTuple( 115863, "DWJ4", "PythiaDW", "tauPerfSmall", 0.000162109, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115863.J4_pythia_DW.e570_s766_s767_r1303_tid150948_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*")))
]
############################################
##### PERUGIA 2010 TUNE ####################
dataset["Perugia2010"] = [
# cross section 7.7788E+06, 398244
DatasetTuple( 115849, "PerugiaJ0", "PythiaP10", "tauPerfSmall", 19.5327, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115849.J0_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 5.0385E+05, 397942
DatasetTuple( 115850, "PerugiaJ1", "PythiaP10", "tauPerfSmall", 1.266139, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115850.J1_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 2.9353E+04, 397856
DatasetTuple( 115851, "PerugiaJ2", "PythiaP10", "tauPerfSmall", 0.07377795 , glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115851.J2_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.5608E+03, 398393
DatasetTuple( 115852, "PerugiaJ3", "PythiaP10", "tauPerfSmall", 0.00391774 , glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115852.J3_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.8760E+00, 397195
DatasetTuple( 115853, "PerugiaJ4", "PythiaP10", "tauPerfSmall", 0.000004723, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115853.J4_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*")))
]

#_______________________________________________________
def getNumEvents( dataset ):
    fChain = ROOT.TChain( "tauPerf" )
    for f in dataset.files:
        fChain.Add( f )
    return fChain.GetEntries()

#_______________________________________________________
def getWeight( dataset ):
    # if the dataset is unwighted, always return 1.0
    # otherwise, take cross section/#events
    if dataset.sigma==1.0:
        return dataset.sigma
    else:
        return (dataset.sigma) / getNumEvents( dataset )
"""
<commit_msg>updates<commit_after>import glob
import namedtuple
import os
import sys
import re
from xml.dom import minidom

mcpattern = re.compile("^group(?P<year>[0-9]+).perf-tau.mc(?P<prodyear>[0-9]+)_(?P<energy>[0-9]+)TeV.(?P<run>[0-9]+).(?P<name>).(?P<tag>[^.]+).(?P<suffix>.+)$")
datapattern = re.compile("^group(?P<year>[0-9]+).(?P<group>[^.]+).(?P<run>[0-9]+).(?P<stream>[^.]+).(?P<tag>[^.]+).(?P<version>[0-9\-]+).D3PD(?:.(?P<edition>[0-9]+))?_StreamD3PD_Tau(?P<size>SMALL$|MEDIUM$)")

Dataset = namedtuple.namedtuple( 'Dataset', 'name datatype classtype treename weight files' )

classes = {
    'BACKGROUND' :0,
    'SIGNAL'     :1
}

types = {
    'DATA' :0,
    'MC'   :1
}

data_periods = {
    'AB' :xrange(152166,155161),
    'C'  :xrange(155228,156683),
    'D'  :xrange(158045,159225),
    'E'  :xrange(160387,161949),
    'F'  :xrange(162347,162883),
    'G'  :xrange(165591,166384),
    'H'  :xrange(166466,166965)
}

if not os.environ.has_key('DATAROOT'):
    sys.exit("DATAROOT not defined!")
dataroot = os.environ['DATAROOT']

def get_sample(name, periods=None):

    base = os.path.join(dataroot,name)
    if not os.path.isdir(base):
        print "Sample %s not found at %s"%(name,base)
        return None
    metafile = os.path.join(base,'meta.xml')
    if not os.path.isfile(metafile):
        print "Metadata %s not found!"%metafile
        return None
    try:
        metafile = open(metafile,'r')
        doc = minidom.parse(metafile)
        metafile.close()
        meta = doc.getElementsByTagName("meta")
        datatype = meta[0].getElementsByTagName("type")[0].childNodes[0].nodeValue.upper()
        classname = meta[0].getElementsByTagName("class")[0].childNodes[0].nodeValue.upper()
        weight = float(eval(str(meta[0].getElementsByTagName("weight")[0].childNodes[0].nodeValue)))
        treename = str(meta[0].getElementsByTagName("tree")[0].childNodes[0].nodeValue)
    except:
        print "Could not parse metadata!"
        return None
    if not classes.has_key(classname):
        print "Class %s is not defined!"%classname
        if len(classes) > 0:
            print "Use one of these:"
            for key in classes.keys():
                print key
        else:
            print "No classes have been defined!"
        return None
    classtype = classes[classname]
    if not types.has_key(datatype):
        print "Datatype %s is not defined!"%datatype
        if len(types) > 0:
            print "Use one of these:"
            for key in types.keys():
                print key
        else:
            print "No datatypes have been defined!"
    datatype = types[datatype]
    dirs = glob.glob(os.path.join(base,'*'))
    actualdirs = []
    for dir in dirs:
        if os.path.isdir(dir):
            actualdirs.append(dir)
    files = []
    if datatype == types['DATA']:
        # check for duplicate runs and take last edition
        runs = {}
        for dir in actualdirs:
            datasetname = os.path.basename(dir)
            match = re.match(datapattern,datasetname)
            if not match:
                print "Warning: directory %s is not a valid dataset name!"%datasetname
            else:
                runnumber = int(match.group('run'))
                if periods != None:
                    isinperiod = False
                    for period in periods:
                        if not data_periods.has_key(period):
                            print "Period %s is not defined!"%period
                            return None
                        if runnumber in data_periods[period]:
                            isinperiod = True
                            break
                    if not isinperiod:
                        continue
                edition = 0
                if match.group('edition'):
                    edition = int(match.group('edition'))
                if runs.has_key(runnumber):
                    print "Warning: multiple editions of dataset %s exist!"%datasetname
                    if edition > runs[runnumber]['edition']:
                        runs[runnumber] = {'edition':edition, 'dir':dir}
                else:
                    runs[runnumber] = {'edition':edition, 'dir':dir}
        for key,value in runs.items():
            files += glob.glob(os.path.join(value['dir'],'*root*'))
    else:
        for dir in actualdirs:
            files += glob.glob(os.path.join(dir,'*root*'))
    samplename = name
    if periods:
        samplename += "_%s"%("".join(periods))
    return Dataset(samplename,datatype,classtype,treename,weight,files)

"""
dataset = {}

#_______________________________________________________
DatasetTuple = namedtuple.namedtuple( 'DatasetTuple', 'runnumber tag generator treename weight files' )

dataset["data"]=[DatasetTuple( 111, "Data", "HCP2010", "tauPerf", 1.0, glob.glob(os.path.join(os.environ["data"],"DPD/data","group10.perf-tau.*.L1Calo-DESD_MET.*.00-06-00-02*TauMEDIUM/*root*")))]
dataset["data_small"]=[DatasetTuple( 111, "Data_small", "HCP2010", "tauPerf", 1.0, glob.glob(os.path.join(os.environ["data"],"DPD/data/small","group10.perf-tau.*.L1Calo-DESD_MET.*.00-06-00-02*TauSMALL/*root*")))]

base = os.path.join(os.environ["data"],"DPD/mc")

dataset["Ztautau"] = [
DatasetTuple( 106052, "Ztautau", "PythiaZtautau", "tauPerfSmall", 1.0, glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.106052.PythiaZtautau.AOD.e468_s765_s767_r1302_r1306.00-06-00-03.D3PD.*StreamD3PD_TauSMALL/*root*")))
]

##### DW TUNE ####################
# cross section 7.7745E+06, 361989
dataset["PythiaDW"] = [
DatasetTuple( 115859, "DWJ0", "PythiaDW", "tauPerfSmall", 21.47717, glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.115859.J0_pythia_DW.e570_s766_s767_r1303_tid150952_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 5.0477E+05, 332993
DatasetTuple( 115860, "DWJ1", "PythiaDW", "tauPerfSmall", 1.51585769 , glob.glob( os.path.join(base,"group10.perf-tau.mc09_7TeV.115860.J1_pythia_DW.e570_s766_s767_r1303_tid150951_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 2.9366E+04, 370993
DatasetTuple( 115861, "DWJ2", "PythiaDW", "tauPerfSmall", 0.079155132, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115861.J2_pythia_DW.e570_s766_s767_r1303_tid150950_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.5600E+03, 392997
DatasetTuple( 115862, "DWJ3", "PythiaDW", "tauPerfSmall", 0.003969496, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115862.J3_pythia_DW.e570_s766_s767_r1303_tid150949_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 6.4517E+01, 397986
DatasetTuple( 115863, "DWJ4", "PythiaDW", "tauPerfSmall", 0.000162109, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115863.J4_pythia_DW.e570_s766_s767_r1303_tid150948_00.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*")))
]
############################################
##### PERUGIA 2010 TUNE ####################
dataset["Perugia2010"] = [
# cross section 7.7788E+06, 398244
DatasetTuple( 115849, "PerugiaJ0", "PythiaP10", "tauPerfSmall", 19.5327, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115849.J0_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 5.0385E+05, 397942
DatasetTuple( 115850, "PerugiaJ1", "PythiaP10", "tauPerfSmall", 1.266139, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115850.J1_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 2.9353E+04, 397856
DatasetTuple( 115851, "PerugiaJ2", "PythiaP10", "tauPerfSmall", 0.07377795 , glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115851.J2_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.5608E+03, 398393
DatasetTuple( 115852, "PerugiaJ3", "PythiaP10", "tauPerfSmall", 0.00391774 , glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115852.J3_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*"))),
# cross section 1.8760E+00, 397195
DatasetTuple( 115853, "PerugiaJ4", "PythiaP10", "tauPerfSmall", 0.000004723, glob.glob( os.path.join(base, "group10.perf-tau.mc09_7TeV.115853.J4_pythia_Perugia2010.e568_s766_s767_r1303.00-06-00-02.D3PD_StreamD3PD_TauSMALL/*root*")))
]

#_______________________________________________________
def getNumEvents( dataset ):
    fChain = ROOT.TChain( "tauPerf" )
    for f in dataset.files:
        fChain.Add( f )
    return fChain.GetEntries()

#_______________________________________________________
def getWeight( dataset ):
    # if the dataset is unwighted, always return 1.0
    # otherwise, take cross section/#events
    if dataset.sigma==1.0:
        return dataset.sigma
    else:
        return (dataset.sigma) / getNumEvents( dataset )
"""
<|endoftext|>


<commit_before>from collections import OrderedDict
import datetime
import json

__author__ = 'rjs'

class Visitor:
    def visit(self, node, *args, **kwargs):
        method = None
        for cls in node.__class__.__mro__:
            method_name = 'visit_' + cls.__name__
            method = getattr(self, method_name, None)
            if method:
                break

        if not method:
            method = self.generic_visit
        return method(node, *args, **kwargs)

    def generic_visit(self, node, *args, **kwargs):
        raise AttributeError("No visit_" + __class__.__name__ + "method.")

class DecimalSerializableFloat(float):
    """A sublcass of float which can store a decimal.

    This horrible little class is used only to trick the JSON serializer
    to render decimals as numbers without losing precision via float.
    """
    def __init__(self, value):
        self._value = value

    def __repr__(self):
        return str(self._value)

class GpxJsonEncoder(json.JSONEncoder, Visitor):

    def default(self, obj):
        return self.visit(obj)

    def generic_visit(self, obj, *args, **kwargs):
        return obj

    def optional_attribute_scalar(self, data, obj, name):
        value = getattr(obj, name)
        if value is not None:
            data[name] = self.visit(value)

    def optional_attribute_list(self, data, obj, name):
        values = getattr(obj, name)
        if values:
            data[name] = [ self.visit(item) for item in values ]

    def visit_GpxModel(self, gpx_model, *args, **kwargs):
        """Render a GPXModel as a single JSON structure."""
        result = OrderedDict()

        put_scalar = lambda name: self.optional_attribute_scalar(result, gpx_model, name)
        put_list = lambda name: self.optional_attribute_list(result, gpx_model, name)

        put_scalar('creator')
        put_scalar('metadata')
        put_list('waypoints')
        put_list('routes')
        put_list('tracks')
        put_list('extensions')

        return result

    def visit_Metadata(self, metadata, *args, **kwargs):
        """Render GPX Metadata as a single JSON structure."""
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, metadata, name)
        put_list = lambda name: self.optional_attribute_list(result, metadata, name)

        put_scalar('name')
        put_scalar('description')
        put_scalar('author')
        put_scalar('copyright')
        put_list('links')
        put_scalar('time')
        put_scalar('keywords')
        put_scalar('bounds')
        put_list('extensions')

        return result

    def visit_Person(self, person, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, person, name)

        put_scalar('name')
        put_scalar('email')
        put_scalar('link')

        return result

    def visit_Copyright(self, copyright, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, copyright, name)

        put_scalar('author')
        put_scalar('year')
        put_scalar('license')

        return result

    def visit_Year(self, year, *args, **kwargs):
        dt = datetime.datetime(year=year.year, month=1, day=1, tzinfo=year.tzinfo)
        result = dt.strftime("%Y%z")
        return result

    def visit_Link(self, link, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, link, name)

        put_scalar('href')
        put_scalar('text')
        put_scalar('mime')

        return result

    def visit_datetime(self, dt, *args, **kwargs):
        return dt.isoformat()

    def visit_Bounds(self, bounds, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, bounds, name)

        put_scalar('minimum_latitude')
        put_scalar('minimum_longitude')
        put_scalar('maximum_latitude')
        put_scalar('maximum_longitude')

        return result

    def visit_Decimal(self, d, *args, **kwargs):
        return DecimalSerializableFloat(d)

    def visit_Waypoint(self, waypoint, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, waypoint, name)
        put_list = lambda name: self.optional_attribute_list(result, waypoint, name)

        put_scalar('latitude')
        put_scalar('longitude')
        put_scalar('elevation')
        put_scalar('time')
        put_scalar('magvar')
        put_scalar('geoid_height')
        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('symbol')
        put_scalar('classification')
        put_scalar('fix')
        put_scalar('num_satellites')
        put_scalar('hdop')
        put_scalar('vdop')
        put_scalar('pdop')
        put_scalar('seconds_since_dgps_update')
        put_scalar('speed')
        put_scalar('course')
        put_list('extensions')

        return result

    def visit_Route(self, route, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, route, name)
        put_list = lambda name: self.optional_attribute_list(result, route, name)

        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('number')
        put_scalar('classification')
        put_list('extensions')
        put_list('points')

        return result

    def visit_Track(self, track, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name: self.optional_attribute_scalar(result, track, name)
        put_list = lambda name: self.optional_attribute_list(result, track, name)

        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('number')
        put_scalar('classification')
        put_list('extensions')
        put_list('segments')

        return result

    def visit_Segment(self, segment, *args, **kwargs):
        result = OrderedDict()
        put_list = lambda name: self.optional_attribute_list(result, segment, name)

        put_list('points')
        put_list('extensions')

        return result

    def visit_Fix(self, fix):
        return str(fix)

def main():
    from trailer.readers.parser import parse_gpx
    with open('/Users/rjs/dev/trailer/data/culra.gpx', 'rb') as xml:
        gpx_model = parse_gpx(xml)
        json_data = json.dumps(gpx_model, cls=GpxJsonEncoder)
        return json_data

if __name__ == '__main__':
    gpx_model = main()
    pass<commit_msg>Adjustments to use shortened camelCase attribute names in the JSON output.<commit_after>from collections import OrderedDict
import datetime
import json

__author__ = 'rjs'

class Visitor:
    def visit(self, node, *args, **kwargs):
        method = None
        for cls in node.__class__.__mro__:
            method_name = 'visit_' + cls.__name__
            method = getattr(self, method_name, None)
            if method:
                break

        if not method:
            method = self.generic_visit
        return method(node, *args, **kwargs)

    def generic_visit(self, node, *args, **kwargs):
        raise AttributeError("No visit_" + __class__.__name__ + "method.")

class DecimalSerializableFloat(float):
    """A sublcass of float which can store a decimal.

    This horrible little class is used only to trick the JSON serializer
    to render decimals as numbers without losing precision via float.
    """
    def __init__(self, value):
        self._value = value

    def __repr__(self):
        return str(self._value)

class GpxJsonEncoder(json.JSONEncoder, Visitor):

    def default(self, obj):
        return self.visit(obj)

    def generic_visit(self, obj, *args, **kwargs):
        return obj

    def optional_attribute_scalar(self, data, obj, name, json_name=None):
        json_name = json_name or name
        value = getattr(obj, name)
        if value is not None:
            data[json_name] = self.visit(value)

    def optional_attribute_list(self, data, obj, name, json_name=None):
        json_name = json_name or name
        values = getattr(obj, name)
        if values:
            data[json_name] = [ self.visit(item) for item in values ]

    def visit_GpxModel(self, gpx_model, *args, **kwargs):
        """Render a GPXModel as a single JSON structure."""
        result = OrderedDict()

        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, gpx_model, name, json_name)
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, gpx_model, name, json_name)

        put_scalar('creator')
        put_scalar('metadata')
        put_list('waypoints')
        put_list('routes')
        put_list('tracks')
        put_list('extensions')

        return result

    def visit_Metadata(self, metadata, *args, **kwargs):
        """Render GPX Metadata as a single JSON structure."""
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, metadata, name, json_name)
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, metadata, name, json_name)

        put_scalar('name')
        put_scalar('description')
        put_scalar('author')
        put_scalar('copyright')
        put_list('links')
        put_scalar('time')
        put_scalar('keywords')
        put_scalar('bounds')
        put_list('extensions')

        return result

    def visit_Person(self, person, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, person, name, json_name)

        put_scalar('name')
        put_scalar('email')
        put_scalar('link')

        return result

    def visit_Copyright(self, copyright, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, copyright, name, json_name)

        put_scalar('author')
        put_scalar('year')
        put_scalar('license')

        return result

    def visit_Year(self, year, *args, **kwargs):
        dt = datetime.datetime(year=year.year, month=1, day=1, tzinfo=year.tzinfo)
        result = dt.strftime("%Y%z")
        return result

    def visit_Link(self, link, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, link, name, json_name)

        put_scalar('href')
        put_scalar('text')
        put_scalar('mime')

        return result

    def visit_datetime(self, dt, *args, **kwargs):
        return dt.isoformat()

    def visit_Bounds(self, bounds, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, bounds, name, json_name)

        put_scalar('minimum_latitude', 'minLat')
        put_scalar('minimum_longitude', 'minLon')
        put_scalar('maximum_latitude', 'maxLat')
        put_scalar('maximum_longitude', 'maxLon')

        return result

    def visit_Decimal(self, d, *args, **kwargs):
        return DecimalSerializableFloat(d)

    def visit_Waypoint(self, waypoint, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, waypoint, name, json_name)
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, waypoint, name, json_name)

        put_scalar('latitude', 'lat')
        put_scalar('longitude', 'lon')
        put_scalar('elevation', 'ele')
        put_scalar('time')
        put_scalar('magvar')
        put_scalar('geoid_height', 'geoidHeight')
        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('symbol')
        put_scalar('classification', 'type')
        put_scalar('fix')
        put_scalar('num_satellites', 'numSatellites')
        put_scalar('hdop')
        put_scalar('vdop')
        put_scalar('pdop')
        put_scalar('seconds_since_dgps_update', 'secondsSinceDgpsUpdate')
        put_scalar('speed')
        put_scalar('course')
        put_list('extensions')

        return result

    def visit_Route(self, route, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, route, name, json_name)
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, route, name, json_name)

        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('number')
        put_scalar('classification', 'type')
        put_list('extensions')
        put_list('points')

        return result

    def visit_Track(self, track, *args, **kwargs):
        result = OrderedDict()
        put_scalar = lambda name, json_name=None: self.optional_attribute_scalar(result, track, name, json_name)
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, track, name, json_name)

        put_scalar('name')
        put_scalar('comment')
        put_scalar('description')
        put_scalar('source')
        put_list('links')
        put_scalar('number')
        put_scalar('classification', 'type')
        put_list('extensions')
        put_list('segments')

        return result

    def visit_Segment(self, segment, *args, **kwargs):
        result = OrderedDict()
        put_list = lambda name, json_name=None: self.optional_attribute_list(result, segment, name, json_name)

        put_list('points')
        put_list('extensions')

        return result

    def visit_Fix(self, fix):
        return str(fix)

def main():
    from trailer.readers.parser import parse_gpx
    with open('/Users/rjs/dev/trailer/data/culra.gpx', 'rb') as xml:
        gpx_model = parse_gpx(xml)
        json_data = json.dumps(gpx_model, cls=GpxJsonEncoder, indent=4, separators=(', ', ': '))
        return json_data

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


<commit_before>#!/usr/bin/env python
# coding:utf-8
import os
import argparse

header_msg = {
    'py': '#!/usr/bin/env python\n# coding:utf-8\n',
    'c': '#include <stdio.h>\n',
    'scm': ';;;\n',
    'm': ''
}

def par():
    parser = argparse.ArgumentParser(
        description='A script for add script header message'
    )
    parser.add_argument('newfile')
    args = parser.parse_args()
    return args

def file_exists(file_path):
    return True if os.path.exists(file_path) else False

def main(args):
    file_path = os.getcwd() + '/' + args.newfile
    if file_exists(file_path):
        print('\t{0} already exists...'.format(args.newfile))
    else:
        newfile_attrs = args.newfile.split('.')
        with open(args.newfile, 'w') as f:
            if (len(newfile_attrs) == 2) and (newfile_attrs[-1] in header_msg):
                f.write(header_msg[newfile_attrs[-1]])

if __name__ == '__main__':
    args = par()
    main(args)
<commit_msg>fix atouch.py<commit_after>#!/usr/bin/env python
# coding:utf-8

import os
import argparse

header_msg = {
    'py': '#!/usr/bin/env python\n# coding:utf-8\n',
    'c': '#include <stdio.h>\n',
    'scm': ';;;\n',
    'm': ''
}

def par():
    parser = argparse.ArgumentParser(
        description='A script for add script header message'
    )
    parser.add_argument('newfile')
    args = parser.parse_args()
    return args

def file_exists(file_path):
    return True if os.path.exists(file_path) else False

def main(args):
    file_path = os.getcwd() + '/' + args.newfile
    if file_exists(file_path):
        print('\t{0} already exists...'.format(args.newfile))
    else:
        newfile_attrs = args.newfile.split('.')
        with open(args.newfile, 'w') as f:
            if (len(newfile_attrs) == 2) and (newfile_attrs[-1] in header_msg):
                f.write(header_msg[newfile_attrs[-1]])

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


<commit_before>from __future__ import unicode_literals

import contextlib
import os
import sys

from pre_commit.envcontext import envcontext
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
from pre_commit.xargs import xargs

ENVIRONMENT_DIR = 'node_env'
get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy

def get_env_patch(venv):  # pragma: windows no cover
    return (
        ('NODE_VIRTUAL_ENV', venv),
        ('NPM_CONFIG_PREFIX', venv),
        ('npm_config_prefix', venv),
        ('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
        ('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
    )

@contextlib.contextmanager
def in_env(repo_cmd_runner, language_version):  # pragma: windows no cover
    envdir = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, language_version),
    )
    with envcontext(get_env_patch(envdir)):
        yield

def install_environment(
        repo_cmd_runner, version, additional_dependencies,
):  # pragma: windows no cover
    additional_dependencies = tuple(additional_dependencies)
    assert repo_cmd_runner.exists('package.json')
    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)

    env_dir = repo_cmd_runner.path(directory)
    with clean_path_on_failure(env_dir):
        cmd = [
            sys.executable, '-m', 'nodeenv', '--prebuilt',
            '{{prefix}}{}'.format(directory),
        ]

        if version != 'default':
            cmd.extend(['-n', version])

        repo_cmd_runner.run(cmd)

        with in_env(repo_cmd_runner, version):
            helpers.run_setup_cmd(
                repo_cmd_runner,
                ('npm', 'install', '-g', '.') + additional_dependencies,
            )

def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs(helpers.to_cmd(hook), file_args)
<commit_msg>NodeJS hooks compatibilty fix for Cygwin<commit_after>from __future__ import unicode_literals

import contextlib
import os
import sys

from pre_commit.envcontext import envcontext
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.util import clean_path_on_failure
from pre_commit.xargs import xargs

ENVIRONMENT_DIR = 'node_env'
get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy

def get_env_patch(venv):  # pragma: windows no cover
    return (
        ('NODE_VIRTUAL_ENV', venv),
        ('NPM_CONFIG_PREFIX', os.path.join(venv, 'bin') if sys.platform == 'cygwin' else venv),
        ('npm_config_prefix', os.path.join(venv, 'bin') if sys.platform == 'cygwin' else venv),
        ('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
        ('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
    )

@contextlib.contextmanager
def in_env(repo_cmd_runner, language_version):  # pragma: windows no cover
    envdir = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, language_version),
    )
    with envcontext(get_env_patch(envdir)):
        yield

def install_environment(
        repo_cmd_runner, version, additional_dependencies,
):  # pragma: windows no cover
    additional_dependencies = tuple(additional_dependencies)
    assert repo_cmd_runner.exists('package.json')
    directory = helpers.environment_dir(ENVIRONMENT_DIR, version)

    env_dir = repo_cmd_runner.path(directory)
    with clean_path_on_failure(env_dir):
        cmd = [
            sys.executable, '-m', 'nodeenv', '--prebuilt',
            '{{prefix}}{}'.format(directory),
        ]

        if version != 'default':
            cmd.extend(['-n', version])

        repo_cmd_runner.run(cmd)

        with in_env(repo_cmd_runner, version):
            helpers.run_setup_cmd(
                repo_cmd_runner,
                ('npm', 'install', '-g', '.') + additional_dependencies,
            )

def run_hook(repo_cmd_runner, hook, file_args):  # pragma: windows no cover
    with in_env(repo_cmd_runner, hook['language_version']):
        return xargs(helpers.to_cmd(hook), file_args)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
__version__ = '0.0.8'

__author__ = 'Ludvig Wadenstein'

VERSION = __version__  # synonym
<commit_msg>Bumped version<commit_after># -*- coding: utf-8 -*-
__version__ = '0.0.9'

__author__ = 'Ludvig Wadenstein'

VERSION = __version__  # synonym
<|endoftext|>


<commit_before>from modules.basemodule import BaseModule

import random
import re
import time

# start with at least 10 charisma, max int and wis
# At level 1, train scholar, practice write
# At level 2, write book
# At level 3, gain engraving and embroidering, prac herbology
# At level 4, gain combat log
#(# At level 5, gain organizing)
# At level 7, gain find home
# At level 9, gain wilderness lore
# At level 11, gain book naming
# At level 13, gain find ship
# At level 14, gain make maps
# At level 16, gain plant lore
# At level 22, gain planar lore
# At level 30, gain master herbology

# TODO: book loaning

skills_by_level = {
        1: ['herb herb'],
        2: ['label book', 'engrave bowl bowl', 'embroider belt belt'],
        3: ['light fire', 'combatlog self', 'combatlog stop'],
        4: ['organ room name'],
        # 5: ['smokesig testing testing 1 2 3'],
        6: ['entitle book book', 'findhome'],
        7: ['bookedit book', 'knowplant herb'],
        9: ['wlore', 'transcribe paper book'],
        10: ['bname book 1 book', 'speculate'],
        11: ['lore book'],
        12: ['rlore human', 'findship'],
        13: ['cwrite book', 'map paper'],
        15: ['plantlore'],
        18: ['recollect happy'],
        21: ['plore astral'],
        22: ['survey room book'],
        29: ['mherb herb'],
        }

def write(mud, matches=None):
    mud.send('stand')
    mud.send('write book "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"')
    mud.send('\n')
    mud.send('\n')
    mud.send('sleep')

def practiceOne(mud):
    level = mud.level() - 1
    mud.log("practiceOne")
    practiceImpl(mud, level, 9, -1)

def practiceTwo(mud):
    mud.log("practiceTwo")
    practiceImpl(mud, 9, 0, -1)

def practiceImpl(mud, begin, end, step):
    if mud.gmcp['room']['info']['num'] != 1741703288:
        mud.log("Not running Scholar script - must be in Pecking Place")
        return

    out = []
    lag = 1
    mud.send("stand")
    for i in range(begin, end, step):
        if i in skills_by_level:
            for skill in skills_by_level[i]:
                mud.log("scholar " + skill)
                mud.timers["scholar_" + skill] = mud.mkdelay(lag, lambda m, skill=skill: mud.send(skill))
                lag += 1
    mud.timers["scholar_sleep"] = mud.mkdelay(lag, lambda m: mud.send("sleep"))
    return

def learnFrom(mud, matches):
    if 'learn' not in mud.state:
        mud.state['learn'] = {}
    mud.state['learn']['from'] = matches[0]

def startLearning(mud, matches):
    if 'learn' not in mud.state:
        mud.state['learn'] = {}
    mud.state['learn']['learner'] = matches[0]
    if 'from' not in mud.state['learn']:
        mud.state['learn']['from'] = 'guildmaster'
    mud.send("study {} {}".format(mud.state['learn']['from'], matches[1]))

def failedLearning(world, matches):
    if 'learn' not in world.state:
        return
    world.state['learn']['failed'] = True
    world.send("study {} {}".format(world.state['learn']['from'], matches[0]))

def doneLearning(world, matches):
    if 'learn' not in world.state:
        return
    state = world.state['learn']

    if 'failed' not in state:
        world.send("teach " + state['learner'] + " " + matches[0])
    else:
        del state['failed']

def tryAgainTeaching(world, matches):
    if 'learn' not in world.state:
        return
    if 'times' not in world.state['learn']:
        world.state['learn']['times'] = 0
    world.state['learn']['times'] += 1
    if world.state['learn']['times'] < 10:
        world.send("teach " + world.state['learn']['learner'] + " " + matches[0])

def doneTeaching(world, matches):
    world.send('study forget ' + matches[0])
    del world.state['learn']

class Scholar(BaseModule):
    def getAliases(self):
        return {
                '#learnfrom (.+)': learnFrom,
                }

    def getTriggers(self):
        return {
            '^You have 58 points remaining.': 'wis 15\nint 15\ncha 7\ncon 15\nstr 6\n\n',
            '^You have 64 points remaining.': 'wis 15\nint 15\ncha 7\ncon 15\nstr 12\n\n',
            '^Please choose from the following Classes:': 'apprentice',
            '^Is Apprentice correct': 'y',
            '^You have remorted back to level 1!': 'run n w\ntrain scholar\ntrain int\ntrain int\ntrain int\ntrain int\nprac write\nrun e s\nsay help',
            'Enter the name of the chapter:': '',
            'You are now in Add Text mode.': '\nq\ny',

            "(.+) whispers to you 'teach me (.+)'.": startLearning,
            ".+ fails to teach you (.+).": failedLearning,
            'You are done learning (.+) from (.+).': doneLearning,
            'You already know (.+).': doneLearning,
            "You don't seem to know (.+).": tryAgainTeaching,
            ".+ has not learned the pre-requisites to (.+) yet.": doneTeaching,
            "You teach .+ '(.+)'": doneTeaching,
            "You attempt to write on .*, but mess up.": write,
            'You are hungry.': 'sta\neat bread\nsleep',
            'You are thirsty.': 'stand\nn\ndrink sink\ndrink sink\ndrink sink\ndrink sink\ns\nsleep',
            }

    def getTimers(self):
        return {
                "practiceOne": (False, 5+2*600, 60, practiceOne),
                "practiceTwo": (False, 5+1*600, 615, practiceTwo),
                "write": (False, 605, 15, write),
                }

def getClass():
    return Scholar
<commit_msg>scholar fixes to stacking<commit_after>from modules.basemodule import BaseModule

import random
import re
import time

# start with at least 10 charisma, max int and wis
# At level 1, train scholar, practice write
# At level 2, write book
# At level 3, gain engraving and embroidering, prac herbology
# At level 4, gain combat log
#(# At level 5, gain organizing)
# At level 7, gain find home
# At level 9, gain wilderness lore
# At level 11, gain book naming
# At level 13, gain find ship
# At level 14, gain make maps
# At level 16, gain plant lore
# At level 22, gain planar lore
# At level 30, gain master herbology

# TODO: book loaning

skills_by_level = {
        1: ['herb herb'],
        2: ['label book', 'engrave bowl bowl', 'embroider belt belt'],
        3: ['light fire', 'combatlog self', 'combatlog stop'],
        4: ['organ room name'],
        # 5: ['smokesig testing testing 1 2 3'],
        6: ['entitle book book', 'findhome'],
        7: ['bookedit book', 'knowplant herb'],
        9: ['wlore', 'transcribe paper book'],
        10: ['bname book 1 book', 'speculate'],
        11: ['lore book'],
        12: ['rlore human', 'findship'],
        13: ['cwrite book', 'map paper'],
        15: ['plantlore'],
        18: ['recollect happy'],
        21: ['plore astral'],
        22: ['survey room book'],
        29: ['mherb herb'],
        }

def write(mud, matches=None):
    if mud.gmcp['room']['info']['num'] != 1741703288:
        mud.log("Not running Scholar script - must be in Pecking Place")
        return

    mud.send('stand')
    mud.send('write book "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"')
    mud.send('\n')
    mud.send('\n')
    mud.send('sleep')

def practiceOne(mud):
    level = mud.level() - 1
    mud.log("practiceOne")
    practiceImpl(mud, level, 0, -2)

def practiceTwo(mud):
    mud.log("practiceTwo")
    level = mud.level() - 1
    practiceImpl(mud, level-1, 0, -2)

def practiceImpl(mud, begin, end, step):
    if mud.gmcp['room']['info']['num'] != 1741703288:
        mud.log("Not running Scholar script - must be in Pecking Place")
        return

    out = []
    lag = 1
    mud.send("stand")
    for i in range(begin, end, step):
        if i in skills_by_level:
            for skill in skills_by_level[i]:
                mud.timers["scholar_" + skill] = mud.mkdelay(lag, lambda m, skill=skill: mud.send(skill))
                lag += 1
    mud.timers["scholar_write_begin"] = mud.mkdelay(lag, lambda m: mud.send('write book "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"'))
    lag += 1
    mud.timers["scholar_write_end"] = mud.mkdelay(lag, lambda m: mud.send('sleep'))
    lag += 1
    mud.timers["scholar_sleep"] = mud.mkdelay(lag + 1, lambda m: mud.send("sleep"))
    return

def learnFrom(mud, matches):
    if 'learn' not in mud.state:
        mud.state['learn'] = {}
    mud.state['learn']['from'] = matches[0]

def startLearning(mud, matches):
    if 'learn' not in mud.state:
        mud.state['learn'] = {}
    mud.state['learn']['learner'] = matches[0]
    if 'from' not in mud.state['learn']:
        mud.state['learn']['from'] = 'guildmaster'
    mud.send("study {} {}".format(mud.state['learn']['from'], matches[1]))

def failedLearning(world, matches):
    if 'learn' not in world.state:
        return
    world.state['learn']['failed'] = True
    world.send("study {} {}".format(world.state['learn']['from'], matches[0]))

def doneLearning(world, matches):
    if 'learn' not in world.state:
        return
    state = world.state['learn']

    if 'failed' not in state:
        world.send("teach " + state['learner'] + " " + matches[0])
    else:
        del state['failed']

def tryAgainTeaching(world, matches):
    if 'learn' not in world.state:
        return
    if 'times' not in world.state['learn']:
        world.state['learn']['times'] = 0
    world.state['learn']['times'] += 1
    if world.state['learn']['times'] < 10:
        world.send("teach " + world.state['learn']['learner'] + " " + matches[0])

def doneTeaching(world, matches):
    world.send('study forget ' + matches[0])
    del world.state['learn']

class Scholar(BaseModule):
    def getAliases(self):
        return {
                '#learnfrom (.+)': learnFrom,
                }

    def getTriggers(self):
        return {
            '^You have 58 points remaining.': 'wis 15\nint 15\ncha 7\ncon 15\nstr 6\n\n',
            '^You have 64 points remaining.': 'wis 15\nint 15\ncha 7\ncon 15\nstr 12\n\n',
            '^Please choose from the following Classes:': 'apprentice',
            '^Is Apprentice correct': 'y',
            '^You have remorted back to level 1!': 'run n w\ntrain scholar\ntrain int\ntrain int\ntrain int\ntrain int\nprac write\nrun e s\nsay help',
            'Enter the name of the chapter:': '',
            'You are now in Add Text mode.': '\nq\ny',

            "(.+) whispers to you 'teach me (.+)'.": startLearning,
            ".+ fails to teach you (.+).": failedLearning,
            'You are done learning (.+) from (.+).': doneLearning,
            'You already know (.+).': doneLearning,
            "You don't seem to know (.+).": tryAgainTeaching,
            ".+ has not learned the pre-requisites to (.+) yet.": doneTeaching,
            "You teach .+ '(.+)'": doneTeaching,
            "You attempt to write on .*, but mess up.": write,
            'You are hungry.': 'sta\neat bread\nsleep',
            'You are thirsty.': 'stand\nn\ndrink sink\ndrink sink\ndrink sink\ndrink sink\ns\nsleep',
            }

    def getTimers(self):
        return {
                "practiceOne": (False, 5+2*600, 60, practiceOne),
                "practiceTwo": (False, 5+1*600, 615, practiceTwo),
                }

def getClass():
    return Scholar
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
from operator import attrgetter

from sqlalchemy import cast, String, and_
from colorama import Fore, init, Style

from db.common import session_scope
from db.team import Team
from db.game import Game
from db.team_game import TeamGame
from db.event import Event
from db.goal import Goal

season = 2017

with session_scope() as session:
    # retrieving power play goals for specified season
    pp_goals = session.query(Goal, Event, Game).filter(and_(
        cast(Goal.event_id, String).like("%d02%%" % season),
        Event.event_id == Goal.event_id,
        Event.game_id == Game.game_id,
        Game.season == season,
        Event.num_situation == 'PP')).all()
    # retrieving shorthanded goals for specified season
    sh_goals = session.query(Goal, Event, Game).filter(and_(
        cast(Goal.event_id, String).like("%d02%%" % season),
        Event.event_id == Goal.event_id,
        Event.game_id == Game.game_id,
        Game.season == season,
        Event.num_situation == 'SH')).all()
    # retrieving teams for specified season
    # TODO: adjust query for others than the current season
    teams = session.query(Team).filter(and_(
        Team.first_year_of_play <= season,
        Team.last_year_of_play.is_(None)
    )).all()
    team_games = session.query(
        TeamGame).filter(cast(
            TeamGame.game_id, String).like("%d02%%" % season)).all()
    last_game_date = max(map(
        attrgetter('date'),
        session.query(Game).filter(Game.season == season).all()))

special_teams_summary = dict()

for goal, event, game in pp_goals[:]:
    if goal.team_id not in special_teams_summary:
        special_teams_summary[goal.team_id] = defaultdict(int)
    if goal.goal_against_team_id not in special_teams_summary:
        special_teams_summary[goal.goal_against_team_id] = defaultdict(int)

    # registering power play goals in special team summary
    special_teams_summary[goal.team_id]['ppgf'] += 1
    special_teams_summary[goal.goal_against_team_id]['ppga'] += 1

for goal, event, game in sh_goals[:]:
    if goal.team_id not in special_teams_summary:
        special_teams_summary[goal.team_id] = defaultdict(int)
    if goal.goal_against_team_id not in special_teams_summary:
        special_teams_summary[goal.goal_against_team_id] = defaultdict(int)

    # registering shorthanded goals in special teams summary
    special_teams_summary[goal.team_id]['shgf'] += 1
    special_teams_summary[goal.goal_against_team_id]['shga'] += 1

# calculating goal special teams goal differential
for team in teams:
    if team.team_id not in special_teams_summary:
        special_teams_summary[team.team_id] = defaultdict(int)
    special_teams_summary[team.team_id]['special_teams_diff'] = (
        special_teams_summary[team.team_id]['ppgf'] -
        special_teams_summary[team.team_id]['ppga'] +
        special_teams_summary[team.team_id]['shgf'] -
        special_teams_summary[team.team_id]['shga'])

for tg in team_games:
    special_teams_summary[tg.team_id]['pp_opps'] += tg.pp_overall

i = 1
init()
print()
print(
    " + NHL Special Teams Goal Differential (%s)" % last_game_date.strftime(
        "%b %d, %Y"))

print("  # %-22s %4s %4s %4s %4s %4s %4s %4s" % (
    'Team', 'PPGF', 'SHGF', 'PPGA', 'SHGA', 'PPO', 'PP%', 'STGD'))

# sorting teams by goal differential
for team_id in sorted(special_teams_summary, key=lambda x: (
        special_teams_summary[x]['special_teams_diff'],
        special_teams_summary[x]['ppgf'],
        special_teams_summary[x]['shgf']), reverse=True):
    team = Team.find_by_id(team_id)

    # determining color for goal differential output
    if special_teams_summary[team_id]['special_teams_diff'] > 0:
        diff_str = "+%2d" % special_teams_summary[
            team_id]['special_teams_diff']
        fore = Fore.LIGHTGREEN_EX
    elif special_teams_summary[team_id]['special_teams_diff'] < 0:
        diff_str = "-%2d" % abs(special_teams_summary[
            team_id]['special_teams_diff'])
        fore = Fore.LIGHTRED_EX
    else:
        diff_str = "%3d" % 0
        fore = Fore.LIGHTYELLOW_EX

    pp_pctg = round(
        special_teams_summary[team.team_id]['ppgf'] /
        special_teams_summary[team.team_id]['pp_opps'] * 100., 1
    )
    pp_pctg = "%2d.%d" % (int(pp_pctg), (pp_pctg - int(pp_pctg)) * 10)

    print("%3d %-22s %4d %4d %4d %4d %4d %s  %s%s%s" % (
        i, team,
        special_teams_summary[team.team_id]['ppgf'],
        special_teams_summary[team.team_id]['shgf'],
        special_teams_summary[team.team_id]['ppga'],
        special_teams_summary[team.team_id]['shga'],
        special_teams_summary[team.team_id]['pp_opps'],
        pp_pctg,
        fore, diff_str, Style.RESET_ALL))
    i += 1
<commit_msg>Include shorthanded situations and penalty killing percentage<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
from operator import attrgetter

from sqlalchemy import cast, String, and_
from colorama import Fore, init, Style

from db.common import session_scope
from db.team import Team
from db.game import Game
from db.team_game import TeamGame
from db.event import Event
from db.goal import Goal

season = 2017

with session_scope() as session:
    # retrieving power play goals for specified season
    pp_goals = session.query(Goal, Event, Game).filter(and_(
        cast(Goal.event_id, String).like("%d02%%" % season),
        Event.event_id == Goal.event_id,
        Event.game_id == Game.game_id,
        Game.season == season,
        Event.num_situation == 'PP')).all()
    # retrieving shorthanded goals for specified season
    sh_goals = session.query(Goal, Event, Game).filter(and_(
        cast(Goal.event_id, String).like("%d02%%" % season),
        Event.event_id == Goal.event_id,
        Event.game_id == Game.game_id,
        Game.season == season,
        Event.num_situation == 'SH')).all()
    # retrieving teams for specified season
    # TODO: adjust query for others than the current season
    teams = session.query(Team).filter(and_(
        Team.first_year_of_play <= season,
        Team.last_year_of_play.is_(None)
    )).all()
    team_games = session.query(
        TeamGame).filter(cast(
            TeamGame.game_id, String).like("%d02%%" % season)).all()
    last_game_date = max(map(
        attrgetter('date'),
        session.query(Game).filter(Game.season == season).all()))

special_teams_summary = dict()

for goal, event, game in pp_goals[:]:
    if goal.team_id not in special_teams_summary:
        special_teams_summary[goal.team_id] = defaultdict(int)
    if goal.goal_against_team_id not in special_teams_summary:
        special_teams_summary[goal.goal_against_team_id] = defaultdict(int)

    # registering power play goals in special team summary
    special_teams_summary[goal.team_id]['ppgf'] += 1
    special_teams_summary[goal.goal_against_team_id]['ppga'] += 1

for goal, event, game in sh_goals[:]:
    if goal.team_id not in special_teams_summary:
        special_teams_summary[goal.team_id] = defaultdict(int)
    if goal.goal_against_team_id not in special_teams_summary:
        special_teams_summary[goal.goal_against_team_id] = defaultdict(int)

    # registering shorthanded goals in special teams summary
    special_teams_summary[goal.team_id]['shgf'] += 1
    special_teams_summary[goal.goal_against_team_id]['shga'] += 1

# calculating goal special teams goal differential
for team in teams:
    if team.team_id not in special_teams_summary:
        special_teams_summary[team.team_id] = defaultdict(int)
    special_teams_summary[team.team_id]['special_teams_diff'] = (
        special_teams_summary[team.team_id]['ppgf'] -
        special_teams_summary[team.team_id]['ppga'] +
        special_teams_summary[team.team_id]['shgf'] -
        special_teams_summary[team.team_id]['shga'])

for tg in team_games:
    special_teams_summary[tg.team_id]['pp_opps'] += tg.pp_overall
    special_teams_summary[tg.team_against_id]['tsh'] += tg.pp_overall

i = 1
init()
print()
print(
    " + NHL Special Teams Goal Differential & Combined Percentage (%s)" % (
        last_game_date.strftime("%b %d, %Y")))

print("  # %-22s %4s %4s %4s %4s %4s %4s %4s %4s %5s %4s" % (
    'Team', 'PPGF', 'SHGF', 'PPGA', 'SHGA',
    'PPO', 'PP%', 'TSH', 'PK%', 'PP+PK', 'STGD'))

# sorting teams by goal differential
for team_id in sorted(special_teams_summary, key=lambda x: (
        special_teams_summary[x]['special_teams_diff'],
        special_teams_summary[x]['ppgf'],
        special_teams_summary[x]['shgf']), reverse=True):
    team = Team.find_by_id(team_id)

    # determining color for goal differential output
    if special_teams_summary[team_id]['special_teams_diff'] > 0:
        diff_str = "+%2d" % special_teams_summary[
            team_id]['special_teams_diff']
        fore = Fore.LIGHTGREEN_EX
    elif special_teams_summary[team_id]['special_teams_diff'] < 0:
        diff_str = "-%2d" % abs(special_teams_summary[
            team_id]['special_teams_diff'])
        fore = Fore.LIGHTRED_EX
    else:
        diff_str = "%3d" % 0
        fore = Fore.LIGHTYELLOW_EX

    pp_pctg = (
        special_teams_summary[team.team_id]['ppgf'] /
        special_teams_summary[team.team_id]['pp_opps'] * 100.
    )
    pp_pctgs = "{:4.1f}".format(pp_pctg)

    pk_pctg = 100 - (
        special_teams_summary[team.team_id]['ppga'] /
        special_teams_summary[team.team_id]['tsh'] * 100.
    )
    pk_pctgs = "{:4.1f}".format(pk_pctg)

    pp_pk_pctg = pp_pctg + pk_pctg
    pp_pk_pctgs = "{:5.1f}".format(pp_pk_pctg)

    print("%3d %-22s %4d %4d %4d %4d %4d %s %4d %s %s  %s%s%s" % (
        i, team,
        special_teams_summary[team.team_id]['ppgf'],
        special_teams_summary[team.team_id]['shgf'],
        special_teams_summary[team.team_id]['ppga'],
        special_teams_summary[team.team_id]['shga'],
        special_teams_summary[team.team_id]['pp_opps'], pp_pctgs,
        special_teams_summary[team.team_id]['tsh'], pk_pctgs, pp_pk_pctgs,
        fore, diff_str, Style.RESET_ALL))
    i += 1
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
#
#    Authors: Laetitia Gangloff
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

{
    "name": "Account invoice accrual",
    "version": "0.1",
    "author": "ACSONE SA/NV",
    "category": "Invoice",
    "website": "http://www.acsone.eu",
    "depends": [
        "account",
        "account_reversal",  # from account-financial-tools/7.0
        "account_cutoff_accrual_base",
    ],
    "description": """

Account invoice accrual
=========================

""",
    "data": [
        "account_invoice_view.xml",
        "wizard/account_move_accrue_view.xml",
        "company_view.xml",
    ],
    "demo": [],
    "test": [
        "test/account_invoice_accrual_confirm.yml",
        "test/account_invoice_accrual_remove.yml",
        "test/account_invoice_accrual_reversal.yml",
    ],
    "active": False,
    "license": "AGPL-3",
    "installable": True,
    "auto_install": False,
    "application": False,
}
<commit_msg>[DEL][account_invoice_accrual] Remove description for manifest<commit_after># -*- coding: utf-8 -*-
#
#
#    Authors: Laetitia Gangloff
#    Copyright (c) 2014 Acsone SA/NV (http://www.acsone.eu)
#    All Rights Reserved
#
#    WARNING: This program as such is intended to be used by professional
#    programmers who take the whole responsibility of assessing all potential
#    consequences resulting from its eventual inadequacies and bugs.
#    End users who are looking for a ready-to-use solution with commercial
#    guarantees and support are strongly advised to contact a Free Software
#    Service Company.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

{
    "name": "Account invoice accrual",
    "version": "0.1",
    "author": "ACSONE SA/NV",
    "category": "Invoice",
    "website": "http://www.acsone.eu",
    "depends": [
        "account",
        "account_reversal",  # from account-financial-tools/7.0
        "account_cutoff_accrual_base",
    ],
    "data": [
        "account_invoice_view.xml",
        "wizard/account_move_accrue_view.xml",
        "company_view.xml",
    ],
    "demo": [],
    "test": [
        "test/account_invoice_accrual_confirm.yml",
        "test/account_invoice_accrual_remove.yml",
        "test/account_invoice_accrual_reversal.yml",
    ],
    "active": False,
    "license": "AGPL-3",
    "installable": True,
    "auto_install": False,
    "application": False,
}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
###############################################################################
#                                                                             #
#   Author: Leonardo Pistone
#   Copyright 2014 Camptocamp SA
#                                                                             #
#   This program is free software: you can redistribute it and/or modify      #
#   it under the terms of the GNU Affero General Public License as            #
#   published by the Free Software Foundation, either version 3 of the        #
#   License, or (at your option) any later version.                           #
#                                                                             #
#   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 Affero General Public License for more details.                       #
#                                                                             #
#   You should have received a copy of the GNU Affero General Public License  #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################
"""Accounting customisation for delayed posting."""

import logging

from openerp.osv import fields, orm
from openerp.tools.translate import _

from openerp.addons.connector.queue.job import job
from openerp.addons.connector.session import ConnectorSession
from openerp.addons.connector.queue.job import OpenERPJobStorage

_logger = logging.getLogger(__name__)

# do a massive write on account moves BLOCK_SIZE at a time
BLOCK_SIZE = 1000

class account_move(orm.Model):

    """We modify the account move to allow delayed posting."""

    _name = 'account.move'
    _inherit = 'account.move'

    _columns = {
        'to_post': fields.boolean(
            'Posting Requested',
            readonly=True,
            help='Check this box to mark the move for batch posting'
        ),
        'post_job_uuid': fields.char(
            'UUID of the Job to approve this move'
        ),
    }

    def _delay_post_marked(self, cr, uid, eta=None, context=None):
        """Create a job for every move marked for posting.

        If some moves already have a job, they are skipped.

        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)

        move_ids = self.search(cr, uid, [
            ('to_post', '=', True),
            ('post_job_uuid', '=', False),
            ('state', '=', 'draft'),
        ], context=context)
        name = self._name

        # maybe not creating too many dictionaries will make us a bit faster
        values = {'post_job_uuid': None}
        _logger.info(
            u'{0} jobs for posting moves have been created.'.format(
                len(move_ids)
            )
        )

        for move_id in move_ids:
            job_uuid = validate_one_move.delay(session, name, move_id,
                                               eta=eta)
            values['post_job_uuid'] = job_uuid
            self.write(cr, uid, [move_id], values)
            cr.commit()

    def _cancel_jobs(self, cr, uid, context=None):
        """Find moves where the mark has been removed and cancel the jobs.

        For the moves that are posted already it's too late: we skip them.

        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)
        storage = OpenERPJobStorage(session)

        move_ids = self.search(cr, uid, [
            ('to_post', '=', False),
            ('post_job_uuid', '!=', False),
            ('state', '=', 'draft'),
        ], context=context)

        for move in self.browse(cr, uid, move_ids, context=context):
            job_rec = storage.load(move.post_job_uuid)
            if job_rec.state in (u'pending', u'enqueued'):
                job_rec.set_done(result=_(
                    u'Task set to Done because the user unmarked the move'
                ))
                storage.store(job_rec)

    def mark_for_posting(self, cr, uid, move_ids, eta=None, context=None):
        """Mark a list of moves for delayed posting, and enqueue the jobs."""
        if context is None:
            context = {}
        # For massive amounts of moves, this becomes necessary to avoid
        # MemoryError's

        _logger.info(
            u'{0} moves marked for posting.'.format(len(move_ids))
        )

        for start in xrange(0, len(move_ids), BLOCK_SIZE):
            self.write(
                cr,
                uid,
                move_ids[start:start + BLOCK_SIZE],
                {'to_post': True},
                context=context)
            # users like to see the flag sooner rather than later
            cr.commit()
        self._delay_post_marked(cr, uid, eta=eta, context=context)

    def unmark_for_posting(self, cr, uid, move_ids, context=None):
        """Unmark moves for delayed posting, and cancel the jobs."""
        if context is None:
            context = {}
        self.write(cr, uid, move_ids, {'to_post': False}, context=context)
        self._cancel_jobs(cr, uid, context=context)

@job(default_channel='root.account_move_batch_validate')
def validate_one_move(session, model_name, move_id):
    """Validate a move, and leave the job reference in place."""
    move_pool = session.pool['account.move']
    if move_pool.exists(session.cr, session.uid, [move_id]):
        move_pool.button_validate(
            session.cr,
            session.uid,
            [move_id]
        )
    else:
        return _(u'Nothing to do because the record has been deleted')
<commit_msg>[FIX] account_move_batch_validate: Protect connector import. Fixes #280<commit_after># -*- coding: utf-8 -*-
###############################################################################
#                                                                             #
#   Author: Leonardo Pistone
#   Copyright 2014 Camptocamp SA
#                                                                             #
#   This program is free software: you can redistribute it and/or modify      #
#   it under the terms of the GNU Affero General Public License as            #
#   published by the Free Software Foundation, either version 3 of the        #
#   License, or (at your option) any later version.                           #
#                                                                             #
#   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 Affero General Public License for more details.                       #
#                                                                             #
#   You should have received a copy of the GNU Affero General Public License  #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################
"""Accounting customisation for delayed posting."""

import logging

from openerp.osv import fields, orm
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)

try:
    from openerp.addons.connector.queue.job import job
    from openerp.addons.connector.session import ConnectorSession
    from openerp.addons.connector.queue.job import OpenERPJobStorage
except ImportError:
    _logger.debug('Can not `import connector`.')

# do a massive write on account moves BLOCK_SIZE at a time
BLOCK_SIZE = 1000

class account_move(orm.Model):

    """We modify the account move to allow delayed posting."""

    _name = 'account.move'
    _inherit = 'account.move'

    _columns = {
        'to_post': fields.boolean(
            'Posting Requested',
            readonly=True,
            help='Check this box to mark the move for batch posting'
        ),
        'post_job_uuid': fields.char(
            'UUID of the Job to approve this move'
        ),
    }

    def _delay_post_marked(self, cr, uid, eta=None, context=None):
        """Create a job for every move marked for posting.

        If some moves already have a job, they are skipped.

        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)

        move_ids = self.search(cr, uid, [
            ('to_post', '=', True),
            ('post_job_uuid', '=', False),
            ('state', '=', 'draft'),
        ], context=context)
        name = self._name

        # maybe not creating too many dictionaries will make us a bit faster
        values = {'post_job_uuid': None}
        _logger.info(
            u'{0} jobs for posting moves have been created.'.format(
                len(move_ids)
            )
        )

        for move_id in move_ids:
            job_uuid = validate_one_move.delay(session, name, move_id,
                                               eta=eta)
            values['post_job_uuid'] = job_uuid
            self.write(cr, uid, [move_id], values)
            cr.commit()

    def _cancel_jobs(self, cr, uid, context=None):
        """Find moves where the mark has been removed and cancel the jobs.

        For the moves that are posted already it's too late: we skip them.

        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)
        storage = OpenERPJobStorage(session)

        move_ids = self.search(cr, uid, [
            ('to_post', '=', False),
            ('post_job_uuid', '!=', False),
            ('state', '=', 'draft'),
        ], context=context)

        for move in self.browse(cr, uid, move_ids, context=context):
            job_rec = storage.load(move.post_job_uuid)
            if job_rec.state in (u'pending', u'enqueued'):
                job_rec.set_done(result=_(
                    u'Task set to Done because the user unmarked the move'
                ))
                storage.store(job_rec)

    def mark_for_posting(self, cr, uid, move_ids, eta=None, context=None):
        """Mark a list of moves for delayed posting, and enqueue the jobs."""
        if context is None:
            context = {}
        # For massive amounts of moves, this becomes necessary to avoid
        # MemoryError's

        _logger.info(
            u'{0} moves marked for posting.'.format(len(move_ids))
        )

        for start in xrange(0, len(move_ids), BLOCK_SIZE):
            self.write(
                cr,
                uid,
                move_ids[start:start + BLOCK_SIZE],
                {'to_post': True},
                context=context)
            # users like to see the flag sooner rather than later
            cr.commit()
        self._delay_post_marked(cr, uid, eta=eta, context=context)

    def unmark_for_posting(self, cr, uid, move_ids, context=None):
        """Unmark moves for delayed posting, and cancel the jobs."""
        if context is None:
            context = {}
        self.write(cr, uid, move_ids, {'to_post': False}, context=context)
        self._cancel_jobs(cr, uid, context=context)

@job(default_channel='root.account_move_batch_validate')
def validate_one_move(session, model_name, move_id):
    """Validate a move, and leave the job reference in place."""
    move_pool = session.pool['account.move']
    if move_pool.exists(session.cr, session.uid, [move_id]):
        move_pool.button_validate(
            session.cr,
            session.uid,
            [move_id]
        )
    else:
        return _(u'Nothing to do because the record has been deleted')
<|endoftext|>