__author__ = 'Pavel Gladkov'
# -*- coding: utf-8 -*-

import logging
from utils.age_gender import *
from utils.utils import get_domains_count
import csv

if __name__ == "__main__":

    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        datefmt='%m-%d-%y %H:%M:%S')

    logger = logging.getLogger(__name__)

    train_file = open('age_gender/data/train.csv', 'w')
    test_file = open('age_gender/data/test.csv', 'w')
    data_file = open('age_gender/data/gender_age_dataset.txt', 'r')

    logger.info("get all domains")
    # домены-фичи, только хорошие
    all_domains = get_good_domains(domains_filename='/home/pgladkov/P/BigData/lab_4/age_gender/data/good_domains.txt',
                                   dataset_file=data_file)

    ages = ['18-24', '25-34', '35-44', '45-54', '>=55']
    genders = ['M', 'F']
    classes = []
    for a in ages:
        for g in genders:
            classes.append(a+g)

    train_writer = csv.DictWriter(train_file, ['cl'] + all_domains)
    train_writer.writeheader()

    test_writer = csv.DictWriter(test_file, ['uid'] + all_domains)
    test_writer.writeheader()

    logger.info("start split")

    def get_result_dict(_data):
        _d_count = get_domains_count(_data.get('visits', []))
        _total_visits = 0
        for _k, _v in _d_count.iteritems():
            _total_visits += _v
        _result_dict = {}
        for _domain in all_domains:
            _v = _d_count.get(_domain, 0)
            _result_dict[_domain] = _v

        return _result_dict

    # курсор в начало файла
    data_file = open('age_gender/data/gender_age_dataset.txt', 'r')
    for line in data_file:
        line = line.strip()
        fields = line.split('\t')
        try:
            gender = fields[0]
            age = fields[1]
            uid = fields[2]
            data = json.loads(fields[3])
        except (IndexError, ValueError):
            continue

        if gender == '-':
            result_dict = get_result_dict(data)
            result_dict['uid'] = uid
            test_writer.writerow(result_dict)
        else:
            result_dict = get_result_dict(data)
            result_dict['cl'] = age + gender
            train_writer.writerow(result_dict)

    test_file.close()
    train_file.close()


import statistics
from grasp_iterated_greedy import GRASPIteratedGreedyCRVP
from grasp_local_search import GRASPLocalSearch
from iterated_greedy import IteratedGreedyCRVP
from iterated_local_search import IteratedLocalSearch

from semi_greedy import SemiGreedyCRVP
from simple_local_search import SimpleLocalSearch

#from simple_local_search import SimpleLocalSearch

# from iterated_greedy import IteratedGreedyCRVP

instances = [
    "instances/A/A-n32-k5.vrp",
    "instances/A/A-n33-k6.vrp",
    "instances/A/A-n39-k5.vrp",
    "instances/A/A-n44-k6.vrp",
    "instances/A/A-n48-k7.vrp",
    "instances/A/A-n54-k7.vrp",
    "instances/A/A-n62-k8.vrp",
    "instances/A/A-n65-k9.vrp",
    "instances/A/A-n80-k10.vrp"
]

'''
semy_greedy_results = []
for instance in instances:
    semi_greedy = SemiGreedyCRVP(file_path=instance,k_percentage=15)
    results = semi_greedy.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"])

    semy_greedy_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(semy_greedy_results) / len(semy_greedy_results))
'''

'''
iterated_greedy_results = []
for instance in instances:
    iterated_greedy = IteratedGreedyCRVP(file_path=instance, max_iterations=5000, destruction_percentage=70, k_percentage=15)
    results = iterated_greedy.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"]," - ",results["routes"])

    iterated_greedy_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(iterated_greedy_results) / len(iterated_greedy_results))
'''

'''
simple_local_search_results = []
for instance in instances:
    simple_local_search = SimpleLocalSearch(file_path=instance,max_iterations=100)
    results = simple_local_search.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"])

    simple_local_search_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(simple_local_search_results) / len(simple_local_search_results))
'''

'''
iterated_local_search_results = []
for instance in instances:
    iterated_local_search = IteratedLocalSearch(file_path=instance,max_iterations=50, n_distortion=20)
    results = iterated_local_search.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"])

    iterated_local_search_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(iterated_local_search_results) / len(iterated_local_search_results))
'''
'''
grasp_simple_local_search_results = []
for instance in instances:
    grasp_simple_local_search = GRASPLocalSearch(file_path=instance,max_iterations=100,k_percentage=15, internal_iterations=100)
    results = grasp_simple_local_search.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"])

    grasp_simple_local_search_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(grasp_simple_local_search_results) / len(grasp_simple_local_search_results))
'''

grasp_iterated_local_search_results = []
for instance in instances:
    grasp_simple_local_search = GRASPIteratedGreedyCRVP(file_path=instance,max_iterations=500, destruction_percentage=70, k_percentage=15,internal_iterations=50)
    results = grasp_simple_local_search.run()

    print(instance," :",results["optimal_cost"]," - ",results["solution_cost"]," - ",results["optimal_cost"]/results["solution_cost"])

    grasp_iterated_local_search_results.append(results["optimal_cost"]/results["solution_cost"])

print(sum(grasp_iterated_local_search_results) / len(grasp_iterated_local_search_results))

#iterated_greedy = IteratedGreedyCRVP(file_path="instances\A\A-n32-k5.vrp")
#print(iterated_greedy.run(max_iterations=5000, destruction_percentage=20))

#simple_local_search = SimpleLocalSearch('instances/A/A-n80-k10.vrp')
#print(simple_local_search.run(5000))


# Face Detection
import cv2

# ອ່ານວິດີໂອ
cap = cv2.VideoCapture('images/Mark.mp4')

# ອ່ານ File xml ສຳຫຼັບ Classification
face_cascade = cv2.CascadeClassifier('detect/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('detect/haarcascade_eye_tree_eyeglasses.xml')

# ສະແດງຜົນວິດີໂອ
while (cap.isOpened()):
    check, frame = cap.read()
    if check == True:
        gray_vdo = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # ຈຳແນກດວງຕາ
        scaleFactory = 1.2
        minNeighber = 5
        # ກວດຈັບໃບໜ້າ
        face_detect = face_cascade.detectMultiScale(gray_vdo, scaleFactory, minNeighber)
        # ກວດຈັບດວງຕາ
        eye_detect = eye_cascade.detectMultiScale(gray_vdo, scaleFactory, minNeighber)
        # ສະແດງຕຳແໜ່ງທີ່ພົບເຫັນດວງຕາ
        for (ex, ey, ew, eh) in face_detect:
            cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), thickness=5)
            for (fx, fy, fw, fh) in eye_detect:
                cv2.rectangle(frame, (fx, fy), (fx+fw, fy+fh), (0, 0, 255), thickness=5)

        cv2.imshow('Output', frame)
        if cv2.waitKey(5) & 0xFF == ord('e'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()


'''
Created on 29 Aug 2017

@author: Michael
'''
from datetime import datetime
import time
import SQLiteAdapter as db
import Topics

class Message(object):
    '''
    classdocs
    '''

    def __init__(self,id=None,username=None,message=None,reply_to=None,topic=None,
                 modified=0,likes=0,dislikes=0,timestamp=datetime.now()):
        '''
        Constructor
        '''
        self.id = id
        self.username = username
        self.message = message
        self.reply_to = reply_to
        self.topic = topic
        self.modified = modified
        self.likes = likes
        self.dislikes = dislikes
        self.timestamp = timestamp

    def save(self):
        '''
            Adds this object to the database if no id exists, otherwise it updates.
            This must be called after any direct modification to this object in order to
            make it permanent.
        '''
        if self.id==None:
            #Add
            res = db.QUERY("INSERT INTO messages (username,message,reply_to,topic,modified,likes,dislikes) VALUES (?,?,?,?,?,?,?)",
                           (self.username,self.message,self.reply_to,self.topic,self.modified,self.likes,self.dislikes))
            if res:
                self.id=res
                return True
        else:
            #Update
            res = db.QUERY("UPDATE messages SET username=?,message=?,reply_to=?,topic=?,modified=?,likes=?,dislikes=? WHERE id=?",
                           (self.username,self.message,self.reply_to,self.topic,self.modified,self.likes,self.dislikes,self.id))

        if res:
            return True
        else:
            return False

    def getMessages(self):
        return getFiltered(filters={'reply_to':self.id})

    def getTopic(self):
        if self.topic:
            return Topics.get(self.topic)
        return None

    def getParentMessage(self):
        if self.reply_to:
            return get(self.reply_to)
        return None

def getCount(filters={}):
    ''' Number of messages in the database given a number of filters'''
    keys = filters.keys()
    values = filters.values()
    query = 'SELECT COUNT(*) AS count FROM messages '
    if len(keys)>0:
        query = query + 'WHERE '
        for key in keys:
            query = query + key + '=?, '
        query = query[:-2]
    res = db.QUERY(query,tuple(values))
    if res:
        return res[0]['count']
    return 0

def get(id=None):
    ''' Gets a Message given it's id
    '''
    if id==None:
        return None

    res = db.QUERY('SELECT * FROM messages WHERE id=?',(id,))
    if res and len(res)>0:
        return Message(**res[0])
    else:
        return None

def getAll(limit=-1):
    ''' Get a list of all Messages '''
    if limit>0:
        res = db.QUERY('SELECT * FROM messages LIMIT ?',(limit,))
    else:
        res = db.QUERY('SELECT * FROM messages')
    rlist = []
    if res:
        for values in res:
            rlist.append(Message(**values))
        return rlist
    return None

def getFiltered(filters={}, limit=-1,order_by=None,ascending=True):
    ''' Get a list of filtered Messages '''

    keys = filters.keys()
    query = 'SELECT * FROM messages '
    if len(keys)>0:
        query = query + 'WHERE '
        for key in keys:
            if filters[key] is None:
                #Need to do something slightly special as SQLite is weird will NULLs
                query = query + key + ' IS NULL and '
                del filters[key]
            else:
                query = query + key + '=? and '
        query = query[:-4]
    values = filters.values()
    if order_by:
        order_by = order_by if order_by.lower() in ['id','username','message',
                                                    'reply_to','topic','modified',
                                                    'likes','dislikes',
                                                    'timestamp'] else 'id'
        query = query + 'ORDER BY ' + order_by
        if ascending:
            query = query + " ASC "
        else:
            query = query + " DESC "
        values.append(order_by)
    if limit>0:
        values.append(limit)
        res = db.QUERY(query + ' LIMIT ?',tuple(values))
    else:
        res = db.QUERY(query,tuple(values))
    if res:
        rlist = []
        for values in res:
            rlist.append(Message(**values))
        return rlist
    return None

def delete(id=None):
    ''' Deletes a Message given it's id
    '''
    if id==None:
        return None
    res = db.QUERY('DELETE FROM messages WHERE id=?',(id,))
    if res>0:
        return True
    else:
        return False


#!/usr/bin/python3
""" a script to add Louisiana to the db
"""
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import sys

if __name__ == "__main__":
    connection = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'
                           .format(sys.argv[1], sys.argv[2], sys.argv[3]))
    Base.metadata.create_all(connection)
    Session = sessionmaker(bind=connection)
    session = Session()
    new_state = State(name='Louisiana')
    session.add(new_state)
    new_instance = session.query(State).filter_by(name='Louisiana').first()
    print(new_instance.id)
    session.commit()


import urllib.request
import json
import logging as lg

"""
Used to make the (undocumented) api call to finance.yahoo.com
to retrieve the current option prices for the specified stock
trading symbol.

Returns the object containing the option prices for the day for
that security with the specified option expiration date and all strike prices.
"""

def retrieve_option_expiration_dates(symbol):
    try:
        urlData = "https://query2.finance.yahoo.com/v7/finance/options/" + symbol
        webURL = urllib.request.urlopen(urlData)
        data = webURL.read()
        encoding = webURL.info().get_content_charset('utf-8')
        jsonObject = json.loads(data.decode(encoding))
        return jsonObject
    except Exception as err:
        lg.error('Error retrieving expiration dates for symbol ' + symbol)

def retrieve_option_prices_by_expiration_date(symbol, expDate):
    try:
        urlData = "https://query2.finance.yahoo.com/v7/finance/options/" + symbol + "?date=" + str(expDate)
        webURL = urllib.request.urlopen(urlData)
        data = webURL.read()
        encoding = webURL.info().get_content_charset('utf-8')
        jsonObject = json.loads(data.decode(encoding))
        return jsonObject
    except Exception as err:
        lg.error('Error retrieving expiration dates for symbol ' + symbol)


from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dictionary={}
        for i,a in enumerate(nums):
           if target-a in dictionary:
               return [dictionary[target-a],i]
           else:
               dictionary[a]=i

        return None

nums=[2, 7, 11, 15]
target= 9
print(Solution().twoSum(nums,target))

哈希表/1. 两数之和.py


import requests
import falcon
import json

from .json_validate import DefaultValidatingDraft4Validator

class UtilBase():

    _error_msg = {
        'authentication_info_required': 'AUTHENTICATION_INFO_REQUIRED',
        'authentication_info_illegal': 'AUTHENTICATION_INFO_ILLEGAL',
        'attempt_too_many_times': 'ATTEMPT_TOO_MANY_TIMES',
        'balance_insufficient': 'BALANCE_INSUFFICIENT',
        'conflict_info_exist': 'CONFLICT_INFO_EXIST',
        'conflict_mobile_exist': 'CONFLICT_MOBILE_EXIST',
        'forbidden': 'FORBIDDEN',
        'img_code_timeout': 'IMG_CODE_TIMEOUT',
        'img_code_verification_failed': 'IMG_CODE_VERIFICATION_FAILED',
        'invalid_body_content': 'INVALID_BODY_CONTENT',
        'invalid_mobile_number': 'INVALID_MOBILE_NUMBER',
        'invalid_query_params': 'INVALID_QUERY_PARAMS',
        'need_credential': 'NEED_CREDENTIAL',
        'not_found': 'NOT_FOUND',
        'no_mobile_info': 'NO_MOBILE_INFO',
        'password_verification_failed': 'PASSWORD_VERIFICATION_FAILED',
        'sms_can_not_send_in_60': 'SMS_CAN_NOT_SEND_IN_60',
        'sms_max_limit_per_day': 'SMS_MAX_LIMIT_PER_DAY',
        'sms_timeout': 'SMS_TIMEOUT',
        'sms_verification_failed': 'SMS_VERIFICATION_FAILED',
        'token_timeout': 'TOKEN_TIMEOUT',
        'token_verification_failed': 'TOKEN_VERIFICATION_FAILED',
        'user_not_exist': 'USER_NOT_EXIST',
        'user_exist_more_information_needed': 'USER_EXIST_MORE_INFORMATION_NEEDED',
        'unsupported_operation': 'UNSUPPORTED_OPERATION',
        'unsupported_transaction_category': 'UNSUPPORTED_TRANSACTION_CATEGORY',
    }

    _headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    _methods = {
        "GET": requests.get,
        "POST": requests.post,
        "PUT": requests.put,
        "DELETE": requests.delete
    }

    @property
    def ERROR_MSG(self):
        return UtilBase._error_msg

    @property
    def HEADERS(self):
        return UtilBase._headers

    @property
    def METHODS(self):
        return UtilBase._methods

    @staticmethod
    def result_error(msg, detail=None):
        msg = {'error': msg}
        if detail:
            msg['detail'] = detail

        return falcon.HTTP_400, json.dumps(msg)

    @staticmethod
    def transform_status_code(status_code):

        if status_code == 200:
            return falcon.HTTP_200

        if status_code == 201:
            return falcon.HTTP_201

        if status_code == 202:
            return falcon.HTTP_202

        if status_code == 403:
            return falcon.HTTP_403

        if status_code == 404:
            return falcon.HTTP_404

        if status_code == 409:
            return falcon.HTTP_409

        if status_code == 415:
            return falcon.HTTP_415

        if status_code == 500:
            return falcon.HTTP_500

    @staticmethod
    def transform_to_int(param_dict, if_raise_value_error=False):
        if not isinstance(param_dict, dict):
            return False, "<%s> is not <dict> type" % type(param_dict)

        for key, value in param_dict.items():
            try:
                param_dict[key] = int(value)
            except ValueError:
                if if_raise_value_error:
                    return False, "'%s' type <%s> can not be converted to <int>" % (value, type(value))
                else:
                    pass
        return True, None

    def request(self, method, url, **params):
        response = self.METHODS[method](url, headers=self.HEADERS, **params)
        return response

    @staticmethod
    def parse_pagination_params(params_dict):
        condition = dict(params_dict)
        pageNum = condition.get('pageNum')
        pageSize = condition.get('pageSize')

        if pageNum:
            del condition['pageNum']
        else:
            pageNum = 0

        if pageSize:
            del condition['pageSize']
        else:
            pageSize = 10

        return condition, pageNum, pageSize

    @staticmethod
    def validate_dict_with_schema(data, schema):
        try:
            DefaultValidatingDraft4Validator(schema).validate(data)
            return 200, None
        except Exception as ex:
            return 415, "parse body exception: %s" % ex

    @classmethod
    def get_params_from_body(cls, body_stream, schema=None, return_dict=False):
        data = {}
        body = body_stream.read()

        try:
            data = json.loads(body)
        except:
            return 415, "parse json failed"

        if schema:
            code, tag = cls.validate_dict_with_schema(data, schema)
            if code != 200:
                return code, tag

        return 200, data if return_dict else json.dumps(data)


from datetime import datetime, timezone
from typing import List

from pyd_sqa import pyd_sqa

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import Session, relationship, sessionmaker

from pydantic.config import ConfigDict

Base = declarative_base()

engine = create_engine("sqlite://", echo=True)

def utc_now() -> datetime:
    return datetime.now(tz=timezone.utc)

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    nickname = Column(String)
    created = Column(DateTime, default=datetime.utcnow)
    updated = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now)

    addresses = relationship(
        "Address", back_populates="user", cascade="all, delete, delete-orphan"
    )

class Address(Base):
    __tablename__ = "addresses"
    id = Column(Integer, primary_key=True)
    email_address = Column(String, nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"))

    user = relationship("User", back_populates="addresses")

Base.metadata.create_all(engine)

LocalSession = sessionmaker(bind=engine)

db: Session = LocalSession()

ed_user = User(name="ed", fullname="Ed Jones", nickname="edsnickname")

address = Address(email_address="ed@example.com")
address2 = Address(email_address="eddy@example.com")
ed_user.addresses = [address, address2]
db.add(ed_user)
db.commit()

def test_defaults() -> None:
    PydanticUser = pyd_sqa(User)
    PydanticAddress = pyd_sqa(Address)

    class PydanticUserWithAddresses(PydanticUser):
        addresses: List[PydanticAddress] = []

    user = db.query(User).first()
    pydantic_user = PydanticUser.model_validate(user)
    data = pydantic_user.model_dump()
    assert isinstance(data["created"], datetime)
    assert isinstance(data["updated"], datetime)
    check_data = data.copy()
    del check_data["created"]
    del check_data["updated"]
    assert check_data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
    }
    pydantic_user_with_addresses = PydanticUserWithAddresses.model_validate(user)
    data = pydantic_user_with_addresses.model_dump()
    assert isinstance(data["updated"], datetime)
    assert isinstance(data["created"], datetime)
    check_data = data.copy()
    del check_data["updated"]
    del check_data["created"]
    assert check_data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
        "addresses": [
            {"email_address": "ed@example.com", "id": 1, "user_id": 1},
            {"email_address": "eddy@example.com", "id": 2, "user_id": 1},
        ],
    }

def test_schema() -> None:
    PydanticUser = pyd_sqa(User)
    PydanticAddress = pyd_sqa(Address)
    assert PydanticUser.model_json_schema() == {
        "title": "User",
        "type": "object",
        "properties": {
            "id": {
                "title": "Id",
                "type": "integer",
            },
            "name": {"title": "Name", "type": "string", "default": None},
            "fullname": {"title": "Fullname", "type": "string", "default": None},
            "nickname": {"title": "Nickname", "type": "string", "default": None},
            "created": {
                "title": "Created",
                "type": "string",
                "format": "date-time",
                "default": None,
            },
            "updated": {
                "title": "Updated",
                "type": "string",
                "format": "date-time",
                "default": None,
            },
        },
        "required": ["id"],
    }
    assert PydanticAddress.model_json_schema() == {
        "title": "Address",
        "type": "object",
        "properties": {
            "id": {"title": "Id", "type": "integer"},
            "email_address": {"title": "Email Address", "type": "string"},
            "user_id": {"title": "User Id", "type": "integer", "default": None},
        },
        "required": ["id", "email_address"],
    }

def test_config() -> None:
    def alias_generator(string: str) -> str:
        pascal_case = "".join(word.capitalize() for word in string.split("_"))
        camel_case = pascal_case[0].lower() + pascal_case[1:]
        return camel_case

    config = ConfigDict(
        populate_by_name=True, from_attributes=True, alias_generator=alias_generator
    )

    PydanticUser = pyd_sqa(User, config=config)
    PydanticAddress = pyd_sqa(Address, config=config)

    class PydanticUserWithAddresses(PydanticUser):
        addresses: List[PydanticAddress] = []

    user = db.query(User).first()
    pydantic_user_with_addresses = PydanticUserWithAddresses.model_validate(user)
    data = pydantic_user_with_addresses.model_dump(by_alias=True)
    assert isinstance(data["created"], datetime)
    assert isinstance(data["updated"], datetime)
    check_data = data.copy()
    del check_data["created"]
    del check_data["updated"]
    assert check_data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "nickname": "edsnickname",
        "addresses": [
            {"emailAddress": "ed@example.com", "id": 1, "userId": 1},
            {"emailAddress": "eddy@example.com", "id": 2, "userId": 1},
        ],
    }

def test_exclude() -> None:
    PydanticUser = pyd_sqa(User, exclude={"nickname"})
    PydanticAddress = pyd_sqa(Address, exclude={"user_id"})

    class PydanticUserWithAddresses(PydanticUser):
        addresses: List[PydanticAddress] = []

    user = db.query(User).first()
    pydantic_user_with_addresses = PydanticUserWithAddresses.model_validate(user)
    data = pydantic_user_with_addresses.model_dump(by_alias=True)
    assert isinstance(data["created"], datetime)
    assert isinstance(data["updated"], datetime)
    check_data = data.copy()
    del check_data["created"]
    del check_data["updated"]
    assert check_data == {
        "fullname": "Ed Jones",
        "id": 1,
        "name": "ed",
        "addresses": [
            {"email_address": "ed@example.com", "id": 1},
            {"email_address": "eddy@example.com", "id": 2},
        ],
    }


# dana.sara.main.py
from _spy.vitollino.main import Cena, Elemento, INVENTARIO as inv
from _spy.vitollino.main import STYLE
STYLE ["width"] = 1000
#STYLE ["min-hight"] = 600

floresta = "https://vignette.wikia.nocookie.net/reinoanimalia/images/c/c3/Bosques.png/revision/latest?cb=20150820071051&path-prefix=es"
coqueiro = "https://2.bp.blogspot.com/-QutwWEONdV0/WSA8KeAeToI/AAAAAAAAJJM/2vz0mWLvEZY-ZPc_EsWi2Nl9O39WSvUkQCLcB/s1600/coqueiro-an%25C3%25A3o-produzindo.jpg"
orvalho = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ-_3_HMMVXDWcMI9dIi_BI6i3F5jV-_983RseAwJ-DpEO2RoSP"
seiva = "http://cdn.ciclovivo.com.br/wp-content/uploads/2015/009/img/noticias/1238633982_9f95150143%20(1).jpg"
cacto = "https://www.guiamuriae.com.br/wp-content/uploads/2016/01/Agua-de-cacto-Foto-Pixabay.jpg"
corda = "https://4.bp.blogspot.com/-VuqgmerQZ_8/VeX2DWsMdoI/AAAAAAAAeZU/oak3CEhkAcs/s1600/N%25C3%25A1utico%2B-%2Bcorda.png"
faca = "https://vignette.wikia.nocookie.net/left4dead/images/0/0c/Machete.png/revision/latest?cb=20101021031134"
pote = "http://rodrigorepresentacoes.com.br/imagens/produtos/902/Pote%20PP-10001.png"
pano = "http://www.stickpng.com/assets/images/5856ad664f6ae202fedf277c.png"
colher = "https://upload.wikimedia.org/wikipedia/commons/3/32/Colher.png"

class Agua():
     def __init__ (self):
      inv.inicia()

      self.floresta= Cena(img=floresta)
      self.florestaOrvalho= Cena(img=floresta)
      self.florestaCoqueiro= Cena(img=floresta)
      self.florestaSeiva= Cena(img=floresta)
      self.florestaCacto= Cena(img=floresta)

      self.floresta.direita = self.florestaCoqueiro
      self.florestaCoqueiro.esquerda = self.floresta

      self.floresta.esquerda = self.florestaOrvalho
      self.florestaOrvalho.direita = self.floresta

      self.florestaCoqueiro.direita = self.florestaCacto
      self.florestaCacto.esquerda = self.florestaCoqueiro

      self.florestaOrvalho.esquerda = self.florestaSeiva
      self.florestaSeiva.direita = self.florestaOrvalho

      self.florestaSeiva.esquerda = self.florestaCacto
      self.florestaCacto.direita = self.florestaSeiva

      self.minicoqueiro= Elemento(img=coqueiro, style=dict (left=150, top=300, height=300,width=200,bottom=200))
      self.miniorvalho= Elemento (img=orvalho, style=dict (left=200, top= 500, height=300, width=200, bottom=200))
      self.miniseiva= Elemento (img=seiva, style=dict (left=500, top= 300, height=50, width=200, bottom=200))
      self.minicacto= Elemento (img=cacto, style=dict (left=300, top= 500, height=200, width=200, bottom=200))

      self.floresta.meio = self.floresta
      self.floresta.vai

      self.minicoqueiro.entra (self.florestaCoqueiro)
      self.minicacto.entra (self.florestaCacto)
      self.miniseiva.entra (self.florestaSeiva)
      self.miniorvalho.entra (self.florestaOrvalho)

      #criar a funÃÂÃÂ§ÃÂÃÂ£o de abrir elemento ao clicar em elemento
      self.cenaCoqueiro = Cena (img=coqueiro)
      self.minicoqueiro.vai = self.cenaCoqueiro.vai
      self.cenaCoqueiro.esquerda = self.florestaCoqueiro
      self.cenaCoqueiro.direita = self.florestaCoqueiro

      self.cenaCacto = Cena (img= cacto)
      self.minicacto.vai = self.cenaCacto.vai
      self.cenaCacto.esquerda = self.florestaCacto
      self.cenaCacto.direita = self.florestaCacto

      self.cenaOrvalho = Cena (img= orvalho)
      self.miniorvalho.vai = self.cenaOrvalho.vai
      self.cenaOrvalho.esquerda = self.florestaOrvalho
      self.cenaOrvalho.direita = self.florestaOrvalho

      self.cenaSeiva = Cena (img= seiva)
      self.miniseiva.vai = self.cenaSeiva.vai
      self.cenaSeiva.esquerda = self.florestaSeiva
      self.cenaSeiva.direita = self.florestaSeiva

      self.corda = Elemento (img=corda, tit="corda", style=dict (left=150, top=300, height=300,width=200,bottom=200))
      inv.bota (self.corda)
      self.faca = Elemento (img=faca, tit="faca", style=dict (left=150, top=300, height=300,width=200,bottom=200))
      inv.bota (self.faca)
      self.pote = Elemento (img=pote, tit="pote", style=dict (left=150, top=300, height=300,width=200,bottom=200))
      inv.bota (self.pote)
      self.pano = Elemento (img=pano, tit="pano", style=dict (left=150, top=300, height=300,width=200,bottom=200))
      inv.bota (self.pano)
      self.colher = Elemento (img=colher, tit="colher", style=dict (left=150, top=300, height=300,width=200,bottom=200))
      inv.bota (self.colher)

      self.floresta.vai ()
if __name__ == "__main__":
     Agua()


# -*- coding: utf-8 -*-
"""
Created on Thu May  2 18:16:06 2019

@author: preet
"""

import numpy as np
import matplotlib.pyplot as plt

class Polynomial:

    def __init__(self, *coefficients):
        self.coefficients = coefficients[::-1]

    def __repr__(self):
        return "Polynomial" + str(self.coefficients[::-1])

    def __call__(self, x):
        res = 0
        for index, coeff in enumerate(self.coefficients):
            res += coeff * x ** index
        return res

p = Polynomial(4, 0, -4, 3, 0)
for x in range(-3, 3):
    print(x, p(x))

X = np.linspace(-3, 3, 50, endpoint=True)
F = p(X)
plt.plot(X, F)
plt.show()


""" <일일 온도> https://leetcode.com/problems/daily-temperatures/

[input]
temperatures = [73,74,75,71,69,72,76,73]

[output]
[1,1,4,2,1,1,0,0]

"""

""" solution-1 스택 값 비교 """

class Solution(object):
    def dailyTemperatures(self, temperatures):
        answer = [0] * len(temperatures)
        stack = []
        for i, temp in enumerate(temperatures):
            # 현재 온도가 스택 값보다 높다면 정답 처리
            while stack and temp > temperatures[stack[-1]]:
                last = stack.pop()
                answer[last] = i - last
            stack.append(i)

        return answer

a = Solution()
print(a.dailyTemperatures([73,74,75,71,69,72,76,73]))


import gzip
import tempfile

from os.path import dirname, join
from collections import Counter
from itertools import chain

from corpustools import add_most_frequent
from corpustools import ContainsEverything
from corpustools import ENGLISH
from corpustools import extract_units, extract_fields
from corpustools import filter_tagged_vocabulary, filter_tagged_event_file
from corpustools import merge_tokens_tags_corpus
from corpustools import ngrams
from corpustools import replace_disallowed
from corpustools import split_collection

top = join(dirname(__file__), "data")

DUMMY_CORPUS = join(top, "dummy_corpus.txt")
DUMMY_MERGED = join(top, "dummy_corpus_merged.txt")
DUMMY_EVENTS = join(top, "dummy_corpus_merged_events.gz")
DUMMY_EVENTS_FILTERED = join(top, "dummy_corpus_merged_events_"
                                  "filtered.gz")
DUMMY_EVENTS_FILLED = join(top, "dummy_corpus_merged_events_"
                                "filtered_fill_cues.gz")

DUMMY_SPECS = {"tag_field": 2,
               "delimiter": "\t",
               "num_fields": 3}

def test_extract_tokens():
    with open(DUMMY_CORPUS) as corpus:
        tokens = extract_fields(corpus,
                                keep_meta={},
                                **DUMMY_SPECS)
        tokens = list(tokens)
        assert len(tokens) == 38

def test_drop_meta_false():
    with open(DUMMY_CORPUS) as corpus:
        tokens = extract_fields(corpus,
                                drop_meta=False,
                                **DUMMY_SPECS)
        tokens = list(tokens)
        assert len(tokens) == 50

def test_keep_everything():
    with open(DUMMY_CORPUS) as corpus:
        tokens = extract_fields(corpus,
                                drop_meta=False,
                                drop_tags=False,
                                **DUMMY_SPECS)
        tokens = list(tokens)
    assert len(tokens) == 56

def test_extract_multiple_fields_and_meta():
    with open(DUMMY_CORPUS) as corpus:
        tokens = extract_fields(corpus,
                                return_fields=[0, 2],
                                drop_meta=False,
                                drop_tags=False,
                                **DUMMY_SPECS)
        tokens = list(tokens)
    assert len(tokens) == 56
    assert sum([isinstance(token, str) for token in tokens]) == 12

def test_extract_tags_tokens():
    with open(DUMMY_CORPUS) as corpus:
        tags_tokens = extract_fields(corpus,
                                     keep_meta={},
                                     return_fields={1, 2},
                                     **DUMMY_SPECS)
        tags_tokens = list(tags_tokens)
        assert len(tags_tokens) == 38
        assert all([len(tt) == 2 for tt in tags_tokens])

def test_extract_sentences():
    with open(DUMMY_CORPUS) as corpus:
        sentences = extract_units(corpus,
                                  **DUMMY_SPECS)
        sentences = list(sentences)
        assert len(sentences) == 3

def test_extract_documents():
    with open(DUMMY_CORPUS) as corpus:
        documents = extract_units(corpus,
                                  boundary="</doc>",
                                  **DUMMY_SPECS)
        documents = list(documents)
        assert len(documents) == 2

def test_split_documents_into_sentences():
    with open(DUMMY_CORPUS) as corpus:
        documents = extract_units(corpus,
                                  boundary="</doc>",
                                  keep_meta={"</doc>", "</s>"},
                                  **DUMMY_SPECS)
        documents = list(documents)
        sentences = [sent for document in documents
                     for sent in split_collection(document, "</s>")]
        assert len(sentences) == 3

def test_split_collection_last_split_missing():
    numbers = [1, 2, 3, 1, 5, 1, 3]
    subsequences = split_collection(numbers, 1)
    subsequences = list(subsequences)
    assert len(subsequences) == 3
    assert subsequences[0] == [2, 3]
    assert subsequences[1] == [5]
    assert subsequences[2] == [3]

def test_merge_tokens_tags_corpus():
    with tempfile.NamedTemporaryFile() as tmp:
        merge_tokens_tags_corpus(DUMMY_CORPUS, tmp.name,
                                 symbols=ENGLISH,
                                 overwrite=True,
                                 replacement="repl",
                                 **DUMMY_SPECS)
        with open(tmp.name) as test, open(DUMMY_MERGED) as standard:
            for line in test:
                assert line == standard.readline()

def test_replace_disallowed_tokens():
    sequence = ["the", "last", "token", "contains",
                "a", "disallowed", "character", "test-word"]
    replaced = replace_disallowed(sequence, ENGLISH, "repl")
    assert sequence[:-1] == replaced[:-1]
    assert replaced[-1] == "repl"

def test_replace_disallowed_fields():
    sequence = [["the", "dt"], ["next", "adj"], ["token", "nn"],
                ["is", "vb"], ["disallowed", "adj"], ["test-word", "nn"],
                ["as", "cc"], ["is", "vb"], ["the", "dt"], ["next", "adj"],
                ["item", "nn"], ["!", "$"]]
    replaced = replace_disallowed(sequence, ENGLISH, "repl")
    assert sequence[:5] + sequence[6:11] == replaced[:5] + replaced[6:11]
    assert ["repl", sequence[5][1]] == replaced[5]
    assert ["repl", "repl"] == replaced[11]

def test_filter_tagged_vocabulary():
    tagged_vocabulary = {"test|nn", "test|vb", "the|dt",
                         "is|vb", "this|dt"}
    vocabulary = {"test", "this"}
    target = {"test|nn", "test|vb", "this|dt"}
    filtered = filter_tagged_vocabulary(tagged_vocabulary, vocabulary, "|")
    assert filtered == target

def test_add_most_frequent():
    targets = {"apple", "banana", "orange", "dragonfruit"}
    vocab = Counter({"apple": 10, "banana": 20, "dragonfruit": 30,
                     "mango": 40, "kiwi": 50, "pear": 60})
    added = add_most_frequent(targets, vocab, 6)
    expected = {"apple", "banana", "orange", "dragonfruit",
                "kiwi", "pear"}
    assert added == expected

def test_add_most_frequent_filter_targets():
    targets = {"apple", "banana", "orange", "dragonfruit"}
    vocab = Counter({"apple": 10, "banana": 20, "dragonfruit": 30,
                     "mango": 40, "kiwi": 50, "pear": 60})
    added = add_most_frequent(targets, vocab, 6, filter_targets=True)
    expected = {"apple", "banana", "dragonfruit",
                "mango", "kiwi", "pear"}
    assert added == expected

def test_add_most_frequent_most_frequent_in_targets():
    targets = {"banana", "orange", "dragonfruit", "pear"}
    vocab = Counter({"apple": 10, "banana": 20, "dragonfruit": 30,
                     "mango": 40, "kiwi": 50, "pear": 60})
    added = add_most_frequent(targets, vocab, 6, filter_targets=False)
    expected = {"banana", "orange", "dragonfruit", "pear",
                "mango", "kiwi"}
    assert added == expected

def test_contains_everything():
    container = ContainsEverything()
    assert "test" in container

def test_filter_tagged_event_file():
    cues = {"code", "functions", "sentence", "symbol"}
    outcomes = {"a", "the"}
    with tempfile.NamedTemporaryFile() as tmp:
        filter_tagged_event_file(DUMMY_EVENTS,
                                 tmp.name,
                                 cues=cues,
                                 outcomes=outcomes,
                                 overwrite=True)
        with gzip.open(DUMMY_EVENTS_FILTERED, "rt") as target, \
                gzip.open(tmp.name, "rt") as test:
            for line in target:
                cues, *outcome = line.strip().split("\t")
                cues = cues.split("_")
                test_line = test.readline()
                test_cues, *test_outcome = test_line.strip().split("\t")
                test_cues = test_cues.split("_")
                assert set(test_cues) == set(cues)
                assert test_outcome == outcome

def test_filter_tagged_event_file_fill_cues():
    cues = {"code", "functions", "sentence", "symbol"}
    outcomes = {"a", "the"}
    with tempfile.NamedTemporaryFile() as tmp:
        filter_tagged_event_file(DUMMY_EVENTS,
                                 tmp.name,
                                 cues=cues,
                                 outcomes=outcomes,
                                 fill_cues=5,
                                 overwrite=True)
        with gzip.open(DUMMY_EVENTS_FILLED, "rt") as target, \
                gzip.open(tmp.name, "rt") as test:
            for line in target:
                cues, *outcome = line.strip().split("\t")
                cues = cues.split("_")
                test_line = test.readline()
                test_cues, *test_outcome = test_line.strip().split("\t")
                test_cues = test_cues.split("_")
                assert set(test_cues) == set(cues)
                assert test_outcome == outcome

def test_ngrams_string():
    word = "banana"
    trigrams = ["ban", "ana", "nan", "ana"]
    assert list(ngrams(word, 3, as_string=False)) == trigrams

def test_ngrams_list():
    sentence = ["this", "is", "a", "test"]
    bigrams = [["this", "is"],
               ["is", "a"],
               ["a", "test"]]
    assert list(ngrams(sentence, 2, as_string=False)) == bigrams

def test_ngrams_multiple_n():
    sentence = ["this", "is", "a", "test"]
    bigrams = [["this", "is"],
               ["is", "a"],
               ["a", "test"]]
    trigrams = [["this", "is", "a"],
                ["is", "a", "test"]]
    grams = (" ".join(gram) for gram in chain(bigrams, trigrams))
    grams = chain(sentence, grams)
    assert set(grams) == set(ngrams(sentence, [1, 2, 3], as_string=True))


import numpy as np
import cv2

img = cv2.imread("jp.png")

print("{}\n{}\n{}".format(img.shape, img.size, img.dtype))

b, g, r = cv2.split(img)
img = cv2.merge((b, g, r))

# something = img[180:240, 230:290]
# img[262:322, 100:160] = something

# in order to add images they need to have the same side
dst = cv2.resize(img, (512, 512))

cv2.imshow('test', img)

cv2.waitKey()


import requests
from bs4 import BeautifulSoup
from csv import writer
import re
from datetime import date
import gspread

today = date.today().strftime('%m/%d/%Y')

codyurl = 'https://cod.tracker.gg/warzone/profile/xbl/Alazyturtle7/overview'
nickurl = 'https://cod.tracker.gg/warzone/profile/xbl/shippedmypants1/overview'
justinurl = 'https://cod.tracker.gg/warzone/profile/xbl/sockz%20n%20sandalz/overview'
bondourl = 'https://cod.tracker.gg/warzone/profile/xbl/BoNDo%20Is%20KLuTCH/overview'
loganurl = 'https://cod.tracker.gg/warzone/profile/xbl/CRAZY%20x2BIGx/overview'
ckurl = 'https://cod.tracker.gg/warzone/profile/atvi/koobie2727%239788810/overview'
everetturl = 'https://cod.tracker.gg/warzone/profile/xbl/Prodigychild11/overview'
erichurl = 'https://cod.tracker.gg/warzone/profile/xbl/captn%20ugly/overview'
zachurl = 'https://cod.tracker.gg/warzone/profile/xbl/Beastlyxone/overview'
samurl = 'https://cod.tracker.gg/warzone/profile/xbl/SKILLMASTERX109/overview'
maxurl = 'https://cod.tracker.gg/warzone/profile/xbl/big%20mackie%20xl/overview'
jonurl = 'https://cod.tracker.gg/warzone/profile/atvi/i-love-my-cub%235807885/overview'

# Start of gathering user data

# Get Cody's Data
response = requests.get(codyurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches played

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

codydata = []
codydata += [today, 'Cody']
codydata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
codydata += [int(values[16])] # Number of wins
codydata += [round(int(values[16])/int(codydata[2]),4)] # Win percentage
codydata += [int(values[17])] # Number of top 5s
codydata += [round(int(values[17])/int(codydata[2]),4)] # Top 5 percentage
codydata += [int(values[18])] # Number of top 10s
codydata += [round(int(values[18])/int(codydata[2]),4)] # Top 10 percentage
codydata += [int(values[19])] # Number of top 25s
codydata += [round(int(values[19])/int(codydata[2]),4)] # Top 25 percentage
codydata += [float(values[22])] # Kill to Death Ratio
codydata += [float(re.sub(',','',values[3]))] # Damage per Game
codydata += [values[24].replace("m ",":").replace("s","")] # Average life length
codydata += [float(re.sub(',','',values[26]))] # Average Score per Minute
codydata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get nick's Data
response = requests.get(nickurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

nickdata = []
nickdata += [today, 'Nick']
nickdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
nickdata += [int(values[16])] # Number of wins
nickdata += [round(int(values[16])/int(nickdata[2]),4)] # Win percentage
nickdata += [int(values[17])] # Number of top 5s
nickdata += [round(int(values[17])/int(nickdata[2]),4)] # Top 5 percentage
nickdata += [int(values[18])] # Number of top 10s
nickdata += [round(int(values[18])/int(nickdata[2]),4)] # Top 10 percentage
nickdata += [int(values[19])] # Number of top 25s
nickdata += [round(int(values[19])/int(nickdata[2]),4)] # Top 25 percentage
nickdata += [float(values[22])] # Kill to Death Ratio
nickdata += [float(re.sub(',','',values[3]))] # Damage per Game
nickdata += [values[24].replace("m ",":").replace("s","")] # Average life length
nickdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
nickdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get justin's Data
response = requests.get(justinurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

justindata = []
justindata += [today, 'Justin']
justindata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
justindata += [int(values[16])] # Number of wins
justindata += [round(int(values[16])/int(justindata[2]),4)] # Win percentage
justindata += [int(values[17])] # Number of top 5s
justindata += [round(int(values[17])/int(justindata[2]),4)] # Top 5 percentage
justindata += [int(values[18])] # Number of top 10s
justindata += [round(int(values[18])/int(justindata[2]),4)] # Top 10 percentage
justindata += [int(values[19])] # Number of top 25s
justindata += [round(int(values[19])/int(justindata[2]),4)] # Top 25 percentage
justindata += [float(values[22])] # Kill to Death Ratio
justindata += [float(re.sub(',','',values[3]))] # Damage per Game
justindata += [values[24].replace("m ",":").replace("s","")] # Average life length
justindata += [float(re.sub(',','',values[26]))] # Average Score per Minute
justindata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get bondo's Data
response = requests.get(bondourl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

bondodata = []
bondodata += [today, 'Bondo']
bondodata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
bondodata += [int(values[16])] # Number of wins
bondodata += [round(int(values[16])/int(bondodata[2]),4)] # Win percentage
bondodata += [int(values[17])] # Number of top 5s
bondodata += [round(int(values[17])/int(bondodata[2]),4)] # Top 5 percentage
bondodata += [int(values[18])] # Number of top 10s
bondodata += [round(int(values[18])/int(bondodata[2]),4)] # Top 10 percentage
bondodata += [int(values[19])] # Number of top 25s
bondodata += [round(int(values[19])/int(bondodata[2]),4)] # Top 25 percentage
bondodata += [float(values[22])] # Kill to Death Ratio
bondodata += [float(re.sub(',','',values[3]))] # Damage per Game
bondodata += [values[24].replace("m ",":").replace("s","")] # Average life length
bondodata += [float(re.sub(',','',values[26]))] # Average Score per Minute
bondodata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get logan's Data
response = requests.get(loganurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

logandata = []
logandata += [today, 'Logan']
logandata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
logandata += [int(values[16])] # Number of wins
logandata += [round(int(values[16])/int(logandata[2]),4)] # Win percentage
logandata += [int(values[17])] # Number of top 5s
logandata += [round(int(values[17])/int(logandata[2]),4)] # Top 5 percentage
logandata += [int(values[18])] # Number of top 10s
logandata += [round(int(values[18])/int(logandata[2]),4)] # Top 10 percentage
logandata += [int(values[19])] # Number of top 25s
logandata += [round(int(values[19])/int(logandata[2]),4)] # Top 25 percentage
logandata += [float(values[22])] # Kill to Death Ratio
logandata += [float(re.sub(',','',values[3]))] # Damage per Game
logandata += [values[24].replace("m ",":").replace("s","")] # Average life length
logandata += [float(re.sub(',','',values[26]))] # Average Score per Minute
logandata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get ck's Data
response = requests.get(ckurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

ckdata = []
ckdata += [today, 'CK']
ckdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
ckdata += [int(values[16])] # Number of wins
ckdata += [round(int(values[16])/int(ckdata[2]),4)] # Win percentage
ckdata += [int(values[17])] # Number of top 5s
ckdata += [round(int(values[17])/int(ckdata[2]),4)] # Top 5 percentage
ckdata += [int(values[18])] # Number of top 10s
ckdata += [round(int(values[18])/int(ckdata[2]),4)] # Top 10 percentage
ckdata += [int(values[19])] # Number of top 25s
ckdata += [round(int(values[19])/int(ckdata[2]),4)] # Top 25 percentage
ckdata += [float(values[22])] # Kill to Death Ratio
ckdata += [float(re.sub(',','',values[3]))] # Damage per Game
ckdata += [values[24].replace("m ",":").replace("s","")] # Average life length
ckdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
ckdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get everett's Data
response = requests.get(everetturl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

everettdata = []
everettdata += [today, 'Everett']
everettdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
everettdata += [int(values[16])] # Number of wins
everettdata += [round(int(values[16])/int(everettdata[2]),4)] # Win percentage
everettdata += [int(values[17])] # Number of top 5s
everettdata += [round(int(values[17])/int(everettdata[2]),4)] # Top 5 percentage
everettdata += [int(values[18])] # Number of top 10s
everettdata += [round(int(values[18])/int(everettdata[2]),4)] # Top 10 percentage
everettdata += [int(values[19])] # Number of top 25s
everettdata += [round(int(values[19])/int(everettdata[2]),4)] # Top 25 percentage
everettdata += [float(values[22])] # Kill to Death Ratio
everettdata += [float(re.sub(',','',values[3]))] # Damage per Game
everettdata += [values[24].replace("m ",":").replace("s","")] # Average life length
everettdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
everettdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get erich's Data
response = requests.get(erichurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

erichdata = []
erichdata += [today, 'Erich']
erichdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
erichdata += [int(values[16])] # Number of wins
erichdata += [round(int(values[16])/int(erichdata[2]),4)] # Win percentage
erichdata += [int(values[17])] # Number of top 5s
erichdata += [round(int(values[17])/int(erichdata[2]),4)] # Top 5 percentage
erichdata += [int(values[18])] # Number of top 10s
erichdata += [round(int(values[18])/int(erichdata[2]),4)] # Top 10 percentage
erichdata += [int(values[19])] # Number of top 25s
erichdata += [round(int(values[19])/int(erichdata[2]),4)] # Top 25 percentage
erichdata += [float(values[22])] # Kill to Death Ratio
erichdata += [float(re.sub(',','',values[3]))] # Damage per Game
erichdata += [values[24].replace("m ",":").replace("s","")] # Average life length
erichdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
erichdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get zach's Data
response = requests.get(zachurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

zachdata = []
zachdata += [today, 'Zach']
zachdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
zachdata += [int(values[16])] # Number of wins
zachdata += [round(int(values[16])/int(zachdata[2]),4)] # Win percentage
zachdata += [int(values[17])] # Number of top 5s
zachdata += [round(int(values[17])/int(zachdata[2]),4)] # Top 5 percentage
zachdata += [int(values[18])] # Number of top 10s
zachdata += [round(int(values[18])/int(zachdata[2]),4)] # Top 10 percentage
zachdata += [int(values[19])] # Number of top 25s
zachdata += [round(int(values[19])/int(zachdata[2]),4)] # Top 25 percentage
zachdata += [float(values[22])] # Kill to Death Ratio
zachdata += [float(re.sub(',','',values[3]))] # Damage per Game
zachdata += [values[24].replace("m ",":").replace("s","")] # Average life length
zachdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
zachdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get sam's Data
response = requests.get(samurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

samdata = []
samdata += [today, 'Sam']
samdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
samdata += [int(values[16])] # Number of wins
samdata += [round(int(values[16])/int(samdata[2]),4)] # Win percentage
samdata += [int(values[17])] # Number of top 5s
samdata += [round(int(values[17])/int(samdata[2]),4)] # Top 5 percentage
samdata += [int(values[18])] # Number of top 10s
samdata += [round(int(values[18])/int(samdata[2]),4)] # Top 10 percentage
samdata += [int(values[19])] # Number of top 25s
samdata += [round(int(values[19])/int(samdata[2]),4)] # Top 25 percentage
samdata += [float(values[22])] # Kill to Death Ratio
samdata += [float(re.sub(',','',values[3]))] # Damage per Game
samdata += [values[24].replace("m ",":").replace("s","")] # Average life length
samdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
samdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get max's Data
response = requests.get(maxurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

maxdata = []
maxdata += [today, 'Max']
maxdata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
maxdata += [int(values[16])] # Number of wins
maxdata += [round(int(values[16])/int(maxdata[2]),4)] # Win percentage
maxdata += [int(values[17])] # Number of top 5s
maxdata += [round(int(values[17])/int(maxdata[2]),4)] # Top 5 percentage
maxdata += [int(values[18])] # Number of top 10s
maxdata += [round(int(values[18])/int(maxdata[2]),4)] # Top 10 percentage
maxdata += [int(values[19])] # Number of top 25s
maxdata += [round(int(values[19])/int(maxdata[2]),4)] # Top 25 percentage
maxdata += [float(values[22])] # Kill to Death Ratio
maxdata += [float(re.sub(',','',values[3]))] # Damage per Game
maxdata += [values[24].replace("m ",":").replace("s","")] # Average life length
maxdata += [float(re.sub(',','',values[26]))] # Average Score per Minute
maxdata += [float(re.sub(',','',values[27]))] # Average Score per Game

# Get jon's Data
response = requests.get(jonurl)

soup = BeautifulSoup(response.text,'html.parser')

matches = soup.find_all(class_='matches')[1].get_text() # Get number of matches

values = []
for value in soup.select('.value'): # Get all other data
    values += [value.get_text()]

jondata = []
jondata += [today, 'Jon']
jondata += [float(re.sub('[^0-9]','',matches))] # Number of matches played with words removed
jondata += [int(values[16])] # Number of wins
jondata += [round(int(values[16])/int(jondata[2]),4)] # Win percentage
jondata += [int(values[17])] # Number of top 5s
jondata += [round(int(values[17])/int(jondata[2]),4)] # Top 5 percentage
jondata += [int(values[18])] # Number of top 10s
jondata += [round(int(values[18])/int(jondata[2]),4)] # Top 10 percentage
jondata += [int(values[19])] # Number of top 25s
jondata += [round(int(values[19])/int(jondata[2]),4)] # Top 25 percentage
jondata += [float(values[22])] # Kill to Death Ratio
jondata += [float(re.sub(',','',values[3]))] # Damage per Game
jondata += [values[24].replace("m ",":").replace("s","")] # Average life length
jondata += [float(re.sub(',','',values[26]))] # Average Score per Minute
jondata += [float(re.sub(',','',values[27]))] # Average Score per Game

# End of gathering user data

# Write data to csv file

csvname = str(date.today().strftime('%Y_%m_%d')) + "_warzone_data.csv"
with open(csvname, 'w', newline='') as csv_file:
    csv_writer = writer(csv_file)
    csv_writer.writerow(codydata)
    csv_writer.writerow(nickdata)
    csv_writer.writerow(justindata)
    csv_writer.writerow(bondodata)
    csv_writer.writerow(logandata)
    csv_writer.writerow(ckdata)
    csv_writer.writerow(everettdata)
    csv_writer.writerow(erichdata)
    csv_writer.writerow(zachdata)
    csv_writer.writerow(samdata)
    csv_writer.writerow(maxdata)
    csv_writer.writerow(jondata)

# Write data to google sheets

gc = gspread.service_account(filename='G:\python\COD_Warzone_Data\client_secret.json')
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1Q7pArMUHR_dGr3Bjk-FvogxhG3hHbouUIZ7ElAv8YwI/edit#gid=1520860220').worksheet("Raw Data")
sh.append_row(codydata, value_input_option='USER_ENTERED')
sh.append_row(nickdata, value_input_option='USER_ENTERED')
sh.append_row(justindata, value_input_option='USER_ENTERED')
sh.append_row(bondodata, value_input_option='USER_ENTERED')
sh.append_row(logandata, value_input_option='USER_ENTERED')
sh.append_row(ckdata, value_input_option='USER_ENTERED')
sh.append_row(everettdata, value_input_option='USER_ENTERED')
sh.append_row(erichdata, value_input_option='USER_ENTERED')
sh.append_row(zachdata, value_input_option='USER_ENTERED')
sh.append_row(samdata, value_input_option='USER_ENTERED')
sh.append_row(maxdata, value_input_option='USER_ENTERED')
sh.append_row(jondata, value_input_option='USER_ENTERED')

sh.format('A62:A', {
    "numberFormat": {
      "type": "DATE"
    }})


import os
from typing import List
import numpy as np

MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
PROJECT_DIR = os.path.join(MODULE_DIR, "..")
INPUT_SOURCE_DIR = os.path.join(PROJECT_DIR, "input")

def get_data_lines(input_file_name):
    input_file = os.path.join(INPUT_SOURCE_DIR, input_file_name)
    print(f"Input file: {input_file}")

    data_file = open(input_file)
    return data_file.read().split("\n")

def fold_at(coordinates: List[List[int]], axis: str, mark: int):
    for coordinate in coordinates:
        x = coordinate[0]
        y = coordinate[1]

        if axis == "x":
            if x > mark:
                coordinate[0] = mark - (x - mark)
        else:
            if y > mark:
                coordinate[1] = mark - (y - mark)

def unique_coordinate_count(coordinates: List[List[int]]):
    return len(set([",".join([str(i) for i in coordinate]) for coordinate in coordinates]))

def print_the_thing(coordinates: List[List[int]]):
    maxes = np.max(coordinates, 0)
    max_x = maxes[0]
    max_y = maxes[1]

    print("")
    for y in range(max_y + 1):
        for x in range(max_x + 1):
            if [x, y] in coordinates:
                print("#", end="")
            else:
                print(" ", end="")
        print("")
    print("")

def do_the_thing(input_file_name: str, folds_to_complete: int = -1, display_output: bool = False):
    data_lines = get_data_lines(input_file_name)
    print(f"Number of data lines: {len(data_lines)}")

    coordinates = []
    record_folds = False
    for data_line in data_lines:
        if data_line.strip() == "":
            record_folds = True
            continue

        if not record_folds:
            coordinates.append(list(map(int, data_line.split(","))))
        elif folds_to_complete != 0:
            fold_instruction = data_line.split()[2].split("=")
            fold_at(coordinates, fold_instruction[0], int(fold_instruction[1]))
            folds_to_complete -= 1

    print(f"Number of unique coordinates after folds: {unique_coordinate_count(coordinates)}")

    if display_output:
        print_the_thing(coordinates)

    print(f"#################################\n")

def day_13_do(input_file_name):
    do_the_thing(input_file_name, 1)

def day_13_do_2(input_file_name):
    do_the_thing(input_file_name, -1, True)

day_13_do("day13.txt")
day_13_do_2("day13.txt")


# Ex 1
# Având lista:
# mașini = ['Audi', 'Volvo', 'BMW', 'Mercedes', 'Aston Martin', 'Lăstun',
# 'Fiat', 'Trabant', 'Opel']
# Folosește un for că să iterezi prin toată lista și să afișezi;
# ● ‘Mașina mea preferată este x’.
# ● Fă același lucru cu un for each.
# ● Fă același lucru cu un while.

masini = ['Audi', 'Volvo', 'BMW', 'Mercedes', 'Aston Martin', 'Lăstun', 'Fiat', 'Trabant', 'Opel']

for i in range(len(masini)):
    print(f'Masina mea preferata este: {masini[i]}')

# for each
for masina in masini:
    print(f'Masina mea preferata este: {masina}')

# while
i = 0
while i < len(masini):
    print(f'Masina mea preferata este: {masini[i]}')
    i = i + 1

# Ex 2
# Aceeași listă:
# Folosește un for else
# În for
# - Modifică elementele din listă astfel încât să fie scrie cu majuscule,
# exceptând primul și ultimul.
# În else:
# - Printează lista.

for masina in range(1, len(masini)-1):
    masini[masina] = masini[masina].upper()
else:
    print(masini)

# Ex 3
# Aceeași listă:
# Vine un cumpărător care dorește să cumpere un Mercedes.
# Itereaza prin ea prin modalitatea aleasă de tine.
# Dacă mașina e mercedes:
# Printează ‘am găsit mașina dorită de dvs’
# Ieși din ciclu folosind un cuvânt cheie care face acest lucru
# Altfel:
# Printează ‘Am găsit mașina X. Mai căutam‘

for masina in masini:
    if masina == 'Mercedes':
        print(f'Am gasit masina dorita de dvs {masina}')
        break
    else:
        print(f'Am gasit masina {masina}. Mai cautam')

# Ex 4
# Aceași listă;
# Vine un cumpărător bogat dar indecis. Îi vom prezenta toate mașinile cu
# excepția Trabant și Lăstun.
# - Dacă mașina e Trabant sau Lăstun:
# Folosește un cuvânt cheie care să dea skip la ce urmează (nu trebuie
# else).
# - Printează S-ar putea să vă placă mașina x.

for masina in masini:
    if masina == "Trabant" or masina == 'Lastun':
        continue
    print(f'S-ar putea sa va placa masina {masina}')

# Ex 5
# Modernizează parcul de mașini:
# ● Crează o listă goală, masini_vechi.
# ● Itereaza prin mașini.
# ● Când găsesti Lăstun sau Trabant:
# - Salvează aceste mașini în masini_vechi.
# - Suprascrie-le cu ‘Tesla’ (în lista inițială de mașini).
# ● Printează Modele vechi: x.
# ● Modele noi: x.

masini_vechi = []

for masina in masini:
    if masina == 'Lastun' or masina == 'Trabant':
        masini_vechi.append(masina)
        i = masini.index(masina)
        masini[i] = 'Tesla'
print('Modele vechi:', masini_vechi)
print('Modele noi:', masini)


phrase = "1113222113"
for _ in range(50): #part1: 40
    new_phrase = ""
    i = 0
    run_len = 0
    current_num = phrase[i]
    while i < len(phrase):
        if phrase[i] == current_num:
            run_len += 1
        else:
            new_phrase += str(run_len) + current_num
            current_num = phrase[i]
            run_len = 1
        i += 1
    phrase = new_phrase + str(run_len) + current_num
print(len(phrase))


import numpy as np
import pandas as pd
import seaborn as sns
import bisect
from bin.deepBind_functions import *
from seaborn import color_palette
from dna_features_viewer import GraphicFeature, GraphicRecord

# COLINDEX
START = 1
END = 2
PRED_SCORE = 3
# COL NAMES
FLOAT_COLS = ['START','END','PREDICTION_SCORE']
PRED_COLS = ['SEQ_ID'] + FLOAT_COLS

def point_to_segment(segment_df: pd.DataFrame, point_arr: np.ndarray) -> np.ndarray:
    """
    Maps points to segments in a continuous range.

    Parameters:
    segment_df (pd.DataFrame): DataFrame with 'START' and 'END' columns representing continuous, non-overlapping ranges.
    point_arr (np.ndarray): Array of points to map to segments.

    Returns:
    np.ndarray: Array with segment indices for each point.
    """
    flat_ranges = segment_df.flatten()
    intersection = (point_arr.apply(lambda x: bisect.bisect_left(flat_ranges, x)) // 2)
    return intersection

def process_prediction_file(file_path: str, seq_id: str) -> pd.DataFrame:
    """
    use only for predictions where each segment is overlapping (only) with it's previous segment
    Process a prediction file -
        1) open to pandas DF
        2) adds cols names
        3) create a DF with each overlapping segments on the same rows

    Parameters:
    file_path (str): Path to the prediction file in TSV format.

    Returns:
    pd.DataFrame: Processed DataFrame containing relevant data.

    Example:
    >>> processed_df = process_prediction_file('SOX9_en13_seq_win_16_shift_8.tsv')
    >>> print(processed_df.head())
    """
    pred_df = pd.read_csv(file_path, sep='\t', header=None)
    pred_df.columns = PRED_COLS
    pred_df.loc[:, FLOAT_COLS] = pred_df.loc[:, FLOAT_COLS].astype(float)
    pred_df = pred_df[pred_df.SEQ_ID == seq_id]

    shifted = pred_df.shift(-1)
    shifted.columns = shifted.columns + '_shift'

    shift_df = pd.concat([pred_df, shifted], axis=1).dropna()

    sanity = (shift_df.START_shift > shift_df.START) & (shift_df.START_shift < shift_df.END)
    # print(sanity.all())
    return shift_df.reset_index(drop=True)

def prepare_range_df(file_path: str,  seq_id: str, seq_length: int) -> pd.DataFrame:
    """
    Prepare a range DataFrame from the output of the previous function.
    returns a nX2 df with START & END for each segment and it's mean score (each segments have 2 prediction score)

    Parameters:
    shift_df (pd.DataFrame): Processed DataFrame from the process_prediction_file.
    seq_length (int): Length of the sequence.

    Returns:
    pd.DataFrame: Range DataFrame with 'START', 'END', and 'SCORE' columns.

    Example:
    >>> range_df = prepare_range_df(processed_df, sequence_length)
    >>> print(range_df.head())
    """
    shift_df = process_prediction_file(file_path,seq_id)

    # Prepare the first and last lines
    first_line = shift_df.loc[0, ['START', 'START_shift', 'PREDICTION_SCORE']].rename({'START_shift': 'END', 'PREDICTION_SCORE': 'SCORE'})

    first_line['END'] = first_line['END'] - 1
    last_line = shift_df.loc[shift_df.shape[0] - 1, ['END', 'END_shift', 'PREDICTION_SCORE_shift']].rename({'END': 'START',
     'END_shift': 'END', 'PREDICTION_SCORE_shift': 'SCORE'})
    first_line['START'] = first_line['START'] + 1

    # Create new columns for the range DataFrame
    new_start = shift_df.START_shift
    new_end = shift_df.END
    new_score = (shift_df.PREDICTION_SCORE + shift_df.PREDICTION_SCORE_shift) / 2

    # Create the main portion of the range DataFrame
    new_df = pd.DataFrame({'START': new_start, 'END': new_end, 'SCORE': new_score})
    new_df['END'] = new_df['END'] - 1

    # Prepare lines for the start and end of the sequence
    zero_line_s = pd.Series([0, first_line.START - 1, 0], index=first_line.index)
    zero_line_e = pd.Series([last_line.END + 1, seq_length - 1, 0], index=first_line.index)

    # Concatenate all lines to create the final range DataFrame
    range_df = pd.concat([pd.DataFrame(zero_line_s).T, pd.DataFrame(first_line).T, new_df, pd.DataFrame(last_line).T, pd.DataFrame(zero_line_e).T]).reset_index(drop=True)

    return range_df

def plot_get_bs_feature(seq_df, bs_col, bs_dict, cmap='pastel'):
    """
    Generate DNA sequence binding site features for visualization.

    This function processes a sequence DataFrame, a binding site column,
    to create DNA sequence binding site features suitable for visualization using the
    `dna_features_viewer` library.

    Parameters:
    seq_df (pd.DataFrame): Sequence DataFrame.
    bs_col (str): Column containing binding site label.

    Returns:
    list: List of GraphicFeature objects representing binding site features.

    Example:
    >>> colors = ["#FFB3BA", "#FFFFBA", "#Baffc9"]
    >>> bs_features = plot_get_bs_feature(sequence_df, "Binding_Sites", colors)
    """
    colors = sns.color_palette(cmap)

    features = []
    bs_list = seq_df[bs_col].unique()
    bs_list = bs_list[bs_list != '']

    for i in range(len(bs_list)):
        bs = bs_list[i]
        bs_df = seq_df[seq_df[bs_col] == bs]
        seq = bs_dict[bs]

        while bs_df.shape[0]:
            start = bs_df.index.min()
            end = start + len(seq) - 1
            features.append(GraphicFeature(start=start, end=end,
                                       strand=+1, color=colors[i], label=bs))
            bs_df = bs_df.drop(range(start,end + 1))

    return features

def add_binding_site_label(seq_df, binding_site, binding_site_name, binding_site_col):
    """
    Label a binding site in a sequence DataFrame.

    Parameters:
    seq_df (pd.DataFrame): Sequence DataFrame.
    binding_site (str): Binding site to label.
    binding_site_name (str): Label for the binding site.
    binding_site_col (str): Column for the label.

    Returns:
    pd.DataFrame: Sequence DataFrame with added label.

    Example:
    sequence_df = add_binding_site_label(sequence_df, "ACTG", "Promoter", "Region_Label")
    """
    cur_seq = "".join(seq_df.base)
    s_idx = cur_seq.find(binding_site)
    save = 0
    while cur_seq:
        s_idx = cur_seq.find(binding_site)
        if s_idx == -1:
            break
        edge = s_idx + save + len(binding_site) - 1
        seq_df.loc[s_idx + save : edge , binding_site_col] = binding_site_name
        save = edge + 1
        cur_seq = cur_seq[s_idx + len(binding_site) - 1:]

    return seq_df

def deepBind_score_df(seq_id, seq_dict, score_file):
    """
    Retrieve deepBind scores and annotate a DNA sequence.

    This function takes a sequence ID, a dictionary of sequences, and a score file path.
    It calculates deepBind scores for the sequence and annotates the sequence DataFrame
    with these scores.

    Parameters:
    seq_id (str): Identifier for the sequence in seq_dict.
    seq_dict (dict): Dictionary containing sequences.
    score_file (str): Path to the score file.

    Returns:
    pd.DataFrame: Annotated sequence DataFrame with deepBind scores.

    Example:
    >>> seq_dict = {"seq1": "ACTGAGCTAG", "seq2": "CGTAGCTA"}
    >>> score_file = "scores.csv"
    >>> annotated_df = deepBind_score_df("seq1", seq_dict, score_file)
    """

    # Retrieve the sequence based on seq_id
    seq = seq_dict[seq_id]

    # Prepare a DataFrame for the sequence
    segment_df = prepare_range_df(score_file, seq_id, len(seq))
    seq_df = pd.DataFrame(list(seq)).reset_index().rename(columns={"index": 'POS', 0: 'base'})

    # Calculate deepBind scores and annotate the sequence DataFrame
    score_idx = point_to_segment(segment_df[['START', 'END']].to_numpy(), seq_df.POS)
    seq_df['SCORE'] = segment_df.SCORE[score_idx].tolist()

    return seq_df

def get_binding_site_label_df(seq_df : pd.DataFrame, bs_dict : dict, label_col_name: str) -> pd.DataFrame:
    result = seq_df[['POS', 'base']].copy()
    result[label_col_name] = ''
    for b in bs_dict.keys():
        result = add_binding_site_label(result,bs_dict[b],b,label_col_name)
    return result

import numpy as np
import matplotlib.pyplot as plt

def color_rows_in_matrix(mat, labels, cmap_name='tab10', fig_width=10, fig_height=6):
    num_rows, num_cols = mat.shape
    cmap = plt.get_cmap(cmap_name, num_rows)

    colored_matrix = np.zeros((num_rows, num_cols, 4))  # Include alpha (transparency) channel

    uniq_lables = list(set([i.split('_')[0] for i in labels]))
    color_dict = {uniq_lables[i]: cmap(i) for i in range(len(uniq_lables)) }

    for i in range(num_rows):
        row_color = color_dict[labels[i].split('_')[0]]
        alpha_values = mat[i]  # Use matrix values for alpha (opacity)

        for j in range(num_cols):
            colored_matrix[i, j, :3] = row_color[:3]  # Assign RGB color
            colored_matrix[i, j, 3] = alpha_values[j]  # Assign alpha value from the matrix

    fig, ax = plt.subplots(figsize=(fig_width, fig_height))
    ax.imshow(colored_matrix, aspect=10)
    ax.axis('off')

    if labels is not None:
        handles = [plt.Line2D([0], [0], marker='o', color='w', label=label, markerfacecolor=cmap(i)) for i, label in enumerate(uniq_lables)]
        legend = ax.legend(handles=handles, title="Labels", bbox_to_anchor=(1.05, 1), loc='upper left')
        ax.add_artist(legend)  # Add the legend back to the plot

    plt.show()


from collections import defaultdict
from psycopg2 import DataError, ProgrammingError
from lib.ep_config import ep_cfg, ConfigFile
from lib.ep_libutil import ep_connection, ep_rtcfg, ep_internal_path

from lib.db import Relation, get_relation, get_geometry_relation
from transformations.builtin import TransformationQueue,\
                                    SRIDTransformation, \
                                    AreaTransformation, \
                                    IntersectTransformation,\
                                    SurrogateTransformation,\
                                    LimitToGridTransformation,\
                                    ToGridTransformation, \
                                    SourcesToGridTransformation,\
                                    MaskTransformation,\
                                    SourceFilterTransformation, \
                                    ScenarioTransformation, \
                                    LevelFilterTransformation
import lib.ep_logging
log = lib.ep_logging.Logger(__name__)

filter_columns = {
        'inventory': 'like',
        'eset': 'like',
        'source_type': 'eq'
        }

def get_filter(k, v):
    op = filter_columns[k]
    if v.strip()[0] == '!':
        op = 'n'+op
        v = v[1:]

    return ('{key}__{op}'.format(key=k, op=op), v)

builtin_transformations_conf = {
        'to_grid': defaultdict(lambda: False, {'type': 'to_grid', 'normalize': True, 'mandatory': True}),
        'scenarios': defaultdict(lambda: False, {'type': 'scenarios'}),
        'level_filter': defaultdict(lambda: False, {'type': 'level_filter'}),
}

def get_builtin_transformation(name):
    class TransformationConfig():
        pass

    if name in builtin_transformations_conf:
        trans = TransformationConfig()
        for k, v in builtin_transformations_conf[name].items():
            setattr(trans, k, v)
    else:
        trans = None

    return trans

def create_default_transformation_queue(queue_id=1, name='default_transformation_chain'):
    case_schema = ep_cfg.db_connection.case_schema
    source_schema = ep_cfg.db_connection.source_schema

    transformation_queue = TransformationQueue(queue_id=queue_id,
                                        name=name,
                                        inrel=Relation(schema=source_schema,
                                                        name='ep_in_geometries',
                                                        fields=['geom_id']),
                                        outrel=Relation(schema=case_schema,
                                                        name='ep_sources_grid',
                                                        pk='sg_id',
                                                        geom_field='geom'),
                                        outsrid=ep_cfg.projection_params.projection_srid)
    transformation_queue.db_connection = ep_connection
    transformation_queue.schema = case_schema
    transformation_queue.cfg = ep_cfg
    transformation_queue.rt_cfg = ep_rtcfg
    transformation_queue.append(SourcesToGridTransformation(srcrel=Relation(schema=source_schema,
                                                                            name='ep_in_sources'),
                                                            case_schema=case_schema))
    return transformation_queue

def prepare():
    global transconf
    transconf = None
    try:
        transconf = ConfigFile(ep_cfg.transformations.source,
                               ep_internal_path('transformations', 'configspec-transformations.conf')).values()
        if ep_cfg.transformations.chains is None:
            chains = [f for f in transconf.transformations if f.mandatory]
        else:
            chains = ep_cfg.transformations.chains
    except AttributeError:
        chains = []

    ep_rtcfg['transformation_queues'] = []
    if len(list(chains)) == 0:
        transformation_queue = create_default_transformation_queue()
        transformation_queue.insert(ToGridTransformation(), index=0)
        ep_rtcfg['transformation_queues'].append(transformation_queue)
    else:
        for idx, chain in enumerate(chains, start=1):
            # build list of mandatory transformation classes
            mandatory_transformations = []
            for transname, trans in builtin_transformations_conf.items():
                if trans['mandatory']:
                    trans = get_builtin_transformation(transname)
                    mandatory_transformations.append(create_transformation(trans))

            # create and build new transformation queue
            transformation_queue = create_default_transformation_queue(queue_id=idx, name=chain)
            ep_rtcfg['transformation_queues'].append(transformation_queue)
            for tidx, transname in enumerate(getattr(chains, chain)):
                log.debug('transname: ', transname)
                transformation = create_transformation(transname)
                transformation_queue.insert(transformation, index=tidx)
                for t in mandatory_transformations:
                    if isinstance(transformation, type(t)):
                        mandatory_transformations.remove(t)

            # ensure all mandatory transformations are included at the end of the chain
            for t in mandatory_transformations:
                transformation_queue.insert(t, -1)

def create_transformation(trans):
    log.debug('create_transformation:',trans)
    filters = []
    transformation = None
    if isinstance(trans, str):
        transname = trans
        if ':' in trans:
            transname, transparams = trans.split(':', maxsplit=1)

        if hasattr(transconf.transformations, transname):
            trans = getattr(transconf.transformations, transname)
        else:
            trans = get_builtin_transformation(transname)

    if trans.type == 'source_filter':
        if hasattr(trans, 'set') and trans.set is not None:
            filter_key, filter_val = get_filter('eset', trans.set)
            filters.append([filter_key, filter_val])
        elif hasattr(trans, 'inventory') and trans.inventory is not None:
            for inv_spec in trans.inventory:
                filter_key, filter_val = get_filter('inventory', inv_spec)
                filters.append([filter_key, filter_val])

        if hasattr(trans, 'filter_by') and hasattr(trans, 'filter_value') and trans.filter_by is not None:
            filter_key, filter_val = get_filter(trans.filter_by, trans.filter_value)
            filters.append([filter_key, filter_val])

        outsrid = None
        if hasattr(trans, 'outsrid') and trans.outsrid is not None:
            outsrid = trans.outsrid

        transformation = SourceFilterTransformation(filters=filters, outsrid=outsrid,
                                                 mask_to_grid=trans.mask_to_grid,
                                                 transform_srid=trans.transform_srid)

    elif trans.type == 'area_multiplicator':
        transformation = AreaTransformation()

    elif trans.type == 'limit_to_grid':
        transformation = LimitToGridTransformation()

    elif trans.type == 'srid_transform':
        transformation = SRIDTransformation()

    elif trans.type == 'mask':
        log.debug('Mask transaction:', trans.mask_file, trans.mask_filters)
        maskrel = get_geometry_relation(trans.mask_file, trans.mask_filters)
        transformation = MaskTransformation(maskrel=maskrel, mask_type=trans.mask_type)

    elif trans.type == 'to_grid':
        if hasattr(trans, 'normalize'):
            normalize = trans.normalize
        else:
            normalize = None
        if hasattr(trans, 'method'):
            method = trans.method
        else:
            method = 'Area'
        transformation = ToGridTransformation(normalize=normalize, method=method)

    elif trans.type == 'intersect' and hasattr(trans, 'intersect') and trans.intersect is not None:
        # TODO this is wrong, it remainded from time of call to_grid as intersect, needs to be generalized here!!!
        case_schema = ep_cfg.db_connection.case_schema
        inrel2 = Relation(schema=case_schema, name=trans.intersect, fields=['grid_id'])
        try:
            outrel = get_relation(trans.target)
            outrel.coef = outrel.fields[0]  # FIXME
        except AttributeError:
            outrel = None
        transformation = IntersectTransformation(inrel2=inrel2, outrel=outrel)

    elif trans.type == 'surrogate' and hasattr(trans, 'surrogate_set') and trans.surrogate_set is not None:
        if hasattr(trans, 'surrogate_type'):
            transformation = SurrogateTransformation(surset=trans.surrogate_set, surtype=trans.surrogate_type)
        else:
            transformation = SurrogateTransformation(surset=trans.surrogate_set)

    elif trans.type == 'scenarios':
        transformation = ScenarioTransformation(transparams)
    elif trans.type == 'level_filter':
        transformation = LevelFilterTransformation(transparams)

    return transformation

def run():
    cur = ep_connection.cursor()
    log.debug('*** Initialize transformation queue...')
    cur.execute('SELECT ep_init_transformation_queue(%s)', [ep_cfg.db_connection.case_schema])
    try:
        for q in ep_rtcfg['transformation_queues']:
            log.debug('Run transformation queue #', q.queue_id)
            q.process()
    except (DataError, ProgrammingError):
        log.sql_debug(ep_connection)
        raise

    log.debug('*** Finalize transformation queue...')
    ftq_placeholders = ['%s']*6
    ftq_args = [ep_cfg.db_connection.source_schema,
                ep_cfg.db_connection.case_schema,
                'ep_in_sources', 'ep_in_emissions', 'ep_in_activity_data',
                'ep_emission_sets']

    if ep_cfg.run_params.scenarios.all_emissions:
        log.debug('*** Global scenario ', ep_cfg.run_params.scenarios.all_emissions, 'will be applied')
        ftq_args.extend(('ep_scenario_list', 'ep_scenario_factors_all', ep_cfg.run_params.scenarios.all_emissions))
        ftq_placeholders.extend(['%s']*3)
    else:
        log.debug('*** No global scenario configured...')

    log.debug(
    cur.mogrify('SELECT ep_finalize_transformation_queue({})'.format(', '.join(ftq_placeholders)),
                ftq_args)
                )
    cur.execute('SELECT ep_finalize_transformation_queue({})'.format(', '.join(ftq_placeholders)),
                ftq_args)

    ep_connection.commit()


#!/usr/bin/python
import argparse
import os.path
import numpy as np
from bidict import bidict

symbolTable = bidict({
	'add' : 1,
	'sub' : 2,
	'mul' : 3,
	'div' : 4,
	'xor' :  5,
	'or' :  6,
	'and' : 7,
	'inv' : 8,
	'inc' : 9,
	'dec' : 10,
	'jmp' : 11,
	'jeq' : 12,
	'jne' : 13,
	'jlt' : 14,
	'jgt' : 15,
	'jlte' : 16,
	'jgte' : 17,
	'push' : 18,
	'pop' : 19,
	'movrm' : 21,
	'movrs' : 22,
	'lstat' : 23,
	'stget' : 24,
	'stput' : 25,
	'print' : 26,
	'read' : 27,
	'ret' : 28,
	'movis' : 29,
	'movim' : 30,
})

singleInstr = [
	'inv',
	'inc',
	'dec',
	'jmp',
	'push',
	'pop',
	'ret']
doubleInstr = [
	'add',
	'sub',
	'mul',
	'div',
	'xor',
	'or',
	'and',
	'lstat',
	'stget',
	'stput',
	'movis',
	'movim',
	'movrm',
	'movrs',
	'print',
	'read']
tripleInstr = [
	'jeq',
	'jne',
	'jlt',
	'jgt',
	'jlte',
	'jgte']

regTable = {
	'm0': 0,
	'm1': 1,
	'm2': 2,
	'm3': 3,
	'm4': 4,
	'm5': 5,
	'm6': 6,
	'm7': 7,
	's0': 0,
	's1': 1,
	's2': 2,
	's3': 3,
	's4': 4,
	's5': 5,
	's6': 6,
	's7': 7,
}

class AssemblyException(Exception):
	def __init__(self, value):
		self.message = value

	def __str__(self):
		return self.message

#NOTE:
#Data fields must be converted to twin-ASCII hex representations
# i.e., to do a newline (\n), do: 0a

def assemble(program):
	lines = program.split('\n')
	lineCount = 1
	dataMode=True
	labels = {}
	offset = 0
	byteStr = ''
	statMem = []
	statMemTable = {}
	for line in lines:
		if dataMode:
			if '#' in line:
				line = line[0:line.find('#')]
			if line == '_data:':
				pass
			elif line == '_text:':
				dataMode = False
			elif ':' in line:
				symbol, data = line.split(':')
				dataBlock = ''
				for i in range(0, len(data), 2):
					currHex = data[i:i+2]
					num = int(currHex, 16)
					dataBlock += chr(num)
				dataLen = len(dataBlock)
				statOff = len(statMem)
				statMemTable[symbol] = (dataLen, statOff)
				for elem in dataBlock:
					statMem.append(elem)
		else:
			if '#' in line:
				line = line[0:line.find('#')]
			spaceArgs = line.strip().split(' ')
			instr = spaceArgs[0]
			del spaceArgs[0]
			if not instr:
				#Don't increment the offset, ignore (should happen
				# for commented out lines)
				pass
			elif instr in symbolTable:
				#Get the correct opcode
				opcode = symbolTable[instr]
				#Parse out any ',' in the arguments and then strip:
				args = [arg.replace(',','').strip() for arg in spaceArgs]
				if instr in singleInstr:
					if len(args) == 1:
						arg = args[0]
						#Handle single instruction
						if arg in regTable:
							arg = regTable[arg]
						elif arg in labels:
							arg = labels[arg]
						elif 'LEN_' in arg:
							dataLen, statOff = statMemTable[arg[len('LEN_'):]]
							arg = dataLen
						elif '_OFF' in arg:
							dataLen, statOff = statMemTable[arg.split('_OFF')[0]]
							arg = statOff
						elif ('%d' % int(arg)) == arg:
							arg = int(arg)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg, lineCount))
						byteStr += chr(opcode)
						byteStr += chr(arg)
						#handle instruction, increment offset
						offset += 2

					else:
						raise AssemblyException(
							'Incorrect number of arguments for instruction %s on line %d' %
							(instr, lineCount))
				elif instr in doubleInstr:
					if len(args) == 2:
						#Handle double instructions
						arg1 = args[0]
						arg2 = args[1]
						if arg1 in regTable:
							arg1 = regTable[arg1]
						elif arg1 in labels:
							arg1 = labels[arg1]
						elif 'LEN_' in arg1:
							dataLen, statOff = statMemTable[arg1[len('LEN_'):]]
							arg1 = dataLen
						elif '_OFF' in arg1:
							dataLen, statOff = statMemTable[arg1.split('_OFF')[0]]
							arg1 = statOff
						elif ('%d' % int(arg1)) == arg1:
							arg1 = int(arg1)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg, lineCount))
						if arg2 in regTable:
							arg2 = regTable[arg2]
						elif arg2 in labels:
							arg2 = labels[arg2]
						elif 'LEN_' in arg2:
							dataLen, statOff = statMemTable[arg2[len('LEN_'):]]
							arg2 = dataLen
						elif '_OFF' in arg2:
							dataLen, statOff = statMemTable[arg2.split('_OFF')[0]]
							arg2 = statOff
						elif ('%d' % int(arg2)) == arg2:
							arg2 = int(arg2)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg, lineCount))
						byteStr += chr(opcode)
						byteStr += chr(arg1)
						byteStr += chr(arg2)
						#handle instruction, increment offset
						offset += 3
					else:
						raise AssemblyException(
							'Incorrect number of arguments for instruction %s on line %d' %
							(instr, lineCount))
				elif instr in tripleInstr:
					if len(args) == 3:
						#Handle three argument instructions
						arg1 = args[0]
						arg2 = args[1]
						arg3 = args[2]
						if arg1 in regTable:
							arg1 = regTable[arg1]
						elif arg1 in labels:
							arg1 = labels[arg1]
						elif 'LEN_' in arg1:
							dataLen, statOff = statMemTable[arg1[len('LEN_'):]]
							arg1 = dataLen
						elif '_OFF' in arg1:
							dataLen, statOff = statMemTable[arg1.split('_OFF')[0]]
							arg1 = statOff
						elif ('%d' % int(arg1)) == arg1:
							arg1 = int(arg1)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg1, lineCount))
						if arg2 in regTable:
							arg2 = regTable[arg2]
						elif arg2 in labels:
							arg2 = labels[arg2]
						elif 'LEN_' in arg2:
							dataLen, statOff = statMemTable[arg2[len('LEN_'):]]
							arg2 = dataLen
						elif '_OFF' in arg2:
							dataLen, statOff = statMemTable[arg2.split('_OFF')[0]]
							arg2 = statOff
						elif ('%d' % int(arg2)) == arg2:
							arg2 = int(arg2)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg2, lineCount))
						if arg3 in regTable:
							arg3 = regTable[arg3]
						elif arg3 in labels:
							arg3 = labels[arg3]
						elif 'LEN_' in arg3:
							dataLen, statOff = statMemTable[arg3[len('LEN_'):]]
							arg3 = dataLen
						elif '_OFF' in arg3:
							dataLen, statOff = statMemTable[arg3.split('_OFF')[0]]
							arg3 = statOff
						elif ('%d' % int(arg3)) == arg3:
							arg3 = int(arg3)
						else:
							raise AssemblyException('Argument not found: %s on line %d' %
								(arg3, lineCount))
						byteStr += chr(opcode)
						byteStr += chr(arg1)
						byteStr += chr(arg2)
						byteStr += chr(arg3)
						#handle instruction, increment offset
						offset += 4
					else:
						raise AssemblyException(
							'Incorrect number of arguments for instruction %s on line %d' %
							(instr, lineCount))
			else:
				#Check for presence of ':' at end of line:
				if instr[-1] == ':':
					#this must be a label, add it to the label dict
					labels[instr[:-1]] = offset
				else:
					raise AssemblyException('Error: instruction not found: %s on line %d'
						% (instr, lineCount))
				#don't increment offset
		lineCount += 1
	statMem = ''.join(statMem)
	return (byteStr, statMem)

def main():
	parser = argparse.ArgumentParser(description='This will assemble Ballad assembly to Ballad bytecode.')
	parser.add_argument('basm', help='The .b file to assemble')
	parser.add_argument('-o', '--obFile', help='The .ob file to output to, default = BASM.ob', nargs='?',
		default=False)
	parser.add_argument('-sm', '--statMem', help='The .smb file to output to, default = BASM.smb', nargs='?',
		default=False)
	args = parser.parse_args()

	baseName = os.path.basename(args.basm).split('.')[0]
	if not args.obFile:
		args.obFile = baseName + '.ob'
	if not args.statMem:
		args.statMem = baseName + '.smb'

	basmFile = open(args.basm, 'r')

	obj, sm = assemble(basmFile.read())
	basmFile.close()
	objFile = open(args.obFile, 'wb')
	objFile.write(obj)
	objFile.close()
	statMemFile = open(args.statMem, 'wb')
	statMemFile.write(sm)
	statMemFile.close()

if __name__ == '__main__':
	main()


#!/usr/bin/env python
### INSTRUCTOR NOTE
# Be sure to specify "python3" above. CoCalc defaults to python2 still.
###

def coord_for(n, a, b):
    h=(b-a)/n
    list_int = []
    for i in range(n+1):
        list_int.append(a + i*h)
    return list_int

### INSTRUCTOR NOTE
# Do not have executable code like this outside of a main block.
print(coord_for(n))
###

def coord_while(n,a,b):
    h=(b-a)/n
    list_int=[]
    i=0
    while i < len(range(n+1)):
        list_int.append(a + i*h)
        i+=1
    return list_int
print(coord_while(n))   ### See above

def coord_comp(n,a,b):
    h=(b-a)/n
    list_int=[3*x for x in range(n+1)]   ### This should be [a + i*h for i in range(n+1)]
    return list_int
print(coord_comp(n))    ### See above


import pandas as Pd
import numpy as np
import matplotlib.pyplot as plt
import xlrd
from scipy import stats, optimize
from Reduction_function import reduce

rho0 = 1.2250
g = 9.81
S = 30.00
C_m_T_c = -0.0064 # From appendix C tables
BEM = 9165    # Basic empty mass, taken from mass report for 2020 [lbs]
c = 2.0569    # MAC, taken from cit_par.py [m]
d = 0.686     # diameter of engine [m], from online source
W_s = 60500   # standard weight [N]

ref = 0
plot = 1
# reading data unto pandas dataframe:
if ref == 1:
    exc_data = 'Post_Flight_Datasheet_Flight_1_DD_12_3_2018.xlsx'
else:
    exc_data = 'C:/Users/nano2598\Documents/actual college ugh/Third year/SVV-FD/Data_repo/20200311_V4.xlsx'

data = Pd.read_excel(exc_data, header=55, usecols='B, D:M', skiprows=[57], nrows=8)

Fe = data['Fe'].iloc[1:].to_numpy(dtype=float)  # Putting pressure alt (hp) values from the dataframe column to np array

hp = data['hp'].iloc[1:].to_numpy(dtype=float)  # Putting pressure alt (hp) values from the dataframe column to np array
V_ias = data['IAS'].iloc[1:].to_numpy(dtype=float)  # Putting V_IAS values from the dataframe column to np array
Tm = data['TAT'].iloc[1:].to_numpy(dtype=float)  # Putting TAT values from the dataframe column to np array

V_e, T, M, Re_range = reduce(hp, V_ias, Tm)
F_used = data['F. used'].iloc[1:].to_numpy(dtype=float)  # Same as what's done above but for fuel used

pax_masses = np.array(Pd.read_excel(exc_data, header=None, usecols='H', skiprows=7, nrows=9))
init_fuel = xlrd.open_workbook(exc_data).sheet_by_index(0).cell_value(17, 3)
ramp_mass = (BEM + init_fuel) * 0.453592 + np.sum(
    pax_masses)  # Calculating total ramp mass, converting some lbs terms to kg
mass = ramp_mass - F_used * 0.453592  # Mass at each data point, also converting to kg

V_e_red = V_e * np.sqrt(W_s / (mass * g))
Fe_red = Fe * (W_s/(mass * 9.81))

coeffs = np.polyfit(V_e_red, Fe_red, 2)

if plot == 1:
    x1 = np.linspace(0, 120, 120)
    y1 = coeffs[0] * x1 ** 2 + coeffs[1] * x1 + coeffs[2]
    xlab = ('Reduced velocity (\u1E7C$_e$) [m/s]')
    ylab = ('Reduced stick force (F$_e^*$) [N]')
    plt.axes(xlim=(60, 105), ylim=(100, -60), xlabel=xlab, ylabel=ylab)
    plt.scatter(V_e_red, Fe_red, s=70, c='r', marker='x', label='Reduced stick force data')
    plt.plot(x1, y1, 'darkturquoise', label='Quadratic least-squares fit')
    plt.grid()
    plt.legend(loc='best')
    plt.savefig('figs/Reduced_Force_Curve')
    plt.show()


import pygame
from pygame.locals import *

def _pixelPerfectCollisionDetection(sp1,sp2):

    rect1 = sp1.rect;
    rect2 = sp2.rect;
    rect  = rect1.clip(rect2)

    x1 = rect.x-rect1.x
    y1 = rect.y-rect1.y
    x2 = rect.x-rect2.x
    y2 = rect.y-rect2.y

    for r in xrange(0,rect.height):
        for c in xrange(0,rect.width):

            if sp1.image.get_at((c+x1, r+y1))[3] & sp2.image.get_at((c+x2, r+y2))[3]:
                return 1

    return 0

def spritecollide_pp(sprite, group, dokill):

    crashed = []
    spritecollide = sprite.rect.colliderect
    ppcollide = _pixelPerfectCollisionDetection
    if dokill:
        for s in group.sprites():
            if spritecollide(s.rect):
                if ppcollide(sprite,s):
                    s.kill()
                    crashed.append(s)
    else:
        for s in group.sprites():
            if spritecollide(s.rect):
                if ppcollide(sprite,s):
                    crashed.append(s)
    return crashed

def groupcollide_pp(groupa, groupb, dokilla, dokillb):

    crashed = {}
    SC = spritecollide_pp
    if dokilla:
        for s in groupa.sprites():
            c = SC(s, groupb, dokillb)
            if c:
                crashed[s] = c
                s.kill()
    else:
        for s in groupa.sprites():
            c = SC(s, groupb, dokillb)
            if c:
                crashed[s] = c
    return crashed

def spritecollideany_pp(sprite, group):

    spritecollide = sprite.rect.colliderect
    ppcollide = _pixelPerfectCollisionDetection
    for s in group.sprites():
        if spritecollide(s.rect):
            if ppcollide(sprite,s):
                return s
    return None


import pymongo
import pymysql
import redis
import json

# mongodb的客户端
client=pymongo.MongoClient(host='127.0.0.1',port=27017)
# redis的客户端
redis_client=redis.StrictRedis(host='192.168.11.68',port=6379,db=0)

sina=client['job']

sina_item=sina['job']
while True:
    source,data=redis_client.blpop(['job:items'])
    print('source===',source)
    print('data===', data)
    data=data.decode('utf-8')
    data=json.loads(data)
    sina_item.insert_one(data)


def fun1(length, a):
    b = a
    for i in range(length):
        a += 1
        print("value of a before: " + str(b) + " now it's " + str(a))
    return a

def main1():
    r1 = fun1(50000, 0)
    r2 = fun1(100, 12)
    r3 = fun1(100, 41)

import asyncio

async def fun2(length, a):
    b = a
    for i in range(length):
        a += 1
        if i % 10000 == 0:
            await asyncio.sleep(0.0001)
        print("value of a before: " + str(b) + " now it's " + str(a))
    return a

async def main2():
    """
    通过async定义一个协程，协程是一个对象，不能直接运行，
    需要把协程加入到事件循环（loop）中，由loop在适当的时候调用协程
    """
    # creating subroutines.
    t1 = loop.create_task(fun2(20000, 0))
    t2 = loop.create_task(fun2(100, 100))
    t3 = loop.create_task(fun2(100, 500))

    await asyncio.wait([t1, t2, t3])

if __name__ == "__main__":
    # main1()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main2())

    loop.close()