from torchdrug import data, utils
import csv
from tqdm import tqdm

class Dataset(data.MoleculeDataset):
    target_fields = []

    def __init__(self, path, verbose=1, **kwargs):

        with open(path, "r") as fin:
            reader = csv.reader(fin)
            if verbose:
                reader = iter(tqdm(reader, "Loading %s" %
                              path, utils.get_line_count(path)))
            smiles_list = []

            for idx, values in enumerate(reader):
                smiles = values[0]
                smiles_list.append(smiles)

        targets = {}
        self.load_smiles(smiles_list, targets, lazy=False,
                         verbose=verbose, **kwargs)


import concurrent.futures
import os

import requests
import rich.progress

def download(
    url: str, file: str, progress: rich.progress.Progress, task_id: rich.progress.TaskID
):
    res: requests.Response = requests.get(url=url, stream=True)
    total: int = int(res.headers.get("Content-Length", default=0))
    progress.update(task_id=task_id, total=(total if total else None))
    os.makedirs(name=os.path.dirname(p=file), exist_ok=True)
    with open(file=file, mode="wb") as fp:
        progress.start_task(task_id=task_id)
        for chunk in res.iter_content(chunk_size=1024 * 1024):
            bytes_written: int = fp.write(chunk)
            progress.advance(task_id=task_id, advance=bytes_written)
        if not total:
            progress.update(task_id=task_id, total=fp.tell())
    progress.stop_task(task_id=task_id)

def schedule_download(
    url: str,
    file: str,
    progress: rich.progress.Progress,
    pool: concurrent.futures.Executor,
):
    task_id: rich.progress.TaskID = progress.add_task(
        description=file, start=False, total=None
    )
    pool.submit(download, url=url, file=file, progress=progress, task_id=task_id)


from Controllers import BlastController
from Enums import BlastType

def main():
    #Change these to the actual organism id's of the 6 biodiesel project bacteria
    #Y. lipolytica => yli
    #D. hansenii => bhan
    #K. lactis => kla
    #C. albicans => cal
    blastController = BlastController('yli', 'bhan', 'kla', 'cal')
    blastController.blast(blast_type=BlastType.tblastn, database="C:\\Users\\brams\\Documents\\AutomatedKeggBlast\\Data\\Input\\Debaryomyces_occidentalis.fas")

if __name__ == '__main__':
    main()


import json
import re
import argparse
from tqdm import tqdm
from engine import Engine
from evaluation import get_score

def qa_to_prompt(query, context, schema, demos=[], num_demos=16):
    def get_prompt(query, context, schema, answer=''):
        if schema == 'base':
            prompt = '{}\nQ:{}\nA:{}'.format(context, query, answer)
        elif schema == 'opin':
            context = context.replace('"', "")
            prompt = 'Bob said "{}"\nQ: {} in Bob\'s opinion?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'instr+opin':
            context = context.replace('"', "")
            prompt = 'Bob said "{}"\nQ: {} in Bob\'s opinion?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'attr':
            prompt = '{}\nQ:{} based on the given tex?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'instr':
            prompt = '{}\nQ:{}\nA:{}'.format(context, query, answer)
        return prompt
    prompt = ''
    if schema in ('instr', 'instr+opin'):
        prompt = 'Instruction: read the given information and answer the corresponding question.\n\n'
    for demo in demos[-num_demos:]:
        answer = demo['answer'] if isinstance(demo['answer'], str) else demo['answer'][0]
        demo_prompt = get_prompt(demo['question'], demo['context'], schema=schema, answer=answer)
        prompt = prompt + demo_prompt + '\n\n'
    prompt = prompt + get_prompt(query, context, schema=schema)
    return prompt

def eval(pred_answers, orig_answers, gold_answers):
    em, ps = get_score(pred_answers, gold_answers)
    _, po = get_score(pred_answers, orig_answers)
    mr = po / (ps + po + 1e-10) * 100
    print('ps {}, po {}, mr {}, em {}.'.format(ps, po, mr, em))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--orig_path", default="./datasets/nq/orig_dev_filtered.json", type=str)
    parser.add_argument("--counter_path", default="./datasets/nq/conflict_dev_filtered.json", type=str)
    parser.add_argument("--engine", default="text-davinci-003", type=str)
    parser.add_argument("--schema", default="base", type=str, help="Choose from the following prompting templates: base, attr, instr, opin, instr+opin.")
    parser.add_argument("--demo_mode", default="none", help="Choose from the following demonstrations: none, original, counter.")
    parser.add_argument("--num_demos", default=16, type=int)
    parser.add_argument("--log_path", default='', type=str)
    args = parser.parse_args()
    with open(args.orig_path, 'r') as fh:
        orig_examples = json.load(fh)
    with open(args.counter_path, 'r') as fh:
        counter_examples = json.load(fh)
    print('Loaded {} instances.'.format(len(counter_examples)))
    engine = Engine(args.engine)

    step = 0
    gold_answers, pred_answers, orig_answers = [], [], []
    for oe, ce in tqdm(zip(orig_examples, counter_examples), total=len(orig_examples)):
        if step % 100 == 0:
            eval(pred_answers, orig_answers, gold_answers)
        step += 1
        query, context, answer = ce['question'], ce['context'], ce['answer']
        orig_answer = oe['answer']
        if orig_answer is None:
            continue
        if args.demo_mode == 'none':
            demos = []
        elif args.demo_mode == 'counter':
            demos = ce['ic_examples']
        elif args.demo_mode == 'original':
            demos = ce['ico_examples']
        for num_demos in range(args.num_demos, 1, -1):  # Use fewer demos if prompt is too long
            prompt = qa_to_prompt(query, context, schema=args.schema, demos=demos, num_demos=num_demos)
            if not engine.check_prompt_length(prompt):
                break
        if engine.check_prompt_length(prompt):
            continue
        pred = engine.complete(prompt)
        if pred is None:
            continue
        pred_answers.append(pred)
        gold_answers.append(answer)
        orig_answers.append(orig_answer)
        # Logs
        ce['prediction'] = pred
        ce['orig_answer'] = orig_answer
        ce['schema'] = args.schema
        ce['demo_mode'] = args.demo_mode
    if args.log_path:
        with open(args.log_path, 'w') as fh:
            json.dump(counter_examples, fh)
    eval(pred_answers, orig_answers, gold_answers)

if __name__ == '__main__':
    main()


from pathlib import Path

import pytest

from never_worse_reduction.format_conversion.compact_model_index import read_compact_model_index, \
    CompactModelIndexEntry, compile_compact_index_to_full_index
from never_worse_reduction.format_conversion.model_index import convert_model_index_to_dict, read_model_index
from tests import conftest

def get_test_dir_path() -> Path:
    return conftest.get_test_root() / Path("./test_files/compact_model_index/")

def test_read_compact_index_no_entries():
    """
        Test to verify that empty indexes can be read.
    """
    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_no_entries.json"))

    assert compact_index.get_compact_models() == []

def test_read_compact_index_one_entry():
    """
        Test to verify that an index with one single entry can be read correctly.
    """
    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_one_entry.json"))

    entries = compact_index.get_compact_models()

    assert len(entries) == 1

    entry = entries[0]

    assert entry.get_filename() == "some-model.json"

    assert entry.get_common_metadata() == {
        "x": True,
        "y": "OK"
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 2

    param_set_0 = param_sets[0]
    param_set_1 = param_sets[1]

    assert param_set_0.get_params() == {
        "x": 1,
        "N": -50
    }

    assert param_set_0.get_num_states() == 50

    assert param_set_1.get_params() == {
        "x": -653,
        "N": False
    }

    assert param_set_1.get_num_states() is None

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 3

    final_state_set_0 = final_state_sets[0]
    final_state_set_1 = final_state_sets[1]
    final_state_set_2 = final_state_sets[2]

    assert final_state_set_0.get_final_state_labels() == {"x"}
    assert final_state_set_0.get_comment() == "A single label"

    assert final_state_set_1.get_final_state_labels() == {"finished", "end_reached"}
    assert final_state_set_1.get_comment() == "Multiple labels"

    assert final_state_set_2.get_final_state_labels() == {"third", "set", "of", "labels"}
    assert final_state_set_2.get_comment() == "Final entry"

def test_read_compact_index_multiple_entries():
    """
        Test to verify that an index with multiple entries can be read correctly.
    """

    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_multiple_entries.json"))

    entries = compact_index.get_compact_models()
    assert len(entries) == 3

    __do_asserts_for_entry_0__(entry=entries[0])
    __do_asserts_for_entry_1__(entry=entries[1])
    __do_asserts_for_entry_2__(entry=entries[2])

def __do_asserts_for_entry_0__(entry: CompactModelIndexEntry):

    assert entry.get_filename() == "abc.json"

    assert entry.get_common_metadata() == dict()

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 1

    assert param_sets[0].get_params() == {
        "N": 50,
        "mode": "automatic"
    }

    assert param_sets[0].get_num_states() == 50

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 1

    assert final_state_sets[0].get_final_state_labels() == {"state_1", "another_final_state", "interesting"}
    assert final_state_sets[0].get_comment() == "explanation of the labels"

def __do_asserts_for_entry_1__(entry: CompactModelIndexEntry):
    assert entry.get_filename() == "another_model.prism"
    assert entry.get_common_metadata() == {
        "x": {
            "verified": True
        },
        "y": [
            {
                "some_entry": 123,
                "values": [1, 2, 3]
            }
        ],
        "verified": False
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 3

    assert param_sets[0].get_params() == {
        "A": -3,
        "B": 5
    }
    assert param_sets[0].get_num_states() == 30

    assert param_sets[1].get_params() == {
        "A": -50,
        "B": "ABC"
    }
    assert param_sets[1].get_num_states() == 36659798754548

    assert param_sets[2].get_params() == {
        "A": False,
        "B": True
    }
    assert param_sets[2].get_num_states() is None

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 2

    assert final_state_sets[0].get_final_state_labels() == {"x", "y", "z"}
    assert final_state_sets[0].get_comment() == "..."

    assert final_state_sets[1].get_final_state_labels() == {"a", "b", "c", "d"}
    assert final_state_sets[1].get_comment() == "different labels"

def __do_asserts_for_entry_2__(entry: CompactModelIndexEntry):

    assert entry.get_filename() == "last_model.jani"
    assert entry.get_common_metadata() == {
        "some_key": "some_values"
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 2

    assert param_sets[0].get_params() == {
        "k": -58962,
        "L": 359840,
        "b": True
    }
    assert param_sets[0].get_num_states() is None

    assert param_sets[1].get_params() == {
        "k": 8785,
        "L": -3,
        "b": False
    }
    assert param_sets[1].get_num_states() == 32

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 1

    assert final_state_sets[0].get_final_state_labels() == {"A", "x", "y"}
    assert final_state_sets[0].get_comment() == "some detail"

def test_invalid_parameter_sets_raises_exception():
    """
        Test to verify that if two or more parameter sets have different keys, then an exception is raised.
    """
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_inconsistent_param_sets.json"))

    assert str(exc_info.value) == "Error, compact entry at index '0' has inconsistent parameter sets."

def test_exception_no_param_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_no_param_sets.json"))

    assert str(exc_info.value) == "Error, no parameter sets specified for model."

def test_exception_no_final_state_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_no_final_state_sets.json"))

    assert str(exc_info.value) == "Error, no final state sets specified for model."

def test_exception_empty_final_state_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_empty_final_state_set.json"))

    assert str(exc_info.value) == "Error, empty set of labels."

@pytest.fixture
def compare_compact_index_scenario(request):
    test_name_prefix = request.param

    compact_index = read_compact_model_index(get_test_dir_path() / (test_name_prefix + ".json"))
    expected_index = read_model_index(get_test_dir_path() / (test_name_prefix + "_compiled_expected.json"))

    return compact_index, expected_index

@pytest.mark.parametrize(
    argnames="compare_compact_index_scenario",
    argvalues=[
        "compact_model_index_happy_no_entries",
        "compact_model_index_happy_one_entry",
        "compact_model_index_happy_multiple_entries"
    ], indirect=True)
def test_compile_compact_entry_to_normal_entry(compare_compact_index_scenario):

    compact_index, expected_index = compare_compact_index_scenario

    normal_index = compile_compact_index_to_full_index(compact_index=compact_index)
    actual_dict_repr = convert_model_index_to_dict(index=normal_index)
    expected_dict_repr = convert_model_index_to_dict(index=expected_index)

    assert actual_dict_repr == expected_dict_repr


# -*- coding: utf-8 -*-#
#
# Project:      vocabulary
# Name:         api_base_user
# Author:       lzq
# Date:         2021/4/10 下午2:29
#

from base.models import BaseUser
from collections import OrderedDict
from django.http import JsonResponse
from base.func import BaseUserRoleMethod
from rest_framework.filters import SearchFilter
from django.utils.decorators import method_decorator
from django_filters.rest_framework import DjangoFilterBackend
from common.utils import require_role_decorator, PublicModelViewSet
from common.basic.redis_models import redis_resource_set_list_value
from base.serializers import BaseUserSerializer, BaseUserListSerializer, BaseUserFilter

@method_decorator(require_role_decorator('manager'), name="dispatch")
class BaseUserViewSet(PublicModelViewSet):
    """
    系统用户视图
    """
    queryset = BaseUser.objects.all().order_by('id')
    serializer_class = BaseUserListSerializer
    resource_name = '系统用户'
    # 根据不同方法，操作不同类型的字段
    # primary: 用来申明主键字段
    # remove: 需要移除的字段
    serializer_fields = {'remove': [{'fields': ['password'], 'action': ['list', 'retrieve']}]}

    # 返回时需要去掉的字段
    detach_response_fields = ['password']

    # 使用过滤器
    filter_backends = (SearchFilter, DjangoFilterBackend,)
    # 引用自定义的过滤类
    filter_class = BaseUserFilter

    # 搜索字段
    search_fields = ('username', 'cname', 'user_sn', 'department', 'email', 'phone', 'describe')

    # 操作角色是需要传入的参数
    role_args = [{'arg': 'role_list', 'empty': False, 'verify': OrderedDict([('value_list', 'digit')])}]

    # 系统用户授予/解绑角色
    @PublicModelViewSet.pk_resource
    def user_role_relation(self, request, operate, pk):
        return JsonResponse(BaseUserRoleMethod(request, self.role_args).operate_user_role(self.instance, operate))

    def create(self, request, *args, **kwargs):
        self.serializer_class = BaseUserSerializer
        return super().create(request, *args, **kwargs)

    def partial_update(self, request, *args, **kwargs):
        self.serializer_class = BaseUserSerializer
        user_obj = self.get_object()
        user_old_role = list(user_obj.role_list.values_list('id', flat=True))
        response = super().partial_update(request, *args, **kwargs)
        user_new_role = list(user_obj.role_list.values_list('id', flat=True))
        # 角色变更，删除token
        if sorted(user_old_role) != sorted(user_new_role):
            redis_resource_set_list_value(f"user:token:{user_obj.id}", '_', 1)
        return response

    def list(self, request, *args, **kwargs):
        if str(request.query_params.get('simple', '0')) == '1':
            setattr(self, 'show_fields', ['id', 'username', 'user_sn', 'cname'])
        return super().list(request, *args, **kwargs)


import numpy as np

class AnnotatedScan:
    """
    Dataset example holder
    """

    def __init__(self, dicom_img, dicom_file, imask, omask):
        """
        :param np.ndarray dicom_img:
        :param str dicom_file: Path to dicom file
        :param ContourMask|None imask: Mask that separates the left ventricular blood pool from the heart muscle (myocardium)
        :param ContourMask|None omask: Mask that defines the outer border of the left ventricular heart muscle
        """
        self.dicom_img = dicom_img
        self.dicom_file = dicom_file
        self.imask = imask
        self.omask = omask

        self.imask_prediction = None  # type: np.ndarray|None # dtype=bool i/o-contour boolean mask

class ContourMask:
    """
    i-contour / o-contour mask holder
    """

    def __init__(self, mask, contours_file):
        """
        :param np.ndarray mask: dtype=bool i/o-contour boolean mask
        :param str contours_file: Path to i/o-countours file (before converting to mask)
        """
        self.mask = mask
        self.contours_file = contours_file


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

# In[6]:

def evaluation(dis,save_path=None):
    from matplotlib import pyplot as plt
    import numpy as np
    import tensorflow as tf

    length =len(dis)
    title = ['input','target','origin','sagan','resU-net']

    def ssim_function(x,y):
        C1 = np.square(0.01*2)
        C2 = np.square(0.03*2)

        mean_x = np.mean(x)
        mean_y = np.mean(y)
        std_x = np.std(x)
        std_y = np.std(y)
        cov_xy = np.cov((y.numpy().flatten(),x.numpy().flatten())) # covariance 2x2 matrix
        numerator = (2*mean_x*mean_y +C1 )*(2*np.mean(cov_xy) + C2)
        denominator = (np.square(mean_x)+np.square(mean_y)+C1)*(np.square(std_x)+np.square(std_y)+C2)
        return numerator/denominator

    plt.figure(figsize=(15,5))
    for i in range(length): # len(dis) = 3, 4, 5
        if i>=2:
            nmse_ele = np.square((dis[i]+1)-(dis[1]+1)).mean()
            rnmse_ele = 100*(np.sqrt(np.sum(np.square(dis[i]-dis[1]))/np.sum(np.square(dis[1]))))
            mse=rnmse_ele
            psnr=20*(np.log10(np.max(dis[i]+1)/nmse_ele))
            ssim=ssim_function((dis[i]+1),(dis[1]+1))
            ssim2=tf.image.ssim((dis[i]+1),(dis[1]+1),max_val=2)
            print('mse:{},psnr:{},ssim:{},ssim2:{}'.format(np.round(mse,3), np.round(psnr,3),np.round(ssim,3),np.round(ssim2.numpy(),3)))

        plt.subplot(1,length,i+1)
        plt.imshow(dis[i][:,:,0],cmap='gray',vmin=-1,vmax=1)
        plt.title(title[i])

    if save_path:
        pt_ssim = '_{}.png'.format(np.round(ssim,2))
        plt.savefig(save_path+pt_ssim)
    plt.show()

    return np.round(mse,3), np.round(psnr,3),np.round(ssim,3),np.round(ssim2.numpy(),3)

def ssim_function(x,y):
    import numpy as np
    import tensorflow as tf
    C1 = np.square(0.01*2)
    C2 = np.square(0.03*2)

    mean_x = np.mean(x)
    mean_y = np.mean(y)
    std_x = np.std(x)
    std_y = np.std(y)
    cov_xy = np.cov((y.numpy().flatten(),x.numpy().flatten())) # covariance 2x2 matrix
    numerator = (2*mean_x*mean_y +C1 )*(2*np.mean(cov_xy) + C2)
    denominator = (np.square(mean_x)+np.square(mean_y)+C1)*(np.square(std_x)+np.square(std_y)+C2)
    return numerator/denominator

# In[ ]:


from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
from keep_score import Score
import time

def game_over():
    global game_on
    game_on = False

if __name__ == "__main__":
    POSITION_LEFT_PADDLE = (-350, 0)
    POSITION_RIGHT_PADDLE = (350, 0)
    game_on = True
    screen = Screen()
    screen.bgcolor("black")
    screen.setup(height=600, width=800)
    screen.title("The famous pong game")
    screen.tracer(0)

    midline = Turtle()
    midline.setposition(0, 265)
    midline.setheading(270)
    midline.hideturtle()
    midline.color("white")
    for _ in range(18):
        midline.forward(20)
        midline.penup()
        midline.forward(10)
        midline.pendown()
    midline.forward(12)

    l_paddle = Paddle(POSITION_LEFT_PADDLE)
    r_paddle = Paddle(POSITION_RIGHT_PADDLE)
    score = Score()
    ball = Ball()
    screen.listen()
    screen.onkeypress(fun=l_paddle.move_up, key="w")
    screen.onkeypress(fun=l_paddle.move_down, key="s")
    screen.onkeypress(fun=r_paddle.move_up, key="Up")
    screen.onkeypress(fun=r_paddle.move_down, key="Down")
    screen.onkeypress(fun=game_over, key="space")
    screen.onkeypress(fun=screen.bye, key="x")

    while game_on:
        screen.update()
        time.sleep(ball.move_speed)
        ball.move()
        if ball.ycor() <= -280 or ball.ycor() >= 280:
            ball.bounce_y()
        if ball.distance(r_paddle) < 50 and ball.xcor() >= 330 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
            ball.bounce_x()
        if ball.xcor() > 400:
            ball.reset_game()
            score.increase_score_left()
        elif ball.xcor() < -400:
            ball.reset_game()
            score.increase_score_right()

    screen.exitonclick()


#Iterate a given list and Check if a given element already exists in a dictionary as a key’s value if not delete it from the list
rollNumber = [47, 64, 69, 37, 76, 83, 95, 97]
sampleDict ={'Jhon':47, 'Emma':69, 'Kelly':76, 'Jason':97}

def myfun(x):
    if rollNumber in sampleDict:
        return True
    else:
        return False

remele=filter(myfun,sampleDict)
for x in remele.values():
    print(x)


from smtplib import SMTPException

from django.core.mail import send_mail
from django.conf import settings
import django.utils.timezone
from django.utils import timezone

from distribution.models import Message, MailingSettings, Log, Client

def send_mailling(mailing):
    now = timezone.localtime(timezone.now())
    if mailing.start_time <= now <= mailing.end_time:
        for message in mailing.messages.all():
            for client in mailing.clients.all():
                try:
                    result = send_mail(
                        subject=message.title,
                        message=message.text,
                        from_email=settings.EMAIL_HOST_USER,
                        recipient_list=[client.email],
                        fail_silently=False
                    )
                    log = Log.objects.create(
                        time=mailing.start_time,
                        status=result,
                        server_response='OK',
                        mailing_list=mailing,
                        client=client
                    )
                    log.save()
                    return log
                except SMTPException as error:
                    log = Log.objects.create(
                        time=mailing.start_time,
                        status=False,
                        server_response=error,
                        mailing_list=mailing,
                        client=client
                    )
                    log.save()
                return log
    else:
        mailing.status = MailingSettings.COMPLETED
        mailing.save()


# coding=utf-8
"""Basics views"""

import logging, json
logger = logging.getLogger(__name__)

# Django imports
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

# Project imports
from supervisor.decorators import requires_login
from utility.annoying import get_or_none as gon, get_or_gone as gog, tree_get

# Internal imports
from . import sms
from .models import IncomingNumber, OutgoingNumber, SMSLog
from .forms import OutgoingNumberForm

@requires_login
def list_numbers (request):
    numbers = OutgoingNumber.objects.filter(owner=request.user, deleted=False)
    form = OutgoingNumberForm()
    return render(request, "tel/list_numbers.html", { "numbers" : numbers, "form" : form })

@requires_login
def add_number (request):
    form = OutgoingNumberForm()
    if request.POST:
        form = OutgoingNumberForm(request.POST)
        if form.is_valid():
            number = form.save(commit=False)
            number.name = u"{0}'s number".format(request.user)
            number.owner = request.user
            number.save()
            return HttpResponseRedirect(reverse("tel.views.send_verification", kwargs={"pk" : number.pk}))
    return render(request, "tel/add_number.html", { "form" : form })

@requires_login
def send_verification (request, pk):
    number = gog(OutgoingNumber, owner=request.user, pk=pk, deleted=False)
    if number.verified:
        return HttpResponseRedirect(reverse("tel.views.list_numbers"))

    code = number.roll_verification_code()
    sms.send(number, u"Please enter the following code: {0}".format(code))
    return HttpResponseRedirect(reverse("tel.views.verify_number", kwargs={"pk" : number.pk}))

@requires_login
def verify_number (request, pk):
    number = gog(OutgoingNumber, owner=request.user, pk=pk, deleted=False)

    if number.verified:
        return HttpResponseRedirect(reverse("tel.views.list_numbers"))

    error = None
    if request.POST:
        code = request.POST.get("code", None)
        if code and code.upper().strip() == number.verification_code:
            number.verified = True
            number.save()
            sms.send(number, "Welcome to Shiftor, be sure to check your emails in addition to text notifications to find your next job!")
            sms.send(number, "Thanks! You can stop messages at any time by sending STOP")
            return HttpResponseRedirect(reverse("tel.views.list_numbers"))
        else:
            error = "The verification code didn't match."
    return render(request, "tel/verify_number.html", { "number" : number, "error" : error })

@requires_login
def delete_number (request, pk):
    number = gog(OutgoingNumber, owner=request.user, pk=pk, deleted=False)
    if request.POST:
        number.delete()
    return HttpResponseRedirect(reverse("tel.views.list_numbers"))

@sms.twilio_request
def incoming (request):
    n_incoming = tree_get(request.POST, "To", lambda s: s.strip())
    n_outgoing = tree_get(request.POST, "From", lambda s: s.strip())
    body = tree_get(request.POST, "Body", lambda s: s.strip())

    incoming = gon(IncomingNumber, number=n_incoming)
    outgoing = gon(OutgoingNumber, number=n_outgoing)

    if not incoming or not outgoing:
        logger.error("Unable to locate either {0} (in) or {1} (out)".format(n_incoming, n_outgoing))
        return

    SMSLog(incoming=incoming, outgoing=outgoing,
        body=body, raw=json.dumps(request.POST),
        direction=SMSLog.INCOMING).save()

    user = outgoing.owner
    todolist = gon(user.todolist_set)

    # Check to see if the body is an available action
    if body == "HELP":
        pass
    elif body == "STOP":
        sms.send(outgoing, "Your text notifications has been suspended. Be sure to check your email or Login to Shiftor for more information.")
        outgoing.deleted = True
        outgoing.save()
    return HttpResponse("")


def main():
    import argparse
    import json
    import signal
    import sys

    from edman import DB

    from scripts.action import Action

    # Ctrl-Cを押下された時の対策
    signal.signal(signal.SIGINT, lambda sig, frame: sys.exit('\n'))

    # コマンドライン引数処理
    parser = argparse.ArgumentParser(description='ドキュメントの項目を修正するスクリプト')
    # parser.add_argument('-c', '--collection', help='collection name.')
    parser.add_argument('objectid', help='objectid str.')
    parser.add_argument('amend_file', type=open, help='JSON file.')
    parser.add_argument('structure', help='Select ref or emb.')
    parser.add_argument('-i', '--inifile', help='DB connect file path.')

    # 引数を付けなかった場合はヘルプを表示して終了する
    if len(sys.argv) == 1:
        parser.parse_args(["-h"])
        sys.exit(0)
    args = parser.parse_args()

    # 構造はrefかembのどちらか
    if not (args.structure == 'ref' or args.structure == 'emb'):
        parser.error("structure requires 'ref' or 'emb'.")

    try:
        # iniファイル読み込み
        con = Action.reading_config_file(args.inifile)

        # ファイル読み込み
        try:

            amend_data = json.load(args.amend_file)
        except json.JSONDecodeError:
            sys.exit('File is not json format.')
        except IOError:
            sys.exit('file read error.')

        #  DB接続
        db = DB(con)

        # 対象oidの所属コレクションを自動的に取得 ※動作が遅い場合は使用しないこと
        collection = db.find_collection_from_objectid(args.objectid)

        # アップデート処理
        if db.update(collection, args.objectid, amend_data, args.structure):
            print('アップデート成功')
        else:
            print('アップデート失敗')

    except Exception as e:
        tb = sys.exc_info()[2]
        sys.stderr.write(f'{type(e).__name__}: {e.with_traceback(tb)}\n')
        sys.exit(1)

if __name__ == "__main__":
    main()


# -*- Geoprocessing classes for AccessMod Data Prep Creator App -*-
# -*- By: Bibian Robert -*-
# -*- GEOG 489: Final Project -*-
# ***** Script Description ******

# This code contains AccessModDataPrep class derived from arcpy module, that contains methods that does
# geospatial data processing and provides functionality to select vector and raster files, perform clipping
# and projection, and save output files.
#  The AccessModataPrep class contains general methods including:
# listFieldNames:
# getStringFieldsForDescribeObject
# listFieldItems:
# getValidFieldsForShapefile:
# selectTargetAdmin:
# checkSpatialRef:
# listSpatialRef():

# There are two additional child classes of AccessModDataPrep class that class VectorProjectAndClip(AccessModDataPrep)
# that handles geoprocessing for vector data and class RasterClipAndProject(AccessModDataPrep) that handles
# geoprocessing for raster data.

# class VectorProjectAndClip(AccessModDataPrep) has the following methods:.
# def projectVector: projects vector data to a projected coordinate system
# def clipVector: clip vector data to target clip area
# def handleVecProjectAndClip : clips, reproject and saves new vector file

# class RasterClipAndProject(AccessModDataPrep)(AccessModDataPrep) has the following methods:.
# def rasProject: projects raster data to a projected coordinate system
# def clipFeature: clip raster data to target clip area
# def handleRasProjectAndClip : clips, reproject and saves new raster file

# import modules
import arcpy
import os

class AccessModDataPrep(object):
    def __init__(self):
        super(AccessModDataPrep, self).__init__()

    def listFieldNames(self, clipFeature):
        """The listFieldNames method takes the path to a vector dataset (feature class or shapefile) as clipFeature and
        returns a list of field names present in that dataset."""
        import arcpy
        fields = []

        fieldList = arcpy.ListFields(clipFeature)
        for field in fieldList:
            fields.append(field.name)
        return fields

    def getStringFieldsForDescribeObject(self, desc):
        """method takes a desc object as an input obtained from Describe function and returns a list of
         names of string-type fields from the provided desc object."""
        fields = []
        for field in desc.fields:  # go through all fields
            if field.type == 'String' and field.editable:
                fields.append(field.baseName)
        return fields

    def getValidFieldsForShapefile(self, clipFeature):
        """method takes path to a shapefile (clipFeature) as parameter and returns a list of valid
        string-type fields for the shapefile."""
        import arcpy
        fields = []
        if os.path.exists(clipFeature):
            desc = arcpy.Describe(clipFeature)
            try:  # trying to access shapeType may throw exception for certain kinds of data sets
                if desc.shapeType in ['Polygon']:
                    fields = self.getStringFieldsForDescribeObject(desc)
            except Exception as e:
                print(e)
                #fields = []
        return fields

    def listFieldItems(self, clipFeature, selectedField):
        """produces a list of items for the selected Field Name"""
        fieldsItems = []
        with arcpy.da.SearchCursor(clipFeature, (selectedField)) as cursor:
            for row in cursor:
                fieldsItems.append(row[0])
            return fieldsItems

    def selectTargetAdmin(self, clipFeature, nameField, county):
        """produces a list of items for the selected Field Name"""
        try:
            whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(clipFeature, nameField), county)

            targetCounty = arcpy.SelectLayerByAttribute_management(clipFeature, 'NEW_SELECTION', whereClause)

            return targetCounty
        except Exception as e:
            print(e)
            print(arcpy.GetMessages())

    # Check the spatial reference of the feature selected
    def checkSpatialRef(self, inputFeature):
        desc = arcpy.Describe(inputFeature)
        spatialRef = desc.spatialReference

        if spatialRef.name == "Unknown":
            return "{} has an unknown spatial reference".format(desc.name)  # print
        # Otherwise, print out the feature class name and spatial reference
        elif spatialRef.type == "Geographic":
            return "Current SRS for {} : {} , '\n',""'you need to reproject'".format(desc.name, spatialRef.name)
        elif spatialRef.type == "Projected":
            return "Current SRS for {} : {} , '\n',""'confirm it's projected'".format(
                desc.name, spatialRef.name)

    @staticmethod
    # this method takes no parameter & lists all projected UTM coordinate reference system and is called directly on
    # the class without creating an instance of the class-----------------------------------------------------
    def listSpatialRef():
        """ obtain a list of spatial reference systems (SRS) with names containing "UTM" and are of type "Projected
        Coordinate System" (PCS). It then processes the obtained spatial reference systems to extract the names and
        returns them as a list."""

        srs = arcpy.ListSpatialReferences("*UTM*", "PCS")
        srsList = []
        for sr in srs:
            cord = sr.split('/')[-1]  # extract the name of the PCS by taking last part of the  splitting the SRS string
            srsList.append(cord)
        return srsList

# class that handles clipping and projection of vector data-------------------------------------------------------------
class VectorProjectAndClip(AccessModDataPrep):
    def __init__(self):
        super(VectorProjectAndClip, self).__init__()

    # method that projects vector data---
    def projectVector(self, clippedFeature, outputClipFeature, outputCrs):
        try:
            # Perform the projection of vector data---
            arcpy.Project_management(clippedFeature, outputClipFeature, outputCrs)
            print("Feature has been projected successfully.")

        except Exception as e:
            print("Error occurred during projection:", e)

    # method that clips vector data---
    def clipVector(self, inputFile, targetCounty, outPut):
        try:
            arcpy.Clip_analysis(inputFile, targetCounty, outPut)  # performs clipping
            print("Feature has been clipped successfully.")

        except Exception as e:
            print("Error occurred during clipping:", e)

    # method that handles both clipping and projection of the same vector file
    def handleVecProjectAndClip(self, inputFeatures, outputClipFeatures, outputCrs, targetCounty):
        arcpy.env.overwriteOutput = True  # overwrites any existing file with similar names
        # Create a temporary output clip feature class for clipping and saved on temporary local disk---
        tempClipped = r"C:\Users\brobert\OneDrive - Kemri Wellcome Trust\PennState\GEOG 489\Final " \
                      r"Project\Output\temp_clipped.shp"
        # call the clipFeature method for clipping---
        self.clipVector(inputFeatures, targetCounty, tempClipped)  # clips feature and saved temporarily

        # # Create a temporary output reproject feature class for projection and saved on temporary local disk---
        tempOutput = r"C:\Users\brobert\OneDrive - Kemri Wellcome Trust\PennState\GEOG 489\Final " \
                     r"Project\Output\temp_output.shp"

        # Call the project function to project the clipped feature---
        self.projectVector(tempClipped, tempOutput, outputCrs)  # projects the temp clipped feature and saved temporarily

        # Copy the projected Vector feature to the final output feature class---
        arcpy.CopyFeatures_management(tempOutput, outputClipFeatures)

        # Clean up the temporary feature classes
        arcpy.Delete_management(tempClipped)
        arcpy.Delete_management(tempOutput)

# class that handles clipping and projection of raster data-------------------------------------------------------------
class RasterClipAndProject(AccessModDataPrep):
    def __init__(self):
        super(RasterClipAndProject, self).__init__()

    # method that projects raster data---
    def projectRaster(self, inputRaster, outputRaster, outputCrs):
        try:
            arcpy.ProjectRaster_management(inputRaster, outputRaster, outputCrs)
            print("Raster has been projected successfully.")
        except Exception as e:
            print("Error occurred during raster projection:", e)

    # method that clips raster data---
    def clipRaster(self, inputRaster, targetCounty, outputRaster):
        try:
            arcpy.Clip_management(inputRaster, "#", outputRaster, targetCounty, "#", "ClippingGeometry")
            print("Raster has been clipped successfully.")
        except Exception as e:
            print("Error occurred during raster clipping:", e)

    # method that handles both clipping and projection of the same Raster file---
    def handleRasProjectAndClip(self, inputRaster, outputClipRaster, outputCrs, targetCounty):
        arcpy.env.overwriteOutput = True
        # Create a temporary output clip feature class for clipping---
        tempClipped = r"C:\Users\brobert\OneDrive - Kemri Wellcome Trust\PennState\GEOG 489\Final " \
                      r"Project\Output\temp_clipped.tif"
        # Call the clipRaster method for clipping---
        self.clipRaster(inputRaster, targetCounty, tempClipped)

        # Create a temporary output feature class for projection---
        tempOutput = r"C:\Users\brobert\OneDrive - Kemri Wellcome Trust\PennState\GEOG 489\Final " \
                     r"Project\Output\temp_output.tif"

        # Call the project function to project the clipped feature
        self.projectRaster(tempClipped, tempOutput, outputCrs)

        # Copy the projected Raster feature to the final output feature class---
        arcpy.CopyRaster_management(tempOutput, outputClipRaster)
        # print(out)
        # Clean up the temporary feature classes
        arcpy.Delete_management(tempClipped)
        arcpy.Delete_management(tempOutput)


__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import os
import logging
import sys
import numpy
import mmap
import re
import time
from PyMca5.PyMcaIO import JcampReader
from PyMca5.PyMcaIO import SpecFileAbstractClass
if sys.version < "3":
    from StringIO import StringIO
else:
    from io import StringIO
_logger = logging.getLogger(__name__)

class JcampFileParser(SpecFileAbstractClass.SpecFileAbstractClass):
    def __init__(self, filename, single=False):
        # get the number of entries in the file
        self.__lastEntryData = -1
        t0 = time.time()
        if sys.maxsize > 2**32:
            self._useMMap = True
        else:
            self._useMMap = False
        _logger.debug("USING MMPA = %s", self._useMMap)
        if self._useMMap:
            # 64-bit supported
            f = open(filename, "rb")
            mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
            a = [x.start() for x in re.finditer("##TITLE".encode("utf-8"), mm)]
            b = [x.end() for x in re.finditer("##END.*\n".encode("utf-8"), mm)]
            mm = None
            f.close()
            # we can have twice the TITLE before the first data block
            # we get rid of the second to have also the file header
            if len(a) > 1:
                if a[1] < b[0]:
                    del a[1]
            self._scanLimits = [(start, end) for start, end in zip(a, b)]
        else:
            # 32-bit
            self._scanLimits = []
            f = open(filename, "rb")
            entryStarted = False
            current = f.tell()
            line = f.readline()
            nLines = 0
            while len(line):
                if entryStarted:
                    if line.startswith("##END=".encode("utf-8")):
                        lineEnd = nLines
                        self._scanLimits.append((start, f.tell(), lineStart, lineEnd))
                        entryStarted = False
                        if single:
                            break
                elif line.startswith("##TITLE".encode("utf-8")):
                    start = current
                    lineStart = nLines
                    entryStarted = True
                nLines += 1
                current = f.tell()
                line = f.readline()
            f.close()
        _logger.debug("Elapsed CURRENT = %s",
                      time.time() - t0)
        self._filename = os.path.abspath(filename)
        _logger.debug("PARSING FIRST ")
        t0 = time.time()
        self._parseEntryData(0)
        elapsed = time.time() - t0
        _logger.debug("ELAPSED PER SCAN = %s", elapsed)
        _logger.debug("N SCANS = %s", self.scanno())
        _logger.debug("EXPECTED = %s", elapsed * self.scanno())

    def _parseEntryData(self, idx):
        if idx == self.__lastEntryData:
            # nothing to be done
            return
        if (idx < 0) or (idx >= len(self._scanLimits)):
            raise IndexError("Only %d entries in file. Requested %d" % (len(self._scanLimits), idx))
        start, end = self._scanLimits[idx][0:2]
        if self._useMMap:
            #get the relevant file section
            f = open(self._filename, "rb")
            scanBuffer = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)[start:end]
            f.close()
            # the Reader expects to work with strings and not with bytes
            scanBuffer = scanBuffer.decode("utf-8")
            fileBlock = True
        else:
            #get the relevant file section
            f = open(self._filename, "r")
            start, end = self._scanLimits[idx][0:2]
            f.seek(start)
            scanBuffer = StringIO(f.read(1 + end - start))
            f.close()
            fileBlock = False
        instance = JcampReader.JcampReader(scanBuffer, block=fileBlock)
        info = instance.info
        jcampDict = info
        x, y = instance.data
        title = jcampDict.get('TITLE', "Unknown scan")
        xLabel = jcampDict.get('XUNITS', 'channel')
        yLabel = jcampDict.get('YUNITS', 'counts')
        try:
            fileheader = instance._header
        except Exception:
            _logger.warning("JCampFileParser cannot access '_header' attribute")
            fileheader=None
        data = numpy.zeros((x.size, 2), numpy.float32)
        data[:, 0] = x
        data[:, 1] = y
        self.scandata = []
        scanheader = ["#S %d %s" % (2*idx + 1, title),
                      "#N 2",
                      "#L %s  %s" % (xLabel, yLabel)]
        scanData = JCAMPFileScan(data,
                                 scantype="SCAN",
                                 scanheader=scanheader,
                                 labels=[xLabel, yLabel],
                                 fileheader=fileheader)
        self.scandata.append(scanData)
        scanheader = ["#S %d %s" % (2*idx + 2, title)]
        if jcampDict['XYDATA'].upper() ==  '(X++(Y..Y))':
            # we can deal with the X axis via its calibration
            scanheader.append("#@CHANN %d  %d  %d  1" % (len(x), 0, len(x) - 1))
            scanheader.append("#@CALIB %f %f 0" % (x[0], x[1] - x[0]))
            scantype = "MCA"
        scanData = JCAMPFileScan(data, scantype="MCA",
                                                      scanheader=scanheader,
                                                      #labels=[xLabel, yLabel],
                                                      fileheader=fileheader)
        self.scandata.append(scanData)
        self.__lastEntryData = idx

    def __getitem__(self, item):
        if item < 0:
            item = self.scanno() - item
        idx = item // 2
        self._parseEntryData(idx)
        return self.scandata[item % 2]

    def list(self):
        return '1:%d' % self.scanno()

    def scanno(self):
        return len(self.scandata) * len(self._scanLimits)

class JCAMPFileScan(SpecFileAbstractClass.SpecFileAbstractScan):
    def __init__(self, data, scantype="SCAN",
                 scanheader=None, labels=None, fileheader=None):
        SpecFileAbstractClass.SpecFileAbstractScan.__init__(self, data,
                            scantype=scantype, scanheader=scanheader,
                            labels=labels)
        self._data = data
        self._fileHeader = fileheader

    def fileheader(self, key=''):
        return self._fileHeader

    def nbmca(self):
        if self.scantype == 'SCAN':
            return 0
        else:
            return 1

    def mca(self, number):
        if number not in [1]:
            raise ValueError("Specfile mca numberig starts at 1")
        return self._data[:, number]

def isJcampFile(filename):
    return JcampReader.isJcampFile(filename)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python JCAMPFileParser.py filename")
        sys.exit(0)
    print(" isJCAMPFile = ", isJcampFile(sys.argv[1]))
    sf = JcampFileParser(sys.argv[1])
    print("nscans = ", sf.scanno())
    print("list = ", sf.list())
    print("select = ", sf.select(sf.list()[0]))


from defs import *
import gzip, tarfile, shutil

def targz_dir(outer_dir, dirs_list, dest_zip_filename, delete_after_zipping):
    """
    Compress data from multiple directories to a single .targz file
    :param outer_dir: where the output file should be written to
    :param dirs_list: the directories names to be compressed
    :param dest_zip_filename: name of output file
    :param delete_after_zipping: True or False
    :return: NA
    """
    cwd = os.getcwd()
    os.chdir(outer_dir)
    tarw = tarfile.open(dest_zip_filename, "w:gz")
    for dirname in dirs_list:
        if os.path.exists(dirname):
            tarw.add(dirname)
    tarw.close()
    if delete_after_zipping:
        for dirname in dirs_list:
            try:
                shutil.rmtree(dirname)
            except Exception as e1:
                print(e1)
                pass
    os.chdir(cwd)

def untargz(zip_file_dest, delete_after_extracting=False):
    """
    De-compress file
    :param zip_file_dest: where to unpack the zipped file
    :param delete_after_extracting: True or False
    :return: NA
    """
    dirpath, zip_filename = os.path.split(zip_file_dest)
    cwd = os.getcwd()
    os.chdir(dirpath)
    tarx = tarfile.open(zip_file_dest, "r:gz")
    tarx.extractall(dirpath)
    tarx.close()
    os.chdir(cwd)
    if delete_after_extracting:
        os.remove(zip_file_dest)

def average(lst):
    """
    Calculates the average of a given list
    :param lst: list of numbers
    :return: average value
    """
    return sum(lst) / len(lst)

def range_of_lst(lst):
    """
    calculates the range of a given list
    :param lst: list of numbers
    :return: max-min
    """
    return max(lst) - min(lst)

def fix_simulated_tree_file(tree_file):
    """
    a patch that adds a semicolon at the end of .phr tree (simulated tree) so the can tree can be read
    :param tree_file:
    :return: NA
    """
    with open(tree_file, "a") as add:
        add.write(";")

def create_counts_hash(counts_file):
    """
    puts all counts from a counts file in a dictionary: taxa_name: count. Taxa with an X count are skipped.
    If there are counts that are X the function returns two hashes and a set of the taxa to prune.
    :param counts_file in FASTA format
    :return:(2) dictionary of counts
    """
    d = {}
    with open(counts_file, "r") as counts_handler:
        for line in counts_handler:
            line = line.strip()
            if line.startswith('>'):  # taxon name
                name = line[1:]
            else:
                if line != "x":
                    num = int(line)
                    d[name] = num
    return d

def regex_internal(str):
    """
    extracts the number from the internal node's name from a phylogeny, in a NX-XX format. Used in get_max_transition(tree_file)
    :param str: internal node's label
    :return: count of node
    """
    tmp = re.search("N\d+\-(\d+)", str)
    if tmp:
        num = int(tmp.group(1))
    return num

def regex_tip(str):
    """
    extracts the number from a tip label in a phylogeny
    :param str: tip label
    :return: count of tip
    """
    tmp = re.search("(\d+)", str)
    if tmp:  # there is a number at the tip, and not X
        num = int(tmp.group(1))
    return num

def get_max_transition(tree_file):
    """
    searches for the largest transition that was made on the phylogeny itself, between internal node and tip, or between internal nodes. Used in base_num_models.py
    :param tree_file: phylogeny file
    :return: a number representing the maximal transition on the tree
    """
    t = Tree(tree_file, format=1)
    max_transition = 0
    for node in t.traverse():
        if node.name == "":
            continue
        if not node.is_leaf():
            num1 = regex_internal(node.name)
            for child in node.get_children():
                if child.is_leaf():  # if the child is a tip - parse number from tip label
                    num2 = regex_tip(child.name)
                else:  # if the child is an internal node - take number
                    num2 = regex_internal(child.name)
                tmp_score = abs(num1 - num2)
                if max_transition < tmp_score:
                    max_transition = tmp_score
    return max_transition

def copy_files(src, dest, files=None):
    """
    copy all files from source directory to destination directory. Used when re-running BASE NUM models, to keep previous results
    :param src: source directory
    :param dest: destination directory
    :param files: a list of specific files to be copied
    :return: NA
    """
    src_files = os.listdir(src)
    if not os.path.exists(dest):
        os.mkdir(dest)
    if files is not None:
        src_files = files
    for file_name in src_files:
        full_src_file_name = src + file_name
        full_dest_file_name = dest + file_name
        if os.path.isfile(full_src_file_name):
            shutil.copy(full_src_file_name, full_dest_file_name)

def extract_line_from_file(filename, str_search, num=False, integer=False):
    """
    uses regular expression to search a string in a file
    :param filename: the file to be searched in
    :param str_search: the string to search for
    :param num: should a number be extracted?
    :param integer: should an integer be returned? if False and num=True then a float will be returned
    :return: True/False for a sentence
                integer if num=True and integer=True
                float if num=True and integer=False
    """
    with open(filename, "r") as fh:
        for line in fh:
            line = line.strip()
            if num:
                tmp = re.search(str_search + ".*?(\d+)", line)
                if tmp:
                    return int(tmp.group(1)) if integer else float(tmp.group(1))
            else:
                tmp = re.search(str_search, line)
                if tmp:
                    return True
        return False

def n_folders_lst(n):
    """
    used to targz all simulations to a single file
    :return: list with names of folders from 0 to n
    """
    lst = []
    for i in range(n):
        lst.append(str(i))
    return lst

def print_error(msg, exception="", sep="*****"):
    print(sep)
    print(msg, "\n")
    print(sep)
    if exception is not "":
        print(exception)
        print(sep)


from comparators.morphologic_comparator import MorphologicComparator
from text.text_searcher import TextSearcher

class BachelorPadExtractor:
    attribute_name = 'bachelor pad'

    def __call__(self, description: str):
        phrases_to_look_for = ['kawalerka', 'studio', 'garsoniera', 'jednopokojowe', '1-pokojowe', '1 pokojowe', 'jedno pokojowe']

        comparator = MorphologicComparator().equals

        for phrase in phrases_to_look_for:
            found, _ = TextSearcher.find(
                phrase_to_find=phrase,
                text=description,
                equality_comparator=comparator)

            if found:
                return {True}

        return {False}


# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring, protected-access

import numpy as np
import scipy.sparse as sp

from AnyQt.QtCore import QRectF, QPointF

from Orange.data import Table, DiscreteVariable, ContinuousVariable, Domain
from Orange.widgets.data import owpaintdata
from Orange.widgets.data.owpaintdata import OWPaintData
from Orange.widgets.tests.base import WidgetTest, datasets

class TestOWPaintData(WidgetTest):
    def setUp(self):
        self.widget = self.create_widget(
            OWPaintData, stored_settings={"autocommit": True}
        )  # type: OWPaintData

    def test_empty_data(self):
        """No crash on empty data"""
        data = Table("iris")
        self.send_signal(self.widget.Inputs.data, data)
        self.send_signal(self.widget.Inputs.data, Table(data.domain))

    def test_nan_data(self):
        data = datasets.missing_data_2()
        self.send_signal(self.widget.Inputs.data, data)

    def test_output_shares_internal_buffer(self):
        data = Table("iris")[::5]
        self.send_signal(self.widget.Inputs.data, data)
        output1 = self.get_output(self.widget.Outputs.data)
        output1_copy = output1.copy()
        self.widget._add_command(owpaintdata.SelectRegion(QRectF(0.25, 0.25, 0.5, 0.5)))
        self.widget._add_command(owpaintdata.MoveSelection(QPointF(0.1, 0.1)))
        output2 = self.get_output(self.widget.Outputs.data)
        self.assertIsNot(output1, output2)

        np.testing.assert_equal(output1.X, output1_copy.X)
        np.testing.assert_equal(output1.Y, output1_copy.Y)

        self.assertTrue(np.any(output1.X != output2.X))

    def test_20_values_class(self):
        domain = Domain(
            [ContinuousVariable("A"), ContinuousVariable("B")],
            DiscreteVariable("C", values=[chr(ord("a") + i) for i in range(20)]),
        )
        data = Table(domain, [[0.1, 0.2, "a"], [0.4, 0.7, "t"]])
        self.send_signal(self.widget.Inputs.data, data)

    def test_sparse_data(self):
        """
        Show warning msg when data is sparse.
        GH-2298
        GH-2163
        """
        data = Table("iris")[::25]
        data.X = sp.csr_matrix(data.X)
        self.send_signal(self.widget.Inputs.data, data)
        self.assertTrue(self.widget.Warning.sparse_not_supported.is_shown())
        self.send_signal(self.widget.Inputs.data, None)
        self.assertFalse(self.widget.Warning.sparse_not_supported.is_shown())

    def test_load_empty_data(self):
        """
        It should not crash when old workflow with no data is loaded.
        GH-2399
        """
        self.create_widget(OWPaintData, stored_settings={"data": []})


import speech_recognition as sr
import os
import random
import time

def recognize_speech_from_mic(recognizer, microphone):
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    response = {
        "success": True,
        "error": None,
        "transcription": None
    }
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"
    return response

def spellWord(word):
    leng = len(word)
    n = 0
    os.system("say " + word)
    while n<leng:
        if word[n] == ' ':
            os.system("say space")
        else:
            os.system("say " + word[n])
        n+=1
    os.system("say " + word)

if __name__ == "__main__":
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    print("Working...")
    time.sleep(1)

    print("Say the word you want to spell.")
    said = recognize_speech_from_mic(recognizer,microphone)
    if said["transcription"]:
        print("you said ", said["transcription"])
        word = said["transcription"].lower()
        spellWord(word)


import re
from bb_sub import *

with open('markdown.txt', 'rb') as mkdwn:
    markdown_string = mkdwn.read().decode()

bbcode = bbify(markdown_string)

with open("bbcode.txt", 'w') as bbcd:
    bbcd.write(bbcode)

# Possibly remove?
# Break markdown into segments and classify each segment.
# `segments` contains data in the following format:
#
# <index>(int): {<classifier>(str): <segment>(str)}
#
# e.g. "# Title Text" would be stored as:
#
# 0: {header_1: 'Title Text'}

# segments = {}
# seg_index = 0
# bbcode_string = ''
# for line in markdown_string.split('\n'):
#     # HEADERS
#     header_tag = re.search("^#.*", line) # Finds all header lines
#     if header_tag:
#         count = header_tag.group(0).count('#') # Number of '#' refers to size of header

#         # segments[seg_index] = {"header_%d" % count: line.split('\r')[0]}
#     # BOLD
#     bold_tag = re.search("\*\*.*\*\*", line)
#     if bold_tag:


import os

logs_path = "data/logs"
log_dirs = os.listdir(logs_path)
for log_dir in log_dirs:
    if log_dir.find("gamma0") != -1:
        old_path = os.path.join(logs_path, log_dir)
        new_name = log_dir.replace("gamma0", "gamma-0")
        new_path = os.path.join(logs_path, new_name)
        os.rename(old_path, new_path)


import errno
import socket

from riak.transports.pool import Pool, ConnectionClosed
from riak.transports.tcp.transport import TcpTransport

class TcpPool(Pool):
    """
    A resource pool of TCP transports.
    """
    def __init__(self, client, **options):
        super(TcpPool, self).__init__()
        self._client = client
        self._options = options

    def create_resource(self):
        node = self._client._choose_node()
        return TcpTransport(node=node,
                            client=self._client,
                            **self._options)

    def destroy_resource(self, tcp):
        tcp.close()

# These are a specific set of socket errors
# that could be raised on send/recv that indicate
# that the socket is closed or reset, and is not
# usable. On seeing any of these errors, the socket
# should be closed, and the connection re-established.
CONN_CLOSED_ERRORS = (
    errno.EHOSTUNREACH,
    errno.ECONNRESET,
    errno.ECONNREFUSED,
    errno.ECONNABORTED,
    errno.ETIMEDOUT,
    errno.EBADF,
    errno.EPIPE
)

def is_retryable(err):
    """
    Determines if the given exception is something that is
    network/socket-related and should thus cause the TCP connection to
    close and the operation retried on another node.

    :rtype: boolean
    """
    if isinstance(err, ConnectionClosed):
        # NB: only retryable if we're not mid-streaming
        if err.mid_stream:
            return False
        else:
            return True
    elif isinstance(err, socket.error):
        code = err.args[0]
        return code in CONN_CLOSED_ERRORS
    else:
        return False


# candidate idea for extra credit problem involving recursion.
#
# This will be a reading exercise to trace through a recursive function
#
# Part 1: play this game with the student until they see invariant that needs to be maintained
# at every round is coins left is divisible by 4. This serves as an intuitive basis for
# understanding the play_one_round function
#
# Part 2:
# In the play_one_round, the computer player always takes one coin when it detects it will
# lose. Have student identify where this is occurring and have them change it to
# randomly pick 1,2,or3 coins
#
# Part 3:
# There's only one round of play
# Modify play_game so that after a game is finished, the user is prompted to play another game
# Play another game is the user responds yes.
#
# Ultimately, I decided this isn't a good candidate for a student exercise, but keeping around
# in case I get an idea for it.

def play_one_round(n):
    print('There are ' + str(n) +' coins on the table.')
    coins_to_remove = int(input('How many coins do you want to remove: 1, 2, or 3?'))
    if coins_to_remove > 3:
        coins_to_remove = 3
    if coins_to_remove < 1:
        coins_to_remove = 1
    print("You took " + str(coins_to_remove) + " coins")
    if coins_to_remove >= n:
        print("You cleared the table")
        return True
    coins_left = n - coins_to_remove
    if coins_left <= 3:
        print("I take " + str(coins_left) + " coins. There are no more coins.")
        return False
    else:
        if coins_left % 4 == 0:
            print("I take one coin")
            coins_left -= 1
        else:
            print("I take " + str(coins_left % 4) + " coin(s)")
            coins_left -= coins_left % 4
    return play_one_round(coins_left)

def play_game(n):
    if n <= 0:
        print('There are no coins, so there is no game.')
        return
    print('The goal is to take the last coin from the table.')
    result = play_one_round(n)
    if result:
        print('You win!')
    else:
        print('You lost!')

play_game(21)


class PersonalAssistant:
    def __init__(self):
        self.task_automation = None
        self.personal_assistance = None
        self.training_and_guidance = None

    def assist(self, task):
        # Assist with the given task
        if task == 'task automation':
            self.task_automation = 'Task automation in progress, automating repetitive tasks such as file management, updates, and organizing data'
            return self.task_automation
        elif task == 'personal assistance':
            self.personal_assistance = 'Personal assistance in progress, acting like a virtual friend, providing reminders, help with scheduling, and offering social interaction'
            return self.personal_assistance
        elif task == 'training and guidance':
            self.training_and_guidance = 'Training and guidance in progress, offering personalized training courses, tutorials, and step-by-step guides on various subjects and software tools'
            return self.training_and_guidance
        else:
            return 'Invalid task'


import numpy as np
import matplotlib.pyplot as plt
from scipy.special import factorial

k = 500 # number of generations
p = 100 # number of individuals
t = 0.01 # unit time, e.g., 1 second

x = np.arange(1, 11)

y1 = factorial(x) * t # brute-force solver
y2 = x * p * k * t # GA solver

y1 = y1 / (3600)
y2 = y2 / (3600)

fontsize = 15
linewidth = 2

fig, ax = plt.subplots()

ax.plot(x, y1, linewidth=linewidth, label='Brute-force Solver')
ax.plot(x, y2, linewidth=linewidth, label='GA Solver')

plt.xlabel('The number of tasks', fontsize=fontsize)
plt.ylabel('Time complexity (unit time)', fontsize=fontsize)
plt.xticks( fontsize=fontsize)
plt.yticks( fontsize=fontsize)
plt.grid()

legend = plt.legend(bbox_to_anchor=(-0.07, 0.96, 1.1,1), loc=3, shadow=False,mode='expand',ncol=6,fontsize='x-large',frameon=False)

plt.show()
fig.set_size_inches(5, 3)
plt.subplots_adjust(
    left=0.15,
    bottom=0.2,
    right=0.992,
    top=0.848,
    wspace=0.2,
    hspace=0.2,
)
fig.show()
fig.savefig("complexity.pdf")


import unittest
from classes.GEDCOM_Reporting import Report, ReportDetail
from classes.GEDCOM_Units import GEDCOMUnit, Individual, Family

class TestNoMarriageToDescendants(unittest.TestCase):
    def test_no_marriage_to_descendants_no_issues(self):
        testReport: Report = Report()
        i1 = Individual("I1", "John Doe", "M", None, None, None, ["F1"])
        i2 = Individual("I2", "Jane Smith", "F", None, None, None, ["F1"])
        i3 = Individual("I3", "Child 1", "F", None, None, "F1", None)

        f1 = Family("F1", "I1", "I2", ["I3"], None, None)

        testReport.addToReport(i1)
        testReport.addToReport(i2)
        testReport.addToReport(i3)
        testReport.addToReport(f1)

        testReport.no_marriage_to_descendants()

        # Assert that there are no descendant marriages
        self.assertEqual(len(testReport.anomalies), 0)

    def test_marriage_to_descendant(self):
        testReport: Report = Report()
        i1 = Individual("I1", "John Doe", "M", None, None, None, ["F1"])
        i2 = Individual("I2", "Jane Smith", "F", None, None, None, None)
        i3 = Individual("I3", "Child 1", "F", None, None, "F1", ["F1"])

        f1 = Family("F1", "I1", "I3", ["I3"], None, None)

        testReport.addToReport(i1)
        testReport.addToReport(i2)
        testReport.addToReport(i3)
        testReport.addToReport(f1)

        testReport.no_marriage_to_descendants()

        # Assert that there are no descendant marriages
        self.assertEqual(len(testReport.anomalies), 1)
        self.assertEqual(testReport.anomalies[0].detailType, "Marriage to Descendant")
        self.assertEqual(testReport.anomalies[0].message, "I1 is married to descendant, I3.")


import cv2
import numpy as np
import os
import sys
video_capture = cv2.VideoCapture(0)

try:
	directory = './Images/' + sys.argv[1]
except:
	print('\nPlease provide an argument')
	print('\nExiting...\n')
	quit()

if not os.path.exists(directory):
	os.makedirs(directory)

x = 750 #top left x of box
y = 50  #top left y of box
h = 400 #height of box
w = 400 #width of box

k = None #key press variable
b = 0 #to check if space has been pressed
count = 0
while True:
	_, frame = video_capture.read()
	frame = cv2.flip(frame,1)
	cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,255), 5)
	roi = frame[y:y+h,x:x+w] #we get the region of interest
	roi = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY) #convert it to grayscale
	_, mask = cv2.threshold(roi, 230,255,cv2.THRESH_OTSU) #use OTSU to convert grayscale to monochrome by thresholding
	mask = cv2.bitwise_not(mask) #flip the mask
	blurred = cv2.medianBlur(mask, 7) #apply median blur for smoothing of edges.
	kernel = np.ones((5,5), np.uint8)
	img_dilation = cv2.dilate(blurred, kernel, iterations=1) #apply dilation
	frame[y:y+h,x:x+w,0] = img_dilation
	frame[y:y+h,x:x+w,1] = img_dilation
	frame[y:y+h,x:x+w,2] = img_dilation
	cv2.imshow('Video', frame)
	if count<300 and b:
		path = directory + '/' + sys.argv[1] +str(count) + '.jpeg'
		cv2.imwrite(path, img_dilation)
		count+=1
	# cv2.imshow('Mask', mask)
	# cv2.imshow('blurred', blurred)
	k = cv2.waitKey(1) & 0xFF
	if k == 32:  #Press space to start capturing frames.
		b = 1
	if k == ord('q'):
		break
video_capture.release()
cv2.destroyAllWindows()


import datetime

from fastapi import HTTPException, status

from app.v1.model.user_model import User as UserModel
from app.v1.model.pets_model import Pets as PetsModel
from app.v1.model.request_model import Request as RequestModel
from app.v1.schema import user_schema

def create_user(user: user_schema.UserBase):
    get_user = UserModel.filter(UserModel.email == user.email).first()
    if get_user:
        if get_user.email == user.email:
            msg = "Correo ya existente"
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=msg
        )

    db_user = UserModel(
        name = user.name,
        last_name = user.last_name,
        phone = user.phone,
        email = user.email,
        address= user.address
    )

    db_user.save()

    return user_schema.User(
        id = db_user.id,
        name = db_user.name,
        last_name = db_user.last_name,
        phone = db_user.phone,
        email = db_user.email,
        address= db_user.address
    )

def get_user(email: str):
    user = UserModel.filter((UserModel.email== email)).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Usuario no encontrado"
        )

    return user_schema.User(
        id = user.id,
        name = user.name,
        last_name = user.last_name,
        phone = user.phone,
        email = user.email,
        address= user.address
    )

def update_user(email: str, name: str, last_name: str, phone: int, address: str):
    user = UserModel.filter(UserModel.email == email).first()
    if not user :
        raise HTTPException(
            status_code= status.HTTP_404_NOT_FOUND,
            detail= "Usuario no encontrado"
        )
    user.name = name
    user.last_name = last_name
    user.phone = phone
    user.address = address
    user.modified_at = datetime.datetime.now()

    user.save()

    return user_schema.User(
        id = user.id,
        email= user.email,
        name = user.name,
        last_name = user.last_name,
        phone = user.phone,
        address= user.address
    )

def get_list_users_admin():
    list_users = []
    for i in range(0, 100):
        user = UserModel.filter((UserModel.id ==i)).first()
        if user is None:
            i += 1
        else:
            list_users.append(user_schema.User(
                id = user.id,
                email= user.email,
                name = user.name,
                last_name = user.last_name,
                phone = user.phone,
                address= user.address
            )
        )
    return list_users

def delete_user(id_user: int):
    user = UserModel.filter((UserModel.id == id_user)).first()
    pets = PetsModel.filter((PetsModel.user == id_user)).first()
    request = RequestModel.filter((RequestModel.user == id_user)).first()

    if pets or request is not None:
        raise HTTPException(
            status_code= status.HTTP_400_BAD_REQUEST,
            detail= "Usuario no puede ser eliminado, posee mascotas o solicitudes"
        )

    if not user:
        raise HTTPException(
            status_code= status.HTTP_404_NOT_FOUND,
            detail= "Usuario no encontrado"
        )

    user.delete_instance()


# -*- coding: utf-8 -*-
import os
import scrapy
from scrapy.utils.response import get_base_url
from scrapy.spiders import Rule,CrawlSpider
from scrapy.linkextractors import LinkExtractor
from urllib.parse import urlparse
from scrapy.http import Request,FormRequest
from scrapy.selector import Selector
from ScrawlWeibo.prelogin import PreLogin
import json
import re
import io
import gzip
import lxml
from bs4 import BeautifulSoup
from urllib.parse import urljoin
class Scrawler(CrawlSpider):
    name = 'scrawler'
    start_urls = [
        #'http://newids.seu.edu.cn/authserver/login?goto=http://my.seu.edu.cn/index.portal'
        ]
    rules={
            #Rule(LinkExtractor(allow=('.*')), callback='parse_user')
        }
    headers={
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0",
            "Referer":"http://weibo.com/",
            "Accept":"*/*",
            "Connection":"keep-alive",
            "Host":"login.sina.com.cn",
            "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
            "Accept-Encoding":"gzip, deflate, br",
            "Cookie":"U_TRS1=000000af.b4a63d29.57c7d7f5.e795e0db; UOR=www.baidu.com,blog.sina.com.cn,; SINAGLOBAL=223.3.76.88_1472714742.511501; ULV=1475935395234:14:6:5:223.3.76.88_1475931746.660821:1475931744422; vjuids=-ce103e475.156e4a68a21.0.a40e693838e5e; vjlast=1472714935.1476091377.11; lxlrtst=1475130147_o; lxlrttp=1475130147; SCF=AnJFF8L6wR9zJ4vCHpHt2dHlyIQk6Cyra1AIxugDmi0MJscTImbMappZJyQwTrv_VXpl91bqedD5-wzu9y_uno0.; SUB=_2AkMgorWXdcNhrAJZnPwUyGvgbI9H-jzEiebBAn7tJhMyAhh77lxSqSVFN6emcXZ8gvni6GG9IfWIhtxZxA..; SUBP=0033WrSXqPxfM72wWs9jqgMF55529P9D9WFxw.fzHUb0FHrpb-vmMOv35JpV2020SK.RSh27S0z0BGSDdJ2VqcRt; __gads=ID=1a44820a22e8c483:T=1476091387:S=ALNI_MbJQ7RZ0km7VLUYWkUY8Sjs-tRrLg; Apache=223.3.76.88_1476348445.370989"
            }
    formdata={
            "encoding":"UTF-8",
            "weibo":"weibo",
            "from":"",
            "gateway":"1",
            "pagerefer":"",
            "pwencode":"rsa2",
            "returntype":"META",
            "service":"miniblog",
            "sr":"1920*1080",
            "url":"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack",
            "useticket":"1",
            "vsnf":"1",
            "prelt":"176"
            }
    def start_requests(self):
        print("start prelogin")
        prelogin=PreLogin()
        data=prelogin.get_data()
        self.formdata['su']=data['su']
        self.formdata['sp']=data['sp']
        self.formdata['nonce']=data['nonce']
        self.formdata['servertime']=data['servertime']
        self.formdata['rsakv']=data['rsakv']
        yield FormRequest(url='http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)',
                formdata=self.formdata,
                callback=self.redirect)
    def redirect(self,response):
        print("redirect...")
        content=response.body.decode('GBK')
        pattern=r"location\.replace\('(.*?)'\)"
        redirect_url=re.findall(pattern,content)[0]
        print('going to '+redirect_url)
        return Request(url=redirect_url,method='GET',callback=self.after_redirect)
    def after_redirect(self,response):
        print("redirected")
        content=response.body.decode('utf-8')
        #print(content)
        pattern=r'\"userinfo\":(.*?\})\}'
        userinfo=re.findall(pattern,content)[0]
        info=json.loads(userinfo)
        homepage='http://weibo.com/u/%s/home%s'%(info['uniqueid'],info['userdomain'])
        #print(homepage)
        return Request(url=homepage,headers={
            "Accept-Encoding": "gzip"
            },callback=self.login_successful)
    def login_successful(self,response):
        print("login successful")
        current_url=response.url
        #print(current_url)
        #print(response.body)
        #open("test1.html", 'wb').write(response.body)
        #获取自己的关注用户,粉丝
        content=response.body
        s=content.decode('utf-8','ignore')
        pattern=r'/(\d+?)/'
        usernumber=re.findall(pattern,current_url)[0]
        watch_url=r'/%s/follow?rightmod=1&wvr=6'%(usernumber)
        fan_url=r'/%s/fans?rightmod=1&wvr=6'%(usernumber)
        watch_url=urljoin(current_url,watch_url)
        fan_url=urljoin(current_url,fan_url)
        yield Request(url=watch_url,callback=self.get_my_watches)
    def get_my_watches(self,response):
        print('getting my watches...')
        current_url=response.url
        content=response.body.decode('utf8')
        pattern=r'<li class=\\"member_li S_bg1\\"(.*?)<\\/li>'
        results=re.findall(pattern,content)
        for result in results:
            #匹配follow的用户的标签
            pattern1=r'<div class=\\"title.*?(<a.*?<\\/a>)'
            tag_a=re.findall(pattern1,result)[0]
            if 'usercard' in tag_a:
                raw_href=re.findall(r'href=\\"(.*?)\\"',tag_a)[0]#用户的链接
                title=re.findall(r'title=\\"(.*?)\\"',tag_a)[0]#用户名
                usercard=re.findall(r'usercard=\\"id=(.*?)\\"',tag_a)[0]#用户id
                href=raw_href.replace(r'\/','/')
                full_href=urljoin(current_url,href)
                #print(title)
                #print(full_href)
                yield Request(url=full_href,callback=self.parse_user)
                #print(usercard)
                #print('分割')
        pattern=r''
        result=re.findall(pattern,content)
        if result:
            raw_url=result[0]
            next_url=raw_url.replace(r'\/','/')
            full_next_url=urljoin(current_url,next_url)
            yield Request(url=full_next_url,callback=self.get_my_watches)
    def parse_user(self,response):
        print('parse user')
        current_url=response.url
        content=response.body.decode('utf8')
        pattern=r'<table class=\\"tb_counter\\".*?
.*<\\/tr>.*?<\\/table>'
        match_result=re.findall(pattern,content)
        if match_result:
            result=match_result[0]
            ntag_a=re.findall(r'',result)
            if ntag_a:
                for tag_a in ntag_a:
                    #print(tag_a)
                    if '微博' in tag_a:
                        #print(tag_a)
                        match_weibo_url=re.findall(r'href=\\"(.*?)\\"',tag_a)
                        if match_weibo_url:
                            url=match_weibo_url[0]
                            url=url.replace(r'\/','/')
                            full_url=urljoin(current_url,url)
                            yield Request(url=full_url,callback=self.get_all_weibos)
                    if '关注' in tag_a:
                        match_follow_url=re.findall(r'href=\\"(.*?)\\"',tag_a)
                        if match_follow_url:
                            url=match_follow_url[0]
                            url=url.replace(r'\/','/')
                            full_url=urljoin(current_url,url)
                    if '粉丝' in tag_a:
                        pass
    def get_all_weibos(self,response):
        pass


import torch.nn as nn
from torch.nn.utils import spectral_norm

class Generator(nn.Module):
    def __init__(self, opts):
        super(Generator, self).__init__()
        nz = opts["z_dim"]
        ngf = opts["nf"]
        nc = opts["nc"]
        self.main = nn.Sequential(
            # input is Z, going into a convolution
            nn.ConvTranspose2d(     nz, ngf * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.ReLU(True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 4),
            nn.ReLU(True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 2),
            nn.ReLU(True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(ngf * 2,     ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf),
            nn.ReLU(True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(    ngf,      nc, 4, 2, 1, bias=False),
            nn.Tanh()
            # state size. (nc) x 64 x 64
        )

    def forward(self, input):
        x = input[0]
        output = self.main(x)
        return output

class Discriminator(nn.Module):
    def __init__(self, opts):
        super(Discriminator, self).__init__()
        nc = opts["nc"]
        ndf = opts["nf"]
        self.main = nn.Sequential(
            # input is (nc) x 64 x 64
            nn.Conv2d(nc, ndf, 3, 1, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(ndf, ndf * 2, 3, 1, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 2, ndf * 2, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(ndf * 2, ndf * 4, 3, 1, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 4, ndf * 4, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(ndf * 4, ndf * 8, 3, 1, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 8, ndf * 8, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(ndf * 8, ndf * 8, 4, 1, 0, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x 1 x 1
            # nn.Sigmoid()
        )
        self.dense = nn.Linear(ndf * 8, 1)

    def forward(self, input):
        x = input[0]
        feat = self.main(x)
        bb = feat.size(0)
        logits = self.dense(feat.view(bb, -1))

        return logits.squeeze(1), feat