#!/usr/bin/env python

"""Performance test for treadmill.scheduler
"""

import timeit
import random
import time

from treadmill import scheduler
from treadmill import utils

def resources(data):
    """Convert resource demand/capacity spec into resource vector."""
    parsers = {
        'memory': utils.megabytes,
        'disk': utils.megabytes,
        'cpu': utils.cpu_units
    }

    return [parsers[k](data.get(k, 0)) for k in ['memory', 'cpu', 'disk']]

# XXX(boysson): Test needs update to new Scheduler API
# XXX:
# def test_reschedule(nodes_count, app_count, attempts, affinity):
#     """Add high priority apps on top of low priority with full capacity.
#     """
#     # print('nodes: %s, apps: %s, attempts: %s' % (nodes_count,
#     #                                              app_count,
#     #                                              attempts))
#     scheduler.DIMENSION_COUNT = 3
#     cell = scheduler.Cell("local", labels=set([None]))
#     for idx in range(0, nodes_count):
#         node = scheduler.Server('node' + str(idx), resources({
#             "memory": "2G",
#             "disk": "20G",
#             "cpu": "90%"
#         }), time.time() * 2)
#         cell.add_node(node)

#     for attempt in range(0, attempts):
#         for app_idx in range(0, app_count):
#             prio = attempt * 5 + random.randint(0, 5)
#             demand = resources({
#                 "memory": "1G",
#                 "disk": "10G",
#                 "cpu": "40%"
#             })
#             name = 'app_%s.%s' % (attempt, app_idx)
#             app = scheduler.Application(name, prio, demand, affinity=affinity(app_idx))
#             cell.partitions[None].allocation.add(app)
#         cell.schedule()

# XXX(boysson): Test needs update to new Scheduler API
# XXX:
# def test_affinity(nodes_count, app_count, affinity_limit):
#     """Add more apps than nodes count to test affinity limit algo."""
#     print 'node: %s, apps: %s, affinity_limit %s' % (nodes_count,
#                                                      app_count,
#                                                      affinity_limit)
# XXX:
#     cell = scheduler.Cell(3)
#     for idx in xrange(0, nodes_count):
#         node = scheduler.Node('node' + str(idx), [48, 48, 48])
#         cell.add_node(node)
# XXX:
#     alloc = scheduler.Allocation([10, 10, 10])
#     cell.add_allocation('a1', alloc)
#     for app_idx in xrange(0, app_count):
#         name = '1.%s' % (app_idx)
#         alloc.add(scheduler.Application(name, 0, [1, 1, 1],
#                                         affinity_limit=affinity_limit,
#                                         affinity='1'))
#         name = '2.%s' % (app_idx)
#         alloc.add(scheduler.Application(name, 0, [1, 1, 1],
#                                         affinity_limit=affinity_limit,
#                                         affinity='2'))
#         name = '3.%s' % (app_idx)
#         alloc.add(scheduler.Application(name, 0, [1, 1, 1],
#                                         affinity_limit=affinity_limit,
#                                         affinity='3'))
# XXX:
#     sched = scheduler.Scheduler(cell)
#     schedule(sched)

def prepareData(nodes_count, app_count, affinity):
    scheduler.DIMENSION_COUNT = 3
    cell = scheduler.Cell("local", labels=set([None]))
    for idx in range(0, nodes_count):
        node = scheduler.Server('node' + str(idx), resources({
            "memory": "2G",
            "disk": "20G",
            "cpu": "90%"
        }), time.time() * 2)
        cell.add_node(node)

    for app_idx in range(0, app_count):
        prio = random.randint(0, 5)
        demand = resources({
            "memory": "1G",
            "disk": "10G",
            "cpu": "40%"
        })
        name = 'app_.%s' % (app_idx)
        app = scheduler.Application(name, prio, demand, affinity=affinity(app_idx))
        cell.partitions[None].allocation.add(app)

    return cell

if __name__ == '__main__':
    cell = None
    for i in range(0, 1001):
        if i % 10 ==0:
            cell = prepareData(500, i, affinity=lambda idx: None)
            print(i, timeit.timeit("cell.schedule()",
                                setup="from __main__ import cell", number=10))

# import cProfile
# import pstats
# if __name__ == '__main__':
#     cProfile.run('test_reschedule(5000, 10000, 1, affinity=lambda idx: None)', 'restats')
#     p = pstats.Stats('restats')
#     p.strip_dirs().sort_stats('tottime').print_stats()


######################################################################################
## This test is open-book. You may use class slides, textbook, notes, previous      ##
## assignments and Python. You may not use anything else including search engines,  ##
## on-line chatting tools, phones, etc. You may not discuss questions with anyone.  ##
##                                                                                ##
## By typing your name below, you indicate agreement with the UNC Honor Code Pledge,##
## that you have not given or received unauthorized assistance on this exam.        ##
## Name: Liang Shan                                                                 ##
######################################################################################

## This script will run as is but, of course, it doesn't yet produce
## the desired results. Your mission is to modify it to get the outputs
## requested.

import numpy as np
from PIL import Image

def mask(im):
    '''
    (2D array)-->(2D array)
    Return a masked version of image im by a circle centered at the center of im with
    '''
    M, N = im.shape
    cx = M/2
    cy = N/2
    r = 250
    x,y = np.meshgrid(np.arange(M), np.arange(N))
    mask = (x-cx)**2 + (y-cy)**2 <= r**2
    return im*mask

if __name__ == '__main__':
    im = np.asarray(Image.open('lena_gray.png'))
    im_new = mask(im)
    toSave = Image.fromarray(im_new).convert('L')
    toSave.save('P2_shan.png')


import time
import logging

logger = logging.getLogger(__name__)

class TimeTrigger(object):
    def __init__(self, timefcn, delayfcn):
        self._timefcn = timefcn if timefcn else time.time
        self._delayfcn = delayfcn if delayfcn else time.sleep
        self._num_steps = 1
        self._time_step = 0

    def set_steps(self, time_step, num_steps):
        self._time_step = time_step
        self._num_steps = num_steps

    def iter(self):
        logger.info('Starting iterator')

        time_finish_now = self._timefcn()
        time_start_next = time_finish_now

        for count_steps in range(self._num_steps):
            if time_start_next > time_finish_now:
                self._delayfcn(time_start_next - time_finish_now)

            logger.info('Start iteration #%d', count_steps)
            yield time_start_next

            time_finish_now = self._timefcn()
            if self._time_step > 0:
                time_start_next += self._time_step
                while time_finish_now > time_start_next:
                    time_start_next += self._time_step
            else:
                time_start_next = time_finish_now

        logger.info('Finished iterator')


'''

Maya.Tools.animTools Created By Skies

On 2013-03-20 14:57:32

'''

import maya.cmds as mc

def IKtoFK():
    switches = mc.ls(sl=1)
    for switch in switches:
        Side = switch.partition("_IKFK_ctrl")[0]

        # Get Positions

        SW = mc.getAttr(Side+'_IKFK_ctrl.IKFK')

        fkR1 = mc.xform(Side+'_IK_01_jnt', q=True, ro=True)
        fkR2 = mc.xform(Side+'_IK_02_jnt', q=True, ro=True)
        fkR3 = mc.xform(Side+'_IK_03_jnt', q=True, ws=True, ro=True)

        ik = mc.xform(Side+'_FK_03_jnt', q=True, ws=True, m=True)
        pv = mc.xform(Side+'_FK_02_jnt', q=True, ws=True, m=True)

        # IK to FK
        if SW == 0:
            mc.rotate(fkR1[0],fkR1[1],fkR1[2], Side+'_FK_01_jnt', os=True)
            mc.rotate(fkR2[0],fkR2[1],fkR2[2], Side+'_FK_02_jnt', os=True)
            mc.rotate(fkR3[0],fkR3[1],fkR3[2], Side+'_FK_03_jnt', ws=True, a=True)
            mc.setAttr (Side+'_IKFK_ctrl.IKFK', 1)

        # FK to IK
        elif SW == 1:
            mc.xform(Side + '_IK_anim', m=ik, ws=1)
            mc.xform(Side + '_IK_PoleVector_anim', m=pv, ws=1)
            mc.setAttr(Side+'_IKFK_ctrl.IKFK', 0)

        if Side == "Right": mc.rotate(180,0,0, Side + '_IK_PoleVector_anim', r=1, os=1)

def sideMirror():
    switch = mc.ls(sl=1)[0]
    Side = switch.partition("_IKFK_ctrl")[0]
    if "Left" in Side: mirror = Side.replace("Left", "Right")
    elif "Right" in Side: mirror = Side.replace("Right", "Left")

    mc.setAttr(Side+'_IKFK_ctrl.IKFK', mc.getAttr(mirror+'_IKFK_ctrl.IKFK'))

    # FK Joints

    if "arm" in Side:
        clav  = mc.xform(mirror.replace("_arm", "")+'_clavicule_anim', q=True, ro=True)
        mc.xform(Side.replace("_arm", "")+'_clavicule_anim', ro=clav)

    fk1 = mc.xform(mirror+'_FK_01_jnt', q=True, ro=True)
    fk2 = mc.xform(mirror+'_FK_02_jnt', q=True, ro=True)
    fk3 = mc.xform(mirror+'_FK_03_jnt', q=True, ro=True)
    mc.xform(Side+'_FK_01_jnt', ro=fk1)
    mc.xform(Side+'_FK_02_jnt', ro=fk2)
    mc.xform(Side+'_FK_03_jnt', ro=fk3)

    # IK Joints

    ikT = mc.xform(mirror+'_IK_anim', q=True, t=True)
    if "arm" in Side: mc.xform(Side+'_IK_anim', t=(-ikT[0], -ikT[1], -ikT[2]))
    if "leg" in Side: mc.xform(Side+'_IK_anim', t=(-ikT[0], ikT[1], ikT[2]))

    ikR = mc.xform(mirror+'_IK_anim', q=True, ro=True)
    if "arm" in Side: mc.xform(Side+'_IK_anim', ro=(ikR[0], ikR[1], ikR[2]))
    if "leg" in Side: mc.xform(Side+'_IK_anim', ro=(ikR[0], -ikR[1], -ikR[2]))

    # IK PV

    if "arm" in Side:
        ikPVT = mc.xform(mirror+'_IK_PoleVector_anim', q=True, t=True)
        mc.xform(Side+'_IK_PoleVector_anim', t=(-ikPVT[0], ikPVT[1], ikPVT[2]))
    if "leg" in Side:
        ikPVT = mc.xform(mirror+'_IK_PV_anim', q=True, t=True)
        mc.xform(Side+'_IK_PV_anim', t=(-ikPVT[0], ikPVT[1], ikPVT[2]))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 13:53:26 2020

@author: engels
"""

import glob
import numpy as np
import insect_tools
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--directory", nargs='?', const='./',
                    help="directory of h5 files, if not ./")
parser.add_argument("-o", "--output", type=str,
                    help="""output *.csv value""")
args = parser.parse_args()

if args.directory is None:
    args.directory ='./'

if args.output is None:
    args.output = "merge.csv"

#%%
files = sorted(glob.glob( args.directory+"/*.csv" ))

print('Found %i csv files' % (len(files)))

# unfortunately extracting wabbit isosurfaces results in cluttered CSV files
d = []
for file in files:
    d.append( np.loadtxt(file, delimiter=',', skiprows=1) )

d = np.vstack(d)

insect_tools.write_csv_file( args.directory+"/"+args.output, d, header=None, sep=';')


import numpy as np
import csv
from PIL import Image
import matplotlib.pyplot as plt
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import random
import cv2

class VimeoDataset(Dataset):
    ''' Vimeo Dataset '''

    def __init__(self, data_path_corrupted, data_path_clean, data_path_MDSR, data_list_file, frame_num=7,
                 transform=None, is_train=True, require_seqid=False):
        # assert frame_window_size%2==1, "frame_window_size should be odd"
        ## file list
        self.data_list_file = data_list_file
        self.folder_list_corrupted = self.get_list(data_path_corrupted)
        # self.frame_num_list = self.get_list(data_path_corrupted)
        self.folder_list_clean = self.get_list(data_path_clean)
        self.folder_list_MDSR = self.get_list(data_path_MDSR)
        self.file_list = ['im' + str(i) + '.png' for i in range(1, frame_num+1)]
        # transform
        self.transform = transform
        self.crop = None
        self.is_train = is_train
        self.M = len(self.folder_list_clean)  # sequences number
        self.frame_num = frame_num  # frame number
        # self.N = self.M * self.K    # total number
        if is_train:
            self.N = self.M * self.frame_num  # total number
        else:
            self.N = self.M
        # get shape
        self.require_seqid = require_seqid
        if self.require_seqid:
            buff, seqid = self.__getitem__(0)
            _, self.H, self.W = buff['input_img1_LR'].size()
        else:
            _, self.H, self.W = self.__getitem__(0)['input_img1_LR'].size()

    def get_list(self, data_path):
        folder_list = list()
        with open(self.data_list_file, 'r') as f_index:
            reader = csv.reader(f_index)
            for row in reader:
                if row:
                    folder_list.append(data_path + row[0] + '/')
        return folder_list

    def get_frame(self, m, f, mode='LR'):
        ''' return a 3-D [3,H,W] float32 [0.0-1.0] tensor '''
        # print (len(self.file_list),f)
        if mode == 'HR':
            filename = self.folder_list_clean[m] + self.file_list[f]
        elif mode == 'LR':
            filename = self.folder_list_corrupted[m] + self.file_list[f]
        elif mode == 'SR':
            filename = self.folder_list_MDSR[m] + self.file_list[f]
        image = Image.open(filename)
        return image

    # def get_all_frames(self, m, mode='LR'):
    #     ''' return a 3-D [3,H,W] float32 [0.0-1.0] tensor '''
    #     # print (len(self.file_list),f)
    #     # print('get all frames>>>>>>>>>>')
    #     if mode == 'HR':
    #         images_list = tuple()
    #         for f in self.file_list:
    #             filename = self.folder_list_clean[m] + f
    #             image = cv2.imread(filename)
    #             images_list = images_list + (image,)
    #         images = np.concatenate(images_list, axis=2)
    #         print('HR>>>>>>>>')
    #         print images.shape
    #     elif mode == 'LR':
    #         images_list = tuple()
    #         for f in self.file_list:
    #             filename = self.folder_list_corrupted[m] + f
    #             image = cv2.imread(filename)
    #             images_list = images_list + (image,)
    #         images = np.concatenate(images_list, axis=2)
    #         print('LR>>>>>>>>')
    #         print images.shape
    #     elif mode == 'SR':
    #         images_list = tuple()
    #         for f in self.file_list:
    #             filename = self.folder_list_MDSR[m] + f
    #             image = cv2.imread(filename)
    #             images = np.concatenate(images_list, axis=2)
    #         images = np.concatenate(images_list, axis=2)
    #         print('HR>>>>>>>>')
    #         print images.shape
    #     return images

    def __len__(self):
        return self.N

    def __getitem__(self, idx):
        if self.is_train:
            m = idx // self.frame_num
            f_st = 0
        else:
            m = idx
            f_st = 0
        # self.window_size = self.frame_num_list[idx]

        sample = dict()

        sample['input_img1_LR'] = self.get_frame(m, f_st, mode='LR')
        if self.transform:
            sample['input_img1_LR'] = self.transform(sample['input_img1_LR'])

        sample['input_img1_HR'] = self.get_frame(m, f_st, mode='HR')
        if self.transform:
            sample['input_img1_HR'] = self.transform(sample['input_img1_HR'])

        sample['input_img1_SR'] = self.get_frame(m, f_st, mode='SR')
        if self.transform:
            sample['input_img1_SR'] = self.transform(sample['input_img1_SR'])

        sample['input_img2_HR'] = self.get_frame(m, self.frame_num-1, mode='HR')
        if self.transform:
            sample['input_img2_HR'] = self.transform(sample['input_img2_HR'])

        if self.require_seqid:
            seq_id = self.folder_list_clean[m]
            return sample, seq_id
        else:
            return sample

if __name__ == "__main__":
    data_path_corrupted = '/fileserver/haitian/Fall2018_Multi_warp/dataset/vimeo_septuplet/sequences_noise/'
    data_path_MDSR = '/fileserver/haitian/Fall2018_Multi_warp/dataset/vimeo_septuplet/sequences_upsampled_MDSR/'
    data_path_clean = '/fileserver/haitian/Fall2018_Multi_warp/dataset/vimeo_septuplet/sequences/'
    data_list_file = '/fileserver/haitian/Fall2018_Multi_warp/dataset/vimeo_septuplet/sep_trainlist.txt'
    # composed = transforms.Compose([transforms.RandomCrop((128,128)),
    #                                 transforms.ToTensor()])

    composed = transforms.Compose([transforms.ToTensor()])
    dataset = VimeoDataset(data_path_corrupted, data_path_clean, data_path_MDSR, data_list_file, frame_window_size=2,
                           transform=composed)

    #### test pytorch dataset
    # print(len(dataset))

    # fig = plt.figure()
    # plt.axis('off')
    # plt.ioff()
    # im = plt.imshow(np.zeros((dataset.H, dataset.W, 3)), vmin=0, vmax=1)

    # for i in range(len(dataset)-1, 0, -1):
    #     sample = dataset[i]
    #     for t in sample:
    #         print(t, sample[t].size())
    #         im.set_data(sample[t].numpy().transpose(1,2,0))
    #         plt.pause(0.1)
    # exit()

    #### test dataloader
    dataloader = DataLoader(dataset, batch_size=32, shuffle=True, num_workers=6)
    print(len(dataset), len(dataloader))

    import cPickle as pickle
    import os

    img_name = ['img1_LR', 'img1_SR', 'img1_HR', 'img2_HR']
    for i_batch, sample_batched in enumerate(dataloader):
        # print(i_batch, sample_batched['gt'].size())
        # visualization
        images_batch = sample_batched['input_img1_LR']
        batch_size = images_batch.size()[0]
        im_size = images_batch.size()[1:]

        print(i_batch)
        save_dir = './vimeo_sr/'
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        f = open(save_dir + str(i_batch + 1), 'wb')
        pickle.dump(sample_batched, f)
        f.close()

        # print(batch_size, im_size)
        # grid = utils.make_grid(images_batch, nrow=2)
        # plt.imshow(grid.numpy().transpose(1,2,0))
        # plt.show()

        # observe 4th batch and stop.
        # if i_batch == 3:
        #     plt.figure()
        #     show_landmarks_batch(sample_batched)
        #     plt.axis('off')
        #     plt.ioff()
        #     plt.show()
        #     break


from restapi import serializers
from fueled import models
from rest_framework import (
    viewsets,
    parsers,
    permissions,
    exceptions,
    mixins
)
from django.shortcuts import get_object_or_404
from django.db.models import Avg
from django.utils.translation import ugettext as _

class UserRestaurantUniqueMixin(object):
    """ Add this for views where user and restaurant must be unique together
        Make sure it is included before mixins.CreateModelMixin, as it overrides one of its methods
    """

    def perform_create(self, serializer):
        user = self.request.user
        restaurant_id = serializer.data['restaurant_id']
        if models.Visit.objects.filter(user=user, restaurant_id=restaurant_id).exists():
            raise exceptions.ValidationError(_('Object with such user_id and restaurant_id already exists'))
        serializer.save(user=self.request.user)

class RestaurantViewSet(viewsets.ReadOnlyModelViewSet):

    queryset = models.Restaurant.objects.all()
    serializer_class = serializers.RestaurantSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        blacklisted = models.Visit.objects.filter(user=self.request.user, blacklisted=True)
        queryset = self.queryset.exclude(visit__in=blacklisted)
        queryset = queryset.annotate(rating=Avg('feedback__evaluation')).order_by('-rating')
        return queryset

class VisitViewSet(viewsets.GenericViewSet,
                   UserRestaurantUniqueMixin,
                   mixins.CreateModelMixin,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin):

    queryset = models.Visit.objects.all()
    serializer_class = serializers.VisitSerializer
    parser_classes = (parsers.JSONParser,)
    permission_classes = (permissions.IsAuthenticated,)
    lookup_field = 'restaurant_id'

    def get_object(self):
        queryset = self.filter_queryset(self.get_queryset())
        filter_kwargs = {'user': self.request.user, self.lookup_field: self.kwargs[self.lookup_field]}
        obj = get_object_or_404(queryset, **filter_kwargs)
        self.check_object_permissions(self.request, obj)
        return obj

class FeedbackViewSet(viewsets.GenericViewSet,
                      UserRestaurantUniqueMixin,
                      mixins.CreateModelMixin,
                      mixins.RetrieveModelMixin,
                      mixins.ListModelMixin):

    queryset = models.Feedback.objects.all()
    serializer_class = serializers.FeedbackSerializer
    parser_classes = (parsers.JSONParser,)
    authentication_classes = (permissions.IsAuthenticated,)
    lookup_field = 'restaurant_id'

    def get_queryset(self):
        queryset = self.queryset
        restaurant_id = self.request.query_params.get('restaurant_id')

        if restaurant_id is None:
            raise exceptions.ValidationError(_('Missing GET param restaurant_id'))

        try:
            restaurant_id = int(restaurant_id)
        except ValueError:
            raise exceptions.ValidationError(_('restaurant_id must be integer'))

        return queryset.filter(restaurant_id=restaurant_id)

class FeedbackCommentViewSet(viewsets.GenericViewSet,
                             mixins.CreateModelMixin,
                             mixins.ListModelMixin):

    queryset = models.FeedbackComment.objects.all()
    serializer_class = serializers.FeedbackCommentSerializer
    parser_classes = (parsers.JSONParser,)
    authentication_classes = (permissions.IsAuthenticated,)
    lookup_field = 'feedback_id'
    filter_fields = ('feedback_id',)

    def get_queryset(self):
        queryset = self.queryset
        feedback_id = self.request.query_params.get('feedback_id')

        if feedback_id is None:
            raise exceptions.ValidationError(_('Missing GET param feedback_id'))

        try:
            feedback_id = int(feedback_id)
        except ValueError:
            raise exceptions.ValidationError(_('feedback_id must be integer'))

        return queryset.filter(feedback_id=feedback_id)


from django.conf import settings
from analytics.utils import api_auth, api_client, api_method

import requests, uuid

def send_event(user_id, tid, event_category, event_action, event_label, event_value):
    auth = api_auth(settings.API_SERVICE_URL, settings.API_SERVICE_LOGIN, settings.API_SERVICE_PASSWORD)
    client = api_client(auth)
    user = api_method(client, settings.API_SERVICE_URL, ['subscriptions', 'subscribers', 'retrive', {'id': user_id}])

    if not user:
        user_uuid = user.uuid
    else:
        user_uuid = uuid.uuid4()

    data = {
        'v': 1,
        'tid': str(tid),
        'cid': str(user_uuid),
        't': 'event',
        'ec': event_category,
        'ea': event_action,
        'el': event_label,
        'ev': event_value
    }
    url = 'http://www.google-analytics.com/collect'
    requests.post(url, data)

    return None


import torch

def print_pre_trained():
	name = '/home/lindeshou/.cache/torch/checkpoints/resnext101_32x4d-a5af3160.pth'
	model = torch.load(name)
	for dicts in model:
		print(dicts)
	#dicts = model['state_dict']
	#for dict in dicts:
		#print(dict)
	#print(type(model))

def print_backbone():
	name = 'feature_get.pth'
	model = torch.load(name)
	#print(model)
	dicts = model["state_dict"]
	for dict in dicts:
		print(dict)

if __name__ == "__main__":
	#print_pre_trained()
	#print_backbone()
	model_name = 'htc_dconv_c3-c5_mstrain_400_1400_x101_64x4d_fpn_20e_20190408-0e50669c.pth'
	model = torch.load(model_name)

	backbone = dict()
	backbone['state_dict'] = dict()
	model_dicts = model['state_dict']
	for model_key in model_dicts:
		if not 'backbone' in model_key:
			continue

		key = model_key.split('backbone.')[1]
		#print("model_key = {}, key = {}".format(model_key, key))
		backbone['state_dict'][key] = model_dicts[model_key]

	torch.save(backbone, 'pre_trained/pre_trained_101_64.pth')


import datetime
import sqlite3
import requests
import urllib3
import re
from bs4 import BeautifulSoup
import textdistance
import time
urllib3.disable_warnings()
from gtts import gTTS
import os
import csv
import telebot
tb = telebot.TeleBot('1025277727:AAGKOX9qGF0mw_Cry--3ufdMQ3RGq50Qv3Y')
import BET

def for_in_every_element(a):
    b=c.execute('select * from FORK')
    for i in b.fetchall():
        cnt=0
        for x in range(16):
            if a[x]==i[x]:
                cnt+=1
        if cnt>7:
            print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Already exist')
            return False
    return True

def compare(ans,a):
        print('comparing started!')
        f = True
        f = for_in_every_element(a)
        return f

def compare_and_insert(ans):
    for a in ans:
        f = compare(ans,a)
        print('comparing succesfull!')

        print('inserting started!')
        if f == True:
            c.execute('''INSERT INTO FORK (data, time,league, opp1, opp2, bet1, coef1_bet1, coef2_bet1, bet2, coef1_bet2, coef2_bet2, win_opp1, percent_of_your_money_for_win_opp1, profit1, win_opp2, percent_of_your_money_for_win_opp2, profit2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15],a[16]))
            conn.commit()
            c.execute('''INSERT INTO FIND (data, time,league, opp1, opp2, bet1, coef1_bet1, coef2_bet1, bet2, coef1_bet2, coef2_bet2, win_opp1, percent_of_your_money_for_win_opp1, profit1, win_opp2, percent_of_your_money_for_win_opp2, profit2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15],a[16]))
        conn.commit()
        print(" ")
        print('inserting succesfull!')

#28.04	00:30	Dota 2. Isolation Cup	Miaww	Siberian Team	1xBET	1.805	1.97	OLIMPBET	3.1	1.34	OLIMPBET Siberian Team	38.856%	20.45360000000001%	1xBET Miaww	61.144%	20.45367999999999%
def get_coefficients(a,bet1,op1):
    coef1 = ''
    coef2 = ''
    if bet1 == a[5]:
        print('bet1==a[5]:')
        if op1 == (a[3]+' '):
            print('op1==a[3]')
            coef1 += a[6]
            coef2 += a[10]
        elif op1 == (a[4]+' '):
            print('op1==a[4]')
            coef1 += a[7]
            coef2 += a[9]
    elif bet1 == a[8]:
        print('bet2==a[8]:')
        if op1 == (a[3]+' '):
            print('op1==a[3]')
            coef1 += a[9]
            coef2 += a[7]
        elif op1 == (a[4]+' '):
            print('op1==a[4]')
            coef1 += a[10]
            coef2 += a[6]
    return coef1,coef2

def float_of_degree_three(a):
    a = format(float(a.replace('%','')),'.3f')
    return a

import mutagen
from mutagen.mp3 import MP3
def creating_and_sending_audio(l):

    text = '''На букмекере {b1}, надо ставить на команду - {opp1} под каэффицентом {c1}, ставить строго {p1} процентов от твоих общих денег,
     а затем На букмекере {b2}, надо ставить на команду - {opp2} под каэффицентом {c2}, ставить строго {p2} процентов от твоих общих денег'''.format(b1=bet1,opp1=op1,p1=p11,b2=bet2,
                                                                                opp2=op2,p2=p22,c1=coef1,c2=coef2)
    language = 'ru'
    myobj = gTTS(text = text, lang=language, slow=False)
    myobj.save(os.getcwd()+"\\audio\{date}_{time}_{opp1}_{opp2}_{c1}_{c2}.mp3".format(date=a[0],time=a[1].replace(':','-'),
                                                                        opp1=op1.strip(),opp2=op2.strip(),p2=p22,c1=coef1,c2=coef2))
    voice = open(os.getcwd()+'\\audio\\{date}_{time}_{opp1}_{opp2}_{c1}_{c2}.mp3'.format(date=a[0],time=a[1].replace(':','-'),
                                                                        opp1=op1.strip(),opp2=op2.strip(),p2=p22,c1=coef1,c2=coef2), 'rb')
    audio = MP3(os.getcwd()+'\\audio\\{date}_{time}_{opp1}_{opp2}_{c1}_{c2}.mp3'.format(date=a[0],time=a[1].replace(':','-'),
                                                                        opp1=op1.strip(),opp2=op2.strip(),p2=p22,c1=coef1,c2=coef2))
    audio_info = audio.info
    length_in_secs = int(audio_info.length)

    c=tb.send_voice(chat_id='@BET_BUSTERS', voice=voice,duration=length_in_secs)
    return c.message_id

def deleting_audio(l):
    os.remove(os.getcwd()+'\\audio\\{date}_{time}_{opp1}_{opp2}_{c1}_{c2}.mp3'.format(date=a[0],time=a[1].replace(':','-'),
                                                                        opp1=op1.strip(),opp2=op2.strip(),p2=p22,c1=coef1,c2=coef2))

def getting_emoji(c):
    from emoji import emojize
    zero = emojize(":zero:", use_aliases=True)
    one = emojize(":one:", use_aliases=True)
    two = emojize(":two:", use_aliases=True)
    three = emojize(":three:", use_aliases=True)
    four = emojize(":four:", use_aliases=True)
    five = emojize(":five:", use_aliases=True)
    six = emojize(":six:", use_aliases=True)
    seven = emojize(":seven:", use_aliases=True)
    eight = emojize(":eight:", use_aliases=True)
    nine = emojize(":nine:", use_aliases=True)
    trophy = emojize(":trophy:", use_aliases=True)
    if 0 <= c < 1:
        text = zero
    if 1 <= c < 2:
        text = one
    if 2 <= c < 3:
        text = two
    if 3 <= c < 4:
        text = three
    if 4 <= c < 5:
        text = four
    if 5 <= c < 6:
        text = five
    if 6 <= c < 7:
        text = six
    if 7 <= c < 8:
        text = seven
    if 8 <= c < 9:
        text = eight
    if 9 <= c < 10:
        text = nine
    if 10 <= c:
        text = trophy

    return text

global csvfile
csvfile = []

from telebot import types
def make_keyboard(bet1,bet2):
    markup = types.InlineKeyboardMarkup()

    if bet1=='1xBET':
        link1='https://m.1xbet.kz/line/Esports/'
    elif bet1=='OLIMPBET':
        link1='https://olimpbet.kz/betting/cybersport'
    elif bet1=='FONBET':
        link1='https://www.fonbet.kz/esports/disciplines/all'

    if bet2=='1xBET':
        link2='https://m.1xbet.kz/line/Esports/'
    elif bet2=='OLIMPBET':
        link2='https://olimpbet.kz/betting/cybersport'
    elif bet2=='FONBET':
        link2='https://www.fonbet.kz/esports/disciplines/all'

    markup.add(types.InlineKeyboardButton(text=bet1,
                                                url=link1),
               types.InlineKeyboardButton(text=bet2,
                                                url=link2))

    return markup

#l=[a[0],a[1],a[2],bet1,op1,coef1,p11,proff1,bet2,op2,coef2,p22,proff2]
def strict_table(l):
    date=l[0]
    time=l[1]
    league=l[2]
    bet1=l[3]
    op1=l[4]
    coef1=l[5]
    p11=l[6]
    proff1=l[7]
    bet2=l[8]
    op2=l[9]
    coef2=l[10]
    p22=l[11]
    proff2=l[12]
    full_len=0
    len_col1=0
    len_col1=0
    text='''
Date: {date}.20 - {time}

{opp1} vs {opp2}
league: {leag}

|    |'''.format(date=date,time=time,opp1=op1,opp2=op2,leag=league)

    if len(bet1)>len(op1):
        text+=bet1+'|'
        len_col1=len(bet1)
    else:

        for i in range(len(op1)-len(bet1)):
            text+=' '
        text+=''+bet1+''
        text+='|'
        len_col1=len(op1)

    if len(bet2)>len(op2):
        text+=''+bet2+'|'
        len_col2=len(bet2)
    else:

        for i in range(len(op2)-len(bet2)):
            text+=' '
        text+=bet2
        text+='|\n|'
        len_col2=len(op2)

    full_len=8+len_col1+len_col2+2
    for i in range(full_len-4):
        text+='-'
    text+='|\n'

    text+='|Team|'

    for i in range(len_col1-len(op1)):
        text+=' '
    text+=op1
    text+='|'

    for i in range(len_col2-len(op2)):
        text+=' '
    text+=op2
    text+='|'

    text+='\n|'
    for i in range(full_len-4):
        text+='-'
    text+='|\n'

    text+='|coef|'

    for i in range(len_col1-len(coef1)):
        text+=' '
    text+=coef1
    text+='|'

    for i in range(len_col2-len(coef2)):
        text+=' '
    text+=coef2
    text+='|'

    text+='\n|'

    for i in range(full_len-4):
        text+='-'
    text+='|\n'

    text+='|cash|'

    for i in range(len_col1-len(p11)-1):
        text+=' '
    text+=p11
    text+='%|'

    for i in range(len_col2-len(p22)-1):
        text+=' '
    text+=p22
    text+='%|'
    text+='\n|'
    for i in range(full_len-4):
        text+='-'
    text+='|\n'

    '''text+='|profit|'

    for i in range(len_col1-len(proff1)):
        text+=' '
    text+=proff1
    text+='|'

    for i in range(len_col2-len(proff2)):
        text+=' '
    text+=proff2
    text+='|'

    text+=' ''' #\n

    text+='
'

    return text

#28.04	00:30	Dota 2. Isolation Cup	Miaww	Siberian Team	1xBET	1.805	1.97	OLIMPBET	3.1	1.34	OLIMPBET Siberian Team	38.856%	20.45360000000001%	1xBET Miaww	61.144%	20.45367999999999%

while True:
    try:
        if (datetime.datetime.now().minute * 0 == 0):
            conn = sqlite3.connect('FOR.db')
            c = conn.cursor()
            c.execute('delete from FIND')
            conn.commit()

            BET.bets()
            ans = BET.find()
            compare_and_insert(ans)

            text = None
            token = "1025277727:AAGKOX9qGF0mw_Cry--3ufdMQ3RGq50Qv3Y"
            url = "https://api.telegram.org/bot%s/sendMessage" % token

            op1 = ''
            op2 = ''
            b=c.execute('select * from FIND')

            for a in b.fetchall():
                m=a[11].split()      # OLIMPBET Siberian Team   ------ >  m = ['OLIMPBET', 'Siberian', 'Team']
                n=a[14].split()
                bet1=m[0]            # bet1= m[0]= 'OLIMPBET'
                bet2=n[0]
                op1=''
                op2=''

                p11 =    float_of_degree_three(a[12])   # 1.23456789 ---> 1.234
                p22 =    float_of_degree_three(a[15])
                proff1 = float_of_degree_three(a[13])
                proff2 = float_of_degree_three(a[16])

                for i in range(1,len(m)):
                    op1+=m[i]                 # op1= 'Siberian' + 'Team'
                    op1+=' '
                for i in range(1,len(n)):
                    op2+=n[i]
                    op2+=' '

                coef1, coef2 = get_coefficients(a,bet1,op1)

                l=[a[0],a[1],a[2],bet1,op1,coef1,p11,proff1,bet2,op2,coef2,p22,proff2]

                voice_msg_id=creating_and_sending_audio(l)
                deleting_audio(l)

                text = getting_emoji(round(float(proff1)))+'% profit\n........................................\n'
                if (len(op1)+len(op2))<0:
                    print(len(op1)+len(op2))
                    text+='''
Date: {date}.20 - {time}

{opp1} vs {opp2}
league: {leag}

*Bet1:* _{b1}_
*Team1:* _{opp1}_
*coef1:* _{c1}_
*cash:* _{p1}%_
*profit:*_{prof1}%_

*Bet2:* _{b2}_
*Team2:* _{opp2}_
*coef2:* _{c2}_
*cash:* _{p2}%_
*profit:* _{prof2}%_ '''.format(leag=a[2],date=a[0],time=a[1],b1=bet1,
                                            opp1=op1,p1=p11,b2=bet2,opp2=op2,p2=p22,
                                            prof1=proff1,prof2=proff2,c1=coef1,c2=coef2)
                    tb.send_message(chat_id='@BET_BUSTERS', text = text,reply_markup=make_keyboard(bet1,bet2),parse_mode='Markdown')
                else:
                    print(len(op1)+len(op2))
                    text += strict_table(l)
                    tb.send_message(chat_id='@BET_BUSTERS', text = text,reply_markup=make_keyboard(bet1,bet2),parse_mode='HTML')
                print(text)
                print(l)
                csvfile.append(l)
            conn.commit()
            conn.close()

        import csv
        with open('csvfile.csv', 'w', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)
            writer.writerows(csvfile)

        print('DONE!')
        #break
    except:
        #break
        print('Something goes wrong')


import os
import csv

read_path = os.path.join("Resources", "budget_data.csv")

months = 0
profit_loss = 0
average_change = 0.00
max_inc = 0
max_dec = 0
max_inc_month = ""
max_dec_month = ""
this_month = 0
last_month = None
changes = []
rate_change = 0
month_name = ""

with open(read_path, newline = "", encoding = "utf8") as temp_file:
    budget_data = csv.reader(temp_file, delimiter = ",")

    #read the header row first (skip if there is no header)
    data_header = next(temp_file)

    #run through each row in csv file
    for row in budget_data:
        this_month = int(row[1])    #identify this month's revenue
        month_name = row[0]         #identify current month
        months += 1                 #count the months
        profit_loss += this_month   #add monthly revenue to total

    #average_change = profit_loss / months
        if last_month is not None: #do not check on first month; no change
            rate_change = (this_month-last_month) #calculate change in rate from previous month
            changes.append(rate_change) #add change to list of month changes
            if rate_change > max_inc:   #if current change rate is highest,
                max_inc = rate_change   #overwrite max rate with new value
                max_inc_month = month_name
            elif rate_change < max_dec: #if current change rate is lowest,
                max_dec = rate_change   #overwrite max decrease with new value
                max_dec_month = month_name
        last_month = this_month       #current month becomes last month for next loop
    average_change = sum(changes)/len(changes)  #calculate average monthly revenue change

#print results
print(f"Total Months: {months}\nTotal: ${profit_loss}\nAverage Change: ${round(average_change, 2)}\nGreatest Increase in Profits: {max_inc_month} (${max_inc})\nGreatest Decrease in Profits: {max_dec_month} (${max_dec})")

# Specify the file to write to
write_path = os.path.join("output.txt")

# Open the file using "write" mode. Specify the variable to hold the contents
with open(write_path, "w") as output_file:
    output_file.write(f"Total Months: {months}\nTotal: ${profit_loss}\nAverage Change: ${round(average_change, 2)}\nGreatest Increase in Profits: {max_inc_month} (${max_inc})\nGreatest Decrease in Profits: {max_dec_month} (${max_dec})")


class Solution:
    def balancedStringSplit(self, s: str) -> int:
        """
        Return maximum amount of split balanced strings

        constraints:
            no empty strings
            s[i] contains only 'L' or 'R'
            s is a balanced string

        examples:
            input: s = "RLRRRLLRLL"
            output: 2
            explanation: s can be split into "RL" and "RRRLLRLL"
            - considering only amount of R's and L's
            - order doesn't matter

        pseudocode:

            def balancedStringSplit(s):
                iterate through string:
                    if char is R, increment R counter
                    elif char is L, increment L counter

                    if R and L counters are equal, increment split balanced string counter
                return counter

        """
        # Space complexity -> O(1)

        # Counters for 'L' and 'R' chars
        left_count, right_count = 0, 0
        # Amount of split balanced strings
        count = 0

        # Time complexity -> O(n)
        for char in s:
            if char == 'L':
                left_count += 1
            elif char == 'R':
                right_count += 1

            if left_count == right_count:
                count += 1

        return count


# runtime: 99.95%, memory usage: 0%
def isPowerOfTwo(n: int) -> bool:
    if n < 1:
        return False

    while n > 1:
        if n % 2 != 0:
            return False

        n >>= 1

    return True

if __name__ == '__main__':
    result = isPowerOfTwo(218)

    print(result)


class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
        if image[sr][sc] == newColor:
            return image
        m, n = len(image), len(image[0])
        newimage = [[image[i][j] for j in range(n)]for i in range(m)]
        havebeen = [[0 for j in range(n)]for i in range(m)]
        def judge(x:int,y:int,t:int)->bool:
            if x>m-1 or y>n-1 or x<0 or y<0:
                return False
            if image[x][y]!=t:
                return False
            return True
        dx, dy = [-1,1,0,0], [0,0,1,-1]
        def dfs(x:int,y:int)->None:
            havebeen[x][y] = 1
            newimage[x][y] = newColor
            for i in range(4):
                nx, ny = x+dx[i],y+dy[i]
                if judge(nx,ny,image[x][y]) and not havebeen[nx][ny]:
                    dfs(nx,ny)
            return
        dfs(sr,sc)
        return newimage


import serial
import csv
import re

port = "/dev/cu.usbmodem14501"
rate = 9600

srl_Heartrate = serial.Serial(port,rate)
#srl_Heartrate.open()
#filename = input("Ardu_heartN.csv->")

myfile = open("Ardu_heart5.csv","w")
'''
while(1):
        val = srl_Heartrate.readline()
        byte_to_str = val.decode()
        dec = int(byte_to_str)
        myfile.write(dec)
        myfile.write('¥n')
        print(dec)
        #writer = csv.writer(file)
        #writer.writerow()

myfile.close()
'''
a = 0
try:                        # try:の部分にループ処理を書く
    while True:
        if a % 2 == 0 :

            val = srl_Heartrate.readline()
            byte_to_str = val.decode()
            #dec = int(byte_to_str)
            myfile.write(byte_to_str)
            print(byte_to_str)
            #writer = csv.writer(file)
            #writer.writerow()
        else:
            val = srl_Heartrate.readline()
            byte_to_str = val.decode()
            #dec = int(byte_to_str)
            print("time:s",byte_to_str)
        a+=1
except KeyboardInterrupt:   # exceptに例外処理を書く
    print('stop!')
    myfile.close()
    srl_Heartrate.close()

'''
while(1):
    val = srl_Heartrate.readline()
    byte_to_str = val.decode()
    dec = int(byte_to_str)
    print(dec)
    with open("Ardu_Heartrate.csv","w") as file:
        writer = csv.writer(file)
        writer
'''


from simfile import SimFile
from simulation import Simulation

# Specify the absolute paths of some of the input files and the output folder

outputDir   = "/Users/rik/Work/PLATO/Simulation/"

# Set up a Simulation object

sim = Simulation("Simul01", ccdParametersFile="/Users/rik/Work/PLATO/myInputs/ccd_parameters.xml")
sim.setOutputDir(outputDir)

# Run the simulation without the flux extraction, with an HDF5 file as output

simFile = sim.run(doPhotometry=False)

# Look at an exposure image

simFile.showImage(0)

# Read an imagette from the output HDF5 file

imagette = simFile.getImagette(15209, 0, radius=2)


""" Render, httpresponse, getobjector404... """
from django.http import HttpResponseRedirect, Http404  # , HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from django.contrib.contenttypes.models import ContentType
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q

from .models import Post
from .forms import PostForm

from comments.models import Comment

# Create your views here.
def post_create(request):
    """ Create Post """
    if not request.user.is_authenticated():
        raise Http404

    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    # request.FILES has to be defined when file upload in Form
    form = PostForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        instance.save()
        messages.success(request, 'Post succesfully added')
        return HttpResponseRedirect(instance.get_absolute_url())
    # else:
    #     messages.error(request, 'Cannot create post')

    context = {
        'form': form,
    }
    return render(request, 'post_form.html', context)

def post_detail(request, post_slug=None):
    """ Post Detail """
    # instance = Post.objects.get('id=2')
    today = timezone.now().date()
    instance = get_object_or_404(Post, slug=post_slug)
    if not request.user.is_staff or not request.user.is_superuser:
        if instance.draft or instance.publish > today:
            raise Http404
    # Building the generic foreign key and fetch all comments with it
    content_type = ContentType.objects.get_for_model(Post)
    obj_id = instance.id
    comments = Comment.objects.filter(content_type=content_type, object_id=obj_id)
    context = {
        'title': instance.title,
        'instance': instance,
        'today': today,
        'comments': comments,
    }
    return render(request, 'post_detail.html', context)

def post_list(request):
    """ List all Posts for staff users or superuser"""
    today = timezone.now().date()
    if request.user.is_staff or request.user.is_superuser:
        posts_list = Post.objects.all()
        is_staff = True
    else:
        # Example using filter function on querying
        # posts_list = Post.objects.filter(draft = False, publish__lte = timezone.now())

        # Example using the created Model manager for the filtering
        posts_list = Post.objects.active()
        is_staff = False

    # Process search
    query = request.GET.get('q')
    if query:
        posts_list = posts_list.filter(Q(title__icontains=query) |
                                       Q(content__icontains=query) |
                                       Q(user__first_name__icontains=query) |
                                       Q(user__last_name__icontains=query)
                                       ).distinct()

    # Process pages
    paginator = Paginator(posts_list, 5)  # Show 5 posts per page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)

    context = {
        'posts': posts,
        'is_staff': is_staff,
        'today': today,
    }
    return render(request, 'post_list.html', context)

def post_update(request, post_slug):
    """ Update Post """
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404

    instance = get_object_or_404(Post, slug=post_slug)
    form = PostForm(request.POST or None,
                    request.FILES or None, instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        print(form.cleaned_data.get("title"))
        instance.save()
        messages.success(request, 'Post succesfully updated')
        return HttpResponseRedirect(instance.get_absolute_url())
    else:
        messages.error(request, 'Cannot create post')

    context = {
        'title': instance.title,
        'instance': instance,
        'form': form,
    }
    return render(request, 'post_form.html', context)

def post_delete(request, post_slug):
    """ Delete Post """
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404

    instance = get_object_or_404(Post, slug=post_slug)
    instance.delete()
    return redirect('posts:list')


from queue import Queue
from collections import deque
from logging import getLogger
from threading import Thread
from sqlalchemy.exc import DBAPIError
from time import time
from random import shuffle

import asyncio

from utils import dump_pickle, load_pickle, get_current_hour, time_until_time, round_coords, get_altitude, get_point_altitudes

import db

class Spawns:
    """Manage spawn points and times"""
    session = db.Session()
    spawns = {}
    despawn_times = {}
    mysteries = set()
    altitudes = {}

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

    def update(self):
        self.spawns, self.despawn_times, m, a = db.get_spawns(self.session)
        self.mysteries.update(m)
        self.altitudes.update(a)
        if not self.altitudes:
            self.altitudes = get_point_altitudes()

    def have_id(self, spawn_id):
        return spawn_id in self.despawn_times

    def get_altitude(self, point):
        point = round_coords(point)
        alt = self.altitudes.get(point)
        if not alt:
            alt = get_altitude(point)
            self.altitudes[point] = alt
        return alt

    def items(self):
        return self.spawns.items()

    def get_mysteries(self):
        mysteries = deque(self.mysteries)
        shuffle(mysteries)
        return mysteries

    def add_mystery(self, point):
        rounded = round_coords(point, precision=4)
        self.mysteries.add(rounded)

    def have_mystery(self, point):
        rounded = round_coords(point, precision=4)
        return rounded in self.mysteries

    def add_despawn(self, spawn_id, despawn_time):
        self.despawn_times[spawn_id] = despawn_time

    def get_despawn_seconds(self, spawn_id):
        return self.despawn_times.get(spawn_id)

    def get_despawn_time(self, spawn_id, seen=None):
        if self.have_id(spawn_id):
            now = seen or time()
            hour = get_current_hour(now=now)
            despawn_time = self.get_despawn_seconds(spawn_id) + hour
            if now > despawn_time - 88:
                despawn_time += 3600
            return despawn_time
        else:
            return None

    def get_time_till_hidden(self, spawn_id):
        if not self.have_id(spawn_id):
            return None
        despawn_seconds = self.get_despawn_seconds(spawn_id)
        return time_until_time(despawn_seconds)

    @property
    def total_length(self):
        return len(self.despawn_times) + len(self.mysteries)

class DatabaseProcessor(Thread):
    spawns = Spawns()

    def __init__(self):
        super().__init__()
        self.queue = Queue()
        self.logger = getLogger('dbprocessor')
        self.running = True
        self._clean_cache = False
        self.count = 0
        self._commit = False

    def stop(self):
        self.running = False

    def add(self, obj):
        self.queue.put(obj)

    def run(self):
        session = db.Session()

        while self.running or not self.queue.empty():
            if self._clean_cache:
                try:
                    db.SIGHTING_CACHE.clean_expired()
                    db.MYSTERY_CACHE.clean_expired(session)
                except Exception as e:
                    self.logger.error('Failed to clean cache. {}'.format(e))
                finally:
                    self._clean_cache = False
            try:
                item = self.queue.get()

                if item['type'] == 'pokemon':
                    if item['valid']:
                        db.add_sighting(session, item)
                        if item['valid'] == True:
                            db.add_spawnpoint(session, item, self.spawns)
                    else:
                        db.add_mystery(session, item, self.spawns)
                    self.count += 1
                elif item['type'] == 'fort':
                    db.add_fort_sighting(session, item)
                elif item['type'] == 'pokestop':
                    db.add_pokestop(session, item)
                elif item['type'] == 'kill':
                    break
                self.logger.debug('Item saved to db')
                if self._commit:
                    session.commit()
                    self._commit = False
            except DBAPIError as e:
                session.rollback()
                self.logger.exception('A wild DB exception appeared! {}'.format(e))
            except Exception as e:
                self.logger.exception('A wild exception appeared! {}'.format(e))

        session.commit()
        session.close()

    def clean_cache(self):
        self._clean_cache = True

    def commit(self):
        self._commit = True


import api
import csv

def main():
    api.authenticate()
    tracks = api.get_tracks(random=True, limit=253)

    with open(f"playlist{len(tracks)}.csv", "w+") as outfile:
        writer = csv.DictWriter(outfile, fieldnames=["track", "artist", "album"])
        writer.writeheader()

        print("Generating csv")

        for track in tracks:
            writer.writerow({
                "track": track["title"],
                "artist": track["artist"]["title"],
                "album": track["album"]["title"]
            })

main()


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

import os
import numpy as np
import matplotlib.pyplot as plt
import PlotSignals as PS
import pandas as pd
import datetime as dt

Prefix     = './inputs/'
pathToSave = './results/'
listFiles  = ['Xtrain_extract.out', 'Ytrain_extract.out', 'Rtrain_extract.out']

def main():

    # first file ##################################

    # The observations
    filenameorig = pathToSave+listFiles[1]
    Y = np.loadtxt(filenameorig)
    N = Y.shape[0]
    print('number of data : ', N)

    i=1
    #PS.draw(Y, 1, pathToSave, filename, listFiles[0], 0, N, save=False)

    # The jumps ##################################
    filenameorig = pathToSave+listFiles[2]
    basename = os.path.basename(filenameorig)
    filename, file_extension = os.path.splitext(basename)
    print(filename, file_extension)
    R = np.zeros(shape=(N))+0.5
    R[0:12]    = 1.
    R[28:40]   = 0.
    R[48:107]  = 1.
    R[120:136] = 0.
    R[151:202] = 1.
    R[219:232] = 0.
    R[244:302] = 1.
    R[317:328] = 0.
    R[340:402] = 1.
    R[413:425] = 0.
    R[438:481] = 1.

    #i=2
    #PS.draw(R, i, pathToSave, filename, listFiles[1], 0, N, save=False)
    np.savetxt(filenameorig, R)

    # draw both of them
    PS.draw1 (Y, R, 'Ytrain_extract', 1, 'Rtrain_extract', 2, pathToSave, "YRtrain", 100, 250, save=True)

    # save as csv file
    filenameorig = pathToSave+listFiles[0]
    X = np.loadtxt(filenameorig)
    XYR = np.zeros(shape=(3, N))
    XYR[0,:] = X
    XYR[1,:] = Y
    XYR[2,:] = R
    pd1= pd.DataFrame(np.transpose(XYR))
    pd1.insert(0, 'TimeStamp', pd.date_range('20131212',freq='H', periods=500))
    pd1.columns = ['Timestamp','X','Y','R_GT']
    pd1.to_csv(pathToSave + 'XYR.csv', index=False)

if __name__ == '__main__':
    main()


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

"""BigMLer - A Higher Level API to BigML's API

# Basic usage
python bigmler.py \
    --train data/iris.csv \
    --test data/test_iris.csv
    --no-test-header

# Create an 10-model ensemble using bagging
python bigmler.py
    --train train.csv \
    --output submission.csv \
    --objective 0 \
    --types types.txt \
    --name 'Iris Ensemble' \
    --number_of_models 10 \
    --sample_rate 0.75 \
    --replacement \
    --tag my_ensemble

# Make predictions using models tagged with my_ensemble
python bigmler.py \
    --model_tag my_ensemble \
    --test test.csv
    --no-test-header

"""
from __future__ import absolute_import

import sys
import os
import datetime
import csv
import re
import shlex

try:
    import simplejson as json
except ImportError:
    import json

import bigml.api
import bigmler.utils as u

from bigml.model import Model
from bigml.multimodel import MultiModel
from bigml.multivote import COMBINATION_WEIGHTS, COMBINER_MAP, PLURALITY
from bigml.fields import Fields

from bigml.util import (localize, console_log, get_csv_delimiter,
                        get_predictions_file_name)

from bigmler.options import create_parser
from bigmler.defaults import get_user_defaults
from bigmler.defaults import DEFAULTS_FILE

MAX_MODELS = 10
EVALUATE_SAMPLE_RATE = 0.8
SEED = "BigML, Machine Learning made easy"
# Date and time in format SunNov0412_120510 to name and tag resources
NOW = datetime.datetime.now().strftime("%a%b%d%y_%H%M%S")
COMMAND_LOG = ".bigmler"
DIRS_LOG = ".bigmler_dir_stack"
SESSIONS_LOG = "bigmler_sessions"
LOG_FILES = [COMMAND_LOG, DIRS_LOG, u.NEW_DIRS_LOG]

def remote_predict(models, headers, output_path, number_of_tests, resume,
                   verbosity, test_reader, exclude, fields, api,
                   prediction_file, method, tags, objective_field,
                   session_file, test_set_header, log, debug):
    """Retrieve predictions remotely, combine them and save predictions to file

    """

    predictions_files = []
    prediction_args = {
        "tags": tags
    }
    for model in models:
        if not isinstance(model, basestring) and 'resource' in model:
            model = model['resource']
        predictions_file = get_predictions_file_name(model,
                                                     output_path)
        predictions_files.append(predictions_file)
        if (not resume or
            not u.checkpoint(u.are_predictions_created, predictions_file,
                             number_of_tests, debug=debug)):
            message = u.dated("Creating remote predictions.\n")
            u.log_message(message, log_file=session_file,
                          console=verbosity)

            predictions_file = csv.writer(open(predictions_file, 'w', 0))
            for row in test_reader:
                for index in exclude:
                    del row[index]
                input_data = fields.pair(row, headers, objective_field)
                prediction = api.create_prediction(model, input_data,
                                                   by_name=test_set_header,
                                                   wait_time=0,
                                                   args=prediction_args)
                u.log_message("%s\n" % prediction['resource'], log_file=log)
                prediction_row = u.prediction_to_row(prediction)
                predictions_file.writerow(prediction_row)
    u.combine_votes(predictions_files,
                    Model(models[0]).to_prediction,
                    prediction_file, method)

def local_predict(models, headers, test_reader, exclude, fields, method,
                  objective_field, output, test_set_header):
    """Get local predictions, combine them and save predictions to file

    """
    local_model = MultiModel(models)
    for row in test_reader:
        for index in exclude:
            del row[index]
        input_data = fields.pair(row, headers, objective_field)
        prediction = local_model.predict(input_data,
                                         by_name=test_set_header,
                                         method=method)
        u.write_prediction(prediction, output)

def local_batch_predict(models, headers, test_reader, exclude, fields, resume,
                        output_path, max_models, number_of_tests, api, output,
                        verbosity, method, objective_field, session_file,
                        debug):
    """Get local predictions form partial Multimodel, combine and save to file

    """
    def draw_progress_bar(current, total):
        """Draws a text based progress report.

        """
        pct = 100 - ((total - current) * 100) / (total)
        console_log("Predicted on %s out of %s models [%s%%]" % (
            localize(current), localize(total), pct))

    models_total = len(models)
    models_splits = [models[index:(index + max_models)] for index
                     in range(0, models_total, max_models)]
    input_data_list = []
    for row in test_reader:
        for index in exclude:
            del row[index]
        input_data_list.append(fields.pair(row, headers,
                                           objective_field))
    total_votes = []
    models_count = 0
    for models_split in models_splits:
        if resume:
            for model in models_split:
                pred_file = get_predictions_file_name(model,
                                                      output_path)
                u.checkpoint(u.are_predictions_created,
                             pred_file,
                             number_of_tests, debug=debug)
        complete_models = []
        for index in range(len(models_split)):
            complete_models.append(api.check_resource(
                models_split[index], api.get_model))

        local_model = MultiModel(complete_models)
        local_model.batch_predict(input_data_list,
                                  output_path, reuse=True)
        votes = local_model.batch_votes(output_path)
        models_count += max_models
        if models_count > models_total:
            models_count = models_total
        if verbosity:
            draw_progress_bar(models_count, models_total)
        if total_votes:
            for index in range(0, len(votes)):
                predictions = total_votes[index].predictions
                predictions.extend(votes[index].predictions)
        else:
            total_votes = votes
    message = u.dated("Combining predictions.\n")
    u.log_message(message, log_file=session_file, console=verbosity)
    for multivote in total_votes:
        u.write_prediction(multivote.combine(method), output)

def predict(test_set, test_set_header, models, fields, output,
            objective_field, remote=False, api=None, log=None,
            max_models=MAX_MODELS, method=0, resume=False,
            tags=None, verbosity=1, session_file=None, debug=False):
    """Computes a prediction for each entry in the `test_set`.

       Predictions can be computed remotely, locally using MultiModels built
       on all the models or locally using MultiModels on subgroups of models.
       Chosing a max_batch_models value not bigger than the number_of_models
       flag will lead to the last case, where memory usage is bounded and each
       model predictions are saved for further use.
    """

    try:
        test_reader = csv.reader(open(test_set, "U"),
                                 delimiter=get_csv_delimiter(),
                                 lineterminator="\n")
    except IOError:
        sys.exit("Error: cannot read test %s" % test_set)

    headers = None
    exclude = []
    if test_set_header:
        headers = test_reader.next()
        # validate headers against model fields excluding objective_field,
        # that may be present or not
        fields_names = [fields.fields[fields.field_id(i)]
                        ['name'] for i in
                        sorted(fields.fields_by_column_number.keys())
                        if i != fields.field_column_number(objective_field)]
        headers = [unicode(header, "utf-8") for header in headers]
        exclude = [i for i in range(len(headers)) if not headers[i]
                   in fields_names]
        exclude.reverse()
        if len(exclude):
            if (len(headers) - len(exclude)):
                print (u"WARNING: predictions will be processed but some data"
                       u" might not be used. The used fields will be:\n\n%s"
                       u"\n\nwhile the headers found in the test file are:"
                       u"\n\n%s" %
                       (",".join(fields_names),
                        ",".join(headers))).encode("utf-8")
                for index in exclude:
                    del headers[index]
            else:
                raise Exception((u"No test field matches the model fields.\n"
                                 u"The expected fields are:\n\n%s\n\nwhile "
                                 u"the headers found in the test file are:\n\n"
                                 u"%s\n\nUse --no-test-header flag if first li"
                                 u"ne should not be interpreted as headers." %
                                 (",".join(fields_names),
                                  ",".join(headers))).encode("utf-8"))

    prediction_file = output
    output_path = u.check_dir(output)
    output = open(output, 'w', 0)
    number_of_tests = None
    if resume:
        number_of_tests = u.file_number_of_lines(test_set)
        if test_set_header:
            number_of_tests -= 1
    # Remote predictions: predictions are computed in bigml.com and stored
    # in a file named after the model in the following syntax:
    #     model_[id of the model]__predictions.csv
    # For instance,
    #     model_50c0de043b563519830001c2_predictions.csv
    if remote:
        remote_predict(models, headers, output_path, number_of_tests, resume,
                       verbosity, test_reader, exclude, fields, api,
                       prediction_file, method, tags, objective_field,
                       session_file, test_set_header, log, debug)
    # Local predictions: Predictions are computed locally using models' rules
    # with MultiModel's predict method
    else:
        message = u.dated("Creating local predictions.\n")
        u.log_message(message, log_file=session_file, console=verbosity)
        # For a small number of models, we build a MultiModel using all of
        # the given models and issue a combined prediction
        if len(models) < max_models:
            local_predict(models, headers, test_reader, exclude, fields,
                          method, objective_field, output, test_set_header)
        # For large numbers of models, we split the list of models in chunks
        # and build a MultiModel for each chunk, issue and store predictions
        # for each model and combine all of them eventually.
        else:
            local_batch_predict(models, headers, test_reader, exclude, fields,
                                resume, output_path, max_models,
                                number_of_tests, api, output,
                                verbosity, method, objective_field,
                                session_file, debug)
    output.close()

def compute_output(api, args, training_set, test_set=None, output=None,
                   objective_field=None,
                   description=None,
                   field_attributes=None,
                   types=None,
                   dataset_fields=None,
                   model_fields=None,
                   name=None, training_set_header=True,
                   test_set_header=True, model_ids=None,
                   votes_files=None, resume=False, fields_map=None):
    """ Creates one or more models using the `training_set` or uses the ids
    of previously created BigML models to make predictions for the `test_set`.

    """
    source = None
    dataset = None
    model = None
    models = None
    fields = None

    path = u.check_dir(output)
    session_file = "%s%s%s" % (path, os.sep, SESSIONS_LOG)
    csv_properties = {}
    # If logging is required, open the file for logging
    log = None
    if args.log_file:
        u.check_dir(args.log_file)
        log = args.log_file
        # If --clear_logs the log files are cleared
        if args.clear_logs:
            try:
                open(log, 'w', 0).close()
            except IOError:
                pass

    if (training_set or (args.evaluate and test_set)):
        if resume:
            resume, args.source = u.checkpoint(u.is_source_created, path,
                                               bigml.api, debug=args.debug)
            if not resume:
                message = u.dated("Source not found. Resuming.\n")
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)

    # If neither a previous source, dataset or model are provided.
    # we create a new one. Also if --evaluate and test data are provided
    # we create a new dataset to test with.
    data_set = None
    if (training_set and not args.source and not args.dataset and
            not args.model and not args.models):
        data_set = training_set
        data_set_header = training_set_header
    elif (args.evaluate and test_set and not args.source):
        data_set = test_set
        data_set_header = test_set_header

    if not data_set is None:

        source_args = {
            "name": name,
            "description": description,
            "category": args.category,
            "tags": args.tag,
            "source_parser": {"header": data_set_header}}
        message = u.dated("Creating source.\n")
        u.log_message(message, log_file=session_file, console=args.verbosity)
        source = api.create_source(data_set, source_args,
                                   progress_bar=args.progress_bar)
        source = api.check_resource(source, api.get_source)
        message = u.dated("Source created: %s\n" % u.get_url(source, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        u.log_message("%s\n" % source['resource'], log_file=log)

        fields = Fields(source['object']['fields'],
                        source['object']['source_parser']['missing_tokens'],
                        source['object']['source_parser']['locale'])
        source_file = open(path + '/source', 'w', 0)
        source_file.write("%s\n" % source['resource'])
        source_file.write("%s\n" % source['object']['name'])
        source_file.flush()
        source_file.close()

    # If a source is provided, we retrieve it.
    elif args.source:
        message = u.dated("Retrieving source. %s\n" %
                          u.get_url(args.source, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        source = api.get_source(args.source)

    # If we already have source, we check that is finished and extract the
    # fields, and update them if needed.
    if source:
        if source['object']['status']['code'] != bigml.api.FINISHED:
            message = u.dated("Retrieving source. %s\n" %
                              u.get_url(source, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            source = api.check_resource(source, api.get_source)
        csv_properties = {'missing_tokens':
                          source['object']['source_parser']['missing_tokens'],
                          'data_locale':
                          source['object']['source_parser']['locale']}

        fields = Fields(source['object']['fields'], **csv_properties)
        update_fields = {}
        if field_attributes:
            for (column, value) in field_attributes.iteritems():
                update_fields.update({
                    fields.field_id(column): value})
            message = u.dated("Updating source. %s\n" %
                              u.get_url(source, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            source = api.update_source(source, {"fields": update_fields})

        update_fields = {}
        if types:
            for (column, value) in types.iteritems():
                update_fields.update({
                    fields.field_id(column): {'optype': value}})
            message = u.dated("Updating source. %s\n" %
                              u.get_url(source, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            source = api.update_source(source, {"fields": update_fields})

    if (training_set or args.source or (args.evaluate and test_set)):
        if resume:
            resume, args.dataset = u.checkpoint(u.is_dataset_created, path,
                                                bigml.api,
                                                debug=args.debug)
            if not resume:
                message = u.dated("Dataset not found. Resuming.\n")
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
    # If we have a source but not dataset or model has been provided, we
    # create a new dataset if the no_dataset option isn't set up. Also
    # if evaluate is set and test_set has been provided.
    if ((source and not args.dataset and not args.model and not model_ids and
            not args.no_dataset) or
            (args.evaluate and args.test_set and not args.dataset)):
        dataset_args = {
            "name": name,
            "description": description,
            "category": args.category,
            "tags": args.tag
        }

        if args.json_filter:
            dataset_args.update(json_filter=args.json_filter)
        elif args.lisp_filter:
            dataset_args.update(lisp_filter=args.lisp_filter)

        input_fields = []
        if dataset_fields:
            for name in dataset_fields:
                input_fields.append(fields.field_id(name))
            dataset_args.update(input_fields=input_fields)
        message = u.dated("Creating dataset.\n")
        u.log_message(message, log_file=session_file, console=args.verbosity)
        dataset = api.create_dataset(source, dataset_args)
        dataset = api.check_resource(dataset, api.get_dataset)
        message = u.dated("Dataset created: %s\n" % u.get_url(dataset, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        u.log_message("%s\n" % dataset['resource'], log_file=log)
        dataset_file = open(path + '/dataset', 'w', 0)
        dataset_file.write("%s\n" % dataset['resource'])
        dataset_file.flush()
        dataset_file.close()

    # If a dataset is provided, let's retrieve it.
    elif args.dataset:
        message = u.dated("Retrieving dataset. %s\n" %
                          u.get_url(args.dataset, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        dataset = api.get_dataset(args.dataset)

    # If we already have a dataset, we check the status and get the fields if
    # we hadn't them yet.
    if dataset:
        if dataset['object']['status']['code'] != bigml.api.FINISHED:
            message = u.dated("Retrieving dataset. %s\n" %
                              u.get_url(dataset, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            dataset = api.check_resource(dataset, api.get_dataset)
        if not csv_properties:
            csv_properties = {'data_locale':
                              dataset['object']['locale']}
        if args.public_dataset:
            if not description:
                raise Exception("You should provide a description to publish.")
            public_dataset = {"private": False}
            if args.dataset_price:
                message = u.dated("Updating dataset. %s\n" %
                                  u.get_url(dataset, api))
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
                public_dataset.update(price=args.dataset_price)
            message = u.dated("Updating dataset. %s\n" %
                              u.get_url(dataset, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            dataset = api.update_dataset(dataset, public_dataset)
        fields = Fields(dataset['object']['fields'], **csv_properties)

    # If we have a dataset but not a model, we create the model if the no_model
    # flag hasn't been set up.
    if (dataset and not args.model and not model_ids and not args.no_model):
        model_args = {
            "name": name,
            "description": description,
            "category": args.category,
            "tags": args.tag
        }
        if objective_field is not None:
            model_args.update({"objective_field":
                               fields.field_id(objective_field)})
        # If evaluate flag is on, we choose a deterministic sampling with 80%
        # of the data to create the model
        if args.evaluate:
            if args.sample_rate == 1:
                args.sample_rate = EVALUATE_SAMPLE_RATE
            seed = SEED
            model_args.update(seed=seed)

        input_fields = []
        if model_fields:
            for name in model_fields:
                input_fields.append(fields.field_id(name))
            model_args.update(input_fields=input_fields)

        if args.pruning and args.pruning != 'smart':
            model_args.update(stat_pruning=(args.pruning == 'statistical'))

        model_args.update(sample_rate=args.sample_rate,
                          replacement=args.replacement,
                          randomize=args.randomize)
        model_ids = []
        models = []
        if resume:
            resume, model_ids = u.checkpoint(u.are_models_created, path,
                                             args.number_of_models,
                                             bigml.api, debug=args.debug)
            if not resume:
                message = u.dated("Found %s models out of %s. Resuming.\n" %
                                  (len(model_ids),
                                   args.number_of_models))
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
            models = model_ids
            args.number_of_models -= len(model_ids)

        model_file = open(path + '/models', 'w', 0)
        for model_id in model_ids:
            model_file.write("%s\n" % model_id)
        last_model = None
        if args.number_of_models > 0:
            message = u.dated("Creating %s.\n" %
                              u.plural("model", args.number_of_models))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            for i in range(1, args.number_of_models + 1):
                if i > args.max_parallel_models:
                    api.check_resource(last_model, api.get_model)
                model = api.create_model(dataset, model_args)
                u.log_message("%s\n" % model['resource'], log_file=log)
                last_model = model
                model_ids.append(model['resource'])
                models.append(model)
                model_file.write("%s\n" % model['resource'])
                model_file.flush()
            if args.number_of_models < 2 and args.verbosity:
                if model['object']['status']['code'] != bigml.api.FINISHED:
                    model = api.check_resource(model, api.get_model)
                    models[0] = model
                message = u.dated("Model created: %s.\n" %
                                  u.get_url(model, api))
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
        model_file.close()

    # If a model is provided, we retrieve it.
    elif args.model:
        message = u.dated("Retrieving model. %s\n" %
                          u.get_url(args.model, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        model = api.get_model(args.model)

    elif args.models or args.model_tag:
        models = model_ids[:]

    if model_ids and test_set and not args.evaluate:
        model_id = ""
        if len(model_ids) == 1:
            model_id = model_ids[0]
        message = u.dated("Retrieving %s. %s\n" %
                          (u.plural("model", len(model_ids)),
                           u.get_url(model_id, api)))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        if len(model_ids) < args.max_batch_models:
            models = []
            for model in model_ids:
                model = api.check_resource(model, api.get_model)
                models.append(model)
            model = models[0]
        else:
            model = api.check_resource(model_ids[0], api.get_model)
            models[0] = model

    # We check that the model is finished and get the fields if haven't got
    # them yet.
    if model and not args.evaluate and (test_set or args.black_box
                                        or args.white_box):
        if model['object']['status']['code'] != bigml.api.FINISHED:
            message = u.dated("Retrieving model. %s\n" %
                              u.get_url(model, api))
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            model = api.check_resource(model, api.get_model)
        if args.black_box:
            if not description:
                raise Exception("You should provide a description to publish.")
            model = api.update_model(model, {"private": False})
        if args.white_box:
            if not description:
                raise Exception("You should provide a description to publish.")
            public_model = {"private": False, "white_box": True}
            if args.model_price:
                message = u.dated("Updating model. %s\n" %
                                  u.get_url(model, api))
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
                public_model.update(price=args.model_price)
            if args.cpp:
                message = u.dated("Updating model. %s\n" %
                                  u.get_url(model, api))
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
                public_model.update(credits_per_prediction=args.cpp)
            model = api.update_model(model, public_model)
        if not csv_properties:
            csv_properties = {'data_locale':
                              model['object']['locale']}
        csv_properties.update(verbose=True)
        if args.user_locale:
            csv_properties.update(data_locale=args.user_locale)

        fields = Fields(model['object']['model']['fields'], **csv_properties)

    if model and not models:
        models = [model]

    if models and test_set and not args.evaluate:
        objective_field = models[0]['object']['objective_fields']
        if isinstance(objective_field, list):
            objective_field = objective_field[0]
        predict(test_set, test_set_header, models, fields, output,
                objective_field, args.remote, api, log,
                args.max_batch_models, args.method, resume, args.tag,
                args.verbosity, session_file, args.debug)

    # When combine_votes flag is used, retrieve the predictions files saved
    # in the comma separated list of directories and combine them
    if votes_files:
        model_id = re.sub(r'.*(model_[a-f0-9]{24})__predictions\.csv$',
                          r'\1', votes_files[0]).replace("_", "/")
        model = api.check_resource(model_id, api.get_model)
        local_model = Model(model)
        message = u.dated("Combining votes.\n")
        u.log_message(message, log_file=session_file,
                      console=args.verbosity)
        u.combine_votes(votes_files, local_model.to_prediction,
                        output, args.method)

    # If evaluate flag is on, create remote evaluation and save results in
    # json and human-readable format.
    if args.evaluate:
        if resume:
            resume, evaluation = u.checkpoint(u.is_evaluation_created, path,
                                              bigml.api,
                                              debug=args.debug)
            if not resume:
                message = u.dated("Evaluation not found. Resuming.\n")
                u.log_message(message, log_file=session_file,
                              console=args.verbosity)
        if not resume:
            evaluation_file = open(path + '/evaluation', 'w', 0)
            evaluation_args = {
                "name": name,
                "description": description,
                "tags": args.tag
            }
            if not fields_map is None:
                update_map = {}
                for (dataset_column, model_column) in fields_map.iteritems():
                    update_map.update({
                        fields.field_id(dataset_column):
                        fields.field_id(model_column)})
                evaluation_args.update({"fields_map": update_map})
            if not ((args.dataset or args.test_set)
                    and (args.model or args.models or args.model_tag)):
                evaluation_args.update(out_of_bag=True, seed=SEED,
                                       sample_rate=args.sample_rate)
            message = u.dated("Creating evaluation.\n")
            u.log_message(message, log_file=session_file,
                          console=args.verbosity)
            evaluation = api.create_evaluation(model, dataset, evaluation_args)
            u.log_message("%s\n" % evaluation['resource'], log_file=log)
            evaluation_file.write("%s\n" % evaluation['resource'])
            evaluation_file.flush()
            evaluation_file.close()
        message = u.dated("Retrieving evaluation. %s\n" %
                          u.get_url(evaluation, api))
        u.log_message(message, log_file=session_file, console=args.verbosity)
        evaluation = api.check_resource(evaluation, api.get_evaluation)
        evaluation_json = open(output + '.json', 'w', 0)
        evaluation_json.write(json.dumps(evaluation['object']['result']))
        evaluation_json.flush()
        evaluation_json.close()
        evaluation_txt = open(output + '.txt', 'w', 0)
        api.pprint(evaluation['object']['result'],
                   evaluation_txt)
        evaluation_txt.flush()
        evaluation_txt.close()

    # Workaround to restore windows console cp850 encoding to print the tree
    if sys.platform == "win32" and sys.stdout.isatty():
        import locale
        data_locale = locale.getlocale()
        if not data_locale[0] is None:
            locale.setlocale(locale.LC_ALL, (data_locale[0], "850"))
        message = (u"\nGenerated files:\n\n" +
                   unicode(u.print_tree(path, " "), "utf-8") + u"\n")
    else:
        message = "\nGenerated files:\n\n" + u.print_tree(path, " ") + "\n"
    u.log_message(message, log_file=session_file, console=args.verbosity)

def main(args=sys.argv[1:]):
    """Main process

    """
    for i in range(0, len(args)):
        if args[i].startswith("--"):
            args[i] = args[i].replace("_", "-")
    # If --clear-logs the log files are cleared
    if "--clear-logs" in args:
        for log_file in LOG_FILES:
            try:
                open(log_file, 'w', 0).close()
            except IOError:
                pass
    literal_args = args[:]
    for i in range(0, len(args)):
        if ' ' in args[i]:
            literal_args[i] = '"%s"' % args[i]
    message = "bigmler %s\n" % " ".join(literal_args)

    # Resume calls are not logged
    if not "--resume" in args:
        with open(COMMAND_LOG, "a", 0) as command_log:
            command_log.write(message)
        resume = False

    parser = create_parser(defaults=get_user_defaults(), constants={'NOW': NOW,
                           'MAX_MODELS': MAX_MODELS, 'PLURALITY': PLURALITY})

    # Parses command line arguments.
    command_args = parser.parse_args(args)

    default_output = ('evaluation' if command_args.evaluate
                      else 'predictions.csv')
    if command_args.resume:
        debug = command_args.debug
        command = u.get_log_reversed(COMMAND_LOG,
                                     command_args.stack_level)
        args = shlex.split(command)[1:]
        output_dir = u.get_log_reversed(DIRS_LOG,
                                        command_args.stack_level)
        defaults_file = "%s%s%s" % (output_dir, os.sep, DEFAULTS_FILE)
        parser = create_parser(defaults=get_user_defaults(defaults_file),
                               constants={'NOW': NOW, 'MAX_MODELS': MAX_MODELS,
                                          'PLURALITY': PLURALITY})
        command_args = parser.parse_args(args)
        if command_args.predictions is None:
            command_args.predictions = ("%s%s%s" %
                                        (output_dir, os.sep,
                                         default_output))
        # Logs the issued command and the resumed command
        session_file = "%s%s%s" % (output_dir, os.sep, SESSIONS_LOG)
        u.log_message(message, log_file=session_file)
        message = "\nResuming command:\n%s\n\n" % command
        u.log_message(message, log_file=session_file, console=True)
        try:
            defaults_handler = open(defaults_file, 'r')
            contents = defaults_handler.read()
            message = "\nUsing the following defaults:\n%s\n\n" % contents
            u.log_message(message, log_file=session_file, console=True)
            defaults_handler.close()
        except IOError:
            pass

        resume = True
    else:
        if command_args.predictions is None:
            command_args.predictions = ("%s%s%s" %
                                        (NOW, os.sep,
                                         default_output))
        if len(os.path.dirname(command_args.predictions).strip()) == 0:
            command_args.predictions = ("%s%s%s" %
                                        (NOW, os.sep,
                                         command_args.predictions))
        directory = u.check_dir(command_args.predictions)
        session_file = "%s%s%s" % (directory, os.sep, SESSIONS_LOG)
        u.log_message(message + "\n", log_file=session_file)
        try:
            defaults_file = open(DEFAULTS_FILE, 'r')
            contents = defaults_file.read()
            defaults_file.close()
            defaults_copy = open("%s%s%s" % (directory, os.sep, DEFAULTS_FILE),
                                 'w', 0)
            defaults_copy.write(contents)
            defaults_copy.close()
        except IOError:
            pass
        with open(DIRS_LOG, "a", 0) as directory_log:
            directory_log.write("%s\n" % os.path.abspath(directory))

    if resume and debug:
        command_args.debug = True

    api_command_args = {
        'username': command_args.username,
        'api_key': command_args.api_key,
        'dev_mode': command_args.dev_mode,
        'debug': command_args.debug}

    api = bigml.api.BigML(**api_command_args)

    if (command_args.evaluate
        and not (command_args.training_set or command_args.source
                 or command_args.dataset)
        and not (command_args.test_set and (command_args.model or
                 command_args.models or command_args.model_tag))):
        parser.error("Evaluation wrong syntax.\n"
                     "\nTry for instance:\n\nbigmler --train data/iris.csv"
                     " --evaluate\nbigmler --model "
                     "model/5081d067035d076151000011 --dataset "
                     "dataset/5081d067035d076151003423 --evaluate")

    if command_args.objective_field:
        objective = command_args.objective_field
        try:
            command_args.objective_field = int(objective)
        except ValueError:
            pass

    output_args = {
        "api": api,
        "training_set": command_args.training_set,
        "test_set": command_args.test_set,
        "output": command_args.predictions,
        "objective_field": command_args.objective_field,
        "name": command_args.name,
        "training_set_header": command_args.train_header,
        "test_set_header": command_args.test_header,
        "args": command_args,
        "resume": resume,
    }

    # Reads description if provided.
    if command_args.description:
        description_arg = u.read_description(command_args.description)
        output_args.update(description=description_arg)
    else:
        output_args.update(description="Created using BigMLer")

    # Parses fields if provided.
    if command_args.field_attributes:
        field_attributes_arg = (
            u.read_field_attributes(command_args.field_attributes))
        output_args.update(field_attributes=field_attributes_arg)

    # Parses types if provided.
    if command_args.types:
        types_arg = u.read_types(command_args.types)
        output_args.update(types=types_arg)

    # Parses dataset fields if provided.
    if command_args.dataset_fields:
        dataset_fields_arg = map(lambda x: x.strip(),
                                 command_args.dataset_fields.split(','))
        output_args.update(dataset_fields=dataset_fields_arg)

    # Parses model input fields if provided.
    if command_args.model_fields:
        model_fields_arg = map(lambda x: x.strip(),
                               command_args.model_fields.split(','))
        output_args.update(model_fields=model_fields_arg)

    model_ids = []
    # Parses model/ids if provided.
    if command_args.models:
        model_ids = u.read_models(command_args.models)
        output_args.update(model_ids=model_ids)

    dataset_id = None
    # Parses dataset/id if provided.
    if command_args.datasets:
        dataset_id = u.read_dataset(command_args.datasets)
        command_args.dataset = dataset_id

    # Retrieve model/ids if provided.
    if command_args.model_tag:
        model_ids = (model_ids +
                     u.list_ids(api.list_models,
                                "tags__in=%s" % command_args.model_tag))
        output_args.update(model_ids=model_ids)

    # Reads a json filter if provided.
    if command_args.json_filter:
        json_filter = u.read_json_filter(command_args.json_filter)
        command_args.json_filter = json_filter

    # Reads a lisp filter if provided.
    if command_args.lisp_filter:
        lisp_filter = u.read_lisp_filter(command_args.lisp_filter)
        command_args.lisp_filter = lisp_filter

    # Adds default tags unless that it is requested not to do so.
    if command_args.no_tag:
        command_args.tag.append('BigMLer')
        command_args.tag.append('BigMLer_%s' % NOW)

    # Checks combined votes method
    if (command_args.method and
            not command_args.method in COMBINATION_WEIGHTS.keys()):
        command_args.method = 0
    else:
        combiner_methods = dict([[value, key]
                                for key, value in COMBINER_MAP.items()])
        command_args.method = combiner_methods.get(command_args.method, 0)

    # Reads votes files in the provided directories.
    if command_args.votes_dirs:
        dirs = map(lambda x: x.strip(), command_args.votes_dirs.split(','))
        votes_path = os.path.dirname(command_args.predictions)
        votes_files = u.read_votes_files(dirs, votes_path)
        output_args.update(votes_files=votes_files)

    # Parses fields map if provided.
    if command_args.fields_map:
        fields_map_arg = u.read_fields_map(command_args.fields_map)
        output_args.update(fields_map=fields_map_arg)

    # Parses resources ids if provided.
    if command_args.delete:
        if command_args.predictions is None:
            path = NOW
        else:
            path = u.check_dir(command_args.predictions)
        session_file = "%s%s%s" % (path, os.sep, SESSIONS_LOG)
        message = u.dated("Retrieving objects to delete.\n")
        u.log_message(message, log_file=session_file,
                      console=command_args.verbosity)
        delete_list = []
        if command_args.delete_list:
            delete_list = map(lambda x: x.strip(),
                              command_args.delete_list.split(','))
        if command_args.delete_file:
            if not os.path.exists(command_args.delete_file):
                raise Exception("File %s not found" % command_args.delete_file)
            delete_list.extend([line for line
                                in open(command_args.delete_file, "r")])
        if command_args.all_tag:
            query_string = "tags__in=%s" % command_args.all_tag
            delete_list.extend(u.list_ids(api.list_sources, query_string))
            delete_list.extend(u.list_ids(api.list_datasets, query_string))
            delete_list.extend(u.list_ids(api.list_models, query_string))
            delete_list.extend(u.list_ids(api.list_predictions, query_string))
            delete_list.extend(u.list_ids(api.list_evaluations, query_string))
        # Retrieve sources/ids if provided
        if command_args.source_tag:
            query_string = "tags__in=%s" % command_args.source_tag
            delete_list.extend(u.list_ids(api.list_sources, query_string))
        # Retrieve datasets/ids if provided
        if command_args.dataset_tag:
            query_string = "tags__in=%s" % command_args.dataset_tag
            delete_list.extend(u.list_ids(api.list_datasets, query_string))
        # Retrieve model/ids if provided
        if command_args.model_tag:
            query_string = "tags__in=%s" % command_args.model_tag
            delete_list.extend(u.list_ids(api.list_models, query_string))
        # Retrieve prediction/ids if provided
        if command_args.prediction_tag:
            query_string = "tags__in=%s" % command_args.prediction_tag
            delete_list.extend(u.list_ids(api.list_predictions, query_string))
        # Retrieve evaluation/ids if provided
        if command_args.evaluation_tag:
            query_string = "tags__in=%s" % command_args.evaluation_tag
            delete_list.extend(u.list_ids(api.list_evaluations, query_string))
        message = u.dated("Deleting objects.\n")
        u.log_message(message, log_file=session_file,
                      console=command_args.verbosity)
        message = "\n".join(delete_list)
        u.log_message(message, log_file=session_file)
        u.delete(api, delete_list)
        if sys.platform == "win32" and sys.stdout.isatty():
            message = (u"\nGenerated files:\n\n" +
                       unicode(u.print_tree(path, " "), "utf-8") + u"\n")
        else:
            message = "\nGenerated files:\n\n" + u.print_tree(path, " ") + "\n"
        u.log_message(message, log_file=session_file,
                      console=command_args.verbosity)
    elif (command_args.training_set or command_args.test_set
          or command_args.source or command_args.dataset
          or command_args.datasets or command_args.votes_dirs):
        compute_output(**output_args)
    u.log_message("_" * 80 + "\n", log_file=session_file)

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


import requests
import json
import testeapi

def acessarapi(n_pag):  #apenas acesa a api e retorna o resultado
    numpag = {'page_number':n_pag}
    conn = requests.request("GET", "https://api.zoom.us/v2/accounts", headers=testeapi.headers, params=numpag).json()
    contas = conn['accounts']

    for i in contas:
        print[i, '\n']

    ###testeapi.lista_subcontas.append()
    print(len(contas))
    return(conn)