import pandas as pd
import numpy as np
import tensorflow as tf

from env import Env
from predict import predict

from rl import (Actor, Critic)

LR_A = 0.001    # learning rate for actor
LR_C = 0.01     # learning rate for critic
MAX_EPISODE = 25
N_F = 3  # mean & curve & block
N_A = 5  # 0, 1, 2, 3, 4

Train_df = pd.read_csv('./data/google/train.csv',
                       names=["Open", "High", "Low", "Close"])
Test_df = pd.read_csv('./data/google/test.csv',
                      names=["Open", "High", "Low", "Close"])
env = Env(Train_df)

sess = tf.Session()

actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A)
# we need a good teacher, so the teacher should learn faster than the actor
critic = Critic(sess, n_features=N_F, lr=LR_C)

sess.run(tf.global_variables_initializer())

# Training
for i_episode in range(MAX_EPISODE):
    s = np.array([0, 0, 0])
    t = 0
    track_r = []
    while True:
        a = actor.choose_action(s)

        s_, r, done = env.step(t, s, a)

        if (done):
            ep_rs_sum = sum(track_r)
            print("episode:", i_episode, "  reward:", int(ep_rs_sum))
            break

        else:
            track_r.append(r)

            # gradient = grad[r + gamma * V(s_) - V(s)]
            td_error = critic.learn(s, r, s_)
            # true_gradient = grad[logPi(s,a) * td_error]
            actor.learn(s, a, td_error)

            s = s_
            t += 1

s = np.array([0, 0, 0])

error = 0
correct = 0
error_1 = 0
error_2 = 0

# user state
trend_block = env.get_trend_block()
predict = predict(trend_block)
hold = 0
money = 0

for day in range(len(Test_df['Open'])):
    # Predict Trend
    trend = actor.choose_action(s)

    # Buy or Sold
    action = predict.action(hold, trend)
    #
    # New Day
    #
    price = Test_df['Open'][day]
    predict.push_data(price)

    # Check money after day 0 (day start from 0)
    if(day > 0):
        print("day: ", day - 1, "state: ", s, " ------ today: ", (s[2] - 2), "predict tomorrow: ", (
            trend - 2), " ------- hold: ", hold, " action tomorrow: ", action, "money: ", money)
        money, hold = predict.check_money(hold, action, money, price)

    s = predict.get_new_state(day, s)

    if(day + 1 == len(Test_df['Open'])):
        print("day: ", day, "state: ", s, " ------ today: ", (s[2] - 2), "predict tomorrow: ", (
            trend - 2), " ------- hold: ", hold, " action tomorrow: ", action, "money: ", money)
        if (hold == 1):
            money += Test_df['Close'][day]
        elif (hold == -1):
            money -= Test_df['Close'][day]
        print("final money: ", money)

    # error check
    if(np.absolute(trend - s[2]) == 0):
        correct += 1
    elif(np.absolute(trend - s[2]) == 1):
        print("1 -- predict: ", trend - 2, " real: ", s[2] - 2)
        error_1 += 1
    else:
        print("2 -- predict: ", trend - 2, " real: ", s[2] - 2)
        error_2 += 1

print("correct: ", correct)
print("error_1: ", error_1)
print("error_2: ", error_2)


import tensorflow as tf
import tensorflow.contrib.slim as slim
from layers import conv_group_nobn, conv_group

def alexnet_argscope(activation=tf.nn.relu, kernel_size=(3, 3), padding='SAME', training=True, w_reg=0.00005,
                     fix_bn=False):
    train_bn = training and not fix_bn
    batch_norm_params = {
        'is_training': train_bn,
        'decay': 0.975,
        'epsilon': 0.001,
        'center': True,
        'fused': train_bn,
    }
    with slim.arg_scope([slim.conv2d],
                        kernel_size=kernel_size,
                        padding=padding,
                        activation_fn=activation,
                        normalizer_fn=slim.batch_norm,
                        normalizer_params=batch_norm_params,
                        weights_regularizer=slim.l2_regularizer(w_reg),
                        biases_initializer=tf.constant_initializer(0.1)):
        with slim.arg_scope([slim.batch_norm], **batch_norm_params):
            with slim.arg_scope([slim.dropout], is_training=training) as arg_sc:
                return arg_sc

class AlexNet:
    def __init__(self, fc_activation=tf.nn.relu, fix_bn=False, pool5=True, pad='SAME'):
        self.fix_bn = fix_bn
        self.fc_activation = fc_activation
        self.use_pool5 = pool5
        self.pad = pad
        self.layers = {}
        self.num_layers = 5

    def classify(self, net, num_classes, reuse=None, training=True, scope='fully_connected'):
        with tf.variable_scope(scope, reuse=reuse):
            with slim.arg_scope(alexnet_argscope(activation=self.fc_activation, padding='SAME', training=training)):
                with slim.arg_scope([slim.conv2d], weights_initializer=tf.truncated_normal_initializer(0.0, 0.005)):
                    enc_shape = net.get_shape().as_list()
                    net = slim.conv2d(net, num_classes, kernel_size=enc_shape[1:3], padding='VALID', scope='fc1',
                                      activation_fn=None,
                                      normalizer_fn=None,
                                      biases_initializer=tf.zeros_initializer())
                    self.layers['fc_1'] = net
                    net = slim.flatten(net)
        return net

    def discriminate(self, net, reuse=None, training=True):
        with tf.variable_scope('discriminator', reuse=reuse):
            with slim.arg_scope(alexnet_argscope(activation=self.fc_activation, padding='SAME', training=training,
                                                 fix_bn=self.fix_bn)):
                self.layers['input'] = net
                net = slim.conv2d(net, 96, kernel_size=[11, 11], stride=4, scope='conv_1', padding=self.pad,
                                  normalizer_fn=None)
                self.layers['conv_1'] = net
                net = slim.max_pool2d(net, kernel_size=[3, 3], stride=2, scope='pool_1', padding=self.pad)
                net = tf.nn.lrn(net, depth_radius=2, alpha=0.00002, beta=0.75)
                net = conv_group_nobn(net, 256, kernel_size=[5, 5], scope='conv_2')
                self.layers['conv_2'] = net
                net = slim.max_pool2d(net, kernel_size=[3, 3], stride=2, scope='pool_2', padding=self.pad)
                net = tf.nn.lrn(net, depth_radius=2, alpha=0.00002, beta=0.75)
                net = slim.conv2d(net, 384, kernel_size=[3, 3], scope='conv_3', normalizer_fn=None)
                self.layers['conv_3'] = net
                net = conv_group_nobn(net, 384, kernel_size=[3, 3], scope='conv_4')
                self.layers['conv_4'] = net
                net = conv_group(net, 256, kernel_size=[3, 3], scope='conv_5')
                self.layers['conv_5'] = net
                if self.use_pool5:
                    net = slim.max_pool2d(net, kernel_size=[3, 3], stride=2, scope='pool_5', padding=self.pad)
                encoded = net

                drop_pred = slim.conv2d(net, 1, kernel_size=[3, 3], padding='SAME', activation_fn=None,
                                        normalizer_fn=None, scope='drop_pred')
                self.layers['drop_pred'] = net
                drop_pred = slim.flatten(drop_pred)

                enc_shape = net.get_shape().as_list()
                net = slim.conv2d(net, 4096, kernel_size=enc_shape[1:3], padding='VALID', scope='fc_1')
                self.layers['fc_1'] = net
                net = slim.dropout(net, 0.5, is_training=training)
                net = slim.conv2d(net, 2, kernel_size=[1, 1], padding='VALID', activation_fn=None,
                                  normalizer_fn=None, scope='fc_2')
                self.layers['fc_2'] = net
                net = slim.flatten(net)

                return net, drop_pred, encoded


import numpy as np
from IPython.display import display, HTML

# Association of the values used in the data storage matrix
YES = 1
NO = 2
MAYBE = 0

class ZebraPuzzle:

    def __init__(self, categories_attributes: dict):
        self.categories_attributes: dict = categories_attributes
        # Prepare some convenience variables
        self.categories: list = list(self.categories_attributes.keys())
        self.number_categories: int = len(self.categories)
        self.number_attributes: int = len(self.categories_attributes[self.categories[0]])

        # Prepare some more convenience variables
        self.attributes = []
        for i, k in enumerate(self.categories):
            for j, n in enumerate(self.categories_attributes[k]):
                self.attributes.append(n.lower())
        self._tot: int = int(self.number_categories * self.number_attributes)

        # Check if each category has same number of attributes
        for k in self.categories_attributes:
            assert len(self.categories_attributes[k]) == self.number_attributes

        # Prepare the data-container
        self._data = np.zeros([self._tot, self._tot], dtype=int)
        # Initialize the trivial data
        assert YES == 1 and NO == 2, "If you want to change the values of YES and NO, please also change the code below accordingly."
        su = 2 * np.ones([self.number_attributes, ] * 2) - np.identity(self.number_attributes)
        for i in range(self.number_categories):
            self._data[i * self.number_attributes:(i + 1) * self.number_attributes,
            i * self.number_attributes:(i + 1) * self.number_attributes] = su

    def get_category(self, x: int) -> int:
        return int(x / self.number_attributes)

    def get_category_from_name(self, name: str) -> int:
        return self.get_category(self.get_id(name))

    def get_id(self, name: str) -> int:
        return self.attributes.index(name.lower())

    def get_assoc_name(self, a: str, b: str) -> int:
        return self.get_assoc(self.get_id(a), self.get_id(b))

    def get_assoc(self, a: int, b: int) -> int:
        assert a != b, "Requesting the association of a attribute with itself does not make sense."
        # assert that a and b are not from the same category
        assert self._data[a, b] == self._data[b, a]
        return self._data[min(a, b), max(a, b)]

    def add_assoc_name(self, name_a: str, name_b: str, assoc: bool):
        k_a = self.get_id(name_a)
        k_b = self.get_id(name_b)
        self.add_assoc(k_a, k_b, assoc)

    def add_assoc(self, a: int, b: int, assoc: bool):
        v = YES if assoc else NO
        assert self.get_assoc(a, b) == MAYBE or self.get_assoc(a, b) == v, \
            f"Try to overwrite field {a, b} with value {self.get_assoc(a, b)}."

        # In case of a positive assoc, we can already remove quite a few possibilities.
        if assoc:
            for i in range(self.number_attributes):
                self._data[self.get_category(a) * self.number_attributes + i, b] = NO
                self._data[a, self.get_category(b) * self.number_attributes + i] = NO
                self._data[b, self.get_category(a) * self.number_attributes + i] = NO
                self._data[self.get_category(b) * self.number_attributes + i, a] = NO

        self._data[a, b] = v
        self._data[b, a] = v

    def generate_matrix_html(self) -> str:
        from IPython.display import display, HTML
        # Some css to make the table look nice
        table = """

    """
        # Now generate the required HTML
        table += '
'
        # Print categories
        table += '
		'
        for ki in range(1, self.number_categories):
            k = self.categories[ki]
            table += f"{k}		"
        table += "

		"
        # Print attributes
        for ki in range(1, self.number_categories):
            k = self.categories[ki]
            for att in self.categories_attributes[k]:
                table += f"{att}	"
            table += "	"
        table += "

"
        # Print main body
        for ki in range(0, self.number_categories - 1):
            # Print categories on left
            k = self.categories[ki]
            table += f"{k}	"
            for i, att in enumerate(self.categories_attributes[k]):
                # Print attributes on left
                it = ki * self.number_attributes + i
                table += f"{att}	"
                for kj in range(1, self.number_categories):
                    for j, att2 in enumerate(self.categories_attributes[k]):
                        # Print values
                        jt = kj * self.number_attributes + j
                        if kj <= ki:
                            c = "-"
                        elif self.get_assoc(jt, it) == YES:
                            c = 'o'
                        elif self.get_assoc(jt, it) == NO:
                            c = 'x'
                        else:
                            c = '.'
                        table += f"{c}	"
                    table += f"|	"
                table += "

"
            table += "-	" * (2 + (self.number_categories - 1) * (self.number_attributes + 1))
            table += "

"
        table += '

'
        table += f"Filled about {int(np.round(self.get_percentage_solved())):d}% of the puzzle."
        return table

    def display_matrix(self):
        # Display output
        html_table = HTML(self.generate_matrix_html())
        display(html_table)

    def _exclude(self, x: int, y: int):
        assert self.get_assoc(x, y) == YES
        kx = self.get_category(x)
        ky = self.get_category(y)
        for xi in range(0, self._tot):
            if xi != y and self.get_assoc(xi, y) == NO:
                if self.get_category(xi) != kx and self.get_category(xi) != ky:
                    if self.get_assoc(x, xi) == MAYBE:
                        print(f"By simple logic '{self.attributes[x]}' and '{self.attributes[xi]}'" +
                              f" are negatively associated.")
                        self.add_assoc(x, xi, False)

    def exclude_all(self):
        for i in range(self._tot):
            for j in range(i + 1, self._tot):
                if self.get_assoc(i, j) == YES:
                    self._exclude(i, j)
                    self._exclude(j, i)

    def find_solved(self):
        for ki in range(self.number_categories):
            for kj in range(ki + 1, self.number_categories):
                sr, er = ki * self.number_attributes, (ki + 1) * self.number_attributes
                for x in range(sr, er):
                    sc, ec = kj * self.number_attributes, (kj + 1) * self.number_attributes
                    d = [r == NO for r in self._data[x, sc:ec]]
                    # print(x, f"{sc}:{ec}", d)
                    if sum(d) == self.number_attributes - 1:
                        for y in range(sc, ec):
                            if self.get_assoc(x, y) == 0:
                                print(f"By simple logic '{self.attributes[x]}' and '{self.attributes[y]}'" +
                                      f" are positively associated.")
                                # print(self._data[x, sc:ec])
                                self.add_assoc(x, y, True)
                # Other way around.
                # TODO: this is ugly code-duplication. Re-implement this at your leisure.
                sc, ec = kj * self.number_attributes, (kj + 1) * self.number_attributes
                for y in range(sc, ec):
                    d = [r == NO for r in self._data[sr:er, y]]
                    # print(x, f"{sc}:{ec}", d)
                    if sum(d) == self.number_attributes - 1:
                        for x in range(sr, er):
                            if self._data[x, y] == 0:
                                print(f"By simple logic '{self.attributes[x]}' and '{self.attributes[y]}'" +
                                      f" are positively associated.")
                                # print(self._data[sc:ec, y])
                                self.add_assoc(x, y, True)

    def iterate_logic(self):
        p = 0
        while self.get_percentage_solved() > p:
            p = self.get_percentage_solved()
            self.exclude_all()
            self.find_solved()

    def get_percentage_solved(self):
        # Estimates the percentage to which this puzzle is solved.
        n = self._tot
        m = self.number_attributes
        n_yes = self.number_categories ** 2 * m
        init = self.number_categories * (m ** 2 - m) * NO + self.number_categories * m * YES

        return 100 * (np.sum(self._data) - init) / (YES * n_yes + NO * (n ** 2 - n_yes) - init)


from flask import Flask, render_template, request, redirect, url_for, abort, \
    send_from_directory
from werkzeug.utils import secure_filename
from PixelBoard import PixelBoard
import image_to_json
import time
import json
import board
import threading
import imghdr
import os

class myBoard(threading.Thread):
    def __init__(self, pBoard):
        threading.Thread.__init__(self)
        self.pixel_board = pBoard
        self.images = self.initImages()
        self.bvalue = 0
        self.loop = False
        self.current_index = 0
        self.length = len(self.images)

    def initImages(self):
        print('loading images')
        with open('/home/pi/Pixel Board/image.json') as f:
            data = json.load(f)
        print('images loaded')
        return data

    def display(self):
        while(True):
            # check if on or off
            if(self.bvalue):
                start = int(time.time())
                temp_index = self.current_index
                # loop till time to move on to next image
                while((int(time.time()) - start < (10)) or self.loop):
                    if(self.current_index != temp_index or not self.bvalue):
                        break
                    for i in range(len(self.images[self.current_index % self.length])):
                        if(self.current_index != temp_index or not self.bvalue):
                            break
                        self.pixel_board.displayImage(
                            self.images[self.current_index % self.length][f'frame {i}'])
                        time.sleep(0.1)
                else:
                    self.current_index = (self.current_index + 1) % self.length
            else:
                self.pixel_board.clear()
                time.sleep(0.1)

    def switch(self):
        self.bvalue = not self.bvalue

    def skip(self):
        self.current_index += 1

    def prev(self):
        self.current_index -=1

    def loopSwitch(self):
        self.loop = not self.loop

    def run(self):
        print('Pixel Board Running')
        while True:
            self.display()

app = Flask(__name__)
my_board = myBoard(PixelBoard(board.D18, 16, 16, 0.6))
app.config['UPLOAD_EXTENSIONS'] = ['.jpg', '.png', '.gif']
app.config['UPLOAD_PATH'] = '/home/pi/Pixel Board/images'

def validate_image(stream):
    header = stream.read(512)
    stream.seek(0)
    format = imghdr.what(None, header)
    if not format:
        return None
    return '.' + (format if format != 'jpeg' else 'jpg')

@app.route('/')
def hello():
    return "Hello World!"

@app.route('/switch')
def switch():
    my_board.switch()
    print('on/off pressed')
    return my_board.bvalue and "ON" or "OFF"

@app.route('/prev')
def prev():
    my_board.prev()
    print('skip button pressed')
    return f'Skipped. Current index: {my_board.current_index}'

@app.route('/next')
def next():
    my_board.skip()
    print('skip button pressed')
    return f'Skipped. Current index: {my_board.current_index}'

@app.route('/loop')
def loop_switch():
    my_board.loopSwitch()
    print('loop button pressed')
    return my_board.loop and "LOOP ON" or "LOOP OFF"

@app.route('/upload')
def index():
    files = os.listdir(app.config['UPLOAD_PATH'])
    return render_template('upload.html', files=files)

@app.route('/upload', methods=['POST'])
def upload_files():
    uploaded_file = request.files['file']
    filename = secure_filename(uploaded_file.filename)
    print(filename)
    if filename != '':
        file_ext = os.path.splitext(filename)[1]
        if file_ext not in app.config['UPLOAD_EXTENSIONS'] or \
                file_ext != validate_image(uploaded_file.stream):
            return "Invalid image", 400
        path = os.path.join(app.config['UPLOAD_PATH'], filename)
        uploaded_file.save(path)
        print(f'File uploaded at {path}')
        print('Processing image')
        image_to_json.imgToJson(path, my_board.images)
        print('image processed')
        my_board.length = len(my_board.images)
        os.system('rm image.json && touch image.json')
        with open('image.json', 'w') as fout:
             json.dump(my_board.images, fout)
        print('image file updated')
    return '', 204

if __name__ == '__main__':
    my_board.start()
    app.run(port=80, host='0.0.0.0', debug=False, use_reloader=False)


class Solution:
    def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
        if startFuel >= target:
            return 0

        n = len(stations)
        # dp[j]: in former i stations, pick j stations to fuel, how far it can mostly reach
        dp = [startFuel] + [0] * n
        rst = sys.maxsize
        for i in range(1, n + 1):
            # since dp[i][j] relates to dp[i - 1][j] and dp[i - 1][j - 1],
            # if updating the compressed 1-d dp array left -> right, dp[j - 1] is updated before dp[j] with row i's dp[i][j - 1] value, which replaced the target value dp[i - 1][j - 1]
            # if updating the compressed 1-d dp array right -> left, dp[j - 1] hasn't been udpated when calculating dp[j], which remains the target value dp[i - 1][j - 1]
            for j in range(i, 0, -1):
                if dp[j - 1] >= stations[i - 1][0]:
                    dp[j] = max(dp[j], dp[j - 1] + stations[i - 1][1])
                if dp[j] >= target:
                    rst = min(rst, j)
        return rst if rst != sys.maxsize else -1


colors = ['coral', 'kiwi', 'yellow']

# x = next(colors)  a list is not an iterabor, so this fails

color_iter = iter(colors)
x = next(color_iter)
print(f"x: {x}")
x = next(color_iter)
print(f"x: {x}")
x = next(color_iter)
print(f"x: {x}")
print('-' * 60)

ecolors = enumerate(colors)
print(f"ecolors: {ecolors}")
print(f"next(ecolors): {next(ecolors)}")
print(f"next(ecolors): {next(ecolors)}")
for i, color in ecolors:
    print(i, color)
print()

ecolors = enumerate(colors)
e_iter = iter(ecolors)
print(ecolors is e_iter)

class CountDown:
    def __init__(self, start_value):
        self._start_value = start_value

    def __iter__(self):
        return self

    def __next__(self):
        if self._start_value == -1:
            raise StopIteration()
        current_value = self._start_value
        self._start_value -= 1
        return current_value

c = CountDown(10)
for num in c:
    print(num)
print('-' * 60)

for num in c:
    print(num)

with open('DATA/mary.txt') as mary_in:
    line1 = next(mary_in)
    print(f"line1: {line1}")
    for raw_line in mary_in:
        line = raw_line.rstrip()
        print(line)
print()


from rest_framework import serializers
from .models import Libro, Categoria

# class LibroSerializer(serializers.Serializer):
#     titulo = serializers.CharField(max_length=255)
#     autor = serializers.CharField(max_length=255)
#     precio = serializers.DecimalField(max_digits=5, decimal_places=2)

class CategoriaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Categoria
        fields = ['id', 'titulo']

class LibroSerializer(serializers.ModelSerializer):
    #categoria = serializers.StringRelatedField(read_only=True)
    categoria_id = serializers.IntegerField(write_only=True)
    categoria = serializers.HyperlinkedRelatedField(
        queryset = Categoria.objects.all(),
        view_name='categoria-detail'
    )
    class Meta:
        model = Libro
        fields = ['id', 'titulo', 'autor', 'precio', 'categoria', 'categoria_id']


from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
module: service_facts
short_description: Return service state information as fact data
description:
     - Return service state information as fact data for various service management utilities.
version_added: "2.5"
requirements: ["Any of the following supported init systems: systemd, sysv, upstart, AIX SRC"]
extends_documentation_fragment:
  -  action_common_attributes
  -  action_common_attributes.facts
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    facts:
        support: full
    platform:
        platforms: posix
notes:
  - When accessing the C(ansible_facts.services) facts collected by this module,
    it is recommended to not use "dot notation" because services can have a C(-)
    character in their name which would result in invalid "dot notation", such as
    C(ansible_facts.services.zuul-gateway). It is instead recommended to
    using the string value of the service name as the key in order to obtain
    the fact data value like C(ansible_facts.services['zuul-gateway'])
  - AIX SRC was added in version 2.11.
author:
  - Adam Miller (@maxamillion)
'''

EXAMPLES = r'''
- name: Populate service facts
  ansible.builtin.service_facts:

- name: Print service facts
  ansible.builtin.debug:
    var: ansible_facts.services
'''

RETURN = r'''
ansible_facts:
  description: Facts to add to ansible_facts about the services on the system
  returned: always
  type: complex
  contains:
    services:
      description: States of the services with service name as key.
      returned: always
      type: complex
      contains:
        source:
          description:
          - Init system of the service.
          - One of C(rcctl), C(systemd), C(sysv), C(upstart), C(src).
          returned: always
          type: str
          sample: sysv
        state:
          description:
          - State of the service.
          - 'This commonly includes (but is not limited to) the following: C(failed), C(running), C(stopped) or C(unknown).'
          - Depending on the used init system additional states might be returned.
          returned: always
          type: str
          sample: running
        status:
          description:
          - State of the service.
          - Either C(enabled), C(disabled), C(static), C(indirect) or C(unknown).
          returned: systemd systems or RedHat/SUSE flavored sysvinit/upstart or OpenBSD
          type: str
          sample: enabled
        name:
          description: Name of the service.
          returned: always
          type: str
          sample: arp-ethers.service
'''

import platform
import re
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.locale import get_best_parsable_locale

class BaseService(object):

    def __init__(self, module):
        self.module = module
        self.incomplete_warning = False

class ServiceScanService(BaseService):

    def gather_services(self):
        services = {}
        service_path = self.module.get_bin_path("service")
        if service_path is None:
            return None
        initctl_path = self.module.get_bin_path("initctl")
        chkconfig_path = self.module.get_bin_path("chkconfig")

        # sysvinit
        if service_path is not None and chkconfig_path is None:
            rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % service_path, use_unsafe_shell=True)
            for line in stdout.split("\n"):
                line_data = line.split()
                if len(line_data) < 4:
                    continue  # Skipping because we expected more data
                service_name = " ".join(line_data[3:])
                if line_data[1] == "+":
                    service_state = "running"
                else:
                    service_state = "stopped"
                services[service_name] = {"name": service_name, "state": service_state, "source": "sysv"}

        # Upstart
        if initctl_path is not None and chkconfig_path is None:
            p = re.compile(r'^\s?(?P<name>.*)\s(?P<goal>\w+)\/(?P<state>\w+)(\,\sprocess\s(?P<pid>[0-9]+))?\s*$')
            rc, stdout, stderr = self.module.run_command("%s list" % initctl_path)
            real_stdout = stdout.replace("\r", "")
            for line in real_stdout.split("\n"):
                m = p.match(line)
                if not m:
                    continue
                service_name = m.group('name')
                service_goal = m.group('goal')
                service_state = m.group('state')
                if m.group('pid'):
                    pid = m.group('pid')
                else:
                    pid = None  # NOQA
                payload = {"name": service_name, "state": service_state, "goal": service_goal, "source": "upstart"}
                services[service_name] = payload

        # RH sysvinit
        elif chkconfig_path is not None:
            # print '%s --status-all | grep -E "is (running|stopped)"' % service_path
            p = re.compile(
                r'(?P<service>.*?)\s+[0-9]:(?P<rl0>on|off)\s+[0-9]:(?P<rl1>on|off)\s+[0-9]:(?P<rl2>on|off)\s+'
                r'[0-9]:(?P<rl3>on|off)\s+[0-9]:(?P<rl4>on|off)\s+[0-9]:(?P<rl5>on|off)\s+[0-9]:(?P<rl6>on|off)')
            rc, stdout, stderr = self.module.run_command('%s' % chkconfig_path, use_unsafe_shell=True)
            # Check for special cases where stdout does not fit pattern
            match_any = False
            for line in stdout.split('\n'):
                if p.match(line):
                    match_any = True
            if not match_any:
                p_simple = re.compile(r'(?P<service>.*?)\s+(?P<rl0>on|off)')
                match_any = False
                for line in stdout.split('\n'):
                    if p_simple.match(line):
                        match_any = True
                if match_any:
                    # Try extra flags " -l --allservices" needed for SLES11
                    rc, stdout, stderr = self.module.run_command('%s -l --allservices' % chkconfig_path, use_unsafe_shell=True)
                elif '--list' in stderr:
                    # Extra flag needed for RHEL5
                    rc, stdout, stderr = self.module.run_command('%s --list' % chkconfig_path, use_unsafe_shell=True)
            for line in stdout.split('\n'):
                m = p.match(line)
                if m:
                    service_name = m.group('service')
                    service_state = 'stopped'
                    service_status = "disabled"
                    if m.group('rl3') == 'on':
                        service_status = "enabled"
                    rc, stdout, stderr = self.module.run_command('%s %s status' % (service_path, service_name), use_unsafe_shell=True)
                    service_state = rc
                    if rc in (0,):
                        service_state = 'running'
                    # elif rc in (1,3):
                    else:
                        if 'root' in stderr or 'permission' in stderr.lower() or 'not in sudoers' in stderr.lower():
                            self.incomplete_warning = True
                            continue
                        else:
                            service_state = 'stopped'
                    service_data = {"name": service_name, "state": service_state, "status": service_status, "source": "sysv"}
                    services[service_name] = service_data
        return services

class SystemctlScanService(BaseService):

    def systemd_enabled(self):
        # Check if init is the systemd command, using comm as cmdline could be symlink
        try:
            f = open('/proc/1/comm', 'r')
        except IOError:
            # If comm doesn't exist, old kernel, no systemd
            return False
        for line in f:
            if 'systemd' in line:
                return True
        return False

    def gather_services(self):
        BAD_STATES = frozenset(['not-found', 'masked', 'failed'])
        services = {}
        if not self.systemd_enabled():
            return None
        systemctl_path = self.module.get_bin_path("systemctl", opt_dirs=["/usr/bin", "/usr/local/bin"])
        if systemctl_path is None:
            return None

        # list units as systemd sees them
        rc, stdout, stderr = self.module.run_command("%s list-units --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True)
        for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line]:

            state_val = "stopped"
            status_val = "unknown"
            fields = line.split()
            for bad in BAD_STATES:
                if bad in fields:  # dot is 0
                    status_val = bad
                    fields = fields[1:]
                    break
            else:
                # active/inactive
                status_val = fields[2]

            # array is normalize so predictable now
            service_name = fields[0]
            if fields[3] == "running":
                state_val = "running"

            services[service_name] = {"name": service_name, "state": state_val, "status": status_val, "source": "systemd"}

        # now try unit files for complete picture and final 'status'
        rc, stdout, stderr = self.module.run_command("%s list-unit-files --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True)
        for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line]:
            # there is one more column (VENDOR PRESET) from `systemctl list-unit-files` for systemd >= 245
            try:
                service_name, status_val = line.split()[:2]
            except IndexError:
                self.module.fail_json(msg="Malformed output discovered from systemd list-unit-files: {0}".format(line))
            if service_name not in services:
                rc, stdout, stderr = self.module.run_command("%s show %s --property=ActiveState" % (systemctl_path, service_name), use_unsafe_shell=True)
                state = 'unknown'
                if not rc and stdout != '':
                    state = stdout.replace('ActiveState=', '').rstrip()
                services[service_name] = {"name": service_name, "state": state, "status": status_val, "source": "systemd"}
            elif services[service_name]["status"] not in BAD_STATES:
                services[service_name]["status"] = status_val

        return services

class AIXScanService(BaseService):

    def gather_services(self):
        services = {}
        if platform.system() != 'AIX':
            return None
        lssrc_path = self.module.get_bin_path("lssrc")
        if lssrc_path is None:
            return None
        rc, stdout, stderr = self.module.run_command("%s -a" % lssrc_path)
        for line in stdout.split('\n'):
            line_data = line.split()
            if len(line_data) < 2:
                continue  # Skipping because we expected more data
            if line_data[0] == "Subsystem":
                continue  # Skip header
            service_name = line_data[0]
            if line_data[-1] == "active":
                service_state = "running"
            elif line_data[-1] == "inoperative":
                service_state = "stopped"
            else:
                service_state = "unknown"
            services[service_name] = {"name": service_name, "state": service_state, "source": "src"}
        return services

class OpenBSDScanService(BaseService):
    def query_rcctl(self, cmd):
        svcs = []

        rc, stdout, stderr = self.module.run_command("%s ls %s" % (self.rcctl_path, cmd))
        if 'needs root privileges' in stderr.lower():
            self.incomplete_warning = True
            return []

        for svc in stdout.split('\n'):
            if svc == '':
                continue
            else:
                svcs.append(svc)

        return svcs

    def gather_services(self):
        services = {}
        self.rcctl_path = self.module.get_bin_path("rcctl")
        if self.rcctl_path is None:
            return None

        for svc in self.query_rcctl('all'):
            services[svc] = {'name': svc, 'source': 'rcctl'}

        for svc in self.query_rcctl('on'):
            services[svc].update({'status': 'enabled'})

        for svc in self.query_rcctl('started'):
            services[svc].update({'state': 'running'})

        # Based on the list of services that are enabled, determine which are disabled
        [services[svc].update({'status': 'disabled'}) for svc in services if services[svc].get('status') is None]

        # and do the same for those are aren't running
        [services[svc].update({'state': 'stopped'}) for svc in services if services[svc].get('state') is None]

        # Override the state for services which are marked as 'failed'
        for svc in self.query_rcctl('failed'):
            services[svc].update({'state': 'failed'})

        return services

def main():
    module = AnsibleModule(argument_spec=dict(), supports_check_mode=True)
    locale = get_best_parsable_locale(module)
    module.run_command_environ_update = dict(LANG=locale, LC_ALL=locale)
    service_modules = (ServiceScanService, SystemctlScanService, AIXScanService, OpenBSDScanService)
    all_services = {}
    incomplete_warning = False
    for svc_module in service_modules:
        svcmod = svc_module(module)
        svc = svcmod.gather_services()
        if svc is not None:
            all_services.update(svc)
            if svcmod.incomplete_warning:
                incomplete_warning = True
    if len(all_services) == 0:
        results = dict(skipped=True, msg="Failed to find any services. Sometimes this is due to insufficient privileges.")
    else:
        results = dict(ansible_facts=dict(services=all_services))
        if incomplete_warning:
            results['msg'] = "WARNING: Could not find status for all services. Sometimes this is due to insufficient privileges."
    module.exit_json(**results)

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import glob
from collections import OrderedDict
import astropy.io.fits as fits

APEDDIR = '/Users/enoto/Dropbox/enoto/library/atomdb/data/v3.0.9/atomdb_v3.0.9_lineid/APED'

# ion names and atomic number (number of electrons)
ions = {
"C":6,
"N":7,
"O":8,
"Ne":10,
"Na":11,
"Mg":12,
"Al":13,
"Si":14,
"S":16,
"Ar":18,
"Ca":20,
"Fe":26,
"Ni":28}

dump  = 'Ion state  & forbidden & Intercommbination & resonance & Reference       \\\\ \n'
dump += '           &  F or z & I1 or y   & I2 or x  & R or x    &  \\\\ \n'
dump += '           &   (eV)  &   (eV)    &   (eV)   &   (eV)    &  \\\\ \n'
dump += '\\hline \\hline\n'
for ion in OrderedDict(sorted(ions.items(),key=lambda x:x[1])):
	ion_n = '%s_%d' % (ion.lower(),ions[ion]-1)
	lvfits_candidate = glob.glob(
		'%s/%s/%s/%s_LV_v*_a.fits' % (
			APEDDIR,ion.lower(),ion_n,ion_n))
	if len(lvfits_candidate) == 0:
		print('corresponding file does not exit for %s' % ion)
		quit()
	lvfits = lvfits_candidate[0]
	print(lvfits)
	hdu = fits.open(lvfits)
	ion_state = hdu[1].name
	Lya_list = []
	for elv in hdu[1].data:
		if elv['ELEC_CONFIG'] == '1s1 2s1' and elv['S_QUAN'] == 1:
			forbidden = elv['ENERGY']
		if elv['ELEC_CONFIG'] == '1s1 2p1' and elv['S_QUAN'] == 0:
			resonance = elv['ENERGY']
		if elv['ELEC_CONFIG'] == '1s1 2p1' and elv['S_QUAN'] == 1 and elv['LEV_DEG'] == 5:
			intercombination1 = elv['ENERGY']
		if elv['ELEC_CONFIG'] == '1s1 2p1' and elv['S_QUAN'] == 1 and elv['LEV_DEG'] == 3:
			intercombination2 = elv['ENERGY']
		ref = elv['ENERGY_REF']
	dump += '{:<11}& {:8,.2f} &  {:8,.2f} &  {:8,.2f} & {:8,.2f} & {:15} \\\\ \n'.format(ion_state,forbidden,intercombination2,intercombination1,resonance,ref)
dump += '\\hline'
print(dump)


import bpy
def findImages():
    images = []
    for obj in bpy.data.images:
        images.append(obj.name)
        print ("found an image", obj.name)
    return images
def saveImage():
    return None
if __name__ == "__main__":
    assert(saveImage() == None)
    findImages()


# import pandas as pd
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
#
# # Choose a pre-trained model suitable for paraphrasing, for example, T5
# model_name = "t5-base"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
#
# def paraphrase_sentence(sentence):
#     inputs = tokenizer.encode("paraphrase: " + sentence, return_tensors="pt")
#     outputs = model.generate(inputs, max_length=50, num_return_sequences=1, num_beams=5, temperature=1.0)
#     paraphrased_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
#     return paraphrased_sentence
#
# # Read the CSV file containing the input sentences
# input_csv_path = r'C:\Users\WIN10\PycharmProjects\pythonProject\synthetic_final_data_sensitive_analysis.csv'
# output_csv_path = 'paraphrased_sentences.csv'
#
# df = pd.read_csv(input_csv_path)
#
# # Perform paraphrasing on each sentence
# paraphrased_data = []
# for sentence in df['Sentences']:
#     paraphrased_sentence = paraphrase_sentence(sentence)
#     paraphrased_data.append(paraphrased_sentence)
#
# # Create a new DataFrame with the paraphrased sentences
# paraphrased_df = pd.DataFrame({'Paraphrased Sentences': paraphrased_data})
#
# # Write the paraphrased DataFrame to a new CSV file
# paraphrased_df.to_csv(output_csv_path, index=False)
#
# print("Paraphrasing completed. Paraphrased sentences written to:", output_csv_path)
import nltk
import random
from nltk.corpus import wordnet

# Download WordNet dataset if not already present
nltk.download('wordnet')

def get_synonyms(word):
    synonyms = set()
    for syn in wordnet.synsets(word):
        for lemma in syn.lemmas():
            synonyms.add(lemma.name())
    return list(synonyms)

def synonym_replacement(sentence, num_replacements=1):
    words = nltk.word_tokenize(sentence)
    augmented_sentences = [sentence]

    for _ in range(num_replacements):
        selected_word = None
        while not selected_word:
            random_word = nltk.word_tokenize(words[random.randint(0, len(words) - 1)])
            synonyms = get_synonyms(random_word[0])
            if len(synonyms) > 1:
                selected_word = random.choice(synonyms)

        augmented_sentence = " ".join([selected_word if word == random_word[0] else word for word in words])
        augmented_sentences.append(augmented_sentence)

    return augmented_sentences

# Input sentence
original_sentence = "Data augmentation helps improve natural language processing models."

# Perform synonym replacement data augmentation with 3 replacements
augmented_sentences = synonym_replacement(original_sentence, num_replacements=3)

print("Original Sentence:", original_sentence)
print("Augmented Sentences:")
for idx, sentence in enumerate(augmented_sentences, 1):
    print(f"{idx}. {sentence}")