#
# Author: Sai Kulkarni
# Email: saikulkarni0@gmail.com
# Project Title: Identifying the 'hand' in Texas Hold'em Poker
#
# Under Texas Hold'em poker rules, each player holds 2 cards in their hand.
# 5 additional cards are revealed on the table and shared by all players.
# From 5 table cards and 2 cards in his hands, each player can
# claim an optimal hand of 5 cards.
# https://en.wikipedia.org/wiki/Texas_hold_'em
#
# Each playing card can be represented with the following attributes:
#    1. A string indicating the suit
#    2. A number from 1-13, indicating value, where face cards are asigned numeric
#       values as follows: Jack = 11, Queen = 12, King = 13.
#    3. Ace can take 2 values, Ace = 1 for (Ace, 2, 3, 4, 5, ...) and
#       Ace = 14 for (10 Jack Queen King Ace)
#
# The ranking of hands in Texas Hold'em rules is as follows:
#
# 1. Straight flush      5 Consecutive values of same suit
# 2. Four of a Kind      4 cards of the same value
# 3. Full House          3 Cards of the same value and 1 pair of different value
# 4. Flush               5 cards of the same suit
# 5. Straight            5 cards of any suit with consecutive values
# 6. Three of a kind     3 cards of same value
# 7. Two pair            2 pairs of 2 cards with same value
# 8. One pair            2 cards with same value
# 9. High card           Using highest value card for comparison, in absence other hands
#
#
# This code sample contains the following components:
#    1. getRank: Given  a list of 5 table cards and a list of 2 player cards,
#       this returns the rank of the best hand possible for that player
#    2. dealCards : Given a number of players, this function returns the list of table cards
#    and list of lists of player cards
#    3. Unit tests to make sure 'getRank' returns correct rank for each possible hand from above list
#    4. Simulation to deal cards for 3 players, get their ranks and order them according to rank
# Note: For now, the case where 2 players have same rank and the tie-break depends on
# the values of cards in their hand is not handled. For example, a hand with pair of 3s and hand with pair of
# 10s currently will have the same rank
#

import itertools # For generating deck of cards using cartesian product
import random # For dealing cards
import numpy as np

# Suit names
S = 'Spade'
H = 'Heart'
C = 'Club'
D = 'Diamond'

# Rank values for each hand
STRAIGHT_FLUSH = 0
FOUR_KIND = 1
FULL_HOUSE = 2
FLUSH = 3
STRAIGHT = 4
THREE_KIND = 5
TWO_PAIR = 6
ONE_PAIR = 7
HIGH_CARD = 8

# Names of hands
HAND_NAMES = ["Straight_flush", "Four_kind", "Full_House", "Flush", "Straight", "Three_kind",
         "Two_pair", "One_pair", "High_card"]

# Possible values for Ace for calculating straight
ACE_LOW = 1
ACE_HIGH = 14

# Checks if a given set of card values contains 5 consecutive values
#   - Ace can form 2 straights: (Ace 2 3 4 5) and (10 Jack Queen King Ace)
def isstraight(values):
    if ACE_LOW in values:
        values.append(ACE_HIGH)
    values.sort()
    prevval = values[0]
    count = 1
    for value in values[1:]:
        if value - prevval == 1:
            count +=1
        else:
            count = 1
        if count == 5:
            break
        prevval = value
    if count == 5:
        return True
    else:
        return False

# Checks if a given hand contains a straight flush by checking the following conditions:
#        - it contains a flush (at least 5 cards of same suit)
#        - the cards from same suit contain a straight

def isstraightflush(countsbysuit, cardsbysuit):
    max_suit = max(countsbysuit, key=countsbysuit.get)
    if countsbysuit[max_suit] < 5:
        return False
    else:
        return isstraight(cardsbysuit[max_suit])

# Checks if a hand is a full house by making sure that the following conditions are met:
#        -if there is one set of 3 cards with same values:
#        -if there is another set of 2/3 cards with same values
# The second condition can also be formutaed as: there are at most 2 values with count = 1
def isfullhouse(countsbyvalue):
    _isfullhouse = False
    if 3 in countsbyvalue.values():
        count_ones = 0
        for count in countsbyvalue.values():
            if count == 1:
                count_ones += 1
        if count_ones <=2:
            _isfullhouse = True
    return _isfullhouse

# Returns the rank of the card from list above given 5 table cards and 2 player cards
def getRank(playercards, tablecards):
    fullhand = tablecards + playercards

    # Preprocess to generate 3 data structures:
    # - dictionary of list of cards by suits
    # - dictionary of number of cards for each value
    # - all cards sorted by value in descending order

    cardsbysuit = {} # cards from each suit
    countsbysuit = {} # number of cards from each suit
    countsbyvalue = {} # number of cards of each value
    for suit, value in fullhand:
        if suit in cardsbysuit:
            cardsbysuit[suit].append(value)
            countsbysuit[suit] += 1
        else:
            cardsbysuit[suit] = [value]
            countsbysuit[suit] = 1
        if value in countsbyvalue:
            countsbyvalue[value] += 1
        else:
            countsbyvalue[value] = 1

    # Check each hand in ascending order of rank
    if isstraightflush(countsbysuit, cardsbysuit):
        # check if straight flush exists
        rank = STRAIGHT_FLUSH
    elif 4 in countsbyvalue.values():
        # checks if any value occurs 4 times
        rank = FOUR_KIND
    elif isfullhouse(countsbyvalue):
        # checks if there exists a value with 3 cards and another value with 2 cards
        rank = FULL_HOUSE
    elif max(countsbysuit.values()) >= 5:
        # checks if any suit has 5 or higher number of cards in the set of 7
        rank = FLUSH
    elif isstraight(list(countsbyvalue.keys())):
        # checks if any 5 cards have consecutive values
        rank = STRAIGHT
    elif 3 in countsbyvalue.values():
        # checks if any value occurs 3 times
        rank = THREE_KIND
    elif 2 in countsbyvalue.values():
        numpairs = 0
        for count in countsbyvalue.values():
            if count == 2:
                numpairs += 1
        if numpairs >= 2:
            # More than 1 pair detected
            rank = TWO_PAIR
        else:
            # Exactly 1 pair detected
            rank = ONE_PAIR
    else:
        # No other combination exists
        rank = HIGH_CARD

    return rank

# Randomly deals 2 cards to each player and 5 on the table
def dealCards(numPlayers=1):

    suits = [S, H, C, D]
    values = list(range(ACE_LOW, ACE_HIGH))
    deck = itertools.product(suits, values)
    deck = set(deck) # random.sample needs set input

    playercards = []
    for _ in range(numPlayers):
        cards = random.sample(deck, 2)
        for card in cards:
            deck.remove(card)
        playercards.append(cards)

    tablecards = random.sample(deck, 5)
    for card in tablecards:
        deck.remove(card)

    return (playercards, tablecards,)

# Test each type of hand:

def runtest(pc, tc, expected):
    name = HAND_NAMES[expected]
    print("TEST: " + name)
    rank = getRank(pc, tc)
    if  rank == expected:
        print("SUCCESS")
    else:
        print("ERROR: TEST FAILED: "+ name)

# Straight Flush
pc = [(S, 3), (S, 7)]
tc = [(D, 2), (S, 4), (S, 5), (C, 13), (S, 6)]
runtest(pc, tc, STRAIGHT_FLUSH)

# Four of a kind
pc = [(S, 3), (H, 3)]
tc = [(D, 3), (S, 4), (S, 5), (C, 3), (S, 6)]
runtest(pc, tc, FOUR_KIND)

# Full House
pc = [(S, 3), (H, 3)]
tc = [(D, 3), (S, 14), (S, 5), (C, 5), (C, 11)]
runtest(pc, tc, FULL_HOUSE)

# Flush
pc = [(S, 11), (H, 3)]
tc = [(S, 3), (S, 4), (S, 5), (C, 12), (S, 6)]
runtest(pc, tc, FLUSH)

# Straight
pc = [(S, 10), (D, 8)]
tc = [(H, 9), (S, 4), (S, 5), (C, 12), (H, 11)]
runtest(pc, tc, STRAIGHT)

# Three of a kind
pc = [(S, 10), (D, 5)]
tc = [(H, 9), (S, 4), (S, 5), (C, 12), (H, 5)]
runtest(pc, tc, THREE_KIND)

# Two Pair
pc = [(S, 10), (D, 5)]
tc = [(H, 9), (S, 4), (S, 5), (C, 12), (H, 10)]
runtest(pc, tc, TWO_PAIR)

# One pair
pc = [(S, 10), (D, 5)]
tc = [(H, 9), (S, 4), (S, 7), (C, 12), (H, 10)]
runtest(pc, tc, ONE_PAIR)

# High card
pc = [(S, 10), (D, 5)]
tc = [(H, 2), (S, 4), (S, 7), (C, 12), (H, 3)]
runtest(pc, tc, HIGH_CARD)

print("\n\n")

# Simulation:
# With randomly dealt hands, generate a ranking order for all players.
# As mentioned earlier We are currently not handling the case where 'rank' is
# same for two players and the tie-breaker depends on the value of the cards
# in their hand

print("SIMULATION: Multiplayer game")
playercards, tablecards = dealCards(3)
print("Table Cards")
print(tablecards)
ranks = []

for pc in playercards:
    rank = getRank(pc, tablecards)
    ranks.append(rank)
sorted_order = np.argsort(np.array(ranks))

for idx in list(sorted_order):
    print("Player " + str(idx) + " has a " + HAND_NAMES[ranks[idx]])
    print("Player " + str(idx) + " cards " + str(playercards[idx]))


from unittest.mock import MagicMock
from unittest.mock import patch

import pytest
import saltext.saltext_apache.states.apache_site as apache_site

@pytest.fixture
def configure_loader_modules():
    return {apache_site: {}}

def test_enabled():
    """
    Test to ensure an Apache site is enabled.
    """
    name = "saltstack.com"

    ret = {"name": name, "result": True, "changes": {}, "comment": ""}

    mock = MagicMock(side_effect=[True, False, False])
    mock_str = MagicMock(return_value={"Status": ["enabled"]})
    with patch.dict(
        apache_site.__salt__,
        {"apache.check_site_enabled": mock, "apache.a2ensite": mock_str},
    ):
        comt = f"{name} already enabled."
        ret.update({"comment": comt})
        assert apache_site.enabled(name) == ret

        comt = f"Apache site {name} is set to be enabled."
        ret.update({"comment": comt, "result": None, "changes": {"new": name, "old": None}})
        with patch.dict(apache_site.__opts__, {"test": True}):
            assert apache_site.enabled(name) == ret

        comt = f"Failed to enable {name} Apache site"
        ret.update({"comment": comt, "result": False, "changes": {}})
        with patch.dict(apache_site.__opts__, {"test": False}):
            assert apache_site.enabled(name) == ret

def test_disabled():
    """
    Test to ensure an Apache site is disabled.
    """
    name = "saltstack.com"

    ret = {"name": name, "result": None, "changes": {}, "comment": ""}

    mock = MagicMock(side_effect=[True, True, False])
    mock_str = MagicMock(return_value={"Status": ["disabled"]})
    with patch.dict(
        apache_site.__salt__,
        {"apache.check_site_enabled": mock, "apache.a2dissite": mock_str},
    ):
        comt = f"Apache site {name} is set to be disabled."
        ret.update({"comment": comt, "changes": {"new": None, "old": name}})
        with patch.dict(apache_site.__opts__, {"test": True}):
            assert apache_site.disabled(name) == ret

        comt = f"Failed to disable {name} Apache site"
        ret.update({"comment": comt, "result": False, "changes": {}})
        with patch.dict(apache_site.__opts__, {"test": False}):
            assert apache_site.disabled(name) == ret

        comt = f"{name} already disabled."
        ret.update({"comment": comt, "result": True})
        assert apache_site.disabled(name) == ret


import pytest
from api.serialization.util import instance_to_model
from api.models.features._basetypes.regional_connectivity import SiibraRegionalConnectivityModel
from api.serialization.util.siibra import RegionalConnectivity, StreamlineLengths, StreamlineCounts, FunctionalConnectivity, parcellations
import os

jba29 = parcellations['2.9']

streamline_counts = [f for f in StreamlineCounts.get_instances()]
streamline_lengths = [f for f in StreamlineLengths.get_instances()]
functional_conn = [f for f in FunctionalConnectivity.get_instances()]

all_connectivity = [
    *streamline_counts,
    *streamline_lengths,
    *functional_conn,
]

@pytest.mark.parametrize('conn', all_connectivity)
def test_conn(conn: RegionalConnectivity):
    model = instance_to_model(conn)
    assert isinstance(model, SiibraRegionalConnectivityModel)
    model.dict()

if not os.getenv("FAST_RUN"):
    @pytest.mark.parametrize("conn,subject",
                            [(f, s)
                                for f in all_connectivity
                                if jba29 in f.anchor.parcellations
                                for s in f.subjects],
                            ids=lambda arg: f"name: {arg.name!r} paradigm: {arg.paradigm if hasattr(arg, 'paradigmn') else ''}"
                                if isinstance(arg, RegionalConnectivity)
                                else f"subject: {arg!r}")
    def test_conn_subject(conn: RegionalConnectivity, subject: str):
        model = instance_to_model(conn, subject=subject, detail=True)
        assert isinstance(model, SiibraRegionalConnectivityModel)
        assert model.matrices is not None
        model.dict()


class Person:
    def __init__(self, name, last_name):
        self.name = name
        self.last_name = last_name

    def greeting_by_name(self):
        return f"Hello {self.name}"

name = "Mike"
last_name = "Haralanov"
person = Person(name, last_name)
person1 = Person("Kalin", "Petrov")

print(person.name)
print(person.last_name)

person.name = "Ivan"   # changing the name // if we update the attribute to _name (private attribute), cannot be changed

print(person.name)
print(person.greeting_by_name())

print(person1.greeting_by_name())


import argparse

import numpy as np
import cv2 as cv

from lpd_yunet import LPD_YuNet

def str2bool(v):
    if v.lower() in ['on', 'yes', 'true', 'y', 't']:
        return True
    elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
        return False
    else:
        raise NotImplementedError

backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
help_msg_targets = "Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
try:
    backends += [cv.dnn.DNN_BACKEND_TIMVX]
    targets += [cv.dnn.DNN_TARGET_NPU]
    help_msg_backends += "; {:d}: TIMVX"
    help_msg_targets += "; {:d}: NPU"
except:
    print('This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')

parser = argparse.ArgumentParser(description='LPD-YuNet for License Plate Detection')
parser.add_argument('--input', '-i', type=str, help='Path to the input image. Omit for using default camera.')
parser.add_argument('--model', '-m', type=str, default='license_plate_detection_lpd_yunet_2022may.onnx', help='Path to the model.')
parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
parser.add_argument('--conf_threshold', type=float, default=0.9, help='Filter out faces of confidence < conf_threshold.')
parser.add_argument('--nms_threshold', type=float, default=0.3, help='Suppress bounding boxes of iou >= nms_threshold.')
parser.add_argument('--top_k', type=int, default=5000, help='Keep top_k bounding boxes before NMS.')
parser.add_argument('--keep_top_k', type=int, default=750, help='Keep keep_top_k bounding boxes after NMS.')
parser.add_argument('--save', '-s', type=str2bool, default=False, help='Set true to save results. This flag is invalid when using camera.')
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
args = parser.parse_args()

def visualize(image, dets, line_color=(0, 255, 0), text_color=(0, 0, 255), fps=None):
    output = image.copy()

    if fps is not None:
        cv.putText(output, 'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, text_color)

    for det in dets:
        bbox = det[:-1].astype(np.int32)
        x1, y1, x2, y2, x3, y3, x4, y4 = bbox

        # Draw the border of license plate
        cv.line(output, (x1, y1), (x2, y2), line_color, 2)
        cv.line(output, (x2, y2), (x3, y3), line_color, 2)
        cv.line(output, (x3, y3), (x4, y4), line_color, 2)
        cv.line(output, (x4, y4), (x1, y1), line_color, 2)

    return output

if __name__ == '__main__':
    # Instantiate LPD-YuNet
    model = LPD_YuNet(modelPath=args.model,
                      confThreshold=args.conf_threshold,
                      nmsThreshold=args.nms_threshold,
                      topK=args.top_k,
                      keepTopK=args.keep_top_k,
                      backendId=args.backend,
                      targetId=args.target)

    # If input is an image
    if args.input is not None:
        image = cv.imread(args.input)
        h, w, _ = image.shape

        # Inference
        model.setInputSize([w, h])
        results = model.infer(image)

        # Print results
        print('{} license plates detected.'.format(results.shape[0]))

        # Draw results on the input image
        image = visualize(image, results)

        # Save results if save is true
        if args.save:
            print('Resutls saved to result.jpg')
            cv.imwrite('result.jpg', image)

        # Visualize results in a new window
        if args.vis:
            cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE)
            cv.imshow(args.input, image)
            cv.waitKey(0)
    else: # Omit input to call default camera
        deviceId = 0
        cap = cv.VideoCapture(deviceId)
        w = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
        h = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
        model.setInputSize([w, h])

        tm = cv.TickMeter()
        while cv.waitKey(1) < 0:
            hasFrame, frame = cap.read()
            if not hasFrame:
                print('No frames grabbed!')
                break

            # Inference
            tm.start()
            results = model.infer(frame) # results is a tuple
            tm.stop()

            # Draw results on the input image
            frame = visualize(frame, results, fps=tm.getFPS())

            # Visualize results in a new Window
            cv.imshow('LPD-YuNet Demo', frame)

            tm.reset()


NO = ["no", "false", "n", "f", "0", "stop", "exit", "quit"]

def main():
    print("""
    ##################################
    ##       Colour Convertor       ##
    ##################################""")

    again = 1
    while again == 1:
        main_convertor()
        asked = input("    Again? ")
        again = 0 if asked.lower() in NO else 1
    print("\n    Stopping...")

def main_convertor():
    colour = input("\n    Input (R, G, B), (C%, M%, Y%, K%) or #HEX to get started.\n        ")
    if colour in NO or len(colour) == 0:
        pass
    elif colour[0] == "#":
        h, e, x = colour[1:3], colour[3:5], colour[5:7]
        if len(h) == 0 or len(e) == 0 or len(x) == 0:
            print("    Nah, mate.")
            main_convertor()
        hex_convertor(h, e, x)
    else:
        try:
            r, g, b = map(int, colour.strip("()").split(", "))
            rgb_convertor(r, g, b)
        except ValueError:
            try:
                c, m, y, k = map(int, colour.strip("()").rstrip("%").split("%, "))
                cmyk_convertor(c, m, y, k)
            except ValueError:
                print("    Nah, mate.")
                main_convertor()

def hex_convertor(h, e, x):
    r, g, b = hex_to_rgb(h, e, x)
    rgb_convertor(r, g, b)

def rgb_convertor(r, g, b):
    c, m, y, k = rgb_to_cmyk(r, g, b)
    build_and_print(r, g, b, c, m, y, k)

def cmyk_convertor(c, m, y, k):
    r, g, b = cmyk_to_rgb(c, m, y, k)
    build_and_print(r, g, b, c, m, y, k)

def rgb_to_hex(r, g, b):
    return "#{0:x}{1:x}{2:x}".format(r, g, b)

def rgb_to_cmyk(r, g, b):
    k = 1 - (max(r, g, b) / 255)
    c = (1 - r / 255 - k) / (1 - k)
    m = (1 - g / 255 - k) / (1 - k)
    y = (1 - b / 255 - k) / (1 - k)
    return round(c * 100), round(m * 100), round(y * 100), round(k * 100)

def cmyk_to_rgb(c, m, y, k):
    r = int(255 * (1 - c / 100) * (1 - k / 100))
    g = int(255 * (1 - m / 100) * (1 - k / 100))
    b = int(255 * (1 - y / 100) * (1 - k / 100))
    return r, g, b

def hex_to_rgb(h, e, x):
    return int(h, 16), int(e, 16), int(x, 16)

def build_rgb(r, g, b):
    return "({0}, {1}, {2})".format(r, g, b)

def build_cmyk(c, m, y, k):
    return "({0}%, {1}%, {2}%, {3}%)".format(c, m, y, k)

def build_and_print(r, g, b, c, m, y, k):
    rgb = build_rgb(r, g, b)
    cmyk = build_cmyk(c, m, y, k)
    hexa = rgb_to_hex(r, g, b)
    print("\n    RGB:\t", rgb, "\n    CMYK:\t", cmyk, "\n    HEX:\t", hexa, "\n")

if __name__ == "__main__":
    main()


import pandas as pd
from applications.DataFacadeOOB.code.ActionDefinition.string_column_replace_null.python.ExecutionHandler import ExecutionHandler

def test_string_replace_null():
    df = pd.DataFrame({'x': ["jon", "app", "mark", None]})

    handler = ExecutionHandler()
    df = handler.execute(df, 'x', "invalid")

    expected = pd.DataFrame({'x': ["jon", "app", "mark", "invalid"]})
    pd.testing.assert_frame_equal(expected, df)


import i2c
import streams

streams.serial()

class Adafruit_SGP30(i2c.I2C):
    """A driver for the SGP30 gas sensor."""

    _SGP30_DEFAULT_I2C_ADDR = 0x58
    _SGP30_FEATURESETS = (0x0020, 0x0022)

    _SGP30_CRC8_POLYNOMIAL = 0x31
    _SGP30_CRC8_INIT = 0xFF
    _SGP30_WORD_LEN = 2

    def __init__(self,i2cdrv,address=_SGP30_DEFAULT_I2C_ADDR,clk=100000):
        i2c.I2C.__init__(self,i2cdrv,address,clk)
        self.addr = address

    def TVOC(self):
        """Total Volatile Organic Compound in parts per billion."""
        return self.iaq_measure()[1]

    def baseline_TVOC(self):
        """Total Volatile Organic Compound baseline value"""
        return self.get_iaq_baseline()[1]

    def eCO2(self):
        """Carbon Dioxide Equivalent in parts per million"""
        return self.iaq_measure()[0]

    def baseline_eCO2(self):
        """Carbon Dioxide Equivalent baseline value"""
        return self.get_iaq_baseline()[0]

    def iaq_init(self):
        """Initialize the IAQ algorithm"""
        # name, command, signals, delay
        self._run_profile(["iaq_init", [0x20, 0x03], 0, 10])

    def iaq_measure(self):
        """Measure the eCO2 and TVOC"""
        # name, command, signals, delay
        return self._run_profile(["iaq_measure", [0x20, 0x08], 2, 50])

    def get_iaq_baseline(self):
        """Retreive the IAQ algorithm baseline for eCO2 and TVOC"""
        # name, command, signals, delay
        return self._run_profile(["iaq_get_baseline", [0x20, 0x15], 2, 10])

    def set_iaq_baseline(self, eCO2, TVOC):
        """Set the previously recorded IAQ algorithm baseline for eCO2 and TVOC"""
        if eCO2 == 0 and TVOC == 0:
            print("Invalid baseline")
            return
        buffer = []
        for value in [TVOC, eCO2]:
            arr = [value >> 8, value & 0xFF]
            arr.append(self._generate_crc(arr))
            buffer += arr
        self._run_profile(["iaq_set_baseline", [0x20, 0x1E] + buffer, 0, 10])

    def set_iaq_humidity(self, gramsPM3):
        """Set the humidity in g/m3 for eCO2 and TVOC compensation algorithm"""
        tmp = int(gramsPM3 * 256)
        buffer = []
        for value in [tmp]:
            arr = [value >> 8, value & 0xFF]
            arr.append(self._generate_crc(arr))
            buffer += arr
        self._run_profile(["iaq_set_humidity", [0x20, 0x61] + buffer, 0, 10])

    def _run_profile(self, profile):
        """Run an SGP 'profile' which is a named command set"""
        name, command, signals, delay = profile

        return self._i2c_read_words_from_cmd(command, delay, signals)

    def _i2c_read_words_from_cmd(self, command, delay, reply_size):
        """Run an SGP command query, get a reply and CRC results if necessary"""

        self.write(command)
        sleep(delay)
        if not reply_size:
            return None
        crc_result = reply_size * (self._SGP30_WORD_LEN + 1)
        data = self.read(crc_result)
        result = []
        for i in range(reply_size):
            word = [data[3 * i], data[3 * i + 1]]
            crc = data[3 * i + 2]
            if self._generate_crc(word) != crc:
                print("CRC Error")
            result.append(word[0] << 8 | word[1])
        return result

    def _generate_crc(self, data):
        """8-bit CRC algorithm for checking data"""
        crc = self._SGP30_CRC8_INIT
        # calculates 8-Bit checksum with given polynomial
        for byte in data:
            crc ^= byte
            for _ in range(8):
                if crc & 0x80:
                    crc = (crc << 1) ^ self._SGP30_CRC8_POLYNOMIAL
                else:
                    crc <<= 1
        return crc & 0xFF


from sglib.models.daw import *
from sgui.daw.shared import *
from sglib.lib.util import *
from sgui.sgqt import *

from sgui import shared as glbl_shared
from sgui.daw import shared
from sglib import constants
from sgui.daw.lib import item as item_lib
from sglib.lib import util
from sglib.lib.translate import _

class TimePitchDialogWidget:
    def __init__(self, a_audio_item):
        self.widget = QDialog(parent=glbl_shared.MAIN_WINDOW.widget)
        self.widget.setWindowTitle(_("Time/Pitch..."))
        self.widget.setMaximumWidth(480)
        self.main_vlayout = QVBoxLayout(self.widget)

        self.layout = QGridLayout()
        self.main_vlayout.addLayout(self.layout)

        self.vlayout2 = QVBoxLayout()
        self.layout.addLayout(self.vlayout2, 1, 1)
        self.start_hlayout = QHBoxLayout()
        self.vlayout2.addLayout(self.start_hlayout)

        self.timestretch_hlayout = QHBoxLayout()
        self.time_pitch_gridlayout = QGridLayout()
        self.vlayout2.addLayout(self.timestretch_hlayout)
        self.vlayout2.addLayout(self.time_pitch_gridlayout)
        self.timestretch_hlayout.addWidget(QLabel(_("Mode:")))
        self.timestretch_mode = QComboBox()

        self.timestretch_mode.setMinimumWidth(240)
        self.timestretch_hlayout.addWidget(self.timestretch_mode)
        self.timestretch_mode.addItems(TIMESTRETCH_MODES)
        self.timestretch_mode.setCurrentIndex(a_audio_item.time_stretch_mode)
        self.timestretch_mode.currentIndexChanged.connect(
            self.timestretch_mode_changed)
        self.time_pitch_gridlayout.addWidget(QLabel(_("Pitch:")), 0, 0)
        self.pitch_shift = QDoubleSpinBox()
        self.pitch_shift.setRange(-36, 36)
        self.pitch_shift.setValue(float(a_audio_item.pitch_shift))
        self.pitch_shift.setDecimals(6)
        self.time_pitch_gridlayout.addWidget(self.pitch_shift, 0, 1)

        self.pitch_shift_end_checkbox = QCheckBox(_("End:"))
        self.pitch_shift_end_checkbox.setChecked(
            a_audio_item.pitch_shift != a_audio_item.pitch_shift_end)
        self.pitch_shift_end_checkbox.toggled.connect(
            self.pitch_end_mode_changed)
        self.time_pitch_gridlayout.addWidget(
            self.pitch_shift_end_checkbox, 0, 2)
        self.pitch_shift_end = QDoubleSpinBox()
        self.pitch_shift_end.setRange(-36, 36)
        self.pitch_shift_end.setValue(float(a_audio_item.pitch_shift_end))
        self.pitch_shift_end.setDecimals(6)
        self.time_pitch_gridlayout.addWidget(self.pitch_shift_end, 0, 3)

        self.time_pitch_gridlayout.addWidget(QLabel(_("Time:")), 1, 0)
        self.timestretch_amt = QDoubleSpinBox()
        self.timestretch_amt.setRange(0.1, 200.0)
        self.timestretch_amt.setDecimals(6)
        self.timestretch_amt.setSingleStep(0.1)
        self.timestretch_amt.setValue(float(a_audio_item.timestretch_amt))
        self.time_pitch_gridlayout.addWidget(self.timestretch_amt, 1, 1)

        self.crispness_layout = QHBoxLayout()
        self.vlayout2.addLayout(self.crispness_layout)
        self.crispness_layout.addWidget(QLabel(_("Crispness")))
        self.crispness_combobox = QComboBox()
        self.crispness_combobox.addItems(CRISPNESS_SETTINGS)
        self.crispness_combobox.setCurrentIndex(a_audio_item.crispness)
        self.crispness_layout.addWidget(self.crispness_combobox)

        self.timestretch_amt_end_checkbox = QCheckBox(_("End:"))
        self.timestretch_amt_end_checkbox.toggled.connect(
            self.timestretch_end_mode_changed)
        self.time_pitch_gridlayout.addWidget(
            self.timestretch_amt_end_checkbox, 1, 2)
        self.timestretch_amt_end = QDoubleSpinBox()
        self.timestretch_amt_end.setRange(0.2, 4.0)
        self.timestretch_amt_end.setDecimals(6)
        self.timestretch_amt_end.setSingleStep(0.1)
        self.timestretch_amt_end.setValue(
            float(a_audio_item.timestretch_amt_end),
        )
        self.time_pitch_gridlayout.addWidget(self.timestretch_amt_end, 1, 3)

        self.timestretch_mode_changed(0)

        self.timestretch_mode.currentIndexChanged.connect(
            self.timestretch_changed)
        self.pitch_shift.valueChanged.connect(self.timestretch_changed)
        self.pitch_shift_end.valueChanged.connect(self.timestretch_changed)
        self.timestretch_amt.valueChanged.connect(self.timestretch_changed)
        self.timestretch_amt_end.valueChanged.connect(self.timestretch_changed)
        self.crispness_combobox.currentIndexChanged.connect(
            self.timestretch_changed)

        self.ok_layout = QHBoxLayout()
        self.ok = QPushButton(_("OK"))
        self.ok.pressed.connect(self.ok_handler)
        self.ok_layout.addWidget(self.ok)
        self.cancel = QPushButton(_("Cancel"))
        self.cancel.pressed.connect(self.widget.close)
        self.ok_layout.addWidget(self.cancel)
        self.vlayout2.addLayout(self.ok_layout)

        self.last_open_dir = HOME

    def timestretch_end_mode_changed(self, a_val=None):
        if not self.timestretch_amt_end_checkbox.isChecked():
            self.timestretch_amt_end.setValue(
                float(self.timestretch_amt.value()),
            )

    def pitch_end_mode_changed(self, a_val=None):
        if not self.pitch_shift_end_checkbox.isChecked():
            self.pitch_shift_end.setValue(
                float(self.pitch_shift.value()),
            )

    def end_mode_changed(self, a_val=None):
        self.end_mode_checkbox.setChecked(True)

    def timestretch_changed(self, a_val=None):
        if not self.pitch_shift_end_checkbox.isChecked():
            self.pitch_shift_end.setValue(
                float(self.pitch_shift.value()),
            )
        if not self.timestretch_amt_end_checkbox.isChecked():
            self.timestretch_amt_end.setValue(
                float(self.timestretch_amt.value()),
            )

    def timestretch_mode_changed(self, a_val=None):
        a_val = util.TIMESTRETCH_INDEXES[
            str(self.timestretch_mode.currentText())]
        if a_val == 0:
            self.pitch_shift.setEnabled(False)
            self.timestretch_amt.setEnabled(False)
            self.pitch_shift.setValue(0.0)
            self.pitch_shift_end.setValue(0.0)
            self.timestretch_amt.setValue(1.0)
            self.timestretch_amt_end.setValue(1.0)
            self.timestretch_amt_end_checkbox.setEnabled(False)
            self.timestretch_amt_end_checkbox.setChecked(False)
            self.pitch_shift_end_checkbox.setEnabled(False)
            self.pitch_shift_end_checkbox.setChecked(False)
            self.crispness_combobox.setCurrentIndex(5)
            self.crispness_combobox.setEnabled(False)
        elif a_val == 1:
            self.pitch_shift.setEnabled(True)
            self.timestretch_amt.setEnabled(False)
            self.timestretch_amt.setValue(1.0)
            self.timestretch_amt_end.setValue(1.0)
            self.timestretch_amt_end.setEnabled(False)
            self.timestretch_amt_end_checkbox.setEnabled(False)
            self.timestretch_amt_end_checkbox.setChecked(False)
            self.pitch_shift_end_checkbox.setEnabled(True)
            self.pitch_shift_end.setEnabled(True)
            self.crispness_combobox.setCurrentIndex(5)
            self.crispness_combobox.setEnabled(False)
        elif a_val == 2:
            self.pitch_shift.setEnabled(False)
            self.timestretch_amt.setEnabled(True)
            self.pitch_shift.setValue(0.0)
            self.pitch_shift_end.setValue(0.0)
            self.pitch_shift_end.setEnabled(False)
            self.timestretch_amt_end.setEnabled(True)
            self.timestretch_amt_end_checkbox.setEnabled(True)
            self.pitch_shift_end_checkbox.setEnabled(False)
            self.pitch_shift_end_checkbox.setChecked(False)
            self.crispness_combobox.setCurrentIndex(5)
            self.crispness_combobox.setEnabled(False)
        elif a_val == 3 or a_val == 4:
            self.pitch_shift.setEnabled(True)
            self.pitch_shift_end.setEnabled(False)
            self.timestretch_amt.setEnabled(True)
            self.timestretch_amt_end_checkbox.setEnabled(False)
            self.timestretch_amt_end_checkbox.setChecked(False)
            self.pitch_shift_end_checkbox.setEnabled(False)
            self.pitch_shift_end_checkbox.setChecked(False)
            self.crispness_combobox.setEnabled(True)
        elif a_val == 5:
            self.pitch_shift.setEnabled(True)
            self.pitch_shift_end.setEnabled(True)
            self.timestretch_amt.setEnabled(True)
            self.timestretch_amt_end.setEnabled(True)
            self.timestretch_amt_end_checkbox.setEnabled(True)
            self.pitch_shift_end_checkbox.setEnabled(True)
            self.crispness_combobox.setCurrentIndex(5)
            self.crispness_combobox.setEnabled(False)
        elif a_val == 6:
            self.pitch_shift.setEnabled(True)
            self.timestretch_amt.setEnabled(True)
            self.timestretch_amt_end.setEnabled(False)
            self.pitch_shift_end.setEnabled(False)
            self.timestretch_amt_end_checkbox.setEnabled(False)
            self.timestretch_amt_end_checkbox.setChecked(False)
            self.pitch_shift_end_checkbox.setEnabled(False)
            self.pitch_shift_end_checkbox.setChecked(False)
            self.crispness_combobox.setCurrentIndex(5)
            self.crispness_combobox.setEnabled(False)

    def ok_handler(self):
        if glbl_shared.IS_PLAYING:
            QMessageBox.warning(
                self.widget,
                _("Error"),
                _("Cannot edit audio items during playback"),
            )
            return

        self.end_mode = 0

        f_selected_count = 0

        f_was_stretching = False

        for f_item in shared.AUDIO_SEQ.audio_items:
            if f_item.isSelected():
                f_new_ts_mode = self.timestretch_mode.currentIndex()
                f_new_ts = round(self.timestretch_amt.value(), 6)
                f_new_ps = round(self.pitch_shift.value(), 6)
                if self.timestretch_amt_end_checkbox.isChecked():
                    f_new_ts_end = round(
                        self.timestretch_amt_end.value(), 6)
                else:
                    f_new_ts_end = f_new_ts
                if self.pitch_shift_end_checkbox.isChecked():
                    f_new_ps_end = round(self.pitch_shift_end.value(), 6)
                else:
                    f_new_ps_end = f_new_ps
                f_item.audio_item.crispness = \
                    self.crispness_combobox.currentIndex()

                if ((f_item.audio_item.time_stretch_mode >= 3) or
                (f_item.audio_item.time_stretch_mode == 1 and \
                (f_item.audio_item.pitch_shift_end !=
                    f_item.audio_item.pitch_shift)) or \
                (f_item.audio_item.time_stretch_mode == 2 and \
                (f_item.audio_item.timestretch_amt_end !=
                    f_item.audio_item.timestretch_amt))) and \
                ((f_new_ts_mode == 0) or \
                (f_new_ts_mode == 1 and f_new_ps == f_new_ps_end) or \
                (f_new_ts_mode == 2 and f_new_ts == f_new_ts_end)):
                    f_item.audio_item.uid = \
                        constants.PROJECT.timestretch_get_orig_file_uid(
                            f_item.audio_item.uid)

                f_item.audio_item.time_stretch_mode = f_new_ts_mode
                f_item.audio_item.pitch_shift = f_new_ps
                f_item.audio_item.timestretch_amt = f_new_ts
                f_item.audio_item.pitch_shift_end = f_new_ps_end
                f_item.audio_item.timestretch_amt_end = f_new_ts_end
                f_item.draw()
                f_item.clip_at_sequence_end()
                if (
                    f_new_ts_mode >= 3
                    or
                    (f_new_ts_mode == 1 and f_new_ps != f_new_ps_end)
                    or
                    (f_new_ts_mode == 2 and f_new_ts != f_new_ts_end)
                    and
                    f_item.orig_string != str(f_item.audio_item)
                ):
                    f_was_stretching = True
                    try:
                        constants.PROJECT.timestretch_audio_item(
                            f_item.audio_item,
                        )
                    except FileNotFoundError as ex:
                        QMessageBox.warning(
                            self.widget,
                            _("Error"),
                            str(ex),
                        )
                        global_open_audio_items(True)
                        return

                f_item.draw()
                f_selected_count += 1
        if f_selected_count == 0:
            QMessageBox.warning(
                self.widget, _("Error"), _("No items selected"))
        else:
            item_lib.save_item(shared.CURRENT_ITEM_NAME, shared.CURRENT_ITEM)
            global_open_audio_items(True)
            constants.DAW_PROJECT.commit(_("Update audio items"))
        self.widget.close()


import urllib3
import requests
import sys
from bs4 import BeautifulSoup
import argparse
import webbrowser
from colored import fg

# include these variables in a print() function to output different clours to your terminal
txt_blue = fg('blue')
txt_green = fg('green')
txt_white = fg('white')
txt_red = fg('red')

# The Website address we will be targeting
url = "https://www.thephonebook.bt.com/Person/PersonSearch"

# Including generic User-Agent http header to make us look like a browser
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}

# A function for outputing the http response to make sure that bt.com is still talking back to us!
# Pass the http response as the argument 'r'
def http_status(r):
    # Generally a http status code higher than 200 is bad news
	if r.status_code == 200:
		traffic = txt_green
		print(txt_blue + "  (Info)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)
	elif r.status_code <= 400:
		traffic = txt_blue
		print(txt_red + "  (warn)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)
	else:
		traffic = txt_red
		print(txt_red + "  (warn)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)

# Start scanning using a wordlist of names to bruteforce phone numbers
def scan_wordlist(street, area, wordlist, output):
    # Define variables
    numsSeen = set()
    num = set()

    # Open wordlist
    with open(wordlist) as f:
        list = f.read().splitlines()
    for name in list:

        # Here is where we package our parameters that we will send to 'bt.com', they are put into an array that will be passed to 'requests.get' as params
        payload = {"Surname": name, "Location": area, "Street": street}

        # We will wait 15 seconds for a response and try again, if we fail to get a response 3 times we skip that name and move on.
        try:
            r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
        except:
            print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, retrying")
            try:
                r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
            except:
                print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, twice, retrying")
                try:
                    r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
                except:
                    print(txt_red + "  (warn)\t" + txt_white + name + " Timed out, tree times, skipping")
                    pass

        # Print http status to terminal
        http_status(r)
        print('trying:\t\t' + txt_blue + name + txt_white, end='\r')

        # Decode response from binary into UTF-8 charcters
        ret = r.content.decode('utf-8')

        # Use Beautiful Soup Module to parse html then find and extract the phone number
        soup = BeautifulSoup(ret, "html.parser")
        nums = soup.select("a[href^=\"tel\"]")

        for element in nums:

            # Extract inner content of 'html'  tags that are found
            num = element.decode_contents()

            # We want to remember numbers we have seen before so that we don't get repeats
            if num not in numsSeen:
                print(str(num) + " returned by: " + str(name))
                if output is not None:
                    o.write(str(element) + " | ")
                numsSeen.add(num)
                print(name, end='\r')
                if output is not None:
                    o.write("returned by: " + name + "
")
    # If outputing to a 'html' file, append closing tabs( </...> ) to the end of the file
    if output is not None:
        o.write("")
        o.close()
        webbrowser.open_new_tab(output)

    # Success
    return 0

# Start a scan only once using a single surname
def scan_surname(street, area, surname, output):

    # Here is where we package our parameters that we will send to 'bt.com', they are put into an array that will be passed to 'requests.get' as params
    payload = {"Surname": surname, "Location": area, "Street": street}

    # We will wait 15 seconds for a response and try again, if we fail to get a response 3 times we skip that name and move on.
    try:
        r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
    except:
        print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, retrying")
        try:
            r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
        except:
            print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, twice, retrying")
            try:
                r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
            except:
                print(txt_red + "  (warn)\t" + txt_white + name + " Timed out, tree times, skipping")
                pass

    # Print http status to terminal
    http_status(r)

    # Decode response from binary into UTF-8 charcters
    ret = r.content.decode('utf-8')

    # Use Beautiful Soup Module to parse html then find and extract the phone number
    soup = BeautifulSoup(ret, "html.parser")
    nums = soup.select("a[href^=\"tel\"]")

    # If there we have found a phone number
    if (len(nums) > 1):
        for element in nums:

            # Extract inner content of 'html'  tags that are found
            num = element.decode_contents()
            print(num)

            # If outputing to a 'html' file, append closing tabs( </...> ) to the end of the file
            if output is not None:
                o.write(str(num))
                o.write("")
                o.close()
                # Finally open the default webbrowser and show the page
                webbrowser.open_new_tab(output)
    else:
    	print(txt_red + '(Notice)\t' + txt_white + 'No phone numbers returned')
    return 0

# Initialize -- Start Here! Here is where we parse command line argyments.
if __name__ == "__main__":
    parser=argparse.ArgumentParser(
        description='''Here is my lovelly little python script to bruteforce phonenumbers from the bt phonebook :-). ''',
        epilog="""Josjuar Lister 2021-2022""")
    parser.add_argument('-n', '--surname', help='specify surname')
    parser.add_argument('-a', '--area', help='Area Town or Postcode', required=True)
    parser.add_argument('-s', '--street', help='specify street', required=True)
    parser.add_argument('-w', '--wordlist', help='input wordlist file')
    parser.add_argument('-o', '--output', help='output html file and open')

    # To use arguments parsed here call 'args.<argument>'
    args=parser.parse_args()

    # If we want to output to a html file, ...
    if args.output is not None:
        print("output: " + args.output)
        o = open(args.output, "a")
        o.write("
" + args.output + "

area: " + args.area + "

street: " + args.street + "
")

    ##Wordlist scan
    if args.wordlist is not None:
        sys.exit(scan_wordlist(args.street, args.area, args.wordlist, args.output))

    ##One person scan
    elif args.surname is not None:
        sys.exit(scan_surname(args.street, args.area, args.surname, args.output))
else:
    exit(1)


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

import os, time, random, shutil, math
import mxnet as mx
import numpy as np
import matplotlib.pyplot as plt

from mxnet import gluon
from mxnet import autograd
from mxnet import image
from mxnet import init
from mxnet import nd
from mxnet.gluon.model_zoo import vision as models

task = "skirt_length_labels"
warmup_label_dir = 'data/web/Annotations/skirt_length_labels.csv'
base_label_dir = 'data/base/Annotations/label.csv'

image_path = []

with open(warmup_label_dir, 'r') as f:
    lines = f.readlines()
    tokens = [l.rstrip().split(',') for l in lines]
    for path, _, label in tokens:
        image_path.append(('data/web/'+path, label))

with open(base_label_dir, 'r') as f:
    lines = f.readlines()
    tokens = [l.rstrip().split(',') for l in lines]
    for path, tk, label in tokens:
        if tk == task:
            image_path.append(('data/base/'+path, label))

def plot_image(img_path):
    with open(img_path, 'rb') as f:
        img = image.imdecode(f.read())
    plt.imshow(img.asnumpy())
    return img

# 分配训练验证集
n = len(image_path)
random.seed(1024)
random.shuffle(image_path)
train_count = 0
for path, label in image_path:
    label_index = list(label).index('y')
    if train_count < n * 0.9:
        shutil.copy(path, os.path.join('data/train_valid', task, 'train', str(label_index)))
    else:
        shutil.copy(path, os.path.join('data/train_valid', task, 'val', str(label_index)))
    train_count += 1

# 迁移学习，定义预训练网络
pretrained_net = models.resnet50_v2(pretrained=True)

num_gpu = 1
ctx = [mx.gpu(i) for i in range(num_gpu)] if num_gpu > 0 else [mx.cpu()]

finetune_net = models.resnet50_v2(classes=6)
finetune_net.features = pretrained_net.features
finetune_net.output.initialize(init.Xavier(), ctx=ctx)
finetune_net.collect_params().reset_ctx(ctx)
finetune_net.hybridize()

# 训练集图像增广
def aug_train(data, label):
    im = data.astype('float32') / 255
    auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256,
                                    rand_crop=True, rand_mirror=True,
                                    mean=np.array([0.485, 0.456, 0.406]),
                                    std=np.array([0.229, 0.224, 0.225]))
    for aug in auglist:
        im = aug(im)
    im = nd.transpose(im, (2, 0, 1))
    return (im, nd.array([label]).asscalar())

# 验证集图像增广
def aug_val(data, label):
    im = data.astype('float32') / 255
    auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256,
                                    mean=np.array([0.485, 0.456, 0.406]),
                                    std=np.array([0.229, 0.224, 0.225]))
    for aug in auglist:
        im = aug(im)
    im = nd.transpose(im, (2, 0, 1))
    return (im, nd.array([label]).asscalar())

# 计算average precision
def calculate_ap(labels, outputs):
    cnt = 0
    ap = 0.
    for label, outptu in zip(labels, outputs):
        for lb, op in zip(label.asnumpy().astype(np.int), outputs.asnumpy()):
            op_argsort = np.argsort(op)[::-1]
            lb_int = int(lb)
            ap += 1.0 / (1+list(op_argsort).index(lb_int))
            cnt += 1
    return ((ap, cnt))

# 在训练集上的测试
def validate(net, val_data, ctx):
    metric = mx.metric.Accuracy()
    L = gluon.loss.SoftmaxCrossEntropyLoss()
    AP = 0.
    AP_cnt = 0
    val_loss = 0.
    for i, batch in enumerate(val_data):
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False)
        label = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False)

        outputs = [net(X) for X in data]
        metric.update(label, outputs)
        loss = [L(yhat, y) for yhat, y in zip(outputs, label)]
        val_loss = sum([l.mean().asscalar() for l in loss]) / len(loss)
        ap, cnt = calculate_ap(label, outputs)
        AP += ap
        AP_cnt += cnt
    _, val_acc = metric.get()
    return ((val_acc, AP / AP_cnt, val_loss / len(val_data)))

lr = 1e-3
momentum = 0.9
wd = 1e-4
epochs = 5
batch_size = 64

train_path = os.path.join('data/train_valid', task, 'train')
val_path = os.path.join('data/train_valid', task, 'val')

# 训练集的DataLoader
train_data = gluon.data.DataLoader(gluon.data.vision.ImageFolderDataset(train_path, transform=aug_train),
                                   batch_size=batch_size, shuffle=True, num_workers=4)
# 验证集的DataLoader
val_data = gluon.data.DataLoader(gluon.data.vision.ImageFolderDataset(val_path, transform=aug_val),
                                 batch_size=batch_size, shuffle=False, num_workers=4)

trainer = gluon.Trainer(finetune_net.collect_params(), 'sgd', {'learning_rate': lr,
                                           'momentum': momentum, 'wd': wd})
L = gluon.loss.SoftmaxCrossEntropyLoss()
metric = mx.metric.Accuracy()

# train
for epoch in epochs:
    tic = time.time()

    train_loss = 0.
    metric.reset()
    AP = 0.
    AP_cnt = 0

    num_batch = len(train_data)

    for i, batch in enumerate(train_data):
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False)
        label = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False)

        with autograd.record():
            outputs = [finetune_net(X) for X in data]
            loss = [L(yhat, y) for yhat, y in zip(outputs, label)]
        for l in loss:
            l.backward()
        trainer.step(batch_size)
        train_loss += sum([l.mean().asscalar() for l in loss]) / len(loss)

        metric.update(label, outputs)
        ap, cnt = calculate_ap(label, outputs)
        AP += ap
        AP_cnt += cnt

    train_map = AP / AP_cnt
    _, train_acc = metric.get()
    train_loss /= num_batch

    val_acc, val_map, val_loss = validate(finetune_net, val_data, ctx)
    print('[Epoch %d] train-acc: %f, mAP: %f, loss: %f, | val-acc: %f, mAP: %f, loss: %f | time: %f'
          % (epoch, train_acc, train_map, train_loss, val_acc, val_map, val_loss, time.time()()-tic))


"""
Review Types
"""
from __future__ import annotations

import logging
import os
from dataclasses import dataclass, field
from typing import Iterator, List, Literal, Optional, Protocol, Tuple

import git
import unidiff

from sparrow.libs import config, scm

class BaseReview(Protocol):
    """
    Base Review Protocol
    """

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read the current file contents which includes the changes made by this review"""
        ...

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        ...

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        ...

    @property
    def root_dir(self) -> str:
        """Returns the root directory of the review"""
        ...

class FileReview(BaseReview):
    def __init__(self, path: str) -> None:
        self.path = path
        with open(path) as f:
            self.file_contents = f.read()

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read a file at the head commit"""
        assert path == self.path
        return self.file_contents

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        return None

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        yield self.path, self.file_contents, [(1, len(self.file_contents.splitlines()))]

    @property
    def root_dir(self) -> str:
        return ""

class SCMReview(BaseReview):
    """
    SCM based review
    """

    head_commit: str
    base_commit: Optional[str]
    repo: git.Repo
    patch_set: unidiff.PatchSet

    def __init__(
        self, head_commit: str, base_commit: Optional[str], repo_path: Optional[str]
    ) -> None:
        self.head_commit = head_commit
        self.base_commit = base_commit if base_commit else head_commit + "^"
        self.repo_root = repo_path or os.getcwd()
        repo = scm.get_repo(self.repo_root)
        if repo is None:
            raise ValueError(
                f"No git repository found with commit: {head_commit}, path: {self.repo_root}"
            )
        self.repo = repo
        self.patch_set = scm.patch_set(self.repo, self.head_commit, self.base_commit)

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read a file at the head commit"""
        try:
            return (
                self.repo.commit(self.head_commit)
                .tree[path]
                .data_stream.read()
                .decode("utf-8")
            )
        except:  # pylint: disable=bare-except
            logging.exception("Error reading %s at commit: %s", path, self.head_commit)

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        try:
            return (
                self.repo.commit(self.base_commit)
                .tree[path]
                .data_stream.read()
                .decode("utf-8")
            )
        except:  # pylint: disable=bare-except
            # This could be the first commit
            pass

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        for single_file_patch in self.patch_set:
            proceed, reason = can_review(single_file_patch)
            if not proceed:
                logging.info("Skipping %s: %s", single_file_patch.path, reason)
                yield single_file_patch.path, None, []
                continue
            unified_diff = "\n".join(str(patch) for patch in single_file_patch)
            hunk_lines = [
                (hunk.target_start, hunk.target_start + hunk.target_length)
                for hunk in single_file_patch
            ]
            yield single_file_patch.path, unified_diff, hunk_lines

    @property
    def root_dir(self) -> str:
        """Returns the root directory of the review"""
        return self.repo_root

@dataclass
class ReviewFile:
    """
    Wrapper for a single LLM call
    """

    file_path: str
    message: str
    status: Literal["needs_review", "skipped"]
    input_tokens: int
    skipped_reason: Optional[str] = None

@dataclass
class ReviewPlan:
    """
    Review broken down into individual review steps.

    Includes other metrics like estimated cost and input/output token counts computed during
    `plan` in case user confirmation is needed.
    """

    files: List[ReviewFile] = field(default_factory=list)
    estimated_cost: float = 0
    input_tokens: int = 0
    estimated_output_tokens: int = 0

    def add_file(self, file: ReviewFile, in_tokens: int, est_out_tokens: int) -> None:
        """
        Add a review step to the plan.
        """
        self.files.append(file)
        self.input_tokens += in_tokens
        self.estimated_output_tokens += est_out_tokens

def can_review(single_file: unidiff.PatchedFile) -> Tuple[bool, str]:
    """
    Returns True if the given file can be reviewed.
    """
    if single_file.is_binary_file:
        return False, "binary file"

    if single_file.is_removed_file:
        return False, "removed file"

    if config.is_excluded(single_file.path):
        return False, "excluded file extension"

    return True, ""


import unittest

from balance_sheet.balance_sheet import BalanceSheet
from menu.menu import MenuImp
from testing_constants_vm import *
from vending_machine.vending_machine import VendingMachine
from vending_machine_error.vending_machine_error import PyVendingMachineError

class TestPurchase(unittest.TestCase):
    def setUp(self):
        balance_sheet = BalanceSheet()
        self.vm = VendingMachine(MenuImp(items), balance_sheet=balance_sheet)
        self.vm.menu.items.__getitem__(drink).__setitem__('stock', 10)

    def test_stock_depletes_after_successful_purchase(self):
        self.vm.insert_five()
        self.vm.make_selection(drink)
        self.assertEqual(9, self.vm.menu.items.__getitem__(drink).info['stock'])
        self.assertEqual('$1.75', self.vm.balance_sheet.get_revenue())

    def test_balance_depletes_after_successful_purchase(self):
        self.vm.insert_five()
        self.vm.make_selection(drink)
        self.assertEqual('$3.25', self.vm.balance.get_amount())

    def test_stock_does_not_deplete_if_purchase_fails(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.insert_dollar()
            self.vm.make_selection(drink)
        self.assertEqual(10, self.vm.menu.items.__getitem__(drink).info['stock'])

    def test_balance_does_not_deplete_if_purchase_fails(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.insert_dollar()
            self.vm.make_selection(drink)
        self.assertEqual(100, self.vm.balance.amount)


from  rest_framework.authentication import   BaseAuthentication
from    rest_framework.views import   APIView
from   rest_framework.serializers import   ModelSerializer
from   app01  import  models
from   rest_framework.exceptions import    AuthenticationFailed
from   app01.all_config.response import   Self_Response

# class    accountserivializer(BaseAuthentication):
#     '''
#     认证
#
#     '''

from  rest_framework.response import   Response
 ###认证
# class   Auth(BaseAuthentication):
#     def   authenticate(self, request):
#     ##拿到加密的token
#         token=request.query_params.get('token')
#         obj=models.UserToken.objects.get(usertoken=token)
#         res={'code':1000,'msg':None}
#         if   not  obj:
#             raise   AuthenticationFailed({'code':1001,'error':'fail'})
#         # return    (obj.username.username,obj)
#         return   [1,2,3]

##注明一下，这个是get的请求，认证
'''
注明一下，这里的method不被允许是因为在view里面没有设置get的请求方式，所以返回不了
def   get():
pass
'''
from  rest_framework.views import   APIView
from   django.shortcuts import  HttpResponse
class Auth(BaseAuthentication):
    def authenticate(self, request):
        try:
            print('认证开始')
            print(request.data)
            # http://wwwww...c0ovmadfasd/?token=adfasdfasdf
            # print(request.POST.get('token'))
            res = Self_Response()
            # token=request.GET.get('token')
            # print(token)
            # token = request.query_params.get('token')##注明一，这个接受只能是get的请求方式的时候
            # print(token)
            token=request.data.get('token')
            print(token)
            if  not  token:
                token = request.query_params.get('token')##注明一，这个接受只能是get的请求方式的时候
            print('token是',token)
            print(type(token))
            obj = models.UserToken.objects.filter(usertoken=token).first()
            print(obj)
            if not obj:
                print('不存在obj')
                res.code=1001
                res.error='认证失败'
                print(res.get_response)
                raise AuthenticationFailed(res.get_response)
            res.msg='%s'%obj.username.username
            res.code=200
            request.session['username']=models.UserToken.objects.get(usertoken=token).username.username
            request.session['username_id']=models.UserToken.objects.get(usertoken=token).username.pk
            print(request.session.get('username'))
            # res.msg='认证成功%s'%request.GET
            res.msg='认证成功%s'%token
            # return   Response(res.get_response)'
            #注明一下

            #####下面返回的2个分别是request.user,request.auth,分别对应下面的值，当你requets,.auth的时候，就拿到了下面的对象obj，直接是requets.auth.useranem.id就可以拿到用户的id值了
            return (obj.username.username, obj)##3在底层源码里面是元祖的形式
            # return (obj.username.username,'sfgsfsdf')##3在底层源码里面是元祖的形式   not enough values to unpack (expected 2, got 1),必须是两个参数
            # return   Response(res.get_response)
        ##放返回值的时候，就可以直接调用，requets.user,requets.auth来进行使用

        except  Exception  as  e:
            print('异常')
            print(e)
            return  (None,None)

###注明一下，应该返回元祖的形式，是返回两个回去

from   app01 import  models
class  LoginSerilazer(ModelSerializer):
    class  Meta:
        model = models.Userinfo
        fields = '__all__'


from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    name = models.CharField(max_length=64)
    description = models.TextField(null=True)

    def __str__(self):
        return self.name

class Mechanic(models.Model):
    name = models.CharField(max_length=64)
    name_pl = models.CharField(max_length=64)
    description = models.TextField(null=True)

    def __str__(self):
        return self.name

class PublishingHouse(models.Model):
    name = models.CharField(max_length=64)

    def __str__(self):
        return self.name

class Game(models.Model):
    name = models.CharField(max_length=64, unique=True)
    categories = models.ManyToManyField(Category, through='GameCategory')
    mechanics = models.ManyToManyField(Mechanic, through="GameMechanic")
    minimum_players = models.SmallIntegerField()
    maximum_players = models.SmallIntegerField()
    publishing_house = models.ForeignKey(PublishingHouse, on_delete=models.CASCADE)
    minimum_age = models.SmallIntegerField()
    description = models.TextField(null=True)
    image = models.ImageField(upload_to="images/", null=True, blank=True)

    def __str__(self):
        return self.name

class GameMechanic(models.Model):
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    mechanic = models.ForeignKey(Mechanic, on_delete=models.CASCADE)

class GameCategory(models.Model):
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

class Shelf(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=64)
    games = models.ManyToManyField(Game, through='ShelfGame')

    def __str__(self):
        return self.name

class ShelfGame(models.Model):
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    shelf = models.ForeignKey(Shelf, on_delete=models.CASCADE)

class APGame(models.Model):
    name = models.CharField(max_length=124)
    price = models.FloatField()
    link = models.TextField()
    availability = models.BooleanField()
    scarp_date = models.DateField(auto_now_add=True, null=True)


"""
KNN - K Nearest Neighbor
Author: Setyo Nugroho

Requirement:
    - Python 3.6
    - Pandas
    - Numpy
"""

from collections import Counter
from math import sqrt

import pandas as pd
import numpy as np

def eculiden_distance(data1, data2):
    """
    Get euclidean distance of 2 array
    :param data1: 1D ndarray
    :param data2: 1D ndarray
    :return: eauclidean distance
    """
    if len(data1) != len(data2):
        raise Exception("Data length should same")

    power_sum = 0
    for i in range(len(data1)):
        power_sum += (data1[i] - data2[i]) ** 2

    return sqrt(power_sum)

def knn(k, train, test):
    """
    Do KNN
    :param k: K used
    :param train: 2D array of X1, X2, X3, X4, X5, Y
    :param test: 2D array of X1, X2, X3, X4, X5, Y
    :return: list of prediction result
    """
    result = []
    for ts in test:
        distance_list = []
        for tr in train:
            # Calculate euclidean without Y column
            distance = eculiden_distance(ts[:5], tr[:5])
            # Add distance list with value (Y, distance)
            distance_list.append((tr[5], distance))

        # Sort by distance
        distance_list.sort(key=lambda x: x[1])
        distance_list = distance_list[:k]

        # Counter for finding most common Y in distance list
        counter = Counter()
        for d in distance_list:
            counter[d[0]] += 1
        result.append(int(counter.most_common(1)[0][0]))

    return result

def get_accuracy(k, train, test):
    """
    Run KNN and calculate the accuracy. Test data Y column should has a value
    :param k: The magic K number
    :param train: 2D array of X1, X2, X3, X4, X5, Y
    :param test: 2D array of X1, X2, X3, X4, X5, Y
    :return: Accuracy
    """
    result = knn(k, train, test)
    correct = 0
    for i in range(len(test)):
        if test[i][5] == result[i]:
            correct += 1

    return correct / len(result) * 100

def cross_validation(k_fold, k, data):
    """
    Do cross validation and return the average accuracy
    :param k_fold: The fold
    :param k: The magic K
    :param data: 2D array of X1, X2, X3, X4, X5, Y
    :return: Average accuracy
    """
    fold = np.split(data, k_fold)
    accuracies = np.array([])
    for i in range(k_fold):
        test = fold[i]
        train = fold[:]  # Copy fold list
        del train[i]  # Remove current fold from train
        train = np.concatenate(train)  # Concat train array
        accuracy = get_accuracy(k, train, test)
        accuracies = np.append(accuracies, accuracy)

    return accuracies.mean()

df_train = pd.read_csv('DataTrain_Tugas3_AI.csv')
df_test = pd.read_csv('DataTest_Tugas3_AI.csv')

print("Finding The Best K")
current_k, current_accuracy = 0, 0

# Find best K in range 1 to 50
for k in range(1, 51):
    # Cross validation using 10 fold
    accuracy = cross_validation(10, k, df_train.iloc[:, 1:].values)
    if accuracy > current_accuracy:
        current_k = k
        current_accuracy = accuracy
    print("K = ", k, "Accuracy = ", accuracy)

print("=====================")
print("Best K")
print("K = ", current_k, "Accuracy = ", current_accuracy)

print("=====================")
print("Do Prediction....")

prediction_result = knn(current_k, df_train.iloc[:, 1:].values, df_test.iloc[:, 1:].values)
df_pred = pd.DataFrame(prediction_result)
df_pred.to_csv('TebakanTugas3.csv', index=False, header=False)
print("Prediction Saved...")


from aiogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove, KeyboardButton
from numpy import insert
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

b1 = KeyboardButton('/Abilities')
b2 = KeyboardButton('/Demands')
b3 = KeyboardButton('/Links')

urlkb = InlineKeyboardMarkup(run_wigth=2)
urlButton = InlineKeyboardButton(text='Tik-Tok', url='https://vt.tiktok.com/ZSdqaYc2K')
urlButton2 = InlineKeyboardButton(text='Instagram', url='https://www.instagram.com/yondu.pro')

urlkb.add(urlButton, urlButton2)

kb_client = ReplyKeyboardMarkup(resize_keyboard=True)

kb_client.add(b1).add(b2).add(b3)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    @Author : 李将
#
#              _____               ______
#     ____====  ]OO|_n_n__][.      |    |]
#    [________]_|__|________)<
#     oo    oo  'oo OOOO-| oo\_   ~o~~~o~'
# +--+--+--+--+--+--+--+--+--+--+--+--+--+
#    @Time : 2022/7/25 19:48
#    @FIle： day6.py
#    @Software: PyCharm
import collections

# 第一次只出现一次的字符
# https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/solution/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-by-3zqv5/
class Solution:
    def firstUniqChar(self, s: str) -> str:
        frequency = collections.Counter(s)
        # print(frequency)
        for i, ch in enumerate(s):
            if frequency[ch] == 1:
                return ch
        return ''

if __name__ == '__main__':
    tmp = Solution()
    print(tmp.firstUniqChar('abaccdeff'))


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 13:24:25 2020

@author: jubco
"""

def flatten(listBoi, memory=[]):
    for x in listBoi:           #for every element in the list
        if isinstance(x, list): #If it is another list, then:
            flatten(x,memory)   #We repeat the last few steps on that list
        else:
            memory.append(x)    #Otherwise, we add it to our memory

    return memory

print("Flatten on example: ", flatten([[1,2,3], [[4],[5]], 6,7,8]))
############################################################################

def powSet(setBoi):
    if len(setBoi) > 0:             #If not an empty set
        start = powSet(setBoi[:-1]) #split with recursion so you get everything minus the last element each recurse
        #print("First: ", first)
        return start + [elem + [setBoi[-1]] for elem in start] #Return every start + one of every element + last element
    else:
        return [[]]

print("\nPowerset of 1 2 3: ", powSet([1,2,3]))
############################################################################

from itertools import permutations #I knew about the import already so i just used that

def allPerms(listBoi):
    result = list(permutations(listBoi))
    return(result)

print("\nPermutations of 1 2 3: ", allPerms([1,2,3]), "\n")
############################################################################

import numpy as np

def spiral(n, end_corner):
    mat = np.full((n, n), -1)
    curnum = (n**2)-1
    if end_corner == 1:         #Upper Left
        curpos = (0,0)
        mat[curpos] = (n**2)-1
        cardinality = 'east'
    elif end_corner == 2:       #Upper Right
        curpos = (0,n-1)
        mat[curpos] = (n**2)-1
        cardinality = 'south'
    elif end_corner == 3:       #Lower Right
        curpos = (n-1,n-1)
        mat[curpos] = (n**2)-1
        cardinality = 'west'
    elif end_corner == 4:       #Lower left
        curpos = (n-1,0)
        mat[curpos] = (n**2)-1
        cardinality = 'north'
    else:
        print("Incorrect input, end_corner must be between 1 and 4")
        return 0

    for i in range(n):
        #If we are going right, keep decrementing to the right until you hit the last col or a non -1
        while(cardinality == 'east' and curpos[1]+1 != n and mat[(curpos[0]),(curpos[1])+1] == -1):
            curpos = ((curpos[0]),(curpos[1])+1)
            curnum -= 1
            mat[curpos] = curnum
        #Change direction
        if cardinality == 'east':
            if curpos[1]+1 == n or (mat[(curpos[0]),(curpos[1]+1)] != -1):
                cardinality = 'south'

        #If we are going south
        while(cardinality == 'south' and curpos[0]+1 != n and mat[(curpos[0]+1),(curpos[1])] == -1):
            curpos = ((curpos[0])+1,(curpos[1]))
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'south':
            if curpos[0]+1 == n or (mat[(curpos[0]+1),(curpos[1])] != -1):
                cardinality = 'west'

        #If we are going west
        while(cardinality == 'west' and curpos[1]-1 != -1 and mat[(curpos[0]),(curpos[1]-1)] == -1):
            curpos = ((curpos[0]),(curpos[1])-1)
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'west':
            if curpos[0]+1 == n or (mat[(curpos[0]),(curpos[1]-1)] != -1):
                cardinality = 'north'

        #If we are going north
        while(cardinality == 'north' and curpos[0]-1 != -1 and mat[(curpos[0]-1),(curpos[1])] == -1):
            curpos = ((curpos[0]-1),(curpos[1]))
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'north':
            if curpos[0]-1 == n or (mat[(curpos[0]-1),(curpos[1])] != -1):
                cardinality = 'east'

    return mat

print("Spiral:\n",spiral(10,4))


# Importing modules

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression, Ridge, LassoCV, ElasticNetCV
import logging
import statsmodels.api as sm
import warnings

warnings.filterwarnings('ignore')

# configuring logging operations
logging.basicConfig(filename='Regression_development_logs.log', level=logging.INFO,
                    format='%(levelname)s:%(asctime)s:%(message)s')

class LR_With_FeatureSelection:
    """This class is used to build Linear regression models with only the relevant features.
        reference_1: https://towardsdatascience.com/feature-selection-with-pandas-e3690ad8504b
        reference_2: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoCV.html

        Parameters:
        X_train: Training data frame containing the independent features.
        y_train: Training dataframe containing the dependent or target feature.
        X_test: Testing dataframe containing the independent features.
        y_test: Testing dataframe containing the dependent or target feature.
    """

    def __init__(self, X_train, y_train, X_test, y_test):
        self.X_train = X_train
        self.y_train = y_train
        self.X_test = X_test
        self.y_test = y_test

    @property
    def forward_selection(self, significance_level=0.05):

        """ function accepts X_train, y_train and significance level as arguments and returns
            the linear regression model, its predictions of both the training and testing dataframes and the relevant features.
        """

        # Logging operation
        logging.info('Entered the Forward selection method of the LR_With_FeatureSelection class')

        try:
            initial_features = self.X_train.columns.tolist()
            best_features_FS = []  # All the relevant columns in a list

            while len(initial_features) > 0:
                remaining_features = list(set(initial_features) - set(best_features_FS))

                new_p_value = pd.Series(index=remaining_features)

                for new_column in remaining_features:
                    model = sm.OLS(self.y_train, sm.add_constant(self.X_train[best_features_FS + [new_column]])).fit()
                    new_p_value[new_column] = model.pvalues[new_column]

                min_p_value = new_p_value.min()

                if min_p_value < significance_level:
                    best_features_FS.append(new_p_value.idxmin())
                else:
                    break
            print()
            print('Features selected by Forward selection method in Linear regression are ', best_features_FS)
            print()

            X_train_FS = self.X_train[best_features_FS]
            X_test_FS = self.X_test[best_features_FS]

            lr = LinearRegression()

            lr.fit(X_train_FS, self.y_train)

            y_pred_train_FS = lr.predict(X_train_FS)
            y_pred_test_FS = lr.predict(X_test_FS)

            # logging operation
            logging.info('Linear regression model built successfully using Forward Selection method.')

            logging.info(
                'Exited the Forward Selection method method of the LR_With_FeatureSelection class')

            return lr, X_train_FS, y_pred_train_FS, X_test_FS, y_pred_test_FS, best_features_FS

        except Exception as e:

            # logging operation

            logging.error(
                'Exception occurred in Forward selection approach method of the LR_With_FeatureSelection class. Exception message:' + str(
                    e))
            logging.info(
                'Forward selection method unsuccessful. Exited the Forward selection method of the LR_With_FeatureSelection class')

    @property
    def backward_elimination(self, Significance_level=0.05):
        """Description: This method builds a linear regression model on all the features and eliminates
        each one w.r.t. its p-value if it is above 0.05. Else it will be retained Raises an exception if it fails.

        returns the linear regression model, its predictions of both the training and testing dataframes and the relevant features.
        """

        # Logging operation
        logging.info('Entered the Backward Elimination method of the LR_With_FeatureSelection class')
        try:
            best_features_BE = self.X_train.columns.tolist()
            while len(best_features_BE) > 0:
                features_with_constant = sm.add_constant((self.X_train[best_features_BE]))
                p_values = sm.OLS(self.y_train, features_with_constant).fit().pvalues[1:]
                max_p_value = p_values.max()

                if max_p_value >= Significance_level:
                    excluded_features = p_values.idxmax()
                    best_features_BE.remove(excluded_features)
                else:
                    break

            print()
            print("Features selected by the Backward elimination method in Linear regression are ", best_features_BE)
            print()

            x_train_BE = self.x_train[best_features_BE]  # considering only the relevant features
            x_test_BE = self.x_test[best_features_BE]  # considering only the relevant features

            lr = LinearRegression()  # instantiating linear regression model from LinearRegression of sci-kit learn

            lr.fit(x_train_BE, self.y_train)  # fitting

            y_pred_train_BE = lr.predict(x_train_BE)  # predictions on train data
            y_pred_test_BE = lr.predict(x_test_BE)  # predictions on test data

            # logging operation
            logging.info('Linear regression model built successfully using Backward Elimination approach.')

            logging.info(
                'Exited the backward_elimination method of the LR_With_FeatureSelection class')

            return lr, x_train_BE, y_pred_train_BE, x_test_BE, y_pred_test_BE, best_features_BE

        except Exception as e:
            # logging operation
            logging.error(
                'Exception occurred in backward_elimination method of the LR_With_FeatureSelection class. Exception '
                'message:' + str(e))
            logging.info(
                'Backward elimination method unsuccessful. Exited the backward_elimination method of the '
                'LR_With_FeatureSelection class')

class Embedded_method_for_feature_selection:
    """This class is used to train the models using Linear regression with Elastic Net model with iterative fitting along a regularization path.
        parameters
        x_train: Training data frame containing the independent features.
        y_train: Training dataframe containing the dependent or target feature.
        x_test: Testing dataframe containing the independent features.
        y_test: Testing dataframe containing the dependent or target feature.
    """

    def __init__(self, X_train, y_train, X_test, y_test):
        self.X_train = X_train
        self.y_train = y_train
        self.X_test = X_test
        self.y_test = y_test

    @property
    def Elastic_Net_CV(self):
        # logging operation
        logging.info('Entered the Elastic_Net_CV method of the Embedded_method_for_feature_selection class.')

        try:
            ECV = ElasticNetCV()  # Instantiating ElasticNetCV
            ECV.fit(self.X_train, self.y_train)  # fitting on the training data

            Coef = pd.Series(ECV.coef_, index=self.X_train.columns)  # feature importance by ElasticNetCV

            imp_Coef = Coef.sort_values(ascending=False)

            print()
            print(f'Feature importance by the ElasticNetCV are : {imp_Coef}')
            print()

            y_pred_train_ECV = ECV.predict(self.X_train)  # predictions on the train data
            y_pred_test_ECV = ECV.predict(self.X_test)  # predictions on the test data

            # logging operation

            logging.info('Linear regression model built successfully using Elastic_Net_CV approach. ')

            logging.info(
                'Exited the Elastic_Net_CV method of the Embedded_method_for_feature_selection class.')  # logging operation

            return ECV, self.X_train, y_pred_train_ECV, self.X_test, y_pred_test_ECV

        except Exception as e:
            # logging operation
            logging.error('Exception occurred in Elastic_Net_CV method of the Embedded_method_for_feature_selection class. Exception '
                          'message:' + str(e))
            logging.info('Elastic_Net_CV method unsuccessful. Exited the Elastic_Net_CV method of the '
                         'Embedded_method_for_feature_selection class ')


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

__author__ = 'Edson Luiz'

import sqlite3

def excluirregistro():
    # Abre conexao com o Banco de Dados
    conn = sqlite3.connect ('dbUersGBD.db')
    cursor = conn.cursor()

    # Lista registros para escolher o excluído
    cursor.execute("""
    SELECT * from usuarios;
    """)
    for linha in cursor.fetchall():
        print(linha)

    print("")
    print("Digite o ID que deverá ser excluído do BD:")
    dbregistro = input("ID Registro: ")

    if (dbregistro == ""):
        print("Preencha o campo!")
    else:
        cursor.execute("DELETE FROM usuarios where idusuario=(?)", (dbregistro,))
        conn.commit()
        print("Sucessful Account Deleted")
        cursor.fetchall()
        conn.close()

Banco de Dados SQLite3 CLI/excluirregdb.py


default_coordinates = (38.87182454344979, -99.33700315795494)

def parseCoordinatesFromString(string)->(float, float):
	"""
	Takes a string and parses out a tuple of two floats.
	Both floats must be valid GPS coordinates.
	"""
	if string is None: return default_coordinates

	try:
		floats = [float(x) for x in string.split()]
	except ValueError as e:
		e.args = (string + " couldn't be parsed to latitude and longitude coordinates.", )
		raise e
	else:
		return coordinateTupleFromFloatArray(floats)

def coordinateTupleFromFloatArray(arr)->(float, float):
	"""
	Takes an array of 2 floats and creats a tuple of latitude and longitude coordinates.
	Extraneous elements in the input array will be ignored.
	Invalid coordinates will raise a CoordinateOutOfBoundsException
	"""
	if arr[0] < -90 or arr[0] > 90:
		raise CoordinateOutOfBoundsException(str(arr[0]) + " isn't a valid latitude.")
	elif arr[1] < -180 or arr[1] > 180:
		raise CoordinateOutOfBoundsException(str(arr[1]) + " isn't a valid longitude.")
	return (arr[0], arr[1])

class CoordinateOutOfBoundsException(Exception):
	"""
	Raised if a coordinate tuple contains a latitude outside the bounds -90 to 90
	or a longtitude outside the bounds of -180 to 180.
	"""
	def __init__(self, *args):
		super().__init__(self, "Latitude can be -90˚ to 90˚, " +
								"Longitude may be -180˚ to 180˚.")


#%%
import pandas as pd
import numpy as np
import pycountry_convert as pc

def country_to_continent(country_name):
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)
    return country_continent_name

# %%
f = open('loans_ridge.csv', 'r', encoding="utf8")
data = pd.read_csv(f)
# %% ADD 10 VARIABLES RELATEDTO TEXT ANALYSIS
# pos and neg words
pos_words = ['years', 'children', 'married', 'help', 'lives', 'income', 'old', 'husband', 'living', 'selling', 'kiva']
neg_words = ['loan', 'business', 'buy', 'family', 'work', 'house', 'store', 'improve']

data['description_texts_en'] = data['description_texts_en'].astype(str)

# add 5 variables for aspects of description
data['contains_old'] = data['description_texts_en'].apply(lambda x: 1 if  'old' in x.lower() else 0)
data['contains_improve'] = data['description_texts_en'].apply(lambda x: 1 if  'improve' in x.lower() else 0)
data['contains_help'] = data['description_texts_en'].apply(lambda x: 1 if  'help' in x.lower() else 0)
data['contains_buy'] = data['description_texts_en'].apply(lambda x: 1 if  'buy' in x.lower() else 0)
data['contains_loan'] = data['description_texts_en'].apply(lambda x: 1 if  'loan' in x.lower() else 0)

# add variable for num neg_words in description
for n in neg_words:
    data[n] = data['description_texts_en'].str.contains(n, case=False).astype(int)
data['negative_sentiment'] = data[neg_words].sum(axis=1)

# add variable for num pos_words in description
for p in pos_words:
    data[p] = data['description_texts_en'].str.contains(p, case=False).astype(int)
data['positive_sentiment'] = data[pos_words].sum(axis=1)

# add indicator for if description is overall pos or neg
data['overall_positive'] = (data['positive_sentiment'] > data['negative_sentiment']) * 1 # cast to int

data['description_length'] = data['description_texts_en'].apply(lambda x: len(str(x).split()))

# add indicators for if description is especially pos or especially neg
# data['high_negative_sentiment'] = data['negative_sentiment'].apply(lambda x : 1 if x > 4 else 0)
# data['high_positive_sentiment'] = data['positive_sentiment'].apply(lambda x : 1 if x > 4  else 0)
# %% ADD 9 FACTOR VARIABLES (for gender, country, sector)

data['borrowers_borrower_gender'] = data['borrowers_borrower_gender'].astype(str)
data['location_country'] = data['location_country'].astype(str)
data['sector'] = data['sector'].astype(str)
data['status'] = data['status'].astype(str)

# dummy for gender
data['is_female'] = data['borrowers_borrower_gender'].apply(lambda x: 1 if x == 'F' else 0)

# dummies for country = iraq and country = bulgaria,because those countries have a much higher avg_days until funded
# (especially bulgaria)
data['is_bulgaria'] = data['location_country'].apply(lambda x: 1 if 'bulgaria' in x.lower() else 0)
data['is_iraq'] = data['location_country'].apply(lambda x: 1 if 'iraq' in x.lower() else 0)
# data =  data.assign(is_bulgaria= lambda row: 1 if row.location_country is 'Bulgaria' else 0,
#            is_iraq= lambda row: 1 if row.location_country is 'Iraq' else 0)

# dummies for country = zambia and country = thailand because those 2 have the lowest avg days_until_funded
data['is_nepal'] = data['location_country'].apply(lambda x: 1 if 'nepal' in x.lower() else 0)
data['is_timor'] = data['location_country'].apply(lambda x: 1 if 'timor' in x.lower() else 0)
# data =  data.assign(is_zambia= lambda row: 1 if row.location_country is 'Zambia' else 0,
#            is_thailand= lambda row: 1 if row.location_country is 'Thailand' else 0)

# dummies for sectors with avg days_until_funded < 1 (health, education, arts, agriculture, and food) as well
# as sector with highest avg days_until_funded (housing)
data['is_health'] = data['sector'].apply(lambda x: 1 if x == 'Health' else 0)
data['is_education'] = data['sector'].apply(lambda x: 1 if x == 'Education' else 0)
data['is_arts'] = data['sector'].apply(lambda x: 1 if x == 'Arts' else 0)
data['is_agriculture'] = data['sector'].apply(lambda x: 1 if x == 'Agriculture' else 0)
data['is_food'] = data['sector'].apply(lambda x: 1 if x == 'Food' else 0)
data['is_housing'] = data['sector'].apply(lambda x: 1 if x == 'Housing' else 0)
# data =  data.assign(is_health=lambda row: 1 if row.sector == 'Health' else 0,
#            is_ed=lambda row: 1 if row.sector == 'Education' else 0,
#            is_arts=lambda row: 1 if row.sector == 'Arts' else 0,
#            is_agriculture=lambda row: 1 if row.sector == 'Agriculture' else 0,
#            is_food=lambda row: 1 if row.sector == 'Food' else 0,
#            is_housing=lambda row: 1 if row.sector == 'Housing' else 0)

for index, row in data.iterrows():
    country = row['location_country']
    country_africa = ['Congo', 'Cote']
    country_asia = ['Timor', 'Myanmar', 'Lao']
    country_europe = ['Kosovo']
    if any(substring in country for substring in country_africa):
        data.at[index, 'african'] = 1
        data.at[index, 'asian'] = 0
        data.at[index, 'north_american'] = 0
        data.at[index, 'south_american'] = 0
        data.at[index, 'european'] = 0
    elif any(substring in country for substring in country_asia):
        data.at[index, 'african'] = 0
        data.at[index, 'asian'] = 1
        data.at[index, 'north_american'] = 0
        data.at[index, 'south_american'] = 0
        data.at[index, 'european'] = 0
    elif any(substring in country for substring in country_europe):
        data.at[index, 'african'] = 0
        data.at[index, 'asian'] = 0
        data.at[index, 'north_american'] = 0
        data.at[index, 'south_american'] = 0
        data.at[index, 'european'] = 1
    else:
        continent = country_to_continent(country)
        if continent == 'Africa':
            data.at[index, 'african'] = 1
            data.at[index, 'asian'] = 0
            data.at[index, 'north_american'] = 0
            data.at[index, 'south_american'] = 0
            data.at[index, 'european'] = 0
        elif continent == 'Asia':
            data.at[index, 'african'] = 0
            data.at[index, 'asian'] = 1
            data.at[index, 'north_american'] = 0
            data.at[index, 'south_american'] = 0
            data.at[index, 'european'] = 0
        elif continent == 'North America':
            data.at[index, 'african'] = 0
            data.at[index, 'asian'] = 0
            data.at[index, 'north_american'] = 1
            data.at[index, 'south_american'] = 0
            data.at[index, 'european'] = 0
        elif continent == 'South America':
            data.at[index, 'african'] = 0
            data.at[index, 'asian'] = 0
            data.at[index, 'north_american'] = 0
            data.at[index, 'south_american'] = 1
            data.at[index, 'european'] = 0
        elif continent == 'Europe':
            data.at[index, 'african'] = 0
            data.at[index, 'asian'] = 0
            data.at[index, 'north_american'] = 0
            data.at[index, 'south_american'] = 0
            data.at[index, 'european'] = 1
        else:
            data.at[index, 'african'] = 0
            data.at[index, 'asian'] = 0
            data.at[index, 'north_american'] = 0
            data.at[index, 'south_american'] = 0
            data.at[index, 'european'] = 0

# create dummies for different statuses (paid, refunded, defaulted, in_repayment)
data['paid'] = data['status'].apply(lambda x: 1 if x == 'paid' else 0)
data['refunded'] = data['status'].apply(lambda x: 1 if x == 'refunded' else 0)
data['defaulted'] = data['status'].apply(lambda x: 1 if x == 'defaulted' else 0)
data['in_repayment'] = data['status'].apply(lambda x: 1 if x == 'in_repayment' else 0)
# data =  data.assign(paid=lambda row: 1 if row.status == 'paid' else 0,
#            refunded=lambda row: 1 if row.status == 'refunded' else 0,
#            defaulted=lambda row: 1 if row.status == 'defaulted' else 0,
#            in_repayment=lambda row: 1 if row.status == 'in_repayment' else 0)

# %%
# CONT VARS = funded_amt, terms_disbursal_amount, loan_amount

# %%
# original features to drop not that have added new ones
# NOTE: not doing anything with location_town or activity
# NOTE: droping paid_amount bc it is null for 10714 rows which is leading to absolute nightmares with type issues in OLS
drop_list = ['description_texts_en', 'id', 'location_town', 'borrowers_borrower_gender', 'sector', 'location_country',
            'activity', 'status', 'paid_amount']

# also drop columns for individual words (but not indicators)
drop_list += pos_words
drop_list += neg_words

data.drop(drop_list, axis=1, inplace=True)

# %%

# move days_until_funded to first column so is easier to access later
days = data.pop('days_until_funded')
data.insert(0, 'days_until_funded', days)

data.to_csv('processed_data.csv')

# %%
# code to determine which countries/sectors have higher and lowest avg days_until_funded

# country
# countries = data['location_country'].unique()
# avg_days_per_country = {}
# avg_days_per_country_lst = []
# for country in countries:
#     avg_days_per_country_lst.append(data[data["location_country"] == country]["days_until_funded"].mean())
#     avg_days_per_country[country] = data[data["location_country"] == country]["days_until_funded"].mean()

# avg_days_per_country = sorted(avg_days_per_country.items(), key=lambda kv: kv[1])

#sector
# sectors = data['sector'].unique()
# avg_days_per_sector = {}
# avg_days_per_sector_lst = []
# for sector in sectors:
#     avg_days_per_sector_lst.append(data[data["sector"] == sector]["days_until_funded"].mean())
#     avg_days_per_sector[sector] = data[data["sector"] == sector]["days_until_funded"].mean()

# avg_days_per_sector = sorted(avg_days_per_sector.items(), key=lambda kv: kv[1])
# %%


#! /usr/bin/python3
# __*__ coding=utf-8 __*__

import requests,json,datetime,time
from wxpy import *
from bs4 import BeautifulSoup
import xlwt

def Wechat_login():
  bot = Bot(cache_path=True)
  chats= bot.chats()
  friends = bot.friends()
  groups = bot.groups()
  return bot

def GetAQI():
  mainURL = 'http://www.pm25.in/nanchang'
  response = requests.get(mainURL)
  return str(response.content,encoding='utf-8')

def parser(content):
  soup = BeautifulSoup(content,features='html.parser')
  ## 明细
  # rows = soup.findAll('tr')
  # datas=[]
  # for row in rows:
  #   row=row.findChildren()
  #   data = []
  #   for column in row:
  #     data.append(column.next)
  #   datas.append(data)
  # return datas
  level = soup.find('div',attrs={'class':'level'}).findChildren()[0].next
  captions = soup.findAll('div',attrs={'class':'caption'})
  values = soup.findAll('div',attrs={'class':'value'})
  strmsg = "南昌空气质量情况:{},".format(level)
  for i in range(0,len(captions)):
    strmsg+="{}值为{}".format(captions[i].next,values[i].next)
  return strmsg.replace('\n','').replace(' ','')

def FillExcel(datas):
  wbk = xlwt.Workbook(encoding='utf-8')
  sheet = wbk.add_sheet('realtime_AQI',cell_overwrite_ok=True)
  for i in range(0,len(datas)):
    for j in range(0,len(datas[i])):
      sheet.write(i,j,datas[i][j])
  wbk.save('realtimeAQI_{}.xls'.format(datetime.datetime.now().strftime('%Y%m%d%H%M')))

bot = Wechat_login()

kd=ensure_one(bot.friends().search('kd'))
while(True):
  content = GetAQI()
  msg = parser(content)
  kd.send('当前时间：{}，{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M'),msg))
  time.sleep(60*60)