import json
import os
import shutil
import sys
import tempfile
import unittest

from compare_perf_tests import LogParser
from compare_perf_tests import PerformanceTestResult
from compare_perf_tests import ReportFormatter
from compare_perf_tests import ResultComparison
from compare_perf_tests import TestComparator
from compare_perf_tests import main
from compare_perf_tests import parse_args

from test_utils import captured_output

class TestPerformanceTestResult(unittest.TestCase):
    def test_init(self):
        header = "#,TEST,SAMPLES,MIN,MAX,MEAN,SD,MEDIAN"
        log_line = "1,AngryPhonebook,20,10664,12933,11035,576,10884"
        r = PerformanceTestResult.fromOldFormat(header, log_line)
        self.assertEqual(r.test_num, 1)
        self.assertEqual(r.name, "AngryPhonebook")
        self.assertEqual(
            (r.num_samples, r.min_value, r.max_value, r.mean, r.sd, r.median),
            (20, 10664, 12933, 11035, 576, 10884),
        )
        self.assertEqual(r.samples, [])

        header = "#,TEST,SAMPLES,MIN,MAX,MEAN,SD,MEDIAN,MAX_RSS"
        log_line = "1,AngryPhonebook,1,12045,12045,12045,0,12045,10510336"
        r = PerformanceTestResult.fromOldFormat(header, log_line)
        self.assertEqual(r.max_rss, 10510336)

    def test_init_quantiles(self):
        header = "#,TEST,SAMPLES,MIN(μs),MEDIAN(μs),MAX(μs)"
        log = "1,Ackermann,3,54383,54512,54601"
        r = PerformanceTestResult.fromQuantileFormat(header, log)
        self.assertEqual(r.test_num, 1)
        self.assertEqual(r.name, "Ackermann")
        self.assertEqual(
            (r.num_samples, r.min_value, r.median, r.max_value),
            (3, 54383, 54512, 54601)
        )
        self.assertAlmostEqual(r.mean, 54498.67, places=2)
        self.assertAlmostEqual(r.sd, 109.61, places=2)
        self.assertEqual(r.samples, [54383, 54512, 54601])

        header = "#,TEST,SAMPLES,MIN(μs),MEDIAN(μs),MAX(μs),MAX_RSS(B)"
        log = "1,Ackermann,3,54529,54760,55807,266240"
        r = PerformanceTestResult.fromQuantileFormat(header, log)
        self.assertEqual((len(r.samples), r.max_rss), (3, 266240))

        header = "#,TEST,SAMPLES,MIN(μs),Q1(μs),Q2(μs),Q3(μs),MAX(μs)"
        log = "1,Ackermann,5,54570,54593,54644,57212,58304"
        r = PerformanceTestResult.fromQuantileFormat(header, log)
        self.assertEqual(
            (r.num_samples, r.min_value, r.median, r.max_value),
            (5, 54570, 54644, 58304)
        )
        self.assertEqual((r.q1, r.q3), (54581.5, 57758))
        self.assertEqual(len(r.samples), 5)

        header = "#,TEST,SAMPLES,MIN(μs),Q1(μs),Q2(μs),Q3(μs),MAX(μs),MAX_RSS(B)"
        log = "1,Ackermann,5,54686,54731,54774,55030,63466,270336"
        r = PerformanceTestResult.fromQuantileFormat(header, log)
        self.assertEqual(r.num_samples, 5)
        self.assertEqual(len(r.samples), 5)
        self.assertEqual(r.max_rss, 270336)

    def test_init_delta_quantiles(self):
        # 2-quantile from 2 samples in repeated min, when delta encoded,
        # the difference is 0, which is omitted -- only separator remains
        header = "#,TEST,SAMPLES,MIN(μs),𝚫MEDIAN,𝚫MAX"
        log = "202,DropWhileArray,2,265,,22"
        r = PerformanceTestResult.fromQuantileFormat(header, log)
        self.assertEqual((r.num_samples, r.min_value, r.median, r.max_value),
                         (2, 265, 276, 287))
        self.assertEqual(len(r.samples), 2)
        self.assertEqual(r.num_samples, 2)

    def test_init_oversampled_quantiles(self):
        """When num_samples is < quantile + 1, some of the measurements are
        repeated in the report summary. Samples should contain only true
        values, discarding the repeated artifacts from quantile estimation.

        The test string is slightly massaged output of the following R script:
        subsample <- function(x, q) {
          quantile(1:x, probs=((0:(q-1))/(q-1)), type=1)}
        tbl <- function(s) t(sapply(1:s, function(x) {
          qs <- subsample(x, s); c(qs[1], diff(qs)) }))
        sapply(c(3, 5, 11, 21), tbl)

        TODO: Delete this test when we delete quantile support from the
        benchmark harness. Reconstructing samples from quantiles as this code is
        trying to do is not really statistically sound, which is why we're going
        to delete most of this in favor of an architecture where the
        lowest-level benchmarking logic reports samples, we store and pass
        raw sample data around as much as possible, and summary statistics are
        only computed as necessary for actual reporting (and then discarded,
        since we can recompute anything we need if we always have the raw
        samples available).
        """

        def validatePTR(deq):  # construct from delta encoded quantiles string
            deq = deq.split(",")
            num_samples = deq.count("1")
            r = PerformanceTestResult(
                ["0", "B", str(num_samples)] + deq, quantiles=True, delta=True
            )
            self.assertEqual(len(r.samples), num_samples)
            self.assertEqual(r.samples, range(1, num_samples + 1))

        delta_encoded_quantiles = """
1,,
1,,1
1,,,,
1,,,1,
1,,1,1,
1,,1,1,1
1,,,,,,,,,,
1,,,,,,1,,,,
1,,,,1,,,1,,,
1,,,1,,,1,,1,,
1,,,1,,1,,1,,1,
1,,1,,1,,1,1,,1,
1,,1,1,,1,1,,1,1,
1,,1,1,1,,1,1,1,1,
1,,1,1,1,1,1,1,1,1,
1,,1,1,1,1,1,1,1,1,1
1,,,,,,,,,,,,,,,,,,,,
1,,,,,,,,,,,1,,,,,,,,,
1,,,,,,,1,,,,,,,1,,,,,,
1,,,,,,1,,,,,1,,,,,1,,,,
1,,,,,1,,,,1,,,,1,,,,1,,,
1,,,,1,,,1,,,,1,,,1,,,1,,,
1,,,1,,,1,,,1,,,1,,,1,,,1,,
1,,,1,,,1,,1,,,1,,1,,,1,,1,,
1,,,1,,1,,1,,1,,,1,,1,,1,,1,,
1,,,1,,1,,1,,1,,1,,1,,1,,1,,1,
1,,1,,1,,1,,1,,1,1,,1,,1,,1,,1,
1,,1,,1,,1,1,,1,,1,1,,1,,1,1,,1,
1,,1,,1,1,,1,1,,1,1,,1,1,,1,1,,1,
1,,1,1,,1,1,,1,1,,1,1,1,,1,1,,1,1,
1,,1,1,,1,1,1,,1,1,1,,1,1,1,,1,1,1,
1,,1,1,1,,1,1,1,1,,1,1,1,1,,1,1,1,1,
1,,1,1,1,1,1,,1,1,1,1,1,1,,1,1,1,1,1,
1,,1,1,1,1,1,1,1,1,,1,1,1,1,1,1,1,1,1,
1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1"""
        map(validatePTR, delta_encoded_quantiles.split("\n")[1:])

    def test_init_meta(self):
        header = (
            "#,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),"
            + "MEDIAN(μs),PAGES,ICS,YIELD"
        )
        log = "1,Ackermann,200,715,1281,726,47,715,7,29,15"
        r = PerformanceTestResult.fromOldFormat(header, log)
        self.assertEqual((r.test_num, r.name), (1, "Ackermann"))
        self.assertEqual(
            (r.num_samples, r.min_value, r.max_value, r.mean, r.sd, r.median),
            (200, 715, 1281, 726, 47, 715),
        )
        self.assertEqual((r.mem_pages, r.involuntary_cs, r.yield_count), (7, 29, 15))
        header = (
            "#,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),"
            + "MAX_RSS(B),PAGES,ICS,YIELD"
        )
        log = "1,Ackermann,200,715,1951,734,97,715,36864,9,50,15"
        r = PerformanceTestResult.fromOldFormat(header, log)
        self.assertEqual(
            (r.num_samples, r.min_value, r.max_value, r.mean, r.sd, r.median),
            (200, 715, 1951, 734, 97, 715),
        )
        self.assertEqual(
            (r.mem_pages, r.involuntary_cs, r.yield_count, r.max_rss),
            (9, 50, 15, 36864),
        )
        header = "#,TEST,SAMPLES,MIN(μs),MAX(μs),PAGES,ICS,YIELD"
        log = "1,Ackermann,200,715,3548,8,31,15"
        r = PerformanceTestResult.fromOldFormat(header, log)
        self.assertEqual((r.num_samples, r.min_value, r.max_value), (200, 715, 3548))
        self.assertEqual(r.samples, [])
        self.assertEqual((r.mem_pages, r.involuntary_cs, r.yield_count), (8, 31, 15))

        header = "#,TEST,SAMPLES,MIN(μs),MAX(μs),MAX_RSS(B),PAGES,ICS,YIELD"
        log = "1,Ackermann,200,715,1259,32768,8,28,15"
        r = PerformanceTestResult.fromOldFormat(header, log)
        self.assertEqual((r.num_samples, r.min_value, r.max_value), (200, 715, 1259))
        self.assertEqual(r.samples, [])
        self.assertEqual(r.max_rss, 32768)
        self.assertEqual((r.mem_pages, r.involuntary_cs, r.yield_count), (8, 28, 15))

    def test_merge(self):
        tests = [
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12045]}""",
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12325],"max_rss":10510336}""",
            """{"number":1,"name":"AngryPhonebook",
            "samples":[11616],"max_rss":10502144}""",
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12270],"max_rss":10498048}"""
        ]

        results = [PerformanceTestResult(json) for json in tests]

        def as_tuple(r):
            return (
                r.num_samples,
                r.min_value,
                r.max_value,
                round(r.mean, 2),
                round(r.sd, 2),
                r.median,
                r.max_rss,
            )

        r = results[0]
        self.assertEqual(as_tuple(r), (1, 12045, 12045, 12045, 0, 12045, None))
        r.merge(results[1])
        self.assertEqual(
            as_tuple(r),
            (2, 12045, 12325, 12185, 197.99, 12185, 10510336),
        )
        r.merge(results[2])
        self.assertEqual(
            as_tuple(r),
            (3, 11616, 12325, 11995.33, 357.1, 12045, 10502144),
        )
        r.merge(results[3])
        self.assertEqual(
            as_tuple(r),
            (4, 11616, 12325, 12064, 322.29, 12157.5, 10498048),
        )

    def test_legacy_merge(self):
        header = """#,TEST,NUM_SAMPLES,MIN,MAX,MEAN,SD,MEDIAN, MAX_RSS"""
        tests = [
            """1,AngryPhonebook,8,12045,12045,12045,0,12045""",
            """1,AngryPhonebook,8,12325,12325,12325,0,12325,10510336""",
            """1,AngryPhonebook,8,11616,11616,11616,0,11616,10502144""",
            """1,AngryPhonebook,8,12270,12270,12270,0,12270,10498048"""
        ]

        results = [PerformanceTestResult.fromOldFormat(header, row) for row in tests]

        def as_tuple(r):
            return (
                r.num_samples,
                r.min_value,
                r.max_value,
                round(r.mean, 2),
                round(r.sd, 2) if r.sd is not None else None,
                r.median,
                r.max_rss,
            )

        r = results[0]
        self.assertEqual(as_tuple(r), (8, 12045, 12045, 12045, 0, 12045, None))
        r.merge(results[1])
        self.assertEqual(
            as_tuple(r),  # Note: SD, Median are lost
            (16, 12045, 12325, 12185, None, None, 10510336),
        )
        r.merge(results[2])
        self.assertEqual(
            as_tuple(r),
            (24, 11616, 12325, 11995.33, None, None, 10502144),
        )
        r.merge(results[3])
        self.assertEqual(
            as_tuple(r),
            (32, 11616, 12325, 12064, None, None, 10498048),
        )

class TestResultComparison(unittest.TestCase):
    def setUp(self):
        self.r0 = PerformanceTestResult(
            """{"number":101,"name":"GlobalClass",
            "samples":[0,0,0,0,0],"max_rss":10185728}"""
        )
        self.r01 = PerformanceTestResult(
            """{"number":101,"name":"GlobalClass",
            "samples":[20,20,20],"max_rss":10185728}"""
        )
        self.r1 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12325],"max_rss":10510336}"""
        )
        self.r2 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[11616],"max_rss":10502144}"""
        )
        self.r3 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[11616,12326],"max_rss":10502144}"""
        )

    def test_init(self):
        rc = ResultComparison(self.r1, self.r2)
        self.assertEqual(rc.name, "AngryPhonebook")
        self.assertAlmostEqual(rc.ratio, 12325.0 / 11616.0)
        self.assertAlmostEqual(rc.delta, (((11616.0 / 12325.0) - 1) * 100), places=3)
        # handle test results that sometimes change to zero, when compiler
        # optimizes out the body of the incorrectly written test
        rc = ResultComparison(self.r0, self.r0)
        self.assertEqual(rc.name, "GlobalClass")
        self.assertAlmostEqual(rc.ratio, 1)
        self.assertAlmostEqual(rc.delta, 0, places=3)
        rc = ResultComparison(self.r0, self.r01)
        self.assertAlmostEqual(rc.ratio, 0, places=3)
        self.assertAlmostEqual(rc.delta, 2000000, places=3)
        rc = ResultComparison(self.r01, self.r0)
        self.assertAlmostEqual(rc.ratio, 20001)
        self.assertAlmostEqual(rc.delta, -99.995, places=3)
        # disallow comparison of different test results
        self.assertRaises(AssertionError, ResultComparison, self.r0, self.r1)

    def test_values_is_dubious(self):
        self.assertFalse(ResultComparison(self.r1, self.r2).is_dubious)
        # new.min < old.min < new.max
        self.assertTrue(ResultComparison(self.r1, self.r3).is_dubious)
        # other way around: old.min < new.min < old.max
        self.assertTrue(ResultComparison(self.r3, self.r1).is_dubious)

class FileSystemIntegration(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.mkdtemp()

    def tearDown(self):
        # Remove the directory after the test
        shutil.rmtree(self.test_dir)

    def write_temp_file(self, file_name, data):
        temp_file_name = os.path.join(self.test_dir, file_name)
        with open(temp_file_name, "w") as f:
            for line in data:
                f.write(line)
                f.write('\n')
        return temp_file_name

class OldAndNewLog(unittest.TestCase):

    old_log_content = [
        """{"number":1,"name":"AngryPhonebook","""
        + """"samples":[10458,12714,11000],"max_rss":10204365}""",
        """{"number":2,"name":"AnyHashableWithAClass","""
        + """"samples":[247027,319065,259056,259056],"max_rss":10250445}""",
        """{"number":3,"name":"Array2D","""
        + """"samples":[335831,400221,346622,346622],"max_rss":28297216}""",
        """{"number":4,"name":"ArrayAppend","""
        + """"samples":[23641,29000,24990,24990],"max_rss":11149926}""",
        """{"number":34,"name":"BitCount","samples":[3,4,4,4],"max_rss":10192896}""",
        """{"number":35,"name":"ByteSwap","samples":[4,6,4,4],"max_rss":10185933}"""
    ]

    new_log_content = [
        """{"number":265,"name":"TwoSum","samples":[5006,5679,5111,5111]}""",
        """{"number":35,"name":"ByteSwap","samples":[0,0,0,0,0]}""",
        """{"number":34,"name":"BitCount","samples":[9,9,9,9]}""",
        """{"number":4,"name":"ArrayAppend","samples":[20000,29000,24990,24990]}""",
        """{"number":3,"name":"Array2D","samples":[335831,400221,346622,346622]}""",
        """{"number":1,"name":"AngryPhonebook","samples":[10458,12714,11000,11000]}"""
    ]

    def makeResult(json_text):
        return PerformanceTestResult(json.loads(json_text))

    old_results = dict(
        [
            (r.name, r) for r in map(makeResult, old_log_content)
        ]
    )

    new_results = dict(
        [
            (r.name, r) for r in map(makeResult, new_log_content)
        ]
    )

    def assert_report_contains(self, texts, report):
        assert not isinstance(texts, str)
        for text in texts:
            self.assertIn(text, report)

class TestLogParser(unittest.TestCase):
    def test_parse_results_csv(self):
        """Ignores unknown lines, extracts data from supported formats."""
        log = """#,TEST,SAMPLES,MIN(us),MAX(us),MEAN(us),SD(us),MEDIAN(us)
7,Array.append.Array.Int?,20,10,10,10,0,10
21,Bridging.NSArray.as!.Array.NSString,20,11,11,11,0,11
42,Flatten.Array.Tuple4.lazy.for-in.Reserve,20,3,4,4,0,4

Total performance tests executed: 1
"""
        parser = LogParser()
        results = parser.parse_results(log.splitlines())
        self.assertTrue(isinstance(results[0], PerformanceTestResult))
        self.assertEqual(results[0].name, "Array.append.Array.Int?")
        self.assertEqual(results[1].name, "Bridging.NSArray.as!.Array.NSString")
        self.assertEqual(results[2].name, "Flatten.Array.Tuple4.lazy.for-in.Reserve")

    def test_parse_results_tab_delimited(self):
        log = "34\tBitCount\t20\t3\t4\t4\t0\t4"
        parser = LogParser()
        results = parser.parse_results(log.splitlines())
        self.assertTrue(isinstance(results[0], PerformanceTestResult))
        self.assertEqual(results[0].name, "BitCount")

    def test_parse_results_formatted_text(self):
        """Parse format that Benchmark_Driver prints to console"""
        log = """
  # TEST      SAMPLES MIN(μs) MAX(μs) MEAN(μs) SD(μs) MEDIAN(μs) MAX_RSS(B)
  3 Array2D        20    2060    2188     2099      0       2099   20915200

Total performance tests executed: 1
"""
        parser = LogParser()
        results = parser.parse_results(log.splitlines()[1:])  # without 1st \n
        self.assertTrue(isinstance(results[0], PerformanceTestResult))
        r = results[0]
        self.assertEqual(r.name, "Array2D")
        self.assertEqual(r.max_rss, 20915200)

    def test_parse_quantiles(self):
        """Gathers samples from reported quantiles. Handles optional memory."""
        r = LogParser.results_from_string(
            """#,TEST,SAMPLES,QMIN(μs),MEDIAN(μs),MAX(μs)
1,Ackermann,3,54383,54512,54601"""
        )["Ackermann"]
        self.assertEqual(r.samples, [54383, 54512, 54601])
        r = LogParser.results_from_string(
            """#,TEST,SAMPLES,QMIN(μs),MEDIAN(μs),MAX(μs),MAX_RSS(B)
1,Ackermann,3,54529,54760,55807,266240"""
        )["Ackermann"]
        self.assertEqual(r.samples, [54529, 54760, 55807])
        self.assertEqual(r.max_rss, 266240)

    def test_parse_delta_quantiles(self):
        r = LogParser.results_from_string(  # 2-quantile aka. median
            "#,TEST,SAMPLES,QMIN(μs),𝚫MEDIAN,𝚫MAX\n0,B,1,101,,"
        )["B"]
        self.assertEqual(
            (r.num_samples, r.min_value, r.median, r.max_value, len(r.samples)),
            (1, 101, 101, 101, 1),
        )
        r = LogParser.results_from_string(
            "#,TEST,SAMPLES,QMIN(μs),𝚫MEDIAN,𝚫MAX\n0,B,2,101,,1"
        )["B"]
        self.assertEqual(
            (r.num_samples, r.min_value, r.median, r.max_value, len(r.samples)),
            (2, 101, 101.5, 102, 2),
        )
        r = LogParser.results_from_string(  # 20-quantiles aka. ventiles
            "#,TEST,SAMPLES,QMIN(μs),𝚫V1,𝚫V2,𝚫V3,𝚫V4,𝚫V5,𝚫V6,𝚫V7,𝚫V8,"
            + "𝚫V9,𝚫VA,𝚫VB,𝚫VC,𝚫VD,𝚫VE,𝚫VF,𝚫VG,𝚫VH,𝚫VI,𝚫VJ,𝚫MAX\n"
            + "202,DropWhileArray,200,214,,,,,,,,,,,,1,,,,,,2,16,464"
        )["DropWhileArray"]
        self.assertEqual(
            (r.num_samples, r.min_value, r.max_value, len(r.samples)),
            (200, 214, 697, 0),
        )

    def test_parse_meta(self):
        r = LogParser.results_from_string(
            "#,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),"
            + "PAGES,ICS,YIELD\n"
            + "0,B,1,2,2,2,0,2,7,29,15"
        )["B"]
        self.assertEqual(
            (r.min_value, r.mem_pages, r.involuntary_cs, r.yield_count), (2, 7, 29, 15)
        )
        r = LogParser.results_from_string(
            "#,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),"
            + "MAX_RSS(B),PAGES,ICS,YIELD\n"
            + "0,B,1,3,3,3,0,3,36864,9,50,15"
        )["B"]
        self.assertEqual(
            (r.min_value, r.mem_pages, r.involuntary_cs, r.yield_count, r.max_rss),
            (3, 9, 50, 15, 36864),
        )
        r = LogParser.results_from_string(
            "#,TEST,SAMPLES,QMIN(μs),MAX(μs),PAGES,ICS,YIELD\n" + "0,B,1,4,4,8,31,15"
        )["B"]
        self.assertEqual(
            (r.min_value, r.mem_pages, r.involuntary_cs, r.yield_count), (4, 8, 31, 15)
        )
        r = LogParser.results_from_string(
            "#,TEST,SAMPLES,QMIN(μs),MAX(μs),MAX_RSS(B),PAGES,ICS,YIELD\n"
            + "0,B,1,5,5,32768,8,28,15"
        )["B"]
        self.assertEqual(
            (r.min_value, r.mem_pages, r.involuntary_cs, r.yield_count, r.max_rss),
            (5, 8, 28, 15, 32768),
        )

    def test_results_from_merge(self):
        """Parsing concatenated log merges same PerformanceTestResults"""
        concatenated_logs = """#,TEST,SAMPLES,MIN,MAX,MEAN,SD,MEDIAN
4,ArrayAppend,20,23641,29000,24990,0,24990
4,ArrayAppend,1,20000,20000,20000,0,20000"""
        results = LogParser.results_from_string(concatenated_logs)
        self.assertEqual(list(results.keys()), ["ArrayAppend"])
        result = results["ArrayAppend"]
        self.assertTrue(isinstance(result, PerformanceTestResult))
        self.assertEqual(result.min_value, 20000)
        self.assertEqual(result.max_value, 29000)

class TestTestComparator(OldAndNewLog):
    def test_init(self):
        def names(tests):
            return [t.name for t in tests]

        tc = TestComparator(self.old_results, self.new_results, 0.05)
        self.assertEqual(names(tc.unchanged), ["AngryPhonebook", "Array2D"])
#        self.assertEqual(names(tc.increased), ["ByteSwap", "ArrayAppend"])
        self.assertEqual(names(tc.decreased), ["BitCount"])
        self.assertEqual(names(tc.added), ["TwoSum"])
        self.assertEqual(names(tc.removed), ["AnyHashableWithAClass"])
        # other way around
        tc = TestComparator(self.new_results, self.old_results, 0.05)
        self.assertEqual(names(tc.unchanged), ["AngryPhonebook", "Array2D"])
        self.assertEqual(names(tc.increased), ["BitCount"])
        self.assertEqual(names(tc.decreased), ["ByteSwap", "ArrayAppend"])
        self.assertEqual(names(tc.added), ["AnyHashableWithAClass"])
        self.assertEqual(names(tc.removed), ["TwoSum"])
        # delta_threshold determines the sorting into change groups;
        # report only change above 100% (ByteSwap's runtime went to 0):
        tc = TestComparator(self.old_results, self.new_results, 1)
        self.assertEqual(
            names(tc.unchanged),
            ["AngryPhonebook", "Array2D", "ArrayAppend", "BitCount"],
        )
        self.assertEqual(names(tc.increased), ["ByteSwap"])
        self.assertEqual(tc.decreased, [])

class TestReportFormatter(OldAndNewLog):
    def setUp(self):
        super(TestReportFormatter, self).setUp()
        self.tc = TestComparator(self.old_results, self.new_results, 0.05)
        self.rf = ReportFormatter(self.tc, changes_only=False)
        self.markdown = self.rf.markdown()
        self.git = self.rf.git()
        self.html = self.rf.html()

    def assert_markdown_contains(self, texts):
        self.assert_report_contains(texts, self.markdown)

    def assert_git_contains(self, texts):
        self.assert_report_contains(texts, self.git)

    def assert_html_contains(self, texts):
        self.assert_report_contains(texts, self.html)

    def test_values(self):
        self.assertEqual(
            ReportFormatter.values(
                PerformanceTestResult(
                    """{"number":1,"name":"AngryPhonebook",
                    "samples":[10664,12933,11035,10884]}"""
                )
            ),
            ("AngryPhonebook", "10664", "12933", "11379", "—"),
        )
        self.assertEqual(
            ReportFormatter.values(
                PerformanceTestResult(
                    """{"number":1,"name":"AngryPhonebook",
                    "samples":[12045],"max_rss":10510336}"""
                )
            ),
            ("AngryPhonebook", "12045", "12045", "12045", "10510336"),
        )

        r1 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12325],"max_rss":10510336}"""
        )
        r2 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[11616],"max_rss":10510336}"""
        )
        self.assertEqual(
            ReportFormatter.values(ResultComparison(r1, r2)),
            ("AngryPhonebook", "12325", "11616", "-5.8%", "1.06x"),
        )
        self.assertEqual(
            ReportFormatter.values(ResultComparison(r2, r1)),
            ("AngryPhonebook", "11616", "12325", "+6.1%", "0.94x"),
        )

        r1 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[12325],"max_rss":10510336}"""
        )
        r2 = PerformanceTestResult(
            """{"number":1,"name":"AngryPhonebook",
            "samples":[11616,12326],"max_rss":10510336}"""
        )
        self.assertEqual(
            ReportFormatter.values(ResultComparison(r1, r2))[4],
            "1.06x (?)",  # is_dubious
        )

    def test_justified_columns(self):
        """Table columns are all formated with same width, defined by the
        longest value.
        """
        self.assert_markdown_contains(
            [
                "AnyHashableWithAClass | 247027 | 319065 | 271051  | 10250445",
                "Array2D               | 335831 | 335831 | +0.0%   | 1.00x",
            ]
        )
        self.assert_git_contains(
            [
                "AnyHashableWithAClass   247027   319065   271051    10250445",
                "Array2D                 335831   335831   +0.0%     1.00x",
            ]
        )

    def test_column_headers(self):
        """Report contains table headers for ResultComparisons and changed
        PerformanceTestResults.
        """
        performance_test_result = self.tc.added[0]
        self.assertEqual(
            ReportFormatter.header_for(performance_test_result),
            ("TEST", "MIN", "MAX", "MEAN", "MAX_RSS"),
        )
        comparison_result = self.tc.increased[0]
        self.assertEqual(
            ReportFormatter.header_for(comparison_result),
            ("TEST", "OLD", "NEW", "DELTA", "RATIO"),
        )
        self.assert_markdown_contains(
            [
                "TEST                  | OLD    | NEW    | DELTA   | RATIO",
                ":---                  | ---:   | ---:   | ---:    | ---:   ",
                "TEST                  | MIN    | MAX    | MEAN    | MAX_RSS",
            ]
        )
        self.assert_git_contains(
            [
                "TEST                    OLD      NEW      DELTA     RATIO",
                "TEST                    MIN      MAX      MEAN      MAX_RSS",
            ]
        )
        self.assert_html_contains(
            [
                """
                <th align='left'>OLD</th>
                <th align='left'>NEW</th>
                <th align='left'>DELTA</th>
                <th align='left'>RATIO</th>""",
                """
                <th align='left'>MIN</th>
                <th align='left'>MAX</th>
                <th align='left'>MEAN</th>
                <th align='left'>MAX_RSS</th>""",
            ]
        )

    def test_emphasize_speedup(self):
        """Emphasize speedup values for regressions and improvements"""
        # tests in No Changes don't have emphasized speedup
        self.assert_markdown_contains(
            [
                "BitCount              | 3      | 9      | +199.9% | **0.33x**",
                "ByteSwap              | 4      | 0      | -100.0% | **4001.00x**",
                "AngryPhonebook        | 10458  | 10458  | +0.0%   | 1.00x ",
                "ArrayAppend           | 23641  | 20000  | -15.4%  | **1.18x (?)**",
            ]
        )
        self.assert_git_contains(
            [
                "BitCount                3        9        +199.9%   **0.33x**",
                "ByteSwap                4        0        -100.0%   **4001.00x**",
                "AngryPhonebook          10458    10458    +0.0%     1.00x",
                "ArrayAppend             23641    20000    -15.4%    **1.18x (?)**",
            ]
        )
        self.assert_html_contains(
            [
                """
        <tr>
                <td align='left'>BitCount</td>
                <td align='left'>3</td>
                <td align='left'>9</td>
                <td align='left'>+199.9%</td>
                <td align='left'><font color='red'>0.33x</font></td>
        </tr>""",
                """
        <tr>
                <td align='left'>ByteSwap</td>
                <td align='left'>4</td>
                <td align='left'>0</td>
                <td align='left'>-100.0%</td>
                <td align='left'><font color='green'>4001.00x</font></td>
        </tr>""",
                """
        <tr>
                <td align='left'>AngryPhonebook</td>
                <td align='left'>10458</td>
                <td align='left'>10458</td>
                <td align='left'>+0.0%</td>
                <td align='left'><font color='black'>1.00x</font></td>
        </tr>""",
            ]
        )

    def test_sections(self):
        """Report is divided into sections with summaries."""
        self.assert_markdown_contains(
            [
                """<details open>
  <summary>Regression (1)</summary>""",
                """<details >
  <summary>Improvement (2)</summary>""",
                """<details >
  <summary>No Changes (2)</summary>""",
                """<details open>
  <summary>Added (1)</summary>""",
                """<details open>
  <summary>Removed (1)</summary>""",
            ]
        )
        self.assert_git_contains(
            [
                "Regression (1): \n",
                "Improvement (2): \n",
                "No Changes (2): \n",
                "Added (1): \n",
                "Removed (1): \n",
            ]
        )
        self.assert_html_contains(
            [
                "<th align='left'>Regression (1)</th>",
                "<th align='left'>Improvement (2)</th>",
                "<th align='left'>No Changes (2)</th>",
                "<th align='left'>Added (1)</th>",
                "<th align='left'>Removed (1)</th>",
            ]
        )

    def test_report_only_changes(self):
        """Leave out tests without significant change."""
        rf = ReportFormatter(self.tc, changes_only=True)
        markdown, git, html = rf.markdown(), rf.git(), rf.html()
        self.assertNotIn("No Changes", markdown)
        self.assertNotIn("AngryPhonebook", markdown)
        self.assertNotIn("No Changes", git)
        self.assertNotIn("AngryPhonebook", git)
        self.assertNotIn("No Changes", html)
        self.assertNotIn("AngryPhonebook", html)

    def test_single_table_report(self):
        """Single table report has inline headers and no elaborate sections."""
        self.tc.removed = []  # test handling empty section
        rf = ReportFormatter(self.tc, changes_only=True, single_table=True)
        markdown = rf.markdown()
        self.assertNotIn("<details", markdown)  # no sections
        self.assertNotIn("\n\n", markdown)  # table must not be broken
        self.assertNotIn("Removed", markdown)
        self.assert_report_contains(
            [
                "\n**Regression** ",
                "| **OLD**",
                "| **NEW**",
                "| **DELTA**",
                "| **RATIO**",
                "\n**Added** ",
                "| **MIN**",
                "| **MAX**",
                "| **MEAN**",
                "| **MAX_RSS**",
            ],
            markdown,
        )
        # Single delimiter row:
        self.assertIn("\n:---", markdown)  # first column is left aligned
        self.assertEqual(markdown.count("| ---:"), 4)  # other, right aligned
        # Separator before every inline header (new section):
        self.assertEqual(markdown.count("  | | | | "), 2)

        git = rf.git()
        self.assertNotIn("): \n", git)  # no sections
        self.assertNotIn("REMOVED", git)
        self.assert_report_contains(
            [
                "\nREGRESSION ",
                " OLD ",
                " NEW ",
                " DELTA ",
                " RATIO ",
                "\n\nADDED ",
                " MIN ",
                " MAX ",
                " MEAN ",
                " MAX_RSS ",
            ],
            git,
        )
        # Separator before every inline header (new section):
        self.assertEqual(git.count("\n\n"), 2)

class Test_parse_args(unittest.TestCase):
    required = ["--old-file", "old.log", "--new-file", "new.log"]

    def test_required_input_arguments(self):
        with captured_output() as (_, err):
            self.assertRaises(SystemExit, parse_args, [])
        self.assertIn("usage: compare_perf_tests.py", err.getvalue())

        args = parse_args(self.required)
        self.assertEqual(args.old_file, "old.log")
        self.assertEqual(args.new_file, "new.log")

    def test_format_argument(self):
        self.assertEqual(parse_args(self.required).format, "markdown")
        self.assertEqual(
            parse_args(self.required + ["--format", "markdown"]).format, "markdown"
        )
        self.assertEqual(parse_args(self.required + ["--format", "git"]).format, "git")
        self.assertEqual(
            parse_args(self.required + ["--format", "html"]).format, "html"
        )

        with captured_output() as (_, err):
            self.assertRaises(
                SystemExit, parse_args, self.required + ["--format", "bogus"]
            )
        self.assertIn(
            "error: argument --format: invalid choice: 'bogus' "
            "(choose from 'markdown', 'git', 'html')",
            err.getvalue(),
        )

    def test_delta_threshold_argument(self):
        # default value
        args = parse_args(self.required)
        self.assertEqual(args.delta_threshold, 0.05)
        # float parsing
        args = parse_args(self.required + ["--delta-threshold", "0.1"])
        self.assertEqual(args.delta_threshold, 0.1)
        args = parse_args(self.required + ["--delta-threshold", "1"])
        self.assertEqual(args.delta_threshold, 1.0)
        args = parse_args(self.required + ["--delta-threshold", ".2"])
        self.assertEqual(args.delta_threshold, 0.2)

        with captured_output() as (_, err):
            self.assertRaises(
                SystemExit, parse_args, self.required + ["--delta-threshold", "2,2"]
            )
        self.assertIn(
            " error: argument --delta-threshold: invalid float " "value: '2,2'",
            err.getvalue(),
        )

    def test_output_argument(self):
        self.assertEqual(parse_args(self.required).output, None)
        self.assertEqual(
            parse_args(self.required + ["--output", "report.log"]).output, "report.log"
        )

    def test_changes_only_argument(self):
        self.assertFalse(parse_args(self.required).changes_only)
        self.assertTrue(parse_args(self.required + ["--changes-only"]).changes_only)

class Test_compare_perf_tests_main(OldAndNewLog, FileSystemIntegration):
    """Integration test that invokes the whole comparison script."""

    markdown = [
        "<summary>Regression (1)</summary>",
        "TEST                  | OLD    | NEW    | DELTA   | RATIO",
        "BitCount              | 3      | 9      | +199.9% | **0.33x**",
    ]
    git = [
        "Regression (1):",
        "TEST                    OLD      NEW      DELTA     RATIO",
        "BitCount                3        9        +199.9%   **0.33x**",
    ]
    html = ["<html>", "<td align='left'>BitCount</td>"]

    def setUp(self):
        super(Test_compare_perf_tests_main, self).setUp()
        self.old_log = self.write_temp_file("old.log", self.old_log_content)
        self.new_log = self.write_temp_file("new.log", self.new_log_content)

    def execute_main_with_format(self, report_format, test_output=False):
        report_file = self.test_dir + "report.log"
        args = [
            "compare_perf_tests.py",
            "--old-file",
            self.old_log,
            "--new-file",
            self.new_log,
            "--format",
            report_format,
        ]

        sys.argv = args if not test_output else args + ["--output", report_file]

        with captured_output() as (out, _):
            main()
        report_out = out.getvalue()

        if test_output:
            with open(report_file, "r") as f:
                report = f.read()
            # because print adds newline, add one here, too:
            report_file = str(report + "\n")
        else:
            report_file = None

        return report_out, report_file

    def test_markdown(self):
        """Writes Markdown formatted report to stdout"""
        report_out, _ = self.execute_main_with_format("markdown")
        self.assert_report_contains(self.markdown, report_out)

    def test_markdown_output(self):
        """Writes Markdown formatted report to stdout and `--output` file."""
        report_out, report_file = self.execute_main_with_format(
            "markdown", test_output=True
        )
        self.assertEqual(report_out, report_file)
        self.assert_report_contains(self.markdown, report_file)

    def test_git(self):
        """Writes Git formatted report to stdout."""
        report_out, _ = self.execute_main_with_format("git")
        self.assert_report_contains(self.git, report_out)

    def test_git_output(self):
        """Writes Git formatted report to stdout and `--output` file."""
        report_out, report_file = self.execute_main_with_format("git", test_output=True)
        self.assertEqual(report_out, report_file)
        self.assert_report_contains(self.git, report_file)

    def test_html(self):
        """Writes HTML formatted report to stdout."""
        report_out, _ = self.execute_main_with_format("html")
        self.assert_report_contains(self.html, report_out)

    def test_html_output(self):
        """Writes HTML formatted report to stdout and `--output` file."""
        report_out, report_file = self.execute_main_with_format(
            "html", test_output=True
        )
        self.assertEqual(report_out, report_file)
        self.assert_report_contains(self.html, report_file)

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


def check(want, ten):
    flag = 1
    for one in want:
        if one not in ten or ten[one] < want[one]:
            flag = 0
            break
    return flag
# 그러면 discount에 있는 값이 100000 까지 존재할 수 있는데 이걸 10개씩 미루면서 딕셔너리 만들면..
#
def solution(want, number, discount):
    dic = {}
    want_dic = {}
    answer = 0
    for i in range(10):
        if discount[i] in dic:
            dic[discount[i]] +=1
        else:
            dic[discount[i]] = 1
    for i,val in enumerate(want):
        want_dic[val] = number[i]
    answer += check(want_dic, dic)
    for i in range(10,len(discount)):
        dic[discount[i-10]] -= 1
        if discount[i] in dic:
            dic[discount[i]] +=1
        else:
            dic[discount[i]] = 1
        answer += check(want_dic,dic)
    return answer

프로그래머스/lv2/131127. 할인 행사/할인 행사.py


from importlib.resources import path
from kubernetes import client as kclient
from kubernetes import config

config.load_incluster_config()
v1 = kclient.CoreV1Api()

# Trainer Pod deletes itself
try:
    api_response = v1.delete_namespaced_pod(
        name='trainer', namespace='mlbuffet')

except Exception as e:
    print("Exception when calling CoreV1Api->connect_delete_namespaced_pod_proxy: %s\n" % e)


#
# @lc app=leetcode id=347 lang=python3
#
# [347] Top K Frequent Elements
#
# https://leetcode.com/problems/top-k-frequent-elements/description/
#
# algorithms
# Medium (62.25%)
# Likes:    4564
# Dislikes: 260
# Total Accepted:    550.4K
# Total Submissions: 881.4K
# Testcase Example:  '[1,1,1,2,2,3]\n2'
#
# Given a non-empty array of integers, return the k most frequent elements.
#
# Example 1:
#
#
# Input: nums = [1,1,1,2,2,3], k = 2
# Output: [1,2]
#
#
#
# Example 2:
#
#
# Input: nums = [1], k = 1
# Output: [1]
#
#
# Note:
#
#
# You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
# Your algorithm's time complexity must be better than O(n log n), where n is
# the array's size.
# It's guaranteed that the answer is unique, in other words the set of the top
# k frequent elements is unique.
# You can return the answer in any order.
#
#
#

# @lc code=start
from heapq import heappush, heappop, heappushpop
from collections import Counter

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        if not nums or len(nums) == 0:
            return []

        counts = Counter(nums)
        freq = [(counts[key], key) for key in counts]

        heap = []
        for f, num in freq:

            if len(heap) < k:
                heappush(heap, (f, num))
            else:
                if f > heap[0][0]:
                    heappushpop(heap, (f, num))

        res = []
        while heap:
            res.append(heappop(heap)[1])
        return res

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        if not nums or len(nums) == 0:
            return []

        freq = Counter(nums)
        heap = []

        for num in freq:
            if len(heap) < k:
                heappush(heap, (freq[num], num))
            else:
                if freq[num] > heap[0][0]:
                    heapreplace(heap, (freq[num], num))

        # size of heap will be k
        res = [pair[1] for pair in heap]
        return res
# @lc code=end

# Time: O(nlog(k)) where n represents the number of unique number, worst case is n
# Space: O(n)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('lexicon', '0112_auto_20160929_1340'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='meaning',
            options={'ordering': ['gloss']},
        ),
        migrations.RenameField(
            model_name='meaning',
            old_name='elicitation',
            new_name='meaningSetIx',
        ),
        migrations.AddField(
            model_name='meaning',
            name='meaningSetMember',
            field=models.IntegerField(default=0),
        ),
    ]


import json
import logging
import math
from django.db import IntegrityError
from django.db.models import F
from datetime import datetime, timedelta

from django.template.loader import render_to_string

from anodyne import settings
from api.models import Reading, Station, StationInfo, StationParameter, \
    Exceedance, SMSAlert
from api.utils import send_mail, send_sms

log = logging.getLogger('vepolink')

class ToDatabase:
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def send_alert(self, exceedances):
        log.info('Sending Alert')
        log.info(exceedances)
        try:
            for exceedance in exceedances:
                station = exceedance.get('station')
                parameter = exceedance.get('parameter')
                param = StationParameter.objects.get(
                    station=station,
                    allowed=True,
                    parameter__name=parameter
                )
                context = {
                    'param': parameter,
                    'value': '%s %s against Pres. Stand. %s %s' % (
                        exceedance.get('value'), param.parameter.unit,
                        param.maximum, parameter),
                    'category': station.industry.type,
                    'industry': '%s, %s, %s' % (
                        station.industry, station.industry.city,
                        station.industry.state),
                    'timestamp': exceedance.get('timestamp').strftime(
                        '%a, %d-%b-%Y %H:%M'),
                    'alert_type': station.monitoring_type,
                    'location': param.monitoring_id
                }

                mail_receipients = station.user_email.split(';')
                html_content = render_to_string(
                    'alerts-mail/exceedance.html', context)
                send_mail(subject='Exceedance Alert',
                          recipient_list=mail_receipients,
                          cc=['info@anodyne.in'],
                          html_message=html_content,
                          message='',
                          from_email=settings.EMAIL_HOST_USER
                          )
                phone_receipients = station.user_ph
                sms_context = "SMS ALERT FROM VEPOLINK%nALERT: {alert_type}%nIndustry Name:{industry}%nCATEGORY:{category}%nLOCATION:{location}%nEXCEEDING PARAMETER:{param}%nVALUE:{value}%n{timestamp}%nAvg Value for last 15 Min%nRespond at customercare@anodyne.in".format(**context)
                log.info('Initiating Exceedance SMS')
                send_sms(numbers=phone_receipients, content=sms_context)
        except:
            log.exception('Failing to Send Mail alert')

    def check_exceedance(self, station, reading):
        log.info('Checking exceedance %s' % station)
        q = {
            'param': F('parameter__name'),
            'min': F('minimum'),
            'max': F('maximum')

        }
        params = StationParameter.objects.filter(
            station=station,
            allowed=True
        ).values(**q)
        exceedances_rec = []
        for meta in params:
            exceedances = {}
            param = meta.get('param')
            pmax = float(meta.get('max', 0))
            pmin = float(meta.get('min', 0))
            if pmin == pmax or pmax == 0:
                continue
            else:
                current_val = float(reading.get(param, 0))
                if current_val > pmax:
                    exceedances.update({
                        'parameter': param,
                        'value': current_val,
                    })
                if param.lower() == 'ph' and pmin > current_val > pmax:
                    exceedances.update({
                        'parameter': param,
                        'value': current_val,
                    })
                if exceedances:
                    log.info('Exceedances %s' % exceedances)
                    exceedances.update({'timestamp': reading.get('timestamp'),
                                        'station': station})
                    exceedances_rec.append(exceedances)

        if exceedances_rec:
            try:
                Exceedance.objects.bulk_create(
                    [Exceedance(**q) for q in exceedances_rec])
                log.info('Exceedance observed %s' % station)
            except IntegrityError:
                pass

            self.send_alert(exceedances_rec)

    def _clean_reading(self, reading):
        if reading:
            clean_reading = {}
            for k, v in reading.items():
                if k.lower() == 'timestamp':
                    k = k.lower()
                    clean_reading[k] = v
                else:
                    try:
                        value = float(v)
                        if not math.isnan(value):
                            clean_reading[k] = float('{0:.2f}'.format(value))
                    except ValueError:
                        pass
            if len(clean_reading.keys()) > 1:
                return clean_reading

    def insert(self):
        basename = self.kwargs.get('basename')
        response = {
            'success': False,
            'msg': ''
        }
        db_status = {
            'db': response
        }
        log.info('Adding to database:%s' % self.kwargs)
        try:
            readings = self._clean_reading(self.kwargs.get('readings'))
            if readings:
                station = Station.objects.get(prefix=self.kwargs.get('prefix'))
                self.check_exceedance(station, readings)
                Reading.objects.create(
                    station=station,
                    reading=readings
                )
                station.status = 'Live'
                station.save()
                sinfo, created = StationInfo.objects.get_or_create(
                    station=station)
                obj = sinfo if sinfo else created
                obj.last_seen = readings.get('timestamp')
                obj.last_upload_info = json.dumps(response)
                readings['timestamp'] = readings.get('timestamp').strftime(
                    '%Y-%m-%d %H:%M:%S ')
                obj.readings = json.dumps(readings)
                obj.save()
                log.info('Added to Reading successfully')
                response['success'] = True
                response['msg'] = "%s: Added Readings" % basename
            else:
                response['success'] = False
                response['msg'] = "%s: No Readings Found" % basename
        except IntegrityError:
            response['msg'] = "%s: Reading exists." % basename
            return db_status
        except Exception as err:
            response['success'] = False
            response['msg'] = "%s: Failed to readings to databse %s" % (
                basename, err
            )
            log.exception('DB ERROR')

        return db_status


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

from django.conf.urls import url
from .views import HomeView, ArchivesView, AboutView, PhotoView, MusicView, ArticleDetailView, CategoryView, TagListView, TagsView, CategoryListView, search

urlpatterns = [
    url(r'^$', HomeView.as_view(), name='home'),
    url(r'^archives/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/$', ArchivesView.as_view(), name='archives'),

    url(r'^category_list/$', CategoryListView.as_view(), name='category_list'),    #分类列表页
    url(r'^category/(?P<category_id>\d+)/$', CategoryView.as_view(), name='category'),

    url(r'^tag_list/$', TagListView.as_view(), name='tag_list'),    #标签列表页
    url(r'^tag/(?P<tag_id>\d+)/$', TagsView.as_view(), name='tag'), #所属标签的文章列表

    url(r'^about/', AboutView.as_view(), name='about'),
    url(r'^photo/', PhotoView.as_view(), name='photo'),
    url(r'^music/', MusicView.as_view(), name='music'),
    url(r'^article_detail/(?P<article_id>\d+)/$', ArticleDetailView.as_view(), name='article_detail'),

    url(r'^search/$',search, name="search"),
]


import datetime
import zadanie2lista6
import zadanie22lista6
plik = 'plik_do_szyfrowania.txt.txt'
openplik = open(plik,"r").read()

a = int(input("Podaj liczbe od 1-10: "))

date_today  = datetime.date.today()
month = date_today.month
year = date_today.year
day = date_today.day

g = ['plik_zaszyfrowany','_',a,year,'-',month,'-',day,'.txt']
all_strings = list(map(str, g))
result = ''.join(all_strings)

s = ['plik_deszyfrowany','_',a,year,'-',month,'-',day,'.txt']
all_strings = list(map(str, s))
wynik2 = ''.join(all_strings)

zaszyfrowane = zadanie2lista6.Szyfr_Cezara(openplik,a)

print(zaszyfrowane)

with open(result,'w', encoding="utf-8") as file:
    file.write(zaszyfrowane)

#ZADANIE2

szyfr2 = open(result,'r')
deszyfrowanie = zadanie22lista6.Szyfr_Cezara2(zaszyfrowane,a)
print(deszyfrowanie)
with open(wynik2,'w', encoding="utf-8") as file:
    file.write(deszyfrowanie)


import os
import sys
import zipfile
import urllib.request
import filecmp
import shutil
import errno
import typing
import orjson

VERSIONS_JSON = "https://launchermeta.mojang.com/mc/game/version_manifest.json"
RELEASE_TYPES = typing.Literal["release", "snapshot"]

def fetch_json(url: str):
    response = urllib.request.urlopen(url)
    return orjson.loads(response.read())

def get_urls(type: RELEASE_TYPES, number: int) -> list[str]:
    global VERSIONS_JSON
    urls = {}

    for item in fetch_json(VERSIONS_JSON)["versions"]:
        if len(urls) < (number + 1) and item["type"] == type:
            urls[item["id"]] = item["url"]

    return list(urls.values())

def save_temp(urls: list[str]) -> list[str]:
    names = []
    if not os.path.exists("temp"):
        os.mkdir("temp")

    for url in urls:
        name = fetch_json(url)["id"]
        names.append(name)

        os.mkdir(f"temp/{name}")
        with open(f"temp/{name}.zip", "wb") as f:
            f.write(
                urllib.request.urlopen(
                    fetch_json(url)["downloads"]["client"]["url"]
                ).read()
            )

        zip_ref = zipfile.ZipFile(f"temp/{name}.zip", "r")
        zip_ref.extractall(f"temp/{name}")
        zip_ref.close()

    return names

def diff_folders(new: str, old: str, type: RELEASE_TYPES, delete_folder: bool = False):
    added = []
    changed = []
    deleted = []

    if not delete_folder:
        diff_folders(old, new, type, delete_folder=True)

    for root, _, files in os.walk(f"temp/{new}"):
        for name in files:
            src = os.path.join(root, name)
            if f"temp/{new}/assets/minecraft/textures/" in src:
                dest = src.replace(new, old, 1)

                if not delete_folder:
                    if not os.path.exists(dest):
                        added.append(src)
                    elif not filecmp.cmp(src, dest):
                        changed.append(src)
                elif not os.path.exists(dest):
                    deleted.append(src)

    for item in added:
        save_diff(new, f"../{type.capitalize()}s/{new}/added", item)

    for item in changed:
        save_diff(new, f"../{type.capitalize()}s/{new}/changed", item)

    for item in deleted:
        save_diff(new, f"../{type.capitalize()}s/{old}/deleted", item)

def save_diff(base_folder: str, new_folder: str, item: str):
    src = item
    dest = item.replace(f"{base_folder}/assets/minecraft/textures/", f"{new_folder}/")

    try:
        shutil.copy(src, dest)
    except IOError as e:
        if e.errno != errno.ENOENT:
            raise

        os.makedirs(os.path.dirname(dest))
        e = shutil.copy(src, dest)

def main():
    release_type = sys.argv[1]
    number = int(sys.argv[2])

    if release_type not in {"release", "snapshot"}:
        print("Invalid release type")
        return

    if typing.TYPE_CHECKING:
        release_type = typing.cast(RELEASE_TYPES, release_type)

    print("Getting files...")
    urls = get_urls(release_type, number)
    folders = save_temp(urls)

    print("Comparing files...")
    for x in range(number):
        diff_folders(folders[x], folders[x + 1], release_type)

if __name__ == "__main__":
    main()


#!/usr/bin/env python
#
# This script downloads a game from OGS and produces a .game file
# that can be used by our test estimator.
#

import requests
import sys

def fetch_game(game_id):
    res = requests.get('https://online-go.com/termination-api/game/%d/state' % game_id)
    if res.status_code != 200:
        sys.stderr.write('Unable to fetch game\n')
        return None
    data = res.json()
    removal = data['removal']
    board = data['board']
    board = [
                [-1 if x == 2 else x for x in row]
                for row in board
            ]
    last_move = data['last_move']
    player_to_move = 0
    if last_move['y'] == -1:
        player_to_move = -board[last_move['y']][last_move['x']]
    if player_to_move == 0:
        player_to_move = 1
    return board, removal, player_to_move

def print_game(output, board, removal, player_to_move):
    output.write('# 1=black -1=white 0=open\n')
    output.write('height %d\n' % len(board))
    output.write('width %d\n' % len(board[0]))
    output.write('player_to_move %d\n' % player_to_move)
    for y in range(len(board)):
        output.write(' '.join('%2d' % x for x in board[y]) + '\n')
    output.write('\n');
    for y in range(len(removal)):
        output.write(' '.join('%2d' % x for x in removal[y]) + '\n')

if __name__ == "__main__":
    if len(sys.argv) != 2 or int(sys.argv[1]) <= 0:
        sys.stderr.write("Usage: ./fetch_ogs_game.py <game-id>\n")
    else:
        game_id = int(sys.argv[1])
        filename = '%d.game' % game_id
        with open(filename, 'w') as output:
            board, removal, player_to_move = fetch_game(game_id)
            print_game(output, board, removal, player_to_move)
        print('Wrote %s. You will want to modify the stone removal map to fix any errors that happened in scoring.' % filename)


"""
Python Crash Course, Third Edition https://ehmatthes.github.io/pcc_3e/
My notes: https://github.com/egalli64/pythonesque/pcc3

Chapter 15 - Generating Data - Plotting a Simple Line Graph - Plotting a Series of Points with scatter()
"""
import matplotlib.pyplot as plt

plt.style.use('seaborn')
fig, ax = plt.subplots()

# a few scattered points
xs = [1, 2, 3, 4, 5]
ys = [1, 4, 9, 16, 25]
ax.scatter(xs, ys, s=100)

ax.set_title("Square Numbers", fontsize=24)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)
ax.tick_params(labelsize=14)

plt.show()


import sys
# Bottom-up implementation of the classic rod-cut problem
def bottom_up_rod(p, n):
    r = [-1] * (n + 1)
    r[0] = 0

    for j in range(1, n+1):
        q = -sys.maxsize + 1
        for i in range(1, j+1):
            q = max(q, p[i] + r[j -i])
        r[j] = q
    return r[n]

prices = {1:1, 2:5, 3:8, 4:9, 5:10, 6:17, 7:17, 8:20, 9:24, 10:30}

print("Max rod revenue for length 4 is %d" % bottom_up_rod(prices, 4))


import XInput
from pynput import keyboard
from pygame import mixer
mixer.init()
import time

class XinputHandler(XInput.EventHandler):
    def __init__(self, keyMan):
        super().__init__(0, 1, 2, 3)
        self.keyMan = keyMan

    def process_button_event(self, event):
        if event.type == XInput.EVENT_BUTTON_PRESSED:
            if event.button_id in [1,2,4,8, 4096, 8192, 16384, 32768]:
                self.keyMan.ckey = '1'
            if event.button_id in [256, 512]:
                self.keyMan.ckey = '2'

class KeyMan:
    def __init__(self):
        self.ckey = ''
        keyboard.Listener(on_press=self.press).start()
        handler = XinputHandler(self)
        thread = XInput.GamepadThread()
        thread.add_event_handler(handler)

    def reset(self):
        self.ckey = '0'

    def press(self, key: keyboard.HotKey):
        try:
            if key.char in 'jf':
                self.ckey = '1'
            elif key.char in 'kd':
                self.ckey = '2'
            elif key.char in 'q':
                self.ckey = 'q'
        except AttributeError:
            pass

class Waiter:
    def __init__(self):
        self.lastTime = None

    def init(self):
        self.lastTime = time.time()

    def wait(self, sec):
        nextTime = self.lastTime + sec
        sleepTime = nextTime - time.time()
        if sleepTime > 0:
            time.sleep(nextTime - time.time())
        self.lastTime = nextTime

key = KeyMan()

beep1 = mixer.Sound('beep1')
beep2 = mixer.Sound('beep2')

#
# エディター
#
def editor(beats, secPerBeat, length, soundSpan, lag):

    if secPerBeat/2-lag < 0 or secPerBeat/2+lag < 0:
        print('lag値が大きすぎます')
        return
    sheet = []

    input('(press enter key to start)')
    key.reset()

    waiter = Waiter()
    waiter.init()
    for _ in range(length):
        line = ''
        beep1.play()
        waiter.wait(secPerBeat/2+lag)
        line += key.ckey
        key.reset()
        waiter.wait(secPerBeat/2-lag)
        for i in range(beats-1):
            if (i+1) % soundSpan == 0:
                beep2.play()
            waiter.wait(secPerBeat/2+lag)
            line += key.ckey
            key.reset()
            waiter.wait(secPerBeat/2-lag)

        print(line)
        sheet.append(line)

    return sheet

def editor_ex(bpm: int, base: int, beats: int, length: int, soundSpan: int=1, lag=0):
    # base : 基本の拍数
    # beats : 1小節内の拍数
    # length : 小節数
    # soundSpan : 音を鳴らす頻度
    # lag : 入力の遅延補正

    secPerMeasure = (60/bpm) * base
    secPerBeat = secPerMeasure / beats

    return editor(beats, secPerBeat, length, soundSpan, lag)


from rest_framework.test import APITestCase
from restapi.models import Companies, Countries

class FilterTest(APITestCase):

    @classmethod
    def setUpTestData(cls):
        companies = 10
        Countries.objects.create(name="c", continent="c", population=1, capital="c", surface=1)
        country = Countries.objects.get(name="c")
        for c_id in range(companies):
            Companies.objects.create(name=f"c {c_id}", year_founded=1950 + c_id, number_of_employees=10, country=country,
                                    activity="a")

    def test_correct_result(self):
        response = self.client.get("/restapi/companies/?year=1950")
        self.assertEqual(len(response.data), 10)
        response1 = self.client.get("/restapi/companies/?year=1955")
        self.assertEqual(len(response1.data), 5)
        response2 = self.client.get("/restapi/companies/?year=1960")
        self.assertEqual(len(response2.data), 0)


# define a function that take list of words as argument and
# return list with reverse of every element in that list

# example :
# ['abc','xyz','tuv']  ---> ['cba', 'zyx', 'vut']

def reverse_item(l):
    r_list = []
    for i in l:
        r_list.append(i[::-1])
    return r_list

l = ['abc','xyz','tuv']
print(reverse_item(l))


import numpy as np
import logo
import words

print('\n')
print(logo.h_logo)

random_word = np.random.choice(words.words).lower()
print('\n')
print("Randomly chosen word for sample game: ", random_word)

display = []
for i in range(len(random_word)):
    display += '_'

game = True
life = 5

while game == True:
    print('\n')
    guess = input("Enter the guess letter: ")

    position = 0
    for letter in random_word:
        if letter == guess:
            print('\n')
            print("The guess was right")
            display[position] = guess
            print(display)
        position += 1

    if guess not in random_word:
        life -= 1
        print(f"The guess was wrong, your current life: {life}/5")

    if '_' not in display:
        print('\n')
        print("*** You Won ***")
        game = False

    if life == 0:
        print('\n')
        print("*** You Lose ***")
        game = False


import json
# things we need for NLP
import nltk
from nltk.stem.lancaster import LancasterStemmer
nltk.download('punkt')
stemmer = LancasterStemmer()
# things we need for Tensorflow
import numpy as np
import tflearn
import tensorflow as tf
import random
import pickle

class ModelBuilder(object):
    def __init__(self):
        with open('intents.json') as json_data:
            self.intents = json.load(json_data)
        self.words = []
        self.classes = []
        self.documents = []
        self.ignore_words = [
            'what', 'are', 'is', 'the', 'why',
            'does', 'how', 'in', 'on', '?', 'my',
            'I'
        ]

    def parse_intents_doc(self):
        # loop through each sentence in our intents patterns
        for intent in self.intents['intents']:
            for pattern in intent['patterns']:
                # tokenize each word in the sentence
                w = nltk.word_tokenize(pattern)
                # add to our words list
                self.words.extend(w)
                # add to documents in our corpus
                self.documents.append((w, intent['tag']))
                # add to our classes list
                if intent['tag'] not in self.classes:
                    self.classes.append(intent['tag'])

        # stem and lower each word and remove duplicates
        self.words = [stemmer.stem(w.lower()) for w in self.words if w not in self.ignore_words]
        self.words = sorted(list(set(self.words)))
        # remove duplicates
        self.classes = sorted(list(set(self.classes)))

    def build_training_data(self):
        # create our training data
        training = []
        output = []
        # create an empty array for our output
        output_empty = [0] * len(self.classes)

        # training set, bag of words for each sentence
        for doc in self.documents:
            # initialize our bag of words
            bag = []
            # list of tokenized words for the pattern
            pattern_words = doc[0]
            # stem each word
            pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
            # create our bag of words array
            for w in self.words:
                if w in pattern_words:
                    bag.append(1)
                else:
                    bag.append(0)

            # output is a '0' for each tag and '1' for current tag
            output_row = list(output_empty)
            output_row[self.classes.index(doc[1])] = 1

            training.append([bag, output_row])

        # shuffle our features and turn into np.array
        random.shuffle(training)
        training = np.array(training)

        # create train and test lists
        train_x = list(training[:,0])
        train_y = list(training[:,1])
        return train_x, train_y

    def train_neural_network(self, train_x, train_y):
        # reset underlying graph data
        tf.reset_default_graph()
        # Build neural network
        net = tflearn.input_data(shape=[None, len(train_x[0])])
        net = tflearn.fully_connected(net, 8)
        net = tflearn.fully_connected(net, 8)
        net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
        net = tflearn.regression(net)

        # Define model and setup tensorboard
        model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
        # Start training (apply gradient descent algorithm)
        model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)
        model.save('model.tflearn')

        # save all of our data structures
        pickle.dump({
            'words': self.words,
            'classes': self.classes,
            'train_x': train_x,
            'train_y': train_y
          },
          open('training_data', 'wb')
        )

if __name__ == '__main__':
    model_builder = ModelBuilder()
    model_builder.parse_intents_doc()
    train_x, train_y = model_builder.build_training_data()
    model_builder.train_neural_network(train_x, train_y)


# Import SQLite3
import sqlite3

#create a database connection called "cars"
conn = sqlite3.connect("cars.db")

#Create the cursor to execute commands
cursor = conn.cursor()

#create a table/query called inventory that includes "make, model and quantity"
#use the cursor to execute this!
cursor.execute("""CREATE TABLE inventory
               (Make TEXT, Model TEXT, Quantity INT)
               """)

#close the connection
conn.close()


from django.urls import path
from . import views

urlpatterns = [
    path(
        '',
        views.all_products,
        name='products'),
    path(
        'ranked/',
        views.products_ranking,
        name='products_ranking'),
    path(
        '<int:product_id>/',
        views.product_detail,
        name='product_detail'),
    path(
        'add/',
        views.ProductCreateView.as_view(),
        name='add_product'),
    path(
        'search/',
        views.search_page,
        name='search_page'),
    path(
        'htmx-search/',
        views.htmx_search_products,
        name='htmx_search'),
    path(
        'edit/<int:pk>/',
        views.ProductUpdateView.as_view(),
        name='edit_product'),
    path(
        'delete/<int:product_id>/',
        views.delete_product,
        name='delete_product'),
    path(
        'add_to_wishlist/<int:id>/',
        views.add_to_wishlist,
        name='add_to_wishlist'),
    path(
        'delete_wishlist/<int:id>/',
        views.delete_wishlist_item,
        name='delete_wishlist_item'),
    path(
        'category/<category>/',
        views.category_products,
        name='product_category_view'),
    path(
        'add_category/',
        views.add_product_category,
        name='add_product_category'),
    path(
        'edit_category/<int:id>/',
        views.edit_product_category,
        name='edit_product_category'),
    path(
        'delete_category/<int:id>/',
        views.delete_product_category,
        name='delete_product_category'),
    path(
        'delete_comment/<int:id>/',
        views.delete_comment,
        name='delete_comment'),
]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 24 13:08:39 2022

@author: luisdsaco

(C) 2017-2022 Luis Díaz Saco

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""

from sacagents import AgentStoppedError, Agent, SpyAgent, CounterAgent
from sacagents import __version__ as sac_ver, __author__ as sac_auth

def test_message(ag):
    ag.print_locked('The current status of created agent', ag.ID(), 'is',
           ag.status())
    ag.print_locked("The total number of agents is", Agent.total_num_agents())

if __name__ == "__main__":

    print("Using Sacagents ", sac_ver, ": (C) 2022 ", sac_auth)

    # Testing Creation and Cloning

    ag1 = Agent(0)
    test_message(ag1)
    ag2 = Agent(5)
    test_message(ag2)
    ag3 = ag1.clone()
    test_message(ag3)
    ag4 = SpyAgent('English')
    test_message(ag4)
    ag5 = CounterAgent()
    test_message(ag5)

    # Testing direct execution

    aglist = [ag1,ag2,ag3,ag4]

    for ag in aglist:
        ag.send_message('Run')

    for ag in aglist:
        ag.send_message('Stop')

    for ag in aglist:
        if ag.is_alive():
            ag.join()

    # Testing threded operations

    for i in range(5):
        ag5.print_locked("Send message",i)
        ag5.send_message('Thread')

    ag5.send_message('Stop')
    if ag5.is_alive():
        ag5.join()

    # Testing delayed operations
    # German agents only confess after 10 attempts
    # There is programmed a delayed confession

    ag4 = SpyAgent('German')
    for i in range(10):
        ag4.send_message('Run')
    test_message(ag4)
    ag4.send_message('Timer')
    ag4.send_message('Stop')
    ag4.join()

    # Testing erroneous commands

    ag5 = SpyAgent('Spanish')
    test_message(ag5)
    ag5.send_message('Err')
    ag5.send_message('Stop')
    ag5.join()

    # Testing invalid data and exception handling

    ag3 = SpyAgent('French')
    test_message(ag3)
    ag3.send_message('Run')
    ag3.send_message('Stop')
    ag3.join()
    ag3.add_confession('French','Je suis un espion américain')
    try:
        ag3.send_message('Run')
        ag3.send_message('Stop')
        ag3.join()
    except AgentStoppedError:
        print('Cannot receive messages again')

   # Testing the modificacion of the status

    ag3 = SpyAgent('French')
    ag3.add_confession('French','Je suis un espion américain')
    try:
        ag3.send_message('Run')
        ag3.send_message('Stop')
        ag3.join()
    except AgentStoppedError:
        print('Cannot execute it again')

    print("End of program before")

    # Delayed messages will appear after the last print command


class Solution(object):
    # Time:O(N^2)  Space: O(N)
    def ThreeSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        nums.sort()
        res = []
        for i in range(len(nums)-2):
            l = i + 1
            r = len(nums)-1

            while l < r:
                if nums[i] + nums[l] + nums[r] == target:
                     res.append([nums[i], nums[l], nums[r]])
                     l += 1
                elif nums[i] + nums[l] + nums[r] > target:
                    r -= 1
                else:
                    l += 1

        return res

nums = [-1,0,1,2,-1,-4]
target = 0

s = Solution()
print(s.ThreeSum(nums, target))


import copy
def polygonal(s, n):
    if s == 3: # Triangle
        return n * (n + 1) // 2
    elif s == 4: # Square
        return n * n
    elif s == 5: # Pentagonal
        return n * (3 * n - 1) // 2
    elif s == 6: # Hexagonal
        return n * (2 * n - 1)
    elif s == 7: # Heptagonal
        return n * (5 * n - 3) // 2
    elif s == 8: # Octagonal
        return n * (3 * n - 2)

# Generate all the 4-digit polygonal numbers
polygonals = {}
for s in range(3, 9):
    polygonals[s] = []
    n = 1
    while True:
        p = polygonal(s, n)
        if p < 1000:
            n += 1
        elif p < 10000:
            polygonals[s].append(p)
            n += 1
        else:
            break

# Recursive function to find the cyclic set
def find_cyclic_set(numbers, polygonal_types, current_set):
    if len(current_set) == 6 and current_set[0] // 100 == current_set[-1] % 100:
        return current_set
    for s in polygonal_types:
        for number in numbers[s]:
            #if len(current_set) == 0:
            #    print('b1')
            if len(current_set) == 0 or current_set[-1] % 100 == number // 100:
                new_polygonal_types = polygonal_types.copy()
                new_polygonal_types.remove(s)
                new_set = current_set + [number]
                result = find_cyclic_set(numbers, new_polygonal_types, new_set)
                if result is not None:
                    return result
    return None

# Find the cyclic set
cyclic_set = find_cyclic_set(polygonals, list(range(3, 9)), [])

# Print the sum of the cyclic set
print(sum(cyclic_set))


import copy
import numpy as np
import cv2
import time
import random
import argparse

def draw_obstacles(canvas,clr=5,unknown=False, map_flag=1):
    """
    @brief: This function goes through each node in the canvas image and checks for the
    obstacle space using the half plane equations.
    If the node is in obstacle space, the color is changed to blue.
    :param canvas: Canvas Image
    :param unknown: Unknown Map flag
    """
    # Uncomment to use the cv2 functions to create the obstacle space
    # cv2.circle(canvas, (300,65),45,(255,0,0),-1)
    # cv2.fillPoly(canvas, pts = [np.array([[115,40],[36,65],[105,150],[80,70]])], color=(255,0,0)) #Arrow
    # cv2.fillPoly(canvas, pts = [np.array([[200,110],[235,130],[235,170],[200,190],[165,170],[165,130]])], color=(255,0,0)) #Hexagon

    height,width,_ = canvas.shape

    for i in range(width):
        for j in range(height):
            if map_flag == 1:
                # Map 01
                if(i<=clr) or (i>=(400-clr)) or (j<=clr) or (j>=(250-clr)):
                    canvas[j][i] = [255,0,0]

                if ((i-300)**2+(j-65)**2-((40+clr)**2))<=0:
                    canvas[j][i] = [255,0,0]

                if (j+(0.57*i)-213.53)>=clr and (j-(0.57*i)+5.04+clr)>=0 and (i-235-clr)<=0 and (j+(0.57*i)-305.04-clr)<=0 and (j-(0.57*i)-76.465-clr)<=0 and (i-155-clr)>=0:
                    canvas[j][i] = [255,0,0]

                if ((j+(0.316*i)-66.1483-clr)>=0 and (j+(0.857*i)-140.156-clr)<=0 and (j-(0.114*i)-55.909-clr)<=0) or ((j-(1.23*i)-23.576-clr)<=0 and (j-(3.2*i)+197.763+clr)>=0 and (j-(0.114*i)-55.909-clr)>=0):
                    canvas[j][i] = [255,0,0]

            elif map_flag == 2:
                # Map 02
                if(i<=clr) or (i>=(400-clr)) or (j<=clr) or (j>=(250-clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=clr) and (j<=63)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=103-clr) and (j<=147+clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=118-clr) and (i<=148+clr) and (j>=187-clr) and (j<=(250-clr))):
                    canvas[j][i] = [255,0,0]

                if ((i>=251-clr) and (i<=281+clr) and (j>=42-clr) and (j<=105+clr)):
                    canvas[j][i] = [255,0,0]

                if ((i>=251-clr) and (i<=281+clr) and (j>=145-clr) and (j<=208+clr)):
                    canvas[j][i] = [255,0,0]

    if(unknown):
        for i in range(width):
            for j in range(height):

                if map_flag == 1:
                    if ((i-110)**2+(j-210)**2-((35)**2))<=0:
                        canvas[j][i] = [0,255,0]
                elif map_flag == 2:
                    if ((i-70)**2+(j-190)**2-((35)**2))<=0:
                        canvas[j][i] = [0,255,0]

                    if ((i-200)**2+(j-140)**2-((20)**2))<=0:
                        canvas[j][i] = [0,255,0]

    return canvas

def is_obstacle(next_width,next_height,canvas,unknown=False):
    """
    @brief: This function checks if the node is present in the obstacle.
    If the node is in obstacle space, the function returns true.
    :param canvas: Canvas Image
    :param unknown: Unknown Map flag
    """
    if(unknown):
        if canvas[int(round(next_height))][int(round(next_width))][1]==255 or canvas[int(round(next_height))][int(round(next_width))][0]==255:
            return True
        else:
            return False
    else:
        if canvas[int(round(next_height))][int(round(next_width))][0]==255:
            # print("In obstacle")
            return True
        else:
            return False

def cost_to_goal(node,final):
    """
    @brief: This function computes the euclidean distance between the current node and the final goal node.
    :param node: present node
    :param final: final node (Goal Node)
    """
    return np.sqrt(np.power(node[0]-final[0],2)+np.power(node[1]-final[1],2))

def compute_distance(node1,node2):
    """
    @brief: This function computes the euclidean distance between the two given nodes. Mainly used to compute
    the edge length.
    :param node1: First Node
    :param node2: Second Node
    """
    return np.sqrt(np.power(node1[0]-node2[0],2)+np.power(node1[1]-node2[1],2))

def nearest(sample, TV):
    """
    @brief: This function returns the nearest node from the previously generated vertices.
    :param sample: sampled node
    :param TV: Tree Vertices
    """
    dist = []
    for vertex in TV:
        dist.append(compute_distance(sample,vertex))

    nearest_vertex = TV[dist.index(min(dist))]
    return nearest_vertex

def neighbor_nodes(sample, TV, d=10):
    """
    @brief: This function computes the nearby neighbouring nodes at a particular distance threshold from
    the sample and the tree vertices.
    :param sample: Node from which the neighbouring nodes are to be returned
    :param TV: Tree vertices
    """
    neighbors = []
    for vertex in TV:
        if compute_distance(sample,vertex) < d:
            neighbors.append(vertex)

    return neighbors

def collision_free(X_nearest,X_rand,canvas,unknown=False): #Replace X_rand with X_new for steer function
    """
    @brief: This function samples the edge and checks for the validity of the edge by checking the obstacle space
    :param X_nearest: Nearest Node
    :param X_rand: Random node
    :param canvas: Map
    :unknown: Flag for unknown map
    """
    if X_rand[0] != X_nearest[0]:
        x1 = min(X_nearest[0],X_rand[0])
        if(X_nearest[0] == x1):
            for w in range(X_nearest[0],X_rand[0]+1):
                h = ((X_rand[1] - X_nearest[1])/(X_rand[0] - X_nearest[0]))*(w - X_nearest[0]) + X_nearest[1]
                if(is_obstacle(int(w),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False
        else:
            for w in range(X_rand[0],X_nearest[0]+1):
                h = ((X_nearest[1] - X_rand[1])/(X_nearest[0] - X_rand[0]))*(w - X_rand[0]) + X_rand[1]
                if(is_obstacle(int(w),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False

    else:
        y1 = min(X_nearest[1],X_rand[1])
        if(y1 == X_nearest[1]):
            for h in range(X_nearest[1],X_rand[1]+1):
                if(is_obstacle(int(X_nearest[0]),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False
        else:
            for h in range(X_rand[1],X_nearest[1]+1):
                if(is_obstacle(int(X_rand[0]),int(h),canvas,unknown)):
                    # print("Collision!")
                    return False

    return True

def rewire(node1,node2,node_dict,canvas,final_state):
    """
    @brief: This function rewires the edge between the nodes by checking the edge length and the cost to goal.
    :param node1: Node 1
    :param node2: Node 2
    :param node_dict: Dictionary containing the Parent nodes and costs
    :param canvas: Map
    :param final_state: Goal Node
    """
    # print("In rewire")
    parent = []
    if collision_free(node1,node2, canvas) is True:
        if (compute_distance(node1, node2) + cost_to_goal(node1, final_state)) < cost_to_goal(node2, final_state):
            node_dict[tuple(node2)] = [node1, compute_distance(node1, node2) + cost_to_goal(node1, final_state)]
            parent = node1.copy()
    if len(parent) != 0:
        return parent
    else:
        return node_dict[tuple(node2)][0]

def mod_rrt_star(initial_state,final_state,canvas):
    """
    @brief: This function generates the random tree for the given obstacle map.
    :param initial_state: Start Node
    :param final_state: final node (Goal Node)
    :param canvas: Map
    """
    TV = []
    TE = {}
    TV.append(final_state)
    node_dict = {}
    node_dict[tuple(final_state)] = final_state
    while True:
        width_rand = random.randint(0,canvas.shape[1]-1)
        height_rand = random.randint(0,canvas.shape[0]-1)

        X_rand = [width_rand,height_rand]
        # print("Random sample: ", X_rand)
        X_nearest = nearest(X_rand,TV)

        #Steer function to be implemented later for non-holonomic constraints.
        #X_new <- Steer(X_rand, X_nearest)
        #Here X_rand is X_new
        if(collision_free(X_nearest, X_rand, canvas) is False):
            continue

        X_parent = X_nearest.copy()
        node_dict[tuple(X_rand)] = [X_parent, cost_to_goal(X_nearest,final_state) + compute_distance(X_nearest,X_rand)]

        X_neighbors = neighbor_nodes(X_rand, TV, 10)

        for n in X_neighbors:
            X_parent = rewire(n, X_rand, node_dict, canvas, final_state)

        TV.append(X_rand)
        TE[tuple(X_rand)] = X_parent.copy()
        # print("X_parent", X_parent)

        for n in X_neighbors:
            X_parent_temp = rewire(X_rand, n, node_dict, canvas, final_state)

            if X_parent_temp == X_rand:
                # print("Before Pop", n)
                TE.pop(tuple(n))
                TE[tuple(n)] = X_rand.copy()

        if compute_distance(X_rand,initial_state) < 5:
            print("RRT* Converged!")
            return TE, TV, X_rand

def backtrack(initial_state, final_state, edges, canvas):
    """
    @brief: This function backtracks the path from the goal node to the start node.
    :param initial_state: start node
    :param final_state: final node (Goal Node)\
    :param edges: Edges between the nodes
    """
    state = initial_state.copy()
    path = []
    while True:
        node = edges[tuple(state)]
        path.append(state)
        # cv2.line(canvas, tuple(state), tuple(node), (0, 255, 230), 3)
        if(tuple(node) == tuple(final_state)):
            path.append(final_state)
            print("Back Tracking Done!")
            break
        state = node.copy()
    return path

def path_sampling(path):
    """
    @brief: This function samples the generated path
    :param path: path from start node to the goal node
    """
    sampled_path = []
    for i in range(0,len(path)-1):
        X_rand = path[i]
        X_nearest = path[i+1]
        if X_rand[0] != X_nearest[0]:
            x1 = min(X_nearest[0],X_rand[0])
            if(X_nearest[0] == x1):
                for w in range(X_nearest[0],X_rand[0]+1):
                    h = ((X_rand[1] - X_nearest[1])/(X_rand[0] - X_nearest[0]))*(w - X_nearest[0]) + X_nearest[1]
                    sampled_path.append([int(w),int(h)])
            else:
                for w in range(X_rand[0],X_nearest[0]+1):
                    h = ((X_nearest[1] - X_rand[1])/(X_nearest[0] - X_rand[0]))*(w - X_rand[0]) + X_rand[1]
                    sampled_path.append([int(w),int(h)])

        else:
            print("vertical line", X_nearest[1], X_rand[1])
            y1 = min(X_nearest[1],X_rand[1])
            print("y1 ", y1)
            if(y1 == X_nearest[1]):
                for h in range(X_nearest[1],X_rand[1]+1):
                    sampled_path.append([int(X_nearest[0]),int(h)])
            else:
                for h in range(X_rand[1],X_nearest[1]+1):
                    sampled_path.append([int(X_rand[0]),int(h)])
    return sampled_path

def path_smoothening(sampled_path, final_state, canvas, unknown = False):
    """
    @brief: This function smoothenes the path by connecting the start nodes with the most feasible node starting
    from the goal node
    :param sampled path: Sampled Path
    :param final_state: final node (Goal Node)
    :param canvas: Map
    :param unknown: Flag for dynamic map ( unknown obstacles )
    """
    shortest_path = []
    # if len(sampled_path) > 0:
    shortest_path.append(sampled_path[0])
    print("Length of Sampled Path: ",len(sampled_path))
    while (tuple(shortest_path[-1]) != tuple(sampled_path[-1])):
        # print(sampled_path.index(shortest_path[-1]))
        for i in range(sampled_path.index(shortest_path[-1]),len(sampled_path)):
            if collision_free(shortest_path[-1],sampled_path[len(sampled_path)-1-i+sampled_path.index(shortest_path[-1])], canvas, unknown):
                shortest_path.append(sampled_path[len(sampled_path)-1-i+sampled_path.index(shortest_path[-1])])
                break
    # print(shortest_path)
    return shortest_path

def path_replanning(path, dynamic_map, edges, vertices, initial, final):
    """
    @brief: This function replans the path based on the dynamic obstacles present in the map.
    :param path: actual path to be followed
    :param dynamic_map: Dynamic map
    :param edges: Edges
    :param vertices: Vertices of the tree
    :param initial: starting node
    :param final: Goal Node
    """
    replanned_path = path.copy()
    print("in path replanning")
    for i in range(1,len(path)):
        node = replanned_path[i]
        X_next = node.copy()
        if is_obstacle(node[0],node[1],dynamic_map,True) or (not collision_free(replanned_path[i-1],X_next,dynamic_map, unknown=True)):
            X_curr = replanned_path[i-1].copy()
            X_candi = []
            X_near = neighbor_nodes(X_curr, vertices, d=50)
            for near in X_near:
                if collision_free(X_curr,near,dynamic_map, unknown=True):
                    if near not in replanned_path:
                        X_candi.append(near)

            X_next = pareto(X_candi,X_curr,final)
            # X_next = X_candi[0]
        if(X_next is not None):
            # print("Nearby node found!")
            # X_curr = X_next.copy()
            # print("Previous: ",replanned_path[i])
            replanned_path[i] = X_next.copy()
            # print("Updated: ",replanned_path[i])

        else:
            print("Not Enough Samples found nearby and hence the path goes through the obstacle")

    # print("Replanned Path: ", replanned_path)
    return replanned_path

def pareto(X_candi,initial,final):
    """
    @brief: This function returns the most dominant node by using the pareto dominance theory
    :param X_candi: Candidate Nodes
    :param initial: Initial Node
    :paran final: Final Node (Goal Node)
    """
    paretos = []
    for candidates in X_candi:
        paretos.append([compute_distance(candidates,initial), cost_to_goal(candidates,final)])

    if(len(paretos) != 0):
        pareto_dict = {}
        for i in range(0,len(paretos)):
            dominant_node = paretos[i].copy()
            ID = 0
            OD = 0
            for j in range(0,len(paretos)):
                if(tuple(paretos[i]) == tuple(paretos[j])):
                    continue
                elif ((paretos[j][0]<=dominant_node[0] and paretos[j][1]<=dominant_node[1]) and (paretos[j][0]<dominant_node[0] or paretos[j][1]<dominant_node[1])):
                    ID += 1
                elif (((paretos[j][0]>=dominant_node[0] and paretos[j][1]>=dominant_node[1]) and (paretos[j][0]>dominant_node[0] or paretos[j][1]>dominant_node[1]))):
                    OD += 1
            pareto_dict[tuple(dominant_node)] = [ID, OD]

        pareto_keys = list(pareto_dict.keys())
        pareto_IDs = []
        pareto_ODs = []
        for p_key in pareto_keys:
            pareto_IDs.append(pareto_dict[tuple(p_key)][0])
            pareto_ODs.append(pareto_dict[tuple(p_key)][1])
        zero_ID_index = list(np.where(np.array(pareto_IDs)==0))[0]
        # print("Zero ID Index Type: ",type(zero_ID_index), zero_ID_index)
        if(len(zero_ID_index)>1):
            zero_ID_keys = []
            for i in zero_ID_index:
                zero_ID_keys.append(pareto_keys[i])

            zero_ID_max_OD = []
            for key in zero_ID_keys:
                zero_ID_max_OD.append(pareto_dict[tuple(key)][1])
            max_OD = np.max(zero_ID_max_OD)
            max_OD_key = zero_ID_keys[zero_ID_max_OD.index(max_OD)]

            # print(max_OD_key)
            return X_candi[paretos.index(list(max_OD_key))]
        elif(len(zero_ID_index)==1):
            return X_candi[paretos.index(list(pareto_keys[zero_ID_index[0]]))]
        else:
            print("NO PARETO!")

    else:
        print("No Candidate Nodes")

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('--map1', action='store_true',
                        help="Loads Map 01")
    parser.add_argument('--map2', action='store_true',
                        help="Loads Map 02")
    args = parser.parse_args()

    #Gives the time at which the program has started
    canvas = np.ones((250,400,3),dtype="uint8") #Creating a blank canvas

    if args.map1:
        flag = 1
    elif args.map2:
        flag = 2

    canvas = draw_obstacles(canvas,clr=5,unknown=False,map_flag=flag) #Draw the obstacles in the canvas, default point robot with 5 units of clearance

    initial_state,final_state = [10,10], [350,150] #Take the start and goal node from the user

    #Changing the cartesian coordinates to image coordinates:
    initial_state[1] = canvas.shape[0]-1 - initial_state[1]
    final_state[1] = canvas.shape[0]-1 - final_state[1]

    #Write a condition to check if the initial state and final state are in the obstacle space and exit from program and ask to rerun with valid start and goal positions
    if(canvas[initial_state[1]][initial_state[0]][0]==255 or canvas[final_state[1]][final_state[0]][0]==255):
        print("Given Start or Goal Node is in the Obstacle Region. Please re-run with Valid Coordinates")
        exit()

    start_time = time.time()
    #Start Node and Goal Node
    cv2.circle(canvas,(int(initial_state[0]),int(initial_state[1])),5,(0,255,0),-1)
    cv2.circle(canvas,(int(final_state[0]),int(final_state[1])),5,(0,0,255),-1)

    #Generate the graph from the Modified RRT* Algorithm
    edges, vertices, s_node = mod_rrt_star(initial_state,final_state,canvas) #Compute the path using A Star Algorithm

    tree_canvas = canvas.copy()
    #Draw the edges
    for key in edges:
        # cv2.line(canvas, tuple(key), tuple(edges[key]), (255, 128, 223), 2)
        cv2.line(tree_canvas, tuple(key), tuple(edges[key]), (0, 0, 255), 3)
        cv2.circle(tree_canvas,(key[0], key[1]), 1, (0,255,255), -1)
        cv2.circle(tree_canvas,(edges[key][0],edges[key][1]), 1, (0,255,0), -1)
    # cv2.imshow("Modified RRT* Tree Expansion", canvas)

    #Generate a dynamic map
    dynamic_map = np.ones((250,400,3),dtype="uint8") #Creating a blank canvas
    dynamic_map = draw_obstacles(dynamic_map,clr=5,unknown=True, map_flag=flag) #Draw the obstacles in the canvas, default point robot with 5 units of clearance
    cv2.imshow("Known Map with Unknown Obstacles", dynamic_map)

    #Backtack the path to reach from start node to goal node
    path = backtrack(s_node, final_state, edges, canvas)
    rrt_path_canvas = tree_canvas.copy()
    for i in range(1,len(path)):
        cv2.line(rrt_path_canvas, tuple(path[i-1]), tuple(path[i]), (0, 255, 0), 3)
    # cv2.imshow("Modified RRT* Path", rrt_path_canvas)

    #Sample and Smoothen the path from the list returned from the backtracking function.
    sampled_path = path_sampling(path)

    smoothened_path = path_smoothening(sampled_path.copy(),final_state,canvas,unknown = False)

    smooth_rrt_path_canvas = rrt_path_canvas.copy()
    for i in range(1,len(smoothened_path)):
        cv2.line(smooth_rrt_path_canvas, tuple(smoothened_path[i-1]), tuple(smoothened_path[i]), (255, 255, 255), 3)
    # cv2.imshow("Smoothened Modified RRT* Path", smooth_rrt_path_canvas)

    #Resample the smoothened path
    sampled_path = path_sampling(smoothened_path)

    #Replan the path from the dynamic obstacles
    replanned_path = path_replanning(sampled_path, dynamic_map, edges, vertices,s_node,final_state)

    replanned_path_canvas = dynamic_map.copy()
    for i in range(0,len(replanned_path)):
        cv2.circle(replanned_path_canvas,tuple(replanned_path[i]), 2, (0,145,145), -1)

    n_path  = []
    prev_path = []
    for i in range(0,len(replanned_path)):
        if(tuple(sampled_path[i]) == tuple(replanned_path[i])):

            prev_path.append(sampled_path[i])
            continue
        else:
            # print("N Path Append")
            n_path.append(sampled_path[i-2])
            n_path.append(sampled_path[i-1])
            for j in range(i,len(replanned_path)):
                n_path.append(replanned_path[j])
            break

    # cv2.imshow("Replanned Modified RRT* Path", replanned_path_canvas)

    # replanned_sampled = path_sampling(replanned_path)
    # print("New path ",n_path)
    new_replanned_path = path_smoothening(n_path.copy(), final_state, dynamic_map, unknown=True)

    smooth_replanned_path_canvas = replanned_path_canvas.copy()
    for i in range(1,len(sampled_path)):
        cv2.line(dynamic_map, tuple(sampled_path[i-1]), tuple(sampled_path[i]), (0, 137, 255), 3)

    # print(replanned_path)
    for i in range(1,len(prev_path)):
        cv2.line(smooth_replanned_path_canvas, tuple(prev_path[i-1]), tuple(prev_path[i]), (255, 128, 223), 2)
    for i in range(1,len(new_replanned_path)):
        cv2.line(smooth_replanned_path_canvas, tuple(new_replanned_path[i-1]), tuple(new_replanned_path[i]), (255, 128, 223), 2)
        # cv2.circle(dynamic_map,tuple(replanned_path[i]), 3, (255,255,255), -1)
    # cv2.imshow("Smoothened Replanned Modified RRT* Path",dynamic_map)

    end_time = time.time() #Time taken to run the whole algorithm to find the optimal path

    cv2.imshow("Known Map with Initial & Final Nodes", canvas)
    cv2.imshow("Modified RRT* Tree Expansion", tree_canvas)
    # cv2.imshow("Known Map with Unknown Obstacles", dynamic_map)
    cv2.imshow("Modified RRT* Path", rrt_path_canvas)
    cv2.imshow("Smoothened Modified RRT* Path", smooth_rrt_path_canvas)
    # cv2.imshow("Replanned Pareto Dominant Nodes", replanned_path_canvas)
    cv2.imshow("Smoothened Replanned Modified RRT* Path", smooth_replanned_path_canvas)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # print("Code Execution Time: ",end_time-start_time) #Prints the total execution time