from itertools import compress
from copy import deepcopy

'''
snp2lib will hold any function that might be useful later on (not just for dealing with the static dump)
for now, it's mainly for getInfo, which will deal with customers, city improvements, etc. found in a difference check
'''

# this is the base url for getting images and such from edgebee
# along with a type version because it's easier
cdnBase = "cdn.edgebee.com/static/"
cndBase = cdnBase

ctimeConverter = {0:'none', 1:'Very Fast', 2:'Fast', 3:'Medium', 4:'Long',  5:'Very Long'}

# I found this solution on stack exchange, to pass a mask as an argument to a list to select which entries to return
class maskList(list):
    def __getitem__(self, index):
        try: return super(maskList, self).__getitem__(index)
        except TypeError: return maskList(compress(self, index))

spacer = '  '
def traverseDict(theDict, theFile, ntabs=0):
    # traverseDict will determine and output the full structure of a json object
    if type(theDict) is dict:
        # if it's a dict, go through each key, record it in theFile, and pass the next thing down
        for key in theDict:
            theFile.write(spacer*ntabs + key + '\n')
            traverseDict(theDict[key], theFile, ntabs+1)
    elif type(theDict) is list:
        # if it's a list, record that it's a list
        theFile.write(spacer*ntabs + 'list of stuff with keys:' + '\n')
        if len(theDict) > 0:
            # if there's stuff in the list, pass each thing down
            traverseDict(theDict[0], theFile, ntabs+1)
    else:
        # record the type of variable it finds
        theFile.write(spacer*ntabs + str(type(theDict)) + '\n')
    return

def findAssetRefs(theDict, target, keysTrail, trackList):
    if type(theDict) is dict:
        for key in theDict.keys():
            keysTrail.append(key)
            findAssetRefs(theDict[key], target, keysTrail, trackList)
            del keysTrail[-1]
    elif type(theDict) is list:
        i=0
        for thing in theDict:
            keysTrail.append(i)
            findAssetRefs(thing, target, keysTrail, trackList)
            del keysTrail[-1]
            i += 1
    else:
        if type(theDict) is str or type(theDict) is unicode:
            if str(target) in theDict:
                trackList.append(deepcopy(keysTrail))
        else:
            if theDict == target:
                trackList.append(deepcopy(keysTrail))
    return

def getCats(iMask):
    # item types are stored in a bit of a strange way: as a bitmask corresponding to the category list:
    catList = maskList( ['swords', 'axes', 'maces', 'spears', 'daggers', 'staves', 'bows', 'music', 'thrown', 'guns',
                         'heavy armor', 'armor', 'clothes', 'heavy helmets', 'helmets', 'hats', 'gauntlets', 'gloves',
                         'bracers', 'heavy boots', 'boots', 'shoes', 'shields', '', 'potions', 'herbs', 'scrolls',
                         'rings', 'amulets', 'rare resources'] )
    # the items_mask value must be converted to a binary number, which, when using bin(), actually makes it a string for
    # some reason, with '0b' at the beginning, so we use element 2 to the end to only get the actual number
    # then, we need to reverse the number; as stored, the 2^0 bit corresponds to swords, so we want that to always
    # be the first digit
    # then just convert each thing to int (since at that point they're all still characters), and save it to mask
    mask = [ int(y) for y in reversed([x for x in bin(iMask)][2:]) ]
    # apply the mask to the category list and return it
    return catList[mask]

def prCustomer(modType, cust, theFile):
    theFile.write('{}\n{} ({}), color: {} (id:{})\n'.format(modType, cust['name'], cust['class'], cust['color'], cust['id']))
    #theFile.write(modType + '\n' + cust['name'] + ' (' + cust['class'] + '), color: ' + cust['color'] + ' (id:' + str(cust['id']) + ')\n')
    theFile.write('Level range: ' + str(cust['startLvl']) + '-' + str(cust['maxLvl']) + '\n')
    theFile.write('Favorite item types: ' + ', '.join(cust['iTypes']) + '\n')
    theFile.write('Fame required: ' + str(cust['lvlReq']) + '\nAppeal required: ' + str(cust['appealReq']) + '\n')
    theFile.write('Unlocked by: ')
    if len(cust['unlockedBy']) > 0:
        theFile.write(' '.join(str(x) for x in cust['unlockedBy']))
    else:
        theFile.write(' -')
    theFile.write('\n'*2)

def prHunt(modType, cust, theFile):
    pass

def prImprovement(modType, cust, theFile):
    pass

def prFameLevel(modType, cust, theFile):
    pass

def prCustLevelValue(modType, cust, theFile):
    pass

def prRecUnlock(modType, cust, theFile):
    pass

def prAsset(modType, cust, theFile):
    pass

def prItem(modType, cust, theFile):
    pass

def prRecipe(modType, cust, theFile):
    pass

def prModule(modType, cust, theFile):
    pass

def prQuest(modType, cust, theFile):
    pass

def prWorker(modType, cust, theFile):
    pass

def prAchievement(modType, cust, theFile):
    pass

def prCharClass(modType, cust, theFile):
    pass

def prInfo(printObj=0, outpFile=0):
    printInfoDict = {'customers':prCustomer, 'hunts':prHunt, 'improvements':prImprovement, 'fame_levels':prFameLevel,
                     'customer_level_values':prCustLevelValue, 'recipe_unlocks':prRecUnlock, 'assets':prAsset,
                     'items':prItem, 'recipes':prRecipe, 'modules':prModule, 'quests':prQuest, 'workers':prWorker,
                     'achievements':prAchievement, 'character_classes':prCharClass}
    if printObj == 0: return printInfoDict.keys()

    prType = printObj[0]
    printInfoDict[prType](printObj[1], printObj[2], outpFile)

def getInfo(change=0, newSD=0):
    # getInfo is the master function to determine the type of change in the static dump and all the info about it
    # currently works for: customers (sans color), quests
    # maybe change it later so each case is a separate function and store them in a dict? who knows

    # default return when given no arguments is a list of change types accepted
    if change == 0:
        return ['customers', 'hunts', 'improvements', 'fame_levels', 'customer_level_values', 'recipe_unlocks',
                'assets', 'items', 'recipes', 'modules', 'quests', 'workers', 'achievements', 'character_classes']

    # if the main dict was passed as newSD, bypass that silly 'result' key
    if 'result' in newSD:
        newSD = newSD['result']

    # putting the assets in their own place just saves typing, as nearly every section needs to search through it
    assets = newSD['assets']

    # get the type of change
    changeType = change[0]
    if changeType == 'customers':
        ''' example customer:
        {u'character_class_id': 35, u'uid': u'5ac120fe88819387bfbab3b1840d2611', u'min_fame': 65,
        u'simple_class': u'knight', u'max_level': 20, u'__type__': u'Customer', u'color': 2236962, u'min_appeal': 10,
        u'starting_level': 15, u'name_id': 27710, u'improvement_id': 52, u'id': 161, u'items_mask': 4268040}
        '''
        # change[2] stores the actual changed object
        newCust = change[2]
        # set up the output customer, get the data that's immediately available: start and max level, level requirement
        # to unlock, and appeal requirement to unlock (and color, which is a hex code stored in dec)
        outputCust = {'name':'', 'startLvl':newCust['starting_level'], 'maxLvl':newCust['max_level'],
                      'class':'', 'lvlReq':newCust['min_fame'], 'appealReq':newCust['min_appeal'],
                      'iTypes':[], 'unlockedBy':[], 'color':hex(newCust['color']), 'id':newCust['id']}
        # the IDs here are used to get the respective names
        classID = newCust['character_class_id']
        nameID = newCust['name_id']
        imprID = newCust['improvement_id']
        # imask is a bitmask that determines a customer's buy list when applied to catList
        iMask = newCust['items_mask']
        # go through the character classes and improvements to get the name ids for the class of and improvement to
        # unlock the customer, plus the level of the improvement
        for cclass in newSD['character_classes']:
            if cclass['id'] == classID:
                classNameID = cclass['name_id']
        for impr in newSD['improvements']:
            if impr['id'] == imprID:
                imprNameID = impr['name_id']
                imprLevel = impr['level']
        # then go through the assets, which is apparently where all text is stored (there's probly a good reason, I
        # dunno) and find each id, getting the 'value' of its asset object (where the text is stored) and storing it
        for asset in assets:
            chkID = asset['id']
            if chkID == imprNameID:
                imprName = asset['value']
            elif chkID == nameID:
                outputCust['name'] = asset['value']
            elif chkID == classNameID:
                className = asset['value']
        # update the output customer
        outputCust['unlockedBy'] = [imprName, imprLevel]
        outputCust['class'] = className
        outputCust['iTypes'] = getCats(iMask)
        # return change[1] (whether it's an add, removal, or change) and the new customer
        return [change[1], outputCust]

    elif changeType == 'hunts':
        ''' example quest:
        {u'min_level': 20, u'min_value': 200000, u'unlock_fame_level': 90, u'__type__': u'Hunt', u'max_level': 21,
        u'max_value': 4000000.0, u'image': u'shopr2/quests/dungeon_27.png', u'rewards_mask': 0, u'loots': [{u'index': 0,
        u'resource_id': 0, u'__type__': u'HuntLoot', u'value': 0, u'amount': 1, u'item_id': 563, u'id': 82},
        {u'index': 1, u'resource_id': 0, u'__type__': u'HuntLoot', u'value': 0, u'amount': 2, u'item_id': 563,
        u'id': 83}, {u'index': 2, u'resource_id': 0, u'__type__': u'HuntLoot', u'value': 0, u'amount': 3,
        u'item_id': 563, u'id': 84}], u'npc': u'npc_ranger', u'description_id': 27732, u'name_id': 27731,
        u'quest_id': 0, u'customer_xp_reward': 7467, u'duration': 230400, u'improvement_id': 0, u'id': 28}
        '''
        # grab the new quest
        newHunt = change[2]
        # fill out the easy stuff: min and max level, time (convert to hours), level required, xp/loot, value for 100%
        # common reward (which for some reason is stored in min_value and needs to be doubled, or max_value divided by
        # 10), a link to the quest icon, and empty loot entries, stored as [amount, item/resource/'gold']
        outputQuest = {'name':'', 'minLvl':newHunt['min_level'], 'maxLvl':newHunt['max_level'],
                       'time':newHunt['duration']/3600, 'shopLvlReq':newHunt['unlock_fame_level'],
                       'loots':[[0,''], [0,''], [0,'']], 'xpPerLoot':newHunt['customer_xp_reward'],
                       'comValue':newHunt['min_value']*2, 'description':'', 'picLink':cdnBase+newHunt['image'],
                       'id':newHunt['id']}
        # name and description id are simply there, grab them
        nameID = newHunt['name_id']
        descID = newHunt['description_id']
        for i in xrange(len(newHunt['loots'])):
            # while the names for the loots are a bit more complicated
            # each loot can be an item, a resource, or gold; if either of the item or resource ids of the loot are
            # non-zero, that is the type of loot it is (they can't both be non-zero), otherwise it's gold
            if newHunt['loots'][i]['item_id'] > 0:
                # if it's an item or resource, set its id to the appropriate thing from newHunt, and set the key to
                # either 'items' or 'resources'; these are to be used to find the name of the loot in the right place
                thingID = newHunt['loots'][i]['item_id']
                searchKey = 'items'
            elif newHunt['loots'][i]['resource_id'] > 0:
                thingID = newHunt['loots'][i]['resource_id']
                searchKey = 'resources'
            else:
                # if it's gold, we don't want to search the assets or anything, so just set the loot entry to [amout,
                # 'gold'] and use continue to skip the rest
                outputQuest['loots'][i][1] = "Gold"
                outputQuest['loots'][i][0] = newHunt['loots'][i]['amount']
                continue
            # if it's an item or resource, use the id to find the name id in the respective location
            for thing in newSD[searchKey]:
                if thing['id'] == thingID:
                    thingNameID = thing['name_id']
            # then find the text in assets and set the loot's name
            for asset in assets:
                if asset['id'] == thingNameID:
                    outputQuest['loots'][i][1] = asset['value']
            # finally set the loot's amount
            outputQuest['loots'][i][0] = newHunt['loots'][i]['amount']
        # now go through assets and get the text for the quest name and description
        for asset in assets:
            chkID = asset['id']
            if chkID == nameID:
                outputQuest['name'] = asset['value']
            elif chkID == descID:
                outputQuest['description'] = asset['value']
        # and return the change status and new quest
        return [change[1], outputQuest]

    elif changeType == 'improvements':
        ''' example improvement:
        {u'index': 106, u'children_ids': [358], u'resource': None, u'uid': u'9dcb759b22b211eaeafb0b7a9ae7e9cb',
        u'level': 3, u'description_id': 0, u'image': u'106_1830x138_blue_mercs', u'parents_ids': [356],
        u'__type__': u'Improvement', u'flags': 2, u'build_time': 172800, u'building_modifier': u'', u'name_id': 27730,
        u'x': 1830, u'y': 138, u'requirements': [{u'index': 0, u'customer_xp_reward': 0,
                                                 u'__type__': u'ImprovementRequirement', u'character_codename': u'bard',
                                                 u'amount': 200, u'item_id': 0, u'improvement_id': 357, u'id': 296}],
        u'codename': u'spellcasters_guild', u'id': 357,
        u'modifier_unlock': {u'min_shop_appeal': 0, u'modifier': {u'while_building': False, u'is_global': True,
        u'__type__': u'Modifier', u'add': 400.0, u'id': 431, u'iso_y': 0.0, u'iso_x': 0.0, u'radius': 0.0,
        u'hidden': False, u'overwrites': False, u'modifies': u'klash_merc_max.spellcasters', u'mult': 0.0},
        u'__type__': u'Modifier', u'id': 231}}
        '''
        # get the new improvement (heh)
        newBuilding = change[2]
        allBuildings = newSD['improvements']
        # fill out the easy stuff
        # what we want to know, probly: name, level, how to unlock, build time, customers/stuff that's unlocked,
        # previous building, type of building (maybe), description maybe? if id is nonzero
        outputBuilding = {'name':'', 'level':newBuilding['level'], 'time':newBuilding['build_time']/3600,
                          'buildUnlocks':[], 'custUnlocks':[], 'bonus':[], 'upgradeCost':[], 'description':'',
                          'unlockedBy':[], 'picLink':cdnBase+'shopr2/city/improvements/'+newBuilding['image']+'.png',
                          'id':newBuilding['id']}
        # get the easy test from assets
        nameID = newBuilding['name_id']
        descID = newBuilding['description_id']
        for asset in assets:
            if asset['id'] == nameID: outputBuilding['name'] = asset['value']
            elif asset['id'] == descID: outputBuilding['description'] = asset['value']
        # first, find the building that unlocks this one, if it exists
        if len(newBuilding['parents_ids']) > 0:
            for impr in allBuildings:
                if impr['id'] == newBuilding['parents_ids'][0]:
                    for asset in assets:
                        if asset['id'] == impr['name_id']: outputBuilding['unlockedBy'] = [asset['value'],
                                                                                impr['level']]
        else: outputBuilding['unlockedBy'] = 'initially available or unlocked by VP'
        # get the requirements, if they exist
        if len(newBuilding['requirements']) > 0:
            # put the dict in reqDict, makes things easier
            reqDict = newBuilding['requirements'][0]
            # get the amount, easy
            amt = reqDict['amount']
            # then there are 3 cases for the type of thing, an item, a customer, or gold
            # only one of item_id and character_codename can be nonzero or non-None, respectively
            if reqDict['item_id'] != 0:
                # if item_id is nonzero, the type of thing is an item; get the name id, then the name from assets
                for item in newSD['items']:
                    if item['id'] == reqDict['item_id']: iNameID = item['name_id']
                for asset in assets:
                    if asset['id'] == iNameID: thing = asset['value']
            elif reqDict['character_codename'] is None: thing = 'gold'
            # if item_id is 0 and character_codename is None, amt is the amount of gold
            # admittedly the order of the logic is odd here, but I just prefer to do 'is None' rather than 'is not None'
            else: thing = reqDict['character_codename']
            # otherwise character_codename isn't blank, and amt is the amount of a type of customer to send
            # put the amount and the thing into the upgrade_cost list
            outputBuilding['upgradeCost'] = [amt, thing]
        # get the building unlocks, if there are any
        if len(newBuilding['children_ids']) > 0:
            for cid in newBuilding['children_ids']:
                for impr in allBuildings:
                    if impr['id'] == cid and newBuilding['codename'] != impr['codename']:
                        for asset in assets:
                            if asset['id'] == impr['name_id']: imprName = asset['value']
                        outputBuilding['buildUnlocks'].append([imprName, impr['level']])
        else: outputBuilding['buildUnlocks'] = 'none'
        # get customer unlocks, if any
        # this requires going through the customers section, since no building actually stores info about a customer it
        # unlocks
        custUnlcks = []
        # go through every customer
        for cust in newSD['customers']:
            # if the improvement that unlocks the customer matches the id of the new building:
            if cust['improvement_id'] == newBuilding['id']:
                # get the name ids for the customer and their class (class name id is retrieved from the character
                # classes section), then go through assets to get the actual text
                custNameID = cust['name_id']
                custClassID = cust['character_class_id']
                for cclass in newSD['character_classes']:
                    if cclass['id'] == custClassID: custClassNameID = cclass['name_id']
                for asset in assets:
                    if asset['id'] == custNameID: custName = asset['value']
                    elif asset['id'] == custClassNameID: custClassName = asset['value']
                # add the name and class to the list
                custUnlcks.append([custName, custClassName])
        # if any classes were found, set the custUnlocks key to the custUnlcks list, otherwise set it to 'none'
        if len(custUnlcks) > 0: outputBuilding['custUnlocks'] = custUnlcks
        else: outputBuilding['custUnlocks'] = 'none'
        # finally, get the bonus the building provides, if any
        if type(newBuilding['modifier_unlock']) is dict:
            # the thing the building modifies will always be there, in modifier -> modifies
            what = newBuilding['modifier_unlock']['modifier']['modifies']
            # if the key 'add' is present, it contains how much of 'what' the building provides
            if 'add' in newBuilding['modifier_unlock']['modifier']:
                howMuch = newBuilding['modifier_unlock']['modifier']['add']
            # if 'add' isn't there, the how much is stored in 'mult', which is only the case for the monastery
            # so gg there edgebee, way to be consistent
            else: howMuch = newBuilding['modifier_unlock']['modifier']['mult']
            outputBuilding['bonus'] = [what, howMuch]
        # if there's no 'modifier_unlock', set the 'bonus' to 'none'
        else: outputBuilding['bonus'] = 'none'
        return [change[1], outputBuilding]

    elif changeType == 'customer_levels':
        pass
    elif changeType == 'fame_levels':
        newShopLevel = change[2]
        outputShopLevel = {'level':newSD[changeType].index(newShopLevel)+1, 'xpNeeded':newShopLevel}
        return [change[1], outputShopLevel]

    elif changeType == 'customer_level_values':
        # very simple section, the new/old/changed level value is the number given
        newCustLevelValue = change[2]
        outputLevel = {'value':newCustLevelValue}
        # if it's an addition, the new level is the length of the list, minus 1
        if change[1] == 'add': outputLevel['level'] = len(newSD[changeType])-1
        # if it's a removal, the old level was the length of the list
        elif change[1] == 'rem': outputLevel['level'] = len(newSD[changeType])
        # if it's a change, find the index with the new value, that's the level (since it has that weird 0 at the start)
        else: outputLevel['level'] = newSD[changeType].index(newCustLevelValue)
        return [change[1], outputLevel]

    elif changeType == 'recipe_unlocks':
        ''' example recipe unlock:
        {u'worker_codename': None, u'uid': u'8ec9d260ca17c4d0ea598e5e04f69c70', u'fame_level': 0,
        u'__type__': u'RecipeUnlock', u'recipe_id': 484, u'crafted_item_id': 483, u'quest_id': 0,
        u'crafted_item_count': 48, u'module_level': 0, u'module_id': 0, u'id': 443}
        '''
        # grab the recipe unlock
        newRecUnlock = change[2]
        # fill out the easy stuff
        outputRecUnlock = {'itemToCraft':[], 'itemUnlocked':'', 'id':newRecUnlock['id']}
        # if the item has a crafted item count of 0, it's a starting recipe or it comes with a worker
        # so for nonzero ones:
        if newRecUnlock['crafted_item_count'] != 0:
            for item in newSD['items']:
                if item['id'] == newRecUnlock['recipe_id']: iUnameID = item['name_id']
                elif item['id'] == newRecUnlock['crafted_item_id']: cInameID = item['name_id']
            for asset in assets:
                if asset['id'] == iUnameID: outputRecUnlock['itemUnlocked'] = asset['value']
                elif asset['id'] == cInameID: outputRecUnlock['itemToCraft'] = [asset['value'],
                                                                                newRecUnlock['crafted_item_count']]
        return [change[1], outputRecUnlock]

    elif changeType == 'assets':
        # assets are just text storage, and the only thing to know about them is where they're referenced
        newAsset = change[2]
        outputAsset = {'text':newAsset['value'], 'id':newAsset['id'], 'locationsUsed':[]}
        findAssetRefs(newSD, newAsset['id'], ['result'], outputAsset['locationsUsed'])
        return [change[1], outputAsset]

    elif changeType == 'items':
        ''' example item:
        {u'hue': 0, u'uid': u'cf7201e5909e9104ab35ac4ecd924e27', u'purchase_cost': 21, u'level': 21,
        u'image': u'shopr2/items/US_genesiscodex.png', u'__type__': u'Item', u'repair_cost': 5, u'id': 556,
        u'price': 2100000, u'name_id': 27720, u'craft_xp': 294002, u'type_str': u'US', u'sell_xp': 1176008,
        u'type': 67108864, u'purchase_price': 1050000, u'unlock_cost': 21}
        '''
        # grab the new item
        newItem = change[2]
        # fill out the easy stuff, all but the name and type
        outputItem = {'name':'', 'value':newItem['price'], 'sellXP':newItem['sell_xp'], 'craftXP':newItem['craft_xp'],
                      'type':'', 'level':newItem['level'], 'repairCost':newItem['repair_cost'],
                      'picLink':cdnBase+newItem['image'], 'id':newItem['id']}
        # the usual, grab the name id
        nameID = newItem['name_id']
        # and find it in assets
        for asset in assets:
            if asset['id'] == nameID:
                outputItem['name'] = asset['value']
        # the type for items is stored as a bitmask, just like customers, though it'll always be a single type of course
        # see the getCats function for an explanation of how/why this works
        iType = getCats(newItem['type'])
        # this length check is because rare resources are in 'items', and they won't get a type from the mask
        if len(iType) > 0:
            outputItem['type'] = iType[0]
        else:
            outputItem['type'] = 'rare resource'
        # return the change status and the new item
        return [change[1], outputItem]

    elif changeType == 'recipes':
        ''' example recipe:
        {u'worker_codename': u'druid', u'uid': u'80de6bf22b9f3563efa4fa06a71d2469', u'crafting_time': 5,
        u'__type__': u'Recipe', u'module_level': 6, u'components': [{u'index': 1, u'resource_id': 0,
        u'__type__': u'RecipeComponent', u'recipe_id': 561, u'item_id': 454, u'id': 1884, u'quantity': 1},
        {u'index': 2, u'resource_id': 0, u'__type__': u'RecipeComponent', u'recipe_id': 561, u'item_id': 451,
        u'id': 1885, u'quantity': 2}, {u'index': 3, u'resource_id': 0, u'__type__': u'RecipeComponent',
        u'recipe_id': 561, u'item_id': 563, u'id': 1886, u'quantity': 1}, {u'index': 4, u'resource_id': 15,
        u'__type__': u'RecipeComponent', u'recipe_id': 561, u'item_id': 0, u'id': 1887, u'quantity': 10}],
        u'item_id': 561, u'module_id': 54, u'id': 561}
        '''
        # grab the new recipe
        newRecipe = change[2]
        allItems = newSD['items']
        # fill out the easy stuff
        outputRecipe = {'name':'', 'madeBy':'', 'craftTime':ctimeConverter[newRecipe['crafting_time']], 'madeOn':[],
                        'ingredients':[], 'type':'', 'id':newRecipe['id']}
        # get the item name and type from the items section
        for item in allItems:
            if item['id'] == newRecipe['id']:
                iNameID = item['name_id']
                iTypeNum = item['type']
        for asset in assets:
            if asset['id'] == iNameID: outputRecipe['name'] = asset['value']
        # since it's a specific item, there will only be one result from the type list, so just grab that
        outputRecipe['type'] = getCats(iTypeNum)[0]
        # if it's not a rare resource (only rare resources have a 0 for module_id);
        if newRecipe['module_id'] != 0:
            # go through classes to get the name for madeBy
            for cclass in newSD['character_classes']:
                if cclass['codename'] == newRecipe['worker_codename']:
                    clNameID = cclass['name_id']
            # go through modules to the the name and level for madeOn
            for module in newSD['modules']:
                if module['id'] == newRecipe['module_id']:
                    modNameID = module['name_id']
                    modLevel = module['power']
            # then get the names from assets
            for asset in assets:
                if asset['id'] == clNameID: outputRecipe['madeBy'] = asset['value']
                elif asset['id'] == modNameID: outputRecipe['madeOn'] = [asset['value'], newRecipe['module_level']]
            # finally, get the ingredients
            for thing in newRecipe['components']:
                # need the name and amount; amount is easy, so get the name first
                # exactly one of item_id and resource_id will be nonzero, which determines the type
                if thing['item_id'] != 0:
                    for item in allItems:
                        if item['id'] == thing['item_id']: thingNameID = item['name_id']
                else:
                    for res in newSD['resources']:
                        if res['id'] == thing['resource_id']: thingNameID = res['name_id']
                for asset in assets:
                    if asset['id'] == thingNameID: outputRecipe['ingredients'].append([thing['quantity'], asset['value']])
        # otherwise, set madeBy and madeOn to 'none', as well as 'ingredients'
        else:
            outputRecipe['madeBy'] = 'none'
            outputRecipe['madeOn'] = 'none'
            outputRecipe['ingredients'] = 'none'
        return [change[1], outputRecipe]

    elif changeType == 'modules':
        ''' example module:
        {u'uid': u'7a24f807012e39da720f8a56ed1de55a', u'disabled': False, u'resource_codename': u'raw_mithril',
        u'id': 150, u'max_upgrade_level': 5, u'resource_modifiers': [{u'while_building': False, u'is_global': True,
        u'__type__': u'Modifier', u'add': 17.0, u'id': 0, u'iso_y': 0.0, u'iso_x': 0.0, u'radius': 0.0,
        u'hidden': False, u'overwrites': False, u'modifies': u'resource.max_count.raw_mithril', u'mult': 0.0},
        {u'while_building': False, u'is_global': True, u'__type__': u'Modifier', u'add': 18.0, u'id': 0, u'iso_y': 0.0,
        u'iso_x': 0.0, u'radius': 0.0, u'hidden': False, u'overwrites': False,
        u'modifies': u'resource.max_count.raw_mithril', u'mult': 0.0}, {u'while_building': False, u'is_global': True,
        u'__type__': u'Modifier', u'add': 19.0, u'id': 0, u'iso_y': 0.0, u'iso_x': 0.0, u'radius': 0.0,
        u'hidden': False, u'overwrites': False, u'modifies': u'resource.max_count.raw_mithril', u'mult': 0.0},
        {u'while_building': False, u'is_global': True, u'__type__': u'Modifier', u'add': 20.0, u'id': 0, u'iso_y': 0.0,
        u'iso_x': 0.0, u'radius': 0.0, u'hidden': False, u'overwrites': False,
        u'modifies': u'resource.max_count.raw_mithril', u'mult': 0.0}, {u'while_building': False, u'is_global': True,
        u'__type__': u'Modifier', u'add': 22.0, u'id': 0, u'iso_y': 0.0, u'iso_x': 0.0, u'radius': 0.0,
        u'hidden': False, u'overwrites': False, u'modifies': u'resource.max_count.raw_mithril', u'mult': 0.0}],
        u'__type__': u'Module', u'parent_id': 45, u'astar_nodes': [], u'name_id': 26679, u'codename': u'bin_mithril',
        u'icon': u'shopr2/shop/icons/magic_bin_mithril_t.png', u'type': 512, u'power': 4, u'pois': [],
        u'scene_objects': [{u'image_path': u'magic_bin_mithril', u'nb_automated_frames': 0, u'edition_type': 5,
        u'id': 150, u'__type__': u'SceneObject', u'iso_y_offset': 0, u'iso_x_offset': 0, u'animation': None,
        u'y_offset': -115, u'iso_height': 2, u'iso_width': 2, u'x_offset': -40, u'module_id': 150, u'children': [],
        u'animation_speed': 1.0}], u'shop_appeals': None, u'modifiers': [], u'unlock_fame_level': 1, u'maximum': 3,
        u'costs': [-1, -1, -1, -1, -1], u'build_times': [345600, 432000, 864000, 1080000, 1296000], u'hammer_cost': 250}
        '''
        # get the new module
        newModule = change[2]
        # set the easy stuff first
        outputModule = {'name':'', 'tier':newModule['power'], 'goldCosts':newModule['costs'],
                        'hammerCost':newModule['hammer_cost'], 'times':newModule['build_times'],
                        'maxBuyable':newModule['maximum'], 'levelReq':newModule['unlock_fame_level'],
                        'appeals':newModule['shop_appeals'], 'unlockedBy':[], 'bonuses':[],
                        'picLink':cdnBase+newModule['icon'], 'id':newModule['id']}
        # get the name
        for asset in assets:
            if asset['id'] == newModule['name_id']: outputModule['name'] = asset['value']
        # get parent module name, if there is one (otherwise unlcokedBy = 'none')
        if newModule['parent_id'] != 0:
            # go through all the modules
            for mod in newSD['modules']:
                # find the one that matches the parent id
                if mod['id'] == newModule['parent_id']:
                    # get teh name id
                    pmodNameID = mod['name_id']
                    # and get the name from assets
                    for asset in assets:
                        if asset['id'] == pmodNameID: outputModule['unlockedBy'] = [asset['value'], mod['power']]
        else: outputModule['unlockedBy'] = 'none'
        # grab all the bonuses; only one of 'modifiers' and 'resource_modifiers' will have anything in it; if they're
        # both empty, no 'bonuses' are provided
        if len(newModule['resource_modifiers']) > 0:
            # for 'resource_modifiers', the capacity for each level is stored in a separate 'resource_modifier' object,
            # and the resource it modifies is stored in each one in 'modifies'
            # so just grab one of the 'modifies'
            what = newModule['resource_modifiers'][0]['modifies']
            # and go through each modifier and get 'add', and put it in a list
            bonusList = [x['add'] for x in newModule['resource_modifiers']]
            # and set bonuses to [what, thatList]
            outputModule['bonuses'] = [what, bonusList]
        elif len(newModule['modifiers']) > 0:
            # for 'modifiers', 'add' is the base bonus that tier provides, and each level of the module (including the
            # first) adds the value in 'add_level'
            # the what is still stored in 'modifies'
            # so for each modifier, which provides a different bonus:
            for mod in newModule['modifiers']:
                # some modifiers don't have 'add' and some don't have 'base', but the end formula is still the same
                # so just check for each and set them
                if 'add' in mod: base = mod['add']
                else: base = 0
                if 'add_level' in mod: add = mod['add_level']
                else: add = 0
                what = mod['modifies']
                bonusList = []
                # now, we want a list of the bonus at each level of this module, which is base + add*level
                for i in xrange(newModule['max_upgrade_level']):
                    bonusList.append(base+add*(i+1))
                # append the what and bonusList to the 'bonuses' key for each modifier present
                outputModule['bonuses'].append([what, bonusList])
        return [change[1], outputModule]

    elif changeType == 'quests':
        '''
        {u'worker_codename': None, u'party_members': [{u'index': 0, u'codename': u'archer',
        u'__type__': u'QuestPartyMember'}, {u'index': 1, u'codename': u'soldier', u'__type__': u'QuestPartyMember'},
        {u'index': 2, u'codename': u'archer', u'__type__': u'QuestPartyMember'}], u'customer_xp_reward': 912,
        u'npc_intro_id': 25611, u'__type__': u'Quest', u'npc': u'npc_ranger', u'id': 17, u'parent_id': 16,
        u'party_items': [{u'item_id': 300, u'index': 0, u'__type__': u'QuestPartyItem', u'item_type_mask': 0},
        {u'item_id': 7, u'index': 1, u'__type__': u'QuestPartyItem', u'item_type_mask': 0}, {u'item_id': 115,
        u'index': 2, u'__type__': u'QuestPartyItem', u'item_type_mask': 0}], u'name_id': 25610,
        u'npc_conclusion_id': 25612, u'duration': 216000, u'improvement_id': 0, u'fame_level': 19,
        u'image': u'shopr2/quests/kurtey.png', u'loot': {u'resource_id': 0, u'__type__': u'QuestLoot',
        u'special_item': None, u'amount': 3, u'recipe_id': 0, u'item_id': 25}}
        '''
        # get the new quest
        newQuest = change[2]
        # fill out the easy stuff
        outputQuest = {'name':'', 'introText':'', 'outroText':'', 'time':newQuest['duration']/3600,
                       'shopLevelReq':newQuest['fame_level'], 'picLink':cdnBase+newQuest['image'], 'unlockedBy':'none',
                       'custsNeeded':[], 'itemsNeeded':[], 'reward':[], 'xp':newQuest['customer_xp_reward'],
                       'id':newQuest['id']}
        # get the easy text IDs
        nameTextID = newQuest['name_id']
        introTextID = newQuest['npc_intro_id']
        outroTextID = newQuest['npc_conclusion_id']
        for asset in assets:
            if asset['id'] == nameTextID: outputQuest['name'] = asset['value']
            elif asset['id'] == introTextID: outputQuest['introText'] = asset['value']
            elif asset['id'] == outroTextID: outputQuest['outroText'] = asset['value']
        # if the quest has a parent id, get the name of the previous quest in the line
        if newQuest['parent_id'] != 0:
            for quest in newSD['quests']:
                if quest['id'] == newQuest['parent_id']: pnameTextID = quest['name_id']
            for asset in assets:
                if asset['id'] == pnameTextID: outputQuest['unlockedBy'] = asset['value']
        # get the customer classes needed
        for cust in newQuest['party_members']:
            for cclass in newSD['character_classes']:
                if cclass['codename'] == cust['codename']:
                    for asset in assets:
                        if asset['id'] == cclass['name_id']: outputQuest['custsNeeded'].append(asset['value'])
        # get the items needed
        for reqItem in newQuest['party_items']:
            for item in newSD['items']:
                if item['id'] == reqItem['item_id']:
                    for asset in assets:
                        if asset['id'] == item['name_id']: outputQuest['itemsNeeded'].append(asset['value'])
        # get the reward info; only one of resource_id, item_id, and special_item will be nonzero/nonNone
        if newQuest['loot']['resource_id'] != 0:
            resID = newQuest['loot']['resource_id']
            for res in newSD['resources']:
                if res['id'] == resID: rewardTextID = res['name_id']
        elif newQuest['loot']['item_id'] != 0:
            itemID = newQuest['loot']['item_id']
            for i in newSD['items']:
                if i['id'] == itemID: rewardTextID = i['name_id']
        else:
            rewardTextID = 0
            outputQuest['reward'] = [newQuest['loot']['amount'], newQuest['loot']['special_item']]
        # if the reward isn't a special item, get the name text from assets
        if rewardTextID != 0:
            for asset in assets:
                if asset['id'] == rewardTextID: outputQuest['reward'] = [newQuest['loot']['amount'], asset['value']]
        return [change[1], outputQuest]

    elif changeType == 'workers':
        '''
        {u'character_class_id': 20, u'uid': u'b2436e9a95f3424c8ee88aec195d7b62', u'ticket_cost': 3, u'color': 0,
        u'__type__': u'Worker', u'unlock_improvement_id': 0, u'cost': 10000, u'unlock_shop_fame': 14, u'id': 6}
        '''
        # get the new worker
        newWorker = change[2]
        # fill out the easy stuff
        outputWorker = {'name':'', 'goldCost':newWorker['cost'], 'couponCost':newWorker['ticket_cost'],
                        'shopLevelReq':newWorker['unlock_shop_fame'], 'id':newWorker['id']}
        # get the class name id
        for cclass in newSD['character_classes']:
            if cclass['id'] == newWorker['character_class_id']:
                for asset in assets:
                    if asset['id'] == cclass['name_id']: outputWorker['name'] = asset['value']
        return [change[1], outputWorker]

    elif changeType == 'achievements':
        '''
        {u'index': 0, u'rewards': [{u'__type__': u'AchievementReward', u'type': 2, u'id': 166, u'data': u'greatmace'}],
        u'description_id': 26350, u'image': None, u'gamename': u'shopr2', u'__type__': u'Achievement', u'flags': 14,
        u'parent_id': 482, u'name_id': 26349, u'limit': 100, u'type': 13, u'id': 483}
        '''
        # get the new achievement
        newAchievement = change[2]
        # fill out the easy stuff
        outputAchievement = {'name':'', 'requrement':'', 'reward':[]}
        # get the name and description text from assets
        for asset in assets:
            if asset['id'] == newAchievement['name_id']: outputAchievement['name'] = asset['value']
            elif asset['id'] == newAchievement['description_id']: outputAchievement['requrement'] = asset['value']
        if newAchievement['rewards'][0]['type'] == 2 or newAchievement['rewards'][0]['type'] == 7:
            outputAchievement['reward'] = newAchievement['rewards'][0]['data']
        else:
            typeDict = {1:'gold', 3:'platinum hammer', 4:'mystical hourglass', 5:'starry coupon', 6:'magical key',
                        8:'mithril gear'}
            outputAchievement['reward'] = [newAchievement['rewards'][0]['data'],
                                           typeDict[newAchievement['rewards'][0]['type']]]
        return [change[1], outputAchievement]

    elif changeType == 'character_classes':
        '''
        {u'bust_image': u'shopr2/characters/heads/monk_female_head.png', u'uid': u'6c827afdc52d3213b8711fcd62766a70',
        u'sprite_collection': u'monk_f_spritesheet', u'description_id': 0, u'gender': False,
        u'full_image': u'shopr2/characters/full/portrait_monk_female.png', u'__type__': u'CharacterClass',
        u'name_id': 27058, u'y_offset': -200, u'x_offset': -125, u'codename': u'monk', u'id': 45,
        u'items_mask': 522489864}
        '''
        # get the new character class
        newCClass = change[2]
        # fill out the easy stuff
        outputCClass = {'name':'none', 'description':'none', 'icon':cdnBase+newCClass['bust_image'],
                        'fullPic':cdnBase+newCClass['full_image'], 'klashItems':'(not a klasher)'}
        # any of name, description, and klash items (items_mask) could be zero, so just check each
        if newCClass['name_id'] != 0:
            for asset in assets:
                if asset['id'] == newCClass['name_id']: outputCClass['name'] = asset['value']
        if newCClass['description_id'] != 0:
            for asset in assets:
                if asset['id'] == newCClass['description_id']: outputCClass['description'] = asset['value']
        if newCClass['items_mask'] != 0: outputCClass['klashItems'] = getCats(newCClass['items_mask'])
        return [change[1], outputCClass]

    elif changeType == 'krown_rewards':
        '''
        {u'level': 0, u'amounts': u'4', u'prize_index': 2, u'__type__': u'KrownReward', u'type': 1, u'id': 3,
        u'prize_uid': u'rareitem_volcanic_rock'}
        '''
        # get the new chest reward
        newChest = change[2]
        # fill out the easy stuff
        outputChest = {'kounter':newChest['level']+1, 'id':newChest['id']}
        if newChest['type'] == 2: outputChest['prize'] = [newChest['amounts'], 'krowns']
        else: outputChest['prize'] = [newChest['amounts'], newChest['prize_uid']]
        return [change[1], outputChest]

    else:
        return change


from tkinter import *
from pytube import YouTube

# creating the display window or the tkinter grid
# Tk() used to initialize tkinter to create display window
window = Tk()
# used to set the window’s width and height
window.geometry('500x500')
# set the fix size of window
window.resizable(0,0)
# used to give the title of window
window.title('Video Downloader App')

# Label() widget used to display text that users can’t able to modify.
Label(window,text = 'Youtube Video Downloader', font ='arial 20 bold').pack()

# Create field to enter link:
# declaring a variable called link and set it to stringvar which is a string type variable and we r gonna use that to store the youtube video link that the user enters.
link = StringVar()

Label(window, text = 'Paste Link Here:', font = 'arial 15 bold').place(x= 160 , y = 60)

# Create an input field to enter the youtube link that you want to download
link_enter = Entry(window, width = 70,textvariable = link).place(x = 32, y = 90)

# Let's create a function to start the downloading process
def downloader():
    url = YouTube(str(link.get()))
    video = url.streams.first()
    video.download()
    Label(window, text = "Video is downloaded and saved in project's folder !", font = 'arial 15').place(x= 20 , y = 210)

# Create a button widget to download and assign the download function to one of the attributes of the Button class which is called command, which simply means by clicking on that button I am giving it a command to trigger the downloader function
Button(window, text="Download", font='arial 15 bold', bg='yellow', padx=2, command=downloader).place(x=180, y=150)

window.mainloop()


import argparse
import os

from kusanagi.base import apply_controller, ExperienceDataset
from kusanagi.ghost import control
from kusanagi.shell import experiment_utils
from kusanagi import utils
import dill
import pickle as pkl

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--dataset_folder', type=str,
                        default=utils.get_output_dir(),
                        help='where to load the results of the experiment')
    parser.add_argument('-r', '--render', type=bool,
                        default=False,
                        help='whether to call env.render')
    parser.add_argument('-k', '--kwarg', nargs=2, action='append',
                        default=[],
                        help='additional arguments for the experiment [name value]')
    parser.add_argument('-s', '--setup_func', type=str,
                        default='setup_cartpole_experiment',
                        help='function used to setup the environment (in kusanagi.shell.experiment_utils)')
    parser.add_argument('-p', '--policy_class', type=str,
                        default='NNPolicy',
                        help='Policy class (in kusanagi.ghost.control)')

    args = parser.parse_args()

    odir = args.dataset_folder
    kwargs = dict(args.kwarg)
    n_trials = int(kwargs.get('n_trials', 5))
    last_iteration = int(kwargs.get('last_iteration', 5))
    config_path = os.path.join(odir, 'initial_config.dill')
    exp_path = os.path.join(odir, 'experience_%d'%(last_iteration))
    pol_path = os.path.join(odir, 'policy_%d'%(last_iteration))
    setup_func = getattr(experiment_utils, args.setup_func)
    policy_class = getattr(control, args.policy_class)

    with open(config_path, 'rb') as f:
        config_dict = dill.load(f)

    params = config_dict['params']
    p0 = params['state0_dist']
    exp = ExperienceDataset(filename=exp_path)
    if args.policy_class == 'NNPolicy':
        pol = policy_class(p0.mean.size, filename=pol_path)
    else:
        pol = policy_class(filename=pol_path)

    env, cost, params = setup_func(params)
    results = experiment_utils.evaluate_policy(env, pol, exp, params, n_trials, render=args.render)

    # dump results to file
    results_path = os.path.join(odir, 'results_%d_%d'%(last_iteration, n_trials))
    with open(results_path, 'wb+') as f:
        utils.print_with_stamp('Dumping results to [%s]' % (results_path))
        pkl.dump(results, f, 2)


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
class Stat:
    def __init__(self):
        self.char_frequency_dict = dict()
        self.num_keyword_list = list()
        self.posi_stat = dict()

    def IsToRemain(self,key_word_list,posi):
        num_key_word = 0
        for one in key_word_list: #删除地图的数据
            if "shadow出口©" in one or "NavInfo" in one or "道道通" in one:
                return []

        for one in key_word_list:
            if one.startswith("key word:"):
                num_key_word += 1
            if one.startswith("sentence:"):
                len_sentence = len(one.strip("sentence:").strip())

        assert (posi not in self.posi_stat)
        self.posi_stat[posi] = (2*num_key_word)/len_sentence
        #print(self.posi_stat)
        return key_word_list

    def readBaikeData(self,file_name):
        with open(file_name, "r",encoding="utf-8") as fo:
            fo.seek(0,os.SEEK_SET)
            posi = fo.tell()
            key_word_list = []
            line = fo.readline()
            while line:
                line = line.strip()
                if not line :
                    if key_word_list != []:
                        self.IsToRemain(key_word_list,posi)
                        posi = fo.tell()
                        key_word_list = []
                else:
                    key_word_list.append(line)
                line = fo.readline()
            if key_word_list != []:
                self.IsToRemain(key_word_list, posi)

    def extractBaikeData(self, file_name, max_Instance_num):
            sort_tuple = sorted(self.posi_stat.items(), key=lambda item: item[1], reverse=True)
            index_list = []
            all_instance_num = max_Instance_num
            if all_instance_num <= 0 or all_instance_num > len(sort_tuple):
                all_instance_num = len(sort_tuple)
            print("all_instance_num:",all_instance_num)
            for i in range(all_instance_num):
                index_list.append(sort_tuple[i][0])
            with open(file_name, "r", encoding="utf-8") as fo, open(file_name + ".moreinfo.sort.version_20180531", "w", encoding="utf-8") as fw_more:
                fo.seek(0,os.SEEK_SET)
                for posi in index_list:
                    fo.seek(posi,os.SEEK_SET)
                    line = fo.readline().strip()

                    while line:
                        fw_more.write(line + "\n")
                        line = fo.readline().strip()
                    fw_more.write("\n")

    def save_char_frequency_dict(self,file_name):

            if file_name == "":
                return
            else:
                import json
                self.char_frequency_dict = sorted(self.char_frequency_dict.items(), key=lambda x: x[1], reverse=True)
                with open(file_name, "w", encoding="utf-8", errors="ignore") as fw:
                    json.dump(self.char_frequency_dict, fw, indent=4, ensure_ascii=False)
                    fw.write('\n')
                    fw.flush()
                    os.fsync(fw)

if __name__ == "__main__":
    stat_test = Stat()
    file_name = sys.argv[1]
    stat_test.readBaikeData(file_name)
    stat_test.extractBaikeData(file_name,0)


import requests
from bs4 import BeautifulSoup as bs
import re
import unicodecsv as csv
import os

BASE_URL = "http://gamli.rvk.is/vefur/owa/{}"
year_url = "http://gamli.rvk.is/vefur/owa/edutils.parse_page?nafn=BN2MEN{}"
years = ["96", "97", "98", "99", "00", "01", "02", "03", "04", "05", "06",
         "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

def scrape(url):
    r = requests.get(url)
    soup = bs(r.text, "html5lib")
    filename = soup.h2.next_sibling.strip().replace(" ", "_").replace(".", "") + ".csv"
    links = soup.find_all(href=re.compile("edutils.parse_page"))
    result = []
    for link in links:
        data = {}
        for el in link.previous_elements:
            try:
                if "Umsókn nr." in el.text:
                    data['nr'] = el.text.strip().replace('Umsókn nr. ', '')
                    break
            except:
                pass
        data['skjal_nr'] = link.get('href').split('=')[-1:][0]
        #data["url"] = BASE_URL.format(link.get('href'))
        data["heimilisfang"] = link.text.lstrip('">').strip()
        tegund = link.find_next("i")
        data["tegund"] = tegund.text.strip()
        text = ""
        for el in tegund.find_next_siblings():
            if el.name == "i":
                break
            if not el.name == "i":
                try:
                    text = text + str(el.next_sibling)
                except AttributeError:
                    pass
        data["nidurstada"] = tegund.find_next("i").get_text(" ", strip=True)
        data["spurning"] = (text.strip().replace("\n", " "))
        result.append(data)
    return (filename, result)

def collect_all():
    for year in years:
        r = requests.get(year_url.format(year))
        soup = bs(r.text, "lxml")
        for link in soup.find_all('a'):
            filename, result = scrape(BASE_URL.format(link.get('href')))
            keys = result[0].keys()
            if not os.path.exists('data/' + year):
                os.makedirs('data/' + year)
            filepath = os.path.join('data', year, filename)
            if os.path.exists(filepath):
                print("  {} exists".format(filepath))
            else:
                with open(filepath, "wb") as f:
                    dict_writer = csv.DictWriter(f, keys)
                    dict_writer.writeheader()
                    dict_writer.writerows(result)
                print("  Sótti {}".format(filepath))
        print("------")
        print("Sótti {}".format(year))

if __name__ == '__main__':
    collect_all()


# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import testtools

import eventlet

from masakarimonitors.hostmonitor.host_handler import handle_host
from masakarimonitors.hostmonitor.host_handler import parse_cib_xml
from masakarimonitors import utils

eventlet.monkey_patch(os=False)

EXECUTE_RETURN = 'Status of crmd@masakari-node: S_NOT_DC (ok)'

class TestHandleHost(testtools.TestCase):

    def setUp(self):
        super(TestHandleHost, self).setUp()

    @mock.patch.object(parse_cib_xml.ParseCibXml, 'get_node_state_tag_list')
    @mock.patch.object(parse_cib_xml.ParseCibXml, 'have_quorum')
    @mock.patch.object(parse_cib_xml.ParseCibXml, 'set_cib_xml')
    @mock.patch.object(utils, 'execute')
    @mock.patch.object(handle_host.HandleHost, '_check_pacemaker_services')
    @mock.patch.object(handle_host.HandleHost, '_check_hb_line')
    def test_monitor_hosts(self,
                           mock_check_hb_line,
                           mock_check_pacemaker_services,
                           mock_execute,
                           mock_set_cib_xml,
                           mock_have_quorum,
                           mock_get_node_state_tag_list):

        obj = handle_host.HandleHost()

        mock_check_hb_line.return_value = 0
        mock_check_pacemaker_services.return_value = False
        mock_execute.return_value = (EXECUTE_RETURN, '')
        mock_set_cib_xml.return_value = None
        mock_have_quorum.return_value = 0
        mock_get_node_state_tag_list.return_value = []

        ret = obj.monitor_hosts()
        self.assertEqual(None, ret)

    @mock.patch.object(utils, 'execute')
    def test_check_hb_line(self,
                           mock_execute):

        obj = handle_host.HandleHost()

        mock_execute.return_value = ('', '')

        ret = obj._check_hb_line()
        self.assertEqual(2, ret)


#!/usr/bin/env python
#Author:JiangZhiheng

#利用ElementTree.XML将字符串解析成XML对象

from xml.etree import ElementTree as ET

#打开文件，读取XML内容
str_xml = open('xo.xml','r').read()
#将字符串解析成xml特殊对象，root代指xml文件的根节点
root = ET.XML(str_xml)
#print(root.tag)
#循环所有的year节点
# 循环所有的year节点

for node in root.iter('year'):
    print(node.tag)

    # 将year节点中的内容自增一
    new_year = int(node.text) + 1
    node.text = str(new_year)

    # 设置属性
    node.set('name', 'alex')
    node.set('age', '18')
    # 删除属性
    del node.attrib['name']

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

#利用ElementTree.parse将文件直接解析成xml对象
from xml.etree import ElementTree as ET
#直接解析xml文件
tree = ET.parse('xo.xml')
#获取xml的根节点
root = tree.getroot()

#遍历xml中所有的year节点
#for node in root.iter('year'):
#   print(node.tag,node.text)


from collections import OrderedDict
from fun_forms.fields.field_templates import int_field, string_field
from fun_forms.fields.funcs import validate_field
from fun_forms.forms.funcs import (make_form_template, populate_form,
                                   validate_form)
from fun_forms.validators.base import make_validator
from fun_forms.validators.numbers import greater_than
from fun_forms.validators.strings import (has_length_when_stripped,
                                          is_upper_case)

def test_make_form_template_basic():
    this_form_fields = (
        ('first_name', string_field(allow_none=True)),
        ('last_name', string_field()),
        ('age', int_field())
    )

    form_template = make_form_template(this_form_fields)

    assert form_template.fields == OrderedDict(this_form_fields)
    assert form_template.validators == tuple()

def test_populate_form():
    this_form_fields = (
        ('first_name', string_field(allow_none=True)),
        ('last_name', string_field()),
        ('age', int_field())
    )

    form_template = make_form_template(this_form_fields)

    populated_form = populate_form(form_template, {
        'last_name': 'Powers',
        'age': 4
    })

    assert populated_form.fields['first_name'].value is None
    assert populated_form.fields['last_name'].value == 'Powers'
    assert populated_form.fields['age'].value == 4

def test_validate_form():
    this_form_fields = (
        ('first_name', string_field(allow_none=True)),
        ('last_name', string_field(
            validators=(has_length_when_stripped, is_upper_case))),
        ('age', int_field(
            validators=(greater_than(18), )
        ))
    )

    this_form_validator = make_validator(
        lambda fields: (fields['last_name'].populated_field.value !=
                        fields['first_name'].populated_field.value),
        lambda _: "first and last names must not be the same"
    )

    form_template = make_form_template(
        fields=this_form_fields,
        validators=(this_form_validator, )
    )
    populated_form = populate_form(form_template, {
        'first_name': 'Keith',
        'last_name': 'Keith',
        'age': 12
    })

    validated_form = validate_form(populated_form)

    assert validated_form.valid is False
    assert validated_form.fields == OrderedDict([
        (fieldname, validate_field(field))
        for fieldname, field in populated_form.fields.items()
    ])
    assert validated_form.errors == OrderedDict([
        ('fields', OrderedDict([
            ('last_name', ('Must be uppercase.',)),
            ('age', ('must be great than 18',))
        ])),
        ('form', ('first and last names must not be the same', ))
    ])


from typing import List
from utils import *

from collections import OrderedDict
class LRUCache(OrderedDict):

    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.capacity = capacity

    def get(self, key):
        """
        :type key: int
        :rtype: int
        """
        if key not in self:
            return - 1

        self.move_to_end(key)
        return self[key]

    def put(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: void
        """
        if key in self:
            self.move_to_end(key)
        self[key] = value
        if len(self) > self.capacity:
            self.popitem(last = False)

# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)

if __name__ == "__main__":
    nums = [2, 7, 11, 15]
    target = 26
    s = "aa"
    arrays = [[1, 2, 3], [4, 5, 6]]
    print(arrays)


#! /usr/bin/python3
import sys
import logging
import optparse
import subprocess
import os
import random
import string
import fitz
import time

logging.basicConfig(format = "%(asctime)-15s %(levelname)s:%(message)s", level=logging.INFO)

# global variables that are related to running the fuzzed binary
AFL_SHOWMAP = None
FUZZED_BINARY = None
BINARY_ARGS = None
MEM_LIMIT = None
TIME_OUT = None
OUTPUT = None
TMP_DIR = None

BLACKLIST = set()

ORIG_MEM_MAP = None

ALL_COVS = dict()
CUR_COVS = set()
CUR_UNIQ_COVS = set()

TMP_DIR='/dev/shm'

TIMEOUT = 1000 * 1 # set timeout as 1s

def remove_page(cur_pdf_output):
    doc = fitz.open(cur_pdf_output, filetype='pdf')
    page_cnt = doc.pageCount

    if page_cnt < 2:
        return 0

    cur_start_page = 0
    succeed_cnt = 0
    tmp_output_pdf = ('%s/%s.pdf' % (TMP_DIR, get_random_string(8)))
    # quick check. If CUR_UNIQ_COVS is NULL. Delete n-1 pages
    if len(CUR_UNIQ_COVS) == 0:
        try:
            doc.deletePageRange(0, page_cnt - 2)
            succeed_cnt = page_cnt - 1
        except:
            return 0
    '''
    while page_cnt > 1:
        for cur_idx in range(cur_start_page, page_cnt):
            page_cnt = doc.pageCount

            if page_cnt < 2:
                break

            try:
                doc.deletePage(cur_idx)
            except:
                continue

            try:
                doc.save(tmp_output_pdf)
            except ValueError:
                continue

            (r_code, covs) = get_covs(tmp_output_pdf)

            if len(covs) > 0 and len(CUR_UNIQ_COVS.difference(covs)) == 0:
                succeed_cnt += 1
                os.system('cp %s %s' % (tmp_output_pdf, cur_pdf_output))
            else:
                cur_start_page += 1
                doc = fitz.open(cur_pdf_output, filetype = 'pdf')
                page_cnt = doc.pageCount
                logging.info("Error. Next")
                break
    '''

    doc.save(tmp_output_pdf, garbage = 3)
    os.system('mv %s %s' % (tmp_output_pdf, cur_pdf_output))
    return succeed_cnt

def init_global_vars(options, args):
    '''
    initialize some global variables

    Args:
        options: options

    Returns:
        None
    '''
    global AFL_SHOWMAP, FUZZED_BINARY, BINARY_ARGS, MEM_LIMIT, TIMEOUT, OUTPUT, SAME_INPUT, TMP_DIR

    AFL_SHOWMAP = options.showmap
    FUZZED_BINARY = options.binary
    BINARY_ARGS = ' '.join(args)
    MEM_LIMIT = options.memory
    TIME_OUT = options.timeout
    OUTPUT = options.output

    #TMP_DIR = os.getenv('TMPDIR')

    if TMP_DIR == '' or not TMP_DIR:
        TMP_DIR = '/tmp'

    if '@@' not in BINARY_ARGS:
        logging.error("Only support the input is a file @@!")
        exit(-1)
    #SAME_INPUT = ('/tmp/%s.html' % get_random_string(8))

def get_random_string(length):
    '''
    generate random string with specific length

    Args:
        length

    Returns:
        random string
    '''
    letters = string.ascii_lowercase
    result_str = ''.join((random.choice(letters) for i in range(length)))
    return result_str

def run_showmap(pdf_input, output):
    '''
    run afl-showmap with specified seed

    Args:
        seed: the input file
        output: output file that store covs

    Returns:
        True if running correctly
    '''
    t_arg = ''

    if TIME_OUT:
        t_arg = ('-t %s' % TIME_OUT)

    #tmp_output='/tmp/%s' % get_random_string(8)

    running_args = ('%s -m %s %s -Z -o %s -- %s %s' %
            (AFL_SHOWMAP, MEM_LIMIT, t_arg, output, FUZZED_BINARY, BINARY_ARGS.replace('@@', pdf_input)))
    #running_args = ('%s -m %s %s -e -o %s -- %s %s %s' %
    #        (AFL_SHOWMAP, MEM_LIMIT, t_arg, tmp_output, FUZZED_BINARY, pdf_input, BINARY_ARGS))

    args_list = running_args.split()

    showmap_run = subprocess.run(args_list, stdout = subprocess.PIPE, \
            stderr = subprocess.PIPE)

    if showmap_run.returncode:
        logging.error('Errors when running afl-showmap firstly!\n \
                The output message is %s\n, The error message is %s\n' %
                (showmap_run.stdout, showmap_run.stderr))
        return False

    return True

def check_binary(binary):
    '''
    check if binary exists

    Args:
        binary: binary

    Returns:
        True if the binary exists
    '''
    c_output = subprocess.run(['which', binary], stdout = subprocess.PIPE)

    if os.path.exists(c_output.stdout.strip()):
        return True
    return False

def pre_check(options):
    '''
    pre check if required arguments exist

    Args:
        options: option

    Returns:
        None
    '''
    assert options.input != None, "Please input the seed to be analyzed with (-i)!"
    assert options.binary != None, "Please input the binary file that is fuzzed with (-b)!"
    assert options.output != None, "Please input the directory of output with (-o)!"

    # check the path of afl-showmap and binary is valid
    if not check_binary(options.showmap):
        logging.error('Please input the path of afl-showmap with (-s)!')
        exit(-1)

    # check the pat of tested binary is valid
    if not check_binary(options.binary):
        logging.error('Please input the path of tested binary with (-b)!')
        exit(-1)

    if not os.path.isdir(options.input):
        logging.error('Please input the directory of input files with (-i)!')
        exit(-1)

    if not os.path.isdir(options.output):
        mk_cmd = ('mkdir -p %s' % (options.output))
        mk_run = subprocess.run(mk_cmd.split(), stdout = subprocess.PIPE,\
                stderr = subprocess.PIPE)
        if mk_run.returncode != 0:
            logging.error('Error when mkdirring directory of output: %s' % options.output)
            exit(-1)

def collect_covs_of_seeds(input_dir):
    '''
    first running of the fuzzed binary.
    check if afl-whowmap runs normally
    and collect the covs of seed

    Args:
        seed: the input file that to be analyzed

    Returns:
        True if afl-showmap runs normally
    '''
    global ORIG_MEM_MAP
    global SAME_INPUT
    global ORIG_HTML_SIZE
    global ORIG_PDF_SIZE
    global BLACKLIST

    all_covs = dict()
    tmp_output = ('%s/%s' % (TMP_DIR, get_random_string(8)))

    logging.info('collecting covs...')

    for seed in os.listdir(input_dir):

        seed = os.path.join(input_dir, seed)

        if os.path.isdir(seed):
            continue

        if not run_showmap(seed, tmp_output):
            os.system('rm %s' % tmp_output)
            logging.error('[showmap error]: add %s to blacklist' % seed)
            BLACKLIST.add(os.path.basename(seed))
            continue

        with open(tmp_output, 'r+') as s_o:
            for line in s_o.readlines():
                if line.strip().isdigit():
                    cur_edge = int(line.strip())

                    if cur_edge in all_covs:
                        all_covs[cur_edge] += 1
                    else:
                        all_covs[cur_edge] = 1

    os.system('rm %s' % tmp_output)
    return (True, all_covs)

def get_covs(seed):
    '''
    first running of the fuzzed binary.
    check if afl-whowmap runs normally
    and collect the covs of seed

    Args:
        seed: the input file that to be analyzed

    Returns:
        True if afl-showmap runs normally
    '''

    covs = set()
    tmp_output = '%s/%s' % (TMP_DIR, get_random_string(8))

    if not run_showmap(seed, tmp_output):
        os.system('rm %s' % tmp_output)
        return (False, covs)

    with open(tmp_output, 'r+') as s_o:
        for line in s_o.readlines():
            if line.strip().isdigit():
                covs.add(int(line.strip()))

    os.system('rm %s' % tmp_output)

    return (True, covs)

def trim_pdf(seed):

    global ALL_COVS

    cur_time_start = int(round(time.time() * 1000))

    base_name_seed = os.path.basename(seed)
    cur_pdf_output = os.path.join(OUTPUT, base_name_seed)

    orig_pdf_size = os.path.getsize(seed)

    if os.path.exists(cur_pdf_output):
        os.system('rm %s' % cur_pdf_output)

    (_, cur_covs) = get_covs(seed)
    # get xrefs
    try:
        doc = fitz.open(seed, filetype='pdf')
        xrefs_cnt = doc.xrefLength()
        doc.save(cur_pdf_output)
    except:
        return

    succeed_cnt = remove_page(cur_pdf_output)
    #logging.info("UNIQ COVS is {}".format(CUR_UNIQ_COVS))

    skip = False

    tmp_output_pdf = ('%s/%s.pdf' % (TMP_DIR, get_random_string(8)))

    failed_cnt = 0

    for cur_xref in range(1, xrefs_cnt):

        if len(CUR_UNIQ_COVS) > 10:
            break

        cur_time = int(round(time.time() * 1000))

        if cur_time - cur_time_start > TIMEOUT:
            logging.warning("timeout. skip it!")
            break

        # do not try much time
        if failed_cnt > 10:
            break

        if not skip:
            doc = fitz.open(cur_pdf_output, filetype='pdf')

        skip = False

        try:
            del_obj = doc.xrefObject(cur_xref)
            doc._deleteObject(cur_xref)
        except:
            skip = True
            logging.error('Delete object error! %s' % repr(del_obj))
            continue

        logging.debug('[Trim]: deleteing %s' % (del_obj))

        try:
            doc.save(tmp_output_pdf)
        except ValueError:
            continue

        if len(CUR_UNIQ_COVS) == 0:
            succeed_cnt += 1
            os.system('cp %s %s' % (tmp_output_pdf, cur_pdf_output))
            skip = True
            continue

        (r_code, covs) = get_covs(tmp_output_pdf)

        # this is valid trimming
        if len(covs) > 0 and len(CUR_UNIQ_COVS.difference(covs)) == 0:
            logging.debug('trim valid!')
            succeed_cnt += 1
            os.system('cp %s %s' % (tmp_output_pdf, cur_pdf_output))
            skip = True
            failed_cnt = 0
        else:
            failed_cnt += 1

    doc = fitz.open(cur_pdf_output, filetype='pdf')
    doc.save(tmp_output_pdf, garbage = 3)
    os.system('mv %s %s' % (tmp_output_pdf, cur_pdf_output))

    (_, output_covs) = get_covs(cur_pdf_output)

    deleted_covs = cur_covs.difference(output_covs)

    for cur_cov in deleted_covs:
        if cur_cov not in ALL_COVS:
            continue
        ALL_COVS[cur_cov] -= 1

        if ALL_COVS[cur_cov] == 0:
            del ALL_COVS[cur_cov]

    # delete covs from ALL_COVS

    trimed_pdf_size = os.path.getsize(cur_pdf_output)
    optimized_rate = (1 - trimed_pdf_size / orig_pdf_size)
    if trimed_pdf_size == 0:
        trimed_pdf_size = orig_pdf_size

    logging.info('Save output %s' % (cur_pdf_output))
    logging.debug('Summary of file %s: remove %d objects' % (base_name_seed, succeed_cnt))
    logging.debug('Summary of file %s: pdf size(original): %d bytes' % (base_name_seed, orig_pdf_size))
    logging.debug('Summary of file %s: pdf size(after trimming): %d bytes' % (base_name_seed, trimed_pdf_size))
    logging.debug('Summary of file %s: optimized rate: %f' % (base_name_seed, optimized_rate))

def trim_seeds(in_dir):
    global CUR_UNIQ_COVS

    for cur_file in os.listdir(in_dir):

        if cur_file in BLACKLIST:
            continue

        seed = os.path.join(in_dir, cur_file)

        if os.path.isdir(seed):
            continue

        logging.info('current seed is %s' % seed)

        (r_code, cur_covs) = get_covs(seed)

        assert len(cur_covs) > 0, 'the length of cur_covs equals to 0!'

        CUR_UNIQ_COVS = set()

        for cov in cur_covs:
            if cov not in ALL_COVS or ALL_COVS[cov] == 1:
                CUR_UNIQ_COVS.add(cov)

        trim_pdf(seed)

def main():

    parser = optparse.OptionParser()

    parser.add_option('-i', '--input', dest = 'input', action = 'store', \
            type = 'string', help = 'the input directory that to be analyzed', default = None)
    parser.add_option('-b', '--binary', dest = 'binary', action = 'store', \
            type = 'string', help = 'the binary file that is fuzzed', default = None)
    parser.add_option('-s', '--showmap', dest = 'showmap', action = 'store', \
            type = 'string', help = 'the path of afl-showmap', default = 'afl-showmap')
    parser.add_option('-m', '--memory', dest = 'memory', action = 'store', \
            type = 'string', help = '[args of afl-showmap]: memory limit', default = 'none')
    parser.add_option('-t', '--timeout', dest = 'timeout', action = 'store', \
            type = 'string', help = '[args of afl-showmap]:time out', default = None)
    parser.add_option('-o', '--output', dest = 'output', action = 'store', \
            type = 'string', help = 'directory of output', default = None)

    (options, args) = parser.parse_args()

    pre_check(options)

    init_global_vars(options, args)

    global ALL_COVS

    (r_code, ALL_COVS) = collect_covs_of_seeds(options.input)

    if not r_code:
        logging.error('Try running afl-showmap error!')
        exit(-1)

    # remove object of pdf
    trim_seeds(options.input)

if __name__ == '__main__':
    main()


from django.urls import path
from. import views
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static

app_name = 'pages'

urlpatterns = [
    path('',views.product_list, name='product_list'),
	path('home',views.home, name='home'),
	path('createpost/',views.createpost, name='createpost') ,
	path('confirm',views.customerorder, name='co') ,
	url(r'^list/$',views.product_list, name='product_list') ,#working
	url(r'^list/(?P<pk>[0-9]+)/$',views.category_select, name='category_select'),
	url(r'^contact/$', views.contactview, name='contact'),
	path('about/',views.about, name='about') ,
	#path('contact/',views.contact, name='contact') ,
	#path('contact/r^(?P<mid>[1-9]+)/$',views.mid, name='mid') ,
	#path('contact/page<int:num>/', views.mid),
	#url(r'^contact/(?P<mid>\d+)/$', views.mid, name='mid'),
	#url(r'^details/(?P<key>[0-9]+)/$', views.mid, name='mid'),
	url(r'^index/$',views.IndexView.as_view(), name='index') ,
	#url(r'^list$',views.product_list, name='list') ,
	url(r'^indexproducts/$',views.pindex, name='indexp') ,
	#url(r'^indexproducts/(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='productdetail') ,
	url(r'^indexproducts/(?P<pk>[0-9]+)/$',views.product_detail, name='productdetail') ,
	#url(r'^indexproducts/(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='pdetail') ,
	url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='mid'),
	url(r'^index/add/$', views.MusicianCreate.as_view(), name='addm'),
	#path('index/',views.index, name='index') ,
	#url(r'^indexstar/',views.indexs, name='indexs'),
	path('indexstar/',views.indexs, name='indexs'),
	url(r'^indexstar/f/$', views.indexstar, name='indexf'),
	url(r'^blogadd/$', views.showform, name='blogadd'),
	url(r'^(?P<id>\d+)/edit/$', views.editpost, name='editpost'),
	url(r'^(?P<id>\d+)/delete/$', views.musician_delete_view, name='deletepost'),
	url(r'^test1/$',views.test1, name='test1') ,
	url(r'^indexproducts/(?P<pk>[0-9]+)/confirm.html',views.confirm, name='confirm') ,
	url(r'^try/$', views.aindex, name='try'),
    url(r'^ajax/', views.ajax,name='aj'),
]+ static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)

#from django.conf.urls import url
#from django.contrib import admin

#from mysite.views import hello

#urlpatterns = [
#   url(r'^admin/', admin.site.urls),
#   url(r'^hello/$', hello),
#


from django.conf.urls import url
from django.contrib import admin

from . import views

app_name = 'polls'

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote')
]

admin.site.site_header = 'chaoskernel'
admin.site.index_title = 'Admin Tools'
admin.site.site_title = 'django sandbox'


# -*- coding: utf-8 -*-
"""
@author zwczou@gmail.com
"""

from ..schema import Schema, And, Use, clear, split, Optional, match, length

valid_mobile = r'^1\d{10}$'

order_id_list_form = Schema({
    "order_id": And(Use(split()), [And(Use(int), lambda x: x > 0)], lambda x: 0 < len(x) <= 200),
})

order_id_form = Schema({
    "order_id": And(Use(int), lambda x: x > 0),
})

_order = ['', "created", "canceled", "paid", "shipped", "closed"]

user_order_form = Schema({
    Optional("status", default=0): And(Use(lambda x: _order.index(x)), lambda x: x > 0),
    Optional("offset", default=0): And(Use(int), lambda x: x >= 0),
    Optional("limit", default=10): And(Use(int), lambda x: x >= -1),
})

def split_item_id(a):
    return dict((int(k), int(v)) for k, v in [x.split(':') for x in a.split(',')])

create_order_form = Schema({
    "item_id": And(Use(clear()), Use(split_item_id), length(1, 200)),
    "nickname": And(Use(clear()), length(1, 20)),
    "mobile": And(match(valid_mobile)),
    Optional("zip_code", ""): And(Use(clear()), lambda x: len(x) == 6),
    "address": And(lambda x: 9 < len(x) < 100, Use(clear())),
    Optional("leave_message", ""): And(basestring, Use(clear(strip=False))),
})


from tkinter import *
from tkinter.ttk import *
from gpiozero import LED
from time import sleep

led = LED(4)

time = [1,2,3,4,5]

screen = Tk()

def on():
   led.on()

def off():
    led.off()

def blink():
    led.on()
    led.sleep(int(cmbvar.get()))
    led.off()
    led.sleep(int(cmbvar.get()))

Button1=Button(screen,text="On",style='TButton',state=ACTIVE,command=on) #Height is measures in lines and Width is defined in characters and not Pixels
Button1.grid(row=1,column=1,columnspan=2,sticky=NSEW)

Button1=Button(screen,text="Off",style='TButton',state=ACTIVE,command=off) #Height is measures in lines and Width is defined in characters and not Pixels
Button1.grid(row=2,column=1,columnspan=2,sticky=NSEW)

Button1=Button(screen,text="Blink",style='TButton',state=ACTIVE,command=blink) #Height is measures in lines and Width is defined in characters and not Pixels
Button1.grid(row=3,column=1,columnspan=2,sticky=NSEW)

cmbvar=StringVar(screen)
Combo1=Combobox(screen,height=10,width=25,values=time,textvariable=cmbvar) #Height refers to number of values it will list without scroll
Combo1.grid(row=4,column=1,columnspan=2,sticky=NSEW)

screen.mainloop()


# Simple Linear Regression
# Is based on the formula y = b0 + b1*x
# y = Dependent variable (something you are trying to understand on how it depends on
# something else)
# x = Independent variable (causing the dependent variable to change or having an implied
# association with the dependent variable)
# b1 = coefficient (how the IV changes)
# b0 = constant

# y_^i = modeled value of DI y_i (the value that is on the modeled regression line)
# Linear Regression: min(sum(y_i - y_^i)^2)

# Importing the library

import numpy as np
# Plot graphics
import matplotlib.pyplot as plt
# Import and manage datasets
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Salary_Data.csv')
print(dataset)

# Creating the matrix of features
# We take values of all the columns except the last one
x = dataset.iloc[:,:-1].values

# Creating the depending variable vector
y = dataset.iloc[:, 1].values

# Splitting dataset into Training set and Test set
# We need a dataset to train the model and one to test it against
from sklearn.cross_validation import train_test_split
# test/train size is portion of the dataset to included in test/train split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3, random_state = 0)

# Fitting Simple Linear Regression into the training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
# We fit the model to the training set
regressor.fit(x_train, y_train)

# Predicting the Test set results

# We create the vector of predictions of DI which contains the predicted
# values (i.e. salaries) for all observations of our test set
y_pred = regressor.predict(x_test)
# y_test holds the real values, y_pred holds predicted values

# Visualising the Training set results
# We draw red dots for our train data
plt.scatter(x_train, y_train, color="red")
# We draw a blue regression line for predictions of training data
plt.plot(x_train, regressor.predict(x_train), color="blue")
plt.title("Salary vs Experience (Training set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()

# We do the same for our test data
# We draw red dots for our test data
plt.scatter(x_test, y_test, color="red")
# We draw a blue regression line
# Because our regressor is trained on the training set, the regression line will be the
# same, regardless of what values we use to draw it
plt.plot(x_train, regressor.predict(x_train), color="blue")
plt.title("Salary vs Experience (Test set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()


"""Functional tests using the API with a fake Apple TV."""

from ipaddress import IPv4Address
import logging

from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from deepdiff import DeepDiff
import pytest

import pyatv
from pyatv import exceptions
from pyatv.companion.server_auth import CLIENT_CREDENTIALS
from pyatv.conf import AppleTV, CompanionService, MrpService
from pyatv.const import Protocol
from pyatv.interface import App, FeatureName, FeatureState

from tests.fake_device import FakeAppleTV
from tests.utils import until

_LOGGER = logging.getLogger(__name__)

TEST_APP: str = "com.test.Test"
TEST_APP_NAME: str = "Test"
TEST_APP2: str = "com.test.Test2"
TEST_APP_NAME2: str = "Test2"

class CompanionFunctionalTest(AioHTTPTestCase):
    async def setUpAsync(self):
        await super().setUpAsync()
        self.conf = AppleTV(IPv4Address("127.0.0.1"), "Test device")
        self.conf.add_service(
            MrpService("mrp_id", self.fake_atv.get_port(Protocol.MRP))
        )
        self.conf.add_service(
            CompanionService(
                self.fake_atv.get_port(Protocol.Companion),
                credentials=CLIENT_CREDENTIALS,
            )
        )
        self.atv = await self.get_connected_device()

    def tearDown(self):
        self.atv.close()
        super().tearDown()

    async def get_application(self, loop=None):
        self.fake_atv = FakeAppleTV(self.loop)
        self.fake_atv.add_service(Protocol.MRP)
        self.state, self.usecase = self.fake_atv.add_service(Protocol.Companion)
        return self.fake_atv.app

    async def get_connected_device(self):
        return await pyatv.connect(self.conf, loop=self.loop)

    @unittest_run_loop
    async def test_connect_only_companion(self):
        conf = AppleTV(IPv4Address("127.0.0.1"), "Test device")
        conf.add_service(CompanionService(self.fake_atv.get_port(Protocol.Companion)))

        with pytest.raises(exceptions.DeviceIdMissingError):
            await pyatv.connect(conf, loop=self.loop)

    @unittest_run_loop
    async def test_launch_app(self):
        await self.atv.apps.launch_app(TEST_APP)
        await until(lambda: self.state.active_app == TEST_APP)

    @unittest_run_loop
    async def test_app_list(self):
        self.usecase.set_installed_apps(
            {
                TEST_APP: TEST_APP_NAME,
                TEST_APP2: TEST_APP_NAME2,
            }
        )

        apps = await self.atv.apps.app_list()

        expected_apps = [App(TEST_APP_NAME, TEST_APP), App(TEST_APP_NAME2, TEST_APP2)]
        assert not DeepDiff(expected_apps, apps)

    @unittest_run_loop
    async def test_features(self):
        assert (
            self.atv.features.get_feature(FeatureName.LaunchApp).state
            == FeatureState.Available
        )
        assert (
            self.atv.features.get_feature(FeatureName.AppList).state
            == FeatureState.Available
        )

    @unittest_run_loop
    async def test_power_functions(self):
        assert self.state.powered_on

        await self.atv.power.turn_off()
        assert not self.state.powered_on

        await self.atv.power.turn_on()
        assert self.state.powered_on

    @unittest_run_loop
    async def test_session_start(self):
        # All commands should trigger a session start, so just use one and verify
        assert self.state.sid == 0
        await self.atv.power.turn_off()
        assert self.state.sid != 0
        assert self.state.service_type == "com.apple.tvremoteservices"


# MP5 Start 10.05.19
from Tkinter import *
import urllib2
from bs4 import BeautifulSoup
import docclass

class FacultyMember:  # Creating the class for faculty members.

    def __init__(self, name, profile_url, publications):
        self.name = name
        self.profile_url = profile_url
        self.publications = publications

class ResearchProject:  # Creating the class for research projects.

    def __init__(self, title, summary, PI):
        self.title = title
        self.summary = summary
        self.PI = PI

class Predictor:  # Creating the class for predictor.

    def __init__(self, classifier):

        self.myclassifier = docclass.naivebayes(
            docclass.getwords)  # Calling the classifier from docclass.py by creating a classifier object
        self.faculty_members = {}
        self.research_projects = {}

    def fetch_members(self, link):  # Parsing the members

        self.members_url = []

        urlsource = urllib2.urlopen(link)
        contents = urlsource.read()

        raw_data = BeautifulSoup(contents, features="html.parser")

        h4_tags = raw_data.find_all("h4")

        for i in h4_tags:
            a_tag = i.find("a")
            self.members_url.append(a_tag.attrs["href"])

        for member in self.members_url:
            linkk = link.split("/en")[0]
            member_link = linkk + member

            member_url = urllib2.urlopen(member_link)
            member_content = member_url.read()
            self.member_data = BeautifulSoup(member_content, features="html.parser")

            name_tag = self.member_data.find("h3")

            n = name_tag.text.strip().split(" ")[0]
            s = name_tag.text.strip().split(" ")[-1]
            member_name = n + " " + s # splitting and reuniting the name and surname because there are some members with middle name

            member_publications = self.fetch_publications() # Calling the function to fetching the publications and it will return and list of publications.

            member_name = FacultyMember(member_name, member_link, member_publications) # Creating the FacultyMember object

            self.faculty_members[s] = member_name # Adding object to dict

    def fetch_publications(self): # Fetching the publications
        list_publications = []

        publication_tag = self.member_data.find_all("div")
        for p in publication_tag:
            try:
                if p.attrs[u"id"] == "flat":
                    pub = p.find_all("li")

                    for pp in pub:
                        ppp = pp.text.strip()
                        pppp = ppp.split("\n")[1]
                        list_publications.append(pppp)

            except:
                pass

        return list_publications

    def fetch_projects(self, link): # Fetching the projects.

        urlsource = urllib2.urlopen(link)
        contents = urlsource.read()

        raw_data = BeautifulSoup(contents, features="html.parser")

        research_tags = raw_data.find_all("li")

        for i in research_tags:

            try:
                if i.attrs[u"class"] == [u'list-group-item']:

                    principal_investigator = i.find_all("a")[1].text.strip()
                    project_name = i.find("h4").text.strip()

                    ptags = i.find_all("p")
                    for i in ptags:
                        try:
                            if i.attrs[u'class'] == [u'gap']:
                                summary = i.text.strip()
                        except:
                            pass

                    members = [self.faculty_members[i].name for i in self.faculty_members]
                    if principal_investigator in members:
                        project_name_obj = ResearchProject(project_name, summary, principal_investigator)

                        self.research_projects[project_name.strip()] = project_name_obj
            except:
                pass

    def train_classifier(self): # Training the data with publications and members as category

        for prof in self.faculty_members:
            catt = self.faculty_members[prof].name
            for pblction in self.faculty_members[prof].publications:
                self.myclassifier.train(pblction, catt)

    def predict_PI(self, content, PI):

        prediction_result = self.myclassifier.classify(content) # Predicting the research project

        if prediction_result == PI:
            return (1, prediction_result) # If our prediction is correct then we will return tuple with our prediction and 1
        else:
            return (0, prediction_result) # If our prediction is wrong then we will return tuple with our prediction and 0

class ESTIMATOR_GUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()
        self.PredictorObj = Predictor("none")

    def initUI(self):
        self.pack(fill=X)
        self.frame0 = Frame(self)
        self.frame1 = Frame(self.frame0)
        self.frame2 = Frame(self.frame0)

        self.header = Label(self, text="PI Estimator Tool for SEHIR CS Projects", bg="lightseagreen", fg="white",
                            height=1, font=("Calibri", 25, "bold"))

        self.firstURL = Entry(self, width=90)
        self.secondURL = Entry(self, width=90)
        self.fetchButton = Button(self, text="Fetch", font=("Calibri", 11, "bold"), width=8, command=self.fetchButtonC)

        # Frame1
        self.v_scroolbar = Scrollbar(self.frame1, orient=VERTICAL)

        self.projectsLabel = Label(self.frame1, text="Projects", font=("Calibri", 25))
        self.projectsListbox = Listbox(self.frame1, width=90, height=10, yscrollcommand=self.v_scroolbar.set)
        self.projectsListbox.bind("<<ListboxSelect>>", self.selectResearch)
        self.v_scroolbar.config(command=self.projectsListbox.yview)

        # Frame2

        self.predictionLabel = Label(self.frame2, text="Prediction", font=("Calibri", 25))
        self.predictionResult = Label(self.frame2, text="")

        self.header.pack(fill=X, pady=(0, 8))

        self.firstURL.pack(pady=4)
        self.secondURL.pack(pady=(4, 6))
        self.fetchButton.pack()

        self.frame0.pack(fill=X, padx=(50, 0))
        self.frame1.pack(side=LEFT, fill=BOTH)
        self.frame2.pack(fill=BOTH)

        self.projectsLabel.pack()
        self.predictionLabel.pack()
        self.projectsListbox.pack(side=LEFT)
        self.v_scroolbar.pack(side=RIGHT, fill=Y)
        self.predictionResult.pack(pady=(30, 0))

    def fetchButtonC(self):
        try:
            membersUrl = self.firstURL.get()
            projectUrl = self.secondURL.get()

            self.PredictorObj.fetch_members(membersUrl)       # Calling the functions to fetch
            self.PredictorObj.fetch_projects(projectUrl)      # Calling the functions to fetch

            projects = []                                                                     # Filling the projects listbox
                                                                                # Filling the projects listbox
            for project in self.PredictorObj.research_projects:                               # Filling the projects listbox
                projects.append(self.PredictorObj.research_projects[project].title)           # Filling the projects listbox
            projects.sort()                                                                   # Filling the projects listbox
                                                                                # Filling the projects listbox
            for project in projects:                                                          # Filling the projects listbox
                self.projectsListbox.insert(END, project.strip())                             # Filling the projects listbox
            self.PredictorObj.train_classifier()                                              # Filling the projects listbox
        except:
            pass

    def selectResearch(self, event):
        try:
            researchTitle = self.projectsListbox.get(self.projectsListbox.curselection())
            for project in self.PredictorObj.research_projects:
                if researchTitle == self.PredictorObj.research_projects[project].title:
                    object = self.PredictorObj.research_projects[project]
                    PI = self.PredictorObj.research_projects[project].PI
                    title = self.PredictorObj.research_projects[project].title
                    summary = self.PredictorObj.research_projects[project].summary
                    content = title + " " + summary

            result = self.PredictorObj.predict_PI(content, PI)

            if result[0] == 1: # If the first item of our tuple which was returned by prediction function then our prediction is correct!
                self.predictionResult.config(text=result[1], bg="green", font=("Calibri", 25, "bold"))
            else:
                self.predictionResult.config(text=result[1], bg="red", font=("Calibri", 25, "bold"))

        except:
            pass
def main():
    root = Tk()
    root.title("ESTIMATOR")
    root.geometry("1000x380+290+50")
    app = ESTIMATOR_GUI(root)
    root.mainloop()

main()


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 14 13:53:44 2018

@author: kuruba
"""
import spacy
import re
from spacy.lang.en import English
from config.paths import input_path
from gensim.models import Word2Vec
from DBConnection.DBActions.get_ar_data import get_ar_data
nlp = spacy.load('en')
tokenizer = English().Defaults.create_tokenizer(nlp)

def clean_numbers(x):
    x = re.sub('[0-9]{5,}', '#####', x)
    x = re.sub('[0-9]{4}', '####', x)
    x = re.sub('[0-9]{3}', '###', x)
    x = re.sub('[0-9]{2}', '##', x)
    return x

def create_W2VModel(sentences_list, op_file_name):
    model = Word2Vec(sentences_list, size=200, window=3, min_count=1, workers=20)
    print('Training model!')
    model.train(sentences_list, total_examples=len(sentences_list), epochs=50)
    print('Model trained!')
    model.save(op_file_name)

if __name__ == '__main__':
    ar_df, df_actions, df_results = get_ar_data()
    sentences = df_actions.nltk_tokenized_raw_actions.to_list()
    sentences += df_results.nltk_tokenized_raw_results.to_list()

    print(len(sentences))
    print('Created sentences!')

    file_name = input_path + '/word_embeddings/Word2Vec_new_ar.bin'
    create_W2VModel(sentences, file_name)


import json

from flask import Flask, render_template, flash, url_for, redirect
from blueprints.book_blueprint import books
from blueprints.user_blueprint import users
from blueprints.reservation_blueprint import reservations

app = Flask("LibraryFrontend")
app.config['SECRET_KEY'] = 'c6852762e4fb8297c336fb03ce0b67bd'

app.register_blueprint(books)
app.register_blueprint(users)
app.register_blueprint(reservations)

@app.template_filter("to_json")
def to_json(dictionary):
    return json.dumps(dictionary)

@app.route("/", methods=["GET"])
def index():
    return redirect(url_for("books.get_all_books"))

@app.errorhandler(404)
def not_found(error):
    flash(f"There was a problem finding the page you wanted", "danger")
    return render_template("404.html", title="Page not found"), 404

if __name__ == '__main__':
    app.jinja_env.auto_reload = True
    app.config['TEMPLATES_AUTO_RELOAD'] = True
    app.run(host="127.0.0.1", port=50001, debug="True")


import socket
import logging
import uuid
import time
import traceback
from multiprocessing import Process, Queue
from .receiver import Receiver
from .cxn import CxnPool
from util.crypto_hash import crypto_hash
from config.network_config import *

logging.basicConfig(level=logging.INFO)

class Server(Process):

    def __init__(self, network: str, message_queue: Queue, cxn_pool: CxnPool):
        Process.__init__(self)
        self._id = crypto_hash(str(uuid.uuid4()), time.time_ns()) # A unique ID for the server
        self.queue = message_queue  # A Queue object to set messages in and share among processes
        self.external_ip = get_ip() # The servers external IP
        self._host = CONFIG_SETTINGS['GRAPEVINE']['listen_address']['host'] # The listening address ip
        self.port = NETWORK_PORT[network] # The listening address port
        self.cxn_pool = cxn_pool # A connection pool object to manage all connections

    def run(self):

        try:
            """
            Run the server process
            """
            # Log out the server id, listening_address and process id
            logging.info(f"Server {self._id} listening at {self.external_ip}:{self.port} - PID: {self.pid}")

            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # initiate the server
            server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set socket options
            server.bind((self._host, self.port)) # bind the server to _host:port
            server.listen(5) # listen for incoming connections

            while True:
                try:
                    # Accept clients
                    _client, address = server.accept()
                    client_ip, client_port = address # unpack the address tuple

                    """initiate a receiver object to receive message, provide
                    the client socket and message queue to the receiver process"""
                    receiver = Receiver(_client,client_ip, client_port,self.queue, self.cxn_pool)
                    receiver.start()    # Start the receiver process
                    logging.info(f"New connections -> {receiver} accepted connection from {client_ip}:{client_port}") # log the new connection

                    """add the connection ip, receiver object and 'server_id'
                    to the cx pool"""
                    self.cxn_pool.add_cxn(client_ip, receiver, f"{client_ip}:{self.port}") # add

                    while not self.queue.empty():
                        # if the queue is not empty get the message
                        item = self.queue.get() # get the oldest item in the queue
                        logging.info(f"Message -> {item}") # log the message

                except Exception as e:
                    logging.debug(f"Exception in Server.run() at pid {self.pid} -> {e} --> {traceback.print_exc()}")
        except KeyboardInterrupt:
            server.close()
            print(f"Exited the process - PID {self.pid}")

if __name__ == "__main__":
    queue = Queue()
    cxn_pool = CxnPool()
    server = Server(network='testnet', message_queue=queue, cxn_pool=cxn_pool)
    try:
        server.start()
    except KeyboardInterrupt:
        server.join()