from bs4 import BeautifulSoup as bs
from requests_html import HTMLSession
from youtube_transcript_api import YouTubeTranscriptApi

import funcs
import os

"""

this code produces Italian, English and Italian+English transcripts
of Italian language YouTube videos directly from their URLs

some YouTube transcripts are auto-generated

these auto-generated transcripts consist of a single string of text
without any sentence delimiters or other punctuation

this identifies an immediate application of natural language processing (NLP):
sentence tokenization of error-ridden auto-generated YouTube transcripts

the above feature allows for a more complete automation of transcripts

"""

"""

example videos #1-5 are from the Nova Lectio channel:
https://www.youtube.com/channel/UCRCWJCFoZUvkkWzIqzfBy6g

example video #1: com'è crollata la nazione più ricca del sud america
video_url = 'https://www.youtube.com/watch?v=ouIMOJsCADY'
video_id = video_url.split("v=")[-1]

example video #2: è davvero la colpa del colonialismo se l'africa è un fallimento
video_url = 'https://www.youtube.com/watch?v=xAJEyMWVVnw'
video_id = video_url.split("v=")[-1]

example video #3: come la cina è diventata una potenza mondiale
video_url = 'https://www.youtube.com/watch?v=n5vIhOA60TM'
video_id = video_url.split("v=")[-1]

example video #4: il business infernale delle miniere di coltan in congo
video_url = 'https://www.youtube.com/watch?v=x-6f2OdXakI'
video_id = video_url.split("v=")[-1]

example video #5: come scappare il paese più blindato nel mondo
(structured example)
video_url = 'https://www.youtube.com/watch?v=V_QjLQaXbqk'
video_id = video_url.split("v=")[-1]

example video #6: alessandro barbero - la grande battaglia: gettysburg, 1863, Storia & Cultura
(unstructured example)
video_url = 'https://www.youtube.com/watch?v=y4HYjEZq21w'
video_id = video_url.split("v=")[-1]

example video #7: come fare la pasta cacio e pepe - ricetta dolci e cucina - tutorial
(no transcript example)
video_url = 'https://www.youtube.com/watch?v=s9Kd-0FiICw'
video_id = video_url.split("v=")[-1]

"""

# user input URL
video_url = input('YouTube video URL: ')
video_id = video_url.split("v=")[-1]
video_title = funcs.get_video_title(video_url)

# fetch Italian language transcript if it exists
transcript = False
try:

	raw_it_script = YouTubeTranscriptApi.get_transcript(video_id, languages=['it'])
	transcript = True
	print('Collecting transcript...')

except:
	print('No transcript available for this URL. Please try another.')

if transcript:

	it_script_str = funcs.clean_script([phrase['text'] for phrase in raw_it_script])
	it_sentences = it_script_str.split("\n\n")
	num_it_sentences = len(it_sentences)

	# translate transcript to English and store as array of strings
	script_list = YouTubeTranscriptApi.list_transcripts(video_id)
	raw_en_script = script_list.find_transcript(['it']).translate('en').fetch()
	en_script_str = funcs.clean_script([phrase['text'] for phrase in raw_en_script])
	en_sentences = en_script_str.split("\n\n")

	# handle common English abbreviations with periods to avoid
	# false sentence creation when separating by the period char
	en_abbrevs = ['St.', 'Mr.', 'Mrs.', 'Dr.', ]

	for sentence in en_sentences:
		for abbrev in en_abbrevs:
			if abbrev in sentence:
				idx = en_sentences.index(sentence)
				en_sentences[idx] = sentence + ' ' + en_sentences[idx+1]
				en_sentences.remove(en_sentences[idx+1])

	en_script_str = '\n\n'.join(en_sentences)

	lang_strs = ['(italiano)', '(english)', '(italiano+english)']
	txt_filenames = funcs.get_txt_fnames(video_title, lang_strs)
	pdf_filenames = funcs.txt_to_other(txt_filenames, '.pdf')
	doc_filenames = funcs.txt_to_other(txt_filenames, '.docx')

	# print italian sentences on top of english sentences
	# and print a line between such pairs
	combo_script_str = str()
	script_strs = [it_script_str, en_script_str, combo_script_str]

	# form combined italian+english transcript string
	# and save all three transcripts as Word documents
	# in the case of a structured YouTube transcript
	if num_it_sentences > 1:

		print('Processing structured/punctuated transcript...')

		for i in range(len(it_sentences)):
		    combo_script_str += it_sentences[i] + "\n" + en_sentences[i] + "\n\n"

		funcs.scripts_to_doc(script_strs, doc_filenames)

	# otherwise, YouTube transcript comes as a single
	# string and needs to be processed differently than above
	elif num_it_sentences == 1:

		print('Processing unstructured/unpunctuated transcript...')

		# perform extra string processing on auto-generated
		# transcripts that are not partitioned into sentences
		char_line_lim = 80

		it_lines = funcs.script_to_lines(it_script_str, char_line_lim)
		en_lines = funcs.script_to_lines(en_script_str, char_line_lim)
		all_lines = [it_lines, en_lines]

		script_strs = list()
		curr_line = str()

		for lines in all_lines:

			lines_out = list()
			for i in range(len(lines)):
				if i%2 == 0:
					lines_out += lines[i] + '\n'
				else:
					lines_out += lines[i] + '\n\n'

			script_strs.append(lines_out)

		funcs.scripts_to_doc(script_strs, doc_filenames)

	else:

		print('No transcript available for this URL...')


from django.shortcuts import render
from django.http import HttpResponse , JsonResponse
from articles.models import Article
from django.db.models import Q

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

def home(request):
    return render(request, 'map/map_container.html')

@csrf_exempt
def set_bounds_map(request):
    print("lat NE = "+request.POST['latne']+"lng NE = "+request.POST['lngne']+"lat SW = "+request.POST['latsw']+"lng SW = "+request.POST['lngsw'])
    lat_ne = float(request.POST['latne'])
    lng_ne = float(request.POST['lngne'])
    lat_sw = float(request.POST['latsw'])
    lng_sw = float(request.POST['lngsw'])
    positions = {}

    articles = Article.objects.filter(Q(lat__gte=lat_sw) & Q(lat__lte=lat_ne)).filter(Q(lng__gte=lng_sw) & Q(lng__lte=lng_ne))
    for a in articles:
        positions[a.id] = {"lat": a.lat ,"lng": a.lng, "titre": a.titre}
    return JsonResponse(positions)


# Total number of the temporary list
BUNCHSIZE = 1000
# Total number of the rows for each file, it's better :
# EACHFILESIZE = BUNCHSIZE * 10,
# or EACHFILESIZE = BUNCHSIZE * 100,
# or EACHFILESIZE = BUNCHSIZE * 1000,
# ... ...
EACHFILESIZE = 2 * BUNCHSIZE

# Each line in the file is the mapping of attribute name to the
# attribute name position in source file for each project, not JSON file.
PROATTRNAME = '.ProAttrNameMap'

# The position of each attribute name in source file for each
# project, no JSON file.
PROATTRNAMEPOSITION = '.ProAttrNamePosition'

# Attribute name and postion for each source file, JSON file.
ATTRFILESUFFIX = '.COMAttrJSON'

# Attribute name file and attribute position file in source file, line to
# line mapping relationship, JSON file.
PROATTRSUFFIX = '.ProNameAndPositionofAttrFileLinetoLineMapJSON'

# Attribute key words for the each project
PROKEYWORDSUFFIX = '.ProKeyWordJSON'

# Map for attribute word to the src code for each project
ALLATTRSUFFIX = '.SRCAttrsMapToEachPro'

# Map for key word to the src code for each project
ALLKEYWORDSUFFIX = '.SRCKeyWordMaptoEachPro'

# All attribute names for each project
EACHPROATTROFSRCSUFFIX = '.ProSrcAttrNameJSON'
# All attribute key words for each proeject after word segmentation
EACHPROSRCKEYWORDSUFFIX = '.ProSrcKeyWordJSON'
# All attribute key words mapping for all the projects after word segmentation
ALLATTTSOFSRC = 'ATTRWD.JSON'
# All attribute key words for all the projects
ALLATTTSOFSRCMAP = 'ATTRWDM.JSON'

# All key word mapping for the project
ALLKEYOFSRC = 'KEYWD.JSON'
ALLKEYOFSRCMAP = 'KEYWDM.JSON'

# Attributes map file to each project
ATTRSMAPREFIX = 'SRC_'

# Key workd file to each project
KEYWORDPREFIX = 'KEY_'


import random
import time

import aiohttp

import structlog
from discord import Embed
from discord.ext import commands
from super.utils import fuz, owoify, superheroes

logger = structlog.getLogger(__name__)

class Astro(commands.Cog):
    """Horoscope"""

    def __init__(self, bot):
        self.bot = bot
        self.sunsigns = [
            "aries",
            "taurus",
            "gemini",
            "cancer",
            "leo",
            "virgo",
            "libra",
            "scorpio",
            "sagittarius",
            "capricorn",
            "aquarius",
            "pisces",
        ]
        self.times = ("today", "week", "month", "year")
        self.api = "http://horoscope-api.herokuapp.com/horoscope/{when}/{sunsign}"

    async def _get_sunsign(self, sunsign, when):
        logger.debug("cogs/astro/_get_sunsign: Fetching", sunsign=sunsign, when=when)
        async with aiohttp.ClientSession() as session:
            async with session.get(
                self.api.format(sunsign=sunsign, when=when),
                params={"t": int(time.time())},
                timeout=5,
            ) as resp:
                return await resp.json()

    async def _astro(self, ctx, owo=False):
        async with ctx.message.channel.typing():
            message = ctx.message.content.split(" ")

            if len(message) >= 2:
                logger.debug("cogs/astro/_astro: Fetching", message=message[1])
                sunsign = fuz(message[1], self.sunsigns, threshold=1)
            else:
                return await ctx.message.channel.send(
                    ".astro <sign> [**today**|week|month|year]"
                )

            when = fuz(
                message[2] if len(message) >= 3 else "today", self.times, "today"
            )

            title = f"{when}'s horoscope for {sunsign}"
            horoscope = (await self._get_sunsign(sunsign, when))["horoscope"]
            horoscope.replace('Ganesha', random.choice(superheroes))
            logger.info("cogs/astro/_astro: Fetched", sunsign=sunsign)
            if owo:
                horoscope = owoify(horoscope)
                title = owoify(title)

            return Embed(title=title, type="rich", description=horoscope)

    @commands.command(no_pm=True, pass_context=True)
    async def astro(self, ctx):
        """**.astro** <sign> [today|week|month|year] - Daily dose of bullshit"""
        return await ctx.message.channel.send(embed=await self._astro(ctx))

    @commands.command(no_pm=True, pass_context=True)
    async def astrowo(self, ctx):
        """**.astrowo** <sign> [today|week|month|year] - Daiwy dose of buwwshit"""
        return await ctx.message.channel.send(embed=await self._astro(ctx, owo=True))

def setup(bot):
    bot.add_cog(Astro(bot))


import pytest

import ibis.tests.util as tu

@pytest.mark.parametrize(
    'column',
    [
        'string_col',
        'double_col',
        'date_string_col',
        pytest.mark.xfail('timestamp_col', raises=AssertionError, reason='NYT')
    ]
)
@tu.skip_if_invalid_operation
@pytest.mark.backend
def test_distinct_column(backend, alltypes, df, column):
    expr = alltypes[column].distinct()
    result = expr.execute()
    expected = df[column].unique()
    assert set(result) == set(expected)


'''MIT License

Copyright (c) 2018 Marcel Meisner (Irrgeist)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import bpy
from .helper import Helper
from .operators import OperatorImportAppLink
from .operators import OperatorExportAppLink
from .operators import OperatorNewPBRMaterial
from .operators import OperatorSelectImage

class GUIProperties(bpy.types.PropertyGroup):

    export_mode = bpy.props.EnumProperty(name='Export type',
                                         items=(('ppp', 'Per-pixel painting', "Loads the mesh into 3DC's paint room for per-pixel painting"),
                                                ('mv', 'Microvertex painting', "Loads the mesh into 3DC's paint room for microvertex painting"),
                                                ('ptex', 'Ptex painting', "Loads the mesh into 3DC's paint room for Ptex painting"),
                                                ('uv', 'UV-mapping', "Loads the mesh into the UV room for UV-mapping"),
                                                ('ref', 'Reference mesh', "Loads the mesh as a reference object into 3DC"),
                                                ('retopo', 'Retopo mesh (new layer)', "Loads the mesh as a new layer into the Retopo room"),
                                                ('vox', 'Voxel object', "Loads the mesh as a voxel object into 3DC"),
                                                ('voxcombine','Single voxel object (merge all)', "Loads the mesh into 3DC as a single voxel object, all objects will be merged together in one volume"),
                                                ('alpha', 'New pen alpha', "Loads the mesh as a new pen alpha into 3DC"),
                                                ('prim', 'Voxel primitive', "Loads the mesh as a new merging primitve for voxels"),
                                                ('curv', 'Curve profile', "Loads the mesh as a curve profile into 3DC"),
                                                ('autopo', 'Auto-retopology', "Loads the mesh into the Retopo room for AUTOPO")
                                                ),
                                            default='ppp'
                                         )

    export_workflow = bpy.props.EnumProperty(name='Workflow type',
                                         description='Choose the PBR workflow for texture',
                                         items=(('metalness', 'Metalness/Roughness', 'Used Maps: color, metalness, roughness, normal, emissive'),
                                                ('manual', 'Manual', '3D-Coat will ask on import/export'),
                                                ),
                                        default='metalness'
                                         )

    color_map = bpy.props.StringProperty(
                                    name="File Path",
                                    description="Albedo/Color Map",
                                    maxlen=255,
                                    subtype='FILE_PATH',
                                    default=Helper.supported_images(),
                                    )

    metal_map = bpy.props.StringProperty(
                                    name="File Path",
                                    description="Metalness Map",
                                    maxlen=255,
                                    subtype='FILE_PATH',
                                    default=Helper.supported_images(),
                                    )

    rough_map = bpy.props.StringProperty(
                                    name="File Path",
                                    description="Roughness Map",
                                    maxlen=255,
                                    subtype='FILE_PATH',
                                    default=Helper.supported_images(),
                                    )

    normal_map = bpy.props.StringProperty(
                                    name="File Path",
                                    description="Normal Map",
                                    maxlen=255,
                                    subtype='FILE_PATH',
                                    default=Helper.supported_images(),
                                    )

    emissive_map = bpy.props.StringProperty(
                                    name="File Path",
                                    description="Emissive Map",
                                    maxlen=255,
                                    subtype='FILE_PATH',
                                    default=Helper.supported_images(),
                                    )
# Create main panel and layout
class AppLink3DCoatPanel(bpy.types.Panel):
    '''3D-Coat AppLink panel for Blender'''

    bl_idname = 'OBJECT_PT_applink_3dcoat_panel'
    bl_label = 'AppLink'
    bl_category = '3D-Coat'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_context = 'objectmode'

    def draw(self, context):
        layout = self.layout
        props = bpy.context.scene.irrgeist_3dc

        sel_obj = Helper.get_selected_mesh(context)
        object_name = sel_obj.name if Helper.is_mesh(sel_obj, context) else None
        has_exchange_folder = Helper.pref_exchange_directory() != ''

        box = layout.box()
        #   Title/Description
        row = box.row()
        if object_name and has_exchange_folder:
            row.label(text='Transfer "' + object_name + '" from/to 3D-Coat')
        elif has_exchange_folder:
            row.label(text='Select a mesh to transfer from/to 3D-Coat')
        else:
            row.label(text='Set exchange folder in the add-on preferences')

        # Export type selection box
        row = box.row()
        row.prop(props, 'export_mode', text='')

        # PBR workflow selection
        row = box.row()
        row.prop(props, 'export_workflow', text='', icon='TEXTURE_SHADED')

        # Buttons for triggering import
        row = box.row(align=True)
        row.operator('pilgway.3dcoat_import', text='Import', icon='IMPORT')
        row.operator('pilgway.3dcoat_export', text='Export', icon='EXPORT')
        row.enabled = object_name != None and has_exchange_folder

        # Create New PBR material to use in Blender
        box = layout.box()
        row = box.row()
        row = box.row()
        if object_name and has_exchange_folder:
            row.label(text='Create new PBR material for "' + object_name + '"')
        elif has_exchange_folder:
            row.label(text='Select a mesh to create a new material')
        else:
            row.label(text='Set exchange folder in the add-on preferences')

        # row = box.row()
        # row.prop(props, 'color_map', text='Color', icon='TEXTURE')

        # row = box.row()
        # row.prop(props, 'metal_map', text='Metalness', icon='TEXTURE')

        # row = box.row()
        # row.prop(props, 'rough_map', text='Roughness', icon='TEXTURE')

        # row = box.row()
        # row.prop(props, 'normal_map', text='Normal', icon='TEXTURE')

        # row = box.row()
        # row.prop(props, 'emissive_map', text='Emissive', icon='TEXTURE')

        #props = layout.operator(OperatorSelectImage.bl_idname)

        row = box.row()
        row.operator('pilgway.3dcoat_new_material', text='Create new material', icon='MATERIAL')
        row.enabled = object_name != None and has_exchange_folder


import unittest

from tfi.as_tensor import from_tensor, _shape_match_score

from functools import partialmethod

class ShapeMatchScoreTest(unittest.TestCase):
    pass

_SHAPE_MATCH_SCORE_FIXTURES = [
    # (name, (src, xfrm), score),
    ('exact_any', (None, None), (2, 0)),
    ('exact_scalar', ([], []), (2, 0)),
    ('exact_1d', ([3], [3]), (2, 1)),
    ('exact_1d_unknown', ([None], [None]), (2, 1)),
    ('exact_2d_unknown', ([None, 3], [None, 3]), (2, 2)),

    ('partial_any_scalar', (None, []), (1, 0)),
    ('partial_any_1d', (None, [3]), (1, 0)),
    ('partial_any_1d_unknown', (None, [None]), (1, 0)),

    ('partial_scalar_any', ([], None), (1, 0)),
    ('partial_1d_any', ([3], None), (1, 0)),
    ('partial_1d_unknown_any', ([None], None), (1, 0)),

    ('partial_image', ([16, 16, 3], [None, None, 3]), (1, 1)),
    ('partial_image_array', ([6, 16, 16, 3], [None, None, 3]), (1, 1)),

    ('none_scalar_1d', ([], [3]), (0, 0)),
    ('none_scalar_2d', ([], [3, 2]), (0, 0)),
    ('none_scalar_1d_unknown', ([], [None]), (0, 0)),
    ('none_scalar_2d_unknown', ([], [None, None]), (0, 0)),
]
for (name, args, expect) in _SHAPE_MATCH_SCORE_FIXTURES:
    def do_test(self, args, expect):
        result = _shape_match_score(*args)
        self.assertEqual(expect, result)

    setattr(ShapeMatchScoreTest, 'test_%s' % name, partialmethod(do_test, args, expect))

class FromTensorTest(unittest.TestCase):
    pass

import numpy as np
import numpy.testing as npt
mappings = [
    (np.int8, (None, None, 3), lambda x: 'int8 image'),
    (np.float32, (None, None, 3), lambda x: 'float32 image'),
]

_FROM_TENSOR_FIXTURES = [
    # (name, (tensor, xfrm), score),
    ('nothing', (None, mappings), None),
    ('image_int8', (np.ones([8,8,3], np.int8), mappings), 'int8 image'),
    ('image_float32', (np.ones([8,8,3], np.float32), mappings), 'float32 image'),
    ('image_float64', (np.ones([8,8,3], np.float64), mappings), None),
    ('arr_image_float32', (np.ones([1,8,8,3], np.float32), mappings), np.array(['float32 image'])),
    ('arr2_image_float32', (np.ones([2,8,8,3], np.float32), mappings), np.array(['float32 image', 'float32 image'])),
    ('arr_2d_image_float32', (np.ones([1,1,8,8,3], np.float32), mappings), np.array([['float32 image']])),
    ('arr2_2d_image_float32', (np.ones([2,1,8,8,3], np.float32), mappings), np.array([['float32 image'], ['float32 image']])),
]
for (name, args, expect) in _FROM_TENSOR_FIXTURES:
    def do_test(self, args, expect):
        result = from_tensor(*args)
        npt.assert_array_equal(expect, result)

    setattr(FromTensorTest, 'test_%s' % name, partialmethod(do_test, args, expect))

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


import random
import json
import string

from solariat_bottle.tests.base import BaseCase, RestCase

from solariat_bottle.db.lpage import LandingPage

URL = 'http://www.solariat.com'

class LPageDbTest(BaseCase):
    def test_create_delete(self):
        lpage = LandingPage.objects.create_by_user(
            self.user, url=URL)
        self.assertEqual(
            LandingPage.objects.count(), 1)
        lpage.delete_by_user(self.user)
        self.assertEqual(
            LandingPage.objects.count(), 0)

class LPageRestTest(RestCase):

    def create(self, url=None):
        if url is None:
            url = URL
        return self.do_post('landingpages', version='v1.2', url = url)

    def test_create(self):
        " correct creation should return json with ok = True and UUID "
        resp = self.do_post('landingpages', version='v1.2', url = URL)
        self.assertEqual(resp['ok'], True)
        self.assertEqual(resp['item']['url'], URL)
        self.assertEqual(resp['item']['display_field'], None)

    def test_create_duplicate(self):
        """ create more than one landingpage with
        the same url should return json with ok = False

        """
        resp1 = self.create(URL)
        resp2 = self.create(URL)
        self.assertTrue(resp1['ok'])
        self.assertFalse(resp2['ok'])

    def test_bad_json(self):
        "raise error on bad formed json filter parameter"
        self.create("foo")
        resp = self.client.get(
            '/api/v1.2/landingpages?token=%s' % self.auth_token,
            data = "{'url':'baz}",
            content_type='application/json')
        data = json.loads(resp.data)
        self.assertFalse(data['ok'])

    def test_get(self):
        "should return 0 or 1 for specific url"
        self.create('foo')
        self.create('bar')

        resp = self.do_get('landingpages', version='v1.2', url='foo')
        self.assertTrue(resp['ok'])
        self.assertEqual(len(resp['list']), 1)

        resp = self.do_get('landingpages', version='v1.2', url='baz')
        self.assertEqual(len(resp['list']), 0)

    def test_get_specific(self):
        " get specific landingpage "

        urls_count = random.randint(5, 10)
        for i in xrange(urls_count):
            self.create(URL + '/' + str(i))

        url = URL + '/' + str(random.randint(0, urls_count - 1))
        resp = self.do_get('landingpages', version='v1.2', url=url)
        self.assertEqual(resp['ok'], True)
        self.assertEqual(len(resp['list']), 1)
        self.assertEqual(resp['list'][0]['url'], url)

    def test_get_all(self):
        " get all landingpages "

        urls_count = random.randint(5, 10)
        for i in xrange(urls_count):
            self.create(URL + '/' + str(i))

        resp  = self.do_get('landingpages', version='v1.2')
        self.assertEqual(resp['ok'], True)
        self.assertEqual(len(resp['list']), urls_count)

    def test_delete_uuid(self):
        " delete specific landingpage using UUID "

        resp = self.create(URL)
        lpage_uuid = resp['item']['id']
        resp = self.do_delete('landingpages/%s' % lpage_uuid, version='v1.2')
        self.assertTrue(resp['ok'])

        resp = self.do_delete('landingpages/%s' % lpage_uuid, version='v1.2')
        self.assertFalse(resp['ok'])

    def test_delete_url(self):
        " delete specific landingpage using url "

        resp = self.create(URL)
        resp = self.do_delete('landingpages', url=URL, version='v1.2')
        self.assertEqual(resp['ok'], True)
        self.assertEqual(resp['message'], '1 docs was deleted, 0 rejected')

        resp = self.do_delete('landingpages', url=URL, version='v1.2')
        self.assertEqual(resp['ok'], True)
        self.assertEqual(resp['message'], '0 docs was deleted, 0 rejected')

    def test_weighted_fields(self):
        """make sure that randomly generated weighted_fields
        restored back without change
        """

        weighted_fields = []
        for _ in xrange(random.randint(1,5)):
            name = ''.join((random.choice(
                        string.letters + string.digits) for _ in xrange(
                        random.randint(5,10)) ))
            value = ''.join((random.choice(
                        string.letters + string.digits) for _ in xrange(
                        random.randint(5,10)) ))
            weight = random.random()
            weighted_fields.append({"name": name,
                                    "value": value,
                                   "weight": weight })

        resp = self.do_post(
            'landingpages', version='v1.2', url=URL, weighted_fields=weighted_fields)
        self.assertEqual(resp['ok'], True)
        self.assertEqual(resp['item']['weighted_fields'], weighted_fields)

    def test_missed_weight(self):
        " make sure that missed weight restored back with 1.0 value "

        name = ''.join((
                random.choice(
                    string.letters + string.digits) for _ in xrange(
                    random.randint(5,10)) ))
        value = ''.join((
                random.choice(
                    string.letters + string.digits) for _ in xrange(
                    random.randint(5,10)) ))
        weighted_fields = [ { 'name': name,
                             'value': value } ]
        resp = self.do_post('landingpages', version='v1.2', url=URL, weighted_fields=weighted_fields)
        self.assertEqual(resp['ok'], True)
        self.assertEqual(resp['item']['weighted_fields'][0]['weight'], 1.0)

    def test_archived(self):
        " test archiving capabilities "

        archived = []

        n = random.randint(3, 6)
        for i in xrange(n):
            url = "%s%s" % (URL, i)
            resp = self.create(url)
            archived.append(resp['item']['id'])
            self.do_delete('landingpages', version='v1.2', url=url)

        self.create()

        self.assertEqual(LandingPage.objects.find().count(), 1)
        self.assertEqual(LandingPage.objects.coll.count(), n + 1)

        lp_archived = random.choice(archived)

        stored_id = LandingPage.objects.get(id=lp_archived,
                                            is_archived=True).id
        self.assertEqual(str(stored_id), lp_archived)

        self.assertRaises(LandingPage.DoesNotExist,
                          LandingPage.objects.get,
                          id=lp_archived)

    def test_mass_deletion(self):
        lpage1 = LandingPage.objects.create_by_user(self.user,
                                                   url='solariat.com')
        lpage2 = LandingPage.objects.create_by_user(self.user,
                                                   url='solariat1.com')
        LandingPage.objects.remove_by_user(self.user)
        self.assertTrue(LandingPage.objects.find_one(lpage1.id) is None)
        self.assertTrue(LandingPage.objects.find_one(lpage2.id) is None)


from __future__ import absolute_import
import sys
sys.path.append('./')

import argparse
import os
import os.path as osp
import numpy as np
import math
import time
from PIL import Image, ImageFile

import torch
from torch import nn, optim
from torch.backends import cudnn
from torch.utils.data import DataLoader
from torchvision import transforms

from config import get_args
from lib import datasets, evaluation_metrics, models
from lib.models.model_builder import ModelBuilder
from lib.datasets.dataset import LmdbDataset, AlignCollate
from lib.loss import SequenceCrossEntropyLoss
from lib.trainers import Trainer
from lib.evaluators import Evaluator
from lib.utils.logging import Logger, TFLogger
from lib.utils.serialization import load_checkpoint, save_checkpoint
from lib.utils.osutils import make_symlink_if_not_exists
from lib.evaluation_metrics.metrics import get_str_list
from lib.utils.labelmaps import get_vocabulary, labels2strs

global_args = get_args(sys.argv[1:])

def image_process(img, imgH=32, imgW=100, keep_ratio=False, min_ratio=1):
  if keep_ratio:
    w, h = img.size
    ratio = w / float(h)
    imgW = int(np.floor(ratio * imgH))
    imgW = max(imgH * min_ratio, imgW)

  img = img.resize((imgW, imgH), Image.BILINEAR)
  img = transforms.ToTensor()(img)
  img.sub_(0.5).div_(0.5)

  return img

class DataInfo(object):
  """
  Save the info about the dataset.
  This a code snippet from dataset.py
  """
  def __init__(self, voc_type):
    super(DataInfo, self).__init__()
    self.voc_type = voc_type

    assert voc_type in ['LOWERCASE', 'ALLCASES', 'ALLCASES_SYMBOLS']
    self.EOS = 'EOS'
    self.PADDING = 'PADDING'
    self.UNKNOWN = 'UNKNOWN'
    self.voc = get_vocabulary(voc_type, EOS=self.EOS, PADDING=self.PADDING, UNKNOWN=self.UNKNOWN)
    self.char2id = dict(zip(self.voc, range(len(self.voc))))
    self.id2char = dict(zip(range(len(self.voc)), self.voc))

    self.rec_num_classes = len(self.voc)

def recognizer(img_path, gt_path, model, device, dataset_info,
               savedir="outputs/", only_price=False):
    gt_file = open(gt_path, "r")

    save_filename = os.path.basename(gt_path)
    save_path = os.path.join(savedir, save_filename)

    if not os.path.isdir(savedir):
      os.mkdir(savedir)

    fp = open(save_path, "w")

    bounding_boxes = [[int(coord) if coord.isnumeric() else coord
                       for coord in bbox.split('\n')[0].split(',', 8)]
                      for bbox in gt_file.readlines()]

    img = Image.open(img_path)
    img_width, img_height = img.width, img.height

    for bbox in bounding_boxes:
        label = bbox[8]

        if only_price and not type(label) == int and len(label) == 1:
            continue

        x1, y1 = bbox[0], bbox[1]
        x2, y2 = bbox[2], bbox[3]
        x3, y3 = bbox[4], bbox[5]
        x4, y4 = bbox[6], bbox[7]

        cropped_img = img.crop((x1, y3, x2, y1))

        cropped_img = image_process(cropped_img.convert('RGB'))

        with torch.no_grad():
            cropped_img = cropped_img.to(device)

        input_dict = {}
        input_dict['images'] = cropped_img.unsqueeze(0)

        rec_targets = torch.IntTensor(1, args.max_len).fill_(1)
        rec_targets[:, args.max_len - 1] = dataset_info.char2id[
          dataset_info.EOS]

        input_dict['rec_targets'] = rec_targets
        input_dict['rec_lengths'] = [args.max_len]

        output_dict = model(input_dict)
        pred_rec = output_dict['output']['pred_rec']
        pred_str, _ = get_str_list(pred_rec, input_dict['rec_targets'],
                                   dataset=dataset_info)

        pred_label = pred_str[0]
        print('Recognition result: {0}'.format(pred_label))

        txt_line = "{},{},{},{},{},{},{},{},{}\n".format(x1, y1, x2, y2,
                                                         x3, y3, x4, y4,
                                                         pred_label)
        fp.write(txt_line)
    fp.close()

def main(args):
  np.random.seed(args.seed)
  torch.manual_seed(args.seed)
  torch.cuda.manual_seed(args.seed)
  torch.cuda.manual_seed_all(args.seed)
  cudnn.benchmark = True
  torch.backends.cudnn.deterministic = True

  args.cuda = args.cuda and torch.cuda.is_available()
  if args.cuda:
    print('using cuda.')
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
  else:
    torch.set_default_tensor_type('torch.FloatTensor')

  # Create data loaders
  if args.height is None or args.width is None:
    args.height, args.width = (32, 100)

  dataset_info = DataInfo(args.voc_type)

  # Create model
  model = ModelBuilder(arch=args.arch, rec_num_classes=dataset_info.rec_num_classes,
                       sDim=args.decoder_sdim, attDim=args.attDim, max_len_labels=args.max_len,
                       eos=dataset_info.char2id[dataset_info.EOS], STN_ON=args.STN_ON)

  # Load from checkpoint
  if args.resume:
    checkpoint = load_checkpoint(args.resume)
    model.load_state_dict(checkpoint['state_dict'])

  if args.cuda:
    device = torch.device("cuda")
    model = model.to(device)
    model = nn.DataParallel(model)

  # Evaluation
  model.eval()
  images_path = args.images_path
  box_path = args.box_path
  imgs = os.listdir(images_path)

  for img in imgs:
      image_path = os.path.join(images_path, img)

      print("Image path:", image_path)

      gt_name = img.replace('jpg', 'txt')
      gt_path = os.path.join(box_path, gt_name)

      recognizer(image_path, gt_path, model, device, dataset_info,
       savedir="outputs/", only_price=False)

if __name__ == '__main__':
  # parse the config
  args = get_args(sys.argv[1:])
  main(args)


from openpyxl.utils import get_column_letter
from openpyxl.styles import Alignment
from decimal import *
from openpyxl import Workbook
from openpyxl import load_workbook
import os

# 创建一个表格
def createExcel():
    wb = Workbook()
    return wb;

def load_excel(path):
    wb = load_workbook(path)
    return wb

# 创建一个sheet
def creatSheet(wb, isFirst, sheetName):
    print('创建一个sheet--->%s' % (sheetName))
    if isFirst:
        ws = wb.active
    else:
        ws = wb.create_sheet()
    ws.title = sheetName
    return ws

# 把rows写进sheet
def putRowsToSheet(rows, ws):
    for row in rows:
        ws.append(row)
    adjustAlignmentAndColumnWidth(rows, ws)

def put_rows_to_sheet(wb, title, rows):
    ws = creatSheet(wb, False, title)
    for row in rows:
        ws.append(row)
    adjustAlignmentAndColumnWidth(rows, ws)

# 居中和自动调整列宽
def adjustAlignmentAndColumnWidth(rows, ws):
    column_widths = []
    for j, row in enumerate(rows):
        for i, cell in enumerate(row):
            ws[get_column_letter(i + 1) + str(j + 1)].alignment = Alignment(vertical="center", horizontal="center")

            if cell is None:
                continue

            if isinstance(cell, float) or isinstance(cell, int):
                continue

            if isinstance(cell, Decimal):
                continue

            if len(column_widths) > i:
                if len(cell) > column_widths[i]:
                    column_widths[i] = len(cell)
            else:
                column_widths += [len(cell)]

    for i, column_width in enumerate(column_widths):
        ws.column_dimensions[get_column_letter(i + 1)].width = (column_width + 2) * 1.75

if __name__ == '__main__':
    wb1 = createExcel()
    rows = [
        ['考试', '初中预备', '初一期末', '初二期末', '初三期末', ],
        ['排名', 32, 30, 30, 27, ],
        ['区平均分', 80.28, 79.04, 74.32, 105.88, ],
        ['同类平均分', 81.28, 72.04, 73.32, 104.88, ],
    ]
    ws = creatSheet(wb1, True, "测试")
    putRowsToSheet(rows, ws)
    wb1.save("test.xlsx")


class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0:
            return ""
        min_len = len(strs[0])
        min_str = ""
        for i in range(len(strs)):
            if len(strs[i]) <= min_len:
                min_len = len(strs[i])
                min_str = strs[i]

        i = 0
        while i < len(min_str):
            for n in strs:
                if min_str[i] != n[i]:
                    return min_str[0:i]
            i = i + 1

        return min_str[0:i]

s =Solution()

strs = ["flower", "flow", "flight", "fludn", "fladd", "flooo"]
strs = ["a"]
ss = s.longestCommonPrefix(strs)
print(ss)


# metadata
metadata = {
    'protocolName': 'PCR Prepation',
    'author': 'Nick <protocols@opentrons.com>',
    'source': 'Custom Protocol Request',
    'apiLevel': '2.0'
}

def run(ctx):

    [p300_multi_mount, p20_single_mount,
        number_of_samples] = get_values(  # noqa: F821
            'p300_multi_mount', 'p20_single_mount', 'number_of_samples'
        )

    # checks
    if p300_multi_mount == p20_single_mount:
        raise Exception('Pipette mounts cannot match.')
    if number_of_samples < 1 or number_of_samples > 32:
        raise Exception('Invalid number of DNA samples (must be 1-32).')

    # load labware
    plate = ctx.load_labware(
        'thermoscientific_96_wellplate_300ul', '1', 'PCR plate')
    tuberacks = [
        ctx.load_labware(
            'opentrons_24_tuberack_eppendorf_1.5ml_safelock_snapcap',
            slot,
            'DNA tuberack ' + str(i+1)
        )
        for i, slot in enumerate(['2', '4'])
    ]
    mm = ctx.load_labware(
        'nest_12_reservoir_15ml', '3', 'reservoir for mastermix').wells()[0]
    tips300 = [
        ctx.load_labware('opentrons_96_tiprack_300ul', '5', '300ul tips')]
    tips20 = [
        ctx.load_labware('opentrons_96_tiprack_20ul', '6', '20ul tips')]

    # pipettes
    m300 = ctx.load_instrument(
        'p300_multi', p300_multi_mount, tip_racks=tips300)
    p20 = ctx.load_instrument(
        'p20_single_gen2', p20_single_mount, tip_racks=tips20)

    # sample setup
    dna_sources = [
        tube
        for rack in tuberacks
        for tube in rack.wells()][:number_of_samples][:number_of_samples]
    mm_dests = plate.rows()[0]
    trip_dests = [
        row[i*3:i*3+3]
        for row in plate.rows() for i in range(4)][:number_of_samples]

    # distribute mastermix
    m300.pick_up_tip()
    for d in mm_dests:
        m300.transfer(24, mm, d.bottom(3), air_gap=10, new_tip='never')
        m300.blow_out(d.top(-2))
    m300.drop_tip()

    # transfer samples in triplicate
    for s, d_set in zip(dna_sources, trip_dests):
        p20.pick_up_tip()
        for d in d_set:
            p20.aspirate(1, s.bottom(2))
            p20.air_gap(2)
            p20.touch_tip(s, v_offset=-3)
            p20.dispense(2, d.top(-2))
            p20.aspirate(9, d.bottom(2))
            p20.dispense(10, d.bottom(2))
            p20.blow_out(d.top(-2))
            p20.touch_tip(d, v_offset=-3)
        p20.drop_tip()


# Copyright (C) 2010-2011 Richard Lincoln
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

from CIM16.IEC61968.Common.Document import Document

class TariffProfile(Document):
    """A schedule of charges; structure associated with Tariff that allows the definition of complex tarif structures such as step and time of use when used in conjunction with TimeTariffInterval and Charge. Inherited 'status.value' is defined in the context of the utility's business rules, for example: active, inactive, etc.A schedule of charges; structure associated with Tariff that allows the definition of complex tarif structures such as step and time of use when used in conjunction with TimeTariffInterval and Charge. Inherited 'status.value' is defined in the context of the utility's business rules, for example: active, inactive, etc.
    """

    def __init__(self, tariffCycle='', Tariffs=None, ConsumptionTariffIntervals=None, TimeTariffIntervals=None, *args, **kw_args):
        """Initialises a new 'TariffProfile' instance.

        @param tariffCycle: The frequency at which the tariff charge schedule is repeated Examples are: once off on a specified date and time; hourly; daily; weekly; monthly; 3-monthly; 6-monthly; 12-monthly; etc. At the end of each cycle, the business rules are reset to start from the beginning again.
        @param Tariffs: All tariffs defined by this tariff profile.
        @param ConsumptionTariffIntervals: All consumption tariff intervals used to define this tariff profile.
        @param TimeTariffIntervals: All time tariff intervals used to define this tariff profile.
        """
        #: The frequency at which the tariff charge schedule is repeated Examples are: once off on a specified date and time; hourly; daily; weekly; monthly; 3-monthly; 6-monthly; 12-monthly; etc. At the end of each cycle, the business rules are reset to start from the beginning again.
        self.tariffCycle = tariffCycle

        self._Tariffs = []
        self.Tariffs = [] if Tariffs is None else Tariffs

        self._ConsumptionTariffIntervals = []
        self.ConsumptionTariffIntervals = [] if ConsumptionTariffIntervals is None else ConsumptionTariffIntervals

        self._TimeTariffIntervals = []
        self.TimeTariffIntervals = [] if TimeTariffIntervals is None else TimeTariffIntervals

        super(TariffProfile, self).__init__(*args, **kw_args)

    _attrs = ["tariffCycle"]
    _attr_types = {"tariffCycle": str}
    _defaults = {"tariffCycle": ''}
    _enums = {}
    _refs = ["Tariffs", "ConsumptionTariffIntervals", "TimeTariffIntervals"]
    _many_refs = ["Tariffs", "ConsumptionTariffIntervals", "TimeTariffIntervals"]

    def getTariffs(self):
        """All tariffs defined by this tariff profile.
        """
        return self._Tariffs

    def setTariffs(self, value):
        for p in self._Tariffs:
            filtered = [q for q in p.TariffProfiles if q != self]
            self._Tariffs._TariffProfiles = filtered
        for r in value:
            if self not in r._TariffProfiles:
                r._TariffProfiles.append(self)
        self._Tariffs = value

    Tariffs = property(getTariffs, setTariffs)

    def addTariffs(self, *Tariffs):
        for obj in Tariffs:
            if self not in obj._TariffProfiles:
                obj._TariffProfiles.append(self)
            self._Tariffs.append(obj)

    def removeTariffs(self, *Tariffs):
        for obj in Tariffs:
            if self in obj._TariffProfiles:
                obj._TariffProfiles.remove(self)
            self._Tariffs.remove(obj)

    def getConsumptionTariffIntervals(self):
        """All consumption tariff intervals used to define this tariff profile.
        """
        return self._ConsumptionTariffIntervals

    def setConsumptionTariffIntervals(self, value):
        for p in self._ConsumptionTariffIntervals:
            filtered = [q for q in p.TariffProfiles if q != self]
            self._ConsumptionTariffIntervals._TariffProfiles = filtered
        for r in value:
            if self not in r._TariffProfiles:
                r._TariffProfiles.append(self)
        self._ConsumptionTariffIntervals = value

    ConsumptionTariffIntervals = property(getConsumptionTariffIntervals, setConsumptionTariffIntervals)

    def addConsumptionTariffIntervals(self, *ConsumptionTariffIntervals):
        for obj in ConsumptionTariffIntervals:
            if self not in obj._TariffProfiles:
                obj._TariffProfiles.append(self)
            self._ConsumptionTariffIntervals.append(obj)

    def removeConsumptionTariffIntervals(self, *ConsumptionTariffIntervals):
        for obj in ConsumptionTariffIntervals:
            if self in obj._TariffProfiles:
                obj._TariffProfiles.remove(self)
            self._ConsumptionTariffIntervals.remove(obj)

    def getTimeTariffIntervals(self):
        """All time tariff intervals used to define this tariff profile.
        """
        return self._TimeTariffIntervals

    def setTimeTariffIntervals(self, value):
        for p in self._TimeTariffIntervals:
            filtered = [q for q in p.TariffProfiles if q != self]
            self._TimeTariffIntervals._TariffProfiles = filtered
        for r in value:
            if self not in r._TariffProfiles:
                r._TariffProfiles.append(self)
        self._TimeTariffIntervals = value

    TimeTariffIntervals = property(getTimeTariffIntervals, setTimeTariffIntervals)

    def addTimeTariffIntervals(self, *TimeTariffIntervals):
        for obj in TimeTariffIntervals:
            if self not in obj._TariffProfiles:
                obj._TariffProfiles.append(self)
            self._TimeTariffIntervals.append(obj)

    def removeTimeTariffIntervals(self, *TimeTariffIntervals):
        for obj in TimeTariffIntervals:
            if self in obj._TariffProfiles:
                obj._TariffProfiles.remove(self)
            self._TimeTariffIntervals.remove(obj)


import networkx as nx
from parse import read_input_file, write_output_file
from utils import *
import sys
import glob
from os.path import basename, normpath

def solve(G, s):
    """
    Args:
        G: networkx.Graph
        s: stress_budget
    Returns:
        D: Dictionary mapping for student to breakout room r e.g. {0:2, 1:0, 2:1, 3:2}
        k: Number of breakout rooms
    """

    room_to_student = {}
    student_to_room = {}

    # Put people into their own breakout room
    for node in list(G.nodes):
        room_to_student[node] = [node]
        student_to_room[node] = node

    is_changed = 1
    while is_changed:
        is_changed = 0
        max_happiness = calculate_happiness(student_to_room, G)
        best_student = 0
        best_move = 0

        for student in list(G.nodes):
            curr_room = student_to_room[student]
            num_rooms = len(room_to_student)
            if len(room_to_student[student_to_room[student]]) == 1:
                num_rooms -= 1

            for new_room in room_to_student.keys():
                room_to_student[curr_room].remove(student)
                room_to_student[new_room].append(student)
                student_to_room[student] = new_room
                curr_happiness = calculate_happiness(student_to_room, G)

                new_room_stress = calculate_stress_for_room(room_to_student[new_room], G)
                stress_budget_room = s / num_rooms
                if new_room_stress > stress_budget_room:
                    curr_happiness = 0

                if curr_happiness > max_happiness:
                    max_happiness = curr_happiness
                    is_changed = 1
                    best_move = new_room
                    best_student = student

                room_to_student[new_room].remove(student)
                room_to_student[curr_room].append(student)
                student_to_room[student] = curr_room

        # Remove best student from current breakout room
        # Add best student to best breakout room
        # Change best student's breakout room to best breakout room

        if is_changed:
            room_to_student[student_to_room[best_student]].remove(best_student)
            room_to_student[best_move].append(best_student)
            student_to_room[best_student] = best_move

        rooms = list(room_to_student.keys())
        for r in rooms:
            if len(room_to_student[r]) == 0:
                room_to_student.pop(r)

    print("Currently at swap")
    #Check for swaps
    is_changed = 1
    while is_changed:
        is_changed = 0
        max_happiness = calculate_happiness(student_to_room, G)
        best_student = 0
        best_student2 = 0

        for student in list(G.nodes):
            num_rooms = len(room_to_student)
            for student2 in list(G.nodes):
                if student != student2:
                    student_old_room = student_to_room[student]
                    student2_old_room = student_to_room[student2]

                    room_to_student[student2_old_room].append(student)
                    #Remove student from student room
                    student_to_room[student] = student2_old_room
                    #Put student2 in student room
                    room_to_student[student_old_room].append(student2)
                    #Remove student2 from student2 room
                    student_to_room[student2] = student_old_room

                    # Make sure students are not in same breakout room
                    student_room_stress = calculate_stress_for_room(room_to_student[student2_old_room], G)
                    student2_room_stress = calculate_stress_for_room(room_to_student[student_old_room], G)

                    stress_budget_room = s / num_rooms
                    if student_room_stress < stress_budget_room or student2_room_stress < stress_budget_room:
                        #Put student in student room
                        room_to_student[student_old_room].append(student)
                        #Remove student from student2 room
                        student_to_room[student] = student_old_room
                        #Put student2 in student room
                        room_to_student[student2_old_room].append(student2)
                        #Remove student2 from student2 room
                        student_to_room[student2] = student2_old_room

                    else:
                        curr_happiness = calculate_happiness(student_to_room, G)
                        if curr_happiness > max_happiness:
                            max_happiness = curr_happiness
                            is_changed = 1
                            best_student = student
                            best_student2 = student2
                        #Put student in student room
                        room_to_student[student_old_room].append(student)
                        #Remove student from student2 room
                        student_to_room[student] = student_old_room
                        #Put student2 in student room
                        room_to_student[student2_old_room].append(student2)
                        student_to_room[student2] = student2_old_room

        if is_changed:
            student_old_room = student_to_room[best_student]
            student2_old_room = student_to_room[best_student2]
            room_to_student[student2_old_room].append(best_student)
            student_to_room[best_student] = student2_old_room
            room_to_student[student_old_room].append(best_student2)
            student_to_room[best_student2] = student_old_room

    index = 0
    output = {}
    for r in room_to_student.keys():
        output[index] = room_to_student[r]
        index += 1

    return convert_dictionary(output), len(output)

# Here's an example of how to run your solver.

# Usage: python3 solver.py test.in

# if __name__ == '__main__':
#     assert len(sys.argv) == 2
#     path = sys.argv[1]
#     G, s = read_input_file(path)
#     D, k = solve(G, s)
#     assert is_valid_solution(D, G, s, k)
#     print("Total Happiness: {}".format(calculate_happiness(D, G)))
#     write_output_file(D, 'out/test.out')

# For testing a folder of inputs to create a folder of outputs, you can use glob (need to import it)
if __name__ == '__main__':
    inputs = glob.glob('inputs/*')
    for input_path in inputs:
        output_path = 'outputs/' + basename(normpath(input_path))[:-3] + '.out'
        G, s = read_input_file(input_path, 100)
        D, k = solve(G, s)
        assert is_valid_solution(D, G, s, k)
        cost_t = calculate_happiness(D, G)
        write_output_file(D, output_path)


import nltk # refers to NATURAL LANGUAGE TOOLKIT
import warnings
import random

warnings.filterwarnings("ignore")

# nltk.download() # for downloading packages
# nltk.download('punkt') #first-time use only
# nltk.download('wordnet') #first-time use only

"""              small greeting function                """

GREETING_INPUTS = ("hello", "hi", "greetings", "sup", "what's up", "hey",)
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are talking to me"]

# Checking for greetings
def greeting(sentence):
    """If user's input is a greeting, return a greeting response"""
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)

""" getres function to give the queried result from wiki
    there's use of little pre-text processing (you can learn online how to do that)
     i know it can be improved further , but yeah try another queries to see at least it is working"""

def getres(user_response):
    # split into words
    tokens = nltk.word_tokenize(user_response)
    # convert to lower case
    tokens = [w.lower() for w in tokens]
    # remove punctuation from each word
    import string
    table = str.maketrans('', '', string.punctuation)
    stripped = [w.translate(table) for w in tokens]
    # remove remaining tokens that are not alphabetic
    words = [word for word in stripped if word.isalpha()]
    # filter out stop words
    from nltk.corpus import stopwords
    stop_words = set(stopwords.words('english'))
    words = [w for w in words if not w in stop_words]
    try:
        import wikipedia
        document = wikipedia.summary(words, sentences=5)
        return document
    except Exception as e:
        print("type query precisely "+str(e))

flag = True
print("ROBO: My name is Robo. I will answer your queries about Chatbots. If you want to exit, type Bye or to make "
      "automatic review just type 'review'")

while (flag == True):
    user_response = input("YOU:")
    user_response = user_response.lower()
    if (user_response != 'bye'):
        if (user_response == 'thanks' or user_response == 'thank you'):
            flag = False
            print("ROBO: You are welcome..")
        else:
            if (greeting(user_response) != None):
                print("ROBO: " + greeting(user_response))
            else:
                if(user_response=="review"):
                    import review
                    review.review_writer()
                else:
                    print("ROBO: ", end="")
                    getres(user_response)
                    print(getres(user_response))
    else:
        flag = False
        print("ROBO: Bye! take care..")


"""Loading screen"""

import Tkinter as tk
import time

def show(bool):
    root = tk.Tk()
    root.title("Loading")
    root.geometry("300x50")

    label = tk.StringVar(root)
    i = 0

    label.set(i)

    l = tk.Label(root,textvariable=label)
    l.grid(row=0,column=0)

    test = tk.Label(root,text="function done!")
    test.grid(row=1, column=0)

    root.mainloop()


import mne
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt

# read the epoch data
epo = np.load("./epo_noref.npy")
n_p, n_conds, n_chan, n_t = epo.shape
pids = range(n_p)

# define the times
t = np.linspace(-0.1, 0.7, n_t)

# compute the neural difference score
X = epo[:, (0, 2)].mean(1) - epo[:, (1, 3)].mean(1)

# read the behavioral data
behav = np.genfromtxt("valence_scores.csv", dtype=None, names=True,
                      delimiter=',')

# compute the behavioral difference score
neu = behav[behav['valence'] == 'neutral']['score']
neg = behav[behav['valence'] == 'negative']['score']
y = (neu - neg) / 48.

# setup the regression
svr = SVR(kernel="linear", C=10**-10)

# setup empty array of scores
R = np.zeros((n_p, n_t))

# loop through the times
for ti in range(n_t):

    # get the channel data
    x = X[:, :, ti]

    # loop through the participants
    for pi in pids:
        # get the training and testing indices
        ix_test = np.in1d(pids, pi)
        ix_train =  np.logical_not(ix_test)

        # get the training and testing values
        y_test = y[ix_test]
        y_train = y[ix_train]

        x_test = x[ix_test]
        x_train = x[ix_train]

        # fit the svm
        svr.fit(x_train, y_train)

        # score the svm
        y_pred = svr.predict(x_test)
        R[pi, ti] = (y_test - y_pred)**2

ax = plt.axes()
ax.plot(t, R.mean(0))
ax.fill_between(t, R.mean(0) - R.std(0) / np.sqrt(n_p),
                R.mean(0) + R.std(0) / np.sqrt(n_p), alpha=0.3)

plt.show()


"""
Profile groupfitter

See where the bulk of computation occurs.

Examples on how to profile with python
https://docs.python.org/2/library/profile.html
"""

import cProfile
import logging
import numpy as np
import pstats
import sys

sys.path.insert(0, '..')
from chronostar.synthdata import SynthData
from chronostar import tabletool
from chronostar import compfitter

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, filename='compfitter.log')
    save_dir = ''
    group_savefile = save_dir + 'origins_stat.npy'
    xyzuvw_init_savefile = save_dir + 'xyzuvw_init_stat.npy'
    astro_savefile = save_dir + 'astro_table_stat.txt'
    xyzuvw_conv_savefile = save_dir + 'xyzuvw_conv_stat.fits'

    pars = np.array([0., 0., 0., 0., 0., 0., 5., 2., 1e-8])
    starcount = 100
    error_frac = 1.
    synth_data = SynthData(pars=pars, starcounts=starcount)
    synth_data.synthesise_everything()
    tabletool.convert_table_astro2cart(synth_data.table)
    data = tabletool.build_data_dict_from_table(synth_data.table)

    stat_file = 'compfitter.stat'
    # best_fit, chain, lnprob = \
    cProfile.run(
        "compfitter.fit_comp(data=data, plot_it=True,"
        "convergence_tol=2., burnin_steps=400, plot_dir='',"
        "save_dir='')",
        stat_file,
    )

    stat = pstats.Stats(stat_file)
    stat.sort_stats('cumtime')
    stat.print_stats(0.3)


#scraper for RotoWire Last 10 FanDuel PG Defense vs. Position DFS Stats

from bs4 import BeautifulSoup
import urllib2
import csv

url = "http://www.rotowire.com/daily/nba/defense-vspos.htm?statview=last10&pos=PF"

page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, "html.parser")

rotowireFanduelDvPLast10PF = open('rotowireFanduelDvPLast10PF.csv', 'w')
rotowireFanduelDvPLast10PF.write("Team, Vs. Pos, Season, Last 5, Last 10, PTS, REB, AST, STL, BLK, 3PM, FG%, FT%, TO" + "\n")

for tr in soup.find_all('tr')[2:]:
    tds = tr.find_all('td')

    name = tds[0].text
    if name in ['Philadelphia 76ers']:
        name = 'PHI'
    if name in ['Minnesota Timberwolves']:
        name = 'MIN'
    if name in ['Washington Wizards']:
        name = 'WAS'
    if name in ['Cleveland Cavaliers']:
        name = 'CLE'
    if name in ['New Orleans Pelicans']:
        name = 'NOP'
    if name in ['Houston Rockets']:
        name = 'HOU'
    if name in ['Portland Trail Blazers']:
        name = 'POR'
    if name in ['Denver Nuggets']:
        name = 'DEN'
    if name in ['Detroit Pistons']:
        name = 'DET'
    if name in ['Dallas Mavericks']:
        name = 'DAL'
    if name in ['Los Angeles Lakers']:
        name = 'LAL'
    if name in ['Los Angeles Clippers']:
        name = 'LAC'
    if name in ['Miami Heat']:
        name = 'MIA'
    if name in ['San Antonio Spurs']:
        name = 'SAS'
    if name in ['Atlanta Hawks']:
        name = 'ATL'
    if name in ['Brooklyn Nets']:
        name = 'BKN'
    if name in ['Phoenix Suns']:
        name = 'PHO'
    if name in ['New York Knicks']:
        name = 'NYK'
    if name in ['Golden State Warriors']:
        name = 'GSW'
    if name in ['Memphis Grizzlies']:
        name = 'MEM'
    if name in ['Sacramento Kings']:
        name = 'SAC'
    if name in ['Boston Celtics']:
        name = 'BOS'
    if name in ['Milwaukee Bucks']:
        name = 'MIL'
    if name in ['Charlotte Hornets']:
        name = 'CHA'
    if name in ['Toronto Raptors']:
        name = 'TOR'
    if name in ['Indiana Pacers']:
        name = 'IND'
    if name in ['Orlando Magic']:
        name = 'ORL'
    if name in ['Utah Jazz']:
        name = 'UTA'
    if name in ['Chicago Bulls']:
        name = 'CHI'
    if name in ['Oklahoma City Thunder']:
        name = 'OKC'
    name = str(name.encode('ascii'))

    vsPos = tds[1].text
    vsPos = str(vsPos.encode('ascii'))

    season = tds[2].text
    season = str(season.encode('ascii'))

    last5 = tds[3].text
    last5 = str(last5.encode('utf-8'))

    last10 = tds[4].text
    last10 = str(last10.encode('utf-8'))

    pts = tds[5].text
    pts = str(pts.encode('utf-8'))

    reb = tds[6].text
    reb = str(reb.encode('utf-8'))

    ast = tds[7].text
    ast = str(ast.encode('utf-8'))

    stl = tds[8].text
    stl = str(stl.encode('utf-8'))

    blk = tds[9].text
    blk = str(blk.encode('utf-8'))

    threePM = tds[10].text
    threePM = str(threePM.encode('utf-8'))

    fgPercent = tds[11].text
    fgPercent = str(fgPercent.encode('utf-8'))

    ftPercent = tds[12].text
    ftPercent = str(ftPercent.encode('utf-8'))

    turnOver = tds[13].text
    turnOver = str(turnOver.encode('utf-8'))

    data = (name, vsPos, season, last5, last10, pts, reb, ast, stl, blk, threePM, fgPercent, ftPercent, turnOver)
    rotowireFanduelDvPLast10PF.write('%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, \n' % data)

rotowireFanduelDvPLast10PF.close()

reader = csv.reader(open("rotowireFanduelDvPLast10PF.csv"))

row = 0

html = '
'

for row in reader:
    html += '
'
    for column in row:
            html += ''
            html += column
            html += '	'
    html += '
'

html += '
'


import os
import torch
import yaml
from datetime import datetime
import random
import numpy as np

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
cfg = yaml.safe_load(open('configs.yml', 'r', encoding='utf-8'))

def set_cfg(new_cfg):
    """
    change cfg file within run. a little shady.
    Args:
        new_cfg:

    Returns: None (cfg is changed for everyone).

    """
    global cfg
    cfg = new_cfg

current_time = datetime.now()
rnd_seed = cfg['misc']['random_seed']
if rnd_seed is not None:
    torch.manual_seed(rnd_seed)
    torch.cuda.manual_seed(rnd_seed)
    np.random.seed(rnd_seed)
    random.seed(rnd_seed)

def get_depth_dir():
    """

    Returns: path to depth folder

    """
    return os.path.join(cfg['dataset']['data_dir'], cfg['dataset']['depth_foldername'])

def get_img_dir():
    """

    Returns: path to img folder

    """
    return os.path.join(cfg['dataset']['data_dir'], cfg['dataset']['img_foldername'])

def get_test_dir():
    """
        Returns: path to test folder
    """
    return os.path.join(cfg['dataset']['data_dir'], cfg['dataset']['test_foldername'])

def get_dev():
    """
    Returns: device to be used for torch (preferably GPU).

    """
    if torch.cuda.is_available() and cfg['misc']['use_cuda']:
        return torch.device('cuda')
    else:
        return torch.device('cpu')

def get_folder_name():
    """

    Returns: folder name in which to save tensorboard run and model checkpoints.

    """
    cfg_checkpoint = cfg['checkpoint']
    use_saved = cfg_checkpoint['use_saved']
    if use_saved:
        folder_name = cfg_checkpoint['saved_path']
        if folder_name.endswith('.pt'):
            return '/'.join(folder_name.split('/')[:-1])
        else:
            return folder_name
    run_name = cfg_checkpoint['run_name']
    if run_name is None:
        run_name = input('enter run name: ')
        cfg_checkpoint['run_name'] = run_name
    return run_name

def save_checkpoint(epoch, net, optimizer, running_loss):
    """
    save a checkpoint of the network for future use.
    location is defined in configs.yml file.
    Args:
        epoch: int.
        net: network object (both weights and model object base is saved).
        optimizer:
        running_loss:

    Returns: None (checkpoint saved).

    """
    logger.info(f'\nsaving checkpoint at epoch {epoch}...')
    folder = get_folder_name()
    folder = os.path.join('../models', folder)
    filename = 'epoch_' + str(epoch).zfill(4) + '.pt'
    if not os.path.exists(folder):
        os.mkdir(folder)
    full_path = os.path.join(folder, filename)
    if os.path.exists(full_path):
        logger.warning(f'not saving {full_path} as it already exists.')
        return
    torch.save({
        'model': net,
        'optimizer': optimizer,
        'epoch': epoch,
        'loss': running_loss
    }, full_path)

def load_checkpoint(path=None):
    """
    load a saved checkpoint from the training process,
    be it for continued training or inference.
    location from which to load checkpoint is defined in configs.yml file.

    Args:
        path (string, optional): specify manual location of checkpoint.
                                 defaults to path in configs file.
    Returns: (net, optim, epoch, loss) where:
        net: weighted net
        optim: weighted optimizer
        epoch: updated epoch from checkpoint
        loss: current loss in checkpoint.

    """
    if path is None:
        path = os.path.join('..', 'models', cfg['checkpoint']['saved_path'])
    if not path.endswith('.pt'):
        # default to last trained model.
        path = os.path.join(path, max(os.listdir(path)))
    if not os.path.exists(path):
        raise FileNotFoundError
    logger.info(f'loading from {path}')
    checkpoint = torch.load(path)
    net = checkpoint['model']
    optim = checkpoint['optimizer']
    epoch = checkpoint['epoch']
    loss = checkpoint['loss']
    return net, optim, epoch, loss


""" Write a Python class to find the three elements that sum to zero
from a list of n real numbers.
Input array : [-25, -10, -7, -3, 2, 4, 8, 10]
Output : [[-10, 2, 8], [-7, -3, 10]]"""
def find_sum(numbers):
    n = len(numbers)

    for i in range(0, n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                if numbers[i]+numbers[j]+numbers[k] == 0:
                    print(numbers[i], numbers[j], numbers[k])

numbers = [1, 1, 4, -4, 0, -2, ]
find_sum(numbers)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('redheart', '0002_auto_20160606_2339'),
    ]

    operations = [
        migrations.RenameField(
            model_name='counter',
            old_name='LikeUser',
            new_name='username',
        ),
        migrations.RemoveField(
            model_name='userprofile',
            name='iconic_image',
        ),
        migrations.RemoveField(
            model_name='userprofile',
            name='like',
        ),
        migrations.AddField(
            model_name='counter',
            name='iconic_image',
            field=models.ImageField(upload_to=b'iconic_image', blank=True),
        ),
        migrations.AddField(
            model_name='counter',
            name='like',
            field=models.IntegerField(default=0),
        ),
    ]


#!/usr/bin/env python3.5
"""
The MIT License

Copyright (c) 2015 Kyle Christensen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from configparser import SafeConfigParser
from operator import itemgetter
import json
import os
import requests

def getUntappdActivity(cfg):
    """Returns a requests object full of Untappd API data"""
    try:
        return requests.get(getURL(cfg, 'checkin/recent')).text
    except ConnectionError:
        print('There was an error connecting to the Untappd API')

def getURL(cfg, method):
    """Returns an API url with credentials inserted"""
    return "https://api.untappd.com/v4/{}?" \
        "client_id={}&client_secret={}&access_token={}&min_id={}".format(
            method,
            cfg.get('untappd', 'id'),
            cfg.get('untappd', 'secret'),
            cfg.get('untappd', 'token'),
            cfg.get('untappd', 'lastseen'))

def loadConfig(cfg_file):
    """Returns a configparser object from the parsed config file"""
    if not os.path.exists(cfg_file):
        raise OSError('ERROR: Configuration file slappd.cfg does not exist')
    cfg = SafeConfigParser()
    cfg.read(cfg_file)
    return cfg

def notifySlack(token, text, icon, title=None, thumb=None):
    """Sends a Slack message via webhooks"""
    url = 'https://hooks.slack.com/services/' + token
    # If thumb is set, we're sending a badge notification
    if thumb is not None:
        payload = {
            'attachments': [
                {
                    'title': title,
                    'text': text,
                    'thumb_url': thumb
                }
            ],
            'icon_url': icon,
            'username': 'Untappd'
        }
    else:
        payload = {
            'icon_url': icon,
            'text': text,
            'username': 'Untappd'
        }
    try:
        requests.post(url, data=json.dumps(payload))
    except ConnectionError:
        print('There was an error connecting to the Slack API')

def main():
    """Where the magic happens"""
    cfg_file = os.path.dirname(os.path.realpath(__file__)) + '/slappd.cfg'
    cfg = loadConfig(cfg_file)
    data = json.loads(getUntappdActivity(cfg))
    text = ''

    if data['meta']['code'] == 200:
        checkins = data['response']['checkins']['items']
        for checkin in checkins:
            user = checkin['user']['user_name'].lower()
            # If this is one of our watched users, let's send a Slack message
            if user in cfg.get('untappd', 'users'):
                # If any users earned badges, let's send individual messages
                for badge in checkin['badges']['items']:
                    title = "<{0}/user/{1}|{2} {3}> earned the {4} badge!" \
                        .format(
                            'http://untappd.com',
                            checkin['user']['user_name'],
                            checkin['user']['first_name'],
                            checkin['user']['last_name'],
                            badge['badge_name'])
                    notifySlack(
                        cfg.get('slack', 'token'),
                        badge['badge_description'],
                        badge['badge_image']['sm'],
                        title,
                        badge['badge_image']['md'])

                # Lump all of the checkins together as one message
                text += ":beer: *<{0}/user/{1}|{2} {3}>* is " \
                    "drinking a *<{0}/b/{8}/{4}|{5}>* by " \
                    "*<{0}/w/{8}/{7}|{6}>* ({9}/5)\n".format(
                        'http://untappd.com',
                        checkin['user']['user_name'],
                        checkin['user']['first_name'],
                        checkin['user']['last_name'],
                        checkin['beer']['bid'],
                        checkin['beer']['beer_name'],
                        checkin['brewery']['brewery_name'],
                        checkin['brewery']['brewery_id'],
                        checkin['brewery']['brewery_slug'],
                        checkin['rating_score'])

                # If there's a check-in comment, include it
                if len(checkin['checkin_comment']):
                    text += "\n>\"{0}\"\n".format(checkin['checkin_comment'])

        # Send a message if there has been any check-in activity
        if len(text):
            notifySlack(
                cfg.get('slack', 'token'),
                text,
                checkin['beer']['beer_label'])

        # Find the id of the most recent checkin
        if data['response']['checkins']['count']:
            cfg.set(
                'untappd',
                'lastseen',
                str(max(data['response']['checkins']['items'],
                    key=itemgetter('checkin_id'))['checkin_id']))

            # Update the config file with the last checkin seen
            try:
                with open(cfg_file, 'w') as fd:
                    cfg.write(fd)
            except EnvironmentError:
                print('There was an error writing to the config file')
    else:
        print("Untappd API returned http code {}".format(data['meta']['code']))

if __name__ == '__main__':
    main()


import argparse
import os
import joblib
from utils import SuperTree, DataLoader, IdConverter, Selector, npz_merge
import numpy as np
from functools import reduce

des = '''
This is an integrated data preprocessor for Ontology-aware Neural Network.

Work mode:
\tcheck mode: check all of your data files, the error data file are saved in tmp/ folder.
\tbuild mode: de novoly build a species tree using your own data.
\tconvert mode: convert tsv file from EBI MGnify database to model acceptable n-dimensional array.
\tcount mode: count the number of samples in each biome.
\tmerge mode: merge multiple npz files to a single npz.
\tselect mode: do feature selection for merged matrices npz.
'''
parser = argparse.ArgumentParser(description=des,
                                formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("mode", type=str, choices=['check', 'build', 'convert', 'count', 'merge', 'select'],
                    default='convert',
                    help = "work mode of the program. default: convert")
parser.add_argument("--n_jobs", type=int, default=1,
                    help = 'the number of processors to use. default: 1')
parser.add_argument('--input_dir', type=str, default='data/',
                    help = 'input directory, must be parent folder of biome folders. default: data/')
parser.add_argument('--output_dir', type=str, default='matrices/',
                    help = 'output directory. default: matrices')
parser.add_argument('--tree', type=str, default='tree/',
                    help = 'the directory of trees (species_tree.pkl and biome_tree.pkl). default: tree/')

args = parser.parse_args()
loader = DataLoader(path = args.input_dir)
if not os.path.isdir(args.output_dir): os.mkdir(args.output_dir)
if not os.path.isdir(args.tree): os.mkdir(args.tree)

if args.mode == 'check':
    # tested
    loader.check_data(header=1)
    loader.save_error_list()

elif args.mode == 'build':
    # tested
    converter = IdConverter()
    paths = []
    for i in map(lambda x: x.iloc(1)[2], loader.get_data(header=1)): paths.extend(i)
    paths = map(lambda x: converter.convert(x, sep=';'), paths)
    # print(list(paths)[0:5])
    stree = SuperTree()
    stree.create_node(identifier='root')
    stree.from_paths(paths)
    # stree.show()
    stree.to_pickle(file=os.path.join(args.tree, 'species_tree.pkl'))

    biomes = map(lambda x: converter.convert(x, sep='-'), os.listdir(args.input_dir))
    biomes = list(map(lambda x: x[1:], biomes))
    print(biomes)
    btree = SuperTree()
    btree.create_node(identifier='root')
    btree.from_paths(biomes)
    btree.show()
    btree.to_pickle(file=os.path.join(args.tree, 'biome_tree.pkl'))

elif args.mode == 'convert':
    tree = SuperTree()
    converter = IdConverter()
    stree = tree.from_pickle(os.path.join(args.tree, 'species_tree.pkl'))
    biome_tree = tree.from_pickle(os.path.join(args.tree, 'biome_tree.pkl'))
    data = []
    for i in map(lambda x: x.iloc(1)[1:], loader.get_data(header=1)): data.append(i.values.tolist())
    data = map(lambda x: {converter.convert(sp[1], sep=';')[-1]: sp[0] for sp in x}, data)

    biomes = map(lambda x: converter.convert(os.path.split(x)[0].split('/')[-1],
                                            sep='-'),
                                            loader.paths_keep)
    biomes = list(biomes)
    matrices = []; labels = []
    for index, sample in enumerate(data):
        species_tree = stree.copy()
        species_tree.init_nodes_data(value = 0)
        species_tree.fill_with(data = sample)
        species_tree.update_value()
        Sum = species_tree['root'].data
        species_tree.remove_levels(species_tree.depth())
        matrices.append(species_tree.get_matrix()/Sum) # relative abundance
        biome_tree.init_nodes_data(value = 0)
        biome_tree.fill_with(data = {biome: 1 for biome in biomes[index]})
        bfs_data = biome_tree.get_bfs_data()
        labels.append([np.array(bfs_data[i], dtype=np.float32) for i in range(biome_tree.depth() + 1)])
    labels = [[np.array(label[i]) for label in labels] for i in range(len(labels[0]))]
    np.savez(os.path.join(args.output_dir, 'matrices.npz'), matrices=matrices,
        label_0 = labels[0], label_1 = labels[1], label_2 = labels[2], label_3 = labels[3],
        label_4 = labels[4], label_5 = labels[5])

elif args.mode == 'count':
    # tested
    sample_count = loader.get_sample_count()
    res = ['{}:{}'.format(key, value) for key, value in sample_count.items()]
    print('\n'.join(res)) # save needed
    # id_id needed, unique tag conversion needed
    # new algorithm

elif args.mode == 'merge':
    # tested
    files = map(lambda x: os.path.join(args.input_dir, x), os.listdir(args.input_dir))
    merged_npz = npz_merge(files)
    np.savez(os.path.join(args.output_dir, 'merged_matrices.npz'),
            matrices = merged_npz['matrices'],
            label_0 = merged_npz['label_0'],
            label_1 = merged_npz['label_1'],
            label_2 = merged_npz['label_2'],
            label_3 = merged_npz['label_3'],
            label_4 = merged_npz['label_4'],
            label_5 = merged_npz['label_5'])

elif args.mode == 'select':
    # tested
    if len(os.listdir(args.input_dir)) != 1: print('you need to merge npzs first')
    tmp = np.load(os.path.join(args.input_dir, os.listdir(args.input_dir)[0]))
    matrices = tmp['matrices']
    print(matrices.shape)
    labels_ = {i: tmp[i] for i in ['label_0', 'label_1','label_2','label_3','label_4','label_5']}
    labels = reduce(lambda x,y: np.concatenate((x,y), axis=1), labels_.values())
    selector = Selector(matrices)
    selector.run_basic_select()
    feature_ixs = selector.basic_select__
    tmp_matrices = matrices[:, feature_ixs, :]
    print(tmp_matrices.shape)
    selector = Selector(tmp_matrices)
    selector.cal_feature_importances(label=labels, n_jobs=2)
    selector.run_RF_regression_select()
    feature_ixs = selector.RF_select__
    new_matrices = tmp_matrices[:, feature_ixs, :]
    print(new_matrices.shape)
    np.savez(os.path.join(args.output_dir, 'matrices_(selected_features).npz'),
            matrices=new_matrices,
            label_0 = labels_['label_0'],
            label_1 = labels_['label_1'],
            label_2 = labels_['label_2'],
            label_3 = labels_['label_3'],
            label_4 = labels_['label_4'],
            label_5 = labels_['label_5'],
            )


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/gwfhlang/__main__.py
# Compiled at: 2019-03-25 15:19:12
# Size of source mod 2**32: 550 bytes
from gwfhlang.parser import get_parser
from gwfhlang.compiler import Compiler
from arkhe.controller import Arkhe
from arkhe.debugger import ADB

def main(file, mode='normal'):
    parser = get_parser()
    compiler = Compiler()
    with open(file) as (f):
        content = f.read()
    code = compiler.transform(parser.parse(content))
    vm = Arkhe(code)
    if mode.lower().startswith('d'):
        adb = ADB()
        adb.vm = vm
        adb.run()
    else:
        vm.eval()

if __name__ == '__main__':
    import sys
    main(*sys.argv[1:])


import sys, os
sys.path.append(os.pardir)  # 부모 디렉터리의 파일을 가져올 수 있도록 설정
import numpy as np
import matplotlib.pyplot as plt
from mnist import load_mnist
from common.multi_layer_net import MultiLayerNet
from common.util import shuffle_dataset
from common.trainer import Trainer

"""
MNIST 데이터셋으로 검증셋을 이용하여 하이퍼파라미터의 최적화 구현
"""

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# 결과를 빠르게 얻기 위해 훈련 데이터를 줄임
x_train = x_train[:500]
t_train = t_train[:500]

#20%로 검증 데이터 분할
validation_rate = 0.2
validation_num = int(x_train.shape[0] * validation_rate)
x_train, t_train = shuffle_dataset(x_train, t_train)  #데이터셋을 무작위로 섞어준다.

x_val = x_train[:validation_num]
t_val = t_train[:validation_num]

x_train = x_train[validation_num:] #슬라이싱으로 검증 데이터셋 이후부터 훈련 데이터셋으로 사용
t_train = t_train[validation_num:]

def __train(lr, weight_decay, epocs = 50):
    #네트워크 생성
    network = MultiLayerNet(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100],
                            output_size= 10, weight_decay_lambda=weight_decay)

    #훈련 초기화
    trainer = Trainer(network,x_train,t_train,x_test,t_test,epocs,mini_batch_size=100,
                      optimizer='adam', optimizer_param={'lr':lr},verbose=False)

    #훈련 시작
    trainer.train()

    return trainer.test_acc_list, trainer.train_acc_list

#하이퍼 파라미터 무작위 탐색================================
optimization_trial = 100
results_val = {}
results_train = {}

for _ in range(optimization_trial):
    #탐색한 하이퍼 파라미터의 범위 지정========================
    weight_decay = 10 ** np.random.uniform(-8,4)
    lr = 10 ** np.random.uniform(-6,-2)
    #===================================================

    val_acc_list, train_acc_list = __train(lr,weight_decay)
    print("val acc:",str(val_acc_list[-1]),"| lr:",lr,", weight decay:",str(weight_decay))
    key = "lr:" + str(lr) + ", weight decay:" + str(weight_decay)
    results_val[key] = val_acc_list
    results_train[key] = train_acc_list

#그래프 그리기===============================================
print("=================Hyper-Parameter Optimization Result ===================")
graph_draw_num = 20
col_num = 5
row_num = int(np.ceil(graph_draw_num/ col_num))  #np.ceil은 올림 함수
i = 0

for key, val_acc_list in sorted(results_val.items(), key = lambda x:x[1][-1], reverse=True):
    print("Best-"+str(i+1)+"(val acc:"+str(val_acc_list[-1])+") | "+key)

    plt.subplot(row_num, col_num, i+1)
    plt.title("Best-"+str(i+1))
    plt.ylim(0.0,1.0)

    if i % 5:
        plt.yticks([])

    plt.xticks([])
    x = np.arange(len(val_acc_list))
    plt.plot(x, val_acc_list)
    plt.plot(x, results_train[key], "--")
    i += 1

    if i >= graph_draw_num:
        break

plt.show()


# -*- coding: utf-8 -*-
# Simple syslog logger

"""
@Time : 2018/11/27 17:28
@Author : Panjks-
@Description : Remote Syslog Output
"""

from __future__ import absolute_import, division

import logging
import logging.handlers
import json
import os

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse
import requests

import cowrie.core.output
from cowrie.core.config import CONFIG

class Output(cowrie.core.output.Output):

    def __init__(self):
        self.enabled = CONFIG.getboolean('output_malshare', 'enabled')
        self.host = CONFIG.get('output_remotesyslog', 'host')
        self.port = CONFIG.get('output_remotesyslog', 'port')
        self.server = CONFIG.get('output_remotesyslog', 'server')
        cowrie.core.output.Output.__init__(self)

    def start(self):
        self.logger = logging.getLogger()
        fh = logging.handlers.SysLogHandler((self.host, int(self.port)), logging.handlers.SysLogHandler.LOG_AUTH)
        formatter = logging.Formatter('%(message)s')
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)

    def stop(self):
        pass

    def write(self, logentry):
        for i in list(logentry.keys()):
            # remove twisted 15 legacy keys
            if i.startswith('log_'):
                del logentry[i]

        self.logger.error(json.dumps(logentry).rstrip('\t\r\n\0'))

        if logentry["eventid"] == "cowrie.session.file_download":
            print("Sending file to Server")
            p = urlparse(logentry["url"]).path
            if p == "":
                fileName = logentry["shasum"]
            else:
                b = os.path.basename(p)
                if b == "":
                    fileName = logentry["shasum"]
                else:
                    fileName = b

            self.postfile(logentry["outfile"], fileName)

        elif logentry["eventid"] == "cowrie.session.file_upload":
            print("Sending file to Server")
            self.postfile(logentry["outfile"], logentry["filename"])

    def postfile(self, artifact, fileName):
        """
        Send a file to Private server
        """
        if self.enabled:
            try:
                res = requests.post(
                    self.server,
                    files={fileName: open(artifact, "rb")}
                )
                if res and res.ok:
                    print("Submited to Server")
                else:
                    print("Server Request failed: {}".format(res.status_code))
            except Exception as e:
                print("Server Request failed: {}".format(e))
        return


#!/usr/bin/env python3
"""Day 12: JSAbacusFramework.io

Santa's Accounting-Elves need help balancing the books after a recent order.
Unfortunately, their accounting software uses a peculiar storage format.

They have a JSON document which contains a variety of things:
arrays (`[1,2,3]`), objects (`{"a":1, "b":2}`), numbers, and strings.
Your first job is to simply find all of the numbers throughout the document
and add them together.

Part 2:
The Accounting-Elves have realized that they double-counted everything red.
Ignore any object (and all of its children) which has any property
with the value "red".

"""
import json

def all_nums(data, ignore_reds=False):
    """Get all the integer values from `data`.

    If `ignore_reds` is set, any dicts containing the value 'red' are ignored.

    Args:
      data (loaded json structure)
      ignore_reds (bool)

    Yields:
      int: Integer values inside `data`.

    """
    if isinstance(data, int):
        yield data

    elif isinstance(data, list):
        for value in data:
            yield from all_nums(value, ignore_reds)

    elif isinstance(data, dict):
        if ignore_reds and 'red' in data.values():
            return
        for value in data.values():
                yield from all_nums(value, ignore_reds)

def main():
    import sys

    with open(sys.argv[1]) as f:
        data = json.load(f)

    print(sum(all_nums(data)))
    print(sum(all_nums(data, ignore_reds=True)))

if __name__ == '__main__':
    main()


import datetime
import json
from decimal import Decimal

from sodasql.scan.group_value import GroupValue

class JsonHelper:

    @staticmethod
    def to_json(o):
        return json.dumps(o)

    @staticmethod
    def to_json_pretty(o):
        return json.dumps(o, indent=2)

    @staticmethod
    def to_jsonnable(o):
        if o is None \
                or isinstance(o, str) \
                or isinstance(o, int) \
                or isinstance(o, float) \
                or isinstance(o, bool):
            return o
        if isinstance(o, dict):
            for key, value in o.items():
                update = False
                if not isinstance(key, str):
                    del o[key]
                    key = str(key)
                    update = True
                jsonnable_value = JsonHelper.to_jsonnable(value)
                if value is not jsonnable_value:
                    value = jsonnable_value
                    update = True
                if update:
                    o[key] = value
            return o
        if isinstance(o, list):
            for i in range(len(o)):
                element = o[i]
                jsonnable_element = JsonHelper.to_jsonnable(element)
                if element is not jsonnable_element:
                    o[i] = jsonnable_element
            return o
        if isinstance(o, Decimal):
            return float(o)
        if isinstance(o, datetime.datetime):
            return o.isoformat()
        if isinstance(o, datetime.date):
            return o.strftime('%Y-%m-%d')
        if isinstance(o, datetime.time):
            return o.strftime('%H:%M:%S')
        if isinstance(o, GroupValue):
            return o.to_json()
        raise RuntimeError(f"Don't know how to jsonize {o} ({type(o)})")


from typing import List, Tuple
from io import StringIO

def parse(puzzle) -> Tuple[int, List[int]]:
    """
    parse input text

    >>> parse(StringIO("123\\n1,2,x,3,x"))
    (123, [1, 2, 3])

    :param puzzle: file like object with input
    :return:
    """
    timestamp = int(puzzle.readline())
    bus_ids = [
        int(bus_id)
        for bus_id
        in puzzle.readline().split(',')
        if bus_id.isnumeric()
    ]
    return timestamp, bus_ids

def next_departure(timestamp: int, bus_ids: List[int]) -> Tuple[int, int]:
    """
    What is the ID of the earliest bus you can take to the airport
    multiplied by the number of minutes you'll need to wait for that bus?
         n*d        (n+1)*d
    ------|-----------|---------
               ^------+----^
               x      |   x+d
          | a  |   ?  |
    x: timestamp
    d: departure interval == bus id
    a: time since last departure: x % d
    ?: time to next departure: d - (x % d)
    """
    print('timestamp:', timestamp)
    print('bus id-s:', bus_ids)
    time_to_departures = [bus_id - (timestamp % bus_id) for bus_id in bus_ids]
    print('time to next departure:', time_to_departures)

    all_zero = [(timestamp + departure) % bus_id for bus_id, departure in zip(bus_ids, time_to_departures)]
    assert all(z == 0 for z in all_zero)

    bus_id, till_next_departure = min(zip(bus_ids, time_to_departures), key=lambda x: x[1])
    print('time to next departure', till_next_departure)
    print('bus', bus_id)
    return till_next_departure, bus_id

if __name__ == '__main__':
    import doctest
    doctest.testmod()

    with open('input.txt', 'rt') as puzzle:
        timestamp, bus_ids = parse(puzzle)

    time, bus_id = next_departure(timestamp, bus_ids)
    print('-'*10)
    print('departure time * bus id:', time * bus_id)


# -*- coding: utf-8 -*-
"""Module providing consultation appointment model"""
from lra.cos.interfaces import IConsultationAppointmentLocator, \
    IConsultationAppointmentGenerator
from sqlalchemy import Table, func
from sqlalchemy import schema as sqlalchemy_schema
from sqlalchemy import types as sqlalchemy_types
from z3c.saconfig import Session
from zope import schema
from zope.interface import Interface, implementer

from lra.cos import ORMBase
from lra.cos import _
from zope.sqlalchemy import register

metadata = ORMBase.metadata

class IConsultationAppointment(Interface):
    """ A consultation appointment """

    consultationAppointmentId = schema.Int(
        title=_(u"Consultation appointment identifier"),
    )

    consultationAppointmentCode = schema.TextLine(
        title=_(u"Consultation appointment code"),
    )

    consultationSlotCode = schema.TextLine(
        title=_(u"Consultation slot code"),
    )

    consultationAppointmentTimeStamp = schema.Datetime(
        title=_(u"Consultation appointment generated time stamp"),
    )

    privacy_notice = schema.Bool(
        title=_(u"Privacy notice accepted?"),
    )

    data_protection_notice = schema.Bool(
        title=_(u"Date protection notice accepted?"),
    )

    consultationAppointmentContactEmail = schema.TextLine(
        title=_(u"Consultation contact email"),
    )

    consultationAppointmentContactSalutation = schema.TextLine(
        title=_(u"Consultation contact salutation"),
    )

    consultationAppointmentContactFirstName = schema.TextLine(
        title=_(u"Consultation contact first name"),
    )

    consultationAppointmentContactLastName = schema.TextLine(
        title=_(u"Consultation contact last name"),
    )

    consultationAppointmentConstructionYear = schema.TextLine(
        title=_(u"Consultation appointment construction year"),
    )

    consultationAppointmentRequest = schema.Text(
        title=_(u"Consultation appointment request"),
    )

@implementer(IConsultationAppointment)
class ConsultationAppointment(ORMBase):
    """ Database-backed implementation of IConsultationAppointment """

    __tablename__ = 'consultation_appointment'

    consultationAppointmentId = sqlalchemy_schema.Column(
        sqlalchemy_types.Integer(),
        primary_key=True,
        autoincrement=True,
    )

    # Generated via secrets.token_hex
    consultationAppointmentCode = sqlalchemy_schema.Column(
        sqlalchemy_types.String(64),
        nullable=False,
    )

    # Generated via secrets.token_hex
    consultationSlotCode = sqlalchemy_schema.Column(
        sqlalchemy_types.String(64),
        nullable=False,
    )

    consultationAppointmentTimeStamp = sqlalchemy_schema.Column(
        sqlalchemy_types.DateTime(),
        nullable=False,
        server_default=func.now()
    )

    privacy_notice = sqlalchemy_schema.Column(
        sqlalchemy_types.Boolean(),
        nullable=False,
    )

    data_protection_notice = sqlalchemy_schema.Column(
        sqlalchemy_types.Boolean(),
        nullable=False,
    )

    consultationAppointmentContactEmail = sqlalchemy_schema.Column(
        sqlalchemy_types.String(120),
        nullable=False,
    )

    consultationAppointmentContactSalutation = sqlalchemy_schema.Column(
        sqlalchemy_types.String(120),
        nullable=False,
    )

    consultationAppointmentContactFirstName = sqlalchemy_schema.Column(
        sqlalchemy_types.String(120),
        nullable=False,
    )

    consultationAppointmentContactLastName = sqlalchemy_schema.Column(
        sqlalchemy_types.String(120),
        nullable=False,
    )

    consultationAppointmentConstructionYear = sqlalchemy_schema.Column(
        sqlalchemy_types.String(64),
        nullable=False,
    )

    consultationAppointmentRequest = sqlalchemy_schema.Column(
        sqlalchemy_types.Text(),
        nullable=False,
    )

#consultation_appointment = Table('consultation_appointment', metadata)

@implementer(IConsultationAppointmentLocator)
class ConsultationAppointmentLocator(object):
    """ Utility to locate available consultation slots """

    @staticmethod
    def available_appointments(from_date):
        """Return a list of all appointments for particular time slots
        on the specified dates.

        Returns a list of dictionaries with keys 'timeSlotCode', 'appointmentCode',
        and 'email'.
        """

        results = Session.query(ConsultationAppointment).filter(
            ConsultationAppointment.showTime.after(from_date)
        )

        appointments = [dict(appointment_id=row.consutationAppointmentId,
                      consultation_slot_code=row.consultationSlotCode,
                      appointment_code=row.consultationAppointmentCode,
                      appointment_email=row.consultationAppointmentContactEmail
                      )
                 for row in results]
        return appointments

@implementer(IConsultationAppointmentGenerator)
class ConsultationAppointmentGenerator(object):
    """ Utility to generate new consultation slots """

    def __call__(self, appointment_request):
        """Make a consultation appointment
        """

        # Make sure the time slot is still available
        # TODO: check for data validity
        session = Session()
        register(session)
        session().add(appointment_request)


# uncompyle6 version 3.7.4
# Python bytecode 3.5 (3350)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/trjg/code/venv/django-geo-db/lib/python3.5/site-packages/django_geo_db/migrations/0007_auto_20180310_0603.py
# Compiled at: 2018-03-10 20:33:06
# Size of source mod 2**32: 1893 bytes
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion, django_geo_db.models

class Migration(migrations.Migration):
    dependencies = [
     ('django_geo_db', '0006_auto_20180228_1147')]
    operations = [
     migrations.CreateModel(name='LocationBounds', fields=[
      (
       'location_bounds_id', models.AutoField(primary_key=True, serialize=False)),
      (
       'max_lat', models.DecimalField(decimal_places=6, max_digits=8)),
      (
       'min_lat', models.DecimalField(decimal_places=6, max_digits=8)),
      (
       'max_lon', models.DecimalField(decimal_places=6, max_digits=9)),
      (
       'min_lon', models.DecimalField(decimal_places=6, max_digits=9)),
      (
       'location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_geo_db.Location'))]),
     migrations.CreateModel(name='LocationMap', fields=[
      (
       'location_map_id', models.AutoField(primary_key=True, serialize=False)),
      (
       'file', models.FileField(upload_to='')),
      (
       'location', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_geo_db.Location'))]),
     migrations.CreateModel(name='LocationMapType', fields=[
      (
       'location_map_type_id', models.AutoField(primary_key=True, serialize=False)),
      (
       'type', models.CharField(max_length=30))]),
     migrations.AddField(model_name='locationmap', name='type', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='django_geo_db.LocationMapType'))]