# Copyright 2021 The SeqIO Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
r"""Dumps preprocessed tasks as TFRecord of tf.Examples.

Usage:
====================
t5_cache_tasks \
--tasks=my_task_*,your_task \
--excluded_tasks=my_task_5 \
--output_cache_dir=/path/to/cache_dir \
--module_import=my.tasks \
--alsologtostderr

"""

import importlib
import os
import re

from absl import app
from absl import flags
from absl import logging

import apache_beam as beam
import seqio
from seqio import beam_utils
import tensorflow.compat.v2 as tf

# Significantly speeds up preprocessing in tf1.
tf.compat.v1.enable_eager_execution()

FLAGS = flags.FLAGS

flags.DEFINE_list(
    "tasks", None,
    "Regexes matching task(s) to build a preprocessed dataset for. Will build "
    "all registered if not specified.")
flags.DEFINE_list(
    "excluded_tasks", None,
    "Regexes matching task(s) to skip.")
flags.DEFINE_string(
    "output_cache_dir", None,
    "The directory to output cached tasks to.")
flags.DEFINE_list(
    "tasks_additional_cache_dirs", [],
    "Additional directories to search for cached Tasks after checking the "
    "global caches and `output_cache_dir`.")
flags.DEFINE_multi_string(
    "module_import", [],
    "Modules to import. Use this, for example, to add new `Task`s to the "
    "global `TaskRegistry`.")
flags.DEFINE_list(
    "pipeline_options", ["--runner=DirectRunner"],
    "A comma-separated list of command line arguments to be used as options "
    "for the Beam Pipeline.")
flags.DEFINE_boolean(
    "overwrite", False,
    "If true, overwrite the cached task even if it exists in the cached "
    "directories.")

def _import_modules(modules):
  for module in modules:
    if module:
      importlib.import_module(module)

def run_pipeline(pipeline,
                 task_names,
                 cache_dir,
                 excluded_tasks=None,
                 modules_to_import=(),
                 overwrite=False,
                 completed_file_contents=""):
  """Run preprocess pipeline."""
  output_dirs = []
  # Includes all names by default.
  included_regex = re.compile(r"(%s\Z)" % r"\Z|".join(task_names or [".*"]))
  # Excludes only empty names by default.
  excluded_regex = re.compile(r"(%s\Z)" % r"\Z|".join(excluded_tasks or []))
  task_names = [
      t for t in seqio.TaskRegistry.names()
      if included_regex.match(t) and not excluded_regex.match(t)]
  for task_name in task_names:
    task = seqio.TaskRegistry.get(task_name)
    if not task.supports_caching:
      logging.info(
          "Skipping task that does not support caching: '%s'", task.name)
      continue

    task_cache_dir = task.cache_dir
    output_dir = os.path.join(
        cache_dir, seqio.get_task_dir_from_name(task.name))

    if task_cache_dir and not overwrite:
      logging.info("Skipping task '%s', which exists in cache dir: %s",
                   task.name, task_cache_dir)
      continue

    if task_cache_dir and overwrite:
      if task_cache_dir == output_dir:
        # We were asked to overwrite the data, and the given directory that we
        # should generate the data in already has the data, then delete it.
        logging.warning(
            "Overwriting already cached data for task '%s' in cache_dir %s",
            task.name, output_dir)
        tf.io.gfile.rmtree(output_dir)
      else:
        # Cannot overwrite, since cache_dir isn't same as task.cache_dir.
        logging.warning("Not overwriting data in task.cache_dir since it is "
                        "different from cache_dir - %s vs %s", task.cache_dir,
                        output_dir)
        continue

    if not task.splits:
      logging.warning("Skipping task '%s' with no splits.", task.name)
      continue

    # Log this task to the terminal.
    print("Caching task '%s' with splits: %s" % (task.name, task.splits))

    output_dirs.append(output_dir)
    completion_values = []

    if isinstance(task.source, seqio.FunctionDataSource):
      logging.warning(
          "Task '%s' using FunctionDataSource cannot be distributed. If your "
          "dataset is large, you may be able to speed up preprocessing by "
          "sharding it and using a TfdsSource, TFExampleSource, or "
          "TextLineSource instead.", task.name)

    for split in task.splits:
      label = "%s_%s" % (task.name, split)

      pat = beam_utils.PreprocessTask(
          task, split, modules_to_import=modules_to_import)
      num_shards = len(pat.shards)
      examples = pipeline | "%s_pat" % label >> pat
      completion_values.append(
          examples
          | "%s_write_tfrecord" % label >> beam_utils.WriteExampleTfRecord(
              seqio.get_cached_tfrecord_prefix(output_dir, split),
              num_shards=num_shards))
      completion_values.append(
          examples
          | "%s_info" % label >> beam_utils.GetInfo(num_shards)
          | "%s_write_info" % label >> beam_utils.WriteJson(
              seqio.get_cached_info_path(output_dir, split)))
      completion_values.append(
          examples
          | "%s_stats" % label >> beam_utils.GetStats(task.output_features)
          | "%s_write_stats" % label >> beam_utils.WriteJson(
              seqio.get_cached_stats_path(output_dir, split)))

    # After all splits for this task have completed, write COMPLETED files to
    # the task's output directory.
    _ = (completion_values
         | "%s_flatten_completion_values" % task.name >> beam.Flatten()
         | "%s_discard_completion_values" % task.name >> beam.Filter(
             lambda _: False)
         | "%s_write_completed_file" % task.name >> beam.io.textio.WriteToText(
             os.path.join(output_dir, "COMPLETED"),
             append_trailing_newlines=False, num_shards=1,
             shard_name_template="", header=completed_file_contents))

  return output_dirs

def main(_):
  flags.mark_flags_as_required(["output_cache_dir"])

  _import_modules(FLAGS.module_import)

  seqio.add_global_cache_dirs(
      [FLAGS.output_cache_dir] + FLAGS.tasks_additional_cache_dirs)

  pipeline_options = beam.options.pipeline_options.PipelineOptions(
      FLAGS.pipeline_options)
  with beam.Pipeline(options=pipeline_options) as pipeline:
    tf.io.gfile.makedirs(FLAGS.output_cache_dir)
    unused_output_dirs = run_pipeline(
        pipeline,
        FLAGS.tasks,
        FLAGS.output_cache_dir,
        FLAGS.excluded_tasks,
        FLAGS.module_import,
        FLAGS.overwrite,
    )

def console_entry_point():
  app.run(main)

if __name__ == "__main__":
  console_entry_point()


# importing various libraries
import sys
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication, QPushButton, QVBoxLayout, QComboBox , QHBoxLayout,QFileDialog
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import random
import numpy as np

# main window
# which inherits QDialog
class DOS1(QDialog):

	# constructor
    def __init__(self, parent=None):
        super(DOS1, self).__init__(parent)

		# a figure instance to plot on
        self.figure = plt.figure()

		# this is the Canvas Widget that
		# displays the 'figure'it takes the
		# 'figure' instance as a parameter to __init__
        self.canvas = FigureCanvas(self.figure)

		# this is the Navigation widget
		# it takes the Canvas widget and a parent
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.buttondata=QPushButton('Open data')

		# Just some button connected to 'plot' method
        self.button = QPushButton('Plot')

		# adding action to the button
        self.button.clicked.connect(self.plot)
        self.buttondata.clicked.connect(self.open_dialog_box)
        self.combox = QComboBox()
        self.comboy = QComboBox()
        self.datapath = "None"
        self.headlines = []

   		# creating a Vertical Box layout
        layout = QVBoxLayout()
		# adding tool bar to the layout
        layout.addWidget(self.toolbar)
		# adding canvas to the layout
        layout.addWidget(self.canvas)

		# adding push button to the layout
        horizlayout=QHBoxLayout()
        horizlayout.addWidget(self.buttondata)
        horizlayout.addWidget(self.combox)
        horizlayout.addWidget(self.comboy)

        layout.addLayout(horizlayout)
        layout.addWidget(self.button)
        # setting layout to the main window
        self.setLayout(layout)

    # action called by thte push button

    def open_dialog_box(self):
        filename = QFileDialog.getOpenFileName()
        path = filename[0]
        directad = path.split('/')[:-1]
        fname=path.split('/')[-1]

        basename=fname.split(".")[0]
        print(path)
        print("your data file is ", fname)

        f=open(path, "r")
        firstline=f.readline()
        headers=firstline.split()[1:]
        f.close()

        self.datapath = path
        list1=headers
        self.combox.addItems(list1)
        self.comboy.addItems(list1)
        for val in list1:
        	self.headlines.append(val)
        print(self.headlines)

    def plot(self):

        xstr = self.combox.currentText()
        ystr = self.comboy.currentText()

        xind = self.headlines.index(xstr)
        yind = self.headlines.index(ystr)
        data = np.loadtxt(self.datapath)
        # clearing old figure
        self.figure.clear()
# create an axis
        ax = self.figure.add_subplot(111)

        # plot data
        ax.set_xlim(min(data[:,xind]), 1.1*max(data[:,xind]))
        ax.set_ylim(min(data[:,yind]), 1.1*max(data[:,yind]))
        ax.set_xlabel(xstr, fontsize=14)
        ax.set_ylabel(ystr, fontsize=12)
        ax.plot(data[:,xind], data[:,yind], '-')
        plt.tight_layout()
        plt.savefig("test00.png",bbox_inches='tight', dpi=300)

		# refresh canvas
        self.canvas.draw()

# driver code
if __name__ == '__main__':

	# creating apyqt5 application
	app = QApplication(sys.argv)
	# creating a window object
	win1 = DOS1()
	# showing the window
	win1.show()
	# loop
	app.exec_()


"""
连接 redis-cli -h 主机 -p 端口
切换数据库 select num

string :
增 set/mset/append key value
删 del key
改 set key
查 get/mget key

list :
增 lpush/rpush key value 或 linsert key before/after value new-value
删 lrem key count value
改 lset key index value
查 lrange key index0 index1

set:
增 sadd key value
删 srem key value
查 smembers key

zset:
增 zadd key score value
删 zrem key value
查 zrange key index0 index1

hash:
增/改 hset/hmset obj key value
删 hdel obj key
查 hget/hmget obj key 或 hkeys key / hvals key

键:
查找键： keys pattern
判断键是否存在： exists key
查看值的类型： type key
删除键值对： del key1

"""

import redis
import pickle

class AA(object):
    name = "meng"

redis_cli = redis.StrictRedis(host="192.168.49.154", port=6379, db=0)
redis_cli.lpush("test", pickle.dumps(AA))
vv = pickle.loads(redis_cli.rpop("test"))
print(vv.name)


import time
import copy
import numpy as np
import math
import logging

import torch
import torchvision
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random
import numpy as np
import matplotlib.pyplot as plt
import os
from tqdm import tqdm
import argparse
from models import losses
from models.en_estimater_debug import Model
from data.ICVL_loader_v2 import ICVL_Loader
from util.visualizer import Visualizer

from numba import cuda

parser = argparse.ArgumentParser()
parser.add_argument('--gpu_id', type=int, default=0, help='gpu id: e.g. 0, 1. -1 is no GPU')

parser.add_argument('--dataroot', default='../data/ICVL/process_output/',help='path to images & laser point clouds')
#parser.add_argument('--dataroot', default='/mnt/data/Chenyujin/Human_analysis/data/ICVL/process_output/',help='path to images & laser point clouds')
# self.parser.add_argument('--classes', type=int, default=50, help='ModelNet40 or ModelNet10')
parser.add_argument('--name', type=str, default='train',
                         help='name of the experiment. It decides where to store samples and models')
parser.add_argument('--trainlist', type=str, default='train_list_1.00_0.50.txt', help='')
parser.add_argument('--testlist', type=str, default='test_list.txt', help='')
parser.add_argument('--train_label_ratio', type=float, default=0.25, help='ratio of labeled frames used for training in trainlist')
parser.add_argument('--batch_size', type=int, default=24, help='input batch size')

parser.add_argument('--augment', type=str, default='no', help='whether do augmentation: yes | no')
parser.add_argument('--es_version', type=str, default='v2.1',
                    help='version of estimation: PoseEstimater(v3) PoseEstimater1(v2.1) PoseEstimater2(v4) PoseEstimater3(v5) PoseEstimater4(v6)')
parser.add_argument('--es_loss_version', type=str, default='loss',
                    help='version of estimation loss: LossEstimation(loss) LossEstimation_nyu_sf(nyu) LossEstimation_msra_sf(msra) LossEstimation_icvl_sf(icvl)')

parser.add_argument('--checkpoints_dir', type=str, default='./checkpoints/encoder_estimater/semi', help='models are saved here')
# self.parser.add_argument('--train_record_dir', type=str, default='/home/cyj/Human_analysis/CYJ_HandNet/Hand_SO-Net/MSRA_handpose/checkpoints/train_record.log', help='record train information')

parser.add_argument('--input_pc_num', type=int, default=1024, help='# of input points')
parser.add_argument('--surface_normal', type=bool, default=True, help='use surface normal in the pc input')
parser.add_argument('--nThreads', default=8, type=int, help='# threads for loading data')

parser.add_argument('--display_winsize', type=int, default=256, help='display window size')
parser.add_argument('--display_id', type=int, default=200, help='window id of the web display')

parser.add_argument('--feature_num', type=int, default=1024, help='length of encoded feature')
parser.add_argument('--activation', type=str, default='relu', help='activation function: relu, elu')
parser.add_argument('--normalization', type=str, default='batch', help='normalization function: batch, instance')

# self.parser.add_argument('--nepoch', type=int, default=100, help='number of epochs to train for')1e-5
parser.add_argument('--lr_encoder', type=float, default=1e-5, help='encoder learning rate')
parser.add_argument('--lr_estimater', type=float, default=1e-5, help='estimater learning rate')
parser.add_argument('--lr_decoder', type=float, default=0, help='decoder learning rate')
parser.add_argument('--dropout', type=float, default=0.6, help='probability of an element to be zeroed')
parser.add_argument('--node_num', type=int, default=64, help='som node number')
parser.add_argument('--k', type=int, default=3, help='k nearest neighbor')
# '/ssd/open-source/so-net-full/autoencoder/checkpoints/save/shapenetpart/183_0.034180_net_encoder.pth'
# self.parser.add_argument('--pretrain', type=str, default=None, help='pre-trained encoder dict path: None or str')
# self.parser.add_argument('--pretrain', type=str, default='checkpoints', help='pre-trained encoder dict path: None or str')
# self.parser.add_argument('--pretrain_encoder', type=str, default='checkpoints/150_0.009468_net_encoder.pth', help='pre-trained encoder dict path: None or str')
parser.add_argument('--pretrain_encoder', type=str, default=None,
                         help='pre-trained encoder dict path: None or str')
parser.add_argument('--pretrain_decoder', type=str, default=None,
                         help='pre-trained decoder dict path: None or str')
parser.add_argument('--pretrain_estimater', type=str, default=None,
                         help='pre-trained estimater dict path: None or str')
# self.parser.add_argument('--pretrain_lr_ratio', type=float, default=1, help='learning rate ratio between pretrained encoder and classifier')

parser.add_argument('--som_k', type=int, default=9, help='k nearest neighbor of SOM nodes searching on SOM nodes')
parser.add_argument('--som_k_type', type=str, default='avg', help='avg / center')

parser.add_argument('--random_pc_dropout_lower_limit', type=float, default=1, help='keep ratio lower limit')
parser.add_argument('--bn_momentum', type=float, default=0.1,
                         help='normalization momentum, typically 0.1. Equal to (1-m) in TF')
parser.add_argument('--bn_momentum_decay_step', type=int, default=None,
                         help='BN momentum decay step. e.g, 0.5->0.01.')
parser.add_argument('--bn_momentum_decay', type=float, default=0.6, help='BN momentum decay step. e.g, 0.5->0.01.')

# self.parser.add_argument('--output_pc_num', type=int, default=1280, help='# of output points')
parser.add_argument('--output_fc_pc_num', type=int, default=256, help='# of fc decoder output points')
parser.add_argument('--output_conv_pc_num', type=int, default=1024, help='# of conv decoder output points')

parser.add_argument('--JOINT_NUM', type=int, default=16, help='number of joints')
parser.add_argument('--OUT_DIM', type=int, default=48, help='number of net output')
parser.add_argument('--size', type=str, default='full', help='how many samples do we load: small | full')
parser.add_argument('--INPUT_FEATURE_NUM', type=int, default=6, help='number of input point features')
parser.add_argument('--workers', type=int, default=0, help='number of data loading workers')
parser.add_argument('--test_index', type=int, default=0, help='test index for cross validation, range: 0~8')
#parser.add_argument('--PCA_SZ', type=int, default=42, help='number of PCA components')
parser.add_argument('--weight', type=float, default=100, help='weight of estimater, while weight of decoder is 1')

opt = parser.parse_args()
opt.device = torch.device("cuda:%d" % (opt.gpu_id) if torch.cuda.is_available() else "cpu")
cuda.select_device(opt.gpu_id)

if not os.path.exists(opt.checkpoints_dir):
    os.makedirs(opt.checkpoints_dir)

logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y/%m/%d %H:%M:%S', \
					filename=os.path.join(opt.checkpoints_dir, 'train.log'), level=logging.INFO)
logging.info('======================================================')
logging.info('weight: %f', opt.weight)
print(opt.pretrain_encoder,opt.pretrain_decoder,opt.pretrain_estimater)
logging.info('encoder=%s' %opt.pretrain_encoder)
logging.info('decoder=%s' %opt.pretrain_decoder)
logging.info('estimater=%s' %opt.pretrain_estimater)

if __name__=='__main__':
    #if not os.path.exists(opt.train_record_dir):
     #   os.system(r"touch{}".format(opt.train_record_dir))

    #load data
    trainset = ICVL_Loader(opt.dataroot, 'train', opt)
    dataset_size = len(trainset)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.nThreads)
    print('#training point clouds = %d' % len(trainset))

    testset = ICVL_Loader(opt.dataroot, 'test', opt)
    testloader = torch.utils.data.DataLoader(testset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.nThreads)
    print('#testing point clouds = %d' % len(testset))
    visualizer = Visualizer(opt)

    # create model, optionally load pre-trained model
    model = Model(opt)
    current_en_index=0
    current_es_index = 0

    n_slect=int(opt.batch_size*opt.train_label_ratio)

    if opt.pretrain_encoder is not None:
        model.encoder.load_state_dict(torch.load(opt.pretrain_encoder))
        current_en_index=int(opt.pretrain_encoder.split("/")[-1].split("_")[0])
    if opt.pretrain_estimater is not None:
        model.estimater.load_state_dict(torch.load(opt.pretrain_estimater))
        current_es_index = int(opt.pretrain_estimater.split("/")[-1].split("_")[0])

    for epoch in range(500):
        print('======>>>>> Online epoch: #%d <<<<<======' % (epoch + current_es_index + 1))
        epoch_iter = 0
        train_loss_es_batch = []
        train_wld_err_batch = []
        joints_err_list_train = [0.0]*opt.JOINT_NUM
        joints_err_list_test = [0.0]*opt.JOINT_NUM
        timer = time.time()
        for i, data in enumerate(tqdm(trainloader,0)):
            epoch_iter += opt.batch_size
            #input_pc, input_sn, input_node, input_node_knn_I, gt_pca, gt_xyz, valid, volume_length, volume_offset, volume_rotate, PCA_coeff, PCA_mean = data
            #model.set_input(input_pc, input_sn, gt_pca, input_node, input_node_knn_I)
            input_pc, input_sn, input_node, input_node_knn_I, gt_xyz, volume_length, volume_offset, volume_rotate = data

            #data use label
            input_pc1, input_sn1, input_node1, input_node_knn_I1, gt_xyz1, volume_length1, volume_offset1, volume_rotate1 = input_pc[0:n_slect,:,:], input_sn[0:n_slect,:,:], input_node[0:n_slect,:,:], input_node_knn_I[0:n_slect,:,:], gt_xyz[0:n_slect,:,:], volume_length[0:n_slect,:], volume_offset[0:n_slect,:], volume_rotate[0:n_slect,:,:]
            #data don't use label
            input_pc2, input_sn2, input_node2, input_node_knn_I2, gt_xyz2, volume_length2, volume_offset2, volume_rotate2 = input_pc[n_slect:opt.batch_size,:,:], input_sn[n_slect:opt.batch_size,:,:], input_node[n_slect:opt.batch_size,:,:], input_node_knn_I[n_slect:opt.batch_size,:,:], gt_xyz[n_slect:opt.batch_size,:,:], volume_length[n_slect:opt.batch_size,:], volume_offset[n_slect:opt.batch_size,:], volume_rotate[n_slect:opt.batch_size,:,:]
            #if epoch_iter==opt.batch_size:
            #    print(volume_length1)
            #    print(volume_length2)
            #Don't need to Train data that don't use label

            #Train data use label
            model.set_input(input_pc1, input_sn1, gt_xyz1, input_node1, input_node_knn_I1)
            model.encoder_estimater_optimize()
            # computer error in world cs
            estimation_joins = model.get_estimation()
            estimation_joins = estimation_joins.view(estimation_joins.size(0), 1,
                                                         estimation_joins.size(1))  # [ ,1,42]
            outputs_xyz = estimation_joins.cpu().view(gt_xyz1.shape)
            # re-compute
            outputs_xyz = outputs_xyz.detach().numpy()  # [8,3,14]
            gt_xyz1 = gt_xyz1.detach().numpy()  # [8,3,14]
            volume_length1 = volume_length1.cpu().detach().numpy()  # [8,1]array
            volume_offset1 = volume_offset1.cpu().numpy()  # [8,3]array
            volume_rotate1 = volume_rotate1.cpu().detach().numpy()  # [8,3,3]array
            len_list = outputs_xyz.shape[0]
            output_xyzs = np.zeros_like(outputs_xyz)  # World C.S.
            gt_xyzs = np.zeros_like(gt_xyz1)
            for ii in range(len_list):
                output_xyz = outputs_xyz[ii, :, :]  # (3,14)
                g_xyz = gt_xyz1[ii, :, :]  # (3,14)
                out_xyz = np.zeros_like(output_xyz)  # (3,14)
                gxyz = np.zeros_like(g_xyz)
                v_length = volume_length1[ii]  # (1)
                v_offset = volume_offset1[ii]  # (3)
                v_rotate = volume_rotate1[ii]  # (3,3)
                for jj in range(output_xyz.shape[1]):
                    xyz = output_xyz[:, jj]  # (3)
                    xyz = xyz + v_offset
                    out_xyz[:, jj] = xyz
                    ggxyz = g_xyz[:, jj]
                    ggxyz = ggxyz + v_offset
                    gxyz[:, jj] = ggxyz
                out_xyz = out_xyz * v_length  # (3,14)
                out_xyz = np.dot(out_xyz.T, np.linalg.inv(v_rotate.T))  # (14,3)
                out_xyz = out_xyz.T  # (3,14)
                output_xyzs[ii] = out_xyz
                gxyz = gxyz * v_length
                gxyz = np.dot(gxyz.T, np.linalg.inv(v_rotate.T))  # Correct
                gxyz = gxyz.T
                gt_xyzs[ii] = gxyz

            output_xyzs = torch.from_numpy(output_xyzs)
            gt_xyzs = torch.from_numpy(gt_xyzs)

            # a=output_xyzs - gt_xyzs
            diff = torch.pow(output_xyzs - gt_xyzs, 2)  # [8,3,14]
            diff_sum = torch.sum(diff, 1)  # [8,14]
            diff_sum_sqrt = torch.sqrt(diff_sum)  # [8,14]
            diff_mean = torch.mean(diff_sum_sqrt, 1).view(-1, 1)  # [8,1]
            train_wld_err_batch.append(diff_mean.sum().detach().numpy().tolist())

            loss_estimater = model.get_es_loss()
            loss_estimater = loss_estimater.cpu()
            train_loss_es_batch.append(loss_estimater.detach().numpy().tolist())

            #compute joint errors
            joints_err_list_train = np.sum([joints_err_list_train, torch.sum(diff_sum_sqrt, 0).view(-1).detach().numpy().tolist()], axis=0)
            #break
        # time taken
        timer = time.time() - timer
        timer = timer / (epoch_iter*opt.train_label_ratio)
        print('Training ==> time to learn 1 sample = %f (ms)' % (timer * 1000))

        # print mse
        train_mse_wld = sum(train_wld_err_batch) / (epoch_iter*opt.train_label_ratio)
        train_loss_es = sum(train_loss_es_batch) / (epoch_iter*opt.train_label_ratio)
        # print('mean-square error of 1 sample: %f, #train_data = %d' % (train_mse, len(trainset)))
        print('average estimater loss: %f ' % (train_loss_es))
        print('average estimation error in world coordinate system: %f (mm)' % (train_mse_wld))

        # test network
        timer = time.time()
        test_wld_err = 0.0
        test_wld_err_batch = []
        test_loss_es = 0
        test_loss_es_batch = []

        epoch_iter1 = 0
        for i1, data1 in enumerate(tqdm(testloader,0)):
            epoch_iter1 += opt.batch_size
            #input_pc, input_sn, input_node, input_node_knn_I, gt_pca, gt_xyz, valid, volume_length, volume_offset, volume_rotate, PCA_coeff, PCA_mean = data1
            #model.set_input(input_pc, input_sn, gt_pca, input_node, input_node_knn_I)
            input_pc, input_sn, input_node, input_node_knn_I, gt_xyz, volume_length, volume_offset, volume_rotate = data1
            model.set_input(input_pc, input_sn, gt_xyz, input_node, input_node_knn_I)

            model.test_encoder_estimater_model()  #

            estimation_joins = model.get_estimation()
            estimation_joins = estimation_joins.view(estimation_joins.size(0), 1, estimation_joins.size(1))
            outputs_xyz = estimation_joins.cpu().view(gt_xyz.shape)

            # re-compute
            outputs_xyz = outputs_xyz.detach().numpy()  # [8,3,14]
            # gt_xyz = gt_xyz.view(-1, opt.JOINT_NUM, 3)# [8,14,3]
            gt_xyz = gt_xyz.detach().numpy()  # [8,3,14]
            volume_length = volume_length.cpu().detach().numpy()  # [8,1]array
            volume_offset = volume_offset.cpu().numpy()  # [8,3]array
            volume_rotate = volume_rotate.cpu().detach().numpy()  # [8,3,3]array

            len_list = outputs_xyz.shape[0]
            output_xyzs = np.zeros_like(outputs_xyz)  # World C.S.
            gt_xyzs = np.zeros_like(gt_xyz)

            for ii in range(len_list):
                output_xyz = outputs_xyz[ii, :, :]  # (3,14)
                g_xyz = gt_xyz[ii, :, :]  # (3,14)
                out_xyz = np.zeros_like(output_xyz)  # (3,14)
                gxyz = np.zeros_like(g_xyz)

                v_length = volume_length[ii]  # (1)
                v_offset = volume_offset[ii]  # (3)
                v_rotate = volume_rotate[ii]  # (3,3)

                for jj in range(output_xyz.shape[1]):
                    xyz = output_xyz[:, jj]  # (3)
                    xyz = xyz + v_offset
                    out_xyz[:, jj] = xyz
                    ggxyz = g_xyz[:, jj]
                    ggxyz = ggxyz + v_offset
                    gxyz[:, jj] = ggxyz
                out_xyz = out_xyz * v_length  # (3,14)
                out_xyz = np.dot(out_xyz.T, np.linalg.inv(v_rotate.T))  # (14,3)
                out_xyz = out_xyz.T  # (3,14)
                output_xyzs[ii] = out_xyz
                gxyz = gxyz * v_length
                gxyz = np.dot(gxyz.T, np.linalg.inv(v_rotate.T))  # Correct
                gxyz = gxyz.T
                gt_xyzs[ii] = gxyz

            output_xyzs = torch.from_numpy(output_xyzs)
            gt_xyzs = torch.from_numpy(gt_xyzs)

            # a=output_xyzs - gt_xyzs
            diff = torch.pow(output_xyzs - gt_xyzs, 2)  # [8,3,14]
            diff_sum = torch.sum(diff, 1)  # [8,14]
            diff_sum_sqrt = torch.sqrt(diff_sum)  # [8,14]
            diff_mean = torch.mean(diff_sum_sqrt, 1).view(-1, 1)  # [8,1]
            test_wld_err_batch.append(diff_mean.sum().detach().numpy().tolist())

            loss_estimater = model.get_es_loss()
            loss_estimater = loss_estimater.cpu()
            test_loss_es_batch.append(loss_estimater.detach().numpy().tolist())

            # compute joint errors
            joints_err_list_test = np.sum([joints_err_list_test, torch.sum(diff_sum_sqrt, 0).view(-1).detach().numpy().tolist()], axis=0)
            #break

        # time taken
        timer = time.time() - timer
        timer = timer / epoch_iter1
        print('Testing ==> time to learn 1 sample = %f (ms)' % (timer * 1000))
        # print mse
        test_loss_es = sum(test_loss_es_batch) / epoch_iter1
        test_wld_err = sum(test_wld_err_batch) / epoch_iter1
        # test_wld_err= np.mean(np.flatten(test_wld_err_list))
        print('average estimater loss: %f ' % (test_loss_es))
        print('average estimation error in world coordinate system: %f (mm)' % (test_wld_err))
        logging.info('Epoch#%d: train error=%e, train wld error = %f mm, test error=%e, test wld error = %f mm, estimater lr = %.8f' % (epoch + current_es_index + 1, train_loss_es, train_mse_wld, test_loss_es, test_wld_err, model.get_es_lr()))

        #Visualize joints errors
        '''

        X=np.arange(opt.JOINT_NUM)
        Y1=[y1/epoch_iter for y1 in joints_err_list_train]
        Y2=[y2/epoch_iter1 for y2 in joints_err_list_test]
        width=0.3
        plt.bar(left=X, height=Y1, width=width,color='yellow',label='Train')
        plt.bar(left=X+width, height=Y2, width=width,color='red',label='Test')
        # plt.plot(x2,y2, '-r', linestyle='-', label='Árfolyam model 2')
        plt.title('Joints Errors')
        plt.xlabel('Joint')
        plt.ylabel('Error')
        plt.legend(loc='best')
        plt.axis([-1,(opt.JOINT_NUM+1),0,100])
        if (epoch + current_es_index + 1) % 5 == 0 and epoch >= 0:
            save_str=os.path.join(opt.checkpoints_dir, str(epoch + current_es_index + 1)+'_'+str(test_wld_err)+'.png')
            plt.savefig(save_str)
        plt.show()
        '''

        # save network
        if (epoch + current_es_index + 1) % 1 == 0:
            print("Saving network...")
            model.save_network(model.encoder, 'encoder', '%d_%f_%.8f' % (epoch + current_en_index + 1, test_wld_err, model.get_es_lr()),
                               opt.gpu_id)
            model.save_network(model.estimater, 'estimater', '%d_%f_%.8f' % (epoch + current_es_index + 1, test_wld_err, model.get_es_lr()),
                                   opt.gpu_id)
        # learning rate decay
        if (epoch + current_es_index + 1) % 10 == 0 and epoch >= 0:
            model.update_learning_rate(0.5)

        torch.cuda.empty_cache()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('watering', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Branch',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('descr', models.CharField(max_length=30)),
                ('duration', models.IntegerField(default=30)),
                ('monday', models.BooleanField()),
                ('tuesday', models.BooleanField()),
                ('wednesday', models.BooleanField()),
                ('thursday', models.BooleanField()),
                ('friday', models.BooleanField()),
                ('saturday', models.BooleanField()),
                ('sunday', models.BooleanField()),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Status',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=15)),
                ('descr', models.CharField(max_length=30)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.DeleteModel(
            name='Dict',
        ),
        migrations.AddField(
            model_name='branch',
            name='status',
            field=models.ForeignKey(to='watering.Status'),
            preserve_default=True,
        ),
    ]


from tkinter import *

root = Tk()
root.title("han GUI")
root.geometry("640x480")

def create_new_file():
    print('새 파일을 만듭니다.')

menu = Menu(root)

#File 메뉴
menu_file = Menu(menu, tearoff = 0)
menu_file.add_command(label = "New file", command = create_new_file)
menu_file.add_command(label = "New Window")
#구분자
menu_file.add_separator()
menu_file.add_command(label= "Open File..")
menu_file.add_separator()
menu_file.add_command(label = "Save All", state = "disable")
menu_file.add_separator()
menu_file.add_command(label = "Exit" , command = root.quit)

#Edit 메뉴
menu.add_cascade(label = "Edit")

#language 메뉴 추가(radio button)
menu_lang = Menu(menu, tearoff = 0)
menu_lang.add_radiobutton(label = "Python")
menu_lang.add_radiobutton(label = "Java")
menu_lang.add_radiobutton(label = "C++")
menu.add_cascade(label = "Language", menu = menu_lang)

#View 메뉴
menu_view = Menu(menu, tearoff = 0)
menu_view.add_checkbutton(label = "Show Minimap")
menu.add_cascade(label = "View", menu = menu_view)

menu.add_cascade(label = "File", menu = menu_file)

root.config(menu= menu)
root.mainloop()


#!/usr/bin/env python3

# https://www.interviewcake.com/question/python/queue-two-stacks

class QTS:
    def __init__(self):
        self.stack0 = []
        self.stack1 = []

    def enqueue(self,v):
        self.stack0.append(v)

    def dequeue(self):
        if len(self.stack0) == 0:
            return None
        self.stack1 = []
        while len(self.stack0) != 0:
            self.stack1.append(self.stack0.pop())
        v = self.stack1.pop()
        while len(self.stack0) != 0:
            self.stack0.append(self.stack1.pop())
        return v

qts = QTS()
qts.enqueue('a')
qts.enqueue('b')
qts.enqueue('c')
print(qts.dequeue())


import math
from typing import List

def part1(lines: List[str]):
    time = int(lines[0])
    split = filter(lambda x: x != "x", lines[1].split(","))
    times = [int(x) for x in split]
    diff = 1_000_000
    for i in range(time, time + diff):
        for j in times:
            if i % j == 0:
                return (i - time) * j

def chinese_remainder(A: List[int], N: List[int]):
    """
    Given

    x === A1 (mod N1)
    x === A2 (mod N2)
    ...
    x === An (mod Nn)

    Args:
        n: List of moduluses
        a: List of residues / remainders

    https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html
    """
    result = 0
    prod = math.prod(N)
    for a_i, n_i in zip(A, N):
        N_i = prod // n_i
        result += a_i * mul_inv(N_i, n_i) * N_i
    return result % prod

def mul_inv(a: int, n: int):
    """Find multiplicative inverse of a under n.

    Solve: ax === 1 (mod n)

    This assumes that gcd(a, n) == 1, i.e.
    that a and n are coprime.

    http://www-math.ucdenver.edu/~wcherowi/courses/m5410/exeucalg.html
    """
    n0 = n
    x0, x1 = 0, 1
    if n == 1:
        return 1
    while a > 1:
        q = a // n
        a, n = n, a % n
        x0, x1 = x1 - q * x0, x0
    if x1 < 0:
        x1 += n0
    return x1

def part2(lines: List[str]):
    split = lines[1].split(",")
    idx = []
    for i, num in enumerate(split):
        if num != "x":
            new_index = (-i) % int(num)
            idx.append([new_index, int(num)])
    A = [x[0] for x in idx]
    N = [x[1] for x in idx]
    return chinese_remainder(A, N)


# Construct a Binary Tree in Python.

class Node(object):
    def __init__(self,value):
        self.value=value
        self.left=None
        self.right=None

class BinaryTree(object):
    def __init__(self,root):
        self.root=Node(root)

    def preorder(self,start,tmp):
        if start is None:
            return
        tmp.append(str(start.value))
        self.preorder(start.left,tmp)
        self.preorder(start.right,tmp)

    def print_tree(self,traversal_type):
        if traversal_type=='preorder':
            tmp=[]
            start=self.root
            self.preorder(start,tmp)
            return ' '.join(tmp)
        elif traversal_type=='inorder':
            return self.inorder(self.root,'')
        elif traversal_type=='postorder':
            return self.postorder(self.root,'')

    def HeightOfBinaryTree(self):

        start=self.root
        tmp=[]
        tmp.append(start)
        tmp.append('None')

        height=0

        while len(tmp)!=0:

            while tmp[0]!='None':
                node=tmp.pop(0)
                if node.left!=None:
                    tmp.append(node.left)
                if node.right!=None:
                    tmp.append(node.right)
            if tmp[0]=='None' and len(tmp)==1:
                break
            else:
                tmp.pop(0)
                height=height+1
                tmp.append('None')

        return height

def main():

    tree=BinaryTree('a')
    tree.root.left=Node('b')
    tree.root.right=Node('c')
    tree.root.left.left=Node('d')
    tree.root.left.right=Node('e')
    tree.root.right.left=Node('f')
    tree.root.right.right=Node('g')
    tree.root.left.left.left = Node('h')
    tree.root.left.left.right = Node('i')
    tree.root.right.left.left = Node('j')
    tree.root.right.left.right= Node('k')
    tree.root.right.right.right= Node('l')
    print(tree.print_tree('preorder'))
    print(tree.HeightOfBinaryTree())

if __name__=='__main__':
    main()


r"""Definition of the DataLoader and associated iterators
that subclass _BaseDataLoaderIter

To support these two classes, in `./_utils` we define many utility methods and
functions to be run in multiprocessing. E.g., the data loading worker loop is
in `./utils/worker.py`.
"""

import threading
import itertools
import warnings
import queue

import multiprocessing as python_multiprocessing
import torch
import torch.multiprocessing as multiprocessing
from torch._utils import ExceptionWrapper
from torch._six import string_classes

from torch.utils.data import (
    IterableDataset,
    Sampler,
    SequentialSampler,
    RandomSampler,
    BatchSampler,
)
from torch.utils.data import _utils
from pytorch_pfn_extras.dataloaders._utils import worker

get_worker_info = worker.get_worker_info

# This function used to be defined in this file. However, it was moved to
# _utils/collate.py. Although it is rather hard to access this from user land
# (one has to explicitly directly `import torch.utils.data.dataloader`), there
# probably is user code out there using it. This aliasing maintains BC in this
# aspect.
default_collate = _utils.collate.default_collate

class _DatasetKind(object):
    Map = 0
    Iterable = 1

    @staticmethod
    def create_fetcher(kind, dataset, auto_collation, collate_fn, drop_last):
        if kind == _DatasetKind.Map:
            return _utils.fetch._MapDatasetFetcher(
                dataset, auto_collation, collate_fn, drop_last
            )
        else:
            return _utils.fetch._IterableDatasetFetcher(
                dataset, auto_collation, collate_fn, drop_last
            )

class _InfiniteConstantSampler(Sampler):
    r"""Analogous to ``itertools.repeat(None, None)``.
    Used as sampler for :class:`~torch.utils.data.IterableDataset`.

    Arguments:
        data_source (Dataset): dataset to sample from
    """

    def __init__(self):
        super(_InfiniteConstantSampler, self).__init__(None)

    def __iter__(self):
        while True:
            yield None

class DataLoader(object):
    r"""
    Data loader. Combines a dataset and a sampler, and provides an
    iterable over the given dataset.

    The :class:`~torch.utils.data.DataLoader` supports both map-style and
    iterable-style datasets with single- or multi-process loading, customizing
    loading order and optional automatic batching (collation)
    and memory pinning.

    See :py:mod:`torch.utils.data` documentation page for more details.

    Arguments:
        dataset (Dataset): dataset from which to load the data.
        batch_size (int, optional): how many samples per batch to load
            (default: ``1``).
        shuffle (bool, optional): set to ``True`` to have the data reshuffled
            at every epoch (default: ``False``).
        sampler (Sampler or Iterable, optional): defines the strategy to draw
            samples from the dataset. Can be any ``Iterable`` with ``__len__``
            implemented. If specified, :attr:`shuffle` must not be specified.
        batch_sampler (Sampler or Iterable, optional): like :attr:`sampler`,
            but returns a batch of indices at a time. Mutually exclusive with
            :attr:`batch_size`, :attr:`shuffle`, :attr:`sampler`,
            and :attr:`drop_last`.
        num_workers (int, optional): how many subprocesses to use for data
            loading. ``0`` means that the data will be loaded in the main
            process. (default: ``0``)
        collate_fn (callable, optional): merges a list of samples to form a
            mini-batch of Tensor(s).  Used when using batched loading from a
            map-style dataset.
        pin_memory (bool, optional): If ``True``, the data loader will copy
            Tensors into CUDA pinned memory before returning them.
            If your data elements are a custom type, or your
            :attr:`collate_fn` returns a batch that is a custom type,
            see the example below.
        drop_last (bool, optional): set to ``True`` to drop the last
            incomplete batch, if the dataset size is not divisible by
            the batch size. If ``False`` and the size of dataset is
            not divisible by the batch size, then the last batch
            will be smaller. (default: ``False``)
        timeout (numeric, optional): if positive, the timeout value
            for collecting a batch
            from workers. Should always be non-negative. (default: ``0``)
        worker_init_fn (callable, optional): If not ``None``, this will
            be called on each worker subprocess with the worker id
            (an int in ``[0, num_workers - 1]``) as
            input, after seeding and before data loading. (default: ``None``)
        persistent_workers (bool, optional): If ``True``, the data loader
            will not shutdown the worker processes after a dataset has
            been consumed once. This allows to maintain the workers
            `Dataset` instances alive. (default: ``False``)

    .. warning:: If the ``spawn`` start method is used, :attr:`worker_init_fn`
                 cannot be an unpicklable object, e.g., a lambda function. See
                 :ref:`multiprocessing-best-practices` on more details related
                 to multiprocessing in PyTorch.

    .. note:: ``len(dataloader)`` heuristic is based on the length of the
                sampler used.
              When :attr:`dataset` is an
              :class:`~torch.utils.data.IterableDataset`, ``len(dataset)``
              (if implemented) is returned instead, regardless
              of multi-process loading configurations, because PyTorch trust
              user :attr:`dataset` code in correctly handling multi-process
              loading to avoid duplicate data. See `Dataset Types`_ for more
              details on these two types of datasets and how
              :class:`~torch.utils.data.IterableDataset` interacts
              with `Multi-process data loading`_.
    """

    __initialized = False

    def __init__(
        self,
        dataset,
        batch_size=1,
        shuffle=False,
        sampler=None,
        batch_sampler=None,
        num_workers=0,
        collate_fn=None,
        pin_memory=False,
        drop_last=False,
        timeout=0,
        worker_init_fn=None,
        multiprocessing_context=None,
        generator=None,
        persistent_workers=False,
    ):
        torch._C._log_api_usage_once("python.data_loader")

        if num_workers < 0:
            raise ValueError(
                "num_workers option should be non-negative; "
                "use num_workers=0 to disable multiprocessing."
            )

        if timeout < 0:
            raise ValueError("timeout option should be non-negative")

        self.dataset = dataset
        self.num_workers = num_workers
        self.pin_memory = pin_memory
        self.timeout = timeout
        self.worker_init_fn = worker_init_fn
        self.multiprocessing_context = multiprocessing_context

        # Arg-check dataset related before checking samplers because we want to
        # tell users that iterable-style datasets are incompatible with custom
        # samplers first, so that they don't learn that this combo doesn't work
        # after spending time fixing the custom sampler errors.
        if isinstance(dataset, IterableDataset):
            self._dataset_kind = _DatasetKind.Iterable
            if shuffle is not False:
                raise ValueError(
                    "DataLoader with IterableDataset: expected unspecified "
                    "shuffle option, but got shuffle={}".format(shuffle)
                )
            elif sampler is not None:
                # See NOTE [ Custom Samplers and IterableDataset ]
                raise ValueError(
                    "DataLoader with IterableDataset: expected unspecified "
                    "sampler option, but got sampler={}".format(sampler)
                )
            elif batch_sampler is not None:
                # See NOTE [ Custom Samplers and IterableDataset ]
                raise ValueError(
                    "DataLoader with IterableDataset: expected unspecified "
                    "batch_sampler option, but got batch_sampler={}".format(
                        batch_sampler
                    )
                )
        else:
            self._dataset_kind = _DatasetKind.Map

        if sampler is not None and shuffle:
            raise ValueError(
                "sampler option is mutually exclusive with " "shuffle"
            )

        if batch_sampler is not None:
            # auto_collation with custom batch_sampler
            if batch_size != 1 or shuffle or sampler is not None or drop_last:
                raise ValueError(
                    "batch_sampler option is mutually exclusive "
                    "with batch_size, shuffle, sampler, and "
                    "drop_last"
                )
            batch_size = None
            drop_last = False
        elif batch_size is None:
            # no auto_collation
            if drop_last:
                raise ValueError(
                    "batch_size=None option disables auto-batching "
                    "and is mutually exclusive with drop_last"
                )

        if sampler is None:  # give default samplers
            if self._dataset_kind == _DatasetKind.Iterable:
                # See NOTE [ Custom Samplers and IterableDataset ]
                sampler = _InfiniteConstantSampler()
            else:  # map-style
                if shuffle:
                    sampler = RandomSampler(dataset, generator=generator)
                else:
                    sampler = SequentialSampler(dataset)

        if batch_size is not None and batch_sampler is None:
            # auto_collation without custom batch_sampler
            batch_sampler = BatchSampler(sampler, batch_size, drop_last)

        self.batch_size = batch_size
        self.drop_last = drop_last
        self.sampler = sampler
        self.batch_sampler = batch_sampler
        self.generator = generator

        if collate_fn is None:
            if self._auto_collation:
                collate_fn = _utils.collate.default_collate
            else:
                collate_fn = _utils.collate.default_convert

        self.collate_fn = collate_fn
        self.__initialized = True
        self._IterableDataset_len_called = (
            None  # See NOTE [ IterableDataset and __len__ ]
        )

        self._persistent_workers = persistent_workers
        self._iterator = None

    def _get_iterator(self):
        if self.num_workers == 0:
            iterator = _SingleProcessDataLoaderIter(self)
        else:
            iterator = _MultiProcessingDataLoaderIter(self)
        return iterator

    @property
    def multiprocessing_context(self):
        return self.__multiprocessing_context

    @multiprocessing_context.setter
    def multiprocessing_context(self, multiprocessing_context):
        if multiprocessing_context is not None:
            if self.num_workers > 0:
                if not multiprocessing._supports_context:
                    raise ValueError(
                        "multiprocessing_context relies on Python >= 3.4, with"
                        " support for different start methods"
                    )

                if isinstance(multiprocessing_context, string_classes):
                    valid_start_methods = (
                        multiprocessing.get_all_start_methods()
                    )
                    if multiprocessing_context not in valid_start_methods:
                        raise ValueError(
                            (
                                "multiprocessing_context option "
                                "should specify a valid start method in {} "
                                " but got multiprocessing_context={}"
                            ).format(
                                valid_start_methods, multiprocessing_context
                            )
                        )
                    multiprocessing_context = multiprocessing.get_context(
                        multiprocessing_context
                    )

                if not isinstance(
                    multiprocessing_context,
                    python_multiprocessing.context.BaseContext,
                ):
                    raise TypeError(
                        (
                            "multiprocessing_context should be a valid context"
                            " object or a string specifying the start method"
                            " but got multiprocessing_context={}"
                        ).format(multiprocessing_context)
                    )
            else:
                raise ValueError(
                    (
                        "multiprocessing_context can only be used with "
                        "multi-process loading (num_workers > 0), but got "
                        "num_workers={}"
                    ).format(self.num_workers)
                )

        self.__multiprocessing_context = multiprocessing_context

    def __setattr__(self, attr, val):
        if self.__initialized and attr in (
            "batch_size",
            "batch_sampler",
            "sampler",
            "drop_last",
            "dataset",
        ):
            raise ValueError(
                "{} attribute should not be set after {} is "
                "initialized".format(attr, self.__class__.__name__)
            )

        super(DataLoader, self).__setattr__(attr, val)

    def __iter__(self):
        # When using a single worker the returned iterator should be
        # created everytime to avoid reseting its state
        # However, in the case of a multiple workers iterator
        # the iterator is only created once in the lifetime of the
        # DataLoader object so that workers can be reused
        if self._persistent_workers and self.num_workers > 0:
            if self._iterator is None:
                self._iterator = self._get_iterator()
            else:
                self._iterator._reset(self)
            return self._iterator
        else:
            return self._get_iterator()

    @property
    def _auto_collation(self):
        return self.batch_sampler is not None

    @property
    def _index_sampler(self):
        # The actual sampler used for generating indices for `_DatasetFetcher`
        # (see _utils/fetch.py) to read data at each time. This would be
        # `.batch_sampler` if in auto-collation mode, and `.sampler` otherwise.
        # We can't change `.sampler` and `.batch_sampler` attributes for BC
        # reasons.
        if self._auto_collation:
            return self.batch_sampler
        else:
            return self.sampler

    def __len__(self):
        if self._dataset_kind == _DatasetKind.Iterable:
            length = self._IterableDataset_len_called = len(self.dataset)
            if self.batch_size is not None:
                from math import ceil

                if self.drop_last:
                    length = length // self.batch_size
                else:
                    length = ceil(length / self.batch_size)
            return length
        else:
            return len(self._index_sampler)

class _BaseDataLoaderIter(object):
    def __init__(self, loader):
        self._dataset = loader.dataset
        self._dataset_kind = loader._dataset_kind
        self._IterableDataset_len_called = loader._IterableDataset_len_called
        self._auto_collation = loader._auto_collation
        self._drop_last = loader.drop_last
        self._index_sampler = loader._index_sampler
        self._num_workers = loader.num_workers
        self._pin_memory = loader.pin_memory and torch.cuda.is_available()
        self._timeout = loader.timeout
        self._collate_fn = loader.collate_fn
        self._sampler_iter = iter(self._index_sampler)
        self._base_seed = (
            torch.empty((), dtype=torch.int64)
            .random_(generator=loader.generator)
            .item()
        )
        self._persistent_workers = loader._persistent_workers
        self._num_yielded = 0

    def _reset(self, loader, first_iter=False):
        self._sampler_iter = iter(self._index_sampler)
        self._num_yielded = 0
        self._IterableDataset_len_called = loader._IterableDataset_len_called

    def __iter__(self):
        return self

    def _next_index(self):
        return next(self._sampler_iter)  # may raise StopIteration

    def _next_data(self):
        raise NotImplementedError

    def __next__(self):
        data = self._next_data()
        self._num_yielded += 1
        if (
            self._dataset_kind == _DatasetKind.Iterable
            and self._IterableDataset_len_called is not None
            and self._num_yielded > self._IterableDataset_len_called
        ):
            warn_msg = (
                "Length of IterableDataset {} was reported to be {} "
                "(when accessing len(dataloader)), but {} "
                "samples have been fetched. "
            ).format(
                self._dataset,
                self._IterableDataset_len_called,
                self._num_yielded,
            )
            if self._num_workers > 0:
                warn_msg += (
                    "For multiprocessing data-loading, this could be caused "
                    "by not properly configuring the "
                    "IterableDataset replica at each worker. Please see "
                    "https://pytorch.org/docs/stable/data.html#torch.utils.data.IterableDataset for examples."  # NOQA
                )
            warnings.warn(warn_msg)
        return data

    next = __next__  # Python 2 compatibility

    def __len__(self):
        return len(self._index_sampler)

    def __getstate__(self):
        # TODO: add limited pickling support for sharing an iterator
        # across multiple threads for HOGWILD.
        # Probably the best way to do this is by moving the sample pushing
        # to a separate thread and then just sharing the data queue
        # but signalling the end is tricky without a non-blocking API
        raise NotImplementedError(
            "{} cannot be pickled", self.__class__.__name__
        )

class _SingleProcessDataLoaderIter(_BaseDataLoaderIter):
    def __init__(self, loader):
        super(_SingleProcessDataLoaderIter, self).__init__(loader)
        assert self._timeout == 0
        assert self._num_workers == 0

        self._dataset_fetcher = _DatasetKind.create_fetcher(
            self._dataset_kind,
            self._dataset,
            self._auto_collation,
            self._collate_fn,
            self._drop_last,
        )

    def _next_data(self):
        index = self._next_index()  # may raise StopIteration
        data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
        if self._pin_memory:
            data = _utils.pin_memory.pin_memory(data)
        return data

class _MultiProcessingDataLoaderIter(_BaseDataLoaderIter):
    def __init__(self, loader):
        super(_MultiProcessingDataLoaderIter, self).__init__(loader)

        assert self._num_workers > 0

        if loader.multiprocessing_context is None:
            multiprocessing_context = multiprocessing
        else:
            multiprocessing_context = loader.multiprocessing_context

        self._worker_init_fn = loader.worker_init_fn
        self._worker_queue_idx_cycle = itertools.cycle(
            range(self._num_workers)
        )
        self._worker_result_queue = multiprocessing_context.Queue()
        self._worker_pids_set = False
        self._shutdown = False
        self._workers_done_event = multiprocessing_context.Event()

        self._index_queues = []
        self._workers = []
        # A list of booleans representing whether each worker still has work to
        # do, i.e., not having exhausted its iterable dataset object. It always
        # contains all `True`s if not using an iterable-style dataset
        # (i.e., if kind != Iterable).
        self._workers_status = []
        for i in range(self._num_workers):
            index_queue = multiprocessing_context.Queue()
            # index_queue.cancel_join_thread()
            w = multiprocessing_context.Process(
                target=worker._worker_loop,
                args=(
                    self._dataset_kind,
                    self._dataset,
                    index_queue,
                    self._worker_result_queue,
                    self._workers_done_event,
                    self._auto_collation,
                    self._collate_fn,
                    self._drop_last,
                    self._base_seed + i,
                    self._worker_init_fn,
                    i,
                    self._num_workers,
                    self._persistent_workers,
                ),
            )
            w.daemon = True
            # NB: Process.start() actually take some time as it needs to
            #     start a process and pass the arguments over via a pipe.
            #     Therefore, we only add a worker to self._workers list after
            #     it started, so that we do not call .join() if program dies
            #     before it starts, and __del__ tries to join but will get:
            #     AssertionError: can only join a started process.
            w.start()
            self._index_queues.append(index_queue)
            self._workers.append(w)
            self._workers_status.append(True)

        if self._pin_memory:
            self._pin_memory_thread_done_event = threading.Event()
            self._data_queue = queue.Queue()
            pin_memory_thread = threading.Thread(
                target=_utils.pin_memory._pin_memory_loop,
                args=(
                    self._worker_result_queue,
                    self._data_queue,
                    torch.cuda.current_device(),
                    self._pin_memory_thread_done_event,
                ),
            )
            pin_memory_thread.daemon = True
            pin_memory_thread.start()
            # Similar to workers (see comment above), we only register
            # pin_memory_thread once it is started.
            self._pin_memory_thread = pin_memory_thread
        else:
            self._data_queue = self._worker_result_queue

        _utils.signal_handling._set_worker_pids(
            id(self), tuple(w.pid for w in self._workers)
        )
        _utils.signal_handling._set_SIGCHLD_handler()
        self._worker_pids_set = True
        self._reset(loader, first_iter=True)

    def _reset(self, loader, first_iter=False):
        super()._reset(loader, first_iter)
        self._send_idx = 0  # idx of the next task to be sent to workers
        self._rcvd_idx = 0  # idx of the next task to be returned in __next__
        self._task_info = {}
        self._tasks_outstanding = 0
        # always equal to count(v for v in task_info.values() if len(v) == 1)
        self._workers_status = [True for i in range(self._num_workers)]
        # We resume the prefetching in case it was enabled
        if not first_iter:
            # The data queue might have older requests pending from a previous
            # iteration, lets delete them
            while not self._data_queue.empty():
                self._data_queue.get()
            for idx in range(self._num_workers):
                self._index_queues[idx].put(worker._ResumeIteration())
        # prime the prefetch loop
        for _ in range(2 * self._num_workers):
            self._try_put_index()

    def _try_get_data(self, timeout=_utils.MP_STATUS_CHECK_INTERVAL):
        try:
            data = self._data_queue.get(timeout=timeout)
            return (True, data)
        except Exception as e:
            # At timeout and error, we manually check whether any worker has
            # failed. Note that this is the only mechanism for Windows
            # to detect worker failures.
            failed_workers = []
            for worker_id, w in enumerate(self._workers):
                if self._workers_status[worker_id] and not w.is_alive():
                    failed_workers.append(w)
                    self._shutdown_worker(worker_id)
            if len(failed_workers) > 0:
                pids_str = ", ".join(str(w.pid) for w in failed_workers)
                raise RuntimeError(
                    "DataLoader worker (pid(s) {}) exited unexpectedly".format(
                        pids_str
                    )
                )
            if isinstance(e, queue.Empty):
                return (False, None)
            import tempfile
            import errno

            try:
                # Raise an exception if we are this close to the FDs limit.
                # Apparently, trying to open only one file is not a sufficient
                # test.
                # See NOTE [ DataLoader on Linux and open files limit ]
                fds_limit_margin = 10
                fs = [  # NOQA
                    tempfile.NamedTemporaryFile()
                    for i in range(fds_limit_margin)
                ]
            except OSError as e:
                if e.errno == errno.EMFILE:
                    raise RuntimeError(
                        "Too many open files. Communication with the"
                        " workers is no longer possible. Please increase the"
                        " limit using `ulimit -n` in the shell or change the"
                        " sharing strategy by calling"
                        " `torch.multiprocessing.set_sharing_strategy('file_system')`"  # NOQA
                        " at the beginning of your code"
                    )
            raise

    def _get_data(self):
        if self._timeout > 0:
            success, data = self._try_get_data(self._timeout)
            if success:
                return data
            else:
                raise RuntimeError(
                    "DataLoader timed out after {} seconds".format(
                        self._timeout
                    )
                )
        elif self._pin_memory:
            while self._pin_memory_thread.is_alive():
                success, data = self._try_get_data()
                if success:
                    return data
            else:
                # while condition is false, i.e., pin_memory_thread died.
                raise RuntimeError("Pin memory thread exited unexpectedly")
        else:
            while True:
                success, data = self._try_get_data()
                if success:
                    return data

    def _next_data(self):
        while True:
            while self._rcvd_idx < self._send_idx:
                info = self._task_info[self._rcvd_idx]
                worker_id = info[0]
                if (
                    len(info) == 2 or self._workers_status[worker_id]
                ):  # has data or is still active
                    break
                del self._task_info[self._rcvd_idx]
                self._rcvd_idx += 1
            else:
                # no valid `self._rcvd_idx` is found (i.e., didn't break)
                if not self._persistent_workers:
                    self._shutdown_workers()
                raise StopIteration

            # Now `self._rcvd_idx` is the batch index we want to fetch

            # Check if the next sample has already been generated
            if len(self._task_info[self._rcvd_idx]) == 2:
                data = self._task_info.pop(self._rcvd_idx)[1]
                return self._process_data(data)

            assert not self._shutdown and self._tasks_outstanding > 0
            idx, data = self._get_data()
            self._tasks_outstanding -= 1
            if self._dataset_kind == _DatasetKind.Iterable:
                # Check for _IterableDatasetStopIteration
                if isinstance(
                    data, worker._IterableDatasetStopIteration
                ):
                    self._shutdown_worker(data.worker_id)
                    self._try_put_index()
                    continue

            if idx != self._rcvd_idx:
                # store out-of-order samples
                self._task_info[idx] += (data,)
            else:
                del self._task_info[idx]
                return self._process_data(data)

    def _try_put_index(self):
        assert self._tasks_outstanding < 2 * self._num_workers
        try:
            index = self._next_index()
        except StopIteration:
            return
        for _ in range(
            self._num_workers
        ):  # find the next active worker, if any
            worker_queue_idx = next(self._worker_queue_idx_cycle)
            if self._workers_status[worker_queue_idx]:
                break
        else:
            # not found (i.e., didn't break)
            return

        self._index_queues[worker_queue_idx].put((self._send_idx, index))
        self._task_info[self._send_idx] = (worker_queue_idx,)
        self._tasks_outstanding += 1
        self._send_idx += 1

    def _process_data(self, data):
        self._rcvd_idx += 1
        self._try_put_index()
        if isinstance(data, ExceptionWrapper):
            data.reraise()
        return data

    def _shutdown_worker(self, worker_id):
        # Mark a worker as having finished its work and dead, e.g., due to
        # exhausting an `IterableDataset`. This should be used only when this
        # `_MultiProcessingDataLoaderIter` is going to continue running.

        assert self._workers_status[worker_id]

        # Signal termination to that specific worker.
        q = self._index_queues[worker_id]
        # Indicate that no more data will be put on this queue by the current
        # process.
        q.put(None)

        self._workers_status[worker_id] = False

    def _shutdown_workers(self):
        python_exit_status = _utils.python_exit_status
        if python_exit_status is True or python_exit_status is None:
            # See (2) of the note. If Python is shutting down, do no-op.
            return
        # Normal exit when last reference is gone / iterator is depleted.
        # See (1) and the second half of the note.
        if not self._shutdown:
            self._shutdown = True
            try:
                if hasattr(self, "_pin_memory_thread"):
                    self._pin_memory_thread_done_event.set()
                    self._worker_result_queue.put((None, None))
                    self._pin_memory_thread.join()
                    self._worker_result_queue.cancel_join_thread()
                    self._worker_result_queue.close()

                # Exit workers now.
                self._workers_done_event.set()
                for worker_id in range(len(self._workers)):
                    if (
                        self._persistent_workers
                        or self._workers_status[worker_id]
                    ):
                        self._shutdown_worker(worker_id)
                for w in self._workers:
                    w.join(timeout=_utils.MP_STATUS_CHECK_INTERVAL)
                    if w.is_alive():
                        w.terminate()
                for q in self._index_queues:
                    q.cancel_join_thread()
                    q.close()
            finally:
                if self._worker_pids_set:
                    _utils.signal_handling._remove_worker_pids(id(self))
                    self._worker_pids_set = False

    def __del__(self):
        self._shutdown_workers()


from django.shortcuts import render

# Create your views here.

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

def result(request):
    text = request.GET['fulltext']
    wordlist = text.split()
    wordlength = len(wordlist)
    wordcount=[]

    for i in wordlist:
        wordcount.append(i+":"+str(wordlist.count(i)))

    wordresult=list(set(wordcount))

    # wordmatch= "\n".join(wordcount)

    return render(request, 'result.html', {
        'text': text,
        'wordlist': wordlist,
        'wordlength': wordlength,
        'wordcount' : wordcount,
        'wordresult' : wordresult,
    })


# MIT License

# Copyright (c) 2021 TRUMPF Werkzeugmaschinen GmbH + Co. KG

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
import datetime

def get_application_uri(hostname, applicationName):
    return f"urn:{hostname}:freeopcua:{applicationName}"

def gen_certificates(privateKeyFullPath, certificateFullPath, hostname, applicationName):

    # Generate the key
    key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
    )

    privateKeyBytes = key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.TraditionalOpenSSL,
            encryption_algorithm=serialization.NoEncryption(),
        )

    # Write our key to disk for safe keeping
    if (privateKeyFullPath):
        with open(privateKeyFullPath, "wb") as f:
            f.write(privateKeyBytes)

    # Various details about who we are. For a self-signed certificate the
    # subject and issuer are always the same.
    # Did not set country, state and locality
    # https://cryptography.io/en/latest/x509/reference.html
    # https://cryptography.io/en/latest/x509/reference.html#general-name-classes
    # https://cryptography.io/en/latest/x509/reference.html#x-509-extensions
    theName = f"{applicationName}@{hostname}"
    serialNumber = x509.random_serial_number()
    subject = issuer = x509.Name([
        x509.NameAttribute(NameOID.LOCALITY_NAME, applicationName),
        x509.NameAttribute(NameOID.ORGANIZATION_NAME, f"{applicationName}_Organization"),
        x509.NameAttribute(NameOID.COMMON_NAME, theName),
       ])

    # Create early, so that ski.digest can be used in the certificate builder
    ski = x509.SubjectKeyIdentifier.from_public_key(key.public_key())

    cert = (
        x509.CertificateBuilder()
        .subject_name(subject)
        .issuer_name(issuer)
        .public_key(key.public_key())
        .serial_number(serialNumber)
        # Start validity period from yesterday
        .not_valid_before(datetime.datetime.today() - datetime.timedelta(days=1))
        # Our certificate will be valid for 10 years
        .not_valid_after(datetime.datetime.utcnow() + (datetime.timedelta(days=365)*10))
        # -------------------------------------------------------------------------------------------------
        # If an extension is marked as critical (critical True), it can not be ignored by an application.
        # The application must recognise and process the extension.
        # -------------------------------------------------------------------------------------------------
        .add_extension(
            # The subject key identifier extension provides a means of identifying
            # certificates that contain a particular public key.
            ski, critical=False)
        .add_extension(
            # The authority key identifier extension provides a means of
            # identifying the public key corresponding to the private key used to sign a certificate.
            x509.AuthorityKeyIdentifier(ski.digest, [x509.DirectoryName(issuer)], serialNumber), critical=False)
        # Subject alternative name is an X.509 extension that provides a list of general name instances
        # that provide a set of identities for which the certificate is valid.
        .add_extension(
            x509.SubjectAlternativeName([
                # URI must be first entry of SubjectAlternativeName. Must exactly match the client.application_uri
                x509.UniformResourceIdentifier(get_application_uri(hostname, applicationName)),
                x509.DNSName(hostname)
                ]), critical=False)
        # The key usage extension defines the purpose of the key contained in the certificate.
        .add_extension(
            # Basic constraints is an X.509 extension type that defines whether a given certificate is
            # allowed to sign additional certificates and what path length restrictions may exist.
            x509.BasicConstraints(ca=False, path_length=None), critical=True)
        .add_extension(
            x509.KeyUsage(
                digital_signature=True, content_commitment=True, key_encipherment=True,
                data_encipherment=True, key_agreement=False, key_cert_sign=True, crl_sign=False,
                encipher_only=False, decipher_only=False), critical=True)
        .add_extension(
            # This extension indicates one or more purposes for which the certified public key may be used,
            # in addition to or in place of the basic purposes indicated in the key usage extension.
            x509.ExtendedKeyUsage([x509.OID_SERVER_AUTH, x509.OID_CLIENT_AUTH]), critical=True)
        # Sign our certificate with our private key
        .sign(key, algorithm=hashes.SHA256()))

    certificateBytes = cert.public_bytes(serialization.Encoding.DER)

    if (certificateFullPath):
        with open(certificateFullPath, "wb") as f:
            f.write(certificateBytes)

    return (privateKeyBytes, certificateBytes)


from random import randint
x = randint(1,6)

class Die():
    def __init__(self, sides):
        self.sides = 6

    def roll_die(self):
        i = 0
        while i < 10:
            print("You throw the die and get " + str(randint(1, self.sides)) + "!")
            i += 1

die = Die(sides=6)

die.roll_die()


#! /usr/bin/env python3

import argparse
import random
import sys
import os
import math

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument( '-n', '--num-samples', default = 1, type = int, help = 'Number of samples to be generated.' )
    parser.add_argument( '-l', '--length', default = 16, type = int, help = 'Length of a sample in bytes.' )
    parser.add_argument( '-v', '--verbose', action = 'store_true' )
    parser.add_argument( '-b', '--bits', default = 8, type = int, help = 'Bits per unit item in sample.' )
    parser.add_argument( 'FILE' )
    args = parser.parse_args()

    if args.verbose == True:
        print( args )

    handle = open( args.FILE, 'w' ) if args.FILE != '-' else sys.stdout

    random.seed()

    unit_format = "%%0%dx" % math.ceil( args.bits / 4 )
    total_bits = args.length * args.bits
    max_value   = 1 << total_bits - 1

    for n in range( args.num_samples ):
        value = random.randint( 0, max_value )
        random_list = [ ( value >> ( i * 8 ) ) & 0xff for i in range( args.length ) ]
        handle.write( " ".join( [ unit_format  % value for value in random_list ] ) + os.linesep )

    if handle != sys.stdout:
        handle.close()

if __name__ == '__main__':
    main()


import sqlite3
import logging

logger = logging.getLogger()

db_file = 'db.sqlite'

class ManageTodoDB:

    def connect_db(self):
        try:
            db = sqlite3.connect(db_file)
            logger.info("Connected to DB: %s", db_file)
            return db
        except sqlite3.Error as e:
            logger.error('Error connecting to DB: %s', e)

    # Create tables
    def create_db_struct(self):
        sql = '''
            CREATE TABLE IF NOT EXISTS tasks (
                task_id INTEGER PRIMARY KEY AUTOINCREMENT,
                task_name VARCHAR(250) NOT NULL,
                task_description VARCHAR(1000) NOT NULL,
                task_assignee INTEGER NOT NULL,
                task_start_date DATE, task_end_date DATE,
                FOREIGN KEY (task_assignee) REFERENCES users(user_id)
            );

            CREATE TABLE IF NOT EXISTS users (
                user_id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_name VARCHAR(50) NOT NULL UNIQUE
            );
              '''
        c = self.connect_db().cursor()
        try:
            c.executescript(sql)
            logger.info("DB structure was created!")
        except sqlite3.Error as e:
            logger.error("Error creating DB: %s", e)
            return e

    # Add new user
    def add_user(self, username):
        sql = '''
            INSERT INTO users (user_name) VALUES("{:s}")
        '''.format(username)
        c = self.connect_db().cursor()
        try:
            c.executescript(sql)
            logger.info("Successfully added user %s", username)
            return True
        except sqlite3.IntegrityError as e:
            logger.error("Error adding user %s. Message: %s", username, e)
            return False

    # Add new task
    def add_task(self, name, description, assignee, start_date, end_date):

        user_id = self.__get_user_id(assignee)
        if not user_id:
            return 404

        sql = '''
            INSERT INTO tasks (task_name, task_description, task_assignee, task_start_date, task_end_date)
            VALUES("{:s}", "{:s}", {:d}, "{:s}", "{:s}")
        '''.format(name, description, user_id, start_date, end_date)

        c = self.connect_db().cursor()
        try:
            c.executescript(sql)
            logger.info("Successfully added task: %s", name)
            return 201

        except sqlite3.IntegrityError as e:
            logger.error("DB integrity error: %s", e)
            return e

    # Helper  method to user id by name
    def __get_user_id(self, user_name):

        sql = '''
            SELECT user_id FROM users WHERE user_name="{:s}"
        '''.format(user_name)

        c = self.connect_db().cursor()
        user_id = c.execute(sql).fetchone()
        if user_id is None:
            return False

        return user_id[0]

    # Remove all tables
    def recreate_db(self):
        sql = '''
            DROP TABLE IF EXISTS tasks;
            DROP TABLE IF EXISTS users;
        '''
        c = self.connect_db().cursor()

        c.executescript(sql)

        logger.info("Deleted all tables")
        self.create_db_struct()

    # Get all tasks in DB
    def get_all_tasks(self):
        sql = '''
            SELECT task_name, task_description, task_start_date, task_end_date, user_name FROM tasks, users
            WHERE task_assignee = user_id;
        '''

        conn = self.connect_db()

        c = conn.cursor()
        c.execute(sql)

        tasks = c.fetchall()
        return tasks

    def get_all_users(self):
        sql = '''
            SELECT * FROM users;
        '''

        conn = self.connect_db()

        c = conn.cursor()
        c.execute(sql)

        users = c.fetchall()

        return users


from django import forms
from django.forms import ModelForm
from .models import Alumno, Participante, ParticipantesEvent, PagosAlumno

#Se requiere modificar este form
class AlumnoForm(ModelForm):
	class Meta:
		model = Alumno
		fields = [
			'RUT',
			'Nombre',
			'Genero',
			'Nacimiento',
			'Profesion',
			'DirPersonal',
			'DirLaboral',
			'Email',
			'Ntelefono',
			]

		labels = {
			'RUT' :'RUT',
			'Nombre' : 'Nombre',
			'Genero' : 'Genero',
			'Nacimiento' : 'Fecha Nacimiento',
			'Profesion' : 'Profesion',
			'DirPersonal' : 'Direccion Personal',
			'DirLaboral' : 'Direccion Laboral',
			'Email' : 'E-mail',
			'Ntelefono' : 'Telefono',
		}

		widgets = {
			'RUT' : forms.TextInput(attrs={'class' : 'forms-control','placeholder': 'Con Punto & Guion'}),
			'Nombre' : forms.TextInput(attrs={'class' : 'forms-control','placeholder': 'Primer Nombre & Apellidos'}),
			'Genero' : forms.Select(attrs={'class' : 'forms-control'}),
			'Nacimiento' : forms.SelectDateWidget(years=range(1950,2101)),
			'Profesion' : forms.TextInput(attrs={'class' : 'forms-control'}),
			'DirPersonal' : forms.TextInput(attrs={'class' : 'forms-control'}),
			'DirLaboral' : forms.TextInput(attrs={'class' : 'forms-control'}),
			'Email' : forms.EmailInput(),
			'Ntelefono' : forms.TextInput(attrs={'class' : 'forms-control'}),
		}

class PagosForm(ModelForm):
	class Meta:
		model = PagosAlumno
		fields= [
    		'id_Alumno',
    		'RUT',
    		'Nombre',
    		'Local',
    		'ClasePagada',
    		'Num_Clases_Pagadas',
    		'Mes',
    		'Anno',
    		'IngresoAlumno',
    		'MetodoPago',
    		'Grupal',
		]

		labels= {
			'id_Alumno':'ID',
    		'RUT':'RUT',
    		'Nombre':'Nombre',
    		'Local':'Local',
    		'ClasePagada':'Clase Pagada',
    		'Num_Clases_Pagadas':'N°Clases Pagadas',
    		'Mes':'Mes',
    		'Anno':'Año',
    		'IngresoAlumno':'Ingreso',
    		'MetodoPago':'Metodo Pago',
    		'Grupal':'Clase Grupal',
		}

		widgets={
			'Local':forms.Select(attrs={'class' : 'forms-control'}),
    		'Mes':forms.Select(attrs={'class' : 'forms-control'}),
    		'Grupal':forms.RadioSelect(attrs={'class' : 'forms-control'}),
		}

class ParticipanteForm(ModelForm):
	class Meta:
		model = Participante
		fields = [
			    'Nombre',
    			'Clase_Perteneciente',
				'Code_Clase',
   				'Local',
    			'Dia',
    			'Horario_Inicio',
    			'Horario_Termino',
			]

		labels = {
			'Nombre':'Nombre',
    		'Clase_Perteneciente':'Clase',
			'Code_Clase':'ID Clase',
   			'Local':'Local',
    		'Dia':'Dia',
    		'Horario_Inicio':'Inicio',
    		'Horario_Termino':'Termino',
		}

		widgets = {
			'Nombre' : forms.TextInput(attrs={'class' : 'forms-control','placeholder': 'Primer Nombre & Apellidos'}),
			'Local':forms.Select(attrs={'class' : 'forms-control'}),
    		'Dia':forms.Select(attrs={'class' : 'forms-control'}),
		}

class ParticipanteEventForm(ModelForm):
	class Meta:
		model = ParticipantesEvent
		fields= [
			'ID_Event',
			'EventInscrito',
    		'NombreParticipante',
    		'Escena',
		]

		labels= {
			'EventInscrito' : 'Evento Inscrito',
    		'NombreParticipante' : 'Nombre Participante',
    		'Escena' : 'Escena participante',
		}

		widgets={
			'NombreParticipante' : forms.TextInput(attrs={'class' : 'forms-control','placeholder': 'Primer Nombre & Apellidos'}),
		}

Proyecto -As You Wish (All Version)/ProyectosUNAB (Final Version)/AsYouWish_Serv/Base_Alumno/forms.py


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from PIL import Image
from io import BytesIO
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
from scrapy.utils.misc import md5sum

from tools.mysql import Mysql
import time

from tutorial.tools.DB import DB

class TutorialPipeline(object):
    def __init__(self):
        pass

    def process_item(self, item, spider):
        sql = 'SELECT * FROM illusts WHERE pixiv_id = "%s"'
        value = {
            'pixiv_id': item['pixiv_id']
        }
        db = DB()
        count = db.select(sql, value)
        if count > 0:
            raise DropItem(item['pixiv_id'])
        else:
            return item

# class DownloadImagePipeline(ImagesPipeline):
#     def item_completed(self, results, item, info):
#         image_paths = [x['path'] for ok, x in results if ok]
#         if not image_paths:
#             raise DropItem("Item contains no images")
#         item['image_paths'] = image_paths['path']
#         self.saveMysql(item)
#         return item

class Save(object):
    def __init__(self):
        pass

    def process_item(self, item, spider):
        if item['mode'] == 'daily':
            illust_mode = 1
        else:
            illust_mode = 0
        date = time.strftime('%Y-%m-%d')
        value = {
            'date': date,
            'mode': illust_mode,
            'path': item['images'][0]['path'],
            'pixiv_id': item['pixiv_id'],
            'title': item['title'],
            'total_score': item['total_score'],
            'author': item['author'],
            'image_urls': item['image_urls'],
            'rank': item['rank'],
            'author_id': item['author_id']
        }
        sql = "INSERT INTO illusts(pixiv_id, title, total_score, author, image_urls, date, rank, author_id, path, mode)  VALUES (%(pixiv_id)s, %(title)s, %(total_score)s, %(author)s, %(image_urls)s, %(DATE)s, %(rank)s, %(author_id)s, %(path)s, %(MODE)s)"
        db = Mysql()
        db.action(sql, value)

class myImagePipelines(ImagesPipeline):
    def convert_image(self, image, size=None):
        buf = BytesIO()
        image.save(buf, image.format)
        return image, buf