from fnmatch import fnmatch
import inspect
import json
import logging
import os
from StringIO import StringIO
import yaml

from twisted.internet.defer import inlineCallbacks, returnValue

from juju.agents.base import TwistedOptionNamespace
from juju.agents.machine import MachineAgent
from juju.agents.unit import UnitAgent
from juju.environment.environment import Environment
from juju.control import status
from juju.control import tests
from juju.state.endpoint import RelationEndpoint
from juju.state.environment import GlobalSettingsStateManager
from juju.state.tests.test_service import ServiceStateManagerTestBase
from juju.tests.common import get_test_zookeeper_address
from juju.unit.workflow import ZookeeperWorkflowState

from .common import ControlToolTest

tests_path = os.path.dirname(inspect.getabsfile(tests))
sample_path = os.path.join(tests_path, "sample_cluster.yaml")
sample_cluster = yaml.load(open(sample_path, "r"))

def dump_stringio(stringio, filename):
    """Debug utility to dump a StringIO to a filename."""
    fp = open(filename, "w")
    fp.write(stringio.getvalue())
    fp.close()

class StatusTestBase(ServiceStateManagerTestBase, ControlToolTest):

    # Status tests setup a large tree every time, make allowances for it.
    # TODO: create minimal trees needed per test.
    timeout = 10

    @inlineCallbacks
    def setUp(self):
        yield super(StatusTestBase, self).setUp()
        settings = GlobalSettingsStateManager(self.client)
        yield settings.set_provider_type("dummy")
        self.log = self.capture_logging()

        config = {
            "environments": {
                "firstenv": {
                    "type": "dummy",
                    "admin-secret": "homer"}}}
        self.write_config(yaml.dump(config))
        self.config.load()

        self.environment = self.config.get_default()
        self.provider = self.environment.get_machine_provider()

        self.output = StringIO()
        self.agents = []

    @inlineCallbacks
    def tearDown(self):
        for agent in self.agents:
            if getattr(agent, "api_socket", None):
                yield agent.api_socket.stopListening()
                agent.api_socket = None
        yield super(StatusTestBase, self).tearDown()

    @inlineCallbacks
    def set_unit_state(self, unit_state, state, port_protos=()):
        unit_state.set_public_address(
            "%s.example.com" % unit_state.unit_name.replace("/", "-"))
        workflow_client = ZookeeperWorkflowState(self.client, unit_state)
        yield workflow_client.set_state(state)
        for port_proto in port_protos:
            yield unit_state.open_port(*port_proto)

    @inlineCallbacks
    def add_relation_unit_states(self, relation_state, unit_states, states):
        for unit_state, state in zip(unit_states, states):
            relation_unit_state = yield relation_state.add_unit_state(
                unit_state)
            workflow_client = ZookeeperWorkflowState(
                self.client, relation_unit_state)
            yield workflow_client.set_state(state)

    @inlineCallbacks
    def add_relation_with_relation_units(
            self,
            source_endpoint, source_units, source_states,
            dest_endpoint, dest_units, dest_states):
        relation_state, service_relation_states = \
            yield self.relation_state_manager.add_relation_state(
            *[source_endpoint, dest_endpoint])
        source_relation_state, dest_relation_state = service_relation_states
        yield self.add_relation_unit_states(
            source_relation_state, source_units, source_states)
        yield self.add_relation_unit_states(
            dest_relation_state, dest_units, dest_states)

    @inlineCallbacks
    def create_agent(self, agent_cls, path, **extra_options):
        agent = agent_cls()
        options = TwistedOptionNamespace()
        options["juju_directory"] = path
        options["zookeeper_servers"] = get_test_zookeeper_address()
        for k, v in extra_options.items():
            options[k] = v
        agent.configure(options)
        agent.set_watch_enabled(False)
        agent.client = self.client
        yield agent.start()
        self.agents.append(agent)

    @inlineCallbacks
    def add_unit(self, service, machine, with_agent=lambda _: True):
        unit = yield service.add_unit_state()
        yield unit.assign_to_machine(machine)
        name = unit.unit_name
        if with_agent(name):
            juju_dir = self.makeDir()
            os.makedirs(os.path.join(juju_dir, "state"))
            os.makedirs(os.path.join(juju_dir, "units",
                                     name.replace("/", "-")))
            yield self.create_agent(UnitAgent, juju_dir, unit_name=name)
        returnValue(unit)

    @inlineCallbacks
    def build_topology(self, base=None, skip_unit_agents=()):
        """Build a simulated topology with a default machine configuration.

        This method returns a dict that can be used to get handles to
        the constructed objects.
        """
        state = {}

        # build out the topology using the state managers
        m1 = yield self.machine_state_manager.add_machine_state()
        m2 = yield self.machine_state_manager.add_machine_state()
        m3 = yield self.machine_state_manager.add_machine_state()
        m4 = yield self.machine_state_manager.add_machine_state()
        m5 = yield self.machine_state_manager.add_machine_state()
        m6 = yield self.machine_state_manager.add_machine_state()
        m7 = yield self.machine_state_manager.add_machine_state()

        # inform the provider about the machine
        yield self.provider.start_machine({"machine-id": 0,
                                           "dns-name": "steamcloud-1.com"})
        yield self.provider.start_machine({"machine-id": 1,
                                           "dns-name": "steamcloud-2.com"})
        yield self.provider.start_machine({"machine-id": 2,
                                           "dns-name": "steamcloud-3.com"})
        yield self.provider.start_machine({"machine-id": 3,
                                           "dns-name": "steamcloud-4.com"})
        yield self.provider.start_machine({"machine-id": 4,
                                           "dns-name": "steamcloud-5.com"})
        yield self.provider.start_machine({"machine-id": 5,
                                           "dns-name": "steamcloud-6.com"})
        yield self.provider.start_machine({"machine-id": 6,
                                           "dns-name": "steamcloud-7.com"})

        yield m1.set_instance_id(0)
        yield m2.set_instance_id(1)
        yield m3.set_instance_id(2)
        yield m4.set_instance_id(3)
        yield m5.set_instance_id(4)
        yield m6.set_instance_id(5)
        yield m7.set_instance_id(6)

        state["machines"] = [m1, m2, m3, m4, m5, m6, m7]

        # "Deploy" services
        wordpress = yield self.add_service_from_charm("wordpress")
        mysql = yield self.add_service_from_charm("mysql")
        yield mysql.set_exposed_flag()  # but w/ no open ports

        varnish = yield self.add_service_from_charm("varnish")
        yield varnish.set_exposed_flag()
        # w/o additional metadata
        memcache = yield self.add_service("memcache")

        state["services"] = dict(wordpress=wordpress, mysql=mysql,
                                 varnish=varnish, memcache=memcache)

        def with_unit(name):
            for pattern in skip_unit_agents:
                if fnmatch(name, pattern):
                    return False
            return True
        wpu = yield self.add_unit(wordpress, m1, with_unit)
        myu1 = yield self.add_unit(mysql, m2, with_unit)
        myu2 = yield self.add_unit(mysql, m3, with_unit)
        vu1 = yield self.add_unit(varnish, m4, with_unit)
        vu2 = yield self.add_unit(varnish, m5, with_unit)
        mc1 = yield self.add_unit(memcache, m6, with_unit)
        mc2 = yield self.add_unit(memcache, m7, with_unit)

         # add unit states to services and assign to machines
        # Set the lifecycle state and open ports, if any, for each unit state.
        yield self.set_unit_state(wpu, "started", [(80, "tcp"), (443, "tcp")])
        yield self.set_unit_state(myu1, "started")
        yield self.set_unit_state(myu2, "stopped")
        yield self.set_unit_state(vu1, "started", [(80, "tcp")])
        yield self.set_unit_state(vu2, "started", [(80, "tcp")])
        yield self.set_unit_state(mc1, None)
        yield self.set_unit_state(mc2, "installed")

        # Wordpress integrates with each of the following
        # services. Each relation endpoint is used to define the
        # specific relation to be established.
        mysql_ep = RelationEndpoint(
            "mysql", "client-server", "db", "server")
        memcache_ep = RelationEndpoint(
            "memcache", "client-server", "cache", "server")
        varnish_ep = RelationEndpoint(
            "varnish", "client-server", "proxy", "client")

        wordpress_db_ep = RelationEndpoint(
            "wordpress", "client-server", "db", "client")
        wordpress_cache_ep = RelationEndpoint(
            "wordpress", "client-server", "cache", "client")
        wordpress_proxy_ep = RelationEndpoint(
            "wordpress", "client-server", "proxy", "server")

        # Create relation service units for each of these relations
        yield self.add_relation_with_relation_units(
            mysql_ep, [myu1, myu2], ["up", "departed"],
            wordpress_db_ep, [wpu], ["up"])
        yield self.add_relation_with_relation_units(
            memcache_ep, [mc1, mc2], ["up", "down"],
            wordpress_cache_ep, [wpu], ["up"])
        yield self.add_relation_with_relation_units(
            varnish_ep, [vu1, vu2], ["up", "up"],
            wordpress_proxy_ep, [wpu], ["up"])

        state["relations"] = dict(
            wordpress=[wpu],
            mysql=[myu1, myu2],
            varnish=[vu1, vu2],
            memcache=[mc1, mc2]
            )
        returnValue(state)

    def mock_environment(self):
        mock_environment = self.mocker.patch(Environment)
        mock_environment.get_machine_provider()
        self.mocker.result(self.provider)

class StatusTest(StatusTestBase):

    @inlineCallbacks
    def test_peer_relation(self):
        """Verify status works with peer relations.
        """
        m1 = yield self.machine_state_manager.add_machine_state()
        m2 = yield self.machine_state_manager.add_machine_state()
        yield self.provider.start_machine({"machine-id": 0,
                                           "dns-name": "steamcloud-1.com"})
        yield self.provider.start_machine({"machine-id": 1,
                                           "dns-name": "steamcloud-2.com"})
        yield m1.set_instance_id(0)
        yield m2.set_instance_id(1)

        riak = yield self.add_service_from_charm("riak")
        riak_u1 = yield self.add_unit(riak, m1)
        riak_u2 = yield self.add_unit(riak, m2, with_agent=lambda _: False)
        yield self.set_unit_state(riak_u1, "started")
        yield self.set_unit_state(riak_u2, "started")

        _, (peer_rel,) = yield self.relation_state_manager.add_relation_state(
            RelationEndpoint("riak", "peer", "ring", "peer"))

        yield ZookeeperWorkflowState(
            self.client,
            (yield peer_rel.add_unit_state(riak_u1))).set_state("up")
        yield peer_rel.add_unit_state(riak_u2)

        state = yield status.collect(
            ["riak"], self.provider, self.client, None)
        self.assertEqual(
            state["services"]["riak"],
            {"charm": "local:series/riak-7",
             "relations": {"ring": "riak"},
             "units": {"riak/0": {"machine": 0,
                                  "public-address": "riak-0.example.com",
                                  "relations": {"ring": {"state": "up"}},
                                  "state": "started"},
                       "riak/1": {"machine": 1,
                                  "public-address": "riak-1.example.com",
                                  "relations": {"ring": {"state": None}},
                                  "state": "down"}}})

    @inlineCallbacks
    def test_collect(self):
        yield self.build_topology(skip_unit_agents=("varnish/1",))

        agent = MachineAgent()
        options = TwistedOptionNamespace()
        options["juju_directory"] = self.makeDir()
        options["zookeeper_servers"] = get_test_zookeeper_address()
        options["machine_id"] = "0"
        agent.configure(options)
        agent.set_watch_enabled(False)
        agent.client = self.client
        yield agent.start()

        # collect everything
        state = yield status.collect(None, self.provider, self.client, None)
        services = state["services"]
        self.assertIn("wordpress", services)
        self.assertIn("varnish", services)
        self.assertIn("mysql", services)

        # and verify the specifics of a single service
        self.assertTrue("mysql" in services)
        units = list(services["mysql"]["units"])
        self.assertEqual(len(units), 2)

        self.assertEqual(state["machines"][0],
                         {"instance-id": 0,
                          "instance-state": "unknown",
                          "dns-name": "steamcloud-1.com",
                          "state": "running"})

        self.assertEqual(services["mysql"]["relations"],
                         {"db": "wordpress"})

        self.assertEqual(services["wordpress"]["relations"],
                         {"cache": "memcache",
                          "db": "mysql",
                          "proxy": "varnish"})

        self.assertEqual(
            services["varnish"],
            {"units":
                 {"varnish/1": {
                        "machine": 4,
                        "state": "down",
                        "open-ports": ["80/tcp"],
                        "public-address": "varnish-1.example.com",
                        "relations": {"proxy": {"state": "up"}}},
                  "varnish/0": {
                        "machine": 3,
                        "state": "started",
                        "public-address": "varnish-0.example.com",
                        "open-ports": ["80/tcp"],
                        "relations": {"proxy": {"state": "up"}}}},
             "exposed": True,
             "charm": "local:series/varnish-1",
             "relations": {"proxy": "wordpress"}})

        self.assertEqual(
            services["wordpress"],
            {"charm": "local:series/wordpress-3",
             "relations":  {
                    "cache": "memcache",
                    "db": "mysql",
                    "proxy": "varnish"},
             "units": {
                    "wordpress/0": {
                        "machine": 0,
                        "public-address": "wordpress-0.example.com",
                        "relations": {
                            "cache": {"state": "up"},
                            "db": {"state": "up"},
                            "proxy": {"state": "up"}},
                        "state": "started"}}})

        self.assertEqual(
            services["memcache"],
            {"charm": "local:series/dummy-1",
             "relations": {"cache": "wordpress"},
             "units": {
                    "memcache/0": {
                        "machine": 5,
                        "public-address": "memcache-0.example.com",
                        "relations": {
                            "cache": {"state": "up"}},
                        "state": "pending"},
                    "memcache/1": {
                        "machine": 6,
                        "public-address": "memcache-1.example.com",
                        "relations": {
                            "cache": {"state": "down"}},
                        "state": "installed"}}})

    @inlineCallbacks
    def test_collect_filtering(self):
        yield self.build_topology()

        # collect by service name
        state = yield status.collect(
            ["wordpress"], self.provider, self.client, None)
        # Validate that only the expected service is present
        # in the state
        self.assertEqual(state["machines"].keys(), [0])
        self.assertEqual(state["services"].keys(), ["wordpress"])

        # collect by unit name
        state = yield status.collect(["*/0"], self.provider, self.client, None)
        self.assertEqual(set(state["machines"].keys()), set([0, 1, 3, 5]))
        self.assertEqual(set(state["services"].keys()),
                         set(["memcache", "varnish", "mysql", "wordpress"]))

        # collect by unit name
        state = yield status.collect(["*/1"], self.provider, self.client, None)
        self.assertEqual(set(state["machines"].keys()), set([2, 4, 6]))

        # verify that only the proper units and services are present
        self.assertEqual(
            state["services"],
            {"memcache": {
                "charm": "local:series/dummy-1",
                    "relations": {"cache": "wordpress"},
                    "units": {
                        "memcache/1": {
                            "machine": 6,
                            "state": "installed",
                            "public-address": "memcache-1.example.com",
                            "relations": {"cache": {"state": "down"}}}}},
             "mysql": {
                    "exposed": True,
                    "charm": "local:series/mysql-1",
                    "relations": {"db": "wordpress"},
                    "units": {
                        "mysql/1": {
                            "machine": 2,
                            "public-address": "mysql-1.example.com",
                            "open-ports": [],
                            "state": "stopped",
                            "relations": {"db": {"state": "departed"}}}}},
             "varnish": {
                    "exposed": True,
                    "charm": "local:series/varnish-1",
                    "relations": {"proxy": "wordpress"},
                    "units": {
                        "varnish/1": {
                            "machine": 4,
                            "public-address": "varnish-1.example.com",
                            "open-ports": ["80/tcp"],
                            "state": "started",
                            "relations": {"proxy": {"state": "up"}}}}}})

        # filter a missing service
        state = yield status.collect(
            ["cluehammer"], self.provider, self.client, None)
        self.assertEqual(set(state["machines"].keys()), set([]))
        self.assertEqual(set(state["services"].keys()), set([]))

        # filter a missing unit
        state = yield status.collect(["*/7"], self.provider, self.client, None)
        self.assertEqual(set(state["machines"].keys()), set([]))
        self.assertEqual(set(state["services"].keys()), set([]))

    @inlineCallbacks
    def test_collect_with_unassigned_machines(self):
        yield self.build_topology()
        # get a service's units and unassign one of them
        wordpress = yield self.service_state_manager.get_service_state(
            "wordpress")
        units = yield wordpress.get_all_unit_states()
        # There is only a single wordpress machine in the topology.
        unit = units[0]
        machine_id = yield unit.get_assigned_machine_id()
        yield unit.unassign_from_machine()
        yield unit.set_public_address(None)
        # test that the machine is in state information w/o assignment
        state = yield status.collect(None, self.provider, self.client, None)
        # verify that the unassigned machine appears in the state
        self.assertEqual(state["machines"][machine_id],
                         {"dns-name": "steamcloud-1.com",
                          "instance-id": 0,
                          "instance-state": "unknown",
                          "state": "not-started"})

        # verify that we have a record of the unassigned service;
        # but note that unassigning this machine without removing the
        # service unit and relation units now produces other dangling
        # records in the topology
        self.assertEqual(
            state["services"]["wordpress"]["units"],
            {"wordpress/0":
                 {"machine": None,
                  "public-address": None,
                  "relations": {
                        "cache": {"state": "up"},
                        "db": {"state": "up"},
                        "proxy": {"state": "up"}},
                 "state": "started"}})

    @inlineCallbacks
    def test_collect_with_removed_unit(self):
        yield self.build_topology()
        # get a service's units and unassign one of them
        wordpress = yield self.service_state_manager.get_service_state(
            "wordpress")
        units = yield wordpress.get_all_unit_states()
        # There is only a single wordpress machine in the topology.
        unit = units[0]
        machine_id = yield unit.get_assigned_machine_id()
        yield wordpress.remove_unit_state(unit)

        # test that wordpress has no assigned service units
        state = yield status.collect(None, self.provider, self.client, None)
        self.assertEqual(
            state["services"]["wordpress"],
            {"charm": "local:series/wordpress-3",
             "relations": {"cache": "memcache",
                           "db": "mysql",
                           "proxy": "varnish"},
             "units": {}})

        # but its machine is still available as reported by status
        seen_machines = set()
        for service, service_data in state["services"].iteritems():
            for unit, unit_data in service_data["units"].iteritems():
                seen_machines.add(unit_data["machine"])
        self.assertIn(machine_id, state["machines"])
        self.assertNotIn(machine_id, seen_machines)

    @inlineCallbacks
    def test_provider_pending_machine_state(self):
        # verify that we get some error reporting if the provider
        # doesn't have proper machine info
        yield self.build_topology()

        # add a new machine to the topology (but not the provider)
        # and status it
        m8 = yield self.machine_state_manager.add_machine_state()
        wordpress = yield self.service_state_manager.get_service_state(
            "wordpress")
        wpu = yield wordpress.add_unit_state()
        yield wpu.assign_to_machine(m8)

        # test that we identify we don't have machine state
        state = yield status.collect(
            None, self.provider, self.client, logging.getLogger())
        self.assertEqual(state["machines"][7]["instance-id"],
                         "pending")

    @inlineCallbacks
    def test_render_yaml(self):
        yield self.build_topology()
        self.mock_environment()
        self.mocker.replay()

        yield status.status(self.environment, [],
                            status.render_yaml, self.output, None)
        state = yaml.load(self.output.getvalue())

        self.assertEqual(set(state["machines"].keys()),
                         set([0, 1, 2, 3, 4, 5, 6]))

        services = state["services"]

        self.assertEqual(set(services["memcache"].keys()),
                         set(["charm", "relations", "units"]))
        self.assertEqual(set(services["mysql"].keys()),
                         set(["exposed", "charm", "relations", "units"]))
        self.assertEqual(set(services["varnish"].keys()),
                         set(["exposed", "charm", "relations", "units"]))
        self.assertEqual(set(services["wordpress"].keys()),
                         set(["charm", "relations", "units"]))

        for service in services.itervalues():
            self.assertGreaterEqual(  # may also include "exposed" key
                set(service.keys()),
                set(["units", "relations", "charm"]))
            self.assertTrue(service["charm"].startswith("local:series/"))

        self.assertEqual(state["machines"][0],
                         {"instance-id": 0,
                          "instance-state": "unknown",
                          "dns-name": "steamcloud-1.com",
                          "state": "down"})

        self.assertEqual(services["mysql"]["relations"],
                         {"db": "wordpress"})

        self.assertEqual(services["mysql"]["units"]["mysql/1"]["open-ports"],
                         [])

        self.assertEqual(services["wordpress"]["relations"],
                         {"cache": "memcache",
                          "db": "mysql",
                          "proxy": "varnish"})

    @inlineCallbacks
    def test_render_json(self):
        yield self.build_topology()
        self.mock_environment()
        self.mocker.replay()

        yield status.status(self.environment, [],
                            status.render_json, self.output, None)

        state = json.loads(self.output.getvalue())
        self.assertEqual(set(state["machines"].keys()),
                         set([unicode(i) for i in [0, 1, 2, 3, 4, 5, 6]]))

        services = state["services"]

        self.assertEqual(set(services["memcache"].keys()),
                         set(["charm", "relations", "units"]))
        self.assertEqual(set(services["mysql"].keys()),
                         set(["exposed", "charm", "relations", "units"]))
        self.assertEqual(set(services["varnish"].keys()),
                         set(["exposed", "charm", "relations", "units"]))
        self.assertEqual(set(services["wordpress"].keys()),
                         set(["charm", "relations", "units"]))

        for service in services.itervalues():
            self.assertTrue(service["charm"].startswith("local:series/"))

        self.assertEqual(state["machines"][u"0"],
                         {"instance-id": 0,
                          "instance-state": "unknown",
                          "dns-name": "steamcloud-1.com",
                          "state": "down"})

        self.assertEqual(services["mysql"]["relations"],
                         {"db": "wordpress"})

        self.assertEqual(services["mysql"]["units"]["mysql/1"]["open-ports"],
                         [])

        self.assertEqual(services["wordpress"]["relations"],
                         {"cache": "memcache",
                          "db": "mysql",
                          "proxy": "varnish"})

        self.assertEqual(
            services["varnish"],
            {
                "exposed": True,
                "units":
                    {"varnish/1": {
                        "machine": 4,
                        "public-address": "varnish-1.example.com",
                        "open-ports": ["80/tcp"],
                        "state": "started",
                        "relations": {"proxy": {"state": "up"}}},
                     "varnish/0": {
                        "machine": 3,
                        "public-address": "varnish-0.example.com",
                        "open-ports": ["80/tcp"],
                        "state": "started",
                        "relations": {"proxy": {"state": "up"}}}},
                     "charm": "local:series/varnish-1",
                "relations": {"proxy": "wordpress"}})

    @inlineCallbacks
    def test_render_dot(self):
        yield self.build_topology()
        self.mock_environment()
        self.mocker.replay()

        yield status.status(self.environment, [],
                            status.render_dot, self.output, None)

        result = self.output.getvalue()
        #dump_stringio(self.output, "/tmp/ens.dot")

        # make mild assertions about the expected DOT output
        # because the DOT language is simple we can test that some
        # relationships are present
        self.assertIn('memcache -> "memcache/1"', result)
        self.assertIn('varnish -> "varnish/0"', result)
        self.assertIn('varnish -> "varnish/1"', result)

        # test that relationships are being rendered
        self.assertIn("wordpress -> memcache", result)
        self.assertIn("mysql -> wordpress", result)

        # assert that properties were applied to a relationship
        self.assertIn("wordpress -> varnish  [dir=none, label=proxy]",
                      result)

        # verify that the renderer picked up the DNS name of the
        # machines (and they are associated with the proper machine)
        self.assertIn(
            '"mysql/0" [color="#DD4814", fontcolor="#ffffff", shape=box, style=filled, label=<mysql/0<br/><i>mysql-0.example.com</i>>]',
            result)
        self.assertIn(
            '"mysql/1" [color="#DD4814", fontcolor="#ffffff", shape=box, style=filled, label=<mysql/1<br/><i>mysql-1.example.com</i>>]',
            result)

        # Check the charms are present in the service node.
        self.assertIn(
            'memcache [color="#772953", fontcolor="#ffffff", shape=component, style=filled, label=<memcache<br/>local:series/dummy-1>]', result)
        self.assertIn(
            'varnish [color="#772953", fontcolor="#ffffff", shape=component, style=filled, label=<varnish<br/>local:series/varnish-1>]',result)
        self.assertIn(
            'mysql [color="#772953", fontcolor="#ffffff", shape=component, style=filled, label=<mysql<br/>local:series/mysql-1>]', result)

        self.assertIn("local:series/dummy-1", result)

    def test_render_dot_bad_clustering(self):
        """Test around Bug #792448.

        Deployment producing bad status dot output, but sane normal
        output.
        """
        self.mocker.replay()

        output = StringIO()
        renderer = status.renderers["dot"]

        renderer(sample_cluster, output, self.environment, format="dot")

        # Verify that the invalid names were properly corrected
        self.assertIn("subgraph cluster_wiki_db {",
                      output.getvalue())
        self.assertIn('wiki_cache -> "wiki_cache/0"',
                      output.getvalue())

    @inlineCallbacks
    def test_render_svg(self):
        yield self.build_topology()
        self.mock_environment()
        self.mocker.replay()

        yield status.status(self.environment, [],
                            status.renderers["svg"],
                            self.output,
                            None)

        #dump_stringio(self.output, "/tmp/ens.svg")

        # look for a hint the process completed.
        self.assertIn("</svg>", self.output.getvalue())


from tkinter import *
from tkinter import messagebox

class Gui(Tk):
    def __init__(self):
        super().__init__()

        self.title("Gui")

        self.map_image = PhotoImage(file="U:\Programming\com404\\2-guis\\4-images\\map.png")
        self.bus_image = PhotoImage(file="U:\Programming\com404\\2-guis\\4-images\\bus1.png")

        self.__add_main_frame()
        self.__add_heading_label()
        self.__add_map_frame()
        self.__add_map_image_label()
        self.__add_bus_image_label()

    def __add_main_frame(self):
        self.main_frame = Frame()
        self.main_frame.pack()
        self.main_frame.configure(highlightthickness=4,highlightbackground="black",bg="white")

    def __add_heading_label(self):
        self.heading_label = Label(self.main_frame)
        self.heading_label.grid(row=0,column=0,columnspan=3)
        self.heading_label.configure(text="Bus Journey",background="white", font="Arial 24")

    def __add_map_frame(self):
        self.map_frame = Frame(self.main_frame)
        self.map_frame.grid(row=1,column=0,padx=50,pady=20)
        self.map_frame.configure(width=715,height=583)

    def __add_map_image_label(self):
        self.map_image_label = Label(self.map_frame)
        self.map_image_label.place(x=0,y=0)
        self.map_image_label.configure(image=self.map_image)

    def __add_bus_image_label(self):
        self.bus_image_label = Label(self.map_frame)
        self.bus_image_label.place(x=20,y=20)
        self.bus_image_label.configure(image=self.bus_image)
        self.bus_image_label.bind('<B1-Motion>', self.change_bus_position)

    def change_bus_position(self,Event):
        mouse_y = str(Event.y)
        mouse_x = str(Event.x)

        self.bus_image_label.place(x=mouse_x,y=mouse_y)

if (__name__ == "__main__"):
    gui = Gui()
    gui.mainloop()


import numpy as np

def peakElement(array, l, r):
    if(l >= r):
        return l

    nby2 = int((l + r)/2)
    if(array[nby2] >= array[nby2 - 1] and array[nby2] >= array[nby2 + 1]):
        return nby2
    else:
        if(array[nby2] < array[nby2 -1]):
            return peakElement(array, l, nby2 - 1)
        else:
            return peakElement(array, nby2 + 1, r)

size = 21
array = np.random.randint(50, size=size)
index = peakElement(array, 0, size-1)
print(array)
if(index > 0 and index < 19):
    print(index)
    print(array[index-1:index+2])
else:
    if(index == 0):
        print(index)
        print(array[:2])
    else:
        print(index)
        print(array[-2:])


import os
import sys
import ctypes
from ctypes import pythonapi, util, py_object

import cv2

# Demo variables
lang = "eng"
output = "dump.config"
filename = "test/table.png"
libpath = "/usr/local/lib/"
tessdata = "/usr/local/share/"

libname = libpath + "libtesseract.so.3.0.2"
libname_alt = "libtesseract.so.3"

try:
	tesseract = ctypes.cdll.LoadLibrary(libname)
except:
	tesseract = ctypes.cdll.LoadLibrary(libname_alt)

tesseract.TessVersion.restype = ctypes.c_char_p
tesseract_version = tesseract.TessVersion()

print("Found tesseract-ocr library version %s." % tesseract_version)

api = tesseract.TessBaseAPICreate()

rc = tesseract.TessBaseAPIInit3(api, tessdata, lang)
if (rc):
	tesseract.TessBaseAPIDelete(api)
	print("Could not initialize tesseract.\n")
	exit(3)

img = cv2.imread(filename)

subimage = img[0:100][0:70]

# cv2.imshow('Img', subimage)
# cv2.waitKey(0)

height, width, channels = subimage.shape
print(str(height)+", "+str(width))
bytesPerPixel = channels

data = str(subimage.data)

tess_set_image = tesseract.TessBaseAPISetImage(api, data, width, height, bytesPerPixel, bytesPerPixel*width)
text_out = tesseract.TessBaseAPIGetUTF8Text(api)
result = ctypes.string_at(text_out)
print(result)


from flask import current_app as app
from flask import Blueprint, render_template

pages = Blueprint('pages', __name__, url_prefix='', template_folder='templates')

@pages.route('/')
@pages.route('/index')
def index():
    # Testing logging levels
    app.logger.warning('I AM WARNING***')
    app.logger.debug('I AM DEBUG***')
    app.logger.error('I AM ERROR***')
    app.logger.info('I AM INFO***')
    return render_template('index.html', config=app.config)


import json
import pandas as pd
import os

from .data.get_input_data import get_input_data
from ..src.TEO_Model import buildmodel
from ..src.utilities import (
    create_parameters_dataframe,
    create_parameters_default_dataframe,
    create_sets_dataframe,
)
from .prepare_inputs import *

def run_test():
    fdef = open(
        os.path.join(os.path.dirname(__file__), "json/parameters_default.json"), "r"
    )
    jsdef = json.load(fdef)
    input_data = get_input_data()

    # Start Module Prepare Input
    gis_module = input_data["gis-module"]
    cf_module = input_data["cf-module"]
    platform = input_data["platform"]

    default_df = create_parameters_default_dataframe(jsdef)
    jsset = platform["platform_sets"]
    jsset["TECHNOLOGY"] = cf_module["sets_technologies"]
    jsset["FUEL"] = cf_module["sets_fuels"]

    sets_df = create_sets_dataframe(jsset)

    df = create_parameters_dataframe(sets_df, default_df)

    df = prepare_inputs(sets_df, df, input_data)

    pd.set_option("display.max_rows", df.shape[0] + 1)
    print("GENERATED PARAMETERS", df)

    # MCS and N are not needed
    # buildmodel(sets_df, df, default_df, None, 0)


from django.utils.translation import gettext_lazy as _

from .exceptions import MissingSettingNames, MultipleSettingsWithDefaults
from .mixins import BaseSettingMixin

__all__ = ('setting_mixin_factory',)

def setting_mixin_factory(*names, setting='', default='', **kwargs):
    """Create a setting mixin from the provided name(s) and kwargs.

    Take a series of setting names and create a mixin that already
    handles all of those names. If only one setting name is provided,
    then the setting label and default may also be passed into the
    factory.
    """

    if len(names) == 0:
        raise MissingSettingNames(_(
            'setting_mixin_factory() expects to receive the name of '
            'at least one setting. Please provide the keyword that '
            'the setting value will be assigned to.'
        ))

    if len(names) > 1 and (setting != '' or default != ''):
        raise MultipleSettingsWithDefaults(_(
            'When setting_mixin_factory() is called with %(count)s '
            "setting names it doesn't expect to handle the setting "
            'label and default as well.'
        ) % {"count": len(names)})

    for name in names:
        kwargs.update({
            f'{name}_default': default,
            f'{name}_setting': setting,
        })

    kwargs['_setting_names'] = names
    return type('SettingMixin', (BaseSettingMixin,), kwargs)


import numpy as np
import random

class ModernHopfield:
    def __init__(self, size = -1, bias = 0) -> None:
        self.size = size
        self.bias = bias
        self.threshold = 0
        self.weights = None
        self.memories = 0

    def train(self, pattern):

        if(self.memories == 0):
            self.patterns = np.array([pattern])
        else:
            self.patterns = np.append(self.patterns, [pattern], axis=0)
        self.memories += 1

    def query(self, query, maxIterations):

        for iteration in range(maxIterations):
            update_order = np.arange(0, self.size)
            random.shuffle(update_order)
            for l in update_order:
                new_neurons_positive = query.copy()
                new_neurons_negative = query.copy()

                new_neurons_positive[0][l] = 1
                positive_energy = self.energy_function(new_neurons_positive)
                new_neurons_negative[0][l] = -1
                negative_energy = self.energy_function(new_neurons_negative)

                query[0][l] = np.sign((-1 * positive_energy) + negative_energy)

                # print(f'hit: {iteration }: {l} : {self.energy_function(query)} \t {query}')

        return query.transpose()

    def energy_function(self, query, power=2):
        energy = 0
        query_transpose = query.transpose()
        for pattern_i in range(self.memories):
            pattern = self.patterns[pattern_i]
            part01 = pattern @ query_transpose
            energy += np.power(part01, power)

            # weights_transpose = self.weights.transpose()
            # part01 = weights_transpose @ query
            # part02 = np.power(part01, power)
            # part03 = np.sum(part02)
        return energy * -1


import logging as lg

lg.basicConfig(
    level=lg.DEBUG,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    datefmt="%H:%M:%S")
_logger = lg.getLogger(__name__)

class Node:
    def __init__(self, children, metadata):
        self.children = children
        self.metadata = metadata

    def sum_metadata(self):
        return sum(self.metadata) + sum([c.sum_metadata() for c in self.children])

    def get_value(self):
        _logger.debug("Node ({} children) metadata: {}".format(len(self.children), self.metadata))
        if self.children:
            cs = self.children
            return sum([cs[idx - 1].get_value() for idx in self.metadata if 0 <= idx - 1 < len(cs)])
        else:
            return sum(self.metadata)

class Parser:
    _node_class = Node

    def __init__(self, data):
        self.data = data
        self.root = None

    @classmethod
    def from_data_str(cls, data_str):
        return cls(list(map(int, data_str.strip().split(" "))))

    def _build_node(self, j):
        n_children = self.data[j]
        n_metadata = self.data[j + 1]
        # _logger.debug("Node at {}: {} children, {} metadata".format(j, n_children, n_metadata))
        children = []
        k = 2
        for _ in range(n_children):
            child, size = self._build_node(j + k)
            children.append(child)
            k += size
        metadata = self.data[j + k:j + k + n_metadata]
        node = self._node_class(children, metadata)
        return node, k + n_metadata

    def parse(self):
        node, size = self._build_node(0)
        assert size == len(self.data)
        self.root = node

with open("input_day8.txt", "r") as f:
    data_str = f.read()
parser = Parser.from_data_str(data_str)
parser.parse()
print("Answer pt1:", parser.root.sum_metadata())

print("Answer pt2:", parser.root.get_value())


import pygame
pygame.init()

#### -------------------------- ####
#### ---- CIRCLES FUNCTION ---- ####
#### -------------------------- ####

# Add a flag parameter
def circles(window, corner, flag):

    if corner == 1:
        x = 100
        y = 100
        modify_x = 100
        modify_y = 100

    elif corner == 2:
        x = 400
        y = 100
        modify_x = -100
        modify_y = 100

    elif corner == 3:
        x = 400
        y = 400
        modify_x = -100
        modify_y = -100

    elif corner == 4:
        x = 100
        y = 400
        modify_x = 100
        modify_y = -100

    # Draw circles
    size = 120
    for i in range(5):

        # Draw regular circle or outline
        if flag:
            pygame.draw.circle(window, (0, 0, 255), (x, y), size, 2)
        else:
            pygame.draw.circle(window, (0, 0, 255), (x, y), size)

        x += modify_x
        y += modify_y
        size -= 20

#### ---------------------- ####
#### ---- MAIN PROGRAM ---- ####
#### ---------------------- ####
w = pygame.display.set_mode([500, 500])

start_val = 100
flag = True
start = 0

# Main loop
drawing = True
while drawing:

    # Event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            drawing = False

    # Draw frame
    w.fill((0, 0, 0))

    # Add flag argument
    circles(w, start + 1, flag)

    # Change the style and location of next group
    flag = not flag
    start = ((start + 1) % 4)

    # Finish frame
    pygame.display.flip()
    pygame.time.wait(1000)


import streamlit as st

from PIL import Image
import time

st.title('青木まひろの人生相談:Streamlitの使い方  入門')

st.write('プログレスバーの表示')
'start'
latest_iteration=st.empty()
bar=st.progress(0)

for i in range(100):
    latest_iteration.text(f'Iteration{i+1}')
    bar.progress(i+1)
    time.sleep(0.05)
'Done'

left_column, right_column=st.columns(2)
button=left_column.button('右カラむに文字表示')
if button:
    right_column.write('ここは右カラむです')

img=Image.open(r"C:\Users\amori\Programing\StreamlitPractice\IMG_2057.jpg")
expander1=st.expander('問い合わせ1')
expander1.write('問い合わせ回答1')
expander2=st.expander('問い合わせ2')
expander2.write('問い合わせ回答2')
expander2.image(img, caption='aoki masahiro', use_column_width=True)

expander3=st.expander('問い合わせ3')
expander3.write('問い合わせ回答3')


import cv2
from PIL import Image, ImageTk
import numpy
import tkinter.filedialog
import tkinter as tk
import os
import threading
import time
from tkinter import ttk

window = tk.Tk()

def getfilesname(path):
    filesname =[]
    if(path != ''):
        dirs = os.listdir(path)
        for i in dirs:
            if os.path.splitext(i)[1] == ".jpg" or os.path.splitext(i)[1] == ".png" or os.path.splitext(i)[1] == ".JPG" or os.path.splitext(i)[1] == ".jpeg":
                filesname+=[path+'/'+i]
    else:
        filesname = []
    return filesname

def selectPath():
    global pathh
    global path
    global image_file1,image_file2,image_file3,image_file4
    global img1,img2,img3,img4
    global filesname
    global img_open1,img_open2,img_open3,img_open4
    global box,box2,box3,box4,scale
    global w_canvas,h_canvas,maxscale
    scale =maxscale
    box = (0, 0, w_canvas*scale, h_canvas*scale)
    box2 = box
    box3 = box
    box4 = box
    path = tkinter.filedialog.askdirectory()
    pathh.set(path)

    filesname = getfilesname(path)

    while(len(filesname) == 0 or len(filesname)%4 != 0):
        path = tkinter.filedialog.askdirectory()
        pathh.set(path)
        filesname = getfilesname(path)

    img1 = cv2.imread(filesname[0],0)
    img2 = cv2.imread(filesname[1],0)
    img3 = cv2.imread(filesname[2],0)
    img4 = cv2.imread(filesname[3],0)

    img_open1 = Image.open(filesname[0])
    img_open2 = Image.open(filesname[1])
    img_open3 = Image.open(filesname[2])
    img_open4 = Image.open(filesname[3])

    image_file1 = create(img_open1,box)
    image_file2 = create(img_open2,box2)
    image_file3 = create(img_open3,box3)
    image_file4 = create(img_open4,box4)

    putimage()

def print_selection():
    print('you have selected ' + var.get())

def create(img_open,box):
    global w_canvas,h_canvas
    region = img_open.crop(box)

    region1 = region.resize((w_canvas, h_canvas))

    image_file = ImageTk.PhotoImage(image = region1)
    return image_file

def putimage():
    global canvas1,canvas2,canvas3,canvas4
    global image1,image2,image3,image4
    global box
    global w_canvas,h_canvas,maxscale

    canvas1.delete(image1)
    canvas2.delete(image2)
    canvas3.delete(image3)
    canvas4.delete(image4)

    image1 = canvas1.create_image(w_canvas, h_canvas, anchor='se',image=image_file1)

    image2 = canvas2.create_image(0, h_canvas, anchor='sw',image=image_file2)

    image3 = canvas3.create_image(w_canvas, 0, anchor='ne',image=image_file3)

    image4 = canvas4.create_image(0, 0, anchor='nw',image=image_file4)

def hit_me():
    global var1,var2,var3,var4,var,box

    print(var1.get(),var2.get(),var3.get(),var4.get(),cmb.get())
    print(box)

def drag(event):
    global box,box2,box3,box4
    global img1,img2,img3,img4
    global image_file1,image_file2,image_file3,image_file4
    global loc
    global scale
    global w_canvas,h_canvas,maxscale
    nowloc=[]
    if (event.widget == canvas1 or event.widget == canvas2 or event.widget == canvas3 or event.widget == canvas4):

        nowloc.append(event.x)
        nowloc.append(event.y)
        loc.append(nowloc)
        if (len(loc) >= 3):
            del loc[0]
        boxx = list(box)
        if (len(loc) == 2):
            if boxx[0]+(loc[0][0]-loc[1][0])*scale<=0:
                boxx[0] = 0
            elif boxx[0]+(loc[0][0]-loc[1][0])*scale>= w_img-w_canvas*scale:
                boxx[0] = w_img-w_canvas*scale
            else:
                boxx[0] = boxx[0]+(loc[0][0]-loc[1][0])*scale

            if boxx[1]+(loc[0][1]-loc[1][1])*scale <= 0:
                boxx[1] = 0
            elif boxx[1]+(loc[0][1]-loc[1][1])*scale>= h_img-h_canvas*scale:
                boxx[1]= h_img-h_canvas*scale
            else:
                boxx[1] = boxx[1]+(loc[0][1]-loc[1][1])*scale

            boxx[2] = boxx[0] +w_canvas*scale
            boxx[3] = boxx[1] +h_canvas*scale

            box = tuple(boxx)

            box2 = box
            box3 = box
            box4 = box
            # if (abs(time.perf_counter()-timer) >10):

            #     box2 = match(img1,img2,box)
            #     box3 = match(img1,img3,box)
            #     box4 = match(img1,img4,box)
            #     timer = time.perf_counter()

            image_file1 = create(img_open1,box)
            image_file2 = create(img_open2,box2)
            image_file3 = create(img_open3,box3)
            image_file4 = create(img_open4,box4)

            putimage()

def scaler(event):
    global scale
    global box,box2,box3,box4
    global img1,img2,img3,img4
    global image_file1,image_file2,image_file3,image_file4
    global w_canvas,h_canvas,maxscale,w_win,h_win

    # print(event.x_root,event.y_root,event.widget,event.type)
    # print(event.widget == canvas1)
    if (event.widget == canvas1 or event.widget == canvas2 or event.widget == canvas3 or event.widget == canvas4):
        boxx = list(box)

        oldscale = scale

        if(event.delta > 0 and oldscale >1):
            scale = max(oldscale-1,1)
            boxx[0] = boxx[0] + event.x
            boxx[1] = boxx[1] + event.y

        if(event.delta < 0 and oldscale < maxscale):
            scale = min(oldscale+1,maxscale)
            if (boxx[0] - event.x <0):
                boxx[0]=0
            elif(boxx[0] - event.x > w_img-w_canvas*scale):
                boxx[0]=w_img-w_canvas*scale
            else:
                boxx[0] = boxx[0] - event.x

            if (boxx[1] - event.y <0):
                boxx[1]=0
            elif(boxx[1] - event.y > h_img-h_canvas*scale):
                boxx[1]=h_img-h_canvas*scale
            else:
                boxx[1] = boxx[1] - event.y

        boxx[2] = boxx[0]+w_canvas*scale
        boxx[3] = boxx[1]+h_canvas*scale

        box = tuple(boxx)
        box2 = box
        box3 = box
        box4 = box
        # box2 = match(img1,img2,box)
        # box3 = match(img1,img3,box)
        # box4 = match(img1,img4,box)

        image_file1 = create(img_open1,box)
        image_file2 = create(img_open2,box2)
        image_file3 = create(img_open3,box3)
        image_file4 = create(img_open4,box4)

        putimage()

def locclear(event):
    global loc
    loc.clear()

def match(img1,img2,box1):
    img = img2
    template = img1[box1[1]:box1[3],box1[0]:box1[2]]
    w, h = template.shape[::-1]

    methods = ['cv2.TM_SQDIFF']
#methods = ['cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED',
#            'cv2.TM_CCORR','cv2.TM_CCORR_NORMED',
#            'cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED']

    for meth in methods:
        method = eval(meth)
        res = cv2.matchTemplate(img,template,method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) #找到最大值和最小值

        if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
            top_left = min_loc
        else:
            top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)
    box=top_left+bottom_right

    return box

def update():
    global box,box2,box3,box4
    global img1,img2,img3,img4,flag
    global image_file1,image_file2,image_file3,image_file4

    box2 = match(img1,img2,box)
    box3 = match(img1,img3,box)
    box4 = match(img1,img4,box)

    image_file1 = create(img_open1,box)
    image_file2 = create(img_open2,box2)
    image_file3 = create(img_open3,box3)
    image_file4 = create(img_open4,box4)

    putimage()

window.title('My Window')

w_win = window.winfo_screenwidth()
h_win = window.winfo_screenheight()

big = str(w_win) + 'x' + str(h_win)
window.geometry(big)

w_canvas =int(w_win*0.4)
h_canvas = int(h_win*0.4)

#path = 'C:/Users/37151/Desktop/tkinter/sense1'
path = os.getcwd() + '/sense1'

pathh = tk.StringVar()
tk.Label(window,text = "目标路径:").place(x=0.2*w_win, y=0.025*h_win, anchor='w')
tk.Entry(window, textvariable = pathh).place(x=0.3*w_win, y=0.025*h_win,width = 300, anchor='w')
tk.Button(window, text = "路径选择", command = selectPath).place(x=0.6*w_win, y=0.025*h_win, anchor='w')

filesname=getfilesname(path)

img1 = cv2.imread(filesname[0],0)
img2 = cv2.imread(filesname[1],0)
img3 = cv2.imread(filesname[2],0)
img4 = cv2.imread(filesname[3],0)

img_open1 = Image.open(filesname[0])
img_open2 = Image.open(filesname[1])
img_open3 = Image.open(filesname[2])
img_open4 = Image.open(filesname[3])

w_img = img_open1.size[0]
h_img = img_open1.size[1]

maxscale = min(int(w_img/w_canvas),int(h_img/h_canvas))

scale = maxscale

loc = []

box = (0, 0, w_canvas*scale, h_canvas*scale)
box2 = (0, 0, w_canvas*scale, h_canvas*scale)
box3 = (0, 0, w_canvas*scale, h_canvas*scale)
box4 = (0, 0, w_canvas*scale, h_canvas*scale)

tk.Label(text='评分属性(请下拉选择)：').place(x=0.85*w_win, y=0.15*h_win, anchor='w')
cmb = ttk.Combobox(window)
cmb.place(x=0.85*w_win, y=0.2*h_win, anchor='w')
# 设置下拉菜单中的值
cmb['value'] = ('noise','detail','expor','color')

var1 = tk.StringVar()
#mark1 = tk.Scale(window,from_=0,  to=5,  resolution=0.5, orient=tk.HORIZONTAL , variable=var1 ,length = 300,showvalue=1,tickinterval=0.5)
mark1 = tk.Scale(window,from_=0,  to=5,  resolution=0.5, orient=tk.HORIZONTAL , variable=var1 ,length = 400,showvalue=0,tickinterval=0.5)
mark1.place(x=0.1*w_win, y=0.075*h_win, anchor='w')

var2 = tk.StringVar()
mark2 = tk.Scale(window,from_=0,  to=5,  resolution=0.5, orient=tk.HORIZONTAL , variable=var2,length = 400,showvalue=0,tickinterval=0.5)
mark2.place(x=0.5*w_win, y=0.075*h_win, anchor='w')

var3 = tk.StringVar()
mark3 = tk.Scale(window,from_=0,  to=5,  resolution=0.5, orient=tk.HORIZONTAL , variable=var3,length = 400,showvalue=0,tickinterval=0.5)
mark3.place(x=0.1*w_win, y=0.925*h_win, anchor='w')

var4 = tk.StringVar()
mark4 = tk.Scale(window,from_=0,  to=5,  resolution=0.5, orient=tk.HORIZONTAL , variable=var4,length = 400,showvalue=0,tickinterval=0.5)
mark4.place(x=0.5*w_win, y=0.925*h_win, anchor='w')

image_file1 = create(img_open1,box)
image_file2 = create(img_open2,box2)
image_file3 = create(img_open3,box3)
image_file4 = create(img_open4,box4)

canvas1 = tk.Canvas(window, height=h_canvas, width=w_canvas)
image1 = canvas1.create_image(w_canvas, h_canvas, anchor='se',image=image_file1)
canvas1.place(x=0.4*w_win, y=0.5*h_win, anchor='se')

canvas2 = tk.Canvas(window,height=h_canvas, width=w_canvas)
image2 = canvas2.create_image(0, h_canvas, anchor='sw',image=image_file2)
canvas2.place(x=0.4*w_win, y=0.5*h_win, anchor='sw')

canvas3 = tk.Canvas(window, height=h_canvas, width=w_canvas)
image3 = canvas3.create_image(w_canvas, 0, anchor='ne',image=image_file3)
canvas3.place(x=0.4*w_win, y=0.5*h_win, anchor='ne')

canvas4 = tk.Canvas(window, height=h_canvas, width=w_canvas)
image4 = canvas4.create_image(0, 0, anchor='nw',image=image_file4)
canvas4.place(x=0.4*w_win, y=0.5*h_win, anchor='nw')

b = tk.Button(window, text='upload(点一下就行)', font=('Arial', 12), command=hit_me)
b.place(x=0.85*w_win, y=0.45*h_win, anchor='w')

b0= tk.Button(window, text='match', font=('Arial', 12), width = 5,command=update)
b0.place(x=0.85*w_win, y=0.4*h_win, anchor='w')

window.bind("<MouseWheel>",scaler)
window.bind("<B1-Motion>",drag)
window.bind("<ButtonRelease-1>",locclear)

#jsonfile=open(os.getcwd(),'w')

window.attributes("-topmost",True)
window.mainloop()


from random import seed
from random import randrange
from csv import reader

# Load a CSV file

def load_csv(filename):
    file = open(filename, "rt")
    lines = reader(file)
    dataset = list(lines)
    return dataset

# Convert string column to float

def str_column_to_float(dataset, column):
    for row in dataset:
        row[column] = float(row[column].strip())

# Split a dataset into k folds

def cross_validation_split(dataset, n_folds):
    dataset_split = list()
    dataset_copy = list(dataset)
    fold_size = int(len(dataset) / n_folds)
    for i in range(n_folds):
        fold = list()
        while len(fold) < fold_size:
            index = randrange(len(dataset_copy))
            fold.append(dataset_copy.pop(index))
        dataset_split.append(fold)
    return dataset_split

# Calculate accuracy percentage

def accuracy_metric(actual, predicted):
    correct = 0
    for i in range(len(actual)):
        if actual[i] == predicted[i]:
            correct += 1
    return correct / float(len(actual)) * 100.0

# Evaluate an algorithm using a cross validation split

def evaluate_algorithm(dataset, n_folds):
    folds = cross_validation_split(dataset, n_folds)
    scores = list()
    for fold in folds:
        train_set = list(folds)
        train_set.remove(fold)
        train_set = sum(train_set, [])
        test_set = list(fold)
        predicted = decision_tree(train_set, test_set)
        actual = [row[feature_intdx] for row in fold]
        accuracy = accuracy_metric(actual, predicted)
        scores.append(accuracy)
    return scores

# Split a dataset based on an attribute and an attribute value

def test_split(index, value, dataset):
    left, right = list(), list()
    for row in dataset:
        if row[index] < value:
            left.append(row)
        else:
            right.append(row)
    return left, right

# Calculate the Gini index for a split dataset

def gini_index(groups, classes):
    # count all samples at split point
    n_instances = float(sum([len(group) for group in groups]))
    # sum weighted Gini index for each group
    gini = 0.0
    for group in groups:
        size = float(len(group))
        # avoid divide by zero
        if size == 0:
            continue
        score = 0.0
        # score the group based on the score for each class
        for class_val in classes:
            p = [row[feature_intdx] for row in group].count(class_val) / size
            score += p * p
        # weight the group score by its relative size
        gini += (1.0 - score) * (size / n_instances)
    return gini

# Select the best split point for a dataset

def get_split(dataset):
    class_values = list(set(row[feature_intdx] for row in dataset))
    b_index, b_value, b_score, b_groups = 9999999, 9999999, 9999999, None
    for index in range(len(dataset[0])):
        if index == feature_intdx:
            continue
        for row in dataset:
            groups = test_split(index, row[index], dataset)
            gini = gini_index(groups, class_values)
            if gini < b_score:
                b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    return {'index': b_index, 'value': b_value, 'groups': b_groups}

# Create a terminal node value

def to_terminal(group):
    return group[0][feature_intdx]

# Create child splits for a node or make terminal

def split(node):
    left, right = node['groups']
    del(node['groups'])
    # check for a no split
    if not left or not right:
        node['left'] = node['right'] = to_terminal(left + right)
        return

    node['left'] = get_split(left)
    split(node['left'])

    node['right'] = get_split(right)
    split(node['right'])

# Build a decision tree

def build_tree(train):
    root = get_split(train)
    split(root)
    return root

# Make a prediction with a decision tree

def predict(node, row):
    if row[node['index']] < node['value']:
        if isinstance(node['left'], dict):
            return predict(node['left'], row)
        else:
            return node['left']
    else:
        if isinstance(node['right'], dict):
            return predict(node['right'], row)
        else:
            return node['right']

# Classification and Regression Tree Algorithm

def print_tree(node, depth=0):

	if isinstance(node, dict):
		print('level:%d %s[X%d < %.3f]' % (depth, depth*'   ', (node['index']+1), node['value']))
		print_tree(node['left'], depth+1)
		print_tree(node['right'], depth+1)
	else:
		print('%s[%s]' % ((depth*'   ', node)))

def decision_tree(train, test):
    tree = build_tree(train)
    print_tree(tree)
    predictions = list()
    for row in test:
        prediction = predict(tree, row)
        predictions.append(prediction)
        print('The row: ',row)
    newRow=[2.0, 12.86, 1.33, 22.97, 23.0, 40.0, 2.64, 5.78, 0.26, 1.50, 3.01, 0.0, 20.99, 0.0]
    newPrediction=predict(tree,newRow)
    print('New wine predicted class: ',newPrediction)
    return(predictions)

seed(1)
# load and prepare data
filename = 'wine.data'
dataset = load_csv(filename)
# convert string attributes to integers
for i in range(len(dataset[0])):
    str_column_to_float(dataset, i)

# evaluate algorithm
n_folds = 10

'''
feature index in 0 is indicate the class name

'''
feature_intdx = 0
scores = evaluate_algorithm(dataset, n_folds)
print('Scores: %s' % scores)
print('Mean Accuracy: %.3f%%' % (sum(scores)/float(len(scores))))

'''

khonika=[2.0, 12.86, 1.33, 1.97, 23.0, 80.0, 2.64, 2.78, 0.26, 1.50, 3.01, 1.25, 2.99, 350.0]

'''


import sys
import os

sys.path.append(os.path.abspath("."))
sys.dont_write_bytecode = True

__author__ = "COSAL"

from analysis.blocks import block_utils
from analysis.helpers import constants, helper, execution_utils, file_utils
from utils import cache, logger
import properties

import random

LOGGER = logger.get_logger(os.path.basename(__file__.split(".")[0]))
DEBUG = True

def set_metadata_for_function(func_meta):
  arg_keys = execution_utils.get_function_arg_and_key(func_meta)
  grouped_arg_keys = execution_utils.group_arg_and_key(arg_keys)
  if execution_utils.has_too_many_arguments(grouped_arg_keys):
    return None
  function_input_key = execution_utils.get_function_args_key(grouped_arg_keys)
  if not function_input_key:
    return None
  func_meta.input_key = function_input_key
  args_permutation = execution_utils.permutate_args_and_key(grouped_arg_keys)
  if not args_permutation:
    return None
  func_meta.args_permutation = args_permutation[0]
  return func_meta

def check_output_validity(gen_func, results, args):
  assert len(results) == len(args)
  arg_matches = [True] * len(args[0])
  previous_result_match = True
  for res_index, res in enumerate(results):
    if res_index > 0 and previous_result_match:
      previous_result_match = res["return_json"] == results[res_index - 1]["return_json"]
    if any(arg_matches):
      for arg_index, arg in enumerate(args[res_index]):
        if not arg_matches[arg_index]:
          continue
        formatted = block_utils.format_value_as_json(arg, gen_func, arg_index)
        arg_matches[arg_index] = res["return_json"] == formatted
    if (not any(arg_matches)) and (not previous_result_match):
      return True
  return False

def is_valid_function(func_meta, func_obj):
  # 1. Set metadata
  func_meta = set_metadata_for_function(func_meta)
  if not func_meta:
    return False
  if len(func_meta.parameters) > properties.MAX_ARGUMENT_SIZE:
    return False
  # 2. Get function args
  args = execution_utils.get_function_args(func_meta)
  if not args:
    return False
  # 3. Execute functions
  results = []
  for arg_index, arg in enumerate(args):
    exec_results = execution_utils.execute_function(func_obj, arg)
    exec_results["return_json"] = block_utils.format_value_as_json(exec_results["return"], func_meta, arg_index)
    results.append(exec_results)
  if not results:
    return False
  # 4. Check if function changes
  cloned_args = execution_utils.get_function_args(func_meta)
  return check_output_validity(func_meta, results, cloned_args)

def filter_file(file_path):
  file_name = cache.get_file_name(file_path)
  if not file_name.startswith(constants.GENERATED_PREFIX):
    LOGGER.info("'%s'is not a generated file. Not validating the snippets!" % cache.get_repo_local_path(file_path))
    return
  LOGGER.info("Filtering file '%s' ... " % cache.get_repo_local_path(file_path))
  STORE = execution_utils.get_function_store()
  file_path = cache.get_repo_local_path(file_path)
  gen_funcs_meta = execution_utils.load_generated_functions_meta(file_path)
  generated_functions = helper.get_generated_functions(cache.get_absolute_path(file_path), as_dict=True)
  invalid_func_names = []
  bodies = []
  source_file = None
  func_index = 0
  for func_name, gen_func_obj in generated_functions.items():
    if len(bodies) == func_index == 5:
      LOGGER.info("'%s'is already filtered!" % cache.get_repo_local_path(file_path))
      return
    func_index += 1
    if func_name not in gen_funcs_meta:
      continue
    # if func_name != "func_26ff925edf694867b1fbcf4e828fefb8":
    #   continue
    # print(func_name)
    if func_index % 10 == 0:
      LOGGER.info("Filtering function %d / %d. # Valids = %d .... "
                  % (func_index, len(generated_functions), len(bodies)))
    gen_func_meta = gen_funcs_meta[func_name]
    if source_file is None:
      source_file = gen_func_meta.source_file
    file_utils.delete_input_temp_files(gen_func_meta.name)
    is_func_valid = is_valid_function(gen_func_meta, gen_func_obj)
    file_utils.delete_input_temp_files(gen_func_meta.name)
    if is_func_valid:
      bodies.append(gen_func_meta.body)
    else:
      invalid_func_names.append(func_name)
    # print(func_name, is_func_valid)
    # exit()
  if bodies and source_file:
    LOGGER.info("Saving '%d' functions ..." % len(bodies))
    local_python_file = source_file.split(properties.PYTHON_PROJECTS_HOME)[-1][1:].split(".")[0]
    headers = ["import sys",
               "sys.path.append('%s')" % properties.PYTHON_PROJECTS_HOME,
               "from %s import *" % local_python_file.replace(os.path.sep, ".")]
    content = "\n".join(headers) + "\n\n" + "\n\n".join(bodies)
    cache.write_file(cache.get_absolute_path(file_path), content)
  else:
    LOGGER.info("Deleting '%s' since it contains 0 valid files!" % file_path)
    cache.delete_file(cache.get_absolute_path(file_path))
  LOGGER.info("Deleting '%d' functions ..." % len(invalid_func_names))
  for func_name in invalid_func_names:
    STORE.del_py_function_metadata(func_name)

def filter_folder(folder_path):
  files = cache.list_files(folder_path, check_nest=True, is_absolute=True,
                           prefix=constants.GENERATED_PREFIX, extension=".py")
  # random.shuffle(files)
  for file_path in files:
    filter_file(file_path)

def filter_dataset():
  LOGGER.info("Filtering generated files for dataset: '%s' ... " % properties.CONFIG.get_dataset())
  filter_folder(properties.PYTHON_PROJECTS_HOME)

def _main():
  filter_dataset()

def _test():
  filter_file("/Users/panzer/Raise/ProgramRepair/CodeSeer/projects/src/main/python/CodeJam/Y13R5P1/gepa/generated_py_b362034eadb14e039a607fb083331620.py")

if __name__ == "__main__":
  # _test()
  _main()


"""
Fetching data from the website of AMFI India in the format of .txt
Inputs-none
Output-.txt file--> NAV_ddmmyy.txt
"""

import requests
import datetime

today=datetime.datetime.now()
today=today.strftime("%d%m%y")
print(today)

txt_filename="NAV"+"_"+today+".txt"
print(txt_filename)

url="https://www.amfiindia.com/spages/NAVAll.txt"
r=requests.get(url)
with open(txt_filename,'wb') as f:
	f.write(r.content)

f.close()


"""additional texts

Revision ID: aaf8cfeb6ba5
Revises: eb713944b6c6
Create Date: 2021-07-23 12:52:01.459077

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'aaf8cfeb6ba5'
down_revision = 'eb713944b6c6'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('location', sa.Column('terms_and_conditions', sa.String(length=255), nullable=True))
    op.add_column('location', sa.Column('bike_size_information', sa.Text(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('location', 'bike_size_information')
    op.drop_column('location', 'terms_and_conditions')
    # ### end Alembic commands ###


def touch(preview):
	import struct
	import time
	import sys

	infile_path = "/dev/input/event0"
	FORMAT = 'llHHI'
	EVENT_SIZE = struct.calcsize(FORMAT)
	in_file = open(infile_path, "rb")
	event = in_file.read(EVENT_SIZE)

	x=0
	y=0
	global screen_w
	global screen_h

	touchscreen_res=4000
	last_touch = 0

	while event:
		(tv_sec, tv_usec, typee, code, value) = struct.unpack(FORMAT, event)
		if typee != 0 or code != 0 or value != 0:
			#print("valid touch")
			#print("Event typee %u, code %u, value %u at %d.%d" % \
			#(typee, code, value, tv_sec, tv_usec)
			if typee==3:																	#is it a valid touch input
				if code==0:
					x=value
					x = int( float(x)/float(touchscreen_res)*float(screen_w) )        			#convert from touch to pixel
				if code==1:
					y=value
					y = int( float(y)/float(touchscreen_res)*float(screen_h) )         		#convert from touch to pixel
				if x!=0 and y!=0:														#we got a VALID TOUCH INPUT
					tv_sec_usec = tv_sec*1000000+tv_usec
					global minimized
					if minimized:
						if touch_is_in_button(renderer_rec,x,y):
							minimize(False)
					global dragging
					detect_drag(last_touch,tv_sec_usec,x,y)								#is it dragging?
					if dragging:
						reposition_preview(x,y+40)
					if camera.preview.fullscreen:
						if detect_double_tap(tv_sec_usec): 								#if it's full screen
							exit_full_screen()
					elif touch_is_in_camera(preview,x,y): 									#is also inside the window
						if is_first_touch(last_touch,tv_sec_usec):
							if touch_is_in_button(renderer_button1,x,y):
								button1()
							if touch_is_in_button(renderer_button2,x,y):
								button2()
							elif touch_is_in_button(renderer_button3,x,y):
								button3()
							else:
								show_button(renderer_button1,True)
								show_button(renderer_button2,True)
								show_button(renderer_button3,True)
					else:
						show_button(renderer_button1,False)
						show_button(renderer_button2,False)
						show_button(renderer_button3,False)
					x=0
					y=0
					last_touch=tv_sec_usec

		event = in_file.read(EVENT_SIZE)
	in_file.close()

clicked = 0
clicktime = 0
def detect_double_tap(tv_sec_usec):
	global clicked
	global clicktime
	clickdelay = 0.5
	clicked=clicked+1
	if clicked == 1:
		clicktime = tv_sec_usec;
	if (clicked > 1) and (100000 < tv_sec_usec - clicktime < clickdelay*1000000):
		clicked = 0
		clicktime = 0
		print("Double CLick")
		return True
	elif (clicked > 2) or (tv_sec_usec - clicktime > 1):
		clicked = 0
	return False

first_x = 0
first_y = 0
dragging = False

def is_first_touch(last_touch,tv_sec_usec):
	return tv_sec_usec-last_touch>50000

def detect_drag(last_touch,tv_sec_usec,x,y):
	#dragging=False
	global first_x
	global first_y
	global dragging
	if is_first_touch(last_touch,tv_sec_usec):
		first_x=x
		first_y=y
		dragging =False

	if (first_x-x)*(first_x-x) + (first_y-y)*(first_y-y) > 80*80:
		if touch_is_in_camera(renderer_preview,first_x,first_y):
			dragging = True

def touch_is_in_button(preview,x,y):
	margin=10
	if preview.window[0]-margin < x < preview.window[0]+preview.window[2] + margin:
		if preview.window[1]-margin < y < preview.window[1] +preview.window[3] + margin:
			return 1
	return 0

def touch_is_in_camera(preview,x,y):
	margin=0
	if preview.window[0]-margin < x < preview.window[0]+preview.window[2] + margin:
		if preview.window[1]-margin < y < preview.window[1]+preview.window[3] + margin:
			return 1
	return 0

def reposition_preview(x,y):
	buttton_size = 50
	adjust=15

	renderer_preview.window = (x-renderer_preview.window[2]/2, y-renderer_preview.window[3]/2, renderer_preview.window[2], renderer_preview.window[3])
	renderer_rec.window = renderer_preview.window[0]+adjust, renderer_preview.window[1]+adjust, renderer_rec.window[2], renderer_rec.window[3]

	buttons_y = renderer_preview.window[1]+renderer_preview.window[3]-buttton_size-adjust
	renderer_button1.window = renderer_preview.window[0]+adjust, buttons_y,renderer_button1.window[2], renderer_button1.window[3]
	renderer_button2.window = renderer_preview.window[0]+renderer_preview.window[2]/2-renderer_button2.window[2]/2, buttons_y, renderer_button2.window[2], renderer_button2.window[3]
	renderer_button3.window = renderer_preview.window[0]+renderer_preview.window[2]-renderer_button3.window[2]-15, buttons_y, renderer_button2.window[2], renderer_button2.window[3]

def button1():
	print('button1')
	minimize(True)

def button2():
	print('button 2')
	enter_full_screen()

def button3():
	print('button 3')
	action_with_arg = partial(open_video_manager)
	t3 = threading.Thread(target=action_with_arg)
	t3.start()
from video_manager import *
def open_video_manager():
	print('button 3')
	t = Interface()

global minimized
minimized = False
def minimize(state):
	if state:
		icon_size = 30
		icon_offset=20
		renderer_preview.window = (0,0,0,0)
		renderer_rec.window = screen_w-icon_size-icon_offset, icon_offset, icon_size, icon_size
		global minimized
		minimized = True
	else:
		global renderer_preview_size
		renderer_preview.window = renderer_preview_size
		reposition_preview(screen_w-renderer_preview_size[2]/2,renderer_preview_size[3]/2)
		minimized = False

def enter_full_screen():
	print('enter full screen')
	camera._set_preview_fullscreen(True)
	camera._set_preview_alpha(255)
	show_button(renderer_rec,False)
	show_button(renderer_button1,False)
	show_button(renderer_button2,False)
	show_button(renderer_button3,False)

def exit_full_screen():
	camera._set_preview_fullscreen(False)
	camera._set_preview_alpha(100)
	show_button(renderer_button1,True)
	show_button(renderer_button2,True)
	show_button(renderer_button3,True)

def show_button(renderer,state):
	if state:
		renderer.window = renderer.window[0],renderer.window[1],renderer.window[2],renderer.window[2]
	else:
		renderer.window = renderer.window[0],renderer.window[1],renderer.window[2],0

def timestamp(camera):
	camera.start_recording(path_h264+"/"+start_now_string+".h264")
	while (dt.datetime.now() - start_now).seconds < recording_length:
		camera.annotate_text = dt.datetime.now().strftime(format)

		#blink button
		if not camera.preview.fullscreen:
			if renderer_rec.window[3]!=0:
				show_button(renderer_rec,False)
			else:
				show_button(renderer_rec,True)

		camera.wait_recording(1)
	print("stop")
	camera.stop_recording()
	camera.close()
	if record_on_loop:
		timestamp(camera)
	else:
		quit()

global screen_w
global screen_h
global renderer_preview_size
global record_on_loop

######CHANGE THESE VARIABLES FOR CUSTOMIZATION#####
screen_w=800
screen_h=400
recording_length=1*60*60;
recording_length=10;
cam_resolution=(960,720)
renderer_preview_size=250
start_minimized = True
record_on_loop = True
#################################

import picamera
camera = picamera.PiCamera(resolution=cam_resolution, framerate=12)
format = '%Y_%m_%d_%H_%M_%S'

import os
import subprocess
path = os.path.dirname(os.path.abspath(__file__))
path_h264 = path+'/h264'
if not os.path.exists(path_h264):
	omxprocess = subprocess.Popen(['mkdir', '-m','777', path_h264], stdin=subprocess.PIPE)

import datetime as dt
import time
camera.annotate_background = picamera.Color('black')
start_now = dt.datetime.now()
start_now_string = dt.datetime.now().strftime(format)
camera.annotate_text = start_now_string
ratio = float(cam_resolution[0])/float(cam_resolution[1])
renderer_preview_size=100,100,renderer_preview_size,int(renderer_preview_size/ratio)
renderer_preview=camera.start_preview(fullscreen=False, window = renderer_preview_size, alpha=200)

buttton_size = 50
from PIL import Image

img_rec = Image.open(path+'/icons/ico_recording.png')
renderer_rec = picamera.PiOverlayRenderer(camera, img_rec.tobytes(), img_rec.size,format=None,fullscreen=False,window = (0,0,25,25),layer=5,alpha=255)
img_button1 = Image.open(path+'/icons/ico_down.png')
renderer_button1 = picamera.PiOverlayRenderer(camera, img_button1.tobytes(), img_button1.size,format=None,fullscreen=False,window = (0,0,buttton_size,buttton_size),layer=5,alpha=255)
img_button2 = Image.open(path+'/icons/ico_full.png')
renderer_button2 = picamera.PiOverlayRenderer(camera, img_button2.tobytes(), img_button2.size,format=None,fullscreen=False,window=(0,0,buttton_size,buttton_size),layer=5,alpha=255)
img_button3 = Image.open(path+'/icons/ico_play.png')
renderer_button3 = picamera.PiOverlayRenderer(camera, img_button3.tobytes(), img_button3.size,format=None,fullscreen=False,window=(0,0,buttton_size,buttton_size),layer=5,alpha=255)

time.sleep(.5)
reposition_preview(screen_w-renderer_preview_size[2]/2,renderer_preview_size[3]/2)

show_button(renderer_button1,False)
show_button(renderer_button2,False)
show_button(renderer_button3,False)

if start_minimized:
	minimize(True)

import threading
from multiprocessing import Queue
from functools import partial
import time

if __name__ == '__main__':
	action_with_arg = partial(timestamp,camera)
	t1 = threading.Thread(target=action_with_arg)
	t1.start()
	#action_with_arg = partial(touch,renderer_preview)
	#t2 = threading.Thread(target=action_with_arg)
	#t2.start()
	touch(renderer_preview)


import numpy as N
import wave
import pyaudio
import pylab as P
import player as PP

# d2x/dt2   + 2 damp w0 dx/dt + w0^2 x = F/m
#
#  y=x    z=dx/dt
#
#  dv/dt = - 2 damp w0 v - w0^2 x + F/m
#  dx/dt =  v

rate=44100.0

damp=0.1
freq=400
duration=1./freq
w0=freq*2*N.pi
samples = duration*rate

dt=1.0/rate

state=complex(0,1)

j=complex(0,1.0)

dRadPerHertz=2*N.pi/rate

dstate=N.exp(dRadPerHertz*freq*j)

nStep=int(duration*rate)

dt=1/rate

xa=N.zeros(nStep,N.float)

for i in range(nStep):
    state*=dstate
    xa[i]=N.real(state)

from matplotlib.pyplot import *
plot(xa)
show()
player=PP.Player()

peak=N.max(xa)
xa *= 16000.0/peak
player.play(xa)


import asyncio
import inspect
import ast
import contextvars
from inspect import getsource

in_event_loop_var = contextvars.ContextVar("in_event_loop")
in_event_loop_var.set(False)

class AutoAwaiter(ast.NodeTransformer):
    def visit_Call(self, node):
        new_node = ast.IfExp(
            test=ast.Call(
                func=ast.Name(id='hasattr', ctx=ast.Load()),
                args=[
                    node.func,
                    ast.Constant(value='is_sync')],
                keywords=[]),
            body=ast.Await(
                value=node),
            orelse=node)
        return new_node

def sync(f):
    code = "\n".join(getsource(f).splitlines()[1:])
    tree = ast.parse(code)
    auto_awaiter = AutoAwaiter()
    tree = auto_awaiter.visit(tree)
    fdef = tree.body[0]
    if not inspect.iscoroutinefunction(f):
        tree.body[0] = ast.AsyncFunctionDef(name=fdef.name, args=fdef.args, body=fdef.body, decorator_list=fdef.decorator_list)
    ast.fix_missing_locations(tree)
    bytecode = compile(tree, filename="<string>", mode="exec")
    frame = inspect.currentframe()
    out_locals = frame.f_back.f_locals
    out_globals = frame.f_back.f_globals
    exec(bytecode, out_globals, out_locals)
    f = out_locals[fdef.name]

    def wrapper(*args, **kwargs):
        r = f(*args, **kwargs)
        if in_event_loop_var.get():
            return r
        else:
            in_event_loop_var.set(True)
            return asyncio.run(r)

    wrapper.is_sync = None

    return wrapper

@sync
async def sleep(s):
    await asyncio.sleep(s)


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 21 09:29:58 2017

@author: cilab_2
"""

#a = 'a'
#a = ord(a)
#b = a+15
#print(chr(b))
import os
mnist = load_data.read_data_sets("1",\
                         "2",one_hot=False)
trainimgs, trainlabels, testimgs, testlabels = mnist.train.images,\
                                               mnist.train.labels,\
                                               mnist.test.images,\
                                               mnist.test.labels
with tf.Session(graph=graph) as sess:
    sess.run(init)
    saver = tf.train.Saver()
    saver.restore(sess, '1/w.data')
    print ("Start optimization")

    for epoch in range(1):
            batch_txs,batch_tys = mnist.test.next_batch(batch_size)
            batch_txs = batch_txs.reshape((batch_size,nsteps,diminput))

            feed_dict={x:batch_txs, y: sparse_tuple_from([[value] for value in batch_tys]), \
                                 seq_len: [nsteps for _ in xrange(batch_size)]}
            test_acc = sess.run(accr, feed_dict=feed_dict)
            print (" Test label error rate: %.3f" % (test_acc))
#            print("                   batch_ys is :  "+str(batch_tys[0])+"  "\
#                                    +str(batch_tys[1])\
#                                +"  "+str(batch_tys[2]))
            p = sess.run(decoded[0],feed_dict=feed_dict)
            print("prediction is :            ")
            print(len(p[1]))
            print(p[1])

            i = 0
            name =''
            predict = p[1][i*4:i*4+len(p[1])]
            print(predict)
            for k in range(len(p[1])):
                if(predict[k]>=10):
                    print(chr(predict[k]-10+ord('a')))
                    name = name +str(chr(predict[k]-10+ord('a')))
                else:
                    print(predict[k])
                    name = name + str(predict[k])
            name = name +'.gif'
            os.rename('1.gif',name)
            a = batch_txs[i]

            a = a.transpose(1,0)
            plt.imshow(a)

print ("Optimization Finished.")


from typing import Optional
from torch import nn
from modules import apc, symmetrize

class ContactPredictionHead(nn.Module):
    """Performs symmetrization, apc, and computes a logistic regression on the output features"""

    def __init__(
        self,
        in_features: int,
        prepend_bos: bool,
        append_eos: bool,
        bias=True,
        eos_idx: Optional[int] = None,
    ):
        super().__init__()
        self.in_features = in_features
        self.prepend_bos = prepend_bos
        self.append_eos = append_eos
        if append_eos and eos_idx is None:
            raise ValueError("Using an alphabet with eos token, but no eos token was passed in.")
        self.eos_idx = eos_idx
        self.regression = nn.Linear(in_features, 1, bias)
        self.activation = nn.Sigmoid()

    def forward(self, tokens, attentions):

        # remove eos token attentions
        if self.append_eos:
            eos_mask = tokens.ne(self.eos_idx).to(attentions)
            eos_mask = eos_mask.unsqueeze(1) * eos_mask.unsqueeze(2)
            attentions = attentions * eos_mask[:, None, None, :, :]
            attentions = attentions[..., :-1, :-1]
        # remove cls token attentions
        if self.prepend_bos:
            attentions = attentions[..., 1:, 1:]

        batch_size, layers, heads, seqlen, _ = attentions.size()
        attentions = attentions.contiguous().view(batch_size, layers * heads, seqlen, seqlen)
        #attentions = attentions.view(batch_size, layers * heads, seqlen, seqlen)

        # features: B x C x T x T
        attentions = attentions.to(
            next(self.parameters())
        )  # attentions always float32, may need to convert to float16
        attentions = apc(symmetrize(attentions))
        attentions = attentions.permute(0, 2, 3, 1) #B*T*T*C
        return self.activation(self.regression(attentions).squeeze(3))


t = int(input())

for e in range(t):

	l1 = raw_input().split(' ')
	l1 = list(map(int, l1))
	n = l1[0]
	k = l1[1]
	e = l1[2]
	m = l1[3]

	scores = []
	for x in range(n):
		inputScore = raw_input().split(' ')
		inputScore = list(map(int, inputScore))
		score = 0
		for y in inputScore:
			score += y
		scores.append(score)

	sergeyScore = scores.pop(-1)
	scores.sort()
	minimumScore = scores[(n-(n-k))-1]+1
	if minimumScore<0:
		minimumScore = 0
	print(minimumScore - sergeyScore)


import glob
import os
import numpy as np
import matplotlib.pyplot as plt

from matplotlib.font_manager import FontProperties

from PIL import Image
from matplotlib_scalebar.scalebar import ScaleBar

font0 = FontProperties()
font0.set_size(13)
font0.set_family('sans-serif')
font0.set_file('/usr/share/fonts/truetype/msttcorefonts/Arial.ttf')

pxsize1kx = 20./214.

for fname in glob.glob('0/*bmp') + glob.glob('5min/*bmp'):
    if os.path.isfile(fname):
        fout = '{}.pdf'.format(os.path.splitext(fname)[0])

        print(fname)

        basename = fname.split('/')[-1]
        basename = basename.split('.')[0]

        mag = basename.split('x')[0].replace('k', 'e3')
        mag = float(mag)/1e3
        pxsize = pxsize1kx/mag

        # open and convert to grayscale
        img = Image.open(fname).convert('LA')
        img = img.crop(box=(161,0,1119,958))
        plt.imshow(img)

        scalebar = ScaleBar(pxsize*1e-6, length_fraction=.3, location='lower left')
        scalebar.font_properties = font0
        plt.gca().add_artist(scalebar)

        plt.gca().set_axis_off()
        plt.subplots_adjust(top=1, bottom=0, right=1,
                            left=0, hspace=0, wspace=0)
        plt.margins(0, 0)
        plt.gca().xaxis.set_major_locator(plt.NullLocator())
        plt.gca().yaxis.set_major_locator(plt.NullLocator())

        plt.savefig(fout, bbox_inches='tight', pad_inches=0, dpi=300)

        plt.close()


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

    generic() is a forcefield class helper (surrogate class)
    genericdata() is a data container for generic()

    > The class helper enables to defines general RULES based on formula
    > and to store GLOBAL parameters in instances
    > Methods operates with LOCAL parameters according to the recommended precedence
      GLOBAL + LOCAL + RULES (consistency is managed internally by a code analysis)

    > A typical use

        # CREATE AN INSTANCE and set GLOBAL PARAMETERS
        mylibrary = mygenericclass(name="myclass",param1=...)
        # CALL THE METHODS FOR THIS INSTANCE
        FF = mylibrary.forcefield(beadtype=1, userid="myliquid")

    > A typical USER class (inherited from generic) would look like

        class mygenericclass(generic):

            # static properties shared between all instances
            _version = xxx
            _description = "............"
            RULES = genericdata(.........) # class rules (please do not change them)

            # constructor method (required) to create an instance
            def __init__(self,name="...",
                         type=None,
                         param1=...,
                         param2=...,
                         ...
                         USER=genericdata()):

                super().__init__()
                self.name, self.type = name, type
                self.GLOBAL = genericdata(
                    param1 = param1, # parameter specific to mygenericclass()
                    param2 = param2, # idem
                    ...
                    ) + USER    # USER enables a unique/default container to be used

            def myforcefield(self,
                             beadtype=1,   # if needed
                             userid = "",  # user id
                             p1=...,       # first parameter
                             p2=...,       # second parameter
                         USER=genericdata()):

                self.LOCAL = genericdata(
                    p1=p1,
                    p2=p2
                    )
                # manage inheritance, defintion consistency
                CURRENT = self.GLOBAL+self.LOCAL+self.RULES
                return someforcefield(beadtype=beadtype, userid=userid, USER=CURRENT)

Created on Fri May 13 22:06:30 2022

@author: olivi
"""

__project__ = "Pizza3"
__author__ = "Olivier Vitrac"
__copyright__ = "Copyright 2022"
__credits__ = ["Olivier Vitrac"]
__license__ = "GPLv3"
__maintainer__ = "Olivier Vitrac"
__email__ = "olivier.vitrac@agroparistech.fr"
__version__ = "0.451"

# INRAE\Olivier Vitrac - rev. 2022-02-13
# contact: olivier.vitrac@agroparistech.fr

# History
# 2022-05-13 draft version (fields are not reordered)
# 2022-05-16 splitting CORE and USER classes and extended help
# 2022-05-16 full implementation of pizza.private.struct.sortdefinitions() in parameterforcefield()

# %% CORE DEFINITIONS (please)

# imports
from pizza.forcefield import parameterforcefield, water

# genericdata() class // please do not motify
class genericdata(parameterforcefield):
    """ generic data class to be used along with pizza.generic """
    _type = "gendata"        # object type
    _fulltype = "generic data" # full name
    _ftype = "item"        # field name
    _maxdisplay = 80

# generic() class // pease do not modify
class generic:
    """ generic helper class """
    _version = 0.1
    _description = "generic helper for forcefield parameterization"
    RULES = genericdata()

    def __init__(self):
        self.name = None
        self.type = None

    def __repr__(self):
        """ representation of generic() """
        id = ""
        if self.type is not None: id = str(self.type)+":"
        if self.name is not None: id = id+str(self.name)
        if id !="": id = '"'+id+'"'
        idstr = '  generic helper: %s\n\t (class "%s")' % (id,self.__class__)
        print("%s" % idstr)
        if len(self.GLOBAL):
            print("  with GLOBAL data")
            self.GLOBAL.__repr__()
        return idstr

# %% USER SECTION

# USERSMD is a class derived from generic()
class USERSMD(generic):
    """ class helper for the module LAMMPS.USERSMD """

    # specific tules (add formula, missing parameters will be provided by GLOBAL or LOCAL)
    RULES = genericdata(
        # list here default forcefield rules (USER can overload these rules)
        c0 = "${vmax}/${Ma}",
        q1 = "8*${nu}/(${c0}*${h})",
        Cp = 1.0,
        contact_stiffness = '2.5*${c0}^2*${rho}'
        )

    # constructor of USERSMD (note that USER can override all definitions)
    def __init__(self, name=None, type=None, h=0.1, Ma=0.1, vmax=0.1, USER = genericdata()):
        """ generic helper constructor """
        super().__init__()
        self.name = name
        self.type = type
        self.GLOBAL = genericdata(
            # variables which apply to all forcefields
            h = h,
            Ma = Ma,
            vmax = vmax
            ) + USER

    # forcefield derived from pizza.forcefield.water()
    def newtonianfluid(self, beadtype=1, userid = "", rho=1000.0, nu = 1e-6, mu = None, USER = genericdata()):
        """
            newtonianfluid() returns a parameterized ULSPH forcefield
            with prescribed viscosity (mu [Pa.s] or nu in [m2/s])
            and density (rho).
            Based on recommendations of J. Comput. Phys 1997, 136, 214–226
        """
        self.LOCAL = genericdata(
            rho = rho,
            nu = nu, # note that that nu is preferred as it is independent of rho
            mu = mu # dynamic viscosity (dependent on rho)
            ) + USER
        if self.LOCAL.nu is None: self.LOCAL.nu = self.LOCAL.mu / self.LOCAL.rho
        if self.LOCAL.nu is None: raise(ValueError("bad kinematic viscosity value: nu [m2/s]"))
        if self.LOCAL.mu is None: self.LOCAL.mu = self.LOCAL.nu * self.LOCAL.rho
        if userid == "":
            userid = print("newtonianfluid rho=%0.4g [kg/m3] and nu=%0.4g [m2/s]" \
                           % (self.LOCAL.rho,self.LOCAL.nu))
        # this simple line with +
        # manages inheritance (the las definition as always higher precendence)
        # sorts definitions to enable execution (struct.sortdefinitions())
        # (see pizza.forcefield.parameterforcefield())
        CURRENT = self.GLOBAL+self.LOCAL+self.RULES
        return water(beadtype=beadtype, userid=userid, USER=CURRENT)

# %% DEBUG
# ===================================================
# main()
# ===================================================
# for debugging purposes (code called as a script)
# the code is called from here
# ===================================================
if __name__ == '__main__':
    mylibrary = USERSMD(name="currentsim",h=1)
    w = mylibrary.newtonianfluid(beadtype=1, userid="fluid")
    print("\n"*2,w)


# File:          lightning_distance-progex3.4.py
# Date:          2019-09-26
# Author:        "Hannes Thiersen" <hannesthiersen@gmail.com>
# Exercise:      prog ex 3.4
# Description:
#   Writ a program that determines the distance to a lightning strike based on
#   the time elapsed between the flash and the sound of thunder. The speed of
#   sound is approximately 1100ft/sec and 1 mile is 5280 ft.
#
#       sos = 335.28 meter/sec

#-----------------------------------------------------------------------------
# FUNCTIONS
#-----------------------------------------------------------------------------
def main():
    print("Calculate the distance to a lightning strike.")
    time_delta = eval(input("Insert the time between the light and sound: "))
    distance = time_delta * 335.28
    print("The approximate distance to the lightning strike:", distance)

#-----------------------------------------------------------------------------
# MAIN
#-----------------------------------------------------------------------------
if __name__ == "__main__":

    main()


#!/Users/kchen/miniconda3/bin/python
# Author: Kai Chen
# Institute: INS, SJTU
#   Coarse grain causal analysis across cortical regions
#   Plot AUC vs. answer threshold.

import time
import numpy as np
import pickle
import matplotlib.pyplot as plt
plt.rcParams['font.size']=20
plt.rcParams['axes.labelsize']=25
from fcpy.core import EcogGC
from fcpy.utils import print_log
from fcpy.roc import scan_auc_threshold
from fcpy.plot import gen_auc_threshold_figure
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
arg_default = {'path': 'data_preprocessing_46_region/',
                'order': 6,
                }
parser = ArgumentParser(prog='CG_gc_auc_threshold',
                        description = "Generate figure for analysis of causality.",
                        formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('path', default=arg_default['path'], nargs='?',
                    type = str,
                    help = "path of working directory."
                    )
parser.add_argument('order', default=arg_default['order'], nargs='?',
                    type = int,
                    help = "order of regression model in GC."
                    )
args = parser.parse_args()

start = time.time()
# Load SC and FC data
# ==================================================
data = EcogGC()
data.init_data()
sc, fc = data.get_sc_fc('cg')
# ==================================================

w_thresholds = np.logspace(-6, 0, num=7, base=10)
aucs = {}
opt_threshold = {}
for band in data.filters:
    if fc[band] is not None:
        aucs[band], opt_threshold[band] = scan_auc_threshold(fc[band], sc[band], w_thresholds)
    else:
        aucs[band], opt_threshold[band] = None, None
        print_log(f"Data for {band:s} band does not exist.", start)

fig = gen_auc_threshold_figure(aucs, w_thresholds)

fname = f'cg_auc-threshold_gc_{args.order:d}.png'
fig.savefig(args.path + fname)
print_log(f'Figure save to {args.path+fname:s}.', start)
plt.savefig(args.path + f'cg_auc-threshold_gc_{args.order:d}.png')
with open(args.path+f'cg_aucs_gc_order_{args.order:d}.pkl', 'wb') as f:
    pickle.dump(aucs, f)
print_log(f'Figure save to {args.path:s}cg_aucs_gc_order_{args.order:d}.pkl', start)


import os
from functools import wraps
from pandas import json_normalize

def response_decorator(func):
    @wraps(func)
    async def decorate(*args, **kwargs):
        response = await func(*args, **kwargs)
        if not response.content:
            response._content = b"{}"
        data = response.json()
        return data

    return decorate

async def save_exel(path, data, file_name):
    path = f'./downloads/{path}'
    os.makedirs(path, exist_ok=True)
    df = json_normalize(data)
    df.to_excel(f'{path}/{file_name}', index=False)

def _filter(f, dicts):
    result = []
    for _dict in dicts:
        if eval(f):
            result.append(_dict)
    return result

async def base_filter(params, list_dicts, depth=None):
    f = [f'_dict["{k}"]=={v}' for k, v in params.items() if v]
    if f:
        f = ' and '.join(f)
        print(11111, f, depth)
        if depth:
            res_list = []
            for _dicts in list_dicts:
                res = _filter(f, _dicts.pop(depth))
                if res:
                    _dicts[depth] = res
                    res_list.append(_dicts)
            return res_list
        else:
            return _filter(f, list_dicts)
    return list_dicts