import json
import os
import re

from wsgi import WSGI, get_body
from base import init_base
from base import sql_select, sql_delete, sql_insert

connect, cursor = init_base('magint_test.db', 'magnit_test_db.sql')

application = WSGI()

comment_validator = dict(
    # id=[lambda x: type(x) == int],
    comment_id=[lambda x: type(x) == int],
    place_id=[lambda x: type(x) == int],
    level=[lambda x: type(x) == int],
    first_name=[lambda x: type(x) == str, lambda x: len(x) < 100],
    last_name=[lambda x: type(x) == str, lambda x: len(x) < 100],
    father_name=[lambda x: type(x) == str, lambda x: x is None or len(x) < 100],
    phone=[
                    lambda x: type(x) == str,
                    lambda x: x is None or len(x) < 100,
                    lambda x: re.match('^((\+7|7|8)+([0-9]){10})$', x)
                ],
    email=[lambda x: type(x) == str, lambda x: x is None or len(x) < 100],
    text=[lambda x: type(x) == str, lambda x: len(x) < 1024]
)

@application.add_route('/places/?', ['GET'])
@application.add_route('/places/(?P<place_id>[0-9]+)', ['GET'])
def view_places(environ, place_id=None):
    """ выборка региона/города
    Args:
        place_id: родительский регион
    Returns:
        список словарей
    """
    sql, args = sql_select('places', place_id=place_id)
    print(sql, args)
    items = cursor.execute(sql, args).fetchall()
    return json.dumps(dict(items=items)), 'application/json'

@application.add_route('^/comments/?', ['GET', 'POST'])
@application.add_route('^/comments/(?P<comment_id>[0-9]+)', ['DELETE', 'GET'])
@application.add_route('^/comments/place/(?P<place_id>[0-9]+)', ['GET'])
def view_comments(environ, comment_id=None, place_id=None):
    """ выборка комментариев

    Args:
        comment_id: выбора вложенных в родительский комментариев
        place_id: выбора комментариев связанных с определённым местом
    Returns:
        список словарей
    """
    result = False
    ctype = 'application/json'
    if environ['REQUEST_METHOD'] == 'DELETE':
        if comment_id:
            sql, args = sql_delete('comments', id=comment_id)
            cursor.execute(sql, args)
            connect.commit()
            result = True
        return json.dumps(dict(result=result)), ctype

    if environ['REQUEST_METHOD'] == 'POST':
        comment = get_body(environ, comment_validator)
        if comment:
            sql, args = sql_insert('comments',  **comment)
            cursor.execute(sql, args)
            connect.commit()
            result = True
        return json.dumps(dict(result=result)), 'application/json'
    limit = 5
    # paginator
    page = int(environ['args'].get('page', [0])[0])
    names = [
            '*',
            '(select name from places where id=comments.place_id) as place',
            '(select count(1) from comments c2 where c2.comment_id=comments.id) as cnt'
        ]
    options = dict(comment_id=comment_id)
    if place_id:
        options = dict(place_id=place_id)
    sql, args = sql_select(
            'comments',
            names=names,
            **options,
            page=page,
            limit=limit,
            order='id desc'
        )
    items = cursor.execute(sql, args).fetchall()
    sql, args = sql_select(
            'comments',
            names='count(1) as total',
            **options,
        )
    print(args)
    total = cursor.execute(sql, args).fetchone()
    return json.dumps(dict(items=items, limit=limit, **total)), ctype

@application.add_route('^/$', ['GET'])
def index(environ):
    """ страница комментариев

    Returns:
        angular-шаблон приложения комментариев
    """
    return open('templates/comment_view.html', 'rb').read(), 'text/html'

@application.add_route('^/stat/', ['GET'])
@application.add_route('^/stat/(?P<place_id>[0-9]+)', ['GET'])
def view_stat(environ, place_id=None):
    """ выборка статистики
    Args:
        place_id: родительский регион
    Returns:
        список словарей
    """
    if place_id is None:
        sql = '''
            select id, name,
                (select count(1) from comments c left join places p on (c.place_id = p.id) where p.place_id = places.id ) as cnt
            from places
            group by cnt
                having cnt > 5
                and place_id is ?'''
    else:
        sql = '''select p.id, c.place_id, count(c.id) cnt, p.name \
            from comments as c \
                left join places as p on (c.place_id = p.id) \
                group by c.place_id \
                and p.place_id = ? '''
    items = cursor.execute(sql, [place_id,]).fetchall()
    return json.dumps(dict(items=items)), 'application/json'


import json
from collections import namedtuple

# convert data (dictionary) into JSON format
obj = {'task': 'run 5 miles', 'goal': 40}
print(json.dumps(obj, indent=4))

# convert custom classes (types) into JSON format
Contact = namedtuple('Contact', 'first last age email')
contact = Contact('John',  'Smith',   43, 'jsbrony@yahoo.com')
print(json.dumps(contact._asdict(), indent=4))

# convert JSON strings into Python objects (dictionaries)
new_obj = json.loads('{"first": "John","last": "Smith","age": 43,"email": "jsbrony@yahoo.com"}')
print(new_obj)

# how about objects???
class Player:
    def __init__(self, first, last, salary, year):
        self.first = first
        self.last = last
        self.salary = salary
        self.year = year

p1 = Player('John', 'Smith', 30000000, 1985)
print(json.dumps(p1, default=lambda player: player.__dict__))

# what about non-serializable fields such as birthdate???
class Player2:
    def __init__(self, first, last, salary, year, birthdate):
        self.first = first
        self.last = last
        self.salary = salary
        self.year = year
        self.birthdate = birthdate

import datetime
bd = datetime.date(1970, 10, 20)
p2 = Player2('Ed', 'Green', 22000000, 198, bd)
try:
    print(json.dumps(p2, default=lambda player: player.__dict__))
except AttributeError as err:
    print('Unable to convert object p2')

# how to fix the above failure....create an encoder class that defines how to handle certain types
class PlayerEncoder(json.JSONEncoder):
    def default(self, obj):
        try:
            result = obj.__dict__
        except (AttributeError, TypeError):
            if isinstance(obj, datetime.date):
                result = '{0}-{1}-{2}'.format(obj.year, obj.month, obj.day)
            else:
                result = 'unable to determine'

        return result

p3 = Player2('Tom', 'Summers', 18000000, 1991, bd)
print(json.dumps(p3, cls=PlayerEncoder))


from cfepm.util.io_utils import read_dataset, ensure_parent_dir
from argparse import ArgumentParser
import sklearn.model_selection as model_selection
from cfepm.lab.validation import cross_val_score
import sklearn.preprocessing as skprep
import sklearn.linear_model as sklinear
import sklearn.metrics.scorer as sksco
from cfepm.pipeline import *
from sklearn.pipeline import Pipeline
from sklearn.externals import joblib
import logging

def prepare_feature_df(args):
    output_path = args.output_npz_path
    ft_model_path = args.fasttext_model
    input_path = args.dataset_csv_path
    min_confidence = args.min_confidence
    print("Reading a dataset from %s" % input_path)
    #
    df = _read_df(input_path, min_confidence)
    #
    pipeline_steps = []
    pipeline_steps.append(("row2bean", Row2BeanConverter()))
    pipeline_steps.append(("preprocessor", BeanTuplePreprocessor(ft_model_path)))
    pipeline_steps.append(("feature_extractor", PMFeatureExtractor()))
    pipeline = Pipeline(pipeline_steps)
    feature_arr = pipeline.transform(df)
    target_arr = df['item_yn'].values
    # write
    print("Writing the feature matrix and target vector into %s..." % output_path)
    ensure_parent_dir(output_path)
    np.savez(output_path, features=feature_arr, labels=target_arr)
    print("Done.")

def _read_df(input_path, min_confidence):
    df = read_dataset(input_path)
    if min_confidence is not None:
        print("Min confidence is set to %s. Filtering..." % min_confidence)
        len_before = len(df.index)
        df = df[df['item_yn:confidence'] >= min_confidence].copy()
        len_after = len(df.index)
        print("Removed %s rows." % (len_before - len_after))
    return df

def evaluate(args):
    verbosity = args.verbose
    with np.load(args.input_npz_path) as npz:
        feature_arr = npz['features']
        target_arr = npz['labels']
    X_arr = feature_arr
    y_arr = target_arr
    cv = model_selection.KFold(n_splits=5, shuffle=True, random_state=42777)
    # prepare feature values
    X_prep = skprep.StandardScaler()
    X_arr = X_prep.fit_transform(X_arr)
    # initialize estimator
    estimator = sklinear.LogisticRegression()
    # run CV
    scoring_tuples = [('accuracy', 'accuracy'),
                      ('yes_precision', sksco.make_scorer(sksco.precision_score, pos_label='yes')),
                      ('yes_recall', sksco.make_scorer(sksco.recall_score, pos_label='yes')),
                      ('yes_f1', sksco.make_scorer(sksco.f1_score, pos_label='yes')),
                      ('no_precision', sksco.make_scorer(sksco.precision_score, pos_label='no')),
                      ('no_recall', sksco.make_scorer(sksco.recall_score, pos_label='no')),
                      ('no_f1', sksco.make_scorer(sksco.f1_score, pos_label='no')),
                      ]
    fold_scores = cross_val_score(
        estimator, X_arr, y_arr,
        scorings=[t[1] for t in scoring_tuples],
        cv=cv, verbose=verbosity)
    fold_scores = np.vstack((fold_scores, fold_scores.mean(axis=0)))
    # print(tabulate(fold_scores,
    #                headers=[t[0] for t in scoring_tuples],
    #                showindex=['fold_' + str(i) for i in range(0, fold_scores.shape[0] - 1)] + ['Avg'],
    #                floatfmt='.4f'))

def train(args):
    input_path = args.input_path
    output_pipeline_path = args.output_pipeline_path
    min_confidence = args.min_confidence
    ft_model_path = {}#args.fasttext_model
    #
    df = _read_df(input_path, min_confidence)
    bean_tuples = Row2BeanConverter().transform(df)
    target_arr = df['item_yn'].values
    if target_arr.shape[0] != len(bean_tuples):
        raise ValueError()
    #
    pipeline_steps = []
    pipeline_steps.append(("preprocessor", BeanTuplePreprocessor(ft_model_path)))
    pipeline_steps.append(("feature_extractor", PMFeatureExtractor()))
    pipeline = Pipeline(pipeline_steps)
    print("Fitting pipeline...")
    # TODO
    pipeline.fit(bean_tuples, target_arr)
    print("Serializing into %s..." % output_pipeline_path)
    ensure_parent_dir(output_pipeline_path)
    joblib.dump(pipeline, output_pipeline_path)
    print("Done.")

if __name__ == '__main__':
    argparser = ArgumentParser("BaselineLab")
    argparser.add_argument('--log-level', default='DEBUG')
    subparsers = argparser.add_subparsers()
    # parser for prepare-feature-df subcommand
    prep_fe_df_parser = subparsers.add_parser('prepare-feature-df')
    prep_fe_df_parser.add_argument('dataset_csv_path')
    prep_fe_df_parser.add_argument('output_npz_path')
    prep_fe_df_parser.add_argument('--min-confidence', default=None, type=float)
    # prep_fe_df_parser.add_argument('--force-preprocessing', default=False, action='store_true')
    prep_fe_df_parser.add_argument('--fasttext-model', required=True)
    prep_fe_df_parser.set_defaults(func=prepare_feature_df)
    # parser for evaluate subcommand
    eval_parser = subparsers.add_parser('evaluate')
    eval_parser.add_argument('input_npz_path')
    eval_parser.add_argument('--verbose', type=int, default=1)
    eval_parser.set_defaults(func=evaluate)
    #
    train_parser = subparsers.add_parser('train')
    train_parser.add_argument('input_path')
    train_parser.add_argument('output_pipeline_path')
    train_parser.add_argument('--min-confidence', default=None, type=float)
    train_parser.set_defaults(func=train)
    # parse and run
    args = argparser.parse_args()
    logging.basicConfig(level=args.log_level)
    args.func(args)


#!/usr/bin/env python
# coding: utf-8

###############################################################################
# Link of our dataset:
# http://www2.isprs.org/commissions/comm3/wg4/semantic-labeling.html
##############################################################################

from PIL import Image
import numpy as np
import os.path
import argparse

parser = argparse.ArgumentParser('image processing')
parser.add_argument('--fold_A', dest='fold_A', help='input directory for images', type=str, default='./datasets/images')
parser.add_argument('--fold_B', dest='fold_B', help='input directory for labels', type=str, default='./datasets/labels')
parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='./datasets/satellite/')
parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=1000000)
args = parser.parse_args()

# define the funtion of combining image
def image_compose():
    to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE)) #create a new image

    IMAGE_SAVE_PATH_TRAIN = os.path.join(IMAGE_SAVE_PATH, 'train')
    if not os.path.exists(IMAGE_SAVE_PATH_TRAIN):
        os.makedirs(IMAGE_SAVE_PATH_TRAIN)

    IMAGE_SAVE_PATH_TEST = os.path.join(IMAGE_SAVE_PATH, 'test')
    if not os.path.exists(IMAGE_SAVE_PATH_TEST):
        os.makedirs(IMAGE_SAVE_PATH_TEST)

    num_train = 0
    num_test = 0
    for name in image_names:
            from_image_a_row = Image.open(IMAGES_PATH_1 + name)
            from_image_b_row = Image.open(IMAGES_PATH_2 + name)

            from_image_width,from_image_height = from_image_a_row.size

            for i in range(int(from_image_width/IMAGE_SIZE)):
                for j in range(int(from_image_height / IMAGE_SIZE)):

                    from_image_a = from_image_a_row.crop((i*IMAGE_SIZE,j*IMAGE_SIZE,(i+1)*IMAGE_SIZE,(j+1)*IMAGE_SIZE))
                    from_image_b = from_image_b_row.crop((i*IMAGE_SIZE,j*IMAGE_SIZE,(i+1)*IMAGE_SIZE,(j+1)*IMAGE_SIZE))

                    to_image.paste(from_image_a, (0 * IMAGE_SIZE, 0 * IMAGE_SIZE))
                    to_image.paste(from_image_b, (1 * IMAGE_SIZE, 0 * IMAGE_SIZE))

                    if np.random.random() < 0.8:
                        to_image.save(os.path.join(IMAGE_SAVE_PATH_TRAIN, str(num_train+1)+'.jpg'),"JPEG") # save the new image as train sample
                        num_train = num_train + 1

                    else:

                        to_image.save(os.path.join(IMAGE_SAVE_PATH_TEST, str(num_test + 1) + '.jpg'), "JPEG")  # save the new image as test sample
                        num_test = num_test + 1

# convert the semantic image and ground_truth image to one paired image
# attention: the name of the paired semantic image and ground_truth image should be the same

# dataset of Vaihingen
IMAGES_PATH_1 = args.fold_A  # ground truth immage, path should be changed
IMAGES_PATH_2 = args.fold_B  # semantic image , path should be changed
IMAGES_FORMAT = ['.tif']
IMAGE_SIZE = 800
IMAGE_ROW = 1
IMAGE_COLUMN = 2
IMAGE_SAVE_PATH = args.fold_AB

# get all the name in the imageset
image_names = [name for name in os.listdir(IMAGES_PATH_2) for item in IMAGES_FORMAT if
               os.path.splitext(name)[1] == item]

image_compose() # call the function to get the aligned image


# -*- coding: utf-8 -*-

from flask import flash
from . import db
from .models import Announcement, AnnouncementType
from .models import AssignmentScore, VBTestScore, YGRETestScore
from .models import Feed

def get_announcements(type_name, user=None, flash_first=False):
    query = Announcement.query\
        .join(AnnouncementType, AnnouncementType.id == Announcement.type_id)\
        .filter(AnnouncementType.name == type_name)\
        .filter(Announcement.show == True)\
        .filter(Announcement.deleted == False)
    if flash_first:
        announcement = query.first()
        if announcement is not None:
            flash('

{}
{}
'.format(announcement.title, announcement.body_html), category='announcement')
        return
    else:
        announcements = query.order_by(Announcement.modified_at.desc()).all()
        for announcement in announcements:
            if not user.notified_by(announcement=announcement):
                flash('

{}
{}
'.format(announcement.title, announcement.body_html), category='announcement')
                announcement.notify(user=user)
                db.session.commit()
        return announcements

def flash_reminders(user):
    for assginment_score in AssignmentScore.query\
        .filter(AssignmentScore.user_id == user.id)\
        .filter(AssignmentScore.notified == False)\
        .all():
        flash('

{}
{}
'.format(assginment_score.alias2, assginment_score.remark_html), category='announcement')
        assginment_score.set_notified()
        db.session.commit()
    for vb_test_score in VBTestScore.query\
        .filter(VBTestScore.user_id == user.id)\
        .filter(VBTestScore.notified == False)\
        .all():
        flash('

{}
{}
'.format(vb_test_score.alias2, vb_test_score.remark_html), category='announcement')
        vb_test_score.set_notified()
        db.session.commit()
    for y_gre_test_score in YGRETestScore.query\
        .filter(YGRETestScore.user_id == user.id)\
        .filter(YGRETestScore.notified == False)\
        .all():
        flash('

{}
{}
'.format(y_gre_test_score.alias3, y_gre_test_score.remark_html), category='announcement')
        y_gre_test_score.set_notified()
        db.session.commit()

def __add_feed(user_id, event, category):
    feed = Feed(user_id=user_id, event=event, category=category)
    db.session.add(feed)

def add_feed(user, event, category):
    __add_feed(user_id=user.id, event=event, category=category)

def add_sys_feed(event, category):
    __add_feed(user_id=1, event=event, category=category)


#
# Copyright (C) 2014 eNovance SAS <licensing@enovance.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from pecan import conf
from pecan import abort
from pecan import request

from pysflib.sfredmine import RedmineUtils

import logging

logger = logging.getLogger(__name__)

def get_client():
    return RedmineUtils(conf.redmine['url'],
                        key=conf.redmine['api_key'])

def init_project(name, inp):
    logger.info('[redmine] Create project %s' % name)
    rm = get_client()
    description = '' if 'description' not in inp else inp['description']
    ptl = [] if 'ptl-group-members' not in inp else inp['ptl-group-members']
    private = False if 'private' not in inp else inp['private']
    core = [] if 'core-group-members' not in inp else inp['core-group-members']
    dev = [] if 'dev-group-members' not in inp else inp['dev-group-members']

    # create the project
    rm.create_project(name, description, private)
    # Add current user to Manager and Developper
    uid = rm.get_user_id_by_username(request.remote_user)
    usermail = rm.r.user.get(uid).mail
    add_user_to_projectgroups(name, usermail, ['ptl-group'], owner=True)
    add_user_to_projectgroups(name, usermail, ['dev-group'])
    for m in ptl:
        add_user_to_projectgroups(name, m, ['ptl-group'])
    for m in core:
        add_user_to_projectgroups(name, m, ['core-group'])
    for m in dev:
        add_user_to_projectgroups(name, m, ['dev-group'])

def delete_project(name):
    rm = get_client()
    if not user_is_administrator() and not user_manages_project(name):
        abort(403)
    rm.delete_project(name)

def add_user_to_projectgroups(project, user, groups, owner=False):
    rm = get_client()
    for g in groups:
        if g not in ['ptl-group', 'core-group', 'dev-group']:
            abort(400)
    roles = rm.get_project_roles_for_user(
        project,
        rm.get_user_id_by_username(request.remote_user))
    logger.info("[redmine] Add user %s in groups %s from project %s" %
                (user, str(groups), project))
    uid = rm.get_user_id(user)
    role_id = []
    # only admin or manager can add manager
    if 'ptl-group' in groups:
        if (not user_is_administrator()) and ('Manager' not in roles) and \
                not owner:
            logger.info("[redmine] Aborded due to user is not admin,Manager")
            abort(401)
        mgr_role_id = rm.get_role_id('Manager')
        role_id.append(mgr_role_id)
    if ('core-group' in groups) or ('dev-group' in groups):
        if (not user_is_administrator()) and ('Manager' not in roles) and \
           ('Developer' not in roles) and not owner:
            logger.info("[redmine] Aborded due to user is not "
                        "admin,Manager,Developer")
            abort(401)
        dev_role_id = rm.get_role_id('Developer')
        role_id.append(dev_role_id)
    m = rm.get_project_membership_for_user(project, uid)
    if m:
        roles = rm.get_project_roles_for_user(project, uid)
        role_ids = [rm.get_role_id(u) for u in roles]
        role_ids.extend(role_id)
        rm.update_membership(m, role_ids)
    else:
        memberships = {'user_id': uid, 'role_ids': role_id}
        rm.update_project_membership(project, [memberships])

def delete_user_from_projectgroups(project, user, group):
    rm = get_client()
    # if user is not project member, then return
    uid = rm.get_user_id(user)
    m = rm.get_project_membership_for_user(project, uid)
    if not m:
        return None

    # get current user roles
    roles = rm.get_project_roles_for_user(
        project,
        rm.get_user_id_by_username(request.remote_user))

    # if user not requested a single group, then delete all groups,
    # otherwise delete requested group
    if group is None:
        # delete all groups
        if (not user_is_administrator()) and ('Manager' not in roles):
            logger.debug("[redmine] Aborded due to user is not admin,Manager")
            abort(401)
        rm.delete_membership(m)
    else:
        if group and group not in ['ptl-group', 'core-group', 'dev-group']:
            abort(400)
        # Get the role id from requested group name
        if group in ['dev-group', 'core-group']:
            if (not user_is_administrator()) and ('Manager' not in roles) and \
               ('Developer' not in roles):
                logger.debug(
                    "[redmine] Aborded due to user is not "
                    "admin,Manager,developer")
                abort(401)
            role_id = rm.get_role_id('Developer')
        else:
            if (not user_is_administrator()) and ('Manager' not in roles):
                logger.debug("[redmine] Aborded due to user is not "
                             "admin,Manager")
                abort(401)
            role_id = rm.get_role_id('Manager')
        # Get list of current role_ids for this user
        uroles = rm.get_project_roles_for_user(project, uid)
        role_ids = [rm.get_role_id(u) for u in uroles]
        # check if requested role is present in the membership roles
        if role_id in role_ids:
            role_ids.remove(role_id)
            # delete te requested role
            rm.update_membership(m, role_ids)

def user_manages_project(prj_name):
    rm = get_client()
    roles = rm.get_project_roles_for_user(
        prj_name,
        rm.get_user_id_by_username(request.remote_user))
    if 'Manager' in roles:
        return True
    return False

def user_is_administrator():
    return request.remote_user == conf.admin['name']

def get_open_issues():
    rm = get_client()
    return rm.get_open_issues()

def get_active_users():
    rm = get_client()
    return rm.active_users()


import xml.dom.minidom as dom
from IEC_Trace import Trace
from IEC_Trace import Level    as TL
from IEC_FileListe import FileListe as FL
from IEC61850_XML_Class import DataTypeTemplates as IECType

##
# \b Parse_LNodeType: this class create the list of LNodeType and the Data Objects elements
# @brief
# @b Description
#   This class is parsing the LnodeType in XML and embed the list of LNodeType for each of the DO.
#<LNodeType id="SIE_A1_MU2/MU01/I01ATCTR1" lnClass="TCTR">
#	<DO name="Mod" type="myMod_0" />
#	<DO name="Beh" type="myBeh_1" />
# ...

class Parse_LNodeType:
    ## \b Description
    #   Constructor is used to keep the dictionary of DOType available.
    #
    # @image html DO.png        width=300px
    # @param _scl: pointer to the SCL structure created by miniDOM
    # @param _TRX: Trace function
    def __init__(self, _scl, _TRX):
        self.TRX = _TRX
        self.SCL = _scl
        self.dicLNodeType = {}

    ##
    # Return a full DoType for a given doType 'id'
    #
    # @param   id: the Date Object type to look up.
    # @return  An instance of the LNodeType Object elements, including the list of DO
    def getIEC_LNodeType(self, id):             # Convert dictionary structure to LNodeType class
        iLNode = self.dicLNodeType.get(id)
        if iLNode is None:
            return None
        _lnClass = iLNode.get('lnClass')
        _desc    = iLNode.get('desc')
        _iedType = iLNode.get('iedType')
        _tDO     = iLNode.get('tDO')
        instLNode = IECType.LNodeType(id, _lnClass, _desc, _iedType, _tDO)

        return instLNode

    ##
    # @return the dictionary of LnodeType
    def GetLnodeDict(self):
        return self.dicLNodeType

    ##
    #  Create the table of DO elements attached to a given LNodeType
    #
    #  @param pDO  pointer to the SCL structure pointing to the DO list
    #  @return     The table of the DO.
    #
    #      <DO name="Op" type="SE_ACT_V005" />
    #      <DO name="NamPlt" type="SE_LPL_V003" desc="Name plate" />
    #
    def Get_DO_Attributes(self, pDO):
        tDO=[]                          # Allocate the table of DO
        pDO = pDO.firstChild.nextSibling
        while pDO:
            if  pDO.localName is None:
                pDO = pDO.nextSibling
                continue
            _name           = pDO.getAttribute("name")
            _type           = pDO.getAttribute("type")
            _accessControl  = pDO.getAttribute("accessControl")
            _transient      = pDO.getAttribute("transient")
            _desc           = pDO.getAttribute("desc")
            iDO= IECType.LNodeType.DOI(_name, _type, _accessControl, _transient, _desc)
            self.TRX.Trace(("     DO:"+_name+" type:"+_type+" desc:"+_desc),TL.DETAIL)
            tDO.append(iDO)
            pDO = pDO.nextSibling
        return tDO

    ##
    # CreateLNodeType_Dict
    #
    # @param DataType: is the result of scl.getElementsByTagName("DataTypeTemplates")
    #
    # @return
    #       tLNodeType : the table of LNodeType objects
    #       self.dicLNodeType: the dictionary (used by  Test_LNodeType)
    #
    def Create_LNodeType_Dict(self, DataType):
        tLNodeType   = []
        tDO = []
        for pDT in DataType:
            pDT = pDT.firstChild.nextSibling

            while pDT.nextSibling:
                if pDT.localName is None:
                    pDT = pDT.nextSibling
                    continue
                if pDT.localName == "LNodeType":
                    _id      = pDT.getAttribute("id")
                    _desc    = pDT.getAttribute("desc")
                    _iedType = pDT.getAttribute("iedType")
                    _lnClass = pDT.getAttribute("lnClass")   # Used as the key for the python dictionnay
                    iLNodeType = IECType.LNodeType(_id, _desc, _iedType, _lnClass, [])
                    tLNodeType.append(iLNodeType)
                    self.TRX.Trace(("LNodeType: id:" + _id + " lnClass:" + _lnClass + " iedType:" + _iedType + " desc:" + _desc),TL.DETAIL)
                    tDO = self.Get_DO_Attributes(pDT)
                    self.dicLNodeType[_id] = {"lnClass": _lnClass, "desc": _desc, "iedType": _iedType, "tDO": tDO}

                    pDT = pDT.nextSibling
                    continue
                pDT = pDT.nextSibling
            continue
        return tLNodeType, self.dicLNodeType

##
# \b Test_LNodeType: unitary test for PARSE DoType
class Test_LNodeType:
    ##
    # Unitary for ParseDoType, invoked by IEC_test.py
    def main(directory, file, scl):
        TRX = Trace.Console(TL.DETAIL)

        TRX.Trace(("---------------------------------------------------"), TL.GENERAL)
        if scl is None:  # UNIT TEST
            scl = dom.parse(directory + file)
        TRX.Trace(("File:" + file), TL.GENERAL)
        DataType     = scl.getElementsByTagName("DataTypeTemplates")
        LNodeType    = Parse_LNodeType(scl, TRX)
        tLNodeType, dicLNodeType = LNodeType.Create_LNodeType_Dict(DataType)
        TRX.Trace(("END OF IEC_LNodeType"), TL.GENERAL)
##
# \b MAIN call the unitary test 'Test_LNodeType for PARSE LNodeType
if __name__ == '__main__':
    fileliste = FL.lstFull  # System level file list
    for file in fileliste:
        Test_LNodeType.main('SCL_files/', file, None)

    fileliste = FL.lstIED  # IED level file list
    for file in fileliste:
        Test_LNodeType.main('SCL_files/', file, None)


#!/usr/bin/python

from PIL import Image, ImageDraw, ImageFont
# 1920x1080
# lenovo is 2560x1440

#screen_height = 2560
#screen_width = 1440

screen_height = 1440
screen_width = 2560

mid_gray = (128, 128, 128, 0)
bright_gray = (200, 200, 200, 0)

x_speed = 25
y_speed = 128

cursor_size = 128

fontpath = "/usr/share/fonts/truetype/freefont/FreeMono.ttf"

def bottom_right(draw, color, size, x_offset, y_offset):
  c1 = {}
  c2 = {}
  c3 = {}

  c1['x'] = x_offset
  c1['y'] = y_offset
  c2['x'] = size+x_offset
  c2['y'] = y_offset
  c3['x'] = x_offset
  c3['y'] = size+y_offset
  draw.polygon([(c1['x'], c1['y']),(c2['x'], c2['y']),(c3['x'], c3['y'])], fill = color)

def bottom_left(draw, color, size, x_offset, y_offset):
  c1 = {}
  c2 = {}
  c3 = {}

  c1['x'] = x_offset
  c1['y'] = y_offset
  c2['x'] = size+x_offset
  c2['y'] = y_offset
  c3['x'] = size+x_offset
  c3['y'] = size+y_offset
  draw.polygon([(c1['x'], c1['y']),(c2['x'], c2['y']),(c3['x'], c3['y'])], fill = color)

def top_left(draw, color, size, x_offset, y_offset):
  c1 = {}
  c2 = {}
  c3 = {}

  # top left
  c1['x'] = size+x_offset
  c1['y'] = 0+y_offset #top right corner
  c2['x'] = 0+x_offset
  c2['y'] = size+y_offset #bottom left corner
  c3['x'] = size+x_offset
  c3['y'] = size+y_offset  #bottom right corner
  draw.polygon([(c1['x'], c1['y']),(c2['x'], c2['y']),(c3['x'], c3['y'])], fill = color)

def top_right(draw, color, size, x_offset, y_offset):
  c1 = {}
  c2 = {}
  c3 = {}

  # top right
  c1['x'] = 0+x_offset
  c1['y'] = 0+y_offset #top left corner

  c2['x'] = 0+x_offset
  c2['y'] = size+y_offset

  c3['x'] = size+x_offset
  c3['y'] = size+y_offset

  draw.polygon([(c1['x'], c1['y']),(c2['x'], c2['y']),(c3['x'], c3['y'])], fill = color)

def cursor_pattern(draw, color, x_offset, y_offset):
  size = cursor_size
  spacer = size / 1.5
  top_left(draw, color, size, 0+x_offset,0+y_offset)
  top_right(draw, color, size, x_offset + size + spacer, 0+y_offset)

  fnt = ImageFont.truetype(fontpath, 30)
  draw.text((x_offset+size/4, y_offset+spacer+size/2), "ABCDEFGHIJKLMNOP", font=fnt, fill=color)

  bottom_left(draw, color, size, 0+x_offset,size + spacer+y_offset)
  bottom_right(draw, color, size, size+spacer+x_offset,size + spacer+y_offset)

def clear(img, draw, color="black"):
  draw.rectangle([(0,0),img.size], fill=color)

def main():
  img = Image.new('RGB', (screen_width, screen_height), "black")
  draw = ImageDraw.Draw(img)

  i = 0
  x = 0
  y = 0

  while (x < screen_width):
    clear(img, draw, color="gray")
    filename = "cursor_pattern_{:0>6d}.png".format(i);
    print(filename)

    y = 0
    while (y < screen_height):
      cursor_pattern(draw, "white", x, y)
      y = y + cursor_size*3

    x = x + x_speed

    i = i + 1
    img.save(filename, 'PNG')

  while (x > 0):
    clear(img, draw, color="gray")
    filename = "cursor_pattern_{:0>6d}.png".format(i);
    print(filename)

    y = 0
    while (y < screen_height):
      cursor_pattern(draw, "white", x, y)
      y = y + cursor_size*3

    x = x - x_speed

    i = i + 1
    img.save(filename, 'PNG')

main();


import os
from django import template
from django.conf import settings
from django.template.loader import find_template_source
from django.template.loader import get_template_from_string
from django.utils.html import escape
from django.utils.safestring import mark_safe
try:
    from pygments import highlight
    from pygments.lexers import PythonLexer, HtmlDjangoLexer, HtmlLexer
    from pygments.formatters import HtmlFormatter
except ImportError:
    highlight = None
from examples import forms

register = template.Library()

def get_form(example_name):
    form_path = os.path.join(os.path.dirname(forms.__file__),
                             '%s.py' % example_name)
    form_code = open(form_path, 'rb').read()
    form_module = __import__('examples.forms.%s' % example_name,
                             globals(),
                             locals(),
                             ['ExampleForm'])
    return getattr(form_module, 'ExampleForm'), form_code

def get_template(example_name):
    template_name = 'examples/examples/%s.html' % example_name
    template_code, origin = find_template_source(template_name)
    template_obj = get_template_from_string(template_code, origin, example_name)
    return template_obj, template_code

@register.inclusion_tag('examples/_example.html')
def example(example_name):
    form, form_code = get_form(example_name)
    template_obj, template_code = get_template(example_name)
    rendered = template_obj.render(template.Context({'form': form(),
                                            'MEDIA_URL': settings.MEDIA_URL}))
    if highlight:
        formatter = HtmlFormatter()
        form_code = highlight(form_code, PythonLexer(), formatter)
        template_code = highlight(template_code, HtmlDjangoLexer(), formatter)
        rendered_code = highlight(rendered, HtmlLexer(), formatter)
    else:
        form_code = u'
%s
' % form_code
        tpl = u'
<form action="" method="POST">%s</form>
'
        template_code = tpl % template_code
        rendered_code = tpl % escape(rendered)
    return {
        'name': example_name,
        'form_code': mark_safe(form_code),
        'template': mark_safe(template_code),
        'rendered_code': mark_safe(rendered_code),
        'rendered': rendered,
    }


# did u see this file in the README
# no right
# so y did u still check here?
# smh never follow instructions

import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

movies = []
with open("movies.csv") as f:
    headers = f.readline().strip().split(";")
    for line in f:
        line_lst = line.strip().split(";")
        movie_dict = dict(zip(headers, line_lst))
        movies.append(movie_dict)

db.execute(
    """
    CREATE TABLE movies (
        "id" SERIAL PRIMARY KEY,
        "Title" TEXT NOT NULL,
        "Year" INT4 NOT NULL,
        "Runtime" INT4 NOT NULL,
        "imdbID" VARCHAR NOT NULL,
        "imdbRating" FLOAT8 NOT NULL,
        "cached_omdb" BOOL NOT NULL,
        "Rated" VARCHAR,
        "Release_Date" VARCHAR,
        "Production" VARCHAR,
        "Director" VARCHAR,
        "Writer" VARCHAR,
        "Actors" VARCHAR,
        "Website" VARCHAR,
        "Poster" VARCHAR,
        "Plot" VARCHAR,
        "Language" VARCHAR,
        "Metascore" VARCHAR,
        "Rotten_Tomatoes" VARCHAR,
        "Box_Office" VARCHAR,
        "Awards" VARCHAR,
        "Genre" VARCHAR
    )
    """
)
db.commit()

for movie in movies:
    db.execute(
        """
        INSERT INTO movies ("Title", "Year", "Runtime", "imdbID", "imdbRating", "cached_omdb")
            VALUES (:Title, :Year, :Runtime, :imdbID, :imdbRating, FALSE)
        """,
        movie
    )
    db.commit()

db.close()


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

"""
Created on 2018/10/28

@author: Gong
"""
import jieba
import pandas as pd

# segment
def token(content, stopwords):
    cut_result = jieba.cut(content.strip())
    out_str = ""
    for partition in cut_result:
        if partition != "\t" and partition not in stopwords:
            out_str += partition + " "
    return out_str

file_path = "data\\"
df = pd.read_csv(file_path + "all_news.csv", encoding="utf-8")

stopwords_list = []
with open("stopwords.txt", 'r') as f:
    for line in f.readlines():
        stopwords_list.append(line.strip())

contents = df["content"].tolist()
segments = []
i = 0
tot = len(df)
for line in contents:
    segments.append(token(line, stopwords_list))
    i += 1
    if i % 10000 == 0:
        print("progress: {:.2f}%".format(i * 100 / tot))
df["content"] = segments

# save result
df.to_csv(file_path + "all_news_tokenized.csv", encoding="utf-8", index=False)
print("Done.")


import unittest
import datetime
from datawinners.accountmanagement.models import Organization
from datawinners.submission.views import process_sms_counter
from datawinners.tests.data import TRIAL_ACCOUNT_ORGANIZATION_ID
from mock import patch
from datawinners.submission.views import check_quotas_and_update_users
from datawinners.settings import NEAR_SUBMISSION_LIMIT_TRIGGER, NEAR_SMS_LIMIT_TRIGGER
from django.core import mail
from django.contrib.auth.models import Group
from django.template.loader import render_to_string
from rest_framework.authtoken.models import Token
from dateutil.relativedelta import relativedelta
from django.contrib.sites.models import Site

class TestProcessSMSCounter(unittest.TestCase):
    def setUp(self):
        self.incoming_request ={}
        self.incoming_request['outgoing_message']=''
        self.incoming_request['incoming_message']=''
        organization = Organization.objects.get(pk=TRIAL_ACCOUNT_ORGANIZATION_ID)
        active_date = datetime.datetime.today()  - relativedelta(days=3)
        organization.active_date, organization.status_changed_datetime = active_date, active_date
        organization.save()
        self.incoming_request['organization'] = organization
        self.outgoing_message = "You have reached your 50 SMS Submission limit. Please upgrade to a monthly subscription to continue sending in SMS Submissions to your projects."
        users = self.incoming_request['organization'].get_related_users()
        self.user = users[0]
        self.group = Group.objects.get(name='NGO Admins')
        self.group.user_set.add(self.user)

    def tearDown(self):
        self.group.user_set.remove(self.user)

    def test_should_go_to_next_state_when_message_limit_does_not_exceeds(self):
        self.incoming_request = process_sms_counter(self.incoming_request)
        self.assertEquals(self.incoming_request['outgoing_message'], '')

    def test_should_return_outgoing_message_when_message_limit_exceeds(self):
        message_tracker = self._get_current_message_tracker_of_organization()
        message_tracker.incoming_sms_count = 50
        message_tracker.save()
        with patch('datawinners.submission.views.get_translated_response_message') as mock_get_translated_response_message:
            self.incoming_request.update({'outgoing_message': self.outgoing_message})
            mock_get_translated_response_message.return_value = self.incoming_request
            self.incoming_request = process_sms_counter(self.incoming_request)
            self.assertEquals(self.incoming_request['outgoing_message'], self.outgoing_message)

    def _get_current_message_tracker_of_organization(self):
        current_month = datetime.date(datetime.datetime.now().year, datetime.datetime.now().month, 1)
        message_tracker = self.incoming_request['organization']._get_message_tracker(current_month)
        return message_tracker

    def test_should_return_outgoing_message_when_organization_is_deactivated(self):
        self.incoming_request['organization'].status = 'Deactivated'
        self.assertEqual(self.incoming_request['organization'].status, 'Deactivated')
        self.incoming_request = process_sms_counter(self.incoming_request)
        self.assertEquals(self.incoming_request['outgoing_message'], '')

    def test_should_send_mail_when_ngo_about_to_reach_submission_limit(self):
        token = Token.objects.get_or_create(user=self.user)[0].key
        organization = self.incoming_request.get('organization')
        site = Site.objects.get_current()
        with patch.object(Organization, "get_total_submission_count") as patch_get_total_submission_count:
            patch_get_total_submission_count.return_value = NEAR_SUBMISSION_LIMIT_TRIGGER
            check_quotas_and_update_users(organization=organization)
            site = Site.objects.get_current()
            email = mail.outbox.pop()
            self.assertEqual(['chinatwu2@gmail.com'], email.to)
            ctx = {'username':'Trial User', 'organization':organization, 'site':site,
                   'token':token}
            self.assertEqual(render_to_string('email/basicaccount/about_to_reach_submission_limit_en.html', ctx), email.body)

    def test_should_send_mail_to_when_sms_limit_is_about_to_reached(self):
        token = Token.objects.get_or_create(user=self.user)[0].key
        organization = self.incoming_request.get('organization')
        with patch.object(Organization, "get_total_incoming_message_count") as patch_get_total_incoming_message_count:
            patch_get_total_incoming_message_count.return_value = NEAR_SMS_LIMIT_TRIGGER
            check_quotas_and_update_users(organization=organization, sms_channel=True)
            site = Site.objects.get_current()
            email = mail.outbox.pop()
            self.assertEqual(['chinatwu2@gmail.com'], email.to)
            ctx = {'username':'Trial User', 'organization':organization, 'site':site,
                   'token': token}
            self.assertEqual(render_to_string('email/basicaccount/about_to_reach_sms_limit_en.html', ctx), email.body)

    def test_should_send_all_email_type_when_all_limit_are_about_to_reached(self):
        organization = self.incoming_request.get('organization')
        with patch.object(Organization, "get_total_submission_count") as patch_get_total_submission_count:
            with patch.object(Organization, "get_total_incoming_message_count") as patch_get_total_incoming_message_count:
                patch_get_total_submission_count.return_value = NEAR_SUBMISSION_LIMIT_TRIGGER
                patch_get_total_incoming_message_count.return_value = NEAR_SMS_LIMIT_TRIGGER
                check_quotas_and_update_users(organization=organization, sms_channel=True)
                expected_subjects = [u'DataWinners | 50 SMS Submission Limit Almost Reached: Upgrade to Continue Collecting Data via SMS!',
                    u'Your DataWinners Submission Limit is Approaching!']
                msgs = [mail.outbox.pop() for i in range(len(mail.outbox))]
                for msg in msgs:
                    self.assertIn(msg.subject, expected_subjects)

    def test_should_return_empty_string_for_expired_account(self):
        with patch.object(Organization, 'is_expired') as patch_is_expired:
            patch_is_expired.return_value = True
            self.assertTrue(self.incoming_request.get('organization').is_expired())
            self.incoming_request = process_sms_counter(self.incoming_request)
            self.assertEqual(self.incoming_request['outgoing_message'], "")


from sklearn.utils import shuffle
import numpy as np
import nltk
from nltk.tokenize import RegexpTokenizer
from tensorflow.keras import preprocessing
from unicodedata import normalize
import collections
from operator import itemgetter

def build_word2index(word_list):
    """
    Takes a list of (unique!) word tokens. Returns a word-index dictionary, last index is assigned to '<UNK>'.
    """
    tokenizer_dict = {}
    i = 1
    for word in word_list:
        tokenizer_dict[word] = i
        i += 1
    tokenizer_dict["<UNK>"] = i
    return tokenizer_dict

def tokenize(clean_text):
    token_list = nltk.word_tokenize(clean_text)
    return token_list

def build_vocabulary(token_list, threshold=1, with_freq=True):
    """
    Takes a list of tokens (from corpus), counts them with frequencies and returns a list
    of (word, freq) with freq >= threshold, sorted from frequent to rare. If with_freq=False,
    returns a list of words, still sorted by frequency.
    """
    token_list_Counter = collections.Counter(token_list)
    token_list_over_threshold = []
    for key, value in token_list_Counter.items():
        token_list_over_threshold.append((key, value))
    token_list_over_threshold.sort(key=itemgetter(1), reverse=True)
    if with_freq:
        token_list_over_threshold = [(word, count) for (word, count) in token_list_over_threshold if count >= threshold]
    else:
        token_list_over_threshold = [word for (word, count) in token_list_Counter if count >= threshold]
    #token_list_over_threshold = sorted(token_list_over_threshold, key=lambda x: x[0])
    #vocabulary_size = len(token_list_over_threshold) + 1
    return token_list_over_threshold

def clean(sentences):
    """
    Takes the corpus, decodes into UTF-8, tokenizes on white space, puts everything in lowercase, removes numbers (!?)
    Returns string.
    """

    clean_txt = ''
    for line in sentences:

        line = normalize('NFD', line).encode('ascii', 'ignore')
        line = line.decode('UTF-8')
        # tokenize on white space
        line = line.split()
        # convert to lowercase
        line = [word.lower() for word in line]
        # remove punctuation from each token
        #line = [word.translate(table) for word in line]
        # remove non-printable chars form each token
        #line = [re_print.sub('', w) for w in line]
        # remove tokens with numbers in them
        line = [word for word in line if word.isalpha()]
        # store as string
        clean_txt += (' '.join(line))
    return clean_txt

def tokenizer(line):
    """
    Uses nltk RefexpTokenizer to tokenize wrt alphanumerics, normalizes, decodes UTF8, ensures lower case.
    Takes a Sentence(s) string as input.
    Returns a list of token words.
    """

    tokenizer = RegexpTokenizer(r'\w+')
    line = normalize('NFD', line).encode('ascii', 'ignore')
    line = line.decode('UTF-8')
    tokenized_line = tokenizer.tokenize(line)
    tokenized_line = [word.lower() for word in tokenized_line]
    return tokenized_line

def encoder(tokenized_line, token_dict, seq_len):
    """
    Takes a list of word tokens and translates it into a list of word indices using a dictionary.
    Returns an array of token words, of shape (?, seq_len). Post padding and post truncating is applied.
    """

    encoded_line = []
    for word in tokenized_line:
        encoded_line.append(token_dict.get(word, token_dict["<UNK>"]))
    encoded_line = preprocessing.sequence.pad_sequences(
    [encoded_line],
    maxlen=seq_len,
    padding="post",
    truncating="post")

    return encoded_line

def one_hot_encode_output(encoded_sentences, sentence_length_dst=50,vocabulary_size_dst=32119):      #, vocab_size):
    curr = encoded_sentences        #encoded_sentences
    number_samples = curr.shape[0]
    decoder_targets_one_hot = np.zeros((
            number_samples,
            sentence_length_dst,
            vocabulary_size_dst+1  # hm... why?
        ))
    for i, sentence in enumerate(curr):
        for t, word in enumerate(sentence):
            decoder_targets_one_hot[i, t, word] = 1
    return decoder_targets_one_hot

# def one_hot_encode_output(encoded_sentences, sentence_length_dst=50,vocabulary_size_dst=32119):      #, vocab_size):
#     for curr in encoded_sentences:         #encoded_sentences
#         number_samples = curr.shape[0]
#         decoder_targets_one_hot = np.zeros((
#                 number_samples,
#                 sentence_length_dst,
#                 vocabulary_size_dst+1  # hm... why?
#             ))
#         for i, sentence in enumerate(curr):
#             for t, word in enumerate(sentence):
#                 decoder_targets_one_hot[i, t, word] = 1
#         yield decoder_targets_one_hot

def generator(X_data, Y_data, batch_size, samples=30000, shf=True, onehot=True):
    """generates data for the fit_generator
    input X_data, Y_data, batch_size, samples=30000
    shf=False returns the first #samples of X_data, Y_data; otherwise shuffles first.
    onehot=True applies one hot encoding to y_batch
    yields x_batch, y_batch"""
    #from sklearn.utils import shuffle
    if shf:
        x_data, y_data = shuffle(X_data, Y_data, n_samples=samples)
    else:
        x_data = X_data[0:samples]
        y_data = Y_data[0:samples]
    samples_per_epoch = x_data.shape[0]
    number_of_batches = samples_per_epoch/batch_size
    counter=0

    while True:
        x_batch = np.array(x_data[batch_size*counter:batch_size*(counter+1)]).astype('float32')
        y_batch = np.array(y_data[batch_size*counter:batch_size*(counter+1)]).astype('float32')
        if onehot:
            y_batch = one_hot_encode_output(y_batch)
        counter += 1
        yield x_batch,y_batch

        #restart counter to yield data in the next epoch as well
        if counter >= number_of_batches:
            counter = 0

def get_word(n, token_dict):
    """
    Returns the work with index n from dictionary token_dict dictionary. Super slow.
    """
    for word, index in token_dict.items():
        if index == n:
            return word
    return None

def convert(preds, token_dict):
    """
    Converts a list of indices into a list of words using token_dict dictionary.
    """
    preds_text = []
    for i in preds:
        temp = []
        for j in range(len(i)):
            t = get_word(i[j], token_dict)
            if j > 0:
                if (t == get_word(i[j-1], token_dict)) or (t == None):
                    temp.append('')
                else:
                    temp.append(t)
            else:
                    if(t == None):
                        temp.append('')
                    else:
                        temp.append(t)

        preds_text.append(' '.join(temp))
    return preds_text

def predictor(model, enc_line):
    pred = model.predict_classes(enc_line)
    pass


from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from rest_framework import permissions

class IsOwner(permissions.BasePermission):
    def has_permission(self, request, view):
        return super().has_permission(request, view)

    def has_object_permission(self, request, view, obj):
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", obj)
        if request.user == obj or request.user == "admin":
            return True
        else:
            return False
        # if :
        #     return True

        # w_u = request.user.websiteuser_set.filter(user_id=request.user.id, email_settings_id=obj.id).first()
        # if w_u:
        #     return True
        # else:
        #     if request.method.lower() == 'get':
        #         return True

        # return False


from jaeschke import t_2
from utils import readfile

bases = [2, 3, 5, 7, 11, 13, 17, 19,
         23, 29, 31, 37, 41, 43, 47, 53]

primes = readfile("primes/primes_1m.txt")

if __name__ == "__main__":
    print(3, 12)
    t_2(bases[:3], 10 ** 12, primes)


from django.urls import path
from . import views

urlpatterns = [
    path('', views.profile, name='profile'),
    path('order_history/<order_number>', views.order_history,
         name='order_history'),
    path('favorites/', views.view_favorites, name='view_favorites'),
    path('add/<product_id>/', views.add_to_favorites, name='add_to_favorites'),
]


import sys
import requests
import json
data="";
# GET
search=sys.argv[1]
res = requests.get('https://api.dictionaryapi.dev/api/v2/entries/en/'+str(search))

if str(res.status_code)=="404":
    data="404"
else:
    val=json.loads(res.text)
    data=val[0]["meanings"][0]["definitions"][0]["definition"]
f = open("./test.txt", 'w')
f.write(data)
f.close()


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/8/22
from django import forms
from app01 import models

class BookForm(forms.Form):
    title = forms.CharField(max_length=32)
    price = forms.DecimalField(max_digits=5, decimal_places=2)
    publish_date = forms.DateField(
        widget=forms.widgets.DateInput(attrs={"type": "date"})
    )
    publisher = forms.ModelChoiceField(
        queryset=models.Publisher.objects.all()
    )
    authors = forms.ModelMultipleChoiceField(
        queryset=models.Author.objects.all()
    )

class BookModelForm(forms.ModelForm):
    class Meta:
        model = models.Book
        fields = "__all__"


"""
Copyright (c) Facebook, Inc. and its affiliates.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""

import pytest
import torch
from torch import nn

import neuralcompression.functional as ncF
from neuralcompression.models import ScaleHyperprior

@pytest.mark.parametrize(
    "batch_size, dim_in, dim_out",
    [(1, 1, 1), (2, 1, 10), (3, 10, 10), (4, 10, 1), (5, 6, 7)],
)
def test_flop_count_ffn(batch_size, dim_in, dim_out):
    # Testing the flop counter counts simple modules as expected.

    ffn_model = nn.Sequential(
        nn.Linear(dim_in, dim_out, bias=False), nn.Linear(dim_out, dim_out, bias=False)
    )
    ffn_inputs = (torch.randn(batch_size, dim_in),)

    total_flops, _, unsupported_ops = ncF.count_flops(ffn_model, ffn_inputs)
    assert total_flops == batch_size * dim_in * dim_out + batch_size * dim_out * dim_out
    assert len(unsupported_ops) == 0

@pytest.mark.parametrize(
    "input_shape, layer_channels, layer_strides, layer_kernels, layer_padding",
    (
        ((1, 3, 64, 64), (10, 6, 11), (1, 2, 1), (1, 2, 3), (0, 1, 5)),
        ((3, 10, 11, 127), (2, 4, 8, 1), (2, 2, 2, 2), (4, 7, 1), (0, 2, 3, 0)),
    ),
)
def test_flop_count_conv(
    input_shape, layer_channels, layer_strides, layer_kernels, layer_padding
):
    # Testing that the flop counter counts a convolutional model as expected
    # (i.e. when compared to a reference value).

    class ConvModel(nn.Module):
        def __init__(self):
            super().__init__()
            self.layers = nn.ModuleList()
            cin = input_shape[1]

            for cout, stride, kernel, padding in zip(
                layer_channels, layer_strides, layer_kernels, layer_padding
            ):
                self.layers.append(
                    nn.Conv2d(
                        cin, cout, kernel_size=kernel, stride=stride, padding=padding
                    )
                )
                cin = cout

        def forward(self, inp):
            flops = torch.tensor(0)
            out = inp
            for layer, k_size in zip(self.layers, layer_kernels):
                cin = out.shape[1]
                out = layer(out)
                # +3 for counting the additions to the flops variable itself
                flops += out.numel() * k_size * k_size * cin + 3

            return out, flops

    model = ConvModel()
    inp = torch.randn(input_shape)
    _, correct_flops = model(inp)

    counted_flops, _, unsupported_ops = ncF.count_flops(model, (inp,))
    assert torch.allclose(torch.tensor(counted_flops).long(), correct_flops)
    assert len(unsupported_ops) == 0

@pytest.mark.parametrize("input_shape", ((10, 3), (1, 1), (5, 9), (10, 9, 8)))
def test_flop_count_elementwise(input_shape):
    # Tests that the flop counter counts flops correctly (i.e.
    # when compared to a reference value) for a model involving
    # lots of binary and elementwise operations.

    class ElementwiseModel(nn.Module):
        def forward(self, inp):
            flops = torch.tensor(0)
            out = inp

            out = out * 2
            # +1 here and below for counting the additions to the flop
            # variable itself.
            flops += out.numel() + 1

            out = out.pow(2).exp().log()
            flops += out.numel() * 3 + 1

            out = out.view(-1, 1) @ out.view(1, -1)
            flops += out.numel() + 1

            out = out + out
            flops += out.numel() + 1

            return out, flops

    model = ElementwiseModel()
    inp = torch.randn(input_shape)
    _, correct_flops = model(inp)

    counted_flops, _, unsupported_ops = ncF.count_flops(
        model, (inp,), use_single_flop_estimates=True
    )
    assert torch.allclose(torch.tensor(counted_flops).long(), correct_flops)
    assert len(unsupported_ops) == 0

@pytest.mark.parametrize(
    "batch_size, network_channels, compression_channels, img_size", [(1, 8, 16, 128)]
)
def test_flop_count_hyperprior(
    batch_size, network_channels, compression_channels, img_size
):
    # Testing that the flop counter doesn't crash and records all operators
    # on a more complicated model, i.e. the scale hyperprior model.
    model = ScaleHyperprior(
        network_channels=network_channels, compression_channels=compression_channels
    )
    inputs = (torch.randn(batch_size, 3, img_size, img_size),)
    _, _, unsupported_ops = ncF.count_flops(
        model, inputs, use_single_flop_estimates=True
    )
    assert len(unsupported_ops) == 0

def test_flop_counter_overrides():
    # Testing that one can override the counter's default implementation for
    # an operation with a custom counter function.

    class Addition(nn.Module):
        def forward(self, x, y):
            return x + y

    add_override_value = 1234567.0

    count, _, _ = ncF.count_flops(
        Addition(),
        (torch.tensor(5.0), torch.tensor(6.0)),
        counter_overrides={"aten::add": lambda inputs, outputs: add_override_value},
    )

    assert torch.allclose(
        torch.tensor(count).float(), torch.tensor(add_override_value).float()
    )


from django.template import Library
from app import models
from datetime import datetime

register = Library()

@register.filter(name='check_perm')
def check_perm(user, perm):
    try:
        user.access_level.permissions.get(identifier='organization.all')

        return True
    except models.Permission.DoesNotExist:
        try:
            user.access_level.permissions.get(identifier=perm)

            return True
        except models.Permission.DoesNotExist:
            return False

@register.filter(name='truncate')
def truncate(text):
    if len(text) > 70:
        return text[:70] + '...'
    else:
        return text

@register.filter(name='notitime')
def notitime(time):
    now = datetime.now().timestamp()
    delta = (now - time.timestamp()) / 60

    if delta < 1:
        return 'Now'
    else:
        if delta / 60 >= 1:
            if (delta / 60) / 24 >= 1:
                return (str(round((delta / 60) / 24)) + ' days ago')
            else:
                return (str(round(delta / 60)) + ' hours ago')
        else:
            return (str(round(delta)) + ' minutes ago')

@register.filter(name='exclude_self')
def exclude_self(queryset, user_object):
    buffer = queryset.exclude(id=user_object.id)
    if buffer.count() == 1:
        return queryset.exclude(id=user_object.id)[0]
    else:
        return queryset.exclude(id=user_object.id)

@register.filter(name='parse_title')
def parse_title(queryset, user_object):
    buffer = queryset.exclude(id=user_object.id)
    if buffer.count() == 1:
        return buffer[0].first_name[0] + buffer[0].last_name[0]
@register.filter(name='com_parse_name')
def parse_name(queryset, user_object):
    buffer = queryset.exclude(id=user_object.id)
    if buffer.count() == 1:
        return buffer[0].first_name + ' ' + buffer[0].last_name
@register.filter(name='com_unread')
def unread(queryset, user_object):
    return queryset.exclude(read_by__id=user_object.id).count()


import json
import sys

arg=sys.argv

with open('package.json', "r") as f:
	data=json.load(f)

# 1st argument is the version of the current package
data['version']=arg[1]

# Setting dependencies versions
if len(arg)>=4:
	for i in range (2,len(arg),2):
		data['dependencies'][arg[i]]=arg[i+1]

with open('package.json', "w") as f:
	json.dump(data,f)


"""
    Automatic Speech Recognition (ASR) abstract dataset.
"""

__all__ = ['AsrDataset', 'NemoMelSpecExtractor', 'asr_test_transform']

import numpy as np
import torch.utils.data as data
import torchvision.transforms as transforms

class AsrDataset(data.Dataset):
    """
    Automatic Speech Recognition (ASR) abstract dataset.

    Parameters:
    ----------
    root : str
        Path to the folder stored the dataset.
    mode : str
        'train', 'val', 'test', or 'demo'.
    transform : func
        A function that takes data and transforms it.
    """
    def __init__(self,
                 root,
                 mode,
                 transform):
        super(AsrDataset, self).__init__()
        assert (mode in ("train", "val", "test", "demo"))
        self.root = root
        self.mode = mode
        self.transform = transform
        self.data = []
        self.preprocessor = NemoMelSpecExtractor(dither=0.0)

    def __getitem__(self, index):
        wav_file_path, label_text = self.data[index]

        audio_data_list = self.read_audio([wav_file_path])
        x_np, x_np_len = self.preprocessor(audio_data_list)

        return (x_np[0], x_np_len[0]), label_text

    def __len__(self):
        return len(self.data)

    @staticmethod
    def read_audio(audio_file_paths,
                   desired_audio_sample_rate=16000):
        """
        Read audio.

        Parameters:
        ----------
        audio_file_paths : list of str
            Paths to audio files.
        desired_audio_sample_rate : int, default 16000
            Desired audio sample rate.

        Returns:
        -------
        list of np.array
            Audio data.
        """
        from soundfile import SoundFile

        audio_data_list = []
        for audio_file_path in audio_file_paths:
            with SoundFile(audio_file_path, "r") as data:
                sample_rate = data.samplerate
                audio_data = data.read(dtype="float32")
            audio_data = audio_data.transpose()
            if desired_audio_sample_rate != sample_rate:
                from librosa.core import resample as lr_resample
                audio_data = lr_resample(y=audio_data, orig_sr=sample_rate, target_sr=desired_audio_sample_rate)
            if audio_data.ndim >= 2:
                audio_data = np.mean(audio_data, axis=1)
            audio_data_list.append(audio_data)

        return audio_data_list

class NemoMelSpecExtractor(object):
    """
    Mel-Spectrogram Extractor from NVIDIA NEMO toolkit.

    Parameters:
    ----------
    sample_rate : int, default 16000
        Sample rate of the input audio data.
    window_size_sec : float, default 0.02
        Size of window for FFT in seconds.
    window_stride_sec : float, default 0.01
        Stride of window for FFT in seconds.
    n_fft : int, default 512
        Length of FT window.
    n_filters : int, default 64
        Number of Mel spectrogram freq bins.
    preemph : float, default 0.97
        Amount of pre emphasis to add to audio.
    dither : float, default 1.0e-05
        Amount of white-noise dithering.
    """
    def __init__(self,
                 sample_rate=16000,
                 window_size_sec=0.02,
                 window_stride_sec=0.01,
                 n_fft=512,
                 n_filters=64,
                 preemph=0.97,
                 dither=1.0e-05,
                 **kwargs):
        super(NemoMelSpecExtractor, self).__init__(**kwargs)
        self.log_zero_guard_value = 2 ** -24
        win_length = int(window_size_sec * sample_rate)
        self.hop_length = int(window_stride_sec * sample_rate)

        from scipy import signal as scipy_signal
        from librosa import stft as librosa_stft
        from librosa.filters import mel as librosa_mel

        window_fn = scipy_signal.hann(win_length, sym=True)
        self.stft = lambda x: librosa_stft(
            x,
            n_fft=n_fft,
            hop_length=self.hop_length,
            win_length=win_length,
            window=window_fn,
            center=True)

        self.dither = dither
        self.preemph = preemph

        self.pad_align = 16

        self.filter_bank = librosa_mel(
            sample_rate,
            n_fft,
            n_mels=n_filters,
            fmin=0,
            fmax=(sample_rate / 2))

    def __call__(self, xs):
        """
        Preprocess audio.

        Parameters:
        ----------
        xs : list of np.array
            Audio data.

        Returns:
        -------
        np.array
            Audio data.
        np.array
            Audio data lengths.
        """
        x_eps = 1e-5

        batch = len(xs)
        x_len = np.zeros((batch,), dtype=np.long)

        ys = []
        for i, xi in enumerate(xs):
            x_len[i] = np.ceil(float(len(xi)) / self.hop_length).astype(np.long)

            if self.dither > 0:
                xi += self.dither * np.random.randn(*xi.shape)

            xi = np.concatenate((xi[:1], xi[1:] - self.preemph * xi[:-1]), axis=0)

            yi = self.stft(xi)
            yi = np.abs(yi)
            yi = np.square(yi)
            yi = np.matmul(self.filter_bank, yi)
            yi = np.log(yi + self.log_zero_guard_value)

            assert (yi.shape[1] != 1)
            yi_mean = yi.mean(axis=1)
            yi_std = yi.std(axis=1)
            yi_std += x_eps
            yi = (yi - np.expand_dims(yi_mean, axis=-1)) / np.expand_dims(yi_std, axis=-1)

            ys.append(yi)

        channels = ys[0].shape[0]
        x_len_max = max([yj.shape[-1] for yj in ys])
        x = np.zeros((batch, channels, x_len_max), dtype=np.float32)
        for i, yi in enumerate(ys):
            x_len_i = x_len[i]
            x[i, :, :x_len_i] = yi[:, :x_len_i]

        pad_rem = x_len_max % self.pad_align
        if pad_rem != 0:
            x = np.pad(x, ((0, 0), (0, 0), (0, self.pad_align - pad_rem)))

        return x, x_len

def asr_test_transform(ds_metainfo):
    assert (ds_metainfo is not None)
    return transforms.Compose([
        transforms.ToTensor(),
    ])


#!/usr/bin/env python3
# coding: utf-8

import requests

fake_headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
    "Connection": "keep-alive",
    "DNT": "1",
    "Host": "lbs.amap.com",
    "Origin": "http://lbs.amap.com",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
}

class PublicAPI(object):
    init_link = 'http://lbs.amap.com/api/webservice/summary'

    def __init__(self):
        self.initialized = False
        self._sess = requests.Session()
        self._sess.headers.update(fake_headers)

    def _check_init(self):
        if self.initialized: return
        self._sess.get(self.init_link)

    def geo(self, address, city):
        self._check_init()

        link = "http://lbs.amap.com/dev/api"
        ret = self._sess.post(link, params = {'address': address, 'city': city}, data={'type': 'geocode/geo', 'version': 'v3'})
        if ret.status_code != 200:
            raise Exception(ret.status_code, ret.content)

        return ret.json()

    def place(self, keywords, city, types='', children=1, offset=20, page=1, extensions='all'):
        self._check_init()

        link = "http://lbs.amap.com/dev/api"
        params = {'keywords': keywords,
                    'city': city,
                    'types': types,
                    'children': children,
                    'offset': offset,
                    'page': page,
                    'extensions': extensions
                    }

        ret = self._sess.post(link, params = params, data={'type': 'place/text', 'version': 'v3'})
        if ret.status_code != 200:
            raise Exception(ret.status_code, ret.content)

        return ret.json()

    def search_city(self, pos, types='',  extensions='base'):
        self._check_init()

        link = "http://lbs.amap.com/dev/api"
        params = {'location': pos,
                    'poitype': types,
                    'radius': 1000,
                    'extensions': 'base'
                    }

        ret = self._sess.post(link, params = params, data={'type': 'geocode/regeo', 'version': 'v3'})
        if ret.status_code != 200:
            raise Exception(ret.status_code, ret.content)

        return ret.json()

def get_city(pos):
    tst = PublicAPI()
    ans = tst.search_city(pos)
    loc = ans['regeocode']['addressComponent']

    return loc['city'],loc['province'],loc['district']


# -*- coding: utf-8 -*-

import logging
from scrapy.http import Request
from scrapy.utils.request import referer_str
from scrapy.pipelines.files import FilesPipeline
from scrapy.pipelines.files import FileException
from scrapy.utils.misc import md5sum
import requests

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

logger = logging.getLogger(__name__)

class OpengazettesSlPipeline(FilesPipeline):
    def media_downloaded(self, response, request, info):
        referer = referer_str(request)

        if response.status != 200:
            logger.warning(
                'File (code: %(status)s): Error downloading file from '
                '%(request)s referred in <%(referer)s>',
                {'status': response.status,
                 'request': request, 'referer': referer},
                extra={'spider': info.spider}
            )
            raise FileException('download-error')

        if not response.body:
            logger.warning(
                'File (empty-content): Empty file from %(request)s referred '
                'in <%(referer)s>: no-content',
                {'request': request, 'referer': referer},
                extra={'spider': info.spider}
            )
            raise FileException('empty-content')

        status = 'cached' if 'cached' in response.flags else 'downloaded'
        logger.debug(
            'File (%(status)s): Downloaded file from %(request)s referred in '
            '<%(referer)s>',
            {'status': status, 'request': request, 'referer': referer},
            extra={'spider': info.spider}
        )
        self.inc_stats(info.spider, status)

        try:
            path = self.file_path(request, response=response, info=info)
            checksum = self.file_downloaded(response, request, info)
        except FileException as exc:
            logger.warning(
                'File (error): Error processing file from %(request)s '
                'referred in <%(referer)s>: %(errormsg)s',
                {'request': request, 'referer': referer, 'errormsg': str(exc)},
                extra={'spider': info.spider}, exc_info=True
            )
            raise
        except Exception as exc:
            logger.error(
                'File (unknown-error): Error processing file from %(request)s '
                'referred in <%(referer)s>',
                {'request': request, 'referer': referer},
                exc_info=True, extra={'spider': info.spider}
            )
            raise FileException(str(exc))

        return {'url': request.url, 'path': path, 'checksum': checksum }

    def get_media_requests(self, item, info):
        # catch the 302 error here, so that requests are made
        # to the correct url
        # return [Request(self.handle_redirect(x), meta=item)
        #         for x in item.get(self.files_urls_field, [])]
        return [Request(self.handle_redirect(x), meta=item)
                for x in item.get(self.files_urls_field, [])]

    def file_path(self, request, response=None, info=None):
        # start of deprecation warning block (can be removed in the future)
        def _warn():
            from scrapy.exceptions import ScrapyDeprecationWarning
            import warnings
            warnings.warn('FilesPipeline.file_key(url) method is deprecated,\
            please use file_path(request, response=None, info=None) instead',
                          category=ScrapyDeprecationWarning, stacklevel=1)

        # check if called from file_key with url as first argument
        if not isinstance(request, Request):
            _warn()
            url = request
        else:
            url = request.url

        # detect if file_key() method has been overridden
        if not hasattr(self.file_key, '_base'):
            _warn()
            return self.file_key(url)
        # end of deprecation warning block
        # Now using file name passed in the meta data
        filename = request.meta['filename']
        media_ext = '.pdf'
        return '%s/%s/%s%s' % \
            (request.meta['publication_date'].strftime("%Y"),
                request.meta['publication_date'].strftime("%m"),
                filename, media_ext)

    def file_downloaded(self, response, request, info):
        path = self.file_path(request, response=response, info=info)
        buf = BytesIO(response.body)
        checksum = md5sum(buf)
        buf.seek(0)
        self.store.persist_file(path, buf, info)
        return checksum

    # this to handle the 302 redirect error,
    # if status code is 302 return the new url
    # else use the original url
    def handle_redirect(self, file_url):
        response = requests.head(file_url)
        if response.status_code == 302:
            file_url = response.headers["Location"]
        return file_url


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

from queue import Queue
from square import Square

class Board(Gtk.Box):

  def __init__(self, maze, player):
    Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL, spacing = 0)

    self.height = maze.height * 2
    self.width = maze.width * 2
    self.maze = maze
    self.player = player

    self.square = []
    for i in range(self.height):
      self.square.append([])
      for j in range(self.width):
        if maze[i//2][j//2] == 1:
          sq = Square(Type = "player")
        else:
          sq = Square(Type = "enemy")
        self.square[i].append(sq)
        sq.update(1, 1)

    for i in range(self.height):
      box = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 0)
      for j in range(self.width):
        box.pack_start(self.square[i][j], True, True, 0)
      self.pack_start(box, True, True, 0)

    self.dist = [[-1 for i in range(self.width)] for j in range(self.height)]

  def inside(self, x, y):
    return (0 <= x and x < self.height) and (0 <= y and y < self.width)

  def __getitem__(self, i):
    return self.square[i]

  def bfs(self, s_x, s_y):
    self.dist = [[-1 for i in range(self.width)] for j in range(self.height)]

    if s_x >= 0:
      self.dist[s_x][s_y] = 0
    else:
      self.dist[-s_x][-s_y] = 0
      for i in range(self.height):
        for j in range(self.width):
          if self[i][j].Type == "enemy":
            if self[i][j].Occupant != -1:
              self.dist[i][j] = 0

    Q = Queue()
    for i in range(self.height):
      for j in range(self.width):
        if self.dist[i][j] == 0:
          Q.put([i, j])

    while not Q.empty():
      u = Q.get()
      for d in [[0, 1], [1, 0], [0, -1], [-1, 0]]:
        if self.inside(u[0] + d[0], u[1] + d[1]):
          if self.dist[u[0] + d[0]][u[1] + d[1]] == -1:
            if self[u[0] + d[0]][u[1] + d[1]].Type == "enemy":
              self.dist[u[0] + d[0]][u[1] + d[1]] = self.dist[u[0]][u[1]] + 1
              Q.put([u[0] + d[0], u[1] + d[1]])

    return True


#Kansas State University
#Electronics Design Club
#Interactive Display (Spring Open House, 2019)
#Author(s): Weston Harder

#############################################################################
#
#                               IMPORTANT!!!
#
#https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial/all
#See "SPI on Pi" "Configuration" section
#Need to enable SPI on the Pi before running this script!!!
#############################################################################

#Weston's public SSH key:
#ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDAMmVCC1zSJuSrtR1HT/uTa/eoovPUA3n6sTwzdcIJbqBEG7V6en++wL0C7Zfo4jz8+ZhlNDu9vCcrAjmJXP6LGyi4wPOdhiOBo0CY985dvl0mKFvFdf67wB7SxDa1kF1sLSN0DqGtS7ex6MiGYhqSg2AAxNJNMYG3KR9fxu6Zb3QBWfmwjP07EdnOgSs+JylN7LCu4KPvsSI+Io+z1ecltb46d1v5kIC0kHDjbM0vZvabHAQH9En6lbFY/ZPzmKuPOCghwp4pa+tSXLmwBpn4Izb82DHpQbdyqcqEk04/AG+2Fiwo4kYlm231HaY195Zuz7Y92/Mj1QrHZqzEwYSyAyOH8CZzWmffCJYnH976nl9sPLlBXJ7UDIERHBN0AAjC7uj6GPWKC57cHueJfrzdeATxh2scNY0SqsimmGc1bfGPm4aKETUfF97PeSJFip0PLTDCNp39bQeR4nS/MZxCAoSV9mtCzsLNdV6JLkRtcv24Lx7bi9SZqVzIKVvQ3G2gVAhd052LL0XIjpPvSH4JcBb1lSELHVeh+jHAO7KRs2Q9Oq5H/zExGCd8yymeYbaD8JRxLVdctS5MpWrV1AVX8ic9TbAIN/VipVfLlJQVA+U7As+KTbruNHak+7iZ7dCMGMFLXLoQKIzcEJo3g0R5hp2BQKljWIBanEvTu9mV6w== wmharder@ksu.edu

import paho.mqtt.client
import RPi.GPIO as GPIO
import spidev
from time import sleep

#GPIO pin numbers (board #, not BCM #)
CLK = 35
CS_IN = 33
RESET = 37

#SPI global variables
spi = None
#https://pypi.org/project/spidev/
#http://tightdev.net/SpiDev_Doc.pdf
#http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-9570-AT42-QTouch-BSW-AT42QT1110-Automotive_Datasheet.pdf

working_touch_ics = list()

def main():
    global CLK
    global CS_IN
    global RESET
    global spi

    #GPIO setup
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(CLK, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(CS_IN, GPIO.OUT, initial=GPIO.HIGH)
    GPIO.setup(RESET, GPIO.OUT, initial=GPIO.HIGH)

    #SPI setup
    spi = spidev.SpiDev()
    spi.open(0, 0) #Not really sure what this should be

    #Both of these come from section 4.1.2: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-9570-AT42-QTouch-BSW-AT42QT1110-Automotive_Datasheet.pdf
    spi.max_speed_hz = 10000 #Max is 1.5 MHz, limit to 1.4 MHz to be safe
    spi.mode = 0b11 # Clock polarity = 1, Clock phase = 1

    tc = touch_controller()
    mqtt_pub = mqtt_publisher()

    #Wait until connected to MQTT server
    print("Waiting")
    while not mqtt_pub.connected:
        pass
    print("Done waiting")

    old_key_states = tc.get_key_states()

    loop = 0

    #Main loop
    while(True):
        #Go through every sensor and check for touch
        #print("{} Polling working touch ICs".format(loop))
        tc.scan()

        new_key_states = tc.get_key_states()

        #print("{} Looking for differing touch states".format(loop))
        #Go through each key number
        for key, new_state in enumerate(new_key_states):
            if(new_state != old_key_states[key]):
                mqtt_pub.publish_touch(key, new_state)

        old_key_states = new_key_states

        loop += 1

    return

class mqtt_publisher(object):
    def __init__(self):
        self.host = "127.0.0.1"
        self.port = 1883
        self.topic_template = "interactive_display/touch_events/{}"
        self.connected = False
        self.mqtt_client = paho.mqtt.client.Client()
        self.mqtt_client.on_connect = self.on_connect
        self.mqtt_client.on_disconnect = self.on_disconnect
        self.connect()
        self.mqtt_client.loop_start()

        return

    def connect(self):
        print("Attempting to connect to \"{}:{}\"".format(self.host, self.port))
        self.mqtt_client.connect_async(self.host, self.port)
        return

    def on_connect(self, client, userdata, flags, rc):
        if rc==0:
            self.connected = True
            print("Connected to MQTT server successfully.")
        else:
            print("Failed to connect to the MQTT server. rc = {}".format(rc))
            print("See http://www.steves-internet-guide.com/client-connections-python-mqtt/ or https://pypi.org/project/paho-mqtt/ for help.")
            self.connect()

        return

    def on_disconnect(self, client, userdata, rc):
        self.connected = False
        print("Disconnected from the MQTT server. rc = {}".format(rc))
        print("See http://www.steves-internet-guide.com/client-connections-python-mqtt/ or https://pypi.org/project/paho-mqtt/ for help.")
        self.connect()

        return

    def publish_touch(self, key, new_state):
        if new_state:
            msg = "on"
        else:
            msg = "off"
        print("Key {} {}".format(key, msg)) #Remove this after testing is done.

        topic = self.topic_template.format(key)
        rc = self.mqtt_client.publish(topic, msg)[0]

        if rc != 0:
            print("Failed to print message \"{}\" to \"{}\". rc = {}".format(msg, topic, rc))
            print("See https://pypi.org/project/paho-mqtt/ for help.")

        return

class selection_manager(object):
    def __init__(self):
        self.touch_ic_count = 16
        self.max_touch_ic = self.touch_ic_count - 1
        self.touch_ic = 0

        self.reset()

        return

    def get_max(self):
        return self.max_touch_ic

    def get_touch_ic_count(self):
        return self.touch_ic_count

    def get_selection(self):
        return self.touch_ic

    def increment(self):
        global CLK
        global CS_IN
        global RESET

        #Set CS_IN high
        GPIO.output(CS_IN, GPIO.HIGH)
        #Set CLK high
        GPIO.output(CLK, GPIO.HIGH)
        #Wait a few ms???
        sleep(0.001)
        #Set CLK low
        GPIO.output(CLK, GPIO.LOW)
        #Wait a few ms???
        sleep(0.001)
        self.touch_ic += 1
        return

    def reset(self):
        global CLK
        global CS_IN
        global RESET

        while self.touch_ic < self.touch_ic_count:
            self.increment()

        return

    def select(self, new_touch_ic):
        global CLK
        global CS_IN
        global RESET

        #If selection is valid
        if new_touch_ic >= 0 and new_touch_ic <= self.max_touch_ic:
            #If selection is below current selection
            if new_touch_ic < self.touch_ic:
                #Select touch IC 0
                self.reset() #Not the most efficient but whatever
                #Set CS_IN low
                GPIO.output(CS_IN, GPIO.LOW)
                #Set CLK high
                GPIO.output(CLK, GPIO.HIGH)
                #Wait a few ms???
                sleep(0.001)
                #Set CLK low
                GPIO.output(CLK, GPIO.LOW)
                #Set CS_IN high
                GPIO.output(CS_IN, GPIO.HIGH)
                #Wait a few ms???
                sleep(0.001)
                self.touch_ic = 0

            #While selection is above current selection
            while new_touch_ic > self.touch_ic:
                self.increment()
            #print("Selected {}".format(new_touch_ic))
            #raw_input()
            return True

        #If selection is invalid
        else:
            self.reset()
            return False

class touch_controller(object):
    def __init__(self):
        global working_touch_ics

        self.key_count = 11
        self.key_states = list()
        self.sm = selection_manager()

        #Initialize all keyy states to False
        for _ in range(0, self.get_touch_ic_count()):
            for _ in range(0, self.key_count):
                self.key_states.append(False)

        #Setup all touch ICs
        for i in range(0, self.get_touch_ic_count()):
            success = self.control_setup(i)
            if success:
                working_touch_ics.append(i)

        #Calibrate all touch ICs
        for i in range(0, self.get_touch_ic_count()):
            self.control_calibrate(i)

        return

    #def get_touch_status()

    def control_calibrate(self, touch_ic):
        global spi
        global working_touch_ics
        if touch_ic not in working_touch_ics:
            return False

        if self.sm.select(touch_ic):
            #SPI: Send 0x03 to calibrate all keys
            spi.writebytes([0x03])

            #Wait at least 150 us before communications can resume
            sleep(150.0/1000000.0)

            return True

        else:
            return False

    def control_erase_eeprom_and_reset(self, touch_ic):
        global spi

        if self.sm.select(touch_ic):
            #SPI: Send 0x0c to erase settings stored in EEPROM and reset touch IC (revert touch IC to default settings)
            spi.writebytes([0x0c])

            #Wait at least 50 ms before communications can resume
            sleep(50.0/1000.0)

            return True

        else:
            return False

    def control_reset(self, touch_ic):
        global spi

        if self.sm.select(touch_ic):
            #SPI: Send 0x04 to reset
            spi.writebytes([0x04])

            #Wait at least 160 ms before communications can resume
            sleep(160.0/1000.0)

            return True

        else:
            return False

    def control_restore_from_eeprom(self, touch_ic):
        global spi

        if self.sm.select(touch_ic):
            #SPI: Send 0x0b to restore RAM contents from EEPROM (revert to checkpoint without needing to do a full reset)
            spi.writebytes([0x0b])

            #Wait at least 150 ms before communications can resume
            sleep(150.0/1000.0)

            return True

        else:
            return False

    def control_setup(self, touch_ic):
        global spi

        #Perform initialization on all touch ICs
            #Set the MODE bit in the Device Mode setup
            #Use a timed trigger (specified length of time or 0 for free run mode)
            #No guard channel key
            #No SYNC pin
            #Possibly use detection integrator to make sure it is touched
            #Burst length limitation??? Probably no
            #Adjacent Key Suppression??? Probably no
            #Don't use CRC checking

        if self.sm.select(touch_ic):
            #SPI: Send 0x01 to start setup
            spi.writebytes([0x01])

            setup_data = [ #Start in section 7.4 page 30: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-9570-AT42-QTouch-BSW-AT42QT1110-Automotive_Datasheet.pdf
                0b11100000, #0:  Device Mode
                0b00000010, #1:  Guard Key/Comms Options
                0b11111000, #2:  Detect Integrator Limit (DIL)/Drift Hold Time
                    #Note: Adjust these if there are false detections or not detecting
                0b01000010, #3:  Positive Threshold (PTHR)/Positive Hysteresis
                    #Note: Adjust these if there are false detections or not detecting
                0b00000110, #4:  Positive Drift Compensation (PDRIFT)
                    #Note: Just used default, adjustable
                0b00000110, #5:  Positive Recalibration Delay (PRD)
                    #Note: Just used default, adjustable
                0b00010010, #6:  Lower Burst Limit (LBL)
                    #Note: Adjust this if there are false detections or not detecting
                0b00000000, #7:  AKS Mask
                0b00000000, #8:  AKS Mask
                0b00000000, #9:  Detect0 PWM
                0b00000000, #10: Detect1 PWM
                0b00000000, #11: Detect2 PWM
                0b00000000, #12: Detect3 PWM
                0b00000000, #13: Detect4 PWM
                0b00000000, #14: Detect5 PWM
                0b00000000, #15: Detect6 PWM
                0b00000000, #16: LED Detect Hold Time
                0b00000000, #17: LED Fade/Key to LED
                0b00000000, #18: LED Latch
                0b00101010, #19: Key  0 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                    #Note: Adjust this if there are false detections or not detecting (same for keys 1-10)
                0b00101010, #20: Key  1 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #21: Key  2 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #22: Key  3 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #23: Key  4 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #24: Key  5 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #25: Key  6 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #26: Key  7 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #27: Key  8 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #28: Key  9 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00101010, #29: Key 10 Negative Threshold (NTHR) / Negative Hysteresis (NHYST)
                0b00000000, #30: Extend Pulse Time
                    #Note: No idea what this does. Left at 0.
                0b01111010, #31: Key  0 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                    #Note: Just used default, adjustable (same for keys 1-10)
                0b00000000, #32: Key  1 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #33: Key  2 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #34: Key  3 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #35: Key  4 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #36: Key  5 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #37: Key  6 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #38: Key  7 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #39: Key  8 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000, #40: Key  9 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                0b00000000  #41: Key 10 Negative Drift Compensation (NDRIFT) / Negative Recalibration Delay (NRD)
                ]

            #SPI: Send 42 bytes of setup data
            spi.writebytes(setup_data)
            #Wait 150 us
            sleep(150.0/1000000.0)
            #If concerned about setup working properly:
            #SPI: Request a dump of setup data
            print("Checking setup for touch IC {}".format(touch_ic))
            spi.writebytes([0xc8])
            received_setup_data = spi.readbytes(42)
            #Check to make sure setup data is correct
            success = True
            for index, byte in enumerate(setup_data):
                if byte != received_setup_data[index]:
                    print("Failed to setup touch IC {}".format(touch_ic))
                    print(setup_data)
                    print(received_setup_data)
                    success = False
                    break

            if success:
                print("  Success")

            #Wait at least 150 us before communications can resume
            sleep(150.0/1000000.0)

            return success

        else:
            return False

    def control_sleep(self, touch_ic):
        global spi

        if self.sm.select(touch_ic):
            #SPI: Send 0x05 to sleep
            spi.writebytes([0x05])

            #Wait at least 150 us after a low signal is applied to the SS_bar pin before communications can resume
            sleep(150.0/1000000.0)

            return True

        else:
            return False

    def control_store_to_eeprom(self, touch_ic):
        global spi

        if self.sm.select(touch_ic):
            #SPI: Send 0x0a to store RAM contents to EEPROM (basically a checkpoint that can be reached again by calling reset)
            spi.writebytes([0x0a])

            #Wait at least 200 ms before communications can resume
            sleep(200.0/1000.0)

            return True

        else:
            return False

    def get_active_keys(self):
        #Return the indices of all active (touched) keys
        return [index for index, key_state in enumerate(self.key_states) if key_state]

    def get_key_count(self):
        return self.key_count

    def get_key_state(self, key):
        return self.key_states[key]

    def get_key_states(self):
        return self.key_states[:]

    def get_touch_ic_key_state(self, touch_ic, key):
        return self.key_states[(touch_ic * self.get_key_count()) + key]

    def get_touch_ic_count(self):
        return self.sm.get_touch_ic_count()

    def report_all_keys(self, touch_ic):
        global spi
        global working_touch_ics
        if touch_ic not in working_touch_ics:
            return False

        if self.sm.select(touch_ic):
            #SPI: Send 0xc1 to request a binary report on all 11 keys
            spi.writebytes([0xc1])

            #SPI: Receive 2 bytes (byte 0 then byte 1)
            response = spi.readbytes(2)

            #           Bit 7   Bit 6   Bit 5   Bit 4   Bit 3   Bit 2   Bit 1   Bit 0
            # Byte 0                                            KEY_10  KEY_9   KEY_8
            # Byte 1    KEY_7   KEY_6   KEY_5   KEY_4   KEY_3   KEY_2   KEY_1   KEY_0

            #Parse data and store in self.key_states (For each key, store True if on/triggered and False if off/not triggered)
            touch_ic_key_states = list()
            touch_ic_key_states.append(response[1] & 0b00000001 > 0)
            touch_ic_key_states.append(response[1] & 0b00000010 > 0)
            touch_ic_key_states.append(response[1] & 0b00000100 > 0)
            touch_ic_key_states.append(response[1] & 0b00001000 > 0)
            touch_ic_key_states.append(response[1] & 0b00010000 > 0)
            touch_ic_key_states.append(response[1] & 0b00100000 > 0)
            touch_ic_key_states.append(response[1] & 0b01000000 > 0)
            touch_ic_key_states.append(response[1] & 0b10000000 > 0)
            touch_ic_key_states.append(response[0] & 0b00000001 > 0)
            touch_ic_key_states.append(response[0] & 0b00000010 > 0)
            touch_ic_key_states.append(response[0] & 0b00000100 > 0)

            for i, touch_ic_key_state in enumerate(touch_ic_key_states):
                self.key_states[(touch_ic * self.get_key_count()) + i] = touch_ic_key_state

            return True

        else:
            return False

    def scan(self):
        success = True

        for i in range(0, self.get_touch_ic_count()):
            success = self.report_all_keys(i) and success

        return success

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, HttpResponse, redirect
from .models import User
from django.contrib import messages
# Create your views here.
def index(request):
    return render(request, 'login/index.html')

def register(request):
    if request.method == 'POST':
        newUser = User.objects.add_user(request.POST)
        if newUser['status']:
            request.session["current_user"] = {
                'id': newUser['user'].id,
            }
            return redirect('wishlist:index')

        else:
            for error in newUser['errors']:
                messages.error(request, error)
            return redirect('user:index')

    else:
        return redirect('user:index')

def login(request):
    if request.method == 'POST':
        user = User.objects.check_user(request.POST)
        if user['status']:
            request.session["current_user"] = {
                'id': user['user'].id,
            }
            return redirect('wishlist:index')
        else:
            messages.error(request, user['errors'])
            return redirect('user:index')

def logout(request):
    request.session.clear()
    return redirect('user:index')