#!/usr/bin/env python
from setuptools import setup, find_packages

__doc__ = "Python wrapper for yr weather service"

install_requires = [
    'xmltodict>=0.9.2',
]

version = '0.0.1.dev0'

PACKAGE_NAME = 'py_yr'

setup(
    name=PACKAGE_NAME,
    version=version,
    author="Sindre Nerdal",
    author_email="Sindre.nerdal@gmail.com",
    url="https://github.com/Matmonsen/py_yr",
    keywords="python yr xml",
    description=__doc__,
    #long_description=read('README.rst') + '\n\n' + read('CHANGELOG.rst'),
    packages=find_packages(),
    install_requires=install_requires,
    py_modules=['py_yr'],
    #tests_require=
    #zip_safe=
    #test_suite=
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        #'Framework :: ',
        #'Intended Audience :: Developers',
        #'License :: OSI Approved :: BSD License',
        #'Topic :: Internet :: WWW/HTTP',
        #'Programming Language :: Python',
        #'Programming Language :: Python :: 3.3',
        #'Programming Language :: Python :: 3.4',
    ],
)


import pandas as pd
import numpy as np
import Random_Forest as RF

# load dataset
credit = pd.read_csv("CreditGame_TRAIN.csv")

target_name = ["DEFAULT","PROFIT_LOSS"] # this dataset has 2 target columns
data = credit.drop(columns=target_name)

# Pre-processing

# mapping of categorical variables
data['TYP_RES'] = data['TYP_RES'].map({'L':0, 'A':1, 'P':2})
data['ST_EMPL'] = data['ST_EMPL'].map({'R':0, 'T':1, 'P':2})

# 'TYP_FIN' column is not informative, only contains one value: 'AUTO'
X = np.array(data.drop(columns=["ID_TRAIN","TYP_FIN"]))
y = credit[target_name[0]]

rf = RF.Random_Forest()
rf_model = rf.Fit(X,y,cat_col=[7,8], num_feat = 3, n_tree = 4, sample_n = None, min_bucket=5, max_size = 4)
pred = rf.Test_Prediction(rf_model, X)


"""Views for Canopact carbon dashboard.

"""

import datetime
import json

from canopact.blueprints.carbon.models.carbon import Carbon
from canopact.blueprints.carbon.models.expense import Expense
from canopact.blueprints.carbon.models.report import Report
from canopact.blueprints.carbon.models.route import Route
from canopact.blueprints.carbon.forms import (
    SearchForm,
    RouteForm,
    JourneysForm,
    DateForm
)
from canopact.blueprints.billing.decorators import (
    subscription_required
)
from canopact.blueprints.user.decorators import (
    email_confirm_required,
    expensify_required
)
from canopact.extensions import db, cache
from flask import (
    Blueprint,
    current_app,
    flash,
    redirect,
    render_template,
    url_for,
    request,
)
from flask_login import current_user, login_required
from sqlalchemy import text

carbon = Blueprint('carbon', __name__, template_folder='templates')

# Dashboard -------------------------------------------------------------------
@cache.cached(timeout=300, key_prefix="date_input")
def get_date_input():
    """Get value from date input form and convert to date.

    Caches date input so that it persists across different levels of
        aggregation.

    Returns:
        date (datetime.datetime): date from input form.

    """
    date = request.form.get('date')
    date = datetime.datetime.strptime(date, "%Y-%m-%d").date()

    return date

@carbon.route('/carbon/dashboard/<agg>', methods=['GET', 'POST'])
# @expensify_required()
@subscription_required
@email_confirm_required()
@login_required
def dashboard(agg):
    """Renders template for the carbon dashboard

    Args:
        agg (str): level of aggregation for the dashboards

    """
    form = DateForm()

    # Date input.
    if form.validate_on_submit():
        cache.clear()
        date = get_date_input()
    else:
        try:
            date = get_date_input()
        except BaseException:
            date = datetime.date.today()

    # Update data in form.
    form.date.data = date

    # KPI cards.
    emissions, prev_emissions, emissions_change = \
        Carbon.emissions_metrics(current_user, agg=agg, date=date)
    per_journeys, prev_per_journeys, per_journeys_change = \
        Carbon.per_journeys_metrics(current_user, agg=agg, date=date)
    cost, prev_cost, cost_change = \
        Carbon.cost_metrics(current_user, agg=agg, date=date)
    cost_per_journey, prev_cost_per_journey, cost_per_journey_change = \
        Carbon.cost_per_journey_metrics(current_user, agg=agg, date=date)

    # Charts.
    journeys = Carbon.group_and_count_journeys_monthly(current_user, agg=agg,
                                                       date=date)
    monthly_carbon = Carbon.group_and_sum_emissions_monthly(current_user,
                                                            agg=agg,
                                                            prev_months=8,
                                                            date=date)
    line = Carbon.emissions_metrics_monthly(current_user, agg=agg,
                                            prev_months=8,
                                            emissions=monthly_carbon,
                                            date=date)

    # Line chart filters.
    emissions_line = line["datasets"]["emissions"]
    per_journey_line = line["datasets"]["per_journey"]
    per_km_line = line["datasets"]["per_km"]

    # Tables.
    transports = Carbon.group_and_count_transport(current_user, agg=agg,
                                                  as_list=True, date=date)
    routes = Carbon.group_and_count_routes(current_user, agg=agg, as_list=True,
                                           date=date)

    return render_template('dashboard/index.html', emissions=emissions,
                           emissions_change=emissions_change,
                           emissions_per_journeys=per_journeys,
                           per_journeys_change=per_journeys_change,
                           cost=cost,
                           cost_change=cost_change,
                           cost_per_journey=cost_per_journey,
                           cost_per_journey_change=cost_per_journey_change,
                           journeys=json.dumps(journeys),
                           monthly_carbon=json.dumps(monthly_carbon),
                           emissions_line=json.dumps(emissions_line),
                           per_journey_line=json.dumps(per_journey_line),
                           per_km_line=json.dumps(per_km_line),
                           routes=routes, transports=transports,
                           form=form)

# Routes Cleaner --------------------------------------------------------------
def get_routes():
    """Fetches routes that do not have a valid origin/destination.

    Orders routes according to sorting selected on the router cleaner table.

    Returns:
        routes (sqlachemy.Query): routes which require origin/destination
            values to be updated.

    """

    sort_by = Route.sort_by(request.args.get('sort', 'expense_created_date'),
                            request.args.get('direction', 'desc'))
    order_values = 'routes.{0} {1}'.format(sort_by[0], sort_by[1])

    routes = db.session.query(Route.id,
                              Route.created_on,
                              Route.expense_id,
                              Route.expense_category,
                              Report.report_name,
                              Expense.expense_merchant,
                              Expense.expense_comment,
                              Expense.expense_created_date) \
        .join(Expense, Route.expense_id == Expense.expense_id) \
        .join(Report, Expense.report_id == Report.report_id) \
        .filter(Expense.user_id == current_user.id) \
        .filter(Route.route_category != 'unit') \
        .filter((Route.origin.is_(None) | Route.destination.is_(None)) |
                (Route.invalid == 1)) \
        .filter(Route.search(request.args.get('q', ''))) \
        .order_by(text(order_values)) \
        .distinct()

    return routes

@carbon.route('/carbon/cleaner')
# @expensify_required()
@subscription_required
@email_confirm_required()
@login_required
def cleaner():
    """Retrieves all expenses with an invalid and renders cleaner template."""

    search_form = SearchForm()
    routes = get_routes()

    return render_template('cleaner/cleaner.html', form=search_form,
                           routes=routes)

@carbon.route('/carbon/cleaner/edit', methods=['GET', 'POST'])
# @expensify_required()
@email_confirm_required()
@login_required
def routes_edit():
    """Opens editor mode to let user enter in valid origin and destination.

    """
    routes = get_routes()
    total = int(routes.count())  # total number of invalid routes.

    journeys_form = JourneysForm()

    # Key for Google Autocomplete API.
    key = current_app.config['DISTANCE_KEY']

    # Iterate over each route submitted on the cleaner.
    if journeys_form.validate_on_submit():
        for i, entry in enumerate(journeys_form.journeys.entries):
            # Get the route id in order to instantiate a Route object.
            id = journeys_form.ids[i]
            r = Route.query.get(id)

            # Parse the fields from the FieldList.
            if entry.data['origin'] == '':
                origin = None
            else:
                origin = entry.data['origin']

            if entry.data['destination'] == '':
                destination = None
            else:
                destination = entry.data['destination']

            if entry.data['return_type']:
                return_type = 'return'
            else:
                return_type = None

            # Update and save the Route model.
            if all(v is not None for v in [origin, destination]):
                r.origin = origin
                r.destination = destination
                r.return_type = return_type
                r.invalid = 0  # Change the invalid flag.
                r.update_and_save(Route, id=id)

        # Clear journeys form.
        while len(journeys_form.journeys.entries) > 0:
            journeys_form.journeys.pop_entry()
        while len(journeys_form.ids) > 0:
            journeys_form.ids.pop()

        flash('Routes has been saved successfully.', 'success')
        return redirect(url_for('carbon.cleaner'))
    else:
        # Clear journeys form.
        while len(journeys_form.journeys.entries) > 0:
            journeys_form.journeys.pop_entry()
        while len(journeys_form.ids) > 0:
            journeys_form.ids.pop()

        for route in routes:
            route_form = RouteForm()
            route_form.origin = None
            route_form.destination = None
            route_form.return_type = None
            journeys_form.ids.append(route.id)
            journeys_form.journeys.append_entry(route_form)

    return render_template('cleaner/edit.html', form=journeys_form,
                           routes=routes, total=total, key=key)


from StringIO import StringIO

from fabric import api
from fabric.contrib.files import exists

UPSTART_TEMPLATE = """
description "Pi-Sensor-RPC-Service"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5

env LOGGLY_TOKEN={loggly_token}
env LOGGLY_SUBDOMAIN={loggly_domain}
env RABBIT_URL={rabbit_url}

script
        cd /home/pi/Pi-Sensor-RPC-Service/app && node main.js
end script
"""

def raspberry_pi():
	api.env.hosts = ['{0}.local'.format(api.prompt('Raspberry Pi:'))]
	api.env.user = 'pi'

def install():
	api.require('hosts', provided_by=[raspberry_pi])

	if exists('/etc/init/sensor-rpc.conf', use_sudo=True):
		print('"sensor-rpc" is already installed, use the "update" task for changes')
		return

	upstart_values = {}
	upstart_values['loggly_token'] = api.prompt("Loggly token:")
	upstart_values['loggly_domain'] = api.prompt("Loggly domain:")
	upstart_values['rabbit_url'] = api.prompt("Rabbit URL:")
	upstart_file = StringIO(UPSTART_TEMPLATE.format(**upstart_values))

	api.sudo('echo Yes, do as I say! | apt-get -y --force-yes install upstart')

	with api.cd('/etc/init'):
		upload = api.put(upstart_file, 'sensor-rpc.conf', use_sudo=True)
		assert upload.succeeded

	api.run('git clone https://github.com/projectweekend/Pi-Sensor-RPC-Service.git')

	with api.cd('~/Pi-Sensor-RPC-Service/app'):
		api.run('npm install')

	api.sudo('service sensor-rpc start')

def update():
	api.require('hosts', provided_by=[raspberry_pi])

	with api.settings(warn_only=True):
		api.sudo('service sensor-rpc stop')

	with api.cd('~/Pi-Sensor-RPC-Service'):
		api.run('git pull origin master')

	with api.cd('~/Pi-Sensor-RPC-Service/app'):
		api.run('npm install')

	api.sudo('service sensor-rpc start')


"""
Title     : Print Function
Subdomain : Introduction
Domain    : Python
Author    : Pavankumar Hegde
Problem   : https://www.hackerrank.com/challenges/python-print/problem

"""

# Solution 1

if __name__ == "__main__":
    n = int(input())
    ar = range(1, n + 1)
    for i in ar:
        print(i, end="")

# Solution 2

"""
Alternate solution:

if __name__ == '__main__':
    n = int(input())
    print("".join([str(i) for i in range(1,n+1)]))

"""


###############################################################################
#
# Filename: regexp_funcs.py
#
# Description: These are all the regex backing functions
#
# Revision History:
#   1-Jun-20: Initial Documentation/Release
#
###############################################################################

import re
import string
from Value import *

def parse_re_opts(vm, opts):

    opts_str = 0

    if vm.get_variable('*', 'scalar')._val:
        opts_str |= re.M

    if opts == None or opts == '':
        return opts_str

    if re.search('i', opts):
        opts_str |= re.I

    if re.search('x', opts):
        opts_str |= re.X

    return opts_str

# builds a range for a transliteration
def build_tr_range(start, end):
    retval = ''
    if start != None and end != None:
        if (start in string.ascii_lowercase and end in string.ascii_lowercase):
            start_index = string.ascii_lowercase.index(start)
            end_index = string.ascii_lowercase.index(end)
            retval = string.ascii_lowercase[start_index:end_index+1]
        elif (start in string.ascii_uppercase and end in string.ascii_uppercase):
            start_index = string.ascii_uppercase.index(start)
            end_index = string.ascii_uppercase.index(end)
            retval = string.ascii_uppercase[start_index:end_index+1]
        elif (start in string.digits and end in string.digits):
            start_index = string.digits.index(start)
            end_index = string.digits.index(end)
            retval = string.digits[start_index:end_index+1]

    if retval != '' and retval != None: return retval

    raise Exception("Invalid transliteration spec")

def transliteration(in_str, spec, repl):
    spec_actual = ''
    last_char = None
    i = 0
    while i < len(spec):
        if spec[i] == '-':
            spec_actual += build_tr_range(last_char, spec[i+1])
            i += 2
            last_char = None
            continue

        if last_char != None: spec_actual += last_char
        last_char = spec[i]
        i += 1

    if last_char != None: spec_actual += last_char

    repl_actual = ''
    last_char = None
    i = 0
    while i < len(repl):
        if repl[i] == '-':
            repl_actual += build_tr_range(last_char, repl[i+1])
            i += 2
            last_char = None
            continue

        if last_char != None: repl_actual += last_char
        last_char = repl[i]
        i += 1

    if last_char != None: repl_actual += last_char

    new_str = ''
    for i in in_str:
        idx = spec_actual.index(i)
        if idx > len(repl_actual)-1:
            idx = len(repl_actual)-1
        new_str += repl_actual[idx]
    return new_str

def do_trans_op(vm, name, index_expr, spec, invert):
    v = None
    idx = None
    cxt = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                cxt = 'hash'
                v = vm.get_variable(name, 'hash')
                v = v[str(idx)].scalar_context()
            else:
                cxt = 'list'
                v = vm.get_variable(name, 'list')
                v = v[int(idx)].scalar_context()
        else:
            cxt = 'scalar'
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        vm.get_variable('_', 'scalar') # get the $_ var

    trans_spec = spec._val['spec']
    repl_spec = spec._val['repl']

    # interpolate it
    vm.perform_interpolated_push(repl_spec)
    repl_spec = vm.stack.pop().stringify()

    ret = transliteration(v.stringify(), trans_spec, repl_spec)

    if (index_expr == True):
        if cxt == 'list':
            v[idx] = Value(ret)
            vm.set_variable(name, v, 'list')
        else:
            v[idx] = Value(ret)
            vm.set_variable(name, v, 'hash')
    else:
        vm.set_variable(name, Value(ret), 'scalar')

    if (bool(ret) ^ invert):
        vm.stack.push(Value(1))
    else:
        vm.stack.push(Value(0))

def do_subs_op(vm, name, index_expr, spec, invert):
    var = None
    v = None
    idx = None
    cxt = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                cxt = 'hash'
                var = vm.get_variable(name, 'hash')
                v = var[str(idx)].scalar_context()
            else:
                cxt = 'list'
                var = vm.get_variable(name, 'list')
                v = var[int(idx)].scalar_context()
        else:
            cxt = 'scalar'
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        name = '_'
        v = vm.get_variable('_', 'scalar') # get the $_ var

    regex = spec._val['spec']
    repl = spec._val['repl']

    # interpolate it
    vm.perform_interpolated_push(regex)
    regex = vm.stack.pop().stringify()

    options = spec._val['opts']
    re_obj = re.compile(regex, parse_re_opts(vm, options))
    ret = re_obj.search(v.stringify())

    # populate $1 thru $10
    for i in range(10):
        vm.set_variable(str(i+1), Value(None), 'scalar')
    if ret and ret.lastindex:
        for i in range(0, ret.lastindex):
            vm.set_variable(str(i+1), Value(ret.group(i+1)), 'scalar')

    if ((bool(ret) ^ invert) and ret.group(0) != ''):
        # interpolate it -- but only if the subs spec isn't delimited by " ' "
        # which is a Perl 1 thing, definitely not in Perl5
        if (spec._val['separator'] != "'"):
            vm.perform_interpolated_push(repl)
            repl = vm.stack.pop().stringify()

        mod_string = None
        num_repls = None
        if ('g' in options):
            mod_string, num_repls = re_obj.subn(repl, v.stringify(), 0)
        else:
            mod_string, num_repls = re_obj.subn(repl, v.stringify(), 1)

        if (index_expr == True):
            if cxt == 'list':
                var[int(idx)] = Value(mod_string)
                vm.set_variable(name, var, 'list')
            else:
                var[str(idx)] = Value(mod_string)
                vm.set_variable(name, var, 'hash')
        else:
            vm.set_variable(name, Value(mod_string), 'scalar')

        vm.stack.push(Value(num_repls))
    else:
        vm.stack.push(Value(0))

def do_match_op(vm, name, index_expr, spec, invert):
    v = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                v = vm.get_variable(name, 'hash')
                v = v[str(idx)].scalar_context()
            else:
                v = vm.get_variable(name, 'list')
                v = v[int(idx)].scalar_context()
        else:
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        vm.get_variable('_', 'scalar') # get the $_ var

    regex = spec._val['spec']
    options = spec._val['opts']
    ret = re.search(regex, v.stringify(), parse_re_opts(vm, options))
    for i in range(10):
        vm.set_variable(str(i+1), Value(None), 'scalar')
    vm.set_variable('+', Value(None), 'scalar')
    if ret and ret.lastindex:
        for i in range(0, ret.lastindex):
            vm.set_variable(str(i+1), Value(ret.group(i+1)), 'scalar')
            vm.set_variable('+', Value(ret.group(i+1)), 'scalar')

    if (bool(ret) ^ invert):
        vm.stack.push(Value(1))
    else:
        vm.stack.push(Value(0))


# @Time    : 2020-6-28
# @Author  : Qiushi Li
# @File    : 8_test_decom.py

import os
import sys
import argparse
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '6'

from setup_task_verify_experiment import *

parser = argparse.ArgumentParser("Test_decom_Params")
parser.add_argument('--log_root', type=str, default='../../EXP', help='log dir')
parser.add_argument('--work_name', type=str, default='test_decom', help='work name, 6_finetune or 7_train_from_scratch')
parser.add_argument('--data_path', type=str, default='', help='dataset path')
parser.add_argument('--load_path', type=str, default='', help='model to be loaded')
parser.add_argument('--load_config', type=str, default='', help='config file path')
args = parser.parse_args()

assert args.load_path != '', "Please specify args.load_path !"
assert args.load_config != '', "Please specify args.load_config !"

if __name__ == '__main__':
    TEST_TXT = BB_TEST_TXT
    COVER_ROOT = os.path.join(args.data_path, 'bb_256_qf75/')
    STEGO_ROOT = os.path.join(args.data_path, 'bb_256_qf75_juniward04/')
    test_gen = snc64.partial(snc64.gen_valid, \
                        COVER_ROOT, STEGO_ROOT, \
                            TEST_TXT, TEST_TXT)

    LOG_DIR = os.path.join(args.log_root, time.strftime("%Y%m%d-%H%M%S-") + args.work_name) # path for a log direcotry

    LOAD_CKPT = args.load_path

    decom_acc_log_path = os.path.join(LOG_DIR, 'logs')
    if not os.path.exists(decom_acc_log_path):
        os.makedirs(decom_acc_log_path)
    decom_acc_log_path = os.path.join(decom_acc_log_path, 'test_decom_acc.txt')

    with open(TEST_TXT) as f:
        test_cover = f.readlines()
        test_cover_list = [a.strip() for a in test_cover]
    test_ds_size = len(test_cover_list) * 2
    print ('test_ds_size: %i'%test_ds_size)

    if test_ds_size % test_batch_size != 0:
        raise ValueError("change batch size for testing!")

    snd.test_dataset(snd.SRNetDecom, test_gen, test_batch_size, test_ds_size, LOAD_CKPT, decom_acc_log_path, args.load_config)


import discord
import os
from discord.ext import commands
from profanity_check import predict
import joblib
import sklearn.externals

class DiscordBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

bot = DiscordBot(command_prefix='>')

@bot.event
async def on_message(message):
    # check for profanity
    if predict([message.content])[0]:
        # delete the message
        await message.delete()
        # send a warning message to the user
        await message.author.send("Please watch your language.")

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')

@bot.command(name='kick', help='Kicks the mentioned user')
async def kick(ctx, member: discord.Member):
    await ctx.guild.kick(member)
    await ctx.send(f'{member} has been kicked')

@bot.command()
async def slowmode(ctx, time: int):
    channel = ctx.channel
    await channel.edit(slowmode_delay=time)
    await ctx.send(f'Slowmode for this channel has been set to {time} seconds.')

@bot.command()
async def warn(ctx, member: discord.Member, *, reason: str):
    await ctx.send(f'{member.mention} has been warned for {reason}')

@bot.command(name='Ban', help='Bans the mentioned user')
async def ban(ctx, member: discord.Member):
    await ctx.guild.ban(member)
    await ctx.send(f'{member} has been banned')

@bot.command(name='Mute', help='Mutes the mentioned user')
async def mute(ctx, member: discord.Member):
    mute_role = discord.utils.get(ctx.guild.roles, name='Muted')
    await member.add_roles(mute_role)
    await ctx.send(f'{member} has been muted')

@bot.command(name='Purge', help='Deletes a specified number of messages')
async def purge(ctx, amount: int):
    await ctx.channel.purge(limit=amount)
    await ctx.send(f'{amount} messages have been deleted')

bot.run(os.environ['BOT_TOKEN'])


import importlib
import inspect
import os

__all__ = [
    'abs_path',
    'asset_path',
    'fully_qualified_name',
    'is_asset_path',
    'is_module_path',
    'is_object_path',
]

def abs_path(path):
    """Get abs. path for ``path``.

    ``path`` may be a relative or absolute file system path or an asset
    path. If ``path`` is already an abs. path, it will be returned as
    is. Otherwise, it will be converted into a normalized abs. path.

    """
    if not os.path.isabs(path):
        if is_asset_path(path):
            path = asset_path(path)
        else:
            path = os.path.expanduser(path)
            path = os.path.normpath(os.path.abspath(path))
    return path

def asset_path(path, *rel_path):
    """Get absolute path to asset in package.

    ``path`` can be just a package name like 'tangled.web' or it can be
    a package name and a relative file system path like
    'tangled.web:some/path'.

    If ``rel_path`` is passed, it will be appended to the base rel. path
    in ``path``.

    Examples::

        >>> asset_path('tangled.util')
        '.../tangled/tangled'
        >>> asset_path('tangled.util:')
        '.../tangled/tangled'
        >>> asset_path('tangled.util:x')
        '.../tangled/tangled/x'
        >>> asset_path('tangled.util', 'x')
        '.../tangled/tangled/x'
        >>> asset_path('tangled.util:x', 'y')
        '.../tangled/tangled/x/y'

    """
    if not (is_asset_path(path) or is_module_path(path)):
        raise ValueError('Path is not an asset path: %s' % path)
    if ':' in path:
        package_name, base_rel_path = path.split(':')
        rel_path = (base_rel_path,) + rel_path
    else:
        package_name = path
    package = importlib.import_module(package_name)
    if not hasattr(package, '__file__'):
        raise ValueError("Can't compute path relative to namespace package")
    package_path = os.path.dirname(package.__file__)
    path = os.path.join(package_path, *rel_path)
    path = os.path.normpath(os.path.abspath(path))
    return path

def fully_qualified_name(obj):
    """Get the fully qualified name for an object.

    Returns the object's module name joined with its qualified name. If
    the object is a module, its name is returned.

    >>> fully_qualified_name(object)
    'builtins.object'
    >>> import tangled.util
    >>> fully_qualified_name(tangled.util)
    'tangled.util'

    """
    if inspect.ismodule(obj):
        return obj.__name__
    return '{}.{}'.format(obj.__module__, obj.__qualname__)

def is_asset_path(path) -> bool:
    """Is ``path`` an asset path like ``package.module:path``?

    If ``path`` is absolute, it will not be considered an asset path.
    Otherwise, it will be considered an asset path if it contains a
    colon *and* the module path contains only valid Python identifiers.
    The file system path to the right of the colon can be empty or any
    string (it's ignored here).

    Examples::

        >>> is_asset_path('/some/abs/path')
        False
        >>> is_asset_path('rel/path')
        False
        >>> is_asset_path('package')
        False
        >>> is_asset_path('package:')
        True
        >>> is_asset_path('package.subpackage:rel/path')
        True
        >>> is_asset_path('package.subpackage:')
        True
        >>> is_asset_path('package.subpackage:rel/path')
        True
        >>> is_asset_path('base.ini')
        False

    """
    if os.path.isabs(path) or ':' not in path:
        return False
    module_path, _ = path.split(':', 1)
    return is_module_path(module_path)

def is_module_path(path) -> bool:
    """Is ``path`` a module path like ``package.module``?

    Examples::

        >>> is_module_path('package')
        True
        >>> is_module_path('package.module')
        True
        >>> is_module_path('.module')
        True
        >>> is_module_path('package.module:obj')
        False
        >>> is_module_path('a/b')
        False
        >>> is_module_path('/a/b')
        False

    """
    return all(p.isidentifier() for p in path.lstrip('.').split('.'))

def is_object_path(path) -> bool:
    """Is ``path`` an object path like ``package.module:obj.path``?

    Examples::

        >>> is_object_path('package.module:obj')
        True
        >>> is_object_path('.module:obj')
        True
        >>> is_object_path('package')
        False
        >>> is_object_path('package:')
        False
        >>> is_object_path('a/b')
        False
        >>> is_object_path('/a/b')
        False

    """
    if ':' not in path:
        return False
    pkg_path, *object_path = path.split(':', 1)
    if object_path:
        object_path = object_path[0]
        return is_module_path(pkg_path) and is_module_path(object_path)
    return False


from . import data_management
from . import network
from . import configs
from . import logs
import os
import time
import tensorflow as tf
import numpy as np
import cv2
import pickle
import glob

__name__ = 'Model'

class Model(object):
    def __init__(self):
        self.configuration = configs.configs()
        self.log_record = logs.logging_init(self.configuration)
        self.data_manager = data_management.data_batch(configs=self.configuration, logs=self.log_record)
        self.log_record['info']('data ready.')
        if not self.configuration.data_generate_mode:
            model_dict = {'Base': network.network_multi_gpu,
                          'Ver1': network.network_output_128,
                          'Ver2': network.network_output_kennel_size_1,
                          'Ver3': network.network_output_down_to_stage_1_deconv_1,
                          'Ver4': network.network_output_256_down_to_stage_1,
                          'Ver5': network.network_pixellink_style_simplified_prediction_module,
                          'Ver6': network.network_TCL,
                          'Ver7': network.network_new_loss,
                          'Ver8': network.network_TCL_old_loss,
                          'Ver9':network.network_output_down_to_stage_1_deconv_1_kernel_1,
                          'Ver10':network.network_geo_balanced,
                          'Ver11':network.network_IB,
                          'Ver12':network.network_IB_TCL}
            self.model = model_dict[self.configuration.model](configs=self.configuration, logs=self.log_record)
        self.initialize_flag = False

    def _initialize_model(self):
        if self.configuration.restore_path:
            self.load()
        else:
            self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))  # TODO GPU control
            self.sess.run(tf.global_variables_initializer())
            self.log_record['info']('model initialized.')

        sum_path = os.path.join(self.configuration.summary_path_directory, "Sum")
        if os.path.isdir(sum_path):
            os.system('rm -r %s' % sum_path)
        self.writer = tf.summary.FileWriter(sum_path, self.sess.graph)
        self.log_record['info']('Tensorboard ready.')
        self.log_record['info']('Training starts.')
        self.pre_model_loss = 100  # recording the loss of the best performing model

        with tf.device('/device:cpu:0'):
            # recording test loss
            self.test_loss = tf.placeholder(dtype=tf.float32, shape=(), name='Test_loss')
            self.test_loss_sum = tf.summary.scalar("test_loss", self.test_loss)
            # image summary for test
            self.reconstructed_map = tf.placeholder(dtype=tf.uint8, shape=(None, None, None, 3), name='reconstructed_image')
            self.original_image = tf.placeholder(dtype=tf.uint8, shape=(None, None, None, 3), name='original_image')
            self.reconstructed_map_sum = tf.summary.image("y_reconstructed_map", self.reconstructed_map)
            self.original_image_sum = tf.summary.image("y_original_image", self.original_image)
            self.image4viz = tf.summary.merge([self.reconstructed_map_sum, self.original_image_sum])

        self.initialize_flag = True
        try:
            self.total_steps = self.sess.run(self.model.global_step)
        except:
            self.total_steps = 0

    def _train_step(self, image_summary=False, image_attribute_summary=False):
        if image_attribute_summary:
            # returning the geo attributes as well
            self.total_loss, _, self.summary, self.summary_image, self.image_attributes_summary_op, self.total_steps = self.sess.run([
                self.model.total_loss,
                self.model.train_step,
                self.model.summary_op,
                self.model.image_summary_op,
                self.model.image_attributes_summary_op,
                self.model.global_step],
                feed_dict={self.model.input_image: self.img_batch,
                           self.model.Labels: self.Labels_batch,
                           self.model.LossWeights: self.Weights_batch
                           })
            self.losses.append(self.total_loss)
            self.writer.add_summary(self.summary, self.total_steps)
            self.writer.add_summary(self.summary_image, self.total_steps)
            self.writer.add_summary(self.image_attributes_summary_op, self.total_steps)
        elif image_summary:
            # writing the image into the sum files
            self.total_loss, _, self.summary, self.summary_image, self.total_steps = self.sess.run(
                [
                    self.model.total_loss,
                    self.model.train_step,
                    self.model.summary_op,
                    self.model.image_summary_op,
                    self.model.global_step],
                feed_dict={self.model.input_image: self.img_batch,
                           self.model.Labels: self.Labels_batch,
                           self.model.LossWeights: self.Weights_batch
                           })
            self.losses.append(self.total_loss)
            self.writer.add_summary(self.summary, self.total_steps)
            self.writer.add_summary(self.summary_image, self.total_steps)
        else:
            # only record losses
            self.total_loss, _, self.summary, self.total_steps = self.sess.run(
                [
                    self.model.total_loss,
                    self.model.train_step,
                    self.model.summary_op,
                    self.model.global_step],
                feed_dict={self.model.input_image: self.img_batch,
                           self.model.Labels: self.Labels_batch,
                           self.model.LossWeights: self.Weights_batch
                           })
            self.losses.append(self.total_loss)
            self.writer.add_summary(self.summary, self.total_steps)

    def _obtain_data(self):
        return self.train_data_generator.get()

    def train(self, dataset='train', eval_per_step=200):
        """
        :param dataset: 'train', or 'Synth'
        :param eval_per_step: frequncy of doing evaluation on test set
        :return:
        """
        if not self.configuration.data_generate_mode:
            if not self.initialize_flag:
                self._initialize_model()
            if self.configuration.evaluate_before_training or self.configuration.test_mode:
                cur_loss = self.evaluate(0)
                test_sum = self.sess.run(self.test_loss_sum, feed_dict={self.test_loss: cur_loss})
                self.writer.add_summary(test_sum, self.total_steps)
            if self.configuration.test_mode:
                return []
            self.log_record['info']('Start to train on %s' % dataset)

            # training data loader initializer
        self.train_data_generator = self.data_manager.data_gen_multiprocess(dataset)
        count = 0
        self.losses = []
        for epoch in range(self.configuration.epochs[dataset]):
            self.log_record['info']('epoch %d starts: ' % epoch)

            if self.configuration.data_generate_mode:
                for i in range(eval_per_step):
                    _ = self._obtain_data()
                    count += self.configuration.batch_size
                print('%d done.' % count)
                continue

            for i in range(eval_per_step):
                start = time.time()

                t0 = time.time()
                self.id_batch, self.img_batch, self.Labels_batch, _, self.Weights_batch = self._obtain_data()
                print('data: %.4f' % (time.time() - t0), end='， ')

                t0 = time.time()
                self._train_step(image_summary=(i % 10 == 0), image_attribute_summary=(i % 200 == 0))
                print('train: %.4f' % (time.time() - t0))

                print('step: %5d, loss: %.5f' % (self.total_steps, self.total_loss), ', time: ', time.time() - start)

            self.log_record['info'](
                "step: %d, average training loss: %.3f" % (self.total_steps, sum(self.losses) / len(self.losses)))
            self.losses = []
            cur_loss = self.evaluate(self.total_steps)
            test_sum = self.sess.run(self.test_loss_sum, feed_dict={self.test_loss: cur_loss})
            self.writer.add_summary(test_sum, self.total_steps)
            self.model.save(self.sess,
                            os.path.join(self.configuration.save_path, self.configuration.code_name + '.cntk'))

            if cur_loss < self.pre_model_loss:
                self.model.save(self.sess, os.path.join(self.configuration.save_path, self.configuration.code_name + '_best_%d.cntk'%self.total_steps))
                if os.path.isdir(self.configuration.test_path + '_best'):
                    os.system('rm -r %s_best' % self.configuration.test_path)
                os.system('cp -r %s %s' % (self.configuration.test_path, self.configuration.test_path + '_best'))
                self.pre_model_loss = cur_loss
        if not self.configuration.data_generate_mode:
            self.model.save(self.sess,
                        os.path.join(self.configuration.save_path, self.configuration.code_name + '_' + dataset + '.cntk'))

        return self.losses

    def evaluate(self, total_steps):
        losses = []
        count = 0
        self.log_record['info']('start to evaluate')
        totaltext_recalls, totaltext_precisions, pascal_recalls, pascal_precisions = [], [], [], []
        self.test_data_generator = self.data_manager.data_test_set_gen(self.configuration.real_test)

        # Warning: the order of the prediction map is [TCL, radius, cos_theta, sin_theta,TR]
        for id_batch, img_batch, Labels_batch, cnt_ground_truth, weights_batch in self.test_data_generator:
            count += 1
            total_loss, summary_image, summary_image_attr, prediction, \
                TR_TCL_GT_map, L_R_map, L_sin, L_cos, \
                TR_TCL_Predictied_map, P_R_map, P_sin, P_cos \
                = self.sess.run([
                    self.model.total_loss,
                    self.model.image_summary_op,
                    self.model.image_attributes_summary_op,
                    self.model.prediction,
                    self.model.TR_TCL_GT_map,
                    self.model.L_R_map,
                    self.model.L_sin,
                    self.model.L_cos,
                    self.model.TR_TCL_Predictied_map,
                    self.model.P_R_map,
                    self.model.P_sin,
                    self.model.P_cos],
                    feed_dict={self.model.input_image: img_batch,
                               self.model.Labels: Labels_batch})

            self.writer.add_summary(summary_image, total_steps)
            self.writer.add_summary(summary_image_attr, total_steps)

            if self.configuration.test_mode:
                for i in range(img_batch.shape[0]):
                    t_r, t_p, p_r, p_p, visualize_map, precise_M, recall_M = data_management.evaluate(img_batch[i, :, :, :],
                                                                                cnts=cnt_ground_truth[i],
                                                                                is_text_cnts=True,
                                                                                is_viz=False,
                                                                                maps=prediction[i,:,:,:],NewEval=self.configuration.NewEval)
                    totaltext_recalls.append(t_r)
                    totaltext_precisions.append(np.nan_to_num(t_p))
                    pascal_recalls.append(p_r)
                    pascal_precisions.append(np.nan_to_num(p_p))
                    test_image_sum = self.sess.run(self.image4viz,
                                                   feed_dict={self.reconstructed_map: np.stack([visualize_map], axis=0),
                                                              self.original_image: img_batch[i:i + 1, :, :, ::-1]})

                    self.writer.add_summary(test_image_sum, self.total_steps + count * self.configuration.batch_size + i)
                    cv2.imwrite(filename=os.path.join(self.configuration.test_path, '%s_%.3f_%.3f_%.3f_%.3f.jpg' %
                                                      (id_batch[i], t_r, t_p, p_r, p_p)),
                                img=np.concatenate([np.concatenate([img_batch[i, :, :, :],
                                                                    np.cast['uint8'](TR_TCL_GT_map[i, :, :, :][:, :, (2, 1, 0)] * 255),
                                                                    np.cast['uint8'](L_R_map[i, :, :, :] * 255),
                                                                    np.cast['uint8'](L_sin[i, :, :, :] * 255),
                                                                    np.cast['uint8'](L_cos[i, :, :, :] * 255)], axis=0),
                                                    np.concatenate([visualize_map[:, :, (2, 1, 0)],
                                                                    np.cast['uint8'](TR_TCL_Predictied_map[i, :, :, :][:, :, (2, 1, 0)] * 255),
                                                                    np.cast['uint8'](P_R_map[i, :, :, :] * 255),
                                                                    np.cast['uint8'](P_sin[i, :, :, :] * 255),
                                                                    np.cast['uint8'](P_cos[i, :, :, :] * 255)], axis=0)], axis=1))  # left: original, right: predicted
                    pickle.dump((precise_M, recall_M), open(os.path.join(self.configuration.test_path, '%s_matrix.bin' % id_batch[i]), 'wb'))
                    pickle.dump(cnt_ground_truth[i],open(os.path.join(self.configuration.test_path, '%s_cnt.bin' % id_batch[i]), 'wb'))
                   # x=input('check')
                try:
                    self.save_instance(id_batch, prediction)
                except:
                    pass
            else:
                totaltext_recalls += [0] * img_batch.shape[0]
                totaltext_precisions += [0] * img_batch.shape[0]
                pascal_recalls += [0] * img_batch.shape[0]
                pascal_precisions += [0] * img_batch.shape[0]
            losses.append(total_loss * img_batch.shape[0])

        if True or self.configuration.dataset_name == 'totaltext':
            P = sum(totaltext_precisions) / len(totaltext_precisions)
            R = sum(totaltext_recalls) / len(totaltext_recalls)
        else:
            P = sum(totaltext_precisions) / len(totaltext_precisions)
            R = sum(pascal_recalls) / len(pascal_precisions)
        if P + R < 1e-3:
            F = 0
        else:
            F = 2 * P * R / (P + R)

        avg_loss = sum(losses) / self.configuration.size['test']

        self.log_record['info']('average test loss: %.3f. P=%.3f, R=%.3f, F=%.3f' % (avg_loss, P, R, F))

        return avg_loss

    def save_instance(self, *args):
        id = args[0]
        predictions = args[1]
        for sample in range(len(id)):
            np.save(os.path.join(self.configuration.test_path, '%s_maps.npy' % (id[sample])), predictions[sample, :, :, :])

    def load(self):
        self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        self.sess.run(tf.global_variables_initializer())
        self.model.load(sess=self.sess, path=self.configuration.restore_path)
        self.log_record['info']('model loaded from %s.' % self.configuration.restore_path)

    def predict(self, paths, output_dir, suffix='jpg'):
        """
        :param paths:
        :return:
        """
        if not self.initialize_flag:
            self._initialize_model()
        files = glob.glob(os.path.join(paths, '*' + suffix))
        for file in files:
            img = cv2.imread(file)
            size = img.shape
            if max(img.shape[:2]) > 2048:
                img = np.resize(img, new_shape=(int(size[0] * 2048 / max(img.shape[:2])) // 32 * 32,
                                                int(size[1] * 2048 / max(img.shape[:2])) // 32 * 32,
                                                size[2])).astype(np.uint8)
            prediction = self.sess.run([
                self.model.prediction
            ],
                feed_dict={self.model.input_image: np.stack([img], axis=0),
                           })
            masks = data_management.evaluate(img,
                                             cnts=None,
                                             is_text_cnts=True,
                                             is_viz=False,
                                             maps=[np.squeeze(map_) for map_ in np.split(prediction[0, :, :, :], 7, axis=-1)])
            np.save(os.path.join(output_dir, file.split('/')[-1][:-(len(suffix))] + str(size) + 'bin'), masks)

if __name__ == '__main__':
    pass


import torch
import torchvision
from torchvision import transforms,datasets,models
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

train_set = datasets.MNIST('~/torch_datasets', train=True, download=True)
test_set = datasets.MNIST('~/torch_datasets', train=False, download=True)
train_img = train_set.data.numpy()
test_img = test_set.data.numpy()

def train_loader_fn():
    train_images = torch.Tensor(train_img).view(train_img.shape[0],1,28,28).to(device)
    return train_images
def test_loader_fn():
    test_images = torch.Tensor(test_img).view(test_img.shape[0],1,28,28).to(device)
    return test_images


"""
Using Cartopy and AxesGrid toolkit
----------------------------------

This example demonstrates how to use cartopy `~cartopy.mpl.geoaxes.GeoAxes`
with `~mpl_toolkits.axes_grid1.axes_grid.AxesGrid` from the
:mod:`mpl_toolkits.axes_grid1`. The script constructs an ``axes_class`` kwarg
with Plate Carree projection and passes it to the
`~mpl_toolkits.axes_grid1.axes_grid.AxesGrid` instance. The
`~mpl_toolkits.axes_grid1.axes_grid.AxesGrid` built-in tick labelling
is not used, and instead a standard procedure of creating grid lines is used.
Then some fake data is plotted.
"""
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np

import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter

def sample_data_3d(shape):
    """Return `lons`, `lats`, `times` and fake `data`"""
    ntimes, nlats, nlons = shape
    lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)
    lons = np.linspace(0, 2 * np.pi, nlons)
    lons, lats = np.meshgrid(lons, lats)
    wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
    mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)

    lats = np.rad2deg(lats)
    lons = np.rad2deg(lons)
    data = wave + mean

    times = np.linspace(-1, 1, ntimes)
    new_shape = data.shape + (ntimes, )
    data = np.rollaxis(data.repeat(ntimes).reshape(new_shape), -1)
    data *= times[:, np.newaxis, np.newaxis]

    return lons, lats, times, data

def main():
    projection = ccrs.PlateCarree()
    axes_class = (GeoAxes,
                  dict(projection=projection))

    lons, lats, times, data = sample_data_3d((6, 73, 145))

    fig = plt.figure()
    axgr = AxesGrid(fig, 111, axes_class=axes_class,
                    nrows_ncols=(3, 2),
                    axes_pad=0.6,
                    cbar_location='right',
                    cbar_mode='single',
                    cbar_pad=0.2,
                    cbar_size='3%',
                    label_mode='keep')

    for i, ax in enumerate(axgr):
        ax.coastlines()
        ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
        ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
        lon_formatter = LongitudeFormatter(zero_direction_label=True)
        lat_formatter = LatitudeFormatter()
        ax.xaxis.set_major_formatter(lon_formatter)
        ax.yaxis.set_major_formatter(lat_formatter)

        p = ax.contourf(lons, lats, data[i, ...],
                        transform=projection,
                        cmap='RdBu')

    axgr.cbar_axes[0].colorbar(p)

    plt.show()

if __name__ == '__main__':
    main()


#!/usr/bin/env python

#Licensed under the MIT licence
#To end the Programme use "C+Ctrl" at the same time.

#pretty obvious, but you'll want these dependencies to be installed for this script to work

# This imports the Raspberry Pi's GPIO library but as GPIO so we don't need to be repeating it.
import RPi.GPIO as GPIO

#This is used to sleep the code a the end.
import time

#Requests is the ONE that is need to be installed with "sudo apt-get install python-requests" this lets us to grab and interpret PHP
import requests

import os

def cls():
    os.system(['clear','cls'][os.name == 'nt'])

#Uncomment this is you don't want to get the GPIO warning. This is useful if you changing things around constantly and are ending the code.
#GPIO.setwarnings(False)

DEBUG = 1

#this defines the pins for each ONE
ONE = 23
TWO = 24
THREE = 25

#sets the mode for the pin out
GPIO.setmode(GPIO.BCM)

#defines if the pins are inputs or out puts
GPIO.setup(ONE, GPIO.OUT)
GPIO.setup(TWO, GPIO.OUT)
GPIO.setup(THREE, GPIO.OUT)

#replace the http://YourWebsite.com/test.php with where your test.php is located.
response = requests.get('http://yourwebsite.com/text.php')

#replace the http://YourWebsite.com/test.php with where your test.php is located.
while response.text != 'exit':
    response = requests.get('http://yourwebsite.com/text.php')

#This says ONE is the first digit TWO is the second and THREE is the last
    colrs = response.text
    one = colrs[0]
    two = colrs[1]
    three = colrs[2]

    cls()

    if response.text == 'exit':
        print (colrs)
    else:
        print ("one" + one + ", "),
        print ("two" + two + ", "),
        print ("three" + three)
        print

# This says if the number is 1 then the the light is on else if it is 0 the light is off.
    if one == '1':
        GPIO.output(ONE,True)
    else:
        GPIO.output(ONE,False)

    if two  == '1':
        GPIO.output(TWO,True)
    else:
        GPIO.output(TWO,False)

    if three  == '1':
        GPIO.output(THREE,True)
    else:
        GPIO.output(THREE,False)

#This can be removed but this time is to stop the web server from being overloaded with requests.
    time.sleep(0.15)


from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
from future.builtins import (int, open, round,
                             str)

class Camera(object):
    def __init__(self, name, field_of_view, capture_width, capture_height, relative_position, relative_rotation):
        self.name = name
        self.field_of_view = field_of_view
        self.capture_width = capture_width
        self.capture_height = capture_height
        self.relative_position = relative_position
        self.relative_rotation = relative_rotation
        self.connection_id = None


# Author: kk.Fang(fkfkbill@gmail.com)

__all__ = [
    "OracleOnlineSQLTextIssueSingleAnalyse"
]

from typing import Generator, List

import rule.const
from ....issue.base import *
from .sql_text import *
from ....single_sql import SingleSQLForOnline
from ....cmdb import OracleCMDB
from ....plain_db import *
from rule.adapters import RuleAdapterSQL
from ....capture import OracleSQLText

@OracleOnlineIssue.need_collect()
class OracleOnlineSQLTextIssueSingleAnalyse(OracleOnlineSQLTextIssue):
    """sql文本问题(逐个分析)"""

    ENTRIES = (rule.const.RULE_ENTRY_ONLINE_SINGLE,)

    @classmethod
    def _single_rule(cls,
                     the_rule,
                     sqls: List[SingleSQLForOnline],
                     cmdb_connector: OraclePlainConnector,
                     entries: List[str],
                     the_cmdb: OracleCMDB,
                     task_record_id: int,
                     schema_name: str,
                     **kwargs) -> Generator["OracleOnlineSQLTextIssue", None, None]:
        for single_sql in sqls:
            ret = RuleAdapterSQL(the_rule).run(
                entries=entries,

                cmdb=the_cmdb,
                single_sql=single_sql
            )
            docs = cls.pack_rule_ret_to_doc(the_rule, ret)
            cls.post_analysed(
                docs=docs,
                task_record_id=task_record_id,
                schema_name=schema_name
            )
            yield from docs


#!/usr/bin/python
#encoding: utf-8
import threading

#  银行存钱和取钱
def add(lock):
    global money  # 生命money为全局变量
    for i in range(1000000):
        # 2. 操作变量之前进行加锁
        lock.acquire()
        money += 1  # money;  money+1; money=money+1;
        # 3. 操作变量之后进行解锁
        lock.release()

def reduce(lock):
    global money
    for i in range(1000000):
        # 2. 操作变量之前进行加锁
        lock.acquire()
        money -= 1
        # 3. 操作变量之后进行解锁
        lock.release()

if __name__ == '__main__':
    money = 0
    # 1. 实例化一个锁对象;
    lock = threading.Lock()

    t1 = threading.Thread(target=add, args=(lock,))
    t2 = threading.Thread(target=reduce, args=(lock,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()

    print("当前金额:", money)


import re
import json

def parser(file):
    dataLines = []
    with open(file) as f:
        for line in f:
            fields = line.split("::")
            if len(fields) == 7:
                pasta, data, nome, pai, mae, info, _ = fields
                dataLines.append({
                    "pasta": pasta,
                    "data": data,
                    "nome": nome,
                    "pai": pai,
                    "mae": mae,
                    "info": info
                })
    return dataLines

def process_anos(data):
    dicAno = {}
    for line in data:
        ano = line["data"].split("-")[0]
        dicAno[ano] = dicAno.get(ano, 0) + 1
    return sorted(dicAno.items(), key=lambda x: x[1], reverse=True)

def seculos(year):
    return (int(year) - 1) // 100 + 1

def nomes_seculo(data):
    dicSeculos = {}
    for line in data:
        ano = line["data"].split("-")[0]
        seculo = seculos(ano)
        name, apelido = re.match(r"^(\w+)\s+(\w+)$", line["nome"]).groups()
        if seculo not in dicSeculos:
            dicSeculos[seculo] = {"nomes": {}, "apelidos": {}}
        dicSeculos[seculo]["nomes"][name] = dicSeculos[seculo]["nomes"].get(name, 0) + 1
        dicSeculos[seculo]["apelidos"][apelido] = dicSeculos[seculo]["apelidos"].get(apelido, 0) + 1

    for sec in dicSeculos:
        dicSeculos[sec]["nomes"] = sorted(dicSeculos[sec]["nomes"].items(), key=lambda x: x[1], reverse=True)
        dicSeculos[sec]["apelidos"] = sorted(dicSeculos[sec]["apelidos"].items(), key=lambda x: x[1], reverse=True)
    return dicSeculos

def relacao(data):
    dicRelacoes = {}
    for line in data:
        if line["info"]:
            rel = re.findall(r",([^,]+)\. Proc", line["info"])
            for i in rel:
                dicRelacoes[i] = dicRelacoes.get(i, 0) + 1
    return dicRelacoes

def exD(data):
    with open("ex.json", "w") as f:
        for i in range(min(20, len(data))):
            json.dump(data[i], f)
            f.write("\n")

info = parser("processos.txt")
anos = process_anos(info)
nomes_apelidos = nomes_seculo(info)

for sec in nomes_apelidos:
    print(f"\nSéculo: {sec}")
    print("\nNomes")
    for i, (name, count) in enumerate(nomes_apelidos[sec]["nomes"][:5], start=1):
        print(f"{i}: {name} - {count}")
    print("\nApelidos")
    for i, (apelido, count) in enumerate(nomes_apelidos[sec]["apelidos"][:5], start=1):
        print(f"{i}: {apelido} - {count}")

print(relacao(info))
exD(info)


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

import wx
import os.path

TITLE_TXT = """Huayra Update"""
ABOUT_TXT = """Te avisa cuando hay actualizaciones del sistema disponibles."""
VERSION_TXT = """Versión: 0.4"""

class AboutDialog(wx.Frame):
    def __init__(self, parent=None):
        super(AboutDialog, self).__init__(
            parent=parent,
            title=u'Acerca de',
        )

        img = os.path.join(wx.GetApp().app_path, 'media', 'fondo.png')
        bmp = wx.Image(img, wx.BITMAP_TYPE_ANY).ConvertToBitmap()

        panel = wx.Panel(self)
        wx.StaticBitmap(panel, -1, bmp, (0, 0))

        font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Asap')

        txt = wx.StaticText(panel, id=-1, label=TITLE_TXT, pos=(115, 40))
        txt.SetFont(
            wx.Font(12, wx.MODERN, wx.NORMAL, wx.BOLD, False, u'Asap')
        )

        txt1 = wx.StaticText(panel, id=-1, label=ABOUT_TXT, pos=(115, 58))
        txt1.Wrap(200)
        txt1.SetFont(font)

        txt2 = wx.StaticText(panel, id=-1, label=VERSION_TXT, pos=(115, 120))
        txt2.Wrap(200)
        txt2.SetFont(font)

        txt.SetForegroundColour((0,0,0))
        txt1.SetForegroundColour((0,0,0))
        txt2.SetForegroundColour((0,0,0))

        self.SetMinSize((350, 250))
        self.SetMaxSize((350, 250))

        w, h = wx.DisplaySize()
        x = (w / 2) - (350 / 2)
        y = (h / 2) - (250 / 2)

        self.SetPosition((x,y))

    def OnClose(self, evt):
        self.Close()


# Show a Scatter Plot

from matplotlib import pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 2, 3, 4, 6, 8, 2, 4, 3]

plt.scatter(x, y, label='Scatter Plot')
plt.legend()
plt.xlabel('X AXIS')
plt.ylabel('Y AXIS')
plt.title('Scatter')

plt.show()


# -*- coding: utf-8 -*-
# __author__ = 'xuting'

"""bcl2fastq 工具 """
import os
import errno
import re
import xml.etree.ElementTree as ET
from biocluster.tool import Tool
from biocluster.agent import Agent
from biocluster.core.exceptions import OptionError
from mbio.packages.datasplit.miseq_split import code2index

class TestWorkflowAgent(Agent):
    """
    bcl2fastq
    version 2.17
    """
    def __init__(self, parent=None):
        super(TestWorkflowAgent, self).__init__(parent)
        self._run_mode = "ssh1"
        options = [
            {'name': 'sample_info', 'type': "string"}
        ]
        self.add_option(options)
        self.step.add_steps("test")
        self.on('start', self.stepstart)
        self.on('end', self.stepfinish)

    def stepstart(self):
        self.step.test.start()
        self.step.update()

    def stepfinish(self):
        self.step.test.finish()
        self.step.update()

    def check_options(self):
        """
        参数检测
        """
        return True

    def set_resource(self):
        """
        设置所需要的资源
        """
        self._cpu = 5
        self._memory = ''

    def end(self):
        result_dir = self.add_upload_dir(self.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "结果输出目录"],
        ])
        super(TestWorkflowAgent, self).end()

class TestWorkflowTool(Tool):
    """
    """
    def __init__(self, config):
        super(TestWorkflowTool, self).__init__(config)

    def bcl2fastq(self):
        """
        运行bcl2fastq
        """
        os.link(self.option("sample_info") + '/RTAComplete.txt', self.output_dir + '/RTAComplete.txt')

    def run(self):
        super(TestWorkflowTool, self).run()
        self.bcl2fastq()
        self.end()


from hangulbase import *

def rule(c1, c2, res):
	print(f'"{c1}{c2}": "{res}",')

# basic rules where characters are simply added to the previous syllable
for i in range(44032, 55203+1):
	c = chr(i)
	if c not in ksx1001:
		continue

	initial, medial, final = syllableBlocks(i)

	if final != 0:
		if finals[final] in finalCompRev:
			c1 = syllable(initial, medial, finals.index(finalCompRev[finals[final]][0]))
			c2 = finalCompRev[finals[final]][1]
			rule(c1, c2, c)
		else:
			c1 = syllable(initial, medial, 0)
			c2 = finals[final]
			rule(c1, c2, c)
	elif medial != 0:
		if medials[medial] in medialCompRev:
			c1 = syllable(initial, medials.index(medialCompRev[medials[medial]][0]), 0)
			c2 = medialCompRev[medials[medial]][1]
			rule(c1, c2, c)
		else:
			c1 = initials[initial]
			c2 = medials[medial]
			rule(c1, c2, c)

# rules where a medial was written after a syllable that had a final so that makes two syllables
for i in range(44032, 55203+1):
	c = chr(i)
	if c not in ksx1001:
		continue

	initial, medial, final = syllableBlocks(i)

	if final == 0:
		continue

	if finals[final] in finalCompRev:
		for m in [v for v in medials if v not in medialCompRev]:
			res1 = syllable(initial, medial, finals.index(finalCompRev[finals[final]][0]))
			res2 = syllable(initials.index(finalCompRev[finals[final]][1]), medials.index(m), 0)
			rule(c, m, f'{res1}{res2}')
	else:
		for m in [v for v in medials if v not in medialCompRev]:
			res1 = syllable(initial, medial, 0)
			res2 = syllable(initials.index(finals[final]), medials.index(m), 0)
			rule(c, m, f'{res1}{res2}')


import sys
import pickle
import pandas
import json
import starcoder
import argparse
import numpy
import warnings
import gzip
import logging
import torch
from typing import Dict, List, Any, Tuple
from torch.optim import Adam, SGD
from starcoder.schema import Schema
from starcoder.dataset import Dataset
from starcoder.ensemble_model import GraphAutoencoder
from starcoder.registry import field_classes, splitter_classes, batchifier_classes, field_model_classes, scheduler_classes
from starcoder.utils import run_epoch, compute_losses, run_over_components, simple_loss_policy, apply_model, apply_to_components
from starcoder.entity import unstack_entities

warnings.filterwarnings("ignore")

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("--input", dest="input", help="Input file")
    parser.add_argument("--schema_input", dest="schema_input", help="Input file")
    parser.add_argument("--dataset_input", dest="dataset_input", help="Input file")
    parser.add_argument("--model_input", dest="model_input", help="Input file")
    parser.add_argument("--categorical", nargs="*", default=[], dest="categorical")
    parser.add_argument("--all_categorical", default=False, action="store_true", dest="all_categorical")
    parser.add_argument("--ignore", nargs="*", default=[], dest="ignore")
    parser.add_argument("--gpu", dest="gpu", default=False, action="store_true", help="Use GPU")
    parser.add_argument("--batch_size", dest="batch_size", type=int, default=256, help="Batch size")
    parser.add_argument("--learning_rate", dest="learning_rate", type=float, default=0.001, help="Learning rate")
    parser.add_argument("--early_stop", dest="early_stop", type=int, default=20, help="Early stop")
    parser.add_argument("--patience", dest="patience", type=int, default=5, help="LR scheduler patience (default: no scheduler)")
    parser.add_argument("--max_epochs", dest="max_epochs", default=100, type=int)
    parser.add_argument("--depth", dest="depth", default=2, type=int)
    parser.add_argument("--autoencoder_shapes", dest="autoencoder_shapes", default=[256, 64], type=int)
    parser.add_argument("--schema_output", dest="schema_output", help="Output file")
    parser.add_argument("--dataset_output", dest="dataset_output", help="Output file")
    parser.add_argument("--model_output", dest="model_output", help="Output file")
    parser.add_argument("--reconstruction_output", dest="reconstruction_output", help="Output file")
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger("starcoder")
    logger.setLevel(logging.INFO)

    logger.info("Loading data from '{}'".format(args.input))
    tsv = pandas.read_excel(args.input) if args.input.endswith("xlsx") else pandas.read_csv(args.input)
    tsv = tsv.filter(items=[f for f in tsv.columns if f not in args.ignore])
    known_entity_types: Dict[str, List[str]] = {"row" : []} #"row_number"]} #x[0] : x[1:] for x in args.entity}
    for field_name in tsv.columns:
        entity_type = field_name.split("_")[0]
        known_entity_types[entity_type] = known_entity_types.get(entity_type, []) + [field_name]

    if args.dataset_input:
        with gzip.open(args.dataset_input, "rb") as ifd:
            dataset = pickle.load(ifd)
    else:
        if args.schema_input:
            with open(args.schema_input, "rt") as ifd:
                spec = json.loads(ifd.read())
        else:
            spec = {"meta" : {"entity_type_field" : "entity_type", "id_field" : "id"},
                    "data_fields" : dict([(k, {"type" : "categorical" if (k in args.categorical or args.all_categorical) else "text" if v == "O" else "numeric"}) for k, v in tsv.dtypes.items()] + []),
                    "entity_types" : {k : {"data_fields" : v} for k, v in known_entity_types.items()},
                    "relationship_fields" : {"row_lists_{}".format(k) : {"source_entity_type" : "row", "target_entity_type" : k} for k in known_entity_types.keys() if k != "row"}
            }
        schema = Schema(spec, field_classes)

        entity_lookup: Dict[Tuple[Any, ...], str] = {}
        entities: Dict[str, Dict[str, Any]] = {}
        for row_number in range(len(tsv)):
            row_entities = {}
            for entity_type, fields in known_entity_types.items():
                if entity_type != "row":
                    tuple_entity = tuple([(f, tsv[f][row_number]) for f in fields])
                    if not all([x == "" for _, x in tuple_entity]):
                        eid = entity_lookup.setdefault(tuple_entity, str(len(entity_lookup)))
                        entity = {k : v for k, v in tuple_entity if isinstance(v, str) or not numpy.isnan(v)}
                        entity["id"] = eid
                        row_entities[entity_type] = entity
            row_entities["row"] = dict([("id", "row {}".format(row_number))] + [("row_to_{}".format(e), row_entities[e]["id"]) for e in known_entity_types.keys() if e != "row"])

            for entity_type, entity in row_entities.items():
                entity["entity_type"] = entity_type
                schema.observe_entity(entity)
                entities[entity["id"]] = entity
        schema.minimize()
        schema.verify()
        logging.info("Created schema: %s", schema)
        if args.schema_output != None:
            with open(args.schema_output, "wt") as ofd:
                ofd.write(json.dumps(schema.json))
            logging.info("Wrote schema to '%s'", args.schema_output)

        dataset = Dataset(schema, list(entities.values()), strict=False)
        logging.info("Loaded dataset: %s", dataset)
        if args.dataset_output != None:
            with open(args.dataset_output, "wb") as ofd: # type: ignore
                ofd.write(pickle.dumps(dataset)) # type: ignore
            logging.info("Wrote dataset to '%s'", args.dataset_output)

    splitter = splitter_classes["sample_components"](["--proportions", ".8", ".1", ".1"])
    train_ids, dev_ids, test_ids = splitter(dataset)
    train_data = dataset.subselect_entities(train_ids)
    dev_data = dataset.subselect_entities(dev_ids)
    model = GraphAutoencoder(schema=dataset.schema,
                             depth=args.depth,
                             autoencoder_shapes=args.autoencoder_shapes, #
                             reverse_relationships=True,
    )

    best_dev_loss = torch.tensor(numpy.nan)
    best_state = {}
    logger.info("Model: %s", model)
    logger.info("Model has %d parameters", model.parameter_count)
    model.init_weights()

    optim = Adam(model.parameters(), lr=args.learning_rate)
    sched = scheduler_classes["basic"](args.early_stop, optim, patience=args.patience, verbose=True) # type: ignore
    batchifier = batchifier_classes["sample_components"]([])

    logger.info("Training StarCoder with %d/%d train/dev entities and %d/%d train/dev components and batch size %d",
                len(train_ids),
                len(dev_ids),
                train_data.num_components,
                dev_data.num_components,
                args.batch_size)

    subselect = False
    for e in range(1, args.max_epochs + 1):

        train_loss, train_loss_by_field, dev_loss, dev_loss_by_field = run_epoch(model,
                                                                                batchifier,
                                                                                optim,
                                                                                simple_loss_policy,
                                                                                train_data,
                                                                                train_data,
                                                                                args.batch_size,
                                                                                args.gpu,
                                                                                [],
                                                                                subselect
        )
        logger.info("Epoch %d: comparable train/dev loss = %.4f/%.4f",
                    e,
                    dev_data.num_entities * (train_loss / train_data.num_entities),
                    dev_loss,
        )

        reduce_rate, es, new_best = sched.step(dev_loss)
        if new_best:
            logger.info("New best dev loss: %.3f", dev_loss)
            best_dev_loss = dev_loss
            best_state = {k : v.clone().detach().cpu() for k, v in model.state_dict().items()}

        if reduce_rate == True:
            model.load_state_dict(best_state)
        if es == True:
            logger.info("Stopping early after no improvement for %d epochs", args.early_stop)
            break
        elif e == args.max_epochs:
            logger.info("Stopping after reaching maximum epochs")

    if args.model_output != None:
        logger.info("Saved model to '%s'", args.model_output)

    reconstructions = {}
    for decoded_fields, norm, _ in apply_to_components(model, batchifier_classes["sample_components"]([]), dataset, args.batch_size, args.gpu):
        for entity in [dataset.schema.unpack(e) for e in unstack_entities(norm, dataset.schema)]:
            reconstructions[entity["id"]] = entity

    if args.reconstruction_output:
        with open(args.reconstruction_output, "wt") as ofd:
            for eid, entity in entities.items():
                item = {"original" : entity,
                        "reconstruction" : reconstructions[eid]}
                ofd.write(json.dumps(item) + "\n")
        logger.info("Saved reconstructions to '%s'", args.reconstruction_output)


import pandas as pd
import numpy as np
from konlpy.tag import Kkma
import re

rawdata_path = './rawdata.csv'
kkma = Kkma()
df = pd.read_csv(rawdata_path)
#영상제목 토큰화 하는 과정
noun_final = []
del_line = []
for text in range(len(df)):
    print(df['title'].iloc[text])
    try:
        noun0=kkma.pos(df['title'].iloc[text])
    except:
        del_line.append(text)
        continue
    noun=[]
    for i,j in noun0:
        if j=='NNG':
            if i == '딩고':
                pass
            else:
                noun.append(i)
    noun_final.append(noun)
df=df.drop(del_line)
df.reset_index(drop=True)
df.view = df.view.str.extract('(\d+)').astype(float)# 조회수 정리
likes_thousand=df['like'].str.contains('\d+천')
likes_tenthousand = df['like'].str.contains('\d+만')
df['like'] = df['like'].str.extract('(\d+)', expand=False).astype(float)
df.loc[likes_thousand, 'like'] *= 1000
df.loc[likes_tenthousand, 'like'] *= 10000

likes_thousand=df['unlike'].str.contains('\d+천')
likes_tenthousand = df['unlike'].str.contains('\d+만')
df['unlike'] = df['unlike'].str.extract('(\d+)', expand=False).astype(float)
df.loc[likes_thousand, 'unlike'] *= 1000
df.loc[likes_tenthousand, 'unlike'] *= 10000

df['token'] = noun_final

#토큰화 작업을 거친 뒤에 단어가 하나인 것은 제외하기
noun_ls = []
for i in range(len(df)):
    noun_ls0=[]
    for j in range(len(df['token'].iloc[i])):
        if len(df['token'].iloc[i][j]) == 1:
            pass
        else:
            noun_ls0.append(df['token'].iloc[i][j])
    noun_ls.append(list(set(noun_ls0))) #중복제거
df['token'] = noun_ls
df.to_csv('./preprocessed.csv',encoding='utf-8-sig')

token_df = pd.DataFrame({'token':[]})
for i in range(len(df)):
    insert_data = pd.DataFrame({'token': df['token'].iloc[i]})
    insert_data['view'] = df['view'].iloc[i]

    token_df = token_df.append(insert_data)

token_df2 = token_df.groupby('token')['view'].sum().reset_index()  # 키워드별 조회수 합
token_df2['count'] = token_df.groupby(['token']).count().reset_index()['view'].tolist()  # 각 키워드의 갯수
# 키워드별 조회수의합 / 갯수 - 동등하게 만들어야 하기 때문에
view_count = []
for i in range(len(token_df2)):
    a = token_df2['view'].iloc[i] / token_df2['count'].iloc[i]
    view_count.append(a)
token_df2['view_count'] = view_count
# del_keyword = ['열애','미드라','바보','다이','질주','은하','어깨','거지','정류장','그레이','준일','종신','방탄','형식','민서','악동','뮤지션','소어','주행','바램','노력','포박','자격지심','시티','푸스','게다리','라마','분노','행복']
del_keyword = ['청하','경리','니트','남우','용사','할아버지','이병기','너와','현우','고수','자존','동갑내기','벚꽃','생존','장애','자수성가','호텔','사장님','역전','민박','나이','스토리','세정','재인','스쿨','유정','세정','구구단','호시','아이오','벨벳','레드','선미','고생','오의','어부','식당','하자',]
for keyword in del_keyword:
    token_df2=token_df2.drop(token_df2[token_df2['token']==keyword].index)
print(token_df2.sort_values(by='view_count',ascending=False).head(30))

token_df2.to_csv('./count.csv',encoding='utf-8-sig')


import socket

# Функция для установления соединения с сервером
def connect_to_server(host, port):
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((host, port))
    return client_socket

# Функция для отправки запроса на сервер
def send_request(socket, request):
    socket.send(request.encode())

# Функция для получения ответа от сервера
def receive_response(socket):
    response = socket.recv(1024).decode()
    return response

# Функция для просмотра списка файлов и папок в текущей директории
def list_files(socket):
    send_request(socket, "LIST")
    response = receive_response(socket)
    print(response)

# Функция для создания папки
def create_directory(socket, directory_name):
    send_request(socket, f"MKDIR {directory_name}")
    response = receive_response(socket)
    print(response)

# Функция для удаления папки
def delete_directory(socket, directory_name):
    send_request(socket, f"RMDIR {directory_name}")
    response = receive_response(socket)
    print(response)

# Функция для удаления файла
def delete_file(socket, file_name):
    send_request(socket, f"DELETE {file_name}")
    response = receive_response(socket)
    print(response)

# Функция для переименования файла
def rename_file(socket, old_name, new_name):
    send_request(socket, f"RENAME {old_name} {new_name}")
    response = receive_response(socket)
    print(response)

# Функция для копирования файла с клиента на сервер
def upload_file(socket, file_name):
    send_request(socket, f"UPLOAD {file_name}")
    response = receive_response(socket)
    print(response)

# Функция для копирования файла с сервера на клиент
def download_file(socket, file_name):
    send_request(socket, f"DOWNLOAD {file_name}")
    response = receive_response(socket)
    print(response)

# Функция для получения содержимого файла
def get_file_content(socket, file_name):
    send_request(socket, f"GET {file_name}")
    response = receive_response(socket)
    print(response)

# Функция для отключения от сервера
def disconnect_from_server(socket):
    send_request(socket, "QUIT")
    socket.close()

# Пример использования клиента
def main():
    # Параметры подключения к серверу
    host = 'localhost'
    port = 8000

    # Подключение к серверу
    client_socket = connect_to_server(host, port)

    # Основной цикл работы клиента
    while True:
        command = input("Введите команду (LIST, MKDIR, RMDIR, DELETE, RENAME, UPLOAD, DOWNLOAD, GET, QUIT): ")

        if command == "LIST":
            list_files(client_socket)
        elif command == "MKDIR":
            directory_name = input("Введите название папки: ")
            create_directory(client_socket, directory_name)
        elif command == "RMDIR":
            directory_name = input("Введите название папки: ")
            delete_directory(client_socket, directory_name)
        elif command == "DELETE":
            file_name = input("Введите название файла: ")
            delete_file(client_socket, file_name)
        elif command == "RENAME":
            old_name = input("Введите текущее название файла: ")
            new_name = input("Введите новое название файла: ")
            rename_file(client_socket, old_name, new_name)
        elif command == "UPLOAD":
            file_name = input("Введите название файла для загрузки: ")
            upload_file(client_socket, file_name)
        elif command == "DOWNLOAD":
            file_name = input("Введите название файла для скачивания: ")
            download_file(client_socket, file_name)
        elif command == "GET":
            file_name = input("Введите название файла: ")
            get_file_content(client_socket, file_name)
        elif command == "QUIT":
            disconnect_from_server(client_socket)
            break
        else:
            print("Некорректная команда.")

if __name__ == '__main__':
    main()


import os
from io import BytesIO
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from googletrans import Translator

MutyalaHarshith = Client(
    "Translator Bot",
    bot_token = os.environ["BOT_TOKEN"],
    api_id = int(os.environ["API_ID"]),
    api_hash = os.environ["API_HASH"]
)

START_TEXT = """
Hello {}, I am a google translator telegram bot.Project By @MutyalaHarshith To Know How To Use Me Do /help.
Made by @MutyalaHarshith
"""
HELP_TEXT = """
- Just send a text with language code
example :- `MutyalaHarshith | te    for telugu`
           `MutyalaHarshith | ml for malayalam`
           `MutyalaHarshith | kn for kannada`
           `MutyalaHarshith | ta for Tamil`
Still more feature adding 😉
Any doubt contact @Harshith_support
Made by @MutyalaHarshith
"""
ABOUT_TEXT = """
- **Bot :** `[Google Translator Bot](https://github.com/MutyalaHarshith/Translate)`
- **Creator :** [Harshith](https://telegram.me/Develoveper)
- **Channel :** [Mutyala Harshith](https://telegram.me/MutyalaHarshith)
- **Developer :** [Harshith](https://github.com/MutyalaHarshith)
- **Language :** [Python3](https://python.org)
- **Library :** [Pyrogram](https://pyrogram.org)
- **Server :** [Heroku](https://heroku.com)
"""
START_BUTTONS = InlineKeyboardMarkup(
        [[
        InlineKeyboardButton('Help', callback_data='help'),
        InlineKeyboardButton('About', callback_data='about'),
        InlineKeyboardButton('Close', callback_data='close')
        ]]
    )
HELP_BUTTONS = InlineKeyboardMarkup(
        [[
        InlineKeyboardButton('Home', callback_data='home'),
        InlineKeyboardButton('About', callback_data='about'),
        InlineKeyboardButton('Close', callback_data='close')
        ]]
    )
ABOUT_BUTTONS = InlineKeyboardMarkup(
        [[
        InlineKeyboardButton('Channel', url='https://telegram.me/MutyalaHarshith'),
        InlineKeyboardButton('Feedback', url='https://telegram.me/Harshith_Support')
        ],[
        InlineKeyboardButton('Home', callback_data='home'),
        InlineKeyboardButton('Help', callback_data='help'),
        InlineKeyboardButton('Close', callback_data='close')
        ]]
    )
CLOSE_BUTTON = InlineKeyboardMarkup(
        [[
        InlineKeyboardButton('Close', callback_data='close')
        ]]
    )
TRANSLATE_BUTTON = InlineKeyboardMarkup(
        [[
        InlineKeyboardButton('👨🔧 Join Updates Channel 👨🔧', url='https://telegram.me/MutyalaHarshith')
        ]]
    )
DEFAULT_LANGUAGE = os.environ.get("DEFAULT_LANGUAGE", "en")

@MutyalaHarshith.on_callback_query()
async def cb_data(bot, update):
    if update.data == "home":
        await update.message.edit_text(
            text=START_TEXT.format(update.from_user.mention),
            disable_web_page_preview=True,
            reply_markup=START_BUTTONS
        )
    elif update.data == "help":
        await update.message.edit_text(
            text=HELP_TEXT,
            disable_web_page_preview=True,
            reply_markup=HELP_BUTTONS
        )
    elif update.data == "about":
        await update.message.edit_text(
            text=ABOUT_TEXT,
            disable_web_page_preview=True,
            reply_markup=ABOUT_BUTTONS
        )
    else:
        await update.message.delete()

@MutyalaHarahith.on_message(filters.command(["start"]))
async def start(bot, update):
    text = START_TEXT.format(update.from_user.mention)
    reply_markup = START_BUTTONS
    await update.reply_text(
        text=text,
        disable_web_page_preview=True,
        reply_markup=reply_markup
    )

@MutyalaHarshith.on_message((filters.private | filters.group | ~filters.channel) & filters.text)
async def translate(bot, update):
    if update.chat.type == "private":
        if " | " in update.text:
            text, language = update.text.split(" | ", 1)
        else:
            text = update.text
            language = DEFAULT_LANGUAGE
    else:
        text = update.reply_to_message.text
        if " " in update.text:
            command, language = update.text.split(" | ", 1)
        else:
            language = DEFAULT_LANGUAGE
    translator = Translator()
    await update.reply_chat_action("typing")
    message = await update.reply_text("`Translating 1 sec...`")
    try:
        translate = translator.translate(text, dest=language)
        translate_text = f"**Translated to {language}**"
        translate_text += f"\n\n{translate.text}"
        translate_text += "\n\nMade by @MutyalaHarshith"
        if len(translate_text) < 4096:
            await message.edit_text(
                text=translate_text,
                disable_web_page_preview=True,
                reply_markup=TRANSLATE_BUTTON
            )
        else:
            with BytesIO(str.encode(str(translate_text))) as translate_file:
                translate_file.name = language + ".txt"
                await update.reply_document(
                    document=translate_file,
                    caption="Made by @MutyalaHarshith",
                    reply_markup=TRANSLATE_BUTTON
                )
                await message.delete()
    except Exception as error:
        print(error)
        await message.edit_text("Something wrong. Contact @MutyalaHarshith.")
        return

MutyalaHarshith.run()


from __future__ import absolute_import

""" PLC Factory: helper functions """

__author__     = "Krisztian Loki"
__copyright__  = "Copyright 2018, European Spallation Source, Lund"
__license__    = "GPLv3"

# Python libraries
import os
from shlex import split as shlex_split
import subprocess
import unicodedata
try:
    from urllib.parse import urlsplit, urlunsplit
except ImportError:
    from urlparse import urlsplit, urlunsplit

try:
    from urllib.parse import quote as urlquote, splituser
except ImportError:
    from urllib import quote as urlquote, splituser

try:
    from pathlib import Path
except ImportError:
    class FakePath(object):
        def __init__(self, p):
            self.parts = tuple(p.split(os.path.sep))
    Path = FakePath

from posixpath import join as posixpathjoin

# Fake a WindowsError exception under non-Windows machines
try:
    we = WindowsError()
    del we
except NameError:
    WindowsError = OSError

# Fake a FileNotFoundError in Python2
try:
    fnf = FileNotFoundError()
    del fnf
    FileNotFoundError = FileNotFoundError
except NameError:
    FileNotFoundError = OSError

class BusyException(OSError):
    pass

def rmdirs(path):
    #
    # Python3(.6?) has a weird interaction with VirtualBox shared folders where shutil.rmtree returns OSError(26) (text file busy) errors
    # So we, check if the exception is OSError(26) and handle that outside of shutil.rmtree() with os.rmdir() and then retry shutil.rmtree
    #
    from shutil import rmtree
    def onrmtreeerror(func, e_path, exc_info):
        if exc_info[0] is OSError and exc_info[1].errno == 26:
            be = BusyException(exc_info[1])
            be.filename = e_path
            raise be

        if e_path != path:
            raise

        if not (func is os.listdir or func is os.rmdir or func is os.lstat):
            raise

        if not (exc_info[0] is OSError or exc_info[0] is FileNotFoundError or exc_info[0] is WindowsError):
            raise

        if exc_info[1].errno != 2:
            raise

    while True:
        try:
            rmtree(path, onerror = onrmtreeerror)
            return
        except BusyException as e:
            os.rmdir(e.filename)

def makedirs(path):
    """
    Helper that ignores already existing directory error
    """
    try:
        os.makedirs(path)
    except OSError:
        if not os.path.isdir(path):
            raise

def sanitize_path(path):
    """
    Helper that sanitizes path; replaces accented characters and removes invalid ones
    """
    path = tounicode(path)

    # replace accented characters with the unaccented equivalent
    path = unicodedata.normalize("NFKD", path).encode("ASCII", "ignore")

    if isinstance(path, bytes) and not isinstance(path, str):
        # Only needed for Python3
        path = path.decode()

    invalid_chars = '<>:"/\\|?*'
    def sanP(p):
        (head, tail) = os.path.split(p)
        if not head:
            return "".join(map(lambda x: '_' if x in invalid_chars else x, p))
        tail = "".join(map(lambda x: '_' if x in invalid_chars else x, tail))
        return os.path.join(sanP(head), tail)

    return sanP(path)

def sanitizeFilename(filename):
    """
    Helper that sanitizes filename; replaces accented characters and removes invalid ones

    It will also replace path separators!
    """
    filename = tounicode(filename)

    # replace accented characters with the unaccented equivalent
    filename = unicodedata.normalize("NFKD", filename).encode("ASCII", "ignore")

    if isinstance(filename, bytes) and not isinstance(filename, str):
        # Only needed for Python3
        filename = filename.decode()

    invalid_chars = '<>:"/\\|?*'
    return "".join(map(lambda x: '_' if x in invalid_chars else x, filename))

def create_cache_dir(product, dirs = None):
    """
    Creates 'cache directory' under ~/.cache
    """
    dname = os.path.join(os.path.expanduser("~"), ".cache", product)
    if dirs is not None:
        dname = os.path.join(dname, dirs)
    makedirs(dname)

    return dname

def create_data_dir(product, dirs = None):
    """
    Creates 'data directory' under ~/.local/share
    """
    dname = os.path.join(os.path.expanduser("~"), ".local", "share", product)
    if dirs is not None:
        dname = os.path.join(dname, dirs)
    makedirs(dname)

    return dname

def tounicode(string):
    """
    Converts string to unicode (Not needed for Python3)
    """
    try:
        # Python2 shortcut
        if isinstance(string, unicode):
            return string
    except NameError:
        # Python3, it is already unicode
        return string

    return string.decode("utf-8")

def url_strip_user(url):
    """
    Removes the 'username@' part of a URL

    https://krisztianloki@gitlab.esss.lu.se/icshwi/plcfactory.git ==> https://gitlab.esss.lu.se/icshiw/plcfactory.git
    """
    comps = urlsplit(url)
    netloc = splituser(comps.netloc)[1]
    return urlunsplit((comps[0], netloc, comps[2], comps[3], comps[4]))

def url_to_path(url):
    """
    Returns the url without the protocol sanitized (invalid characters removed) as a path

    https://gitlab.esss.lu.se/icshwi/linac-vac-plc-def/raw/master/blob/VACUUM_VAC-PLCIO.def ==> gitlab.esss.lu.se/icshwi/linac-vac-plc-def/raw/master/blob/VACUUM_VAC-PLCIO.def
    """

    comps = urlsplit(url)
    comps = splituser(comps.netloc)[1] + comps.path
    comps = comps.split('/')

    return os.path.join(*map(lambda sde: sanitizeFilename(sde), comps))

def url_to_host(url):
    """
    Returns the host part of the url

    https://gitlab.esss.lu.se/icshwi/plcfactory.git ==> gitlab.esss.lu.se
    """

    return splituser(urlsplit(url).netloc)[1]

def urljoin(part1, part2, *more):
    """
    Join paths to form a URL

    NOT using urljoin because it ignores any path component in part1
    """
    if part2[0] == '/':
        part2 = part2[1:]

    if more:
        part2 = posixpathjoin(part2, *[ m[1:] if m[0] =='/' else m for m in more])

    return posixpathjoin(part1, part2)

def sanitize_url(url):
    """
    Sanitizes URLs (removes double //)
    """
    parts = list(urlsplit(url))

    while "//" in parts[2]:
        parts[2] = parts[2].replace("//", "/")

    return urlunsplit(parts)

def xdg_open(url):
    try:
        subprocess.check_call(shlex_split("xdg-open {}".format(url)))
    except (OSError, FileNotFoundError) as e:
        if e.errno == 2:
            raise FileNotFoundError(e)

        raise


# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.

#solution-1
def solution1(limit):
	arr = []

	for i in range(1, limit):
		if i*3 < limit:
			arr.append(i*3)
			if i*5 < limit:
				arr.append(i*5)
		else:
			break

	return sum(list(set(arr)))

#solution-2
def solution2(number):
	return sum(i for i in range(number) if i % 3 == 0 or i % 5 == 0)

#solution-3
def solution3(number):
    max3,max5,max15 = 0,0,0

    for i in range(number-1,0,-1):
        if i%3 == 0 and i//3 > max3:
            max3 = i//3

        if i%5 == 0 and i//5 > max5:
            max5 = i//5

        if i%15 == 0 and i//15 > max15:
            max15 = i//15

        if max3 != 0 and max5 != 0 and max15 != 0:
            break

    three = ((max3*(max3+1))//2)*3 # 3*(1+2+3+....+33)
    five = ((max5*(max5+1))//2)*5
    fifteen = ((max15*(max15+1))//2)*15

    return three + five - fifteen

print(solution1(1000))
print(solution2(1000))
print(solution3(1000))


#!/usr/bin/python3

# External Resources

from UTILS import function_gerenic_except_handler

# Internal Resources

def function_proxy_validator(setConnection):

    try:

        classType = "<class 'mysql.connector.connection_cext.CMySQLConnection'>"

        return True if classType in str(type(setConnection)) \
        and setConnection.is_connected() == True else False

    except Exception as Error:

        return function_gerenic_except_handler(__name__, 'function_mysql_connect', Error)


class ProblemState:

    def __init__(self, intersections, roads, agents, pickups):
        self.intersections = intersections
        self.roads = roads
        self.agents = agents
        self.pickups = pickups

class StateError(Exception):
    """Exception for when the simulator finds the problem in an inconsistent state"""

# Takes in a road and an integer progress showing distance travelled along that road and
#   returns a tuple giving a location coordinate
def position_to_location(road, progress):

    end1 = road.ends[0].location
    end2 = road.ends[1].location

    distance_percent = progress / road.length

    location_x = (1 - distance_percent) * end1[0] + distance_percent * end2[0]
    location_y = (1 - distance_percent) * end1[1] + distance_percent * end2[1]

    return location_x, location_y