import logging

import requests
from telegram.ext import run_async, CommandHandler

from commands.yts.utils import get_minimal_movie, prettify_yts_movie
from utils.decorators import send_typing_action, log_time
from keyboards.keyboards import yts_navigator_keyboard

logger = logging.getLogger(__name__)

@log_time
@send_typing_action
@run_async
def yts(bot, update, chat_data):
    try:
        r = requests.get('https://yts.am/api/v2/list_movies.json', params={'limit': 50})
    except requests.exceptions.ConnectionError:
        update.message.reply_text("ðŸ“¡ La api de yts estÃ¡ caida. IntentÃ¡ mÃ¡s tarde")
        return
    if r.status_code != 200:
        logger.info(f"Yts api down. {r.status_code}")
        return None
    try:
        movies = r.json()['data']['movies']
    except KeyError:
        logger.info(
            f'Response has no moives {r.url} {r.status_code} {r.reason} {r.json()}'
        )
        return None

    # Build context based on the imdb_id
    chat_data['context'] = {
        'data': movies,
        'movie_number': 0,
        'movie_count': len(movies),
        'command': 'yts',
        'edit_original_text': True,
    }
    title, synopsis, rating, imdb, yt_trailer, image = get_minimal_movie(movies[0])
    movie_desc = prettify_yts_movie(title, synopsis, rating)
    yts_navigator = yts_navigator_keyboard(imdb_id=imdb, yt_trailer=yt_trailer)
    bot.send_photo(
        chat_id=update.message.chat_id,
        photo=image,
        caption=movie_desc,
        reply_markup=yts_navigator,
    )

yts_handler = CommandHandler('yts', yts, pass_chat_data=True)


from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [

    path('', views.landing, name = 'landing'),
    path('app/', views.scheduler, name = 'scheduler'),
    path('paymentsuccess/', views.paymentsuccess, name = 'paymentsuccess'),
    path('paymentsettings/', views.paymentsettings, name = 'paymentsettings'),
    path('app/project/<slug>', views.projects, name = 'project'),
    path('add-project/', views.add_project, name='add_project'),
    path('add-module/<pk>', views.add_module, name='add_module'),
    path('delete-module/<pk>', views.delete_module, name='delete_module'),
    path('add-task/<pk>', views.add_task, name='add_task'),
    path('delete-task/<pk>', views.delete_task, name='delete_task'),
    path('app/project/edit-task/<pk>', views.edit_task, name='edit_task'),
    path('edit-task/<pk>', views.edit_task, name='edit_task'),
    path('app/project/edit-task/', views.edit_task, name='edit_task'),
    path('try-progress/', views.try_progress, name='try_progress'),
    path('add-email-launch/', views.add_email_launch, name='add_email_launch'),

    path('demo/', views.scheduler_demo, name = 'scheduler_demo'),
    path('demoapp/project/<slug>', views.projects_demo, name = 'project_demo'),
]


#https://colab.research.google.com/drive/1L_2syN8Cqtyywm3MOkzCOXyfw3FuVxOc#scrollTo=zcirEKOgpWGk

import multiprocessing
import math

# funcion fibonacci con la tecnica de la memorizacion para mejorar el tiempo de ejecucion
def fibonacci(n, memo={}):
    if n in memo:
      return memo[n]

    # Cálculo fibo con formula
    memo[n]=math.trunc((pow((1+math.sqrt(5))/2,n) - pow((1-math.sqrt(5))/2,n))/math.sqrt(5))
    return memo[n]

if __name__ == '__main__':
    with multiprocessing.Pool() as pool:
        result = pool.map(fibonacci, range(1000))
    print(result)


import argparse
import ccl_bplist
import construct
import plistlib

"""
An old version of yk_bmdata.py that makes use of construct library.
Limitation: Number of ToCs it can parse
"""

__author__ = 'yukai'

# to reduce code width
_ARRAY = construct.Array
_POINTER = construct.Pointer
_REPEAT = construct.RepeatUntil
_INT = construct.ULInt32('integer')
_INT64 = construct.ULInt64('integer64')

_BOOKMARK_DATA = construct.Struct(
    'bookmark_data',
    construct.String('magic', 4),
    construct.ULInt32('length'),
    construct.UBInt32('version'),
    construct.ULInt32('offset'),  # offset to "FirstToC Offset"
    _POINTER(lambda ctx: ctx.offset,
             construct.Struct(
                'ftoc_offset',
                construct.Anchor('abs_offset'),
                construct.ULInt32('offset'),
                _POINTER(lambda ctx: ctx.abs_offset + ctx.offset,
                         _REPEAT(lambda obj, ctx: obj.offset == 0x00000000,
                                 construct.Struct(
                                     'toc',
                                     construct.ULInt32('length'),
                                     construct.SLInt16('type'),
                                     construct.UBInt16('flag'),
                                     construct.ULInt32('level'),
                                     construct.ULInt32('offset'),  # offset to next ToC (0 if none)
                                     construct.ULInt32('count'),
                                     _ARRAY(lambda ctx: ctx.count,
                                            construct.Struct(
                                                'record',
                                                construct.ULInt16('type'),
                                                construct.ULInt16('flag'),
                                                construct.ULInt64('offset'),  # offset to data record
                                                _POINTER(lambda ctx: ctx._._.abs_offset + ctx.offset,
                                                         construct.Struct(
                                                             'data_record',
                                                             construct.ULInt32('length'),
                                                             construct.ULInt32('type'),
                                                             construct.Field('data', lambda ctx: ctx.length)))))))))))
_DATA = construct.Struct(
    'std_data',
    construct.ULInt32('length'),
    construct.ULInt32('type'),
    construct.Field('data', lambda ctx: ctx.length))

def parse():
    result = {}
    bookmark = _BOOKMARK_DATA.parse(raw_data)

    for table in bookmark.ftoc_offset.toc:
        for record in table.record:
            if record.type == 4100:  # pathComponents
                result['path'] = ''
                offsets = interpret(record.data_record, bookmark)  # obtain absolute offsets
                for offset in offsets:
                    path_component = _DATA.parse(raw_data[offset:])
                    result['path'] += '/' + path_component.data.decode('utf-8')
                print('filePath     : {}'.format(result['path']))

            elif record.type == 4101:  # fileIDs
                result['inodes'] = ''
                offsets = interpret(record.data_record, bookmark)  # obtain absolute offsets
                for offset in offsets:
                    inodes = _DATA.parse(raw_data[offset:])
                    result['inodes'] += '/' + str(_INT64.parse(inodes.data))
                print('inodePath    : {}'.format(result['inodes']))

            elif record.type == 4112:  # resourceProps
                print('resourceProps: {}'.format('?'))

            elif record.type == 4160:  # creationDate
                print('creationDate : {}'.format('?'))

            elif record.type == 8192:  # volInfoDepths
                print('volInfoDepths: {}'.format('?'))

            elif record.type == 8194:  # volPath
                result['volPath'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('volPath      : {}'.format(result['volPath']))

            elif record.type == 8197:  # volURL
                result['volURL'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('volURL       : {}'.format(result['volURL']))

            elif record.type == 8208:  # volName
                result['volName'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('volName      : {}'.format(result['volName']))

            elif record.type == 8209:  # volUUID
                result['volUUID'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('volUUID      : {}'.format(result['volUUID']))

            elif record.type == 8272:  # volMountURL
                print('volMountURL  : {}'.format('?'))

            elif record.type == 49169:  # user
                result['user'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('user         : {}'.format(result['user']))

            elif record.type == 61568:  # sandboxInfo
                result['sandbox'] = interpret(record.data_record, bookmark).decode('utf-8')
                print('sandboxInfo  : {}'.format(result['sandbox']))

            else:
                print('record type {} is currently unknown'.format(record.type))

def interpret(data_record, bookmark):
    count = data_record.length // 4
    if data_record.length % 4 > 0:
        count += 1

    if data_record.type == 1537:  # offsets
        offsets = []
        data = data_record.data
        for offset in range(count):
            offsets.append(bookmark.offset + _INT.parse(data))  # base offset + relative offset
            data = data[4:]
        return offsets

    if data_record.type == 257 or 513 or 2305:
        string_data = construct.String('string', data_record.length).parse(data_record.data)
        return string_data

def _debug():
    bookmark = _BOOKMARK_DATA.parse(raw_data)

    print('Magic           : {}'.format(bookmark.magic))
    print('Length          : {}'.format(bookmark.length))
    print('Version         : {}'.format(bookmark.version))
    print('Data Offset     : {}'.format(bookmark.offset))
    print('Absolute Offset : {}'.format(bookmark.ftoc_offset.abs_offset))
    print('First ToC Offset: {}'.format(bookmark.ftoc_offset.offset))

    for table in bookmark.ftoc_offset.toc:
        print('\t===== ToC =====')
        print('\tLength: {}'.format(table.length))
        print('\tType  : {}'.format(table.type))
        print('\tFlag  : {}'.format(table.flag))
        print('\tLevel : {}'.format(table.level))
        print('\tOffset: {}'.format(table.offset))
        print('\tCount : {}'.format(table.count))
        for record in table.record:
            print('\t\t===== Record =====')
            print('\t\tType  : {}'.format(record.type))
            print('\t\tFlag  : {}'.format(record.flag))
            print('\t\tOffset: {}'.format(record.offset))
        print('\t===== End of ToC =====')

parser = argparse.ArgumentParser(description='Parse bookmark data within plists')
parser.add_argument('plist_path', metavar='plist', type=str, nargs=1, help='specify path to plist file')
args = parser.parse_args()
raw_data = None

with open(args.plist_path[0], 'rb') as plist:
    try:
        pl = plistlib.load(plist)
    except ValueError:
        pl = ccl_bplist.load(plist)

    try:
        for item in pl["RecentDocuments"]["CustomListItems"]:
            raw_data = item['Bookmark']
            # _debug()
            parse()
            print('====================')
    except KeyError:
        pass


import rasterio
from pathlib import Path
import yaml

# reading in geotiff file as numpy array
def read_tif(file: Path):
    if not file.exists():
        raise FileNotFoundError(f'File {file} not found')

    with rasterio.open(file) as dataset:
        arr = dataset.read()  # (bands X height X width)
        transform = dataset.transform
        crs = dataset.crs

    return arr.transpose((1, 2, 0)), transform, crs

# writing an array to a geo tiff file
def write_tif(file: Path, arr, transform, crs):
    if not file.parent.exists():
        file.parent.mkdir()

    height, width, bands = arr.shape
    with rasterio.open(
            file,
            'w',
            driver='GTiff',
            height=height,
            width=width,
            count=bands,
            dtype=arr.dtype,
            crs=crs,
            transform=transform,
    ) as dst:
        for i in range(bands):
            dst.write(arr[:, :, i], i + 1)


from __future__ import annotations
from typing import Any, Optional, TYPE_CHECKING

from bgpy.simulation_engine.announcement import Announcement as Ann
from bgpy.simulation_engine.ann_containers import RecvQueue
from bgpy.enums import Relationships

if TYPE_CHECKING:
    from bgpy.simulation_framework import Scenario

def receive_ann(self, ann: Ann, accept_withdrawals: bool = False):
    """Function for recieving announcements, adds to recv_q"""

    if ann.withdraw and not accept_withdrawals:
        raise NotImplementedError("Policy can't handle withdrawals")
    self._recv_q.add_ann(ann)

def process_incoming_anns(
    self,
    *,
    from_rel: Relationships,
    propagation_round: int,
    scenario: "Scenario",
    reset_q: bool = True,
):
    """Process all announcements that were incoming from a specific rel"""

    # For each prefix, get all anns recieved
    for prefix, ann_list in self._recv_q.prefix_anns():
        # Get announcement currently in local rib
        current_ann: Ann = self._local_rib.get_ann(prefix)
        current_processed: bool = True

        # Seeded Ann will never be overriden, so continue
        if getattr(current_ann, "seed_asn", None) is not None:
            continue
        # For each announcement that was incoming
        for ann in ann_list:
            # Make sure there are no loops
            # In ROV subclass also check roa validity
            if self._valid_ann(ann, from_rel):
                # Determine if the new ann is better
                new_ann_better: bool = self._new_ann_better(
                    current_ann, current_processed, from_rel, ann, False, from_rel
                )
                # If new ann is better, replace the current_ann with it
                if new_ann_better:
                    current_ann = ann
                    current_processed = False

        # This is a new best ann. Process it and add it to the local rib
        if current_processed is False:
            current_ann = self._copy_and_process(current_ann, from_rel)
            # Save to local rib
            self._local_rib.add_ann(current_ann)

    self._reset_q(reset_q)

def _valid_ann(self, ann: Ann, recv_relationship: Relationships) -> bool:
    """Determine if an announcement is valid or should be dropped"""

    # BGP Loop Prevention Check
    return self.asn not in ann.as_path

def _copy_and_process(
    self,
    ann: Ann,
    recv_relationship: Relationships,
    overwrite_default_kwargs: Optional[dict[Any, Any]] = None,
) -> Ann:
    """Deep copies ann and modifies attrs

    Prepends AS to AS Path and sets recv_relationship
    """

    kwargs: dict[str, Any] = {
        "as_path": (self.asn,) + ann.as_path,
        "recv_relationship": recv_relationship,
    }

    if overwrite_default_kwargs:
        kwargs.update(overwrite_default_kwargs)
    # Don't use a dict comp here for speed
    return ann.copy(overwrite_default_kwargs=kwargs)

def _reset_q(self, reset_q: bool):
    """Resets the recieve q"""

    if reset_q:
        self._recv_q = RecvQueue()


'''

Part 1 in sigma clipping routine
---------------------------------

Sigma clipping on the not-galaxy spaxels.  This script effectively runs like regular sigma clipping.
However, the galaxy spaxels are first masked out.

'''

__author__ = 'Taylor Hutchison'
__email__ = 'astro.hutchison@gmail.com'

import time
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from fitting_ifu_spectra import * # written by TAH

# specify which galaxy
# --------------------
target = 'SGAS1723'

saveit = True # True or False
sigma = 5

# returns dictionary of info for chosen galaxy
# also path to reduced FITS cubes
galaxy, path, grating = get_galaxy_info(target,grat='g395h')

endname = '-nobg-outlier' #'-nsclean'

# since updated pmap:
pmap_scale = 1e2 # was 1e4 for pmap1027 with old pipeline version

# defining some values
# --------------------
name,filename = galaxy['name'],galaxy['grating'][grating]['filename']
scale,z = galaxy['grating'][grating]['scale']/pmap_scale, galaxy['z']

x,y = galaxy['grating'][grating]['x,y']
filt = galaxy['grating'][grating]['filter']

# before & after NSClean
# filename = 'testing-nsclean-not/pmap1084-88/Level3_SGAS1723_g140h-f100lp_s3d_nsclean.fits'
# filename = 'testing-nsclean-not/pmap1084-88/Level3_SGAS1723_g140h-f100lp_s3d_orig.fits'
# filename = 'testing-nsclean-not/pmap1084-88/Level3_SGAS1723_g395h-f290lp_s3d_nsclean.fits'
# filename = 'testing-nsclean-not/pmap1084-88/Level3_SGAS1723_g395h-f290lp_s3d_orig.fits'

# nsclean figure for overview paper
filename = f'testing-nsclean-not/pmap1105/Level3_SGAS1723_NOBG_OUTLIER_{grating}-{filt}_s3d.fits'

# # TESTING OUTLIER DETECTION STEP
# # NOW THAT IT'S "NEW AND IMPROVED"
# filename = 'testing-outlier-detection/pmap1105/Level3_SGAS1723_BGSUB_OUTLIER_g140h-f100lp_s3d.fits'

# sli = 674 # for SGAS1723, not an emission line
# sli = 300 # for SPT2147, not an emission line
sli = galaxy['grating'][grating]['slice']

if name == 'SGAS1723' and grating == 'g140h':
    x_c,y_c = 7,30 # +bad spaxel
    x_n,y_n = 45,29 # bkgd spaxel
    x_b,y_b = 13,40 # -bad spaxel
elif name == 'SGAS1723' and grating == 'g395h':
    x_c,y_c = 7,30 # +bad spaxel
    x_n,y_n = 45,20 # bkgd spaxel
    x_b,y_b = 8,14 # -bad spaxel
elif name == 'SPT0418':
    x_c,y_c = 40,6 # +bad spaxel
    x_n,y_n = 43,29 # bkgd spaxel
    x_b,y_b = 13,40 # -bad spaxel
elif name == 'SPT2147':
    x_c,y_c = 28,46 # +bad spaxel
    x_n,y_n = 33,8 # bkgd spaxel
    x_b,y_b = 8,30 # -bad spaxel
elif name == 'SGAS1226':
    x_c,y_c = 21,36 # +bad spaxel
    x_n,y_n = 39,7 # bkgd spaxel
    x_b,y_b = 7,30 # -bad spaxel

# cube
data, header = fits.getdata(path+f'{name}/{filename}', header=True)
error = fits.getdata(path+f'{name}/{filename}', ext=2)

slice_1 = data[sli].copy()

# plotting slice
# ---------------

plt.figure(figsize=(8,6))

# plt.imshow(slice_1,clim=(1e-10*pmap_scale,5e-2*pmap_scale),origin='lower',
plt.imshow(slice_1,clim=(-5e-3*pmap_scale,5e-2*pmap_scale),origin='lower',
            cmap='viridis')#,norm=LogNorm())

plt.scatter(x,y,s=40,edgecolor='k',color='C0',lw=1.5) # good pixel
plt.scatter(x_c,y_c,s=40,edgecolor='k',color='g',lw=1.5) # +bad pixel
plt.scatter(x_n,y_n,s=40,edgecolor='k',color='#CB9E0E',lw=1.5) # bkgd pixel
plt.scatter(x_b,y_b,s=40,edgecolor='k',color='r',lw=1.5) # -bad pixell

plt.text(0.975,0.92,f'{name}',transform=plt.gca().transAxes,ha='right',fontsize=20,color='w')

plt.gca().set_yticklabels([])
plt.gca().set_xticklabels([])

plt.tight_layout()
plt.show()
plt.close('all')

# sys.exit(0)

# LOOKING AT 1D AROUND HOT PIXEL
# setting wavelenth array & making spec
# -------------------------------------
bigwave = np.arange(header['CRVAL3'],
                 header['CRVAL3']+(header['CDELT3']*len(data)),
                 header['CDELT3'])

sli_wave = bigwave[sli]

# slicing out line cube
if grating[-1] == 'h': w_win = 0.0085
elif grating[-1] == 'm': w_win = 0.05

wavemin,wavemax = sli_wave-w_win, sli_wave+w_win
wave_mask = np.where((bigwave<wavemax)&(bigwave>wavemin),bigwave,-1)
wave_mask = np.arange(len(wave_mask))[wave_mask > 0]

wave = bigwave[wave_mask]
ldata = data[wave_mask].copy()
lerror = error[wave_mask].copy()

def get_spec(x,y,d=ldata,e=lerror):
    # doing it this way to circumvent the Big Endian pandas error
    dat = [float(f) for f in d[:,int(y),int(x)].copy()]
    err = [float(f) for f in e[:,int(y),int(x)].copy()]

    spec = pd.DataFrame({'wave':wave,'flam':dat,'flamerr':err})
    spec = spec_wave_range(spec,[wavemin,wavemax])
    spec = convert_MJy_cgs(spec.copy())
    return spec.copy()

# pulling spectrum
spec = get_spec(x,y)
spec_good = get_spec(x_c,y_c)
spec_bkgd = get_spec(x_n,y_n)
spec_nbad = get_spec(x_b,y_b)

plt.figure(figsize=(8,5))

plt.step(spec.wave,spec.flam/scale,where='mid')
plt.axvline(sli_wave,color='k')

plt.step(spec_good.wave,spec_good.flam/scale,where='mid',color='g')
plt.step(spec_bkgd.wave,spec_bkgd.flam/scale,where='mid',color='#CB9E0E')
plt.step(spec_nbad.wave,spec_nbad.flam/scale,where='mid',color='r')

y_scale = np.nanmean([spec_good.median().flam,spec_bkgd.median().flam,
                        spec_nbad.median().flam]) / scale
if target == 'SGAS1723' and grating == 'g140h': y_scale *= 1e1
plt.ylim(-50*y_scale,80*y_scale)

plt.tight_layout()
plt.show()
plt.close('all')

# sys.exit(0)

# sigma clipping NOT the galaxy

# mask = get_mask(name,array_2d=True) # want the 2D array, not coord list
mask, mask_info = get_mask(name,array_2d=True,layers=True,grating='g140h')
full_mask = mask[0].copy()

masked_slice = slice_1.copy()
masked_slice[full_mask>0] = np.nan

# sigma clipping masked slice
with warnings.catch_warnings():  # Ignore warnings
    warnings.simplefilter('ignore')
    masked_slice_clipped = sigma_clip(masked_slice, sigma=sigma, maxiters=5)

# plotting slice with mask
# ------------------------
plt.figure(figsize=(12,6))
gs = gridspec.GridSpec(1,2,width_ratios=[1,1],wspace=0)

ax = plt.subplot(gs[0])

ax.imshow(masked_slice,clim=(-5e-3*pmap_scale,5e-2*pmap_scale),origin='lower',
           cmap='viridis')#,norm=LogNorm())

ax.scatter(x,y,s=40,edgecolor='k',color='C0',lw=1.5) # good pixel
ax.scatter(x_c,y_c,s=40,edgecolor='k',color='g',lw=1.5) # +bad pixel
ax.scatter(x_n,y_n,s=40,edgecolor='k',color='#CB9E0E',lw=1.5) # bkgd pixel
ax.scatter(x_b,y_b,s=40,edgecolor='k',color='r',lw=1.5) # -bad pixell

ax.text(0.975,0.92,f'{name}',transform=ax.transAxes,ha='right',fontsize=20,color='w')
ax.set_yticklabels([])
ax.set_xticklabels([])

ax = plt.subplot(gs[1])

ax.imshow(masked_slice_clipped,clim=(-5e-3*pmap_scale,5e-2*pmap_scale),origin='lower',
           cmap='viridis')#,norm=LogNorm())

ax.text(0.975,0.92,f'{name}',transform=ax.transAxes,ha='right',fontsize=20,color='w')
ax.set_yticklabels([])
ax.set_xticklabels([])

plt.tight_layout()
plt.show()
plt.close('all')

# sys.exit(0)

# clipping slices
# ---------------

# making cube of zeros to log when a pixel has been clipped
clipped_pixels = np.zeros(data.shape) # will be 1 in slice if pixel is clipped

# copy of original cube & error
data_clipped = data.copy()
error_clipped = error.copy()

for i in range(len(data_clipped)):
    data_slice = data[i].copy()
    error_slice = error[i].copy()

    # first, masking out galaxy spaxels
    data_slice[full_mask>0] = np.nan
    error_slice[full_mask>0] = np.nan

    mask_data_slice = data_slice.copy()
    mask_error_slice = error_slice.copy()

    # marking the pixels that were nans before
    nans_before = np.zeros(mask_data_slice.shape)
    nans_before[np.isnan(mask_data_slice) == True] = 1

    # first checking for the all-NaN slices in IFU
    # if yes, nothing happens for that slice and it just
    # gets added to the final cube
    if np.isnan(mask_data_slice).all() == False:
        # sigma clipping
        with warnings.catch_warnings():  # Ignore warnings
            warnings.simplefilter('ignore')

            clip_mask = sigma_clip(mask_data_slice, sigma=sigma, maxiters=5).mask

        clip_mask[full_mask>0] = False # re-masking out the galaxy

        # replaced flagged things with nanmedian (WAS nanmean)
        mask_data_slice[clip_mask] = np.nanmedian(mask_data_slice)
        mask_error_slice[clip_mask] = np.nanmedian(mask_error_slice)

        # logging pixels that were clipped in separate cube
        clipped_pixels[i][clip_mask] = 1
        # removing pixels that were NaNs before
        clipped_pixels[i][nans_before == 1] = 0

        # adding back in nans that were there before
        mask_data_slice[nans_before == 1] = np.nan
        mask_error_slice[nans_before == 1] = np.nan

    # adding back into cube
    data_clipped[i] = mask_data_slice.copy()
    error_clipped[i] = mask_error_slice.copy()

# LOOKING AT CHANGES
wave = bigwave[wave_mask]
ldata_clipped = data_clipped[wave_mask].copy()
lerror_clipped = error_clipped[wave_mask].copy()

# pulling spectrum
spec = get_spec(x,y,d=ldata_clipped,e=lerror_clipped)
spec_good = get_spec(x_c,y_c,d=ldata_clipped,e=lerror_clipped)
spec_bkgd = get_spec(x_n,y_n,d=ldata_clipped,e=lerror_clipped)
spec_nbad = get_spec(x_b,y_b,d=ldata_clipped,e=lerror_clipped)

plt.figure(figsize=(8,5))

plt.step(spec.wave,spec.flam/scale,where='mid')
plt.axvline(sli_wave,color='k')

plt.step(spec_good.wave,spec_good.flam/scale,where='mid',color='g')
plt.step(spec_bkgd.wave,spec_bkgd.flam/scale,where='mid',color='#CB9E0E')
plt.step(spec_nbad.wave,spec_nbad.flam/scale,where='mid',color='r')

# plt.ylim(-5*pmap_scale,8*pmap_scale)
plt.ylim(-50*y_scale,80*y_scale)

plt.tight_layout()
plt.show()
plt.close('all')

# sys.exit(0)

# writing out sigma clipped line flux FITS cube to file
# using the header from the reduced s3d cube to preserve WCS & wavelength
# -----------------------------
if saveit == True:
    pieces_path = 'plots-data/data-reduction/sigma-clipping-pieces/'

    hdu = fits.PrimaryHDU(header=header)
    hdu1 = fits.ImageHDU(data_clipped,header=header) # the data cube
    hdu2 = fits.ImageHDU(error_clipped,header=header) # the error cube
    hdu3 = fits.ImageHDU(clipped_pixels,header=header) # the clipped pixels logging
    hdul = fits.HDUList([hdu, hdu1, hdu2, hdu3])
    hdul.writeto(f'{pieces_path}/{name}-sigmaclipping-bkgd-{grating}-s3d{endname}.fits',overwrite=True)
    print('\nsigma clipped FITS cube saved.  Exiting script...',end='\n\n')

# CODE TO USE IN TROUBLESHOOTING
# -----------------------------

# meant to be used for a subset of the full cube
# ldata instead of data

# # pulling spectrum
# cspec = get_spec(x,y,d=ldata_clipped,e=lerror_clipped)
# cspec_good = get_spec(x_c,y_c,d=ldata_clipped,e=lerror_clipped)
# cspec_bkgd = get_spec(x_n,y_n,d=ldata_clipped,e=lerror_clipped)
# cspec_nbad = get_spec(x_b,y_b,d=ldata_clipped,e=lerror_clipped)

# plt.figure(figsize=(8,5))

# plt.step(cspec.wave,cspec.flam/scale,where='mid')
# plt.axvline(sli_wave,color='k')

# plt.step(cspec_good.wave,cspec_good.flam/scale,where='mid',color='g')
# plt.step(cspec_bkgd.wave,cspec_bkgd.flam/scale,where='mid',color='#CB9E0E')
# plt.step(cspec_nbad.wave,cspec_nbad.flam/scale,where='mid',color='r')

# plt.ylim(-5,8)

# plt.tight_layout()
# plt.show()
# plt.close('all')

# # plotting slice with mask
# # ------------------------
# plt.figure(figsize=(12,6))
# gs = gridspec.GridSpec(1,2,width_ratios=[1,1],wspace=0)

# new_sli = 36

# ax = plt.subplot(gs[0])

# ax.imshow(ldata[new_sli],clim=(-5e-3,5e-2),origin='lower',
#            cmap='viridis')#,norm=LogNorm())

# ax.set_yticklabels([])
# ax.set_xticklabels([])

# ax = plt.subplot(gs[1])

# ax.imshow(ldata_clipped[new_sli],clim=(-5e-3,5e-2),origin='lower',
#            cmap='viridis')#,norm=LogNorm())

# ax.set_yticklabels([])
# ax.set_xticklabels([])

# plt.tight_layout()
# plt.show()
# plt.close('all')


# VPN WITH PROXY BY POLAIV | https://github.com/polaiv

import tkinter
import winreg
import customtkinter
from PIL import Image

class ToplevelWindow(customtkinter.CTkToplevel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.geometry('400x300')
        self.label = customtkinter.CTkLabel(self, text='Error')
        self.label.pack(padx=20, pady=20)

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.geometry('495x595')
        self.title('VPN_WITH_PROXY')
        self.resizable(False, False)
        self.toplevel_window = None
        try:
            self.logo = customtkinter.CTkImage(dark_image=Image.open('vpn_logo.png'), size=(460, 460))
            self.logo_label = customtkinter.CTkLabel(master=self, text='', image=self.logo)
            self.logo_label.place(relx=0.5, rely=0.20, anchor=tkinter.CENTER)
        except:
            self.lbl_error = customtkinter.CTkLabel(self, text='most likely, the logo image \n is not in the same directory')
            self.lbl_error.place(relx=0.5, rely=0.20, anchor=tkinter.CENTER)

        self.lbl_enable = customtkinter.CTkLabel(self, text='VPN was successfully enabled ✅')
        self.lbl_disable = customtkinter.CTkLabel(self, text='VPN was successfully turned off ❌')

        self.btn = customtkinter.CTkButton(self, text="Enable VPN ✅", command=self.toggle_vpn)
        self.btn.place(relx=0.5, rely=0.7, anchor=tkinter.CENTER)

    def toggle_vpn(self):
        if self.btn.cget("text") == "Enable VPN ✅":
            self.btn.configure(text="Turn off VPN ❌")
            self.set_proxy_enabled(True)
            self.lbl_disable.place_forget()
            self.lbl_enable.place(relx=0.5, rely=0.8, anchor=tkinter.CENTER)
        else:
            self.btn.configure(text="Enable VPN ✅")
            self.set_proxy_enabled(False)
            self.lbl_enable.place_forget()
            self.lbl_disable.place(relx=0.5, rely=0.8, anchor=tkinter.CENTER)

    def set_proxy_enabled(self, enabled):
        try:
            registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                                          r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
                                          0, winreg.KEY_WRITE)

            if enabled:
                winreg.SetValueEx(registry_key, 'ProxyEnable', 0, winreg.REG_DWORD, 1)
            else:
                winreg.SetValueEx(registry_key, 'ProxyEnable', 0, winreg.REG_DWORD, 0)

            winreg.CloseKey(registry_key)

        except Exception as e:
            self.open_toplevel()

if __name__ == '__main__':
    app = App()
    app.mainloop()


from typing import List

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        res = []
        def recurr(idx, sub):
            if idx == len(nums):
                res.append(sub[::])
                return
            # if item gets picked
            sub.append(nums[idx])
            recurr(idx+1, sub)
            sub.pop()
            # if not picked
            recurr(idx+1, sub)

        recurr(0, [])
        return res

solution = Solution()
nums = [1, 2, 3]
result = solution.subsets(nums)
print(result)


from django.contrib.auth import models
from django.contrib.auth.models import PermissionsMixin

class DiscordOAuthManager(models.UserManager):
    def create_new(self, user):
        __tag = '{}#{}'.format(user.get('username'), user.get('discriminator'))
        __user = self.create(
            user_id=user.get('id'),
            avatar=user.get('avatar'),
            public_flags=user.get('public_flags'),
            flags=user.get('flags'),
            locale=user.get('locale'),
            mfa_enabled=user.get('mfa_enabled'),
            tag=__tag,
            is_active=False
        )
        return __user


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

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import xlrd

print("Iniciando nosso robô... \n")

arq = open("resultados.txt", "w")

dominios = []

#Lendo do Excel
workbook = xlrd.open_workbook('dominio.xlsx')
sheet = workbook.sheet_by_index(0)
for linha in range(0,8):
    dominios.append(sheet.cell_value(linha,0))

driver = webdriver.Chrome('/home/marcos/Projetos/robos/RobosAutomatico/chromedriver')
driver.get("https://registro.br/")

for dominio in dominios:
    pesquisa = driver.find_element_by_id("is-avail-field")
    pesquisa.clear() # Limpando Input de Pesquisa
    pesquisa.send_keys(dominio)
    pesquisa.send_keys(Keys.RETURN)
    time.sleep(2); #Dormi 2 s
    resultados = driver.find_elements_by_tag_name("strong")
    #import pdb; pdb.set_trace() Break pdb
    texto = "Dominio %s %s\n" % (dominio, resultados[4].text)
    arq.write(texto.encode('utf-8', errors='ignore'))

arq.close()
driver.close()


from django.urls import path
from . import views

urlpatterns = [
    path('index', views.index_view, name='index_page'),
    path('course/<str:title>', views.topic_view, name='topic_page'),
    path('search', views.search_view, name='search_page'),
    path('new', views.new_page_view, name='new_page'),
    path('edit/<str:title>', views.edit_page_view, name='edit_page'),
    path('random', views.random_page_view, name='random_page'),
]


# ! /usr/env/bin python3
# -*-encoding utf-8-*-

"""
--------------------------------------------------------------------------------
    Stacked histogram
--------------------------------------------------------------------------------
Author: Simone Santoni, simone.santoni.1@city.ac.uk;
        credits to https://github.com/selva86
Dates: - created Thu Oct 24 11:06:17 UTC 2019
       - last change Thu Oct 24 11:06:17 UTC 2019
Notes: This script produces a histogram that shows the distribution of a
       focal variable conditionally on one or multiple categorical variables
"""

# %% libraries
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd

# %%  matplotlib viz setup
plt.style.use('seaborn-bright')
rc('font',**{'family':'sans-serif','sans-serif':['Avant Garde']})
rc('text', usetex=True)

# %% fake data
'''
Simulate three random variables drawn from different distributions
'''
# contoinuous vars
x = np.random.normal(loc=-1, scale=0.75, size=1000)
y = np.random.normal(loc=0, scale=1, size=1000)
z = np.random.normal(loc=1, scale=0.5, size=1000)

# qualitative categories
categories = ['x', 'y', 'z']

np.shape(x)

# %% create figure

# figure
fig = plt.figure(figsize=(6, 4))

# add figure
ax = fig.add_subplot(1, 1, 1)

# plot data
ax.hist([x, y, z], bins=50, histtype="barstacked", density=True,
         label=categories, color=["darkorange", "darkorchid", "lime"],
         alpha=0.6)

# axes
ax.set_xlabel("Values")
ax.set_ylabel("Density")

# legend
ax.legend(loc="best")

# save plot
out_f = os.path.join(os.getcwd(), 'stacked_histogram.pdf')
plt.savefig(out_f,
            transparent=True,
            bbox_inches='tight',
            pad_inches=0)

# show plot
plt.show()


import traceback

from rest_framework import serializers

from .models import Entry

class EntrySerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        ModelClass = self.Meta.model
        try:
            instance = ModelClass.objects.using('mongo').create(**validated_data)
        except TypeError:
            tb = traceback.format_exc()
            msg = (
                'Got a `TypeError` when calling `%s.%s.create()`. '
                'This may be because you have a writable field on the '
                'serializer class that is not a valid argument to '
                '`%s.%s.create()`. You may need to make the field '
                'read-only, or override the %s.create() method to handle '
                'this correctly.\nOriginal exception was:\n %s' %
                (
                    ModelClass.__name__,
                    ModelClass._default_manager.name,
                    ModelClass.__name__,
                    ModelClass._default_manager.name,
                    self.__class__.__name__,
                    tb
                )
            )
            raise TypeError(msg)
        return instance

    class Meta:
        model = Entry
        fields = '__all__'


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('logout_user', views.logout_user, name = 'logout'),
    path('register_user', views.register_user, name = 'register'),
    path('detail_record/<int:pk>', views.detail_record, name = 'detail'),
    path('delete_record/<int:pk>', views.delete_record, name = 'delete'),
    path('update_user/<int:pk>', views.update_user, name = 'update'),

    path('add_record', views.add_record, name = 'add_record'),

]


import os
import yaml
from datetime import timedelta, datetime
from airflow.models import DAG, Variable
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.bash_operator import BashOperator

AIRFLOW_ENV_VAR = 'airflow_env'
ENV = Variable.get(AIRFLOW_ENV_VAR)
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = "config_{}.yaml".format(ENV)

with open(os.path.join(CURRENT_DIR, CONFIG_FILE)) as config:
    conf = yaml.load(config, Loader=yaml.FullLoader)
    pipeline_conf = conf['pipeline']
    dag_args = conf['dag_args']

STARTDATE = datetime.strptime(dag_args['start_date'], '%Y%m%d')

default_dag_args = {
    'dag_id': pipeline_conf['name'],
    'start_date': STARTDATE,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': dag_args['retries'],
    'retry_delay': timedelta(minutes=dag_args['delay']),
    'catchup': dag_args['catchup']
}

dag = DAG(
    dag_id=pipeline_conf['name'],
    default_args=default_dag_args,
    schedule_interval=dag_args['schedule_interval'])

start = DummyOperator(task_id='start', dag=dag)

run_pipeline = BashOperator(
    task_id='run_pipeline',
    bash_command='bash '+CURRENT_DIR+'/execute.sh ' + pipeline_conf['date'] + ' ' + pipeline_conf['env'],
    dag=dag
)
start >> run_pipeline


from typing import Any, Optional
import json

class Solution:
    key = "12"

    def __init__(self, input: str):
        self.input = input

    def part_one(self) -> int:
        data = self.parse()
        return self.traverse(data)

    def part_two(self) -> int:
        data = self.parse()
        return self.traverse(data, True)

    def parse(self):
        return json.loads(self.input)

    def traverse(self, data: Any, drop_red=False) -> int:
        acc = 0

        if isinstance(data, dict):
            if drop_red and "red" in data.values():
                return acc

            for key in data:
                acc += self.traverse(data[key], drop_red)
        elif isinstance(data, list):
            for item in data:
                acc += self.traverse(item, drop_red)
        elif isinstance(data, int):
            acc += data

        return acc

if __name__ == "__main__":
    with open(f"./inputs/{Solution.key}.txt", "r") as fh:
        input = fh.read().strip()
    print("Part 1:", Solution(input).part_one())
    print("Part 2:", Solution(input).part_two())


from math import sqrt
import numpy as np
import matplotlib.pyplot as plt

def createList(n):
    numbers = np.random.uniform(0, 1, n)
    return numbers

def expecValue(n):
    M = sum(createList(n)) / n
    return M

def dispersion(list, M):
    sumPow = 0
    for i in range(0, n):
        sumPow += (list[i] - M) ** 2
    D = sumPow / n
    return D

def densityFunc(n, numbers):
    array = []
    for i in range(1, n):
        a = 0
        b = numbers[i]
        c = 1/(b-a)
        if numbers[i] >= a and numbers[i] <= b:
            array.append(c)
        else:
            array.append(0)
    return array

def autocorFunc(n, M, numbers):
    K = []
    for i in range(1, n):
        f = i
        sum1 = 0
        sum2 = 0
        for j in range(1, n - f):
            sum1 += (numbers[j] - M) * (numbers[j + f] - M)
        for g in range(1, n):
            sum2 += (numbers[g] - M) ** 2
        K.append(sum1/sum2)
    return K

n = 10
print("N = ", n)
numbers1 = createList(n)
M = expecValue(n)
D = dispersion(numbers1, M)
S = sqrt(D)
print("Numbers: \n", numbers1)
print("M: ", M)
print("D: ", D)
print("S: ", S)
K1 = autocorFunc(n, M, numbers1)
locs = np.arange(len(K1))
width = 0.75
plt.bar(locs, K1, width=width)
plt.ylabel('K(f)')
plt.xlabel('f')
plt.title('Correlogram, n=10')
plt.savefig('correl1.png')
plt.close()
densityFunc(n, numbers1)
weights = np.ones_like(numbers1) / float(len(numbers1))
plt.hist(numbers1, weights=weights)
plt.ylabel('f(x)')
plt.xlabel('x')
plt.title('density function, n=10')
plt.savefig('denstFunct1.png')
numbers1.sort()
plt.close()
plt.plot(numbers1 / n)
plt.ylabel('F(x)')
plt.xlabel('x')
plt.title('integrate function, n=10')
plt.savefig("integFunct1.png")
plt.close()
print("\n")

n = 100
print("N = ", n)
numbers2 = createList(n)
M = expecValue(n)
D = dispersion(numbers2, M)
S = sqrt(D)
print("Numbers: \n", numbers2)
print("M: ", M)
print("D: ", D)
print("S: ", S)
K2 = autocorFunc(n, M, numbers2)
locs = np.arange(len(K2))
width = 0.75
plt.bar(locs, K2, width=width)
plt.ylabel('K(f)')
plt.xlabel('f')
plt.title('Correlogram, n=100')
plt.savefig('correl2.png')
plt.close()
densityFunc(n, numbers2)
weights = np.ones_like(numbers2) / float(len(numbers2))
plt.hist(numbers2, weights=weights)
plt.ylabel('f(x)')
plt.xlabel('x')
plt.title('density function, n=100')
plt.savefig('denstFunct2.png')
numbers2.sort()
plt.close()
plt.plot(numbers2 / n)
plt.ylabel('F(x)')
plt.xlabel('x')
plt.title('integrate function, n=100')
plt.savefig("integFunct2.png")
plt.close()
print("\n")

n = 1000
print("N = ", n)
numbers3 = createList(n)
M = expecValue(n)
D = dispersion(numbers3, M)
S = sqrt(D)
print("Numbers: \n", numbers3)
print("M: ", M)
print("D: ", D)
print("S: ", S)
K3 = autocorFunc(n, M, numbers3)
locs = np.arange(len(K3))
width = 0.75
plt.bar(locs, K3, width=width)
plt.ylabel('K(f)')
plt.xlabel('f')
plt.title('Correlogram, n=1000')
plt.savefig('correl3.png')
plt.close()
densityFunc(n, numbers3)
weights = np.ones_like(numbers3) / float(len(numbers3))
plt.hist(numbers3, weights=weights)
plt.ylabel('f(x)')
plt.xlabel('x')
plt.title('density function, n=1000')
plt.savefig('denstFunct3.png')
numbers3.sort()
plt.close()
plt.plot(numbers3 / n)
plt.ylabel('F(x)')
plt.xlabel('x')
plt.title('integrate function, n=1000')
plt.savefig("integFunct3.png")
plt.close()
print("\n")

n = 10000
K4 = []
print("N = ", n)
numbers4 = createList(n)
M = expecValue(n)
D = dispersion(numbers4, M)
S = sqrt(D)
print("Numbers: \n", numbers4)
print("M: ", M)
print("D: ", D)
print("S: ", S)
K4 = autocorFunc(n, M, numbers4)
locs = np.arange(len(K4))
width = 0.75

plt.bar(locs, K4, width=width)
plt.ylabel('K(f)')
plt.xlabel('f')
plt.title('Correlogram, n=10000')
plt.savefig('correl4.png')
plt.close()
densityFunc(n, numbers4)
weights = np.ones_like(numbers4) / float(len(numbers4))
plt.hist(numbers4, weights=weights)
plt.ylabel('f(x)')
plt.xlabel('x')
plt.title('density function, n=10000')
plt.savefig('denstFunct4.png')
numbers4.sort()
plt.close()
plt.plot(numbers4 / n)
plt.ylabel('F(x)')
plt.xlabel('x')
plt.title('integrate function, n=10000')
plt.savefig("integFunct4.png")
plt.close()
print("\n")


import numpy

import numpy as np
import json
import corpus_resource
fp = "corpus_resource/n_gram/2014_corpus_1gram.txt"
fpp = "corpus_resource/POS_source/word_class_static.txt"
fonp = "corpus_resource/POS_source/pw_matrix"
fotxt = "corpus_resource/POS_source/pw_matrix.txt"

fc = "D:\\resource\\STUDY\\NLP\\2014_corpus_processed.txt"

if __name__ == '__main__':
    with open(fp, 'r')as f:
        gram1 = json.load(f)
    with open(fpp, 'r')as ff:
        word_class = json.load(ff)

    corpus = open(fc,"r")

    print(len(word_class))

    word_class_list = list(word_class.keys())

    num_word_class = len(word_class)

    wp_matrix = np.zeros([num_word_class,num_word_class])

    for line in corpus:
        line = line.strip()
        words = line.split()
        #print(line)
        #print(words)
        for (word_index,word) in enumerate(words):
            #print(word)
            if word_index == 0:
                cur_word_class = "start"
            else:
                cur_word_record = words[word_index].split("/")
                #cur_word = cur_word_record[0].replace("[","").replace("]","")
                cur_word_class = cur_word_record[-1].replace("[","").replace("]","")
            #print(cur_word_class)
            try:
                from_class_matrix_index = word_class_list.index(cur_word_class)
            except:
                continue

            if word_index == len(words)-1:
                next_word_class = "end"

            else:

                next_word_record = words[word_index+1].split("/")
                #next_word = next_word_record[0].replace("[","").replace("]","")
                next_word_class = next_word_record[-1].replace("[","").replace("]","")

            print(cur_word_class,next_word_class)
            try:
                to_class_matrix_index = word_class_list.index(next_word_class)
            except:
                continue

            wp_matrix[from_class_matrix_index][to_class_matrix_index]+=1

    print(wp_matrix)
    np.save(fonp, wp_matrix)
    np.savetxt(fotxt, wp_matrix, fmt=numpy.float, delimiter=',')


#! /usr/bin/env python
import sys, os.path
from nose.tools import with_setup
import urllib2
from wsgi_intercept import urllib2_intercept
import wsgi_intercept
from wsgi_intercept import test_wsgi_app

_saved_debuglevel = None

def add_intercept():
    _saved_debuglevel, wsgi_intercept.debuglevel = wsgi_intercept.debuglevel, 1
    wsgi_intercept.add_wsgi_intercept('some_hopefully_nonexistant_domain', 80, test_wsgi_app.create_fn)

def add_https_intercept():
    _saved_debuglevel, wsgi_intercept.debuglevel = wsgi_intercept.debuglevel, 1
    wsgi_intercept.add_wsgi_intercept('some_hopefully_nonexistant_domain', 443, test_wsgi_app.create_fn)

def remove_intercept():
    wsgi_intercept.debuglevel = _saved_debuglevel
    wsgi_intercept.remove_wsgi_intercept()

@with_setup(add_intercept, remove_intercept)
def test():
    urllib2_intercept.install_opener()
    urllib2.urlopen('http://some_hopefully_nonexistant_domain:80/')
    assert test_wsgi_app.success()

@with_setup(add_https_intercept, remove_intercept)
def test_https():
    urllib2_intercept.install_opener()
    urllib2.urlopen('https://some_hopefully_nonexistant_domain:443/')
    assert test_wsgi_app.success()

@with_setup(add_intercept, remove_intercept)
def test_https_default_port():
    # I guess the default port for https is 80 but I thoght it would be 443
    urllib2_intercept.install_opener()
    urllib2.urlopen('https://some_hopefully_nonexistant_domain/')
    assert test_wsgi_app.success()


# Import the running_stats function
from helper import running_stats
import numpy as np
from astropy.io import fits
# Write your median_bins_fits and median_approx_fits here:

def get_data(images):
  a = np.array([])

  for image in images:
    hdulist = fits.open(image)
    data = hdulist[0].data
    if a == []:
      a = data
    else:
      a = np.append(a, data)

  a = np.reshape(a, (len(images), len(data), len(data[0])))

  return a

def median_bins_fits(images, bins):
  mean, stdv = running_stats(images)
  a = get_data(images)

  minval = mean - stdv
  maxval = mean + stdv
  width = 2 * stdv / bins

  left = a[0:len(a)] < minval
  left_bin = np.sum(left, axis = (0))

  in_bins = []
  x = minval
  i = 0
  while  i < bins:
    passend = (a[0:len(a)] >= x) & (a[0:len(a)] < x + width)
    s = np.sum(passend, axis = (0))
    if in_bins == []:
       in_bins = s
    else:
       in_bins = np.dstack((in_bins, s))
    x += width
    i += 1

  return (mean, stdv, left_bin, np.array(in_bins))

def median_approx_fits(images, bins):
  mean, std, left_bin, in_bins = median_bins_fits(images, bins)

  minval = mean - std
  maxval = mean + std
  width = 2 * std / bins

  x = minval
  total = left_bin
  i = 0
  #while np.all((x < maxval) & (total < (len(images) + 1)/2)):
  if (len(images) % 2 == 0):
    t = len(images) // 2
  else:
    t = len(images) // 2 + 1

  while (i < bins) & np.all(total <= t):
    total += np.sum(in_bins[:,:,i])
    i += 1
    x += width

  if (bins % 2 == 0):
    return x + width/2
  else:
    return x - width/2

# You can use this to test your function.
# Any code inside this `if` statement will be ignored by the automarker.
if __name__ == '__main__':
  # Run your function with examples from the question.
  mean, std, left_bin, bins = median_bins_fits(['image0.fits', 'image1.fits', 'image2.fits'], 5)
  median = median_approx_fits(['image0.fits', 'image1.fits', 'image2.fits'], 5)
  print(mean[100,100])
  print(std[100,100])
  print(left_bin[100,100])
  print(bins[100,100,:])
  print(median[100, 100])

  mean, std, left_bin, bins = median_bins_fits(['image{}.fits'.format(str(i)) for i in range(11)], 4)
  median = median_approx_fits(['image{}.fits'.format(str(i)) for i in range(11)], 4)
  print(mean[100,100])
  print(std[100,100])
  print(left_bin[100,100])
  print(bins[100,100,:])
  print(median[100, 100])


# -*- coding: utf_8 -*-

from interpreter.exceptions import CommandNotInThisModule
from interpreter.structs import Result

import sqlite3

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

    def initDbConnection(self):
        self.connection = sqlite3.connect("./modules/vocable/vocables.db")
        self.cursor = self.connection.cursor()

    def getCommands(self):
        return {
            "dictionary.commands" : self.commands,

            "dictionary.hebrew" : self.hebrew,
            "dictionary.greek" : self.greek,
            "dictionary.aramaic" : self.aramaic,
            "dictionary.akkadian" : self.akkadian,
        }

    def interpreter(self, command, args, queue):
        print("args:", args)

        commands = self.getCommands()

        return commands.get(command, self.commandNotFound)(command, args)

    def commandNotFound(self, c, a):
        raise CommandNotInThisModule("command not found in module quran")

    def commands(self, c, a):
        dic = self.getCommands()

        commands = sorted(dic.items())

        all_commands = []
        for key in commands:
            line = str(key).split(",")[0]
            all_commands.append(str(line[2:-1]))

        result_object = Result()
        result_object.category = "list"
        result_object.payload = all_commands
        return result_object

    def hebrew(self, c, args):
        return self.dictionaryHelper(args, 'hebrew')
    def greek(self, c, args):
        return self.dictionaryHelper(args, 'greek')
    def aramaic(self, c, args):
        return self.dictionaryHelper(args, 'aramaic')
    def akkadian(self, c, args):
        return self.dictionaryHelper(args, 'akkadian')

    def dictionaryHelper(self, args, language):
        result_object = Result()

        query = """
        SELECT display, gloss
        FROM {0}
        WHERE display LIKE ? OR gloss LIKE ?
        """.format(language)
        try:
            param = '%'+str(args[0])+'%'
        except IndexError:
            result_object.error = 'invalid parameter'
        else:
            self.initDbConnection()
            self.cursor.execute(query, [param, param])
            result_object.payload = self.cursor.fetchall()

        result_object.category = "itemized"
        result_object.name = "dictionary result"
        return result_object


from airflow import DAG
from airflow.operators.python import PythonOperator

from abc import abstractmethod
from datetime import datetime, timedelta

import pandas as pd
import numpy as np
from typing import Dict
import ticker_crawler
import pytz
from miner import Miner

class RSMiner(Miner):
    def __init__(self, input_tables: Dict[str, int], indicator_name: str, recursive_range: int):
        super().__init__(input_tables, indicator_name, recursive_range)

    def get_inputs(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        """
        :param output_list: list containing temp values of calculated indicator
        :param current_date: the date of current time in ISO Format; example: "2023-12-31T00:00"
        :param dict_input_dfs: dict of input dataframes with keys as input_table + "_data"
        :return: input variables for the formula
        """

        if not current_date.endswith(".000Z"):
            current_date += ".000Z"
        ticker_data = dict_input_dfs["ticker_data"]
        ticker_data = ticker_data.reset_index(drop=True)

        row_index_list = ticker_data.index[ticker_data['timestamp'] == current_date].tolist()
        if len(row_index_list) == 0:
            print("invalid timestamp")
            return None

        current_row_index = row_index_list[0]
        if current_row_index < 13:
            print(f"current_row_index is {current_row_index}, less than 13")
            return None

        close_list = (ticker_data.loc[current_row_index - 13:current_row_index, ["close"]])["close"].to_list()
        close_list = [int(close) for close in close_list]
        gain_list = []
        loss_list = []

        for i in range(1, len(close_list) - 1):
            diff = close_list[i] - close_list[i - 1]
            if diff >= 0:
                gain_list.append(diff)
            else:
                loss_list.append(diff)

        # time = ticker_data.loc[current_row_index, ["timestamp"]]
        #
        # if len(gain_list) == 0:
        #     print(f"gain_list of {time}: {len(gain_list)}")

        avg_gain = sum(gain_list) / len(gain_list) if len(gain_list) != 0 else 0
        avg_loss = sum(loss_list) / len(loss_list) if len(loss_list) != 0 else 0
        return avg_gain, avg_loss

    def formula(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        inputs = self.get_inputs(current_date, dict_input_dfs, output_list)
        if inputs is None:
            rs = 0
        else:
            avg_gain, avg_loss = inputs
            rs = avg_gain / np.abs(avg_loss) if avg_loss != 0 else 0

        output_list.append(rs)
        return output_list, rs

class RSIMiner(Miner):
    def __init__(self, input_tables: Dict[str, int], indicator_name: str, recursive_range: int):
        super().__init__(input_tables, indicator_name, recursive_range)

    def get_inputs(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        """
        :param output_list: list containing temp values of calculated indicator
        :param current_date: the date of current time in ISO Format; example: "2023-12-31T00:00"
        :param dict_input_dfs: dict of input dataframes with keys as input_table + "_data"
        :return: input variables for the formula
        """

        if not current_date.endswith(".000Z"):
            current_date += ".000Z"
        rs_data = dict_input_dfs["rs_data"]
        rs_data = rs_data.reset_index(drop=True)

        row_index_list = rs_data.index[rs_data['timestamp'] == current_date].tolist()
        if len(row_index_list) == 0:
            print("invalid timestamp")
            return None

        current_row_index = row_index_list[0]
        rs = rs_data.loc[current_row_index]["rs_data"]

        return rs

    def formula(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        rs = self.get_inputs(current_date, dict_input_dfs, output_list)
        if rs is None:
            rsi = 0
        else:
            rsi = 100 / (100 + rs)

        output_list.append(rs)
        return output_list, rsi

class OBVMiner(Miner):
    def __init__(self, input_tables, indicator_name, recursive_range):
        super().__init__(input_tables, indicator_name, recursive_range)

    def get_inputs(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        if not current_date.endswith(".000Z"):
            current_date += ".000Z"
        ticker_data = dict_input_dfs["ticker_data"]
        ticker_data = ticker_data.reset_index(drop=True)

        row_index_list = ticker_data.index[ticker_data['timestamp'] == current_date].tolist()
        if len(row_index_list) == 0:
            return None

        current_row_index = row_index_list[0]
        cur_close = float(ticker_data["close"][current_row_index])
        cur_volume = float(ticker_data["close"][current_row_index])
        prev_close = float(ticker_data["close"][current_row_index - 1]) if current_row_index > 0 else 0

        prev_1_obv = output_list[-1] if len(output_list) > 0 else None

        return cur_close, cur_volume, prev_close, prev_1_obv

    def formula(self, current_date: str, dict_input_dfs: Dict[str, pd.DataFrame], output_list: list):
        inputs = self.get_inputs(current_date, dict_input_dfs, output_list)
        if inputs is None:
            cur_obv = None
        else:
            cur_close, cur_volume, prev_close, prev_1_obv = inputs
            if prev_1_obv is None:
                cur_obv = 0
            else:
                cur_obv = prev_1_obv + np.sign(cur_close - prev_close) * cur_volume
        output_list.append(cur_obv)
        return output_list, cur_obv
# SECTION
tz = pytz.timezone('Asia/Ho_Chi_Minh')

def localize_utc_tz(d):
    return tz.fromutc(d)

indicator_dag = DAG(
    dag_id='indicator_dag',
    default_args={
        'retries': 1
    },
    description="DAG for stock indicators computation",
    schedule_interval='15 15 * * 1-5',
    start_date=datetime.today().replace(tzinfo=tz),
    tags=['indicator'],
    user_defined_filters={
        'local_tz': localize_utc_tz,
    }
)

crawler_task = PythonOperator(task_id="crawler", python_callable=ticker_crawler.main, dag=indicator_dag)

def compute_rs():
    print("RS Miner executing...")
    # rs_miner = RSMiner(input_tables={"ticker_data": 20}, indicator_name="rs", recursive_range=0)
    # rs_miner.execute()
    return "RS Miner finish"

rs_task = PythonOperator(task_id="rs", python_callable=compute_rs, dag=indicator_dag)

def compute_rsi():
    print("RSI Miner executing")
    # rsi_miner = RSIMiner(input_tables={"rs_data": 0}, indicator_name="rsi", recursive_range=0)
    # rsi_miner.execute()
    return "RSI Miner finish"

rsi_task = PythonOperator(task_id="rsi", python_callable=compute_rsi, dag=indicator_dag)

def compute_obv():
    obv_miner = OBVMiner(input_tables={"ticker_data":0}, indicator_name="obv",
                                       recursive_range=1)
    obv_miner.execute()
    print("OBV Miner executing...")
    return "OBV_MINER"

obv_task = PythonOperator(task_id="obv", python_callable=compute_obv, dag=indicator_dag)
# SECTION
crawler_task >> (rs_task)
rs_task >> rsi_task

obv_task


import os
import json

def main(file_path, activated, cardio_circle, output_dir, name_rule):
    file_number = len(os.listdir(file_path))
    with open(file_path+"/"+os.listdir(file_path)[0], "r") as f:
        time_slice = -1
        for step, row in enumerate(f):
            time_slice += 1
            pass
        f.close()
    if time_slice >= 100:
        cardio_circle = cardio_circle / time_slice
    elif time_slice >= 90:
        print("WARNING: Only {} time-slice detected!".format(time_slice))
        cardio_circle = cardio_circle / time_slice
    else:
        print("WARNING: Only {} time-slice detected, please confirm the cardio circle...".format(time_slice))
        cardio_circle = input("Cardio Circle:")
    prof = [file_path, file_number, str(activated), cardio_circle, output_dir, name_rule]

    with open('./forward_difference_starter.bat', 'w') as file:
        file.write('set current_path=%~dp0')
        file.write('\n')
        file.write('start  %current_path%\\forward_difference.py %1 %2 %3 %4 %5 %6')
        file.close()

    with open('./global.json', 'w') as file:
        json.dump(prof, file)

    with open('./global.json', 'r') as file:
        data = json.load(file)

    for item in data[2]:
        para = "forward_difference_starter.bat {} {} {} {} {} {}"\
            .format(data[0], data[1], item, data[3], data[4], data[5])
        os.system(para)


#!/usr/bin/env python
# coding: utf-8

import os, datetime
import torch, pyro, numpy as np
torch.set_default_tensor_type(torch.cuda.FloatTensor)
from torch import tensor
import torch.nn as nn
import torchvision.transforms.functional as TF

import click

import swyft

DEVICE = 'cuda'

from utils import *

@click.command()
@click.option("--m",    type=int, default = 12, help="Exponent of subhalo mass.")
@click.option("--nsub", type=int, default = 1, help="Number of subhaloes.")
@click.option("--nsim", type=int, default = 100, help="Number of simulations to run.")

def run(m, nsub, nsim):
    time_start = datetime.datetime.now()

    # Set definitions (should go to click)
    system_name = "ngc4414"

    # Set utilities
    sim_name, sim_path = get_sim_path(m, nsub, nsim, system_name)

    torch.set_default_tensor_type(torch.cuda.FloatTensor)  # HACK
    CONFIG = get_config(system_name, str(nsub), str(m))
    torch.set_default_tensor_type(torch.FloatTensor)

    torch.set_default_tensor_type(torch.cuda.FloatTensor)  # HACK
    ppd = CONFIG.ppd()['model_trace'].nodes
    torch.set_default_tensor_type(torch.FloatTensor)

    prior, uv, lows, highs = get_prior(CONFIG)
    L = CONFIG.kwargs["defs"]["nx"]
    print(f'Image has L = {L}.')

    assert nsub == CONFIG.umodel.alphas["main"].sub.nsub
    if m > 4:
        assert all([i == pow(10, m) for i in ppd['main/sub/m_sub']['value']])
    else:
        print(f'm = {m} <= 0!', ppd['main/sub/m_sub']['value'])

    # Create Store
    pnames = [f'{z}_{i+1}' for i in range(nsub) for z in ['x', 'y', 'm']]
    print(pnames)
    simulator = swyft.Simulator(model = lambda v: simul(v, CONFIG),
                                pnames = pnames,
                                sim_shapes={"image": (L, L)})

    store = swyft.DirectoryStore(path = sim_path, simulator = simulator)
    store.add(nsim, prior)
    store.simulate()

    print('Done!')
    print(f"Total creating time is {str(datetime.datetime.now() - time_start).split('.')[0]}!")

if __name__ == "__main__":
    run()


from static.python.full_sass import do_full_sass
import os
import subprocess

scss_Folder = 'static/scss'
css_Folder = 'static/css'

if __name__=='__main__':

    def do_initialize(css):
        new_css_Folder = css
        os.system('pip install -r requirements.txt')
        os.system('npm install')
        try:
            subprocess.check_call('sass', shell=True)
            do_full_sass()
        except subprocess.CalledProcessError:
            print("Error: 'sass' is not recognized as a command.")

            new_css_Folder = 'static/scss'
            print("Need sass variable in PATH!")
        return new_css_Folder

    def do_full_sass():
       os.system(f'py static/python/full_sass.py {scss_Folder} {css_Folder}')

    _ = "New-Item -Path $profile -Type File -Force"
    _ = "notepad $PROFILE"

    do_initialize(css_Folder)


import logging

LogFile = 'EmployeeLog'
LogFileMode = 'a+'
LoggerName = __name__
LogLevel = logging.DEBUG
LogFormat = '%(asctime)s %(filename)s %(**%(levelname)s** %(message)s'

logger = logging.getLogger(__name__)
logger.setLevel(LogLevel)

formatter = logging.Formatter(LogFormat)

handler = logging.FileHandler(LogFile)
handler.setFormatter(formatter)

logger.addHandler(handler)

class Employee:
    def __init__(self, eid, name):
        self.eid = eid
        self.name = name

        logger.info("Created new Employee : Name : {} Employee Id : {}".format(self.name, self.eid))

def main():
    emp1 = Employee('Nehal', 'IBE1117')
    emp2 = Employee('Kumar', 'IBE1118')

    print(emp1)
    print(emp2)

if __name__ == '__main__':
    main()


import time
import urllib.request
import urllib.error
import urllib.parse
import logging
import json
from arbitrage import config
from arbitrage.fiatconverter import FiatConverter
from arbitrage.utils import log_exception

class Market(object):
    def __init__(self, currency, cryptowatch_code=None):
        self.name = self.__class__.__name__
        self.currency = currency
        self.cryptowatch_code = cryptowatch_code
        self.cryptowatch_price = 0
        self.depth_updated = 0
        self.update_rate = 60
        self.fc = FiatConverter()
        self.fc.update()

    def get_cryptowatch_price(self):
        if not self.cryptowatch_code:
            return

        url = ('https://api.cryptowat.ch/markets/' +
            self.name.lower().replace('usd', '').replace('eur', '') +
            '/' + self.cryptowatch_code + '/price')

        res = urllib.request.urlopen(url)
        jsonstr = res.read().decode('utf8')
        try:
            price = json.loads(jsonstr)
        except Exception:
            logging.error("Cryptowatch %s - Can't parse json: %s" % (self.name, jsonstr))
        self.cryptowatch_price = self.fc.convert(price["result"]["price"], self.currency , "USD")

    def double_ckeck_price(self, price, direction, allowed_percent=None):
        if self.cryptowatch_price == 0:
            self.get_cryptowatch_price()

        allowed_percent = allowed_percent if allowed_percent else 10

        if abs(price - self.cryptowatch_price) > self.cryptowatch_price * allowed_percent / 100:
            logging.error("Big diff. @%s depth price(%s) vs Cryptowatch price %f / %f" %
                          (self.name, direction, price, self.cryptowatch_price))
            return False

        return True

    def get_depth(self):
        timediff = time.time() - self.depth_updated
        if timediff > self.update_rate:
            self.ask_update_depth()
        timediff = time.time() - self.depth_updated
        if timediff > config.market_expiration_time:
            logging.warning('Market: %s order book is expired' % self.name)
            self.depth = {'asks': [{'price': 0, 'amount': 0}], 'bids': [
                {'price': 0, 'amount': 0}]}
        return self.depth

    def convert_to_usd(self):
        if self.currency == "USD":
            return
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                # self.double_ckeck_price(order["price"], direction, order)
                # we don't do it here any more
                order["price"] = self.fc.convert(order["price"], self.currency, "USD")

    # there are some prices inside the market depth that are ment to de destabilize the market
    # clear them out
    def sort_out_market_crush_prices(self):
        new_depth = {'asks': [], 'bids': []}
        for direction in ("asks", "bids"):
            for order in self.depth[direction]:
                if self.double_ckeck_price(order["price"], direction, 30):
                    new_depth[direction].append(order)

        if len(new_depth["ask"]) != len(self.depth["ask"]) or len(new_depth["bids"]) != len(self.depth["bids"]):
            logging.warning('Market: %s removed some market crush crush items' % self.name)

        self.depth = new_depth

    def ask_update_depth(self):
        try:
            self.update_depth()
            self.convert_to_usd()
            self.get_cryptowatch_price()
            self.depth_updated = time.time()
        except (urllib.error.HTTPError, urllib.error.URLError) as e:
            logging.error("HTTPError, can't update market: %s" % self.name)
            log_exception(logging.DEBUG)
        except Exception as e:
            logging.error("Can't update market: %s - %s" % (self.name, str(e)))
            log_exception(logging.DEBUG)

    def get_ticker(self):
        depth = self.get_depth()
        res = {'ask': 0, 'bid': 0}
        if len(depth['asks']) > 0 and len(depth["bids"]) > 0:
            res = {'ask': depth['asks'][0],
                   'bid': depth['bids'][0]}
        return res

    ## Abstract methods
    def update_depth(self):
        pass

    def buy(self, price, amount):
        pass

    def sell(self, price, amount):
        pass


import os
import sys

import torch
from pytorch_lightning import LightningModule, Trainer, seed_everything

# loop = True;

# inferTest 불러오기
root_path = sys.path[0]
model_path = os.path.join(root_path, '../models')
sys.path.append(model_path)
# inferTest 불러오기

from single_task_model import Model
from clean import clean

MODEL_PATH = "../checkpoint/single_kcbert-l_0.000005_0/epoch=2-val_loss=0.07.ckpt"
#HPARAMS_PATH = "./lightning_logs/version_2/hparams.yaml"

from glob import glob

latest_ckpt = sorted(glob(MODEL_PATH))[0]
#model = trainning.Model.load_from_checkpoint(latest_ckpt, hparams_file=HPARAMS_PATH)
model = Model.load_from_checkpoint(latest_ckpt)

model.eval()
#map location 해주면 환경 바뀌어도 가능
def main():
    while True:
        sentence = input("문장을 입력하시오! ")
        judge(sentence=sentence)

def infer(x):
    return model(**model.tokenizer(x, return_tensors='pt'))
def judge(sentence):
    sentence=str(sentence)
    if sentence == "":
        print("빈 문장")
    else:
        LABEL_COLUMNS=["욕설","모욕","폭력위협/범죄조장","외설","성혐오","연령","인종/출신지","장애","종교","정치성향","직업혐오"]
        test_prediction = infer(clean(sentence))
        output = torch.sigmoid(test_prediction.logits)
        output = output.detach().flatten().numpy()
        for i in zip(LABEL_COLUMNS, output):
            if i[1] > 0.5:
                return 1
                #print(i)
                #print(f'probability : {prediction}')
        return 0

if __name__ == "__main__":
    main()


from tendims import TenDimensionsClassifier
from flask import Flask, request, redirect , jsonify ,send_from_directory
from flask_cors import CORS
import json

# Load models
models_dir = 'models/lstm_trained_models'
embeddings_dir = 'embeddings'  # change urls to embeddings dir
success_model_file = 'models/meeting_success/xgboost_10dims_success_prediction_model.dat'
model = TenDimensionsClassifier(models_dir=models_dir, embeddings_dir=embeddings_dir)
success_predictor = SuccessPredictor(success_model_file)
print('Models loaded')

app = Flask(__name__)

@app.route("/tenDimensions", methods=['POST'])
def tenDimensions():
        text = [request.form['text']]
        print(f"received Text : {sentences}")
        try:
                # you can give in input one string of text
                # dimensions = None extracts all dimensions
                tendim_scores = model.compute_score(text, dimensions=None)
                success_probability = success_predictor.predict_success(tendim_scores)
                tendim_scores['success'] = success_probability
                return jsonify(scores)
        except Exception as e:
                print (e)
                print("Something went wrong while opening summary")
                return '400'

if __name__ == "__main__":
        CORS(app)
        app.run(host="0.0.0.0",port=5000,threaded=True)


import errno
import json
import os
import sys
import time

import numpy as np
import torch

def rand(a=0., b=1.):
    return np.random.rand() * (b - a) + a

def get_path_root():
    '''os.path.dirname(os.path.abspath(__file__))'''
    debug_vars = dict((a, b) for a, b in os.environ.items() if a.find('IPYTHONENABLE') >= 0)
    # 根据不同场景获取根目录
    if len(debug_vars) > 0:
        """当前为debug运行时"""
        path_root = sys.path[2]
    elif getattr(sys, 'frozen', False):
        """当前为exe运行时"""
        path_root = os.getcwd()
    else:
        """正常执行"""
        path_root = sys.path[1]
    path_root = path_root.replace("\\", "/")  # 替换斜杠
    return path_root

def is_float(str):
    from f_tools.GLOBAL_LOG import flog
    if str.count('.') == 1:  # 小数有且仅有一个小数点
        left = str.split('.')[0]  # 小数点左边（整数位，可为正或负）
        right = str.split('.')[1]  # 小数点右边（小数位，一定为正）
        lright = ''  # 取整数位的绝对值（排除掉负号）
        if str.count('-') == 1 and str[0] == '-':  # 如果整数位为负，则第一个元素一定是负号
            lright = left.split('-')[1]
        elif str.count('-') == 0:
            lright = left
        else:
            flog.debug('%s 不是小数' % str)
            return False
        if right.isdigit() and lright.isdigit():  # 判断整数位的绝对值和小数位是否全部为数字
            flog.debug('%s 是小数' % str)
            return True
        else:
            flog.debug('%s 不是小数' % str)
            return False

    else:
        flog.debug('%s 不是小数' % str)
        return False

def show_time(f, *arg):
    import time
    from f_tools.GLOBAL_LOG import flog

    start = time.time()
    flog.debug('show_time---开始---%s-------' % (f.__name__))
    ret = f(*arg)
    flog.debug('show_time---完成---%s---%s' % (f.__name__, time.time() - start))
    return ret

def show_progress(epoch, epochs, title, context):
    '''
    完成后在循环的外层需要补一个        print()
    '''
    rate = (epoch + 1) / epochs
    a = "*" * int(rate * 50)
    b = "." * int((1 - rate) * 50)
    time.sleep(0.2)
    print("\r{}: {:^3.0f}%[{}->{}] {}".format(title, int(rate * 100), a, b, context), end="")

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(NpEncoder, self).default(obj)

def mkdir(path):
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

def labels2onehot4ts(labels, num_class):
    '''
    这里是必须0开始  需labels-1  通常 label 从1开始
    :param labels: 类别 index [,2,1,1,3,5]
    :param num_class:
    :return: torch.Size([1, 20])
    '''
    # labels = labels - 1
    batch = labels.shape[0]
    labels.resize_(batch, 1)  # labels[:,None]
    zeros = torch.zeros(batch, num_class, device=labels.device, dtype=torch.int64)
    return zeros.scatter_(1, labels.long(), 1)  # dim,index,value 数组索引

class Dict2Obj(object):
    def __init__(self, d):
        for a, b in d.items():
            if isinstance(b, (list, tuple)):
                setattr(self, a, [Dict2Obj(x) if isinstance(x, dict) else x for x in b])
            else:
                setattr(self, a, Dict2Obj(b) if isinstance(b, dict) else b)

if __name__ == "__main__":
    pass


from typing import Optional

from pydantic import BaseModel

class FormattedBody(BaseModel):
    title: str
    data: str
    description: Optional[str]

class MsgToSend(BaseModel):
    subject: str
    recipients: list[str]
    regular_body: Optional[str] = None
    formatted_body: Optional[FormattedBody] = None
    subtype: str = "html"

    def __pretty_recipients(self) -> str:
        return ", ".join([r for r in self.recipients])

    def __str__(self) -> str:
        recipients = self.__pretty_recipients()
        subj = self.subject

        if self.formatted_body:
            body_title = self.formatted_body.title
            body_data = self.formatted_body.data
            body_description = self.formatted_body.description
        else:
            body_title = ""
            body_data = ""
            body_description = ""

        return f"Recipients: {recipients}. Subject: {subj}, Title: {body_title}, Data: {body_data}, Description: {body_description}"


# Import flask dependencies
from flask import Blueprint, jsonify, request

# request, render_template, flash, g, session, redirect, url_for

from ..services import user_service
from ..schema.user_schema import user_schema, users_schema

user_controller = Blueprint('users', __name__, url_prefix='/users')

# Set the route and accepted methods

@user_controller.route('', methods=['GET'])
def get_all_users():
    # all_users = user_service.get_all_users()
    # return users_schema.jsonify(all_users), 200
    return jsonify({
        'hola': 'mundo'
    })

@user_controller.route('/add', methods=['POST'])
def add_user():
    post_data = request.get_json()

    if not post_data or 'username' not in request.args or 'email' not in request.args:
        response_object = {
            'status': 'fail',
            'message': 'Invalid payload'
        }
        return jsonify(response_object), 400

    username = post_data['username']
    email = post_data['email']
    try:
        new_user = user_service.add_user(username, email)
        return user_schema.jsonify(new_user), 200
    except Exception as e:
        response_object = {
            'status': 'fail',
            'message': e.args[0]
        }
        return jsonify(response_object), 500


import networkx as nx
import matplotlib.pyplot as plt

MNDA = nx.MultiGraph()

MNDA.add_nodes_from(["S1","S5"])
MNDA.add_edges_from([("S1","S2"),("S2","S3"),("S2","S4"),("S2","S5"),("S3","S4"),("S3","S4"),("S4","S5"),
                     ("S4","S5"),("S3","S5")])

nx.draw(MNDA, node_size = 1000, node_color = 'y', node_shape = 'o', with_labels=True)
plt.draw()
plt.savefig("MNDA.eps")
plt.show()


import argparse
import numpy as np

from data_loader import DataLoader
from rnn import RNN, rnn_training_step, init_params, sample
from optim import Adam

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', default='data/input.txt')
    parser.add_argument('--hidden_size', type=int, default=100)
    parser.add_argument('--seq_length', type=int, default=25)
    parser.add_argument('--sample_every', type=int, default=10000)
    parser.add_argument('--sample_size', type=int, default=200)
    parser.add_argument('--num_epochs', type=int, default=2)
    parser.add_argument('--lr', type=float, default=1e-3)
    parser.add_argument('--print_every', type=int, default=10000)
    return parser.parse_args()

def main():
    args = parse_args()
    with open(args.input, 'r') as fp:
        data_loader = DataLoader(fp.read(), batch_size=args.seq_length)

    rnn = RNN()
    params = init_params(data_loader.vocab_size, hidden_size=args.hidden_size)
    optimizer = Adam(params, lr=args.lr)
    it = 0
    for epoch in range(args.num_epochs):
        hidden_state = np.zeros((1, args.hidden_size))
        for x, y in data_loader:
            if it % args.sample_every == 0:
                one_hot = sample(rnn, hidden_state, x[0], params, args.sample_size)
                generated_text = data_loader.decode(one_hot)
                print(generated_text)
            loss, hidden_state, dparams = rnn_training_step(rnn, hidden_state, x, y, params)
            if it % args.print_every == 0:
                print('iteration: {}, loss: {}'.format(it, loss))
            optimizer.step(dparams)
            it += 1

if __name__ == '__main__':
    main()


from django.urls import path,include
from . import views
from rest_framework import routers,authtoken

router = routers.DefaultRouter()
router.register(r'movies', views.AddMovie)
router.register(r'movies_timings', views.AddTimings)
router.register('user', views.userviewsets)

urlpatterns = [
    path('',include(router.urls)),
    path('api-token-auth/', authtoken.views.obtain_auth_token),
    path('purchase/<int:id>/',views.purchase,name='purchase'),
    path('total_admin/',views.total_admin,name='total_admin'),
]


import pygame
from projectile import Projectile
from game_object import GameObject

class Invader(GameObject):

    MOVING_RIGHT = 0
    MOVING_RDOWN = 1
    MOVING_LEFT = 2
    MOVING_LDOWN = 3

    'Inits the invader'
    def __init__(self, projectile_list, x, y, max_side_move, max_down_move = 40):
        GameObject.__init__(self, x, y, 30, 30)
        self.speed = 0.05
        self.direction = self.MOVING_RIGHT

        self.projectile_list = projectile_list

        self.max_down_move = max_down_move
        self.max_side_move = max_side_move
        self._moved = max_side_move

        self.fire_delay = 0

    'Calculates the invader move'
    def _move(self, dt):
        #Quantifies the amount it must move
        move = round(self.speed*dt)
        self._moved -= move
        if self._moved < 0:
            move += self._moved

        #moves depending in the current direction
        if self.direction == self.MOVING_RIGHT:
            self.box.x += move
        elif self.direction == self.MOVING_RDOWN or self.direction == self.MOVING_LDOWN:
            self.box.y += move
        elif self.direction == self.MOVING_LEFT:
            self.box.x -= move

        #Determines the next direction
        if self._moved < 0:
            self._next_direction()

    def _next_direction(self):
        #sets the direction it will move to
        self.direction += 1
        if self.direction >3:
            self.direction = 0

        #sets the amount it must move
        if self.direction == self.MOVING_RDOWN or self.direction == self.MOVING_LDOWN:
            self._moved = self.max_down_move
        else:
            self._moved = self.max_side_move

    'Maybe used to speed up the invader velocity'
    def speedUp(self, amount = 1):
        self.speed += amount

    'Calculates the invader shoot'
    def _shoot(self, dt):
        #create a new projectile object moving downward
        if self.fire_delay > 30:
            self.fire_delay = 0

            self.projectile_list.append(Projectile(self.box.x+12.25, self.box.y+15, 2.5))

        #increases the fire time
        else:
            self.fire_delay += 0.01*dt

    '''
    update() is called by the manager class individually for each invader, and will handle calling
    that invader's _move() and _shoot()
    '''
    def update(self, dt):
        self._move(dt)
        self._shoot(dt)

    def render(self, screen):
        pygame.draw.rect(screen, pygame.Color(230,230,230),
                         (self.box.x, self.box.y, self.box.width, self.box.height))


import django

PROJECT_APPS = (
    "django_fsm",
    "testapp",
)

INSTALLED_APPS = (
    "django.contrib.contenttypes",
    "django.contrib.auth",
    "guardian",
) + PROJECT_APPS

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",  # this is default
    "guardian.backends.ObjectPermissionBackend",
)

DATABASE_ENGINE = "sqlite3"
SECRET_KEY = "nokey"
MIDDLEWARE_CLASSES = ()
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
    }
}

if django.VERSION < (1, 9):

    class DisableMigrations(object):
        def __contains__(self, item):
            return True

        def __getitem__(self, item):
            return "notmigrations"

    MIGRATION_MODULES = DisableMigrations()
else:
    MIGRATION_MODULES = {
        "auth": None,
        "contenttypes": None,
        "guardian": None,
    }

ANONYMOUS_USER_ID = 0

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"


#!/usr/bin/env python
# coding: Latin-1

# sudo pip3 install evdev
# sudo pip3 install explorerhat

# if you see the following erro rthen ther is no keyboard connected:

# keyboard = devices[0]
#       IndexError: list index out of range

# Load library functions we want
from evdev import InputDevice, list_devices, ecodes
from explorerhat import motor

# Depend on which way round your wires are to each motor pair these may need to be a negative number
LEFT_MOTOR_MAX_POWER_FWD  = 100
RIGHT_MOTOR_MAX_POWER_FWD = 100

# Depending on which side of your robot wires are connected these might need swapping
left_motor = motor.one
right_motor = motor.two

def get_first_keyboard():

    # see: # https://gpiozero.readthedocs.io/en/stable/recipes.html#keyboard-controlled-robot

    # Get the list of available input devices
    devices = [InputDevice(device) for device in list_devices()]
    # Filter out everything that's not a keyboard. Keyboards are defined as any
    # device which has keys, and which specifically has keys 1..31 (roughly Esc,
    # the numeric keys, the first row of QWERTY plus a few more) and which does
    # *not* have key 0 (reserved)
    must_have = {i for i in range(1, 32)}
    must_not_have = {0}
    devices = [
        dev
        for dev in devices
        for keys in (set(dev.capabilities().get(ecodes.EV_KEY, [])),)
        if must_have.issubset(keys)
        and must_not_have.isdisjoint(keys)
    ]
    # Pick the first keyboard

    return devices[0]

# Motor Control

def forward():
    left_motor.speed(LEFT_MOTOR_MAX_POWER_FWD)
    right_motor.speed(RIGHT_MOTOR_MAX_POWER_FWD)

def backward():
    left_motor.speed(-LEFT_MOTOR_MAX_POWER_FWD)
    right_motor.speed(-RIGHT_MOTOR_MAX_POWER_FWD)

def left():
    left_motor.speed(-LEFT_MOTOR_MAX_POWER_FWD)
    right_motor.speed( RIGHT_MOTOR_MAX_POWER_FWD)

def right():
    left_motor.speed( LEFT_MOTOR_MAX_POWER_FWD)
    right_motor.speed(-RIGHT_MOTOR_MAX_POWER_FWD)

def stop():
    left_motor.speed(0)
    right_motor.speed(0)

keypress_actions = {
    # Arrow keys:
    ecodes.KEY_UP: forward,
    ecodes.KEY_DOWN: backward,
    ecodes.KEY_LEFT: left,
    ecodes.KEY_RIGHT: right,

    # 8BitDo 'keys'
    ecodes.KEY_C: forward,
    ecodes.KEY_D: backward,
    ecodes.KEY_E: left,
    ecodes.KEY_F: right,
}

try:
    print('Press CTRL+C to quit')
    keyboard = get_first_keyboard()

    for event in keyboard.read_loop():
        if event.type == ecodes.EV_KEY and event.code in keypress_actions:
            if event.value == 1:  # key down
                keypress_actions[event.code]()
            if event.value == 0:  # key up
                stop()

except KeyboardInterrupt:
    # CTRL+C exit, disable all drives
    print("stop")
    motor.stop()
print("Bye")