import datetime
from FileBlackHolePy import FileBlackHole, initLib, destroyLib
from MigurdiaPy      import Migurdia
from json            import dumps, loads
from colors          import log, bcolors
from PIL             import Image
from credentials     import __USERNAME__, __PASSWORD__, __TOKEN__
from os.path         import getsize, isfile, isdir
from pixivpy_async   import AppPixivAPI
from os              import remove, rename, mkdir
from shutil          import copyfile
from random          import randint

import aiohttp
import asyncio

tempFolder        = f"./temp-{datetime.datetime.now()}"
migurdiaSessionID = ""

async def isValidImageFile(path):
    try:
        Image.open(path).verify()
    except:
        return False

    return True

async def getThumbnail(path):
    if not await isValidImageFile(path):
        copyfile(path, path + "bc")
        log(f"[!] File {path} is not a valid image.", [bcolors.FAIL])
        return None

    image = Image.open(path)

    pixelArea   = image.size[0] * image.size[1]
    aspectRatio = image.size[0] / image.size[1]

    # return path as it is small itself
    if getsize(path) < 512000: return path

    # begin creating thumbnail by creating path for it
    path = path.split('.')
    path = '.'.join( path[:-1] + ["thumbnail", path[-1]] )

    # convert to jpeg
    image = image.convert('RGB')

    # if res is small, save it as jpeg and return
    if pixelArea <= (512 * 512):
        image.save( path )
        return path

    # calculate new size for image (keeping aspect ratio)
    newWidth  = ((512 * 512) * aspectRatio) ** 0.5
    newHeight =  (512 * 512) / newWidth

    size = (int(newWidth), int(newHeight))

    image = image.resize(size)
    image.save(path)

    return path

class PixivScraper(Migurdia):
    downloader = None
    pixivApp   = None

    def __init__(self):
        super().__init__()

        connector = aiohttp.TCPConnector(limit=4)
        self.downloader = aiohttp.ClientSession(connector=connector)
        self.pixivApp   = AppPixivAPI()

    async def quit(self):
        await self.downloader.close()

    async def login(self, migurdiaUsername, migurdiaPassword, pixivRefreshToken):
        await self.pixivApp.login(refresh_token=pixivRefreshToken)
        await super().login(migurdiaUsername, migurdiaPassword)

    async def downloadFile(self, url, tries=6):
        if tries == 0: return None

        localFilename = f"{tempFolder}/{url.split('/')[-1]}"

        if isfile(localFilename): remove(localFilename)

        hs = {
            'referer'      : 'https://www.pixiv.net/',
            'cache-control': 'no-cache',
            'pragma'       : 'no-cache'
        }

        try:
            async with self.downloader.get(url, headers=hs) as response:
                with open(localFilename, 'wb+') as file:
                    file.write( await response.read() )

            if not await isValidImageFile(localFilename):
                remove(localFilename)
                raise Exception("Downloaded file is invalid.")
        except:
            x = random.randint(5, 100)
            log(f"[!] Failed to download file ({tries} tries left). Waiting {x} seconds.", [bcolors.WARNING])
            await asyncio.sleep(x)
            return await self.downloadFile(url, tries - 1)
        return localFilename

    async def addPixivFile(self, fb, fileUrl, title, desc, tags):
        path = await self.downloadFile(fileUrl)

        if path is None:
            log(f"[!] Failed to download file {fileUrl}.", [bcolors.FAIL])
            return None

        thumbnailPath = await getThumbnail(path)
        if thumbnailPath != path: remove(path)

        if thumbnailPath is None:
            log(f"[!] Failed to create thumbnail of {path}.", [bcolors.FAIL])
            return None

        thumbnailCode = await fb.uploadFile(thumbnailPath)
        remove(thumbnailPath)

        if thumbnailCode is None or thumbnailCode['exitCode'] != 0:
            log(f"[!] Failed to upload thumbnail of {path}.", [bcolors.FAIL])
            return None

        result = await super().addPost(
            fileUrl,
            f"https://fileblackhole.000webhostapp.com/files/{thumbnailCode['result']}",
            tags,
            title,
            desc
        )

        if result             is None: log(f"[!] Failed to process file {path}", [bcolors.FAIL])
        if result['exitCode'] !=    0:
            log(f"[!] Failed to add post for {path}. (CODE: {result['exitCode']})", [bcolors.FAIL])

        return result['result'][0]['result']

    async def addPixivPost(self, fb, post):
        log(f"[*] Processing post (ID: {post.id}).", [bcolors.OKCYAN])

        urls = []

        if post.page_count > 1: urls = [ post.image_urls.original for post in post.meta_pages ]
        else:                   urls = [ post.meta_single_page.original_image_url             ]

        tags = [ tag.name for tag in post.tags ]
        tags.append(post.user.name)

        tasks = []
        for url in urls:
            tasks.append( self.addPixivFile(fb, url, post.title, post.caption, tags) )

        result = await asyncio.gather(*tasks)

        log(f"[*] Successfully processed post id {post.id}", [bcolors.OKGREEN])

        for r in result:
            if r is None:
                with open(f"{tempFolder}/{post.id}", 'w+') as f:
                    f.write( dumps(result) )

    async def scrapePixivAuthor(self, authorID):
        log(f"[*] Scraping author id {authorID}", [bcolors.OKCYAN])

        fileBlackHole = FileBlackHole()
        await fileBlackHole.createSession()

        tasksPA   = []
        next_qs = { 'user_id': authorID }
        while True:
            result = await self.pixivApp.user_illusts(**next_qs)

            if result.illusts is None: break

            for illust in result.illusts:
                tasksPA.append( self.addPixivPost(fileBlackHole, illust) )

            next_qs = self.pixivApp.parse_qs(result.next_url)

            if next_qs is None: break

        await asyncio.gather(*tasksPA)
        await fileBlackHole.close()

        log(f"[*] Successfully scraped author id {authorID}", [bcolors.OKGREEN])

if not isdir(tempFolder):
    mkdir(tempFolder)

async def main():
    with open("final.json") as f:
        authors = loads(f.read())

    await initLib()
    scraper = PixivScraper()
    await scraper.login(__USERNAME__, __PASSWORD__, __TOKEN__)

    tasksA = []
    for i in range(len(authors)):
        if i % 3 == 0:
            await asyncio.gather(*tasksA)
            tasksA = []

        tasksA.append( scraper.scrapePixivAuthor(int(authors[i])) )

    await scraper.quit()
    await destroyLib()

if __name__ == "__main__":
    asyncio.run(main())


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import unittest

import apache_beam as beam
from apache_beam.runners.direct import direct_runner
from apache_beam.runners.interactive import cache_manager as cache
from apache_beam.runners.interactive import pipeline_analyzer

def to_stable_runner_api(p):
  """The extra round trip ensures a stable pipeline proto.
  """
  return (
      beam.pipeline.Pipeline.from_runner_api(
          p.to_runner_api(use_fake_coders=True), p.runner,
          p._options).to_runner_api(use_fake_coders=True))

class PipelineAnalyzerTest(unittest.TestCase):
  def setUp(self):
    self.runner = direct_runner.DirectRunner()
    self.cache_manager = cache.FileBasedCacheManager()

  def tearDown(self):
    self.cache_manager.cleanup()

  def assertPipelineEqual(self, pipeline_proto1, pipeline_proto2):
    """A naive check for Pipeline proto equality.
    """
    components1 = pipeline_proto1.components
    components2 = pipeline_proto2.components

    self.assertEqual(len(components1.transforms), len(components2.transforms))
    self.assertEqual(
        len(components1.pcollections), len(components2.pcollections))

    # GreatEqual instead of Equal because the pipeline_proto_to_execute could
    # include more windowing_stratagies and coders than necessary.
    self.assertGreaterEqual(
        len(components1.windowing_strategies),
        len(components2.windowing_strategies))
    self.assertGreaterEqual(len(components1.coders), len(components2.coders))
    self.assertTransformEqual(
        pipeline_proto1,
        pipeline_proto1.root_transform_ids[0],
        pipeline_proto2,
        pipeline_proto2.root_transform_ids[0])

  def assertTransformEqual(
      self, pipeline_proto1, transform_id1, pipeline_proto2, transform_id2):
    """A naive check for Transform proto equality.
    """
    transform_proto1 = pipeline_proto1.components.transforms[transform_id1]
    transform_proto2 = pipeline_proto2.components.transforms[transform_id2]
    self.assertEqual(transform_proto1.spec.urn, transform_proto2.spec.urn)
    # Skipping payload checking because PTransforms of the same functionality
    # could generate different payloads.
    self.assertEqual(
        len(transform_proto1.subtransforms),
        len(transform_proto2.subtransforms))
    self.assertSetEqual(
        set(transform_proto1.inputs), set(transform_proto2.inputs))
    self.assertSetEqual(
        set(transform_proto1.outputs), set(transform_proto2.outputs))

  def test_basic(self):
    p = beam.Pipeline(runner=self.runner)

    # The cold run.
    pcoll = (
        p
        | 'Create' >> beam.Create([1, 2, 3])
        | 'Double' >> beam.Map(lambda x: x * 2)
        | 'Square' >> beam.Map(lambda x: x**2))
    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, to_stable_runner_api(p), self.runner)
    pipeline_to_execute = beam.pipeline.Pipeline.from_runner_api(
        analyzer.pipeline_proto_to_execute(), p.runner, p._options)
    pipeline_to_execute.run().wait_until_finish()
    self.assertEqual(
        len(analyzer.tl_required_trans_ids()),
        7  # Create, Double, Square, CacheSample * 3, CacheFull
    )
    self.assertEqual(len(analyzer.tl_referenced_pcoll_ids()), 3)
    self.assertEqual(len(analyzer.read_cache_ids()), 0)
    self.assertEqual(len(analyzer.write_cache_ids()), 4)

    # The second run.
    _ = (
        pcoll
        | 'Triple' >> beam.Map(lambda x: x * 3)
        | 'Cube' >> beam.Map(lambda x: x**3))
    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, to_stable_runner_api(p), self.runner)
    pipeline_to_execute = beam.pipeline.Pipeline.from_runner_api(
        analyzer.pipeline_proto_to_execute(), p.runner, p._options)
    self.assertEqual(
        len(analyzer.tl_required_trans_ids()),
        6  # Read, Triple, Cube, CacheSample * 2, CacheFull
    )
    self.assertEqual(len(analyzer.tl_referenced_pcoll_ids()), 3)
    self.assertEqual(len(analyzer.read_cache_ids()), 1)
    self.assertEqual(len(analyzer.write_cache_ids()), 3)

    # No need to actually execute the second run.

  def test_word_count(self):
    p = beam.Pipeline(runner=self.runner)

    class WordExtractingDoFn(beam.DoFn):
      def process(self, element):
        text_line = element.strip()
        words = text_line.split()
        return words

    # Count the occurrences of each word.
    pcoll1 = p | beam.Create(['to be or not to be that is the question'])
    pcoll2 = pcoll1 | 'Split' >> beam.ParDo(WordExtractingDoFn())
    pcoll3 = pcoll2 | 'Pair with One' >> beam.Map(lambda x: (x, 1))
    pcoll4 = pcoll3 | 'Group' >> beam.GroupByKey()
    pcoll5 = pcoll4 | 'Count' >> beam.Map(lambda item: (item[0], sum(item[1])))

    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, to_stable_runner_api(p), self.runner)

    cache_label1 = 'PColl-1111111'
    cache_label2 = 'PColl-2222222'
    cache_label3 = 'PColl-3333333'
    cache_label4 = 'PColl-4444444'
    cache_label5 = 'PColl-5555555'

    # pylint: disable=expression-not-assigned
    pcoll1 | 'CacheSample%s' % cache_label1 >> cache.WriteCache(
        self.cache_manager, cache_label1, sample=True, sample_size=10)
    pcoll2 | 'CacheSample%s' % cache_label2 >> cache.WriteCache(
        self.cache_manager, cache_label2, sample=True, sample_size=10)
    pcoll3 | 'CacheSample%s' % cache_label3 >> cache.WriteCache(
        self.cache_manager, cache_label3, sample=True, sample_size=10)
    pcoll4 | 'CacheSample%s' % cache_label4 >> cache.WriteCache(
        self.cache_manager, cache_label3, sample=True, sample_size=10)
    pcoll5 | 'CacheSample%s' % cache_label5 >> cache.WriteCache(
        self.cache_manager, cache_label3, sample=True, sample_size=10)
    pcoll5 | 'CacheFull%s' % cache_label5 >> cache.WriteCache(
        self.cache_manager, cache_label3)
    expected_pipeline_proto = to_stable_runner_api(p)

    self.assertPipelineEqual(
        analyzer.pipeline_proto_to_execute(), expected_pipeline_proto)

    pipeline_to_execute = beam.pipeline.Pipeline.from_runner_api(
        analyzer.pipeline_proto_to_execute(), p.runner, p._options)
    pipeline_to_execute.run().wait_until_finish()

  def test_write_cache_expansion(self):
    p = beam.Pipeline(runner=self.runner)

    pcoll1 = p | 'Create' >> beam.Create([1, 2, 3])
    pcoll2 = pcoll1 | 'Double' >> beam.Map(lambda x: x * 2)
    pcoll3 = pcoll2 | 'Square' >> beam.Map(lambda x: x**2)
    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, to_stable_runner_api(p), self.runner)

    cache_label1 = 'PColl-1234567'
    cache_label2 = 'PColl-7654321'
    cache_label3 = 'PColl-3141593'

    # pylint: disable=expression-not-assigned
    pcoll1 | 'CacheSample%s' % cache_label1 >> cache.WriteCache(
        self.cache_manager, cache_label1, sample=True, sample_size=10)
    pcoll2 | 'CacheSample%s' % cache_label2 >> cache.WriteCache(
        self.cache_manager, cache_label2, sample=True, sample_size=10)
    pcoll3 | 'CacheSample%s' % cache_label3 >> cache.WriteCache(
        self.cache_manager, cache_label3, sample=True, sample_size=10)
    pcoll3 | 'CacheFull%s' % cache_label3 >> cache.WriteCache(
        self.cache_manager, cache_label3)
    expected_pipeline_proto = to_stable_runner_api(p)

    # Since WriteCache expansion leads to more than 50 PTransform protos in the
    # pipeline, a simple check of proto map size is enough.
    self.assertPipelineEqual(
        analyzer.pipeline_proto_to_execute(), expected_pipeline_proto)

  def test_read_cache_expansion(self):
    p = beam.Pipeline(runner=self.runner)

    # The cold run.
    pcoll = (
        p
        | 'Create' >> beam.Create([1, 2, 3])
        | 'Double' >> beam.Map(lambda x: x * 2)
        | 'Square' >> beam.Map(lambda x: x**2))
    pipeline_proto = to_stable_runner_api(p)

    pipeline_info = pipeline_analyzer.PipelineInfo(pipeline_proto.components)
    pcoll_id = 'ref_PCollection_PCollection_12'  # Output PCollection of Square
    cache_label1 = pipeline_info.cache_label(pcoll_id)

    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, pipeline_proto, self.runner)
    pipeline_to_execute = beam.pipeline.Pipeline.from_runner_api(
        analyzer.pipeline_proto_to_execute(), p.runner, p._options)
    pipeline_to_execute.run().wait_until_finish()

    # The second run.
    _ = (
        pcoll
        | 'Triple' >> beam.Map(lambda x: x * 3)
        | 'Cube' >> beam.Map(lambda x: x**3))
    analyzer = pipeline_analyzer.PipelineAnalyzer(
        self.cache_manager, to_stable_runner_api(p), self.runner)

    expected_pipeline = beam.Pipeline(runner=self.runner)
    pcoll1 = (
        expected_pipeline
        | 'Load%s' % cache_label1 >> cache.ReadCache(
            self.cache_manager, cache_label1))
    pcoll2 = pcoll1 | 'Triple' >> beam.Map(lambda x: x * 3)
    pcoll3 = pcoll2 | 'Cube' >> beam.Map(lambda x: x**3)

    cache_label2 = 'PColl-7654321'
    cache_label3 = 'PColl-3141593'

    # pylint: disable=expression-not-assigned
    pcoll2 | 'CacheSample%s' % cache_label2 >> cache.WriteCache(
        self.cache_manager, cache_label2, sample=True, sample_size=10)
    pcoll3 | 'CacheSample%s' % cache_label3 >> cache.WriteCache(
        self.cache_manager, cache_label3, sample=True, sample_size=10)
    pcoll3 | 'CacheFull%s' % cache_label3 >> cache.WriteCache(
        self.cache_manager, cache_label3)

    # Since ReadCache & WriteCache expansion leads to more than 50 PTransform
    # protos in the pipeline, a simple check of proto map size is enough.
    self.assertPipelineEqual(
        analyzer.pipeline_proto_to_execute(),
        to_stable_runner_api(expected_pipeline))

class PipelineInfoTest(unittest.TestCase):
  def setUp(self):
    self.runner = direct_runner.DirectRunner()

  def test_passthrough(self):
    """
    Test that PTransforms which pass through their input PCollection can be
    used with PipelineInfo.
    """
    class Passthrough(beam.PTransform):
      def expand(self, pcoll):
        return pcoll

    p = beam.Pipeline(runner=self.runner)
    p | beam.Impulse() | Passthrough()  # pylint: disable=expression-not-assigned
    proto = to_stable_runner_api(p).components
    info = pipeline_analyzer.PipelineInfo(proto)
    for pcoll_id in info.all_pcollections():
      # FIXME: If PipelineInfo does not support passthrough PTransforms, this
      #        will only fail some of the time, depending on the ordering of
      #        transforms in the Pipeline proto.

      # Should not throw exception
      info.cache_label(pcoll_id)

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


from datetime import datetime

def foo(n):
    n = int(n)
    global start_time
    start_time = datetime.now()
    list = []
    i = 0
    while len(list) < n:
        flag = True
        if i == 0 or i == 1:
            i += 1
            continue
        for j in range(i+1):
            if j == 0 or j == 1 or j == i:
                continue
            if i % j == 0:
                flag = False
        if flag == True:
            list.append(i)
        i += 1
    return list

print(foo(n=input('Write: ')))
print(datetime.now() - start_time)


#Python imports
import json
from uuid       import UUID
from datetime   import date
from datetime   import datetime
from typing     import Optional, List

#Pydantic imports
from pydantic import Field as FD
from pydantic import BaseModel as BMW
from pydantic import EmailStr

#FastAPI imports
from fastapi import FastAPI
from fastapi import status, HTTPException
from fastapi import Body, Path, Form

#This is the app
app = FastAPI()

#Those are the models to use
class UserBase(BMW):

    user_id : UUID = FD(
        ...,
    )

    email: EmailStr = FD(
        ...,
        example=""
    )

class UserLogin(UserBase):

    password: str = FD(
        ...,
        min_length=8,
        max_length=22,
        example=""  ,
    )

class User(UserBase):

    user_name: str = FD(
        ...,
        min_length=5,
        max_length=15,
        example="@cordellatt"
    )
    first_name : str = FD(
        ...,
        min_length=1,
        max_length=20,
        example="",
    )

    last_name : str = FD(
        ...,
        min_length=1,
        max_length=20,
        example="",
    )

    birth_date: Optional[date] = FD(default=None)

class UserRegister(User):

    password: str = FD(
        ...,
        min_length=8,
        max_length=22,
        example=""
    )

class Tweet(BMW):

    tweet_id : str = FD(
        ...,
        min_length=5,
        max_length=15,
    )

    content: str = FD(
        ...,
        min_length=1,
        max_length=256,
        example="Hello dear. This is the first tweet from my Api"
    )

    created_at: datetime = FD(default=datetime.now())
    updated_at: Optional[datetime] = FD(default=None)
    by: UserBase = FD (...)

# Troll
@app.post(
    path="/F",
    tags=["Troll"]
    )
def troll():
    return "Me está llevando el diablo, send help"

#Home: Here you can see all tweets
@app.get(
    path="/",
    response_model= List[Tweet],
    status_code= status.HTTP_200_OK,
    summary="Show all the tweets",
    tags=["Home"],
)
def home():
    """
    Show all tweets

    In this page you can see all the tweers in the app

    Parameters:
        -

    Returns a Json whith the basics users information:

    - tweet:id : UUID
    - content : str
    - created : datetime
    - updated : Optional[datetime]
    - by: UserBase
    """
    with open("tweets.json", "r", encoding="utf-8") as tweets_data:
        results = json.loads(tweets_data.read())
        return results

#Login
@app.post(
    path="/login/{user_name}",
    response_model= User,
    status_code= status.HTTP_200_OK,
    summary="Login a user",
    tags=["User"],
)
def login(
            user_name: str = Path(..., title="User name"),
            password: str = Form(..., title="Password")
    ):
    """
    Login

    This Path Operation login the user in the app...

    Parameters:
        - Request body parameter:
        - user: UserRegister

    Returns a Json whith the basics users information:

        - user id : UUID
        - email : Emailstr
        - first name: str
        - last name: str
        - birth date: datetime
    """
    with open("user.json", "r", encoding="utf-8") as user_data:
        result: list[dict] = json.load(user_data)

    user_position = 0

    while user_position < len(result) + 1:

        for user in result:

            if result[user_position]["user_name"] == user_name and result[user_position]["password"] == password:
                return user

            else:
                user_position = user_position + 1

            if user_position == len(result):
                raise HTTPException(
                status_code = status.HTTP_404_NOT_FOUND,
                detail="That user don't exist!")

## Path Operations

### Users

#### Register a user
@app.post(
    path="/signup",
    response_model= User,
    status_code= status.HTTP_201_CREATED,
    summary="Register a new User",
    tags=["User"],
)
def signup(user: UserRegister = Body(...)):
    """
    Sign-Up

    Register a user in the app

    Parameters:
        - Request body parameter
            - user: UserRegister

    Returns a Json whith the basice user information:

        - user id : UUID
        - email : Emailstr
        - first name: str
        - last name: str
        - birth date: datetime
    """
    with open("user.json", "r+", encoding="utf-8") as user_data:
        results = json.loads(user_data.read())
        user_dict = user.dict()
        user_dict["user_id"] = str(user_dict["user_id"])
        user_dict["birth_date"] = str(user_dict["birth_date"])
        results.append(user_dict)
        user_data.seek(0)
        user_data.write(json.dumps(results))
        return user

#### Show all user
@app.get(
    path="/users",
    response_model= List[User],
    status_code= status.HTTP_200_OK,
    summary="Show all the users",
    tags=["User"],
)
def show_all_users():
    """
    Show all users

    In this page you can see all the users in the app

    Parameters:
        - Request body parameter:
        - user: UserRegister

    Returns a Json whith the basics users information:

        - user id : UUID
        - email : Emailstr
        - first name: str
        - last name: str
        - birth date: datetime
    """
    with open("user.json", "r", encoding="utf-8") as user_data:
        results = json.loads(user_data.read())
        return results

#### Show a user:
@app.get(
    path="/user/{user_name}",
    response_model= User,
    status_code= status.HTTP_200_OK,
    summary="Show a User",
    tags=["User"],
)
def show_a_user(user_name: str = Path(..., title="User name")):
    """
    Show a user

    In this page you can see a user with her user name

    Parameters:
    - User's user name

    Returns a Json whith the basics users information:

    - user id : UUID
    - email : Emailstr
    - user name: str
    - first name: str
    - last name: str
    - birth date: datetime
    """
    with open("user.json", "r", encoding="utf-8") as user_data:
        result: list[dict] = json.load(user_data)

    user_position = 0

    while user_position < len(result) + 1:

        for user in result:

            if result[user_position]["user_name"] == user_name:
               return user

            if not result[user_position]["user_name"] == user_name:
                user_position = user_position + 1

            if user_position == len(result):
                raise HTTPException(
                status_code = status.HTTP_404_NOT_FOUND,
                detail="That user don't exist!")

#### Delete a user
@app.delete(
    path="/user/{user_name}/delete",
    response_model= str,
    status_code= status.HTTP_200_OK,
    summary="Delete a user",
    tags=["User"],
)
def delete_user(user_name: str = Path(..., title="User name")):
    """
    Delete a user

    In this page you can delete a user with her user name

    Parameters:
    - User's user name

    Returns a message with the result
    """
    with open("user.json", "r+", encoding="utf-8") as user_data:
        result: list[dict] = json.load(user_data)

        for user in result:

            if user["user_name"] == str(user_name):
                result.remove(user)

                with open("user.json", "w", encoding="utf-8") as user_data:
                    user_data.seek(0)
                    json.dump(result, user_data)
                return f"The user {user_name} was be eliminated"

        raise HTTPException(
            status_code = status.HTTP_404_NOT_FOUND,
            detail="This user don't exist!"
            )

#### Update a user
@app.put(
    path="/user/{user_name}/update/",
    response_model= User,
    status_code= status.HTTP_200_OK,
    summary="Update a user",
    tags=["User"],
)
def update_user(

    user_name : str = Path(
    ...,
    title="User name",
    description="Please insert the user that you want update",
    ),

    user: UserRegister = Body(...),
):
    """

    Update a user

    In this page you can update a user with his user name

    Parameters:
        user name: str
        user: UserRegister
        -

    Returns the new tweet model
    """

    user_name = str(user_name)
    user_dict = user.dict()
    user_dict['user_id'] = str(user_dict['user_id'])
    user_dict['birth_date'] = str(user_dict['birth_date'])

    with open("user.json", "r+", encoding="utf-8") as user_data:

        result = json.loads(user_data.read())

        for user in result:

            if  user['user_name'] == user_name:
                result[result.index(user)] = user_dict

                with open("user.json", "w", encoding="utf-8") as user_data:
                    user_data.seek(0)

                    user_data.write(json.dumps(result))
                return user_dict

            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Ese usuario no existe")

### Tweets

#### Post a tweet
@app.post(
    path="/post",
    response_model=Tweet,
    status_code=status.HTTP_201_CREATED,
    summary="Post a tweet",
    tags=["Tweets"]
)
def post_a_tweet(tweet : Tweet = Body(...)):
    """
    Post a Tweet

    In this page you can create a new tweet

    Parameters:
    - Request body parameter:
        - tweet: Tweets

    Returns a Json whith the basics users information:

    - tweet:id : UUID
    - content : str
    - created : datetime
    - updated : Optional[datetime]
    - by: UserBase
    """

    with open("tweets.json", "r+", encoding="utf-8") as tweet_data:
        results = json.loads(tweet_data.read())
        tweet_dict = tweet.dict()
        tweet_dict["tweet_id"] = str(tweet_dict["tweet_id"])
        tweet_dict["created_at"] = str(tweet_dict["created_at"])
        if tweet_dict["updated_at"] is not None:
            tweet_dict["updated_at"] = str(tweet_dict["updated_at"])

        tweet_dict["by"]["user_id"] = str(tweet_dict["by"]["user_id"])
        tweet_dict["by"]["email"] = str(tweet_dict["by"]["email"])
        results.append(tweet_dict)
        tweet_data.seek(0)
        tweet_data.write(json.dumps(results))
        return tweet

#### Show a tweet
@app.get(
    path="/tweets/{tweet_id}",
    response_model= Tweet,
    status_code= status.HTTP_200_OK,
    summary="Show a tweet",
    tags=["Tweets"],
)
def show_a_tweet(tweet_id: str = Path(..., title="First name")):
    """
    Show a tweet

    In this page you can see a user with her first name

    Parameters:
    - User's first name

    Returns a Json whith the basics users information:

    - tweet:id : UUID
    - content : str
    - created : datetime
    - updated : Optional[datetime]
    - by: UserBase
    """
    with open("tweets.json", "r", encoding="utf-8") as tweets_data:
        result: list[dict] = json.load(tweets_data)

    tweets_position = 0

    while tweets_position < len(result) + 1:
        for tweets in result:
            if result[tweets_position]["tweet_id"] == tweet_id:
               return tweets

            if not result[tweets_position]["tweet_id"] == tweet_id:
                tweets_position = tweets_position + 1

            if tweets_position == len(result):
                raise HTTPException(
                status_code = status.HTTP_404_NOT_FOUND,
                detail="That tweet don't exist!")

#### Delete a tweet
@app.delete(
    path="/tweets/{tweet_id}/delete",
    response_model= str,
    status_code= status.HTTP_200_OK,
    summary="Delete a tweet",
    tags=["Tweets"],
)
def delete_tweet(tweet_id : str = Path(..., title="Tweet Id")):
    """
    Delete a tweet

    In this page you can delete a tweet with his id

    Parameters:
        -

    Returns a message with the result.
    """
    with open("tweets.json", "r+", encoding="utf-8") as tweets_data:
        result: list[dict] = json.load(tweets_data)

        for tweet in result:

            if tweet["tweet_id"] == str(tweet_id):
                result.remove(tweet)

                with open("tweets.json", "w", encoding="utf-8") as tweets_data:
                    tweets_data.seek(0)
                    json.dump(result, tweets_data)
                    return f"The tweet {tweet_id} was be eliminated"

        raise HTTPException(
            status_code = status.HTTP_404_NOT_FOUND,
            detail="This tweet don't exist!"
            )

#### Update a tweet
@app.put(
    path="/tweets/{tweet_id}/update",
    response_model= Tweet,
    status_code= status.HTTP_200_OK,
    summary="Update a tweet",
    tags=["Tweets"],
)
def update_tweet(

        tweet_id : str = Path(
            ...,
            title="Tweet Id"
            ),

        new_content: str = Form(
            ...,
            title="New tweet information")

):
    """
    Update a tweet

    In this page you can update a tweet content with his id

    Parameters:
        tweet id: str
        new_content: str
        -

    Returns the new tweet model
    """
    with open("tweets.json", "r+", encoding="utf-8") as tweets_data:
        result: list[dict] = json.load(tweets_data)

        for tweet in result:

            if tweet['tweet_id'] == str(tweet_id):
                tweet['content'] = new_content

                with open("tweets.json", "w", encoding="utf-8") as tweets_data:
                    tweets_data.seek(0)
                    tweets_data.write(json.dumps(result))
                    return tweet

            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Ese tweet no existe")


import datetime

from ..command_system import Command
from ...models import Client, Schedule, Teacher, Subject
from ...utils import awesome_date

def next_lesson(domain):
   client = Client.query.filter(Client.social_network == 'https://vk.com/'+ domain['domain']).first()
   schedule = Schedule.query.filter((Schedule.client_id == client.id) & (Schedule.time > datetime.datetime.now())).all()

   if client:
      if schedule:
         teacher = Client.query.get_or_404(Teacher.query.get_or_404(schedule[-1].teacher_id).login_id)
         subject = Subject.query.get_or_404(schedule[-1].subject_id)
         message = 'Немного информации о следующем заняии:\n'+'Оно пройдет '+awesome_date(schedule[-1].time)+'\nПреподаватель - '+teacher.first_name+' '+teacher.last_name
      else:
         message = 'Кхм... А следующего занятия нет. Если ты считаешь, что это ошибка, то напиши, пожалуйста, в поддержку.'
   else:
      message = 'Прости, но это доступно только для клиентов. Если ты уже клиент, то пожалуйста зайди на http://edukato.ru/chat-bot и укажи адрес страницы'
   return message, ''

next_lesson_command = Command()

next_lesson_command.keys = ['следующее занятие', 'когда следующее занятие', 'когда следующий урок', 'урок следующий', 'занятие следующее']
next_lesson_command.description = 'Расскажу о следующем занятии *'
next_lesson_command.process = next_lesson


from tkinter import*
import tkinter as tk
import cv2
import numpy as np
import webbrowser

#GRAY ONLY
def obj():
    thres = 0.45
    nms_threshold = 0.2
    cap = cv2.VideoCapture(0)

    className = []
    classFile = 'coco.names'
    with open(classFile, 'rt') as f:
        className = f.read().rstrip('\n').split('\n')

    # print(classNames)
    configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
    weightsPath = 'frozen_inference_graph.pb'

    net = cv2.dnn_DetectionModel(weightsPath, configPath)
    net.setInputSize(320, 320)
    net.setInputScale(1.0 / 127.5)
    net.setInputMean((127.5, 127.5, 127.5))
    net.setInputSwapRB(True)

    while True:
        success, img = cap.read()
        classIds, confs, bbox = net.detect(img, confThreshold=thres)
        bbox = list(bbox)
        confs = list(np.array(confs).reshape(1, -1)[0])
        confs = list(map(float, confs))
        # print(type(confs[0]))
        # print(confs)

        indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)

        for i in indices:
            i = i[0]
            box = bbox[i]
            x, y, w, h = box[0], box[1], box[2], box[3]
            cv2.rectangle(img, (x, y), (x + w, h + y), color=(0, 255, 0), thickness=2)
            cv2.putText(img, className[classIds[i][0] - 1].upper(), (box[0] + 10, box[1] + 30),
                        cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

        cv2.imshow("Display Object Detection                Q for EXIT", img)

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    cv2.destroyAllWindows()

#GRAY ONLY
def GRAY():
    path = cv2.VideoCapture(0)

    while True:
        success, img = path.read()
        imgc = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        cv2.imshow("Display GRAY                Q for EXIT",imgc)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

#HUE ONLY
def HLS():
    path = cv2.VideoCapture(0)

    while True:
        success, img = path.read()
        imgHLS = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)

        cv2.imshow("Display HLS           Q for EXIT",imgHLS)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

#HSV
def HSV():

    path = cv2.VideoCapture(0)

    while True:
        success, img = path.read()
        imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        cv2.imshow("Display HSV                Q for EXIT", imgHSV)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

#VIDEO + REC
def Recandvid():
    def empty(a):
          pass

    def stackImages(scale, imgArray):
        rows = len(imgArray)
        cols = len(imgArray[0])
        rowsAvailable = isinstance(imgArray[0], list)
        width = imgArray[0][0].shape[1]
        height = imgArray[0][0].shape[0]
        if rowsAvailable:
            for x in range(0, rows):
                for y in range(0, cols):
                    if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
                        imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                    else:
                        imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]),
                                                    None, scale, scale)
                    if len(imgArray[x][y].shape) == 2: imgArray[x][y] = cv2.cvtColor(imgArray[x][y], cv2.COLOR_GRAY2BGR)
            imageBlank = np.zeros((height, width, 3), np.uint8)
            hor = [imageBlank] * rows
            hor_con = [imageBlank] * rows
            for x in range(0, rows):
                hor[x] = np.hstack(imgArray[x])
            ver = np.vstack(hor)
        else:
            for x in range(0, rows):
                if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                    imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
                else:
                    imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale,
                                             scale)
                if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
            hor = np.hstack(imgArray)
            ver = hor
        return ver

    path = cv2.VideoCapture(0)
    cv2.namedWindow("TrackBars")
    cv2.resizeWindow("TrackBars", 640, 240)
    cv2.createTrackbar("Hue Min", "TrackBars", 0, 179, empty)
    cv2.createTrackbar("Hue Max", "TrackBars", 19, 179, empty)
    cv2.createTrackbar("Sat Min", "TrackBars", 110, 255, empty)
    cv2.createTrackbar("Sat Max", "TrackBars", 240, 255, empty)
    cv2.createTrackbar("Val Min", "TrackBars", 153, 255, empty)
    cv2.createTrackbar("Val Max", "TrackBars", 255, 255, empty)

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter("Recoded_Vid.avi", fourcc, 20.0, (740, 480))

    while True:
        success, img = path.read()
        imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        h_min = cv2.getTrackbarPos("Hue Min", "TrackBars")
        h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
        s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
        s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
        v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
        v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
        print(h_min, h_max, s_min, s_max, v_min, v_max)
        lower = np.array([h_min, s_min, v_min])
        upper = np.array([h_max, s_max, v_max])
        mask = cv2.inRange(imgHSV, lower, upper)
        imgResult = cv2.bitwise_and(img, img, mask=mask)

        imgStack = stackImages(0.6, ([img, imgHSV], [mask, imgResult]))
        cv2.imshow("All Display and Recording....       Q for EXIT", imgStack)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

def callback(event):
    webbrowser.open_new(event.widget.cget("text"))

root =Tk()
button1= Button(root,text="Video and Record",bg='#47d1d1', command=Recandvid).place(x=40, y=70)
button2= Button(root,text="HSV",bg='#47d1d1',command=HSV).place(x=180, y=70)
button3= Button(root,text="HLS",bg='#47d1d1', command=HLS).place(x=230, y=70)
button4= Button(root,text="GRAY",bg='#47d1d1', command=GRAY).place(x=280, y=70)
button5= Button(root,text="Object Detection",bg='#47d1d1', command=obj).place(x=360, y=70)

lbl = tk.Label(root, text=r"https://github.com/jahin44", fg="blue", cursor="hand2")
lbl.pack()
Label(root, text=" Click here to get more software =>", bg="#eee6ff", fg="black").place(x=0, y=0)

lbl.bind("<Button-1>", callback)
lbl1 = tk.Label(root, text=r"https://jahinhasan.blogspot.com", fg="blue", cursor="hand2")
Label(root, text="     Click here to know about me =>", bg="#eee6ff", fg="black").place(x=0, y=20)

lbl1.pack()
lbl1.bind("<Button-1>", callback)

root.geometry("560x300+120+200")
root.mainloop()


import streamlit as st
import plotting

def stats_view(dfmedicine, dfuniques, visualization_mode):

    st.markdown("## Conteos generales del hospital respecto a los pacientes ingresados por trauma")

    metric_values = zip(dfuniques.iloc[:, 0].values, dfuniques.iloc[:, 1].values)
    metric_columns = st.columns(3)
    metric_count = 0

    for label, value in metric_values:
        metric_columns[metric_count].metric(label, value)
        metric_count += 1

    barchart = plotting.group_comparisons_bar(dfmedicine, "Medicamento o Insumo", "Precio",
    "Comparativa de los precios de los medicamentos", "Costo")
    st.markdown("## Resumen del costo promedio de los medicamentos")

    if visualization_mode == "Gráfica":
        st.markdown("Haga click sobre el gráfico para obtener mayor información acerca del medicamento")
        st.plotly_chart(barchart)
    else:
        st.dataframe(dfmedicine)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 20:16:19 2020

@author: Antonio Hickey
"""
#-------------------------------------------------------------------
# Importing Modules
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import pandas as pd
import csv
#-------------------------------------------------------------------
# Config
url =  'https://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/textview.aspx?data=yield'
uClient = uReq(url)
page_soup = soup(uClient.read(), "html.parser")
uClient.close()
# Defining Elements
Elements = page_soup.findAll("td", {"class" : "text_view_data"})
# Splitting most recent elements
date = Elements[-13].text
mo1 = Elements[-12].text
mo2 = Elements[-11].text
mo3 = Elements[-10].text
mo6 = Elements[-9].text
yr1 = Elements[-8].text
yr2 = Elements[-7].text
yr3 = Elements[-6].text
yr5 = Elements[-5].text
yr7 = Elements[-4].text
yr10 = Elements[-3].text
yr20 = Elements[-2].text
yr30 = Elements[-1].text

# Defining New Data
data = (date, mo1, mo2, mo3, mo6, yr1, yr2, yr3, yr5, yr7, yr10, yr20, yr30)

# Appending Dataset with New Elements
with open(r'Yeild Curve.csv', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(data)


from PyQt5.QtWidgets import (QWidget, QCalendarWidget,QLabel, QApplication, QVBoxLayout,QPushButton)
from PyQt5.QtCore import QDate
import sys
global ap
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        vbox = QVBoxLayout(self)
        ap=''
        cal = QCalendarWidget(self)
        cal.setGridVisible(True)
        cal.clicked[QDate].connect(self.showDate)
        vbox.addWidget(cal)
        self.lbl = QLabel(self)
        date = cal.selectedDate()
        self.lbl.setText(date.toString())
        self.ok = QPushButton(self)
        self.ok.setText("OK")
        self.ok.clicked.connect(self.okay)
        vbox.addWidget(self.lbl)
        vbox.addWidget(self.ok)
        self.setLayout(vbox)
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Calendar')
        self.show()
    def showDate(self, date):
        self.lbl.setText(date.toString())
        #nonlocal ap
        ap= date.toString()
        file = open("from.txt", mode='w')
        file.write('%s' %ap)
        file.close()
        print(ap)
    def okay(self):
        self.close()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    #doot = QWidget()
    ex = Example()
    #ex.initUI(doot)
    #ex.show()
    sys.exit(app.exec_())


from typing import List

class Solution:
    def _fizzBuzz(self, number):
        if number % 15 == 0:
            return "FizzBuzz"
        elif number % 5 == 0:
            return "Buzz"
        elif number % 3 == 0:
            return "Fizz"
        else:
            return str(number)

    def fizzBuzz(self, n: int) -> List[str]:
        return [self._fizzBuzz(i) for i in range(1, n + 1)]


# First things, first. Import the wxPython package.
from concurrent.futures import thread
from tracemalloc import start
from turtle import pos
from numpy import size, true_divide
import wx
from wx.adv import *
from Utilities.media_utils import batchDownload, download, spotifyToSearches, ytFromLink
import misc
from spotify_dl import spotify
from spotipy.oauth2 import SpotifyClientCredentials
import threading
from multiprocessing import Process
import ctypes
import time

#Account for high DPI screens on windows
try:
    ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
    pass

# Next, create an application object.
app = wx.App()

class MainFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize,
            style=wx.DEFAULT_FRAME_STYLE^wx.RESIZE_BORDER^wx.MAXIMIZE_BOX)

def getOption():
    if ytl.Value:
        return "link"
    elif spp.Value:
        return "playlist"
    elif spa.Value:
        return "album"
    elif spt.Value:
        return "track"
    else:
        return -1

frm = MainFrame(None,"PyraTunes",(680,230))
frm.SetIcon(wx.Icon("misc/FrameIcon.png"))

#Set up panel
pan = wx.Panel(frm)

radiolabel = wx.StaticText(pan,id=1, label="Choose a download option:", pos=(132,75))
f1label = wx.StaticText(pan, label="Enter URL:", pos=(21,15))
f12label = wx.StaticText(pan, label="Enter Target Folder Name:", pos=(351,15))

ytl = wx.RadioButton(pan, label="Youtube Link", style=0, pos=(132,100))
spp = wx.RadioButton(pan, label="Spotify Playlist", style=0, pos=(285,100))
spa = wx.RadioButton(pan, label="Spotify Album", style=0, pos=(132,125))
spt = wx.RadioButton(pan, label="Spotify Track", style=0, pos=(285,125))

url_field = wx.TextCtrl(pan, pos=(20,40), size=(320,30), name="Enter URL")
target_field = wx.TextCtrl(pan, pos=(390,40), size=(250,30), name="Enter directory")
target_field.SetHint("Downloads")

dl_button = wx.Button(pan, label="Download", pos=(20,80), size=(102,55))

folder_img = wx.Image("misc/folder.png")
folder_img = folder_img.Scale(26,26, wx.IMAGE_QUALITY_HIGH)
dir_button = wx.BitmapButton(pan, bitmap=folder_img.ConvertToBitmap(), pos=(350,40))

loading_anim = wx.ActivityIndicator(parent=pan, size=(50 , 50), pos=(550 , 90))
loading_anim.Hide()

#DL Button Background Function
def dlfunc():
    dp = target_field.Value

    if url_field.Value == "":
        loading_anim.Stop()
        loading_anim.Hide()
        return

    if dp == "":
        dp = "Downloads"

    if getOption() == "link":
        download(ytFromLink(url_field.Value), dp)
    else:
        batchDownload(spotifyToSearches(url_field.Value, getOption()),dp)

    loading_anim.Stop()
    loading_anim.Hide()

#Define directory button functionality
def onClickDirectory(event):
    dlg = wx.DirDialog(None, "Select folder to download in", "Downloads", wx.DD_DEFAULT_STYLE)
    if dlg.ShowModal() == wx.ID_OK:
        target_field.SetValue(dlg.GetPath())
    dlg.Destroy()

#Define Download Button functionality
def onClickDownload(event):
    loading_anim.Show()
    loading_anim.Start()
    thread_downloader = threading.Thread(target=dlfunc)
    thread_downloader.start()

dl_button.Bind(wx.EVT_BUTTON, onClickDownload)
dir_button.Bind(wx.EVT_BUTTON, onClickDirectory)

# Show frame.
frm.Show()

#Gui Thread Start
app.MainLoop()


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

"""
ReferIt, UNC, UNC+ and GRef referring image segmentation PyTorch dataset.

Define and group batches of images, segmentations and queries.
Based on:
https://github.com/chenxi116/TF-phrasecut-public/blob/master/build_batches.py
"""

import os
import re
# import cv2
import sys
import json
import torch
import numpy as np
import os.path as osp
import scipy.io as sio
import torch.utils.data as data
sys.path.append('.')

from PIL import Image
from pytorch_pretrained_bert.tokenization import BertTokenizer
from utils.word_utils import Corpus

def read_examples(input_line, unique_id):
    """Read a list of `InputExample`s from an input file."""
    examples = []
    # unique_id = 0
    line = input_line #reader.readline()
    # if not line:
    #     break
    line = line.strip()
    text_a = None
    text_b = None
    m = re.match(r"^(.*) \|\|\| (.*)$", line)
    if m is None:
        text_a = line
    else:
        text_a = m.group(1)
        text_b = m.group(2)
    examples.append(
        InputExample(unique_id=unique_id, text_a=text_a, text_b=text_b))
    # unique_id += 1
    return examples

## Bert text encoding
class InputExample(object):
    def __init__(self, unique_id, text_a, text_b):
        self.unique_id = unique_id
        self.text_a = text_a
        self.text_b = text_b

class InputFeatures(object):
    """A single set of features of data."""
    def __init__(self, unique_id, tokens, input_ids, input_mask, input_type_ids):
        self.unique_id = unique_id
        self.tokens = tokens
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.input_type_ids = input_type_ids

def convert_examples_to_features(examples, seq_length, tokenizer):
    """Loads a data file into a list of `InputBatch`s."""
    features = []
    for (ex_index, example) in enumerate(examples):
        tokens_a = tokenizer.tokenize(example.text_a)

        tokens_b = None
        if example.text_b:
            tokens_b = tokenizer.tokenize(example.text_b)

        if tokens_b:
            # Modifies `tokens_a` and `tokens_b` in place so that the total
            # length is less than the specified length.
            # Account for [CLS], [SEP], [SEP] with "- 3"
            _truncate_seq_pair(tokens_a, tokens_b, seq_length - 3)
        else:
            # Account for [CLS] and [SEP] with "- 2"
            if len(tokens_a) > seq_length - 2:
                tokens_a = tokens_a[0:(seq_length - 2)]
        tokens = []
        input_type_ids = []
        tokens.append("[CLS]")
        input_type_ids.append(0)
        for token in tokens_a:
            tokens.append(token)
            input_type_ids.append(0)
        tokens.append("[SEP]")
        input_type_ids.append(0)

        if tokens_b:
            for token in tokens_b:
                tokens.append(token)
                input_type_ids.append(1)
            tokens.append("[SEP]")
            input_type_ids.append(1)

        input_ids = tokenizer.convert_tokens_to_ids(tokens)

        # The mask has 1 for real tokens and 0 for padding tokens. Only real
        # tokens are attended to.
        input_mask = [1] * len(input_ids)

        # Zero-pad up to the sequence length.
        while len(input_ids) < seq_length:
            input_ids.append(0)
            input_mask.append(0)
            input_type_ids.append(0)

        assert len(input_ids) == seq_length
        assert len(input_mask) == seq_length
        assert len(input_type_ids) == seq_length
        features.append(
            InputFeatures(
                unique_id=example.unique_id,
                tokens=tokens,
                input_ids=input_ids,
                input_mask=input_mask,
                input_type_ids=input_type_ids))
    return features

class DatasetNotFoundError(Exception):
    pass

class TransVGDataset(data.Dataset):
    SUPPORTED_DATASETS = {
        'referit': {'splits': ('train', 'val', 'trainval', 'test')},
        'unc': {
            'splits': ('train', 'val', 'trainval', 'testA', 'testB'),
            'params': {'dataset': 'refcoco', 'split_by': 'unc'}
        },
        'unc+': {
            'splits': ('train', 'val', 'trainval', 'testA', 'testB'),
            'params': {'dataset': 'refcoco+', 'split_by': 'unc'}
        },
        'gref': {
            'splits': ('train', 'val'),
            'params': {'dataset': 'refcocog', 'split_by': 'google'}
        },
        'gref_umd': {
            'splits': ('train', 'val', 'test'),
            'params': {'dataset': 'refcocog', 'split_by': 'umd'}
        },
        'flickr': {
            'splits': ('train', 'val', 'test')}
    }

    def __init__(self, data_root, split_root='data', dataset='referit',
                 transform=None, return_idx=False, testmode=False,
                 split='train', max_query_len=128, lstm=False,
                 bert_model='bert-base-uncased'):
        self.images = []
        self.data_root = data_root
        self.split_root = split_root
        self.dataset = dataset
        self.query_len = max_query_len
        self.lstm = lstm
        self.transform = transform
        self.testmode = testmode
        self.split = split
        self.tokenizer = BertTokenizer.from_pretrained(bert_model, do_lower_case=True)
        self.return_idx=return_idx

        assert self.transform is not None

        if split == 'train':
            self.augment = True
        else:
            self.augment = False

        if self.dataset == 'referit':
            self.dataset_root = osp.join(self.data_root, 'referit')
            self.im_dir = osp.join(self.dataset_root, 'images')
            self.split_dir = osp.join(self.dataset_root, 'splits')
        elif  self.dataset == 'flickr':
            self.dataset_root = osp.join(self.data_root, 'Flickr30k')
            self.im_dir = osp.join(self.dataset_root, 'flickr30k_images')
        else:   ## refcoco, etc.
            self.dataset_root = osp.join(self.data_root, 'other')
            self.im_dir = osp.join(
                self.dataset_root, 'images', 'mscoco', 'images', 'train2014')
            self.split_dir = osp.join(self.dataset_root, 'splits')

        if not self.exists_dataset():
            # self.process_dataset()
            print('Please download index cache to data folder: \n \
                https://drive.google.com/open?id=1cZI562MABLtAzM6YU4WmKPFFguuVr0lZ')
            exit(0)

        dataset_path = osp.join(self.split_root, self.dataset)
        valid_splits = self.SUPPORTED_DATASETS[self.dataset]['splits']

        if self.lstm:
            self.corpus = Corpus()
            corpus_path = osp.join(dataset_path, 'corpus.pth')
            self.corpus = torch.load(corpus_path)

        if split not in valid_splits:
            raise ValueError(
                'Dataset {0} does not have split {1}'.format(
                    self.dataset, split))

        splits = [split]
        if self.dataset != 'referit':
            splits = ['train', 'val'] if split == 'trainval' else [split]
        for split in splits:
            imgset_file = '{0}_{1}.pth'.format(self.dataset, split)
            imgset_path = osp.join(dataset_path, imgset_file)
            self.images += torch.load(imgset_path)

    def exists_dataset(self):
        return osp.exists(osp.join(self.split_root, self.dataset))

    def pull_item(self, idx):
        if self.dataset == 'flickr':
            img_file, bbox, phrase = self.images[idx]
        else:
            img_file, _, bbox, phrase, attri = self.images[idx]
        ## box format: to x1y1x2y2
        if not (self.dataset == 'referit' or self.dataset == 'flickr'):
            bbox = np.array(bbox, dtype=int)
            bbox[2], bbox[3] = bbox[0]+bbox[2], bbox[1]+bbox[3]
        else:
            bbox = np.array(bbox, dtype=int)

        img_path = osp.join(self.im_dir, img_file)
        img = Image.open(img_path).convert("RGB")
        # img = cv2.imread(img_path)
        # ## duplicate channel if gray image
        # if img.shape[-1] > 1:
        #     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        # else:
        #     img = np.stack([img] * 3)

        bbox = torch.tensor(bbox)
        bbox = bbox.float()
        return img, phrase, bbox

    def tokenize_phrase(self, phrase):
        return self.corpus.tokenize(phrase, self.query_len)

    def untokenize_word_vector(self, words):
        return self.corpus.dictionary[words]

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        img, phrase, bbox = self.pull_item(idx)
        # phrase = phrase.decode("utf-8").encode().lower()
        phrase = phrase.lower()
        input_dict = {'img': img, 'box': bbox, 'text': phrase}
        input_dict = self.transform(input_dict)
        img = input_dict['img']
        bbox = input_dict['box']
        phrase = input_dict['text']
        img_mask = input_dict['mask']

        if self.lstm:
            phrase = self.tokenize_phrase(phrase)
            word_id = phrase
            word_mask = np.array(word_id>0, dtype=int)
        else:
            ## encode phrase to bert input
            examples = read_examples(phrase, idx)
            features = convert_examples_to_features(
                examples=examples, seq_length=self.query_len, tokenizer=self.tokenizer)
            word_id = features[0].input_ids
            word_mask = features[0].input_mask

        if self.testmode:
            return img, np.array(word_id, dtype=int), np.array(word_mask, dtype=int), \
                np.array(bbox, dtype=np.float32), np.array(ratio, dtype=np.float32), \
                np.array(dw, dtype=np.float32), np.array(dh, dtype=np.float32), self.images[idx][0]
        else:
            # print(img.shape)
            return img, np.array(img_mask), np.array(word_id, dtype=int), np.array(word_mask, dtype=int), np.array(bbox, dtype=np.float32)


import pytest
import time
import json
import logging

from error_code.error_status import SignatureStatus
from automation_framework.utilities.workflow import submit_request
from automation_framework.work_order_get_result.work_order_get_result_params \
    import WorkOrderGetResult
import avalon_client_sdk.worker.worker_details as worker
from automation_framework.utilities.request_args import GetResultWaitTime

logger = logging.getLogger(__name__)

def submit_work_order_get_result(input_request, request_mode, tamper,
                                 output_json_file_name, uri_client,
                                 err_cd, work_order_id,
                                 request_id):
    """ Function to submit work order get result request.
    Uses WorkOrderGetResult class to initialize request object.
    Return err_cd, response."""

    logger.info("------ Testing WorkOrderGetResult ------")

    submiting_time = ""

    if err_cd == 0:
        if request_mode == "object":
            # submit work order get result and retrieve response
            logger.info("----- Constructing WorkOrderGetResult -----")
            request_obj = WorkOrderGetResult()
            request_obj.set_work_order_id(work_order_id)
            request_obj.set_request_id(request_id)
            input_get_result = json.loads(request_obj.to_string())
        else:
            request_obj = WorkOrderGetResult()
            request_obj.set_work_order_id(work_order_id)
            request_obj.set_request_id(request_id)
            input_get_result = json.loads(request_obj.to_string())

        logger.info("----- Validating WorkOrderGetResult Response ------")
        response = {}

        response_timeout_start = time.time()
        response_timeout_multiplier = ((6000 / 3600) + 6) * 3
        while "result" not in response:
            if "error" in response:
                if response["error"]["code"] != 5:
                    logger.info('WorkOrderGetResult - '
                                'Response received with error code. ')
                    err_cd = 1
                    break

            response_timeout_end = time.time()
            if ((response_timeout_end - response_timeout_start) >
                    response_timeout_multiplier):
                logger.info('ERROR: WorkOrderGetResult response is not \
                           received within expected time.')
                break

            # submit work order get result request and retrieve response
            response = submit_request(uri_client, input_get_result,
                                      output_json_file_name)
            time.sleep(GetResultWaitTime.LOOP_WAIT_TIME.value)

    else:
        logger.info('ERROR: WorkOrderGetResult not performed - \
                    Expected response not received for \
                    WorkOrderSubmit.')

    response_tup = (err_cd, response, submiting_time)
    return response_tup


from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import *
import logging
import pymongo

import emission.storage.timeseries.timequery as estt

import emission.core.wrapper.entry as ecwe
import emission.storage.timeseries.abstract_timeseries as esta

RAW_TRIP_KEY = "segmentation/raw_trip"
RAW_PLACE_KEY = "segmentation/raw_place"
RAW_SECTION_KEY = "segmentation/raw_section"
RAW_STOP_KEY = "segmentation/raw_stop"
RAW_UNTRACKED_KEY = "segmentation/raw_untracked"
CLEANED_TRIP_KEY = "analysis/cleaned_trip"
CLEANED_PLACE_KEY = "analysis/cleaned_place"
CLEANED_SECTION_KEY = "analysis/cleaned_section"
INFERRED_SECTION_KEY = "analysis/inferred_section"
CLEANED_STOP_KEY = "analysis/cleaned_stop"
CLEANED_UNTRACKED_KEY = "analysis/cleaned_untracked"
CLEANED_LOCATION_KEY = "analysis/recreated_location"
INFERRED_TRIP_KEY = "analysis/inferred_trip"
EXPECTED_TRIP_KEY = "analysis/expected_trip"
CONFIRMED_TRIP_KEY = "analysis/confirmed_trip"
CONFIRMED_PLACE_KEY = "analysis/confirmed_place"
CONFIRMED_UNTRACKED_KEY = "analysis/confirmed_untracked"
COMPOSITE_TRIP_KEY = "analysis/composite_trip"
METRICS_DAILY_USER_COUNT = "metrics/daily_user_count"
METRICS_DAILY_MEAN_COUNT = "metrics/daily_mean_count"
METRICS_DAILY_USER_DISTANCE = "metrics/daily_user_distance"
METRICS_DAILY_MEAN_DISTANCE = "metrics/daily_mean_distance"
METRICS_DAILY_USER_DURATION = "metrics/daily_user_duration"
METRICS_DAILY_MEAN_DURATION = "metrics/daily_mean_duration"
METRICS_DAILY_USER_MEDIAN_SPEED = "metrics/daily_user_median_speed"
METRICS_DAILY_MEAN_MEDIAN_SPEED = "metrics/daily_mean_median_speed"
INFERRED_LABELS_KEY = "inference/labels"
TRIP_MODEL_STORE_KEY = "inference/trip_model"

# General methods

def get_object(key, object_id):
    return get_entry(key, object_id).data

def get_entry(key, object_id):
    return esta.TimeSeries.get_aggregate_time_series().get_entry_from_id(
        key, object_id)

def get_objects(key, user_id, time_query, geo_query=None):
    return [entry.data for entry in
            get_entries(key, user_id=user_id, time_query=time_query,
                        geo_query=geo_query)]

def get_entries(key, user_id, time_query, untracked_key = None,
                geo_query=None,
                extra_query_list=None):
    ts = get_timeseries_for_user(user_id)
    key_list = [key] if untracked_key is None else [key, untracked_key]
    if untracked_key is not None:
        logging.debug("after appending untracked_key %s, key_list is %s" %
                      (untracked_key, key_list))
    doc_cursor = ts.find_entries(key_list, time_query, geo_query, extra_query_list)
    # TODO: Fix "TripIterator" and return it instead of this list
    curr_entry_list = [ecwe.Entry(doc) for doc in doc_cursor]
    logging.debug("Returning entry with length %d result" % len(curr_entry_list))
    return curr_entry_list

def get_data_df(key, user_id, time_query, geo_query=None,
                extra_query_list=None):
    ts = get_timeseries_for_user(user_id)
    data_df = ts.get_data_df(key, time_query,
                     geo_query, extra_query_list)
    logging.debug("Returning entry with length %d result" % len(data_df))
    return data_df

def get_timeseries_for_user(user_id):
    if user_id is not None:
        ts = esta.TimeSeries.get_time_series(user_id)
    else:
        ts = esta.TimeSeries.get_aggregate_time_series()
    logging.debug("for user %s, returning timeseries %s" % (user_id, ts))
    return ts

# Object-specific associations

def get_time_query_for_trip_like(key, trip_like_id):
    """
    Returns the query that returns all the points associated with this
    trip-like (examples of trip-like objects are: raw trip, cleaned trip, raw section)
    """
    trip = get_object(key, trip_like_id)
    return get_time_query_for_trip_like_object(trip)

def get_time_query_for_trip_like_object(trip_like):
    """
    Returns the query that returns all the points associated with this
    trip-like (raw trip, cleaned trip, raw section)
    """
    return estt.TimeQuery("data.ts", trip_like.start_ts, trip_like.end_ts)


import torch, torchvision
import os

class CNN(torch.nn.Module):

    def __init__(self, network_type, dataset_name):

        super(CNN, self).__init__()
        self.network_type = network_type

        if dataset_name == 'dogscats':
            classes = 2
        elif dataset_name == 'imagenet':
            classes = 6

        if self.network_type == 'images_resnet18':
            self.model = torchvision.models.resnet18()
            self.model.fc = torch.nn.Linear(512,classes)
        elif self.network_type == 'images_mobilenetv2' :
            self.model = torchvision.models.mobilenet_v2()
            self.model.classifier = torch.nn.Linear(1280,classes)
        elif self.network_type == 'images_shufflenetv2':
            self.model = torchvision.models.shufflenet_v2_x0_5(pretrained=True)
            self.model.fc = torch.nn.Linear(1024,classes)

    def forward(self, x):

        return self.model(x)


from enum import Enum
import numpy as np
import pandas as pd
from dataclasses import dataclass
from copy import deepcopy
from typing import List, Union

class Direction(Enum):
    UP = 'U'
    DOWN = 'D'
    LEFT = 'L'
    RIGHT = 'R'

@dataclass
class Motion:
    direction: Union[Direction, str]
    steps: int

    def __post_init__(self):
        if isinstance(self.direction, str):
            self.direction = Direction(self.direction)

@dataclass
class Position:
    x: int = 0
    y: int = 0

class Iteration:
    def __init__(self, knots_number: int):
        self.knots_number = knots_number
        self.knots: List[Position] = [Position() for _ in range(knots_number)]

    @property
    def H(self) -> Position:
        return self.knots[0]

    @property
    def T(self) -> Position:
        return self.knots[-1]

class Simulation:
    def __init__(self, knots_number: int):
        self.knots_number = knots_number
        self.iterations: List[Iteration] = [Iteration(knots_number)]

    @property
    def last_iteration(self) -> Iteration:
        return self.iterations[-1]

    def execute_motion(self, motion: Motion) -> None:
        for step in range(motion.steps):
            new_iter = deepcopy(self.last_iteration)
            Moves.head_move(new_iter.knots[0], motion.direction)
            for i in range(1, self.knots_number):
                Moves.tail_move(new_iter.knots[i - 1], new_iter.knots[i])
            self.iterations.append(new_iter)

    def execute_motions(self, motions: List[Motion]) -> None:
        for motion in motions:
            self.execute_motion(motion)

    @property
    def tail_positions(self) -> pd.DataFrame:
        df = pd.DataFrame([(i.T.x, i.T.y) for i in self.iterations])
        df.columns = ('x', 'y')
        return df

class Moves:

    @staticmethod
    def head_move(H: Position, direction: Direction) -> None:
        if direction == Direction.UP:
            H.y += 1
        elif direction == Direction.DOWN:
            H.y -= 1
        elif direction == Direction.LEFT:
            H.x -= 1
        elif direction == Direction.RIGHT:
            H.x += 1

    @staticmethod
    def tail_move(H: Position, T: Position) -> None:
        x_diff = T.x - H.x
        y_diff = T.y - H.y

        if x_diff > 1:
            if y_diff > 1:
                T.x = H.x + 1
                T.y = H.y + 1
            elif y_diff < -1:
                T.x = H.x + 1
                T.y = H.y - 1
            else:
                T.x = H.x + 1
                T.y = H.y
        elif x_diff < -1:
            if y_diff > 1:
                T.x = H.x - 1
                T.y = H.y + 1
            elif y_diff < -1:
                T.x = H.x - 1
                T.y = H.y - 1
            else:
                T.x = H.x - 1
                T.y = H.y
        else:
            if y_diff > 1:
                T.x = H.x
                T.y = H.y + 1
            elif y_diff < -1:
                T.x = H.x
                T.y = H.y - 1
            else:
                pass

def read_input_row(row: str) -> Motion:
    direction, steps = row.split(' ')
    return Motion(direction, int(steps))

def main() -> None:
    with open('input.txt', 'r') as f:
        input_data = f.read().strip().split('\n')
    motions: List[Motion] = [read_input_row(row) for row in input_data]

    def part_one() -> int:
        simulation = Simulation(knots_number=2)
        simulation.execute_motions(motions)
        return len(simulation.tail_positions.drop_duplicates())

    def part_two() -> int:
        simulation = Simulation(knots_number=10)
        simulation.execute_motions(motions)
        return len(simulation.tail_positions.drop_duplicates())

    print(f'Part one: {part_one()}')
    print(f'Part two: {part_two()}')

if __name__ == '__main__':
    main()


import argparse

def create_parser():
	parser = argparse.ArgumentParser(description='HR invetory software')
	parser.add_argument('path', help='Path to file to be exported')
	parser.add_argument('--export', action='store_true', help='Export current settings to json file')
	return parser

def main():
	from hr import users, inventory

	args = create_parser().parse_args()

	if args.export:
		inventory.export_users(args.path)
	else:
		user_list = inventory.read_file(args.path)
		users.match_users(user_list)


"""

A Jarvis plugin for listening music according
to your mood through Spotify's Web Player!
Jarvis asks for your mood and based on your choice it
opens a specific playlist of Spotify that fits
your mood.

"""
import webbrowser
from plugin import plugin
from plugin import require
from colorama import Fore

@require(network=True)
@plugin("mood music")
def open_spotify(jarvis, s):
    jarvis.say("\nHello! What's your mood for today? \n")
    # list that stores the available mood choices
    list_of_moods = ["1.Feel Good Morning",
                     "2.So excited just can't hide it!",
                     "3.Party time",
                     "4.Workout beats",
                     "5.Me, myself and I",
                     "6.Chilled out",
                     "7.Roadtrip",
                     "8.Sunset to Sunrise",
                     "9.Jazz Chills",
                     "10.Back to 90s",
                     "11.Stress Relief",
                     "12.Not my Day,my Week,my Month or even my Year",
                     ]
    # loops for printing the available moods
    for i in range(0, 3):
        jarvis.say(list_of_moods[i], Fore.LIGHTCYAN_EX)
    for i in range(3, 6):
        jarvis.say(list_of_moods[i], Fore.CYAN)
    for i in range(6, 9):
        jarvis.say(list_of_moods[i], Fore.LIGHTMAGENTA_EX)
    for i in range(9, 12):
        jarvis.say(list_of_moods[i], Fore.MAGENTA)
    print()
    stop = False
    while not stop:
        # variable for validating the input mood
        # initialize it as True
        invalid_mood = True
        # loop for validating the input mood
        # input must be integer
        # and in the range of 1-14
        while invalid_mood:
            mood = jarvis.input("Choose your mood (1-12): ")
            print()
            try:
                int_mood = int(mood)
                if int_mood < 1 or int_mood > 12:
                    print("Sorry invalid input was given! "
                          "Please enter a valid one!(1-12)")
                else:
                    invalid_mood = False
            except ValueError:
                invalid_mood = True
                print("Sorry invalid input was given! "
                      "Please enter a valid one!(1-12)")

        # variable containing the main url
        # of a Spotify's playlist
        url = "https://open.spotify.com/playlist/"
        # dictionary for storing the urls based on every mood
        # url is defined by the main url
        # plus the unique ending of each url
        url_dict = {"1": url + "3IBrsav3Sh8AImtaGoaP07",
                    "2": url + "37i9dQZF1DWSf2RDTDayIx",
                    "3": url + "4MKC0zUOwvz5gGfKX93LV1",
                    "4": url + "190wZ2oVo7MTrBvNlPiub2",
                    "5": url + "37i9dQZF1DWZLcGGC0HJbc",
                    "6": url + "37i9dQZF1DX889U0CL85jj",
                    "7": url + "0l0a4uSRYz0VWnt38VAEzR",
                    "8": url + "0Au3b2NB5uz8Iwwx5sl6K5",
                    "9": url + "37i9dQZF1DX0SM0LYsmbMT",
                    "10": url + "37i9dQZF1DXbTxeAdrVG2l",
                    "11": url + "37i9dQZF1DWXe9gFZP0gtP",
                    "12": url + "3c0Nv5CY6TIaRszlTZbUFk",
                    }
        # opens the Spotify Web Player
        webbrowser.open(url_dict[mood])
        jarvis.say("Changed your mood?")
        answer = jarvis.input("Type anything to continue or No to exit: ")
        print()
        if answer.upper() == "NO":
            stop = True


from setuptools import setup, find_packages

with open('readme.md', encoding='utf-8') as f:
    long_description = f.read()

setup(
    packages = find_packages(),
    name = 'pbat',
    version = '0.0.16',
    author = "Stanislav Doronin",
    author_email = "mugisbrows@gmail.com",
    url = 'https://github.com/mugiseyebrows/pbat',
    description = 'Batch file preprocessor',
    long_description = long_description,
    long_description_content_type = 'text/markdown',
    install_requires = ['lark'],
    package_data = {
        'pbat': ['examples/*.pbat', 'examples/*.bat', '*.lark']
    },
    entry_points = {
        'console_scripts': [
            'pbat = pbat.compile:main'
        ]
    }
)


from django.db import models
from django.utils import timezone

from . import Season

class SeasonPlayerManager(models.Manager):

    def update_active(self, player, elo, wins, losses):
        """Update or create the season player instance for the active season."""
        active_season = Season.objects.get_active()

        updated_values = {
            'player': player,
            'season': active_season,
            'elo_score': elo,
            'win_count': wins,
            'loss_count': losses
        }
        SeasonPlayer.objects.update_or_create(player=player, season=active_season, defaults=updated_values)

    def get_winner(self, season):
        """Returns the player with the most elo points for the given season."""
        return (
            self.get_queryset()
                .filter(season=season)
                .order_by('-elo_score')
                .first()
        )

class SeasonPlayer(models.Model):
    """Denormalized representation of a player for a particular season."""

    # FK relationships to other models
    player = models.ForeignKey('core.Player')
    season = models.ForeignKey('core.Season')

    # denormalized counts and elo score
    elo_score = models.IntegerField(blank=True)
    win_count = models.IntegerField(blank=True)
    loss_count = models.IntegerField(blank=True)

    objects = SeasonPlayerManager()

    def __unicode__(self):
        return "{player} during {season} season".format(
            player=self.player,
            season=self.season
        )

    def undo_win(self, elo_points, commit=True):
        self.elo_score -= elo_points
        self.win_count -= 1
        if commit:
            self.save()

    def undo_loss(self, elo_points, commit=True):
        self.elo_score += elo_points
        self.loss_count -= 1
        if commit:
            self.save()


#coding=utf-8
from confluent_kafka import Producer
import MySQLdb
import json
import time
import random

p = Producer({"bootstrap.servers": "118.24.53.99:9092"})

db = MySQLdb.connect("localhost", "root", "123456", "test_kafka", charset='utf8' )
cursor = db.cursor()

sql = "SELECT msg_body FROM order_kafka_msg;"

def delivery_report(err, msg):
    """ Called once for each message produced to indicate delivery result.
        Triggered by poll() or flush(). """
    if err is not None:
        print('Message delivery failed: {}'.format(err))
    else:
        print('Message delivered to {} [{}]'.format(msg.topic(), msg.partition()))

data_list=[]

# while (True):
cursor.execute(sql.encode("utf-8"))
results = cursor.fetchall()
for result in results:
        print("------------------")
        setJson = json.dumps(json.loads(result[0]),ensure_ascii=False)
        print(setJson.encode("utf-8"))
        p.produce("test",setJson.encode("utf-8"),callback=delivery_report)

p.flush()
    # time.sleep(60)


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy
from scipy.misc import derivative

def fn_plot1d(fn, x_min, x_max, filename):
    num_values = 1000
    x = np.linspace(x_min, x_max, num_values)
    fnv = np.vectorize(fn)
    y = fnv(x)
    Xlabel = "X axis"
    Ylabel = fn.__name__ + "(x)"
    Title = "1D plot of " + Ylabel
    plt.plot(x, y)
    plt.xlabel(Xlabel)
    plt.ylabel(Ylabel)
    plt.title(Title)
    #plt.show()
    plt.savefig(filename)
    plt.clf()

def fn_plot2d(fn, x_min, x_max, y_min, y_max, filename):
    num_x = 1000
    num_y = 1000
    x = np.linspace(x_min, x_max, num_x)
    y = np.linspace(y_min, y_max, num_y)
    X,Y = np.meshgrid(x,y)
    fnv = np.vectorize(fn)
    Z = fnv(X,Y)
    fig = plt.figure()
    ax = fig.gca(projection = '3d')
    ax.plot_surface(X,Y,Z)
    Xlabel = "X axis"
    Ylabel = "Y axis"
    Zlabel = fn.__name__ + "(x,y)"
    Title = "2D plot of " + Zlabel
    ax.set_xlabel(Xlabel)
    ax.set_ylabel(Ylabel)
    ax.set_zlabel(Zlabel)
    ax.set_title(Title)
    #plt.show()
    ax.figure.savefig(filename)
    #plt.gca(projection = 'rectilinear')
    plt.clf()

def nth_derivative_plotter(fn, n, x_min, x_max, filename):
    num_values = 1000
    x = np.linspace(x_min, x_max, num_values)
    fnv = np.vectorize(fn)
    y = derivative(fnv,x,dx = 1e-3, n = n)
    strn = str(n)
    Xlabel = "X axis"
    Ylabel = fn.__name__ + fr'$^{{({strn})}}$' + '(x)'
    Title = "nth derivative plotter (n = " + strn + ")"
    plt.plot(x,y)
    plt.xlabel(Xlabel)
    plt.ylabel(Ylabel)
    plt.title(Title)
    plt.savefig(filename)
    plt.clf()

def h(x):
    if x > 0:
        return np.exp(-1/(x*x))
    else:
        return 0

def g(x):
    return (h(2-x)/(h(2-x)+h(x-1)))

def b(x):
    if x < 0:
        x = -x
    return g(x)

def sinc(x,y):
    if (x == 0 and y == 0):
        return 1
    else:
        norm = (x**2 + y**2) ** (0.5)
        return (np.sin(norm)/norm)

if __name__ == '__main__':
    fn_plot1d(b,-2,2,'fn1plot.png')
    fn_plot2d(sinc, -1.5*np.pi, 1.5*np.pi, -1.5*np.pi, 1.5*np.pi, 'fn2plot.png')
    nth_derivative_plotter(b, 1, -2, 2, 'bd_1.png')
    nth_derivative_plotter(b, 2, -2, 2, 'bd_2.png')


import torch
import torch.nn as nn

class NegativeLabelLoss(nn.Module):
    """
    https://www.desmos.com/calculator/9oaqcjayrw
    """
    def __init__(self, ignore_index=-100, reduction='mean',alpha=1.0,beta=0.8):
        super(NegativeLabelLoss, self).__init__()
        self.softmax = nn.Softmax(dim=1)
        self.alpha = alpha
        self.beta = beta
        self.nll = nn.NLLLoss(ignore_index=ignore_index, reduction=reduction)

    def forward(self, logits, target):
        nsoftmax = self.softmax(logits)
        nsoftmax = torch.where(
                nsoftmax<=torch.tensor([self.beta],dtype=logits.dtype).to(logits.device),
                torch.tensor([0.0],requires_grad=True,dtype=logits.dtype).to(logits.device),
                nsoftmax
            )
        nsoftmax = torch.clamp((1.0 - nsoftmax), min=1e-32)
        return self.nll(torch.log(nsoftmax) * self.alpha, target)