# A test suite for pdb; not very comprehensive at the moment.

import doctest
import os
import pdb
import sys
import types
import unittest
import subprocess
import textwrap

from test import support
# This little helper class is essential for testing pdb under doctest.
from test.test_doctest import _FakeInput

class PdbTestInput(object):
    """Context manager that makes testing Pdb in doctests easier."""

    def __init__(self, input):
        self.input = input

    def __enter__(self):
        self.real_stdin = sys.stdin
        sys.stdin = _FakeInput(self.input)
        self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None

    def __exit__(self, *exc):
        sys.stdin = self.real_stdin
        if self.orig_trace:
            sys.settrace(self.orig_trace)

def test_pdb_displayhook():
    """This tests the custom displayhook for pdb.

    >>> def test_function(foo, bar):
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     pass

    >>> with PdbTestInput([
    ...     'foo',
    ...     'bar',
    ...     'for i in range(5): print(i)',
    ...     'continue',
    ... ]):
    ...     test_function(1, None)
    > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function()
    -> pass
    (Pdb) foo
    1
    (Pdb) bar
    (Pdb) for i in range(5): print(i)
    0
    1
    2
    3
    4
    (Pdb) continue
    """

def test_pdb_basic_commands():
    """Test the basic commands of pdb.

    >>> def test_function_2(foo, bar='default'):
    ...     print(foo)
    ...     for i in range(5):
    ...         print(i)
    ...     print(bar)
    ...     for i in range(10):
    ...         never_executed
    ...     print('after for')
    ...     print('...')
    ...     return foo.upper()

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     ret = test_function_2('baz')
    ...     print(ret)

    >>> with PdbTestInput([  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ...     'step',       # entering the function call
    ...     'args',       # display function args
    ...     'list',       # list function source
    ...     'bt',         # display backtrace
    ...     'up',         # step up to test_function()
    ...     'down',       # step down to test_function_2() again
    ...     'next',       # stepping to print(foo)
    ...     'next',       # stepping to the for loop
    ...     'step',       # stepping into the for loop
    ...     'until',      # continuing until out of the for loop
    ...     'next',       # executing the print(bar)
    ...     'jump 8',     # jump over second for loop
    ...     'return',     # return out of function
    ...     'retval',     # display return value
    ...     'continue',
    ... ]):
    ...    test_function()
    > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
    -> ret = test_function_2('baz')
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
    -> def test_function_2(foo, bar='default'):
    (Pdb) args
    foo = 'baz'
    bar = 'default'
    (Pdb) list
      1  ->     def test_function_2(foo, bar='default'):
      2             print(foo)
      3             for i in range(5):
      4                 print(i)
      5             print(bar)
      6             for i in range(10):
      7                 never_executed
      8             print('after for')
      9             print('...')
     10             return foo.upper()
    [EOF]
    (Pdb) bt
    ...
      <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>()
    -> test_function()
      <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
    -> ret = test_function_2('baz')
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
    -> def test_function_2(foo, bar='default'):
    (Pdb) up
    > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function()
    -> ret = test_function_2('baz')
    (Pdb) down
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2()
    -> def test_function_2(foo, bar='default'):
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2()
    -> print(foo)
    (Pdb) next
    baz
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2()
    -> for i in range(5):
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2()
    -> print(i)
    (Pdb) until
    0
    1
    2
    3
    4
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2()
    -> print(bar)
    (Pdb) next
    default
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2()
    -> for i in range(10):
    (Pdb) jump 8
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2()
    -> print('after for')
    (Pdb) return
    after for
    ...
    --Return--
    > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ'
    -> return foo.upper()
    (Pdb) retval
    'BAZ'
    (Pdb) continue
    BAZ
    """

def test_pdb_breakpoint_commands():
    """Test basic commands related to breakpoints.

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     print(1)
    ...     print(2)
    ...     print(3)
    ...     print(4)

    First, need to clear bdb state that might be left over from previous tests.
    Otherwise, the new breakpoints might get assigned different numbers.

    >>> from bdb import Breakpoint
    >>> Breakpoint.next = 1
    >>> Breakpoint.bplist = {}
    >>> Breakpoint.bpbynumber = [None]

    Now test the breakpoint commands.  NORMALIZE_WHITESPACE is needed because
    the breakpoint list outputs a tab for the "stop only" and "ignore next"
    lines, which we don't want to put in here.

    >>> with PdbTestInput([  # doctest: +NORMALIZE_WHITESPACE
    ...     'break 3',
    ...     'disable 1',
    ...     'ignore 1 10',
    ...     'condition 1 1 < 2',
    ...     'break 4',
    ...     'break 4',
    ...     'break',
    ...     'clear 3',
    ...     'break',
    ...     'condition 1',
    ...     'enable 1',
    ...     'clear 1',
    ...     'commands 2',
    ...     'p "42"',
    ...     'print("42", 7*6)',     # Issue 18764 (not about breakpoints)
    ...     'end',
    ...     'continue',  # will stop at breakpoint 2 (line 4)
    ...     'clear',     # clear all!
    ...     'y',
    ...     'tbreak 5',
    ...     'continue',  # will stop at temporary breakpoint
    ...     'break',     # make sure breakpoint is gone
    ...     'continue',
    ... ]):
    ...    test_function()
    > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function()
    -> print(1)
    (Pdb) break 3
    Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
    (Pdb) disable 1
    Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
    (Pdb) ignore 1 10
    Will ignore next 10 crossings of breakpoint 1.
    (Pdb) condition 1 1 < 2
    New condition set for breakpoint 1.
    (Pdb) break 4
    Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) break 4
    Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) break
    Num Type         Disp Enb   Where
    1   breakpoint   keep no    at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
            stop only if 1 < 2
            ignore next 10 hits
    2   breakpoint   keep yes   at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    3   breakpoint   keep yes   at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) clear 3
    Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) break
    Num Type         Disp Enb   Where
    1   breakpoint   keep no    at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
            stop only if 1 < 2
            ignore next 10 hits
    2   breakpoint   keep yes   at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) condition 1
    Breakpoint 1 is now unconditional.
    (Pdb) enable 1
    Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
    (Pdb) clear 1
    Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3
    (Pdb) commands 2
    (com) p "42"
    (com) print("42", 7*6)
    (com) end
    (Pdb) continue
    1
    '42'
    42 42
    > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function()
    -> print(2)
    (Pdb) clear
    Clear all breaks? y
    Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4
    (Pdb) tbreak 5
    Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
    (Pdb) continue
    2
    Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5
    > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function()
    -> print(3)
    (Pdb) break
    (Pdb) continue
    3
    4
    """

def do_nothing():
    pass

def do_something():
    print(42)

def test_list_commands():
    """Test the list and source commands of pdb.

    >>> def test_function_2(foo):
    ...     import test.test_pdb
    ...     test.test_pdb.do_nothing()
    ...     'some...'
    ...     'more...'
    ...     'code...'
    ...     'to...'
    ...     'make...'
    ...     'a...'
    ...     'long...'
    ...     'listing...'
    ...     'useful...'
    ...     '...'
    ...     '...'
    ...     return foo

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     ret = test_function_2('baz')

    >>> with PdbTestInput([  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ...     'list',      # list first function
    ...     'step',      # step into second function
    ...     'list',      # list second function
    ...     'list',      # continue listing to EOF
    ...     'list 1,3',  # list specific lines
    ...     'list x',    # invalid argument
    ...     'next',      # step to import
    ...     'next',      # step over import
    ...     'step',      # step into do_nothing
    ...     'longlist',  # list all lines
    ...     'source do_something',  # list all lines of function
    ...     'source fooxxx',        # something that doesn't exit
    ...     'continue',
    ... ]):
    ...    test_function()
    > <doctest test.test_pdb.test_list_commands[1]>(3)test_function()
    -> ret = test_function_2('baz')
    (Pdb) list
      1         def test_function():
      2             import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
      3  ->         ret = test_function_2('baz')
    [EOF]
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2()
    -> def test_function_2(foo):
    (Pdb) list
      1  ->     def test_function_2(foo):
      2             import test.test_pdb
      3             test.test_pdb.do_nothing()
      4             'some...'
      5             'more...'
      6             'code...'
      7             'to...'
      8             'make...'
      9             'a...'
     10             'long...'
     11             'listing...'
    (Pdb) list
     12             'useful...'
     13             '...'
     14             '...'
     15             return foo
    [EOF]
    (Pdb) list 1,3
      1  ->     def test_function_2(foo):
      2             import test.test_pdb
      3             test.test_pdb.do_nothing()
    (Pdb) list x
    *** ...
    (Pdb) next
    > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2()
    -> import test.test_pdb
    (Pdb) next
    > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2()
    -> test.test_pdb.do_nothing()
    (Pdb) step
    --Call--
    > ...test_pdb.py(...)do_nothing()
    -> def do_nothing():
    (Pdb) longlist
    ...  ->     def do_nothing():
    ...             pass
    (Pdb) source do_something
    ...         def do_something():
    ...             print(42)
    (Pdb) source fooxxx
    *** ...
    (Pdb) continue
    """

def test_post_mortem():
    """Test post mortem traceback debugging.

    >>> def test_function_2():
    ...     try:
    ...         1/0
    ...     finally:
    ...         print('Exception!')

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     test_function_2()
    ...     print('Not reached.')

    >>> with PdbTestInput([  # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ...     'next',      # step over exception-raising call
    ...     'bt',        # get a backtrace
    ...     'list',      # list code of test_function()
    ...     'down',      # step into test_function_2()
    ...     'list',      # list code of test_function_2()
    ...     'continue',
    ... ]):
    ...    try:
    ...        test_function()
    ...    except ZeroDivisionError:
    ...        print('Correctly reraised.')
    > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
    -> test_function_2()
    (Pdb) next
    Exception!
    ZeroDivisionError: division by zero
    > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
    -> test_function_2()
    (Pdb) bt
    ...
      <doctest test.test_pdb.test_post_mortem[2]>(10)<module>()
    -> test_function()
    > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function()
    -> test_function_2()
      <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
    -> 1/0
    (Pdb) list
      1         def test_function():
      2             import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
      3  ->         test_function_2()
      4             print('Not reached.')
    [EOF]
    (Pdb) down
    > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2()
    -> 1/0
    (Pdb) list
      1         def test_function_2():
      2             try:
      3  >>             1/0
      4             finally:
      5  ->             print('Exception!')
    [EOF]
    (Pdb) continue
    Correctly reraised.
    """

def test_pdb_skip_modules():
    """This illustrates the simple case of module skipping.

    >>> def skip_module():
    ...     import string
    ...     import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace()
    ...     string.capwords('FOO')

    >>> with PdbTestInput([
    ...     'step',
    ...     'continue',
    ... ]):
    ...     skip_module()
    > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()
    -> string.capwords('FOO')
    (Pdb) step
    --Return--
    > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None
    -> string.capwords('FOO')
    (Pdb) continue
    """

# Module for testing skipping of module that makes a callback
mod = types.ModuleType('module_to_skip')
exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)

def test_pdb_skip_modules_with_callback():
    """This illustrates skipping of modules that call into other code.

    >>> def skip_module():
    ...     def callback():
    ...         return None
    ...     import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace()
    ...     mod.foo_pony(callback)

    >>> with PdbTestInput([
    ...     'step',
    ...     'step',
    ...     'step',
    ...     'step',
    ...     'step',
    ...     'continue',
    ... ]):
    ...     skip_module()
    ...     pass  # provides something to "step" to
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()
    -> mod.foo_pony(callback)
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback()
    -> def callback():
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()
    -> return None
    (Pdb) step
    --Return--
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None
    -> return None
    (Pdb) step
    --Return--
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None
    -> mod.foo_pony(callback)
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>()
    -> pass  # provides something to "step" to
    (Pdb) continue
    """

def test_pdb_continue_in_bottomframe():
    """Test that "continue" and "next" work properly in bottom frame (issue #5294).

    >>> def test_function():
    ...     import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False)
    ...     inst.set_trace()
    ...     inst.botframe = sys._getframe()  # hackery to get the right botframe
    ...     print(1)
    ...     print(2)
    ...     print(3)
    ...     print(4)

    >>> with PdbTestInput([  # doctest: +ELLIPSIS
    ...     'next',
    ...     'break 7',
    ...     'continue',
    ...     'next',
    ...     'continue',
    ...     'continue',
    ... ]):
    ...    test_function()
    > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function()
    -> inst.botframe = sys._getframe()  # hackery to get the right botframe
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function()
    -> print(1)
    (Pdb) break 7
    Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7
    (Pdb) continue
    1
    2
    > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function()
    -> print(3)
    (Pdb) next
    3
    > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function()
    -> print(4)
    (Pdb) continue
    4
    """

def pdb_invoke(method, arg):
    """Run pdb.method(arg)."""
    getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg)

def test_pdb_run_with_incorrect_argument():
    """Testing run and runeval with incorrect first argument.

    >>> pti = PdbTestInput(['continue',])
    >>> with pti:
    ...     pdb_invoke('run', lambda x: x)
    Traceback (most recent call last):
    TypeError: exec() arg 1 must be a string, bytes or code object

    >>> with pti:
    ...     pdb_invoke('runeval', lambda x: x)
    Traceback (most recent call last):
    TypeError: eval() arg 1 must be a string, bytes or code object
    """

def test_pdb_run_with_code_object():
    """Testing run and runeval with code object as a first argument.

    >>> with PdbTestInput(['step','x', 'continue']):  # doctest: +ELLIPSIS
    ...     pdb_invoke('run', compile('x=1', '<string>', 'exec'))
    > <string>(1)<module>()...
    (Pdb) step
    --Return--
    > <string>(1)<module>()->None
    (Pdb) x
    1
    (Pdb) continue

    >>> with PdbTestInput(['x', 'continue']):
    ...     x=0
    ...     pdb_invoke('runeval', compile('x+1', '<string>', 'eval'))
    > <string>(1)<module>()->None
    (Pdb) x
    1
    (Pdb) continue
    """

def test_next_until_return_at_return_event():
    """Test that pdb stops after a next/until/return issued at a return debug event.

    >>> def test_function_2():
    ...     x = 1
    ...     x = 2

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     test_function_2()
    ...     test_function_2()
    ...     test_function_2()
    ...     end = 1

    >>> from bdb import Breakpoint
    >>> Breakpoint.next = 1
    >>> with PdbTestInput(['break test_function_2',
    ...                    'continue',
    ...                    'return',
    ...                    'next',
    ...                    'continue',
    ...                    'return',
    ...                    'until',
    ...                    'continue',
    ...                    'return',
    ...                    'return',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function()
    -> test_function_2()
    (Pdb) break test_function_2
    Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1
    (Pdb) continue
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
    -> x = 1
    (Pdb) return
    --Return--
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
    -> x = 2
    (Pdb) next
    > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function()
    -> test_function_2()
    (Pdb) continue
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
    -> x = 1
    (Pdb) return
    --Return--
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
    -> x = 2
    (Pdb) until
    > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function()
    -> test_function_2()
    (Pdb) continue
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2()
    -> x = 1
    (Pdb) return
    --Return--
    > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None
    -> x = 2
    (Pdb) return
    > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function()
    -> end = 1
    (Pdb) continue
    """

def test_pdb_next_command_for_generator():
    """Testing skip unwindng stack on yield for generators for "next" command

    >>> def test_gen():
    ...     yield 0
    ...     return 1
    ...     yield 2

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     it = test_gen()
    ...     try:
    ...         if next(it) != 0:
    ...             raise AssertionError
    ...         next(it)
    ...     except StopIteration as ex:
    ...         if ex.value != 1:
    ...             raise AssertionError
    ...     print("finished")

    >>> with PdbTestInput(['step',
    ...                    'step',
    ...                    'step',
    ...                    'next',
    ...                    'next',
    ...                    'step',
    ...                    'step',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function()
    -> it = test_gen()
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function()
    -> try:
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function()
    -> if next(it) != 0:
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen()
    -> def test_gen():
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen()
    -> yield 0
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()
    -> return 1
    (Pdb) step
    --Return--
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1
    -> return 1
    (Pdb) step
    StopIteration: 1
    > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function()
    -> next(it)
    (Pdb) continue
    finished
    """

if False:
    def test_pdb_next_command_for_coroutine():
        """Testing skip unwindng stack on yield for coroutines for "next" command

        >>> import asyncio

        >>> async def test_coro():
        ...     await asyncio.sleep(0)
        ...     await asyncio.sleep(0)
        ...     await asyncio.sleep(0)

        >>> async def test_main():
        ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
        ...     await test_coro()

        >>> def test_function():
        ...     loop = asyncio.new_event_loop()
        ...     loop.run_until_complete(test_main())
        ...     loop.close()
        ...     print("finished")

        >>> with PdbTestInput(['step',
        ...                    'step',
        ...                    'next',
        ...                    'next',
        ...                    'next',
        ...                    'step',
        ...                    'continue']):
        ...     test_function()
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
        -> await test_coro()
        (Pdb) step
        --Call--
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro()
        -> async def test_coro():
        (Pdb) step
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro()
        -> await asyncio.sleep(0)
        (Pdb) next
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro()
        -> await asyncio.sleep(0)
        (Pdb) next
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro()
        -> await asyncio.sleep(0)
        (Pdb) next
        Internal StopIteration
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()
        -> await test_coro()
        (Pdb) step
        --Return--
        > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None
        -> await test_coro()
        (Pdb) continue
        finished
        """

    def test_pdb_next_command_for_asyncgen():
        """Testing skip unwindng stack on yield for coroutines for "next" command

        >>> import asyncio

        >>> async def agen():
        ...     yield 1
        ...     await asyncio.sleep(0)
        ...     yield 2

        >>> async def test_coro():
        ...     async for x in agen():
        ...         print(x)

        >>> async def test_main():
        ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
        ...     await test_coro()

        >>> def test_function():
        ...     loop = asyncio.new_event_loop()
        ...     loop.run_until_complete(test_main())
        ...     loop.close()
        ...     print("finished")

        >>> with PdbTestInput(['step',
        ...                    'step',
        ...                    'next',
        ...                    'next',
        ...                    'step',
        ...                    'next',
        ...                    'continue']):
        ...     test_function()
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main()
        -> await test_coro()
        (Pdb) step
        --Call--
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro()
        -> async def test_coro():
        (Pdb) step
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
        -> async for x in agen():
        (Pdb) next
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro()
        -> print(x)
        (Pdb) next
        1
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro()
        -> async for x in agen():
        (Pdb) step
        --Call--
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen()
        -> yield 1
        (Pdb) next
        > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen()
        -> await asyncio.sleep(0)
        (Pdb) continue
        2
        finished
        """

    def test_pdb_return_command_for_coroutine():
        """Testing no unwindng stack on yield for coroutines for "return" command

    >>> import asyncio

    >>> async def test_coro():
    ...     await asyncio.sleep(0)
    ...     await asyncio.sleep(0)
    ...     await asyncio.sleep(0)

    >>> async def test_main():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     await test_coro()

    >>> def test_function():
    ...     loop = asyncio.new_event_loop()
    ...     loop.run_until_complete(test_main())
    ...     loop.close()
    ...     print("finished")

    >>> with PdbTestInput(['step',
    ...                    'step',
    ...                    'next',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main()
    -> await test_coro()
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro()
    -> async def test_coro():
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro()
    -> await asyncio.sleep(0)
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro()
    -> await asyncio.sleep(0)
    (Pdb) continue
    finished
    """

    def test_pdb_until_command_for_coroutine():
        """Testing no unwindng stack for coroutines
           for "until" command if target breakpoing is not reached

        >>> import asyncio

        >>> async def test_coro():
        ...     print(0)
        ...     await asyncio.sleep(0)
        ...     print(1)
        ...     await asyncio.sleep(0)
        ...     print(2)
        ...     await asyncio.sleep(0)
        ...     print(3)

        >>> async def test_main():
        ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
        ...     await test_coro()

        >>> def test_function():
        ...     loop = asyncio.new_event_loop()
        ...     loop.run_until_complete(test_main())
        ...     loop.close()
        ...     print("finished")

        >>> with PdbTestInput(['step',
        ...                    'until 8',
        ...                    'continue']):
        ...     test_function()
        > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main()
        -> await test_coro()
        (Pdb) step
        --Call--
        > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro()
        -> async def test_coro():
        (Pdb) until 8
        0
        1
        2
        > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro()
        -> print(3)
        (Pdb) continue
        3
        finished
        """

def test_pdb_return_command_for_generator():
    """Testing no unwindng stack on yield for generators
       for "return" command

    >>> def test_gen():
    ...     yield 0
    ...     return 1
    ...     yield 2

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     it = test_gen()
    ...     try:
    ...         if next(it) != 0:
    ...             raise AssertionError
    ...         next(it)
    ...     except StopIteration as ex:
    ...         if ex.value != 1:
    ...             raise AssertionError
    ...     print("finished")

    >>> with PdbTestInput(['step',
    ...                    'step',
    ...                    'step',
    ...                    'return',
    ...                    'step',
    ...                    'step',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function()
    -> it = test_gen()
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function()
    -> try:
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function()
    -> if next(it) != 0:
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen()
    -> def test_gen():
    (Pdb) return
    StopIteration: 1
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function()
    -> next(it)
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function()
    -> except StopIteration as ex:
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function()
    -> if ex.value != 1:
    (Pdb) continue
    finished
    """

def test_pdb_until_command_for_generator():
    """Testing no unwindng stack on yield for generators
       for "until" command if target breakpoing is not reached

    >>> def test_gen():
    ...     yield 0
    ...     yield 1
    ...     yield 2

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     for i in test_gen():
    ...         print(i)
    ...     print("finished")

    >>> with PdbTestInput(['step',
    ...                    'until 4',
    ...                    'step',
    ...                    'step',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function()
    -> for i in test_gen():
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen()
    -> def test_gen():
    (Pdb) until 4
    0
    1
    > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()
    -> yield 2
    (Pdb) step
    --Return--
    > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2
    -> yield 2
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function()
    -> print(i)
    (Pdb) continue
    2
    finished
    """

def test_pdb_next_command_in_generator_for_loop():
    """The next command on returning from a generator controlled by a for loop.

    >>> def test_gen():
    ...     yield 0
    ...     return 1

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     for i in test_gen():
    ...         print('value', i)
    ...     x = 123

    >>> with PdbTestInput(['break test_gen',
    ...                    'continue',
    ...                    'next',
    ...                    'next',
    ...                    'next',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
    -> for i in test_gen():
    (Pdb) break test_gen
    Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1
    (Pdb) continue
    > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen()
    -> yield 0
    (Pdb) next
    value 0
    > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen()
    -> return 1
    (Pdb) next
    Internal StopIteration: 1
    > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function()
    -> for i in test_gen():
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function()
    -> x = 123
    (Pdb) continue
    """

def test_pdb_next_command_subiterator():
    """The next command in a generator with a subiterator.

    >>> def test_subgenerator():
    ...     yield 0
    ...     return 1

    >>> def test_gen():
    ...     x = yield from test_subgenerator()
    ...     return x

    >>> def test_function():
    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
    ...     for i in test_gen():
    ...         print('value', i)
    ...     x = 123

    >>> with PdbTestInput(['step',
    ...                    'step',
    ...                    'next',
    ...                    'next',
    ...                    'next',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
    -> for i in test_gen():
    (Pdb) step
    --Call--
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen()
    -> def test_gen():
    (Pdb) step
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen()
    -> x = yield from test_subgenerator()
    (Pdb) next
    value 0
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen()
    -> return x
    (Pdb) next
    Internal StopIteration: 1
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function()
    -> for i in test_gen():
    (Pdb) next
    > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function()
    -> x = 123
    (Pdb) continue
    """

def test_pdb_issue_20766():
    """Test for reference leaks when the SIGINT handler is set.

    >>> def test_function():
    ...     i = 1
    ...     while i <= 2:
    ...         sess = pdb.Pdb()
    ...         sess.set_trace(sys._getframe())
    ...         print('pdb %d: %s' % (i, sess._previous_sigint_handler))
    ...         i += 1

    >>> with PdbTestInput(['continue',
    ...                    'continue']):
    ...     test_function()
    > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
    -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
    (Pdb) continue
    pdb 1: <built-in function default_int_handler>
    > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
    -> sess.set_trace(sys._getframe())
    (Pdb) continue
    pdb 2: <built-in function default_int_handler>
    """

class PdbTestCase(unittest.TestCase):

    def run_pdb(self, script, commands):
        """Run 'script' lines with pdb and the pdb 'commands'."""
        filename = 'main.py'
        with open(filename, 'w') as f:
            f.write(textwrap.dedent(script))
        self.addCleanup(support.unlink, filename)
        self.addCleanup(support.rmtree, '__pycache__')
        cmd = [sys.executable, '-m', 'pdb', filename]
        stdout = stderr = None
        with subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                   stdin=subprocess.PIPE,
                                   stderr=subprocess.STDOUT,
                                   ) as proc:
            stdout, stderr = proc.communicate(str.encode(commands))
        stdout = stdout and bytes.decode(stdout)
        stderr = stderr and bytes.decode(stderr)
        return stdout, stderr

    def _assert_find_function(self, file_content, func_name, expected):
        file_content = textwrap.dedent(file_content)

        with open(support.TESTFN, 'w') as f:
            f.write(file_content)

        expected = None if not expected else (
            expected[0], support.TESTFN, expected[1])
        self.assertEqual(
            expected, pdb.find_function(func_name, support.TESTFN))

    def test_find_function_empty_file(self):
        self._assert_find_function('', 'foo', None)

    def test_find_function_found(self):
        self._assert_find_function(
            """\
            def foo():
                pass

            def bar():
                pass

            def quux():
                pass
            """,
            'bar',
            ('bar', 4),
        )

    def test_issue7964(self):
        # open the file as binary so we can force \r\n newline
        with open(support.TESTFN, 'wb') as f:
            f.write(b'print("testing my pdb")\r\n')
        cmd = [sys.executable, '-m', 'pdb', support.TESTFN]
        proc = subprocess.Popen(cmd,
            stdout=subprocess.PIPE,
            stdin=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            )
        self.addCleanup(proc.stdout.close)
        stdout, stderr = proc.communicate(b'quit\n')
        self.assertNotIn(b'SyntaxError', stdout,
                         "Got a syntax error running test script under PDB")

    def test_issue13183(self):
        script = """
            from bar import bar

            def foo():
                bar()

            def nope():
                pass

            def foobar():
                foo()
                nope()

            foobar()
        """
        commands = """
            from bar import bar
            break bar
            continue
            step
            step
            quit
        """
        bar = """
            def bar():
                pass
        """
        with open('bar.py', 'w') as f:
            f.write(textwrap.dedent(bar))
        self.addCleanup(support.unlink, 'bar.py')
        stdout, stderr = self.run_pdb(script, commands)
        self.assertTrue(
            any('main.py(5)foo()->None' in l for l in stdout.splitlines()),
            'Fail to step into the caller after a return')

    def test_issue13210(self):
        # invoking "continue" on a non-main thread triggered an exception
        # inside signal.signal

        # raises SkipTest if python was built without threads
        support.import_module('threading')

        with open(support.TESTFN, 'wb') as f:
            f.write(textwrap.dedent("""
                import threading
                import pdb

                def start_pdb():
                    pdb.Pdb(readrc=False).set_trace()
                    x = 1
                    y = 1

                t = threading.Thread(target=start_pdb)
                t.start()""").encode('ascii'))
        cmd = [sys.executable, '-u', support.TESTFN]
        proc = subprocess.Popen(cmd,
            stdout=subprocess.PIPE,
            stdin=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            )
        self.addCleanup(proc.stdout.close)
        stdout, stderr = proc.communicate(b'cont\n')
        self.assertNotIn('Error', stdout.decode(),
                         "Got an error running test script under PDB")

    def test_issue16180(self):
        # A syntax error in the debuggee.
        script = "def f: pass\n"
        commands = ''
        expected = "SyntaxError:"
        stdout, stderr = self.run_pdb(script, commands)
        self.assertIn(expected, stdout,
            '\n\nExpected:\n{}\nGot:\n{}\n'
            'Fail to handle a syntax error in the debuggee.'
            .format(expected, stdout))

    def test_readrc_kwarg(self):
        script = textwrap.dedent("""
            import pdb; pdb.Pdb(readrc=False).set_trace()

            print('hello')
        """)

        save_home = os.environ.pop('HOME', None)
        try:
            with support.temp_cwd():
                with open('.pdbrc', 'w') as f:
                    f.write("invalid\n")

                with open('main.py', 'w') as f:
                    f.write(script)

                cmd = [sys.executable, 'main.py']
                proc = subprocess.Popen(
                    cmd,
                    stdout=subprocess.PIPE,
                    stdin=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
                with proc:
                    stdout, stderr = proc.communicate(b'q\n')
                    self.assertNotIn("NameError: name 'invalid' is not defined",
                                  stdout.decode())

        finally:
            if save_home is not None:
                os.environ['HOME'] = save_home

    def tearDown(self):
        support.unlink(support.TESTFN)

def load_tests(*args):
    from test import test_pdb
    suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)]
    return unittest.TestSuite(suites)

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


# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from mlxtend.data import loadlocal_mnist

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

# Load data using library
fashion_mnist = keras.datasets.fashion_mnist
(train_images2, train_labels2), (test_images2, test_labels2) = fashion_mnist.load_data()

print('Dimensions train_images2: %s x %s' % (train_images2.shape[0], train_images2.shape[1]))
print('Dimensions train_labels2: %s' % (train_labels2.shape[0]))

train_images, train_labels = loadlocal_mnist(
        images_path='C:\\Hiep-working\\NeuralNetwork\\Fashion_MNIST\\data\\train-images-idx3-ubyte',
        labels_path='C:\\Hiep-working\\NeuralNetwork\\Fashion_MNIST\\data\\train-labels-idx1-ubyte')

test_images, test_labels = loadlocal_mnist(
        images_path='C:\\Hiep-working\\NeuralNetwork\\Fashion_MNIST\\data\\t10k-images-idx3-ubyte',
        labels_path='C:\\Hiep-working\\NeuralNetwork\\Fashion_MNIST\\data\\t10k-labels-idx1-ubyte')

print('Dimensions train_images: %s x %s' % (train_images.shape[0], train_images.shape[1]))
print('Dimensions train_labels: %s' % (train_labels.shape[0]))

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# print("train_images.shape=", train_images.shape)
# print("len(train_labels)=", len(train_labels))
# print(train_labels)
# print(test_images.shape)
# print(len(test_labels))

# plt.figure()
# plt.imshow(train_images[0])
# plt.colorbar()
# plt.grid(False)
# plt.show()

train_images = train_images / 255.0
test_images = test_images / 255.0

# plt.figure(figsize=(10,10))
# for i in range(25):
#     plt.subplot(5,5,i+1)
#     plt.xticks([])
#     plt.yticks([])
#     plt.grid(False)
#     plt.imshow(train_images[i], cmap=plt.cm.binary)
#     plt.xlabel(class_names[train_labels[i]])
# plt.show()

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
print("Training...")
model.fit(train_images, train_labels, epochs=5)
print("Training...Done")
test_loss, test_acc = model.evaluate(test_images, test_labels)

print('\nTest accuracy:', test_acc)

predictions = model.predict(test_images)
print(predictions[0])
print(np.argmax(predictions[0]))
print(test_labels[0])

def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array[i], true_label[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)

# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, test_labels)

plt.show()
# Grab an image from the test dataset
img = test_images[0]

print(img.shape)


"""
python gefsv12_ensemble_probability_weighted.py cyyyymmddhh clead cthresh
"""

import os, sys
from datetime import datetime
import numpy as np
import numpy.ma as ma
import _pickle as cPickle
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from matplotlib import rcParams
from mpl_toolkits.basemap import Basemap, interp
from mpl_toolkits.axes_grid1 import make_axes_locatable
import _pickle as cPickle
import scipy.stats as stats

rcParams['xtick.labelsize']='medium'
rcParams['ytick.labelsize']='medium'
rcParams['legend.fontsize']='large'

# =====================================================================
# =====================================================================

# ---- inputs from command line

cyyyymmddhh = sys.argv[1] #
clead = sys.argv[2] # 018 not 18
cthresh = sys.argv[3]  #0.254, 1.0,  5.0, 10.0, 25.0
jnd = 1350
ind = 1110

cyear = cyyyymmddhh[0:4]
rthresh = float(cthresh)
cmonth = cyyyymmddhh[4:6]
imonth = int(cmonth)-1
cmonths = ['Jan','Feb','Mar','Apr','May','Jun',\
    'Jul','Aug','Sep','Oct','Nov','Dec']
ccmonth = cmonths[imonth]
cdomain = 'conus'
#master_directory_fullfield_output = '/Volumes/NBM/conus_gefsv12/fullfield/'
master_directory_thinned_output = '/Volumes/NBM/conus_gefsv12/thinned/'
dressed = False
plot_fullfield = True
weighted = True
makeplot = True

# ---- read the stored netCDF full-field probabilities, quantile mapped

#if weighted == True:
#    if dressed == True:
#        infile = master_directory_fullfield_output +\
#            ccmonth+cyear+'_use99_lead'+clead+\
#            '_probabilities_weighted_dressed_fullfield.nc'
#    else:
#        infile = master_directory_fullfield_output +\
#            ccmonth+cyear+'_use99_lead'+clead+\
#            '_probabilities_weighted_fullfield.nc'
#else:
#    infile = master_directory_fullfield_output +\
#        ccmonth+cyear+'_use99_lead'+clead+\
#        '_probabilities_fullfield.nc'

if weighted == True:
    if dressed == True:
        infile = master_directory_thinned_output +\
            ccmonth+cyear+'_use99_lead'+clead+\
            '_probabilities_weighted_dressed_thinned.nc'
    else:
        infile = master_directory_thinned_output +\
            ccmonth+cyear+'_use99_lead'+clead+\
            '_probabilities_weighted_thinned.nc'
else:
    infile = master_directory_thinned_output +\
        ccmonth+cyear+'_use99_lead'+clead+\
        '_probabilities_thinned.nc'

print ('reading from ', infile)
nc = Dataset(infile,'r')
yyyymmddhh = nc.variables['yyyymmddhh_init'][:]
idd = np.where(yyyymmddhh == int(cyyyymmddhh))[0]
print ('yyyymmddhh = ', yyyymmddhh)
thresholds = nc.variables['thresholdv'][:]
if rthresh == 0.254:
    itt = 0
else:
    itt = int(np.where(thresholds == rthresh)[0])
print ('itt = ', itt)
lons_ndfd = nc.variables['lons'][:,:]
lats_ndfd = nc.variables['lats'][:,:]
prob_qmapped_fullfield = \
    np.squeeze(nc.variables['probability_qmapped'][idd,itt,:,:])
nc.close()
ny_ndfd, nx_ndfd = np.shape(lons_ndfd)

# CONUS
#m = Basemap(llcrnrlon=233.7234,llcrnrlat=19.229,\
#    urcrnrlon = 300.95782, urcrnrlat = 54.37279,\
#    projection='lcc',lat_1=25.,lat_2=25.,lon_0=265.,\
#    resolution ='l',area_thresh=1000.)

# Western US
m = Basemap(llcrnrlon=235.,llcrnrlat=30.,\
    urcrnrlon = 260., urcrnrlat = 45.,\
    projection='lcc',lat_1=25.,lat_2=25.,lon_0=265.,\
    resolution ='l',area_thresh=1000.)

# Pac Northwest
#m = Basemap(llcrnrlon=233.7234,llcrnrlat=37.,\
#    urcrnrlon = 260., urcrnrlat = 54.37279,\
#    projection='lcc',lat_1=25.,lat_2=25.,lon_0=265.,\
#    resolution ='l',area_thresh=1000.)

#m = Basemap(llcrnrlon=245.,llcrnrlat=35.,
#    urcrnrlon = 260., urcrnrlat = 43.,\
#    projection='lcc',lat_1=25.,lat_2=25.,lon_0=255.,\
#    resolution ='l',area_thresh=1000.)

# Cali
#m = Basemap(llcrnrlon=235.,llcrnrlat=31.,
#    urcrnrlon = 245., urcrnrlat = 42.,\
#    projection='lcc',lat_1=25.,lat_2=25.,lon_0=245.,\
#    resolution ='l',area_thresh=1000.)

#m = Basemap(llcrnrlon=230.,llcrnrlat=22.,
#    urcrnrlon = 290., urcrnrlat = 51.,\
#    projection='lcc',lat_1=25.,lat_2=25.,lon_0=255.,\
#    resolution ='l',area_thresh=1000.)

clevs = [0.0,0.05,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.93,0.97,1.0]
colorst = ['White','#E4FFFF','#C4E8FF','#8FB3FF','#D8F9D8',\
    '#A6ECA6','#42F742','Yellow','Gold','Orange','#FCD5D9','#F6A3AE',\
    '#FA5257','Orchid','#AD8ADB','#A449FF','LightGray']
parallels = np.arange(20.,60.01,5.)
meridians = np.arange(-140.,-49.95,5.)
lons = lons_ndfd
lats = lats_ndfd
prob_qmapped = np.squeeze(prob_qmapped_fullfield)
ctype = ''

if makeplot == True:
    x, y = m(lons, lats)

    # ---- plot the quantile-mapped probability

    #fig = plt.figure(figsize=(9,6.5))
    fig = plt.figure(figsize=(9.,6.5))
    #fig = plt.figure(figsize=(6.,7.7))
    #axloc = [0.06,0.15,0.92,0.8]
    axloc = [0.06,0.11,0.92,0.8]
    ax1 = fig.add_axes(axloc)
    cleadb = str(int(clead)-6)
    #title = ctype+' Quantile-mapped GEFSv12 P(obs > '+\
    #    cthresh+' mm), IC = '+cyyyymmddhh+', lead = '+clead+' h'
    if weighted == True:
        if dressed == True:
            title = 'Weighted, dressed quantile-mapped GEFSv12 P(obs > '+\
                cthresh+' mm),\nIC = '+cyyyymmddhh+', lead = '+clead+' h'
            plot_title = 'qmapped_chist_dressed_probability_'+cthresh+'mm_'+\
                cyyyymmddhh+'_lead'+clead+'.png'
        else:
            title = 'Weighted, quantile-mapped GEFSv12 P(obs > '+\
                cthresh+' mm),\nIC = '+cyyyymmddhh+', lead = '+clead+' h'
            plot_title = 'qmapped_chist_probability_'+cthresh+'mm_'+\
                cyyyymmddhh+'_lead'+clead+'.png'
    else:
        title = 'Quantile-mapped GEFSv12 P(obs > '+\
            cthresh+' mm),\nIC = '+cyyyymmddhh+', lead = '+clead+' h'
        plot_title = 'qmapped_probability_'+cthresh+'mm_'+\
            cyyyymmddhh+'_lead'+clead+'.png'

    ax1.set_title(title, fontsize=14,color='Black')
    CS2 = m.contourf(x, y, prob_qmapped[:,:], clevs,\
        cmap=None, colors=colorst, extend='both')

    m.drawcoastlines(linewidth=0.8,color='Gray')
    m.drawcountries(linewidth=0.8,color='Gray')
    m.drawstates(linewidth=0.8,color='Gray')
    m.drawcounties(linewidth=0.3,color='LightGray')

    #m.drawparallels(parallels,labels=[1,0,0,0],\
    #    color='Gray',fontsize=8,linewidth=0.2)
    #meridians = np.arange(220.,360.,5.)
    #m.drawmeridians(meridians,labels=[0,0,0,1],\
    #    color='Gray',fontsize=8,linewidth=0.2)

    cax = fig.add_axes([0.06,0.06,0.92,0.02])
    cb = plt.colorbar(CS2,orientation='horizontal',cax=cax,\
        drawedges=True,ticks=clevs,format='%g')
    cb.ax.tick_params(labelsize=7)
    cb.set_label('Probability',fontsize=9)

    fig.savefig(plot_title, dpi=300)
    plt.close()
    print ('saving plot to file = ',plot_title)

#


import socket
import csv
import datetime
import schedule
import xlwings as xw
import gc
from time import sleep

tusin = 0

# ソケットを作成する
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # 12345番ポートで接続を待つ
    print('========== 開始 =========')
    s.bind(('0.0.0.0', 12345))
    s.listen(5)
    while True:
        # スケジュールを呼び出す
        # schedule.run_pending()
        sleep(1)
        # 接続と送信元IPアドレスを得る
        conn, addr = s.accept()
        with conn:
            # while True:
            # データを受信する
            data = conn.recv(1024)
            if not data:
                print("=== データなし break ===")
                break
            data = data.decode()
            tusin += 1
            # 受信したデータを表示する
            print(str(tusin) + ' Recv:' + data)
            # レスポンスを送信する
            conn.sendall(('Received: ' + data + '\n').encode())
            #　csvファイルに時間とセンサー番号を書き込み
            d_today = datetime.date.today()
            t_now = datetime.datetime.now().time()
            t_now = t_now.replace(microsecond=0)
            dataSplit = data.split("_")
            with open("https://192.168.254.204/tossy_test/" + str(d_today) + "-" + dataSplit[0] + ".csv", 'a', newline="") as f:
                writer = csv.writer(f)
                writer.writerow([str(d_today), str(t_now), dataSplit[1]])

        del data, d_today, t_now, dataSplit, writer
        gc.collect()


class Valve():
    counter = 1
    def __init__(self,name='Valve'):
        self.mode = None
        self.modeVal = None
        self.OM_data_eqn = ''
        self.OM_data_init = ''
        self.InputStms = None
        self.OutputStms = None
        self.type = 'Valve'

        self.Prop = {
                'pressDrop':None,
                'outP':None
            }
        # new
        self.name = name + str(Valve.counter)
        self.no_of_input = 1
        self.no_of_output = 1
        Valve.counter += 1

    def getname(self):
        return self.name

    def modesList(self):
        return ["pressDrop","outP"]

    def paramgetter(self,mode="pressDrop"):
        self.mode = mode
        dict = {self.mode:None}
        return dict

    def paramsetter(self,dict):

        self.modeVal = dict[self.mode]

    def connect(self,InputStms = None,OutputStms = None):
        self.InputStms = InputStms
        self.OutputStms = OutputStms

    def OM_Flowsheet_Init(self, addedcomp):
        self.OM_data_init = ''
        comp_count = len(addedcomp)
        self.OM_data_init = self.OM_data_init + (
        "Simulator.Unit_Operations.Valve " + self.name + "(Nc = " + str(comp_count))
        self.OM_data_init = self.OM_data_init + (",comp = {")
        comp = str(addedcomp).strip('[').strip(']')
        comp = comp.replace("'", "")
        self.OM_data_init = self.OM_data_init + comp + ("});\n")
        return self.OM_data_init

    def OM_Flowsheet_Eqn(self, addedcomp):
        self.OM_data_eqn = ''

        self.OM_data_eqn = self.OM_data_eqn + ('connect(' + self.InputStms[0].name + '.outlet,' +  self.name + '.inlet' + ');\n')
        self.OM_data_eqn = self.OM_data_eqn + ('connect(' + self.name + '.outlet,' + self.OutputStms[0].name + '.inlet);\n')
        self.OM_data_eqn = self.OM_data_eqn + (self.name+'.'+self.mode+'='+ self.modeVal + ';\n')
        return self.OM_data_eqn


import enum
from datetime import datetime
from typing import TYPE_CHECKING, Any, ClassVar
from uuid import UUID

import sqlalchemy
import sqlalchemy as sa
from sqlalchemy import (
    TIMESTAMP,
    BigInteger,
    Boolean,
    ForeignKey,
    Index,
    Integer,
    String,
    Text,
    UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import (
    Mapped,
    MappedColumn,
    declared_attr,
    mapped_column,
    relationship,
)
from sqlalchemy_utils.types.ts_vector import TSVectorType

from polar.config import settings
from polar.enums import Platforms
from polar.kit.db.models import RecordModel
from polar.kit.extensions.sqlalchemy import PostgresUUID, StringEnum
from polar.types import JSONAny

if TYPE_CHECKING:  # pragma: no cover
    from polar.models.issue_reference import IssueReference
    from polar.models.organization import Organization
    from polar.models.pledge import Pledge
    from polar.models.repository import Repository

class IssueFields:
    class State(str, enum.Enum):
        OPEN = "open"
        CLOSED = "closed"

    platform: Mapped[Platforms] = mapped_column(StringEnum(Platforms), nullable=False)
    external_id: Mapped[int] = mapped_column(Integer, nullable=False)

    @declared_attr
    def organization_id(cls) -> MappedColumn[UUID]:
        return mapped_column(
            PostgresUUID,
            ForeignKey("organizations.id"),
            nullable=False,
            index=True,
        )

    @declared_attr
    def organization(cls) -> "Mapped[Organization]":
        return relationship("Organization", lazy="raise")

    @declared_attr
    def repository_id(cls) -> "MappedColumn[UUID]":
        return mapped_column(
            PostgresUUID,
            ForeignKey("repositories.id"),
            nullable=False,
            index=True,
        )

    @declared_attr
    def repository(cls) -> "Mapped[Repository]":
        return relationship("Repository", lazy="raise")

    number: Mapped[int] = mapped_column(Integer, nullable=False)

    title: Mapped[str] = mapped_column(String, nullable=False)
    body: Mapped[str | None] = mapped_column(Text, nullable=True)
    comments: Mapped[int | None] = mapped_column(Integer, nullable=True)

    author: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    author_association: Mapped[str | None] = mapped_column(String, nullable=True)
    labels: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    assignee: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    assignees: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    milestone: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    closed_by: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)
    reactions: Mapped[JSONAny] = mapped_column(JSONB(none_as_null=True), nullable=True)

    state: Mapped[str] = mapped_column(StringEnum(State), nullable=False)
    state_reason: Mapped[str | None] = mapped_column(String, nullable=True)

    issue_closed_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )
    issue_created_at: Mapped[datetime] = mapped_column(
        TIMESTAMP(timezone=True), nullable=False
    )
    issue_modified_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )

    title_tsv: Mapped[TSVectorType] = mapped_column(
        TSVectorType("title", regconfig="simple"),
        sa.Computed("to_tsvector('simple', \"title\")", persisted=True),
    )

issue_fields_mutables = {
    "title",
    "body",
    "comments",
    "author",
    "author_association",
    "labels",
    "assignee",
    "assignees",
    "milestone",
    "closed_by",
    "reactions",
    "state",
    "state_reason",
    "issue_closed_at",
    "issue_modified_at",
}

class Issue(IssueFields, RecordModel):
    __tablename__ = "issues"
    __table_args__ = (
        UniqueConstraint("external_id"),
        UniqueConstraint("platform", "external_lookup_key"),
        UniqueConstraint("organization_id", "repository_id", "number"),
        # Search index
        Index("idx_issues_title_tsv", "title_tsv", postgresql_using="gin"),
        Index(
            "idx_issues_id_closed_at",
            "id",
            "issue_closed_at",
        ),
        Index(
            "idx_issues_pledged_amount_sum",
            "pledged_amount_sum",
        ),
        # TODO: deprecated, remove when we've migrated to positive_reactions_count and
        # total_engagement_count
        Index(
            "idx_issues_reactions_plus_one",
            sqlalchemy.text("((reactions::jsonb ->> 'plus_one')::int)"),
            postgresql_using="btree",
        ),
        Index(
            "idx_issues_positive_reactions_count",
            "positive_reactions_count",
        ),
        Index(
            "idx_issues_positive_total_engagement_count",
            "total_engagement_count",
        ),
    )

    TRANSFERRABLE_PROPERTIES: ClassVar[set[str]] = {
        "pledge_badge_embedded_at",
        "pledge_badge_ever_embedded",
        "pledge_badge_currently_embedded",
        "has_pledge_badge_label",
        "badge_custom_content",
        "funding_goal",
        "pledged_amount_sum",
        "needs_confirmation_solved",
        "confirmed_solved_at",
        "confirmed_solved_by",
    }

    external_lookup_key: Mapped[str] = mapped_column(String, nullable=True, index=True)

    pledge_badge_embedded_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )
    pledge_badge_ever_embedded: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False
    )
    pledge_badge_currently_embedded: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False
    )

    has_pledge_badge_label: Mapped[bool] = mapped_column(
        Boolean, nullable=False, default=False
    )

    badge_custom_content: Mapped[str | None] = mapped_column(
        String,
        nullable=True,
        default=None,
    )

    github_issue_etag: Mapped[str | None] = mapped_column(String, nullable=True)
    github_issue_fetched_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )

    github_timeline_etag: Mapped[str | None] = mapped_column(String, nullable=True)
    github_timeline_fetched_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )

    @declared_attr
    def references(cls) -> "Mapped[list[IssueReference]]":
        return relationship("IssueReference", lazy="raise", viewonly=True)

    @declared_attr
    def pledges(cls) -> "Mapped[list[Pledge]]":
        return relationship(
            "Pledge",
            lazy="raise",
            viewonly=True,
        )

    funding_goal: Mapped[int | None] = mapped_column(
        BigInteger, nullable=True, default=None
    )

    # calculated sum of pledges, used for sorting and in Public APIs
    pledged_amount_sum: Mapped[int] = mapped_column(
        BigInteger, nullable=False, default=0
    )

    issue_has_in_progress_relationship: Mapped[bool] = mapped_column(
        Boolean, nullable=False, server_default="false"
    )

    issue_has_pull_request_relationship: Mapped[bool] = mapped_column(
        Boolean, nullable=False, server_default="false"
    )

    positive_reactions_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0
    )

    total_engagement_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0
    )

    needs_confirmation_solved: Mapped[bool] = mapped_column(
        Boolean, default=False, nullable=False
    )

    confirmed_solved_at: Mapped[datetime | None] = mapped_column(
        TIMESTAMP(timezone=True), nullable=True
    )

    confirmed_solved_by: Mapped[UUID | None] = mapped_column(
        PostgresUUID,
        ForeignKey("users.id"),
        nullable=True,
    )

    # a number between 0 and 100
    # share of rewards that will go to contributors of this issue
    upfront_split_to_contributors: Mapped[int | None] = mapped_column(
        Integer, nullable=True
    )

    @classmethod
    def contains_pledge_badge_label(cls, labels: Any) -> bool:
        if not labels:
            return False

        return any(
            label["name"].lower() == settings.GITHUB_BADGE_EMBED_LABEL
            for label in labels
        )


#!/home/corr/miniconda3/envs/LLC/bin/python

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

from VVMsubs import vvmInit, VVM_MEAS

import time

#this program will load data to the adam to control the line stretcher
#it will do this by intaking a decimal number (0-4095)
#converting it to a 12 bit binary number
#reversing it, and sending it to the ADAM

#do I need all this?
hostname = '128.171.166.181'
port = 502
UNIT = 0x1
WR = 0
CS = 1
A0 = 2
A1 = 3
DB0 = 4
DB1 = 5
DB2 = 6
DB3 = 7
DB4 = 8
DB5 = 9
DB6 = 10
DB7 = 11
DB8 = 12
DB9 = 13
DB10 = 14
DB11 = 15

decimal = 0
channel = 1

def send_to_adam(decimal, channel):
    #this function converts the decimal number passed in to a
    #12 bit binary number
    #decimal should be a decimal number from 0-4095

    #convert decimal to 12 bit binary here
    binary = (bin(decimal)[2:])

    #turn binary int into array
    binary_array = list(map(int, str(binary)))

    #flip it around
    flipped = binary_array[::-1]

    #turn into 12 bit number
    for i in range(12-len(flipped)):
        flipped.append(0)

    #call all functions to load data per the manual
    load_data_mode()
    time.sleep(0.1)
    load_data(flipped)
    select_channel(channel)
    chip_select()

    time.sleep(0.1)
    client.write_coils(WR, [1], unit=UNIT)
    time.sleep(0.1)

#create a function for each step in the manual a,b,c,d

def load_data_mode():
    #step a: change W/R from 1(default) to 0(load data mode)
    time.sleep(0.1)
    client.write_coils(WR, [0], unit=UNIT)
    time.sleep(0.1)

def load_data(flipped):
    #takes in 12 bit binary number and loads it to DB0-DB11

    #step b: load data to DB0-DB11

    ports = [DB0, DB1, DB2, DB3, DB4, DB5, DB6, DB7, DB8, DB9, DB10, DB11]

    # for i in range(12):
    #     if flipped[i] == 0:
    #         client.write_coils(ports[i], [0], unit=UNIT)
    #     else:
    #         client.write_coils(ports[i], [1], unit=UNIT)
    time.sleep(0.1)
    client.write_coils(DB0,flipped,unit=UNIT)
    time.sleep(0.1)

def select_channel(channel):
    #takes in channel number and sets A0 and A1 accordingly

    #step c: set A1, A0 to select output channel
    # channel  A1   A0
    #    1      0    0
    #    2      0    1
    #    3      1    0
    #    4      1    1

    if (channel == 1):
        client.write_coils(A0, [0,0], unit=UNIT)
        time.sleep(0.1)
    if (channel == 2):
        client.write_coils(A0, [1,0], unit=UNIT)
        time.sleep(0.1)
    if (channel == 3):
        client.write_coils(A0, [0,1], unit=UNIT)
        time.sleep(0.1)
    if (channel == 4):
        client.write_coils(A0, [1,1], unit=UNIT)
        time.sleep(0.1)

def chip_select():
    #step d: change C/S (chip select) from 1(default) to 0(select chip)
    #and back to 1 again.

    client.write_coils(CS, [0], unit=UNIT)
    time.sleep(0.1)
    client.write_coils(CS, [1], unit=UNIT)
    time.sleep(0.1)

client=ModbusClient(hostname,port)
time.sleep(0.1)
client.connect()
time.sleep(0.1)
send_to_adam(decimal,channel)
time.sleep(0.1)
client.close()

#write_holding_registers(channel, 16bit)
#channel 0 1 2 3
#16 bit- could only do half of this, since output is 10V and ls can only take 5V


scorpions = {"emperor", "red claw", "arizona", "forest", "fat tail"}
snakes = {"python", "cobra", "viper", "anaconda", "mamba"}
spiders = {"tarantula", "black widow", "wolf spider", "crab spider"}
vespas = {"yellow jacket", "hornet", "paper wasp", "john wick"}

# Spiders and snakes bite.
# Vespas and Scorpions sting.

# Creating empty sets
biters = {*{}}
stingers = {*{}}

biters.update(spiders, snakes)
stingers.update(vespas, scorpions)

print(".update")
print(biters)
print(stingers)

biters.clear()
stingers.clear()

biters = spiders.union(snakes)
stingers = vespas.union(scorpions)

print(".union")
print(biters)
print(stingers)

biters.clear()
stingers.clear()

# Can't union multiple sets with -comma(,)-
# biters |= spiders, snakes
# stingers |= vespas, scorpions

# Can't union multiple sets with -and- as well
# biters |= spiders and snakes and vespas
# stingers |= vespas and scorpions

# Can union multiple sets with -pipe(|)-
biters |= spiders | snakes
stingers |= vespas | scorpions

print("|=")
print(biters)
print(stingers)

biters.clear()
stingers.clear()


from django.contrib.auth import login, authenticate
from django.contrib.auth.decorators import login_required

from django.shortcuts import render, redirect
from ob_system.forms import SignUpForm, LoginForm

from django.http import Http404
from django.core.exceptions import ObjectDoesNotExist, EmptyResultSet

from .models import Booking, Report, Archive, CriminalProfile, CashBail
from .forms import BookingForm, ReportingForm, CriminalProfileForm, CashBailForm
from .filters import SearchFilter

import datetime as dt
import datetime

from dal import autocomplete

from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.template.loader import render_to_string
from .tokens import account_activation_token

from django.contrib.auth.models import User
from django.utils.encoding import force_text
from django.utils.http import urlsafe_base64_decode

# Create your views here.
@login_required(login_url='/accounts/login/')
def officer_login(request):

    try:

        if request.method == 'POST':

            form = LoginForm(request.POST)

            if form.is_valid():

                user = form.save(commit=False)
                user.is_active = True
                user.save()

                return redirect(index)

            else:

                form = LoginForm()

                return render(request, 'login.html', {'form': form})

        else:

            form = LoginForm()

            return render(request, 'login.html', {'form': form})

    except Exception as exception:

        raise exception

@login_required(login_url='/accounts/login/')
def signup(request):

    try:

        if request.method == 'POST':
            form = SignUpForm(request.POST)
            if form.is_valid():
                user = form.save(commit=False)
                user.is_active = False
                user.save()

                current_site = get_current_site(request)
                subject = 'Activate Your Account'
                message = render_to_string('emailing/account_activation_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
                user.email_user(subject, message)

                return redirect('account_activation_sent')
        else:
            form = SignUpForm()
        return render(request, 'signup.html', {'form': form})

    except Exception as exception:

        raise exception

def account_activation_sent(request):

    return render(request, 'emailing/account_activation_sent.html')

@login_required(login_url='/accounts/login/')
def activate(request, uidb64, token):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.profile.email_confirmed = True
        user.save()
        return redirect('login')
    else:
        return render(request, 'emailing/account_activation_invalid.html')

# The landing page
@login_required(login_url='/accounts/login/')
def index(request):

    suspect_list = CriminalProfile.objects.all()

    suspect_filter = SearchFilter(request.GET, queryset=suspect_list)

    # return render(request, 'search/searchlist.html', {'filter': suspect_filter})

    return render(request, 'index.html',{'filter': suspect_filter})

# occurrence book
@login_required(login_url='/accounts/login/')
def occurrence_book(request):

    '''
    :param request: current day reports
    :return: current day bookings and reports
    '''

    # if not request.user.is_authenticated():
    #
    #     return Archive.objects.none()

    try:
        date = dt.date.today()

        bookings = Booking.current_day_bookings().order_by('-time')

        reports = Report.current_day_reports().order_by('-time')

        try:

            if request.method == 'POST':

                reportingform = ReportingForm(request.POST)
                suspectform = CriminalProfileForm(request.POST)

                if reportingform.is_valid():

                    reporting = reportingform.save(commit=False)
                    reporting.user = request.user
                    reporting.save()

                    return redirect(occurrence_book)

                else:

                    reportingform = ReportingForm()

                if suspectform.is_valid():

                    profile = suspectform.save(commit=False)
                    profile.user = request.user
                    profile.save()

                    return redirect('search')

                else:

                    suspectform = CriminalProfileForm()

                    return render(request, 'occurrence-book/occurrence.html', {'reportingform': reportingform, 'date': date,
                                                                               'bookings': bookings, 'reports': reports,
                                                                               'suspectform': suspectform})

            else:

                reportingform = ReportingForm()
                suspectform = CriminalProfileForm()

                return render(request, 'occurrence-book/occurrence.html', {'reportingform': reportingform, 'date': date,
                                                                           'bookings': bookings, 'reports': reports,
                                                                           'suspectform': suspectform})

        except Exception as exception:

            raise exception

    except Exception as exception:

        raise exception

# Archives page
@login_required(login_url='/accounts/login/')
def archives(request):

    '''
    :param request:
    :return: list of searched record e.g bookings and reports

    '''

    try:

        return render(request, 'archives/archive.html')

    except ValueError:

        raise Http404()

#  Cash bail page
@login_required(login_url='/accounts/login/')
def cash_bail(request):

    '''
    :param request: list of current day bailed out suspects
    :return:
    '''
    date = dt.date.today()

    # now = datetime.datetime.now().strftime('%H:%M:%S')
    bail = CashBail.objects.all()

    try:

        if request.method == 'POST':

            form = CashBailForm(request.POST)

            if form.is_valid():

                bail = form.save(commit=False)

                bail.user = request.user

                bail.save()

                return redirect(cash_bail)

            else:

                form = CashBailForm()

                return render(request, 'occurrence-book/cashbail.html', {'form': form,'date':date,'bail':bail})

        else:

            form = CashBailForm()

            return render(request, 'occurrence-book/cashbail.html', {'form': form,'date':date,'bail':bail})

    except Exception as exception:

        raise exception

# return render(request, 'occurrence-book/cashbail.html',{'date':date,'bail':bail})

@login_required(login_url='/accounts/login/')
def create_criminal_profile(request):

    '''
    :param request: function to create a new suspect profile
    :return: created suspect profile
    '''

    try:

        if request.method == 'POST':

            form = CriminalProfileForm(request.POST)

            if form.is_valid():

                profile = form.save(commit=False)

                profile.user = request.user

                profile.save()

                return redirect(occurrence_book)

            else:

                form = CriminalProfileForm()

                return render(request, 'occurrence-book/occurrence.html', {'form': form})

        else:

            form = CriminalProfileForm()

            return render(request, 'occurrence-book/occurrence.html', {'form': form})

    except Exception as exception:

        raise exception

@login_required(login_url='/accounts/login/')
def cashbailform(request):

    '''
    :param request: function to record a bailed out suspect
    :return: list of all bailed out suspects
    '''

    try:

        if request.method == 'POST':

            form = CashBailForm(request.POST)

            if form.is_valid():

                bail = form.save(commit=False)

                bail.user = request.user

                bail.save()

                return redirect(cash_bail)

            else:

                form = CashBailForm()

                return render(request, 'cashbail/cashbail.html', {'form': form})

        else:

            form = CashBailForm()

            return render(request, 'cashbail/cashbail.html', {'form': form})

    except Exception as exception:

        raise exception

@login_required(login_url='/accounts/login/')
def criminal_profile(request, criminalprofile_id_no):

    '''

    :param request: single suspect
    :param criminalprofile_id_no:
    :return: suspect profile and related items
    '''

    try:

        profile = CriminalProfile.objects.get(id_no=criminalprofile_id_no)

        bookings = Booking.objects.filter(criminal=profile).all().order_by('-id')

        try:

            if request.method == 'POST':

                form = BookingForm(request.POST)

                if form.is_valid():
                    booking = form.save(commit=False)
                    booking.user = request.user
                    booking.save()

                    return redirect(occurrence_book)

                else:

                    form = BookingForm()

                    return render(request, 'profiles/criminal-profile.html', {'profile': profile, 'bookings': bookings,
                                                                              'form': form})

            else:

                form = BookingForm()

                return render(request, 'profiles/criminal-profile.html', {'profile': profile, 'bookings': bookings,
                                                                          'form': form})

        except Exception as exception:

            raise exception

    except Exception as exception:

        raise exception

@login_required(login_url='/accounts/login/')
def search_results(request):

    try:

        if 'q' in request.GET and request.GET['q']:

            search_term = request.GET.get('q')

            bookings = Booking.search_by_pub_date(search_term).all()

            reportings = Report.search_by_pub_date(search_term).all()

            return render(request, 'archives/archive.html', {'bookings': bookings, 'reportings': reportings})

        else:

            message = "You haven't searched for any term"

            return render(request, 'searched-tag.html', {"message": message,})

    except Exception as exception:
        raise exception

@login_required(login_url='/accounts/login/')
def search(request):

    '''

    :param request: function to search db for searched item
    :return: return related searched items from the database
    '''

    suspect_list = CriminalProfile.objects.all()

    suspect_filter = SearchFilter(request.GET, queryset=suspect_list)

    return render(request, 'search/searchlist.html', {'filter': suspect_filter})


import numpy as np
import torch
import pdb

from ..utils.arrays import to_torch

VALUE_PLACEHOLDER = 1e6

def make_prefix(discretizer, context, obs, prefix_context=True):
    observation_dim = obs.size
    obs_discrete = discretizer.discretize(obs, subslice=[0, observation_dim])
    obs_discrete = to_torch(obs_discrete, dtype=torch.long)

    if prefix_context:
        prefix = torch.cat(context + [obs_discrete], dim=-1)
    else:
        prefix = obs_discrete

    return prefix

def extract_actions(x, observation_dim, action_dim, t=None):
    assert x.shape[1] == observation_dim + action_dim + 2
    actions = x[:, observation_dim:observation_dim+action_dim]
    if t is not None:
        return actions[t]
    else:
        return actions

def update_context(context, discretizer, observation, action, reward, max_context_transitions):
    '''
        context : list of transitions
            [ tensor( transition_dim ), ... ]
    '''
    ## use a placeholder for value because input values are masked out by model
    rew_val = np.array([reward, VALUE_PLACEHOLDER])
    transition = np.concatenate([observation, action, rew_val])

    ## discretize transition and convert to torch tensor
    transition_discrete = discretizer.discretize(transition)
    transition_discrete = to_torch(transition_discrete, dtype=torch.long)

    ## add new transition to context
    context.append(transition_discrete)

    ## crop context if necessary
    context = context[-max_context_transitions:]

    return context


from gtts import gTTS
from playsound import playsound

#speak the given text
def speak(audio):
    print(audio)
    print()

    obj = gTTS(text=audio, lang='en', slow=False)
    obj.save("speak.mp3")

    playsound('speak.mp3')

if __name__ == "__main__":
    str = "Inti"
    speak(str)


'''
goal : n*m 금광에서 얻을 수 있는 최대 금의 크기 출력
0. 라이브러리 추가 : 없음
1. 입력 받기
    1) 테스트 횟수 : T
    2) 금광 : N*M
    3) 금광안의 금 : 한 줄 라인 -> gold 안에 이중배열로 담기
2. 로직 수행
    1) dp 테이블 초기화 : 이중배열 0으로 초기화
    2) [0]인경우 초기화
    3) n*m 루프돌며 dp 테이블 입력
    4) 출력
'''
# 1
# T = int(input())
# for _ in range(T):
#     n, m = map(int, input().split())
#     d = [[0]*m for _ in range(n)]
#     gold = []
#     row = []
#     for i, v in enumerate(list(map(int, input().split()))):
#         row.append(v)
#         if (i+1) % 4 == 0:
#             gold.append(row)
#             row = []
#     # d[0]인 경우 초기화
#     for i in range(n):
#         d[i][0] = gold[i][0]

#     for j in range(1, m):
#         for i in range(n):
#             d[i][j] = d[i][j-1]+gold[i][j]

#             if i-1 >= 0:
#                 d[i][j] = max(d[i][j], d[i-1][j-1]+gold[i][j])
#             if i+1 < n:
#                 d[i][j] = max(d[i][j], d[i+1][j-1]+gold[i][j])
#     print(max(list(zip(*d))[m-1]))

# 문제 풀이 답안 (조금 다른 방식 풀이)
for tc in range(int(input())):
    # 입력 받기 _ 금광정보
    n, m = map(int, input().split())
    array = list(map(int, input().split()))
    # dp테이블 초기화
    dp = []
    index = 0
    for i in range(n):
        # 다른 값이 아닌 주어진 값을 slicing 하여 초기화
        dp.append(array[index:index+m])
        index += m

    for j in range(1, m):
        for i in range(n):
            if i == 0:  # i-1가 리스트에서 벗어나는 경우
                left_up = 0
            else:
                left_up = dp[i-1][j-1]
            if i == n-1:  # i+1이 리스트에서 벗어나는 경우
                left_down = 0
            else:
                left_down = dp[i+1][j-1]
            left = dp[i][j-1]
            dp[i][j] = max(left_down, left, left_up) + array[i][j]
    result = 0
    for i in range(n):
        result = max(result, dp[i][m-1])
    print(result)


import re
import os
import time
import argparse
from os import walk

def removeFromName(dir_path, reg):
    dirpath, dirnames, filenames = next(walk(dir_path))
    for directory in dirnames:
        removeFromName(directory, reg)

    for filename in filenames:
        newName = re.sub(reg, '', filename).strip()
        if(newName == ""): continue
        print(newName)
        os.rename(dirpath + "/" + filename, dirpath + "/" + newName)

parser = argparse.ArgumentParser(description='Convert to HEVC video encoder.')
parser.add_argument(
        '-r', '--regex', required=True, type=str, help='regex')
parser.add_argument(
        '-d', '--directory', required=True, type=str, help='directory path')

args = parser.parse_args()
regex = args.regex
dir_path = args.directory

removeFromName(dir_path, regex)


provider = input('Nama Provider      : ')
no = input('Nomor Telp         : ')
jam = int(input('Durasi Jam         : '))
menit = int(input('Durasi Menit       : '))
detik = int(input('Durasi Detik       : '))
saldo = int(input('Saldo Pulsa        : '))

totaldetik = (jam*60*60) + (menit*60) + detik
biaya = (totaldetik/30) * 1000
if 10000 >= biaya < 20000:
    bonus = 1000
elif 20000 >= biaya < 40000:
    bonus = 5000
elif biaya >= 40000:
    bonus = 10000
saldo_akhir = saldo - (biaya-bonus)

print('Nama Provider            :', provider)
print('Nomor Telp               :', no)
print('Ubah Jam+Menit+Detik     : {} detik' .format(totaldetik))
print('Total Pengeluaran Pulsa  : {} Rupiah' .format(biaya))
print('Bonus Pulsa              : {} Rupiah' .format(bonus))
print('Saldo Pulsa Sekarang     :', saldo_akhir)


import sys

s = sys.stdin.readline().rstrip('\n')
idx = 1
str_set = set()
cnt = 0

while (True) :
	str_set.add(s[cnt : cnt + idx])
	cnt += 1
	if (idx == len(s)) :
		break
	if ((cnt + idx) == (len(s) + 1)) :
		cnt = 0
		idx += 1
print(len(str_set))

백준/Silver/11478. 서로 다른 부분 문자열의 개수/서로 다른 부분 문자열의 개수.py

서로 다른 부분 문자열의 개수.py


#!/usr/bin/env python3

'''
Source name: demo.py
Author(s): Stoyan Shopov

Python Version: 3.* 32-bit or 64-bit
License: LGPL

Description:
    This program was demoed on EV3D4 at PyCon Australia 2017.
    It kicks off 2 threads a move thread and a feel thread.
    The move thread drives the bot forward until the feel thread
    detects an obstacle.
    Then the move thread makes the bot move around in a circle
    until the feel thread detects a touch on the touch sensor.

Preconditions:
    The program has been loaded on to EV3 running ev3dev

Postcoditions:
    Program exits cleanly.

References:
    https://github.com/sshopov/pyconau2017
    https://github.com/rhempel/ev3dev-lang-python

Release history:
----------------------------------------------------
0.0.1 - 06/08/2017:
    Initial release
'''

import sys
import time
import threading
import signal

from ev3dev import ev3

def move(done):
    lm = ev3.LargeMotor('outB'); assert lm.connected

    rm = ev3.LargeMotor('outC'); assert rm.connected

    cl = ev3.ColorSensor(); assert cl.connected
    cl.mode='COL-AMBIENT'

    speed = 250 #cl.value()

    lm.run_forever(speed_sp=speed)
    rm.run_forever(speed_sp=speed)

    while not done.is_set():
        time.sleep(1)

    #stop both motors
    lm.stop(stop_action='brake')
    rm.stop(stop_action='brake')
    lm.wait_while('running')
    rm.wait_while('running')

    #run around in a circle
    done.clear()
    lm.run_forever(speed_sp=speed)

    while not done.is_set():
        time.sleep(1)

    lm.stop(stop_action='brake')
    lm.wait_while('running')

def feel(done):
    ir = ev3.InfraredSensor(); assert ir.connected
    ts = ev3.TouchSensor(); assert ts.connected

    screen = ev3.Screen()
    sound = ev3.Sound()

    screen.draw.text((60,40), 'Going for a walk')
    screen.update()

    while ir.proximity > 30:
        if done.is_set():
            break
        time.sleep(0.1)

    done.set() #this will set it running in a circle

    ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.RED)
    ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.RED)

    screen.clear()
    screen.draw.text((60,20), 'There is something is front of me')
    screen.update()

    while not ts.is_pressed:
        sound.speak("Where should I go next?").wait()
        time.sleep(0.5)

    done.set() #will stop the circle dance

# The 'done' event will be used to signal the threads to stop:
done = threading.Event()

# We also need to catch SIGINT (keyboard interrup) and SIGTERM (termination
# signal from brickman) and exit gracefully:
def signal_handler(signal, frame):
    done.set()

signal.signal(signal.SIGINT,  signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

# Now that we have the worker functions defined, lets run those in separate
# threads.
move_thread = threading.Thread(target=move, args=(done,))
feel_thread = threading.Thread(target=feel, args=(done,))

move_thread.start()
feel_thread.start()

# The main thread will wait for the 'back' button to be pressed.  When that
# happens, it will signal the worker threads to stop and wait for their completion.
btn = ev3.Button()
while not btn.backspace and not done.is_set():
    time.sleep(1)

done.set()
move_thread.join()
feel_thread.join()

ev3.Sound.speak('Farewell and good bye!').wait()
ev3.Leds.all_off()