'''
Given an array which may contain duplicates, print all elements and their frequencies.

Examples:

Input :  arr[] = {10, 20, 20, 10, 10, 20, 5, 20}
Output : 10 3
         20 4
         5  1

Input : arr[] = {10, 20, 20}
Output : 10 1
         20 2
'''

# Is there a min/max length, is there a specified data type,
# are there mixed data types, input from command line? file?
# what about length 0

def count_freq(array):
    '''Count the frequency of all elements in an array'''
    # create dictionary
    frequencies = dict()
    # iterate down array
    for each in array:
    # first entry assigned value of 1
        if each in frequencies:
            frequencies[each] += 1
    # subsequent entries get value += 1
        else:
            frequencies[each] = 1
    # print in desired format
    # print(frequencies.items())
    for every in frequencies:
        print(str(every) + " " + str(frequencies[every]))
    print()
    # O(n): iterates over array once, checks a hashmap

if __name__ == "__main__":
    array1 = [10, 20, 20, 10, 10, 20, 5, 20]
    array2 = [10, 20, 20]
    count_freq(array1)
    count_freq(array2)


# Q3. Create a Game class that allows users to create their own games
# your class should allow users to create from a list of at least one game.
# You can decide the game to design in your class.

class Game:
    def __init__(self, name):
        """
        Create games by choosing `name`.
        """
        self.name = name
        self.questions = []
        self.player=[]

    def add_questions(self, questions):
        self.questions.extend(questions)
        print("{} questions added to {} successfully!".format(len(questions), self.name))

    def play(self,player):
        player=input("Type your name for the games : ")
        self.player.append({'name':player,'score':0})

        print("{}\n\n {} are you ready?\nLets begin:\n".format(self.name,self.player))
        playing = True
        while playing:
            question = (self.questions)
            print(question.find('question'))
            if question.get('options'):
                for option in question.get('options'):
                    print('- '+option)
            response = input('\n: ')
            if response.strip().lower() == 'q' or response.strip().lower() == 'quit' or response.strip().lower() == 'exit':
                playing = False
            elif response.strip().lower() == question.get('answer').strip().lower():
                print('Thats correct you earn 5 pionts!')
                self.player[-1]['score'] +=5
            else:
                print('Opps thats wrong you lose 3 pionts, the right answer is {}'.format(question.get('answer')))
                self.player[-1]['score'] -=3

firstGame = Game('Guess or Die')
secondGame = Game('Quiz of Life')
thirdGame = Game('Foodball is Life')

questions1 =  [{
                'question':"How old was Nur when he got his first tooth?",
                'options':[
                    '10 years',
                    '1 year',
                    '2 years',
                ],
                'answer': '1 year'
            },
            {
                'question':"What is Alameen's favorite thing?",
                'options':[
                    'Reading',
                    'Swimming',
                    'Food',
                ],
                'answer': 'Food'
            },
            ]

questions3 =  [{
                'question':"Who scored the winning goal in the 2014 world cup finals?",
                'options':[
                    'Gotze',
                    'Messi',
                    'Muller',
                ],
                'answer': 'Gotze'
            },
            {
                'question':"Who sponsors Real Madrid?",
                'options':[
                    'Spotify',
                    'Sporty',
                    'Emirates',
                ],
                'answer': 'Emirates'
            },
            ]

questions2 =  [{
                'question':"I am slim and burny what am I?",
                'answer': 'match stick'
            },
            {
                'question':"I let you travel through time without actually doing so, what am I?",
                'answer': 'book'
            },
            ]

firstGame.add_questions(questions2)
secondGame.add_questions(questions1)
thirdGame.add_questions(questions3)
thirdGame.add_questions(questions1)
thirdGame.add_questions(questions2)
thirdGame.play('nur')

# Q4. Upgrade the game class to manage players:
# there names, their scores and a leaderboard


#!/usr/bin/python3
"""lists all State objects from the database hbtn_0e_6_usa"""
from sys import argv
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

if __name__ == '__main__':
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'
                           .format(argv[1], argv[2],
                                   argv[3],), pool_pre_ping=True)
    name = argv[4]
    Base.metadata.create_all(engine)
    session = Session(engine)
    row = session.query(State).filter(State.name == name).first()
    if row is not None:
        print(row.id)
    else:
        print("Not found")
    session.close()


from repo.FileRepository import FileRepository
from domain.entities import Music
import random
import string

class MusicService(object):
    def __init__(self, repository):
        self.__repository = repository
        self.__genuri = ['Rock', 'Pop', 'Jazz', 'Alele']

    def get_all(self):
        """
        returnam toate elementele din lista
        :return: returnam toate elementele din lista
        """
        return self.__repository.get_all()

    def add(self, titlu, artist, gen, durata):
        """
        creeaza o noua entitate de tipul muzica
        :param titlu: titlul piesei
        :param artist: artistul piesei
        :param gen: genul piesei
        :param durata: durata piesei
        :return:
        """
        if gen in self.__genuri:
            music = Music(titlu, artist, gen, durata)
            self.__repository.add_item(music)
            return music
        return ValueError

    def update(self, titlu, artist, gen, durata):
        """
            updateaza o noua entitate de tipul muzica
            :param titlu: titlul piesei
            :param artist: artistul piesei
            :param gen: genul piesei
            :param durata: durata piesei
            :return:
        """
        if gen in self.__genuri:
            music = self.get_all()
            for melodie in music:
                if titlu == melodie.getTitlu() and artist == melodie.getArtist():
                    music = Music(titlu, artist, gen, durata)
                    self.__repository.update(melodie,music)
            return music
        return ValueError

    def save(self):
        """
        salveaza toate datele in fisier si in memorie
        :return:
        """
        music = self.get_all()
        save = FileRepository('data/data')
        for melodie in music:
            save.add_item(melodie)

    def random(self, size):
        """
        genereaza un anumit numar de elemente radom
        :param size: numarul de elemente de adaugat
        :return:
        """
        for i in range(int(size)):
            gen = random.sample(['Jazz', 'Pop', 'Rock', 'Altele'], k=1)
            durata = random.sample(range(1,300),k=1)

            gen1 = ''
            for i in gen:
                gen1=i
            print(gen1)
            durata1 = ''
            for i in durata:
                durata1 = i
            print(durata1)
            first1 = rest1 = ''
            for i in range(2):
                first = str(random.choices(string.ascii_lowercase))
                vocal = str(random.sample(['a','e','i','o','u'],k=1))
                first1 = first1 + first + vocal
            for i in range(3):
                rest = random.choices(string.ascii_lowercase)
                vocal = random.sample(['a', 'e', 'i', 'o', 'u'], k=1)
                rest1 = rest1 + str(rest) + str(vocal)
            first=rest = ''
            for i in first1:
                if i in string.ascii_lowercase:
                    first = first+i
            for i in rest1:
                if i in string.ascii_lowercase:
                    rest = rest+i
            titlu = str(first) +" " + rest
            print(titlu)
            first1 = rest1 = ''
            for i in range(2):
                first = str(random.choices(string.ascii_lowercase))
                vocal = str(random.sample(['a', 'e', 'i', 'o', 'u'], k=1))
                first1 = first1 + first + vocal
            for i in range(3):
                rest = random.choices(string.ascii_lowercase)
                vocal = random.sample(['a', 'e', 'i', 'o', 'u'], k=1)
                rest1 = rest1 + str(rest) + str(vocal)
            first = rest = ''
            for i in first1:
                if i in string.ascii_lowercase:
                    first = first + i
            for i in rest1:
                if i in string.ascii_lowercase:
                    rest = rest + i
            artist = str(first) + " " + rest
            print(artist)
            music = Music(titlu, artist, gen1, durata1)
            self.__repository.add_item(music)


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

from django.db import models, migrations
from django.conf import settings
import datetime
import main.models

class Migration(migrations.Migration):

    dependencies = [
        ('auth', '0006_require_contenttypes_0002'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Answer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('viewed_hint', models.BooleanField(default=False)),
                ('attempts', models.PositiveIntegerField(default=0)),
            ],
            options={
                'ordering': ('id',),
            },
        ),
        migrations.CreateModel(
            name='Exam',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('name', models.CharField(max_length=50)),
                ('info', models.TextField(blank=True)),
                ('time_limit', models.DurationField(verbose_name='Time limit to complete exam', default=datetime.timedelta(0))),
                ('shuffle_sections', models.BooleanField(verbose_name='Randomly shuffle sections', default=False)),
                ('can_attempt_again', models.BooleanField(verbose_name='Can a user attempt this exam multiple times', default=True)),
                ('author', models.TextField(blank=True)),
                ('comment', models.TextField(blank=True)),
                ('postinfo', models.TextField(blank=True)),
                ('attempt_group', models.ForeignKey(to='auth.Group', null=True, related_name='attexam_set')),
                ('owner', models.ForeignKey(to=settings.AUTH_USER_MODEL, null=True)),
                ('solutions_group', models.ForeignKey(to='auth.Group', null=True, related_name='solexam_set')),
            ],
        ),
        migrations.CreateModel(
            name='ExamAnswerSheet',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('start_time', models.DateTimeField(null=True)),
                ('end_time', models.DateTimeField(null=True)),
                ('exam', models.ForeignKey(to='main.Exam')),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.CreateModel(
            name='McqAnswer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('answer', models.OneToOneField(to='main.Answer')),
            ],
            options={
                'ordering': ('id',),
            },
            bases=(models.Model, main.models.SpecialAnswer),
        ),
        migrations.CreateModel(
            name='McqAnswerToMcqOption',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('mcq_answer', models.ForeignKey(to='main.McqAnswer')),
            ],
        ),
        migrations.CreateModel(
            name='McqOption',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('title', models.CharField(max_length=30, blank=True)),
                ('text', models.TextField()),
                ('is_correct', models.BooleanField(default=False)),
            ],
            options={
                'ordering': ('id',),
            },
        ),
        migrations.CreateModel(
            name='McqQuestion',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('multicorrect', models.BooleanField(default=False)),
            ],
            options={
                'ordering': ('id',),
            },
            bases=(models.Model, main.models.SpecialQuestion),
        ),
        migrations.CreateModel(
            name='Question',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('title', models.CharField(max_length=120, blank=True)),
                ('text', models.TextField(blank=True)),
                ('hint', models.TextField(blank=True)),
                ('solution', models.TextField(blank=True)),
                ('comment', models.TextField(blank=True)),
                ('metadata', models.TextField(blank=True)),
            ],
            options={
                'ordering': ('id',),
            },
        ),
        migrations.CreateModel(
            name='Section',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('name', models.CharField(max_length=50)),
                ('info', models.TextField(blank=True)),
                ('comment', models.TextField(blank=True)),
                ('postinfo', models.TextField(blank=True)),
                ('correct_marks', models.IntegerField(verbose_name='Marks for correct answer', default=1)),
                ('wrong_marks', models.IntegerField(verbose_name='Marks for wrong answer', default=0)),
                ('na_marks', models.IntegerField(verbose_name='Marks for not attempting question', default=0)),
                ('hint_deduction', models.IntegerField(verbose_name='Marks deducted for viewing hint', default=0)),
                ('unlock_marks', models.IntegerField(verbose_name='Minimum marks to attempt this section', null=True)),
                ('unlock_questions', models.PositiveIntegerField(verbose_name='Minimum attempted questions to attempt this section', null=True)),
                ('unlock_both_needed', models.BooleanField(verbose_name='Should both minimum question and minimum marks requirements be fulfilled?', default=True)),
                ('shuffle_options', models.BooleanField(verbose_name='Randomly shuffle options of questions', default=False)),
                ('shuffle_questions', models.BooleanField(verbose_name='Randomly shuffle questions', default=False)),
                ('allowed_attempts', models.IntegerField(verbose_name='Number of attempts allowed per question', default=0)),
                ('show_correct_answer', models.BooleanField(verbose_name='Should correct answer be shown after attempting a question?', default=False)),
                ('show_solution', models.BooleanField(verbose_name='Should solution be shown after attempting a question?', default=False)),
                ('max_questions_to_attempt', models.PositiveIntegerField(verbose_name='Maximum number of questions a student is allowed to attempt', default=0)),
                ('exam', models.ForeignKey(to='main.Exam')),
            ],
            options={
                'ordering': ('id',),
            },
        ),
        migrations.CreateModel(
            name='SectionAnswerSheet',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('exam_answer_sheet', models.ForeignKey(to='main.ExamAnswerSheet')),
                ('section', models.ForeignKey(to='main.Section')),
            ],
            options={
                'ordering': ('id',),
            },
        ),
        migrations.CreateModel(
            name='Tag',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('name', models.CharField(max_length=30, unique=True)),
                ('description', models.TextField(blank=True)),
            ],
        ),
        migrations.CreateModel(
            name='TextAnswer',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('response', models.TextField(blank=True)),
                ('answer', models.OneToOneField(to='main.Answer')),
            ],
            options={
                'ordering': ('id',),
            },
            bases=(models.Model, main.models.SpecialAnswer),
        ),
        migrations.CreateModel(
            name='TextQuestion',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, primary_key=True, auto_created=True)),
                ('ignore_case', models.BooleanField(default=True)),
                ('use_regex', models.BooleanField(default=False)),
                ('correct_answer', models.TextField()),
                ('question', models.OneToOneField(to='main.Question')),
            ],
            options={
                'ordering': ('id',),
            },
            bases=(models.Model, main.models.SpecialQuestion),
        ),
        migrations.AddField(
            model_name='textanswer',
            name='special_question',
            field=models.ForeignKey(to='main.TextQuestion'),
        ),
        migrations.AddField(
            model_name='section',
            name='tags',
            field=models.ManyToManyField(to='main.Tag'),
        ),
        migrations.AddField(
            model_name='question',
            name='section',
            field=models.ForeignKey(to='main.Section'),
        ),
        migrations.AddField(
            model_name='question',
            name='tags',
            field=models.ManyToManyField(to='main.Tag'),
        ),
        migrations.AddField(
            model_name='mcqquestion',
            name='question',
            field=models.OneToOneField(to='main.Question'),
        ),
        migrations.AddField(
            model_name='mcqoption',
            name='special_question',
            field=models.ForeignKey(to='main.McqQuestion'),
        ),
        migrations.AddField(
            model_name='mcqanswertomcqoption',
            name='mcq_option',
            field=models.ForeignKey(to='main.McqOption'),
        ),
        migrations.AddField(
            model_name='mcqanswer',
            name='chosen_options',
            field=models.ManyToManyField(to='main.McqOption', through='main.McqAnswerToMcqOption'),
        ),
        migrations.AddField(
            model_name='mcqanswer',
            name='special_question',
            field=models.ForeignKey(to='main.McqQuestion'),
        ),
        migrations.AddField(
            model_name='exam',
            name='tags',
            field=models.ManyToManyField(to='main.Tag'),
        ),
        migrations.AddField(
            model_name='answer',
            name='section_answer_sheet',
            field=models.ForeignKey(to='main.SectionAnswerSheet'),
        ),
    ]


def fib():
    fibs = [0, 1]

    for i in range(1,99):
        i += 1
        fib = (fibs[i-1]) + (fibs[i-2])

        fibs.append(fib)

    return fibs

def main():
    print('OUTPUT', fib())

if __name__ == "__main__":
    main()


import os
import cv2

images = os.listdir("/home/anirudh_/images/")
#images.sort()
j = 0
length = len(images)
for i in images:
    img = cv2.imread(r"/home/anirudh_/images/" + i, 1)
    print(i, j, length)
    j+=1
    cv2.imshow(i, img)
    if cv2.waitKey(0) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        continue
    elif cv2.waitKey(0) & 0xFF == ord('e'):
        cv2.destroyAllWindows()
        os.remove(i)


import struct, datetime, pprint, sys, numpy, os
from podi_definitions import *

def my_float(s):
    try:
        return float(s)
    except:
        return 999.99 #numpy.NaN
    return 0

# functions for converting input fields to usable data
cnv_text = lambda s: s.rstrip()
cnv_int = lambda s: int(s)
cnv_float = lambda s: my_float(s)
cnv_date_dmy = lambda s: datetime.datetime.strptime(s, "%d%m%Y") # ddmmyyyy
# etc

# field specs (field name, start pos (1-relative), len, converter func)
fieldspecs = [
    ('usnob1_id',   1,  12, cnv_text),
    ('ra',         15,  10, cnv_float),
    ('dec',        26,  10, cnv_float),
    ('bmag1',      77,   5, cnv_float),
    ('rmag1',      84,   5, cnv_float),
    ('bmag2',      91,   5, cnv_float),
    ('rmag2',      98,   5, cnv_float),
    ]

fieldspecs.sort(key=lambda x: x[1]) # just in case

# build the format for struct.unpack
unpack_len = 0
unpack_fmt = ""
for fieldspec in fieldspecs:
    start = fieldspec[1] - 1
    end = start + fieldspec[2]
    if start > unpack_len:
        unpack_fmt += str(start - unpack_len) + "x"
    unpack_fmt += str(end - start) + "s"
    unpack_len = end
field_indices = range(len(fieldspecs))
#print unpack_len, unpack_fmt
unpacker = struct.Struct(unpack_fmt).unpack_from

#print "CALCSIZE=",struct.calcsize(unpack_fmt)

def query_usno(ra, dec, radius, maxcount, catfile, download=True):

    if (ra < 0): ra += 360.0

    # Assemble the command string
    findusno = "findusnob1 %.8f %.8f -r %d -e b -m %d -sm > %s" % (ra, dec, radius, maxcount, catfile)
    print(findusno)
    stdout_write("Downloading USNO catalog from CDS/Vizier...\n")
    if (download):
        os.system(findusno)

    # Read the resulting catalog file
    file = open(catfile, "rb")
    raw_data = file.read()

    results = numpy.zeros(shape=(maxcount, 5))

    # Now interpret the resulting catalog line by line
    current_star = 0
    for line in file.readlines():

        # Ignore comment lines
        if (line[0] == "#"):
            continue

        raw_fields = unpacker(line[:struct.calcsize(unpack_fmt)])

        # Activate this for debugging
        #for x in field_indices:
        #    print fieldspecs[x][0], fieldspecs[x][3](raw_fields[x]),
        #print

        # Put the data into the resulting array
        results[current_star,0] = fieldspecs[1][3](raw_fields[1])
        results[current_star,1] = fieldspecs[2][3](raw_fields[2])

        bmag = numpy.array([fieldspecs[3][3](raw_fields[3]),
                            fieldspecs[5][3](raw_fields[5])])
        rmag = numpy.array([fieldspecs[4][3](raw_fields[4]),
                            fieldspecs[6][3](raw_fields[6])])

        results[current_star,2] = numpy.min(bmag)
        results[current_star,3] = numpy.min(rmag)
        results[current_star,4] = numpy.min([bmag,rmag])
        current_star += 1

    return results[:current_star,:]

if __name__ == "__main__":

    results = query_usno(10.74225833333333, 41.37409777777778, 45, 1000, "usno.cat")
    print("Read",results.shape[0],"stars from USNO-B1")

    #for i in range(results.shape[0]):
    #    print results[i,0], results[i,1], results[i,2], results[i,3], results[i,4]

    #print results


import pandas, numpy, scipy.ndimage
import os, timeit
import matplotlib.pyplot as plt
import random
import math
import logging
import multiprocessing as mp

home = 'C:/Users/OPEN/Documents/NanZHAO/Projet CIR Fraude/'
path = 'C:/Users/OPEN/Documents/NanZHAO/Projet CIR Fraude/data/store/'
logging.basicConfig(filename = os.path.join(home, 'code/images_info.log'), level = logging.INFO)
process_number = 40

images_all = os.listdir(path)

def get_image_info(im):
	postId = im.split('_')[0]
	Id = im.split(postId+'_')[1]
	logging.info(Id)
	shapes = scipy.ndimage.imread(os.path.join(path, im)).shape
	height = shapes[0]
	width  = shapes[1]
	if len(shapes) == 3:
		channels = shapes[2]
	else:
		channels = 2

	return(pandas.Series({'Id':Id, 'postId':postId, 'height':height, 'width':width, 'channels': channels}))

if __name__ == '__main__':
	start = timeit.default_timer()
	image_info = pandas.DataFrame(columns = ('Id','postId', 'height', 'width', 'channels'))
	count =  0
	p = mp.Pool(process_number)
	for image in images_all:
		res = p.apply_async(get_image_info, (image,)).get(timeout=10)
		#print(res)
		image_info = image_info.append (res, ignore_index=True)
		#image_info.append (res.get(timeout=10))
		#print(image_info.head())
		"""
		count = count + 1
		if (count%2075) == 0:
			print(" %d percentage is done." %(count/2075) )
		"""
	print(image_info.head())
	print(len(image_info))
	image_info.to_json(os.path.join(home, 'data/images_info.json'))
	stop = timeit.default_timer()
	print("Execution time: ", (stop-start))


def getRepos():
    import pisi
    import deopkg

    rdb = pisi.db.repodb.RepoDB()
    for repo in rdb.list_repos(False):
        dr = deopkg.EopkgRepo()
        dr.uri = rdb.get_repo_url(repo)
        dr.name = repo
        dr.enabled = rdb.repo_active(repo)
        yield dr


import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
import pandas as pd
import pickle
import re
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.multioutput import MultiOutputClassifier
from sklearn.pipeline import Pipeline
from sqlalchemy import create_engine
import sys

nltk.download(['punkt', 'stopwords', 'wordnet'])

def load_data(database_filepath):
    """
    Reads in data from SQLite database and outputs dataframes for ML model

    INPUT:
        database_filepath: filepath to SQLite database

    OUTPUTS:
        X: features dataframe
        y: target dataframe
        category_names: names of category targets
    """
    # create SQLAlchemy engine and read in data
    engine = create_engine('sqlite:///' + database_filepath)
    df = pd.read_sql_table('df', engine)

    # create features (X) and target (y) dataframes
    X = df['message']
    y = df.iloc[:, 4:]

    # retrieve names of targets
    category_names = y.columns

    return X, y, category_names

def tokenize(text):
    """
    Cleans text data in order to use it for machine learning later.

    INPUT:
        text: Raw message

    OUTPUT:
        cleaned_tokens: Cleaned text (w/o urls, normalized, tokenized, w/o stopwords, lemmatized) as list
    """
    # Use regex expression to detect urls in text
    url_regex = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
    detected_urls = re.findall(url_regex, text)

    # Replace all urls with placeholder
    for detected_url in detected_urls:
        text = text.replace(detected_url, "url")

    # Normalization
    text = re.sub(r"[^a-zA-Z0-9]", " ", text.lower())

    # Tokenization
    words = word_tokenize(text)

    # Removal of stopwords
    words = [w for w in words if w not in stopwords.words("english")]

    # Lemmatization (nouns)
    cleaned_tokens = [WordNetLemmatizer().lemmatize(w).strip() for w in words]

    return cleaned_tokens

def build_model():
    """
    Builds a ML Pipeline using a RandomForest classifier and GridSearchCV to tune it to its optimal hyperparameters.

    INPUT:
        None

    OUTPUT:
        model: GridSearch output from cross-validation
    """
    # set up of ML Pipeline
    pipeline = Pipeline([
        ('vect', CountVectorizer(tokenizer=tokenize)),
        ('tfidf', TfidfTransformer()),
        ('clf', MultiOutputClassifier(RandomForestClassifier(random_state=42)))
    ])

    # set up of hyper-parameter dictionary for GridSearchCV of RandomForestClassifier
    params = {
        'clf__estimator__min_samples_split': [2, 4],  # min number of data points in node before the node is split
        'clf__estimator__max_features': ['log2', 'auto'],  # max number of features considered for splitting a node
        'clf__estimator__n_estimators': [50, 100],  # number of trees in the forest
    }

    # create model
    model = GridSearchCV(pipeline, param_grid=params, scoring='accuracy', verbose=3, n_jobs=1, cv=3)
    return model

def evaluate_model(model, X_test, y_test, category_names):
    """
    Evaluate trained ML model performance against test data.

    INPUTS:
        model: Trained ML model
        X_test: Test data (features)
        y_test: Test data (targets / labels)
        target_names: Names of labels

    OUTPUT:
        Printed results from GridSearch; classification report and accuracy for all categories
    """
    # print results from GridSearch
    model_results_table = pd.concat([pd.DataFrame(model.cv_results_["params"]),
                                     pd.DataFrame(model.cv_results_["mean_test_score"],
                                                  columns=["Accuracy"])], axis=1)

    print("GridSearch Results Table")
    print(model_results_table)

    print("Best-performing parameters from GridSearch:", model.best_params_)

    # make predictions on test data
    y_pred = model.predict(X_test)

    # print classification reports
    print("Classification report incl. overall micro, macro, weighted and sample averages")
    print(classification_report(y_test, y_pred, target_names=category_names, zero_division=0))

    print("Individual classification report incl. accuracy, macro and weighted averages")
    y_pred_df = pd.DataFrame(y_pred, columns=y_test.columns)
    for category in y_test.columns:
        print('-------------------------------------------------\n Category: {}\n'.format(category))
        print(classification_report(y_test[category], y_pred_df[category], zero_division=0))

def save_model(model, model_filepath):
    """
    Save trained model to pickle file

    INPUTS:
        model: trained model
        model_filepath: filepath to save model as pickle file (byte stream)

    OUTPUT:
        pickle file // none
    """
    # save trained model in binary
    with open(model_filepath, 'wb') as file:
        pickle.dump(model, file)

def main():
    if len(sys.argv) == 3:
        database_filepath, model_filepath = sys.argv[1:]
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, y, category_names = load_data(database_filepath)
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

        print('Building model...')
        model = build_model()

        print('Training model...')
        model.fit(X_train, y_train)

        print('Evaluating model...')
        evaluate_model(model, X_test, y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    else:
        print('Please provide the filepath of the disaster messages database ' \
              'as the first argument and the filepath of the pickle file to ' \
              'save the model to as the second argument. \n\nExample: python ' \
              'train_classifier.py ../data/Disaster.db classifier.pkl')

if __name__ == '__main__':
    main()


import pandas as pd
import dash
from dash import dcc, html, Input, Output
from dash.exceptions import PreventUpdate
from dash.dependencies import State
import base64
import io

# Create the Dash app
app = dash.Dash(__name__)

# Add the Font Awesome CSS link to your app
app.css.append_css({
    "external_url": "https://use.fontawesome.com/releases/v5.15.3/css/all.css"
})

# Define the layout of the dashboard
app.layout = html.Div([
    html.H1([html.I(className='fas fa-chart-bar'), ' Data Visualization Dashboard'], style={'textAlign': 'center', 'color': '#007BFF'}),

    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '50%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': 'auto',
            'marginBottom': '20px'
        },
        # Allow multiple files to be uploaded
        multiple=False
    ),dcc.Dropdown(
        id='dropdown',
        options=[],
        value=None,
        style={'width': '50%', 'margin': 'auto', 'marginBottom': '20px'}
    ),

    html.Div([
        dcc.Graph(id='bar-chart', className='graph-container'),
        dcc.Graph(id='line-chart', className='graph-container'),
    ], style={'display': 'flex', 'flexWrap': 'wrap', 'justifyContent': 'center'})

])

# Callback to read the uploaded file and update the dropdown options
@app.callback(
    [Output('dropdown', 'options'),
     Output('dropdown', 'value')],
    [Input('upload-data', 'contents')],
    [State('upload-data', 'filename')]
)
def update_dropdown_options(contents, filename):
    if contents is None:
        raise PreventUpdate

    df = parse_contents(contents, filename)
    dropdown_options = [{'label': col, 'value': col} for col in df.columns]
    dropdown_value = df.columns[0] if not df.empty else None
    return dropdown_options, dropdown_value

# Callback to update the bar chart based on the dropdown selection
@app.callback(
    Output('bar-chart', 'figure'),
    [Input('dropdown', 'value')],
    [State('upload-data', 'contents')],
    [State('upload-data', 'filename')]
)
def update_bar_chart(selected_value, contents, filename):
    if selected_value is None or contents is None:
        raise PreventUpdate

    df = parse_contents(contents, filename)
    data_to_plot = df[['Month', selected_value]].to_dict('list')
    return {
        'data': [{'x': data_to_plot['Month'], 'y': data_to_plot[selected_value], 'type': 'bar'}],
        'layout': {'title': f'{selected_value} by Month'}
    }

# Callback to update the line chart based on the dropdown selection
@app.callback(
    Output('line-chart', 'figure'),
    [Input('dropdown', 'value')],
    [State('upload-data', 'contents')],
    [State('upload-data', 'filename')]
)
def update_line_chart(selected_value, contents, filename):
    if selected_value is None or contents is None:
        raise PreventUpdate

    df = parse_contents(contents, filename)
    data_to_plot = df[['Month', selected_value]].to_dict('list')
    return {
        'data': [{'x': data_to_plot['Month'], 'y': data_to_plot[selected_value], 'type': 'line'}],
        'layout': {'title': f'{selected_value} by Month'}
    }

def parse_contents(contents, filename):
    content_type, content_string = contents.split(',')
    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV file
            df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
        else:
            raise Exception('Unsupported file format')
    except Exception as e:
        print(e)
        return pd.DataFrame()
    return df

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)


import os
import sys
from tempfile import TemporaryFile

from metomi.rose.config import ConfigDecodeError, ConfigLoader
from metomi.rose.popen import RosePopener, RosePopenError
from metomi.rose.reporter import Reporter

class BadChange(Exception):
    """An error representing a bad change in a commit transaction."""

    FMT_HEAD = "%s: %-4s%s"
    FMT_TAIL_1 = ": %s"
    FMT_TAIL_M = ":\n%s"
    NO_INFO = "SUITE MUST HAVE INFO FILE"
    NO_OWNER = "SUITE MUST HAVE OWNER SPECIFIED IN INFO FILE"
    NO_TRUNK = "SUITE MUST HAVE TRUNK"
    PERM = "PERMISSION DENIED"
    USER = "NO SUCH USER"
    VALUE = "BAD VALUE IN FILE"

    def __init__(self, status, path, reason=PERM, content=""):
        Exception.__init__(self, status, path, reason, content)
        self.status = status
        self.path = path
        self.reason = reason
        self.content = content

    def __str__(self):
        tail = ""
        if self.content and "\n" in self.content:
            tail = self.FMT_TAIL_M % self.content
        elif self.content:
            tail = self.FMT_TAIL_1 % self.content
        return self.FMT_HEAD % (self.reason, self.status, self.path) + tail

class InfoFileError(BadChange):
    """Error representing a bad or missing info file.

    Args:
        reason (str): a user-friendly message.
        exception (Exception): the exception encountered in loading the
            bad info file.
    """

    VALUE = "BAD VALUE IN INFO FILE"

    def __init__(self, reason=BadChange.PERM, exception=None):
        self.reason = reason
        self.exc = exception

    def __str__(self):
        if self.exc:
            return f"{self.reason}: {self.exc}"
        return self.reason

class BadChanges(Exception):
    """An error representing bad changes in a commit transaction."""

    def __str__(self):
        bad_changes = self.args[0]
        return "\n".join(str(bad_change) for bad_change in bad_changes)

class RosieSvnHook(object):
    """A parent class for hooks on a Rosie Subversion repository."""

    DATE_FMT = "%Y-%m-%d %H:%M:%S %Z"
    ID_CHARS_LIST = ["abcdefghijklmnopqrstuvwxyz"] * 2 + ["0123456789"] * 3
    LEN_ID = len(ID_CHARS_LIST)
    TRUNK = "trunk"
    INFO_FILE = "rose-suite.info"
    TRUNK_INFO_FILE = "trunk/rose-suite.info"
    ST_ADDED = "A"
    ST_DELETED = "D"
    ST_MODIFIED = "M"
    ST_UPDATED = "U"
    ST_EMPTY = " "

    def __init__(self, event_handler=None, popen=None):
        if event_handler is None:
            event_handler = Reporter()
        self.event_handler = event_handler
        if popen is None:
            popen = RosePopener(self.event_handler)
        self.popen = popen
        self.path = os.path.dirname(
            os.path.dirname(sys.modules["metomi.rosie"].__file__)
        )

    def _svnlook(self, *args):
        """Return the standard output from "svnlook"."""
        command = ["svnlook", *args]
        return self.popen(*command)[0]

    def _load_info(
        self,
        repos,
        sid,
        branch=None,
        revision=None,
        transaction=None,
        allow_popen_err=False,
    ):
        """Load info file from branch_path in repos @revision.

        Returns a ConfigNode for the "rose-suite.info" of a suite at a
        particular revision or transaction.

        Args:
            repos (str): The path of the repository.
            sid (str): The Rosie suite unique identifier, e.g. "ay327".
            branch (str): The branch that the info file is under.
            revision (int, str): A commit revision number. Cannot be used with
                transaction.
            transaction (str): A commit transaction identifer. Cannot be used
                with revision.
            allow_popen_err (bool): If True, return None if a RosePopenError
                occurs during svnlook command.
        """
        if not branch:
            branch = self.TRUNK
        commit_opts = []
        if transaction is not None:
            if revision is not None:
                raise ValueError(
                    f"Cannot load transaction {transaction} and "
                    f"revision {revision} at the same time"
                )
            commit_opts = ["-t", transaction]
        if revision is not None:
            commit_opts = ["-r", str(revision)]
        info_file_path = os.path.join("/".join(sid), branch, self.INFO_FILE)
        t_handle = TemporaryFile()
        try:
            t_handle.write(
                self._svnlook(
                    "cat", repos, info_file_path, *commit_opts
                ).encode()
            )
        except UnicodeDecodeError as err:
            raise ConfigDecodeError(info_file_path, err)
        except RosePopenError as err:
            if allow_popen_err:
                return None
            raise err
        t_handle.seek(0)
        config_node = ConfigLoader(allow_sections=False).load(t_handle)
        t_handle.close()
        return config_node


import logging
import scipy
import time
import json
from sentence_transformers import SentenceTransformer
import threading
import zmq
import zmq.decorators as zmqd
# from multiprocessing import Process

class QTCosine(threading.Thread):
    def __init__(self, model_path, stop_sign:threading.Event, recv_port=1991, send_port=1992, classid=0, logger=logging.getLogger("QTCosine")):
        super(QTCosine, self).__init__()
        self.model_path = model_path
        self.model = SentenceTransformer(model_path)
        self.logger = logger
        self.id = classid
        self.recv_port = recv_port
        self.send_port = send_port
        self.stop_sign = stop_sign

    def run(self):
        self._run()

    @zmqd.context()
    @zmqd.socket(zmq.PULL)
    @zmqd.socket(zmq.PUSH)
    def _run(self,_, receiver, sender):
        receiver.connect('tcp://127.0.0.1:%d'%(self.recv_port))
        sender.connect('tcp://127.0.0.1:%d'%(self.send_port))
        self.logger.info("QTCosine[%d] connect recv_port:%d, send_por:%d" % (self.id, self.recv_port, self.send_port))

        while self.stop_sign.is_set() == False:
            events = receiver.poll(1000)
            if events:
                data = receiver.recv().decode("utf-8")
                json_part = json.loads(data.split('\t')[0])
                query = json_part["query"]
                title = json_part["title"]
                texts = [query, title]
                embeddings = self.model.encode(texts, convert_to_numpy=True)
                cosine = 1 - scipy.spatial.distance.cosine(embeddings[0], embeddings[1])

                res_data = "%s\t%f" % (data, cosine)
                res_data = res_data.encode("utf-8")
                sender.send(res_data)

        self.logger.info("QTCosine[%d] stop run" % (self.id))

class DataReader(threading.Thread):
    def __init__(self, input_file, stop_sign:threading.Event, send_port=1991, logger=logging.getLogger(), encoding="utf-8"):
        super(DataReader, self).__init__()
        self.input_file = input_file
        self.stop_sign = stop_sign
        self.encoding = encoding
        self.send_port = send_port
        self.logger = logger

    def run(self):
        self._run()

    @zmqd.context()
    @zmqd.socket(zmq.PUSH)
    def _run(self, _, sender):
        sender.bind('tcp://127.0.0.1:%d'%(self.send_port))
        self.logger.info("DataReader bind send_port:%d" % (self.send_port))
        rfp = open(input_file, "r", encoding=self.encoding)
        while self.stop_sign.is_set() == False:
            for line in rfp:
                data = line.rstrip('\n').encode("utf-8")
                sender.send(data)
        rfp.close()
        self.logger.info("DataReader stop")

class DataReceiver(threading.Thread):
    def __init__(self, output_file, stop_sign:threading.Event, recv_port=1992, logger=logging.getLogger(), encoding="utf-8"):
        super(DataReceiver, self).__init__()
        self.output_file = output_file
        self.stop_sign = stop_sign
        self.encoding = encoding
        self.recv_port = recv_port
        self.logger = logger

    def run(self):
        self._run()

    @zmqd.context()
    @zmqd.socket(zmq.PULL)
    def _run(self, _, receiver):
        receiver.bind('tcp://127.0.0.1:%d'%(self.recv_port))
        self.logger.info("DataReceiver bind recv_port:%d" % (self.recv_port))
        last_time = time.time()
        wfp = open(self.output_file, "w", encoding=self.encoding)
        count = 0
        st = time.time()
        while self.stop_sign.is_set() == False:
            events = receiver.poll(timeout=1000)
            if events:
                data = receiver.recv()
                data = data.decode("utf-8")
                wfp.write("%s\n" % (data))
                count += 1
                last_time = time.time()
            else:
                now = time.time()
                if int(now - last_time) > 20:
                    self.stop_sign.set()
                    self.logger.info("set stop_sign")
            ed = time.time()
            if int(ed - st) >= 60:
                st = time.time()
                speed = float(count) / 60
                self.logger.info("[check speed] %d" % (speed))
                count = 0
        wfp.close()
        self.logger.info("DataReceiver stop")

def predict_query_title_score(input_file, output_file, model_path, thread_num=3):
    logging.basicConfig(level=logging.INFO,
                        format="[%(levelname).1s %(asctime)s] %(message)s",
                        datefmt="%Y-%m-%d_%H:%M:%S")
    logger = logging.getLogger()

    stop_sign = threading.Event()
    data_read_port = 1991
    data_recv_port = 1992
    data_reader = DataReader(input_file, stop_sign, data_read_port, logger=logger)
    data_receiver = DataReceiver(output_file, stop_sign, data_recv_port, logger=logger)

    worker_list = []
    for i in range(thread_num):
        worker = QTCosine(model_path, stop_sign, recv_port=data_read_port, send_port=data_recv_port, classid=i, logger=logger)
        worker_list.append(worker)

    for w in worker_list:
        w.start()

    data_reader.start()
    data_receiver.start()

    for w in worker_list:
        w.join()

    data_reader.join()
    data_receiver.join()

if __name__ == "__main__":
    input_file = "/search/odin/liruihong/websearch/websearch_data/data_20part"
    output_file = "/search/odin/liruihong/websearch/res_data/data_20part_qtfeature"
    model_path = "/search/odin/liruihong/websearch/model_data/fuse-data-epoch5-utf8"
    thread_num = 30
    predict_query_title_score(input_file, output_file, model_path, thread_num = thread_num)


#!/usr/bin/env python2

import os
import os.path

def main():
    options = gather_information(get_defaults())
    if 'DRONE_SERVER' not in os.environ or 'DRONE_TOKEN' not in os.environ:
        setup_cli_access(
            drone_server=options['drone_server'],
            drone_token=options['drone_token']
        )
    repos = get_repos()
    secrets = get_secrets()
    add_secrets_to_repos(
        repos=repos,
        secrets=secrets
    )
    cleanup()

def get_defaults():
    return {
        'drone_server': 'https://example.com',
        'drone_token': 'my-drone-token'
    }

def gather_information(defaults):
    options = {}
    if 'DRONE_SERVER' in os.environ or 'DRONE_TOKEN' in os.environ:
        return options
    options['drone_server'] = _default_prompt('Drone Server', defaults['drone_server'])
    options['drone_token'] = _default_prompt('Drone Token', defaults['drone_token'])
    return options

def setup_cli_access(**kwargs):
    if os.path.isfile(os.environ['HOME'] + '/.bashrc'):
        path = os.environ['HOME'] + '/.bashrc'
        _append_to_file(path, '# Drone')
        _append_to_file(path, 'export DRONE_SERVER="' + kwargs['drone_server'] + '"')
        _append_to_file(path, 'export DRONE_TOKEN="' + kwargs['drone_token'] + '"')
    if os.path.isfile(os.environ['HOME'] + '/.zshrc'):
        path = os.environ['HOME'] + '/.zshrc'
        _append_to_file(path, '# Drone')
        _append_to_file(path, 'export DRONE_SERVER="' + kwargs['drone_server'] + '"')
        _append_to_file(path, 'export DRONE_TOKEN="' + kwargs['drone_token'] + '"')
    else:
        path = os.environ['HOME'] + '/.profile'
        _append_to_file(path, '# Drone')
        _append_to_file(path, 'export DRONE_SERVER="' + kwargs['drone_server'] + '"')
        _append_to_file(path, 'export DRONE_TOKEN="' + kwargs['drone_token'] + '"')

def get_repos(**kwargs):
    repos = kwargs['repos'] if 'repos' in kwargs else list()
    print('''
Create Repo
--------------------------------''')
    name = _default_prompt('Name', '')
    if name != '':
        repos.append(name)
        return get_repos(repos=repos)
    return repos

def get_secrets(**kwargs):
    secrets = kwargs['secrets'] if 'secrets' in kwargs else list()
    print('''
Add Secret
--------------------------------''')
    name = _default_prompt('Name', '')
    key = _default_prompt('Key', '')
    if name != '':
        secrets.append({
            'name': name,
            'key': key
        })
        return get_secrets(secrets=secrets)
    return secrets

def add_secrets_to_repos(**kwargs):
    for repo in kwargs['repos']:
        for secret in kwargs['secrets']:
            add_secret(
                repo=repo,
                secret=secret
            )
        print('''
''' + repo + '''
--------------------------------
        ''')
        os.system('drone secret ls ' + repo)

def add_secret(**kwargs):
    secret = kwargs['secret']
    os.system('drone secret add ' + kwargs['repo'] + ' ' + secret['name'] + ' ' + secret['key'])

def cleanup():
    if os.path.isfile(os.environ['HOME'] + '/.zshrc'):
        os.system('exec zsh -l')
    else:
        os.system('exec bash -l')

def _default_prompt(name, fallback):
    message = name + ': '
    if fallback != '':
        message = name + ' (' + fallback + '): '
    response = raw_input(message)
    assert isinstance(response, str)
    if (response):
        return response
    else:
        return fallback

def _append_to_file(path, content):
    with open(path, 'a') as f:
        f.write(content + '\n')

main()


class Solution:
    def equalPairs(self, grid: List[List[int]]) -> int:
        size = len(grid)
        countOfEqualPairs = 0
        # This dictionary stores tuple form of the rows and their occurences
        rowDict = defaultdict(int)
        # This dictionary stores tuple form of the columns and their occurences
        colDict = defaultdict(int)

        for row in grid:
            rowDict[tuple(row)] += 1

        for colNum in range(size):
            column = []

            for rowNum in range(size):
                column.append(grid[rowNum][colNum])

            colDict[tuple(column)] += 1

        # This block checks if a tuple is both a row and column
        for tup in rowDict:
            if tup in colDict:
                # The number of possible pairs is calculated
                countOfEqualPairs += (rowDict[tup] * colDict[tup])

        return countOfEqualPairs


from pathlib import Path

directory = "/Users/mcgurn/Downloads/tmp_13698594"

files = Path(directory).glob('*')
for file in files:
    with open(file) as openFile:

        line = openFile.readlines()[-18]
        if "Event begin: Radiation::RadReturn" not in line:
            print(file)
            print(line)


# hotfix - update!
import urllib.request
from bs4 import BeautifulSoup

link = "https://www.facebook.com/apple"
html_page = urllib.request.urlopen(link)
soup = BeautifulSoup(html_page , "lxml")

for link in soup.findAll('a'):
    a = str(link.get('href'))
    if 'ref_page_id' in a:
        print((a.strip().split("&"))[0].strip().split("=")[1])

print("ok")


def wc(file):
    with open (file,'r') as f:
        # reading the file in a list
        content = f.read().splitlines()
        # atribuindo a qtde de linhas a variavel lines
        lines = len(content)
        # contando as qtde de palavras existentes em cada linha
        words = 0
        for line in content:
            words += len(line.split())
        chars = 0
        for line in content:
            chars += len(list(line))
        return lines, words, chars
print(wc('sample_file.txt'))


#!/usr/bin/env python
from .app import app, db
from .db import Job
from flask import session, abort, request, flash, redirect, render_template, url_for, g, jsonify

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You were logged in', 'success')
            return redirect(url_for('show_jobs'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out', 'info')
    return redirect(url_for('show_jobs'))

@app.route('/jobs/', methods=['POST'])
def create_job():
    if not session.get('logged_in'):
        abort(401)

    job = Job(**request.form.to_dict(flat=True))
    db.session.add(job)
    db.session.commit()

    flash('New job was successfully created', 'success')
    return redirect(url_for('show_jobs'))

@app.route('/jobs/', methods=['GET'])
def show_jobs():
    jobs = Job.query.all()
    return render_template('show_jobs.html', jobs=jobs)

@app.route('/jobs/<int:job_id>', methods=['DELETE'])
def delete_job(job_id):
    try:
        Job.query(id=job_id).delete()
    except:
        return jsonify(False)
    return jsonify(True)

if __name__ == "__main__":
    app.run()


import socket

def send(connection, status_line, headers):
	encoding = "UTF-8"
	lines = [status_line] + headers + [""]
	connection.send("\r\n".join(lines).encode(encoding))

def is_prime(n):
	if n < 2:
		return False
	if n == 2:
		return True
	if n % 2 == 0:
		return False
	i = 3;
	while i * i <= n:
		if n % i == 0:
			return False
		i += 2;
	return True

def send_chunk(text, connection):
		lines = ["", str(len(text)), text]
		try:
			connection.send("\r\n".join(lines).encode(encoding))
		except ConnectionResetError:
			return False
		return True

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('localhost', 8080))
s.listen(1)

status_line = "HTTP/1.1 200 OK"

headers = [
	"Content-Type: text/plain",
	"Transfer-Encoding: chunked",
];

while 1:
	running = True
	encoding = "UTF-8"
	connection, address = s.accept();
	send(connection, status_line, headers)
	text = send_chunk("2\n", connection)
	i = 3;
	while running:
		if is_prime(i):
			running = send_chunk(str(i) + "\n", connection)
		i += 2


import datetime
from typing import Any, Dict, List, Type, TypeVar

import attr
from dateutil.parser import isoparse

T = TypeVar("T", bound="TablePublication")

@attr.s(auto_attribs=True)
class TablePublication:
    """
    Attributes:
        created_at (datetime.datetime):
        export_job_id (int):
        id (int):
    """

    created_at: datetime.datetime
    export_job_id: int
    id: int
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        created_at = self.created_at.isoformat()

        export_job_id = self.export_job_id
        id = self.id

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "createdAt": created_at,
                "exportJobId": export_job_id,
                "id": id,
            }
        )

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        created_at = isoparse(d.pop("createdAt"))

        export_job_id = d.pop("exportJobId")

        id = d.pop("id")

        table_publication = cls(
            created_at=created_at,
            export_job_id=export_job_id,
            id=id,
        )

        table_publication.additional_properties = d
        return table_publication

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


from rest_framework.response import Response
import random, json

from account.api.serializer import (
	StudentDetailSerializer,
	TeacherDetailSerializer,
	StaffDetailSerializer,
	GuardianDetailSerializer,
	PictureSerializer
)
from account.models import (
	Accounts,
	Student,
	Teacher,
	Staff,
	Guardian
)
from classes.models import (
	StudentAcademicInfo,
	ClassYear,
	Groups
)
from classes.api.updateOrCreate import (
	update_StudentAcademicInfo,
	update_TeacherAcademicInfo,
	update_StaffAcademicInfo
)

def getUsername(first_name, last_name):
	User = Accounts

	full_name = (first_name+last_name).replace(' ', '').lower()
	try_number = 0
	if len(full_name)<=12:
		name = full_name
		while User.objects.filter(username=name).exists() and try_number<500:
			name = full_name+str(random.choice(range(9999)))
		if try_number<500:
			return name

	first_name = first_name.replace(' ', '').lower()
	try_number = 0
	if len(first_name)<=12:
		name = first_name
		while User.objects.filter(username=name).exists() and try_number<500:
			name = first_name+str(random.choice(range(9999)))
		if try_number<500:
			return name

	last_name = last_name.replace(' ', '').lower()
	name = last_name
	while User.objects.filter(username=name).exists():
		name = last_name+str(random.choice(range(9999)))
	return name

# 											Student

def update_student(instance, data, context=None):
	if data.get('student_personal_info'):
		if data['student_personal_info'].get('email'):
			if instance.student_personal_info.email == data['student_personal_info']['email']:
				data['student_personal_info'].pop('email')
		if data['student_personal_info'].get('guardian'):
			stu = Student.objects.get(account=instance)
			stu.guardian = Guardian.objects.get(account__username=data['student_personal_info'].pop('guardian'))
			stu.save()
	if data.get('academic_info'):
		academic_info = data.pop('academic_info')
		student_instance = instance.student_personal_info
		try:
			academic_data = update_StudentAcademicInfo(student_instance, academic_info, action='update')
		except:
			return Response({'error': 'academic info error'})

	serializer = StudentDetailSerializer(instance, data=data, context=context, partial=True)
	if serializer.is_valid():
		serializer.save()
		return Response(StudentDetailSerializer(instance).data)
	return Response(serializer.errors)

def create_student(form_data):
	data = json.loads(form_data['data'])
	data['phone_numbers'] = checkNumbers(data.pop('phone_numbers'))
	data.pop('pictures')
	serializer = StudentDetailSerializer(data=data, partial=True)
	if serializer.is_valid():
		serializer.validated_data['username'] = getUsername(data['first_name'], data['last_name'])
		serializer.save()

		student = Accounts.objects.get(username=serializer.validated_data['username'])
		student.student_personal_info.guardian = Guardian.objects.filter(account__username=data['student_personal_info'].get('guardian')).first()
		student.student_personal_info.guardian_relation = data['student_personal_info'].get('guardian_relation')
		student.student_personal_info.save()

		if data.get('academic_info'):
			academic_info = update_StudentAcademicInfo(student.student_personal_info, data.get('academic_info'), action='new')
			serializer.validated_data['academic_info'] = academic_info
		if form_data.get('picture'):	# setting picture
			pic_seri = PictureSerializer(student.pictures, data={'profile': form_data['picture']})
			if pic_seri.is_valid():
				pic_seri.save()
			else:
				print(pic_seri.errors)
		serializer.validated_data['success'] = True
		return Response(serializer.validated_data)
	return Response(serializer.errors)

# 										Teacher

def update_teacher(instance, data):
	if data.get('teacher_personal_info') and data['teacher_personal_info'].get('email', None):
		if instance.teacher_personal_info.email == data['teacher_personal_info']['email']:
			data['teacher_personal_info'].pop('email')
	serializer = TeacherDetailSerializer(instance, data=data, partial=True)
	academic_serializer = update_TeacherAcademicInfo(instance.teacher_personal_info.academic_info, data=data.get('academic_info'))
	if serializer.is_valid() and academic_serializer['is_valid']:
		serializer.save()
		serializer.validated_data['academic_info'] = academic_serializer['validated_data']
		return Response(serializer.validated_data)
	else:
		return Response(serializer.errors) if not serializer.is_valid() else Response(academic_serializer['errors'])

def create_teacher(data):
	serializer = TeacherDetailSerializer(data=data, partial=True)
	if serializer.is_valid():
		serializer.validated_data['username'] = getUsername(data['first_name'], data['last_name'])
		serializer.save()
		if data.get('academic_info'):
			academic_info = update_TeacherAcademicInfo(Teacher.objects.get(account__username=serializer.validated_data['username']), data.get('academic_info'))
			if academic_info['is_valid']:
				serializer.validated_data['academic_info'] = academic_info['validated_data']
			else:
				serializer.validated_data['academic_info'] = academic_info['errors']
		return Response(serializer.validated_data)
	return Response(serializer.errors)

# 										Staff

def update_staff(instance, data):
	if data.get('staff_personal_info') and data['staff_personal_info'].get('email', None):
		if instance.staff_personal_info.email == data['staff_personal_info']['email']:
			data['staff_personal_info'].pop('email')
	serializer = StaffDetailSerializer(instance, data=data, partial=True)
	academic_serializer = update_StaffAcademicInfo(instance.staff_personal_info.academic_info, data=data.get('academic_info'))
	if serializer.is_valid() and academic_serializer['is_valid']:
		serializer.save()
		serializer.validated_data['academic_info'] = academic_serializer['validated_data']
		return Response(serializer.validated_data)
	else:
		return Response(serializer.errors) if not serializer.is_valid() else Response(academic_serializer['errors'])

def create_staff(data):
	serializer = StaffDetailSerializer(data=data, partial=True)
	if serializer.is_valid():
		serializer.validated_data['username'] = getUsername(data['first_name'], data['last_name'])
		serializer.save()
		if data.get('academic_info'):
			academic_info = update_StaffAcademicInfo(Staff.objects.get(account__username=serializer.validated_data['username']), data.get('academic_info'))
			serializer.validated_data['academic_info'] = academic_info['validated_data'] if academic_info['is_valid'] else academic_info['errors']
		return Response(serializer.validated_data)
	return Response(serializer.errors)

# 										guardian

def update_guardian(instance, data):
	if data.get('guardian_personal_info') and data['guardian_personal_info'].get('email', None):
		if instance.guardian_personal_info.email == data['guardian_personal_info']['email']:
			data['guardian_personal_info'].pop('email')
	serializer = GuardianDetailSerializer(instance, data=data, partial=True)
	if serializer.is_valid():
		serializer.save()
		return Response(serializer.validated_data)
	return Response(serializer.errors)

def create_guardian(data):
	data['phone_numbers'] = checkNumbers(data.pop('phone_numbers'))
	serializer = GuardianDetailSerializer(data=data, partial=True)
	if serializer.is_valid():
		serializer.validated_data['username'] = getUsername(data['first_name'], data['last_name'])
		serializer.save()
		serializer.validated_data['success'] = True
		return Response(serializer.validated_data)
	return Response(serializer.errors)

def checkNumbers(number_list):
	for i, number in enumerate(number_list):
		if number['number'] == None:
			del number_list[i]
	return number_list


from os import listdir, remove

from discord.ext import commands, tasks

from utils import basic
from utils.decos import handle_errors

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

class loops(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.cache_cleanup.start()

    def cog_unload(self):
        self.cache_cleanup.cancel()

    @tasks.loop(seconds=60)
    @handle_errors
    async def cache_cleanup(self):
        cache_size = basic.get_size("cache")
        if cache_size >= 2000000000:
            cache_folder = listdir("cache")
            cache_folder.sort(reverse=False, key=lambda x: int(''.join(filter(str.isdigit, x))))
            cache_folder = cache_folder[:1000]

            for file in cache_folder:
                remove(f"cache/{file}")

            await self.bot.cache.bulk_remove(int(cache_file[:-8]) for cache_file in cache_folder)

    @cache_cleanup.before_loop
    async def before_loops(self):
        await self.bot.wait_until_ready()


# -*- coding: utf-8 -*-
"""Setup info for building OpenPype 3.0."""
import os
import re
import platform
import distutils.spawn
from pathlib import Path

from cx_Freeze import setup, Executable
from sphinx.setup_command import BuildDoc

openpype_root = Path(os.path.dirname(__file__))

def validate_thirdparty_binaries():
    """Check existence of thirdpart executables."""
    low_platform = platform.system().lower()
    binary_vendors_dir = os.path.join(
        openpype_root,
        "vendor",
        "bin"
    )

    error_msg = (
        "Missing binary dependency {}. Please fetch thirdparty dependencies."
    )
    # Validate existence of FFmpeg
    ffmpeg_dir = os.path.join(binary_vendors_dir, "ffmpeg", low_platform)
    if low_platform == "windows":
        ffmpeg_dir = os.path.join(ffmpeg_dir, "bin")
    ffmpeg_executable = os.path.join(ffmpeg_dir, "ffmpeg")
    ffmpeg_result = distutils.spawn.find_executable(ffmpeg_executable)
    if ffmpeg_result is None:
        raise RuntimeError(error_msg.format("FFmpeg"))

    # Validate existence of OpenImageIO (not on MacOs)
    oiio_tool_path = None
    if low_platform == "linux":
        oiio_tool_path = os.path.join(
            binary_vendors_dir,
            "oiio",
            low_platform,
            "bin",
            "oiiotool"
        )
    elif low_platform == "windows":
        oiio_tool_path = os.path.join(
            binary_vendors_dir,
            "oiio",
            low_platform,
            "oiiotool"
        )
    oiio_result = None
    if oiio_tool_path is not None:
        oiio_result = distutils.spawn.find_executable(oiio_tool_path)
        if oiio_result is None:
            raise RuntimeError(error_msg.format("OpenImageIO"))

# Give ability to skip vaidation
if not os.getenv("SKIP_THIRD_PARTY_VALIDATION"):
    validate_thirdparty_binaries()

version = {}

with open(openpype_root / "openpype" / "version.py") as fp:
    exec(fp.read(), version)

version_match = re.search(r"(\d+\.\d+.\d+).*", version["__version__"])
__version__ = version_match.group(1)

low_platform_name = platform.system().lower()
IS_WINDOWS = low_platform_name == "windows"
IS_LINUX = low_platform_name == "linux"
IS_MACOS = low_platform_name == "darwin"

base = None
if IS_WINDOWS:
    base = "Win32GUI"

# -----------------------------------------------------------------------
# build_exe
# Build options for cx_Freeze. Manually add/exclude packages and binaries

install_requires = [
    "appdirs",
    "cx_Freeze",
    "keyring",
    "clique",
    "jsonschema",
    "pathlib2",
    "pkg_resources",
    "PIL",
    "pymongo",
    "pynput",
    "jinxed",
    "blessed",
    "Qt",
    "qtpy",
    "speedcopy",
    "googleapiclient",
    "httplib2",
    # Harmony implementation
    "filecmp",
    "dns",
    # Python defaults (cx_Freeze skip them by default)
    "dbm",
    "sqlite3",
    "dataclasses",
    "timeit"
]

includes = []
# WARNING: As of cx_freeze there is a bug?
# when this is empty, its hooks will not kick in
# and won't clean platform irrelevant modules
# like dbm mentioned above.
excludes = [
    "openpype"
]
bin_includes = [
    "vendor"
]
include_files = [
    "igniter",
    "openpype",
    "LICENSE",
    "README.md"
]

if IS_WINDOWS:
    install_requires.extend([
        # `pywin32` packages
        "win32ctypes",
        "win32comext",
        "pythoncom"
    ])

icon_path = openpype_root / "igniter" / "openpype.ico"
mac_icon_path = openpype_root / "igniter" / "openpype.icns"

build_exe_options = dict(
    packages=install_requires,
    includes=includes,
    excludes=excludes,
    bin_includes=bin_includes,
    include_files=include_files,
    optimize=0
)

bdist_mac_options = dict(
    bundle_name=f"OpenPype {__version__}",
    iconfile=mac_icon_path
)

executables = [
    Executable(
        "start.py",
        base=base,
        target_name="openpype_gui",
        icon=icon_path.as_posix()
    ),
    Executable(
        "start.py",
        base=None,
        target_name="openpype_console",
        icon=icon_path.as_posix()
    ),
]

if IS_LINUX:
    executables.append(
        Executable(
            "app_launcher.py",
            base=None,
            target_name="app_launcher",
            icon=icon_path.as_posix()
        )
    )

setup(
    name="OpenPype",
    version=__version__,
    description="OpenPype",
    cmdclass={"build_sphinx": BuildDoc},
    options={
        "build_exe": build_exe_options,
        "bdist_mac": bdist_mac_options,
        "build_sphinx": {
            "project": "OpenPype",
            "version": __version__,
            "release": __version__,
            "source_dir": (openpype_root / "docs" / "source").as_posix(),
            "build_dir": (openpype_root / "docs" / "build").as_posix()
        }
    },
    executables=executables,
    packages=[]
)


#!/usr/bin/python3
# -*- encoding="UTF-8" -*-

import sys
sys.path.append("../..")

from Models.Diode.DiodeClass import Diode

from Functions.addNode import addNode
from Functions.string2num import string2num

import parameters

def parseDiode(stringDiode = ''):
	DiodeList = stringDiode.split()
	if len(DiodeList) > 6:
		print("****Error! More parameters than EXPECTED!")
	elif len(DiodeList) < 6:
		print("****Error! Less parameters than EXPECTED!")
	else:
		DiodeTemp = Diode()
		DiodeTemp.name = DiodeList[0]
		DiodeTemp.port1 = DiodeList[1]
		DiodeTemp.port2 = DiodeList[2]
		DiodeTemp.Model = DiodeList[3]
		ITemp = 4
		while ITemp < len(DiodeList):
			if '=' in DiodeList[ITemp]:
				DiodeValTemp = DiodeList[ITemp].split('=')
				if DiodeValTemp[0] == 'is':
					DiodeTemp.Isat = DiodeValTemp[1]
				elif DiodeValTemp[0] == 'alpha':
					DiodeTemp.Alpha = DiodeValTemp[1]
				else :
					print("****Error! Unknow parameter NAME",DiodeValTemp[0])
				ITemp = ITemp + 1
			else:
				print("****Error! Unknow parameter NAME",DiodeList[ITemp])
				exit()

		parameters.listD.append(DiodeTemp)
		addNode(DiodeTemp.port1)
		addNode(DiodeTemp.port2)
		return DiodeTemp


class Solution(object):
    def findSubstring(self, s, words):
        if len(words) == 0:
            return []

        rv = []
        word_length = len(words[0])
        assert word_length > 0
        word_dict = {}  # word -> available_count
        for w in words:
            if w in word_dict:
                word_dict[w] += 1
            else:
                word_dict[w] = 1

        def recover_count(divided, start, times):
            for i in range(start, start+times):
                word_dict[divided[i]] += 1

        # import pytest; pytest.set_trace()

        for offset in range(word_length):
            divided = self._divide(s, offset, word_length)
            cursor = 0
            while cursor < len(divided):
                start = cursor
                word_count = 0
                while cursor < len(divided):
                    current_word = divided[cursor]
                    cursor += 1

                    current_word_count = word_dict.get(current_word)
                    if current_word_count is None:  # Discard the whole sequence
                        break
                    elif current_word_count > 0:  # Success
                        word_dict[current_word] -= 1
                        word_count += 1
                    else:
                        # assert current_word_count == 0
                        # Move forward `start` until we pass `current_word`.
                        # This is like borrow an available count from the first
                        # `current_word` after original `start`.
                        while True:
                            temp_word = divided[start]
                            if temp_word == current_word:
                                break
                            start += 1
                            word_count -= 1
                            word_dict[temp_word] += 1
                        # Move `start` to pass `current_word`. However, do not
                        # need to adjust `word_dict[current_word]`.
                        start += 1

                    # assert word_count <= len(words)
                    if word_count == len(words):  # Find a concatenation
                        rv.append(start*word_length+offset)
                        # Move forward start, to prepare for finding next one
                        recover_count(divided, start, 1)
                        start += 1
                        word_count -= 1
                recover_count(divided, start, word_count)

        return rv

    @staticmethod
    def _divide(string, offset, word_length):
        rv = []
        s = offset
        e = s + word_length
        while e <= len(string):
            rv.append(string[s:e])
            s = e
            e += word_length
        return rv

def test():
    f = Solution().findSubstring

    #         012345678901234567890123456
    assert f('abcabcdfabeabcababcd', list('abc')) == [0,1,2,3,11,12,13,16]
    assert f('abcabcdfabacabadbcaabdcbaad', list('aabc')) == [0,8,9,10,11,16,17,22]
    assert f('foobarfooxfoofoobarbar', ['foo', 'bar']) == [0,3,13]
    assert f('wordgoodgoodgoodbestword', ['word','good','best','good']) == [8]
    assert sorted(f('aaaaaaaa', ['aa', 'aa', 'aa'])) == [0, 1, 2]


import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as wav
import wave
import sounddevice as sd
import glob
import os.path

myPath = 'C:/python/kkmmoouu/fft/wavsamples/realuse/roop2/why'
myExt = '*.wav'
freqframe = []
frqs = [70, 104, 150, 3700, 3260, 6813]

for filename in glob.glob(os.path.join(myPath, myExt)):
    rate, data0 = wav.read(filename)
    data1 = data0
    original = wave.open(filename, 'rb')
    nChannel = original.getnchannels()
    length = original.getnframes()
    original.close()

    if nChannel == 2:
        data2 = data0[:, 0]
        spectre = np.fft.fft(data2)
        freq = np.fft.fftfreq(data2.size, 1/rate)
        mask = 0 < freq
    else:
        spectre = np.fft.fft(data0)
        freq = np.fft.fftfreq(data0.size, 1 / rate)
        mask = 0 < freq

    maxamp = np.abs(np.max(spectre[mask]))

    #보통 500만대고 317만이 최소다 250만 ㄱㄱ
    #sliding window 써보자
    # sliding window
     #avgspectre = []
     #slicerange = 300
     #for i in range(0, np.alen(freq)-(slicerange*2), slicerange):
     #    j = i
     #    spectresum = 0
     #    for j in range(j, j+((slicerange*2)-1), 1):
     #        spectresum = spectresum + np.abs(int(spectre[j]))
     #
     #    avgspectre.append(spectresum/200)
     #
     #for i in range(0, np.alen(freq) - (slicerange*2), slicerange):
     #    j = i
     #    for j in range(j, j+((slicerange*2)-1), 1):
     #        spectre[j] = avgspectre[int(i/slicerange)]

    masks = freq[mask]
    number = 0
    frews = []

    for mask in masks:

        number = number + 1
        if np.abs(spectre[number]) == maxamp:
            maxfreq = round(freq[number], 2)

        if freq[number] < 2000:
            if spectre[number] < 3000000:
                spectre[number] = 0
                freq[number] = 0
        else:
            if spectre[number] < 450000:
                spectre[number] = 0
                freq[number] = 0
        spectre[number] = 0

        for num in range(0, 3):
            if freq[number] > 0:
                if abs(abs(frqs[num]) - freq[number]) < 7:
                    frews.append(frqs[num])

        for num in range(3, 6):
            if freq[number] > 0:
                if abs(abs(frqs[num]) - freq[number]) < 20:
                    frews.append(frqs[num])

    plt.title(filename + " " + np.str(maxfreq) + " hz")
    f = set(frews)
    print(f)

   # plt.title(filename + " " + np.str(maxfreq) + " hz, amp = " + np.str(maxamp))
    #freqframe.append((maxfreq))

    plt.plot(freq, np.abs(spectre), '.')
    plt.xlabel('freq')
    plt.ylabel('amp')
    plt.xlim(0, 10000)
    plt.ylim(0)
    plt.grid()
    plt.show()


__author__ = "Emil Eldstål"
__copyright__ = "Copyright 2023, Emil Eldstål"
__version__ = "0.1.1"

from PySide2 import QtWidgets
from PySide2.QtCore import Qt

import substance_painter.ui
import substance_painter.event

import os
import configparser
import subprocess

def config_ini(overwrite):
    # Get the path to the script's directory
    script_dir = os.path.dirname(os.path.abspath(__file__))

    # Define the path to the StarfieldPluginSettings.ini file
    ini_file_path = os.path.join(script_dir, "Starfield-DDS-Exporter-PluginSettings.ini")

    # Create a ConfigParser object
    config = configparser.ConfigParser()

    # Check if the INI file exists
    if os.path.exists(ini_file_path):
        # Read the INI file
        config.read(ini_file_path)

        # Check if the section and key exist
        if 'General' in config and 'TexConvDirectory' in config['General']:
            # Check if the value is empty
            if not config['General']['TexConvDirectory']:
                # Let's the user choose where TexConv is if not configured
                config['General']['TexConvDirectory'] = choose_texconv_folder()
            if overwrite:
                # Let's the user choose where TexConv is if using overwrite button
                config['General']['TexConvDirectory'] = choose_texconv_folder()

            # Assign the TexConvDirectory value to the TexConvPath variable
            TexConvPath = config['General']['TexConvDirectory']
        else:
            TexConvPath = choose_texconv_folder()
            # If the section or key doesn't exist, create it and set the value
            config['General'] = {}
            config['General']['TexConvDirectory'] = TexConvPath
            print("Starfield DDS Exporter Plugin: TexConvDirectory value set or updated in StarfieldPluginSettings.ini")

        # Write the updated configuration back to the INI file
        with open(ini_file_path, 'w') as configfile:
            config.write(configfile)
    else:
        TexConvPath = choose_texconv_folder()
        # If the INI file doesn't exist, create it and set the value
        with open(ini_file_path, 'w') as configfile:
            config['General'] = {}
            config['General']['TexConvDirectory'] = TexConvPath
            config.write(configfile)

    return TexConvPath

def choose_texconv_folder():
    path = QtWidgets.QFileDialog.getExistingDirectory(
    substance_painter.ui.get_main_window(),"Choose Texconv directory")
    return path +"/texconv.exe"

def convert_png_to_dds(texconvPath, sourcePNG, overwrite):
    # Replace backslashes with forward slashes in the provided paths
    texconvPath = texconvPath.replace('\\', '/')
    sourceFolder = os.path.dirname(sourcePNG)
    sourceFolder = sourceFolder.replace('\\', '/')
    outputFolder = sourceFolder + "/DDS/"

    isExist = os.path.exists(outputFolder)
    if not isExist:
        # Create the DDS directory if it does not exist
        os.makedirs(outputFolder)
        print("Created DDS subfolder")

    # for filename in os.listdir(sourceFolder):
    filename = sourcePNG
    if filename.endswith(".png"):
        sourceFile = os.path.splitext(filename)[0]
        suffix = sourceFile.split('_')[-1]
        suffix = suffix.rstrip('_')

        outputFile = sourceFile + ".dds"

        if suffix in ["metal", "rough", "transmissive", "emissive", "ao", "opacity", "height", "mask"]:
            format_option = "BC4_UNORM"
        elif suffix == "normal":
            format_option = "BC5_SNORM"
        elif suffix == "color":
            format_option = "BC7_UNORM"
        # If for some reason it's using some other suffix that's not supported
        else:
            format_option = "BC1_UNORM"

        format_option = format_option.rstrip('"')
        if overwrite:
            overwrite_option = "-y"
        else:
            overwrite_option = ""

        if outputFile:
            texconv_cmd = [
                texconvPath,
                "-nologo", overwrite_option,
                "-o", outputFolder,
                "-f", format_option,
                os.path.join(sourceFolder, filename)
            ]
            texconv_cmd_str = subprocess.list2cmdline(texconv_cmd)

            try:
                subprocess.run(texconv_cmd_str, shell=True, check=True)
                print(f"Successfully converted {filename} to {outputFile}")
            except subprocess.CalledProcessError:
                print(f"Failed to convert {filename}")

class StarfieldDDSPlugin:
    def __init__(self):
        # Export boolean whether to add DDS creation or not
        self.export = True
        # Overwrites existing DDS files if checked
        self.overwrite = True
        # Plugin Version
        self.version = "0.1.1"

        # Create a dock widget to report plugin activity.
        self.log = QtWidgets.QTextEdit()
        self.window = QtWidgets.QWidget()
        self.TexConvPath = config_ini(False)

        layout = QtWidgets.QVBoxLayout()
        sub_layout = QtWidgets.QHBoxLayout()

        checkbox = QtWidgets.QCheckBox("Export DDS files")
        checkbox.setChecked(True)
        checkbox_overwrite = QtWidgets.QCheckBox("Overwrite DDS files")
        checkbox_overwrite.setChecked(True)
        button_texconv = QtWidgets.QPushButton("Choose Texconv location")
        button_clear = QtWidgets.QPushButton("Clear Log")

        version_label = QtWidgets.QLabel("Version: {}".format(self.version))

        # Adds buttons to sub-layout
        sub_layout.addWidget(checkbox)
        sub_layout.addWidget(checkbox_overwrite)
        sub_layout.addWidget(button_texconv)
        sub_layout.addWidget(button_clear)

        # Adds all widgets to main layout
        layout.addLayout(sub_layout)
        layout.addWidget(self.log)
        layout.addWidget(version_label)

        self.window.setLayout(layout)
        self.window.setWindowTitle("Starfield DDS Auto Converter")

        self.log.setReadOnly(True)

        # Connects buttons to click events
        checkbox.stateChanged.connect(self.checkbox_export_change)
        checkbox_overwrite.stateChanged.connect(self.checkbox_overwrite_change)
        button_texconv.clicked.connect(self.button_texconv_clicked)
        button_clear.clicked.connect(self.button_clear_clicked)

        # Adds Qt as dockable widget to Substance Painter
        substance_painter.ui.add_dock_widget(self.window)

        self.log.append("TexConv Path: {}".format(self.TexConvPath))

        connections = {
            substance_painter.event.ExportTexturesEnded: self.on_export_finished
        }
        for event, callback in connections.items():
            substance_painter.event.DISPATCHER.connect(event, callback)

    def button_texconv_clicked(self):
        self.TexConvPath = config_ini(True)
        self.log.append("New TexConv Path: {}".format(self.TexConvPath))

    def button_clear_clicked(self):
        self.log.clear()

    def checkbox_export_change(self,state):
        if state == Qt.Checked:
            self.export = True
        else:
            self.export = False

    def checkbox_overwrite_change(self,state):
        if state == Qt.Checked:
            self.overwrite = True
        else:
            self.overwrite = False

    def __del__(self):
        # Remove all added UI elements.
        substance_painter.ui.delete_ui_element(self.log)
        substance_painter.ui.delete_ui_element(self.window)

    def on_export_finished(self, res):
        if(self.export):
            self.log.append(res.message)
            self.log.append("Exported files:")
            for file_list in res.textures.values():
                for file_path in file_list:
                    self.log.append("  {}".format(file_path))

            self.log.append("Converting to DDS files:")
            for file_list in res.textures.values():
                for file_path in file_list:
                    convert_png_to_dds(self.TexConvPath,file_path,self.overwrite)
                    file_path = file_path[:-3]+"DDS"
                    self.log.append("  {}".format(file_path))

    def on_export_error(self, err):
        self.log.append("Export failed.")
        self.log.append(repr(err))

STARFIELD_DDS_PLUGIN = None

def start_plugin():
    """This method is called when the plugin is started."""
    print ("Starfield DDS Exporter Plugin Initialized")
    global STARFIELD_DDS_PLUGIN
    STARFIELD_DDS_PLUGIN = StarfieldDDSPlugin()

def close_plugin():
    """This method is called when the plugin is stopped."""
    print ("Starfield DDS Exporter Plugin Shutdown")
    global STARFIELD_DDS_PLUGIN
    del STARFIELD_DDS_PLUGIN

if __name__ == "__main__":
    start_plugin()


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

@Author : kazi
@File : subsequences.py
@Time : 3/27/22 3:55 PM
@Desc:
"""

import itertools as it

def sub_sequences(string):
    """
    "Print all sub sequence of a string using itertools.
    Args:
        string:

    Returns:

    """
    result = []
    for i in range(1, len(string) + 1):
        result.append(list(it.combinations(string, i)))
    for c in result:
        for t in c:
            print(''.join(t), end=' ')

'''
s = 'ab'
possible subsequence = a, b, c, ab, ac, bc, abc
'''

def main():
    """
    main function to drive code
    Returns:

    """
    sub_sequences('abc')

if __name__ == "__main__":
    main()


#!/usr/bin/python

import os, argparse

supportArchs = [
  "x86_64",
  #"aarch64"
]

rootdir = ""
selectedArch = ""

rebuildFlag = False;

def mkdir(dirName : str) :
  os.system("mkdir -p " + rootdir + dirName)
  print("创建了文件夹[" + rootdir + dirName + "]")

def cp(src : str, dest : str) :
  ret = os.system("cp -f " + src + " " + rootdir + dest)
  if (ret != 0) :
    print("复制系统文件[" + rootdir + dest + "]失败,返回值[{0}]".format(ret))
    exit();
  print("复制系统文件[" + rootdir + dest + "]完成")

dependcies = [
  "build/kernel.sys",
  "boot/build/boot.efi",
  ]

dependciesDestinations = [
  "/EXOS/kernel.sys",
  "placeholder__BOOTLOADER",
  ]

def checkDependcies() -> bool :
  if rebuildFlag == True:
    return False
  print("检查依赖中...")
  for depFile in dependcies:
    if (not os.path.isfile(depFile)) :
      return False

  return True

if (len(dependcies) != len(dependciesDestinations)) :
  print("安装脚本依赖列表与目的地列表不对等!请检查依赖列表与目的地列表")

argparser = argparse.ArgumentParser(description="EXOS安装脚本")
argparser.add_argument("path", type=str, help="安装目录")
argparser.add_argument("--rebuild", action="store_true", help="强制重新构建", default=False)
argparser.add_argument("--arch", type=str, help="目标机器架构", default="x86_64")
args = argparser.parse_args()

rebuildFlag = args.rebuild
rootdir = args.path
selectedArch = args.arch

if selectedArch not in supportArchs:
  print("不支持的架构: " + selectedArch)
  exit(-1)

dependciesDestinations[dependciesDestinations.index("placeholder__BOOTLOADER")] = "/efi/boot/boot" + ["x64", "aa64"][supportArchs.index(selectedArch)] + ".efi"

if (not os.access(rootdir, os.W_OK))  :
  os.system("mkdir -p " + rootdir)
  if (not os.access(rootdir, os.W_OK))  :
    print("没有目标文件夹的写入权限!请尝试使用sudo再试一次")
    exit()

if (not checkDependcies()):
  if (os.system("cd boot;python ./build.py --arch " + selectedArch) != 0) :
    print("构建引导出错!!!")
    exit()
  print("引导构建完成")
  if (os.system("python ./build.py --arch " + selectedArch) != 0) :
    print("构建内核出错!!!")
    exit()
  print("内核构建完成")

mkdir("/EXOS")
mkdir("/efi/boot/")
mkdir("/efi/EXOS/")

for i in range(0, len(dependciesDestinations))  :
  cp(dependcies[i], dependciesDestinations[i])

print("安装完成")


from core.views.View import View
from core.views.GameModeView import GameModeView
from core.views.SettingsView import SettingsView

class MainMenuView(View):

    def show(self) -> None:
        self.clearWindow()
        print("Welcome to tic tac toe")
        print("----------------------")
        print("1. Choose Game Mode")
        print("2. Settings")
        print("3. Exit")
        print()

    def processInput(self) -> None:
        userInput = input()

        if userInput == '1':
            gameModeView = GameModeView()
            return self.__init__()
        elif userInput == '2':
            settingsView = SettingsView()
            return self.__init__()
        elif userInput == '3':
            return self.finish()
        else:
            print("Invalid input")
            return self.processInput()

    def finish(self) -> None:
        return


# -*- coding: utf-8 -*-

# link to the api http://api.population.io/

import requests
import json
from datetime import datetime

class CountryList:
    "Returns a json list with all available countries the population-api takes as valid input"

    url = "http://api.population.io:80/1.0/countries"

    def get(self):
        ans = getrequest(self.url)
        return ans['countries']

class WPrank:
    "Determine world population rank. Returns a json object"

    def __init__(self, dob, sex, country):
        self.dob = dob
        self.sex = sex
        self.country = makehtmlformat(country)
        self.url = "http://api.population.io:80/1.0/wp-rank/{0}/{1}/{2}/".format(
            self.dob, self.sex, self.country)

    def today(self):
        """
        Calculates the world population rank of a person with the given date of birth, sex and country of origin as of today.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.\n
        Today's date is always based on the current time in the timezone UTC.
        """
        url = self.url + "today"
        ans = getrequest(url)
        return ans["rank"]

    def on(self, date):
        """
        Calculates the world population rank of a person with the given date of birth, sex and country of origin on a certain date.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.
        """
        url = self.url + "on/{0}".format(date)
        ans = getrequest(url)
        return ans["rank"]

    def aged(self, age):
        """
        Calculates the world population rank of a person with the given date of birth, sex and country of origin on a certain date as expressed by the person's age.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.
        """
        url = self.url + "aged/{0}".format(age)
        ans = getrequest(url)
        return ans["rank"]

    def ago(self, ago):
        """
        Calculates the world population rank of a person with the given date of birth, sex and country of origin on a certain date as expressed by an offset towards the past from today.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.\n
        Today's date is always based on the current time in the timezone UTC.
        """
        url = self.url + "ago/{0}".format(ago)
        ans = getrequest(url)
        return ans["rank"]

    def inn(self, inn):
        """
        Calculates the world population rank of a person with the given date of birth, sex and country of origin on a certain date as expressed by an offset towards the past from today.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.\n
        Today's date is always based on the current time in the timezone UTC.
        """
        url = self.url + "in/{0}".format(inn)
        ans = getrequest(url)
        return ans["rank"]

    def rank(self, rank):
        """
        Calculates the day on which a person with the given date of birth, sex and country of origin has reached (or will reach) a certain world population rank.\n
        The world population rank is defined as the position of someone's birthday among the group of living people of the same sex and country of origin, ordered by date of birth decreasing.
        The last person born is assigned rank #1.\n
        Please be carefull with very small and big ranks. You´ll maybe encounter errors.
        """
        url = self.url + "ranked/{0}".format(rank)
        ans = getrequest(url)
        return ans["date_on_rank"]

class LifeExpectancy:
    "Calculate life expectancy"

    def __init__(self, sex, country):
        self.sex = sex
        self.country = makehtmlformat(country)
        self.url = "http://api.population.io:80/1.0/life-expectancy/"

    def remaining(self, date, age):
        "Calculate remaining life expectancy of a person with given sex, country, and age at a given point in time."
        url = self.url + \
            "remaining/{0}/{1}/{2}/{3}".format(self.sex,
                                               self.country, date, age)
        ans = getrequest(url)
        return ans["remaining_life_expectancy"]

    def total(self, dob):
        """
        Calculate total life expectancy of a person with given sex, country, and date of birth.\n
        Note that this function is implemented based on the remaining life expectancy by picking a reference date based on an age of 35 years.
        It is therefore of limited accuracy.
        """
        url = self.url + \
            "total/{0}/{1}/{2}/".format(self.sex, self.country, dob)
        ans = getrequest(url)
        return ans["total_life_expectancy"]

    def diabetes(self, age, hasD="yes"):
        """
        Calculate diabetes life expectancy of a person with given sex, country, age and whether has diabetes or not.\n
        Yes is default if not specified exactly (yes/no).
        """
        url = self.url + \
            "diabetes/{0}/{1}/{2}/{3}/".format(self.sex,
                                               self.country, age, hasD)
        ans = getrequest(url)
        return ans["total_life_expectancy"]

class Population:
    "Retrieve population tables"

    def __init__(self, country):
        self.country = makehtmlformat(country)
        self.url = "http://api.population.io:80/1.0/population/"

    def population(self, year, age, modus):
        """
        Retrieve population table for specific age group in the given year.\n
        Modus 1 == return total population.\n
        Modus 2 == return male population.\n
        Modus 3 == return female population.
        """
        url = self.url + "{0}/aged/{1}/".format(year, age)
        ans = getrequest(url)
        i = 0
        for name in ans:
            if name['country'] == self.country:
                ans = ans[i]
                break
            i += 1

        if modus == 1:
            return ans["total"]
        elif modus == 2:
            return ans["males"]
        elif modus == 3:
            return ans["females"]

    def bydate(self, date):
        "Determines total population for a given country on a given date. Valid dates are 2013-01-01 to 2022-12-31."

        url = self.url + "{0}/{1}/".format(self.country, date)
        ans = getrequest(url)
        return ans["total_population"]["population"]

    def today(self):
        "Determines total population for a given country for today"

        url = self.url + "{0}/today-and-tomorrow".format(self.country)
        ans = getrequest(url)
        return ans["total_population"][0]["population"]

    def tomorrow(self):
        "Determines total population for a given country for tomorrow"

        url = self.url + "{0}/today-and-tomorrow".format(self.country)
        ans = getrequest(url)
        return ans["total_population"][1]["population"]

class MortalityDistribution:
    "Retrieve mortality distribution tables"

    def __init__(self, sex, country, age):
        self.age = age
        self.sex = sex
        self.country = makehtmlformat(country)
        self.url = "http://api.population.io:80/1.0/mortality-distribution/"

    def byage(self):
        url = self.url + \
            "{0}/{1}/{2}/today/".format(self.country, self.sex, self.age)
        ans = getrequest(url)
        return ans['mortality_distribution']

    def diabetes(self, hasD="unknown"):
        url = self.url + \
            "diabetes/{0}/{1}/{2}/{3}/".format(hasD,
                                               self.country, self.sex, self.age)
        ans = getrequest(url)
        return ans['mortality_distribution']

# helper function to change spaces to %20 for the url calls

def makehtmlformat(txt):
    txt = txt.replace(" ", "%20")
    return txt

# helper function to manage the requests and checks for validity

def getrequest(url):
    try:
        r = requests.get(url)
        ans = json.loads(r.content.decode('UTF-8'))
    except:
        exit("Some critical errors were encountered in url:\n{0}\nBody:\n{1}".format(
            url, r.content))
    if not 200 == r.status_code:
        return exit(ans["detail"])

    return ans


import matplotlib.pyplot as plt
import numpy as np

from nerdart import Parser
from nerdart.utils import save_or_show

@save_or_show(__file__)
def plot(args):
    n_rectangles = args.n_rectangles

    x = np.random.random(n_rectangles)
    cm = plt.cm.autumn(np.linspace(0, 1, n_rectangles))
    ax = plt.subplot()

    ax.bar(x, np.tan(x), alpha=0.25, color=cm, edgecolor="k")

    ax.axis("off")

def main():
    parser = Parser()
    parser.add("-n", "--n-rectangles", type=int, default=100)
    args = parser.parse()
    plot(args)

if __name__ == "__main__":
    main()