# https://leetcode.com/problems/add-two-numbers/
# memory usage: 13.8 MB, time usage: 65 ms
from typing import Optional

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def add_two_numbers(l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
    result = ListNode(0)
    result_head = result
    carry = 0
    while l1 is not None or l2 is not None or carry != 0:
        current_l1_val = l1.val if l1 else 0
        current_l2_val = l2.val if l2 else 0

        temp_sum = current_l1_val + current_l2_val + carry
        carry = temp_sum // 10

        temp_node = ListNode(temp_sum - carry * 10)
        result.next = temp_node
        result = temp_node

        l1 = l1.next if l1 else None
        l2 = l2.next if l2 else None

    return result_head.next

if __name__ == "__main__":
    l1 = ListNode(1)
    l1.next = ListNode(4)
    l1.next.next = ListNode(3)

    l2 = ListNode(5)
    l2.next = ListNode(6)
    l2.next.next = ListNode(4)

    result_node = add_two_numbers(l1, l2)

    while result_node:
        print(result_node.val)
        result_node = result_node.next


import cv2

kamera = cv2.VideoCapture(0)
kamera.set(3, 640)
kamera.set(4, 480)
face_detector = cv2.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
MAXFOTOSAY = 50
face_id = 1
print("\n [INFO] Kayıtlar başlıyor. Kameraya poz ver :)")

say = 0

while(True):
    ret, img = kamera.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    yuzler = face_detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in yuzler:
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
        say += 1
        # Yakalanan imajı veriseti klasörüne kaydet
        cv2.imwrite("veriseti1/" + str(face_id) + '.' + str(say) + ".jpg", gray[y:y+h,x:x+w])
        cv2.imshow('imaj', img)
        print("Kayıt no: ",say)
    k = cv2.waitKey(100) & 0xff
    if k == 27:
        break
    elif say >= MAXFOTOSAY:
         break
print("\n [INFO] Program sonlanıyor ve bellek temizleniyor.")
kamera.release()
cv2.destroyAllWindows()

Face Recognition with OpenCv/02_uygulama_kodlari/Python ve OpenCv ile Görüntü İşleme/veriseti/01_yuz_veriseti.py


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

import numpy as np
import pandas as pd
import multiprocessing
from scipy.stats import percentileofscore
import argparse

def concat_combination(data, window_size = 3, dname = 'cert'):

    if dname == 'cert':
        info_cols = ['sessionid','day','week',"starttime", "endtime",
                     'user', 'project', 'role', 'b_unit', 'f_unit', 'dept', 'team', 'ITAdmin',
                     'O', 'C', 'E', 'A', 'N', 'insider']

    combining_features = [ f for f in data.columns if f not in info_cols]
    info_features = [f for f in data.columns if f in info_cols]

    data_info = data[info_features].values

    data_combining_features = data[combining_features].values
    useridx = data['user'].values

    userset = set(data['user'])

    cols = []
    for shiftrange in range(window_size-1,0,-1):
        cols += [str(-shiftrange) + '_' + f for f in combining_features]
    cols += combining_features + info_features

    combined_data = []
    for u in userset:
        data_cf_u = data_combining_features[useridx == u, ]

        data_cf_u_shifted = []
        for shiftrange in range(window_size-1,0,-1):
            data_cf_u_shifted.append(np.roll(data_cf_u, shiftrange, axis = 0))

        data_cf_u_shifted.append(data_cf_u)
        data_cf_u_shifted.append(data_info[useridx==u, ])

        combined_data.append(np.hstack(data_cf_u_shifted)[window_size:,])

    combined_data = pd.DataFrame(np.vstack(combined_data), columns=cols)

    return combined_data

def subtract_combination_uworker(u, alluserdict, dtype, calc_type, window_size, udayidx, udata, uinfo, uorg):
    if u%200==0:
        print(u)

    data_out = []

    if dtype in ['day', 'week']:

        for i in range(len(udayidx)):
            t = udayidx[i]
            if dtype in ['day','week']: min_idx = min(udayidx)+window_size

            if t>=min_idx:
                if calc_type == 'meandiff':
                    prevdata = udata[(udayidx > t - 1 - window_size) & (udayidx <= t-1),]
                    if len(prevdata) < 1: continue
                    window_mean = np.mean(prevdata, axis = 0)
                    data_out.append(np.concatenate((udata[i] - window_mean, uorg[i,:], uinfo[i,:])))

                if calc_type == 'meddiff':
                    prevdata = udata[(udayidx > t - 1 - window_size) & (udayidx <= t-1),]
                    if len(prevdata) < 1: continue
                    window_med = np.median(prevdata, axis = 0)
                    data_out.append(np.concatenate((udata[i] - window_med, uorg[i,:], uinfo[i,:])))
                elif calc_type == 'percentile':
                    window = udata[(udayidx > t - 1 - window_size) & (udayidx <= t-1),]
                    if window.shape[0] < 1: continue
                    percentile_i = [percentileofscore(window[:,j], udata[i,j], 'mean') - 50 for j in range(window.shape[1])]
                    data_out.append(np.concatenate((percentile_i , uorg[i,:], uinfo[i,:])))

    if len(data_out) > 0: alluserdict[u] = np.vstack(data_out)

def subtract_percentile_combination(data, dtype, calc_type = 'percentile', window_size = 7, dname = 'cert', parallel = True):
    '''
    Combine data to generate different temporal representations
    window_size: window size by days (for CERT data)
    '''
    if dname == 'cert':
        info_cols = ['sessionid','day','week',"starttime", "endtime",
                     'user', 'project', 'role', 'b_unit', 'f_unit', 'dept', 'team', 'ITAdmin',
                     'O', 'C', 'E', 'A', 'N', 'insider','subs_ind']
        keep_org_cols = ["pc", "isworkhour", "isafterhour", "isweekday", "isweekend", "isweekendafterhour", "n_days",
                         "duration", "n_concurrent_sessions", "start_with", "end_with", "ses_start", "ses_end"]

    combining_features = [ f for f in data.columns if f not in info_cols]
    info_features = [f for f in data.columns if f in info_cols]
    keep_org_features = [f for f in data.columns if f in keep_org_cols]

    data_info = data[info_features].values
    data_org = data[keep_org_features].values
    data_combining_features = data[combining_features].values
    useridx = data['user'].values
    if dtype in ['day']: dayidx = data['day'].values
    if dname == 'cert': weekidx = data['week'].values

    userset = set(data['user'])

    if dtype == 'week':
        window_size = np.floor(window_size/7)
        idx = weekidx
    elif dtype in ['day']: idx = dayidx

    if parallel:
        manager = multiprocessing.Manager()
        return_dict = manager.dict()
        jobs = []
        for u in userset:
            udayidx = idx[useridx==u]
            udata = data_combining_features[useridx==u, ]
            uinfo = data_info[useridx==u, ]
            uorg = data_org[useridx==u, ]
            p = multiprocessing.Process(target=subtract_combination_uworker, args=(u, return_dict, dtype, calc_type,
                                                                                window_size, udayidx,
                                                                                udata, uinfo, uorg))
            jobs.append(p)
            p.start()

        for proc in jobs:
            proc.join()
    else:
        return_dict = {}
        for u in userset:
            udayidx = idx[useridx==u]
            udata = data_combining_features[useridx==u, ]
            uinfo = data_info[useridx==u, ]
            uorg = data_org[useridx==u, ]
            subtract_combination_uworker(u, return_dict, dtype, calc_type,
                                        window_size, udayidx,
                                        udata, uinfo, uorg)

    combined_data = pd.DataFrame(np.vstack([return_dict[ri] for ri in return_dict.keys()]), columns=combining_features+['org_'+f for f in keep_org_features] + info_features)

    return combined_data

if __name__ == "__main__":
    parser=argparse.ArgumentParser()
    parser.add_argument('--representation', help='Data representation to extract (concat, percentile, meandiff, mediandiff). Default: percentile',
                        type= str, default = 'percentile')
    parser.add_argument('--file_input', help='CERT input file name. Default: week-r5.2.csv.gz', type= str, default= 'week-r5.2.csv.gz')
    parser.add_argument('--window_size', help='Window size for percentile or mean/median difference representation. Default: 30',
                        type = int, default=30)
    parser.add_argument('--num_concat', help='Number of data points for concatenation. Default: 3',
                        type = int, default=3)
    args=parser.parse_args()

    print('If "too many opened files", or "ForkAwareLocal" error, run ulimit command, e.g. "$ulimit -n 10000" to increase the limit first')
    if args.representation == 'all':
        reps = ['concat', 'percentile','meandiff','meddiff']
    elif args.representation in ['concat', 'percentile','meandiff','meddiff']:
        reps = [args.representation]

    fileName = (args.file_input).replace('.csv','').replace('.gz','')
    if 'day' in fileName:
        data_type = 'day'
    elif 'week' in fileName:
        data_type = 'week'
    s = pd.read_csv(f'{args.file_input}')

    for rep in reps:
        if rep in ['percentile','meandiff','meddiff']:
            s1 = subtract_percentile_combination(s, data_type, calc_type = rep, window_size = args.window_size, dname='cert')
            s1.to_pickle(f'{fileName}-{rep}{args.window_size}.pkl')
        else:
            s1 = concat_combination(s, window_size = args.num_concat, dname = 'cert')
            s1.to_pickle(f'{fileName}-{rep}{args.num_concat}.pkl')


""" Main program for Discount Code Microservice"""
""" Developer: Fawad I Sheikh
    Date: 5-Oct-2022
"""

from db_setup import Brand, Discount_Code, brand_schema, brands_schema, discount_codes_schema, discount_code_schema
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from disc_codes import generate_dc, fetch_dc
import os
app = Flask(__name__)
base_dir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
    os.path.join(base_dir, 'loyalty.db')
db = SQLAlchemy(app)

#
# ------ Routes Definition ---------
#

@app.route("/")
def index():
    return "Welcome to Discount code microservice API "

@app.route("/brands")
def brands():
    brands_list = Brand.query.all()
    result = brands_schema.dump(brands_list)
    # print(result)
    return jsonify(result)

@app.route("/codes")
def codes():
    brand_id = request.args.get('brand_id')
    if brand_id:
        codes_list = Discount_Code.query.filter_by(brand_id=brand_id)
    else:
        codes_list = Discount_Code.query.all()

    result = discount_codes_schema.dump(codes_list)
    # print(result)
    if not result:
        return jsonify(message='No discount codes found'), 404

    return jsonify(result)

# ----- Generate discount_code  END POINT ----------
@app.route("/generate_disc_codes", methods=['GET', 'POST'])
def generate_disc_codes():
    """Generate discount codes, the endpoint expects two parameters
        brand_code = the brand code for which the codes will be generated
        count = how many discount codes to be generated. It is optional, default value is 10 if nothing is passed. """

    brand_id = request.args.get("brand_id")
    seed = 0

    if not brand_id:
        return jsonify(message='Please pass Brand id, it is required to generate discount codes'), 404

    count = request.args.get("count")
    if not count:
        count = 10
    else:
        """ determine count is a +ve number """
        try:
            count = int(count)
            if count <= 0:
                raise ValueError

        except ValueError:
            return {'message': 'Please send a positive numeric value for count'}, 401

    status = {'message': '', 'code': 0}
    codes = generate_dc(brand_id, count, status)

    if not codes:
        return jsonify(message=f'Codes not generated, please contact customer support. Error - {status["message"]}'), status['code']

    return jsonify(message=f'{count} Discount codes generated for Brand_id = {brand_id} ')

# ----- fetch_discount_code  END POINT ----------
@ app.route("/fetch_discount_code", methods=['GET', 'POST'])
def fetch_disc_code():
    """Fetch discount codes, the endpoint expects two parameters
        brand_id = the brand id for which the discount code will be returned
        user_id = the user_id requesting for discount code. """

    brand_id = request.args.get("brand_id")
    if not brand_id:
        return jsonify(message='Please pass Brand id, it is required to fetch discount code'), 404

    user_id = request.args.get("user_id")
    if not user_id:
        return jsonify(message='Please pass User id, it is required to fetch discount code'), 404

    status = {'message': '', 'code': 0}

    code = fetch_dc(brand_id, user_id, status)
    if not code:
        return jsonify(message=f'Code not fetched, please contact customer support. Error - {status["message"]}'), status['code']

    return jsonify(message=f'{code.discount_code} discount code is fetched for brand id = {brand_id}')

# -
# -
# ------ Main  ---------
if __name__ == "__main__":
    app.run(debug=True)


import argparse

def parse():
    parser = argparse.ArgumentParser(
        prog="Bilibili downloader",
        description="Download videos from bilibili.com"
    )
    subparsers = parser.add_subparsers()
    parser_d = subparsers.add_parser("download", aliases=["d"])

    parser_d.add_argument("bvid")

    args = parser.parse_args()

    return args


from typing import Generator
import praw
from praw import models
import requests
from os import environ

USER_AGENT = "MoringMarkArchiver (by u/hakiergrzonzo)"

session = requests.session()
session.headers.update({"User-Agent": USER_AGENT})

reddit = praw.Reddit(
    user_agent="MoringMarkArchiver (by u/hakiergrzonzo)",
    client_id="xd-riCdiH0xPRTRFIglbCQ",
    client_secret=environ["SECRET"],
)

def get_moring_mark() -> models.Redditor:
    return reddit.redditor("makmark")

def get_new_comics() -> Generator[models.Submission, None, None]:
    mark_mark = get_moring_mark()
    for submission in mark_mark.submissions.new(limit=None):
        if submission.subreddit.display_name != "TheOwlHouse":
            continue
        yield submission


import json
import os

import requests

import config

def create_images_generation(prompt: str, n: int = 1, size: str = '256x256') -> dict:
    '''Create images using Openai APIs.

    Parameters:
        - prompt (str): user query
        - n (int): number of generation
        - size (str): size of the generation, must be square, and the number must be divisible by 8

    Returns: dictionary with information from Openai API call
    '''
    try:
        url = f"{config.BASE_URL}/images/generations"
        payload = {
            "prompt": prompt,
            "n": n,
            "size": size
        }
        headers = {
            "Content-Type": "application/json",
            "Authorization": config.AUTHORIZATION
        }
        response = requests.request("POST", url, json=payload, headers=headers)
        response = json.loads(response.text)
        return response
    except:
        return {}

def create_images_edit(image_path: str, mask_path: str, prompt: str) -> dict:
    '''Create images edits from image and mask file, both files must be png.

    Parameters:
        - image_path (str): image file path
        - mask_path (str): mask file path
        - prompt (str): a prompt that will replace the mask in the original image

    Returns: dictionary with information from Openai API call
    '''
    try:
        image_file_name = os.path.basename(image_path)
        image_extention = image_path.split('.', 1)[-1]
        mask_file_name = os.path.basename(mask_path)
        mask_extention = mask_path.split('.', 1)[-1]
        url = f"{config.BASE_URL}/images/edits"
        files = [
            ('image', (image_file_name, open(image_path, 'rb'), f'image/{image_extention}')),
            ('mask', (mask_file_name, open(mask_path, 'rb'), f'image/{mask_extention}'))
        ]
        headers = {
            "Authorization": config.AUTHORIZATION
        }
        payload = {
            "prompt": prompt
        }
        response = requests.request(
            "POST", url, files=files, headers=headers, data=payload)
        response = json.loads(response.text)
        return response
    except:
        return {}

def create_images_variation(image_path: str, n: int = 1, size: str = '256x256') -> dict:
    '''Create variations of an image.

    Parameters:
        - image_path (str): image file path
        - n (int): number of generation
        - size (str): size of the generation, must be square, and the number must be divisible by 8

    Returns: dictionary with information from Openai API call
    '''
    try:
        file_name = os.path.basename(image_path)
        extention = image_path.split('.', 1)[-1]
        url = f"{config.BASE_URL}/images/variations"
        files = [
            ('image', (file_name, open(image_path, 'rb'), f'image/{extention}'))
        ]
        headers = {
            "Authorization": config.AUTHORIZATION
        }
        payload = {
            "n": n,
            "size": size
        }
        response = requests.request(
            "POST", url, files=files, headers=headers, data=payload)
        response = json.loads(response.text)
        return response
    except:
        return {}


import os
import sqlite3
import shutil
from lib.base_case import BaseCase
import pytest
from lib.logger import Logger

def delete_files_from_db():
    BaseCase.change_dir_to_root()
    db_name = 'FileStorage/FileStorage.db'
    conn = sqlite3.connect(db_name)
    cursor = conn.cursor()
    query = 'DROP TABLE IF EXISTS files'
    try:
        cursor.execute(query)
        conn.commit()
        return True
    finally:
        conn.close()

@pytest.fixture(scope='session', autouse=True)
def preparing_files_for_upload_and_delete_after_session():
    files = BaseCase.get_files()
    if len(files) == 0:
        raise Exception('There are no files in upload_folders')
    yield files
    # delete tests/files_for_upload/
    BaseCase.delete_upload_files(BaseCase.FILES_FOR_UPLOAD)

@pytest.fixture(scope='class', autouse=True)
def delete_files_from_server_setup(request):
    delete_files_from_db()

    def delete_uploaded_files_after_test_teardown():
        file_storage_db_path = 'FileStorage/uploaded_files'
        shutil.rmtree(file_storage_db_path)
        os.mkdir(file_storage_db_path)
        delete_files_from_db()
    request.addfinalizer(delete_uploaded_files_after_test_teardown)

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def return_test_result_in_log(item, call):
    # execute all hooks to get report object
    outcome = yield
    rep = outcome.get_result()

    # we only look for failed test calls, not setup/teardown
    if rep.when == "call" and rep.failed:
        Logger.add_test_results('Test Failed')


import os.path
import pandas as pd
import numpy as np
import env

def csv_exist():
    return os.path.isfile('zillow.csv')

def get_zillow_sql():
     return '''
        select
            bathroomcnt as bedroom_count,
            bedroomcnt as bathroom_count,
            calculatedfinishedsquarefeet as total_sqft,
            taxvaluedollarcnt as tax_property_value,
            taxamount as tax_amount,
            fips
        from `properties_2017`
        join `predictions_2017`using(`parcelid`)
        where propertylandusetypeid = 261 and `transactiondate` >= '2017-05-01' and `transactiondate` <= '2017-06-30'
    '''

def get_zillow_url():
    return f'mysql+pymysql://{env.user}:{env.password}@{env.host}/zillow'

def get_zillow_data():
    zdf = pd.read_sql(get_zillow_sql(), get_zillow_url())
    print('- CodeUp_db successfully accessed ...')
    print('- Zillow SQL query successful ...')

    return zdf

def get_zillow_csv():
    df = pd.read_table('zillow.csv')
    df.drop(columns=['Unnamed: 0'], inplace=True)
    return df

def get_fips():
    fips_df = pd.read_table('FIPS.txt')
    print('- fips_df dataframe successfully created.')
    return fips_df

def merge_dfs(zdf, fips_df):
    print('- merging zillow_df and fips_df ...')
    mdf = pd.merge(left=zdf, right=fips_df, left_on='fips', right_on='fips')
    mdf.rename(columns={'name':'county', 'state':'state'}, inplace=True)
    return mdf

def generate_csv():
    if csv_exist():
        print('- csv already exist')
    else:
        df = merge_dfs(get_zillow_data(), get_fips())
        print('- merge successful, creating zillow.csv')
        df.to_csv('zillow.csv')
        print('- zillow.csv successfully created')

def acquire_data():
    print('Acquiring data ...\n')
    generate_csv()
    print('\nData has been acquired')
    df = pd.read_csv('zillow.csv')
    df.drop(columns=['Unnamed: 0'], inplace=True)
    return df


#!/bin/env python3
import sys
import os
import os.path
import glob
import copy
import traceback
import time
import re
import csv
import tempfile
import urllib.request, urllib.parse, urllib.error
import shutil
import atexit
import subprocess
import time
from collections import defaultdict, Counter
from os.path import join, dirname, realpath, split
try:
    scriptdir = dirname(realpath(__file__))
except NameError:
    scriptdir = dirname(realpath(sys.argv[0]))
scriptdir1 = realpath(join(scriptdir, '..', '..', 'common', 'src'))
sys.path.append(scriptdir1)
try:
    scriptextn = "." + os.path.split(sys.argv[0])[1].rsplit('.', 1)[1]
except:
    scriptextn = ""

from execute import Execute
execprog = Execute(scriptdir,scriptdir1,extn=scriptextn)

from optparse_gui import OptionParser, OptionGroup, GUI, UserCancelledError, ProgressText
from util import *

from version import VERSION
VERSION = '%s' % (VERSION,)

def excepthook(etype, value, tb):
    traceback.print_exception(etype, value, tb)
    print("Type <Enter> to Exit...", end=' ', file=sys.stderr)
    sys.stderr.flush()
    input()
sys.excepthook = excepthook

toremove = []

def cleanup():
    for d in toremove:
        shutil.rmtree(d, ignore_errors=True)
atexit.register(cleanup)

if not GUI() and len(sys.argv) == 2 and sys.argv[1] == '--GUI':
    from optparse_gui.needswx import *
    sys.exit(1)

if GUI() and len(sys.argv) == 1:
    from optparse_gui import OptionParserGUI
    parser = OptionParserGUI(version=VERSION)
    error_kwargs = {'exit': False}
else:
    parser = OptionParser(version=VERSION)
    error_kwargs = {}

advanced = OptionGroup(parser, "Advanced")
parser.add_option("-s", "--snps", type="files", dest="snps", default=None,
                  help="Single-Nucleotide-Polymophisms. Required.", name="SNPs",
                  notNone=True, remember=True,
                  filetypes=[("SNPs", "*.vcf;*.csv;*.tsv;*.xls;*.xlsx;*.txt")])
parser.add_option("-j", "--junctions", type="files", dest="junctions", default=None,
                  help="Splice junctions. Required.", name="Splice Junctions",
                  notNone=True, remember=True,
                  filetypes=[("Splice Junctions (BED Format)", "*.bed")])
parser.add_option("-r", "--readalignments", type="files", dest="alignments", default=None,
                  help="Read alignments in BAM/SAM format. Required.", name="Read Alignments",
                  notNone=True, remember=True,
                  filetypes=[("Read Alignments (BAM/SAM Format)", "*.bam;*.sam")])
advanced.add_option("-e", "--exoncoords", type="file", dest="exoncoords", default=None,
                  help="Exon coordinates for SNV filtering. Optional.", name="Exon Coords.",
                  remember=True,
                  filetypes=[("Exonic Coordinates", "*.txt")])
advanced.add_option("-d", "--distance", type="int", dest="dist", default=50, remember=True,
                    help="Upper bound on the distance between SNP locus and splice junction. Default: 50.",
                    name="Distance Bound")
advanced.add_option("-R", "--readthrough", type="int", dest="readthrough", default=5, remember=True,
                    help="Number of bases aligning into intron. Default: 5bp.",
                    name="Read-Through")
# advanced.add_option("-M", "--matepairs", action="store_true", dest="mates", default=False, remember=True,
#                     help="Consider the mate-pair reads for the detection of splicing. Default=False.", name="Mates")
advanced.add_option("-F", "--full", action="store_true", dest="full", default=False, remember=True,
                    help="Output extra diagnostic read count fields. Default=False.", name="All Fields")
advanced.add_option("-f", "--alignmentfilter", action="store_false", dest="filter", default=True, remember=True,
                    help="(Turn off) alignment filtering by length, edits, etc.", name="Filter Alignments")
advanced.add_option("-U", "--uniquereads", action="store_true", dest="unique", default=False, remember=True,
                    help="Consider only distinct reads.", name="Unique Reads")
advanced.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, remember=True,
                    help="Quiet.", name="Quiet")
parser.add_option("-o", "--output", type="savefile", dest="output", remember=True,
                  help="Output file. Leave empty for console ouptut.", default="",
                  name="Output File", filetypes=[("All output formats", "*.xlsx;*.xls;*.csv;*.tsv;*.txt"),
                                                 ("Excel", "*.xlsx"), ("Excel2003", "*.xls"),
                                                 ("CSV", "*.csv"), ("TSV", "*.tsv"), ("Text", "*.txt")])
parser.add_option_group(advanced)

opt = None
while True:
    if 'exit' in error_kwargs:
        try:
            opt, args = parser.parse_args(opts=opt)
        except UserCancelledError:
            sys.exit(0)
    else:
        opt, args = parser.parse_args()

    break

opts.mates = False

progress = None
if not opt.output:
    opt.quiet = True
progress = ProgressText(quiet=opt.quiet)

import pysam
from dataset import XLSFileTable, CSVFileTable, TSVFileTable, XLSXFileTable, TXTFileTable, BEDFile, VCFFile

progress.stage("Read SNP data", len(opt.snps))
snpheaders = [_f for _f in """
CHROM POS REF ALT
""".split() if _f]

snvdata = {}
snvchroms = defaultdict(set)
extrasnpheaders = []
usedsnpheaders = set()
for filename in opt.snps:
    filename0 = filename
    base, extn = filename.rsplit('.', 1)
    extn = extn.lower()
    tempfilename = None
    if opt.exoncoords:
        if extn != 'vcf':
            extn = 'tsv'
        tempfileno,tempfilename = tempfile.mkstemp(suffix="."+extn)
        execprog.execute("exonicFilter", "--exons", opt.exoncoords,
                         "--input", filename, "--output", tempfilename)
        filename = tempfilename

    if extn == 'csv':
        snps = CSVFileTable(filename=filename)
    elif extn == 'vcf':
        snps = VCFFile(filename=filename)
    elif extn == 'tsv':
        snps = TSVFileTable(filename=filename)
    elif extn == 'xls':
        snps = XLSFileTable(filename=filename)
    elif extn == 'xlsx':
        snps = XLSXFileTable(filename=filename)
    elif extn == 'txt':
        snps = TXTFileTable(filename=filename, headers=snpheaders)
    else:
        raise RuntimeError("Unexpected SNP file extension: %s" % filename)

    for h in snpheaders:
        if h not in snps.headers():
            raise RuntimeError(
                "Required header: %s missing from SNP file %s" % (h, filename))

    for h in snps.headers():
        if h in snpheaders:
            continue
        if h not in extrasnpheaders:
            extrasnpheaders.append(h)

    for r in snps:
        chr = r[snpheaders[0]]
        snvchroms[filename0].add(chr)
        locus = int(r[snpheaders[1]])
        ref = r[snpheaders[2]]
        alt = r[snpheaders[3]]
        if r.get('INFO:INDEL'):
            continue
        if len(ref) != 1:
            continue
        if not re.search(r'^[ACGT](,[ACGT])*$', alt):
            continue
        for h in r:
            if r.get(h):
                usedsnpheaders.add(h)
        # cannonr = (",".join(map(lambda t: "%s:%s" % t, sorted(r.items()))))
        snpkey = (filename0, chr, locus, ref, alt)
        if snpkey not in snpdata:
            snpdata[snpkey] = r

    if tempfilename != None:
        os.unlink(tempfilename)

    progress.update()
progress.done()

chrreg = ChromLabelRegistry()

for snvfile in snvchroms:
    chrreg.add_labels(snvfile,snvchroms[snvfile])

snpdata1 = {}
for (sf, chr, locus, ref, alt), r in snvdata.items():
    chrom = chrreg.label2chrom(sf,chr)
    assert(chrom)
    snvkey = (chrom,locus,ref,alt)
    if snvkey not in snvdata1:
        snvdata1[snvkey] = (chrom,locus,ref,alt,r)

for bamfile in opt.alignments:
    chrreg.add_bamlabels(bamfile)

progress.stage("Read splice junction data", len(opt.junctions))
juncdata = set()
juncchrom = defaultdict(set)
for juncfile in opt.junctions:
    junc = BEDFile(filename=juncfile)
    for r in junc:
        chr = r['chrom']
        juncchrom[juncfile].add(chr)
        st = int(r['chromStart'])
        ed = int(r['chromEnd'])
        bs = list(map(int, r['blockSizes'].split(',')))
        assert(len(bs) == 2)
        gap = (st + bs[0], ed - bs[1])
        key = (juncfile, chr, gap)
        juncdata.add(key)
    progress.update()
progress.done()

for juncfile in juncchroms:
    chrreg.add_labels(juncfile,juncchroms[juncfile])

juncdata1 = set()
for jf, chr, gap in juncdata:
    chrom = chrreg.label2chrom(jf,chr)
    juncdata1.add((chrom, gap))

chrreg.determine_chrom_order()

snpdata = sorted(list(snvdata1.values()),key=lambda s: (chrreg.chrom_order(s[0]),s[1],s[2],s[3]))

extrasnpheaders = [h for h in extrasnpheaders if h in usedsnpheaders]
progress.message("SNPs: %d" % len(snpdata))

juncdata = sorted(((chr, gap[i], gap) for i in (0, 1) for chr, gap in juncdata1), key=lambda j: (chrreg.chrom_order(j[0]),j[1],j[2]))
progress.message("Exon/Intron junctions: %d" % len(juncdata))

outheaders = snpheaders + [_f for _f in """
NumofJuncs
Distance
Junctions
SNPJuncIntronCount
SNPJuncNoIntronCount
NoSNPJuncIntronCount
NoSNPJuncNoIntronCount
Probability
LOD
P-Value
Bonferroni
FDR
%BadRead
""".split() if _f]

debugging = [_f for _f in """
SNPMateCount
NoSNPMateCount
SNPCount
NoSNPCount
MatesCount
NotMatesCount
IntronCount
NoIntronCount
SpanningReads
RemovedDuplicateReads
FilteredReads
SNPLociReads
""".split() if _f]
debugging.extend(sorted(BadRead.allheaders))

outheaders.extend(debugging)

pos = outheaders.index("NumofJuncs")
for h in reversed(extrasnpheaders):
    outheaders.insert(pos, h)

outheaders1 = copy.copy(outheaders)
if not opt.full:
    for dh in debugging:
        if dh in outheaders1:
            outheaders1.remove(dh)

if not opt.mates:
    for mh in "SNPMateCount NoSNPMateCount MatesCount NotMatesCount".split():
        if mh in outheaders1:
            outheaders1.remove(mh)

emptysym = None
if opt.output:
    filename = opt.output
    base, extn = filename.rsplit('.', 1)
    extn = extn.lower()
    if extn == 'csv':
        output = CSVFileTable(filename=filename, headers=outheaders1)
    elif extn == 'tsv':
        output = TSVFileTable(filename=filename, headers=outheaders1)
    elif extn == 'xls':
        output = XLSFileTable(
            filename=filename, headers=outheaders1, sheet='Results')
    elif extn == 'xlsx':
        output = XLSXFileTable(
            filename=filename, headers=outheaders1, sheet='Results')
    elif extn == 'txt':
        output = TXTFileTable(filename=filename, headers=outheaders1)
    else:
        raise RuntimeError("Unexpected output file extension: %s" % filename)
else:
    output = TXTFileTable(filename=sys.stdout, headers=outheaders1)
    emptysym = "-"

outrows = []

from fisher import fisher_exact, bonferroni, fdr, lod
pvalues = []
if opt.filter:
    alifilter = SNPPileupReadFilter(maxsegments=2)
    matefilter = ReadFilter(maxsegments=2)
else:
    alifilter = BasicFilter()
    matefilter = BasicFilter()

if opt.tpb == 0:
    pileups = SerialPileups(snpdata, opt.alignments, alifilter, chrreg, matedist=opt.dist).iterator()
else:
    pileups = MultiprocPileups(snpdata, opt.alignments, alifilter, chrreg, procsperbam=opt.tpb, matedist=opt.dist).iterator()

juncindst = 0
progress.stage("Count reads per SNP and splice junction", len(snpdata))
for snpchr, snppos, ref, alt, snpextra in snpdata:

    if juncindst < len(juncdata):
        juncchr, juncpos, (inst, ined) = juncdata[juncindst]
        while (juncchr < snpchr) or ((juncchr == snpchr) and (juncpos < (snppos - opt.dist))):
            juncindst += 1
            if juncindst >= len(juncdata):
                break
            juncchr, juncpos, (inst, ined) = juncdata[juncindst]

    juncinded = juncindst

    if juncinded < len(juncdata):
        juncchr, juncpos, (inst, ined) = juncdata[juncinded]
        while (juncchr < snpchr) or ((juncchr == snpchr) and (juncpos < (snppos + opt.dist))):
            juncinded += 1
            if juncinded >= len(juncdata):
                break
            juncchr, juncpos, (inst, ined) = juncdata[juncinded]

    njunc = (juncinded - juncindst)

    snvchr1, snvpos1, ref1, alt1, total, reads, badread = next(pileups)
    assert(snvchr == snvchr1 and snvpos == snvpos1)

    reads = []
    total = 0
    badread = Counter()
    if njunc > 0:
        snppos1 = snppos - 1
        for i, samfile in enumerate(samfiles):
            for pileupcolumn in samfile.pileup(snpchr, snppos1, snppos1 + 1, truncate=True):
                total += pileupcolumn.n
                for pileupread in pileupcolumn.pileups:
                    try:
                        al, pos, base, nseg = alifilter.test(pileupread)
                    except BadRead as e:
                        badread[e.message] += 1
                        continue
                    reads.append((al, pos, base, i, nseg, None))
                    badread['Good'] += 1

    if opt.mates:
        matedreads = []
        for r in reads:
            read, qpos, base, sind, nseg, dummy = r
            if read.is_paired and not read.mate_is_unmapped and nseg == 1 and \
                    read.tid == read.rnext and abs(read.pos - read.pnext) < opt.dist - read.qlen:
                mate = samfiles[sind].mate(read)
                try:
                    matefilter.test(mate)
                    matedreads.append((read, qpos, base, sind, nseg, mate))
                except BadRead as e:
                    matedreads.append((read, qpos, base, sind, nseg, None))
            else:
                matedreads.append((read, qpos, base, sind, nseg, None))
        reads = matedreads

    any = False
    for juncind in range(juncindst, juncinded):
        juncchr, juncpos, (inst, ined) = juncdata[juncind]

        # If intron is to the left of the SNP, we require the query
        # to read into (or over) the intron by some number of bases
        goodreads = defaultdict(list)
        if juncpos == ined:
            for al, pos, base, si, ns, mal in reads:
                if (ined + opt.readthrough) < al.aend and (al.aend - al.qlen) < (ined - opt.readthrough):
                    goodreads[base].append((si, al, False))
                elif mal and (ined + opt.readthrough) < mal.aend and (mal.aend - mal.qlen) < (ined - opt.readthrough):
                    goodreads[base].append((si, mal, True))
        elif juncpos == inst:
            for al, pos, base, si, ns, mal in reads:
                if al.pos < (inst - opt.readthrough) and (inst + opt.readthrough) < (al.pos + al.qlen):
                    goodreads[base].append((si, al, False))
                elif mal and mal.pos < (inst - opt.readthrough) and (inst + opt.readthrough) < (mal.pos + mal.qlen):
                    goodreads[base].append((si, mal, True))

        if len(goodreads) == 0:
            continue

        any = True

        # Deduplicate the reads based on the read sequence or the
        # start and end of the alignment or ???
        duplicates_removed = 0
        if opt.unique:
            for base in goodreads:
                seen = set()
                retain = list()
                for si, al, mate in goodreads[base]:
                    if (si, al.seq) not in seen:
                        retain.append((si, al, mate))
                        seen.add((si, al.seq))
                    else:
                        duplicates_removed += 1
                goodreads[base] = retain

        # goodreads now contains the relevant read alignments.
        # detect read-through of splice site by checking the
        # alignment span on the reference.
        # if len(goodreads[ref]) < 5 or len(goodreads[alt]) < 5:
        #     continue
        spliced = 'SPLICED'
        notspliced = 'NOTSPLICED'
        other = 'OTHER'
        all = 'ALL'
        mates = 'MATES'
        notmates = 'NOTMATES'
        counts = defaultdict(int)
        for base in goodreads:
            for si, al, mate in goodreads[base]:
                if abs(al.alen - al.qlen) < 2:
                    counts[(base, notspliced)] += 1
                elif abs(al.alen - al.qlen - (ined - inst)) < 2:
                    counts[(base, spliced)] += 1
                else:
                    counts[(base, other)] += 1
                if mate:
                    counts[(base, mates)] += 1
                else:
                    counts[(base, notmates)] += 1
            counts[(base, all)] = counts[(base, notspliced)] + \
                counts[(base, spliced)] + \
                counts[(base, other)]
            counts[notspliced] += counts[(base, notspliced)]
            counts[spliced] += counts[(base, spliced)]
            counts[other] += counts[(base, other)]
            counts[all] += counts[(base, all)]
            counts[mates] += counts[(base, mates)]
            counts[notmates] += counts[(base, notmates)]

        nsnpi = sum([counts[(nuc, notspliced)] for nuc in alt.split(',')])
        nsnpex = sum([counts[(nuc, spliced)] for nuc in alt.split(',')])
        nwti = counts[(ref, notspliced)]
        nwtex = counts[(ref, spliced)]
        p = emptysym
        pval = emptysym
        lodval = emptysym
        if (nsnpi + nsnpex) > 0 and \
           (nwti + nwtex) > 0 and \
           (nsnpi + nwti) > 0:

            psnp = nsnpi / float(nsnpi + nsnpex)
            pwt = nwti / float(nwti + nwtex)
            p = psnp / (psnp + pwt)

            pval = fisher_exact(nsnpi,
                                (nsnpi + nsnpex),
                                (nsnpi + nwti),
                                (nsnpi + nsnpex + nwti + nwtex))

            pvalues.append(pval)

            l = lod(nsnpi, (nsnpi + nsnpex), (nsnpi + nwti),
                    (nsnpi + nsnpex + nwti + nwtex))
            if l != None:
                lodval = l

        row = [ snpchr, snppos, ref, alt ] + \
              [ snpextra.get(k, emptysym) for k in extrasnpheaders ] + \
              [njunc,
               juncpos - snppos + 1 * (juncpos > snppos),
               "%s:%d-%d" % (juncchr, inst, ined),
               counts[(alt, notspliced)],
               counts[(alt, spliced)],
               counts[(ref, notspliced)],
               counts[(ref, spliced)],
               p,
               lodval,
               pval,
               100.0 * (total - len(reads)) / float(total),
               counts[(alt, mates)],
               counts[(ref, mates)],
               counts[(alt, all)],
               counts[(ref, all)],
               counts[mates],
               counts[notmates],
               counts[notspliced],
               counts[spliced],
               counts[all],
               duplicates_removed,
               len(reads),
               total]
        for s in sorted(BadRead.allheaders):
            row.append(badread[s])
        outrows.append(row)

    if not any:

        # ["%s:%d_%s/%s"%(snpchr,snppos,ref,alt)] \
        outrows.append([snpchr, snppos, ref, alt]
                       + [snpextra.get(k, emptysym) for k in extrasnpheaders]
                       + [0])

    progress.update()
progress.done()

progress.stage('Multiple-test correction and FDR computation')
bonf = bonferroni(pvalues)
fdr = fdr(pvalues)

i = 0
pvalpos = outheaders.index('P-Value')
for r in outrows:
    if len(r) > pvalpos:
        if r[pvalpos] != emptysym:
            r.insert(pvalpos + 1, fdr[i])
            r.insert(pvalpos + 1, bonf[i])
            i += 1
        else:
            r.insert(pvalpos + 1, emptysym)
            r.insert(pvalpos + 1, emptysym)
progress.done()

progress.stage('Output results')
output.from_rows(
    [dict(list(zip(outheaders, r + [emptysym] * 50))) for r in outrows])
progress.done()


# Your program asks the user for the subtotal but does not ask the user for the day of the week. Your program gets the day of the week from your computer's operating system.
# Your program correctly computes and prints the discount amount if applicable.
# Your program correctly computes and prints the sales tax amount and the total amount due.

from datetime import datetime

subtotal =0
price = -1
print('Please enter the price and quantity of each item. (enter a quantity of 0 to finish)')
while price != 0:
    price = float(input('What is your items price? $'))
    if(price == 0):
        break
    quantity = int(input('How many of this item? '))
    subtotal += (price * quantity)

print(f'Your subtotal is ${subtotal:.2f}')

day_of_the_week = datetime.now().strftime("%A")
sub_discount = 0
if ( day_of_the_week == 'Tuesday' or day_of_the_week == 'Wednesday'):

    if subtotal > 50 :
        discount = 0.1
        sub_discount = subtotal * discount
        print(f'Discount is ${sub_discount:.2f}')
    else:
        additional_amount = 50-subtotal

        print(f'To recive a 10% discount please spend an additional ${additional_amount:.2f}')

sales_tax = 0.06
tax_total = (subtotal - sub_discount) * sales_tax
print(f'Sales tax is ${tax_total:.2f}')
total_amount = subtotal - sub_discount+tax_total

print(f'Your total amount due is ${total_amount:.2f}')


"""汎用関数"""
from datetime import datetime
from pathlib import Path

def simple_format_time(duration):
    """
    秒数をHH時間MM分SS秒にフォーマットする
    """
    hours, remainder = divmod(duration, 3600)
    minutes, seconds = divmod(remainder, 60)
    return f"{int(hours)}時間{int(minutes)}分{int(seconds)}秒"

def create_output_directory(base_output_path: Path) -> Path:
    """
    指定したパスの配下に出力ディレクトリを作成

    出力ディレクトリはYYYYMMDDHHMMSS形式
    中間ディレクトリは自動で作成される
    """
    datetime_str = datetime.now().strftime("%Y%m%d%H%M%S")
    output_path = base_output_path / datetime_str
    output_path.mkdir(parents=True, exist_ok=True)
    return output_path


import argparse
from datetime import datetime, timedelta
from pathlib import Path

parser = argparse.ArgumentParser(
    description="Create log folders (YYYY/MM/DD) for N days into the future."
)
parser.add_argument(
    'num_days', type=int, help="Create folders for this many days"
)

num_days = parser.parse_args().num_days
dates = [datetime.now() + timedelta(days=i) for i in range(num_days)]

def fmt(n):
    return f'{n:02d}'

for date in dates:
    year, month, day = map(fmt, (date.year, date.month, date.day))
    p = Path.cwd().joinpath(year, month, day)
    p.mkdir(exist_ok=True, parents=True)
    p.parent.parent.chmod(0o2755)
    p.parent.chmod(0o2755)
    p.chmod(0o3777)


#pixelise an image. turning image into numbers in list

import cv2
import numpy as np

img_path = 'onechinese.png'
img = cv2.imread(img_path, 0)
img_reverted = cv2.bitwise_not(img) #255 is black and 0 is white as it is inverted
#print(img[50])

s = 1
ws = 4
p = 2

from matplotlib import pyplot as plt
plt.imshow(img_reverted, interpolation='nearest')
plt.show()

#force images of different resolutions into the same resolution

def compress(img_reverted, s, ws):
    compressedList = []

    lenImg = len(img_reverted)
    #print("////////////////")
    #print(lenImg)
    for j in range(0,lenImg -ws, s):
        listOfRow = []

        for i in range(0,lenImg-ws, s):
            sumOfPixels = 0
            for k in range(i,i + ws):
                for x in range(j,j + ws):
                    sumOfPixels = sumOfPixels + img_reverted[x][k]

            avg = sumOfPixels/(ws**2)
            listOfRow.append(avg)
            #print(len(img_reverted))

        compressedList.append(listOfRow)

    return compressedList

#print(compress(img_reverted, s, ws))
s = 4
ws = 8
compressedImg = compress(img_reverted, s, ws)

def compressMaxPooling(img_reverted, s, ws):
    compressedList = []

    lenImg = len(img_reverted)
    #print("////////////////")
    #print(lenImg)
    for j in range(0,lenImg -ws, s):
        listOfRow = []

        for i in range(0,lenImg-ws, s):
            sumOfPixels = 0
            for k in range(i,i + ws):
                for x in range(j,j + ws):
                    sumOfPixels = sumOfPixels + img_reverted[x][k]
                    value = []
                    value.append(img_reverted[x][k])
                    max1 = 0
                    lenIMG = len(value)
                    for i in range(0,lenIMG):
                        if max1 < value[i]:
                            max1 = value[i]

            listOfRow.append(max1)
            #print(len(img_reverted))

        compressedList.append(listOfRow)

    return compressedList

compressMaxPooling = compressMaxPooling(img_reverted, s, ws)
print(compressMaxPooling)

def padding(img_reverted, p):

    paddedList = []
    listOfZeros = []
    for b in range(0,len(img_reverted) + 2*p):
        listOfZeros.append(0)
    for g in range(0,len(img_reverted)):
        newList = img_reverted[g]
        for pi in range(0,p):
            newList = np.insert(newList,0,0)
            newList = np.append(newList, 0)
        newList = newList.tolist()
        paddedList.append(newList)

    for pi in range(0,p):
        paddedList.insert(0,listOfZeros)
        paddedList.append(listOfZeros)

    return paddedList

def paddingHorizontally(img_reverted, p):

    paddedList = []
    for g in range(0,len(img_reverted)):
        newList = img_reverted[g]
        for pi in range(0,p):
            newList = np.insert(newList,0,0)
            newList = np.append(newList, 0)
        newList = newList.tolist()
        paddedList.append(newList)

    return paddedList

def paddingVertically(img_reverted, p):

    paddedList = []
    listOfZeros = []
    for b in range(0,len(img_reverted)):
        listOfZeros.append(0)

    for pi in range(0,p):
        paddedList.insert(0,listOfZeros)
        paddedList.append(listOfZeros)

    return paddedList

def paddingHorizontallyLeft(img_reverted, p):

    paddedList = []
    for g in range(0,len(img_reverted)):
        newList = img_reverted[g]
        for pi in range(0,p):
            newList = np.insert(newList,0,0)

        newList = newList.tolist()
        paddedList.append(newList)

    return paddedList

def paddingHorizontallyRight(img_reverted, p):

    paddedList = []
    for g in range(0,len(img_reverted)):
        newList = img_reverted[g]
        for pi in range(0,p):

            newList = np.append(newList, 0)
        newList = newList.tolist()
        paddedList.append(newList)

    return paddedList

def paddingVerticallyTop(img_reverted, p):

    paddedList = []
    listOfZeros = []
    for b in range(0,len(img_reverted)):
        listOfZeros.append(0)

    for pi in range(0,p):
        paddedList.insert(0,listOfZeros)

    return paddedList

def paddingVerticallyBottom(img_reverted, p):

    paddedList = []
    listOfZeros = []
    for b in range(0,len(img_reverted)):
        listOfZeros.append(0)

    for pi in range(0,p):

        paddedList.append(listOfZeros)

    return paddedList

def SquaringImage(img_reverted):
    horizontalResolution = len(img_reverted[0])
    verticalResolution = len(img_reverted)

    if horizontalResolution == verticalResolution:
        return img_reverted
    else:
        if abs(horizontalResolution - verticalResolution) % 2 == 0:
            #eg. 8 by 10 img, we know how to pad

            if horizontalResolution > verticalResolution:
                difference = horizontalResolution - verticalResolution
                pad = difference / 2
                p = int(pad)
                return paddingVertically(img_reverted, p)

            elif verticalResolution > horizontalResolution:
                difference = verticalResolution - horizontalResolution
                pad = difference / 2
                p = int(pad)
                return paddingHorizontally(img_reverted, p)

        else:
            #eg. 7 by 10 img, tricky to pad

            if horizontalResolution > verticalResolution:
                difference = horizontalResolution - verticalResolution
                pad = difference / 2
                largeP = int(pad)
                smallP = difference - largeP
                img_reverted = paddingVerticallyTop(img_reverted, largeP)
                img_reverted = paddingVerticallyBottom(img_reverted, smallP)
                return img_reverted

            elif horizontalResolution < verticalResolution:
                difference = verticalResolution - horizontalResolution
                pad = difference / 2
                largeP = int(pad)
                smallP = difference - largeP
                img_reverted = paddingHorizontallyLeft(img_reverted, largeP)
                img_reverted = paddingHorizontallyRight(img_reverted, smallP)
                return img_reverted

paddedImg = padding(img_reverted, p)
lenPadded = len(paddedImg)
lenPaddedX = len(paddedImg[0])
#print(lenPadded)
#print(lenPaddedX)
for v in range(0,lenPadded):
    for w in range(0,lenPaddedX):
        paddedImg[v][w] = np.float64(paddedImg[v][w])

plt.imshow(compressedImg, interpolation='nearest')
plt.show()

#print(type(paddedImg[0][0]))

plt.imshow(paddedImg, interpolation='nearest')
plt.show()

"""Make image of any size into a square image of a particular size"""
resolutionNew = 100
def resize(img_reverted, resolutionNew = 100):

    #pad first
    img_squared = SquaringImage(img_reverted)

    widthOfImg = len(img_squared)
    print(widthOfImg)
    #width is 81
    lengthOfImg = len(img_squared[0])
    print(lengthOfImg)
    #length is 101

    if lengthOfImg != widthOfImg:
        print("Error! Lenght of image does not equal to width of image")
    else:
        #50 = 101 - ws / s  +1

        #rn = ro - ws +2p/s +1

        #ws = -(rn - 1)*s + Ro

        s = 1

        ws = -(resolutionNew - 1)*s + lengthOfImg
        ws = int(ws)
        print("/////////////////////////////////////////////")
        print(ws)
        resizedList = []

        resizedList = compress(img_squared, s, ws)

        return resizedList

resizedList = resize(img_reverted, resolutionNew)
#print(resizedList)

lenResized = len(resizedList)
lenResizedX = len(resizedList[0])
print("/////////////////////////////////////////////")
print(lenResized)
print(lenResizedX)
print("/////////////////////////////////////////////")
for v in range(0,lenResized):
    for w in range(0,lenResizedX):
        resizedList[v][w] = np.float64(resizedList[v][w])

plt.imshow(resizedList, interpolation='nearest')
plt.show()


'''
This program allows you to measure the speed of the incoming and outgoing speed of your Internet connection.

Author: Fora1ds
Version: 1.0.0
'''

from urllib.request import Request, urlopen
from urllib.parse import urlparse
from Exceptions import *
from json import loads
from time import time

SERVERS_URL = 'https://www.speedtest.net/api/js/servers'
DOWNLOAD_URL = '/download?size=25000000'
UPLOAD_URL = '/upload'

class Format:
    def __init__(self, round_byte=True, units=True, digits=3):
        self.Round = round_byte
        self.Units = units
        self.Digits = digits

    def set_type(self, byte_value, units) -> [float, str]:
        '''
        Set a new byte type:

        1. 4836849.75 -> 38.694 Mbps
        2. 4836849.75 -> 38.694
        3. 4836849.75 -> 39
        '''

        Value = byte_value

        if self.Round == True:
            Value = round(Value, self.Digits)

        if self.Units == True:
            Value = '{} {}'.format(Value, units)

        return Value

    def to_Bps(self, value) -> [float, str]:
        '''
        Convert bytes to Bps
        '''

        return self.set_type(byte_value=value / 0.125, units='Bps')

    def to_Kbps(self, value) -> [float, str]:
        '''
        Convert bytes to Kbps
        '''

        return self.set_type(byte_value=value / 125, units='Kbps')

    def to_Mbps(self, value) -> [float, str]:
        '''
        Convert bytes to Mbps
        '''

        return self.set_type(byte_value=value / 125000, units='Mbps')

    def to_Gbps(self, value) -> [float, str]:
        '''
         Convert bytes to Gbps
        '''

        return self.set_type(byte_value=value / 125000000, units='Gbps')

    def to_Tbps(self, value) -> [float, str]:
        '''
        Convert bytes to Tbps
        '''

        return self.set_type(byte_value=value / 125000000000, units='Tbps')

class SpeedTest:
    def __init__(self):
        # Standard Ookla packet size
        self.PACKET_SIZE = 25000000

        self.select_server(number=0)

    def _request(self, url, content=None, url_method='GET') -> bytes:
        '''
        Sending a request with headers, data and method
        '''

        Url = Request(url, data=content,
            headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0',
                     'Accept': 'application/json, text/plain, */*',
                     'Accept-Language': 'en-US,en;q=0.5'}, method=url_method)

        try:
            URL_Request = urlopen(Url).read()
        except :
            raise NoConnectionError(message='Failed to connect to {}'.format(urlparse(url).netloc))

        return URL_Request

    def servers(self) -> list:
        '''
        Get Ookla servers
        '''

        Servers = loads(self._request(url=SERVERS_URL))

        if not Servers:
            raise SpeedTestServersError(message='No Servers Found')

        return Servers

    def select_server(self, number) -> None:
        '''
        Based on the server name, upload and download URLs are generated
        '''

        Server = self.servers()[number]
        Name = Server.get('host')

        self.DOWNLOAD = 'https://{}{}'.format(Name, DOWNLOAD_URL)
        self.UPLOAD = 'https://{}{}'.format(Name, UPLOAD_URL)

    def _get_speed(self, speed) -> float:
        '''
        Each speed is calculated according to the formula:

        Speed = PACKET_SIZE
                -----------
                T2   -   T1
        '''

        Start = time()

        try:
            if speed == 'DOWNLOAD':
                self._request(url=self.DOWNLOAD)
            else:
                self._request(url=self.UPLOAD, content=b'Q' * self.PACKET_SIZE, url_method='POST')
        except :
            raise SpeedTestError(message='Failed to get {} speed'.format(speed.lower()))

        Done = time()

        Result = self.PACKET_SIZE / (Done - Start)

        return Result

    def download(self) -> float:
        '''
        Get download speed
        '''

        return self._get_speed(speed='DOWNLOAD')

    def upload(self) -> float:
        '''
        Get upload speed
        '''

        return self._get_speed(speed='UPLOAD')


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('tablemanager', '0017_auto_20151124_1144'),
    ]

    operations = [
        migrations.AddField(
            model_name='input',
            name='ds_modify_time',
            field=models.DateTimeField(null=True, editable=False),
            preserve_default=True,
        ),
    ]


# -*- coding: utf-8 -*-
"""snake game state definitions
"""
import enum
import logging
import collections
import itertools
import random

logger = logging.getLogger("snakai")

class Direction(enum.IntEnum):
    """direction"""
    LEFT = 0
    RIGHT = 1
    UP = 2
    DOWN = 3
    NONE = 4

Point = collections.namedtuple('Point', "x, y")

class SnakeStateMachine(object):
    """snake-state-machine
    only keep inner status, the IO logic should be impl in the other class.

    the state mainly including 3 elements:
    1. environment: should be viewed as a matrix,
        with w = width, h = height
        the matrix is consisted of points, each has position.
        the position starts with 0.
        so all the points should bound in: x \in [0, w), y \in [0, h)
    2. snake: could be viewed as a list of continuous points and a direction.
        snake is some continuous points in the environments. it has a direction.
    3. feed: a point in the environment.

    what's more, some extra elements is used for game:
    1. score: game score, currently equals to the food eaten
    2. steps: move steps. each update on not-ended state should increase the steps.
    3. remaining-steps: max remaining steps before game-over.
        if food not eaten while remaining-steps == 0, then game over.
        else if food has been eaten, the remaining steps will be refilled.
        This is used to avoid `circle` operations.
    """
    class InnerStatus(object):
        """inner status"""
        RUNNING = 0
        FAIL = 1
        SUCCESS = 2
        UN_INIT = 3

    def __init__(self, width, height):
        """init
        """
        # type: collections.deque
        self.snake = None
        # type: Point
        self.food = None
        self.direction = Direction.NONE
        # type: int
        self.steps = None
        # type: fint
        self.remaining_steps = None
        # type: float
        self.score = None

        self._w = width
        self._h = height
        self._status = self.InnerStatus.UN_INIT
        self._rng = random.Random()

    def is_state_ok(self) -> bool:
        """query whether current state is ok
        """
        return self._status == self.InnerStatus.RUNNING

    def update_state(self, d: Direction) -> bool:
        """
        Parameters
        ------------
        d: Direction

        Returns
        ---------
        Boolean
            whether game is ok.
        """
        def _udpate_direction():
            if not DirectionUtil.is_effective(d) or DirectionUtil.is_opposite(d, self.direction):
                return
            self.direction = d

        def _add_snake_head():
            current_head_p = self.head
            d = self.direction
            new_head = gen_next_step_point(current_head_p, d)
            # no need to check collision
            self.snake.appendleft(new_head)

        def _is_new_head_collide():
            new_head = self.head
            # case1: new-head collide on the edge
            if new_head.x in [-1, self._w] or new_head.y in [-1, self._h]:
                return True
            # case2: new-head collide on self body
            for test_point in itertools.islice(self.snake, 1, len(self.snake)):
                if new_head == test_point:
                    return True
            return False

        def _is_exceed_max_steps():
            return self.remaining_steps <= 0

        def _has_eaten_food():
            new_head = self.head
            return new_head == self.food

        def _has_succeeded():
            return len(self.snake) == self._h * self._w

        def _update_food():
            # if snake is too long and occupies almost all space,
            # this logic may dramatically costly
            while True:
                x = self._rng.randrange(0, self._w)
                y = self._rng.randrange(0, self._h)
                new_food = Point(x, y)
                if new_food in self.snake:
                    continue
                else:
                    break
            self.food = new_food

        if not self.is_state_ok():
            return False

        self.steps += 1
        self.remaining_steps -= 1
        _udpate_direction()

        _add_snake_head()
        if _is_new_head_collide() or _is_exceed_max_steps():
            self._status = self.InnerStatus.FAIL
            return False

        if _has_eaten_food():
            logger.debug("eat food! score = %s", self.score)
            self.score += 1
            self.remaining_steps = _calc_max_remaining_steps(self)
            if _has_succeeded():
                self._status = self.InnerStatus.SUCCESS
                return False
            _update_food()
        else:
            # remove tail to make a moving illusion
            self.snake.pop()

        return True

    def new_state(self):

        SNAKE_LENGTH = 3

        def _random_snake_head():
            """init a snake.
            left space for snake body
            """
            head_x = self._rng.randrange(SNAKE_LENGTH + 1, self._w - SNAKE_LENGTH - 1)
            head_y = self._rng.randrange(SNAKE_LENGTH + 1, self._h - SNAKE_LENGTH - 1)
            return Point(head_x, head_y)

        def _random_food(snake_head):
            # rand a food, can't in snake
            while True:
                x = self._rng.randrange(0, self._w)
                y = self._rng.randrange(0, self._h)
                if x == snake_head.x or y == snake_head.y:
                    # food should not be in the same x and y with snake head.
                    continue
                return Point(x, y)

        def _get_direction(snake_head, food):
            # in init, we let snake only has left & right direction
            # should head for the food in the initialization.
            return Direction.RIGHT if snake_head.x <= food.x else Direction.LEFT

        def _init_snake(snake_head, direction):
            # in init, only left/right direction
            tail_x_offset = -1 if direction == Direction.RIGHT else 1
            snake = collections.deque([snake_head])
            for i in range(1, SNAKE_LENGTH):
                tail = Point(snake_head.x + tail_x_offset * i, snake_head.y)
                snake.append(tail)
            return snake

        snake_head = _random_snake_head()
        food = _random_food(snake_head)
        direction = _get_direction(snake_head, food)
        snake = _init_snake(snake_head, direction)

        self.snake = snake
        self.food = food
        self.direction = direction
        self.score = 0
        self.steps = 0
        self.remaining_steps = _calc_max_remaining_steps(self)
        self._status = self.InnerStatus.RUNNING

    def is_success(self):
        """whether SUCCESS
        """
        return self._status == self.InnerStatus.SUCCESS

    def is_fail(self):
        """whether fail
        """
        return self._status == self.InnerStatus.FAIL

    def is_outer_point_collide2snake(self, p: Point) -> bool:
        """whether collide to the snake body?
        here we don't consider the window-edge.
        """
        for test_point in self.snake:
            if test_point == p:
                return True
        return False

    @property
    def state_width(self):
        """get width"""
        return self._w

    @property
    def state_height(self):
        """get height"""
        return self._h

    @property
    def head(self):
        """head"""
        return self.snake[0]

    def __str__(self):
        return f"food={self.food}, snake={self.snake}, step={self.steps}, score={self.score}"

def gen_next_step_point(p: Point, direction: Direction) -> Point:
    """generate next step point according to current point and direction
    """
    if not DirectionUtil.is_effective(direction):
        raise ValueError(f"not effective direction {direction}")

    x = p.x
    y = p.y
    if direction == Direction.LEFT:
        return Point(x - 1, y)
    elif direction == Direction.RIGHT:
        return Point(x + 1, y)
    elif direction == Direction.UP:
        return Point(x, y - 1)
    elif direction == Direction.DOWN:
        return Point(x, y + 1)

    raise ValueError(f"impossible direction [{direction}], something must wrong.")

class DirectionUtil(object):
    """direction util
    """
    _OPPOSITE_DIRECTION_SET = {
        (Direction.LEFT, Direction.RIGHT), (Direction.RIGHT, Direction.LEFT),
        (Direction.UP, Direction.DOWN), (Direction.DOWN, Direction.UP)
    }
    _OPPOSITE_DIRECTION_MAP = dict(_OPPOSITE_DIRECTION_SET)
    _EFFECTIVE_DIRECTION = {Direction.LEFT, Direction.RIGHT, Direction.UP, Direction.DOWN}

    @classmethod
    def get_opposite(cls, d: Direction):
        """get opposite direction
        """
        opposite_direction_dict = cls._OPPOSITE_DIRECTION_MAP
        if d not in opposite_direction_dict:
            raise ValueError(f"not effective action: {d}")
        return opposite_direction_dict[d]

    @classmethod
    def is_opposite(cls, a: Direction, b: Direction) -> bool:
        """whether a and b are the opposite directions
        """
        return (a, b) in cls._OPPOSITE_DIRECTION_SET

    @classmethod
    def is_valid(cls, d: Direction) -> bool:
        """whether d is a valid direction
        """
        return d in {cls.LEFT, cls.RIGHT, cls.UP, cls.DOWN, cls.NONE}

    @classmethod
    def get_effective(cls) -> set:
        """get effective directions
        """
        return cls._EFFECTIVE_DIRECTION

    @classmethod
    def is_effective(cls, d: Direction) -> bool:
        """whether d is a effective direction
        """
        return d in cls.get_effective()

def _calc_max_remaining_steps(game_state: SnakeStateMachine):
    """Calc max remaing steps.
    currently we naively impl it:
        1. if only 1 node, go to food, need max `Width + Height`
        2. for a snake line, the head need to bypass the body, most at `snake-body`
        3. duplicated operations, at most 1.2 -> 2 times (when body short, duplicated op should be less)
    """
    _dist_base = game_state.state_width + game_state.state_height
    _body_len_base = len(game_state.snake)
    _multiplier =  1.2 + 0.8 * (_body_len_base / (game_state.state_width * game_state.state_height))
    _max_val = (_dist_base + _body_len_base) * _multiplier
    _max_val = int(_max_val)
    return _max_val


from django.urls import path
from . import views
from siteblog import settings
from django.conf.urls.static import static

app_name = 'blog'

urlpatterns=[
    path('', views.home, name='home'),
    path('datascience/', views.datascience, name='datascience'),
    path('webdevelopment/', views.webdevelopment, name='webdevelopment'),
    path('contact/', views.contact, name='contact'),
    path('blog/', views.PostViewList.as_view(), name='post_list'),
    path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


from tkinter import *
#from ljf import run_labjack
import datetime

from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
from ljf import run_labjack

class LJGui:

    def __init__(self):
        #set up main window
        self.myGui = Tk()
        self.myGui.title("Balance Community LabJack Processor")
        self.myGui.geometry("450x200+600+200")
        load = Image.open("logo.png")
        load_resized = load.resize((200, 25), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(load_resized)
        img = Label(self.myGui, image=render)
        img.image = render
        img.place(x=0, y=0)
        self.myLabel = Label(text="Welcome! Enter the max number of requests, directory to save to and press run:",
                             fg="blue",justify='left').place(x=5,y=30)

        #variables
        self.MAX_REQUEST= IntVar()
        self.MAX_REQUEST_OUTPUT = str(self.MAX_REQUEST)
        self.directory = StringVar()
        self.path = StringVar()
        self.output= StringVar()

        #labels
        self.finalMax = Label(textvariable = self.MAX_REQUEST_OUTPUT,fg='red',justify='left').place(x=150,y=60)
        self.finalSaveTo= Label(textvariable = self.directory,fg='red',justify='left').place(x=150,y=90)
        self.finalRun = Label(textvariable = self.path, fg = 'red',justify='left').place(x=150,y=120)

        #input boxes and buttons
        MaxEntry = Entry(self.myGui, textvariable =  self.MAX_REQUEST,justify='center').place(x=10,y=60)
        SaveToButton = Button(text = "Save to", command = self.SaveTo).place(x=10,y=90)
        RunButton = Button(text = "Run", command = self.Run).place(x=10,y=120)
        reset = Button(text = "Reset Values", command = self.reset_values).place(x=10,y=160)
        quit = Button(text="Quit", command = self.closeWin).place(x=100,y=160)

        self.myGui.mainloop()

    #functions

    def SaveTo(self):
        self.directory.set(filedialog.askdirectory(initialdir='/'))

    def Run(self):
        self.path.set(str(self.directory.get()) + '/' + datetime.datetime.now().strftime(format='%Y-%m-%d_%H-%M-%S'))
        try:
            run_labjack(self.path.get(), self.MAX_REQUEST.get())
            self.output.set("Success")
        except Exception as e:
            self.output.set(str(e))

    def closeWin(self):
        self.myGui.destroy()                   #Close Window Function

    def reset_values(self):
        self.MAX_REQUEST.set(0)
        self.directory.set('')
        self.path.set('')
        self.output.set('')


import os
import re
from bs4 import BeautifulSoup
from sharedutils import errlog
from parse import appender
import json

def main():
    for filename in os.listdir('source'):
        try:
            if filename.startswith('ransomhouse-zoh'):
                html_doc='source/'+filename
                file=open(html_doc, 'r')
                soup=BeautifulSoup(file,'html.parser')
                jsonpart= soup.pre.contents # type: ignore
                data = json.loads(jsonpart[0]) # type: ignore
                for element in data['data']:
                    title = element['header']
                    website = element['url']
                    description = re.sub(r'<[^>]*>', '',element['info'])
                    # stdlog(title)
                    appender(title, 'ransomhouse', description,website)
                file.close()
        except:
            errlog('ransomhouse: ' + 'parsing fail')
            pass


#encoding:utf-8
from django.shortcuts import render
from django.template import RequestContext
from django.template.loader import render_to_string
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse, Http404
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.contrib.auth.decorators import login_required, permission_required
from django.shortcuts import get_object_or_404
from sedes.reportes import sms_gestion
from sedes.log_usuarios import log_addition, log_change

from gestion.models import Gestion, Poblacion
from inscripcion.models import Inscripcion, Actividad_Inscripcion, Actividad_grupo_poblacion
from centrosalud.models import Centro
from actividad.models import Actividad
from grupo.models import Grupo

@permission_required('inscripcion.index_inscripcion', login_url='/login')
def index_inscripcion(request):
    sms_gestion(request)
    gestion = Gestion.objects.get(gestion = request.session['gestion'])
    inscripciones = Inscripcion.objects.filter(gestion = gestion)
    return render(request, 'inscripciones/index.html', {
        'inscripciones':inscripciones,
    })

@permission_required('inscripcion.add_inscripcion', login_url='/login')
def list_centros_inscribir(request):
    sms_gestion(request)
    gestion = Gestion.objects.get(gestion = request.session['gestion'])
    inscripciones = Inscripcion.objects.filter(gestion = gestion)
    centros = Centro.objects.exclude(id__in = inscripciones.values('centro_id'))

    return render(request, 'inscripciones/list_centros_asignar.html', {
        'centros':centros,
    })

@permission_required('inscripcion.add_inscripcion', login_url='/login')
def add_centro_inscripcion(request, centro_id):
    centro = get_object_or_404(Centro, pk = centro_id)
    gestion = Gestion.objects.get(gestion = request.session['gestion'])
    insc = Inscripcion.objects.create(
        gestion = gestion,
        centro = centro,
    )
    log_addition(request, insc, 'Inscripcion Realizada')
    sms = "Centro %s Inscrito Correctamente"% (centro.nombre)
    messages.info(request, sms)
    return HttpResponseRedirect(reverse(list_centros_inscribir))

@permission_required('inscripcion.index_activdad', login_url='/login')
def index_asignacion(request):
    sms_gestion(request)
    gestion = Gestion.objects.get(gestion = request.session['gestion'])
    inscripciones = Inscripcion.objects.filter(gestion = gestion)
    return render(request, 'asignaciones/index.html', {
        'inscripciones':inscripciones,
    })

@permission_required('inscripcion.add_actividad_inscripcion', login_url='/login')
def inscripcion_actividad_asignar(request, insc_id):
    sms_gestion(request)
    inscripcion = get_object_or_404(Inscripcion, pk = insc_id)
    asignados = Actividad_Inscripcion.objects.filter(inscripcion = inscripcion)
    actividades = Actividad.objects.exclude(id__in = asignados.values('actividad_id'))

    return render(request, 'asignaciones/inscripcion_asignacion.html', {
        'inscripcion':inscripcion,
        'asignados':asignados,
        'actividades':actividades,
    })

@permission_required('inscripcion.add_actividad_inscripcion', login_url='/login')
def add_actividad_inscripcion(request, insc_id, actividad_id):
    inscripcion = get_object_or_404(Inscripcion, pk = insc_id)
    actividad = get_object_or_404(Actividad, pk = actividad_id)
    a = Actividad_Inscripcion.objects.create(
        inscripcion = inscripcion,
        actividad = actividad,
    )
    log_addition(request, a, 'Actividad asignada')
    log_change(request, actividad, 'Asignacion realizada')
    sms = "Actividad %s Asignada Correctamente"% (actividad.nombre)
    messages.info(request, sms)
    return HttpResponseRedirect(reverse(inscripcion_actividad_asignar, args={inscripcion.id, }))

@permission_required('inscripcion.add_actividad_inscripcion', login_url='/login')
def remove_asignacion(request, asig_id):
    asignacion = get_object_or_404(Actividad_Inscripcion, pk = asig_id)
    inscripcion = Inscripcion.objects.get(id = asignacion.inscripcion_id)
    asignacion.delete()
    sms = 'Asignacion Quitada Correctamente'
    messages.info(request, sms)
    return HttpResponseRedirect(reverse(inscripcion_actividad_asignar, args={inscripcion.id, }))

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def list_actividades_add_grupo(request, insc_id):
    sms_gestion(request)
    inscripcion = get_object_or_404(Inscripcion, pk = insc_id)
    actividades_inscripcion = Actividad_Inscripcion.objects.filter(inscripcion = inscripcion)

    return render(request, 'asignaciones/list_actividades_add_grupo.html', {
        'inscripcion':inscripcion,
        'actividades_inscripcion':actividades_inscripcion,
    })

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def ajax_list_grupos_add_actividad(request, insc_acti_id):
    if request.is_ajax():
        inscripcion_actividad = get_object_or_404(Actividad_Inscripcion, pk = insc_acti_id)
        actividad_grupo = Actividad_grupo_poblacion.objects.filter(actividad_inscripcion = inscripcion_actividad)
        grupos = Grupo.objects.exclude(id__in = actividad_grupo.values('grupo_id'))
        #grupos = Grupo.objects.all()
        html = render_to_string('asignaciones/ajax_list_grupos_add_actividad.html', {
            'inscripcion_actividad':inscripcion_actividad,
            'grupos':grupos,
        }, context_instance=RequestContext(request))
        return JsonResponse(html, safe=False)
    else:
        raise Http404

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def ajax_list_poblacion_add_grupo(request):
    if request.is_ajax():
        grupo_id = request.GET['grupo']
        act_insc_id = request.GET['actividad_inscripcion']
        grupo = Grupo.objects.get(id = grupo_id)
        inscripcion_actividad = Actividad_Inscripcion.objects.get(id = act_insc_id)
        poblaciones = Poblacion.objects.all()
        html = render_to_string('asignaciones/ajax_list_poblacion_add_grupo.html', {
            'grupo':grupo,
            'poblaciones':poblaciones,
            'inscripcion_actividad':inscripcion_actividad,
        }, context_instance=RequestContext(request))
        return JsonResponse(html, safe=False)
    else:
        raise Http404

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def ajax_list_grupos_asignados(request):
    if request.is_ajax():
        insc_acti_id = request.GET['actividad_inscripcion']
        actividad_inscripcion = Actividad_Inscripcion.objects.get(id = insc_acti_id)
        grupos_asignados = Actividad_grupo_poblacion.objects.filter(actividad_inscripcion = actividad_inscripcion)
        html = render_to_string('asignaciones/ajax_list_grupos_asignados.html', {
            'grupos_asignados':grupos_asignados,
        }, context_instance=RequestContext(request))
        return JsonResponse(html, safe=False)
    else:
        raise Http404

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def asignar_grupo_actividad_inscripcion(request, grupo_id, insc_acti_id, poblacion_id):
    poblacion = get_object_or_404(Poblacion, pk = poblacion_id)
    grupo = get_object_or_404(Grupo, pk = grupo_id)
    actividad_inscripcion = get_object_or_404(Actividad_Inscripcion, pk = insc_acti_id)
    a_g_p = Actividad_grupo_poblacion.objects.create(
        actividad_inscripcion = actividad_inscripcion,
        grupo = grupo,
        poblacion = poblacion,
    )
    log_addition(request, a_g_p, 'Grupo Asignado Correctamete')
    sms = "Grupo %s asignado correctamente"% (grupo.variable)
    messages.info(request, sms)
    return HttpResponseRedirect(reverse(list_actividades_add_grupo, args={actividad_inscripcion.inscripcion_id, }))

@permission_required('inscripcion.add_actividad_grupo_poblacion', login_url='/login')
def remove_grupo_actividad_inscripcion(request, grupo_id):
    g_p_i = get_object_or_404(Actividad_grupo_poblacion, pk = grupo_id)
    inscripcion = Inscripcion.objects.get(id = g_p_i.actividad_inscripcion.inscripcion_id)
    g_p_i.delete()
    sms = "Grupo Eliminado Corrsssectamente"
    messages.info(request, sms)
    return HttpResponseRedirect(reverse(list_actividades_add_grupo, args={inscripcion.id, }))


from collections import  Counter
from datetime import datetime, timedelta
from odoo.tools import float_is_zero, float_compare, DEFAULT_SERVER_DATETIME_FORMAT
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError##Warning
import logging
_logger = logging.getLogger(__name__)
Delivery = [
    ('none','None'),
    ('fixed','Fixed'),
    ('base_on_rule','Base on Rule'),

]

class ProductTemplate(models.Model):
    _inherit = "product.template"
    wk_packaging_ids = fields.Many2many(
        'product.packaging',
        'product_tmp_product_packaging_rel',
        'product_tmpl_id',
        'packaging_id',
        string='Packaging'
    )

class SaleOrder(models.Model):
    _inherit = "sale.order"
    carrier_price = fields.Float(
        string='Actual Carrier Charges',
        help="Actual Carrier Charges recive from api "
    )
    delivery_type = fields.Selection(related='carrier_id.delivery_type', readonly=True)

    create_package = fields.Selection(
        string='Create Package',
        selection=[('auto','Automatic'),('manual','Manual')],
        default='auto',
        help='Create  automatic package as per packing max weight limit and  max qty  ',
    )
    wk_packaging_ids = fields.One2many(
        comodel_name='product.package',
        inverse_name='order_id',
        string='Product Package'
    )

    def auto_create_package(self):
        obj = self.filtered(lambda o:o.carrier_id)
        no_carrier = self-obj
        if len(no_carrier):
            raise ValidationError('Select Delivery Method For %s .'%(no_carrier.mapped('name')))
        for order in obj:
            order.carrier_id.wk_set_order_package(order)
        return True
    @api.model
    def wk_get_order_package(self):
        return map(lambda wk_packaging_id:dict(
            packaging_id = wk_packaging_id.packaging_id.id,
            weight = wk_packaging_id.weight,
            width=wk_packaging_id.width,
            length=wk_packaging_id.length,
            height=wk_packaging_id.height,
            cover_amount=wk_packaging_id.cover_amount,
            qty=wk_packaging_id.qty,
        ),self.wk_packaging_ids)

    def _wk_check_carrier_quotation(self,force_carrier_id=None):
        res=True

        try:
            ctx =dict(self._context)
            ctx['wk_website']=1
            res= self.with_context(ctx)._check_carrier_quotation(force_carrier_id)
        except Exception as e:
            _logger.error("Checking carrier quotation #%s" % e)
            res=False
        return res


## implementation of Metropolis–Hastings sampling
import numpy as np
from scipy.stats import multivariate_normal
from scipy.stats import pearsonr
from sklearn.cluster import KMeans
import sklearn.preprocessing as sp

def clusters(X, levels):
    ans = [np.array(range(len(X)))]
    for i in range(levels):
        ansnew = []
        for j in range(len(ans)):
            kmeans = KMeans(n_clusters=2, random_state=0).fit(np.expand_dims(X[ans[j]], axis=1))
            # print(kmeans.labels_)
            ansnew.append(ans[j][np.where(kmeans.labels_==0)[0]])
            ansnew.append(ans[j][np.where(kmeans.labels_==1)[0]])
        ans = ansnew
    return ans

def gaussian_pdf(mean, cov, x):
    k = len(mean)
    assert k == len(x), 'Input dimension is not correct!'
    invcov = np.linalg.solve(cov, np.eye(k))
    diff = x - mean
    prod = diff.T.dot(invcov).dot(diff)
    det = np.linalg.det(cov)
    pdf = 1.0/np.sqrt((2*np.pi)**k / det)*np.exp(-prod*0.5)
    return pdf

def MHSampling(init_state, target_pdf, iter):
    x0 = init_state
    cov = np.eye(len(init_state))
    for t in range(iter):
        x = np.random.multivariate_normal(x0, cov)
        a = target_pdf(x)*gaussian_pdf(x, cov, x0)
        a0 = target_pdf(x0)*gaussian_pdf(x0, cov, x)
        #print(a, a0)
        acceptance = min(1, a/a0)
        u = np.random.rand(1)
        if u < acceptance: x0 = x
    return x0

def uniformlychange1ele(x):
    n = len(x)
    seeds = np.random.rand(0, n, int(n/3))
    for i in range(len(seeds)):
        ind = seeds[i]
        x[ind] = -x[ind]
    return x

def proposaldist(x, y, rate=0.5):
    prob = 1.0
    for i in range(len(x)):
        if abs(x[i]-y[i]) < 1e-14: prob = prob*(1-rate)
        else: prob = prob*rate
    return prob

def uniformlychange(x, rate=0.5):
    y = np.zeros(len(x))
    for i in range(len(x)):
        if np.random.rand() < rate: y[i] = -x[i]
        else: y[i] = x[i]
    return y

def MHSampling4Ising(init_state, target_pdf, iter, rate):
    x0 = init_state
    for t in range(iter):
        x = uniformlychange(x0, rate)
        #acceptance = min(1, target_pdf(x)*proposaldist(x, x0, rate)/target_pdf(x0)*proposaldist(x0, x, rate))
        acceptance = min(1, target_pdf(x)/target_pdf(x0))
        u = np.random.rand(1)
        if u < acceptance: x0 = x
    return x0

def E_linear(A, x):
    return x.T.dot(A).dot(x)

def boltzmann_pdf(beta, E_fn, x, const):
    return const*np.exp(-beta*E_fn(x))

def normal_pdf(mu, A, x):
    k = len(mu)
    diff = x - mu
    assert len(diff.shape) == 1
    invA = np.linalg.solve(A, np.eye(k))
    nu = np.exp(-diff.T.dot(invA).dot(diff)*0.5)
    de = np.sqrt(np.linalg.det(A)*((2*np.pi)**k) )
    #print(nu, de)
    return nu/de

def enumerate(ans, num_var):
    if num_var > 1:
        n = len(ans)//2
        assert n*2 == 2**num_var
        ans[:n, 0] = 1
        enumerate(ans[:n, 1:], num_var-1)
        ans[n:, 0] = -1
        enumerate(ans[n:, 1:], num_var-1)
    else:
        ans[0, :] = 1
        ans[1, :] = -1
        return

def pearson_corr(samples):
    num_samples = len(samples)
    num_var = len(samples[0])
    corr = np.zeros((num_var, num_var))
    #print(samples.shape, corr.shape)
    for i in range(num_var):
        for j in range(i, num_var):
            corr[i, j] = pearsonr(samples[:, i], samples[:, j])[0]
    return corr.T + corr - np.diag(np.diag(corr))

def simplecount(x0, x1):
    num = len(x0)
    ans = np.zeros((2,2))
    for i in range(num):
        if abs(x0[i]-1) < 1e-14 and abs(x1[i]-1) < 1e-14:
            ans[0, 0] += 1
        elif abs(x0[i]-1) < 1e-14 and abs(x1[i]+1) < 1e-14:
            ans[0, 1] += 1
        elif abs(x0[i]+1) < 1e-14 and abs(x1[i]-1) < 1e-14:
            ans[1, 0] += 1
        else:
            ans[1, 1] += 1
    assert np.sum(np.sum(ans)) == num
    return ans/num

def marginalize4Ising(samples):
    num_var = samples.shape[1]
    marginal = np.zeros((num_var, num_var, 2, 2))
    for i in range(num_var):
        for j in range(i, num_var):
            marginal[i, j, :, :] = simplecount(samples[:, i], samples[:, j])
    return marginal

def marginalize4Ising_truth(samples, dist):
    num_var = samples.shape[1]
    marginal = np.zeros((num_var, num_var, 2, 2))
    for i in range(num_var):
        for j in range(num_var):
            marginal[i, j, :, :] = simplecount_prob(samples[:, i], samples[:, j], dist)
    return marginal

def expectation_truth(samples, dist):
    (m, n) = samples.shape
    ep = np.zeros(n)
    for i in range(n):
        prob1 = 0
        prob_1 = 0
        for j in range(m):
            if abs(samples[j, i]-1) < 1e-14: prob1 += dist[j]
            else: prob_1 += dist[j]
        assert abs(prob1 + prob_1 - 1) < 1e-12
        ep[i] = prob1-prob_1
    return ep

def pearson_corr_truth(margin, exp):
    n = len(exp)
    corr = np.zeros((n, n))
    for i in range(n):
        for j in range(i, n):
            epi, epj = exp[i], exp[j]
            cov = margin[i, j, 0, 0] + margin[i, j, 1, 1] - margin[i, j, 0, 1]- margin[i, j, 1, 0] - epi*epj
            sigmai = np.sqrt(1-epi**2)
            sigmaj = np.sqrt(1-epj**2)
            corr[i, j] = cov/sigmai/sigmaj
    corr = corr + corr.T - np.diag(np.diag(corr))
    return corr

def simplecount_prob(x0, x1, dist):
    num = len(x0)
    ans = np.zeros((2,2))
    for i in range(num):
        if abs(x0[i]-1) < 1e-14 and abs(x1[i]-1) < 1e-14:
            ans[0, 0] += dist[i]
        elif abs(x0[i]-1) < 1e-14 and abs(x1[i]+1) < 1e-14:
            ans[0, 1] += dist[i]
        elif abs(x0[i]+1) < 1e-14 and abs(x1[i]-1) < 1e-14:
            ans[1, 0] += dist[i]
        else:
            ans[1, 1] += dist[i]
    #assert abs(np.sum(np.sum(ans))-1) < 1e-14
    return ans

def preprocess(samples, MST):
    edges = sum([len(MST[i]) for i in range(len(MST))])
    num_samples = samples.shape[0]
    ans = np.zeros((num_samples, edges))
    count = 0
    for i in range(len(MST)):
        edge = MST[i]
        for j in range(len(edge)):
            ans[:, count+j] = samples[:, edge[j]]
        count += len(edge)
    return ans

def to0or1(num):
    return np.array((num+1) / 2)

def frequency(samples):
    (n, m) = samples.shape
    freq = np.zeros(2**m)
    weights = np.array([2**(m-i-1) for i in range(m)])
    idx = to0or1(samples).dot(weights)
    print(idx.shape)
    for i in range(n):
        freq[int(idx[i])] += 1
    freq = freq/n
    for i in range(2**m):
        if freq[i] == 0.0: freq[i] = 1e-14
    prob = np.zeros(n)
    for i in range(n):
        prob[i] = freq[int(idx[i])]
    return prob, freq

def probbyfreq(samples, freq):
    (n, m) = samples.shape
    weights = np.array([2**(m-i-1) for i in range(m)])
    idx = to0or1(samples).dot(weights)
    prob = np.zeros(n)
    for i in range(n):
        prob[i] = freq[int(idx[i])]
    return prob