#!/usr/bin/python3
from math import sin, cos, radians
from collections import namedtuple, defaultdict
from functools import reduce
from itertools import permutations, combinations

lines = open("day14.dat").read().splitlines()

#registers for part 1 and 2
reg1 = {}
reg2 = {}

for line in lines:
    if line.count("mask = ") == 1:
        mask = line.split("mask = ")[1]
        # and mask (to set to 0)
        a = 0
        # or mask (to set to 1)
        o = 0
        # x mask for part 2
        mx = []

        val = 1
        bitcount = 0
        for m in mask[::-1]:
            if (m == "X"):
                mx.append(bitcount)
            if (m != "0"):
                a += val
            if (m == "1"):
                o += val

            val *= 2
            bitcount += 1
    else:
        pos = int(line.split("mem[")[1].split("]")[0])
        val = int(line.split(" = ")[1])

        # write part 1
        reg1[pos] = (val | o) & a

        # basis pos for part 2
        pos = pos | o & a

        # calculate all positions for part 2
        positions = []
        for bit in mx:
            np = set()
            for op in positions:
                np.add(op | (1<<(bit)))
                np.add(op & ~(1<<(bit)))

            np.add(pos | (1<<(bit)))
            np.add(pos & ~(1<<(bit)))

            positions = np

        # write positions for part 2
        for op in positions:
            reg2[op] = val

ans1 = 0
for r in reg1.keys():
    ans1 += reg1[r]

print("Part1:", ans1)

ans2 = 0
for r in reg2.keys():
    ans2 += reg2[r]

print("Part2:", ans2)


import csv
import re

from application.contact import Contact
from application.phone_book import PhoneBook

IDENTITY_DATA = re.compile(
    r'^(?P<lastname>.*?)\W+(?P<firstname>.*?)\W+(?P<surname>.*?)\W+(?P<organization>.*?)\W+(?P<position>.*?),(?P<phone>.*?),(?P<email>.*?)$')

def input_data():
    phone_book = PhoneBook()
    with open("phonebook_raw.csv", mode='r', encoding='utf8') as ff:
        rows = csv.reader(ff, delimiter="\n")
        contacts_list = list(rows)

        for contact in contacts_list:
            person = Contact(IDENTITY_DATA.findall(contact[0])[0])
            phone_book.add_contact(contact=person)
    return phone_book

def write_data(contacts_list):
    with open("phonebook.csv", mode="w", encoding='utf8') as fe:
        writer = csv.writer(fe, delimiter=',')
        writer.writerows(contacts_list)

if __name__ == '__main__':
    phone_book = input_data()
    contacts_list = phone_book.get_contacts_list()
    write_data(contacts_list=contacts_list)


import datetime
from operator import itemgetter

from django.core.management.base import BaseCommand, CommandError
from django.db.models import *

from buckley.models import *

def cache_totals():
    totals = Total.objects.all()
    if totals:
        total = totals[0]
    else:
        total = Total()
    ie_total = Expenditure.objects.filter(candidate__cycle=2010, electioneering_communication=False).aggregate(total=Sum('expenditure_amount'))['total']
    total.ie_total = ie_total
    ec_total = Expenditure.objects.filter(electioneering_communication=True).aggregate(total=Sum('expenditure_amount'))['total']
    total.ec_total = ec_total
    total.total = ie_total or 0 + ec_total or 0

    by_party = sorted(list(Expenditure.objects.exclude(support_oppose='', candidate__party='').filter(candidate__cycle=2010, candidate__party__in=['R', 'D',]).values('candidate__party', 'support_oppose').annotate(amt=Sum('expenditure_amount'))), key=itemgetter('candidate__party', 'support_oppose'), reverse=True)
    for i in by_party:
        i['party_cmtes'] = Expenditure.objects.filter(candidate__cycle=2010, committee__tax_status='FECA Party', candidate__party=i['candidate__party'], support_oppose=i['support_oppose']).aggregate(t=Sum('expenditure_amount'))['t'] or 0
        i['non_party_cmtes'] = Expenditure.objects.exclude(committee__tax_status='FECA Party').filter(candidate__cycle=2010, candidate__party=i['candidate__party'], support_oppose=i['support_oppose']).aggregate(t=Sum('expenditure_amount'))['t'] or 0

    for i in by_party:
        if i['candidate__party'] == 'R' and i['support_oppose'] == 'S':
            total.republican_support_nonparty = i['non_party_cmtes']
            total.republican_support_party = i['party_cmtes']
            total.republican_support_total = i['amt']
        elif i['candidate__party'] == 'R' and i['support_oppose'] == 'O':
            total.republican_oppose_nonparty = i['non_party_cmtes']
            total.republican_oppose_party = i['party_cmtes']
            total.republican_oppose_total = i['amt']
        elif i['candidate__party'] == 'D' and i['support_oppose'] == 'S':
            total.democrat_support_nonparty = i['non_party_cmtes']
            total.democrat_support_party = i['party_cmtes']
            total.democrat_support_total = i['amt']
        elif i['candidate__party'] == 'D' and i['support_oppose'] == 'O':
            total.democrat_oppose_nonparty = i['non_party_cmtes']
            total.democrat_oppose_party = i['party_cmtes']
            total.democrat_oppose_total = i['amt']

    cutoff = datetime.date.today() - datetime.timedelta(days=5)

    non_party_committees = Expenditure.objects.filter(candidate__cycle=2010, expenditure_date__gt=cutoff).exclude(Q(committee__slug='') | Q(committee__tax_status='FECA Party')).order_by('committee').values('committee__name', 'committee__slug').annotate(amount=Sum('expenditure_amount')).order_by('-amount')
    TopCommittee.objects.all().delete()
    for committee in non_party_committees[:10]:
        c = Committee.objects.get(slug=committee['committee__slug'])
        amount = committee['amount']
        TopCommittee.objects.create(committee=c,
                                    amount=amount)

    party_committees = Expenditure.objects.filter(candidate__cycle=2010, expenditure_date__gt=cutoff, committee__tax_status='FECA Party').exclude(committee__slug='').order_by('committee').values('committee__name', 'committee__slug').annotate(amount=Sum('expenditure_amount')).order_by('-amount')
    TopPartyCommittee.objects.all().delete()
    for committee in party_committees[:10]:
        c = Committee.objects.get(slug=committee['committee__slug'])
        amount = committee['amount']
        TopPartyCommittee.objects.create(committee=c,
                                         amount=amount)

    top_races = Expenditure.objects.exclude(race='', candidate=None).filter(candidate__cycle=2010, expenditure_date__gt=cutoff).order_by('race').values('race').annotate(amount=Sum('expenditure_amount')).order_by('-amount')
    TopRace.objects.all().delete()
    for race in top_races[:10]:
        TopRace.objects.create(race=race['race'],
                               amount=race['amount'])

    top_candidates = Expenditure.objects.filter(candidate__cycle=2010, expenditure_date__gt=cutoff).order_by('candidate').values('candidate').annotate(amount=Sum('expenditure_amount')).order_by('-amount')[:10]
    TopCandidate.objects.all().delete()
    for candidate in top_candidates[:10]:
        try:
            TopCandidate.objects.create(candidate=Candidate.objects.get(pk=candidate['candidate']),
                                        amount=candidate['amount'])
        except Candidate.DoesNotExist:
            continue

    total.save()

class Command(BaseCommand):

    def handle(self, *args, **options):
        cache_totals()


'''Exercise 2: Write a program that categorizes each mail message by which day of the week the commit was done. To do this look for lines
that start with “From”, then look for the third word and keep a running count of each of the days of the week. At the end of the program print
out the contents of your dictionary (order does not matter).
Sample Line:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Sample Execution:
python dow.py
Enter a file name: mbox-short.txt
{'Fri': 20, 'Thu': 6, 'Sat': 1}
'''

fname = input("Enter a file name: ")

try:
    fhand = open(fname)
except:
    print("File name: "+fname+" can't be found")
ddays = dict()
week_day = ""
for line in fhand:
    line = line.rstrip()
    if line.startswith("From "):
        words = line.split()
        week_day = words[2]
        if week_day not in ddays:
            ddays[week_day] = 1
        else:
            ddays[week_day] += 1
print(ddays)


import telebot
from model_predict import predict

from preprocessing import preprocess_text

bot = telebot.TeleBot(
    "2087987681:AAG813Ais8YRNy4nlhrHZTK5UfQc4ZYa55Y", parse_mode=None)

# for /start and /help commands

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Enter a message and we'll predict if it's hateful or not.")

@bot.message_handler(commands=['preprocess'])
def send_welcome(message):
    bot.reply_to(message, preprocess_text(message.text))

@bot.message_handler(func=lambda m: True)
def echo_all(message):
    toReturn = predict(message.text)
    if toReturn == 1:
        bot.reply_to(message, "Your message is hate")
    else:
        bot.reply_to(message, "Your message is fine")

    # not replying to the toReturn	# bot.send_message(message.chat.id, "it's working!!")

bot.infinity_polling()


#!C:\Users\varunachalam\AppData\Local\Programs\Python\Python36-32\python
import sumofcubes1

def main():
    no1=eval(input("Enter the no you would like to Check whether this is Armstrong no"))
    no2=sumofcubes1.sumcubes(no1)
    if no1==no2:
        print("Entered no %d is Armstrong no"%(no1))
    else:
        print("Entered no %d is Not Armstrong no"%(no1))
if __name__=='__main__':
    main()


import json

from sqlite_testdb import SqliteTest

class TestSbsVariationApi(SqliteTest):
    def test_get_integrity(self):
        """
        Tests if expected result is returned for integrity api
        """
        actual_json = self.client.get('/sbs/analyses/1/integrities')
        actual_json = json.loads(actual_json.data)
        self.assertEqual('Success', actual_json['status']['message'])
        self.assertEqual(1, actual_json['data'][0]['id'])

    def test_update_tier(self):
        """
        Tests if tier_label is updated for single or multiple variation
        """
        variation_list = [dict(variation_id=1, tier_label='Ignore')]
        request_data = json.dumps(dict(variation_list=variation_list))
        actual_json = self.client.put('/sbs/variations', data=request_data,
                                      content_type='application/json')
        actual_json = json.loads(actual_json.data)
        self.assertEqual('Success', actual_json['status']['message'])
        self.assertEqual(1, actual_json['data'][0]['id'])

    def test_update_tier_with_incorrect_variation_id(self):
        """
        Tests if error is returned for incorrect variation_id
        """
        variation_list = [dict(variation_id='2a', tier_label='Ignore')]
        request_data = json.dumps(dict(variation_list=variation_list))
        actual_json = self.client.put('/sbs/variations', data=request_data,
                                      content_type='application/json')
        actual_json = json.loads(actual_json.data)
        self.assertEqual('Error', actual_json['status']['message'])


import peewee
import main
import logging

db_filename = main.CONF.get('VK', 'db_file', fallback='')
db = peewee.SqliteDatabase(db_filename, pragmas={'journal_mode': 'wal',
                                                 'cache_size': 64,
                                                 'foreign_keys': 1,
                                                 'ignore_check_constraints': 0,
                                                 'synchronous': 0})

class User(peewee.Model):
    id = peewee.IntegerField(primary_key=True)
    name = peewee.CharField()
    is_fem = peewee.BooleanField()
    money = peewee.IntegerField(default=0)

    class Meta:
        database = db

class Admins(peewee.Model):
    user = peewee.ForeignKeyField(User, primary_key=True, on_delete='cascade')
    status = peewee.IntegerField(default=0)

    class Meta:
        database = db

class Group(peewee.Model):
    id = peewee.IntegerField(primary_key=True)
    name = peewee.CharField()
    add_by = peewee.ForeignKeyField(User)
    accepted = peewee.IntegerField(default=0)
    nsfw = peewee.BooleanField(default=False)
    likes = peewee.IntegerField(default=0)
    views = peewee.IntegerField(default=0)
    subs = peewee.IntegerField(default=0)
    last_update = peewee.IntegerField(default=0)
    last_post = peewee.IntegerField(default=0)
    last_scan = peewee.IntegerField(default=0)

    class Meta:
        database = db

class Price(peewee.Model):
    group = peewee.ForeignKeyField(Group, primary_key=True, on_delete='cascade')
    add_by = peewee.ForeignKeyField(User)
    accepted = peewee.IntegerField(default=0)
    last_scan = peewee.IntegerField(default=0)
    head = peewee.IntegerField(default=0)
    half = peewee.IntegerField(default=0)
    full = peewee.IntegerField(default=0)

    class Meta:
        database = db

class Art(peewee.Model):
    id = peewee.IntegerField(primary_key=True)
    vk_id = peewee.CharField(unique=True)
    url = peewee.CharField(unique=True)
    source = peewee.CharField()
    add_by = peewee.ForeignKeyField(User)
    from_group = peewee.ForeignKeyField(Group, on_delete='cascade')
    accepted = peewee.IntegerField(default=0)
    add_time = peewee.IntegerField(default=0)
    message_id = peewee.IntegerField(default=0)

    class Meta:
        database = db

class Tag(peewee.Model):
    id = peewee.IntegerField(primary_key=True)
    title = peewee.CharField(default='None')
    description = peewee.CharField(default='None')

    class Meta:
        database = db

class ArtTag(peewee.Model):
    art = peewee.ForeignKeyField(Art, on_delete='cascade')
    tag = peewee.ForeignKeyField(Tag, on_delete='cascade')

    class Meta:
        database = db
        primary_key = peewee.CompositeKey('art', 'tag')

class Migrations(peewee.Model):
    id = peewee.IntegerField(primary_key=True)

    class Meta:
        database = db

def init_db():
    if not db.get_tables():
        db.create_tables([User, Group, Admins, Migrations, Art, Tag, ArtTag, Price])
        Migrations.create(id=1)
        Migrations.create(id=2)
        Migrations.create(id=3)
        return True
    return False

def update_admins(admin_list):
    for admin in admin_list:
        if admin['role'] in ['creator', 'administrator']:
            user = User.get_or_none(id=admin['id'])
            if user:
                if not Admins.get_or_none(user=user):
                    Admins.create(user=user)

def update_db():
    import playhouse.migrate as playhouse_migrate

    db_migrate = peewee.SqliteDatabase(db_filename, pragmas={'journal_mode': 'wal',
                                                             'cache_size': 64,
                                                             'foreign_keys': 0,
                                                             'ignore_check_constraints': 0,
                                                             'synchronous': 0})
    migrator = playhouse_migrate.SqliteMigrator(db_migrate)
    if not Migrations.get_or_none(1):
        logging.info(f'migration 1')
        playhouse_migrate.migrate(
            migrator.add_column('Group', 'accepted', peewee.BooleanField(default=False)),
        )
        Migrations.create(id=1)
    if not Migrations.get_or_none(2):
        logging.info(f'migration 2')
        playhouse_migrate.migrate(
            migrator.add_column('Group', 'last_scan', Group.last_scan),
            migrator.add_column('Art', 'message_id', Art.message_id),
        )
        Migrations.create(id=2)
    if not Migrations.get_or_none(3):
        logging.info(f'migration 3')
        db.create_tables([Price])
        Migrations.create(id=3)
    #         playhouse_migrate.migrate(
    #             migrator.add_column('RpProfile', 'show_link', RpProfile.show_link),
    #             # migrator.rename_column('ProfileSettingList', 'item_id', 'item'),
    #             # migrator.drop_column('RoleOffer', 'to_profile_id')
    #         )

if __name__ == "__main__":
    update_db()
    pass


from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from .forms import PilotForm
from .models import Site, Pilot, Comment
from . import creed
from . import webster
import datetime
import traceback

class IndexView(generic.ListView):
    template_name = 'assure/index.html'
    context_object_name = 'latest_site_list'
    fields = ['url']
    form_class = PilotForm
    model = Site

    def __init__(self):
        self.webster = webster.Webster()
        self.creed = creed.Main(self.webster)

    def get_queryset(self):
        """Return the last five published sites."""
        return Site.objects.order_by('-pub_date')[:5]

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['latest_site_list'] = Site.objects.all()
        context.update({'form':self.form_class})
        return context

    def post(self, request):
        if request.method == 'POST':
            site=''
            pilot=''
            url=''
            form = PilotForm(request.POST)
            if form.is_valid():
                url,url_test = self.validate_url(request.POST.get('url'))
            try: #fails = site is new and requires a report
                site = Site.objects.get(url__contains=url)
            except Site.DoesNotExist:
                site = Site(url=url,pub_date=datetime.datetime.now(),domain=url.split('.')[1])
                site.save()
            finally:
                try:
                    pilot = Pilot.objects.get(site=site)
                    dictionary = self.creed.test_units(url=site.url)
                    pilot.pub_date=datetime.datetime.now()
                    pilot.viewport_json=dictionary['viewport']
                    pilot.site_json=dictionary['site']
                    pilot.homepage_json=dictionary['homepage']
                    pilot.save()
                except Pilot.DoesNotExist:
                    try:
                        dictionary = self.creed.test_units(url=url)
                        pilot = Pilot(site=site,current_rating=9,pub_date=datetime.datetime.now(),viewport_json=dictionary['viewport'],site_json=dictionary['site'])
                        pilot.save()
                    except Exception as e:
                        traceback.print_exc()
                        self.creed.quit_driver()
                        Site.objects.get(url__contains=url).delete()

        return HttpResponseRedirect(reverse('assure:detail', args=(site.id,)))

        #return self.render_to_response(self.get_context_data(context_object_name=self.context_object_name, form=form))

    def validate_url(self, url):
        import re
        regex = re.compile(
            r'^(?:http|ftp)s?://' # http:// or https://
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
            r'localhost|' #localhost...
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
            r'(?::\d+)?' # optional port
            r'(?:/?|[/?]\S+)$', re.IGNORECASE
        )
        return url,re.match(regex, url) is not None

class DetailView(generic.DetailView):
    model = Site
    template_name = 'assure/detail.html'
    context_object_name = 'latest_report'
    fields = ['url']
    form_class = PilotForm

    def __init__(self):
        self.webster = webster.Webster()
        self.creed = creed.Main(self.webster)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        site=Site.objects.get(url__contains=kwargs['object'])
        try:
            pilot = Pilot.objects.get(site=site)
        except Pilot.DoesNotExist:
            pilot=None
        context['latest_report'] = pilot
        context.update({'form':self.form_class})
        return context

    def post(self, request, pk):
        if request.method == 'POST':
            dictionary={}
            site=''
            pilot=''
            form = PilotForm(request.POST)
            try:
                site=Site.objects.get(pk=pk)
                pilot = Pilot.objects.get(site=site)
            except Pilot.DoesNotExist:
                pilot = Pilot(site=site)
            finally:
                try:
                    pilot = Pilot.objects.get(site=site)
                    dictionary = self.creed.test_units(url=site.url)
                    pilot.pub_date=datetime.datetime.now()
                    pilot.viewport_json=dictionary['viewport']
                    pilot.site_json=dictionary['site']
                    pilot.homepage_json=dictionary['homepage']
                    pilot.save()
                except Pilot.DoesNotExist:
                    try:
                        dictionary = self.creed.test_units(url=site.url)
                        pilot = Pilot(site=site,
                                current_rating=9,
                                pub_date=datetime.datetime.now(),
                                viewport_json=dictionary['viewport'],
                                site_json=dictionary['site']
                            )
                        pilot.save()
                    except Exception as e:
                        traceback.print_exc()
                        self.creed.quit_driver()
                        Site.objects.get(url__contains=url).delete()

        return HttpResponseRedirect(reverse('assure:detail', args=(site.id,)))

'''def detail(request, site_id):
    pilot = get_object_or_404(Pilot, pk=site_id)
    return render(request, 'assure/detail.html', {'pilot':pilot})'''

class ResultsView(generic.DetailView):
    model = Site
    template_name = 'assure/results.html'

def comment(request, site_id):
    site = get_object_or_404(Site, pk=site_id)
    try:
        selected_choice = site.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the site voting form.
        return render(request, 'assure/detail.html', {
            'site': site,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('assure:results', args=(site.id,)))


#!/usr/bin/env python
#coding=utf-8
# pip install aliyun-python-sdk-alidns
from aliyunsdkcore import client
from aliyunsdkalidns.request.v20150109 import SetDomainRecordStatusRequest
from flask import current_app
from datetime import datetime

class UpRecordStatus(object):
    '''
    region_id需要提交工单查询，默认cn_hangzhou
    域名解析变更，把小米FDS的cname解析暂停 => 把阿里OSS的cname解析启用
    '''
    def __init__(self):
        self.clt = client.AcsClient(
            current_app.config['DNSACCESSKEYID'],
            current_app.config['DNSACCESSKEYSECRET'])
        self.request = SetDomainRecordStatusRequest.SetDomainRecordStatusRequest()
        self.request.set_accept_format('json')

    def changestatus(self):
        for recordid in current_app.config['ENABLELIST']:
            # 设置参数
            self.request.add_query_param('Status', 'Enable')
            self.request.add_query_param('RecordId', recordid)
            # 发起请求
            self.clt.do_action(self.request)
            print(datetime.now(),'启用dns'+recordid)
        for recordid in current_app.config['DISABLELIST']:
            self.request.add_query_param('Status', 'Disable')
            self.request.add_query_param('RecordId', recordid)
            self.clt.do_action(self.request)
            print(datetime.now(),'暂停dns'+recordid)


# Databricks notebook source
# MAGIC %pip install -r requirements.txt

# COMMAND ----------

import warnings
warnings.filterwarnings("ignore")

# COMMAND ----------

# MAGIC %sh
# MAGIC mkdir -p /dbfs/FileStore/solution_accelerators/digitization/ && touch /dbfs/FileStore/solution_accelerators/digitization/init.sh
# MAGIC cat <<EOF > /dbfs/FileStore/solution_accelerators/digitization/init.sh
# MAGIC #!/usr/bin/env bash
# MAGIC sudo apt-get install -y tesseract-ocr
# MAGIC sudo apt-get install -y poppler-utils
# MAGIC EOF

# COMMAND ----------

# Set mlflow experiment explicitly to make sure the code runs in both interactive execution and job execution
import mlflow
username = dbutils.notebook.entry_point.getDbutils().notebook().getContext().userName().get()
mlflow.set_experiment('/Users/{}/document_digitization'.format(username))
model_name = 'table_classification'

# COMMAND ----------

# Set sector to include brewing companies
sector = 'i22'

# Here we use a `/tmp/...` path in DBFS to minimize dependency. We recommend using a `/mnt/...` path or one that directly connects to your cloud storage for production usage. To learn more about mount points, please review [this document](https://docs.databricks.com/dbfs/mounts.html).  If you would like to use a mount point or a different path, please update the variable below with the appropriate path:
landing_zone = '/tmp/fsi/datasets/digitization/csr/files'
k = 3
landing_zone_fs = '{}/**/pages'.format(landing_zone)

# COMMAND ----------


from hplcpostproagent.hypo_rxn.hypo_rxn import *

TIME_TEMPERATURE_ECO_SCORE_FACTOR = 0.002
AMBIENT_TEMPERATURE_DEGREECELSIUS = 25
ECO_SCORE_BASE_VALUE = 100

def calculate_performance_indicator(
    hypo_reactor: HypoReactor,
    hypo_end_stream: HypoEndStream,
    rxn_exp_instance: ReactionExperiment,
    target_clz: str
) -> List[PerformanceIndicator]:
    """
        This method calculates the value of performance indicator and returns an instance of PerformanceIndicator.
        Arguments:
            hypo_reactor - instance of HypoReactor
            hypo_end_stream - instance of HypoEndStream
            rxn_exp_instance - instance of ontorxn.ReactionExperiment from chemistry_and_robots package
            target_clz - class iri of the target PerformanceIndicator
    """

    # Locate the reference instance of the PerformanceIndicator in the source ReactionExperiment that the ReactionVariation instance isVariationOf
    # For those PerformanceIndicator that NO reference instance are located, we compute directly
    lst_reference = locate_reference_performance_indicator(rxn_exp_instance, target_clz)
    # Create and return the list of PerformanceIndicator instances depends on target_clz
    if target_clz == ONTOREACTION_YIELD:
        return [calculate_yield(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    elif target_clz == ONTOREACTION_CONVERSION:
        return [calculate_conversion(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    elif target_clz == ONTOREACTION_ECOSCORE:
        return [calculate_eco_score(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    elif target_clz == ONTOREACTION_ENVIRONMENTALFACTOR:
        return [calculate_enviromental_factor(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    elif target_clz == ONTOREACTION_SPACETIMEYIELD:
        return [calculate_space_time_yield(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    elif target_clz == ONTOREACTION_RUNMATERIALCOST:
        return [calculate_run_material_cost(rxn_exp_instance, hypo_reactor, hypo_end_stream, pi) for pi in lst_reference]
    else:
        raise NotImplementedError("Requested target clz <%s> as PerformanceIndicator is NOT yet supported when post-processing ReactionExperiment: %s." % (
            target_clz, str(rxn_exp_instance)))

def calculate_yield(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction yield based on the non-catalyst yield limiting reactant (the one with the lowest run concentration)."""
    yield_limiting_species = retrieve_yield_limiting_species(hypo_reactor)
    target_product_species = retrieve_product_species(hypo_end_stream)

    yield_limiting_conc = utils.unit_conversion_return_value_dq(yield_limiting_species.run_conc, utils.UNIFIED_CONCENTRATION_UNIT)
    prod_run_conc = utils.unit_conversion_return_value_dq(target_product_species.run_conc, utils.UNIFIED_CONCENTRATION_UNIT)

    _yield = round(prod_run_conc / yield_limiting_conc, 4) # Round the decimal place

    pi_yield = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _yield, OM_ONE)
    pi_yield.yieldLimitingSpecies = yield_limiting_species.species_iri # Also set the yieldLimitingSpecies

    return pi_yield

def calculate_conversion(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction conversion based on the non-catalyst yield limiting reactant (the one with the lowest run concentration)."""
    # NOTE here the conversion is calculated based on the yield limiting species
    yield_limiting_species = retrieve_yield_limiting_species(hypo_reactor)
    _species_in_end_stream = [species for species in hypo_end_stream.component if species.species_iri == yield_limiting_species.species_iri][0]

    yield_limiting_conc = utils.unit_conversion_return_value_dq(yield_limiting_species.run_conc, utils.UNIFIED_CONCENTRATION_UNIT)
    unreacted_conc = utils.unit_conversion_return_value_dq(_species_in_end_stream.run_conc, utils.UNIFIED_CONCENTRATION_UNIT)

    _conversion = round(1 - unreacted_conc / yield_limiting_conc, 4) # Round the decimal place

    pi_conversion = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _conversion, OM_ONE)
    pi_conversion.yieldLimitingSpecies = yield_limiting_species.species_iri # Also set the yieldLimitingSpecies

    return pi_conversion

def calculate_space_time_yield(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction space time yield, which commonly has (kg per litre per minute) as its unit."""
    target_product_species = retrieve_product_species(hypo_end_stream)

    prod_run_mass = utils.unit_conversion_return_value_dq(target_product_species._run_mass, utils.UNIFIED_MASS_UNIT)
    residence_time = utils.unit_conversion_return_value_dq(hypo_reactor.residence_time, utils.UNIFIED_TIME_UNIT)
    reactor_volume = utils.unit_conversion_return_value_dq(hypo_reactor.reactor_volume, utils.UNIFIED_VOLUME_UNIT)

    _sty = round(prod_run_mass / residence_time / reactor_volume, 2) # Round the decimal place

    pi_sty = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _sty, utils.UNIFIED_SPACETIMEYIELD_UNIT)

    return pi_sty

def calculate_eco_score(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction eco score."""
    residence_time = utils.unit_conversion_return_value_dq(hypo_reactor.residence_time, utils.UNIFIED_TIME_UNIT)
    reactor_temperature = utils.unit_conversion_return_value_dq(hypo_reactor.reactor_temperature, OM_DEGREECELSIUS)
    time_temperature_eco_score = TIME_TEMPERATURE_ECO_SCORE_FACTOR * residence_time * (
        (reactor_temperature-AMBIENT_TEMPERATURE_DEGREECELSIUS) * (reactor_temperature-AMBIENT_TEMPERATURE_DEGREECELSIUS) / abs(reactor_temperature-AMBIENT_TEMPERATURE_DEGREECELSIUS))
    total_run_eco_score = retrieve_total_run_eco_score(hypo_reactor)
    _eco_score = round(ECO_SCORE_BASE_VALUE - time_temperature_eco_score - total_run_eco_score, 2) # Round the decimal place

    pi_eco_score = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _eco_score, utils.UNIFIED_ECOSCORE_UNIT)

    return pi_eco_score

def calculate_enviromental_factor(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction environmental factor."""
    target_product_species = retrieve_product_species(hypo_end_stream)
    all_reactant = [s for inlet in hypo_reactor.inlet_run_stream for s in inlet.solute if s._is_reactant]
    all_solvent = [inlet.solvent for inlet in hypo_reactor.inlet_run_stream]
    reactant_and_solvent = all_reactant + all_solvent

    total_reac_n_solvent_run_mass = sum([utils.unit_conversion_return_value_dq(s._run_mass, utils.UNIFIED_MASS_UNIT) for s in reactant_and_solvent])
    prod_run_mass = utils.unit_conversion_return_value_dq(target_product_species._run_mass, utils.UNIFIED_MASS_UNIT)

    _e_factor = round(prod_run_mass / (total_reac_n_solvent_run_mass - prod_run_mass), 2) # Round the decimal place

    pi_e_factor = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _e_factor, utils.UNIFIED_ENVIRONMENTFACTOR_UNIT)

    return pi_e_factor

def calculate_run_material_cost(rxn_exp_instance: ReactionExperiment, hypo_reactor: HypoReactor, hypo_end_stream: HypoEndStream, reference_performance_indicator: PerformanceIndicator) -> PerformanceIndicator:
    """This method calculates the reaction material cost for a single run, which commonly has (pound sterling) as its unit."""
    all_reactant = [s for inlet in hypo_reactor.inlet_run_stream for s in inlet.solute if s._is_reactant]
    all_solvent = [inlet.solvent for inlet in hypo_reactor.inlet_run_stream]
    reactant_and_solvent = all_reactant + all_solvent
    # NOTE here the unit of the _run_volume and def_cost should already be standardised at creation of each HypoStreamSpecies instance
    # NOTE therefore the unit conversion is omitted
    _run_material_cost = round(sum([s._run_volume.hasNumericalValue * s.def_cost.hasNumericalValue for s in reactant_and_solvent]), 2) # Round the decimal place

    pi_run_material_cost = create_performance_indicator_instance(rxn_exp_instance, reference_performance_indicator, _run_material_cost, utils.UNIFIED_RUN_MATERIAL_COST_UNIT)

    return pi_run_material_cost

def locate_reference_performance_indicator(
    rxn_exp_instance: ReactionExperiment,
    target_clz: str
) -> PerformanceIndicator:
    """This method creates a placeholder PerformanceIndicator instance that to be added to the ReactionExperiment instance."""
    if isinstance(rxn_exp_instance, ReactionVariation):
        lst_target = [pi for pi in rxn_exp_instance.isVariationOf.hasPerformanceIndicator if pi.clz == target_clz]
        if len(lst_target) == 1:
            return lst_target
        elif len(lst_target) == 0:
            return [PerformanceIndicator(
                instance_iri="http://placeholder",
                clz=target_clz,
                rxn_exp_iri=rxn_exp_instance.instance_iri, # The value here should NOT matter as it should not be accessed by any codes
                objPropWithExp=OBJECT_RELATIONSHIP_PERFORMANCE_INDICATOR_RXN_EXP_DICT[target_clz],
                hasValue=None,
                positionalID=None
            )]
        else:
            raise Exception("Multiple target PerformanceIndicator with a clz <%s> is NOT yet supported, identified in ReactionExperiment: %s" % (
                target_clz, str(rxn_exp_instance)))
    else:
        raise NotImplementedError("Post processing ReactionExperiment is NOT yet supported.")

def create_performance_indicator_instance(
    rxn_exp_instance: ReactionExperiment,
    reference_performance_indicator: PerformanceIndicator,
    numerical_value,
    unit: str
) -> PerformanceIndicator:
    """This method creates the instance of PerformanceIndicator given the placeholder instance and the computed value to be used for OM_Measure."""
    _objPropWithExp = reference_performance_indicator.objPropWithExp
    if ONTOREACTION_HASPERFORMANCEINDICATOR not in reference_performance_indicator.objPropWithExp:
        _objPropWithExp.append(ONTOREACTION_HASPERFORMANCEINDICATOR)

    performance_indicator_instance = PerformanceIndicator(
        instance_iri=INSTANCE_IRI_TO_BE_INITIALISED,
        namespace_for_init=getNameSpace(rxn_exp_instance.instance_iri),
        clz=reference_performance_indicator.clz,
        rxn_exp_iri=rxn_exp_instance.instance_iri,
        objPropWithExp=_objPropWithExp,
        hasValue=OM_Measure(
            instance_iri=INSTANCE_IRI_TO_BE_INITIALISED,
            namespace_for_init=getNameSpace(rxn_exp_instance.instance_iri),
            hasUnit=unit,
            hasNumericalValue=numerical_value
        ),
        positionalID=reference_performance_indicator.positionalID
    )
    return performance_indicator_instance

def retrieve_yield_limiting_species(hypo_reactor: HypoReactor) -> HypoStreamSpecies:
    """This method retrieves the yield limiting reactant given the instance of HypoReactor."""
    all_inlet_stream = hypo_reactor.inlet_run_stream
    all_reactant_species = {r.species_iri:r for reac in all_inlet_stream for r in reac.solute if r._is_reactant}
    all_reactant_conc = {all_reactant_species[s].species_iri:all_reactant_species[s].run_conc for s in all_reactant_species}
    yield_limiting_conc = min([utils.unit_conversion_return_value_dq(all_reactant_conc[dq], utils.UNIFIED_CONCENTRATION_UNIT) for dq in all_reactant_conc])
    yield_limiting_species_lst = [species for species in all_reactant_conc if all_reactant_conc[species].hasNumericalValue == yield_limiting_conc]
    if len(yield_limiting_species_lst) > 1:
        raise NotImplementedError("Processing multiple yield limiting species in the reactor input streams is NOT yet supported, the HypoReactor: %s" % (
            str(hypo_reactor)))
    else:
        yield_limiting_species_iri = yield_limiting_species_lst[0]
    return all_reactant_species[yield_limiting_species_iri]

def retrieve_product_species(hypo_end_stream: HypoEndStream) -> HypoStreamSpecies:
    """This method retrieves the product species given the instance of HypoEndStream."""
    all_target_product = [comp for comp in hypo_end_stream.component if comp._is_target_product]

    if len(all_target_product) > 1:
        raise NotImplementedError("Multiple TargetProduct in the end stream is NOT yet supported: %s" % (str(all_target_product)))
    elif len(all_target_product) < 1:
        raise Exception("No TargetProduct identified in the end stream %s" % (str(hypo_end_stream)))
    else:
        target_product = all_target_product[0]
    return target_product

def retrieve_total_run_eco_score(hypo_reactor: HypoReactor):
    """This method retrieves tht total run eco score given the instance of HypoReactor."""
    all_solute = [s for inlet in hypo_reactor.inlet_run_stream for s in inlet.solute]
    all_solvent = [inlet.solvent for inlet in hypo_reactor.inlet_run_stream]
    all_species = all_solute + all_solvent
    total_run_eco_score = sum([utils.unit_conversion_return_value_dq(s.def_eco_score, utils.UNIFIED_ECOSCORE_UNIT) for s in all_species])
    return total_run_eco_score


"""
Plot omega, sliding net charge
plot sliding fraction of Q/N, S/T, A/G, P
"""
import os
import pandas as pd
import matplotlib.pyplot as plt
from deconstruct_lc import tools_lc
from deconstruct_lc.complementarity import motif_seq

class Fraction(object):

    def __init__(self):
        self.k = 6
        self.lca = 'SGEQAPDTNKR'
        self.lce = 1.6

    def process_seq(self, seq, k):
        kmers = tools_lc.seq_to_kmers(seq, k)
        qn = self.alph_fracs(kmers, 'QN')
        st = self.alph_fracs(kmers, 'ST')
        ag = self.alph_fracs(kmers, 'AG')
        p = self.alph_fracs(kmers, 'P')
        ed = self.alph_fracs(kmers, 'ED')
        kr = self.alph_fracs(kmers, 'KR')
        f = self.alph_fracs(kmers, 'F')
        r = self.alph_fracs(kmers, 'R')
        #plt.plot(qn, label='QN')
        plt.plot(st, label='ST')
        #plt.plot(ag, label='AG')
        #plt.plot(r, label='R')
        #plt.plot(f, label='F')
        lca_x, lca_y, lce_x, lce_y = self.get_motif_index(seq)
        plt.scatter(lca_x, lca_y, color='black', s=2)
        plt.scatter(lce_x, lce_y, color='red', s=2)
        #plt.plot(ed, label='ED')
        #plt.plot(kr, label='KR')
        plt.plot(p, label='P')
        plt.legend()
        plt.show()

    def alph_fracs(self, kmers, alph):
        fracs = []
        for kmer in kmers:
            frac = self.get_frac(kmer, alph)
            fracs.append(frac)
        return fracs

    def get_frac(self, kmer, alph):
        tot_count = 0
        for aa in alph:
            tot_count += kmer.count(aa)
        assert tot_count <= len(kmer)
        frac = float(tot_count)/float(len(kmer))
        return frac

    def get_motif_index(self, sequence):
        mot = motif_seq.LcSeq(sequence, self.k, self.lca, 'lca')
        ind_in, ind_out = mot._get_motif_indexes()
        lca_x = list(ind_in)
        lca_y = [1]*(len(lca_x))
        mot = motif_seq.LcSeq(sequence, self.k, self.lce, 'lce')
        ind_in, ind_out = mot._get_motif_indexes()
        lce_x = list(ind_in)
        lce_y = [1.1]*(len(lce_x))
        return lca_x, lca_y, lce_x, lce_y

class Pipeline(object):

    def __init__(self):
        self.base_fp = os.path.join(os.path.dirname(__file__), '..', 'data')
        self.nmo_fpi = os.path.join(self.base_fp, 'scores',
                                    'nmo_6_SGEQAPDTNKR_6_1.6_seq_scores.tsv')
        self.pdb_fpi = os.path.join(self.base_fp, 'scores',
                                    'pdb_nomiss_cd50_6_SGEQAPDTNKR_6_1.6_seq_scores.tsv')
        self.lca_label = '6_SGEQAPDTNKR'
        self.lce_label = '6_1.6'

    def sandbox(self):
        label = self.lca_label
        df = pd.read_csv(self.nmo_fpi, sep='\t', index_col=0)
        df = df[(df[label] > 30)]
        df = df.sort_values(by=[label])
        df = df.reset_index(drop=True)
        for i, row in df.iterrows():
            sequence = row['Sequence']
            print(len(sequence))
            print(row[label])
            print(row['Protein ID'])
            frac = Fraction()
            frac.process_seq(sequence, 6)

def main():
    pipe = Pipeline()
    pipe.sandbox()

if __name__ == '__main__':
    main()


import logging
import time
import random
from langchain.agents import initialize_agent, load_tools
from langchain.chat_models import ChatOpenAI

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# import and initialize graphsignal
# add GRAPSIGNAL_API_KEY to your environment variables
import graphsignal
graphsignal.configure(deployment='langchain-demo')

def solve(user_id, task):
    graphsignal.set_context_tag('user', user_id)

    llm = ChatOpenAI(temperature=0)
    tools = load_tools(["llm-math"], llm=llm)
    agent = initialize_agent(
        tools, llm, agent="zero-shot-react-description", verbose=True
    )
    agent.run(task)

# simulate some requests
while True:
    id = random.randint(0, 10)
    num = random.randint(0, 100)

    try:
        solve(f'user{id}', f"What is {num} raised to .123243 power?")
        logger.debug('Task solved')
    except:
        logger.error("Error while solving task", exc_info=True)

    time.sleep(5 * random.random())


#!/usr/bin/python3

import sys
import collections

class Tree:
    children = None
    metadata = None

    def __init__(self):
        self.children = collections.deque()
        self.metadata = collections.deque()

def parse_input():
    line = next(sys.stdin).strip()

    i = 0
    while i < len(line):
        num = 0
        while line[i] != ' ' and i < len(line):
            num *= 10
            num += int(line[i])
            i += 1
        i += 1
        yield num
    return

def build_tree(line = None, i = None):
    if not line:
        line = [int(x) for x in next(sys.stdin).split()]
        i = 0

    t = Tree()
    nc = line[i + 0]
    nm = line[i + 1]

    if nc == 0:
        for j in range(nm):
            t.metadata.append(line[i + 2 + j])
        return (t, i + 2 + nm)

    ni = i + 2
    for j in range(nc):
        (st, ni) = build_tree(line, ni)
        t.children.append(st)

    for j in range(nm):
        t.metadata.append(line[ni])
        ni += 1

    return (t, ni)

def checksum(tree):
    ret = sum(tree.metadata)
    for subtree in tree.children:
        ret += checksum(subtree)

    return ret

def value(tree):
    if len(tree.children) == 0:
        return sum(tree.metadata)

    ret = 0
    for entry in tree.metadata:
        if entry == 0:
            continue
        idx = entry - 1
        if idx < len(tree.children):
            ret += value(tree.children[idx])
    return ret

def main():
    (t, _) = build_tree()
    print(checksum(t))
    print(value(t))

if __name__ == "__main__":
    main()


def get_cats_info(path):
    cat_list = []
    with open (path, 'r') as opened_file:
        while True:
            line = opened_file.readline()
            if not line:
                break
            elif line[-1] == '\n':
                line = line[:-1]

            cat_line = line.split(',')
            cat_list.append({'id':cat_line[0], 'name':cat_line[1], 'age':cat_line[2]})
    return cat_list


from enum import Enum
import logging
import coloredlogs
import itertools

_LoggerEnums = {
    1: ['system', 'SYSTEM'],
    2: ['client', 'CLIENT'],
    3: ['handler', 'HANDLER'],
}
LoggerEnums = Enum(
    value='LoggerEnums',
    names=itertools.chain.from_iterable(
        itertools.product(v, [k]) for k, v in _LoggerEnums.items()
    )
)

logging.getLogger("urllib3").propagate = False

def initLogging(config):
    global logger
    coloredlogs.DEFAULT_LOG_FORMAT = '%(asctime)s [%(name)10s] [%(levelname).1s] %(message)s'
    coloredlogs.DEFAULT_DATE_FORMAT = '%Y-%m-%d %H:%M:%S.%f'
    coloredlogs.DEFAULT_FIELD_STYLES = {'asctime': {'color': 'cyan'}, 'name': {'bold': True, 'color': 'black'}, 'levelname': {'bold': True, 'color': 'black'}}
    coloredlogs.install(level=config['log']['level'])

def getLogger(type: LoggerEnums):
    logger = logging.getLogger(type.name)
    return logger


import os.path as osp

import mmcv
import numpy as np
import pycocotools.mask as maskUtils
from pycocotools.coco import COCO

from .coco import CocoDataset
from .registry import DATASETS

@DATASETS.register_module
class SideWalkDataset(CocoDataset):

    CLASSES = ('barricade', 'bench', 'bicycle', 'bollard', 'bus', 'car',
               'carrier', 'cat', 'chair', 'dog', 'fire_hydrant', 'kiosk',
               'motorcycle', 'movable_signage', 'parking_meter', 'person',
               'pole', 'potted_plant', 'power_controller', 'scooter', 'stop',
               'stroller', 'table', 'traffic_light',
               'traffic_light_controller', 'traffic_sign', 'tree_trunk',
               'truck', 'wheelchair')

    def load_annotations(self, ann_file):
        self.coco = COCO(ann_file)
        self.cat_ids = self.coco.getCatIds()
        self.cat2label = {
            cat_id: i + 1
            for i, cat_id in enumerate(self.cat_ids)
        }
        self.img_ids = self.coco.getImgIds()
        img_infos = []
        for i in self.img_ids:
            info = self.coco.loadImgs([i])[0]
            info['filename'] = osp.join(info['img_prefix'], info['file_name'])
            img_infos.append(info)
        return img_infos

    def show_annotations(self, img_id, show=False, out_file=None, **kwargs):
        img_info = self.img_infos[img_id]
        img_path = osp.join(self.img_prefix, img_info['filename'])
        img = mmcv.imread(img_path)
        annotations = self.coco.loadAnns(self.coco.getAnnIds(imgIds=[img_id]))

        bboxes = []
        labels = []
        class_names = ['bg'] + list(self.CLASSES)
        for ann in annotations:
            if len(ann['segmentation']) > 0:
                rle = maskUtils.frPyObjects(ann['segmentation'],
                                            img_info['height'],
                                            img_info['width'])
                ann_mask = np.sum(
                    maskUtils.decode(rle), axis=2).astype(np.bool)
                color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)
                img[ann_mask] = img[ann_mask] * 0.5 + color_mask * 0.5
            bbox = ann['bbox']
            x, y, w, h = bbox
            bboxes.append([x, y, x + w, y + h])
            labels.append(ann['category_id'])
        bboxes = np.stack(bboxes)
        labels = np.stack(labels)
        mmcv.imshow_det_bboxes(
            img,
            bboxes,
            labels,
            class_names=class_names,
            show=show,
            out_file=out_file,
            **kwargs)
        if not (show or out_file):
            return img


def encrypt(plaintext, key):
    try:
        plaintext = plaintext.upper()
        key = key.upper()
        key_length = len(key)
        key_as_int = [ord(i) for i in key]
        plaintext_int = [ord(i) for i in plaintext]
        ciphertext = ''
        for i in range(len(plaintext_int)):
            value = (plaintext_int[i] + key_as_int[i % key_length]) % 26
            ciphertext += chr(value + 65)
        return ciphertext
    except:
        print("Check again bruh")

def decrypt(cipher, key):
    try:
        cipher = cipher.upper()
        key = key.upper()
        key_length = len(key)
        key_as_int = [ord(i) for i in key]
        cipher_int = [ord(i) for i in cipher]
        plaintext = ''
        for i in range(len(cipher_int)):
            value = (cipher_int[i] - key_as_int[i % key_length]) % 26
            plaintext += chr(value + 65)
        return plaintext
    except:
        print("Check again bruh")

if __name__ == "__main__":
    plain = input("plain:")
    key = input("key:")
    print(encrypt(plain,key))
    print("==decrypted==")
    print(decrypt(encrypt(plain,key),key))


from datetime import datetime
from string import Template

import pandas as pd

def read_template(file: str) -> Template:
    with open(file, "r") as f:
        content = f.read()
    return Template(content)

df = pd.read_csv("export.csv")
df = df.astype(
    {
        "repository_stars_count": "Int64",
    }
)
df = df.fillna(
    {
        "repository_domain": "",
        "description": "",
        "repository_last_update": "",
        "distribution": "",
    }
)

header = (
    "
\n"
    "
\n"
    "Name	\n"
    "Repository	\n"
    "Repository Stars Count	\n"
    "Repository Last Update	\n"
    "Repository Domain	\n"
    "Categories	\n"
    "Compatibility	\n"
    "Frameworks	\n"
    "Distribution	\n"
    "Description	\n"
    "
\n"
    "
\n"
)

table_data = "
\n"
for index, row in df.iterrows():
    name = f"{row['name']}	"
    table_data += (
        "
\n"
        ""
        f"{name}"
        "\n"
        f"{row['repository']}	"
        "\n"
        f"{row['repository_stars_count']}	"
        "\n"
        f"{row['repository_last_update']}	"
        "\n"
        f"{row['repository_domain']}	"
        "\n"
        f"{row['categories']}	"
        "\n"
        f"{row['compatibility']}	"
        "\n"
        f"{row['frameworks']}	"
        "\n"
        f"{row['distribution']}	"
        "\n"
        f"{row['description']}	"
        "\n"
        "
\n"
    )
table_data += "
\n"

date_update = datetime.today().strftime("%Y-%m-%d")

formatted_message = read_template("template.html").safe_substitute(
    {"date_update": date_update, "header": header, "table_data": table_data}
)
with open("docs/index.html", "w") as f:
    f.write(formatted_message)


from constants import *
import numpy as np

def load_data(filepath, coeffs=ALL_COEFFS):
    """
    :param filepath: Path for the file to read, either the TRAIN_FILE or TEST_FILE
    :return:
    Organizes the data in the input file into a list of matrices. Each matrix is the data for a spoken digit,
     where the columns are the coefficients and each row is the index of the time window
    """
    mask = convert_list_to_mask(coeffs)

    digits = []
    labels = []
    current_digit = []
    with open(filepath, 'r') as f:
        # For each block, append the items into one long list and then reshape to be (-1, 13) and append to digits
        for idx, line in enumerate(f):
            if idx == 0:
                continue
            if line.isspace() or line == "\n":
                digits = add_digit(digits, current_digit)
                current_digit = []
            else:
                mfcc = list(map(float, line.split(' ')))
                mfcc = np.asarray(mfcc)
                mfcc_filtered = mfcc[mask]
                current_digit.append(mfcc_filtered)
        if len(current_digit):
            digits = add_digit(digits, current_digit)

    num_entries_per_digit = int(len(digits) / 10)  # Should be 660 for train, 220 for test
    for digit_value in range(NUM_DIGITS):
        for i in range(num_entries_per_digit):
            labels.append(digit_value)

    return np.asarray(digits, dtype=object), np.asarray(labels)

def add_digit(digits, current_digit):
    """
    :param digits: List of digits, which are each a numpy array
    :param current_digit: Current digit, which is a list of the coefficients for the digit
    :return:
    Formats and reshapes the current_digit list as a numpy array and appends it to digits and returns digits
    """
    digit_matrix = np.asarray(current_digit)
    digit_matrix = np.reshape(digit_matrix, (-1, len(current_digit[0])))
    digits.append(digit_matrix)
    return digits

def get_train_data(coeffs=ALL_COEFFS):
    return load_data(TRAIN_FILE, coeffs)

def get_test_data(coeffs=ALL_COEFFS):
    return load_data(TEST_FILE, coeffs)

def convert_list_to_mask(list_of_indices):
    mask = [False] * NUM_MFCC
    for index in list_of_indices:
        assert (0 <= index < NUM_MFCC)
        mask[index] = True
    return mask

if __name__ == "__main__":
    coeffs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    test_digits, test_labels = get_test_data()
    assert (len(test_digits[0][0]) == len(coeffs))


pkgname = "openscenegraph"
pkgver = "3.6.5"
pkgrel = 0
build_style = "cmake"
configure_args = [
    # avoid lib64
    "-DLIB_POSTFIX="
]
hostmakedepends = ["cmake", "ninja", "pkgconf"]
makedepends = [
    # no ffmpeg here, not compatible with ffmpeg 6
    "mesa-devel",
    "libcurl-devel",
    "giflib-devel",
    "librsvg-devel",
    "jasper-devel",
    "libtiff-devel",
    "sdl-devel",
    "gst-plugins-base-devel",
    "libpoppler-glib-devel",
    "libxrandr-devel",
    "libxinerama-devel",
]
pkgdesc = "High-performance real-time graphics toolkit"
maintainer = "q66 <q66@chimera-linux.org>"
license = "custom:OpenSceneGraph"
url = "https://openscenegraph.com"
source = f"https://github.com/openscenegraph/OpenSceneGraph/archive/OpenSceneGraph-{pkgver}.tar.gz"
sha256 = "aea196550f02974d6d09291c5d83b51ca6a03b3767e234a8c0e21322927d1e12"
# unit tests are off
options = ["!check", "!cross"]

def post_install(self):
    self.install_file("CMakeModules/FindOSG.cmake", "usr/lib/cmake/OSG")
    self.install_license("LICENSE.txt")

@subpackage("openscenegraph-devel")
def _devel(self):
    return self.default_devel()


from aperr002v01 import APERR002V01
from ap8 import AP8
from asi import Rec
from math import log10, sqrt, sin, cos
from tkinter import Tk, E, W, StringVar, IntVar, END
from tkinter import ttk

class S1855(APERR002V01):
    def input_widgets(self):
        super().input_widgets()
        self.root.label_coeffa.destroy()
        self.root.coeffa_frame.destroy()
        #   Label for Dgso entry
        self.root.label_dgso =\
            ttk.Label(self.root.frame_inputs, text='Параметр Dgso, м')
        self.root.label_dgso.grid(column=0, row=5, sticky=E, padx=5)
        #   Entry for Dgso
        self.root.d_gso = StringVar()
        self.root.entry_dgso =\
            ttk.Entry(self.root.frame_inputs, width=10,
                      textvariable=self.root.d_gso, validate='all',
                      validatecommand=(self.vcmd, '%P'))
        self.root.entry_dgso.grid(column=1, row=5, sticky=W)
        self.root.entry_dgso.insert(0, '2.1')
        #   Label for theta entry
        self.root.label_theta =\
            ttk.Label(self.root.frame_inputs, text='Параметр \u03B8, \u00b0')
        self.root.label_theta.grid(column=0, row=6, sticky=E, padx=5)
        #   Entry for theta
        self.root.theta = StringVar()
        self.root.entry_theta =\
            ttk.Entry(self.root.frame_inputs, width=10,
                      textvariable=self.root.theta, validate='all',
                      validatecommand=(self.vcmd, '%P'))
        self.root.entry_theta.grid(column=1, row=6, sticky=W)
        self.root.entry_theta.insert(0, '0')

    def output_widgets(self):
        super().output_widgets()
        self.k_widgets()
        self.dtheta_widgets()
        self.phi1_widgets()
        self.phimin_widgets()

    def k_widgets(self):
        #   Label for K entry
        self.root.label_k = \
            ttk.Label(self.root.frame_add_out, text='Параметр K')
        self.root.label_k.grid(column=0, row=7, sticky=E, padx=5)
        #   Entry for K
        self.root.entry_k = ttk.Entry(self.root.frame_add_out, width=10)
        self.root.entry_k.grid(column=1, row=7, sticky=W)

    def dtheta_widgets(self):
        #   Label for Dtheta entry
        self.root.label_dtheta = \
            ttk.Label(self.root.frame_add_out, text='Параметр Dtheta')
        self.root.label_dtheta.grid(column=0, row=8, sticky=E, padx=5)
        #   Entry for Dtheta
        self.root.entry_dtheta = ttk.Entry(self.root.frame_add_out, width=10)
        self.root.entry_dtheta.grid(column=1, row=8, sticky=W)

    def phi1_widgets(self):
        #   Label for φ1
        self.root.label_phi1 = ttk.Label(self.root.frame_add_out,
                                         text='Внеосевой угол \u03C61, \u00b0')
        self.root.label_phi1.grid(column=0, row=9, sticky=E, padx=5)
        #   Entry for φ1
        self.root.entry_phi1 = ttk.Entry(self.root.frame_add_out, width=10)
        self.root.entry_phi1.grid(column=1, row=9, sticky=W)

    def phimin_widgets(self):
        #   Label for φmin
        self.root.label_phimin = \
            ttk.Label(self.root.frame_add_out,
                      text='Внеосевой угол \u03C6min, \u00b0')
        self.root.label_phimin.grid(column=0, row=10, sticky=E, padx=5)
        #   Entry for φmin
        self.root.entry_phimin = ttk.Entry(self.root.frame_add_out, width=10)
        self.root.entry_phimin.grid(column=1, row=10, sticky=W)

    def get_inputs(self):
        AP8.get_inputs(self)
        self.eta = float(self.root.eta.get())
        self.d_eq = self.d
        self.d_gso = float(self.root.d_gso.get())
        self.theta = float(self.root.theta.get())

    def set_outputs(self, event):
        super().set_outputs(event)
        #   Set K
        self.root.entry_k.delete(0, END)
        self.root.entry_k.insert(0, self.kr)
        #   Set Dtheta
        self.root.entry_dtheta.delete(0, END)
        self.root.entry_dtheta.insert(0, self.d_thetar)
        #   Set φ1
        self.root.entry_phi1.delete(0, END)
        self.root.entry_phi1.insert(0, self.phi_1r)
        #   Set φmin
        self.root.entry_phimin.delete(0, END)
        self.root.entry_phimin.insert(0, self.phi_minr)

    def add_params(self):
        self.coeffa = 0
        super().add_params()
        self.k_calc()
        self.dtheta_calc()
        self.phir2_calc()
        self.phi1_calc()
        self.phimin_calc()
        self.g12_calc()
        self.phim2_calc()
        self.phib_calc()
        self.a_p[3:] = [self.g1r, self.phi_mr, self.phi_rr, self.phi_br,
                        self.kr, self.d_thetar, self.phi_1r, self.phi_minr]
        return self.a_p

    def k_calc(self):
        self.k = (self.d_gso/self.d_eq) ** 2
        self.kr = round(self.k, 2)

    def dtheta_calc(self):
        self.d_theta = (self.d_gso / self.k) / \
                       sqrt(sin(self.theta) ** 2 + (1 / self.k) ** 2 *
                            cos(self.theta) ** 2)
        self.d_thetar = round(self.d_theta, 2)

    def phir2_calc(self):
        self.phi_r = 15.85 * (self.d_theta / self.w) ** (-0.6)
        self.phi_rr = round(self.phi_r, 2)

    def phi1_calc(self):
        self.phi_1 = 0.9 * 114 * (self.d_theta / self.w) ** (-1.09)
        self.phi_1r = round(self.phi_1, 2)

    def phimin_calc(self):
        self.phi_min = max(self.phi_r, 118 * (self.d_theta / self.w) ** (-1.06))
        self.phi_minr = round(self.phi_min, 2)

    def g12_calc(self):
        self.g1 = 29 - 25 * log10(self.phi_r) + 3 * sin(self.theta) ** 2
        self.g1r = round(self.g1, 2)

    def phim2_calc(self):
        self.d = self.d_theta
        super().phim_calc()

    def phib_calc(self):
        if self.dw >= 46.8:
            self.phi_b = 10 ** (42 / 25)
        else:
            self.phi_b = 10 ** (37 / 25)
        self.phi_br = round(self.phi_b, 2)

    def offaxis_gain(self):
        [gmax, d, w, p] = [self.gmax, self.d_theta, self.w, self.phi]
        t = self.theta
        if self.phi_m < self.phi_r:
            if 0 <= p < self.phi_m:
                g = gmax - 2.5 * 10 ** (-3) * ((d / w) * p) ** 2
            elif self.phi_m <= p <= self.phi_r:
                g = self.g1
            elif self.phi_r < p < self.phi_min:
                g = min(self.g1, 29 + 3 * sin(t) ** 2 - 25 * log10(p))
        elif self.phi_m >= self.phi_r:
            if 0 <= p < self.phi_1:
                g = gmax - 2.5 * 10 ** (-3) * ((d / w) * p) ** 2
            elif self.phi_1 <= p < self.phi_min:
                g = max(gmax - 2.5 * 10 ** (-3) * ((d / w) * p) ** 2,
                             29 + 3 * sin(t) ** 2 - 25 * log10(p))
        if self.phi_min <= p <= 7:
            g = 29 + 3 * sin(t) ** 2 - 25 * log10(p)
        elif 7 < p <= 9.2:
            g = 7.9 + 3 * sin(t) ** 2 * ((9.2 - p) / 2.2)
        elif 9.2 < p <= self.phi_b:
            g = 32 - 25 * log10(p)
        if (self.d_eq / w) >= 46.8:
            if self.phi_b < p <= 180:
                g = - 10
        elif 15 <= (self.d_eq / w) < 46.8:
            if self.phi_b < p <= 70:
                g = - 5
            elif 70 < p <= 180:
                g = 0
        if p > 180:
            g = '\u03C6 > 180 !'
        self.g = g
        self.gr = round(self.g, 2) if type(self.g) == float else self.g
        return [self.gr]

    def ref_info(self, txt):
        txt = 'Альтернативная эталонная диаграмма направленности для антенн' \
              ' земных станций, используемых со спутниками на геостационарной' \
              ' орбите в целях применения при координации и/или оценке помех' \
              ' в диапазоне частот от 2 до 31 ГГц'
        Rec.ref_info(self, txt)


import traceback
import logger as log
import pandas as pd
from datetime import (
    datetime
)
from database import (
    DrugsMetaCollection,
    IngredientsCollection,
    LyophilizedCollection
)
from dash import (
    html,
    dcc
)

class MongoData(object):

    def __init__(self):
        self._ingredients_db_obj = None
        self._lyophilized_db_obj = None

    @property
    def ingredients_db_obj(self):
        if not self._ingredients_db_obj:
            self._ingredients_db_obj = IngredientsCollection()
        return self._ingredients_db_obj

    @property
    def lyophilized_db_obj(self):
        if not self._lyophilized_db_obj:
            self._lyophilized_db_obj = LyophilizedCollection()
        return self._lyophilized_db_obj

    def sanitize_list(self, item):
        if not isinstance(item, list):
            result = list()
            result.append(item)
            return result
        return item

    def get_table_data(self, start_date=None, end_date=None, product_search=None, active_search=None, inactive_search=None):

        search_query = dict()
        result = list()
        if product_search:
            result.extend(self.sanitize_list(product_search))
            search_query.update({
                'products': {'$in': result}
            })
        elif active_search:
            result.extend(self.sanitize_list(active_search))
            search_query.update({
                'active_ingredients_list': {'$in': result}
            })
        elif inactive_search:
            result.extend(self.sanitize_list(inactive_search))
            search_query.update({
                'inactive_ingredients_list': {'$in': result}
            })

        if not (start_date or search_query):
            today = datetime.today()
            start_date = datetime(today.year, today.month, 1)

            ##
            ## For Testing purposes, using start date from Jan 1, 2022.
            ##
            start_date = datetime(2022, 1, 1)
            search_query.update({'date': {'$gte': start_date}})
        if end_date:
            search_query['date'].update({
                '$lt': end_date
            })

        records_rows = list()
        record_id = None
        try:
            for records in self.lyophilized_db_obj.get_records(query=search_query):
                record_id = records.get('_id')
                labels_rows = list()
                active_ingredients_rows = list()
                active_strength_rows = list()
                inactive_ingredients_rows = list()
                inactive_strength_rows = list()
                for setid, labels in records.get('set_ids', {}).items():
                    row = list()
                    active = records.get('active_ingredients', {}).get(setid, [])
                    inactive = records.get('inactive_ingredients', {}).get(setid, [])
                    active_len = len(active)
                    inactive_len = len(inactive)
                    row_span = max(active_len, max(inactive_len, 0))

                    if row_span == 0:
                        continue

                    row.append(dcc.Markdown(f"[{labels.get('title')}]({labels.get('web_url')})"))
                    row.append(row_span)

                    labels_rows.append(row)
                    for i in range(1, row_span):
                        labels_rows.append(list())

                    for i in range(0, row_span):
                        active_name_row = ["", 1]
                        active_strength_row = ["", 1]
                        inactive_name_row = ["", 1]
                        inactive_strength_row = ["", 1]

                        if i < active_len:
                            active_name_row = [active[i].get('name', ""), 1]
                            active_strength_row = [active[i].get('strength', ""), 1]

                        if i < inactive_len:
                            inactive_name_row = [inactive[i].get('name', ""), 1]
                            inactive_strength_row = [inactive[i].get('strength', ""), 1]

                        active_ingredients_rows.append(active_name_row)
                        active_strength_rows.append(active_strength_row)
                        inactive_ingredients_rows.append(inactive_name_row)
                        inactive_strength_rows.append(inactive_strength_row)

                total_span = len(labels_rows)
                if total_span <= 0:
                    total_span = 1

                app_number = records.get('application_number')
                app_list = [app_number, total_span]

                product_outer_list = list()
                for product in records.get('products', []):
                    product_rows = [product.upper(), html.Br()]

                product_rows.append(app_number)
                product_outer_list = [product_rows, total_span, "products"]

                date_rows = list()
                if not start_date:
                    date_value = list()
                    for date in records.get('date'):
                        date_value.append(date.strftime('%m-%d-%Y'))
                        date_value.append(html.Br())
                else:
                    date_value = ""
                    for date in records.get('date'):
                        if date.year >= start_date.year and date.month >= start_date.month:
                            if end_date:
                                if date.year <= end_date.year and date.month <= end_date.month:
                                    date_value = date.strftime('%m-%d-%Y')
                                    break
                            else:
                                date_value = date.strftime('%m-%d-%Y')
                                break

                date_rows = [date_value, total_span, "dates"]
                company_rows = [records.get('company'), total_span]

                current_row = [date_rows, product_outer_list, company_rows]

                for i in range(0, len(labels_rows)):
                    if labels_rows[i]:
                        current_row.append(labels_rows[i])
                    current_row.extend([active_ingredients_rows[i], active_strength_rows[i]])
                    current_row.extend([inactive_ingredients_rows[i], inactive_strength_rows[i]])

                    records_rows.append(current_row)
                    current_row = list()

        except Exception as exc:
            log.do_error(f"Exception occurred while fetching records from database for record: {record_id}, error: {str(exc)}, traceback: {traceback.format_exc()}")

        return records_rows

    def get_search_bar_data(self):
        products = set()
        active_ingredients = set()
        inactive_ingredients = set()
        for records in self.lyophilized_db_obj.get_records():
            products.update(set(records.get('products', [])))
            active_ingredients.update(set(records.get('active_ingredients_list', [])))
            inactive_ingredients.update(set(records.get('inactive_ingredients_list', [])))

        return (list(products), list(active_ingredients), list(inactive_ingredients))

    def generate_occurences_data(self):
        try:
            active_ingredients_dict = dict()
            inactive_ingredients_dict = dict()
            products_set = set()
            for record in self.lyophilized_db_obj.get_records():
                products = record.get('products', [])
                active_ingredients = record.get('active_ingredients_list', [])
                inactive_ingredients = record.get('inactive_ingredients_list', [])

                date = record.get('date', [])[0]
                date_value = date.strftime('%Y')

                for product in products:
                    for ingredient in active_ingredients:
                        id = product + ingredient
                        if id not in products_set:
                            products_set.add(id)
                            existing_val = active_ingredients_dict.get(ingredient, {}).get(date_value, 0)
                            total_val = active_ingredients_dict.get(ingredient, {}).get('total_count', 0)
                            if active_ingredients_dict.get(ingredient):
                                active_ingredients_dict[ingredient].update({date_value: existing_val + 1})
                                active_ingredients_dict[ingredient].update({'total_count': total_val + 1})
                            else:
                                active_ingredients_dict[ingredient] = {}
                                active_ingredients_dict[ingredient].update({date_value: 1})
                                active_ingredients_dict[ingredient].update({'total_count': 1})

                    for ingredient in inactive_ingredients:
                        id = product + ingredient
                        if id not in products_set:
                            products_set.add(id)
                            existing_val = inactive_ingredients_dict.get(ingredient, {}).get(date_value, 0)
                            total_val = inactive_ingredients_dict.get(ingredient, {}).get('total_count', 0)
                            if inactive_ingredients_dict.get(ingredient):
                                inactive_ingredients_dict[ingredient].update({date_value: existing_val + 1})
                                inactive_ingredients_dict[ingredient].update({'total_count': total_val + 1})
                            else:
                                inactive_ingredients_dict[ingredient] = {}
                                inactive_ingredients_dict[ingredient].update({date_value: 1})
                                inactive_ingredients_dict[ingredient].update({'total_count': 1})
        except Exception as exc:
            log.do_error(f"Exception occurred while fetching ingredients from Database, error: {str(exc)}")

        return {'active': active_ingredients_dict, 'inactive': inactive_ingredients_dict}

    def get_timeseries_dataframe(self):

        time_series_data = self.generate_occurences_data()
        inactive_list = list()
        inactive_ing_list = list()
        active_list = list()
        active_ing_list = list()
        inactive_dates_list = list()
        active_dates_list = list()
        inactive_occ_list = list()
        active_occ_list = list()
        active_total_count = list()
        inactive_total_count = list()

        for ingredient, value in time_series_data.get('active', {}).items():
            active_ing_list.append(ingredient)
            for key, count in value.items():
                if key == "total_count":
                    active_total_count.append(count)
                else:
                    active_dates_list.append(int(key))
                    active_list.append(ingredient)
                    active_occ_list.append(count)

        for ingredient, value in time_series_data.get('inactive', {}).items():
            inactive_ing_list.append(ingredient)
            for key, count in value.items():
                if key == "total_count":
                    inactive_total_count.append(count)
                else:
                    inactive_dates_list.append(int(key))
                    inactive_list.append(ingredient)
                    inactive_occ_list.append(count)

        active_time_series = pd.DataFrame({
            "Ingredient": active_list,
            "Year": active_dates_list,
            "Occurences": active_occ_list
        })

        inactive_time_series = pd.DataFrame({
            "Ingredient": inactive_list,
            "Year": inactive_dates_list,
            "Occurences": inactive_occ_list
        })

        active_chart = pd.DataFrame({
            "Ingredient": active_ing_list,
            "Occurences": active_total_count
        })

        inactive_chart = pd.DataFrame({
            "Ingredient": inactive_ing_list,
            "Occurences": inactive_total_count
        })

        active_time_series = active_time_series.sort_values(by=['Year'])
        inactive_time_series = inactive_time_series.sort_values(by=['Year'])
        active_chart = active_chart.sort_values(by=['Occurences'])
        inactive_chart = inactive_chart.sort_values(by=['Occurences'])

        return (active_chart, inactive_chart, active_time_series, inactive_time_series)


"""The module contains test functions to test Tankerkoenig API wrapper."""

import tempfile
import os

from unittest import TestCase
from unittest.mock import MagicMock, patch

from homemonitoring.tankerkoenig import TankerKoenig

class TestTankerKoenig(TestCase):
    """TestTankerKoenig contains the test cases for the TankerKoenig class."""

    RESPONSE_DETAIL_FIXTURES = {
        "id_1": {
            "station": {"id": "id_1"}
        }
    }

    @patch("requests.get")
    def test_call_api(self, mock):
        """Checks function that composes API call."""
        apikey = 'apikey'
        ids = "1,2,3"
        api = TankerKoenig(apikey)
        api._call_api('details.php', ids=ids)
        mock.assert_called_with(
            'https://creativecommons.tankerkoenig.de/json/details.php',
            params={'ids': ids, 'apikey': apikey}
        )

    def test_get_station_details(self):
        """Checks get station details."""
        station_id = 'id_1'
        with tempfile.TemporaryDirectory() as tmp_dir:
            api = TankerKoenig('api-key', cache_dir=tmp_dir)

            def mock_call_api(endpoint, **params):
                return self.RESPONSE_DETAIL_FIXTURES[params['id']]
            api._call_api = MagicMock(side_effect=mock_call_api)

            # call "API"
            got = api.get_station_details(station_id, force_update=True)
            expected = {'station': {'id': 'id_1'}}
            self.assertDictEqual(got, expected)
            assert os.path.exists(os.path.join(tmp_dir, f'{station_id}.json'))
            api._call_api.assert_called_once()

            # this time from file
            got = api.get_station_details(station_id, force_update=False)
            self.assertDictEqual(got, expected)
            api._call_api.assert_called_once()  # API should not have been called again

            # this time force update
            got = api.get_station_details(station_id, force_update=True)
            self.assertDictEqual(got, expected)
            assert api._call_api.call_count == 2  # API should not have been called again


#!/usr/bin/env python
import boto3
import subprocess
#output = subprocess.call(['/home/ansadmin/nagaratest/docker.sh'])
import time
ec2 = boto3.resource('ec2')
import yaml
config = yaml.load(open('config.yml'))

ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
    ImageId = config['ImageId'],
    MinCount = config['MinCount'],
    MaxCount = config['MaxCount'],
    InstanceType = config['InstanceType'],
    KeyName = config['KeyName'],
    SubnetId = config['SubnetId'],
    UserData = '#!/bin/bash'+'\n'+'sudo su'+'\n'+'yum update -y'+'\n'+'yum install docker -y'+'\n'+'yum install git -y'+'\n'+'git clone https://github.com/asquarezone/DockerZone.git /home/ec2-user/tomcat_test'+'\n'+'sleep 15'+'\n'+'service docker start'+'\n'+'docker build -t tomcat_test /home/ec2-user/tomcat_test/ImageCreation/DockerFiles/tomcat_demo'+'\n'+'docker run -itd -p 8080:8080 tomcat_test')
print (instance[0].id)

instance = instance[0]
# Wait for the instance to enter the running state
instance.wait_until_running()

# Reload the instance attributes
instance.load()
print (instance.public_ip_address)
print ( "url is: " + instance.public_dns_name + ":8080" )


import requests
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import re

with open('raw_kaggle_huffpost.json', 'r') as file:
    entries = file.read().lower().split("\n")

def create_cookies():
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
    driver.get('https://www.huffpost.com/entry/immigrant-children-separated-from-parents_n_5b087b90e4b0802d69cb4070')
    driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[4]/div/div[2]/form[1]/div/input').click()
    cookies_list = driver.get_cookies()
    cookies_dict = {}
    for cookie in cookies_list:
        cookies_dict[cookie['name']] = cookie['value']

    return cookies_dict

def parse_response(response, new):
    soup = BeautifulSoup(response.text, 'html.parser')

    title = soup.find(attrs={"class": "headline__title"})
    if title is not None:
        title = title.getText()
    else:
        title = soup.find(attrs={"class": "headline"}).getText()

    subtitle = soup.find(attrs={"class": "headline__subtitle"})
    if subtitle is not None:
        subtitle = subtitle.getText()
    else:
        subtitle = soup.find(attrs={"class": "dek"})
        if subtitle is not None:
            subtitle = subtitle.getText()

    date = soup.find(attrs={"class": "timestamp"})
    if date is not None:
        date = date.getText().split()[0]

    text = ""

    paragraphs = soup.find_all(attrs={"class": "content-list-component yr-content-list-text text"})
    if len(paragraphs) == 0:
        paragraphs = soup.find_all(attrs={"class": "cli cli-text"})

    for paragraph in paragraphs:
        text += paragraph.getText()
        text += '\n'

    return {
        'title': title,
        'subtitle': subtitle,
        'category': new['category'],
        'text': text,
        'date': date,
        'url': url
    }

regex = re.compile(
    r'^(?:http|ftp)s?://'  # http:// or https://
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain...
    r'localhost|'  # localhost...
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
    r'(?::\d+)?'  # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)

cookies = create_cookies()
headers = {
    'Referer': 'https://www.huffpost.com/entry/immigrant-children-separated-from-parents_n_5b087b90e4b0802d69cb4070',
    'Sec-Fetch-Mode': 'no-cors',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}

cached = pd.read_csv('datasets/cached_news.csv')
network = pd.read_csv('datasets/network_news.csv')

df = pd.concat([cached, network]).drop_duplicates().reset_index(drop=True)
df.to_csv('datasets/cached_news.csv', index=False)
network.iloc[0:0].to_csv('datasets/network_news.csv', index=False)

data_source = []
news = []

for entry in entries:
    new = eval(entry)

    if new['category'] in ['politics', 'entertainment', 'queer voices',
                           'business', 'comedy', 'sports', 'black voices',
                           'the worldpost', 'women', 'impact', 'crime', 'media',
                           'weird news', 'green', 'religion', 'science', 'world news',
                           'tech', 'arts & culture', 'latino voices', 'education']:
        news.append(new)

index_from_network = 0
for i, new in enumerate(news):
    if i % 50 == 0:
        print(i, index_from_network, len(news))

    url = new['link']

    row = df[df.url == url]
    if not row.empty:
        continue

    try:
        index_from_network = index_from_network + 1
        if re.match(regex, url) is None:
            print('invalid url:', url)
            continue

        response = requests.get(url, headers=headers, cookies=cookies)
        new = parse_response(response, new)
        data_source.append(new)
        if index_from_network % 100 == 0:
            print('saving dataset', index_from_network)
            pd.DataFrame(data_source).to_csv('datasets/network_news.csv', index=False)
    except:
        print('error when trying to process', url)


import unittest
from parlai.core.agents import create_agent
from parlai.core.params import ParlaiParser
from parlai.core.worlds import create_task
from parlai.core.teachers import DialogTeacher, register_teacher
import parlai.utils.testing as testing_utils

@register_teacher('teacher1')
class Teacher1(DialogTeacher):
    def __init__(self, opt, shared=None):
        opt['datafile'] = 'filler'
        self.id = 'teacher1'
        super().__init__(opt, shared)

    def setup_data(self, datafile):
        for i in range(100):
            yield (str(i), str(i)), False
            yield (str(i), str(i)), True

@register_teacher('teacher2')
class Teacher2(DialogTeacher):
    def __init__(self, opt, shared=None):
        opt['datafile'] = 'filler'
        self.id = 'teacher2'
        super().__init__(opt, shared)

    def setup_data(self, datafile):
        for i in range(10):
            yield (str(i), str(i)), True

class TestMultiworld(unittest.TestCase):
    @testing_utils.retry(ntries=10)
    def test_equal(self):
        opt = ParlaiParser(True, True).parse_kwargs(
            task='teacher1,teacher2',
            multitask_weights='1,1',
            model='fixed_response',
            fixed_response='None',
            datatype='train',
            batchsize=1,
        )
        agent = create_agent(opt)
        world = create_task(opt, agent)

        for _ in range(1000):
            world.parley()

        report = world.report()
        ratio = report['teacher1/exs'].value() / report['teacher2/exs'].value()
        assert ratio > 1.7
        assert ratio < 2.3

    @testing_utils.retry(ntries=10)
    def test_stochastic(self):
        opt = ParlaiParser(True, True).parse_kwargs(
            task='teacher1,teacher2',
            multitask_weights='stochastic',
            model='fixed_response',
            fixed_response='None',
            datatype='train',
            batchsize=1,
        )
        agent = create_agent(opt)
        world = create_task(opt, agent)

        for _ in range(1000):
            world.parley()

        report = world.report()
        # stochastic so wide range
        ratio = report['teacher1/exs'].value() / report['teacher2/exs'].value()
        assert ratio > 18
        assert ratio < 45

    def test_with_stream(self):
        """
        Test that multi-tasking works with datatype train:stream.
        """
        task1 = 'integration_tests:infinite_train'
        task2 = 'integration_tests:short_fixed'
        opt = ParlaiParser(True, True).parse_kwargs(
            task=f'{task1},{task2}',
            model='fixed_response',
            fixed_response='Hello!',
            datatype='train:stream',
            batchsize=16,
        )
        agent = create_agent(opt)
        world = create_task(opt, agent)

        for i in range(100):
            world.parley()
            if i % 10 == 0 and i > 0:
                teacher_acts, _ = world.get_acts()
                for act in teacher_acts:
                    act_id = act.get('id')
                    assert 'text' in act, f'Task {act_id} acts are empty'
                report = world.report()
                for task in [task1, task2]:
                    err = f'Task {task} has no examples on iteration {i}'
                    assert f'{task}/exs' in report, err
                    exs = report[f'{task}/exs'].value()
                    assert exs > 0, err
                world.reset_metrics()

    def test_with_ordered(self):
        """
        Test that multi-tasking works deterministically with datatype train:ordered.
        """

        opt = ParlaiParser(True, True).parse_kwargs(
            task='teacher1,teacher2',
            multitask_weights='1,1',
            model='fixed_response',
            fixed_response='None',
            datatype='train:ordered',
            batchsize=1,
        )
        multiworld1 = create_task(opt, create_agent(opt))
        multiworld2 = create_task(opt, create_agent(opt))

        while not (multiworld1.epoch_done() or multiworld2.epoch_done()):
            multiworld1.parley()
            acts1 = multiworld1.get_acts()

            multiworld2.parley()
            acts2 = multiworld2.get_acts()

            self.assertEqual(len(acts1), len(acts2))
            assert all([act1 == act2 for act1, act2 in zip(acts1, acts2)])

        assert multiworld1.epoch_done() and multiworld2.epoch_done()


import re
import unitydoc

#rename_command = re.compile("^!rename +(.+)$")
#valid_name = re.compile("^[A-Za-z_]+$")

def response(self, prefix, command, params, trailing):
    if command == 'PRIVMSG' and params == self.channel and trailing.startswith('!define'):
        self.send("PRIVMSG {} :{}".format(self.channel,
            "Okay, let me google that for you.. NOT!"))
    elif command == 'PRIVMSG' and params == self.channel and trailing.startswith('!save'):
        self.send("PRIVMSG {} :{}".format(self.channel,
            "Why not write it down yourself?"))
    elif command == 'PRIVMSG' and params == self.channel and trailing.startswith('!help'):
        self.send("PRIVMSG {} :{}".format(self.channel,
            "Did you really think I would help you? Bwahahahaahaaaa!"))
    elif command == 'PRIVMSG' and trailing.startswith('!unity'):
        if params == self.channel:
            recipient = self.channel
        else:
            recipient = prefix.split('!', 1)[0]
        if trailing.strip() == '!unity':
            self.send("PRIVMSG {} :{}".format(recipient,
                "http://docs.unity3d.com/ScriptReference/"))
        else:
            query = re.sub(r"^.unity ?", "", trailing)
            for score, title, page, summary in unitydoc.PerformSearch(query)[0:3]:
                self.send("PRIVMSG {} :{}".format(recipient,
                    "<http://docs.unity3d.com/ScriptReference/{}.html> {}".format(page, summary)))

#    elif command == 'PRIVMSG' and params == self.channel and trailing.startswith('!rename'):
#        match = rename_command.match(trailing)
#        if match and valid_name.match(trailing):
#            self.send("NICK {}".format(match.groups()[0]))
#        else:
#            self.send("PRIVMSG {} :{}".format(self.channel, "Not going to name myself like that!"))
    elif command == 'PRIVMSG' and params == self.channel and trailing.startswith('!'):
        self.send("PRIVMSG {} :{}".format(self.channel,
            "Ask for !help"))

def levenshteinDistance(s1,s2):
    if len(s1) > len(s2):
        s1,s2 = s2,s1
    distances = range(len(s1) + 1)
    for index2,char2 in enumerate(s2):
        newDistances = [index2+1]
        for index1,char1 in enumerate(s1):
            if char1 == char2:
                newDistances.append(distances[index1])
            else:
                newDistances.append(1 + min((distances[index1],
                                             distances[index1+1],
                                             newDistances[-1])))
        distances = newDistances
    return distances[-1]


import sys
from collections import deque

def bfs(node):
    queue = deque()

    queue.append(node)
    visited[node] = True

    while len(queue) != 0:
        cur_node = queue.popleft()

        for linked_node in adj[cur_node]:
            if not visited[linked_node]:
                visited[linked_node] = True
                queue.append(linked_node)

if __name__ == "__main__":
    N, M = map(int, sys.stdin.readline().split())
    adj = [[] for _ in range(N + 1)]
    visited = [False] * (N + 1)

    for _ in range(M):
        srt, end = map(int, sys.stdin.readline().split())

        adj[srt].append(end)
        adj[end].append(srt)

    cnt = 0

    for node in range(1, N + 1):
        if not visited[node]:
            bfs(node)
            cnt += 1

    print(cnt)


import argparse
import logging
from .system import system_parser
from .network import network_parser
from .stress import stress_parser

log_debug = logging.getLogger("debugLog")

def get_parser(parent=None):
    # connection with main parser
    if not parent:
        anomaly_inject = argparse.ArgumentParser(description='Anomaly Injection', prog='anomaly')
        anomaly_inject.add_argument("--debug", help="Run command in debug mode", dest="debug", action='store_true')

    else:
        anomaly_inject = parent.add_parser('anomaly', help='Anomaly Injection')

    # Anomaly commands
    anomaly_parser = argparse.ArgumentParser(add_help=False)
    anomaly_parser.add_argument('--host', help='Hostname or ip of target', metavar='[hostname]', dest="target_host")

    # Anomaly parsers
    anomaly_subcommands = anomaly_inject.add_subparsers(dest="target")
    anomaly_container = anomaly_subcommands.add_parser('container', prog='container',
                                                       parents=[anomaly_parser],
                                                       usage=f"{anomaly_inject.prog} %(prog)s",
                                                       description="Container anomaly injector, "
                                                                   "must specify host and name or id")

    anomaly_node = anomaly_subcommands.add_parser('node', prog='node', usage=f"{anomaly_inject.prog} %(prog)s",
                                                  parents=[anomaly_parser],
                                                  description="Node anomaly injector, "
                                                              "must specify host and name or id")

    anomaly_network = anomaly_subcommands.add_parser('network', prog='network', usage=f"{anomaly_inject.prog} %(prog)s",
                                                     parents=[anomaly_parser],
                                                     description="Network anomaly injector, "
                                                                 "must specify host and interface ")
    anomaly_network.add_argument('--interface', help='Name of interface', type=str, metavar='[NAME]',
                                 dest="target_interface")
    anomaly_deployment = anomaly_subcommands.add_parser('deployment', prog='deployment',
                                                        usage=f"{anomaly_inject.prog} %(prog)s",
                                                        parents=[anomaly_parser],
                                                        description="Deployment anomaly injector, "
                                                                    "must specify host and name or id")

    # Container Anomaly
    a_c_group = anomaly_container.add_mutually_exclusive_group(required=True)
    a_c_group.add_argument('--name', '-n', help='Name of container', type=str, metavar='[name]', dest="target_name")
    a_c_group.add_argument('--id', help='Container ID', type=str, metavar='[id]', dest="target_id")
    cont_anomalies = anomaly_container.add_subparsers(dest="anomaly_type")
    cont_system = cont_anomalies.add_parser("system", parents=[system_parser])
    cont_net = cont_anomalies.add_parser("network", parents=[network_parser])
    cont_stress = cont_anomalies.add_parser("stress", parents=[stress_parser])

    # Node Anomaly
    node_anomalies = anomaly_node.add_subparsers(dest="anomaly_type")
    node_system = node_anomalies.add_parser("system", parents=[system_parser])
    node_net = node_anomalies.add_parser("network", parents=[network_parser])
    node_stress = node_anomalies.add_parser("stress", parents=[stress_parser], )

    # Deployment Anomaly
    dep_anomalies = anomaly_deployment.add_subparsers(dest="anomaly_type")
    dep_system = dep_anomalies.add_parser("system", parents=[system_parser])
    dep_stress = dep_anomalies.add_parser("stress", parents=[stress_parser])
    dep_net = dep_anomalies.add_parser("network", parents=[network_parser])

    # Network Anomaly
    net_anomalies = anomaly_network.add_subparsers(dest="anomaly_type")
    net_stress = net_anomalies.add_parser("stress", parents=[stress_parser])
    net_system = net_anomalies.add_parser("system", parents=[system_parser])
    net_net = net_anomalies.add_parser("network", parents=[network_parser])

    if parent:
        return parent
    else:
        return anomaly_inject

def parse_arguments(args):
    args = vars(args)
    unpacked = unpack_targets(args)
    unpacked.update(unpack_params(args))
    unpacked['name'] = args.get('anomaly_name', args['anomaly_type'])
    log_debug.debug("Unpacked arguments" + str(unpacked))
    return unpacked

def unpack_targets(args):
    _unpacked = dict()
    for arg in args:
        if "target" in arg and args[arg]:
            param_split = arg.split("_")
            if len(param_split) > 1:
                _unpacked[param_split[1]] = args[arg]
            else:
                _unpacked[arg] = args[arg]
    return {"target": _unpacked}

def unpack_params(args):
    _unpacked = dict()
    for arg in args:
        if "param" in arg and args[arg]:
            param_split = arg.split("_")
            if len(param_split) > 1:
                _unpacked[param_split[1]] = args[arg]
            else:
                _unpacked[arg] = args[arg]
    return {"params": _unpacked}


import logging

from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
from markdown.util import etree

logger = logging.getLogger(__name__)

def build_url(label, base, end):
    """ Build a url from the label, a base, and an end. """
    return '%s%s%s' % (base, label, end)

def build_label(label):
    return label.split('/')[-1]

def url_exists(url):
    print("ORIGINAL URL")
    return True

class WikiLinkExtension(Extension):

    def __init__(self, *args, **kwargs):
        self.config = {
            'base_url': ['/', 'String to append to beginning or URL.'],
            'end_url': ['/', 'String to append to end of URL.'],
            'html_class': ['wikilink', 'CSS hook. Leave blank for none.'],
            'build_url': [build_url, 'Callable that formats URL from label.'],
            'build_label': [build_label, 'Callable that formats the label.'],
            'url_exists': [url_exists,
                           'Callable that returns wether a URL exists']
        }

        super(WikiLinkExtension, self).__init__(*args, **kwargs)

    def extendMarkdown(self, md, md_globals):
        # append to end of inline patterns
        WIKILINK_RE = r'\[\[([A-Za-z0-9_ -/]+)\]\]'
        wikilinkPattern = MDWikiLinks(WIKILINK_RE, self.getConfigs())
        wikilinkPattern.md = md

        md.inlinePatterns.add('mdwikilink', wikilinkPattern, "<not_strong")

class MDWikiLinks(Pattern):
    def __init__(self, pattern, config):
        super().__init__(pattern)
        self.config = config

    def handleMatch(self, m):
        if m.group(2).strip():
            a = etree.Element('a')
            base_url, end_url, html_class = self._getMeta()
            label = m.group(2).strip()
            a.set('title', label)
            url = self.config['build_url'](label, base_url, end_url)
            label_short = self.config['build_label'](label)
            a.set('href', url)

            if not self.config['url_exists'](url):
                a.set('class', 'missing')
                a.text = label
            else:
                a.text = label_short
        else:
            a = ''
        return a

    def _getMeta(self):
        """ Return meta data or config data. """
        base_url = self.config['base_url']
        end_url = self.config['end_url']
        html_class = self.config['html_class']
        if hasattr(self.md, 'Meta'):
            if 'wiki_base_url' in self.md.Meta:
                base_url = self.md.Meta['wiki_base_url'][0]
            if 'wiki_end_url' in self.md.Meta:
                end_url = self.md.Meta['wiki_end_url'][0]
            if 'wiki_html_class' in self.md.Meta:
                html_class = self.md.Meta['wiki_html_class'][0]
        return base_url, end_url, html_class