import torch
from torch import nn
import torch.nn.functional as F
from torch.autograd import Variable
from multi_env import MultiEnv

def train(model, create_env, num_envs, optimizer, gamma, num_updates,
          max_episode_length, steps_per_update):
    # torch.manual_seed(args.seed)

    # env.seed(args.seed)

    model.train()

    env = MultiEnv(create_env, num_envs)
    state = env.reset()  # list of states for each concurrent env
    state = torch.from_numpy(state)
    episode_done = True

    episode_length = 0
    update = 0
    while update < num_updates:
        episode_length += 1

        values = []
        log_action_probs = []
        rewards = []
        entropies = []

        for step in range(steps_per_update):
            # list of values and action logits for each concurrent env
            value, action_logit = model(Variable(state))
            action_prob = F.softmax(action_logit)
            log_action_prob = F.log_softmax(action_logit)
            entropy = -(log_action_prob * action_prob).sum(1)
            entropies.append(entropy)

            action = action_prob.multinomial().data
            log_action_prob = log_action_prob.gather(1, Variable(action))

            state, reward, episode_done, _ = env.step(action.numpy())
            if episode_length >= max_episode_length:
                episode_done = True
            reward = max(min(reward, 1), -1)

            state = torch.from_numpy(state)
            values.append(value)
            log_action_probs.append(log_action_prob)
            rewards.append(reward)

            if episode_done:
                episode_length = 0
                state = env.reset()
                break

        R = torch.zeros(1, 1)
        if not episode_done:
            value, _ = model(Variable(state))
            R = value.data

        values.append(Variable(R))
        policy_loss = 0
        value_loss = 0
        R = Variable(R)
        advantage = torch.zeros(1, 1)
        for i in reversed(range(len(rewards))):
            R = gamma * R + rewards[i]
            advantage = R - values[i]
            value_loss = value_loss + 0.5 * advantage.pow(2)

            advantage = Variable(advantage * gamma + rewards[i] +
                                 gamma * values[i + 1].data - values[i].data)

            policy_loss = policy_loss - log_action_probs[i] * advantage - 0.01 * entropies[i]

        loss = policy_loss + 0.5 * value_loss

        optimizer.zero_grad()

        loss.backward()
        nn.utils.clip_grad_norm(model.parameters(), 40)

        optimizer.step()
        update += 1


import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import matplotlib.pyplot as plt
import pickle

class CredFlow(object):
    # class properties
    client_id = None
    client_secret = None
    spotify_obj = None

    def __init__(self, client_id, client_secret):
        self.client_id = client_id
        self.client_secret = client_secret
        self.spotify_obj = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=self.client_id, client_secret=self.client_secret))
        self.createSpotifyObj()

    def createSpotifyObj(self):
        # obtains authentication object from Spotify API
        self.spotify_obj = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=self.client_id, client_secret=self.client_secret))

    def oneHitWonderChecker(self, artist, views_lim=0.5):
        query = self.spotify_obj.search("artist:" + artist, type="artist")
        items = query["artists"]["items"]
        if len(items) > 0:
            artist = items[0]
            followers = artist["followers"]["total"]
            id = artist["id"]
            genres = artist["genres"]
            popularity = artist["popularity"]

            response = self.spotify_obj.artist_top_tracks(id)
            top_tracks = []
            for track in response["tracks"]:
                top_tracks.append([track["name"], track["popularity"]])

            print(top_tracks)

            maximum = 0
            for i in range(len(top_tracks)):
                if top_tracks[i][1] > top_tracks[maximum][1]:
                    maximum = i

            maxSum = top_tracks[maximum][1]
            popSum = 0
            for track in top_tracks:
                popSum += track[1]

            if maxSum/popSum >= views_lim:
                return True
            else:
                return False

        else:
            raise Exception("Artist not found")

    def get_playlist_tracks(self, playlist_id):
        # gets all the tracks within a playlist { {"name":"id"}, ... }
        # extra steps necessary because request limit for the method is 100

        results = self.spotify_obj.playlist_items(playlist_id)
        tracks = results["items"]
        track_info = {}

        while results["next"]:
            results = self.spotify_obj.next(results)
            tracks.extend(results["items"])

        for x in range(results["total"]):
            track_info[tracks[x]["track"]["name"]] = tracks[x]["track"]["id"]

        return track_info

    def topFiftySplit(self):
        # can be sorted by explicit, artists, artist genre
        query = self.spotify_obj.search("Global Top 50", type="playlist")
        playlist_id = query["playlists"]["items"][0]["id"]
        tracks = self.get_playlist_tracks(playlist_id)

        all_artists = []
        freq = {}
        for track in tracks.keys():
            query = self.spotify_obj.search(track, type="track")
            artists = query["tracks"]["items"][0]["artists"]
            for i in range(len(artists)):
                all_artists.append(artists[i]["name"])
                freq[artists[i]["name"]] = 0

        for artist in all_artists:
            freq[artist] += 1

        labels = freq.keys()
        sizes = freq.values()

        patches, texts = plt.pie(sizes, shadow=True, startangle=90)
        plt.legend(patches, labels, loc="best")
        plt.axis("equal")
        plt.tight_layout()
        plt.show()

    def writeCategories(self):
        response = self.spotify_obj.categories(limit=50)
        categories = {}
        for item in response["categories"]["items"]:
            categories[item["name"]] = item["id"]
            print(categories[item["name"]] + " =  " + item["id"])

        with open("SpotifyData/categories.pickle", "wb") as file:
            pickle.dump(categories, file)

    def exploreCategory(self, category):
        with open("SpotifyData/categories.pickle", "rb") as file:
            categories = pickle.load(file)
            category_id = categories[category]

        category_info = self.spotify_obj.category(category_id=category_id)
        category_pic = category_info["icons"][0]["url"]

        category_playlists_response = self.spotify_obj.category_playlists(category_id=category_id, limit=50)
        category_playlists = {}

        for playlist in category_playlists_response["playlists"]["items"]:
            category_playlists[playlist["name"]] = playlist["id"]

        return [category_pic, category_playlists]

    def writeRecommendations(self):
        recommendations_response = self.spotify_obj.recommendation_genre_seeds()
        recommendations_list = []
        for genre in recommendations_response["genres"]:
            recommendations_list.append(genre)

        with open("SpotifyData/recommendation_seeds.pickle", "wb") as file:
            pickle.dump(recommendations_list, file)

    def generateRecommendations(self, seeds, seed_type="genre"):
        with open("SpotifyData/recommendation_seeds.pickle", "rb") as file:
            recommendations_list = pickle.load(file)

        if seed_type == "genre" and seed_type not in recommendations_list:
            raise ValueError("This genre is not accepted by the Spotify API")

        tracks = []
        if seed_type == "genre":
            tracks = self.spotify_obj.recommendations(seed_genres=seeds, limit=20)

        elif seed_type == "artist":
            id_seeds = []
            for seed in seeds:
                query = self.spotify_obj.search("artist:" + seed, type="artist")
                items = query["artists"]["items"]
                if len(items) > 0:
                    id_seeds.append(items[0]["id"])
            tracks = self.spotify_obj.recommendations(seed_artists=id_seeds, limit=20)

        else:
            id_seeds = []
            for seed in seeds:
                query = self.spotify_obj.search(seed, type="track")
                id_seeds.append(query["tracks"]["items"][0]["id"])
            tracks = self.spotify_obj.recommendations(seed_tracks=id_seeds, limit=20)

        recommended_tracks = []
        for track in tracks["tracks"]:
            artists = [artist["name"] for artist in track["artists"]]
            strArtists = ""
            for artist in artists:
                strArtists += str(artist) + " "

            track_info = {"artist": strArtists, "name": track["name"], "id": track["id"], "image": track["album"]["images"][0]}
            recommended_tracks.append(track_info)

        return recommended_tracks

    def audioFeatures(self, tracks):
        tracks_features = []
        for track in tracks:
            query = self.spotify_obj.search(track, type="track")
            track_id = query["tracks"]["items"][0]["id"]
            features = self.spotify_obj.audio_features([track_id])[0]
            features["name"] = track
            tracks_features.append(features)
        return tracks_features


from gevent.event import Event
from gevent.lock import RLock
from freenas.utils.query import wrap

class CacheStore(object):
    class CacheItem(object):
        def __init__(self):
            self.valid = Event()
            self.data = None

    def __init__(self):
        self.lock = RLock()
        self.store = {}

    def __getitem__(self, item):
        return self.get(item)

    def put(self, key, data):
        with self.lock:
            item = self.store[key] if key in self.store else self.CacheItem()
            item.data = data
            item.valid.set()

            if key not in self.store:
                self.store[key] = item
                return True

            return False

    def get(self, key, default=None, timeout=None):
        item = self.store.get(key)
        if item:
            item.valid.wait(timeout)
            return item.data

        return default

    def remove(self, key):
        with self.lock:
            if key in self.store:
                del self.store[key]
                return True

            return False

    def exists(self, key):
        return key in self.store

    def rename(self, oldkey, newkey):
        with self.lock:
            obj = self.get(oldkey)
            self.put(newkey, obj)
            self.remove(oldkey)

    def is_valid(self, key):
        item = self.store.get(key)
        if item:
            return item.valid.is_set()

        return False

    def invalidate(self, key):
        with self.lock:
            item = self.store.get(key)
            if item:
                item.valid.clear()

    def itervalid(self):
        for key, value in list(self.store.items()):
            if value.valid.is_set():
                yield (key, value.data)

    def validvalues(self):
        for value in list(self.store.values()):
            if value.valid.is_set():
                yield value.data

    def query(self, *filter, **params):
        return wrap(list(self.validvalues())).query(*filter, **params)

class EventCacheStore(CacheStore):
    def __init__(self, dispatcher, name):
        super(EventCacheStore, self).__init__()
        self.dispatcher = dispatcher
        self.ready = False
        self.name = name

    def put(self, key, data):
        ret = super(EventCacheStore, self).put(key, data)
        if self.ready:
            self.dispatcher.emit_event('{0}.changed'.format(self.name), {
                'operation': 'create' if ret else 'update',
                'ids': [key]
            })

        return ret

    def remove(self, key):
        ret = super(EventCacheStore, self).remove(key)
        if ret and self.ready:
            self.dispatcher.emit_event('{0}.changed'.format(self.name), {
                'operation': 'delete',
                'ids': [key]
            })

        return ret

    def remove_predicate(self, predicate):
        result = []
        for k, v in self.itervalid():
            if predicate(v):
                self.remove(k)
                result.append(k)

        return result

    def rename(self, oldkey, newkey):
        super(EventCacheStore, self).rename(oldkey, newkey)
        if self.ready:
            self.dispatcher.emit_event('{0}.changed'.format(self.name), {
                'operation': 'rename',
                'ids': [[oldkey, newkey]]
            })

    def propagate(self, event, callback=None):
        if event['operation'] == 'delete':
            for i in event['ids']:
                self.remove(i)

            return

        if event['operation'] == 'rename':
            for o, i in event['ids']:
                self.rename(o, i)

            return

        if event['operation'] in ('create', 'update'):
            for i in event['entities']:
                obj = callback(i) if callback else i
                if not obj:
                    continue
                self.put(obj['id'], obj)

    def populate(self, collection, callback=None):
        for i in collection:
            obj = callback(i) if callback else i
            if obj is not None:
                self.put(obj['id'], obj)


from PyQt4 import QtCore, QtGui
import gameboard
import random
import sys

class AppWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(AppWidget, self).__init__(parent)
        self.setGeometry(0,0,825,625)
        horizontalLayout = QtGui.QHBoxLayout()
        self.gameboardGraphicView = QtGui.QGraphicsView()
        self.gameboardGraphicScene = game_board(self.gameboardGraphicView)
        self.gameboardGraphicView.setScene(self.gameboardGraphicScene)
        self.gameboardGraphicView.setSceneRect(0,0,800,600)
        self.gameboardGraphicView.setSceneRect(self.gameboardGraphicScene.sceneRect())

        '''
        self.testRect = QtGui.QGraphicsRectItem(20,20,20,20)
        self.testRect.setBrush(QtGui.QBrush(QtCore.Qt.darkCyan))
        '''

        '''
        self.testRect = playing_card_graphic(3,6)
        self.testRect2 = playing_card_graphic(1,0)
        print(self.testRect.x(),self.testRect.y())
        self.testRect2.setX(64)
        self.testRect2.setY(64)

        self.color_balance = [i%5 for i in range(48)]+[random.randint(0,4)]
        self.color_balance = [i//12 for i in range(48)]+[random.randint(0,3)]
        self.num_bal = [i%6+1 for i in range(48)]+[0]
        self.cards = []
        for i in range(49):
            self.cards.append([self.color_balance.pop(),self.num_bal.pop()])
        print(self.cards)

        random.shuffle(self.cards)

        self.all_cards = [playing_card_graphic(i[0],i[1]) for i in self.cards]

        for i,j in enumerate(self.all_cards):
            spacer = 8
            j.setX(i%7*(64+spacer)+spacer)
            j.setY(i//7*(64+spacer)+spacer)
            self.gameboardGraphicScene.addItem(j)

        '''
        #self.gameboardGraphicScene.addItem(self.testRect)
        #self.gameboardGraphicScene.addItem(self.testRect2)
        print(self.gameboardGraphicScene.get_coordinate_dictionary_for_board())

        # set style change handler
        horizontalLayout.addWidget(self.gameboardGraphicView)
        self.setLayout(horizontalLayout)

    # handler for changing style
    def handleStyleChanged(self, style):
        QtGui.qApp.setStyle(style)

class game_board_background(QtGui.QGraphicsItemGroup):
    def __init__(self, parent=None, scene=None):
        QtGui.QGraphicsItemGroup.__init__(self,parent,scene)
        background_rect = QtGui.QGraphicsRectItem()
        background_rect.setBrush(QtGui.QBrush(QtGui.QColor(80,60,0)))
        background_rect.setRect(0,0,64*7+8*8,64*7+8*8)

        self.addToGroup(background_rect)

class player_card_dock(QtGui.QGraphicsItemGroup):
    def __init__(self, parent=None, scene=None):
        QtGui.QGraphicsItemGroup.__init__(self,parent,scene)
        self.card_dock_back = QtGui.QGraphicsRectItem()
        self.card_dock_back.setBrush(QtGui.QBrush(QtGui.QColor(60,45,0)))

        self.card_dock_back.setRect(0,0,64*8,64*2)

        self.addToGroup(self.card_dock_back)

class game_board(QtGui.QGraphicsScene):
    def __init__(self, parent=None):
        QtGui.QGraphicsScene.__init__(self,parent)
        self.main_board = gameboard.gameboard(0,1)
        self.main_board.set_initial_board()
        self.cards_on_board_dictionary={i:None for i in range(self.main_board.get_board_side_length())}
        self.setSceneRect(QtCore.QRectF())
        background = game_board_background()
        background.setX(0)
        background.setY(0)

        card_dock = player_card_dock()
        card_dock.setY(64*8+8)

        self.addItem(background)
        self.addItem(card_dock)
        self.layout_board()

    def get_coordinate_dictionary_for_board(self):
        board_length = self.main_board.get_board_side_length()
        piece_width = 64
        spacer = 8
        coordinates = {}
        for i in range(0,int(board_length**2)):
            coordinates[i]=[i % board_length*(piece_width+spacer)+spacer, i//board_length*(piece_width+spacer)+spacer]
        return coordinates

    def drop_card_on_board(self, card):
        x=card.x()
        y=card.y()
        coord_dict = self.get_coordinate_dictionary_for_board()
        temp_dict = {}
        for i in coord_dict.keys():
            temp_dict[i] = ((coord_dict[i][0]-x)**2+(coord_dict[i][1]-y)**2)**.5

        smallest = [temp_dict[0],0]
        for i in temp_dict.keys():
            if temp_dict[i] < smallest[0]:
                smallest = [temp_dict[i],i]
        if self.main_board.is_legal_to_anchor_card(card.get_card_class(),self.main_board.get_coordinates_by_index(smallest[1])):
            card.setX(coord_dict[smallest[1]][0])
            card.setY(coord_dict[smallest[1]][1])
        else:
            old_pos = card.old_position
            card.setX(old_pos[0])
            card.setY(old_pos[1])
        card.set_click_off()

    def layout_board(self):
        cards_on_table = self.main_board.get_dictionary_of_slot_occupants()
        coordinates = self.get_coordinate_dictionary_for_board()
        for i in cards_on_table:
            if cards_on_table[i]:
                card_info = cards_on_table[i].get_info(0)
                if card_info[0] == 12:
                    card_info[0] = -1
                #self.cards_on_board_dictionary[i] = playing_card_graphic(card_info[1],card_info[0]+1,scene=self)
                self.cards_on_board_dictionary[i] = playing_card_graphic(card=cards_on_table[i],scene=self)
                self.cards_on_board_dictionary[i].setX(coordinates[i][0])
                self.cards_on_board_dictionary[i].setY(coordinates[i][1])
                #if i % 2 == 0:
                self.cards_on_board_dictionary[i].set_click_off()
                self.addItem(self.cards_on_board_dictionary[i])

class playing_card_graphic(QtGui.QGraphicsItem):
    def __init__(self, suit=0, value=0,card=None,parent=None,scene=None):
        if card:
            self.card = card
            card_info = self.card.get_info(0)
            if card_info[0] == 12:
                value = 0
            else:
                value = card_info[0]+1
            suit = card_info[1]

        QtGui.QGraphicsItem.__init__(self,parent,scene)
        self.top_scene = scene
        self.click_disabled = False
        self.suit = suit % 5
        red = QtGui.QColor(175,0,0)
        blue = QtGui.QColor(48,89,214)
        blue = QtGui.QColor(25,117,209)
        green = QtGui.QColor(0,164,0)
        green = QtGui.QColor(25,117,25)
        #green = QtGui.QColor(48,131,48)
        #green = QtGui.QColor(0,255,0)
        yellow = QtGui.QColor(192,200,0)
        yellow = QtGui.QColor(153,153,0)
        violet = QtGui.QColor(92,0,230)
        self.suit_color=[QtCore.Qt.red, QtCore.Qt.darkCyan, QtCore.Qt.green, QtCore.Qt.yellow]
        self.back_pix_maps=[QtGui.QPixmap('./cards/card_template_red_square.png'),
                            QtGui.QPixmap('./cards/card_template_blue_square.png'),
                            QtGui.QPixmap('./cards/card_template_green_square.png'),
                            QtGui.QPixmap('./cards/card_template_yellow_square.png'),
                            QtGui.QPixmap('./cards/card_template_violet_square.png')]

        self.suit_color=[red,blue,green,yellow,violet]
        '''
        self.background = QtGui.QGraphicsRectItem(0,0,64,64,self)
        self.background.setBrush(QtGui.QBrush(self.suit_color[self.suit]))
        '''
        self.background = QtGui.QGraphicsPixmapItem(self)
        self.background.setPixmap(self.back_pix_maps[self.suit])

        #self.whiteLayerBackground = QtGui.QGraphicsRectItem(8,8,48,48,self)
        #self.whiteLayerBackground.setBrush(QtGui.QBrush(QtCore.Qt.black))
        self.number = QtGui.QGraphicsTextItem(str(value),self)
        self.number.setDefaultTextColor(self.suit_color[self.suit])
        self.number.setX(17)
        self.number.setY(5)
        self.font = QtGui.QFont('Ubuntu Mono',36,1)
        self.number.setFont(self.font)
        self.setAcceptHoverEvents(True)
        self.old_position = [self.x(),self.y()]

    def get_card_class(self):
        return self.card

    def set_click_off(self):
        self.click_disabled = True

    def set_click_on(self):
        self.click_disabled = False

    def boundingRect(self):
        return QtCore.QRectF(0,0,64,64)

    def hoverEnterEvent(self, event):
        if self.click_disabled:
            return
        cursor = QtGui.QCursor(QtCore.Qt.OpenHandCursor)
        QtGui.QApplication.instance().setOverrideCursor(cursor)

    def hoverLeaveEvent(self,event):
        QtGui.QApplication.instance().restoreOverrideCursor()

    def mouseMoveEvent(self,event):
        if self.click_disabled:
            return
        new_position = event.scenePos()

        #old_position = self.scenePos()
        #new_position.setY(old_position.y())
        self.setX(new_position.x()-32)
        self.setY(new_position.y()-32)

    def mousePressEvent(self, event):
        if self.click_disabled:
            return
        self.old_position = [self.x(),self.y()]
        self.setZValue(1)
        new_position = event.scenePos()
        self.setX(new_position.x()-32)
        self.setY(new_position.y()-32)

    def mouseReleaseEvent(self, event):
        if self.click_disabled:
            return
        self.setZValue(1)
        if self.top_scene:
            print(self.top_scene.drop_card_on_board(self))
        print(self.x(),self.y())

    def get_card(self):
        return self

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    widgetApp = AppWidget()
    widgetApp.show()
    sys.exit(app.exec_())

test (Owner-PC-Windows's conflicted copy 2016-03-11).py


#!/usr/bin/env python
# coding: utf-8

# each api key belongs to a project attached to a different billing account. each billing account gets $200/month credit.
#
# from: https://cloud.google.com/maps-platform/pricing/sheet/
#
# up to 40,000 calls for free monthly
#
# From https://developers.google.com/maps/documentation/elevation/intro
#
# > URLs must be properly encoded to be valid and are limited to 8192 characters for all web services. Be aware of this limit when constructing your URLs. Note that different browsers, proxies, and servers may have different URL character limits as well.

# In[ ]:

import json
import os
import osmnx as ox
import pandas as pd

print('osmnx version', ox.__version__)

# In[ ]:

# load google maps elevation api keys
with open('keys.json') as f:
    data = json.load(f)
    api_keys = data['api_keys']

# load configs
with open('../../config.json') as f:
    config = json.load(f)

batch_size = 400
max_url_length = 8192
max_requests = 39000

# input data
nodes_folder = config['elevation_google_nodeclusters_path'] #clustered nodes with xy data
elevations_path = config['elevation_google_elevations_path'] #any preexisting elevations data from previous runs

# output data
urls_path = config['elevation_google_urls_path'] #urls we create
nodestoget_path = config['elevation_google_nodestoget_path'] #node osmids in same order as their coords appear in batched lists of urls

# In[ ]:

def get_url(cluster):
    # google maps elevation API endpoint
    url_template = 'https://maps.googleapis.com/maps/api/elevation/json?locations={}&key='
    locations = '|'.join(cluster['latlng'])
    return url_template.format(locations)

# In[ ]:

def get_nodes(file):
    # load clustered nodes from file
    nodes = pd.read_csv(f'{nodes_folder}/{file}', index_col='osmid')

    # create latlng column rounded to 5 decimals (ie, 1-meter precision)
    nodes['latlng'] = nodes.apply(lambda row: '{:.5f},{:.5f}'.format(row['y'], row['x']), axis=1)

    return nodes.drop(columns=['x', 'y'])

# In[ ]:

print(ox.ts(), 'loading nodes from graph files:', end=' ')

# load clustered nodes
nodes = pd.DataFrame()
for file in sorted(os.listdir(nodes_folder)):
    print(file.split('-')[0], end=' ', flush=True)
    nodes = nodes.append(other=get_nodes(file), ignore_index=False, verify_integrity=False)

print('')
print(ox.ts(), f'load {len(nodes)} total nodes')

# In[ ]:

# remove any duplicate nodes that appeared in multiple graphs
nodes = nodes.sort_index()
nodes = nodes.loc[~nodes.index.duplicated(keep='first')]
assert nodes.index.is_unique
print(ox.ts(), 'keep {} unique nodes'.format(len(nodes)))

# In[ ]:

# if this file already exists, we have already downloaded some elevation data
# so drop any nodes that we already downloaded elevation for
if os.path.exists(elevations_path):
    preexisting = pd.read_csv(elevations_path).set_index('osmid')
    existing_elevations = preexisting[pd.notnull(preexisting['elev'])]
    print(ox.ts(), 'found', len(existing_elevations), 'preexisting node elevations at', elevations_path)
    labels = existing_elevations.reindex(nodes.index).dropna().index
    nodes = nodes.drop(labels=labels)
print(ox.ts(), 'retained {} nodes we currently lack elevation data for'.format(len(nodes)))

# In[ ]:

# resort by cluster so nodes and urls will be in same order
nodes = nodes.sort_values('cluster')
clusters = nodes.groupby('cluster', sort=True)
print(ox.ts(), 'there are {} clusters'.format(len(clusters)))

# create one url for each cluster of nodes
urls = {label:get_url(cluster) for label, cluster in clusters}
urls = pd.Series(urls)
print(ox.ts(), 'constructed {} urls'.format(len(urls)))

# In[ ]:

# attach an api key to each url
keys_list = []
for key in api_keys:
    keys_list.extend([key] * max_requests)

keys = pd.Series(data=keys_list[0:len(urls)], index=urls.index)
assert len(keys) == len(urls)

urls = urls + keys

# In[ ]:

# ensure that all urls are less than the max allowed length
assert (urls.map(len) < max_url_length).all()
assert len(nodes['cluster'].unique()) == len(urls)

# In[ ]:

# save urls to disk
urls = urls.rename('url')
urls.index.rename('cluster', inplace=True)
urls.to_csv(urls_path, index=True, encoding='utf-8')
print(ox.ts(), 'saved {} urls at {}'.format(len(urls), urls_path))

# In[ ]:

key_counts = keys.value_counts().reindex(api_keys).fillna(0).astype(int)
for key, count in key_counts.iteritems():
    print(ox.ts(), 'key ~{} has {} urls'.format(key[-6:], count))

# In[ ]:

# save nodes that we are going to request elevations for to disk in order
# we will use this order to match the url batch requests back to their nodes
nodes.to_csv(nodestoget_path, index=True, encoding='utf-8')
print(ox.ts(), 'saved {} node osmids at {}'.format(len(nodes), nodestoget_path))

# In[ ]:


from django.conf import settings
from django.db import connection
from django.test import TransactionTestCase
from django.test.client import RequestFactory
from cerebromix.tenant_schemas.middleware import TenantMiddleware
from cerebromix.tenant_schemas.tests.models import Tenant
from cerebromix.tenant_schemas.utils import get_public_schema_name

class RoutesTestCase(TransactionTestCase):
    @classmethod
    def setUpClass(cls):
        settings.TENANT_APPS = ('tenant_schemas',
                                'django.contrib.contenttypes',
                                'django.contrib.auth', )

    def setUp(self):
        self.factory = RequestFactory()
        self.tm = TenantMiddleware()

        # settings needs some patching
        settings.TENANT_MODEL = 'cerebromix.tenant_schemas.Tenant'

        # add the public tenant
        self.public_tenant_domain = 'test.com'
        self.public_tenant = Tenant(domain_url=self.public_tenant_domain,
                                    schema_name='public')
        self.public_tenant.save()

        # add a test tenant
        self.tenant_domain = 'tenant.test.com'
        self.tenant = Tenant(domain_url=self.tenant_domain, schema_name='test')
        self.tenant.save()

    def tearDown(self):
        """
        Delete all tenant schemas. Tenant schema are not deleted
        automatically by django.
        """
        connection.set_schema_to_public()
        do_not_delete = [get_public_schema_name(), 'information_schema']
        cursor = connection.cursor()

        # Use information_schema.schemata instead of pg_catalog.pg_namespace in
        # utils.schema_exists, so that we only "see" schemas that we own
        cursor.execute('SELECT schema_name FROM information_schema.schemata')

        for row in cursor.fetchall():
            if not row[0].startswith('pg_') and row[0] not in do_not_delete:
                print("Deleting schema %s" % row[0])
                cursor.execute('DROP SCHEMA %s CASCADE' % row[0])

        Tenant.objects.all().delete()

    def test_tenant_routing(self):
        """
        request path should not be altered
        """
        request_url = '/any/request/'
        request = self.factory.get('/any/request/',
                                   HTTP_HOST=self.tenant_domain)
        self.tm.process_request(request)

        self.assertEquals(request.path_info, request_url)

        # request.tenant should also have been set
        self.assertEquals(request.tenant, self.tenant)

    def test_public_schema_routing(self):
        """
        request path should not be altered
        """
        request_url = '/any/request/'
        request = self.factory.get('/any/request/',
                                   HTTP_HOST=self.public_tenant_domain)
        self.tm.process_request(request)

        self.assertEquals(request.path_info, request_url)

        # request.tenant should also have been set
        self.assertEquals(request.tenant, self.public_tenant)


#!/usr/bin/python3
# coding: utf-8

'''
testing LoadCompressPrime/store_prime
'''

try:
    # larger and slower
    from sip import LoadCompressPrime as StorePrime
    print('use **LoadCompressPrime**')
except ImportError:
    # smaller and quicker
    from store_prime import StorePrime
    print('use **store_prime**')

def gold_bach(val):
    ''' it could be found if val < 4 * 10^17 '''
    print(f'test: {val}')
    max_len = 12

    with StorePrime() as sp:
        ret = sp.get_primes_less_than(val)
        if isinstance(ret, list):
            if len(ret) > max_len:
                print(f"got a list which size is more than {max_len}")
            else:
                print(ret)

def main():
    ''' main '''
    gold_bach(500)

if __name__ == '__main__':
    main()


from rllab.algos.base import RLAlgorithm

import numpy as np

from rllab.misc.special import discount_cumsum
from rllab.sampler import parallel_sampler, stateful_pool
from rllab.sampler.utils import rollout
from rllab.core.serializable import Serializable
import rllab.misc.logger as logger
import rllab.plotter as plotter

def _worker_rollout_policy(G, args):
    sample_std = args["sample_std"].flatten()
    cur_mean = args["cur_mean"].flatten()
    K = len(cur_mean)
    params = np.random.standard_normal(K) * sample_std + cur_mean
    G.policy.set_param_values(params)
    path = rollout(G.env, G.policy, args["max_path_length"])
    path["returns"] = discount_cumsum(path["rewards"], args["discount"])
    path["undiscounted_return"] = sum(path["rewards"])
    if args["criterion"] == "samples":
        inc = len(path["rewards"])
    elif args["criterion"] == "paths":
        inc = 1
    else:
        raise NotImplementedError
    return (params, path), inc

class CEM(RLAlgorithm, Serializable):
    def __init__(
            self,
            env,
            policy,
            n_itr=500,
            max_path_length=500,
            discount=0.99,
            init_std=1.,
            n_samples=100,
            batch_size=None,
            best_frac=0.05,
            extra_std=1.,
            extra_decay_time=100,
            plot=False,
            **kwargs
    ):
        """
        :param n_itr: Number of iterations.
        :param max_path_length: Maximum length of a single rollout.
        :param batch_size: # of samples from trajs from param distribution, when this
        is set, n_samples is ignored
        :param discount: Discount.
        :param plot: Plot evaluation run after each iteration.
        :param init_std: Initial std for param distribution
        :param extra_std: Decaying std added to param distribution at each iteration
        :param extra_decay_time: Iterations that it takes to decay extra std
        :param n_samples: #of samples from param distribution
        :param best_frac: Best fraction of the sampled params
        :return:
        """
        Serializable.quick_init(self, locals())
        self.env = env
        self.policy = policy
        self.batch_size = batch_size
        self.plot = plot
        self.extra_decay_time = extra_decay_time
        self.extra_std = extra_std
        self.best_frac = best_frac
        self.n_samples = n_samples
        self.init_std = init_std
        self.discount = discount
        self.max_path_length = max_path_length
        self.n_itr = n_itr

    def train(self):
        parallel_sampler.populate_task(self.env, self.policy)
        if self.plot:
            plotter.init_plot(self.env, self.policy)

        cur_std = self.init_std
        cur_mean = self.policy.get_param_values()
        # K = cur_mean.size
        n_best = max(1, int(self.n_samples * self.best_frac))

        for itr in range(self.n_itr):
            # sample around the current distribution
            extra_var_mult = max(1.0 - itr / self.extra_decay_time, 0)
            sample_std = np.sqrt(np.square(cur_std) + np.square(self.extra_std) * extra_var_mult)
            if self.batch_size is None:
                criterion = 'paths'
                threshold = self.n_samples
            else:
                criterion = 'samples'
                threshold = self.batch_size
            infos = stateful_pool.singleton_pool.run_collect(
                _worker_rollout_policy,
                threshold=threshold,
                args=(dict(cur_mean=cur_mean,
                          sample_std=sample_std,
                          max_path_length=self.max_path_length,
                          discount=self.discount,
                          criterion=criterion),)
            )
            xs = np.asarray([info[0] for info in infos])
            paths = [info[1] for info in infos]

            fs = np.array([path['returns'][0] for path in paths])
            print((xs.shape, fs.shape))
            best_inds = (-fs).argsort()[:n_best]
            best_xs = xs[best_inds]
            cur_mean = best_xs.mean(axis=0)
            cur_std = best_xs.std(axis=0)
            best_x = best_xs[0]
            logger.push_prefix('itr #%d | ' % itr)
            logger.record_tabular('Iteration', itr)
            logger.record_tabular('CurStdMean', np.mean(cur_std))
            undiscounted_returns = np.array([path['undiscounted_return'] for path in paths])
            logger.record_tabular('AverageReturn',
                                  np.mean(undiscounted_returns))
            logger.record_tabular('StdReturn',
                                  np.mean(undiscounted_returns))
            logger.record_tabular('MaxReturn',
                                  np.max(undiscounted_returns))
            logger.record_tabular('MinReturn',
                                  np.min(undiscounted_returns))
            logger.record_tabular('AverageDiscountedReturn',
                                  np.mean(fs))
            logger.record_tabular('AvgTrajLen',
                                  np.mean([len(path['returns']) for path in paths]))
            logger.record_tabular('NumTrajs',
                                  len(paths))
            self.policy.set_param_values(best_x)
            self.env.log_diagnostics(paths)
            self.policy.log_diagnostics(paths)
            logger.save_itr_params(itr, dict(
                itr=itr,
                policy=self.policy,
                env=self.env,
                cur_mean=cur_mean,
                cur_std=cur_std,
            ))
            logger.dump_tabular(with_prefix=False)
            logger.pop_prefix()
            if self.plot:
                plotter.update_plot(self.policy, self.max_path_length)
        parallel_sampler.terminate_task()


from fastapi import APIRouter, Query, HTTPException
from typing import List

router_custom_app_cascade = APIRouter(
  tags=["custom_app_cascade"],
  prefix="/cascade"
)

## cascading combobox multiple selection in vitevue example web application

countriesEastMasterList = {
  "Asia": ["Russia", "Japan", "Burma", "Indonesia", "Afghanistan"],
  "Europe": ["Russia", "Germany", "France", "Poland", "Sweden", "Italy"],
  "Africa": ["Egypt", "Nigeria", "Kenya", "Liberia"],
  "ME": ["Egypt", "Saudi Arabia", "Afghanistan"],
}

countriesWestMasterList = {
  "NA": ["United States", "Canada"],
  "SA": ["Brazil", "Argentina", "Ecuador"],
}

westCountryStatesMasterList = {
  "United States": ["California", "New York", "Ohio", "Utah", "Texas"],
  "Canada": ["Ontario", "Quebec", "BC", "Alberta"],
  "Brazil": ["B1", "B2"],
  "Argentina": ["A1", "A2", "A3"],
  "Ecuador": ["EC1", "EC2"],
}

@router_custom_app_cascade.get("/continents")
def get_continents():
  continents = ["Asia", "Europe", "NA", "SA", "Africa", "ME"]
  continents.sort()
  return continents

@router_custom_app_cascade.get("/countries")
def get_coountries_by_continents(continents: str = Query(None)):
  countriesEastList = []
  countriesWestList = []
  try:
    continents_list = continents.split(",")
    for continent in continents_list:
      countries = countriesEastMasterList.get(continent)
      if countries != None:
        countriesEastList = countriesEastList + countries
      countries = countriesWestMasterList.get(continent)
      if countries != None:
        countriesWestList = countriesWestList + countries
    return {
      "countriesEastList": countriesEastList,
      "countriesWestList": countriesWestList
    }
  except Exception as e:
    raise HTTPException(status_code=500, detail=str(e))

@router_custom_app_cascade.get("/states")
def get_states_by_countries(countries: str = Query(None)):
  states_list = []
  try:
    countries_list = countries.split(",")
    for country in countries_list:
      states = westCountryStatesMasterList.get(country)
      if states != None:
        states_list = states_list + states
    # states_list.sort()
    return states_list
  except Exception as e:
    raise HTTPException(status_code=500, detail=str(e))


#!/usr/bin/python3
#指定使用python3

#执行方法：python3 applyPatch.py Q1-kGenProg Chart 8 0
#添加文件的可执行权限之后可以直接使用./applyPatch.py

#Linux系统
# 对于每个工具可修复的每个bug产生的每个补丁：将补丁应用到源项目中
#进入补丁目录，找到补丁中所有以Java结尾的文件，循环处理每个Java文件：
#**1.以“.”分隔文件名，一段是一级目录，最后一个是文件名，根据分隔出来的结果到源项目中找到该文件
#**2.删除bug项目中的文件
#**3.将补丁中该文件复制到源项目中，并修改名字
#**4.补丁应用完成后，编译项目-----》放shell脚本中

#参数：先导入系统接口模块import sys，再调用系统命令行参数sys.argv，sys.argv[1]是命令行的第一个参数。用形参来接收sys.argv[1]
#1.repair tool: Q1-kGenProg,kGenProg-Jaccard,kGenProg-Tarantula,kGenProg-file
#2.project:Chart
#3.bugId:1...
#4.seed:0-9

#编写：2021.4.21

import sys
import os
from pathlib import Path

#删除bug项目中的文件f(绝对路径)
def delFile(f,file_name):
    #判断文件是否存在
    if(os.path.exists(f)):
        #删除该文件
        os.remove(f)
        print (file_name+'文件删除成功')
    else:
        print (file_name+'文件不存在！')

#将bug项目中的文件替换为补丁中对应的文件
def applyPatch(patchdir,bugdir):
        for parentdir,dirname,filenames in os.walk(patchdir):
                #1.获取补丁子目录下所有的Java文件(not null)
                if len(dirname) != 0:
                        for pdir,dname,fnames in os.walk(patchdir+"/"+dirname[0]):
                                for filename in fnames:
                                        #循环处理每一个Java文件
                                        if os.path.splitext(filename)[1]=='.java':#java文件
                                                print("******deal with file: ********",filename)
                                                file = []
                                                bugP = ""
                                                for i in range(len(filename.split('.'))): #"."分割文件名
                                                        file.append(filename.split('.')[i])
                                                for j in range(len(file)-2): #文件名和java后缀排除
                                                        #拼接路径
                                                        bugP += os.path.join("/"+file[j])
                                                #print(bugP)

                                                #源bug项目中该文件所在目录和文件
                                                fileP =  bugdir+""+bugP
                                                fname = file[-2] +"."+ file[-1]

                                                #文件的绝对path
                                                f = fileP+"/"+fname
                                                #print(f)
                                                #2.删除源程序file
                                                delFile(f,fname)

                                                #3.将补丁中该文件复制到源项目中，并修改名字
                                                s = 'cp '+patchdir+'/'+dirname[0]+'/'+filename+' '+fileP
                                                print(s)
                                                os.system('cp '+patchdir+'/'+dirname[0]+'/'+filename+' '+fileP)
                                                #修改文件名(旧,新)
                                                s = 'mv '+fileP+'/'+filename+' '+fileP+'/'+fname
                                                print(s)
                                                #os.rename(fileP+'/'+filename,fileP+'/'+fname)
                                                os.system('mv '+fileP+'/'+filename+' '+fileP+'/'+fname)

#主函数
if __name__=="__main__":
    #print("main")
    #接收命令行的参数
    tool = sys.argv[1]
    project = sys.argv[2]
    bugId = sys.argv[3]
    seed = sys.argv[4]
    src = sys.argv[5]
    #print(tool)

    #补丁的位置
    patchdir = "/home/rtx/repair/d4j/"+project+"/"+bugId+"b/"+tool+"/patch"+seed
    #print(patchdir)
    #bug项目中源文件位置(不同项目会有所不同)  #src/java   source
    bugdir = "/home/rtx/repair/d4j/patch/"+tool+"/"+project+"/"+bugId+"b-"+seed+"/"+src
    #print(bugdir)

    #调用函数，将补丁应用到源bug项目中
    applyPatch(patchdir,bugdir)

    #4.补丁应用完成后，编译项目
    #os.system('cd '+bugdir)
    #os.system('defects4j compile')


"""Package information"""

__project__ = "python-gnutls"
__summary__ = "Python wrapper for the GnuTLS library"
__webpage__ = "https://github.com/AGProjects/python-gnutls"

__version__ = "3.1.3"

__author__ = "Dan Pascu"
__email__ = "dan@ag-projects.com"

__license__ = "LGPL"
__copyright__ = "Copyright 2006-2020 {}".format(__author__)


import asyncio
import aiohttp

session = aiohttp.ClientSession()

async def fetch(method, url, **kwargs):
    result = kwargs.get('result')
    async with session.request(method, url, **kwargs) as resp:
        if result == 'text':
            return await resp.text()
        elif result == 'json':
            return await resp.json()

async def get(url, params=None, headers=None, verify=None, timeout=60, result='text'):
    try:
        return await fetch('GET', url, params=params, headers=headers, verify=verify, timeout=timeout,
                           result=result)
    except Exception as e:
        raise e
    finally:
        tear_down()

async def post(self, url, data=None, params=None, headers=None, verify=None, timeout=60, **kwargs):
    try:
        return await self.fetch('POST', url, data=data, params=params, headers=headers, verify=verify,
                                timeout=timeout, **kwargs)
    except Exception as e:
        raise e
    finally:
        self.tear_down()

def tear_down():
    session.close()

def get_province():
    url = 'https://s.map.qq.com/jsonEditor/config/xpconfig/xpconfig.js'


import os
import numpy as np
from scipy import sparse
from sklearn import preprocessing
from numpy.linalg import norm
from sklearn.datasets import load_svmlight_file
from download import download
from bz2 import BZ2Decompressor

def download_finance(path):
    url = "https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets" \
        "/regression/log1p.E2006.train.bz2"
    path = download(url, path, replace=False)
    return path

def decompress_finance(compressed_path, decompressed_path):
    decompressor = BZ2Decompressor()
    with open(decompressed_path, "wb") as f, open(compressed_path, "rb") as g:
        f.write(decompressor.decompress(g.read()))

def preprocess_finance(decompressed_path, X_path, y_path):
    """Normalization performed:
    - X with only columns with >= 3 non zero elements, norm-1 columns, and
      a constant column to fit intercept
    - y centered and set to norm 1"""

    n_features_total = 4272227
    with open(decompressed_path, 'rb') as f:
        X, y = load_svmlight_file(f, n_features_total)
        X = sparse.csc_matrix(X)

        NNZ = np.diff(X.indptr)  # number of non zero elements per feature
        # keep only features with >=3 non zero values
        X_new = X[:, NNZ >= 3]

        # add constant feature to fit intercept
        X_new = preprocessing.add_dummy_feature(X_new, 1.)
        # set all feature norms to 1
        X_new = preprocessing.normalize(X_new, axis=0)
        # center y
        y -= np.mean(y)
        # normalize y to get a first duality gap of 0.5
        y /= norm(y, ord=2)

        # very important for sparse/sparse dot products: have sorted X.indices
        X_new.sort_indices()
        sparse.save_npz(X_path, X_new)
        np.save(y_path, y)

if __name__ == "__main__":
    compressed_path = "./data/log1p.E2006.train.bz2"
    download_finance(compressed_path)

    decompressed_path = "./data/log1p.E2006.train"
    if not os.path.isfile(decompressed_path):
        decompress_finance(compressed_path, decompressed_path)

    y_path = "./data/finance_target_preprocessed"
    X_path = "./data/finance_data_preprocessed"

    preprocess_finance(decompressed_path, X_path, y_path)


import random
import re
from datetime import datetime

import lorem
from django.db import connection

from fullstack_challenge.models import Category, Order

mocked_descriptions = re.split(r".[\n]+|\.\s", lorem.text())

mocked_data = [
    [
        "Hidráulica",
        "Alcides",
        "(11) 99999-9999",
        "Imobiliária Sampa",
        "Reparos S.A.",
        datetime(2021, 11, 10),
    ],
    [
        "Infiltração",
        "Moacir",
        "(48) 99999-9999",
        "Imobiliária Floripa",
        "Conserto Ltda",
        datetime(2021, 11, 14),
    ],
    [
        "Elétrica",
        "Ercildes",
        "(11) 99999-9999",
        "Imobiliária Floripa",
        "Conserto Ltda",
        datetime(2021, 11, 15),
    ],
    [
        "Retirada de mobília",
        "Adarci",
        "(11) 99999-9999",
        "Imobiliária Sampa",
        "Reparos S.A.",
        datetime(2021, 11, 15),
    ],
]

for data in mocked_data:
    Order.objects.create(
        category=Category.objects.create(name=data[0]),
        contact_name=data[1],
        contact_phone=data[2],
        agency=data[3],
        company=data[4],
        deadline=data[5],
        description=mocked_descriptions[random.randrange(len(mocked_descriptions))],
    )

# (Optional) Reset Order IDs to begin on 101 instead of 1

with connection.cursor() as cursor:
    cursor.execute(
        "UPDATE SQLITE_SEQUENCE SET SEQ=100 WHERE NAME='fullstack_challenge_order';"
    )

for order in Order.objects.all():
    order.id = None
    order.save()

Order.objects.filter(id__lte=100).delete()


#
# @lc app=leetcode id=2192 lang=python3
#
# [2192] All Ancestors of a Node in a Directed Acyclic Graph
#

# @lc code=start
class Solution:
    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:
        adj_list = defaultdict(list)
        for src, dst in edges:
            adj_list[dst].append(src)

        res = []
        for i in range(n):
            stack = [i]
            ans = []
            visited = set()
            while stack:
                node = stack.pop()
                for neighbor in adj_list[node]:
                    if neighbor not in visited:
                        stack.append(neighbor)
                        visited.add(neighbor)
                        ans.append(neighbor)
            res.append(sorted(ans))
        return res

# @lc code=end


import pytest

from course_discovery.apps.core.utils import serialize_datetime
from course_discovery.apps.publisher.api.utils import (
    serialize_entitlement_for_ecommerce_api, serialize_seat_for_ecommerce_api
)
from course_discovery.apps.publisher.models import Seat
from course_discovery.apps.publisher.tests.factories import CourseEntitlementFactory, SeatFactory

@pytest.mark.django_db
class TestSerializeSeatForEcommerceApi:
    def test_serialize_seat_for_ecommerce_api(self):
        seat = SeatFactory()
        actual = serialize_seat_for_ecommerce_api(seat)
        assert actual['price'] == str(seat.price)
        assert actual['product_class'] == 'Seat'

    def test_serialize_seat_for_ecommerce_api_with_audit_seat(self):
        seat = SeatFactory(type=Seat.AUDIT)
        actual = serialize_seat_for_ecommerce_api(seat)
        expected = {
            'expires': serialize_datetime(seat.calculated_upgrade_deadline),
            'price': str(seat.price),
            'product_class': 'Seat',
            'attribute_values': [
                {
                    'name': 'certificate_type',
                    'value': '',
                },
                {
                    'name': 'id_verification_required',
                    'value': False,
                }
            ]
        }

        assert actual == expected

    @pytest.mark.parametrize('seat_type', (Seat.VERIFIED, Seat.PROFESSIONAL))
    def test_serialize_seat_for_ecommerce_api_with_id_verification(self, seat_type):
        seat = SeatFactory(type=seat_type)
        actual = serialize_seat_for_ecommerce_api(seat)
        expected_attribute_values = [
            {
                'name': 'certificate_type',
                'value': seat_type,
            },
            {
                'name': 'id_verification_required',
                'value': True,
            }
        ]
        assert actual['attribute_values'] == expected_attribute_values

@pytest.mark.django_db
class TestSerializeEntitlementForEcommerceApi:
    def test_serialize_entitlement_for_ecommerce_api(self):
        entitlement = CourseEntitlementFactory()
        actual = serialize_entitlement_for_ecommerce_api(entitlement)
        expected = {
            'price': str(entitlement.price),
            'product_class': 'Course Entitlement',
            'attribute_values': [
                {
                    'name': 'certificate_type',
                    'value': entitlement.mode,
                },
            ]
        }

        assert actual == expected


# make a program which asks you one random addition math question
# and asks you until you get the answer correct
# every time you run the porgram, you should get a new question

# the program should end when you get the answer correct

# clue files:
#  - guessing.py for using import random and using while loops

import random

number1 = random.randint(1, 40)
number2 = random.randint(1, 40)
answer = (number1 + number2)

print("What is", number1, "+", number2, "?")
guess = int(input())

while guess != answer:
    guess = int(input("Try again! You're Wrong :(\n"))

if guess == answer:
    print("Your're correct!")
else:
    print("You're wrong! Try again!")


from approvaltests.combination_approvals import verify_all_combinations
from gilded_rose import GildedRose
from item import Item

def test_what_does_update_items_do():

    name = [
        "Default Item",
        "Aged Brie",
        "Backstage passes to a TAFKAL80ETC concert",
        "Sulfuras, Hand of Ragnaros",
    ]
    sell_in = [-1, 0, 1, 5, 6, 10, 11]
    quality = [-1, 0, 1, 2, 48, 49, 50]

    # result = update_item('foo', 0, 0)
    # verify(result, reporter)
    verify_all_combinations(update_item, [name, sell_in, quality])

def update_item(name, sell_in, quality):
    items = [Item(name, sell_in, quality)]
    gilded_rose = GildedRose(items)
    gilded_rose.update_quality()
    result = ""
    for item in gilded_rose.items:
        result += f"name: {item.name} sell_in: {item.sell_in} quality: {item.quality}"
    return result


from modulo import Pessoa,InterfacePrograma

pessoas_cadastradas,opção=InterfacePrograma.menu()

pessoas=Pessoa(pessoas_cadastradas)

fila_ordenada = pessoas.fila_prioridade_por_idosos(2,60)

pessoas.mostra_tabela_idosos_e_nao_idosos()
pessoas.cria_um_txt_com_as_pessoas_separadas()
if opção == '2':
    pessoas.cria_um_json('+w')

print(f'A fila de prioridade é {fila_ordenada}')


#!/usr/bin/env python3

''' Module defining child class LIFOCache
'''

BaseCaching = __import__('base_caching').BaseCaching

class LIFOCache(BaseCaching):
    '''Inherits from BaseCaching
    '''
    def __init__(self):
        ''' Overloads the child class
        '''
        super().__init__()
        self.key_indexes = []

    def put(self, key, item):
        ''' Put a value item to the Cache_data dict
        based on the key
        '''
        if key and item:
            if len(self.cache_data) >= self.MAX_ITEMS:
                if key in self.cache_data:
                    del self.cache_data[key]
                    self.key_indexes.remove(key)
                else:
                    del self.cache_data[self.key_indexes[self.MAX_ITEMS - 1]]
                    item_discarded = self.key_indexes.pop(self.MAX_ITEMS - 1)
                    print("DISCARD:", item_discarded)

            self.cache_data[key] = item
            self.key_indexes.append(key)

    def get(self, key):
        ''' Get value from the Cache_data dict
        base on the the key
        '''
        if key in self.cache_data:
            return self.cache_data[key]
        return None


import os
import re

from setuptools import setup

os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

with open('README.rst') as f:
    long_description = f.read()

with open('polypie.py') as f:
    author, author_email, version = re.search(
        "__author__ = '(.+) <(.+)>'.+__version__ = '([.0-9a-z]+)'",
        f.read(),
        flags=re.DOTALL
    ).groups()

setup(
    name='polypie',
    version=version,
    py_modules=['polypie'],
    install_requires=['typeguard'],
    python_requires='>=3.5',
    license='BSD License',
    description='Python polymorphic function declaration with obvious syntax',
    long_description=long_description,
    url='https://github.com/un-def/polypie',
    author=author,
    author_email=author_email,
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Topic :: Software Development :: Libraries',
        'Topic :: Utilities',
    ],
)


""" Vlan namespace commands. """

import click
from IPy import IP

from gandi.cli.core.cli import cli
from gandi.cli.core.utils import (
    output_vlan, output_generic, output_iface, output_ip, output_line,
    DatacenterLimited
)
from gandi.cli.core.params import option, pass_gandi, DATACENTER

@cli.group(name='vlan')
@pass_gandi
def vlan(gandi):
    """Commands related to hosting virtual lan networks."""

@vlan.command()
@click.option('--datacenter', type=DATACENTER, default=None,
              help='Filter by datacenter.')
@click.option('--id', help='Display ids.', is_flag=True)
@click.option('--subnet', help='Display subnets.', is_flag=True)
@click.option('--gateway', help='Display gateway.', is_flag=True)
@pass_gandi
def list(gandi, datacenter, id, subnet, gateway):
    """List vlans."""
    output_keys = ['name', 'state', 'dc']
    if id:
        output_keys.append('id')
    if subnet:
        output_keys.append('subnet')
    if gateway:
        output_keys.append('gateway')

    datacenters = gandi.datacenter.list()

    vlans = gandi.vlan.list(datacenter)
    for num, vlan in enumerate(vlans):
        if num:
            gandi.separator_line()
        output_vlan(gandi, vlan, datacenters, output_keys)

    return vlans

@vlan.command()
@click.option('--ip', help='Display ips.', is_flag=True)
@click.argument('resource')
@pass_gandi
def info(gandi, resource, ip):
    """Display information about a vlan."""
    output_keys = ['name', 'state', 'dc', 'subnet', 'gateway']

    datacenters = gandi.datacenter.list()

    vlan = gandi.vlan.info(resource)

    gateway = vlan['gateway']
    if not ip:
        output_vlan(gandi, vlan, datacenters, output_keys, justify=11)
        return vlan

    gateway_exists = False

    vms = dict([(vm_['id'], vm_) for vm_ in gandi.iaas.list()])
    ifaces = gandi.vlan.ifaces(resource)

    for iface in ifaces:
        for ip in iface['ips']:
            if gateway == ip['ip']:
                gateway_exists = True

    if gateway_exists:
        vlan.pop('gateway')
    else:
        vlan['gateway'] = ("%s don't exists" % gateway if gateway
                           else 'none')

    output_vlan(gandi, vlan, datacenters, output_keys, justify=11)

    output_keys = ['vm', 'bandwidth']
    for iface in ifaces:
        gandi.separator_line()
        output_iface(gandi, iface, datacenters, vms, output_keys,
                     justify=11)
        for ip in iface['ips']:
            output_ip(gandi, ip, None, None, None, ['ip'])
            if gateway == ip['ip']:
                output_line(gandi, 'gateway', 'true', justify=11)

    return vlan

@vlan.command()
@click.option('--background', '--bg', default=False, is_flag=True,
              help='Run command in background mode (default=False).')
@click.option('--force', '-f', is_flag=True,
              help='This is a dangerous option that will cause CLI to continue'
                   ' without prompting. (default=False).')
@click.argument('resource', nargs=-1, required=True)
@pass_gandi
def delete(gandi, background, force, resource):
    """Delete a vlan.

    Resource can be a vlan name or an ID
    """
    output_keys = ['id', 'type', 'step']

    possible_resources = gandi.vlan.resource_list()
    for item in resource:
        if item not in possible_resources:
            gandi.echo('Sorry vlan %s does not exist' % item)
            gandi.echo('Please use one of the following: %s' %
                       possible_resources)
            return

    if not force:
        vlan_info = "'%s'" % ', '.join(resource)
        proceed = click.confirm('Are you sure to delete vlan %s?' %
                                vlan_info)

        if not proceed:
            return

    opers = gandi.vlan.delete(resource, background)
    if background:
        for oper in opers:
            output_generic(gandi, oper, output_keys)

    return opers

@vlan.command()
@click.option('--name', required=True, help='Name of the vlan.')
@option('--datacenter', type=DATACENTER, default='FR-SD5',
        help='Datacenter where the vlan will be spawned.')
@click.option('--subnet', help='The vlan subnet.')
@click.option('--gateway', help='The vlan gateway.')
@click.option('--background', '--bg', default=False, is_flag=True,
              help='Run command in background mode (default=False).')
@pass_gandi
def create(gandi, name, datacenter, subnet, gateway, background):
    """ Create a new vlan """
    try:
        gandi.datacenter.is_opened(datacenter, 'iaas')
    except DatacenterLimited as exc:
        gandi.echo('/!\ Datacenter %s will be closed on %s, '
                   'please consider using another datacenter.' %
                   (datacenter, exc.date))

    result = gandi.vlan.create(name, datacenter, subnet, gateway, background)

    if background:
        gandi.pretty_echo(result)

    return result

@vlan.command()
@click.option('--name', help='Name of the vlan.')
@click.option('--gateway', help='Gateway of the vlan.')
@click.option('--create', default=False, is_flag=True,
              help='If gateway is a vm and does not have any ip in the good '
                   'vlan, we will start by putting an ip in the vlan.')
@option('--bandwidth', type=click.INT, default=102400,
        help="Network bandwidth in kbit/s used to create the VM's ip in this "
             'vlan.')
@click.argument('resource')
@pass_gandi
def update(gandi, resource, name, gateway, create, bandwidth):
    """ Update a vlan

    ``gateway`` can be a vm name or id, or an ip.
    """
    params = {}
    if name:
        params['name'] = name

    vlan_id = gandi.vlan.usable_id(resource)

    try:
        if gateway:
            IP(gateway)
            params['gateway'] = gateway
    except ValueError:
        vm = gandi.iaas.info(gateway)
        ips = [ip for sublist in
               [[ip['ip'] for ip in iface['ips'] if ip['version'] == 4]
                for iface in vm['ifaces']
                if iface['vlan'] and iface['vlan'].get('id') == vlan_id]
               for ip in sublist]

        if len(ips) > 1:
            gandi.echo("This vm has two ips in the vlan, don't know which one"
                       ' to choose (%s)' % (', '.join(ips)))
            return

        if not ips and not create:
            gandi.echo("Can't find '%s' in '%s' vlan" % (gateway, resource))
            return

        if not ips and create:
            gandi.echo('Will create a new ip in this vlan for vm %s' % gateway)
            oper = gandi.ip.create('4', vm['datacenter_id'], bandwidth,
                                   vm['hostname'], resource)

            iface_id = oper['iface_id']
            iface = gandi.iface.info(iface_id)
            ips = [ip['ip'] for ip in iface['ips'] if ip['version'] == 4]

        params['gateway'] = ips[0]

    result = gandi.vlan.update(resource, params)

    return result


from external.models import PriceDeterminator
from decimal import Decimal
import urllib
from dal import autocomplete
from django.shortcuts import reverse
from andablog.admin import EntryAdmin
from andablog.models import Entry
from ckeditor.widgets import CKEditorWidget
from django import forms
from django.db import models
from django.contrib import admin, messages
from django.contrib.admin.helpers import ActionForm
from django.contrib.auth import get_user_model
from django.conf import settings
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
import requests

# Register your models here.
from .models import RequestPool, BlogCategory, BlogArticle, TutorJobResponse
from config.utils import create_modeladmin, get_result
from external.models import BaseRequestTutor
from external.tasks import send_profile_to_client, send_text_message
from users.models import UserProfile
from skills.models import related_subjects, TutorSkill

from registration.tasks import verify_id_to_new_tutors
from hubspot import HubspotAPI

def profile_picture_filter():
    class HasProfilePicFilter22(admin.SimpleListFilter):
        title = _("Profile Pic")
        parameter_name = "profile_pic"

        def lookups(self, request, model_admin):
            return (
                ("has_profile_pic", _("Has Profile Pic")),
                ("no_profile_pic", _("No profile pic")),
            )

        def queryset(self, request, queryset):
            if self.value() == "has_profile_pic":
                return queryset.exclude(tutor__profile__image=None)
            if self.value() == "no_profile_pic":
                return queryset.filter(tutor__profile__image=None)
            return queryset

    return HasProfilePicFilter22

def gender_filter():
    class GenderFilter(admin.SimpleListFilter):
        title = _("Gender")
        parameter_name = "gender"

        def lookups(self, request, model_admin):
            return (
                ("male_tutors", _("Male Tutors")),
                ("female_tutors", _("Female Tutors")),
            )

        def queryset(self, request, queryset):
            if self.value() == "male_tutors":
                return queryset.filter(tutor__profile__gender=UserProfile.MALE)
            if self.value() == "female_tutors":
                return queryset.filter(tutor__profile__gender=UserProfile.FEMALE)
            return queryset

    return GenderFilter

@admin.register(BlogCategory)
class BlogCategoryAdmin(admin.ModelAdmin):
    list_display = ("slug", "name")

class BlogArticleAdmin(admin.StackedInline):
    model = BlogArticle

class EntryForm(forms.ModelForm):
    # content = forms.CharField(widget=AdminPagedownWidget())
    content = forms.CharField(widget=CKEditorWidget())

    class Meta:
        model = Entry
        fields = "__all__"
        widgets = {"author": autocomplete.ModelSelect2(url="users:user-autocomplete")}

class BudgetUpdateForm(ActionForm):
    amount = forms.DecimalField(
        widget=forms.TextInput(attrs=dict(placeholder="amount")), required=False
    )
    subjects = forms.CharField(
        widget=forms.TextInput(attrs=dict(placeholder="subjects")), required=False
    )
    default_subject = forms.CharField(
        widget=forms.TextInput(attrs=dict(placeholder="d_subjects")), required=False
    )
    text_msg = forms.CharField(
        widget=forms.Textarea(attrs=dict(rows=3, placeholder="text_msg")),
        required=False,
    )

class NewEntryAdmin(EntryAdmin):
    list_display = ["title", "featured", "is_published", "published_timestamp"]
    form = EntryForm
    inlines = (BlogArticleAdmin,)

    def featured(self, obj):
        return obj.link.featured

    featured.boolean = True

    def mark_as_featured(self, request_queryset):
        queryset.update(featured=True)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "author":
            kwargs["queryset"] = get_user_model().objects.all()
            # Q(is_superuser=True) | Q(user_permissions__content_type__app_label='andablog',
            #                          user_permissions__content_type__model='entry')).distinct()
            kwargs["initial"] = request.user.id
        return super(EntryAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )

admin.site.unregister(Entry)
admin.site.register(Entry, NewEntryAdmin)

class RemoveApprovedRequest(admin.SimpleListFilter):
    title = _("Not Approved Yet")
    parameter_name = "a_r"

    def lookups(self, request, model_admin):
        return (
            ("approved_request", _("Approved Request")),
            ("not_approved", _("Not Approved Requests")),
        )

    def queryset(self, request, queryset):
        x = queryset.filter(approved=True).values_list("req_id", flat=True)
        if self.value() == "approved_request":
            return queryset.filter(req_id__in=x)
        if self.value() == "not_approved":
            return queryset.exclude(req_id__in=x)
        return queryset

class RequestTypeFilter(admin.SimpleListFilter):
    title = _("Request Type")
    parameter_name = "_req_type"

    def lookups(self, request, model_admin):
        return (
            ("parent", _("Parent Request")),
            ("generic", _("Generic Request")),
            ("tutor", _("Tutor Request")),
        )

    def queryset(self, request, queryset):
        if self.value() == "parent":
            return queryset.filter(req__is_parent_request=True)
        if self.value() == "generic":
            return queryset.filter(req__is_parent_request=False, req__request_type=1)
        if self.value() == "tutor":
            return queryset.filter(req__request_type=2)
        return queryset

def create_modeladmin(modeladmin, model, name=None):
    class Meta:
        proxy = True
        app_label = model._meta.app_label

    attrs = {"__module__": "", "Meta": Meta}

    newmodel = type(name, (model,), attrs)

    admin.site.register(newmodel, modeladmin)
    return modeladmin

class RequestForm(forms.ModelForm):
    class Meta:
        model = RequestPool
        fields = "__all__"
        widgets = {
            "tutor": autocomplete.ModelSelect2(url="users:user-autocomplete"),
            "req": autocomplete.ModelSelect2(url="baserequesttutor-autocomplete"),
        }

@admin.register(TutorJobResponse)
class TutorJobResponseAdmin(admin.ModelAdmin):
    list_display = [
        "slug",
        "tutor_email",
        "request_info",
        "reason",
        "comment",
        "status",
        "created",
        "agent",
    ]
    search_fields = ("req__slug", "tutor__email", "req__email")
    list_select_related = ["tutor", "req"]
    list_filter = [
        "status",
    ]
    form = RequestForm

    def slug(self, obj):
        return obj.req.slug

    def comment(self, obj):
        if obj.data_dump:
            return obj.data_dump.get("comment", "")
        return ""

    def reason(self, obj):
        if obj.data_dump:
            return obj.data_dump.get("reason")
        return ""

    def tutor_email(self, obj):
        if obj.tutor:
            return '%s' % (
                obj.tutor_id,
                obj.tutor.email,
            )

    tutor_email.allow_tags = True

    def request_info(self, obj):
        return '%s' % (
            obj.req.slug,
            obj.req.slug,
        )

    request_info.allow_tags = True

    def agent(self, obj):
        if obj.req:
            if obj.req.agent:
                return obj.req.agent.name

@admin.register(RequestPool)
class RequestPoolAdmin(admin.ModelAdmin):
    list_display = [
        "slug",
        "tutor",
        "req_url",
        "quiz_url",
        "request_subjects",
        "user_subjects",
        "approved",
        "cost",
        "per_hour",
        "tutor_gender",
        "full_address",
        "distance",
        "subjects",
        "remarks",
        "full_name",
        "number",
        "ttp",
        "budget",
        "tutor_skills",
        "tutor_requested",
    ]
    actions = [
        "add_tutor_to_client_pool",
        "resend_link_to_hubspot",
        "remove_tutor_from_pool",
        "update_price_of_client_and_request",
        "set_tutor_as_recommended",
        "send_mail_to_tutors_to_update_exhibitions",
        "send_mail_to_tutors_to_update_skill",
        "update_tutors_price",
        "send_mail_to_reupload_profile_picture",
        "update_client_subject_with_tutors",
        "default_subject_update",
        "mark_as_approved",
        "send_text_message_to_tutors",
    ]
    action_form = BudgetUpdateForm
    list_filter = [
        RemoveApprovedRequest,
        RequestTypeFilter,
        profile_picture_filter(),
        gender_filter(),
        "approved",
    ]
    search_fields = ("req__slug", "tutor__email", "req__email")
    form = RequestForm
    tuteria_subjects = []

    def slug(self, obj):
        return obj.req.slug

    slug.admin_order_field = "-req"

    def quiz_url(self, obj):
        if obj.tutor:
            return 'quiz_url'.format(
                obj.tutor.email
            )

    quiz_url.allow_tags = True

    def req_url(self, obj):
        return 'request_url'.format(
            obj.req.slug
        )

    req_url.allow_tags = True

    def tutor_gender(self, obj):
        return obj.req.get_gender_display()

    def tutor_skills(self, obj):
        if obj.tutor:
            return 'tutor_skills'.format(
                obj.tutor.email
            )

    tutor_skills.allow_tags = True

    def tutor_requested(self, obj):
        return obj.req.tutor

    def full_name(self, obj):
        x = obj
        if x.tutor:
            return "{} {}".format(x.tutor.first_name, x.tutor.last_name)

    def full_address(self, obj):
        if obj.get_tutor_address:
            return obj.get_tutor_address.full_address

    def number(self, obj):
        if obj.tutor:
            all_numbers = obj.tutor.phonenumber_set.all()
            return all_numbers[0].number
        # return ''.join(x.number for x in obj.primary_phone_no)
        # return obj.tutor.primary_phone_no

    def budget(self, obj):
        return obj.req.budget

    def request_subjects(self, obj):
        return obj.req.request_subjects

    def per_hour(self, obj):
        return '{}'.format(
            obj.req.slug, obj.req.per_hour()
        )

    per_hour.allow_tags = True

    def send_text_message_to_tutors(self, request, queryset):
        ids = queryset.values_list("pk", flat=True)
        text = request.POST.get("text_msg")
        if text:
            send_text_message.delay(list(ids), text)

    def update_tutors_price(self, request, queryset):
        new_price = request.POST.get("amount")
        if new_price:
            queryset.update_tutors_prices(Decimal(new_price))
            messages.info(request, "Tutor's price has been updated")

    def get_google_distance(self, request, queryset):
        if request.GET.get("q"):
            xx = queryset.first()
            if xx:
                xx = xx.req
            tutor_ids = [x.tutor_id for x in queryset.all()]
            if xx:
                if xx.latitude:
                    self.distances = get_result(xx, queryset, tutor_ids)
                else:
                    self.distances = []
            else:
                self.distances = []

    def distance(self, obj):
        if hasattr(self, "distances"):
            x = [y for y in self.distances if y[1] == obj.tutor_id]
            if x:
                return x[0][0]

    def send_mail_to_reupload_profile_picture(self, request, queryset):
        for x in queryset.all():
            verify_id_to_new_tutors.delay(x.tutor_id, False)

    def update_client_subject_with_tutors(self, request, queryset):
        req_id = queryset.first().req
        queryset.update(subjects=req_id.request_subjects)

    def get_queryset(self, request):
        from connect_tutor.forms import RequestWithRelations
        from users.models import PhoneNumber

        self.tuteria_subjects = RequestWithRelations.get_tuteria_subjects()
        query = (
            super(RequestPoolAdmin, self)
            .get_queryset(request)
            .select_related("req", "tutor")
            .filter(req__booking=None)
            .prefetch_related(
                models.Prefetch(
                    "tutor__tutorskill_set",
                    queryset=TutorSkill.objects.select_related("skill").exclude(
                        status=TutorSkill.DENIED
                    ),
                    to_attr="valid_tutorskills",
                ),
                "tutor__phonenumber_set",
                "tutor__location_set",
            )
        )
        return query

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(RequestPoolAdmin, self).get_search_results(
            request, queryset, search_term
        )

        self.get_google_distance(request, queryset)
        return queryset, use_distinct

    def remove_tutor_from_pool(self, request, queryset):
        req_ids = [vv.req_id for vv in queryset.all()]
        queryset.update(approved=False)
        BaseRequestTutor.objects.filter(id__in=req_ids).update(
            status=BaseRequestTutor.COMPLETED
        )

    def update_price_of_client_and_request(self, request, queryset):
        reqs = [x.req_id for x in queryset.all()]
        BaseRequestTutor.objects.filter(id__in=reqs).update(
            budget=request.POST.get("amount")
        )
        messages.info(request, "Budget amount updated")

    def mark_as_approved(self, request, queryset):
        queryset.update(approved=True)

    def shared_implementation(self, request, queryset):
        first = queryset.first().req
        main_request = first.related_request
        req_ids = [vv.req_id for vv in queryset.all()]
        BaseRequestTutor.objects.filter(id__in=req_ids).update(
            status=BaseRequestTutor.PENDING, payment_date=timezone.now()
        )
        queryset.update(approved=True, modified=timezone.now())
        if main_request:
            main_request.status = BaseRequestTutor.PENDING
            main_request.save()
        # else:
        first.status = BaseRequestTutor.PENDING
        first.email = first.email.strip()
        first.save()
        h = PriceDeterminator.get_rates()
        if h.use_hubspot:
            h_instance = HubspotAPI(reverse("hubspot:hubspot_code"))
            if not first.hubspot_contact_id:
                first.hubspot_contact_id = h_instance.create_contact(first)
            if not first.hubspot_deal_id:
                first.hubspot_deal_id = h_instance.create_deal(
                    first, "hubspot_contact_id"
                )
            h_instance.send_email(first, deal="hubspot_deal_id", agent_field="name")
        return req_ids

    def resend_link_to_hubspot(self, request, queryset):
        self.shared_implementation(request, queryset)

    def add_tutor_to_client_pool(self, request, queryset):
        req_ids = self.shared_implementation(request, queryset)
        send_profile_to_client(req_ids[0])
        # send_profile_to_client.delay(req_ids[0])
        messages.info(request, "Added To Pool")

    def set_tutor_as_recommended(self, request, queryset):
        queryset.update(recommended=True)

    def send_mail_to_tutors_to_update_skill(self, request, queryset):
        from .tasks import send_mail_to_update_skills

        subjects = request.POST.get("subjects")
        for x in queryset.all():
            send_mail_to_update_skills.delay(x.tutor_id, x.req_id, subjects)

    def send_mail_to_tutors_to_update_exhibitions(self, request, queryset):
        from .tasks import send_mail_to_tutors_to_update_exhibitions

        subjects = request.POST.get("subjects")
        for x in queryset.all():
            send_mail_to_tutors_to_update_exhibitions.delay(
                x.tutor_id, x.req_id, subjects
            )

    def default_subject_update(self, request, queryset):
        subjects = request.POST.get("default_subject")
        queryset.update(default_subject=subjects)

    def user_subjects(self, obj):
        if obj.tutor:
            subjects = obj.tutor.valid_tutorskills
            subjects_related = [y.lower() for y in related_subjects(obj.subjects, self.tuteria_subjects)]
            s = [
                x
                for x in subjects
                if len(list(set(x.skill.related_with).intersection(subjects_related)))
                > 0
            ]
            # s = obj.tutor.tutorskill_set.related_subject(obj.subjects).all()
            return [
                '%s'
                % (v.get_absolute_url(), v.skill.name)
                for v in s
            ]

    user_subjects.allow_tags = True

class BookingsToBePlacedAdmin(admin.ModelAdmin):
    list_display = [
        "slug",
        "pk",
        "tutor_subjects",
        "budget",
        "amount_paid",
        "available_days",
        "time_of_lesson",
        "request_subjects",
        "class_urgency",
        "hijack_user",
        "expectation",
    ]

    def hijack_user(self, obj):
        return 'Hijack %s' % (
            obj.user.pk,
            obj.user.first_name,
        )

    hijack_user.allow_tags = True

    def tutor_subjects(self, obj):
        s = []
        for u in obj.request.valid_tutors():
            for d in u.tutorskill_set.related_subject(obj.request_subjects).all():
                s.append(d)
        return [
            '%s' % (v.get_absolute_url(), v.skill.name)
            for v in s
        ]

    tutor_subjects.allow_tags = True

    def get_queryset(self, request):
        return (
            super(BookingsToBePlacedAdmin, self)
            .get_queryset(request)
            .filter(status=BaseRequestTutor.PAYED)
            .filter(booking=None)
            .exclude(amount_paid=Decimal(0))
            .tutor_count_exists()
        )

create_modeladmin(BookingsToBePlacedAdmin, BaseRequestTutor, name="BookingsToBePlaced")


# import os
# import cv2
# from yolov8 import YOLOv8
# import argparse

# def process_video(video_path, output_folder):
#     cap = cv2.VideoCapture(video_path)
#     start_time = 5  # skip first {start_time} seconds
#     cap.set(cv2.CAP_PROP_POS_FRAMES, start_time * cap.get(cv2.CAP_PROP_FPS))

#     # Initialize YOLOv7 model
#     model_path = "models/best2d.onnx"
#     yolov8_detector = YOLOv8(model_path, conf_thres=0.5, iou_thres=0.5)

#     # Get video file name without extension
#     video_name = os.path.splitext(os.path.basename(video_path))[0]

#     # Create the output folder if it doesn't exist
#     os.makedirs(output_folder, exist_ok=True)

#     # Output video path
#     output_video_path = os.path.join(output_folder, f"{video_name}_output.avi")
#     out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), cap.get(cv2.CAP_PROP_FPS),
#                           (3840, 2160))

#     cv2.namedWindow("Detected Objects", cv2.WINDOW_NORMAL)
#     while cap.isOpened():

#         # Press key q to stop
#         if cv2.waitKey(1) == ord('q'):
#             break

#         try:
#             # Read frame from the video
#             ret, frame = cap.read()
#             if not ret:
#                 break
#         except Exception as e:
#             print(e)
#             continue

#         # Update object localizer
#         boxes, scores, class_ids = yolov8_detector(frame)

#         combined_img = yolov8_detector.draw_detections(frame)
#         cv2.imshow("Detected Objects", combined_img)
#         out.write(combined_img)

#     out.release()
#     cap.release()

# def main():
#     parser = argparse.ArgumentParser(description='Run YOLOv8 on videos in a folder')
#     parser.add_argument('video_folder', type=str, help='Path to the folder containing videos')
#     args = parser.parse_args()

#     video_folder = args.video_folder
#     if not os.path.isdir(video_folder):
#         print(f"Error: The specified path '{video_folder}' is not a directory.")
#         return

#     video_files = [file for file in os.listdir(video_folder) if file.endswith('.mp4')]
#     if not video_files:
#         print(f"No video files found in '{video_folder}'.")
#         return

#     output_folder = "runs_detect"  # Replace with the path where you want to save the output videos
#     for video_file in video_files:
#         video_path = os.path.join(video_folder, video_file)
#         process_video(video_path, output_folder)

# if __name__ == "__main__":
#     main()

import cv2
from cap_from_youtube import cap_from_youtube

from yolov8 import YOLOv8

# # Initialize video
# cap = cv2.VideoCapture("input.mp4")

# videoUrl = 'https://youtu.be/Snyg0RqpVxY'
#cap = cap_from_youtube(videoUrl, resolution='720p')

cap = cv2.VideoCapture("05_06_23_13_50_05_958647_10cs_5mm_455.mp4")
start_time = 5 # skip first {start_time} seconds
cap.set(cv2.CAP_PROP_POS_FRAMES, start_time * cap.get(cv2.CAP_PROP_FPS))

# out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), cap.get(cv2.CAP_PROP_FPS), (3840, 2160))

# Initialize YOLOv7 model
model_path = "models/best2d.onnx"
yolov8_detector = YOLOv8(model_path, conf_thres=0.1, iou_thres=0.5)

cv2.namedWindow("Detected Objects", cv2.WINDOW_NORMAL)
while cap.isOpened():

    # Press key q to stop
    if cv2.waitKey(1) == ord('q'):
        break

    try:
        # Read frame from the video
        ret, frame = cap.read()
        if not ret:
            break
    except Exception as e:
        print(e)
        continue

    # Update object localizer
    boxes, scores, class_ids = yolov8_detector(frame)

    combined_img = yolov8_detector.draw_detections(frame)
    cv2.imshow("Detected Objects", combined_img)
    # out.write(combined_img)

# out.release()


#%%
import pandas as pd
import matplotlib.pyplot as plt
import plotly_express as px

#%%
df = pd.read_excel("Data/Estado de estudiantes.xlsx")
print(df)

#%%
fig, ax = plt.subplots(figsize=(10, 8))
g = [n for n in df.iloc[:, -1].value_counts()]
values, bins, bars = plt.hist(x=df.iloc[:, -1])
ax.set_xlabel("Gender", fontsize=16)
ax.set_ylabel("Amount", fontsize=16)
ax.tick_params(axis='x', labelsize=14)
ax.tick_params(axis='y', labelsize=14)
ax.set_title("Students per Gender", fontsize=18, fontweight="bold")
plt.bar_label(bars)
plt.tight_layout()
plt.show()

#%%
fig_1 = px.histogram(df, x=df.iloc[:, -1], labels={"x": "Gender"}, color=df.iloc[:, -1])
fig_1.show()

#%%
df_est = pd.read_csv("Data/Estado de estudiantes.csv")
# Rename column names
df_est.rename(columns={"MATR�CULA": "MATRÍCULA", "G�nero": "GÉNERO"}, inplace=True)
#%%
df_est.to_csv("Data/Estado Estudiantes.csv", index=False)

#%%
fig_2, ax = plt.subplots(figsize=(10, 8))
ax.hist(df.iloc[:, 3], bins=30)
plt.show()


import urllib.request
import re
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException

def scrape_data():

	url1="https://www.tapology.com/search/mma-event-figures/ppv-pay-per-view-buys-buyrate"
	ufc_link="https://www.ufc.com/athletes/all"

	row1=[]
	source1=urllib.request.urlopen(url1)
	soup1=BeautifulSoup(source1,"lxml")
	table1=soup1.select('table')[0]

	table1_row=table1.find_all('tr')
	for tr in table1_row:
		td=tr.find_all('td')
		temp=[i.text for i in td]
		row1.append(temp)
	row1=row1[1:]
	dic=dict()
	for r in row1:
		temp=''

		#To remove commas from amount
		for s in r[6]:
			if s!=',':
				temp=temp+s

		if int(temp)>1000000:
			temp1=r[2].split()
			for word in temp1:
				if word=='The' or re.match(r'[0-9vs]',word):    #remove unnecessary numbers and words from list
					continue

				elif word.isspace()==False:
					if word in dic:
						dic[word]=dic[word]+1
					else:
						dic[word]=1

	#Store name of fighter with highest key
	key_name=''
	value_num=0
	for key in dic:
		if dic[key]>value_num:
			key_name=key
			value_num=dic[key]

	def find(driver):
	    element = driver.find_element_by_xpath("//*[@id='block-mainpagecontent']/div/div/div[1]/div/div/ul/li/a")
	    if element:
	        return element
	    else:
	        return False

	#To click on load more button on the page
	driver=webdriver.Chrome(executable_path="/home/akhil/crawler/chromedriver_linux64 (2)/chromedriver")
	driver.get('https://www.ufc.com/athletes/all')

	delay = 7 #seconds

	while True:
	    try:
	        myElem = WebDriverWait(driver, delay).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="block-mainpagecontent"]/div/div/div[2]/div/div/ul/li/a')))
	        print("found the btn")
	    except TimeoutException:
	    	print('-------------------------------------------')
	    	break

	    driver.execute_script("arguments[0].click()", myElem)
	    print('click')
	    driver.implicitly_wait(delay)
	    time.sleep(delay)
	    print('sleep')

	print("out")

	source = driver.page_source				#To store or save html page after clicking load more button

	#name_tree=urllib.request.urlopen(ufc_link)
	name_tree_request=BeautifulSoup(source,"lxml")
	link=''
	im=''
	fighters_info={}					#To store information of all fighters

	for i in name_tree_request.find_all('div',class_='c-listing-athlete-flipcard'):
		full_name=i.find('span',class_='c-listing-athlete__name').text
		first=full_name.split()
		print(first)
		sec = first[1] if len(first)>1 else first[0]
		im=i.find('span',class_='c-listing-athlete__name')
		im=i.img['src']
		link=i.find('div',class_='c-listing-athlete-flipcard__action').a['href']
		print('-------',sec)
		fighters_info[sec]={}
		fighters_info[sec]['full_name']=full_name
		fighters_info[sec]['img']=im
		fighters_info[sec]['link']=link

	link_to_fighters_page=fighters_info[key_name]['link']
	fighters_image=fighters_info[key_name]['img']
	record=[]
	next_url="https://www.ufc.com"+link_to_fighters_page

	#print(next_url)
	source2=urllib.request.urlopen(next_url)
	soup2=BeautifulSoup(source2,"lxml")
	record_tree=soup2.find_all('div',class_='c-hero__headline-suffix')

	record=list(record_tree[0].text.split())

	fighters_name=fighters_info[key_name]['full_name']

	fighters_name=' '.join(fighters_name.split())
	fighters_record=' '.join(record)

	return fighters_name,fighters_record,dic

if __name__=='__main__':

	fighters_name,fighters_record,dic=scrape_data()


# This script allows

import sys
import re
import csv
import os
import xlwt
import xlrd
import xlsxwriter

# Function: load_menu
#---------------------
#
#
def load_main_menu():
    menu = {}
    menu['1']="Text File"
    menu['2']="Excel File"
    menu['3'] ="Exit Program"

    while True:
        for key, value in sorted(menu.items()):
            print(key,value)

        selection=input()
        if selection =='1':
            text_to_excel()
            break
        elif selection == '2':
            excel_to_text()
            break
        elif selection == '3':
            break
        else:
            print("Unknown Option Selected!")

# Function: load_column_headers
#------------------------------
#
#
def load_column_headers():
    book_header = ["Author (Last, First)","Book Title",
              "Publication City", "Publisher",
              "Year of Publication",
              "Medium (Print or Online)"
              ]
    es_header = ["Author (Last, First)", "Article Name",
                 "Title of Website", "Version Numbers", "Publisher",
                 "Publication Year", "Page Numbers", "Medium of Publication",
                 "Date Accessed", "URL"]

    headers = []
    headers.append(book_header)
    headers.append(es_header)
    return headers

# Function: intialize_excelsheet
#--------------------------------
#
#
def initialize_excelsheet(fileName):
    workbook = xlsxwriter.Workbook(fileName)
    sheets = []
    sheets.append(workbook.add_worksheet("Books"))
    sheets.append(workbook.add_worksheet("Electronic Sources"))

    #Initializes formating for header columns
    header_format = workbook.add_format()
    header_format.set_bold()
    header_format.set_bottom()
    header_format.set_align('center')

    headers = load_column_headers()
    for i in range(0,len(headers)):
        sheet = sheets[i]
        header = headers[i]
        for c in range(0,len(header)):
            sheet.write(0,c, header[c], header_format)
            if header[c] == 'Book Title':
                size_factor = 2
            elif header[c] == 'URL':
                size_factor = 8
            else:
                size_factor = 1
            sheet.set_column(c, c, size_factor*(len(header[c])+1))

# Function: text_to_excel
#-------------------------
#
#
def text_to_excel():
    textFileName = input("Enter the name of the input file: (Use .txt) \n")
    excelFileName = input('Name the output file: (Use .xlsx) \n')

    file = open(textFileName, 'r')
    line = file.read()
    words = line.split()
    for word in words:
        print(word)

#--------------------------------------
#    workbook= xlwt.Workbook()
#    sheet = workbook.add_sheet("test")
#    sheet.write(0, 0, line)
#    workbook.save(excelFileName)
#--------------------------------------

    file.close()

# Function: excel_to_text
#-------------------------
#
#
def excel_to_text():
    excelFileName = input('Name the input file: (Use .xlsx) \n')
    textFileName = input("Name the output file: (Use .txt) \n")

    initialize_excelsheet(excelFileName)
    print("\nEnter the citation information in the Excel Sheet created.")
    print("(Make sure to close & save the document when you are done!)\n")
    while True:
        answer = input("Did you finish? (y/n)")
        if (answer == 'y'):
            break
        elif (answer == 'n'):
            print("\nPlease close & save the document before continuing")
        else:
            print("\nAnswer not recognized.")

    print("Processing File...\n")
    #ADD code HERE!!

    textFile = open(textFileName, 'w')
    workbook= xlrd.open_workbook(excelFileName)
    worksheet = workbook.sheet_by_name('Books')

    numRows = worksheet.nrows -1
    curRow = 0
    while curRow < numRows:
        curRow +=1
        data =[]
        for i in range(0,6):
            data.append(worksheet.cell_value(curRow, i))

        textFile.write(data[0] + '. ' + data[1] + '. '
                       + data[2] + ': ' + data[3] + ', '
                       + str(int(data[4])) + '.\n\t' + data[5]+ '.\n\n')
    textFile.close()
    print("Process Complete.\nOpen " + textFileName + " to see citations.")

# Function: main
#-----------------
#
#
def main():
    print("Welcome to Biblio! \n")
    print("Please select an input file:")
    load_main_menu()

# Main Program Execution
main()