#!/usr/bin/env python
#
# Helper functions for data manipulation
#
################################################
from PIL import Image
import os, glob, re
import numpy as np, pandas as pd

import sys

def ResizeWithProportions(im, desired_size):
	'''
	Take and image and resize it to a square of the desired size.
	0) If any dimension of the image is larger than the desired size, shrink until the image can fully fit in the desired size
	1) Add black paddings to create a square
	'''

	old_size    = im.size
	largest_dim = max(old_size)
	smallest_dim = min(old_size)

	# If the image dimensions are very different, reducing the larger one to `desired_size` can make the other
	# dimension too small. We impose that it be at least 4 pixels.
	if desired_size*smallest_dim/largest_dim<4:
		print('Image size: ({},{})'.format(largest_dim,smallest_dim ))
		print('Desired size: ({},{})'.format(desired_size,desired_size))
		raise ValueError('Images are too extreme rectangles to be reduced to this size. Try increasing the desired image size.')

	rescaled    = 0 # This flag tells us whether there was a rescaling of the image (besides the padding). We can use it as feature for training.

	# 0) If any dimension of the image is larger than the desired size, shrink until the image can fully fit in the desired size
	if max(im.size)>desired_size:

		ratio = float(desired_size)/max(old_size)
		new_size = tuple([int(x*ratio) for x in old_size])
		# print('new_size:',new_size)
		sys.stdout.flush()
		im = im.resize(new_size, Image.LANCZOS)
		rescaled = 1

    # 1) Add black paddings to create a square
	new_im = Image.new("RGB", (desired_size, desired_size), color=0)
	new_im.paste(im, (	(desired_size-im.size[0])//2,
						(desired_size-im.size[1])//2))

	return new_im, rescaled

def ReduceClasses(datapaths, class_select):
	print('datapaths:',datapaths)
	# allClasses = [ name for name in os.listdir(datapaths) if os.path.isdir(os.path.join(datapaths, name)) ]
	allClasses = list(set([ name for idata in range(len(datapaths)) for name in os.listdir(datapaths[idata]) if os.path.isdir(os.path.join(datapaths[idata], name))]))
	print('classes from datapaths:', allClasses)
	if class_select is None:
		class_select = allClasses
	else:
		if not set(class_select).issubset(allClasses):
			print('Some of the classes input by the user are not present in the dataset.')
			print('class_select:',class_select)
			print('all  classes:',allClasses)
			raise ValueError
	return class_select

def LoadMixed(datapaths, L, class_select=None, alsoImages=True, training_data=True):
	'''
	Uses the data in datapath to create a DataFrame with images and features.
	For each class, we read a tsv file with the features. This file also contains the name of the corresponding image, which we fetch and resize.
	For each line in the tsv file, we then have all the features in the tsv, plus class name, image (as numpy array), and a binary variable stating whether the image was resized or not.
	Assumes a well-defined directory structure.

	Arguments:
	datapaths 	  - list with the paths where the data is stored. Inside each datapath, we expect to find directories with the names of the classes
	L 			  - images are rescaled to a square of size LxL (maintaining proportions)
	class_select  - a list of the classes to load. If None (default), loads all classes
	alsoImages    - flag that tells whether to only load features, or features+images
	training_data - flag for adding a subdirectory called training_data
	Output:
	df 		 	  - a dataframe with classname, npimage, rescaled, and all the columns contained in features.tsv
	'''
	training_data_dir = '/training_data/' if training_data==True else '/'

	df = pd.DataFrame()
	class_select=ReduceClasses(datapaths, class_select)	# Decide whether to use all available classes

	# Loop for data loading
	for c in class_select: # Loop over the classes

		# Read from tsv file, and create column with full path to the image
		dfFeat = pd.DataFrame()
		for idp in range(len(datapaths)):
			try: # It could happen that a class is contained in one datapath but not in the others
				dftemp = pd.read_csv(datapaths[idp]+c+'/features.tsv', sep = '\t')
				dftemp['filename'] = [datapaths[idp]+c+training_data_dir+os.path.basename(dftemp.url[ii]) for ii in range(len(dftemp))]
				dfFeat = pd.concat([dfFeat, dftemp], axis=0, sort=True)
			except:
				pass

		print('class: {} ({})'.format(c, len(dfFeat)))

		# Each line in features.tsv should be associated with classname (and image, if the options say it's true)
		for index, row in dfFeat.iterrows():

			if alsoImages:
				image=Image.open(row.filename)
				image,rescaled = ResizeWithProportions(image, L) # Set image's largest dimension to target size, and fill the rest with black pixels
				npimage = np.array(image.copy() , dtype=np.float32)			 # Convert to numpy

				dftemp=pd.DataFrame([[c,npimage,rescaled]+row.to_list()], columns=['classname','npimage','rescaled']+dfFeat.columns.to_list())
				image.close()
			else: #alsoImages is False here
				dftemp=pd.DataFrame([[c]+row.to_list()] ,columns=['classname']+dfFeat.columns.to_list())

			df=pd.concat([df,dftemp], axis=0, sort=True)

	# If images were loaded, scale the raw pixel intensities to the range [0, 1]
	if alsoImages:
		df.npimage = df.npimage / 255.0

	return df.reset_index(drop=True) # The data was loaded without an index, that we add with reset_index()

def LoadImage(filename, L=None, show=False):
	''' Loads one image, and rescales it to size L.
	The pixel values are between 0 and 255, instead of between 0 and 1, so they should be normalized outside of the function
	'''

	image = Image.open(filename)
	# Set image's largest dimension to target size, and fill the rest with black pixels
	if L is None:
		rescaled=0
	else:
		image,rescaled = ResizeWithProportions(image, L) # width and height are assumed to be the same (assertion at the beginning)
	npimage = np.array(image.copy(), dtype=np.float32)

	if show:
		image.show()
	image.close()
	return npimage, rescaled

def LoadImages(datapaths, L, class_select=None, training_data=True):
	'''
	Uses the data in datapath to create a DataFrame with images only.
	This cannot be a particular case of the mixed loading, because the mixed depends on the files written in the features.tsv file, whereas here we fetch the images directly.

	Arguments:
	datapath 	 - the path where the data is stored. Inside datapath, we expect to find directories with the names of the classes
	L 			 - images are rescaled to a square of size LxL (maintaining proportions)
	class_select - a list of the classes to load. If None (default), loads all classes
	training_data- a boolean variable to decide the structure of the directories
	Output:
	df 			 - a dataframe with classname, npimage, rescaled.
	'''

	df = pd.DataFrame()
	class_select=ReduceClasses(datapaths, class_select)	# Decide whether to use all available classes

	# The following condition is because the taxonomists used different directory structures
	names='/training_data/*.jp*g' if training_data==True else '/*.jp*g'

	for c in class_select:

		# Get names of images belonging to this class, from all the data paths
		classImages = []
		for idp in range(len(datapaths)):
			classImages.extend( glob.glob(datapaths[idp]+'/'+c+'/'+names) )

		# Create an empty dataframe for this class
		dfClass=pd.DataFrame(columns=['classname','npimage'])

		print('class: {} ({})'.format(c, len(classImages)))

		for i,imageName in enumerate(classImages):

			npimage,rescaled=LoadImage(imageName,L)
			dfClass.loc[i] = [c,npimage]

		df=pd.concat([df,dfClass], axis=0)

	df.npimage = df.npimage / 255.0

	return df.reset_index(drop=True)

def LoadImageList(im_names, L, show=False):
	'''
	Function that loads a list of images given in im_names, and returns
	them in a numpy format that can be used by the classifier.
	'''
	npimages=np.ndarray((len(im_names),L,L,3))

	for i,im_name in enumerate(im_names):
		npimage,rescaled=LoadImage(im_name,L, show)
		npimages[i]=npimage
	npimages/=255.0
	return npimages

class Cdata:

	def __init__(self, datapath, L=None, class_select=None, kind='mixed', training_data=True):
		self.datapath=datapath
		if L is None and kind!='feat':
			print('CData: image size needs to be set, unless kind is \'feat\'')
			raise ValueError
		self.L=L
		self.class_select=class_select
		self.kind=kind
		self.df=None
		self.y=None
		self.X=None
		self.Load(self.datapath, self.L, self.class_select, self.kind, training_data=training_data)
		return

	def Load(self, datapaths, L, class_select, kind='mixed', training_data=True):
		'''
		Loads dataset
		For the moment, only mixed data. Later, also pure images or pure features.
		'''
		self.L=L
		self.datapath=datapaths
		self.class_select=class_select
		self.kind=kind

		if kind=='mixed':
			self.df = LoadMixed(datapaths, L, class_select, alsoImages=True)
		elif kind=='feat':
			self.df = LoadMixed(datapaths, L, class_select, alsoImages=False)
		elif kind=='image':
			self.df = LoadImages(datapaths, L, class_select, training_data=training_data)
		else:
			raise NotImplementedError('Only mixed, image or feat data-loading')

		self.classes=self.df['classname'].unique()
		self.kind=kind 		# Now the data kind is kind. In most cases, we had already kind=self.kind, but if the user tested another kind, it must be changed
		self.Check()  		# Some sanity checks on the dataset
		self.CreateXy()		# Creates X and y, i.e. features and labels
		return

	def Check(self):
		''' Basic checks on the dataset '''

		#Number of different classes
		classes=self.classes
		if len(classes)<2:
			print('There are less than 2 classes ({})'.format(len(classes)))
			raise ValueError

		# Columns potentially useful for classification
		ucols=self.df.drop(columns=['classname','url','filename','file_size','timestamp'], errors='ignore').columns
		if len(ucols)<1:
			print('Columns: {}'.format(self.df.columns))
			raise ValueError('The dataset has no useful columns.')

		# Check for NaNs
		if self.df.isnull().any().any():
			print('There are NaN values in the data.')
			print(self.df)
			raise ValueError

		# Check that the images have the expected size
		if 'npimage' in self.df.columns:
			if self.df.npimage[0].shape != (self.L, self.L, 3):
				print('Cdata Check(): Images were not reshaped correctly: {} instead of {}'.format(self.npimage[0].shape, (self.L, self.L, 3)))

		return

	def CreateXy(self):
		'''
		Creates features and target
		- removing the evidently junk columns.
		- allowing to access images and features separately and confortably
		'''

		self.y = self.df.classname
		self.X = self.df.drop(columns=['classname','url','filename','file_size','timestamp'], errors='ignore')

		self.Ximage = self.X.npimage if (self.kind != 'feat') else None
		self.Xfeat  = self.X.drop(columns=['npimage'], errors='ignore') if (self.kind != 'image') else None

		return

def ReadArgsTxt(modelpath, verbose=False):
	'''
	Looks with what arguments the model was trained, and makes a series of consistency checks.
	Reads the following two files:
	- params.txt  (txt file with the simulation parameters -- parameter names are reconstructed through regex)
	- classes.npy (numpy file with the list of classes)

	'''

	print('NOTIMPLEMENTED WARNING: ReadArgsTxt only reads some of the input parameters (those that were useful when I wrote the function, which may have changed)')

	argsname=modelpath+'/params.txt'
	params={'L':None,
			'model':None,
			'layers':[None,None],
			'datapaths':None,
			'outpath':None,
			'datakind':None,
			'ttkind':None
			}

	# Read Arguments
	with open(argsname,'r') as fargs:
		args=fargs.read()
		if verbose:
			print('---------- Arguments for generation of the model ----------')
			print('{} contains the following parameters:\n{}'.format(argsname,args))
			print('-----------------------------------------------------------')
		for s in re.split('[\,,\),\(]',args):
			if 'L=' in s:
				params['L']=np.int64(re.search('(\d+)',s).group(1))
			if 'model=' in s:
				params['model']=re.search('=\'(.+)\'$',s).group(1)
			if 'layers=' in s: #first layer
				params['layers'][0]=np.int64(re.search('=\[(.+)$',s).group(1))
			if re.match('^ \d+',s): #second layer
				params['layers'][1]=np.int64(re.match('^ (\d+)',s).group(1))
			if 'datapaths=' in s:
				# print('datapaths: ',s)
				temp = re.search('=\[\'(.+)\'$',s).group(1)
				params['datapaths']= [temp]
				# print('params[datapaths]=',params['datapaths'])
			if 'outpath=' in s:
				params['outpath']=re.search('=\'(.+)\'$',s).group(1)
			if 'datakind=' in s:
				params['datakind']=re.search('=\'(.+)\'$',s).group(1)
			if 'ttkind=' in s:
				params['ttkind']=re.search('=\'(.+)\'$',s).group(1)
			if 'class_select=' in s:
				print('class_select: ',s)
				temp = re.search('=\[\'(.+)\'\]$',s).group(1)
				print('temp:',temp)
				params['class_select']= [temp]
				print('params[class_select]=',params['class_select'])

		if verbose:
			print('We retrieve this subset of parameters:\n',params)
			print('-----------------------------------------------------------')

	def OutputClassPaths():
		''' Auxiliary function for error messages '''
		print('\nclasses1 are the subdirectories in the following folders: ', datapaths)
		print('classes1:',classes1)
		print('\nclasses2 full filename: ',modelpath+'classes2.npy')
		print('classes2:',classes2)
		print('')

	# Now we extract the classes. Typically, they should be written in the classes.npy file.
	# Since sometimes we reduce the number of classes, these might be fewer than those contained in the dataset.

	# Extract classes from npy file in output directory
	try:
		classes=np.load(modelpath+'/classes.npy',allow_pickle=True)
	except FileNotFoundError:
		# If the classes.npy file does not exist, we just assume that all the classes were used
		print('INPUT WARNING: the file {} was not found, so we assume that the classes are all those contained in {}'.format(modelpath+'/classes.npy',params['datapaths']))
		classes=list(set([ name for idata in range(len(params['datapaths'])) for name in os.listdir(params['datapaths'][idata]) if os.path.isdir(os.path.join(params['datapaths'][idata], name))]))

	print(classes)

	return params, classes

if __name__=='__main__':
	pass


#！/usr/bin/python

# -*- coding: UTF-8 -*-

import MySQLdb

#包的导入

db = MySQLdb.connect("jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&useSSL=true","root","root","demo")

#打开数据库的连接

cursor = db.cursor()

#使用cursor()方法获得操作游标

try:
   # 执行sql语句
   cursor.execute("SELECT VERSION()")
   data = cursor.fetchone()

   print ("Database version : %s " % data)

   # 提交到数据库执行
   db.commit()
except:
   # 发生错误时回滚    回滚到获取游标的位置开始重新执行  看代码上面的文字有说明
   db.rollback()

db.close()

#关闭数据库的连接


from setuptools import setup, find_packages
import sys, os

version = '0.1'

setup(name='firstpy',
      version=version,
      description="just learning",
      long_description="""\
no long desc""",
      classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
      keywords='bioinformatics',
      author='Mark T. Holder',
      author_email='mtholder@gmail.com',
      url='',
      license='',
      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          # -*- Extra requirements: -*-
      ],
      entry_points="""
      # -*- Entry points: -*-
      """,
      test_suite='firstpy.tests'
      )


import os
import sys
import unittest

import ament_index_python

import launch
import launch.actions
import launch.substitutions

import launch_testing
import launch_testing.actions
import launch_testing.util

import pytest

@pytest.mark.launch_test
def generate_test_description():
    dut_process = launch.actions.ExecuteProcess(
        cmd=[
            sys.executable,
            os.path.join(
                ament_index_python.get_package_prefix('launch_testing'),
                'lib/launch_testing',
                'terminating_proc.py',
            ),

            # Arguments
            launch.substitutions.LaunchConfiguration('dut_arg')
        ],
    )

    return launch.LaunchDescription([

        # This argument can be passed into the test, and can be discovered by running
        # launch_test --show-args
        launch.actions.DeclareLaunchArgument(
            'dut_arg',
            default_value=['default'],
            description='Passed to the terminating process',
        ),

        dut_process,

        # In tests where all of the procs under tests terminate themselves, it's necessary
        # to add a dummy process not under test to keep the launch alive. launch_test
        # provides a simple launch action that does this:
        launch_testing.util.KeepAliveProc(),

        launch_testing.actions.ReadyToTest()
    ]), {'dut_process': dut_process}

class TestTerminatingProcessStops(unittest.TestCase):

    def test_proc_terminates(self, proc_info, dut_process):
        proc_info.assertWaitForShutdown(process=dut_process, timeout=10)

@launch_testing.post_shutdown_test()
class TestProcessOutput(unittest.TestCase):

    def test_ran_with_arg(self, dut_process):
        self.assertNotIn(
            'default',
            dut_process.process_details['cmd'],
            'Try running: launch_test test_with_args.test.py dut_arg:=arg'
        )

    def test_arg_printed_in_output(self, proc_output, test_args, dut_process):
        launch_testing.asserts.assertInStdout(
            proc_output,
            test_args['dut_arg'],
            dut_process
        )

    def test_default_not_printed(self, proc_output, dut_process):
        with self.assertRaises(AssertionError):
            launch_testing.asserts.assertInStdout(
                proc_output,
                'default',
                dut_process
            )


from ..facebook._fb_api_caller import FacebookApiCaller, InstagramApiCaller
from django.conf import settings

def get_user_content(token: str, user_id: str):
    params = {
        'fields': 'id,username'
    }
    ret = FacebookApiCaller(user_id, bearer_token=token, params=params).get()
    return ret

def get_bussiness_id(token: str):
    params = {
        'fields': 'instagram_business_account'
    }
    ret = FacebookApiCaller('me', bearer_token=token, params=params).get()
    return ret

def get_id_from(token: str, post_id: str):
    params = {
        'fields': 'from'
    }
    ret = FacebookApiCaller(post_id, bearer_token=token, params=params).get()
    return ret

def get_profile_picture(token: str, user_id: str):
    params = {
        'fields': 'profile_pic'
    }
    ret = FacebookApiCaller(user_id, bearer_token=token, params=params).get()
    return ret

def get_long_lived_token(token: str):
    params = {
        'grant_type': 'ig_exchange_token',
        'client_secret': settings.FACEBOOK_APP_CREDS['app_secret'],
        'access_token': token,
    }
    ret = InstagramApiCaller(f'oauth/access_token',
                             params=params).get()
    return ret


#!/usr/bin/env python

from charmhelpers.contrib.sysctl.watermark_scale_factor import (
    watermark_scale_factor,
    calculate_watermark_scale_factor,
    get_normal_managed_pages,
)

from mock import patch
import unittest

from tests.helpers import patch_open

TO_PATCH = [
    "log",
    "ERROR",
    "DEBUG"
]

PROC_ZONEINFO = """
Node 0, zone   Normal
  pages free     1253032
        min      16588
        low      40833
        high     65078
        spanned  24674304
        present  24674304
        managed  24247810
        protection: (0, 0, 0, 0, 0)
"""

class TestWatermarkScaleFactor(unittest.TestCase):

    def setUp(self):
        for m in TO_PATCH:
            setattr(self, m, self._patch(m))

    def _patch(self, method):
        _m = patch('charmhelpers.contrib.sysctl.watermark_scale_factor.' + method)
        mock = _m.start()
        self.addCleanup(_m.stop)
        return mock

    @patch('charmhelpers.contrib.sysctl.watermark_scale_factor.get_normal_managed_pages')
    @patch('charmhelpers.core.host.get_total_ram')
    def test_calculate_watermark_scale_factor(self, get_total_ram, get_normal_managed_pages):
        get_total_ram.return_value = 101254156288
        get_normal_managed_pages.return_value = [24247810]
        wmark = calculate_watermark_scale_factor()
        self.assertTrue(wmark >= 10, "ret {}".format(wmark))
        self.assertTrue(wmark <= 1000, "ret {}".format(wmark))

    def test_get_normal_managed_pages(self):
        with patch_open() as (mock_open, mock_file):
            mock_file.readlines.return_value = PROC_ZONEINFO.splitlines()
            self.assertEqual(get_normal_managed_pages(), [24247810])
            mock_open.assert_called_with('/proc/zoneinfo', 'r')

    def test_watermark_scale_factor(self):
        mem_totals = [17179803648, 34359607296, 549753716736]
        managed_pages = [4194288, 24247815, 8388576, 134217216]
        arglists = [[mem, managed] for mem in mem_totals for managed in managed_pages]

        for arglist in arglists:
            wmark = watermark_scale_factor(*arglist)
            self.assertTrue(wmark >= 10, "assert failed for args: {}, ret {}".format(arglist, wmark))
            self.assertTrue(wmark <= 1000, "assert failed for args: {}, ret {}".format(arglist, wmark))


import csv
import config

class ProcessingRequest:

    def __init__(self):
        self.query_part_number = ''
        self.detail_dict = {}
        self.ans = []
        self.dictation_preprocess()

    def make_new_part_num(self, part_number):
        self.query_part_number = part_number

    def dictation_preprocess(self):
        self.detail_dict = {}
        with open(config.file_location) as tsvFile:
            reader = csv.DictReader(tsvFile, dialect='excel-tab')
            for row in reader:
                the_next_detail_pt_num = row['part_number'].lower()
                if the_next_detail_pt_num in self.detail_dict:
                    self.add_to_dict(the_next_detail_pt_num,
                                     [row['supplier'],
                                      float(row['price']),
                                      row['brand'],
                                      int(row['quantity']),
                                      int(row['case_quantity']),
                                      float(row['shipping_weight']),
                                      row['handling_time'],
                                      row['image']])

                else:
                    self.detail_dict[the_next_detail_pt_num] = list()
                    self.add_to_dict(the_next_detail_pt_num,
                                     [row['supplier'],
                                      float(row['price']),
                                      row['brand'],
                                      int(row['quantity']),
                                      int(row['case_quantity']),
                                      float(row['shipping_weight']),
                                      row['handling_time'],
                                      row['image']])

    def add_to_dict(self, key, det_info):
        self.detail_dict[key].append(det_info)

    def data_proc(self):
        self.ans = self.detail_dict.get(self.query_part_number, 'empty')
        if self.ans != 'empty':
            return sorted(self.ans, key=lambda i: [i[2].lower(), i[1]])
        else:
            return 'Sorry, there is no given part number :('


from rssfeeds.site import Site
from .slack import Slack
from .mail import Mail
import html2text
import configparser

class Notify:
	def __init__(self, site, entry):
		config = configparser.RawConfigParser(allow_no_value=True)
		config.read("config/services.cfg")
		MailEnabled = config.get("Mail", "enabled")
		SlackEnabled = config.get("Slack", "enabled")
		self.site = site
		self.entry = entry

		self.formatter = html2text.HTML2Text()
		self.formatter.ignore_links = True
		self.formatter.ignore_images = True
		self.formatter.re_space  = True
		self.formatter.re_unescape = True
		self.formatter.inline_links = True
		self.formatter.protect_links = True
		self.formatter.single_linke_breaks = True

		if MailEnabled == "True":
			self.send_mail_message(self.entry)
		if SlackEnabled == "True":
			self.send_slack_message(self.entry)

	def send_slack_message(self, entry):
		Slack(self.site.getSlackConfig()).send_message(entry.title, self.formatter.handle(entry.summary)[:125], getattr(entry, "author", "Unknown Author"), entry.link)

	def send_mail_message(self, entry):
		Mail(self.site.getEmailConfig()).send_message(entry.title, self.formatter.handle(entry.summary)[:125], entry.link)


# coding: utf-8
# modelnet_dataset.py

# Refer to the following implementation:
# https://github.com/yewzijian/RPMNet/blob/master/src/data_loader/datasets.py

# Download the ModelNet40 dataset from the following URL:
# https://shapenet.cs.stanford.edu/media/modelnet40_ply_hdf5_2048.zip

import h5py
import numpy as np
import os
import sys
import torch.utils.data
import torchvision

from typing import List

sys.path.insert(0, os.path.abspath(os.path.join(
    os.path.dirname(__file__), os.pardir)))

from net import modelnet_transforms

def load_modelnet_category(file_name: str):
    # Check if the category file exists
    file_name = os.path.expanduser(file_name)
    if not os.path.exists(file_name) or not os.path.isfile(file_name):
        raise RuntimeError(f"Category file not found: {file_name}")

    # Load the categories to use
    with open(file_name, "r") as f:
        categories = [x.strip() for x in f]

    return categories

def get_train_set(dataset_dir: str, category_file_name: str, num_points: int,
                  rot_mag: float, trans_mag: float, random_mag: bool,
                  jitter_scale: float, jitter_clip: float):
    if category_file_name is not None:
        categories = load_modelnet_category(category_file_name)
        categories.sort()
    else:
        categories = None

    transforms = torchvision.transforms.Compose([
        modelnet_transforms.Resampler(num_points),
        modelnet_transforms.RandomTransformSE3(rot_mag, trans_mag, random_mag),
        modelnet_transforms.RandomJitter(jitter_scale, jitter_clip),
        modelnet_transforms.ShufflePoints()])
    train_set = ModelNetHdf(dataset_dir, "train", categories, transforms)

    return train_set

def get_test_set(dataset_dir: str, category_file_name: str, num_points: int):
    if category_file_name is not None:
        categories = load_modelnet_category(category_file_name)
        categories.sort()
    else:
        categories = None

    transforms = torchvision.transforms.Compose([
        modelnet_transforms.SetDeterministic(),
        modelnet_transforms.Resampler(num_points)])
    test_set = ModelNetHdf(dataset_dir, "test", categories, transforms)

    return test_set

class ModelNetHdf(torch.utils.data.Dataset):
    def __init__(self, dataset_dir: str, subset: str,
                 categories: List[str], transform=None):
        # Check the subset name
        if subset not in ["train", "test"]:
            raise RuntimeError(f"Invalid subset: {subset} " \
                               f"(expected: `train` or `test`)")

        # Check if the dataset path exists
        dataset_dir = os.path.expanduser(dataset_dir)
        if not os.path.exists(dataset_dir) or not os.path.isdir(dataset_dir):
            raise RuntimeError(f"Dataset not found: {dataset_dir}")

        # Load the shape names
        shape_names_path = os.path.join(dataset_dir, "shape_names.txt")
        with open(shape_names_path, "r") as f:
            self.classes = [x.strip() for x in f]
            self.category_to_idx = {
                x[1]: x[0] for x in enumerate(self.classes) }
            self.idx_to_category = {
                x[0]: x[1] for x in enumerate(self.classes) }

        # Load the HDF5 file names
        h5_file_names_path = os.path.join(dataset_dir, f"{subset}_files.txt")
        with open(h5_file_names_path, "r") as f:
            h5_file_names = [x.strip() for x in f]
            h5_file_names = [x.replace("data/modelnet40_ply_hdf5_2048/", "")
                for x in h5_file_names]
            h5_file_names = [os.path.join(dataset_dir, x)
                for x in h5_file_names]

        # Load the categories to use
        if categories is not None:
            self.classes = categories
            categories_idx = [self.category_to_idx[x] for x in categories]
        else:
            categories_idx = [self.category_to_idx[x] for x in self.classes]

        # Load the HDF5 files
        self.data, self.labels = self.read_h5_files(
            h5_file_names, categories_idx)

        self.transform = transform

    @property
    def num_classes(self):
        return len(self.classes)

    def __len__(self):
        return self.data.shape[0]

    def __getitem__(self, idx):
        sample = { "points": self.data[idx, :, :],
                   "label": self.labels[idx],
                   "idx": np.array(idx, dtype=np.int64) }

        if self.transform is not None:
            sample = self.transform(sample)

        return sample

    def read_h5_files(self, h5_file_names: List[str],
                      categories_idx: List[int]):
        all_data = []
        all_labels = []

        for file_name in h5_file_names:
            f = h5py.File(file_name, "r")
            data = f["data"][:].astype(np.float32)
            normal = f["normal"][:].astype(np.float32)
            labels = f["label"][:].flatten().astype(np.int64)

            # Filter out the unwanted categories
            mask = np.isin(labels, categories_idx).flatten()
            data = data[mask, ...]
            normal = normal[mask, ...]
            labels = labels[mask, ...]

            all_data.append(data)
            all_labels.append(labels)

        all_data = np.concatenate(all_data, axis=0)
        all_labels = np.concatenate(all_labels, axis=0)

        return all_data, all_labels


from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save

import jieba
import ChineseTone as ct

from lingodream.articles.models import Article, ProcessedText
from config.settings.base import JIEBA_DICT_PATH

jieba.set_dictionary(JIEBA_DICT_PATH)

# Create your models here.
@receiver(post_save, sender=Article)
def chinese_text_processing(sender, instance, created, *args, **kwargs):
    if instance and created:
        if instance.language == 'zh-hans' or instance.language == 'zh-hant':
            # eliminate spaces
            pre_processing = instance.body.replace(" ", "")
            # seperate Chinese words by spaces
            seg_list = jieba.cut(pre_processing, cut_all=False)

            processed_text, pt_obj_created = ProcessedText.objects.get_or_create(article=instance)
            segmented_text = " ".join(seg_list)
            processed_text.segmented_text = segmented_text

            #generate Pinyin
            pinyin_list = []
            punctuation_marks = ('，', ',', '。', '.', '、', '：',':', '《', '》', '〈', '〉', '<', '>', '「', '」',
                                '︽', '︾', '︿', '﹀', '（', '）', '(', ')', '［', '］', '[', ']', '{', '}', '【',
                                '】', '〔', '〕', '〘', '〙', "’", "'", '❨', '❩', '❴', '❵', '⟨', '⟩', '‒–', '—', '―',
                                '~', '──', '…', '...', '！', '!', '﹏', '＿', '～', '‐', '?', '？', '『', '』', '"',
                                '；', ';', '/', "\\", '□', '×', '&', '@', '*', '•', '^', '°', "〃", '々', '#', '№',
                                '÷', '%', '‰', '‱', '¶', '§', '_', '|', '¦', '©', '-', '+', '=', '®', '$', '€', '¥',
                                '︴', '０', '１', '２', '３', '４','５', '６', '７', '８', '９', '＠', '＼', '＾', '｀',
                                '＊', '＃', '％', '￥', '＜', '＞', '“', '”')

            for wd in segmented_text.split(" "):
                if ct.ChineseHelper.isChinese(wd):
                    p = '-'.join(ct.PinyinHelper.convertToPinyinFromSentence(wd, pinyinFormat=ct.PinyinFormat.WITH_TONE_MARK))
                else:
                    p = ''.join(ct.PinyinHelper.convertToPinyinFromSentence(wd, pinyinFormat=ct.PinyinFormat.WITH_TONE_MARK))

                if p in punctuation_marks or p.isnumeric():
                    p = '·'
                pinyin_list.append(p)

            processed_text.pronunciation = " ".join(pinyin_list)

            processed_text.save()


# Data Model:
# ===========
# lines is a list of "\n" terminated strings from the input file
#
# In Part 1 I use a brute force solution, and assume each letter
# pair in the template and all mutations will be valid rules
# There for I convert each rule in the form "CH -> B" to a dictionary
# entry with key = "CH" and value = "CBH". Therefore I can replace
# each pair in the input with the dictionary look up (because of the
# overlap, all but the last triple will be truncated to two characters.
# Note this solution grows very quickly doubling on each iteration, and
# within a few dozen iteration will yield a string that does not fit in
# memory.

# In part 2, I use a new algorithm which keeps track of all character
# pairs in the input as well as how many times they exist.  This dictionary
# will always be no bigger than the list of rules, and the counts can grow
# as large as the system can handle (the counts will be bigger than a Int32,
# but smaller than an int64, although Python can grow ints arbitrarily large)
# I convert each rule in the form "CH -> B" to a dictionary entry with
# key = "CH" and value = ("CB", "BH"). on each iteration, a new dictionary
# of pairs is created. If for example, I found there were 12 pairs of "CH" in the
# the input, I would add 12 to the counts for "CB" and "BH".
# counting is more complicated, because of the duplicates.  I noted that the first
# and last characters are always the same after each mutation, and they are the only
# locations that are not counted twice in the total of the pairs.
# for example in the stating sample template "NNCB", ends = (N,B) and
# pairs = {"NN":1, "NC":1, "CB":1}, the character counts in ends {N:1,B:1}, added
# to the character counts in pairs {N:3,C:2,B:1} = {N:4,C:2,B:2}, which is exactly
# double the real character counts

# Obviously the solution for Part2 also is a valid solution for Part1, but I kept
# both for comparison.

import collections # for Counter dictionary

def part1(lines):
    template, rules = parse(lines)
    polymer = template
    for i in range(0,10):
        polymer = mutate(polymer, rules)
    count = max_less_min(polymer)
    return count

def part2(lines):
    pairs, rules, ends = parse2(lines)
    for i in range(0,40):
        pairs = mutate2(pairs, rules)
    count = max_less_min2(pairs, ends)
    return count

#########
# Part 1 functions
#########

def parse(lines):
    template = lines[0].strip()
    rules = {}
    for line in lines[2:]:
        pair, result = line.strip().split(" -> ")
        rules[pair] = pair[0] + result + pair[1]
    return template, rules

def mutate(template, rules):
    result = "X"
    # assume that there is a valid pair (in rules) at each step in the polymer
    for i in range(0, len(template) - 1):
        pair = template[i:i+2]
        # if the assumption fails, i.e. a character pair in the polymer does not mutate,
        # the following lookup in rules will fail
        triple = rules[pair]
        result = result[:-1] + triple
    return result

def max_less_min(polymer):
    counts = collections.Counter(polymer)
    max = 0
    min = len(polymer)
    for count in counts.values():
        if count > max:
            max = count
        if count < min:
            min = count
    return max - min

#########
# Part 2 versions
#########

def parse2(lines):
    template = lines[0].strip()
    rules = {}
    pairs = {}
    for line in lines[2:]:
        pair, result = line.strip().split(" -> ")
        rules[pair] = (pair[0] + result, result + pair[1])
    for i in range(0, len(template) - 1):
        pair = template[i:i+2]
        if pair not in pairs: pairs[pair] = 0
        pairs[pair] += 1
    # The ends of the template are always the same, and they need to be added to the
    # character counts in pairs before halving the counts to get true count
    ends = (template[0],template[-1])
    return pairs, rules, ends

def mutate2(pairs, rules):
    # update has to happen simultaneously, so write to a new dictionary
    new_pairs = {}
    for pair, count in pairs.items():
        one, two = rules[pair]
        if one not in new_pairs: new_pairs[one] = 0
        new_pairs[one] += count
        if two not in new_pairs: new_pairs[two] = 0
        new_pairs[two] += count
    return new_pairs

def max_less_min2(pairs, ends):
    counts = {}
    counts[ends[0]] = 1
    counts[ends[1]] = 1
    for chars, count in pairs.items():
        if chars[0] not in counts: counts[chars[0]] = 0
        if chars[1] not in counts: counts[chars[1]] = 0
        counts[chars[0]] += count
        counts[chars[1]] += count
    max = 0
    min = 1_000_000_000_000_000
    for count in counts.values():
        if count > max:
            max = count
        if count < min:
            min = count
    # all the counts are doubles since adjacent pairs overlap
    return (max - min)//2

if __name__ == '__main__':
    # lines = open("test.txt").readlines() # as a list of line strings
    lines = open("input.txt").readlines() # as a list of line strings
    print(f"Part 1: {part1(lines)}")
    print(f"Part 2: {part2(lines)}")


from os import system
from os.path import exists
from hashlib import md5
from json import dump, load, loads
from random import getrandbits, randint

def get_db_content():
    with open('skey_parameters/host/db.json', 'r', encoding='utf-8') as db_read:
        db = db_read.read()
        if not db or db == 'null':
            return {}
        else:
            return loads(db)

def user_is_in_database(user):
    if not get_db_content():
        return False
    return user in get_db_content().keys()

def generate_hashes(number, n):
    hashes = []
    first = md5(bytes(number)).hexdigest()
    hashes.append(first)
    for _ in range(n):
        new = md5(first.encode()).hexdigest()
        hashes.append(new)
        first = new
    return hashes

def save_user_in_db(user, hash):
    updated_db_content = get_db_content()
    updated_db_content.update({user: hash})
    with open('skey_parameters/host/db.json', 'w', encoding='utf-8') as db_write:
        dump(updated_db_content, db_write, indent=4)

def get_user_hashes(user):
    with open(f'skey_parameters/clients/{user}.json', 'r', encoding='utf-8') as hashes_read:
        hashes = hashes_read.read()
        return loads(hashes) if hashes else {}

def save_user_hashes(user, hashes):
    if not exists(f'skey_parameters/clients/{user}.json'):
        with open(f'skey_parameters/clients/{user}.json', 'w', encoding='utf-8') as hashes_write:
            dump({'hashes': hashes}, hashes_write, indent=4)
    else:
        user_hashes = get_user_hashes(user).get('hashes')
        with open(f'skey_parameters/clients/{user}.json', 'w', encoding='utf-8') as hashes_write:
            dump({'hashes': [*hashes]}, hashes_write, indent=4)

def create_user_params():
    user = input('Введите имя пользователя: ')
    number = getrandbits(int(input('Введите длину случайного числа: ')))
    with open(f'skey_parameters/clients/{user}_paramaters.json', 'w', encoding='utf-8') as params:
        dump({'name': user, 'number': number}, params, indent=4)
    # with open(f'skey_parameters/clients/{user}.py', 'w', encoding='utf-8') as prog:
    #     prog.write(f'print(\'{user}\')')
    return

def register_user():
    user = input('Имя пользователя: ')

    if not exists(f'skey_parameters/clients/{user}_paramaters.json'):
        print('Необходимо произвести генерацию параметров пользователя перед регистрацией в системе')
        return

    if user_is_in_database(user) and get_user_hashes(user)['hashes']:
        print('Пользователь с таким именем уже зарегестрирован')
        return

    n = int(input('Введите параметр n (число одноразовых паролей): '))
    with open(f'skey_parameters/clients/{user}_paramaters.json') as params:
        parameters = load(params)
        name, number = parameters['name'], parameters['number']
    hashes = generate_hashes(number, n)
    save_user_in_db(user, hashes[-1])
    save_user_hashes(user, hashes[:-1])
    print(f'Пользователь {user} зарегистрирован')
    return
    # if not user_is_in_database(user):

def login():
    user = input('Введите имя пользователя: ')
    if not user_is_in_database(user):
        print('Пользователь с таким именем не зарегистрирован')
        return

    if not get_user_hashes(user)['hashes']:
        print('Для данного пользователя исчерпан запас ключей, повторите регистрацию')
        return

    last = get_user_hashes(user)['hashes'][-1]
    _hash = md5(last.encode()).hexdigest()
    if _hash == get_db_content()[f'{user}']:
        print('Вход выполнен успешно')
        updhashes = get_user_hashes(user)['hashes']
        updhashes.remove(last)
        save_user_hashes(user, updhashes)
        save_user_in_db(user, last)
    else:
        print('Вход не выполнен')

def main():
    while True:
        opt = input(
            'Выберите действие:\n0 - Генерация параметров пользователя для регистрации\n1 - Регистрация пользователя в системе\n2 - Вход в систему\n3 - Выход:\n')
        if opt == '0':
            create_user_params()
        elif opt == '1':
            register_user()
        elif opt == '2':
            login()
        elif opt == '3':
            break
        else:
            print('Ошибка, повторите ввод')

if __name__ == '__main__':
    main()


from datetime import datetime
import requests
import json
import uuid

class paypalSubscription:

    # Initializer / Instance Attributes
    def __init__(self, arr):
        self.token = arr['token']
        self.plan_id = arr['plan_id']
        self.email = arr['email']
        self.name = arr['name']
        self.currency_code = arr['currency_code']
        self.price = arr['price']

    def addSubscription(self):

        headers = {
        'Accept': 'application/json',
        'Authorization': 'Bearer '+ self.token

        }
        dateObj = datetime.now()
        date = str(dateObj)
        date =date[:19]
        date = date.replace(' ', 'T')
        date = date + 'Z'

        data={
          "plan_id": self.plan_id,
          "start_time": date,
          "quantity": "1",
          "shipping_amount": {
            "currency_code": self.currency_code,
            "value": self.price
          },
          "subscriber": {
            "name": {
              "given_name": self.name,

            },
            "email_address": self.email,
            "shipping_address": {
              "name": {
                "full_name": self.name
              },
              "address": {
                "address_line_1": "2211 N First Street",
                "address_line_2": "Building 17",
                "admin_area_2": "San Jose",
                "admin_area_1": "CA",
                "postal_code": "95131",
                "country_code": "US"
              }
            }
          },
          "application_context": {
            "brand_name": "walmart",
            "locale": "en-US",
            "shipping_preference": "SET_PROVIDED_ADDRESS",
            "user_action": "SUBSCRIBE_NOW",
            "payment_method": {
              "payer_selected": "PAYPAL",
              "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"
            },
            "return_url": "https://example.com/returnUrl",
            "cancel_url": "https://example.com/cancelUrl"
          }
        }
        # return data
        data = json.dumps(data)

        url = 'https://api.sandbox.paypal.com/v1/billing/subscriptions'

        r = requests.post(url, headers=headers, data=data)

        # url = 'https://api.sandbox.paypal.com/v1/billing/plans'
        # r = requests.get(url=url, headers=headers)

        response = json.loads(r.text)
        return response
        if r.status_code == 201:
            return response
        return 'not successful in creating plan'


import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
import math
import os

from os import listdir
from os.path import isfile, join

from PIL import Image

PATH_IMG = '/home/tai/Desktop/M2/ResultsFromServer/images/epoch001_fake_B.png'

def mse2(imageA, imageB):
	err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
	err /= float(np.size(imageA))
	return err

def psnr(im1,im2, pmax=1.0):
    NUM_ELEMENTS = np.size(im1)
    mse = mse2(im1, im2)
    psnr = 10*np.log10(pmax**2/mse)
    return psnr

print("PSNR:")

for i in range(112):
    str_index = str(i+1).zfill(3)
#     print(str_index)
    img_org = np.asanyarray(Image.open(PATH_IMG + "epoch" + str_index + "_real_B.png"), dtype=np.uint8)
    img_denois = np.asanyarray(Image.open(PATH_IMG + "epoch" + str_index + "_fake_B.png"), dtype=np.uint8)
    img_nois = np.asanyarray(Image.open(PATH_IMG + "epoch" + str_index + "_real_A.png"), dtype=np.uint8)
#     plt.imshow(img_denois)
    print("Noisy: %.2f -- Denoised: %.2f (dB)" % (psnr(img_org, img_nois,pmax=255.0), psnr(img_org, img_denois, pmax=255.0)))
#     print(np.amax(img_org))


"""
Created on Thu Jun  4 19:23:29 2020

@author: Aditya
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor as RF
from sklearn.metrics import r2_score

dataset=pd.read_csv("./Position_Salary.csv")
X=dataset.iloc[:,1].values        #input
Y=dataset.iloc[:,2].values          #output

X=X.reshape(-1,1)
#Y=Y.reshape(-1,1)
plt.scatter(X,Y,color="blue")
print(X)
print(Y)

reg=RF(n_estimators=200,max_leaf_nodes=9,random_state=1)
reg.fit(X,Y)

print(reg.estimator_[5].predict([[7.5]]))

grid=np.arange(min(X),max(X),0.1)
grid=grid.reshape(-1,1)
plt.plot(grid,reg.predict(grid),color="green")
#plt.plot(X,Y,color="red")
plt.show()

print(r2_score(Y, reg.predict(X)))


import io
import logging
from logging.handlers import TimedRotatingFileHandler
import signal
import sys
from datetime import date, datetime, timedelta
from threading import Thread
from time import sleep
from typing import Any, Dict, Optional, List

import paho.mqtt.client as mqtt
import pytz
from astral import LocationInfo
from astral.sun import sun

from .camera import SkyPiCamera
from .output import SkyPiOutput
from .storage import SkyPiFileManager

class SkyPiWatchdog(Thread):
    def __init__(self, timeout_seconds: Optional[int], reset_cmd: Optional[List]):
        super().__init__(daemon=True)
        self.log = logging.getLogger("watchdog")
        self.timeout = (
            timedelta(seconds=timeout_seconds) if timeout_seconds is not None else None
        )
        self.reset_cmd = reset_cmd
        self.last_ping = None

    def run(self):
        if self.timeout is None:
            return
        while True:
            sleep(5)
            self.check()

    def check(self):
        if self.last_ping is None:
            return

        elapsed = datetime.now() - self.last_ping

        if elapsed < self.timeout:
            return

        self.log.warn(
            f"Watchdog reset after {elapsed.total_seconds()}s! Shutting down."
        )
        if self.reset_cmd is not None:
            self.publish("watchdog_timeout", elapsed.total_seconds())
            sleep(3)
            self.run_cmd(self.reset_cmd)
        sys.exit()

    def ping(self):
        self.last_ping = datetime.now()

    def pause(self):
        self.last_ping = None

class SkyPiControl:
    MODE_RECALC_TIME: int = 120  # seconds
    WATCHDOG_TIMEOUT = timedelta(seconds=360)

    stop = False
    shutdown = False

    current_mode: Optional[str] = None
    forced_mode: Optional[str] = None

    watchdog: Optional[SkyPiWatchdog]
    file_managers: Dict[str, SkyPiFileManager]

    log: logging.Logger
    mqtt_client: Optional[mqtt.Client]
    location: LocationInfo

    update_camera_settings: Dict[str, Any] = {}

    def __init__(self, settings: Dict[str, Any]):
        self.log = logging.getLogger()
        signal.signal(signal.SIGINT, self.signal_handler)

        self.configure_logging(settings["log"])

        self.settings = settings
        if 'mqtt' in self.settings:
            self.mqtt_client = mqtt.Client(self.settings["mqtt"]["client_name"])
            self.mqtt_client.on_connect = self.on_connect
            self.mqtt_client.on_message = self.on_message
            self.mqtt_client.connect(self.settings["mqtt"]["server"])
            self.mqtt_client.loop_start()
        else:
            self.mqtt_client = None

        self.location = LocationInfo(**self.settings["location"])
        if 'watchdog' in self.settings:
            self.watchdog = SkyPiWatchdog(**self.settings["watchdog"])
            self.watchdog.start()
        else:
            self.watchdog = None

        self.file_managers: Dict[str, SkyPiFileManager] = {
            name: SkyPiFileManager(name=name, **kwargs)
            for (name, kwargs) in self.settings["files"].items()
        }

    def configure_logging(self, filename):
        handler = TimedRotatingFileHandler(filename, when="D", backupCount=7)
        handler.setFormatter(
            logging.Formatter("%(asctime)s  %(name)s  %(levelname)s \t%(message)s")
        )
        handler.setLevel(logging.DEBUG)
        self.log.addHandler(handler)

    def on_message(self, client, userdata, msg):
        base = self.settings["mqtt"]["topic"]
        topic = msg.topic[len(base) :]
        payload = msg.payload.decode("utf-8").strip()
        self.log.debug(f"Received MQTT message on topic {topic} containing {payload}")
        if topic == "force_mode":
            self.forced_mode = payload
            self.stop = True
        if topic.startswith("set/"):
            setting = topic[len("set/") :]
            try:
                value = eval(payload)
            except Exception as e:
                self.log.exception(e)
            else:
                self.update_camera_settings[setting] = value

    def on_connect(self, client, userdata, flags, rc):
        base = self.settings["mqtt"]["topic"]
        client.subscribe(f"{base}#")

    def signal_handler(self, sig, frame):
        self.log.warning("Caught Ctrl+C, stopping....")
        self.stop = True
        self.shutdown = True

    def publish(self, ext, message):
        if self.mqtt_client is None:
            return
        topic = self.settings["mqtt"]["topic"] + ext
        self.log.debug(f"mqtt: {topic}: {message}")
        self.mqtt_client.publish(topic, message)

    def calculate_event_times(self):
        events_yesterday = sun(
            self.location.observer, date=date.today() - timedelta(days=1)
        )
        events = sun(self.location.observer, date=date.today())
        events_tomorrow = sun(
            self.location.observer, date=date.today() + timedelta(days=1)
        )

        self.log.info(f"Astral events yesterday")
        for event, time in events_yesterday.items():
            self.log.info(f" * {event} at {time}")

        self.log.info(f"Astral events today")
        for event, time in events.items():
            self.log.info(f" * {event} at {time}")

        self.log.info(f"Astral events tomorrow")
        for event, time in events_tomorrow.items():
            self.log.info(f" * {event} at {time}")

        now = datetime.now(pytz.utc)

        if now < events["dawn"]:
            self.current_mode = "night"
            self.canonical_date = date.today() - timedelta(days=1)
            self.last_switch = events_yesterday["dusk"]
            self.next_switch = events["dawn"]
        elif events["dawn"] <= now < events["dusk"]:
            self.current_mode = "day"
            self.canonical_date = date.today()
            self.last_switch = events["dawn"]
            self.next_switch = events["dusk"]
        else:
            self.current_mode = "night"
            self.canonical_date = date.today()
            self.last_switch = events["dusk"]
            self.next_switch = events_tomorrow["dawn"]

        self.log.info(f"new mode: '{self.current_mode}' until {self.next_switch}")
        self.publish("mode", self.current_mode or "none")

        self.total_period_time = (self.next_switch - self.last_switch).total_seconds()

    def run(self):
        while not self.shutdown:
            self.calculate_event_times()
            if self.forced_mode is not None:
                self.current_mode = self.forced_mode
                self.forced_mode = None
            if self.current_mode is None:
                self.log.debug("Nothing to do...")
                sleep(60)
            elif self.current_mode in self.settings["modes"]:
                self.log.debug(f"Starting mode {self.current_mode}")
                self.run_mode(self.current_mode)
                for _, filemanager in self.file_managers.items():
                    filemanager.cleanup()
            self.stop = False

        for _, filemanager in self.file_managers.items():
            filemanager.close()

    def run_mode(self, mode):
        settings = self.settings["modes"][mode]
        started = datetime.now()
        canonical_date = self.canonical_date

        self.log.info(
            f"Started mode {mode}\n - started: {started}\n - canonical_date: {canonical_date}"
        )

        output = SkyPiOutput(
            self.file_managers, settings["processors"], date=canonical_date, mode=mode,
        )

        if self.watchdog is not None:
            self.watchdog.ping()

        with SkyPiCamera(settings["camera"]) as camera:

            if self.watchdog is not None:
                self.watchdog.ping()

            stream = io.BytesIO()

            image_time = datetime.now()
            if 'annotation' in settings:
                camera.annotate_text = settings["annotation"].format(timestamp=image_time)
            # sleep(10)
            self.log.debug(f"First image: {camera.annotate_text}")

            for _ in camera.capture_continuous(stream, **settings["capture_options"]):
                # Touch the watchdog
                if self.watchdog is not None:
                    self.watchdog.ping()
                conversion_time = datetime.now()

                # Check if we need to update the camera settings
                for key, value in self.update_camera_settings.items():
                    try:
                        setattr(camera, key, value)
                    except Exception as e:
                        self.log.exception(e)
                    else:
                        self.log.info(f"Setting camera setting {key} to {repr(value)}")
                self.update_camera_settings = {}

                # Let our subscribers know
                self.log.debug(f"Click! (took {(datetime.now()-image_time).seconds}s)")

                stream.truncate()
                stream.seek(0)
                output.add_image(
                    stream, timestamp=image_time,
                )

                self.log.debug(
                    f"Image conversion done (took {(datetime.now()-conversion_time).total_seconds()}s)"
                )
                sleep(settings["wait_between"])

                now_tz = datetime.now(pytz.utc)

                in_period = (now_tz - self.last_switch).total_seconds()
                period_percent = (in_period / self.total_period_time) * 100

                awb_red, awb_blue = camera.awb_gains
                status = {
                    "capture/exposure_speed": camera.exposure_speed,
                    "capture/iso": camera.iso,
                    "capture/awb_gains/red": float(round(awb_red, 2)),
                    "capture/awb_gains/blue": float(round(awb_blue, 2)),
                    "capture/gains/digital": float(round(camera.digital_gain, 2)),
                    "capture/gains/analog": float(round(camera.analog_gain, 2)),
                    "capture/exposure_duration": (
                        conversion_time - image_time
                    ).total_seconds(),
                    "timing/period_percent": period_percent,
                }

                for topic, message in status.items():
                    self.publish(topic, message)

                if now_tz > self.next_switch or self.stop:
                    self.log.info("Finishing recording.")
                    break

                stream.seek(0)
                image_time = datetime.now()
                if 'annotation' in settings:
                    camera.annotate_text = settings["annotation"].format(
                        timestamp=image_time
                    )
                self.log.debug(f"Next image: {camera.annotate_text}")

        if self.watchdog is not None:
            self.watchdog.pause()
        stream.close()
        output.close()

        finished = datetime.now()
        self.log.info(f"Finished at {finished}; watchdog disabled.")


"""
Obtenemos los seguidores del usuario que pasamos como parámetro.
"""
import sys
import tweepy
import credentials  # las credenciales para la conexión están aquí

if len(sys.argv) != 2:
    print("Número de argumentos erróneo", file=sys.stderr)
    sys.exit(-1)

client = tweepy.Client(bearer_token=credentials.BEARER_TOKEN, wait_on_rate_limit=True)

user_query = client.get_user(username=sys.argv[1])
for user in tweepy.Paginator(client.get_users_followers, id=user_query.data.id, max_results=1000).flatten():
    print(user.username)


import datetime
from functools import wraps
import re

import jwt
from flask import request, jsonify
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from marshmallow import ValidationError
from app.utils.jwt import encode as jwt_encode, decode as jwt_decode

from app.utils.error import Error, StatusCode

SECRET_KEY = "YOUR_SECRET_STRING"

def access_token_required(f):
    @wraps(f)
    def decorated_func(*args, **kwargs):
        auth_header = request.headers.get("Authorization")

        if auth_header is None:
            errors = {
                "token": "Access token was missing"
            }
            raise Error(StatusCode.UNAUTHORIZED, "Missing authorization header", errors)

        token = auth_header.split(" ")[1]

        if not token:
            errors = {
                "token": "Access token was missing"
            }
            raise Error(StatusCode.UNAUTHORIZED, "Unauthorized request", errors)

        try:
            # Decode token by app's secret key
            payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
        except jwt.ExpiredSignatureError:
            # Check that token is expired
            errors = {
                "token": "Token has expired. Please login again"
            }
            raise Error(StatusCode.UNAUTHORIZED, "Your session has expired. Please login again", errors)

        except jwt.InvalidTokenError:
            # Check that token is invalid
            errors = {
                "token": "Invalid token"
            }
            raise Error(StatusCode.UNAUTHORIZED, "Request unauthorized", errors)

        print("payload: ", payload)
        # id = payload['id']
        # kwargs['user_id']=payload['id']
        # kwargs['role']=payload['role']
        return f(user_id = payload['id'],role = payload['role'], *args, **kwargs)

    return decorated_func

def get_value_from_access_token():
    auth_header = request.headers.get("Authorization")

    if auth_header is None:
        errors = {
            "token": "Access token was missing"
        }
        return None

    token = auth_header.split(" ")[1]

    if not token:
        return None

    try:
            # Decode token by app's secret key
        payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
    except:
            # Check that token is expired
        return None

    return payload['role']

def validate_schema(schema, data):
    """Validate request data
    Return a tuple (success, data)
    success: True if passing validation
    data: if success is False, data is set to error messages, else set to validated data
    """
    try:
        validated_data = schema.load(data)
        print("rasing1...")
    except ValidationError as e:
        errors = e.messages
        print("rasing...")
        raise Error(StatusCode.BAD_REQUEST, "Validation failed", errors)

    return validated_data

def is_valid_password(password):

    return re.match("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{5,10}$", password)

# def jwt_encode(user):
#     token = jwt.encode({
#         "sub": user.id,
#         "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=3)
#     }, SECRET_KEY)
#     return token

# def jwt_decode(token):
#     return jwt.decode(token, SECRET_KEY, algorithms=['HS256'])


from flask import Flask, Blueprint, request
from flask_login import login_required, current_user
from app.models import db, League, User, LeaguesTournament
from app.forms import LeagueForm

#  url_prefix="/api/leagues
league_routes = Blueprint("leagues", __name__)

# GET ALL LEAGUES
@league_routes.route("/")
@login_required
def leagues():
    leagues = League.query.all()
    return {"leagues": [league.to_dict() for league in leagues]}, 201

# GET USER'S LEAGUES
@league_routes.route("/user")
@login_required
def leagues_user():
    leagues = current_user.leagues
    return {"leagues": [league.to_dict() for league in leagues]}, 201

# GET LEAGUES WHICH ARE PUBLIC AND USER'S
@league_routes.route("/public")
@login_required
def leagues_public():
    league1 = League.query.filter_by(is_private=False).all()
    if current_user:
        user_leagues = current_user.leagues
        leagues = list(set(league1).union(set(user_leagues)))
        return {"leagues": [league.to_dict() for league in leagues]}, 201
    else:
        return {"leagues": [league.to_dict() for league in league1]}, 201

# CREATE LEAGUE
@league_routes.route("/new", methods=["POST"])
@login_required
def create_league():
    userId = int(current_user.id)
    res = request.get_json()
    form = LeagueForm()
    form["csrf_token"].data = request.cookies["csrf_token"]
    tournaments = res["tournaments"]
    if tournaments == None:
        return {"error": ["League must choose atleast one tournament"]}, 403

    if len(tournaments) < 1:
        return {"error": ["League must choose atleast one tournament"]}, 403

    if form.validate_on_submit():
        league = League (
            owner_id=userId,
            name=res["name"],
            display_pic=res["display_pic"],
            team_budget=res["team_budget"],
            is_private=res["is_private"],
        )
        db.session.add(league)
        db.session.commit()
        for tournament in tournaments:
            league_tournament = LeaguesTournament(
                league_id=league.id,
                tournament_id=tournament
            )
            db.session.add(league_tournament)
            db.session.commit()

        return {"league": league.to_dict()}, 201

# EDIT LEAGUE
@league_routes.route("/<int:id>", methods=["PUT"])
@login_required
def edit_league(id):
    userId = int(current_user.id)
    league = League.query.get(id)
    if (league == None):
        return {"errors": ["League does not exist"]}, 404
    if (league.owner_id != userId):
        return {"errors": ["This league doesn't belong to this user"]}, 403

    res = request.get_json()
    form = LeagueForm()
    form["csrf_token"].data = request.cookies["csrf_token"]
    if form.validate_on_submit():
        league.name = res["name"]
        league.display_pic = res["display_pic"]
        league.is_private = res["is_private"]
        db.session.commit()

        print("LEAGUE -------->", league.to_dict())
        return {"league": league.to_dict()}, 200

    return form.errors

# DELETE LEAGUE
@league_routes.route("/<int:league_id>", methods=["DELETE"])
@login_required
def delete_league(league_id):
    userId = int(current_user.id)
    league = League.query.get(league_id)
    if (league == None):
        return {"errors": ["League does not exist"]}, 404
    if (league.owner_id != userId):
        return {"errors": ["This league doesn't belong to this user"]}, 403

    db.session.delete(league)
    db.session.commit()

    return {"league": league_id}


from django.db.models import Manager, Q
from django.utils.translation import ugettext_lazy as _
from django.db.models.query import QuerySet
from django.utils.translation import get_language, activate
from django.conf import settings

from hvad.manager import TranslationManager

from datetime import datetime

def isvalidlanguage(lang):
	found = False
	for k,v in settings.LANGUAGES:
		if k == lang:
			found = True
			break
	return found

if settings.LANGUAGE_CODE != get_language() and not isvalidlanguage(get_language()):
	activate(settings.LANGUAGE_CODE)

class BaseManager(TranslationManager):
	def translated(self):
		qs = self.model.objects
		try:
			getattr(self.model.objects, 'language')
			lang = get_language()
			if not isvalidlanguage(lang):
				lang = settings.LANGUAGE_CODE
			if not isvalidlanguage(lang) and len(settings.LANGUAGES) > 0:
				lang = settings.LANGUAGES[0][0]
			qs = qs.language(lang)
		except AttributeError:
			pass
		return qs

class PublishableManager(BaseManager):

	STATUS_DRAFT = 1
	STATUS_PUBLISHED = 2
	STATUS_DISABLED = 3
	STATUS_CHOICES = (
		(STATUS_DRAFT, _("Draft")),
		(STATUS_PUBLISHED, _("Published")),
		(STATUS_DISABLED, _("Disabled")),
	)

	def published(self, request=None):
		# allow staff users to preview pages
		allow_unpublished  = (request and hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff)
		if allow_unpublished:
			return self.translated()
		else:
			return self.translated().filter(
				Q(expiry_date__gte=datetime.now()) | Q(expiry_date__isnull=True),
				publish_date__lte=datetime.now(),
				status=PublishableManager.STATUS_PUBLISHED
			)

class PromotableManager(PublishableManager):
	def promoted(self):
		return self.published().filter(promoted=True)


from django.conf.urls import url

from . import views

app_name = 'invest'

urlpatterns = [
        # ex: /invest/
        url(r'^$', views.index, name='index'),
        url(r'^adddb/$', views.adddb, name='adddb'),
        url(r'^compare/$', views.compare, name='compare'),
        url(r'^data/$', views.dataoperation, name='dataoperation'),
        url(r'^insert/$', views.insert, name='insert'),
        url(r'^delete/$', views.delete, name='delete'),
        url(r'^deleteid/$', views.deleteid, name='deleteid'),
        url(r'^deleteidbatch/$', views.deleteidbatch, name='deleteidbatch'),

        # ex: /polls/5/
        #url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
        # ex: /polls/5/results/
        #url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
        # ex: /polls/5/vote/
        #url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
            ]


from django.shortcuts import render, redirect
from django.views import generic
from .models import Pregunta, Persona
from .forms import PreguntaForm, PersonaForm
from django.contrib.auth.decorators import login_required, permission_required

# Create your views here.
@login_required
def eliminar_persona(request, id):
    persona = Persona.objects.get(id=id)
    persona.delete()

    return redirect(to="listar_persona")
@login_required
def modificar_persona(request, id):
    persona = Persona.objects.get(id=id)
    data = {
        'form': PersonaForm(instance=persona)
    }
    if request.method =='POST':
        formulario = PersonaForm(data=request.POST, instance=persona, files=request.FILES )
        if formulario.is_valid():
            formulario.save()
            data['mensaje'] = "Persona Modificada Correctamente"
            data['form'] = formulario
    return render(request,'modificar_persona.html', data)
@login_required
def persona_list(request):
    preguntas = Pregunta.objects.all()
    data = {
        'preguntas':preguntas
    }
    return render(request, 'persona_list.html', data)
@permission_required('agregar_pre')
def nueva_pregunta(request):
    data = {
        'form':PreguntaForm()
    }
    if request.method == 'POST':
        formulario = PreguntaForm(request.POST)
        if formulario.is_valid():
            formulario.save()
            data['mensaje'] = "Enviado Correctamente"
    return render(request, 'agregar_pre.html',data)
@login_required
def modificar_pregunta(request, id):
    pregunta = Pregunta.objects.get(id=id)
    data = {
        'form': PreguntaForm(instance=pregunta)
    }
    if request.method =='POST':
        formulario = PreguntaForm(data=request.POST, instance=pregunta)
        if formulario.is_valid():
            formulario.save()
            data['mensaje'] = "Pregunta Modificada Correctamente"
            data['form'] = formulario

    return render(request,'modificar_pre.html', data)
@login_required
def eliminar_pregunta(request, id):
    pregunta = Pregunta.objects.get(id=id)
    pregunta.delete()

    return redirect(to="persona_list")
@permission_required('nueva_persona')
def nueva_persona(request):
    data = {
        'form':PersonaForm()

    }
    if request.method == 'POST':
        formulario = PersonaForm(request.POST, files=request.FILES)
        if formulario.is_valid():
            formulario.save()
            data['mensaje']= "Guardado correctamente"

    return render(request, 'nueva_persona.html', data)

def home(request):

    return render(request, 'home.html')

def registro(request):
    return render(request, 'registro.html')

def login(request):
	return render(request, 'login.html')

def preguntasyrespuestas(request):
    return render(request, 'preguntasyrespuestas.html')

def listar_persona(request):
    personas= Persona.objects.all()
    data = {
        'personas': personas
    }
    return render(request, 'listar_persona.html', data)

class PersonaListView(generic.ListView):
	model = Persona

class PersonaDetailView(generic.DetailView):
	model = Persona


import category_encoders as ce
from .base import EncoderWrapper

class HashingEncoder(EncoderWrapper):
    def __init__(self, cat_vars, *args, **kwargs):
        super().__init__(
            cat_vars=cat_vars,
            encoder_class=ce.hashing.HashingEncoder,
            suffix='hse',
            *args, **kwargs
        )


import logging

from helpers.config import CONFIG
from helpers.odoo_api import OdooAPI
from update_meal_voucher_products.categories import MEAL_VOUCHER_CATEGORIES

def get_produts_that_should_be_mv(odoo_api):
    entities = odoo_api.search_read(
        entity="product.template",
        cond=[
            ["sale_ok", "=", True],
            ["meal_voucher_ok", "=", False],
            ["categ_id", "in", MEAL_VOUCHER_CATEGORIES],
        ],
        fields=["id", "name"],
    )
    return entities

def set_products_mv(odoo_api, entities):
    odoo_api.update(
        entity="product.template",
        ids=[p["id"] for p in entities],
        fields={"meal_voucher_ok": True},
    )

def main():
    config = CONFIG.get(__name__)
    odoo_api = OdooAPI(
        config.odoo.url,
        config.odoo.db,
        config.odoo.user,
        config.odoo.password,
    )
    entities = get_produts_that_should_be_mv(odoo_api)
    if entities:
        set_products_mv(odoo_api, entities)
        logging.info(f"{len(entities)} products have been updated")
    else:
        logging.info("No product to update")


# -*- coding:utf-8 -*-
import inspect
import json
import logging

import yaml
from appium.webdriver import WebElement
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver import Remote
from appium_xueqiu.page.wrapper import handle_alert

class BasePage:
	logging.basicConfig(level=logging.INFO)
	_driver:WebDriver
	_params = dict()

	def __init__(self, driver:WebDriver = None):
		self._driver = driver

	def set_implicitly(self, time):
		self._driver.implicitly_wait(time)

	def wait_element_visible(self, time, locator):
		'''等待元素出现'''
		return WebDriverWait(self._driver, time).until(expected_conditions.visibility_of_element_located(locator))

	def finds(self, locator, value:str = None):
		elements: list
		if isinstance(locator, tuple):
			elements = self._driver.find_elements(*locator)
		else:
			elements = self._driver.find_elements(locator, value)
		return elements

	# @handle_alert
	def find(self,locator, value: str = None):
		element: WebElement
		if isinstance(locator, tuple):
			element = self._driver.find_element(*locator)
		else:
			element = self._driver.find_element(locator, value)
		return element

	def steps(self, path):
		''' 解析 yaml 文件，yaml格式：
		方法：
			- 定位方式
			  定位元素
			  操作方式
		:param path: yaml 文件路径
		:param func: 待执行的函数名： func = inspect.stack()[1].function 通过过这个方式获取
		:return:
		'''
		with open(path, encoding='utf-8') as fr:
			logging.info(f'1、读取 {path} 文件')
			func = inspect.stack()[1].function # [0] -> steps, [1] -> 调用steps的方法名
			logging.info(f'正在获取方法 【{func}】 的yaml数据')
			steps = yaml.safe_load(fr)[func]
			logging.info('2、解析文件')

			# 将外部参数传入yaml文件，将 dict 转成 str, 用 str的replace方法，将 参数 替换成值，再转回dict
			# if self._params:
			steps_str = json.dumps(steps)
			for key, value in self._params.items():
				steps_str = steps_str.replace('${' + key + '}', value)
			steps = json.loads(steps_str)

			for step in steps:
				if "action" in step.keys():
					logging.info('3、多个动作解析')
					action = step['action']
					if action == 'click':
						logging.info('3.1、click 点击操作')
						self.find(step['by'], step['locator']).click()
					if action == 'send':
						logging.info('3.2、输入操作')
						if 'value' in step.keys():
							value = step['value']
							self.find(step['by'], step['locator']).send_keys(value)
						else:
							logging.info(f'文件[{path}]中定位元素[{step["locator"]}]缺少输入值 value')
					if action == 'len > 1':
						logging.info('3.3 验证操作')
						eles = self.finds(step['by'], step['locator'])
						logging.info('len(eles) > 0: ', len(eles) > 0)
						return len(eles) > 0

	'''---------------------------------------------'''
	''' 模拟键盘操作                                  '''
	'''---------------------------------------------'''
	def enter(self):
		'''回车，KEYCODE_ENTER -> 66'''
		self._driver.keyevent = 66


import json

import torch
from torch import nn, optim
from torch.nn.parameter import Parameter

class InsensitiveLoss(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, y_pred, y, w):
        diff = torch.abs(y_pred - y)
        diff[diff < 0.5] = 0.0
        return (diff * w).sum() / w.sum()

class Prot1(nn.Module):
    def __init__(self):
        super().__init__()
        self.c = Parameter(torch.tensor(1.0, dtype=torch.float64), requires_grad=True)

    def forward(self, x):
        max_score, max_pods, max_time, score_per_pod, ms, pr, pods_collected = torch.tensor_split(x, 7, dim=1)
        return (pods_collected * pr * torch.exp(ms - 1) + self.c) / max_pods - .5

def get_data():
    with open('data/merged_scores.json') as r:
        score_dict = json.load(r)

    with open('data/params.json') as r:
        params = json.load(r)

    sym_data = []
    sym_target = []
    sym_weights = []

    mss = {o['iz_name']: o['MS Multiplier'] for o in params}
    prs = {o['iz_name']: o['PR Multiplier'] for o in params}

    for k, score_info in score_dict.items():
        objs = [v for v in score_info['scores'] if 'fm' in v]
        max_score = score_info['max_score']
        max_pods = score_info['max_pods']
        max_time = score_info['max_time']

        sym_data.append([
            float(max_score),
            float(max_pods),
            float(max_time),
            max_score / max_pods,
            mss[k],
            prs[k],
            0.0,
        ])
        sym_target.append(0.0)
        sym_weights.append(1.0)

        for o in objs:
            sym_data.append([
                float(max_score),
                float(max_pods),
                float(max_time),
                max_score / max_pods,
                mss[k],
                prs[k],
                float(o['pods']),
            ])
            sym_target.append(float(o['fm']))
            sym_weights.append(1.0)

    sym_data = torch.DoubleTensor(sym_data)
    sym_target = torch.DoubleTensor(sym_target)
    sym_weights = torch.DoubleTensor(sym_weights)

    return sym_data, sym_target, sym_weights

def main():
    model = Prot1()

    sym_data, sym_target, sym_weights = get_data()

    optimizer = optim.Adam(model.parameters(), lr=1e-3)
    sched = optim.lr_scheduler.StepLR(optimizer, step_size=7000, gamma=0.01)
    crit = InsensitiveLoss()

    model.eval()
    with torch.no_grad():
        optimizer.zero_grad()

        y_pred = model(sym_data).squeeze(-1).detach().cpu()
        y = sym_target.detach().cpu()
        w = sym_weights.detach().cpu()

    print('Prev Loss:', crit(y_pred, y, w).item())

    model.train()
    for epoch in range(10000):
        optimizer.zero_grad()

        pred = model(sym_data).squeeze(-1)
        loss = crit(pred, sym_target, sym_weights)

        print(f'Epoch: {epoch + 1:6d} Loss {loss.detach().cpu().item():.7f}', end='\r')

        loss.backward()
        optimizer.step()
        sched.step()

    model.eval()
    with torch.no_grad():
        optimizer.zero_grad()

        y_pred = model(sym_data).squeeze(-1).detach().cpu()
        y = sym_target.detach().cpu()
        w = sym_weights.detach().cpu()

    print()
    print([abs(a - b) for a, b in zip(y_pred.numpy(), y.numpy()) if abs(round(a - b)) > 0])
    print('Errors', sum([abs(round(a - b)) > 0 for a, b in zip(y_pred.numpy(), y.numpy())]), 'out of', y_pred.shape[0])
    print('Loss:', crit(y_pred, y, w).item())
    print('Coef:', model.c.item())

if __name__ == '__main__':
    main()


from functools import wraps
import importlib
from json import dumps, loads
import os
from platform import architecture, release
from pprint import pformat as pf
from subprocess import Popen, PIPE
import sys
from time import time_ns
from typing import Any, Callable, Dict, List

from typeguard import typechecked
from wenv import EnvConfig, PythonVersion

from .cmd import run_cmd
from .const import ARCHITECTURE, ARCHS, CONVENTIONS, PLATFORM, PYTHONBUILDS_FN
from .names import get_benchmark_fld
from .pythonversion import read_python_builds

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ROUTINES
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

@typechecked
def _permutations(fn: str):
    """
    Provides all permutations on calling convention, arch, ctypes/Python version

    Args:
        - fn: File name of Python source file
    Yields:
        Tuple containing arch, convention, ctypes, dll handle
    """

    from .ctypes import CTYPES, get_dll_handle

    fn = os.path.basename(fn)
    fld = get_benchmark_fld()

    for convention in CONVENTIONS:
        for arch in ARCHS:
            for ctypes_build in CTYPES.get(arch, tuple()):
                if PLATFORM == "unix" or arch[3:] == ARCHITECTURE:
                    yield (
                        arch,
                        convention,
                        ctypes_build,
                        get_dll_handle(
                            arch,
                            ctypes_build,
                            convention,
                            fld,
                            fn,
                        ),
                    )

@typechecked
def benchmark(fn: str, initializer: Callable) -> Any:
    """
    Decorator for benchmark functions

    Args:
        - fn: File name of Python source file
    Yields:
        DLL handles per calling convention, architecture and wenv Python version
    """

    def outer(func: Callable) -> Callable:

        @wraps(func)
        def inner() -> List[Dict]:

            reports = []

            for arch, convention, ctypes, dll_handle in _permutations(fn):

                print(f'Benchmark ...')

                func_handle = initializer(
                    ctypes = ctypes,
                    dll_handle = dll_handle,
                    conv = convention,
                )
                min_runtime = None
                benchmark_start = time_ns()
                counter = 0

                while 1_000_000_000 > time_ns() - benchmark_start:  # at least one sec

                    iteration_start = time_ns()

                    func(ctypes, func_handle)

                    runtime = time_ns() - iteration_start
                    if min_runtime is None or min_runtime > runtime:
                        min_runtime = runtime

                    counter += 1

                server = None
                if PLATFORM == 'unix':
                    server = '.'.join(str(ctypes.zb_get_parameter('pythonversion')).split('.')[:3])

                report = dict(
                    platform = PLATFORM,
                    arch = arch,
                    convention = convention,
                    name = func.__name__,
                    runtime = min_runtime,
                    runs = counter,
                    server = server,
                    client = sys.version.split(' ')[0],
                )
                reports.append(report)

                print(pf(report))

            return reports

        inner.is_benchmark = None
        return inner

    return outer

@typechecked
def _get_benchmarks() -> List[Callable]:
    """
    auto-detects benchmarks
    """

    benchmarks = []

    for item in os.listdir('benchmark'):

        if item.startswith("_") or not item.lower().endswith(".py"):
            continue

        try:
            mod = importlib.import_module(f"benchmark.{item[:-3]:s}")
        except ModuleNotFoundError:  # likely no gui support
            continue

        for attr in dir(mod):
            entry = getattr(mod, attr)
            if not hasattr(entry, 'is_benchmark'):
                continue
            benchmarks.append(entry)

    return benchmarks

@typechecked
def _group_data(data: List) -> Dict:
    """
    Group parsed raw data by benchmark, arch, version and calling convention
    """

    by_benchmark = {}
    for entry in data:
        name = entry['name']
        try:
            group = by_benchmark[name]
        except KeyError:
            group = []
            by_benchmark[name] = group
        group.append(entry)

    by_archverconv = {name: {} for name in by_benchmark.keys()}
    for name, group in by_benchmark.items():
        for entry in group:
            archver = (
                entry['server'] if entry['server'] is not None else entry['client'],
                entry['arch'],
                entry['convention']
            )
            try:
                subgroup = by_archverconv[name][archver]
            except KeyError:
                subgroup = []
                by_archverconv[name][archver] = subgroup
            subgroup.append(entry)

    return by_archverconv

@typechecked
def _make_table(name: str, group: Dict, doc: str):
    """
    Write on RST table from parsed and grouped data
    """

    keys = sorted(
        group.keys(),
        key = (lambda item: (PythonVersion.from_config(item[1], item[0]), item[1], item[2])),
    )

    with open(os.path.join('docs', 'source', f'benchmark_{name}.rst'), mode = 'w', encoding="utf-8") as f:

        f.write(f'.. csv-table:: "{name:s}" benchmark, CPython {sys.version.split(" ")[0]:s} on {sys.platform:s}, versions of CPython on Wine\n')
        f.write('    :header: "version", "arch", "convention", "ctypes [µs]", "zugbruecke [µs]", "overhead [µs]"\n')
        f.write('    :delim: 0x0003B\n')
        f.write('\n')

        for version, arch, conv in keys:
            unix, wine = group[(version, arch, conv)]
            if unix['server'] is None:
                unix, wine = wine, unix
            unix, wine = round(unix['runtime'] / 1e3, 1), round(wine['runtime'] / 1e3, 1)
            f.write(f'    "{version:s}"; "{arch:s}"; "{conv:s}"; {wine:,.01f}; {unix:,.01f}; {unix-wine:,.01f}\n')

        f.write('\n')
        for line in doc.split('\n'):
            f.write(f'{line[4:]:s}\n')

@typechecked
def _make_inventory(names: List[str]):

    with open(os.path.join('docs', 'source', 'benchmarks_all.rst'), mode = 'w', encoding="utf-8") as f:
        for name in names:
            f.write(f'.. include:: benchmark_{name:s}.rst\n')

def _make_sysinfo():

    with open('/proc/cpuinfo', mode='r', encoding='utf-8') as f:
        cpuinfo = f.read()
    cpuinfo = [line for line in cpuinfo.split('\n') if 'model name' in line][0].split(':')[1].strip()

    proc = Popen(['wine', '--version'], stdout = PIPE)
    wine, _ = proc.communicate()
    wine = wine.decode('utf-8').strip().split('-')[1]

    with open(os.path.join('docs', 'source', 'benchmarks_sysinfo.rst'), mode = 'w', encoding="utf-8") as f:
        f.write('Benchmarks were performed on an ')
        f.write(f'"{cpuinfo:s}" CPU, ')
        f.write(f'Linux {release():s} {architecture()[0]}, ')
        f.write(f'and Wine {wine:s}')
        f.write('.\n')

def _make_tables():
    """
    Write RST table from raw benchmark data
    """

    benchmarks = {
        entry.__name__: entry.__doc__
        for entry in _get_benchmarks()
    }

    data = []
    with open(os.path.join('benchmark', 'data.raw'), mode = 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if len(line) == 0:
                continue
            data.extend(loads(line))

    for name, group in _group_data(data).items():
        _make_table(name, group, benchmarks[name])

    _make_inventory(list(benchmarks.keys()))
    _make_sysinfo()

def _run_wenv():
    """
    Run benchmarks on Wine from Unix
    """

    cfg = EnvConfig()
    builds = read_python_builds(fn = os.path.join(cfg['prefix'], PYTHONBUILDS_FN))

    for arch, _builds in builds.items():
        for build in _builds:
            run_cmd(
                cmd = ['make', '_clean_py'],
            )
            run_cmd(
                cmd = [
                    'wenv', 'python', '-m', 'tests.lib.benchmark'
                ],
                env = {
                    'WENV_DEBUG': '0',
                    'WENV_ARCH': arch,
                    'WENV_PYTHONVERSION': str(build),
                    'WENV_NO_PTH_FILE': 'true' if build.minor >= 11 else 'false',  # CPython PR #31542
                },
            )
    run_cmd(
        cmd = ['make', '_clean_py'],
    )

def main():
    """
    Run all benchmarks both on Unix and Wine
    """

    if len(sys.argv) > 1 and sys.argv[1] == 'wine':
        _run_wenv()
        return
    if len(sys.argv) > 1 and sys.argv[1] == 'table':
        _make_tables()
        return
    if len(sys.argv) > 1 and sys.argv[1] != 'unix':
        raise SystemError(f'unknown parameter "{sys.argv[1]:s}"')

    fn = os.path.join('benchmark', 'data.raw')

    if not os.path.exists(fn):
        with open(fn, mode = 'w', encoding='utf-8') as f:
            f.write("")

    for item in _get_benchmarks():
        reports = item()
        with open(fn, mode = "a", encoding='utf-8') as f:
            f.write(f'{dumps(reports):s}\n')

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# MODULE ENTRY POINT
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if __name__ == "__main__":

    main()


import json
import os
import hickle as hkl
import numpy as np

oneOutput = False

for jsonFileName in os.listdir('input'):
    name = jsonFileName.split(".")[0]

    os.chdir('input')
    f = open(jsonFileName, mode="r", encoding="utf-8")

    data = json.load(f)
    f.close()
    os.chdir('../')

    messagesPerId = {}

    for i in data["messages"]:
        content = i["content"].encode()
        userId = i["author"]["id"]
        isBot = i["author"]["isBot"]

        if not isBot and content:
            if userId in messagesPerId:
                messagesPerId[userId].append(content)
            else:
                messagesPerId[userId] = [content]

    os.chdir('output')

    if oneOutput:
        npMessages = np.array(messagesPerId)
        hkl.dump(npMessages, name + "_useless" + ".hkl", mode='w', compression='gzip')

        #data = hkl.load("output.hkl")
        #print(data)

    else:
        for id, messages in messagesPerId.items():
            npMessages = np.array(messages)
            fileName = id + ".hkl"

            hkl.dump(npMessages, fileName, mode='w', compression='gzip')

            #data = hkl.load(fileName)
            #print(data)

    os.chdir('../')


import altair as alt
import numpy as np

import loudspeakerexplorer as lsx

def make_chart(
        data,
        process_before=lambda chart: chart,
        process_after=lambda chart: chart,
        *kargs, **kwargs):
    # Semantically equivalent to:
    #   base = alt.Chart(data.reset_index(), *kargs, **kwargs)
    #   process_before(process_after(base))
    #
    # Improves readability by making it possible to write the top layer
    # processing code *before* the sublayers, which matches the order in which
    # Vega processes the spec. This is especially important when it comes to
    # the order in which transforms run.
    #
    # Optimizes for minimum Vega spec size by packing unique index values
    # together and rounding long floating point numbers.

    data = (data
            # Round numbers like 0.999999999999 to prevent them from
            # unnecessarily increasing spec size.
            .apply(lambda column: np.around(column, 3), raw=True)
            .pipe(lsx.pd.implode))
    return lsx.util.pipe(
        alt.Chart(data.reset_index(), *kargs, **kwargs),
        process_after,
        lambda chart: chart.transform_flatten(data.columns.values),
        process_before)

def encode_selection(chart, selection, channel_type, selected, unselected):
    return (chart
            .add_selection(selection)
            .encode(**{channel_type: alt.condition(selection, selected, unselected)}))

def filter_selection(chart, selection):
    return (chart
            .add_selection(selection)
            .transform_filter(selection))

def highlight_mouseover(chart, fields=None):
    additional_args = {}
    if fields is not None:
        additional_args['fields'] = fields
    return lsx.alt.encode_selection(
        chart,
        alt.selection_single(
            on='mouseover', clear='mouseout', **additional_args),
        'fillOpacity', alt.value(1), alt.value(0.2))

def interactive_line(
        chart,
        add_mark=lambda chart: chart.mark_line(clip=True, interpolate='monotone')):
    # Note that the channels should explicitly override the legend symbolType to
    #  'stroke', otherwise it gets set to 'circle' from the hidden layer, which
    # is wrong. A clear way to avoid this problem would be to make the legends
    # independent and disable the legend on the hidden layer, but that causes
    # problems with faceted charts, see:
    #   https://github.com/vega/vega-lite/issues/6261

    # This is equivalent to using the `point` line mark property.
    # The reason why we don't simply do that is because tooltips wouldn't work
    # as well due to this Vega-lite bug:
    #   https://github.com/vega/vega-lite/issues/6107
    return alt.layer(
        # Note: order is important. If the points chart comes first, legend selection doesn't work.
        lsx.util.pipe(
            chart,
            add_mark,
            lambda chart: encode_selection(
                chart, alt.selection_multi(encodings=['color'], bind='legend'),
                'opacity', alt.value(1), alt.value(0.2))),
        lsx.util.pipe(
            chart
            .mark_circle(clip=True, size=100),
            # We don't use legend_selection for points. If we do, it seems to
            # break legend interactivity in weird ways on non-faceted charts.
            lambda chart: encode_selection(chart, alt.selection_single(
                on='mouseover', clear='mouseout', empty='none',
                # We explicitly specify the encodings, as the defaults might not take
                # new fields added by transforms into account. See:
                #   https://github.com/vega/vega-lite/issues/6389
                encodings=['x', 'y']),
                'fillOpacity', alt.value(0.3), alt.value(0))))


import community
from infomap import infomap
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

color_keys = list(mcolors.TABLEAU_COLORS.keys())

file_in = './bin/edges12.txt'
edges = []
with open(file_in) as f:
    for line in f:
        n1, n2, w = line.split(' ')
        edges.append((int(n1), int(n2), float(w)))
        # edges.append((int(n2), int(n1), float(w)))

G = nx.Graph()
G.add_weighted_edges_from(edges)
# G.add_edge(1, 2, weight=1)

# nx.layou

#first compute the best partition
# louvain
partition = community.best_partition(G, resolution=0.2)

# dendogram = community.generate_dendrogram(G, resolution=0.2)
# partition = community.partition_at_level(dendogram, 1)

#drawing
size = float(len(set(partition.values())))
pos = nx.kamada_kawai_layout(G)

# infomap
infomapWrapper = infomap.Infomap("--two-level")

print("Building Infomap network from a NetworkX graph...")
for e in G.edges():
    infomapWrapper.addLink(*e)

print("Find communities with Infomap...")
infomapWrapper.run();

tree = infomapWrapper.tree

print("Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength()))

partition = {}
for node in tree.leafIter():
    partition[node] = node.moduleIndex()

count = 0.
for com in set(partition.values()) :
    count = count + 1.
    list_nodes = [nodes for nodes in partition.keys()
                                if partition[nodes] == com]
    nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 60,
                                node_color = mcolors.TABLEAU_COLORS[color_keys[int(count) % len(color_keys)]])

nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.show()


from django.contrib import admin
from django.urls import path, include
from core.views import *
#from ..core.views import *

urlpatterns = [
    path('', Index.as_view(), name='index'),

    path('dashboard/', Dashboard.as_view(), name='dashboard'),

    path('login/', Login.as_view(), name='login'),

    path('hello/', Hello.as_view(), name='hello'),
    path('hello/<str:clazz>/<int:oid>', Hello.as_view(), name='hello'),

    path('graph/', Graph.as_view(), name='graph'),
    path('graph/<str:clazz>/<int:oid>', Graph.as_view(), name='graph'),

    path('stock/', StockView.as_view(), name='stock'),
    path('stock/refresh', StockRefresh.as_view(), name='stock-refresh'),
    path('stock/add/', StockCreate.as_view(), name='stock-add'),
    path('stock/<int:pk>/', StockUpdate.as_view(), name='stock-update'),
    path('stock/<int:pk>/delete/', StockDelete.as_view(), name='stock-delete'),

    path('employee/', EmployeeView.as_view(), name='employee'),
    path('employee/add/', EmployeeCreate.as_view(), name='employee-add'),
    path('employee/<int:pk>/', EmployeeUpdate.as_view(), name='employee-update'),
    path('employee/<int:pk>/delete/', EmployeeDelete.as_view(), name='employee-delete'),

    path('get_pk/<str:clazz>/<str:name>/', get_pk, name='get_pk'),

    path('scraper/', Scraper.as_view(), name='scraper'),

    path('admin/', admin.site.urls, name='admin'),
    path('api/', include('api.urls'), name='api'),
    path('live/', include('live.urls'), name='live'),

    path("async/", async_view),
    path("sync/", sync_view),
]


# 4. UCI is one of the most common places to get data sets for data science and machine learning.
#   Read the content of UCL (https://archive.ics.uci.edu/ml/datasets.php).
#   Without additional libraries it will be difficult, so you may try it with BeautifulSoup4

import requests
from bs4 import BeautifulSoup

url = 'https://archive.ics.uci.edu/ml/datasets.php'
page = requests.get(url)

soup = BeautifulSoup(page.content, 'html.parser')

table = soup.find('table', {'cellpadding': '5'})
rows = table.findAll('tr')

data = []
for row in rows:
    cols = row.findAll('td')
    cols = [col.text.strip() for col in cols]
    data.append(cols)

print(data[:2])


from functools import lru_cache
from typing import List
import os

from pytezos.rpc.node import RpcQuery
from pytezos.rpc.block import Block, BlockListList
from pytezos.rpc.operation import Operation

class OperationsDict(RpcQuery):

    def _get_operations_list(self, key, kind=None):
        operations = self.get(key)

        if kind:
            if isinstance(kind, str):
                kind = {kind}
            elif isinstance(kind, list):
                kind = set(kind)

            operations = filter(
                lambda op: any(map(
                    lambda x: x['kind'] in kind, op['contents'])), operations)

        return list(map(
            lambda x: Operation(data=x, node=self._node, **self._kwargs),
            operations
        ))

    def applied(self, kind=None) -> List[Operation]:
        """
        :param kind: endorsement, seed_nonce_revelation, double_endorsement_evidence, double_baking_evidence,
        activate_account, proposals, ballot, reveal, transaction, origination, delegation
        :return:
        """
        return self._get_operations_list('applied', kind=kind)

    def refused(self) -> List[Operation]:
        return self._get_operations_list('refused')

    def branch_delayed(self) -> List[Operation]:
        return self._get_operations_list('branch_delayed')

    def unprocessed(self) -> List[Operation]:
        return self._get_operations_list('unprocessed')

class Mempool(RpcQuery):

    def __init__(self, *args, **kwargs):
        super(Mempool, self).__init__(
            properties={'pending_operations': OperationsDict},
            *args, **kwargs)

    @property
    def filter(self, **kwargs):
        """
        minimal_fees, minimal_nanotez_per_gas_unit, minimal_nanotez_per_byte
        :return:
        """
        if kwargs:
            return self._node.post(f'{self._path}/filter', json=kwargs)
        return self._node.get(f'{self._path}/filter')

class Chain(RpcQuery):

    def __init__(self, *args, **kwargs):
        kwargs.update(
            chain_id=os.path.basename(kwargs.get('path', ''))
        )
        super(Chain, self).__init__(
            properties={'mempool': Mempool},
            *args, **kwargs)

    @property
    @lru_cache(maxsize=None)
    def blocks(self):
        return BlockListList(
            path=f'{self._path}/blocks',
            node=self._node,
            child_class=Block,
            properties=['head', 'genesis'],
            **self._kwargs
        )

    @property
    def head(self) -> Block:
        return self.blocks.head

    @property
    def genesis(self) -> Block:
        return self.blocks.genesis