# -*- coding: utf-8 -*-
"""学堂在线"""

from moocs.utils import *
from utils.crawler import Crawler

name = "xuetangx_next"
need_cookies = True
CANDY = Crawler()
CONFIG = {}
FILES = {}
VIDEOS = []
exports = {}
__all__ = ["name", "need_cookies", "start", "exports"]

def get_summary(url):
    """从课程主页面获取信息"""

    sign, cid = re.match(r"https?://next.xuetangx.com/course/"
                        "(?P<sign>.+?)/(?P<cid>.+)", url).group("sign", "cid")

    res = CANDY.get("https://next.xuetangx.com/api/v1/lms/learn/product/info?cid=%s&sign=%s" % (cid, sign))
    course_name = res.json()['data']['classroom_name']
    # 机构名称不太容易获取，暂时不获取
    dir_name = course_dir(course_name, "学堂在线")

    print(dir_name)
    CONFIG['sign'] = sign
    CONFIG['cid'] = cid
    return cid, sign, dir_name

def parse_resource(resource):

    cid, sign = CONFIG['cid'], CONFIG['sign']
    file_name = resource.file_name
    item_id, item_info_id = resource.meta
    res = CANDY.get("https://next.xuetangx.com/api/v1/lms/learn/leaf_info/%s/%s/?sign=%s" % (cid, item_id, sign),
                    headers={"xtbz": "xt"})
    if resource.type == 'Video':
        ccid = res.json()['data']['content_info']['media']['ccid']

        video_url_res = CANDY.get("https://next.xuetangx.com/api/v1/lms/service/playurl/%s/?appid=10000" % ccid)
        sources = video_url_res.json()['data']['sources']
        qualitys = ['20', '10']
        for qa in qualitys:
            if sources.get('quality' + qa):
                # 居然是个数组，暂时没发现多段的，希望以后也没有吧……
                video_url = sources['quality' + qa][0]
                break

        ext = '.mp4'
        if WORK_DIR.need_download(file_name + ext, CONFIG["overwrite"]):
            FILES['renamer'].write(video_url.split('?')[0].split('/')[-1], file_name, ext)
            FILES['video'].write_string(video_url)
            VIDEOS.append((video_url, file_name+ext))
            resource.ext = ext

        if not CONFIG['sub']:
            return
        # 暂未支持多语言
        subtitle_res = CANDY.get("https://next.xuetangx.com/api/v1/lms/service/subtitle_parse/?c_d=%s&lg=0" % ccid)
        if subtitle_res.status_code != 200:
            return
        subtitle_json = subtitle_res.json()
        starts, ends, texts = subtitle_json['start'], subtitle_json['end'], subtitle_json['text']
        subtitle = Subtitle(WORK_DIR.file(file_name + '.srt'))
        assert len(starts) == len(ends) == len(texts)
        for i in range(len(starts)):
            subtitle.write(texts[i], starts[i]/1000, ends[i]/1000)

    elif resource.type == 'Document':
        if not WORK_DIR.need_download(file_name + '.pdf', CONFIG["overwrite"]):
            return
        # 暂时也没遇到多个文件的情况
        downloads = res.json()['data']['content_info']['download']
        if downloads:
            pdf_url = downloads[0]['file_url']
            CANDY.download_bin(pdf_url, WORK_DIR.file(file_name + '.pdf'))

def get_resource(cid, sign):
    """获取各种资源"""

    outline = Outline()
    counter = Counter()

    video_list = []
    pdf_list = []

    res = CANDY.get("https://next.xuetangx.com/api/v1/lms/learn/course/chapter?cid=%s&sign=%s" % (cid, sign),
                    headers={"xtbz": "xt"})
    for chapter in res.json()['data']['course_chapter']:
        counter.add(0)
        chapter_id, chapter_name, chapter_order = chapter['id'], chapter['name'], chapter['order']
        outline.write(chapter_name, counter, 0)

        for section in chapter['section_leaf_list']:
            counter.add(1)
            section_id, section_name, section_order = section['id'], section['name'], section['order']
            outline.write(section_name, counter, 1)

            # 暂时忽略讨论、测验，以后可能支持（在 section 中作为叶子结点， type_id = 4 6）
            for item in section.get('leaf_list', []):
                counter.add(2)
                item_id, item_name, item_order = item['id'], item['name'], item['order']
                item_type, item_info_id = item['leaf_type'], item['leafinfo_id']
                # Video
                if item_type == 0:
                    outline.write(item_name, counter, 2, sign='#')
                    video_list.append(Video(counter, item_name, (item_id, item_info_id)))
                # Docs
                elif item_type == 3:
                    item_name = item_name.rstrip('.pdf')
                    outline.write(item_name, counter, 2, sign='*')
                    if CONFIG['doc']:
                        pdf_list.append(Document(counter, item_name, (item_id, item_info_id)))

    if video_list:
        rename = WORK_DIR.file('Names.txt') if CONFIG['rename'] else False
        WORK_DIR.change('Videos')
        playlist = get_playlist(CONFIG["playlist_type"], CONFIG["playlist_path_type"])
        if playlist is not None:
            parse_res_list(video_list, rename, parse_resource, playlist.write)
        else:
            parse_res_list(video_list, rename, parse_resource)
    if pdf_list:
        WORK_DIR.change('PDFs')
        parse_res_list(pdf_list, None, parse_resource)

def start(url, config, cookies=None):
    """调用接口函数"""

    global WORK_DIR
    CANDY.set_cookies(cookies)
    CONFIG.update(config)

    cid, sign, course_name = get_summary(url)

    WORK_DIR = WorkingDir(CONFIG['dir'], course_name)
    WORK_DIR.change('Videos')
    FILES['renamer'] = Renamer(WORK_DIR.file('Rename.{ext}'))
    FILES['video'] = ClassicFile(WORK_DIR.file('Videos.txt'))

    get_resource(cid, sign)

    exports.update({
        "workdir": WORK_DIR,
        "spider": CANDY,
        "videos": VIDEOS
    })


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

# In[1]:

#!/usr/bin/env python
# coding: utf-8
from bs4 import BeautifulSoup
import requests
import re

##过滤HTML中的标签
#将HTML中标签等信息去掉
def filter_tags(htmlstr):
    #先过滤CDATA
    re_cdata=re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #匹配CDATA
    re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
    re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
    re_br=re.compile('<br\s*?/?>')#处理换行
    re_h=re.compile('</?\w+[^>]*>')#HTML标签
    re_comment=re.compile('<!--[^>]*-->')#HTML注释
    s=re_cdata.sub('',htmlstr)#去掉CDATA
    s=re_script.sub('',s) #去掉SCRIPT
    s=re_style.sub('',s)#去掉style
    s=re_br.sub('\n',s)#将br转换为换行
    s=re_h.sub('',s) #去掉HTML 标签
    s=re_comment.sub('',s)#去掉HTML注释
    #去掉多余的空行
    blank_line=re.compile('\n+')
    s=blank_line.sub('\n',s)
    return s

#抓取Target网页内文章的title,abstract和keywords
def getPaperInfo(Target):
    #进入文章页面
    Req = requests.get(Target)
    Html = Req.text
    Html

    bf = BeautifulSoup(Html,"html5lib")
    #抓取title
    title = filter_tags(str(bf.find_all('h1',class_ = 'ArticleTitle')))

    #抓取abstract
    tag = bf.section
    contents = tag.p.contents
    abstract = ''
    for i in range(len(contents)):
        abstract = abstract + str(contents[i])

    abstract = filter_tags(abstract)

    #抓取keywords
    keywords_tag = bf.find_all('span',class_ = 'Keyword')
    keywords = []
    for i in range(len(keywords_tag)):
        keywords.append(filter_tags(str(keywords_tag[i]))[:-1])

    return {"title":title,"abstract":abstract,"keywords":keywords}

# In[2]:

#进入首页
url = 'https://link.springer.com'
target = url + '/journal/11192'
req = requests.get(url = target)
html = req.text

bf = BeautifulSoup(html,"html5lib")
#找到所有文章页面
show_all = bf.find_all('div',class_ = 'show-all')
href = url + show_all[0].a.get('href')
#进入所有文章页面
req = requests.get(url = href)
html = req.text

# In[3]:

bf = BeautifulSoup(html,"html5lib")

# In[8]:

#找到要抓取文章页面
paperTitle = bf.find_all('a',class_ = 'title')

#paperNum为抓取的最新文章数，目前最大值为20，大于此数值时需要切换下一页
#paperNum = len(paperTitle) #抓取本页面所有文章（20篇）
paperNum = 5 #抓取前5篇文章
targets = []
for i in range(paperNum):
    targets.append(url + paperTitle[i].get('href'))
targets

# In[9]:

#抓取文章
papers = []
for i in range(paperNum):
    papers.append(getPaperInfo(targets[i]))

# In[22]:
#结果为list，每个list元素为一个dictionary，分别有title，abstract，keywords字段
print(papers)


import matplotlib.pyplot as plt
import numpy as np

def nearest_neighbor(input_signal, zoom_multiples):
    '''
	最近邻插值（适用于灰度图）
	:param input_signal: 输入图像
	:param zoom_multiples:  缩放倍数
	:return: 缩放后的图像
	'''
    input_signal_cp = np.copy(input_signal)  # 输入图像的副本

    input_row, input_col = input_signal_cp.shape  # 输入图像的尺寸（行、列）

    # 输出图像的尺寸
    output_row = int(input_row * zoom_multiples[0])
    output_col = int(input_col * zoom_multiples[1])

    output_signal = np.zeros((output_row, output_col))  # 输出图片

    for i in range(output_row):
        for j in range(output_col):
            # 输出图片中坐标 （i，j）对应至输入图片中的（m，n）
            m = round(i / output_row * input_row)
            n = round(j / output_col * input_col)
            # 防止四舍五入后越界
            if m >= input_row:
                m = input_row - 1
            if n >= input_col:
                n = input_col - 1
            # 插值
            output_signal[i, j] = input_signal_cp[m, n]

    return output_signal

a = np.array([[2, 5, 15], [3, 10, 19]], dtype='float')
b = nearest_neighbor(a, (1, 3))
print(a)
plt.imshow(a)
plt.show()
print(b)
plt.imshow(b)
plt.show()


from opengever.core.upgrade import SchemaMigration
from sqlalchemy import text

class AddCaseInsensitiveSQLIndexesForUserAndGroupIDs(SchemaMigration):
    """Add case insensitive SQL indexes for user and group IDs.
    """

    def migrate(self):
        INDEXES = (
            ('ix_users_userid_lower', 'users', [text('lower(userid)')]),
            ('ix_groups_groupid_lower', 'groups', [text('lower(groupid)')]),
        )

        for idx_name, table_name, idx_columns in INDEXES:
            self.create_index_if_not_exists(idx_name, table_name, idx_columns)


from appium import webdriver
import yaml
import os

#文件  =====》封装读取yaml文件方式----了解
def appium_desired():
    # 获取当前路径---就算被别的文件调用 获取当前位置
    base_dir = os.path.dirname(__file__)
    yaml_path = os.path.join(base_dir, 'ck_kyb_caps.yaml')
    print(yaml_path)
    ckfile=open(yaml_path,'r',encoding='utf-8')
    # 加载yaml文件
    data=yaml.load(ckfile)
    desired_caps={}
    desired_caps['platformName']=data['platformName']
    desired_caps['platformVersion']=data['platformVersion']
    desired_caps['deviceName']=data['deviceName']
    # os.path.dirname代表获取当前路径  比如  c：/a/b/c.txt ====>c：/a/b/
    # 两层的话再获取上一层路径
    base_dir = os.path.dirname(os.path.dirname(__file__))
    print("项目路径",base_dir)
    # 获取项目路径下 app路径下的 apk文件
    app_path = os.path.join(base_dir,'app', data['appname'])
    desired_caps['app']=app_path
    desired_caps['appPackage']=data['appPackage']
    desired_caps['appActivity']=data['appActivity']
    desired_caps['noReset']=data['noReset']
    # appium服务的端口
    dr=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
    dr.implicitly_wait(4)
    return dr

# 单元测试
if __name__ == '__main__':
    appium_desired()


__all__ = ('CircularLayout', )
import math
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget

class CircularLayout (FloatLayout):
    def __init__(self, **kvargs):
        # The max widgets that the layout can hold, I think it does not have a purpose but I havent checked
        self._max_widgets = 0
        # Stores which widget is exactly to the right, even after rotation
        self._first_widget = -1
        # Kivy docs explain timers
        self._trigger_layout = Clock.create_trigger(self._do_layout, -1)
        super (FloatLayout, self).__init__(**kvargs)
        # Any changes to these attributes will trigger a method call to _do_layout at the earliest time possible
        self.bind (children = self._trigger_layout,
                   pos = self._trigger_layout,
                   pos_hint = self._trigger_layout,
                   size_hint = self._trigger_layout,
                   size = self._trigger_layout)

    def _do_layout (self, *largs):
        if len(self.children) == 0:
            return
        # see above
        self._max_widgets = num_widgets = len(self.children)
        # stores in local variables for efficient access
        temp_center_x, temp_center_y = self.center
        children = self.children[:]

        max_width = max_height = 0
        # looks for the max possible diagonal that can occur by finding max possible width and height
        for c in children:
            csx, csy = c.size
            if csx > max_width:
                max_width = csx
            if csy > max_height:
                max_height = csy
        max_diagonal = math.sqrt (max_width*max_width + max_height*max_height)

        # Some math to calculate minimum radius required to a widget given widget radius (max_diagonal/2)
        # and number of widgets. Math works I believe
        layout_radius = math.ceil (max_diagonal/2/math.sqrt(2*(1-math.cos(math.pi/len(children)))))

        # Iterates at each angle starting with the first widget set by the variable _first_widget
        # and does some work to find the widget's position
        theta = 0
        theta_increment = 2*math.pi/len(self.children)
        i = self._first_widget

        # No do/while loop in python so implemented as an infinite loop with a break
        # Basically, the first widget can be anywhere in the list and each widget in the list must be drawn in order
        # from that first widget including the ones behind it in terms of index
        # So, iterates with modulo operations
        while True:
            new_center = temp_center_x + math.cos (theta)*layout_radius, temp_center_y + math.sin (theta)*layout_radius
            self.reposition_child (children[i], center=new_center)
            theta += theta_increment
            i = (i - 1)%num_widgets
            if i == self._first_widget:
                break

    def do_rotation (self, steps=1):
        # Some modulo operations used to rotate the first child position
        num_widgets = len(self.children)
        if num_widgets == 0:
            return
        steps %= num_widgets
        self._first_widget = (self._first_widget - steps)%num_widgets
        self._do_layout ()

    def get_child (self, index=0):
        return self.children[(self._first_widget + index)%len(self.children)]

    def add_widget (self, widget, index=0):
        # Binds the new widget callback so that the layout is rearranged when a new widget is added
        self._first_widget += 1
        widget.bind (
            size = self._trigger_layout,
            size_hint = self._trigger_layout,
            pos = self._trigger_layout,
            pos_hint = self._trigger_layout)
        return super (FloatLayout, self).add_widget (widget, index)

    def remove_widget (self, widget):
        # Binds the new widget callback so that the layout is rearranged when a new widget is added
        self._first_widget -= 1
        widget.unbind(
            size = self._trigger_layout,
            size_hint = self._trigger_layout,
            pos = self._trigger_layout,
            pos_hint = self._trigger_layout)
        return super(Layout, self).remove_widget(widget)


# [REF]
# https://wikidocs.net/47449
# [API]
# https://github.com/sharebook-kr/pykrx
# [Terms]
# OHLC: Open High Low Close: 시가, 최고가, 최저가, 종가

import requests

from restful.parse_xml import get_corp_code_by_corp_name

DEBUG = True
RETRY_COUNT = 100

from pykrx import stock
import datetime

def test():
    # tickers: 시세
    tickers = stock.get_market_ticker_list(market="KOSPI")
    tickers += stock.get_market_ticker_list(market="KOSDAQ")
    tickers += stock.get_market_ticker_list(market="KONEX")

    return tickers

def get_latest_market_ohlcv(today=None, market="ALL"):

    if today == None:
        today = datetime.datetime.today().strftime("%Y%m%d")

    if DEBUG == True: print(get_latest_market_ohlcv.__name__, today)

    today_datetime = datetime.datetime.strptime(today, "%Y%m%d")
    for i in range(0, RETRY_COUNT):
        latest_date = (today_datetime - datetime.timedelta(i)).strftime("%Y%m%d")
        if DEBUG: print(latest_date)
        result = stock.get_market_ohlcv_by_ticker(latest_date, market)
        if result.empty == False:
            return result

    if DEBUG: print("fail")
    return None
def get_latest_market_cap(today=None, market="ALL"):
    if today == None:
        today = datetime.datetime.today().strftime("%Y%m%d")

    if DEBUG == True: print(get_latest_market_ohlcv.__name__, today)

    today_datetime = datetime.datetime.strptime(today, "%Y%m%d")
    for i in range(0, RETRY_COUNT):

        latest_date = (today_datetime - datetime.timedelta(i)).strftime("%Y%m%d")
        if DEBUG: print(latest_date)
        result = stock.get_market_cap_by_ticker(latest_date, market)
        if result.empty == False:
            return result

    return None

def test_latest_market_ohlcv():
    df_latest_market_ohlcv = get_latest_market_ohlcv()

    print(df_latest_market_ohlcv.loc['000020'])
    print(df_latest_market_ohlcv.columns)
    print(df_latest_market_ohlcv.index)

    loop = 0
    loop_count = 10
    for index in df_latest_market_ohlcv.index:
        # print(df_latest_market_ohlcv.loc[index])
        종목명 = df_latest_market_ohlcv.loc[index][df_latest_market_ohlcv.columns[0]]
        시가 = df_latest_market_ohlcv.loc[index][df_latest_market_ohlcv.columns[1]]
        종가 = df_latest_market_ohlcv.loc[index][df_latest_market_ohlcv.columns[4]]
        시가총액 = df_latest_market_ohlcv.loc[index][df_latest_market_ohlcv.columns[7]]
        print(종목명, 종가, 시가총액)

        loop += 1
        if loop > loop_count:
            break

def get_lastest_market_fundamental(today=None):
    if today == None:
        today = datetime.datetime.today().strftime("%Y%m%d")

    if DEBUG == True:
        print(get_lastest_market_fundamental.__name__, today)

    today_datetime = datetime.datetime.strptime(today, "%Y%m%d")

    for i in range(0, RETRY_COUNT):
        latest_date = (today_datetime - datetime.timedelta(i)).strftime("%Y%m%d")
        if DEBUG: print(latest_date)
        result = stock.get_market_fundamental_by_ticker(latest_date)
        if result.empty == False:
            return result

    if DEBUG: print("fail")
    return None

def test_lastest_market_fundamental():
    df_lastest_market_fundamental = get_lastest_market_fundamental()

    # print(df_lastest_market_fundamental)
    # print(df_lastest_market_fundamental.loc['000020'])
    # print(df_lastest_market_fundamental.columns)
    # print(df_lastest_market_fundamental.index)

    loop = 0
    loop_count = 10
    for index in df_lastest_market_fundamental.index:
        # print(df_latest_market_ohlcv.loc[index])
        종목명 = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[0]]
        DIV = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[1]]
        BPS = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[2]]
        PER = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[3]]
        EPS = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[4]]
        PBR = df_lastest_market_fundamental.loc[index][df_lastest_market_fundamental.columns[5]]
        print(종목명, DIV, BPS, PER, EPS, PBR)

    #     loop += 1
    #     if loop > loop_count:
    #         break

# 장 열렸을 때만 사용 가능,,
def 실시간_시세(corp_name):
    # corp_code = get_corp_code_by_corp_name(corp_name)
    corp_stock_code = "005930"
    # 실시간_시세
    URI = "http://asp1.krx.co.kr/servlet/krx.asp.XMLSise?code=" + corp_stock_code
    # 공시정보
    # URI = "http://asp1.krx.co.kr/servlet/krx.asp.DisList4MainServlet?code=" + corp_stock_code + "&gubun=" + kor_eng # (K:국문/E:영문)
    # 재무종합
    # URI = "http://asp1.krx.co.kr/servlet/krx.asp.XMLJemu?code=" + corp_code
    # 재무종합2
    # URI = "http://asp1.krx.co.kr/servlet/krx.asp.XMLJemu2?code=" + corp_code
    # 재무종합3
    # URI = "http://asp1.krx.co.kr/servlet/krx.asp.XMLJemu3?code=" + corp_code
    # 텍스트
    # URI = "http://asp1.krx.co.kr/servlet/krx.asp.XMLText?code=" + corp_code
    if (DEBUG): print("URI:", URI)
    res = requests.get(URI)
    return res


'''
Compute minimum interatomic distance for each conformation in each path
and plot the result
'''

import numpy as np
import matplotlib.pyplot as plt

def compute_bond_length_stats(bond_indexer):
	'''
	Compute min, max, and avg length of bonds
	'''
	min_bl = bond_indexer[0].length.angstrom;									# initialize min length
	max_bl = bond_indexer[0].length.angstrom;									# initialize max length
	avg_bl = 0.0;
	for b in bond_indexer:														# loop over bonds in the bond indexer
		bl = b.length.angstrom													# get the bond length in Angstroms
		avg_bl = avg_bl + bl
		if bl < min_bl : min_bl = bl
		if bl > max_bl : max_bl = bl

	avg_bl = avg_bl / bond_indexer.size

	return [max_bl, avg_bl, min_bl]

def compute_bond_length_stats_per_path(path, bond_indexer):
	'''
	Compute min, max, and avg length of bonds along the path
	'''
	bond_length_along_path = []

	for i in range(path.numberOfSteps):											# loop over path steps
		path.currentStep = i													# set the current step of the path
		bond_length_along_path.append(compute_bond_length_stats(bond_indexer))	# compute bond length stats for the current step
		# If you want to see in the viewport how the path changes, force SAMSON to process events
		# Comment it for bigger systems, since it may slow down the computations
		SAMSON.processEvents()

	return bond_length_along_path

# Open a molecule with a path
SAMSON.importFromFile(os.path.dirname(path_to_pyscript) + '/../samples/1VPK-path.sam')
SAMSON.processEvents()

path_indexer = SAMSON.getNodes('n.t path')										# get an indexer of all paths in the active document
bond_indexer = SAMSON.getNodes('n.t bond')										# get an indexer of all bands in the active document

# Compute bond length stats for each path in the document
bond_length_along_paths = []
for path in path_indexer:														# loop over paths in the path indexer
	bond_length_along_paths.append( compute_bond_length_stats_per_path(path, bond_indexer) )

# Plot results using matplotlib
for i in range(0, path_indexer.size):
	plt.figure(i)
	plt.plot(bond_length_along_paths[i])
	plt.title(path_indexer[i].name)
	plt.legend(['max','mean','min'])
	plt.ylabel('bond length, Å')
	plt.xlabel('conformation')
	plt.show()


# _*_ coding:utf-8 _*_
import pika

rabbitmq_host = '172.16.10.10'
rabbitmq_port = 5672

connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host, rabbitmq_port))
channel = connection.channel()

channel.exchange_declare(exchange='logs',
                         type='fanout')

# 不指定queue名字,rabbitmq会随机分配一个名字
# exclusive=True 表示使用此queue的消费者断开后,自动将queue删除
result = channel.queue_declare(exclusive=True)

queue_name = result.method.queue

# 绑定消费者连接指定exchange
channel.queue_bind(exchange='logs',
                   queue=queue_name)

print('[*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print("[x] %r" % body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(callback,
                      queue=queue_name)

channel.start_consuming()


# %%
from pathlib import Path
from os import path
from shutil import unpack_archive
from urllib.request import urlretrieve

import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from dublin_building_stock import join

data_dir = Path("../data")

# %% [markdown]
# # Amalgamate Cross-tabulated Small Area building stock for all Dublin LAs

# %%
dublin_buildings_at_small_area = (
    pd.concat(
        [
            pd.read_csv(f"../data/{local_authority}_SA_2011.csv")
            for local_authority in ["DCC", "DLR", "FCC", "SD"]
        ]
    )
    .query("`sa_2011` != ['Dublin City', 'South Dublin']")
    .query("`period_built_unstandardised` != ['Total', 'All Houses']")
    .replace({">3": 1, "<3": 1, ".": np.nan})
    .dropna(subset=["value"])
    .assign(
        value=lambda df: df["value"].astype(np.int32),
        sa_2011=lambda df: df["sa_2011"].str.replace(r"_", r"/"),
        period_built_unstandardised=lambda df: df["period_built_unstandardised"]
        .str.lower()
        .str.replace("2006 or later", "2006 - 2011"),
    )
)

# %% [markdown]
# # Read 2011 Small Area Boundaries
dublin_small_area_boundaries = (
    gpd.read_file(
        data_dir / "DublinCensus2011_Small_Areas_generalised20m_with_postcodes",
    )
    .to_crs(epsg=2157)
    .drop(columns="postcodes")
)

# %% [markdown]
# # Read Postcode Boundaries
ireland_postcode_boundaries = gpd.read_file(
    data_dir / "routingkeys_shape_itm_2016_09_29",
).to_crs(epsg=2157)

# %% [markdown]
# # Link 2011 Small Areas to Postcodes

# %%
dublin_small_area_boundaries["CountyName"] = (
    gpd.sjoin(
        dublin_small_area_boundaries.assign(
            geometry=lambda gdf: gdf.geometry.representative_point()
        ),
        ireland_postcode_boundaries,
        op="within",
        how="left",
    )
    .loc[:, "Descriptor"]
    .str.title()
    .replace(r"(^(?!Dublin|Bray|Maynooth).*$)", "Co. Dublin", regex=True)
)

# %% [markdown]
# # Expand each Small Area to Individual Buildings

# %%
def expand_to_indiv_buildings(stock, on="value"):
    return pd.DataFrame(stock.values.repeat(stock[on], axis=0), columns=stock.columns)

dublin_indiv_buildings_at_small_area = expand_to_indiv_buildings(
    dublin_buildings_at_small_area
).drop(columns="value")

# %% [markdown]
# # Anonymise stock to Postcode level

# %%
dublin_indiv_buildings_at_postcode_level = dublin_indiv_buildings_at_small_area.merge(
    dublin_small_area_boundaries, how="left", left_on="sa_2011", right_on="SMALL_AREA"
).loc[:, ["dwelling_type_unstandardised", "period_built_unstandardised", "CountyName"]]

# %% [markdown]
# # Save

# %%
dublin_indiv_buildings_at_postcode_level.to_csv(
    "../data/dublin_building_stock_up_to_2011.csv", index=False
)

# %%


#!/usr/bin/env python

# See: https://github.com/fiedl/hole-ice-study/issues/57#issuecomment-385347525

# import code; code.interact(local=dict(globals(), **locals()))  # like binding.pry

import cli
import options

parameters_esca = []
parameters_r_r_dom = []
llhs = []

for data_dir in cli.data_dirs():
  simulation_options = options.read(data_dir)
  llh = simulation_options["llh"]
  esca = simulation_options["effective_scattering_length"]
  r_r_dom = simulation_options["hole_ice_radius_in_dom_radii"]

  if (llh < 0) and (esca > 0) and (r_r_dom > 0):
    parameters_esca.append(esca)
    parameters_r_r_dom.append(r_r_dom)
    llhs.append(llh)

import numpy as np

best_llh = np.max(llhs)
neg_two_delta_llhs = - 2 * (np.asarray(llhs) - best_llh)

print("best values:")
print("  LLH: " + str(best_llh))
i = np.where(llhs == best_llh)[0][0]
print("  esca = " + str(parameters_esca[i]) + "m")
print("  r = " + str(parameters_r_r_dom[i]) + " r_dom")

import matplotlib as mpl
from matplotlib.mlab import griddata
from matplotlib import ticker
import matplotlib.pyplot as plt

# import code; code.interact(local=dict(globals(), **locals()))  # like binding.pry

# prepare canvas
fig, ax = plt.subplots(1, 1, facecolor="white")

# extract data
x = parameters_r_r_dom
y = parameters_esca
z = neg_two_delta_llhs

# define grid
xi = np.linspace(np.min(x), np.max(x), 100)
yi = np.linspace(np.min(y), np.max(y), 100)

# grid the data
zi = griddata(x, y, z, xi, yi, interp='linear')

cf0 = ax.contourf(xi, yi, zi, 15)
# cf0 = ax.contourf(xi, yi, zi, 15, vmax = 15, extend = 'max') #, vmax=1.5e6)
# clev = np.arange(z.min(), 15, 1)
# cf0 = ax.contourf(xi, yi, zi, clev, extend = 'max')

cbar = plt.colorbar(cf0)
cbar.ax.set_ylabel("$-2 \Delta \mathrm{LLH}$")

# plot data points.
ax.scatter(x, y, marker='o', s=1, zorder=10)

# labels
ax.set_title("Flasher parameter scan: LLH (simulation vs data), poisson equal weights")
ax.set(xlabel = 'hole-ice column radius [r_DOM]', ylabel = 'effective scattering length [m]')

plt.show()


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

import boto3

import pyramid.httpexceptions as exc

'''
CREATE a table
--------------

import time
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, GlobalKeysOnlyIndex

table = Table.create(shorturl, schema=[
    HashKey('url_short'),
], throughput={
    'read': 18,
    'write': 18,
},
global_indexes=[
    GlobalKeysOnlyIndex('UrlIndex', parts=[
        HashKey('url')
    ], throughput={
        'read': 18,
        'write': 18
    }),
])
time.sleep(30)

DROP a table
------------

from boto.dynamodb import connect_to_region

conn = connect_to_region(region_name='eu-west-1')
table=conn.get_table('shorturl')
table.delete()

'''

class DynamodbConnection:
    # This is a singleton. This is nice.
    def __init__(self, region='eu-west-1'):
        self.conn = None
        self.region = region

    def get(self):
        if self.conn is None:
            try:
                self.conn = boto3.resource('dynamodb', region_name=self.region)
            except Exception as e:  # pragma: no cover
                raise exc.HTTPBadRequest(
                    'DynamoDB: Error during connection init %s' % e)
        return self.conn

class S3Connect:
    def __init__(self, region='eu-west-1'):
        self.conn = None
        self.region = region

    def get(self):
        if self.conn is None:
            try:
                # Cannot use bucket names with dots
                # see: https://github.com/boto/boto/issues/2836
                self.conn = boto3.client('s3', region_name=self.region)
            except Exception as e:  # pragma: no cover
                raise exc.HTTPInternalServerError(
                    'S3: Error during connection init %s' % e)
        return self.conn

dynamodb_connection = DynamodbConnection()
s3_connection = S3Connect()

def get_dynamodb_table(table_name='shorturl', region='eu-west-1'):
    dyn = dynamodb_connection
    dyn.region = region
    conn = dyn.get()
    try:
        table = conn.Table(table_name)
    except Exception as e:  # pragma: no cover
        raise exc.HTTPInternalServerError(
            'DynamoDB: Error during connection to the table %s\n%s' % (
                table_name, e))
    return table

def get_file_from_bucket(bucket_name, file_name):
    conn = s3_connection.get()
    try:
        response = conn.get_object(Bucket=bucket_name,
                                   Key=file_name)
    except Exception as e:
        raise exc.HTTPInternalServerError("bucket (%s) or file (%s) not valids. \n%s" % (bucket_name, file_name, e))
    return response

def delete_file_in_bucket(bucket_name, file_name):
    return s3_connection.get().delete_object(Bucket=bucket_name,
                                             Key=file_name)

def upload_object_to_bucket(bucket_name, file_id, mime, content_encoding, data, cache_control):
    conn = s3_connection.get()
    return conn.put_object(
        Body=data,
        Key=file_id,
        ContentType=mime,
        ContentEncoding=content_encoding,
        CacheControl=cache_control,
        Bucket=bucket_name
    )


import numpy as np
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Lipinski
from rdkit.Chem import Descriptors
from rdkit.DataStructs import FingerprintSimilarity, ConvertToNumpyArray

def clean_mol(smiles):
    """
    Construct a molecule from a SMILES string, removing stereochemistry and
    explicit hydrogens, and setting aromaticity.
    """
    mol = Chem.MolFromSmiles(str(smiles), sanitize=64)
    if mol is None:
        raise ValueError("Invalid SMILES")
    Chem.RemoveStereochemistry(mol)
    Chem.SanitizeMol(mol)
    mol = Chem.RemoveHs(mol)
    return mol

def clean_mols(all_smiles):
    """
    Construct a list of molecules from a list of SMILES strings, replacing
    invalid molecules with None in the list.
    """
    mols = []
    for smiles in all_smiles:
        try:
            mol = clean_mol(smiles)
            mols.append(mol)
        except ValueError:
            mols.append(None)
    return mols

def in_Ro5(mol):
    """
    Test whether a molecule is in Lipinski "Rule of 5" space, meaning
    - 5 or fewer H bond donors
    - 10 or fewer H bond acceptors
    - MW < 500 Da
    - logP < 5
    """

    h_donor = Lipinski.NumHDonors(mol)
    h_accept = Lipinski.NumHAcceptors(mol)
    mw = Descriptors.MolWt(mol)
    logP = Descriptors.MolLogP(mol)

    Ro5 = h_donor <= 5 and h_accept <= 10 and mw <= 500 and logP < 5
    return(Ro5)

def get_ecfp6_fingerprints(mols):
    """
    Get ECFP6 fingerprints for a list of molecules which may include `None`s,
    gracefully handling `None` values by returning a `None` value in that
    position.
    """
    fps = []
    for mol in mols:
        if mol is None:
            fps.append(None)
        else:
            fp = AllChem.GetMorganFingerprintAsBitVect(mol, 3, nBits=1024)
            fps.append(fp)
    return(fps)

def get_bit_vector(fp):
    arr = np.zeros((1,))
    ConvertToNumpyArray(fp, arr)
    return(arr)

def get_tanimoto(list1, list2):
    tcs = []
    for fp1 in list1:
        for fp2 in list2:
            if fp1 is None or fp2 is None:
                tcs.append(None)
            else:
                tc = FingerprintSimilarity(fp1, fp2)
                tcs.append(tc)
    return(tcs)


import sys
from settings import Settings
from ship import Ship
from pygame.sprite import Group
import game_functions as gf
from alien import Alien
from game_stats import GameStats

import pygame

def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    # Set the background Color
    bg_color = (230,230,230)

    # Make a ship, a group of bullets, and a group of aliens.
    ship = Ship(ai_settings, screen)
    bullets = Group()
    aliens = Group()

    # Create the fleet of aliens
    gf.create_fleet(ai_settings,screen,ship,aliens)

    # Create an instance to store game statistics.
    stats = GameStats(ai_settings)

    # Start the main loop for the game
    while True:
        # Watch for keyboard and mouse events
        gf.check_events(ai_settings,screen,ship,bullets)

        if stats.game_active:
            ship.update()

            # Make the most recently drawn screen visible
            gf.update_bullets(ai_settings,screen,ship,aliens,bullets)
            gf.update_aliens(ai_settings,stats,ship,screen,aliens,bullets)

        gf.update_screen(ai_settings,screen,ship,aliens,bullets)

run_game()


from . import main
from flask import render_template, url_for, request, redirect
from .forms import PostForm
from ..models import Post, db

@main.route('/', methods=['GET', 'POST'])
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    img_url='/static/img/' + form.img_url.data,
                    description=form.description.data,
                    download_link=form.download_link.data)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = Post.query.order_by(Post.timestamp.desc()).paginate(page, 10, error_out=False)
    posts = pagination.items
    return render_template('main/index.html', form=form, posts=posts, pagination=pagination, endpoint='main.index')

@main.route('/hot')
def hot():
    page = request.args.get('page', 1, type=int)
    pagination = Post.query.order_by(Post.download_count.desc()).paginate(page, 10, error_out=False)
    posts = pagination.items
    return render_template('main/hot.html', posts=posts, pagination=pagination, endpoint='main.hot')

@main.route('/download/<int:post_id>')
def download(post_id):
    post = Post.query.get_or_404(post_id)
    post.download_count += 1
    db.session.add(post)
    db.session.commit()
    return redirect(post.download_link)


from typing import List, Tuple

import hummingbot
from hummingbot.core.utils.symbol_fetcher import SymbolFetcher
from hummingbot.strategy.discovery.discovery_config_map import discovery_config_map
from hummingbot.strategy.discovery.discovery import DiscoveryMarketPair, DiscoveryStrategy

def start(self: "hummingbot.client.hummingbot_application.HummingbotApplication"):
    try:
        market_1 = discovery_config_map.get("primary_market").value.lower()
        market_2 = discovery_config_map.get("secondary_market").value.lower()
        target_symbol_1 = list(discovery_config_map.get("target_symbol_1").value)
        target_symbol_2 = list(discovery_config_map.get("target_symbol_2").value)
        target_profitability = float(discovery_config_map.get("target_profitability").value)
        target_amount = float(discovery_config_map.get("target_amount").value)
        equivalent_token: List[List[str]] = list(discovery_config_map.get("equivalent_tokens").value)

        if not target_symbol_2:
            target_symbol_2 = SymbolFetcher.get_instance().symbols.get(market_2, [])
        if not target_symbol_1:
            target_symbol_1 = SymbolFetcher.get_instance().symbols.get(market_1, [])

        market_names: List[Tuple[str, List[str]]] = [(market_1, target_symbol_1), (market_2, target_symbol_2)]
        target_base_quote_1: List[Tuple[str, str]] = self._initialize_market_assets(market_1, target_symbol_1)
        target_base_quote_2: List[Tuple[str, str]] = self._initialize_market_assets(market_2, target_symbol_2)

        self._trading_required = False
        self._initialize_wallet(token_symbols=[])  # wallet required only for dex hard dependency
        self._initialize_markets(market_names)

        self.market_pair = DiscoveryMarketPair(
            *(
                [self.markets[market_1], self.markets[market_1].get_active_exchange_markets]
                + [self.markets[market_2], self.markets[market_2].get_active_exchange_markets]
            )
        )
        self.strategy = DiscoveryStrategy(
            market_pairs=[self.market_pair],
            target_symbols=target_base_quote_1 + target_base_quote_2,
            equivalent_token=equivalent_token,
            target_profitability=target_profitability,
            target_amount=target_amount,
        )
    except Exception as e:
        self._notify(str(e))
        self.logger().error("Error initializing strategy.", exc_info=True)


#!/usr/bin/env python3

"""
Programme calculant le volume d'un cylindre

Par Éthan Leduc
"""

import argparse
import math

def parse_args() -> argparse.Namespace:
    """Parse les arguments entrés nécéssaire au fonctionnement du script"""
    parser = argparse.ArgumentParser(description="Calculateur de volume pour cylindre -- ©2020, par Éthan Leduc")
    parser.add_argument('-r', '--rayon', type=float, metavar='R', required=True, help='Rayon du cylindre')
    parser.add_argument('-H', '--hauteur', type=float, metavar='H', required=True, help='Heuteur du cylindre')
    parser.add_argument('-p', '--précision', type=int, metavar='P', required=False, help='Précision du calcul')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-q', '--quiet', action='store_true', help='Afficher seulement le volume')
    group.add_argument('-v', '--verbeux', action='store_true', help='Afficher le maximum d\'info')
    return parser.parse_args()

def volume_cylindre(rayon: float, hauteur: float) -> float:
    """Formule calculant le volume du cylindre"""
    return math.pi * (rayon ** 2) * hauteur

def main() -> None:
    """Fonction principale"""
    args = parse_args()
    volume = volume_cylindre(args.rayon, args.hauteur)
    if args.précision is not None:
        format_string = "{:." + str(args.précision) + "f}"
        volume = format_string.format(volume)
    if args.quiet:
        print(volume)
    elif args.verbeux:
        print(f"Volume du cylindre de hauteur {args.hauteur} et de rayon {args.rayon} selon EL: {volume}")
    else:
        print(f"Volume du cylindre selon EL: {volume}")
    return

if __name__ == '__main__':
    main()


import numpy as np
import matplotlib.pyplot as pl
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(1)

x_min = 4
x_max = 30
X = 5 + 25 * np.random.rand(16)
T = 170 - 108 * np.exp(-0.2 * X) + 4 * np.random.randn(16)

# Mean square error function
def MeanSquareError(x, t, w):
    y = w[0] * x + w[1]
    out = np.mean((t - y) ** 2)

    return out

def DeviationMeanSquareError(x, t, w):
    Y = w[0] * x + w[1]
    dW0 = 2 * np.mean((Y - t) * x)
    dW1 = 2 * np.mean(Y - t)

    return dW0, dW1

def Fit(x, t):
    alpha - .001
    iterations = 100000
    eps = .1
    w = np.zeros([iterations, 2])
    w[0, : ] = [10.0, 165.0]

    for index in range(1, iterations):
        out = DeviationMeanSquareError(x, t, w[index - 1])
        w[index, 0] = w[index - 1, 0] - alpha * out[0]
        w[index, 1] = w[index - 1, 1] - alpha * out[1]

        if max(np.absolute(out)) < eps:
           break

    w0 = w[index, 0]
    w1 = w[index, 1]
    w = w[ : index, : ]

    return w0, w1, out, w

if __name__ == '__main__':
   alpha = .001
   N = 100
   x0 = np.linspace(-25, 25, N)
   x1 = np.linspace(120, 170, N)
   X0, X1 = np.meshgrid(x0, x1)
   Y = np.zeros((len(x0), len(x1)))

   for i in range(N):
       for j in range(N):
           Y[j, i] = MeanSquareError(X, T, (x0[i], x1[j]))

   pl.figure(figsize = (9.5, 4))
   pl.subplots_adjust(wspace = .5)

   # subplot(nrows, ncols, index, **kwars)
   axis = pl.subplot(1, 2, 1, projection = '3d')
   axis.plot_surface(X0, X1, Y, rstride = 10, cstride = 10, alpha = .3,
     color = 'blue', edgecolor = 'black')
   axis.set_xticks([-20, 0, 20])
   axis.set_yticks([120, 140, 160])
   axis.view_init(20, -40)

   pl.subplot(1, 2, 2)
   cont =pl.contour(X0, X1, Y, 30, color = 'black',
     levels = [100, 1000, 10000, 100000])
   cont.clabel(fmt = '%1.0f', fontsize = 8)
   pl.grid(True)
   pl.show()

   # Gradient descent
   w0, w1, out, w = Fit(X, T)

   print('Number of iterations: ' + str(w.shape[0]))
   print('w = [' + str(w0) + ', ' + str(w1) + ']')
   print('Deviation = [' + str(out[0]) + ', ' + str(out[1]) + ']')
   print('Mean square error = ' + str(MeanSquareError(X, T, [w0, w1])))

   pl.plot(w[:, 0], w[:, 1], '.-', color = 'gray', markersize = 10,
     markeredgecolor = 'cornflowerblue')

   pl.show()


import logging

from pitop.battery import Battery
from pitop.common.pt_os import get_pitopOS_info
from pitop.common.sys_info import get_pi_top_ip
from pt_miniscreen.core import Component
from pt_miniscreen.core.components import Text
from pt_miniscreen.core.utils import apply_layers, layer, rectangle

from ...backend.helpers.device import firmware_version

logger = logging.getLogger(__name__)

FONT_SIZE = 14
SIZE = (128, 64)
TEXT_POS = (0, 0)

class DetailedInstructionsPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.text_component = self.create_child(
            Text,
            text="Are you stuck?\nGo to\npi-top.com/start-4",
            wrap=False,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class BatteryInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.battery_instance = Battery()

        self.text_component = self.create_child(
            Text,
            text=self.text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    @property
    def text(self):
        def _power_source_text():
            if self.battery_instance.is_full or self.battery_instance.is_charging:
                return "Power Adapter"
            return "Battery"

        return (
            f"Battery: {self.battery_instance.capacity}%\n"
            f"Power Source: {_power_source_text()}"
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class OsInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        build_info = get_pitopOS_info()
        text = (
            f"pi-topOS v{build_info.build_os_version}\n"
            f"{build_info.build_type}\n" + f"{build_info.build_date}"
        )

        self.text_component = self.create_child(
            Text,
            text=text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class FwInfoPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        build_info = get_pitopOS_info()
        text = (
            f"pi-top Firmware: {firmware_version()}\n"
            + f"Schema: {build_info.schema_version}\n"
            + f"Run: {build_info.build_run_number}\n"
            + f"#: {build_info.build_commit}"
        )

        self.text_component = self.create_child(
            Text,
            text=text,
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )

class NetworksPage(Component):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.text_component = self.create_child(
            Text,
            text=f"IP address:\n{get_pi_top_ip()}",
            font_size=FONT_SIZE,
            align="center",
            vertical_align="center",
            fill=0,
        )

    def render(self, image):
        return apply_layers(
            image,
            [
                layer(rectangle, size=SIZE, pos=(0, 0)),
                layer(
                    self.text_component.render,
                    size=SIZE,
                    pos=TEXT_POS,
                ),
            ],
        )


"""
Usage:
conda activate dih4cps-large
pip install gi

sudo apt-get update
sudo apt-get install gir1.2-gst-rtsp-server-1.0

Source:
https://www.semicolonworld.com/question/62502/how-to-convert-a-video-on-disk-to-a-rtsp-stream
"""

import sys
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib

loop = GLib.MainLoop()
Gst.init(None)

class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self):
        GstRtspServer.RTSPMediaFactory.__init__(self)

    def do_create_element(self, url):
        #set mp4 file path to filesrc's location property
        src_demux = "filesrc location=/path/to/dir/test.mp4 ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        #uncomment following line if video transcoding is necessary
        #h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
        pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
        print ("Element created: " + pipeline)
        return Gst.parse_launch(pipeline)

class GstreamerRtspServer():
    def __init__(self):
        self.rtspServer = GstRtspServer.RTSPServer()
        factory = TestRtspMediaFactory()
        factory.set_shared(True)
        mountPoints = self.rtspServer.get_mount_points()
        mountPoints.add_factory("/stream1", factory)
        self.rtspServer.attach(None)

if __name__ == '__main__':
    s = GstreamerRtspServer()
    loop.run()


"""
Author: Sanam Jena
CWID:10454295
Date: 08 November 2020
Objective: In this assignment is to work on datetime,files & prettytable
"""

from typing import Tuple, Iterator, List, IO, Dict
from datetime import timedelta, datetime
from prettytable import PrettyTable
import os

def date_arithmetic() -> Tuple[datetime, datetime, int]:
    """In this function, we will be answering the following questions:
    1. What is the date three days after Feb 27, 2020?
    2. What is the date three days after Feb 27, 2019?
    3. How many days passed between Feb 1, 2019 and Sept 30, 2019?

    Returns:
        A tuple with three values in the following order:
        1. An instance of  class datetime representing the date three days
        after Feb 27, 2020.
        2. An instance of  class datetime representing the date three days
        after Feb 27, 2019.
        3. An int representing the number of days between Feb 1, 2019 and
        Sept 30, 2019
    """
    three_days_after_02272020: datetime = datetime(
        2020, 2, 27) + timedelta(days=3)
    three_days_after_02272019: datetime = datetime(
        2019, 2, 27) + timedelta(days=3)
    days_passed_02012019_09302019: int = (
        datetime(2019, 9, 30) - datetime(2019, 2, 1)).days

    return (
        three_days_after_02272020,
        three_days_after_02272019,
        days_passed_02012019_09302019)

def file_reader(path: str, fields: int, sep: str = ',',
                header: bool = False) -> Iterator[List[str]]:
    """In this function, we will implement a generator that will yield
    new line of a file on call of next

    Args:
        path (str): File Path
        fields (int): fields
        sep (str, optional): Separator. Defaults to ','.
        header (bool, optional): Defaults to False.

    Yields:
        Iterator[Tuple[str]]
    """
    try:
        fp: IO = open(path, 'r')
    except FileNotFoundError:
        raise FileNotFoundError(f"Cannot open file at {path} ")
    else:
        line_number: int = 0
        for line in fp:
            row: List[str] = line.strip().split(sep)
            line_number += 1
            if len(row) != fields:
                fp.close()
                raise ValueError(
                    f"File {path} at line {line_number} has  {len(row)} items, whereas the \
                    expected items where {fields} ")
            if header is False:
                yield row
            else:
                header = False

class FileAnalyzer:
    """given a directory name, searches that directory for Python files
    (i.e. files ending with .py). For each .py file in the directory, open
    each file and calculate a summary of the file including:
         1.the file name
         2.the total number of lines in the file
         3.the total number of characters in the file
         4.the number of Python functions (lines that begin with ‘def ’,
         including methods inside class definitions)
         5.the number of Python classes (lines that begin with ‘class ’)
    Discussed with Rajat Verma & Tehreem Tungekar
    """

    def __init__(self, directory: str) -> None:
        """ In this method, we are initializing the class
        """
        self.directory: str = directory  # NOT mandatory!
        self.files_summary: Dict[str, Dict[str, int]] = dict()
        self.analyze_files()  # summerize the python files data

    def analyze_files(self) -> None:
        """ In this method, we will be summarizing and storing the data
        into self.files_summary
        """
        path: str = self.directory
        direct: List[str] = os.listdir(path)
        for f in direct:
            if f.endswith('.py'):
                try:
                    fp = open(os.path.join(path, f), 'r')
                except FileNotFoundError:
                    raise FileNotFoundError(f"File {f} cannot be openend")
                else:
                    with fp:
                        character_count: int = 0
                        class_count: int = 0
                        function_count: int = 0
                        line_count: int = 0

                        for line in fp:
                            character_count += len(line)
                            line_count += 1

                            if line.strip().startswith('def '):
                                function_count += 1
                            if line.startswith('class '):
                                class_count += 1

                        self.files_summary[f] = {
                            'classes': class_count,
                            'functions': function_count,
                            'lines': line_count,
                            'characters': character_count
                        }

    def pretty_print(self) -> None:
        """In this function, we will print the summarized detail into
        a table using prettytable module
        """
        output: PrettyTable = PrettyTable()
        output.field_names = ['File Name', 'Classes',
                              'Functions', 'Lines', 'Characters']

        for i, j in self.files_summary.items():
            output.add_row([i, j['classes'], j['functions'],
                            j['lines'], j['characters']])

        print("\nSummary for ", self.directory)
        print(output)


import sys
import pygame
from time import sleep
from alien import Alien
from bullet import Bullet
from settings import Settings
from ship import Ship
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

class AlienInvasion:
    """""Clase para manejar los assets del juego y su comportamiento"""

    def __init__(self):
        """""Inicia el juego y crea los recursos"""

        # Inicia pygame.
        pygame.init()

        # Incia Settings de la clase settings
        self.settings = Settings()

        # Inicia pantalla del juego.
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        #self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        #self.settings.screen_height = self.screen.get_rect().height
        #self.settings.screen_width = self.screen.get_rect().width

        # Pone el nombre en la barra de titulos.
        pygame.display.set_caption("Alien Invasion")

        # Instancia de las estadisticas del juego
        self.stats = GameStats(self)

        # Instancia del Scoreboard
        self.sb = Scoreboard(self)

        # Instancia de la nave del juego.
        self.ship = Ship(self)

        # Crea un grupo de sprites que almacenara las balas.
        self.bullets = pygame.sprite.Group()

        # Crea un grupo de aliens para crear la flota.
        self.aliens = pygame.sprite.Group()
        self._create_fleet_()

        # Crear el boton "Play"
        self.play_button = Button(self, "Play")

        # Fija color de fondo para la pantalla.
        self.backgroung_color = (self.settings.background_color)

############################################### RUN GAME ########################################################

    def run_game(self):
        """""Inicia el Main Loop"""

        while True:
            # Responde a los eventos de teclado o mouse.
            self._check_events()

            if self.stats.game_active:

                # Actualiza la posicion de la nave cada vuelta de bucle.
                self.ship.update()

                # Actualiza las balas cada vuelta de bucle.
                self._update_bullets()

                # Actualiza la posicion de los aliens.
                self._update_aliens()

            # Cada vuelta de bucle pinta la pantalla.
            self._update_screen()

############################################### EVENTOS ########################################################

    def _check_events(self):
        """"Responde a enventos de teclado o mouse"""

        # Salir
        for event in pygame.event.get():
            if event.type == pygame.QUIT:

                sys.exit()

            # Tecla presionada.
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)

            # Tecla liberada.
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_keydown_events(self, event):
        """Responde cuando se presiona una tecla"""

        if event.key == pygame.K_RIGHT:  # Movimiento a la derecha
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:  # Movimiento a la izquierda
            self.ship.moving_left = True
        elif event.key == pygame.K_ESCAPE:
            sys.exit()
        elif event.key == pygame.K_SPACE: # Dispara la bala
            self._fire_bullet()

    def _check_keyup_events(self, event):
        """Responde cuando se libera una tecla"""

        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def _check_play_button(self, mouse_pos):
        """Empieza un nuevo juego cuando el jugador
        presiona el boton "Play" """

        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:

            # Resetear los ajustes del juego.
            self.settings.initialize_dynamic_settings()

            # Resetear las estadisticas del juego.
            self.stats.game_active = True

            # Resetea los stats
            self.stats.reset_stats()

            # Resetear Score.
            self.stats.score = 0
            self.sb.prep_score()
            self.stats.level = self.stats.level
            self.sb.prep_level()
            self.stats.ships_left = self.settings.ship_limit
            self.sb.prep_ships()

            # Quitar todas las balas y aliens.
            self.aliens.empty()
            self.bullets.empty()

            # Crear una nueva flota y centrar la nave.
            self._create_fleet_()
            self.ship.center_ship()

            # Esconder el cursor del mouse.
            pygame.mouse.set_visible(False)

############################################### BULLETS ########################################################

    def _fire_bullet(self):
        """Crea una nueva bala y la agrega al grupo de ballas"""

        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        """Actualizar la posicion de las balas y deshacerse de las viejas siempre ingratas"""

        # Actualizar la posicion de las balas.
        self.bullets.update()

        # Deshacerse de las balas que desparecen de la pantalla.
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

        # Llama al metodo en caso de detectar una colision.
        self._check_bullet_alien_collision()

    def _check_bullet_alien_collision(self):
        """Responder a una colision de balas y aliens"""

        # Remover cualquier alien y bala que haya colisionado.
        collisions = pygame.sprite.groupcollide(
            self.bullets, self.aliens, True, True)

        # Agregar puntaje por cada alien cruelmente asesinado.
        if collisions:
            for aliens in collisions.values():
                self.stats.score += self.settings.aliens_point * len(aliens)
            self.sb.prep_score()

        # En caso de tener la lista de alien vacia.
        # Repopular la flota de aliens.
        if not self.aliens:
            self.bullets.empty()
            self._create_fleet_()
            self.settings.increase_speed()

            # Incrementar el nivel.
            self.stats.level += 1
            self.sb.prep_level()

################################################## NAVE ##########################################################

    def _ship_hit(self):
        """Responde cuando una nave es impactada por un alien"""

        # Si el numero de naves es mayor a 0
        # Se restablece el juego
        if self.stats.ships_left > 0:

            # Reduce el numero de naves restantes
            self.stats.ships_left -= 1

            # Actualiza el numero de naves del Scoreboard.
            self.sb.prep_ships()

            # Se deshace de cualquier alien y bala sobrante
            self.aliens.empty()
            self.bullets.empty()

            # Se crea una nueva flota y se centra la nave
            self._create_fleet_()
            self.ship.center_ship()

            # Pausa
            sleep(0.8)

        # Si el numero de naves es menor a 0
        # El estado del juego pasa a False
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)

############################################## ALIEN FLEET #######################################################

    def _create_fleet_(self):
        """Crear la flota de aliens invasores como cerebros"""

        # ***alien por fila*** Crear un alien y encontrar el numero de aliens que entran en una fila.
        # El espacio entre cada alien es igual al ancho de un alien.
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        avalible_space_x = self.settings.screen_width - (2 * alien_width)
        number_aliens_x = avalible_space_x // (2 * alien_width)

        # ****alien por columna*** Determina el numero de filas de aliens que entran en la pantalla
        ship_height = self.ship.rect.height
        avalible_space_y = (self.settings.screen_height - (3 * alien_height) - ship_height)
        numbers_rows = avalible_space_y // (2 * alien_height)

        # Crear la flota llena de aliens.
        for row_number in range(numbers_rows):
            for alien_number in range(number_aliens_x):
                #Invoca al metodo create_alien()
                self._create_alien(alien_number, row_number)

    def _create_alien(self, alien_number, row_number):
        """Crea un solo alien"""

        # Crear un alien y colocarlo en la fila
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
        self.aliens.add(alien)

    def _check_fleet_edges(self):
        """Responde apropiadamente cuando algun alien toca el borde de la pantalla"""

        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        """Baja la flota de aliens y cambia su direccion"""

        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed # Ajusta la coordenada y
        self.settings.fleet_direction *= -1 #Cambia la direccion

    def _check_aliens_bottom(self):
        """Verificar si algun alien ha tocado el fondo de la pantalla"""

        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break

    def _update_aliens(self):
        """Actualizar la posicion de todos los aliens en la flota"""

        self._check_fleet_edges() # Verifica si algun alien ha llegado al borde la pantalla
        self.aliens.update() # Llama al metodo update de aline para la coordenada x

        # Buscar colisiones entre los aliens y la nave.
        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            self._ship_hit()

        #Buscar por aliens que toquen el fondo de la pantalla
        self._check_aliens_bottom()

############################################# UPDATE SCREEN ######################################################

    def _update_screen(self):
        """Actualiza la imagen en la pantalla"""

        # Fija el color de fondo para la pantalla.
        self.screen.fill(self.backgroung_color)

        # Dibuja la nave en la pantalla.
        self.ship.blitme()

        # Recorre el grupo de balas y las dibuja
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()

        # Dibuja los aliens.
        self.aliens.draw(self.screen)

        # Dibuja el Scoreboard.
        self.sb.show_score()

        # Dibujar el HighScore.
        self.sb.check_high_score()

        # Dibujar boton "Play" si el juego esta inactivo.
        if not self.stats.game_active:
            self.play_button.draw_button()

        # Hacer visible el ultimo dibujo de pantalla.
        pygame.display.flip()

################################################## MAIN #############################################################

if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()


"""
Copyright 2017 Neural Networks and Deep Learning lab, MIPT

Licensed inder the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import spacy
import re

from deeppavlov.core.models.inferable import Inferable
from deeppavlov.core.common.registry import register

@register('spacy_tokenizer')
class SpacyTokenizer(Inferable):

    def __init__(self, save_path=None, **kwargs):
        self.NLP = spacy.load('en')
        super().__init__(save_path=save_path, **kwargs)

    def _tokenize(self, text, **kwargs):
        """Tokenize with spacy, placing service words as individual tokens."""
        return [t.text for t in self.NLP.tokenizer(text)]

    def _detokenize(self, tokens):
        """
        Detokenizing a text undoes the tokenizing operation, restoring
        punctuation and spaces to the places that people expect them to be.
        Ideally, `detokenize(tokenize(text))` should be identical to `text`,
        except for line breaks.
        """
        text = ' '.join(tokens)
        step0 = text.replace('. . .',  '...')
        step1 = step0.replace("`` ", '"').replace(" ''", '"')
        step2 = step1.replace(" ( ", " (").replace(" ) ", ") ")
        step3 = re.sub(r' ([.,:;?!%]+)([ \'"`])', r"\1\2", step2)
        step4 = re.sub(r' ([.,:;?!%]+)$', r"\1", step3)
        step5 = step4.replace(" '", "'").replace(" n't", "n't")\
            .replace(" nt", "nt").replace("can not", "cannot")
        step6 = step5.replace(" ` ", " '")
        return step6.strip()

    def infer(self, x):
        if isinstance(x, str):
            return self._tokenize(x)
        if isinstance(x, list):
            return self._detokenize(x)
        raise TypeError("SpacyTokenize.infer() not implemented for `{}`"\
                        .format(type(x)))


# bot.py
import os

import discord
from discord.ext import tasks, commands

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD_ID = int(os.getenv('DISCORD_GUILD'))

class SmashBotSpain(commands.Bot):
    VERSION =  "v1.1"

    def __init__(self, command_prefix, intents):
        super().__init__(command_prefix=command_prefix, intents=intents)
        self.help_command = None

    async def on_ready(self):
        self.guild = self.get_guild(GUILD_ID)

        print(
            f'{client.user} is connected to the following guild:\n'
            f'{self.guild.name}(id: {self.guild.id})'
        )

        matchmaking = self.get_cog('Matchmaking')
        flairing = self.get_cog('Flairing')
        await matchmaking.setup_matchmaking(guild=self.guild)
        await flairing.setup_flairing(guild=self.guild)

intents = discord.Intents.default()  # All but the two privileged ones
intents.members = True

client = SmashBotSpain(command_prefix=["."], intents=intents)
client.load_extension("extensions.member_nickname")
client.load_extension("cogs.Matchmaking")
client.load_extension("cogs.HelpCommands")
client.load_extension("cogs.Flairing")

client.run(TOKEN)


#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

t = np.linspace(-1, 1, 1000)

x = 1-(t**2)
y = 2*t
z = 1+(t**2)

x_2d = x/z
y_2d = y/z
z_2d = np.ones_like(y_2d)

x_3d = x
y_3d = y
z_3d = z

x_plot = np.concatenate([x_2d, x_3d])
y_plot = np.concatenate([y_2d, y_3d])
z_plot = np.concatenate([z_2d, z_3d])

fig = plt.figure(); ax = fig.add_subplot(1, 1, 1, projection='3d');
ax.set_xlabel('x'); ax.set_ylabel('y'); ax.set_zlabel('z')
ax.scatter(x_plot, y_plot, z_plot, c='b', marker='o')

plt.show(block=True)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pyautogui as py

class whatsappBot:
    def __init__(self,victim,msg,num):
        self.victim=victim
        self.msg=msg
        self.num=num
        self.bot=webdriver.Firefox()

    def login(self):
        bot=self.bot
        bot.get('https://web.whatsapp.com/')
        pause=input('Press Anything To Continue!')
        time.sleep(2)

        vic=bot.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/div[1]/div/label/div/div[2]')
        print('hello')
        vic.send_keys(self.victim)
        vic.send_keys(Keys.ENTER)
        time.sleep(2)
        elem=bot.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]')

        elem.send_keys('Script Time.Chill.And Not Sorry. #B')
        elem.send_keys(Keys.ENTER)

        y=self.msg
        for i in range(0,self.num):
            elem.send_keys(y)
            elem.send_keys(Keys.ENTER)
            if(i%100==0):
                print(i)

print('Author-@b\n')

victim=input('Enter The Contact (Exact) : ')
msg=input('Enter Your Message : ')
num=int(input('Enter The Number Of Times You Want To Send: '))
print('Login Now And Enjoy.#B')

plan=whatsappBot(victim,msg,num)

plan.login()


import argparse
import logging
import os

import cv2 as cv
import torch

from config import max_target_len, dict, converter

def clip_gradient(optimizer, grad_clip):
    """
    Clips gradients computed during backpropagation to avoid explosion of gradients.
    :param optimizer: optimizer with the gradients to be clipped
    :param grad_clip: clip value
    """
    for group in optimizer.param_groups:
        for param in group['params']:
            if param.grad is not None:
                param.grad.data.clamp_(-grad_clip, grad_clip)

def save_checkpoint(epoch, epochs_since_improvement, model, optimizer, hmean, is_best):
    state = {'epoch': epoch,
             'epochs_since_improvement': epochs_since_improvement,
             'hmean': hmean,
             'model': model,
             'optimizer': optimizer}
    # filename = 'checkpoint_' + str(epoch) + '_' + str(loss) + '.tar'
    filename = 'checkpoint.tar'
    torch.save(state, filename)
    # If this checkpoint is the best so far, store a copy so it doesn't get overwritten by a worse checkpoint
    if is_best:
        torch.save(state, 'BEST_checkpoint.tar')

class AverageMeter(object):
    """
    Keeps track of most recent, average, sum, and count of a metric.
    """

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def adjust_learning_rate(optimizer, shrink_factor):
    """
    Shrinks learning rate by a specified factor.
    :param optimizer: optimizer whose learning rate must be shrunk.
    :param shrink_factor: factor in interval (0, 1) to multiply learning rate with.
    """

    print("\nDECAYING learning rate.")
    for param_group in optimizer.param_groups:
        param_group['lr'] = param_group['lr'] * shrink_factor
    print("The new learning rate is %f\n" % (optimizer.param_groups[0]['lr'],))

def get_learning_rate(optimizer):
    return optimizer.param_groups[0]['lr']

def accuracy(inputs, input_lengths, labels, batch_size):
    n_correct = 0
    # print(preds.size())
    _, inputs = inputs.max(2)
    # print(preds.size())
    # preds = preds.squeeze(2)
    inputs = inputs.transpose(1, 0).contiguous().view(-1)
    sim_preds = converter.decode(inputs.data, input_lengths.data, raw=False)
    for pred, target in zip(sim_preds, labels):
        if pred == target:
            n_correct += 1
    accuracy = n_correct / float(batch_size)
    return accuracy

def parse_args():
    parser = argparse.ArgumentParser(description='Train CRNN network')
    # general
    parser.add_argument('--optimizer', default='adam', help='optimizer')
    parser.add_argument('--batch-size', type=int, default=64, help='batch size')
    parser.add_argument('--lr', type=float, default=0.01, help='start learning rate')
    parser.add_argument('--end-epoch', type=int, default=1000, help='training epoch size.')

    # optimizer
    parser.add_argument('--k', default=0.2, type=float,
                        help='tunable scalar multiply to learning rate')
    parser.add_argument('--warmup_steps', default=4000, type=int,
                        help='warmup steps')

    parser.add_argument('--checkpoint', type=str, default=None, help='checkpoint')
    args = parser.parse_args()
    return args

def get_logger():
    logger = logging.getLogger()
    handler = logging.StreamHandler()
    formatter = logging.Formatter("%(asctime)s %(levelname)s \t%(message)s")
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    return logger

def draw_str(dst, target, s):
    x, y = target
    cv.putText(dst, s, (x + 1, y + 1), cv.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2, lineType=cv.LINE_AA)
    cv.putText(dst, s, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv.LINE_AA)

def ensure_folder(folder):
    if not os.path.exists(folder):
        os.makedirs(folder)

def encode_target(target):
    return [dict[c] for c in target] + [0] * (max_target_len - len(target))

def get_images_for_test():
    from config import annotation_files
    split = 'test'
    print('loading {} annotation data...'.format('test'))
    annotation_file = annotation_files[split]
    with open(annotation_file, 'r') as file:
        lines = file.readlines()

    image_paths = [line.split(' ')[0] for line in lines]
    return image_paths


import re
from collections import defaultdict
from dotty_dict import dotty

from loader.src.config.logger import create_logger

logger = create_logger("reference_binder")

# dotty-dict does not handle brackets indices,
# it uses dots instead (a.0.b instead of a[0].b)
def dotty_paths(paths):
    for path in paths:
        yield re.sub(r"\[(\d+)\]", r".\1", path)

def partial_identifier(identifier):
    (
        value,
        system,
        identifier_type_code,
        identifier_type_system,
    ) = ReferenceBinder.extract_key_tuple(identifier)
    if value:
        return {"identifier.value": value, "identifier.system": system}
    else:
        return {
            "identifier.type.coding.0.code": identifier_type_code,
            "identifier.type.coding.0.system": identifier_type_system,
        }

class ReferenceBinder:
    def __init__(self, fhirstore):
        self.fhirstore = fhirstore

        # cache is a dict of form
        # {
        #   (fhir_type_target, (value, system, code, code_system)):
        #           {(fhir_type_source, path, isArray): [fhir_id1, ...]},
        #   (fhir_type_target, (value, system, code, code_system)):
        #           {(fhir_type_source, path, isArray): [fhir_id]},
        #   ...
        # }
        # eg:
        # {
        #   (Practitioner, 1234, system): {(Patient, generalPractitioner, True): [fhir-pract-id]},
        #   ...
        # }

        self.cache = defaultdict(lambda: defaultdict(list))

    def resolve_references(self, unresolved_fhir_object, reference_paths):
        fhir_object = dotty(unresolved_fhir_object)

        # iterate over the instance's references and try to resolve them
        for reference_path in dotty_paths(reference_paths):
            logger.debug(
                f"Trying to resolve reference for resource {fhir_object['id']} "
                f"at {reference_path}"
            )
            try:
                bound_ref = self.bind_existing_reference(fhir_object, reference_path)
                fhir_object[reference_path] = bound_ref
            except Exception as e:
                logger.warning(
                    "Error while binding reference for instance "
                    f"{fhir_object} at path {reference_path}: {e}"
                )

        if "identifier" in fhir_object:
            self.resolve_pending_references(fhir_object)

        return fhir_object.to_dict()

    def bind_existing_reference(self, fhir_object, reference_path):
        reference_attribute = fhir_object[reference_path]

        def bind(ref, isArray=False):
            # extract the type and itentifier of the reference
            reference_type = ref["type"]
            identifier = ref["identifier"]
            try:
                identifier_tuple = self.extract_key_tuple(identifier)
            except Exception as e:
                logger.error(e)
                return ref

            # search the referenced resource in the database
            referenced_resource = self.fhirstore.db[reference_type].find_one(
                partial_identifier(identifier), ["id"],
            )
            if referenced_resource:
                # if found, add the ID as the "literal reference"
                # (https://www.hl7.org/fhir/references-definitions.html#Reference.reference)
                logger.info(f"reference to {reference_type} {identifier} resolved")
                ref["reference"] = f"{reference_type}/{referenced_resource['id']}"
            else:
                logger.info(
                    f"caching reference to {reference_type} {identifier} at {reference_path}"
                )

                # otherwise, cache the reference to resolve it later
                target_ref = (reference_type, identifier_tuple)
                source_ref = (fhir_object["resourceType"], reference_path, isArray)
                self.cache[target_ref][source_ref].append(fhir_object["id"])
            return ref

        # If we have a list of references, we want to bind all of them.
        # Thus, we loop on all the items in reference_attribute.
        if isinstance(reference_attribute, list):
            return [bind(ref, isArray=True) for ref in reference_attribute]
        else:
            return bind(reference_attribute)

    def resolve_pending_references(self, fhir_object):
        for identifier in fhir_object["identifier"]:
            try:
                identifier_tuple = self.extract_key_tuple(identifier)
            except Exception as e:
                logger.error(e)
                continue

            target_ref = (fhir_object["resourceType"], identifier_tuple)
            pending_refs = self.cache.get(target_ref, {})
            for (source_type, reference_path, isArray), refs in pending_refs.items():
                find_predicate = {
                    "id": {"$in": refs},
                    reference_path: {"$elemMatch": partial_identifier(identifier)}
                    if isArray
                    else partial_identifier(identifier),
                }
                # handle updating reference arrays:
                # we keep the indices in the path (eg: "identifier.0.assigner.reference")
                # but if fhir_object[reference_path] is an array, we use the '$' feature of mongo
                # in order to update the right element of the array.
                # https://docs.mongodb.com/manual/reference/operator/update/positional/#update-documents-in-an-array
                # FIXME: won't work if multiple elements of the array need to be updated (see
                # https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#identifier).
                update_predicate = {
                    "$set": {
                        f"{reference_path}{'.$' if isArray else ''}.reference": fhir_object["id"]
                    }
                }
                logger.info(
                    "Updating resource %s: %s %s", source_type, find_predicate, update_predicate
                )
                self.fhirstore.db[source_type].update_many(find_predicate, update_predicate)
            if len(pending_refs) > 0:
                del self.cache[target_ref]

    @staticmethod
    def extract_key_tuple(identifier):
        """ Build a tuple that contains the essential information from an Identifier.
        This tuple serves as a map key.
        """
        value = identifier.get("value")
        system = identifier.get("system")
        identifier_type_coding = identifier["type"]["coding"][0] if "type" in identifier else {}
        identifier_type_system = identifier_type_coding.get("system")
        identifier_type_code = identifier_type_coding.get("code")

        logger.debug("%s %s", (value and system), (identifier_type_system and identifier_type_code))
        if not (bool(value and system) ^ bool(identifier_type_system and identifier_type_code)):
            raise Exception(
                f"invalid identifier: {identifier} identifier.value and identifier.system "
                "or identifier.type are required and mutually exclusive"
            )

        return (value, system, identifier_type_code, identifier_type_system)


from typing import List

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def lowestCommonAncestor(
        self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
    ) -> "TreeNode":
        if root in (None, p, q):
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if not left and not right:
            return None
        if left and right:
            return root
        return right if not left else right
        # if not root:
        #     return None
        # res, _ = self.recursive(root, p, q)
        # return res

    def recursive1(
        self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
    ) -> ("TreeNode", List["TreeNode"]):
        if not root:
            return None, []

        left, l_data = self.recursive(root.left, p, q)
        right, r_data = self.recursive(root.right, p, q)

        if left:
            return left, []
        if right:
            return right, []

        l_data.extend(r_data)
        if root.val == p.val or root.val == q.val:
            l_data.append(root)

        if len(l_data) == 2:
            return root, []

        return None, l_data

def main():
    nums = [0, 1, 2, 3, 4, 5, 6, 7]
    # nums = [0, 1, -1, 3, -1, -1, -1, 7]
    # nums = [0, 1, 2, 3, -1, 4, 6, -1, -1, -1, -1, 5, -1, 7]
    root = makeTree(nums, 1)
    sol = Solution()
    res = sol.lowestCommonAncestor(root, TreeNode(4), TreeNode(5))
    print(res.val)

def makeTree(nums: List[int], pos: int) -> TreeNode:
    if pos >= len(nums) or nums[pos] < 0:
        return None
    node = TreeNode(nums[pos])
    node.left = makeTree(nums, pos * 2)
    node.right = makeTree(nums, pos * 2 + 1)
    return node

main()

leetcode/old_session/236. Lowest Common Ancestor of a Binary Tree.py


import msgpack

import imageio

import os
import sys
import re
import argparse

import numpy as np
import cv2
import time

import json

import multiprocessing as mp
import random

import logging

import tensorflow as tf

from msgpack_dataset_creator import MsgPackWriter

from google.protobuf.json_format import MessageToJson

def parse_args():
    parser = argparse.ArgumentParser(description='')

    parser.add_argument('-v', '--verbose', action='store_true', help='verbose output')
    parser.add_argument('-p', '--path', help='verbose output')
    parser.add_argument('-o', '--output', help='path to dir of images')
    parser.add_argument('-m', '--mapping')
    parser.add_argument('--process', default=8, type=int)

    parser.add_argument('-s', '--shuffle', action='store_true', default=True, help='verbose output')

    args = parser.parse_args()
    return args

def read_records(args):
    record = args['record']
    mapping = args['mapping']
    data_dict_list = []
    for example in tf.data.TFRecordDataset(record):
        data_dict = {}
        for key, value in tf.train.Example.FromString(example.numpy()).features.feature.items():
            values = []
            for v in value.float_list.value:
                values.append(v)
            for v in value.bytes_list.value:
                values.append(v)
            for v in value.int64_list.value:
                values.append(v)

            if len(values) == 1:
                values = values[0]
            if key in mapping:
                data_dict[mapping[key]] = values
            else:
                data_dict[key] = values
        data_dict_list.append(data_dict)
    return data_dict_list

def main():
    args = parse_args()

    if args.mapping is not None:
        mapping = json.loads(args.mapping)
    else:
        mapping = {}
    #record_re = re.compile()
    records = [os.path.join(args.path, x) for x in os.listdir(args.path)]

    with mp.Pool(args.process) as pool:
        with MsgPackWriter(args.output) as f:
            start = time.time()
            records = [{'record': x, 'mapping': mapping} for x in records]
            count = 0
            for i, x in enumerate(pool.imap(read_records, records)):
                for example in x:
                    if example is None:
                        continue
                    #print(i)
                    f.write(example)

                    # f.write(json.dumps({'key': x['path'], 'path': x['path'], 'index': i}) + '\n')
                    if count % 1000 == 0:
                        # txn.commit()
                        end = time.time()
                        print(f'{count}: {1000/(end - start):.2f} image/s')
                        start = end
                    count += 1

    return 0

if __name__ == '__main__':
    sys.exit(main())


#!/bin/env python

from typing import List, Tuple
import numpy as np

Point = Tuple[int,int]

def translate_move(move: str) -> Point:
    direction = move[0]
    quantity = int(move[1:])
    if direction == 'R':
        return (quantity,0)
    elif direction == 'L':
        return (0-quantity,0)
    elif direction == 'U':
        return (0,quantity)
    elif direction == 'D':
        return (0, 0-quantity)
    else:
        print('error')

def move_to(initial: Point, move: Point) -> Point:
    return (initial[0]+move[0], initial[1]+move[1])

def get_positions(listOfMoves: List[Point]) -> List[Point]:
    position = (0,0)
    listOfPositions = []
    for move in listOfMoves:
        position = move_to(position,move)
        listOfPositions.append(position)
    return listOfPositions

def interpolate(a: Point, b: Point):
    listOfPositions = []
    tmp = a
    while tmp[0] != b[0]:
        if tmp[0] > b[0]:
            tmp = (tmp[0]-1, tmp[1])
        else: #a<b
            tmp = (tmp[0]+1, tmp[1])
        listOfPositions.append(tmp)

    while tmp[1] != b[1]:
        if tmp[1] > b[1]:
            tmp = (tmp[0],tmp[1]-1)
        else: #a<b
            tmp = (tmp[0],tmp[1]+1)
        listOfPositions.append(tmp)
    return listOfPositions

def get_all_positions(line: str) -> List[Point]:
    moves = [translate_move(move) for move in line.split(',')]
    positions = get_positions(moves)
    detailed_positions = []
    start = (0,0)
    for i in range(len(positions)):
        interpolated = interpolate(start, positions[i]) if i == 0  else interpolate(positions[i-1], positions[i])
        detailed_positions.extend(interpolated)
    return detailed_positions

def find_crossings(listA: List[Point], listB: List[Point]) -> List[Point]:
    crossings = []
    for point in listA:
        if point in listB:
            crossings.append(point)
    return crossings

def manhattan_distance(init: Point, x: Point) -> int:
    return abs(init[0]-x[0]) + abs(init[1]-x[1])

def find_xing_index(listA: List[Point], listB: List[Point]) -> List[int]:
    xing_index = []
    for xing in find_crossings(listA, listB):
        xing_index.append((listA.index(xing)+1 + listB.index(xing)+1))
    return xing_index

with open('03-input.txt') as file:
    line1 = file.readline()
    line2 = file.readline()

    pos1 = get_all_positions(line1)
    #print(pos1)
    pos2 = get_all_positions(line2)
    #print(pos2)

    #dist = [manhattan_distance((0,0), x) for x in find_crossings(pos1, pos2)]
    dist = find_xing_index(pos1, pos2)

    print(dist)
    print(np.min(dist))


from PIL import Image, ImageDraw, ImageFont
import random, numpy, sys, math

# define variables here
flag = 'BSidesPDX{d0t}' # max 16 bytes
number_of_papers = 100

matrix_size = (8, 15) # was 15
matrix_ratio = 2.5

dot_diameter = 1 / 250

date = (18, 9, 22) # y / m / d
time = (18, 30)
serial = (13, 37, random.randint(0, 64), random.randint(0, 64))
unknown = 108

# size definition variables
paper_width = 8.5 # inches
paper_height = 11.0 # inches
paper_margins = 0.5 # inches

# open the paper image
default_paper_image = Image.open('paper.png')
default_paper = numpy.asarray(default_paper_image)

# 1024 random samples of color from the paper
choices = [tuple(random.choice(random.choice(default_paper))) for x in range(1024)]

# returns an automatically generated image of a paper
def getPaper():
    # create the default image
    size_x, size_y = (int(paper_width / dot_diameter / matrix_ratio), int(paper_height / dot_diameter / matrix_ratio)) # img.size
    img = Image.new("RGB", (size_x, size_y))

    # get an array of pixels
    pixels = img.load()

    # randomize all of the pixels using the `choices` array from earlier
    for x in range(size_x):
        for y in range(size_y):
            pixels[x, y] = random.choice(choices)

        # write a pretty status message to console
        percentage = x / size_x
        length = 40
        sys.stdout.write('\r[%s>%s] %s%s' % ('=' * round(percentage * length), ' ' * (length - round(percentage * length)), round(x / size_x * 100), '%'))
        sys.stdout.flush()

    # draw 5 random lines (red herring)
    draw = ImageDraw.Draw(img)
    for i in range(5):
        draw.line((random.choice(range(size_x)), random.choice(range(size_y)), random.choice(range(size_x)), random.choice(range(size_y))), fill = 128)

    # draw heading
    draw.text((10, 10), "BSidesPDX CTF - 2018", font = ImageFont.truetype("arial.ttf"), fill = (0, 0, 0))

    # write a newline after status message
    sys.stdout.write('\n')
    sys.stdout.flush()

    return img

# create dot matrix
matrix_height, matrix_width = matrix_size
matrix = []

# fills in the column at `index` with `array`
def fillHeight(index, array):
    for i in range(len(array)):
        matrix[i + 1][index + 1] = (True if array[i] == '1' else False) # 1 offset because of parity bit

# converts an int to an array of strings of binary digits
def toBinary(number, zfill = 7):
    return list(str(bin(number))[2:].zfill(zfill))

# creates an empty matrix
def resetMatrix():
    for y in range(matrix_height):
        matrix.append([False for x in range(matrix_width)])

# adds parity to a matrix
def calculateParity():
    # row parity
    for row in matrix:
        if row.count(True) % 2 == 0:
            row[0] = True

    # column parity
    for x in range(1, matrix_width):
        bit = True

        for y in range(matrix_height):
            if matrix[y][x]:
                bit = not bit

        if bit:
            matrix[0][x] = True

    # [0, 0] parity
    matrix[0][0] = True

# fill matrix with default data
def getDefaultMatrix():
    resetMatrix()

    fillHeight(0, toBinary(time[1])) # col 2: time in seconds
    fillHeight(3, toBinary(time[0])) # col 5: time in hours

    fillHeight(4, toBinary(date[2])) # col 6: date in days
    fillHeight(5, toBinary(date[1])) # col 7: date in months
    fillHeight(6, toBinary(date[0])) # col 8: date in years

    fillHeight(8, list('1' * 7)) # col 10: separator

    fillHeight(9, toBinary(serial[3])) # col 11: serial digit group
    fillHeight(10, toBinary(serial[2])) # col 12: serial digit group
    fillHeight(11, toBinary(serial[1])) # col 13: serial digit group
    fillHeight(12, toBinary(serial[0])) # col 14: serial digit group

    fillHeight(13, toBinary(unknown)) # col 15: unknown

    calculateParity()

    return matrix

# fill matrix with flag data
def getFlagMatrix():
    resetMatrix()

    # convert flag to an array of ints (ascii)
    binary_flag = [ord(x) for x in list(flag)]

    # fill out matrix
    for i in range(len(binary_flag)):
        fillHeight(i, toBinary(binary_flag[i]))

    calculateParity()

    return matrix

# print out matrix
def printMatrix(matrix):
    print('\n'.join([''.join([('X' if b else ' ') for b in a]) for a in matrix]))

# choose a paper to embed flag in
flag_paper = random.randint(0, number_of_papers - 1)

# add a little hint
with open('.hint', 'w') as f:
    f.write('- ignore the lines on the images\n- if you are completely lost: what was the title of the challenge again?')

# generate all papers
for i in range(number_of_papers):
    serial = (13, 37, random.randint(0, 64), random.randint(0, 64))

    # generate dot matrix
    type = 'dummy'
    if flag_paper == i:
        type = 'flag'
        matrix = getFlagMatrix()
    else:
        matrix = getDefaultMatrix()

    # print status info
    print('Generating %s paper (%s/%s)...' % (type, i + 1, number_of_papers))

    # create a new randomized paper image
    paper = getPaper()
    pixels = paper.load()

    # size definition variables
    image_width, image_height = paper.size # pixels

    # useful unit conversion functions
    toInches = lambda pixels : paper_width / image_width * pixels
    toPixels = lambda inches : image_width / paper_width * inches

    # define yellow dot size and position variables in pixels
    yellow_dot_diameter = toPixels(dot_diameter) # pixels
    yellow_dot_spacing = int(toPixels(3 / 64)) # pixels
    yellow_dot_padding = toPixels(1 / 2) # pixels

    # matrix dimensions
    dot_matrix_size = (matrix_size[1] * (yellow_dot_diameter + yellow_dot_spacing) + yellow_dot_padding, matrix_size[0] * (yellow_dot_diameter + yellow_dot_spacing) + yellow_dot_padding)

    # add yellow dots
    for x in range(math.floor((toPixels(paper_width) - (1 * toPixels(paper_margins))) / dot_matrix_size[0])):
        for y in range(math.floor((toPixels(paper_height) - (1 * toPixels(paper_margins))) / dot_matrix_size[1])):
            # define upper left corner of the dot matrix
            position = ((x * dot_matrix_size[0]) + toPixels(paper_margins), (y * dot_matrix_size[1]) + toPixels(paper_margins))

            # set positions
            for row in range(matrix_width):
                for col in range(matrix_height):
                    if matrix[col][row]:
                        pixels[round(position[0] + (yellow_dot_spacing * row)), round(position[1] + (yellow_dot_spacing * col))] = (0xFF, 0xFF, random.randint(0x77, 0x88))

    paper.save('scan%s.png' % i)

    # extra newline
    print()