import paho.mqtt.client as mqtt
import uuid
import busio
import board
import time
import adafruit_ssd1306
from adafruit_bus_device.i2c_device import I2CDevice
from struct import pack, unpack
from PIL import Image, ImageDraw, ImageFont

def draw_text(text):
    image = Image.new("1", (oled.width, oled.height))
    draw = ImageDraw.Draw(image)

    # Draw a white background
    font = ImageFont.load_default()

    # Draw Some Text
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12)
    (font_width, font_height) = font.getsize(text)
    draw.text(
        (oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2),
        text,
        font=font,
        fill=255,
    )
    oled.image(image)
    oled.show()

def read_register(dev, register, n_bytes=1):
    # write a register number then read back the value
    reg = register.to_bytes(1, 'little')
    buf = bytearray(n_bytes)
    with dev:
        dev.write_then_readinto(reg, buf)
    return int.from_bytes(buf, 'little')

DEVICE_ADDRESS = 0x6f  # device address of our button
STATUS = 0x03 # reguster for button status
AVAILIBLE = 0x1
BEEN_CLICKED = 0x2
IS_PRESSED = 0x4

# The follow is for I2C communications
i2c = busio.I2C(board.SCL, board.SDA)
device = I2CDevice(i2c, DEVICE_ADDRESS)

oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
oled.fill(0)
# we just blanked the framebuffer. to push the framebuffer onto the display, we call show()
oled.show()
WIDTH = 128
HEIGHT = 32  # Change to 64 if needed
BORDER = 5

# Every client needs a random ID
client = mqtt.Client(str(uuid.uuid1()))
# configure network encryption etc
client.tls_set()
# this is the username and pw we have setup for the class
client.username_pw_set('idd', 'device@theFarm')
#connect to the broker
client.connect(
    'farlab.infosci.cornell.edu',
    port=8883)
channels = ['select ch', 'ch> emergency', 'ch> bathroom 1', 'ch> bathroom 2','ch> emergency','ch> food 1', ]
messages = ['msg> Help!!!',
            'msg> dance!',
            'line is 0-10 people',
            'line is 10-25 people',
            'line is 25-40 people',
            'line is greater than 40 people',
            'help needed']
channel_state = 0
message_state = 0
pressed_since = 0
prev_time = 0
counter = 10
short_press, long_press = False, False
mode = 0
while True:

    btn_status = read_register(device, STATUS)
    # check if button pressed
    if (btn_status & IS_PRESSED) != 0:
        if abs(prev_time - time.time()) < 1:
            pressed_since = pressed_since + time.time() - prev_time
            prev_time = time.time()
            print("prev_time", pressed_since)
        prev_time = time.time()
        time.sleep(0.2)
        print("Button pressed")
        continue
    else:
        if pressed_since < 2 and pressed_since > 0:
            if mode == 0:
                channel_state += 1
                channel_state = channel_state % 5
            else:
                message_state += 1
                message_state = message_state % 5

            # mode = 0
        elif pressed_since >= 3:
            #message_state += 1
            #message_state = message_state % 5
            mode += 1
            mode = mode % 3
        pressed_since = 0
        prev_time = 0

    cmd = channels[channel_state]
    if ' ' in cmd:
        print('sorry white space is a no go for topics')
    else:
        topic = f"IDD/{cmd}"
        print(f"now writing to topic {topic}")
        if True:
            val = messages[message_state]
            if val == 'new-topic':
                break
            else:
                client.publish(topic, val)
    if mode == 0:
        draw_text(channels[channel_state])
    elif mode == 1:
        draw_text(messages[message_state])
    elif mode == 2:
        draw_text("msg sent")


from pandas import read_csv
from matplotlib import pyplot

def box():
    filename="iris.csv"
    """
    DataFrame([data, index, columns, dtype, copy])
    Two-dimensional size-mutable, potentially heterogeneous tabular data
    structure with labeled axes (rows and columns).
     here dataset is a dataframe
    """
    datasets=read_csv(filename,skiprows=[1])
    datasets.plot.area(stacked="TRUE")
    pyplot.show()
    datasets.plot.area(stacked="FALSE")
    pyplot.show()

def main():
    box()

if __name__ == '__main__':
    main()


from collections import namedtuple

Query = namedtuple('Query','tstart tstop metrics tags groupby aggrby limit time_zone')

class QueryBuilder(object):
    """
        Kairosdb query builder
    """
    def __init__(self):
        self.query = Query(tstart=None,
                           tstop = None,
                           metrics = None,
                           tags = None,
                           groupby = None,
                           aggrby = None,
                           limit = None,
                           time_zone = 'Europe/Rome')
        self.metrics = []
        self.tags = {}
        self.aggregators = []
        self.groups = []
        self.required_fields = ['tstart', 'metrics']

    def setStart(self, tstart):
        self.query = self.query._replace(tstart = tstart)

    def setEnd(self, tstop):
        self.query = self.query._replace(tstop = tstop)

    def setLimit(self, limit):
        self.query = self.query._replace(limit = limit)

    def setTimezone(self, time_zone):
        self.query = self.query._replace(time_zone = time_zone)

    def addMetric(self, metric):
        self.__additem(metric, self.metrics)

    def addTags(self, tag):
        self.__additem(tag, self.tags)

    def addAggregator(self, aggr):
        self.__additem(aggr, self.aggregators)

    def addGroupby(self, groupby):
        self.__additem(groupby, self.groups)

    def __additem(self, x, xlist):
        if type(xlist) is list:
            if type(x) is list:
                xlist.extend(x)
            else:
                xlist.append(x)
        if type(xlist) is dict:
            if type(x) is dict:
                xlist.update(x)
            else:
                raise ValueError('%s type is dict' % x)

    def getQuery(self):
        if len(self.metrics) > 0:
            self.query = self.query._replace(metrics=self.metrics)
        if len(self.aggregators) > 0:
            self.query = self.query._replace(aggrby=self.aggregators)
        if len(self.groups) > 0:
            self.query = self.query._replace(groupby=self.groups)
        if len(self.tags) > 0:
            self.query = self.query._replace(tags=self.tags)
        for item in self.required_fields:
            if getattr(self.query, item) is None:
                raise ValueError('%s value is required' % item)
        return self.query


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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form_usuario(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(330, 347)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(33)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        Form.setMinimumSize(QtCore.QSize(330, 347))
        Form.setMaximumSize(QtCore.QSize(330, 347))
        Form.setFocusPolicy(QtCore.Qt.TabFocus)
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(10, 20, 221, 21))
        font = QtGui.QFont()
        font.setFamily("FreeMono")
        font.setPointSize(14)
        font.setBold(True)
        font.setItalic(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setStyleSheet("")
        self.label_3.setObjectName("label_3")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(170, -10, 161, 101))
        self.label.setStyleSheet("image: url(:/img/img/1455555019_users-9_icon-icons.com_53249.svg);")
        self.label.setText("")
        self.label.setObjectName("label")
        self.tabWidget = QtWidgets.QTabWidget(Form)
        self.tabWidget.setGeometry(QtCore.QRect(10, 70, 311, 221))
        self.tabWidget.setStyleSheet("background: rgb(200, 200,200);")
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.widget = QtWidgets.QWidget(self.tab)
        self.widget.setGeometry(QtCore.QRect(60, 50, 201, 91))
        self.widget.setObjectName("widget")
        self.formLayout = QtWidgets.QFormLayout(self.widget)
        self.formLayout.setContentsMargins(8, 8, 4, 8)
        self.formLayout.setHorizontalSpacing(8)
        self.formLayout.setVerticalSpacing(21)
        self.formLayout.setObjectName("formLayout")
        self.label_2 = QtWidgets.QLabel(self.widget)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.lineEdit = QtWidgets.QLineEdit(self.widget)
        self.lineEdit.setObjectName("lineEdit")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
        self.label_4 = QtWidgets.QLabel(self.widget)
        self.label_4.setObjectName("label_4")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_4)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
        self.tabWidget.addTab(self.tab, "")
        self.splitter = QtWidgets.QSplitter(Form)
        self.splitter.setGeometry(QtCore.QRect(130, 300, 191, 31))
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.splitter.setObjectName("splitter")
        self.pushButton = QtWidgets.QPushButton(self.splitter)
        self.pushButton.setStyleSheet("image: url(:/img/img/icons8-ok-filled.svg);\n"
"background-color:   rgb(200,200,200);")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/img/img/icons8-ok-filled.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton.setIcon(icon)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.splitter)
        self.pushButton_2.setStyleSheet("background-color:   rgb(200,200,200);")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/img/img/icons8-cancelar.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_2.setIcon(icon1)
        self.pushButton_2.setObjectName("pushButton_2")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(0, 0, 658, 41))
        self.label_6.setStyleSheet("background-color:  rgba(255, 207, 183, 200);")
        self.label_6.setText("")
        self.label_6.setObjectName("label_6")
        self.label_6.raise_()
        self.label.raise_()
        self.label_3.raise_()
        self.tabWidget.raise_()
        self.splitter.raise_()

        self.retranslateUi(Form)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(Form)
        Form.setTabOrder(self.pushButton, self.pushButton_2)
        Form.setTabOrder(self.pushButton_2, self.tabWidget)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Cadastrar Usuário"))
        self.label_3.setText(_translate("Form", "Cadastrar Produto"))
        self.label_2.setText(_translate("Form", "Nome"))
        self.label_4.setText(_translate("Form", "Senha"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Form", "Cadastrar"))
        self.pushButton.setText(_translate("Form", "Cadastrar"))
        self.pushButton_2.setText(_translate("Form", "Cancelar"))

import recursos

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form_usuario()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())


'''
가장 긴 연속으로 증하가느 배열의 길이를 찾는다
1회 기회를 사용할 때마다 배열의 길이를 1씩 증가시켜야 한다.
즉 전체 어린의길이 - 연속으로 증가하는 배열의 길이가 답이다.
dp[n] = n 을 포함한 최장 수열 길이
'''
import sys
input = sys.stdin.readline

N = int(input())
childs = list(map(int,input().split()))
dp = [0]*(N+1)
answer = 0

for i in childs:
    dp[i] = dp[i-1]+1

print(dp)
print(N-max(dp))

# import sys
# input = sys.stdin.readline

# N = int(input())
# childs = list(map(int,input().split()))
# dp = [1]*(N+1)
# answer = 0

# for i in range(1,N):
#     # i-1의 인데스를 찾고 dp의 인덱스에 해당하는 값에 +1을 해준다. 만약 해당하는 인덱스가 없으면 1을 넣어줌
#     target = childs[i]-1
#     r = childs[:i]
#     if  target in r:
#         dp[i] = dp[r.index(target)]+1
#     else:
#         dp[i] = 1
#     answer = max(answer,dp[i])

# print(N-answer)


# 4. Пользователь вводит целое положительное число. Найдите самую большую цифру в числе. Для решения используйте цикл while и арифметические операции.
number = input('enter number for 4th task: ')
iterator = 0
max_digit = 0

while iterator < len(number):
    if int(number[iterator]) > max_digit:
        max_digit = int(number[iterator])
    iterator += 1

print(f'the biggest digit is {max_digit}')


from opengever.ogds.base.utils import ogds_service
from opengever.ogds.models.org_unit import OrgUnit
from opengever.setup import DEVELOPMENT_USERS_GROUP
from opengever.setup.creation.unit import UnitCreator
from opengever.setup.exception import GeverSetupException
from plone import api

class OrgUnitCreator(UnitCreator):

    item_name = 'OrgUnit'
    item_class = OrgUnit
    required_attributes = ('unit_id',
                           'admin_unit_id',
                           'users_group_id',
                           'inbox_group_id')

    def apply_development_config(self, item):
        item['users_group_id'] = DEVELOPMENT_USERS_GROUP
        item['inbox_group_id'] = DEVELOPMENT_USERS_GROUP

    def check_constraints(self, item):
        super(OrgUnitCreator, self).check_constraints(item)

        org_unit_id = item['unit_id']
        admin_unit_id = item['admin_unit_id']

        self.check_admin_unit_id(admin_unit_id, org_unit_id)
        self.check_group_id(item['users_group_id'], org_unit_id)
        self.check_group_id(item['inbox_group_id'], org_unit_id)

    def check_admin_unit_id(self, admin_unit_id, org_unit_id):
        admin_unit = ogds_service().fetch_admin_unit(admin_unit_id)
        if admin_unit is None:
            raise GeverSetupException(
                "Missing Admin-Unit '{}' while creating Org-Unit '{}'".format(
                    admin_unit_id, org_unit_id))

    def check_group_id(self, groupid, org_unit_id):
        group = ogds_service().fetch_group(groupid)
        if group is None:
            raise GeverSetupException(
                "Missing Group '{}' while creating Org-Unit '{}'".format(
                    groupid, org_unit_id))

    def create_unit(self, item):
        super(OrgUnitCreator, self).create_unit(item)

        site = api.portal.get()
        site.acl_users.portal_role_manager.assignRoleToPrincipal(
            'Member', item['users_group_id'])


from flask import Flask, render_template, request, redirect
from flask import Blueprint
from models.fitness_class import FitnessClass
from models.class_type import ClassType
from models.member import Member
from models.booking import Booking
import repositories.member_repository as member_repository
import repositories.fitness_class_repository as fitness_class_repository

members_blueprint = Blueprint("members", __name__)

@members_blueprint.route("/members")
def member_list():
    all_members = member_repository.select_all()
    return render_template("members/index.html", all_members = all_members)

@members_blueprint.route("/members/add", methods=['GET'])
def new_member():
    return render_template("members/add.html")

@members_blueprint.route("/members", methods=['POST'])
def create_member():
    name = request.form['name']
    address = request.form['address']
    phone = request.form['phone']
    email = request.form['email']
    premium = request.form['premium']
    membership_no = request.form['membership_no']

    new_member = Member(name, address, phone, email, premium, membership_no)
    member_repository.add(new_member)
    return redirect('/members')

@members_blueprint.route("/members/<id>/edit", methods=['GET'])
def edit_member_details(id):
    member = member_repository.select(id)
    return render_template("members/edit.html", member = member)

@members_blueprint.route("/members/<id>", methods=['POST'])
def update_member_details(id):
    name = request.form['name']
    address = request.form['address']
    phone = request.form['phone']
    email = request.form['email']
    premium = request.form['premium']
    membership_no = request.form['membership_no']

    updated_member = Member(name, address, phone, email, premium, membership_no, id)
    member_repository.edit(updated_member)
    return redirect('/members')

@members_blueprint.route("/members/<id>/delete", methods=['POST'])
def delete_member(id):
    member_repository.delete(id)
    return redirect('/members')

@members_blueprint.route("/members/find-member-by-name")
def find_member_form():
    return render_template("members/find_member_by_name.html")

@members_blueprint.route("/members/member-not-found")
def member_not_found_message(id):
    return render_template("members/member_not_found.html")

@members_blueprint.route("/members/found-member", methods=['POST'])
def get_members_from_name():
    member_name = request.form['name']
    members = member_repository.find_members_by_name(member_name)

    if len(members) > 0:
        return render_template("members/found_member.html", members = members)

    else:
        return redirect('/members/member-not-found')

@members_blueprint.route("/members/<id>/booked-classes")
def display_booked_classes(id):
    member = member_repository.select(id)
    class_list = member_repository.get_booked_classes(member)
    return render_template("members/class_list.html", class_list = class_list)


#Find the longest common prefix string amongst an array of strings.
def longestCommonPrefix(self, strs):
    if strs == []:
        return ""

    ret = strs[0]
    for i in range(1,len(strs)):
        l1 = len(strs[0])
        l2 = len(strs[i])
        if l1<l2:
            l = l1
        else:
            l = l2
        ret = ret[0:l]

        for j in range(l):
            if ret[j] != strs[i][j]:
                ret = ret[0:j]
                break
    return ret


noOfClusters = 32

file_clusters = open("32cluster_info_wiki10.txt","r")

temp_clusters = file_clusters.read().strip().split("\n")
temp_clusters = [ ( x.split() ) for x in temp_clusters ]

# list of list and inner list has labels for that cluster
clusters = []
for ct in temp_clusters:
	for x in ct:
		clusters.append( int(x) )

file_clusters.close()

labelids = open("../../dataset/sorted_labelid.txt","r").read().strip().split("\n")

outFile = open("sorted_labelid_dl_clustering.txt" ,"w")

for x in labelids:
	outFile.write( str(clusters.index(int(x))) + "\n" )

outFile.close()


counties = ["Arapahoe", "Denver", "Jefferson"]
if "El Paso" in counties:
    print("El Paso is in the list of counties")
else:
    print("El Paso is not in the list of counties")

voting_data = [{"county":"Arapahoe", "registered_voters": 422829},
                {"county":"Denver", "registered_voters": 463353},
                {"county":"Jefferson", "registered_voters": 432438}]

for county_dict in voting_data:
    print(county_dict)


#!/usr/bin/python
# -*- coding: utf-8 -*-
# Add files to paper

from __future__ import print_function
import sys
import aipy as A
import glob
import socket
import os
import dbi as dev
import time
import uv_data

### Script to add files to paper database
### Adds files using dbi

### Author: Immanuel Washington
### Date: 5-06-15

#Functions which simply find the file size
def get_size(start_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size

def sizeof_fmt(num):
    #converts bytes to MB
    for byte_size in ('KB', 'MB'):
        num /= 1024.0
    return round(num, 1)

### other functions
def calc_size(host, path, filename):
    named_host = socket.gethostname()
    full_path = os.path.join(path, filename)
    #DEFAULT VALUE
    size = 0
    if named_host == host:
        size = sizeof_fmt(get_size(full_path))
    else:
        ssh = dev.login_ssh(host)
        sftp = ssh.open_sftp()
        size_bytes = sftp.stat(full_path).st_size
        size = sizeof_fmt(size_bytes)
        sftp.close()
        ssh.close()

    return size

def calc_md5sum(host, path, filename):
    named_host = socket.gethostname()
    full_path = os.path.join(path, filename)
    #DEFAULT VALUE
    md5 = None
    if named_host == host:
        md5 = dev.get_md5sum(full_path)
    else:
        ssh = dev.login_ssh(host)
        try:
            sftp = ssh.open_sftp()
            remote_path = sftp.file(full_path, mode='r')
            md5 = remote_path.check('md5', block_size=65536)
            sftp.close()
        except(IOError):
            stdin, md5_out, stderr = ssh.exec_command('md5sum {full_path}/visdata'.format(full_path=full_path))
            md5 = md5_out.read().split(' ')[0]
        ssh.close()

    return md5

def get_uv_data(host, full_path, mode=None):
    ssh = dev.login_ssh(host)
    uv_data_script = os.path.expanduser('~/paper/dbi/scripts/uv_data.py')
    sftp = ssh.open_sftp()
    moved_script = './uv_data.py'
    try:
        filestat = sftp.stat(uv_data_script)
    except(IOError):
        try:
            filestat = sftp.stat(moved_script)
        except(IOError):
            sftp.put(uv_data_script, moved_script)
    sftp.close()

    if mode is None:
        uv_comm = 'python {moved_script} {host} {full_path}'.format(moved_script=moved_script, host=host, full_path=full_path)
        _, uv_dat, _ = ssh.exec_command(uv_comm)
        time_start, time_end, delta_time, julian_date, polarization, length, obsnum = [round(float(info), 5) if key in (0, 1, 2, 3, 5)
                                                                                else int(info) if key in (6,)
                                                                                else info
                                                                                for key, info in enumerate(uv_dat.read().split(','))]
        ssh.close()
        return time_start, time_end, delta_time, julian_date, polarization, length, obsnum

    elif mode == 'time':
        uv_comm = 'python {moved_script} {host} {full_path} time'.format(moved_script=moved_script, host=host, full_path=full_path)
        _, uv_dat, _ = ssh.exec_command(uv_comm)
        time_start, time_end, delta_time, length = [round(float(info), 5) for info in uv_dat.read().split(',')]
        ssh.close()
        return time_start, time_end, delta_time, length

def julian_era(julian_date):
    #indicates julian day and set of data
    if julian_date < 2456100:
        era = 32
    elif julian_date < 2456400:
        era = 64
    else:
        era = 128

    julian_day = int(str(julian_date)[3:7])

    return era, julian_day

def is_edge(prev_obs, next_obs):
    if (prev_obs, next_obs) == (None, None):
        edge = None
    else:
        edge = (None in (prev_obs, next_obs))
    return edge

def obs_pn(s, obsnum):
    table = getattr(dev, 'Observation')
    PREV_OBS = s.query(table).filter(getattr(table, 'obsnum') == obsnum - 1).one()
    if PREV_OBS is not None:
        prev_obs = getattr(PREV_OBS, 'obsnum')
    else:
        prev_obs = None
    NEXT_OBS = s.query(table).filter(getattr(table, 'obsnum') == obsnum + 1).one()
    if NEXT_OBS is not None:
        next_obs = getattr(NEXT_OBS, 'obsnum')
    else:
        next_obs = None

    return prev_obs, next_obs

def obs_edge(obsnum, sess=None):
    #unknown prev/next observation
    if obsnum == None:
        prev_obs = None
        next_obs = None
    else:
        if sess is None:
            dbi = dev.DataBaseInterface()
            s = dbi.Session()
            prev_obs, next_obs = obs_pn(s, obsnum)
            s.close()
        else:
            prev_obs, next_obs = obs_pn(sess, obsnum)

    edge = is_edge(prev_obs, next_obs)

    return prev_obs, next_obs, edge

def file_names(full_path):
    path = os.path.dirname(full_path)
    filename = os.path.basename(full_path)
    filetype = filename.split('.')[-1]
    return path, filename, filetype

def calc_obs_data(host, full_path):
    #mostly file data
    host = host
    path, filename, filetype = file_names(full_path)

    #Dictionary of polarizations
    pol_dict = {-5:'xx',-6:'yy',-7:'xy',-8:'yx'}

    #allows uv access
    named_host = socket.gethostname()
    if filetype in ('uv', 'uvcRRE'):
        if named_host == host:
            time_start, time_end, delta_time, julian_date, polarization, length, obsnum = uv_data.calc_uv_data(host, full_path)
        else:
            time_start, time_end, delta_time, julian_date, polarization, length, obsnum = get_uv_data(host, full_path)

    elif filetype in ('npz',):
        #filename is zen.2456640.24456.xx.uvcRE.npz or zen.2456243.24456.uvcRE.npz
        jdate = ''.join(filename.split('.')[1], '.', filename.split('.')[2])
        julian_date = round(float(jdate, 5))

        dbi = dev.DataBaseInterface()
        s = dbi.Session()
        if len(filename.split('.')) == 5:
            polarization = 'all'
            pol = polarization
        elif len(filename.split('.')) == 6:
            polarization = filename.split('.')[3]
            pol = polarization
        table = getattr(dev, 'Observation')
        OBS = s.query(table).filter(getattr(table, 'julian_date') == julian_date).filter(getattr(table, 'polarization') == pol).one()

        time_start = getattr(OBS, 'time_start')
        time_end = getattr(OBS, 'time_end')
        delta_time = getattr(OBS, 'delta_time')
        length = getattr(OBS, 'length')
        obsnum = getattr(OBS, 'obsnum')

        s.close()

    era, julian_day = julian_era(julian_date)

    #indicates type of file in era
    era_type = None

    prev_obs, next_obs, edge = obs_edge(obsnum)

    filesize = calc_size(host, path, filename)
    md5 = calc_md5sum(host, path, filename)
    tape_index = None

    source_host = host
    write_to_tape = False
    delete_file = False

    timestamp = int(time.time())

    obs_data = {'obsnum':obsnum,
                'julian_date':julian_date,
                'polarization':polarization,
                'julian_day':julian_day,
                'era':era,
                'era_type':era_type,
                'length':length,
                'time_start':time_start,
                'time_end':time_end,
                'delta_time':delta_time,
                'prev_obs':prev_obs,
                'next_obs':next_obs,
                'edge':edge,
                'timestamp':timestamp}
    file_data = {'host':host,
                'path':path,
                'filename':filename,
                'filetype':filetype,
                'full_path':full_path,
                'obsnum':obsnum,
                'filesize':filesize,
                'md5sum':md5,
                'tape_index':tape_index,
                'source_host':source_host,
                'write_to_tape':write_to_tape,
                'delete_file':delete_file,
                'timestamp':timestamp}

    action = 'add by scan'
    table = None
    identifier = full_path
    log_data = {'action':action,
                'table':table,
                'identifier':identifier,
                'timestamp':timestamp}

    return obs_data, file_data, log_data

def dupe_check(input_host, input_paths):
    dbi = dev.DataBaseInterface()
    s = dbi.Session()
    #all files on same host
    table = getattr(dev, 'File')
    FILEs = s.query(table).filter(getattr(table, 'host') == input_host).all()
    full_paths = tuple(os.path.join(getattr(FILE, 'path'), getattr(FILE, 'filename')) for FILE in FILEs)
    s.close()

    #for each input file, check if in full_paths
    unique_paths = tuple(input_path for input_path in input_paths if input_path not in full_paths)

    return unique_paths

def set_obs(s, OBS, field):
    if field == 'prev_obs':
        edge_num = getattr(OBS, 'obsnum') - 1
        edge_time = getattr(OBS, 'time_start') - getattr(OBS, 'delta_time')
    elif field == 'next_obs':
        edge_num = getattr(OBS, 'obsnum') + 1
        edge_time = getattr(OBS, 'time_start') + getattr(OBS, 'delta_time')

    table = getattr(dev, 'Observation')
    EDGE_OBS = s.query(table).filter(getattr(table, 'julian_date') == edge_time).one()
    if EDGE_OBS is not None:
        edge_obs = getattr(EDGE_OBS, 'obsnum')
        dbi.set_entry(OBS, field, edge_obs)
    else:
        pol = OBS.polarization
        EDGE_OBS = s.query(table).filter(getattr(table, 'julian_date') == edge_time).filter(getattr(table, 'polarization') == pol).one()
        if EDGE_OBS is not None:
            edge_obs = EDGE_OBS.obsnum
            dbi.set_entry(OBS, field, edge_obs)

    return EDGE_OBS

def update_obsnums():
    dbi = dev.DataBaseInterface()
    s = dbi.Session()
    table = getattr(dev, 'Observation')
    OBSs = s.query(table).all()

    for OBS in OBSs:
        PREV_OBS = set_obs(s, OBS, 'prev_obs')
        NEXT_OBS = set_obs(s, OBS, 'next_obs')
        #sets edge
        edge = is_edge(PREV_OBS, NEXT_OBS)
        dbi.set_entry(OBS, 'edge', edge)

    s.close()
    return None

def add_files(input_host, input_paths):
    dbi = dev.DataBaseInterface()
    for input_path in input_paths:
        path = os.path.dirname(input_path)
        filename = os.path.basename(input_path)
        obs_data, file_data, log_data = calc_obs_data(input_host, input_path)
        try:
            dbi.add_to_table('observation', obs_data)
        except:
            print('Failed to load in obs ', path, filename)
        try:
            dbi.add_to_table('file', file_data)
        except:
            print('Failed to load in file ', path, filename)
        try:
            dbi.add_to_table('log', log_data)
        except:
            print('Failed to load in log ', path, filename)

    return None

if __name__ == '__main__':
    named_host = socket.gethostname()
    if len(sys.argv) == 2:
        input_host = sys.argv[1].split(':')[0]
        if input_host == sys.argv[1]:
            print('Needs host')
            sys.exit()
        input_paths = sys.argv[1].split(':')[1]
    elif len(sys.argv) == 3:
        input_host = sys.argv[1]
        input_paths = sys.argv[2]
    else:
        input_host = raw_input('Source directory host: ')
        input_paths = raw_input('Source directory path: ')

    if named_host == input_host:
        input_paths = glob.glob(input_paths)
    else:
        ssh = dev.login_ssh(input_host)
        input_paths = raw_input('Source directory path: ')
        stdin, path_out, stderr = ssh.exec_command('ls -d {input_paths}'.format(input_paths=input_paths))
        input_paths = path_out.read().split('\n')[:-1]
        ssh.close()

    input_paths = dupe_check(input_host, input_paths)
    input_paths.sort()
    npz_paths = [npz_path for npz_path in input_paths if '.npz' in npz_path]
    npz_paths.sort()
    input_paths = [input_path for input_path in input_paths if '.npz' not in input_path]
    input_paths.sort()
    add_files(input_host, input_paths)
    add_files(input_host, npz_paths)
    update_obsnums()


"""
Problem:

Given a string s, find the longest palindromic substring in s.
You may assume that the maximum length of s is 1000.
"""

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        palindrome = ""
        for center in range(0, len(s)):
            paliOdd = self.findPali(s, center, center)
            paliEven = self.findPali(s, center, center+1)
            if len(paliOdd) > len(palindrome):
                palindrome = paliOdd
            if len(paliEven) > len(palindrome):
                palindrome = paliEven
        return palindrome

    def findPali(self, s, index1, index2):
        """
        :type s: str
        :type index1: int
        :type index2: int
        "rtype: str
        """
        palindrome = ""
        while(index1 >= 0 and index2 < len(s) and s[index1] == s[index2]):
            newPali = s[index1:index2+1]
            index1 -= 1
            index2 += 1
            if(len(newPali) > len(palindrome)):
                palindrome = newPali
        return palindrome


"""clean module for the cli."""
import click

import iocage.lib.ioc_common as ioc_common
import iocage.lib.iocage as ioc

__rootcmd__ = True

@click.command(name="clean", help="Destroy specified dataset types.")
@click.option("--force", "-f", default=False, is_flag=True)
@click.option("--all", "-a", "dataset_type", flag_value="all",
              help="Destroy all iocage data that has been created.")
@click.option("--jails", "-j", "dataset_type", flag_value="jails",
              help="Destroy all jails created.")
@click.option("--base", "-r", "-b", "dataset_type", flag_value="release",
              help="Destroy all RELEASEs fetched.")
@click.option("--template", "-t", "dataset_type", flag_value="template",
              help="Destroy all templates.")
def cli(force, dataset_type):
    """Calls the correct destroy function."""
    if dataset_type == "jails":
        msg = {
            "level"  : "WARNING",
            "message": "\nThis will destroy ALL jails and any "
                       "snapshots on a RELEASE,"
                       "including templates!"
        }
    elif dataset_type == "all":
        msg = {
            "level"  : "WARNING",
            "message": "\nThis will destroy ALL iocage data!"
        }
    elif dataset_type == "release":
        msg = {
            "level"  : "WARNING",
            "message": "\nThis will destroy ALL fetched RELEASES and"
                       " jails/templates created from them!"
        }
    elif dataset_type == "template":
        msg = {
            "level"  : "WARNING",
            "message": "This will destroy ALL templates and jails"
                       " created from them!"
        }
    else:
        ioc_common.logit({
            "level"  : "EXCEPTION",
            "message": "Please specify a dataset type to clean!"
        })

    if not force:
        ioc_common.logit(msg)
        if not click.confirm("\nAre you sure?"):
            exit()

    ioc.IOCage(skip_jails=True).clean(dataset_type)


from django.conf import settings
from django.utils import translation
from django.utils.cache import patch_vary_headers

from utils import get_language_from_request

class LocaleMiddleware(object):
    def process_request(self, request):
        language = get_language_from_request(request)
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        if not response.cookies.has_key(settings.LANGUAGE_COOKIE_NAME):
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, translation.get_language())
        patch_vary_headers(response, ('Accept-Language',))
        if 'Content-Language' not in response:
            response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response


# DFS solution
def solution(n, computers):
    answer = 0
    visited = [0] * n
    stack = []

    while 0 in visited:
        stack.append(visited.index(0))
        answer += 1
        while stack:
            i = stack.pop()
            visited[i] = 1

            for j, connection in enumerate(computers[i]):
                if connection and not visited[j]:
                    stack.append(j)
    return answer

# DFS solution, x2 faster
def solution2(n, computers):
    networks = 0
    not_visited = [i for i in range(n)]

    while not_visited:
        networks += 1
        head = not_visited.pop()
        stack = []
        stack.append(head)

        while stack:
            tail = stack.pop()
            for i, connected in enumerate(computers[tail]):
                if connected and (i in not_visited):
                    stack.append(i)
                    not_visited.remove(i)
    return networks

# BFS solution
import queue
def solution3(n, computers):
    answer = 0
    visited = [0]*n
    qu = queue.Queue()

    while 0 in visited:
        qu.put(visited.index(0))
        answer += 1

        while qu.qsize() > 0:
            i = qu.get()
            visited[i] = 1
            for j, connected in enumerate(computers[i]):
                if connected and (not visited[j]):
                    qu.put(j)
    return answer

# BFS solution, x3 faster
from collections import deque
def solution4(n, computers):
    networks = 0
    not_visited = [i for i in range(n)]

    while not_visited:
        networks += 1
        head = not_visited.pop()
        queue = deque()
        queue.append(head)

        while queue:
            tail = queue.popleft()
            for i, connected in enumerate(computers[tail]):
                if connected and (i in not_visited):
                    queue.append(i)
                    not_visited.remove(i)
    return networks

if __name__ == '__main__':
    n = 3
    computers = [[1, 1, 0], [1, 1, 1], [0, 1, 1]]
    print(solution(n, computers))


from lxml import html
import requests
import pandas as pd

class HtmlScraper:
    """Scrape data (table) from HTML pages

    You may specify the table id to retrive specific table's data in the webpage

    """

    def parse_url(self, url, headers):

        page = requests.get(url, headers=headers)
        return html.fromstring(page.content)

    @staticmethod
    def clean_text(elt):
        return elt.text_content().replace(u'\xa0', u'')

    def parse_html_table(self, root, table_id):
        ''' parse the html table according to the id

        @return the DataFrame from the table including header and data
        '''

        # Get the table from the root according to the id
        table = root.get_element_by_id(table_id)
        # Retrieve the header information
        table_header = table.xpath('.//th')
        header = [self.clean_text(th) for th in table_header]
        data = [[self.clean_text(td) for td in tr.xpath('.//td')]
                for tr in table.xpath('.//tr')]
        data = [row for row in data if len(row) == len(header)]
        return pd.DataFrame(data, columns=header)

        # print(header)

    def save_to_csv(self, data, columns, filename):
        data.to_csv(path_or_buf=filename, columns=columns,
                    index=False)

def retrieve_data_to_csv(url, filename, cols):
    ''' Scrape data and save to csv files
    '''
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    sc = HtmlScraper()
    root = sc.parse_url(url, headers)
    df = sc.parse_html_table(root, "curr_table")

    with open(filename, 'wb') as csvfile:
        sc.save_to_csv(df, cols, csvfile)

if __name__ == "__main__":
    cols = ['Date', 'Price']
    url = 'https://www.investing.com/commodities/gold-historical-data'
    retrieve_data_to_csv(url, "gold.csv", cols)

    url = 'https://www.investing.com/commodities/silver-historical-data'
    retrieve_data_to_csv(url, "silver.csv", cols)


import sqlite3
import os

def clear_cmd():
    os.system("clear")

def page():

    print("""  __       __            __    """)
    print(""" /  |  _  /  |          /  |   """)
    print(""" $$ | / \ $$ |  ______  $$ |  _______   ______   _____  ____    ______  """)
    print(""" $$ |/$  \$$ | /      / $$ | /       | /      \ /     \/    \  /      \ """)
    print(""" $$ /$$$  $$ |/$$$$$$  |$$ |/$$$$$$$/ /$$$$$$  |$$$$$$ $$$$  |/$$$$$$  |""")
    print(""" $$ $$/$$ $$ |$$    $$ |$$ |$$ |      $$ |  $$ |$$ | $$ | $$ |$$    $$ |""")
    print(""" $$$$/  $$$$ |$$$$$$$$/ $$ |$$ |_____ $$ |__$$ |$$ | $$ | $$ |$$$$$$$$/ """)
    print(""" $$$/    $$$ |$$       |$$ |$$       |$$    $$/ $$ | $$ | $$ |$$       |""")
    print(""" $$/      $$/  $$$$$$$/ $$/  $$$$$$$/  $$$$$$/  $$/  $$/  $$/  $$$$$$$/ """)

    input("\n\nPress Any Key to Continue...")

def create_db():

    conn = sqlite3.connect("lab.db")
    cur = conn.cursor()

    table_create_sql = """create table if not exists todo(
        id integer primary key autoincrement,
        what text not null,
        due text not null,
        category text not null,
        finished integer);"""

    cur.execute(table_create_sql)
    conn.close()

def help_msg():
    print("This is simple todo list program. \n \
 - To add data, press '1' and write your todo list. \n \
 - To view data, press '2'. \n \
 - To modify data, press '3'. \n \
 - To quit, press '5'. \n \
Made by Hanyang Univ. ERICA AnsanBladeWind \n \
Contact Us: https://github.com/SonMinWoo/AnsanBladeWind ")

def run_program():
    while 1:
        select = input("Choose what to do:\n(1: Add data, 2: List todo, 3: Modify todo, 4: Delete todo, 5: Help, 0: Quit) \n")
        if select == '1':
            add_todo()
        elif select == '2':
            list_todo()
        elif select == '3':
            modify_todo()
        elif select == '4':
            delete_todo()
        elif select == '5':
            help_msg()
        elif select == '0':
            break

def list_todo():
    conn = sqlite3.connect("lab.db")
    cur = conn.cursor()
    sql = "select * from todo where 1"
    cur.execute(sql)

    rows = cur.fetchall()

    print("\n" + "="*76)
    print("| {}| {}| {}| {}| {}|" .format("id".center(5,' '),"To Do".center(20,' '),"Due".center(15,' '),"Category".center(15,' '),"finished?".center(10,' ')))
    print("|"+"-"*74+"|")
    for row in rows:
        if(row[4] == 1):
            print("| {}| {}| {}| {}| {}|" .format(str(row[0]).center(5,' '),row[1].center(20,' '),row[2].center(15,' '),row[3].center(15,' '),'O'.center(10,' ')))
        else:
            print("| {}| {}| {}| {}| {}|" .format(str(row[0]).center(5,' '),row[1].center(20,' '),row[2].center(15,' '),row[3].center(15,' '),'X'.center(10,' ')))
    print("="*76+"\n")

    conn.close()

def add_todo():
    conn = sqlite3.connect("lab.db")
    cur = conn.cursor()

    what = input("Todo? \n")
    while(len(what) > 20):
        print("Maximum string number of contents is 20. Please reduce the number of letters\n ")
        what = input("Todo? \n")

    due = input("Due date? \n")
    while(len(due) > 15):
        print("Maximum string number of contents is 15. Please reduce the number of letters\n ")
        due = input("Due date? \n")

    category = input("Which category? \n")
    while(len(due) > 15):
        print("Maximum string number of contents is 15. Please reduce the number of letters\n ")
        category = input("Which category? \n")

    sql = "insert into todo (what, due, category, finished) values (?, ?, ?, ?)"
    cur.execute(sql, (what, due, category, 0))
    conn.commit()
    conn.close()

def modify_todo():
    list_todo()
    conn = sqlite3.connect("lab.db")
    cur = conn.cursor()

    sel_id = input("Record id? \n")

    #항목별로 수정할 수 있도록 개선
    sel_item = input("Select item \n 1: what, 2: Due, 3: category, 4: finished \n")

    if(sel_item=='1'):
        sel_what = input("Todo? \n")
        while(len(sel_what) > 20):
            print("Maximum string number of contents is 20. Please reduce the number of letters\n ")
            sel_what = input("Todo? \n")
        cur.execute("update todo set what = ? where id = ?", (sel_what, sel_id))

    elif(sel_item=='2'):
        sel_due = input("Due date? \n")
        while(len(sel_due) > 15):
            print("Maximum string number of contents is 15. Please reduce the number of letters\n ")
            sel_due = input("Due date? \n")
        cur.execute("update todo set due = ? where id = ?", (sel_due, sel_id))

    elif(sel_item=='3'):
        sel_catagory = input("Which category? \n")
        while(len(due) > 15):
            print("Maximum string number of contents is 15. Please reduce the number of letters\n ")
            sel_category = input("Which category? \n")
        cur.execute("update todo set category = ? where id = ?", (sel_catagory, sel_id))

    elif(sel_item=='4'):
        sel_fin = input("Finished (1: yes, 0: no)? \n")
        while(1):
            if(sel_fin == '1' or sel_fin == '0'):
                break
            print("Invalid value. Value must be 1 or 0\n")
            sel_fin = input("Finished (1: yes, 0: no)? \n")
        cur.execute("update todo set finished = ? where id = ?", (sel_fin, sel_id))

    conn.commit()
    conn.close()

def delete_todo():
    list_todo()
    conn = sqlite3.connect("lab.db")
    cur = conn.cursor()

    sel_id = input("Record id? \n")

    cur.execute("delete from todo where id = ?", (sel_id))

    conn.commit()
    conn.close()

clear_cmd()
page()

create_db()
run_program()


#!/usr/bin/env python
# coding=utf-8
from __future__ import unicode_literals, absolute_import, print_function, division

# sopel imports
import sopel.module

# imports for system and OS access, directories
import os
import sys

# imports based on THIS file
moduledir = os.path.dirname(__file__)
shareddir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(shareddir)
from BotShared import *

@module.event('001')
@module.rule('.*')
@module.thread(True)
def auto_feeds(bot, trigger):

    # don't run jobs if not ready
    while not bot_startup_requirements_met(bot, ["monologue"]):
        pass

    if "feeds" not in bot.memory:
        feed_configs(bot)

    bot_startup_requirements_set(bot, "feeds")

    for feed in bot.memory["feeds"].keys():
        feeds_grab(bot, feed)
        Thread(target=feeds_thread, args=(bot, feed,)).start()

# @module.interval(57)
# @module.thread(True)
# def auto_feeds(bot):
#
#    # don't run jobs if not ready
#    while not bot_startup_requirements_met(bot, ["monologue"]):
#        pass
#
#    if "feeds" not in bot.memory:
#        feed_configs(bot)
#
#    for feed in bot.memory["feeds"].keys():
#        feeds_grab(bot, feed)
#        dispmsg = bot_dictcom_feeds_handler(bot, feed, False)
#        if dispmsg != []:
#
#            for channel in bot.channels.keys():
#                feeds_enabled = get_channel_value(bot, channel, "long", "feeds", "enabled") or []
#                if feed in feeds_enabled:
#                    osd(bot, str(channel), 'say', dispmsg)
#
#            for user in bot.memory["botdict"]["tempvals"]["servers_list"][str(bot.memory["botdict"]["tempvals"]['server'])]['all_current_users']:
#                feed_enabled = get_nick_value(bot, user, "long", "feeds", "enabled") or []
#                if feed in feed_enabled:
#                    osd(bot, user, 'notice', dispmsg)

def feeds_thread(bot, feed):
    while True:
        time.sleep(57)
        dispmsg = bot_dictcom_feeds_handler(bot, feed, False)
        if dispmsg != []:

            for channel in bot.channels.keys():
                feeds_enabled = get_channel_value(bot, channel, "long", "feeds", "enabled") or []
                if feed in feeds_enabled:
                    osd(bot, str(channel), 'say', dispmsg)

            for user in bot.memory["botdict"]["tempvals"]["servers_list"][str(bot.memory["botdict"]["tempvals"]['server'])]['all_current_users']:
                feed_enabled = get_nick_value(bot, user, "long", "feeds", "enabled") or []
                if feed in feed_enabled:
                    osd(bot, user, 'notice', dispmsg)

def feeds_grab(bot, feed):
    dispmsg = bot_dictcom_feeds_handler(bot, feed, False)
    if dispmsg != []:

        for channel in bot.channels.keys():
            feeds_enabled = get_channel_value(bot, channel, "long", "feeds", "enabled") or []
            if feed in feeds_enabled:
                osd(bot, str(channel), 'say', dispmsg)

        for user in bot.memory["botdict"]["tempvals"]["servers_list"][str(bot.memory["botdict"]["tempvals"]['server'])]['all_current_users']:
            feed_enabled = get_nick_value(bot, user, "long", "feeds", "enabled") or []
            if feed in feed_enabled:
                osd(bot, user, 'notice', dispmsg)

@sopel.module.commands('feed', "feeds")
@module.thread(True)
def mainfunction(bot, trigger):

    botcom = bot_module_prerun(bot, trigger)
    if not botcom.modulerun:
        return

    if not botcom.multiruns:
        execute_main(bot, trigger, botcom)
    else:
        # IF "&&" is in the full input, it is treated as multiple commands, and is split
        commands_array = spicemanip.main(botcom.triggerargsarray, "split_&&")
        if commands_array == []:
            commands_array = [[]]
        for command_split_partial in commands_array:
            botcom.triggerargsarray = spicemanip.main(command_split_partial, 'create')
            execute_main(bot, trigger, botcom)

    botdict_save(bot)

def execute_main(bot, trigger, botcom):

    if "feeds" not in bot.memory:
        feed_configs(bot)

    if not len(bot.memory['feeds'].keys()):
        return osd(bot, botcom.channel_current, 'say', "There are no valid feeds!!")

    valid_commands = ['enable', 'disable', 'reset', 'run', 'subscribe', 'unsubscribe']
    command = spicemanip.main([x for x in botcom.triggerargsarray if x in valid_commands], 1) or 'run'
    if command in botcom.triggerargsarray:
        botcom.triggerargsarray.remove(command)

    feed_select = spicemanip.main([x for x in botcom.triggerargsarray if x in bot.memory['feeds'].keys() or x == 'all'], 1) or None
    if not feed_select:
        feed_list = spicemanip.main(bot.memory['feeds'].keys(), 'list')
        osd(bot, botcom.channel_current, 'say', "Valid Feeds are " + feed_list)
        return

    if feed_select == 'all':
        current_feed_list = bot.memory['feeds'].keys()
    else:
        current_feed_list = []
        for word in botcom.triggerargsarray:
            if word in bot.memory['feeds'].keys():
                current_feed_list.append(word)

    if command == 'run':
        for feed in current_feed_list:
            dispmsg = bot_dictcom_feeds_handler(bot, feed, True)
            if dispmsg == []:
                osd(bot, botcom.channel_current, 'say', feed_select + " appears to have had an unknown error.")
            else:
                if feed_select == 'all':
                    osd(bot, botcom.instigator, 'notice', dispmsg)
                else:
                    osd(bot, botcom.channel_current, 'say', dispmsg)
        return

    if command == 'subscribe':
        instigatormodulesarray = get_nick_value(bot, botcom.instigator, "long", "feeds", "enabled") or []
        newlist = []
        for feed in current_feed_list:
            if feed not in instigatormodulesarray:
                newlist.append(feed)
        if newlist != []:
            adjust_nick_array(bot, botcom.instigator, "long", "feeds", "enabled", newlist, "add")
            osd(bot, botcom.channel_current, 'say', "You are now " + command + "d to " + spicemanip.main(newlist, 'list'))
        else:
            osd(bot, botcom.channel_current, 'say', "No selected feeds to " + command + ".")
        return

    if command == 'unsubscribe':
        instigatormodulesarray = get_nick_value(bot, botcom.instigator, "long", "feeds", "enabled") or []
        newlist = []
        for feed in current_feed_list:
            if feed in instigatormodulesarray:
                newlist.append(feed)
        if newlist != []:
            adjust_nick_array(bot, botcom.instigator, "long", "feeds", "enabled", newlist, "del")
            osd(bot, botcom.channel_current, 'say', "You are now " + command + "d from " + spicemanip.main(newlist, 'list'))
        else:
            osd(bot, botcom.channel_current, 'say', "No selected feeds to " + command + ".")
        return

    if not bot_command_run_check(bot, botcom, ['admin', 'owner', 'OWNER', 'OP', 'ADMIN']):
        osd(bot, botcom.channel_current, 'say', "Only Bot Admins and Channel Operators are able to adjust Feed settings.")
        return

    if command == 'reset':
        newlist = []
        for feed in current_feed_list:
            feed_type = bot.memory['feeds'][feed]["type"]
            if feed_type in ['rss', 'youtube', 'github', 'redditrss', 'redditapi', 'twitter']:
                newlist.append(feed)
        if newlist != []:
            for feed in newlist:
                reset_nick_value(bot, str(bot.nick), 'long', 'feeds', feed + '_lastbuildtime')
                reset_nick_value(bot, str(bot.nick), 'long', 'feeds', feed + '_lastbuildtitle')
                reset_nick_value(bot, str(bot.nick), 'long', 'feeds', feed + '_lastbuildlink')
            osd(bot, botcom.channel_current, 'say', spicemanip.main(newlist, 'list') + " " + hashave(newlist) + " been " + command + ".")
        else:
            osd(bot, botcom.channel_current, 'say', "No selected feeds to " + command + ".")
        return

    channelselect = spicemanip.main([x for x in botcom.triggerargsarray if x in bot.channels.keys()], 1) or botcom.channel_current

    if command == 'enable':
        feeds_enabled = get_channel_value(bot, channelselect, "long", "feeds", "enabled") or []
        newlist = []
        for feed in current_feed_list:
            if feed not in feeds_enabled:
                newlist.append(feed)
        if newlist != []:
            adjust_channel_array(bot, channelselect, "long", "feeds", "enabled", newlist, "add")
            osd(bot, botcom.channel_current, 'say', spicemanip.main(newlist, 'list') + " " + hashave(newlist) + " been " + command + "d for " + str(channelselect) + ".")
        else:
            osd(bot, botcom.channel_current, 'say', "No selected feeds to " + command + ".")
        return

    if command == 'disable':
        feeds_enabled = get_channel_value(bot, channelselect, "long", "feeds", "enabled") or []
        newlist = []
        for feed in current_feed_list:
            if feed in feeds_enabled:
                newlist.append(feed)
        if newlist != []:
            adjust_channel_array(bot, channelselect, "long", "feeds", "enabled", newlist, "del")
            osd(bot, botcom.channel_current, 'say', spicemanip.main(newlist, 'list') + " " + hashave(newlist) + " been " + command + "d for " + str(channelselect) + ".")
        else:
            osd(bot, botcom.channel_current, 'say', "No selected feeds to " + command + ".")
        return

def feed_configs(bot):

    bot.memory['feeds'] = dict()

    dirdict = {
                "name": "feeds",
                "dirname": "Feeds",
                }

    filedicts, filecount = configs_dir_read(bot, dirdict)

    for dict_from_file in filedicts:

        comconf = dict_from_file["filename"]

        if "type" not in dict_from_file.keys():
            dict_from_file["type"] = quick_coms_type

        if "displayname" not in dict_from_file.keys():
            dict_from_file["displayname"] = None

        if "url" not in dict_from_file.keys():
            dict_from_file["url"] = None

        if dict_from_file["type"] == "redditapi":

            if "path" not in dict_from_file.keys():
                dict_from_file["path"] = None

            if not dict_from_file["url"]:
                dict_from_file["url"] = "https://www.reddit.com"

        if dict_from_file["type"] == "twitter":

            if "handle" not in dict_from_file.keys():
                dict_from_file["handle"] = None

            if not dict_from_file["url"]:
                dict_from_file["url"] = "https://twitter.com"

        if dict_from_file["type"] == "googlecalendar":

            if "calendar" not in dict_from_file.keys():
                dict_from_file["calendar"] = None

            if "link" not in dict_from_file.keys():
                dict_from_file["link"] = None

            if not dict_from_file["url"]:
                dict_from_file["url"] = "https://google.com"

        if dict_from_file["type"] == "dailyscrapes":

            if "scrapetitle" not in dict_from_file.keys():
                dict_from_file["scrapetitle"] = None

            if "scrapehour" not in dict_from_file.keys():
                dict_from_file["scrapehour"] = 1

            if "scrapeminute" not in dict_from_file.keys():
                dict_from_file["scrapeminute"] = 1

            if "scrapetimezone" not in dict_from_file.keys():
                dict_from_file["scrapetimezone"] = "UTC"

            if "scrapelink" not in dict_from_file.keys():
                dict_from_file["scrapelink"] = None

            if "linkprecede" not in dict_from_file.keys():
                dict_from_file["linkprecede"] = None

        if dict_from_file["type"] == "events":

            if "scrapetitle" not in dict_from_file.keys():
                dict_from_file["scrapetitle"] = None

            if "eventmonth" not in dict_from_file.keys():
                dict_from_file["eventmonth"] = 1

            if "eventday" not in dict_from_file.keys():
                dict_from_file["eventday"] = 1

            if "eventhour" not in dict_from_file.keys():
                dict_from_file["eventhour"] = 0

            if "eventminute" not in dict_from_file.keys():
                dict_from_file["eventminute"] = 0

            if "timezone" not in dict_from_file.keys():
                dict_from_file["timezone"] = "UTC"

            if "rightnow" not in dict_from_file.keys():
                dict_from_file["rightnow"] = None

        if dict_from_file["type"] == "scrapes":

            if "scrapetitle" not in dict_from_file.keys():
                dict_from_file["scrapetitle"] = None

            if "scrapetime" not in dict_from_file.keys():
                dict_from_file["scrapetime"] = None

            if "scrapetimezone" not in dict_from_file.keys():
                dict_from_file["scrapetimezone"] = "UTC"

            if "scrapelink" not in dict_from_file.keys():
                dict_from_file["scrapelink"] = None

            if "linkprecede" not in dict_from_file.keys():
                dict_from_file["linkprecede"] = None

        if dict_from_file["type"] == "webinarscrapes":

            if "scrapetime" not in dict_from_file.keys():
                dict_from_file["scrapetime"] = None

            if "scrapetitle" not in dict_from_file.keys():
                dict_from_file["scrapetitle"] = None

            if "scrapelink" not in dict_from_file.keys():
                dict_from_file["scrapelink"] = None

            if "linkprecede" not in dict_from_file.keys():
                dict_from_file["linkprecede"] = None

            if "scrapebonus" not in dict_from_file.keys():
                dict_from_file["scrapebonus"] = None

            if "scrapebonussplit" not in dict_from_file.keys():
                dict_from_file["scrapebonussplit"] = None

            if "scrapetimezone" not in dict_from_file.keys():
                dict_from_file["scrapetimezone"] = "UTC"

        if comconf not in bot.memory['feeds'].keys():
            bot.memory['feeds'][comconf] = dict_from_file


from app import app, mysql
from flask import request, jsonify
import MySQLdb
from shortcuts import *
from response import *

@app.route('/db/api/thread/listPosts/', methods = ['GET'])
def thread_list_posts_view():
	thread_id = request.args.get('thread')
	if thread_id is None:
		return didntFind('thread')

	resp = []

	sort = request.args.get('sort')
	since = request.args.get('since')
	limit = request.args.get('limit')
	order = request.args.get('order')

	if limit is not None:
		limit=int(limit)
	if sort is None or sort == 'flat':
		extra = sinceOrderLimit(since, order, limit)
		if extra == False:
			return badExtra()
		cursor = mysql.connection.cursor()
		if False == getThreadPostsResp(resp, cursor, thread_id=thread_id, extra=extra):
			return dontExist('thread')
	else:
		cursor = mysql.connection.cursor()
		if False == treeSort(cursor, resp, thread_id, sort, since, order, limit):
			return badExtra()

	return OK(resp)

@app.route('/db/api/thread/vote/', methods = ['POST'])
def thread_vote():
	if not('vote' in request.json and 'thread' in request.json):
		didntFind('thread id and vote')
	thread_id = request.json['thread']
	vote = request.json['vote']
	if vote != 1 and vote != -1:
		badJson('vote must be 1 or -1')

	if vote == 1:
		query = "UPDATE thread SET likes = likes + 1 WHERE id = %s;"
	else:
		query = "UPDATE thread SET dislikes = dislikes + 1 WHERE id = %s;"
	cursor = mysql.connection.cursor()
	cursor.execute(query, [thread_id])
	mysql.connection.commit()

	resp = {}
	getThreadRespById(resp, cursor, thread_id)
	return OK(resp)

@app.route('/db/api/thread/update/', methods = ['POST'])
def thread_update2():
	try:
		thread_id = request.json['thread']
		slug = request.json['slug']
		message = request.json['message']
		if not areOfType((slug, message), basestring):
			return badTypes()
	except:
		didntFind('thread message, slug and id')
	query = "UPDATE thread SET slug = %s, message = %s WHERE id = %s;"
	cursor = mysql.connection.cursor()
	cursor.execute(query, [slug, message, thread_id])
	mysql.connection.commit()

	resp = {}
	if False == getThreadRespById(resp, cursor, thread_id):
		return dontExist('thread')
	return OK(resp)

@app.route('/db/api/thread/unsubscribe/', methods = ['POST'])
@app.route('/db/api/thread/subscribe/', methods = ['POST'])
def subscribe():
	try:
		thread_id = request.json['thread']
		email = request.json['user']
	except:
		didntFind('thread id and user email')
	cursor = mysql.connection.cursor()
	if getThreadIdById(cursor, thread_id) is None:
		return dontExist('thread')
	user_id = getUserByEmail(email, cursor)
	if user_id is None:
		return dontExist('user')

	try:
		if request.url_rule.rule == '/db/api/thread/subscribe/':
			query = ("INSERT INTO subscription (user_id, thread_id)"
					"VALUES(%s, %s);")
		else:
			query = "DELETE FROM subscription WHERE user_id = %s AND thread_id = %s;"

		cursor.execute(query, [user_id, thread_id])
		mysql.connection.commit()
	except:
		pass
	resp={}
	resp['user'] = email
	resp['thread'] = thread_id
	return OK(resp)

@app.route('/db/api/thread/close/', methods = ['POST'])
@app.route('/db/api/thread/open/', methods = ['POST'])
@app.route('/db/api/thread/remove/', methods = ['POST'])
@app.route('/db/api/thread/restore/', methods = ['POST'])
def clopen_thread():
	if not('thread' in request.json):
		return didntFind('thread')
	id = request.json['thread']
	cursor = mysql.connection.cursor()
	if getThreadIdById(cursor, id) is None:
		return dontExist('thread')

	if request.url_rule.rule == '/db/api/thread/close/':
		toset = "isClosed = TRUE"
	elif request.url_rule.rule == '/db/api/thread/open/':
		toset = "isClosed = FALSE"
	elif request.url_rule.rule == '/db/api/thread/remove/':
		toset = "isDeleted = TRUE"
		query = "UPDATE post SET isDeleted = TRUE WHERE thread_id = %s;"
		cursor.execute(query, [id])
		mysql.connection.commit()
	else:
		toset = "isDeleted = FALSE"
		query = "UPDATE post SET isDeleted = FALSE WHERE thread_id = %s;"
		cursor.execute(query, [id])
		mysql.connection.commit()
	query = "UPDATE thread SET "+toset+" WHERE id = %s;"
	cursor.execute(query, [id])
	mysql.connection.commit()
	resp = {}
	resp['thread'] = id

	return OK(resp)

@app.route('/db/api/forum/listThreads/', methods = ['GET'])
@app.route('/db/api/thread/list/', methods = ['GET'])
def list_threads():
	fromforum = (request.url_rule.rule == '/db/api/forum/listThreads/')

	forum_short = request.args.get('forum')
	user_email = request.args.get('user')
	if forum_short is None:
		if fromforum:
			return didntFind('forum shortname')
		if user_email is None:
			return didntFind('forum shortname or user email')

	else:
		if user_email is not None:
			return badJson('both user and forum arguments found')

	cursor = mysql.connection.cursor()
	if fromforum:
		related = request.args.getlist('related')
	else:
		related = []
	known = {}
	if forum_short is not None:
		forum_id = getForumIdByShortname(cursor, forum_short)
		if 'forum' in related:
			forum = {}
			getForumResp(forum, cursor, forum_id)
			known['forum'] = forum
		else:
			known['forum'] = forum_short
	else:
		forum_id = None
		forum = None

	since = request.args.get('since')
	limit = request.args.get('limit')
	order = request.args.get('order')

	extra = sinceOrderLimit(since, order, limit)
	if extra == False:
		return badExtra()
	resp = []

	if getThreadsResp(resp, cursor, extra, forum_id, user_email, related=related, known=known) == False:

		return didntFind('forum or user')

	return OK(resp)

@app.route('/db/api/thread/create/', methods = ['POST'])
def create_thread():
	try:
		forum_short = request.json['forum']
		title = request.json['title']
		isClosed = request.json['isClosed']
		founder_email = request.json['user']
		date = request.json['date']
		message = request.json['message']
		slug = request.json['slug']
		if not (areOfType((title, date, message, slug), basestring) and type(isClosed)==bool):
			return badTypes()
	except:
		return didntFind()
	if 'isDeleted' in request.json:
		isDeleted = request.json['isDeleted']
		if type(isDeleted) != bool:
			return badTypes()
	else:
		isDeleted = False

	cursor = mysql.connection.cursor()
	founder_id = getUserByEmail(founder_email, cursor)
	if founder_id is None:
		return dontExist('user')
	forum_id = getForumIdByShortname(cursor, forum_short)
	if forum_id is None:
		return dontExist('forum')

	query = ("INSERT INTO thread (title, slug, creator_id, "
			"message, forum_id, date, isClosed, isDeleted, creator_email) "
			"VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s);")

	cursor.execute(query, [title, slug, founder_id, message \
	, forum_id, date, isClosed, isDeleted, founder_email])
	mysql.connection.commit()
	resp = {}
	resp['id'] = cursor.lastrowid
	resp['date'] = date
	resp['forum'] = forum_short
	resp['isClosed'] = isClosed
	resp['isDeleted'] = isDeleted
	resp['message'] = message
	resp['slug'] = slug
	resp['title'] = title
	resp['user'] = founder_email

	return OK(resp)

@app.route('/db/api/thread/details/', methods = ['GET'])
def get_thread_details():
	thread_id = request.args.get('thread')
	if thread_id is None:
		return didntFind('thread id')

	related = request.args.getlist('related')
	if checkRelated(related, ['user', 'forum']) == False:
		return badJson('related parameter is incorrect')

	resp = {}
	cursor = mysql.connection.cursor()
	if getThreadRespById(resp, cursor, thread_id, related) == False:
		return dontExist('thread')
	return OK(resp)


'''
This problem was asked by Palantir.

You are given a list of N numbers, in which each number is located at most k places away from its sorted position. For example, if k = 1, a given element at index 4 might end up at indices 3, 4, or 5.

Come up with an algorithm that sorts this list in O(N log k) time.

'''

from heapq import *

def solve(a,k):
	res = []
	heap = []
	for ele in a[:k]:
		heappush(heap,ele)

	for ele in a[k:]:

		heappush(heap,ele)
		res.append(heappop(heap))

	while heap:
		res.append(heappop(heap))

	return res

if __name__ == '__main__':
	a = [4,3,5]
	k = 1
	print(solve(a,k))


from .main import *
import unittest

class TestPokerHands(unittest.TestCase):
    def testHandGetValue(self):
        high_card = Hand('TH 2C 3C 4C 5C')
        one_pair = Hand('6H 6D TC JS 8C')
        two_pairs = Hand('9C 9D 8C 7C 8S')
        three_of_a_kind = Hand('3C 3S 3D AS KS')
        straight_1 = Hand('AC 2D 3D 4S 5H')
        straight_2 = Hand('6C 2D 3D 4S 5H')
        straight_3 = Hand('AH KH QS JS TS')
        flush_1 = Hand('AC TC 3C 4C 5C')
        flush_2 = Hand('JS TS AS KS 2S')
        full_house_1 = Hand('AS AD AC TD TS')
        full_house_2 = Hand('AS AD TH TD TS')
        four_of_a_kind = Hand('3C 3S 3C 3D 4C')
        straight_flush_1 = Hand('AC 2C 3C 4C 5C')
        straight_flush_2 = Hand('AC KC QC JC TC')
        print(f'{high_card.get_hand_value()}')
        self.assertEqual(HandType.HIGH_CARD, high_card.get_hand_value().get_hand_type())
        self.assertEqual(HandType.ONE_PAIR, one_pair.get_hand_value().get_hand_type())
        self.assertEqual(HandType.TWO_PAIRS, two_pairs.get_hand_value().get_hand_type())
        self.assertEqual(HandType.THREE_OF_A_KIND, three_of_a_kind.get_hand_value().get_hand_type())
        self.assertEqual(HandType.STRAIGHT, straight_1.get_hand_value().get_hand_type())
        self.assertEqual(HandType.STRAIGHT, straight_2.get_hand_value().get_hand_type())
        self.assertEqual(HandType.STRAIGHT, straight_3.get_hand_value().get_hand_type())
        self.assertEqual(HandType.FLUSH, flush_1.get_hand_value().get_hand_type())
        self.assertEqual(HandType.FLUSH, flush_2.get_hand_value().get_hand_type())
        self.assertEqual(HandType.FULL_HOUSE, full_house_1.get_hand_value().get_hand_type())
        self.assertEqual(HandType.FULL_HOUSE, full_house_2.get_hand_value().get_hand_type())
        self.assertEqual(HandType.FOUR_OF_A_KIND, four_of_a_kind.get_hand_value().get_hand_type())
        self.assertEqual(HandType.STRAIGHT_FLUSH, straight_flush_1.get_hand_value().get_hand_type())
        self.assertEqual(HandType.STRAIGHT_FLUSH, straight_flush_2.get_hand_value().get_hand_type())

if __name__ == '__main__':
    unittest.main()


#!/usr/bin/env python

import requests
import json
import utils
from bs4 import BeautifulSoup
from models import File, Term

NAME = 'merriam-webster'

def parse_btn(btn):
    lang = btn['data-lang'].replace('_', '/')
    dir = btn['data-dir']
    file = btn['data-file']
    pat = 'https://media.merriam-webster.com/audio/prons/{0}/mp3/{1}/{2}.mp3'
    return pat.format(lang, dir, file)
def stripped_text(node):
    if node is None:
        return None
    return node.get_text().strip()

def get_data(query, lang):
    if lang != 'en':
        return None

    data = {
        'audio': [],
        #'visual': [],
        'tag': [],
        'transcription': [],
        'definition': [],
        'in': [],
        'synonym': [],
        'antonym': [],
        'related': []
    }

    pat = 'https://www.merriam-webster.com/dictionary/{0}'

    url = pat.format(query)

    headers = {
        'User-Agent': 'script',
        'Accept': 'text/html',
    }
    resp = requests.get(url, headers=headers)
    resp.raise_for_status()

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

    #find transcription and audio
    prs = soup.find('span', class_='prs')

    transcription = prs.find('span', class_='pr')
    transcription=stripped_text(transcription)

    data['transcription'].append(transcription)

    btns = prs.find_all('a', class_='play-pron')
    urls = [parse_btn(b) for b in btns]
    urls = [u for u in urls if utils.url_exists(u)]
    for url in urls:
        data['audio'].append(File(url=url, region=None))

    #find definitions and 'in'

    vg = soup.find_all('div', class_='vg')

    for v in vg:
        definitions = v.find_all(class_='dt')
        for d in definitions:
            text = stripped_text(d)
            #all defenitions start with ':' with class mw_t_bc
            if (d.find(class_='mw_t_bc') is not None):
                text = text.lstrip(':').strip()
                #with defenitions we can take examples of text with class ex-sent, we need drop it
                if (d.find(class_='ex-sent') is not None):
                    text = text.split('\n')[0].strip()
                data['definition'].append(Term(text=text, lang=lang, region=None))
    #parse examples
    data_in = soup.find_all(class_='ex-sent')
    for d in data_in:
        if ('t' in d['class']):
            data['in'].append(
                Term(text=stripped_text(d), lang=lang, region=None))
    #parse related
    ure = soup.find_all(class_='ure')
    for d in ure:
        data['related'].append(Term(text=stripped_text(d), lang=lang, region=None))
    #parse tags
    tag = soup.find_all('span', class_='fl')
    for d in tag:
        data['tag'].append(Term(text=stripped_text(d), lang=lang, region=None))

    #add tag with name 'word', becouse our name is word
    data['tag'].append(Term(text='word', lang=lang, region=None))

    #move to second page, in teasaurus
    pat_t = 'https://www.merriam-webster.com/thesaurus/{0}'
    url_t = pat_t.format(query)
    resp = requests.get(url_t, headers=headers)
    resp.raise_for_status()

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

    dlist = soup.find_all('span', class_='syn-list')
    for d in dlist:
        synonyms = d.find_all('a')
        for s in synonyms:
            data['synonym'].append(Term(text=stripped_text(s), lang=lang, region=None))

    dlist = soup.find_all('span', class_='rel-list')
    for d in dlist:
        related = d.find_all('a')
        for r in related:
            data['related'].append(Term(text=stripped_text(r), lang=lang, region=None))

    dlist = soup.find_all('span', class_='ant-list')
    for d in dlist:
        antonyms = d.find_all('a')
        for r in antonyms:
            data['antonym'].append(Term(text=stripped_text(r), lang=lang, region=None))

    return data

def main():
    (text, lang) = utils.find_audio_args()
    result = get_data(text, lang)
    print(json.dumps(result, sort_keys=True, indent='  ', ensure_ascii=False))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
RandstadCN spider created on the top of ATSSpider

scrapy crawl randstad_cn -a mining_job_id=9999 -a iteration=1 -a extract=1 -a url="http://www.randstad.cn/Jobs/JobList?q=&loc="

Seed URL:
    http://www.randstad.cn/Jobs/JobList?q=&loc=
"""

from re import compile
from scrapy.http import Request
from scrapy.selector import Selector
from urlparse import urljoin

from brightcorp.base.atsspiders import ATSSpider
from brightcorp.items import BrightcorpItemLoader
from brightcorp.processors import ConvertDateString, Prefix, Replace, UrlJoin

class RandstadCN(ATSSpider):

    name = 'randstad_cn'
    REF_REGEX = compile(r'jobid=(\d+)')

    def parse(self, response):
        sel = Selector(response)

        for div in sel.xpath(
            '//div[@class="resultsList"]/div[@class="resultBlock"]'
        ):
            href = div.xpath('./a[@id="TitleHyperlink"]/@href').extract()
            if href:
                yield Request(
                    callback=self.parse_job_callback(),
                    meta={
                        'title': div.xpath(
                            './a[@id="TitleHyperlink"]/text()'
                        ).extract(),
                        'date': div.xpath(
                            './p[@class="date"]/text()'
                        ).extract(),
                        'logo_url': div.xpath(
                            './p[@class="date"]/img/@src'
                        ).extract(),
                        'jobcategory': div.xpath(
                            './p/a[contains(@id, "JobSectorLink")]/text()'
                        ).extract(),
                        'location': div.xpath(
                            './p[a[contains(@id, "JobSectorLink")]]/text()'
                        ).extract(),
                        'company': div.xpath(
                            './p[@class="description"]/following-sibling::span[1]/text()'
                        ).extract(),
                    },
                    url=urljoin(response.url, href[0])
                )

        next_page = sel.xpath(
            '//div/a[contains(text(), "%s")]/@href' % unicode('下一页', 'utf-8')
        ).extract()
        if next_page:
            yield Request(
                callback=self.parse,
                url=urljoin(response.url, next_page[0])
            )

    def parse_job(self, response):
        sel = Selector(response)
        loader = BrightcorpItemLoader(selector=sel)

        loader.add_value('title', response.meta.get('title'))
        loader.add_value(
            'date',
            response.meta.get('date'),
            ConvertDateString('%Y-%m-%d')
        )
        loader.add_value(
            'logo_url',
            response.meta.get('logo_url'),
            UrlJoin(response.url)
        )
        loader.add_value(
            'jobcategory', response.meta.get('jobcategory')
        )
        loader.add_xpath(
            'location',
            '//div[p[contains(text(), "%s")]]/following-sibling::div[1]/p/text()' % unicode('所在省份', 'utf-8')
        )
        if not loader.get_output_value('location'):
            loader.add_value(
                'location', response.meta.get('location'), Replace('- ')
            )
        loader.add_value(
            'company', response.meta.get('company')
        )
        loader.add_value(
            'referencenumber',
            response.url,
            Prefix('%s-' % self.name),
            re=self.REF_REGEX
        )
        loader.add_xpath(
            'description',
            '//div[contains(@class, "jobDescription")]/div[contains(@id, "JobDescriptionPanel")]'
        )
        loader.add_xpath(
            'baseSalary',
            '//div/p[contains(text(), "%s")]/../following-sibling::div[1]/p/text()' % unicode('薪资范围', 'utf-8')
        )
        loader.add_value('url', response.url)
        loader.add_value('apply_url', response.url)

        yield loader.load_item()


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

import django

django.setup()

from poll.models import Poll, PollQuestion, VoteSheet, PollQuestionAnswer
from crm.models import Resident, Room
from datetime import date

poll = Poll.objects.all()[0]
poll_questions = []

with open('./tools/voteResult.csv') as f:
    lines = f.readlines()
    for question in lines[0].split(',')[7:]:
        question = question.replace('\r', '').replace('\n', '')
        poll_question = PollQuestion.objects.get(poll = poll, content = question)

        poll_questions.append(poll_question)

    for voteSheet in lines[1:]:
        voteSheet = voteSheet[:-1]

        cells = voteSheet.split(',')
        abbr = cells[0]
        room = Room.objects.get_by_abbr(abbr)
        resident = Resident.objects.filter(room = room)[0]
        try:
            voteSheet = VoteSheet.objects.get(poll = poll, room = room)
        except VoteSheet.DoesNotExist:
            voteSheet = VoteSheet(poll = poll, room = room, resident = resident, created_time = date.today())

        valid = cells[5]
        if valid != '有效':
            voteSheet.voted = False
            voteSheet.save()
            continue

        voteSheet.voted = True
        voteSheet.save()

        for idx, voteAnswer in enumerate(cells[7:]):
            voteAnswer = voteAnswer.replace('\r', '').replace('\n', '')
            poll_question = poll_questions[idx]

            try:
                question_answer = PollQuestionAnswer.objects.get(poll_question = poll_question, vote_sheet = voteSheet)
            except PollQuestionAnswer.DoesNotExist:
                question_answer = PollQuestionAnswer(poll_question = poll_question, vote_sheet = voteSheet)

            answer = 0
            if voteAnswer == '赞成':
                answer = 1
            elif voteAnswer == '反对':
                answer = -1
            elif voteAnswer == '弃权':
                answer = -2

            question_answer.answer = answer
            question_answer.save()


# -*- coding: utf-8 -*-
# For copyright and license notices, see LICENSE file

import win32service
import win32serviceutil
import servicemanager
# from epsonFiscalDriver import socketServer
import time
import logging

logging.basicConfig(
    filename='c:\\hasar\\service.log',
    level=logging.DEBUG,
    format='%(asctime)s [fiscalservice] %(levelname)-7.7s %(message)s'
)

class FiscalService(win32serviceutil.ServiceFramework):
    _svc_name_ = "fiscal_service"
    _svc_display_name_ = "Servidor de Impresora Fiscal"
    _svc_description_ = "Permite imprimir en una impresora conectada en " \
                        "otra pc de la red."

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        logging.info('INIT ===')
        self.is_alive = True

    def SvcDoRun(self):
        servicemanager.LogInfoMsg(self._svc_name_ + " - Iniciando servicio")
        logging.info('START ===')
        while self.is_alive:
            time.sleep(1)

    def SvcStop(self):
        servicemanager.LogInfoMsg(
            self._svc_name_ + " - Deteniendo el servicio")
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        logging.info('STOP ===')
        self.is_alive = False

if __name__ == '__main__':
    import sys
    if len(sys.argv) == 1:
        logging.info('handle command line 1')
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(FiscalService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(FiscalService)
        logging.info('handle command line 2')


from django.views.generic import TemplateView
from rest_framework.views import APIView
from decouple import config
from rest_framework.permissions import AllowAny
from django.shortcuts import redirect
import requests
from django.contrib.auth.models import User
from django.contrib.auth import login

class VueView(TemplateView):
    template_name = 'index.html'

class LoginView(APIView):
    permission_classes = (AllowAny,)

    def get(self, request, *args, **kwargs):
        SCOPE = 'profile+email'
        CLIENT_ID = config('GOOGLE_OAUTH2_KEY',cast=str)
        CLIENT_SECRET = config('GOOGLE_OAUTH2_SECRET',cast=str)
        FRONTEND_URL=config('FRONTEND_URL',cast=str)
        REDIRECT_URI = config('REDIRECT_URL',cast=str)
        if not request.GET.get('code'):
            auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code'
                        '&client_id={}&redirect_uri={}&scope={}').format(CLIENT_ID, REDIRECT_URI, SCOPE)
            return redirect(auth_uri)

        else:
            auth_code = request.GET.get('code')
            data = {'code': auth_code,
                    'client_id': CLIENT_ID,
                    'client_secret': CLIENT_SECRET,
                    'redirect_uri': REDIRECT_URI,
                    'grant_type': 'authorization_code'}
            token = requests.post('https://oauth2.googleapis.com/token', data=data)
            resp = requests.post('https://oauth2.googleapis.com/tokeninfo', data=token)

            data = resp.json()
        user = User.objects.filter(email=data['email']).first()
        if user is None:
            user = User.objects.create_user(
                email=data["email"], username=data["name"])
        login(request, user)
        return redirect(FRONTEND_URL+'/dashboard')


from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time
import re

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://web.whatsapp.com")

input("Press anything after QR scan")
time.sleep(8)

names = ["Bui Airtel","Rahul Tgt","Gaurav Tgt Whatsapp"]

while True:
    for name in names:
        time.sleep(2)
        person = driver.find_element_by_xpath('//span[@title="{}"]'.format(name))
        time.sleep(2)
        person.click()

        for i in range(1,3):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        msg_got = driver.find_elements_by_css_selector("span._3Whw5.selectable-text.invisible-space.copyable-text")
        msg = [message.text for message in msg_got]

        if re.search(".*@deepak|hello|hey|Hello|Hey|@Deepak|Deepak|deepak.*", msg[-1]):
            reply = driver.find_elements_by_class_name("_3FRCZ.copyable-text.selectable-text")[1]
            reply.clear()
            reply.send_keys("Yes.. I'm offline, my chatbot is listening!")
            reply.send_keys(Keys.RETURN)


from os import path, getenv, system
from urllib.request import urlopen, Request
import platform
import json
import webbrowser
from time import sleep
import random
from datetime import datetime, time

from dotenv import load_dotenv
import requests

platform = platform.system()
PLT_WIN = "Windows"
PLT_LIN = "Linux"
PLT_MAC = "Darwin"
GET_SELENIUM = 0
GET_URLLIB = 1
GET_API = 2

# CONFIG SECTION BELOW --------------------------------------------------------

'''
Template for adding a new website to check:

The key is the url of the website you want to check

The Value is a tuple of size 4 with the following values:
    0. The substring that you're looking for in the html of the website, OR the API URL for the site.
    1. If this is True, it will alert when the substring is found in the html. If False, it will alert if the substring is NOT found in the HTML
    2. Set this to GET_SELENIUM, GET_URLLIB, or GET_API to choose which method is used to fetch data from the site. USE_SELENIUM is useful for jsx pages.
    3. A nickname for the alert to use. This is displayed in alerts.
'''

url_keywords = {
    "https://www.nvidia.com/en-us/geforce/graphics-cards/30-series/rtx-3080/": ("https://api-prod.nvidia.com/direct-sales-shop/DR/products/en_us/USD/5438481700", False, GET_API, 'Nvidia 3080', ),
    "https://www.nvidia.com/en-us/geforce/graphics-cards/30-series/rtx-3090/": ("https://api-prod.nvidia.com/direct-sales-shop/DR/products/en_us/USD/5438481600,5443202600", False, GET_API, 'Nvidia 3090', ),
    "https://www.evga.com/products/productlist.aspx?type=0&family=GeForce+30+Series+Family&chipset=RTX+3080": ("AddCart", True, GET_URLLIB, 'EVGA 3080'),
    # "https://www.evga.com/products/productlist.aspx?type=0&family=GeForce+16+Series+Family&chipset=GTX+1650+Super": ("AddCart", True, GET_URLLIB, 'EVGATest'),
    "https://www.newegg.com/p/pl?d=rtx+3080&N=100007709%20601357247": ("Add to cart", True, GET_URLLIB, 'Newegg 3080'),
    "https://www.bhphotovideo.com/c/search?q=3080&filters=fct_category%3Agraphic_cards_6567": ("Add to Cart", True, GET_URLLIB, 'BandH 3080'),
    "https://www.bestbuy.com/site/searchpage.jsp?st=3080": ("cart.svg", True, GET_SELENIUM, "BestBuy 3080"),
    # "https://www.bestbuy.com/site/searchpage.jsp?st=tv": ("cart.svg", True, GET_SELENIUM, "BestBuyTest")
    "https://www.amazon.com/stores/page/6B204EA4-AAAC-4776-82B1-D7C3BD9DDC82?ingress=0": (">Add to Cart<", True, GET_URLLIB, 'Amazon 3080')
    # "https://store.asus.com/us/item/202009AM160000001": (">Buy Now<", True, GET_URLLIB, 'ASUS')
}

# END OF CONFIG SECTION -------------------------------------------------------

# Set up environment variables and constants. Do not modify this unless you know what you are doing!
load_dotenv()
USE_TWILIO = False
USE_SELENIUM = False
USE_DISCORD_HOOK = False
WEBDRIVER_PATH = path.normpath(getenv('WEBDRIVER_PATH'))
DISCORD_WEBHOOK_URL = getenv('DISCORD_WEBHOOK_URL')
TWILIO_TO_NUM = getenv('TWILIO_TO_NUM')
TWILIO_FROM_NUM = getenv('TWILIO_FROM_NUM')
TWILIO_SID = getenv('TWILIO_SID')
TWILIO_AUTH = getenv('TWILIO_AUTH')
ALERT_DELAY = int(getenv('ALERT_DELAY'))
MIN_DELAY = int(getenv('MIN_DELAY'))
MAX_DELAY = int(getenv('MAX_DELAY'))
OPEN_WEB_BROWSER = getenv('OPEN_WEB_BROWSER') == 'true'

# Selenium Setup
if WEBDRIVER_PATH:
    USE_SELENIUM = True
    print("Enabling Selenium... ", end='')
    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    options = Options()
    options.headless = True
    driver = webdriver.Firefox(options=options, executable_path=WEBDRIVER_PATH)
    reload_count = 0
    print("Done!")

# Twilio Setup
if TWILIO_TO_NUM and TWILIO_FROM_NUM and TWILIO_SID and TWILIO_AUTH:
    USE_TWILIO = True
    print("Enabling Twilio... ", end='')
    from twilio.rest import Client
    client = Client(TWILIO_SID, TWILIO_AUTH)
    print("Done!")

# Discord Setup
if DISCORD_WEBHOOK_URL:
    USE_DISCORD_HOOK = True
    print('Enabled Discord Web Hook.')

# Platform specific settings
print("Running on {}".format(platform))
if platform == PLT_WIN:
    from win10toast import ToastNotifier
    toast = ToastNotifier()

def alert(url):
    product = url_keywords[url][3]
    print("{} IN STOCK".format(product))
    print(url)
    if OPEN_WEB_BROWSER:
        webbrowser.open(url, new=1)
    os_notification("{} IN STOCK".format(product), url)
    sms_notification(url)
    discord_notification(product, url)
    sleep(ALERT_DELAY)

def os_notification(title, text):
    if platform == PLT_MAC:
        system("""
                  osascript -e 'display notification "{}" with title "{}"'
                  """.format(text, title))
        system('afplay /System/Library/Sounds/Glass.aiff')
        system('say "{}"'.format(title))
    elif platform == PLT_WIN:
        toast.show_toast(title, text, duration=5, icon_path="icon.ico")
    elif platform == PLT_LIN:
        # Feel free to add something here :)
        pass

def sms_notification(url):
    if USE_TWILIO:
        client.messages.create(to=TWILIO_TO_NUM, from_=TWILIO_FROM_NUM, body=url)

def discord_notification(product, url):
    if USE_DISCORD_HOOK:
        data = {
            "content": "{} in stock at {}".format(product, url),
            "username": "In Stock Alert!"
        }
        result = requests.post(DISCORD_WEBHOOK_URL, data=json.dumps(data), headers={"Content-Type": "application/json"})
        try:
            result.raise_for_status()
        except requests.exceptions.HTTPError as err:
            print(err)
        else:
            print("Payload delivered successfully, code {}.".format(result.status_code))

def selenium_get(url):
    global driver
    global reload_count

    driver.get(url)
    http = driver.page_source

    reload_count += 1
    if reload_count == 10:
        reload_count = 0
        driver.close()
        driver.quit()
        driver = webdriver.Firefox(options=options, executable_path=WEBDRIVER_PATH)
    return http

def urllib_get(url):
    # for regular sites
    # Fake a Firefox client
    request = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    page = urlopen(request, timeout=30)
    html_bytes = page.read()
    html = html_bytes.decode("utf-8")
    return html

def nvidia_get(url, api_url):
    response = requests.get(api_url, timeout=5)
    item = response.json()
    if item['products']['product'][0]['inventoryStatus']['status'] != "PRODUCT_INVENTORY_OUT_OF_STOCK":
        alert(url)

def main():
    search_count = 0
    while True:
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        print("Starting search {} at {}".format(search_count, current_time))
        search_count += 1
        for url, info in url_keywords.items():
            print("\tChecking {}...".format(info[3]))

            try:
                if info[2] == GET_SELENIUM:
                    if not USE_SELENIUM:
                        continue
                    html = selenium_get(url)
                elif info[2] == GET_API:
                    if 'nvidia' in info[3].lower():
                        nvidia_get(url, info[0])
                    continue
                else:
                    html = urllib_get(url)
            except Exception as e:
                print("\t\tConnection failed...")
                print("\t\t{}".format(e))
                continue
            keyword = info[0]
            alert_on_found = info[1]
            index = html.upper().find(keyword.upper())
            if alert_on_found and index != -1:
                alert(url)
            elif not alert_on_found and index == -1:
                alert(url)

        base_sleep = 1
        total_sleep = base_sleep + random.uniform(MIN_DELAY, MAX_DELAY)
        sleep(total_sleep)

if __name__ == '__main__':
    main()


import cv2
import numpy as np

import brailleforming
import dithering

class Braille:
    """Class to store the data of of original & converted images"""

    def __init__(self, source: str, width: int, method: int):
        self.path = source
        self.original = read_img_file(self.path)
        self.scaled = scale(self.original, width)

        height, width = self.scaled.shape[:2]
        while width % 2 != 0:
            width -= 1
        while height % 4 != 0:
            height -= 1
        self.res = (height, width, *self.original.shape)
        self.scaled = self.scaled[0:height, 0:width]

        self.frame = convert(self.scaled, method)

def convert(array, method) -> str:
    """Do the conversion from image to unicode string"""

    mat = np.asarray(array)
    dithered_mat = dither(mat, method)
    frame = to_string(dithered_mat)
    return frame

def read_img_file(source: str) -> np.ndarray:
    """Read image using open cv and return as NumPy array"""

    return cv2.imread(source, 0)

def scale(img, new_width):
    """Scale an image to fit the given width."""

    original_width = img.shape[1]

    scaling_factor = new_width / original_width
    scaled_img = cv2.resize(
        src=img,
        dsize=None,
        fx=scaling_factor,
        fy=scaling_factor,
        interpolation=cv2.INTER_AREA,
    )
    return scaled_img

def dither(img: np.ndarray, method: int = 0) -> np.ndarray:
    """Binarize image using dithering algorithm if method > 0."""

    res = img.shape[:2]
    if method == 1:
        dithered = dithering.threshold(*res, img)
    elif method == 2:
        dithered = dithering.quantize(*res, img)
    elif method == 3:
        dithered = dithering.random(*res, img)
    else:
        return img

    return dithered

def to_string(array: np.ndarray):
    """
    Convert a NumPy array to the actual image using the boolean statuses
    from the array.
    """

    res = array.shape[:2]
    grid = brailleforming.subdivide(*res)
    frame = brailleforming.form_string(grid, array)
    return frame


import datetime

Tanggal = datetime.datetime.now()

Nama = input("Masukkan nama : ")
Usia = int(input("Masukkan usia saat ini : "))

def usia_50(x,y):
    a = int(50) - y
    b = Tanggal.year + a
    print("Nama anda", x, ",", "anda berusia", y, "tahun. Pada tahun", b, "anda akan berusia", 50, "tahun.")

usia_50(Nama,Usia)


'''
@author: Quarkonics
'''
import os
import zstackwoodpecker.test_util as test_util
import zstackwoodpecker.test_lib as test_lib
import zstackwoodpecker.test_state as test_state
import zstackwoodpecker.operations.host_operations as host_ops
import zstackwoodpecker.operations.resource_operations as res_ops
import zstackwoodpecker.operations.vm_operations as vm_ops

test_stub = test_lib.lib_get_test_stub()
test_obj_dict = test_state.TestStateDict()

def test():
    test_util.test_dsc('Test VM online change instance offering')

    cpuNum = 1
    memorySize = 555 * 1024 * 1024
    new_offering = test_lib.lib_create_instance_offering(cpuNum = cpuNum,\
            memorySize = memorySize)

    vm = test_stub.create_vm(vm_name = 'ckvmoffering-c7-64', image_name = "imageName_i_c7", instance_offering_uuid=new_offering.uuid)
    vm.check()
    test_obj_dict.add_vm(vm)
    test_obj_dict.add_instance_offering(new_offering)
    cpuNum = 1
    memorySize = 667 * 1024 * 1024
    new_offering = test_lib.lib_create_instance_offering(cpuNum = cpuNum,\
            memorySize = memorySize)

    test_obj_dict.add_instance_offering(new_offering)
    new_offering_uuid = new_offering.uuid
    vm.change_instance_offering(new_offering_uuid)
    vm.check()

    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('VM online change instance offering Test Pass')

#Will be called only if exception happens in test().
def error_cleanup():
    test_lib.lib_error_cleanup(test_obj_dict)


import requests
from data.config import api_link
from collections import Counter
from example_google_table import users # это экземпляр класса, а не список юзеров
from example_google_table import achievements_group_by_user_for_week

list_of_achievements = {'delicious_house': 'За вкусный дом 👩❤👨🏡',
                       'health': 'За занятие здоровьем 💊',
                       'language': 'За изучение языков 🇺🇸',
                       'music': 'За занятие музыкой 🎸',
                       'personal_care': 'Уход за собой 💆♀💇♂',
                       'proper_nutrition': 'За правильное питание 🥙',
                       'sport': 'За занятия спортом 🏋🏻♀🏋🏻',
                       'studying': 'За учёбу 🎓',
                       'work': 'За хорошую работу 🛠'
                       }

def print_achievements_of_users():
    list_of_users = users.id_and_name_of_users()
    for user in list_of_users:
        achievements_for_the_week_of_user = Counter(achievements_group_by_user_for_week[user[1]])
        #print('Пользователь', user[1])
        message = 'Привет, вот твои результаты за неделю!\n\n'
        for letter, count in achievements_for_the_week_of_user.items():
            message += (list_of_achievements[letter] + ' - ' + str(count) + '\n')
        #print(message)
        notifying = requests.get(api_link + f'/sendMessage?chat_id={user[0]}&text={message}')