"""Test the ORM's `Path` model."""

import json

import googlemaps
import pytest
import sqlalchemy as sqla
from geopy import distance
from sqlalchemy import exc as sa_exc

from urban_meal_delivery import db
from urban_meal_delivery.db import utils

@pytest.fixture
def another_address(make_address):
    """Another `Address` object in the `city`."""
    return make_address()

@pytest.fixture
def path(address, another_address, make_address):
    """A `Path` from `address` to `another_address`."""
    air_distance = distance.great_circle(  # noqa:WPS317
        address.location.lat_lng, another_address.location.lat_lng,
    ).meters

    # We put 5 latitude-longitude pairs as the "path" from
    # `.first_address` to `.second_address`.
    directions = json.dumps(
        [
            (float(add.latitude), float(add.longitude))
            for add in (make_address() for _ in range(5))  # noqa:WPS335
        ],
    )

    return db.Path(
        first_address=address,
        second_address=another_address,
        air_distance=round(air_distance),
        bicycle_distance=round(1.25 * air_distance),
        bicycle_duration=300,
        _directions=directions,
    )

class TestSpecialMethods:
    """Test special methods in `Path`."""

    def test_create_an_address_address_association(self, path):
        """Test instantiation of a new `Path` object."""
        assert path is not None

@pytest.mark.db
@pytest.mark.no_cover
class TestConstraints:
    """Test the database constraints defined in `Path`."""

    def test_insert_into_database(self, db_session, path):
        """Insert an instance into the (empty) database."""
        assert db_session.query(db.Path).count() == 0

        db_session.add(path)
        db_session.commit()

        assert db_session.query(db.Path).count() == 1

    def test_delete_a_referenced_first_address(self, db_session, path):
        """Remove a record that is referenced with a FK."""
        db_session.add(path)
        db_session.commit()

        # Must delete without ORM as otherwise an UPDATE statement is emitted.
        stmt = sqla.delete(db.Address).where(db.Address.id == path.first_address.id)

        with pytest.raises(
            sa_exc.IntegrityError,
            match='fk_addresses_addresses_to_addresses_via_first_address',  # shortened
        ):
            db_session.execute(stmt)

    def test_delete_a_referenced_second_address(self, db_session, path):
        """Remove a record that is referenced with a FK."""
        db_session.add(path)
        db_session.commit()

        # Must delete without ORM as otherwise an UPDATE statement is emitted.
        stmt = sqla.delete(db.Address).where(db.Address.id == path.second_address.id)

        with pytest.raises(
            sa_exc.IntegrityError,
            match='fk_addresses_addresses_to_addresses_via_second_address',  # shortened
        ):
            db_session.execute(stmt)

    def test_reference_an_invalid_city(self, db_session, address, another_address):
        """Insert a record with an invalid foreign key."""
        db_session.add(address)
        db_session.add(another_address)
        db_session.commit()

        # Must insert without ORM as otherwise SQLAlchemy figures out
        # that something is wrong before any query is sent to the database.
        stmt = sqla.insert(db.Path).values(
            first_address_id=address.id,
            second_address_id=another_address.id,
            city_id=999,
            air_distance=123,
        )

        with pytest.raises(
            sa_exc.IntegrityError,
            match='fk_addresses_addresses_to_addresses_via_first_address',  # shortened
        ):
            db_session.execute(stmt)

    def test_redundant_addresses(self, db_session, path):
        """Insert a record that violates a unique constraint."""
        db_session.add(path)
        db_session.commit()

        # Must insert without ORM as otherwise SQLAlchemy figures out
        # that something is wrong before any query is sent to the database.
        stmt = sqla.insert(db.Path).values(
            first_address_id=path.first_address.id,
            second_address_id=path.second_address.id,
            city_id=path.city_id,
            air_distance=path.air_distance,
        )

        with pytest.raises(sa_exc.IntegrityError, match='duplicate key value'):
            db_session.execute(stmt)

    def test_symmetric_addresses(self, db_session, path):
        """Insert a record that violates a check constraint."""
        db_session.add(path)
        db_session.commit()

        another_path = db.Path(
            first_address=path.second_address,
            second_address=path.first_address,
            air_distance=path.air_distance,
        )
        db_session.add(another_path)

        with pytest.raises(
            sa_exc.IntegrityError,
            match='ck_addresses_addresses_on_distances_are_symmetric_for_bicycles',
        ):
            db_session.commit()

    def test_negative_air_distance(self, db_session, path):
        """Insert an instance with invalid data."""
        path.air_distance = -1
        db_session.add(path)

        with pytest.raises(sa_exc.IntegrityError, match='realistic_air_distance'):
            db_session.commit()

    def test_air_distance_too_large(self, db_session, path):
        """Insert an instance with invalid data."""
        path.air_distance = 20_000
        path.bicycle_distance = 21_000
        db_session.add(path)

        with pytest.raises(sa_exc.IntegrityError, match='realistic_air_distance'):
            db_session.commit()

    def test_bicycle_distance_too_large(self, db_session, path):
        """Insert an instance with invalid data."""
        path.bicycle_distance = 25_000
        db_session.add(path)

        with pytest.raises(sa_exc.IntegrityError, match='realistic_bicycle_distance'):
            db_session.commit()

    def test_air_distance_shorter_than_bicycle_distance(self, db_session, path):
        """Insert an instance with invalid data."""
        path.bicycle_distance = round(0.75 * path.air_distance)
        db_session.add(path)

        with pytest.raises(sa_exc.IntegrityError, match='air_distance_is_shortest'):
            db_session.commit()

    @pytest.mark.parametrize('duration', [-1, 3601])
    def test_unrealistic_bicycle_travel_time(self, db_session, path, duration):
        """Insert an instance with invalid data."""
        path.bicycle_duration = duration
        db_session.add(path)

        with pytest.raises(
            sa_exc.IntegrityError, match='realistic_bicycle_travel_time',
        ):
            db_session.commit()

@pytest.mark.db
class TestFromAddresses:
    """Test the alternative constructor `Path.from_addresses()`.

    Includes tests for the convenience method `Path.from_order()`,
    which redirects to `Path.from_addresses()`.
    """

    @pytest.fixture
    def _prepare_db(self, db_session, address):
        """Put the `address` into the database.

        `Address`es must be in the database as otherwise the `.city_id` column
        cannot be resolved in `Path.from_addresses()`.
        """
        db_session.add(address)

    @pytest.mark.usefixtures('_prepare_db')
    def test_make_path_instance(
        self, db_session, address, another_address,
    ):
        """Test instantiation of a new `Path` instance."""
        assert db_session.query(db.Path).count() == 0

        db.Path.from_addresses(address, another_address)

        assert db_session.query(db.Path).count() == 1

    @pytest.mark.usefixtures('_prepare_db')
    def test_make_the_same_path_instance_twice(
        self, db_session, address, another_address,
    ):
        """Test instantiation of a new `Path` instance."""
        assert db_session.query(db.Path).count() == 0

        db.Path.from_addresses(address, another_address)

        assert db_session.query(db.Path).count() == 1

        db.Path.from_addresses(another_address, address)

        assert db_session.query(db.Path).count() == 1

    @pytest.mark.usefixtures('_prepare_db')
    def test_structure_of_return_value(self, db_session, address, another_address):
        """Test instantiation of a new `Path` instance."""
        results = db.Path.from_addresses(address, another_address)

        assert isinstance(results, list)

    @pytest.mark.usefixtures('_prepare_db')
    def test_instances_must_have_air_distance(
        self, db_session, address, another_address,
    ):
        """Test instantiation of a new `Path` instance."""
        paths = db.Path.from_addresses(address, another_address)

        result = paths[0]

        assert result.air_distance is not None

    @pytest.mark.usefixtures('_prepare_db')
    def test_do_not_sync_instances_with_google_maps(
        self, db_session, address, another_address,
    ):
        """Test instantiation of a new `Path` instance."""
        paths = db.Path.from_addresses(address, another_address)

        result = paths[0]

        assert result.bicycle_distance is None
        assert result.bicycle_duration is None

    @pytest.mark.usefixtures('_prepare_db')
    def test_sync_instances_with_google_maps(
        self, db_session, address, another_address, monkeypatch,
    ):
        """Test instantiation of a new `Path` instance."""

        def sync(self):
            self.bicycle_distance = 1.25 * self.air_distance
            self.bicycle_duration = 300

        monkeypatch.setattr(db.Path, 'sync_with_google_maps', sync)

        paths = db.Path.from_addresses(address, another_address, google_maps=True)

        result = paths[0]

        assert result.bicycle_distance is not None
        assert result.bicycle_duration is not None

    @pytest.mark.usefixtures('_prepare_db')
    def test_one_path_for_two_addresses(self, db_session, address, another_address):
        """Test instantiation of a new `Path` instance."""
        result = len(db.Path.from_addresses(address, another_address))

        assert result == 1

    @pytest.mark.usefixtures('_prepare_db')
    def test_two_paths_for_three_addresses(self, db_session, make_address):
        """Test instantiation of a new `Path` instance."""
        result = len(db.Path.from_addresses(*[make_address() for _ in range(3)]))

        assert result == 3

    @pytest.mark.usefixtures('_prepare_db')
    def test_six_paths_for_four_addresses(self, db_session, make_address):
        """Test instantiation of a new `Path` instance."""
        result = len(db.Path.from_addresses(*[make_address() for _ in range(4)]))

        assert result == 6

    # Tests for the `Path.from_order()` convenience method.

    @pytest.mark.usefixtures('_prepare_db')
    def test_make_path_instance_from_order(
        self, db_session, order,
    ):
        """Test instantiation of a new `Path` instance."""
        assert db_session.query(db.Path).count() == 0

        db.Path.from_order(order)

        assert db_session.query(db.Path).count() == 1

    @pytest.mark.usefixtures('_prepare_db')
    def test_make_the_same_path_instance_from_order_twice(
        self, db_session, order,
    ):
        """Test instantiation of a new `Path` instance."""
        assert db_session.query(db.Path).count() == 0

        db.Path.from_order(order)

        assert db_session.query(db.Path).count() == 1

        db.Path.from_order(order)

        assert db_session.query(db.Path).count() == 1

    @pytest.mark.usefixtures('_prepare_db')
    def test_structure_of_return_value_from_order(self, db_session, order):
        """Test instantiation of a new `Path` instance."""
        result = db.Path.from_order(order)

        assert isinstance(result, db.Path)

    @pytest.mark.usefixtures('_prepare_db')
    def test_sync_instance_from_order_with_google_maps(
        self, db_session, order, monkeypatch,
    ):
        """Test instantiation of a new `Path` instance."""

        def sync(self):
            self.bicycle_distance = 1.25 * self.air_distance
            self.bicycle_duration = 300

        monkeypatch.setattr(db.Path, 'sync_with_google_maps', sync)

        result = db.Path.from_order(order, google_maps=True)

        assert result.bicycle_distance is not None
        assert result.bicycle_duration is not None

@pytest.mark.db
class TestSyncWithGoogleMaps:
    """Test the `Path.sync_with_google_maps()` method."""

    @pytest.fixture
    def api_response(self):
        """A typical (shortened) response by the Google Maps Directions API."""
        return [  # noqa:ECE001
            {
                'bounds': {
                    'northeast': {'lat': 44.8554284, 'lng': -0.5652398},
                    'southwest': {'lat': 44.8342256, 'lng': -0.5708206},
                },
                'copyrights': 'Map data ©2021',
                'legs': [
                    {
                        'distance': {'text': '3.0 km', 'value': 2999},
                        'duration': {'text': '10 mins', 'value': 596},
                        'end_address': '13 Place Paul et Jean Paul Avisseau, ...',
                        'end_location': {'lat': 44.85540839999999, 'lng': -0.5672105},
                        'start_address': '59 Rue Saint-François, 33000 Bordeaux, ...',
                        'start_location': {'lat': 44.8342256, 'lng': -0.570372},
                        'steps': [
                            {
                                'distance': {'text': '0.1 km', 'value': 138},
                                'duration': {'text': '1 min', 'value': 43},
                                'end_location': {
                                    'lat': 44.83434380000001,
                                    'lng': -0.5690105999999999,
                                },
                                'html_instructions': 'Head east on  ...',
                                'polyline': {'points': '}tspGxknBKcDIkB'},
                                'start_location': {'lat': 44.8342256, 'lng': -0.57032},
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.1 km', 'value': 115},
                                'duration': {'text': '1 min', 'value': 22},
                                'end_location': {'lat': 44.8353651, 'lng': -0.569199},
                                'html_instructions': 'Turn left onto  ...',
                                'maneuver': 'turn-left',
                                'polyline': {'points': 'suspGhcnBc@JE@_@DiAHA?w@F'},
                                'start_location': {
                                    'lat': 44.83434380000001,
                                    'lng': -0.5690105999999999,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.3 km', 'value': 268},
                                'duration': {'text': '1 min', 'value': 59},
                                'end_location': {'lat': 44.8362675, 'lng': -0.5660914},
                                'html_instructions': 'Turn right onto  ...',
                                'maneuver': 'turn-right',
                                'polyline': {
                                    'points': 'a|spGndnBEYEQKi@Mi@Is@CYCOE]CQIq@ ...',
                                },
                                'start_location': {'lat': 44.8353651, 'lng': -0.56919},
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.1 km', 'value': 95},
                                'duration': {'text': '1 min', 'value': 29},
                                'end_location': {'lat': 44.8368458, 'lng': -0.5652398},
                                'html_instructions': 'Slight left onto  ...',
                                'maneuver': 'turn-slight-left',
                                'polyline': {
                                    'points': 'uatpG`qmBg@aAGM?ACE[k@CICGACEGCCAAEAG?',
                                },
                                'start_location': {
                                    'lat': 44.8362675,
                                    'lng': -0.5660914,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '23 m', 'value': 23},
                                'duration': {'text': '1 min', 'value': 4},
                                'end_location': {'lat': 44.83697, 'lng': -0.5654425},
                                'html_instructions': 'Slight left to stay ...',
                                'maneuver': 'turn-slight-left',
                                'polyline': {
                                    'points': 'ietpGvkmBA@C?CBCBEHA@AB?B?B?B?@',
                                },
                                'start_location': {
                                    'lat': 44.8368458,
                                    'lng': -0.5652398,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.2 km', 'value': 185},
                                'duration': {'text': '1 min', 'value': 23},
                                'end_location': {'lat': 44.8382126, 'lng': -0.5669969},
                                'html_instructions': 'Take the ramp to Le Lac ...',
                                'polyline': {
                                    'points': 'aftpG~lmBY^[^sAdB]`@CDKLQRa@h@A@IZ',
                                },
                                'start_location': {'lat': 44.83697, 'lng': -0.5654425},
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.3 km', 'value': 253},
                                'duration': {'text': '1 min', 'value': 43},
                                'end_location': {'lat': 44.840163, 'lng': -0.5686525},
                                'html_instructions': 'Merge onto Quai Richelieu',
                                'maneuver': 'merge',
                                'polyline': {
                                    'points': 'ymtpGvvmBeAbAe@b@_@ZUN[To@f@e@^A?g ...',
                                },
                                'start_location': {
                                    'lat': 44.8382126,
                                    'lng': -0.5669969,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.1 km', 'value': 110},
                                'duration': {'text': '1 min', 'value': 21},
                                'end_location': {'lat': 44.841079, 'lng': -0.5691835},
                                'html_instructions': 'Continue onto Quai de la ...',
                                'polyline': {'points': '_ztpG`anBUNQLULUJOHMFKDWN'},
                                'start_location': {'lat': 44.840163, 'lng': -0.56865},
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.3 km', 'value': 262},
                                'duration': {'text': '1 min', 'value': 44},
                                'end_location': {'lat': 44.8433375, 'lng': -0.5701161},
                                'html_instructions': 'Continue onto Quai du ...',
                                'polyline': {
                                    'points': 'w_upGjdnBeBl@sBn@gA^[JIBc@Nk@Nk@L',
                                },
                                'start_location': {'lat': 44.841079, 'lng': -0.56915},
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.6 km', 'value': 550},
                                'duration': {'text': '2 mins', 'value': 97},
                                'end_location': {
                                    'lat': 44.84822339999999,
                                    'lng': -0.5705307,
                                },
                                'html_instructions': 'Continue onto Quai ...',
                                'polyline': {
                                    'points': '{mupGfjnBYFI@IBaAPUD{AX}@NK@]Fe@H ...',
                                },
                                'start_location': {
                                    'lat': 44.8433375,
                                    'lng': -0.5701161,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.5 km', 'value': 508},
                                'duration': {'text': '1 min', 'value': 87},
                                'end_location': {'lat': 44.8523224, 'lng': -0.5678223},
                                'html_instructions': 'Continue onto ...',
                                'polyline': {
                                    'points': 'klvpGxlnBWEUGWGSGMEOEOE[KMEQGIA] ...',
                                },
                                'start_location': {
                                    'lat': 44.84822339999999,
                                    'lng': -0.5705307,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '28 m', 'value': 28},
                                'duration': {'text': '1 min', 'value': 45},
                                'end_location': {
                                    'lat': 44.85245620000001,
                                    'lng': -0.5681259,
                                },
                                'html_instructions': 'Turn left onto  ...',
                                'maneuver': 'turn-left',
                                'polyline': {'points': '_fwpGz{mBGLADGPCFEN'},
                                'start_location': {
                                    'lat': 44.8523224,
                                    'lng': -0.5678223,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.2 km', 'value': 176},
                                'duration': {'text': '1 min', 'value': 31},
                                'end_location': {'lat': 44.8536857, 'lng': -0.5667282},
                                'html_instructions': 'Turn right onto  ...',
                                'maneuver': 'turn-right',
                                'polyline': {
                                    'points': '{fwpGx}mB_@c@mAuAOQi@m@m@y@_@c@',
                                },
                                'start_location': {
                                    'lat': 44.85245620000001,
                                    'lng': -0.5681259,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.2 km', 'value': 172},
                                'duration': {'text': '1 min', 'value': 28},
                                'end_location': {'lat': 44.8547766, 'lng': -0.5682825},
                                'html_instructions': 'Turn left onto  ... ',
                                'maneuver': 'turn-left',
                                'polyline': {'points': 'qnwpG`umBW`@UkDtF'},
                                'start_location': {
                                    'lat': 44.8536857,
                                    'lng': -0.5667282,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '0.1 km', 'value': 101},
                                'duration': {'text': '1 min', 'value': 17},
                                'end_location': {'lat': 44.8554284, 'lng': -0.5673822},
                                'html_instructions': 'Turn right onto ...',
                                'maneuver': 'turn-right',
                                'polyline': {'points': 'kuwpGv~mBa@q@cA_B[a@'},
                                'start_location': {
                                    'lat': 44.8547766,
                                    'lng': -0.5682825,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                            {
                                'distance': {'text': '15 m', 'value': 15},
                                'duration': {'text': '1 min', 'value': 3},
                                'end_location': {
                                    'lat': 44.85540839999999,
                                    'lng': -0.5672105,
                                },
                                'html_instructions': 'Turn right onto  ...',
                                'maneuver': 'turn-right',
                                'polyline': {'points': 'mywpGbymBBC@C?E?C?E?EAC'},
                                'start_location': {
                                    'lat': 44.8554284,
                                    'lng': -0.5673822,
                                },
                                'travel_mode': 'BICYCLING',
                            },
                        ],
                        'traffic_speed_entry': [],
                        'via_waypoint': [],
                    },
                ],
                'overview_polyline': {
                    'points': '}tspGxknBUoGi@LcDVe@_CW{Ba@sC[eA_@} ...',
                },
                'summary': 'Quai des Chartrons',
                'warnings': ['Bicycling directions are in beta ...'],
                'waypoint_order': [],
            },
        ]

    @pytest.fixture
    def _fake_google_api(self, api_response, monkeypatch):
        """Patch out the call to the Google Maps Directions API."""

        def directions(_self, *_args, **_kwargs):
            return api_response

        monkeypatch.setattr(googlemaps.Client, 'directions', directions)

    @pytest.mark.usefixtures('_fake_google_api')
    def test_sync_instances_with_google_maps(self, db_session, path):
        """Call the method for a `Path` object without Google data."""
        path.bicycle_distance = None
        path.bicycle_duration = None
        path._directions = None

        path.sync_with_google_maps()

        assert path.bicycle_distance == 2_999
        assert path.bicycle_duration == 596
        assert path._directions is not None

    @pytest.mark.usefixtures('_fake_google_api')
    def test_repeated_sync_instances_with_google_maps(self, db_session, path):
        """Call the method for a `Path` object with Google data.

        That call should immediately return without changing any data.

        We use the `path`'s "Google" data from above.
        """
        old_distance = path.bicycle_distance
        old_duration = path.bicycle_duration
        old_directions = path._directions

        path.sync_with_google_maps()

        assert path.bicycle_distance is old_distance
        assert path.bicycle_duration is old_duration
        assert path._directions is old_directions

class TestProperties:
    """Test properties in `Path`."""

    def test_waypoints_structure(self, path):
        """Test `Path.waypoints` property."""
        result = path.waypoints

        assert isinstance(result, list)
        assert isinstance(result[0], utils.Location)

    def test_waypoints_content(self, path):
        """Test `Path.waypoints` property."""
        result = path.waypoints

        # There are 5 inner points, excluding start and end,
        # i.e., the `.first_address` and `second_address`.
        assert len(result) == 5

    def test_waypoints_is_cached(self, path):
        """Test `Path.waypoints` property."""
        result1 = path.waypoints
        result2 = path.waypoints

        assert result1 is result2


import serial
import sys

with open('Tester_clean.hex', 'r') as f:
    archivo = f.read().split('\n')

archivo.pop()
print(archivo)
print(type(archivo))
# puerto = serial.Serial('COM4', 9600,  rtscts = True) # rtscts = False PARA PROTEUS !!!!!!!!!!!
puerto = serial.Serial('COM1', 9600,  rtscts = False) # rtscts = False PARA PROTEUS !!!!!!!!!!!

for linea in archivo:
    while 1:
        puerto.write(linea.encode())

        respuesta = puerto.readline().decode()

        if(respuesta == 'OK\n'):
            print('Linea enviada correctamente...')
            break

        elif(respuesta == 'BAD\n'):
            print('Checksum invalido, reenviando...')

print('Grabado finalizado...')


#!/usr/bin/env python3

import sys

oldKey = None
totalHits = 0

# Loop around the data
# It will be in the format key\tval
# Where key is the store name, val is the sale amount
#
# All the sales for a particular store will be presented,
# then the key will change and we'll be dealing with the next store

for line in sys.stdin:
    data_mapped = line.strip().split("\t")
    if len(data_mapped) != 2:
        # Something has gone wrong. Skip this line.
        continue

    thisKey, thisStatus = data_mapped

    if oldKey and oldKey != thisKey:
        print(oldKey, "\t", totalHits)
        oldKey = thisKey;
        totalHits = 0

    oldKey = thisKey
    if int(thisStatus) >= 100 and int(thisStatus) < 400:
      totalHits += 1

if oldKey != None:
    print (oldKey, "\t", totalHits)


from downpour.download import Status
from downpour.core import models
from twisted.internet import defer, threads
from time import time
from datetime import datetime
from dateutil.parser import parse as parsedate
import logging, os, re, mimetypes, shutil

mediatypes = {
    'audio/music': 'Music',
    'audio/podcast': 'Podcasts',
    'audio/other': 'Other Audio',
    'video/movie': 'Movies',
    'video/tv': 'TV Series',
    'video/other': 'Other Video',
    'image/photos': 'Photos',
    'image/other': 'Other Images'
}

media_mimetypes = {
    'audio/music': ['audio/'],
    'audio/podcast': ['audio/'],
    'audio/other': ['audio/'],
    'video/movie': ['video/'],
    'video/tv': ['video/'],
    'video/other': ['video/'],
    'image/photos': ['image/'],
    'image/other': ['image/']
}

extra_mimetypes = {
    'mkv': 'video/x-matroska',
    'mka': 'audio/x-matroska',
}

match_patterns = {
    'audio/music': [
        # Artist - Album/Number - Track Name.ext
        re.compile(r'(?P<a>[^/]+?)[ _]*-[ _]*(?P<b>[^/]+)/(?P<t>[0-9]+)[ _]*-[ _]*(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
        # Artist - Album/Track Name.ext
        re.compile(r'(?P<a>[^/]+?)[ _]*-[ _]*(?P<b>[^/]+)/(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
        # Artist/Album/Number - Track Name.ext
        re.compile(r'(?P<a>[^/]+)/(?P<b>[^/]+)/(?P<t>[0-9]+)[ _]*-[ _]*(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
        # Artist/Album/Track Name.ext
        re.compile(r'(?P<a>[^/]+)/(?P<b>[^/]+)/(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
        # Artist - Track Name.ext
        re.compile(r'(?P<a>[^/]+?)[ _]*-[ _]*(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
        # Track Name.ext
        re.compile(r'(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
    ],
    'audio/podcast': [
        # TODO I don't use podcasts, need to look up examples
    ],
    'audio/other': [
    ],
    'video/movie': [
        # Movie Name (2009).avi
        # Movie.Name.2009.mp4
        # Movie.Name[2009].DVDRIP.XviD.avi
        re.compile(r'(?P<n>[^/]+?)\W?[\W\S](?P<y>[0-9]{4})[^/]*\.(?P<x>\w+)$', re.IGNORECASE),
        # Movie.Name.DVDRIP.XviD.avi
        re.compile(r'(?P<n>[^/]+)\Wdvdrip[^/]*\.(?P<x>\w+)$', re.IGNORECASE),
        re.compile(r'(?P<n>[^/]+)\Wb[rd]rip[^/]*\.(?P<x>\w+)$', re.IGNORECASE),
        # Movie Name.avi
        re.compile(r'(?P<n>[^/]+)\.(?P<x>\w+)$', re.IGNORECASE),
    ],
    'video/tv': [
        # s01e01.avi
        #re.compile(r's(?P<s>\d{1,2})\W?e(?P<e>\d{1,2}).*\.(?P<x>\w+)$', re.IGNORECASE),
        # 01x01.avi
        #re.compile(r'(?P<s>\d{1,2})x(?P<e>\d{1,2}).*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show Name - Episode Title s01.e01.Episode.Title.avi
        re.compile(r'(?P<z>[\w \.]+?)\W*-\W*(?P<n>[\w \.]+?)\W*s(?P<s>\d{1,2})\W?e(?P<e>\d{1,2}).*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show.Name.s01.e01.Episode.Title.avi
        # Show.Name.s01e01.Episode.Title.avi
        # Show_Name.s01e01_Episode_Title.avi
        # Show Name - s01e01 - Episode Title.avi
        re.compile(r'(?P<z>[\w -\.]+?)\W*s(?P<s>\d{1,2})\W?e(?P<e>\d{1,2})\W*(?P<n>[\w -\.]*\w)?.*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show.Name.01x01.Episode.Title.avi
        # Show_Name_01x01_Episode_Title.avi
        # Show Name - 01x01 - Episode Title.avi
        re.compile(r'(?P<z>[\w -\.]+?)\W*(?P<s>\d{1,2})x(?P<e>\d{1,2})\W*(?P<n>[\w -\.]*\w)?.*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show Name - s01e01.avi
        #re.compile(r'(?P<z>[\w -\.]+?)\W*s(?P<s>\d{1,2})\W?e(?P<e>\d{1,2}).*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show Name - 01x01.avi
        #re.compile(r'(?P<z>[\w -\.]+?)\W*(?P<s>\d{1,2})x(?P<e>\d{1,2}).*\.(?P<x>\w+)$', re.IGNORECASE),
    ],
    'video/other': [
        # Show Name - Title - Date.ext
        re.compile(r'(?P<z>[\w \.]+?)\W*-\W*(?P<n>[\w \.]+?)\W*(?P<D>[0-9-\.]{3,}[0-9]).*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show Name - Date - Title.ext
        re.compile(r'(?P<z>[\w -\.]+?)\W*(?P<D>[0-9-\.]{3,}[0-9])\W*(?P<n>[\w -\.]*\w)?.*\.(?P<x>\w+)$', re.IGNORECASE),
        # Show Name - Title.ext
        re.compile(r'(?P<z>[\w \.]+?)\W*-\W*(?P<n>[\w \.]+?).*\.(?P<x>\w+)$', re.IGNORECASE),
        # Title.ext
        re.compile(r'(?P<n>.*)\.(?P<x>\w+)$', re.IGNORECASE),
    ],
    'image/photos': [
    ],
    'image/other': [
    ]
}

rename_patterns = {
    'audio/music': [
        '%a/%b/%t - %n.%x',
        '%a/%b - %n.%x',
        '%a/%n.%x',
        '%a - %b/%t - %n.%x',
        '%a - %b - %n.%x',
        '%a - %n.%x'
    ],
    'audio/podcast': [
        '%z/%e - %n - %D.%x',
        '%z/%z %y-%m-%d %n.%x',
        '%z/%Z.%y.%m.%d.%N.%x',
        '%z/%z - %n.%x',
        '%z/%Z.%N.%x',
        '%n/%n.%x',
        '%n/%N.%x',
    ],
    'audio/other': [
    ],
    'video/movie': [
        '%n (%y).%x',
        '%N(%y).%x',
        '%n.%x',
        '%N.%x'
    ],
    'video/tv': [
        '%z/Season %S/%z S%sE%e %n.%x',
        '%z/Season %S/%Z.s%s.e%e.%N.%x',
        '%z/%z S%sE%e %n.%x',
        '%z/%Z.s%s.e%e.%N.%x',
        '%z/S%sE%e %n.%x',
        '%z/s%s.e%e.%N.%x'
    ],
    'video/other': [
        '%z/%z - %y-%m-%d - %n.%x',
        '%z/%Z.%y.%m.%d.%N.%x',
        '%z/%z - %n.%x',
        '%z/%Z.%N.%x',
        '%z/%n.%x',
        '%z/%N.%x',
        '%n.%x',
        '%N.%x',
    ],
    'image/photos': [
        '%y/%m/%f.%x',
        '%y/%m/%d/%f.%x'
    ],
    'image/other': [
    ]
}

stopwords = [
    # Any three-letter (or more) acronym (HDTV, LOL, etc)
    re.compile(r'\W[A-Z]{3,}\b.*'),
    # DVDRIP/BDRIP tags
    re.compile(r'\Wdvdrip\b.*', re.IGNORECASE),
    re.compile(r'\Wb[rd]rip\b.*', re.IGNORECASE),
    # XviD tags
    re.compile(r'\Wxvid\b.*', re.IGNORECASE),
    # UNRATED
    re.compile(r'\Wunrated\b.*', re.IGNORECASE),
    # 1080p/720p
    re.compile(r'\[0-9]{3,4}p\b.*', re.IGNORECASE),
]

blacklist = [
    # Sample videos
    re.compile(r'\Wsample\b.*', re.IGNORECASE),
]

# Post-process downloads to organize them into media libraries
# This should be _very_ fault-tolerant; it can be run multiple
# times (if a user changes which library they want a download
# to be assigned to, etc) and should handle updating previously
# processed downloads gracefully
def process_download(manager, download, client):

    dfr = defer.succeed(True)

    library = None
    libraries = get_media_libraries(manager.get_libraries())
    if download.media_type:
        library = libraries[download.media_type]
    if not library:
        library = models.Library()
        library.directory = None
        library.pattern = u'%p'
        library.keepall = True

    if download.imported:
        # Already imported
        dfr = import_files(download, manager, library, firstRun=False)
    else:
        # New download
        for file in client.get_files():
            f = models.File()
            f.user = download.user
            f.download = download
            f.directory = None
            f.filename = file['path'].decode('utf8')
            f.size = file['size']
            f.media_type = download.media_type
            f.original_filename = file['path'].decode('utf8')
            f.added = time()
            download.files.add(f)
        dfr = import_files(download, manager, library, firstRun=True)

    manager.store.commit()

    return dfr

# Copy media into library
def import_files(download, manager, library, firstRun=True):

    fmap = {}
    dl = []

    targetdir = manager.get_library_directory()
    if library.directory:
        targetdir = '%s/%s' % (targetdir, library.directory)

    for file in download.files:

        # Skip blacklisted patterns (samples, etc)
        blocked = False
        for bl in blacklist:
            if bl.search(file.filename):
                blocked = True
                break

        if blocked:
            logging.debug('Skipping blacklisted file %s' % file.filename)
            continue

        fullpath = file.filename
        if firstRun:
            fullpath = '%s/%s' % (manager.get_work_directory(download), file.filename)
        else:
            if file.directory:
                fullpath = '%s/%s/%s' % (manager.get_library_directory(), \
                    file.directory, fullpath)
            else:
                fullpath = '%s/%s' % (manager.get_library_directory(), fullpath)

        # Skip unrecognized media files
        if not library.keepall:
            mimetype = mimetypes.guess_type(file.filename)[0]
            if not mimetype and file.filename.rfind('.') > -1:
                ext = file.filename[file.filename.rfind('.') + 1:]
                if ext in extra_mimetypes:
                    mimetype = extra_mimetypes[ext]
            matches = sum([1 for m in media_mimetypes[download.media_type] \
                    if mimetype and mimetype.startswith(m)])
            if matches == 0:
                download.files.remove(file)
                if not firstRun:
                    os.remove(fullpath)
                    dir = os.path.dirname(fullpath)
                    while os.path.exists(dir) and not len(os.listdir(dir)):
                        os.rmdir(dir)
                        dir = os.path.dirname(dir)
                logging.debug('Skipping unrecognized media %s' % file.filename)
                continue

        # Map filename to desired renaming pattern
        metadata = get_metadata(file.original_filename, download, fullpath)
        dest = pattern_replace(library.pattern, metadata)
        if dest:
            while dest.find('//') > -1:
                dest = dest.replace('//', '/')
        else:
            logging.debug('Pattern replace failed, skipping %s' % file.filename)
            continue

        # Move file on disk
        dfr = None
        targetfile = '%s/%s' % (targetdir, dest)
        logging.debug('Importing file %s to %s' % (file.filename, targetfile))
        if not firstRun:
            dfr = threads.deferToThread(move_file, \
                fullpath, targetfile, trim_empty_dirs=True)
            dfr.addCallback(manager.application.event_callback, 'library_file_removed', fullpath, download)
            dfr.addCallback(manager.application.event_callback, 'library_file_added', targetfile, download)
        else:
            dfr = threads.deferToThread(copy_file, fullpath, targetfile)
            dfr.addCallback(manager.application.event_callback, 'library_file_added', targetfile, download)
        dfr.addCallback(file_op_complete, download, file, firstRun, \
            library.directory, unicode(dest), download.media_type)
        dl.append(dfr)

    return defer.DeferredList(dl)

# Update database
def file_op_complete(success, download, file, firstRun, newdir, newfile, newtype):
    if success:
        file.directory = newdir
        file.filename = newfile
        file.media_type = newtype
    elif firstRun:
        download.files.remove(file)

def get_metadata(path, source, filename=None):

    metadata = {'a': None, 'b': None, 'd': None, 'D': None,
        'e': None, 'E': None, 'f': None, 'm': None, 'n': None,
        'N': None, 'p': path, 's': None, 'S': None, 't': '1',
        'T': '01', 'x': None, 'y': None, 'z': None, 'Z': None}

    filename = os.path.basename(path)
    pos = filename.rfind('.')
    if pos > -1:
        metadata['f'] = filename[:pos]
        metadata['x'] = filename[pos+1:]
    else:
        metadata['f'] = filename

    # Parse metadata from filename
    if source and source.media_type:
        for m in match_patterns[source.media_type]:
            match = m.search(path)
            if match:
                metadata.update(match.groupdict())
                break;

    # Override with real metadata if file exists
    if filename and os.access(filename, os.R_OK):
        metadata.update(get_file_metadata(filename))

    # Source can be either feed or download
    name = None
    if hasattr(source, 'feed') and source.feed:
        name = source.feed.name
    elif hasattr(source, 'name'):
        name = source.name
    normalize_metadata(metadata, name)

    return metadata

# TODO Merge in real metadata from hachoir-metadata parser
def get_file_metadata(path):
    return {}

def normalize_metadata(metadata, name=None):

    if name:
        metadata['z'] = name
        metadata['Z'] = metadata['z'].replace(' ', '.')
    elif metadata['z']:
        metadata['z'] = metadata['z'].replace('.', ' ')
        metadata['z'] = metadata['z'].replace('_', ' ')
        metadata['Z'] = metadata['z'].replace(' ', '.')
    elif metadata['Z']:
        metadata['Z'] = metadata['Z'].replace(' ', '.')
        metadata['Z'] = metadata['Z'].replace('_', '.')
        metadata['z'] = metadata['Z'].replace('.', ' ')
    elif name:
        metadata['z'] = name
        metadata['Z'] = metadata['z'].replace(' ', '.')

    if metadata['n']:
        metadata['n'] = metadata['n'].replace('.', ' ')
        metadata['n'] = metadata['n'].replace('_', ' ')
        metadata['N'] = metadata['n'].replace(' ', '.')
    elif metadata['N']:
        metadata['N'] = metadata['N'].replace(' ', '.')
        metadata['N'] = metadata['N'].replace('_', '.')
        metadata['n'] = metadata['N'].replace('.', ' ')
    else:
        metadata['n'] = 'Unknown Title'
        metadata['N'] = 'Unknown.Title'

    for sw in stopwords:
        if metadata['z'] and sw.search(metadata['z']):
            metadata['z'] = sw.sub('', metadata['z'])
        if metadata['Z'] and sw.search(metadata['Z']):
            metadata['Z'] = sw.sub('', metadata['Z'])
        if metadata['n'] and sw.search(metadata['n']):
            metadata['n'] = sw.sub('', metadata['n'])
        if metadata['N'] and sw.search(metadata['N']):
            metadata['N'] = sw.sub('', metadata['N'])

    if metadata['e']:
        e = int(metadata['e'])
        metadata['e'] = '%02d' % e
        metadata['E'] = '%d' % e

    if metadata['s']:
        s = int(metadata['s'])
        metadata['s'] = '%02d' % s
        metadata['S'] = '%d' % s

    if metadata['D']:
        d = parsedate(metadata['D'])
        metadata['D'] = d.strftime('%Y-%m-%d')
        if not metadata['y']:
            metadata['y'] = d.strftime('%Y')
        if not metadata['m']:
            metadata['m'] = d.strftime('%m')
        if not metadata['d']:
            metadata['d'] = d.strftime('%d')
    elif metadata['y']:
        if not metadata['d']:
            metadata['d'] = '01'
        if not metadata['m']:
            metadata['m'] = '01'
        metadata['D'] = '%s-%s-%s' % (metadata['y'], metadata['m'], metadata['d'])

def pattern_replace(pattern, values):
    for m in values:
        if values[m] is None:
            pattern = pattern.replace('%' + m, '')
        else:
            pattern = pattern.replace('%' + m, values[m])
    return pattern

def move_file(src, dest, trim_empty_dirs=False):
    try:
        destdir = os.path.dirname(dest)
        if not os.path.exists(destdir):
            os.makedirs(destdir)
        shutil.move(src, dest)
        if trim_empty_dirs:
            srcdir = os.path.dirname(src)
            while os.path.exists(srcdir) and not len(os.listdir(srcdir)):
                os.rmdir(srcdir)
                srcdir = os.path.dirname(srcdir)
        return True
    except Exception as e:
        return False

def remove_file(file, trim_empty_dirs=False):
    try:
        os.remove(file)
        if trim_empty_dirs:
            srcdir = os.path.dirname(file)
            while os.path.exists(srcdir) and not len(os.listdir(srcdir)):
                os.rmdir(srcdir)
                srcdir = os.path.dirname(srcdir)
        return True
    except Exception as e:
        return False

def copy_file(src, dest):
    try:
        destdir = os.path.dirname(dest)
        if not os.path.exists(destdir):
            os.makedirs(destdir)
        shutil.copy(src, dest)
        return True
    except Exception as e:
        return False

def move_files(filemap, trim_empty_dirs=False):
    for src in filemap:
        dest = filemap[src]
        destdir = os.path.dirname(dest)
        if not os.path.exists(destdir):
            os.makedirs(destdir)
        shutil.move(src, dest)
        srcdir = os.path.dirname(src)
        while os.path.exists(srcdir) and not len(os.listdir(srcdir)):
            os.rmdir(srcdir)
            srcdir = os.path.dirname(srcdir)

def copy_files(filemap):
    for src in filemap:
        dest = filemap[src]
        destdir = os.path.dirname(dest)
        if not os.path.exists(destdir):
            os.makedirs(destdir)
        shutil.copy(src, dest)

def get_media_types():
    return mediatypes

def get_media_libraries(userlibs):
    libraries = {}
    for t in mediatypes:
        for l in userlibs:
            if l.media_type == t:
                libraries[t] = l
        if not t in libraries:
            libraries[t] = None
    return libraries

def get_file_patterns():
    patterndesc = {}
    replacements = {
        'a': 'Artist',
        'b': 'Album',
        'd': '15',
        'D': '2009-10-15',
        'e': '03',
        'f': 'filename',
        'E': '3',
        'm': '10',
        'n': 'Media Title',
        'N': 'Media.Title',
        'p': 'filename.ext',
        's': '01',
        'S': '1',
        't': '05',
        'T': '5',
        'y': '2009',
        'z': 'Series Name',
        'Z': 'Series.Name',
        'x': 'ext',
        }
    for t in rename_patterns:
        patterndesc[t] = {}
        for p in rename_patterns[t]:
            patterndesc[t][p] = pattern_replace(p, replacements)
    return patterndesc


map=dict()
for i in range(4):
    f = open('file'+str(i+1),'r')
    for j in f:
        key=j.strip()
        map[key]=map.get(key,0)+1

l1 = list(map.keys())
l1.sort()
for i in l1:
    print("{}\t{}".format(i,map.get(i)))


def CalculateOverlap(C_dict,scaf,side,nbr_scaf,nbr_side,gap,Scaffolds):
    #print 'Neg'
    ########## If negative gap, do Smith Waterman ###################
    ## check if negative gap, if gap is negative: do SW for the gap +300bp
    ## and retrieve a score of the best overlap
    def RevComp(string):
        rev_nuc={'A':'T','C':'G','G':'C','T':'A','N':'N','X':'X'}
        rev_comp=''.join([rev_nuc[nucl] for nucl in reversed(string)])
        return(rev_comp)
    ### GET SEQUENCES ####

    #First sequence
    #Need to obtain the outermost sequence within a scaffold object
    contigs_in_scaf=Scaffolds[scaf].contigs
    if side == 'R':
        max_pos=0
        for contig in contigs_in_scaf:
            if contig.position >= max_pos:
                overlapping_contig=contig
                max_pos=contig.position
        #if contig direction == -1 do rev comp
        #else seq1=C_dict[overlapping_contig.name][-(abs(gap)+200):]
        seq1=C_dict[overlapping_contig.name][-(abs(gap)+200):] #last part of current sequence
    else:
        min_pos=sys.maxint
        for contig in contigs_in_scaf:
            if contig.position <= min_pos:
                overlapping_contig=contig
                min_pos=contig.position
        seq1=RevComp(C_dict[overlapping_contig.name])
        seq1=seq1[-(abs(gap)+200):]  # last part of current sequence
    #if contig direction == -1 do rev comp
    #else seq1=C_dict[overlapping_contig.name][-(abs(gap)+200):]

    #piece of second sequence

    contigs_in_scaf_nbr=Scaffolds[nbr_scaf].contigs
    if nbr_side == 'R':
        max_pos=0
        for contig in contigs_in_scaf_nbr:
            if contig.position >= max_pos:
                overlapping_contig=contig
                max_pos=contig.position
        seq2=C_dict[overlapping_contig.name][0:abs(gap)+200] #first part of next sequence
    else:
        min_pos=sys.maxint
        for contig in contigs_in_scaf_nbr:
            if contig.position <= min_pos:
                overlapping_contig=contig
                min_pos=contig.position
        seq2=RevComp(C_dict[overlapping_contig.name])
        seq2=seq2[0:abs(gap)+200]  #first part of next sequence

    ### START ALIGNMENT ####

    score,row,column,traceback_matrix=SW.SW(seq1,seq2,-4,-4)
    #print score,row,column
    ## if score is significant and overlap is over (say) 16 bp, merge the contigs on the positions
    ## suggested. Otherwise: set gap to 0
    if score > 0 and ( column > 16 and column < len(seq2) ):  ## no use to have: and column < len(seq1)-16)
        gap=SW.traceback_parser(score,row,column,traceback_matrix,seq1,seq2)
    elif score > 0 and (row > 16 and column == len(seq2)):
        ##error, we have chosen too short sequences or repeat structure within contigs, or by chanse got a good alignment (unlikely)
        ## just append the contigs too eachother (0 gap distance)
        gap=0
    else:
        ## If no significant alignment was found, the most likely gap estimate between the contigs is zero
        ## since we got an negative distance estimate from the start. Set the gap to 0, abutting contigs
        gap=0
    return(gap)
####################################################################


#!/usr/bin/env python

''' zeromq server example.

    One front facing server and three workers are instantiated.
    Front facing server accepts connections from multiple clients and distributes computation requests among workers.
    Also it collects computed results from workers and send the result back to the original client.

    Demonstrables:
        Multiple client connections.
        Work distribution among a pool of workers. '''

# Author - Kasun Herath <kasunh01 at gmail.com>
# Source - https://github.com/kasun/zeromq-client-server.git

import threading

import zmq

from face_pb2 import  ProcessStatus
import face_pb2 as pb2

import subprocess

import os

class Server(object):
    ''' Front facing server. '''

    def __init__(self):
        self.zmq_context = zmq.Context()

    def start(self):
        ''' Main execution.
            Instantiate workers, Accept client connections,
            distribute computation requests among workers and route computed results back to clients. '''

        # Front facing socket to accept client connections.
        socket_front = self.zmq_context.socket(zmq.ROUTER)
        socket_front.bind('tcp://*:5000')

        # Backend socket to distribute work.
        socket_back = self.zmq_context.socket(zmq.DEALER)
        socket_back.bind('inproc://backend')

        # Start three workers.
        for i in range(1,2):
            worker = Worker(self.zmq_context, i)
            worker.start()

        # Use built in queue device to distribute requests among workers.
        # What queue device does internally is,
        #   1. Read a client's socket ID and request.
        #   2. Send socket ID and request to a worker.
        #   3. Read a client's socket ID and result from a worker.
        #   4. Route result back to the client using socket ID.
        zmq.device(zmq.QUEUE, socket_front, socket_back)

class Worker(threading.Thread):
    ''' Workers accept computation requests from front facing server.
        Does computations and return results back to server. '''

    def __init__(self, zmq_context, _id):
        threading.Thread.__init__(self)
        self.zmq_context = zmq_context
        self.worker_id = _id

    def run(self):
        ''' Main execution. '''
        # Socket to communicate with front facing server.
        print("\n")
        print("Start ZMQ listening server...")
        print("worker id : "+ str(self.worker_id))
        print("\n")

        socket = self.zmq_context.socket(zmq.DEALER)
        socket.connect('inproc://backend')

        processStatus =  ProcessStatus()

        while True:
            # main process
            print("Receive Frame from backend")
            msg = socket.recv()
            # emptyString = socket.recv()
            topic = socket.recv()
            messsage = socket.recv()

            print(msg)
            # print(emptyString)
            print(topic)
            print(messsage)

            process = pb2.ProcessStatus()
            process.ParseFromString(messsage)
            print(process)

if __name__ == '__main__':
    # server = Server().start()
    # list_files = subprocess.run(["ps", "aux"],[ "grep", "nginx"])
    # print("The exit code was: %d" % list_files.returncode)

    # Kill process via ssh

    # execute_command = []
    # str_command1 = 'ssh'
    # str_command2 = 'root@192.168.0.221 -p 2235'
    # # str_command3 = 'kill ' + str(7536)
    # str_command3 = 'ls'

    # execute_command.append(str_command1)
    # execute_command.append(str_command2)
    # execute_command.append(str_command3)

    # proc1 = subprocess.Popen(execute_command, stdout=subprocess.PIPE)
    # proc1 = subprocess.Popen(['ssh','root@192.168.0.221','-p 2235'], stdout=subprocess.PIPE)

    # proc2 = subprocess.Popen(['ls'], stdin=proc1.ssstdout,
    # stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
    # out, err = proc1.communicate()
    # print('out: {0}'.format(out))
    # print('err: {0}'.format(err))
    command = 'ssh root@192.168.0.221 -p 2235 kill ' + str(1132)
    os.system(command)


import os
import json
import numpy as np

def convert(JSON_FOLDER, NPY_PATH):
    # Initialize an empty list to accumulate the pose data
    pose_data_list = []

    # Loop over all JSON files in the folder
    for JSON_FILE in sorted(os.listdir(JSON_FOLDER)):
        # Check if the file is a JSON file
        if JSON_FILE.endswith('.json'):
            # Construct the full path to the JSON file
            JSON_PATH = os.path.join(JSON_FOLDER, JSON_FILE)

            # Load the JSON data
            with open(JSON_PATH, 'r') as f:
                data = json.load(f)

            # Extract the pose data from the JSON data
            pose_data = np.array(data['people'][0]['pose_keypoints_2d']).reshape((-1, 3))

            # Add the pose data to the list
            pose_data_list.append(pose_data)

    # Convert the list of pose data arrays to a single NumPy array
    pose_data_array = np.stack(pose_data_list, axis=0)

    # Save the pose data as an NPY file
    np.save(NPY_PATH, pose_data_array)

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Convert OpenPose JSON files to NPY file")
    parser.add_argument("--json_dir", type=str, required=True)
    parser.add_argument("--output_file", type=str, default="keypoints.npy")
    args = parser.parse_args()

    JSON_FOLDER = args.json_dir
    NPY_PATH = os.path.join(JSON_FOLDER, "..", args.output_file)

    convert(JSON_FOLDER, NPY_PATH)


# Databricks notebook source
from azure.storage.blob import BlobServiceClient
import os
import pandas as pd
from pyspark.sql.types import StructField, StructType, StringType

connection_string = "<connection_string>"
service = BlobServiceClient.from_connection_string(conn_str=connection_string)

# COMMAND ----------

v_container_name = "folder_name_on_blob_service"
#local_path = 'c:\\work\\'
#local_file_name = '.txt'
#var_filename_from_blob = 'GLIM_Delicias_Order_66.csv'

# COMMAND ----------

container_client = service.get_container_client(v_container_name)

# COMMAND ----------

# criando a lista de arquivos NJM_GLIM e GLIMS
ct = 0
ct2 = 0
ct_glims = 0
list_njm = []
list_glim = []
def insere_lista(nome_lista, item):
    nome_lista.append(str(item.name))

for item_blob in container_client.list_blobs():
    # find a specific string in the names of all items on a blob
    if 'NJM_GLIM20' in str(item_blob.name).upper():
        insere_lista(list_njm, item_blob)
        ct += 1
        if ct < 5:
            print(str(item_blob.name))
    elif 'GLIM20' in str(item_blob.name).upper() and 'delicia' not in str(item_blob.name).lower():
        insere_lista(list_glim, item_blob)
        ct2 += 1
        if ct2 < 5:
            print(str(item_blob.name))

print('# ct NJM ---> ' + str(ct))
print('# ct2 Glim -> ' + str(ct2))

# COMMAND ----------

# delete to Glim
ct = 0
ct2 = 0
for arq_no_blob in container_client.list_blobs():
    if str(arq_no_blob.name) in list_glim:
        ct += 1
        blobFile = service.get_blob_client(v_container_name, arq_no_blob.name)
        blobFile.delete_blob()
    elif str(arq_no_blob.name) in list_njm:
        ct2 += 1
        blobFile = service.get_blob_client(v_container_name, arq_no_blob.name)
        blobFile.delete_blob()
print('# Excluidos NJM  -> ' + str(ct))
print('# Excluidos Glim -> ' + str(ct2))


total_count=int(input())
digits=[]
for i in range(total_count):
    digits.append(input())
result=[]
for j in digits:
    if j=="1":
        result.append(1)
for j in digits:
    if j=="0":
        result.append(0)
for j in digits:
    if j=="2":
        result.append(2)
print(*result)

# n=int(input())
# list=[]
# for _ in range(n):
#     list.append(int(input()))
# zeros=[]
# ones=[]
# twos=[]
# for i in list:
#     if (i==0):
#         zeros.append(i)
#     elif(i==1):
#         ones.append(i)
#     else:
#         twos.append(i)
# result=ones+zeros+twos
# for i in result:
#     print(i,end=" ")

venv/TCSCODINGQUESTIONS/17-PRA BOOK STORE TCS/DCA/DCA-17-APR-21-TWO.py


inve = {'gold coin': 42, 'rope': 1}

dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']

def displayInventory(inv):
    itemCount = 0
    print('Inventory: ')
    for item in inv.keys():
        print(str(inv[item]) + ' ' + item )
        itemCount += inv[item]
    print('Total number of items: ' + str(itemCount))

def addToInventory(inventory, addedItems):
    for item in addedItems:
        inventory.setdefault(item, 0)

        inventory[item] += 1
    return inventory

displayInventory(inve)

inve = addToInventory(inve, dragonLoot)

displayInventory(inve)


import os
from setuptools import setup, find_packages

version = "0.1.0"

requirements_file = 'requirements.txt'
with open(requirements_file) as f:
  install_requires = f.read().splitlines()

setup(name="deepmeme",
      packages=find_packages(),
      version=version,
      description="Package for accessing the clustering tools",
      classifiers=[
          'Development Status :: Beta',
          'Environment :: Web/Desktop Environment',
          'Intended Audience :: Developers',
          'Intended Audience :: Science/Research',
          'Natural Language :: English',
          'Programming Language :: Python :: 3',
      ],
      author='Albert Chung',
      author_email='albertswchung@gmail.com',
      license='',
      python_requires='>=3',
      install_requires=install_requires,
)


import os
import time
import traceback

from pyChaining import Chains
from progress_utils_xh import ProgressPinger
from kafka import KafkaAdminClient
from kafka.admin import NewTopic

if __name__ == '__main__':
    print(os.environ, flush=True)
    brokers = os.getenv("hosts")
    print("Broker from env: ", brokers, flush=True)

    topics = []
    with open("topic_list.txt") as f:
        for line in f.readlines():
            topic = line.strip("\n")
            print(f"Add topic[{line}]", flush=True)
            topics.append(line.rstrip("\n"))
    topics = Chains.of(topics).filter(lambda x: x!="").list()
    i = 0
    while True:
        print(f"retry for {i} time{'s' if i > 1 else ''}", flush=True)
        i += 1
        try:
            print(brokers, flush=True)
            admin = KafkaAdminClient(
                bootstrap_servers=brokers,
                client_id="test-client"
            )
            existingTopic = admin.list_topics()
            print("Existing topic ",existingTopic, flush=True)
            topic_list = Chains.of(topics).\
                filter(lambda x: x not in existingTopic).\
                map(lambda x: NewTopic(x, num_partitions=1, replication_factor=1)).\
                list()

            if len(topic_list) == 0:
                print("Complete creating all topics", flush=True)
                break
            else:
                print(f"{len(topic_list)} topics to create", flush=True)
                rs = admin.create_topics(new_topics=topic_list)
                print(rs, flush=True)
        except Exception as ex:
            print("err", flush=True)
            print(ex, flush=True)
            traceback.print_exc()
            pass
        time.sleep(2)
    print("Done", flush=True)


import json
from collections import Counter
import timeit

data_file = open('data.json')
data = json.load(data_file)

starttime = timeit.default_timer()

status_list = []
piece_list = []
start_time_list = []

for item in data:
    piece_list.append(item["piece_id"])
    status_list.append(item["status"])
    start_time_list.append(item["start_time"])

unique_p_id = []

# get only the unique piece ids
for item in piece_list:
    if item not in unique_p_id:
        unique_p_id.append(item)
print(len(unique_p_id))
# create a dictionary of just unique ids
data_dict = {key: [] for key in unique_p_id}

# store status list and start time list with the key as the piece_id
for i in range(len(piece_list)):
    data_dict[piece_list[i]].append([status_list[i], start_time_list[i]])

# get just the paths
paths = list()

# append the keys to the paths
for i, values in enumerate(data_dict.items()):
    paths.append(values[1])  # status, time

paths = sorted(paths)  # sort on the time

path_list = []

for k in paths:
    temp = []
    if not k[0]:
        del k[0]
    k.sort(key=lambda x: x[1])
    for i, vals in enumerate(k):
        if vals:
            temp.append(vals[0])

    path_list.append(tuple(temp))

    # if key in path_dict:
    #     path_dict[key] += 1
    # else:
    #     path_dict[key] = path_dict.setdefault(key, 1)

# print(path_dict)
# print(counter)
most_common_path, occurrences = Counter(path_list).most_common(1)[0][0], Counter(path_list).most_common(1)[0][1]
print(f"The most common path a piece takes is: {most_common_path}")
print(f"It occurs {occurrences} times.")
print(timeit.default_timer() - starttime)
data_file.close()


# from pudb import set_trace; set_trace()
from typing import List
import math

class Solution1:
    def countQuadruplets(self, nums: List[int]) -> int:
        """TLE, because num_smaller and num_larger takes too long to build.
        """
        N = len(nums)
        # num_smaller[i][j] is the number of vals smaller than nums[i] to the
        # left of nums[j]
        num_smaller = [[0] * N for _ in range(N)]
        # num_larger[i][j] is the number of vals bigger than nums[i] to the
        # right of nums[j]
        num_larger = [[0] * N for _ in range(N)]
        for i in range(N):
            pre = 0
            for j in range(i + 1):
                num_smaller[i][j] = pre + int(nums[j] < nums[i])
                pre = num_smaller[i][j]
        for i in range(N - 1, -1, -1):
            pre = 0
            for j in range(N - 1, i - 1, -1):
                num_larger[i][j] = pre + int(nums[j] > nums[i])
                pre = num_larger[i][j]
        # find j, k of the triplet i, j, k, l, such that nums[j] > nums[k]

        # for j in range(1, N - 2):
        #     for k in range(j + 1, N - 1):
        #         if nums[j] > nums[k]:
        #             print(nums[j], nums[k], num_smaller[k], num_larger[j])
        return sum(num_smaller[k][j] * num_larger[j][k] for j in range(1, N - 2) for k in range(j + 1, N - 1) if nums[j] > nums[k])

class Solution2:
    def countQuadruplets(self, nums: List[int]) -> int:
        """Use a 2D array to handle num_smaller matrix. The observation is that
        if I am handling value 3. I check the idx of value 2. If value 2 is to
        my right, that means we don't have to do anything about value 3. The
        num_smaller of value 3 is the same as num_smaller of value 2.

        If value 2 is to my left, then I only need to fill the values from 2 to
        3, by adding 1 to the last num_smaller of value 2.

        num_larger can be handled in the same way. Thus, we can complete
        num_smaller and num_larger in O(N^2) time with some saveings and
        O(N^2) space.

        Once the prefix sum matrices for values smaller and values bigger are
        created, we pick any pair of nums[j] and nums[k] where j < k and
        nums[j] > nums[k]. Then we just need to find the number of values to
        the left of nums[j] that is smaller than nums[k] (all of these values
        can serve as nums[i]) and the number of values to the right of nums[k]
        that is larger than nums[j] (all of these values can serve as nums[l]).

        The product of these two values are the total number of quadruplets
        that can be formed with the current pair of nums[j] and nums[k].

        O(N^2), 6504 ms, faster than 21.21%
        """
        N = len(nums)
        num_idx_map = {n: i for i, n in enumerate(nums)}
        # num_smaller[i][j] is the number of vals smaller than nums[i] to the
        # left of nums[j]
        num_smaller = {num_idx_map[1]: [0] * N}
        for n in range(2, N + 1):
            i, j = num_idx_map[n], num_idx_map[n - 1]
            if j > i:
                num_smaller[i] = num_smaller[j]
            else:
                num_smaller[i] = num_smaller[j][:]
                pre = num_smaller[j][j]
                for k in range(j, i + 1):
                    num_smaller[i][k] = pre + int(nums[k] < nums[i])
                    pre = num_smaller[i][k]
        # num_larger[i][j] is the number of vals bigger than nums[i] to the
        # right of nums[j]
        num_larger = {num_idx_map[n]: [0] * N}
        for n in range(N - 1, 0, -1):
            i, j = num_idx_map[n], num_idx_map[n + 1]
            if j < i:
                num_larger[i] = num_larger[j]
            else:
                num_larger[i] = num_larger[j][:]
                pre = num_larger[j][j]
                for k in range(j, i - 1, -1):
                    num_larger[i][k] = pre + int(nums[k] > nums[i])
                    pre = num_larger[i][k]
        return sum(num_smaller[k][j] * num_larger[j][k] for j in range(1, N - 2) for k in range(j + 1, N - 1) if nums[j] > nums[k])

class Solution3:
    def countQuadruplets(self, nums: List[int]) -> int:
        """Solution from: https://leetcode.com/problems/count-increasing-quadruplets/discuss/3111697/C%2B%2BorJavaorPython3-Cleanest-DP-with-Clarification-O(n2)

        This is just too rich, too good.

        Let dp[j] be the number of valid triplets i, j, k, such that i < j < k
        and nums[i] < nums[k] < nums[j], given an l > j.

        Thus, as we iterate j from 0 to l - 1, each time nums[j] < nums[l], we
        can add dp[j] to the answer.

        Also, we keep track of the number of vals smaller than nums[l] along the
        way. We use pre_small to keep track of this number.
        Thus, if nums[j] > nums[l], then the number of valid triplets centering
        in nums[j] increase by pre_small because we can include nums[l] into the
        triplet. This is to prepare the DP triplet count for the next round of
        l.

        O(N^2), 3045 ms, faster than 68.70%
        """
        N = len(nums)
        dp = [0] * N
        res = 0
        for l in range(N):
            pre_small = 0
            for j in range(l):
                if nums[j] < nums[l]:
                    res += dp[j]
                    pre_small += 1
                else:
                    dp[j] += pre_small
        return res

sol = Solution2()
tests = [
    ([1,3,2,4,5], 2),
    ([1,2,3,4], 0),
    ([1,3,5,2,4], 1),
]

for i, (nums, ans) in enumerate(tests):
    res = sol.countQuadruplets(nums)
    if res == ans:
        print(f'Test {i}: PASS')
    else:
        print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')


# -*- coding: utf-8 -*-
# --------------------------------- #
#       Author: Anemily Sippola     #
#       Python Version: 3.10.4       #
#       Created on: 2023-01-31      #
# --------------------------------- #

"""
Essentially just wraps an actual model and outputs the loss and predictions
"""

import torch

from .custom_nn_module import CustomNNModule

class SingleClassClassification(CustomNNModule):

	def __init__(self, *args, **kwargs):

		self.number_of_classes = kwargs.pop("number_of_classes")

		self.prediction_logits_key = kwargs.pop("prediction_logits_key")
		self.true_labels_key = kwargs.pop("true_labels_key")

		self.loss_values_key = kwargs.pop("loss_values_key", None)
		self.class_predictions_key = kwargs.pop("class_predictions_key")
		self.class_percentages_key = kwargs.pop("class_percentages_key", None)

		#call to custom NN module
		super(SingleClassClassification, self).__init__(*args, **kwargs)

		self.model = None
		self.loss_fn = None
		self.predict_class = None
		self.output_dimension = None

	def set_model(self, model):

		self.model = model

	def set_loss_fn(self, fn):

		self.loss_fn = fn

	def set_predict_class_fn(self, fn):

		self.predict_class = fn

	def to_device(self):
		self.model.to_device()

	def forward(self, state_object):

		state_object = self.model(state_object)

		prediction_logits = state_object[self.prediction_logits_key]

		with torch.no_grad():

			state_object[self.class_predictions_key] = self.predict_class(self, prediction_logits)

			if (self.class_percentages_key is not None) and (self.class_percentages_key not in state_object):

				state_object[self.class_percentages_key] = torch.nn.functional.softmax(prediction_logits, dim=1)

		if (self.loss_values_key is not None) and (self.true_labels_key in state_object):

			true_labels = state_object[self.true_labels_key]
			true_labels = true_labels.to(prediction_logits.device)

			state_object[self.loss_values_key] = self.loss_fn(prediction_logits, true_labels)

		return state_object

	def _max_value_prediction(self, predictions):

		predictions = predictions.detach()

		return (predictions.max(1)[1]).detach()

	def get_param_dict(self):

		param_dict={
			"model_dict":self.model.get_param_dict()
		}
		return param_dict

	def load_from_dict(self, model_dict):

		self.model.load_from_dict(**model_dict)


import re
from django.conf import settings
from django.contrib.auth import load_backend
from django.core.exceptions import ImproperlyConfigured, PermissionDenied

SESSION_KEY = '_auth_user_id'
BACKEND_SESSION_KEY = '_auth_user_backend'
HASH_SESSION_KEY = '_auth_user_hash'

def get_user(request):
    from django.contrib.auth.models import AnonymousUser
    try:
        user_id = request.session[SESSION_KEY]
        backend_path = request.session[BACKEND_SESSION_KEY]
        assert backend_path in settings.AUTHENTICATION_BACKENDS
        backend = load_backend(backend_path)
        user = backend.get_user(user_id) or AnonymousUser()
    except (KeyError, AssertionError):
        user = AnonymousUser()
    return user


from fractions import Fraction

def isPrime(n):
    return [1] == [i for i in range(1,n) if n % i == 0]

primelist = [1 if isPrime(i) else -1 for i in range(501)]
frog = [1,1,1,1,-1,-1,1,1,1,-1,1,1,-1,1,-1]
getProb = lambda a,b: Fraction(1,2) + Fraction(a*b,6)

accumulator = [0] + [Fraction(1,500)]*500

for i in range(0,15):
    newaccumulator = [0]*501
    for n in range(1,501):
        current = getProb(frog[i],primelist[n])
        if i ==0:
            newaccumulator[n] = current * accumulator[n]
        elif n == 1:
            newaccumulator[n] = current * Fraction(1,2) * accumulator[2]
        elif n == 500:
            newaccumulator[n] = current * Fraction(1,2) * accumulator[499]
        elif n == 2:
            newaccumulator[n] = current * (accumulator[1] + Fraction(1,2) * accumulator[3])
        elif n == 499:
            newaccumulator[n] = current * (accumulator[500] + Fraction(1,2) * accumulator[498])
        else:
            newaccumulator[n] = current * Fraction(1,2) * (accumulator[n-1] + accumulator[n+1])
    accumulator = newaccumulator

print(sum(accumulator))


from flask import Blueprint, redirect, request, jsonify
from app.models import db, User, Post, Comment, FriendRequest
from flask_login import current_user
from app.forms import FriendForm
from sqlalchemy import or_, and_

friend_routes = Blueprint('requests', __name__, url_prefix="/requests")

@friend_routes.route('')
def all_friends():
  friends = User.query.join(FriendRequest,
    (FriendRequest.recipient_id == User.id) | (FriendRequest.sender_id == User.id)).filter(
    (FriendRequest.sender_id == current_user.id) | (FriendRequest.recipient_id == current_user.id)
    ).filter(FriendRequest.status == 'accepted').filter(User.id != current_user.id).all()

  return {'all_friends': [friend.to_dict() for friend in friends]}

@friend_routes.route('/profile/<userId>')
def get_one(userId):
  one_request = FriendRequest.query.filter((FriendRequest.recipient_id == userId) | (FriendRequest.sender_id == userId)).filter(
    (FriendRequest.recipient_id == current_user.id) | (FriendRequest.sender_id == current_user.id)).first()

  if one_request:
    return one_request.to_dict()
  else: return {}

@friend_routes.route("", methods=["POST"])
def create_friend_request():
  new_request = FriendForm()
  new_request['csrf_token'].data = request.cookies['csrf_token']

  recipient_id = new_request.data["recipient_id"]
  status = new_request.data["status"]

  friend_request = FriendRequest(
    sender_id=current_user.id,
    recipient_id=recipient_id,
    status=status
  )
  friend = User.query.get(recipient_id)
  db.session.add(friend_request)
  db.session.commit()
  return {'one_request': friend_request.to_dict()}

@friend_routes.route('/<requestId>', methods=["PUT"])
def accept_request(requestId):
  request = FriendRequest.query.get(requestId)
  updated_request = FriendForm()

  # updated_request['csrf_token'].data = request.cookies['csrf_token']
  sender_id = updated_request.data["sender_id"]
  recipient_id = updated_request.data["recipient_id"]
  status = updated_request.data["status"]

  request.sender_id = sender_id
  request.recipient_id = recipient_id
  request.status = status

  sender = User.query.get(sender_id)
  recipient = User.query.get(recipient_id)

  db.session.commit()
  return sender.to_dict()

@friend_routes.route('/<requestId>/<userId>', methods=["DELETE"])
def delete_request(requestId, userId):
  request = FriendRequest.query.get(requestId)
  sender = User.query.get(request.sender_id)
  recipient = User.query.get(request.recipient_id)
  friend = User.query.get(userId)

  db.session.delete(request)
  db.session.commit()
  return "Successfully deleted"


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 15:35:55 2023

@author: chggo
"""

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

import cv2
import numpy as np
import os
import math

class ProcessImages():
    def __init__(self,dumpImg,resultPath,gamma=2.5):
        self.dumpImg=dumpImg
        self.resultPath=resultPath
        self.gamma=gamma

    def pickImages(self):
        subList=[]
        idx=np.round(np.linspace(0, len(self.dumpImg) - 1, 10)).astype(int)  # pick equally spaced 10 images
        for i in range(len(self.dumpImg)):
              if i in idx:
                img=self.dumpImg[i]
                img=img[92:292,92:292]  #crop
                img=cv2.resize(img,(96,96))
                img=self.gammaCorrection(img,self.gamma)
                cv2.putText(img=img, text='frame-'+str(i), org=(10,12), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=0.3,color=0xBDF7,thickness=1)

                subList.append(img)
        Img=(cv2.hconcat(subList))

        path=os.path.join(self.resultPath,'img.png')
        #cv2.imwrite(path,Img)
        plt.imsave(path,Img,cmap='jet',vmin=0,vmax=65535)
        return(idx)

    def gammaCorrection(self,src, gamma):
        inv_gamma = 1 / gamma
        table = ((np.arange(0, 65536) / 65535) ** inv_gamma) * 65535
        table = table.astype(np.uint16)

        return table[src]

    def makeList(self):
        # create list for all graphs which are saved or to be saved
        imageNameList=[]

        L=['maxIn.png','img.png','Plot1.png','Plot2.png']
        for i in L:
            imageNameList.append(os.path.join(self.resultPath,i))
        return(imageNameList)

    def readPng(self):
        Im=[]
        imageNameList= self.makeList()
        for j in imageNameList:
            im=cv2.imread(j,cv2.IMREAD_ANYCOLOR)
            Im.append(im)
        return(Im,imageNameList)

    def mergePng(self,count):
        Im,imageNameList=self.readPng()

        graph=cv2.vconcat(Im)
        path=os.path.join(self.resultPath,'final_'+str(count)+'.png')
        cv2.imwrite(path,graph)
        self.remove(imageNameList)

        return(Im)

    def remove(self,imageNameList):
        for i in imageNameList:
           os.remove(i)


txt = open("d3.txt").read()
data = txt.split("\n")

triangles = []
for item in data:
	triangles.append(item.split("  ")[1:])
triangles.pop()

for triangle in triangles:
	if '' in triangle:
		triangle.remove('')
	for id, line in enumerate(triangle):
		triangle[id] = int(triangle[id].strip())

triangles_v2 = []
for x in range(3):
	group_of_lines = []
	for triangle in triangles:
		group_of_lines.append(triangle[x])
	new_triangle = []
	i = 0
	while i <= len(group_of_lines)-3:
		new_triangle = [group_of_lines[i], group_of_lines[i+1], group_of_lines[i+2]]
		i += 3
		triangles_v2.append(new_triangle)

# print(len(triangles_v2))
count = 0
for triangle in triangles_v2:
	if triangle[0] + triangle[1] > triangle[2] and\
	   triangle[0] + triangle[2] > triangle[1] and\
	   triangle[1] + triangle[2] > triangle[0]:
	   count += 1
print(count)


# iris cnn cp

from sklearn.datasets import load_iris
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dropout, Dense
from tensorflow.keras.layers import Conv2D, Flatten, MaxPool2D
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping, TensorBoard

import matplotlib.pyplot as plt
import numpy as np

# 데이터
iris = load_iris()
x = iris.data
y = iris.target

print(x.shape) #(150, 4)
print(y.shape) #(150,)

# 데이터 전처리
scaler = StandardScaler()
scaler.fit(x)
x = scaler.transform(x)
x = x.reshape(150, 2, 2, 1)

x_train, x_test, y_train, y_test = train_test_split(
    x, y, shuffle=True, train_size=0.7
)

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

x_predict = x_train[:10]
y_real = y_train[:10]

# 모델링
model = Sequential()
model.add(Conv2D(100, (2,2), padding='same', input_shape=(x_train.shape[1],2,1)))
model.add(Conv2D(300, (1,1)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(20))
model.add(Dense(3, activation='softmax'))

model.summary()

# 3. 컴파일, 훈련
modelpath = './model/cifar10-{epoch:02d}-{val_loss:.4f}.hdf5' #현재 모델 경로(study에 model폴더)
#파일명 : epoch:02니깐 2자리 정수 - val_loss .4니깐 소수 4째자리 표기
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
es = EarlyStopping(monitor='val_loss', patience=5, mode='auto')
cp = ModelCheckpoint(filepath=modelpath, monitor='val_loss',
        save_best_only=True, mode='auto')

model.compile(loss='categorical_crossentropy', optimizer='adam',
                metrics=['acc'])

hist = model.fit(x_train, y_train, epochs=30, batch_size=32,
            verbose=1, validation_split=0.5, callbacks=[es,cp])

# 4. 평가, 예측
result = model.evaluate(x_test, y_test, batch_size=32)
print("loss : ", result[0])
print("acc : ", result[1])

# 시각화
import matplotlib.pyplot as plt

plt.figure(figsize=(10,6)) #단위 무엇인지 찾아볼것!

plt.subplot(2,1,1) #(2행 1열에서 1번째 그림)
plt.plot(hist.history['loss'], marker='.', c='red', label='loss') # x값과, y값이 들어가야 합니다
#x는 안넣어도 순서대로 list 형식으로 저장되서 안 넣어줬습니다

plt.plot(hist.history['val_loss'], marker='.', c='blue', label='val_loss')
plt.grid()
plt.title('loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(loc='upper right') #우측 상단에 legend(label 2개 loss랑 val_loss) 표시

plt.subplot(2,1,2) #(2행 1열에서 2번째 그림)
plt.plot(hist.history['acc'], marker='.', c='red')
plt.plot(hist.history['val_acc'], marker='.', c='blue')
plt.grid()

plt.title('acc')
plt.ylabel('acc')
plt.xlabel('epoch')
plt.legend(['acc', 'val_acc'])

plt.show()

'''
loss :  0.18260696530342102
acc :  0.9111111164093018
'''


'''
pickling:the process writeing a state of object
--------
unpickling:the process reading a state of object
----------
import pickle
pickle.dump(object,file)
pickle.load(file)
'''
import pickle
class employee:
      def __init__(self,eno,ename,esal,eaddr):
            self.eno=eno
            self.ename=ename
            self.esal=esal
            self.eaddr=eaddr
      def display(self):
            print(self.eno,"\t",self.ename,"\t",self.esal,"\t",self.eaddr)
#pickleing:
with open("PickleUnpickle_python.dat",'wb') as f:
      e=employee(100,'gopi',10000,"kkd")
      pickle.dump(e,f)
      print("pickling of emp obj completed....")

#unpickleing:

with open("PickleUnpickle_python.dat",'rb') as f:
      obj=pickle.load(f)
      print("emplyee info after unpickleing...")
      obj.display()# to print the object data

#ALSO USE JOBLIB INSAME FUNCTIONS


from Game import *

game = Game()

#초기상태 설정(시작 턴, 말 위치...)
game.init_normal_game()

print(game)

# print(game.status)

#게임 루프 시작
while game.status != "stop":

    #checkmate면 게임 끝
    if game.is_checkmate() == True:
        print("게임 끝")
        if game.turn == "black":
            win = "white가"
        elif game.turn == "white":
            win = "black이"
        print(f"{win} 이겼습니다.")
        break

    #좌표 선택
    selected_piece = list(map(int,input("좌표 위의 말을 선택하세요: ").split()))

    selected_piece = game.piece_on(selected_piece)

    # 좌표의 팀 체크
    if game.turn_check(selected_piece) == False:
      continue
    else:
      pass

    #갈 수 있는 좌표 표시
    if selected_piece:
        print(f"{selected_piece.name}은 해당 좌표들로 움직일 수 있습니다: {game.legal_moves(selected_piece)}")

    #새 좌표 선택
    new_position = list(map(int,input("움직일 좌표를 선택하세요: ").split()))

    #새 좌표로 이동: 체크라면 자동으로 확인
    game.move_piece_to(selected_piece, new_position)
    # 이동 할 수 없는 좌표인 경우 좌표 다시 선택

    #다음 턴으로 바꿈
    game.next_turn()

    print(game)
    #print("-------------------------------------------------")


'''
Created on 22 nov. 2012

@author: Laurent
'''
import fnmatch
import os
import shutil

fromdir = 'E:/Documents/ExercicesPrepaSupSpe'
todir = 'E:/Cloud/Dropbox/ExercicesPrepaSupSpe'
os.chdir(fromdir)
for root, dirnames, filenames in os.walk('.'):
    for filename in fnmatch.filter(filenames, '*LG*.*'):
        fromfile = os.path.normpath(os.path.join(root, filename))
        tofile = os.path.normpath(os.path.join(todir, root, filename))
        shutil.copy2(fromfile, tofile)

print('Fichiers copiés')


import gymnasium as gym
import os
from stable_baselines3 import *
import numpy as np
from custom_breakout_with_stacking_images import CustomBreakoutWithStackingImages

models_dir = "models/custom_a2c"
log_dir = "logs"

if not os.path.exists(models_dir):
    os.makedirs(models_dir)

if not os.path.exists(log_dir):
    os.makedirs(log_dir)

env = CustomBreakoutWithStackingImages()

model = A2C("CnnPolicy", env, verbose=1, tensorboard_log=log_dir)

TIMESTEPS = 10000

for i in range(1, 300):
    model.learn(total_timesteps=TIMESTEPS, reset_num_timesteps=False, tb_log_name="custom_A2C")
    model.save(f"{models_dir}/{TIMESTEPS*i}")


#from module1 import print_list
import module1

print("this is start.")

movies = ["the holy grail", "the life of brian", "the meaning  of life", ["nested item", ["again item"]]]

import os
print(os.getcwd())

#print_list(movies)
#module1.print_list (movies)

data = open("sketch.txt")
data.seek(0)

man = []
other = []

for each_line in data:
    try:
        (l_value, r_value) = each_line.split(":", 1)
        #print( l_value, end='')
        #print( " said: ", end="")
        #print( r_value, end='')

        r_value.strip()

        if( l_value == 'Man'):
            man.append(r_value)
        elif (l_value == "Other Man"):
            other.append(r_value)

    except:
        print(" exception:", each_line)

data.close()

try:
    out = open("data.out", "w")

    print("gogogo", file=out)
    print(man, file=out)
    print(other, file=out)

except IOError:
    pass

finally:
    out.close()


import requests
from bs4 import BeautifulSoup

def get_current_temperature(station_id):
    temperature = 15

    return temperature

def calculate_pressure_altitude(elevation, altimeter_setting):
    standard_pressure = 29.92  # Standard pressure at sea level in inches of Mercury (inHg)
    pressure_altitude = (standard_pressure - altimeter_setting) * 1000 + elevation
    return pressure_altitude

def calculate_takeoff_performance(base_distance, base_ground_roll, weight, std_weight, pressure_altitude, current_temp, wind_component):
    weight_factor = weight / std_weight
    altitude_factor = 1 + 0.03 * (pressure_altitude / 1000)
    temperature_deviation = current_temp - 15
    temperature_factor = 1 + 0.01 * (temperature_deviation / 10)

    if wind_component > 0:
        wind_factor = 1 - (wind_component / 20)
    else:
        wind_factor = 1 + (abs(wind_component) / 20)

    takeoff_distance = base_distance * weight_factor * altitude_factor * temperature_factor * wind_factor
    takeoff_ground_roll = base_ground_roll * weight_factor * altitude_factor * temperature_factor * wind_factor

    return takeoff_distance, takeoff_ground_roll

def main():
    base_distance = 1800
    base_ground_roll = 910
    base_short_field_distance = 1500
    base_short_field_ground_roll = 900
    short_field_factor = 1

    weight = float(input("Enter aircraft weight (lbs): "))
    wind_component = float(input("Enter headwind component (knots): "))
    std_weight = 2550
    station_id = "KGFK"
    field_elevation = 0

    current_temp = get_current_temperature(station_id)
    print(f"Current Temperature: {current_temp}°C")
    altimeter_setting = 29.92
    pressure_altitude = calculate_pressure_altitude(field_elevation, altimeter_setting)
    print(f"Pressure Altitude: {pressure_altitude:.2f} feet")

    takeoff_distance, takeoff_ground_roll = calculate_takeoff_performance(base_distance, base_ground_roll, weight, std_weight, pressure_altitude, current_temp, wind_component)
    short_field_takeoff_distance, short_field_takeoff_ground_roll = calculate_takeoff_performance(base_short_field_distance, base_short_field_ground_roll, weight, std_weight, pressure_altitude, current_temp, wind_component)

    short_field_takeoff_distance *= short_field_factor
    short_field_takeoff_ground_roll *= short_field_factor

    print("\nNormal Takeoff:")
    print(f"  - Takeoff Distance: {takeoff_distance:.2f} feet")
    print(f"  - Takeoff Ground Roll: {takeoff_ground_roll:.2f} feet")
    print("\nShort Field Takeoff:")
    print(f"  - Takeoff Distance: {short_field_takeoff_distance:.2f} feet")
    print(f"  - Takeoff Ground Roll: {short_field_takeoff_ground_roll:.2f} feet")

main()


#!/usr/bin/python
import re
import sys
import pefile
import struct
import binascii
import subprocess

class Symbol(object):
	def __init__(self, sec, offset, name):
		self.sec = sec
		self.offset = offset
		self.name = name

class Section(object):
	def __init__(self, offset, length, name, kind):
		self.offset = offset
		self.length = length
		self.name = name
		self.kind = kind
		self.symbols = []

pefilename = sys.argv[1]
symbolfilename = sys.argv[2]
outputfilename = sys.argv[3]

image_base = None
sections = []
symbols = []

pe = pefile.PE(pefilename)
#print(pe.dump_info())
image_base = pe.OPTIONAL_HEADER.ImageBase
for section in pe.sections:
	offset = section.VirtualAddress
	length = section.SizeOfRawData
	name = section.Name.strip('\x00')
	kind = section.Characteristics
	if section.Characteristics & pefile.SECTION_CHARACTERISTICS['IMAGE_SCN_CNT_CODE']:
		kind = "CODE"
	else:
		kind = "DATA"
	sections.append(Section(offset, length, name, kind))

section = Section(0x0, sections[0].offset, "HEADER", "DATA")
sections.insert(1, section)

file = open(symbolfilename, "r")
for line in file.readlines():

	### Section
	### Does not work as IDA output is broken. Get from the PE directly instead.
#	m = re.match("^\s*([0-9A-Fa-f]+):([0-9A-Fa-f]+)\s+([0-9A-Fa-f]+)H\s+([^s]*)\s+(CODE|DATA)\s*$", line)
#	if m:
#		sec = m.group(1)
#		offset = m.group(2)
#		length = m.group(3)
#		name = m.group(4)
#		kind = m.group(5)
#
#		sec = int(sec, 16)
#		offset = int(offset, 16)
#		length = int(length, 16)
#

	### Symbol
	m = re.match("^\s*([0-9A-Fa-f]+):([0-9A-Fa-f]+)\s+([^\s]+)\s*$", line)
	if m:
		sec = m.group(1)
		offset = m.group(2)
		name = m.group(3)

		sec = int(sec, 16)
		sec = sections[sec - 1]

		offset, = struct.unpack('>q', binascii.unhexlify(offset))
		if offset < 0:
			offset = (sec.offset + sec.length + offset) - sec.offset

		symbol = Symbol(sec, offset, name)
		sec.symbols.append(symbol)

file.close()

for section in sections[:]:
	print(section.name)
	if section.name == 'HEADER' or \
	   section.name == '.reloc' or \
	   section.name == 'text' or \
	   section.name == '':
	   sections.remove(section)

last_address = 0x0
file = open("/tmp/gdbtab.s", "w")

sorted_sections = sorted(sections, key = lambda k: k.offset)
for section in sorted_sections:
	print("last_address: 0x{0:x}".format(last_address))
	print("offset: 0x{0:x} length: 0x{1:x} name: {2!s} kind: {3!s}".format(section.offset, section.length, section.name, section.kind))
	if not section.symbols:
		continue

	section_offset = section.offset - sorted_sections[0].offset
	section_space = section_offset - last_address
	if section_space > 0:
		file.write('.space {},0x90\n'.format(section_space))

	if section.name != ".rdata":
		file.write('{}\n'.format(section.name))

	last_address = section_offset

	sorted_symbols = sorted(section.symbols, key = lambda k: k.offset)
	for symbol in sorted_symbols:
		symbol_offset = section_offset + symbol.offset
		symbol_space = symbol_offset - last_address
		print("symbol: 0x{0:x} offset: 0x{1:x} name: {2!s}".format(symbol_offset, symbol.offset, symbol.name))
		if symbol_space > 0:
			file.write('.space {},0x90\n'.format(symbol_space))
		file.write('.global \"{}\"\n'.format(symbol.name))
		file.write(' \"{}\":\n'.format(symbol.name))
		last_address = section_offset + symbol.offset

file.write('.p2align 12'.format(symbol.name))
file.close()

### Mac
popen = subprocess.Popen(("/usr/bin/as", "-arch", "x86_64", "/tmp/gdbtab.s", "-o", "/tmp/gdbtab.o"), stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print(output)

# seg1addr and image_base are the same thing
popen = subprocess.Popen(("ld", "-arch", "x86_64", "-macosx_version_min", "10.10", "-preload", "-segalign", "0x20", "-pie", "-seg1addr", "0x240", "-image_base", '0x{:x}'.format(image_base), "-o", outputfilename, "/tmp/gdbtab.o"), stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print(output)

### Mac (untested)
#popen = subprocess.Popen(("gcc", "-Wl,-image_base,0x{:x}".format(image_base), "-dynamiclib", "-o",  outputfilename, "/tmp/gdbtab.s"), stdout=subprocess.PIPE)

### Linux (untested)
#popen = subprocess.Popen(("gcc", "-Wl,-N,-Ttext," + image_base, "-nostdlib", "-o",  outputfilename, "/tmp/gdbtab.s"), stdout=subprocess.PIPE)


import tensorflow as tf
import numpy as np

# train data
x_data = np.random.randint(1, 100, size=15)
x_data = x_data.reshape(-1, 3)
x_data = tf.convert_to_tensor(x_data, dtype=tf.float32)
print(x_data)

y_data = np.array([[152], [185], [180], [196], [142]], dtype='float32')
y_data = y_data.reshape(-1, 1)
print(y_data)

# Simple Model
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(1, input_shape=[3])
])

model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.SGD(lr=1e-5), metrics=['accuracy'])
model.fit(x_data, y_data, epochs=2000)

print(model.predict(np.array([[71, 23, 82]], dtype='float32')))

Simple Network/Multiple Linear Regression(Tensorflow).py