# python3 ip-info.py 8.8.8.8

# add alien vault
# add virustotal
"""
AbuseIPDB_API_KEY = key['ABUSEIPDB_API_KEY'] #abuseipdb
apility_key = key['APILITY_API_KEY'] #apility #apility.io
app_ipgeolocation_key = key['APP_IPGEOLOCATION_API_KEY'] #ipgeolocation api.ipgeolocation.io
blacklist_key = key['BLACKLIST_MASTER_API_KEY'] #blacklistmaster #blacklistmaster.com
cisco_investigate_token = key['CISCO_INVESTIGATE_TOKEN'] #cisco investigate
blacklist_username = key['BLACKLIST_MASTER_API_USERNAME'] #blacklistmaster #blacklistmaster.com
"""

#map:

# try and except for each
# assigned empty string for each variables
# one variable for each json object

from datetime import date
from requests import get
from sys import argv
import requests
import socket
import json
import csv
import os

keys_json = json.load(open('ip_config.json', 'r')) # import config file
key = keys_json['keys']
#print(key)

ip = argv[1]

AbuseIPDB_API_KEY = key['ABUSEIPDB_API_KEY']
apility_key = key['APILITY_API_KEY']
app_ipgeolocation_key = key['APP_IPGEOLOCATION_API_KEY']
blacklist_key = key['BLACKLIST_MASTER_API_KEY']
cisco_investigate_token = key['CISCO_INVESTIGATE_TOKEN']
blacklist_username = key['BLACKLIST_MASTER_API_USERNAME']

profile = os.getenv('USERPROFILE')
path_to_file = f"{profile}\desktop\ip.html"
file = open(path_to_file,"w")
file.write(f"

{ip}")
file.write("

Whois
")

def whois(item):
    ''' Apility + ipapi '''
    ''' returns Json '''

    print("Whois\n")

    ''' Apility geo location information '''
    try:
        geo_function = json.loads(json.dumps(returns_json(query_type_geo,item)))
    except:
        print("error with geo_function")

    ''' Apility asn '''
    try:
        version_one_as_json = json.loads(json.dumps(returns_json(version_one_as,item)))
    except:
        print("error with version_one_as_json")

    ''' Apility whois info '''
    try:
        whois_info = json.loads(json.dumps(returns_json(query_type_whois_info,item)))
    except:
        print("error with whois_info")

    ''' IPAPI '''
    try:
        json_info_country = get('https://ipapi.co/%s/country_name/' % item).text
        json_info_region = get('https://ipapi.co/%s/region/' % item).text
        json_info_city = get('https://ipapi.co/%s/city/' % item).text
    except:
        print("error with ipapi")

    ''' api.ipgeolocation.io '''
    try:
        ipgeo = requests.get(f"https://api.ipgeolocation.io/ipgeo?apiKey={app_ipgeolocation_key}&ip={item}")
        json_ipgeo = json.loads(ipgeo.content.decode("utf-8"))
        print(f"\n\tIPGeolocation.io:    Continent::      {json_ipgeo['continent_name']}")
        print(f"\tIPGeolocation.io:    Country::        {json_ipgeo['country_name']}")
        print(f"\tIPGeolocation.io:    Country Code::   {json_ipgeo['country_code2']}")
        print(f"\tIPGeolocation.io:    State, Prov::    {json_ipgeo['state_prov']}")
        print(f"\tIPGeolocation.io:    City::           {json_ipgeo['city']}")
        print(f"\tIPGeolocation.io:    ISP::            {json_ipgeo['isp']}")
        print(f"\tIPGeolocation.io:    Organization::   {json_ipgeo['organization']}")
        file.write(f"
\n\tIPGeolocation.io:    Continent::      {json_ipgeo['continent_name']}\n")
        file.write(f"
\tIPGeolocation.io:    Country::        {json_ipgeo['country_name']}\n")
        file.write(f"
\tIPGeolocation.io:    Country Code::   {json_ipgeo['country_code2']}\n")
        file.write(f"
\tIPGeolocation.io:    State, Prov::    {json_ipgeo['state_prov']}\n")
        file.write(f"
\tIPGeolocation.io:    City::           {json_ipgeo['city']}\n")
        file.write(f"
\tIPGeolocation.io:    ISP::            {json_ipgeo['isp']}\n")
        file.write(f"
\tIPGeolocation.io:    Organization::   {json_ipgeo['organization']}\n")
    except:
        print("error with api.ipgeolocation.io")

    '''Abuse IPDB '''
    ''' returns status and confidence score for a domain or IP '''
    endpoint  = 'https://api.abuseipdb.com/api/v2/check'
    query_string = {
        'ipAddress': ip,
        'maxAgeInDays': '90',
        'verbose': 'yes'
    }

    headers = {
        'Accept': 'application/json',
        'key': AbuseIPDB_API_KEY
    }
    try:
        response = requests.get(url=endpoint, headers=headers, params=query_string)
        abuse_ipdb_json_result = json.loads(response.content)
        print(f"\n\tAbuseIPDB:           Country Name::   {abuse_ipdb_json_result['data']['countryName']}")
        print(f"\tAbuseIPDB:           Country Code::   {abuse_ipdb_json_result['data']['countryCode']}")
        print(f"\tAbuseIPDB:           ISP::            {abuse_ipdb_json_result['data']['isp']}")
        print(f"\tAbuseIPDB:           Domain::         {abuse_ipdb_json_result['data']['domain']}")
        file.write(f"

\tAbuseIPDB:           Country Name::   {abuse_ipdb_json_result['data']['countryName']}")
        file.write(f"
\tAbuseIPDB:           Country Code::   {abuse_ipdb_json_result['data']['countryCode']}")
        file.write(f"
\tAbuseIPDB:           ISP::            {abuse_ipdb_json_result['data']['isp']}")
        file.write(f"
\tAbuseIPDB:           Domain::         {abuse_ipdb_json_result['data']['domain']}")

    except:
        print("error with abuseipdb")
        abuse_ipdb_json_result = "error"

    try:
        print(f"\n\tApility:             Continent Name:: {geo_function['ip']['continent_names']['en']}")
        print(f"\tApility:             Country::        {version_one_as_json['as']['country']}")
        print(f"\tApility:             Time Zone::      {geo_function['ip']['time_zone']}")
        print(f"\tApility:             Network Cidr::   {whois_info['whois']['network']['cidr']}")
        print(f"\tApility:             Networks::       {version_one_as_json['as']['networks']}")
        print(f"\tApility:             AS Name::        {version_one_as_json['as']['name']}")
        print(f"\n\tIpapi:               Country::        {json_info_country}")						# united Stateeprint
        print(f"\tIpapi:               Region::         {json_info_region}")							# 21202
        print(f"\tIpapi:               City::           {json_info_city}")                           # Los Angeles
        file.write(f"

\tApility:             Continent Name:: {geo_function['ip']['continent_names']['en']}")
        file.write(f"
\tApility:             Country::        {version_one_as_json['as']['country']}")
        file.write(f"
\tApility:             Time Zone::      {geo_function['ip']['time_zone']}")
        file.write(f"
\tApility:             Network Cidr::   {whois_info['whois']['network']['cidr']}")
        file.write(f"
\tApility:             Networks::       {version_one_as_json['as']['networks']}")
        file.write(f"
\tApility:             AS Name::        {version_one_as_json['as']['name']}")
        file.write(f"

\tIpapi:               Country::        {json_info_country}")
        file.write(f"
\tIpapi:               Region::         {json_info_region}")
        file.write(f"
\tIpapi:               City::           {json_info_city}")
    except:
        print("Error with Apility and or IPAPI")

    return abuse_ipdb_json_result

def blacklist_multi(item, abuse_ipdb_json_result):

    print("\n\nReputation\n")
    file.write(f"

\tReputation\n")

    ''' Blacklist Master '''
    ''' returns minimal info regarding blacklist status '''
    score = ' '
    try:
        blacklist = get('https://www.blacklistmaster.com/restapi/v0/blacklistcheck/ip/%s' % item, auth=(blacklist_username, blacklist_key)).text
        blacklist_json_format = json.loads(blacklist)
        status = blacklist_json_format["status"]
        if status == 'Not blacklisted':
            count = ' '
        else:
            count = blacklist_json_format["blacklist_cnt"]
        print(str(f"\n\tBlacklistmaster::    {status} {count}"))
        file.write(f"

\tBlacklistmaster::    {status} {count}")
    except:
        print("error with blacklist master")

    ''' printing apility blacklist results '''
    try:
        print(f"\n{blacklist_apility}") # returns 'blacklisted by apility' or 'not blacklisted by apility'
        file.write(f"
\t{blacklist_apility}")
    except:
        print("error printing Apility Blacklist info")

    ''' printing abuseipdb results '''
    try:
        print(f"\n\tAbuseIPDB:     Whitelisted::          {abuse_ipdb_json_result['data']['isWhitelisted']}")
        print(f"\tAbuseIPDB:     Total Reports::        {abuse_ipdb_json_result['data']['totalReports']}")
        print(f"\tAbuseIPDB:     Confidence of Abuse::  {abuse_ipdb_json_result['data']['abuseConfidenceScore']}\n")
        file.write(f"

\tAbuseIPDB:     Whitelisted::          {abuse_ipdb_json_result['data']['isWhitelisted']}")
        file.write(f"
\tAbuseIPDB:     Total Reports::        {abuse_ipdb_json_result['data']['totalReports']}")
        file.write(f"
\tAbuseIPDB:     Confidence of Abuse::  {abuse_ipdb_json_result['data']['abuseConfidenceScore']}\n")
    except:
        print("Error printing AbuseIPDB results")

    ''' Cisco Investigate '''

    headers = {
        'Authorization': 'Bearer ' + cisco_investigate_token
    }
    try:
        investigate_response = requests.get(f'https://investigate.api.umbrella.com/ips/{item}/latest_domains', headers=headers)
        investigate_json = json.loads(investigate_response.content.decode("utf-8"))
        print(f"\n\tCisco Investigate: Latest Malicious Domains Associated with this IP address:\n\t {investigate_json}")
        file.write(f"
\tCisco Investigate: Latest Malicious Domains Associated with this IP address:\n\t {investigate_json}")
    except:
        print("error with cisco investigate")

    try:
        threat_miner = requests.get(f"https://api.threatminer.org/v2/host.php?q={item}&rt=4")
        print(f"\tThreat Miner: Related Samples::\t {threat_miner.content}")
        file.write(f"
\tThreat Miner: Related Samples::\t {threat_miner.content}")
    except:
        print("error with threatminer")
        print(f"Threat Miner response code {threat_miner.status_code}")
        file.write("Error with Threat Miner")

########################################################################################
# Apility below

# Apility scan type variables
blacklist_checker = 'badip'             #  blacklist
query_type_whois_info = 'whois/ip'      #  whois
query_type_geo = 'geoip'                #  geo info
version_one_as = 'as/ip'                #  asn info

############### functions for all Apility API calls -BENEATH THIS LINE- ########################

def blacklist(query_type,input):
    ''' checks if IP is found in Apility's blacklist '''
    ''' returns 404 error if it was not found in a blacklist '''
    ''' returns 200 OK if it was found in blacklist '''
    url = 'https://api.apility.net/%s/%s' % (query_type,input)
    headers = {'X-Auth-Token': apility_key}
    try:
        http_response_code = str(requests.get(url, headers=headers))
        if http_response_code == '<Response [404]>':
            http_response_code = "\tBlacklist Apility:: Not Blacklisted"
            return http_response_code
        else:
            http_response_code = "\tBlacklist Apility:: Blacklisted"
            return http_response_code
    except:
        print("error with Apility Blacklist")

def returns_json(query_type,input):
    ''' checks multiple different types of calls and returns json '''
    call = requests.get('https://api.apility.net/%s/%s' % (query_type,input), headers={
        "Accept": "application/json",
        "X-Auth-Token": apility_key
    })
    return call.json()

############### functions for all Apility API calls -ABOVE THIS LINE- ########################

''' Apility checks blacklist and returns http status code '''
blacklist_apility = str(blacklist(blacklist_checker,ip))

print("\n%s\n" % ip)
whois_info = whois(ip) # line[0] = IP

blacklist_multi(ip, whois_info)

os.system(f"powershell.exe start-process chrome {path_to_file}")


emails = ("a@example.com", "b@example.com")
message = {
        'emails': emails,
        'subject': "You Have Mail!",
        'message': "Here's some mail for you!"
        }
template = """
From: <{0[emails][0]}>
To: <{0[emails][1]}>
Subject: {0[subject]}
{0[message]}"""
print(template.format(message))


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

from Symbol import *
from Sym import *

class NoteHead(Symbol):
    def __init__(self, s):
        Symbol.__init__(self, s)

    def type(self):
        return ElementType.NOTEHEAD

noteHeads = (
      (
      ( SymName.wholeheadSym,         SymName.halfheadSym,         SymName.quartheadSym,      SymName.brevisheadSym        ),
      ( SymName.wholecrossedheadSym,  SymName.halfcrossedheadSym,  SymName.crossedheadSym,    SymName.wholecrossedheadSym  ),
      ( SymName.wholediamondheadSym,  SymName.halfdiamondheadSym,  SymName.diamondheadSym,    SymName.wholediamondheadSym  ),
      ( SymName.s0triangleHeadSym,    SymName.d1triangleHeadSym,   SymName.d2triangleHeadSym, SymName.s0triangleHeadSym    ),
      ( SymName.s0miHeadSym,          SymName.s1miHeadSym,         SymName.s2miHeadSym,       -1                   ),
      ( SymName.wholeslashheadSym,    SymName.halfslashheadSym,    SymName.quartslashheadSym, SymName.wholeslashheadSym    ),
      ( SymName.xcircledheadSym,      SymName.xcircledheadSym,     SymName.xcircledheadSym,   SymName.xcircledheadSym      ),
      ( SymName.s0doHeadSym,          SymName.d1doHeadSym,         SymName.d2doHeadSym,       -1                   ),
      ( SymName.s0reHeadSym,          SymName.d1reHeadSym,         SymName.d2reHeadSym,       -1                   ),
      ( SymName.d0faHeadSym,          SymName.d1faHeadSym,         SymName.d2faHeadSym,       -1                   ),
      ( SymName.s0laHeadSym,          SymName.s1laHeadSym,         SymName.s2laHeadSym,       -1                   ),
      ( SymName.s0tiHeadSym,          SymName.d1tiHeadSym,         SymName.d2tiHeadSym,       -1                   ),
      ( SymName.s0solHeadSym,         SymName.s1solHeadSym,        SymName.s2solHeadSym,      -1                   ),
      ( SymName.wholeheadSym,         SymName.halfheadSym,         SymName.quartheadSym,      SymName.brevisheadaltSym     ),
      ),
      (
      ( SymName.wholeheadSym,         SymName.halfheadSym,         SymName.quartheadSym,      SymName.brevisheadSym        ),
      ( SymName.wholecrossedheadSym,  SymName.halfcrossedheadSym,  SymName.crossedheadSym,    SymName.wholecrossedheadSym  ),
      ( SymName.wholediamondheadSym,  SymName.halfdiamondheadSym,  SymName.diamondheadSym,    SymName.wholediamondheadSym  ),
      ( SymName.s0triangleHeadSym,    SymName.u1triangleHeadSym,   SymName.u2triangleHeadSym, SymName.s0triangleHeadSym    ),
      ( SymName.s0miHeadSym,          SymName.s1miHeadSym,         SymName.s2miHeadSym,       -1                   ),
      ( SymName.wholeslashheadSym,    SymName.halfslashheadSym,    SymName.quartslashheadSym, SymName.wholeslashheadSym    ),
      ( SymName.xcircledheadSym,      SymName.xcircledheadSym,     SymName.xcircledheadSym,   SymName.xcircledheadSym      ),
      ( SymName.s0doHeadSym,          SymName.u1doHeadSym,         SymName.u2doHeadSym,       -1                   ),
      ( SymName.s0reHeadSym,          SymName.u1reHeadSym,         SymName.u2reHeadSym,       -1                   ),
      ( SymName.u0faHeadSym,          SymName.u1faHeadSym,         SymName.u2faHeadSym,       -1                   ),
      ( SymName.s0laHeadSym,          SymName.s1laHeadSym,         SymName.s2laHeadSym,       -1                   ),
      ( SymName.s0tiHeadSym,          SymName.u1tiHeadSym,         SymName.u2tiHeadSym,       -1                   ),
      ( SymName.s0solHeadSym,         SymName.s1solHeadSym,        SymName.s2solHeadSym,      -1                   ),
      ( SymName.wholeheadSym,         SymName.halfheadSym,         SymName.quartheadSym,      SymName.brevisheadaltSym     ),
      )
      )

class Note(Element):
    def __init__(self, s):
        Element.__init__(self, s)
        self.dragMode           = False
        self._pitch             = 0
        self._ppitch            = 0
        self._tuning            = 0.0
        self._userAccidental    = AccidentalType.ACC_NONE
        self._accidental        = 0
        self._mirror            = False
        self._userMirror        = DirectionH.DH_AUTO
        self._line              = 0
        self._lineOffset        = 0
        self._tieFor            = 0
        self._tieBack           = 0
        self._tpc               = -1
        self._headGroup         = 0
        self._headType          = NoteHeadType.HEAD_AUTO

        self._hidden            = False
        self._subchannel        = 0

        self._veloType          = ValueType.AUTO_VAL
        self._velocity          = 80
        self._veloOffset        = 0

        self._onTimeType        = ValueType.AUTO_VAL
        self._onTimeOffset      = 0
        self._onTimeUserOffset  = 0

        self._offTimeType       = ValueType.AUTO_VAL
        self._offTimeOffset     = 0
        self._offTimeUserOffset = 0

    def type(self):
        return ElementType.NOTE

class ShadowNote(Element):
    def __init__(self, s):
        Element.__init__(self, s)
        self._line = 1000
        self._headGroup = 0
        self._head      = 2

    def type(self):
        return ElementType.SHADOW_NOTE

    def line(self):
        return self._line

    def setLine(self, n):
        self._line = n

    def headGroup(self):
        return self._headGroup

    def setHead(self, val):
        self._head = val

    def head(self):
        return self._head


import logging
from logging.handlers import TimedRotatingFileHandler

from src.logic.pathes import log_path

formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

file_handler = TimedRotatingFileHandler(log_path, when="midnight", interval=1)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)

# add a suffix which you want
file_handler.suffix = "%Y-%m-%d"

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)

# finally add handler to logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)


import datetime
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
from .models import Action

def create_action(user, verb, target=None):
    """创建一个动作"""
    now = timezone.now()
    last_minute = now - datetime.timedelta(seconds=60)
    similar_actions = Action.objects.filter(user_id=user.id,
                                            verb=verb,
                                            created__gte=last_minute)  # 创建时间大于一分钟前的所有实例
    if target:
        target_ct = ContentType.objects.get_for_model(target)  # 如果目标存在 获取目标实例
        similar_actions = similar_actions.filter(target_ct=target_ct,
                                                 target_id=target.id)  # 在里面检测这个操作是不是在一分钟前
    if not similar_actions:
        action = Action(user=user, verb=verb, target=target)  # 如果不是相似操作 保存实例
        action.save()
        return True
    return False


import io
import random
import re
import string
import warnings
import nltk
import wikipedia
import numpy as np
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from speech.tts import speak
from listen.stt import to_text

warnings.filterwarnings('ignore')

nltk.download('popular', quiet=True)

# uncomment the following only the first time
# nltk.download('punkt') # first-time use only
# nltk.download('wordnet') # first-time use only

def define(subject, cli):
	words = subject.split()
	words.remove("define")
	subject = " ".join(words)

	try:
		wiki_data = wikipedia.summary(subject, sentences = 5)

		regex = re.compile(r"([^\(]*)\([^\)]*\) *(.*)")
		m = regex.match(wiki_data)
		while m:
			wiki_data = m.group(1) + m.group(2)
			m = regex.match(wiki_data)

		wiki_data = wiki_data.replace("'", "")
		print("Defination of " + subject + ":\n" + wiki_data)
		speak(wiki_data)
		print("Do you have any more question? (y/N)")
		speak("Do you have any more question?")
		if cli == "true":
			reply = input().lower()
		else:
			reply = to_text()
		set(["yes"]).issubset(set(reply.split()))
		if set(["yes"]).issubset(set(reply.split())) or set(["sure"]).issubset(set(reply.split())) or reply == "y":
			init_chat(subject, cli)

	except wikipedia.exceptions.DisambiguationError as e:
		print("Can you please be more specific? You may choose from the following: {0}".format(e))
		speak("Can you please be more specific?")

def init_chat(subject, cli):

	raw = wikipedia.page(subject).content.replace("'", "").replace("\n", " ").replace("=", "")

	#TOkenisation
	sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences
	word_tokens = nltk.word_tokenize(raw)# converts to list of words

	# Preprocessing
	lemmer = WordNetLemmatizer()
	def LemTokens(tokens):
		return [lemmer.lemmatize(token) for token in tokens]
	remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
	def LemNormalize(text):
		return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))

	# Keyword Matching
	GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up","hey",)
	GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]

	def greeting(sentence):
		"""If user's input is a greeting, return a greeting response"""
		for word in sentence.split():
			if word.lower() in GREETING_INPUTS:
				return random.choice(GREETING_RESPONSES)

	# Generating response
	def response(user_response):
		chat_response=''
		sent_tokens.append(user_response)
		TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
		tfidf = TfidfVec.fit_transform(sent_tokens)
		vals = cosine_similarity(tfidf[-1], tfidf)
		idx=vals.argsort()[0][-2]
		flat = vals.flatten()
		flat.sort()
		req_tfidf = flat[-2]
		if(req_tfidf==0):
			chat_response=chat_response+"Sorry! I don't understand you"
			return chat_response
		else:
			chat_response = chat_response+sent_tokens[idx]
			return chat_response

	def chat(subject):
		flag=True
		print("> What more queries do you have about " + subject + "?. If you want to exit, type Bye!")
		speak("What more queries do you have about " + subject + "?. If you want to exit, say Bye!")

		while(flag==True):
			if cli == "true":
				user_response = input()
				user_response=user_response.lower()
			else:
				user_response = to_text()

			if(user_response!='bye'):
				if(user_response=='thanks' or user_response=='thank you' ):
					flag=False
					print("> You are welcome..")
					speak("You are welcome.")
				else:
					if(greeting(user_response)!=None):
						reply_text = greeting(user_response)
						print("> " + reply_text)
						speak(reply_text)
					else:
						reply_text = response(user_response)
						print("> ",end="")
						print(reply_text)
						speak(reply_text)
						sent_tokens.remove(user_response)
			else:
				flag=False
				print("> Bye! take care..")
				speak("Bye! take care.")
	chat(subject)


from utils.ListNode import ListNode

def remove_nth_node_end_lost_list(head: ListNode, n: int) -> ListNode | None:
    list_size = count_linked_list_size(head)
    # Handle special case where the element to remove is the head
    if list_size == n:
        return head.next
    # Go to the node before the node to remove. Set its next to the node to remove's next
    to_remove_predecessor_index = list_size - n - 1
    current = head
    for i in range(to_remove_predecessor_index):
        current = current.next
    current.next = current.next.next
    return head

def count_linked_list_size(head):
    list_size = 1
    current = head
    while current.next is not None:
        list_size += 1
        current = current.next
    return list_size

def access_nth_node(head, n):
    current = head
    for i in range(n):
        current = current.next
    return current


#so far just some lines for you to use in the interactive console

from BeautifulSoup import BeautifulSoup

f1 = open('/home/leon/doc/class/wuqi-2012-07-08-clean.html')

soup = BeautifulSoup(f1)

clickable = soup.findAll('span', attrs={'class':'clickable'})

removed = [c.extract() for c in clickable]

#below two lines are used for the whole chat record that is exported (instead of the chat record that is of the current day)
msgHead = soup.findAll('dt', attrs={'class':'msgHead'})
replaced = [c.replaceWith('Ñ§Éú£º') for c in msgHead if 'Leon' not in c.text]

imgs = soup.findAll('img')

imgs_removed = [img.extract() for img in imgs]

f2 = open('/home/leon/doc/class/wuqi-2012-07-08-cleaned.html', 'w')

f2.write(str(soup))

f1.close()

f2.close()


T = int(input())
for tc in range(1, T+1):
    N = int(input())
    arr = list(map(int, input().split()))
    maxV = 0 # testcase 바뀔 때마다 초기화
    for i in range(N): # 기준
        cnt = 0 # arr[i] 오른쪽 원소 중 더 작은 원소의 개수
        for j in range(i + 1, N):
            if arr[i] > arr[j]:
                cnt += 1
        # cnt 중 최대값
        if maxV < cnt:
            maxV = cnt
    print(f'#{tc} {maxV}')
######################################################
# TC = int(input())
# for T in range(1, TC+1): # testcase 마다
#     N = int(input()) # 가로의 길이
#     arr = list(map(int, input().split())) # 각 블럭의 높이
#     Max = 0 # 오른쪽에 비어 있는 블럭 개수가 가장 많은 경우 찾기
#     for i in range(N): # 각 블럭 마다
#         cnt = 0  # 기준 블럭 바뀔 때마다 리셋
#         for j in range(i+1, N): # 각 블럭의 오른쪽에 있는 비어 있는 블럭 개수 구하기
#             if arr[i] > arr[j]:
#                 cnt += 1
#         if cnt > Max:
#             Max = cnt
#     print(f'#{T} {Max}')


def solution(triangle):
    # 정답을 담을 리스트 초기화
    answers = triangle.pop()

    # 트라이앵글에 원소가 있을 때 까지 반복
    while(triangle):
        # 트라이앵글을 밑에서부터 한 줄 pop
        row = triangle.pop()
        temp = []
        # row 길이만큼 반복
        for i in range(len(row)):
            # answers가 밑에 줄, row가 위에 줄
            # 밑에 줄에서 위에 줄로 가는 경로는 row 각 요소마다 2개씩
            # row 각 요소에 가는 경로 2가지 중 최대값을 저장
            temp.append(max(row[i] + answers[i], row[i] + answers[i+1]))
        answers = temp  # 결과를 answers로
    return answers[0]

# test
print(solution([[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]]))


# text = ''
#
# while text != 'Stop':
#     text = input()
#     if text == 'Stop':
#         break
#     print(text)

#Judge approved. By writing text = '', you say text = string, but do NOT input anything. So first value is the input after the
#while loop. This is how you avoid it not reading the first problem
#Judge approved

#Why do we avoid the issue doing it this way?

while True:
    command = input()

    if command =='Stop':
        break

    print(command)

PythonBasics/9.While Loop - Lab/1_read_text.py


__all__ = [
    "ProcessorModel",
    "get_proc_repo",
    "ProcessorRepo",
]

from dataclasses import dataclass
from typing import List, Optional

@dataclass
class ProcessorModel:
    name: str
    cores: int
    threads: int

global_processors: List[ProcessorModel] = []

class ProcessorRepo:
    """
    The source of data for services, methods are used only by services.
    At this layer, class doesn't know about facades and only does work related to data.
    CRUD queries, filtering, deletion are made here.
    """

    def add_new_proc(self, name: str, cores: int, threads: int) -> ProcessorModel:
        proc = ProcessorModel(
            name=name,
            cores=cores,
            threads=threads,
        )
        global_processors.append(proc)
        return proc

    def filter_by_cores(self, min_cores: int, max_cores: int) -> Optional[List[ProcessorModel]]:
        res = []
        for i in global_processors:
            if min_cores <= i.cores <= max_cores:
                res.append(i)
        return res

    def filter_by_threads(self, min_thr: int, max_thr: int) -> Optional[List[ProcessorModel]]:
        res = []
        for i in global_processors:
            if min_thr <= i.threads <= max_thr:
                res.append(i)
        return res

    def get_all(self) -> List[ProcessorModel]:
        return global_processors

def get_proc_repo() -> ProcessorRepo:
    return ProcessorRepo()


import pygame
import sys
from spritesheets import Spritesheet
from random import randint
import _random
import random
from button import Button
from pygame.locals import (
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    K_w,
    K_s,
    QUIT,
)
fontt=pygame.font.SysFont(r"new\font\CANDY___.otf",90)

count=0
SCREEN_WIDTH=800
SCREEN_HEIGHT=600

size=SCREEN_WIDTH,SCREEN_HEIGHT
pygame.mixer.init()
pygame.init()

time=pygame.time.Clock()
background_sound=pygame.mixer.Sound(r"new/sounds/background.mp3")
#ppaddle_color=pygame.mixer.Sound(r"new/sounds/paddle.m4a")
#brick_sound=pygame.mixer.Sound(r"new/sounds/brick.m4a")
#wall_sound=pygame.mixer.Sound(r"new/sounds/wall.m4a")
hit_sound=pygame.mixer.Sound(r"new/sounds/hit.mp3")
fps=30
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Breakout")

my_spritesheet=Spritesheet(r"new\img\Sprite Sheet\Breakout_Tile_Free.png")

blue=my_spritesheet.get_sprite(772,390,384,128)
blue=pygame.transform.scale(blue,(80,30))

red=my_spritesheet.get_sprite(772 ,260 ,384 ,128)
red=pygame.transform.scale(red,(80,30))

green=my_spritesheet.get_sprite(0 ,130,384,128)
green=pygame.transform.scale(green,(80,30))

blue_1=my_spritesheet.get_sprite(0 ,0 ,384,128)
blue_1=pygame.transform.scale(blue_1,(80,30))

red_1=my_spritesheet.get_sprite(772 ,130 ,384 ,128)
red_1=pygame.transform.scale(red_1,(80,30))

green_1=my_spritesheet.get_sprite(0 ,260 ,384,128)
green_1=pygame.transform.scale(green_1,(80,30))

block_color=[red,blue,green]
block_color1=[red_1,blue_1,green_1]

sound_on=True

def sound_check():
    global sound_on
    if sound_on==True:
        sound=pygame.image.load(r"new/img/sound.png")
        sound=pygame.transform.scale(sound,(30,30))
    if sound_on== False:
        sound=pygame.image.load(r"new/img/mute.png")
        sound=pygame.transform.scale(sound,(30,30))
    #sound_rect=sound.get_rect(center=(600,400))
    return sound

BALL_RECT= int(10*2**0.5)
def collision(dx,dy,ball,rect):
    if dx>0:
        delta_x=ball.right-rect.left
    else:
        delta_x=rect.right-ball.left
    if dy>0:
        delta_y=ball.bottom-rect.top
    else:
        delta_y=rect.bottom-ball.top

    if abs(delta_x-delta_y)<10:
        dx*=-1
        dy*=-1
    else:
        if delta_x<delta_y:
            dx*=-1
        else:
            dy*=-1
    return dx,dy
class Paddle(pygame.sprite.Sprite):
    def __init__(self):
        self.surf=pygame.Surface((120,10))
        self.surf.fill("white")
        self.rect=self.surf.get_rect()
        self.rect.x=400-60
        self.rect.y=600-20

    def update(self,key_pressed):
        if key_pressed[K_RIGHT] and self.rect.right<800:
            self.rect.right+=10
        if key_pressed[K_LEFT] and self.rect.left>0:
            self.rect.left-=10
class Block(pygame.sprite.Sprite):
    def __init__(self,i,j):
        global block_color,block_color1

        self.hit=randint(0,1)
        self.color=randint(0,2)
        if self.hit==0:
         self.surf=block_color[self.color]
        else:
            self.surf=block_color1[self.color]
        self.rect=self.surf.get_rect()
        self.rect.x=10+90*i
        self.rect.y=10+65*j
    def update(self):
        global block_color,block_color1
        if self.hit==0:
            self.hit=1
        if self.hit==1:
            self.surf=block_color1[self.color]
            self.hit==0
class Ball(pygame.sprite.Sprite):
    def __init__(self):
        global BALL_RECT
        self.surf=pygame.Surface((BALL_RECT,BALL_RECT))
        self.surf.fill("white")
        self.rect=self.surf.get_rect()
        self.rect.x=randint(BALL_RECT,800-BALL_RECT)
        self.rect.y=300
        self.dx=1
        self.dy=-1
    def update(self):
        self.rect.x+=5*self.dx
        self.rect.y+=5*self.dy

def win_check(win):
    if win==True:
        screen.fill("black")
        text=fontt.render("YOU WIN !",True,"white")
        screen.blit(text,(300,300))

    else:
        screen.fill("black")
        text=fontt.render("YOU LOSE !",True,"white")
        screen.blit(text,(300,300))

    pygame.display.flip()
    pygame.time.wait(2000)
    menu()

def play():
    global ppaddle_color,wall_sound,brick_sound,hit_sound
    my_spritesheet=Spritesheet(r"new\img\Sprite Sheet\Breakout_Tile_Free.png")
    score=0
    paddle_image=my_spritesheet.get_sprite(1158,396,243,64)
    paddle_image=pygame.transform.scale(paddle_image,(120,10))
    paddle=Paddle()
    ball_image=my_spritesheet.get_sprite(1403,652,64,64)

    ball_image=pygame.transform.scale(ball_image,(BALL_RECT,BALL_RECT))

    ball=Ball()
    block_list=[Block(i,j) for i in range(9) for j in range(4)]

    while True:

        screen.fill("black")
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()

        key=pygame.key.get_pressed()
        paddle.update(key)
        ball.update()

        if ball.rect.centerx>800-BALL_RECT or ball.rect.centerx<BALL_RECT:
            ball.dx*=-1
            hit_sound.play()
        if ball.rect.y<BALL_RECT and ball.dy<0:
            ball.dy*=-1
            hit_sound.play()

        if ball.rect.colliderect(paddle.rect) and ball.dy>0:
            ball.dx,ball.dy=collision(ball.dx,ball.dy,ball.rect,paddle.rect)
            hit_sound.play()
        screen.blit(paddle_image,paddle.rect)
        screen.blit(ball_image,ball.rect)
        if ball.rect.centery>600-BALL_RECT:
            win_check(False)
        for b in block_list:
            #b=block_rect[i]
            screen.blit(b.surf,b.rect)
    # [screen.blit(bb[1],b[1]) for b in enumerate(block_rect) for bb in enumerate(block_list)]

        hit_index=ball.rect.collidelist(block_list)
        if hit_index!=-1:
            hit_sound.play()
            hit_block=block_list[hit_index]
            hit_rect=hit_block.rect
            hit=hit_block.hit
            if hit==0:
                hit_block.update()

            else:
                block_list.pop(hit_index)
                score+=1

            ball.dx,ball.dy=collision(ball.dx,ball.dy,ball.rect,hit_rect)
            if score==36:
                win_check(True)
            global fps
            fps+=2
        font1=pygame.font.SysFont(r"new\font\CANDY___.otf",30)
        textt=font1.render("score={} ".format(score),True,"white")
        screen.blit(textt,(700,550))
        pygame.display.flip()
        time.tick(fps)

def menu():
    global background_sound
    global sound_on
    screen.fill("black")
    running =True
    if sound_on:
        background_sound.play(loops=-1)
    while running:

        MENU_MOUSE_POS=pygame.mouse.get_pos()
        menu_text=fontt.render("MAIN MENU",True,"white")
        menu_rect=menu_text.get_rect(center=(400,150))
        screen.blit(menu_text,menu_rect)

        PLAY_BUTTON=Button(image=None,pos=(400,300),text_input="PLAY",font=fontt,base_colour="white",hovering_colour="red")
        QUIT_BUTTON=Button(image=None,pos=(400,400),text_input="QUIT",font=fontt,base_colour="white",hovering_colour="red")
        SOUND_BUTTON=Button(image=sound_check(),pos=(700,500),text_input="",font=fontt,base_colour="white",hovering_colour="white")
        for button in PLAY_BUTTON,QUIT_BUTTON,SOUND_BUTTON:
            button.changeColor(MENU_MOUSE_POS)
            button.update(screen)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type==pygame.MOUSEBUTTONDOWN:
                if PLAY_BUTTON.checkForInput(MENU_MOUSE_POS):
                    background_sound.stop()
                    play()
                if SOUND_BUTTON.checkForInput(MENU_MOUSE_POS):
                    black_image=pygame.image.load(r"new/img/black.png")
                    black_image=pygame.transform.scale(black_image,(30,30))
                    screen.blit(black_image,SOUND_BUTTON.rect)
                    if sound_on:
                        sound_on=False
                        background_sound.stop()
                    else:
                        sound_on=True
                        background_sound.play()

                if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS):
                    pygame.quit()
                    sys.exit()

        pygame.display.flip()
        global fps
        time.tick(fps)
menu()


import pygame as pg
from src.common.map_loader import load_map
from src.surface.surfaces import Terrain
from src.common.common_objects import StaticBlock
from src.common.config import TITLE_WIDTH

class Map:
    def __init__(self, game):
        self.terrain = Terrain()
        self.game = game
        map_name = "title.json"
        layers_data = load_map(map_name)
        self.layers = []
        self.body_shape = []
        for layer in layers_data:
            if layer["name"] in ["Terrain"]:
                group = pg.sprite.Group()
                for idx, tile in enumerate(layer["data"]):
                    if tile != 0:
                        group.add(StaticBlock(self.convert_tile_to_surface(tile), idx, TITLE_WIDTH))
                self.layers.append(group)

    def convert_tile_to_surface(self, tile_id):
        if 1 <= int(tile_id) <= 242:
            return self.terrain.get_tile(tile_id)
        else:
            raise Exception("invalid tile_id")
# class Field(BaseSprite):
#     def __init__(self, game):
#         super().__init__(game)
#         self.surface = FieldSurface()
#
#     def get_rect(self):
#         return self.surface.get_surface().get_rect(topleft=(0, 0))
#
#
# class PressKeyText(BaseSprite):
#     def __init__(self, game):
#         super().__init__(game)
#         self.surface = TextSurface()
#         self.surface.set_text("Press any key to start")
#
#     def get_rect(self):
#         return self.surface.get_surface().get_rect(midtop=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#
#
# class ControlGuideLeft(BaseSprite):
#     def __init__(self, game):
#         super().__init__(game)
#         self.surface = TextSurface(30)
#         self.surface.set_text("Left controls: Move(W, A, S, D), Switch(Q, E), Shoot(SPACE)")
#
#     def get_rect(self):
#         return self.surface.get_surface().get_rect(topleft=(100, 100))
#
#
# class ControlGuideRight(BaseSprite):
#     def __init__(self, game):
#         super().__init__(game)
#         self.surface = TextSurface(30)
#         self.surface.set_text("Right controls: Move(U, D, L, R), Switch(/, R_SHIFT), Shoot(ENTER)")
#
#     def get_rect(self):
#         return self.surface.get_surface().get_rect(bottomleft=(100, 160))
#
#
# class Score(BaseSprite):
#     def __init__(self, game):
#         super().__init__(game)
#         self.team0 = 0
#         self.team1 = 0
#         self.surface = TextSurface()
#         self.surface.set_text("")
#
#     def update_score(self, team0=None, team1=None):
#         if team0 is not None:
#             self.team0 = team0
#         if team1 is not None:
#             self.team1 = team1
#         self.surface.set_text("Your final score: " + str(self.team0) + " | " + str(self.team1))
#
#     def get_rect(self):
#         return self.surface.get_surface().get_rect(midtop=(SCREEN_WIDTH/2, 100))


from datetime import datetime, timedelta
from app.logic.services.base_service import BaseService
from app.persistence.repositories.event_repository import EventRepository
from app.persistence.repositories.ceremony_repository import CeremonyRepository
from app.logic.model_mappers import EventModelMapper, CeremonyModelMapper
from app.logic.models import Event, Ceremony
from app.utils.exceptions import NotFoundError

class EventService(BaseService):

    def get_events(self)->list[Event]:
        event_entities = EventRepository(self.session).get_events()
        return [EventModelMapper().map_to_event_model(event_entity) for event_entity in event_entities]

    def get_event(self, id: int = None, year: int = None)->Event:
        event_entity = EventRepository(self.session).get_event(id, year)

        if event_entity is None:
            raise NotFoundError(field="id",message=f"Event with id {id} not found")

        return EventModelMapper().map_to_event_model(event_entity)

    def create_event_and_associated_ceremonies(self, event: Event, grand_final_date: datetime)->Event:
        event_entity = EventModelMapper().map_to_event_entity(event)
        created_event_entity = EventRepository(self.session).create_event(event_entity)
        created_event_model = EventModelMapper().map_to_event_model_without_submodels(created_event_entity)
        self.associate_ceremonies_to_event(created_event_model, grand_final_date)

        return created_event_model

    def associate_ceremonies_to_event(self, event: Event, grand_final_date: datetime):
        semifinal1_date = grand_final_date - timedelta(days=4)
        semifinal2_date = grand_final_date - timedelta(days=2)

        first_semifinal_ceremony_type = (CeremonyModelMapper()
                                        .map_to_ceremony_type_model(CeremonyRepository(self.session).get_ceremony_type(code='SF1')))

        second_semifinal_ceremony_type = (CeremonyModelMapper()
                                        .map_to_ceremony_type_model(CeremonyRepository(self.session).get_ceremony_type(code='SF2')))

        grand_final_ceremony_type = (CeremonyModelMapper()
                                        .map_to_ceremony_type_model(CeremonyRepository(self.session).get_ceremony_type(code='GF')))

        first_semifinal_ceremony = Ceremony(date=semifinal1_date, ceremony_type=first_semifinal_ceremony_type, event=event)
        second_semifinal_ceremony = Ceremony(date=semifinal2_date, ceremony_type=second_semifinal_ceremony_type, event=event)
        grand_final_ceremmony = Ceremony(date=grand_final_date, ceremony_type=grand_final_ceremony_type, event=event)

        CeremonyRepository(self.session).create_ceremony(CeremonyModelMapper().map_to_ceremony_entity(first_semifinal_ceremony))
        CeremonyRepository(self.session).create_ceremony(CeremonyModelMapper().map_to_ceremony_entity(second_semifinal_ceremony))
        CeremonyRepository(self.session).create_ceremony(CeremonyModelMapper().map_to_ceremony_entity(grand_final_ceremmony))

    def update_event(self, event_id: int, event: Event):
        self.get_event(id=event_id)
        updated_event_entity = EventModelMapper().map_to_event_entity(event=event)
        updated_event_entity.id = event_id
        EventRepository(self.session).update_event(event=updated_event_entity)


"""All classes of my game."""

from random import randrange
import pygame
from pygame import sprite
from pygame.locals import *
from constant import LENGHT_SPRITE, NB_OF_CASE

class Labyrinth:

    """class for creat lab"""

    def __init__(self, lab):
        """Initialized the labyrinth"""
        self.lab = lab
        self.structure = 0

    def load_lab(self):
        """load the labyrinth in file .txt"""
        with open(self.lab, "r") as lab:
            structure_level = []
            for line in lab:
                line_level = []
                for sprite in line:
                    if sprite != "\n":
                        line_level.append(sprite)
                structure_level.append(line_level)
            self.structure = structure_level

    def display_lab(self, window):
        """ display the labyrinth with the guardian
            and display the wall"""
        wall = pygame.image.load("picture/wall.jpg").convert()
        guardian = pygame.image.load("picture/guardian.jpg").convert()

        count_line = 0
        for line in self.structure:
            num_case = 0
            for yolo in line:
                x_in_x = num_case * LENGHT_SPRITE
                y_in_y = count_line * LENGHT_SPRITE
                if yolo == '+':
                    window.blit(wall, (x_in_x, y_in_y))
                elif yolo == 'O':
                    window.blit(guardian, (x_in_x, y_in_y))
                num_case += 1
            count_line += 1

class Perso:

    """Create perso"""

    def __init__(self, picture, level):
        """Initialized the characer"""
        self.picture = pygame.image.load(picture).convert()
        self.x_x = 0
        self.y_y = 0
        self.case_x = 0
        self.case_y = 0
        self.level = level

    def move(self, direction):
        """move the character in the good direction sprite by sprite"""
        if direction == 'right':
            if self.case_x < NB_OF_CASE - 1:
                if self.level.structure[self.case_y][self.case_x+1] != "+":
                    self.case_x += 1
                    self.x_x = self.case_x * LENGHT_SPRITE

        if direction == 'left':
            if self.case_x > 0:
                if self.level.structure[self.case_y][self.case_x-1] != "+":
                    self.case_x -= 1
                    self.x_x = self.case_x * LENGHT_SPRITE

        if direction == 'down':
            if self.case_y < NB_OF_CASE - 1:
                if self.level.structure[self.case_y+1][self.case_x] != "+":
                    self.case_y += 1
                    self.y_y = self.case_y * LENGHT_SPRITE

        if direction == 'up':
            if self.case_y > 0:
                if self.level.structure[self.case_y-1][self.case_x] != "+":
                    self.case_y -= 1
                    self.y_y = self.case_y * LENGHT_SPRITE

class Tresor:

    """Create a tresor"""

    def __init__(self, picture, level):
        """Initialized a part of the seringue"""
        self.picture = pygame.image.load(picture).convert_alpha()
        self.level = level
        self.x_for_x = 0
        self.y_for_y = 0
        self.case_x = 0
        self.case_y = 0

    def display_tresor(self, window):
        """display the tresor somewhere in the labyrinth"""
        i = 0
        limited = (len(self.level.structure)-1)*LENGHT_SPRITE
        self.x_for_x = randrange(0, limited, LENGHT_SPRITE)
        self.y_for_y = randrange(0, limited, LENGHT_SPRITE)
        self.case_x = int(self.x_for_x / LENGHT_SPRITE)
        self.case_y = int(self.y_for_y / LENGHT_SPRITE)
        while i == 0:
            if self.level.structure[self.case_y][self.case_x] != "x":
                self.x_for_x = randrange(0, limited, LENGHT_SPRITE)
                self.y_for_y = randrange(0, limited, LENGHT_SPRITE)
                self.case_x = int(self.x_for_x / LENGHT_SPRITE)
                self.case_y = int(self.y_for_y / LENGHT_SPRITE)
            elif self.level.structure[self.case_y][self.case_x] == "x":
                window.blit(self.picture, (self.x_for_x, self.y_for_y))
                i += 1


t = int(input())
for _ in range(t):
    n = int(input())
    c = 10
    res = []
    while n > 0:
        if n % 10 != 0:
            x = n % 10
            res.append(x * (c//10))
        c *= 10
        n //= 10
    print(len(res))
    print(*res)


from pipeline.module import DailyLoopDataPortalModule
import numpy as np

class DataManagerAdjPrice(DailyLoopDataPortalModule):
    def initialize(self):
        self.start_date = self.context.start_date
        self.backdays = int(self.context.fetch_constant('backdays'))
        self.start_di = self.context.date_idx(self.start_date)
        self.rawopen = self.context.fetch_data('open')
        self.rawhigh = self.context.fetch_data('high')
        self.rawlow = self.context.fetch_data('low')
        self.rawclose = self.context.fetch_data('close')
        self.rawvwap = self.context.fetch_data('vwap')
        self.accumAdjFactor = self.context.fetch_data('accumAdjFactor')

    def build(self):
        di = self.start_di - 1
        if di < self.backdays - 1:
            mark = di + 1
        else:
            mark = self.backdays
        for i in range(mark):
            adjfactor = self.accumAdjFactor[di - i] / self.accumAdjFactor[di]
            self.open[di - i] = self.rawopen[di - i] * adjfactor
            self.high[di - i] = self.rawhigh[di - i] * adjfactor
            self.low[di - i] = self.rawlow[di - i] * adjfactor
            self.close[di - i] = self.rawclose[di - i] * adjfactor
            self.vwap[di - i] = self.rawvwap[di - i] * adjfactor

    def start_day(self, di):
        pass

    def intro_day(self, di):
        pass

    def end_day(self, di):
        if di < self.backdays - 1:
            mark = di + 1
        else:
            mark = self.backdays

        self.open[di][:] = self.rawopen[di][:]
        self.high[di][:] = self.rawhigh[di][:]
        self.low[di][:] = self.rawlow[di][:]
        self.close[di][:] = self.rawclose[di][:]
        self.vwap[di][:] = self.rawvwap[di][:]

        adjfactor = self.accumAdjFactor[di - 1] / self.accumAdjFactor[di]
        tmp = abs(1 - adjfactor) > 1e-4
        for i in range(1, mark):
            self.open[di - i][tmp] = self.open[di - i][tmp] * adjfactor[tmp]
            self.high[di - i][tmp] = self.high[di - i][tmp] * adjfactor[tmp]
            self.low[di - i][tmp] = self.low[di - i][tmp] * adjfactor[tmp]
            self.close[di - i][tmp] = self.close[di - i][tmp] * adjfactor[tmp]
            self.vwap[di - i][tmp] = self.vwap[di - i][tmp] * adjfactor[tmp]

    def dependencies(self):
        self.register_dependency('open')
        self.register_dependency('high')
        self.register_dependency('low')
        self.register_dependency('close')
        self.register_dependency('vwap')
        self.register_dependency('accumAdjFactor')

    def provide_data(self):
        self.di_size = len(self.context.di_list)
        self.ii_size = len(self.context.ii_list)

        self.open = np.ndarray((self.di_size, self.ii_size), dtype=float)
        self.high = np.ndarray((self.di_size, self.ii_size), dtype=float)
        self.low = np.ndarray((self.di_size, self.ii_size), dtype=float)
        self.close = np.ndarray((self.di_size, self.ii_size), dtype=float)
        self.vwap = np.ndarray((self.di_size, self.ii_size), dtype=float)

        self.register_data('adj_open', self.open)
        self.register_data('adj_high', self.high)
        self.register_data('adj_low', self.low)
        self.register_data('adj_close', self.close)
        self.register_data('adj_vwap', self.vwap)


from deepstack_sdk import ServerConfig, Face

config = ServerConfig("http://localhost:80")
face = Face(config)

response = face.recognizeFaceVideo("video.mp4", output="webcam.mp4" )
print(response)

"""
:: face.recognizeFaceVideo()

--- Available Paramaters

- video (file, Camera video feed IP, integer for OpenCV Camera e.g 0, 1, 2)
- min_confidence (0.1 to 1.0)
- callback (function name, parses in image_byte [without label and boxes] and detections into the function)
- output (file path, cv2.VideoWriter)
- output_font (cv2 font)
- output_font_color ( r, g, b )
"""


"""This package contains classes implementing custom Bayesian Optimization method from the paper.
"""

import numpy as np

from bayes_opt import Events
from sklearn.gaussian_process.kernels import Matern

class IterationCounter(object):
    """Iteration counter implementation that works as an event listener for :py:class:`BayesianOptimization`."""

    def __init__(self):
        self.counter = 0

    def inc(self):
        self.counter += 1

    def update(self, event, *args, **kwargs):
        if event == Events.OPTIMIZATION_STEP:
            self.inc()

class CustomKernel(Matern):
    """Custom kernel based on a Matern one.
    Modifies covariance of an unseen variable with itself in a such way that dispersion of values predicted by
        a Gaussian Process is increased in the needed area.
    The detailed description is in the paper.
    """

    def __init__(self,
                 length_scale=1.0,
                 length_scale_bounds=(1e-5, 1e5),
                 nu=1.5,
                 iteration_counter=None,
                 sigmoid_scale=1e-3,
                 sigmoid_x_scale=10,
                 sigmoid_x_shift=0,
                 sigmoid_decay_iterations=3
                 ):
        """Constructor.

        :param length_scale: `length_scale` for Matern kernel
        :param length_scale_bounds: `length_scale_bounds` for Matern kernel
        :param nu: `nu` for Matern kernel
        :param iteration_counter: An object that contains `counter` property to get current optimization iteration.
            If None, iteration number is always zero
        :param sigmoid_scale: Vertical scale of a sigmoid
        :param sigmoid_x_scale: Horizontal scale of a sigmoid
        :param sigmoid_x_shift: Horizontal shift of a sigmoid
        :param sigmoid_decay_iterations: Maximal iteration number at which covariance modification is applied
        """
        super().__init__(length_scale, length_scale_bounds, nu)
        self.iteration_counter = iteration_counter
        self.sigmoid_scale = sigmoid_scale
        self.sigmoid_x_scale = sigmoid_x_scale
        self.sigmoid_x_shift = sigmoid_x_shift
        self.sigmoid_decay_iterations = sigmoid_decay_iterations

    def diag(self, X):
        res = super().diag(X)

        iteration_n = 0
        if self.iteration_counter is not None:
            iteration_n = self.iteration_counter.counter

        sigmoid = 1 / (1 + np.exp(-(X[:, 0] + self.sigmoid_x_shift) * self.sigmoid_x_scale))
        sigmoid_multiplier = self.sigmoid_scale * (
                max(self.sigmoid_decay_iterations - iteration_n, 0) / self.sigmoid_decay_iterations)
        mulitplier = 1 + (1 - sigmoid) * sigmoid_multiplier
        res = np.multiply(res, mulitplier)
        return res


import re
FuzzerPopulation 				        = 5
FuzzerNumberOfMutations		            = 2
FuzzerNumberOfHardestKept		        = 1
FuzzerNumberPopulationStart	            = 5
BugMode						            = False
# GeneratorMaxDepth				        = 5
# GeneratorNumConst				        = 5
NoBandit = False
# num_assertions							= 5
timeout							        = 20
theory = 'QF_SLIA'
solvers = []

InputFiles = []
mode = '' # cvc4, z3str3, z3seq, commit

# commits = []
string_ops =  ['Concat', 'Contains', 'At', 'Length', 'IndexOf2', 'PrefixOf', 'SuffixOf', 'Replace', 'ReInter', 'ReRange', 'RePlus', 'ReStar', 'ReConcat', 'Str2Re', 'InRegex', 'ToInt', 'Substring']
file_name                               = ''
reg_num = 0
reg_path = ''
# non_reg_num = 0
file_path = ''
# non_reg_path = ''
# diff_path = './diff'
# file_num = 0
#outputdir
OutputDirectory = None
setInfo = re.compile(r'\(set-info .*\)')
init_time = 0
diff_num = 0
#pre-defined parameter
max_var_num = 3
max_assert_num = 4
max_depth = 4
max_str_len = 20

average_time = [0, 0]

#about cases
NumAssert = 15

# Maximum Parameter
MaxStr = 500
MaxVarNum = 20
MaxAssertNum = 15
MaxDepth = 6

# Minimum Parameter
MinStr = 10
MinVarNum = 3
MinAssertNum = 3
MinDepth = 2

### for Generators
it = 0
new_var_probability = 0.0
found_time_list = [0, 0, 0]
op_dict = {}
all_run_time = []
all_time_diff = []

class para:
    def __init__(self):
        self.type = ''
        self.timeout = 20
        self.root_path = ''
        self.max_var_num = 3
        self.max_assert_num = 4
        self.max_depth = 4
        self.max_str_len = 20
        self.time0 = 0


#!/usr/bin/env python

"""Short docstring

Long Docstring

"""

from Event import *

__author__ = "Michael Pitcher"
__copyright__ = "Copyright 2017"
__credits__ = ["Michael Pitcher"]
__license__ = ""
__version__ = ""
__email__ = "mjp22@st-andrews.ac.uk"
__status__ = "Development"

class Change(Event):
    """
    Change event - a member of one compartment changes to another
    """
    def __init__(self, reaction_parameter, nodes, compartment_from, compartment_to, influencing_compartments=None):
        """
        New change event. Must specify the compartment changing from and to, may specify members of which compartments
        have caused change (else spontaneous change)
        :param reaction_parameter: Reaction parameter of event
        :param nodes: Nodes where change occurs
        :param compartment_from: The compartment to remove member from
        :param compartment_to: Compartment to add new member into
        :param influencing_compartments: Compartments whose members cause change (if not specified, spontaneous
        change)
        """
        self._compartment_from = compartment_from
        self._compartment_to = compartment_to
        self._influencing_compartments = influencing_compartments
        Event.__init__(self, reaction_parameter, nodes)

    def _calculate_state_variable_at_node(self, node):
        """
        The amount this node provides to the state variable. If no influencing compartments specified, value is count of
        compartment changing from, else value is count * sum of counts of influencing compartments
        counts of specified compartments
        :param node: Node to calculate
        :return:
        """
        state_variable = node[self._compartment_from]
        if self._influencing_compartments:
            state_variable = state_variable * sum([node[c] for c in self._influencing_compartments])
        return state_variable

    def _update_node(self, node):
        """
        Update the node by removing a member from one compartment and adding a member into another compartment
        :param node: Node to update
        :return:
        """
        node.update({self._compartment_from: -1, self._compartment_to: 1})


#!/usr/bin/env python

##########################################################################
#                                                                        #
#  This program is free software; you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation; version 2 of the License.               #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
##########################################################################

import os
import sys
import re
try:
    import re2
    re2_compile_with_fallback = re2.compile
except ImportError:
    logging.warn("pyre2 not available. It's gonna be a long job")
    re2_compile_with_fallback = re.compile
import time
#import guppy
from array import array
from datetime import datetime
import logging

## PROJECT LIBS
import sonet.mediawiki as mwlib
from sonet.lib import find_open_for_this_file
from sonet.timr import Timr
from multiprocessing import Process, Pipe

## DATABASE
from sonet.models import get_contributions_table
from base64 import b64encode
from zlib import compress
from wbin import serialize

ATTR_LEN = None

class UserContrib(object):
    __slots__ = ['comments_length', 'namespace_count', 'data']

    def __init__(self):
        ##self.namespace_count = np.zeros((attr_len,), dtype=np.int)
        ## TODO: class wide attribute
        ## data = [comments_count, minor, welcome, npov,
        ##         please, thanks, revert, first_time, last_time, normal_edits]

        ## 4 bytes per item on 64bit pc
        ## array of unsigned int, len = 10
        self.data = array('I', (0,)*10)

        ## this can be very large, in this way it uses python bigints.
        ## KEEP THIS OUT OF self.data!!
        self.comments_length = 0

        ## we don't define namespace_count here but in inc_namespace() to save
        ## memory

    @property
    def normal_count(self):
        return self.data[9]

    def inc_normal(self):
        self.data[9] += 1

    def inc_namespace(self, idx):
        if not hasattr(self, 'namespace_count'):
            ##TODO: maybe attr_len contains unneeded namespace? like key=0
            self.namespace_count = array('I', (0,)*ATTR_LEN)
        self.namespace_count[idx] += 1

    @property
    def first_time(self):
        return datetime.fromtimestamp(self.data[7])

    @property
    def last_time(self):
        return datetime.fromtimestamp(self.data[8])

    def time(self, time_):
        epoch = int(time.mktime(time_.timetuple()))
        if self.data[7] == 0 or self.data[7] > epoch:
            self.data[7] = epoch
        if self.data[8] == 0 or self.data[8] < epoch:
            self.data[8] = epoch

    @property
    def comment_length(self):
        try:
            return 1.*self.comments_length/self.data[0]
        except ZeroDivisionError:
            return 0.

    @comment_length.setter
    def comment_length(self, length):
        self.comments_length += length
        self.data[0] += 1

    @property
    def comment_count(self):
        return self.data[0]

    @property
    def minor(self):
        return self.data[1]

    def inc_minor(self):
        self.data[1] += 1

    @property
    def welcome(self):
        return self.data[2]

    def inc_welcome(self):
        self.data[2] += 1

    @property
    def npov(self):
        return self.data[3]

    def inc_npov(self):
        self.data[3] += 1

    @property
    def please(self):
        return self.data[4]

    def inc_please(self):
        self.data[4] += 1

    @property
    def thanks(self):
        return self.data[5]

    def inc_thanks(self):
        self.data[5] += 1

    @property
    def revert(self):
        return self.data[6]

    def inc_revert(self):
        self.data[6] += 1

class ContribDict(dict):
    def __init__(self, namespaces):
        global ATTR_LEN
        super(ContribDict, self).__init__()
        self._namespaces = namespaces
        ATTR_LEN = len(namespaces)
        self._d_namespaces = dict([(name.decode('utf-8'), idx) for idx, (_,
            name) in enumerate(namespaces)])
        self._re_welcome = re2_compile_with_fallback(r'well?come', flags=re.I)
        self._re_npov = re2_compile_with_fallback(r'[ n]pov', flags=re.I)
        self._re_please = re2_compile_with_fallback(r'pl(s|z|ease)',
                                                    flags=re.I)
        self._re_thanks = re2_compile_with_fallback(r'th(ank|anx|x)',
                                                    flags=re.I)
        self._re_revert = re2_compile_with_fallback(r'(revert| rev )',
                                                    flags=re.I)

        contributions, self.connection = get_contributions_table()
        self.insert = contributions.insert()

    #----------------------------------------------------------------------
    def append(self, user, page_title, timestamp, comment, minor):
        try:
            contrib = self[user]
        except KeyError:
            contrib = UserContrib()
            self[user] = contrib

        ## Namespace
        a_title = page_title.split(':')
        if len(a_title) == 1:
            contrib.inc_normal()
        else:
            try:
                contrib.inc_namespace(self._d_namespaces[a_title[0]])
            except KeyError:
                contrib.inc_normal()

        year = int(timestamp[:4])
        month = int(timestamp[5:7])
        day = int(timestamp[8:10])
        hour = int(timestamp[11:13])
        minutes = int(timestamp[14:16])
        seconds = int(timestamp[17:19])

        timestamp = datetime(year, month, day, hour, minutes, seconds)
        ## Time
        contrib.time(timestamp)

        ## Minor
        if minor:
            contrib.inc_minor()

        ## Comment
        if not comment: return
        contrib.comment_length = len(comment)
        if self._re_welcome.search(comment) is not None:
            contrib.inc_welcome()
        if self._re_npov.search(comment) is not None:
            contrib.inc_npov()
        if self._re_please.search(comment) is not None:
            contrib.inc_please()
        if self._re_thanks.search(comment) is not None:
            contrib.inc_thanks()
        if self._re_revert.search(comment) is not None:
            contrib.inc_revert()

    #----------------------------------------------------------------------
    def save(self, lang):
        """
        Save the accumulated data into DB
        """
        from itertools import islice

        iterator = self.iteritems()
        step = 100000
        for _ in xrange(0, len(self), step):
            data = [{'username': user,
                     'lang': lang,
                     'normal_edits': d.normal_count,
                     'namespace_edits': b64encode(
                         compress(serialize(d.namespace_count.tolist())))
                             if hasattr(d, 'namespace_count') else None,
                     'first_edit': d.first_time,
                     'last_edit': d.last_time,
                     'comments_count': d.comment_count,
                     'comments_avg': d.comment_length,
                     'minor': d.minor,
                     'welcome': d.welcome,
                     'npov': d.npov,
                     'please': d.please,
                     'thanks': d.thanks,
                     'revert': d.revert
                     }
                    for user, d in islice(iterator, step)]
            self.connection.execute(self.insert, data)

def use_contrib_dict(receiver, namespaces, lang):
    cd = ContribDict(namespaces)

    while 1:
        rev = receiver.recv()

        try:
            cd.append(*rev)
        except TypeError:
            cd.save(lang)
            return

class UserContributionsPageProcessor(mwlib.PageProcessor):
    """
    UserContributionsPageProcessor extracts a graph from a meta-history or a
    stub-meta-history dump.

    A state-machine-like approach is used to parse the file.

    Only tag-end events are used. (eg. in  the first event is for
    the  tag, then the one for ).

    The entry point is process_title (one per page). Then, in every page there
    are many revisions, and each one has timestamp and contributor tags.

    <page>
        <title>Title</title>
        <revision>
            <timestamp>...<timestamp>
            <contributor>...</contributor>
        </revision>
        (... more revisions ...)
    </page>
    """
    __slots__ = tuple()
    # to limit the extraction to changes before a datetime
    time_end = None
    # to limit the extraction to changes after a datetime
    time_start = None
    _re_welcome = None
    __welcome_pattern = None
    sender = None ## multiprocessing Connection object
    __namespaces = None
    count_revision = 0

    @property
    def namespaces(self):
        return self.__namespaces

    @namespaces.setter
    def namespaces(self, namespaces):
        self.__namespaces = namespaces
        self.contribution = ContribDict(namespaces)

    @property
    def welcome_pattern(self):
        return self.__welcome_pattern

    @welcome_pattern.setter
    def welcome_pattern(self, value):
        self.__welcome_pattern = value
        self._re_welcome = re.compile(value, flags=re.IGNORECASE)

    ## PAGE RELATED VARIABLES
    _skip = False
    _title = None

    ## REVISION RELATED VARIABLES
    _time = None ## time of this revision
    _comment = None
    _skip_revision = False
    _sender = None
    _minor = False

    def process_title(self, elem):
        self._title = elem.text

    def process_timestamp(self, elem):
        if self._skip_revision: return

        self._time = elem.text

    def process_contributor(self, contributor):
        if self._skip_revision: return

        if contributor is None:
            logging.warning('contributor is None')
            self._skip_revision = True

        sender_tag = contributor.find(self.tag['username'])
        if sender_tag is None:
            self._skip_revision = True
        else:
            try:
                self._sender = mwlib.normalize_pagename(sender_tag.text)
            except AttributeError:
                ## if username is defined but empty, look for id tag
                self._sender = contributor.find(self.tag['id']).text

    def process_comment(self, elem):
        if self._skip_revision or not elem.text:
            return
        self._comment = elem.text

    def process_minor(self, _):
        self._minor = True

    def process_revision(self, _):
        skip, self._skip_revision = self._skip_revision or self._skip, False
        comment, self._comment = self._comment, None
        minor, self._minor = self._minor, False
        if skip: return

        self.count_revision += 1
        assert self._sender is not None, "Sender still not defined"
        assert self._title is not None, "Page title not defined"
        assert self._time is not None, "time not defined"

        self.sender.send((self._sender, self._title, self._time,
                comment, minor))

        self._sender = None

    def process_page(self, _):
        if self._skip:
            self._skip = False
            return

        self._title = None

        self.count += 1
        if not self.count % 500:
            logging.debug("%d %d", self.count, self.count_revision)
            #with Timr('guppy'):
            #    logging.debug(guppy.hpy().heap())

    #def end(self):
    #    with Timr('save'):
    #        self.contribution.save(self.lang)

def opt_parse():
    from optparse import OptionParser
    from sonet.lib import SonetOption

    p = OptionParser(usage="usage: %prog [options] dumpfile",
                     option_class=SonetOption)
    p.add_option('-s', '--start', action="store",
        dest='start', type="yyyymmdd", metavar="YYYYMMDD", default=None,
        help="Look for revisions starting from this date")
    p.add_option('-e', '--end', action="store",
        dest='end', type="yyyymmdd", metavar="YYYYMMDD", default=None,
        help="Look for revisions until this date")
    opts, args = p.parse_args()

    ## CHECK IF OPTIONS ARE OK
    if len(args) != 1:
        p.error("Wrong number of arguments")
    if not os.path.exists(args[0]):
        p.error("Dump file does not exist (%s)" % (args[0],))
    return (opts, args)

def main():
    logging.basicConfig(#filename="usercontributions.log",
                        stream=sys.stderr,
                        level=logging.DEBUG)
    logging.info('---------------------START---------------------')

    receiver, sender = Pipe(duplex=False)

    _, args = opt_parse()
    xml = args[0]

    ## SET UP FOR PROCESSING
    lang, _, _ = mwlib.explode_dump_filename(xml)

    deflate, _lineno = find_open_for_this_file(xml)

    if _lineno:
        src = deflate(xml, 51)
    else:
        src = deflate(xml)

    tag = mwlib.get_tags(src,
        tags='page,title,revision,timestamp,contributor,username,ip'+ \
             ',comment,id,minor')

    namespaces = mwlib.get_namespaces(src)

    src.close()
    logging.info("BEGIN PARSING")
    src = deflate(xml)

    processor = UserContributionsPageProcessor(tag=tag, lang=lang)
    processor.sender = sender
    processor.namespaces = namespaces
    ##TODO: only works on it.wikipedia.org! :-)
    processor.welcome_pattern = r'Benvenut'

    p = Process(target=use_contrib_dict, args=(receiver, processor.namespaces,
                                               lang))
    p.start()

    with Timr('PROCESSING'):
        processor.start(src) ## PROCESSING

    sender.send(None)
    p.join() ## wait until save is complete

if __name__ == "__main__":
    #import cProfile as profile
    #profile.run('main()', 'mainprof')
    main()
    #h = guppy.hpy()
    #print h.heap()


'''
https://programmers.co.kr/learn/courses/30/lessons/62049
'''

def solution(n):

    if n == 1:
        return [0]
    else:
        left = list()
        right = list()
        left = solution(n-1)
        mid = [0]
        right = solution(n-1)
        right[len(right)//2] = 1
        left.extend(mid)
        left.extend(right)
        return left

print(solution(3))


# -*- coding: utf-8 -*-
from aiohttp import web
from argparse import ArgumentParser
from os import listdir, getcwd, path, stat, remove, makedirs
import random
import aiofiles

routes = web.RouteTableDef()

@routes.get('/')
async def index(request):
    '''
    index page перенаправление на /notes
    '''
    raise web.HTTPFound('/notes')

@routes.post('/notes')
async def create_new_file(request):
    '''
    функция создает новый файл с контентом из запроса

    '''
    def file_name_gen():
        '''генерирует рандомное имя файла'''
        return ''.join(
            random.choice(u'qwertyuiopasdfghjklzxcvbnm') for i in range(random.randint(5, 10)))
    data = await request.json()
    folder_name = request.app['settings'].folder
    file_name = file_name_gen()
    with open('{}/{}.txt'.format(folder_name, file_name), 'w', encoding='utf-8') as f:
        f.write(data['content'])
    return web.json_response({'note id': file_name})

@routes.get('/notes')
async def get_files_information(request):
    '''
    функция возвращает информацию по всем файлам в директории: мета и контент
    :return
        JSON
        {
            'info files': [
                {file name: file info}
            ]
        }
    '''
    files_names = listdir(request.app['settings'].folder)
    response = {'info files': [await _info(file_name, request)
                               for file_name in files_names]}
    return web.json_response(response)

@routes.get('/notes/{filename}')
async def get_file_information(request):
    '''
    функция возвращает информацию по одному файлу
    :args
        имя файла
    :return
        Json
        {
            'file name': file data
        }
    '''
    file_name = request.match_info['filename'] + '.txt'
    response = await _info(file_name, request)
    return web.json_response(response)

@routes.delete('/notes/{filename}')
async def delete_file(request):
    '''
    функция удаляет файл из директории
    :args
        имя файла
    :return
        200 в случае успеха и
        Json
            {'status': deleted}
        404 в случае, если файл не существует
    '''
    file_name = request.match_info['filename'] + '.txt'
    try:
        path_to_file = '{}/{}'.format(
            request.app['settings'].folder, file_name)
        remove(path_to_file)
        return web.json_response({'status': 'deleted'})
    except FileNotFoundError as e:
        raise web.HTTPNotFound(content_type='application/json')

async def _info(file_name, request):
    '''
    Функция возвращает метаданные и содержимое файла в директории по его имени
    :args
        имя файла
        реквест
    :return
        dict
            {
                имя файла: мета информация и контент файла
            }
        или
        Json 404 файл не найден
    '''
    path_to_file = '{}/{}'.format(request.app['settings'].folder, file_name)
    try:
        meta_values = list(stat(path_to_file))  # метаданные
        meta_keys = ['mode', 'ino', 'dev', 'nlink', 'uid',
                     'gid', 'size', 'atime', 'mtime', 'ctime']
        # Формирование словаря из метаданных
        file_meta = dict(zip(meta_keys, meta_values))
        async with aiofiles.open(path_to_file, mode='r', encoding='utf-8') as f:
            file_meta['content'] = await f.read()
        data = {file_name: file_meta}
        return data
    except FileNotFoundError as e:
        raise web.HTTPNotFound(content_type='application/json')

def server_init(args):
    '''
    фабрика приложений
    :args
        аргументы коммандной строки
    :return
        aiohttp application
    '''
    app = web.Application()
    if not path.exists(args.folder):
        makedirs(args.folder)
    app["settings"] = args
    app.add_routes(routes)
    return app

if __name__ == '__main__':
    pass


import os
import _pickle
import pickle
import signal

from time import sleep
from datetime import datetime
from functools import partial

from .logger import get_logger
from .utils import get_next_ts, ts, should_run_fallback
from .que import Queue

from .job import JobStatus, Job

loads = partial(_pickle.loads, protocol=pickle.HIGHEST_PROTOCOL)
default_ttl = 10

logger = get_logger('tq')

class Worker(object):
    def __init__(self, queues, connection, name=None):
        self.working = True
        self.connection = connection
        self.qnames = queues
        self.queues = [Queue(connection, queue) for queue in queues]
        self.name = type(self).__name__ if not name else name
        self.pid = os.getpid()

    def set_signals(self):
        logger.info('{0} started at {1} with pid {2}\nwith queues={3}\n'.format(
            self.name,datetime.now(), self.pid, self.qnames))

        pid = self.pid

        def graceful_exit(signum, frame):
            logger.info('Shutting down worker - {0} ({1})'.
                        format(self.name, pid))

            self.working = False
            os._exit(0)

        signal.signal(signal.SIGTERM, graceful_exit)
        signal.signal(signal.SIGINT, graceful_exit)

    def run(self):
        self.set_signals()

        while self.working:
            job = Queue.deque_any(self.queues, self.connection)
            self.perform(job)

    def perform(self, job, fallback=False):
        fork = os.fork()
        if fork == 0:
            success = self.perform_job(job, fallback)
            os._exit(int(not success))
        else:
            os.waitpid(fork, 0)

    def perform_job(self, job, fallback):
        try:
            logger.info('Performing {}'.format(job.id))

            started_at = ts(datetime.now())

            job.perform(run_fallback=fallback)
            pipe = self.connection.pipeline()
            pipe.multi()

            job.set_status(JobStatus.Finished, pipe)
            job.save(pipeline=pipe)

            success = True

            finished_at = ts(datetime.now())

            logger.info('successfully performed job, took {} seconds'
                        .format((finished_at - started_at)))

        except Exception as e:
            logger.error('failed to perform {}\n log-trace:\n {}'.
                         format(job.id, e))
            success = False

        return success

class ScheduledWorker(Worker):
    def __init__(self, queues, connection, latency=1):
        super().__init__(queues, connection, type(self).__name__)

        queue = queues[0]

        logger.info('Listening for jobs on {}\n'.format(queue))

        self.queue = Queue(connection, queue)
        self.latency = latency

    def reschedule(self, job, next_ts, pipeline):
        logger.info('Job-{} rescheduled at {}'.format(job.id, next_ts))
        self.queue.requeue_scheduled(job, next_ts, pipeline)

    def run(self):
        self.set_signals()

        def wait():
            sleep(self.latency)

        while self.working:
            query_at, jobs = self.queue.get_scheduled()

            if not jobs:
                wait()
                continue

            logger.info('Found {} new jobs on {}'.format(len(jobs),
                        self.queue.name.split(':')[-1]))

            for key in jobs:
                job = Job.fetch(key, self.connection)

                run_fallback = should_run_fallback(query_at, job)
                self.perform(job, run_fallback)

                job.refresh()

                next_runtime = get_next_ts(job)
                can_run_again = next_runtime is not None

                pipeline = self.connection.pipeline()

                if job.reschedulable and can_run_again:
                    job.ran_at = query_at
                    self.reschedule(job, next_runtime, pipeline)
                    continue

                self.queue.delete_scheduled(key, pipeline)
                job.expire(pipeline)
                pipeline.execute()

                logger.info('Job-{} removed from queue'.format(job.id))

            wait()

class FailedWorker(ScheduledWorker):
    latency = 30

    def __init__(self, queues, connection):
        super().__init__(queues, connection, self.latency)


import json
import requests
import environ
from requests.auth import HTTPBasicAuth

def delete_user():

    env = environ.Env()
    environ.Env.read_env()

    # your connector ID
    user_id = ''

    API_KEY = env("API_KEY")
    API_SECRET = env("API_SECRET")

    base64 = HTTPBasicAuth(API_KEY, API_SECRET)
    endpoint = 'https://api.fivetran.com/v1/users/{}'.format(user_id)

    request = requests.delete(url=endpoint, auth=base64).json()

    return request


# import cv2
# img = cv2.imread("output.png")
# edges = cv2.Canny(img, 50, 150, apertureSize = 3)
# cv2.imshow("canny",edges)
# cv2.waitKey()

"""
cv2.HoughLines()
	dst:   输出图像. 它应该是个灰度图 (但事实上是个二值化图)
	lines: 储存着检测到的直线的参数对 (r,\theta) 的容器
	rho : 参数极径 r 以像素值为单位的分辨率. 我们使用 1 像素.
	theta: 参数极角 \theta 以弧度为单位的分辨率. 我们使用 1度 (即CV_PI/180)
	threshold:    设置阈值： 一条直线所需最少的的曲线交点
	srn and stn:  参数默认为0

cv2.HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 )
	dst:    输出图像. 它应该是个灰度图 (但事实上是个二值化图)
	lines:  储存着检测到的直线的参数对 (x_{start}, y_{start}, x_{end}, y_{end}) 的容器
	rho :   参数极径 r 以像素值为单位的分辨率. 我们使用 1 像素.
	theta:  参数极角 \theta 以弧度为单位的分辨率. 我们使用 1度 (即CV_PI/180)
	threshold:    设置阈值： 一条直线所需最少的的曲线交点。超过设定阈值才被检测出线段，值越大，基本上意味着检出的线段越长，检出的线段个数越少。
	minLinLength: 能组成一条直线的最少点的数量. 点数量不足的直线将被抛弃.
	maxLineGap:   能被认为在一条直线上的两点的最大距离。
"""
import cv2
import numpy as np

original_img = cv2.imread("output.png", 0)

kernel = np.ones((20,20), np.uint8)
img_dilate = cv2.dilate(original_img, kernel)

img = cv2.resize(img_dilate, None, fx=0.8, fy=0.8,
                 interpolation=cv2.INTER_CUBIC)

# img = cv2.GaussianBlur(img, (3, 3), 0)
edges = cv2.Canny(img, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 90)  # 这里对最后一个参数使用了经验型的值
result = img.copy()

for line in lines:
    rho = line[0][0]  # 第一个元素是距离rho
    theta = line[0][1]  # 第二个元素是角度theta
    # print(rho)
    # print(theta)
    if (theta < (np.pi / 4.)) or (theta > (3. * np.pi / 4.0)):  # 垂直直线
        pt1 = (int(rho / np.cos(theta)), 0)  # 该直线与第一行的交点
        # 该直线与最后一行的焦点
        pt2 = (int((rho - result.shape[0] * np.sin(theta)) / np.cos(theta)), result.shape[0])
        cv2.line(result, pt1, pt2, (255))  # 绘制一条白线

        print(pt1,pt2)

    else:  # 水平直线
        pt1 = (0, int(rho / np.sin(theta)))  # 该直线与第一列的交点
        # 该直线与最后一列的交点
        pt2 = (result.shape[1], int((rho - result.shape[1] * np.cos(theta)) / np.sin(theta)))
        cv2.line(result, pt1, pt2, (255), 1)  # 绘制一条直线

cv2.imshow('Canny', edges)
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()


# BATTLEFIELD ("Battleship") GAME. A 2-player Python Terminal Game of Battleship
#
# Created in 2021 by Vahan Bznuni
#  for a CodeAcademy Portfolio Project: "Terminal Game"
#
# http://github.com/vahanbznuni/Battlefield-Game

#Test

"""
Main Module. Controls main flow of the game.

The check_winner funciton checks if there is a winner.
The current_turn_string function returns custom string indicating who's turn\
     it is (Player's or Computer's).
The turn function executes a turn (targetting opposing player).
The ready_to_play contains a print statement for player readiness to start\
     - and a calls a display of the player's battlerfield.
The play_game function executes the main game - by alternating turns until\
     there\ is a winner.
The end_game function contains the closing statements - including winner\
     statement.
"""

from Elements.Player import Player, Computer
from Elements.Battlefield_Strings import Battlefield_Strings as strings
NL ="\n"

def check_winner():
    """Check if there is a winner.

    Returns:
      The winner (player1 or player2), or <None>, if no winner.
    """
    if player1.check_fleet_sunk():
        winner = player2
    elif player2.check_fleet_sunk():
        winner = player1
    else:
        winner = None
    return winner

def current_turn_string(player):
    """Return custom string indicating to the player who's turn it is (main\
         Player's (player1) or Computer's (player2)).

    Args:
      player (object): the Player who's turn it currently is.
    Returns:
      A customized string from the strings module to be printed,\
           indicating current turn.
    """
    if player == player1:
        string = strings.TargettingStrings.target_str
    elif player == player2:
        string = strings.TargettingStrings.incoming_str
    return string

def turn(player_x, player_y):
    """Execute a turn, targetting the opposing player by calling the target\
         method of the player who's turn it is.

    Targetting is preceded by:
      input: asking playuer to press ENTER key to continue
      print statement: letting player know who's turn it is
      input: asking playuer to press ENTER key to continue

    Args:
      player_x (object): the Player who's turn it is.
      player_y (object): the opposing Player against.
    """
    input(strings.continue_str)
    print(strings.Formatting.line_str2 + \
        NL*2 + str(current_turn_string(player_x)))
    input(strings.continue_str)
    player_x.target(player_y)

def ready_to_play():
    """print a statement indicating that the game (i.e. first turn) is about\
         to start; and display main player's Battlefiled"""
    print(strings.Formatting.line_str2 + NL*2 + \
        strings.OpeningStatements.ready_str + NL)
    player1.battlefield.display()

def play_game():
    """Until there is a winner, alternate turns for player1 and player2."""
    while not check_winner():
        turn(player1, player2)
        if not check_winner():
            turn(player2, player1)

def end_game():
    """Declare the winner and print ending statements."""
    print(strings.ClosingStatements.winner_str.format(check_winner()))
    print(NL*2 + strings.ClosingStatements.final_str3)
    print(NL + strings.ClosingStatements.final_str1)
    input(NL + strings.ClosingStatements.final_str2)

#-----------------------------------------------------------------------------

#Start Game. Opening Statements.
strings.OpeningStatements.intro_str()

#Initialze players. Includes interactive ship placement by user.
player1 = Player()
player2 = Computer()

#Main Sequence of Game & Ending.
ready_to_play()
play_game()
end_game()


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
# dht.py
# created 30.04.2021
# Thomas Kaulke, kaulketh@gmail.com
# https://github.com/kaulketh
# -----------------------------------------------------------
from sys import stdout, stderr
from time import sleep

import adafruit_dht

SENSOR = adafruit_dht.DHT22(24)
EMPTY = ""

def get_values() -> tuple:
    """
    Do not call up more often than every 3 seconds!

    :rtype: tuple
    :returns:   Tuple which contains:
                humidity string(human readable),
                temperature string(human readable),
                concatenated string(human readable),
                humidity value,
                temperature value
    """

    temperature_string = EMPTY
    humidity_string = EMPTY
    summary_string = EMPTY
    stdout.write("Get temperature and humidity values: ")
    humidity = SENSOR.humidity
    temperature = SENSOR.temperature

    if humidity is not None and temperature is not None:
        temperature_string += f"{temperature:04.1f}°C"
        humidity_string += f"{humidity:04.1f}%"
        summary_string += f"{temperature_string} {humidity_string}"
        stdout.write(f"{summary_string}\n")
    else:
        stderr.write(
            "Failed to get temperature and humidity values. Set to '0'!\n")
        humidity = 0
        temperature = 0
    return \
        humidity_string, \
        temperature_string, \
        summary_string, \
        humidity, \
        temperature

if __name__ == '__main__':
    while True:
        for v in get_values():
            print(f" - {v}")
        sleep(10)