from django.shortcuts import render
from apps.produtos.models import (
    Produtos,
    MovimentacaoProdutos,
    MediaProduto
)
from django.http import JsonResponse
from apps.datatable.datatable import DataTableFilter, DataTableRaw
from django.contrib.auth.decorators import login_required

@login_required(login_url='/login/')
def index(request):
    return render(request, 'produtos/index.html')

@login_required(login_url='/login/')
def modal_add_produto(request):
    return render(request, 'produtos/modalAddProduto.html')

@login_required(login_url='/login/')
def modal_compra_produtos(request):
    return render(request, 'produtos/modalCompraProdutos.html')

@login_required(login_url='/login/')
def modal_vender_produtos(request):
    return render(request, 'produtos/modalVenderProdutos.html')

@login_required(login_url='/login/')
def modal_detalhes_produto(request):
    return render(request, 'produtos/modalDetalhesProduto.html')

@login_required(login_url='/login/')
def modal_produtos_comprados(request):
    lista = lista_comprados()

    return render(request, 'produtos/modalProdutosComprados.html',{
        'lista': lista
    })

@login_required(login_url='/login/')
def modal_produtos_vendidos(request):
    lista = lista_vendidos()

    return render(request, 'produtos/modalProdutosVendidos.html',{
        'lista': lista
    })

@login_required(login_url='/login/')
def salvar_add_produto(request):
    descricao = request.POST.get("descricao")

    if not descricao:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Descrição é um campo obrigatório"
        }

        return JsonResponse(data, safe=False)

    exists = Produtos.objects.filter(
        descricao=descricao
    ).exists()

    if not exists:
        fs = Produtos()
        fs.descricao = descricao
        fs.ativo = 'S'
        fs.save()

        data = {
            "title": "Sucesso!",
            "status": "success",
            "msg": "Produto adicionado com sucesso!"
        }
    else:
        data = {
            "title": "Erro",
            "status": "error",
            "msg": "Produto já está cadastrado!"
        }

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def select_desc_produto(request):
    descricao = request.POST.get('descricao', '')

    items = Produtos.objects.filter(
        descricao__contains=descricao
    )
    data = list()

    for item in items:
        data.append({
            'id': item.id_produto,
            'text': item.descricao
        })

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def salvar_movimentacao_estoque(request):
    id_produto = request.POST.get('id_produto')
    quantidade = request.POST.get('quantidade').replace(",", ".")
    valor = request.POST.get('valor').replace(",", ".")
    tipo = request.POST.get('tipo')

    if not id_produto or not quantidade or not valor or not tipo:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Todos os campos são obrigatórios"
        }

        return JsonResponse(data, safe=False)

    if float(quantidade) <= 0 or float(valor) <= 0:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Quantidade e Valor não podem ser 0"
        }

        return JsonResponse(data, safe=False)

    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    estoque = calcular_estoque(id_produto, tipo, quantidade)

    print(estoque)

    if estoque >= 0:
        fs = MovimentacaoProdutos()
        fs.id_produto_id = id_produto
        fs.quantidade = quantidade
        fs.valor = valor
        fs.media_compra = 0
        fs.tipo = tipo
        fs.save()

        fs = MediaProduto()
        fs.id_produto_id = id_produto
        fs.tipo = tipo
        fs.valor = calcular_media(id_produto, tipo)
        fs.save()

        data = {
            "title": "Sucesso",
            "status": "success",
            "msg": "Produto movimentado com sucesso!"
        }

    else:

        data = {
            "title": "Erro",
            "status": "error",
            "msg": "O estoque do produto não pode ser menor que 0"
        }

    return JsonResponse(data, safe=False)

def calcular_estoque(id_produto, tipo, quantidade):
    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    total = 0
    for item in items:
        if item.tipo == 'E':
            total = float(total) + float(item.quantidade)
        elif item.tipo == 'S':
            total = float(total) - float(item.quantidade)

    if tipo == 'E':
        return total + float(quantidade)
    elif tipo == 'S':
        return total - float(quantidade)

def calcular_media(id_produto, tipo):
    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto,
        tipo=tipo
    )

    media = 0
    total = 0
    total_valor = 0
    total_quantidade = 0
    for item in items:
        total_quantidade = float(total_quantidade) + float(item.quantidade)
        total = total + (float(item.valor) * float(item.quantidade))

    media = total / total_quantidade

    return float(media)

@login_required(login_url='/login/')
def verificar_estoque(request):
    id_produto = request.POST.get("id_produto")

    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    total = 0
    for item in items:
        if item.tipo == 'E':
            total = float(total) + float(item.quantidade)
        elif item.tipo == 'S':
            total = float(total) - float(item.quantidade)

    data = {
        "saldo": total
    }

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def verificar_media(request):
    id_produto = request.POST.get("id_produto")
    tipo = request.POST.get("tipo")

    media = 0
    item = MediaProduto.objects.filter(
        id_produto=id_produto,
        tipo=tipo
    ).latest('id_media_produto')
    if item:
        media = item.valor

    data = {
        "media": media
    }

    return JsonResponse(data, safe=False)

def lista_comprados():
    items = MovimentacaoProdutos.objects.filter(
        tipo="E"
    )

    data = list()

    for item in items:

        media = MediaProduto.objects.filter(
            id_produto=item.id_produto_id,
            tipo="E"
        ).latest('id_media_produto')

        data.append({
            "descricao": item.id_produto.descricao,
            "quantidade": item.quantidade,
            "valor": item.valor,
            "media": media.valor
        })

    return data

def lista_vendidos():
    items = MovimentacaoProdutos.objects.filter(
        tipo="S"
    )

    data = list()

    for item in items:
        media = MediaProduto.objects.filter(
            id_produto=item.id_produto_id,
            tipo="S"
        ).latest('id_media_produto')

        data.append({
            "descricao": item.id_produto.descricao,
            "quantidade": item.quantidade,
            "valor": item.valor,
            "media": media.valor
        })

    return data

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

        columns = list()
        columns = [
            {
                'cl': 'id_produto',
                'type': 'str'
            },
            {
                'cl': 'descricao',
                'type': 'str'
            },
        ]

        where = {}

        order = [
            'id_produto',
        ]

        datatable = DataTableFilter(request, Produtos, columns, where, order)

        return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})

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

        id_produto = request.POST.get("id_produto", None)

        columns = list()
        columns = [
            {
                'cl': 'tipo',
                'type': 'str'
            },
            {
                'cl': 'quantidade',
                'type': 'str'
            },
            {
                'cl': 'dt_movimentacao',
                'type': 'date'
            },
        ]

        where = {
            'id_produto': id_produto
        }

        order = [
            'dt_movimentacao',
        ]

        datatable = DataTableFilter(request, MovimentacaoProdutos, columns, where, order)

        return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})

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

    columns = list()
    columns = [
        'descricao',
        'entrada',
        'saida',
        'saldo',
        'id_produto_id'
    ]

    query = '''
       SELECT * FROM v_estoque_produtos
    '''

    where = None

    value_where = []

    group_by = None

    datatable = DataTableRaw(request, MovimentacaoProdutos, columns, query, where, value_where, group_by)

    return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})


from django.test import TestCase
from knesset.settings import PROJECT_ROOT
from knesset.mks.models import Member
from knesset.committees.models import Committee
from knesset.mmm.models import Document, parse_json, text_lookup, verify
from datetime import datetime
import simplejson
import re

FP = open(PROJECT_ROOT + "/mmm/test_matches.json")
JSON = simplejson.load(FP)
OK_CANDIDATES = re.sub(r"\s+", r" " , " ".join(JSON[0]['candidates']))
OK_DATE = datetime.strptime(JSON[0]['date'], '%d/%m/%Y')

class MmmTest(TestCase):
    """ Testing mmm functions """

    def test_parse_json(self):
        """
        Tests data modification
        """

        j = parse_json(FP)

        self.assertEqual(OK_CANDIDATES, j[0]['candidates'])
        self.assertEqual(OK_DATE, j[0]['date'])

    def test_text_lookup(self):
        """
        Tests in text look up for mks and committees names
        """
        mk_name = u'\u05e8\u05d5\u05e0\u05d9\u05ea \u05ea\u05d9\u05e8\u05d5\u05e9'
        mks = Member.objects.create(name=mk_name)
        c = Committee.objects.create(name='c1')

        self.assertIn(mk_name, OK_CANDIDATES)

        mk = text_lookup(Member, OK_CANDIDATES)
        self.assertEqual(mks.id, mk[0])

        self.assertNotIn('c1', OK_CANDIDATES)

        committees = text_lookup(Committee, OK_CANDIDATES)
        self.assertEqual([], committees)

    def test_verify(self):
        """
        Tests verify method
        """
        o = JSON[0]
        mk_name = u'\u05e8\u05d5\u05e0\u05d9\u05ea \u05ea\u05d9\u05e8\u05d5\u05e9'
        mks = Member.objects.create(name=mk_name)
        mkses = [1]
        committees = []
        d1 = Document.objects.create(url=JSON[0]['url'], title=JSON[0]['title'],
                                     publication_date=OK_DATE, author_names=JSON[0]['author'])
        d1.req_mks.add(mks.id)

        self.assertEqual(o['url'], d1.url)
        self.assertEqual(o['title'], d1.title)
        self.assertEqual(OK_DATE, d1.publication_date)
        self.assertEqual(o['author'], d1.author_names)
        self.assertEqual(1, d1.req_mks.count())
        self.assertEqual(0, d1.req_committee.count())

        self.assertTrue(verify(o, [d1], mkses, committees))


import unittest

from mock import patch

from linotp.tokens.emailtoken import EmailTokenClass
from linotp.tokens.hmactoken import HmacTokenClass
from linotp.tokens.passwordtoken import PasswordTokenClass
from linotp.tokens.smstoken import SmsTokenClass
from linotp.tokens.totptoken import TimeHmacTokenClass
from linotp.tokens.yubikeytoken import YubikeyTokenClass

# ---------------------------------------------------------------------------- -

class FakeTokenModel(object):
    def __init__(self):
        self.LinOtpOtpLen = 8
        # emailtoken needs LinOtpCount variable to function correctly
        self.LinOtpCount = 0

    def setType(self, typ):
        self.type = typ

    def getSerial(self):
        return "FakeToken1"

    def getInfo(self):
        return ""

    def storeToken(self):
        pass

    def get_encrypted_seed(self):
        return b"", b""

def mock_send_email(self_obj):
    status = True
    status_message = "e-mail sent successfully"
    return status, status_message

def mock_send_email_fail(self_obj):
    status = False
    status_message = "sending e-mail failed"
    return status, status_message

Config_dict = {
    "TOTP_CHALLENGE_PROMPT": "Hello TOTP Prompt",
    "HMAC_CHALLENGE_PROMPT": "Hello HMAC Prompt",
    "YUBIKEY_CHALLENGE_PROMPT": "Hello yubikey Prompt",
    "PW_CHALLENGE_PROMPT": "Hello Password Prompt",
    "SMS_CHALLENGE_PROMPT": "Hello SMS Prompt",
    "EMAIL_CHALLENGE_PROMPT": "Hello E-Mail Prompt",
}

def mock_getFromConfig(key, fallback):
    return Config_dict.get(key, fallback)

class TestChallengePrompt(unittest.TestCase):
    """"""

    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.smstoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.smstoken.SmsTokenClass.loadLinOtpSMSValidTime")
    @patch("linotp.tokens.smstoken.SmsTokenClass.setValidUntil")
    @patch("linotp.tokens.smstoken.SmsTokenClass.submitChallenge")
    def tests_sms_token_challenge_prompt(
        self,
        mock_submitChallenge,
        mock_setValidUntil,
        mock_loadLinOtpSMSValidTime,
    ):
        mock_submitChallenge.return_value = True, "sms submitted"
        mock_setValidUntil.return_value = None
        mock_loadLinOtpSMSValidTime.return_value = 30

        prompt = Config_dict.get("SMS_CHALLENGE_PROMPT")

        sms_token = SmsTokenClass(FakeTokenModel())

        (_, message, _data, _) = sms_token.createChallenge(
            "131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_password_token_challenge_prompt(self):
        prompt = Config_dict.get("PW_CHALLENGE_PROMPT")

        pass_token = PasswordTokenClass(FakeTokenModel())

        (_, message, _data, _) = pass_token.createChallenge(
            "131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    def tests_yubikey_token_challenge_prompt(self):
        prompt = Config_dict.get("YUBIKEY_CHALLENGE_PROMPT")

        yubi_token = YubikeyTokenClass(FakeTokenModel())

        (_, message, _data, _) = yubi_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_hmac_token_challenge_prompt(self):
        prompt = Config_dict.get("HMAC_CHALLENGE_PROMPT")

        hmac_token = HmacTokenClass(FakeTokenModel())

        (_, message, _data, _) = hmac_token.createChallenge(
            state="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.totptoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_totp_token_challenge_prompt(self):
        prompt = Config_dict.get("TOTP_CHALLENGE_PROMPT")

        totp_token = TimeHmacTokenClass(FakeTokenModel())

        (_, message, _data, _) = totp_token.createChallenge(
            state="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch(
        "linotp.tokens.emailtoken.EmailTokenClass._sendEmail", mock_send_email
    )
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.emailtoken.getFromConfig", mock_getFromConfig)
    def tests_email_token_challenge_prompt(self):
        prompt = Config_dict.get("EMAIL_CHALLENGE_PROMPT")

        email_token = EmailTokenClass(FakeTokenModel())

        (_, message, _data, _) = email_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch(
        "linotp.tokens.emailtoken.EmailTokenClass._sendEmail",
        mock_send_email_fail,
    )
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.emailtoken.getFromConfig", mock_getFromConfig)
    def tests_failed_email_token_challenge_prompt(self):
        """To test that the prompt will be the output of
        the email provider rather than the custom message from the config
        """

        # this is the prompt that mock_send_email will produce
        prompt = "sending e-mail failed"

        email_token = EmailTokenClass(FakeTokenModel())

        (_, message, _data, _) = email_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return


from uuid import uuid4

from storm.locals import Not

from fluiddb.data.namespace import Namespace, createNamespace
from fluiddb.data.object import getDirtyObjects, DirtyObject
from fluiddb.data.permission import (
    NamespacePermission, Operation, Policy, TagPermission, createTagPermission,
    getTagPermissions)
from fluiddb.data.system import createSystemData
from fluiddb.data.tag import Tag, createTag, getTags
from fluiddb.data.value import createTagValue, getTagValues
from fluiddb.exceptions import FeatureError
from fluiddb.model.exceptions import DuplicatePathError
from fluiddb.model.permission import PermissionAPI
from fluiddb.model.tag import TagAPI
from fluiddb.model.user import UserAPI, getUser
from fluiddb.model.value import TagValueAPI
from fluiddb.testing.basic import FluidinfoTestCase
from fluiddb.testing.resources import ConfigResource, DatabaseResource

class TagAPITestMixin(object):

    def assertDefaultPermissions(self, user, tagPath):
        """
        Assert that a L{TagPermission} exists for the specified L{Tag.path}
        and that it uses the default system-wide policy.
        """
        tag, permission = getTagPermissions([tagPath]).one()
        self.assertEqual(Policy.CLOSED, permission.updatePolicy)
        self.assertEqual([user.id], permission.updateExceptions)
        self.assertEqual(Policy.CLOSED, permission.deletePolicy)
        self.assertEqual([user.id], permission.deleteExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlPolicy)
        self.assertEqual([user.id], permission.controlExceptions)
        self.assertEqual(Policy.CLOSED, permission.writeValuePolicy)
        self.assertEqual([user.id], permission.writeValueExceptions)
        self.assertEqual(Policy.OPEN, permission.readValuePolicy)
        self.assertEqual([], permission.readValueExceptions)
        self.assertEqual(Policy.CLOSED, permission.deleteValuePolicy)
        self.assertEqual([user.id], permission.deleteValueExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlValuePolicy)
        self.assertEqual([user.id], permission.controlValueExceptions)

    def testCreateWithoutData(self):
        """
        L{SecureTagAPI.create} raises a L{FeatureError} when no L{Tag}
        data is provided.
        """
        self.assertRaises(FeatureError, self.tags.create, [])

    def testCreate(self):
        """L{TagAPI.create} creates new L{Tag}s using the provided data."""
        values = [(u'username/tag', u'A description')]
        self.tags.create(values)
        systemTags = self.system.tags.keys()
        tag = self.store.find(Tag, Not(Tag.path.is_in(systemTags))).one()
        self.assertEqual(u'username/tag', tag.path)
        self.assertEqual(u'tag', tag.name)
        self.assertIdentical(self.user.namespace, tag.namespace)

    def testCreateCreatesPermissions(self):
        """
        L{TagAPI.create} creates permissions for the newly created L{Tag}s.
        """
        self.tags.create([(u'username/tag', u'A description')])
        self.assertDefaultPermissions(self.user, u'username/tag')

    def testCreateChildTagInheritsPermissions(self):
        """
        L{TagAPI.create} creates new L{Namespace}s based on the provided data.
        """
        UserAPI().create([(u'user', u'secret', u'name', u'user@example.com')])
        user = getUser(u'user')
        TagAPI(user).create([(u'user/tag', u'A description')])
        self.assertDefaultPermissions(user, u'user/tag')

    def testCreateChildTagInheritsParentNamespacePermissions(self):
        """
        L{TagAPI.create} creates new L{Tag}s with permissions inherited from
        the parent L{Namespace}'s permissions.
        """
        PermissionAPI(self.user).set([
            (u'username', Operation.CREATE_NAMESPACE, Policy.CLOSED,
             [u'username']),
            (u'username', Operation.UPDATE_NAMESPACE, Policy.OPEN, []),
            (u'username', Operation.DELETE_NAMESPACE, Policy.OPEN, []),
            (u'username', Operation.LIST_NAMESPACE, Policy.CLOSED,
             [u'username']),
            (u'username', Operation.CONTROL_NAMESPACE, Policy.OPEN, [])])

        self.tags.create([(u'username/tag', u'A child tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreateChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.  In the case of default permissions, the user
        creating the new L{Tag} is granted L{Operation.UPDATE_TAG},
        L{Operation.DELETE_TAG}, L{Operation.WRITE_TAG_VALUE} and
        L{Operation.DELETE_TAG_VALUE}.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE,
              Policy.CLOSED, [u'username', u'friend'])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreatePublicChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE, Policy.OPEN, [])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreatePrivateChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.  If the L{Operation.READ_TAG_VALUE}
        permission is L{Policy.CLOSED} the creator is added to the exceptions
        list.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE, Policy.CLOSED,
              [u'username', u'friend']),
             (u'username', Operation.LIST_NAMESPACE, Policy.CLOSED,
              [u'username', u'friend'])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreateStoresTagDescriptions(self):
        """
        L{TagAPI.create} creates new C{fluiddb/tags/description} L{TagValue}s
        to store the specified L{Tag} descriptions.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/tags/description']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'A tag description', value.value)

    def testCreateStoresTagPaths(self):
        """
        L{TagAPI.create} creates new C{fluiddb/tags/path} L{TagValue}s to store
        the specified L{Tag} paths.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/tags/path']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'username/tag', value.value)

    def testCreateCreatesAboutTag(self):
        """
        L{TagAPI.create} creates new C{fluiddb/about} L{TagValue}s when
        creating new L{Tag}s.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/about']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'Object for the attribute username/tag', value.value)

    def testCreateReusesPreviousObjectIDs(self):
        """
        If a L{Tag} is deleted and created again, L{TagAPI.create}
        uses the old object ID.
        """
        values = [(u'username/tag', u'A tag description')]
        [(firstObjectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        [(secondObjectID, path)] = self.tags.create(values)
        self.assertEqual(firstObjectID, secondObjectID)

    def testCreateWithExistingNamespacePath(self):
        """
        L{TagAPI.create} can be used to create L{Tag}s with the same path as
        an existing L{Namespace}.
        """
        createNamespace(self.user, u'username/name', self.user.namespace.id)
        values = [(u'username/name', u'A description')]
        self.tags.create(values)
        systemTags = self.system.tags.keys()
        tag = self.store.find(Tag, Not(Tag.path.is_in(systemTags))).one()
        self.assertEqual(u'username/name', tag.path)
        self.assertEqual(u'name', tag.name)
        self.assertIdentical(self.user.namespace, tag.namespace)

    def testCreateWithExistingTagPath(self):
        """
        L{TagAPI.create} raises a L{DuplicatePathError} exception if an
        attempt to create a L{Tag} with the same path an existing L{Tag} is
        made.
        """
        createTag(self.user, self.user.namespace, u'name')
        self.assertRaises(
            DuplicatePathError, self.tags.create,
            [(u'username/name', u'Already used tag path.')])

    def testCreatePermissionsWithImplicitNamespace(self):
        """
        L{TagAPI.create} creates permissions for the newly created
        L{Namespace}s and L{Tag}s.
        """
        values = [(u'username/namespace/tag', u'A description')]
        self.tags.create(values)

        result = self.store.find(
            NamespacePermission,
            NamespacePermission.namespaceID == Namespace.id,
            Namespace.path == u'username/namespace')
        permission = result.one()
        self.assertEqual(Policy.CLOSED, permission.createPolicy)
        self.assertEqual([self.user.id], permission.createExceptions)
        self.assertEqual(Policy.CLOSED, permission.updatePolicy)
        self.assertEqual([self.user.id], permission.updateExceptions)
        self.assertEqual(Policy.CLOSED, permission.deletePolicy)
        self.assertEqual([self.user.id], permission.deleteExceptions)
        self.assertEqual(Policy.OPEN, permission.listPolicy)
        self.assertEqual([], permission.listExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlPolicy)
        self.assertEqual([self.user.id], permission.controlExceptions)

        self.assertDefaultPermissions(self.user, u'username/namespace/tag')

    def testDelete(self):
        """L{TagAPI.delete} removes L{Tag}s."""
        self.tags.create([(u'username/child', u'A description')])
        self.tags.delete([u'username/child'])
        self.assertIdentical(None, getTags(paths=[u'username/child']).one())

    def testDeleteDoesNotDeleteOtherTags(self):
        """
        L{TagAPI.delete} removes just the L{Tag} is is asked to and not any
        other tags.
        """
        self.tags.create([(u'username/child1', u'A description')])
        self.tags.create([(u'username/child2', u'A description')])
        self.tags.delete([u'username/child1'])
        tag = getTags(paths=[u'username/child2']).one()
        self.assertEqual(u'username/child2', tag.path)

    def testDeleteDoesNotDeleteOtherTagsWhenPassedAGenerator(self):
        """
        L{TagAPI.delete} removes just the L{Tag} is is asked to and not any
        other tags when it is passed a generator (as opposed to a C{list}).
        """
        self.tags.create([(u'username/child1', u'A description')])
        self.tags.create([(u'username/child2', u'A description')])
        self.tags.delete(name for name in [u'username/child1'])
        tag = getTags(paths=[u'username/child2']).one()
        self.assertEqual(u'username/child2', tag.path)

    def testDeleteRemovesDescription(self):
        """
        L{TagAPI.delete} removes the C{fluiddb/tags/description} values stored
        for deleted L{Tag}s.
        """
        values = [(u'username/tag', u'A description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        result = TagValueAPI(self.user).get(
            objectIDs=[objectID], paths=[u'fluiddb/tags/description'])
        self.assertEqual({}, result)

    def testDeleteRemovesPath(self):
        """
        L{TagAPI.delete} removes the C{fluiddb/tags/path} values stored
        for deleted L{Tag}s.
        """
        values = [(u'username/tag', u'A description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        result = TagValueAPI(self.user).get(
            objectIDs=[objectID], paths=[u'fluiddb/tags/path'])
        self.assertEqual({}, result)

    def testDeleteKeepsTheAboutTag(self):
        """
        L{TagAPI.delete} keeps the C{fluiddb/about} tag value for the deleted
        tag.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        tag = getTags(paths=[u'fluiddb/about']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'Object for the attribute username/tag', value.value)

    def testDeleteRemovesPermissions(self):
        """L{TagAPI.delete} removes permissions when L{Tag}s are deleted."""
        values = [(u'username/tag', u'A description')]
        self.tags.create(values)

        self.tags.delete([u'username/tag'])
        result = self.store.find(TagPermission,
                                 TagPermission.tagID == Tag.id,
                                 Tag.path == u'username/tag')
        self.assertTrue(result.is_empty())

    def testDeleteReturnsObjectIDs(self):
        """
        L{TagAPI.delete} returns a list of C{(objectID, Tag.path)} 2-tuples
        for L{Tag}s that were removed.
        """
        result1 = self.tags.create([(u'username/child', u'A description')])
        result2 = self.tags.delete([u'username/child'])
        self.assertEqual(result1, result2)

    def testDeleteUpdatesObjectsTagged(self):
        """
        L{TagAPI.delete} updates the objects previously tagged with the given
        paths.
        """
        objectID = uuid4()
        self.tags.create([(u'username/child', u'A description')])
        TagValueAPI(self.user).set({objectID: {u'username/child': 64}})
        getDirtyObjects([objectID]).remove()
        self.tags.delete([u'username/child'])
        self.assertIn(objectID, getDirtyObjects().values(DirtyObject.objectID))

    def testGetWithoutData(self):
        """
        L{TagAPI.get} raises a L{FeatureError} if no L{Tag.path}s are
        provided.
        """
        self.assertRaises(FeatureError, self.tags.get, [])

    def testGet(self):
        """L{TagAPI.get} returns L{Tag}s that match the specified paths."""
        createTag(self.user, self.user.namespace, u'ignored')
        tag = createTag(self.user, self.user.namespace, u'tag')
        self.assertEqual({u'username/tag': {'id': tag.objectID}},
                         self.tags.get([u'username/tag']))

    def testGetWithDescriptions(self):
        """
        L{TagAPI.get} can optionally include L{Tag.description}s in the
        result.
        """
        descriptionTag = self.system.tags[u'fluiddb/tags/description']
        tag = createTag(self.user, self.user.namespace, u'tag')
        createTagValue(self.user.id, descriptionTag.id, tag.objectID, u'A tag')
        result = self.tags.get([u'username/tag'], withDescriptions=True)
        self.assertEqual(tag.objectID, result[u'username/tag']['id'])
        self.assertEqual(u'A tag', result[u'username/tag']['description'])

    def testGetWithNonexistentDescriptions(self):
        """
        L{TagAPI.get} returns an empty string as description if the
        C{fluiddb/tags/description} tag is not present on the tag object.
        """
        tag = createTag(self.user, self.user.namespace, u'tag')
        result = self.tags.get([u'username/tag'], withDescriptions=True)
        self.assertEqual(tag.objectID, result[u'username/tag']['id'])
        self.assertEqual(u'', result[u'username/tag']['description'])

    def testSet(self):
        """L{TagAPI.set} updates the description for the specified L{Tag}s."""
        descriptionTag = self.system.tags[u'fluiddb/tags/description']
        tag = createTag(self.user, self.user.namespace, u'tag')
        createTagPermission(tag)
        self.tags.set({u'username/tag': u'A fancy new description.'})
        result = getTagValues([(tag.objectID, descriptionTag.id)])
        description = result.one()
        self.assertEqual(u'A fancy new description.', description.value)

    def testSetReturnsUpdateResultWithObjectIDAndPathPairs(self):
        """
        L{TagAPI.delete} returns a list of C{(objectID, Tag.path)} 2-tuples
        for L{Tag}s that were updated.
        """
        result1 = self.tags.create([(u'username/tag', u'A description')])
        result2 = self.tags.set({u'username/tag': u'A fancy new description.'})
        self.assertEqual(result1, result2)

class TagAPITest(TagAPITestMixin, FluidinfoTestCase):

    resources = [('config', ConfigResource()),
                 ('store', DatabaseResource())]

    def setUp(self):
        super(TagAPITest, self).setUp()
        self.system = createSystemData()
        UserAPI().create([(u'username', u'password', u'User',
                           u'user@example.com')])
        self.user = getUser(u'username')
        self.permissions = PermissionAPI(self.user)
        self.tags = TagAPI(self.user)


class Car:
    def __init__(self, x: int, y: int, tank: int, fuel: int) -> None:
        self.x = x
        self.y = y
        self.tank = tank
        self.fuel = fuel

    def move(self, x: int, y: int) -> bool:
        if ((x - self.x) ** 2 + (y - self.y) ** 2) ** 0.5 <= self.fuel:
            self.x = x
            self.y = y
            self.fuel -= ((x - self.x) ** 2 + (y - self.y) ** 2) ** 0.5
            return True

        return False


# -*- coding: utf-8 -*-
# @Author: Nandadeep Davuluru
# @Date:   2019-03-10 11:38:34
# @Last Modified by:   nandadeep
# @Last Modified time: 2019-03-12 20:42:22
import os, time
from kmp import KMP
from naive import NaiveStringMatch
from bm import BoyerMoore
from random import randint
from random import shuffle
from matplotlib import pyplot as plt
from timeit import default_timer as timer
import itertools

PATTERN_SIZE = 4 # hyper-parameter?
DIRECTORY = '/Users/nandadeepd/Desktop/string-matching/'

class Plotter(object):

    def plot_text_time(self, experiment, s_times, lengths, color):
        print("-----------------")
        print(s_times, lengths)
        print("-----------------")
        plt.scatter(s_times, lengths, c=["r", "g", "c", "b", "k"])
        plt.xlabel("Time"); plt.ylabel("Text Length")
        plt.savefig(DIRECTORY + "/metrics/" + str(experiment) + ".png")

    def sample_plot(self):
        times = [1, 5, 3, 2, 6, 7, 8, 9, 10]
        lengt = [50, 100, 40, 60 , 80 , 90, 300, 60, 500]
        self.plot_text_time(experiment=1, times=times, lengths=lengt)

def pattern_generator(text, total_len):
	len_edge = total_len - PATTERN_SIZE # avoiding out of bounds!
	start_num = randint(0, len_edge)
	end_num = start_num + PATTERN_SIZE # size of the pattern.
	return start_num, end_num

def main():

	files = [f for f in os.listdir(DIRECTORY + "/datasets/")] # linear time with respect to the files.
	file_sizes, times, p_times = list(), list(), list() # for plots
	# files = shuffle(files)
	shuffle(files)
	for file in files: # linear pass over all files

		# if "url" in file:
		# 	# experiment = "all"
		# 	print("reading file: {}".format(file))
		# 	content = ""
		# 	with open(DIRECTORY + "/datasets/" + file, 'rb') as f: # avoiding unicode parse errors
		# 		content = f.read()
		# 	total_len = len(content)
		# 	print("file length: {}".format(total_len))
		# 	file_sizes.append(total_len)
		# 	pattern = 'jpg'
		# 	print("patter to search for:", pattern)
		# 	start_time = timer()
		# 	naive_matcher = NaiveStringMatch(content, pattern)
		# 	matches = naive_matcher.search()
		# 	print(len(matches))
		# 	end_time = timer()
		# 	times.append((end_time - start_time) )

		if "dna" in file:
			# experiment = "dna_"
			print("reading file: {}".format(file))

			content = ""
			with open(DIRECTORY + "/datasets/" + file, 'rb') as f: # avoiding unicode parse errors
				content = f.read()
			total_len = len(content)
			file_sizes.append(total_len)
			print("file length: {}".format(total_len))
			indices = pattern_generator(content, total_len)
			pattern = content[indices[0]:indices[1]]
			print("patter to search for: ", pattern)
			##################### NAIVE TIMER BEGINS ######################
			start_time = time.time()
			naive_matcher = NaiveStringMatch(content, pattern)
			matches = naive_matcher.search()
			print(len(matches))
			end_time = time.time()
			##################### NAIVE TIMER ENDS #######################
			###################### KMP TIMER BEGINS ######################
			# start_time = time.time()
			# kmp_matcher = KMP(pattern, content)
			# matches = kmp_matcher.KMPSearch()
			# end_time = time.time()
			###################### KMP TIMER ENDS #######################
			times.append((end_time - start_time) )
			print((end_time - start_time) * 10)
		print("--------------------------------------------------------------")
	p = Plotter()
	colors = itertools.cycle(5 * ["r", "g", "c", "b", "k"])
	p.plot_text_time(experiment="experiment", s_times=times, lengths=file_sizes, color = next(colors))
		# break

if __name__ == '__main__':
	main()


#using async
import asyncio
import random

async def produce(cookiejar, n):
    for x in range(1, n + 1):
        # produce an item
        print('producing {}/{}'.format(x, n))
        # simulate i/o operation using sleep
        print("<<producer waiting>>")
        await asyncio.sleep(random.random())
        print(">>producer waking<<")
        item = str(x)
        # put the item in the queue
        print("$producing "+item)
        await cookiejar.put(item)

    # indicate the producer is done
    await cookiejar.put(None)

async def consume(cookiejar):
    while True:
        # wait for an item from the producer
        print("<<consumer waiting>>")
        item = await cookiejar.get()
        print(">>consumer waking<<")
        if item is None:
            # the producer emits None to indicate that it is done
            break

        # process the item
        print('@consuming item {}...'.format(item))
        # simulate i/o operation using sleep
        await asyncio.sleep(random.random())

loop = asyncio.get_event_loop()
cookiejar = asyncio.Queue(loop=loop)
producer = produce(cookiejar, 10)
consumer = consume(cookiejar)
loop.run_until_complete(asyncio.gather(producer, consumer))
loop.close()


# ps1.

## ps1.2
## dfs를 수단이나 과정말고 결과를 이해하는 데 활용
## dfs로 구현했다고 하면, 주어지거나 전처리를 통해 자료구조화된 노드, 간선, 그래서 트리나 그래프 등의 개념이 전제되나 아래는 너무 추상적
## 단, 구현은 Recursive

# ps2.

# ps3.

## recursive 기반 구현의 특징은 depth, limit이다 왜냐하면, 이것이 있어야 무한재귀에 안빠지니까
## 그외 추가적인 파라미터를 활용하는 것으로 우선 이해

# ps4.
# 프로그래머스 타겟넘버
# https://programmers.co.kr/learn/courses/30/lessons/43165

# global ; answer
def recursive(depth,limit, sub_result,target):
    global answer, nums

    if depth == limit:
        if sub_result == target:
            answer += 1
        return
    else:
        # 현재 방문 노드 + 전제
        sub_result1= sub_result + nums[depth]
        recursive(depth+1, limit,sub_result1,target)
        # 현재 방문 노드 - 전제
        sub_result2= sub_result - nums[depth]
        recursive(depth+1, limit,sub_result2,target)

def solution(numbers, target):
    global answer, nums
    answer = 0
    nums=numbers[:]

    depth=0
    limit=len(numbers)
    result=0
    # list
    progress_result=[]

    recursive(depth,limit,0,target)

    return answer


__author__ = 'thecreator232'

from exceptions import UserNotFoundException

class Tweet(object):
    def __init__(self, tweet):
        self.tweet = tweet

    @staticmethod
    def text(self):
        """Returns the text of a tweet.

        Returns:
            unicode formatted string.
        """
        return self.tweet.get("text", "")

    @staticmethod
    def hashtag(self):
        """ Returns a list of hashtags associated with the tweet.

        Returns:
            List of hashtags.

        """
        return self.tweet.get("entities").get("hashtags", [])

    @staticmethod
    def username(self):
        """Returns the user name of the person or entity that published the tweet.
        Raises UserNotFoundException if username is not present in the tweet.

        Returns:
            unicode formatted string    .
        """
        try:
            self.username = self.tweet.get("user").get("name", False)
            if not self.username:
                raise AttributeError
            return self.username
        except AttributeError:
            raise UserNotFoundException


from bs4 import BeautifulSoup
import lxml
import pandas as pd
import re
df = pd.read_csv('react-native.csv')

print(df.head())

#content = df['body'].tolist()

###
  ### read body as a single string
###
content = df['body'].str.cat(sep=',')

print(len(content))

soup = BeautifulSoup(content, "lxml")

###
  ### remove any code snippets and HTML tags
###
for tag in soup.find_all(['code', 'a', 'pre']):
    tag.decompose()

###
  ### remove common English stop words and non-alphanumeric characters
###
text = soup.get_text(strip=True).replace("."," ").replace(","," ").replace("is "," ").replace("a "," ").replace("the "," ")

  ### remove digits
text = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", text)

#print(len(text))

  ### remove duplicate spaces
text = " ".join(text.split())
print(len(text))

#######################################################################################

  ### save output as text file, be sure encode as utf-8
###
textfile = open('react-native.txt', 'w', encoding='utf-8')
textfile.write(text)
textfile.close()


import matplotlib.pyplot as plt
import matplotlib.style
import csv
import numpy as np
import pandas as pd
import glob
import math
import re

matplotlib.style.use('ggplot')

class PlotStatistics:
    def __init__(self):
        pass

    def plotReward(self, pathToCSV, numMovAvg):
        data = pd.read_csv(pathToCSV)
        data = data.rolling(window=numMovAvg).mean()

        plt.plot(data)

        plt.title('Game Reward over Time')
        plt.xlabel('Number of Games')
        plt.ylabel('Game Reward')
        plt.show()

    def plotMultiple(self, pathToFolder, numMovAvg):
        reward_files = glob.glob(pathToFolder + "/Reward*.csv")

        if len(reward_files) == 0:
            print("No reward.csv files found")

        for n in range(math.ceil(len(reward_files)/4)): # Plot 4 rewards in one figure
            row = 0
            col = 0
            temp_files = reward_files[n*4:n*4+4]
            fig, axs = plt.subplots(2, 2)
            plt.figure(n+1)

            for i, path in enumerate(temp_files):
                if i%2 == 0 and i > 0:
                    row = row + 1

                data = pd.read_csv(path)
                data = data.rolling(window=numMovAvg).mean()
                axs[row, col].plot(data)
                # Regex for getting title
                title = re.findall("(?<=Reward_)(.*?)(?=.csv)", path)
                axs[row, col].set_title(title)

                col = col + 1
                if col > 1:
                    col = 0

            fig.tight_layout()
        plt.show()

    def plot_chromosome_3D(self, path_to_csv):
        # chromosomes_generations = pd.read_csv(path_to_csv)
        # print(np.array(chromosomes_generations)[:,0])

        fig = plt.figure()
        ax = fig.add_subplot(projection='3d')

        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                if i > 3:
                    print("Error more than 3 generations check csv file")

                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)
        gen_chromosomes = np.array(gen_chromosomes)
        print(len(gen_chromosomes[2][:,0]))
        ax.scatter(gen_chromosomes[0][:,0], gen_chromosomes[0][:,1], gen_chromosomes[0][:,2], marker='o', color=['red'], label='Generation 1') # generation 1 (x,y,z)
        ax.scatter(gen_chromosomes[1][:,0], gen_chromosomes[1][:,1], gen_chromosomes[1][:,2], marker='^', color=['blue'], label='Generation 50') # generation 2 (x,y,z)
        ax.scatter(gen_chromosomes[2][:,0], gen_chromosomes[2][:,1], gen_chromosomes[2][:,2], marker='*', color=['green'], label='Generation 100') # generation 3 (x,y,z)
        ax.legend()
        #for gen_chrom in gen_chromosomes:1, 1.5, 2.5, 3, 3.5, 6.5, 5, 6, 7, 8, 7.5
           # ax.scatter(gen_chrom[:,0], gen_chrom[:,1], gen_chrom[:,2])

        ax.set_xlabel(r'$\epsilon$')
        ax.set_ylabel(r'$\gamma$')
        ax.set_zlabel(r'$\alpha$')

        plt.show()

    def plot_chromosome_2D(self, path_to_csv, labels):
        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)

        gen_chromosomes = np.array(gen_chromosomes)

        plot_idx = 0
        prev = 0

        for chrom_i in range(0,(len(gen_chromosomes[0][0])),2):
            fig = plt.figure(plot_idx)
            ax = fig.add_subplot()
            # ax[plot_idx].axis('equal')
            ax.scatter(gen_chromosomes[0][:,chrom_i], gen_chromosomes[0][:,chrom_i+1], marker='o', color=['red']) # generation 1
            ax.scatter(gen_chromosomes[1][:,chrom_i], gen_chromosomes[1][:,chrom_i+1], marker='^', color=['blue']) # generation 2
            ax.scatter(gen_chromosomes[2][:,chrom_i], gen_chromosomes[2][:,chrom_i+1], marker='*', color=['green']) # generation 3
            ax.set_xlabel(labels[chrom_i])
            ax.set_ylabel(labels[chrom_i+1])

            if prev == chrom_i:
                plot_idx += 1

        plt.show()

    def plot_chromosome_2D_qlearning(self, path_to_csv, labels):
        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)

        gen_chromosomes = np.array(gen_chromosomes)

        plot_idx = 0
        prev = 0

        for chrom_i in range((len(gen_chromosomes[0][0]))):
            for chrom_j in range((len(gen_chromosomes[0][0]))):
                fig = plt.figure(plot_idx)
                ax = fig.add_subplot()
                if chrom_i >= chrom_j:
                    continue
                # ax[plot_idx].axis('equal')
                ax.scatter(gen_chromosomes[0][:,chrom_i], gen_chromosomes[0][:,chrom_j], marker='o', color=['red'], label='Generation 1') # generation 1
                ax.scatter(gen_chromosomes[1][:,chrom_i], gen_chromosomes[1][:,chrom_j], marker='^', color=['blue'], label='Generation 50') # generation 2
                ax.scatter(gen_chromosomes[2][:,chrom_i], gen_chromosomes[2][:,chrom_j], marker='*', color=['green'], label='Generation 100') # generation 3
               # ax.set_xlabel(r'$\theta$%s' %(chrom_i+1))
               # ax.set_ylabel(r'$\theta$%s' %(chrom_j+1))
                ax.set_xlabel(labels[chrom_i])
                ax.set_ylabel(labels[chrom_j])

                ax.legend()
                if prev == chrom_i:
                    plot_idx += 1

        plt.show()


import streamlit as st
import networkx as nx
import matplotlib.pyplot as plt

G, undirected_G = st.session_state.G, st.session_state.undirected_G

def visualize_network(G, plot_options):
    pos = nx.spring_layout(G, iterations=20, seed=1721)
    fig, ax = plt.subplots(figsize=(40, 15), dpi=10000)  # Increase dpi here
    ax.axis("off")
    nx.draw_networkx(G, pos=pos, ax=ax, **plot_options)
    st.pyplot(fig)

plot_options = {
    'node_color': 'blue',
    'node_size': 1000,
    'edge_color': 'gray',
    'linewidths': 0,
    'width': 100,
    'with_labels': True
}

# Visualize the network
visualize_network(G, plot_options)


# coding: utf-8
"""
The functions below use OpenCV functions to detect edges in a given frame.

Sources:
- cv2.equalizeHist: improve the contrast of the frame
    https://docs.opencv.org/3.2.0/d5/daf/tutorial_py_histogram_equalization.html

- cv2.Canny: edge detection.
    https://docs.opencv.org/3.3.1/da/d22/tutorial_py_canny.html

- cv2.GaussianBlur: removing gaussian noise from the frame.
    https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html

- cv2.getPerspectiveTransform & cv.warpPerspective: convert frame to a wrapped/flattened bird's eye view
    https://docs.opencv.org/3.4.0/da/d6e/tutorial_py_geometric_transformations.html

"""

# libraries and dependencies
# ---------------------------------------------------------------------------- #
import cv2
import numpy as np
import glob
# ---------------------------------------------------------------------------- #

def get_yellow_lanes(frame):
    """
    Apply HSV mask to a frame to highlight yellow lanes in the frame.

    OpenCV Docs:
        https://docs.opencv.org/3.0.0/df/d9d/tutorial_py_colorspaces.html
    """
    min_value = np.array([0, 70, 70]) # light red
    max_value = np.array([50, 255, 255]) # light blue

    # convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of yellow color in hsv
    lower_threshold = np.all(hsv > min_value, axis=2)
    upper_threshold = np.all(hsv < max_value, axis=2)

    # define a threshold to get only yellow colors.
    mask = np.logical_and(lower_threshold, upper_threshold)

    return mask

def get_white_lanes(frame):
    """
    Apply histogram equalization to a frame to improve the contrast of the frame
    then threshold it, which will help to highlight the white lines in the frame

    OpenCv Docs:
        https://docs.opencv.org/3.2.0/d5/daf/tutorial_py_histogram_equalization.html
    """
    # convert BGR to GrayScale
    grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    equalization_histogram = cv2.equalizeHist(grayscale)

    _, mask = cv2.threshold(equalization_histogram, thresh=250, maxval=255, type=cv2.THRESH_BINARY)

    return mask

def canny_edge_detection(frame, sigma=0.33, kernel_size=3):
    """
    Apply Canny edge detection to a frame, then threshold the result
    to highlight only the edges points in the frame

    OpenCV Docs:
        Canny - https://docs.opencv.org/3.3.1/da/d22/tutorial_py_canny.html
        GaussianBlur - https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html
    """
    # convert BGR to GrayScale
    grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Gaussian blurring is highly effective in removing gaussian noise from the frame.
    # kernel_size: should be positive and odd
    blurred = cv2.GaussianBlur(grayscale, (kernel_size, kernel_size), 0)

    # compute the median of the pixles intensity
    median = np.median(blurred)

    # calculate thresholds using the computed median
    lower_threshold = int(max(0, (1.0 - sigma) * median))
    upper_threshold = int(min(255, (1.0 + sigma) * median))

    #apply Canny edge detection
    return cv2.Canny(blurred, lower_threshold, upper_threshold)

def convert_to_bitmap(frame, kernel_size=3):
    """
    Convert a input frame to a bitmap (2D-array of zeros and ones)
    where only the pixels corresponding to lanes in the frame
    would be highlighted by assigning them with (ones)
    and the rest of the pixels would be ignored by assigning them to (zeros).
    """
    # create an empty bitmap and initialize all its values to zeros
    lanes_bitmap = np.zeros(shape=frame.shape[:2], dtype=np.uint8)

    # highlight yellow lanes by threshold in HSV color space
    lanes_bitmap = np.logical_or(lanes_bitmap, get_yellow_lanes(frame))

    # highlight white lanes by thresholding the equalized frame
    lanes_bitmap = np.logical_or(lanes_bitmap, get_white_lanes(frame))

    # apply canny_edge_detection algorithm to enhance lanes detection
    lanes_bitmap = np.logical_or(lanes_bitmap, canny_edge_detection(frame, kernel_size))

    # apply a morphological transformation to paint/fill the small gaps in the detected lines
    # basically to get a (solid line) instead of an (intermittent or dashed line)
    # for more information please check out this page:
    # https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
    bitmap = cv2.morphologyEx(
        lanes_bitmap.astype(np.uint8),
        cv2.MORPH_CLOSE,
        np.ones((kernel_size, kernel_size), np.uint8))

    return bitmap

def convert_to_birdseye_view(frame):
    """
    Convert frame to a wrapped/flattened bird's eye view.
    """
    height, width = frame.shape[:2]

    # define 4 points in the original space
    src = np.float32([[width, height],
                      [0, height],
                      [width/3, height * (2/3)],
                      [width/2, height * (2/3)]])

    # define 4 points in the warped space
    dst = np.float32([[width, height],
                      [0, height],
                      [0, 0],
                      [width, 0]])

    # compute transformation matrices
    forward_transformation_matrix = cv2.getPerspectiveTransform(src, dst)
    backward_transformation_matrix = cv2.getPerspectiveTransform(dst, src)

    # warp frame to get bird's eye view
    birdseye_view = cv2.warpPerspective(frame,
        forward_transformation_matrix,
        (width, height),
        flags=cv2.INTER_LINEAR)

    return birdseye_view, forward_transformation_matrix, backward_transformation_matrix


import csv

# ouvrir le fichier CSV en mode lecture
with open('data_comptage_nettoyees_v2.csv', newline='', encoding='utf-8') as csvfile:
    # créer un objet DictReader pour parcourir les lignes du fichier CSV
    csv_reader = csv.DictReader(csvfile, delimiter=';')

    # initialiser un dictionnaire pour stocker les couples (boucle_num, jour) déjà rencontrés
    couples_deja_rencontres = {}

    # initialiser une liste pour stocker les numéros de ligne des couples dupliqués
    lignes_dupliquees = []

    # parcourir les lignes du fichier CSV
    for i, row in enumerate(csv_reader):
        # extraire les valeurs de la colonne "boucle_num" et de la colonne "jour"
        boucle_num = row['boucle_num']
        jour = row['jour']

        # vérifier si ce couple a déjà été rencontré
        if (boucle_num, jour) in couples_deja_rencontres:
            # si oui, ajouter le numéro de ligne aux lignes dupliquées
            lignes_dupliquees.append(i + 1)  # ajouter 1 pour prendre en compte l'en-tête du fichier CSV
        else:
            # sinon, ajouter le couple au dictionnaire des couples déjà rencontrés
            couples_deja_rencontres[(boucle_num, jour)] = i

    # afficher les numéros de ligne des couples dupliqués
    if lignes_dupliquees:
        print(f"Les couples (boucle_num, jour) suivants sont dupliqués : {lignes_dupliquees}")
    else:
        print("Il n'y a pas de couples (boucle_num, jour) dupliqués dans le fichier CSV.")

1 - BDD/4 - Explications/fichiers/fuck.py


import torch
import seaborn as sns
import pandas as pd
from  sklearn.metrics import confusion_matrix
import torchaudio
from dataset import audioDataset
from train import SAMPLE_RATE, NUM_SAMPLES
from sklearn.metrics import (ConfusionMatrixDisplay, classification_report,
                             confusion_matrix)
import matplotlib.pyplot as plt
import torchvision

NUM_SAMPLES = 32000
SAMPLE_RATE = 16000

DATASET = "gender"
CNN = "resnet50"

if DATASET == "gender":
    AUDIO_DIR = "H:\\voxc2\\voxceleb2\\test\\aac" #replace with own path
if DATASET == "emotion":
    AUDIO_DIR = "EMOTION_TEST" # replace with own path

if (DATASET == "gender"):
        class_mapping = [
        "female",
        "male"
    ]
elif (DATASET == "emotion"):
    class_mapping = [
        "anger",
        "disgust",
        "fear",
        "happiness",
        "neutral",
        "sadness",
    ]
def predict(model, input, target, class_mapping):
    model.eval()
    with torch.no_grad():
        predictions = model(input)
        predicted_index = predictions[0].argmax(0)
        predicted = class_mapping[predicted_index]
        expected = class_mapping[target]
    return predicted, expected

if __name__ == "__main__":

    cuda_ = "cuda:0"
    print(cuda_)

    if torch.cuda.is_available():
        print("cuda available")
        device = "cpu"
    else:
        device = "cpu"
    print(f"Using {device}")

    if DATASET == "gender":
        if CNN == "densenet121":
            cnn = torchvision.models.densenet121(num_classes = 2)
        elif CNN == "alexnet":
            cnn = torchvision.models.alexnet(num_classes = 2)
        elif CNN == "mobilenetv3":
            cnn = torchvision.models.mobilenet_v3_small(num_classes = 2)
        elif CNN == "resnet50":
            cnn = torchvision.models.resnet50(num_classes = 2)
    elif DATASET == "emotion":
        if CNN == "densenet121":
            cnn = torchvision.models.densenet121(num_classes = 6)
        elif CNN == "alexnet":
            cnn = torchvision.models.alexnet(num_classes = 6)
        elif CNN == "mobilenetv3":
            cnn = torchvision.models.mobilenet_v3_small(num_classes = 6)
        elif CNN == "resnet50":
            cnn = torchvision.models.resnet50(num_classes = 6)

    state_dict = torch.load(f"models/{CNN}_{DATASET}_final.pth")

    cnn.load_state_dict(state_dict)
    cnn = cnn.to(device)

    # creating mel spectrogram
    mel_spectrogram = torchaudio.transforms.MelSpectrogram(
        sample_rate=SAMPLE_RATE,
        n_fft=1024,
        hop_length=512,
        n_mels=128,
    )

    # creating the custom dataset
    emotionData = audioDataset(
                            AUDIO_DIR,
                            None,
                            mel_spectrogram,
                            NUM_SAMPLES,
                            SAMPLE_RATE,
                            device,
                            DATASET)

    count = 0
    correct = 0
    skipped = 0
    y_true = []
    y_pred = []

    while count < len(emotionData):
        input = emotionData[count][0]
        target = emotionData[count][1]
        if target < len(class_mapping):
            input.unsqueeze_(0)
            predicted, expected = predict(cnn, input, target, class_mapping)
            if (predicted == expected):
                correct = correct + 1
            y_pred.append(class_mapping.index(predicted))
            y_true.append(class_mapping.index(expected))
        else:
            skipped = skipped +1
        count = count + 1

    # confusion matrix
    print("acc = ", correct / len(emotionData))
    print(skipped)
    print(classification_report(y_true=y_true, y_pred=y_pred, target_names=class_mapping, digits=4))
    cm = confusion_matrix(y_true=y_true, y_pred=y_pred)
    print(cm)
    print("result predicted ", predicted, " expected ", expected)

    # plots
    ax = plt.subplot()
    sns.set(font_scale=1.5)
    hm = sns.heatmap(cm, annot=True, ax=ax, fmt="g", cmap='Blues')
    ax.set_xlabel('Predicted class', size=17);
    ax.set_ylabel('Actual class', size=17);
    ax.set_title('');
    if DATASET == "emotion":
        ax.xaxis.set_ticklabels(['Anger', 'Disgust', "Fear", "Happiness", "neutral", "sadness"], size=12);
        ax.yaxis.set_ticklabels(['Anger', 'Disgust', "Fear", "Happiness", "neutral", "sadness"], size=12);
    if DATASET == "gender":
        ax.xaxis.set_ticklabels(['Female', 'Male'], size=12);
        ax.yaxis.set_ticklabels(['Female', 'Male'], size=12);
    plt.show()


import typing

import boto3

iot_client = boto3.client('iot')

def describe_thing(thing_name: str) -> dict:
    thing = iot_client.describe_thing(thingName=thing_name)
    thing.pop('ResponseMetadata', None)
    return thing

def list_things_in_thing_group(thing_group_name: str, callback: typing.Callable[[str], None]) -> None:
    response = None
    while not response or 'nextToken' in response:

        kwargs = {}
        if response and 'nextToken' in response:
            kwargs['nextToken'] = response['nextToken']

        response = iot_client.list_things_in_thing_group(
            thingGroupName=thing_group_name,
            recursive=True,
            maxResults=100,
            **kwargs
        )

        for thing_name in response['things']:
            callback(thing_name)

def search_index(query: str, index_name: str = 'AWS_Things') -> typing.List[str]:
    things = []

    response_list = None

    while not response_list or 'nextToken' in response_list:

        kwargs = {}
        if response_list and 'nextToken' in response_list:
            kwargs['nextToken'] = response_list['nextToken']

        response_list = iot_client.search_index(
            indexName=index_name,
            queryString=query,
            **kwargs)

        things.extend(response_list['things'])

    return things


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File    ：test_config.py
@Author  ：Zeno
@Email   ：zhangyongjun@ones.ai
@Date    ：2021/12/24
@Desc    ：甘特图测试用例
"""

from falcons.check import Checker
from falcons.com.nick import feature, story, parametrize, fixture

from main.api import project as prj
from main.params import plan

@fixture(scope='module')
def _gan_storage():
    """
    eg:
        {
          "item": {
            "create_time": 1640671442,
            "import_config": {
              "projects": []
            },
            "item_type": "gantt_chart",
            "key": "gantt_chart-5aHNoJq2",
            "name": "Gantt-kmLZ",
            "name_pinyin": "Gantt-kmLZ",
            "owner": "7tMnkPmX",
            "shared": false,
            "sync_from_project": true,
            "sync_to_project": true,
            "uuid": "5aHNoJq2"
          }
        }
    :return:
    """
    return []

@feature('甘特图')
class TestGanttCreate(Checker):
    @story('T22825 新建甘特图：不选择数据源')
    @parametrize('param', plan.create_gantt())
    def test_create_gan_no_project(self, param, _gan_storage):
        """"""
        param.json['item']['import_config']['projects'] = []  # 不选择数据源

        gan_add = self.call(prj.ItemsAdd, param)
        gan_add.check_response('item.name', param.json['item']['name'])

        _gan_storage.append(gan_add.json())

    @story('T22828 新建甘特图：选择数据源来自ONES Project')
    @parametrize('param', plan.create_gantt())
    def test_add_gant_with_project(self, param, _gan_storage):
        """"""

        gan_add = self.call(prj.ItemsAdd, param)
        gan_add.check_response('item.name', param.json['item']['name'])

        _gan_storage.append(gan_add.json())

    # @story('同步设置：甘特图数据还原到ONES Project')
    # @parametrize('param', plan.gantt_sync_to_proj())
    # def test_gant_sync_project(self, param, _gan_storage):
    #     """"""
    #
    #     gan_add = prj.ItemUpdate()
    #     gan_add.call(param.json, **param.extra)
    #     gan_add.is_response_code(200)

    @story('T22883 同步设置：甘特图数据同步到ONES Project')
    @parametrize('param', plan.gantt_sync_to_proj())
    def test_gant_sync_project(self, param, _gan_storage):
        """"""

        g_uuid = _gan_storage[0]['item']['uuid']
        # 先在甘特图下创建一个任务，用于同步
        a_param = plan.create_task()[0]
        a_param.json['item']['gantt_chart_uuid'] = g_uuid

        gan_add = self.call(prj.ItemsAdd, a_param)
        task_uuid = gan_add.json()['item']['uuid']

        param.json['uuids'] = [task_uuid, ]
        param.uri_args({'gantt_uuid': g_uuid})

        self.call(prj.GanttSync, param)

    @story('T22900 同步设置：同步到「ONES Project」功能开关')
    @story('T22899 同步设置：自动更新导入的项目和迭代')
    @parametrize('param', plan.gantt_close_sync())
    def test_close_sync(self, param, _gan_storage):
        """"""
        g_uuid = _gan_storage[0]['item']['uuid']
        g_key = _gan_storage[0]['item']['key']
        param.json['item']['uuid'] = g_uuid
        param.uri_args({'item_key': g_key})

        gan_update = self.call(prj.ItemUpdate, param)
        gan_update.check_response('item.sync_to_project', False)

    @story('T22894 甘特图名称：修改甘特图名称')
    @parametrize('param', plan.update_gantt())
    def test_modify_gantt_name(self, param, _gan_storage):
        """"""
        item = _gan_storage[0]['item']
        param.json['item']['uuid'] = item['uuid']
        param.uri_args({'item_key': item['key']})

        gan_update = self.call(prj.ItemUpdate, param)
        gan_update.check_response('item.name', param.json['item']['name'])

        _gan_storage[0] = gan_update.json()

    @story('T23720 可见范围：修改甘特图可见范围为共享')
    @parametrize('param', plan.share_gantt())
    def test_share_gantt(self, param, _gan_storage):
        """"""
        item = _gan_storage[0]['item']
        param.json['item']['uuid'] = item['uuid']
        param.uri_args({'item_key': item['key']})

        gan_update = self.call(prj.ItemUpdate, param)
        gan_update.check_response('item.name', param.json['item']['name'])
        gan_update.check_response('item.shared', True)

        _gan_storage[0] = gan_update.json()

    @story('T22895 可见范围：修改甘特图可见范围为私人')
    @parametrize('param', plan.private_gantt())
    def test_private_gantt(self, param, _gan_storage):
        """"""
        item = _gan_storage[0]['item']
        param.json['item']['uuid'] = item['uuid']
        param.uri_args({'item_key': item['key']})

        gan_update = self.call(prj.ItemUpdate, param)

        gan_update.check_response('item.name', param.json['item']['name'])
        gan_update.check_response('item.shared', False)

        _gan_storage[0] = gan_update.json()

    @story('T22898 删除甘特图')
    @parametrize('param', plan.delete_item())
    def test_delete_gantt(self, param, _gan_storage):
        """"""
        gantt_keys = [g['item']['key'] for g in _gan_storage]
        gan_add = prj.ItemDelete()

        for key in gantt_keys:
            param.uri_args({'item_key': key})
            gan_add.call(param.json, **param.extra)
            gan_add.is_response_code(200)
            gan_add.check_response('code', 200)