from django.test import TestCase
from api import models

def create_channel(name):

    return models.Channel.objects.create(name=name)

def create_category(channel, category, parent_category):

    return models.Category.objects.update_or_create(
            name=category,
            parent=parent_category,
            channel=channel
        )

class ChannelTest(TestCase):

    def test_str(self):

        name = 'test_channel'
        channel = create_channel(name)
        self.assertEqual(channel.__str__(), name)

class CategoryTest(TestCase):

    def test_str(self):

        channel_name = 'test_channel_category'
        parent_category_name = 'test_category_parent'
        category_name = 'test_category'

        channel = create_channel(channel_name)

        parent_category, _ = create_category(channel, parent_category_name,
                                             None)
        category, _ = create_category(channel, category_name, parent_category)

        self.assertEqual(category.__str__(),
                         "%s:%s:%s" % (channel_name,
                                       category_name,
                                       parent_category_name))


from Tax_Analysis import Taxbase, Progressive_Tax, gini, plot_gini

rates = [0, 10, 20, 30]   # 10%  20%  30%

brackets = [0,
            10000,        # first 10,000
            30000,        # next  20,000
            70000]        # next  40,000

North_City = Taxbase("NorthCityResidents.csv")
South_City = Taxbase("SouthCityResidents.csv")
new_tax = Progressive_Tax(rates, brackets)
amount_paid = South_City.get_capital_income_progressive_tax(new_tax)
income = South_City.regular_income_table.values
plot_gini(income)
print(gini(income))


import json
import re

def getRightLocationUser(data):

        # make a pattern to recognise Paris or paris.
    p = re.compile(".*paris.*", re.I)

    rightUsersCount = 0
    users = {}
    for d in data:
        location = data[d]['location']
        r = p.search(location)
        if r != None:
            users[d] = data[d]
            rightUsersCount += 1

    print('before filter', len(data))
    print('after filter', rightUsersCount)
    return users


import gps_tracker
import detector
import RPi.GPIO as GPIO
import time
GPIO.cleanup()
gps_tracker.play_sound_notification('welcome')

while True:
    try:
        current_location = gps_tracker.get_current_location()
        if current_location != (0.0, 0.0):
            break

    except:
        continue

gps_tracker.play_sound_notification("ready")

street_crosser = 13
GPIO.setmode(GPIO.BCM)
GPIO.setup(street_crosser, GPIO.IN)

while True:
    if not(GPIO.input(street_crosser)):
        time.sleep(0.5)
        detector.street_crosser()
        GPIO.cleanup()
        break


""" Integration tests of the gps.py module """

import pandas as pd

from causal_curve import GPS

def test_full_gps_flow(dataset_fixture):
    """
    Tests the full flow of the GPS tool
    """

    gps = GPS(
        treatment_grid_num=10,
        lower_grid_constraint=0.0,
        upper_grid_constraint=1.0,
        spline_order=3,
        n_splines=10,
        max_iter=100,
        random_seed=100,
        verbose=True,
    )
    gps.fit(
        T=dataset_fixture["treatment"],
        X=dataset_fixture[["x1", "x2"]],
        y=dataset_fixture["outcome"],
    )
    gps_results = gps.calculate_CDRC(0.95)

    assert isinstance(gps_results, pd.DataFrame)
    check = gps_results.columns == ["Treatment", "CDRC", "Lower_CI", "Upper_CI"]
    assert check.all()


huruf = input ("Masukkan input kata: ")
print("1. Huruf Prima")
print("2. Palindrom")
print("3. Reverse")
print("4. Keluar dari Program")
b = input("Pilihan anda: ")

x = int(len(huruf))
i=0
if b == "1":
    while i<=x:
        for j in range (2, x-1):
            if i%j ==0:
                print(huruf[i])
        i+=1

elif b == '2':
    if huruf[::-1] == huruf:
        print("PALINDROM")
    else:
        print("BUKAN PALINDROM")

elif b =='3':
    for i in range (x-1, -1, -1):
        print(huruf[i], end=" ")

elif b == '4':
    while True:
        break


#!/usr/bin/env python

# Program created to search for delta scuti stars in Fernandez-Trincado et al. (2015a) data: http://cdsads.u-strasbg.fr/abs/2015A%26A...574A..15F
# Author: J. G. Fernandez-Trincado
# Status: Article, ApJ, 2016.
# Last update: 2015 November 13
# This program is a modified version of the original, supported by: https://github.com/keatonb/pyperiod04/blob/master/pyperiod04.py
# J. G. Fernandez-Trincado also thanks Patrick Lenz by the support of "period04-v1.2.0-co140-linux-64bit.csh" (Update: November 2015)

import os
import numpy as np
import scipy as sc

def period04(lc, copy_lc='temp.lc', output_ft='temp.ft', output_project='project.p04', batchfile='batch_temp.bat'):

        """
        [1] lc: array, col1 = time, col2 = mag, col3 = error_mag
        """

        # Constant values ...
        fmin, fmax = str(0), str(50)                                # range of frequencies: mim to max
        np.savetxt(copy_lc,lc)                                      # Write out the light curve file

        # The new version of Period04 was supported by Patrick Lenz.
        f = open(batchfile,'w')
        f.write('import to '+copy_lc+'\n')
        f.write('fourier '+fmin+' '+fmax+' o y\n')
        # f.write('addharmonics 3 \n')
        f.write('fit o 2 \n')
        f.write('saveproject '+output_project+'\n')
        f.write('savefourier '+fmin+' '+fmax+' o y '+output_ft+'\n')
        f.write('exit\n')
        f.close()

        os.system('period04 -batch='+batchfile)                     # Run the batch file
        ft = np.loadtxt(output_ft)                                  # Read in the FT
        if copy_lc   == 'temp.lc': os.remove(copy_lc)               # clean up light curve copy
        if batchfile == 'batch_temp.bat': os.remove(batchfile)      # clean up batch file
        return ft                                                   # Return the Period04

lc = sc.genfromtxt('lc.dat')
period04(lc, copy_lc='temp.lc', output_ft='fourier.ft', output_project='project.p04', batchfile='batch_temp.bat')


import sys

def readFile(filename):
	_file = open(filename, "r")
	resultList = list()
	while True:
		line = _file.readline()
		if not line:
			break
		line = line.strip().split()
		# resultList.append(str(int(line[0])+1)+"\t"+str(int(line[1])+1)+"\t0.0")
		resultList.append(str(line[0])+"\t"+str(line[1])+"\t0.0")
	_file.close()
	return resultList

def print2File(filename, targetList):
	_file = open(filename, "w")
	for result in targetList:
		_file.write(result+"\n")
	_file.close()

if __name__ == '__main__':
	if len(sys.argv)<3:
		print("usage : python preprocessTest.py [input filename] [output filename]")
		exit(1)
	targetFilename = sys.argv[1]
	testFilename = sys.argv[2]

	resultList = readFile(targetFilename)
	print2File(testFilename, resultList)


from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import os

datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')

path = "C:\\Dev\\AI\\2U\\data\\train\\ducks"
aug_value = 10
j = 0
for idx, filename in enumerate(os.listdir(path)):
    j+=1
    if j == 60:
        break
    img = load_img(os.path.join(path, filename))
    x = img_to_array(img)
    x = x.reshape((1,) + x.shape)
    print ("Augmenting image " + filename + " " + str(aug_value) + " times.")
    i = 0
    for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='data/augmentedDucksTrain',
                          save_prefix='duck',
                          save_format='jpg'):
        i += 1
        if (i > aug_value):
            break


#!/usr/bin/env python

import os
import sys
from toollib.files import ParameterParser

def printCols(data, headers = None, stream = sys.stdout, min_lengths = None):
   rows = []
   if headers is not None:
      rows.append(headers)
   rows.extend(data)
   # Nothing to print!
   if len(rows) == 0:
      return None

   # Set the initial lengths
   lengths = min_lengths if min_lengths is not None else [0] * len(rows[0])
   # Run once over the rows to find the maximum column lengths
   for row in rows:
      lengths = [max(lengths[c], len(v)) for c,v in enumerate(row)]
   # Print the rows using maximum column lengths
   for row in rows:
      print >>stream, ' '.join(['{:>{width}}'.format(v, width = lengths[c]) for c,v in enumerate(row)]).rstrip()

   return lengths

def glob(instream, header, rows_per_glob):
   rows = []
   if header is not None:
      rows.append(header)
   for row in instream:
      if len(rows) == rows_per_glob:
         yield rows
         rows = []
      rows.append(row)
   if len(rows) > 0:
      yield rows

if __name__ == "__main__":
    pp = ParameterParser('Pretty print file', columns = 0, group = False, append = False, ordered = False)
    pp.parser.add_argument('-k', '--k', type = int, default = 30, help = 'frequency in rows to recalculate column width')
    args = pp.parseArgs()

    header = args.infile.header if args.header else None
    length = None
    for rows in glob(args.infile, header, args.k):
        length = printCols(rows, min_lengths = length)


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

"""
    crckit.py

    Additional classes and functions for CRCKit.

    Classes:
      * Arguments       parsing console line arguments;
      * ConsoleTable    printing table in console.
"""

from __future__ import print_function
import json
import os
import sys
import time

import functions
import toolbox

__author__ = 'vlanski'
__version__ = '0.1'
__date__ = '2018-07-12'

_CONF_FILE = 'crckit.conf'
_MAX_LEVEL = -1
_SHOW_PYTHON_CODE = True

_INTRO_TEXT = """\
`green::Welcome to CRCKit %s (%s)`
https://github.com/vlanski/crckit
""" % (__version__, __date__)

_HELP_TEXT = """\
`green::Usage:` crckit [<Options>] <Data> [<Checksum>]

`green::Operations`
-C, --calc               Calculate checksums for <Data>;
-D, --determine          Deep checksums determination for <Data>;
-H, --help               Print help;
-L, --list               Print list of available checksum functions;
-S, --skim               Superficial checksums determination for <Data>;

`green::Options`
-I, --input <file>       Load <file> for checksum analysing;
-O, --output <file>      Save TXT or HTML report with results to <file>;
-X, --hex                <Data> is in HEX format;
-Z, --lazy               Stop after first match.

`green::Filtering`
-N, --name  <string>     Checksum has to contain <string> in it's name;
-W, --width <number>     Checksum width has to equal to <number>.
"""

# - Report templates -----------------------------------------------------------
_HTML_TEMPLATE = """\
<!DOCTYPE html>

<title>CRCKit report</title>
<meta charset="utf-8">

CRCKit Report

CRCKit version: {$VERSION}

Report date: {$DATE}

Elapsed time: {$TIME}

{$TABLE}

"""

_TXT_TEMPLATE = """\
CRCKIT REPORT

CRCKit version:    {$VERSION}
Report date:        {$DATE}
Elapsed time:       {$TIME}

{$TABLE}
"""
# ------------------------------------------------------------------------------

_SCHEME = [{'text': 'calc',      'abbr': 'c', 'has_param': False},
           {'text': 'determine', 'abbr': 'd', 'has_param': False},
           {'text': 'skim',      'abbr': 's', 'has_param': False},
           {'text': 'help',      'abbr': 'h', 'has_param': False},
           {'text': 'list',      'abbr': 'l', 'has_param': False},
           {'text': 'file',      'abbr': 'f', 'has_param': False},
           {'text': 'hex',       'abbr': 'x', 'has_param': False},
           {'text': 'report',    'abbr': 'r', 'has_param': True },
           {'text': 'lazy',      'abbr': 'z', 'has_param': False},
           {'text': 'name',      'abbr': 'n', 'has_param': True },
           {'text': 'width',     'abbr': 'w', 'has_param': True }]

_CMP_FUNCS = {'=':  lambda x, y: x == y,
             '!=': lambda x, y: x != y,
             '>':  lambda x, y: x > y,
             '>=': lambda x, y: x >= y,
             '<':  lambda x, y: x < y,
             '<=': lambda x, y: x <= y}

_IS_PYTHON3 = sys.version_info[0] >= 3

if not _IS_PYTHON3:
    range = xrange

# ------------------------------------------------------------------------------
class KitResult:
    """

    """
    def __init__(self, func, data, pos=0):
        self.name  = func.name
        self.width = func.width
        self.func  = func.function
        self.data  = data
        self.pos   = pos
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
class Kit:
    """

    """
    def __init__(self, data, name_filter='', width=0, feedback=False):
        """

        """
        self._data = str(data)
        self._filter = name_filter
        self._width = width
        self._feedback = feedback

    # - Match two numbers by bytes ---------------------------------------------
    def _match(self, data1, data2, width=8):
        """
            Match two numbers by bytes
            _match(0xAB12, 0x12AB) == True
        """

        # - Inaccurate Comparing -----------------------------------------------
        if (width != 0) and data1 < 0:
            data1 &= (1 << width) - 1

        if (width != 0) and data2 < 0:
            data2 &= (1 << width) - 1

        num1 = data1
        num2 = data2

        xor_sum = 0

        while num1 != 0:
            xor_sum ^= num1 & 0xFF
            num1 = num1 >> 8

        while num2 != 0:
            xor_sum ^= num2 & 0xFF
            num2 = num2 >> 8
        # ----------------------------------------------------------------------

        # - Accurate Comparing -------------------------------------------------
        if xor_sum == 0:
            s = s1 = s2 = ''

            while data1 != 0:
                s1 += chr(data1 & 0xFF)
                data1 = data1 >> 8

            while data2 != 0:
                s2 += chr(data2 & 0xFF)
                data2 = data2 >> 8

            s1 = s1.rjust(width / 8, '\x00')
            s2 = s2.rjust(width / 8, '\x00')

            for ch1 in s1:
                for ch2 in s2:
                    if ch1 == ch2:
                        s += ch1
                        f = s2.find(ch1)
                        s2 = s2[:f] + s2[f + 1:]
                        break

            return s == s1
        else:
            return False
        # ----------------------------------------------------------------------
    # --------------------------------------------------------------------------

    # - Proceed Samples for Calculating Checksums ------------------------------
    def _get_samples(self, data, samples, max_level=-1, level=0, rev=False):
        """

        """
        if data != '' and (max_level == -1 or level < max_level):
            data_range = range(0, len(data))

            if not rev:
                data_range = reversed(data_range)

            iter = (level + 1) / 2

            for i in data_range:
                if rev:
                    samples.append((data[i:], iter + i))
                else:
                    samples.append((data[0:i+1], iter))

            if not rev:
                self._get_samples(data[1:],  samples, max_level, level+1, True)
            else:
                self._get_samples(data[:-1], samples, max_level, level+1, False)
    # --------------------------------------------------------------------------

    # - General Function for Espy ----------------------------------------------
    def _espy(self, checksum, results_list, lazy=False, max_level=-1):
        """

        """
        samples = []

        try:
            self._get_samples(self._data, samples, max_level)
        except MemoryError:
            if self._feedback:
                toolbox.printf('`yellow::Warning: Out of memory`')
        except KeyboardInterrupt:
            result = False
            return

        samples_count = len(samples)
        sample_ind = 0
        sample_ratio = 0

        if self._feedback:
            toolbox.printf('Samples count: %d' % samples_count)

        if self._feedback:
            pbar = toolbox.ProgBar(width=20)

        try:
            for sample, pos in samples:
                sample_ind += 1
                for func in functions._list:
                     # - Name filter -------------------------------------------
                    if self._filter != '':
                        if self._filter.startswith('!'): # ! is NOT operator
                            if func.name.find(self._filter[1:]) != -1:
                                continue
                        else:
                            if func.name.find(self._filter) == -1:
                                continue
                    # ----------------------------------------------------------

                    # - Width filter -------------------------------------------
                    if self._width != 0:
                        if type(self._width) is int:
                            width = (self._width, '=')
                        else:
                            width = self._width

                        if not _CMP_FUNCS[width[1]](func.width, width[0]):
                            continue
                    # ----------------------------------------------------------

                    curr_checksum = func.function(sample) \
                                    & ((1 << func.width) - 1)

                    if self._match(checksum, curr_checksum, func.width):
                        results_list.append(KitResult(func, sample, pos))

                        # Lazy mode - stop after first match
                        if lazy:
                            if self._feedback:
                                pbar.close()

                            return True

                if self._feedback:
                    if sample_ratio != (sample_ind * 100) / samples_count:
                        sample_ratio = (sample_ind * 100)  / samples_count
                        pbar.render(float(sample_ratio) / 100)

            result = True

        except KeyboardInterrupt:
            result = False

        if self._feedback:
            pbar.close()

        return result
    # --------------------------------------------------------------------------

    # - ------------------------------------------------------------------------
    def determine(self, checksum, lazy=False, max_level=-1):
        """

        """
        start_time = time.clock()

        results = []

        if self._feedback:
            if max_level > 2 or max_level == -1:
                toolbox.printf('Determining...')
            else:
                toolbox.printf('Skimming...')

        is_finished = self._espy(checksum, results,
                                 max_level=max_level, lazy=lazy)

        self.elapsed_time = time.clock() - start_time

        if self._feedback:
            if is_finished:
                toolbox.printf('`green::Finished`')
            else:
                toolbox.printf('`red::Aborted`')

            toolbox.printf('Elapsed time: %.2f s.' % self.elapsed_time)

        return tuple(results)
    # --------------------------------------------------------------------------

    # -  -----------------------------------------------------------------------
    def skim(self, checksum, lazy=False):
        """

        """
        return self.determine(checksum, lazy=lazy, max_level=2)
    # --------------------------------------------------------------------------

    # - Calculate checksums ----------------------------------------------------
    def calc(self):
        """

        """
        start_time = time.clock()

        functions_count = len(functions._list)
        results = []

        if self._feedback:
            toolbox.printf('Checksums calculating...')

        try:
            pbar = toolbox.ProgBar(width=20)

            for i, func in enumerate(sorted(functions._list, key=_sort_key)):
                # - Name filter ------------------------------------------------
                if self._filter != '':
                    if self._filter.startswith('!'): # ! is NOT operator
                        if func.name.find(self._filter[1:]) != -1:
                            continue
                    else:
                        if func.name.find(self._filter) == -1:
                            continue
                # --------------------------------------------------------------

                # - Width filter -----------------------------------------------
                if self._width != 0:
                    if type(self._width) is int:
                        width = (self._width, '=')
                    else:
                        width = self._width

                    if not _CMP_FUNCS[width[1]](func.width, width[0]):
                        continue
                # --------------------------------------------------------------

                chechsum = func.function(self._data) \
                           & ((1 << func.width) - 1)

                results.append(KitResult(func, chechsum))

                pbar.render(float(i) / functions_count)

            is_finished = True

        except KeyboardInterrupt:
            is_finished = False

        if self._feedback:
            pbar.close()

        self.elapsed_time = time.clock() - start_time

        if self._feedback:
            if is_finished:
                toolbox.printf('`green::Finished`')
            else:
                toolbox.printf('`red::Aborted`')

            toolbox.printf('Elapsed time: %.2f s.' % self.elapsed_time)

        return tuple(results)
    # --------------------------------------------------------------------------
# ------------------------------------------------------------------------------

# - Config loader --------------------------------------------------------------
def _conf_load():
    """
        Load configuration from file "crckit.json"
    """
    global _MAX_LEVEL
    global _SHOW_PYTHON_CODE

    is_loaded = True
    error_str = ''

    conf = {'kit': {}, 'ui': {}}

    try:
        with open(os.path.join(os.path.dirname(__file__),
                               _CONF_FILE)) as json_fs:
            conf = json.load(json_fs)

    except IOError:
        error_str = '`yellow::Warning: Can\'t load JSON configuration`'

    except ValueError:
        error_str = '`yellow::Warning: Corrupted JSON configuration`'

    # - Default values in configuration ----------------------------------------
    conf.setdefault('kit', {})
    conf.setdefault('ui',   {})

    conf['kit'].setdefault('max_level', -1)
    conf['kit'].setdefault('show_python_code', 1)

    conf['ui'].setdefault('terminal_width', 80)
    conf['ui'].setdefault('color_scheme', 1)
    conf['ui'].setdefault('disable_box_drawing', 0)
    conf['ui'].setdefault('simple_box_drawing', 0)
    # --------------------------------------------------------------------------

    # - Set conf params --------------------------------------------------------
    try:
        _MAX_LEVEL = int(conf['kit']['max_level'])

        if _MAX_LEVEL == 0:
            _MAX_LEVEL = 1

        _SHOW_PYTHON_CODE = int(conf['kit']['show_python_code']) != 0

        toolbox.TABLE_MAX_WIDTH = int(conf['ui']['terminal_width'])

        if toolbox.TABLE_MAX_WIDTH < 80:
            toolbox.TABLE_MAX_WIDTH = 80

        toolbox.USE_COLOR_SCHEME    = int(conf['ui']['color_scheme']) != 0
        toolbox.DISABLE_BOX_DRAWING = int(conf['ui']['disable_box_drawing']) !=0
        toolbox.SIMPLE_BOX_DRAWING  = int(conf['ui']['simple_box_drawing']) != 0

    except ValueError as exc:
        error_str = '`red::Error: Configuration params error`'
        is_loaded = False
    # --------------------------------------------------------------------------

    return is_loaded, error_str
# ------------------------------------------------------------------------------

# - UI: calculate checksum -----------------------------------------------------
def _cmd_calc(args):
    """
        Command "--calc"
        Calculate checksums
    """
    is_error, is_file, file_name, name_filter, width_filter, _, \
    report, report_file_name, data, _ = _extract_args(args, False)

    if is_error:
        return

    calc_kit = Kit(data, name_filter=name_filter, width=width_filter,
                   feedback=True)

    crc_results = calc_kit.calc()

    if report is None:
        toolbox.printf('\n`green::Checksums results`')
    else:
        toolbox.printf('\n`green::Checksums results were exported to "%s"`' %
                       report_file_name)

    if report == 'html':
        table = toolbox.HTMLTable([{'title': 'Param'},
                                   {'title': 'Width', 'align': 'right'},
                                   {'title': 'Value'}])
    else:
        table = toolbox.Table([{'title': 'Param'},
                               {'title': 'Width', 'align': 'right'},
                               {'title': 'Value'}], report=not report is None)

    if is_file:
        table.add_line(['file', '-', file_name])

    table.add_line(['ascii', '-', '"' + data + '"'])
    table.add_line(['hex', '-', data.encode('hex')])
    table.add_line(['length', '-', '%d bytes' % len(data)])

    if name_filter != '':
        table.add_line(['name filter', '-', '"' + name_filter + '"'])

    if width_filter != 0:
        if type(width_filter) is int:
            width_filter = (width_filter, '=')

        table.add_line(['width filter', '-', '%s%d bits' % (width_filter[1],
                                                            width_filter[0])])

    table.add_separator()

    for result in crc_results:
        table.add_line([result.name, result.width,
                        toolbox.hex_view(result.data, result.width)])

    if len(crc_results) == 0:
        table.add_line(['-', '-', 'No checksum functions found'])

    if report is None:
        table.render()
    else:
        _report(report, report_file_name, table.render(), calc_kit.elapsed_time)
# ------------------------------------------------------------------------------

# - UI: deep determination -----------------------------------------------------
def _cmd_determine(args, skim_mode=False):
    """
        Command "--determine"
        Deep checksums determination
    """
    is_error, is_file, file_name, name_filter, width_filter, lazy_mode, \
    report, report_file_name, data, checksum = _extract_args(args, True)

    if is_error:
        return

    espy_kit = Kit(data, name_filter=name_filter, width=width_filter,
                   feedback=True)

    if skim_mode:
        crc_results = espy_kit.skim(checksum, lazy=lazy_mode)
    else:
        crc_results = espy_kit.determine(checksum, lazy=lazy_mode,
                                         max_level=_MAX_LEVEL)

    print_text = '\n`green::Skimming results' if skim_mode else \
                 '\n`green::Determining results'

    if report is None:
        print_text += '`'
    else:
        print_text += 'were exported to "%s"`' % report_file_name

    toolbox.printf(print_text)

    if report == 'html':
        table = toolbox.HTMLTable([{'title': 'Param'},
                                   {'title': 'Width', 'align': 'right'},
                                   {'title': 'Value'}])
    else:
        table = toolbox.Table([{'title': 'Param'},
                               {'title': 'Width', 'align': 'right'},
                               {'title': 'Value'}], report=not report is None)

    if is_file:
        table.add_line(['file', '-', file_name])

    table.add_line(['ascii', '-', '"' + data + '"'])
    table.add_line(['hex', '-', data.encode('hex')])
    table.add_line(['length', '-', '%d bytes' % len(data)])
    table.add_line(['checksum', '-', hex(checksum)])

    if name_filter != '':
        table.add_line(['name filter', '-', '"' + name_filter + '"'])

    if width_filter != 0:
        if type(width_filter) is int:
            width_filter = (width_filter, '=')

        table.add_line(['width filter', '-', '%s%d bits' % (width_filter[1],
                                                            width_filter[0])])

    if lazy_mode:
        table.add_line(['lazy mode', '-', 'enabled'])

    table.add_separator()

    crc_results_count = len(crc_results)

    for i, result in enumerate(crc_results):
        pos = result.pos
        length = len(result.data)

        table.add_line([result.name, result.width,
                       '"' + result.data + '"'])
        table.add_line(['', '', result.data.encode('hex')])

        table.add_line(['', '', toolbox.hex_view(result.func(result.data),
                                                 result.width)])

        table.add_line(['', '', 'begin: %d, length: %d' % (pos, length)])

        if _SHOW_PYTHON_CODE:
            range_str = ''
            if not (pos == 0 and length == len(data)):
                if pos == 0:
                    range_str = '[:'
                else:
                    range_str = '[%d:' % pos

                if pos + length == len(data):
                    range_str += ']'
                else:
                    range_str += '%d]' % (pos + length)

            table.add_line(['', '', 'crckit.functions.' + result.name +
                                    '(data' + range_str + ')'])

        if i != crc_results_count - 1:
            table.add_separator()

    if len(crc_results) == 0:
        table.add_line(['-', '-', 'No checksum functions found'])

    if report is None:
        table.render()
    else:
        _report(report, report_file_name, table.render(), espy_kit.elapsed_time)
# ------------------------------------------------------------------------------

# - UI: list of available functions --------------------------------------------
def _cmd_list(args):
    """
        Command "--list"
        Print list of available checksum functions
    """
    toolbox.printf('`green::List of available checksum functions`')

    columns = [{'title': ' # ',               'align': 'right'},
               {'title': 'Checksum function', 'align': 'left' },
               {'title': 'Width',             'align': 'right'}]

    table = toolbox.Table(columns)

    for i, func in enumerate(sorted(functions._list,
                                    key=_sort_key)):

        table.add_line([i + 1, func.name, func.width])

    table.render()
# ------------------------------------------------------------------------------

# - UI: help message -----------------------------------------------------------
def _cmd_help(args):
    """
        Command "--help"
        Print help message
    """
    toolbox.printf(_HELP_TEXT)
# ------------------------------------------------------------------------------

# - UI: superficial determination ----------------------------------------------
def _cmd_skim(args):
    """
        Command "--skim"
        Superficial checksums determination
    """
    _cmd_determine(args, True)
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
def _extract_args(args, need_checksum):
    """
    """
    is_error = False

    file_name = ''

    data = ''
    checksum = ''

    name_filter = ''
    width_filter = 0

    lazy_mode = False

    report = None
    report_file_name = ''

    # - Hex and file flags -----------------------------------------------------
    is_hex  = 'x' in args.options
    is_file = 'f' in args.options
    # --------------------------------------------------------------------------

    if len(args.data) < 1:
        toolbox.printf('`red::Error: No input data`')
        is_error = True

    else:
        # - Get data -----------------------------------------------------------
        if is_hex and is_file:
            toolbox.printf('`yellow::Warning: Hex and file params detected`')

        if is_hex:
            try:
                data = args.data[0].replace(' ', '').replace('\t', '').  \
                                    replace('\n', '').replace('\r', ''). \
                                    decode('hex')
            except Exception:
                toolbox.printf('`red::Error: Can\'t decode hex data`')
                is_error = True

        elif is_file:
            if os.path.isfile(args.data[0]):
                try:
                    with open(args.data[0], 'rb') as fp:
                        data = fp.read()
                        file_name = args.data[0]
                except IOError:
                    toolbox.printf('`red::Error: File "' + args.data[0] + \
                                   '": Permission denied`')
                    is_error = True

            else:
                toolbox.printf('`red::Error: File "' + args.data[0] + \
                               '" is not found`')
                is_error = True

        else:
            data = args.data[0]
        # ----------------------------------------------------------------------

        # - Get checksum -------------------------------------------------------
        if need_checksum:
            if len(args.data) < 2:
                toolbox.printf('`red::Error: No input checksum`')
                is_error = True
            else:
                checksum = args.data[1]

                checksum = checksum.strip()

                if checksum.startswith('0x') or checksum.startswith('0X'):
                    checksum = checksum[2:]

                if checksum.startswith('$'):
                    checksum = checksum[1:]

                if checksum.endswith('h'):
                    checksum = checksum[:-1]

                checksum = checksum.replace(' ', '').replace('\t', '').  \
                                    replace('\n', '').replace('\r', '')

                try:
                    checksum = int(checksum, 16)
                except ValueError:
                    toolbox.printf('`red::Error: "' + args.data[1] + \
                                   '" is not hex number`')
                    is_error = True
        # ----------------------------------------------------------------------

        # - Name filter --------------------------------------------------------
        if 'n' in args.options:
            name_filter = args.options['n'].strip()
        # ----------------------------------------------------------------------

        # - Width filter -------------------------------------------------------
        # Parsing results:
        #  {width}, ={width}, =={width}    {width}
        #  !{width}, !={width}             ({width}, '!')
        #  >{width}                        ({width}, '>')
        #  >={width}                       ({width}, '>=')
        #  <{width}                        ({width}, '<')
        #  <={width}                       ({width}, '<=')

        if 'w' in args.options:
            width_filter = args.options['w'].strip()

            try:
                sub_found = False
                for sub_i, sub_o in [('>=', '>='), ('<=', '<='),
                                     ('!=', '!='), ('==', None),
                                     ('>', '>'),   ('<', '<'),
                                     ('!', '!='),  ('=', None)]:
                    if width_filter.startswith(sub_i):
                        width_filter = (int(width_filter[len(sub_i):]), sub_o)
                        sub_found = True
                        break

                if sub_found and width_filter[1] is None:
                    width_filter = width_filter[0]

                if not sub_found:
                    width_filter = int(width_filter)

            except ValueError:
                toolbox.printf('`red::Error: "' + width_filter + \
                               '" is not a number`')
                is_error = True
        # ----------------------------------------------------------------------

        # - Lazy mode flag -----------------------------------------------------
        if 'z' in args.options:
            lazy_mode = True
        # ----------------------------------------------------------------------

        # - Report options -----------------------------------------------------
        if 'r' in args.options:
            report = 'html'
            report_file_name = args.options['r']

            if os.path.splitext(report_file_name)[1][1:].lower() \
                                               in ['html', 'htm']:
                report = 'html'
            else:
                report = 'utf-8'
        # ----------------------------------------------------------------------

    return is_error, is_file, file_name, name_filter, width_filter, \
           lazy_mode, report, report_file_name, data, checksum
# ------------------------------------------------------------------------------

# - HTML and TXT report --------------------------------------------------------
def _report(file_type, file_name, text, elapsed_time):
    """

    """
    if file_type == 'html':
        is_html = True
    elif file_type == 'utf-8':
        is_html = False
    else:
        toolbox.printf('red::Error`: Unknown report type`')
        return

    if is_html:
        temp = _HTML_TEMPLATE
    else:
        temp = _TXT_TEMPLATE

    report = temp.replace('{$VERSION}', '%s (%s)' % (__version__, __date__)). \
                  replace('{$DATE}', time.strftime("%c", time.localtime())). \
                  replace('{$TIME}', '%.2f s' % elapsed_time). \
                  replace('{$TABLE}', text)

    try:
        with open(file_name, 'w') as fs:
            if type(report) is unicode:
                report = '\xef\xbb\xbf' + report.encode('utf-8')

            fs.write(report)

    except IOError:
        print('fuck')
# ------------------------------------------------------------------------------

# - Short function for sorting -------------------------------------------------
def _sort_key(x):
    return (getattr(x, 'width'), getattr(x, 'name'))
# ------------------------------------------------------------------------------

# - Main Function --------------------------------------------------------------
def main():
    """
        Start CRCKit UI
    """
    is_loaded, error_str = _conf_load()

    toolbox.printf(_INTRO_TEXT)

    if error_str != '':
        toolbox.printf(error_str)

    if is_loaded:
        args = toolbox.Arguments(_SCHEME)

        if args.error != '':
            toolbox.printf('`red::Error: ' + args.error + '`')
        else:
            if   'c' in args.options:
                _cmd_calc(args)

            elif 'd' in args.options:
                _cmd_determine(args)

            elif 'h' in args.options:
                _cmd_help(args)

            elif 'l' in args.options:
                _cmd_list(args)

            elif 's' in args.options:
                _cmd_skim(args)

            else:
                args_count = len(args.data)

                if args_count == 1:
                    _cmd_calc(args)

                elif args_count == 2:
                    _cmd_skim(args)

                else:
                    _cmd_help(args)

if __name__ == '__main__':
    main()
# ------------------------------------------------------------------------------

__all__ = ['KitResult', 'Kit', 'main']


import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

DEBUG = True

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'saferecipient'
]

ROOT_URLCONF = 'travis-ci.urls'

WSGI_APPLICATION = 'travis-ci.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


string1 = "Length of this String"
print ("length of this string is", len(string1))

test_str = "www.google.com"
res = {}
for keys in test_str:
    res[keys] = res.get(keys, 0) + 1
print ("Letter Frequency", str(res))
string2 = 'Environment'
print("First and Last 2 Characters in this string = " , string2[0:2] + string2[-2:])

def change_letter(str1):
  char = str1[0]
  str1 = str1.replace(char, '$')
  str1 = char + str1[1:]
  return str1
print(change_letter('why wont this work'))

def rearrange_letters(a, b):
  new_a = b[:2] + a[2:]
  new_b = a[:2] + b[2:]
  return new_a + ' ' + new_b
print(rearrange_letters('abcdefg', 'hijklmnop'))

def add_ing(str2):
  length = len(str2)
  if length > 2:
    if str2[-3:] == 'ing':
      str2 += 'ly'
    else:
      str2 += 'ing'
  return str2
print(add_ing('abc'))

str3 = "The man is not that poor!"
not_location = str3.find('not')
poor_location = str3.find('poor')
if not_location < poor_location and not_location!=-1:
    str3 = str3 [:not_location] + "good" + str3[poor_location+4:]
print(str3)

def longestword():
    list1 =["Pizza", "Burgers", "Pineapple"]
    longest = list1[0]
    for item in list1:
        if len(item) > len(longest):
            longest = item
    return len(longest)
print(longestword())

def remove_letter(str, n):
    first_letter = str[:n]
    last_letter = str[n + 1:]
    return first_letter + last_letter
print(remove_letter('bananas', 0))
print(remove_letter('bananas', 4))

def change_word(str4):
    return str4 [-1:]+str4[1:-1]+str4[:1]
print(change_word('Jaguar'))

def odd_values_string(str):
    for i in range(0,len(str),2):
        print(str[i],end='')
print (odd_values_string('Banana'))

sentence = 'this is the sentence for this question.'

from collections import Counter

def a_sentence():
    b_sentence = input('Enter a sentence: ')
    c_sentence = b_sentence[:-1]
    words = c_sentence.split()
    a = Counter(words)
    return a
print(a_sentence())

word = input('enter the words:')
answer = word.upper()
answer1 = word.lower()
print(answer, answer1)

w = ['red', 'white', 'black', 'red', 'green', 'black']
print(*(sorted(set(w))))


# THIS PROGRAM ASSUMES THAT THERE ARE NO DUPLICATE EDGES (DUE TO VAR TWICE IN CLAUSE, OR, DUPLICATE CLAUSE)

from sys import argv

Vars = []	# Contains list of variables
Doms = {}	# Hash-table for domain of each variable
num = -1	# Number of variables
feat = []	# A list of features. Each feature is given as (w, clause) where clause is a list of var-val pairs (a,v)

mapping = {}	# A map storing the variable name for each vertex index
var_map = {}	# A map storing the vertex index for each variable name
splits = []		# Splits for colours
vertex_count = 0	# Number of vertices

edges = []

weight_map = {}	# A map storing list of features for each weight

def read_file(file_name):
	global Vars, Doms, num

	inp = open(file_name, 'r')

	temp = inp.readline().strip()
	if(temp == ''):
		temp = inp.readline().strip()

	temp = temp.split(':')[1]
	Vars = temp.split(',')

	num = len(Vars)

	temp = inp.readline().strip()
	if(temp == ''):
		temp = inp.readline().strip()

	for a in range(num):
		[var, dom] = temp.split('=')

		if not(var in Vars):
			raise Exception(var + " does not exist!")

		dom = dom.split(',')
		Doms[var] = dom

		temp = inp.readline().strip()

	if(temp == ''):
		temp = inp.readline().strip()

	while(temp != ''):
		[w, cl] = temp.split('\t')
		w = float(w)
		cl = cl.split(',')
		cl = [(l.split()[0], l.split()[1]) for l in cl]
		feat.append((w,cl))

		temp = inp.readline().strip()

def construct_graph():
	global mapping, var_map, splits, edges, vertex_count, weight_map

	mapping = {}
	var_map = {}
	dom2vars ={}
	splits = []
	edges = []
	vertex_count = 0
	weight_map = {}

	for i in range(num):
		mapping[i] = Vars[i]
		var_map[Vars[i]] = i

	splits.append(num)	# One colour for the hub nodes
	vertex_count = num

	for var in Vars:
		for d in Doms[var]:
			if d in dom2vars.keys():
				dom2vars[d].append(var)
			else:
				dom2vars[d]=[var]

	for d in dom2vars:
		for var in dom2vars[d]:
			mapping[vertex_count] = (var,d)
			var_map[(var,d)] = vertex_count
			edges.append((var_map[var],vertex_count))
			vertex_count += 1
		splits.append(vertex_count)

	for (w, cl) in feat:
		if(weight_map.get(w) == None):
			weight_map[w] = [cl]
		else:
			weight_map[w].append(cl)

	for w in weight_map:
		for cl in weight_map[w]:
			mapping[vertex_count] = cl
			# var_map[cl] = vertex_count

			for l in cl:
				edges.append((vertex_count, var_map[l]))

			vertex_count += 1

		splits.append(vertex_count)

def write_file(file_name):
	out = open(file_name, 'w')

	num_col = len(splits)
	out.write(str(vertex_count) + ' ' + str(len(edges)) + ' ' + str(num_col))

	for c in range(num_col-1):
		out.write(' ' + str(splits[c]))

	out.write('\n')

	for e in edges:
		out.write(str(e[0]) + ' '  + str(e[1]) + '\n')

def main():
	read_file(argv[1])

	# print "Vars:"
	# print Vars

	# print "\nDomains:"
	# print Doms

	# print "\nFeatrues:"
	# print feat

	construct_graph()

	# print "\nWeight Map:"
	# print weight_map

	# print "\nMapping:"
	# print mapping

	# print "\nVar_map:"
	# print var_map

	# print "\nSplits:"
	# print splits

	# print "\nEdges:"
	# print edges

	write_file(argv[2])

main()


import sys

sys.path.append('.')

from decorators import input_number

@input_number
def custom_abs(x):
    """
    Модуль числа

    :param x:
    :return:
    """

    if x < 0:
        return x * -1
    else:
        return x

input_value = input("Введите целое число: ")
message_error = "[Ошибка]: Введённое значение не является числом."

print(custom_abs(input_value, message_error))


import numpy as np

class Sample:
    def __init__(self, W):
        self.W = np.copy(W)
        self.n = len(W)
        self.W[:, 0] = 0
        np.fill_diagonal(self.W, 0)
        for i in range(1, self.n):
            self.W[:, i] /= np.sum(self.W[:, i])

    def _desc(self, tree, i):
        nodes = set()
        stack = [i]
        while len(stack):
            x = stack.pop(0)
            nodes.add(x)
            for j, u in enumerate(tree):
                if u == x:
                    stack.append(j)
        return nodes

    def sample(self):
        W = np.copy(self.W)
        tree = - np.ones(self.n).astype(int)
        for j in range(1, self.n):
            nodes = self._desc(tree, j)
            for u in nodes:
                W[u, j] = 0
            W[:, j] /= np.sum(W[:, j])
            i = np.random.choice(np.arange(self.n), p=W[:, j])
            tree[j] = i
        return tree

class WilsonSample:
    def __init__(self, W):
        self.W = np.copy(W)
        self.n = len(W)
        self.W[:, 0] = 0
        np.fill_diagonal(self.W, 0)
        for i in range(1, self.n):
            self.W[:, i] /= np.sum(self.W[:, i])

    def sample(self):
        inTree = np.zeros(self.n).astype(bool)
        next = - np.ones(self.n).astype(int)
        inTree[0] = True
        for i in range(1, self.n):
            u = i
            while not inTree[u]:
                next[u] = np.random.choice(np.arange(self.n), p=self.W[:, u])
                u = next[u]
            u = i
            while not inTree[u]:
                inTree[u] = True
                u = next[u]
        return next

class WilsonRCSample:
    def __init__(self, W):
        self.W = np.copy(W)
        self.r = self.W[0, 1:]
        self.A = self.W[1:, 1:]
        np.fill_diagonal(self.A, 0)
        self.n = len(self.A)
        self.r /= np.sum(self.r)
        for i in range(self.n):
            self.A[:, i] /= np.sum(self.A[:, i])

    def sample(self):
        inTree = np.zeros(self.n + 1).astype(bool)
        next = - np.ones(self.n + 1).astype(int)
        inTree[0] = True
        # Pick root edge
        j = np.random.choice(np.arange(self.n), p=self.r)
        next[j+1] = 0
        inTree[j+1] = True
        for i in range(self.n):
            u = i + 1
            while not inTree[u]:
                next[u] = np.random.choice(np.arange(self.n), p=self.A[:, u-1]) + 1
                u = next[u]
            u = i + 1
            while not inTree[u]:
                inTree[u] = True
                u = next[u]
        return next


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @doc https://www.crummy.com/software/BeautifulSoup/bs4/doc/
from bs4 import BeautifulSoup

def first_demo():
    html_doc = """
<title>The Dormouse's story</title>

The Dormouse's story

Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie

"""

    soup = BeautifulSoup(html_doc)
    print(soup.prettify())

    print(soup.title)
    print(soup.title.name)
    print(soup.title.string)

    for link in soup.find_all("a"):
        print(link, link.get("href"))

    print(soup.get_text())

    for find in soup.find_all(**{'class': 'sister'}):
        print(find)

    for find in soup.select("#link1"):
        print(find)

def tag_demo():
    xml = """
    Extremely bold
    """
    soup = BeautifulSoup(xml)
    tag = soup.b
    print(type(soup))
    print(type(tag))
    print(tag, tag.attrs)

if __name__ == "__main__":
    first_demo()


from Selectors import *
import string

def buildSearch(query):
    return SearchBuilder(query).produceSearch()

indent = 0

def debug(s):
    if True:
        print("%s%s" % ('\t' * indent, s))

class SearchBuilder(object):
    def __init__(self, buffer):
        self.buffer = buffer
        self.offset = 0

    # This should be replaced with an actual tokeniser/etc, please.
    def match(self, kind):
        if self.offset >= len(self.buffer):
            return kind == 'EOF'
        while self.buffer[self.offset] == ' ':
            self.offset += 1

        ##debug("Match %s on %s" % (kind, self.buffer[self.offset:]))
        if kind == 'PIPE':
            return self.buffer[self.offset] == '|'
        elif kind == 'LPAREN':
            return self.buffer[self.offset] == '('
        elif kind == 'RPAREN':
            return self.buffer[self.offset] == ')'
        elif kind == 'EXCLUSION':
            return self.buffer[self.offset] == '-'
        elif kind == 'EOF':
            return self.offset == len(self.buffer)
        elif kind == 'WORD':
            lookahead = self.offset
            count = 0
            if lookahead < len(self.buffer) and not self.buffer[lookahead] in ')-| \t':
                return True
            return False

    def forward(self):
        while self.buffer[self.offset] == ' ':
            self.offset += 1

        if self.match('PIPE'):
            self.offset += 1
            return '|'
        elif self.match('LPAREN'):
            self.offset += 1
            return '('
        elif self.match('RPAREN'):
            self.offset += 1
            return ')'
        elif self.match('EXCLUSION'):
            self.offset += 1
            construction = '-'
            if self.match('WORD'):
                construction += self.forward()
            else:
                raise Exception("Minus symbol not preceding a word?")
            return construction
        elif self.match('WORD'):
            construction = ''
            while self.offset < len(self.buffer) and not self.buffer[self.offset] in ')-| \t':
                c = self.buffer[self.offset]
                construction += c
                self.offset += 1
            if len(construction) == 0:
                raise Exception('Failed to forward through word')
            return construction
        elif self.match('EOF'):
            raise Exception("Can't forward EOF")

    def produceSearch(self):
        union = self.produceUnion()
        return self.optimizeUnion(union)

    def optimizeUnion(self, union):
        # A union should never appear at the top level, otherwise
        # the user will have to provide two database cursors.
        # A union can occur after a Split however, but it's probably more
        # ideal to use a Forwarder in most cases where you'd do that.
        global indent
        while isinstance(union, UnionSelector):
            #debug(union.left)
            #debug(union.right)

            if isinstance(union, UnionSelector) and \
               isinstance(union.left, Selector) and\
               isinstance(union.right, UnionSelector) and\
               isinstance(union.right.left, Selector) and\
               isinstance(union.right.right, UnionSelector):
                fake_union = UnionSelector(union.left, union.right.left)
                optimized_fake = self.optimizeUnion(fake_union)
                if isinstance(optimized_fake, Forwarder):
                    #debug("Left-hand optimization.")
                    debug("Union(Selector %x, Union(Selector %y, ...)) -> Split(Optimize(%x, %y), ...)")
                    union = SplitSelector(optimized_fake, union.right.right)
                    if isinstance(union.right, Yielder):
                        debug("Yielder -> " + union.original.__class__.__name__)
                        union.right = union.original

            if isinstance(union, UnionSelector) or isinstance(union, SplitSelector):
                left = union.left
                right = union.right
            elif isinstance(union, Forwarder):
                left = union.source
                right = union.target
            else:
                return union

            if isinstance(left, UnionSelector):
                debug("Optimizing left branch...")
                indent = indent + 1
                left = self.optimizeUnion(left)
                indent = indent - 1
                debug("... done.")
            if isinstance(right, UnionSelector):
                debug("Optimizing right branch...")
                indent = indent + 1
                right = self.optimizeUnion(right)
                indent = indent - 1
                debug("... done.")

            if isinstance(union, UnionSelector) or isinstance(union, SplitSelector):
                union.left = left
                union.right = right
            elif isinstance(union, Forwarder):
                union.source = left
                union.target = right

            if isinstance(union, SplitSelector) and\
               isinstance(union.left, Forwarder) and\
               isinstance(union.right, SplitSelector) and\
               isinstance(union.right.left, Forwarder):
                #debug("Potential left-recursion")
                # Recursive optimization on the left-hand.
                left_forward = union.left
                right_forward = union.right.left

                # Are these what we think they are?
                if isinstance(left_forward.source, AnySelector) and\
                   isinstance(left_forward.target, Selector) and\
                   isinstance(right_forward.source, AnySelector) and\
                   isinstance(right_forward.target, Selector):
                    # This appears to be the expression:
                    # ((...) ...) | ((...) ...)
                    # Attempt to reduce that down into (...) ...
                    # How to determine if it's actually correct?
                    left_join = left_forward.source.set | right_forward.source.set
                    right_join = right_forward.target.set & left_forward.target.set
                    if len(right_forward.target.set ^ left_forward.target.set) == 0:
                        #debug("((%s) %s) | ((%s) %s) -> (%s) %s" % ('|'.join(left_forward.source.set), ' '.join(left_forward.target.set), '|'.join(right_forward.source.set), ' '.join(right_forward.target.set), '|'.join(left_join), ' '.join(right_join)))
                        union = SplitSelector(Forwarder(AnySelector(left_join), Selector(right_join)), union.right.right)
                        if isinstance(union.right, Yielder):
                            debug("Yielder -> " + union.right.original.__class__.__name__)
                            union.right = union.right.original

            #debug(union)

            if not isinstance(union, UnionSelector):
                return union

            if isinstance(union.left, Selector) and \
               isinstance(union.right, Selector):
                if union.left.set == union.right.set:
                    debug("Union(Select %x, Select %x) -> %x")
                    union = union.left
                else:
                    intersection = union.left.set & union.right.set
                    if len(intersection):
                        onion = union.left.set ^ union.right.set
                        # I have no idea what this has become any more.
                        if len(onion) > 1 and len(union.left.set - union.right.set) <= 1:
                            #debug("The expression %s would be better written as '(%s) %s', let me change that for you." % ("(%s) | (%s)" % (' '.join(union.left.set), ' '.join(union.right.set)), ' | '.join(onion), ' '.join(intersection)))
                            debug("Union(Select %x, Select %y) -> Forwarder(Any(%x.set ^ %y.set), Select(%left.set & %right.set))")
                            debug(union.left.set)
                            debug(union.right.set)
                            union = Forwarder(AnySelector(onion), Selector(intersection))
                        else:
                            #debug("Optimization a: (Selector, Selector) -> Split(Selector, Selector)")
                            if not len(intersection):
                                debug("Union(Select, Select) -> Split(Select, Select)")
                                union = SplitSelector(union.left, union.right)
                            else:
                                #debug(union.left.set)
                                #debug(union.right.set)
                                #debug(intersection)
                                if union.left.set == intersection or union.right.set == intersection:
                                    debug("Fix for dumb attempt at optimization #1")
                                    union = Selector(intersection)
                                else:
                                    debug("Fix for dumb attempt at optimization #2")
                                    union.left.set -= intersection
                                    union.right.set -= intersection
                                    union = SplitSelector(union.left, union.right, rule=Selector(intersection))
                    else:
                        #debug("Optimization b: (Selector, Selector) -> Split(Selector, Selector)")
                        # (...) | (...)
                        debug("Union(Selector %x, Select %y) -> Split(%x, %y)")
                        union = SplitSelector(union.left, union.right)
            elif isinstance(union.left, Selector) and \
                 isinstance(union.right, Forwarder):
                if isinstance(union.right.source, AnySelector) and \
                   isinstance(union.right.target, Selector):
                    # Incomplete optimization: (...) | (...) | (...) ...
                    #debug("Optimization: Three-select, post two reduction optimization (incomplete).")
                    right = union.left.set & union.right.target.set
                    left = union.left.set ^ union.right.target.set
                    if len(left):
                        left |= union.right.source.set
                        debug("Union(Select %x, Forward(Any %y, Select %z)) -> Forward(Any(%x.set & %z.set), Select((%x.set ^ %z.set) | %y.set))")
                        #debug("The expression (%s) | (%s) %s would be better rewritten as (%s) %s, let me change that for you." % (' '.join(union.left.set), ' | '.join(union.right.source.set), ' '.join(union.right.target.set), ' | '.join(left), ' '.join(right)))
                        #debug("THIS OPTIMIZATION IS EXPERIMENTAL, PLEASE VERIFY IT WORKS~")
                        union = Forwarder(AnySelector(left), Selector(right))
                    else:
                        debug("Union(Selector %x, Forwarder %y) -> Split(%x, %y)")
                        union = SplitSelector(union.left, union.right)
            elif isinstance(union.left, Selector) and \
                 isinstance(union.right, SplitSelector):
                    # Incomplete optimization: (...) | (...) | (...) | (...) ...
                    #debug("Optimization: Four+-select (very incomplete, may break output)")
                    debug("Union(Selector %x, Split %y) -> Split(%x, %y)")
                    union = SplitSelector(union.left, union.right)
            elif isinstance(union.left, ExtrudeSelector) and \
                 isinstance(union.right, ExtrudeSelector):
                debug("Union(Extrude %x, Extrude %y) -> Split(%x, %y, Any(%x.inclusive | %y.inclusive))")
                #debug("Optimization: Union(Extrude, Extrude) -> Split(Extrude, Extrude, AnySelector(Left.inclusive | Right.inclusive))")
                left_extrude = union.left
                right_extrude = union.right
                union = SplitSelector(left_extrude, right_extrude, rule=AnySelector(union.left.inclusive | union.right.inclusive))

            if isinstance(union, SplitSelector) and\
                 isinstance(union.left, Selector) and\
                 isinstance(union.right, SplitSelector) and\
                 isinstance(union.right.left, Selector):
                #debug("Possible recursive-left/right optimization?")
                fake_union = UnionSelector(union.left, union.right.left)
                optimized_union = self.optimizeUnion(fake_union)
                if isinstance(optimized_union, Forwarder):
                    debug("Split(Select %x, Split(Select %y, ...)) -> Split(Optimize(%x, $y), ...)")
                    union.left = optimized_union
                    union.right = union.right.right
                    if isinstance(union.right, Yielder):
                        debug("Yielder -> " + union.right.original.__class__.__name__)
                        union.right = union.right.original
        return union

    def produceUnion(self):
        expression = self.produceExpression()

        while True:
            if self.match('PIPE'):
                self.forward()
                right_hand = self.produceExpression()
                if isinstance(expression, Selector) and \
                   isinstance(right_hand, Selector) and len(right_hand.set) == 1:
                    expression = AnySelector(expression.set | right_hand.set)
                elif isinstance(expression, AnySelector) and \
                    isinstance(right_hand, Selector) and len(right_hand.set) == 1:
                    expression.set |= right_hand.set
                else:
                    expression = UnionSelector(expression, right_hand)
            else:
                break
        ##debug("Returning: %s" % (expression))
        return expression

    def produceExpression(self):
        ##debug(self.buffer[self.offset:])
        factors = [self.produceFactor()]

        if isinstance(factors[0], UnionSelector):
            return factors[0]

        while True:
            if self.match('WORD'):
                if len(factors) > 1:
                    if isinstance(factors[-1], ExcludeSelector) and \
                       isinstance(factors[-2], Selector):
                            factors[-2].set |= self.produceFactor().set
                    else:
                        raise Exception("Uhh")
                elif isinstance(factors[-1], Selector):
                    factors[-1].set |= self.produceFactor().set
                else:
                    factors.append(self.produceFactor())
            elif self.match('EXCLUSION'):
                factors.append(self.produceFactor())
            else:
                ##debug("Factor stack before: %s" % (factors))
                while len(factors) > 1:
                    if isinstance(factors[-1], Selector):
                        factors[-1], factors[-2] = factors[-2], factors[-1]
                    if isinstance(factors[-1], ExcludeSelector) and \
                        isinstance(factors[-2], Selector):
                        exclude = factors.pop()
                        select = factors.pop()
                        factors.append(ExtrudeSelector(select.set, exclude.set))
                    elif isinstance(factors[-1], ExcludeSelector) and \
                         isinstance(factors[-2], AnySelector):
                         exclude = factors.pop()
                         anyselect = factors.pop()
                         factors.append(Forwarder(exclude, anyselect))
                    else:
                        break
                ##debug("Factor stack after: %s" % (factors))
                return factors[0]

    def produceFactor(self):
        if self.match('LPAREN'):
            self.forward()
            union = self.produceUnion()
            if not self.match('RPAREN'):
                raise Exception("Unbalanced parenthesis")
            self.forward()
            return union
        elif self.match('WORD'):
            word = self.forward()
            return Selector(set([word]))
        elif self.match('EXCLUSION'):
            exclusion = self.forward()
            word = exclusion[1:]
            return ExcludeSelector(set([word]))
        else:
            raise Exception("produceFactor didn't find a factor.")


from zope.component import getUtility
from Products.CMFCore.utils import getToolByName
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

try:
    from zope.site.hooks import getSite
except ImportError:
    from zope.app.component.hooks import getSite

class Rights(BrowserView):
    template = ViewPageTemplateFile('rights.pt')
    title = 'Title'
    description = 'Description'

    def __call__(self):
        return self.template()

class UserRights(Rights):
    '''
    '''
    title = 'Users / Groups'
    description = ''
    # description = ('This is an overview of the users with rights overrides on '
    #                'the share tab of content.')
    which = 'users'

    def results(self):
        acl_users = getToolByName(self.context, 'acl_users')
        prm = acl_users.portal_role_manager
        roles = [r for r in prm.listRoleIds() if r != 'Owner']
        _users = dict([(u.getUserName(), u) for u in acl_users.getUsers()])
        _groups = dict([(u.getUserName(), u) for u in acl_users.getGroups()
            if [r for r in u.getRoles() if r != 'Authenticated']])
        principals = dict(
            (r, prm._safeListAssignedPrincipals(r)) for r in roles)
        all_users = set()
        for usergroup in principals.values():
            all_users |= set(usergroup)

        i, groups, gusers = 1, {}, {}
        for g in _groups.values():
            groups[g.getGroupId()] = i
            for uid in [u.id for u in g.getAllGroupMembers()]:
                _u = gusers.get(uid, {})
                for r in g.getRoles():
                    if r in roles:
                        _u[r] = _u.get(r, []) + [str(i)]
                if _u:
                    gusers[uid] = _u
            i += 1

        head = ['Who'] + roles
        _tu = '%s'
        _tg = '%s'
        _gn = '%s'
        _gr = '%s'
        # Try to find the user in users, first by name, then by id.
        # Then see if the user listed eroniously (e.g. not found), and finish
        # with creating a url with the fingers crossed (LDAP users can be none
        # of the above and then have the default LDAP userrights).
        user = lambda u: [
             u[0] in _users and _tu % (_users[u[0]], u[0]) or
             u[1] in _users and _tu % (_users[u[1]], u[0]) or
             u[1].endswith(': not found>') and ' '.join((u[0], '*')) or
             _tu % (u[1], u[0])]
        # Format with role and the group numbers giving the role for users
        # getting rights from groups.
        gr = lambda x, r: _gr % ' '.join([r, _gn % ', '.join(x)]) if x else ''
        ur = lambda x, r: ' '.join([r, _gn % ', '.join(x)]) if x else r
        role_list = lambda u: [(
            ur((gusers.get(u[0], {}) or gusers.get(u[1], {})).get(r, []), r
                ) if u in principals[r] else
            gr((gusers.get(u[0], {}) or gusers.get(u[1], {})).get(r, []), r))
            for r in roles]

        body = [user(u) + role_list(u) for u in sorted(list(all_users))
                if u[0] not in _groups]

        return {'head': head, 'body': body, 'groups': groups}

class ShareRights(Rights):
    ''''''
    title = 'Sharing'
    description = ''
    # description = ('This is an overview of the users with rights overrides on '
    #                'the share tab of content.')
    which = 'shared'

    def get_name(self, id):
        member_info = self.pm.getMemberInfo(id)

        # member_info is None if there's no Plone user object, as when
        # using OpenID.
        if member_info:
            fullname = member_info.get('fullname', '')
            if fullname:
                return fullname

        return id

    def parse_results(self, results):
        roles = [u'Contributor', u'Editor', u'Reader', u'Reviewer']
        head = [u'Where', u'Who'] + roles
        body = []
        userdata = {}

        for r in results:
            path, url = r.getPath(), r.getURL()
            where = '%s' % ('/'.join((url, '@@sharing')),
                                             path)

            for s in r['Sharing']:
                if r['Sharing']:
                    type, id, title, share = s.split('|')
                    share = [['v', ' ', 'global', 'aquired']['tfga'.find(i)]
                             for i in share]
                    if 'v' in share:
                        type = 'Group' if type == 'g' else 'User'
                        contributor, editor, reader, reviewer = share
                        body.append([where, title, contributor, editor, reader,
                                     reviewer])

        return {'head': head, 'body': body}

    def results(self):
        self.catalog = getToolByName(self.context, 'portal_catalog')
        self.pm = getToolByName(self.context, 'portal_membership')

        return self.parse_results(self.catalog(sort_on='path'))


# Simple Image Viewer w/ GUI by Zack Vanderpool
# Based on the tutorials by John Elder for his Youtube Channel, Codemy.com
# His channel can be found at https://www.youtube.com/channel/UCFB0dxMudkws1q8w5NJEAmw
# I used videos 8 and 9 of the playlist "Python GUI's with TKinter," which can be found at https://www.youtube.com/playlist?list=PLCC34OHNcOtoC6GglhF3ncJ5rLwQrLGnV

# This program builds a GUI that can be used to view up to 5 images at a time.
# It has next and previous buttons to scroll through the pictures, and an exit button to exit the program.
# For the program to work correctly, you must put it in a directory containing a folder named "pictures".
# Inside the "pictures" folder, you'll need 5 .jpg files named "image1.jpg" through "image5.jpg".
# If you want to change the max number of pictures the viewer can show, you can add more "pic#" variables to the code block at line along with the file path to the pictures.
# Once you've done that, make sure to add the new pics to the picList list variable.

from tkinter import * # Imports tkinter, a Python module used to make GUIs.
from PIL import ImageTk,Image # Imports Pillow, a Python module used to display images.

win = Tk() # Creates the window our program's content will be displayed in.
win.title('Tkinter Photo Viewer') # Gives our window a name to be displayed at the top of the window.

# Creates pictures that we will use to populate the pictures list.
pic1 = ImageTk.PhotoImage(Image.open("pictures/image.jpg"))
pic2 = ImageTk.PhotoImage(Image.open("pictures/image2.jpg"))
pic3 = ImageTk.PhotoImage(Image.open("pictures/image3.jpg"))
pic4 = ImageTk.PhotoImage(Image.open("pictures/image4.jpg"))
pic5 = ImageTk.PhotoImage(Image.open("pictures/image5.jpg"))

picList = [pic1, pic2, pic3, pic4, pic5] # Puts each picture into a list.

lbl = Label(image=pic1) # Creates the label where our pictures will be displayed.
lbl.grid(row=0, column=0, columnspan=3) # Places the label created above into the window.

def next(pic_num): # A function to move to the next image.
    global lbl
    global btn_next
    global btn_prvs

    lbl.grid_forget() # Removes current picture to make room for next one.
    lbl = Label(image=picList[pic_num - 1]) # Recreates the label to show the next picture.
    # Recreates the buttons so that they have an updated pic_num variable to work with. Without this, the buttons will not work.
    btn_next = Button(win, text="->", command=lambda: next(pic_num + 1))
    btn_prvs = Button(win, text="<-", command=lambda: prvs(pic_num - 1))

    if pic_num == len(picList): # Disables the next button if there are no pictures to continue forward to.
        btn_next = Button(win, text="->", state = DISABLED)

    # Replaces the current navigation buttons with the updated ones.
    lbl.grid(row=0, column=0, columnspan=3)
    btn_prvs.grid(row=1, column=0)
    btn_next.grid(row=1, column=2)

def prvs(pic_num): # A function to move to the previous image.
    global lbl
    global btn_next
    global btn_prvs

    lbl.grid_forget() # Removes current picture to make room for next one.
    lbl = Label(image=picList[pic_num - 1]) # Recreates the label to show the next picture.
    # Recreates the buttons so that they have an updated pic_num variable to work with. Without this, the buttons will not work.
    btn_next = Button(win, text="->", command=lambda: next(pic_num + 1))
    btn_prvs = Button(win, text="<-", command=lambda: prvs(pic_num - 1))

    if pic_num == 1: # Disables the previous button if there are no pictures to go backwards to.
        btn_prvs = Button(win, text="<-", state = DISABLED)

    # Replaces the current navigation buttons with the updated ones.
    lbl.grid(row=0, column=0, columnspan=3)
    btn_prvs.grid(row=1, column=0)
    btn_next.grid(row=1, column=2)

btn_prvs = Button(win, text="<-", command=prvs, state = DISABLED) # Creates the previous button, used to scroll through the list of images.
btn_exit = Button(win, text="Exit Viewer", command=win.quit) # Creates an exit button for the program, used to exit the program.
btn_next = Button(win, text="->", command=lambda: next(2)) # Creates the next button, used to scroll through the list of images.

# Places navigation/exit buttons into the window.
btn_prvs.grid(row=1, column=0)
btn_exit.grid(row=1, column=1)
btn_next.grid(row=1, column=2)

win.mainloop() # This line tells the program to continue looping through all above lines. Without it, our program would only show up for a split second and would not be usable.


import os
import numpy as np
import pandas as pd

owid=pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')
covid=pd.read_csv(os.path.join(os.getcwd(),'data/covid.csv'),encoding='utf-8',sep=',')

aIndicator=['total_vaccinations','total_tests','hosp_patients']
dIndicator={'Vaccinations':9,'Tests':10,'Hospitalizations':11}

for e in aIndicator:
    #Formatting data
    df=owid.loc[:,['iso_code','continent','location','date',e]].dropna()
    df.iloc[:,2]=df.iloc[:,2].astype(str)
    df.iloc[:,3]=df.iloc[:,3].astype('datetime64[ns]').dt.strftime('%Y-%m-%d')
    covid.iloc[:,0]=covid.iloc[:,0].astype(str)

    df.sort_values(by=['date','location'],inplace=True)
    df.reset_index(inplace=True)
    df.drop(df.columns[0],axis=1,inplace=True)

    #Adding new indicators
    sIndicator=list(dIndicator.keys())[aIndicator.index(e)]
    nIndicator=dIndicator.get(sIndicator)
    covid.insert(nIndicator,sIndicator,0)

    #Dictionary for country & region name
    dict_mc={'US':'United States','Taiwan*':'Taiwan'}
    dict_mr={'Macau':'Macao','Faroe Islands':'Faeroe Islands','Falkland Islands (Malvinas)':'Falkland Islands','Saint Helena, Ascension and Tristan da Cunha':'Saint Helena'}

    #Update country name
    for i in list(dict_mc.keys()):
        df1=covid[covid.iloc[:,1]==i].copy()
        for j in df1.index: covid.iloc[j,1]=dict_mc.get(i)

    #Update region name
    for i in list(dict_mr.keys()):
        df1=covid[covid.iloc[:,0]==i].copy()
        for j in df1.index: covid.iloc[j,0]=dict_mr.get(i)

    for i in range(len(df)):
        #country/region & nan
        try:
            covid.iloc[covid[(covid.iloc[:,1]==df.iloc[i,2]) & (covid['Date']==df.iloc[i,3]) & (covid.iloc[:,0]=='nan')].iloc[:,nIndicator].index[0],nIndicator]=df.iloc[i,4]
        except IndexError as e:
            try:
                #province/state
                covid.iloc[covid[(covid.iloc[:,0]==df.iloc[i,2]) & (covid['Date']==df.iloc[i,3])].iloc[:,nIndicator].index[0],nIndicator]=df.iloc[i,4]
            except IndexError as e1:
                try:
                    #country/region with no nan
                    covid.iloc[covid[(covid.iloc[:,1]==df.iloc[i,2]) & (covid['Date']==df.iloc[i,3])].iloc[:,nIndicator].index[0],nIndicator]=df.iloc[i,4]
                except IndexError as e2:
                    pass

covid.to_csv(os.path.join(os.getcwd(),'data/covid.csv'))
#len(dict(zip(np.unique(np.array(covid.iloc[:,9]),return_counts=True))).keys())


#!/home/mariaharris/.conda/envs/my-rdkit-env/bin/python

"""
Find reactant products for the input SMILES (atom-mapped) string using xTB meta-dynamics
"""

import os
import shutil
import time
import tarfile
import itertools
import pickle
import xyz2mol_local
import numpy as np
import pandas as pd
from rdkit import RDLogger
from rdkit.Chem.rdmolops import GetFormalCharge
from rdkit.Chem import rdmolops

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import rdchem

from rdkit.Chem.rdmolops import FastFindRings

def reorder_atoms_to_map(mol):

    """
    Reorders the atoms in a mol objective to match that of the mapping
    """

    atom_map_order = np.zeros(mol.GetNumAtoms()).astype(int)
    for atom in mol.GetAtoms():
        map_number = atom.GetAtomMapNum()-1
        atom_map_order[map_number] = atom.GetIdx()
    mol = Chem.RenumberAtoms(mol, atom_map_order.tolist())
    return mol

def choose_resonance_structure(mol):
    """
    This function creates all resonance structures of the mol object, counts
    the number of rotatable bonds for each structure and chooses the one with
    fewest rotatable bonds (most 'locked' structure)
    """
    resonance_mols = rdchem.ResonanceMolSupplier(mol,
                                                 rdchem.ResonanceFlags.ALLOW_CHARGE_SEPARATION)
    new_mol = None
    for res_mol in resonance_mols:
        try:
            Chem.SanitizeMol(res_mol)
        except:
            continue
        n_rot_bonds = Chem.rdMolDescriptors.CalcNumRotatableBonds(res_mol)
        if new_mol is None:
            smallest_rot_bonds = n_rot_bonds
            new_mol = res_mol
        if n_rot_bonds < smallest_rot_bonds:
            smallest_rot_bonds = n_rot_bonds
            new_mol = res_mol

    Chem.DetectBondStereochemistry(new_mol, -1)
    rdmolops.AssignStereochemistry(new_mol, flagPossibleStereoCenters=True,
                                   force=True)
    Chem.AssignAtomChiralTagsFromStructure(new_mol, -1)
    return new_mol

def chiral_tags(mol):
    """
    Tag methylene and methyl groups with a chiral tag priority defined
    from the atom index of the hydrogens
    """
    li_list = []
    smarts_ch2 = '[!#1][*]([#1])([#1])([!#1])'
    atom_sets = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts_ch2))
    for atoms in atom_sets:
        atoms = sorted(atoms[2:4])
        prioritized_H = atoms[-1]
        li_list.append(prioritized_H)
        mol.GetAtoms()[prioritized_H].SetAtomicNum(9)
    smarts_ch3 = '[!#1][*]([#1])([#1])([#1])'
    atom_sets = mol.GetSubstructMatches(Chem.MolFromSmarts(smarts_ch3))
    for atoms in atom_sets:
        atoms = sorted(atoms[2:])
        H1 = atoms[-1]
        H2 = atoms[-2]
        li_list.append(H1)
        li_list.append(H2)
        mol.GetAtoms()[H1].SetAtomicNum(9)
        mol.GetAtoms()[H2].SetAtomicNum(9)

    Chem.AssignAtomChiralTagsFromStructure(mol, -1)
    rdmolops.AssignStereochemistry(mol)
    for atom_idx in li_list:
        mol.GetAtoms()[atom_idx].SetAtomicNum(1)

    return mol

def write_xyz_file(mol, file_name):

    """
    Embeds a mol object to get 3D coordinates which are written to an .xyz file
    """

    n_atoms = mol.GetNumAtoms()
    charge = Chem.GetFormalCharge(mol)
    symbols = [a.GetSymbol() for a in mol.GetAtoms()]

    Chem.SanitizeMol(mol)
    rdmolops.AssignStereochemistry(mol)
    AllChem.EmbedMolecule(mol)
    AllChem.MMFFOptimizeMolecule(mol)

    with open(file_name, 'w') as _file:
        _file.write(str(n_atoms)+'\n\n')
        for atom, symbol in enumerate(symbols):
            coord = mol.GetConformers()[0].GetAtomPosition(atom)
            line = " ".join((symbol, str(coord.x), str(coord.y), str(coord.z), "\n"))
            _file.write(line)
        if charge != 0:
            _file.write("$set\n")
            _file.write("chrg "+str(charge)+"\n")
            _file.write("$end")

def submit_md_run(file_name, box_scale, reactant_idx):
    """
    submits a xTB md run and extracts last structure
    """
    jobid = os.popen('../submit_scripts/submit_xtb_md ' + file_name\
            + ' ' + str(box_scale)).read()
    jobid = int(jobid.split()[-1])
    return {jobid}

def check_finished_jobs(job_ids, job_dictionary):
    """
    for the finished jobs: check whether
        - the job excited normally: remove jobid from checked tuple
        - the job excited abnormally: submit the job again
    """

    successful_jobs = set()
    crashed_run_numbers = []
    for jobid in job_ids:
        job_crashed = False
        run_number = job_dictionary[str(jobid)]
        os.chdir('run'+str(run_number))
        with open(str(run_number)+'.out', 'r') as ofile:
            line = ofile.readline()
            while line:
                if 'emergency exit' in line:
                    job_crashed = True
                    crashed_run_numbers.append(run_number)
                line = ofile.readline()
        os.chdir('../')
        if not job_crashed:
            successful_jobs = successful_jobs|{jobid}

    return successful_jobs, crashed_run_numbers

def wait_for_jobs_to_finish(job_ids):
    """
    This function checks with slurm if a specific set of jobids is finished with a
    frequency of 1 minute.
    Stops when the jobs are done.
    """
    while True:
        job_info1 = os.popen("squeue -p mko").readlines()[1:]
        job_info2 = os.popen("squeue -u mariaharris").readlines()[1:]
        current_jobs1 = {int(job.split()[0]) for job in job_info1}
        current_jobs2 = {int(job.split()[0]) for job in job_info2}
        current_jobs = current_jobs1|current_jobs2
        if current_jobs.isdisjoint(job_ids):
            break
        else:
            time.sleep(30)

def wait_for_jobs_to_finish_meta(job_ids, job_dictionary, xyz_file, kpush, alp,
                                 scale_factor):
    """
    This function checks with slurm if a specific set of jobids is finished with a
    frequency of 30 s.
    Stops when the jobs are done.
    """
    while True:
        job_info1 = os.popen("squeue -p mko").readlines()[1:]
        job_info2 = os.popen("squeue -u mariaharris").readlines()[1:]
        current_jobs1 = {int(job.split()[0]) for job in job_info1}
        current_jobs2 = {int(job.split()[0]) for job in job_info2}
        current_jobs = current_jobs1|current_jobs2

        if current_jobs.isdisjoint(job_ids):
            break

        intersection_jobs = current_jobs.intersection(job_ids)
        finished_jobs = job_ids.symmetric_difference(intersection_jobs)
        if finished_jobs:
            successful_jobs, crashed_run_numbers = check_finished_jobs(finished_jobs,
                                                                       job_dictionary)
            job_ids = job_ids.symmetric_difference(finished_jobs)
            for i in crashed_run_numbers:
                shutil.rmtree('run'+str(i))
            new_job_ids, new_job_dictionary = submit_metadynamics_runs(xyz_file, kpush,
                                                                       alp, scale_factor,
                                                                       crashed_run_numbers)
            job_ids = job_ids|new_job_ids
            job_dictionary.update(new_job_dictionary)

        time.sleep(60)

def extract_last_structure(trj_file, last_structure_name):
    """
    Extracts the last structure in a trajectory file
    """

    with open(trj_file, 'r') as _file:
        line = _file.readline()
        n_lines = int(line.split()[0])+2
    count = 0
    input_file = open(trj_file, 'r')
    dest = None
    for line in input_file:
        if count % n_lines == 0:
            if dest:
                dest.close()
            dest = open(last_structure_name, "w")
        count += 1
        dest.write(line)

def calculate_md_relaxed_structure(xyz_file, scale_factor, reactant_index):
    """
    This function submits an md with a box with size scaled by scale_factor and
    extracts last structure of the trajectory file
    """
    jobid = submit_md_run(xyz_file, scale_factor, reactant_index)
    wait_for_jobs_to_finish(jobid)
    trj_file = str(scale_factor)+'_md.trj'
    out_file = str(scale_factor)+'_md.xyz'
    extract_last_structure(trj_file, out_file)
    shutil.copy(out_file, '../')
    return out_file

def submit_metadynamics_runs(xyz_file, k_push, alp, box_scale, run_numbers):
    """
    submits a metadynamics calculation with the indicated parameters
    """
    job_dictionary={}
    jobids = set()
    for i in run_numbers:
        os.mkdir('run'+str(i))
        shutil.copy('metadyn.inp', 'run'+str(i))
        shutil.copy(xyz_file, 'run'+str(i))
        os.chdir('run'+str(i))
        jobid = os.popen('../../../submit_scripts/submit_xtb_meta ' + xyz_file + ' ' + str(k_push) \
        + ' ' + str(alp) + ' ' +  str(box_scale) + ' ' + str(i)).read()
        job_dictionary[jobid.split()[-1]] = str(i)
        jobid = {int(jobid.split()[-1])}
        jobids = jobids|jobid
        os.chdir('../')
    return jobids, job_dictionary

def extract_smiles(xyz_file, charge):
    """
    uses xyz2mol to extract smiles with as much 3d structural information as
    possible
    """
    atoms, _, xyz_coordinates = xyz2mol_local.read_xyz_file(xyz_file)
    input_mol = xyz2mol_local.xyz2mol(atoms, xyz_coordinates, charge=charge,
                                      use_graph=True,
                                      allow_charged_fragments=True,
                                      use_huckel=True, use_atom_maps=True,
                                      embed_chiral=True)
    input_mol = reorder_atoms_to_map(input_mol)
    structure_mol = choose_resonance_structure(input_mol)
    structure_mol = chiral_tags(structure_mol)
    rdmolops.AssignStereochemistry(structure_mol)
    structure_smiles = Chem.MolToSmiles(structure_mol)

    return structure_smiles, GetFormalCharge(structure_mol)

def canonicalize_smiles(structure_smiles):
    """
    remove all structural info an atom mapping information
    """
    mol = Chem.MolFromSmiles(structure_smiles, sanitize=False)
    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(0)
    Chem.SanitizeMol(mol)
    mol = Chem.RemoveHs(mol)
    canonical_smiles = Chem.MolToSmiles(mol)

    return canonical_smiles

def check_opt_xyz(charge, n_steps_list):

    n_steps_list_opt = []
    reactions = []
    canonical_reactants = []
    canonical_products = []
    smiles_list = []

    opt_files = [f for f in os.listdir(os.curdir) if f.endswith("optxyz")]
    opt_files.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))

    for i, optfile in enumerate(opt_files[:-1]):
        try:
            smiles, formal_charge = extract_smiles(optfile, charge)
            print(smiles)
            if not smiles_list and formal_charge == charge:
                smiles_list.append(smiles)
                shutil.copy(optfile, 'optimized_structures')

            elif smiles != smiles_list[-1] and formal_charge == charge:
                reactant = smiles_list[-1]
                reaction = reactant+'>>'+smiles
                reactions.append(reaction)

                canonical_reactant = canonicalize_smiles(smiles_list[-1])
                canonical_reactants.append(canonical_reactant)
                canonical_product = canonicalize_smiles(smiles)
                canonical_products.append(canonical_product)

                smiles_list.append(smiles)
                n_steps_list_opt.append(n_steps_list[i])
                shutil.copy(optfile, 'optimized_structures')
        except ValueError:
            pass
    #print(n_steps_list_opt)

    return reactions, canonical_reactants, canonical_products, n_steps_list_opt

def check_metadyn_runs(n_runs, n_check, charge, combined_df_name):

    jobids = set()
    for i in range(n_runs):
        os.chdir('run'+str(i))
        jobid = os.popen('../../../submit_scripts/submit_python '+\
            '/home/mariaharris/github/ReactionDiscovery/metadynamics_search/split_trajectory.py xtb.trj '\
                         +str(n_check) + ' ' + str(charge) + ' list_file.p').read()
        jobid = {int(jobid.split()[-1])}
        jobids = jobids|jobid
        os.chdir('../')

    wait_for_jobs_to_finish(jobids)

    jobids = set()
    for i in range(n_runs):
        os.chdir('run'+str(i)+'/analyze_trajectory')
        jobid = os.popen('../../../../submit_scripts/submit_batches_xtb_opt').read()
        jobid = {int(jobid.split()[-1])}
        jobids = jobids|jobid
        os.chdir('../../')

    wait_for_jobs_to_finish(jobids)

    combined_df = pd.DataFrame()

    for i in range(n_runs):
        with open('run'+str(i)+'/list_file.p', 'rb') as list_file:
            n_steps_list = pickle.load(list_file)
        os.chdir('run'+str(i)+'/analyze_trajectory')
        tfs = [f for f in os.listdir(os.curdir) if f.endswith("log_xtb.tar.gz")]
        for filename in tfs:
            tf = tarfile.open(filename, mode='r')
            tf.extractall()
            tf.close()
        os.mkdir('optimized_structures')
        reactions, canonical_reactants, canonical_products, n_steps_list_opt = \
                check_opt_xyz(charge, n_steps_list)
        df = pd.DataFrame()
        df['Reactions'] = reactions
        df['Reactants_canonical'] = canonical_reactants
        df['Products_canonical'] = canonical_products
        df['N_steps'] = n_steps_list_opt
        df.to_pickle('dataframe.pkl')
        combined_df = pd.concat([combined_df, df])
        os.chdir('../../')

    combined_df_final = combined_df.groupby(['Reactions', 'Reactants_canonical',
                                             'Products_canonical']).size().reset_index(name='counts')

    print(combined_df_final)
    print(combined_df.groupby(['Reactions', 'Reactants_canonical',
                               'Products_canonical'])['N_steps'].mean())

    combined_df_final['average steps'] = combined_df.groupby(['Reactions', 'Reactants_canonical',
                                                              'Products_canonical'])['N_steps'].mean().values
    combined_df_final['median steps'] = combined_df.groupby(['Reactions', 'Reactants_canonical',
                                                             'Products_canonical'])['N_steps'].median().values
    print(combined_df_final)
    combined_df_final.to_pickle(combined_df_name)

def test_parameters(md_file, scale_factor, k_push, alp, n_runs, n_check, charge):
    """
    Do n_runs metadynamic runs with the specified parameters
    """
    directory_name = str(scale_factor)+'_'+str(k_push)+'_'+str(alp)
    os.mkdir(directory_name)
    shutil.copy('../'+md_file, directory_name)
    shutil.copy('../metadyn.inp', directory_name)
    os.chdir(directory_name)
    jobids, job_dictionary = submit_metadynamics_runs(md_file, k_push, alp, scale_factor,
                                                      range(n_runs))
    wait_for_jobs_to_finish_meta(jobids, job_dictionary, md_file, k_push, alp,
                                 scale_factor)
    #check the trajectory for reactions
    check_metadyn_runs(n_runs, n_check, charge, 'combined_dataframe.pkl')

    os.chdir('../')

if __name__ == "__main__":

    SCALE_FACTOR_LIST = [0.8]
    K_PUSH_LIST = [0.05]
    ALP_LIST = [0.3]

    REACTANT_IDX = 1
    N_CHECK = 10 #check every 10th saved structure in the calculated directory
    N_RUNS = 10 #do N_RUNS metadyn runs per parameter combination
    FILE_NAME = str(REACTANT_IDX) + '.xyz'
    SMILES = '[C:1](=[O:2])([C@:3]([C@:5]([O:8][O:11][H:12])([H:9])[H:10])([H:6])[H:7])[H:4]'
    MOL = Chem.MolFromSmiles(SMILES, sanitize=False)
    MOL = reorder_atoms_to_map(MOL)
    write_xyz_file(MOL, FILE_NAME)

    #Do md runs
    os.mkdir('md')
    shutil.copy(FILE_NAME, 'md')
    shutil.copy('md.inp', 'md')
    os.chdir('md')
    MD_IN_FILE = FILE_NAME
    for scale in SCALE_FACTOR_LIST:
        MD_OUT_FILE = calculate_md_relaxed_structure(MD_IN_FILE, scale, REACTANT_IDX)
        MD_IN_FILE = MD_OUT_FILE
    os.chdir('../')

    #Do metadynamics runs
    os.mkdir('metadynamics')
    os.chdir('metadynamics')

    for SCALE_FACTOR, K_PUSH, ALP in itertools.product(SCALE_FACTOR_LIST,
                                                       K_PUSH_LIST, ALP_LIST):
        MD_FILE = str(SCALE_FACTOR)+'_md.xyz'
        test_parameters(MD_FILE, SCALE_FACTOR, K_PUSH, ALP, N_RUNS, N_CHECK,
                        charge=0)