from django.core.management.base import BaseCommand
from django.conf import settings

import requests
import json
import telebot
from telebot import types

from crypton.models import Profile
from preferences import preferences

bot = telebot.TeleBot(settings.TOKEN)

coins = {
    'BTC': ['btc', 'bitcoin', 'биткоин'],
    'ETH': ['eth', 'ethereum', 'эфириум'],
    'DOGE': ['doge', 'dogecoin', 'догикоин']
}

class Command(BaseCommand):
    help = 'Telegram Bot'

    def handle(self, *args, **options):
        bot.polling(none_stop=True)

def exchange(crypto):
    url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'

    headers = {
        "X-CMC_PRO_API_KEY": settings.COINMARKETCAP_API_KEY,
        "Accept": "application/json"
    }

    parameters = {
        'symbol': crypto
    }

    session = requests.Session()
    session.headers.update(headers)
    data = session.get(url, params=parameters)

    results = (json.loads(data.text)).get('data')
    return results[f'{crypto}']['quote']['USD']['price']

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.send_message(message.chat.id, preferences.BotPreferences.welcome, reply_markup=choice_crypto())

    chat_id = message.chat.id

    Profile.objects.get_or_create(
                tg_id=chat_id,
                defaults={
                    'tg_username': message.from_user.username,
                    'tg_firstname': message.from_user.first_name,
                    'tg_lastname': message.from_user.last_name,
                }
    )

@bot.message_handler(content_types=["text"])
def send_anytext(message):
    text = message.text.lower()
    chat_id = message.chat.id
    for key, val in coins.items():
        if text in val:
            bot.send_message(chat_id, exchange(key), reply_markup=choice_crypto())
            break
    else:
        bot.send_message(chat_id, preferences.BotPreferences.error_message, reply_markup=choice_crypto())

def choice_crypto():
    markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
    btc = types.KeyboardButton(preferences.BotPreferences.btc)
    eth = types.KeyboardButton(preferences.BotPreferences.eth)
    doge = types.KeyboardButton(preferences.BotPreferences.doge)
    markup.add(btc, eth, doge)
    return markup


import warnings, logging, os, sys
warnings.filterwarnings('ignore',category=FutureWarning)
logging.disable(logging.WARNING)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"

import json
import tensorflow as tf
from utils import *
from arguments import get_args_many

args = get_args_many()

MDIR         = args.MDIR

n2d_layers   = 61
n2d_filters  = 64
window2d     = 3
wmin         = 0.8
ns           = 21

# load network weights in RAM
w,b,beta_,gamma_ = load_weights(args.MDIR)

#
# network
#
config = tf.ConfigProto(
    gpu_options = tf.GPUOptions(allow_growth=True)
)

activation = tf.nn.elu
conv1d = tf.layers.conv1d
conv2d = tf.layers.conv2d
with tf.Graph().as_default():

    with tf.name_scope('input'):
        ncol = tf.placeholder(dtype=tf.int32, shape=())
        nrow = tf.placeholder(dtype=tf.int32, shape=())
        msa = tf.placeholder(dtype=tf.uint8, shape=(None,None))

    #
    # collect features
    #
    msa1hot  = tf.one_hot(msa, ns, dtype=tf.float32)
    weights = reweight(msa1hot, wmin)

    # 1D features
    f1d_seq = msa1hot[0,:,:20]
    f1d_pssm = msa2pssm(msa1hot, weights)

    f1d = tf.concat(values=[f1d_seq, f1d_pssm], axis=1)
    f1d = tf.expand_dims(f1d, axis=0)
    f1d = tf.reshape(f1d, [1,ncol,42])

    # 2D features
    f2d_dca = tf.cond(nrow>1, lambda: fast_dca(msa1hot, weights), lambda: tf.zeros([ncol,ncol,442], tf.float32))
    f2d_dca = tf.expand_dims(f2d_dca, axis=0)

    f2d = tf.concat([tf.tile(f1d[:,:,None,:], [1,1,ncol,1]),
                    tf.tile(f1d[:,None,:,:], [1,ncol,1,1]),
                    f2d_dca], axis=-1)
    f2d = tf.reshape(f2d, [1,ncol,ncol,442+2*42])

    #
    # 2D network
    #

    # store ensemble of networks in separate branches
    layers2d = [[] for _ in range(len(w))]
    preds = [[] for _ in range(4)]

    Activation   = tf.nn.elu

    for i in range(len(w)):

        layers2d[i].append(Conv2d(f2d,w[i][0],b[i][0]))
        layers2d[i].append(InstanceNorm(layers2d[i][-1],beta_[i][0],gamma_[i][0]))
        layers2d[i].append(Activation(layers2d[i][-1]))

        # resnet
        idx = 1
        dilation = 1
        for _ in range(n2d_layers):
            layers2d[i].append(Conv2d(layers2d[i][-1],w[i][idx],b[i][idx],dilation))
            layers2d[i].append(InstanceNorm(layers2d[i][-1],beta_[i][idx],gamma_[i][idx]))
            layers2d[i].append(Activation(layers2d[i][-1]))
            idx += 1
            layers2d[i].append(Conv2d(layers2d[i][-1],w[i][idx],b[i][idx],dilation))
            layers2d[i].append(InstanceNorm(layers2d[i][-1],beta_[i][idx],gamma_[i][idx]))
            layers2d[i].append(Activation(layers2d[i][-1] + layers2d[i][-6]))
            idx += 1
            dilation *= 2
            if dilation > 16:
                dilation = 1

        # probabilities for theta and phi
        preds[0].append(tf.nn.softmax(Conv2d(layers2d[i][-1],w[i][123],b[i][123]))[0])
        preds[1].append(tf.nn.softmax(Conv2d(layers2d[i][-1],w[i][124],b[i][124]))[0])

        # symmetrize
        layers2d[i].append(0.5*(layers2d[i][-1]+tf.transpose(layers2d[i][-1],perm=[0,2,1,3])))

        # probabilities for dist and omega
        preds[2].append(tf.nn.softmax(Conv2d(layers2d[i][-1],w[i][125],b[i][125]))[0])
        preds[3].append(tf.nn.softmax(Conv2d(layers2d[i][-1],w[i][127],b[i][127]))[0])
        #preds[4].append(tf.nn.softmax(Conv2d(layers2d[i][-1],w[i][126],b[i][126]))[0])

    # average over all branches
    prob_theta = tf.reduce_mean(tf.stack(preds[0]),axis=0)
    prob_phi   = tf.reduce_mean(tf.stack(preds[1]),axis=0)
    prob_dist  = tf.reduce_mean(tf.stack(preds[2]),axis=0)
    prob_omega = tf.reduce_mean(tf.stack(preds[3]),axis=0)

    with tf.Session(config=config) as sess:

        # loop over all A3M files in the imput folder
        for filename in os.listdir(args.ALNDIR):
            if not filename.endswith(".a3m"):
                continue

            # parse & predict
            a3m = parse_a3m(args.ALNDIR + '/' + filename)
            print("processing:", filename)

            pd, pt, pp, po = sess.run([prob_dist, prob_theta, prob_phi, prob_omega],
                                    feed_dict = {msa : a3m, ncol : a3m.shape[1], nrow : a3m.shape[0] })

	    # save distograms & anglegrams
            npz_file = args.NPZDIR + '/' + filename[:-3] + 'npz'
            np.savez_compressed(npz_file, dist=pd, omega=po, theta=pt, phi=pp)


import sqlite3
import DepartmentStudentAmounts as dsa

Database = sqlite3.connect('Identities')
items = Database.cursor()

"""class used to validate the adding none year group specific items

Attributes
-----------

private Attributes

---> name item
---> average
---> department

"""

class validate_adding_item_none_yeargroup_specfic:
    name_item_length_max = 25
    name_item_length_mini = 3
    average_upper_bound = 6
    average_lower_bound = 1
    total_students = 0
    DefaultStockAlertValue = 0

    def __init__(self, name_item, average, department):
        self.name_item = name_item
        self.average = average
        self.department = department

    # Method is used to check the user inputs

    def check(self):
        if validate_adding_item_none_yeargroup_specfic.name_item_length_mini < len(self.name_item) <= validate_adding_item_none_yeargroup_specfic.name_item_length_max and validate_adding_item_none_yeargroup_specfic.average_lower_bound <= self.average < validate_adding_item_none_yeargroup_specfic.average_upper_bound:
            return True
        else:
            return False

    # Method is used to perform claculation about the amount that should be inputted into the database

    def calculations(self):
        # Loops through the dictonary calculating the total amount of students
        for i in dsa.StudentAmounts:
            value = dsa.StudentAmounts[i][self.department]
            # Adds the value to the total students class variable
            self.total_students += value
        return self.total_students

    # Method is used to perform the final calcultaion abou the amount that would be inputted to the database

    def final_calc(self):
        TotalStudents = self.calculations()
        CurrentAmount = TotalStudents * self.average
        MinimumAmount = round((CurrentAmount * 0.1), 0)
        return CurrentAmount, MinimumAmount

    # Method is used to check if the item already exsists in the database

    def checkItem_exsist(self):
        previous = self.check()
        if previous:
            items.execute("SELECT * FROM None_yeargroup_specific WHERE Name_item=?", (self.name_item,))
            item = items.fetchone()
            Database.commit()
            if item is not None:
                return True
            else:
                return False
        else:
            return True

    # Method is used to input the item to the database

    def inputItem(self):
        previous = self.checkItem_exsist()
        if previous is False:
            CalculationResults = self.final_calc()
            CurrentAmount = CalculationResults[0]
            MinimumAmount = CalculationResults[1]
            items.execute("INSERT INTO None_yeargroup_specific VALUES (?, ?, ?, ?, ?)", (self.name_item, CurrentAmount, MinimumAmount, self.department, validate_adding_item_none_yeargroup_specfic.DefaultStockAlertValue))
            result = Database.commit()
            if result is not None:
                return False
            else:
                return True
        else:
            return False

"""Subclass used to validate entries

Subclass ( valdidate_adding_yeargroup_specfic ) inherites from Superclass ( validate_adding_item_none_yeargroup_specfic )

      subclass                                              superclass
validate_adding_item_none_yeargroup_specfic  ----->  valdidate_adding_yeargroup_specfic

Attributes
-----------
---> year group
+ the inherted attributes

"""

class valdidate_adding_yeargroup_specfic(validate_adding_item_none_yeargroup_specfic):
    def __init__(self, name_item, average, department, yeargroup):
        super().__init__(name_item, average, department)
        self.yeargroup = yeargroup

    # Polyphorism may be able to be used on this method by overriding it.

    def calculation(self):
        amount_students = dsa.StudentAmounts["Year " + self.yeargroup][self.department]
        result = self.average * amount_students
        Minimum_amount = round(result * 0.3)
        return result, Minimum_amount

    # Polyphorism used on this method by overriding it.

    def checkItem_exsist(self):
        previous = self.check()
        print(previous)
        if previous:
            items.execute("SELECT * FROM year_group_specific WHERE Name_item=? AND year_group=?", (self.name_item, self.yeargroup))
            result = Database.commit()
            if result is not None:
                return True
            else:
                return False
        else:
            return False
        # The method check would be called first if True connect to year group specific database and determine if the item exsist or not.

    # Polyphorism is used here to input the item. (Overidding)
    def inputItem(self):
        previous = self.checkItem_exsist()
        if previous is False:
            # return tuple (result, minmum amount)
            result = self.calculation()
            MinimumAmount = result[1]
            CurrentAmount = result[0]
            items.execute("INSERT INTO year_group_specific VALUES (?, ?, ?, ?, ?, ?)", (self.name_item, CurrentAmount, MinimumAmount, self.department, self.yeargroup, validate_adding_item_none_yeargroup_specfic.DefaultStockAlertValue, ))
            result = Database.commit()
            if result is not None:
                return False
            else:
                return True
        else:
            return False


"""[Recommend] iPhone 13 Again"""
def main():
    """iPhone 13 Again"""
    model = input()
    capacity = input()
    result = ""
    case1 = capacity == '128 GB' or capacity == '256 GB' or capacity == '512 GB'
    case2 = capacity == '128 GB' or capacity == '256 GB' or \
        capacity == '512 GB' or capacity == '1 TB'
    if (model == "iPhone 13 mini" or model == "iPhone 13") and case1:
        capacity = int(capacity[0:3])
        capacity = capacity // 128
        if model == "iPhone 13":
            result += str(29900 + 4000*(capacity-1))
        else:
            result += str(25900 + 4000*(capacity-1))
    elif (model == "iPhone 13 Pro" or model == "iPhone 13 Pro Max") and case2:
        if capacity == "1 TB":
            capacity = 6
        else:
            capacity = int(capacity[0:3])//128
        if model == "iPhone 13 Pro":
            result += str(38900 + 4000*(capacity-1))
        else:
            result += str(42900 + 4000*(capacity-1))
    else:
        result += "Not Available"
    print(result)
main()


import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

service_obj = Service("C:/Users/PUJA/chromedriver")
driver = webdriver.Chrome(service=service_obj)
driver.implicitly_wait(5)
expectedlist=['Cucumber - 1 Kg', 'Raspberry - 1/4 Kg', 'Strawberry - 1/4 Kg']
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
driver.find_element(By.CSS_SELECTOR,".search-keyword").send_keys("ber")
time.sleep(2)
#product_list:
actuallist=[]
productlist= driver.find_elements(By.XPATH,"//div/h4")
for product in productlist:
    actuallist.append(product.text)
print(actuallist)

assert actuallist == expectedlist

count = driver.find_elements(By.XPATH,"//div[@class='products']/div")
print(len(count))
assert len(count)>0
#Chaining of web element
for c in count:
    c.find_element(By.XPATH,"div/button").click()

driver.find_element(By.XPATH,"//img[@alt='Cart']").click()
driver.find_element(By.XPATH,"//button[text()='PROCEED TO CHECKOUT']").click()
#sum _validation
sum=0
rates= driver.find_elements(By.XPATH,"//tr/td[5]/p")
for rate in rates:
     sum= sum+int(rate.text)
print(sum)
totalamount= int(driver.find_element(By.CSS_SELECTOR,".totAmt").text)
assert sum == totalamount

driver.find_element(By.CSS_SELECTOR,".promoCode").send_keys("rahulshettyacademy")
driver.find_element(By.CLASS_NAME,"promoBtn").click()
wait= WebDriverWait(driver,5)
wait.until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,".promoInfo")))
print(driver.find_element(By.CLASS_NAME,"promoInfo").text)

discountedamount= float(driver.find_element(By.CLASS_NAME,"discountAmt").text)
print(discountedamount)
assert totalamount>discountedamount

driver.find_element(By.XPATH,"//button[text()='Place Order']").click()
dropdown= Select(driver.find_element(By.XPATH,"//div/select"))
dropdown.select_by_value("India")

driver.find_element(By.CSS_SELECTOR,".chkAgree").click()
driver.find_element(By.XPATH,"//button[text()='Proceed']").click()


import pandas as pd
import pymongo
from .metacritic_scrape import scrape_game_page
from .pandas_functions import clean_df
from selenium.webdriver import Firefox
import random
import time

mc = pymongo.MongoClient()
db =mc['game_recommender']
reviews_coll = db['reviews']
games=db['games']

#omc=pymongo.MongoClient()
#cb = omc['ps4_game_data']
#games = cb['games']

def flatten_game_dict(game_dict):
    """Take a dictionary of dictionaries & flatten it"""
    for (game_id, user_score_dict) in game_dict.items():
        for (user_id, score) in user_score_dict.items():
            yield {'game_id': game_id, 'user_id': user_id, 'score': score}

def store_all_users(coll=games):
    """Take raw_html from a game's user review page, and store the game, username, & score
    as an entry in reviews collection"""
    games_dict={}

    df = pd.DataFrame(list(coll.find()))
    #df =clean_df(df=df)

    game_titles = list(df.title)
    browser=Firefox()
    for game in game_titles:
        result= scrape_game_page(title=game, browser=browser)
        if not result:
            continue
        games_dict[game] = result

    existing_game_reviews=set((r['game_id'], r['user_id'])
                                for r in reviews_coll.find())

    flattened=flatten_game_dict(game_dict=games_dict)
    for review in flattened:
        game_id = review['game_id']
        user_id = review['user_id']
        if (game_id, user_id) not in existing_game_reviews:
            reviews_coll.insert_one(review)

def make_preference_df(db=reviews_coll):
    """Go from all entries in reviews collection, to pandas dataframe"""
    df=pd.DataFrame(list(db.find()))

    """Set of unique user & game IDs"""
    users=set(df['user_id'])
    games=set(df['game_id'])

    """Zipping a number to each unique user & game ID"""
    game_id_lookup = dict(zip(games, range(len(games))))
    user_id_lookup = dict(zip(users, range(len(users))))

    df['game_number']=df['game_id'].apply(game_id_lookup.get)
    df['user_number']=df['user_id'].apply(user_id_lookup.get)

    #df=df.pivot(index='user_number', columns='game_number', values='score' )
    return df


from parlai.utils.safety import OffensiveLanguageClassifier
from parlai.utils.safety import OffensiveStringMatcher
from parlai.utils.testing import skipUnlessBPE, skipUnlessGPU

import unittest

DEFINITELY_GOOD = [
    'hey how are you',
    'what is your name?',
    'i like to play sports',
    'i have a horse',
]
PROBABLY_BAD = ['i hate you', 'you are pretty ugly', 'parlai is the worst platform']
DEFINITELY_BAD = ['fuck you', 'you are a piece of shit']

class TestSafetyModules(unittest.TestCase):
    def test_string_matcher(self):
        sm = OffensiveStringMatcher()
        for phrase in DEFINITELY_BAD:
            assert phrase in sm, f'`{phrase}` is offensive'
        for phrase in DEFINITELY_GOOD:
            assert phrase not in sm, f'`{phrase}` is not offensive'

    @skipUnlessGPU
    @skipUnlessBPE
    def test_classifier(self):
        lc = OffensiveLanguageClassifier()
        for phrase in DEFINITELY_BAD:
            assert phrase in lc, f'`{phrase}` is offensive'
        for phrase in PROBABLY_BAD:
            assert phrase in lc, f'`{phrase}` is offensive'
        for phrase in DEFINITELY_GOOD:
            assert phrase not in lc, f'`{phrase}` is not offensive'

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


#!/usr/local/bin/py
import argparse
import hashlib
import logging
import os
import oss2  # pip install oss2
import re

logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger('oss2').setLevel(logging.WARNING)
logging.basicConfig(
    level=logging.INFO,
    format='[%(asctime)s][%(levelname)s] %(message)s',
    # filename='/tmp/oss-sync.log'
)

_CACHE = {}
ROOT_API_KEY = os.path.join(os.getenv('HOME'), '.aliyun')

# Doc: https://help.aliyun.com/knowledge_detail/5974206.htm
# 青岛节点外网地址： oss-cn-qingdao.aliyuncs.com
# 青岛节点内网地址： oss-cn-qingdao-internal.aliyuncs.com
#
# 北京节点外网地址：oss-cn-beijing.aliyuncs.com
# 北京节点内网地址：oss-cn-beijing-internal.aliyuncs.com
#
# 杭州节点外网地址： oss-cn-hangzhou.aliyuncs.com
# 杭州节点内网地址： oss-cn-hangzhou-internal.aliyuncs.com
#
# 上海节点外网地址： oss-cn-shanghai.aliyuncs.com
# 上海节点内网地址： oss-cn-shanghai-internal.aliyuncs.com
#
# 香港节点外网地址： oss-cn-hongkong.aliyuncs.com
# 香港节点内网地址： oss-cn-hongkong-internal.aliyuncs.com
#
# 深圳节点外网地址： oss-cn-shenzhen.aliyuncs.com
# 深圳节点内网地址： oss-cn-shenzhen-internal.aliyuncs.com
#
# 美国节点外网地址： oss-us-west-1.aliyuncs.com
# 美国节点内网地址：  oss-us-west-1-internal.aliyuncs.com
#
# 新加坡节点外网地址： oss-ap-southeast-1.aliyuncs.com
# 新加坡节点内网地址：  oss-ap-southeast-1-internal.aliyuncs.com
#
# 原地址oss.aliyuncs.com 默认指向杭州节点外网地址。
# 原内网地址oss-internal.aliyuncs.com 默认指向杭州节点内网地址
API_URL = 'oss-cn-hangzhou.aliyuncs.com'

IGNORE_FILES = (
    '\/\..*$',
    '\.pyc$',
)

def is_in_ignore_files(file_path):
    for p in IGNORE_FILES:
        if re.search(p, file_path):
            return True
    return False

def get_file_md5(file_path):
    hasher = hashlib.md5()
    with open(file_path, 'rb') as f:
        buf = f.read(65536)
        while len(buf) > 0:
            hasher.update(buf)
            buf = f.read(65536)
    return hasher.hexdigest()

def sizeof_fmt(num):
    if num <= 1024:
        return '1 KB'
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)
        num /= 1024.0

def get_bucket(args):
    if 'bucket' in _CACHE:
        return _CACHE['bucket']

    api_key = open(os.path.join(ROOT_API_KEY, 'apikey')).read().strip()
    api_secret = open(os.path.join(ROOT_API_KEY, 'secretkey')).read().strip()
    auth = oss2.Auth(api_key, api_secret)
    bucket = oss2.Bucket(auth, API_URL, args.bucket)
    _CACHE['bucket'] = bucket
    return bucket

def get_local_objects(target_path):
    objects = {}
    oss_dir = os.path.dirname(__file__)
    if target_path:
        oss_dir = os.path.join(oss_dir, target_path)
    else:
        oss_dir = os.path.join(oss_dir, '.')
    if not os.path.exists(oss_dir):
        return objects

    file_count = 0
    if os.path.isdir(oss_dir):
        for root, dirs, files in os.walk(oss_dir):
            for f in files:
                root = re.sub(r'^\./?', '', root)
                local_path = os.path.join(root, f)
                if is_in_ignore_files(local_path):
                    logging.info('ignored file: {}'.format(local_path))
                    continue
                md5 = get_file_md5(local_path)
                objects[local_path] = md5.upper()
                file_count += 1
    else:
        md5 = get_file_md5(oss_dir)
        local_path = re.sub(r'^\./', '', target_path)
        objects[local_path] = md5.upper()
        file_count += 1
    logging.info('local files: {}'.format(file_count))
    return objects

def get_remote_objects(args):
    objects = {'files': {}, 'etags': {}, 'meta': {}}
    bucket = get_bucket(args)
    marker = None
    file_count = 0
    prefix = re.sub(r'^\./?', '', args.target_path or '')
    while True:
        result = bucket.list_objects(prefix=prefix, max_keys=100, marker=marker)
        for obj in result.object_list:
            if obj.key.endswith('/'):
                continue
            if args.min_size and obj.size < args.min_size:
                continue
            if args.max_size and obj.size > args.max_size:
                continue
            if args.re and not re.search(args.re, obj.key):
                continue
            objects['files'][obj.key] = obj.etag
            objects['etags'][obj.etag] = obj.key
            objects['meta'][obj.key] = obj
            file_count += 1
        marker = result.next_marker
        if not result.is_truncated:
            break
    logging.info('remote files: {}'.format(file_count))
    return objects

def upload_file(local_path, args):
    bucket = get_bucket(args)
    key = re.sub(r'^\./?', '', local_path)
    res = bucket.put_object_from_file(key, local_path)
    if res.status != 200:
        logging.error('Upload {} failed. Exit.'.format(local_path))
        exit(1)

def upload_files_to_oss(args):
    target_path = re.sub(r'^\./?', '', args.target_path)
    logging.info('Uploading/Updating for: {}'.format(target_path))
    los = get_local_objects(target_path)
    if args.check_duplicated:
        ros = get_remote_objects(args)
    else:
        ros = get_remote_objects(args)

    files_need_to_update = []
    files_need_to_upload = []

    for local_path in los.keys():
        md5 = los[local_path]
        if md5 in ros['etags']:
            logging.info('* Identical file found:')
            logging.info('* @ {}'.format(ros['etags'][md5]))
            continue

        if local_path not in ros['files']:
            size = sizeof_fmt(os.path.getsize(local_path))
            files_need_to_upload.append((local_path, size))
        elif ros['files'][local_path] != md5:
            size = sizeof_fmt(os.path.getsize(local_path))
            files_need_to_update.append((local_path, size))

    files_need_to_update.sort()
    files_need_to_upload.sort()

    index = 1
    count = len(files_need_to_update)
    for local_path, size in files_need_to_update:
        if args.no:
            break
        elif args.yes:
            upload_file(local_path, args)
            index += 1
        else:
            print('Q: Do you want to update {}:'.format(local_path))
            response = input()
            while response.lower().strip() not in ('yes', 'no'):
                print('Q: Do you want to update {}:'.format(local_path))
                response = input()
            if response == 'no':
                logging.info('skipped {} by user'.format(local_path))
                continue
            logging.info('= [{}/{}] Updating old file: {} ({})'.format(
                index, count, local_path, size))
            upload_file(local_path, args)
            index += 1

    index = 1
    count = len(files_need_to_upload)
    for local_path, size in files_need_to_upload:
        try:
            logging.info('+ [{}/{}] Uploading new file: {} ({})'.format(
                index, count, local_path, size))
        except:
            pass
        upload_file(local_path, args)
        index += 1

    logging.info('Uploading/Updating Done\n')

def _get_dir_of_file(f):
    return '/'.join(f.split('/')[:-1])

def download_file(oss_path, local_path, args):
    dir_ = _get_dir_of_file(local_path)
    if not os.path.exists(dir_):
        os.makedirs(dir_)
    logging.info('+ Downloading {}'.format(oss_path))
    bucket = get_bucket(args)
    local_path = local_path.encode('utf-8')
    res = bucket.get_object_to_file(oss_path, local_path)
    if res.status != 200:
        logging.error('Download {} failed. Exit.'.format(oss_path))
        exit(1)

def list_files_on_oss(args):
    files = get_remote_objects(args)
    size_total = 0
    for o in files['meta']:
        size_total += files['meta'][o].size
        if args.verbose:
            print('\n- file: {}'.format(o))
            print('- size: {}'.format(sizeof_fmt(files['meta'][o].size)))
            print('- md5: {}'.format(files['meta'][o].etag))

    if not args.verbose:
        keys_to_list = list(files['files'].keys())
        keys_to_list.sort()
        print('== First 3 files:')
        for x in keys_to_list[:3]:
            print('   - {}'.format(x))
        print('== Last 3 files:')
        for x in keys_to_list[-3:]:
            print('   - {}'.format(x))

    print('\n== Total file count: {}'.format(len(files['files'])))
    print('== Total size: {}'.format(sizeof_fmt(size_total)))

def delete_files_from_oss(args):
    files = get_remote_objects(args)
    keys_to_delete = list(files['files'].keys())
    keys_to_delete.sort()
    print('== Will delete {} files:'.format(len(keys_to_delete)))
    print('== First 3 files:')
    for x in keys_to_delete[:3]:
        print('   - {}'.format(x))
    print('== Last 3 files:')
    for x in keys_to_delete[-3:]:
        print('   - {}'.format(x))

    answer = input('== Please enter YES to delete them ALL: ')
    if answer.strip() != 'YES':
        print('\nAction Canceled. Files are safe. Bye.')
        return

    bucket = get_bucket(args)
    count = 0
    for x in keys_to_delete:
        bucket.delete_object(x)
        count += 1
        print('- deleted: {}'.format(x))
    print('\nDeleted {} files.'.format(count))

def download_files_from_oss(args):
    target_path = args.target_path
    if target_path.startswith('./'):
        target_path = target_path[2:]
    if target_path.startswith('/'):
        raise ValueError('Must use relative path')

    oss_dir = os.path.dirname(__file__)
    oss_dir = os.path.join(oss_dir, '.')
    logging.info('Downloading file from: {}'.format(target_path))
    los = get_local_objects(target_path)
    ros = get_remote_objects(args)
    target_files = []
    for obj_key in ros['files']:
        if obj_key in los and ros['files'][obj_key] == los[obj_key]:
            logging.info('= {} exists'.format(obj_key))
            continue
        target_files.append(obj_key)

    target_files.sort()
    for oss_path in target_files:
        local_path = os.path.join(oss_dir, oss_path)
        download_file(oss_path, local_path, args)
    logging.info('Downloading Done\n')

def main():
    parser = argparse.ArgumentParser(description='Use Aliyun-OSS as Dropbox')
    parser.add_argument(
        '--target-path',
        '-p',
        action='store',
        const=None,
        default=None,
        help='Target path to sync/delete files'
    )
    parser.add_argument(
        '--download',
        '-d',
        action='store_true',
        default=False,
        help='Download files from OSS'
    )
    parser.add_argument(
        '--yes',
        action='store_true',
        default=False,
        help='overwrite existing files'
    )
    parser.add_argument(
        '--no',
        action='store_true',
        default=False,
        help='Do NOT overwrite existing files'
    )
    parser.add_argument(
        '--upload',
        '-u',
        action='store_true',
        default=False,
        help='Upload files to OSS'
    )
    parser.add_argument(
        '--listing',
        '-L',
        action='store_true',
        default=False,
        help='List files meta info on OSS'
    )
    parser.add_argument(
        '--min-size',
        type=int,
        default=0,
        help='[Listing] do not list size smaller than this'
    )
    parser.add_argument(
        '--max-size',
        type=int,
        default=0,
        help='[Listing] do not list size bigger than this'
    )
    parser.add_argument(
        '--re',
        type=str,
        default='',
        help='[Listing] filter file name by RE string'
    )
    parser.add_argument(
        '--check-duplicated',
        '-c',
        action='store_false',
        default=True,
        help='Do not upload files already in bucket other dirs'
    )
    parser.add_argument(
        '--bucket',
        '-b',
        required=True,
        help='bucket name to store data',
    )
    parser.add_argument(
        '--delete',
        action='store_true',
        help='To delete files with prefix from OSS',
    )
    parser.add_argument(
        '--verbose',
        '-v',
        action='store_true',
        help='Print more info',
    )

    args = parser.parse_args()
    if args.listing:
        list_files_on_oss(args)
    elif args.download:
        download_files_from_oss(args)
    elif args.delete:
        delete_files_from_oss(args)
    else:
        upload_files_to_oss(args)

if __name__ == "__main__":
    main()


import pygame, time, random
from cars import car
from things import thing
from shooting_thing import shoot

pygame.init()

display_width = 800
display_height = 600

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

random_color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))

gameDisplay = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption('fun_run_car')

clock = pygame.time.Clock()

car_img = pygame.image.load('car.png')
dack = pygame.image.load('dack.png')

def overall_music():#open music
    global pause
    pygame.mixer.music.load('Raining Bits.ogg')
    pygame.mixer.music.play(-1)

def stop_music():
    pygame.mixer.music.stop()

def things_dodged(count): #counter for dodges
    font = pygame.font.SysFont(None, 30)
    text = font.render("Dodge: " + str(count), True, black)
    gameDisplay.blit(text, (0, 0))

#massages
def text_object(text, font):
    TextSurface = font.render(text, True, red)
    return TextSurface, TextSurface.get_rect()

def message_display(text, game_type):
    LargeText = pygame.font.Font('freesansbold.ttf', 120)
    TextSurf, TextRect = text_object(text, LargeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(3)
    stop_music()
    #pygame.mixer.music.stop()
    game_loop(game_type)

def crash_sound():
    pygame.mixer.music.load('Aargh7.ogg')
    pygame.mixer.music.play(1)

def crash(game_type):#when the car crashes
    stop_music()
    crash_sound()
    message_display('you Crashed', game_type)

def game_start():
    message_display('time to play')

def button(msg,x,y,w,h,ic,ac,action=None):#buttons
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:#checking if the mouse press any button
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))

        if click[0] == 1 and action != None:
            if msg == 'quit':
                action()
            else:
                action(msg)
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    smallText = pygame.font.SysFont("comicsansms", 20)
    textSurf, textRect = text_object(msg, smallText)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    gameDisplay.blit(textSurf, textRect)

def quit_game():
    pygame.quit()
    quit()

def game_intro():#game intro screen
    pygame.mixer.music.load('intro_music.wav')#music
    pygame.mixer.music.play(-1)
    intro =True
    while intro:
        events = pygame.event.get()
        for event in events:  # event per frame per sec
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.blit(dack, (0, 0))
        LargeText = pygame.font.Font('freesansbold.ttf', 120)
        TextSurf, TextRect = text_object("fun run car", LargeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        #add difficulty
        button("normal", display_width * 0.1875, display_height * 0.85, display_width*0.125, display_height*0.085, green, white,
               game_loop)  # first and sec x,y sec rectangle boundaries
        button("shooting", display_width * 0.1875, display_height * 0.65, display_width*0.125,  display_height*0.085, green, white, game_loop)
        button("quit", display_width * 0.6875, display_height * 0.75, display_width*0.125,  display_height*0.085, blue, white, quit_game)
        #button("register", display_width * 0.4, display_height * 0.85, display_width * 0.125, display_height * 0.085,
         #      black, white, reg_log)  # first and sec x,y sec rectangle boundaries
        #calls the buttons function

        pygame.display.update()
        clock.tick(15)

def destroy_thing(things_list, shot):#when obstacles get destroy
    for thingss in things_list:
        if shot.y < thingss.thing_starty + thingss.thing_height and shot.y > thingss.thing_starty or \
                                        shot.y + shot.height < thingss.thing_starty + thingss.thing_height and shot.y + shot.height > thingss.thing_starty:  # checking his back of the car
            # print ('y crossover')
            if shot.x > thingss.thing_startx and shot.x < thingss.thing_startx + thingss.thing_width or \
                                            shot.x + shot.width > thingss.thing_startx and shot.x + shot.width < thingss.thing_startx + thingss.thing_width:

                return thingss
    return 0

def reset_things(things,list_thing, dodged):#reset obstacles
    things.change_thing_starty_reset()
    things.change_thing_startx(display_width)
    things.change_thing_speed()
    things.change_thing_width()
    new_color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
    things.change_thing_color(new_color)
    if dodged % 7 == 0:
        list_thing.append(thing(random.randrange(0, display_width), -600, 4, 20, 100, random_color, 1, 1.2))

#normal gamee loop
def game_loop(game_type):#main game
    overall_music()
    shoot.shooting_counter = 0

    shoot_speed = -5.0
    #__init__(self, car_width, car_height, x, y, car_img)
    list_cars = [] #cars list of objects
    list_cars.append(car(33, 55, (display_width * 0.45), (display_height * 0.8), car_img))#x,y,car_width,car_height,car_img creating car object
    x_change = 0
    y_change = 0
    #__init__(self,thing_startx, thing_starty, thing_speed, thing_width, thing_height, color, thing_increase_speed, thing_increase_width):
    list_thing = [] #obstacles list of objects
    list_thing.append(thing(random.randrange(0, display_width), -600, 4, 20, 100, random_color, 0.1, 0.5))#x_start,y_start,thing_speed, thing_width,thing height, color, speed_after dodge, width_increase_after_dodge

    shooting_list = []
    y_dack = 0

    dodged = 0

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():  # event per frame per sec, checking every event that occur in game
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            # moving x
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5.0
                elif event.key == pygame.K_RIGHT:
                    x_change = +5.0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0.0

            # moving y
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5.0
                elif event.key == pygame.K_DOWN:
                    y_change = +5.0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0.0

            #shooting
            if game_type == 'shooting':
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        #def __init__(self, x, y, speed, width, height, color):
                        for cars in list_cars:
                            shooting_list.append(shoot(cars.x,cars.y, red))

        # event endler
        for cars in list_cars:
            cars.move_car(x_change,y_change)#moving car

        gameDisplay.blit(dack, (0, 0))  # background highway drawing

        things_dodged(dodged)

        # gameDisplay.fill(black)
        for things in list_thing: #draw obstacles
            things.draw_thing(gameDisplay)
            things.change_thing_starty_speed()#change the y with speed

        for cars in list_cars:
            cars.draw_car(gameDisplay)  # drawing the car

        #
            if cars.x > display_width -cars.car_width or cars.x < 0:#checking ends of screen
                crash(game_type)

            if cars.y > display_height - cars.car_height or cars.y < 0:#checking ends of screen
                crash(game_type)

        for things in list_thing:
            if things.thing_starty > display_height: #reseting after obstacles out of screen + counter
                reset_things(things,list_thing, dodged)
                dodged += 1

        for cars in list_cars:
            for things in list_thing:#checking crash with obstacles
                if cars.y < things.thing_starty + things.thing_height and cars.y > things.thing_starty or \
                                        cars.y + cars.car_height < things.thing_starty + things.thing_height and cars.y + cars.car_height > things.thing_starty: #checking his back of the car
                    #print ('y crossover')
                    if cars.x > things.thing_startx and cars.x < things.thing_startx + things.thing_width or \
                                                    cars.x +cars.car_width > things.thing_startx and cars.x +cars.car_width < things.thing_startx + things.thing_width:
                        #print (cars.x)
                        #print ("sx: " + str(things.thing_startx) + "tw: " + str(things.thing_startx + things.thing_width))
                        crash(game_type)

        #shooting
        if shoot.shooting_counter > 0:#checking shooting hit with obstacles and counted as dodged
            for shooting in shooting_list:
                shooting.move_shoot()
                shooting.draw_shoot(gameDisplay)
                destroy = destroy_thing(list_thing, shooting)
                if (destroy > 0):
                    shooting_list.remove(shooting)

                    list_thing.append(thing(random.randrange(0, display_width), -600, destroy.thing_speed, 20, 100, random_color, 0.1,
                                            0.5))  # x_start,y_start,thing_speed, thing_width,thing height, color, speed_after dodge, width_increase_after_dodge
                    list_thing.remove(destroy)

                    dodged += 1

        pygame.display.update()
        clock.tick(60) #fps

#main
game_intro()


from __future__ import annotations

from abc import abstractmethod, ABC
from typing import Optional

from api.mvc.controller.content_model.i_content_model_controller import IContentModelController
from api.mvc.controller.project.i_project_controller import IProjectController
from api.mvc.controller.property.i_property_controller import IPropertyController
from api.mvc.model.data.aspect_model import AspectModel
from api.mvc.model.data.content_model import ContentModel
from api.mvc.model.data.content_type_model import ContentTypeModel
from api.mvc.model.data.data_model import DataModel
from api.mvc.model.data.data_type import DataType
from api.mvc.model.data.folder_type_model import FolderTypeModel
from api.mvc.model.data.property_model import PropertyModel
from api.mvc.model.data.type_model import TypeModel
from api.mvc.model.service.file.content_model_service import ContentModelFileService
from api_core.exception.api_exception import ApiException
from api_core.helper.file_folder_helper import FileFolderHelper
from api_core.mvc.controller.controller import Controller
from api_core.mvc.service.model.service import Service
from api_core.mvc.view.view import View

class DataController(Controller, ABC):
    """
    Controller class used to manage API project's data.
    """

    def __init__(self, name: str, service: Service, view: View, pc: IProjectController, cmc: IContentModelController):
        """
        Initialize a new instance of DataController class.
        :param name: The name of the controller.
        :param service: The controller's basic service.
        :param view: The controller's view.
        :param pc: A project controller.
        :param cmc: A content model controller.
        :param prc: A property controller.
        """
        super().__init__(name, service, view)
        self._pc: IProjectController = pc
        self._cmc: IContentModelController = cmc
        self._prc: Optional[IPropertyController] = None
        self._cmfs: ContentModelFileService = ContentModelFileService()

    def set_property_controller(self, value: IPropertyController):
        """
        Change value of class property '_rpc'
        :param value: The new value of the '_rpc' class property.
        """
        self._prc = value

    def _get(self, content_model: ContentModel, data_type: str, name: str) -> Optional[DataModel]:
        """
        Retrieves the data model of an Alfresco AIO type or aspect.
        :param data_type: The type of the data.
        :param content_model: The type's content-model.
        :param name: The type or aspect name.
        :return: The data model of a type or aspect otherwise None.
        """
        if data_type.__eq__(DataType.TYPE.value):
            if name.__eq__("folder"):
                return FolderTypeModel(content_model)

            elif name.__eq__("content"):
                return ContentTypeModel(content_model)

        filename: str = FileFolderHelper.extract_filename_from_path(content_model.path)

        # Verification that the type exists.
        if self._cmfs.find_data(content_model, data_type, name) is None:
            return None

        # Verification that the aspect has been declared only once in the file.
        datas_name: list[str] = self._cmfs.get_data_names(content_model, data_type)
        if datas_name.count(name).__gt__(1):
            raise ApiException("{3} '{0}' was declared more than once in content model '{1}' in file '{2}'."
                               .format(name, content_model.complete_name, filename, data_type.title()))

        # Verification that there is no circular inheritance.
        ancestors: list[str] = self.__check_ancestors(content_model, data_type, name,
                                                      "{0}:{1}".format(content_model.prefix, name), [])
        self._check_mandatory_aspects(content_model, name, "{0}:{1}".format(content_model.prefix, name), ancestors, [])

        data: Optional[AspectModel | TypeModel] = None
        if data_type.__eq__(DataType.ASPECT.value):
            data = AspectModel(content_model, name, self._cmfs.get_aspect_title(content_model, name),
                               self._cmfs.get_aspect_description(content_model, name))
        else:
            data = TypeModel(content_model, name, self._cmfs.get_type_title(content_model, name),
                             self._cmfs.get_type_description(content_model, name))

        # Set the parent data.
        data.parent = self._get(content_model, self._cmfs.get_data_parent(content_model, data_type, name), name)

        # Set the data mandatory aspects.
        try:
            if data_type.__eq__(DataType.TYPE.value):
                for mandatory_aspect in self._cmfs.get_type_mandatory_aspects(content_model, name):
                    data.add_mandatory_aspect(self._get(content_model, DataType.ASPECT.value,
                                                        mandatory_aspect.rsplit(":", 1)[1]))

            else:
                for mandatory_aspect in self._cmfs.get_aspect_mandatory_aspects(content_model, name):
                    data.add_mandatory_aspect(self._get(content_model, DataType.ASPECT.value,
                                                        mandatory_aspect.rsplit(":", 1)[1]))
        except IndexError:
            raise ApiException("A mandatory aspect value of {0} '{1}' of content model '{2}' in file '{3}' is not "
                               "valid. Its be formed this way: prefix:name."
                               .format(data_type, name, content_model.complete_name, filename))

        # Recovery of properties.
        properties: list[str] = self._cmfs.get_data_property_names(content_model, data)

        index: int = 0
        property_found: bool = False
        maximum: int = len(properties)
        prop: Optional[PropertyModel] = None

        while index.__lt__(maximum) and not property_found:
            # Recovery of a property.
            prop = self._prc.load_property(content_model, data, properties[index])
            # Verification that a property is not declared twice.
            (property_found, data_name) = self.is_property_exist(data, data, prop)
            # Not declare = addition in the data model.
            if not property_found:
                data.add_property(prop)
                index += 1

        # property found = Declare twice = error
        if property_found:
            raise ApiException("Property '{0}' is defined twice in {1} '{2}' of content model '{3}' of file '{4}'."
                               .format(prop.name, data.typology, data.name, content_model.complete_name, filename))

        # Return the data
        return data

    def _extend(self, content_model: ContentModel, data_type: str, source_name: str, parent_name: str):
        """
        Method allowing a datum (aspect or type) to extend over another datum.
        :param content_model: The data content model.
        :param data_type: The type of data to bind.
        :param source_name: The name of the data to expand.
        :param parent_name: The name of the parent data.
        """
        source: DataModel = self._get(content_model, data_type, source_name)
        parent: DataModel = self._get(content_model, data_type, parent_name)
        filename: str = FileFolderHelper.extract_filename_from_path(content_model.path)

        if source is None:
            raise ApiException("The '{0}' {3} does not exist in the '{1}' content-model of the '{2} file.'"
                               .format(source_name, content_model.complete_name, filename, data_type))
        elif parent is None:
            raise ApiException("The '{0}' {3} does not exist in the '{1}' content-model of the '{2} file.'"
                               .format(source_name, content_model.complete_name, filename, data_type))

        self.__check_data_link(content_model, data_type, source, parent)
        self.__check_data_link(content_model, data_type, parent, source)

        self._service.extend(content_model, source, parent)

    def _add_mandatory(self, content_model: ContentModel, data_type: str, source_name: str, mandatory_name: str):
        """
        Method allowing to add a "mandatory-aspect" to data (aspect or type).
        :param content_model: The data content model.
        :param data_type: The type of data to bind.
        :param source_name: The type of data to modify (the one that will include the new mandatory-aspect).
        :param mandatory_name: The name of the required aspect to add.
        """
        # Retrieving the source data model model.
        source: DataModel = self._get(content_model, data_type, source_name)
        filename: str = FileFolderHelper.extract_filename_from_path(content_model.path)
        # Obligatory aspect recovery.
        mandatory: DataModel = self._get(content_model, DataType.ASPECT.value, mandatory_name)

        # Verification of the existence of data models.
        if source is None:
            raise ApiException("The '{0}' {3} does not exist in the '{1}' content-model of the '{2} file.'"
                               .format(source_name, content_model.complete_name, filename, data_type))
        elif mandatory is None:
            raise ApiException("The '{0}' {3} does not exist in the '{1}' content-model of the '{2} file.'"
                               .format(source_name, content_model.complete_name, filename, data_type))

        # Check that there is no circular inheritance between the two data models
        self.__check_data_link(content_model, data_type, source, mandatory)
        self.__check_data_link(content_model, data_type, mandatory, source)

        # Addition of the aspect in the list of mandatory aspects.
        self._cmfs.add_mandatory(content_model, source, mandatory)

    def __check_data_link(self, content_model: ContentModel, data_type: str, data_1: DataModel, data_2: DataModel):
        # source: str = data_1.name
        # complete_name: str = "{0}:{1}".format(content_model.prefix, source)

        filename: str = FileFolderHelper.extract_filename_from_path(content_model.path)
        ancestors: list[str] = self.__check_ancestors(content_model, data_type, data_1.name, data_1.complete_name, [])

        if data_2.name in ancestors:
            raise ApiException("The '{0}' {1} already has the '{2}' {1} for ancestor in the '{3}' file."
                               .format(data_1.name, data_type, data_2.name, filename))

        mandatory: list[str] = self._check_mandatory_aspects(content_model, data_1.name, data_1.complete_name, ancestors, [])

        if data_2.name in mandatory:
            raise ApiException("The '{0}' {1} already has the '{2}' {1} in the list of mandatory aspects (by "
                               "inheritance or directly) in the '{3}' file."
                               .format(data_1.name, data_type, data_2.name, filename))

    def __check_ancestors(self, content_model: ContentModel, typology: str, source: str, complete_name: Optional[str],
                          ancestors: list[str]) -> list[str]:
        if complete_name is None or (complete_name.__eq__("cm:folder") or complete_name.__eq__("cm:content")
                                     and typology.__eq__(DataType.TYPE.value)):
            # Removing the first element, which is the aspect we're trying to get.
            if len(ancestors).__gt__(0):
                ancestors.pop(0)
            return ancestors

        name: str = complete_name.rsplit(":", 1)[1]
        if self._cmfs.find_data(content_model, typology, name) is None:
            raise ApiException("There is an inheritance problem. {4} '{0}' inherits {5} '{1}' which does not "
                               "exist in content model '{2}' of file '{3}'.\n"
                               .format(ancestors[len(ancestors) - 1 if len(ancestors).__gt__(0) else 0], name,
                                       content_model.complete_name,
                                       FileFolderHelper.extract_filename_from_path(content_model.path),
                                       typology.title(), typology))

        if ancestors.count(name).__gt__(0):
            raise ApiException("There is an inheritance problem. {3} '{0}' appears twice in the ancestors of aspect"
                               " '{1}'.\n{2}".format(name, source, " -> ".join(ancestors), typology.title()))

        ancestors.append(name)
        return self.__check_ancestors(content_model, typology, source,
                                      self._cmfs.get_aspect_parent(content_model, name), ancestors)

    @abstractmethod
    def _check_mandatory_aspects(self, content_model: ContentModel, source: str, complete_name: Optional[str],
                                 ancestors: list[str], mandatory: list[str]) -> list[str]:
        pass

    def is_property_exist(self, data_source: DataModel, data: DataModel, property_model: PropertyModel) \
            -> tuple[bool, Optional[str]]:
        if data.parent is not None:
            self.is_property_exist(data_source, data.parent, property_model)

        index: int = 0
        maximum: int = len(data.properties)
        while index.__lt__(maximum) and data.properties[index].name.__ne__(property_model.name):
            index += 1

        if index.__ne__(maximum):
            return True, data.name

        success: bool = False
        index = 0
        maximum = len(data.mandatory)
        while index.__lt__(maximum) and not success:
            (success, data_model) = self.is_property_exist(data_source, data.mandatory[index], property_model)
            if not success:
                index += 1

        if index.__ne__(maximum):
            return True, data.name

        return False, None


import pandas as pd
df = pd.read_csv('HappinessIndex.csv')
dff = pd.DataFrame(df)

#Country with largest population
col = "pop2022"
max_pop = dff.loc[dff[col].idxmax()]
print("Country with the largest population and its corresponding rank, happines index in 2020 and 2021: ")
print( max_pop)

#Country with smallest population
col = "pop2022"
min_pop = dff.loc[dff[col].idxmin()]
print("Country with the smallest population and its corresponding rank, happines index in 2020 and 2021: ")
print( min_pop)

#Average happiness index in each year
#2020
df1 = dff["happiness2020"].mean()
print ("The average happiness index in 2020 is " ,df1)

#2021
df2 = dff["happiness2021"].mean()
print ("The average happiness index in 2021 is " ,df2)


# Opus/UrbanSim urban simulation software.
# Copyright (C) 2010-2011 University of California, Berkeley, 2005-2009 University of Washington
# See opus_core/LICENSE

from urbansim.gridcell.has_SSS_buildings import has_SSS_buildings as gridcell_has_SSS_buildings
from variable_functions import my_attribute_label

class has_SSS_buildings(gridcell_has_SSS_buildings):
    """Returns 1 if the location contains buildings of the given type, otherwise 0."""

    def dependencies(self):
        return [my_attribute_label(self.number_of_buildings)]

from opus_core.tests import opus_unittest
from urbansim.variable_test_toolbox import VariableTestToolbox
from urbansim.datasets.building_type_dataset import BuildingTypeDataset
from numpy import array
from numpy import ma
from opus_core.storage_factory import StorageFactory

class Tests(opus_unittest.OpusTestCase):
    variable_name = "urbansim.zone.has_commercial_buildings"

    def test_my_inputs(self):
        storage = StorageFactory().get_storage('dict_storage')

        building_types_table_name = 'building_types'
        storage.write_table(
            table_name = building_types_table_name,
            table_data = {
                'building_type_id':array([1,2]),
                'name': array(['foo', 'commercial'])
                }
            )

        building_types = BuildingTypeDataset(in_storage=storage, in_table_name=building_types_table_name)

        values = VariableTestToolbox().compute_variable(self.variable_name,
            data_dictionary = {
                'zone':{
                    'zone_id':array([1,2,3]),
                    },
                'building': {
                    'building_type_id':array([1,2,1,2,1,1]),
                    'zone_id':array([2,3,1,1,2,1])
                    },
                'building_type': building_types
                },
            dataset = 'zone'
            )

        should_be = array([1, 0, 1])

        self.assert_(ma.allequal(values, should_be),
            'Error in ' + self.variable_name)

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


"""Computes eigenvalues and eigenvectors of the PMI similarity matrices for a given attribute type. Saves the results of this along with kMeans clustering of the attributes, and the assignment of graph nodes to clusters."""

import pickle
import time
import numpy as np
import pandas as pd
import optparse
from scipy.sparse import coo_matrix, diags
from sklearn.cluster import KMeans
from gplus import *

def generate_cluster_report(attr_analyzer, attr_type, cluster_labels, topN = 30):
    """Given the AttributeAnalyzer, attr_type, and a list of cluster labels (corresponding to the attribute vocab indices only), generates a report listing the top N members of each cluster, and the frequency and prevalence (relative frequency) of each attribute in the data set. Orders the clusters by total occurrences of attributes in each cluster. If topN = None, list all the attributes in each cluster."""
    attr_freq_dict = attr_analyzer.attr_freqs_by_type[attr_type]
    total_attr_freqs = sum(attr_freq_dict.values())
    pfa = attr_analyzer.pairwise_freq_analyzers[attr_type]
    attr_indices, attr_vocab = get_attr_indices(pfa, attr_analyzer.attributed_nodes)
    unique_cluster_labels = set(cluster_labels)
    # compute vocab lists for each cluster
    attr_vocab_by_cluster = dict((lab, []) for lab in unique_cluster_labels)
    for (i, lab) in enumerate(cluster_labels):
        v = attr_vocab[i]
        if v.startswith('*???*'):
            continue
        freq = attr_freq_dict[v]
        attr_vocab_by_cluster[lab].append((v, freq, freq / total_attr_freqs))
    # sort vocab lists by decreasing frequencies
    for lab in unique_cluster_labels:
        attr_vocab_by_cluster[lab].sort(key = lambda item : item[1], reverse = True)
    # total number of occurrences of any attribute in each cluster
    total_freqs_by_cluster = dict((lab, sum([item[1] for item in attr_vocab_by_cluster[lab]])) for lab in unique_cluster_labels)
    info_by_cluster = dict((lab, dict()) for lab in unique_cluster_labels)
    # create a DataFrame for each cluster listing the top N vocab items in order with their frequencies and prevalences
    for lab in unique_cluster_labels:
        df = pd.DataFrame(attr_vocab_by_cluster[lab], columns = ['attribute', 'frequency', 'prevalence'])
        info_by_cluster[lab]['df'] = df if (topN is None) else df[:topN]
        info_by_cluster[lab]['size'] = len(attr_vocab_by_cluster[lab])
        info_by_cluster[lab]['totalFreq'] = total_freqs_by_cluster[lab]
        info_by_cluster[lab]['totalPrevalence'] = sum(df['prevalence'])
    # sort clusters by decreasing number of occurrences
    sorted_clusters_with_total_freqs = sorted(total_freqs_by_cluster.items(), key = lambda item : item[1], reverse = True)
    # generate report
    num_attrs = len(attr_vocab)
    s = ''
    for (lab, freq) in sorted_clusters_with_total_freqs:
        info = info_by_cluster[lab]
        width = 12 + len(str(lab))
        s += '#' * width + '\n'
        s += '# ' + 'CLUSTER ' + str(lab) + ' #\n'
        s += '#' * width + '\n\n'
        s += 'attribute prevalence  = %6d / %6d = %f\n' % (info['size'], num_attrs, info['size'] / num_attrs)
        s += 'occurrence prevalence = %6d / %6d = %f\n\n' % (info['totalFreq'], total_attr_freqs, info['totalPrevalence'])
        s += info['df'].to_string(index = False) + '\n\n\n'
    return s

# save off:
# matrix or LinearOperator for similarity matrix
# eigenvalues and scree plot
# embedded vectors corresponding to attributes
# kmeans clusters corresponding to attributes
# report of top clusters
# mappings from nodes to clusters of the given attribute type
def main():
    p = optparse.OptionParser()
    p.add_option('--attr_type', '-a', type = str, help = 'attribute type')
    p.add_option('-p', type = str, help = 'PMI type (PMIs, NPMI1s, or NPMI2s)')
    p.add_option('-e', type = str, help = 'embedding (adj, normlap, regnormlap)')
    p.add_option('-s', action = 'store_true', default = False, help = 'normalize in sphere')
    p.add_option('-d', type = float, help = 'smoothing parameter')
    p.add_option('-k', type = int, help = 'number of eigenvalues')
    p.add_option('-c', type = int, help = 'number of kmeans clusters')
    p.add_option('-t', type = float, default = None, help = 'tolerance for eigsh')
    p.add_option('-v', action = 'store_true', default = False, help = 'save scree plot')
    opts, args = p.parse_args()

    attr_type = opts.attr_type
    sim = opts.p
    embedding = opts.e
    assert (embedding in ['adj', 'normlap', 'regnormlap'])
    sphere = opts.s
    delta = opts.d
    k = opts.k
    nclusts = opts.c
    tol = opts.t
    save_plot = opts.v
    topN = 50  # for the report
    assert (((sim == 'PMIs') or (delta == 0)) and (sim in ['PMIs', 'NPMI1s', 'NPMI2s']))

    data_folder = 'gplus0_lcc/data/PMI/'
    report_folder = 'gplus0_lcc/reports/PMI/'
    plot_folder = 'gplus0_lcc/plots/PMI/'
    file_prefix1 = ('%s_%s_%s_delta' % (attr_type, sim, embedding)) + str(delta) + ('_k%d' % k)
    file_prefix2 = ('%s_%s_%s_delta' % (attr_type, sim, embedding)) + str(delta) + ('_k%d%s_c%d' % (k, '_normalized' if sphere else '', nclusts))

    print_flush("\nLoading AttributeAnalyzer...")
    a = AttributeAnalyzer()
    a.load_pairwise_freq_analyzer(attr_type)
    a.make_attrs_by_node_by_type()
    attrs_by_node = a.attrs_by_node_by_type[attr_type]
    pfa = a.pairwise_freq_analyzers[attr_type]
    n = pfa.num_vocab
    tol = (1.0 / n) if (tol is None) else tol  # use 1/n instead of machine precision as default tolerance
    attr_indices, attr_vocab = get_attr_indices(pfa, a.attributed_nodes)

    try:
        print_flush("\nLoading labels from '%s%s_labels.csv'..." % (data_folder, file_prefix2))
        labels = np.loadtxt('%s%s_labels.csv' % (data_folder, file_prefix2), dtype = int)
        print_flush("\nLoading cluster centers from '%s%s_cluster_centers.csv'..." % (data_folder, file_prefix2))
        cluster_centers = np.loadtxt('%s%s_cluster_centers.csv' % (data_folder, file_prefix2), delimiter = ',')
        print_flush("\nLoading eigenvalues from '%s%s_eigvals.csv'..." % (data_folder, file_prefix1))
        eigvals = np.loadtxt('%s%s_eigvals.csv' % (data_folder, file_prefix1), delimiter = ',')
        print_flush("\nLoading embedded features from '%s%s_features.pickle'..." % (data_folder, file_prefix1))
        features = pickle.load(open('%s%s_features.pickle' % (data_folder, file_prefix1), 'rb'))
        if sphere:
            for i in range(len(attr_indices)):
                features[i] = normalize(features[i])
    except FileNotFoundError:
        print_flush("Failed to load.")
        try:
            print_flush("\nLoading eigenvalues from '%s%s_eigvals.csv'..." % (data_folder, file_prefix1))
            eigvals = np.loadtxt('%s%s_eigvals.csv' % (data_folder, file_prefix1), delimiter = ',')
            print_flush("\nLoading embedded features from '%s%s_features.pickle'..." % (data_folder, file_prefix1))
            features = pickle.load(open('%s%s_features.pickle' % (data_folder, file_prefix1), 'rb'))
        except FileNotFoundError:
            print_flush("Failed to load.")
            print_flush("\nComputing similarity matrix (%s)..." % sim)
            sim_op = pfa.to_sparse_PMI_operator(sim, delta)
            matrix_type = 'adjacency' if (embedding == 'adj') else ('normalized Laplacian' if (embedding == 'normlap') else 'regularized normalized Laplacian')
            print_flush("\nComputing eigenvectors of %s matrix (k = %d)..." % (matrix_type, k))
            if (embedding == 'adj'):
                (eigvals, features) = timeit(eigsh)(sim_op, k = k, tol = tol)
                features = np.sqrt(np.abs(eigvals)) * features  # scale the feature columns by the sqrt of the eigenvalues
            elif (embedding == 'normlap'):
                normlap = SparseNormalizedLaplacian(sim_op)
                (eigvals, features) = timeit(eigsh)(normlap, k = k, tol = tol)
            elif (embedding == 'regnormlap'):
                regnormlap = SparseRegularizedNormalizedLaplacian(sim_op)
                (eigvals, features) = timeit(eigsh)(regnormlap, k = k, tol = tol)
            features = features[attr_indices, :]  # free up memory by deleting embeddings of nodes with no attributes
            np.savetxt('%s%s_eigvals.csv' % (data_folder, file_prefix1), eigvals, delimiter = ',')
            pickle.dump(features, open('%s%s_features.pickle' % (data_folder, file_prefix1), 'wb'))
        if sphere:  # normalize the features to have unit norm (better for kMeans)
            for i in range(len(attr_indices)):
                features[i] = normalize(features[i])
        km = KMeans(nclusts)
        print_flush("\nClustering attribute feature vectors into %d clusters using kMeans..." % nclusts)
        labels = timeit(km.fit_predict)(features)
        # save the cluster labels
        np.savetxt('%s%s_labels.csv' % (data_folder, file_prefix2), np.array(labels, dtype = int), delimiter = ',', fmt = '%d')
        # save the cluster centers
        cluster_centers = km.cluster_centers_
        np.savetxt('%s%s_cluster_centers.csv' % (data_folder, file_prefix2), cluster_centers, delimiter = ',')
        # save the attribute cluster report
        with open('%s%s_cluster_report.txt' % (report_folder, file_prefix2), 'w') as f:
            f.write(generate_cluster_report(a, attr_type, labels, topN))

    if save_plot:
        print_flush("\nSaving scree plot to '%s%s_screeplot.png'..." % (plot_folder, file_prefix1))
        scree_plot(eigvals, show = False, filename = '%s%s_screeplot.png' % (plot_folder, file_prefix1))

    print_flush("\nAssigning cluster labels to each node...")
    indices_by_vocab = dict((v, i) for (i, v) in enumerate(attr_vocab))
    centers = [normalize(center) for center in cluster_centers] if sphere else cluster_centers
    def assign_cluster(node):
        """Assigns -1 to a node with no attribute present. Otherwise, takes the cluster whose center is closest to the mean of the attribute vectors. Uses cosine distance if sphere = True, otherwise Euclidean distance."""
        if (node not in attrs_by_node):
            return -1
        else:
            attrs = list(attrs_by_node[node])
            if (len(attrs) == 1):
                return labels[indices_by_vocab[attrs[0]]]
            else:
                vec = np.zeros(k, dtype = float)
                for attr in attrs:
                    vec += features[indices_by_vocab[attr]]
                vec /= len(attrs)
                if sphere:
                    vec = normalize(vec)
                    sims = [np.dot(vec, center) for center in centers]
                else:
                    sims = [-np.linalg.norm(vec - center) for center in centers]
                max_index, max_sim = -1, -float('inf')
                for (i, sim) in enumerate(sims):
                    if (sim > max_sim):
                        max_index = i
                        max_sim = sim
                return max_index

    # save file with the list of cluster labels for each node
    clusters_by_node = [assign_cluster(i) for i in range(a.num_vertices)]
    np.savetxt('%s%s_node_labels.csv' % (data_folder, file_prefix2), np.array(clusters_by_node, dtype = int), delimiter = ',', fmt = '%d')
    print_flush("\nDone!")

if __name__ == "__main__":
    main()


import random
from string import ascii_letters
from typing import Any, Generator

def data_stream_gen(
    total_length: int | None = None, element_length: int = 100
) -> Generator[str, None, None]:
    if total_length is None:
        total_length == float("inf")
    idx = 0
    while idx < total_length:
        element = ""
        for el_length in range(element_length):
            element += random.choice(ascii_letters)
        yield element
        idx += 1

def n_th_data(data_gen: Generator[str, None, None], idx: int) -> str:
    for i in range(idx + 1):
        element = next(data_gen)
    return element

def solution(big_stream: Any) -> Any:
    random_element = None

    for idx, element in enumerate(big_stream):
        if idx == 0:
            random_element = element
        elif random.randint(1, idx + 1) == 1:  # prob of 1 in n
            random_element = element

    return random_element

TOTAL_LENGTH = 1_000_000
ELEMENT_LENGTH = 100
data = data_stream_gen(TOTAL_LENGTH, ELEMENT_LENGTH)
element_idx = random.randint(0, TOTAL_LENGTH)
print(n_th_data(data, element_idx))


import falcon
from routes.user import UserRoutes
from routes.workspace import WorkspaceRoutes
from routes.display import DisplayRoutes
from routes.token import TokenRoutes
from routes.scene import SceneRoutes
from routes.slide import SlideRoutes
from falcon.http_status import HTTPStatus

class HandleCORS(object):
	def process_request(self, req, resp):
		resp.set_header('Access-Control-Allow-Origin', '*')
		resp.set_header('Access-Control-Allow-Methods', '*')
		resp.set_header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
		resp.set_header('Access-Control-Max-Age', 1728000)  # 20 days
		if req.method == 'OPTIONS':
			raise HTTPStatus(falcon.HTTP_200, body='\n')

app = falcon.API(middleware=[HandleCORS()])
userRoutes = UserRoutes()
workspaceRoutes = WorkspaceRoutes()
displayRoutes = DisplayRoutes()
tokenRoutes = TokenRoutes()
sceneRoutes = SceneRoutes()
slideRoutes = SlideRoutes()

app.add_route('/user', userRoutes)
app.add_route('/user/register', userRoutes, suffix='register')
app.add_route('/user/login', userRoutes, suffix='login')
app.add_route('/user/forgot', userRoutes, suffix='forgot')
app.add_route('/user/reset', userRoutes, suffix='reset')

app.add_route('/token', tokenRoutes)

app.add_route('/workspaces', workspaceRoutes)
app.add_route('/workspaces/{workspaceId}', workspaceRoutes)

app.add_route('/workspaces/{workspaceId}/users/{userId}', userRoutes, suffix='giveaccess')

app.add_route('/workspaces/{workspaceId}/scenes', sceneRoutes)
app.add_route('/workspaces/{workspaceId}/scenes/{sceneId}', sceneRoutes, suffix='withSceneId')

app.add_route('/workspaces/{workspaceId}/slides', slideRoutes)
app.add_route('/workspaces/{workspaceId}/slides/{slideId}', slideRoutes, suffix='withSlideId')

app.add_route('/workspaces/{workspaceId}/displays', displayRoutes)
app.add_route('/workspaces/{workspaceId}/displays/{displayId}', displayRoutes, suffix='withDisplayId')