from django.shortcuts import render
from Electronics.models import ProductType
from Garments.models import GarmentsProductType
from Cosmetics.models import CosmeticsProductType
from DailyNeeds.models import DailyNeedsProductType
from Pharmaceuticals.models import MedicineType
from .models import partner, Quote, Contact, Career, ClientOwnerForm, EmployeeOwnerForm
from django.core.files.storage import FileSystemStorage
from django.contrib import messages

# Create your views here.

#index
def index(request):
    if request.method=="POST":
        name=request.POST['name']
        companyname=request.POST['companyname']
        email=request.POST['email']
        phone=request.POST['phone']
        country=request.POST['country']
        product=request.POST['product']
        quotes =request.POST['quotes']

        if len(name)<2 or len(email)<5 or len(phone)<9 or len(product)<2 or len(quotes)<10:
            messages.error(request, "Please fill the form correctly")
        else:
            quote=Quote(name=name, companyname=companyname, email=email, phone=phone, quotes=quotes, country=country, product=product)
            quote.save()
            messages.success(request, "Your quote has been sent successfully")

    Electronicsitems= ProductType.objects.all()
    Garmentsitems= GarmentsProductType.objects.all()
    Cosmeticsitems= CosmeticsProductType.objects.all()
    DailyNeedsitems= DailyNeedsProductType.objects.all()
    Pharmaceuticals= MedicineType.objects.all()

    context= {
        'Electronicsitems': Electronicsitems,
        'Garmentsitems' : Garmentsitems,
        'Cosmeticsitems' : Cosmeticsitems,
        'DailyNeedsitems' : DailyNeedsitems,
        'Pharmaceuticals': Pharmaceuticals,
        }

    return render(request, 'index.html',context)

#about
def about(request):
    return render(request, 'about.html',)

#mission
def mission(request):
    return render(request, 'mission.html',)

#vision
def vision(request):
    return render(request, 'vision.html',)

#career
def career(request):
    if request.method=="POST":
        firstname=request.POST['firstname']
        lastname=request.POST['lastname']
        email=request.POST['email']
        phone=request.POST['phone']
        city=request.POST['city']
        position=request.POST['position']
        message =request.POST['message']
        cv =request.FILES['cv']

        if len(firstname)<2 or len(lastname)<2 or len(email)<5 or len(phone)<9 or len(position)<2 or len(message)<10:
            messages.error(request, "Please fill the form correctly")
        else:
            career=Career(firstname=firstname, lastname=lastname, email=email, phone=phone, message=message, city=city, position=position, cv=cv)
            career.save()
            messages.success(request, "Your application has been submitted successfully")

    return render(request, 'career.html',)

#policy
def policy(request):
    return render(request, 'PrivacyPolicy.html',)

#partners
def partners(request):
    partners = partner.objects.all()
    contex = {'partners': partners}

    return render(request, 'partners.html', contex)

#faq
def faq(request):
    return render(request, 'faq.html',)

#clientform
def clientform(request):
    # owner form
    if request.method=="POST":
        if 'ownerform' in request.POST:
            ownerName = request.POST['ownerName']
            ownerCompanyname = request.POST['ownerCompanyname']
            ownerDesignation = request.POST['ownerDesignation']
            ownerBusinessLocation = request.POST['ownerBusinessLocation']
            ownerBusinessType = request.POST['ownerBusinessType']
            ownerAddress = request.POST['ownerAddress']
            ownerCity = request.POST['ownerCity']
            ownerState = request.POST['ownerState']
            ownerZip = request.POST['ownerZip']
            ownerEmail = request.POST['ownerEmail']
            ownerPhone =request.POST['ownerPhone']
            ownerFax = request.POST['ownerFax']
            ownerWebsite = request.POST['ownerWebsite']
            ownerMessage = request.POST['ownerMessage']

            if len(ownerName)<2 or len(ownerCompanyname)<2 or len(ownerEmail)<5 or len(ownerPhone)<9 or len(ownerAddress)<2 or len(ownerMessage)<10:
                messages.error(request, "Please fill the form correctly")
            else:
                clientform=ClientOwnerForm(ownerName=ownerName, ownerCompanyname=ownerCompanyname, ownerDesignation=ownerDesignation, ownerBusinessLocation=ownerBusinessLocation, ownerBusinessType=ownerBusinessType, ownerAddress=ownerAddress, ownerCity=ownerCity, ownerState=ownerState, ownerZip=ownerZip, ownerEmail=ownerEmail, ownerPhone=ownerPhone, ownerFax=ownerFax, ownerWebsite=ownerWebsite, ownerMessage=ownerMessage)
                clientform.save()
                messages.success(request, "Your message has been sent successfully")

    # employee form
        elif 'employeeform' in request.POST:
            EmployeeName = request.POST['EmployeeName']
            EmployeeCompanyname = request.POST['EmployeeCompanyname']
            EmployeeDesignation = request.POST['EmployeeDesignation']
            EmployeeBusinessLocation = request.POST['EmployeeBusinessLocation']
            EmployeeBusinessType = request.POST['EmployeeBusinessType']
            EmployeeAddress = request.POST['EmployeeAddress']
            EmployeeCity = request.POST['EmployeeCity']
            EmployeeState = request.POST['EmployeeState']
            EmployeeZip = request.POST['EmployeeZip']
            EmployeeEmail = request.POST['EmployeeEmail']
            EmployeePhone =request.POST['EmployeePhone']
            EmployeeFax = request.POST['EmployeeFax']
            EmployeeWebsite = request.POST['EmployeeWebsite']

            OwnerName = request.POST['OwnerName']
            OwnerDesignation = request.POST['OwnerDesignation']
            OwnerAddress = request.POST['OwnerAddress']
            OwnerCity = request.POST['OwnerCity']
            OwnerState = request.POST['OwnerState']
            OwnerZip = request.POST['OwnerZip']
            OwnerEmail = request.POST['OwnerEmail']
            OwnerPhone =request.POST['OwnerPhone']

            EmployeeMessage = request.POST['EmployeeMessage']

            if len(EmployeeName)<2 or len(EmployeeCompanyname)<2 or len(EmployeeEmail)<5 or len(EmployeePhone)<9 or len(EmployeeAddress)<2 or len(EmployeeMessage)<10:
                messages.error(request, "Please fill the form correctly")
            else:
                employeeform=EmployeeOwnerForm(EmployeeName=EmployeeName, EmployeeCompanyname=EmployeeCompanyname, EmployeeDesignation=EmployeeDesignation, EmployeeBusinessLocation=EmployeeBusinessLocation, EmployeeBusinessType=EmployeeBusinessType, EmployeeAddress=EmployeeAddress, EmployeeCity=EmployeeCity, EmployeeState=EmployeeState, EmployeeZip=EmployeeZip, EmployeeEmail=EmployeeEmail, EmployeePhone=EmployeePhone, EmployeeFax=EmployeeFax, EmployeeWebsite=EmployeeWebsite, EmployeeMessage=EmployeeMessage,
                OwnerName=OwnerName, OwnerDesignation=OwnerDesignation, OwnerAddress=OwnerAddress, OwnerCity=OwnerCity, OwnerState=OwnerState, OwnerZip=OwnerZip, OwnerEmail=OwnerEmail, OwnerPhone=OwnerPhone)
                employeeform.save()
                messages.success(request, "Your message has been sent successfully")

    return render(request, 'clientform.html',)

#contact
def contact(request):
    if request.method=="POST":
        firstname = request.POST['firstname']
        lastname = request.POST['lastname']
        companyname = request.POST['companyname']
        email = request.POST['email']
        phoneno = request.POST['phoneno']
        address = request.POST['address']
        address2 = request.POST['address2']
        city = request.POST['city']
        state = request.POST['state']
        zipcode = request.POST['zipcode']
        message = request.POST['message']
        if len(firstname)<2 or len(lastname)<2 or len(email)<5 or len(phoneno)<9 or len(address)<2 or len(message)<10:
            messages.error(request, "Please fill the form correctly")
        else:
            contact=Contact(firstname=firstname, lastname=lastname, companyname=companyname, email=email, phoneno=phoneno, city=city, state=state, zipcode=zipcode, address=address, address2=address2, message=message)
            contact.save()
            messages.success(request, "Your message has been sent successfully")

    return render(request, 'contact.html',)

#quote
def quote(request):
    if request.method=="POST":
        name=request.POST['name']
        companyname=request.POST['companyname']
        email=request.POST['email']
        phone=request.POST['phone']
        country=request.POST['country']
        product=request.POST['product']
        quotes =request.POST['quotes']

        if len(name)<2 or len(email)<5 or len(phone)<9 or len(product)<2 or len(quotes)<10:
            messages.error(request, "Please fill the form correctly")
        else:
            quote=Quote(name=name, companyname=companyname, email=email, phone=phone, quotes=quotes, country=country, product=product)
            quote.save()
            messages.success(request, "Your quote has been sent successfully")
    return render(request, 'quote.html',)


from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import six

from google.appengine.ext import ndb

from dashboard import alerts
from dashboard.api import api_request_handler
from dashboard.api import utils as api_utils
from dashboard.common import descriptor
from dashboard.common import timing
from dashboard.common import utils
from dashboard.models import anomaly
from dashboard.models import graph_data
from dashboard.models import histogram

# These limits should prevent DeadlineExceededErrors.
# TODO(benjhayden): Find a better strategy for staying under the deadline.
DIAGNOSTICS_QUERY_LIMIT = 10000
HISTOGRAMS_QUERY_LIMIT = 1000
ROWS_QUERY_LIMIT = 20000

COLUMNS_REQUIRING_ROWS = {'timestamp', 'revisions',
                          'annotations'}.union(descriptor.STATISTICS)

from flask import request

def _CheckUser():
  pass

@api_request_handler.RequestHandlerDecoratorFactory(_CheckUser)
def TimeSeries2Post():
  desc = descriptor.Descriptor(
      test_suite=request.values.get('test_suite'),
      measurement=request.values.get('measurement'),
      bot=request.values.get('bot'),
      test_case=request.values.get('test_case'),
      statistic=request.values.get('statistic', None),
      build_type=request.values.get('build_type'))
  min_revision = request.values.get('min_revision')
  min_revision = int(min_revision) if min_revision else None
  max_revision = request.values.get('max_revision')
  max_revision = int(max_revision) if max_revision else None
  query = TimeseriesQuery(
      desc,
      request.values.get('columns').split(','), min_revision, max_revision,
      api_utils.ParseISO8601(request.values.get('min_timestamp', None)),
      api_utils.ParseISO8601(request.values.get('max_timestamp', None)))
  try:
    result = query.FetchSync()
  except AssertionError as e:
    # The caller has requested internal-only data but is not authorized.
    six.raise_from(api_request_handler.NotFoundError, e)
  return result

class TimeseriesQuery:

  def __init__(self,
               desc,
               columns,
               min_revision=None,
               max_revision=None,
               min_timestamp=None,
               max_timestamp=None):
    self._descriptor = desc
    self._columns = columns
    self._min_revision = min_revision
    self._max_revision = max_revision
    self._min_timestamp = min_timestamp
    self._max_timestamp = max_timestamp
    self._statistic_columns = []
    self._unsuffixed_test_metadata_keys = []
    self._test_keys = []
    self._test_metadata_keys = []
    self._units = None
    self._improvement_direction = None
    self._data = {}
    self._private = False

  @property
  def private(self):
    return self._private

  @timing.TimeWall('fetch')
  @timing.TimeCpu('fetch')
  def FetchSync(self):
    return self.FetchAsync().get_result()

  @ndb.tasklet
  def FetchAsync(self):
    """Returns {units, improvement_direction, data}.

    Raises:
      api_request_handler.NotFoundError when the timeseries cannot be found.
      AssertionError when an external user requests internal-only data.
    """
    # Always need to query TestMetadata even if the caller doesn't need units
    # and improvement_direction because Row doesn't have internal_only.
    # Use tasklets so that they can process the data as it arrives.
    self._CreateTestKeys()
    futures = [self._FetchTests()]
    if COLUMNS_REQUIRING_ROWS.intersection(self._columns):
      futures.append(self._FetchRows())
    elif 'histogram' in self._columns:
      futures.append(self._FetchHistograms())
    if 'alert' in self._columns:
      self._ResolveTimestamps()
      futures.append(self._FetchAlerts())
    if 'diagnostics' in self._columns:
      self._ResolveTimestamps()
      futures.append(self._FetchDiagnostics())
    yield futures
    raise ndb.Return({
        'units':
            self._units,
        'improvement_direction':
            self._improvement_direction,
        'data': [[datum.get(col)
                  for col in self._columns]
                 for _, datum in sorted(self._data.items())],
    })

  def _ResolveTimestamps(self):
    if self._min_timestamp and self._min_revision is None:
      self._min_revision = self._ResolveTimestamp(self._min_timestamp)
    if self._max_timestamp and self._max_revision is None:
      self._max_revision = self._ResolveTimestamp(self._max_timestamp)

  def _ResolveTimestamp(self, timestamp):
    query = graph_data.Row.query(
        graph_data.Row.parent_test.IN(self._test_keys),
        graph_data.Row.timestamp <= timestamp)
    query = query.order(-graph_data.Row.timestamp)
    row_keys = query.fetch(1, keys_only=True)
    if not row_keys:
      return None
    return row_keys[0].integer_id()

  def _CreateTestKeys(self):
    desc = self._descriptor.Clone()

    self._statistic_columns = [
        col for col in self._columns if col in descriptor.STATISTICS
    ]
    if desc.statistic and desc.statistic not in self._statistic_columns:
      self._statistic_columns.append(desc.statistic)

    desc.statistic = None
    unsuffixed_test_paths = desc.ToTestPathsSync()
    self._unsuffixed_test_metadata_keys = [
        utils.TestMetadataKey(path) for path in unsuffixed_test_paths
    ]

    test_paths = []
    for statistic in self._statistic_columns:
      desc.statistic = statistic
      test_paths.extend(desc.ToTestPathsSync())

    self._test_metadata_keys = [
        utils.TestMetadataKey(path) for path in test_paths
    ]
    self._test_metadata_keys.extend(self._unsuffixed_test_metadata_keys)
    test_paths.extend(unsuffixed_test_paths)

    test_old_keys = [utils.OldStyleTestKey(path) for path in test_paths]
    self._test_keys = test_old_keys + self._test_metadata_keys

  @ndb.tasklet
  def _FetchTests(self):
    # Don't fetch OldStyleTestKeys. The Test model has been removed. Only fetch
    # TestMetadata entities.
    with timing.WallTimeLogger('fetch_tests'):
      tests = yield [key.get_async() for key in self._test_metadata_keys]
    tests = [test for test in tests if test]
    if not tests:
      raise api_request_handler.NotFoundError

    improvement_direction = None
    for test in tests:
      if test.internal_only:
        self._private = True

      test_desc = yield descriptor.Descriptor.FromTestPathAsync(
          utils.TestPath(test.key))
      # The unit for 'count' statistics is trivially always 'count'. Callers
      # certainly want the units of the measurement, which is the same as the
      # units of the 'avg' and 'std' statistics.
      if self._units is None or test_desc.statistic != 'count':
        self._units = test.units
        improvement_direction = test.improvement_direction

    if improvement_direction == anomaly.DOWN:
      self._improvement_direction = 'down'
    elif improvement_direction == anomaly.UP:
      self._improvement_direction = 'up'
    else:
      self._improvement_direction = None

  def _Datum(self, revision):
    return self._data.setdefault(revision, {'revision': revision})

  @ndb.tasklet
  def _FetchRows(self):
    yield [self._FetchRowsForTest(test_key) for test_key in self._test_keys]

  @staticmethod
  def Round(x):
    return round(x, 6)

  def _RowQueryProjection(self, statistic):
    limit = ROWS_QUERY_LIMIT
    projection = None

    # revisions and annotations are not in any index, so a projection query
    # can't get them.
    if 'revisions' in self._columns or 'annotations' in self._columns:
      return projection, limit

    # Disable projection queries for timestamp for now. There's just an index
    # for ascending revision, not descending revision with timestamp.
    if 'timestamp' in self._columns:
      return projection, limit

    # There is no index like (parent_test, -timestamp, revision, value):
    self._ResolveTimestamps()

    # If statistic is not None, then project value. _CreateTestKeys will
    # generate test keys for the other statistics in columns if there are any.
    # If statistic is None and the only statistic is avg, then project value.
    # If statistic is None and there are multiple statistics, then fetch full
    # Row entities because we might need their 'error' aka 'std' or
    # 'd_'-prefixed statistics.
    if ((statistic is not None)
        or (','.join(self._statistic_columns) == 'avg')):
      projection = ['revision', 'value']
      if 'timestamp' in self._columns:
        projection.append('timestamp')
      limit = None

    return projection, limit

  @ndb.tasklet
  def _FetchRowsForTest(self, test_key):
    test_desc = yield descriptor.Descriptor.FromTestPathAsync(
        utils.TestPath(test_key))
    projection, limit = self._RowQueryProjection(test_desc.statistic)
    query = graph_data.Row.query(projection=projection)
    query = query.filter(graph_data.Row.parent_test == test_key)
    query = self._FilterRowQuery(query)

    with timing.WallTimeLogger('fetch_test'):
      rows = yield query.fetch_async(limit)

    with timing.CpuTimeLogger('rows'):
      for row in rows:
        # Sometimes the dev environment just ignores some filters.
        if self._min_revision and row.revision < self._min_revision:
          continue
        if self._min_timestamp and row.timestamp < self._min_timestamp:
          continue
        if self._max_revision and row.revision > self._max_revision:
          continue
        if self._max_timestamp and row.timestamp > self._max_timestamp:
          continue
        datum = self._Datum(row.revision)
        if test_desc.statistic is None:
          datum['avg'] = self.Round(row.value)
          if hasattr(row, 'error') and row.error:
            datum['std'] = self.Round(row.error)
        else:
          datum[test_desc.statistic] = self.Round(row.value)
        for stat in self._statistic_columns:
          if hasattr(row, 'd_' + stat):
            datum[stat] = self.Round(getattr(row, 'd_' + stat))
        if 'timestamp' in self._columns:
          datum['timestamp'] = row.timestamp.isoformat()
        if 'revisions' in self._columns:
          datum['revisions'] = {
              attr: value
              for attr, value in row.to_dict().items()
              if attr.startswith('r_')
          }
        if 'annotations' in self._columns:
          datum['annotations'] = {
              attr: value
              for attr, value in row.to_dict().items()
              if attr.startswith('a_')
          }

    if 'histogram' in self._columns and test_desc.statistic == None:
      with timing.WallTimeLogger('fetch_histograms'):
        yield [self._FetchHistogram(test_key, row.revision) for row in rows]

  def _FilterRowQuery(self, query):
    if self._min_revision or self._max_revision:
      if self._min_revision:
        query = query.filter(graph_data.Row.revision >= self._min_revision)
      if self._max_revision:
        query = query.filter(graph_data.Row.revision <= self._max_revision)
      query = query.order(-graph_data.Row.revision)  # pylint: disable=invalid-unary-operand-type
    elif self._min_timestamp or self._max_timestamp:
      if self._min_timestamp:
        query = query.filter(graph_data.Row.timestamp >= self._min_timestamp)
      if self._max_timestamp:
        query = query.filter(graph_data.Row.timestamp <= self._max_timestamp)
      query = query.order(-graph_data.Row.timestamp)
    else:
      query = query.order(-graph_data.Row.revision)  # pylint: disable=invalid-unary-operand-type
    return query

  @ndb.tasklet
  def _FetchAlerts(self):
    anomalies, _, _ = yield anomaly.Anomaly.QueryAsync(
        test_keys=self._test_keys,
        max_start_revision=self._max_revision,
        min_end_revision=self._min_revision)
    for alert in anomalies:
      if alert.internal_only:
        self._private = True
      datum = self._Datum(alert.end_revision)
      # TODO(benjhayden) bisect_status
      datum['alert'] = alerts.AnomalyDicts([alert], v2=True)[0]

  @ndb.tasklet
  def _FetchHistograms(self):
    yield [self._FetchHistogramsForTest(test) for test in self._test_keys]

  @ndb.tasklet
  def _FetchHistogramsForTest(self, test):
    query = graph_data.Row.query(graph_data.Row.parent_test == test)
    query = self._FilterRowQuery(query)
    with timing.WallTimeLogger('fetch_histograms'):
      row_keys = yield query.fetch_async(HISTOGRAMS_QUERY_LIMIT, keys_only=True)
      yield [
          self._FetchHistogram(test, row_key.integer_id())
          for row_key in row_keys
      ]

  @ndb.tasklet
  def _FetchHistogram(self, test, revision):
    query = histogram.Histogram.query(
        histogram.Histogram.test == utils.TestMetadataKey(test),
        histogram.Histogram.revision == revision)
    hist = yield query.get_async()
    if hist is None:
      return
    if hist.internal_only:
      self._private = True
    self._Datum(hist.revision)['histogram'] = hist.data

  @ndb.tasklet
  def _FetchDiagnostics(self):
    with timing.WallTimeLogger('fetch_diagnosticss'):
      yield [
          self._FetchDiagnosticsForTest(test)
          for test in self._unsuffixed_test_metadata_keys
      ]

  @ndb.tasklet
  def _FetchDiagnosticsForTest(self, test):
    query = histogram.SparseDiagnostic.query(
        histogram.SparseDiagnostic.test == test)
    if self._min_revision:
      query = query.filter(
          histogram.SparseDiagnostic.start_revision >= self._min_revision)
    if self._max_revision:
      query = query.filter(
          histogram.SparseDiagnostic.start_revision <= self._max_revision)
    query = query.order(-histogram.SparseDiagnostic.start_revision)
    diagnostics = yield query.fetch_async(DIAGNOSTICS_QUERY_LIMIT)
    for diag in diagnostics:
      if diag.internal_only:
        self._private = True
      datum = self._Datum(diag.start_revision)
      datum_diags = datum.setdefault('diagnostics', {})
      datum_diags[diag.name] = diag.data


import ckan.lib.app_globals as app_globals
import ckan.lib.i18n as i18n
import ckan.model as model
import ckan.plugins.toolkit as t
import datetime as d
from ckan.lib.render import TemplateNotFound
from ckan.common import OrderedDict
from pylons import config

log = __import__('logging').getLogger(__name__)
namespace = 'ckanext.federagobes'

c = t.c

class RDFController(t.BaseController):

	def view(self):

		context = {'model': model, 'session': model.Session,
					'user': c.user, 'for_view': True,
					'auth_user_obj': c.userobj}

		# Get the plugin configuration
		site_url = config.get('ckan.site_url') + config.get('ckan.root_path').replace('{{LANG}}', '')
		date_created = config.get(namespace + '.create_date')
		date_modified = d.datetime.now().isoformat()
		publisher = config.get(namespace + '.publisher')
		theme_taxonomy = config.get(namespace + '.theme_taxonomy')
		license_url = config.get(namespace + '.license_url')
		catalog_title = {}
		catalog_desc = {}

		for locale in i18n.get_locales():
			catalog_title[locale] = config.get(namespace + '.catalog_title.' + locale)
			catalog_desc[locale] = config.get(namespace + '.catalog_desc.' + locale)

		# Get the dataset catalogue
		packages = t.get_action('package_search')(context, {
				'include_private': False,
				'rows': 1000
			})
		packages = packages['results']

		t.response.headers['Content-Type'] = 'application/rdf+xml; charset=utf-8'
		return t.render('federador.xml', extra_vars={
				'site_url': site_url,
				'date_created': date_created,
				'date_modified': date_modified,
				'publisher': publisher,
				'theme_taxonomy': theme_taxonomy,
				'license_url': license_url,
				'packages': packages,
				'catalog_title': catalog_title,
				'catalog_desc': catalog_desc
			})


import os
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from generators.gpt3_5_turbo import ChatGPT
from generators.openAiParameters import Parameters
from detectors.turnitin_detector import TurnitIn
from detectors.open_ai_classifier import OpenAiClassifier
import logging

class ParameterOptimizer:
    def __init__(self, generator, turnitin, gpt2_detector, openai_classifier, max_tokens, param_name, param_bounds, param_step, texts_dir_path, result_dir_path,
                 log_dir_path, train_prompts):
        self.generator = generator
        self.turnitin = turnitin
        self.gpt2_detector = gpt2_detector
        self.openai_classifier = openai_classifier
        self.max_tokens = max_tokens
        self.param_name = param_name
        self.param_bounds = param_bounds
        self.param_step = param_step
        self.texts_dir_path = texts_dir_path
        self.result_dir_path = result_dir_path
        self.log_dir_path = log_dir_path
        self.train_prompts = train_prompts
        self.file_paths_dict = {}
        logging.basicConfig(filename=os.path.join(self.log_dir_path, 'parameter_optimization.log'), level=logging.INFO,
                            format='%(asctime)s %(message)s')

    def generate_and_save_texts(self):
        param_values = np.arange(self.param_bounds[0], self.param_bounds[1] + self.param_step, self.param_step)
        for param_value in param_values:
            param_value = round(param_value.item(), 2)
            parameters = Parameters(max_tokens=self.max_tokens)
            parameters.__dict__[self.param_name] = param_value
            print(parameters.__dict__)
            logging.info(parameters.__dict__)

            for idx, prompt in enumerate(self.train_prompts):
                file_name = f"{self.param_name}_{param_value}_prompt_{idx}.txt"
                self.generator.generate_prompt_save_txt(prompt, parameters, self.texts_dir_path, file_name)
                logging.info(prompt)
                file_path = os.path.join(self.texts_dir_path, file_name)
                # Add the file path to the dictionary
                self.file_paths_dict.setdefault(f"{self.param_name}_{param_value}", []).append(file_path)

        # Return file paths
        return self.file_paths_dict

    def submit_and_calculate_scores_turnitin(self, file_paths_dict, prompts_list, waittime, single_file_waittime):
        average_scores_dict = {}
        null_counts_dict = {}  # New dictionary to count null values
        null_present = True  # to keep the loop going until no nulls found

        while null_present:
            null_present = False  # reset for each complete run
            scores_dict = self.turnitin.submit_and_scrape_existing_files(file_paths_dict, waittime)
            # Creating a temporary dictionary to hold file paths with None scores
            file_paths_dict_temp = {}
            for param_value, scores in scores_dict.items():
                logging.info(f"Scores for {self.param_name} = {param_value}: {scores}")
                new_scores = [round(score / 100, 2) if score is not None else None for score in scores]

                if param_value not in null_counts_dict:
                    null_counts_dict[param_value] = 0  # Initialize count if not exist

                # Regenerate text for null scores and re-check
                for i, score in enumerate(new_scores):
                    if score is None:
                        null_present = True  # null found, another run required
                        null_counts_dict[param_value] += 1  # Increase the count
                        file_path = file_paths_dict[param_value][i]
                        directory, filename = os.path.split(file_path)
                        prompt_index = int(filename.split('_')[-1].split('.')[0])  # Extract prompt index from filename
                        prompt = prompts_list[prompt_index]  # Retrieve the prompt from the prompt list
                        print(f"Null text: {file_path}, Score value: {score}, Prompt index: {prompt_index}")

                        # Prepare parameters
                        parameters = Parameters(max_tokens=self.max_tokens)
                        parameters.__dict__[self.param_name] = float(param_value.split('_')[-1])

                        # Generate new texts
                        print(
                            f"generating new text for {filename} with filepath={file_path} and parameters: {parameters}")
                        self.generator.generate_prompt_save_txt(prompt, parameters, directory, filename)

                        # Add the regenerated file's path to the temporary dictionary
                        if param_value not in file_paths_dict_temp:
                            file_paths_dict_temp[param_value] = []
                        file_paths_dict_temp[param_value].append(file_path)

                        # Check the scores again
                        single_file_dict = {param_value: [file_path]}  # Adjust the dict for the specific file
                        new_score_dict = self.turnitin.submit_and_scrape_existing_files(single_file_dict,
                                                                                single_file_waittime)
                        new_scores[i] = new_score_dict[param_value][0]

                total = sum(score for score in new_scores if score is not None)
                average_scores_dict[param_value] = total / len(new_scores)
            # Update file_paths_dict to include only file paths which had None scores in the current iteration
            file_paths_dict = file_paths_dict_temp

        return average_scores_dict, null_counts_dict

    def submit_and_calculate_scores_openai(self, file_paths_dict):
        scores_dict = {}
        for param, file_paths in file_paths_dict.items():
            scores_list = []
            for file_path in file_paths:
                print(f"OpenAI: {param}: {file_path}")
                with open(file_path, 'r', encoding='utf-8') as file:  # Open each file
                    file_content = file.read()  # Read the content of the file
                    score = self.openai_classifier.submit_and_scrape(file_content)  # Execute the submit_and_scrape function
                    scores_list.append(score)  # Append the score to the scores list
            new_scores = [round(score, 2) for score in scores_list]
            scores_dict[param] = sum(new_scores) / len(scores_list)  # Assign the scores list to the corresponding key in the dictionary
        return scores_dict

    def submit_and_calculate_scores_gpt2(self, file_paths_dict):
        scores_dict = {}
        for param, file_paths in file_paths_dict.items():
            scores_list = []
            for file_path in file_paths:
                print(f"GPT2: {param}: {file_path}")
                with open(file_path, 'r', encoding='utf-8') as file:  # Open each file
                    file_content = file.read()  # Read the content of the file
                    score = self.gpt2_detector.submit_and_scrape(
                        file_content).fake_probability  # Execute the submit_and_scrape function
                    scores_list.append(score)  # Append the score to the scores list
            new_scores = [round(score, 2) for score in scores_list]
            scores_dict[param] = sum(new_scores) / len(scores_list)
        return scores_dict

    def save_scores_to_csv(self, scores_dict, detector_name, param_name):
        new_scores_dict = {float(key.split('_')[-1]): value for key, value in scores_dict.items()}
        df = pd.DataFrame(list(new_scores_dict.items()), columns=[param_name, 'Score'])
        df.to_csv(f'{self.result_dir_path}/{self.param_name}_{detector_name}_scores.csv', index=False)

    def load_scores_from_csv(self, detector_name, param_name):
        # Define the path to the csv file
        file_path = f'{self.result_dir_path}/{self.param_name}_{detector_name}_scores.csv'

        # Read the csv file into a DataFrame
        df = pd.read_csv(file_path)

        # Convert the DataFrame back into a dictionary
        scores_dict = {f"{self.param_name}_{row[param_name]}": row['Score'] for index, row in df.iterrows()}

        return scores_dict

    def plot_scores(self, scores_dict, detector_name, param_name):
        new_scores_dict = {float(key.split('_')[-1]): value for key, value in scores_dict.items()}
        plt.plot(list(new_scores_dict.keys()), list(new_scores_dict.values()), label=f'{detector_name} score')
        plt.legend()
        plt.xlabel(param_name)
        plt.ylabel(f'{detector_name} score')
        plt.title('Parameter tuning results')
        plt.savefig(f'{self.result_dir_path}/{self.param_name}_{detector_name}_scores.png')
        plt.show()

    def save_scores_to_combined_csv(self, scores_dicts, detector_names, param_name):
        df_combined = None
        for scores_dict, detector_name in zip(scores_dicts, detector_names):
            new_scores_dict = {float(key.split('_')[-1]): value for key, value in scores_dict.items()}
            df = pd.DataFrame(list(new_scores_dict.items()), columns=[param_name, detector_name])
            if df_combined is None:
                df_combined = df
            else:
                df_combined = df_combined.merge(df, on=param_name)
        df_combined.to_csv(f'{self.result_dir_path}/{self.param_name}_combined_scores.csv', index=False)

    def plot_combined_scores(self, scores_dicts, detector_names, param_name):
        plt.figure(figsize=(5, 4))
        for scores_dict, detector_name in zip(scores_dicts, detector_names):
            new_scores_dict = {float(key.split('_')[-1]): value for key, value in scores_dict.items()}
            plt.plot(list(new_scores_dict.keys()), list(new_scores_dict.values()), label=f'{detector_name} score')
        plt.legend()
        plt.xlabel(param_name)
        plt.ylabel('Score')
        plt.title('Parameter tuning results')
        plt.savefig(f'{self.result_dir_path}/{self.param_name}_combined_scores.png')
        plt.show()

    def save_null_counts_to_csv(self, null_counts_dict, param_name):
        df = pd.DataFrame(list(null_counts_dict.items()), columns=[param_name, 'Null Counts'])
        df.to_csv(f'{self.result_dir_path}/{self.param_name}_null_counts.csv', index=False)

    def plot_null_counts(self, null_counts_dict, param_name):
        plt.bar(list(null_counts_dict.keys()), list(null_counts_dict.values()), label='Null counts')
        plt.xlabel(param_name)
        plt.ylabel('Null counts')
        plt.title('Number of documents unclassified by Turnitin')
        plt.savefig(f'{self.result_dir_path}/{self.param_name}_null_counts.png')
        plt.show()


import os
import shutil
import tempfile
import unittest
from unittest.mock import call, patch

import pytest
from clinv.inventory import Inventory
from yaml import YAMLError

class ClinvBaseTestClass(object):
    """
    Base class to setup the setUp and tearDown methods for the test cases.
    """

    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self.inventory_dir = self.tmp
        self.logging_patch = patch("clinv.inventory.logging", autospect=True)
        self.logging = self.logging_patch.start()
        self.print_patch = patch("clinv.inventory.print", autospect=True)
        self.print = self.print_patch.start()

    def tearDown(self):
        self.logging_patch.stop()
        self.print_patch.stop()
        shutil.rmtree(self.tmp)

class InventoryBaseTestClass(ClinvBaseTestClass):
    """
    Base class to setup the setUp and tearDown methods for the Inventory test
    cases.
    """

    def setUp(self):
        super().setUp()
        self.source_data_path = os.path.join(self.inventory_dir, "source_data.yaml",)
        self.user_data_path = os.path.join(self.inventory_dir, "user_data.yaml",)

    def tearDown(self):
        super().tearDown()

class TestInventory(InventoryBaseTestClass, unittest.TestCase):
    """
    Test class to assess that the Inventory class works as expected
    """

    def setUp(self):
        super().setUp()
        self.source_plugins = []

        self.inv = Inventory(self.inventory_dir, self.source_plugins)

    def tearDown(self):
        super().tearDown()

    def test_init_sets_inventory_dir(self):
        self.assertEqual(self.inv.inventory_dir, self.inventory_dir)

    def test_init_sets_source_plugins(self):
        self.assertEqual(self.inv._source_plugins, self.source_plugins)

    def test_init_sets_source_data_path(self):
        self.assertEqual(self.inv.source_data_path, self.source_data_path)

    def test_init_sets_user_data_path(self):
        self.assertEqual(self.inv.user_data_path, self.user_data_path)

    def test_yaml_saving(self):
        save_file = os.path.join(self.tmp, "yaml_save_test.yaml")
        dictionary = {"a": "b", "c": "d"}
        self.inv._save_yaml(save_file, dictionary)
        with open(save_file, "r") as f:
            self.assertEqual("a: b\nc: d\n", f.read())

    def test_load_yaml(self):
        with open(self.source_data_path, "w") as f:
            f.write("test: this is a test")
        yaml_content = self.inv._load_yaml(self.source_data_path)
        self.assertEqual(yaml_content["test"], "this is a test")

    @patch("clinv.inventory.os")
    @patch("clinv.inventory.yaml")
    def test_load_yaml_raises_error_if_wrong_format(self, yamlMock, osMock):
        yamlMock.safe_load.side_effect = YAMLError("error")

        with self.assertRaises(YAMLError):
            self.inv._load_yaml(self.source_data_path)
        self.assertEqual(
            str(self.logging.getLogger.return_value.error.mock_calls),
            str([call(YAMLError("error"))]),
        )

    @patch("clinv.inventory.open")
    def test_load_yaml_raises_error_if_file_not_found(self, openMock):
        openMock.side_effect = FileNotFoundError()

        with self.assertRaises(FileNotFoundError):
            self.inv._load_yaml(self.source_data_path)
        self.assertEqual(
            str(self.logging.getLogger.return_value.error.mock_calls),
            str([call("Error opening yaml file {}".format(self.source_data_path))]),
        )

    @patch("clinv.inventory.Inventory._load_yaml")
    def test_load_loads_source_data(self, loadMock):
        self.inv.load()
        self.assertTrue(call(self.source_data_path) in loadMock.mock_calls)
        self.assertEqual(self.inv.source_data, loadMock())

    @patch("clinv.inventory.Inventory._load_yaml")
    def test_load_loads_user_data(self, loadMock):
        self.inv.load()
        self.assertTrue(call(self.user_data_path) in loadMock.mock_calls)
        self.assertEqual(self.inv.user_data, loadMock())

    @patch("clinv.inventory.Inventory._load_yaml")
    @patch("clinv.inventory.Inventory._load_plugins")
    @patch("clinv.inventory.Inventory._generate_inventory_objects")
    def test_load_loads_plugins(self, invMock, loadMock, yamlMock):
        self.inv.load()
        self.assertTrue(loadMock.called)

    @patch("clinv.inventory.Inventory._load_yaml")
    @patch("clinv.inventory.Inventory._generate_inventory_objects")
    def test_load_generates_inventory_objects(self, invMock, yamlMock):
        self.inv.load()
        self.assertTrue(invMock.called)

    @patch("clinv.inventory.Inventory._save_yaml")
    def test_save_saves_source_data(self, saveMock):
        self.inv.save()
        self.assertTrue(
            call(self.source_data_path, self.inv.source_data) in saveMock.mock_calls
        )

    @patch("clinv.inventory.Inventory._save_yaml")
    def test_save_saves_user_data(self, saveMock):
        self.inv.save()
        self.assertTrue(
            call(self.user_data_path, self.inv.user_data) in saveMock.mock_calls
        )

class TestInventoryPluginLoad(InventoryBaseTestClass, unittest.TestCase):
    """
    Test class to assess that the Inventory plugins methods work as expected
    """

    def setUp(self):
        super().setUp()
        self.source_patch = patch("clinv.inventory.aws.Route53src", autospect=True)
        self.source = self.source_patch.start()
        self.source.return_value.id = "source_id"
        self.source_plugins = [self.source]

        self.inv = Inventory(self.inventory_dir, self.source_plugins)
        self.inv._load_plugins()

    def tearDown(self):
        super().tearDown()
        self.source_patch.stop()

    def test_load_plugins_creates_expected_list_if_user_data(self):
        self.inv.user_data = {"source_id": {"user": "data"}}

        self.inv._load_plugins()

        self.assertEqual(
            self.source.assert_called_with(source_data={}, user_data={"user": "data"},),
            None,
        )
        self.assertEqual(self.inv.sources, [self.source.return_value])

    def test_load_plugins_creates_expected_list_if_no_user_data(self):
        self.inv.user_data = {}

        self.inv._load_plugins()

        self.assertEqual(
            self.source.assert_called_with(source_data={}, user_data={}), None
        )
        self.assertEqual(self.inv.sources, [self.source.return_value])

    def test_generate_source_data_loads_data_from_plugins(self):
        self.inv._generate_source_data()

        self.assertEqual(
            self.inv.source_data,
            {"source_id": self.source.return_value.generate_source_data.return_value},
        )

    def test_generate_user_data_loads_data_from_plugins(self):
        self.inv._generate_user_data()

        self.assertEqual(
            self.inv.user_data,
            {"source_id": self.source.return_value.generate_user_data.return_value},
        )

    def test_generate_inventory_objects_loads_data_from_plugins(self):
        self.inv._generate_inventory_objects()

        self.assertEqual(
            self.inv.inv,
            {"source_id": self.source.return_value.generate_inventory.return_value},
        )

    @patch("clinv.inventory.Inventory._generate_inventory_objects")
    @patch("clinv.inventory.Inventory._generate_user_data")
    @patch("clinv.inventory.Inventory._generate_source_data")
    @patch("clinv.inventory.Inventory.save")
    @patch("clinv.inventory.Inventory._load_yaml")
    def test_generate_loads_data_from_plugins(
        self, loadMock, saveMock, sourceMock, userMock, inventoryMock,
    ):
        self.inv.generate()

        self.assertTrue(saveMock.called)
        self.assertTrue(sourceMock.called)
        self.assertTrue(userMock.called)
        self.assertTrue(inventoryMock.called)
        self.assertTrue(call(self.user_data_path) in loadMock.mock_calls)

    @pytest.mark.skip("Wait till we don't use mocks ")
    def test_generate_inventory_accepts_resource_type(self):

        pass


import pytest
import redis

from config.settings import REDIS_HOST, REDIS_PORT, REDIS_DB
from modules.helper.redis_client import RedisClient

class TestRedisClient:
    def test_redis_server_is_alive(self):
        r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
        response = r.ping()

        assert response is True

    def test_instantiation_without_arguments(self):
        redis_client = RedisClient()

        assert isinstance(redis_client, RedisClient)

    def test_context_manager_usage(self):
        with RedisClient() as redis_client:
            assert isinstance(redis_client, redis.Redis)

    @pytest.mark.parametrize(
        "key, value", [("key", b"value"), ("key", b"123"), (123, b"value")]
    )
    def test_get_and_set_values(self, key, value):
        with RedisClient() as redis_client:
            redis_client.set(key, value)
            assert redis_client.get(key) == value

    def test_invalid_arguments(self):
        with RedisClient() as redis_client:
            with pytest.raises(redis.exceptions.DataError):
                redis_client.set("key", "value", "extra_argument")


import datetime
import re
from importlib import import_module

from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect
from django.shortcuts import render

from lockdown import settings

def compile_url_exceptions(url_exceptions):
    """Return a list of compiled regex objects, containing the url exceptions.

    All URLs in that list returned won't be considered as locked.
    """
    return [re.compile(p) for p in url_exceptions]

def get_lockdown_form(form_path):
    """Return a form class for a given string pointing to a lockdown form."""
    if not form_path:
        raise ImproperlyConfigured('No LOCKDOWN_FORM specified.')
    form_path_list = form_path.split(".")
    new_module = ".".join(form_path_list[:-1])
    attr = form_path_list[-1]
    try:
        mod = import_module(new_module)
    except (ImportError, ValueError):
        raise ImproperlyConfigured('Module configured in LOCKDOWN_FORM (%s) to'
                                   ' contain the form class couldn\'t be '
                                   'found.' % new_module)
    try:
        form = getattr(mod, attr)
    except AttributeError:
        raise ImproperlyConfigured('The module configured in LOCKDOWN_FORM '
                                   ' (%s) doesn\'t define a "%s" form.'
                                   % (new_module, attr))
    return form

# pylint: disable=too-many-instance-attributes
class LockdownMiddleware(object):
    """Middleware to lock down a whole Django site."""

    # pylint: disable=too-many-arguments
    def __init__(self, get_response=None, form=None, until_date=None,
                 after_date=None, logout_key=None, session_key=None,
                 url_exceptions=None, remote_addr_exceptions=None,
                 extra_context=None, **form_kwargs):
        """Initialize the middleware, by setting the configuration values."""
        if logout_key is None:
            logout_key = settings.LOGOUT_KEY
        if session_key is None:
            session_key = settings.SESSION_KEY
        self.get_response = get_response
        self.form = form
        self.form_kwargs = form_kwargs
        self.until_date = until_date
        self.after_date = after_date
        self.logout_key = logout_key
        self.session_key = session_key
        self.url_exceptions = url_exceptions
        self.remote_addr_exceptions = remote_addr_exceptions
        self.extra_context = extra_context

    def __call__(self, request):
        """Handle calls to the class instance."""
        response = self.process_request(request)

        if not response:
            response = self.get_response(request)

        return response

    # pylint: disable=too-many-locals,too-many-return-statements
    # pylint: disable=too-many-statements,too-many-branches
    def process_request(self, request):
        """Check if each request is allowed to access the current resource."""
        try:
            session = request.session
        except AttributeError:
            raise ImproperlyConfigured('django-lockdown requires the Django '
                                       'sessions framework')

        # Don't lock down if django-lockdown is disabled altogether.
        if settings.ENABLED is False:
            return None

        # Don't lock down if the client REMOTE_ADDR matched and is part of the
        # exception list.
        if self.remote_addr_exceptions:
            remote_addr_exceptions = self.remote_addr_exceptions
        else:
            remote_addr_exceptions = settings.REMOTE_ADDR_EXCEPTIONS

        if remote_addr_exceptions and \
                request.META.get('REMOTE_ADDR') in remote_addr_exceptions:
            return None

        # Don't lock down if the URL matches an exception pattern.
        if self.url_exceptions:
            url_exceptions = compile_url_exceptions(self.url_exceptions)
        else:
            url_exceptions = compile_url_exceptions(settings.URL_EXCEPTIONS)
        for pattern in url_exceptions:
            if pattern.search(request.path):
                return None

        # Don't lock down if outside of the lockdown dates.
        if self.until_date:
            until_date = self.until_date
        else:
            until_date = settings.UNTIL_DATE

        if self.after_date:
            after_date = self.after_date
        else:
            after_date = settings.AFTER_DATE

        if until_date or after_date:
            locked_date = False
            if until_date and datetime.datetime.now() < until_date:
                locked_date = True
            if after_date and datetime.datetime.now() > after_date:
                locked_date = True
            if not locked_date:
                return None

        form_data = request.POST if request.method == 'POST' else None
        if self.form:
            form_class = self.form
        else:
            form_class = get_lockdown_form(settings.FORM)
        form = form_class(data=form_data, **self.form_kwargs)

        authorized = False
        token = session.get(self.session_key)
        if hasattr(form, 'authenticate'):
            if form.authenticate(token):
                authorized = True
        elif token is True:
            authorized = True

        if authorized and self.logout_key and self.logout_key in request.GET:
            if self.session_key in session:
                del session[self.session_key]
            querystring = request.GET.copy()
            del querystring[self.logout_key]
            return self.redirect(request)

        # Don't lock down if the user is already authorized for previewing.
        if authorized:
            return None

        if form.is_valid():
            if hasattr(form, 'generate_token'):
                token = form.generate_token()
            else:
                token = True
            session[self.session_key] = token
            return self.redirect(request)

        page_data = {'until_date': until_date, 'after_date': after_date}
        if not hasattr(form, 'show_form') or form.show_form():
            page_data['form'] = form

        if self.extra_context:
            page_data.update(self.extra_context)

        return render(request, 'lockdown/form.html', page_data)

    def redirect(self, request):
        """Handle redirects properly."""
        url = request.path
        querystring = request.GET.copy()
        if self.logout_key and self.logout_key in request.GET:
            del querystring[self.logout_key]
        if querystring:
            url = '%s?%s' % (url, querystring.urlencode())
        return HttpResponseRedirect(url)


from fastapi import APIRouter

from model import Report, ReportType
from view import ReportAPI

router = APIRouter()

@router.get("/{report_type}/all", response_model=Report)
async def report_all(report_type: ReportType):
    reports = await ReportAPI.get_report_all(report_type)
    return [Report(**report) for report in reports]

@router.get("/{report_type}/{stock_code}", response_model=Report)
async def report_stock_code(report_type: ReportType, stock_code: str):
    report = await ReportAPI.get_report_code(report_type, stock_code)
    return Report(**report)


import pytest
from search_in_a_binary_search_tree import Solution
from helpers.treenode import TreeNode

@pytest.mark.parametrize('arr, val, expected', [
    ([4,2,7,1,3], 2, [2,1,3]),
    ([4,2,7,1,3], 5, []),
])
def test_solution(arr, val, expected):
    root = TreeNode.from_array(arr)
    expected = TreeNode.from_array(expected)
    assert Solution().searchBST(root, val) == expected


from pyrogram import Client, filters

import os

import jikanpy

# Create a new Pyrogram client object and connect to the Telegram API

app = Client(

    "Obanai_iguru_bot",

    api_id=os.environ.get("API_ID"),

    api_hash=os.environ.get("API_HASH"),

    bot_token=os.environ.get("BOT_TOKEN")

)

# Create a new Jikan API client object

jikan = jikanpy.Jikan()

# Handle the /start command

@app.on_message(filters.command("start"))

def start_handler(client, message):

    client.send_message(message.chat.id, "Hello, I am Obanai iguru Bot!")

# Handle the /anime and /manga commands

@app.on_message(filters.command(["anime", "manga"]))

def anime_handler(client, message):

    query = message.text.split(" ", 1)[1]

    anime = jikan.search("anime", query)["results"][0]

    title = anime["title"]

    synopsis = anime["synopsis"]

    url = anime["url"]

    response = f"{title}\n\n{synopsis}\n\n{url}"

    client.send_message(message.chat.id, response)

# Run the bot

app.run()


import matplotlib.pyplot as plt
import numpy as np

plt.style.use("seaborn")

f = open("./data/ethereum.csv", "r")

count = dict()
for line in f:
    count[line] = count.get(line, 0) + 1

raw_data = []
for k in count.values():
    raw_data.append(k)
data = [x / sum(raw_data) for x in raw_data]

top5 = sorted(count.items(), key=lambda item: item[1], reverse=True)[:5]

num_bins = 150

fig, ax = plt.subplots()

# the histogram of the data
n, bins, patches = ax.hist(data, num_bins, density=False)

# label
label = ["Ethermine", "f2pool2", "Hiveon Pool", "Spark Pool", "Nanopool"]
"""
for ((k, v), label_text) in zip(top5, label):
    x = v / sum(raw_data)
    y = -1
    ax.text(
        x,
        y,
        label_text,
        fontsize=5,
        horizontalalignment="center",
        verticalalignment="center",
    )
"""

# add a 'best fit' line
# mu = np.mean(data)
# sigma = np.std(data)
# y = (1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * (1 / sigma * (bins - mu)) ** 2)
# ax.plot(bins, y, "--")

# add a 'best fit' line
ax.set_xlabel("Ratio of hashing power (resource)", fontsize=24)
ax.set_ylabel("The number of participants", fontsize=24)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)

# exp = lambda x: 10 ** (x)
# log = lambda x: np.log(x)
# ax.set_yscale("function", functions=(exp, log))

# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
fig.savefig("./results/ethereum_resource_distribution.png")
# plt.show()


from typing import TYPE_CHECKING, Any, Optional

from ...core.anonymize import obfuscate_address, obfuscate_email
from ..base_plugin import BasePlugin
from . import obfuscate_order

if TYPE_CHECKING:
    from ...account.models import Address, User
    from ...order.models import Order

class AnonymizePlugin(BasePlugin):
    """Anonymize all user data in the checkout, user profile and its orders."""

    PLUGIN_NAME = "Anonymize"
    PLUGIN_ID = "mirumee.anonymize"
    DEFAULT_ACTIVE = True
    PLUGIN_DESCRIPTION = (
        "Anonymize customer's personal data in the checkout, such as shipping "
        "or billing address, email and phone number."
    )
    CONFIGURATION_PER_CHANNEL = False

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.active = True

    def change_user_address(
        self,
        address: "Address",
        address_type: Optional[str],
        user: Optional["User"],
        previous_value: "Address",
    ) -> "Address":
        if address.phone:
            address.phone = ""  # type: ignore
        address = obfuscate_address(address)
        address.save()
        return address

    def order_created(self, order: "Order", previous_value: Any):
        order = obfuscate_order(order)
        order.save()

    def customer_created(self, customer: "User", previous_value: Any) -> Any:
        customer.email = obfuscate_email(customer.email)
        customer.save()


#!/usr/bin/env python3
import utils
from year_2017.day_11 import part_a

class Challenge(utils.BaseChallenge):
    def solve(self, _input, debug=False):
        """
        >>> Challenge().default_solve()
        1524
        """
        directions = part_a.DirectionsRotator()\
            .rotate_directions(_input.strip().split(','))
        max_distance = 0
        zero_point = utils.PointHex(0, 0)
        point = zero_point
        for direction in directions:
            point = point.move(direction)
            max_distance = max(max_distance, point.step_distance(zero_point))
        return max_distance

Challenge.main()
challenge = Challenge()


from fastapi import Form
from pydantic import BaseModel

class ProductForm(BaseModel):
    name: str
    price: float

    class Config:
        orm_mode = True

    @classmethod
    def as_form(
            cls,
            name: str = Form(...),
            price: str = Form(...)
    ):
        return cls(name=name,price=price)


from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from application import app, db

manager = Manager(app)

migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)

@manager.command
def run():
    app.run()

if __name__ == '__main__':
    manager.run()


from dataclasses import dataclass
from typing import List
from environs import Env

@dataclass
class TgBot:
    token: str
    admin_ids: List[int]
    use_redis: bool

@dataclass
class Misc:

    credentials_file: str

@dataclass
class Config:
    tg_bot: TgBot
    misc: Misc

def load_config(path: str = None):
    env = Env()
    env.read_env(path)

    return Config(
        tg_bot=TgBot(
            token = env.str("BOT_TOKEN"),
            admin_ids = list(map(int, env.list("ADMINS"))),
            use_redis=env.bool("USE_REDIS")
        ),
        misc=Misc(
            credentials_file = env.str("CREDENTIALS")
        )
    )


import graphene

from ....page.models import Page
from ...tests.utils import get_graphql_content

def test_delete_pages(staff_api_client, page_list, permission_manage_pages):
    query = """
    mutation pageBulkDelete($ids: [ID]!) {
        pageBulkDelete(ids: $ids) {
            count
        }
    }
    """

    variables = {
        "ids": [graphene.Node.to_global_id("Page", page.id) for page in page_list]
    }
    response = staff_api_client.post_graphql(
        query, variables, permissions=[permission_manage_pages]
    )
    content = get_graphql_content(response)

    assert content["data"]["pageBulkDelete"]["count"] == len(page_list)
    assert not Page.objects.filter(id__in=[page.id for page in page_list]).exists()


# -*- coding: utf-8 -*-
"""
Credit to Akkana. Original code can be found here: https://github.com/akkana/scripts/blob/master/mapping/election2016/bluered-censusshapes.py.
"""
import sys
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

from difflib import SequenceMatcher

# We need CSV to parse the county data:
import csv
# but then the county shapes are encoded inside the CSV as JSON, so
import simplejson as json

def draw_us_map():
    # Set the lower left and upper right limits of the bounding box:
    lllon = -119
    urlon = -64
    lllat = 22.0
    urlat = 50.5
    # and calculate a centerpoint, needed for the projection:
    centerlon = float(lllon + urlon) / 2.0
    centerlat = float(lllat + urlat) / 2.0

    m = Basemap(resolution='i',  # crude, low, intermediate, high, full
                llcrnrlon = lllon, urcrnrlon = urlon,
                lon_0 = centerlon,
                llcrnrlat = lllat, urcrnrlat = urlat,
                lat_0 = centerlat,
                projection='tmerc')

    # map.drawmapboundary(fill_color='ivory')
    # map.fillcontinents(color='coral', lake_color='aqua')

    # m.drawcoastlines()
    # m.drawcountries()

    # drawstates() is built into basemap -- but then there's no
    # way to associate the shapes with state names. Hence the shape files.
    # m.drawstates()

    # draw state boundaries.
    # data from U.S Census Bureau
    # http://www.census.gov/geo/www/cob/st2000.html
    shp_info = m.readshapefile('data/state-shapes/st99_d00', 'states',
                               drawbounds=True, color="lightgrey")

    # County data from Census Bureau
    shp_info = m.readshapefile('data/census-counties-2018/cb_2018_us_county_500k',
                               'counties',
                               drawbounds=True)

    maxstatefp = 0
    states = [None] * 75
    for state in m.states_info:
        statefp = int(state["STATE"])
        # print statefp, state["NAME"]
        if statefp > maxstatefp:
            maxstatefp = statefp
        # Many states have multiple entries in m.states (because of islands?).
        # Only need to add it once.
        if not states[statefp]:
            states[statefp] = state["NAME"]

    # print "max statefp:", maxstatefp
    # for i, s in enumerate(states):
    #     if s:
    #         print i, s

    return m, states

#############

def map_county_data(m, states):
    '''
    '''
    # fp = open("data/alamos.csv")
    # csv.field_size_limit(sys.maxsize)
    fp = open("data/tidy_data.csv")
    reader = csv.DictReader(fp)

    ax = plt.gca()     # get current axes instance

    # Make a dictionary of all counties and their colors.
    county_colors = {}
    for county in reader:
        # What color is this county?

        # Alaska has no results at all:
        if not county["votes16_clintonh"] or not county["votes16_trumpd"]:
            print ("No results for", cname)
            continue

        # print "keys:", county.keys()
        # The dataset holds at least one blank county in Alaska ...
        # which nevertheless has lots of demographic stats,
        # though no election results.
        if not county["name_16"]:
            # Alaska counties are all missing their county name,
            # but we might be able to get it from the longer County field.
            cname = county["County"][:-8]
            if cname.endswith(" Census Area"):
                cname = cname[:-12]
            elif cname.endswith(" City and Borough"):
                cname = cname[:-17]
            elif cname.endswith(" Borough"):
                cname = cname[:-8]
            else:
                print ("Not sure what", cname, "ends with")
                continue
            print ("County name was blank, using", cname)
        else:
            cname = county["name_16"]

        # What color is this county?
        # print "keys:", county.keys()
        # print county
        dem = float(county["votes16_clintonh"])
        rep = float(county["votes16_trumpd"])
        # pop = float(county["Total.Population"])
        pop = float(county["votes"])
        #print (county["name_16"], ":", dem, rep, pop, county["votes"])
        blue = dem/pop
        red = rep/pop

        # In the Deleetdk data, some counties end with "County" inappropriately.
        # Remove it, if so.
        if cname.lower().endswith(" county"):
            print ("Stripping -county", cname)
            cname = cname[:-7]
        county_colors["%s, %s" % (cname, county["State"])] \
            = (red, 0, blue)

    fp.close()

    # Now loop through all counties on the map, coloring them appropriately.
    for i, county in enumerate(m.counties_info):
        countyname = county["NAME"]
        try:
            statename = states[int(county["STATEFP"])]
        except IndexError:
            print (countyname, "has out-of-index statefp of", county["STATEFP"])
            continue

        # The file has no results for Puerto Rico and Alaska.
        if statename == "Puerto Rico" or statename == "Alaska":
            continue

        if not statename:
            print ("No state for", countyname)
            continue

        countystate = "%s, %s" % (countyname, statename)
        #print(countystate)

        try:
            ccolor = county_colors[countystate]
        except KeyError:
            # No exact match. Try for a fuzzy match.
            # Some counties in the Deleetdk are capitalized,
            # lack spaces between words, lack tildes or similar.
            fuzzyname = fuzzy_find(countystate, county_colors.keys())
            if fuzzyname:
                print ("Got a fuzzy match,", countystate, "=", fuzzyname)
                ccolor = county_colors[fuzzyname]
                county_colors[countystate] = ccolor
            else:
                print ("No match for", countystate)
                continue
        countyseg = m.counties[i]

        # Move Hawaii and Alaska:
        # http://stackoverflow.com/questions/39742305/how-to-use-basemap-python-to-plot-us-with-50-states
        # Offset Alaska and Hawaii to the lower-left corner.
        if statename == 'Alaska':
        # Alaska is too big. Scale it down to 35% first, then transate it.
            countyseg = list(map(lambda x: (0.35*x[0] + 1100000, 0.35*x[1]-1300000), countyseg))
        elif statename == 'Hawaii':
            countyseg = list(map(lambda x: (x[0] + 5750000, x[1]-1400000), countyseg))

        poly = Polygon(countyseg, facecolor=ccolor)  # edgecolor="white"
        #print(poly)
        ax.add_patch(poly)

###########

def fuzzy_find(s, slist):
    '''Try to find a fuzzy match for s in slist.
    '''
    best_ratio = -1
    best_match = None

    ls = s.lower()
    for ss in slist:
        r = SequenceMatcher(None, ls, ss.lower()).ratio()
        if r > best_ratio:
            best_ratio = r
            best_match = ss
    if best_ratio > .75:
        return best_match
    return None

def init_map():
    '''Draw a map of the US, upon which we can graph county results.
    '''
    m, states = draw_us_map()

#     for i, shapedict in enumerate(m.states_info):
#         statename = shapedict['NAME']
# #         if statename == "New Mexico":
# #             seg = m.states[i]
# # #             poly = Polygon(seg, facecolor='red', edgecolor='red')
# # #             ax.add_patch(poly)

    return m, states

def show_map():
    # This gets rid of most of the extra horizontal whitespace,
    # and about half the vertical:
    plt.tight_layout(pad=0, w_pad=0, h_pad=0)

    plt.title('The Map')
    plt.show()
    # plt.savefig('test.png')

m, states = init_map()

map_county_data(m, states)

show_map()


"""empty message

Revision ID: 151d6d25be16
Revises: 5e352cc76d16
Create Date: 2020-08-21 16:38:59.387656

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '151d6d25be16'
down_revision = '5e352cc76d16'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('artists', sa.Column('seeking_description', sa.Text(), nullable=True))
    op.add_column('artists', sa.Column('seeking_venue', sa.Boolean(), nullable=False))
    op.alter_column('artists', 'genres',
               existing_type=sa.VARCHAR(length=120),
               nullable=False)
    op.create_unique_constraint(None, 'artists', ['facebook_link'])
    op.create_unique_constraint(None, 'artists', ['website_link'])
    op.create_unique_constraint(None, 'artists', ['phone'])
    op.create_unique_constraint(None, 'artists', ['name'])
    op.drop_column('artists', 'seeking_performance')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('artists', sa.Column('seeking_performance', sa.BOOLEAN(), autoincrement=False, nullable=False))
    op.drop_constraint(None, 'artists', type_='unique')
    op.drop_constraint(None, 'artists', type_='unique')
    op.drop_constraint(None, 'artists', type_='unique')
    op.drop_constraint(None, 'artists', type_='unique')
    op.alter_column('artists', 'genres',
               existing_type=sa.VARCHAR(length=120),
               nullable=True)
    op.drop_column('artists', 'seeking_venue')
    op.drop_column('artists', 'seeking_description')
    # ### end Alembic commands ###


from enum import Enum
from typing import Optional, Dict
import cv2
import numpy as np

from utils.images import imread, imwrite

class Color(Enum):
    """An enum that defines engine colors.

    Contains red, green, blue, cyan, yellow, magenta, white and black.
    """
    red = (0, 0, 255)
    green = (0, 255, 0)
    blue = (255, 0, 0)
    cyan = (255, 255, 0)
    yellow = (0, 255, 255)
    magenta = (255, 0, 255)
    white = (255, 255, 255)
    black = (0, 0, 0)

def color_val(color):
    """Convert various input to color tuples.

    Args:
        color (:obj:`Color`/str/tuple/int/ndarray): Color inputs

    Returns:
        tuple[int]: A tuple of 3 integers indicating BGR channels.
    """
    if isinstance(color, str):
        return Color[color].value
    if isinstance(color, Color):
        return color.value
    if isinstance(color, tuple):
        assert len(color) == 3
        for channel in color:
            assert 0 <= channel <= 255
        return color
    if isinstance(color, int):
        assert 0 <= color <= 255
        return color, color, color
    if isinstance(color, np.ndarray):
        assert color.ndim == 1 and color.size == 3
        assert np.all((color >= 0) & (color <= 255))
        color = color.astype(np.uint8)
        return tuple(color)
    raise TypeError(f'Invalid type for color: {type(color)}')

def imshow(img, win_name='', wait_time=0):
    """
    Show an image.

    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
    """
    cv2.imshow(win_name, imread(img))
    if wait_time == 0:  # prevent from hanging if windows was closed
        while True:
            ret = cv2.waitKey(1)

            closed = cv2.getWindowProperty(win_name, cv2.WND_PROP_VISIBLE) < 1
            # if user closed window or if some key pressed
            if closed or ret != -1:
                break
    else:
        ret = cv2.waitKey(wait_time)

def show_result(img: str,
                result: Dict[int, float],
                text_color: str = 'green',
                font_scale: float = 0.5,
                row_width: int = 20,
                show: bool = False,
                win_name: str = '',
                wait_time: int = 0,
                out_file: Optional[str] = None) -> None:
    """Mark the results on the picture.

    Args:
        img (str): The image to be displayed.
        result (dict): The classification results to draw over `img`.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        font_scale (float): Font scales of texts.
        row_width (int): width between each row of results on the image.
        show (bool): Whether to show the image. Default: False.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param. Default: 0.
        out_file (str or None): The filename to write the image. Default: None.

    Returns:
        None
    """
    img = imread(img, mode="RGB")
    img = img.copy()

    # Write results on left-top of the image.
    x, y = 0, row_width
    text_color = color_val(text_color)
    for k, v in result.items():
        if isinstance(v, float):
            v = f'{v:.2f}'
        label_text = f'{k}: {v}'
        cv2.putText(img, label_text, (x, y), cv2.FONT_HERSHEY_COMPLEX,
                    font_scale, text_color)
        y += row_width

    # If out_file specified, do not show image in window.
    if out_file:
        show = False
        imwrite(img, out_file)

    if show:
        imshow(img, win_name, wait_time)


from pyspark.sql import SparkSession
from pyspark.conf import SparkConf

conf = SparkConf().setAppName("Counter").setMaster("spark://spark:7077")

spark = SparkSession.builder.config(conf=conf).getOrCreate()

sc = spark.sparkContext

text_file = sc.textFile("/tmp/data/lorem.txt")

counts = text_file.flatMap(lambda linea: linea.split(" ")).countByValue()

for word, count in counts.items():
    print("------------------")
    print("{} : {}".format(word, count))

sc.stop()


import asyncio
import aiohttp
import time
import hashlib
import hmac
import urllib.parse
from typing import List, Tuple
from aiohttp.client_exceptions import ClientError
from exchanges.base_api import BaseAPI
from logger import get_logger
logger = get_logger(__name__)

class PoloniexAPI(BaseAPI):
    def __init__(self, api_key: str, api_secret: str):
        super().__init__()
        self._base_url = 'https://poloniex.com/'
        self._request_interval = 0.17  # rate limit is 6 requests/sec
        self._api_key = api_key
        self._api_secret = api_secret
        loop = asyncio.get_event_loop()
        self._session = aiohttp.ClientSession(loop=loop, headers={})

    async def all_tickers(self, urgency: int = 0):
        return await self._safe_call(urgency, self._request_public, 'returnTicker')

    async def balances(self, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'returnBalances')

    async def buy(self, symbol: str, price: str, amount: str, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'buy', {
            'currencyPair': symbol,
            'rate': price,
            'amount': amount
        })

    async def sell(self, symbol: str, price: str, amount: str, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'sell', {
            'currencyPair': symbol,
            'rate': price,
            'amount': amount
        })

    async def order_status(self, order_id: str, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'returnOrderStatus', {'orderNumber': order_id})

    async def order_trades(self, order_id: str, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'returnOrderTrades', {'orderNumber': order_id})

    async def cancel_order(self, order_id: str, urgency: int = 0):
        return await self._safe_call(urgency, self._request_private, 'cancelOrder', {'orderNumber': order_id})

    async def measure_ping(self) -> Tuple[int, int, int]:
        pings = [
            await self._throttle(self._measure_ping_once)
            for i in range(10)
        ]
        avg = int(sum(pings) / len(pings))
        return min(pings), max(pings), avg

    async def stop(self):
        await super().stop()
        await self._session.close()

    async def _measure_ping_once(self) -> int:
        t = time.time()
        await self._request_private('returnBalances')
        return int((time.time() - t) * 1000)

    async def _request_public(self, command: str):
        data = {'command': command}
        url = f'public?{urllib.parse.urlencode(data)}'
        return await self._request('get', url)

    async def _request_private(self, command: str, data: dict = None) -> dict:
        if data is None:
            data = {}
        # preparing data
        data = {
            'command': command,
            'nonce': int(time.time() * 1000),
            **data
        }
        query = urllib.parse.urlencode(data)
        sig = hmac.new(self._api_secret.encode('utf8'), query.encode('utf8'), hashlib.sha512).hexdigest()
        kwargs = {
            'headers': {
                'Key': self._api_key,
                'Sign': sig
            },
            'data': data,
        }
        return await self._request('post', 'tradingApi', **kwargs)

    async def _request(self, verb: str, endpoint: str, **kwargs) -> dict:
        url = self._base_url + endpoint
        try:
            async with getattr(self._session, verb)(url, **kwargs) as response:
                result = await self._handle_response(response)
        except ClientError as e:
            raise PoloniexAPI.Error(str(e))
        return result

    async def _handle_response(self, response: aiohttp.ClientResponse) -> dict:
        try:
            r = await response.json()
        except ValueError:
            raise PoloniexAPI.Error(f'Invalid Response: {await response.text()}')
        # check if poloniex returned an error
        try:
            if 'result' in r:
                if 'error' in r['result']:
                    raise PoloniexAPI.Error(r['result']['error'])
                else:
                    return r['result']
            else:
                if 'error' in r:
                    raise PoloniexAPI.Error(r['error'])
                else:
                    return r
        except PoloniexAPI.Error as e:
            if 'Order not found' in e.message or 'is either completed or does not exist' in e.message:
                raise BaseAPI.OrderNotFound
            if 'Not enough' in e.message:
                raise PoloniexAPI.ErrorNoRetry(e.message)
            if 'Total must be at least' in e.message:
                raise PoloniexAPI.ErrorNoRetry(e.message)
            raise

async def main():
    from config import config

    api = PoloniexAPI(
        config.get('Exchange', 'APIKey'),
        config.get('Exchange', 'APISecret')
    )
    logger.info(f'Tickers: {await api.all_tickers()}')
    logger.info(f'Balances: {await api.balances()}')
    # logger.info(f'Placing order: {await api.buy("BTC_ETH", "0.01000000", "0.1")}')
    # logger.info(f'Cancelling order: {await api.cancel_order("661906638940")}')
    # logger.info(f'Nonexistent order status: {await api.order_status("123456")}')
    # logger.info(f'Nonexistent order trades: {await api.order_trades("123456")}')
    await api.stop()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


import json
import os
import shutil
import tempfile

from scilpy.io.fetcher import get_testing_files_dict, fetch_data, get_home

# If they already exist, this only takes 5 seconds (check md5sum)
fetch_data(get_testing_files_dict(), keys=['tracking.zip'])
tmp_dir = tempfile.TemporaryDirectory()

def test_help_option(script_runner):
    ret = script_runner.run('scil_score_bundles.py',
                            '--help')
    assert ret.success

def test_score_bundles(script_runner):
    os.chdir(os.path.expanduser(tmp_dir.name))
    in_tractogram = os.path.join(get_home(), 'tracking', 'pft.trk')

    # Pretend we have our segmented bundles on disk
    shutil.copyfile(in_tractogram, './NC.trk')
    os.mkdir('./segmented_VB')
    shutil.copyfile(in_tractogram, './segmented_VB/bundle1_VS.trk')
    shutil.copyfile(in_tractogram, './segmented_VB/bundle2_VS.trk')
    os.mkdir('./segmented_IB')
    shutil.copyfile(in_tractogram, './segmented_IB/roi1_roi2_VS.trk')

    json_contents = {
        "bundle1": {
            "gt_mask": os.path.join(get_home(), 'tracking',
                                    'seeding_mask.nii.gz'),
        },
        "bundle2": {
            "gt_mask": os.path.join(get_home(), 'tracking',
                                    'seeding_mask.nii.gz'),
        }
    }
    with open(os.path.join("config_file.json"), "w") as f:
        json.dump(json_contents, f)

    ret = script_runner.run('scil_score_bundles.py',
                            "config_file.json", "./", '--no_bbox_check')

    assert ret.success


import logging,time
from selenium.webdriver.common.by import By
from page.base import PageBase
from common.tools import switch_execute

class PageHome(PageBase):
	def __init__(self,driver):
		# 首页 页面
		super().__init__(driver)
		self.btnReload={
			'Android':(By.ID,'com.cmbi.zytx:id/reload_btn'),
			'iOS':(By.IOS_PREDICATE,"name=='reload'")
		}
		self.eleRegister={
			'Android':(By.XPATH,"//*[text()='港美开户']"),
			'iOS':(By.IOS_PREDICATE,"name=='港美开户'")
		}
		self.eleCurrExchange={
			'Android':(By.XPATH,"//*[text()='货币兑换']"),
			'iOS':(By.IOS_PREDICATE,"name=='货币兑换'")
		}
		self.eleRegisterNow=(By.ID,"getAccount")

		#基金板块
		self.btnFundMore={
			'Android':(By.CLASS_NAME,"global-fund-more"),
			'iOS':(By.IOS_PREDICATE,"name=='查看更多'")
		}
		self.eleFund1={
			'Android':(By.CLASS_NAME,"global-fund-name"),
			'iOS':(By.IOS_PREDICATE,"name=='近一年收益率'")
		}
		self.eleFund1Name=(By.CLASS_NAME,"global-fund-name")
		#深度推荐板块
		self.btnArticleOne={
			'Android':(By.CLASS_NAME,"card"),
			'iOS':(By.XPATH,"//*[@name='关注']/../following-sibling::*[1]")
		}
		self.eleArticleTitle=(By.CLASS_NAME,"detailItem-title")

		self.passwordInput={
			'Android':(By.ID,'com.cmbi.zytx:id/input_trade_password'),
			'iOS':(By.IOS_PREDICATE,"type=='XCUIElementTypeSecureTextField'")
		}
		self.confirm={
			'Android':(By.ID,'com.cmbi.zytx:id/btn_submit'),
			'iOS':(By.IOS_PREDICATE,"name=='确定'")
		}

	@switch_execute
	def inputPwordIFneeded(self):
		try:
			self.findElement(self.passwordInput[self.PFN],timeout=3,screen=0).send_keys('aaaa1111')
			self.findElement(self.confirm[self.PFN],timeout=3,screen=0).click()
		except:
			pass

	# @switch_execute
	def clickReload(self):
		logging.info('点击 reload')
		self.findElement(self.btnReload[self.PFN]).click()
		time.sleep(5)

	def clickRegister(self):
		logging.info('点击 港美开户')
		if self.PFN=='Android':
			self.myTap(self.findElement(self.eleRegister[self.PFN]))
		else:
			self.clickRegister_IOS()

	@switch_execute
	def clickRegister_IOS(self):
		self.findElement(self.eleRegister[self.PFN]).click()

	@switch_execute
	def clickCurrExchange_IOS(self):
		self.findElement(self.eleCurrExchange[self.PFN]).click()

	def clickCurrExchange(self):
		logging.info('点击 货币兑换')
		if self.PFN=='Android':
			self.myTap(self.findElement(self.eleCurrExchange[self.PFN]))
		else:
			self.clickCurrExchange_IOS()
		time.sleep(3)
		self.inputPwordIFneeded()

	def clickRegisterNow(self):
		logging.info('点击 立即开户')
		self.myTap(self.findElement(self.eleRegisterNow))

	#基金板块
	def clickFundMore(self):
		logging.info('点击基金板块 更多 按钮')
		self.driver.execute_script(f'document.getElementById("hotTitle").scrollIntoView();')
		if self.PFN=='Android':
			self.myTap(self.findElement(self.btnFundMore[self.PFN]))
		else:
			self.clickFundMore_IOS()

	@switch_execute
	def clickFundMore_IOS(self):
		self.findElement(self.btnFundMore[self.PFN]).click()

	def textFund1(self):
		self.driver.execute_script('document.querySelector(".hot-fund-img").scrollIntoView();')
		txt=self.findElement(self.eleFund1Name,until='located').get_attribute("innerText")
		return txt

	def clickFund1(self):
		logging.info('点击基金板块中的第1个基金')
		if self.PFN=='Android':
			self.myTap(self.findElement(self.eleFund1[self.PFN]))
		else:
			self.clickFund1_IOS()

	@switch_execute
	def clickFund1_IOS(self):
		self.findElement(self.eleFund1[self.PFN]).click()

	#深度推荐板块
	def textArticleOne(self):
		time.sleep(3)
		self.driver.execute_script('document.getElementsByClassName("card-title")[0].scrollIntoView();')
		time.sleep(1)
		text=self.driver.execute_script('return document.getElementsByClassName("card-title")[0].textContent;')
		return text

	def clickArticleOne(self):
		logging.info('点击深度推荐板块中的第1篇文章')
		time.sleep(3)
		self.driver.execute_script('document.getElementsByClassName("card-title")[0].scrollIntoView();')
		time.sleep(1)
		if self.PFN=='Android':
			self.myTap(self.findElement(self.btnArticleOne[self.PFN]))
		else:
			self.clickArticleOne_IOS()

	@switch_execute
	def clickArticleOne_IOS(self):
		self.findElement(self.btnArticleOne[self.PFN]).click()

	def textArticleTitle(self):
		for i in range(10):
			txt=self.findElement(self.eleArticleTitle).text
			if txt:return txt
			else:time.sleep(1)


#!/usr/bin/env python3
import random

import utils

def boxEmUp(args):
    filedata = ""
    if args.infile:
        fileopen = open(args.infile, 'r')
        filedata = fileopen.read()
        fileopen.close()

    if args.encoder == "base64":
        s = utils.standardEncoding(filedata)
        filedata = s.baseE64()
    if args.encoder == "hex":
        s = utils.standardEncoding(filedata)
        filedata = s.hexE()

    if args.obfu == "ranenc":
        re = utils.encoderGenrator(filedata)
        filedata = re.codeGenrator()
    if args.obfu == "strcla":
        sc = utils.stringObjuscator(filedata)
        filedata = sc.obfuscat()

    if args.packer:
        t = utils.packer(filedata)
        filedata = t.returnString()

    if args.encryption:
        ec = utils.basicEncryption(filedata)
        filedata = ec.encrypt()

    if args.random:
        a = [1,2,3,4,5,6]
        random.shuffle(a)

        for x in a:
            if x == 1:
                s = utils.standardEncoding(filedata)
                filedata = s.baseE64()
            elif x == 2:
                s = utils.standardEncoding(filedata)
                filedata = s.hexE()
            elif x == 3:
                re = utils.encoderGenrator(filedata)
                filedata = re.codeGenrator()
            elif x == 4:
                sc = utils.stringObjuscator(filedata)
                filedata = sc.obfuscat()
            elif x == 5:
                t = utils.packer(filedata)
                filedata = t.returnString()
            elif x == 6:
                ec = utils.basicEncryption(filedata)
                filedata = ec.encrypt()
    if args.outfile:
        with open(args.outfile, 'w') as file:
            file.write(filedata)
    else:
        print(filedata)

def main():
    print("\n\tJack In The Box\nby Michael Provenzano")
    print("        \n\n\n                 What is anti-malware.\n\n\n !!! PYTHON file ONLY !!!")
    arg = utils.age()
    args = arg.get_args()

    if args.encoders:
        arg.enco()
        exit()

    if args.obfus:
        arg.obfuscators()
        exit()

    if args.infile:
        boxEmUp(args)

if __name__ == "__main__":
    main()


import os, sys
import random

from collections import OrderedDict
_getwordsfile_dloadloc = 'http://gist.githubusercontent.com/wchargin/8927565/raw/d9783627c731268fb2935a731a618aa8e95cf465/words'
_init_filelocs = [
    '/usr/share/dict/words'
]
_init_defaults = OrderedDict(dodownload=False, downloadloc=_getwordsfile_dloadloc, downloadout=None, filelocs=_init_filelocs)
def init(dodownload=2, downloadloc=_getwordsfile_dloadloc, downloadout=None, filelocs=_init_filelocs):
    global wordsfile, openwordsfile, openedwordsfile, wordslist
    global getwordsfile, getwordslist

    _defenc = sys.getdefaultencoding()
    _getwordsfile_dloadloc = 'http://gist.githubusercontent.com/wchargin/8927565/raw/d9783627c731268fb2935a731a618aa8e95cf465/words'
    def getwordsfile(dodownload=False, downloadloc=_getwordsfile_dloadloc, downloadout=None):
        dictfile = ''

        for loc in filelocs:
            if os.path.isfile(loc):
                dictfile = loc
                break
        else:
            if dodownload == 1:
                if downloadout is None: downloadout = locs[0]
                import urllib.request as request, shutil
                fin = request.urlopen(downloadloc)
                outdir = '/'.join(downloadout.split('/')[:-1])
                if not os.path.exists(outdir): os.makedirs(outdir)
                fout = open(downloadout, 'wb')
                shutil.copyfileobj(fin, fout)
                fout.close()
                dictfile = downloadout
            elif dodownload == 0:
                raise FileNotFoundError
        return dictfile
    wordsfile = getwordsfile(dodownload)
    openwordsfile = lambda wordsfile=wordsfile: open(wordsfile)

    def getwordslist(wordsfile_=wordsfile):
        if wordsfile_ == wordsfile and openedwordsfile:
            openedwordsfile_ = openedwordsfile
        else:
            openedwordsfile_ = openwordsfile(wordsfile_)
        res = []
        for line in openedwordsfile_:
            res.append(line.strip())
        return res
    try:
        openedwordsfile = openwordsfile()
        wordslist = getwordslist()
    except OSError:
        openedwordsfile = None
        wordslist = []
init.__defaults__ = tuple(_init_defaults.values())
import inspect
def preinit(*args, **kwargs):
    argnames = inspect.getfullargspec(init)[0]
    for (i, val) in enumerate(args):
        _init_defaults[argnames[i]] = val
    _init_defaults.update(kwargs)
    init.__defaults__ = tuple(_init_defaults.values())

# lowerwordslist = lambda: (x.lower() for x in wordslist)

def scramble(word, random_=None):
    word = list(word)
    random.shuffle(word, random_)
    return ''.join(word)

def unscramble(word, lowercaps=True):
    if lowercaps: word = word.lower()
    word = list(word)
    worddef = word[:]
    if lowercaps: mywordslist = [x.lower() for x in wordslist]
    else: mywordslist = wordslist
    visited = set()
    for testword in mywordslist:
        if testword in visited: continue
        for (i, char) in enumerate(testword):
            if char in word:
                word.pop(word.index(char))
            else: break
            if i == len(testword) - 1 and word: break
        else:
            yield testword
            visited.add(testword)
        word = worddef[:]

def makewingui():
    form = Form()
    form.SuspendLayout()
    stxt = TextBox()
    utxt = TextBox()
    sbtn = Button()
    ubtn = Button()

    #
    # stxt
    #
    stxt.Location = Point(12, 8)
    stxt.Name = 'stxt'
    stxt.Size = Size(319, 20)
    stxt.TabIndex = 0
    #
    # utxt
    #
    utxt.Location = Point(12, 34)
    utxt.Name = 'utxt'
    utxt.Size = Size(319, 20)
    utxt.TabIndex = 2
    #
    # sbtn
    #
    sbtn.Anchor = AnchorStyles.Top | AnchorStyles.Right
    sbtn.Location = Point(337, 6)
    sbtn.Name = 'sbtn'
    sbtn.Size = Size(75, 23)
    sbtn.TabIndex = 1
    sbtn.Text = 'Scramble'
    sbtn.UseVisualStyleBackColor = True
    def sres(sender, e):
        stxt.Text = scramble(stxt.Text)
    sbtn.Click += EventHandler(sres)
    # ubtn.MouseEnter += EventHandler(sres)
    #
    # ubtn
    #
    ubtn.Anchor = AnchorStyles.Top | AnchorStyles.Right
    ubtn.Location = Point(337, 32)
    ubtn.Name = 'ubtn'
    ubtn.Size = Size(75, 23)
    ubtn.TabIndex = 3
    ubtn.Text = 'Unscramble'
    ubtn.UseVisualStyleBackColor = True
    def ures(sender, e):
        utxt.Text = '/'.join(unscramble(utxt.Text))
    ubtn.Click += EventHandler(ures)
    # ubtn.MouseEnter += EventHandler(ures)
    #
    # form
    #
    form.AutoScaleDimensions = SizeF(6.0, 13.0)
    form.AutoScaleMode = AutoScaleMode.Font
    form.ClientSize = Size(424, 61)
    form.Controls.Add(stxt)
    form.Controls.Add(utxt)
    form.Controls.Add(sbtn)
    form.Controls.Add(ubtn)
    form.FormBorderStyle = FormBorderStyle.Fixed3D
    form.Name = 'form'
    form.Text = os.path.basename(__file__)
    form.ResumeLayout(False)
    form.PerformLayout()

    Application.EnableVisualStyles()
    Application.Run(form)

if __name__ == '__main__':
    # wordsfile = getwordsfile(True)
    # openedwordsfile = openwordsfile()
    # wordslist = getwordslist()
    preinit(True)
    init()
    get = lambda name: sys.argv[sys.argv.index(name) + 1]
    if '--gui' in sys.argv:
        import clr
        clr.AddReference('System')
        from System import *
        clr.AddReference('System.Drawing')
        from System.Drawing import *
        clr.AddReference('System.Windows.Forms')
        from System.Windows.Forms import *
        makewingui()
        sys.exit()
    done = False
    if   '-s' in sys.argv:
        done = True
        print('Scrambled:')
        print('   ', scramble(get('-s')))
    elif '--scramble' in sys.argv:
        done = True
        print('Scrambled:')
        print('   ', scramble(get('--scramble')))
    if   '-u' in sys.argv:
        done = True
        print('Unscrambled:')
        for result in unscramble(get('-u'), not ('--caps' in sys.argv or '--capitalization' in sys.argv)):
            print('   ', result)
    elif '--unscramble' in sys.argv:
        done = True
        print('Unscrambled:')
        for result in unscramble(get('--unscramble'), not ('--caps' in sys.argv or '--capitalization' in sys.argv)):
            print('   ', result)
    if not done or '-h' in sys.argv or '--help' in sys.argv:
        print('Usage:', sys.executable, __file__, '[-h|--help]', '[--gui]', '[--caps|--capitalization]',
        '[-s|--scramble <word>]', '[-u|--unscramble <scrambled_word>]')
    # import pprint
    # pprint.pprint(list(unscramble(scramble('aloha'))))


from typing import Protocol, List, Iterable, Type
from pydantic import BaseModel
from dataclasses import dataclass, field
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from sqlalchemy import create_engine
from tqdm import tqdm
from abcs import StateCampaignFinanceConfigs
from logger import Logger
from sqlalchemy.dialects.postgresql import insert

@dataclass
class PostgresLoader:
    _base: Type[DeclarativeBase]
    table: Type[DeclarativeBase] = field(init=False)
    models: Iterable[DeclarativeBase] = field(init=False)
    __logger: Logger = field(init=False)

    def __post_init__(self):
        self.__logger = Logger(PostgresLoader.__class__.__name__)

    def build(self, engine: create_engine):
        self._base.metadata.create_all(engine)
        print("Created Postgres tables")

    def create(self, values: List[dict], table: Type[DeclarativeBase]):
        self.table = table

        _models = []
        for v in tqdm(values, desc=f"Creating {table.__tablename__} models"):
            _v = dict(v)
            _models.append(table(**_v))
        self.models = iter(_models)
        return self.table, self.models

    def load(self, session: sessionmaker):

        def upload_records(records):
            _errors = []
            with session() as upload:
                try:
                    upload.add_all(records)
                    upload.commit()
                except Exception as e:
                    upload.rollback()
                    self.__logger.error(f"Error: {e}")
                    _errors.append(e)
            return _errors
        errors = []
        _queue = []
        for model in tqdm(self.models, desc=f"Loading to {self.table.__tablename__}"):
            _queue.append(model)
            if len(_queue) == 16000:
                _errors = upload_records(_queue)
                _queue = []
                errors.append(_errors)
        _errors = upload_records(_queue)
        errors.append(_errors)
        return errors

    def _get_model(self, _validator: BaseModel, _config: StateCampaignFinanceConfigs):
        if _validator.__name__ == "TECExpense":
            _model = _config.EXPENSE_MODEL
        elif _validator.__name__ == "TECContribution":
            _model = _config.CONTRIBUTION_MODEL
        elif _validator.__name__ == "TECFiler":
            _model = _config.FILERS_MODEL
        else:
            raise ValueError(f"Invalid type: {_validator.__name__}")
        return _model

    # def insert_to_db(self, _list: list, _validator: BaseModel, _config: StateCampaignFinanceConfigs):
    #     _model = self._get_model(_validator, _config)
    #     with _config.DB_SESSION() as upload:
    #         _list = [dict(x) for x in _list]
    #         _add = insert(_model).values(_list).on_conflict_do_nothing()
    #         upload.execute(_add)
    #         upload.commit()

    def add_to_db(self, _list: list,  _validator: BaseModel, _config: StateCampaignFinanceConfigs):
        self.__logger.debug(f"Called _load_to_db() for {_validator}...")
        _model = self._get_model(_validator, _config)
        _loader = PostgresLoader(_config.DB_BASE)
        _loader.build(engine=_config.DB_ENGINE)
        _loader.create(values=_list, table=_model)
        _loader.load(session=_config.DB_SESSION)
        self.__logger.debug(f"Loaded {len(_list):,} records to database...")


'''
Created on Sep 13, 2016

@author: Juan Manuel Acevedo Valle
'''

if __name__ == '__main__':
    import os,sys
    sys.path.append(os.getcwd())

    import numpy as np
    from matplotlib.pyplot  import draw, show

    from exploration.models.GeneralModels.Trash.ILGMM_GREC import ILGMM
    from exploration.data.PlotTools import initializeFigure

    # Number of samples per component
    n_samples = 500

    # Generate random sample, two components
    np.random.seed(0)
    C = np.array([[0., -0.1], [1.7, .4]])
    X = np.r_[np.dot(np.random.randn(n_samples, 2), C),
              .7 * np.random.randn(n_samples, 2) + np.array([-6, 3])]

    model = ILGMM(min_components=3)
    model.train(X)

    #Model computed with three Gaussians
    fig1, ax1 = initializeFigure()
    ax1 = model.plot_gmm_projection(0,1,axes=ax1)

    model.mergeGaussians(0,2)

    #Model merging similar Gaussians
    fig2, ax2 = initializeFigure()
    ax2 = model.plot_gmm_projection(0, 1, axes=ax2)

    #Model computed with two Gaussians
    model = ILGMM(min_components=2)
    model.train(X)
    fig3, ax3 = initializeFigure()
    ax3 = model.plot_gmm_projection(0, 1, axes=ax3)

    ax1.relim()
    ax1.autoscale_view()
    ax2.relim()
    ax2.autoscale_view()
    ax3.relim()
    ax3.autoscale_view()

    draw()
    show()


import csv
import pathlib

def main():
    rajs = set()
    for f in pathlib.Path('./results').glob('*_verification.csv'):
        rajs.add((f.name).split('_')[0])

    for raj in rajs:
        csv_data_list = []
        with open(f'./results/{raj}_verification.csv', 'r', newline='') as csvfile:
            csv_data = csv.reader(csvfile, delimiter=';', quotechar='"')
            next(csv_data)
            for row in csv_data:
                csv_data_list.append(row)

        d = {}
        for row in csv_data_list:
            month_value = row[2][:7]
            err_code_value = row[36]
            key = f'{month_value}_{err_code_value}'
            if key not in d:
                d[key] = [0, 0]
                d[month_value] = [0, 0]

        for row in csv_data_list:
            key = f'{row[2][:7]}_{row[36]}'
            d[key][0] += 1
            d[row[2][:7]][0] += 1
            if len(row) >= 61 and row[61] == 'P':
                d[key][1] += 1
                d[row[2][:7]][1] += 1

        arr = [['Місяць', 'Код помилки', 'Кількість рекомендацій надано',
                'Кількість рекомендацій опрацьовано', '%']]
        for k, v in sorted(d.items()):
            if len(k) == 7:
                arr.append([k, 'Загальна', v[0], v[1],
                           f'{round(v[1]/v[0]*100)}%'])
            else:
                arr.append([k.split('_')[0], k.split('_')[1],
                           v[0], v[1], f'{round(v[1]/v[0]*100)}%'])

        with open(f'./results/{raj}_report.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile, delimiter=';', quotechar='"')
            for row in arr:
                writer.writerow(row)

if __name__ == '__main__':
    main()