# -*- coding: utf-8 -*-
# @Time : 2022/5/12 14:53
# @Author : 慕永利
# @FileName : standardAnswerFileOpt.py
# @Email : 1203962063@qq.com
# @Description :  对标准答案文本进行处理

import re
import copy

def standardAnswerLoad(filePath):
    """
    进行模型的加载
    :return:
    """
    resDict = {}
    with open(file=filePath, mode="r", encoding="utf-8") as fp:
        tempKeyList = []
        allLineList = fp.readlines()
        lineNumber = len(allLineList)
        for i in range(0, lineNumber):
            # print(allLineList[i])
            tempList = re.split("\t", str(allLineList[i]).strip())
            if i == 0:
                for tempElem in tempList:
                    tempKeyList.append(str(tempElem).strip())
                    resDict[str(tempElem).strip()] = []
            else:
                for j in range(0, len(tempList)):
                    resDict[tempKeyList[j]].append(str(tempList[j]).strip())
    # print(RuleAndStandardAnswer)
    return resDict

def accordingToConditions(originalDict,paperValue="",paperId="",gapOrderList=[]):
    """
    根据条件，选取特定条件字典。
    :return:
    """
    newDict = copy.deepcopy(originalDict)
    dictKeyList = list(newDict.keys())
    indexList = []

    # '物理试题2 探究串联电路中用电器两端的电压与电源两端电压的关系'
    # '物理试题2 探究串联电路中用电器两端的电压与电源两端电压的关系'
    if paperId != "":
        for i in range(0,len(newDict["examination_paper_id"])):
            if paperId!="" and gapOrderList==[]:
                if str(paperId).strip() == str(newDict["examination_paper_id"][i]).strip():
                    indexList.append(i)
            elif paperId!="" and gapOrderList!=[]:
                for j in range(0,len(gapOrderList)):  # gapOrderList 的下标一定都在 range(0,len(newDict["examination_paper"])) 中， 是它的一个子集。
                    if str(paperId).strip() == str(newDict["examination_paper_id"][i]).strip() and str(gapOrderList[j]).strip() == str(newDict["gap_order"][i]).strip():
                        indexList.append(i)
    elif paperValue!="":
        for i in range(0,len(newDict["examination_paper"])):
            if paperValue!="" and gapOrderList==[]:
                if str(paperValue).strip() == str(newDict["examination_paper"][i]).strip():
                    indexList.append(i)
            elif paperValue!="" and gapOrderList!=[]:
                for j in range(0,len(gapOrderList)):  # gapOrderList 的下标一定都在 range(0,len(newDict["examination_paper"])) 中， 是它的一个子集。
                    if str(paperValue).strip() == str(newDict["examination_paper"][i]).strip() and str(gapOrderList[j]).strip() == str(newDict["gap_order"][i]).strip():
                        indexList.append(i)
    # if newDict["examination_paper"][0]=="化学试题3:鉴别硬水和软水并软化硬水":
    #     pass
    resDict = {}
    for tempKey in dictKeyList:
        resDict[tempKey] = []
        for tempIndex in indexList:
            resDict[tempKey].append(newDict[tempKey][tempIndex])
    # except Exception:
    #     pass
    return resDict


"""
This is a factory class dedicated to creating new instances of objects in
the Massenergize Database
"""

from _main_.utils.utils import get_models_and_field_types
from database import models
from _main_.utils.constants import CREATE_ERROR_MSG
from collections.abc import Iterable
from sentry_sdk import capture_message

MODELS_AND_FIELDS = get_models_and_field_types(models)

class CreateFactory:
  def __init__(self, name=None):
    self.name = name

  def verify_required_fields(self, model, args):
    errors = []
    required_fields = MODELS_AND_FIELDS[model]['required_fields']
    for f in required_fields:
      if f not in args and f !='id':
        errors.append(f"You are missing a required field: {f}")
    return errors

  def create(self, model, args={}):
    args.pop('id', None) #remove id provided.  We want db to assign its own
    errors = self.verify_required_fields(model, args)
    new_object = None

    if errors:
      return None, errors

    #if code gets here we have everything all required fields
    try:

      many_to_many_fields =  MODELS_AND_FIELDS[model]['m2m']
      fk_fields = MODELS_AND_FIELDS[model]['fk']
      field_values = {}
      for field_name, value in args.items():

        if field_name in fk_fields:
          fkModel = model._meta.get_field(field_name).remote_field.model
          field_values[field_name] = fkModel.objects.filter(pk=value).first()
        elif field_name not in many_to_many_fields:
          field_values[field_name] = value

      new_object = model.objects.create(**field_values)
      new_object.full_clean()
      new_object.save()
      for field_name, value in args.items():
        if field_name in many_to_many_fields:
          m2mModel = model._meta.get_field(field_name).remote_field.model
          if not isinstance(value, str) and isinstance(value, Iterable):
            addManyFunction = getattr(getattr(new_object, field_name), "set")
            addManyFunction(m2mModel.objects.filter(pk__in=value))
          else:
            addManyFunction = getattr(getattr(new_object, field_name), "set")
            addManyFunction(m2mModel.objects.filter(pk=value))

      new_object.save()

    except Exception as e:
      capture_message(str(e), level="error")
      errors =  [CREATE_ERROR_MSG, str(e)]
    return new_object, errors

  def update(self, model, args={}):

    errors = []
    new_object = None

    #if code gets here we have everything all required fields
    try:
      many_to_many_fields =  MODELS_AND_FIELDS[model]['m2m']
      fk_fields = MODELS_AND_FIELDS[model]['fk']

      id = args.pop('id', None)
      obj = model.objects.filter(pk=id).first()
      if not obj:
        return None, [f"Resource with id: {id} does not exist"]
      for field_name, value in args.items():
        if field_name in fk_fields:
          fkModel = model._meta.get_field(field_name).remote_field.model
          setattr(obj, field_name, fkModel.objects.filter(pk=value).first())
        elif field_name not in many_to_many_fields:
          setattr(obj, field_name, value)
        elif field_name in many_to_many_fields:
          m2mModel = model._meta.get_field(field_name).remote_field.model
          if not isinstance(value, str) and isinstance(value, Iterable):
            addManyFunction = getattr(getattr(obj, field_name), "set")
            addManyFunction(m2mModel.objects.filter(pk__in=value))
          else:
            addManyFunction = getattr(getattr(obj, field_name), "set")
            oldList = list(getattr(obj, field_name).all())
            res = oldList + list(m2mModel.objects.filter(pk=value))
            addManyFunction(res)

      obj.save()

    except Exception as e:
      capture_message(str(e), level="error")
      obj, errors =  None, [CREATE_ERROR_MSG, str(e)]

    return obj, errors


from collections import defaultdict
from io import BytesIO

import pandas as pd
from flask import Flask, render_template, request

pd.set_option("display.max_colwidth", -1)

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

def _to_str(token):
    if type(token) != str:
        token = str(int(token))
    return token

def _hotel_summary(df):
    num_unique_guests = len(df["Token"][df["Token"].notna()].unique())
    end_time = pd.to_datetime(df.groupby(df["Token"])["Date/Time"].max())
    start_time = pd.to_datetime(df.groupby(df["Token"])["Date/Time"].min())
    duration = end_time - start_time

    return {
        "max_duration_token": _to_str(duration.idxmax()),
        "max_duration_minutes": duration.max().seconds // 60,
        "num_unique_guests": num_unique_guests,
    }

def _chat_summary(df):
    df[["Token"]] = df[["Token"]].fillna(method="ffill")

    data = {}
    for token, sub_df in df.groupby("Token"):
        filled = sub_df[["Guest messages", "Bebot messages"]].fillna("")
        filled["Guest messages"] = filled["Guest messages"].str.replace("\n", "
")
        filled["Bebot messages"] = filled["Bebot messages"].str.replace("\n", "
")
        data[_to_str(token)] = filled.to_html(
            index=False, escape=False, classes="table"
        )

    result = ""
    for t, table in data.items():
        result += f"
 {t}
"
        result += table
    return result

@app.route("/result", methods=["POST"])
def generate_result():
    file = request.files["file"]
    if not file.filename.endswith("xlsx"):
        return "Please upload an excel spreadsheet"
    content_bytes = BytesIO(file.read())

    dfs = pd.read_excel(content_bytes, engine="xlrd", sheet_name=None)
    results = {}
    for hotel, df_ in dfs.items():
        try:
            results[hotel] = _hotel_summary(df_)
        except Exception:
            results[hotel] = f"Error: Can't find summary data for {hotel} sheet"
    chats = {}
    for hotel, df_ in dfs.items():
        try:
            chats[hotel] = _chat_summary(df_)
        except Exception:
            chats[hotel] = f"Error: Can't find chat data for {hotel} sheet"

    return render_template("result.html", results=results, chats=chats)

if __name__ == "__main__":
    app.run(debug=True)


import pandas as pd
import numpy as np
from utils import *

def compute_oscillation(low_values, high_values, close_values, current_index, K_values, day_range=14, slow=False):
    #current_window = closing_price_history[current_index:current_index+day_range-1] * 5
    curr_low = low_values[current_index : current_index + day_range]
    curr_high = high_values[current_index : current_index + day_range]
    curr_close = close_values[current_index : current_index + day_range]

    L_day_range = np.min(curr_low)
    H_day_range = np.max(curr_high)

    K = ((curr_close[-1] - L_day_range) / (H_day_range - L_day_range )) * 100
    if slow:
        if current_index > 3:
            K = (K_values[current_index-1] + K_values[current_index-2] + K)/3

    return K

def compute_stochastic_oscillator(low_values, high_values, close_values):

    K_values = []

    j = 0
    for i in range(len(close_values)-13):
        curr_k = compute_oscillation(low_values, high_values, close_values, i, K_values)
        K_values.append(curr_k)

    return K_values

if __name__ == "__main__":

    df = read_df("./data/tsla.csv")
    START_DATE = "2020-01-01"
    END_DATE = "2021-01-01"

    temp = filter_df(START_DATE, END_DATE, df)

    low_values = temp['Low'].values
    high_values = temp['High'].values
    close_values = temp['Close'].values

    K_values = compute_stochastic_oscillator(low_values, high_values, close_values)
    print(K_values)

    fig, axs = plt.subplots(2)
    axs[0].plot(close_values)
    axs[1].plot(K_values)
    plt.show()


import sys
input = sys.stdin.readline

k = int(input())
arr = list()

for _ in range(k):
    a = int(input())
    if a == 0:
        del arr[-1]
    else:
        arr.append(a)

print(sum(arr))


questions = ["1+1", "2+2", "3+3"]
answers = ["2", "4", "6"]
number=0
correct=0
for running in range(3):
  inp=input("What is "+questions[number]+"? ")
  if(inp==answers[number]):
    print("Correct")
    correct+=1
  else:
    print("Wrong. The answer is "+answers[number])
  number=+1
print("You got "+str(correct)+"/3")


from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = r'''
---
author:
- AIX Development Team (@pbfinley1911)
module: nim
short_description: Server setup, install packages, update SP or TL.
description:
- Server setup, install packages, update SP or TL.
version_added: '2.9'
requirements:
- AIX >= 7.1 TL3
- Python >= 2.7
options:
  action:
    description:
    - Specifies the action to perform.
    - C(update) to update NIM clients with a specified C(lpp_source).
    - C(master_setup) to setup a NIM master.
    - C(check) to retrieve the C(Cstate) of each NIM client.
    - C(compare) to compare installation inventories of the NIM clients.
    - C(script) to apply a script to customize NIM clients.
    - C(allocate) to allocate a resource to specified NIM clients.
    - C(deallocate) to deallocate a resource for specified NIM clients.
    - C(bos_inst) to install a given list of NIM clients.
    - C(define_script) to define a script NIM resource.
    - C(remove) to remove a specified NIM resource.
    - C(reset) to reset the C(Cstate) of a NIM client.
    - C(reboot) to reboot the given NIM clients if they are running.
    - C(maintenance) to perform a maintenance operation on NIM clients.
    type: str
    choices: [ update, master_setup, check, compare, script, allocate, deallocate, bos_inst, define_script, remove, reset, reboot, maintenance ]
    required: true
  lpp_source:
    description:
    - Indicates the lpp_source to apply to the targets.
    - C(latest_tl), C(latest_sp), C(next_tl) and C(next_sp) can be specified;
      based on the NIM server resources, nim will determine
      the actual oslevel necessary to update the targets.
    type: str
  targets:
    description:
    - Specifies the NIM clients to perform the action on.
    - C(foo*) designates all the NIM clients with name starting by C(foo).
    - C(foo[2:4]) designates the NIM clients among foo2, foo3 and foo4.
    - C(*) or C(ALL) designates all the NIM clients.
    type: list
    elements: str
  asynchronous:
    description:
    - If set to C(no), NIM client will be completely installed before starting
      the installation of another NIM client.
    type: bool
    default: no
  device:
    description:
    - The device (or directory) where to find the lpp source to install.
    type: str
  script:
    description:
    - NIM script resource.
    type: str
  resource:
    description:
    - NIM resource.
    type: str
  location:
    description:
    - Specifies the full path name of the script resource file.
    type: str
  group:
    description:
    - NIM group resource.
    type: str
  force:
    description:
    - Forces action.
    type: bool
    default: no
  operation:
    description:
    - NIM maintenance operation.
    type: str
  description:
    description:
    - Describes the NIM operation (informational only).
    type: str
'''

EXAMPLES = r'''
- name: Install using group resource
  nim:
    action: bos_inst
    targets: nimclient01
    group: basic_res_grp

- name: Check the Cstate of all NIM clients
  nim:
    action: check

- name: Define a script resource on NIM master
  nim:
    action: define_script
    resource: myscript
    location: /tmp/myscript.sh

- name: Execute a script on all NIM clients synchronously
  nim:
    action: script
    script: myscript
    asynchronous: no
    targets: all
'''

RETURN = r'''
msg:
    description: Status information.
    returned: always
    type: str
nim_output:
    description: Output from nim commands.
    returned: always
    type: str
nim_node:
    description: NIM node info.
    returned: always
    type: dict
    contains:
        lpp_source:
            description: List of lpp sources.
            returned: always
            type: dict
        master:
            description: NIM master.
            returned: always
            type: dict
        standalone:
            description: List of standalone NIM resources.
            returned: always
            type: dict
        vios:
            description: List of VIOS NIM resources.
            returned: always
            type: dict
    sample:
        "nim_node": {
            "lpp_source": {
                "723lpp_res": "/export/nim/lpp_source/723lpp_res"
            },
            "master": {
                "cstate": "ready for a NIM operation",
                "type": "master"
            },
            "standalone": {
                "nimclient01": {
                    "cstate": "ready for a NIM operation",
                    "ip": "nimclient01.mydomain.com",
                    "type": "standalone"
                },
                "nimclient02": {
                    "cstate": "ready for a NIM operation",
                    "ip": "nimclient02.mydomain.com",
                    "type": "standalone"
                }
            },
            "vios": {}
        }
'''

import re
import threading
# pylint: disable=wildcard-import,unused-wildcard-import,redefined-builtin
from ansible.module_utils.basic import AnsibleModule

nim_node = {}

def run_oslevel_cmd(module, machine, result):
    """
    Run the oslevel command on target machine.

    Stores the output in the dedicated slot of the result dictionary.

    arguments:
        machine (str): The machine name
        result  (dict): The result of the oslevel command
    """

    result[machine] = 'timedout'

    if machine == 'master':
        cmd = ['oslevel', '-s']
    else:
        cmd = ['/usr/lpp/bos.sysmgt/nim/methods/c_rsh',
               machine,
               '"/usr/bin/oslevel -s; echo rc=$?"']

    rc, stdout, stderr = module.run_command(cmd)
    if rc == 0:
        module.debug('{0} oslevel stdout: "{1}"'.format(machine, stdout))
        if stderr.rstrip():
            module.log('"{0}" command stderr: {1}'.format(' '.join(cmd), stderr))

        # remove the rc of c_rsh with echo $?
        if machine != 'master':
            stdout = re.sub(r'rc=[-\d]+\n$', '', stdout)

        # return stdout only ... stripped!
        result[machine] = stdout.rstrip()
    else:
        msg = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), rc)
        module.log('Failed to get oslevel for {0}: {1}'.format(machine, msg))

def get_nim_clients_info(module, lpar_type):
    """
    Return the list of nim lpar_type objects defined on the
           nim master and their associated Cstate value.
    """
    global results

    cmd = ['lsnim', '-t', lpar_type, '-l']
    rc, stdout, stderr = module.run_command(cmd)
    if rc != 0:
        results['stdout'] = stdout
        results['stderr'] = stderr
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), rc)
        module.fail_json(**results)

    # client name and associated Cstate
    info_hash = {}
    for line in stdout.rstrip().split('\n'):
        match_key = re.match(r"^(\S+):", line)
        if match_key:
            obj_key = match_key.group(1)
            info_hash[obj_key] = {}
            info_hash[obj_key]['type'] = lpar_type
            continue

        match_cstate = re.match(r"^\s+Cstate\s+=\s+(.*)$", line)
        if match_cstate:
            cstate = match_cstate.group(1)
            info_hash[obj_key]['cstate'] = cstate
            continue

        match_mgmtprof = re.match(r"^\s+mgmt_profile1\s+=\s+(.*)$", line)
        if match_mgmtprof:
            mgmt_elts = match_mgmtprof.group(1).split()
            if len(mgmt_elts) >= 3:
                info_hash[obj_key]['mgmt_hmc_id'] = mgmt_elts[0]
                info_hash[obj_key]['mgmt_id'] = mgmt_elts[1]
                info_hash[obj_key]['mgmt_cec_serial'] = mgmt_elts[2]
            else:
                module.log('[WARNING] {0} management profile does not have 3 elements: {1}'
                           .format(obj_key, match_mgmtprof.group(1)))
            continue

        match_if = re.match(r"^\s+if1\s+=\s+\S+\s+(\S+)\s+.*$", line)
        if match_if:
            info_hash[obj_key]['ip'] = match_if.group(1)
            continue

    return info_hash

def get_nim_master_info(module):
    """
    Get the Cstate of the nim master.
    """
    global results

    cmd = ['lsnim', '-l', 'master']
    rc, stdout, stderr = module.run_command(cmd)
    if rc != 0:
        results['stdout'] = stdout
        results['stderr'] = stderr
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), rc)
        module.fail_json(**results)

    # Retrieve associated Cstate
    cstate = ''
    for line in stdout.rstrip().split('\n'):
        match_cstate = re.match(r"^\s+Cstate\s+=\s+(.*)$", line)
        if match_cstate:
            cstate = match_cstate.group(1)

    return cstate

def get_oslevels(module, targets):
    """
    Get the oslevel of the specified targets.

    return a dictionary of the oslevels
    """

    # =========================================================================
    # Launch threads to collect information on targets
    # =========================================================================
    threads = []
    oslevels = {}

    for machine in targets:
        process = threading.Thread(target=run_oslevel_cmd,
                                   args=(module, machine, oslevels))
        process.start()
        threads.append(process)

    for process in threads:
        process.join(300)  # wait 5 min for c_rsh to timeout
        if process.is_alive():
            module.log('[WARNING] {0} Not responding'.format(process))

    return oslevels

def get_nim_lpp_source(module):
    """
    Get the list of lpp_source defined on the nim master.
    """

    global results

    cmd = ['lsnim', '-t', 'lpp_source', '-l']
    ret, stdout, stderr = module.run_command(cmd)
    if ret != 0:
        module.log('NIM - Error getting the lpp_source list - rc:{0}, error:{1}'
                   .format(ret, stderr))
        results['stdout'] = stdout
        results['stderr'] = stderr
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    # lpp_source list
    lpp_source_list = {}
    for line in stdout.rstrip().split('\n'):
        match_key = re.match(r"^(\S+):", line)
        if match_key:
            obj_key = match_key.group(1)
        else:
            match_loc = re.match(r"^\s+location\s+=\s+(\S+)$", line)
            if match_loc:
                loc = match_loc.group(1)
                lpp_source_list[obj_key] = loc

    return lpp_source_list

def build_nim_node(module):
    """
    Build nim_node dictionary containing nim clients info and lpp sources info.
    """

    global nim_node

    # =========================================================================
    # Build nim lpp_source list
    # =========================================================================
    nim_lpp_sources = get_nim_lpp_source(module)
    nim_node['lpp_source'] = nim_lpp_sources
    module.debug('lpp source list: {0}'.format(nim_lpp_sources))

    # =========================================================================
    # Build nim clients info
    # =========================================================================
    standalones = get_nim_clients_info(module, 'standalone')
    nim_node['standalone'] = standalones
    module.debug('NIM Clients: {0}'.format(standalones))

    vioses = get_nim_clients_info(module, 'vios')
    nim_node['vios'] = vioses
    module.debug('NIM VIOS Clients: {0}'.format(vioses))

    # =========================================================================
    # Build master info
    # =========================================================================
    cstate = get_nim_master_info(module)
    nim_node['master'] = {}
    nim_node['master']['type'] = 'master'
    nim_node['master']['cstate'] = cstate
    module.debug('NIM master: Cstate = {0}'.format(cstate))

def expand_targets(targets):
    """
    Expand the list of target patterns.

    A target pattern can be of the following form:
        target*       all the nim client machines whose names start
                          with 'target'
        target[n1:n2] where n1 and n2 are numeric: target<n1> to target<n2>
        * or ALL      all the nim client machines
        client_name   the nim client named 'client_name'
        master        the nim master

    arguments:
        targets (list): The list of target patterns

    return: the list of existing machines matching the target patterns
    """
    global nim_node

    # ===========================================
    # Build clients list
    # ===========================================
    clients = []
    for target in targets:

        # -----------------------------------------------------------
        # Build target(s) from: range i.e. quimby[7:12]
        # -----------------------------------------------------------
        rmatch = re.match(r"(\w+)\[(\d+):(\d+)\]", target)
        if rmatch:
            name = rmatch.group(1)
            start = rmatch.group(2)
            end = rmatch.group(3)

            for i in range(int(start), int(end) + 1):
                # target_results.append('{0}{1:02}'.format(name, i))
                curr_name = name + str(i)
                if curr_name in nim_node['standalone']:
                    clients.append(curr_name)

            continue

        # -----------------------------------------------------------
        # Build target(s) from: val*. i.e. quimby*
        # -----------------------------------------------------------
        rmatch = re.match(r"(\w+)\*$", target)
        if rmatch:

            name = rmatch.group(1)

            for curr_name in nim_node['standalone']:
                if re.match(r"^%s\.*" % name, curr_name):
                    clients.append(curr_name)

            continue

        # -----------------------------------------------------------
        # Build target(s) from: all or *
        # -----------------------------------------------------------
        if target.upper() == 'ALL' or target == '*':
            clients = list(nim_node['standalone'])
            continue

        # -----------------------------------------------------------
        # Build target(s) from: quimby05 quimby08 quimby12
        # -----------------------------------------------------------
        if (target in nim_node['standalone']) or (target == 'master'):
            clients.append(target)

    return list(set(clients))

def perform_async_customization(module, lpp_source, targets):
    """
    Perform an asynchronous customization of the given target clients,
    applying the given lpp_source.

    return: the return code of the command.
    """

    global results

    module.debug('NIM - perform_async_customization - lpp_spource: {0}, targets: {1} '
                 .format(lpp_source, targets))

    cmd = ['nim', '-o', 'cust',
           '-a', 'lpp_source=' + lpp_source,
           '-a', 'fixes=update_all',
           '-a', 'accept_licenses=yes',
           '-a', 'async=yes']
    cmd += targets

    module.debug('NIM - Command:{0}'.format(cmd))
    results['nim_output'].append('NIM - Command:{0}'.format(' '.join(cmd)))
    results['nim_output'].append('Start updating machine(s) {0} to {1}'
                                 .format(targets, lpp_source))

    do_not_error = False

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['nim_output'].append('{0}'.format(stderr))

    for line in stdout.rstrip().split('\n'):
        line = line.rstrip()
        matched = re.match(r"Either the software is already at the same level as on the media, or",
                           line)
        if matched:
            do_not_error = True

    results['nim_output'].append('NIM - Finish updating {0} asynchronously.'
                                 .format(targets))
    if ret != 0 or do_not_error:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['nim_output'].append('NIM - Error: Command {0} returns above error!'
                                     .format(cmd))

    module.log("Done nim customize operation {0}".format(cmd))

    return ret

def perform_sync_customization(module, lpp_source, target):
    """
    Perform a synchronous customization of the given target client,
    applying the given lpp_source.

    return: the return code of the command.
    """

    global results

    module.debug(
        'NIM - perform_sync_customization - lpp_spource: {0}, target: {1} '
        .format(lpp_source, target))

    cmd = ['nim', '-o', 'cust',
           '-a', 'lpp_source=' + lpp_source,
           '-a', 'fixes=update_all',
           '-a', 'accept_licenses=yes',
           '-a', 'async=no',
           target]

    module.debug('NIM - Command:{0}'.format(cmd))
    results['nim_output'].append('NIM - Command:{0}'.format(' '.join(cmd)))
    results['nim_output'].append('Start updating machine(s) {0} to {1}'
                                 .format(target, lpp_source))

    do_not_error = False

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))

    for line in stdout.rstrip().split('\n'):
        results['nim_output'].append('{0}'.format(line))
        line = line.rstrip()
        matched = re.match(r"^Filesets processed:.*?[0-9]+ of [0-9]+", line)
        if matched:
            results['nim_output'].append('\033[2K\r{0}'.format(line))
            continue
        matched = re.match(r"^Finished processing all filesets.", line)
        if matched:
            results['nim_output'].append('\033[2K\r{0}'.format(line))
            continue

    for line in stderr.rstrip().split('\n'):
        line = line.rstrip()
        matched = re.match(r"^Either the software is already at the same level as on the media, or",
                           line)
        if matched:
            do_not_error = True

    results['nim_output'].append('NIM - Finish updating {0} synchronously.'.format(target))
    if ret != 0 or do_not_error:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['nim_output'].append('NIM - Error: Command {0} returns above error!'
                                     .format(cmd))

    module.log("Done nim customize operation {0}".format(cmd))

    return ret

def list_fixes(target, module):
    """
    Get the list of interim fixes for a specified nim client.

    return: a return code (0 if OK)
            the list of fixes
    """

    global results

    fixes = []
    if target == 'master':
        cmd = ['emgr', '-l']
    else:
        cmd = ['/usr/lpp/bos.sysmgt/nim/methods/c_rsh',
               target,
               '"LC_ALL=C /usr/sbin/emgr -l; echo rc=$?"']

    module.debug('EMGR list - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))

    # remove the rc of c_rsh with echo $?
    if target != 'master':
        s = re.search(r'rc=([-\d]+)$', stdout)
        if s:
            if ret == 0:
                ret = int(s.group(1))
            stdout = re.sub(r'rc=[-\d]+\n$', '', stdout)

    for line in stdout.rstrip().split('\n'):
        line = line.rstrip()
        line_array = line.split(' ')
        matched = re.match(r"[0-9]", line_array[0])
        if matched:
            module.debug('EMGR list - adding fix {0} to fixes list'
                         .format(line_array[2]))
            fixes.append(line_array[2])

    results['nim_output'].append('{0}'.format(stderr))

    if ret != 0:
        module.log("Error: Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['nim_output'].append('EMGR list - Error: Command {0} returns above error!'
                                     .format(cmd))

    return(ret, fixes)

def remove_fix(target, fix, module):
    """
    Remove an interim fix for a specified nim client.

    return: the return code of the command.
    """

    global results

    if target == 'master':
        cmd = ['emgr', '-r', '-L', fix]
    else:
        cmd = ['/usr/lpp/bos.sysmgt/nim/methods/c_rsh',
               target,
               '"/usr/sbin/emgr -r -L {0}; echo rc=$?"'.format(fix)]

    module.debug('EMGR remove - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))

    # remove the rc of c_rsh with echo $?
    if target != 'master':
        s = re.search(r'rc=([-\d]+)$', stdout)
        if s:
            if ret == 0:
                ret = int(s.group(1))
            stdout = re.sub(r'rc=[-\d]+\n$', '', stdout)

    results['nim_output'].append('{0}'.format(stderr))

    if ret != 0:
        module.log("Error: Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['nim_output'].append('EMGR remove - Error: Command {0} returns above error!'
                                     .format(cmd))

    return ret

def find_resource_by_client(module, lpp_type, lpp_time, oslevel_elts):
    """
    Retrieve the good SP or TL resource to associate to the nim client oslevel.

    parameters: lpp_type   SP or TL
                lpp_time   next or latest

    return: the lpp_source found or the current oslevel if not found
    """
    global nim_node

    module.debug('NIM - find resource: {0} {1}'.format(lpp_time, lpp_type))

    lpp_source = ''
    lpp_source_list = sorted(nim_node['lpp_source'].keys())

    if lpp_type == 'tl':
        # reading lpp_source table until we have found the good tl
        for lpp in lpp_source_list:
            lpp_elts = lpp.split('-')
            if lpp_elts[0] != oslevel_elts[0] or lpp_elts[1] <= oslevel_elts[1]:
                continue
            lpp_source = lpp
            if lpp_time == 'next':
                break

    if lpp_type == 'sp':
        # reading lpp_source table until we have found the good sp
        for lpp in lpp_source_list:
            lpp_elts = lpp.split('-')
            if lpp_elts[0] != oslevel_elts[0] \
               or lpp_elts[1] != oslevel_elts[1] \
               or lpp_elts[2] <= oslevel_elts[2]:
                continue
            lpp_source = lpp
            if lpp_time == 'next':
                break

    if (lpp_source is None) or (not lpp_source.strip()):
        # setting lpp_source to current oslevel if not found
        lpp_source = '{0}-{1}-{2}-{3}-lpp_source'.format(oslevel_elts[0], oslevel_elts[1],
                                                         oslevel_elts[2], oslevel_elts[3])
        module.debug('NIM - find resource: server already to the {0} {1}, or no lpp_source were '
                     'found, {2} will be utilized'.format(lpp_time, lpp_type, lpp_source))
    else:
        module.debug('NIM - find resource: found the {0} lpp_source, {1} will be utilized'
                     .format(lpp_time, lpp_source))

    return lpp_source

def nim_update(module, params):
    """
    Update nim clients (targets) with a specified lpp_source.

    In case of updating to the latest TL or SP, the synchronous mode is forced.
    Interim fixes that could block the install are removed.
    """

    global results
    global nim_node

    lpp_source = params['lpp_source']

    async_update = 'no'
    if params['asynchronous']:
        async_update = 'yes'
        log_async = 'asynchronous'
    else:
        log_async = 'synchronous'

    module.log('NIM - {0} update operation on {1} with {2} lpp_source'
               .format(log_async, params['targets'], lpp_source))

    if (params['asynchronous'] and (lpp_source == 'latest_tl'
                                    or lpp_source == 'latest_sp'
                                    or lpp_source == 'next_tl'
                                    or lpp_source == 'next_sp')):
        module.log('[WARNING] Force customization synchronously')
        async_update = 'no'

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    # force interim fixes automatic removal
    if params['force']:
        for target in target_list:
            (ret, fixes) = list_fixes(target, module)
            if ret != 0:
                module.log("Continue to remove as many interim fixes we can")
            for fix in fixes:
                remove_fix(target, fix, module)
                module.log("[WARNING] Interim fix {0} has been automatically removed from {1}"
                           .format(fix, target))

    if async_update == 'yes':   # async update
        if lpp_source not in nim_node['lpp_source']:
            module.log('NIM - Error: cannot find lpp_source {0}'
                       .format(lpp_source))
            results['msg'] = 'NIM - Error: cannot find lpp_source {0}'\
                             .format(nim_node['lpp_source'])
            module.fail_json(**results)
        else:
            module.log('NIM - perform asynchronous software customization for client(s) {0} '
                       'with resource {1}'.format(' '.join(target_list), lpp_source))
            perform_async_customization(module, lpp_source, target_list)

    else:    # synchronous update

        # Get the oslevels of the specified targets only
        oslevels = get_oslevels(module, target_list)
        for (k, val) in oslevels.items():
            if k != 'master':
                nim_node['standalone'][k]['oslevel'] = val
            else:
                nim_node['master']['oslevel'] = val

        for target in target_list:
            # get current oslevel
            cur_oslevel = ''
            if target == 'master':
                cur_oslevel = nim_node['master']['oslevel']
            else:
                cur_oslevel = nim_node['standalone'][target]['oslevel']
            module.debug('NIM - current oslevel: {0}'.format(cur_oslevel))
            if (cur_oslevel is None) or (not cur_oslevel.strip()):
                module.log('[WARNING] Cannot get oslevel for machine {0}'.format(target))
                continue
            cur_oslevel_elts = cur_oslevel.split('-')

            # get lpp source
            new_lpp_source = ''
            if lpp_source == 'latest_tl' or lpp_source == 'latest_sp' \
               or lpp_source == 'next_tl' or lpp_source == 'next_sp':
                lpp_source_array = lpp_source.split('_')
                lpp_time = lpp_source_array[0]
                lpp_type = lpp_source_array[1]
                new_lpp_source = find_resource_by_client(module,
                                                         lpp_type, lpp_time,
                                                         cur_oslevel_elts)
                module.debug('NIM - new_lpp_source: {0}'.format(new_lpp_source))
            else:
                if lpp_source not in nim_node['lpp_source']:
                    module.log('NIM - Error: cannot find lpp_source {0}'
                               .format(lpp_source))
                    results['msg'] = 'NIM - Error: cannot find lpp_source {0}'\
                                     .format(nim_node['lpp_source'])
                    module.fail_json(**results)
                else:
                    new_lpp_source = lpp_source

            # extract oslevel from lpp source
            oslevel_elts = []
            matched = re.match(r"^([0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{4})-lpp_source$",
                               new_lpp_source)
            if matched:
                oslevel_elts = matched.group(1).split('-')
            else:
                module.log('[WARNING] Cannot get oslevel from lpp source name {0}'
                           .format(new_lpp_source))
                continue

            if cur_oslevel_elts[0] != oslevel_elts[0]:
                module.log('[WARNING] Machine {0} has different release than {1}'
                           .format(target, oslevel_elts[0]))
                continue
            if (cur_oslevel_elts[1] > oslevel_elts[1] or cur_oslevel_elts[1] == oslevel_elts[1] and cur_oslevel_elts[2] >= oslevel_elts[2]):
                module.log('[WARNING] Machine {0} is already at same or higher level than {1}'
                           .format(target, '-'.join(oslevel_elts)))
                continue

            module.log('Machine {0} needs upgrade from {1} to {2}'
                       .format(target, cur_oslevel, '-'.join(oslevel_elts)))

            module.log('NIM - perform synchronous software customization for client(s) {0} '
                       'with resource {1}'.format(target, new_lpp_source))
            perform_sync_customization(module, new_lpp_source, target)

    results['changed'] = True

def nim_maintenance(module, params):
    """
    Apply a maintenance operation (commit) on nim clients (targets).
    """

    global results
    global nim_node

    module.log('NIM - {0} maintenance operation on {1}'
               .format(params['operation'], params['targets']))

    target_list = expand_targets(params['targets'])
    module.debug('NIM - Target list: {0}'.format(target_list))

    flag = '-c'  # initialized to commit flag

    for target in target_list:
        module.log('NIM - perform maintenance operation for client {0}'
                   .format(target))
        cmd = []
        if target in nim_node['standalone']:
            cmd = ['nim', '-o', 'maint',
                   '-a', 'installp_flags=' + flag,
                   '-a', 'filesets=ALL',
                   target]
        else:
            cmd = ['/usr/lpp/bos.sysmgt/nim/methods/c_rsh',
                   target,
                   '"/usr/sbin/installp -c all; echo rc=$?"']

        module.debug('NIM - Command:{0}'.format(cmd))
        results['nim_output'].append('NIM - Command:{0}'.format(' '.join(cmd)))

        ret, stdout, stderr = module.run_command(cmd)

        module.log("[RC] {0}".format(ret))
        module.log("[STDOUT] {0}".format(stdout))
        module.log("[STDERR] {0}".format(stderr))

        # remove the rc of c_rsh with echo $?
        if target not in nim_node['standalone']:
            s = re.search(r'rc=([-\d]+)$', stdout)
            if s:
                if ret == 0:
                    ret = int(s.group(1))
                stdout = re.sub(r'rc=[-\d]+\n$', '', stdout)

        results['nim_output'].append('{0}'.format(stderr))

        results['nim_output'].append('NIM - Finished committing {0}.'.format(target))
        if ret != 0:
            module.log("Error: NIM Command: {0} failed with return code {1}"
                       .format(cmd, ret))
            results['nim_output'].append('NIM - Error: Command {0} returns above error!'
                                         .format(cmd))
        else:
            module.log("nim maintenance operation: {0} done".format(cmd))
            results['changed'] = True

def nim_master_setup(module, params):
    """
    Setup a nim master.

    parameter: the device (directory) where to find the lpp source to install
    """

    global results

    module.log('NIM - master setup operation using {0} device'
               .format(params['device']))

    cmd = ['nim_master_setup', '-B',
           '-a', 'mk_resource=no',
           '-a', 'device=' + params['device']]

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_check(module, params):
    """
    Retrieve the oslevel and the Cstate of each nim client
    """

    global results
    global nim_node

    module.log('NIM - check operation')

    if params['targets'] is None:
        # Get the oslevel of all NIM clients and master

        oslevels = get_oslevels(module, nim_node['standalone'])
        for (k, val) in oslevels.items():
            nim_node['standalone'][k]['oslevel'] = val

        oslevels = get_oslevels(module, nim_node['vios'])
        for (k, val) in oslevels.items():
            nim_node['vios'][k]['oslevel'] = val

        oslevels = get_oslevels(module, ['master'])
        nim_node['master']['oslevel'] = oslevels['master']
    else:
        # Get the oslevel for specified targets only

        target_list = expand_targets(params['targets'])
        oslevels = get_oslevels(module, target_list)
        for (k, val) in oslevels.items():
            if k != 'master':
                nim_node['standalone'][k]['oslevel'] = val
            else:
                nim_node['master']['oslevel'] = val

def nim_compare(module, params):
    """
    Compare installation inventory of the nim clients.
    """

    global results

    module.log('NIM - installation inventory comparison for {0} clients'
               .format(params['targets']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    cmd = ['niminv', '-o', 'invcmp',
           '-a', 'targets=' + ','.join(target_list),
           '-a', 'base=any']

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

def nim_script(module, params):
    """
    Apply a script to customize nim client targets.
    """

    global results

    async_script = ''
    if params['asynchronous']:
        async_script = 'yes'
        log_async = 'asynchronous'
    else:
        async_script = 'no'
        log_async = 'synchronous'

    module.log('NIM - {0} customize operation on {1} with {2} script'
               .format(log_async, params['targets'], params['script']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    cmd = ['nim', '-o', 'cust',
           '-a', 'script=' + params['script'],
           '-a', 'async=' + async_script]
    cmd += target_list

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_allocate(module, params):
    """
    Allocate a resource to specified nim clients.
    """

    global results

    module.log('NIM - allocate operation on {0} for {1} lpp source'
               .format(params['targets'], params['lpp_source']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    cmd = ['nim', '-o', 'allocate',
           '-a', 'lpp_source=' + params['lpp_source']]
    cmd += target_list

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_deallocate(module, params):
    """
    Deallocate a resource for specified nim clients.
    """

    global results

    module.log('NIM - deallocate operation on {0} for {1} lpp source'
               .format(params['targets'], params['lpp_source']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    cmd = ['nim', '-o', 'deallocate',
           '-a', 'lpp_source=' + params['lpp_source']]
    cmd += target_list

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_bos_inst(module, params):
    """
    Install a given list of nim clients.
    A specified group resource is used to install the nim clients.
    If specified, a script is applied to customize the installation.
    """

    global results

    module.log('NIM - bos_inst operation on {0} using {1} resource group'
               .format(params['targets'], params['group']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    cmd = ['nim', '-o', 'bos_inst',
           '-a', 'source=mksysb',
           '-a', 'group=' + params['group']]
    if params['script'] and params['script'].strip():
        cmd += ['-a', 'script=' + params['script']]
    cmd += target_list

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_define_script(module, params):
    """
    Define a script nim resource.
    A script resource is defined using the specified script location.
    """

    global results

    module.log(
        'NIM - define script operation for {0} ressource with location {1}'
        .format(params['resource'], params['location']))

    cmd = ['nim', '-o', 'define', '-t', 'script',
           '-a', 'location=' + params['location'],
           '-a', 'server=master',
           params['resource']]

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_remove(module, params):
    """
    Remove a nim resource from the nim database.
    The location of the resource is not destroyed.
    """

    global results

    module.log('NIM - remove operation on {0} resource'
               .format(params['resource']))

    cmd = ['nim', '-o', 'remove', params['resource']]

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['changed'] = True

def nim_reset(module, params):
    """
    Reset the Cstate of a nim client.
    If the Cstate of the nim client is already in ready state, a warning is
        issued and no operation is made for this client.
    """

    global results
    global nim_node

    module.log('NIM - reset operation on {0} resource'
               .format(params['targets']))

    module.debug('NIM - force is {0}'.format(params['force']))

    target_list = expand_targets(params['targets'])

    module.debug('NIM - Target list: {0}'.format(target_list))

    # remove from the list the targets that are already in "ready' state
    targets_to_reset = []
    targets_discarded = []
    for target in target_list:
        if nim_node['standalone'][target]['cstate'] != 'ready for a NIM operation':
            targets_to_reset.append(target)
        else:
            targets_discarded.append(target)

    if targets_discarded:
        module.log('[WARNING] The following targets are already in a correct state: {0}'
                   .format(','.join(targets_discarded)))

    if targets_to_reset:
        cmd = ['nim']
        if params['force']:
            cmd += ['-F']
        cmd += ['-o', 'reset']
        cmd += targets_to_reset

        module.debug('NIM - Command:{0}'.format(cmd))
        results['nim_output'].append('NIM - Command:{0}'.format(' '.join(cmd)))

        ret, stdout, stderr = module.run_command(cmd)

        module.log("[RC] {0}".format(ret))
        module.log("[STDOUT] {0}".format(stdout))
        module.log("[STDERR] {0}".format(stderr))
        results['stdout'] = stdout
        results['stderr'] = stderr

        if ret != 0:
            module.log("Error: NIM Command: {0} failed with return code {1}"
                       .format(cmd, ret))
            results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
            module.fail_json(**results)

        results['changed'] = True

def nim_reboot(module, params):
    """
    Reboot the given nim clients if they are running.
    """

    global results

    module.log('NIM - reboot operation on {0}'.format(params['targets']))

    target_list = expand_targets(params['targets'])
    if not target_list:
        results['msg'] = 'No matching target found for {0}.'.format(params['targets'])
        module.fail_json(**results)

    module.debug('NIM - Target list: {0}'.format(target_list))

    if 'master' in target_list:
        module.log('[WARNING] master can not be rebooted, master is discarded from the target list')
        target_list.remove('master')
        if not target_list:
            return

    cmd = ['nim', '-o', 'reboot']
    cmd += target_list

    module.debug('NIM - Command:{0}'.format(cmd))

    ret, stdout, stderr = module.run_command(cmd)

    module.log("[RC] {0}".format(ret))
    module.log("[STDOUT] {0}".format(stdout))
    module.log("[STDERR] {0}".format(stderr))
    results['stdout'] = stdout
    results['stderr'] = stderr

    if ret != 0:
        module.log("Error: NIM Command: {0} failed with return code {1}"
                   .format(cmd, ret))
        results['msg'] = 'Command \'{0}\' failed with return code {1}.'.format(' '.join(cmd), ret)
        module.fail_json(**results)

    results['msg'] = 'Command \'{0}\' successful.'.format(' '.join(cmd))
    results['changed'] = True

def main():
    global results
    global nim_node

    module = AnsibleModule(
        argument_spec=dict(
            action=dict(type='str', required=True,
                        choices=['update', 'master_setup', 'check', 'compare',
                                 'script', 'allocate', 'deallocate',
                                 'bos_inst', 'define_script', 'remove',
                                 'reset', 'reboot', 'maintenance']),
            description=dict(type='str'),
            lpp_source=dict(type='str'),
            targets=dict(type='list', elements='str'),
            asynchronous=dict(type='bool', default=False),
            device=dict(type='str'),
            script=dict(type='str'),
            resource=dict(type='str'),
            location=dict(type='str'),
            group=dict(type='str'),
            force=dict(type='bool', default=False),
            operation=dict(type='str'),
        ),
        required_if=[
            ['action', 'update', ['targets', 'lpp_source']],
            ['action', 'master_setup', ['device']],
            ['action', 'compare', ['targets']],
            ['action', 'script', ['targets', 'script']],
            ['action', 'allocate', ['targets', 'lpp_source']],
            ['action', 'deallocate', ['targets', 'lpp_source']],
            ['action', 'bos_inst', ['targets', 'group']],
            ['action', 'define_script', ['resource', 'location']],
            ['action', 'remove', ['resource']],
            ['action', 'reset', ['targets']],
            ['action', 'reboot', ['targets']],
            ['action', 'maintenance', ['targets']]
        ]
    )

    results = dict(
        changed=False,
        msg='',
        stdout='',
        stderr='',
        nim_output=[],
    )

    module.debug('*** START ***')

    # =========================================================================
    # Get module params
    # =========================================================================
    lpp_source = module.params['lpp_source']
    targets = module.params['targets']
    asynchronous = module.params['asynchronous']
    device = module.params['device']
    script = module.params['script']
    resource = module.params['resource']
    location = module.params['location']
    group = module.params['group']
    force = module.params['force']
    action = module.params['action']
    operation = module.params['operation']

    params = {}

    description = module.params['description']
    if description is None:
        description = "NIM operation: {0} request".format(action)
    params['description'] = description

    # =========================================================================
    # Build nim node info
    # =========================================================================
    build_nim_node(module)

    if action == 'update':
        params['targets'] = targets
        params['lpp_source'] = lpp_source
        params['asynchronous'] = asynchronous
        params['force'] = force
        nim_update(module, params)

    elif action == 'maintenance':
        params['targets'] = targets
        params['operation'] = operation
        nim_maintenance(module, params)

    elif action == 'master_setup':
        params['device'] = device
        nim_master_setup(module, params)

    elif action == 'check':
        params['targets'] = targets
        nim_check(module, params)

    elif action == 'compare':
        params['targets'] = targets
        nim_compare(module, params)

    elif action == 'script':
        params['targets'] = targets
        params['script'] = script
        params['asynchronous'] = asynchronous
        nim_script(module, params)

    elif action == 'allocate':
        params['targets'] = targets
        params['lpp_source'] = lpp_source
        nim_allocate(module, params)

    elif action == 'deallocate':
        params['targets'] = targets
        params['lpp_source'] = lpp_source
        nim_deallocate(module, params)

    elif action == 'bos_inst':
        params['targets'] = targets
        params['group'] = group
        params['script'] = script
        nim_bos_inst(module, params)

    elif action == 'define_script':
        params['resource'] = resource
        params['location'] = location
        nim_define_script(module, params)

    elif action == 'remove':
        params['resource'] = resource
        nim_remove(module, params)

    elif action == 'reset':
        params['targets'] = targets
        params['force'] = force
        nim_reset(module, params)

    elif action == 'reboot':
        params['targets'] = targets
        nim_reboot(module, params)

    else:
        results['msg'] = 'NIM - Error: Unknown action {0}'.format(action)
        module.fail_json(**results)

    results['nim_node'] = nim_node
    results['msg'] = 'NIM {0} completed successfully'.format(action)
    module.exit_json(**results)

if __name__ == '__main__':
    main()


import sys, os, logging

from .foo import foo
from .bar import bar
from flask import Flask

logger = logging.getLogger('foobar')
hdlr   = logging.FileHandler('/data/foobar.log')
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

app = Flask(__name__)

@app.route("/")
def main():
    logger.debug('Got main')
    return "Foobar!"

@app.route("/foo")
def get_foo():
    logger.debug('Got foo')
    return foo()

@app.route("/bar")
def get_bar():
    logger.debug('Got bar')
    return bar()


from SW_GPTS_Learner import *
from ThreePhasesScenario import *
from Optimizer import *

from tqdm import tqdm
import math
import numpy as np
from matplotlib import pyplot as plt

def compute_ideal(scenarios):
    # Compute the real value of the tru function
    real_values = []
    for scenario in scenarios:
        real_values.append(scenario.phase_values[:number_of_budgets])

    # FIND IDEAL ARMS (to compute regret)
    # Given the real value of the true function, find the ideal combination of arms
    arms = ideal_optimizer.optimize(real_values)

    # Compute the ideal optimal result
    result = 0
    for index, scenario in enumerate(scenarios):
        result += scenario.phase_values[arms[index]]

    return arms, result

number_of_experiments = 5
window_coefficients = [1, 3, 5, 0]

# The budget cap and the possible daily budget that can be set
budget_cap = 27000
daily_budgets = np.linspace(5000, budget_cap, endpoint=True, num=45).astype(int)

# The number of possible budgets that can be allocated to each subcampaign
number_of_budgets = 11

time_horizon = 300
phase_duration = int(time_horizon / 3)

regret = []

fig, ax = plt.subplots(2, 2, figsize=(24, 18))

for i in range(0, len(window_coefficients)):

    # See slide 67 1-09
    window_coefficient = window_coefficients[i]
    window_length = int(window_coefficient * math.sqrt(time_horizon))

    for e in range(0, number_of_experiments):

        print('\n')
        print('Starting experiment', e + 1, '(c=', window_coefficients[i], ')')

        scenarios = [ThreePhasesScenario(daily_budgets=daily_budgets,
                                         campaign=0,
                                         phase_duration=phase_duration),
                     ThreePhasesScenario(daily_budgets=daily_budgets,
                                         campaign=1,
                                         phase_duration=phase_duration),
                     ThreePhasesScenario(daily_budgets=daily_budgets,
                                         campaign=2,
                                         phase_duration=phase_duration)]

        subcampaigns = [SW_GPTS_Learner(arms=daily_budgets[:number_of_budgets],
                                        window_length=window_length),
                        SW_GPTS_Learner(arms=daily_budgets[:number_of_budgets],
                                        window_length=window_length),
                        SW_GPTS_Learner(arms=daily_budgets[:number_of_budgets],
                                        window_length=window_length)]

        optimizer = Optimizer(daily_budgets, len(daily_budgets))
        ideal_optimizer = Optimizer(daily_budgets, len(daily_budgets))

        # The optimal and the ideal result obtained by playing the best possible combination of arms known
        optimal_rewards_per_round = np.zeros(time_horizon)
        ideal_rewards_per_round = np.zeros(time_horizon)

        ideal_arms, ideal_result = compute_ideal(scenarios)

        for t in tqdm(range(0, time_horizon)):

            # The ideal result obtainable at time t
            if t % phase_duration == 1:
                ideal_arms, ideal_result = compute_ideal(scenarios)

            # Sample the Gaussian Process for each subcampaign
            sampled_values = []
            for subcampaign in subcampaigns:
                sampled_values.append(subcampaign.sample_values())

            # FIND OPTIMAL ARMS
            # Find the best combination of arms to play at time t based on the sampling of the Gaussian Processes
            optimal_arms = optimizer.optimize(sampled_values)
            # The optimal result obtainable at time t by playing the optimal arms
            optimal_result = 0

            for sub_index in range(0, len(subcampaigns)):
                # Sample the true function to get a reward for the played arm
                reward = scenarios[sub_index].play_round(optimal_arms[sub_index])
                optimal_result += reward

                # Update the model based on the result of the played arm
                subcampaigns[sub_index].update(optimal_arms[sub_index], reward)

                # Advance the time of the scenario and eventually trigger a new phase
                scenarios[sub_index].advance_time()

            optimal_rewards_per_round[t] = optimal_result
            ideal_rewards_per_round[t] = ideal_result
            # print("\n",
            #       "[", t, "]Optimal solution:", optimal_arms, "(", ideal_arms, ")",
            #       "with a result of: ", round(optimal_result, 2), " (", round(ideal_result, 2), ").")

        regret.append(ideal_rewards_per_round - optimal_rewards_per_round)
        print("\n", "Regret:", np.cumsum(ideal_rewards_per_round - optimal_rewards_per_round)[-1], "\n")

    # PLOTTING THE REGRET

    ax[int(i/2), i % 2].set_title("Regret for c = " + str(window_coefficient), fontsize=17, ha='center')

    ax[int(i/2), i % 2].grid(axis='x', alpha=0.3, linestyle='--')

    ax[int(i/2), i % 2].set_ylim(bottom=0)
    ax[int(i/2), i % 2].set_xlim(left=0)

    ax[int(i/2), i % 2].set_xlabel("Day", fontsize=14)
    ax[int(i/2), i % 2].set_ylabel("Regret", fontsize=14)

    ax[int(i/2), i % 2].tick_params(length=0)
    ax[int(i/2), i % 2].set_yticks(np.linspace(0, 40000, 11))
    ax[int(i/2), i % 2].set_yticklabels(np.linspace(0, 40000, 11).astype(np.int64), fontsize=12, alpha=0.7)

    ax[int(i/2), i % 2].spines['right'].set_alpha(0)
    ax[int(i/2), i % 2].spines['left'].set_alpha(0.3)
    ax[int(i/2), i % 2].spines['top'].set_alpha(0)
    ax[int(i/2), i % 2].spines['bottom'].set_alpha(0.3)

    ax[int(i/2), i % 2].axvline(phase_duration*1, color='lightcoral')
    ax[int(i/2), i % 2].axvline(phase_duration*2, color='lightcoral')

    ax[int(i/2), i % 2].plot(np.cumsum(np.mean(regret, axis=0)), linewidth='2')
    ax[int(i/2), i % 2].set_xlim([0, time_horizon])

    # ax[int(i/2), i % 2].legend(frameon=False)

fig.savefig('chapter3_regret.png')

fig.show()


import random
import sqlite3
import numpy as np

from cannier_framework.collate import collate_data_proj

def test_collate_data_proj_items(args, db_file):
    items_input = [
        ("test_a", 0, 10, 0,  10,  0, 0),
        ("test_b", 0, 10, 5,  10,  0, 1),
        ("test_c", 0, 10, 0,  10,  5, 0),
        ("test_d", 0,  9, 0,  10,  0, 1),
        ("test_e", 0, 10, 5,  10,  5, 0),
        ("test_f", 0, 10, 0,  10, 10, 1),
        ("test_g", 0, 10, 10, 10,  5, 0)
    ]

    random.shuffle(items_input)
    args.n_repeats = 0
    args.n_reruns = 10

    with sqlite3.connect(db_file) as con:
        cur = con.cursor()

        cur.execute(
            "replace into counters "
            "values (1, 1, 0, 10, 10)"
        )

        cur.executemany(
            "insert into item "
            "values (null, ?, ?, ?, ?, ?, ?, ?)",
            items_input
        )

        cur.execute(
            "select id, nodeid "
            "from item"
        )

        id_to_nodeid = {
            item_id: nodeid for item_id, nodeid in cur.fetchall()
        }

        id_map, items_output, *_ = collate_data_proj(cur, 0)

    nodeid_to_id = {id_to_nodeid[id1]: id2 for id1, id2 in id_map.items()}
    assert "test_d" not in nodeid_to_id

    order = [
        nodeid_to_id[nodeid] for nodeid in (
            "test_a", "test_b", "test_c", "test_e", "test_f", "test_g"
        )
    ]

    items_expected = np.array([
        [0, 0,  0, 0, 0, 0, 0],
        [0, 5,  0, 1, 0, 0, 0],
        [0, 0,  5, 0, 1, 1, 0],
        [0, 5,  5, 1, 0, 1, 0],
        [0, 0, 10, 0, 1, 1, 0],
        [0, 10, 5, 0, 1, 0, 0],
    ])

    assert (items_output[order] == items_expected).all()

def test_collate_data_proj_features(args, db_file):
    items = [
        ("test_a", 5, 10,  0, 10,  0, 0),
        ("test_b", 5, 10,  0, 10, 10, 1),
        ("test_c", 5,  9,  0, 10,  0, 1),
        ("test_d", 5, 10, 10, 10,  0, 0),
    ]

    features_input = [[i + 1] * 19 for i in range(len(items))] * 5
    random.shuffle(features_input)
    args.n_repeats = 5
    args.n_reruns = 10

    with sqlite3.connect(db_file) as con:
        cur = con.cursor()

        cur.execute(
            "replace into counters "
            "values (1, 1, 5, 10, 10)"
        )

        cur.executemany(
            "insert into item "
            "values (null, ?, ?, ?, ?, ?, ?, ?)",
            items
        )

        cur.executemany(
            "insert into features "
            "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            features_input
        )

        _, _, features_output, _ = collate_data_proj(cur, 0)

    features_expected = np.array(
        [item_id * np.ones((5, 18)) for item_id in (1, 2, 4)]
    )

    assert (features_output == features_expected).all()

def test_collate_data_proj_dependencies(args, db_file):
    items = [
        ("test_a", 0, 5, 0, 5, 0, 1),
        ("test_b", 0, 5, 0, 5, 5, 1),
        ("test_c", 0, 5, 5, 5, 0, 1),
        ("test_d", 0, 5, 0, 5, 2, 1),
    ]

    dependencies_input = [(2, 1), (3, 1), (4, 2)]
    random.shuffle(dependencies_input)
    args.n_repeats = 0
    args.n_reruns = 5

    with sqlite3.connect(db_file) as con:
        cur = con.cursor()

        cur.execute(
            "replace into counters "
            "values (1, 1, 0, 5, 5)"
        )

        cur.executemany(
            "insert into item "
            "values (null, ?, ?, ?, ?, ?, ?, ?)",
            items
        )

        cur.executemany(
            "insert into dependency "
            "values (?, ?)",
            dependencies_input
        )

        _, _, _, dependencies_output = collate_data_proj(con.cursor(), 0)

    dependencies_output = np.unpackbits(dependencies_output, axis=0, count=4)

    dependencies_expected = np.array([
        [0, 0, 0, 0],
        [1, 0, 0, 0],
        [1, 0, 0, 0],
        [0, 1, 0, 0]
    ])

    assert (dependencies_output == dependencies_expected).all()


def calculateTemp(temp, unit):
    temp = float(temp)
    if (unit == 'c' or unit == 'C'):
        return (temp * 9 / 5) + 32
    elif (unit == 'f' or unit == 'F'):
        return (temp - 32) * 5 / 9
    else:
        return "invalid"

def main():
    while True:
        einheit = input("Bitte Einheit angeben: (C°/F°)\n")
        temp = input("Temp eingeben\n")
        print(calculateTemp(temp, einheit))
        einheit = input("Aufhören? (j/n)\n")
        if(einheit == 'j'):
            break

if __name__ == "__main__":
    main()


import cv2, time, pandas
from datetime import datetime
import imutils

first_frame = None
status_list = [None, None]  # to prevent index out of bound error at the beginning
times = []
df = pandas.DataFrame(columns=["Start", "End"])

# video = cv2.VideoCapture(cv2.CAP_DSHOW)
f = "e:\\DEV\\MSVC_Projects\\Astronomy\\test.mp4"
video = cv2.VideoCapture(f)

time.sleep(2)
is_video = True
vs = video
firstFrame = None
frame_num = 0
dx = 127
dy = 255
while True:
    # grab the current frame and initialize the occupied/unoccupied
    # text
    frame = vs.read()
    frame = frame if not is_video else frame[1]

    # if the frame could not be grabbed, then we have reached the end
    # of the video
    if frame is None:
        break

    # resize the frame, convert it to grayscale, and blur it
    frame = imutils.resize(frame, width=700)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (1, 1), 0)

    # if the first frame is None, initialize it
    if firstFrame is None:
        firstFrame = gray
        continue

    # compute the absolute difference between the current frame and
    # first frame
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, dx, dy, cv2.THRESH_BINARY)[1]
    firstFrame = gray

    # dilate the thresholded image to fill in holes, then find contours
    # on thresholded image
    thresh = cv2.dilate(thresh, None, iterations=2)
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)

    # loop over the contours
    for c in cnts:
        # if the contour is too small, ignore it
        # if cv2.contourArea(c) < args["min_area"]:
        t = cv2.contourArea(c)
        if t < 1:
            continue
        if t > 200:
            continue

        # compute the bounding box for the contour, draw it on the frame,
        # and update the text
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.putText(frame, "frame {}".format(frame_num), (0, 20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.putText(frame, "DX={0} DY={1}".format(dx, dy), (0, 40),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

    # show the frame and record if the user presses a key
    cv2.imshow("Security Feed", frame)
    cv2.moveWindow("Security Feed", 0, 0)
    cv2.imshow("Frame Delta", frameDelta)
    cv2.moveWindow("Frame Delta", 0, 400)
    cv2.imshow("Thresh", thresh)
    cv2.moveWindow("Thresh", 0, 800)
    key = cv2.waitKeyEx(1)

    if key in [0x1b, ord('q')]:
        break
    if key == 0x250000:  # left
        frame_num -= 30
        if (frame_num < 0):
            frame_num = 0
        print("rewind")
        vs.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
    if key == 0x260000:  # up
        dx += 1
        if (dx > 255):
            dx = 255
    if key == 0x280000:  # down
        dx -= 1
        if (dx < 0):
            dx = 0
    if key == 0x2d:  # -
        dy -= 10
        if (dy < 0):
            dy = 0
    if key == 0x2b:  # +
        dy += 10
        if (dy > 255):
            dy = 255
        pass
    if key >= 0:
        print(hex(key))

    frame_num += 1
    if(frame_num>300):
        frame_num=0
        print("rewind")
        vs.set(cv2.CAP_PROP_POS_FRAMES, frame_num)

print(status_list)
print(times)

video.release()
cv2.destroyAllWindows()


import pygame

window = pygame.display.set_mode((500,500))
red = (200,0,0)
rect = (70, 90, 100, 150) # x,y,width,height
rect2 = (335, 90, 100, 150) # x,y,width,height

active = True

while active:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            active = False
    pygame.draw.ellipse(window, red, rect) # DRAW ELLIPSE
    pygame.draw.ellipse(window, red, rect2)
    pygame.display.update()


from googleplaces import GooglePlaces, types, lang
from math import radians, cos, sin, asin, sqrt
import pprint
import json
import six

pp = pprint.PrettyPrinter()

API_KEY = 'Dontexposethatkey'

google_places = GooglePlaces(API_KEY)

#helper function because JSON doesn't like decimals... at all
def loc_with_float(location):
	loc = {}
	for k,v in location.items():
		loc[k] = float(v)
	return loc

#assembles the results of the query into a client-readable form
#requires client location to compute distance
def build_list(loc, query_result):
	#pp.pprint(query_result.raw_response)
	restaurant_list = {'restaurants': [], 'next_page_token': query_result.raw_response['next_page_token']}
	v = 0
	for place in query_result.places:
		place.get_details() #request additional info
		photo = place.photos[0].get(maxheight=1000)
		restaurant_list['restaurants'].append(
		{'id':v, 'name': place.name, 'distance': str(haversine(float(place.geo_location['lng']), float(place.geo_location['lat']), float(loc['lng']), float(loc['lat'])))[:4] , 'address':place.formatted_address,'photo_url': place.photos[0].url, 'phone':place.local_phone_number})
		v = v + 1
	return restaurant_list

def get_restaurants(location):
	#Get restaurants list from places given arguments
	#Future versions could have more dynamic values
	query_result = google_places.nearby_search(
		lat_lng=location, radius=1620, types=[types.TYPE_FOOD])
	return build_list(location, query_result)

#support for page extension
def get_next_page(loc, token):
	query_result = google_places
	return build_list(loc, query_result)

#shamelessly copied from SO, but we could totally derive this
def haversine(lon1, lat1, lon2, lat2):
#Calculate the great circle distance between two points
#on the earth (specified in decimal degrees)
# convert decimal degrees to radians
	lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
	dlon = lon2 - lon1
	dlat = lat2 - lat1
	a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
	c = 2 * asin(sqrt(a))
	km = 6367 * c
#to miles
	return km / 1.60934


import win32com.client as win32
import Data.VisumPy.helpers2 as helpers
from Data.A_CstesModus import *
import multiprocessing
import os
import pickle as pkl
# from dossiers_simul import *
from Data.read_mat_file import read_mat

from Quatre_Etapes.dossiers_simul import *

def affect(ver, matVP, Iter, H, dir_itern):
    myvisum = win32.Dispatch("Visum.Visum")
    myvisum.LoadVersion(ver)
    helpers.SetODMatrix(myvisum, 'V', matVP)
    myvisum.procedures.Execute()
    mat1 = helpers.GetSkimMatrix(myvisum, 'V', 'V')
    matT = helpers.GetSkimMatrix(myvisum, 'TpsCh', 'V')

    dbfile = open(f'{dir_dataTemp}TV_{H}_scen', 'wb')
    pkl.dump(matT, dbfile)
    dbfile.close()

    # # Kiko - ce que j'ai fait n'a pas de sens, puisque VISUM ne change pas la matrice de demande pendant l'étape.
    # dbfile = open(f'{dir_dataTemp}ModusUVPcarre_{H}_scen', 'wb')
    # pkl.dump(mat1, dbfile)
    # dbfile.close()

    dbfile = open(f'{dir_dataTemp}MODUSCaleUVP_{H}_scen_prec', 'wb')
    pkl.dump(matVP, dbfile)
    dbfile.close()

    myvisum.SaveVersion(os.path.join(dir_itern, f'Vers{H}_scen_iter{Iter}.ver'))
    myvisum = None

    dbfile = open(f'{dir_dataTemp}done_affect{Iter}', 'rb')
    done_affect = pkl.load(dbfile)
    done_affect += 1
    dbfile = open(f'{dir_dataTemp}done_affect{Iter}', 'wb')
    pkl.dump(done_affect, dbfile)
    dbfile.close()
    print(f'Affectation terminé pour {H}')
    return mat1

def affect_PL(H):
    '''Cette fonction lit la matrice de PL projetée et l'intègre dans le fichier VISUM, prêt pour l'affectation
    multiclasse. '''
    read_mat_scen = read_mat(n='scen', per=H)
    matPL = read_mat_scen.CALEPL_func()

    # Il faudra gérer ici les fichiers .ver pour PPM, PCJ, PPS
    myvisum = win32.Dispatch("Visum.Visum")
    myvisum.LoadVersion(Donnees_Res[f'Version_{H}_scen'])

    helpers.SetODMatrix(myvisum, 'P', matPL)

    # Sauvegarde de la version intérmediare contenant les PL
    myvisum.SaveVersion(os.path.join(dir_dataTemp, f'Version_{H}_scen.ver'))
    # pass

# # # This is to test the route assignment code above.
# myvisum = win32.Dispatch("Visum.Visum")
# myvisum.LoadVersion(dir_dataScen + '\\210219_ReseauVPv4.6_PPM2030_editedb.ver')
#
# mat1 = helpers.GetODMatrix(myvisum, 'V')
# mat2 = helpers.GetODMatrix(myvisum, 'P')
# matrand = np.abs(np.random.randn(1327, 1327))
# matVP = mat1 + matrand
# matPL = mat2 + matrand
# myvisum = None
# # myvisum.procedures.Execute()

# mat1 = affect(dir_dataScen + '\\210219_ReseauVPv4.6_PPM2030_editedb.ver', matVP, matPL)

if __name__ == '__main__':

    # mat1 = multiprocessing.Process(name='mat1', target=affect,
    #                                args=(dir_dataScen + '\\210219_ReseauVPv4.6_PPM2030_editedb.ver', matVP, matPL, result))
    # mat2 = multiprocessing.Process(name='mat2', target=affect,
    #                                args=(dir_dataScen + '\\210219_ReseauVPv4.6_PPS2030_edited.ver', matVP, matPL, result))
    # mat1.start()
    # mat2.start()
    affect_PL('PPM')


import tensorflow as tf
import numpy as np
import os
import sys
import gflags

from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import TensorBoard
from keras.utils import plot_model
from keras import backend as K

import logz
import nets
import cifar10_resnet
import utils
import data_utils
import log_utils
from common_flags import FLAGS
from time import time, strftime, localtime
import pdb

# Constants
TRAIN_PHASE = 1

os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin'
def getModel(img_width, img_height, img_channels, output_dim, weights_path):
    """
    Initialize model.

    # Arguments
       img_width: Target image widht.
       img_height: Target image height.
       img_channels: Target image channels. -----------FUTURE NUMBER OF IMAGES
       output_dim: Dimension of model output (number of classes).
       weights_path: Path to pre-trained model.

    # Returns
       model: A Model instance.

    ------------ FUTURE CNN?? TO FIT THE INPUT (MANY IMAGES)
    """
    model = nets.resnet50(img_width, img_height, img_channels, output_dim)

    if weights_path:
        try:
            model.load_weights(weights_path)
            print("Loaded model from {}".format(weights_path))
        except:
            print("Impossible to find weight path. Returning untrained model")

    return model

def getModelResnet(n, version, img_width, img_height, img_channels, output_dim, weights_path):
    """
    Initialize model.

    # Arguments
       n: parameter that determines the net depth.
       version: 1 for resnet v1 or 2 for v2.
       img_width: Target image widht.
       img_height: Target image height.
       img_channels: Target image channels.----- # IMAGES PER BLOCK
       output_dim: Dimension of model output (number of classes).
       weights_path: Path to pre-trained model.

    # Returns
       model: A Model instance.
    """

    # Model parameter
    # ----------------------------------------------------------------------------
    #           |      | 200-epoch | Orig Paper| 200-epoch | Orig Paper| sec/epoch
    # Model     |  n   | ResNet v1 | ResNet v1 | ResNet v2 | ResNet v2 | GTX1080Ti
    #           |v1(v2)| %Accuracy | %Accuracy | %Accuracy | %Accuracy | v1 (v2)
    # ----------------------------------------------------------------------------
    # ResNet20  | 3 (2)| 92.16     | 91.25     | -----     | -----     | 35 (---)
    # ResNet32  | 5(NA)| 92.46     | 92.49     | NA        | NA        | 50 ( NA)
    # ResNet44  | 7(NA)| 92.50     | 92.83     | NA        | NA        | 70 ( NA)
    # ResNet56  | 9 (6)| 92.71     | 93.03     | 93.01     | NA        | 90 (100)
    # ResNet110 |18(12)| 92.65     | 93.39+-.16| 93.15     | 93.63     | 165(180)
    # ResNet164 |27(18)| -----     | 94.07     | -----     | 94.54     | ---(---)
    # ResNet1001| (111)| -----     | 92.39     | -----     | 95.08+-.14| ---(---)
    # ---------------------------------------------------------------------------

    # Model version
    # Orig paper: version = 1 (ResNet v1), Improved ResNet: version = 2 (ResNet v2)

    # Computed depth from supplied model parameter n
    if version == 1:
        depth = n * 6 + 2
    elif version == 2:
        depth = n * 9 + 2

#    model = nets.resnet50(img_width, img_height, img_channels, output_dim)
    input_shape = (img_height, img_width, img_channels);

    if version == 2:
        model = cifar10_resnet.resnet_v2(input_shape=input_shape, depth=depth, num_classes=output_dim)
    else:
        model = cifar10_resnet.resnet_v1(input_shape=input_shape, depth=depth, num_classes=output_dim)

    # Model name, depth and version
    model_type = 'ResNet%dv%d' % (depth, version)
    print(model_type)
    print(model.summary())

    if weights_path:
        try:
            model.load_weights(weights_path)
            print("Loaded model from {}".format(weights_path))
        except:
            print("Impossible to find weight path. Returning untrained model")

    return model

def trainModel(train_data_generator, val_data_generator, model, initial_epoch):
    """
    Model training.

    # Arguments
       train_data_generator: Training data generated batch by batch.
       val_data_generator: Validation data generated batch by batch.
       model: A Model instance.
       initial_epoch: Epoch from which training starts.
    """
    # Configure training process
    model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=cifar10_resnet.lr_schedule(0)),
              metrics=['categorical_accuracy'])

    # Save model with the lowest validation loss
    weights_path = os.path.join(FLAGS.experiment_rootdir, 'weights_{epoch:03d}.h5')
    writeBestModel = ModelCheckpoint(filepath=weights_path, monitor='val_loss',
                                     save_best_only=True, save_weights_only=True)

    # Save training and validation losses.
    logz.configure_output_dir(FLAGS.experiment_rootdir)
    saveModelAndLoss = log_utils.MyCallback(filepath=FLAGS.experiment_rootdir)

    # Train model
    steps_per_epoch = int(np.ceil(train_data_generator.samples / FLAGS.batch_size))
    validation_steps = int(np.ceil(val_data_generator.samples / FLAGS.batch_size))-1

    lr_scheduler = LearningRateScheduler(cifar10_resnet.lr_schedule)

    lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=5,
                                   min_lr=0.5e-6)

    #tensorboard = TensorBoard(log_dir="logs/{}".format(time()))
    strTime = strftime("%Y%b%d_%Hh%Mm%Ss", localtime(time()))
#    tensorboard = TensorBoard(log_dir="logs/{}".format(strTime), histogram_freq=10,
#                              batch_size=32, write_graph=False, write_grads=True,
#                              write_images=False, embeddings_freq=0,
#                              embeddings_layer_names=None, embeddings_metadata=None)
    tensorboard = TensorBoard(log_dir="logs/{}".format(strTime), histogram_freq=0)
    # TENSORBOARD SIRVE PARA VISUALIZAR LOS RESULTADOS DE LAS ITERACIONES
    # HASTA AQUI SE HARÁ UN PROCESO ITERATIVO QUE GUARDARÁ DE TODOS LOS CASOS
    # EL MODELO CON EL MEJOR RESULTADO SOBRE LOS DATOS DE VALIDACIÓN

    callbacks = [writeBestModel, saveModelAndLoss, lr_reducer, lr_scheduler, tensorboard]

    model.fit_generator(train_data_generator,
                        epochs=FLAGS.epochs, steps_per_epoch = steps_per_epoch,
                        callbacks=callbacks,
                        validation_data=val_data_generator,
                        validation_steps = validation_steps,
                        initial_epoch=initial_epoch) # INITIAL EPOCH: POR SI EL ENTRENAMIENTO SE QUEDA A MEDIAS Y QUIERES QUE SIGA
    # CON EL MEJOR MODELO, SE ENTRENA

def _main():

    # Set random seed
    if FLAGS.random_seed:
        seed = np.random.randint(0,2*31-1)
    else:
        seed = 5
    np.random.seed(seed)
    tf.set_random_seed(seed)

    # Set training phase
    K.set_learning_phase(TRAIN_PHASE)

    # Create the experiment rootdir if not already there: create a model if the name of the one in the parameters doesn't exist
    if not os.path.exists(FLAGS.experiment_rootdir):
        os.makedirs(FLAGS.experiment_rootdir)

    # Input image dimensions: CAMBIAR EN COMMON_FLAGS : ESTA POR DEFECTO
    img_width, img_height = FLAGS.img_width, FLAGS.img_height

    # Image mode (RGB or grayscale)
    if FLAGS.img_mode=='rgb':
        img_channels = 3
    elif FLAGS.img_mode == 'grayscale':
        img_channels = 1
    else:
        raise IOError("Unidentified image mode: use 'grayscale' or 'rgb'")

    # Output dimension (7 classes/gestures): CAMBIAR SEGUN LAS CLASES DE LA BASE DE DATOS
    num_classes = 7

    # Generate training data with real-time augmentation: HABRÁ QUE CAMBIARLO
    # TODO LO QUE SE HAGA SOBRE LOS DATOS AQUI SE TENDRA QUE HACER EN LOS DATOS PARA EL TEST
    train_datagen = data_utils.DataGenerator(rescale = 1./255)

    # Iterator object containing training data to be generated batch by batch
    # ESTA ES LA FUNCIÓN QUE TENDREMOS QUE TOCAR. BATCH-SIZE: CUANTOS DATOS PASAMOS EN CADA "TANDA" POR PROBLEMAS DE MEMORIA
    train_generator = train_datagen.flow_from_directory(FLAGS.train_dir,
                                                        num_classes,
                                                        shuffle = True,
                                                        img_mode = FLAGS.img_mode,
                                                        target_size=(img_height, img_width),
                                                        batch_size = FLAGS.batch_size)

    # Check if the number of classes in dataset corresponds to the one specified
    assert train_generator.num_classes == num_classes, \
                        " Not macthing output dimensions in training data."

    # Generate validation data with real-time augmentation
    val_datagen = data_utils.DataGenerator(rescale = 1./255)

    # Iterator object containing validation data to be generated batch by batch
    val_generator = val_datagen.flow_from_directory(FLAGS.val_dir,
                                                    num_classes,
                                                    shuffle = False,
                                                    img_mode = FLAGS.img_mode,
                                                    target_size=(img_height, img_width),
                                                    batch_size = FLAGS.batch_size)

    # Check if the number of classes in dataset corresponds to the one specified
    assert val_generator.num_classes == num_classes, \
                        " Not matching output dimensions in validation data."

    # Weights to restore
    weights_path = FLAGS.weights_fname

    # Epoch from which training starts
    initial_epoch = 0
    if not FLAGS.restore_model:
        # In this case weights are initialized randomly
        weights_path = None
    else:
        # In this case weigths are initialized as specified in pre-trained model
        initial_epoch = FLAGS.initial_epoch

    # Define model: SE DEFINE LA RED CON LOS PARAMETROS QUE QUERAMOS O SOBRE LOS PESOS QUE YA TENGAMOS ANTES
    n = 1
    version = 1 # 1 o 2
    model = getModelResnet(n, version, img_width, img_height, img_channels,
                    num_classes, weights_path)

    # Save the architecture of the network as png: IMAGEN DE LA ESTRUCTURA DE LA RED
    plot_arch_path = os.path.join(FLAGS.experiment_rootdir, 'architecture.png')
    plot_model(model, to_file=plot_arch_path)

    # Serialize model into json: SE GUARDA EL "ESQUELETO" DE LA RED: NUMERO DE CAPAS, NEURONAS... LOS PESOS SE GUARDAN A PARTE
    json_model_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.json_model_fname)
    utils.modelToJson(model, json_model_path)

    # Train model
    trainModel(train_generator, val_generator, model, initial_epoch)

    # Plot training and validation losses
    utils.plot_loss(FLAGS.experiment_rootdir)

def main(argv):
    # Utility main to load flags
    try:
      argv = FLAGS(argv)  # parse flags
    except gflags.FlagsError:
      print ('Usage: %s ARGS\\n%s' % (sys.argv[0], FLAGS))

      sys.exit(1)
    _main()

if __name__ == "__main__":
    main(sys.argv)


from eve.utils import config
from superdesk import get_resource_service
from planning.common import ASSIGNMENT_WORKFLOW_STATE
from copy import deepcopy
from flask_babel import lazy_gettext

def update_on_assign_id(item, **kwargs):
    assign_id = item.get("assignment_id")
    if assign_id:
        assignments_service = get_resource_service("assignments")
        assignment = assignments_service.find_one(req=None, _id=assign_id)
        if assignment is None:
            return item
        if assignment["assigned_to"]["state"] == ASSIGNMENT_WORKFLOW_STATE.ASSIGNED:
            updates = {"assigned_to": deepcopy(assignment.get("assigned_to"))}
            updates["assigned_to"]["state"] = ASSIGNMENT_WORKFLOW_STATE.IN_PROGRESS
            assignments_service.patch(assignment[config.ID_FIELD], updates)
    return item

name = "assign_status"
label = lazy_gettext("Update Status On Assignment ID")
callback = update_on_assign_id
access_type = "backend"
action_type = "direct"


from typing import List

"""
1. Recursively visit each node to the end of the linked-list.
2. Swap the pointers
    a. next node points to head node
    b. head node points to previous node
3. Return the next node to be used as previous node in the next stack
4. Finally return the new head
"""

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        return self.swap(head)

    def swap(self, head: ListNode):
        if head == None or head.next == None:
            return head

        prev_node = self.swap(head.next.next)
        next_node = head.next

        head.next.next = head
        head.next = prev_node

        return next_node


"""
Project: Twiptter
Author: iHazzu
Function: Collects posts from Twitter and publishes to a WordPress site
Date: 07/02/2022
"""

from datetime import timedelta, datetime
from twitter import search_tweets
from wordpress import publish_tweet
import logging

# This script should run every 10 minutes
now = datetime.utcnow()
last_search = now - timedelta(minutes=10)
tweets_list = []
try:
    tweets_list = search_tweets(last_search)
    for tweet in tweets_list:   # post tweets on Wordpress
        publish_tweet(tweet)
except Exception as error:
    logging.basicConfig(filename="logs/main_logs.log", level=logging.ERROR)
    logging.exception(f" {datetime.utcnow()} - EXCEPTION: {error}\nTWEETS: {tweets_list}")